xenbus_probe.c 25 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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. unsigned long page = 0;
  652. DPRINTK("");
  653. err = -ENODEV;
  654. if (!xen_domain())
  655. goto out_error;
  656. /* Register ourselves with the kernel bus subsystem */
  657. err = bus_register(&xenbus_frontend.bus);
  658. if (err)
  659. goto out_error;
  660. err = xenbus_backend_bus_register();
  661. if (err)
  662. goto out_unreg_front;
  663. /*
  664. * Domain0 doesn't have a store_evtchn or store_mfn yet.
  665. */
  666. if (xen_initial_domain()) {
  667. struct evtchn_alloc_unbound alloc_unbound;
  668. /* Allocate Xenstore page */
  669. page = get_zeroed_page(GFP_KERNEL);
  670. if (!page)
  671. goto out_error;
  672. xen_store_mfn = xen_start_info->store_mfn =
  673. pfn_to_mfn(virt_to_phys((void *)page) >>
  674. PAGE_SHIFT);
  675. /* Next allocate a local port which xenstored can bind to */
  676. alloc_unbound.dom = DOMID_SELF;
  677. alloc_unbound.remote_dom = 0;
  678. err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
  679. &alloc_unbound);
  680. if (err == -ENOSYS)
  681. goto out_error;
  682. BUG_ON(err);
  683. xen_store_evtchn = xen_start_info->store_evtchn =
  684. alloc_unbound.port;
  685. xen_store_interface = mfn_to_virt(xen_store_mfn);
  686. } else {
  687. if (xen_hvm_domain()) {
  688. uint64_t v = 0;
  689. err = hvm_get_parameter(HVM_PARAM_STORE_EVTCHN, &v);
  690. if (err)
  691. goto out_error;
  692. xen_store_evtchn = (int)v;
  693. err = hvm_get_parameter(HVM_PARAM_STORE_PFN, &v);
  694. if (err)
  695. goto out_error;
  696. xen_store_mfn = (unsigned long)v;
  697. xen_store_interface = ioremap(xen_store_mfn << PAGE_SHIFT, PAGE_SIZE);
  698. } else {
  699. xen_store_evtchn = xen_start_info->store_evtchn;
  700. xen_store_mfn = xen_start_info->store_mfn;
  701. xen_store_interface = mfn_to_virt(xen_store_mfn);
  702. xenstored_ready = 1;
  703. }
  704. }
  705. /* Initialize the interface to xenstore. */
  706. err = xs_init();
  707. if (err) {
  708. printk(KERN_WARNING
  709. "XENBUS: Error initializing xenstore comms: %i\n", err);
  710. goto out_unreg_back;
  711. }
  712. #ifdef CONFIG_XEN_COMPAT_XENFS
  713. /*
  714. * Create xenfs mountpoint in /proc for compatibility with
  715. * utilities that expect to find "xenbus" under "/proc/xen".
  716. */
  717. proc_mkdir("xen", NULL);
  718. #endif
  719. return 0;
  720. out_unreg_back:
  721. xenbus_backend_bus_unregister();
  722. out_unreg_front:
  723. bus_unregister(&xenbus_frontend.bus);
  724. out_error:
  725. if (page != 0)
  726. free_page(page);
  727. return err;
  728. }
  729. postcore_initcall(xenbus_init);
  730. MODULE_LICENSE("GPL");
  731. static int is_device_connecting(struct device *dev, void *data)
  732. {
  733. struct xenbus_device *xendev = to_xenbus_device(dev);
  734. struct device_driver *drv = data;
  735. struct xenbus_driver *xendrv;
  736. /*
  737. * A device with no driver will never connect. We care only about
  738. * devices which should currently be in the process of connecting.
  739. */
  740. if (!dev->driver)
  741. return 0;
  742. /* Is this search limited to a particular driver? */
  743. if (drv && (dev->driver != drv))
  744. return 0;
  745. xendrv = to_xenbus_driver(dev->driver);
  746. return (xendev->state < XenbusStateConnected ||
  747. (xendev->state == XenbusStateConnected &&
  748. xendrv->is_ready && !xendrv->is_ready(xendev)));
  749. }
  750. static int exists_connecting_device(struct device_driver *drv)
  751. {
  752. return bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
  753. is_device_connecting);
  754. }
  755. static int print_device_status(struct device *dev, void *data)
  756. {
  757. struct xenbus_device *xendev = to_xenbus_device(dev);
  758. struct device_driver *drv = data;
  759. /* Is this operation limited to a particular driver? */
  760. if (drv && (dev->driver != drv))
  761. return 0;
  762. if (!dev->driver) {
  763. /* Information only: is this too noisy? */
  764. printk(KERN_INFO "XENBUS: Device with no driver: %s\n",
  765. xendev->nodename);
  766. } else if (xendev->state < XenbusStateConnected) {
  767. enum xenbus_state rstate = XenbusStateUnknown;
  768. if (xendev->otherend)
  769. rstate = xenbus_read_driver_state(xendev->otherend);
  770. printk(KERN_WARNING "XENBUS: Timeout connecting "
  771. "to device: %s (local state %d, remote state %d)\n",
  772. xendev->nodename, xendev->state, rstate);
  773. }
  774. return 0;
  775. }
  776. /* We only wait for device setup after most initcalls have run. */
  777. static int ready_to_wait_for_devices;
  778. /*
  779. * On a 5-minute timeout, wait for all devices currently configured. We need
  780. * to do this to guarantee that the filesystems and / or network devices
  781. * needed for boot are available, before we can allow the boot to proceed.
  782. *
  783. * This needs to be on a late_initcall, to happen after the frontend device
  784. * drivers have been initialised, but before the root fs is mounted.
  785. *
  786. * A possible improvement here would be to have the tools add a per-device
  787. * flag to the store entry, indicating whether it is needed at boot time.
  788. * This would allow people who knew what they were doing to accelerate their
  789. * boot slightly, but of course needs tools or manual intervention to set up
  790. * those flags correctly.
  791. */
  792. static void wait_for_devices(struct xenbus_driver *xendrv)
  793. {
  794. unsigned long start = jiffies;
  795. struct device_driver *drv = xendrv ? &xendrv->driver : NULL;
  796. unsigned int seconds_waited = 0;
  797. if (!ready_to_wait_for_devices || !xen_domain())
  798. return;
  799. while (exists_connecting_device(drv)) {
  800. if (time_after(jiffies, start + (seconds_waited+5)*HZ)) {
  801. if (!seconds_waited)
  802. printk(KERN_WARNING "XENBUS: Waiting for "
  803. "devices to initialise: ");
  804. seconds_waited += 5;
  805. printk("%us...", 300 - seconds_waited);
  806. if (seconds_waited == 300)
  807. break;
  808. }
  809. schedule_timeout_interruptible(HZ/10);
  810. }
  811. if (seconds_waited)
  812. printk("\n");
  813. bus_for_each_dev(&xenbus_frontend.bus, NULL, drv,
  814. print_device_status);
  815. }
  816. #ifndef MODULE
  817. static int __init boot_wait_for_devices(void)
  818. {
  819. if (xen_hvm_domain() && !xen_platform_pci_unplug)
  820. return -ENODEV;
  821. ready_to_wait_for_devices = 1;
  822. wait_for_devices(NULL);
  823. return 0;
  824. }
  825. late_initcall(boot_wait_for_devices);
  826. #endif