xenbus_client.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. /******************************************************************************
  2. * Client-facing interface for the Xenbus driver. In other words, the
  3. * interface between the Xenbus and the device-specific code, be it the
  4. * frontend or the backend of that driver.
  5. *
  6. * Copyright (C) 2005 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. #include <linux/types.h>
  33. #include <linux/vmalloc.h>
  34. #include <asm/xen/hypervisor.h>
  35. #include <xen/interface/xen.h>
  36. #include <xen/interface/event_channel.h>
  37. #include <xen/events.h>
  38. #include <xen/grant_table.h>
  39. #include <xen/xenbus.h>
  40. const char *xenbus_strstate(enum xenbus_state state)
  41. {
  42. static const char *const name[] = {
  43. [ XenbusStateUnknown ] = "Unknown",
  44. [ XenbusStateInitialising ] = "Initialising",
  45. [ XenbusStateInitWait ] = "InitWait",
  46. [ XenbusStateInitialised ] = "Initialised",
  47. [ XenbusStateConnected ] = "Connected",
  48. [ XenbusStateClosing ] = "Closing",
  49. [ XenbusStateClosed ] = "Closed",
  50. };
  51. return (state < ARRAY_SIZE(name)) ? name[state] : "INVALID";
  52. }
  53. EXPORT_SYMBOL_GPL(xenbus_strstate);
  54. /**
  55. * xenbus_watch_path - register a watch
  56. * @dev: xenbus device
  57. * @path: path to watch
  58. * @watch: watch to register
  59. * @callback: callback to register
  60. *
  61. * Register a @watch on the given path, using the given xenbus_watch structure
  62. * for storage, and the given @callback function as the callback. Return 0 on
  63. * success, or -errno on error. On success, the given @path will be saved as
  64. * @watch->node, and remains the caller's to free. On error, @watch->node will
  65. * be NULL, the device will switch to %XenbusStateClosing, and the error will
  66. * be saved in the store.
  67. */
  68. int xenbus_watch_path(struct xenbus_device *dev, const char *path,
  69. struct xenbus_watch *watch,
  70. void (*callback)(struct xenbus_watch *,
  71. const char **, unsigned int))
  72. {
  73. int err;
  74. watch->node = path;
  75. watch->callback = callback;
  76. err = register_xenbus_watch(watch);
  77. if (err) {
  78. watch->node = NULL;
  79. watch->callback = NULL;
  80. xenbus_dev_fatal(dev, err, "adding watch on %s", path);
  81. }
  82. return err;
  83. }
  84. EXPORT_SYMBOL_GPL(xenbus_watch_path);
  85. /**
  86. * xenbus_watch_pathfmt - register a watch on a sprintf-formatted path
  87. * @dev: xenbus device
  88. * @watch: watch to register
  89. * @callback: callback to register
  90. * @pathfmt: format of path to watch
  91. *
  92. * Register a watch on the given @path, using the given xenbus_watch
  93. * structure for storage, and the given @callback function as the callback.
  94. * Return 0 on success, or -errno on error. On success, the watched path
  95. * (@path/@path2) will be saved as @watch->node, and becomes the caller's to
  96. * kfree(). On error, watch->node will be NULL, so the caller has nothing to
  97. * free, the device will switch to %XenbusStateClosing, and the error will be
  98. * saved in the store.
  99. */
  100. int xenbus_watch_pathfmt(struct xenbus_device *dev,
  101. struct xenbus_watch *watch,
  102. void (*callback)(struct xenbus_watch *,
  103. const char **, unsigned int),
  104. const char *pathfmt, ...)
  105. {
  106. int err;
  107. va_list ap;
  108. char *path;
  109. va_start(ap, pathfmt);
  110. path = kvasprintf(GFP_NOIO | __GFP_HIGH, pathfmt, ap);
  111. va_end(ap);
  112. if (!path) {
  113. xenbus_dev_fatal(dev, -ENOMEM, "allocating path for watch");
  114. return -ENOMEM;
  115. }
  116. err = xenbus_watch_path(dev, path, watch, callback);
  117. if (err)
  118. kfree(path);
  119. return err;
  120. }
  121. EXPORT_SYMBOL_GPL(xenbus_watch_pathfmt);
  122. /**
  123. * xenbus_switch_state
  124. * @dev: xenbus device
  125. * @state: new state
  126. *
  127. * Advertise in the store a change of the given driver to the given new_state.
  128. * Return 0 on success, or -errno on error. On error, the device will switch
  129. * to XenbusStateClosing, and the error will be saved in the store.
  130. */
  131. int xenbus_switch_state(struct xenbus_device *dev, enum xenbus_state state)
  132. {
  133. /* We check whether the state is currently set to the given value, and
  134. if not, then the state is set. We don't want to unconditionally
  135. write the given state, because we don't want to fire watches
  136. unnecessarily. Furthermore, if the node has gone, we don't write
  137. to it, as the device will be tearing down, and we don't want to
  138. resurrect that directory.
  139. Note that, because of this cached value of our state, this function
  140. will not work inside a Xenstore transaction (something it was
  141. trying to in the past) because dev->state would not get reset if
  142. the transaction was aborted.
  143. */
  144. int current_state;
  145. int err;
  146. if (state == dev->state)
  147. return 0;
  148. err = xenbus_scanf(XBT_NIL, dev->nodename, "state", "%d",
  149. &current_state);
  150. if (err != 1)
  151. return 0;
  152. err = xenbus_printf(XBT_NIL, dev->nodename, "state", "%d", state);
  153. if (err) {
  154. if (state != XenbusStateClosing) /* Avoid looping */
  155. xenbus_dev_fatal(dev, err, "writing new state");
  156. return err;
  157. }
  158. dev->state = state;
  159. return 0;
  160. }
  161. EXPORT_SYMBOL_GPL(xenbus_switch_state);
  162. int xenbus_frontend_closed(struct xenbus_device *dev)
  163. {
  164. xenbus_switch_state(dev, XenbusStateClosed);
  165. complete(&dev->down);
  166. return 0;
  167. }
  168. EXPORT_SYMBOL_GPL(xenbus_frontend_closed);
  169. /**
  170. * Return the path to the error node for the given device, or NULL on failure.
  171. * If the value returned is non-NULL, then it is the caller's to kfree.
  172. */
  173. static char *error_path(struct xenbus_device *dev)
  174. {
  175. return kasprintf(GFP_KERNEL, "error/%s", dev->nodename);
  176. }
  177. static void xenbus_va_dev_error(struct xenbus_device *dev, int err,
  178. const char *fmt, va_list ap)
  179. {
  180. int ret;
  181. unsigned int len;
  182. char *printf_buffer = NULL;
  183. char *path_buffer = NULL;
  184. #define PRINTF_BUFFER_SIZE 4096
  185. printf_buffer = kmalloc(PRINTF_BUFFER_SIZE, GFP_KERNEL);
  186. if (printf_buffer == NULL)
  187. goto fail;
  188. len = sprintf(printf_buffer, "%i ", -err);
  189. ret = vsnprintf(printf_buffer+len, PRINTF_BUFFER_SIZE-len, fmt, ap);
  190. BUG_ON(len + ret > PRINTF_BUFFER_SIZE-1);
  191. dev_err(&dev->dev, "%s\n", printf_buffer);
  192. path_buffer = error_path(dev);
  193. if (path_buffer == NULL) {
  194. dev_err(&dev->dev, "failed to write error node for %s (%s)\n",
  195. dev->nodename, printf_buffer);
  196. goto fail;
  197. }
  198. if (xenbus_write(XBT_NIL, path_buffer, "error", printf_buffer) != 0) {
  199. dev_err(&dev->dev, "failed to write error node for %s (%s)\n",
  200. dev->nodename, printf_buffer);
  201. goto fail;
  202. }
  203. fail:
  204. kfree(printf_buffer);
  205. kfree(path_buffer);
  206. }
  207. /**
  208. * xenbus_dev_error
  209. * @dev: xenbus device
  210. * @err: error to report
  211. * @fmt: error message format
  212. *
  213. * Report the given negative errno into the store, along with the given
  214. * formatted message.
  215. */
  216. void xenbus_dev_error(struct xenbus_device *dev, int err, const char *fmt, ...)
  217. {
  218. va_list ap;
  219. va_start(ap, fmt);
  220. xenbus_va_dev_error(dev, err, fmt, ap);
  221. va_end(ap);
  222. }
  223. EXPORT_SYMBOL_GPL(xenbus_dev_error);
  224. /**
  225. * xenbus_dev_fatal
  226. * @dev: xenbus device
  227. * @err: error to report
  228. * @fmt: error message format
  229. *
  230. * Equivalent to xenbus_dev_error(dev, err, fmt, args), followed by
  231. * xenbus_switch_state(dev, XenbusStateClosing) to schedule an orderly
  232. * closedown of this driver and its peer.
  233. */
  234. void xenbus_dev_fatal(struct xenbus_device *dev, int err, const char *fmt, ...)
  235. {
  236. va_list ap;
  237. va_start(ap, fmt);
  238. xenbus_va_dev_error(dev, err, fmt, ap);
  239. va_end(ap);
  240. xenbus_switch_state(dev, XenbusStateClosing);
  241. }
  242. EXPORT_SYMBOL_GPL(xenbus_dev_fatal);
  243. /**
  244. * xenbus_grant_ring
  245. * @dev: xenbus device
  246. * @ring_mfn: mfn of ring to grant
  247. * Grant access to the given @ring_mfn to the peer of the given device. Return
  248. * 0 on success, or -errno on error. On error, the device will switch to
  249. * XenbusStateClosing, and the error will be saved in the store.
  250. */
  251. int xenbus_grant_ring(struct xenbus_device *dev, unsigned long ring_mfn)
  252. {
  253. int err = gnttab_grant_foreign_access(dev->otherend_id, ring_mfn, 0);
  254. if (err < 0)
  255. xenbus_dev_fatal(dev, err, "granting access to ring page");
  256. return err;
  257. }
  258. EXPORT_SYMBOL_GPL(xenbus_grant_ring);
  259. /**
  260. * Allocate an event channel for the given xenbus_device, assigning the newly
  261. * created local port to *port. Return 0 on success, or -errno on error. On
  262. * error, the device will switch to XenbusStateClosing, and the error will be
  263. * saved in the store.
  264. */
  265. int xenbus_alloc_evtchn(struct xenbus_device *dev, int *port)
  266. {
  267. struct evtchn_alloc_unbound alloc_unbound;
  268. int err;
  269. alloc_unbound.dom = DOMID_SELF;
  270. alloc_unbound.remote_dom = dev->otherend_id;
  271. err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
  272. &alloc_unbound);
  273. if (err)
  274. xenbus_dev_fatal(dev, err, "allocating event channel");
  275. else
  276. *port = alloc_unbound.port;
  277. return err;
  278. }
  279. EXPORT_SYMBOL_GPL(xenbus_alloc_evtchn);
  280. /**
  281. * Bind to an existing interdomain event channel in another domain. Returns 0
  282. * on success and stores the local port in *port. On error, returns -errno,
  283. * switches the device to XenbusStateClosing, and saves the error in XenStore.
  284. */
  285. int xenbus_bind_evtchn(struct xenbus_device *dev, int remote_port, int *port)
  286. {
  287. struct evtchn_bind_interdomain bind_interdomain;
  288. int err;
  289. bind_interdomain.remote_dom = dev->otherend_id;
  290. bind_interdomain.remote_port = remote_port;
  291. err = HYPERVISOR_event_channel_op(EVTCHNOP_bind_interdomain,
  292. &bind_interdomain);
  293. if (err)
  294. xenbus_dev_fatal(dev, err,
  295. "binding to event channel %d from domain %d",
  296. remote_port, dev->otherend_id);
  297. else
  298. *port = bind_interdomain.local_port;
  299. return err;
  300. }
  301. EXPORT_SYMBOL_GPL(xenbus_bind_evtchn);
  302. /**
  303. * Free an existing event channel. Returns 0 on success or -errno on error.
  304. */
  305. int xenbus_free_evtchn(struct xenbus_device *dev, int port)
  306. {
  307. struct evtchn_close close;
  308. int err;
  309. close.port = port;
  310. err = HYPERVISOR_event_channel_op(EVTCHNOP_close, &close);
  311. if (err)
  312. xenbus_dev_error(dev, err, "freeing event channel %d", port);
  313. return err;
  314. }
  315. EXPORT_SYMBOL_GPL(xenbus_free_evtchn);
  316. /**
  317. * xenbus_map_ring_valloc
  318. * @dev: xenbus device
  319. * @gnt_ref: grant reference
  320. * @vaddr: pointer to address to be filled out by mapping
  321. *
  322. * Based on Rusty Russell's skeleton driver's map_page.
  323. * Map a page of memory into this domain from another domain's grant table.
  324. * xenbus_map_ring_valloc allocates a page of virtual address space, maps the
  325. * page to that address, and sets *vaddr to that address.
  326. * Returns 0 on success, and GNTST_* (see xen/include/interface/grant_table.h)
  327. * or -ENOMEM on error. If an error is returned, device will switch to
  328. * XenbusStateClosing and the error message will be saved in XenStore.
  329. */
  330. int xenbus_map_ring_valloc(struct xenbus_device *dev, int gnt_ref, void **vaddr)
  331. {
  332. struct gnttab_map_grant_ref op = {
  333. .flags = GNTMAP_host_map,
  334. .ref = gnt_ref,
  335. .dom = dev->otherend_id,
  336. };
  337. struct vm_struct *area;
  338. *vaddr = NULL;
  339. area = xen_alloc_vm_area(PAGE_SIZE);
  340. if (!area)
  341. return -ENOMEM;
  342. op.host_addr = (unsigned long)area->addr;
  343. if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1))
  344. BUG();
  345. if (op.status != GNTST_okay) {
  346. xen_free_vm_area(area);
  347. xenbus_dev_fatal(dev, op.status,
  348. "mapping in shared page %d from domain %d",
  349. gnt_ref, dev->otherend_id);
  350. return op.status;
  351. }
  352. /* Stuff the handle in an unused field */
  353. area->phys_addr = (unsigned long)op.handle;
  354. *vaddr = area->addr;
  355. return 0;
  356. }
  357. EXPORT_SYMBOL_GPL(xenbus_map_ring_valloc);
  358. /**
  359. * xenbus_map_ring
  360. * @dev: xenbus device
  361. * @gnt_ref: grant reference
  362. * @handle: pointer to grant handle to be filled
  363. * @vaddr: address to be mapped to
  364. *
  365. * Map a page of memory into this domain from another domain's grant table.
  366. * xenbus_map_ring does not allocate the virtual address space (you must do
  367. * this yourself!). It only maps in the page to the specified address.
  368. * Returns 0 on success, and GNTST_* (see xen/include/interface/grant_table.h)
  369. * or -ENOMEM on error. If an error is returned, device will switch to
  370. * XenbusStateClosing and the error message will be saved in XenStore.
  371. */
  372. int xenbus_map_ring(struct xenbus_device *dev, int gnt_ref,
  373. grant_handle_t *handle, void *vaddr)
  374. {
  375. struct gnttab_map_grant_ref op = {
  376. .host_addr = (unsigned long)vaddr,
  377. .flags = GNTMAP_host_map,
  378. .ref = gnt_ref,
  379. .dom = dev->otherend_id,
  380. };
  381. if (HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, &op, 1))
  382. BUG();
  383. if (op.status != GNTST_okay) {
  384. xenbus_dev_fatal(dev, op.status,
  385. "mapping in shared page %d from domain %d",
  386. gnt_ref, dev->otherend_id);
  387. } else
  388. *handle = op.handle;
  389. return op.status;
  390. }
  391. EXPORT_SYMBOL_GPL(xenbus_map_ring);
  392. /**
  393. * xenbus_unmap_ring_vfree
  394. * @dev: xenbus device
  395. * @vaddr: addr to unmap
  396. *
  397. * Based on Rusty Russell's skeleton driver's unmap_page.
  398. * Unmap a page of memory in this domain that was imported from another domain.
  399. * Use xenbus_unmap_ring_vfree if you mapped in your memory with
  400. * xenbus_map_ring_valloc (it will free the virtual address space).
  401. * Returns 0 on success and returns GNTST_* on error
  402. * (see xen/include/interface/grant_table.h).
  403. */
  404. int xenbus_unmap_ring_vfree(struct xenbus_device *dev, void *vaddr)
  405. {
  406. struct vm_struct *area;
  407. struct gnttab_unmap_grant_ref op = {
  408. .host_addr = (unsigned long)vaddr,
  409. };
  410. /* It'd be nice if linux/vmalloc.h provided a find_vm_area(void *addr)
  411. * method so that we don't have to muck with vmalloc internals here.
  412. * We could force the user to hang on to their struct vm_struct from
  413. * xenbus_map_ring_valloc, but these 6 lines considerably simplify
  414. * this API.
  415. */
  416. read_lock(&vmlist_lock);
  417. for (area = vmlist; area != NULL; area = area->next) {
  418. if (area->addr == vaddr)
  419. break;
  420. }
  421. read_unlock(&vmlist_lock);
  422. if (!area) {
  423. xenbus_dev_error(dev, -ENOENT,
  424. "can't find mapped virtual address %p", vaddr);
  425. return GNTST_bad_virt_addr;
  426. }
  427. op.handle = (grant_handle_t)area->phys_addr;
  428. if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &op, 1))
  429. BUG();
  430. if (op.status == GNTST_okay)
  431. xen_free_vm_area(area);
  432. else
  433. xenbus_dev_error(dev, op.status,
  434. "unmapping page at handle %d error %d",
  435. (int16_t)area->phys_addr, op.status);
  436. return op.status;
  437. }
  438. EXPORT_SYMBOL_GPL(xenbus_unmap_ring_vfree);
  439. /**
  440. * xenbus_unmap_ring
  441. * @dev: xenbus device
  442. * @handle: grant handle
  443. * @vaddr: addr to unmap
  444. *
  445. * Unmap a page of memory in this domain that was imported from another domain.
  446. * Returns 0 on success and returns GNTST_* on error
  447. * (see xen/include/interface/grant_table.h).
  448. */
  449. int xenbus_unmap_ring(struct xenbus_device *dev,
  450. grant_handle_t handle, void *vaddr)
  451. {
  452. struct gnttab_unmap_grant_ref op = {
  453. .host_addr = (unsigned long)vaddr,
  454. .handle = handle,
  455. };
  456. if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &op, 1))
  457. BUG();
  458. if (op.status != GNTST_okay)
  459. xenbus_dev_error(dev, op.status,
  460. "unmapping page at handle %d error %d",
  461. handle, op.status);
  462. return op.status;
  463. }
  464. EXPORT_SYMBOL_GPL(xenbus_unmap_ring);
  465. /**
  466. * xenbus_read_driver_state
  467. * @path: path for driver
  468. *
  469. * Return the state of the driver rooted at the given store path, or
  470. * XenbusStateUnknown if no state can be read.
  471. */
  472. enum xenbus_state xenbus_read_driver_state(const char *path)
  473. {
  474. enum xenbus_state result;
  475. int err = xenbus_gather(XBT_NIL, path, "state", "%d", &result, NULL);
  476. if (err)
  477. result = XenbusStateUnknown;
  478. return result;
  479. }
  480. EXPORT_SYMBOL_GPL(xenbus_read_driver_state);