xenbus_client.c 16 KB

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