xenbus_probe.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  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 pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  33. #define dev_fmt pr_fmt
  34. #define DPRINTK(fmt, args...) \
  35. pr_debug("xenbus_probe (%s:%d) " fmt ".\n", \
  36. __func__, __LINE__, ##args)
  37. #include <linux/kernel.h>
  38. #include <linux/err.h>
  39. #include <linux/string.h>
  40. #include <linux/ctype.h>
  41. #include <linux/fcntl.h>
  42. #include <linux/mm.h>
  43. #include <linux/proc_fs.h>
  44. #include <linux/notifier.h>
  45. #include <linux/kthread.h>
  46. #include <linux/mutex.h>
  47. #include <linux/io.h>
  48. #include <linux/slab.h>
  49. #include <linux/module.h>
  50. #include <asm/page.h>
  51. #include <asm/xen/hypervisor.h>
  52. #include <xen/xen.h>
  53. #include <xen/xenbus.h>
  54. #include <xen/events.h>
  55. #include <xen/xen-ops.h>
  56. #include <xen/page.h>
  57. #include <xen/hvm.h>
  58. #include "xenbus.h"
  59. static int xs_init_irq;
  60. int xen_store_evtchn;
  61. EXPORT_SYMBOL_GPL(xen_store_evtchn);
  62. struct xenstore_domain_interface *xen_store_interface;
  63. EXPORT_SYMBOL_GPL(xen_store_interface);
  64. enum xenstore_init xen_store_domain_type;
  65. EXPORT_SYMBOL_GPL(xen_store_domain_type);
  66. static unsigned long xen_store_gfn;
  67. static BLOCKING_NOTIFIER_HEAD(xenstore_chain);
  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. EXPORT_SYMBOL_GPL(xenbus_match);
  86. static void free_otherend_details(struct xenbus_device *dev)
  87. {
  88. kfree(dev->otherend);
  89. dev->otherend = NULL;
  90. }
  91. static void free_otherend_watch(struct xenbus_device *dev)
  92. {
  93. if (dev->otherend_watch.node) {
  94. unregister_xenbus_watch(&dev->otherend_watch);
  95. kfree(dev->otherend_watch.node);
  96. dev->otherend_watch.node = NULL;
  97. }
  98. }
  99. static int talk_to_otherend(struct xenbus_device *dev)
  100. {
  101. struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
  102. free_otherend_watch(dev);
  103. free_otherend_details(dev);
  104. return drv->read_otherend_details(dev);
  105. }
  106. static int watch_otherend(struct xenbus_device *dev)
  107. {
  108. struct xen_bus_type *bus =
  109. container_of(dev->dev.bus, struct xen_bus_type, bus);
  110. return xenbus_watch_pathfmt(dev, &dev->otherend_watch,
  111. bus->otherend_will_handle,
  112. bus->otherend_changed,
  113. "%s/%s", dev->otherend, "state");
  114. }
  115. int xenbus_read_otherend_details(struct xenbus_device *xendev,
  116. char *id_node, char *path_node)
  117. {
  118. int err = xenbus_gather(XBT_NIL, xendev->nodename,
  119. id_node, "%i", &xendev->otherend_id,
  120. path_node, NULL, &xendev->otherend,
  121. NULL);
  122. if (err) {
  123. xenbus_dev_fatal(xendev, err,
  124. "reading other end details from %s",
  125. xendev->nodename);
  126. return err;
  127. }
  128. if (strlen(xendev->otherend) == 0 ||
  129. !xenbus_exists(XBT_NIL, xendev->otherend, "")) {
  130. xenbus_dev_fatal(xendev, -ENOENT,
  131. "unable to read other end from %s. "
  132. "missing or inaccessible.",
  133. xendev->nodename);
  134. free_otherend_details(xendev);
  135. return -ENOENT;
  136. }
  137. return 0;
  138. }
  139. EXPORT_SYMBOL_GPL(xenbus_read_otherend_details);
  140. void xenbus_otherend_changed(struct xenbus_watch *watch,
  141. const char *path, const char *token,
  142. int ignore_on_shutdown)
  143. {
  144. struct xenbus_device *dev =
  145. container_of(watch, struct xenbus_device, otherend_watch);
  146. struct xenbus_driver *drv = to_xenbus_driver(dev->dev.driver);
  147. enum xenbus_state state;
  148. /* Protect us against watches firing on old details when the otherend
  149. details change, say immediately after a resume. */
  150. if (!dev->otherend ||
  151. strncmp(dev->otherend, path, strlen(dev->otherend))) {
  152. dev_dbg(&dev->dev, "Ignoring watch at %s\n", path);
  153. return;
  154. }
  155. state = xenbus_read_driver_state(dev->otherend);
  156. dev_dbg(&dev->dev, "state is %d, (%s), %s, %s\n",
  157. state, xenbus_strstate(state), dev->otherend_watch.node, path);
  158. /*
  159. * Ignore xenbus transitions during shutdown. This prevents us doing
  160. * work that can fail e.g., when the rootfs is gone.
  161. */
  162. if (system_state > SYSTEM_RUNNING) {
  163. if (ignore_on_shutdown && (state == XenbusStateClosing))
  164. xenbus_frontend_closed(dev);
  165. return;
  166. }
  167. if (drv->otherend_changed)
  168. drv->otherend_changed(dev, state);
  169. }
  170. EXPORT_SYMBOL_GPL(xenbus_otherend_changed);
  171. #define XENBUS_SHOW_STAT(name) \
  172. static ssize_t name##_show(struct device *_dev, \
  173. struct device_attribute *attr, \
  174. char *buf) \
  175. { \
  176. struct xenbus_device *dev = to_xenbus_device(_dev); \
  177. \
  178. return sprintf(buf, "%d\n", atomic_read(&dev->name)); \
  179. } \
  180. static DEVICE_ATTR_RO(name)
  181. XENBUS_SHOW_STAT(event_channels);
  182. XENBUS_SHOW_STAT(events);
  183. XENBUS_SHOW_STAT(spurious_events);
  184. XENBUS_SHOW_STAT(jiffies_eoi_delayed);
  185. static ssize_t spurious_threshold_show(struct device *_dev,
  186. struct device_attribute *attr,
  187. char *buf)
  188. {
  189. struct xenbus_device *dev = to_xenbus_device(_dev);
  190. return sprintf(buf, "%d\n", dev->spurious_threshold);
  191. }
  192. static ssize_t spurious_threshold_store(struct device *_dev,
  193. struct device_attribute *attr,
  194. const char *buf, size_t count)
  195. {
  196. struct xenbus_device *dev = to_xenbus_device(_dev);
  197. unsigned int val;
  198. ssize_t ret;
  199. ret = kstrtouint(buf, 0, &val);
  200. if (ret)
  201. return ret;
  202. dev->spurious_threshold = val;
  203. return count;
  204. }
  205. static DEVICE_ATTR_RW(spurious_threshold);
  206. static struct attribute *xenbus_attrs[] = {
  207. &dev_attr_event_channels.attr,
  208. &dev_attr_events.attr,
  209. &dev_attr_spurious_events.attr,
  210. &dev_attr_jiffies_eoi_delayed.attr,
  211. &dev_attr_spurious_threshold.attr,
  212. NULL
  213. };
  214. static const struct attribute_group xenbus_group = {
  215. .name = "xenbus",
  216. .attrs = xenbus_attrs,
  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. if (!try_module_get(drv->driver.owner)) {
  241. dev_warn(&dev->dev, "failed to acquire module reference on '%s'\n",
  242. drv->driver.name);
  243. err = -ESRCH;
  244. goto fail;
  245. }
  246. down(&dev->reclaim_sem);
  247. err = drv->probe(dev, id);
  248. up(&dev->reclaim_sem);
  249. if (err)
  250. goto fail_put;
  251. err = watch_otherend(dev);
  252. if (err) {
  253. dev_warn(&dev->dev, "watch_otherend on %s failed.\n",
  254. dev->nodename);
  255. return err;
  256. }
  257. dev->spurious_threshold = 1;
  258. if (sysfs_create_group(&dev->dev.kobj, &xenbus_group))
  259. dev_warn(&dev->dev, "sysfs_create_group on %s failed.\n",
  260. dev->nodename);
  261. return 0;
  262. fail_put:
  263. module_put(drv->driver.owner);
  264. fail:
  265. xenbus_dev_error(dev, err, "xenbus_dev_probe on %s", dev->nodename);
  266. return err;
  267. }
  268. EXPORT_SYMBOL_GPL(xenbus_dev_probe);
  269. void xenbus_dev_remove(struct device *_dev)
  270. {
  271. struct xenbus_device *dev = to_xenbus_device(_dev);
  272. struct xenbus_driver *drv = to_xenbus_driver(_dev->driver);
  273. DPRINTK("%s", dev->nodename);
  274. sysfs_remove_group(&dev->dev.kobj, &xenbus_group);
  275. free_otherend_watch(dev);
  276. if (drv->remove) {
  277. down(&dev->reclaim_sem);
  278. drv->remove(dev);
  279. up(&dev->reclaim_sem);
  280. }
  281. module_put(drv->driver.owner);
  282. free_otherend_details(dev);
  283. /*
  284. * If the toolstack has forced the device state to closing then set
  285. * the state to closed now to allow it to be cleaned up.
  286. * Similarly, if the driver does not support re-bind, set the
  287. * closed.
  288. */
  289. if (!drv->allow_rebind ||
  290. xenbus_read_driver_state(dev->nodename) == XenbusStateClosing)
  291. xenbus_switch_state(dev, XenbusStateClosed);
  292. }
  293. EXPORT_SYMBOL_GPL(xenbus_dev_remove);
  294. int xenbus_register_driver_common(struct xenbus_driver *drv,
  295. struct xen_bus_type *bus,
  296. struct module *owner, const char *mod_name)
  297. {
  298. drv->driver.name = drv->name ? drv->name : drv->ids[0].devicetype;
  299. drv->driver.bus = &bus->bus;
  300. drv->driver.owner = owner;
  301. drv->driver.mod_name = mod_name;
  302. return driver_register(&drv->driver);
  303. }
  304. EXPORT_SYMBOL_GPL(xenbus_register_driver_common);
  305. void xenbus_unregister_driver(struct xenbus_driver *drv)
  306. {
  307. driver_unregister(&drv->driver);
  308. }
  309. EXPORT_SYMBOL_GPL(xenbus_unregister_driver);
  310. struct xb_find_info {
  311. struct xenbus_device *dev;
  312. const char *nodename;
  313. };
  314. static int cmp_dev(struct device *dev, void *data)
  315. {
  316. struct xenbus_device *xendev = to_xenbus_device(dev);
  317. struct xb_find_info *info = data;
  318. if (!strcmp(xendev->nodename, info->nodename)) {
  319. info->dev = xendev;
  320. get_device(dev);
  321. return 1;
  322. }
  323. return 0;
  324. }
  325. static struct xenbus_device *xenbus_device_find(const char *nodename,
  326. struct bus_type *bus)
  327. {
  328. struct xb_find_info info = { .dev = NULL, .nodename = nodename };
  329. bus_for_each_dev(bus, NULL, &info, cmp_dev);
  330. return info.dev;
  331. }
  332. static int cleanup_dev(struct device *dev, void *data)
  333. {
  334. struct xenbus_device *xendev = to_xenbus_device(dev);
  335. struct xb_find_info *info = data;
  336. int len = strlen(info->nodename);
  337. DPRINTK("%s", info->nodename);
  338. /* Match the info->nodename path, or any subdirectory of that path. */
  339. if (strncmp(xendev->nodename, info->nodename, len))
  340. return 0;
  341. /* If the node name is longer, ensure it really is a subdirectory. */
  342. if ((strlen(xendev->nodename) > len) && (xendev->nodename[len] != '/'))
  343. return 0;
  344. info->dev = xendev;
  345. get_device(dev);
  346. return 1;
  347. }
  348. static void xenbus_cleanup_devices(const char *path, struct bus_type *bus)
  349. {
  350. struct xb_find_info info = { .nodename = path };
  351. do {
  352. info.dev = NULL;
  353. bus_for_each_dev(bus, NULL, &info, cleanup_dev);
  354. if (info.dev) {
  355. device_unregister(&info.dev->dev);
  356. put_device(&info.dev->dev);
  357. }
  358. } while (info.dev);
  359. }
  360. static void xenbus_dev_release(struct device *dev)
  361. {
  362. if (dev)
  363. kfree(to_xenbus_device(dev));
  364. }
  365. static ssize_t nodename_show(struct device *dev,
  366. struct device_attribute *attr, char *buf)
  367. {
  368. return sprintf(buf, "%s\n", to_xenbus_device(dev)->nodename);
  369. }
  370. static DEVICE_ATTR_RO(nodename);
  371. static ssize_t devtype_show(struct device *dev,
  372. struct device_attribute *attr, char *buf)
  373. {
  374. return sprintf(buf, "%s\n", to_xenbus_device(dev)->devicetype);
  375. }
  376. static DEVICE_ATTR_RO(devtype);
  377. static ssize_t modalias_show(struct device *dev,
  378. struct device_attribute *attr, char *buf)
  379. {
  380. return sprintf(buf, "%s:%s\n", dev->bus->name,
  381. to_xenbus_device(dev)->devicetype);
  382. }
  383. static DEVICE_ATTR_RO(modalias);
  384. static ssize_t state_show(struct device *dev,
  385. struct device_attribute *attr, char *buf)
  386. {
  387. return sprintf(buf, "%s\n",
  388. xenbus_strstate(to_xenbus_device(dev)->state));
  389. }
  390. static DEVICE_ATTR_RO(state);
  391. static struct attribute *xenbus_dev_attrs[] = {
  392. &dev_attr_nodename.attr,
  393. &dev_attr_devtype.attr,
  394. &dev_attr_modalias.attr,
  395. &dev_attr_state.attr,
  396. NULL,
  397. };
  398. static const struct attribute_group xenbus_dev_group = {
  399. .attrs = xenbus_dev_attrs,
  400. };
  401. const struct attribute_group *xenbus_dev_groups[] = {
  402. &xenbus_dev_group,
  403. NULL,
  404. };
  405. EXPORT_SYMBOL_GPL(xenbus_dev_groups);
  406. int xenbus_probe_node(struct xen_bus_type *bus,
  407. const char *type,
  408. const char *nodename)
  409. {
  410. char devname[XEN_BUS_ID_SIZE];
  411. int err;
  412. struct xenbus_device *xendev;
  413. size_t stringlen;
  414. char *tmpstring;
  415. enum xenbus_state state = xenbus_read_driver_state(nodename);
  416. if (state != XenbusStateInitialising) {
  417. /* Device is not new, so ignore it. This can happen if a
  418. device is going away after switching to Closed. */
  419. return 0;
  420. }
  421. stringlen = strlen(nodename) + 1 + strlen(type) + 1;
  422. xendev = kzalloc(sizeof(*xendev) + stringlen, GFP_KERNEL);
  423. if (!xendev)
  424. return -ENOMEM;
  425. xendev->state = XenbusStateInitialising;
  426. /* Copy the strings into the extra space. */
  427. tmpstring = (char *)(xendev + 1);
  428. strcpy(tmpstring, nodename);
  429. xendev->nodename = tmpstring;
  430. tmpstring += strlen(tmpstring) + 1;
  431. strcpy(tmpstring, type);
  432. xendev->devicetype = tmpstring;
  433. init_completion(&xendev->down);
  434. xendev->dev.bus = &bus->bus;
  435. xendev->dev.release = xenbus_dev_release;
  436. err = bus->get_bus_id(devname, xendev->nodename);
  437. if (err)
  438. goto fail;
  439. dev_set_name(&xendev->dev, "%s", devname);
  440. sema_init(&xendev->reclaim_sem, 1);
  441. /* Register with generic device framework. */
  442. err = device_register(&xendev->dev);
  443. if (err) {
  444. put_device(&xendev->dev);
  445. xendev = NULL;
  446. goto fail;
  447. }
  448. return 0;
  449. fail:
  450. kfree(xendev);
  451. return err;
  452. }
  453. EXPORT_SYMBOL_GPL(xenbus_probe_node);
  454. static int xenbus_probe_device_type(struct xen_bus_type *bus, const char *type)
  455. {
  456. int err = 0;
  457. char **dir;
  458. unsigned int dir_n = 0;
  459. int i;
  460. dir = xenbus_directory(XBT_NIL, bus->root, type, &dir_n);
  461. if (IS_ERR(dir))
  462. return PTR_ERR(dir);
  463. for (i = 0; i < dir_n; i++) {
  464. err = bus->probe(bus, type, dir[i]);
  465. if (err)
  466. break;
  467. }
  468. kfree(dir);
  469. return err;
  470. }
  471. int xenbus_probe_devices(struct xen_bus_type *bus)
  472. {
  473. int err = 0;
  474. char **dir;
  475. unsigned int i, dir_n;
  476. dir = xenbus_directory(XBT_NIL, bus->root, "", &dir_n);
  477. if (IS_ERR(dir))
  478. return PTR_ERR(dir);
  479. for (i = 0; i < dir_n; i++) {
  480. err = xenbus_probe_device_type(bus, dir[i]);
  481. if (err)
  482. break;
  483. }
  484. kfree(dir);
  485. return err;
  486. }
  487. EXPORT_SYMBOL_GPL(xenbus_probe_devices);
  488. static unsigned int char_count(const char *str, char c)
  489. {
  490. unsigned int i, ret = 0;
  491. for (i = 0; str[i]; i++)
  492. if (str[i] == c)
  493. ret++;
  494. return ret;
  495. }
  496. static int strsep_len(const char *str, char c, unsigned int len)
  497. {
  498. unsigned int i;
  499. for (i = 0; str[i]; i++)
  500. if (str[i] == c) {
  501. if (len == 0)
  502. return i;
  503. len--;
  504. }
  505. return (len == 0) ? i : -ERANGE;
  506. }
  507. void xenbus_dev_changed(const char *node, struct xen_bus_type *bus)
  508. {
  509. int exists, rootlen;
  510. struct xenbus_device *dev;
  511. char type[XEN_BUS_ID_SIZE];
  512. const char *p, *root;
  513. if (char_count(node, '/') < 2)
  514. return;
  515. exists = xenbus_exists(XBT_NIL, node, "");
  516. if (!exists) {
  517. xenbus_cleanup_devices(node, &bus->bus);
  518. return;
  519. }
  520. /* backend/<type>/... or device/<type>/... */
  521. p = strchr(node, '/') + 1;
  522. snprintf(type, XEN_BUS_ID_SIZE, "%.*s", (int)strcspn(p, "/"), p);
  523. type[XEN_BUS_ID_SIZE-1] = '\0';
  524. rootlen = strsep_len(node, '/', bus->levels);
  525. if (rootlen < 0)
  526. return;
  527. root = kasprintf(GFP_KERNEL, "%.*s", rootlen, node);
  528. if (!root)
  529. return;
  530. dev = xenbus_device_find(root, &bus->bus);
  531. if (!dev)
  532. xenbus_probe_node(bus, type, root);
  533. else
  534. put_device(&dev->dev);
  535. kfree(root);
  536. }
  537. EXPORT_SYMBOL_GPL(xenbus_dev_changed);
  538. int xenbus_dev_suspend(struct device *dev)
  539. {
  540. int err = 0;
  541. struct xenbus_driver *drv;
  542. struct xenbus_device *xdev
  543. = container_of(dev, struct xenbus_device, dev);
  544. DPRINTK("%s", xdev->nodename);
  545. if (dev->driver == NULL)
  546. return 0;
  547. drv = to_xenbus_driver(dev->driver);
  548. if (drv->suspend)
  549. err = drv->suspend(xdev);
  550. if (err)
  551. dev_warn(dev, "suspend failed: %i\n", err);
  552. return 0;
  553. }
  554. EXPORT_SYMBOL_GPL(xenbus_dev_suspend);
  555. int xenbus_dev_resume(struct device *dev)
  556. {
  557. int err;
  558. struct xenbus_driver *drv;
  559. struct xenbus_device *xdev
  560. = container_of(dev, struct xenbus_device, dev);
  561. DPRINTK("%s", xdev->nodename);
  562. if (dev->driver == NULL)
  563. return 0;
  564. drv = to_xenbus_driver(dev->driver);
  565. err = talk_to_otherend(xdev);
  566. if (err) {
  567. dev_warn(dev, "resume (talk_to_otherend) failed: %i\n", err);
  568. return err;
  569. }
  570. xdev->state = XenbusStateInitialising;
  571. if (drv->resume) {
  572. err = drv->resume(xdev);
  573. if (err) {
  574. dev_warn(dev, "resume failed: %i\n", err);
  575. return err;
  576. }
  577. }
  578. err = watch_otherend(xdev);
  579. if (err) {
  580. dev_warn(dev, "resume (watch_otherend) failed: %d\n", err);
  581. return err;
  582. }
  583. return 0;
  584. }
  585. EXPORT_SYMBOL_GPL(xenbus_dev_resume);
  586. int xenbus_dev_cancel(struct device *dev)
  587. {
  588. /* Do nothing */
  589. DPRINTK("cancel");
  590. return 0;
  591. }
  592. EXPORT_SYMBOL_GPL(xenbus_dev_cancel);
  593. /* A flag to determine if xenstored is 'ready' (i.e. has started) */
  594. int xenstored_ready;
  595. int register_xenstore_notifier(struct notifier_block *nb)
  596. {
  597. int ret = 0;
  598. if (xenstored_ready > 0)
  599. ret = nb->notifier_call(nb, 0, NULL);
  600. else
  601. blocking_notifier_chain_register(&xenstore_chain, nb);
  602. return ret;
  603. }
  604. EXPORT_SYMBOL_GPL(register_xenstore_notifier);
  605. void unregister_xenstore_notifier(struct notifier_block *nb)
  606. {
  607. blocking_notifier_chain_unregister(&xenstore_chain, nb);
  608. }
  609. EXPORT_SYMBOL_GPL(unregister_xenstore_notifier);
  610. static void xenbus_probe(void)
  611. {
  612. xenstored_ready = 1;
  613. if (!xen_store_interface) {
  614. xen_store_interface = memremap(xen_store_gfn << XEN_PAGE_SHIFT,
  615. XEN_PAGE_SIZE, MEMREMAP_WB);
  616. /*
  617. * Now it is safe to free the IRQ used for xenstore late
  618. * initialization. No need to unbind: it is about to be
  619. * bound again from xb_init_comms. Note that calling
  620. * unbind_from_irqhandler now would result in xen_evtchn_close()
  621. * being called and the event channel not being enabled again
  622. * afterwards, resulting in missed event notifications.
  623. */
  624. free_irq(xs_init_irq, &xb_waitq);
  625. }
  626. /*
  627. * In the HVM case, xenbus_init() deferred its call to
  628. * xs_init() in case callbacks were not operational yet.
  629. * So do it now.
  630. */
  631. if (xen_store_domain_type == XS_HVM)
  632. xs_init();
  633. /* Notify others that xenstore is up */
  634. blocking_notifier_call_chain(&xenstore_chain, 0, NULL);
  635. }
  636. /*
  637. * Returns true when XenStore init must be deferred in order to
  638. * allow the PCI platform device to be initialised, before we
  639. * can actually have event channel interrupts working.
  640. */
  641. static bool xs_hvm_defer_init_for_callback(void)
  642. {
  643. #ifdef CONFIG_XEN_PVHVM
  644. return xen_store_domain_type == XS_HVM &&
  645. !xen_have_vector_callback;
  646. #else
  647. return false;
  648. #endif
  649. }
  650. static int xenbus_probe_thread(void *unused)
  651. {
  652. DEFINE_WAIT(w);
  653. /*
  654. * We actually just want to wait for *any* trigger of xb_waitq,
  655. * and run xenbus_probe() the moment it occurs.
  656. */
  657. prepare_to_wait(&xb_waitq, &w, TASK_INTERRUPTIBLE);
  658. schedule();
  659. finish_wait(&xb_waitq, &w);
  660. DPRINTK("probing");
  661. xenbus_probe();
  662. return 0;
  663. }
  664. static int __init xenbus_probe_initcall(void)
  665. {
  666. if (!xen_domain())
  667. return -ENODEV;
  668. /*
  669. * Probe XenBus here in the XS_PV case, and also XS_HVM unless we
  670. * need to wait for the platform PCI device to come up or
  671. * xen_store_interface is not ready.
  672. */
  673. if (xen_store_domain_type == XS_PV ||
  674. (xen_store_domain_type == XS_HVM &&
  675. !xs_hvm_defer_init_for_callback() &&
  676. xen_store_interface != NULL))
  677. xenbus_probe();
  678. /*
  679. * For XS_LOCAL or when xen_store_interface is not ready, spawn a
  680. * thread which will wait for xenstored or a xenstore-stubdom to be
  681. * started, then probe. It will be triggered when communication
  682. * starts happening, by waiting on xb_waitq.
  683. */
  684. if (xen_store_domain_type == XS_LOCAL || xen_store_interface == NULL) {
  685. struct task_struct *probe_task;
  686. probe_task = kthread_run(xenbus_probe_thread, NULL,
  687. "xenbus_probe");
  688. if (IS_ERR(probe_task))
  689. return PTR_ERR(probe_task);
  690. }
  691. return 0;
  692. }
  693. device_initcall(xenbus_probe_initcall);
  694. int xen_set_callback_via(uint64_t via)
  695. {
  696. struct xen_hvm_param a;
  697. int ret;
  698. a.domid = DOMID_SELF;
  699. a.index = HVM_PARAM_CALLBACK_IRQ;
  700. a.value = via;
  701. ret = HYPERVISOR_hvm_op(HVMOP_set_param, &a);
  702. if (ret)
  703. return ret;
  704. /*
  705. * If xenbus_probe_initcall() deferred the xenbus_probe()
  706. * due to the callback not functioning yet, we can do it now.
  707. */
  708. if (!xenstored_ready && xs_hvm_defer_init_for_callback())
  709. xenbus_probe();
  710. return ret;
  711. }
  712. EXPORT_SYMBOL_GPL(xen_set_callback_via);
  713. /* Set up event channel for xenstored which is run as a local process
  714. * (this is normally used only in dom0)
  715. */
  716. static int __init xenstored_local_init(void)
  717. {
  718. int err = -ENOMEM;
  719. unsigned long page = 0;
  720. struct evtchn_alloc_unbound alloc_unbound;
  721. /* Allocate Xenstore page */
  722. page = get_zeroed_page(GFP_KERNEL);
  723. if (!page)
  724. goto out_err;
  725. xen_store_gfn = virt_to_gfn((void *)page);
  726. /* Next allocate a local port which xenstored can bind to */
  727. alloc_unbound.dom = DOMID_SELF;
  728. alloc_unbound.remote_dom = DOMID_SELF;
  729. err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
  730. &alloc_unbound);
  731. if (err == -ENOSYS)
  732. goto out_err;
  733. BUG_ON(err);
  734. xen_store_evtchn = alloc_unbound.port;
  735. return 0;
  736. out_err:
  737. if (page != 0)
  738. free_page(page);
  739. return err;
  740. }
  741. static int xenbus_resume_cb(struct notifier_block *nb,
  742. unsigned long action, void *data)
  743. {
  744. int err = 0;
  745. if (xen_hvm_domain()) {
  746. uint64_t v = 0;
  747. err = hvm_get_parameter(HVM_PARAM_STORE_EVTCHN, &v);
  748. if (!err && v)
  749. xen_store_evtchn = v;
  750. else
  751. pr_warn("Cannot update xenstore event channel: %d\n",
  752. err);
  753. } else
  754. xen_store_evtchn = xen_start_info->store_evtchn;
  755. return err;
  756. }
  757. static struct notifier_block xenbus_resume_nb = {
  758. .notifier_call = xenbus_resume_cb,
  759. };
  760. static irqreturn_t xenbus_late_init(int irq, void *unused)
  761. {
  762. int err;
  763. uint64_t v = 0;
  764. err = hvm_get_parameter(HVM_PARAM_STORE_PFN, &v);
  765. if (err || !v || !~v)
  766. return IRQ_HANDLED;
  767. xen_store_gfn = (unsigned long)v;
  768. wake_up(&xb_waitq);
  769. return IRQ_HANDLED;
  770. }
  771. static int __init xenbus_init(void)
  772. {
  773. int err;
  774. uint64_t v = 0;
  775. bool wait = false;
  776. xen_store_domain_type = XS_UNKNOWN;
  777. if (!xen_domain())
  778. return -ENODEV;
  779. xenbus_ring_ops_init();
  780. if (xen_pv_domain())
  781. xen_store_domain_type = XS_PV;
  782. if (xen_hvm_domain())
  783. xen_store_domain_type = XS_HVM;
  784. if (xen_hvm_domain() && xen_initial_domain())
  785. xen_store_domain_type = XS_LOCAL;
  786. if (xen_pv_domain() && !xen_start_info->store_evtchn)
  787. xen_store_domain_type = XS_LOCAL;
  788. if (xen_pv_domain() && xen_start_info->store_evtchn)
  789. xenstored_ready = 1;
  790. switch (xen_store_domain_type) {
  791. case XS_LOCAL:
  792. err = xenstored_local_init();
  793. if (err)
  794. goto out_error;
  795. xen_store_interface = gfn_to_virt(xen_store_gfn);
  796. break;
  797. case XS_PV:
  798. xen_store_evtchn = xen_start_info->store_evtchn;
  799. xen_store_gfn = xen_start_info->store_mfn;
  800. xen_store_interface = gfn_to_virt(xen_store_gfn);
  801. break;
  802. case XS_HVM:
  803. err = hvm_get_parameter(HVM_PARAM_STORE_EVTCHN, &v);
  804. if (err)
  805. goto out_error;
  806. xen_store_evtchn = (int)v;
  807. err = hvm_get_parameter(HVM_PARAM_STORE_PFN, &v);
  808. if (err)
  809. goto out_error;
  810. /*
  811. * Uninitialized hvm_params are zero and return no error.
  812. * Although it is theoretically possible to have
  813. * HVM_PARAM_STORE_PFN set to zero on purpose, in reality it is
  814. * not zero when valid. If zero, it means that Xenstore hasn't
  815. * been properly initialized. Instead of attempting to map a
  816. * wrong guest physical address return error.
  817. *
  818. * Also recognize all bits set as an invalid/uninitialized value.
  819. */
  820. if (!v) {
  821. err = -ENOENT;
  822. goto out_error;
  823. }
  824. if (v == ~0ULL) {
  825. wait = true;
  826. } else {
  827. /* Avoid truncation on 32-bit. */
  828. #if BITS_PER_LONG == 32
  829. if (v > ULONG_MAX) {
  830. pr_err("%s: cannot handle HVM_PARAM_STORE_PFN=%llx > ULONG_MAX\n",
  831. __func__, v);
  832. err = -EINVAL;
  833. goto out_error;
  834. }
  835. #endif
  836. xen_store_gfn = (unsigned long)v;
  837. xen_store_interface =
  838. memremap(xen_store_gfn << XEN_PAGE_SHIFT,
  839. XEN_PAGE_SIZE, MEMREMAP_WB);
  840. if (xen_store_interface->connection != XENSTORE_CONNECTED)
  841. wait = true;
  842. }
  843. if (wait) {
  844. err = bind_evtchn_to_irqhandler(xen_store_evtchn,
  845. xenbus_late_init,
  846. 0, "xenstore_late_init",
  847. &xb_waitq);
  848. if (err < 0) {
  849. pr_err("xenstore_late_init couldn't bind irq err=%d\n",
  850. err);
  851. goto out_error;
  852. }
  853. xs_init_irq = err;
  854. }
  855. break;
  856. default:
  857. pr_warn("Xenstore state unknown\n");
  858. break;
  859. }
  860. /*
  861. * HVM domains may not have a functional callback yet. In that
  862. * case let xs_init() be called from xenbus_probe(), which will
  863. * get invoked at an appropriate time.
  864. */
  865. if (xen_store_domain_type != XS_HVM) {
  866. err = xs_init();
  867. if (err) {
  868. pr_warn("Error initializing xenstore comms: %i\n", err);
  869. goto out_error;
  870. }
  871. }
  872. if ((xen_store_domain_type != XS_LOCAL) &&
  873. (xen_store_domain_type != XS_UNKNOWN))
  874. xen_resume_notifier_register(&xenbus_resume_nb);
  875. #ifdef CONFIG_XEN_COMPAT_XENFS
  876. /*
  877. * Create xenfs mountpoint in /proc for compatibility with
  878. * utilities that expect to find "xenbus" under "/proc/xen".
  879. */
  880. proc_create_mount_point("xen");
  881. #endif
  882. return 0;
  883. out_error:
  884. xen_store_domain_type = XS_UNKNOWN;
  885. return err;
  886. }
  887. postcore_initcall(xenbus_init);
  888. MODULE_LICENSE("GPL");