| 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 |
|
| 33 |
#include <linux/mm.h> |
| 34 |
#include <linux/slab.h> |
| 35 |
#include <linux/types.h> |
| 36 |
#include <linux/spinlock.h> |
| 37 |
#include <linux/vmalloc.h> |
| 38 |
#include <linux/export.h> |
| 39 |
#include <asm/xen/hypervisor.h> |
| 40 |
#include <xen/page.h> |
| 41 |
#include <xen/interface/xen.h> |
| 42 |
#include <xen/interface/event_channel.h> |
| 43 |
#include <xen/balloon.h> |
| 44 |
#include <xen/events.h> |
| 45 |
#include <xen/grant_table.h> |
| 46 |
#include <xen/xenbus.h> |
| 47 |
#include <xen/xen.h> |
| 48 |
#include <xen/features.h> |
| 49 |
|
| 50 |
#include "xenbus.h" |
| 51 |
|
| 52 |
#define XENBUS_PAGES(_grants) (DIV_ROUND_UP(_grants, XEN_PFN_PER_PAGE)) |
| 53 |
|
| 54 |
#define XENBUS_MAX_RING_PAGES (XENBUS_PAGES(XENBUS_MAX_RING_GRANTS)) |
| 55 |
|
| 56 |
struct xenbus_map_node { |
| 57 |
struct list_head next; |
| 58 |
union { |
| 59 |
struct { |
| 60 |
struct vm_struct *area; |
| 61 |
} pv; |
| 62 |
struct { |
| 63 |
struct page *pages[XENBUS_MAX_RING_PAGES]; |
| 64 |
unsigned long addrs[XENBUS_MAX_RING_GRANTS]; |
| 65 |
void *addr; |
| 66 |
} hvm; |
| 67 |
}; |
| 68 |
grant_handle_t handles[XENBUS_MAX_RING_GRANTS]; |
| 69 |
unsigned int nr_handles; |
| 70 |
}; |
| 71 |
|
| 72 |
struct map_ring_valloc { |
| 73 |
struct xenbus_map_node *node; |
| 74 |
|
| 75 |
/* Why do we need two arrays? See comment of __xenbus_map_ring */ |
| 76 |
unsigned long addrs[XENBUS_MAX_RING_GRANTS]; |
| 77 |
phys_addr_t phys_addrs[XENBUS_MAX_RING_GRANTS]; |
| 78 |
|
| 79 |
struct gnttab_map_grant_ref map[XENBUS_MAX_RING_GRANTS]; |
| 80 |
struct gnttab_unmap_grant_ref unmap[XENBUS_MAX_RING_GRANTS]; |
| 81 |
|
| 82 |
unsigned int idx; |
| 83 |
}; |
| 84 |
|
| 85 |
static DEFINE_SPINLOCK(xenbus_valloc_lock); |
| 86 |
static LIST_HEAD(xenbus_valloc_pages); |
| 87 |
|
| 88 |
struct xenbus_ring_ops { |
| 89 |
int (*map)(struct xenbus_device *dev, struct map_ring_valloc *info, |
| 90 |
grant_ref_t *gnt_refs, unsigned int nr_grefs, |
| 91 |
void **vaddr); |
| 92 |
int (*unmap)(struct xenbus_device *dev, void *vaddr); |
| 93 |
}; |
| 94 |
|
| 95 |
static const struct xenbus_ring_ops *ring_ops __read_mostly; |
| 96 |
|
| 97 |
const char *xenbus_strstate(enum xenbus_state state) |
| 98 |
{ |
| 99 |
static const char *const name[] = { |
| 100 |
[ XenbusStateUnknown ] = "Unknown", |
| 101 |
[ XenbusStateInitialising ] = "Initialising", |
| 102 |
[ XenbusStateInitWait ] = "InitWait", |
| 103 |
[ XenbusStateInitialised ] = "Initialised", |
| 104 |
[ XenbusStateConnected ] = "Connected", |
| 105 |
[ XenbusStateClosing ] = "Closing", |
| 106 |
[ XenbusStateClosed ] = "Closed", |
| 107 |
[XenbusStateReconfiguring] = "Reconfiguring", |
| 108 |
[XenbusStateReconfigured] = "Reconfigured", |
| 109 |
}; |
| 110 |
return (state < ARRAY_SIZE(name)) ? name[state] : "INVALID"; |
| 111 |
} |
| 112 |
EXPORT_SYMBOL_GPL(xenbus_strstate); |
| 113 |
|
| 114 |
/** |
| 115 |
* xenbus_watch_path - register a watch |
| 116 |
* @dev: xenbus device |
| 117 |
* @path: path to watch |
| 118 |
* @watch: watch to register |
| 119 |
* @will_handle: events queuing determine callback |
| 120 |
* @callback: callback to register |
| 121 |
* |
| 122 |
* Register a @watch on the given path, using the given xenbus_watch structure |
| 123 |
* for storage, @will_handle function as the callback to determine if each |
| 124 |
* event need to be queued, and the given @callback function as the callback. |
| 125 |
* On success, the given @path will be saved as @watch->node, and remains the |
| 126 |
* caller's to free. On error, @watch->node will be NULL, the device will |
| 127 |
* switch to %XenbusStateClosing, and the error will be saved in the store. |
| 128 |
* |
| 129 |
* Returns: %0 on success or -errno on error |
| 130 |
*/ |
| 131 |
int xenbus_watch_path(struct xenbus_device *dev, const char *path, |
| 132 |
struct xenbus_watch *watch, |
| 133 |
bool (*will_handle)(struct xenbus_watch *, |
| 134 |
const char *, const char *), |
| 135 |
void (*callback)(struct xenbus_watch *, |
| 136 |
const char *, const char *)) |
| 137 |
{ |
| 138 |
int err; |
| 139 |
|
| 140 |
watch->node = path; |
| 141 |
watch->will_handle = will_handle; |
| 142 |
watch->callback = callback; |
| 143 |
|
| 144 |
err = register_xenbus_watch(watch); |
| 145 |
|
| 146 |
if (err) { |
| 147 |
watch->node = NULL; |
| 148 |
watch->will_handle = NULL; |
| 149 |
watch->callback = NULL; |
| 150 |
xenbus_dev_fatal(dev, err, "adding watch on %s", path); |
| 151 |
} |
| 152 |
|
| 153 |
return err; |
| 154 |
} |
| 155 |
EXPORT_SYMBOL_GPL(xenbus_watch_path); |
| 156 |
|
| 157 |
|
| 158 |
/** |
| 159 |
* xenbus_watch_pathfmt - register a watch on a sprintf-formatted path |
| 160 |
* @dev: xenbus device |
| 161 |
* @watch: watch to register |
| 162 |
* @will_handle: events queuing determine callback |
| 163 |
* @callback: callback to register |
| 164 |
* @pathfmt: format of path to watch |
| 165 |
* |
| 166 |
* Register a watch on the given @path, using the given xenbus_watch |
| 167 |
* structure for storage, @will_handle function as the callback to determine if |
| 168 |
* each event need to be queued, and the given @callback function as the |
| 169 |
* callback. On success, the watched path (@path/@path2) will be saved |
| 170 |
* as @watch->node, and becomes the caller's to kfree(). |
| 171 |
* On error, watch->node will be NULL, so the caller has nothing to |
| 172 |
* free, the device will switch to %XenbusStateClosing, and the error will be |
| 173 |
* saved in the store. |
| 174 |
* |
| 175 |
* Returns: %0 on success or -errno on error |
| 176 |
*/ |
| 177 |
int xenbus_watch_pathfmt(struct xenbus_device *dev, |
| 178 |
struct xenbus_watch *watch, |
| 179 |
bool (*will_handle)(struct xenbus_watch *, |
| 180 |
const char *, const char *), |
| 181 |
void (*callback)(struct xenbus_watch *, |
| 182 |
const char *, const char *), |
| 183 |
const char *pathfmt, ...) |
| 184 |
{ |
| 185 |
int err; |
| 186 |
va_list ap; |
| 187 |
char *path; |
| 188 |
|
| 189 |
va_start(ap, pathfmt); |
| 190 |
path = kvasprintf(GFP_NOIO | __GFP_HIGH, pathfmt, ap); |
| 191 |
va_end(ap); |
| 192 |
|
| 193 |
if (!path) { |
| 194 |
xenbus_dev_fatal(dev, -ENOMEM, "allocating path for watch"); |
| 195 |
return -ENOMEM; |
| 196 |
} |
| 197 |
err = xenbus_watch_path(dev, path, watch, will_handle, callback); |
| 198 |
|
| 199 |
if (err) |
| 200 |
kfree(path); |
| 201 |
return err; |
| 202 |
} |
| 203 |
EXPORT_SYMBOL_GPL(xenbus_watch_pathfmt); |
| 204 |
|
| 205 |
__printf(4, 5) |
| 206 |
static void xenbus_switch_fatal(struct xenbus_device *, int, int, |
| 207 |
const char *, ...); |
| 208 |
|
| 209 |
static int |
| 210 |
__xenbus_switch_state(struct xenbus_device *dev, |
| 211 |
enum xenbus_state state, int depth) |
| 212 |
{ |
| 213 |
/* We check whether the state is currently set to the given value, and |
| 214 |
if not, then the state is set. We don't want to unconditionally |
| 215 |
write the given state, because we don't want to fire watches |
| 216 |
unnecessarily. Furthermore, if the node has gone, we don't write |
| 217 |
to it, as the device will be tearing down, and we don't want to |
| 218 |
resurrect that directory. |
| 219 |
|
| 220 |
Note that, because of this cached value of our state, this |
| 221 |
function will not take a caller's Xenstore transaction |
| 222 |
(something it was trying to in the past) because dev->state |
| 223 |
would not get reset if the transaction was aborted. |
| 224 |
*/ |
| 225 |
|
| 226 |
struct xenbus_transaction xbt; |
| 227 |
int current_state; |
| 228 |
int err, abort; |
| 229 |
bool vanished = false; |
| 230 |
|
| 231 |
if (state == dev->state || dev->vanished) |
| 232 |
return 0; |
| 233 |
|
| 234 |
again: |
| 235 |
abort = 1; |
| 236 |
|
| 237 |
err = xenbus_transaction_start(&xbt); |
| 238 |
if (err) { |
| 239 |
xenbus_switch_fatal(dev, depth, err, "starting transaction"); |
| 240 |
return 0; |
| 241 |
} |
| 242 |
|
| 243 |
err = xenbus_scanf(xbt, dev->nodename, "state", "%d", ¤t_state); |
| 244 |
if (err != 1) |
| 245 |
goto abort; |
| 246 |
if (current_state != dev->state && current_state == XenbusStateInitialising) { |
| 247 |
vanished = true; |
| 248 |
goto abort; |
| 249 |
} |
| 250 |
|
| 251 |
err = xenbus_printf(xbt, dev->nodename, "state", "%d", state); |
| 252 |
if (err) { |
| 253 |
xenbus_switch_fatal(dev, depth, err, "writing new state"); |
| 254 |
goto abort; |
| 255 |
} |
| 256 |
|
| 257 |
abort = 0; |
| 258 |
abort: |
| 259 |
err = xenbus_transaction_end(xbt, abort); |
| 260 |
if (err) { |
| 261 |
if (err == -EAGAIN && !abort) |
| 262 |
goto again; |
| 263 |
xenbus_switch_fatal(dev, depth, err, "ending transaction"); |
| 264 |
} else if (!vanished) |
| 265 |
dev->state = state; |
| 266 |
|
| 267 |
return 0; |
| 268 |
} |
| 269 |
|
| 270 |
/** |
| 271 |
* xenbus_switch_state - save the new state of a driver |
| 272 |
* @dev: xenbus device |
| 273 |
* @state: new state |
| 274 |
* |
| 275 |
* Advertise in the store a change of the given driver to the given new_state. |
| 276 |
* On error, the device will switch to XenbusStateClosing, and the error |
| 277 |
* will be saved in the store. |
| 278 |
* |
| 279 |
* Returns: %0 on success or -errno on error |
| 280 |
*/ |
| 281 |
int xenbus_switch_state(struct xenbus_device *dev, enum xenbus_state state) |
| 282 |
{ |
| 283 |
return __xenbus_switch_state(dev, state, 0); |
| 284 |
} |
| 285 |
|
| 286 |
EXPORT_SYMBOL_GPL(xenbus_switch_state); |
| 287 |
|
| 288 |
int xenbus_frontend_closed(struct xenbus_device *dev) |
| 289 |
{ |
| 290 |
xenbus_switch_state(dev, XenbusStateClosed); |
| 291 |
complete(&dev->down); |
| 292 |
return 0; |
| 293 |
} |
| 294 |
EXPORT_SYMBOL_GPL(xenbus_frontend_closed); |
| 295 |
|
| 296 |
__printf(3, 0) |
| 297 |
static void xenbus_va_dev_error(struct xenbus_device *dev, int err, |
| 298 |
const char *fmt, va_list ap) |
| 299 |
{ |
| 300 |
unsigned int len; |
| 301 |
char *printf_buffer; |
| 302 |
char *path_buffer; |
| 303 |
|
| 304 |
#define PRINTF_BUFFER_SIZE 4096 |
| 305 |
|
| 306 |
printf_buffer = kmalloc(PRINTF_BUFFER_SIZE, GFP_KERNEL); |
| 307 |
if (!printf_buffer) |
| 308 |
return; |
| 309 |
|
| 310 |
len = sprintf(printf_buffer, "%i ", -err); |
| 311 |
vsnprintf(printf_buffer + len, PRINTF_BUFFER_SIZE - len, fmt, ap); |
| 312 |
|
| 313 |
dev_err(&dev->dev, "%s\n", printf_buffer); |
| 314 |
|
| 315 |
path_buffer = kasprintf(GFP_KERNEL, "error/%s", dev->nodename); |
| 316 |
if (path_buffer) |
| 317 |
xenbus_write(XBT_NIL, path_buffer, "error", printf_buffer); |
| 318 |
|
| 319 |
kfree(printf_buffer); |
| 320 |
kfree(path_buffer); |
| 321 |
} |
| 322 |
|
| 323 |
/** |
| 324 |
* xenbus_dev_error - place an error message into the store |
| 325 |
* @dev: xenbus device |
| 326 |
* @err: error to report |
| 327 |
* @fmt: error message format |
| 328 |
* |
| 329 |
* Report the given negative errno into the store, along with the given |
| 330 |
* formatted message. |
| 331 |
*/ |
| 332 |
void xenbus_dev_error(struct xenbus_device *dev, int err, const char *fmt, ...) |
| 333 |
{ |
| 334 |
va_list ap; |
| 335 |
|
| 336 |
va_start(ap, fmt); |
| 337 |
xenbus_va_dev_error(dev, err, fmt, ap); |
| 338 |
va_end(ap); |
| 339 |
} |
| 340 |
EXPORT_SYMBOL_GPL(xenbus_dev_error); |
| 341 |
|
| 342 |
/** |
| 343 |
* xenbus_dev_fatal - put an error messages into the store and then shutdown |
| 344 |
* @dev: xenbus device |
| 345 |
* @err: error to report |
| 346 |
* @fmt: error message format |
| 347 |
* |
| 348 |
* Equivalent to xenbus_dev_error(dev, err, fmt, args), followed by |
| 349 |
* xenbus_switch_state(dev, XenbusStateClosing) to schedule an orderly |
| 350 |
* closedown of this driver and its peer. |
| 351 |
*/ |
| 352 |
|
| 353 |
void xenbus_dev_fatal(struct xenbus_device *dev, int err, const char *fmt, ...) |
| 354 |
{ |
| 355 |
va_list ap; |
| 356 |
|
| 357 |
va_start(ap, fmt); |
| 358 |
xenbus_va_dev_error(dev, err, fmt, ap); |
| 359 |
va_end(ap); |
| 360 |
|
| 361 |
xenbus_switch_state(dev, XenbusStateClosing); |
| 362 |
} |
| 363 |
EXPORT_SYMBOL_GPL(xenbus_dev_fatal); |
| 364 |
|
| 365 |
/* |
| 366 |
* Equivalent to xenbus_dev_fatal(dev, err, fmt, args), but helps |
| 367 |
* avoiding recursion within xenbus_switch_state. |
| 368 |
*/ |
| 369 |
static void xenbus_switch_fatal(struct xenbus_device *dev, int depth, int err, |
| 370 |
const char *fmt, ...) |
| 371 |
{ |
| 372 |
va_list ap; |
| 373 |
|
| 374 |
va_start(ap, fmt); |
| 375 |
xenbus_va_dev_error(dev, err, fmt, ap); |
| 376 |
va_end(ap); |
| 377 |
|
| 378 |
if (!depth) |
| 379 |
__xenbus_switch_state(dev, XenbusStateClosing, 1); |
| 380 |
} |
| 381 |
|
| 382 |
/* |
| 383 |
* xenbus_setup_ring |
| 384 |
* @dev: xenbus device |
| 385 |
* @vaddr: pointer to starting virtual address of the ring |
| 386 |
* @nr_pages: number of pages to be granted |
| 387 |
* @grefs: grant reference array to be filled in |
| 388 |
* |
| 389 |
* Allocate physically contiguous pages for a shared ring buffer and grant it |
| 390 |
* to the peer of the given device. The ring buffer is initially filled with |
| 391 |
* zeroes. The virtual address of the ring is stored at @vaddr and the |
| 392 |
* grant references are stored in the @grefs array. In case of error @vaddr |
| 393 |
* will be set to NULL and @grefs will be filled with INVALID_GRANT_REF. |
| 394 |
*/ |
| 395 |
int xenbus_setup_ring(struct xenbus_device *dev, gfp_t gfp, void **vaddr, |
| 396 |
unsigned int nr_pages, grant_ref_t *grefs) |
| 397 |
{ |
| 398 |
unsigned long ring_size = nr_pages * XEN_PAGE_SIZE; |
| 399 |
grant_ref_t gref_head; |
| 400 |
unsigned int i; |
| 401 |
void *addr; |
| 402 |
int ret; |
| 403 |
|
| 404 |
addr = *vaddr = alloc_pages_exact(ring_size, gfp | __GFP_ZERO); |
| 405 |
if (!*vaddr) { |
| 406 |
ret = -ENOMEM; |
| 407 |
goto err; |
| 408 |
} |
| 409 |
|
| 410 |
ret = gnttab_alloc_grant_references(nr_pages, &gref_head); |
| 411 |
if (ret) { |
| 412 |
xenbus_dev_fatal(dev, ret, "granting access to %u ring pages", |
| 413 |
nr_pages); |
| 414 |
goto err; |
| 415 |
} |
| 416 |
|
| 417 |
for (i = 0; i < nr_pages; i++) { |
| 418 |
unsigned long gfn; |
| 419 |
|
| 420 |
if (is_vmalloc_addr(*vaddr)) |
| 421 |
gfn = pfn_to_gfn(vmalloc_to_pfn(addr)); |
| 422 |
else |
| 423 |
gfn = virt_to_gfn(addr); |
| 424 |
|
| 425 |
grefs[i] = gnttab_claim_grant_reference(&gref_head); |
| 426 |
gnttab_grant_foreign_access_ref(grefs[i], dev->otherend_id, |
| 427 |
gfn, 0); |
| 428 |
|
| 429 |
addr += XEN_PAGE_SIZE; |
| 430 |
} |
| 431 |
|
| 432 |
return 0; |
| 433 |
|
| 434 |
err: |
| 435 |
if (*vaddr) |
| 436 |
free_pages_exact(*vaddr, ring_size); |
| 437 |
for (i = 0; i < nr_pages; i++) |
| 438 |
grefs[i] = INVALID_GRANT_REF; |
| 439 |
*vaddr = NULL; |
| 440 |
|
| 441 |
return ret; |
| 442 |
} |
| 443 |
EXPORT_SYMBOL_GPL(xenbus_setup_ring); |
| 444 |
|
| 445 |
/* |
| 446 |
* xenbus_teardown_ring |
| 447 |
* @vaddr: starting virtual address of the ring |
| 448 |
* @nr_pages: number of pages |
| 449 |
* @grefs: grant reference array |
| 450 |
* |
| 451 |
* Remove grants for the shared ring buffer and free the associated memory. |
| 452 |
* On return the grant reference array is filled with INVALID_GRANT_REF. |
| 453 |
*/ |
| 454 |
void xenbus_teardown_ring(void **vaddr, unsigned int nr_pages, |
| 455 |
grant_ref_t *grefs) |
| 456 |
{ |
| 457 |
unsigned int i; |
| 458 |
|
| 459 |
for (i = 0; i < nr_pages; i++) { |
| 460 |
if (grefs[i] != INVALID_GRANT_REF) { |
| 461 |
gnttab_end_foreign_access(grefs[i], NULL); |
| 462 |
grefs[i] = INVALID_GRANT_REF; |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
if (*vaddr) |
| 467 |
free_pages_exact(*vaddr, nr_pages * XEN_PAGE_SIZE); |
| 468 |
*vaddr = NULL; |
| 469 |
} |
| 470 |
EXPORT_SYMBOL_GPL(xenbus_teardown_ring); |
| 471 |
|
| 472 |
/* |
| 473 |
* Allocate an event channel for the given xenbus_device, assigning the newly |
| 474 |
* created local port to *port. Return 0 on success, or -errno on error. On |
| 475 |
* error, the device will switch to XenbusStateClosing, and the error will be |
| 476 |
* saved in the store. |
| 477 |
*/ |
| 478 |
int xenbus_alloc_evtchn(struct xenbus_device *dev, evtchn_port_t *port) |
| 479 |
{ |
| 480 |
struct evtchn_alloc_unbound alloc_unbound; |
| 481 |
int err; |
| 482 |
|
| 483 |
alloc_unbound.dom = DOMID_SELF; |
| 484 |
alloc_unbound.remote_dom = dev->otherend_id; |
| 485 |
|
| 486 |
err = HYPERVISOR_event_channel_op(EVTCHNOP_alloc_unbound, |
| 487 |
&alloc_unbound); |
| 488 |
if (err) |
| 489 |
xenbus_dev_fatal(dev, err, "allocating event channel"); |
| 490 |
else |
| 491 |
*port = alloc_unbound.port; |
| 492 |
|
| 493 |
return err; |
| 494 |
} |
| 495 |
EXPORT_SYMBOL_GPL(xenbus_alloc_evtchn); |
| 496 |
|
| 497 |
|
| 498 |
/* |
| 499 |
* Free an existing event channel. Returns 0 on success or -errno on error. |
| 500 |
*/ |
| 501 |
int xenbus_free_evtchn(struct xenbus_device *dev, evtchn_port_t port) |
| 502 |
{ |
| 503 |
struct evtchn_close close; |
| 504 |
int err; |
| 505 |
|
| 506 |
close.port = port; |
| 507 |
|
| 508 |
err = HYPERVISOR_event_channel_op(EVTCHNOP_close, &close); |
| 509 |
if (err) |
| 510 |
xenbus_dev_error(dev, err, "freeing event channel %u", port); |
| 511 |
|
| 512 |
return err; |
| 513 |
} |
| 514 |
EXPORT_SYMBOL_GPL(xenbus_free_evtchn); |
| 515 |
|
| 516 |
|
| 517 |
/** |
| 518 |
* xenbus_map_ring_valloc - allocate & map pages of VA space |
| 519 |
* @dev: xenbus device |
| 520 |
* @gnt_refs: grant reference array |
| 521 |
* @nr_grefs: number of grant references |
| 522 |
* @vaddr: pointer to address to be filled out by mapping |
| 523 |
* |
| 524 |
* Map @nr_grefs pages of memory into this domain from another |
| 525 |
* domain's grant table. xenbus_map_ring_valloc allocates @nr_grefs |
| 526 |
* pages of virtual address space, maps the pages to that address, and sets |
| 527 |
* *vaddr to that address. If an error is returned, device will switch to |
| 528 |
* XenbusStateClosing and the error message will be saved in XenStore. |
| 529 |
* |
| 530 |
* Returns: %0 on success or -errno on error |
| 531 |
*/ |
| 532 |
int xenbus_map_ring_valloc(struct xenbus_device *dev, grant_ref_t *gnt_refs, |
| 533 |
unsigned int nr_grefs, void **vaddr) |
| 534 |
{ |
| 535 |
int err; |
| 536 |
struct map_ring_valloc *info; |
| 537 |
|
| 538 |
*vaddr = NULL; |
| 539 |
|
| 540 |
if (nr_grefs > XENBUS_MAX_RING_GRANTS) |
| 541 |
return -EINVAL; |
| 542 |
|
| 543 |
info = kzalloc_obj(*info); |
| 544 |
if (!info) |
| 545 |
return -ENOMEM; |
| 546 |
|
| 547 |
info->node = kzalloc_obj(*info->node); |
| 548 |
if (!info->node) |
| 549 |
err = -ENOMEM; |
| 550 |
else |
| 551 |
err = ring_ops->map(dev, info, gnt_refs, nr_grefs, vaddr); |
| 552 |
|
| 553 |
kfree(info->node); |
| 554 |
kfree(info); |
| 555 |
return err; |
| 556 |
} |
| 557 |
EXPORT_SYMBOL_GPL(xenbus_map_ring_valloc); |
| 558 |
|
| 559 |
/* N.B. sizeof(phys_addr_t) doesn't always equal to sizeof(unsigned |
| 560 |
* long), e.g. 32-on-64. Caller is responsible for preparing the |
| 561 |
* right array to feed into this function */ |
| 562 |
static int __xenbus_map_ring(struct xenbus_device *dev, |
| 563 |
grant_ref_t *gnt_refs, |
| 564 |
unsigned int nr_grefs, |
| 565 |
grant_handle_t *handles, |
| 566 |
struct map_ring_valloc *info, |
| 567 |
unsigned int flags, |
| 568 |
bool *leaked) |
| 569 |
{ |
| 570 |
int i, j; |
| 571 |
|
| 572 |
if (nr_grefs > XENBUS_MAX_RING_GRANTS) |
| 573 |
return -EINVAL; |
| 574 |
|
| 575 |
for (i = 0; i < nr_grefs; i++) { |
| 576 |
gnttab_set_map_op(&info->map[i], info->phys_addrs[i], flags, |
| 577 |
gnt_refs[i], dev->otherend_id); |
| 578 |
handles[i] = INVALID_GRANT_HANDLE; |
| 579 |
} |
| 580 |
|
| 581 |
gnttab_batch_map(info->map, i); |
| 582 |
|
| 583 |
for (i = 0; i < nr_grefs; i++) { |
| 584 |
if (info->map[i].status != GNTST_okay) { |
| 585 |
xenbus_dev_fatal(dev, info->map[i].status, |
| 586 |
"mapping in shared page %d from domain %d", |
| 587 |
gnt_refs[i], dev->otherend_id); |
| 588 |
goto fail; |
| 589 |
} else |
| 590 |
handles[i] = info->map[i].handle; |
| 591 |
} |
| 592 |
|
| 593 |
return 0; |
| 594 |
|
| 595 |
fail: |
| 596 |
for (i = j = 0; i < nr_grefs; i++) { |
| 597 |
if (handles[i] != INVALID_GRANT_HANDLE) { |
| 598 |
gnttab_set_unmap_op(&info->unmap[j], |
| 599 |
info->phys_addrs[i], |
| 600 |
GNTMAP_host_map, handles[i]); |
| 601 |
j++; |
| 602 |
} |
| 603 |
} |
| 604 |
|
| 605 |
BUG_ON(HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, info->unmap, j)); |
| 606 |
|
| 607 |
*leaked = false; |
| 608 |
for (i = 0; i < j; i++) { |
| 609 |
if (info->unmap[i].status != GNTST_okay) { |
| 610 |
*leaked = true; |
| 611 |
break; |
| 612 |
} |
| 613 |
} |
| 614 |
|
| 615 |
return -ENOENT; |
| 616 |
} |
| 617 |
|
| 618 |
/** |
| 619 |
* xenbus_unmap_ring - unmap memory from another domain |
| 620 |
* @dev: xenbus device |
| 621 |
* @handles: grant handle array |
| 622 |
* @nr_handles: number of handles in the array |
| 623 |
* @vaddrs: addresses to unmap |
| 624 |
* |
| 625 |
* Unmap memory in this domain that was imported from another domain. |
| 626 |
* |
| 627 |
* Returns: %0 on success or GNTST_* on error |
| 628 |
* (see xen/include/interface/grant_table.h). |
| 629 |
*/ |
| 630 |
static int xenbus_unmap_ring(struct xenbus_device *dev, grant_handle_t *handles, |
| 631 |
unsigned int nr_handles, unsigned long *vaddrs) |
| 632 |
{ |
| 633 |
struct gnttab_unmap_grant_ref unmap[XENBUS_MAX_RING_GRANTS]; |
| 634 |
int i; |
| 635 |
int err; |
| 636 |
|
| 637 |
if (nr_handles > XENBUS_MAX_RING_GRANTS) |
| 638 |
return -EINVAL; |
| 639 |
|
| 640 |
for (i = 0; i < nr_handles; i++) |
| 641 |
gnttab_set_unmap_op(&unmap[i], vaddrs[i], |
| 642 |
GNTMAP_host_map, handles[i]); |
| 643 |
|
| 644 |
BUG_ON(HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, unmap, i)); |
| 645 |
|
| 646 |
err = GNTST_okay; |
| 647 |
for (i = 0; i < nr_handles; i++) { |
| 648 |
if (unmap[i].status != GNTST_okay) { |
| 649 |
xenbus_dev_error(dev, unmap[i].status, |
| 650 |
"unmapping page at handle %d error %d", |
| 651 |
handles[i], unmap[i].status); |
| 652 |
err = unmap[i].status; |
| 653 |
break; |
| 654 |
} |
| 655 |
} |
| 656 |
|
| 657 |
return err; |
| 658 |
} |
| 659 |
|
| 660 |
static void xenbus_map_ring_setup_grant_hvm(unsigned long gfn, |
| 661 |
unsigned int goffset, |
| 662 |
unsigned int len, |
| 663 |
void *data) |
| 664 |
{ |
| 665 |
struct map_ring_valloc *info = data; |
| 666 |
unsigned long vaddr = (unsigned long)gfn_to_virt(gfn); |
| 667 |
|
| 668 |
info->phys_addrs[info->idx] = vaddr; |
| 669 |
info->addrs[info->idx] = vaddr; |
| 670 |
|
| 671 |
info->idx++; |
| 672 |
} |
| 673 |
|
| 674 |
static int xenbus_map_ring_hvm(struct xenbus_device *dev, |
| 675 |
struct map_ring_valloc *info, |
| 676 |
grant_ref_t *gnt_ref, |
| 677 |
unsigned int nr_grefs, |
| 678 |
void **vaddr) |
| 679 |
{ |
| 680 |
struct xenbus_map_node *node = info->node; |
| 681 |
int err; |
| 682 |
void *addr; |
| 683 |
bool leaked = false; |
| 684 |
unsigned int nr_pages = XENBUS_PAGES(nr_grefs); |
| 685 |
|
| 686 |
err = xen_alloc_unpopulated_pages(nr_pages, node->hvm.pages); |
| 687 |
if (err) |
| 688 |
goto out_err; |
| 689 |
|
| 690 |
gnttab_foreach_grant(node->hvm.pages, nr_grefs, |
| 691 |
xenbus_map_ring_setup_grant_hvm, |
| 692 |
info); |
| 693 |
|
| 694 |
err = __xenbus_map_ring(dev, gnt_ref, nr_grefs, node->handles, |
| 695 |
info, GNTMAP_host_map, &leaked); |
| 696 |
node->nr_handles = nr_grefs; |
| 697 |
|
| 698 |
if (err) |
| 699 |
goto out_free_ballooned_pages; |
| 700 |
|
| 701 |
addr = vmap(node->hvm.pages, nr_pages, VM_MAP | VM_IOREMAP, |
| 702 |
PAGE_KERNEL); |
| 703 |
if (!addr) { |
| 704 |
err = -ENOMEM; |
| 705 |
goto out_xenbus_unmap_ring; |
| 706 |
} |
| 707 |
|
| 708 |
node->hvm.addr = addr; |
| 709 |
|
| 710 |
spin_lock(&xenbus_valloc_lock); |
| 711 |
list_add(&node->next, &xenbus_valloc_pages); |
| 712 |
spin_unlock(&xenbus_valloc_lock); |
| 713 |
|
| 714 |
*vaddr = addr; |
| 715 |
info->node = NULL; |
| 716 |
|
| 717 |
return 0; |
| 718 |
|
| 719 |
out_xenbus_unmap_ring: |
| 720 |
if (!leaked) |
| 721 |
xenbus_unmap_ring(dev, node->handles, nr_grefs, info->addrs); |
| 722 |
else |
| 723 |
pr_alert("leaking %p size %u page(s)", |
| 724 |
addr, nr_pages); |
| 725 |
out_free_ballooned_pages: |
| 726 |
if (!leaked) |
| 727 |
xen_free_unpopulated_pages(nr_pages, node->hvm.pages); |
| 728 |
out_err: |
| 729 |
return err; |
| 730 |
} |
| 731 |
|
| 732 |
/** |
| 733 |
* xenbus_unmap_ring_vfree - unmap a page of memory from another domain |
| 734 |
* @dev: xenbus device |
| 735 |
* @vaddr: addr to unmap |
| 736 |
* |
| 737 |
* Based on Rusty Russell's skeleton driver's unmap_page. |
| 738 |
* Unmap a page of memory in this domain that was imported from another domain. |
| 739 |
* Use xenbus_unmap_ring_vfree if you mapped in your memory with |
| 740 |
* xenbus_map_ring_valloc (it will free the virtual address space). |
| 741 |
* |
| 742 |
* Returns: %0 on success or GNTST_* on error |
| 743 |
* (see xen/include/interface/grant_table.h). |
| 744 |
*/ |
| 745 |
int xenbus_unmap_ring_vfree(struct xenbus_device *dev, void *vaddr) |
| 746 |
{ |
| 747 |
return ring_ops->unmap(dev, vaddr); |
| 748 |
} |
| 749 |
EXPORT_SYMBOL_GPL(xenbus_unmap_ring_vfree); |
| 750 |
|
| 751 |
#ifdef CONFIG_XEN_PV |
| 752 |
static int map_ring_apply(pte_t *pte, unsigned long addr, void *data) |
| 753 |
{ |
| 754 |
struct map_ring_valloc *info = data; |
| 755 |
|
| 756 |
info->phys_addrs[info->idx++] = arbitrary_virt_to_machine(pte).maddr; |
| 757 |
return 0; |
| 758 |
} |
| 759 |
|
| 760 |
static int xenbus_map_ring_pv(struct xenbus_device *dev, |
| 761 |
struct map_ring_valloc *info, |
| 762 |
grant_ref_t *gnt_refs, |
| 763 |
unsigned int nr_grefs, |
| 764 |
void **vaddr) |
| 765 |
{ |
| 766 |
struct xenbus_map_node *node = info->node; |
| 767 |
struct vm_struct *area; |
| 768 |
bool leaked = false; |
| 769 |
int err = -ENOMEM; |
| 770 |
|
| 771 |
area = get_vm_area(XEN_PAGE_SIZE * nr_grefs, VM_IOREMAP); |
| 772 |
if (!area) |
| 773 |
return -ENOMEM; |
| 774 |
if (apply_to_page_range(&init_mm, (unsigned long)area->addr, |
| 775 |
XEN_PAGE_SIZE * nr_grefs, map_ring_apply, info)) |
| 776 |
goto failed; |
| 777 |
err = __xenbus_map_ring(dev, gnt_refs, nr_grefs, node->handles, |
| 778 |
info, GNTMAP_host_map | GNTMAP_contains_pte, |
| 779 |
&leaked); |
| 780 |
if (err) |
| 781 |
goto failed; |
| 782 |
|
| 783 |
node->nr_handles = nr_grefs; |
| 784 |
node->pv.area = area; |
| 785 |
|
| 786 |
spin_lock(&xenbus_valloc_lock); |
| 787 |
list_add(&node->next, &xenbus_valloc_pages); |
| 788 |
spin_unlock(&xenbus_valloc_lock); |
| 789 |
|
| 790 |
*vaddr = area->addr; |
| 791 |
info->node = NULL; |
| 792 |
|
| 793 |
return 0; |
| 794 |
|
| 795 |
failed: |
| 796 |
if (!leaked) |
| 797 |
free_vm_area(area); |
| 798 |
else |
| 799 |
pr_alert("leaking VM area %p size %u page(s)", area, nr_grefs); |
| 800 |
|
| 801 |
return err; |
| 802 |
} |
| 803 |
|
| 804 |
static int xenbus_unmap_ring_pv(struct xenbus_device *dev, void *vaddr) |
| 805 |
{ |
| 806 |
struct xenbus_map_node *node; |
| 807 |
struct gnttab_unmap_grant_ref unmap[XENBUS_MAX_RING_GRANTS]; |
| 808 |
unsigned int level; |
| 809 |
int i; |
| 810 |
bool leaked = false; |
| 811 |
int err; |
| 812 |
|
| 813 |
spin_lock(&xenbus_valloc_lock); |
| 814 |
list_for_each_entry(node, &xenbus_valloc_pages, next) { |
| 815 |
if (node->pv.area->addr == vaddr) { |
| 816 |
list_del(&node->next); |
| 817 |
goto found; |
| 818 |
} |
| 819 |
} |
| 820 |
node = NULL; |
| 821 |
found: |
| 822 |
spin_unlock(&xenbus_valloc_lock); |
| 823 |
|
| 824 |
if (!node) { |
| 825 |
xenbus_dev_error(dev, -ENOENT, |
| 826 |
"can't find mapped virtual address %p", vaddr); |
| 827 |
return GNTST_bad_virt_addr; |
| 828 |
} |
| 829 |
|
| 830 |
for (i = 0; i < node->nr_handles; i++) { |
| 831 |
unsigned long addr; |
| 832 |
|
| 833 |
memset(&unmap[i], 0, sizeof(unmap[i])); |
| 834 |
addr = (unsigned long)vaddr + (XEN_PAGE_SIZE * i); |
| 835 |
unmap[i].host_addr = arbitrary_virt_to_machine( |
| 836 |
lookup_address(addr, &level)).maddr; |
| 837 |
unmap[i].dev_bus_addr = 0; |
| 838 |
unmap[i].handle = node->handles[i]; |
| 839 |
} |
| 840 |
|
| 841 |
BUG_ON(HYPERVISOR_grant_table_op(GNTTABOP_unmap_grant_ref, unmap, i)); |
| 842 |
|
| 843 |
err = GNTST_okay; |
| 844 |
leaked = false; |
| 845 |
for (i = 0; i < node->nr_handles; i++) { |
| 846 |
if (unmap[i].status != GNTST_okay) { |
| 847 |
leaked = true; |
| 848 |
xenbus_dev_error(dev, unmap[i].status, |
| 849 |
"unmapping page at handle %d error %d", |
| 850 |
node->handles[i], unmap[i].status); |
| 851 |
err = unmap[i].status; |
| 852 |
break; |
| 853 |
} |
| 854 |
} |
| 855 |
|
| 856 |
if (!leaked) |
| 857 |
free_vm_area(node->pv.area); |
| 858 |
else |
| 859 |
pr_alert("leaking VM area %p size %u page(s)", |
| 860 |
node->pv.area, node->nr_handles); |
| 861 |
|
| 862 |
kfree(node); |
| 863 |
return err; |
| 864 |
} |
| 865 |
|
| 866 |
static const struct xenbus_ring_ops ring_ops_pv = { |
| 867 |
.map = xenbus_map_ring_pv, |
| 868 |
.unmap = xenbus_unmap_ring_pv, |
| 869 |
}; |
| 870 |
#endif |
| 871 |
|
| 872 |
struct unmap_ring_hvm |
| 873 |
{ |
| 874 |
unsigned int idx; |
| 875 |
unsigned long addrs[XENBUS_MAX_RING_GRANTS]; |
| 876 |
}; |
| 877 |
|
| 878 |
static void xenbus_unmap_ring_setup_grant_hvm(unsigned long gfn, |
| 879 |
unsigned int goffset, |
| 880 |
unsigned int len, |
| 881 |
void *data) |
| 882 |
{ |
| 883 |
struct unmap_ring_hvm *info = data; |
| 884 |
|
| 885 |
info->addrs[info->idx] = (unsigned long)gfn_to_virt(gfn); |
| 886 |
|
| 887 |
info->idx++; |
| 888 |
} |
| 889 |
|
| 890 |
static int xenbus_unmap_ring_hvm(struct xenbus_device *dev, void *vaddr) |
| 891 |
{ |
| 892 |
int rv; |
| 893 |
struct xenbus_map_node *node; |
| 894 |
void *addr; |
| 895 |
struct unmap_ring_hvm info = { |
| 896 |
.idx = 0, |
| 897 |
}; |
| 898 |
unsigned int nr_pages; |
| 899 |
|
| 900 |
spin_lock(&xenbus_valloc_lock); |
| 901 |
list_for_each_entry(node, &xenbus_valloc_pages, next) { |
| 902 |
addr = node->hvm.addr; |
| 903 |
if (addr == vaddr) { |
| 904 |
list_del(&node->next); |
| 905 |
goto found; |
| 906 |
} |
| 907 |
} |
| 908 |
node = addr = NULL; |
| 909 |
found: |
| 910 |
spin_unlock(&xenbus_valloc_lock); |
| 911 |
|
| 912 |
if (!node) { |
| 913 |
xenbus_dev_error(dev, -ENOENT, |
| 914 |
"can't find mapped virtual address %p", vaddr); |
| 915 |
return GNTST_bad_virt_addr; |
| 916 |
} |
| 917 |
|
| 918 |
nr_pages = XENBUS_PAGES(node->nr_handles); |
| 919 |
|
| 920 |
gnttab_foreach_grant(node->hvm.pages, node->nr_handles, |
| 921 |
xenbus_unmap_ring_setup_grant_hvm, |
| 922 |
&info); |
| 923 |
|
| 924 |
rv = xenbus_unmap_ring(dev, node->handles, node->nr_handles, |
| 925 |
info.addrs); |
| 926 |
if (!rv) { |
| 927 |
vunmap(vaddr); |
| 928 |
xen_free_unpopulated_pages(nr_pages, node->hvm.pages); |
| 929 |
} |
| 930 |
else |
| 931 |
WARN(1, "Leaking %p, size %u page(s)\n", vaddr, nr_pages); |
| 932 |
|
| 933 |
kfree(node); |
| 934 |
return rv; |
| 935 |
} |
| 936 |
|
| 937 |
/** |
| 938 |
* xenbus_read_driver_state - read state from a store path |
| 939 |
* @dev: xenbus device pointer |
| 940 |
* @path: path for driver |
| 941 |
* |
| 942 |
* Returns: the state of the driver rooted at the given store path, or |
| 943 |
* XenbusStateUnknown if no state can be read. |
| 944 |
*/ |
| 945 |
enum xenbus_state xenbus_read_driver_state(const struct xenbus_device *dev, |
| 946 |
const char *path) |
| 947 |
{ |
| 948 |
enum xenbus_state result; |
| 949 |
|
| 950 |
if (dev && dev->vanished) |
| 951 |
return XenbusStateUnknown; |
| 952 |
|
| 953 |
int err = xenbus_gather(XBT_NIL, path, "state", "%d", &result, NULL); |
| 954 |
if (err) |
| 955 |
result = XenbusStateUnknown; |
| 956 |
|
| 957 |
return result; |
| 958 |
} |
| 959 |
EXPORT_SYMBOL_GPL(xenbus_read_driver_state); |
| 960 |
|
| 961 |
static const struct xenbus_ring_ops ring_ops_hvm = { |
| 962 |
.map = xenbus_map_ring_hvm, |
| 963 |
.unmap = xenbus_unmap_ring_hvm, |
| 964 |
}; |
| 965 |
|
| 966 |
void __init xenbus_ring_ops_init(void) |
| 967 |
{ |
| 968 |
#ifdef CONFIG_XEN_PV |
| 969 |
if (xen_pv_domain()) |
| 970 |
ring_ops = &ring_ops_pv; |
| 971 |
else |
| 972 |
#endif |
| 973 |
ring_ops = &ring_ops_hvm; |
| 974 |
} |