xenbus.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756
  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_lateeoi(
  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. enum xenbus_state state)
  294. {
  295. int err = 0;
  296. int num_devs;
  297. int domain, bus, slot, func;
  298. unsigned 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. if (xenbus_read_driver_state(pdev->xdev->nodename) != state)
  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. if (state != XenbusStateReconfiguring)
  416. /* Make sure we only reconfigure once. */
  417. goto out;
  418. err = xenbus_switch_state(pdev->xdev, XenbusStateReconfigured);
  419. if (err) {
  420. xenbus_dev_fatal(pdev->xdev, err,
  421. "Error switching to reconfigured state!");
  422. goto out;
  423. }
  424. out:
  425. mutex_unlock(&pdev->dev_lock);
  426. return 0;
  427. }
  428. static void xen_pcibk_frontend_changed(struct xenbus_device *xdev,
  429. enum xenbus_state fe_state)
  430. {
  431. struct xen_pcibk_device *pdev = dev_get_drvdata(&xdev->dev);
  432. dev_dbg(&xdev->dev, "fe state changed %d\n", fe_state);
  433. switch (fe_state) {
  434. case XenbusStateInitialised:
  435. xen_pcibk_attach(pdev);
  436. break;
  437. case XenbusStateReconfiguring:
  438. xen_pcibk_reconfigure(pdev, XenbusStateReconfiguring);
  439. break;
  440. case XenbusStateConnected:
  441. /* pcifront switched its state from reconfiguring to connected.
  442. * Then switch to connected state.
  443. */
  444. xenbus_switch_state(xdev, XenbusStateConnected);
  445. break;
  446. case XenbusStateClosing:
  447. xen_pcibk_disconnect(pdev);
  448. xenbus_switch_state(xdev, XenbusStateClosing);
  449. break;
  450. case XenbusStateClosed:
  451. xen_pcibk_disconnect(pdev);
  452. xenbus_switch_state(xdev, XenbusStateClosed);
  453. if (xenbus_dev_is_online(xdev))
  454. break;
  455. /* fall through if not online */
  456. case XenbusStateUnknown:
  457. dev_dbg(&xdev->dev, "frontend is gone! unregister device\n");
  458. device_unregister(&xdev->dev);
  459. break;
  460. default:
  461. break;
  462. }
  463. }
  464. static int xen_pcibk_setup_backend(struct xen_pcibk_device *pdev)
  465. {
  466. /* Get configuration from xend (if available now) */
  467. int domain, bus, slot, func;
  468. int err = 0;
  469. int i, num_devs;
  470. char dev_str[64];
  471. char state_str[64];
  472. mutex_lock(&pdev->dev_lock);
  473. /* It's possible we could get the call to setup twice, so make sure
  474. * we're not already connected.
  475. */
  476. if (xenbus_read_driver_state(pdev->xdev->nodename) !=
  477. XenbusStateInitWait)
  478. goto out;
  479. dev_dbg(&pdev->xdev->dev, "getting be setup\n");
  480. err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
  481. &num_devs);
  482. if (err != 1) {
  483. if (err >= 0)
  484. err = -EINVAL;
  485. xenbus_dev_fatal(pdev->xdev, err,
  486. "Error reading number of devices");
  487. goto out;
  488. }
  489. for (i = 0; i < num_devs; i++) {
  490. int l = snprintf(dev_str, sizeof(dev_str), "dev-%d", i);
  491. if (unlikely(l >= (sizeof(dev_str) - 1))) {
  492. err = -ENOMEM;
  493. xenbus_dev_fatal(pdev->xdev, err,
  494. "String overflow while reading "
  495. "configuration");
  496. goto out;
  497. }
  498. err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, dev_str,
  499. "%x:%x:%x.%x", &domain, &bus, &slot, &func);
  500. if (err < 0) {
  501. xenbus_dev_fatal(pdev->xdev, err,
  502. "Error reading device configuration");
  503. goto out;
  504. }
  505. if (err != 4) {
  506. err = -EINVAL;
  507. xenbus_dev_fatal(pdev->xdev, err,
  508. "Error parsing pci device "
  509. "configuration");
  510. goto out;
  511. }
  512. err = xen_pcibk_export_device(pdev, domain, bus, slot, func, i);
  513. if (err)
  514. goto out;
  515. /* Switch substate of this device. */
  516. l = snprintf(state_str, sizeof(state_str), "state-%d", i);
  517. if (unlikely(l >= (sizeof(state_str) - 1))) {
  518. err = -ENOMEM;
  519. xenbus_dev_fatal(pdev->xdev, err,
  520. "String overflow while reading "
  521. "configuration");
  522. goto out;
  523. }
  524. err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, state_str,
  525. "%d", XenbusStateInitialised);
  526. if (err) {
  527. xenbus_dev_fatal(pdev->xdev, err, "Error switching "
  528. "substate of dev-%d\n", i);
  529. goto out;
  530. }
  531. }
  532. err = xen_pcibk_publish_pci_roots(pdev, xen_pcibk_publish_pci_root);
  533. if (err) {
  534. xenbus_dev_fatal(pdev->xdev, err,
  535. "Error while publish PCI root buses "
  536. "for frontend");
  537. goto out;
  538. }
  539. err = xenbus_switch_state(pdev->xdev, XenbusStateInitialised);
  540. if (err)
  541. xenbus_dev_fatal(pdev->xdev, err,
  542. "Error switching to initialised state!");
  543. out:
  544. mutex_unlock(&pdev->dev_lock);
  545. if (!err)
  546. /* see if pcifront is already configured (if not, we'll wait) */
  547. xen_pcibk_attach(pdev);
  548. return err;
  549. }
  550. static void xen_pcibk_be_watch(struct xenbus_watch *watch,
  551. const char *path, const char *token)
  552. {
  553. struct xen_pcibk_device *pdev =
  554. container_of(watch, struct xen_pcibk_device, be_watch);
  555. switch (xenbus_read_driver_state(pdev->xdev->nodename)) {
  556. case XenbusStateInitWait:
  557. xen_pcibk_setup_backend(pdev);
  558. break;
  559. case XenbusStateInitialised:
  560. /*
  561. * We typically move to Initialised when the first device was
  562. * added. Hence subsequent devices getting added may need
  563. * reconfiguring.
  564. */
  565. xen_pcibk_reconfigure(pdev, XenbusStateInitialised);
  566. break;
  567. default:
  568. break;
  569. }
  570. }
  571. static int xen_pcibk_xenbus_probe(struct xenbus_device *dev,
  572. const struct xenbus_device_id *id)
  573. {
  574. int err = 0;
  575. struct xen_pcibk_device *pdev = alloc_pdev(dev);
  576. if (pdev == NULL) {
  577. err = -ENOMEM;
  578. xenbus_dev_fatal(dev, err,
  579. "Error allocating xen_pcibk_device struct");
  580. goto out;
  581. }
  582. /* wait for xend to configure us */
  583. err = xenbus_switch_state(dev, XenbusStateInitWait);
  584. if (err)
  585. goto out;
  586. /* watch the backend node for backend configuration information */
  587. err = xenbus_watch_path(dev, dev->nodename, &pdev->be_watch,
  588. NULL, xen_pcibk_be_watch);
  589. if (err)
  590. goto out;
  591. pdev->be_watching = 1;
  592. /* We need to force a call to our callback here in case
  593. * xend already configured us!
  594. */
  595. xen_pcibk_be_watch(&pdev->be_watch, NULL, 0);
  596. out:
  597. return err;
  598. }
  599. static int xen_pcibk_xenbus_remove(struct xenbus_device *dev)
  600. {
  601. struct xen_pcibk_device *pdev = dev_get_drvdata(&dev->dev);
  602. if (pdev != NULL)
  603. free_pdev(pdev);
  604. return 0;
  605. }
  606. static const struct xenbus_device_id xen_pcibk_ids[] = {
  607. {"pci"},
  608. {""},
  609. };
  610. static struct xenbus_driver xen_pcibk_driver = {
  611. .name = DRV_NAME,
  612. .ids = xen_pcibk_ids,
  613. .probe = xen_pcibk_xenbus_probe,
  614. .remove = xen_pcibk_xenbus_remove,
  615. .otherend_changed = xen_pcibk_frontend_changed,
  616. };
  617. const struct xen_pcibk_backend *__read_mostly xen_pcibk_backend;
  618. int __init xen_pcibk_xenbus_register(void)
  619. {
  620. xen_pcibk_backend = &xen_pcibk_vpci_backend;
  621. if (passthrough)
  622. xen_pcibk_backend = &xen_pcibk_passthrough_backend;
  623. pr_info("backend is %s\n", xen_pcibk_backend->name);
  624. return xenbus_register_backend(&xen_pcibk_driver);
  625. }
  626. void __exit xen_pcibk_xenbus_unregister(void)
  627. {
  628. xenbus_unregister_driver(&xen_pcibk_driver);
  629. }