1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * linux/fs/9p/trans_xen 4 * 5 * Xen transport layer. 6 * 7 * Copyright (C) 2017 by Stefano Stabellini <stefano@aporeto.com> 8 */ 9 10 #include <xen/events.h> 11 #include <xen/grant_table.h> 12 #include <xen/xen.h> 13 #include <xen/xenbus.h> 14 #include <xen/interface/io/9pfs.h> 15 16 #include <linux/module.h> 17 #include <linux/spinlock.h> 18 #include <net/9p/9p.h> 19 #include <net/9p/client.h> 20 #include <net/9p/transport.h> 21 22 #define XEN_9PFS_NUM_RINGS 2 23 #define XEN_9PFS_RING_ORDER 9 24 #define XEN_9PFS_RING_SIZE(ring) XEN_FLEX_RING_SIZE(ring->intf->ring_order) 25 26 struct xen_9pfs_header { 27 uint32_t size; 28 uint8_t id; 29 uint16_t tag; 30 31 /* uint8_t sdata[]; */ 32 } __attribute__((packed)); 33 34 /* One per ring, more than one per 9pfs share */ 35 struct xen_9pfs_dataring { 36 struct xen_9pfs_front_priv *priv; 37 38 struct xen_9pfs_data_intf *intf; 39 grant_ref_t ref; 40 int evtchn; 41 int irq; 42 /* protect a ring from concurrent accesses */ 43 spinlock_t lock; 44 45 struct xen_9pfs_data data; 46 wait_queue_head_t wq; 47 struct work_struct work; 48 }; 49 50 /* One per 9pfs share */ 51 struct xen_9pfs_front_priv { 52 struct list_head list; 53 struct xenbus_device *dev; 54 char *tag; 55 struct p9_client *client; 56 57 int num_rings; 58 struct xen_9pfs_dataring *rings; 59 }; 60 61 static LIST_HEAD(xen_9pfs_devs); 62 static DEFINE_RWLOCK(xen_9pfs_lock); 63 64 /* We don't currently allow canceling of requests */ 65 static int p9_xen_cancel(struct p9_client *client, struct p9_req_t *req) 66 { 67 return 1; 68 } 69 70 static int p9_xen_create(struct p9_client *client, const char *addr, char *args) 71 { 72 struct xen_9pfs_front_priv *priv; 73 74 if (addr == NULL) 75 return -EINVAL; 76 77 read_lock(&xen_9pfs_lock); 78 list_for_each_entry(priv, &xen_9pfs_devs, list) { 79 if (!strcmp(priv->tag, addr)) { 80 priv->client = client; 81 read_unlock(&xen_9pfs_lock); 82 return 0; 83 } 84 } 85 read_unlock(&xen_9pfs_lock); 86 return -EINVAL; 87 } 88 89 static void p9_xen_close(struct p9_client *client) 90 { 91 struct xen_9pfs_front_priv *priv; 92 93 read_lock(&xen_9pfs_lock); 94 list_for_each_entry(priv, &xen_9pfs_devs, list) { 95 if (priv->client == client) { 96 priv->client = NULL; 97 read_unlock(&xen_9pfs_lock); 98 return; 99 } 100 } 101 read_unlock(&xen_9pfs_lock); 102 } 103 104 static bool p9_xen_write_todo(struct xen_9pfs_dataring *ring, RING_IDX size) 105 { 106 RING_IDX cons, prod; 107 108 cons = ring->intf->out_cons; 109 prod = ring->intf->out_prod; 110 virt_mb(); 111 112 return XEN_9PFS_RING_SIZE(ring) - 113 xen_9pfs_queued(prod, cons, XEN_9PFS_RING_SIZE(ring)) >= size; 114 } 115 116 static int p9_xen_request(struct p9_client *client, struct p9_req_t *p9_req) 117 { 118 struct xen_9pfs_front_priv *priv; 119 RING_IDX cons, prod, masked_cons, masked_prod; 120 unsigned long flags; 121 u32 size = p9_req->tc.size; 122 struct xen_9pfs_dataring *ring; 123 int num; 124 125 read_lock(&xen_9pfs_lock); 126 list_for_each_entry(priv, &xen_9pfs_devs, list) { 127 if (priv->client == client) 128 break; 129 } 130 read_unlock(&xen_9pfs_lock); 131 if (list_entry_is_head(priv, &xen_9pfs_devs, list)) 132 return -EINVAL; 133 134 num = p9_req->tc.tag % priv->num_rings; 135 ring = &priv->rings[num]; 136 137 again: 138 while (wait_event_killable(ring->wq, 139 p9_xen_write_todo(ring, size)) != 0) 140 ; 141 142 spin_lock_irqsave(&ring->lock, flags); 143 cons = ring->intf->out_cons; 144 prod = ring->intf->out_prod; 145 virt_mb(); 146 147 if (XEN_9PFS_RING_SIZE(ring) - 148 xen_9pfs_queued(prod, cons, XEN_9PFS_RING_SIZE(ring)) < size) { 149 spin_unlock_irqrestore(&ring->lock, flags); 150 goto again; 151 } 152 153 masked_prod = xen_9pfs_mask(prod, XEN_9PFS_RING_SIZE(ring)); 154 masked_cons = xen_9pfs_mask(cons, XEN_9PFS_RING_SIZE(ring)); 155 156 xen_9pfs_write_packet(ring->data.out, p9_req->tc.sdata, size, 157 &masked_prod, masked_cons, 158 XEN_9PFS_RING_SIZE(ring)); 159 160 p9_req->status = REQ_STATUS_SENT; 161 virt_wmb(); /* write ring before updating pointer */ 162 prod += size; 163 ring->intf->out_prod = prod; 164 spin_unlock_irqrestore(&ring->lock, flags); 165 notify_remote_via_irq(ring->irq); 166 p9_req_put(client, p9_req); 167 168 return 0; 169 } 170 171 static void p9_xen_response(struct work_struct *work) 172 { 173 struct xen_9pfs_front_priv *priv; 174 struct xen_9pfs_dataring *ring; 175 RING_IDX cons, prod, masked_cons, masked_prod; 176 struct xen_9pfs_header h; 177 struct p9_req_t *req; 178 int status; 179 180 ring = container_of(work, struct xen_9pfs_dataring, work); 181 priv = ring->priv; 182 183 while (1) { 184 cons = ring->intf->in_cons; 185 prod = ring->intf->in_prod; 186 virt_rmb(); 187 188 if (xen_9pfs_queued(prod, cons, XEN_9PFS_RING_SIZE(ring)) < 189 sizeof(h)) { 190 notify_remote_via_irq(ring->irq); 191 return; 192 } 193 194 masked_prod = xen_9pfs_mask(prod, XEN_9PFS_RING_SIZE(ring)); 195 masked_cons = xen_9pfs_mask(cons, XEN_9PFS_RING_SIZE(ring)); 196 197 /* First, read just the header */ 198 xen_9pfs_read_packet(&h, ring->data.in, sizeof(h), 199 masked_prod, &masked_cons, 200 XEN_9PFS_RING_SIZE(ring)); 201 202 req = p9_tag_lookup(priv->client, h.tag); 203 if (!req || req->status != REQ_STATUS_SENT) { 204 dev_warn(&priv->dev->dev, "Wrong req tag=%x\n", h.tag); 205 cons += h.size; 206 virt_mb(); 207 ring->intf->in_cons = cons; 208 continue; 209 } 210 211 if (h.size > req->rc.capacity) { 212 dev_warn(&priv->dev->dev, 213 "requested packet size too big: %d for tag %d with capacity %zd\n", 214 h.size, h.tag, req->rc.capacity); 215 req->status = REQ_STATUS_ERROR; 216 goto recv_error; 217 } 218 219 memcpy(&req->rc, &h, sizeof(h)); 220 req->rc.offset = 0; 221 222 masked_cons = xen_9pfs_mask(cons, XEN_9PFS_RING_SIZE(ring)); 223 /* Then, read the whole packet (including the header) */ 224 xen_9pfs_read_packet(req->rc.sdata, ring->data.in, h.size, 225 masked_prod, &masked_cons, 226 XEN_9PFS_RING_SIZE(ring)); 227 228 recv_error: 229 virt_mb(); 230 cons += h.size; 231 ring->intf->in_cons = cons; 232 233 status = (req->status != REQ_STATUS_ERROR) ? 234 REQ_STATUS_RCVD : REQ_STATUS_ERROR; 235 236 p9_client_cb(priv->client, req, status); 237 } 238 } 239 240 static irqreturn_t xen_9pfs_front_event_handler(int irq, void *r) 241 { 242 struct xen_9pfs_dataring *ring = r; 243 244 if (!ring || !ring->priv->client) { 245 /* ignore spurious interrupt */ 246 return IRQ_HANDLED; 247 } 248 249 wake_up_interruptible(&ring->wq); 250 schedule_work(&ring->work); 251 252 return IRQ_HANDLED; 253 } 254 255 static struct p9_trans_module p9_xen_trans = { 256 .name = "xen", 257 .maxsize = 1 << (XEN_9PFS_RING_ORDER + XEN_PAGE_SHIFT - 2), 258 .pooled_rbuffers = false, 259 .def = 1, 260 .create = p9_xen_create, 261 .close = p9_xen_close, 262 .request = p9_xen_request, 263 .cancel = p9_xen_cancel, 264 .owner = THIS_MODULE, 265 }; 266 267 static const struct xenbus_device_id xen_9pfs_front_ids[] = { 268 { "9pfs" }, 269 { "" } 270 }; 271 272 static void xen_9pfs_front_free(struct xen_9pfs_front_priv *priv) 273 { 274 int i, j; 275 276 write_lock(&xen_9pfs_lock); 277 list_del(&priv->list); 278 write_unlock(&xen_9pfs_lock); 279 280 for (i = 0; i < priv->num_rings; i++) { 281 if (!priv->rings[i].intf) 282 break; 283 if (priv->rings[i].irq > 0) 284 unbind_from_irqhandler(priv->rings[i].irq, priv->dev); 285 if (priv->rings[i].data.in) { 286 for (j = 0; 287 j < (1 << priv->rings[i].intf->ring_order); 288 j++) { 289 grant_ref_t ref; 290 291 ref = priv->rings[i].intf->ref[j]; 292 gnttab_end_foreign_access(ref, NULL); 293 } 294 free_pages_exact(priv->rings[i].data.in, 295 1UL << (priv->rings[i].intf->ring_order + 296 XEN_PAGE_SHIFT)); 297 } 298 gnttab_end_foreign_access(priv->rings[i].ref, NULL); 299 free_page((unsigned long)priv->rings[i].intf); 300 } 301 kfree(priv->rings); 302 kfree(priv->tag); 303 kfree(priv); 304 } 305 306 static int xen_9pfs_front_remove(struct xenbus_device *dev) 307 { 308 struct xen_9pfs_front_priv *priv = dev_get_drvdata(&dev->dev); 309 310 dev_set_drvdata(&dev->dev, NULL); 311 xen_9pfs_front_free(priv); 312 return 0; 313 } 314 315 static int xen_9pfs_front_alloc_dataring(struct xenbus_device *dev, 316 struct xen_9pfs_dataring *ring, 317 unsigned int order) 318 { 319 int i = 0; 320 int ret = -ENOMEM; 321 void *bytes = NULL; 322 323 init_waitqueue_head(&ring->wq); 324 spin_lock_init(&ring->lock); 325 INIT_WORK(&ring->work, p9_xen_response); 326 327 ring->intf = (struct xen_9pfs_data_intf *)get_zeroed_page(GFP_KERNEL); 328 if (!ring->intf) 329 return ret; 330 ret = gnttab_grant_foreign_access(dev->otherend_id, 331 virt_to_gfn(ring->intf), 0); 332 if (ret < 0) 333 goto out; 334 ring->ref = ret; 335 bytes = alloc_pages_exact(1UL << (order + XEN_PAGE_SHIFT), 336 GFP_KERNEL | __GFP_ZERO); 337 if (!bytes) { 338 ret = -ENOMEM; 339 goto out; 340 } 341 for (; i < (1 << order); i++) { 342 ret = gnttab_grant_foreign_access( 343 dev->otherend_id, virt_to_gfn(bytes) + i, 0); 344 if (ret < 0) 345 goto out; 346 ring->intf->ref[i] = ret; 347 } 348 ring->intf->ring_order = order; 349 ring->data.in = bytes; 350 ring->data.out = bytes + XEN_FLEX_RING_SIZE(order); 351 352 ret = xenbus_alloc_evtchn(dev, &ring->evtchn); 353 if (ret) 354 goto out; 355 ring->irq = bind_evtchn_to_irqhandler(ring->evtchn, 356 xen_9pfs_front_event_handler, 357 0, "xen_9pfs-frontend", ring); 358 if (ring->irq >= 0) 359 return 0; 360 361 xenbus_free_evtchn(dev, ring->evtchn); 362 ret = ring->irq; 363 out: 364 if (bytes) { 365 for (i--; i >= 0; i--) 366 gnttab_end_foreign_access(ring->intf->ref[i], NULL); 367 free_pages_exact(bytes, 1UL << (order + XEN_PAGE_SHIFT)); 368 } 369 gnttab_end_foreign_access(ring->ref, NULL); 370 free_page((unsigned long)ring->intf); 371 return ret; 372 } 373 374 static int xen_9pfs_front_probe(struct xenbus_device *dev, 375 const struct xenbus_device_id *id) 376 { 377 int ret, i; 378 struct xenbus_transaction xbt; 379 struct xen_9pfs_front_priv *priv = NULL; 380 char *versions; 381 unsigned int max_rings, max_ring_order, len = 0; 382 383 versions = xenbus_read(XBT_NIL, dev->otherend, "versions", &len); 384 if (IS_ERR(versions)) 385 return PTR_ERR(versions); 386 if (strcmp(versions, "1")) { 387 kfree(versions); 388 return -EINVAL; 389 } 390 kfree(versions); 391 max_rings = xenbus_read_unsigned(dev->otherend, "max-rings", 0); 392 if (max_rings < XEN_9PFS_NUM_RINGS) 393 return -EINVAL; 394 max_ring_order = xenbus_read_unsigned(dev->otherend, 395 "max-ring-page-order", 0); 396 if (max_ring_order > XEN_9PFS_RING_ORDER) 397 max_ring_order = XEN_9PFS_RING_ORDER; 398 if (p9_xen_trans.maxsize > XEN_FLEX_RING_SIZE(max_ring_order)) 399 p9_xen_trans.maxsize = XEN_FLEX_RING_SIZE(max_ring_order) / 2; 400 401 priv = kzalloc(sizeof(*priv), GFP_KERNEL); 402 if (!priv) 403 return -ENOMEM; 404 405 priv->dev = dev; 406 priv->num_rings = XEN_9PFS_NUM_RINGS; 407 priv->rings = kcalloc(priv->num_rings, sizeof(*priv->rings), 408 GFP_KERNEL); 409 if (!priv->rings) { 410 kfree(priv); 411 return -ENOMEM; 412 } 413 414 for (i = 0; i < priv->num_rings; i++) { 415 priv->rings[i].priv = priv; 416 ret = xen_9pfs_front_alloc_dataring(dev, &priv->rings[i], 417 max_ring_order); 418 if (ret < 0) 419 goto error; 420 } 421 422 again: 423 ret = xenbus_transaction_start(&xbt); 424 if (ret) { 425 xenbus_dev_fatal(dev, ret, "starting transaction"); 426 goto error; 427 } 428 ret = xenbus_printf(xbt, dev->nodename, "version", "%u", 1); 429 if (ret) 430 goto error_xenbus; 431 ret = xenbus_printf(xbt, dev->nodename, "num-rings", "%u", 432 priv->num_rings); 433 if (ret) 434 goto error_xenbus; 435 for (i = 0; i < priv->num_rings; i++) { 436 char str[16]; 437 438 BUILD_BUG_ON(XEN_9PFS_NUM_RINGS > 9); 439 sprintf(str, "ring-ref%d", i); 440 ret = xenbus_printf(xbt, dev->nodename, str, "%d", 441 priv->rings[i].ref); 442 if (ret) 443 goto error_xenbus; 444 445 sprintf(str, "event-channel-%d", i); 446 ret = xenbus_printf(xbt, dev->nodename, str, "%u", 447 priv->rings[i].evtchn); 448 if (ret) 449 goto error_xenbus; 450 } 451 priv->tag = xenbus_read(xbt, dev->nodename, "tag", NULL); 452 if (IS_ERR(priv->tag)) { 453 ret = PTR_ERR(priv->tag); 454 goto error_xenbus; 455 } 456 ret = xenbus_transaction_end(xbt, 0); 457 if (ret) { 458 if (ret == -EAGAIN) 459 goto again; 460 xenbus_dev_fatal(dev, ret, "completing transaction"); 461 goto error; 462 } 463 464 write_lock(&xen_9pfs_lock); 465 list_add_tail(&priv->list, &xen_9pfs_devs); 466 write_unlock(&xen_9pfs_lock); 467 dev_set_drvdata(&dev->dev, priv); 468 xenbus_switch_state(dev, XenbusStateInitialised); 469 470 return 0; 471 472 error_xenbus: 473 xenbus_transaction_end(xbt, 1); 474 xenbus_dev_fatal(dev, ret, "writing xenstore"); 475 error: 476 dev_set_drvdata(&dev->dev, NULL); 477 xen_9pfs_front_free(priv); 478 return ret; 479 } 480 481 static int xen_9pfs_front_resume(struct xenbus_device *dev) 482 { 483 dev_warn(&dev->dev, "suspend/resume unsupported\n"); 484 return 0; 485 } 486 487 static void xen_9pfs_front_changed(struct xenbus_device *dev, 488 enum xenbus_state backend_state) 489 { 490 switch (backend_state) { 491 case XenbusStateReconfiguring: 492 case XenbusStateReconfigured: 493 case XenbusStateInitialising: 494 case XenbusStateInitialised: 495 case XenbusStateUnknown: 496 break; 497 498 case XenbusStateInitWait: 499 break; 500 501 case XenbusStateConnected: 502 xenbus_switch_state(dev, XenbusStateConnected); 503 break; 504 505 case XenbusStateClosed: 506 if (dev->state == XenbusStateClosed) 507 break; 508 fallthrough; /* Missed the backend's CLOSING state */ 509 case XenbusStateClosing: 510 xenbus_frontend_closed(dev); 511 break; 512 } 513 } 514 515 static struct xenbus_driver xen_9pfs_front_driver = { 516 .ids = xen_9pfs_front_ids, 517 .probe = xen_9pfs_front_probe, 518 .remove = xen_9pfs_front_remove, 519 .resume = xen_9pfs_front_resume, 520 .otherend_changed = xen_9pfs_front_changed, 521 }; 522 523 static int __init p9_trans_xen_init(void) 524 { 525 int rc; 526 527 if (!xen_domain()) 528 return -ENODEV; 529 530 pr_info("Initialising Xen transport for 9pfs\n"); 531 532 v9fs_register_trans(&p9_xen_trans); 533 rc = xenbus_register_frontend(&xen_9pfs_front_driver); 534 if (rc) 535 v9fs_unregister_trans(&p9_xen_trans); 536 537 return rc; 538 } 539 module_init(p9_trans_xen_init); 540 MODULE_ALIAS_9P("xen"); 541 542 static void __exit p9_trans_xen_exit(void) 543 { 544 v9fs_unregister_trans(&p9_xen_trans); 545 return xenbus_unregister_driver(&xen_9pfs_front_driver); 546 } 547 module_exit(p9_trans_xen_exit); 548 549 MODULE_ALIAS("xen:9pfs"); 550 MODULE_AUTHOR("Stefano Stabellini <stefano@aporeto.com>"); 551 MODULE_DESCRIPTION("Xen Transport for 9P"); 552 MODULE_LICENSE("GPL"); 553