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