xenbus_client.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940
  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 <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.h"
  49. #define XENBUS_PAGES(_grants) (DIV_ROUND_UP(_grants, XEN_PFN_PER_PAGE))
  50. #define XENBUS_MAX_RING_PAGES (XENBUS_PAGES(XENBUS_MAX_RING_GRANTS))
  51. struct xenbus_map_node {
  52. struct list_head next;
  53. union {
  54. struct {
  55. struct vm_struct *area;
  56. } pv;
  57. struct {
  58. struct page *pages[XENBUS_MAX_RING_PAGES];
  59. unsigned long addrs[XENBUS_MAX_RING_GRANTS];
  60. void *addr;
  61. } hvm;
  62. };
  63. grant_handle_t handles[XENBUS_MAX_RING_GRANTS];
  64. unsigned int nr_handles;
  65. };
  66. static DEFINE_SPINLOCK(xenbus_valloc_lock);
  67. static LIST_HEAD(xenbus_valloc_pages);
  68. struct xenbus_ring_ops {
  69. int (*map)(struct xenbus_device *dev,
  70. grant_ref_t *gnt_refs, unsigned int nr_grefs,
  71. void **vaddr);
  72. int (*unmap)(struct xenbus_device *dev, void *vaddr);
  73. };
  74. static const struct xenbus_ring_ops *ring_ops __read_mostly;
  75. const char *xenbus_strstate(enum xenbus_state state)
  76. {
  77. static const char *const name[] = {
  78. [ XenbusStateUnknown ] = "Unknown",
  79. [ XenbusStateInitialising ] = "Initialising",
  80. [ XenbusStateInitWait ] = "InitWait",
  81. [ XenbusStateInitialised ] = "Initialised",
  82. [ XenbusStateConnected ] = "Connected",
  83. [ XenbusStateClosing ] = "Closing",
  84. [ XenbusStateClosed ] = "Closed",
  85. [XenbusStateReconfiguring] = "Reconfiguring",
  86. [XenbusStateReconfigured] = "Reconfigured",
  87. };
  88. return (state < ARRAY_SIZE(name)) ? name[state] : "INVALID";
  89. }
  90. EXPORT_SYMBOL_GPL(xenbus_strstate);
  91. /**
  92. * xenbus_watch_path - register a watch
  93. * @dev: xenbus device
  94. * @path: path to watch
  95. * @watch: watch to register
  96. * @callback: callback to register
  97. *
  98. * Register a @watch on the given path, using the given xenbus_watch structure
  99. * for storage, and the given @callback function as the callback. Return 0 on
  100. * success, or -errno on error. On success, the given @path will be saved as
  101. * @watch->node, and remains the caller's to free. On error, @watch->node will
  102. * be NULL, the device will switch to %XenbusStateClosing, and the error will
  103. * be saved in the store.
  104. */
  105. int xenbus_watch_path(struct xenbus_device *dev, const char *path,
  106. struct xenbus_watch *watch,
  107. void (*callback)(struct xenbus_watch *,
  108. const char *, const char *))
  109. {
  110. int err;
  111. watch->node = path;
  112. watch->callback = callback;
  113. err = register_xenbus_watch(watch);
  114. if (err) {
  115. watch->node = NULL;
  116. watch->callback = NULL;
  117. xenbus_dev_fatal(dev, err, "adding watch on %s", path);
  118. }
  119. return err;
  120. }
  121. EXPORT_SYMBOL_GPL(xenbus_watch_path);
  122. /**
  123. * xenbus_watch_pathfmt - register a watch on a sprintf-formatted path
  124. * @dev: xenbus device
  125. * @watch: watch to register
  126. * @callback: callback to register
  127. * @pathfmt: format of path to watch
  128. *
  129. * Register a watch on the given @path, using the given xenbus_watch
  130. * structure for storage, and the given @callback function as the callback.
  131. * Return 0 on success, or -errno on error. On success, the watched path
  132. * (@path/@path2) will be saved as @watch->node, and becomes the caller's to
  133. * kfree(). On error, watch->node will be NULL, so the caller has nothing to
  134. * free, the device will switch to %XenbusStateClosing, and the error will be
  135. * saved in the store.
  136. */
  137. int xenbus_watch_pathfmt(struct xenbus_device *dev,
  138. struct xenbus_watch *watch,
  139. void (*callback)(struct xenbus_watch *,
  140. const char *, const char *),
  141. const char *pathfmt, ...)
  142. {
  143. int err;
  144. va_list ap;
  145. char *path;
  146. va_start(ap, pathfmt);
  147. path = kvasprintf(GFP_NOIO | __GFP_HIGH, pathfmt, ap);
  148. va_end(ap);
  149. if (!path) {
  150. xenbus_dev_fatal(dev, -ENOMEM, "allocating path for watch");
  151. return -ENOMEM;
  152. }
  153. err = xenbus_watch_path(dev, path, watch, callback);
  154. if (err)
  155. kfree(path);
  156. return err;
  157. }
  158. EXPORT_SYMBOL_GPL(xenbus_watch_pathfmt);
  159. static void xenbus_switch_fatal(struct xenbus_device *, int, int,
  160. const char *, ...);
  161. static int
  162. __xenbus_switch_state(struct xenbus_device *dev,
  163. enum xenbus_state state, int depth)
  164. {
  165. /* We check whether the state is currently set to the given value, and
  166. if not, then the state is set. We don't want to unconditionally
  167. write the given state, because we don't want to fire watches
  168. unnecessarily. Furthermore, if the node has gone, we don't write
  169. to it, as the device will be tearing down, and we don't want to
  170. resurrect that directory.
  171. Note that, because of this cached value of our state, this
  172. function will not take a caller's Xenstore transaction
  173. (something it was trying to in the past) because dev->state
  174. would not get reset if the transaction was aborted.
  175. */
  176. struct xenbus_transaction xbt;
  177. int current_state;
  178. int err, abort;
  179. if (state == dev->state)
  180. return 0;
  181. again:
  182. abort = 1;
  183. err = xenbus_transaction_start(&xbt);
  184. if (err) {
  185. xenbus_switch_fatal(dev, depth, err, "starting transaction");
  186. return 0;
  187. }
  188. err = xenbus_scanf(xbt, dev->nodename, "state", "%d", &current_state);
  189. if (err != 1)
  190. goto abort;
  191. err = xenbus_printf(xbt, dev->nodename, "state", "%d", state);
  192. if (err) {
  193. xenbus_switch_fatal(dev, depth, err, "writing new state");
  194. goto abort;
  195. }
  196. abort = 0;
  197. abort:
  198. err = xenbus_transaction_end(xbt, abort);
  199. if (err) {
  200. if (err == -EAGAIN && !abort)
  201. goto again;
  202. xenbus_switch_fatal(dev, depth, err, "ending transaction");
  203. } else
  204. dev->state = state;
  205. return 0;
  206. }
  207. /**
  208. * xenbus_switch_state
  209. * @dev: xenbus device
  210. * @state: new state
  211. *
  212. * Advertise in the store a change of the given driver to the given new_state.
  213. * Return 0 on success, or -errno on error. On error, the device will switch
  214. * to XenbusStateClosing, and the error will be saved in the store.
  215. */
  216. int xenbus_switch_state(struct xenbus_device *dev, enum xenbus_state state)
  217. {
  218. return __xenbus_switch_state(dev, state, 0);
  219. }
  220. EXPORT_SYMBOL_GPL(xenbus_switch_state);
  221. int xenbus_frontend_closed(struct xenbus_device *dev)
  222. {
  223. xenbus_switch_state(dev, XenbusStateClosed);
  224. complete(&dev->down);
  225. return 0;
  226. }
  227. EXPORT_SYMBOL_GPL(xenbus_frontend_closed);
  228. static void xenbus_va_dev_error(struct xenbus_device *dev, int err,
  229. const char *fmt, va_list ap)
  230. {
  231. unsigned int len;
  232. char *printf_buffer;
  233. char *path_buffer;
  234. #define PRINTF_BUFFER_SIZE 4096
  235. printf_buffer = kmalloc(PRINTF_BUFFER_SIZE, GFP_KERNEL);
  236. if (!printf_buffer)
  237. return;
  238. len = sprintf(printf_buffer, "%i ", -err);
  239. vsnprintf(printf_buffer + len, PRINTF_BUFFER_SIZE - len, fmt, ap);
  240. dev_err(&dev->dev, "%s\n", printf_buffer);
  241. path_buffer = kasprintf(GFP_KERNEL, "error/%s", dev->nodename);
  242. if (!path_buffer ||
  243. xenbus_write(XBT_NIL, path_buffer, "error", printf_buffer))
  244. dev_err(&dev->dev, "failed to write error node for %s (%s)\n",
  245. dev->nodename, printf_buffer);
  246. kfree(printf_buffer);
  247. kfree(path_buffer);
  248. }
  249. /**
  250. * xenbus_dev_error
  251. * @dev: xenbus device
  252. * @err: error to report
  253. * @fmt: error message format
  254. *
  255. * Report the given negative errno into the store, along with the given
  256. * formatted message.
  257. */
  258. void xenbus_dev_error(struct xenbus_device *dev, int err, const char *fmt, ...)
  259. {
  260. va_list ap;
  261. va_start(ap, fmt);
  262. xenbus_va_dev_error(dev, err, fmt, ap);
  263. va_end(ap);
  264. }
  265. EXPORT_SYMBOL_GPL(xenbus_dev_error);
  266. /**
  267. * xenbus_dev_fatal
  268. * @dev: xenbus device
  269. * @err: error to report
  270. * @fmt: error message format
  271. *
  272. * Equivalent to xenbus_dev_error(dev, err, fmt, args), followed by
  273. * xenbus_switch_state(dev, XenbusStateClosing) to schedule an orderly
  274. * closedown of this driver and its peer.
  275. */
  276. void xenbus_dev_fatal(struct xenbus_device *dev, int err, const char *fmt, ...)
  277. {
  278. va_list ap;
  279. va_start(ap, fmt);
  280. xenbus_va_dev_error(dev, err, fmt, ap);
  281. va_end(ap);
  282. xenbus_switch_state(dev, XenbusStateClosing);
  283. }
  284. EXPORT_SYMBOL_GPL(xenbus_dev_fatal);
  285. /**
  286. * Equivalent to xenbus_dev_fatal(dev, err, fmt, args), but helps
  287. * avoiding recursion within xenbus_switch_state.
  288. */
  289. static void xenbus_switch_fatal(struct xenbus_device *dev, int depth, int err,
  290. const char *fmt, ...)
  291. {
  292. va_list ap;
  293. va_start(ap, fmt);
  294. xenbus_va_dev_error(dev, err, fmt, ap);
  295. va_end(ap);
  296. if (!depth)
  297. __xenbus_switch_state(dev, XenbusStateClosing, 1);
  298. }
  299. /**
  300. * xenbus_grant_ring
  301. * @dev: xenbus device
  302. * @vaddr: starting virtual address of the ring
  303. * @nr_pages: number of pages to be granted
  304. * @grefs: grant reference array to be filled in
  305. *
  306. * Grant access to the given @vaddr to the peer of the given device.
  307. * Then fill in @grefs with grant references. Return 0 on success, or
  308. * -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, void *vaddr,
  312. unsigned int nr_pages, grant_ref_t *grefs)
  313. {
  314. int err;
  315. int i, j;
  316. for (i = 0; i < nr_pages; i++) {
  317. err = gnttab_grant_foreign_access(dev->otherend_id,
  318. virt_to_gfn(vaddr), 0);
  319. if (err < 0) {
  320. xenbus_dev_fatal(dev, err,
  321. "granting access to ring page");
  322. goto fail;
  323. }
  324. grefs[i] = err;
  325. vaddr = vaddr + XEN_PAGE_SIZE;
  326. }
  327. return 0;
  328. fail:
  329. for (j = 0; j < i; j++)
  330. gnttab_end_foreign_access_ref(grefs[j], 0);
  331. return err;
  332. }
  333. EXPORT_SYMBOL_GPL(xenbus_grant_ring);
  334. /**
  335. * Allocate an event channel for the given xenbus_device, assigning the newly
  336. * created local port to *port. Return 0 on success, or -errno on error. On
  337. * error, the device will switch to XenbusStateClosing, and the error will be
  338. * saved in the store.
  339. */
  340. int xenbus_alloc_evtchn(struct xenbus_device *dev, int *port)
  341. {
  342. struct evtchn_alloc_unbound alloc_unbound;
  343. int err;
  344. alloc_unbound.dom = DOMID_SELF;
  345. alloc_unbound.remote_dom = dev->otherend_id;
  346. err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound,
  347. &alloc_unbound);
  348. if (err)
  349. xenbus_dev_fatal(dev, err, "allocating event channel");
  350. else
  351. *port = alloc_unbound.port;
  352. return err;
  353. }
  354. EXPORT_SYMBOL_GPL(xenbus_alloc_evtchn);
  355. /**
  356. * Free an existing event channel. Returns 0 on success or -errno on error.
  357. */
  358. int xenbus_free_evtchn(struct xenbus_device *dev, int port)
  359. {
  360. struct evtchn_close close;
  361. int err;
  362. close.port = port;
  363. err = HYPERVISOR_event_channel_op(EVTCHNOP_close, &close);
  364. if (err)
  365. xenbus_dev_error(dev, err, "freeing event channel %d", port);
  366. return err;
  367. }
  368. EXPORT_SYMBOL_GPL(xenbus_free_evtchn);
  369. /**
  370. * xenbus_map_ring_valloc
  371. * @dev: xenbus device
  372. * @gnt_refs: grant reference array
  373. * @nr_grefs: number of grant references
  374. * @vaddr: pointer to address to be filled out by mapping
  375. *
  376. * Map @nr_grefs pages of memory into this domain from another
  377. * domain's grant table. xenbus_map_ring_valloc allocates @nr_grefs
  378. * pages of virtual address space, maps the pages to that address, and
  379. * sets *vaddr to that address. Returns 0 on success, and GNTST_*
  380. * (see xen/include/interface/grant_table.h) or -ENOMEM / -EINVAL on
  381. * error. If an error is returned, device will switch to
  382. * XenbusStateClosing and the error message will be saved in XenStore.
  383. */
  384. int xenbus_map_ring_valloc(struct xenbus_device *dev, grant_ref_t *gnt_refs,
  385. unsigned int nr_grefs, void **vaddr)
  386. {
  387. return ring_ops->map(dev, gnt_refs, nr_grefs, vaddr);
  388. }
  389. EXPORT_SYMBOL_GPL(xenbus_map_ring_valloc);
  390. /* N.B. sizeof(phys_addr_t) doesn't always equal to sizeof(unsigned
  391. * long), e.g. 32-on-64. Caller is responsible for preparing the
  392. * right array to feed into this function */
  393. static int __xenbus_map_ring(struct xenbus_device *dev,
  394. grant_ref_t *gnt_refs,
  395. unsigned int nr_grefs,
  396. grant_handle_t *handles,
  397. phys_addr_t *addrs,
  398. unsigned int flags,
  399. bool *leaked)
  400. {
  401. struct gnttab_map_grant_ref map[XENBUS_MAX_RING_GRANTS];
  402. struct gnttab_unmap_grant_ref unmap[XENBUS_MAX_RING_GRANTS];
  403. int i, j;
  404. int err = GNTST_okay;
  405. if (nr_grefs > XENBUS_MAX_RING_GRANTS)
  406. return -EINVAL;
  407. for (i = 0; i < nr_grefs; i++) {
  408. memset(&map[i], 0, sizeof(map[i]));
  409. gnttab_set_map_op(&map[i], addrs[i], flags, gnt_refs[i],
  410. dev->otherend_id);
  411. handles[i] = INVALID_GRANT_HANDLE;
  412. }
  413. gnttab_batch_map(map, i);
  414. for (i = 0; i < nr_grefs; i++) {
  415. if (map[i].status != GNTST_okay) {
  416. err = map[i].status;
  417. xenbus_dev_fatal(dev, map[i].status,
  418. "mapping in shared page %d from domain %d",
  419. gnt_refs[i], dev->otherend_id);
  420. goto fail;
  421. } else
  422. handles[i] = map[i].handle;
  423. }
  424. return GNTST_okay;
  425. fail:
  426. for (i = j = 0; i < nr_grefs; i++) {
  427. if (handles[i] != INVALID_GRANT_HANDLE) {
  428. memset(&unmap[j], 0, sizeof(unmap[j]));
  429. gnttab_set_unmap_op(&unmap[j], (phys_addr_t)addrs[i],
  430. GNTMAP_host_map, handles[i]);
  431. j++;
  432. }
  433. }
  434. if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, unmap, j))
  435. BUG();
  436. *leaked = false;
  437. for (i = 0; i < j; i++) {
  438. if (unmap[i].status != GNTST_okay) {
  439. *leaked = true;
  440. break;
  441. }
  442. }
  443. return err;
  444. }
  445. struct map_ring_valloc_hvm
  446. {
  447. unsigned int idx;
  448. /* Why do we need two arrays? See comment of __xenbus_map_ring */
  449. phys_addr_t phys_addrs[XENBUS_MAX_RING_GRANTS];
  450. unsigned long addrs[XENBUS_MAX_RING_GRANTS];
  451. };
  452. static void xenbus_map_ring_setup_grant_hvm(unsigned long gfn,
  453. unsigned int goffset,
  454. unsigned int len,
  455. void *data)
  456. {
  457. struct map_ring_valloc_hvm *info = data;
  458. unsigned long vaddr = (unsigned long)gfn_to_virt(gfn);
  459. info->phys_addrs[info->idx] = vaddr;
  460. info->addrs[info->idx] = vaddr;
  461. info->idx++;
  462. }
  463. static int xenbus_map_ring_valloc_hvm(struct xenbus_device *dev,
  464. grant_ref_t *gnt_ref,
  465. unsigned int nr_grefs,
  466. void **vaddr)
  467. {
  468. struct xenbus_map_node *node;
  469. int err;
  470. void *addr;
  471. bool leaked = false;
  472. struct map_ring_valloc_hvm info = {
  473. .idx = 0,
  474. };
  475. unsigned int nr_pages = XENBUS_PAGES(nr_grefs);
  476. if (nr_grefs > XENBUS_MAX_RING_GRANTS)
  477. return -EINVAL;
  478. *vaddr = NULL;
  479. node = kzalloc(sizeof(*node), GFP_KERNEL);
  480. if (!node)
  481. return -ENOMEM;
  482. err = alloc_xenballooned_pages(nr_pages, node->hvm.pages);
  483. if (err)
  484. goto out_err;
  485. gnttab_foreach_grant(node->hvm.pages, nr_grefs,
  486. xenbus_map_ring_setup_grant_hvm,
  487. &info);
  488. err = __xenbus_map_ring(dev, gnt_ref, nr_grefs, node->handles,
  489. info.phys_addrs, GNTMAP_host_map, &leaked);
  490. node->nr_handles = nr_grefs;
  491. if (err)
  492. goto out_free_ballooned_pages;
  493. addr = vmap(node->hvm.pages, nr_pages, VM_MAP | VM_IOREMAP,
  494. PAGE_KERNEL);
  495. if (!addr) {
  496. err = -ENOMEM;
  497. goto out_xenbus_unmap_ring;
  498. }
  499. node->hvm.addr = addr;
  500. spin_lock(&xenbus_valloc_lock);
  501. list_add(&node->next, &xenbus_valloc_pages);
  502. spin_unlock(&xenbus_valloc_lock);
  503. *vaddr = addr;
  504. return 0;
  505. out_xenbus_unmap_ring:
  506. if (!leaked)
  507. xenbus_unmap_ring(dev, node->handles, nr_grefs, info.addrs);
  508. else
  509. pr_alert("leaking %p size %u page(s)",
  510. addr, nr_pages);
  511. out_free_ballooned_pages:
  512. if (!leaked)
  513. free_xenballooned_pages(nr_pages, node->hvm.pages);
  514. out_err:
  515. kfree(node);
  516. return err;
  517. }
  518. /**
  519. * xenbus_map_ring
  520. * @dev: xenbus device
  521. * @gnt_refs: grant reference array
  522. * @nr_grefs: number of grant reference
  523. * @handles: pointer to grant handle to be filled
  524. * @vaddrs: addresses to be mapped to
  525. * @leaked: fail to clean up a failed map, caller should not free vaddr
  526. *
  527. * Map pages of memory into this domain from another domain's grant table.
  528. * xenbus_map_ring does not allocate the virtual address space (you must do
  529. * this yourself!). It only maps in the pages to the specified address.
  530. * Returns 0 on success, and GNTST_* (see xen/include/interface/grant_table.h)
  531. * or -ENOMEM / -EINVAL on error. If an error is returned, device will switch to
  532. * XenbusStateClosing and the first error message will be saved in XenStore.
  533. * Further more if we fail to map the ring, caller should check @leaked.
  534. * If @leaked is not zero it means xenbus_map_ring fails to clean up, caller
  535. * should not free the address space of @vaddr.
  536. */
  537. int xenbus_map_ring(struct xenbus_device *dev, grant_ref_t *gnt_refs,
  538. unsigned int nr_grefs, grant_handle_t *handles,
  539. unsigned long *vaddrs, bool *leaked)
  540. {
  541. phys_addr_t phys_addrs[XENBUS_MAX_RING_GRANTS];
  542. int i;
  543. if (nr_grefs > XENBUS_MAX_RING_GRANTS)
  544. return -EINVAL;
  545. for (i = 0; i < nr_grefs; i++)
  546. phys_addrs[i] = (unsigned long)vaddrs[i];
  547. return __xenbus_map_ring(dev, gnt_refs, nr_grefs, handles,
  548. phys_addrs, GNTMAP_host_map, leaked);
  549. }
  550. EXPORT_SYMBOL_GPL(xenbus_map_ring);
  551. /**
  552. * xenbus_unmap_ring_vfree
  553. * @dev: xenbus device
  554. * @vaddr: addr to unmap
  555. *
  556. * Based on Rusty Russell's skeleton driver's unmap_page.
  557. * Unmap a page of memory in this domain that was imported from another domain.
  558. * Use xenbus_unmap_ring_vfree if you mapped in your memory with
  559. * xenbus_map_ring_valloc (it will free the virtual address space).
  560. * Returns 0 on success and returns GNTST_* on error
  561. * (see xen/include/interface/grant_table.h).
  562. */
  563. int xenbus_unmap_ring_vfree(struct xenbus_device *dev, void *vaddr)
  564. {
  565. return ring_ops->unmap(dev, vaddr);
  566. }
  567. EXPORT_SYMBOL_GPL(xenbus_unmap_ring_vfree);
  568. #ifdef CONFIG_XEN_PV
  569. static int xenbus_map_ring_valloc_pv(struct xenbus_device *dev,
  570. grant_ref_t *gnt_refs,
  571. unsigned int nr_grefs,
  572. void **vaddr)
  573. {
  574. struct xenbus_map_node *node;
  575. struct vm_struct *area;
  576. pte_t *ptes[XENBUS_MAX_RING_GRANTS];
  577. phys_addr_t phys_addrs[XENBUS_MAX_RING_GRANTS];
  578. int err = GNTST_okay;
  579. int i;
  580. bool leaked;
  581. *vaddr = NULL;
  582. if (nr_grefs > XENBUS_MAX_RING_GRANTS)
  583. return -EINVAL;
  584. node = kzalloc(sizeof(*node), GFP_KERNEL);
  585. if (!node)
  586. return -ENOMEM;
  587. area = alloc_vm_area(XEN_PAGE_SIZE * nr_grefs, ptes);
  588. if (!area) {
  589. kfree(node);
  590. return -ENOMEM;
  591. }
  592. for (i = 0; i < nr_grefs; i++)
  593. phys_addrs[i] = arbitrary_virt_to_machine(ptes[i]).maddr;
  594. err = __xenbus_map_ring(dev, gnt_refs, nr_grefs, node->handles,
  595. phys_addrs,
  596. GNTMAP_host_map | GNTMAP_contains_pte,
  597. &leaked);
  598. if (err)
  599. goto failed;
  600. node->nr_handles = nr_grefs;
  601. node->pv.area = area;
  602. spin_lock(&xenbus_valloc_lock);
  603. list_add(&node->next, &xenbus_valloc_pages);
  604. spin_unlock(&xenbus_valloc_lock);
  605. *vaddr = area->addr;
  606. return 0;
  607. failed:
  608. if (!leaked)
  609. free_vm_area(area);
  610. else
  611. pr_alert("leaking VM area %p size %u page(s)", area, nr_grefs);
  612. kfree(node);
  613. return err;
  614. }
  615. static int xenbus_unmap_ring_vfree_pv(struct xenbus_device *dev, void *vaddr)
  616. {
  617. struct xenbus_map_node *node;
  618. struct gnttab_unmap_grant_ref unmap[XENBUS_MAX_RING_GRANTS];
  619. unsigned int level;
  620. int i;
  621. bool leaked = false;
  622. int err;
  623. spin_lock(&xenbus_valloc_lock);
  624. list_for_each_entry(node, &xenbus_valloc_pages, next) {
  625. if (node->pv.area->addr == vaddr) {
  626. list_del(&node->next);
  627. goto found;
  628. }
  629. }
  630. node = NULL;
  631. found:
  632. spin_unlock(&xenbus_valloc_lock);
  633. if (!node) {
  634. xenbus_dev_error(dev, -ENOENT,
  635. "can't find mapped virtual address %p", vaddr);
  636. return GNTST_bad_virt_addr;
  637. }
  638. for (i = 0; i < node->nr_handles; i++) {
  639. unsigned long addr;
  640. memset(&unmap[i], 0, sizeof(unmap[i]));
  641. addr = (unsigned long)vaddr + (XEN_PAGE_SIZE * i);
  642. unmap[i].host_addr = arbitrary_virt_to_machine(
  643. lookup_address(addr, &level)).maddr;
  644. unmap[i].dev_bus_addr = 0;
  645. unmap[i].handle = node->handles[i];
  646. }
  647. if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, unmap, i))
  648. BUG();
  649. err = GNTST_okay;
  650. leaked = false;
  651. for (i = 0; i < node->nr_handles; i++) {
  652. if (unmap[i].status != GNTST_okay) {
  653. leaked = true;
  654. xenbus_dev_error(dev, unmap[i].status,
  655. "unmapping page at handle %d error %d",
  656. node->handles[i], unmap[i].status);
  657. err = unmap[i].status;
  658. break;
  659. }
  660. }
  661. if (!leaked)
  662. free_vm_area(node->pv.area);
  663. else
  664. pr_alert("leaking VM area %p size %u page(s)",
  665. node->pv.area, node->nr_handles);
  666. kfree(node);
  667. return err;
  668. }
  669. static const struct xenbus_ring_ops ring_ops_pv = {
  670. .map = xenbus_map_ring_valloc_pv,
  671. .unmap = xenbus_unmap_ring_vfree_pv,
  672. };
  673. #endif
  674. struct unmap_ring_vfree_hvm
  675. {
  676. unsigned int idx;
  677. unsigned long addrs[XENBUS_MAX_RING_GRANTS];
  678. };
  679. static void xenbus_unmap_ring_setup_grant_hvm(unsigned long gfn,
  680. unsigned int goffset,
  681. unsigned int len,
  682. void *data)
  683. {
  684. struct unmap_ring_vfree_hvm *info = data;
  685. info->addrs[info->idx] = (unsigned long)gfn_to_virt(gfn);
  686. info->idx++;
  687. }
  688. static int xenbus_unmap_ring_vfree_hvm(struct xenbus_device *dev, void *vaddr)
  689. {
  690. int rv;
  691. struct xenbus_map_node *node;
  692. void *addr;
  693. struct unmap_ring_vfree_hvm info = {
  694. .idx = 0,
  695. };
  696. unsigned int nr_pages;
  697. spin_lock(&xenbus_valloc_lock);
  698. list_for_each_entry(node, &xenbus_valloc_pages, next) {
  699. addr = node->hvm.addr;
  700. if (addr == vaddr) {
  701. list_del(&node->next);
  702. goto found;
  703. }
  704. }
  705. node = addr = NULL;
  706. found:
  707. spin_unlock(&xenbus_valloc_lock);
  708. if (!node) {
  709. xenbus_dev_error(dev, -ENOENT,
  710. "can't find mapped virtual address %p", vaddr);
  711. return GNTST_bad_virt_addr;
  712. }
  713. nr_pages = XENBUS_PAGES(node->nr_handles);
  714. gnttab_foreach_grant(node->hvm.pages, node->nr_handles,
  715. xenbus_unmap_ring_setup_grant_hvm,
  716. &info);
  717. rv = xenbus_unmap_ring(dev, node->handles, node->nr_handles,
  718. info.addrs);
  719. if (!rv) {
  720. vunmap(vaddr);
  721. free_xenballooned_pages(nr_pages, node->hvm.pages);
  722. }
  723. else
  724. WARN(1, "Leaking %p, size %u page(s)\n", vaddr, nr_pages);
  725. kfree(node);
  726. return rv;
  727. }
  728. /**
  729. * xenbus_unmap_ring
  730. * @dev: xenbus device
  731. * @handles: grant handle array
  732. * @nr_handles: number of handles in the array
  733. * @vaddrs: addresses to unmap
  734. *
  735. * Unmap memory in this domain that was imported from another domain.
  736. * Returns 0 on success and returns GNTST_* on error
  737. * (see xen/include/interface/grant_table.h).
  738. */
  739. int xenbus_unmap_ring(struct xenbus_device *dev,
  740. grant_handle_t *handles, unsigned int nr_handles,
  741. unsigned long *vaddrs)
  742. {
  743. struct gnttab_unmap_grant_ref unmap[XENBUS_MAX_RING_GRANTS];
  744. int i;
  745. int err;
  746. if (nr_handles > XENBUS_MAX_RING_GRANTS)
  747. return -EINVAL;
  748. for (i = 0; i < nr_handles; i++)
  749. gnttab_set_unmap_op(&unmap[i], vaddrs[i],
  750. GNTMAP_host_map, handles[i]);
  751. if (HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, unmap, i))
  752. BUG();
  753. err = GNTST_okay;
  754. for (i = 0; i < nr_handles; i++) {
  755. if (unmap[i].status != GNTST_okay) {
  756. xenbus_dev_error(dev, unmap[i].status,
  757. "unmapping page at handle %d error %d",
  758. handles[i], unmap[i].status);
  759. err = unmap[i].status;
  760. break;
  761. }
  762. }
  763. return err;
  764. }
  765. EXPORT_SYMBOL_GPL(xenbus_unmap_ring);
  766. /**
  767. * xenbus_read_driver_state
  768. * @path: path for driver
  769. *
  770. * Return the state of the driver rooted at the given store path, or
  771. * XenbusStateUnknown if no state can be read.
  772. */
  773. enum xenbus_state xenbus_read_driver_state(const char *path)
  774. {
  775. enum xenbus_state result;
  776. int err = xenbus_gather(XBT_NIL, path, "state", "%d", &result, NULL);
  777. if (err)
  778. result = XenbusStateUnknown;
  779. return result;
  780. }
  781. EXPORT_SYMBOL_GPL(xenbus_read_driver_state);
  782. static const struct xenbus_ring_ops ring_ops_hvm = {
  783. .map = xenbus_map_ring_valloc_hvm,
  784. .unmap = xenbus_unmap_ring_vfree_hvm,
  785. };
  786. void __init xenbus_ring_ops_init(void)
  787. {
  788. #ifdef CONFIG_XEN_PV
  789. if (!xen_feature(XENFEAT_auto_translated_physmap))
  790. ring_ops = &ring_ops_pv;
  791. else
  792. #endif
  793. ring_ops = &ring_ops_hvm;
  794. }