xenbus_probe.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936
  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 DPRINTK(fmt, args...) \
  33. pr_debug("xenbus_probe (%s:%d) " fmt ".\n", \
  34. __func__, __LINE__, ##args)
  35. #include <linux/kernel.h>
  36. #include <linux/err.h>
  37. #include <linux/string.h>
  38. #include <linux/ctype.h>
  39. #include <linux/fcntl.h>
  40. #include <linux/mm.h>
  41. #include <linux/notifier.h>
  42. #include <linux/kthread.h>
  43. #include <linux/mutex.h>
  44. #include <linux/io.h>
  45. #include <asm/page.h>
  46. #include <asm/pgtable.h>
  47. #include <asm/xen/hypervisor.h>
  48. #include <xen/xenbus.h>
  49. #include <xen/events.h>
  50. #include <xen/page.h>
  51. #include "xenbus_comms.h"
  52. #include "xenbus_probe.h"
  53. int xen_store_evtchn;
  54. struct xenstore_domain_interface *xen_store_interface;
  55. static unsigned long xen_store_mfn;
  56. static BLOCKING_NOTIFIER_HEAD(xenstore_chain);
  57. static void wait_for_devices(struct xenbus_driver *xendrv);
  58. static int xenbus_probe_frontend(const char *type, const char *name);
  59. static void xenbus_dev_shutdown(struct device *_dev);
  60. /* If something in array of ids matches this device, return it. */
  61. static const struct xenbus_device_id *
  62. match_device(const struct xenbus_device_id *arr, struct xenbus_device *dev)
  63. {
  64. for (; *arr->devicetype != '\0'; arr++) {
  65. if (!strcmp(arr->devicetype, dev->devicetype))
  66. return arr;
  67. }
  68. return NULL;
  69. }
  70. int xenbus_match(struct device *_dev, struct device_driver *_drv)
  71. {
  72. struct xenbus_driver *drv = to_xenbus_driver(_drv);
  73. if (!drv->ids)
  74. return 0;
  75. return match_device(drv->ids, to_xenbus_device(_dev)) != NULL;
  76. }
  77. /* device/<type>/<id> => <type>-<id> */
  78. static int frontend_bus_id(char bus_id[BUS_ID_SIZE], const char *nodename)
  79. {
  80. nodename = strchr(nodename, '/');
  81. if (!nodename || strlen(nodename + 1) >= BUS_ID_SIZE) {
  82. printk(KERN_WARNING "XENBUS: bad frontend %s\n", nodename);
  83. return -EINVAL;
  84. }
  85. strlcpy(bus_id, nodename + 1, BUS_ID_SIZE);
  86. if (!strchr(bus_id, '/')) {
  87. printk(KERN_WARNING "XENBUS: bus_id %s no slash\n", bus_id);
  88. return -EINVAL;
  89. }
  90. *strchr(bus_id, '/') = '-';
  91. return 0;
  92. }
  93. static void free_otherend_details(struct xenbus_device *dev)
  94. {
  95. kfree(dev->otherend);
  96. dev->otherend = NULL;
  97. }
  98. static void free_otherend_watch(struct xenbus_device *dev)
  99. {
  100. if (dev->otherend_watch.node) {
  101. unregister_xenbus_watch(&dev->otherend_watch);
  102. kfree(dev->otherend_watch.node);
  103. dev->otherend_watch.node = NULL;
  104. }
  105. }
  106. int read_otherend_details(struct xenbus_device *xendev,
  107. char *id_node, char *path_node)
  108. {
  109. int err = xenbus_gather(XBT_NIL, xendev->nodename,
  110. id_node, "%i", &xendev->otherend_id,
  111. path_node, NULL, &xendev->otherend,
  112. NULL);
  113. if (err) {
  114. xenbus_dev_fatal(xendev, err,
  115. "reading other end details from %s",
  116. xendev->nodename);
  117. return err;
  118. }
  119. if (strlen(xendev->otherend) == 0 ||
  120. !xenbus_exists(XBT_NIL, xendev->otherend, "")) {
  121. xenbus_dev_fatal(xendev, -ENOENT,
  122. "unable to read other end from %s. "
  123. "missing or inaccessible.",
  124. xendev->nodename);
  125. free_otherend_details(xendev);
  126. return -ENOENT;
  127. }
  128. return 0;
  129. }
  130. static int read_backend_details(struct xenbus_device *xendev)
  131. {
  132. return read_otherend_details(xendev, "backend-id", "backend");
  133. }
  134. /* Bus type for frontend drivers. */
  135. static struct xen_bus_type xenbus_frontend = {
  136. .root = "device",
  137. .levels = 2, /* device/type/<id> */
  138. .get_bus_id = frontend_bus_id,
  139. .probe = xenbus_probe_frontend,
  140. .bus = {
  141. .name = "xen",
  142. .match = xenbus_match,
  143. .probe = xenbus_dev_probe,
  144. .remove = xenbus_dev_remove,
  145. .shutdown = xenbus_dev_shutdown,
  146. },
  147. };
  148. static void otherend_changed(struct xenbus_watch *watch,
  149. const char **vec, unsigned int len)
  150. {
  151. struct xenbus_device *dev =
  152. container_of(watch, struct xenbus_device, otherend_watch);
  153. struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
  154. enum xenbus_state state;
  155. /* Protect us against watches firing on old details when the otherend
  156. details change, say immediately after a resume. */
  157. if (!dev->otherend ||
  158. strncmp(dev->otherend, vec[XS_WATCH_PATH],
  159. strlen(dev->otherend))) {
  160. dev_dbg(&dev->dev, "Ignoring watch at %s\n",
  161. vec[XS_WATCH_PATH]);
  162. return;
  163. }
  164. state = xenbus_read_driver_state(dev->otherend);
  165. dev_dbg(&dev->dev, "state is %d, (%s), %s, %s\n",
  166. state, xenbus_strstate(state), dev->otherend_watch.node,
  167. vec[XS_WATCH_PATH]);
  168. /*
  169. * Ignore xenbus transitions during shutdown. This prevents us doing
  170. * work that can fail e.g., when the rootfs is gone.
  171. */
  172. if (system_state > SYSTEM_RUNNING) {
  173. struct xen_bus_type *bus = bus;
  174. bus = container_of(dev->dev.bus, struct xen_bus_type, bus);
  175. /* If we're frontend, drive the state machine to Closed. */
  176. /* This should cause the backend to release our resources. */
  177. if ((bus == &xenbus_frontend) && (state == XenbusStateClosing))
  178. xenbus_frontend_closed(dev);
  179. return;
  180. }
  181. if (drv->otherend_changed)
  182. drv->otherend_changed(dev, state);
  183. }
  184. static int talk_to_otherend(struct xenbus_device *dev)
  185. {
  186. struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
  187. free_otherend_watch(dev);
  188. free_otherend_details(dev);
  189. return drv->read_otherend_details(dev);
  190. }
  191. static int watch_otherend(struct xenbus_device *dev)
  192. {
  193. return xenbus_watch_pathfmt(dev, &dev->otherend_watch, otherend_changed,
  194. "%s/%s", dev->otherend, "state");
  195. }
  196. int xenbus_dev_probe(struct device *_dev)
  197. {
  198. struct xenbus_device *dev = to_xenbus_device(_dev);
  199. struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
  200. const struct xenbus_device_id *id;
  201. int err;
  202. DPRINTK("%s", dev->nodename);
  203. if (!drv->probe) {
  204. err = -ENODEV;
  205. goto fail;
  206. }
  207. id = match_device(drv->ids, dev);
  208. if (!id) {
  209. err = -ENODEV;
  210. goto fail;
  211. }
  212. err = talk_to_otherend(dev);
  213. if (err) {
  214. dev_warn(&dev->dev, "talk_to_otherend on %s failed.\n",
  215. dev->nodename);
  216. return err;
  217. }
  218. err = drv->probe(dev, id);
  219. if (err)
  220. goto fail;
  221. err = watch_otherend(dev);
  222. if (err) {
  223. dev_warn(&dev->dev, "watch_otherend on %s failed.\n",
  224. dev->nodename);
  225. return err;
  226. }
  227. return 0;
  228. fail:
  229. xenbus_dev_error(dev, err, "xenbus_dev_probe on %s", dev->nodename);
  230. xenbus_switch_state(dev, XenbusStateClosed);
  231. return -ENODEV;
  232. }
  233. int xenbus_dev_remove(struct device *_dev)
  234. {
  235. struct xenbus_device *dev = to_xenbus_device(_dev);
  236. struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
  237. DPRINTK("%s", dev->nodename);
  238. free_otherend_watch(dev);
  239. free_otherend_details(dev);
  240. if (drv->remove)
  241. drv->remove(dev);
  242. xenbus_switch_state(dev, XenbusStateClosed);
  243. return 0;
  244. }
  245. static void xenbus_dev_shutdown(struct device *_dev)
  246. {
  247. struct xenbus_device *dev = to_xenbus_device(_dev);
  248. unsigned long timeout = 5*HZ;
  249. DPRINTK("%s", dev->nodename);
  250. get_device(&dev->dev);
  251. if (dev->state != XenbusStateConnected) {
  252. printk(KERN_INFO "%s: %s: %s != Connected, skipping\n", __func__,
  253. dev->nodename, xenbus_strstate(dev->state));
  254. goto out;
  255. }
  256. xenbus_switch_state(dev, XenbusStateClosing);
  257. timeout = wait_for_completion_timeout(&dev->down, timeout);
  258. if (!timeout)
  259. printk(KERN_INFO "%s: %s timeout closing device\n",
  260. __func__, dev->nodename);
  261. out:
  262. put_device(&dev->dev);
  263. }
  264. int xenbus_register_driver_common(struct xenbus_driver *drv,
  265. struct xen_bus_type *bus,
  266. struct module *owner,
  267. const char *mod_name)
  268. {
  269. drv->driver.name = drv->name;
  270. drv->driver.bus = &bus->bus;
  271. drv->driver.owner = owner;
  272. drv->driver.mod_name = mod_name;
  273. return driver_register(&drv->driver);
  274. }
  275. int __xenbus_register_frontend(struct xenbus_driver *drv,
  276. struct module *owner, const char *mod_name)
  277. {
  278. int ret;
  279. drv->read_otherend_details = read_backend_details;
  280. ret = xenbus_register_driver_common(drv, &xenbus_frontend,
  281. owner, mod_name);
  282. if (ret)
  283. return ret;
  284. /* If this driver is loaded as a module wait for devices to attach. */
  285. wait_for_devices(drv);
  286. return 0;
  287. }
  288. EXPORT_SYMBOL_GPL(__xenbus_register_frontend);
  289. void xenbus_unregister_driver(struct xenbus_driver *drv)
  290. {
  291. driver_unregister(&drv->driver);
  292. }
  293. EXPORT_SYMBOL_GPL(xenbus_unregister_driver);
  294. struct xb_find_info
  295. {
  296. struct xenbus_device *dev;
  297. const char *nodename;
  298. };
  299. static int cmp_dev(struct device *dev, void *data)
  300. {
  301. struct xenbus_device *xendev = to_xenbus_device(dev);
  302. struct xb_find_info *info = data;
  303. if (!strcmp(xendev->nodename, info->nodename)) {
  304. info->dev = xendev;
  305. get_device(dev);
  306. return 1;
  307. }
  308. return 0;
  309. }
  310. struct xenbus_device *xenbus_device_find(const char *nodename,
  311. struct bus_type *bus)
  312. {
  313. struct xb_find_info info = { .dev = NULL, .nodename = nodename };
  314. bus_for_each_dev(bus, NULL, &info, cmp_dev);
  315. return info.dev;
  316. }
  317. static int cleanup_dev(struct device *dev, void *data)
  318. {
  319. struct xenbus_device *xendev = to_xenbus_device(dev);
  320. struct xb_find_info *info = data;
  321. int len = strlen(info->nodename);
  322. DPRINTK("%s", info->nodename);
  323. /* Match the info->nodename path, or any subdirectory of that path. */
  324. if (strncmp(xendev->nodename, info->nodename, len))
  325. return 0;
  326. /* If the node name is longer, ensure it really is a subdirectory. */
  327. if ((strlen(xendev->nodename) > len) && (xendev->nodename[len] != '/'))
  328. return 0;
  329. info->dev = xendev;
  330. get_device(dev);
  331. return 1;
  332. }
  333. static void xenbus_cleanup_devices(const char *path, struct bus_type *bus)
  334. {
  335. struct xb_find_info info = { .nodename = path };
  336. do {
  337. info.dev = NULL;
  338. bus_for_each_dev(bus, NULL, &info, cleanup_dev);
  339. if (info.dev) {
  340. device_unregister(&info.dev->dev);
  341. put_device(&info.dev->dev);
  342. }
  343. } while (info.dev);
  344. }
  345. static void xenbus_dev_release(struct device *dev)
  346. {
  347. if (dev)
  348. kfree(to_xenbus_device(dev));
  349. }
  350. static ssize_t xendev_show_nodename(struct device *dev,
  351. struct device_attribute *attr, char *buf)
  352. {
  353. return sprintf(buf, "%s\n", to_xenbus_device(dev)->nodename);
  354. }
  355. DEVICE_ATTR(nodename, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_nodename, NULL);
  356. static ssize_t xendev_show_devtype(struct device *dev,
  357. struct device_attribute *attr, char *buf)
  358. {
  359. return sprintf(buf, "%s\n", to_xenbus_device(dev)->devicetype);
  360. }
  361. DEVICE_ATTR(devtype, S_IRUSR | S_IRGRP | S_IROTH, xendev_show_devtype, NULL);
  362. int xenbus_probe_node(struct xen_bus_type *bus,
  363. const char *type,
  364. const char *nodename)
  365. {
  366. int err;
  367. struct xenbus_device *xendev;
  368. size_t stringlen;
  369. char *tmpstring;
  370. enum xenbus_state state = xenbus_read_driver_state(nodename);
  371. if (state != XenbusStateInitialising) {
  372. /* Device is not new, so ignore it. This can happen if a
  373. device is going away after switching to Closed. */
  374. return 0;
  375. }
  376. stringlen = strlen(nodename) + 1 + strlen(type) + 1;
  377. xendev = kzalloc(sizeof(*xendev) + stringlen, GFP_KERNEL);
  378. if (!xendev)
  379. return -ENOMEM;
  380. xendev->state = XenbusStateInitialising;
  381. /* Copy the strings into the extra space. */
  382. tmpstring = (char *)(xendev + 1);
  383. strcpy(tmpstring, nodename);
  384. xendev->nodename = tmpstring;
  385. tmpstring += strlen(tmpstring) + 1;
  386. strcpy(tmpstring, type);
  387. xendev->devicetype = tmpstring;
  388. init_completion(&xendev->down);
  389. xendev->dev.bus = &bus->bus;
  390. xendev->dev.release = xenbus_dev_release;
  391. err = bus->get_bus_id(xendev->dev.bus_id, xendev->nodename);
  392. if (err)
  393. goto fail;
  394. /* Register with generic device framework. */
  395. err = device_register(&xendev->dev);
  396. if (err)
  397. goto fail;
  398. err = device_create_file(&xendev->dev, &dev_attr_nodename);
  399. if (err)
  400. goto fail_unregister;
  401. err = device_create_file(&xendev->dev, &dev_attr_devtype);
  402. if (err)
  403. goto fail_remove_file;
  404. return 0;
  405. fail_remove_file:
  406. device_remove_file(&xendev->dev, &dev_attr_nodename);
  407. fail_unregister:
  408. device_unregister(&xendev->dev);
  409. fail:
  410. kfree(xendev);
  411. return err;
  412. }
  413. /* device/<typename>/<name> */
  414. static int xenbus_probe_frontend(const char *type, const char *name)
  415. {
  416. char *nodename;
  417. int err;
  418. nodename = kasprintf(GFP_KERNEL, "%s/%s/%s",
  419. xenbus_frontend.root, type, name);
  420. if (!nodename)
  421. return -ENOMEM;
  422. DPRINTK("%s", nodename);
  423. err = xenbus_probe_node(&xenbus_frontend, type, nodename);
  424. kfree(nodename);
  425. return err;
  426. }
  427. static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type)
  428. {
  429. int err = 0;
  430. char **dir;
  431. unsigned int dir_n = 0;
  432. int i;
  433. dir = xenbus_directory(XBT_NIL, bus->root, type, &dir_n);
  434. if (IS_ERR(dir))
  435. return PTR_ERR(dir);
  436. for (i = 0; i < dir_n; i++) {
  437. err = bus->probe(type, dir[i]);
  438. if (err)
  439. break;
  440. }
  441. kfree(dir);
  442. return err;
  443. }
  444. int xenbus_probe_devices(struct xen_bus_type *bus)
  445. {
  446. int err = 0;
  447. char **dir;
  448. unsigned int i, dir_n;
  449. dir = xenbus_directory(XBT_NIL, bus->root, "", &dir_n);
  450. if (IS_ERR(dir))
  451. return PTR_ERR(dir);
  452. for (i = 0; i < dir_n; i++) {
  453. err = xenbus_probe_device_type(bus, dir[i]);
  454. if (err)
  455. break;
  456. }
  457. kfree(dir);
  458. return err;
  459. }
  460. static unsigned int char_count(const char *str, char c)
  461. {
  462. unsigned int i, ret = 0;
  463. for (i = 0; str[i]; i++)
  464. if (str[i] == c)
  465. ret++;
  466. return ret;
  467. }
  468. static int strsep_len(const char *str, char c, unsigned int len)
  469. {
  470. unsigned int i;
  471. for (i = 0; str[i]; i++)
  472. if (str[i] == c) {
  473. if (len == 0)
  474. return i;
  475. len--;
  476. }
  477. return (len == 0) ? i : -ERANGE;
  478. }
  479. void xenbus_dev_changed(const char *node, struct xen_bus_type *bus)
  480. {
  481. int exists, rootlen;
  482. struct xenbus_device *dev;
  483. char type[BUS_ID_SIZE];
  484. const char *p, *root;
  485. if (char_count(node, '/') < 2)
  486. return;
  487. exists = xenbus_exists(XBT_NIL, node, "");
  488. if (!exists) {
  489. xenbus_cleanup_devices(node, &bus->bus);
  490. return;
  491. }
  492. /* backend/<type>/... or device/<type>/... */
  493. p = strchr(node, '/') + 1;
  494. snprintf(type, BUS_ID_SIZE, "%.*s", (int)strcspn(p, "/"), p);
  495. type[BUS_ID_SIZE-1] = '\0';
  496. rootlen = strsep_len(node, '/', bus->levels);
  497. if (rootlen < 0)
  498. return;
  499. root = kasprintf(GFP_KERNEL, "%.*s", rootlen, node);
  500. if (!root)
  501. return;
  502. dev = xenbus_device_find(root, &bus->bus);
  503. if (!dev)
  504. xenbus_probe_node(bus, type, root);
  505. else
  506. put_device(&dev->dev);
  507. kfree(root);
  508. }
  509. static void frontend_changed(struct xenbus_watch *watch,
  510. const char **vec, unsigned int len)
  511. {
  512. DPRINTK("");
  513. xenbus_dev_changed(vec[XS_WATCH_PATH], &xenbus_frontend);
  514. }
  515. /* We watch for devices appearing and vanishing. */
  516. static struct xenbus_watch fe_watch = {
  517. .node = "device",
  518. .callback = frontend_changed,
  519. };
  520. static int suspend_dev(struct device *dev, void *data)
  521. {
  522. int err = 0;
  523. struct xenbus_driver *drv;
  524. struct xenbus_device *xdev;
  525. DPRINTK("");
  526. if (dev->driver == NULL)
  527. return 0;
  528. drv = to_xenbus_driver(dev->driver);
  529. xdev = container_of(dev, struct xenbus_device, dev);
  530. if (drv->suspend)
  531. err = drv->suspend(xdev);
  532. if (err)
  533. printk(KERN_WARNING
  534. "xenbus: suspend %s failed: %i\n", dev->bus_id, err);
  535. return 0;
  536. }
  537. static int suspend_cancel_dev(struct device *dev, void *data)
  538. {
  539. int err = 0;
  540. struct xenbus_driver *drv;
  541. struct xenbus_device *xdev;
  542. DPRINTK("");
  543. if (dev->driver == NULL)
  544. return 0;
  545. drv = to_xenbus_driver(dev->driver);
  546. xdev = container_of(dev, struct xenbus_device, dev);
  547. if (drv->suspend_cancel)
  548. err = drv->suspend_cancel(xdev);
  549. if (err)
  550. printk(KERN_WARNING
  551. "xenbus: suspend_cancel %s failed: %i\n",
  552. dev->bus_id, err);
  553. return 0;
  554. }
  555. static int resume_dev(struct device *dev, void *data)
  556. {
  557. int err;
  558. struct xenbus_driver *drv;
  559. struct xenbus_device *xdev;
  560. DPRINTK("");
  561. if (dev->driver == NULL)
  562. return 0;
  563. drv = to_xenbus_driver(dev->driver);
  564. xdev = container_of(dev, struct xenbus_device, dev);
  565. err = talk_to_otherend(xdev);
  566. if (err) {
  567. printk(KERN_WARNING
  568. "xenbus: resume (talk_to_otherend) %s failed: %i\n",
  569. dev->bus_id, err);
  570. return err;
  571. }
  572. xdev->state = XenbusStateInitialising;
  573. if (drv->resume) {
  574. err = drv->resume(xdev);
  575. if (err) {
  576. printk(KERN_WARNING
  577. "xenbus: resume %s failed: %i\n",
  578. dev->bus_id, err);
  579. return err;
  580. }
  581. }
  582. err = watch_otherend(xdev);
  583. if (err) {
  584. printk(KERN_WARNING
  585. "xenbus_probe: resume (watch_otherend) %s failed: "
  586. "%d.\n", dev->bus_id, err);
  587. return err;
  588. }
  589. return 0;
  590. }
  591. void xenbus_suspend(void)
  592. {
  593. DPRINTK("");
  594. bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, suspend_dev);
  595. xenbus_backend_suspend(suspend_dev);
  596. xs_suspend();
  597. }
  598. EXPORT_SYMBOL_GPL(xenbus_suspend);
  599. void xenbus_resume(void)
  600. {
  601. xb_init_comms();
  602. xs_resume();
  603. bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, resume_dev);
  604. xenbus_backend_resume(resume_dev);
  605. }
  606. EXPORT_SYMBOL_GPL(xenbus_resume);
  607. void xenbus_suspend_cancel(void)
  608. {
  609. xs_suspend_cancel();
  610. bus_for_each_dev(&xenbus_frontend.bus, NULL, NULL, suspend_cancel_dev);
  611. xenbus_backend_resume(suspend_cancel_dev);
  612. }
  613. EXPORT_SYMBOL_GPL(xenbus_suspend_cancel);
  614. /* A flag to determine if xenstored is 'ready' (i.e. has started) */
  615. int xenstored_ready = 0;
  616. int register_xenstore_notifier(struct notifier_block *nb)
  617. {
  618. int ret = 0;
  619. if (xenstored_ready > 0)
  620. ret = nb->notifier_call(nb, 0, NULL);
  621. else
  622. blocking_notifier_chain_register(&xenstore_chain, nb);
  623. return ret;
  624. }
  625. EXPORT_SYMBOL_GPL(register_xenstore_notifier);
  626. void unregister_xenstore_notifier(struct notifier_block *nb)
  627. {
  628. blocking_notifier_chain_unregister(&xenstore_chain, nb);
  629. }
  630. EXPORT_SYMBOL_GPL(unregister_xenstore_notifier);
  631. void xenbus_probe(struct work_struct *unused)
  632. {
  633. BUG_ON((xenstored_ready <= 0));
  634. /* Enumerate devices in xenstore and watch for changes. */
  635. xenbus_probe_devices(&xenbus_frontend);
  636. register_xenbus_watch(&fe_watch);
  637. xenbus_backend_probe_and_watch();
  638. /* Notify others that xenstore is up */
  639. blocking_notifier_call_chain(&xenstore_chain, 0, NULL);
  640. }
  641. static int __init xenbus_probe_init(void)
  642. {
  643. int err = 0;
  644. DPRINTK("");
  645. err = -ENODEV;
  646. if (!is_running_on_xen())
  647. goto out_error;
  648. /* Register ourselves with the kernel bus subsystem */
  649. err = bus_register(&xenbus_frontend.bus);
  650. if (err)
  651. goto out_error;
  652. err = xenbus_backend_bus_register();
  653. if (err)
  654. goto out_unreg_front;
  655. /*
  656. * Domain0 doesn't have a store_evtchn or store_mfn yet.
  657. */
  658. if (is_initial_xendomain()) {
  659. /* dom0 not yet supported */
  660. } else {
  661. xenstored_ready = 1;
  662. xen_store_evtchn = xen_start_info->store_evtchn;
  663. xen_store_mfn = xen_start_info->store_mfn;
  664. }
  665. xen_store_interface = mfn_to_virt(xen_store_mfn);
  666. /* Initialize the interface to xenstore. */
  667. err = xs_init();
  668. if (err) {
  669. printk(KERN_WARNING
  670. "XENBUS: Error initializing xenstore comms: %i\n", err);
  671. goto out_unreg_back;
  672. }
  673. if (!is_initial_xendomain())
  674. xenbus_probe(NULL);
  675. return 0;
  676. out_unreg_back:
  677. xenbus_backend_bus_unregister();
  678. out_unreg_front:
  679. bus_unregister(&xenbus_frontend.bus);
  680. out_error:
  681. return err;
  682. }
  683. postcore_initcall(xenbus_probe_init);
  684. MODULE_LICENSE("GPL");
  685. static int is_disconnected_device(struct device *dev, void *data)
  686. {
  687. struct xenbus_device *xendev = to_xenbus_device(dev);
  688. struct device_driver *drv = data;
  689. /*
  690. * A device with no driver will never connect. We care only about
  691. * devices which should currently be in the process of connecting.
  692. */
  693. if (!dev->driver)
  694. return 0;
  695. /* Is this search limited to a particular driver? */
  696. if (drv && (dev->driver != drv))
  697. return 0;
  698. return (xendev->state != XenbusStateConnected);
  699. }
  700. static int exists_disconnected_device(struct device_driver *drv)
  701. {
  702. return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
  703. is_disconnected_device);
  704. }
  705. static int print_device_status(struct device *dev, void *data)
  706. {
  707. struct xenbus_device *xendev = to_xenbus_device(dev);
  708. struct device_driver *drv = data;
  709. /* Is this operation limited to a particular driver? */
  710. if (drv && (dev->driver != drv))
  711. return 0;
  712. if (!dev->driver) {
  713. /* Information only: is this too noisy? */
  714. printk(KERN_INFO "XENBUS: Device with no driver: %s\n",
  715. xendev->nodename);
  716. } else if (xendev->state != XenbusStateConnected) {
  717. printk(KERN_WARNING "XENBUS: Timeout connecting "
  718. "to device: %s (state %d)\n",
  719. xendev->nodename, xendev->state);
  720. }
  721. return 0;
  722. }
  723. /* We only wait for device setup after most initcalls have run. */
  724. static int ready_to_wait_for_devices;
  725. /*
  726. * On a 10 second timeout, wait for all devices currently configured. We need
  727. * to do this to guarantee that the filesystems and / or network devices
  728. * needed for boot are available, before we can allow the boot to proceed.
  729. *
  730. * This needs to be on a late_initcall, to happen after the frontend device
  731. * drivers have been initialised, but before the root fs is mounted.
  732. *
  733. * A possible improvement here would be to have the tools add a per-device
  734. * flag to the store entry, indicating whether it is needed at boot time.
  735. * This would allow people who knew what they were doing to accelerate their
  736. * boot slightly, but of course needs tools or manual intervention to set up
  737. * those flags correctly.
  738. */
  739. static void wait_for_devices(struct xenbus_driver *xendrv)
  740. {
  741. unsigned long timeout = jiffies + 10*HZ;
  742. struct device_driver *drv = xendrv ? &xendrv->driver : NULL;
  743. if (!ready_to_wait_for_devices || !is_running_on_xen())
  744. return;
  745. while (exists_disconnected_device(drv)) {
  746. if (time_after(jiffies, timeout))
  747. break;
  748. schedule_timeout_interruptible(HZ/10);
  749. }
  750. bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
  751. print_device_status);
  752. }
  753. #ifndef MODULE
  754. static int __init boot_wait_for_devices(void)
  755. {
  756. ready_to_wait_for_devices = 1;
  757. wait_for_devices(NULL);
  758. return 0;
  759. }
  760. late_initcall(boot_wait_for_devices);
  761. #endif