xenbus.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  1. /*
  2. * PCI Backend Xenbus Setup - handles setup with frontend and xend
  3. *
  4. * Author: Ryan Wilson <hap9@epoch.ncsc.mil>
  5. */
  6. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  7. #include <linux/moduleparam.h>
  8. #include <linux/init.h>
  9. #include <linux/list.h>
  10. #include <linux/vmalloc.h>
  11. #include <linux/workqueue.h>
  12. #include <xen/xenbus.h>
  13. #include <xen/events.h>
  14. #include <asm/xen/pci.h>
  15. #include "pciback.h"
  16. #define INVALID_EVTCHN_IRQ (-1)
  17. static bool __read_mostly passthrough;
  18. module_param(passthrough, bool, S_IRUGO);
  19. MODULE_PARM_DESC(passthrough,
  20. "Option to specify how to export PCI topology to guest:\n"\
  21. " 0 - (default) Hide the true PCI topology and makes the frontend\n"\
  22. " there is a single PCI bus with only the exported devices on it.\n"\
  23. " For example, a device at 03:05.0 will be re-assigned to 00:00.0\n"\
  24. " while second device at 02:1a.1 will be re-assigned to 00:01.1.\n"\
  25. " 1 - Passthrough provides a real view of the PCI topology to the\n"\
  26. " frontend (for example, a device at 06:01.b will still appear at\n"\
  27. " 06:01.b to the frontend). This is similar to how Xen 2.0.x\n"\
  28. " exposed PCI devices to its driver domains. This may be required\n"\
  29. " for drivers which depend on finding their hardward in certain\n"\
  30. " bus/slot locations.");
  31. static struct xen_pcibk_device *alloc_pdev(struct xenbus_device *xdev)
  32. {
  33. struct xen_pcibk_device *pdev;
  34. pdev = kzalloc(sizeof(struct xen_pcibk_device), GFP_KERNEL);
  35. if (pdev == NULL)
  36. goto out;
  37. dev_dbg(&xdev->dev, "allocated pdev @ 0x%p\n", pdev);
  38. pdev->xdev = xdev;
  39. mutex_init(&pdev->dev_lock);
  40. pdev->sh_info = NULL;
  41. pdev->evtchn_irq = INVALID_EVTCHN_IRQ;
  42. pdev->be_watching = 0;
  43. INIT_WORK(&pdev->op_work, xen_pcibk_do_op);
  44. if (xen_pcibk_init_devices(pdev)) {
  45. kfree(pdev);
  46. pdev = NULL;
  47. }
  48. dev_set_drvdata(&xdev->dev, pdev);
  49. out:
  50. return pdev;
  51. }
  52. static void xen_pcibk_disconnect(struct xen_pcibk_device *pdev)
  53. {
  54. mutex_lock(&pdev->dev_lock);
  55. /* Ensure the guest can't trigger our handler before removing devices */
  56. if (pdev->evtchn_irq != INVALID_EVTCHN_IRQ) {
  57. unbind_from_irqhandler(pdev->evtchn_irq, pdev);
  58. pdev->evtchn_irq = INVALID_EVTCHN_IRQ;
  59. }
  60. /* If the driver domain started an op, make sure we complete it
  61. * before releasing the shared memory */
  62. flush_work(&pdev->op_work);
  63. if (pdev->sh_info != NULL) {
  64. xenbus_unmap_ring_vfree(pdev->xdev, pdev->sh_info);
  65. pdev->sh_info = NULL;
  66. }
  67. mutex_unlock(&pdev->dev_lock);
  68. }
  69. static void free_pdev(struct xen_pcibk_device *pdev)
  70. {
  71. if (pdev->be_watching) {
  72. unregister_xenbus_watch(&pdev->be_watch);
  73. pdev->be_watching = 0;
  74. }
  75. xen_pcibk_disconnect(pdev);
  76. /* N.B. This calls pcistub_put_pci_dev which does the FLR on all
  77. * of the PCIe devices. */
  78. xen_pcibk_release_devices(pdev);
  79. dev_set_drvdata(&pdev->xdev->dev, NULL);
  80. pdev->xdev = NULL;
  81. kfree(pdev);
  82. }
  83. static int xen_pcibk_do_attach(struct xen_pcibk_device *pdev, int gnt_ref,
  84. int remote_evtchn)
  85. {
  86. int err = 0;
  87. void *vaddr;
  88. dev_dbg(&pdev->xdev->dev,
  89. "Attaching to frontend resources - gnt_ref=%d evtchn=%d\n",
  90. gnt_ref, remote_evtchn);
  91. err = xenbus_map_ring_valloc(pdev->xdev, &gnt_ref, 1, &vaddr);
  92. if (err < 0) {
  93. xenbus_dev_fatal(pdev->xdev, err,
  94. "Error mapping other domain page in ours.");
  95. goto out;
  96. }
  97. pdev->sh_info = vaddr;
  98. err = bind_interdomain_evtchn_to_irqhandler(
  99. pdev->xdev->otherend_id, remote_evtchn, xen_pcibk_handle_event,
  100. 0, DRV_NAME, pdev);
  101. if (err < 0) {
  102. xenbus_dev_fatal(pdev->xdev, err,
  103. "Error binding event channel to IRQ");
  104. goto out;
  105. }
  106. pdev->evtchn_irq = err;
  107. err = 0;
  108. dev_dbg(&pdev->xdev->dev, "Attached!\n");
  109. out:
  110. return err;
  111. }
  112. static int xen_pcibk_attach(struct xen_pcibk_device *pdev)
  113. {
  114. int err = 0;
  115. int gnt_ref, remote_evtchn;
  116. char *magic = NULL;
  117. mutex_lock(&pdev->dev_lock);
  118. /* Make sure we only do this setup once */
  119. if (xenbus_read_driver_state(pdev->xdev->nodename) !=
  120. XenbusStateInitialised)
  121. goto out;
  122. /* Wait for frontend to state that it has published the configuration */
  123. if (xenbus_read_driver_state(pdev->xdev->otherend) !=
  124. XenbusStateInitialised)
  125. goto out;
  126. dev_dbg(&pdev->xdev->dev, "Reading frontend config\n");
  127. err = xenbus_gather(XBT_NIL, pdev->xdev->otherend,
  128. "pci-op-ref", "%u", &gnt_ref,
  129. "event-channel", "%u", &remote_evtchn,
  130. "magic", NULL, &magic, NULL);
  131. if (err) {
  132. /* If configuration didn't get read correctly, wait longer */
  133. xenbus_dev_fatal(pdev->xdev, err,
  134. "Error reading configuration from frontend");
  135. goto out;
  136. }
  137. if (magic == NULL || strcmp(magic, XEN_PCI_MAGIC) != 0) {
  138. xenbus_dev_fatal(pdev->xdev, -EFAULT,
  139. "version mismatch (%s/%s) with pcifront - "
  140. "halting " DRV_NAME,
  141. magic, XEN_PCI_MAGIC);
  142. err = -EFAULT;
  143. goto out;
  144. }
  145. err = xen_pcibk_do_attach(pdev, gnt_ref, remote_evtchn);
  146. if (err)
  147. goto out;
  148. dev_dbg(&pdev->xdev->dev, "Connecting...\n");
  149. err = xenbus_switch_state(pdev->xdev, XenbusStateConnected);
  150. if (err)
  151. xenbus_dev_fatal(pdev->xdev, err,
  152. "Error switching to connected state!");
  153. dev_dbg(&pdev->xdev->dev, "Connected? %d\n", err);
  154. out:
  155. mutex_unlock(&pdev->dev_lock);
  156. kfree(magic);
  157. return err;
  158. }
  159. static int xen_pcibk_publish_pci_dev(struct xen_pcibk_device *pdev,
  160. unsigned int domain, unsigned int bus,
  161. unsigned int devfn, unsigned int devid)
  162. {
  163. int err;
  164. int len;
  165. char str[64];
  166. len = snprintf(str, sizeof(str), "vdev-%d", devid);
  167. if (unlikely(len >= (sizeof(str) - 1))) {
  168. err = -ENOMEM;
  169. goto out;
  170. }
  171. /* Note: The PV protocol uses %02x, don't change it */
  172. err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
  173. "%04x:%02x:%02x.%02x", domain, bus,
  174. PCI_SLOT(devfn), PCI_FUNC(devfn));
  175. out:
  176. return err;
  177. }
  178. static int xen_pcibk_export_device(struct xen_pcibk_device *pdev,
  179. int domain, int bus, int slot, int func,
  180. int devid)
  181. {
  182. struct pci_dev *dev;
  183. int err = 0;
  184. dev_dbg(&pdev->xdev->dev, "exporting dom %x bus %x slot %x func %x\n",
  185. domain, bus, slot, func);
  186. dev = pcistub_get_pci_dev_by_slot(pdev, domain, bus, slot, func);
  187. if (!dev) {
  188. err = -EINVAL;
  189. xenbus_dev_fatal(pdev->xdev, err,
  190. "Couldn't locate PCI device "
  191. "(%04x:%02x:%02x.%d)! "
  192. "perhaps already in-use?",
  193. domain, bus, slot, func);
  194. goto out;
  195. }
  196. err = xen_pcibk_add_pci_dev(pdev, dev, devid,
  197. xen_pcibk_publish_pci_dev);
  198. if (err)
  199. goto out;
  200. dev_info(&dev->dev, "registering for %d\n", pdev->xdev->otherend_id);
  201. if (xen_register_device_domain_owner(dev,
  202. pdev->xdev->otherend_id) != 0) {
  203. dev_err(&dev->dev, "Stealing ownership from dom%d.\n",
  204. xen_find_device_domain_owner(dev));
  205. xen_unregister_device_domain_owner(dev);
  206. xen_register_device_domain_owner(dev, pdev->xdev->otherend_id);
  207. }
  208. /* TODO: It'd be nice to export a bridge and have all of its children
  209. * get exported with it. This may be best done in xend (which will
  210. * have to calculate resource usage anyway) but we probably want to
  211. * put something in here to ensure that if a bridge gets given to a
  212. * driver domain, that all devices under that bridge are not given
  213. * to other driver domains (as he who controls the bridge can disable
  214. * it and stop the other devices from working).
  215. */
  216. out:
  217. return err;
  218. }
  219. static int xen_pcibk_remove_device(struct xen_pcibk_device *pdev,
  220. int domain, int bus, int slot, int func)
  221. {
  222. int err = 0;
  223. struct pci_dev *dev;
  224. dev_dbg(&pdev->xdev->dev, "removing dom %x bus %x slot %x func %x\n",
  225. domain, bus, slot, func);
  226. dev = xen_pcibk_get_pci_dev(pdev, domain, bus, PCI_DEVFN(slot, func));
  227. if (!dev) {
  228. err = -EINVAL;
  229. dev_dbg(&pdev->xdev->dev, "Couldn't locate PCI device "
  230. "(%04x:%02x:%02x.%d)! not owned by this domain\n",
  231. domain, bus, slot, func);
  232. goto out;
  233. }
  234. dev_dbg(&dev->dev, "unregistering for %d\n", pdev->xdev->otherend_id);
  235. xen_unregister_device_domain_owner(dev);
  236. /* N.B. This ends up calling pcistub_put_pci_dev which ends up
  237. * doing the FLR. */
  238. xen_pcibk_release_pci_dev(pdev, dev, true /* use the lock. */);
  239. out:
  240. return err;
  241. }
  242. static int xen_pcibk_publish_pci_root(struct xen_pcibk_device *pdev,
  243. unsigned int domain, unsigned int bus)
  244. {
  245. unsigned int d, b;
  246. int i, root_num, len, err;
  247. char str[64];
  248. dev_dbg(&pdev->xdev->dev, "Publishing pci roots\n");
  249. err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
  250. "root_num", "%d", &root_num);
  251. if (err == 0 || err == -ENOENT)
  252. root_num = 0;
  253. else if (err < 0)
  254. goto out;
  255. /* Verify that we haven't already published this pci root */
  256. for (i = 0; i < root_num; i++) {
  257. len = snprintf(str, sizeof(str), "root-%d", i);
  258. if (unlikely(len >= (sizeof(str) - 1))) {
  259. err = -ENOMEM;
  260. goto out;
  261. }
  262. err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
  263. str, "%x:%x", &d, &b);
  264. if (err < 0)
  265. goto out;
  266. if (err != 2) {
  267. err = -EINVAL;
  268. goto out;
  269. }
  270. if (d == domain && b == bus) {
  271. err = 0;
  272. goto out;
  273. }
  274. }
  275. len = snprintf(str, sizeof(str), "root-%d", root_num);
  276. if (unlikely(len >= (sizeof(str) - 1))) {
  277. err = -ENOMEM;
  278. goto out;
  279. }
  280. dev_dbg(&pdev->xdev->dev, "writing root %d at %04x:%02x\n",
  281. root_num, domain, bus);
  282. err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
  283. "%04x:%02x", domain, bus);
  284. if (err)
  285. goto out;
  286. err = xenbus_printf(XBT_NIL, pdev->xdev->nodename,
  287. "root_num", "%d", (root_num + 1));
  288. out:
  289. return err;
  290. }
  291. static int xen_pcibk_reconfigure(struct xen_pcibk_device *pdev)
  292. {
  293. int err = 0;
  294. int num_devs;
  295. int domain, bus, slot, func;
  296. unsigned int substate;
  297. int i, len;
  298. char state_str[64];
  299. char dev_str[64];
  300. dev_dbg(&pdev->xdev->dev, "Reconfiguring device ...\n");
  301. mutex_lock(&pdev->dev_lock);
  302. /* Make sure we only reconfigure once */
  303. if (xenbus_read_driver_state(pdev->xdev->nodename) !=
  304. XenbusStateReconfiguring)
  305. goto out;
  306. err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
  307. &num_devs);
  308. if (err != 1) {
  309. if (err >= 0)
  310. err = -EINVAL;
  311. xenbus_dev_fatal(pdev->xdev, err,
  312. "Error reading number of devices");
  313. goto out;
  314. }
  315. for (i = 0; i < num_devs; i++) {
  316. len = snprintf(state_str, sizeof(state_str), "state-%d", i);
  317. if (unlikely(len >= (sizeof(state_str) - 1))) {
  318. err = -ENOMEM;
  319. xenbus_dev_fatal(pdev->xdev, err,
  320. "String overflow while reading "
  321. "configuration");
  322. goto out;
  323. }
  324. substate = xenbus_read_unsigned(pdev->xdev->nodename, state_str,
  325. XenbusStateUnknown);
  326. switch (substate) {
  327. case XenbusStateInitialising:
  328. dev_dbg(&pdev->xdev->dev, "Attaching dev-%d ...\n", i);
  329. len = snprintf(dev_str, sizeof(dev_str), "dev-%d", i);
  330. if (unlikely(len >= (sizeof(dev_str) - 1))) {
  331. err = -ENOMEM;
  332. xenbus_dev_fatal(pdev->xdev, err,
  333. "String overflow while "
  334. "reading configuration");
  335. goto out;
  336. }
  337. err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
  338. dev_str, "%x:%x:%x.%x",
  339. &domain, &bus, &slot, &func);
  340. if (err < 0) {
  341. xenbus_dev_fatal(pdev->xdev, err,
  342. "Error reading device "
  343. "configuration");
  344. goto out;
  345. }
  346. if (err != 4) {
  347. err = -EINVAL;
  348. xenbus_dev_fatal(pdev->xdev, err,
  349. "Error parsing pci device "
  350. "configuration");
  351. goto out;
  352. }
  353. err = xen_pcibk_export_device(pdev, domain, bus, slot,
  354. func, i);
  355. if (err)
  356. goto out;
  357. /* Publish pci roots. */
  358. err = xen_pcibk_publish_pci_roots(pdev,
  359. xen_pcibk_publish_pci_root);
  360. if (err) {
  361. xenbus_dev_fatal(pdev->xdev, err,
  362. "Error while publish PCI root"
  363. "buses for frontend");
  364. goto out;
  365. }
  366. err = xenbus_printf(XBT_NIL, pdev->xdev->nodename,
  367. state_str, "%d",
  368. XenbusStateInitialised);
  369. if (err) {
  370. xenbus_dev_fatal(pdev->xdev, err,
  371. "Error switching substate of "
  372. "dev-%d\n", i);
  373. goto out;
  374. }
  375. break;
  376. case XenbusStateClosing:
  377. dev_dbg(&pdev->xdev->dev, "Detaching dev-%d ...\n", i);
  378. len = snprintf(dev_str, sizeof(dev_str), "vdev-%d", i);
  379. if (unlikely(len >= (sizeof(dev_str) - 1))) {
  380. err = -ENOMEM;
  381. xenbus_dev_fatal(pdev->xdev, err,
  382. "String overflow while "
  383. "reading configuration");
  384. goto out;
  385. }
  386. err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
  387. dev_str, "%x:%x:%x.%x",
  388. &domain, &bus, &slot, &func);
  389. if (err < 0) {
  390. xenbus_dev_fatal(pdev->xdev, err,
  391. "Error reading device "
  392. "configuration");
  393. goto out;
  394. }
  395. if (err != 4) {
  396. err = -EINVAL;
  397. xenbus_dev_fatal(pdev->xdev, err,
  398. "Error parsing pci device "
  399. "configuration");
  400. goto out;
  401. }
  402. err = xen_pcibk_remove_device(pdev, domain, bus, slot,
  403. func);
  404. if (err)
  405. goto out;
  406. /* TODO: If at some point we implement support for pci
  407. * root hot-remove on pcifront side, we'll need to
  408. * remove unnecessary xenstore nodes of pci roots here.
  409. */
  410. break;
  411. default:
  412. break;
  413. }
  414. }
  415. err = xenbus_switch_state(pdev->xdev, XenbusStateReconfigured);
  416. if (err) {
  417. xenbus_dev_fatal(pdev->xdev, err,
  418. "Error switching to reconfigured state!");
  419. goto out;
  420. }
  421. out:
  422. mutex_unlock(&pdev->dev_lock);
  423. return 0;
  424. }
  425. static void xen_pcibk_frontend_changed(struct xenbus_device *xdev,
  426. enum xenbus_state fe_state)
  427. {
  428. struct xen_pcibk_device *pdev = dev_get_drvdata(&xdev->dev);
  429. dev_dbg(&xdev->dev, "fe state changed %d\n", fe_state);
  430. switch (fe_state) {
  431. case XenbusStateInitialised:
  432. xen_pcibk_attach(pdev);
  433. break;
  434. case XenbusStateReconfiguring:
  435. xen_pcibk_reconfigure(pdev);
  436. break;
  437. case XenbusStateConnected:
  438. /* pcifront switched its state from reconfiguring to connected.
  439. * Then switch to connected state.
  440. */
  441. xenbus_switch_state(xdev, XenbusStateConnected);
  442. break;
  443. case XenbusStateClosing:
  444. xen_pcibk_disconnect(pdev);
  445. xenbus_switch_state(xdev, XenbusStateClosing);
  446. break;
  447. case XenbusStateClosed:
  448. xen_pcibk_disconnect(pdev);
  449. xenbus_switch_state(xdev, XenbusStateClosed);
  450. if (xenbus_dev_is_online(xdev))
  451. break;
  452. /* fall through if not online */
  453. case XenbusStateUnknown:
  454. dev_dbg(&xdev->dev, "frontend is gone! unregister device\n");
  455. device_unregister(&xdev->dev);
  456. break;
  457. default:
  458. break;
  459. }
  460. }
  461. static int xen_pcibk_setup_backend(struct xen_pcibk_device *pdev)
  462. {
  463. /* Get configuration from xend (if available now) */
  464. int domain, bus, slot, func;
  465. int err = 0;
  466. int i, num_devs;
  467. char dev_str[64];
  468. char state_str[64];
  469. mutex_lock(&pdev->dev_lock);
  470. /* It's possible we could get the call to setup twice, so make sure
  471. * we're not already connected.
  472. */
  473. if (xenbus_read_driver_state(pdev->xdev->nodename) !=
  474. XenbusStateInitWait)
  475. goto out;
  476. dev_dbg(&pdev->xdev->dev, "getting be setup\n");
  477. err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
  478. &num_devs);
  479. if (err != 1) {
  480. if (err >= 0)
  481. err = -EINVAL;
  482. xenbus_dev_fatal(pdev->xdev, err,
  483. "Error reading number of devices");
  484. goto out;
  485. }
  486. for (i = 0; i < num_devs; i++) {
  487. int l = snprintf(dev_str, sizeof(dev_str), "dev-%d", i);
  488. if (unlikely(l >= (sizeof(dev_str) - 1))) {
  489. err = -ENOMEM;
  490. xenbus_dev_fatal(pdev->xdev, err,
  491. "String overflow while reading "
  492. "configuration");
  493. goto out;
  494. }
  495. err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, dev_str,
  496. "%x:%x:%x.%x", &domain, &bus, &slot, &func);
  497. if (err < 0) {
  498. xenbus_dev_fatal(pdev->xdev, err,
  499. "Error reading device configuration");
  500. goto out;
  501. }
  502. if (err != 4) {
  503. err = -EINVAL;
  504. xenbus_dev_fatal(pdev->xdev, err,
  505. "Error parsing pci device "
  506. "configuration");
  507. goto out;
  508. }
  509. err = xen_pcibk_export_device(pdev, domain, bus, slot, func, i);
  510. if (err)
  511. goto out;
  512. /* Switch substate of this device. */
  513. l = snprintf(state_str, sizeof(state_str), "state-%d", i);
  514. if (unlikely(l >= (sizeof(state_str) - 1))) {
  515. err = -ENOMEM;
  516. xenbus_dev_fatal(pdev->xdev, err,
  517. "String overflow while reading "
  518. "configuration");
  519. goto out;
  520. }
  521. err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, state_str,
  522. "%d", XenbusStateInitialised);
  523. if (err) {
  524. xenbus_dev_fatal(pdev->xdev, err, "Error switching "
  525. "substate of dev-%d\n", i);
  526. goto out;
  527. }
  528. }
  529. err = xen_pcibk_publish_pci_roots(pdev, xen_pcibk_publish_pci_root);
  530. if (err) {
  531. xenbus_dev_fatal(pdev->xdev, err,
  532. "Error while publish PCI root buses "
  533. "for frontend");
  534. goto out;
  535. }
  536. err = xenbus_switch_state(pdev->xdev, XenbusStateInitialised);
  537. if (err)
  538. xenbus_dev_fatal(pdev->xdev, err,
  539. "Error switching to initialised state!");
  540. out:
  541. mutex_unlock(&pdev->dev_lock);
  542. if (!err)
  543. /* see if pcifront is already configured (if not, we'll wait) */
  544. xen_pcibk_attach(pdev);
  545. return err;
  546. }
  547. static void xen_pcibk_be_watch(struct xenbus_watch *watch,
  548. const char *path, const char *token)
  549. {
  550. struct xen_pcibk_device *pdev =
  551. container_of(watch, struct xen_pcibk_device, be_watch);
  552. switch (xenbus_read_driver_state(pdev->xdev->nodename)) {
  553. case XenbusStateInitWait:
  554. xen_pcibk_setup_backend(pdev);
  555. break;
  556. default:
  557. break;
  558. }
  559. }
  560. static int xen_pcibk_xenbus_probe(struct xenbus_device *dev,
  561. const struct xenbus_device_id *id)
  562. {
  563. int err = 0;
  564. struct xen_pcibk_device *pdev = alloc_pdev(dev);
  565. if (pdev == NULL) {
  566. err = -ENOMEM;
  567. xenbus_dev_fatal(dev, err,
  568. "Error allocating xen_pcibk_device struct");
  569. goto out;
  570. }
  571. /* wait for xend to configure us */
  572. err = xenbus_switch_state(dev, XenbusStateInitWait);
  573. if (err)
  574. goto out;
  575. /* watch the backend node for backend configuration information */
  576. err = xenbus_watch_path(dev, dev->nodename, &pdev->be_watch,
  577. xen_pcibk_be_watch);
  578. if (err)
  579. goto out;
  580. pdev->be_watching = 1;
  581. /* We need to force a call to our callback here in case
  582. * xend already configured us!
  583. */
  584. xen_pcibk_be_watch(&pdev->be_watch, NULL, 0);
  585. out:
  586. return err;
  587. }
  588. static int xen_pcibk_xenbus_remove(struct xenbus_device *dev)
  589. {
  590. struct xen_pcibk_device *pdev = dev_get_drvdata(&dev->dev);
  591. if (pdev != NULL)
  592. free_pdev(pdev);
  593. return 0;
  594. }
  595. static const struct xenbus_device_id xen_pcibk_ids[] = {
  596. {"pci"},
  597. {""},
  598. };
  599. static struct xenbus_driver xen_pcibk_driver = {
  600. .name = DRV_NAME,
  601. .ids = xen_pcibk_ids,
  602. .probe = xen_pcibk_xenbus_probe,
  603. .remove = xen_pcibk_xenbus_remove,
  604. .otherend_changed = xen_pcibk_frontend_changed,
  605. };
  606. const struct xen_pcibk_backend *__read_mostly xen_pcibk_backend;
  607. int __init xen_pcibk_xenbus_register(void)
  608. {
  609. xen_pcibk_backend = &xen_pcibk_vpci_backend;
  610. if (passthrough)
  611. xen_pcibk_backend = &xen_pcibk_passthrough_backend;
  612. pr_info("backend is %s\n", xen_pcibk_backend->name);
  613. return xenbus_register_backend(&xen_pcibk_driver);
  614. }
  615. void __exit xen_pcibk_xenbus_unregister(void)
  616. {
  617. xenbus_unregister_driver(&xen_pcibk_driver);
  618. }