xenbus_probe.c 23 KB

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