blob: 3814b44bf1f76e2dc16964c557f731315a4c8876 [file] [log] [blame]
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -04001/*
2 * PCI Backend Xenbus Setup - handles setup with frontend and xend
3 *
4 * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
5 */
Joe Perches283c0972013-06-28 03:21:41 -07006
7#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
8
Paul Gortmaker59aa56b2016-02-21 19:06:04 -05009#include <linux/moduleparam.h>
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040010#include <linux/init.h>
11#include <linux/list.h>
12#include <linux/vmalloc.h>
13#include <linux/workqueue.h>
14#include <xen/xenbus.h>
15#include <xen/events.h>
Konrad Rzeszutek Wilk6221a9b2009-12-09 17:43:15 -050016#include <asm/xen/pci.h>
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040017#include "pciback.h"
18
19#define INVALID_EVTCHN_IRQ (-1)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040020
Rusty Russell90ab5ee2012-01-13 09:32:20 +103021static bool __read_mostly passthrough;
Konrad Rzeszutek Wilk2ebdc422011-07-11 16:49:41 -040022module_param(passthrough, bool, S_IRUGO);
23MODULE_PARM_DESC(passthrough,
24 "Option to specify how to export PCI topology to guest:\n"\
25 " 0 - (default) Hide the true PCI topology and makes the frontend\n"\
26 " there is a single PCI bus with only the exported devices on it.\n"\
27 " For example, a device at 03:05.0 will be re-assigned to 00:00.0\n"\
28 " while second device at 02:1a.1 will be re-assigned to 00:01.1.\n"\
29 " 1 - Passthrough provides a real view of the PCI topology to the\n"\
30 " frontend (for example, a device at 06:01.b will still appear at\n"\
31 " 06:01.b to the frontend). This is similar to how Xen 2.0.x\n"\
32 " exposed PCI devices to its driver domains. This may be required\n"\
33 " for drivers which depend on finding their hardward in certain\n"\
34 " bus/slot locations.");
35
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040036static struct xen_pcibk_device *alloc_pdev(struct xenbus_device *xdev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040037{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040038 struct xen_pcibk_device *pdev;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040039
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040040 pdev = kzalloc(sizeof(struct xen_pcibk_device), GFP_KERNEL);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040041 if (pdev == NULL)
42 goto out;
43 dev_dbg(&xdev->dev, "allocated pdev @ 0x%p\n", pdev);
44
45 pdev->xdev = xdev;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040046
Konrad Rzeszutek Wilkb1766b62011-09-16 14:43:14 -040047 mutex_init(&pdev->dev_lock);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040048
49 pdev->sh_info = NULL;
50 pdev->evtchn_irq = INVALID_EVTCHN_IRQ;
51 pdev->be_watching = 0;
52
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040053 INIT_WORK(&pdev->op_work, xen_pcibk_do_op);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040054
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040055 if (xen_pcibk_init_devices(pdev)) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040056 kfree(pdev);
57 pdev = NULL;
58 }
Doug Goldstein584a5612015-11-26 14:32:39 -060059
60 dev_set_drvdata(&xdev->dev, pdev);
61
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040062out:
63 return pdev;
64}
65
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040066static void xen_pcibk_disconnect(struct xen_pcibk_device *pdev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040067{
Konrad Rzeszutek Wilkb1766b62011-09-16 14:43:14 -040068 mutex_lock(&pdev->dev_lock);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040069 /* Ensure the guest can't trigger our handler before removing devices */
70 if (pdev->evtchn_irq != INVALID_EVTCHN_IRQ) {
71 unbind_from_irqhandler(pdev->evtchn_irq, pdev);
72 pdev->evtchn_irq = INVALID_EVTCHN_IRQ;
73 }
74
75 /* If the driver domain started an op, make sure we complete it
76 * before releasing the shared memory */
Konrad Rzeszutek Wilk494ef202010-07-23 14:35:47 -040077
Bhaktipriya Shridhar429eafe2016-06-01 19:45:08 +053078 flush_work(&pdev->op_work);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040079
80 if (pdev->sh_info != NULL) {
81 xenbus_unmap_ring_vfree(pdev->xdev, pdev->sh_info);
82 pdev->sh_info = NULL;
83 }
Konrad Rzeszutek Wilkb1766b62011-09-16 14:43:14 -040084 mutex_unlock(&pdev->dev_lock);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040085}
86
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040087static void free_pdev(struct xen_pcibk_device *pdev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040088{
Konrad Rzeszutek Wilk494ef202010-07-23 14:35:47 -040089 if (pdev->be_watching) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040090 unregister_xenbus_watch(&pdev->be_watch);
Konrad Rzeszutek Wilk494ef202010-07-23 14:35:47 -040091 pdev->be_watching = 0;
92 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040093
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040094 xen_pcibk_disconnect(pdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040095
Konrad Rzeszutek Wilk8be9df62013-12-03 21:47:37 -050096 /* N.B. This calls pcistub_put_pci_dev which does the FLR on all
97 * of the PCIe devices. */
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040098 xen_pcibk_release_devices(pdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040099
100 dev_set_drvdata(&pdev->xdev->dev, NULL);
101 pdev->xdev = NULL;
102
103 kfree(pdev);
104}
105
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400106static int xen_pcibk_do_attach(struct xen_pcibk_device *pdev, int gnt_ref,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400107 int remote_evtchn)
108{
109 int err = 0;
110 void *vaddr;
111
112 dev_dbg(&pdev->xdev->dev,
113 "Attaching to frontend resources - gnt_ref=%d evtchn=%d\n",
114 gnt_ref, remote_evtchn);
115
Wei Liuccc9d902015-04-03 14:44:59 +0800116 err = xenbus_map_ring_valloc(pdev->xdev, &gnt_ref, 1, &vaddr);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400117 if (err < 0) {
118 xenbus_dev_fatal(pdev->xdev, err,
119 "Error mapping other domain page in ours.");
120 goto out;
121 }
Konrad Rzeszutek Wilk494ef202010-07-23 14:35:47 -0400122
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400123 pdev->sh_info = vaddr;
124
125 err = bind_interdomain_evtchn_to_irqhandler(
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400126 pdev->xdev->otherend_id, remote_evtchn, xen_pcibk_handle_event,
127 0, DRV_NAME, pdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400128 if (err < 0) {
129 xenbus_dev_fatal(pdev->xdev, err,
130 "Error binding event channel to IRQ");
131 goto out;
132 }
133 pdev->evtchn_irq = err;
134 err = 0;
135
136 dev_dbg(&pdev->xdev->dev, "Attached!\n");
137out:
138 return err;
139}
140
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400141static int xen_pcibk_attach(struct xen_pcibk_device *pdev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400142{
143 int err = 0;
144 int gnt_ref, remote_evtchn;
145 char *magic = NULL;
146
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400147
Konrad Rzeszutek Wilkb1766b62011-09-16 14:43:14 -0400148 mutex_lock(&pdev->dev_lock);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400149 /* Make sure we only do this setup once */
150 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
151 XenbusStateInitialised)
152 goto out;
153
154 /* Wait for frontend to state that it has published the configuration */
155 if (xenbus_read_driver_state(pdev->xdev->otherend) !=
156 XenbusStateInitialised)
157 goto out;
158
159 dev_dbg(&pdev->xdev->dev, "Reading frontend config\n");
160
161 err = xenbus_gather(XBT_NIL, pdev->xdev->otherend,
162 "pci-op-ref", "%u", &gnt_ref,
163 "event-channel", "%u", &remote_evtchn,
164 "magic", NULL, &magic, NULL);
165 if (err) {
166 /* If configuration didn't get read correctly, wait longer */
167 xenbus_dev_fatal(pdev->xdev, err,
168 "Error reading configuration from frontend");
169 goto out;
170 }
171
172 if (magic == NULL || strcmp(magic, XEN_PCI_MAGIC) != 0) {
173 xenbus_dev_fatal(pdev->xdev, -EFAULT,
174 "version mismatch (%s/%s) with pcifront - "
Jan Beulich402c5e12011-09-21 16:22:11 -0400175 "halting " DRV_NAME,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400176 magic, XEN_PCI_MAGIC);
Wei Yongjun2ef76032014-07-20 13:46:01 +0800177 err = -EFAULT;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400178 goto out;
179 }
180
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400181 err = xen_pcibk_do_attach(pdev, gnt_ref, remote_evtchn);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400182 if (err)
183 goto out;
184
185 dev_dbg(&pdev->xdev->dev, "Connecting...\n");
186
187 err = xenbus_switch_state(pdev->xdev, XenbusStateConnected);
188 if (err)
189 xenbus_dev_fatal(pdev->xdev, err,
190 "Error switching to connected state!");
191
192 dev_dbg(&pdev->xdev->dev, "Connected? %d\n", err);
193out:
Konrad Rzeszutek Wilkb1766b62011-09-16 14:43:14 -0400194 mutex_unlock(&pdev->dev_lock);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400195
196 kfree(magic);
197
198 return err;
199}
200
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400201static int xen_pcibk_publish_pci_dev(struct xen_pcibk_device *pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400202 unsigned int domain, unsigned int bus,
203 unsigned int devfn, unsigned int devid)
204{
205 int err;
206 int len;
207 char str[64];
208
209 len = snprintf(str, sizeof(str), "vdev-%d", devid);
210 if (unlikely(len >= (sizeof(str) - 1))) {
211 err = -ENOMEM;
212 goto out;
213 }
214
Konrad Rzeszutek Wilke4de8662012-01-25 16:00:00 -0500215 /* Note: The PV protocol uses %02x, don't change it */
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400216 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
217 "%04x:%02x:%02x.%02x", domain, bus,
218 PCI_SLOT(devfn), PCI_FUNC(devfn));
219
220out:
221 return err;
222}
223
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400224static int xen_pcibk_export_device(struct xen_pcibk_device *pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400225 int domain, int bus, int slot, int func,
226 int devid)
227{
228 struct pci_dev *dev;
229 int err = 0;
230
231 dev_dbg(&pdev->xdev->dev, "exporting dom %x bus %x slot %x func %x\n",
232 domain, bus, slot, func);
233
234 dev = pcistub_get_pci_dev_by_slot(pdev, domain, bus, slot, func);
235 if (!dev) {
236 err = -EINVAL;
237 xenbus_dev_fatal(pdev->xdev, err,
238 "Couldn't locate PCI device "
Konrad Rzeszutek Wilke4de8662012-01-25 16:00:00 -0500239 "(%04x:%02x:%02x.%d)! "
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400240 "perhaps already in-use?",
241 domain, bus, slot, func);
242 goto out;
243 }
244
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400245 err = xen_pcibk_add_pci_dev(pdev, dev, devid,
246 xen_pcibk_publish_pci_dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400247 if (err)
248 goto out;
249
Konrad Rzeszutek Wilk15390612014-12-03 16:40:29 -0500250 dev_info(&dev->dev, "registering for %d\n", pdev->xdev->otherend_id);
Konrad Rzeszutek Wilk6221a9b2009-12-09 17:43:15 -0500251 if (xen_register_device_domain_owner(dev,
252 pdev->xdev->otherend_id) != 0) {
Konrad Rzeszutek Wilk6c254de2012-01-04 14:16:45 -0500253 dev_err(&dev->dev, "Stealing ownership from dom%d.\n",
254 xen_find_device_domain_owner(dev));
Konrad Rzeszutek Wilk6221a9b2009-12-09 17:43:15 -0500255 xen_unregister_device_domain_owner(dev);
256 xen_register_device_domain_owner(dev, pdev->xdev->otherend_id);
257 }
258
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400259 /* TODO: It'd be nice to export a bridge and have all of its children
260 * get exported with it. This may be best done in xend (which will
261 * have to calculate resource usage anyway) but we probably want to
262 * put something in here to ensure that if a bridge gets given to a
263 * driver domain, that all devices under that bridge are not given
264 * to other driver domains (as he who controls the bridge can disable
265 * it and stop the other devices from working).
266 */
267out:
268 return err;
269}
270
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400271static int xen_pcibk_remove_device(struct xen_pcibk_device *pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400272 int domain, int bus, int slot, int func)
273{
274 int err = 0;
275 struct pci_dev *dev;
276
277 dev_dbg(&pdev->xdev->dev, "removing dom %x bus %x slot %x func %x\n",
278 domain, bus, slot, func);
279
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400280 dev = xen_pcibk_get_pci_dev(pdev, domain, bus, PCI_DEVFN(slot, func));
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400281 if (!dev) {
282 err = -EINVAL;
283 dev_dbg(&pdev->xdev->dev, "Couldn't locate PCI device "
Konrad Rzeszutek Wilke4de8662012-01-25 16:00:00 -0500284 "(%04x:%02x:%02x.%d)! not owned by this domain\n",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400285 domain, bus, slot, func);
286 goto out;
287 }
288
Konrad Rzeszutek Wilk6221a9b2009-12-09 17:43:15 -0500289 dev_dbg(&dev->dev, "unregistering for %d\n", pdev->xdev->otherend_id);
290 xen_unregister_device_domain_owner(dev);
291
Konrad Rzeszutek Wilk8be9df62013-12-03 21:47:37 -0500292 /* N.B. This ends up calling pcistub_put_pci_dev which ends up
293 * doing the FLR. */
Konrad Rzeszutek Wilke8801a72014-12-03 16:40:26 -0500294 xen_pcibk_release_pci_dev(pdev, dev, true /* use the lock. */);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400295
296out:
297 return err;
298}
299
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400300static int xen_pcibk_publish_pci_root(struct xen_pcibk_device *pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400301 unsigned int domain, unsigned int bus)
302{
303 unsigned int d, b;
304 int i, root_num, len, err;
305 char str[64];
306
307 dev_dbg(&pdev->xdev->dev, "Publishing pci roots\n");
308
309 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
310 "root_num", "%d", &root_num);
311 if (err == 0 || err == -ENOENT)
312 root_num = 0;
313 else if (err < 0)
314 goto out;
315
316 /* Verify that we haven't already published this pci root */
317 for (i = 0; i < root_num; i++) {
318 len = snprintf(str, sizeof(str), "root-%d", i);
319 if (unlikely(len >= (sizeof(str) - 1))) {
320 err = -ENOMEM;
321 goto out;
322 }
323
324 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
325 str, "%x:%x", &d, &b);
326 if (err < 0)
327 goto out;
328 if (err != 2) {
329 err = -EINVAL;
330 goto out;
331 }
332
333 if (d == domain && b == bus) {
334 err = 0;
335 goto out;
336 }
337 }
338
339 len = snprintf(str, sizeof(str), "root-%d", root_num);
340 if (unlikely(len >= (sizeof(str) - 1))) {
341 err = -ENOMEM;
342 goto out;
343 }
344
345 dev_dbg(&pdev->xdev->dev, "writing root %d at %04x:%02x\n",
346 root_num, domain, bus);
347
348 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
349 "%04x:%02x", domain, bus);
350 if (err)
351 goto out;
352
353 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename,
354 "root_num", "%d", (root_num + 1));
355
356out:
357 return err;
358}
359
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400360static int xen_pcibk_reconfigure(struct xen_pcibk_device *pdev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400361{
362 int err = 0;
363 int num_devs;
364 int domain, bus, slot, func;
Juergen Gross4e81f1c2016-10-31 14:58:41 +0100365 unsigned int substate;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400366 int i, len;
367 char state_str[64];
368 char dev_str[64];
369
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400370
371 dev_dbg(&pdev->xdev->dev, "Reconfiguring device ...\n");
372
Konrad Rzeszutek Wilkb1766b62011-09-16 14:43:14 -0400373 mutex_lock(&pdev->dev_lock);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400374 /* Make sure we only reconfigure once */
375 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
376 XenbusStateReconfiguring)
377 goto out;
378
379 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
380 &num_devs);
381 if (err != 1) {
382 if (err >= 0)
383 err = -EINVAL;
384 xenbus_dev_fatal(pdev->xdev, err,
385 "Error reading number of devices");
386 goto out;
387 }
388
389 for (i = 0; i < num_devs; i++) {
390 len = snprintf(state_str, sizeof(state_str), "state-%d", i);
391 if (unlikely(len >= (sizeof(state_str) - 1))) {
392 err = -ENOMEM;
393 xenbus_dev_fatal(pdev->xdev, err,
394 "String overflow while reading "
395 "configuration");
396 goto out;
397 }
Juergen Gross4e81f1c2016-10-31 14:58:41 +0100398 substate = xenbus_read_unsigned(pdev->xdev->nodename, state_str,
399 XenbusStateUnknown);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400400
401 switch (substate) {
402 case XenbusStateInitialising:
403 dev_dbg(&pdev->xdev->dev, "Attaching dev-%d ...\n", i);
404
405 len = snprintf(dev_str, sizeof(dev_str), "dev-%d", i);
406 if (unlikely(len >= (sizeof(dev_str) - 1))) {
407 err = -ENOMEM;
408 xenbus_dev_fatal(pdev->xdev, err,
409 "String overflow while "
410 "reading configuration");
411 goto out;
412 }
413 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
414 dev_str, "%x:%x:%x.%x",
415 &domain, &bus, &slot, &func);
416 if (err < 0) {
417 xenbus_dev_fatal(pdev->xdev, err,
418 "Error reading device "
419 "configuration");
420 goto out;
421 }
422 if (err != 4) {
423 err = -EINVAL;
424 xenbus_dev_fatal(pdev->xdev, err,
425 "Error parsing pci device "
426 "configuration");
427 goto out;
428 }
429
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400430 err = xen_pcibk_export_device(pdev, domain, bus, slot,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400431 func, i);
432 if (err)
433 goto out;
434
435 /* Publish pci roots. */
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400436 err = xen_pcibk_publish_pci_roots(pdev,
437 xen_pcibk_publish_pci_root);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400438 if (err) {
439 xenbus_dev_fatal(pdev->xdev, err,
440 "Error while publish PCI root"
441 "buses for frontend");
442 goto out;
443 }
444
445 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename,
446 state_str, "%d",
447 XenbusStateInitialised);
448 if (err) {
449 xenbus_dev_fatal(pdev->xdev, err,
450 "Error switching substate of "
451 "dev-%d\n", i);
452 goto out;
453 }
454 break;
455
456 case XenbusStateClosing:
457 dev_dbg(&pdev->xdev->dev, "Detaching dev-%d ...\n", i);
458
459 len = snprintf(dev_str, sizeof(dev_str), "vdev-%d", i);
460 if (unlikely(len >= (sizeof(dev_str) - 1))) {
461 err = -ENOMEM;
462 xenbus_dev_fatal(pdev->xdev, err,
463 "String overflow while "
464 "reading configuration");
465 goto out;
466 }
467 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
468 dev_str, "%x:%x:%x.%x",
469 &domain, &bus, &slot, &func);
470 if (err < 0) {
471 xenbus_dev_fatal(pdev->xdev, err,
472 "Error reading device "
473 "configuration");
474 goto out;
475 }
476 if (err != 4) {
477 err = -EINVAL;
478 xenbus_dev_fatal(pdev->xdev, err,
479 "Error parsing pci device "
480 "configuration");
481 goto out;
482 }
483
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400484 err = xen_pcibk_remove_device(pdev, domain, bus, slot,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400485 func);
486 if (err)
487 goto out;
488
489 /* TODO: If at some point we implement support for pci
490 * root hot-remove on pcifront side, we'll need to
491 * remove unnecessary xenstore nodes of pci roots here.
492 */
493
494 break;
495
496 default:
497 break;
498 }
499 }
500
501 err = xenbus_switch_state(pdev->xdev, XenbusStateReconfigured);
502 if (err) {
503 xenbus_dev_fatal(pdev->xdev, err,
504 "Error switching to reconfigured state!");
505 goto out;
506 }
507
508out:
Konrad Rzeszutek Wilkb1766b62011-09-16 14:43:14 -0400509 mutex_unlock(&pdev->dev_lock);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400510 return 0;
511}
512
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400513static void xen_pcibk_frontend_changed(struct xenbus_device *xdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400514 enum xenbus_state fe_state)
515{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400516 struct xen_pcibk_device *pdev = dev_get_drvdata(&xdev->dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400517
518 dev_dbg(&xdev->dev, "fe state changed %d\n", fe_state);
519
520 switch (fe_state) {
521 case XenbusStateInitialised:
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400522 xen_pcibk_attach(pdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400523 break;
524
525 case XenbusStateReconfiguring:
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400526 xen_pcibk_reconfigure(pdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400527 break;
528
529 case XenbusStateConnected:
530 /* pcifront switched its state from reconfiguring to connected.
531 * Then switch to connected state.
532 */
533 xenbus_switch_state(xdev, XenbusStateConnected);
534 break;
535
536 case XenbusStateClosing:
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400537 xen_pcibk_disconnect(pdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400538 xenbus_switch_state(xdev, XenbusStateClosing);
539 break;
540
541 case XenbusStateClosed:
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400542 xen_pcibk_disconnect(pdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400543 xenbus_switch_state(xdev, XenbusStateClosed);
544 if (xenbus_dev_is_online(xdev))
545 break;
546 /* fall through if not online */
547 case XenbusStateUnknown:
548 dev_dbg(&xdev->dev, "frontend is gone! unregister device\n");
549 device_unregister(&xdev->dev);
550 break;
551
552 default:
553 break;
554 }
555}
556
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400557static int xen_pcibk_setup_backend(struct xen_pcibk_device *pdev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400558{
559 /* Get configuration from xend (if available now) */
560 int domain, bus, slot, func;
561 int err = 0;
562 int i, num_devs;
563 char dev_str[64];
564 char state_str[64];
565
Konrad Rzeszutek Wilkb1766b62011-09-16 14:43:14 -0400566 mutex_lock(&pdev->dev_lock);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400567 /* It's possible we could get the call to setup twice, so make sure
568 * we're not already connected.
569 */
570 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
571 XenbusStateInitWait)
572 goto out;
573
574 dev_dbg(&pdev->xdev->dev, "getting be setup\n");
575
576 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
577 &num_devs);
578 if (err != 1) {
579 if (err >= 0)
580 err = -EINVAL;
581 xenbus_dev_fatal(pdev->xdev, err,
582 "Error reading number of devices");
583 goto out;
584 }
585
586 for (i = 0; i < num_devs; i++) {
587 int l = snprintf(dev_str, sizeof(dev_str), "dev-%d", i);
588 if (unlikely(l >= (sizeof(dev_str) - 1))) {
589 err = -ENOMEM;
590 xenbus_dev_fatal(pdev->xdev, err,
591 "String overflow while reading "
592 "configuration");
593 goto out;
594 }
595
596 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, dev_str,
597 "%x:%x:%x.%x", &domain, &bus, &slot, &func);
598 if (err < 0) {
599 xenbus_dev_fatal(pdev->xdev, err,
600 "Error reading device configuration");
601 goto out;
602 }
603 if (err != 4) {
604 err = -EINVAL;
605 xenbus_dev_fatal(pdev->xdev, err,
606 "Error parsing pci device "
607 "configuration");
608 goto out;
609 }
610
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400611 err = xen_pcibk_export_device(pdev, domain, bus, slot, func, i);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400612 if (err)
613 goto out;
614
615 /* Switch substate of this device. */
616 l = snprintf(state_str, sizeof(state_str), "state-%d", i);
617 if (unlikely(l >= (sizeof(state_str) - 1))) {
618 err = -ENOMEM;
619 xenbus_dev_fatal(pdev->xdev, err,
620 "String overflow while reading "
621 "configuration");
622 goto out;
623 }
624 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, state_str,
625 "%d", XenbusStateInitialised);
626 if (err) {
627 xenbus_dev_fatal(pdev->xdev, err, "Error switching "
628 "substate of dev-%d\n", i);
629 goto out;
630 }
631 }
632
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400633 err = xen_pcibk_publish_pci_roots(pdev, xen_pcibk_publish_pci_root);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400634 if (err) {
635 xenbus_dev_fatal(pdev->xdev, err,
636 "Error while publish PCI root buses "
637 "for frontend");
638 goto out;
639 }
640
641 err = xenbus_switch_state(pdev->xdev, XenbusStateInitialised);
642 if (err)
643 xenbus_dev_fatal(pdev->xdev, err,
644 "Error switching to initialised state!");
645
646out:
Konrad Rzeszutek Wilkb1766b62011-09-16 14:43:14 -0400647 mutex_unlock(&pdev->dev_lock);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400648 if (!err)
649 /* see if pcifront is already configured (if not, we'll wait) */
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400650 xen_pcibk_attach(pdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400651 return err;
652}
653
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400654static void xen_pcibk_be_watch(struct xenbus_watch *watch,
Juergen Gross5584ea22017-02-09 14:39:57 +0100655 const char *path, const char *token)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400656{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400657 struct xen_pcibk_device *pdev =
658 container_of(watch, struct xen_pcibk_device, be_watch);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400659
660 switch (xenbus_read_driver_state(pdev->xdev->nodename)) {
661 case XenbusStateInitWait:
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400662 xen_pcibk_setup_backend(pdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400663 break;
664
665 default:
666 break;
667 }
668}
669
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400670static int xen_pcibk_xenbus_probe(struct xenbus_device *dev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400671 const struct xenbus_device_id *id)
672{
673 int err = 0;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400674 struct xen_pcibk_device *pdev = alloc_pdev(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400675
676 if (pdev == NULL) {
677 err = -ENOMEM;
678 xenbus_dev_fatal(dev, err,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400679 "Error allocating xen_pcibk_device struct");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400680 goto out;
681 }
682
683 /* wait for xend to configure us */
684 err = xenbus_switch_state(dev, XenbusStateInitWait);
685 if (err)
686 goto out;
687
688 /* watch the backend node for backend configuration information */
689 err = xenbus_watch_path(dev, dev->nodename, &pdev->be_watch,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400690 xen_pcibk_be_watch);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400691 if (err)
692 goto out;
Konrad Rzeszutek Wilk494ef202010-07-23 14:35:47 -0400693
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400694 pdev->be_watching = 1;
695
696 /* We need to force a call to our callback here in case
697 * xend already configured us!
698 */
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400699 xen_pcibk_be_watch(&pdev->be_watch, NULL, 0);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400700
701out:
702 return err;
703}
704
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400705static int xen_pcibk_xenbus_remove(struct xenbus_device *dev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400706{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400707 struct xen_pcibk_device *pdev = dev_get_drvdata(&dev->dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400708
709 if (pdev != NULL)
710 free_pdev(pdev);
711
712 return 0;
713}
714
Jan Beulich73db1442011-12-22 09:08:13 +0000715static const struct xenbus_device_id xen_pcibk_ids[] = {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400716 {"pci"},
717 {""},
718};
719
David Vrabel95afae42014-09-08 17:30:41 +0100720static struct xenbus_driver xen_pcibk_driver = {
721 .name = DRV_NAME,
722 .ids = xen_pcibk_ids,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400723 .probe = xen_pcibk_xenbus_probe,
724 .remove = xen_pcibk_xenbus_remove,
725 .otherend_changed = xen_pcibk_frontend_changed,
David Vrabel95afae42014-09-08 17:30:41 +0100726};
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400727
Jan Beulich402c5e12011-09-21 16:22:11 -0400728const struct xen_pcibk_backend *__read_mostly xen_pcibk_backend;
Konrad Rzeszutek Wilk2ebdc422011-07-11 16:49:41 -0400729
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400730int __init xen_pcibk_xenbus_register(void)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400731{
Konrad Rzeszutek Wilk2ebdc422011-07-11 16:49:41 -0400732 xen_pcibk_backend = &xen_pcibk_vpci_backend;
733 if (passthrough)
734 xen_pcibk_backend = &xen_pcibk_passthrough_backend;
Joe Perches283c0972013-06-28 03:21:41 -0700735 pr_info("backend is %s\n", xen_pcibk_backend->name);
Jan Beulich73db1442011-12-22 09:08:13 +0000736 return xenbus_register_backend(&xen_pcibk_driver);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400737}
738
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400739void __exit xen_pcibk_xenbus_unregister(void)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400740{
Jan Beulich73db1442011-12-22 09:08:13 +0000741 xenbus_unregister_driver(&xen_pcibk_driver);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400742}