]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blame - drivers/block/xen-blkback/xenbus.c
Merge remote-tracking branches 'regulator/topic/act8865', 'regulator/topic/anatop...
[mirror_ubuntu-zesty-kernel.git] / drivers / block / xen-blkback / xenbus.c
CommitLineData
4d05a28d
KRW
1/* Xenbus code for blkif backend
2 Copyright (C) 2005 Rusty Russell <rusty@rustcorp.com.au>
3 Copyright (C) 2005 XenSource Ltd
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
4d05a28d
KRW
15*/
16
77387b82
TC
17#define pr_fmt(fmt) "xen-blkback: " fmt
18
4d05a28d
KRW
19#include <stdarg.h>
20#include <linux/module.h>
21#include <linux/kthread.h>
ee9ff853
KRW
22#include <xen/events.h>
23#include <xen/grant_table.h>
4d05a28d
KRW
24#include "common.h"
25
1375590d
TC
26/* Enlarge the array size in order to fully show blkback name. */
27#define BLKBACK_NAME_LEN (20)
86839c56 28#define RINGREF_NAME_LEN (20)
1375590d 29
d6091b21 30struct backend_info {
01f37f2d 31 struct xenbus_device *dev;
51854322 32 struct xen_blkif *blkif;
01f37f2d
KRW
33 struct xenbus_watch backend_watch;
34 unsigned major;
35 unsigned minor;
36 char *mode;
4d05a28d
KRW
37};
38
8b6bf747 39static struct kmem_cache *xen_blkif_cachep;
4d05a28d
KRW
40static void connect(struct backend_info *);
41static int connect_ring(struct backend_info *);
42static void backend_changed(struct xenbus_watch *, const char **,
43 unsigned int);
814d04e7
VP
44static void xen_blkif_free(struct xen_blkif *blkif);
45static void xen_vbd_free(struct xen_vbd *vbd);
4d05a28d 46
8b6bf747 47struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be)
98e036a3
JF
48{
49 return be->dev;
50}
51
814d04e7
VP
52/*
53 * The last request could free the device from softirq context and
54 * xen_blkif_free() can sleep.
55 */
56static void xen_blkif_deferred_free(struct work_struct *work)
57{
58 struct xen_blkif *blkif;
59
60 blkif = container_of(work, struct xen_blkif, free_work);
61 xen_blkif_free(blkif);
62}
63
30fd1502 64static int blkback_name(struct xen_blkif *blkif, char *buf)
4d05a28d
KRW
65{
66 char *devpath, *devname;
67 struct xenbus_device *dev = blkif->be->dev;
68
69 devpath = xenbus_read(XBT_NIL, dev->nodename, "dev", NULL);
70 if (IS_ERR(devpath))
71 return PTR_ERR(devpath);
72
d6091b21
KRW
73 devname = strstr(devpath, "/dev/");
74 if (devname != NULL)
4d05a28d
KRW
75 devname += strlen("/dev/");
76 else
77 devname = devpath;
78
1375590d 79 snprintf(buf, BLKBACK_NAME_LEN, "blkback.%d.%s", blkif->domid, devname);
4d05a28d
KRW
80 kfree(devpath);
81
82 return 0;
83}
84
30fd1502 85static void xen_update_blkif_status(struct xen_blkif *blkif)
4d05a28d
KRW
86{
87 int err;
1375590d 88 char name[BLKBACK_NAME_LEN];
4d05a28d
KRW
89
90 /* Not ready to connect? */
91 if (!blkif->irq || !blkif->vbd.bdev)
92 return;
93
94 /* Already connected? */
95 if (blkif->be->dev->state == XenbusStateConnected)
96 return;
97
98 /* Attempt to connect: exit if we fail to. */
99 connect(blkif->be);
100 if (blkif->be->dev->state != XenbusStateConnected)
101 return;
102
103 err = blkback_name(blkif, name);
104 if (err) {
105 xenbus_dev_error(blkif->be->dev, err, "get blkback dev name");
106 return;
107 }
108
cbf46290
CL
109 err = filemap_write_and_wait(blkif->vbd.bdev->bd_inode->i_mapping);
110 if (err) {
111 xenbus_dev_error(blkif->be->dev, err, "block flush");
112 return;
113 }
114 invalidate_inode_pages2(blkif->vbd.bdev->bd_inode->i_mapping);
115
f170168b 116 blkif->xenblkd = kthread_run(xen_blkif_schedule, blkif, "%s", name);
4d05a28d
KRW
117 if (IS_ERR(blkif->xenblkd)) {
118 err = PTR_ERR(blkif->xenblkd);
119 blkif->xenblkd = NULL;
120 xenbus_dev_error(blkif->be->dev, err, "start xenblkd");
3f3aad5e 121 return;
4d05a28d
KRW
122 }
123}
124
30fd1502 125static struct xen_blkif *xen_blkif_alloc(domid_t domid)
ee9ff853 126{
30fd1502 127 struct xen_blkif *blkif;
ee9ff853 128
402b27f9 129 BUILD_BUG_ON(MAX_INDIRECT_PAGES > BLKIF_MAX_INDIRECT_PAGES_PER_REQUEST);
ee9ff853 130
654dbef2 131 blkif = kmem_cache_zalloc(xen_blkif_cachep, GFP_KERNEL);
ee9ff853
KRW
132 if (!blkif)
133 return ERR_PTR(-ENOMEM);
134
ee9ff853
KRW
135 blkif->domid = domid;
136 spin_lock_init(&blkif->blk_ring_lock);
137 atomic_set(&blkif->refcnt, 1);
138 init_waitqueue_head(&blkif->wq);
29bde093
KRW
139 init_completion(&blkif->drain_complete);
140 atomic_set(&blkif->drain, 0);
ee9ff853 141 blkif->st_print = jiffies;
0a8704a5 142 blkif->persistent_gnts.rb_node = NULL;
c6cc142d
RPM
143 spin_lock_init(&blkif->free_pages_lock);
144 INIT_LIST_HEAD(&blkif->free_pages);
ef753411 145 INIT_LIST_HEAD(&blkif->persistent_purge_list);
c6cc142d 146 blkif->free_pages_num = 0;
3f3aad5e 147 atomic_set(&blkif->persistent_gnt_in_use, 0);
c05f3e3c 148 atomic_set(&blkif->inflight, 0);
abb97b8c 149 INIT_WORK(&blkif->persistent_purge_work, xen_blkbk_unmap_purged_grants);
ee9ff853 150
bf0720c4 151 INIT_LIST_HEAD(&blkif->pending_free);
814d04e7 152 INIT_WORK(&blkif->free_work, xen_blkif_deferred_free);
bf0720c4
RPM
153 spin_lock_init(&blkif->pending_free_lock);
154 init_waitqueue_head(&blkif->pending_free_wq);
8e3f8755 155 init_waitqueue_head(&blkif->shutdown_wq);
ee9ff853
KRW
156
157 return blkif;
158}
159
86839c56
BL
160static int xen_blkif_map(struct xen_blkif *blkif, grant_ref_t *gref,
161 unsigned int nr_grefs, unsigned int evtchn)
ee9ff853
KRW
162{
163 int err;
164
165 /* Already connected through? */
166 if (blkif->irq)
167 return 0;
168
86839c56 169 err = xenbus_map_ring_valloc(blkif->be->dev, gref, nr_grefs,
ccc9d90a 170 &blkif->blk_ring);
2d073846 171 if (err < 0)
ee9ff853 172 return err;
ee9ff853
KRW
173
174 switch (blkif->blk_protocol) {
175 case BLKIF_PROTOCOL_NATIVE:
176 {
177 struct blkif_sring *sring;
2d073846 178 sring = (struct blkif_sring *)blkif->blk_ring;
86839c56 179 BACK_RING_INIT(&blkif->blk_rings.native, sring, PAGE_SIZE * nr_grefs);
ee9ff853
KRW
180 break;
181 }
182 case BLKIF_PROTOCOL_X86_32:
183 {
184 struct blkif_x86_32_sring *sring_x86_32;
2d073846 185 sring_x86_32 = (struct blkif_x86_32_sring *)blkif->blk_ring;
86839c56 186 BACK_RING_INIT(&blkif->blk_rings.x86_32, sring_x86_32, PAGE_SIZE * nr_grefs);
ee9ff853
KRW
187 break;
188 }
189 case BLKIF_PROTOCOL_X86_64:
190 {
191 struct blkif_x86_64_sring *sring_x86_64;
2d073846 192 sring_x86_64 = (struct blkif_x86_64_sring *)blkif->blk_ring;
86839c56 193 BACK_RING_INIT(&blkif->blk_rings.x86_64, sring_x86_64, PAGE_SIZE * nr_grefs);
ee9ff853
KRW
194 break;
195 }
196 default:
197 BUG();
198 }
199
8b6bf747
KRW
200 err = bind_interdomain_evtchn_to_irqhandler(blkif->domid, evtchn,
201 xen_blkif_be_int, 0,
202 "blkif-backend", blkif);
ee9ff853 203 if (err < 0) {
2d073846 204 xenbus_unmap_ring_vfree(blkif->be->dev, blkif->blk_ring);
ee9ff853
KRW
205 blkif->blk_rings.common.sring = NULL;
206 return err;
207 }
208 blkif->irq = err;
209
210 return 0;
211}
212
814d04e7 213static int xen_blkif_disconnect(struct xen_blkif *blkif)
ee9ff853 214{
f929d42c
RPM
215 struct pending_req *req, *n;
216 int i = 0, j;
217
ee9ff853
KRW
218 if (blkif->xenblkd) {
219 kthread_stop(blkif->xenblkd);
8e3f8755 220 wake_up(&blkif->shutdown_wq);
ee9ff853
KRW
221 blkif->xenblkd = NULL;
222 }
223
814d04e7
VP
224 /* The above kthread_stop() guarantees that at this point we
225 * don't have any discard_io or other_io requests. So, checking
226 * for inflight IO is enough.
227 */
228 if (atomic_read(&blkif->inflight) > 0)
229 return -EBUSY;
ee9ff853
KRW
230
231 if (blkif->irq) {
232 unbind_from_irqhandler(blkif->irq, blkif);
233 blkif->irq = 0;
234 }
235
236 if (blkif->blk_rings.common.sring) {
2d073846 237 xenbus_unmap_ring_vfree(blkif->be->dev, blkif->blk_ring);
ee9ff853
KRW
238 blkif->blk_rings.common.sring = NULL;
239 }
814d04e7 240
12ea7296
VK
241 /* Remove all persistent grants and the cache of ballooned pages. */
242 xen_blkbk_free_caches(blkif);
243
f929d42c
RPM
244 /* Check that there is no request in use */
245 list_for_each_entry_safe(req, n, &blkif->pending_free, free_list) {
246 list_del(&req->free_list);
247
248 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++)
249 kfree(req->segments[j]);
250
251 for (j = 0; j < MAX_INDIRECT_PAGES; j++)
252 kfree(req->indirect_pages[j]);
253
254 kfree(req);
255 i++;
256 }
257
258 WARN_ON(i != (XEN_BLKIF_REQS_PER_PAGE * blkif->nr_ring_pages));
259 blkif->nr_ring_pages = 0;
260
814d04e7 261 return 0;
ee9ff853
KRW
262}
263
2911758f 264static void xen_blkif_free(struct xen_blkif *blkif)
ee9ff853 265{
bf0720c4 266
814d04e7
VP
267 xen_blkif_disconnect(blkif);
268 xen_vbd_free(&blkif->vbd);
bf0720c4 269
ef753411
RPM
270 /* Make sure everything is drained before shutting down */
271 BUG_ON(blkif->persistent_gnt_c != 0);
272 BUG_ON(atomic_read(&blkif->persistent_gnt_in_use) != 0);
273 BUG_ON(blkif->free_pages_num != 0);
274 BUG_ON(!list_empty(&blkif->persistent_purge_list));
275 BUG_ON(!list_empty(&blkif->free_pages));
276 BUG_ON(!RB_EMPTY_ROOT(&blkif->persistent_gnts));
277
8b6bf747 278 kmem_cache_free(xen_blkif_cachep, blkif);
ee9ff853
KRW
279}
280
8b6bf747 281int __init xen_blkif_interface_init(void)
ee9ff853 282{
8b6bf747 283 xen_blkif_cachep = kmem_cache_create("blkif_cache",
30fd1502 284 sizeof(struct xen_blkif),
8b6bf747
KRW
285 0, 0, NULL);
286 if (!xen_blkif_cachep)
ee9ff853
KRW
287 return -ENOMEM;
288
289 return 0;
290}
4d05a28d 291
a1397fa3 292/*
4d05a28d
KRW
293 * sysfs interface for VBD I/O requests
294 */
295
296#define VBD_SHOW(name, format, args...) \
297 static ssize_t show_##name(struct device *_dev, \
298 struct device_attribute *attr, \
299 char *buf) \
300 { \
301 struct xenbus_device *dev = to_xenbus_device(_dev); \
5cf6e4f6 302 struct backend_info *be = dev_get_drvdata(&dev->dev); \
4d05a28d
KRW
303 \
304 return sprintf(buf, format, ##args); \
305 } \
306 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
307
986cacbd
ZK
308VBD_SHOW(oo_req, "%llu\n", be->blkif->st_oo_req);
309VBD_SHOW(rd_req, "%llu\n", be->blkif->st_rd_req);
310VBD_SHOW(wr_req, "%llu\n", be->blkif->st_wr_req);
311VBD_SHOW(f_req, "%llu\n", be->blkif->st_f_req);
312VBD_SHOW(ds_req, "%llu\n", be->blkif->st_ds_req);
313VBD_SHOW(rd_sect, "%llu\n", be->blkif->st_rd_sect);
314VBD_SHOW(wr_sect, "%llu\n", be->blkif->st_wr_sect);
4d05a28d 315
3d814731 316static struct attribute *xen_vbdstat_attrs[] = {
4d05a28d
KRW
317 &dev_attr_oo_req.attr,
318 &dev_attr_rd_req.attr,
319 &dev_attr_wr_req.attr,
24f567f9 320 &dev_attr_f_req.attr,
b3cb0d6a 321 &dev_attr_ds_req.attr,
4d05a28d
KRW
322 &dev_attr_rd_sect.attr,
323 &dev_attr_wr_sect.attr,
324 NULL
325};
326
3d814731 327static struct attribute_group xen_vbdstat_group = {
4d05a28d 328 .name = "statistics",
3d814731 329 .attrs = xen_vbdstat_attrs,
4d05a28d
KRW
330};
331
332VBD_SHOW(physical_device, "%x:%x\n", be->major, be->minor);
333VBD_SHOW(mode, "%s\n", be->mode);
334
2911758f 335static int xenvbd_sysfs_addif(struct xenbus_device *dev)
4d05a28d
KRW
336{
337 int error;
338
339 error = device_create_file(&dev->dev, &dev_attr_physical_device);
d6091b21 340 if (error)
4d05a28d
KRW
341 goto fail1;
342
343 error = device_create_file(&dev->dev, &dev_attr_mode);
344 if (error)
345 goto fail2;
346
3d814731 347 error = sysfs_create_group(&dev->dev.kobj, &xen_vbdstat_group);
4d05a28d
KRW
348 if (error)
349 goto fail3;
350
351 return 0;
352
3d814731 353fail3: sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group);
4d05a28d
KRW
354fail2: device_remove_file(&dev->dev, &dev_attr_mode);
355fail1: device_remove_file(&dev->dev, &dev_attr_physical_device);
356 return error;
357}
358
2911758f 359static void xenvbd_sysfs_delif(struct xenbus_device *dev)
4d05a28d 360{
3d814731 361 sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group);
4d05a28d
KRW
362 device_remove_file(&dev->dev, &dev_attr_mode);
363 device_remove_file(&dev->dev, &dev_attr_physical_device);
364}
365
42c7841d 366
3d814731 367static void xen_vbd_free(struct xen_vbd *vbd)
42c7841d
KRW
368{
369 if (vbd->bdev)
370 blkdev_put(vbd->bdev, vbd->readonly ? FMODE_READ : FMODE_WRITE);
371 vbd->bdev = NULL;
372}
373
3d814731
KRW
374static int xen_vbd_create(struct xen_blkif *blkif, blkif_vdev_t handle,
375 unsigned major, unsigned minor, int readonly,
376 int cdrom)
42c7841d 377{
3d814731 378 struct xen_vbd *vbd;
42c7841d 379 struct block_device *bdev;
24f567f9 380 struct request_queue *q;
42c7841d
KRW
381
382 vbd = &blkif->vbd;
383 vbd->handle = handle;
384 vbd->readonly = readonly;
385 vbd->type = 0;
386
387 vbd->pdevice = MKDEV(major, minor);
388
389 bdev = blkdev_get_by_dev(vbd->pdevice, vbd->readonly ?
390 FMODE_READ : FMODE_WRITE, NULL);
391
392 if (IS_ERR(bdev)) {
77387b82 393 pr_warn("xen_vbd_create: device %08x could not be opened\n",
42c7841d
KRW
394 vbd->pdevice);
395 return -ENOENT;
396 }
397
398 vbd->bdev = bdev;
42c7841d 399 if (vbd->bdev->bd_disk == NULL) {
77387b82 400 pr_warn("xen_vbd_create: device %08x doesn't exist\n",
42c7841d 401 vbd->pdevice);
3d814731 402 xen_vbd_free(vbd);
42c7841d
KRW
403 return -ENOENT;
404 }
6464920a 405 vbd->size = vbd_sz(vbd);
42c7841d
KRW
406
407 if (vbd->bdev->bd_disk->flags & GENHD_FL_CD || cdrom)
408 vbd->type |= VDISK_CDROM;
409 if (vbd->bdev->bd_disk->flags & GENHD_FL_REMOVABLE)
410 vbd->type |= VDISK_REMOVABLE;
411
24f567f9
KRW
412 q = bdev_get_queue(bdev);
413 if (q && q->flush_flags)
414 vbd->flush_support = true;
415
5ea42986
KRW
416 if (q && blk_queue_secdiscard(q))
417 vbd->discard_secure = true;
418
77387b82 419 pr_debug("Successful creation of handle=%04x (dom=%u)\n",
42c7841d
KRW
420 handle, blkif->domid);
421 return 0;
422}
8b6bf747 423static int xen_blkbk_remove(struct xenbus_device *dev)
4d05a28d 424{
5cf6e4f6 425 struct backend_info *be = dev_get_drvdata(&dev->dev);
4d05a28d 426
77387b82 427 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
4d05a28d
KRW
428
429 if (be->major || be->minor)
430 xenvbd_sysfs_delif(dev);
431
432 if (be->backend_watch.node) {
433 unregister_xenbus_watch(&be->backend_watch);
434 kfree(be->backend_watch.node);
435 be->backend_watch.node = NULL;
436 }
437
814d04e7
VP
438 dev_set_drvdata(&dev->dev, NULL);
439
4d05a28d 440 if (be->blkif) {
8b6bf747 441 xen_blkif_disconnect(be->blkif);
814d04e7 442 xen_blkif_put(be->blkif);
4d05a28d
KRW
443 }
444
9d092603 445 kfree(be->mode);
4d05a28d 446 kfree(be);
4d05a28d
KRW
447 return 0;
448}
449
24f567f9
KRW
450int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt,
451 struct backend_info *be, int state)
4d05a28d
KRW
452{
453 struct xenbus_device *dev = be->dev;
454 int err;
455
24f567f9 456 err = xenbus_printf(xbt, dev->nodename, "feature-flush-cache",
4d05a28d
KRW
457 "%d", state);
458 if (err)
3389bb8b 459 dev_warn(&dev->dev, "writing feature-flush-cache (%d)", err);
4d05a28d
KRW
460
461 return err;
462}
463
3389bb8b 464static void xen_blkbk_discard(struct xenbus_transaction xbt, struct backend_info *be)
b3cb0d6a
LD
465{
466 struct xenbus_device *dev = be->dev;
467 struct xen_blkif *blkif = be->blkif;
b3cb0d6a 468 int err;
c926b701 469 int state = 0, discard_enable;
4dae7670
KRW
470 struct block_device *bdev = be->blkif->vbd.bdev;
471 struct request_queue *q = bdev_get_queue(bdev);
472
c926b701
OH
473 err = xenbus_scanf(XBT_NIL, dev->nodename, "discard-enable", "%d",
474 &discard_enable);
475 if (err == 1 && !discard_enable)
476 return;
477
4dae7670
KRW
478 if (blk_queue_discard(q)) {
479 err = xenbus_printf(xbt, dev->nodename,
480 "discard-granularity", "%u",
481 q->limits.discard_granularity);
482 if (err) {
3389bb8b
KRW
483 dev_warn(&dev->dev, "writing discard-granularity (%d)", err);
484 return;
4dae7670
KRW
485 }
486 err = xenbus_printf(xbt, dev->nodename,
487 "discard-alignment", "%u",
488 q->limits.discard_alignment);
489 if (err) {
3389bb8b
KRW
490 dev_warn(&dev->dev, "writing discard-alignment (%d)", err);
491 return;
b3cb0d6a 492 }
4dae7670
KRW
493 state = 1;
494 /* Optional. */
495 err = xenbus_printf(xbt, dev->nodename,
496 "discard-secure", "%d",
497 blkif->vbd.discard_secure);
498 if (err) {
a71e23d9 499 dev_warn(&dev->dev, "writing discard-secure (%d)", err);
3389bb8b 500 return;
b3cb0d6a 501 }
b3cb0d6a 502 }
b3cb0d6a
LD
503 err = xenbus_printf(xbt, dev->nodename, "feature-discard",
504 "%d", state);
505 if (err)
3389bb8b 506 dev_warn(&dev->dev, "writing feature-discard (%d)", err);
b3cb0d6a 507}
29bde093
KRW
508int xen_blkbk_barrier(struct xenbus_transaction xbt,
509 struct backend_info *be, int state)
510{
511 struct xenbus_device *dev = be->dev;
512 int err;
513
514 err = xenbus_printf(xbt, dev->nodename, "feature-barrier",
515 "%d", state);
516 if (err)
3389bb8b 517 dev_warn(&dev->dev, "writing feature-barrier (%d)", err);
29bde093
KRW
518
519 return err;
520}
b3cb0d6a 521
01f37f2d 522/*
4d05a28d
KRW
523 * Entry point to this code when a new device is created. Allocate the basic
524 * structures, and watch the store waiting for the hotplug scripts to tell us
525 * the device's physical major and minor numbers. Switch to InitWait.
526 */
8b6bf747
KRW
527static int xen_blkbk_probe(struct xenbus_device *dev,
528 const struct xenbus_device_id *id)
4d05a28d
KRW
529{
530 int err;
531 struct backend_info *be = kzalloc(sizeof(struct backend_info),
532 GFP_KERNEL);
77387b82
TC
533
534 /* match the pr_debug in xen_blkbk_remove */
535 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
536
4d05a28d
KRW
537 if (!be) {
538 xenbus_dev_fatal(dev, -ENOMEM,
539 "allocating backend structure");
540 return -ENOMEM;
541 }
542 be->dev = dev;
5cf6e4f6 543 dev_set_drvdata(&dev->dev, be);
4d05a28d 544
8b6bf747 545 be->blkif = xen_blkif_alloc(dev->otherend_id);
4d05a28d
KRW
546 if (IS_ERR(be->blkif)) {
547 err = PTR_ERR(be->blkif);
548 be->blkif = NULL;
549 xenbus_dev_fatal(dev, err, "creating block interface");
550 goto fail;
551 }
552
553 /* setup back pointer */
554 be->blkif->be = be;
555
88122933
JF
556 err = xenbus_watch_pathfmt(dev, &be->backend_watch, backend_changed,
557 "%s/%s", dev->nodename, "physical-device");
4d05a28d
KRW
558 if (err)
559 goto fail;
560
86839c56
BL
561 err = xenbus_printf(XBT_NIL, dev->nodename, "max-ring-page-order", "%u",
562 xen_blkif_max_ring_order);
563 if (err)
564 pr_warn("%s write out 'max-ring-page-order' failed\n", __func__);
565
4d05a28d
KRW
566 err = xenbus_switch_state(dev, XenbusStateInitWait);
567 if (err)
568 goto fail;
569
570 return 0;
571
572fail:
77387b82 573 pr_warn("%s failed\n", __func__);
8b6bf747 574 xen_blkbk_remove(dev);
4d05a28d
KRW
575 return err;
576}
577
578
01f37f2d 579/*
4d05a28d
KRW
580 * Callback received when the hotplug scripts have placed the physical-device
581 * node. Read it and the mode node, and create a vbd. If the frontend is
582 * ready, connect.
583 */
584static void backend_changed(struct xenbus_watch *watch,
585 const char **vec, unsigned int len)
586{
587 int err;
588 unsigned major;
589 unsigned minor;
590 struct backend_info *be
591 = container_of(watch, struct backend_info, backend_watch);
592 struct xenbus_device *dev = be->dev;
593 int cdrom = 0;
9d092603 594 unsigned long handle;
4d05a28d
KRW
595 char *device_type;
596
77387b82 597 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
4d05a28d
KRW
598
599 err = xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x:%x",
600 &major, &minor);
601 if (XENBUS_EXIST_ERR(err)) {
01f37f2d
KRW
602 /*
603 * Since this watch will fire once immediately after it is
604 * registered, we expect this. Ignore it, and wait for the
605 * hotplug scripts.
606 */
4d05a28d
KRW
607 return;
608 }
609 if (err != 2) {
610 xenbus_dev_fatal(dev, err, "reading physical-device");
611 return;
612 }
613
9d092603
JB
614 if (be->major | be->minor) {
615 if (be->major != major || be->minor != minor)
77387b82 616 pr_warn("changing physical device (from %x:%x to %x:%x) not supported.\n",
9d092603 617 be->major, be->minor, major, minor);
4d05a28d
KRW
618 return;
619 }
620
621 be->mode = xenbus_read(XBT_NIL, dev->nodename, "mode", NULL);
622 if (IS_ERR(be->mode)) {
623 err = PTR_ERR(be->mode);
624 be->mode = NULL;
625 xenbus_dev_fatal(dev, err, "reading mode");
626 return;
627 }
628
629 device_type = xenbus_read(XBT_NIL, dev->otherend, "device-type", NULL);
630 if (!IS_ERR(device_type)) {
631 cdrom = strcmp(device_type, "cdrom") == 0;
632 kfree(device_type);
633 }
634
9d092603 635 /* Front end dir is a number, which is used as the handle. */
bb8e0e84 636 err = kstrtoul(strrchr(dev->otherend, '/') + 1, 0, &handle);
9d092603
JB
637 if (err)
638 return;
4d05a28d 639
9d092603
JB
640 be->major = major;
641 be->minor = minor;
4d05a28d 642
9d092603
JB
643 err = xen_vbd_create(be->blkif, handle, major, minor,
644 !strchr(be->mode, 'w'), cdrom);
4d05a28d 645
9d092603
JB
646 if (err)
647 xenbus_dev_fatal(dev, err, "creating vbd structure");
648 else {
4d05a28d
KRW
649 err = xenvbd_sysfs_addif(dev);
650 if (err) {
3d814731 651 xen_vbd_free(&be->blkif->vbd);
4d05a28d 652 xenbus_dev_fatal(dev, err, "creating sysfs entries");
4d05a28d 653 }
9d092603 654 }
4d05a28d 655
9d092603
JB
656 if (err) {
657 kfree(be->mode);
658 be->mode = NULL;
659 be->major = 0;
660 be->minor = 0;
661 } else {
4d05a28d 662 /* We're potentially connected now */
8b6bf747 663 xen_update_blkif_status(be->blkif);
4d05a28d
KRW
664 }
665}
666
667
01f37f2d 668/*
4d05a28d
KRW
669 * Callback received when the frontend's state changes.
670 */
671static void frontend_changed(struct xenbus_device *dev,
672 enum xenbus_state frontend_state)
673{
5cf6e4f6 674 struct backend_info *be = dev_get_drvdata(&dev->dev);
4d05a28d
KRW
675 int err;
676
77387b82 677 pr_debug("%s %p %s\n", __func__, dev, xenbus_strstate(frontend_state));
4d05a28d
KRW
678
679 switch (frontend_state) {
680 case XenbusStateInitialising:
681 if (dev->state == XenbusStateClosed) {
77387b82 682 pr_info("%s: prepare for reconnect\n", dev->nodename);
4d05a28d
KRW
683 xenbus_switch_state(dev, XenbusStateInitWait);
684 }
685 break;
686
687 case XenbusStateInitialised:
688 case XenbusStateConnected:
01f37f2d
KRW
689 /*
690 * Ensure we connect even when two watches fire in
42b2aa86 691 * close succession and we miss the intermediate value
01f37f2d
KRW
692 * of frontend_state.
693 */
4d05a28d
KRW
694 if (dev->state == XenbusStateConnected)
695 break;
696
01f37f2d
KRW
697 /*
698 * Enforce precondition before potential leak point.
1bc05b0a 699 * xen_blkif_disconnect() is idempotent.
313d7b00 700 */
814d04e7
VP
701 err = xen_blkif_disconnect(be->blkif);
702 if (err) {
703 xenbus_dev_fatal(dev, err, "pending I/O");
704 break;
705 }
313d7b00 706
4d05a28d
KRW
707 err = connect_ring(be);
708 if (err)
709 break;
8b6bf747 710 xen_update_blkif_status(be->blkif);
4d05a28d
KRW
711 break;
712
713 case XenbusStateClosing:
4d05a28d
KRW
714 xenbus_switch_state(dev, XenbusStateClosing);
715 break;
716
717 case XenbusStateClosed:
6f5986bc 718 xen_blkif_disconnect(be->blkif);
4d05a28d
KRW
719 xenbus_switch_state(dev, XenbusStateClosed);
720 if (xenbus_dev_is_online(dev))
721 break;
722 /* fall through if not online */
723 case XenbusStateUnknown:
1bc05b0a 724 /* implies xen_blkif_disconnect() via xen_blkbk_remove() */
4d05a28d
KRW
725 device_unregister(&dev->dev);
726 break;
727
728 default:
729 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
730 frontend_state);
731 break;
732 }
733}
734
735
736/* ** Connection ** */
737
738
01f37f2d 739/*
4d05a28d
KRW
740 * Write the physical details regarding the block device to the store, and
741 * switch to Connected state.
742 */
743static void connect(struct backend_info *be)
744{
745 struct xenbus_transaction xbt;
746 int err;
747 struct xenbus_device *dev = be->dev;
748
77387b82 749 pr_debug("%s %s\n", __func__, dev->otherend);
4d05a28d
KRW
750
751 /* Supply the information about the device the frontend needs */
752again:
753 err = xenbus_transaction_start(&xbt);
754 if (err) {
755 xenbus_dev_fatal(dev, err, "starting transaction");
756 return;
757 }
758
3389bb8b
KRW
759 /* If we can't advertise it is OK. */
760 xen_blkbk_flush_diskcache(xbt, be, be->blkif->vbd.flush_support);
4d05a28d 761
3389bb8b 762 xen_blkbk_discard(xbt, be);
b3cb0d6a 763
3389bb8b 764 xen_blkbk_barrier(xbt, be, be->blkif->vbd.flush_support);
29bde093 765
0a8704a5
RPM
766 err = xenbus_printf(xbt, dev->nodename, "feature-persistent", "%u", 1);
767 if (err) {
768 xenbus_dev_fatal(dev, err, "writing %s/feature-persistent",
769 dev->nodename);
770 goto abort;
771 }
402b27f9
RPM
772 err = xenbus_printf(xbt, dev->nodename, "feature-max-indirect-segments", "%u",
773 MAX_INDIRECT_SEGMENTS);
774 if (err)
775 dev_warn(&dev->dev, "writing %s/feature-max-indirect-segments (%d)",
776 dev->nodename, err);
0a8704a5 777
4d05a28d 778 err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
42c7841d 779 (unsigned long long)vbd_sz(&be->blkif->vbd));
4d05a28d
KRW
780 if (err) {
781 xenbus_dev_fatal(dev, err, "writing %s/sectors",
782 dev->nodename);
783 goto abort;
784 }
785
786 /* FIXME: use a typename instead */
787 err = xenbus_printf(xbt, dev->nodename, "info", "%u",
42c7841d
KRW
788 be->blkif->vbd.type |
789 (be->blkif->vbd.readonly ? VDISK_READONLY : 0));
4d05a28d
KRW
790 if (err) {
791 xenbus_dev_fatal(dev, err, "writing %s/info",
792 dev->nodename);
793 goto abort;
794 }
795 err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu",
42c7841d
KRW
796 (unsigned long)
797 bdev_logical_block_size(be->blkif->vbd.bdev));
4d05a28d
KRW
798 if (err) {
799 xenbus_dev_fatal(dev, err, "writing %s/sector-size",
800 dev->nodename);
801 goto abort;
802 }
7c4d7d71
SB
803 err = xenbus_printf(xbt, dev->nodename, "physical-sector-size", "%u",
804 bdev_physical_block_size(be->blkif->vbd.bdev));
805 if (err)
806 xenbus_dev_error(dev, err, "writing %s/physical-sector-size",
807 dev->nodename);
4d05a28d
KRW
808
809 err = xenbus_transaction_end(xbt, 0);
810 if (err == -EAGAIN)
811 goto again;
812 if (err)
813 xenbus_dev_fatal(dev, err, "ending transaction");
814
815 err = xenbus_switch_state(dev, XenbusStateConnected);
816 if (err)
08b8bfc1 817 xenbus_dev_fatal(dev, err, "%s: switching to Connected state",
4d05a28d
KRW
818 dev->nodename);
819
820 return;
821 abort:
822 xenbus_transaction_end(xbt, 1);
823}
824
825
826static int connect_ring(struct backend_info *be)
827{
828 struct xenbus_device *dev = be->dev;
86839c56
BL
829 unsigned int ring_ref[XENBUS_MAX_RING_PAGES];
830 unsigned int evtchn, nr_grefs, ring_page_order;
0a8704a5 831 unsigned int pers_grants;
4d05a28d 832 char protocol[64] = "";
69b91ede
BL
833 struct pending_req *req, *n;
834 int err, i, j;
4d05a28d 835
77387b82 836 pr_debug("%s %s\n", __func__, dev->otherend);
4d05a28d 837
86839c56
BL
838 err = xenbus_scanf(XBT_NIL, dev->otherend, "event-channel", "%u",
839 &evtchn);
840 if (err != 1) {
841 err = -EINVAL;
842 xenbus_dev_fatal(dev, err, "reading %s/event-channel",
4d05a28d
KRW
843 dev->otherend);
844 return err;
845 }
86839c56
BL
846 pr_info("event-channel %u\n", evtchn);
847
848 err = xenbus_scanf(XBT_NIL, dev->otherend, "ring-page-order", "%u",
849 &ring_page_order);
850 if (err != 1) {
851 err = xenbus_scanf(XBT_NIL, dev->otherend, "ring-ref",
852 "%u", &ring_ref[0]);
853 if (err != 1) {
854 err = -EINVAL;
855 xenbus_dev_fatal(dev, err, "reading %s/ring-ref",
856 dev->otherend);
857 return err;
858 }
859 nr_grefs = 1;
860 pr_info("%s:using single page: ring-ref %d\n", dev->otherend,
861 ring_ref[0]);
862 } else {
863 unsigned int i;
864
865 if (ring_page_order > xen_blkif_max_ring_order) {
866 err = -EINVAL;
867 xenbus_dev_fatal(dev, err, "%s/request %d ring page order exceed max:%d",
868 dev->otherend, ring_page_order,
869 xen_blkif_max_ring_order);
870 return err;
871 }
872
873 nr_grefs = 1 << ring_page_order;
874 for (i = 0; i < nr_grefs; i++) {
875 char ring_ref_name[RINGREF_NAME_LEN];
876
877 snprintf(ring_ref_name, RINGREF_NAME_LEN, "ring-ref%u", i);
878 err = xenbus_scanf(XBT_NIL, dev->otherend, ring_ref_name,
879 "%u", &ring_ref[i]);
880 if (err != 1) {
881 err = -EINVAL;
882 xenbus_dev_fatal(dev, err, "reading %s/%s",
883 dev->otherend, ring_ref_name);
884 return err;
885 }
886 pr_info("ring-ref%u: %u\n", i, ring_ref[i]);
887 }
888 }
4d05a28d 889
b042a3ca 890 be->blkif->blk_protocol = BLKIF_PROTOCOL_DEFAULT;
4d05a28d
KRW
891 err = xenbus_gather(XBT_NIL, dev->otherend, "protocol",
892 "%63s", protocol, NULL);
893 if (err)
b042a3ca 894 strcpy(protocol, "unspecified, assuming default");
4d05a28d
KRW
895 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_NATIVE))
896 be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
897 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_32))
898 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_32;
899 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_64))
900 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_64;
901 else {
902 xenbus_dev_fatal(dev, err, "unknown fe protocol %s", protocol);
903 return -1;
904 }
0a8704a5 905 err = xenbus_gather(XBT_NIL, dev->otherend,
cb5bd4d1 906 "feature-persistent", "%u",
0a8704a5
RPM
907 &pers_grants, NULL);
908 if (err)
909 pers_grants = 0;
910
911 be->blkif->vbd.feature_gnt_persistent = pers_grants;
912 be->blkif->vbd.overflow_max_grants = 0;
86839c56 913 be->blkif->nr_ring_pages = nr_grefs;
0a8704a5 914
86839c56
BL
915 pr_info("ring-pages:%d, event-channel %d, protocol %d (%s) %s\n",
916 nr_grefs, evtchn, be->blkif->blk_protocol, protocol,
0a8704a5 917 pers_grants ? "persistent grants" : "");
4d05a28d 918
86839c56 919 for (i = 0; i < nr_grefs * XEN_BLKIF_REQS_PER_PAGE; i++) {
69b91ede
BL
920 req = kzalloc(sizeof(*req), GFP_KERNEL);
921 if (!req)
922 goto fail;
923 list_add_tail(&req->free_list, &be->blkif->pending_free);
924 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++) {
925 req->segments[j] = kzalloc(sizeof(*req->segments[0]), GFP_KERNEL);
926 if (!req->segments[j])
927 goto fail;
928 }
929 for (j = 0; j < MAX_INDIRECT_PAGES; j++) {
930 req->indirect_pages[j] = kzalloc(sizeof(*req->indirect_pages[0]),
931 GFP_KERNEL);
932 if (!req->indirect_pages[j])
933 goto fail;
934 }
935 }
936
4d05a28d 937 /* Map the shared frame, irq etc. */
86839c56 938 err = xen_blkif_map(be->blkif, ring_ref, nr_grefs, evtchn);
4d05a28d 939 if (err) {
86839c56 940 xenbus_dev_fatal(dev, err, "mapping ring-ref port %u", evtchn);
4d05a28d
KRW
941 return err;
942 }
943
944 return 0;
69b91ede
BL
945
946fail:
947 list_for_each_entry_safe(req, n, &be->blkif->pending_free, free_list) {
948 list_del(&req->free_list);
949 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++) {
950 if (!req->segments[j])
951 break;
952 kfree(req->segments[j]);
953 }
954 for (j = 0; j < MAX_INDIRECT_PAGES; j++) {
955 if (!req->indirect_pages[j])
956 break;
957 kfree(req->indirect_pages[j]);
958 }
959 kfree(req);
960 }
961 return -ENOMEM;
4d05a28d
KRW
962}
963
8b6bf747 964static const struct xenbus_device_id xen_blkbk_ids[] = {
4d05a28d
KRW
965 { "vbd" },
966 { "" }
967};
968
95afae48
DV
969static struct xenbus_driver xen_blkbk_driver = {
970 .ids = xen_blkbk_ids,
8b6bf747
KRW
971 .probe = xen_blkbk_probe,
972 .remove = xen_blkbk_remove,
4d05a28d 973 .otherend_changed = frontend_changed
95afae48 974};
4d05a28d 975
8b6bf747 976int xen_blkif_xenbus_init(void)
4d05a28d 977{
73db144b 978 return xenbus_register_backend(&xen_blkbk_driver);
4d05a28d 979}