xenbus.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744
  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. 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. err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, state_str,
  325. "%d", &substate);
  326. if (err != 1)
  327. substate = XenbusStateUnknown;
  328. switch (substate) {
  329. case XenbusStateInitialising:
  330. dev_dbg(&pdev->xdev->dev, "Attaching dev-%d ...\n", i);
  331. len = snprintf(dev_str, sizeof(dev_str), "dev-%d", i);
  332. if (unlikely(len >= (sizeof(dev_str) - 1))) {
  333. err = -ENOMEM;
  334. xenbus_dev_fatal(pdev->xdev, err,
  335. "String overflow while "
  336. "reading configuration");
  337. goto out;
  338. }
  339. err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
  340. dev_str, "%x:%x:%x.%x",
  341. &domain, &bus, &slot, &func);
  342. if (err < 0) {
  343. xenbus_dev_fatal(pdev->xdev, err,
  344. "Error reading device "
  345. "configuration");
  346. goto out;
  347. }
  348. if (err != 4) {
  349. err = -EINVAL;
  350. xenbus_dev_fatal(pdev->xdev, err,
  351. "Error parsing pci device "
  352. "configuration");
  353. goto out;
  354. }
  355. err = xen_pcibk_export_device(pdev, domain, bus, slot,
  356. func, i);
  357. if (err)
  358. goto out;
  359. /* Publish pci roots. */
  360. err = xen_pcibk_publish_pci_roots(pdev,
  361. xen_pcibk_publish_pci_root);
  362. if (err) {
  363. xenbus_dev_fatal(pdev->xdev, err,
  364. "Error while publish PCI root"
  365. "buses for frontend");
  366. goto out;
  367. }
  368. err = xenbus_printf(XBT_NIL, pdev->xdev->nodename,
  369. state_str, "%d",
  370. XenbusStateInitialised);
  371. if (err) {
  372. xenbus_dev_fatal(pdev->xdev, err,
  373. "Error switching substate of "
  374. "dev-%d\n", i);
  375. goto out;
  376. }
  377. break;
  378. case XenbusStateClosing:
  379. dev_dbg(&pdev->xdev->dev, "Detaching dev-%d ...\n", i);
  380. len = snprintf(dev_str, sizeof(dev_str), "vdev-%d", i);
  381. if (unlikely(len >= (sizeof(dev_str) - 1))) {
  382. err = -ENOMEM;
  383. xenbus_dev_fatal(pdev->xdev, err,
  384. "String overflow while "
  385. "reading configuration");
  386. goto out;
  387. }
  388. err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
  389. dev_str, "%x:%x:%x.%x",
  390. &domain, &bus, &slot, &func);
  391. if (err < 0) {
  392. xenbus_dev_fatal(pdev->xdev, err,
  393. "Error reading device "
  394. "configuration");
  395. goto out;
  396. }
  397. if (err != 4) {
  398. err = -EINVAL;
  399. xenbus_dev_fatal(pdev->xdev, err,
  400. "Error parsing pci device "
  401. "configuration");
  402. goto out;
  403. }
  404. err = xen_pcibk_remove_device(pdev, domain, bus, slot,
  405. func);
  406. if (err)
  407. goto out;
  408. /* TODO: If at some point we implement support for pci
  409. * root hot-remove on pcifront side, we'll need to
  410. * remove unnecessary xenstore nodes of pci roots here.
  411. */
  412. break;
  413. default:
  414. break;
  415. }
  416. }
  417. err = xenbus_switch_state(pdev->xdev, XenbusStateReconfigured);
  418. if (err) {
  419. xenbus_dev_fatal(pdev->xdev, err,
  420. "Error switching to reconfigured state!");
  421. goto out;
  422. }
  423. out:
  424. mutex_unlock(&pdev->dev_lock);
  425. return 0;
  426. }
  427. static void xen_pcibk_frontend_changed(struct xenbus_device *xdev,
  428. enum xenbus_state fe_state)
  429. {
  430. struct xen_pcibk_device *pdev = dev_get_drvdata(&xdev->dev);
  431. dev_dbg(&xdev->dev, "fe state changed %d\n", fe_state);
  432. switch (fe_state) {
  433. case XenbusStateInitialised:
  434. xen_pcibk_attach(pdev);
  435. break;
  436. case XenbusStateReconfiguring:
  437. xen_pcibk_reconfigure(pdev);
  438. break;
  439. case XenbusStateConnected:
  440. /* pcifront switched its state from reconfiguring to connected.
  441. * Then switch to connected state.
  442. */
  443. xenbus_switch_state(xdev, XenbusStateConnected);
  444. break;
  445. case XenbusStateClosing:
  446. xen_pcibk_disconnect(pdev);
  447. xenbus_switch_state(xdev, XenbusStateClosing);
  448. break;
  449. case XenbusStateClosed:
  450. xen_pcibk_disconnect(pdev);
  451. xenbus_switch_state(xdev, XenbusStateClosed);
  452. if (xenbus_dev_is_online(xdev))
  453. break;
  454. /* fall through if not online */
  455. case XenbusStateUnknown:
  456. dev_dbg(&xdev->dev, "frontend is gone! unregister device\n");
  457. device_unregister(&xdev->dev);
  458. break;
  459. default:
  460. break;
  461. }
  462. }
  463. static int xen_pcibk_setup_backend(struct xen_pcibk_device *pdev)
  464. {
  465. /* Get configuration from xend (if available now) */
  466. int domain, bus, slot, func;
  467. int err = 0;
  468. int i, num_devs;
  469. char dev_str[64];
  470. char state_str[64];
  471. mutex_lock(&pdev->dev_lock);
  472. /* It's possible we could get the call to setup twice, so make sure
  473. * we're not already connected.
  474. */
  475. if (xenbus_read_driver_state(pdev->xdev->nodename) !=
  476. XenbusStateInitWait)
  477. goto out;
  478. dev_dbg(&pdev->xdev->dev, "getting be setup\n");
  479. err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
  480. &num_devs);
  481. if (err != 1) {
  482. if (err >= 0)
  483. err = -EINVAL;
  484. xenbus_dev_fatal(pdev->xdev, err,
  485. "Error reading number of devices");
  486. goto out;
  487. }
  488. for (i = 0; i < num_devs; i++) {
  489. int l = snprintf(dev_str, sizeof(dev_str), "dev-%d", i);
  490. if (unlikely(l >= (sizeof(dev_str) - 1))) {
  491. err = -ENOMEM;
  492. xenbus_dev_fatal(pdev->xdev, err,
  493. "String overflow while reading "
  494. "configuration");
  495. goto out;
  496. }
  497. err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, dev_str,
  498. "%x:%x:%x.%x", &domain, &bus, &slot, &func);
  499. if (err < 0) {
  500. xenbus_dev_fatal(pdev->xdev, err,
  501. "Error reading device configuration");
  502. goto out;
  503. }
  504. if (err != 4) {
  505. err = -EINVAL;
  506. xenbus_dev_fatal(pdev->xdev, err,
  507. "Error parsing pci device "
  508. "configuration");
  509. goto out;
  510. }
  511. err = xen_pcibk_export_device(pdev, domain, bus, slot, func, i);
  512. if (err)
  513. goto out;
  514. /* Switch substate of this device. */
  515. l = snprintf(state_str, sizeof(state_str), "state-%d", i);
  516. if (unlikely(l >= (sizeof(state_str) - 1))) {
  517. err = -ENOMEM;
  518. xenbus_dev_fatal(pdev->xdev, err,
  519. "String overflow while reading "
  520. "configuration");
  521. goto out;
  522. }
  523. err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, state_str,
  524. "%d", XenbusStateInitialised);
  525. if (err) {
  526. xenbus_dev_fatal(pdev->xdev, err, "Error switching "
  527. "substate of dev-%d\n", i);
  528. goto out;
  529. }
  530. }
  531. err = xen_pcibk_publish_pci_roots(pdev, xen_pcibk_publish_pci_root);
  532. if (err) {
  533. xenbus_dev_fatal(pdev->xdev, err,
  534. "Error while publish PCI root buses "
  535. "for frontend");
  536. goto out;
  537. }
  538. err = xenbus_switch_state(pdev->xdev, XenbusStateInitialised);
  539. if (err)
  540. xenbus_dev_fatal(pdev->xdev, err,
  541. "Error switching to initialised state!");
  542. out:
  543. mutex_unlock(&pdev->dev_lock);
  544. if (!err)
  545. /* see if pcifront is already configured (if not, we'll wait) */
  546. xen_pcibk_attach(pdev);
  547. return err;
  548. }
  549. static void xen_pcibk_be_watch(struct xenbus_watch *watch,
  550. const char **vec, unsigned int len)
  551. {
  552. struct xen_pcibk_device *pdev =
  553. container_of(watch, struct xen_pcibk_device, be_watch);
  554. switch (xenbus_read_driver_state(pdev->xdev->nodename)) {
  555. case XenbusStateInitWait:
  556. xen_pcibk_setup_backend(pdev);
  557. break;
  558. default:
  559. break;
  560. }
  561. }
  562. static int xen_pcibk_xenbus_probe(struct xenbus_device *dev,
  563. const struct xenbus_device_id *id)
  564. {
  565. int err = 0;
  566. struct xen_pcibk_device *pdev = alloc_pdev(dev);
  567. if (pdev == NULL) {
  568. err = -ENOMEM;
  569. xenbus_dev_fatal(dev, err,
  570. "Error allocating xen_pcibk_device struct");
  571. goto out;
  572. }
  573. /* wait for xend to configure us */
  574. err = xenbus_switch_state(dev, XenbusStateInitWait);
  575. if (err)
  576. goto out;
  577. /* watch the backend node for backend configuration information */
  578. err = xenbus_watch_path(dev, dev->nodename, &pdev->be_watch,
  579. xen_pcibk_be_watch);
  580. if (err)
  581. goto out;
  582. pdev->be_watching = 1;
  583. /* We need to force a call to our callback here in case
  584. * xend already configured us!
  585. */
  586. xen_pcibk_be_watch(&pdev->be_watch, NULL, 0);
  587. out:
  588. return err;
  589. }
  590. static int xen_pcibk_xenbus_remove(struct xenbus_device *dev)
  591. {
  592. struct xen_pcibk_device *pdev = dev_get_drvdata(&dev->dev);
  593. if (pdev != NULL)
  594. free_pdev(pdev);
  595. return 0;
  596. }
  597. static const struct xenbus_device_id xen_pcibk_ids[] = {
  598. {"pci"},
  599. {""},
  600. };
  601. static struct xenbus_driver xen_pcibk_driver = {
  602. .name = DRV_NAME,
  603. .ids = xen_pcibk_ids,
  604. .probe = xen_pcibk_xenbus_probe,
  605. .remove = xen_pcibk_xenbus_remove,
  606. .otherend_changed = xen_pcibk_frontend_changed,
  607. };
  608. const struct xen_pcibk_backend *__read_mostly xen_pcibk_backend;
  609. int __init xen_pcibk_xenbus_register(void)
  610. {
  611. xen_pcibk_backend = &xen_pcibk_vpci_backend;
  612. if (passthrough)
  613. xen_pcibk_backend = &xen_pcibk_passthrough_backend;
  614. pr_info("backend is %s\n", xen_pcibk_backend->name);
  615. return xenbus_register_backend(&xen_pcibk_driver);
  616. }
  617. void __exit xen_pcibk_xenbus_unregister(void)
  618. {
  619. xenbus_unregister_driver(&xen_pcibk_driver);
  620. }