xenbus.c 18 KB

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