xenbus_probe.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798
  1. /******************************************************************************
  2. * Talks to Xen Store to figure out what devices we have.
  3. *
  4. * Copyright (C) 2005 Rusty Russell, IBM Corporation
  5. * Copyright (C) 2005 Mike Wray, Hewlett-Packard
  6. * Copyright (C) 2005, 2006 XenSource Ltd
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License version 2
  10. * as published by the Free Software Foundation; or, when distributed
  11. * separately from the Linux kernel or incorporated into other
  12. * software packages, subject to the following license:
  13. *
  14. * Permission is hereby granted, free of charge, to any person obtaining a copy
  15. * of this source file (the "Software"), to deal in the Software without
  16. * restriction, including without limitation the rights to use, copy, modify,
  17. * merge, publish, distribute, sublicense, and/or sell copies of the Software,
  18. * and to permit persons to whom the Software is furnished to do so, subject to
  19. * the following conditions:
  20. *
  21. * The above copyright notice and this permission notice shall be included in
  22. * all copies or substantial portions of the Software.
  23. *
  24. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  25. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  27. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  28. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  29. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  30. * IN THE SOFTWARE.
  31. */
  32. #define DPRINTK(fmt, args...) \
  33. pr_debug("xenbus_probe (%s:%d) " fmt ".\n", \
  34. __func__, __LINE__, ##args)
  35. #include <linux/kernel.h>
  36. #include <linux/err.h>
  37. #include <linux/string.h>
  38. #include <linux/ctype.h>
  39. #include <linux/fcntl.h>
  40. #include <linux/mm.h>
  41. #include <linux/proc_fs.h>
  42. #include <linux/notifier.h>
  43. #include <linux/kthread.h>
  44. #include <linux/mutex.h>
  45. #include <linux/io.h>
  46. #include <linux/slab.h>
  47. #include <linux/module.h>
  48. #include <asm/page.h>
  49. #include <asm/pgtable.h>
  50. #include <asm/xen/hypervisor.h>
  51. #include <xen/xen.h>
  52. #include <xen/xenbus.h>
  53. #include <xen/events.h>
  54. #include <xen/page.h>
  55. #include <xen/hvm.h>
  56. #include "xenbus_comms.h"
  57. #include "xenbus_probe.h"
  58. int xen_store_evtchn;
  59. EXPORT_SYMBOL_GPL(xen_store_evtchn);
  60. struct xenstore_domain_interface *xen_store_interface;
  61. EXPORT_SYMBOL_GPL(xen_store_interface);
  62. enum xenstore_init xen_store_domain_type;
  63. EXPORT_SYMBOL_GPL(xen_store_domain_type);
  64. static unsigned long xen_store_mfn;
  65. static BLOCKING_NOTIFIER_HEAD(xenstore_chain);
  66. /* If something in array of ids matches this device, return it. */
  67. static const struct xenbus_device_id *
  68. match_device(const struct xenbus_device_id *arr, struct xenbus_device *dev)
  69. {
  70. for (; *arr->devicetype != '\0'; arr++) {
  71. if (!strcmp(arr->devicetype, dev->devicetype))
  72. return arr;
  73. }
  74. return NULL;
  75. }
  76. int xenbus_match(struct device *_dev, struct device_driver *_drv)
  77. {
  78. struct xenbus_driver *drv = to_xenbus_driver(_drv);
  79. if (!drv->ids)
  80. return 0;
  81. return match_device(drv->ids, to_xenbus_device(_dev)) != NULL;
  82. }
  83. EXPORT_SYMBOL_GPL(xenbus_match);
  84. static void free_otherend_details(struct xenbus_device *dev)
  85. {
  86. kfree(dev->otherend);
  87. dev->otherend = NULL;
  88. }
  89. static void free_otherend_watch(struct xenbus_device *dev)
  90. {
  91. if (dev->otherend_watch.node) {
  92. unregister_xenbus_watch(&dev->otherend_watch);
  93. kfree(dev->otherend_watch.node);
  94. dev->otherend_watch.node = NULL;
  95. }
  96. }
  97. static int talk_to_otherend(struct xenbus_device *dev)
  98. {
  99. struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
  100. free_otherend_watch(dev);
  101. free_otherend_details(dev);
  102. return drv->read_otherend_details(dev);
  103. }
  104. static int watch_otherend(struct xenbus_device *dev)
  105. {
  106. struct xen_bus_type *bus =
  107. container_of(dev->dev.bus, struct xen_bus_type, bus);
  108. return xenbus_watch_pathfmt(dev, &dev->otherend_watch,
  109. bus->otherend_changed,
  110. "%s/%s", dev->otherend, "state");
  111. }
  112. int xenbus_read_otherend_details(struct xenbus_device *xendev,
  113. char *id_node, char *path_node)
  114. {
  115. int err = xenbus_gather(XBT_NIL, xendev->nodename,
  116. id_node, "%i", &xendev->otherend_id,
  117. path_node, NULL, &xendev->otherend,
  118. NULL);
  119. if (err) {
  120. xenbus_dev_fatal(xendev, err,
  121. "reading other end details from %s",
  122. xendev->nodename);
  123. return err;
  124. }
  125. if (strlen(xendev->otherend) == 0 ||
  126. !xenbus_exists(XBT_NIL, xendev->otherend, "")) {
  127. xenbus_dev_fatal(xendev, -ENOENT,
  128. "unable to read other end from %s. "
  129. "missing or inaccessible.",
  130. xendev->nodename);
  131. free_otherend_details(xendev);
  132. return -ENOENT;
  133. }
  134. return 0;
  135. }
  136. EXPORT_SYMBOL_GPL(xenbus_read_otherend_details);
  137. void xenbus_otherend_changed(struct xenbus_watch *watch,
  138. const char **vec, unsigned int len,
  139. int ignore_on_shutdown)
  140. {
  141. struct xenbus_device *dev =
  142. container_of(watch, struct xenbus_device, otherend_watch);
  143. struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
  144. enum xenbus_state state;
  145. /* Protect us against watches firing on old details when the otherend
  146. details change, say immediately after a resume. */
  147. if (!dev->otherend ||
  148. strncmp(dev->otherend, vec[XS_WATCH_PATH],
  149. strlen(dev->otherend))) {
  150. dev_dbg(&dev->dev, "Ignoring watch at %s\n",
  151. vec[XS_WATCH_PATH]);
  152. return;
  153. }
  154. state = xenbus_read_driver_state(dev->otherend);
  155. dev_dbg(&dev->dev, "state is %d, (%s), %s, %s\n",
  156. state, xenbus_strstate(state), dev->otherend_watch.node,
  157. vec[XS_WATCH_PATH]);
  158. /*
  159. * Ignore xenbus transitions during shutdown. This prevents us doing
  160. * work that can fail e.g., when the rootfs is gone.
  161. */
  162. if (system_state > SYSTEM_RUNNING) {
  163. if (ignore_on_shutdown && (state == XenbusStateClosing))
  164. xenbus_frontend_closed(dev);
  165. return;
  166. }
  167. if (drv->otherend_changed)
  168. drv->otherend_changed(dev, state);
  169. }
  170. EXPORT_SYMBOL_GPL(xenbus_otherend_changed);
  171. int xenbus_dev_probe(struct device *_dev)
  172. {
  173. struct xenbus_device *dev = to_xenbus_device(_dev);
  174. struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
  175. const struct xenbus_device_id *id;
  176. int err;
  177. DPRINTK("%s", dev->nodename);
  178. if (!drv->probe) {
  179. err = -ENODEV;
  180. goto fail;
  181. }
  182. id = match_device(drv->ids, dev);
  183. if (!id) {
  184. err = -ENODEV;
  185. goto fail;
  186. }
  187. err = talk_to_otherend(dev);
  188. if (err) {
  189. dev_warn(&dev->dev, "talk_to_otherend on %s failed.\n",
  190. dev->nodename);
  191. return err;
  192. }
  193. err = drv->probe(dev, id);
  194. if (err)
  195. goto fail;
  196. err = watch_otherend(dev);
  197. if (err) {
  198. dev_warn(&dev->dev, "watch_otherend on %s failed.\n",
  199. dev->nodename);
  200. return err;
  201. }
  202. return 0;
  203. fail:
  204. xenbus_dev_error(dev, err, "xenbus_dev_probe on %s", dev->nodename);
  205. xenbus_switch_state(dev, XenbusStateClosed);
  206. return err;
  207. }
  208. EXPORT_SYMBOL_GPL(xenbus_dev_probe);
  209. int xenbus_dev_remove(struct device *_dev)
  210. {
  211. struct xenbus_device *dev = to_xenbus_device(_dev);
  212. struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
  213. DPRINTK("%s", dev->nodename);
  214. free_otherend_watch(dev);
  215. if (drv->remove)
  216. drv->remove(dev);
  217. free_otherend_details(dev);
  218. xenbus_switch_state(dev, XenbusStateClosed);
  219. return 0;
  220. }
  221. EXPORT_SYMBOL_GPL(xenbus_dev_remove);
  222. void xenbus_dev_shutdown(struct device *_dev)
  223. {
  224. struct xenbus_device *dev = to_xenbus_device(_dev);
  225. unsigned long timeout = 5*HZ;
  226. DPRINTK("%s", dev->nodename);
  227. get_device(&dev->dev);
  228. if (dev->state != XenbusStateConnected) {
  229. printk(KERN_INFO "%s: %s: %s != Connected, skipping\n", __func__,
  230. dev->nodename, xenbus_strstate(dev->state));
  231. goto out;
  232. }
  233. xenbus_switch_state(dev, XenbusStateClosing);
  234. timeout = wait_for_completion_timeout(&dev->down, timeout);
  235. if (!timeout)
  236. printk(KERN_INFO "%s: %s timeout closing device\n",
  237. __func__, dev->nodename);
  238. out:
  239. put_device(&dev->dev);
  240. }
  241. EXPORT_SYMBOL_GPL(xenbus_dev_shutdown);
  242. int xenbus_register_driver_common(struct xenbus_driver *drv,
  243. struct xen_bus_type *bus)
  244. {
  245. drv->driver.bus = &bus->bus;
  246. return driver_register(&drv->driver);
  247. }
  248. EXPORT_SYMBOL_GPL(xenbus_register_driver_common);
  249. void xenbus_unregister_driver(struct xenbus_driver *drv)
  250. {
  251. driver_unregister(&drv->driver);
  252. }
  253. EXPORT_SYMBOL_GPL(xenbus_unregister_driver);
  254. struct xb_find_info {
  255. struct xenbus_device *dev;
  256. const char *nodename;
  257. };
  258. static int cmp_dev(struct device *dev, void *data)
  259. {
  260. struct xenbus_device *xendev = to_xenbus_device(dev);
  261. struct xb_find_info *info = data;
  262. if (!strcmp(xendev->nodename, info->nodename)) {
  263. info->dev = xendev;
  264. get_device(dev);
  265. return 1;
  266. }
  267. return 0;
  268. }
  269. static struct xenbus_device *xenbus_device_find(const char *nodename,
  270. struct bus_type *bus)
  271. {
  272. struct xb_find_info info = { .dev = NULL, .nodename = nodename };
  273. bus_for_each_dev(bus, NULL, &info, cmp_dev);
  274. return info.dev;
  275. }
  276. static int cleanup_dev(struct device *dev, void *data)
  277. {
  278. struct xenbus_device *xendev = to_xenbus_device(dev);
  279. struct xb_find_info *info = data;
  280. int len = strlen(info->nodename);
  281. DPRINTK("%s", info->nodename);
  282. /* Match the info->nodename path, or any subdirectory of that path. */
  283. if (strncmp(xendev->nodename, info->nodename, len))
  284. return 0;
  285. /* If the node name is longer, ensure it really is a subdirectory. */
  286. if ((strlen(xendev->nodename) > len) && (xendev->nodename[len] != '/'))
  287. return 0;
  288. info->dev = xendev;
  289. get_device(dev);
  290. return 1;
  291. }
  292. static void xenbus_cleanup_devices(const char *path, struct bus_type *bus)
  293. {
  294. struct xb_find_info info = { .nodename = path };
  295. do {
  296. info.dev = NULL;
  297. bus_for_each_dev(bus, NULL, &info, cleanup_dev);
  298. if (info.dev) {
  299. device_unregister(&info.dev->dev);
  300. put_device(&info.dev->dev);
  301. }
  302. } while (info.dev);
  303. }
  304. static void xenbus_dev_release(struct device *dev)
  305. {
  306. if (dev)
  307. kfree(to_xenbus_device(dev));
  308. }
  309. static ssize_t nodename_show(struct device *dev,
  310. struct device_attribute *attr, char *buf)
  311. {
  312. return sprintf(buf, "%s\n", to_xenbus_device(dev)->nodename);
  313. }
  314. static ssize_t devtype_show(struct device *dev,
  315. struct device_attribute *attr, char *buf)
  316. {
  317. return sprintf(buf, "%s\n", to_xenbus_device(dev)->devicetype);
  318. }
  319. static ssize_t modalias_show(struct device *dev,
  320. struct device_attribute *attr, char *buf)
  321. {
  322. return sprintf(buf, "%s:%s\n", dev->bus->name,
  323. to_xenbus_device(dev)->devicetype);
  324. }
  325. struct device_attribute xenbus_dev_attrs[] = {
  326. __ATTR_RO(nodename),
  327. __ATTR_RO(devtype),
  328. __ATTR_RO(modalias),
  329. __ATTR_NULL
  330. };
  331. EXPORT_SYMBOL_GPL(xenbus_dev_attrs);
  332. int xenbus_probe_node(struct xen_bus_type *bus,
  333. const char *type,
  334. const char *nodename)
  335. {
  336. char devname[XEN_BUS_ID_SIZE];
  337. int err;
  338. struct xenbus_device *xendev;
  339. size_t stringlen;
  340. char *tmpstring;
  341. enum xenbus_state state = xenbus_read_driver_state(nodename);
  342. if (state != XenbusStateInitialising) {
  343. /* Device is not new, so ignore it. This can happen if a
  344. device is going away after switching to Closed. */
  345. return 0;
  346. }
  347. stringlen = strlen(nodename) + 1 + strlen(type) + 1;
  348. xendev = kzalloc(sizeof(*xendev) + stringlen, GFP_KERNEL);
  349. if (!xendev)
  350. return -ENOMEM;
  351. xendev->state = XenbusStateInitialising;
  352. /* Copy the strings into the extra space. */
  353. tmpstring = (char *)(xendev + 1);
  354. strcpy(tmpstring, nodename);
  355. xendev->nodename = tmpstring;
  356. tmpstring += strlen(tmpstring) + 1;
  357. strcpy(tmpstring, type);
  358. xendev->devicetype = tmpstring;
  359. init_completion(&xendev->down);
  360. xendev->dev.bus = &bus->bus;
  361. xendev->dev.release = xenbus_dev_release;
  362. err = bus->get_bus_id(devname, xendev->nodename);
  363. if (err)
  364. goto fail;
  365. dev_set_name(&xendev->dev, "%s", devname);
  366. /* Register with generic device framework. */
  367. err = device_register(&xendev->dev);
  368. if (err)
  369. goto fail;
  370. return 0;
  371. fail:
  372. kfree(xendev);
  373. return err;
  374. }
  375. EXPORT_SYMBOL_GPL(xenbus_probe_node);
  376. static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type)
  377. {
  378. int err = 0;
  379. char **dir;
  380. unsigned int dir_n = 0;
  381. int i;
  382. dir = xenbus_directory(XBT_NIL, bus->root, type, &dir_n);
  383. if (IS_ERR(dir))
  384. return PTR_ERR(dir);
  385. for (i = 0; i < dir_n; i++) {
  386. err = bus->probe(bus, type, dir[i]);
  387. if (err)
  388. break;
  389. }
  390. kfree(dir);
  391. return err;
  392. }
  393. int xenbus_probe_devices(struct xen_bus_type *bus)
  394. {
  395. int err = 0;
  396. char **dir;
  397. unsigned int i, dir_n;
  398. dir = xenbus_directory(XBT_NIL, bus->root, "", &dir_n);
  399. if (IS_ERR(dir))
  400. return PTR_ERR(dir);
  401. for (i = 0; i < dir_n; i++) {
  402. err = xenbus_probe_device_type(bus, dir[i]);
  403. if (err)
  404. break;
  405. }
  406. kfree(dir);
  407. return err;
  408. }
  409. EXPORT_SYMBOL_GPL(xenbus_probe_devices);
  410. static unsigned int char_count(const char *str, char c)
  411. {
  412. unsigned int i, ret = 0;
  413. for (i = 0; str[i]; i++)
  414. if (str[i] == c)
  415. ret++;
  416. return ret;
  417. }
  418. static int strsep_len(const char *str, char c, unsigned int len)
  419. {
  420. unsigned int i;
  421. for (i = 0; str[i]; i++)
  422. if (str[i] == c) {
  423. if (len == 0)
  424. return i;
  425. len--;
  426. }
  427. return (len == 0) ? i : -ERANGE;
  428. }
  429. void xenbus_dev_changed(const char *node, struct xen_bus_type *bus)
  430. {
  431. int exists, rootlen;
  432. struct xenbus_device *dev;
  433. char type[XEN_BUS_ID_SIZE];
  434. const char *p, *root;
  435. if (char_count(node, '/') < 2)
  436. return;
  437. exists = xenbus_exists(XBT_NIL, node, "");
  438. if (!exists) {
  439. xenbus_cleanup_devices(node, &bus->bus);
  440. return;
  441. }
  442. /* backend/<type>/... or device/<type>/... */
  443. p = strchr(node, '/') + 1;
  444. snprintf(type, XEN_BUS_ID_SIZE, "%.*s", (int)strcspn(p, "/"), p);
  445. type[XEN_BUS_ID_SIZE-1] = '\0';
  446. rootlen = strsep_len(node, '/', bus->levels);
  447. if (rootlen < 0)
  448. return;
  449. root = kasprintf(GFP_KERNEL, "%.*s", rootlen, node);
  450. if (!root)
  451. return;
  452. dev = xenbus_device_find(root, &bus->bus);
  453. if (!dev)
  454. xenbus_probe_node(bus, type, root);
  455. else
  456. put_device(&dev->dev);
  457. kfree(root);
  458. }
  459. EXPORT_SYMBOL_GPL(xenbus_dev_changed);
  460. int xenbus_dev_suspend(struct device *dev)
  461. {
  462. int err = 0;
  463. struct xenbus_driver *drv;
  464. struct xenbus_device *xdev
  465. = container_of(dev, struct xenbus_device, dev);
  466. DPRINTK("%s", xdev->nodename);
  467. if (dev->driver == NULL)
  468. return 0;
  469. drv = to_xenbus_driver(dev->driver);
  470. if (drv->suspend)
  471. err = drv->suspend(xdev);
  472. if (err)
  473. printk(KERN_WARNING
  474. "xenbus: suspend %s failed: %i\n", dev_name(dev), err);
  475. return 0;
  476. }
  477. EXPORT_SYMBOL_GPL(xenbus_dev_suspend);
  478. int xenbus_dev_resume(struct device *dev)
  479. {
  480. int err;
  481. struct xenbus_driver *drv;
  482. struct xenbus_device *xdev
  483. = container_of(dev, struct xenbus_device, dev);
  484. DPRINTK("%s", xdev->nodename);
  485. if (dev->driver == NULL)
  486. return 0;
  487. drv = to_xenbus_driver(dev->driver);
  488. err = talk_to_otherend(xdev);
  489. if (err) {
  490. printk(KERN_WARNING
  491. "xenbus: resume (talk_to_otherend) %s failed: %i\n",
  492. dev_name(dev), err);
  493. return err;
  494. }
  495. xdev->state = XenbusStateInitialising;
  496. if (drv->resume) {
  497. err = drv->resume(xdev);
  498. if (err) {
  499. printk(KERN_WARNING
  500. "xenbus: resume %s failed: %i\n",
  501. dev_name(dev), err);
  502. return err;
  503. }
  504. }
  505. err = watch_otherend(xdev);
  506. if (err) {
  507. printk(KERN_WARNING
  508. "xenbus_probe: resume (watch_otherend) %s failed: "
  509. "%d.\n", dev_name(dev), err);
  510. return err;
  511. }
  512. return 0;
  513. }
  514. EXPORT_SYMBOL_GPL(xenbus_dev_resume);
  515. int xenbus_dev_cancel(struct device *dev)
  516. {
  517. /* Do nothing */
  518. DPRINTK("cancel");
  519. return 0;
  520. }
  521. EXPORT_SYMBOL_GPL(xenbus_dev_cancel);
  522. /* A flag to determine if xenstored is 'ready' (i.e. has started) */
  523. int xenstored_ready;
  524. int register_xenstore_notifier(struct notifier_block *nb)
  525. {
  526. int ret = 0;
  527. if (xenstored_ready > 0)
  528. ret = nb->notifier_call(nb, 0, NULL);
  529. else
  530. blocking_notifier_chain_register(&xenstore_chain, nb);
  531. return ret;
  532. }
  533. EXPORT_SYMBOL_GPL(register_xenstore_notifier);
  534. void unregister_xenstore_notifier(struct notifier_block *nb)
  535. {
  536. blocking_notifier_chain_unregister(&xenstore_chain, nb);
  537. }
  538. EXPORT_SYMBOL_GPL(unregister_xenstore_notifier);
  539. void xenbus_probe(struct work_struct *unused)
  540. {
  541. xenstored_ready = 1;
  542. /* Notify others that xenstore is up */
  543. blocking_notifier_call_chain(&xenstore_chain, 0, NULL);
  544. }
  545. EXPORT_SYMBOL_GPL(xenbus_probe);
  546. static int __init xenbus_probe_initcall(void)
  547. {
  548. if (!xen_domain())
  549. return -ENODEV;
  550. if (xen_initial_domain() || xen_hvm_domain())
  551. return 0;
  552. xenbus_probe(NULL);
  553. return 0;
  554. }
  555. device_initcall(xenbus_probe_initcall);
  556. /* Set up event channel for xenstored which is run as a local process
  557. * (this is normally used only in dom0)
  558. */
  559. static int __init xenstored_local_init(void)
  560. {
  561. int err = 0;
  562. unsigned long page = 0;
  563. struct evtchn_alloc_unbound alloc_unbound;
  564. /* Allocate Xenstore page */
  565. page = get_zeroed_page(GFP_KERNEL);
  566. if (!page)
  567. goto out_err;
  568. xen_store_mfn = xen_start_info->store_mfn =
  569. pfn_to_mfn(virt_to_phys((void *)page) >>
  570. PAGE_SHIFT);
  571. /* Next allocate a local port which xenstored can bind to */
  572. alloc_unbound.dom = DOMID_SELF;
  573. alloc_unbound.remote_dom = DOMID_SELF;
  574. err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
  575. &alloc_unbound);
  576. if (err == -ENOSYS)
  577. goto out_err;
  578. BUG_ON(err);
  579. xen_store_evtchn = xen_start_info->store_evtchn =
  580. alloc_unbound.port;
  581. return 0;
  582. out_err:
  583. if (page != 0)
  584. free_page(page);
  585. return err;
  586. }
  587. static int __init xenbus_init(void)
  588. {
  589. int err = 0;
  590. uint64_t v = 0;
  591. xen_store_domain_type = XS_UNKNOWN;
  592. if (!xen_domain())
  593. return -ENODEV;
  594. xenbus_ring_ops_init();
  595. if (xen_pv_domain())
  596. xen_store_domain_type = XS_PV;
  597. if (xen_hvm_domain())
  598. xen_store_domain_type = XS_HVM;
  599. if (xen_hvm_domain() && xen_initial_domain())
  600. xen_store_domain_type = XS_LOCAL;
  601. if (xen_pv_domain() && !xen_start_info->store_evtchn)
  602. xen_store_domain_type = XS_LOCAL;
  603. if (xen_pv_domain() && xen_start_info->store_evtchn)
  604. xenstored_ready = 1;
  605. switch (xen_store_domain_type) {
  606. case XS_LOCAL:
  607. err = xenstored_local_init();
  608. if (err)
  609. goto out_error;
  610. xen_store_interface = mfn_to_virt(xen_store_mfn);
  611. break;
  612. case XS_PV:
  613. xen_store_evtchn = xen_start_info->store_evtchn;
  614. xen_store_mfn = xen_start_info->store_mfn;
  615. xen_store_interface = mfn_to_virt(xen_store_mfn);
  616. break;
  617. case XS_HVM:
  618. err = hvm_get_parameter(HVM_PARAM_STORE_EVTCHN, &v);
  619. if (err)
  620. goto out_error;
  621. xen_store_evtchn = (int)v;
  622. err = hvm_get_parameter(HVM_PARAM_STORE_PFN, &v);
  623. if (err)
  624. goto out_error;
  625. xen_store_mfn = (unsigned long)v;
  626. xen_store_interface =
  627. xen_remap(xen_store_mfn << PAGE_SHIFT, PAGE_SIZE);
  628. break;
  629. default:
  630. pr_warn("Xenstore state unknown\n");
  631. break;
  632. }
  633. /* Initialize the interface to xenstore. */
  634. err = xs_init();
  635. if (err) {
  636. printk(KERN_WARNING
  637. "XENBUS: Error initializing xenstore comms: %i\n", err);
  638. goto out_error;
  639. }
  640. #ifdef CONFIG_XEN_COMPAT_XENFS
  641. /*
  642. * Create xenfs mountpoint in /proc for compatibility with
  643. * utilities that expect to find "xenbus" under "/proc/xen".
  644. */
  645. proc_mkdir("xen", NULL);
  646. #endif
  647. out_error:
  648. return err;
  649. }
  650. postcore_initcall(xenbus_init);
  651. MODULE_LICENSE("GPL");