Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 2 | /* |
| 3 | * PCI Backend - Provides a Virtual PCI bus (with real devices) |
| 4 | * to the frontend |
| 5 | * |
| 6 | * Author: Ryan Wilson <hap9@epoch.ncsc.mil> |
| 7 | */ |
| 8 | |
Joe Perches | 283c097 | 2013-06-28 03:21:41 -0700 | [diff] [blame] | 9 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
| 10 | |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 11 | #include <linux/list.h> |
| 12 | #include <linux/slab.h> |
| 13 | #include <linux/pci.h> |
Konrad Rzeszutek Wilk | 74d33de | 2011-09-21 17:04:47 -0400 | [diff] [blame] | 14 | #include <linux/mutex.h> |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 15 | #include "pciback.h" |
| 16 | |
| 17 | #define PCI_SLOT_MAX 32 |
| 18 | |
| 19 | struct vpci_dev_data { |
| 20 | /* Access to dev_list must be protected by lock */ |
| 21 | struct list_head dev_list[PCI_SLOT_MAX]; |
Konrad Rzeszutek Wilk | 74d33de | 2011-09-21 17:04:47 -0400 | [diff] [blame] | 22 | struct mutex lock; |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 23 | }; |
| 24 | |
| 25 | static inline struct list_head *list_first(struct list_head *head) |
| 26 | { |
| 27 | return head->next; |
| 28 | } |
| 29 | |
Konrad Rzeszutek Wilk | 2ebdc42 | 2011-07-11 16:49:41 -0400 | [diff] [blame] | 30 | static struct pci_dev *__xen_pcibk_get_pci_dev(struct xen_pcibk_device *pdev, |
| 31 | unsigned int domain, |
| 32 | unsigned int bus, |
| 33 | unsigned int devfn) |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 34 | { |
| 35 | struct pci_dev_entry *entry; |
| 36 | struct pci_dev *dev = NULL; |
| 37 | struct vpci_dev_data *vpci_dev = pdev->pci_dev_data; |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 38 | |
| 39 | if (domain != 0 || bus != 0) |
| 40 | return NULL; |
| 41 | |
| 42 | if (PCI_SLOT(devfn) < PCI_SLOT_MAX) { |
Konrad Rzeszutek Wilk | 74d33de | 2011-09-21 17:04:47 -0400 | [diff] [blame] | 43 | mutex_lock(&vpci_dev->lock); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 44 | |
| 45 | list_for_each_entry(entry, |
| 46 | &vpci_dev->dev_list[PCI_SLOT(devfn)], |
| 47 | list) { |
| 48 | if (PCI_FUNC(entry->dev->devfn) == PCI_FUNC(devfn)) { |
| 49 | dev = entry->dev; |
| 50 | break; |
| 51 | } |
| 52 | } |
| 53 | |
Konrad Rzeszutek Wilk | 74d33de | 2011-09-21 17:04:47 -0400 | [diff] [blame] | 54 | mutex_unlock(&vpci_dev->lock); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 55 | } |
| 56 | return dev; |
| 57 | } |
| 58 | |
| 59 | static inline int match_slot(struct pci_dev *l, struct pci_dev *r) |
| 60 | { |
| 61 | if (pci_domain_nr(l->bus) == pci_domain_nr(r->bus) |
| 62 | && l->bus == r->bus && PCI_SLOT(l->devfn) == PCI_SLOT(r->devfn)) |
| 63 | return 1; |
| 64 | |
| 65 | return 0; |
| 66 | } |
| 67 | |
Konrad Rzeszutek Wilk | 2ebdc42 | 2011-07-11 16:49:41 -0400 | [diff] [blame] | 68 | static int __xen_pcibk_add_pci_dev(struct xen_pcibk_device *pdev, |
| 69 | struct pci_dev *dev, int devid, |
| 70 | publish_pci_dev_cb publish_cb) |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 71 | { |
Jan Beulich | 3ae527e | 2021-05-18 18:13:42 +0200 | [diff] [blame] | 72 | int err = 0, slot, func = PCI_FUNC(dev->devfn); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 73 | struct pci_dev_entry *t, *dev_entry; |
| 74 | struct vpci_dev_data *vpci_dev = pdev->pci_dev_data; |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 75 | |
| 76 | if ((dev->class >> 24) == PCI_BASE_CLASS_BRIDGE) { |
| 77 | err = -EFAULT; |
| 78 | xenbus_dev_fatal(pdev->xdev, err, |
| 79 | "Can't export bridges on the virtual PCI bus"); |
| 80 | goto out; |
| 81 | } |
| 82 | |
| 83 | dev_entry = kmalloc(sizeof(*dev_entry), GFP_KERNEL); |
| 84 | if (!dev_entry) { |
| 85 | err = -ENOMEM; |
| 86 | xenbus_dev_fatal(pdev->xdev, err, |
| 87 | "Error adding entry to virtual PCI bus"); |
| 88 | goto out; |
| 89 | } |
| 90 | |
| 91 | dev_entry->dev = dev; |
| 92 | |
Konrad Rzeszutek Wilk | 74d33de | 2011-09-21 17:04:47 -0400 | [diff] [blame] | 93 | mutex_lock(&vpci_dev->lock); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 94 | |
Laszlo Ersek | 8a5248f | 2012-10-17 11:55:55 +0200 | [diff] [blame] | 95 | /* |
| 96 | * Keep multi-function devices together on the virtual PCI bus, except |
Jan Beulich | 3ae527e | 2021-05-18 18:13:42 +0200 | [diff] [blame] | 97 | * that we want to keep virtual functions at func 0 on their own. They |
| 98 | * aren't multi-function devices and hence their presence at func 0 |
| 99 | * may cause guests to not scan the other functions. |
Laszlo Ersek | 8a5248f | 2012-10-17 11:55:55 +0200 | [diff] [blame] | 100 | */ |
Jan Beulich | 3ae527e | 2021-05-18 18:13:42 +0200 | [diff] [blame] | 101 | if (!dev->is_virtfn || func) { |
Laszlo Ersek | 8a5248f | 2012-10-17 11:55:55 +0200 | [diff] [blame] | 102 | for (slot = 0; slot < PCI_SLOT_MAX; slot++) { |
| 103 | if (list_empty(&vpci_dev->dev_list[slot])) |
| 104 | continue; |
| 105 | |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 106 | t = list_entry(list_first(&vpci_dev->dev_list[slot]), |
| 107 | struct pci_dev_entry, list); |
Jan Beulich | 3ae527e | 2021-05-18 18:13:42 +0200 | [diff] [blame] | 108 | if (t->dev->is_virtfn && !PCI_FUNC(t->dev->devfn)) |
| 109 | continue; |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 110 | |
| 111 | if (match_slot(dev, t->dev)) { |
Joe Perches | 283c097 | 2013-06-28 03:21:41 -0700 | [diff] [blame] | 112 | pr_info("vpci: %s: assign to virtual slot %d func %d\n", |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 113 | pci_name(dev), slot, |
Jan Beulich | 3ae527e | 2021-05-18 18:13:42 +0200 | [diff] [blame] | 114 | func); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 115 | list_add_tail(&dev_entry->list, |
| 116 | &vpci_dev->dev_list[slot]); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 117 | goto unlock; |
| 118 | } |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | /* Assign to a new slot on the virtual PCI bus */ |
| 123 | for (slot = 0; slot < PCI_SLOT_MAX; slot++) { |
| 124 | if (list_empty(&vpci_dev->dev_list[slot])) { |
Joe Perches | 283c097 | 2013-06-28 03:21:41 -0700 | [diff] [blame] | 125 | pr_info("vpci: %s: assign to virtual slot %d\n", |
| 126 | pci_name(dev), slot); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 127 | list_add_tail(&dev_entry->list, |
| 128 | &vpci_dev->dev_list[slot]); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 129 | goto unlock; |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | err = -ENOMEM; |
| 134 | xenbus_dev_fatal(pdev->xdev, err, |
| 135 | "No more space on root virtual PCI bus"); |
| 136 | |
| 137 | unlock: |
Konrad Rzeszutek Wilk | 74d33de | 2011-09-21 17:04:47 -0400 | [diff] [blame] | 138 | mutex_unlock(&vpci_dev->lock); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 139 | |
| 140 | /* Publish this device. */ |
| 141 | if (!err) |
| 142 | err = publish_cb(pdev, 0, 0, PCI_DEVFN(slot, func), devid); |
Daeseok Youn | cea37f8 | 2014-04-01 19:15:59 +0900 | [diff] [blame] | 143 | else |
| 144 | kfree(dev_entry); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 145 | |
| 146 | out: |
| 147 | return err; |
| 148 | } |
| 149 | |
Konrad Rzeszutek Wilk | 2ebdc42 | 2011-07-11 16:49:41 -0400 | [diff] [blame] | 150 | static void __xen_pcibk_release_pci_dev(struct xen_pcibk_device *pdev, |
Konrad Rzeszutek Wilk | e8801a7 | 2014-12-03 16:40:26 -0500 | [diff] [blame] | 151 | struct pci_dev *dev, bool lock) |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 152 | { |
| 153 | int slot; |
| 154 | struct vpci_dev_data *vpci_dev = pdev->pci_dev_data; |
| 155 | struct pci_dev *found_dev = NULL; |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 156 | |
Konrad Rzeszutek Wilk | 74d33de | 2011-09-21 17:04:47 -0400 | [diff] [blame] | 157 | mutex_lock(&vpci_dev->lock); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 158 | |
| 159 | for (slot = 0; slot < PCI_SLOT_MAX; slot++) { |
Jan Beulich | 402c5e1 | 2011-09-21 16:22:11 -0400 | [diff] [blame] | 160 | struct pci_dev_entry *e; |
| 161 | |
| 162 | list_for_each_entry(e, &vpci_dev->dev_list[slot], list) { |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 163 | if (e->dev == dev) { |
| 164 | list_del(&e->list); |
| 165 | found_dev = e->dev; |
| 166 | kfree(e); |
| 167 | goto out; |
| 168 | } |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | out: |
Konrad Rzeszutek Wilk | 74d33de | 2011-09-21 17:04:47 -0400 | [diff] [blame] | 173 | mutex_unlock(&vpci_dev->lock); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 174 | |
Konrad Rzeszutek Wilk | e8801a7 | 2014-12-03 16:40:26 -0500 | [diff] [blame] | 175 | if (found_dev) { |
| 176 | if (lock) |
| 177 | device_lock(&found_dev->dev); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 178 | pcistub_put_pci_dev(found_dev); |
Konrad Rzeszutek Wilk | e8801a7 | 2014-12-03 16:40:26 -0500 | [diff] [blame] | 179 | if (lock) |
| 180 | device_unlock(&found_dev->dev); |
| 181 | } |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 182 | } |
| 183 | |
Konrad Rzeszutek Wilk | 2ebdc42 | 2011-07-11 16:49:41 -0400 | [diff] [blame] | 184 | static int __xen_pcibk_init_devices(struct xen_pcibk_device *pdev) |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 185 | { |
| 186 | int slot; |
| 187 | struct vpci_dev_data *vpci_dev; |
| 188 | |
| 189 | vpci_dev = kmalloc(sizeof(*vpci_dev), GFP_KERNEL); |
| 190 | if (!vpci_dev) |
| 191 | return -ENOMEM; |
| 192 | |
Konrad Rzeszutek Wilk | 74d33de | 2011-09-21 17:04:47 -0400 | [diff] [blame] | 193 | mutex_init(&vpci_dev->lock); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 194 | |
| 195 | for (slot = 0; slot < PCI_SLOT_MAX; slot++) |
| 196 | INIT_LIST_HEAD(&vpci_dev->dev_list[slot]); |
| 197 | |
| 198 | pdev->pci_dev_data = vpci_dev; |
| 199 | |
| 200 | return 0; |
| 201 | } |
| 202 | |
Konrad Rzeszutek Wilk | 2ebdc42 | 2011-07-11 16:49:41 -0400 | [diff] [blame] | 203 | static int __xen_pcibk_publish_pci_roots(struct xen_pcibk_device *pdev, |
| 204 | publish_pci_root_cb publish_cb) |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 205 | { |
| 206 | /* The Virtual PCI bus has only one root */ |
| 207 | return publish_cb(pdev, 0, 0); |
| 208 | } |
| 209 | |
Konrad Rzeszutek Wilk | 2ebdc42 | 2011-07-11 16:49:41 -0400 | [diff] [blame] | 210 | static void __xen_pcibk_release_devices(struct xen_pcibk_device *pdev) |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 211 | { |
| 212 | int slot; |
| 213 | struct vpci_dev_data *vpci_dev = pdev->pci_dev_data; |
| 214 | |
| 215 | for (slot = 0; slot < PCI_SLOT_MAX; slot++) { |
| 216 | struct pci_dev_entry *e, *tmp; |
| 217 | list_for_each_entry_safe(e, tmp, &vpci_dev->dev_list[slot], |
| 218 | list) { |
Konrad Rzeszutek Wilk | e8801a7 | 2014-12-03 16:40:26 -0500 | [diff] [blame] | 219 | struct pci_dev *dev = e->dev; |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 220 | list_del(&e->list); |
Konrad Rzeszutek Wilk | e8801a7 | 2014-12-03 16:40:26 -0500 | [diff] [blame] | 221 | device_lock(&dev->dev); |
| 222 | pcistub_put_pci_dev(dev); |
| 223 | device_unlock(&dev->dev); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 224 | kfree(e); |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | kfree(vpci_dev); |
| 229 | pdev->pci_dev_data = NULL; |
| 230 | } |
| 231 | |
Konrad Rzeszutek Wilk | 2ebdc42 | 2011-07-11 16:49:41 -0400 | [diff] [blame] | 232 | static int __xen_pcibk_get_pcifront_dev(struct pci_dev *pcidev, |
| 233 | struct xen_pcibk_device *pdev, |
| 234 | unsigned int *domain, unsigned int *bus, |
| 235 | unsigned int *devfn) |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 236 | { |
| 237 | struct pci_dev_entry *entry; |
| 238 | struct pci_dev *dev = NULL; |
| 239 | struct vpci_dev_data *vpci_dev = pdev->pci_dev_data; |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 240 | int found = 0, slot; |
| 241 | |
Konrad Rzeszutek Wilk | 74d33de | 2011-09-21 17:04:47 -0400 | [diff] [blame] | 242 | mutex_lock(&vpci_dev->lock); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 243 | for (slot = 0; slot < PCI_SLOT_MAX; slot++) { |
| 244 | list_for_each_entry(entry, |
| 245 | &vpci_dev->dev_list[slot], |
| 246 | list) { |
| 247 | dev = entry->dev; |
| 248 | if (dev && dev->bus->number == pcidev->bus->number |
| 249 | && pci_domain_nr(dev->bus) == |
| 250 | pci_domain_nr(pcidev->bus) |
| 251 | && dev->devfn == pcidev->devfn) { |
| 252 | found = 1; |
| 253 | *domain = 0; |
| 254 | *bus = 0; |
| 255 | *devfn = PCI_DEVFN(slot, |
| 256 | PCI_FUNC(pcidev->devfn)); |
| 257 | } |
| 258 | } |
| 259 | } |
Dan Carpenter | e1db4ce | 2011-09-27 10:07:21 +0300 | [diff] [blame] | 260 | mutex_unlock(&vpci_dev->lock); |
Konrad Rzeszutek Wilk | 30edc14 | 2009-10-13 17:22:20 -0400 | [diff] [blame] | 261 | return found; |
| 262 | } |
Konrad Rzeszutek Wilk | 2ebdc42 | 2011-07-11 16:49:41 -0400 | [diff] [blame] | 263 | |
Jan Beulich | 402c5e1 | 2011-09-21 16:22:11 -0400 | [diff] [blame] | 264 | const struct xen_pcibk_backend xen_pcibk_vpci_backend = { |
Konrad Rzeszutek Wilk | 2ebdc42 | 2011-07-11 16:49:41 -0400 | [diff] [blame] | 265 | .name = "vpci", |
| 266 | .init = __xen_pcibk_init_devices, |
| 267 | .free = __xen_pcibk_release_devices, |
| 268 | .find = __xen_pcibk_get_pcifront_dev, |
| 269 | .publish = __xen_pcibk_publish_pci_roots, |
| 270 | .release = __xen_pcibk_release_pci_dev, |
| 271 | .add = __xen_pcibk_add_pci_dev, |
| 272 | .get = __xen_pcibk_get_pci_dev, |
| 273 | }; |