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