blob: 8e1c44d8ab469d2e28d47a838cb003fafb79949c [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 */
6#include <linux/module.h>
7#include <linux/init.h>
8#include <linux/list.h>
9#include <linux/vmalloc.h>
10#include <linux/workqueue.h>
11#include <xen/xenbus.h>
12#include <xen/events.h>
Konrad Rzeszutek Wilk6221a9b2009-12-09 17:43:15 -050013#include <asm/xen/pci.h>
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040014#include "pciback.h"
15
16#define INVALID_EVTCHN_IRQ (-1)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040017struct workqueue_struct *xen_pcibk_wq;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040018
Konrad Rzeszutek Wilk2ebdc422011-07-11 16:49:41 -040019static int __read_mostly passthrough;
20module_param(passthrough, bool, S_IRUGO);
21MODULE_PARM_DESC(passthrough,
22 "Option to specify how to export PCI topology to guest:\n"\
23 " 0 - (default) Hide the true PCI topology and makes the frontend\n"\
24 " there is a single PCI bus with only the exported devices on it.\n"\
25 " For example, a device at 03:05.0 will be re-assigned to 00:00.0\n"\
26 " while second device at 02:1a.1 will be re-assigned to 00:01.1.\n"\
27 " 1 - Passthrough provides a real view of the PCI topology to the\n"\
28 " frontend (for example, a device at 06:01.b will still appear at\n"\
29 " 06:01.b to the frontend). This is similar to how Xen 2.0.x\n"\
30 " exposed PCI devices to its driver domains. This may be required\n"\
31 " for drivers which depend on finding their hardward in certain\n"\
32 " bus/slot locations.");
33
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040034static struct xen_pcibk_device *alloc_pdev(struct xenbus_device *xdev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040035{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040036 struct xen_pcibk_device *pdev;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040037
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040038 pdev = kzalloc(sizeof(struct xen_pcibk_device), GFP_KERNEL);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040039 if (pdev == NULL)
40 goto out;
41 dev_dbg(&xdev->dev, "allocated pdev @ 0x%p\n", pdev);
42
43 pdev->xdev = xdev;
44 dev_set_drvdata(&xdev->dev, pdev);
45
Konrad Rzeszutek Wilkb1766b62011-09-16 14:43:14 -040046 mutex_init(&pdev->dev_lock);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040047
48 pdev->sh_info = NULL;
49 pdev->evtchn_irq = INVALID_EVTCHN_IRQ;
50 pdev->be_watching = 0;
51
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040052 INIT_WORK(&pdev->op_work, xen_pcibk_do_op);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040053
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040054 if (xen_pcibk_init_devices(pdev)) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040055 kfree(pdev);
56 pdev = NULL;
57 }
58out:
59 return pdev;
60}
61
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040062static void xen_pcibk_disconnect(struct xen_pcibk_device *pdev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040063{
Konrad Rzeszutek Wilkb1766b62011-09-16 14:43:14 -040064 mutex_lock(&pdev->dev_lock);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040065 /* Ensure the guest can't trigger our handler before removing devices */
66 if (pdev->evtchn_irq != INVALID_EVTCHN_IRQ) {
67 unbind_from_irqhandler(pdev->evtchn_irq, pdev);
68 pdev->evtchn_irq = INVALID_EVTCHN_IRQ;
69 }
70
71 /* If the driver domain started an op, make sure we complete it
72 * before releasing the shared memory */
Konrad Rzeszutek Wilk494ef202010-07-23 14:35:47 -040073
74 /* Note, the workqueue does not use spinlocks at all.*/
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040075 flush_workqueue(xen_pcibk_wq);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040076
77 if (pdev->sh_info != NULL) {
78 xenbus_unmap_ring_vfree(pdev->xdev, pdev->sh_info);
79 pdev->sh_info = NULL;
80 }
Konrad Rzeszutek Wilkb1766b62011-09-16 14:43:14 -040081 mutex_unlock(&pdev->dev_lock);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040082}
83
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040084static void free_pdev(struct xen_pcibk_device *pdev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040085{
Konrad Rzeszutek Wilk494ef202010-07-23 14:35:47 -040086 if (pdev->be_watching) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040087 unregister_xenbus_watch(&pdev->be_watch);
Konrad Rzeszutek Wilk494ef202010-07-23 14:35:47 -040088 pdev->be_watching = 0;
89 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040090
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040091 xen_pcibk_disconnect(pdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040092
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040093 xen_pcibk_release_devices(pdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040094
95 dev_set_drvdata(&pdev->xdev->dev, NULL);
96 pdev->xdev = NULL;
97
98 kfree(pdev);
99}
100
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400101static int xen_pcibk_do_attach(struct xen_pcibk_device *pdev, int gnt_ref,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400102 int remote_evtchn)
103{
104 int err = 0;
105 void *vaddr;
106
107 dev_dbg(&pdev->xdev->dev,
108 "Attaching to frontend resources - gnt_ref=%d evtchn=%d\n",
109 gnt_ref, remote_evtchn);
110
111 err = xenbus_map_ring_valloc(pdev->xdev, gnt_ref, &vaddr);
112 if (err < 0) {
113 xenbus_dev_fatal(pdev->xdev, err,
114 "Error mapping other domain page in ours.");
115 goto out;
116 }
Konrad Rzeszutek Wilk494ef202010-07-23 14:35:47 -0400117
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400118 pdev->sh_info = vaddr;
119
120 err = bind_interdomain_evtchn_to_irqhandler(
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400121 pdev->xdev->otherend_id, remote_evtchn, xen_pcibk_handle_event,
122 0, DRV_NAME, pdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400123 if (err < 0) {
124 xenbus_dev_fatal(pdev->xdev, err,
125 "Error binding event channel to IRQ");
126 goto out;
127 }
128 pdev->evtchn_irq = err;
129 err = 0;
130
131 dev_dbg(&pdev->xdev->dev, "Attached!\n");
132out:
133 return err;
134}
135
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400136static int xen_pcibk_attach(struct xen_pcibk_device *pdev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400137{
138 int err = 0;
139 int gnt_ref, remote_evtchn;
140 char *magic = NULL;
141
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400142
Konrad Rzeszutek Wilkb1766b62011-09-16 14:43:14 -0400143 mutex_lock(&pdev->dev_lock);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400144 /* Make sure we only do this setup once */
145 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
146 XenbusStateInitialised)
147 goto out;
148
149 /* Wait for frontend to state that it has published the configuration */
150 if (xenbus_read_driver_state(pdev->xdev->otherend) !=
151 XenbusStateInitialised)
152 goto out;
153
154 dev_dbg(&pdev->xdev->dev, "Reading frontend config\n");
155
156 err = xenbus_gather(XBT_NIL, pdev->xdev->otherend,
157 "pci-op-ref", "%u", &gnt_ref,
158 "event-channel", "%u", &remote_evtchn,
159 "magic", NULL, &magic, NULL);
160 if (err) {
161 /* If configuration didn't get read correctly, wait longer */
162 xenbus_dev_fatal(pdev->xdev, err,
163 "Error reading configuration from frontend");
164 goto out;
165 }
166
167 if (magic == NULL || strcmp(magic, XEN_PCI_MAGIC) != 0) {
168 xenbus_dev_fatal(pdev->xdev, -EFAULT,
169 "version mismatch (%s/%s) with pcifront - "
Jan Beulich402c5e12011-09-21 16:22:11 -0400170 "halting " DRV_NAME,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400171 magic, XEN_PCI_MAGIC);
172 goto out;
173 }
174
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400175 err = xen_pcibk_do_attach(pdev, gnt_ref, remote_evtchn);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400176 if (err)
177 goto out;
178
179 dev_dbg(&pdev->xdev->dev, "Connecting...\n");
180
181 err = xenbus_switch_state(pdev->xdev, XenbusStateConnected);
182 if (err)
183 xenbus_dev_fatal(pdev->xdev, err,
184 "Error switching to connected state!");
185
186 dev_dbg(&pdev->xdev->dev, "Connected? %d\n", err);
187out:
Konrad Rzeszutek Wilkb1766b62011-09-16 14:43:14 -0400188 mutex_unlock(&pdev->dev_lock);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400189
190 kfree(magic);
191
192 return err;
193}
194
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400195static int xen_pcibk_publish_pci_dev(struct xen_pcibk_device *pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400196 unsigned int domain, unsigned int bus,
197 unsigned int devfn, unsigned int devid)
198{
199 int err;
200 int len;
201 char str[64];
202
203 len = snprintf(str, sizeof(str), "vdev-%d", devid);
204 if (unlikely(len >= (sizeof(str) - 1))) {
205 err = -ENOMEM;
206 goto out;
207 }
208
209 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
210 "%04x:%02x:%02x.%02x", domain, bus,
211 PCI_SLOT(devfn), PCI_FUNC(devfn));
212
213out:
214 return err;
215}
216
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400217static int xen_pcibk_export_device(struct xen_pcibk_device *pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400218 int domain, int bus, int slot, int func,
219 int devid)
220{
221 struct pci_dev *dev;
222 int err = 0;
223
224 dev_dbg(&pdev->xdev->dev, "exporting dom %x bus %x slot %x func %x\n",
225 domain, bus, slot, func);
226
227 dev = pcistub_get_pci_dev_by_slot(pdev, domain, bus, slot, func);
228 if (!dev) {
229 err = -EINVAL;
230 xenbus_dev_fatal(pdev->xdev, err,
231 "Couldn't locate PCI device "
232 "(%04x:%02x:%02x.%01x)! "
233 "perhaps already in-use?",
234 domain, bus, slot, func);
235 goto out;
236 }
237
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400238 err = xen_pcibk_add_pci_dev(pdev, dev, devid,
239 xen_pcibk_publish_pci_dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400240 if (err)
241 goto out;
242
Konrad Rzeszutek Wilk6221a9b2009-12-09 17:43:15 -0500243 dev_dbg(&dev->dev, "registering for %d\n", pdev->xdev->otherend_id);
244 if (xen_register_device_domain_owner(dev,
245 pdev->xdev->otherend_id) != 0) {
Konrad Rzeszutek Wilk6c254de2012-01-04 14:16:45 -0500246 dev_err(&dev->dev, "Stealing ownership from dom%d.\n",
247 xen_find_device_domain_owner(dev));
Konrad Rzeszutek Wilk6221a9b2009-12-09 17:43:15 -0500248 xen_unregister_device_domain_owner(dev);
249 xen_register_device_domain_owner(dev, pdev->xdev->otherend_id);
250 }
251
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400252 /* TODO: It'd be nice to export a bridge and have all of its children
253 * get exported with it. This may be best done in xend (which will
254 * have to calculate resource usage anyway) but we probably want to
255 * put something in here to ensure that if a bridge gets given to a
256 * driver domain, that all devices under that bridge are not given
257 * to other driver domains (as he who controls the bridge can disable
258 * it and stop the other devices from working).
259 */
260out:
261 return err;
262}
263
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400264static int xen_pcibk_remove_device(struct xen_pcibk_device *pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400265 int domain, int bus, int slot, int func)
266{
267 int err = 0;
268 struct pci_dev *dev;
269
270 dev_dbg(&pdev->xdev->dev, "removing dom %x bus %x slot %x func %x\n",
271 domain, bus, slot, func);
272
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400273 dev = xen_pcibk_get_pci_dev(pdev, domain, bus, PCI_DEVFN(slot, func));
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400274 if (!dev) {
275 err = -EINVAL;
276 dev_dbg(&pdev->xdev->dev, "Couldn't locate PCI device "
277 "(%04x:%02x:%02x.%01x)! not owned by this domain\n",
278 domain, bus, slot, func);
279 goto out;
280 }
281
Konrad Rzeszutek Wilk6221a9b2009-12-09 17:43:15 -0500282 dev_dbg(&dev->dev, "unregistering for %d\n", pdev->xdev->otherend_id);
283 xen_unregister_device_domain_owner(dev);
284
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400285 xen_pcibk_release_pci_dev(pdev, dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400286
287out:
288 return err;
289}
290
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400291static int xen_pcibk_publish_pci_root(struct xen_pcibk_device *pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400292 unsigned int domain, unsigned int bus)
293{
294 unsigned int d, b;
295 int i, root_num, len, err;
296 char str[64];
297
298 dev_dbg(&pdev->xdev->dev, "Publishing pci roots\n");
299
300 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
301 "root_num", "%d", &root_num);
302 if (err == 0 || err == -ENOENT)
303 root_num = 0;
304 else if (err < 0)
305 goto out;
306
307 /* Verify that we haven't already published this pci root */
308 for (i = 0; i < root_num; i++) {
309 len = snprintf(str, sizeof(str), "root-%d", i);
310 if (unlikely(len >= (sizeof(str) - 1))) {
311 err = -ENOMEM;
312 goto out;
313 }
314
315 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
316 str, "%x:%x", &d, &b);
317 if (err < 0)
318 goto out;
319 if (err != 2) {
320 err = -EINVAL;
321 goto out;
322 }
323
324 if (d == domain && b == bus) {
325 err = 0;
326 goto out;
327 }
328 }
329
330 len = snprintf(str, sizeof(str), "root-%d", root_num);
331 if (unlikely(len >= (sizeof(str) - 1))) {
332 err = -ENOMEM;
333 goto out;
334 }
335
336 dev_dbg(&pdev->xdev->dev, "writing root %d at %04x:%02x\n",
337 root_num, domain, bus);
338
339 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
340 "%04x:%02x", domain, bus);
341 if (err)
342 goto out;
343
344 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename,
345 "root_num", "%d", (root_num + 1));
346
347out:
348 return err;
349}
350
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400351static int xen_pcibk_reconfigure(struct xen_pcibk_device *pdev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400352{
353 int err = 0;
354 int num_devs;
355 int domain, bus, slot, func;
356 int substate;
357 int i, len;
358 char state_str[64];
359 char dev_str[64];
360
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400361
362 dev_dbg(&pdev->xdev->dev, "Reconfiguring device ...\n");
363
Konrad Rzeszutek Wilkb1766b62011-09-16 14:43:14 -0400364 mutex_lock(&pdev->dev_lock);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400365 /* Make sure we only reconfigure once */
366 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
367 XenbusStateReconfiguring)
368 goto out;
369
370 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
371 &num_devs);
372 if (err != 1) {
373 if (err >= 0)
374 err = -EINVAL;
375 xenbus_dev_fatal(pdev->xdev, err,
376 "Error reading number of devices");
377 goto out;
378 }
379
380 for (i = 0; i < num_devs; i++) {
381 len = snprintf(state_str, sizeof(state_str), "state-%d", i);
382 if (unlikely(len >= (sizeof(state_str) - 1))) {
383 err = -ENOMEM;
384 xenbus_dev_fatal(pdev->xdev, err,
385 "String overflow while reading "
386 "configuration");
387 goto out;
388 }
389 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, state_str,
390 "%d", &substate);
391 if (err != 1)
392 substate = XenbusStateUnknown;
393
394 switch (substate) {
395 case XenbusStateInitialising:
396 dev_dbg(&pdev->xdev->dev, "Attaching dev-%d ...\n", i);
397
398 len = snprintf(dev_str, sizeof(dev_str), "dev-%d", i);
399 if (unlikely(len >= (sizeof(dev_str) - 1))) {
400 err = -ENOMEM;
401 xenbus_dev_fatal(pdev->xdev, err,
402 "String overflow while "
403 "reading configuration");
404 goto out;
405 }
406 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
407 dev_str, "%x:%x:%x.%x",
408 &domain, &bus, &slot, &func);
409 if (err < 0) {
410 xenbus_dev_fatal(pdev->xdev, err,
411 "Error reading device "
412 "configuration");
413 goto out;
414 }
415 if (err != 4) {
416 err = -EINVAL;
417 xenbus_dev_fatal(pdev->xdev, err,
418 "Error parsing pci device "
419 "configuration");
420 goto out;
421 }
422
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400423 err = xen_pcibk_export_device(pdev, domain, bus, slot,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400424 func, i);
425 if (err)
426 goto out;
427
428 /* Publish pci roots. */
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400429 err = xen_pcibk_publish_pci_roots(pdev,
430 xen_pcibk_publish_pci_root);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400431 if (err) {
432 xenbus_dev_fatal(pdev->xdev, err,
433 "Error while publish PCI root"
434 "buses for frontend");
435 goto out;
436 }
437
438 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename,
439 state_str, "%d",
440 XenbusStateInitialised);
441 if (err) {
442 xenbus_dev_fatal(pdev->xdev, err,
443 "Error switching substate of "
444 "dev-%d\n", i);
445 goto out;
446 }
447 break;
448
449 case XenbusStateClosing:
450 dev_dbg(&pdev->xdev->dev, "Detaching dev-%d ...\n", i);
451
452 len = snprintf(dev_str, sizeof(dev_str), "vdev-%d", i);
453 if (unlikely(len >= (sizeof(dev_str) - 1))) {
454 err = -ENOMEM;
455 xenbus_dev_fatal(pdev->xdev, err,
456 "String overflow while "
457 "reading configuration");
458 goto out;
459 }
460 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
461 dev_str, "%x:%x:%x.%x",
462 &domain, &bus, &slot, &func);
463 if (err < 0) {
464 xenbus_dev_fatal(pdev->xdev, err,
465 "Error reading device "
466 "configuration");
467 goto out;
468 }
469 if (err != 4) {
470 err = -EINVAL;
471 xenbus_dev_fatal(pdev->xdev, err,
472 "Error parsing pci device "
473 "configuration");
474 goto out;
475 }
476
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400477 err = xen_pcibk_remove_device(pdev, domain, bus, slot,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400478 func);
479 if (err)
480 goto out;
481
482 /* TODO: If at some point we implement support for pci
483 * root hot-remove on pcifront side, we'll need to
484 * remove unnecessary xenstore nodes of pci roots here.
485 */
486
487 break;
488
489 default:
490 break;
491 }
492 }
493
494 err = xenbus_switch_state(pdev->xdev, XenbusStateReconfigured);
495 if (err) {
496 xenbus_dev_fatal(pdev->xdev, err,
497 "Error switching to reconfigured state!");
498 goto out;
499 }
500
501out:
Konrad Rzeszutek Wilkb1766b62011-09-16 14:43:14 -0400502 mutex_unlock(&pdev->dev_lock);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400503 return 0;
504}
505
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400506static void xen_pcibk_frontend_changed(struct xenbus_device *xdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400507 enum xenbus_state fe_state)
508{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400509 struct xen_pcibk_device *pdev = dev_get_drvdata(&xdev->dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400510
511 dev_dbg(&xdev->dev, "fe state changed %d\n", fe_state);
512
513 switch (fe_state) {
514 case XenbusStateInitialised:
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400515 xen_pcibk_attach(pdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400516 break;
517
518 case XenbusStateReconfiguring:
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400519 xen_pcibk_reconfigure(pdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400520 break;
521
522 case XenbusStateConnected:
523 /* pcifront switched its state from reconfiguring to connected.
524 * Then switch to connected state.
525 */
526 xenbus_switch_state(xdev, XenbusStateConnected);
527 break;
528
529 case XenbusStateClosing:
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400530 xen_pcibk_disconnect(pdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400531 xenbus_switch_state(xdev, XenbusStateClosing);
532 break;
533
534 case XenbusStateClosed:
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400535 xen_pcibk_disconnect(pdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400536 xenbus_switch_state(xdev, XenbusStateClosed);
537 if (xenbus_dev_is_online(xdev))
538 break;
539 /* fall through if not online */
540 case XenbusStateUnknown:
541 dev_dbg(&xdev->dev, "frontend is gone! unregister device\n");
542 device_unregister(&xdev->dev);
543 break;
544
545 default:
546 break;
547 }
548}
549
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400550static int xen_pcibk_setup_backend(struct xen_pcibk_device *pdev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400551{
552 /* Get configuration from xend (if available now) */
553 int domain, bus, slot, func;
554 int err = 0;
555 int i, num_devs;
556 char dev_str[64];
557 char state_str[64];
558
Konrad Rzeszutek Wilkb1766b62011-09-16 14:43:14 -0400559 mutex_lock(&pdev->dev_lock);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400560 /* It's possible we could get the call to setup twice, so make sure
561 * we're not already connected.
562 */
563 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
564 XenbusStateInitWait)
565 goto out;
566
567 dev_dbg(&pdev->xdev->dev, "getting be setup\n");
568
569 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
570 &num_devs);
571 if (err != 1) {
572 if (err >= 0)
573 err = -EINVAL;
574 xenbus_dev_fatal(pdev->xdev, err,
575 "Error reading number of devices");
576 goto out;
577 }
578
579 for (i = 0; i < num_devs; i++) {
580 int l = snprintf(dev_str, sizeof(dev_str), "dev-%d", i);
581 if (unlikely(l >= (sizeof(dev_str) - 1))) {
582 err = -ENOMEM;
583 xenbus_dev_fatal(pdev->xdev, err,
584 "String overflow while reading "
585 "configuration");
586 goto out;
587 }
588
589 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, dev_str,
590 "%x:%x:%x.%x", &domain, &bus, &slot, &func);
591 if (err < 0) {
592 xenbus_dev_fatal(pdev->xdev, err,
593 "Error reading device configuration");
594 goto out;
595 }
596 if (err != 4) {
597 err = -EINVAL;
598 xenbus_dev_fatal(pdev->xdev, err,
599 "Error parsing pci device "
600 "configuration");
601 goto out;
602 }
603
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400604 err = xen_pcibk_export_device(pdev, domain, bus, slot, func, i);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400605 if (err)
606 goto out;
607
608 /* Switch substate of this device. */
609 l = snprintf(state_str, sizeof(state_str), "state-%d", i);
610 if (unlikely(l >= (sizeof(state_str) - 1))) {
611 err = -ENOMEM;
612 xenbus_dev_fatal(pdev->xdev, err,
613 "String overflow while reading "
614 "configuration");
615 goto out;
616 }
617 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, state_str,
618 "%d", XenbusStateInitialised);
619 if (err) {
620 xenbus_dev_fatal(pdev->xdev, err, "Error switching "
621 "substate of dev-%d\n", i);
622 goto out;
623 }
624 }
625
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400626 err = xen_pcibk_publish_pci_roots(pdev, xen_pcibk_publish_pci_root);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400627 if (err) {
628 xenbus_dev_fatal(pdev->xdev, err,
629 "Error while publish PCI root buses "
630 "for frontend");
631 goto out;
632 }
633
634 err = xenbus_switch_state(pdev->xdev, XenbusStateInitialised);
635 if (err)
636 xenbus_dev_fatal(pdev->xdev, err,
637 "Error switching to initialised state!");
638
639out:
Konrad Rzeszutek Wilkb1766b62011-09-16 14:43:14 -0400640 mutex_unlock(&pdev->dev_lock);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400641 if (!err)
642 /* see if pcifront is already configured (if not, we'll wait) */
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400643 xen_pcibk_attach(pdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400644 return err;
645}
646
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400647static void xen_pcibk_be_watch(struct xenbus_watch *watch,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400648 const char **vec, unsigned int len)
649{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400650 struct xen_pcibk_device *pdev =
651 container_of(watch, struct xen_pcibk_device, be_watch);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400652
653 switch (xenbus_read_driver_state(pdev->xdev->nodename)) {
654 case XenbusStateInitWait:
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400655 xen_pcibk_setup_backend(pdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400656 break;
657
658 default:
659 break;
660 }
661}
662
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400663static int xen_pcibk_xenbus_probe(struct xenbus_device *dev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400664 const struct xenbus_device_id *id)
665{
666 int err = 0;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400667 struct xen_pcibk_device *pdev = alloc_pdev(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400668
669 if (pdev == NULL) {
670 err = -ENOMEM;
671 xenbus_dev_fatal(dev, err,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400672 "Error allocating xen_pcibk_device struct");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400673 goto out;
674 }
675
676 /* wait for xend to configure us */
677 err = xenbus_switch_state(dev, XenbusStateInitWait);
678 if (err)
679 goto out;
680
681 /* watch the backend node for backend configuration information */
682 err = xenbus_watch_path(dev, dev->nodename, &pdev->be_watch,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400683 xen_pcibk_be_watch);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400684 if (err)
685 goto out;
Konrad Rzeszutek Wilk494ef202010-07-23 14:35:47 -0400686
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400687 pdev->be_watching = 1;
688
689 /* We need to force a call to our callback here in case
690 * xend already configured us!
691 */
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400692 xen_pcibk_be_watch(&pdev->be_watch, NULL, 0);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400693
694out:
695 return err;
696}
697
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400698static int xen_pcibk_xenbus_remove(struct xenbus_device *dev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400699{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400700 struct xen_pcibk_device *pdev = dev_get_drvdata(&dev->dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400701
702 if (pdev != NULL)
703 free_pdev(pdev);
704
705 return 0;
706}
707
Jan Beulich73db1442011-12-22 09:08:13 +0000708static const struct xenbus_device_id xen_pcibk_ids[] = {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400709 {"pci"},
710 {""},
711};
712
Jan Beulich73db1442011-12-22 09:08:13 +0000713static DEFINE_XENBUS_DRIVER(xen_pcibk, DRV_NAME,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400714 .probe = xen_pcibk_xenbus_probe,
715 .remove = xen_pcibk_xenbus_remove,
716 .otherend_changed = xen_pcibk_frontend_changed,
Jan Beulich73db1442011-12-22 09:08:13 +0000717);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400718
Jan Beulich402c5e12011-09-21 16:22:11 -0400719const struct xen_pcibk_backend *__read_mostly xen_pcibk_backend;
Konrad Rzeszutek Wilk2ebdc422011-07-11 16:49:41 -0400720
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400721int __init xen_pcibk_xenbus_register(void)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400722{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400723 xen_pcibk_wq = create_workqueue("xen_pciback_workqueue");
724 if (!xen_pcibk_wq) {
Konrad Rzeszutek Wilk8bfd4e02011-07-19 20:09:43 -0400725 printk(KERN_ERR "%s: create"
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400726 "xen_pciback_workqueue failed\n", __func__);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400727 return -EFAULT;
728 }
Konrad Rzeszutek Wilk2ebdc422011-07-11 16:49:41 -0400729 xen_pcibk_backend = &xen_pcibk_vpci_backend;
730 if (passthrough)
731 xen_pcibk_backend = &xen_pcibk_passthrough_backend;
732 pr_info(DRV_NAME ": backend is %s\n", xen_pcibk_backend->name);
Jan Beulich73db1442011-12-22 09:08:13 +0000733 return xenbus_register_backend(&xen_pcibk_driver);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400734}
735
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400736void __exit xen_pcibk_xenbus_unregister(void)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400737{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400738 destroy_workqueue(xen_pcibk_wq);
Jan Beulich73db1442011-12-22 09:08:13 +0000739 xenbus_unregister_driver(&xen_pcibk_driver);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400740}