xenbus_probe.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841
  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_gfn;
  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. put_device(&xendev->dev);
  386. xendev = NULL;
  387. goto fail;
  388. }
  389. return 0;
  390. fail:
  391. kfree(xendev);
  392. return err;
  393. }
  394. EXPORT_SYMBOL_GPL(xenbus_probe_node);
  395. static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type)
  396. {
  397. int err = 0;
  398. char **dir;
  399. unsigned int dir_n = 0;
  400. int i;
  401. dir = xenbus_directory(XBT_NIL, bus->root, type, &dir_n);
  402. if (IS_ERR(dir))
  403. return PTR_ERR(dir);
  404. for (i = 0; i < dir_n; i++) {
  405. err = bus->probe(bus, type, dir[i]);
  406. if (err)
  407. break;
  408. }
  409. kfree(dir);
  410. return err;
  411. }
  412. int xenbus_probe_devices(struct xen_bus_type *bus)
  413. {
  414. int err = 0;
  415. char **dir;
  416. unsigned int i, dir_n;
  417. dir = xenbus_directory(XBT_NIL, bus->root, "", &dir_n);
  418. if (IS_ERR(dir))
  419. return PTR_ERR(dir);
  420. for (i = 0; i < dir_n; i++) {
  421. err = xenbus_probe_device_type(bus, dir[i]);
  422. if (err)
  423. break;
  424. }
  425. kfree(dir);
  426. return err;
  427. }
  428. EXPORT_SYMBOL_GPL(xenbus_probe_devices);
  429. static unsigned int char_count(const char *str, char c)
  430. {
  431. unsigned int i, ret = 0;
  432. for (i = 0; str[i]; i++)
  433. if (str[i] == c)
  434. ret++;
  435. return ret;
  436. }
  437. static int strsep_len(const char *str, char c, unsigned int len)
  438. {
  439. unsigned int i;
  440. for (i = 0; str[i]; i++)
  441. if (str[i] == c) {
  442. if (len == 0)
  443. return i;
  444. len--;
  445. }
  446. return (len == 0) ? i : -ERANGE;
  447. }
  448. void xenbus_dev_changed(const char *node, struct xen_bus_type *bus)
  449. {
  450. int exists, rootlen;
  451. struct xenbus_device *dev;
  452. char type[XEN_BUS_ID_SIZE];
  453. const char *p, *root;
  454. if (char_count(node, '/') < 2)
  455. return;
  456. exists = xenbus_exists(XBT_NIL, node, "");
  457. if (!exists) {
  458. xenbus_cleanup_devices(node, &bus->bus);
  459. return;
  460. }
  461. /* backend/<type>/... or device/<type>/... */
  462. p = strchr(node, '/') + 1;
  463. snprintf(type, XEN_BUS_ID_SIZE, "%.*s", (int)strcspn(p, "/"), p);
  464. type[XEN_BUS_ID_SIZE-1] = '\0';
  465. rootlen = strsep_len(node, '/', bus->levels);
  466. if (rootlen < 0)
  467. return;
  468. root = kasprintf(GFP_KERNEL, "%.*s", rootlen, node);
  469. if (!root)
  470. return;
  471. dev = xenbus_device_find(root, &bus->bus);
  472. if (!dev)
  473. xenbus_probe_node(bus, type, root);
  474. else
  475. put_device(&dev->dev);
  476. kfree(root);
  477. }
  478. EXPORT_SYMBOL_GPL(xenbus_dev_changed);
  479. int xenbus_dev_suspend(struct device *dev)
  480. {
  481. int err = 0;
  482. struct xenbus_driver *drv;
  483. struct xenbus_device *xdev
  484. = container_of(dev, struct xenbus_device, dev);
  485. DPRINTK("%s", xdev->nodename);
  486. if (dev->driver == NULL)
  487. return 0;
  488. drv = to_xenbus_driver(dev->driver);
  489. if (drv->suspend)
  490. err = drv->suspend(xdev);
  491. if (err)
  492. pr_warn("suspend %s failed: %i\n", dev_name(dev), err);
  493. return 0;
  494. }
  495. EXPORT_SYMBOL_GPL(xenbus_dev_suspend);
  496. int xenbus_dev_resume(struct device *dev)
  497. {
  498. int err;
  499. struct xenbus_driver *drv;
  500. struct xenbus_device *xdev
  501. = container_of(dev, struct xenbus_device, dev);
  502. DPRINTK("%s", xdev->nodename);
  503. if (dev->driver == NULL)
  504. return 0;
  505. drv = to_xenbus_driver(dev->driver);
  506. err = talk_to_otherend(xdev);
  507. if (err) {
  508. pr_warn("resume (talk_to_otherend) %s failed: %i\n",
  509. dev_name(dev), err);
  510. return err;
  511. }
  512. xdev->state = XenbusStateInitialising;
  513. if (drv->resume) {
  514. err = drv->resume(xdev);
  515. if (err) {
  516. pr_warn("resume %s failed: %i\n", dev_name(dev), err);
  517. return err;
  518. }
  519. }
  520. err = watch_otherend(xdev);
  521. if (err) {
  522. pr_warn("resume (watch_otherend) %s failed: %d.\n",
  523. dev_name(dev), err);
  524. return err;
  525. }
  526. return 0;
  527. }
  528. EXPORT_SYMBOL_GPL(xenbus_dev_resume);
  529. int xenbus_dev_cancel(struct device *dev)
  530. {
  531. /* Do nothing */
  532. DPRINTK("cancel");
  533. return 0;
  534. }
  535. EXPORT_SYMBOL_GPL(xenbus_dev_cancel);
  536. /* A flag to determine if xenstored is 'ready' (i.e. has started) */
  537. int xenstored_ready;
  538. int register_xenstore_notifier(struct notifier_block *nb)
  539. {
  540. int ret = 0;
  541. if (xenstored_ready > 0)
  542. ret = nb->notifier_call(nb, 0, NULL);
  543. else
  544. blocking_notifier_chain_register(&xenstore_chain, nb);
  545. return ret;
  546. }
  547. EXPORT_SYMBOL_GPL(register_xenstore_notifier);
  548. void unregister_xenstore_notifier(struct notifier_block *nb)
  549. {
  550. blocking_notifier_chain_unregister(&xenstore_chain, nb);
  551. }
  552. EXPORT_SYMBOL_GPL(unregister_xenstore_notifier);
  553. void xenbus_probe(struct work_struct *unused)
  554. {
  555. xenstored_ready = 1;
  556. /* Notify others that xenstore is up */
  557. blocking_notifier_call_chain(&xenstore_chain, 0, NULL);
  558. }
  559. EXPORT_SYMBOL_GPL(xenbus_probe);
  560. static int __init xenbus_probe_initcall(void)
  561. {
  562. if (!xen_domain())
  563. return -ENODEV;
  564. if (xen_initial_domain() || xen_hvm_domain())
  565. return 0;
  566. xenbus_probe(NULL);
  567. return 0;
  568. }
  569. device_initcall(xenbus_probe_initcall);
  570. /* Set up event channel for xenstored which is run as a local process
  571. * (this is normally used only in dom0)
  572. */
  573. static int __init xenstored_local_init(void)
  574. {
  575. int err = 0;
  576. unsigned long page = 0;
  577. struct evtchn_alloc_unbound alloc_unbound;
  578. /* Allocate Xenstore page */
  579. page = get_zeroed_page(GFP_KERNEL);
  580. if (!page)
  581. goto out_err;
  582. xen_store_gfn = xen_start_info->store_mfn = virt_to_gfn((void *)page);
  583. /* Next allocate a local port which xenstored can bind to */
  584. alloc_unbound.dom = DOMID_SELF;
  585. alloc_unbound.remote_dom = DOMID_SELF;
  586. err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
  587. &alloc_unbound);
  588. if (err == -ENOSYS)
  589. goto out_err;
  590. BUG_ON(err);
  591. xen_store_evtchn = xen_start_info->store_evtchn =
  592. alloc_unbound.port;
  593. return 0;
  594. out_err:
  595. if (page != 0)
  596. free_page(page);
  597. return err;
  598. }
  599. static int xenbus_resume_cb(struct notifier_block *nb,
  600. unsigned long action, void *data)
  601. {
  602. int err = 0;
  603. if (xen_hvm_domain()) {
  604. uint64_t v = 0;
  605. err = hvm_get_parameter(HVM_PARAM_STORE_EVTCHN, &v);
  606. if (!err && v)
  607. xen_store_evtchn = v;
  608. else
  609. pr_warn("Cannot update xenstore event channel: %d\n",
  610. err);
  611. } else
  612. xen_store_evtchn = xen_start_info->store_evtchn;
  613. return err;
  614. }
  615. static struct notifier_block xenbus_resume_nb = {
  616. .notifier_call = xenbus_resume_cb,
  617. };
  618. static int __init xenbus_init(void)
  619. {
  620. int err = 0;
  621. uint64_t v = 0;
  622. xen_store_domain_type = XS_UNKNOWN;
  623. if (!xen_domain())
  624. return -ENODEV;
  625. xenbus_ring_ops_init();
  626. if (xen_pv_domain())
  627. xen_store_domain_type = XS_PV;
  628. if (xen_hvm_domain())
  629. xen_store_domain_type = XS_HVM;
  630. if (xen_hvm_domain() && xen_initial_domain())
  631. xen_store_domain_type = XS_LOCAL;
  632. if (xen_pv_domain() && !xen_start_info->store_evtchn)
  633. xen_store_domain_type = XS_LOCAL;
  634. if (xen_pv_domain() && xen_start_info->store_evtchn)
  635. xenstored_ready = 1;
  636. switch (xen_store_domain_type) {
  637. case XS_LOCAL:
  638. err = xenstored_local_init();
  639. if (err)
  640. goto out_error;
  641. xen_store_interface = gfn_to_virt(xen_store_gfn);
  642. break;
  643. case XS_PV:
  644. xen_store_evtchn = xen_start_info->store_evtchn;
  645. xen_store_gfn = xen_start_info->store_mfn;
  646. xen_store_interface = gfn_to_virt(xen_store_gfn);
  647. break;
  648. case XS_HVM:
  649. err = hvm_get_parameter(HVM_PARAM_STORE_EVTCHN, &v);
  650. if (err)
  651. goto out_error;
  652. xen_store_evtchn = (int)v;
  653. err = hvm_get_parameter(HVM_PARAM_STORE_PFN, &v);
  654. if (err)
  655. goto out_error;
  656. xen_store_gfn = (unsigned long)v;
  657. xen_store_interface =
  658. xen_remap(xen_store_gfn << XEN_PAGE_SHIFT,
  659. XEN_PAGE_SIZE);
  660. break;
  661. default:
  662. pr_warn("Xenstore state unknown\n");
  663. break;
  664. }
  665. /* Initialize the interface to xenstore. */
  666. err = xs_init();
  667. if (err) {
  668. pr_warn("Error initializing xenstore comms: %i\n", err);
  669. goto out_error;
  670. }
  671. if ((xen_store_domain_type != XS_LOCAL) &&
  672. (xen_store_domain_type != XS_UNKNOWN))
  673. xen_resume_notifier_register(&xenbus_resume_nb);
  674. #ifdef CONFIG_XEN_COMPAT_XENFS
  675. /*
  676. * Create xenfs mountpoint in /proc for compatibility with
  677. * utilities that expect to find "xenbus" under "/proc/xen".
  678. */
  679. proc_mkdir("xen", NULL);
  680. #endif
  681. out_error:
  682. return err;
  683. }
  684. postcore_initcall(xenbus_init);
  685. MODULE_LICENSE("GPL");