xenbus_probe.c 24 KB

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