Dominique Martinet | 024b7d6 | 2021-11-02 22:12:01 +0900 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Stefano Stabellini | 868eb12 | 2017-04-05 12:03:57 -0700 | [diff] [blame] | 2 | /* |
| 3 | * linux/fs/9p/trans_xen |
| 4 | * |
| 5 | * Xen transport layer. |
| 6 | * |
| 7 | * Copyright (C) 2017 by Stefano Stabellini <stefano@aporeto.com> |
Stefano Stabellini | 868eb12 | 2017-04-05 12:03:57 -0700 | [diff] [blame] | 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> |
Stefano Stabellini | 71ebd71 | 2017-04-05 12:03:58 -0700 | [diff] [blame] | 17 | #include <linux/spinlock.h> |
Stefano Stabellini | 868eb12 | 2017-04-05 12:03:57 -0700 | [diff] [blame] | 18 | #include <net/9p/9p.h> |
| 19 | #include <net/9p/client.h> |
| 20 | #include <net/9p/transport.h> |
| 21 | |
Stefano Stabellini | 71ebd71 | 2017-04-05 12:03:58 -0700 | [diff] [blame] | 22 | #define XEN_9PFS_NUM_RINGS 2 |
Stefano Stabellini | 36f9967 | 2020-05-21 12:32:42 -0700 | [diff] [blame] | 23 | #define XEN_9PFS_RING_ORDER 9 |
| 24 | #define XEN_9PFS_RING_SIZE(ring) XEN_FLEX_RING_SIZE(ring->intf->ring_order) |
Stefano Stabellini | 71ebd71 | 2017-04-05 12:03:58 -0700 | [diff] [blame] | 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 | |
Stefano Stabellini | f023f18 | 2017-04-05 12:03:59 -0700 | [diff] [blame] | 64 | /* We don't currently allow canceling of requests */ |
Stefano Stabellini | 868eb12 | 2017-04-05 12:03:57 -0700 | [diff] [blame] | 65 | static int p9_xen_cancel(struct p9_client *client, struct p9_req_t *req) |
| 66 | { |
Stefano Stabellini | f023f18 | 2017-04-05 12:03:59 -0700 | [diff] [blame] | 67 | return 1; |
Stefano Stabellini | 868eb12 | 2017-04-05 12:03:57 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | static int p9_xen_create(struct p9_client *client, const char *addr, char *args) |
| 71 | { |
Stefano Stabellini | f023f18 | 2017-04-05 12:03:59 -0700 | [diff] [blame] | 72 | struct xen_9pfs_front_priv *priv; |
| 73 | |
Tomas Bortoli | 10aa145 | 2018-07-27 13:05:58 +0200 | [diff] [blame] | 74 | if (addr == NULL) |
| 75 | return -EINVAL; |
| 76 | |
Stefano Stabellini | f023f18 | 2017-04-05 12:03:59 -0700 | [diff] [blame] | 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; |
Stefano Stabellini | 868eb12 | 2017-04-05 12:03:57 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | static void p9_xen_close(struct p9_client *client) |
| 90 | { |
Stefano Stabellini | f023f18 | 2017-04-05 12:03:59 -0700 | [diff] [blame] | 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 | |
Stefano Stabellini | 36f9967 | 2020-05-21 12:32:42 -0700 | [diff] [blame] | 112 | return XEN_9PFS_RING_SIZE(ring) - |
| 113 | xen_9pfs_queued(prod, cons, XEN_9PFS_RING_SIZE(ring)) >= size; |
Stefano Stabellini | 868eb12 | 2017-04-05 12:03:57 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | static int p9_xen_request(struct p9_client *client, struct p9_req_t *p9_req) |
| 117 | { |
Harshvardhan Jha | 732b33d | 2021-07-27 05:37:10 +0530 | [diff] [blame] | 118 | struct xen_9pfs_front_priv *priv; |
Stefano Stabellini | f023f18 | 2017-04-05 12:03:59 -0700 | [diff] [blame] | 119 | RING_IDX cons, prod, masked_cons, masked_prod; |
| 120 | unsigned long flags; |
Dominique Martinet | 523adb6 | 2018-07-30 05:55:19 +0000 | [diff] [blame] | 121 | u32 size = p9_req->tc.size; |
Stefano Stabellini | f023f18 | 2017-04-05 12:03:59 -0700 | [diff] [blame] | 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); |
Harshvardhan Jha | 732b33d | 2021-07-27 05:37:10 +0530 | [diff] [blame] | 131 | if (list_entry_is_head(priv, &xen_9pfs_devs, list)) |
Stefano Stabellini | f023f18 | 2017-04-05 12:03:59 -0700 | [diff] [blame] | 132 | return -EINVAL; |
| 133 | |
Dominique Martinet | 523adb6 | 2018-07-30 05:55:19 +0000 | [diff] [blame] | 134 | num = p9_req->tc.tag % priv->num_rings; |
Stefano Stabellini | f023f18 | 2017-04-05 12:03:59 -0700 | [diff] [blame] | 135 | ring = &priv->rings[num]; |
| 136 | |
| 137 | again: |
Tuomas Tynkkynen | 9523fea | 2017-09-06 17:59:08 +0300 | [diff] [blame] | 138 | while (wait_event_killable(ring->wq, |
| 139 | p9_xen_write_todo(ring, size)) != 0) |
Stefano Stabellini | f023f18 | 2017-04-05 12:03:59 -0700 | [diff] [blame] | 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 | |
Stefano Stabellini | 36f9967 | 2020-05-21 12:32:42 -0700 | [diff] [blame] | 147 | if (XEN_9PFS_RING_SIZE(ring) - |
| 148 | xen_9pfs_queued(prod, cons, XEN_9PFS_RING_SIZE(ring)) < size) { |
Stefano Stabellini | f023f18 | 2017-04-05 12:03:59 -0700 | [diff] [blame] | 149 | spin_unlock_irqrestore(&ring->lock, flags); |
| 150 | goto again; |
| 151 | } |
| 152 | |
Stefano Stabellini | 36f9967 | 2020-05-21 12:32:42 -0700 | [diff] [blame] | 153 | masked_prod = xen_9pfs_mask(prod, XEN_9PFS_RING_SIZE(ring)); |
| 154 | masked_cons = xen_9pfs_mask(cons, XEN_9PFS_RING_SIZE(ring)); |
Stefano Stabellini | f023f18 | 2017-04-05 12:03:59 -0700 | [diff] [blame] | 155 | |
Dominique Martinet | 523adb6 | 2018-07-30 05:55:19 +0000 | [diff] [blame] | 156 | xen_9pfs_write_packet(ring->data.out, p9_req->tc.sdata, size, |
Stefano Stabellini | 36f9967 | 2020-05-21 12:32:42 -0700 | [diff] [blame] | 157 | &masked_prod, masked_cons, |
| 158 | XEN_9PFS_RING_SIZE(ring)); |
Stefano Stabellini | f023f18 | 2017-04-05 12:03:59 -0700 | [diff] [blame] | 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); |
Tomas Bortoli | 728356d | 2018-08-14 19:43:42 +0200 | [diff] [blame] | 166 | p9_req_put(p9_req); |
Stefano Stabellini | f023f18 | 2017-04-05 12:03:59 -0700 | [diff] [blame] | 167 | |
Stefano Stabellini | 868eb12 | 2017-04-05 12:03:57 -0700 | [diff] [blame] | 168 | return 0; |
| 169 | } |
| 170 | |
Stefano Stabellini | 71ebd71 | 2017-04-05 12:03:58 -0700 | [diff] [blame] | 171 | static void p9_xen_response(struct work_struct *work) |
| 172 | { |
Stefano Stabellini | f66c72b | 2017-04-05 12:04:00 -0700 | [diff] [blame] | 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 | |
Stefano Stabellini | 36f9967 | 2020-05-21 12:32:42 -0700 | [diff] [blame] | 188 | if (xen_9pfs_queued(prod, cons, XEN_9PFS_RING_SIZE(ring)) < |
Stefano Stabellini | f66c72b | 2017-04-05 12:04:00 -0700 | [diff] [blame] | 189 | sizeof(h)) { |
| 190 | notify_remote_via_irq(ring->irq); |
| 191 | return; |
| 192 | } |
| 193 | |
Stefano Stabellini | 36f9967 | 2020-05-21 12:32:42 -0700 | [diff] [blame] | 194 | masked_prod = xen_9pfs_mask(prod, XEN_9PFS_RING_SIZE(ring)); |
| 195 | masked_cons = xen_9pfs_mask(cons, XEN_9PFS_RING_SIZE(ring)); |
Stefano Stabellini | f66c72b | 2017-04-05 12:04:00 -0700 | [diff] [blame] | 196 | |
| 197 | /* First, read just the header */ |
| 198 | xen_9pfs_read_packet(&h, ring->data.in, sizeof(h), |
| 199 | masked_prod, &masked_cons, |
Stefano Stabellini | 36f9967 | 2020-05-21 12:32:42 -0700 | [diff] [blame] | 200 | XEN_9PFS_RING_SIZE(ring)); |
Stefano Stabellini | f66c72b | 2017-04-05 12:04:00 -0700 | [diff] [blame] | 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 | |
Dominique Martinet | 523adb6 | 2018-07-30 05:55:19 +0000 | [diff] [blame] | 211 | memcpy(&req->rc, &h, sizeof(h)); |
| 212 | req->rc.offset = 0; |
Stefano Stabellini | f66c72b | 2017-04-05 12:04:00 -0700 | [diff] [blame] | 213 | |
Stefano Stabellini | 36f9967 | 2020-05-21 12:32:42 -0700 | [diff] [blame] | 214 | masked_cons = xen_9pfs_mask(cons, XEN_9PFS_RING_SIZE(ring)); |
Stefano Stabellini | f66c72b | 2017-04-05 12:04:00 -0700 | [diff] [blame] | 215 | /* Then, read the whole packet (including the header) */ |
Dominique Martinet | 523adb6 | 2018-07-30 05:55:19 +0000 | [diff] [blame] | 216 | xen_9pfs_read_packet(req->rc.sdata, ring->data.in, h.size, |
Stefano Stabellini | f66c72b | 2017-04-05 12:04:00 -0700 | [diff] [blame] | 217 | masked_prod, &masked_cons, |
Stefano Stabellini | 36f9967 | 2020-05-21 12:32:42 -0700 | [diff] [blame] | 218 | XEN_9PFS_RING_SIZE(ring)); |
Stefano Stabellini | f66c72b | 2017-04-05 12:04:00 -0700 | [diff] [blame] | 219 | |
| 220 | virt_mb(); |
| 221 | cons += h.size; |
| 222 | ring->intf->in_cons = cons; |
| 223 | |
| 224 | status = (req->status != REQ_STATUS_ERROR) ? |
| 225 | REQ_STATUS_RCVD : REQ_STATUS_ERROR; |
| 226 | |
| 227 | p9_client_cb(priv->client, req, status); |
| 228 | } |
Stefano Stabellini | 71ebd71 | 2017-04-05 12:03:58 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | static irqreturn_t xen_9pfs_front_event_handler(int irq, void *r) |
| 232 | { |
| 233 | struct xen_9pfs_dataring *ring = r; |
| 234 | |
| 235 | if (!ring || !ring->priv->client) { |
| 236 | /* ignore spurious interrupt */ |
| 237 | return IRQ_HANDLED; |
| 238 | } |
| 239 | |
| 240 | wake_up_interruptible(&ring->wq); |
| 241 | schedule_work(&ring->work); |
| 242 | |
| 243 | return IRQ_HANDLED; |
| 244 | } |
| 245 | |
Stefano Stabellini | 868eb12 | 2017-04-05 12:03:57 -0700 | [diff] [blame] | 246 | static struct p9_trans_module p9_xen_trans = { |
| 247 | .name = "xen", |
Stefano Stabellini | 36f9967 | 2020-05-21 12:32:42 -0700 | [diff] [blame] | 248 | .maxsize = 1 << (XEN_9PFS_RING_ORDER + XEN_PAGE_SHIFT - 2), |
Stefano Stabellini | 868eb12 | 2017-04-05 12:03:57 -0700 | [diff] [blame] | 249 | .def = 1, |
| 250 | .create = p9_xen_create, |
| 251 | .close = p9_xen_close, |
| 252 | .request = p9_xen_request, |
| 253 | .cancel = p9_xen_cancel, |
| 254 | .owner = THIS_MODULE, |
| 255 | }; |
| 256 | |
| 257 | static const struct xenbus_device_id xen_9pfs_front_ids[] = { |
| 258 | { "9pfs" }, |
| 259 | { "" } |
| 260 | }; |
| 261 | |
Stefano Stabellini | 71ebd71 | 2017-04-05 12:03:58 -0700 | [diff] [blame] | 262 | static void xen_9pfs_front_free(struct xen_9pfs_front_priv *priv) |
| 263 | { |
| 264 | int i, j; |
| 265 | |
| 266 | write_lock(&xen_9pfs_lock); |
| 267 | list_del(&priv->list); |
| 268 | write_unlock(&xen_9pfs_lock); |
| 269 | |
| 270 | for (i = 0; i < priv->num_rings; i++) { |
| 271 | if (!priv->rings[i].intf) |
| 272 | break; |
| 273 | if (priv->rings[i].irq > 0) |
| 274 | unbind_from_irqhandler(priv->rings[i].irq, priv->dev); |
| 275 | if (priv->rings[i].data.in) { |
Stefano Stabellini | 36f9967 | 2020-05-21 12:32:42 -0700 | [diff] [blame] | 276 | for (j = 0; |
| 277 | j < (1 << priv->rings[i].intf->ring_order); |
| 278 | j++) { |
Stefano Stabellini | 71ebd71 | 2017-04-05 12:03:58 -0700 | [diff] [blame] | 279 | grant_ref_t ref; |
| 280 | |
| 281 | ref = priv->rings[i].intf->ref[j]; |
| 282 | gnttab_end_foreign_access(ref, 0, 0); |
| 283 | } |
| 284 | free_pages((unsigned long)priv->rings[i].data.in, |
Stefano Stabellini | 36f9967 | 2020-05-21 12:32:42 -0700 | [diff] [blame] | 285 | priv->rings[i].intf->ring_order - |
Stefano Stabellini | 71ebd71 | 2017-04-05 12:03:58 -0700 | [diff] [blame] | 286 | (PAGE_SHIFT - XEN_PAGE_SHIFT)); |
| 287 | } |
| 288 | gnttab_end_foreign_access(priv->rings[i].ref, 0, 0); |
| 289 | free_page((unsigned long)priv->rings[i].intf); |
| 290 | } |
| 291 | kfree(priv->rings); |
| 292 | kfree(priv->tag); |
| 293 | kfree(priv); |
| 294 | } |
| 295 | |
Stefano Stabellini | 868eb12 | 2017-04-05 12:03:57 -0700 | [diff] [blame] | 296 | static int xen_9pfs_front_remove(struct xenbus_device *dev) |
| 297 | { |
Stefano Stabellini | 71ebd71 | 2017-04-05 12:03:58 -0700 | [diff] [blame] | 298 | struct xen_9pfs_front_priv *priv = dev_get_drvdata(&dev->dev); |
| 299 | |
| 300 | dev_set_drvdata(&dev->dev, NULL); |
| 301 | xen_9pfs_front_free(priv); |
Stefano Stabellini | 868eb12 | 2017-04-05 12:03:57 -0700 | [diff] [blame] | 302 | return 0; |
| 303 | } |
| 304 | |
Stefano Stabellini | 71ebd71 | 2017-04-05 12:03:58 -0700 | [diff] [blame] | 305 | static int xen_9pfs_front_alloc_dataring(struct xenbus_device *dev, |
Stefano Stabellini | 36f9967 | 2020-05-21 12:32:42 -0700 | [diff] [blame] | 306 | struct xen_9pfs_dataring *ring, |
| 307 | unsigned int order) |
Stefano Stabellini | 71ebd71 | 2017-04-05 12:03:58 -0700 | [diff] [blame] | 308 | { |
| 309 | int i = 0; |
| 310 | int ret = -ENOMEM; |
| 311 | void *bytes = NULL; |
| 312 | |
| 313 | init_waitqueue_head(&ring->wq); |
| 314 | spin_lock_init(&ring->lock); |
| 315 | INIT_WORK(&ring->work, p9_xen_response); |
| 316 | |
| 317 | ring->intf = (struct xen_9pfs_data_intf *)get_zeroed_page(GFP_KERNEL); |
| 318 | if (!ring->intf) |
| 319 | return ret; |
| 320 | ret = gnttab_grant_foreign_access(dev->otherend_id, |
| 321 | virt_to_gfn(ring->intf), 0); |
| 322 | if (ret < 0) |
| 323 | goto out; |
| 324 | ring->ref = ret; |
| 325 | bytes = (void *)__get_free_pages(GFP_KERNEL | __GFP_ZERO, |
Stefano Stabellini | 36f9967 | 2020-05-21 12:32:42 -0700 | [diff] [blame] | 326 | order - (PAGE_SHIFT - XEN_PAGE_SHIFT)); |
Stefano Stabellini | 71ebd71 | 2017-04-05 12:03:58 -0700 | [diff] [blame] | 327 | if (!bytes) { |
| 328 | ret = -ENOMEM; |
| 329 | goto out; |
| 330 | } |
Stefano Stabellini | 36f9967 | 2020-05-21 12:32:42 -0700 | [diff] [blame] | 331 | for (; i < (1 << order); i++) { |
Stefano Stabellini | 71ebd71 | 2017-04-05 12:03:58 -0700 | [diff] [blame] | 332 | ret = gnttab_grant_foreign_access( |
| 333 | dev->otherend_id, virt_to_gfn(bytes) + i, 0); |
| 334 | if (ret < 0) |
| 335 | goto out; |
| 336 | ring->intf->ref[i] = ret; |
| 337 | } |
Stefano Stabellini | 36f9967 | 2020-05-21 12:32:42 -0700 | [diff] [blame] | 338 | ring->intf->ring_order = order; |
Stefano Stabellini | 71ebd71 | 2017-04-05 12:03:58 -0700 | [diff] [blame] | 339 | ring->data.in = bytes; |
Stefano Stabellini | 36f9967 | 2020-05-21 12:32:42 -0700 | [diff] [blame] | 340 | ring->data.out = bytes + XEN_FLEX_RING_SIZE(order); |
Stefano Stabellini | 71ebd71 | 2017-04-05 12:03:58 -0700 | [diff] [blame] | 341 | |
| 342 | ret = xenbus_alloc_evtchn(dev, &ring->evtchn); |
| 343 | if (ret) |
| 344 | goto out; |
| 345 | ring->irq = bind_evtchn_to_irqhandler(ring->evtchn, |
| 346 | xen_9pfs_front_event_handler, |
| 347 | 0, "xen_9pfs-frontend", ring); |
| 348 | if (ring->irq >= 0) |
| 349 | return 0; |
| 350 | |
| 351 | xenbus_free_evtchn(dev, ring->evtchn); |
| 352 | ret = ring->irq; |
| 353 | out: |
| 354 | if (bytes) { |
| 355 | for (i--; i >= 0; i--) |
| 356 | gnttab_end_foreign_access(ring->intf->ref[i], 0, 0); |
| 357 | free_pages((unsigned long)bytes, |
Stefano Stabellini | 36f9967 | 2020-05-21 12:32:42 -0700 | [diff] [blame] | 358 | ring->intf->ring_order - |
Stefano Stabellini | 71ebd71 | 2017-04-05 12:03:58 -0700 | [diff] [blame] | 359 | (PAGE_SHIFT - XEN_PAGE_SHIFT)); |
| 360 | } |
| 361 | gnttab_end_foreign_access(ring->ref, 0, 0); |
| 362 | free_page((unsigned long)ring->intf); |
| 363 | return ret; |
| 364 | } |
| 365 | |
Stefano Stabellini | 868eb12 | 2017-04-05 12:03:57 -0700 | [diff] [blame] | 366 | static int xen_9pfs_front_probe(struct xenbus_device *dev, |
| 367 | const struct xenbus_device_id *id) |
| 368 | { |
Stefano Stabellini | 71ebd71 | 2017-04-05 12:03:58 -0700 | [diff] [blame] | 369 | int ret, i; |
| 370 | struct xenbus_transaction xbt; |
| 371 | struct xen_9pfs_front_priv *priv = NULL; |
| 372 | char *versions; |
Stefano Stabellini | 31d4726 | 2017-04-13 10:57:56 -0700 | [diff] [blame] | 373 | unsigned int max_rings, max_ring_order, len = 0; |
Stefano Stabellini | 71ebd71 | 2017-04-05 12:03:58 -0700 | [diff] [blame] | 374 | |
| 375 | versions = xenbus_read(XBT_NIL, dev->otherend, "versions", &len); |
Dominique Martinet | 2f9ad0a | 2018-08-14 02:43:48 +0000 | [diff] [blame] | 376 | if (IS_ERR(versions)) |
| 377 | return PTR_ERR(versions); |
Stefano Stabellini | 71ebd71 | 2017-04-05 12:03:58 -0700 | [diff] [blame] | 378 | if (strcmp(versions, "1")) { |
| 379 | kfree(versions); |
| 380 | return -EINVAL; |
| 381 | } |
| 382 | kfree(versions); |
| 383 | max_rings = xenbus_read_unsigned(dev->otherend, "max-rings", 0); |
| 384 | if (max_rings < XEN_9PFS_NUM_RINGS) |
| 385 | return -EINVAL; |
| 386 | max_ring_order = xenbus_read_unsigned(dev->otherend, |
| 387 | "max-ring-page-order", 0); |
Stefano Stabellini | 36f9967 | 2020-05-21 12:32:42 -0700 | [diff] [blame] | 388 | if (max_ring_order > XEN_9PFS_RING_ORDER) |
| 389 | max_ring_order = XEN_9PFS_RING_ORDER; |
| 390 | if (p9_xen_trans.maxsize > XEN_FLEX_RING_SIZE(max_ring_order)) |
| 391 | p9_xen_trans.maxsize = XEN_FLEX_RING_SIZE(max_ring_order) / 2; |
Stefano Stabellini | 71ebd71 | 2017-04-05 12:03:58 -0700 | [diff] [blame] | 392 | |
| 393 | priv = kzalloc(sizeof(*priv), GFP_KERNEL); |
| 394 | if (!priv) |
| 395 | return -ENOMEM; |
| 396 | |
| 397 | priv->dev = dev; |
| 398 | priv->num_rings = XEN_9PFS_NUM_RINGS; |
| 399 | priv->rings = kcalloc(priv->num_rings, sizeof(*priv->rings), |
| 400 | GFP_KERNEL); |
| 401 | if (!priv->rings) { |
| 402 | kfree(priv); |
| 403 | return -ENOMEM; |
| 404 | } |
| 405 | |
| 406 | for (i = 0; i < priv->num_rings; i++) { |
| 407 | priv->rings[i].priv = priv; |
Stefano Stabellini | 36f9967 | 2020-05-21 12:32:42 -0700 | [diff] [blame] | 408 | ret = xen_9pfs_front_alloc_dataring(dev, &priv->rings[i], |
| 409 | max_ring_order); |
Stefano Stabellini | 71ebd71 | 2017-04-05 12:03:58 -0700 | [diff] [blame] | 410 | if (ret < 0) |
| 411 | goto error; |
| 412 | } |
| 413 | |
| 414 | again: |
| 415 | ret = xenbus_transaction_start(&xbt); |
| 416 | if (ret) { |
| 417 | xenbus_dev_fatal(dev, ret, "starting transaction"); |
| 418 | goto error; |
| 419 | } |
| 420 | ret = xenbus_printf(xbt, dev->nodename, "version", "%u", 1); |
| 421 | if (ret) |
| 422 | goto error_xenbus; |
| 423 | ret = xenbus_printf(xbt, dev->nodename, "num-rings", "%u", |
| 424 | priv->num_rings); |
| 425 | if (ret) |
| 426 | goto error_xenbus; |
| 427 | for (i = 0; i < priv->num_rings; i++) { |
| 428 | char str[16]; |
| 429 | |
| 430 | BUILD_BUG_ON(XEN_9PFS_NUM_RINGS > 9); |
Ye Bin | 316a1be | 2020-10-09 16:05:52 +0800 | [diff] [blame] | 431 | sprintf(str, "ring-ref%d", i); |
Stefano Stabellini | 71ebd71 | 2017-04-05 12:03:58 -0700 | [diff] [blame] | 432 | ret = xenbus_printf(xbt, dev->nodename, str, "%d", |
| 433 | priv->rings[i].ref); |
| 434 | if (ret) |
| 435 | goto error_xenbus; |
| 436 | |
Ye Bin | 316a1be | 2020-10-09 16:05:52 +0800 | [diff] [blame] | 437 | sprintf(str, "event-channel-%d", i); |
Stefano Stabellini | 71ebd71 | 2017-04-05 12:03:58 -0700 | [diff] [blame] | 438 | ret = xenbus_printf(xbt, dev->nodename, str, "%u", |
| 439 | priv->rings[i].evtchn); |
| 440 | if (ret) |
| 441 | goto error_xenbus; |
| 442 | } |
| 443 | priv->tag = xenbus_read(xbt, dev->nodename, "tag", NULL); |
Wei Yongjun | 14e3995 | 2017-05-16 14:22:47 +0000 | [diff] [blame] | 444 | if (IS_ERR(priv->tag)) { |
| 445 | ret = PTR_ERR(priv->tag); |
Stefano Stabellini | 71ebd71 | 2017-04-05 12:03:58 -0700 | [diff] [blame] | 446 | goto error_xenbus; |
| 447 | } |
| 448 | ret = xenbus_transaction_end(xbt, 0); |
| 449 | if (ret) { |
| 450 | if (ret == -EAGAIN) |
| 451 | goto again; |
| 452 | xenbus_dev_fatal(dev, ret, "completing transaction"); |
| 453 | goto error; |
| 454 | } |
| 455 | |
| 456 | write_lock(&xen_9pfs_lock); |
| 457 | list_add_tail(&priv->list, &xen_9pfs_devs); |
| 458 | write_unlock(&xen_9pfs_lock); |
| 459 | dev_set_drvdata(&dev->dev, priv); |
| 460 | xenbus_switch_state(dev, XenbusStateInitialised); |
| 461 | |
Stefano Stabellini | 868eb12 | 2017-04-05 12:03:57 -0700 | [diff] [blame] | 462 | return 0; |
Stefano Stabellini | 71ebd71 | 2017-04-05 12:03:58 -0700 | [diff] [blame] | 463 | |
| 464 | error_xenbus: |
| 465 | xenbus_transaction_end(xbt, 1); |
| 466 | xenbus_dev_fatal(dev, ret, "writing xenstore"); |
| 467 | error: |
| 468 | dev_set_drvdata(&dev->dev, NULL); |
| 469 | xen_9pfs_front_free(priv); |
| 470 | return ret; |
Stefano Stabellini | 868eb12 | 2017-04-05 12:03:57 -0700 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | static int xen_9pfs_front_resume(struct xenbus_device *dev) |
| 474 | { |
Colin Ian King | 680a2846 | 2018-05-09 10:48:33 +0100 | [diff] [blame] | 475 | dev_warn(&dev->dev, "suspend/resume unsupported\n"); |
Stefano Stabellini | 868eb12 | 2017-04-05 12:03:57 -0700 | [diff] [blame] | 476 | return 0; |
| 477 | } |
| 478 | |
| 479 | static void xen_9pfs_front_changed(struct xenbus_device *dev, |
| 480 | enum xenbus_state backend_state) |
| 481 | { |
Stefano Stabellini | 71ebd71 | 2017-04-05 12:03:58 -0700 | [diff] [blame] | 482 | switch (backend_state) { |
| 483 | case XenbusStateReconfiguring: |
| 484 | case XenbusStateReconfigured: |
| 485 | case XenbusStateInitialising: |
| 486 | case XenbusStateInitialised: |
| 487 | case XenbusStateUnknown: |
| 488 | break; |
| 489 | |
| 490 | case XenbusStateInitWait: |
| 491 | break; |
| 492 | |
| 493 | case XenbusStateConnected: |
| 494 | xenbus_switch_state(dev, XenbusStateConnected); |
| 495 | break; |
| 496 | |
| 497 | case XenbusStateClosed: |
| 498 | if (dev->state == XenbusStateClosed) |
| 499 | break; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 500 | fallthrough; /* Missed the backend's CLOSING state */ |
Stefano Stabellini | 71ebd71 | 2017-04-05 12:03:58 -0700 | [diff] [blame] | 501 | case XenbusStateClosing: |
| 502 | xenbus_frontend_closed(dev); |
| 503 | break; |
| 504 | } |
Stefano Stabellini | 868eb12 | 2017-04-05 12:03:57 -0700 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | static struct xenbus_driver xen_9pfs_front_driver = { |
| 508 | .ids = xen_9pfs_front_ids, |
| 509 | .probe = xen_9pfs_front_probe, |
| 510 | .remove = xen_9pfs_front_remove, |
| 511 | .resume = xen_9pfs_front_resume, |
| 512 | .otherend_changed = xen_9pfs_front_changed, |
| 513 | }; |
| 514 | |
Wei Yongjun | aaf0475 | 2017-05-18 15:22:41 +0000 | [diff] [blame] | 515 | static int p9_trans_xen_init(void) |
Stefano Stabellini | 868eb12 | 2017-04-05 12:03:57 -0700 | [diff] [blame] | 516 | { |
YueHaibing | 80a316f | 2019-04-30 22:39:33 +0800 | [diff] [blame] | 517 | int rc; |
| 518 | |
Stefano Stabellini | 868eb12 | 2017-04-05 12:03:57 -0700 | [diff] [blame] | 519 | if (!xen_domain()) |
| 520 | return -ENODEV; |
| 521 | |
| 522 | pr_info("Initialising Xen transport for 9pfs\n"); |
| 523 | |
| 524 | v9fs_register_trans(&p9_xen_trans); |
YueHaibing | 80a316f | 2019-04-30 22:39:33 +0800 | [diff] [blame] | 525 | rc = xenbus_register_frontend(&xen_9pfs_front_driver); |
| 526 | if (rc) |
| 527 | v9fs_unregister_trans(&p9_xen_trans); |
| 528 | |
| 529 | return rc; |
Stefano Stabellini | 868eb12 | 2017-04-05 12:03:57 -0700 | [diff] [blame] | 530 | } |
| 531 | module_init(p9_trans_xen_init); |
Thomas Weißschuh | 4cd82a5 | 2021-10-17 15:46:11 +0200 | [diff] [blame] | 532 | MODULE_ALIAS_9P("xen"); |
Stefano Stabellini | 868eb12 | 2017-04-05 12:03:57 -0700 | [diff] [blame] | 533 | |
Wei Yongjun | aaf0475 | 2017-05-18 15:22:41 +0000 | [diff] [blame] | 534 | static void p9_trans_xen_exit(void) |
Stefano Stabellini | 868eb12 | 2017-04-05 12:03:57 -0700 | [diff] [blame] | 535 | { |
| 536 | v9fs_unregister_trans(&p9_xen_trans); |
| 537 | return xenbus_unregister_driver(&xen_9pfs_front_driver); |
| 538 | } |
| 539 | module_exit(p9_trans_xen_exit); |
Stephen Hemminger | d542296 | 2018-01-08 08:23:18 -0800 | [diff] [blame] | 540 | |
Thomas Weißschuh | 99aa673 | 2021-11-03 20:38:22 +0100 | [diff] [blame] | 541 | MODULE_ALIAS("xen:9pfs"); |
Stephen Hemminger | d542296 | 2018-01-08 08:23:18 -0800 | [diff] [blame] | 542 | MODULE_AUTHOR("Stefano Stabellini <stefano@aporeto.com>"); |
| 543 | MODULE_DESCRIPTION("Xen Transport for 9P"); |
| 544 | MODULE_LICENSE("GPL"); |