xenbus_probe.c 23 KB

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