xenbus.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742
  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 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. 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. /* Note: The PV protocol uses %02x, don't change it */
  170. err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
  171. "%04x:%02x:%02x.%02x", domain, bus,
  172. PCI_SLOT(devfn), PCI_FUNC(devfn));
  173. out:
  174. return err;
  175. }
  176. static int xen_pcibk_export_device(struct xen_pcibk_device *pdev,
  177. int domain, int bus, int slot, int func,
  178. int devid)
  179. {
  180. struct pci_dev *dev;
  181. int err = 0;
  182. dev_dbg(&pdev->xdev->dev, "exporting dom %x bus %x slot %x func %x\n",
  183. domain, bus, slot, func);
  184. dev = pcistub_get_pci_dev_by_slot(pdev, domain, bus, slot, func);
  185. if (!dev) {
  186. err = -EINVAL;
  187. xenbus_dev_fatal(pdev->xdev, err,
  188. "Couldn't locate PCI device "
  189. "(%04x:%02x:%02x.%d)! "
  190. "perhaps already in-use?",
  191. domain, bus, slot, func);
  192. goto out;
  193. }
  194. err = xen_pcibk_add_pci_dev(pdev, dev, devid,
  195. xen_pcibk_publish_pci_dev);
  196. if (err)
  197. goto out;
  198. dev_dbg(&dev->dev, "registering for %d\n", pdev->xdev->otherend_id);
  199. if (xen_register_device_domain_owner(dev,
  200. pdev->xdev->otherend_id) != 0) {
  201. dev_err(&dev->dev, "Stealing ownership from dom%d.\n",
  202. xen_find_device_domain_owner(dev));
  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.%d)! 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. xen_unregister_device_domain_owner(dev);
  234. xen_pcibk_release_pci_dev(pdev, dev);
  235. out:
  236. return err;
  237. }
  238. static int xen_pcibk_publish_pci_root(struct xen_pcibk_device *pdev,
  239. unsigned int domain, unsigned int bus)
  240. {
  241. unsigned int d, b;
  242. int i, root_num, len, err;
  243. char str[64];
  244. dev_dbg(&pdev->xdev->dev, "Publishing pci roots\n");
  245. err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
  246. "root_num", "%d", &root_num);
  247. if (err == 0 || err == -ENOENT)
  248. root_num = 0;
  249. else if (err < 0)
  250. goto out;
  251. /* Verify that we haven't already published this pci root */
  252. for (i = 0; i < root_num; i++) {
  253. len = snprintf(str, sizeof(str), "root-%d", i);
  254. if (unlikely(len >= (sizeof(str) - 1))) {
  255. err = -ENOMEM;
  256. goto out;
  257. }
  258. err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
  259. str, "%x:%x", &d, &b);
  260. if (err < 0)
  261. goto out;
  262. if (err != 2) {
  263. err = -EINVAL;
  264. goto out;
  265. }
  266. if (d == domain && b == bus) {
  267. err = 0;
  268. goto out;
  269. }
  270. }
  271. len = snprintf(str, sizeof(str), "root-%d", root_num);
  272. if (unlikely(len >= (sizeof(str) - 1))) {
  273. err = -ENOMEM;
  274. goto out;
  275. }
  276. dev_dbg(&pdev->xdev->dev, "writing root %d at %04x:%02x\n",
  277. root_num, domain, bus);
  278. err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
  279. "%04x:%02x", domain, bus);
  280. if (err)
  281. goto out;
  282. err = xenbus_printf(XBT_NIL, pdev->xdev->nodename,
  283. "root_num", "%d", (root_num + 1));
  284. out:
  285. return err;
  286. }
  287. static int xen_pcibk_reconfigure(struct xen_pcibk_device *pdev)
  288. {
  289. int err = 0;
  290. int num_devs;
  291. int domain, bus, slot, func;
  292. int substate;
  293. int i, len;
  294. char state_str[64];
  295. char dev_str[64];
  296. dev_dbg(&pdev->xdev->dev, "Reconfiguring device ...\n");
  297. mutex_lock(&pdev->dev_lock);
  298. /* Make sure we only reconfigure once */
  299. if (xenbus_read_driver_state(pdev->xdev->nodename) !=
  300. XenbusStateReconfiguring)
  301. goto out;
  302. err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
  303. &num_devs);
  304. if (err != 1) {
  305. if (err >= 0)
  306. err = -EINVAL;
  307. xenbus_dev_fatal(pdev->xdev, err,
  308. "Error reading number of devices");
  309. goto out;
  310. }
  311. for (i = 0; i < num_devs; i++) {
  312. len = snprintf(state_str, sizeof(state_str), "state-%d", i);
  313. if (unlikely(len >= (sizeof(state_str) - 1))) {
  314. err = -ENOMEM;
  315. xenbus_dev_fatal(pdev->xdev, err,
  316. "String overflow while reading "
  317. "configuration");
  318. goto out;
  319. }
  320. err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, state_str,
  321. "%d", &substate);
  322. if (err != 1)
  323. substate = XenbusStateUnknown;
  324. switch (substate) {
  325. case XenbusStateInitialising:
  326. dev_dbg(&pdev->xdev->dev, "Attaching dev-%d ...\n", i);
  327. len = snprintf(dev_str, sizeof(dev_str), "dev-%d", i);
  328. if (unlikely(len >= (sizeof(dev_str) - 1))) {
  329. err = -ENOMEM;
  330. xenbus_dev_fatal(pdev->xdev, err,
  331. "String overflow while "
  332. "reading configuration");
  333. goto out;
  334. }
  335. err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
  336. dev_str, "%x:%x:%x.%x",
  337. &domain, &bus, &slot, &func);
  338. if (err < 0) {
  339. xenbus_dev_fatal(pdev->xdev, err,
  340. "Error reading device "
  341. "configuration");
  342. goto out;
  343. }
  344. if (err != 4) {
  345. err = -EINVAL;
  346. xenbus_dev_fatal(pdev->xdev, err,
  347. "Error parsing pci device "
  348. "configuration");
  349. goto out;
  350. }
  351. err = xen_pcibk_export_device(pdev, domain, bus, slot,
  352. func, i);
  353. if (err)
  354. goto out;
  355. /* Publish pci roots. */
  356. err = xen_pcibk_publish_pci_roots(pdev,
  357. xen_pcibk_publish_pci_root);
  358. if (err) {
  359. xenbus_dev_fatal(pdev->xdev, err,
  360. "Error while publish PCI root"
  361. "buses for frontend");
  362. goto out;
  363. }
  364. err = xenbus_printf(XBT_NIL, pdev->xdev->nodename,
  365. state_str, "%d",
  366. XenbusStateInitialised);
  367. if (err) {
  368. xenbus_dev_fatal(pdev->xdev, err,
  369. "Error switching substate of "
  370. "dev-%d\n", i);
  371. goto out;
  372. }
  373. break;
  374. case XenbusStateClosing:
  375. dev_dbg(&pdev->xdev->dev, "Detaching dev-%d ...\n", i);
  376. len = snprintf(dev_str, sizeof(dev_str), "vdev-%d", i);
  377. if (unlikely(len >= (sizeof(dev_str) - 1))) {
  378. err = -ENOMEM;
  379. xenbus_dev_fatal(pdev->xdev, err,
  380. "String overflow while "
  381. "reading configuration");
  382. goto out;
  383. }
  384. err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
  385. dev_str, "%x:%x:%x.%x",
  386. &domain, &bus, &slot, &func);
  387. if (err < 0) {
  388. xenbus_dev_fatal(pdev->xdev, err,
  389. "Error reading device "
  390. "configuration");
  391. goto out;
  392. }
  393. if (err != 4) {
  394. err = -EINVAL;
  395. xenbus_dev_fatal(pdev->xdev, err,
  396. "Error parsing pci device "
  397. "configuration");
  398. goto out;
  399. }
  400. err = xen_pcibk_remove_device(pdev, domain, bus, slot,
  401. func);
  402. if (err)
  403. goto out;
  404. /* TODO: If at some point we implement support for pci
  405. * root hot-remove on pcifront side, we'll need to
  406. * remove unnecessary xenstore nodes of pci roots here.
  407. */
  408. break;
  409. default:
  410. break;
  411. }
  412. }
  413. err = xenbus_switch_state(pdev->xdev, XenbusStateReconfigured);
  414. if (err) {
  415. xenbus_dev_fatal(pdev->xdev, err,
  416. "Error switching to reconfigured state!");
  417. goto out;
  418. }
  419. out:
  420. mutex_unlock(&pdev->dev_lock);
  421. return 0;
  422. }
  423. static void xen_pcibk_frontend_changed(struct xenbus_device *xdev,
  424. enum xenbus_state fe_state)
  425. {
  426. struct xen_pcibk_device *pdev = dev_get_drvdata(&xdev->dev);
  427. dev_dbg(&xdev->dev, "fe state changed %d\n", fe_state);
  428. switch (fe_state) {
  429. case XenbusStateInitialised:
  430. xen_pcibk_attach(pdev);
  431. break;
  432. case XenbusStateReconfiguring:
  433. xen_pcibk_reconfigure(pdev);
  434. break;
  435. case XenbusStateConnected:
  436. /* pcifront switched its state from reconfiguring to connected.
  437. * Then switch to connected state.
  438. */
  439. xenbus_switch_state(xdev, XenbusStateConnected);
  440. break;
  441. case XenbusStateClosing:
  442. xen_pcibk_disconnect(pdev);
  443. xenbus_switch_state(xdev, XenbusStateClosing);
  444. break;
  445. case XenbusStateClosed:
  446. xen_pcibk_disconnect(pdev);
  447. xenbus_switch_state(xdev, XenbusStateClosed);
  448. if (xenbus_dev_is_online(xdev))
  449. break;
  450. /* fall through if not online */
  451. case XenbusStateUnknown:
  452. dev_dbg(&xdev->dev, "frontend is gone! unregister device\n");
  453. device_unregister(&xdev->dev);
  454. break;
  455. default:
  456. break;
  457. }
  458. }
  459. static int xen_pcibk_setup_backend(struct xen_pcibk_device *pdev)
  460. {
  461. /* Get configuration from xend (if available now) */
  462. int domain, bus, slot, func;
  463. int err = 0;
  464. int i, num_devs;
  465. char dev_str[64];
  466. char state_str[64];
  467. mutex_lock(&pdev->dev_lock);
  468. /* It's possible we could get the call to setup twice, so make sure
  469. * we're not already connected.
  470. */
  471. if (xenbus_read_driver_state(pdev->xdev->nodename) !=
  472. XenbusStateInitWait)
  473. goto out;
  474. dev_dbg(&pdev->xdev->dev, "getting be setup\n");
  475. err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
  476. &num_devs);
  477. if (err != 1) {
  478. if (err >= 0)
  479. err = -EINVAL;
  480. xenbus_dev_fatal(pdev->xdev, err,
  481. "Error reading number of devices");
  482. goto out;
  483. }
  484. for (i = 0; i < num_devs; i++) {
  485. int l = snprintf(dev_str, sizeof(dev_str), "dev-%d", i);
  486. if (unlikely(l >= (sizeof(dev_str) - 1))) {
  487. err = -ENOMEM;
  488. xenbus_dev_fatal(pdev->xdev, err,
  489. "String overflow while reading "
  490. "configuration");
  491. goto out;
  492. }
  493. err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, dev_str,
  494. "%x:%x:%x.%x", &domain, &bus, &slot, &func);
  495. if (err < 0) {
  496. xenbus_dev_fatal(pdev->xdev, err,
  497. "Error reading device configuration");
  498. goto out;
  499. }
  500. if (err != 4) {
  501. err = -EINVAL;
  502. xenbus_dev_fatal(pdev->xdev, err,
  503. "Error parsing pci device "
  504. "configuration");
  505. goto out;
  506. }
  507. err = xen_pcibk_export_device(pdev, domain, bus, slot, func, i);
  508. if (err)
  509. goto out;
  510. /* Switch substate of this device. */
  511. l = snprintf(state_str, sizeof(state_str), "state-%d", i);
  512. if (unlikely(l >= (sizeof(state_str) - 1))) {
  513. err = -ENOMEM;
  514. xenbus_dev_fatal(pdev->xdev, err,
  515. "String overflow while reading "
  516. "configuration");
  517. goto out;
  518. }
  519. err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, state_str,
  520. "%d", XenbusStateInitialised);
  521. if (err) {
  522. xenbus_dev_fatal(pdev->xdev, err, "Error switching "
  523. "substate of dev-%d\n", i);
  524. goto out;
  525. }
  526. }
  527. err = xen_pcibk_publish_pci_roots(pdev, xen_pcibk_publish_pci_root);
  528. if (err) {
  529. xenbus_dev_fatal(pdev->xdev, err,
  530. "Error while publish PCI root buses "
  531. "for frontend");
  532. goto out;
  533. }
  534. err = xenbus_switch_state(pdev->xdev, XenbusStateInitialised);
  535. if (err)
  536. xenbus_dev_fatal(pdev->xdev, err,
  537. "Error switching to initialised state!");
  538. out:
  539. mutex_unlock(&pdev->dev_lock);
  540. if (!err)
  541. /* see if pcifront is already configured (if not, we'll wait) */
  542. xen_pcibk_attach(pdev);
  543. return err;
  544. }
  545. static void xen_pcibk_be_watch(struct xenbus_watch *watch,
  546. const char **vec, unsigned int len)
  547. {
  548. struct xen_pcibk_device *pdev =
  549. container_of(watch, struct xen_pcibk_device, be_watch);
  550. switch (xenbus_read_driver_state(pdev->xdev->nodename)) {
  551. case XenbusStateInitWait:
  552. xen_pcibk_setup_backend(pdev);
  553. break;
  554. default:
  555. break;
  556. }
  557. }
  558. static int xen_pcibk_xenbus_probe(struct xenbus_device *dev,
  559. const struct xenbus_device_id *id)
  560. {
  561. int err = 0;
  562. struct xen_pcibk_device *pdev = alloc_pdev(dev);
  563. if (pdev == NULL) {
  564. err = -ENOMEM;
  565. xenbus_dev_fatal(dev, err,
  566. "Error allocating xen_pcibk_device struct");
  567. goto out;
  568. }
  569. /* wait for xend to configure us */
  570. err = xenbus_switch_state(dev, XenbusStateInitWait);
  571. if (err)
  572. goto out;
  573. /* watch the backend node for backend configuration information */
  574. err = xenbus_watch_path(dev, dev->nodename, &pdev->be_watch,
  575. xen_pcibk_be_watch);
  576. if (err)
  577. goto out;
  578. pdev->be_watching = 1;
  579. /* We need to force a call to our callback here in case
  580. * xend already configured us!
  581. */
  582. xen_pcibk_be_watch(&pdev->be_watch, NULL, 0);
  583. out:
  584. return err;
  585. }
  586. static int xen_pcibk_xenbus_remove(struct xenbus_device *dev)
  587. {
  588. struct xen_pcibk_device *pdev = dev_get_drvdata(&dev->dev);
  589. if (pdev != NULL)
  590. free_pdev(pdev);
  591. return 0;
  592. }
  593. static const struct xenbus_device_id xen_pcibk_ids[] = {
  594. {"pci"},
  595. {""},
  596. };
  597. static DEFINE_XENBUS_DRIVER(xen_pcibk, DRV_NAME,
  598. .probe = xen_pcibk_xenbus_probe,
  599. .remove = xen_pcibk_xenbus_remove,
  600. .otherend_changed = xen_pcibk_frontend_changed,
  601. );
  602. const struct xen_pcibk_backend *__read_mostly xen_pcibk_backend;
  603. int __init xen_pcibk_xenbus_register(void)
  604. {
  605. xen_pcibk_wq = create_workqueue("xen_pciback_workqueue");
  606. if (!xen_pcibk_wq) {
  607. printk(KERN_ERR "%s: create"
  608. "xen_pciback_workqueue failed\n", __func__);
  609. return -EFAULT;
  610. }
  611. xen_pcibk_backend = &xen_pcibk_vpci_backend;
  612. if (passthrough)
  613. xen_pcibk_backend = &xen_pcibk_passthrough_backend;
  614. pr_info(DRV_NAME ": 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. destroy_workqueue(xen_pcibk_wq);
  620. xenbus_unregister_driver(&xen_pcibk_driver);
  621. }