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