xenbus.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748
  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 DRV_NAME "xen-pciback"
  16. #define INVALID_EVTCHN_IRQ (-1)
  17. struct workqueue_struct *xen_pcibk_wq;
  18. static int __read_mostly passthrough;
  19. module_param(passthrough, bool, S_IRUGO);
  20. MODULE_PARM_DESC(passthrough,
  21. "Option to specify how to export PCI topology to guest:\n"\
  22. " 0 - (default) Hide the true PCI topology and makes the frontend\n"\
  23. " there is a single PCI bus with only the exported devices on it.\n"\
  24. " For example, a device at 03:05.0 will be re-assigned to 00:00.0\n"\
  25. " while second device at 02:1a.1 will be re-assigned to 00:01.1.\n"\
  26. " 1 - Passthrough provides a real view of the PCI topology to the\n"\
  27. " frontend (for example, a device at 06:01.b will still appear at\n"\
  28. " 06:01.b to the frontend). This is similar to how Xen 2.0.x\n"\
  29. " exposed PCI devices to its driver domains. This may be required\n"\
  30. " for drivers which depend on finding their hardward in certain\n"\
  31. " bus/slot locations.");
  32. static struct xen_pcibk_device *alloc_pdev(struct xenbus_device *xdev)
  33. {
  34. struct xen_pcibk_device *pdev;
  35. pdev = kzalloc(sizeof(struct xen_pcibk_device), GFP_KERNEL);
  36. if (pdev == NULL)
  37. goto out;
  38. dev_dbg(&xdev->dev, "allocated pdev @ 0x%p\n", pdev);
  39. pdev->xdev = xdev;
  40. dev_set_drvdata(&xdev->dev, pdev);
  41. spin_lock_init(&pdev->dev_lock);
  42. pdev->sh_info = NULL;
  43. pdev->evtchn_irq = INVALID_EVTCHN_IRQ;
  44. pdev->be_watching = 0;
  45. INIT_WORK(&pdev->op_work, xen_pcibk_do_op);
  46. if (xen_pcibk_init_devices(pdev)) {
  47. kfree(pdev);
  48. pdev = NULL;
  49. }
  50. out:
  51. return pdev;
  52. }
  53. static void xen_pcibk_disconnect(struct xen_pcibk_device *pdev)
  54. {
  55. spin_lock(&pdev->dev_lock);
  56. /* Ensure the guest can't trigger our handler before removing devices */
  57. if (pdev->evtchn_irq != INVALID_EVTCHN_IRQ) {
  58. unbind_from_irqhandler(pdev->evtchn_irq, pdev);
  59. pdev->evtchn_irq = INVALID_EVTCHN_IRQ;
  60. }
  61. spin_unlock(&pdev->dev_lock);
  62. /* If the driver domain started an op, make sure we complete it
  63. * before releasing the shared memory */
  64. /* Note, the workqueue does not use spinlocks at all.*/
  65. flush_workqueue(xen_pcibk_wq);
  66. spin_lock(&pdev->dev_lock);
  67. if (pdev->sh_info != NULL) {
  68. xenbus_unmap_ring_vfree(pdev->xdev, pdev->sh_info);
  69. pdev->sh_info = NULL;
  70. }
  71. spin_unlock(&pdev->dev_lock);
  72. }
  73. static void free_pdev(struct xen_pcibk_device *pdev)
  74. {
  75. if (pdev->be_watching) {
  76. unregister_xenbus_watch(&pdev->be_watch);
  77. pdev->be_watching = 0;
  78. }
  79. xen_pcibk_disconnect(pdev);
  80. xen_pcibk_release_devices(pdev);
  81. dev_set_drvdata(&pdev->xdev->dev, NULL);
  82. pdev->xdev = NULL;
  83. kfree(pdev);
  84. }
  85. static int xen_pcibk_do_attach(struct xen_pcibk_device *pdev, int gnt_ref,
  86. int remote_evtchn)
  87. {
  88. int err = 0;
  89. void *vaddr;
  90. dev_dbg(&pdev->xdev->dev,
  91. "Attaching to frontend resources - gnt_ref=%d evtchn=%d\n",
  92. gnt_ref, remote_evtchn);
  93. err = xenbus_map_ring_valloc(pdev->xdev, gnt_ref, &vaddr);
  94. if (err < 0) {
  95. xenbus_dev_fatal(pdev->xdev, err,
  96. "Error mapping other domain page in ours.");
  97. goto out;
  98. }
  99. spin_lock(&pdev->dev_lock);
  100. pdev->sh_info = vaddr;
  101. spin_unlock(&pdev->dev_lock);
  102. err = bind_interdomain_evtchn_to_irqhandler(
  103. pdev->xdev->otherend_id, remote_evtchn, xen_pcibk_handle_event,
  104. 0, DRV_NAME, pdev);
  105. if (err < 0) {
  106. xenbus_dev_fatal(pdev->xdev, err,
  107. "Error binding event channel to IRQ");
  108. goto out;
  109. }
  110. spin_lock(&pdev->dev_lock);
  111. pdev->evtchn_irq = err;
  112. spin_unlock(&pdev->dev_lock);
  113. err = 0;
  114. dev_dbg(&pdev->xdev->dev, "Attached!\n");
  115. out:
  116. return err;
  117. }
  118. static int xen_pcibk_attach(struct xen_pcibk_device *pdev)
  119. {
  120. int err = 0;
  121. int gnt_ref, remote_evtchn;
  122. char *magic = NULL;
  123. /* Make sure we only do this setup once */
  124. if (xenbus_read_driver_state(pdev->xdev->nodename) !=
  125. XenbusStateInitialised)
  126. goto out;
  127. /* Wait for frontend to state that it has published the configuration */
  128. if (xenbus_read_driver_state(pdev->xdev->otherend) !=
  129. XenbusStateInitialised)
  130. goto out;
  131. dev_dbg(&pdev->xdev->dev, "Reading frontend config\n");
  132. err = xenbus_gather(XBT_NIL, pdev->xdev->otherend,
  133. "pci-op-ref", "%u", &gnt_ref,
  134. "event-channel", "%u", &remote_evtchn,
  135. "magic", NULL, &magic, NULL);
  136. if (err) {
  137. /* If configuration didn't get read correctly, wait longer */
  138. xenbus_dev_fatal(pdev->xdev, err,
  139. "Error reading configuration from frontend");
  140. goto out;
  141. }
  142. if (magic == NULL || strcmp(magic, XEN_PCI_MAGIC) != 0) {
  143. xenbus_dev_fatal(pdev->xdev, -EFAULT,
  144. "version mismatch (%s/%s) with pcifront - "
  145. "halting xen_pcibk",
  146. magic, XEN_PCI_MAGIC);
  147. goto out;
  148. }
  149. err = xen_pcibk_do_attach(pdev, gnt_ref, remote_evtchn);
  150. if (err)
  151. goto out;
  152. dev_dbg(&pdev->xdev->dev, "Connecting...\n");
  153. err = xenbus_switch_state(pdev->xdev, XenbusStateConnected);
  154. if (err)
  155. xenbus_dev_fatal(pdev->xdev, err,
  156. "Error switching to connected state!");
  157. dev_dbg(&pdev->xdev->dev, "Connected? %d\n", err);
  158. out:
  159. kfree(magic);
  160. return err;
  161. }
  162. static int xen_pcibk_publish_pci_dev(struct xen_pcibk_device *pdev,
  163. unsigned int domain, unsigned int bus,
  164. unsigned int devfn, unsigned int devid)
  165. {
  166. int err;
  167. int len;
  168. char str[64];
  169. len = snprintf(str, sizeof(str), "vdev-%d", devid);
  170. if (unlikely(len >= (sizeof(str) - 1))) {
  171. err = -ENOMEM;
  172. goto out;
  173. }
  174. err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
  175. "%04x:%02x:%02x.%02x", domain, bus,
  176. PCI_SLOT(devfn), PCI_FUNC(devfn));
  177. out:
  178. return err;
  179. }
  180. static int xen_pcibk_export_device(struct xen_pcibk_device *pdev,
  181. int domain, int bus, int slot, int func,
  182. int devid)
  183. {
  184. struct pci_dev *dev;
  185. int err = 0;
  186. dev_dbg(&pdev->xdev->dev, "exporting dom %x bus %x slot %x func %x\n",
  187. domain, bus, slot, func);
  188. dev = pcistub_get_pci_dev_by_slot(pdev, domain, bus, slot, func);
  189. if (!dev) {
  190. err = -EINVAL;
  191. xenbus_dev_fatal(pdev->xdev, err,
  192. "Couldn't locate PCI device "
  193. "(%04x:%02x:%02x.%01x)! "
  194. "perhaps already in-use?",
  195. domain, bus, slot, func);
  196. goto out;
  197. }
  198. err = xen_pcibk_add_pci_dev(pdev, dev, devid,
  199. xen_pcibk_publish_pci_dev);
  200. if (err)
  201. goto out;
  202. dev_dbg(&dev->dev, "registering for %d\n", pdev->xdev->otherend_id);
  203. if (xen_register_device_domain_owner(dev,
  204. pdev->xdev->otherend_id) != 0) {
  205. dev_err(&dev->dev, "device has been assigned to another " \
  206. "domain! Over-writting the ownership, but beware.\n");
  207. xen_unregister_device_domain_owner(dev);
  208. xen_register_device_domain_owner(dev, pdev->xdev->otherend_id);
  209. }
  210. /* TODO: It'd be nice to export a bridge and have all of its children
  211. * get exported with it. This may be best done in xend (which will
  212. * have to calculate resource usage anyway) but we probably want to
  213. * put something in here to ensure that if a bridge gets given to a
  214. * driver domain, that all devices under that bridge are not given
  215. * to other driver domains (as he who controls the bridge can disable
  216. * it and stop the other devices from working).
  217. */
  218. out:
  219. return err;
  220. }
  221. static int xen_pcibk_remove_device(struct xen_pcibk_device *pdev,
  222. int domain, int bus, int slot, int func)
  223. {
  224. int err = 0;
  225. struct pci_dev *dev;
  226. dev_dbg(&pdev->xdev->dev, "removing dom %x bus %x slot %x func %x\n",
  227. domain, bus, slot, func);
  228. dev = xen_pcibk_get_pci_dev(pdev, domain, bus, PCI_DEVFN(slot, func));
  229. if (!dev) {
  230. err = -EINVAL;
  231. dev_dbg(&pdev->xdev->dev, "Couldn't locate PCI device "
  232. "(%04x:%02x:%02x.%01x)! not owned by this domain\n",
  233. domain, bus, slot, func);
  234. goto out;
  235. }
  236. dev_dbg(&dev->dev, "unregistering for %d\n", pdev->xdev->otherend_id);
  237. xen_unregister_device_domain_owner(dev);
  238. xen_pcibk_release_pci_dev(pdev, dev);
  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. /* Make sure we only reconfigure once */
  302. if (xenbus_read_driver_state(pdev->xdev->nodename) !=
  303. XenbusStateReconfiguring)
  304. goto out;
  305. err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
  306. &num_devs);
  307. if (err != 1) {
  308. if (err >= 0)
  309. err = -EINVAL;
  310. xenbus_dev_fatal(pdev->xdev, err,
  311. "Error reading number of devices");
  312. goto out;
  313. }
  314. for (i = 0; i < num_devs; i++) {
  315. len = snprintf(state_str, sizeof(state_str), "state-%d", i);
  316. if (unlikely(len >= (sizeof(state_str) - 1))) {
  317. err = -ENOMEM;
  318. xenbus_dev_fatal(pdev->xdev, err,
  319. "String overflow while reading "
  320. "configuration");
  321. goto out;
  322. }
  323. err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, state_str,
  324. "%d", &substate);
  325. if (err != 1)
  326. substate = XenbusStateUnknown;
  327. switch (substate) {
  328. case XenbusStateInitialising:
  329. dev_dbg(&pdev->xdev->dev, "Attaching dev-%d ...\n", i);
  330. len = snprintf(dev_str, sizeof(dev_str), "dev-%d", i);
  331. if (unlikely(len >= (sizeof(dev_str) - 1))) {
  332. err = -ENOMEM;
  333. xenbus_dev_fatal(pdev->xdev, err,
  334. "String overflow while "
  335. "reading configuration");
  336. goto out;
  337. }
  338. err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
  339. dev_str, "%x:%x:%x.%x",
  340. &domain, &bus, &slot, &func);
  341. if (err < 0) {
  342. xenbus_dev_fatal(pdev->xdev, err,
  343. "Error reading device "
  344. "configuration");
  345. goto out;
  346. }
  347. if (err != 4) {
  348. err = -EINVAL;
  349. xenbus_dev_fatal(pdev->xdev, err,
  350. "Error parsing pci device "
  351. "configuration");
  352. goto out;
  353. }
  354. err = xen_pcibk_export_device(pdev, domain, bus, slot,
  355. func, i);
  356. if (err)
  357. goto out;
  358. /* Publish pci roots. */
  359. err = xen_pcibk_publish_pci_roots(pdev,
  360. xen_pcibk_publish_pci_root);
  361. if (err) {
  362. xenbus_dev_fatal(pdev->xdev, err,
  363. "Error while publish PCI root"
  364. "buses for frontend");
  365. goto out;
  366. }
  367. err = xenbus_printf(XBT_NIL, pdev->xdev->nodename,
  368. state_str, "%d",
  369. XenbusStateInitialised);
  370. if (err) {
  371. xenbus_dev_fatal(pdev->xdev, err,
  372. "Error switching substate of "
  373. "dev-%d\n", i);
  374. goto out;
  375. }
  376. break;
  377. case XenbusStateClosing:
  378. dev_dbg(&pdev->xdev->dev, "Detaching dev-%d ...\n", i);
  379. len = snprintf(dev_str, sizeof(dev_str), "vdev-%d", i);
  380. if (unlikely(len >= (sizeof(dev_str) - 1))) {
  381. err = -ENOMEM;
  382. xenbus_dev_fatal(pdev->xdev, err,
  383. "String overflow while "
  384. "reading configuration");
  385. goto out;
  386. }
  387. err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
  388. dev_str, "%x:%x:%x.%x",
  389. &domain, &bus, &slot, &func);
  390. if (err < 0) {
  391. xenbus_dev_fatal(pdev->xdev, err,
  392. "Error reading device "
  393. "configuration");
  394. goto out;
  395. }
  396. if (err != 4) {
  397. err = -EINVAL;
  398. xenbus_dev_fatal(pdev->xdev, err,
  399. "Error parsing pci device "
  400. "configuration");
  401. goto out;
  402. }
  403. err = xen_pcibk_remove_device(pdev, domain, bus, slot,
  404. func);
  405. if (err)
  406. goto out;
  407. /* TODO: If at some point we implement support for pci
  408. * root hot-remove on pcifront side, we'll need to
  409. * remove unnecessary xenstore nodes of pci roots here.
  410. */
  411. break;
  412. default:
  413. break;
  414. }
  415. }
  416. err = xenbus_switch_state(pdev->xdev, XenbusStateReconfigured);
  417. if (err) {
  418. xenbus_dev_fatal(pdev->xdev, err,
  419. "Error switching to reconfigured state!");
  420. goto out;
  421. }
  422. out:
  423. return 0;
  424. }
  425. static void xen_pcibk_frontend_changed(struct xenbus_device *xdev,
  426. enum xenbus_state fe_state)
  427. {
  428. struct xen_pcibk_device *pdev = dev_get_drvdata(&xdev->dev);
  429. dev_dbg(&xdev->dev, "fe state changed %d\n", fe_state);
  430. switch (fe_state) {
  431. case XenbusStateInitialised:
  432. xen_pcibk_attach(pdev);
  433. break;
  434. case XenbusStateReconfiguring:
  435. xen_pcibk_reconfigure(pdev);
  436. break;
  437. case XenbusStateConnected:
  438. /* pcifront switched its state from reconfiguring to connected.
  439. * Then switch to connected state.
  440. */
  441. xenbus_switch_state(xdev, XenbusStateConnected);
  442. break;
  443. case XenbusStateClosing:
  444. xen_pcibk_disconnect(pdev);
  445. xenbus_switch_state(xdev, XenbusStateClosing);
  446. break;
  447. case XenbusStateClosed:
  448. xen_pcibk_disconnect(pdev);
  449. xenbus_switch_state(xdev, XenbusStateClosed);
  450. if (xenbus_dev_is_online(xdev))
  451. break;
  452. /* fall through if not online */
  453. case XenbusStateUnknown:
  454. dev_dbg(&xdev->dev, "frontend is gone! unregister device\n");
  455. device_unregister(&xdev->dev);
  456. break;
  457. default:
  458. break;
  459. }
  460. }
  461. static int xen_pcibk_setup_backend(struct xen_pcibk_device *pdev)
  462. {
  463. /* Get configuration from xend (if available now) */
  464. int domain, bus, slot, func;
  465. int err = 0;
  466. int i, num_devs;
  467. char dev_str[64];
  468. char state_str[64];
  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. 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 xenpci_ids[] = {
  594. {"pci"},
  595. {""},
  596. };
  597. static struct xenbus_driver xenbus_xen_pcibk_driver = {
  598. .name = DRV_NAME,
  599. .owner = THIS_MODULE,
  600. .ids = xenpci_ids,
  601. .probe = xen_pcibk_xenbus_probe,
  602. .remove = xen_pcibk_xenbus_remove,
  603. .otherend_changed = xen_pcibk_frontend_changed,
  604. };
  605. struct xen_pcibk_backend *xen_pcibk_backend;
  606. int __init xen_pcibk_xenbus_register(void)
  607. {
  608. xen_pcibk_wq = create_workqueue("xen_pciback_workqueue");
  609. if (!xen_pcibk_wq) {
  610. printk(KERN_ERR "%s: create"
  611. "xen_pciback_workqueue failed\n", __func__);
  612. return -EFAULT;
  613. }
  614. xen_pcibk_backend = &xen_pcibk_vpci_backend;
  615. if (passthrough)
  616. xen_pcibk_backend = &xen_pcibk_passthrough_backend;
  617. pr_info(DRV_NAME ": backend is %s\n", xen_pcibk_backend->name);
  618. return xenbus_register_backend(&xenbus_xen_pcibk_driver);
  619. }
  620. void __exit xen_pcibk_xenbus_unregister(void)
  621. {
  622. destroy_workqueue(xen_pcibk_wq);
  623. xenbus_unregister_driver(&xenbus_xen_pcibk_driver);
  624. }