Bjorn Helgaas | 7328c8f | 2018-01-26 11:45:16 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 2 | /* |
Bjorn Helgaas | df62ab5 | 2018-03-09 16:36:33 -0600 | [diff] [blame] | 3 | * PCI Express I/O Virtualization (IOV) support |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 4 | * Single Root IOV 1.0 |
Yu Zhao | 302b421 | 2009-05-18 13:51:32 +0800 | [diff] [blame] | 5 | * Address Translation Service 1.0 |
Bjorn Helgaas | df62ab5 | 2018-03-09 16:36:33 -0600 | [diff] [blame] | 6 | * |
| 7 | * Copyright (C) 2009 Intel Corporation, Yu Zhao <yu.zhao@intel.com> |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <linux/pci.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 11 | #include <linux/slab.h> |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 12 | #include <linux/mutex.h> |
Paul Gortmaker | 363c75d | 2011-05-27 09:37:25 -0400 | [diff] [blame] | 13 | #include <linux/export.h> |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 14 | #include <linux/string.h> |
| 15 | #include <linux/delay.h> |
| 16 | #include "pci.h" |
| 17 | |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 18 | #define VIRTFN_ID_LEN 16 |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 19 | |
Wei Yang | b07579c | 2015-03-25 16:23:48 +0800 | [diff] [blame] | 20 | int pci_iov_virtfn_bus(struct pci_dev *dev, int vf_id) |
Yu Zhao | a28724b | 2009-03-20 11:25:13 +0800 | [diff] [blame] | 21 | { |
Wei Yang | b07579c | 2015-03-25 16:23:48 +0800 | [diff] [blame] | 22 | if (!dev->is_physfn) |
| 23 | return -EINVAL; |
Yu Zhao | a28724b | 2009-03-20 11:25:13 +0800 | [diff] [blame] | 24 | return dev->bus->number + ((dev->devfn + dev->sriov->offset + |
Wei Yang | b07579c | 2015-03-25 16:23:48 +0800 | [diff] [blame] | 25 | dev->sriov->stride * vf_id) >> 8); |
Yu Zhao | a28724b | 2009-03-20 11:25:13 +0800 | [diff] [blame] | 26 | } |
| 27 | |
Wei Yang | b07579c | 2015-03-25 16:23:48 +0800 | [diff] [blame] | 28 | int pci_iov_virtfn_devfn(struct pci_dev *dev, int vf_id) |
Yu Zhao | a28724b | 2009-03-20 11:25:13 +0800 | [diff] [blame] | 29 | { |
Wei Yang | b07579c | 2015-03-25 16:23:48 +0800 | [diff] [blame] | 30 | if (!dev->is_physfn) |
| 31 | return -EINVAL; |
Yu Zhao | a28724b | 2009-03-20 11:25:13 +0800 | [diff] [blame] | 32 | return (dev->devfn + dev->sriov->offset + |
Wei Yang | b07579c | 2015-03-25 16:23:48 +0800 | [diff] [blame] | 33 | dev->sriov->stride * vf_id) & 0xff; |
Yu Zhao | a28724b | 2009-03-20 11:25:13 +0800 | [diff] [blame] | 34 | } |
| 35 | |
Wei Yang | f59dca2 | 2015-03-25 16:23:46 +0800 | [diff] [blame] | 36 | /* |
| 37 | * Per SR-IOV spec sec 3.3.10 and 3.3.11, First VF Offset and VF Stride may |
| 38 | * change when NumVFs changes. |
| 39 | * |
| 40 | * Update iov->offset and iov->stride when NumVFs is written. |
| 41 | */ |
| 42 | static inline void pci_iov_set_numvfs(struct pci_dev *dev, int nr_virtfn) |
| 43 | { |
| 44 | struct pci_sriov *iov = dev->sriov; |
| 45 | |
| 46 | pci_write_config_word(dev, iov->pos + PCI_SRIOV_NUM_VF, nr_virtfn); |
| 47 | pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_OFFSET, &iov->offset); |
| 48 | pci_read_config_word(dev, iov->pos + PCI_SRIOV_VF_STRIDE, &iov->stride); |
| 49 | } |
| 50 | |
Wei Yang | 4449f07 | 2015-03-25 16:23:47 +0800 | [diff] [blame] | 51 | /* |
| 52 | * The PF consumes one bus number. NumVFs, First VF Offset, and VF Stride |
| 53 | * determine how many additional bus numbers will be consumed by VFs. |
| 54 | * |
Alexander Duyck | ea9a885 | 2015-10-29 16:20:50 -0500 | [diff] [blame] | 55 | * Iterate over all valid NumVFs, validate offset and stride, and calculate |
| 56 | * the maximum number of bus numbers that could ever be required. |
Wei Yang | 4449f07 | 2015-03-25 16:23:47 +0800 | [diff] [blame] | 57 | */ |
Alexander Duyck | ea9a885 | 2015-10-29 16:20:50 -0500 | [diff] [blame] | 58 | static int compute_max_vf_buses(struct pci_dev *dev) |
Wei Yang | 4449f07 | 2015-03-25 16:23:47 +0800 | [diff] [blame] | 59 | { |
| 60 | struct pci_sriov *iov = dev->sriov; |
Alexander Duyck | ea9a885 | 2015-10-29 16:20:50 -0500 | [diff] [blame] | 61 | int nr_virtfn, busnr, rc = 0; |
Wei Yang | 4449f07 | 2015-03-25 16:23:47 +0800 | [diff] [blame] | 62 | |
Alexander Duyck | ea9a885 | 2015-10-29 16:20:50 -0500 | [diff] [blame] | 63 | for (nr_virtfn = iov->total_VFs; nr_virtfn; nr_virtfn--) { |
Wei Yang | 4449f07 | 2015-03-25 16:23:47 +0800 | [diff] [blame] | 64 | pci_iov_set_numvfs(dev, nr_virtfn); |
Alexander Duyck | ea9a885 | 2015-10-29 16:20:50 -0500 | [diff] [blame] | 65 | if (!iov->offset || (nr_virtfn > 1 && !iov->stride)) { |
| 66 | rc = -EIO; |
| 67 | goto out; |
| 68 | } |
| 69 | |
Wei Yang | b07579c | 2015-03-25 16:23:48 +0800 | [diff] [blame] | 70 | busnr = pci_iov_virtfn_bus(dev, nr_virtfn - 1); |
Alexander Duyck | ea9a885 | 2015-10-29 16:20:50 -0500 | [diff] [blame] | 71 | if (busnr > iov->max_VF_buses) |
| 72 | iov->max_VF_buses = busnr; |
Wei Yang | 4449f07 | 2015-03-25 16:23:47 +0800 | [diff] [blame] | 73 | } |
| 74 | |
Alexander Duyck | ea9a885 | 2015-10-29 16:20:50 -0500 | [diff] [blame] | 75 | out: |
| 76 | pci_iov_set_numvfs(dev, 0); |
| 77 | return rc; |
Wei Yang | 4449f07 | 2015-03-25 16:23:47 +0800 | [diff] [blame] | 78 | } |
| 79 | |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 80 | static struct pci_bus *virtfn_add_bus(struct pci_bus *bus, int busnr) |
| 81 | { |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 82 | struct pci_bus *child; |
| 83 | |
| 84 | if (bus->number == busnr) |
| 85 | return bus; |
| 86 | |
| 87 | child = pci_find_bus(pci_domain_nr(bus), busnr); |
| 88 | if (child) |
| 89 | return child; |
| 90 | |
| 91 | child = pci_add_new_bus(bus, NULL, busnr); |
| 92 | if (!child) |
| 93 | return NULL; |
| 94 | |
Yinghai Lu | b7eac05 | 2012-05-17 18:51:13 -0700 | [diff] [blame] | 95 | pci_bus_insert_busn_res(child, busnr, busnr); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 96 | |
| 97 | return child; |
| 98 | } |
| 99 | |
Jiang Liu | dc087f2 | 2013-05-25 21:48:37 +0800 | [diff] [blame] | 100 | static void virtfn_remove_bus(struct pci_bus *physbus, struct pci_bus *virtbus) |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 101 | { |
Jiang Liu | dc087f2 | 2013-05-25 21:48:37 +0800 | [diff] [blame] | 102 | if (physbus != virtbus && list_empty(&virtbus->devices)) |
| 103 | pci_remove_bus(virtbus); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 104 | } |
| 105 | |
Wei Yang | 0e6c912 | 2015-03-25 16:23:44 +0800 | [diff] [blame] | 106 | resource_size_t pci_iov_resource_size(struct pci_dev *dev, int resno) |
| 107 | { |
| 108 | if (!dev->is_physfn) |
| 109 | return 0; |
| 110 | |
| 111 | return dev->sriov->barsz[resno - PCI_IOV_RESOURCES]; |
| 112 | } |
| 113 | |
KarimAllah Ahmed | cf0921b | 2018-03-19 21:06:00 +0100 | [diff] [blame] | 114 | static void pci_read_vf_config_common(struct pci_dev *virtfn) |
| 115 | { |
| 116 | struct pci_dev *physfn = virtfn->physfn; |
| 117 | |
| 118 | /* |
| 119 | * Some config registers are the same across all associated VFs. |
| 120 | * Read them once from VF0 so we can skip reading them from the |
| 121 | * other VFs. |
| 122 | * |
| 123 | * PCIe r4.0, sec 9.3.4.1, technically doesn't require all VFs to |
| 124 | * have the same Revision ID and Subsystem ID, but we assume they |
| 125 | * do. |
| 126 | */ |
| 127 | pci_read_config_dword(virtfn, PCI_CLASS_REVISION, |
| 128 | &physfn->sriov->class); |
| 129 | pci_read_config_byte(virtfn, PCI_HEADER_TYPE, |
| 130 | &physfn->sriov->hdr_type); |
| 131 | pci_read_config_word(virtfn, PCI_SUBSYSTEM_VENDOR_ID, |
| 132 | &physfn->sriov->subsystem_vendor); |
| 133 | pci_read_config_word(virtfn, PCI_SUBSYSTEM_ID, |
| 134 | &physfn->sriov->subsystem_device); |
| 135 | } |
| 136 | |
Jan H. Schönherr | 753f612 | 2017-09-26 12:53:23 -0500 | [diff] [blame] | 137 | int pci_iov_add_virtfn(struct pci_dev *dev, int id) |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 138 | { |
| 139 | int i; |
Jiang Liu | dc087f2 | 2013-05-25 21:48:37 +0800 | [diff] [blame] | 140 | int rc = -ENOMEM; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 141 | u64 size; |
| 142 | char buf[VIRTFN_ID_LEN]; |
| 143 | struct pci_dev *virtfn; |
| 144 | struct resource *res; |
| 145 | struct pci_sriov *iov = dev->sriov; |
Gu Zheng | 8b1fce0 | 2013-05-25 21:48:31 +0800 | [diff] [blame] | 146 | struct pci_bus *bus; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 147 | |
Wei Yang | b07579c | 2015-03-25 16:23:48 +0800 | [diff] [blame] | 148 | bus = virtfn_add_bus(dev->bus, pci_iov_virtfn_bus(dev, id)); |
Jiang Liu | dc087f2 | 2013-05-25 21:48:37 +0800 | [diff] [blame] | 149 | if (!bus) |
| 150 | goto failed; |
| 151 | |
| 152 | virtfn = pci_alloc_dev(bus); |
| 153 | if (!virtfn) |
| 154 | goto failed0; |
| 155 | |
Wei Yang | b07579c | 2015-03-25 16:23:48 +0800 | [diff] [blame] | 156 | virtfn->devfn = pci_iov_virtfn_devfn(dev, id); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 157 | virtfn->vendor = dev->vendor; |
Filippo Sironi | 3142d83 | 2017-08-28 15:38:49 +0200 | [diff] [blame] | 158 | virtfn->device = iov->vf_device; |
KarimAllah Ahmed | cf0921b | 2018-03-19 21:06:00 +0100 | [diff] [blame] | 159 | virtfn->is_virtfn = 1; |
| 160 | virtfn->physfn = pci_dev_get(dev); |
| 161 | |
| 162 | if (id == 0) |
| 163 | pci_read_vf_config_common(virtfn); |
| 164 | |
Po Liu | 156c553 | 2016-08-29 15:28:01 +0800 | [diff] [blame] | 165 | rc = pci_setup_device(virtfn); |
| 166 | if (rc) |
KarimAllah Ahmed | cf0921b | 2018-03-19 21:06:00 +0100 | [diff] [blame] | 167 | goto failed1; |
Po Liu | 156c553 | 2016-08-29 15:28:01 +0800 | [diff] [blame] | 168 | |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 169 | virtfn->dev.parent = dev->dev.parent; |
Alex Williamson | aa931977 | 2014-01-09 08:36:08 -0700 | [diff] [blame] | 170 | virtfn->multifunction = 0; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 171 | |
| 172 | for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { |
Bjorn Helgaas | c1fe1f9 | 2015-03-25 16:23:45 +0800 | [diff] [blame] | 173 | res = &dev->resource[i + PCI_IOV_RESOURCES]; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 174 | if (!res->parent) |
| 175 | continue; |
| 176 | virtfn->resource[i].name = pci_name(virtfn); |
| 177 | virtfn->resource[i].flags = res->flags; |
Wei Yang | 0e6c912 | 2015-03-25 16:23:44 +0800 | [diff] [blame] | 178 | size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 179 | virtfn->resource[i].start = res->start + size * id; |
| 180 | virtfn->resource[i].end = virtfn->resource[i].start + size - 1; |
| 181 | rc = request_resource(res, &virtfn->resource[i]); |
| 182 | BUG_ON(rc); |
| 183 | } |
| 184 | |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 185 | pci_device_add(virtfn, virtfn->bus); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 186 | |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 187 | sprintf(buf, "virtfn%u", id); |
| 188 | rc = sysfs_create_link(&dev->dev.kobj, &virtfn->dev.kobj, buf); |
| 189 | if (rc) |
KarimAllah Ahmed | cf0921b | 2018-03-19 21:06:00 +0100 | [diff] [blame] | 190 | goto failed2; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 191 | rc = sysfs_create_link(&virtfn->dev.kobj, &dev->dev.kobj, "physfn"); |
| 192 | if (rc) |
KarimAllah Ahmed | cf0921b | 2018-03-19 21:06:00 +0100 | [diff] [blame] | 193 | goto failed3; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 194 | |
| 195 | kobject_uevent(&virtfn->dev.kobj, KOBJ_CHANGE); |
| 196 | |
Stuart Hayes | 27d6162 | 2017-10-04 10:57:52 -0500 | [diff] [blame] | 197 | pci_bus_add_device(virtfn); |
| 198 | |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 199 | return 0; |
| 200 | |
KarimAllah Ahmed | cf0921b | 2018-03-19 21:06:00 +0100 | [diff] [blame] | 201 | failed3: |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 202 | sysfs_remove_link(&dev->dev.kobj, buf); |
KarimAllah Ahmed | cf0921b | 2018-03-19 21:06:00 +0100 | [diff] [blame] | 203 | failed2: |
| 204 | pci_stop_and_remove_bus_device(virtfn); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 205 | failed1: |
| 206 | pci_dev_put(dev); |
Jiang Liu | dc087f2 | 2013-05-25 21:48:37 +0800 | [diff] [blame] | 207 | failed0: |
| 208 | virtfn_remove_bus(dev->bus, bus); |
| 209 | failed: |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 210 | |
| 211 | return rc; |
| 212 | } |
| 213 | |
Jan H. Schönherr | 753f612 | 2017-09-26 12:53:23 -0500 | [diff] [blame] | 214 | void pci_iov_remove_virtfn(struct pci_dev *dev, int id) |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 215 | { |
| 216 | char buf[VIRTFN_ID_LEN]; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 217 | struct pci_dev *virtfn; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 218 | |
Jiang Liu | dc087f2 | 2013-05-25 21:48:37 +0800 | [diff] [blame] | 219 | virtfn = pci_get_domain_bus_and_slot(pci_domain_nr(dev->bus), |
Wei Yang | b07579c | 2015-03-25 16:23:48 +0800 | [diff] [blame] | 220 | pci_iov_virtfn_bus(dev, id), |
| 221 | pci_iov_virtfn_devfn(dev, id)); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 222 | if (!virtfn) |
| 223 | return; |
| 224 | |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 225 | sprintf(buf, "virtfn%u", id); |
| 226 | sysfs_remove_link(&dev->dev.kobj, buf); |
Yinghai Lu | 09cedbe | 2012-02-04 22:55:01 -0800 | [diff] [blame] | 227 | /* |
| 228 | * pci_stop_dev() could have been called for this virtfn already, |
| 229 | * so the directory for the virtfn may have been removed before. |
| 230 | * Double check to avoid spurious sysfs warnings. |
| 231 | */ |
| 232 | if (virtfn->dev.kobj.sd) |
| 233 | sysfs_remove_link(&virtfn->dev.kobj, "physfn"); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 234 | |
Yinghai Lu | 210647a | 2012-02-25 13:54:20 -0800 | [diff] [blame] | 235 | pci_stop_and_remove_bus_device(virtfn); |
Jiang Liu | dc087f2 | 2013-05-25 21:48:37 +0800 | [diff] [blame] | 236 | virtfn_remove_bus(dev->bus, virtfn->bus); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 237 | |
Jiang Liu | dc087f2 | 2013-05-25 21:48:37 +0800 | [diff] [blame] | 238 | /* balance pci_get_domain_bus_and_slot() */ |
| 239 | pci_dev_put(virtfn); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 240 | pci_dev_put(dev); |
| 241 | } |
| 242 | |
Kelsey Skunberg | aaee0c1 | 2019-08-13 14:45:13 -0600 | [diff] [blame^] | 243 | static ssize_t sriov_totalvfs_show(struct device *dev, |
| 244 | struct device_attribute *attr, |
| 245 | char *buf) |
| 246 | { |
| 247 | struct pci_dev *pdev = to_pci_dev(dev); |
| 248 | |
| 249 | return sprintf(buf, "%u\n", pci_sriov_get_totalvfs(pdev)); |
| 250 | } |
| 251 | |
| 252 | static ssize_t sriov_numvfs_show(struct device *dev, |
| 253 | struct device_attribute *attr, |
| 254 | char *buf) |
| 255 | { |
| 256 | struct pci_dev *pdev = to_pci_dev(dev); |
| 257 | |
| 258 | return sprintf(buf, "%u\n", pdev->sriov->num_VFs); |
| 259 | } |
| 260 | |
| 261 | /* |
| 262 | * num_vfs > 0; number of VFs to enable |
| 263 | * num_vfs = 0; disable all VFs |
| 264 | * |
| 265 | * Note: SRIOV spec does not allow partial VF |
| 266 | * disable, so it's all or none. |
| 267 | */ |
| 268 | static ssize_t sriov_numvfs_store(struct device *dev, |
| 269 | struct device_attribute *attr, |
| 270 | const char *buf, size_t count) |
| 271 | { |
| 272 | struct pci_dev *pdev = to_pci_dev(dev); |
| 273 | int ret; |
| 274 | u16 num_vfs; |
| 275 | |
| 276 | ret = kstrtou16(buf, 0, &num_vfs); |
| 277 | if (ret < 0) |
| 278 | return ret; |
| 279 | |
| 280 | if (num_vfs > pci_sriov_get_totalvfs(pdev)) |
| 281 | return -ERANGE; |
| 282 | |
| 283 | device_lock(&pdev->dev); |
| 284 | |
| 285 | if (num_vfs == pdev->sriov->num_VFs) |
| 286 | goto exit; |
| 287 | |
| 288 | /* is PF driver loaded w/callback */ |
| 289 | if (!pdev->driver || !pdev->driver->sriov_configure) { |
| 290 | pci_info(pdev, "Driver does not support SRIOV configuration via sysfs\n"); |
| 291 | ret = -ENOENT; |
| 292 | goto exit; |
| 293 | } |
| 294 | |
| 295 | if (num_vfs == 0) { |
| 296 | /* disable VFs */ |
| 297 | ret = pdev->driver->sriov_configure(pdev, 0); |
| 298 | goto exit; |
| 299 | } |
| 300 | |
| 301 | /* enable VFs */ |
| 302 | if (pdev->sriov->num_VFs) { |
| 303 | pci_warn(pdev, "%d VFs already enabled. Disable before enabling %d VFs\n", |
| 304 | pdev->sriov->num_VFs, num_vfs); |
| 305 | ret = -EBUSY; |
| 306 | goto exit; |
| 307 | } |
| 308 | |
| 309 | ret = pdev->driver->sriov_configure(pdev, num_vfs); |
| 310 | if (ret < 0) |
| 311 | goto exit; |
| 312 | |
| 313 | if (ret != num_vfs) |
| 314 | pci_warn(pdev, "%d VFs requested; only %d enabled\n", |
| 315 | num_vfs, ret); |
| 316 | |
| 317 | exit: |
| 318 | device_unlock(&pdev->dev); |
| 319 | |
| 320 | if (ret < 0) |
| 321 | return ret; |
| 322 | |
| 323 | return count; |
| 324 | } |
| 325 | |
| 326 | static ssize_t sriov_offset_show(struct device *dev, |
| 327 | struct device_attribute *attr, |
| 328 | char *buf) |
| 329 | { |
| 330 | struct pci_dev *pdev = to_pci_dev(dev); |
| 331 | |
| 332 | return sprintf(buf, "%u\n", pdev->sriov->offset); |
| 333 | } |
| 334 | |
| 335 | static ssize_t sriov_stride_show(struct device *dev, |
| 336 | struct device_attribute *attr, |
| 337 | char *buf) |
| 338 | { |
| 339 | struct pci_dev *pdev = to_pci_dev(dev); |
| 340 | |
| 341 | return sprintf(buf, "%u\n", pdev->sriov->stride); |
| 342 | } |
| 343 | |
| 344 | static ssize_t sriov_vf_device_show(struct device *dev, |
| 345 | struct device_attribute *attr, |
| 346 | char *buf) |
| 347 | { |
| 348 | struct pci_dev *pdev = to_pci_dev(dev); |
| 349 | |
| 350 | return sprintf(buf, "%x\n", pdev->sriov->vf_device); |
| 351 | } |
| 352 | |
| 353 | static ssize_t sriov_drivers_autoprobe_show(struct device *dev, |
| 354 | struct device_attribute *attr, |
| 355 | char *buf) |
| 356 | { |
| 357 | struct pci_dev *pdev = to_pci_dev(dev); |
| 358 | |
| 359 | return sprintf(buf, "%u\n", pdev->sriov->drivers_autoprobe); |
| 360 | } |
| 361 | |
| 362 | static ssize_t sriov_drivers_autoprobe_store(struct device *dev, |
| 363 | struct device_attribute *attr, |
| 364 | const char *buf, size_t count) |
| 365 | { |
| 366 | struct pci_dev *pdev = to_pci_dev(dev); |
| 367 | bool drivers_autoprobe; |
| 368 | |
| 369 | if (kstrtobool(buf, &drivers_autoprobe) < 0) |
| 370 | return -EINVAL; |
| 371 | |
| 372 | pdev->sriov->drivers_autoprobe = drivers_autoprobe; |
| 373 | |
| 374 | return count; |
| 375 | } |
| 376 | |
| 377 | static DEVICE_ATTR_RO(sriov_totalvfs); |
| 378 | static DEVICE_ATTR(sriov_numvfs, 0664, sriov_numvfs_show, sriov_numvfs_store); |
| 379 | static DEVICE_ATTR_RO(sriov_offset); |
| 380 | static DEVICE_ATTR_RO(sriov_stride); |
| 381 | static DEVICE_ATTR_RO(sriov_vf_device); |
| 382 | static DEVICE_ATTR(sriov_drivers_autoprobe, 0664, sriov_drivers_autoprobe_show, |
| 383 | sriov_drivers_autoprobe_store); |
| 384 | |
| 385 | static struct attribute *sriov_dev_attrs[] = { |
| 386 | &dev_attr_sriov_totalvfs.attr, |
| 387 | &dev_attr_sriov_numvfs.attr, |
| 388 | &dev_attr_sriov_offset.attr, |
| 389 | &dev_attr_sriov_stride.attr, |
| 390 | &dev_attr_sriov_vf_device.attr, |
| 391 | &dev_attr_sriov_drivers_autoprobe.attr, |
| 392 | NULL, |
| 393 | }; |
| 394 | |
| 395 | static umode_t sriov_attrs_are_visible(struct kobject *kobj, |
| 396 | struct attribute *a, int n) |
| 397 | { |
| 398 | struct device *dev = kobj_to_dev(kobj); |
| 399 | |
| 400 | if (!dev_is_pf(dev)) |
| 401 | return 0; |
| 402 | |
| 403 | return a->mode; |
| 404 | } |
| 405 | |
| 406 | const struct attribute_group sriov_dev_attr_group = { |
| 407 | .attrs = sriov_dev_attrs, |
| 408 | .is_visible = sriov_attrs_are_visible, |
| 409 | }; |
| 410 | |
Wei Yang | 995df52 | 2015-03-25 16:23:49 +0800 | [diff] [blame] | 411 | int __weak pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs) |
| 412 | { |
Alexander Duyck | a39e3fc | 2015-10-29 16:21:11 -0500 | [diff] [blame] | 413 | return 0; |
| 414 | } |
| 415 | |
| 416 | int __weak pcibios_sriov_disable(struct pci_dev *pdev) |
| 417 | { |
| 418 | return 0; |
Wei Yang | 995df52 | 2015-03-25 16:23:49 +0800 | [diff] [blame] | 419 | } |
| 420 | |
Sebastian Ott | 18f9e9d | 2018-12-21 15:14:18 +0100 | [diff] [blame] | 421 | static int sriov_add_vfs(struct pci_dev *dev, u16 num_vfs) |
| 422 | { |
| 423 | unsigned int i; |
| 424 | int rc; |
| 425 | |
Sebastian Ott | aff68a5a | 2018-12-21 15:14:19 +0100 | [diff] [blame] | 426 | if (dev->no_vf_scan) |
| 427 | return 0; |
| 428 | |
Sebastian Ott | 18f9e9d | 2018-12-21 15:14:18 +0100 | [diff] [blame] | 429 | for (i = 0; i < num_vfs; i++) { |
| 430 | rc = pci_iov_add_virtfn(dev, i); |
| 431 | if (rc) |
| 432 | goto failed; |
| 433 | } |
| 434 | return 0; |
| 435 | failed: |
| 436 | while (i--) |
| 437 | pci_iov_remove_virtfn(dev, i); |
| 438 | |
| 439 | return rc; |
| 440 | } |
| 441 | |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 442 | static int sriov_enable(struct pci_dev *dev, int nr_virtfn) |
| 443 | { |
| 444 | int rc; |
Alexander Duyck | 3443c38 | 2015-10-29 16:21:05 -0500 | [diff] [blame] | 445 | int i; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 446 | int nres; |
Alexander Duyck | ce288ec | 2015-10-29 16:20:57 -0500 | [diff] [blame] | 447 | u16 initial; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 448 | struct resource *res; |
| 449 | struct pci_dev *pdev; |
| 450 | struct pci_sriov *iov = dev->sriov; |
Ram Pai | bbef98a | 2011-11-06 10:33:10 +0800 | [diff] [blame] | 451 | int bars = 0; |
Wei Yang | b07579c | 2015-03-25 16:23:48 +0800 | [diff] [blame] | 452 | int bus; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 453 | |
| 454 | if (!nr_virtfn) |
| 455 | return 0; |
| 456 | |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 457 | if (iov->num_VFs) |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 458 | return -EINVAL; |
| 459 | |
| 460 | pci_read_config_word(dev, iov->pos + PCI_SRIOV_INITIAL_VF, &initial); |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 461 | if (initial > iov->total_VFs || |
| 462 | (!(iov->cap & PCI_SRIOV_CAP_VFM) && (initial != iov->total_VFs))) |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 463 | return -EIO; |
| 464 | |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 465 | if (nr_virtfn < 0 || nr_virtfn > iov->total_VFs || |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 466 | (!(iov->cap & PCI_SRIOV_CAP_VFM) && (nr_virtfn > initial))) |
| 467 | return -EINVAL; |
| 468 | |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 469 | nres = 0; |
| 470 | for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { |
Ram Pai | bbef98a | 2011-11-06 10:33:10 +0800 | [diff] [blame] | 471 | bars |= (1 << (i + PCI_IOV_RESOURCES)); |
Bjorn Helgaas | c1fe1f9 | 2015-03-25 16:23:45 +0800 | [diff] [blame] | 472 | res = &dev->resource[i + PCI_IOV_RESOURCES]; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 473 | if (res->parent) |
| 474 | nres++; |
| 475 | } |
| 476 | if (nres != iov->nres) { |
Frederick Lawler | 7506dc7 | 2018-01-18 12:55:24 -0600 | [diff] [blame] | 477 | pci_err(dev, "not enough MMIO resources for SR-IOV\n"); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 478 | return -ENOMEM; |
| 479 | } |
| 480 | |
Wei Yang | b07579c | 2015-03-25 16:23:48 +0800 | [diff] [blame] | 481 | bus = pci_iov_virtfn_bus(dev, nr_virtfn - 1); |
Bjorn Helgaas | 68f8e9f | 2015-03-25 16:23:42 +0800 | [diff] [blame] | 482 | if (bus > dev->bus->busn_res.end) { |
Frederick Lawler | 7506dc7 | 2018-01-18 12:55:24 -0600 | [diff] [blame] | 483 | pci_err(dev, "can't enable %d VFs (bus %02x out of range of %pR)\n", |
Bjorn Helgaas | 68f8e9f | 2015-03-25 16:23:42 +0800 | [diff] [blame] | 484 | nr_virtfn, bus, &dev->bus->busn_res); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 485 | return -ENOMEM; |
| 486 | } |
| 487 | |
Ram Pai | bbef98a | 2011-11-06 10:33:10 +0800 | [diff] [blame] | 488 | if (pci_enable_resources(dev, bars)) { |
Frederick Lawler | 7506dc7 | 2018-01-18 12:55:24 -0600 | [diff] [blame] | 489 | pci_err(dev, "SR-IOV: IOV BARS not allocated\n"); |
Ram Pai | bbef98a | 2011-11-06 10:33:10 +0800 | [diff] [blame] | 490 | return -ENOMEM; |
| 491 | } |
| 492 | |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 493 | if (iov->link != dev->devfn) { |
| 494 | pdev = pci_get_slot(dev->bus, iov->link); |
| 495 | if (!pdev) |
| 496 | return -ENODEV; |
| 497 | |
Jiang Liu | dc087f2 | 2013-05-25 21:48:37 +0800 | [diff] [blame] | 498 | if (!pdev->is_physfn) { |
| 499 | pci_dev_put(pdev); |
Stefan Assmann | 652d110 | 2013-07-31 16:47:56 -0600 | [diff] [blame] | 500 | return -ENOSYS; |
Jiang Liu | dc087f2 | 2013-05-25 21:48:37 +0800 | [diff] [blame] | 501 | } |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 502 | |
| 503 | rc = sysfs_create_link(&dev->dev.kobj, |
| 504 | &pdev->dev.kobj, "dep_link"); |
Jiang Liu | dc087f2 | 2013-05-25 21:48:37 +0800 | [diff] [blame] | 505 | pci_dev_put(pdev); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 506 | if (rc) |
| 507 | return rc; |
| 508 | } |
| 509 | |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 510 | iov->initial_VFs = initial; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 511 | if (nr_virtfn < initial) |
| 512 | initial = nr_virtfn; |
| 513 | |
Alexander Duyck | c23b613 | 2015-10-29 16:21:20 -0500 | [diff] [blame] | 514 | rc = pcibios_sriov_enable(dev, initial); |
| 515 | if (rc) { |
Frederick Lawler | 7506dc7 | 2018-01-18 12:55:24 -0600 | [diff] [blame] | 516 | pci_err(dev, "failure %d from pcibios_sriov_enable()\n", rc); |
Alexander Duyck | c23b613 | 2015-10-29 16:21:20 -0500 | [diff] [blame] | 517 | goto err_pcibios; |
Wei Yang | 995df52 | 2015-03-25 16:23:49 +0800 | [diff] [blame] | 518 | } |
| 519 | |
Gavin Shan | f40ec3c | 2016-10-26 12:15:35 +1100 | [diff] [blame] | 520 | pci_iov_set_numvfs(dev, nr_virtfn); |
| 521 | iov->ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE; |
| 522 | pci_cfg_access_lock(dev); |
| 523 | pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl); |
| 524 | msleep(100); |
| 525 | pci_cfg_access_unlock(dev); |
| 526 | |
Sebastian Ott | 18f9e9d | 2018-12-21 15:14:18 +0100 | [diff] [blame] | 527 | rc = sriov_add_vfs(dev, initial); |
| 528 | if (rc) |
| 529 | goto err_pcibios; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 530 | |
| 531 | kobject_uevent(&dev->dev.kobj, KOBJ_CHANGE); |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 532 | iov->num_VFs = nr_virtfn; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 533 | |
| 534 | return 0; |
| 535 | |
Alexander Duyck | c23b613 | 2015-10-29 16:21:20 -0500 | [diff] [blame] | 536 | err_pcibios: |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 537 | iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE); |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 538 | pci_cfg_access_lock(dev); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 539 | pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl); |
| 540 | ssleep(1); |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 541 | pci_cfg_access_unlock(dev); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 542 | |
Gavin Shan | 0fc690a | 2017-08-11 18:19:33 +1000 | [diff] [blame] | 543 | pcibios_sriov_disable(dev); |
| 544 | |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 545 | if (iov->link != dev->devfn) |
| 546 | sysfs_remove_link(&dev->dev.kobj, "dep_link"); |
| 547 | |
Alexander Duyck | b390864 | 2015-10-29 16:21:16 -0500 | [diff] [blame] | 548 | pci_iov_set_numvfs(dev, 0); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 549 | return rc; |
| 550 | } |
| 551 | |
Sebastian Ott | 18f9e9d | 2018-12-21 15:14:18 +0100 | [diff] [blame] | 552 | static void sriov_del_vfs(struct pci_dev *dev) |
| 553 | { |
| 554 | struct pci_sriov *iov = dev->sriov; |
| 555 | int i; |
| 556 | |
Sebastian Ott | aff68a5a | 2018-12-21 15:14:19 +0100 | [diff] [blame] | 557 | if (dev->no_vf_scan) |
| 558 | return; |
| 559 | |
Sebastian Ott | 18f9e9d | 2018-12-21 15:14:18 +0100 | [diff] [blame] | 560 | for (i = 0; i < iov->num_VFs; i++) |
| 561 | pci_iov_remove_virtfn(dev, i); |
| 562 | } |
| 563 | |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 564 | static void sriov_disable(struct pci_dev *dev) |
| 565 | { |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 566 | struct pci_sriov *iov = dev->sriov; |
| 567 | |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 568 | if (!iov->num_VFs) |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 569 | return; |
| 570 | |
Sebastian Ott | 18f9e9d | 2018-12-21 15:14:18 +0100 | [diff] [blame] | 571 | sriov_del_vfs(dev); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 572 | iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE); |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 573 | pci_cfg_access_lock(dev); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 574 | pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl); |
| 575 | ssleep(1); |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 576 | pci_cfg_access_unlock(dev); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 577 | |
Gavin Shan | 0fc690a | 2017-08-11 18:19:33 +1000 | [diff] [blame] | 578 | pcibios_sriov_disable(dev); |
| 579 | |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 580 | if (iov->link != dev->devfn) |
| 581 | sysfs_remove_link(&dev->dev.kobj, "dep_link"); |
| 582 | |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 583 | iov->num_VFs = 0; |
Wei Yang | f59dca2 | 2015-03-25 16:23:46 +0800 | [diff] [blame] | 584 | pci_iov_set_numvfs(dev, 0); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 585 | } |
| 586 | |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 587 | static int sriov_init(struct pci_dev *dev, int pos) |
| 588 | { |
Wei Yang | 0e6c912 | 2015-03-25 16:23:44 +0800 | [diff] [blame] | 589 | int i, bar64; |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 590 | int rc; |
| 591 | int nres; |
| 592 | u32 pgsz; |
Alexander Duyck | ea9a885 | 2015-10-29 16:20:50 -0500 | [diff] [blame] | 593 | u16 ctrl, total; |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 594 | struct pci_sriov *iov; |
| 595 | struct resource *res; |
| 596 | struct pci_dev *pdev; |
| 597 | |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 598 | pci_read_config_word(dev, pos + PCI_SRIOV_CTRL, &ctrl); |
| 599 | if (ctrl & PCI_SRIOV_CTRL_VFE) { |
| 600 | pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, 0); |
| 601 | ssleep(1); |
| 602 | } |
| 603 | |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 604 | ctrl = 0; |
| 605 | list_for_each_entry(pdev, &dev->bus->devices, bus_list) |
| 606 | if (pdev->is_physfn) |
| 607 | goto found; |
| 608 | |
| 609 | pdev = NULL; |
| 610 | if (pci_ari_enabled(dev->bus)) |
| 611 | ctrl |= PCI_SRIOV_CTRL_ARI; |
| 612 | |
| 613 | found: |
| 614 | pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, ctrl); |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 615 | |
Ben Shelton | ff45f9d | 2015-10-29 16:20:31 -0500 | [diff] [blame] | 616 | pci_read_config_word(dev, pos + PCI_SRIOV_TOTAL_VF, &total); |
| 617 | if (!total) |
| 618 | return 0; |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 619 | |
| 620 | pci_read_config_dword(dev, pos + PCI_SRIOV_SUP_PGSIZE, &pgsz); |
| 621 | i = PAGE_SHIFT > 12 ? PAGE_SHIFT - 12 : 0; |
| 622 | pgsz &= ~((1 << i) - 1); |
| 623 | if (!pgsz) |
| 624 | return -EIO; |
| 625 | |
| 626 | pgsz &= ~(pgsz - 1); |
Vaidyanathan Srinivasan | 8161fe9 | 2012-02-02 23:11:20 +0530 | [diff] [blame] | 627 | pci_write_config_dword(dev, pos + PCI_SRIOV_SYS_PGSIZE, pgsz); |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 628 | |
Wei Yang | 0e6c912 | 2015-03-25 16:23:44 +0800 | [diff] [blame] | 629 | iov = kzalloc(sizeof(*iov), GFP_KERNEL); |
| 630 | if (!iov) |
| 631 | return -ENOMEM; |
| 632 | |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 633 | nres = 0; |
| 634 | for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { |
Bjorn Helgaas | c1fe1f9 | 2015-03-25 16:23:45 +0800 | [diff] [blame] | 635 | res = &dev->resource[i + PCI_IOV_RESOURCES]; |
David Daney | 1118399 | 2015-10-29 17:35:40 -0500 | [diff] [blame] | 636 | /* |
| 637 | * If it is already FIXED, don't change it, something |
| 638 | * (perhaps EA or header fixups) wants it this way. |
| 639 | */ |
| 640 | if (res->flags & IORESOURCE_PCI_FIXED) |
| 641 | bar64 = (res->flags & IORESOURCE_MEM_64) ? 1 : 0; |
| 642 | else |
| 643 | bar64 = __pci_read_base(dev, pci_bar_unknown, res, |
| 644 | pos + PCI_SRIOV_BAR + i * 4); |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 645 | if (!res->flags) |
| 646 | continue; |
| 647 | if (resource_size(res) & (PAGE_SIZE - 1)) { |
| 648 | rc = -EIO; |
| 649 | goto failed; |
| 650 | } |
Wei Yang | 0e6c912 | 2015-03-25 16:23:44 +0800 | [diff] [blame] | 651 | iov->barsz[i] = resource_size(res); |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 652 | res->end = res->start + resource_size(res) * total - 1; |
Frederick Lawler | 7506dc7 | 2018-01-18 12:55:24 -0600 | [diff] [blame] | 653 | pci_info(dev, "VF(n) BAR%d space: %pR (contains BAR%d for %d VFs)\n", |
Wei Yang | e88ae01 | 2015-03-25 16:23:43 +0800 | [diff] [blame] | 654 | i, res, i, total); |
Wei Yang | 0e6c912 | 2015-03-25 16:23:44 +0800 | [diff] [blame] | 655 | i += bar64; |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 656 | nres++; |
| 657 | } |
| 658 | |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 659 | iov->pos = pos; |
| 660 | iov->nres = nres; |
| 661 | iov->ctrl = ctrl; |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 662 | iov->total_VFs = total; |
Jakub Kicinski | 8d85a7a | 2018-05-25 08:18:34 -0500 | [diff] [blame] | 663 | iov->driver_max_VFs = total; |
Filippo Sironi | 3142d83 | 2017-08-28 15:38:49 +0200 | [diff] [blame] | 664 | pci_read_config_word(dev, pos + PCI_SRIOV_VF_DID, &iov->vf_device); |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 665 | iov->pgsz = pgsz; |
| 666 | iov->self = dev; |
Bodong Wang | 0e7df22 | 2017-04-13 01:51:40 +0300 | [diff] [blame] | 667 | iov->drivers_autoprobe = true; |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 668 | pci_read_config_dword(dev, pos + PCI_SRIOV_CAP, &iov->cap); |
| 669 | pci_read_config_byte(dev, pos + PCI_SRIOV_FUNC_LINK, &iov->link); |
Yijing Wang | 62f87c0 | 2012-07-24 17:20:03 +0800 | [diff] [blame] | 670 | if (pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END) |
Yu Zhao | 4d135db | 2009-05-20 17:11:57 +0800 | [diff] [blame] | 671 | iov->link = PCI_DEVFN(PCI_SLOT(dev->devfn), iov->link); |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 672 | |
| 673 | if (pdev) |
| 674 | iov->dev = pci_dev_get(pdev); |
Yu Zhao | e277d2f | 2009-05-18 13:51:33 +0800 | [diff] [blame] | 675 | else |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 676 | iov->dev = dev; |
Yu Zhao | e277d2f | 2009-05-18 13:51:33 +0800 | [diff] [blame] | 677 | |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 678 | dev->sriov = iov; |
| 679 | dev->is_physfn = 1; |
Alexander Duyck | ea9a885 | 2015-10-29 16:20:50 -0500 | [diff] [blame] | 680 | rc = compute_max_vf_buses(dev); |
| 681 | if (rc) |
| 682 | goto fail_max_buses; |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 683 | |
| 684 | return 0; |
| 685 | |
Alexander Duyck | ea9a885 | 2015-10-29 16:20:50 -0500 | [diff] [blame] | 686 | fail_max_buses: |
| 687 | dev->sriov = NULL; |
| 688 | dev->is_physfn = 0; |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 689 | failed: |
| 690 | for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { |
Bjorn Helgaas | c1fe1f9 | 2015-03-25 16:23:45 +0800 | [diff] [blame] | 691 | res = &dev->resource[i + PCI_IOV_RESOURCES]; |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 692 | res->flags = 0; |
| 693 | } |
| 694 | |
Wei Yang | 0e6c912 | 2015-03-25 16:23:44 +0800 | [diff] [blame] | 695 | kfree(iov); |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 696 | return rc; |
| 697 | } |
| 698 | |
| 699 | static void sriov_release(struct pci_dev *dev) |
| 700 | { |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 701 | BUG_ON(dev->sriov->num_VFs); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 702 | |
Yu Zhao | e277d2f | 2009-05-18 13:51:33 +0800 | [diff] [blame] | 703 | if (dev != dev->sriov->dev) |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 704 | pci_dev_put(dev->sriov->dev); |
| 705 | |
| 706 | kfree(dev->sriov); |
| 707 | dev->sriov = NULL; |
| 708 | } |
| 709 | |
Yu Zhao | 8c5cdb6 | 2009-03-20 11:25:12 +0800 | [diff] [blame] | 710 | static void sriov_restore_state(struct pci_dev *dev) |
| 711 | { |
| 712 | int i; |
| 713 | u16 ctrl; |
| 714 | struct pci_sriov *iov = dev->sriov; |
| 715 | |
| 716 | pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &ctrl); |
| 717 | if (ctrl & PCI_SRIOV_CTRL_VFE) |
| 718 | return; |
| 719 | |
Tony Nguyen | ff26449 | 2017-10-04 08:52:58 -0700 | [diff] [blame] | 720 | /* |
| 721 | * Restore PCI_SRIOV_CTRL_ARI before pci_iov_set_numvfs() because |
| 722 | * it reads offset & stride, which depend on PCI_SRIOV_CTRL_ARI. |
| 723 | */ |
| 724 | ctrl &= ~PCI_SRIOV_CTRL_ARI; |
| 725 | ctrl |= iov->ctrl & PCI_SRIOV_CTRL_ARI; |
| 726 | pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, ctrl); |
| 727 | |
Yu Zhao | 8c5cdb6 | 2009-03-20 11:25:12 +0800 | [diff] [blame] | 728 | for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++) |
| 729 | pci_update_resource(dev, i); |
| 730 | |
| 731 | pci_write_config_dword(dev, iov->pos + PCI_SRIOV_SYS_PGSIZE, iov->pgsz); |
Wei Yang | f59dca2 | 2015-03-25 16:23:46 +0800 | [diff] [blame] | 732 | pci_iov_set_numvfs(dev, iov->num_VFs); |
Yu Zhao | 8c5cdb6 | 2009-03-20 11:25:12 +0800 | [diff] [blame] | 733 | pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl); |
| 734 | if (iov->ctrl & PCI_SRIOV_CTRL_VFE) |
| 735 | msleep(100); |
| 736 | } |
| 737 | |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 738 | /** |
| 739 | * pci_iov_init - initialize the IOV capability |
| 740 | * @dev: the PCI device |
| 741 | * |
| 742 | * Returns 0 on success, or negative on failure. |
| 743 | */ |
| 744 | int pci_iov_init(struct pci_dev *dev) |
| 745 | { |
| 746 | int pos; |
| 747 | |
Kenji Kaneshige | 5f4d91a | 2009-11-11 14:36:17 +0900 | [diff] [blame] | 748 | if (!pci_is_pcie(dev)) |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 749 | return -ENODEV; |
| 750 | |
| 751 | pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_SRIOV); |
| 752 | if (pos) |
| 753 | return sriov_init(dev, pos); |
| 754 | |
| 755 | return -ENODEV; |
| 756 | } |
| 757 | |
| 758 | /** |
| 759 | * pci_iov_release - release resources used by the IOV capability |
| 760 | * @dev: the PCI device |
| 761 | */ |
| 762 | void pci_iov_release(struct pci_dev *dev) |
| 763 | { |
| 764 | if (dev->is_physfn) |
| 765 | sriov_release(dev); |
| 766 | } |
| 767 | |
| 768 | /** |
Jakub Kicinski | 3897237 | 2018-06-29 15:08:52 -0500 | [diff] [blame] | 769 | * pci_iov_remove - clean up SR-IOV state after PF driver is detached |
| 770 | * @dev: the PCI device |
| 771 | */ |
| 772 | void pci_iov_remove(struct pci_dev *dev) |
| 773 | { |
| 774 | struct pci_sriov *iov = dev->sriov; |
| 775 | |
| 776 | if (!dev->is_physfn) |
| 777 | return; |
| 778 | |
| 779 | iov->driver_max_VFs = iov->total_VFs; |
| 780 | if (iov->num_VFs) |
| 781 | pci_warn(dev, "driver left SR-IOV enabled after remove\n"); |
| 782 | } |
| 783 | |
| 784 | /** |
Bjorn Helgaas | 6ffa248 | 2016-11-28 09:15:52 -0600 | [diff] [blame] | 785 | * pci_iov_update_resource - update a VF BAR |
| 786 | * @dev: the PCI device |
| 787 | * @resno: the resource number |
| 788 | * |
| 789 | * Update a VF BAR in the SR-IOV capability of a PF. |
| 790 | */ |
| 791 | void pci_iov_update_resource(struct pci_dev *dev, int resno) |
| 792 | { |
| 793 | struct pci_sriov *iov = dev->is_physfn ? dev->sriov : NULL; |
| 794 | struct resource *res = dev->resource + resno; |
| 795 | int vf_bar = resno - PCI_IOV_RESOURCES; |
| 796 | struct pci_bus_region region; |
Bjorn Helgaas | 546ba9f | 2016-11-28 16:43:06 -0600 | [diff] [blame] | 797 | u16 cmd; |
Bjorn Helgaas | 6ffa248 | 2016-11-28 09:15:52 -0600 | [diff] [blame] | 798 | u32 new; |
| 799 | int reg; |
| 800 | |
| 801 | /* |
| 802 | * The generic pci_restore_bars() path calls this for all devices, |
| 803 | * including VFs and non-SR-IOV devices. If this is not a PF, we |
| 804 | * have nothing to do. |
| 805 | */ |
| 806 | if (!iov) |
| 807 | return; |
| 808 | |
Bjorn Helgaas | 546ba9f | 2016-11-28 16:43:06 -0600 | [diff] [blame] | 809 | pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &cmd); |
| 810 | if ((cmd & PCI_SRIOV_CTRL_VFE) && (cmd & PCI_SRIOV_CTRL_MSE)) { |
| 811 | dev_WARN(&dev->dev, "can't update enabled VF BAR%d %pR\n", |
| 812 | vf_bar, res); |
| 813 | return; |
| 814 | } |
| 815 | |
Bjorn Helgaas | 6ffa248 | 2016-11-28 09:15:52 -0600 | [diff] [blame] | 816 | /* |
| 817 | * Ignore unimplemented BARs, unused resource slots for 64-bit |
| 818 | * BARs, and non-movable resources, e.g., those described via |
| 819 | * Enhanced Allocation. |
| 820 | */ |
| 821 | if (!res->flags) |
| 822 | return; |
| 823 | |
| 824 | if (res->flags & IORESOURCE_UNSET) |
| 825 | return; |
| 826 | |
| 827 | if (res->flags & IORESOURCE_PCI_FIXED) |
| 828 | return; |
| 829 | |
| 830 | pcibios_resource_to_bus(dev->bus, ®ion, res); |
| 831 | new = region.start; |
| 832 | new |= res->flags & ~PCI_BASE_ADDRESS_MEM_MASK; |
| 833 | |
| 834 | reg = iov->pos + PCI_SRIOV_BAR + 4 * vf_bar; |
| 835 | pci_write_config_dword(dev, reg, new); |
| 836 | if (res->flags & IORESOURCE_MEM_64) { |
| 837 | new = region.start >> 16 >> 16; |
| 838 | pci_write_config_dword(dev, reg + 4, new); |
| 839 | } |
| 840 | } |
| 841 | |
Wei Yang | 978d2d6 | 2015-03-25 16:23:50 +0800 | [diff] [blame] | 842 | resource_size_t __weak pcibios_iov_resource_alignment(struct pci_dev *dev, |
| 843 | int resno) |
| 844 | { |
| 845 | return pci_iov_resource_size(dev, resno); |
| 846 | } |
| 847 | |
Yu Zhao | 8c5cdb6 | 2009-03-20 11:25:12 +0800 | [diff] [blame] | 848 | /** |
Chris Wright | 6faf17f | 2009-08-28 13:00:06 -0700 | [diff] [blame] | 849 | * pci_sriov_resource_alignment - get resource alignment for VF BAR |
| 850 | * @dev: the PCI device |
| 851 | * @resno: the resource number |
| 852 | * |
| 853 | * Returns the alignment of the VF BAR found in the SR-IOV capability. |
| 854 | * This is not the same as the resource size which is defined as |
| 855 | * the VF BAR size multiplied by the number of VFs. The alignment |
| 856 | * is just the VF BAR size. |
| 857 | */ |
Cam Macdonell | 0e52247 | 2010-09-07 17:25:20 -0700 | [diff] [blame] | 858 | resource_size_t pci_sriov_resource_alignment(struct pci_dev *dev, int resno) |
Chris Wright | 6faf17f | 2009-08-28 13:00:06 -0700 | [diff] [blame] | 859 | { |
Wei Yang | 978d2d6 | 2015-03-25 16:23:50 +0800 | [diff] [blame] | 860 | return pcibios_iov_resource_alignment(dev, resno); |
Chris Wright | 6faf17f | 2009-08-28 13:00:06 -0700 | [diff] [blame] | 861 | } |
| 862 | |
| 863 | /** |
Yu Zhao | 8c5cdb6 | 2009-03-20 11:25:12 +0800 | [diff] [blame] | 864 | * pci_restore_iov_state - restore the state of the IOV capability |
| 865 | * @dev: the PCI device |
| 866 | */ |
| 867 | void pci_restore_iov_state(struct pci_dev *dev) |
| 868 | { |
| 869 | if (dev->is_physfn) |
| 870 | sriov_restore_state(dev); |
| 871 | } |
Yu Zhao | a28724b | 2009-03-20 11:25:13 +0800 | [diff] [blame] | 872 | |
| 873 | /** |
Bryant G. Ly | 608c0d8 | 2017-11-09 08:00:35 -0600 | [diff] [blame] | 874 | * pci_vf_drivers_autoprobe - set PF property drivers_autoprobe for VFs |
| 875 | * @dev: the PCI device |
| 876 | * @auto_probe: set VF drivers auto probe flag |
| 877 | */ |
| 878 | void pci_vf_drivers_autoprobe(struct pci_dev *dev, bool auto_probe) |
| 879 | { |
| 880 | if (dev->is_physfn) |
| 881 | dev->sriov->drivers_autoprobe = auto_probe; |
| 882 | } |
| 883 | |
| 884 | /** |
Yu Zhao | a28724b | 2009-03-20 11:25:13 +0800 | [diff] [blame] | 885 | * pci_iov_bus_range - find bus range used by Virtual Function |
| 886 | * @bus: the PCI bus |
| 887 | * |
| 888 | * Returns max number of buses (exclude current one) used by Virtual |
| 889 | * Functions. |
| 890 | */ |
| 891 | int pci_iov_bus_range(struct pci_bus *bus) |
| 892 | { |
| 893 | int max = 0; |
Yu Zhao | a28724b | 2009-03-20 11:25:13 +0800 | [diff] [blame] | 894 | struct pci_dev *dev; |
| 895 | |
| 896 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 897 | if (!dev->is_physfn) |
| 898 | continue; |
Wei Yang | 4449f07 | 2015-03-25 16:23:47 +0800 | [diff] [blame] | 899 | if (dev->sriov->max_VF_buses > max) |
| 900 | max = dev->sriov->max_VF_buses; |
Yu Zhao | a28724b | 2009-03-20 11:25:13 +0800 | [diff] [blame] | 901 | } |
| 902 | |
| 903 | return max ? max - bus->number : 0; |
| 904 | } |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 905 | |
| 906 | /** |
| 907 | * pci_enable_sriov - enable the SR-IOV capability |
| 908 | * @dev: the PCI device |
Randy Dunlap | 52a8873 | 2009-04-01 17:45:30 -0700 | [diff] [blame] | 909 | * @nr_virtfn: number of virtual functions to enable |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 910 | * |
| 911 | * Returns 0 on success, or negative on failure. |
| 912 | */ |
| 913 | int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn) |
| 914 | { |
| 915 | might_sleep(); |
| 916 | |
| 917 | if (!dev->is_physfn) |
Stefan Assmann | 652d110 | 2013-07-31 16:47:56 -0600 | [diff] [blame] | 918 | return -ENOSYS; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 919 | |
| 920 | return sriov_enable(dev, nr_virtfn); |
| 921 | } |
| 922 | EXPORT_SYMBOL_GPL(pci_enable_sriov); |
| 923 | |
| 924 | /** |
| 925 | * pci_disable_sriov - disable the SR-IOV capability |
| 926 | * @dev: the PCI device |
| 927 | */ |
| 928 | void pci_disable_sriov(struct pci_dev *dev) |
| 929 | { |
| 930 | might_sleep(); |
| 931 | |
| 932 | if (!dev->is_physfn) |
| 933 | return; |
| 934 | |
| 935 | sriov_disable(dev); |
| 936 | } |
| 937 | EXPORT_SYMBOL_GPL(pci_disable_sriov); |
Yu Zhao | 74bb1bc | 2009-03-20 11:25:16 +0800 | [diff] [blame] | 938 | |
| 939 | /** |
Williams, Mitch A | fb8a0d9 | 2010-02-10 01:43:04 +0000 | [diff] [blame] | 940 | * pci_num_vf - return number of VFs associated with a PF device_release_driver |
| 941 | * @dev: the PCI device |
| 942 | * |
| 943 | * Returns number of VFs, or 0 if SR-IOV is not enabled. |
| 944 | */ |
| 945 | int pci_num_vf(struct pci_dev *dev) |
| 946 | { |
Bjorn Helgaas | 1452cd7 | 2012-11-09 20:35:01 -0700 | [diff] [blame] | 947 | if (!dev->is_physfn) |
Williams, Mitch A | fb8a0d9 | 2010-02-10 01:43:04 +0000 | [diff] [blame] | 948 | return 0; |
Bjorn Helgaas | 1452cd7 | 2012-11-09 20:35:01 -0700 | [diff] [blame] | 949 | |
| 950 | return dev->sriov->num_VFs; |
Williams, Mitch A | fb8a0d9 | 2010-02-10 01:43:04 +0000 | [diff] [blame] | 951 | } |
| 952 | EXPORT_SYMBOL_GPL(pci_num_vf); |
Donald Dutile | bff7315 | 2012-11-05 15:20:37 -0500 | [diff] [blame] | 953 | |
| 954 | /** |
Alexander Duyck | 5a8eb24 | 2013-04-25 04:42:29 +0000 | [diff] [blame] | 955 | * pci_vfs_assigned - returns number of VFs are assigned to a guest |
| 956 | * @dev: the PCI device |
| 957 | * |
| 958 | * Returns number of VFs belonging to this device that are assigned to a guest. |
Stefan Assmann | 652d110 | 2013-07-31 16:47:56 -0600 | [diff] [blame] | 959 | * If device is not a physical function returns 0. |
Alexander Duyck | 5a8eb24 | 2013-04-25 04:42:29 +0000 | [diff] [blame] | 960 | */ |
| 961 | int pci_vfs_assigned(struct pci_dev *dev) |
| 962 | { |
| 963 | struct pci_dev *vfdev; |
| 964 | unsigned int vfs_assigned = 0; |
| 965 | unsigned short dev_id; |
| 966 | |
| 967 | /* only search if we are a PF */ |
| 968 | if (!dev->is_physfn) |
| 969 | return 0; |
| 970 | |
| 971 | /* |
| 972 | * determine the device ID for the VFs, the vendor ID will be the |
| 973 | * same as the PF so there is no need to check for that one |
| 974 | */ |
Filippo Sironi | 3142d83 | 2017-08-28 15:38:49 +0200 | [diff] [blame] | 975 | dev_id = dev->sriov->vf_device; |
Alexander Duyck | 5a8eb24 | 2013-04-25 04:42:29 +0000 | [diff] [blame] | 976 | |
| 977 | /* loop through all the VFs to see if we own any that are assigned */ |
| 978 | vfdev = pci_get_device(dev->vendor, dev_id, NULL); |
| 979 | while (vfdev) { |
| 980 | /* |
| 981 | * It is considered assigned if it is a virtual function with |
| 982 | * our dev as the physical function and the assigned bit is set |
| 983 | */ |
| 984 | if (vfdev->is_virtfn && (vfdev->physfn == dev) && |
Ethan Zhao | be63497 | 2014-09-09 10:21:28 +0800 | [diff] [blame] | 985 | pci_is_dev_assigned(vfdev)) |
Alexander Duyck | 5a8eb24 | 2013-04-25 04:42:29 +0000 | [diff] [blame] | 986 | vfs_assigned++; |
| 987 | |
| 988 | vfdev = pci_get_device(dev->vendor, dev_id, vfdev); |
| 989 | } |
| 990 | |
| 991 | return vfs_assigned; |
| 992 | } |
| 993 | EXPORT_SYMBOL_GPL(pci_vfs_assigned); |
| 994 | |
| 995 | /** |
Donald Dutile | bff7315 | 2012-11-05 15:20:37 -0500 | [diff] [blame] | 996 | * pci_sriov_set_totalvfs -- reduce the TotalVFs available |
| 997 | * @dev: the PCI PF device |
Randy Dunlap | 2094f16 | 2013-01-09 17:12:52 -0800 | [diff] [blame] | 998 | * @numvfs: number that should be used for TotalVFs supported |
Donald Dutile | bff7315 | 2012-11-05 15:20:37 -0500 | [diff] [blame] | 999 | * |
| 1000 | * Should be called from PF driver's probe routine with |
| 1001 | * device's mutex held. |
| 1002 | * |
| 1003 | * Returns 0 if PF is an SRIOV-capable device and |
Stefan Assmann | 652d110 | 2013-07-31 16:47:56 -0600 | [diff] [blame] | 1004 | * value of numvfs valid. If not a PF return -ENOSYS; |
| 1005 | * if numvfs is invalid return -EINVAL; |
Donald Dutile | bff7315 | 2012-11-05 15:20:37 -0500 | [diff] [blame] | 1006 | * if VFs already enabled, return -EBUSY. |
| 1007 | */ |
| 1008 | int pci_sriov_set_totalvfs(struct pci_dev *dev, u16 numvfs) |
| 1009 | { |
Stefan Assmann | 652d110 | 2013-07-31 16:47:56 -0600 | [diff] [blame] | 1010 | if (!dev->is_physfn) |
| 1011 | return -ENOSYS; |
Bjorn Helgaas | 51259d0 | 2018-05-25 08:51:50 -0500 | [diff] [blame] | 1012 | |
Stefan Assmann | 652d110 | 2013-07-31 16:47:56 -0600 | [diff] [blame] | 1013 | if (numvfs > dev->sriov->total_VFs) |
Donald Dutile | bff7315 | 2012-11-05 15:20:37 -0500 | [diff] [blame] | 1014 | return -EINVAL; |
| 1015 | |
| 1016 | /* Shouldn't change if VFs already enabled */ |
| 1017 | if (dev->sriov->ctrl & PCI_SRIOV_CTRL_VFE) |
| 1018 | return -EBUSY; |
Donald Dutile | bff7315 | 2012-11-05 15:20:37 -0500 | [diff] [blame] | 1019 | |
Bjorn Helgaas | 51259d0 | 2018-05-25 08:51:50 -0500 | [diff] [blame] | 1020 | dev->sriov->driver_max_VFs = numvfs; |
Donald Dutile | bff7315 | 2012-11-05 15:20:37 -0500 | [diff] [blame] | 1021 | return 0; |
| 1022 | } |
| 1023 | EXPORT_SYMBOL_GPL(pci_sriov_set_totalvfs); |
| 1024 | |
| 1025 | /** |
Jonghwan Choi | ddc191f | 2013-07-08 14:02:43 -0600 | [diff] [blame] | 1026 | * pci_sriov_get_totalvfs -- get total VFs supported on this device |
Donald Dutile | bff7315 | 2012-11-05 15:20:37 -0500 | [diff] [blame] | 1027 | * @dev: the PCI PF device |
| 1028 | * |
| 1029 | * For a PCIe device with SRIOV support, return the PCIe |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 1030 | * SRIOV capability value of TotalVFs or the value of driver_max_VFs |
Stefan Assmann | 652d110 | 2013-07-31 16:47:56 -0600 | [diff] [blame] | 1031 | * if the driver reduced it. Otherwise 0. |
Donald Dutile | bff7315 | 2012-11-05 15:20:37 -0500 | [diff] [blame] | 1032 | */ |
| 1033 | int pci_sriov_get_totalvfs(struct pci_dev *dev) |
| 1034 | { |
Bjorn Helgaas | 1452cd7 | 2012-11-09 20:35:01 -0700 | [diff] [blame] | 1035 | if (!dev->is_physfn) |
Stefan Assmann | 652d110 | 2013-07-31 16:47:56 -0600 | [diff] [blame] | 1036 | return 0; |
Donald Dutile | bff7315 | 2012-11-05 15:20:37 -0500 | [diff] [blame] | 1037 | |
Jakub Kicinski | 8d85a7a | 2018-05-25 08:18:34 -0500 | [diff] [blame] | 1038 | return dev->sriov->driver_max_VFs; |
Donald Dutile | bff7315 | 2012-11-05 15:20:37 -0500 | [diff] [blame] | 1039 | } |
| 1040 | EXPORT_SYMBOL_GPL(pci_sriov_get_totalvfs); |
Alexander Duyck | 8effc39 | 2018-04-21 15:23:09 -0500 | [diff] [blame] | 1041 | |
| 1042 | /** |
| 1043 | * pci_sriov_configure_simple - helper to configure SR-IOV |
| 1044 | * @dev: the PCI device |
| 1045 | * @nr_virtfn: number of virtual functions to enable, 0 to disable |
| 1046 | * |
| 1047 | * Enable or disable SR-IOV for devices that don't require any PF setup |
| 1048 | * before enabling SR-IOV. Return value is negative on error, or number of |
| 1049 | * VFs allocated on success. |
| 1050 | */ |
| 1051 | int pci_sriov_configure_simple(struct pci_dev *dev, int nr_virtfn) |
| 1052 | { |
| 1053 | int rc; |
| 1054 | |
| 1055 | might_sleep(); |
| 1056 | |
| 1057 | if (!dev->is_physfn) |
| 1058 | return -ENODEV; |
| 1059 | |
| 1060 | if (pci_vfs_assigned(dev)) { |
| 1061 | pci_warn(dev, "Cannot modify SR-IOV while VFs are assigned\n"); |
| 1062 | return -EPERM; |
| 1063 | } |
| 1064 | |
| 1065 | if (nr_virtfn == 0) { |
| 1066 | sriov_disable(dev); |
| 1067 | return 0; |
| 1068 | } |
| 1069 | |
| 1070 | rc = sriov_enable(dev, nr_virtfn); |
| 1071 | if (rc < 0) |
| 1072 | return rc; |
| 1073 | |
| 1074 | return nr_virtfn; |
| 1075 | } |
| 1076 | EXPORT_SYMBOL_GPL(pci_sriov_configure_simple); |