xenbus.c 18 KB

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