blob: 66e9b814cc8615d636f2e56e27dcc31a34a28cab [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04002/*
3 * PCI Backend - Provides restricted access to the real PCI bus topology
4 * to the frontend
5 *
6 * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
7 */
8
9#include <linux/list.h>
10#include <linux/pci.h>
Jan Beulich04df3552011-09-19 17:32:15 +010011#include <linux/mutex.h>
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040012#include "pciback.h"
13
14struct passthrough_dev_data {
15 /* Access to dev_list must be protected by lock */
16 struct list_head dev_list;
Jan Beulich04df3552011-09-19 17:32:15 +010017 struct mutex lock;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040018};
19
Konrad Rzeszutek Wilk2ebdc422011-07-11 16:49:41 -040020static struct pci_dev *__xen_pcibk_get_pci_dev(struct xen_pcibk_device *pdev,
21 unsigned int domain,
22 unsigned int bus,
23 unsigned int devfn)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040024{
25 struct passthrough_dev_data *dev_data = pdev->pci_dev_data;
26 struct pci_dev_entry *dev_entry;
27 struct pci_dev *dev = NULL;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040028
Jan Beulich04df3552011-09-19 17:32:15 +010029 mutex_lock(&dev_data->lock);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040030
31 list_for_each_entry(dev_entry, &dev_data->dev_list, list) {
32 if (domain == (unsigned int)pci_domain_nr(dev_entry->dev->bus)
33 && bus == (unsigned int)dev_entry->dev->bus->number
34 && devfn == dev_entry->dev->devfn) {
35 dev = dev_entry->dev;
36 break;
37 }
38 }
39
Jan Beulich04df3552011-09-19 17:32:15 +010040 mutex_unlock(&dev_data->lock);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040041
42 return dev;
43}
44
Konrad Rzeszutek Wilk2ebdc422011-07-11 16:49:41 -040045static int __xen_pcibk_add_pci_dev(struct xen_pcibk_device *pdev,
46 struct pci_dev *dev,
47 int devid, publish_pci_dev_cb publish_cb)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040048{
49 struct passthrough_dev_data *dev_data = pdev->pci_dev_data;
50 struct pci_dev_entry *dev_entry;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040051 unsigned int domain, bus, devfn;
52 int err;
53
54 dev_entry = kmalloc(sizeof(*dev_entry), GFP_KERNEL);
55 if (!dev_entry)
56 return -ENOMEM;
57 dev_entry->dev = dev;
58
Jan Beulich04df3552011-09-19 17:32:15 +010059 mutex_lock(&dev_data->lock);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040060 list_add_tail(&dev_entry->list, &dev_data->dev_list);
Jan Beulich04df3552011-09-19 17:32:15 +010061 mutex_unlock(&dev_data->lock);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040062
63 /* Publish this device. */
64 domain = (unsigned int)pci_domain_nr(dev->bus);
65 bus = (unsigned int)dev->bus->number;
66 devfn = dev->devfn;
67 err = publish_cb(pdev, domain, bus, devfn, devid);
68
69 return err;
70}
71
Konrad Rzeszutek Wilk2ebdc422011-07-11 16:49:41 -040072static void __xen_pcibk_release_pci_dev(struct xen_pcibk_device *pdev,
Konrad Rzeszutek Wilke8801a72014-12-03 16:40:26 -050073 struct pci_dev *dev, bool lock)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040074{
75 struct passthrough_dev_data *dev_data = pdev->pci_dev_data;
76 struct pci_dev_entry *dev_entry, *t;
77 struct pci_dev *found_dev = NULL;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040078
Jan Beulich04df3552011-09-19 17:32:15 +010079 mutex_lock(&dev_data->lock);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040080
81 list_for_each_entry_safe(dev_entry, t, &dev_data->dev_list, list) {
82 if (dev_entry->dev == dev) {
83 list_del(&dev_entry->list);
84 found_dev = dev_entry->dev;
85 kfree(dev_entry);
86 }
87 }
88
Jan Beulich04df3552011-09-19 17:32:15 +010089 mutex_unlock(&dev_data->lock);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040090
Konrad Rzeszutek Wilke8801a72014-12-03 16:40:26 -050091 if (found_dev) {
92 if (lock)
93 device_lock(&found_dev->dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040094 pcistub_put_pci_dev(found_dev);
Konrad Rzeszutek Wilke8801a72014-12-03 16:40:26 -050095 if (lock)
96 device_unlock(&found_dev->dev);
97 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040098}
99
Konrad Rzeszutek Wilk2ebdc422011-07-11 16:49:41 -0400100static int __xen_pcibk_init_devices(struct xen_pcibk_device *pdev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400101{
102 struct passthrough_dev_data *dev_data;
103
104 dev_data = kmalloc(sizeof(*dev_data), GFP_KERNEL);
105 if (!dev_data)
106 return -ENOMEM;
107
Jan Beulich04df3552011-09-19 17:32:15 +0100108 mutex_init(&dev_data->lock);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400109
110 INIT_LIST_HEAD(&dev_data->dev_list);
111
112 pdev->pci_dev_data = dev_data;
113
114 return 0;
115}
116
Konrad Rzeszutek Wilk2ebdc422011-07-11 16:49:41 -0400117static int __xen_pcibk_publish_pci_roots(struct xen_pcibk_device *pdev,
118 publish_pci_root_cb publish_root_cb)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400119{
120 int err = 0;
121 struct passthrough_dev_data *dev_data = pdev->pci_dev_data;
Jan Beulich04df3552011-09-19 17:32:15 +0100122 struct pci_dev_entry *dev_entry, *e;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400123 struct pci_dev *dev;
124 int found;
125 unsigned int domain, bus;
126
Jan Beulich04df3552011-09-19 17:32:15 +0100127 mutex_lock(&dev_data->lock);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400128
Jan Beulich04df3552011-09-19 17:32:15 +0100129 list_for_each_entry(dev_entry, &dev_data->dev_list, list) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400130 /* Only publish this device as a root if none of its
131 * parent bridges are exported
132 */
133 found = 0;
134 dev = dev_entry->dev->bus->self;
135 for (; !found && dev != NULL; dev = dev->bus->self) {
136 list_for_each_entry(e, &dev_data->dev_list, list) {
137 if (dev == e->dev) {
138 found = 1;
139 break;
140 }
141 }
142 }
143
144 domain = (unsigned int)pci_domain_nr(dev_entry->dev->bus);
145 bus = (unsigned int)dev_entry->dev->bus->number;
146
147 if (!found) {
148 err = publish_root_cb(pdev, domain, bus);
149 if (err)
150 break;
151 }
152 }
153
Jan Beulich04df3552011-09-19 17:32:15 +0100154 mutex_unlock(&dev_data->lock);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400155
156 return err;
157}
158
Konrad Rzeszutek Wilk2ebdc422011-07-11 16:49:41 -0400159static void __xen_pcibk_release_devices(struct xen_pcibk_device *pdev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400160{
161 struct passthrough_dev_data *dev_data = pdev->pci_dev_data;
162 struct pci_dev_entry *dev_entry, *t;
163
164 list_for_each_entry_safe(dev_entry, t, &dev_data->dev_list, list) {
Konrad Rzeszutek Wilke8801a72014-12-03 16:40:26 -0500165 struct pci_dev *dev = dev_entry->dev;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400166 list_del(&dev_entry->list);
Konrad Rzeszutek Wilke8801a72014-12-03 16:40:26 -0500167 device_lock(&dev->dev);
168 pcistub_put_pci_dev(dev);
169 device_unlock(&dev->dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400170 kfree(dev_entry);
171 }
172
173 kfree(dev_data);
174 pdev->pci_dev_data = NULL;
175}
176
Konrad Rzeszutek Wilk2ebdc422011-07-11 16:49:41 -0400177static int __xen_pcibk_get_pcifront_dev(struct pci_dev *pcidev,
178 struct xen_pcibk_device *pdev,
179 unsigned int *domain, unsigned int *bus,
180 unsigned int *devfn)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400181{
182 *domain = pci_domain_nr(pcidev->bus);
183 *bus = pcidev->bus->number;
184 *devfn = pcidev->devfn;
185 return 1;
186}
Konrad Rzeszutek Wilk2ebdc422011-07-11 16:49:41 -0400187
Jan Beulich402c5e12011-09-21 16:22:11 -0400188const struct xen_pcibk_backend xen_pcibk_passthrough_backend = {
Konrad Rzeszutek Wilk2ebdc422011-07-11 16:49:41 -0400189 .name = "passthrough",
190 .init = __xen_pcibk_init_devices,
191 .free = __xen_pcibk_release_devices,
192 .find = __xen_pcibk_get_pcifront_dev,
193 .publish = __xen_pcibk_publish_pci_roots,
194 .release = __xen_pcibk_release_pci_dev,
195 .add = __xen_pcibk_add_pci_dev,
196 .get = __xen_pcibk_get_pci_dev,
197};