xenbus.c 18 KB

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