xenbus_probe.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930
  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_will_handle,
  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 *path, const char *token,
  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, path, strlen(dev->otherend))) {
  151. dev_dbg(&dev->dev, "Ignoring watch at %s\n", path);
  152. return;
  153. }
  154. state = xenbus_read_driver_state(dev->otherend);
  155. dev_dbg(&dev->dev, "state is %d, (%s), %s, %s\n",
  156. state, xenbus_strstate(state), dev->otherend_watch.node, path);
  157. /*
  158. * Ignore xenbus transitions during shutdown. This prevents us doing
  159. * work that can fail e.g., when the rootfs is gone.
  160. */
  161. if (system_state > SYSTEM_RUNNING) {
  162. if (ignore_on_shutdown && (state == XenbusStateClosing))
  163. xenbus_frontend_closed(dev);
  164. return;
  165. }
  166. if (drv->otherend_changed)
  167. drv->otherend_changed(dev, state);
  168. }
  169. EXPORT_SYMBOL_GPL(xenbus_otherend_changed);
  170. int xenbus_dev_probe(struct device *_dev)
  171. {
  172. struct xenbus_device *dev = to_xenbus_device(_dev);
  173. struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
  174. const struct xenbus_device_id *id;
  175. int err;
  176. DPRINTK("%s", dev->nodename);
  177. if (!drv->probe) {
  178. err = -ENODEV;
  179. goto fail;
  180. }
  181. id = match_device(drv->ids, dev);
  182. if (!id) {
  183. err = -ENODEV;
  184. goto fail;
  185. }
  186. err = talk_to_otherend(dev);
  187. if (err) {
  188. dev_warn(&dev->dev, "talk_to_otherend on %s failed.\n",
  189. dev->nodename);
  190. return err;
  191. }
  192. err = drv->probe(dev, id);
  193. if (err)
  194. goto fail;
  195. err = watch_otherend(dev);
  196. if (err) {
  197. dev_warn(&dev->dev, "watch_otherend on %s failed.\n",
  198. dev->nodename);
  199. return err;
  200. }
  201. return 0;
  202. fail:
  203. xenbus_dev_error(dev, err, "xenbus_dev_probe on %s", dev->nodename);
  204. xenbus_switch_state(dev, XenbusStateClosed);
  205. return err;
  206. }
  207. EXPORT_SYMBOL_GPL(xenbus_dev_probe);
  208. int xenbus_dev_remove(struct device *_dev)
  209. {
  210. struct xenbus_device *dev = to_xenbus_device(_dev);
  211. struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
  212. DPRINTK("%s", dev->nodename);
  213. free_otherend_watch(dev);
  214. if (drv->remove)
  215. drv->remove(dev);
  216. free_otherend_details(dev);
  217. xenbus_switch_state(dev, XenbusStateClosed);
  218. return 0;
  219. }
  220. EXPORT_SYMBOL_GPL(xenbus_dev_remove);
  221. void xenbus_dev_shutdown(struct device *_dev)
  222. {
  223. struct xenbus_device *dev = to_xenbus_device(_dev);
  224. unsigned long timeout = 5*HZ;
  225. DPRINTK("%s", dev->nodename);
  226. get_device(&dev->dev);
  227. if (dev->state != XenbusStateConnected) {
  228. pr_info("%s: %s: %s != Connected, skipping\n",
  229. __func__, dev->nodename, xenbus_strstate(dev->state));
  230. goto out;
  231. }
  232. xenbus_switch_state(dev, XenbusStateClosing);
  233. timeout = wait_for_completion_timeout(&dev->down, timeout);
  234. if (!timeout)
  235. pr_info("%s: %s timeout closing device\n",
  236. __func__, dev->nodename);
  237. out:
  238. put_device(&dev->dev);
  239. }
  240. EXPORT_SYMBOL_GPL(xenbus_dev_shutdown);
  241. int xenbus_register_driver_common(struct xenbus_driver *drv,
  242. struct xen_bus_type *bus,
  243. struct module *owner, const char *mod_name)
  244. {
  245. drv->driver.name = drv->name ? drv->name : drv->ids[0].devicetype;
  246. drv->driver.bus = &bus->bus;
  247. drv->driver.owner = owner;
  248. drv->driver.mod_name = mod_name;
  249. return driver_register(&drv->driver);
  250. }
  251. EXPORT_SYMBOL_GPL(xenbus_register_driver_common);
  252. void xenbus_unregister_driver(struct xenbus_driver *drv)
  253. {
  254. driver_unregister(&drv->driver);
  255. }
  256. EXPORT_SYMBOL_GPL(xenbus_unregister_driver);
  257. struct xb_find_info {
  258. struct xenbus_device *dev;
  259. const char *nodename;
  260. };
  261. static int cmp_dev(struct device *dev, void *data)
  262. {
  263. struct xenbus_device *xendev = to_xenbus_device(dev);
  264. struct xb_find_info *info = data;
  265. if (!strcmp(xendev->nodename, info->nodename)) {
  266. info->dev = xendev;
  267. get_device(dev);
  268. return 1;
  269. }
  270. return 0;
  271. }
  272. static struct xenbus_device *xenbus_device_find(const char *nodename,
  273. struct bus_type *bus)
  274. {
  275. struct xb_find_info info = { .dev = NULL, .nodename = nodename };
  276. bus_for_each_dev(bus, NULL, &info, cmp_dev);
  277. return info.dev;
  278. }
  279. static int cleanup_dev(struct device *dev, void *data)
  280. {
  281. struct xenbus_device *xendev = to_xenbus_device(dev);
  282. struct xb_find_info *info = data;
  283. int len = strlen(info->nodename);
  284. DPRINTK("%s", info->nodename);
  285. /* Match the info->nodename path, or any subdirectory of that path. */
  286. if (strncmp(xendev->nodename, info->nodename, len))
  287. return 0;
  288. /* If the node name is longer, ensure it really is a subdirectory. */
  289. if ((strlen(xendev->nodename) > len) && (xendev->nodename[len] != '/'))
  290. return 0;
  291. info->dev = xendev;
  292. get_device(dev);
  293. return 1;
  294. }
  295. static void xenbus_cleanup_devices(const char *path, struct bus_type *bus)
  296. {
  297. struct xb_find_info info = { .nodename = path };
  298. do {
  299. info.dev = NULL;
  300. bus_for_each_dev(bus, NULL, &info, cleanup_dev);
  301. if (info.dev) {
  302. device_unregister(&info.dev->dev);
  303. put_device(&info.dev->dev);
  304. }
  305. } while (info.dev);
  306. }
  307. static void xenbus_dev_release(struct device *dev)
  308. {
  309. if (dev)
  310. kfree(to_xenbus_device(dev));
  311. }
  312. static ssize_t nodename_show(struct device *dev,
  313. struct device_attribute *attr, char *buf)
  314. {
  315. return sprintf(buf, "%s\n", to_xenbus_device(dev)->nodename);
  316. }
  317. static DEVICE_ATTR_RO(nodename);
  318. static ssize_t devtype_show(struct device *dev,
  319. struct device_attribute *attr, char *buf)
  320. {
  321. return sprintf(buf, "%s\n", to_xenbus_device(dev)->devicetype);
  322. }
  323. static DEVICE_ATTR_RO(devtype);
  324. static ssize_t modalias_show(struct device *dev,
  325. struct device_attribute *attr, char *buf)
  326. {
  327. return sprintf(buf, "%s:%s\n", dev->bus->name,
  328. to_xenbus_device(dev)->devicetype);
  329. }
  330. static DEVICE_ATTR_RO(modalias);
  331. static ssize_t state_show(struct device *dev,
  332. struct device_attribute *attr, char *buf)
  333. {
  334. return sprintf(buf, "%s\n",
  335. xenbus_strstate(to_xenbus_device(dev)->state));
  336. }
  337. static DEVICE_ATTR_RO(state);
  338. static struct attribute *xenbus_dev_attrs[] = {
  339. &dev_attr_nodename.attr,
  340. &dev_attr_devtype.attr,
  341. &dev_attr_modalias.attr,
  342. &dev_attr_state.attr,
  343. NULL,
  344. };
  345. static const struct attribute_group xenbus_dev_group = {
  346. .attrs = xenbus_dev_attrs,
  347. };
  348. const struct attribute_group *xenbus_dev_groups[] = {
  349. &xenbus_dev_group,
  350. NULL,
  351. };
  352. EXPORT_SYMBOL_GPL(xenbus_dev_groups);
  353. int xenbus_probe_node(struct xen_bus_type *bus,
  354. const char *type,
  355. const char *nodename)
  356. {
  357. char devname[XEN_BUS_ID_SIZE];
  358. int err;
  359. struct xenbus_device *xendev;
  360. size_t stringlen;
  361. char *tmpstring;
  362. enum xenbus_state state = xenbus_read_driver_state(nodename);
  363. if (state != XenbusStateInitialising) {
  364. /* Device is not new, so ignore it. This can happen if a
  365. device is going away after switching to Closed. */
  366. return 0;
  367. }
  368. stringlen = strlen(nodename) + 1 + strlen(type) + 1;
  369. xendev = kzalloc(sizeof(*xendev) + stringlen, GFP_KERNEL);
  370. if (!xendev)
  371. return -ENOMEM;
  372. xendev->state = XenbusStateInitialising;
  373. /* Copy the strings into the extra space. */
  374. tmpstring = (char *)(xendev + 1);
  375. strcpy(tmpstring, nodename);
  376. xendev->nodename = tmpstring;
  377. tmpstring += strlen(tmpstring) + 1;
  378. strcpy(tmpstring, type);
  379. xendev->devicetype = tmpstring;
  380. init_completion(&xendev->down);
  381. xendev->dev.bus = &bus->bus;
  382. xendev->dev.release = xenbus_dev_release;
  383. err = bus->get_bus_id(devname, xendev->nodename);
  384. if (err)
  385. goto fail;
  386. dev_set_name(&xendev->dev, "%s", devname);
  387. /* Register with generic device framework. */
  388. err = device_register(&xendev->dev);
  389. if (err) {
  390. put_device(&xendev->dev);
  391. xendev = NULL;
  392. goto fail;
  393. }
  394. return 0;
  395. fail:
  396. kfree(xendev);
  397. return err;
  398. }
  399. EXPORT_SYMBOL_GPL(xenbus_probe_node);
  400. static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type)
  401. {
  402. int err = 0;
  403. char **dir;
  404. unsigned int dir_n = 0;
  405. int i;
  406. dir = xenbus_directory(XBT_NIL, bus->root, type, &dir_n);
  407. if (IS_ERR(dir))
  408. return PTR_ERR(dir);
  409. for (i = 0; i < dir_n; i++) {
  410. err = bus->probe(bus, type, dir[i]);
  411. if (err)
  412. break;
  413. }
  414. kfree(dir);
  415. return err;
  416. }
  417. int xenbus_probe_devices(struct xen_bus_type *bus)
  418. {
  419. int err = 0;
  420. char **dir;
  421. unsigned int i, dir_n;
  422. dir = xenbus_directory(XBT_NIL, bus->root, "", &dir_n);
  423. if (IS_ERR(dir))
  424. return PTR_ERR(dir);
  425. for (i = 0; i < dir_n; i++) {
  426. err = xenbus_probe_device_type(bus, dir[i]);
  427. if (err)
  428. break;
  429. }
  430. kfree(dir);
  431. return err;
  432. }
  433. EXPORT_SYMBOL_GPL(xenbus_probe_devices);
  434. static unsigned int char_count(const char *str, char c)
  435. {
  436. unsigned int i, ret = 0;
  437. for (i = 0; str[i]; i++)
  438. if (str[i] == c)
  439. ret++;
  440. return ret;
  441. }
  442. static int strsep_len(const char *str, char c, unsigned int len)
  443. {
  444. unsigned int i;
  445. for (i = 0; str[i]; i++)
  446. if (str[i] == c) {
  447. if (len == 0)
  448. return i;
  449. len--;
  450. }
  451. return (len == 0) ? i : -ERANGE;
  452. }
  453. void xenbus_dev_changed(const char *node, struct xen_bus_type *bus)
  454. {
  455. int exists, rootlen;
  456. struct xenbus_device *dev;
  457. char type[XEN_BUS_ID_SIZE];
  458. const char *p, *root;
  459. if (char_count(node, '/') < 2)
  460. return;
  461. exists = xenbus_exists(XBT_NIL, node, "");
  462. if (!exists) {
  463. xenbus_cleanup_devices(node, &bus->bus);
  464. return;
  465. }
  466. /* backend/<type>/... or device/<type>/... */
  467. p = strchr(node, '/') + 1;
  468. snprintf(type, XEN_BUS_ID_SIZE, "%.*s", (int)strcspn(p, "/"), p);
  469. type[XEN_BUS_ID_SIZE-1] = '\0';
  470. rootlen = strsep_len(node, '/', bus->levels);
  471. if (rootlen < 0)
  472. return;
  473. root = kasprintf(GFP_KERNEL, "%.*s", rootlen, node);
  474. if (!root)
  475. return;
  476. dev = xenbus_device_find(root, &bus->bus);
  477. if (!dev)
  478. xenbus_probe_node(bus, type, root);
  479. else
  480. put_device(&dev->dev);
  481. kfree(root);
  482. }
  483. EXPORT_SYMBOL_GPL(xenbus_dev_changed);
  484. int xenbus_dev_suspend(struct device *dev)
  485. {
  486. int err = 0;
  487. struct xenbus_driver *drv;
  488. struct xenbus_device *xdev
  489. = 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);
  496. if (err)
  497. pr_warn("suspend %s failed: %i\n", dev_name(dev), err);
  498. return 0;
  499. }
  500. EXPORT_SYMBOL_GPL(xenbus_dev_suspend);
  501. int xenbus_dev_resume(struct device *dev)
  502. {
  503. int err;
  504. struct xenbus_driver *drv;
  505. struct xenbus_device *xdev
  506. = 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. pr_warn("resume (talk_to_otherend) %s failed: %i\n",
  514. dev_name(dev), err);
  515. return err;
  516. }
  517. xdev->state = XenbusStateInitialising;
  518. if (drv->resume) {
  519. err = drv->resume(xdev);
  520. if (err) {
  521. pr_warn("resume %s failed: %i\n", dev_name(dev), err);
  522. return err;
  523. }
  524. }
  525. err = watch_otherend(xdev);
  526. if (err) {
  527. pr_warn("resume (watch_otherend) %s failed: %d.\n",
  528. dev_name(dev), err);
  529. return err;
  530. }
  531. return 0;
  532. }
  533. EXPORT_SYMBOL_GPL(xenbus_dev_resume);
  534. int xenbus_dev_cancel(struct device *dev)
  535. {
  536. /* Do nothing */
  537. DPRINTK("cancel");
  538. return 0;
  539. }
  540. EXPORT_SYMBOL_GPL(xenbus_dev_cancel);
  541. /* A flag to determine if xenstored is 'ready' (i.e. has started) */
  542. int xenstored_ready;
  543. int register_xenstore_notifier(struct notifier_block *nb)
  544. {
  545. int ret = 0;
  546. if (xenstored_ready > 0)
  547. ret = nb->notifier_call(nb, 0, NULL);
  548. else
  549. blocking_notifier_chain_register(&xenstore_chain, nb);
  550. return ret;
  551. }
  552. EXPORT_SYMBOL_GPL(register_xenstore_notifier);
  553. void unregister_xenstore_notifier(struct notifier_block *nb)
  554. {
  555. blocking_notifier_chain_unregister(&xenstore_chain, nb);
  556. }
  557. EXPORT_SYMBOL_GPL(unregister_xenstore_notifier);
  558. static void xenbus_probe(void)
  559. {
  560. xenstored_ready = 1;
  561. /*
  562. * In the HVM case, xenbus_init() deferred its call to
  563. * xs_init() in case callbacks were not operational yet.
  564. * So do it now.
  565. */
  566. if (xen_store_domain_type == XS_HVM)
  567. xs_init();
  568. /* Notify others that xenstore is up */
  569. blocking_notifier_call_chain(&xenstore_chain, 0, NULL);
  570. }
  571. /*
  572. * Returns true when XenStore init must be deferred in order to
  573. * allow the PCI platform device to be initialised, before we
  574. * can actually have event channel interrupts working.
  575. */
  576. static bool xs_hvm_defer_init_for_callback(void)
  577. {
  578. #ifdef CONFIG_XEN_PVHVM
  579. return xen_store_domain_type == XS_HVM &&
  580. !xen_have_vector_callback;
  581. #else
  582. return false;
  583. #endif
  584. }
  585. static int xenbus_probe_thread(void *unused)
  586. {
  587. DEFINE_WAIT(w);
  588. /*
  589. * We actually just want to wait for *any* trigger of xb_waitq,
  590. * and run xenbus_probe() the moment it occurs.
  591. */
  592. prepare_to_wait(&xb_waitq, &w, TASK_INTERRUPTIBLE);
  593. schedule();
  594. finish_wait(&xb_waitq, &w);
  595. DPRINTK("probing");
  596. xenbus_probe();
  597. return 0;
  598. }
  599. static int __init xenbus_probe_initcall(void)
  600. {
  601. /*
  602. * Probe XenBus here in the XS_PV case, and also XS_HVM unless we
  603. * need to wait for the platform PCI device to come up.
  604. */
  605. if (xen_store_domain_type == XS_PV ||
  606. (xen_store_domain_type == XS_HVM &&
  607. !xs_hvm_defer_init_for_callback()))
  608. xenbus_probe();
  609. /*
  610. * For XS_LOCAL, spawn a thread which will wait for xenstored
  611. * or a xenstore-stubdom to be started, then probe. It will be
  612. * triggered when communication starts happening, by waiting
  613. * on xb_waitq.
  614. */
  615. if (xen_store_domain_type == XS_LOCAL) {
  616. struct task_struct *probe_task;
  617. probe_task = kthread_run(xenbus_probe_thread, NULL,
  618. "xenbus_probe");
  619. if (IS_ERR(probe_task))
  620. return PTR_ERR(probe_task);
  621. }
  622. return 0;
  623. }
  624. device_initcall(xenbus_probe_initcall);
  625. int xen_set_callback_via(uint64_t via)
  626. {
  627. struct xen_hvm_param a;
  628. int ret;
  629. a.domid = DOMID_SELF;
  630. a.index = HVM_PARAM_CALLBACK_IRQ;
  631. a.value = via;
  632. ret = HYPERVISOR_hvm_op(HVMOP_set_param, &a);
  633. if (ret)
  634. return ret;
  635. /*
  636. * If xenbus_probe_initcall() deferred the xenbus_probe()
  637. * due to the callback not functioning yet, we can do it now.
  638. */
  639. if (!xenstored_ready && xs_hvm_defer_init_for_callback())
  640. xenbus_probe();
  641. return ret;
  642. }
  643. EXPORT_SYMBOL_GPL(xen_set_callback_via);
  644. /* Set up event channel for xenstored which is run as a local process
  645. * (this is normally used only in dom0)
  646. */
  647. static int __init xenstored_local_init(void)
  648. {
  649. int err = -ENOMEM;
  650. unsigned long page = 0;
  651. struct evtchn_alloc_unbound alloc_unbound;
  652. /* Allocate Xenstore page */
  653. page = get_zeroed_page(GFP_KERNEL);
  654. if (!page)
  655. goto out_err;
  656. xen_store_gfn = virt_to_gfn((void *)page);
  657. /* Next allocate a local port which xenstored can bind to */
  658. alloc_unbound.dom = DOMID_SELF;
  659. alloc_unbound.remote_dom = DOMID_SELF;
  660. err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
  661. &alloc_unbound);
  662. if (err == -ENOSYS)
  663. goto out_err;
  664. BUG_ON(err);
  665. xen_store_evtchn = alloc_unbound.port;
  666. return 0;
  667. out_err:
  668. if (page != 0)
  669. free_page(page);
  670. return err;
  671. }
  672. static int xenbus_resume_cb(struct notifier_block *nb,
  673. unsigned long action, void *data)
  674. {
  675. int err = 0;
  676. if (xen_hvm_domain()) {
  677. uint64_t v = 0;
  678. err = hvm_get_parameter(HVM_PARAM_STORE_EVTCHN, &v);
  679. if (!err && v)
  680. xen_store_evtchn = v;
  681. else
  682. pr_warn("Cannot update xenstore event channel: %d\n",
  683. err);
  684. } else
  685. xen_store_evtchn = xen_start_info->store_evtchn;
  686. return err;
  687. }
  688. static struct notifier_block xenbus_resume_nb = {
  689. .notifier_call = xenbus_resume_cb,
  690. };
  691. static int __init xenbus_init(void)
  692. {
  693. int err = 0;
  694. uint64_t v = 0;
  695. xen_store_domain_type = XS_UNKNOWN;
  696. if (!xen_domain())
  697. return -ENODEV;
  698. xenbus_ring_ops_init();
  699. if (xen_pv_domain())
  700. xen_store_domain_type = XS_PV;
  701. if (xen_hvm_domain())
  702. xen_store_domain_type = XS_HVM;
  703. if (xen_hvm_domain() && xen_initial_domain())
  704. xen_store_domain_type = XS_LOCAL;
  705. if (xen_pv_domain() && !xen_start_info->store_evtchn)
  706. xen_store_domain_type = XS_LOCAL;
  707. if (xen_pv_domain() && xen_start_info->store_evtchn)
  708. xenstored_ready = 1;
  709. switch (xen_store_domain_type) {
  710. case XS_LOCAL:
  711. err = xenstored_local_init();
  712. if (err)
  713. goto out_error;
  714. xen_store_interface = gfn_to_virt(xen_store_gfn);
  715. break;
  716. case XS_PV:
  717. xen_store_evtchn = xen_start_info->store_evtchn;
  718. xen_store_gfn = xen_start_info->store_mfn;
  719. xen_store_interface = gfn_to_virt(xen_store_gfn);
  720. break;
  721. case XS_HVM:
  722. err = hvm_get_parameter(HVM_PARAM_STORE_EVTCHN, &v);
  723. if (err)
  724. goto out_error;
  725. xen_store_evtchn = (int)v;
  726. err = hvm_get_parameter(HVM_PARAM_STORE_PFN, &v);
  727. if (err)
  728. goto out_error;
  729. xen_store_gfn = (unsigned long)v;
  730. xen_store_interface =
  731. xen_remap(xen_store_gfn << XEN_PAGE_SHIFT,
  732. XEN_PAGE_SIZE);
  733. break;
  734. default:
  735. pr_warn("Xenstore state unknown\n");
  736. break;
  737. }
  738. /*
  739. * HVM domains may not have a functional callback yet. In that
  740. * case let xs_init() be called from xenbus_probe(), which will
  741. * get invoked at an appropriate time.
  742. */
  743. if (xen_store_domain_type != XS_HVM) {
  744. err = xs_init();
  745. if (err) {
  746. pr_warn("Error initializing xenstore comms: %i\n", err);
  747. goto out_error;
  748. }
  749. }
  750. if ((xen_store_domain_type != XS_LOCAL) &&
  751. (xen_store_domain_type != XS_UNKNOWN))
  752. xen_resume_notifier_register(&xenbus_resume_nb);
  753. #ifdef CONFIG_XEN_COMPAT_XENFS
  754. /*
  755. * Create xenfs mountpoint in /proc for compatibility with
  756. * utilities that expect to find "xenbus" under "/proc/xen".
  757. */
  758. proc_create_mount_point("xen");
  759. #endif
  760. out_error:
  761. return err;
  762. }
  763. postcore_initcall(xenbus_init);
  764. MODULE_LICENSE("GPL");