xenbus.c 18 KB

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