blob: 206c4ce030bc98c9b2e5f66caca50ed27c459848 [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 <linux/workqueue.h>
15#include "pciback.h"
16
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040017#define DRV_NAME "xen-pciback"
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040018#define INVALID_EVTCHN_IRQ (-1)
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040019struct workqueue_struct *xen_pcibk_wq;
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040020
Konrad Rzeszutek Wilk2ebdc422011-07-11 16:49:41 -040021static int __read_mostly passthrough;
22module_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;
46 dev_set_drvdata(&xdev->dev, pdev);
47
48 spin_lock_init(&pdev->dev_lock);
49
50 pdev->sh_info = NULL;
51 pdev->evtchn_irq = INVALID_EVTCHN_IRQ;
52 pdev->be_watching = 0;
53
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040054 INIT_WORK(&pdev->op_work, xen_pcibk_do_op);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040055
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040056 if (xen_pcibk_init_devices(pdev)) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040057 kfree(pdev);
58 pdev = NULL;
59 }
60out:
61 return pdev;
62}
63
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040064static void xen_pcibk_disconnect(struct xen_pcibk_device *pdev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040065{
66 spin_lock(&pdev->dev_lock);
67
68 /* Ensure the guest can't trigger our handler before removing devices */
69 if (pdev->evtchn_irq != INVALID_EVTCHN_IRQ) {
70 unbind_from_irqhandler(pdev->evtchn_irq, pdev);
71 pdev->evtchn_irq = INVALID_EVTCHN_IRQ;
72 }
Konrad Rzeszutek Wilk494ef202010-07-23 14:35:47 -040073 spin_unlock(&pdev->dev_lock);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040074
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
78 /* Note, the workqueue does not use spinlocks at all.*/
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040079 flush_workqueue(xen_pcibk_wq);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040080
Konrad Rzeszutek Wilk494ef202010-07-23 14:35:47 -040081 spin_lock(&pdev->dev_lock);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040082 if (pdev->sh_info != NULL) {
83 xenbus_unmap_ring_vfree(pdev->xdev, pdev->sh_info);
84 pdev->sh_info = NULL;
85 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040086 spin_unlock(&pdev->dev_lock);
Konrad Rzeszutek Wilk494ef202010-07-23 14:35:47 -040087
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040088}
89
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040090static void free_pdev(struct xen_pcibk_device *pdev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040091{
Konrad Rzeszutek Wilk494ef202010-07-23 14:35:47 -040092 if (pdev->be_watching) {
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040093 unregister_xenbus_watch(&pdev->be_watch);
Konrad Rzeszutek Wilk494ef202010-07-23 14:35:47 -040094 pdev->be_watching = 0;
95 }
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040096
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040097 xen_pcibk_disconnect(pdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -040098
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -040099 xen_pcibk_release_devices(pdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400100
101 dev_set_drvdata(&pdev->xdev->dev, NULL);
102 pdev->xdev = NULL;
103
104 kfree(pdev);
105}
106
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400107static int xen_pcibk_do_attach(struct xen_pcibk_device *pdev, int gnt_ref,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400108 int remote_evtchn)
109{
110 int err = 0;
111 void *vaddr;
112
113 dev_dbg(&pdev->xdev->dev,
114 "Attaching to frontend resources - gnt_ref=%d evtchn=%d\n",
115 gnt_ref, remote_evtchn);
116
117 err = xenbus_map_ring_valloc(pdev->xdev, gnt_ref, &vaddr);
118 if (err < 0) {
119 xenbus_dev_fatal(pdev->xdev, err,
120 "Error mapping other domain page in ours.");
121 goto out;
122 }
Konrad Rzeszutek Wilk494ef202010-07-23 14:35:47 -0400123
124 spin_lock(&pdev->dev_lock);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400125 pdev->sh_info = vaddr;
Konrad Rzeszutek Wilk494ef202010-07-23 14:35:47 -0400126 spin_unlock(&pdev->dev_lock);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400127
128 err = bind_interdomain_evtchn_to_irqhandler(
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400129 pdev->xdev->otherend_id, remote_evtchn, xen_pcibk_handle_event,
130 0, DRV_NAME, pdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400131 if (err < 0) {
132 xenbus_dev_fatal(pdev->xdev, err,
133 "Error binding event channel to IRQ");
134 goto out;
135 }
Konrad Rzeszutek Wilk494ef202010-07-23 14:35:47 -0400136
137 spin_lock(&pdev->dev_lock);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400138 pdev->evtchn_irq = err;
Konrad Rzeszutek Wilk494ef202010-07-23 14:35:47 -0400139 spin_unlock(&pdev->dev_lock);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400140 err = 0;
141
142 dev_dbg(&pdev->xdev->dev, "Attached!\n");
143out:
144 return err;
145}
146
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400147static int xen_pcibk_attach(struct xen_pcibk_device *pdev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400148{
149 int err = 0;
150 int gnt_ref, remote_evtchn;
151 char *magic = NULL;
152
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400153
154 /* Make sure we only do this setup once */
155 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
156 XenbusStateInitialised)
157 goto out;
158
159 /* Wait for frontend to state that it has published the configuration */
160 if (xenbus_read_driver_state(pdev->xdev->otherend) !=
161 XenbusStateInitialised)
162 goto out;
163
164 dev_dbg(&pdev->xdev->dev, "Reading frontend config\n");
165
166 err = xenbus_gather(XBT_NIL, pdev->xdev->otherend,
167 "pci-op-ref", "%u", &gnt_ref,
168 "event-channel", "%u", &remote_evtchn,
169 "magic", NULL, &magic, NULL);
170 if (err) {
171 /* If configuration didn't get read correctly, wait longer */
172 xenbus_dev_fatal(pdev->xdev, err,
173 "Error reading configuration from frontend");
174 goto out;
175 }
176
177 if (magic == NULL || strcmp(magic, XEN_PCI_MAGIC) != 0) {
178 xenbus_dev_fatal(pdev->xdev, -EFAULT,
179 "version mismatch (%s/%s) with pcifront - "
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400180 "halting xen_pcibk",
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400181 magic, XEN_PCI_MAGIC);
182 goto out;
183 }
184
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400185 err = xen_pcibk_do_attach(pdev, gnt_ref, remote_evtchn);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400186 if (err)
187 goto out;
188
189 dev_dbg(&pdev->xdev->dev, "Connecting...\n");
190
191 err = xenbus_switch_state(pdev->xdev, XenbusStateConnected);
192 if (err)
193 xenbus_dev_fatal(pdev->xdev, err,
194 "Error switching to connected state!");
195
196 dev_dbg(&pdev->xdev->dev, "Connected? %d\n", err);
197out:
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400198
199 kfree(magic);
200
201 return err;
202}
203
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400204static int xen_pcibk_publish_pci_dev(struct xen_pcibk_device *pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400205 unsigned int domain, unsigned int bus,
206 unsigned int devfn, unsigned int devid)
207{
208 int err;
209 int len;
210 char str[64];
211
212 len = snprintf(str, sizeof(str), "vdev-%d", devid);
213 if (unlikely(len >= (sizeof(str) - 1))) {
214 err = -ENOMEM;
215 goto out;
216 }
217
218 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
219 "%04x:%02x:%02x.%02x", domain, bus,
220 PCI_SLOT(devfn), PCI_FUNC(devfn));
221
222out:
223 return err;
224}
225
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400226static int xen_pcibk_export_device(struct xen_pcibk_device *pdev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400227 int domain, int bus, int slot, int func,
228 int devid)
229{
230 struct pci_dev *dev;
231 int err = 0;
232
233 dev_dbg(&pdev->xdev->dev, "exporting dom %x bus %x slot %x func %x\n",
234 domain, bus, slot, func);
235
236 dev = pcistub_get_pci_dev_by_slot(pdev, domain, bus, slot, func);
237 if (!dev) {
238 err = -EINVAL;
239 xenbus_dev_fatal(pdev->xdev, err,
240 "Couldn't locate PCI device "
241 "(%04x:%02x:%02x.%01x)! "
242 "perhaps already in-use?",
243 domain, bus, slot, func);
244 goto out;
245 }
246
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400247 err = xen_pcibk_add_pci_dev(pdev, dev, devid,
248 xen_pcibk_publish_pci_dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400249 if (err)
250 goto out;
251
Konrad Rzeszutek Wilk6221a9b2009-12-09 17:43:15 -0500252 dev_dbg(&dev->dev, "registering for %d\n", pdev->xdev->otherend_id);
253 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);
292 xen_unregister_device_domain_owner(dev);
293
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400294 xen_pcibk_release_pci_dev(pdev, dev);
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;
365 int substate;
366 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
373 /* Make sure we only reconfigure once */
374 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
375 XenbusStateReconfiguring)
376 goto out;
377
378 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
379 &num_devs);
380 if (err != 1) {
381 if (err >= 0)
382 err = -EINVAL;
383 xenbus_dev_fatal(pdev->xdev, err,
384 "Error reading number of devices");
385 goto out;
386 }
387
388 for (i = 0; i < num_devs; i++) {
389 len = snprintf(state_str, sizeof(state_str), "state-%d", i);
390 if (unlikely(len >= (sizeof(state_str) - 1))) {
391 err = -ENOMEM;
392 xenbus_dev_fatal(pdev->xdev, err,
393 "String overflow while reading "
394 "configuration");
395 goto out;
396 }
397 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, state_str,
398 "%d", &substate);
399 if (err != 1)
400 substate = XenbusStateUnknown;
401
402 switch (substate) {
403 case XenbusStateInitialising:
404 dev_dbg(&pdev->xdev->dev, "Attaching dev-%d ...\n", i);
405
406 len = snprintf(dev_str, sizeof(dev_str), "dev-%d", i);
407 if (unlikely(len >= (sizeof(dev_str) - 1))) {
408 err = -ENOMEM;
409 xenbus_dev_fatal(pdev->xdev, err,
410 "String overflow while "
411 "reading configuration");
412 goto out;
413 }
414 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
415 dev_str, "%x:%x:%x.%x",
416 &domain, &bus, &slot, &func);
417 if (err < 0) {
418 xenbus_dev_fatal(pdev->xdev, err,
419 "Error reading device "
420 "configuration");
421 goto out;
422 }
423 if (err != 4) {
424 err = -EINVAL;
425 xenbus_dev_fatal(pdev->xdev, err,
426 "Error parsing pci device "
427 "configuration");
428 goto out;
429 }
430
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400431 err = xen_pcibk_export_device(pdev, domain, bus, slot,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400432 func, i);
433 if (err)
434 goto out;
435
436 /* Publish pci roots. */
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400437 err = xen_pcibk_publish_pci_roots(pdev,
438 xen_pcibk_publish_pci_root);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400439 if (err) {
440 xenbus_dev_fatal(pdev->xdev, err,
441 "Error while publish PCI root"
442 "buses for frontend");
443 goto out;
444 }
445
446 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename,
447 state_str, "%d",
448 XenbusStateInitialised);
449 if (err) {
450 xenbus_dev_fatal(pdev->xdev, err,
451 "Error switching substate of "
452 "dev-%d\n", i);
453 goto out;
454 }
455 break;
456
457 case XenbusStateClosing:
458 dev_dbg(&pdev->xdev->dev, "Detaching dev-%d ...\n", i);
459
460 len = snprintf(dev_str, sizeof(dev_str), "vdev-%d", i);
461 if (unlikely(len >= (sizeof(dev_str) - 1))) {
462 err = -ENOMEM;
463 xenbus_dev_fatal(pdev->xdev, err,
464 "String overflow while "
465 "reading configuration");
466 goto out;
467 }
468 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
469 dev_str, "%x:%x:%x.%x",
470 &domain, &bus, &slot, &func);
471 if (err < 0) {
472 xenbus_dev_fatal(pdev->xdev, err,
473 "Error reading device "
474 "configuration");
475 goto out;
476 }
477 if (err != 4) {
478 err = -EINVAL;
479 xenbus_dev_fatal(pdev->xdev, err,
480 "Error parsing pci device "
481 "configuration");
482 goto out;
483 }
484
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400485 err = xen_pcibk_remove_device(pdev, domain, bus, slot,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400486 func);
487 if (err)
488 goto out;
489
490 /* TODO: If at some point we implement support for pci
491 * root hot-remove on pcifront side, we'll need to
492 * remove unnecessary xenstore nodes of pci roots here.
493 */
494
495 break;
496
497 default:
498 break;
499 }
500 }
501
502 err = xenbus_switch_state(pdev->xdev, XenbusStateReconfigured);
503 if (err) {
504 xenbus_dev_fatal(pdev->xdev, err,
505 "Error switching to reconfigured state!");
506 goto out;
507 }
508
509out:
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 Wilk30edc142009-10-13 17:22:20 -0400566 /* It's possible we could get the call to setup twice, so make sure
567 * we're not already connected.
568 */
569 if (xenbus_read_driver_state(pdev->xdev->nodename) !=
570 XenbusStateInitWait)
571 goto out;
572
573 dev_dbg(&pdev->xdev->dev, "getting be setup\n");
574
575 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
576 &num_devs);
577 if (err != 1) {
578 if (err >= 0)
579 err = -EINVAL;
580 xenbus_dev_fatal(pdev->xdev, err,
581 "Error reading number of devices");
582 goto out;
583 }
584
585 for (i = 0; i < num_devs; i++) {
586 int l = snprintf(dev_str, sizeof(dev_str), "dev-%d", i);
587 if (unlikely(l >= (sizeof(dev_str) - 1))) {
588 err = -ENOMEM;
589 xenbus_dev_fatal(pdev->xdev, err,
590 "String overflow while reading "
591 "configuration");
592 goto out;
593 }
594
595 err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, dev_str,
596 "%x:%x:%x.%x", &domain, &bus, &slot, &func);
597 if (err < 0) {
598 xenbus_dev_fatal(pdev->xdev, err,
599 "Error reading device configuration");
600 goto out;
601 }
602 if (err != 4) {
603 err = -EINVAL;
604 xenbus_dev_fatal(pdev->xdev, err,
605 "Error parsing pci device "
606 "configuration");
607 goto out;
608 }
609
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400610 err = xen_pcibk_export_device(pdev, domain, bus, slot, func, i);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400611 if (err)
612 goto out;
613
614 /* Switch substate of this device. */
615 l = snprintf(state_str, sizeof(state_str), "state-%d", i);
616 if (unlikely(l >= (sizeof(state_str) - 1))) {
617 err = -ENOMEM;
618 xenbus_dev_fatal(pdev->xdev, err,
619 "String overflow while reading "
620 "configuration");
621 goto out;
622 }
623 err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, state_str,
624 "%d", XenbusStateInitialised);
625 if (err) {
626 xenbus_dev_fatal(pdev->xdev, err, "Error switching "
627 "substate of dev-%d\n", i);
628 goto out;
629 }
630 }
631
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400632 err = xen_pcibk_publish_pci_roots(pdev, xen_pcibk_publish_pci_root);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400633 if (err) {
634 xenbus_dev_fatal(pdev->xdev, err,
635 "Error while publish PCI root buses "
636 "for frontend");
637 goto out;
638 }
639
640 err = xenbus_switch_state(pdev->xdev, XenbusStateInitialised);
641 if (err)
642 xenbus_dev_fatal(pdev->xdev, err,
643 "Error switching to initialised state!");
644
645out:
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400646 if (!err)
647 /* see if pcifront is already configured (if not, we'll wait) */
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400648 xen_pcibk_attach(pdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400649
650 return err;
651}
652
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400653static void xen_pcibk_be_watch(struct xenbus_watch *watch,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400654 const char **vec, unsigned int len)
655{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400656 struct xen_pcibk_device *pdev =
657 container_of(watch, struct xen_pcibk_device, be_watch);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400658
659 switch (xenbus_read_driver_state(pdev->xdev->nodename)) {
660 case XenbusStateInitWait:
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400661 xen_pcibk_setup_backend(pdev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400662 break;
663
664 default:
665 break;
666 }
667}
668
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400669static int xen_pcibk_xenbus_probe(struct xenbus_device *dev,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400670 const struct xenbus_device_id *id)
671{
672 int err = 0;
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400673 struct xen_pcibk_device *pdev = alloc_pdev(dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400674
675 if (pdev == NULL) {
676 err = -ENOMEM;
677 xenbus_dev_fatal(dev, err,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400678 "Error allocating xen_pcibk_device struct");
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400679 goto out;
680 }
681
682 /* wait for xend to configure us */
683 err = xenbus_switch_state(dev, XenbusStateInitWait);
684 if (err)
685 goto out;
686
687 /* watch the backend node for backend configuration information */
688 err = xenbus_watch_path(dev, dev->nodename, &pdev->be_watch,
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400689 xen_pcibk_be_watch);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400690 if (err)
691 goto out;
Konrad Rzeszutek Wilk494ef202010-07-23 14:35:47 -0400692
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400693 pdev->be_watching = 1;
694
695 /* We need to force a call to our callback here in case
696 * xend already configured us!
697 */
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400698 xen_pcibk_be_watch(&pdev->be_watch, NULL, 0);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400699
700out:
701 return err;
702}
703
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400704static int xen_pcibk_xenbus_remove(struct xenbus_device *dev)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400705{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400706 struct xen_pcibk_device *pdev = dev_get_drvdata(&dev->dev);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400707
708 if (pdev != NULL)
709 free_pdev(pdev);
710
711 return 0;
712}
713
714static const struct xenbus_device_id xenpci_ids[] = {
715 {"pci"},
716 {""},
717};
718
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400719static struct xenbus_driver xenbus_xen_pcibk_driver = {
720 .name = DRV_NAME,
Konrad Rzeszutek Wilk8bfd4e02011-07-19 20:09:43 -0400721 .owner = THIS_MODULE,
722 .ids = xenpci_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,
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400726};
727
Konrad Rzeszutek Wilk2ebdc422011-07-11 16:49:41 -0400728struct xen_pcibk_backend *xen_pcibk_backend;
729
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 Wilka92336a2011-07-19 19:40:51 -0400732 xen_pcibk_wq = create_workqueue("xen_pciback_workqueue");
733 if (!xen_pcibk_wq) {
Konrad Rzeszutek Wilk8bfd4e02011-07-19 20:09:43 -0400734 printk(KERN_ERR "%s: create"
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400735 "xen_pciback_workqueue failed\n", __func__);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400736 return -EFAULT;
737 }
Konrad Rzeszutek Wilk2ebdc422011-07-11 16:49:41 -0400738 xen_pcibk_backend = &xen_pcibk_vpci_backend;
739 if (passthrough)
740 xen_pcibk_backend = &xen_pcibk_passthrough_backend;
741 pr_info(DRV_NAME ": backend is %s\n", xen_pcibk_backend->name);
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400742 return xenbus_register_backend(&xenbus_xen_pcibk_driver);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400743}
744
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400745void __exit xen_pcibk_xenbus_unregister(void)
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400746{
Konrad Rzeszutek Wilka92336a2011-07-19 19:40:51 -0400747 destroy_workqueue(xen_pcibk_wq);
748 xenbus_unregister_driver(&xenbus_xen_pcibk_driver);
Konrad Rzeszutek Wilk30edc142009-10-13 17:22:20 -0400749}