xenbus_probe.c 18 KB

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