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