xenbus_probe.c 19 KB

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