xenbus_client.c 17 KB

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