xenbus_probe.c 23 KB

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