xenbus_probe.c 22 KB

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