xenbus_client.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724
  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/mm.h>
  33. #include <linux/slab.h>
  34. #include <linux/types.h>
  35. #include <linux/spinlock.h>
  36. #include <linux/vmalloc.h>
  37. #include <linux/export.h>
  38. #include <asm/xen/hypervisor.h>
  39. #include <asm/xen/page.h>
  40. #include <xen/interface/xen.h>
  41. #include <xen/interface/event_channel.h>
  42. #include <xen/balloon.h>
  43. #include <xen/events.h>
  44. #include <xen/grant_table.h>
  45. #include <xen/xenbus.h>
  46. #include <xen/xen.h>
  47. #include <xen/features.h>
  48. #include "xenbus_probe.h"
  49. struct xenbus_map_node {
  50. struct list_head next;
  51. union {
  52. struct vm_struct *area; /* PV */
  53. struct page *page; /* HVM */
  54. };
  55. grant_handle_t handle;
  56. };
  57. static DEFINE_SPINLOCK(xenbus_valloc_lock);
  58. static LIST_HEAD(xenbus_valloc_pages);
  59. struct xenbus_ring_ops {
  60. int (*map)(struct xenbus_device *dev, int gnt, void **vaddr);
  61. int (*unmap)(struct xenbus_device *dev, void *vaddr);
  62. };
  63. static const struct xenbus_ring_ops *ring_ops __read_mostly;
  64. const char *xenbus_strstate(enum xenbus_state state)
  65. {
  66. static const char *const name[] = {
  67. [ XenbusStateUnknown ] = "Unknown",
  68. [ XenbusStateInitialising ] = "Initialising",
  69. [ XenbusStateInitWait ] = "InitWait",
  70. [ XenbusStateInitialised ] = "Initialised",
  71. [ XenbusStateConnected ] = "Connected",
  72. [ XenbusStateClosing ] = "Closing",
  73. [ XenbusStateClosed ] = "Closed",
  74. [XenbusStateReconfiguring] = "Reconfiguring",
  75. [XenbusStateReconfigured] = "Reconfigured",
  76. };
  77. return (state < ARRAY_SIZE(name)) ? name[state] : "INVALID";
  78. }
  79. EXPORT_SYMBOL_GPL(xenbus_strstate);
  80. /**
  81. * xenbus_watch_path - register a watch
  82. * @dev: xenbus device
  83. * @path: path to watch
  84. * @watch: watch to register
  85. * @callback: callback to register
  86. *
  87. * Register a @watch on the given path, using the given xenbus_watch structure
  88. * for storage, and the given @callback function as the callback. Return 0 on
  89. * success, or -errno on error. On success, the given @path will be saved as
  90. * @watch->node, and remains the caller's to free. On error, @watch->node will
  91. * be NULL, the device will switch to %XenbusStateClosing, and the error will
  92. * be saved in the store.
  93. */
  94. int xenbus_watch_path(struct xenbus_device *dev, const char *path,
  95. struct xenbus_watch *watch,
  96. void (*callback)(struct xenbus_watch *,
  97. const char **, unsigned int))
  98. {
  99. int err;
  100. watch->node = path;
  101. watch->callback = callback;
  102. err = register_xenbus_watch(watch);
  103. if (err) {
  104. watch->node = NULL;
  105. watch->callback = NULL;
  106. xenbus_dev_fatal(dev, err, "adding watch on %s", path);
  107. }
  108. return err;
  109. }
  110. EXPORT_SYMBOL_GPL(xenbus_watch_path);
  111. /**
  112. * xenbus_watch_pathfmt - register a watch on a sprintf-formatted path
  113. * @dev: xenbus device
  114. * @watch: watch to register
  115. * @callback: callback to register
  116. * @pathfmt: format of path to watch
  117. *
  118. * Register a watch on the given @path, using the given xenbus_watch
  119. * structure for storage, and the given @callback function as the callback.
  120. * Return 0 on success, or -errno on error. On success, the watched path
  121. * (@path/@path2) will be saved as @watch->node, and becomes the caller's to
  122. * kfree(). On error, watch->node will be NULL, so the caller has nothing to
  123. * free, the device will switch to %XenbusStateClosing, and the error will be
  124. * saved in the store.
  125. */
  126. int xenbus_watch_pathfmt(struct xenbus_device *dev,
  127. struct xenbus_watch *watch,
  128. void (*callback)(struct xenbus_watch *,
  129. const char **, unsigned int),
  130. const char *pathfmt, ...)
  131. {
  132. int err;
  133. va_list ap;
  134. char *path;
  135. va_start(ap, pathfmt);
  136. path = kvasprintf(GFP_NOIO | __GFP_HIGH, pathfmt, ap);
  137. va_end(ap);
  138. if (!path) {
  139. xenbus_dev_fatal(dev, -ENOMEM, "allocating path for watch");
  140. return -ENOMEM;
  141. }
  142. err = xenbus_watch_path(dev, path, watch, callback);
  143. if (err)
  144. kfree(path);
  145. return err;
  146. }
  147. EXPORT_SYMBOL_GPL(xenbus_watch_pathfmt);
  148. static void xenbus_switch_fatal(struct xenbus_device *, int, int,
  149. const char *, ...);
  150. static int
  151. __xenbus_switch_state(struct xenbus_device *dev,
  152. enum xenbus_state state, int depth)
  153. {
  154. /* We check whether the state is currently set to the given value, and
  155. if not, then the state is set. We don't want to unconditionally
  156. write the given state, because we don't want to fire watches
  157. unnecessarily. Furthermore, if the node has gone, we don't write
  158. to it, as the device will be tearing down, and we don't want to
  159. resurrect that directory.
  160. Note that, because of this cached value of our state, this
  161. function will not take a caller's Xenstore transaction
  162. (something it was trying to in the past) because dev->state
  163. would not get reset if the transaction was aborted.
  164. */
  165. struct xenbus_transaction xbt;
  166. int current_state;
  167. int err, abort;
  168. if (state == dev->state)
  169. return 0;
  170. again:
  171. abort = 1;
  172. err = xenbus_transaction_start(&xbt);
  173. if (err) {
  174. xenbus_switch_fatal(dev, depth, err, "starting transaction");
  175. return 0;
  176. }
  177. err = xenbus_scanf(xbt, dev->nodename, "state", "%d", &current_state);
  178. if (err != 1)
  179. goto abort;
  180. err = xenbus_printf(xbt, dev->nodename, "state", "%d", state);
  181. if (err) {
  182. xenbus_switch_fatal(dev, depth, err, "writing new state");
  183. goto abort;
  184. }
  185. abort = 0;
  186. abort:
  187. err = xenbus_transaction_end(xbt, abort);
  188. if (err) {
  189. if (err == -EAGAIN && !abort)
  190. goto again;
  191. xenbus_switch_fatal(dev, depth, err, "ending transaction");
  192. } else
  193. dev->state = state;
  194. return 0;
  195. }
  196. /**
  197. * xenbus_switch_state
  198. * @dev: xenbus device
  199. * @state: new state
  200. *
  201. * Advertise in the store a change of the given driver to the given new_state.
  202. * Return 0 on success, or -errno on error. On error, the device will switch
  203. * to XenbusStateClosing, and the error will be saved in the store.
  204. */
  205. int xenbus_switch_state(struct xenbus_device *dev, enum xenbus_state state)
  206. {
  207. return __xenbus_switch_state(dev, state, 0);
  208. }
  209. EXPORT_SYMBOL_GPL(xenbus_switch_state);
  210. int xenbus_frontend_closed(struct xenbus_device *dev)
  211. {
  212. xenbus_switch_state(dev, XenbusStateClosed);
  213. complete(&dev->down);
  214. return 0;
  215. }
  216. EXPORT_SYMBOL_GPL(xenbus_frontend_closed);
  217. /**
  218. * Return the path to the error node for the given device, or NULL on failure.
  219. * If the value returned is non-NULL, then it is the caller's to kfree.
  220. */
  221. static char *error_path(struct xenbus_device *dev)
  222. {
  223. return kasprintf(GFP_KERNEL, "error/%s", dev->nodename);
  224. }
  225. static void xenbus_va_dev_error(struct xenbus_device *dev, int err,
  226. const char *fmt, va_list ap)
  227. {
  228. int ret;
  229. unsigned int len;
  230. char *printf_buffer = NULL;
  231. char *path_buffer = NULL;
  232. #define PRINTF_BUFFER_SIZE 4096
  233. printf_buffer = kmalloc(PRINTF_BUFFER_SIZE, GFP_KERNEL);
  234. if (printf_buffer == NULL)
  235. goto fail;
  236. len = sprintf(printf_buffer, "%i ", -err);
  237. ret = vsnprintf(printf_buffer+len, PRINTF_BUFFER_SIZE-len, fmt, ap);
  238. BUG_ON(len + ret > PRINTF_BUFFER_SIZE-1);
  239. dev_err(&dev->dev, "%s\n", printf_buffer);
  240. path_buffer = error_path(dev);
  241. if (path_buffer == NULL) {
  242. dev_err(&dev->dev, "failed to write error node for %s (%s)\n",
  243. dev->nodename, printf_buffer);
  244. goto fail;
  245. }
  246. if (xenbus_write(XBT_NIL, path_buffer, "error", printf_buffer) != 0) {
  247. dev_err(&dev->dev, "failed to write error node for %s (%s)\n",
  248. dev->nodename, printf_buffer);
  249. goto fail;
  250. }
  251. fail:
  252. kfree(printf_buffer);
  253. kfree(path_buffer);
  254. }
  255. /**
  256. * xenbus_dev_error
  257. * @dev: xenbus device
  258. * @err: error to report
  259. * @fmt: error message format
  260. *
  261. * Report the given negative errno into the store, along with the given
  262. * formatted message.
  263. */
  264. void xenbus_dev_error(struct xenbus_device *dev, int err, const char *fmt, ...)
  265. {
  266. va_list ap;
  267. va_start(ap, fmt);
  268. xenbus_va_dev_error(dev, err, fmt, ap);
  269. va_end(ap);
  270. }
  271. EXPORT_SYMBOL_GPL(xenbus_dev_error);
  272. /**
  273. * xenbus_dev_fatal
  274. * @dev: xenbus device
  275. * @err: error to report
  276. * @fmt: error message format
  277. *
  278. * Equivalent to xenbus_dev_error(dev, err, fmt, args), followed by
  279. * xenbus_switch_state(dev, XenbusStateClosing) to schedule an orderly
  280. * closedown of this driver and its peer.
  281. */
  282. void xenbus_dev_fatal(struct xenbus_device *dev, int err, const char *fmt, ...)
  283. {
  284. va_list ap;
  285. va_start(ap, fmt);
  286. xenbus_va_dev_error(dev, err, fmt, ap);
  287. va_end(ap);
  288. xenbus_switch_state(dev, XenbusStateClosing);
  289. }
  290. EXPORT_SYMBOL_GPL(xenbus_dev_fatal);
  291. /**
  292. * Equivalent to xenbus_dev_fatal(dev, err, fmt, args), but helps
  293. * avoiding recursion within xenbus_switch_state.
  294. */
  295. static void xenbus_switch_fatal(struct xenbus_device *dev, int depth, int err,
  296. const char *fmt, ...)
  297. {
  298. va_list ap;
  299. va_start(ap, fmt);
  300. xenbus_va_dev_error(dev, err, fmt, ap);
  301. va_end(ap);
  302. if (!depth)
  303. __xenbus_switch_state(dev, XenbusStateClosing, 1);
  304. }
  305. /**
  306. * xenbus_grant_ring
  307. * @dev: xenbus device
  308. * @ring_mfn: mfn of ring to grant
  309. * Grant access to the given @ring_mfn to the peer of the given device. Return
  310. * 0 on success, or -errno on error. On error, the device will switch to
  311. * XenbusStateClosing, and the error will be saved in the store.
  312. */
  313. int xenbus_grant_ring(struct xenbus_device *dev, unsigned long ring_mfn)
  314. {
  315. int err = gnttab_grant_foreign_access(dev->otherend_id, ring_mfn, 0);
  316. if (err < 0)
  317. xenbus_dev_fatal(dev, err, "granting access to ring page");
  318. return err;
  319. }
  320. EXPORT_SYMBOL_GPL(xenbus_grant_ring);
  321. /**
  322. * Allocate an event channel for the given xenbus_device, assigning the newly
  323. * created local port to *port. Return 0 on success, or -errno on error. On
  324. * error, the device will switch to XenbusStateClosing, and the error will be
  325. * saved in the store.
  326. */
  327. int xenbus_alloc_evtchn(struct xenbus_device *dev, int *port)
  328. {
  329. struct evtchn_alloc_unbound alloc_unbound;
  330. int err;
  331. alloc_unbound.dom = DOMID_SELF;
  332. alloc_unbound.remote_dom = dev->otherend_id;
  333. err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
  334. &alloc_unbound);
  335. if (err)
  336. xenbus_dev_fatal(dev, err, "allocating event channel");
  337. else
  338. *port = alloc_unbound.port;
  339. return err;
  340. }
  341. EXPORT_SYMBOL_GPL(xenbus_alloc_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. return ring_ops->map(dev, gnt_ref, vaddr);
  373. }
  374. EXPORT_SYMBOL_GPL(xenbus_map_ring_valloc);
  375. static int xenbus_map_ring_valloc_pv(struct xenbus_device *dev,
  376. int gnt_ref, void **vaddr)
  377. {
  378. struct gnttab_map_grant_ref op = {
  379. .flags = GNTMAP_host_map | GNTMAP_contains_pte,
  380. .ref = gnt_ref,
  381. .dom = dev->otherend_id,
  382. };
  383. struct xenbus_map_node *node;
  384. struct vm_struct *area;
  385. pte_t *pte;
  386. *vaddr = NULL;
  387. node = kzalloc(sizeof(*node), GFP_KERNEL);
  388. if (!node)
  389. return -ENOMEM;
  390. area = alloc_vm_area(PAGE_SIZE, &pte);
  391. if (!area) {
  392. kfree(node);
  393. return -ENOMEM;
  394. }
  395. op.host_addr = arbitrary_virt_to_machine(pte).maddr;
  396. gnttab_batch_map(&op, 1);
  397. if (op.status != GNTST_okay) {
  398. free_vm_area(area);
  399. kfree(node);
  400. xenbus_dev_fatal(dev, op.status,
  401. "mapping in shared page %d from domain %d",
  402. gnt_ref, dev->otherend_id);
  403. return op.status;
  404. }
  405. node->handle = op.handle;
  406. node->area = area;
  407. spin_lock(&xenbus_valloc_lock);
  408. list_add(&node->next, &xenbus_valloc_pages);
  409. spin_unlock(&xenbus_valloc_lock);
  410. *vaddr = area->addr;
  411. return 0;
  412. }
  413. static int xenbus_map_ring_valloc_hvm(struct xenbus_device *dev,
  414. int gnt_ref, void **vaddr)
  415. {
  416. struct xenbus_map_node *node;
  417. int err;
  418. void *addr;
  419. *vaddr = NULL;
  420. node = kzalloc(sizeof(*node), GFP_KERNEL);
  421. if (!node)
  422. return -ENOMEM;
  423. err = alloc_xenballooned_pages(1, &node->page, false /* lowmem */);
  424. if (err)
  425. goto out_err;
  426. addr = pfn_to_kaddr(page_to_pfn(node->page));
  427. err = xenbus_map_ring(dev, gnt_ref, &node->handle, addr);
  428. if (err)
  429. goto out_err_free_ballooned_pages;
  430. spin_lock(&xenbus_valloc_lock);
  431. list_add(&node->next, &xenbus_valloc_pages);
  432. spin_unlock(&xenbus_valloc_lock);
  433. *vaddr = addr;
  434. return 0;
  435. out_err_free_ballooned_pages:
  436. free_xenballooned_pages(1, &node->page);
  437. out_err:
  438. kfree(node);
  439. return err;
  440. }
  441. /**
  442. * xenbus_map_ring
  443. * @dev: xenbus device
  444. * @gnt_ref: grant reference
  445. * @handle: pointer to grant handle to be filled
  446. * @vaddr: address to be mapped to
  447. *
  448. * Map a page of memory into this domain from another domain's grant table.
  449. * xenbus_map_ring does not allocate the virtual address space (you must do
  450. * this yourself!). It only maps in the page to the specified address.
  451. * Returns 0 on success, and GNTST_* (see xen/include/interface/grant_table.h)
  452. * or -ENOMEM on error. If an error is returned, device will switch to
  453. * XenbusStateClosing and the error message will be saved in XenStore.
  454. */
  455. int xenbus_map_ring(struct xenbus_device *dev, int gnt_ref,
  456. grant_handle_t *handle, void *vaddr)
  457. {
  458. struct gnttab_map_grant_ref op;
  459. gnttab_set_map_op(&op, (unsigned long)vaddr, GNTMAP_host_map, gnt_ref,
  460. dev->otherend_id);
  461. gnttab_batch_map(&op, 1);
  462. if (op.status != GNTST_okay) {
  463. xenbus_dev_fatal(dev, op.status,
  464. "mapping in shared page %d from domain %d",
  465. gnt_ref, dev->otherend_id);
  466. } else
  467. *handle = op.handle;
  468. return op.status;
  469. }
  470. EXPORT_SYMBOL_GPL(xenbus_map_ring);
  471. /**
  472. * xenbus_unmap_ring_vfree
  473. * @dev: xenbus device
  474. * @vaddr: addr to unmap
  475. *
  476. * Based on Rusty Russell's skeleton driver's unmap_page.
  477. * Unmap a page of memory in this domain that was imported from another domain.
  478. * Use xenbus_unmap_ring_vfree if you mapped in your memory with
  479. * xenbus_map_ring_valloc (it will free the virtual address space).
  480. * Returns 0 on success and returns GNTST_* on error
  481. * (see xen/include/interface/grant_table.h).
  482. */
  483. int xenbus_unmap_ring_vfree(struct xenbus_device *dev, void *vaddr)
  484. {
  485. return ring_ops->unmap(dev, vaddr);
  486. }
  487. EXPORT_SYMBOL_GPL(xenbus_unmap_ring_vfree);
  488. static int xenbus_unmap_ring_vfree_pv(struct xenbus_device *dev, void *vaddr)
  489. {
  490. struct xenbus_map_node *node;
  491. struct gnttab_unmap_grant_ref op = {
  492. .host_addr = (unsigned long)vaddr,
  493. };
  494. unsigned int level;
  495. spin_lock(&xenbus_valloc_lock);
  496. list_for_each_entry(node, &xenbus_valloc_pages, next) {
  497. if (node->area->addr == vaddr) {
  498. list_del(&node->next);
  499. goto found;
  500. }
  501. }
  502. node = NULL;
  503. found:
  504. spin_unlock(&xenbus_valloc_lock);
  505. if (!node) {
  506. xenbus_dev_error(dev, -ENOENT,
  507. "can't find mapped virtual address %p", vaddr);
  508. return GNTST_bad_virt_addr;
  509. }
  510. op.handle = node->handle;
  511. op.host_addr = arbitrary_virt_to_machine(
  512. lookup_address((unsigned long)vaddr, &level)).maddr;
  513. if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &op, 1))
  514. BUG();
  515. if (op.status == GNTST_okay)
  516. free_vm_area(node->area);
  517. else
  518. xenbus_dev_error(dev, op.status,
  519. "unmapping page at handle %d error %d",
  520. node->handle, op.status);
  521. kfree(node);
  522. return op.status;
  523. }
  524. static int xenbus_unmap_ring_vfree_hvm(struct xenbus_device *dev, void *vaddr)
  525. {
  526. int rv;
  527. struct xenbus_map_node *node;
  528. void *addr;
  529. spin_lock(&xenbus_valloc_lock);
  530. list_for_each_entry(node, &xenbus_valloc_pages, next) {
  531. addr = pfn_to_kaddr(page_to_pfn(node->page));
  532. if (addr == vaddr) {
  533. list_del(&node->next);
  534. goto found;
  535. }
  536. }
  537. node = addr = NULL;
  538. found:
  539. spin_unlock(&xenbus_valloc_lock);
  540. if (!node) {
  541. xenbus_dev_error(dev, -ENOENT,
  542. "can't find mapped virtual address %p", vaddr);
  543. return GNTST_bad_virt_addr;
  544. }
  545. rv = xenbus_unmap_ring(dev, node->handle, addr);
  546. if (!rv)
  547. free_xenballooned_pages(1, &node->page);
  548. else
  549. WARN(1, "Leaking %p\n", vaddr);
  550. kfree(node);
  551. return rv;
  552. }
  553. /**
  554. * xenbus_unmap_ring
  555. * @dev: xenbus device
  556. * @handle: grant handle
  557. * @vaddr: addr to unmap
  558. *
  559. * Unmap a page of memory in this domain that was imported from another domain.
  560. * Returns 0 on success and returns GNTST_* on error
  561. * (see xen/include/interface/grant_table.h).
  562. */
  563. int xenbus_unmap_ring(struct xenbus_device *dev,
  564. grant_handle_t handle, void *vaddr)
  565. {
  566. struct gnttab_unmap_grant_ref op;
  567. gnttab_set_unmap_op(&op, (unsigned long)vaddr, GNTMAP_host_map, handle);
  568. if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, &op, 1))
  569. BUG();
  570. if (op.status != GNTST_okay)
  571. xenbus_dev_error(dev, op.status,
  572. "unmapping page at handle %d error %d",
  573. handle, op.status);
  574. return op.status;
  575. }
  576. EXPORT_SYMBOL_GPL(xenbus_unmap_ring);
  577. /**
  578. * xenbus_read_driver_state
  579. * @path: path for driver
  580. *
  581. * Return the state of the driver rooted at the given store path, or
  582. * XenbusStateUnknown if no state can be read.
  583. */
  584. enum xenbus_state xenbus_read_driver_state(const char *path)
  585. {
  586. enum xenbus_state result;
  587. int err = xenbus_gather(XBT_NIL, path, "state", "%d", &result, NULL);
  588. if (err)
  589. result = XenbusStateUnknown;
  590. return result;
  591. }
  592. EXPORT_SYMBOL_GPL(xenbus_read_driver_state);
  593. static const struct xenbus_ring_ops ring_ops_pv = {
  594. .map = xenbus_map_ring_valloc_pv,
  595. .unmap = xenbus_unmap_ring_vfree_pv,
  596. };
  597. static const struct xenbus_ring_ops ring_ops_hvm = {
  598. .map = xenbus_map_ring_valloc_hvm,
  599. .unmap = xenbus_unmap_ring_vfree_hvm,
  600. };
  601. void __init xenbus_ring_ops_init(void)
  602. {
  603. if (!xen_feature(XENFEAT_auto_translated_physmap))
  604. ring_ops = &ring_ops_pv;
  605. else
  606. ring_ops = &ring_ops_hvm;
  607. }