blob: deb3f001791f159c5c7ebce19814de31e3106a5e [file] [log] [blame]
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001/* 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
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040015*/
16
Tao Chen77387b82015-04-01 15:04:22 +000017#define pr_fmt(fmt) "xen-blkback: " fmt
18
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040019#include <stdarg.h>
20#include <linux/module.h>
21#include <linux/kthread.h>
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -040022#include <xen/events.h>
23#include <xen/grant_table.h>
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040024#include "common.h"
25
Tao Chen13755902015-03-27 13:15:54 +000026/* Enlarge the array size in order to fully show blkback name. */
27#define BLKBACK_NAME_LEN (20)
Bob Liu86839c52015-06-03 13:40:03 +080028#define RINGREF_NAME_LEN (20)
Tao Chen13755902015-03-27 13:15:54 +000029
Konrad Rzeszutek Wilkd6091b22011-04-14 17:33:30 -040030struct backend_info {
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -040031 struct xenbus_device *dev;
Konrad Rzeszutek Wilk51854322011-05-12 18:02:28 -040032 struct xen_blkif *blkif;
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -040033 struct xenbus_watch backend_watch;
34 unsigned major;
35 unsigned minor;
36 char *mode;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040037};
38
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -040039static struct kmem_cache *xen_blkif_cachep;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040040static void connect(struct backend_info *);
41static int connect_ring(struct backend_info *);
42static void backend_changed(struct xenbus_watch *, const char **,
43 unsigned int);
Valentin Priescu814d04e2014-05-20 22:28:50 +020044static void xen_blkif_free(struct xen_blkif *blkif);
45static void xen_vbd_free(struct xen_vbd *vbd);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040046
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -040047struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be)
Jeremy Fitzhardinge98e036a2010-03-18 15:35:05 -070048{
49 return be->dev;
50}
51
Valentin Priescu814d04e2014-05-20 22:28:50 +020052/*
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
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -040064static int blkback_name(struct xen_blkif *blkif, char *buf)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040065{
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
Konrad Rzeszutek Wilkd6091b22011-04-14 17:33:30 -040073 devname = strstr(devpath, "/dev/");
74 if (devname != NULL)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040075 devname += strlen("/dev/");
76 else
77 devname = devpath;
78
Tao Chen13755902015-03-27 13:15:54 +000079 snprintf(buf, BLKBACK_NAME_LEN, "blkback.%d.%s", blkif->domid, devname);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040080 kfree(devpath);
81
82 return 0;
83}
84
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -040085static void xen_update_blkif_status(struct xen_blkif *blkif)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040086{
87 int err;
Tao Chen13755902015-03-27 13:15:54 +000088 char name[BLKBACK_NAME_LEN];
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040089
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
Chris Lalancettecbf46292010-07-21 12:41:45 -0700109 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
Kees Cookf1701682013-07-03 15:04:58 -0700116 blkif->xenblkd = kthread_run(xen_blkif_schedule, blkif, "%s", name);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400117 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");
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200121 return;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400122 }
123}
124
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400125static struct xen_blkif *xen_blkif_alloc(domid_t domid)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400126{
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400127 struct xen_blkif *blkif;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400128
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200129 BUILD_BUG_ON(MAX_INDIRECT_PAGES > BLKIF_MAX_INDIRECT_PAGES_PER_REQUEST);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400130
Wei Yongjun654dbef2012-08-27 12:28:57 +0800131 blkif = kmem_cache_zalloc(xen_blkif_cachep, GFP_KERNEL);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400132 if (!blkif)
133 return ERR_PTR(-ENOMEM);
134
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400135 blkif->domid = domid;
136 spin_lock_init(&blkif->blk_ring_lock);
137 atomic_set(&blkif->refcnt, 1);
138 init_waitqueue_head(&blkif->wq);
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400139 init_completion(&blkif->drain_complete);
140 atomic_set(&blkif->drain, 0);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400141 blkif->st_print = jiffies;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200142 blkif->persistent_gnts.rb_node = NULL;
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200143 spin_lock_init(&blkif->free_pages_lock);
144 INIT_LIST_HEAD(&blkif->free_pages);
Roger Pau Monneef753412014-02-04 11:26:13 +0100145 INIT_LIST_HEAD(&blkif->persistent_purge_list);
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200146 blkif->free_pages_num = 0;
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200147 atomic_set(&blkif->persistent_gnt_in_use, 0);
Roger Pau Monnec05f3e32014-02-04 11:26:14 +0100148 atomic_set(&blkif->inflight, 0);
Roger Pau Monneabb97b82014-02-11 20:34:03 -0700149 INIT_WORK(&blkif->persistent_purge_work, xen_blkbk_unmap_purged_grants);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400150
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200151 INIT_LIST_HEAD(&blkif->pending_free);
Valentin Priescu814d04e2014-05-20 22:28:50 +0200152 INIT_WORK(&blkif->free_work, xen_blkif_deferred_free);
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200153 spin_lock_init(&blkif->pending_free_lock);
154 init_waitqueue_head(&blkif->pending_free_wq);
Konrad Rzeszutek Wilk8e3f8752013-01-23 16:54:32 -0500155 init_waitqueue_head(&blkif->shutdown_wq);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400156
157 return blkif;
158}
159
Bob Liu86839c52015-06-03 13:40:03 +0800160static int xen_blkif_map(struct xen_blkif *blkif, grant_ref_t *gref,
161 unsigned int nr_grefs, unsigned int evtchn)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400162{
163 int err;
164
165 /* Already connected through? */
166 if (blkif->irq)
167 return 0;
168
Bob Liu86839c52015-06-03 13:40:03 +0800169 err = xenbus_map_ring_valloc(blkif->be->dev, gref, nr_grefs,
Wei Liuccc9d902015-04-03 14:44:59 +0800170 &blkif->blk_ring);
David Vrabel2d073842011-09-29 16:53:30 +0100171 if (err < 0)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400172 return err;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400173
174 switch (blkif->blk_protocol) {
175 case BLKIF_PROTOCOL_NATIVE:
176 {
177 struct blkif_sring *sring;
David Vrabel2d073842011-09-29 16:53:30 +0100178 sring = (struct blkif_sring *)blkif->blk_ring;
Bob Liu86839c52015-06-03 13:40:03 +0800179 BACK_RING_INIT(&blkif->blk_rings.native, sring, PAGE_SIZE * nr_grefs);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400180 break;
181 }
182 case BLKIF_PROTOCOL_X86_32:
183 {
184 struct blkif_x86_32_sring *sring_x86_32;
David Vrabel2d073842011-09-29 16:53:30 +0100185 sring_x86_32 = (struct blkif_x86_32_sring *)blkif->blk_ring;
Bob Liu86839c52015-06-03 13:40:03 +0800186 BACK_RING_INIT(&blkif->blk_rings.x86_32, sring_x86_32, PAGE_SIZE * nr_grefs);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400187 break;
188 }
189 case BLKIF_PROTOCOL_X86_64:
190 {
191 struct blkif_x86_64_sring *sring_x86_64;
David Vrabel2d073842011-09-29 16:53:30 +0100192 sring_x86_64 = (struct blkif_x86_64_sring *)blkif->blk_ring;
Bob Liu86839c52015-06-03 13:40:03 +0800193 BACK_RING_INIT(&blkif->blk_rings.x86_64, sring_x86_64, PAGE_SIZE * nr_grefs);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400194 break;
195 }
196 default:
197 BUG();
198 }
199
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400200 err = bind_interdomain_evtchn_to_irqhandler(blkif->domid, evtchn,
201 xen_blkif_be_int, 0,
202 "blkif-backend", blkif);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400203 if (err < 0) {
David Vrabel2d073842011-09-29 16:53:30 +0100204 xenbus_unmap_ring_vfree(blkif->be->dev, blkif->blk_ring);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400205 blkif->blk_rings.common.sring = NULL;
206 return err;
207 }
208 blkif->irq = err;
209
210 return 0;
211}
212
Valentin Priescu814d04e2014-05-20 22:28:50 +0200213static int xen_blkif_disconnect(struct xen_blkif *blkif)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400214{
215 if (blkif->xenblkd) {
216 kthread_stop(blkif->xenblkd);
Konrad Rzeszutek Wilk8e3f8752013-01-23 16:54:32 -0500217 wake_up(&blkif->shutdown_wq);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400218 blkif->xenblkd = NULL;
219 }
220
Valentin Priescu814d04e2014-05-20 22:28:50 +0200221 /* The above kthread_stop() guarantees that at this point we
222 * don't have any discard_io or other_io requests. So, checking
223 * for inflight IO is enough.
224 */
225 if (atomic_read(&blkif->inflight) > 0)
226 return -EBUSY;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400227
228 if (blkif->irq) {
229 unbind_from_irqhandler(blkif->irq, blkif);
230 blkif->irq = 0;
231 }
232
233 if (blkif->blk_rings.common.sring) {
David Vrabel2d073842011-09-29 16:53:30 +0100234 xenbus_unmap_ring_vfree(blkif->be->dev, blkif->blk_ring);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400235 blkif->blk_rings.common.sring = NULL;
236 }
Valentin Priescu814d04e2014-05-20 22:28:50 +0200237
Vitaly Kuznetsov12ea7292014-09-08 15:21:33 +0200238 /* Remove all persistent grants and the cache of ballooned pages. */
239 xen_blkbk_free_caches(blkif);
240
Valentin Priescu814d04e2014-05-20 22:28:50 +0200241 return 0;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400242}
243
Konrad Rzeszutek Wilk29117582012-08-13 10:53:17 -0400244static void xen_blkif_free(struct xen_blkif *blkif)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400245{
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200246 struct pending_req *req, *n;
247 int i = 0, j;
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200248
Valentin Priescu814d04e2014-05-20 22:28:50 +0200249 xen_blkif_disconnect(blkif);
250 xen_vbd_free(&blkif->vbd);
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200251
Roger Pau Monneef753412014-02-04 11:26:13 +0100252 /* Make sure everything is drained before shutting down */
253 BUG_ON(blkif->persistent_gnt_c != 0);
254 BUG_ON(atomic_read(&blkif->persistent_gnt_in_use) != 0);
255 BUG_ON(blkif->free_pages_num != 0);
256 BUG_ON(!list_empty(&blkif->persistent_purge_list));
257 BUG_ON(!list_empty(&blkif->free_pages));
258 BUG_ON(!RB_EMPTY_ROOT(&blkif->persistent_gnts));
259
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200260 /* Check that there is no request in use */
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200261 list_for_each_entry_safe(req, n, &blkif->pending_free, free_list) {
262 list_del(&req->free_list);
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200263
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200264 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++)
265 kfree(req->segments[j]);
266
267 for (j = 0; j < MAX_INDIRECT_PAGES; j++)
268 kfree(req->indirect_pages[j]);
269
270 kfree(req);
271 i++;
272 }
273
Bob Liu86839c52015-06-03 13:40:03 +0800274 WARN_ON(i != (XEN_BLKIF_REQS_PER_PAGE * blkif->nr_ring_pages));
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200275
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400276 kmem_cache_free(xen_blkif_cachep, blkif);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400277}
278
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400279int __init xen_blkif_interface_init(void)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400280{
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400281 xen_blkif_cachep = kmem_cache_create("blkif_cache",
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400282 sizeof(struct xen_blkif),
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400283 0, 0, NULL);
284 if (!xen_blkif_cachep)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400285 return -ENOMEM;
286
287 return 0;
288}
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400289
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400290/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400291 * sysfs interface for VBD I/O requests
292 */
293
294#define VBD_SHOW(name, format, args...) \
295 static ssize_t show_##name(struct device *_dev, \
296 struct device_attribute *attr, \
297 char *buf) \
298 { \
299 struct xenbus_device *dev = to_xenbus_device(_dev); \
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800300 struct backend_info *be = dev_get_drvdata(&dev->dev); \
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400301 \
302 return sprintf(buf, format, ##args); \
303 } \
304 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
305
Zoltan Kiss986cacb2013-03-11 16:15:50 +0000306VBD_SHOW(oo_req, "%llu\n", be->blkif->st_oo_req);
307VBD_SHOW(rd_req, "%llu\n", be->blkif->st_rd_req);
308VBD_SHOW(wr_req, "%llu\n", be->blkif->st_wr_req);
309VBD_SHOW(f_req, "%llu\n", be->blkif->st_f_req);
310VBD_SHOW(ds_req, "%llu\n", be->blkif->st_ds_req);
311VBD_SHOW(rd_sect, "%llu\n", be->blkif->st_rd_sect);
312VBD_SHOW(wr_sect, "%llu\n", be->blkif->st_wr_sect);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400313
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400314static struct attribute *xen_vbdstat_attrs[] = {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400315 &dev_attr_oo_req.attr,
316 &dev_attr_rd_req.attr,
317 &dev_attr_wr_req.attr,
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400318 &dev_attr_f_req.attr,
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800319 &dev_attr_ds_req.attr,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400320 &dev_attr_rd_sect.attr,
321 &dev_attr_wr_sect.attr,
322 NULL
323};
324
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400325static struct attribute_group xen_vbdstat_group = {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400326 .name = "statistics",
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400327 .attrs = xen_vbdstat_attrs,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400328};
329
330VBD_SHOW(physical_device, "%x:%x\n", be->major, be->minor);
331VBD_SHOW(mode, "%s\n", be->mode);
332
Konrad Rzeszutek Wilk29117582012-08-13 10:53:17 -0400333static int xenvbd_sysfs_addif(struct xenbus_device *dev)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400334{
335 int error;
336
337 error = device_create_file(&dev->dev, &dev_attr_physical_device);
Konrad Rzeszutek Wilkd6091b22011-04-14 17:33:30 -0400338 if (error)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400339 goto fail1;
340
341 error = device_create_file(&dev->dev, &dev_attr_mode);
342 if (error)
343 goto fail2;
344
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400345 error = sysfs_create_group(&dev->dev.kobj, &xen_vbdstat_group);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400346 if (error)
347 goto fail3;
348
349 return 0;
350
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400351fail3: sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400352fail2: device_remove_file(&dev->dev, &dev_attr_mode);
353fail1: device_remove_file(&dev->dev, &dev_attr_physical_device);
354 return error;
355}
356
Konrad Rzeszutek Wilk29117582012-08-13 10:53:17 -0400357static void xenvbd_sysfs_delif(struct xenbus_device *dev)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400358{
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400359 sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400360 device_remove_file(&dev->dev, &dev_attr_mode);
361 device_remove_file(&dev->dev, &dev_attr_physical_device);
362}
363
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400364
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400365static void xen_vbd_free(struct xen_vbd *vbd)
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400366{
367 if (vbd->bdev)
368 blkdev_put(vbd->bdev, vbd->readonly ? FMODE_READ : FMODE_WRITE);
369 vbd->bdev = NULL;
370}
371
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400372static int xen_vbd_create(struct xen_blkif *blkif, blkif_vdev_t handle,
373 unsigned major, unsigned minor, int readonly,
374 int cdrom)
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400375{
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400376 struct xen_vbd *vbd;
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400377 struct block_device *bdev;
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400378 struct request_queue *q;
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400379
380 vbd = &blkif->vbd;
381 vbd->handle = handle;
382 vbd->readonly = readonly;
383 vbd->type = 0;
384
385 vbd->pdevice = MKDEV(major, minor);
386
387 bdev = blkdev_get_by_dev(vbd->pdevice, vbd->readonly ?
388 FMODE_READ : FMODE_WRITE, NULL);
389
390 if (IS_ERR(bdev)) {
Tao Chen77387b82015-04-01 15:04:22 +0000391 pr_warn("xen_vbd_create: device %08x could not be opened\n",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400392 vbd->pdevice);
393 return -ENOENT;
394 }
395
396 vbd->bdev = bdev;
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400397 if (vbd->bdev->bd_disk == NULL) {
Tao Chen77387b82015-04-01 15:04:22 +0000398 pr_warn("xen_vbd_create: device %08x doesn't exist\n",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400399 vbd->pdevice);
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400400 xen_vbd_free(vbd);
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400401 return -ENOENT;
402 }
Laszlo Ersek64649202011-05-25 12:24:25 +0200403 vbd->size = vbd_sz(vbd);
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400404
405 if (vbd->bdev->bd_disk->flags & GENHD_FL_CD || cdrom)
406 vbd->type |= VDISK_CDROM;
407 if (vbd->bdev->bd_disk->flags & GENHD_FL_REMOVABLE)
408 vbd->type |= VDISK_REMOVABLE;
409
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400410 q = bdev_get_queue(bdev);
411 if (q && q->flush_flags)
412 vbd->flush_support = true;
413
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400414 if (q && blk_queue_secdiscard(q))
415 vbd->discard_secure = true;
416
Tao Chen77387b82015-04-01 15:04:22 +0000417 pr_debug("Successful creation of handle=%04x (dom=%u)\n",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400418 handle, blkif->domid);
419 return 0;
420}
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400421static int xen_blkbk_remove(struct xenbus_device *dev)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400422{
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800423 struct backend_info *be = dev_get_drvdata(&dev->dev);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400424
Tao Chen77387b82015-04-01 15:04:22 +0000425 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400426
427 if (be->major || be->minor)
428 xenvbd_sysfs_delif(dev);
429
430 if (be->backend_watch.node) {
431 unregister_xenbus_watch(&be->backend_watch);
432 kfree(be->backend_watch.node);
433 be->backend_watch.node = NULL;
434 }
435
Valentin Priescu814d04e2014-05-20 22:28:50 +0200436 dev_set_drvdata(&dev->dev, NULL);
437
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400438 if (be->blkif) {
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400439 xen_blkif_disconnect(be->blkif);
Valentin Priescu814d04e2014-05-20 22:28:50 +0200440 xen_blkif_put(be->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400441 }
442
Jan Beulich9d092602012-12-20 10:31:11 +0000443 kfree(be->mode);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400444 kfree(be);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400445 return 0;
446}
447
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400448int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt,
449 struct backend_info *be, int state)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400450{
451 struct xenbus_device *dev = be->dev;
452 int err;
453
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400454 err = xenbus_printf(xbt, dev->nodename, "feature-flush-cache",
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400455 "%d", state);
456 if (err)
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400457 dev_warn(&dev->dev, "writing feature-flush-cache (%d)", err);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400458
459 return err;
460}
461
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400462static void xen_blkbk_discard(struct xenbus_transaction xbt, struct backend_info *be)
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800463{
464 struct xenbus_device *dev = be->dev;
465 struct xen_blkif *blkif = be->blkif;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800466 int err;
Olaf Heringc926b702014-05-21 16:32:42 +0200467 int state = 0, discard_enable;
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400468 struct block_device *bdev = be->blkif->vbd.bdev;
469 struct request_queue *q = bdev_get_queue(bdev);
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800470
Olaf Heringc926b702014-05-21 16:32:42 +0200471 err = xenbus_scanf(XBT_NIL, dev->nodename, "discard-enable", "%d",
472 &discard_enable);
473 if (err == 1 && !discard_enable)
474 return;
475
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400476 if (blk_queue_discard(q)) {
477 err = xenbus_printf(xbt, dev->nodename,
478 "discard-granularity", "%u",
479 q->limits.discard_granularity);
480 if (err) {
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400481 dev_warn(&dev->dev, "writing discard-granularity (%d)", err);
482 return;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800483 }
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400484 err = xenbus_printf(xbt, dev->nodename,
485 "discard-alignment", "%u",
486 q->limits.discard_alignment);
487 if (err) {
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400488 dev_warn(&dev->dev, "writing discard-alignment (%d)", err);
489 return;
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400490 }
491 state = 1;
492 /* Optional. */
493 err = xenbus_printf(xbt, dev->nodename,
494 "discard-secure", "%d",
495 blkif->vbd.discard_secure);
496 if (err) {
Konrad Rzeszutek Wilka71e23d2012-04-16 21:55:04 -0400497 dev_warn(&dev->dev, "writing discard-secure (%d)", err);
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400498 return;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800499 }
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800500 }
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800501 err = xenbus_printf(xbt, dev->nodename, "feature-discard",
502 "%d", state);
503 if (err)
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400504 dev_warn(&dev->dev, "writing feature-discard (%d)", err);
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800505}
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400506int xen_blkbk_barrier(struct xenbus_transaction xbt,
507 struct backend_info *be, int state)
508{
509 struct xenbus_device *dev = be->dev;
510 int err;
511
512 err = xenbus_printf(xbt, dev->nodename, "feature-barrier",
513 "%d", state);
514 if (err)
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400515 dev_warn(&dev->dev, "writing feature-barrier (%d)", err);
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400516
517 return err;
518}
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800519
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400520/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400521 * Entry point to this code when a new device is created. Allocate the basic
522 * structures, and watch the store waiting for the hotplug scripts to tell us
523 * the device's physical major and minor numbers. Switch to InitWait.
524 */
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400525static int xen_blkbk_probe(struct xenbus_device *dev,
526 const struct xenbus_device_id *id)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400527{
528 int err;
529 struct backend_info *be = kzalloc(sizeof(struct backend_info),
530 GFP_KERNEL);
Tao Chen77387b82015-04-01 15:04:22 +0000531
532 /* match the pr_debug in xen_blkbk_remove */
533 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
534
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400535 if (!be) {
536 xenbus_dev_fatal(dev, -ENOMEM,
537 "allocating backend structure");
538 return -ENOMEM;
539 }
540 be->dev = dev;
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800541 dev_set_drvdata(&dev->dev, be);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400542
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400543 be->blkif = xen_blkif_alloc(dev->otherend_id);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400544 if (IS_ERR(be->blkif)) {
545 err = PTR_ERR(be->blkif);
546 be->blkif = NULL;
547 xenbus_dev_fatal(dev, err, "creating block interface");
548 goto fail;
549 }
550
551 /* setup back pointer */
552 be->blkif->be = be;
553
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -0800554 err = xenbus_watch_pathfmt(dev, &be->backend_watch, backend_changed,
555 "%s/%s", dev->nodename, "physical-device");
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400556 if (err)
557 goto fail;
558
Bob Liu86839c52015-06-03 13:40:03 +0800559 err = xenbus_printf(XBT_NIL, dev->nodename, "max-ring-page-order", "%u",
560 xen_blkif_max_ring_order);
561 if (err)
562 pr_warn("%s write out 'max-ring-page-order' failed\n", __func__);
563
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400564 err = xenbus_switch_state(dev, XenbusStateInitWait);
565 if (err)
566 goto fail;
567
568 return 0;
569
570fail:
Tao Chen77387b82015-04-01 15:04:22 +0000571 pr_warn("%s failed\n", __func__);
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400572 xen_blkbk_remove(dev);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400573 return err;
574}
575
576
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400577/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400578 * Callback received when the hotplug scripts have placed the physical-device
579 * node. Read it and the mode node, and create a vbd. If the frontend is
580 * ready, connect.
581 */
582static void backend_changed(struct xenbus_watch *watch,
583 const char **vec, unsigned int len)
584{
585 int err;
586 unsigned major;
587 unsigned minor;
588 struct backend_info *be
589 = container_of(watch, struct backend_info, backend_watch);
590 struct xenbus_device *dev = be->dev;
591 int cdrom = 0;
Jan Beulich9d092602012-12-20 10:31:11 +0000592 unsigned long handle;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400593 char *device_type;
594
Tao Chen77387b82015-04-01 15:04:22 +0000595 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400596
597 err = xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x:%x",
598 &major, &minor);
599 if (XENBUS_EXIST_ERR(err)) {
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400600 /*
601 * Since this watch will fire once immediately after it is
602 * registered, we expect this. Ignore it, and wait for the
603 * hotplug scripts.
604 */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400605 return;
606 }
607 if (err != 2) {
608 xenbus_dev_fatal(dev, err, "reading physical-device");
609 return;
610 }
611
Jan Beulich9d092602012-12-20 10:31:11 +0000612 if (be->major | be->minor) {
613 if (be->major != major || be->minor != minor)
Tao Chen77387b82015-04-01 15:04:22 +0000614 pr_warn("changing physical device (from %x:%x to %x:%x) not supported.\n",
Jan Beulich9d092602012-12-20 10:31:11 +0000615 be->major, be->minor, major, minor);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400616 return;
617 }
618
619 be->mode = xenbus_read(XBT_NIL, dev->nodename, "mode", NULL);
620 if (IS_ERR(be->mode)) {
621 err = PTR_ERR(be->mode);
622 be->mode = NULL;
623 xenbus_dev_fatal(dev, err, "reading mode");
624 return;
625 }
626
627 device_type = xenbus_read(XBT_NIL, dev->otherend, "device-type", NULL);
628 if (!IS_ERR(device_type)) {
629 cdrom = strcmp(device_type, "cdrom") == 0;
630 kfree(device_type);
631 }
632
Jan Beulich9d092602012-12-20 10:31:11 +0000633 /* Front end dir is a number, which is used as the handle. */
Jingoo Hanbb8e0e82013-09-11 14:20:07 -0700634 err = kstrtoul(strrchr(dev->otherend, '/') + 1, 0, &handle);
Jan Beulich9d092602012-12-20 10:31:11 +0000635 if (err)
636 return;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400637
Jan Beulich9d092602012-12-20 10:31:11 +0000638 be->major = major;
639 be->minor = minor;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400640
Jan Beulich9d092602012-12-20 10:31:11 +0000641 err = xen_vbd_create(be->blkif, handle, major, minor,
642 !strchr(be->mode, 'w'), cdrom);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400643
Jan Beulich9d092602012-12-20 10:31:11 +0000644 if (err)
645 xenbus_dev_fatal(dev, err, "creating vbd structure");
646 else {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400647 err = xenvbd_sysfs_addif(dev);
648 if (err) {
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400649 xen_vbd_free(&be->blkif->vbd);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400650 xenbus_dev_fatal(dev, err, "creating sysfs entries");
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400651 }
Jan Beulich9d092602012-12-20 10:31:11 +0000652 }
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400653
Jan Beulich9d092602012-12-20 10:31:11 +0000654 if (err) {
655 kfree(be->mode);
656 be->mode = NULL;
657 be->major = 0;
658 be->minor = 0;
659 } else {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400660 /* We're potentially connected now */
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400661 xen_update_blkif_status(be->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400662 }
663}
664
665
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400666/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400667 * Callback received when the frontend's state changes.
668 */
669static void frontend_changed(struct xenbus_device *dev,
670 enum xenbus_state frontend_state)
671{
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800672 struct backend_info *be = dev_get_drvdata(&dev->dev);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400673 int err;
674
Tao Chen77387b82015-04-01 15:04:22 +0000675 pr_debug("%s %p %s\n", __func__, dev, xenbus_strstate(frontend_state));
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400676
677 switch (frontend_state) {
678 case XenbusStateInitialising:
679 if (dev->state == XenbusStateClosed) {
Tao Chen77387b82015-04-01 15:04:22 +0000680 pr_info("%s: prepare for reconnect\n", dev->nodename);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400681 xenbus_switch_state(dev, XenbusStateInitWait);
682 }
683 break;
684
685 case XenbusStateInitialised:
686 case XenbusStateConnected:
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400687 /*
688 * Ensure we connect even when two watches fire in
Justin P. Mattock42b2aa82011-11-28 20:31:00 -0800689 * close succession and we miss the intermediate value
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400690 * of frontend_state.
691 */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400692 if (dev->state == XenbusStateConnected)
693 break;
694
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400695 /*
696 * Enforce precondition before potential leak point.
Joe Jin1bc05b02011-08-15 12:57:07 +0800697 * xen_blkif_disconnect() is idempotent.
Keir Fraser313d7b02010-11-24 22:08:20 -0800698 */
Valentin Priescu814d04e2014-05-20 22:28:50 +0200699 err = xen_blkif_disconnect(be->blkif);
700 if (err) {
701 xenbus_dev_fatal(dev, err, "pending I/O");
702 break;
703 }
Keir Fraser313d7b02010-11-24 22:08:20 -0800704
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400705 err = connect_ring(be);
706 if (err)
707 break;
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400708 xen_update_blkif_status(be->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400709 break;
710
711 case XenbusStateClosing:
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400712 xenbus_switch_state(dev, XenbusStateClosing);
713 break;
714
715 case XenbusStateClosed:
Joe Jin6f5986b2011-08-15 12:51:31 +0800716 xen_blkif_disconnect(be->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400717 xenbus_switch_state(dev, XenbusStateClosed);
718 if (xenbus_dev_is_online(dev))
719 break;
720 /* fall through if not online */
721 case XenbusStateUnknown:
Joe Jin1bc05b02011-08-15 12:57:07 +0800722 /* implies xen_blkif_disconnect() via xen_blkbk_remove() */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400723 device_unregister(&dev->dev);
724 break;
725
726 default:
727 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
728 frontend_state);
729 break;
730 }
731}
732
733
734/* ** Connection ** */
735
736
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400737/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400738 * Write the physical details regarding the block device to the store, and
739 * switch to Connected state.
740 */
741static void connect(struct backend_info *be)
742{
743 struct xenbus_transaction xbt;
744 int err;
745 struct xenbus_device *dev = be->dev;
746
Tao Chen77387b82015-04-01 15:04:22 +0000747 pr_debug("%s %s\n", __func__, dev->otherend);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400748
749 /* Supply the information about the device the frontend needs */
750again:
751 err = xenbus_transaction_start(&xbt);
752 if (err) {
753 xenbus_dev_fatal(dev, err, "starting transaction");
754 return;
755 }
756
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400757 /* If we can't advertise it is OK. */
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400758 xen_blkbk_flush_diskcache(xbt, be, be->blkif->vbd.flush_support);
759
760 xen_blkbk_discard(xbt, be);
761
762 xen_blkbk_barrier(xbt, be, be->blkif->vbd.flush_support);
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400763
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200764 err = xenbus_printf(xbt, dev->nodename, "feature-persistent", "%u", 1);
765 if (err) {
766 xenbus_dev_fatal(dev, err, "writing %s/feature-persistent",
767 dev->nodename);
768 goto abort;
769 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200770 err = xenbus_printf(xbt, dev->nodename, "feature-max-indirect-segments", "%u",
771 MAX_INDIRECT_SEGMENTS);
772 if (err)
773 dev_warn(&dev->dev, "writing %s/feature-max-indirect-segments (%d)",
774 dev->nodename, err);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200775
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400776 err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400777 (unsigned long long)vbd_sz(&be->blkif->vbd));
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400778 if (err) {
779 xenbus_dev_fatal(dev, err, "writing %s/sectors",
780 dev->nodename);
781 goto abort;
782 }
783
784 /* FIXME: use a typename instead */
785 err = xenbus_printf(xbt, dev->nodename, "info", "%u",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400786 be->blkif->vbd.type |
787 (be->blkif->vbd.readonly ? VDISK_READONLY : 0));
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400788 if (err) {
789 xenbus_dev_fatal(dev, err, "writing %s/info",
790 dev->nodename);
791 goto abort;
792 }
793 err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400794 (unsigned long)
795 bdev_logical_block_size(be->blkif->vbd.bdev));
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400796 if (err) {
797 xenbus_dev_fatal(dev, err, "writing %s/sector-size",
798 dev->nodename);
799 goto abort;
800 }
Stefan Bader7c4d7d72013-05-13 16:28:15 +0200801 err = xenbus_printf(xbt, dev->nodename, "physical-sector-size", "%u",
802 bdev_physical_block_size(be->blkif->vbd.bdev));
803 if (err)
804 xenbus_dev_error(dev, err, "writing %s/physical-sector-size",
805 dev->nodename);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400806
807 err = xenbus_transaction_end(xbt, 0);
808 if (err == -EAGAIN)
809 goto again;
810 if (err)
811 xenbus_dev_fatal(dev, err, "ending transaction");
812
813 err = xenbus_switch_state(dev, XenbusStateConnected);
814 if (err)
Joe Perches08b8bfc2011-06-12 09:21:13 -0700815 xenbus_dev_fatal(dev, err, "%s: switching to Connected state",
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400816 dev->nodename);
817
818 return;
819 abort:
820 xenbus_transaction_end(xbt, 1);
821}
822
823
824static int connect_ring(struct backend_info *be)
825{
826 struct xenbus_device *dev = be->dev;
Bob Liu86839c52015-06-03 13:40:03 +0800827 unsigned int ring_ref[XENBUS_MAX_RING_PAGES];
828 unsigned int evtchn, nr_grefs, ring_page_order;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200829 unsigned int pers_grants;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400830 char protocol[64] = "";
Bob Liu69b91ed2015-06-03 13:40:01 +0800831 struct pending_req *req, *n;
832 int err, i, j;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400833
Tao Chen77387b82015-04-01 15:04:22 +0000834 pr_debug("%s %s\n", __func__, dev->otherend);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400835
Bob Liu86839c52015-06-03 13:40:03 +0800836 err = xenbus_scanf(XBT_NIL, dev->otherend, "event-channel", "%u",
837 &evtchn);
838 if (err != 1) {
839 err = -EINVAL;
840 xenbus_dev_fatal(dev, err, "reading %s/event-channel",
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400841 dev->otherend);
842 return err;
843 }
Bob Liu86839c52015-06-03 13:40:03 +0800844 pr_info("event-channel %u\n", evtchn);
845
846 err = xenbus_scanf(XBT_NIL, dev->otherend, "ring-page-order", "%u",
847 &ring_page_order);
848 if (err != 1) {
849 err = xenbus_scanf(XBT_NIL, dev->otherend, "ring-ref",
850 "%u", &ring_ref[0]);
851 if (err != 1) {
852 err = -EINVAL;
853 xenbus_dev_fatal(dev, err, "reading %s/ring-ref",
854 dev->otherend);
855 return err;
856 }
857 nr_grefs = 1;
858 pr_info("%s:using single page: ring-ref %d\n", dev->otherend,
859 ring_ref[0]);
860 } else {
861 unsigned int i;
862
863 if (ring_page_order > xen_blkif_max_ring_order) {
864 err = -EINVAL;
865 xenbus_dev_fatal(dev, err, "%s/request %d ring page order exceed max:%d",
866 dev->otherend, ring_page_order,
867 xen_blkif_max_ring_order);
868 return err;
869 }
870
871 nr_grefs = 1 << ring_page_order;
872 for (i = 0; i < nr_grefs; i++) {
873 char ring_ref_name[RINGREF_NAME_LEN];
874
875 snprintf(ring_ref_name, RINGREF_NAME_LEN, "ring-ref%u", i);
876 err = xenbus_scanf(XBT_NIL, dev->otherend, ring_ref_name,
877 "%u", &ring_ref[i]);
878 if (err != 1) {
879 err = -EINVAL;
880 xenbus_dev_fatal(dev, err, "reading %s/%s",
881 dev->otherend, ring_ref_name);
882 return err;
883 }
884 pr_info("ring-ref%u: %u\n", i, ring_ref[i]);
885 }
886 }
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400887
David Vrabelb042a3c2015-02-05 17:09:56 +0000888 be->blkif->blk_protocol = BLKIF_PROTOCOL_DEFAULT;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400889 err = xenbus_gather(XBT_NIL, dev->otherend, "protocol",
890 "%63s", protocol, NULL);
891 if (err)
David Vrabelb042a3c2015-02-05 17:09:56 +0000892 strcpy(protocol, "unspecified, assuming default");
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400893 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_NATIVE))
894 be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
895 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_32))
896 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_32;
897 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_64))
898 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_64;
899 else {
900 xenbus_dev_fatal(dev, err, "unknown fe protocol %s", protocol);
901 return -1;
902 }
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200903 err = xenbus_gather(XBT_NIL, dev->otherend,
Roger Pau Monnecb5bd4d2012-11-02 16:43:04 +0100904 "feature-persistent", "%u",
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200905 &pers_grants, NULL);
906 if (err)
907 pers_grants = 0;
908
909 be->blkif->vbd.feature_gnt_persistent = pers_grants;
910 be->blkif->vbd.overflow_max_grants = 0;
Bob Liu86839c52015-06-03 13:40:03 +0800911 be->blkif->nr_ring_pages = nr_grefs;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200912
Bob Liu86839c52015-06-03 13:40:03 +0800913 pr_info("ring-pages:%d, event-channel %d, protocol %d (%s) %s\n",
914 nr_grefs, evtchn, be->blkif->blk_protocol, protocol,
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200915 pers_grants ? "persistent grants" : "");
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400916
Bob Liu86839c52015-06-03 13:40:03 +0800917 for (i = 0; i < nr_grefs * XEN_BLKIF_REQS_PER_PAGE; i++) {
Bob Liu69b91ed2015-06-03 13:40:01 +0800918 req = kzalloc(sizeof(*req), GFP_KERNEL);
919 if (!req)
920 goto fail;
921 list_add_tail(&req->free_list, &be->blkif->pending_free);
922 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++) {
923 req->segments[j] = kzalloc(sizeof(*req->segments[0]), GFP_KERNEL);
924 if (!req->segments[j])
925 goto fail;
926 }
927 for (j = 0; j < MAX_INDIRECT_PAGES; j++) {
928 req->indirect_pages[j] = kzalloc(sizeof(*req->indirect_pages[0]),
929 GFP_KERNEL);
930 if (!req->indirect_pages[j])
931 goto fail;
932 }
933 }
934
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400935 /* Map the shared frame, irq etc. */
Bob Liu86839c52015-06-03 13:40:03 +0800936 err = xen_blkif_map(be->blkif, ring_ref, nr_grefs, evtchn);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400937 if (err) {
Bob Liu86839c52015-06-03 13:40:03 +0800938 xenbus_dev_fatal(dev, err, "mapping ring-ref port %u", evtchn);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400939 return err;
940 }
941
942 return 0;
Bob Liu69b91ed2015-06-03 13:40:01 +0800943
944fail:
945 list_for_each_entry_safe(req, n, &be->blkif->pending_free, free_list) {
946 list_del(&req->free_list);
947 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++) {
948 if (!req->segments[j])
949 break;
950 kfree(req->segments[j]);
951 }
952 for (j = 0; j < MAX_INDIRECT_PAGES; j++) {
953 if (!req->indirect_pages[j])
954 break;
955 kfree(req->indirect_pages[j]);
956 }
957 kfree(req);
958 }
959 return -ENOMEM;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400960}
961
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400962static const struct xenbus_device_id xen_blkbk_ids[] = {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400963 { "vbd" },
964 { "" }
965};
966
David Vrabel95afae42014-09-08 17:30:41 +0100967static struct xenbus_driver xen_blkbk_driver = {
968 .ids = xen_blkbk_ids,
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400969 .probe = xen_blkbk_probe,
970 .remove = xen_blkbk_remove,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400971 .otherend_changed = frontend_changed
David Vrabel95afae42014-09-08 17:30:41 +0100972};
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400973
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400974int xen_blkif_xenbus_init(void)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400975{
Jan Beulich73db1442011-12-22 09:08:13 +0000976 return xenbus_register_backend(&xen_blkbk_driver);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400977}