xenbus_client.c 20 KB

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