xenbus_probe.c 23 KB

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