xenbus.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749
  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 <linux/workqueue.h>
  15. #include "pciback.h"
  16. #define DRV_NAME "xen-pciback"
  17. #define INVALID_EVTCHN_IRQ (-1)
  18. struct workqueue_struct *xen_pcibk_wq;
  19. static int __read_mostly passthrough;
  20. module_param(passthrough, bool, S_IRUGO);
  21. MODULE_PARM_DESC(passthrough,
  22. "Option to specify how to export PCI topology to guest:\n"\
  23. " 0 - (default) Hide the true PCI topology and makes the frontend\n"\
  24. " there is a single PCI bus with only the exported devices on it.\n"\
  25. " For example, a device at 03:05.0 will be re-assigned to 00:00.0\n"\
  26. " while second device at 02:1a.1 will be re-assigned to 00:01.1.\n"\
  27. " 1 - Passthrough provides a real view of the PCI topology to the\n"\
  28. " frontend (for example, a device at 06:01.b will still appear at\n"\
  29. " 06:01.b to the frontend). This is similar to how Xen 2.0.x\n"\
  30. " exposed PCI devices to its driver domains. This may be required\n"\
  31. " for drivers which depend on finding their hardward in certain\n"\
  32. " bus/slot locations.");
  33. static struct xen_pcibk_device *alloc_pdev(struct xenbus_device *xdev)
  34. {
  35. struct xen_pcibk_device *pdev;
  36. pdev = kzalloc(sizeof(struct xen_pcibk_device), GFP_KERNEL);
  37. if (pdev == NULL)
  38. goto out;
  39. dev_dbg(&xdev->dev, "allocated pdev @ 0x%p\n", pdev);
  40. pdev->xdev = xdev;
  41. dev_set_drvdata(&xdev->dev, pdev);
  42. spin_lock_init(&pdev->dev_lock);
  43. pdev->sh_info = NULL;
  44. pdev->evtchn_irq = INVALID_EVTCHN_IRQ;
  45. pdev->be_watching = 0;
  46. INIT_WORK(&pdev->op_work, xen_pcibk_do_op);
  47. if (xen_pcibk_init_devices(pdev)) {
  48. kfree(pdev);
  49. pdev = NULL;
  50. }
  51. out:
  52. return pdev;
  53. }
  54. static void xen_pcibk_disconnect(struct xen_pcibk_device *pdev)
  55. {
  56. spin_lock(&pdev->dev_lock);
  57. /* Ensure the guest can't trigger our handler before removing devices */
  58. if (pdev->evtchn_irq != INVALID_EVTCHN_IRQ) {
  59. unbind_from_irqhandler(pdev->evtchn_irq, pdev);
  60. pdev->evtchn_irq = INVALID_EVTCHN_IRQ;
  61. }
  62. spin_unlock(&pdev->dev_lock);
  63. /* If the driver domain started an op, make sure we complete it
  64. * before releasing the shared memory */
  65. /* Note, the workqueue does not use spinlocks at all.*/
  66. flush_workqueue(xen_pcibk_wq);
  67. spin_lock(&pdev->dev_lock);
  68. if (pdev->sh_info != NULL) {
  69. xenbus_unmap_ring_vfree(pdev->xdev, pdev->sh_info);
  70. pdev->sh_info = NULL;
  71. }
  72. spin_unlock(&pdev->dev_lock);
  73. }
  74. static void free_pdev(struct xen_pcibk_device *pdev)
  75. {
  76. if (pdev->be_watching) {
  77. unregister_xenbus_watch(&pdev->be_watch);
  78. pdev->be_watching = 0;
  79. }
  80. xen_pcibk_disconnect(pdev);
  81. xen_pcibk_release_devices(pdev);
  82. dev_set_drvdata(&pdev->xdev->dev, NULL);
  83. pdev->xdev = NULL;
  84. kfree(pdev);
  85. }
  86. static int xen_pcibk_do_attach(struct xen_pcibk_device *pdev, int gnt_ref,
  87. int remote_evtchn)
  88. {
  89. int err = 0;
  90. void *vaddr;
  91. dev_dbg(&pdev->xdev->dev,
  92. "Attaching to frontend resources - gnt_ref=%d evtchn=%d\n",
  93. gnt_ref, remote_evtchn);
  94. err = xenbus_map_ring_valloc(pdev->xdev, gnt_ref, &vaddr);
  95. if (err < 0) {
  96. xenbus_dev_fatal(pdev->xdev, err,
  97. "Error mapping other domain page in ours.");
  98. goto out;
  99. }
  100. spin_lock(&pdev->dev_lock);
  101. pdev->sh_info = vaddr;
  102. spin_unlock(&pdev->dev_lock);
  103. err = bind_interdomain_evtchn_to_irqhandler(
  104. pdev->xdev->otherend_id, remote_evtchn, xen_pcibk_handle_event,
  105. 0, DRV_NAME, pdev);
  106. if (err < 0) {
  107. xenbus_dev_fatal(pdev->xdev, err,
  108. "Error binding event channel to IRQ");
  109. goto out;
  110. }
  111. spin_lock(&pdev->dev_lock);
  112. pdev->evtchn_irq = err;
  113. spin_unlock(&pdev->dev_lock);
  114. err = 0;
  115. dev_dbg(&pdev->xdev->dev, "Attached!\n");
  116. out:
  117. return err;
  118. }
  119. static int xen_pcibk_attach(struct xen_pcibk_device *pdev)
  120. {
  121. int err = 0;
  122. int gnt_ref, remote_evtchn;
  123. char *magic = NULL;
  124. /* Make sure we only do this setup once */
  125. if (xenbus_read_driver_state(pdev->xdev->nodename) !=
  126. XenbusStateInitialised)
  127. goto out;
  128. /* Wait for frontend to state that it has published the configuration */
  129. if (xenbus_read_driver_state(pdev->xdev->otherend) !=
  130. XenbusStateInitialised)
  131. goto out;
  132. dev_dbg(&pdev->xdev->dev, "Reading frontend config\n");
  133. err = xenbus_gather(XBT_NIL, pdev->xdev->otherend,
  134. "pci-op-ref", "%u", &gnt_ref,
  135. "event-channel", "%u", &remote_evtchn,
  136. "magic", NULL, &magic, NULL);
  137. if (err) {
  138. /* If configuration didn't get read correctly, wait longer */
  139. xenbus_dev_fatal(pdev->xdev, err,
  140. "Error reading configuration from frontend");
  141. goto out;
  142. }
  143. if (magic == NULL || strcmp(magic, XEN_PCI_MAGIC) != 0) {
  144. xenbus_dev_fatal(pdev->xdev, -EFAULT,
  145. "version mismatch (%s/%s) with pcifront - "
  146. "halting xen_pcibk",
  147. magic, XEN_PCI_MAGIC);
  148. goto out;
  149. }
  150. err = xen_pcibk_do_attach(pdev, gnt_ref, remote_evtchn);
  151. if (err)
  152. goto out;
  153. dev_dbg(&pdev->xdev->dev, "Connecting...\n");
  154. err = xenbus_switch_state(pdev->xdev, XenbusStateConnected);
  155. if (err)
  156. xenbus_dev_fatal(pdev->xdev, err,
  157. "Error switching to connected state!");
  158. dev_dbg(&pdev->xdev->dev, "Connected? %d\n", err);
  159. out:
  160. kfree(magic);
  161. return err;
  162. }
  163. static int xen_pcibk_publish_pci_dev(struct xen_pcibk_device *pdev,
  164. unsigned int domain, unsigned int bus,
  165. unsigned int devfn, unsigned int devid)
  166. {
  167. int err;
  168. int len;
  169. char str[64];
  170. len = snprintf(str, sizeof(str), "vdev-%d", devid);
  171. if (unlikely(len >= (sizeof(str) - 1))) {
  172. err = -ENOMEM;
  173. goto out;
  174. }
  175. err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
  176. "%04x:%02x:%02x.%02x", domain, bus,
  177. PCI_SLOT(devfn), PCI_FUNC(devfn));
  178. out:
  179. return err;
  180. }
  181. static int xen_pcibk_export_device(struct xen_pcibk_device *pdev,
  182. int domain, int bus, int slot, int func,
  183. int devid)
  184. {
  185. struct pci_dev *dev;
  186. int err = 0;
  187. dev_dbg(&pdev->xdev->dev, "exporting dom %x bus %x slot %x func %x\n",
  188. domain, bus, slot, func);
  189. dev = pcistub_get_pci_dev_by_slot(pdev, domain, bus, slot, func);
  190. if (!dev) {
  191. err = -EINVAL;
  192. xenbus_dev_fatal(pdev->xdev, err,
  193. "Couldn't locate PCI device "
  194. "(%04x:%02x:%02x.%01x)! "
  195. "perhaps already in-use?",
  196. domain, bus, slot, func);
  197. goto out;
  198. }
  199. err = xen_pcibk_add_pci_dev(pdev, dev, devid,
  200. xen_pcibk_publish_pci_dev);
  201. if (err)
  202. goto out;
  203. dev_dbg(&dev->dev, "registering for %d\n", pdev->xdev->otherend_id);
  204. if (xen_register_device_domain_owner(dev,
  205. pdev->xdev->otherend_id) != 0) {
  206. dev_err(&dev->dev, "device has been assigned to another " \
  207. "domain! Over-writting the ownership, but beware.\n");
  208. xen_unregister_device_domain_owner(dev);
  209. xen_register_device_domain_owner(dev, pdev->xdev->otherend_id);
  210. }
  211. /* TODO: It'd be nice to export a bridge and have all of its children
  212. * get exported with it. This may be best done in xend (which will
  213. * have to calculate resource usage anyway) but we probably want to
  214. * put something in here to ensure that if a bridge gets given to a
  215. * driver domain, that all devices under that bridge are not given
  216. * to other driver domains (as he who controls the bridge can disable
  217. * it and stop the other devices from working).
  218. */
  219. out:
  220. return err;
  221. }
  222. static int xen_pcibk_remove_device(struct xen_pcibk_device *pdev,
  223. int domain, int bus, int slot, int func)
  224. {
  225. int err = 0;
  226. struct pci_dev *dev;
  227. dev_dbg(&pdev->xdev->dev, "removing dom %x bus %x slot %x func %x\n",
  228. domain, bus, slot, func);
  229. dev = xen_pcibk_get_pci_dev(pdev, domain, bus, PCI_DEVFN(slot, func));
  230. if (!dev) {
  231. err = -EINVAL;
  232. dev_dbg(&pdev->xdev->dev, "Couldn't locate PCI device "
  233. "(%04x:%02x:%02x.%01x)! not owned by this domain\n",
  234. domain, bus, slot, func);
  235. goto out;
  236. }
  237. dev_dbg(&dev->dev, "unregistering for %d\n", pdev->xdev->otherend_id);
  238. xen_unregister_device_domain_owner(dev);
  239. xen_pcibk_release_pci_dev(pdev, dev);
  240. out:
  241. return err;
  242. }
  243. static int xen_pcibk_publish_pci_root(struct xen_pcibk_device *pdev,
  244. unsigned int domain, unsigned int bus)
  245. {
  246. unsigned int d, b;
  247. int i, root_num, len, err;
  248. char str[64];
  249. dev_dbg(&pdev->xdev->dev, "Publishing pci roots\n");
  250. err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
  251. "root_num", "%d", &root_num);
  252. if (err == 0 || err == -ENOENT)
  253. root_num = 0;
  254. else if (err < 0)
  255. goto out;
  256. /* Verify that we haven't already published this pci root */
  257. for (i = 0; i < root_num; i++) {
  258. len = snprintf(str, sizeof(str), "root-%d", i);
  259. if (unlikely(len >= (sizeof(str) - 1))) {
  260. err = -ENOMEM;
  261. goto out;
  262. }
  263. err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
  264. str, "%x:%x", &d, &b);
  265. if (err < 0)
  266. goto out;
  267. if (err != 2) {
  268. err = -EINVAL;
  269. goto out;
  270. }
  271. if (d == domain && b == bus) {
  272. err = 0;
  273. goto out;
  274. }
  275. }
  276. len = snprintf(str, sizeof(str), "root-%d", root_num);
  277. if (unlikely(len >= (sizeof(str) - 1))) {
  278. err = -ENOMEM;
  279. goto out;
  280. }
  281. dev_dbg(&pdev->xdev->dev, "writing root %d at %04x:%02x\n",
  282. root_num, domain, bus);
  283. err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, str,
  284. "%04x:%02x", domain, bus);
  285. if (err)
  286. goto out;
  287. err = xenbus_printf(XBT_NIL, pdev->xdev->nodename,
  288. "root_num", "%d", (root_num + 1));
  289. out:
  290. return err;
  291. }
  292. static int xen_pcibk_reconfigure(struct xen_pcibk_device *pdev)
  293. {
  294. int err = 0;
  295. int num_devs;
  296. int domain, bus, slot, func;
  297. int substate;
  298. int i, len;
  299. char state_str[64];
  300. char dev_str[64];
  301. dev_dbg(&pdev->xdev->dev, "Reconfiguring device ...\n");
  302. /* Make sure we only reconfigure once */
  303. if (xenbus_read_driver_state(pdev->xdev->nodename) !=
  304. XenbusStateReconfiguring)
  305. goto out;
  306. err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
  307. &num_devs);
  308. if (err != 1) {
  309. if (err >= 0)
  310. err = -EINVAL;
  311. xenbus_dev_fatal(pdev->xdev, err,
  312. "Error reading number of devices");
  313. goto out;
  314. }
  315. for (i = 0; i < num_devs; i++) {
  316. len = snprintf(state_str, sizeof(state_str), "state-%d", i);
  317. if (unlikely(len >= (sizeof(state_str) - 1))) {
  318. err = -ENOMEM;
  319. xenbus_dev_fatal(pdev->xdev, err,
  320. "String overflow while reading "
  321. "configuration");
  322. goto out;
  323. }
  324. err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, state_str,
  325. "%d", &substate);
  326. if (err != 1)
  327. substate = XenbusStateUnknown;
  328. switch (substate) {
  329. case XenbusStateInitialising:
  330. dev_dbg(&pdev->xdev->dev, "Attaching dev-%d ...\n", i);
  331. len = snprintf(dev_str, sizeof(dev_str), "dev-%d", i);
  332. if (unlikely(len >= (sizeof(dev_str) - 1))) {
  333. err = -ENOMEM;
  334. xenbus_dev_fatal(pdev->xdev, err,
  335. "String overflow while "
  336. "reading configuration");
  337. goto out;
  338. }
  339. err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
  340. dev_str, "%x:%x:%x.%x",
  341. &domain, &bus, &slot, &func);
  342. if (err < 0) {
  343. xenbus_dev_fatal(pdev->xdev, err,
  344. "Error reading device "
  345. "configuration");
  346. goto out;
  347. }
  348. if (err != 4) {
  349. err = -EINVAL;
  350. xenbus_dev_fatal(pdev->xdev, err,
  351. "Error parsing pci device "
  352. "configuration");
  353. goto out;
  354. }
  355. err = xen_pcibk_export_device(pdev, domain, bus, slot,
  356. func, i);
  357. if (err)
  358. goto out;
  359. /* Publish pci roots. */
  360. err = xen_pcibk_publish_pci_roots(pdev,
  361. xen_pcibk_publish_pci_root);
  362. if (err) {
  363. xenbus_dev_fatal(pdev->xdev, err,
  364. "Error while publish PCI root"
  365. "buses for frontend");
  366. goto out;
  367. }
  368. err = xenbus_printf(XBT_NIL, pdev->xdev->nodename,
  369. state_str, "%d",
  370. XenbusStateInitialised);
  371. if (err) {
  372. xenbus_dev_fatal(pdev->xdev, err,
  373. "Error switching substate of "
  374. "dev-%d\n", i);
  375. goto out;
  376. }
  377. break;
  378. case XenbusStateClosing:
  379. dev_dbg(&pdev->xdev->dev, "Detaching dev-%d ...\n", i);
  380. len = snprintf(dev_str, sizeof(dev_str), "vdev-%d", i);
  381. if (unlikely(len >= (sizeof(dev_str) - 1))) {
  382. err = -ENOMEM;
  383. xenbus_dev_fatal(pdev->xdev, err,
  384. "String overflow while "
  385. "reading configuration");
  386. goto out;
  387. }
  388. err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename,
  389. dev_str, "%x:%x:%x.%x",
  390. &domain, &bus, &slot, &func);
  391. if (err < 0) {
  392. xenbus_dev_fatal(pdev->xdev, err,
  393. "Error reading device "
  394. "configuration");
  395. goto out;
  396. }
  397. if (err != 4) {
  398. err = -EINVAL;
  399. xenbus_dev_fatal(pdev->xdev, err,
  400. "Error parsing pci device "
  401. "configuration");
  402. goto out;
  403. }
  404. err = xen_pcibk_remove_device(pdev, domain, bus, slot,
  405. func);
  406. if (err)
  407. goto out;
  408. /* TODO: If at some point we implement support for pci
  409. * root hot-remove on pcifront side, we'll need to
  410. * remove unnecessary xenstore nodes of pci roots here.
  411. */
  412. break;
  413. default:
  414. break;
  415. }
  416. }
  417. err = xenbus_switch_state(pdev->xdev, XenbusStateReconfigured);
  418. if (err) {
  419. xenbus_dev_fatal(pdev->xdev, err,
  420. "Error switching to reconfigured state!");
  421. goto out;
  422. }
  423. out:
  424. return 0;
  425. }
  426. static void xen_pcibk_frontend_changed(struct xenbus_device *xdev,
  427. enum xenbus_state fe_state)
  428. {
  429. struct xen_pcibk_device *pdev = dev_get_drvdata(&xdev->dev);
  430. dev_dbg(&xdev->dev, "fe state changed %d\n", fe_state);
  431. switch (fe_state) {
  432. case XenbusStateInitialised:
  433. xen_pcibk_attach(pdev);
  434. break;
  435. case XenbusStateReconfiguring:
  436. xen_pcibk_reconfigure(pdev);
  437. break;
  438. case XenbusStateConnected:
  439. /* pcifront switched its state from reconfiguring to connected.
  440. * Then switch to connected state.
  441. */
  442. xenbus_switch_state(xdev, XenbusStateConnected);
  443. break;
  444. case XenbusStateClosing:
  445. xen_pcibk_disconnect(pdev);
  446. xenbus_switch_state(xdev, XenbusStateClosing);
  447. break;
  448. case XenbusStateClosed:
  449. xen_pcibk_disconnect(pdev);
  450. xenbus_switch_state(xdev, XenbusStateClosed);
  451. if (xenbus_dev_is_online(xdev))
  452. break;
  453. /* fall through if not online */
  454. case XenbusStateUnknown:
  455. dev_dbg(&xdev->dev, "frontend is gone! unregister device\n");
  456. device_unregister(&xdev->dev);
  457. break;
  458. default:
  459. break;
  460. }
  461. }
  462. static int xen_pcibk_setup_backend(struct xen_pcibk_device *pdev)
  463. {
  464. /* Get configuration from xend (if available now) */
  465. int domain, bus, slot, func;
  466. int err = 0;
  467. int i, num_devs;
  468. char dev_str[64];
  469. char state_str[64];
  470. /* It's possible we could get the call to setup twice, so make sure
  471. * we're not already connected.
  472. */
  473. if (xenbus_read_driver_state(pdev->xdev->nodename) !=
  474. XenbusStateInitWait)
  475. goto out;
  476. dev_dbg(&pdev->xdev->dev, "getting be setup\n");
  477. err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, "num_devs", "%d",
  478. &num_devs);
  479. if (err != 1) {
  480. if (err >= 0)
  481. err = -EINVAL;
  482. xenbus_dev_fatal(pdev->xdev, err,
  483. "Error reading number of devices");
  484. goto out;
  485. }
  486. for (i = 0; i < num_devs; i++) {
  487. int l = snprintf(dev_str, sizeof(dev_str), "dev-%d", i);
  488. if (unlikely(l >= (sizeof(dev_str) - 1))) {
  489. err = -ENOMEM;
  490. xenbus_dev_fatal(pdev->xdev, err,
  491. "String overflow while reading "
  492. "configuration");
  493. goto out;
  494. }
  495. err = xenbus_scanf(XBT_NIL, pdev->xdev->nodename, dev_str,
  496. "%x:%x:%x.%x", &domain, &bus, &slot, &func);
  497. if (err < 0) {
  498. xenbus_dev_fatal(pdev->xdev, err,
  499. "Error reading device configuration");
  500. goto out;
  501. }
  502. if (err != 4) {
  503. err = -EINVAL;
  504. xenbus_dev_fatal(pdev->xdev, err,
  505. "Error parsing pci device "
  506. "configuration");
  507. goto out;
  508. }
  509. err = xen_pcibk_export_device(pdev, domain, bus, slot, func, i);
  510. if (err)
  511. goto out;
  512. /* Switch substate of this device. */
  513. l = snprintf(state_str, sizeof(state_str), "state-%d", i);
  514. if (unlikely(l >= (sizeof(state_str) - 1))) {
  515. err = -ENOMEM;
  516. xenbus_dev_fatal(pdev->xdev, err,
  517. "String overflow while reading "
  518. "configuration");
  519. goto out;
  520. }
  521. err = xenbus_printf(XBT_NIL, pdev->xdev->nodename, state_str,
  522. "%d", XenbusStateInitialised);
  523. if (err) {
  524. xenbus_dev_fatal(pdev->xdev, err, "Error switching "
  525. "substate of dev-%d\n", i);
  526. goto out;
  527. }
  528. }
  529. err = xen_pcibk_publish_pci_roots(pdev, xen_pcibk_publish_pci_root);
  530. if (err) {
  531. xenbus_dev_fatal(pdev->xdev, err,
  532. "Error while publish PCI root buses "
  533. "for frontend");
  534. goto out;
  535. }
  536. err = xenbus_switch_state(pdev->xdev, XenbusStateInitialised);
  537. if (err)
  538. xenbus_dev_fatal(pdev->xdev, err,
  539. "Error switching to initialised state!");
  540. out:
  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. struct xen_pcibk_backend *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. }