xenbus_probe.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  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. static unsigned long xen_store_mfn;
  63. static BLOCKING_NOTIFIER_HEAD(xenstore_chain);
  64. /* If something in array of ids matches this device, return it. */
  65. static const struct xenbus_device_id *
  66. match_device(const struct xenbus_device_id *arr, struct xenbus_device *dev)
  67. {
  68. for (; *arr->devicetype != '\0'; arr++) {
  69. if (!strcmp(arr->devicetype, dev->devicetype))
  70. return arr;
  71. }
  72. return NULL;
  73. }
  74. int xenbus_match(struct device *_dev, struct device_driver *_drv)
  75. {
  76. struct xenbus_driver *drv = to_xenbus_driver(_drv);
  77. if (!drv->ids)
  78. return 0;
  79. return match_device(drv->ids, to_xenbus_device(_dev)) != NULL;
  80. }
  81. EXPORT_SYMBOL_GPL(xenbus_match);
  82. static void free_otherend_details(struct xenbus_device *dev)
  83. {
  84. kfree(dev->otherend);
  85. dev->otherend = NULL;
  86. }
  87. static void free_otherend_watch(struct xenbus_device *dev)
  88. {
  89. if (dev->otherend_watch.node) {
  90. unregister_xenbus_watch(&dev->otherend_watch);
  91. kfree(dev->otherend_watch.node);
  92. dev->otherend_watch.node = NULL;
  93. }
  94. }
  95. static int talk_to_otherend(struct xenbus_device *dev)
  96. {
  97. struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
  98. free_otherend_watch(dev);
  99. free_otherend_details(dev);
  100. return drv->read_otherend_details(dev);
  101. }
  102. static int watch_otherend(struct xenbus_device *dev)
  103. {
  104. struct xen_bus_type *bus =
  105. container_of(dev->dev.bus, struct xen_bus_type, bus);
  106. return xenbus_watch_pathfmt(dev, &dev->otherend_watch,
  107. bus->otherend_changed,
  108. "%s/%s", dev->otherend, "state");
  109. }
  110. int xenbus_read_otherend_details(struct xenbus_device *xendev,
  111. char *id_node, char *path_node)
  112. {
  113. int err = xenbus_gather(XBT_NIL, xendev->nodename,
  114. id_node, "%i", &xendev->otherend_id,
  115. path_node, NULL, &xendev->otherend,
  116. NULL);
  117. if (err) {
  118. xenbus_dev_fatal(xendev, err,
  119. "reading other end details from %s",
  120. xendev->nodename);
  121. return err;
  122. }
  123. if (strlen(xendev->otherend) == 0 ||
  124. !xenbus_exists(XBT_NIL, xendev->otherend, "")) {
  125. xenbus_dev_fatal(xendev, -ENOENT,
  126. "unable to read other end from %s. "
  127. "missing or inaccessible.",
  128. xendev->nodename);
  129. free_otherend_details(xendev);
  130. return -ENOENT;
  131. }
  132. return 0;
  133. }
  134. EXPORT_SYMBOL_GPL(xenbus_read_otherend_details);
  135. void xenbus_otherend_changed(struct xenbus_watch *watch,
  136. const char **vec, unsigned int len,
  137. int ignore_on_shutdown)
  138. {
  139. struct xenbus_device *dev =
  140. container_of(watch, struct xenbus_device, otherend_watch);
  141. struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
  142. enum xenbus_state state;
  143. /* Protect us against watches firing on old details when the otherend
  144. details change, say immediately after a resume. */
  145. if (!dev->otherend ||
  146. strncmp(dev->otherend, vec[XS_WATCH_PATH],
  147. strlen(dev->otherend))) {
  148. dev_dbg(&dev->dev, "Ignoring watch at %s\n",
  149. vec[XS_WATCH_PATH]);
  150. return;
  151. }
  152. state = xenbus_read_driver_state(dev->otherend);
  153. dev_dbg(&dev->dev, "state is %d, (%s), %s, %s\n",
  154. state, xenbus_strstate(state), dev->otherend_watch.node,
  155. vec[XS_WATCH_PATH]);
  156. /*
  157. * Ignore xenbus transitions during shutdown. This prevents us doing
  158. * work that can fail e.g., when the rootfs is gone.
  159. */
  160. if (system_state > SYSTEM_RUNNING) {
  161. if (ignore_on_shutdown && (state == XenbusStateClosing))
  162. xenbus_frontend_closed(dev);
  163. return;
  164. }
  165. if (drv->otherend_changed)
  166. drv->otherend_changed(dev, state);
  167. }
  168. EXPORT_SYMBOL_GPL(xenbus_otherend_changed);
  169. int xenbus_dev_probe(struct device *_dev)
  170. {
  171. struct xenbus_device *dev = to_xenbus_device(_dev);
  172. struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
  173. const struct xenbus_device_id *id;
  174. int err;
  175. DPRINTK("%s", dev->nodename);
  176. if (!drv->probe) {
  177. err = -ENODEV;
  178. goto fail;
  179. }
  180. id = match_device(drv->ids, dev);
  181. if (!id) {
  182. err = -ENODEV;
  183. goto fail;
  184. }
  185. err = talk_to_otherend(dev);
  186. if (err) {
  187. dev_warn(&dev->dev, "talk_to_otherend on %s failed.\n",
  188. dev->nodename);
  189. return err;
  190. }
  191. err = drv->probe(dev, id);
  192. if (err)
  193. goto fail;
  194. err = watch_otherend(dev);
  195. if (err) {
  196. dev_warn(&dev->dev, "watch_otherend on %s failed.\n",
  197. dev->nodename);
  198. return err;
  199. }
  200. return 0;
  201. fail:
  202. xenbus_dev_error(dev, err, "xenbus_dev_probe on %s", dev->nodename);
  203. xenbus_switch_state(dev, XenbusStateClosed);
  204. return err;
  205. }
  206. EXPORT_SYMBOL_GPL(xenbus_dev_probe);
  207. int xenbus_dev_remove(struct device *_dev)
  208. {
  209. struct xenbus_device *dev = to_xenbus_device(_dev);
  210. struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
  211. DPRINTK("%s", dev->nodename);
  212. free_otherend_watch(dev);
  213. if (drv->remove)
  214. drv->remove(dev);
  215. free_otherend_details(dev);
  216. xenbus_switch_state(dev, XenbusStateClosed);
  217. return 0;
  218. }
  219. EXPORT_SYMBOL_GPL(xenbus_dev_remove);
  220. void xenbus_dev_shutdown(struct device *_dev)
  221. {
  222. struct xenbus_device *dev = to_xenbus_device(_dev);
  223. unsigned long timeout = 5*HZ;
  224. DPRINTK("%s", dev->nodename);
  225. get_device(&dev->dev);
  226. if (dev->state != XenbusStateConnected) {
  227. printk(KERN_INFO "%s: %s: %s != Connected, skipping\n", __func__,
  228. dev->nodename, xenbus_strstate(dev->state));
  229. goto out;
  230. }
  231. xenbus_switch_state(dev, XenbusStateClosing);
  232. timeout = wait_for_completion_timeout(&dev->down, timeout);
  233. if (!timeout)
  234. printk(KERN_INFO "%s: %s timeout closing device\n",
  235. __func__, dev->nodename);
  236. out:
  237. put_device(&dev->dev);
  238. }
  239. EXPORT_SYMBOL_GPL(xenbus_dev_shutdown);
  240. int xenbus_register_driver_common(struct xenbus_driver *drv,
  241. struct xen_bus_type *bus)
  242. {
  243. drv->driver.bus = &bus->bus;
  244. return driver_register(&drv->driver);
  245. }
  246. EXPORT_SYMBOL_GPL(xenbus_register_driver_common);
  247. void xenbus_unregister_driver(struct xenbus_driver *drv)
  248. {
  249. driver_unregister(&drv->driver);
  250. }
  251. EXPORT_SYMBOL_GPL(xenbus_unregister_driver);
  252. struct xb_find_info {
  253. struct xenbus_device *dev;
  254. const char *nodename;
  255. };
  256. static int cmp_dev(struct device *dev, void *data)
  257. {
  258. struct xenbus_device *xendev = to_xenbus_device(dev);
  259. struct xb_find_info *info = data;
  260. if (!strcmp(xendev->nodename, info->nodename)) {
  261. info->dev = xendev;
  262. get_device(dev);
  263. return 1;
  264. }
  265. return 0;
  266. }
  267. struct xenbus_device *xenbus_device_find(const char *nodename,
  268. struct bus_type *bus)
  269. {
  270. struct xb_find_info info = { .dev = NULL, .nodename = nodename };
  271. bus_for_each_dev(bus, NULL, &info, cmp_dev);
  272. return info.dev;
  273. }
  274. static int cleanup_dev(struct device *dev, void *data)
  275. {
  276. struct xenbus_device *xendev = to_xenbus_device(dev);
  277. struct xb_find_info *info = data;
  278. int len = strlen(info->nodename);
  279. DPRINTK("%s", info->nodename);
  280. /* Match the info->nodename path, or any subdirectory of that path. */
  281. if (strncmp(xendev->nodename, info->nodename, len))
  282. return 0;
  283. /* If the node name is longer, ensure it really is a subdirectory. */
  284. if ((strlen(xendev->nodename) > len) && (xendev->nodename[len] != '/'))
  285. return 0;
  286. info->dev = xendev;
  287. get_device(dev);
  288. return 1;
  289. }
  290. static void xenbus_cleanup_devices(const char *path, struct bus_type *bus)
  291. {
  292. struct xb_find_info info = { .nodename = path };
  293. do {
  294. info.dev = NULL;
  295. bus_for_each_dev(bus, NULL, &info, cleanup_dev);
  296. if (info.dev) {
  297. device_unregister(&info.dev->dev);
  298. put_device(&info.dev->dev);
  299. }
  300. } while (info.dev);
  301. }
  302. static void xenbus_dev_release(struct device *dev)
  303. {
  304. if (dev)
  305. kfree(to_xenbus_device(dev));
  306. }
  307. static ssize_t nodename_show(struct device *dev,
  308. struct device_attribute *attr, char *buf)
  309. {
  310. return sprintf(buf, "%s\n", to_xenbus_device(dev)->nodename);
  311. }
  312. static ssize_t devtype_show(struct device *dev,
  313. struct device_attribute *attr, char *buf)
  314. {
  315. return sprintf(buf, "%s\n", to_xenbus_device(dev)->devicetype);
  316. }
  317. static ssize_t modalias_show(struct device *dev,
  318. struct device_attribute *attr, char *buf)
  319. {
  320. return sprintf(buf, "%s:%s\n", dev->bus->name,
  321. to_xenbus_device(dev)->devicetype);
  322. }
  323. struct device_attribute xenbus_dev_attrs[] = {
  324. __ATTR_RO(nodename),
  325. __ATTR_RO(devtype),
  326. __ATTR_RO(modalias),
  327. __ATTR_NULL
  328. };
  329. EXPORT_SYMBOL_GPL(xenbus_dev_attrs);
  330. int xenbus_probe_node(struct xen_bus_type *bus,
  331. const char *type,
  332. const char *nodename)
  333. {
  334. char devname[XEN_BUS_ID_SIZE];
  335. int err;
  336. struct xenbus_device *xendev;
  337. size_t stringlen;
  338. char *tmpstring;
  339. enum xenbus_state state = xenbus_read_driver_state(nodename);
  340. if (state != XenbusStateInitialising) {
  341. /* Device is not new, so ignore it. This can happen if a
  342. device is going away after switching to Closed. */
  343. return 0;
  344. }
  345. stringlen = strlen(nodename) + 1 + strlen(type) + 1;
  346. xendev = kzalloc(sizeof(*xendev) + stringlen, GFP_KERNEL);
  347. if (!xendev)
  348. return -ENOMEM;
  349. xendev->state = XenbusStateInitialising;
  350. /* Copy the strings into the extra space. */
  351. tmpstring = (char *)(xendev + 1);
  352. strcpy(tmpstring, nodename);
  353. xendev->nodename = tmpstring;
  354. tmpstring += strlen(tmpstring) + 1;
  355. strcpy(tmpstring, type);
  356. xendev->devicetype = tmpstring;
  357. init_completion(&xendev->down);
  358. xendev->dev.bus = &bus->bus;
  359. xendev->dev.release = xenbus_dev_release;
  360. err = bus->get_bus_id(devname, xendev->nodename);
  361. if (err)
  362. goto fail;
  363. dev_set_name(&xendev->dev, devname);
  364. /* Register with generic device framework. */
  365. err = device_register(&xendev->dev);
  366. if (err)
  367. goto fail;
  368. return 0;
  369. fail:
  370. kfree(xendev);
  371. return err;
  372. }
  373. EXPORT_SYMBOL_GPL(xenbus_probe_node);
  374. static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type)
  375. {
  376. int err = 0;
  377. char **dir;
  378. unsigned int dir_n = 0;
  379. int i;
  380. dir = xenbus_directory(XBT_NIL, bus->root, type, &dir_n);
  381. if (IS_ERR(dir))
  382. return PTR_ERR(dir);
  383. for (i = 0; i < dir_n; i++) {
  384. err = bus->probe(bus, type, dir[i]);
  385. if (err)
  386. break;
  387. }
  388. kfree(dir);
  389. return err;
  390. }
  391. int xenbus_probe_devices(struct xen_bus_type *bus)
  392. {
  393. int err = 0;
  394. char **dir;
  395. unsigned int i, dir_n;
  396. dir = xenbus_directory(XBT_NIL, bus->root, "", &dir_n);
  397. if (IS_ERR(dir))
  398. return PTR_ERR(dir);
  399. for (i = 0; i < dir_n; i++) {
  400. err = xenbus_probe_device_type(bus, dir[i]);
  401. if (err)
  402. break;
  403. }
  404. kfree(dir);
  405. return err;
  406. }
  407. EXPORT_SYMBOL_GPL(xenbus_probe_devices);
  408. static unsigned int char_count(const char *str, char c)
  409. {
  410. unsigned int i, ret = 0;
  411. for (i = 0; str[i]; i++)
  412. if (str[i] == c)
  413. ret++;
  414. return ret;
  415. }
  416. static int strsep_len(const char *str, char c, unsigned int len)
  417. {
  418. unsigned int i;
  419. for (i = 0; str[i]; i++)
  420. if (str[i] == c) {
  421. if (len == 0)
  422. return i;
  423. len--;
  424. }
  425. return (len == 0) ? i : -ERANGE;
  426. }
  427. void xenbus_dev_changed(const char *node, struct xen_bus_type *bus)
  428. {
  429. int exists, rootlen;
  430. struct xenbus_device *dev;
  431. char type[XEN_BUS_ID_SIZE];
  432. const char *p, *root;
  433. if (char_count(node, '/') < 2)
  434. return;
  435. exists = xenbus_exists(XBT_NIL, node, "");
  436. if (!exists) {
  437. xenbus_cleanup_devices(node, &bus->bus);
  438. return;
  439. }
  440. /* backend/<type>/... or device/<type>/... */
  441. p = strchr(node, '/') + 1;
  442. snprintf(type, XEN_BUS_ID_SIZE, "%.*s", (int)strcspn(p, "/"), p);
  443. type[XEN_BUS_ID_SIZE-1] = '\0';
  444. rootlen = strsep_len(node, '/', bus->levels);
  445. if (rootlen < 0)
  446. return;
  447. root = kasprintf(GFP_KERNEL, "%.*s", rootlen, node);
  448. if (!root)
  449. return;
  450. dev = xenbus_device_find(root, &bus->bus);
  451. if (!dev)
  452. xenbus_probe_node(bus, type, root);
  453. else
  454. put_device(&dev->dev);
  455. kfree(root);
  456. }
  457. EXPORT_SYMBOL_GPL(xenbus_dev_changed);
  458. int xenbus_dev_suspend(struct device *dev)
  459. {
  460. int err = 0;
  461. struct xenbus_driver *drv;
  462. struct xenbus_device *xdev
  463. = container_of(dev, struct xenbus_device, dev);
  464. DPRINTK("%s", xdev->nodename);
  465. if (dev->driver == NULL)
  466. return 0;
  467. drv = to_xenbus_driver(dev->driver);
  468. if (drv->suspend)
  469. err = drv->suspend(xdev);
  470. if (err)
  471. printk(KERN_WARNING
  472. "xenbus: suspend %s failed: %i\n", dev_name(dev), err);
  473. return 0;
  474. }
  475. EXPORT_SYMBOL_GPL(xenbus_dev_suspend);
  476. int xenbus_dev_resume(struct device *dev)
  477. {
  478. int err;
  479. struct xenbus_driver *drv;
  480. struct xenbus_device *xdev
  481. = container_of(dev, struct xenbus_device, dev);
  482. DPRINTK("%s", xdev->nodename);
  483. if (dev->driver == NULL)
  484. return 0;
  485. drv = to_xenbus_driver(dev->driver);
  486. err = talk_to_otherend(xdev);
  487. if (err) {
  488. printk(KERN_WARNING
  489. "xenbus: resume (talk_to_otherend) %s failed: %i\n",
  490. dev_name(dev), err);
  491. return err;
  492. }
  493. xdev->state = XenbusStateInitialising;
  494. if (drv->resume) {
  495. err = drv->resume(xdev);
  496. if (err) {
  497. printk(KERN_WARNING
  498. "xenbus: resume %s failed: %i\n",
  499. dev_name(dev), err);
  500. return err;
  501. }
  502. }
  503. err = watch_otherend(xdev);
  504. if (err) {
  505. printk(KERN_WARNING
  506. "xenbus_probe: resume (watch_otherend) %s failed: "
  507. "%d.\n", dev_name(dev), err);
  508. return err;
  509. }
  510. return 0;
  511. }
  512. EXPORT_SYMBOL_GPL(xenbus_dev_resume);
  513. int xenbus_dev_cancel(struct device *dev)
  514. {
  515. /* Do nothing */
  516. DPRINTK("cancel");
  517. return 0;
  518. }
  519. EXPORT_SYMBOL_GPL(xenbus_dev_cancel);
  520. /* A flag to determine if xenstored is 'ready' (i.e. has started) */
  521. int xenstored_ready;
  522. int register_xenstore_notifier(struct notifier_block *nb)
  523. {
  524. int ret = 0;
  525. if (xenstored_ready > 0)
  526. ret = nb->notifier_call(nb, 0, NULL);
  527. else
  528. blocking_notifier_chain_register(&xenstore_chain, nb);
  529. return ret;
  530. }
  531. EXPORT_SYMBOL_GPL(register_xenstore_notifier);
  532. void unregister_xenstore_notifier(struct notifier_block *nb)
  533. {
  534. blocking_notifier_chain_unregister(&xenstore_chain, nb);
  535. }
  536. EXPORT_SYMBOL_GPL(unregister_xenstore_notifier);
  537. void xenbus_probe(struct work_struct *unused)
  538. {
  539. xenstored_ready = 1;
  540. /* Notify others that xenstore is up */
  541. blocking_notifier_call_chain(&xenstore_chain, 0, NULL);
  542. }
  543. EXPORT_SYMBOL_GPL(xenbus_probe);
  544. static int __init xenbus_probe_initcall(void)
  545. {
  546. if (!xen_domain())
  547. return -ENODEV;
  548. if (xen_initial_domain() || xen_hvm_domain())
  549. return 0;
  550. xenbus_probe(NULL);
  551. return 0;
  552. }
  553. device_initcall(xenbus_probe_initcall);
  554. /* Set up event channel for xenstored which is run as a local process
  555. * (this is normally used only in dom0)
  556. */
  557. static int __init xenstored_local_init(void)
  558. {
  559. int err = 0;
  560. unsigned long page = 0;
  561. struct evtchn_alloc_unbound alloc_unbound;
  562. /* Allocate Xenstore page */
  563. page = get_zeroed_page(GFP_KERNEL);
  564. if (!page)
  565. goto out_err;
  566. xen_store_mfn = xen_start_info->store_mfn =
  567. pfn_to_mfn(virt_to_phys((void *)page) >>
  568. PAGE_SHIFT);
  569. /* Next allocate a local port which xenstored can bind to */
  570. alloc_unbound.dom = DOMID_SELF;
  571. alloc_unbound.remote_dom = DOMID_SELF;
  572. err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
  573. &alloc_unbound);
  574. if (err == -ENOSYS)
  575. goto out_err;
  576. BUG_ON(err);
  577. xen_store_evtchn = xen_start_info->store_evtchn =
  578. alloc_unbound.port;
  579. return 0;
  580. out_err:
  581. if (page != 0)
  582. free_page(page);
  583. return err;
  584. }
  585. static int __init xenbus_init(void)
  586. {
  587. int err = 0;
  588. if (!xen_domain())
  589. return -ENODEV;
  590. xenbus_ring_ops_init();
  591. if (xen_hvm_domain()) {
  592. uint64_t v = 0;
  593. err = hvm_get_parameter(HVM_PARAM_STORE_EVTCHN, &v);
  594. if (err)
  595. goto out_error;
  596. xen_store_evtchn = (int)v;
  597. err = hvm_get_parameter(HVM_PARAM_STORE_PFN, &v);
  598. if (err)
  599. goto out_error;
  600. xen_store_mfn = (unsigned long)v;
  601. xen_store_interface = ioremap(xen_store_mfn << PAGE_SHIFT, PAGE_SIZE);
  602. } else {
  603. xen_store_evtchn = xen_start_info->store_evtchn;
  604. xen_store_mfn = xen_start_info->store_mfn;
  605. if (xen_store_evtchn)
  606. xenstored_ready = 1;
  607. else {
  608. err = xenstored_local_init();
  609. if (err)
  610. goto out_error;
  611. }
  612. xen_store_interface = mfn_to_virt(xen_store_mfn);
  613. }
  614. /* Initialize the interface to xenstore. */
  615. err = xs_init();
  616. if (err) {
  617. printk(KERN_WARNING
  618. "XENBUS: Error initializing xenstore comms: %i\n", err);
  619. goto out_error;
  620. }
  621. #ifdef CONFIG_XEN_COMPAT_XENFS
  622. /*
  623. * Create xenfs mountpoint in /proc for compatibility with
  624. * utilities that expect to find "xenbus" under "/proc/xen".
  625. */
  626. proc_mkdir("xen", NULL);
  627. #endif
  628. out_error:
  629. return err;
  630. }
  631. postcore_initcall(xenbus_init);
  632. MODULE_LICENSE("GPL");