xenbus_probe.c 19 KB

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