xenbus_probe.c 23 KB

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