blob: 3cc6d1d86f1efc038f451dab3bb3452bed7de92b [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
Konrad Rzeszutek Wilkfa3184b2016-02-03 16:40:05 -050026/* On the XenBus the max length of 'ring-ref%u'. */
Bob Liu86839c52015-06-03 13:40:03 +080027#define RINGREF_NAME_LEN (20)
Tao Chen13755902015-03-27 13:15:54 +000028
Konrad Rzeszutek Wilkd6091b22011-04-14 17:33:30 -040029struct backend_info {
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -040030 struct xenbus_device *dev;
Konrad Rzeszutek Wilk51854322011-05-12 18:02:28 -040031 struct xen_blkif *blkif;
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -040032 struct xenbus_watch backend_watch;
33 unsigned major;
34 unsigned minor;
35 char *mode;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040036};
37
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -040038static struct kmem_cache *xen_blkif_cachep;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040039static void connect(struct backend_info *);
40static int connect_ring(struct backend_info *);
41static void backend_changed(struct xenbus_watch *, const char **,
42 unsigned int);
Valentin Priescu814d04e2014-05-20 22:28:50 +020043static void xen_blkif_free(struct xen_blkif *blkif);
44static void xen_vbd_free(struct xen_vbd *vbd);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040045
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -040046struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be)
Jeremy Fitzhardinge98e036a2010-03-18 15:35:05 -070047{
48 return be->dev;
49}
50
Valentin Priescu814d04e2014-05-20 22:28:50 +020051/*
52 * The last request could free the device from softirq context and
53 * xen_blkif_free() can sleep.
54 */
55static 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
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -040063static int blkback_name(struct xen_blkif *blkif, char *buf)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040064{
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
Konrad Rzeszutek Wilkd6091b22011-04-14 17:33:30 -040072 devname = strstr(devpath, "/dev/");
73 if (devname != NULL)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040074 devname += strlen("/dev/");
75 else
76 devname = devpath;
77
Konrad Rzeszutek Wilkfa3184b2016-02-03 16:40:05 -050078 snprintf(buf, TASK_COMM_LEN, "%d.%s", blkif->domid, devname);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040079 kfree(devpath);
80
81 return 0;
82}
83
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -040084static void xen_update_blkif_status(struct xen_blkif *blkif)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040085{
86 int err;
Konrad Rzeszutek Wilkfa3184b2016-02-03 16:40:05 -050087 char name[TASK_COMM_LEN];
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -050088 struct xen_blkif_ring *ring;
89 int i;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040090
91 /* Not ready to connect? */
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -050092 if (!blkif->rings || !blkif->rings[0].irq || !blkif->vbd.bdev)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040093 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
Chris Lalancettecbf46292010-07-21 12:41:45 -0700110 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
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500117 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 }
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400127 }
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500128 return;
129
130out:
131 while (--i >= 0) {
132 ring = &blkif->rings[i];
133 kthread_stop(ring->xenblkd);
134 }
135 return;
136}
137
138static 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);
Bob Liud4bf0062015-11-14 11:12:19 +0800152 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);
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500156
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;
Bob Liudb6fbc12015-12-09 07:44:02 +0800161 ring->st_print = jiffies;
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500162 xen_blkif_get(blkif);
163 }
164
165 return 0;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400166}
167
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400168static struct xen_blkif *xen_blkif_alloc(domid_t domid)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400169{
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400170 struct xen_blkif *blkif;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400171
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200172 BUILD_BUG_ON(MAX_INDIRECT_PAGES > BLKIF_MAX_INDIRECT_PAGES_PER_REQUEST);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400173
Wei Yongjun654dbef2012-08-27 12:28:57 +0800174 blkif = kmem_cache_zalloc(xen_blkif_cachep, GFP_KERNEL);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400175 if (!blkif)
176 return ERR_PTR(-ENOMEM);
177
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400178 blkif->domid = domid;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400179 atomic_set(&blkif->refcnt, 1);
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400180 init_completion(&blkif->drain_complete);
Bob Liu59795702015-11-14 11:12:15 +0800181 INIT_WORK(&blkif->free_work, xen_blkif_deferred_free);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400182
183 return blkif;
184}
185
Bob Liu59795702015-11-14 11:12:15 +0800186static int xen_blkif_map(struct xen_blkif_ring *ring, grant_ref_t *gref,
Bob Liu86839c52015-06-03 13:40:03 +0800187 unsigned int nr_grefs, unsigned int evtchn)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400188{
189 int err;
Bob Liu59795702015-11-14 11:12:15 +0800190 struct xen_blkif *blkif = ring->blkif;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400191
192 /* Already connected through? */
Bob Liu59795702015-11-14 11:12:15 +0800193 if (ring->irq)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400194 return 0;
195
Bob Liu86839c52015-06-03 13:40:03 +0800196 err = xenbus_map_ring_valloc(blkif->be->dev, gref, nr_grefs,
Bob Liu59795702015-11-14 11:12:15 +0800197 &ring->blk_ring);
David Vrabel2d073842011-09-29 16:53:30 +0100198 if (err < 0)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400199 return err;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400200
201 switch (blkif->blk_protocol) {
202 case BLKIF_PROTOCOL_NATIVE:
203 {
204 struct blkif_sring *sring;
Bob Liu59795702015-11-14 11:12:15 +0800205 sring = (struct blkif_sring *)ring->blk_ring;
206 BACK_RING_INIT(&ring->blk_rings.native, sring,
Julien Grall67de5df2015-05-05 16:25:56 +0100207 XEN_PAGE_SIZE * nr_grefs);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400208 break;
209 }
210 case BLKIF_PROTOCOL_X86_32:
211 {
212 struct blkif_x86_32_sring *sring_x86_32;
Bob Liu59795702015-11-14 11:12:15 +0800213 sring_x86_32 = (struct blkif_x86_32_sring *)ring->blk_ring;
214 BACK_RING_INIT(&ring->blk_rings.x86_32, sring_x86_32,
Julien Grall67de5df2015-05-05 16:25:56 +0100215 XEN_PAGE_SIZE * nr_grefs);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400216 break;
217 }
218 case BLKIF_PROTOCOL_X86_64:
219 {
220 struct blkif_x86_64_sring *sring_x86_64;
Bob Liu59795702015-11-14 11:12:15 +0800221 sring_x86_64 = (struct blkif_x86_64_sring *)ring->blk_ring;
222 BACK_RING_INIT(&ring->blk_rings.x86_64, sring_x86_64,
Julien Grall67de5df2015-05-05 16:25:56 +0100223 XEN_PAGE_SIZE * nr_grefs);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400224 break;
225 }
226 default:
227 BUG();
228 }
229
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400230 err = bind_interdomain_evtchn_to_irqhandler(blkif->domid, evtchn,
231 xen_blkif_be_int, 0,
Bob Liu59795702015-11-14 11:12:15 +0800232 "blkif-backend", ring);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400233 if (err < 0) {
Bob Liu59795702015-11-14 11:12:15 +0800234 xenbus_unmap_ring_vfree(blkif->be->dev, ring->blk_ring);
235 ring->blk_rings.common.sring = NULL;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400236 return err;
237 }
Bob Liu59795702015-11-14 11:12:15 +0800238 ring->irq = err;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400239
240 return 0;
241}
242
Valentin Priescu814d04e2014-05-20 22:28:50 +0200243static int xen_blkif_disconnect(struct xen_blkif *blkif)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400244{
Roger Pau Monnef929d422015-09-04 12:08:07 +0200245 struct pending_req *req, *n;
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500246 unsigned int j, r;
Roger Pau Monnef929d422015-09-04 12:08:07 +0200247
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500248 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->xenblkd) {
253 kthread_stop(ring->xenblkd);
254 wake_up(&ring->shutdown_wq);
255 ring->xenblkd = NULL;
256 }
257
258 /* The above kthread_stop() guarantees that at this point we
259 * don't have any discard_io or other_io requests. So, checking
260 * for inflight IO is enough.
261 */
262 if (atomic_read(&ring->inflight) > 0)
263 return -EBUSY;
264
265 if (ring->irq) {
266 unbind_from_irqhandler(ring->irq, ring);
267 ring->irq = 0;
268 }
269
270 if (ring->blk_rings.common.sring) {
271 xenbus_unmap_ring_vfree(blkif->be->dev, ring->blk_ring);
272 ring->blk_rings.common.sring = NULL;
273 }
274
275 /* Remove all persistent grants and the cache of ballooned pages. */
276 xen_blkbk_free_caches(ring);
277
278 /* Check that there is no request in use */
279 list_for_each_entry_safe(req, n, &ring->pending_free, free_list) {
280 list_del(&req->free_list);
281
282 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++)
283 kfree(req->segments[j]);
284
285 for (j = 0; j < MAX_INDIRECT_PAGES; j++)
286 kfree(req->indirect_pages[j]);
287
288 kfree(req);
289 i++;
290 }
291
Bob Liud4bf0062015-11-14 11:12:19 +0800292 BUG_ON(atomic_read(&ring->persistent_gnt_in_use) != 0);
293 BUG_ON(!list_empty(&ring->persistent_purge_list));
294 BUG_ON(!RB_EMPTY_ROOT(&ring->persistent_gnts));
295 BUG_ON(!list_empty(&ring->free_pages));
296 BUG_ON(ring->free_pages_num != 0);
297 BUG_ON(ring->persistent_gnt_c != 0);
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500298 WARN_ON(i != (XEN_BLKIF_REQS_PER_PAGE * blkif->nr_ring_pages));
Bob Liu93bb2772015-12-10 09:16:48 +0800299 xen_blkif_put(blkif);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400300 }
Roger Pau Monnef929d422015-09-04 12:08:07 +0200301 blkif->nr_ring_pages = 0;
Bob Liu93bb2772015-12-10 09:16:48 +0800302 /*
303 * blkif->rings was allocated in connect_ring, so we should free it in
304 * here.
305 */
306 kfree(blkif->rings);
307 blkif->rings = NULL;
308 blkif->nr_rings = 0;
Roger Pau Monnef929d422015-09-04 12:08:07 +0200309
310 return 0;
311}
312
313static void xen_blkif_free(struct xen_blkif *blkif)
314{
315
316 xen_blkif_disconnect(blkif);
317 xen_vbd_free(&blkif->vbd);
318
319 /* Make sure everything is drained before shutting down */
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400320 kmem_cache_free(xen_blkif_cachep, blkif);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400321}
322
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400323int __init xen_blkif_interface_init(void)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400324{
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400325 xen_blkif_cachep = kmem_cache_create("blkif_cache",
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400326 sizeof(struct xen_blkif),
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400327 0, 0, NULL);
328 if (!xen_blkif_cachep)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400329 return -ENOMEM;
330
331 return 0;
332}
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400333
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400334/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400335 * sysfs interface for VBD I/O requests
336 */
337
Bob Liudb6fbc12015-12-09 07:44:02 +0800338#define VBD_SHOW_ALLRING(name, format) \
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400339 static ssize_t show_##name(struct device *_dev, \
340 struct device_attribute *attr, \
341 char *buf) \
342 { \
343 struct xenbus_device *dev = to_xenbus_device(_dev); \
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800344 struct backend_info *be = dev_get_drvdata(&dev->dev); \
Bob Liudb6fbc12015-12-09 07:44:02 +0800345 struct xen_blkif *blkif = be->blkif; \
346 unsigned int i; \
347 unsigned long long result = 0; \
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400348 \
Bob Liudb6fbc12015-12-09 07:44:02 +0800349 if (!blkif->rings) \
350 goto out; \
351 \
352 for (i = 0; i < blkif->nr_rings; i++) { \
353 struct xen_blkif_ring *ring = &blkif->rings[i]; \
354 \
355 result += ring->st_##name; \
356 } \
357 \
358out: \
359 return sprintf(buf, format, result); \
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400360 } \
361 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
362
Bob Liudb6fbc12015-12-09 07:44:02 +0800363VBD_SHOW_ALLRING(oo_req, "%llu\n");
364VBD_SHOW_ALLRING(rd_req, "%llu\n");
365VBD_SHOW_ALLRING(wr_req, "%llu\n");
366VBD_SHOW_ALLRING(f_req, "%llu\n");
367VBD_SHOW_ALLRING(ds_req, "%llu\n");
368VBD_SHOW_ALLRING(rd_sect, "%llu\n");
369VBD_SHOW_ALLRING(wr_sect, "%llu\n");
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400370
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400371static struct attribute *xen_vbdstat_attrs[] = {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400372 &dev_attr_oo_req.attr,
373 &dev_attr_rd_req.attr,
374 &dev_attr_wr_req.attr,
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400375 &dev_attr_f_req.attr,
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800376 &dev_attr_ds_req.attr,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400377 &dev_attr_rd_sect.attr,
378 &dev_attr_wr_sect.attr,
379 NULL
380};
381
Jan Beulich53043942016-07-07 01:38:58 -0600382static const struct attribute_group xen_vbdstat_group = {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400383 .name = "statistics",
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400384 .attrs = xen_vbdstat_attrs,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400385};
386
Bob Liudb6fbc12015-12-09 07:44:02 +0800387#define VBD_SHOW(name, format, args...) \
388 static ssize_t show_##name(struct device *_dev, \
389 struct device_attribute *attr, \
390 char *buf) \
391 { \
392 struct xenbus_device *dev = to_xenbus_device(_dev); \
393 struct backend_info *be = dev_get_drvdata(&dev->dev); \
394 \
395 return sprintf(buf, format, ##args); \
396 } \
397 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
398
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400399VBD_SHOW(physical_device, "%x:%x\n", be->major, be->minor);
400VBD_SHOW(mode, "%s\n", be->mode);
401
Konrad Rzeszutek Wilk29117582012-08-13 10:53:17 -0400402static int xenvbd_sysfs_addif(struct xenbus_device *dev)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400403{
404 int error;
405
406 error = device_create_file(&dev->dev, &dev_attr_physical_device);
Konrad Rzeszutek Wilkd6091b22011-04-14 17:33:30 -0400407 if (error)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400408 goto fail1;
409
410 error = device_create_file(&dev->dev, &dev_attr_mode);
411 if (error)
412 goto fail2;
413
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400414 error = sysfs_create_group(&dev->dev.kobj, &xen_vbdstat_group);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400415 if (error)
416 goto fail3;
417
418 return 0;
419
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400420fail3: sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400421fail2: device_remove_file(&dev->dev, &dev_attr_mode);
422fail1: device_remove_file(&dev->dev, &dev_attr_physical_device);
423 return error;
424}
425
Konrad Rzeszutek Wilk29117582012-08-13 10:53:17 -0400426static void xenvbd_sysfs_delif(struct xenbus_device *dev)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400427{
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400428 sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400429 device_remove_file(&dev->dev, &dev_attr_mode);
430 device_remove_file(&dev->dev, &dev_attr_physical_device);
431}
432
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400433
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400434static void xen_vbd_free(struct xen_vbd *vbd)
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400435{
436 if (vbd->bdev)
437 blkdev_put(vbd->bdev, vbd->readonly ? FMODE_READ : FMODE_WRITE);
438 vbd->bdev = NULL;
439}
440
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400441static int xen_vbd_create(struct xen_blkif *blkif, blkif_vdev_t handle,
442 unsigned major, unsigned minor, int readonly,
443 int cdrom)
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400444{
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400445 struct xen_vbd *vbd;
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400446 struct block_device *bdev;
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400447 struct request_queue *q;
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400448
449 vbd = &blkif->vbd;
450 vbd->handle = handle;
451 vbd->readonly = readonly;
452 vbd->type = 0;
453
454 vbd->pdevice = MKDEV(major, minor);
455
456 bdev = blkdev_get_by_dev(vbd->pdevice, vbd->readonly ?
457 FMODE_READ : FMODE_WRITE, NULL);
458
459 if (IS_ERR(bdev)) {
Tao Chen77387b82015-04-01 15:04:22 +0000460 pr_warn("xen_vbd_create: device %08x could not be opened\n",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400461 vbd->pdevice);
462 return -ENOENT;
463 }
464
465 vbd->bdev = bdev;
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400466 if (vbd->bdev->bd_disk == NULL) {
Tao Chen77387b82015-04-01 15:04:22 +0000467 pr_warn("xen_vbd_create: device %08x doesn't exist\n",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400468 vbd->pdevice);
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400469 xen_vbd_free(vbd);
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400470 return -ENOENT;
471 }
Laszlo Ersek64649202011-05-25 12:24:25 +0200472 vbd->size = vbd_sz(vbd);
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400473
474 if (vbd->bdev->bd_disk->flags & GENHD_FL_CD || cdrom)
475 vbd->type |= VDISK_CDROM;
476 if (vbd->bdev->bd_disk->flags & GENHD_FL_REMOVABLE)
477 vbd->type |= VDISK_REMOVABLE;
478
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400479 q = bdev_get_queue(bdev);
Jens Axboec888a8f2016-04-13 13:33:19 -0600480 if (q && test_bit(QUEUE_FLAG_WC, &q->queue_flags))
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400481 vbd->flush_support = true;
482
Christoph Hellwig288dab82016-06-09 16:00:36 +0200483 if (q && blk_queue_secure_erase(q))
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400484 vbd->discard_secure = true;
485
Tao Chen77387b82015-04-01 15:04:22 +0000486 pr_debug("Successful creation of handle=%04x (dom=%u)\n",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400487 handle, blkif->domid);
488 return 0;
489}
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400490static int xen_blkbk_remove(struct xenbus_device *dev)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400491{
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800492 struct backend_info *be = dev_get_drvdata(&dev->dev);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400493
Tao Chen77387b82015-04-01 15:04:22 +0000494 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400495
496 if (be->major || be->minor)
497 xenvbd_sysfs_delif(dev);
498
499 if (be->backend_watch.node) {
500 unregister_xenbus_watch(&be->backend_watch);
501 kfree(be->backend_watch.node);
502 be->backend_watch.node = NULL;
503 }
504
Valentin Priescu814d04e2014-05-20 22:28:50 +0200505 dev_set_drvdata(&dev->dev, NULL);
506
Bob Liu93bb2772015-12-10 09:16:48 +0800507 if (be->blkif)
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400508 xen_blkif_disconnect(be->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400509
Bob Liu93bb2772015-12-10 09:16:48 +0800510 /* Put the reference we set in xen_blkif_alloc(). */
511 xen_blkif_put(be->blkif);
Jan Beulich9d092602012-12-20 10:31:11 +0000512 kfree(be->mode);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400513 kfree(be);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400514 return 0;
515}
516
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400517int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt,
518 struct backend_info *be, int state)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400519{
520 struct xenbus_device *dev = be->dev;
521 int err;
522
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400523 err = xenbus_printf(xbt, dev->nodename, "feature-flush-cache",
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400524 "%d", state);
525 if (err)
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400526 dev_warn(&dev->dev, "writing feature-flush-cache (%d)", err);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400527
528 return err;
529}
530
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400531static void xen_blkbk_discard(struct xenbus_transaction xbt, struct backend_info *be)
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800532{
533 struct xenbus_device *dev = be->dev;
534 struct xen_blkif *blkif = be->blkif;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800535 int err;
Olaf Heringc926b702014-05-21 16:32:42 +0200536 int state = 0, discard_enable;
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400537 struct block_device *bdev = be->blkif->vbd.bdev;
538 struct request_queue *q = bdev_get_queue(bdev);
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800539
Olaf Heringc926b702014-05-21 16:32:42 +0200540 err = xenbus_scanf(XBT_NIL, dev->nodename, "discard-enable", "%d",
541 &discard_enable);
542 if (err == 1 && !discard_enable)
543 return;
544
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400545 if (blk_queue_discard(q)) {
546 err = xenbus_printf(xbt, dev->nodename,
547 "discard-granularity", "%u",
548 q->limits.discard_granularity);
549 if (err) {
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400550 dev_warn(&dev->dev, "writing discard-granularity (%d)", err);
551 return;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800552 }
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400553 err = xenbus_printf(xbt, dev->nodename,
554 "discard-alignment", "%u",
555 q->limits.discard_alignment);
556 if (err) {
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400557 dev_warn(&dev->dev, "writing discard-alignment (%d)", err);
558 return;
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400559 }
560 state = 1;
561 /* Optional. */
562 err = xenbus_printf(xbt, dev->nodename,
563 "discard-secure", "%d",
564 blkif->vbd.discard_secure);
565 if (err) {
Konrad Rzeszutek Wilka71e23d2012-04-16 21:55:04 -0400566 dev_warn(&dev->dev, "writing discard-secure (%d)", err);
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400567 return;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800568 }
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800569 }
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800570 err = xenbus_printf(xbt, dev->nodename, "feature-discard",
571 "%d", state);
572 if (err)
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400573 dev_warn(&dev->dev, "writing feature-discard (%d)", err);
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800574}
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400575int xen_blkbk_barrier(struct xenbus_transaction xbt,
576 struct backend_info *be, int state)
577{
578 struct xenbus_device *dev = be->dev;
579 int err;
580
581 err = xenbus_printf(xbt, dev->nodename, "feature-barrier",
582 "%d", state);
583 if (err)
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400584 dev_warn(&dev->dev, "writing feature-barrier (%d)", err);
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400585
586 return err;
587}
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800588
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400589/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400590 * Entry point to this code when a new device is created. Allocate the basic
591 * structures, and watch the store waiting for the hotplug scripts to tell us
592 * the device's physical major and minor numbers. Switch to InitWait.
593 */
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400594static int xen_blkbk_probe(struct xenbus_device *dev,
595 const struct xenbus_device_id *id)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400596{
597 int err;
598 struct backend_info *be = kzalloc(sizeof(struct backend_info),
599 GFP_KERNEL);
Tao Chen77387b82015-04-01 15:04:22 +0000600
601 /* match the pr_debug in xen_blkbk_remove */
602 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
603
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400604 if (!be) {
605 xenbus_dev_fatal(dev, -ENOMEM,
606 "allocating backend structure");
607 return -ENOMEM;
608 }
609 be->dev = dev;
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800610 dev_set_drvdata(&dev->dev, be);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400611
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400612 be->blkif = xen_blkif_alloc(dev->otherend_id);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400613 if (IS_ERR(be->blkif)) {
614 err = PTR_ERR(be->blkif);
615 be->blkif = NULL;
616 xenbus_dev_fatal(dev, err, "creating block interface");
617 goto fail;
618 }
619
Jan Beulich5a705842016-02-10 04:18:10 -0700620 err = xenbus_printf(XBT_NIL, dev->nodename,
621 "feature-max-indirect-segments", "%u",
622 MAX_INDIRECT_SEGMENTS);
623 if (err)
624 dev_warn(&dev->dev,
625 "writing %s/feature-max-indirect-segments (%d)",
626 dev->nodename, err);
627
Bob Liud62d8602015-11-14 11:12:17 +0800628 /* Multi-queue: advertise how many queues are supported by us.*/
629 err = xenbus_printf(XBT_NIL, dev->nodename,
630 "multi-queue-max-queues", "%u", xenblk_max_queues);
631 if (err)
632 pr_warn("Error writing multi-queue-max-queues\n");
633
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400634 /* setup back pointer */
635 be->blkif->be = be;
636
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -0800637 err = xenbus_watch_pathfmt(dev, &be->backend_watch, backend_changed,
638 "%s/%s", dev->nodename, "physical-device");
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400639 if (err)
640 goto fail;
641
Bob Liu86839c52015-06-03 13:40:03 +0800642 err = xenbus_printf(XBT_NIL, dev->nodename, "max-ring-page-order", "%u",
643 xen_blkif_max_ring_order);
644 if (err)
645 pr_warn("%s write out 'max-ring-page-order' failed\n", __func__);
646
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400647 err = xenbus_switch_state(dev, XenbusStateInitWait);
648 if (err)
649 goto fail;
650
651 return 0;
652
653fail:
Tao Chen77387b82015-04-01 15:04:22 +0000654 pr_warn("%s failed\n", __func__);
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400655 xen_blkbk_remove(dev);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400656 return err;
657}
658
659
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400660/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400661 * Callback received when the hotplug scripts have placed the physical-device
662 * node. Read it and the mode node, and create a vbd. If the frontend is
663 * ready, connect.
664 */
665static void backend_changed(struct xenbus_watch *watch,
666 const char **vec, unsigned int len)
667{
668 int err;
669 unsigned major;
670 unsigned minor;
671 struct backend_info *be
672 = container_of(watch, struct backend_info, backend_watch);
673 struct xenbus_device *dev = be->dev;
674 int cdrom = 0;
Jan Beulich9d092602012-12-20 10:31:11 +0000675 unsigned long handle;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400676 char *device_type;
677
Tao Chen77387b82015-04-01 15:04:22 +0000678 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400679
680 err = xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x:%x",
681 &major, &minor);
682 if (XENBUS_EXIST_ERR(err)) {
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400683 /*
684 * Since this watch will fire once immediately after it is
685 * registered, we expect this. Ignore it, and wait for the
686 * hotplug scripts.
687 */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400688 return;
689 }
690 if (err != 2) {
691 xenbus_dev_fatal(dev, err, "reading physical-device");
692 return;
693 }
694
Jan Beulich9d092602012-12-20 10:31:11 +0000695 if (be->major | be->minor) {
696 if (be->major != major || be->minor != minor)
Tao Chen77387b82015-04-01 15:04:22 +0000697 pr_warn("changing physical device (from %x:%x to %x:%x) not supported.\n",
Jan Beulich9d092602012-12-20 10:31:11 +0000698 be->major, be->minor, major, minor);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400699 return;
700 }
701
702 be->mode = xenbus_read(XBT_NIL, dev->nodename, "mode", NULL);
703 if (IS_ERR(be->mode)) {
704 err = PTR_ERR(be->mode);
705 be->mode = NULL;
706 xenbus_dev_fatal(dev, err, "reading mode");
707 return;
708 }
709
710 device_type = xenbus_read(XBT_NIL, dev->otherend, "device-type", NULL);
711 if (!IS_ERR(device_type)) {
712 cdrom = strcmp(device_type, "cdrom") == 0;
713 kfree(device_type);
714 }
715
Jan Beulich9d092602012-12-20 10:31:11 +0000716 /* Front end dir is a number, which is used as the handle. */
Jingoo Hanbb8e0e82013-09-11 14:20:07 -0700717 err = kstrtoul(strrchr(dev->otherend, '/') + 1, 0, &handle);
Jan Beulichaea305e12016-07-07 01:38:13 -0600718 if (err) {
719 kfree(be->mode);
720 be->mode = NULL;
Jan Beulich9d092602012-12-20 10:31:11 +0000721 return;
Jan Beulichaea305e12016-07-07 01:38:13 -0600722 }
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400723
Jan Beulich9d092602012-12-20 10:31:11 +0000724 be->major = major;
725 be->minor = minor;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400726
Jan Beulich9d092602012-12-20 10:31:11 +0000727 err = xen_vbd_create(be->blkif, handle, major, minor,
728 !strchr(be->mode, 'w'), cdrom);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400729
Jan Beulich9d092602012-12-20 10:31:11 +0000730 if (err)
731 xenbus_dev_fatal(dev, err, "creating vbd structure");
732 else {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400733 err = xenvbd_sysfs_addif(dev);
734 if (err) {
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400735 xen_vbd_free(&be->blkif->vbd);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400736 xenbus_dev_fatal(dev, err, "creating sysfs entries");
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400737 }
Jan Beulich9d092602012-12-20 10:31:11 +0000738 }
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400739
Jan Beulich9d092602012-12-20 10:31:11 +0000740 if (err) {
741 kfree(be->mode);
742 be->mode = NULL;
743 be->major = 0;
744 be->minor = 0;
745 } else {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400746 /* We're potentially connected now */
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400747 xen_update_blkif_status(be->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400748 }
749}
750
751
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400752/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400753 * Callback received when the frontend's state changes.
754 */
755static void frontend_changed(struct xenbus_device *dev,
756 enum xenbus_state frontend_state)
757{
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800758 struct backend_info *be = dev_get_drvdata(&dev->dev);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400759 int err;
760
Tao Chen77387b82015-04-01 15:04:22 +0000761 pr_debug("%s %p %s\n", __func__, dev, xenbus_strstate(frontend_state));
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400762
763 switch (frontend_state) {
764 case XenbusStateInitialising:
765 if (dev->state == XenbusStateClosed) {
Tao Chen77387b82015-04-01 15:04:22 +0000766 pr_info("%s: prepare for reconnect\n", dev->nodename);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400767 xenbus_switch_state(dev, XenbusStateInitWait);
768 }
769 break;
770
771 case XenbusStateInitialised:
772 case XenbusStateConnected:
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400773 /*
774 * Ensure we connect even when two watches fire in
Justin P. Mattock42b2aa82011-11-28 20:31:00 -0800775 * close succession and we miss the intermediate value
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400776 * of frontend_state.
777 */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400778 if (dev->state == XenbusStateConnected)
779 break;
780
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400781 /*
782 * Enforce precondition before potential leak point.
Joe Jin1bc05b02011-08-15 12:57:07 +0800783 * xen_blkif_disconnect() is idempotent.
Keir Fraser313d7b02010-11-24 22:08:20 -0800784 */
Valentin Priescu814d04e2014-05-20 22:28:50 +0200785 err = xen_blkif_disconnect(be->blkif);
786 if (err) {
787 xenbus_dev_fatal(dev, err, "pending I/O");
788 break;
789 }
Keir Fraser313d7b02010-11-24 22:08:20 -0800790
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400791 err = connect_ring(be);
Konrad Rzeszutek Wilk2d0382f2015-11-25 13:20:14 -0500792 if (err) {
793 /*
794 * Clean up so that memory resources can be used by
795 * other devices. connect_ring reported already error.
796 */
797 xen_blkif_disconnect(be->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400798 break;
Konrad Rzeszutek Wilk2d0382f2015-11-25 13:20:14 -0500799 }
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400800 xen_update_blkif_status(be->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400801 break;
802
803 case XenbusStateClosing:
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400804 xenbus_switch_state(dev, XenbusStateClosing);
805 break;
806
807 case XenbusStateClosed:
Joe Jin6f5986b2011-08-15 12:51:31 +0800808 xen_blkif_disconnect(be->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400809 xenbus_switch_state(dev, XenbusStateClosed);
810 if (xenbus_dev_is_online(dev))
811 break;
812 /* fall through if not online */
813 case XenbusStateUnknown:
Joe Jin1bc05b02011-08-15 12:57:07 +0800814 /* implies xen_blkif_disconnect() via xen_blkbk_remove() */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400815 device_unregister(&dev->dev);
816 break;
817
818 default:
819 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
820 frontend_state);
821 break;
822 }
823}
824
825
826/* ** Connection ** */
827
828
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400829/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400830 * Write the physical details regarding the block device to the store, and
831 * switch to Connected state.
832 */
833static void connect(struct backend_info *be)
834{
835 struct xenbus_transaction xbt;
836 int err;
837 struct xenbus_device *dev = be->dev;
838
Tao Chen77387b82015-04-01 15:04:22 +0000839 pr_debug("%s %s\n", __func__, dev->otherend);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400840
841 /* Supply the information about the device the frontend needs */
842again:
843 err = xenbus_transaction_start(&xbt);
844 if (err) {
845 xenbus_dev_fatal(dev, err, "starting transaction");
846 return;
847 }
848
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400849 /* If we can't advertise it is OK. */
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400850 xen_blkbk_flush_diskcache(xbt, be, be->blkif->vbd.flush_support);
851
852 xen_blkbk_discard(xbt, be);
853
854 xen_blkbk_barrier(xbt, be, be->blkif->vbd.flush_support);
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400855
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200856 err = xenbus_printf(xbt, dev->nodename, "feature-persistent", "%u", 1);
857 if (err) {
858 xenbus_dev_fatal(dev, err, "writing %s/feature-persistent",
859 dev->nodename);
860 goto abort;
861 }
862
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400863 err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400864 (unsigned long long)vbd_sz(&be->blkif->vbd));
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400865 if (err) {
866 xenbus_dev_fatal(dev, err, "writing %s/sectors",
867 dev->nodename);
868 goto abort;
869 }
870
871 /* FIXME: use a typename instead */
872 err = xenbus_printf(xbt, dev->nodename, "info", "%u",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400873 be->blkif->vbd.type |
874 (be->blkif->vbd.readonly ? VDISK_READONLY : 0));
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400875 if (err) {
876 xenbus_dev_fatal(dev, err, "writing %s/info",
877 dev->nodename);
878 goto abort;
879 }
880 err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400881 (unsigned long)
882 bdev_logical_block_size(be->blkif->vbd.bdev));
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400883 if (err) {
884 xenbus_dev_fatal(dev, err, "writing %s/sector-size",
885 dev->nodename);
886 goto abort;
887 }
Stefan Bader7c4d7d72013-05-13 16:28:15 +0200888 err = xenbus_printf(xbt, dev->nodename, "physical-sector-size", "%u",
889 bdev_physical_block_size(be->blkif->vbd.bdev));
890 if (err)
891 xenbus_dev_error(dev, err, "writing %s/physical-sector-size",
892 dev->nodename);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400893
894 err = xenbus_transaction_end(xbt, 0);
895 if (err == -EAGAIN)
896 goto again;
897 if (err)
898 xenbus_dev_fatal(dev, err, "ending transaction");
899
900 err = xenbus_switch_state(dev, XenbusStateConnected);
901 if (err)
Joe Perches08b8bfc2011-06-12 09:21:13 -0700902 xenbus_dev_fatal(dev, err, "%s: switching to Connected state",
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400903 dev->nodename);
904
905 return;
906 abort:
907 xenbus_transaction_end(xbt, 1);
908}
909
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500910/*
911 * Each ring may have multi pages, depends on "ring-page-order".
912 */
913static int read_per_ring_refs(struct xen_blkif_ring *ring, const char *dir)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400914{
Julien Grall9cce2912015-10-13 17:50:11 +0100915 unsigned int ring_ref[XENBUS_MAX_RING_GRANTS];
Bob Liu69b91ed2015-06-03 13:40:01 +0800916 struct pending_req *req, *n;
917 int err, i, j;
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500918 struct xen_blkif *blkif = ring->blkif;
919 struct xenbus_device *dev = blkif->be->dev;
920 unsigned int ring_page_order, nr_grefs, evtchn;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400921
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500922 err = xenbus_scanf(XBT_NIL, dir, "event-channel", "%u",
Bob Liu86839c52015-06-03 13:40:03 +0800923 &evtchn);
924 if (err != 1) {
925 err = -EINVAL;
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500926 xenbus_dev_fatal(dev, err, "reading %s/event-channel", dir);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400927 return err;
928 }
Bob Liu86839c52015-06-03 13:40:03 +0800929
930 err = xenbus_scanf(XBT_NIL, dev->otherend, "ring-page-order", "%u",
931 &ring_page_order);
932 if (err != 1) {
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500933 err = xenbus_scanf(XBT_NIL, dir, "ring-ref", "%u", &ring_ref[0]);
Bob Liu86839c52015-06-03 13:40:03 +0800934 if (err != 1) {
935 err = -EINVAL;
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500936 xenbus_dev_fatal(dev, err, "reading %s/ring-ref", dir);
Bob Liu86839c52015-06-03 13:40:03 +0800937 return err;
938 }
939 nr_grefs = 1;
Bob Liu86839c52015-06-03 13:40:03 +0800940 } else {
941 unsigned int i;
942
943 if (ring_page_order > xen_blkif_max_ring_order) {
944 err = -EINVAL;
945 xenbus_dev_fatal(dev, err, "%s/request %d ring page order exceed max:%d",
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500946 dir, ring_page_order,
Bob Liu86839c52015-06-03 13:40:03 +0800947 xen_blkif_max_ring_order);
948 return err;
949 }
950
951 nr_grefs = 1 << ring_page_order;
952 for (i = 0; i < nr_grefs; i++) {
953 char ring_ref_name[RINGREF_NAME_LEN];
954
955 snprintf(ring_ref_name, RINGREF_NAME_LEN, "ring-ref%u", i);
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500956 err = xenbus_scanf(XBT_NIL, dir, ring_ref_name,
Bob Liu86839c52015-06-03 13:40:03 +0800957 "%u", &ring_ref[i]);
958 if (err != 1) {
959 err = -EINVAL;
960 xenbus_dev_fatal(dev, err, "reading %s/%s",
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500961 dir, ring_ref_name);
Bob Liu86839c52015-06-03 13:40:03 +0800962 return err;
963 }
Bob Liu86839c52015-06-03 13:40:03 +0800964 }
965 }
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500966 blkif->nr_ring_pages = nr_grefs;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400967
Bob Liu86839c52015-06-03 13:40:03 +0800968 for (i = 0; i < nr_grefs * XEN_BLKIF_REQS_PER_PAGE; i++) {
Bob Liu69b91ed2015-06-03 13:40:01 +0800969 req = kzalloc(sizeof(*req), GFP_KERNEL);
970 if (!req)
971 goto fail;
Bob Liu59795702015-11-14 11:12:15 +0800972 list_add_tail(&req->free_list, &ring->pending_free);
Bob Liu69b91ed2015-06-03 13:40:01 +0800973 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++) {
974 req->segments[j] = kzalloc(sizeof(*req->segments[0]), GFP_KERNEL);
975 if (!req->segments[j])
976 goto fail;
977 }
978 for (j = 0; j < MAX_INDIRECT_PAGES; j++) {
979 req->indirect_pages[j] = kzalloc(sizeof(*req->indirect_pages[0]),
980 GFP_KERNEL);
981 if (!req->indirect_pages[j])
982 goto fail;
983 }
984 }
985
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400986 /* Map the shared frame, irq etc. */
Bob Liu59795702015-11-14 11:12:15 +0800987 err = xen_blkif_map(ring, ring_ref, nr_grefs, evtchn);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400988 if (err) {
Bob Liu86839c52015-06-03 13:40:03 +0800989 xenbus_dev_fatal(dev, err, "mapping ring-ref port %u", evtchn);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400990 return err;
991 }
992
993 return 0;
Bob Liu69b91ed2015-06-03 13:40:01 +0800994
995fail:
Bob Liu59795702015-11-14 11:12:15 +0800996 list_for_each_entry_safe(req, n, &ring->pending_free, free_list) {
Bob Liu69b91ed2015-06-03 13:40:01 +0800997 list_del(&req->free_list);
998 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++) {
999 if (!req->segments[j])
1000 break;
1001 kfree(req->segments[j]);
1002 }
1003 for (j = 0; j < MAX_INDIRECT_PAGES; j++) {
1004 if (!req->indirect_pages[j])
1005 break;
1006 kfree(req->indirect_pages[j]);
1007 }
1008 kfree(req);
1009 }
1010 return -ENOMEM;
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -05001011
1012}
1013
1014static int connect_ring(struct backend_info *be)
1015{
1016 struct xenbus_device *dev = be->dev;
1017 unsigned int pers_grants;
1018 char protocol[64] = "";
1019 int err, i;
1020 char *xspath;
1021 size_t xspathsize;
1022 const size_t xenstore_path_ext_size = 11; /* sufficient for "/queue-NNN" */
Bob Liud62d8602015-11-14 11:12:17 +08001023 unsigned int requested_num_queues = 0;
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -05001024
1025 pr_debug("%s %s\n", __func__, dev->otherend);
1026
1027 be->blkif->blk_protocol = BLKIF_PROTOCOL_DEFAULT;
Jan Beulich66943892016-07-07 02:05:21 -06001028 err = xenbus_scanf(XBT_NIL, dev->otherend, "protocol",
1029 "%63s", protocol);
1030 if (err <= 0)
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -05001031 strcpy(protocol, "unspecified, assuming default");
1032 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_NATIVE))
1033 be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
1034 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_32))
1035 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_32;
1036 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_64))
1037 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_64;
1038 else {
1039 xenbus_dev_fatal(dev, err, "unknown fe protocol %s", protocol);
Konrad Rzeszutek Wilkbde21f72015-11-25 13:07:39 -05001040 return -ENOSYS;
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -05001041 }
Jan Beulich66943892016-07-07 02:05:21 -06001042 err = xenbus_scanf(XBT_NIL, dev->otherend,
1043 "feature-persistent", "%u", &pers_grants);
1044 if (err <= 0)
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -05001045 pers_grants = 0;
1046
1047 be->blkif->vbd.feature_gnt_persistent = pers_grants;
1048 be->blkif->vbd.overflow_max_grants = 0;
1049
Bob Liud62d8602015-11-14 11:12:17 +08001050 /*
1051 * Read the number of hardware queues from frontend.
1052 */
1053 err = xenbus_scanf(XBT_NIL, dev->otherend, "multi-queue-num-queues",
1054 "%u", &requested_num_queues);
1055 if (err < 0) {
1056 requested_num_queues = 1;
1057 } else {
1058 if (requested_num_queues > xenblk_max_queues
1059 || requested_num_queues == 0) {
1060 /* Buggy or malicious guest. */
1061 xenbus_dev_fatal(dev, err,
1062 "guest requested %u queues, exceeding the maximum of %u.",
1063 requested_num_queues, xenblk_max_queues);
1064 return -ENOSYS;
1065 }
1066 }
1067 be->blkif->nr_rings = requested_num_queues;
1068 if (xen_blkif_alloc_rings(be->blkif))
1069 return -ENOMEM;
1070
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -05001071 pr_info("%s: using %d queues, protocol %d (%s) %s\n", dev->nodename,
1072 be->blkif->nr_rings, be->blkif->blk_protocol, protocol,
1073 pers_grants ? "persistent grants" : "");
1074
1075 if (be->blkif->nr_rings == 1)
1076 return read_per_ring_refs(&be->blkif->rings[0], dev->otherend);
1077 else {
1078 xspathsize = strlen(dev->otherend) + xenstore_path_ext_size;
1079 xspath = kmalloc(xspathsize, GFP_KERNEL);
1080 if (!xspath) {
1081 xenbus_dev_fatal(dev, -ENOMEM, "reading ring references");
1082 return -ENOMEM;
1083 }
1084
1085 for (i = 0; i < be->blkif->nr_rings; i++) {
1086 memset(xspath, 0, xspathsize);
1087 snprintf(xspath, xspathsize, "%s/queue-%u", dev->otherend, i);
1088 err = read_per_ring_refs(&be->blkif->rings[i], xspath);
1089 if (err) {
1090 kfree(xspath);
1091 return err;
1092 }
1093 }
1094 kfree(xspath);
1095 }
1096 return 0;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001097}
1098
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001099static const struct xenbus_device_id xen_blkbk_ids[] = {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001100 { "vbd" },
1101 { "" }
1102};
1103
David Vrabel95afae42014-09-08 17:30:41 +01001104static struct xenbus_driver xen_blkbk_driver = {
1105 .ids = xen_blkbk_ids,
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001106 .probe = xen_blkbk_probe,
1107 .remove = xen_blkbk_remove,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001108 .otherend_changed = frontend_changed
David Vrabel95afae42014-09-08 17:30:41 +01001109};
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001110
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001111int xen_blkif_xenbus_init(void)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001112{
Jan Beulich73db1442011-12-22 09:08:13 +00001113 return xenbus_register_backend(&xen_blkbk_driver);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001114}