xenbus_client.c 17 KB

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