xenbus_probe.c 19 KB

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