xenbus_probe.c 19 KB

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