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