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); |
KarimAllah Ahmed | 975bb8b | 2018-10-11 11:49:58 -0500 | [diff] [blame] | 135 | |
| 136 | physfn->sriov->cfg_size = pci_cfg_space_size(virtfn); |
KarimAllah Ahmed | cf0921b | 2018-03-19 21:06:00 +0100 | [diff] [blame] | 137 | } |
| 138 | |
Jan H. Schönherr | 753f612 | 2017-09-26 12:53:23 -0500 | [diff] [blame] | 139 | int pci_iov_add_virtfn(struct pci_dev *dev, int id) |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 140 | { |
| 141 | int i; |
Jiang Liu | dc087f2 | 2013-05-25 21:48:37 +0800 | [diff] [blame] | 142 | int rc = -ENOMEM; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 143 | u64 size; |
| 144 | char buf[VIRTFN_ID_LEN]; |
| 145 | struct pci_dev *virtfn; |
| 146 | struct resource *res; |
| 147 | struct pci_sriov *iov = dev->sriov; |
Gu Zheng | 8b1fce0 | 2013-05-25 21:48:31 +0800 | [diff] [blame] | 148 | struct pci_bus *bus; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 149 | |
Wei Yang | b07579c | 2015-03-25 16:23:48 +0800 | [diff] [blame] | 150 | bus = virtfn_add_bus(dev->bus, pci_iov_virtfn_bus(dev, id)); |
Jiang Liu | dc087f2 | 2013-05-25 21:48:37 +0800 | [diff] [blame] | 151 | if (!bus) |
| 152 | goto failed; |
| 153 | |
| 154 | virtfn = pci_alloc_dev(bus); |
| 155 | if (!virtfn) |
| 156 | goto failed0; |
| 157 | |
Wei Yang | b07579c | 2015-03-25 16:23:48 +0800 | [diff] [blame] | 158 | virtfn->devfn = pci_iov_virtfn_devfn(dev, id); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 159 | virtfn->vendor = dev->vendor; |
Filippo Sironi | 3142d83 | 2017-08-28 15:38:49 +0200 | [diff] [blame] | 160 | virtfn->device = iov->vf_device; |
KarimAllah Ahmed | cf0921b | 2018-03-19 21:06:00 +0100 | [diff] [blame] | 161 | virtfn->is_virtfn = 1; |
| 162 | virtfn->physfn = pci_dev_get(dev); |
| 163 | |
| 164 | if (id == 0) |
| 165 | pci_read_vf_config_common(virtfn); |
| 166 | |
Po Liu | 156c553 | 2016-08-29 15:28:01 +0800 | [diff] [blame] | 167 | rc = pci_setup_device(virtfn); |
| 168 | if (rc) |
KarimAllah Ahmed | cf0921b | 2018-03-19 21:06:00 +0100 | [diff] [blame] | 169 | goto failed1; |
Po Liu | 156c553 | 2016-08-29 15:28:01 +0800 | [diff] [blame] | 170 | |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 171 | virtfn->dev.parent = dev->dev.parent; |
Alex Williamson | aa931977 | 2014-01-09 08:36:08 -0700 | [diff] [blame] | 172 | virtfn->multifunction = 0; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 173 | |
| 174 | for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { |
Bjorn Helgaas | c1fe1f9 | 2015-03-25 16:23:45 +0800 | [diff] [blame] | 175 | res = &dev->resource[i + PCI_IOV_RESOURCES]; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 176 | if (!res->parent) |
| 177 | continue; |
| 178 | virtfn->resource[i].name = pci_name(virtfn); |
| 179 | virtfn->resource[i].flags = res->flags; |
Wei Yang | 0e6c912 | 2015-03-25 16:23:44 +0800 | [diff] [blame] | 180 | size = pci_iov_resource_size(dev, i + PCI_IOV_RESOURCES); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 181 | virtfn->resource[i].start = res->start + size * id; |
| 182 | virtfn->resource[i].end = virtfn->resource[i].start + size - 1; |
| 183 | rc = request_resource(res, &virtfn->resource[i]); |
| 184 | BUG_ON(rc); |
| 185 | } |
| 186 | |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 187 | pci_device_add(virtfn, virtfn->bus); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 188 | |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 189 | sprintf(buf, "virtfn%u", id); |
| 190 | rc = sysfs_create_link(&dev->dev.kobj, &virtfn->dev.kobj, buf); |
| 191 | if (rc) |
KarimAllah Ahmed | cf0921b | 2018-03-19 21:06:00 +0100 | [diff] [blame] | 192 | goto failed2; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 193 | rc = sysfs_create_link(&virtfn->dev.kobj, &dev->dev.kobj, "physfn"); |
| 194 | if (rc) |
KarimAllah Ahmed | cf0921b | 2018-03-19 21:06:00 +0100 | [diff] [blame] | 195 | goto failed3; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 196 | |
| 197 | kobject_uevent(&virtfn->dev.kobj, KOBJ_CHANGE); |
| 198 | |
Stuart Hayes | 27d6162 | 2017-10-04 10:57:52 -0500 | [diff] [blame] | 199 | pci_bus_add_device(virtfn); |
| 200 | |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 201 | return 0; |
| 202 | |
KarimAllah Ahmed | cf0921b | 2018-03-19 21:06:00 +0100 | [diff] [blame] | 203 | failed3: |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 204 | sysfs_remove_link(&dev->dev.kobj, buf); |
KarimAllah Ahmed | cf0921b | 2018-03-19 21:06:00 +0100 | [diff] [blame] | 205 | failed2: |
| 206 | pci_stop_and_remove_bus_device(virtfn); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 207 | failed1: |
| 208 | pci_dev_put(dev); |
Jiang Liu | dc087f2 | 2013-05-25 21:48:37 +0800 | [diff] [blame] | 209 | failed0: |
| 210 | virtfn_remove_bus(dev->bus, bus); |
| 211 | failed: |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 212 | |
| 213 | return rc; |
| 214 | } |
| 215 | |
Jan H. Schönherr | 753f612 | 2017-09-26 12:53:23 -0500 | [diff] [blame] | 216 | void pci_iov_remove_virtfn(struct pci_dev *dev, int id) |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 217 | { |
| 218 | char buf[VIRTFN_ID_LEN]; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 219 | struct pci_dev *virtfn; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 220 | |
Jiang Liu | dc087f2 | 2013-05-25 21:48:37 +0800 | [diff] [blame] | 221 | virtfn = pci_get_domain_bus_and_slot(pci_domain_nr(dev->bus), |
Wei Yang | b07579c | 2015-03-25 16:23:48 +0800 | [diff] [blame] | 222 | pci_iov_virtfn_bus(dev, id), |
| 223 | pci_iov_virtfn_devfn(dev, id)); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 224 | if (!virtfn) |
| 225 | return; |
| 226 | |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 227 | sprintf(buf, "virtfn%u", id); |
| 228 | sysfs_remove_link(&dev->dev.kobj, buf); |
Yinghai Lu | 09cedbe | 2012-02-04 22:55:01 -0800 | [diff] [blame] | 229 | /* |
| 230 | * pci_stop_dev() could have been called for this virtfn already, |
| 231 | * so the directory for the virtfn may have been removed before. |
| 232 | * Double check to avoid spurious sysfs warnings. |
| 233 | */ |
| 234 | if (virtfn->dev.kobj.sd) |
| 235 | sysfs_remove_link(&virtfn->dev.kobj, "physfn"); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 236 | |
Yinghai Lu | 210647a | 2012-02-25 13:54:20 -0800 | [diff] [blame] | 237 | pci_stop_and_remove_bus_device(virtfn); |
Jiang Liu | dc087f2 | 2013-05-25 21:48:37 +0800 | [diff] [blame] | 238 | virtfn_remove_bus(dev->bus, virtfn->bus); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 239 | |
Jiang Liu | dc087f2 | 2013-05-25 21:48:37 +0800 | [diff] [blame] | 240 | /* balance pci_get_domain_bus_and_slot() */ |
| 241 | pci_dev_put(virtfn); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 242 | pci_dev_put(dev); |
| 243 | } |
| 244 | |
Wei Yang | 995df52 | 2015-03-25 16:23:49 +0800 | [diff] [blame] | 245 | int __weak pcibios_sriov_enable(struct pci_dev *pdev, u16 num_vfs) |
| 246 | { |
Alexander Duyck | a39e3fc | 2015-10-29 16:21:11 -0500 | [diff] [blame] | 247 | return 0; |
| 248 | } |
| 249 | |
| 250 | int __weak pcibios_sriov_disable(struct pci_dev *pdev) |
| 251 | { |
| 252 | return 0; |
Wei Yang | 995df52 | 2015-03-25 16:23:49 +0800 | [diff] [blame] | 253 | } |
| 254 | |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 255 | static int sriov_enable(struct pci_dev *dev, int nr_virtfn) |
| 256 | { |
| 257 | int rc; |
Alexander Duyck | 3443c38 | 2015-10-29 16:21:05 -0500 | [diff] [blame] | 258 | int i; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 259 | int nres; |
Alexander Duyck | ce288ec | 2015-10-29 16:20:57 -0500 | [diff] [blame] | 260 | u16 initial; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 261 | struct resource *res; |
| 262 | struct pci_dev *pdev; |
| 263 | struct pci_sriov *iov = dev->sriov; |
Ram Pai | bbef98a | 2011-11-06 10:33:10 +0800 | [diff] [blame] | 264 | int bars = 0; |
Wei Yang | b07579c | 2015-03-25 16:23:48 +0800 | [diff] [blame] | 265 | int bus; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 266 | |
| 267 | if (!nr_virtfn) |
| 268 | return 0; |
| 269 | |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 270 | if (iov->num_VFs) |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 271 | return -EINVAL; |
| 272 | |
| 273 | pci_read_config_word(dev, iov->pos + PCI_SRIOV_INITIAL_VF, &initial); |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 274 | if (initial > iov->total_VFs || |
| 275 | (!(iov->cap & PCI_SRIOV_CAP_VFM) && (initial != iov->total_VFs))) |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 276 | return -EIO; |
| 277 | |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 278 | if (nr_virtfn < 0 || nr_virtfn > iov->total_VFs || |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 279 | (!(iov->cap & PCI_SRIOV_CAP_VFM) && (nr_virtfn > initial))) |
| 280 | return -EINVAL; |
| 281 | |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 282 | nres = 0; |
| 283 | for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { |
Ram Pai | bbef98a | 2011-11-06 10:33:10 +0800 | [diff] [blame] | 284 | bars |= (1 << (i + PCI_IOV_RESOURCES)); |
Bjorn Helgaas | c1fe1f9 | 2015-03-25 16:23:45 +0800 | [diff] [blame] | 285 | res = &dev->resource[i + PCI_IOV_RESOURCES]; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 286 | if (res->parent) |
| 287 | nres++; |
| 288 | } |
| 289 | if (nres != iov->nres) { |
Frederick Lawler | 7506dc7 | 2018-01-18 12:55:24 -0600 | [diff] [blame] | 290 | pci_err(dev, "not enough MMIO resources for SR-IOV\n"); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 291 | return -ENOMEM; |
| 292 | } |
| 293 | |
Wei Yang | b07579c | 2015-03-25 16:23:48 +0800 | [diff] [blame] | 294 | bus = pci_iov_virtfn_bus(dev, nr_virtfn - 1); |
Bjorn Helgaas | 68f8e9f | 2015-03-25 16:23:42 +0800 | [diff] [blame] | 295 | if (bus > dev->bus->busn_res.end) { |
Frederick Lawler | 7506dc7 | 2018-01-18 12:55:24 -0600 | [diff] [blame] | 296 | 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] | 297 | nr_virtfn, bus, &dev->bus->busn_res); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 298 | return -ENOMEM; |
| 299 | } |
| 300 | |
Ram Pai | bbef98a | 2011-11-06 10:33:10 +0800 | [diff] [blame] | 301 | if (pci_enable_resources(dev, bars)) { |
Frederick Lawler | 7506dc7 | 2018-01-18 12:55:24 -0600 | [diff] [blame] | 302 | pci_err(dev, "SR-IOV: IOV BARS not allocated\n"); |
Ram Pai | bbef98a | 2011-11-06 10:33:10 +0800 | [diff] [blame] | 303 | return -ENOMEM; |
| 304 | } |
| 305 | |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 306 | if (iov->link != dev->devfn) { |
| 307 | pdev = pci_get_slot(dev->bus, iov->link); |
| 308 | if (!pdev) |
| 309 | return -ENODEV; |
| 310 | |
Jiang Liu | dc087f2 | 2013-05-25 21:48:37 +0800 | [diff] [blame] | 311 | if (!pdev->is_physfn) { |
| 312 | pci_dev_put(pdev); |
Stefan Assmann | 652d110 | 2013-07-31 16:47:56 -0600 | [diff] [blame] | 313 | return -ENOSYS; |
Jiang Liu | dc087f2 | 2013-05-25 21:48:37 +0800 | [diff] [blame] | 314 | } |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 315 | |
| 316 | rc = sysfs_create_link(&dev->dev.kobj, |
| 317 | &pdev->dev.kobj, "dep_link"); |
Jiang Liu | dc087f2 | 2013-05-25 21:48:37 +0800 | [diff] [blame] | 318 | pci_dev_put(pdev); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 319 | if (rc) |
| 320 | return rc; |
| 321 | } |
| 322 | |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 323 | iov->initial_VFs = initial; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 324 | if (nr_virtfn < initial) |
| 325 | initial = nr_virtfn; |
| 326 | |
Alexander Duyck | c23b613 | 2015-10-29 16:21:20 -0500 | [diff] [blame] | 327 | rc = pcibios_sriov_enable(dev, initial); |
| 328 | if (rc) { |
Frederick Lawler | 7506dc7 | 2018-01-18 12:55:24 -0600 | [diff] [blame] | 329 | pci_err(dev, "failure %d from pcibios_sriov_enable()\n", rc); |
Alexander Duyck | c23b613 | 2015-10-29 16:21:20 -0500 | [diff] [blame] | 330 | goto err_pcibios; |
Wei Yang | 995df52 | 2015-03-25 16:23:49 +0800 | [diff] [blame] | 331 | } |
| 332 | |
Gavin Shan | f40ec3c | 2016-10-26 12:15:35 +1100 | [diff] [blame] | 333 | pci_iov_set_numvfs(dev, nr_virtfn); |
| 334 | iov->ctrl |= PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE; |
| 335 | pci_cfg_access_lock(dev); |
| 336 | pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl); |
| 337 | msleep(100); |
| 338 | pci_cfg_access_unlock(dev); |
| 339 | |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 340 | for (i = 0; i < initial; i++) { |
Jan H. Schönherr | 753f612 | 2017-09-26 12:53:23 -0500 | [diff] [blame] | 341 | rc = pci_iov_add_virtfn(dev, i); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 342 | if (rc) |
| 343 | goto failed; |
| 344 | } |
| 345 | |
| 346 | kobject_uevent(&dev->dev.kobj, KOBJ_CHANGE); |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 347 | iov->num_VFs = nr_virtfn; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 348 | |
| 349 | return 0; |
| 350 | |
| 351 | failed: |
Alexander Duyck | 3443c38 | 2015-10-29 16:21:05 -0500 | [diff] [blame] | 352 | while (i--) |
Jan H. Schönherr | 753f612 | 2017-09-26 12:53:23 -0500 | [diff] [blame] | 353 | pci_iov_remove_virtfn(dev, i); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 354 | |
Alexander Duyck | c23b613 | 2015-10-29 16:21:20 -0500 | [diff] [blame] | 355 | err_pcibios: |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 356 | iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE); |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 357 | pci_cfg_access_lock(dev); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 358 | pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl); |
| 359 | ssleep(1); |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 360 | pci_cfg_access_unlock(dev); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 361 | |
Gavin Shan | 0fc690a | 2017-08-11 18:19:33 +1000 | [diff] [blame] | 362 | pcibios_sriov_disable(dev); |
| 363 | |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 364 | if (iov->link != dev->devfn) |
| 365 | sysfs_remove_link(&dev->dev.kobj, "dep_link"); |
| 366 | |
Alexander Duyck | b390864 | 2015-10-29 16:21:16 -0500 | [diff] [blame] | 367 | pci_iov_set_numvfs(dev, 0); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 368 | return rc; |
| 369 | } |
| 370 | |
| 371 | static void sriov_disable(struct pci_dev *dev) |
| 372 | { |
| 373 | int i; |
| 374 | struct pci_sriov *iov = dev->sriov; |
| 375 | |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 376 | if (!iov->num_VFs) |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 377 | return; |
| 378 | |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 379 | for (i = 0; i < iov->num_VFs; i++) |
Jan H. Schönherr | 753f612 | 2017-09-26 12:53:23 -0500 | [diff] [blame] | 380 | pci_iov_remove_virtfn(dev, i); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 381 | |
| 382 | iov->ctrl &= ~(PCI_SRIOV_CTRL_VFE | PCI_SRIOV_CTRL_MSE); |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 383 | pci_cfg_access_lock(dev); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 384 | pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl); |
| 385 | ssleep(1); |
Jan Kiszka | fb51ccb | 2011-11-04 09:45:59 +0100 | [diff] [blame] | 386 | pci_cfg_access_unlock(dev); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 387 | |
Gavin Shan | 0fc690a | 2017-08-11 18:19:33 +1000 | [diff] [blame] | 388 | pcibios_sriov_disable(dev); |
| 389 | |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 390 | if (iov->link != dev->devfn) |
| 391 | sysfs_remove_link(&dev->dev.kobj, "dep_link"); |
| 392 | |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 393 | iov->num_VFs = 0; |
Wei Yang | f59dca2 | 2015-03-25 16:23:46 +0800 | [diff] [blame] | 394 | pci_iov_set_numvfs(dev, 0); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 395 | } |
| 396 | |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 397 | static int sriov_init(struct pci_dev *dev, int pos) |
| 398 | { |
Wei Yang | 0e6c912 | 2015-03-25 16:23:44 +0800 | [diff] [blame] | 399 | int i, bar64; |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 400 | int rc; |
| 401 | int nres; |
| 402 | u32 pgsz; |
Alexander Duyck | ea9a885 | 2015-10-29 16:20:50 -0500 | [diff] [blame] | 403 | u16 ctrl, total; |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 404 | struct pci_sriov *iov; |
| 405 | struct resource *res; |
| 406 | struct pci_dev *pdev; |
| 407 | |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 408 | pci_read_config_word(dev, pos + PCI_SRIOV_CTRL, &ctrl); |
| 409 | if (ctrl & PCI_SRIOV_CTRL_VFE) { |
| 410 | pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, 0); |
| 411 | ssleep(1); |
| 412 | } |
| 413 | |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 414 | ctrl = 0; |
| 415 | list_for_each_entry(pdev, &dev->bus->devices, bus_list) |
| 416 | if (pdev->is_physfn) |
| 417 | goto found; |
| 418 | |
| 419 | pdev = NULL; |
| 420 | if (pci_ari_enabled(dev->bus)) |
| 421 | ctrl |= PCI_SRIOV_CTRL_ARI; |
| 422 | |
| 423 | found: |
| 424 | pci_write_config_word(dev, pos + PCI_SRIOV_CTRL, ctrl); |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 425 | |
Ben Shelton | ff45f9d | 2015-10-29 16:20:31 -0500 | [diff] [blame] | 426 | pci_read_config_word(dev, pos + PCI_SRIOV_TOTAL_VF, &total); |
| 427 | if (!total) |
| 428 | return 0; |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 429 | |
| 430 | pci_read_config_dword(dev, pos + PCI_SRIOV_SUP_PGSIZE, &pgsz); |
| 431 | i = PAGE_SHIFT > 12 ? PAGE_SHIFT - 12 : 0; |
| 432 | pgsz &= ~((1 << i) - 1); |
| 433 | if (!pgsz) |
| 434 | return -EIO; |
| 435 | |
| 436 | pgsz &= ~(pgsz - 1); |
Vaidyanathan Srinivasan | 8161fe9 | 2012-02-02 23:11:20 +0530 | [diff] [blame] | 437 | pci_write_config_dword(dev, pos + PCI_SRIOV_SYS_PGSIZE, pgsz); |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 438 | |
Wei Yang | 0e6c912 | 2015-03-25 16:23:44 +0800 | [diff] [blame] | 439 | iov = kzalloc(sizeof(*iov), GFP_KERNEL); |
| 440 | if (!iov) |
| 441 | return -ENOMEM; |
| 442 | |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 443 | nres = 0; |
| 444 | for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { |
Bjorn Helgaas | c1fe1f9 | 2015-03-25 16:23:45 +0800 | [diff] [blame] | 445 | res = &dev->resource[i + PCI_IOV_RESOURCES]; |
David Daney | 1118399 | 2015-10-29 17:35:40 -0500 | [diff] [blame] | 446 | /* |
| 447 | * If it is already FIXED, don't change it, something |
| 448 | * (perhaps EA or header fixups) wants it this way. |
| 449 | */ |
| 450 | if (res->flags & IORESOURCE_PCI_FIXED) |
| 451 | bar64 = (res->flags & IORESOURCE_MEM_64) ? 1 : 0; |
| 452 | else |
| 453 | bar64 = __pci_read_base(dev, pci_bar_unknown, res, |
| 454 | pos + PCI_SRIOV_BAR + i * 4); |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 455 | if (!res->flags) |
| 456 | continue; |
| 457 | if (resource_size(res) & (PAGE_SIZE - 1)) { |
| 458 | rc = -EIO; |
| 459 | goto failed; |
| 460 | } |
Wei Yang | 0e6c912 | 2015-03-25 16:23:44 +0800 | [diff] [blame] | 461 | iov->barsz[i] = resource_size(res); |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 462 | res->end = res->start + resource_size(res) * total - 1; |
Frederick Lawler | 7506dc7 | 2018-01-18 12:55:24 -0600 | [diff] [blame] | 463 | 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] | 464 | i, res, i, total); |
Wei Yang | 0e6c912 | 2015-03-25 16:23:44 +0800 | [diff] [blame] | 465 | i += bar64; |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 466 | nres++; |
| 467 | } |
| 468 | |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 469 | iov->pos = pos; |
| 470 | iov->nres = nres; |
| 471 | iov->ctrl = ctrl; |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 472 | iov->total_VFs = total; |
Jakub Kicinski | 8d85a7a | 2018-05-25 08:18:34 -0500 | [diff] [blame] | 473 | iov->driver_max_VFs = total; |
Filippo Sironi | 3142d83 | 2017-08-28 15:38:49 +0200 | [diff] [blame] | 474 | 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] | 475 | iov->pgsz = pgsz; |
| 476 | iov->self = dev; |
Bodong Wang | 0e7df22 | 2017-04-13 01:51:40 +0300 | [diff] [blame] | 477 | iov->drivers_autoprobe = true; |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 478 | pci_read_config_dword(dev, pos + PCI_SRIOV_CAP, &iov->cap); |
| 479 | pci_read_config_byte(dev, pos + PCI_SRIOV_FUNC_LINK, &iov->link); |
Yijing Wang | 62f87c0 | 2012-07-24 17:20:03 +0800 | [diff] [blame] | 480 | if (pci_pcie_type(dev) == PCI_EXP_TYPE_RC_END) |
Yu Zhao | 4d135db | 2009-05-20 17:11:57 +0800 | [diff] [blame] | 481 | iov->link = PCI_DEVFN(PCI_SLOT(dev->devfn), iov->link); |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 482 | |
| 483 | if (pdev) |
| 484 | iov->dev = pci_dev_get(pdev); |
Yu Zhao | e277d2f | 2009-05-18 13:51:33 +0800 | [diff] [blame] | 485 | else |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 486 | iov->dev = dev; |
Yu Zhao | e277d2f | 2009-05-18 13:51:33 +0800 | [diff] [blame] | 487 | |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 488 | dev->sriov = iov; |
| 489 | dev->is_physfn = 1; |
Alexander Duyck | ea9a885 | 2015-10-29 16:20:50 -0500 | [diff] [blame] | 490 | rc = compute_max_vf_buses(dev); |
| 491 | if (rc) |
| 492 | goto fail_max_buses; |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 493 | |
| 494 | return 0; |
| 495 | |
Alexander Duyck | ea9a885 | 2015-10-29 16:20:50 -0500 | [diff] [blame] | 496 | fail_max_buses: |
| 497 | dev->sriov = NULL; |
| 498 | dev->is_physfn = 0; |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 499 | failed: |
| 500 | for (i = 0; i < PCI_SRIOV_NUM_BARS; i++) { |
Bjorn Helgaas | c1fe1f9 | 2015-03-25 16:23:45 +0800 | [diff] [blame] | 501 | res = &dev->resource[i + PCI_IOV_RESOURCES]; |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 502 | res->flags = 0; |
| 503 | } |
| 504 | |
Wei Yang | 0e6c912 | 2015-03-25 16:23:44 +0800 | [diff] [blame] | 505 | kfree(iov); |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 506 | return rc; |
| 507 | } |
| 508 | |
| 509 | static void sriov_release(struct pci_dev *dev) |
| 510 | { |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 511 | BUG_ON(dev->sriov->num_VFs); |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 512 | |
Yu Zhao | e277d2f | 2009-05-18 13:51:33 +0800 | [diff] [blame] | 513 | if (dev != dev->sriov->dev) |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 514 | pci_dev_put(dev->sriov->dev); |
| 515 | |
| 516 | kfree(dev->sriov); |
| 517 | dev->sriov = NULL; |
| 518 | } |
| 519 | |
Yu Zhao | 8c5cdb6 | 2009-03-20 11:25:12 +0800 | [diff] [blame] | 520 | static void sriov_restore_state(struct pci_dev *dev) |
| 521 | { |
| 522 | int i; |
| 523 | u16 ctrl; |
| 524 | struct pci_sriov *iov = dev->sriov; |
| 525 | |
| 526 | pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &ctrl); |
| 527 | if (ctrl & PCI_SRIOV_CTRL_VFE) |
| 528 | return; |
| 529 | |
Tony Nguyen | ff26449 | 2017-10-04 08:52:58 -0700 | [diff] [blame] | 530 | /* |
| 531 | * Restore PCI_SRIOV_CTRL_ARI before pci_iov_set_numvfs() because |
| 532 | * it reads offset & stride, which depend on PCI_SRIOV_CTRL_ARI. |
| 533 | */ |
| 534 | ctrl &= ~PCI_SRIOV_CTRL_ARI; |
| 535 | ctrl |= iov->ctrl & PCI_SRIOV_CTRL_ARI; |
| 536 | pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, ctrl); |
| 537 | |
Yu Zhao | 8c5cdb6 | 2009-03-20 11:25:12 +0800 | [diff] [blame] | 538 | for (i = PCI_IOV_RESOURCES; i <= PCI_IOV_RESOURCE_END; i++) |
| 539 | pci_update_resource(dev, i); |
| 540 | |
| 541 | 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] | 542 | pci_iov_set_numvfs(dev, iov->num_VFs); |
Yu Zhao | 8c5cdb6 | 2009-03-20 11:25:12 +0800 | [diff] [blame] | 543 | pci_write_config_word(dev, iov->pos + PCI_SRIOV_CTRL, iov->ctrl); |
| 544 | if (iov->ctrl & PCI_SRIOV_CTRL_VFE) |
| 545 | msleep(100); |
| 546 | } |
| 547 | |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 548 | /** |
| 549 | * pci_iov_init - initialize the IOV capability |
| 550 | * @dev: the PCI device |
| 551 | * |
| 552 | * Returns 0 on success, or negative on failure. |
| 553 | */ |
| 554 | int pci_iov_init(struct pci_dev *dev) |
| 555 | { |
| 556 | int pos; |
| 557 | |
Kenji Kaneshige | 5f4d91a | 2009-11-11 14:36:17 +0900 | [diff] [blame] | 558 | if (!pci_is_pcie(dev)) |
Yu Zhao | d1b054d | 2009-03-20 11:25:11 +0800 | [diff] [blame] | 559 | return -ENODEV; |
| 560 | |
| 561 | pos = pci_find_ext_capability(dev, PCI_EXT_CAP_ID_SRIOV); |
| 562 | if (pos) |
| 563 | return sriov_init(dev, pos); |
| 564 | |
| 565 | return -ENODEV; |
| 566 | } |
| 567 | |
| 568 | /** |
| 569 | * pci_iov_release - release resources used by the IOV capability |
| 570 | * @dev: the PCI device |
| 571 | */ |
| 572 | void pci_iov_release(struct pci_dev *dev) |
| 573 | { |
| 574 | if (dev->is_physfn) |
| 575 | sriov_release(dev); |
| 576 | } |
| 577 | |
| 578 | /** |
Jakub Kicinski | 3897237 | 2018-06-29 15:08:52 -0500 | [diff] [blame] | 579 | * pci_iov_remove - clean up SR-IOV state after PF driver is detached |
| 580 | * @dev: the PCI device |
| 581 | */ |
| 582 | void pci_iov_remove(struct pci_dev *dev) |
| 583 | { |
| 584 | struct pci_sriov *iov = dev->sriov; |
| 585 | |
| 586 | if (!dev->is_physfn) |
| 587 | return; |
| 588 | |
| 589 | iov->driver_max_VFs = iov->total_VFs; |
| 590 | if (iov->num_VFs) |
| 591 | pci_warn(dev, "driver left SR-IOV enabled after remove\n"); |
| 592 | } |
| 593 | |
| 594 | /** |
Bjorn Helgaas | 6ffa248 | 2016-11-28 09:15:52 -0600 | [diff] [blame] | 595 | * pci_iov_update_resource - update a VF BAR |
| 596 | * @dev: the PCI device |
| 597 | * @resno: the resource number |
| 598 | * |
| 599 | * Update a VF BAR in the SR-IOV capability of a PF. |
| 600 | */ |
| 601 | void pci_iov_update_resource(struct pci_dev *dev, int resno) |
| 602 | { |
| 603 | struct pci_sriov *iov = dev->is_physfn ? dev->sriov : NULL; |
| 604 | struct resource *res = dev->resource + resno; |
| 605 | int vf_bar = resno - PCI_IOV_RESOURCES; |
| 606 | struct pci_bus_region region; |
Bjorn Helgaas | 546ba9f | 2016-11-28 16:43:06 -0600 | [diff] [blame] | 607 | u16 cmd; |
Bjorn Helgaas | 6ffa248 | 2016-11-28 09:15:52 -0600 | [diff] [blame] | 608 | u32 new; |
| 609 | int reg; |
| 610 | |
| 611 | /* |
| 612 | * The generic pci_restore_bars() path calls this for all devices, |
| 613 | * including VFs and non-SR-IOV devices. If this is not a PF, we |
| 614 | * have nothing to do. |
| 615 | */ |
| 616 | if (!iov) |
| 617 | return; |
| 618 | |
Bjorn Helgaas | 546ba9f | 2016-11-28 16:43:06 -0600 | [diff] [blame] | 619 | pci_read_config_word(dev, iov->pos + PCI_SRIOV_CTRL, &cmd); |
| 620 | if ((cmd & PCI_SRIOV_CTRL_VFE) && (cmd & PCI_SRIOV_CTRL_MSE)) { |
| 621 | dev_WARN(&dev->dev, "can't update enabled VF BAR%d %pR\n", |
| 622 | vf_bar, res); |
| 623 | return; |
| 624 | } |
| 625 | |
Bjorn Helgaas | 6ffa248 | 2016-11-28 09:15:52 -0600 | [diff] [blame] | 626 | /* |
| 627 | * Ignore unimplemented BARs, unused resource slots for 64-bit |
| 628 | * BARs, and non-movable resources, e.g., those described via |
| 629 | * Enhanced Allocation. |
| 630 | */ |
| 631 | if (!res->flags) |
| 632 | return; |
| 633 | |
| 634 | if (res->flags & IORESOURCE_UNSET) |
| 635 | return; |
| 636 | |
| 637 | if (res->flags & IORESOURCE_PCI_FIXED) |
| 638 | return; |
| 639 | |
| 640 | pcibios_resource_to_bus(dev->bus, ®ion, res); |
| 641 | new = region.start; |
| 642 | new |= res->flags & ~PCI_BASE_ADDRESS_MEM_MASK; |
| 643 | |
| 644 | reg = iov->pos + PCI_SRIOV_BAR + 4 * vf_bar; |
| 645 | pci_write_config_dword(dev, reg, new); |
| 646 | if (res->flags & IORESOURCE_MEM_64) { |
| 647 | new = region.start >> 16 >> 16; |
| 648 | pci_write_config_dword(dev, reg + 4, new); |
| 649 | } |
| 650 | } |
| 651 | |
Wei Yang | 978d2d6 | 2015-03-25 16:23:50 +0800 | [diff] [blame] | 652 | resource_size_t __weak pcibios_iov_resource_alignment(struct pci_dev *dev, |
| 653 | int resno) |
| 654 | { |
| 655 | return pci_iov_resource_size(dev, resno); |
| 656 | } |
| 657 | |
Yu Zhao | 8c5cdb6 | 2009-03-20 11:25:12 +0800 | [diff] [blame] | 658 | /** |
Chris Wright | 6faf17f | 2009-08-28 13:00:06 -0700 | [diff] [blame] | 659 | * pci_sriov_resource_alignment - get resource alignment for VF BAR |
| 660 | * @dev: the PCI device |
| 661 | * @resno: the resource number |
| 662 | * |
| 663 | * Returns the alignment of the VF BAR found in the SR-IOV capability. |
| 664 | * This is not the same as the resource size which is defined as |
| 665 | * the VF BAR size multiplied by the number of VFs. The alignment |
| 666 | * is just the VF BAR size. |
| 667 | */ |
Cam Macdonell | 0e52247 | 2010-09-07 17:25:20 -0700 | [diff] [blame] | 668 | 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] | 669 | { |
Wei Yang | 978d2d6 | 2015-03-25 16:23:50 +0800 | [diff] [blame] | 670 | return pcibios_iov_resource_alignment(dev, resno); |
Chris Wright | 6faf17f | 2009-08-28 13:00:06 -0700 | [diff] [blame] | 671 | } |
| 672 | |
| 673 | /** |
Yu Zhao | 8c5cdb6 | 2009-03-20 11:25:12 +0800 | [diff] [blame] | 674 | * pci_restore_iov_state - restore the state of the IOV capability |
| 675 | * @dev: the PCI device |
| 676 | */ |
| 677 | void pci_restore_iov_state(struct pci_dev *dev) |
| 678 | { |
| 679 | if (dev->is_physfn) |
| 680 | sriov_restore_state(dev); |
| 681 | } |
Yu Zhao | a28724b | 2009-03-20 11:25:13 +0800 | [diff] [blame] | 682 | |
| 683 | /** |
Bryant G. Ly | 608c0d8 | 2017-11-09 08:00:35 -0600 | [diff] [blame] | 684 | * pci_vf_drivers_autoprobe - set PF property drivers_autoprobe for VFs |
| 685 | * @dev: the PCI device |
| 686 | * @auto_probe: set VF drivers auto probe flag |
| 687 | */ |
| 688 | void pci_vf_drivers_autoprobe(struct pci_dev *dev, bool auto_probe) |
| 689 | { |
| 690 | if (dev->is_physfn) |
| 691 | dev->sriov->drivers_autoprobe = auto_probe; |
| 692 | } |
| 693 | |
| 694 | /** |
Yu Zhao | a28724b | 2009-03-20 11:25:13 +0800 | [diff] [blame] | 695 | * pci_iov_bus_range - find bus range used by Virtual Function |
| 696 | * @bus: the PCI bus |
| 697 | * |
| 698 | * Returns max number of buses (exclude current one) used by Virtual |
| 699 | * Functions. |
| 700 | */ |
| 701 | int pci_iov_bus_range(struct pci_bus *bus) |
| 702 | { |
| 703 | int max = 0; |
Yu Zhao | a28724b | 2009-03-20 11:25:13 +0800 | [diff] [blame] | 704 | struct pci_dev *dev; |
| 705 | |
| 706 | list_for_each_entry(dev, &bus->devices, bus_list) { |
| 707 | if (!dev->is_physfn) |
| 708 | continue; |
Wei Yang | 4449f07 | 2015-03-25 16:23:47 +0800 | [diff] [blame] | 709 | if (dev->sriov->max_VF_buses > max) |
| 710 | max = dev->sriov->max_VF_buses; |
Yu Zhao | a28724b | 2009-03-20 11:25:13 +0800 | [diff] [blame] | 711 | } |
| 712 | |
| 713 | return max ? max - bus->number : 0; |
| 714 | } |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 715 | |
| 716 | /** |
| 717 | * pci_enable_sriov - enable the SR-IOV capability |
| 718 | * @dev: the PCI device |
Randy Dunlap | 52a8873 | 2009-04-01 17:45:30 -0700 | [diff] [blame] | 719 | * @nr_virtfn: number of virtual functions to enable |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 720 | * |
| 721 | * Returns 0 on success, or negative on failure. |
| 722 | */ |
| 723 | int pci_enable_sriov(struct pci_dev *dev, int nr_virtfn) |
| 724 | { |
| 725 | might_sleep(); |
| 726 | |
| 727 | if (!dev->is_physfn) |
Stefan Assmann | 652d110 | 2013-07-31 16:47:56 -0600 | [diff] [blame] | 728 | return -ENOSYS; |
Yu Zhao | dd7cc44 | 2009-03-20 11:25:15 +0800 | [diff] [blame] | 729 | |
| 730 | return sriov_enable(dev, nr_virtfn); |
| 731 | } |
| 732 | EXPORT_SYMBOL_GPL(pci_enable_sriov); |
| 733 | |
| 734 | /** |
| 735 | * pci_disable_sriov - disable the SR-IOV capability |
| 736 | * @dev: the PCI device |
| 737 | */ |
| 738 | void pci_disable_sriov(struct pci_dev *dev) |
| 739 | { |
| 740 | might_sleep(); |
| 741 | |
| 742 | if (!dev->is_physfn) |
| 743 | return; |
| 744 | |
| 745 | sriov_disable(dev); |
| 746 | } |
| 747 | EXPORT_SYMBOL_GPL(pci_disable_sriov); |
Yu Zhao | 74bb1bc | 2009-03-20 11:25:16 +0800 | [diff] [blame] | 748 | |
| 749 | /** |
Williams, Mitch A | fb8a0d9 | 2010-02-10 01:43:04 +0000 | [diff] [blame] | 750 | * pci_num_vf - return number of VFs associated with a PF device_release_driver |
| 751 | * @dev: the PCI device |
| 752 | * |
| 753 | * Returns number of VFs, or 0 if SR-IOV is not enabled. |
| 754 | */ |
| 755 | int pci_num_vf(struct pci_dev *dev) |
| 756 | { |
Bjorn Helgaas | 1452cd7 | 2012-11-09 20:35:01 -0700 | [diff] [blame] | 757 | if (!dev->is_physfn) |
Williams, Mitch A | fb8a0d9 | 2010-02-10 01:43:04 +0000 | [diff] [blame] | 758 | return 0; |
Bjorn Helgaas | 1452cd7 | 2012-11-09 20:35:01 -0700 | [diff] [blame] | 759 | |
| 760 | return dev->sriov->num_VFs; |
Williams, Mitch A | fb8a0d9 | 2010-02-10 01:43:04 +0000 | [diff] [blame] | 761 | } |
| 762 | EXPORT_SYMBOL_GPL(pci_num_vf); |
Donald Dutile | bff7315 | 2012-11-05 15:20:37 -0500 | [diff] [blame] | 763 | |
| 764 | /** |
Alexander Duyck | 5a8eb24 | 2013-04-25 04:42:29 +0000 | [diff] [blame] | 765 | * pci_vfs_assigned - returns number of VFs are assigned to a guest |
| 766 | * @dev: the PCI device |
| 767 | * |
| 768 | * 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] | 769 | * If device is not a physical function returns 0. |
Alexander Duyck | 5a8eb24 | 2013-04-25 04:42:29 +0000 | [diff] [blame] | 770 | */ |
| 771 | int pci_vfs_assigned(struct pci_dev *dev) |
| 772 | { |
| 773 | struct pci_dev *vfdev; |
| 774 | unsigned int vfs_assigned = 0; |
| 775 | unsigned short dev_id; |
| 776 | |
| 777 | /* only search if we are a PF */ |
| 778 | if (!dev->is_physfn) |
| 779 | return 0; |
| 780 | |
| 781 | /* |
| 782 | * determine the device ID for the VFs, the vendor ID will be the |
| 783 | * same as the PF so there is no need to check for that one |
| 784 | */ |
Filippo Sironi | 3142d83 | 2017-08-28 15:38:49 +0200 | [diff] [blame] | 785 | dev_id = dev->sriov->vf_device; |
Alexander Duyck | 5a8eb24 | 2013-04-25 04:42:29 +0000 | [diff] [blame] | 786 | |
| 787 | /* loop through all the VFs to see if we own any that are assigned */ |
| 788 | vfdev = pci_get_device(dev->vendor, dev_id, NULL); |
| 789 | while (vfdev) { |
| 790 | /* |
| 791 | * It is considered assigned if it is a virtual function with |
| 792 | * our dev as the physical function and the assigned bit is set |
| 793 | */ |
| 794 | if (vfdev->is_virtfn && (vfdev->physfn == dev) && |
Ethan Zhao | be63497 | 2014-09-09 10:21:28 +0800 | [diff] [blame] | 795 | pci_is_dev_assigned(vfdev)) |
Alexander Duyck | 5a8eb24 | 2013-04-25 04:42:29 +0000 | [diff] [blame] | 796 | vfs_assigned++; |
| 797 | |
| 798 | vfdev = pci_get_device(dev->vendor, dev_id, vfdev); |
| 799 | } |
| 800 | |
| 801 | return vfs_assigned; |
| 802 | } |
| 803 | EXPORT_SYMBOL_GPL(pci_vfs_assigned); |
| 804 | |
| 805 | /** |
Donald Dutile | bff7315 | 2012-11-05 15:20:37 -0500 | [diff] [blame] | 806 | * pci_sriov_set_totalvfs -- reduce the TotalVFs available |
| 807 | * @dev: the PCI PF device |
Randy Dunlap | 2094f16 | 2013-01-09 17:12:52 -0800 | [diff] [blame] | 808 | * @numvfs: number that should be used for TotalVFs supported |
Donald Dutile | bff7315 | 2012-11-05 15:20:37 -0500 | [diff] [blame] | 809 | * |
| 810 | * Should be called from PF driver's probe routine with |
| 811 | * device's mutex held. |
| 812 | * |
| 813 | * Returns 0 if PF is an SRIOV-capable device and |
Stefan Assmann | 652d110 | 2013-07-31 16:47:56 -0600 | [diff] [blame] | 814 | * value of numvfs valid. If not a PF return -ENOSYS; |
| 815 | * if numvfs is invalid return -EINVAL; |
Donald Dutile | bff7315 | 2012-11-05 15:20:37 -0500 | [diff] [blame] | 816 | * if VFs already enabled, return -EBUSY. |
| 817 | */ |
| 818 | int pci_sriov_set_totalvfs(struct pci_dev *dev, u16 numvfs) |
| 819 | { |
Stefan Assmann | 652d110 | 2013-07-31 16:47:56 -0600 | [diff] [blame] | 820 | if (!dev->is_physfn) |
| 821 | return -ENOSYS; |
Bjorn Helgaas | 51259d0 | 2018-05-25 08:51:50 -0500 | [diff] [blame] | 822 | |
Stefan Assmann | 652d110 | 2013-07-31 16:47:56 -0600 | [diff] [blame] | 823 | if (numvfs > dev->sriov->total_VFs) |
Donald Dutile | bff7315 | 2012-11-05 15:20:37 -0500 | [diff] [blame] | 824 | return -EINVAL; |
| 825 | |
| 826 | /* Shouldn't change if VFs already enabled */ |
| 827 | if (dev->sriov->ctrl & PCI_SRIOV_CTRL_VFE) |
| 828 | return -EBUSY; |
Donald Dutile | bff7315 | 2012-11-05 15:20:37 -0500 | [diff] [blame] | 829 | |
Bjorn Helgaas | 51259d0 | 2018-05-25 08:51:50 -0500 | [diff] [blame] | 830 | dev->sriov->driver_max_VFs = numvfs; |
Donald Dutile | bff7315 | 2012-11-05 15:20:37 -0500 | [diff] [blame] | 831 | return 0; |
| 832 | } |
| 833 | EXPORT_SYMBOL_GPL(pci_sriov_set_totalvfs); |
| 834 | |
| 835 | /** |
Jonghwan Choi | ddc191f | 2013-07-08 14:02:43 -0600 | [diff] [blame] | 836 | * pci_sriov_get_totalvfs -- get total VFs supported on this device |
Donald Dutile | bff7315 | 2012-11-05 15:20:37 -0500 | [diff] [blame] | 837 | * @dev: the PCI PF device |
| 838 | * |
| 839 | * For a PCIe device with SRIOV support, return the PCIe |
Bjorn Helgaas | 6b13672 | 2012-11-09 20:27:53 -0700 | [diff] [blame] | 840 | * SRIOV capability value of TotalVFs or the value of driver_max_VFs |
Stefan Assmann | 652d110 | 2013-07-31 16:47:56 -0600 | [diff] [blame] | 841 | * if the driver reduced it. Otherwise 0. |
Donald Dutile | bff7315 | 2012-11-05 15:20:37 -0500 | [diff] [blame] | 842 | */ |
| 843 | int pci_sriov_get_totalvfs(struct pci_dev *dev) |
| 844 | { |
Bjorn Helgaas | 1452cd7 | 2012-11-09 20:35:01 -0700 | [diff] [blame] | 845 | if (!dev->is_physfn) |
Stefan Assmann | 652d110 | 2013-07-31 16:47:56 -0600 | [diff] [blame] | 846 | return 0; |
Donald Dutile | bff7315 | 2012-11-05 15:20:37 -0500 | [diff] [blame] | 847 | |
Jakub Kicinski | 8d85a7a | 2018-05-25 08:18:34 -0500 | [diff] [blame] | 848 | return dev->sriov->driver_max_VFs; |
Donald Dutile | bff7315 | 2012-11-05 15:20:37 -0500 | [diff] [blame] | 849 | } |
| 850 | EXPORT_SYMBOL_GPL(pci_sriov_get_totalvfs); |
Alexander Duyck | 8effc39 | 2018-04-21 15:23:09 -0500 | [diff] [blame] | 851 | |
| 852 | /** |
| 853 | * pci_sriov_configure_simple - helper to configure SR-IOV |
| 854 | * @dev: the PCI device |
| 855 | * @nr_virtfn: number of virtual functions to enable, 0 to disable |
| 856 | * |
| 857 | * Enable or disable SR-IOV for devices that don't require any PF setup |
| 858 | * before enabling SR-IOV. Return value is negative on error, or number of |
| 859 | * VFs allocated on success. |
| 860 | */ |
| 861 | int pci_sriov_configure_simple(struct pci_dev *dev, int nr_virtfn) |
| 862 | { |
| 863 | int rc; |
| 864 | |
| 865 | might_sleep(); |
| 866 | |
| 867 | if (!dev->is_physfn) |
| 868 | return -ENODEV; |
| 869 | |
| 870 | if (pci_vfs_assigned(dev)) { |
| 871 | pci_warn(dev, "Cannot modify SR-IOV while VFs are assigned\n"); |
| 872 | return -EPERM; |
| 873 | } |
| 874 | |
| 875 | if (nr_virtfn == 0) { |
| 876 | sriov_disable(dev); |
| 877 | return 0; |
| 878 | } |
| 879 | |
| 880 | rc = sriov_enable(dev, nr_virtfn); |
| 881 | if (rc < 0) |
| 882 | return rc; |
| 883 | |
| 884 | return nr_virtfn; |
| 885 | } |
| 886 | EXPORT_SYMBOL_GPL(pci_sriov_configure_simple); |