xenbus_probe.c 24 KB

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