xenbus.c 19 KB

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