blob: 18db31f13a4c91637c08732ca6eeb836e340954a [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
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040016#define DRV_NAME "xen-pciback"
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040017#define INVALID_EVTCHN_IRQ (-1)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040018struct workqueue_struct *xen_pcibk_wq;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040019
Konrad Rzeszutek Wilk2ebdc422011-07-11 16:49:41 -040020static int __read_mostly passthrough;
21module_param(passthrough, bool, S_IRUGO);
22MODULE_PARM_DESC(passthrough,
23 "Option to specify how to export PCI topology to guest:\n"\
24 " 0 - (default) Hide the true PCI topology and makes the frontend\n"\
25 " there is a single PCI bus with only the exported devices on it.\n"\
26 " For example, a device at 03:05.0 will be re-assigned to 00:00.0\n"\
27 " while second device at 02:1a.1 will be re-assigned to 00:01.1.\n"\
28 " 1 - Passthrough provides a real view of the PCI topology to the\n"\
29 " frontend (for example, a device at 06:01.b will still appear at\n"\
30 " 06:01.b to the frontend). This is similar to how Xen 2.0.x\n"\
31 " exposed PCI devices to its driver domains. This may be required\n"\
32 " for drivers which depend on finding their hardward in certain\n"\
33 " bus/slot locations.");
34
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040035static struct xen_pcibk_device *alloc_pdev(struct xenbus_device *xdev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040036{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040037 struct xen_pcibk_device *pdev;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040038
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040039 pdev = kzalloc(sizeof(struct xen_pcibk_device), GFP_KERNEL);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040040 if (pdev == NULL)
41 goto out;
42 dev_dbg(&xdev->dev, "allocated pdev @ 0x%p\n", pdev);
43
44 pdev->xdev = xdev;
45 dev_set_drvdata(&xdev->dev, pdev);
46
47 spin_lock_init(&pdev->dev_lock);
48
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 }
59out:
60 return pdev;
61}
62
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040063static void xen_pcibk_disconnect(struct xen_pcibk_device *pdev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040064{
65 spin_lock(&pdev->dev_lock);
66
67 /* Ensure the guest can't trigger our handler before removing devices */
68 if (pdev->evtchn_irq != INVALID_EVTCHN_IRQ) {
69 unbind_from_irqhandler(pdev->evtchn_irq, pdev);
70 pdev->evtchn_irq = INVALID_EVTCHN_IRQ;
71 }
Konrad Rzeszutek Wilk494ef202010-07-23 14:35:47 -040072 spin_unlock(&pdev->dev_lock);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040073
74 /* If the driver domain started an op, make sure we complete it
75 * before releasing the shared memory */
Konrad Rzeszutek Wilk494ef202010-07-23 14:35:47 -040076
77 /* Note, the workqueue does not use spinlocks at all.*/
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040078 flush_workqueue(xen_pcibk_wq);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040079
Konrad Rzeszutek Wilk494ef202010-07-23 14:35:47 -040080 spin_lock(&pdev->dev_lock);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040081 if (pdev->sh_info != NULL) {
82 xenbus_unmap_ring_vfree(pdev->xdev, pdev->sh_info);
83 pdev->sh_info = NULL;
84 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040085 spin_unlock(&pdev->dev_lock);
Konrad Rzeszutek Wilk494ef202010-07-23 14:35:47 -040086
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040087}
88
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040089static void free_pdev(struct xen_pcibk_device *pdev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040090{
Konrad Rzeszutek Wilk494ef202010-07-23 14:35:47 -040091 if (pdev->be_watching) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040092 unregister_xenbus_watch(&pdev->be_watch);
Konrad Rzeszutek Wilk494ef202010-07-23 14:35:47 -040093 pdev->be_watching = 0;
94 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040095
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040096 xen_pcibk_disconnect(pdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040097
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
116 err = xenbus_map_ring_valloc(pdev->xdev, gnt_ref, &vaddr);
117 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
123 spin_lock(&pdev->dev_lock);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400124 pdev->sh_info = vaddr;
Konrad Rzeszutek Wilk494ef202010-07-23 14:35:47 -0400125 spin_unlock(&pdev->dev_lock);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400126
127 err = bind_interdomain_evtchn_to_irqhandler(
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400128 pdev->xdev->otherend_id, remote_evtchn, xen_pcibk_handle_event,
129 0, DRV_NAME, pdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400130 if (err < 0) {
131 xenbus_dev_fatal(pdev->xdev, err,
132 "Error binding event channel to IRQ");
133 goto out;
134 }
Konrad Rzeszutek Wilk494ef202010-07-23 14:35:47 -0400135
136 spin_lock(&pdev->dev_lock);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400137 pdev->evtchn_irq = err;
Konrad Rzeszutek Wilk494ef202010-07-23 14:35:47 -0400138 spin_unlock(&pdev->dev_lock);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400139 err = 0;
140
141 dev_dbg(&pdev->xdev->dev, "Attached!\n");
142out:
143 return err;
144}
145
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400146static int xen_pcibk_attach(struct xen_pcibk_device *pdev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400147{
148 int err = 0;
149 int gnt_ref, remote_evtchn;
150 char *magic = NULL;
151
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400152
153 /* Make sure we only do this setup once */
154 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
155 XenbusStateInitialised)
156 goto out;
157
158 /* Wait for frontend to state that it has published the configuration */
159 if (xenbus_read_driver_state(pdev->xdev->otherend) !=
160 XenbusStateInitialised)
161 goto out;
162
163 dev_dbg(&pdev->xdev->dev, "Reading frontend config\n");
164
165 err = xenbus_gather(XBT_NIL, pdev->xdev->otherend,
166 "pci-op-ref", "%u", &gnt_ref,
167 "event-channel", "%u", &remote_evtchn,
168 "magic", NULL, &magic, NULL);
169 if (err) {
170 /* If configuration didn't get read correctly, wait longer */
171 xenbus_dev_fatal(pdev->xdev, err,
172 "Error reading configuration from frontend");
173 goto out;
174 }
175
176 if (magic == NULL || strcmp(magic, XEN_PCI_MAGIC) != 0) {
177 xenbus_dev_fatal(pdev->xdev, -EFAULT,
178 "version mismatch (%s/%s) with pcifront - "
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400179 "halting xen_pcibk",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400180 magic, XEN_PCI_MAGIC);
181 goto out;
182 }
183
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400184 err = xen_pcibk_do_attach(pdev, gnt_ref, remote_evtchn);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400185 if (err)
186 goto out;
187
188 dev_dbg(&pdev->xdev->dev, "Connecting...\n");
189
190 err = xenbus_switch_state(pdev->xdev, XenbusStateConnected);
191 if (err)
192 xenbus_dev_fatal(pdev->xdev, err,
193 "Error switching to connected state!");
194
195 dev_dbg(&pdev->xdev->dev, "Connected? %d\n", err);
196out:
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400197
198 kfree(magic);
199
200 return err;
201}
202
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400203static int xen_pcibk_publish_pci_dev(struct xen_pcibk_device *pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400204 unsigned int domain, unsigned int bus,
205 unsigned int devfn, unsigned int devid)
206{
207 int err;
208 int len;
209 char str[64];
210
211 len = snprintf(str, sizeof(str), "vdev-%d", devid);
212 if (unlikely(len >= (sizeof(str) - 1))) {
213 err = -ENOMEM;
214 goto out;
215 }
216
217 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
218 "%04x:%02x:%02x.%02x", domain, bus,
219 PCI_SLOT(devfn), PCI_FUNC(devfn));
220
221out:
222 return err;
223}
224
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400225static int xen_pcibk_export_device(struct xen_pcibk_device *pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400226 int domain, int bus, int slot, int func,
227 int devid)
228{
229 struct pci_dev *dev;
230 int err = 0;
231
232 dev_dbg(&pdev->xdev->dev, "exporting dom %x bus %x slot %x func %x\n",
233 domain, bus, slot, func);
234
235 dev = pcistub_get_pci_dev_by_slot(pdev, domain, bus, slot, func);
236 if (!dev) {
237 err = -EINVAL;
238 xenbus_dev_fatal(pdev->xdev, err,
239 "Couldn't locate PCI device "
240 "(%04x:%02x:%02x.%01x)! "
241 "perhaps already in-use?",
242 domain, bus, slot, func);
243 goto out;
244 }
245
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400246 err = xen_pcibk_add_pci_dev(pdev, dev, devid,
247 xen_pcibk_publish_pci_dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400248 if (err)
249 goto out;
250
Konrad Rzeszutek Wilk6221a9b2009-12-09 17:43:15 -0500251 dev_dbg(&dev->dev, "registering for %d\n", pdev->xdev->otherend_id);
Konrad Rzeszutek Wilkcf177fd2011-09-26 12:22:01 -0400252 dev->dev_flags |= PCI_DEV_FLAGS_ASSIGNED;
Konrad Rzeszutek Wilk6221a9b2009-12-09 17:43:15 -0500253 if (xen_register_device_domain_owner(dev,
254 pdev->xdev->otherend_id) != 0) {
255 dev_err(&dev->dev, "device has been assigned to another " \
256 "domain! Over-writting the ownership, but beware.\n");
257 xen_unregister_device_domain_owner(dev);
258 xen_register_device_domain_owner(dev, pdev->xdev->otherend_id);
259 }
260
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400261 /* TODO: It'd be nice to export a bridge and have all of its children
262 * get exported with it. This may be best done in xend (which will
263 * have to calculate resource usage anyway) but we probably want to
264 * put something in here to ensure that if a bridge gets given to a
265 * driver domain, that all devices under that bridge are not given
266 * to other driver domains (as he who controls the bridge can disable
267 * it and stop the other devices from working).
268 */
269out:
270 return err;
271}
272
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400273static int xen_pcibk_remove_device(struct xen_pcibk_device *pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400274 int domain, int bus, int slot, int func)
275{
276 int err = 0;
277 struct pci_dev *dev;
278
279 dev_dbg(&pdev->xdev->dev, "removing dom %x bus %x slot %x func %x\n",
280 domain, bus, slot, func);
281
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400282 dev = xen_pcibk_get_pci_dev(pdev, domain, bus, PCI_DEVFN(slot, func));
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400283 if (!dev) {
284 err = -EINVAL;
285 dev_dbg(&pdev->xdev->dev, "Couldn't locate PCI device "
286 "(%04x:%02x:%02x.%01x)! not owned by this domain\n",
287 domain, bus, slot, func);
288 goto out;
289 }
290
Konrad Rzeszutek Wilk6221a9b2009-12-09 17:43:15 -0500291 dev_dbg(&dev->dev, "unregistering for %d\n", pdev->xdev->otherend_id);
Konrad Rzeszutek Wilkcf177fd2011-09-26 12:22:01 -0400292 dev->dev_flags &= ~PCI_DEV_FLAGS_ASSIGNED;
Konrad Rzeszutek Wilk6221a9b2009-12-09 17:43:15 -0500293 xen_unregister_device_domain_owner(dev);
294
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400295 xen_pcibk_release_pci_dev(pdev, dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400296
297out:
298 return err;
299}
300
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400301static int xen_pcibk_publish_pci_root(struct xen_pcibk_device *pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400302 unsigned int domain, unsigned int bus)
303{
304 unsigned int d, b;
305 int i, root_num, len, err;
306 char str[64];
307
308 dev_dbg(&pdev->xdev->dev, "Publishing pci roots\n");
309
310 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
311 "root_num", "%d", &root_num);
312 if (err == 0 || err == -ENOENT)
313 root_num = 0;
314 else if (err < 0)
315 goto out;
316
317 /* Verify that we haven't already published this pci root */
318 for (i = 0; i < root_num; i++) {
319 len = snprintf(str, sizeof(str), "root-%d", i);
320 if (unlikely(len >= (sizeof(str) - 1))) {
321 err = -ENOMEM;
322 goto out;
323 }
324
325 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
326 str, "%x:%x", &d, &b);
327 if (err < 0)
328 goto out;
329 if (err != 2) {
330 err = -EINVAL;
331 goto out;
332 }
333
334 if (d == domain && b == bus) {
335 err = 0;
336 goto out;
337 }
338 }
339
340 len = snprintf(str, sizeof(str), "root-%d", root_num);
341 if (unlikely(len >= (sizeof(str) - 1))) {
342 err = -ENOMEM;
343 goto out;
344 }
345
346 dev_dbg(&pdev->xdev->dev, "writing root %d at %04x:%02x\n",
347 root_num, domain, bus);
348
349 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
350 "%04x:%02x", domain, bus);
351 if (err)
352 goto out;
353
354 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename,
355 "root_num", "%d", (root_num + 1));
356
357out:
358 return err;
359}
360
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400361static int xen_pcibk_reconfigure(struct xen_pcibk_device *pdev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400362{
363 int err = 0;
364 int num_devs;
365 int domain, bus, slot, func;
366 int substate;
367 int i, len;
368 char state_str[64];
369 char dev_str[64];
370
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400371
372 dev_dbg(&pdev->xdev->dev, "Reconfiguring device ...\n");
373
374 /* 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 }
398 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, state_str,
399 "%d", &substate);
400 if (err != 1)
401 substate = XenbusStateUnknown;
402
403 switch (substate) {
404 case XenbusStateInitialising:
405 dev_dbg(&pdev->xdev->dev, "Attaching dev-%d ...\n", i);
406
407 len = snprintf(dev_str, sizeof(dev_str), "dev-%d", i);
408 if (unlikely(len >= (sizeof(dev_str) - 1))) {
409 err = -ENOMEM;
410 xenbus_dev_fatal(pdev->xdev, err,
411 "String overflow while "
412 "reading configuration");
413 goto out;
414 }
415 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
416 dev_str, "%x:%x:%x.%x",
417 &domain, &bus, &slot, &func);
418 if (err < 0) {
419 xenbus_dev_fatal(pdev->xdev, err,
420 "Error reading device "
421 "configuration");
422 goto out;
423 }
424 if (err != 4) {
425 err = -EINVAL;
426 xenbus_dev_fatal(pdev->xdev, err,
427 "Error parsing pci device "
428 "configuration");
429 goto out;
430 }
431
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400432 err = xen_pcibk_export_device(pdev, domain, bus, slot,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400433 func, i);
434 if (err)
435 goto out;
436
437 /* Publish pci roots. */
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400438 err = xen_pcibk_publish_pci_roots(pdev,
439 xen_pcibk_publish_pci_root);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400440 if (err) {
441 xenbus_dev_fatal(pdev->xdev, err,
442 "Error while publish PCI root"
443 "buses for frontend");
444 goto out;
445 }
446
447 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename,
448 state_str, "%d",
449 XenbusStateInitialised);
450 if (err) {
451 xenbus_dev_fatal(pdev->xdev, err,
452 "Error switching substate of "
453 "dev-%d\n", i);
454 goto out;
455 }
456 break;
457
458 case XenbusStateClosing:
459 dev_dbg(&pdev->xdev->dev, "Detaching dev-%d ...\n", i);
460
461 len = snprintf(dev_str, sizeof(dev_str), "vdev-%d", i);
462 if (unlikely(len >= (sizeof(dev_str) - 1))) {
463 err = -ENOMEM;
464 xenbus_dev_fatal(pdev->xdev, err,
465 "String overflow while "
466 "reading configuration");
467 goto out;
468 }
469 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
470 dev_str, "%x:%x:%x.%x",
471 &domain, &bus, &slot, &func);
472 if (err < 0) {
473 xenbus_dev_fatal(pdev->xdev, err,
474 "Error reading device "
475 "configuration");
476 goto out;
477 }
478 if (err != 4) {
479 err = -EINVAL;
480 xenbus_dev_fatal(pdev->xdev, err,
481 "Error parsing pci device "
482 "configuration");
483 goto out;
484 }
485
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400486 err = xen_pcibk_remove_device(pdev, domain, bus, slot,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400487 func);
488 if (err)
489 goto out;
490
491 /* TODO: If at some point we implement support for pci
492 * root hot-remove on pcifront side, we'll need to
493 * remove unnecessary xenstore nodes of pci roots here.
494 */
495
496 break;
497
498 default:
499 break;
500 }
501 }
502
503 err = xenbus_switch_state(pdev->xdev, XenbusStateReconfigured);
504 if (err) {
505 xenbus_dev_fatal(pdev->xdev, err,
506 "Error switching to reconfigured state!");
507 goto out;
508 }
509
510out:
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400511 return 0;
512}
513
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400514static void xen_pcibk_frontend_changed(struct xenbus_device *xdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400515 enum xenbus_state fe_state)
516{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400517 struct xen_pcibk_device *pdev = dev_get_drvdata(&xdev->dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400518
519 dev_dbg(&xdev->dev, "fe state changed %d\n", fe_state);
520
521 switch (fe_state) {
522 case XenbusStateInitialised:
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400523 xen_pcibk_attach(pdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400524 break;
525
526 case XenbusStateReconfiguring:
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400527 xen_pcibk_reconfigure(pdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400528 break;
529
530 case XenbusStateConnected:
531 /* pcifront switched its state from reconfiguring to connected.
532 * Then switch to connected state.
533 */
534 xenbus_switch_state(xdev, XenbusStateConnected);
535 break;
536
537 case XenbusStateClosing:
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400538 xen_pcibk_disconnect(pdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400539 xenbus_switch_state(xdev, XenbusStateClosing);
540 break;
541
542 case XenbusStateClosed:
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400543 xen_pcibk_disconnect(pdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400544 xenbus_switch_state(xdev, XenbusStateClosed);
545 if (xenbus_dev_is_online(xdev))
546 break;
547 /* fall through if not online */
548 case XenbusStateUnknown:
549 dev_dbg(&xdev->dev, "frontend is gone! unregister device\n");
550 device_unregister(&xdev->dev);
551 break;
552
553 default:
554 break;
555 }
556}
557
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400558static int xen_pcibk_setup_backend(struct xen_pcibk_device *pdev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400559{
560 /* Get configuration from xend (if available now) */
561 int domain, bus, slot, func;
562 int err = 0;
563 int i, num_devs;
564 char dev_str[64];
565 char state_str[64];
566
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 Wilk30edc142009-10-13 17:22:20 -0400647 if (!err)
648 /* see if pcifront is already configured (if not, we'll wait) */
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400649 xen_pcibk_attach(pdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400650
651 return err;
652}
653
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400654static void xen_pcibk_be_watch(struct xenbus_watch *watch,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400655 const char **vec, unsigned int len)
656{
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
715static const struct xenbus_device_id xenpci_ids[] = {
716 {"pci"},
717 {""},
718};
719
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400720static struct xenbus_driver xenbus_xen_pcibk_driver = {
721 .name = DRV_NAME,
Konrad Rzeszutek Wilk8bfd4e02011-07-19 20:09:43 -0400722 .owner = THIS_MODULE,
723 .ids = xenpci_ids,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400724 .probe = xen_pcibk_xenbus_probe,
725 .remove = xen_pcibk_xenbus_remove,
726 .otherend_changed = xen_pcibk_frontend_changed,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400727};
728
Konrad Rzeszutek Wilk2ebdc422011-07-11 16:49:41 -0400729struct xen_pcibk_backend *xen_pcibk_backend;
730
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400731int __init xen_pcibk_xenbus_register(void)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400732{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400733 xen_pcibk_wq = create_workqueue("xen_pciback_workqueue");
734 if (!xen_pcibk_wq) {
Konrad Rzeszutek Wilk8bfd4e02011-07-19 20:09:43 -0400735 printk(KERN_ERR "%s: create"
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400736 "xen_pciback_workqueue failed\n", __func__);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400737 return -EFAULT;
738 }
Konrad Rzeszutek Wilk2ebdc422011-07-11 16:49:41 -0400739 xen_pcibk_backend = &xen_pcibk_vpci_backend;
740 if (passthrough)
741 xen_pcibk_backend = &xen_pcibk_passthrough_backend;
742 pr_info(DRV_NAME ": backend is %s\n", xen_pcibk_backend->name);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400743 return xenbus_register_backend(&xenbus_xen_pcibk_driver);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400744}
745
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400746void __exit xen_pcibk_xenbus_unregister(void)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400747{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400748 destroy_workqueue(xen_pcibk_wq);
749 xenbus_unregister_driver(&xenbus_xen_pcibk_driver);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400750}