blob: 16e28ee880a40aa7ef904d34dd7e0a0f96ae17b6 [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 Wilk2fb1ef42015-12-11 12:08:48 -050089 struct xen_blkif_ring *ring;
90 int i;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040091
92 /* Not ready to connect? */
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -050093 if (!blkif->rings || !blkif->rings[0].irq || !blkif->vbd.bdev)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040094 return;
95
96 /* Already connected? */
97 if (blkif->be->dev->state == XenbusStateConnected)
98 return;
99
100 /* Attempt to connect: exit if we fail to. */
101 connect(blkif->be);
102 if (blkif->be->dev->state != XenbusStateConnected)
103 return;
104
105 err = blkback_name(blkif, name);
106 if (err) {
107 xenbus_dev_error(blkif->be->dev, err, "get blkback dev name");
108 return;
109 }
110
Chris Lalancettecbf46292010-07-21 12:41:45 -0700111 err = filemap_write_and_wait(blkif->vbd.bdev->bd_inode->i_mapping);
112 if (err) {
113 xenbus_dev_error(blkif->be->dev, err, "block flush");
114 return;
115 }
116 invalidate_inode_pages2(blkif->vbd.bdev->bd_inode->i_mapping);
117
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500118 for (i = 0; i < blkif->nr_rings; i++) {
119 ring = &blkif->rings[i];
120 ring->xenblkd = kthread_run(xen_blkif_schedule, ring, "%s-%d", name, i);
121 if (IS_ERR(ring->xenblkd)) {
122 err = PTR_ERR(ring->xenblkd);
123 ring->xenblkd = NULL;
124 xenbus_dev_fatal(blkif->be->dev, err,
125 "start %s-%d xenblkd", name, i);
126 goto out;
127 }
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400128 }
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500129 return;
130
131out:
132 while (--i >= 0) {
133 ring = &blkif->rings[i];
134 kthread_stop(ring->xenblkd);
135 }
136 return;
137}
138
139static int xen_blkif_alloc_rings(struct xen_blkif *blkif)
140{
141 unsigned int r;
142
143 blkif->rings = kzalloc(blkif->nr_rings * sizeof(struct xen_blkif_ring), GFP_KERNEL);
144 if (!blkif->rings)
145 return -ENOMEM;
146
147 for (r = 0; r < blkif->nr_rings; r++) {
148 struct xen_blkif_ring *ring = &blkif->rings[r];
149
150 spin_lock_init(&ring->blk_ring_lock);
151 init_waitqueue_head(&ring->wq);
152 INIT_LIST_HEAD(&ring->pending_free);
Bob Liud4bf0062015-11-14 11:12:19 +0800153 INIT_LIST_HEAD(&ring->persistent_purge_list);
154 INIT_WORK(&ring->persistent_purge_work, xen_blkbk_unmap_purged_grants);
155 spin_lock_init(&ring->free_pages_lock);
156 INIT_LIST_HEAD(&ring->free_pages);
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500157
158 spin_lock_init(&ring->pending_free_lock);
159 init_waitqueue_head(&ring->pending_free_wq);
160 init_waitqueue_head(&ring->shutdown_wq);
161 ring->blkif = blkif;
Bob Liudb6fbc12015-12-09 07:44:02 +0800162 ring->st_print = jiffies;
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500163 xen_blkif_get(blkif);
164 }
165
166 return 0;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400167}
168
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400169static struct xen_blkif *xen_blkif_alloc(domid_t domid)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400170{
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400171 struct xen_blkif *blkif;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400172
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200173 BUILD_BUG_ON(MAX_INDIRECT_PAGES > BLKIF_MAX_INDIRECT_PAGES_PER_REQUEST);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400174
Wei Yongjun654dbef2012-08-27 12:28:57 +0800175 blkif = kmem_cache_zalloc(xen_blkif_cachep, GFP_KERNEL);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400176 if (!blkif)
177 return ERR_PTR(-ENOMEM);
178
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400179 blkif->domid = domid;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400180 atomic_set(&blkif->refcnt, 1);
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400181 init_completion(&blkif->drain_complete);
Bob Liu59795702015-11-14 11:12:15 +0800182 INIT_WORK(&blkif->free_work, xen_blkif_deferred_free);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400183
184 return blkif;
185}
186
Bob Liu59795702015-11-14 11:12:15 +0800187static int xen_blkif_map(struct xen_blkif_ring *ring, grant_ref_t *gref,
Bob Liu86839c52015-06-03 13:40:03 +0800188 unsigned int nr_grefs, unsigned int evtchn)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400189{
190 int err;
Bob Liu59795702015-11-14 11:12:15 +0800191 struct xen_blkif *blkif = ring->blkif;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400192
193 /* Already connected through? */
Bob Liu59795702015-11-14 11:12:15 +0800194 if (ring->irq)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400195 return 0;
196
Bob Liu86839c52015-06-03 13:40:03 +0800197 err = xenbus_map_ring_valloc(blkif->be->dev, gref, nr_grefs,
Bob Liu59795702015-11-14 11:12:15 +0800198 &ring->blk_ring);
David Vrabel2d073842011-09-29 16:53:30 +0100199 if (err < 0)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400200 return err;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400201
202 switch (blkif->blk_protocol) {
203 case BLKIF_PROTOCOL_NATIVE:
204 {
205 struct blkif_sring *sring;
Bob Liu59795702015-11-14 11:12:15 +0800206 sring = (struct blkif_sring *)ring->blk_ring;
207 BACK_RING_INIT(&ring->blk_rings.native, sring,
Julien Grall67de5df2015-05-05 16:25:56 +0100208 XEN_PAGE_SIZE * nr_grefs);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400209 break;
210 }
211 case BLKIF_PROTOCOL_X86_32:
212 {
213 struct blkif_x86_32_sring *sring_x86_32;
Bob Liu59795702015-11-14 11:12:15 +0800214 sring_x86_32 = (struct blkif_x86_32_sring *)ring->blk_ring;
215 BACK_RING_INIT(&ring->blk_rings.x86_32, sring_x86_32,
Julien Grall67de5df2015-05-05 16:25:56 +0100216 XEN_PAGE_SIZE * nr_grefs);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400217 break;
218 }
219 case BLKIF_PROTOCOL_X86_64:
220 {
221 struct blkif_x86_64_sring *sring_x86_64;
Bob Liu59795702015-11-14 11:12:15 +0800222 sring_x86_64 = (struct blkif_x86_64_sring *)ring->blk_ring;
223 BACK_RING_INIT(&ring->blk_rings.x86_64, sring_x86_64,
Julien Grall67de5df2015-05-05 16:25:56 +0100224 XEN_PAGE_SIZE * nr_grefs);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400225 break;
226 }
227 default:
228 BUG();
229 }
230
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400231 err = bind_interdomain_evtchn_to_irqhandler(blkif->domid, evtchn,
232 xen_blkif_be_int, 0,
Bob Liu59795702015-11-14 11:12:15 +0800233 "blkif-backend", ring);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400234 if (err < 0) {
Bob Liu59795702015-11-14 11:12:15 +0800235 xenbus_unmap_ring_vfree(blkif->be->dev, ring->blk_ring);
236 ring->blk_rings.common.sring = NULL;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400237 return err;
238 }
Bob Liu59795702015-11-14 11:12:15 +0800239 ring->irq = err;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400240
241 return 0;
242}
243
Valentin Priescu814d04e2014-05-20 22:28:50 +0200244static int xen_blkif_disconnect(struct xen_blkif *blkif)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400245{
Roger Pau Monnef929d422015-09-04 12:08:07 +0200246 struct pending_req *req, *n;
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500247 unsigned int j, r;
Roger Pau Monnef929d422015-09-04 12:08:07 +0200248
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500249 for (r = 0; r < blkif->nr_rings; r++) {
250 struct xen_blkif_ring *ring = &blkif->rings[r];
251 unsigned int i = 0;
252
253 if (ring->xenblkd) {
254 kthread_stop(ring->xenblkd);
255 wake_up(&ring->shutdown_wq);
256 ring->xenblkd = NULL;
257 }
258
259 /* The above kthread_stop() guarantees that at this point we
260 * don't have any discard_io or other_io requests. So, checking
261 * for inflight IO is enough.
262 */
263 if (atomic_read(&ring->inflight) > 0)
264 return -EBUSY;
265
266 if (ring->irq) {
267 unbind_from_irqhandler(ring->irq, ring);
268 ring->irq = 0;
269 }
270
271 if (ring->blk_rings.common.sring) {
272 xenbus_unmap_ring_vfree(blkif->be->dev, ring->blk_ring);
273 ring->blk_rings.common.sring = NULL;
274 }
275
276 /* Remove all persistent grants and the cache of ballooned pages. */
277 xen_blkbk_free_caches(ring);
278
279 /* Check that there is no request in use */
280 list_for_each_entry_safe(req, n, &ring->pending_free, free_list) {
281 list_del(&req->free_list);
282
283 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++)
284 kfree(req->segments[j]);
285
286 for (j = 0; j < MAX_INDIRECT_PAGES; j++)
287 kfree(req->indirect_pages[j]);
288
289 kfree(req);
290 i++;
291 }
292
Bob Liud4bf0062015-11-14 11:12:19 +0800293 BUG_ON(atomic_read(&ring->persistent_gnt_in_use) != 0);
294 BUG_ON(!list_empty(&ring->persistent_purge_list));
295 BUG_ON(!RB_EMPTY_ROOT(&ring->persistent_gnts));
296 BUG_ON(!list_empty(&ring->free_pages));
297 BUG_ON(ring->free_pages_num != 0);
298 BUG_ON(ring->persistent_gnt_c != 0);
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500299 WARN_ON(i != (XEN_BLKIF_REQS_PER_PAGE * blkif->nr_ring_pages));
Bob Liu93bb2772015-12-10 09:16:48 +0800300 xen_blkif_put(blkif);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400301 }
Roger Pau Monnef929d422015-09-04 12:08:07 +0200302 blkif->nr_ring_pages = 0;
Bob Liu93bb2772015-12-10 09:16:48 +0800303 /*
304 * blkif->rings was allocated in connect_ring, so we should free it in
305 * here.
306 */
307 kfree(blkif->rings);
308 blkif->rings = NULL;
309 blkif->nr_rings = 0;
Roger Pau Monnef929d422015-09-04 12:08:07 +0200310
311 return 0;
312}
313
314static void xen_blkif_free(struct xen_blkif *blkif)
315{
316
317 xen_blkif_disconnect(blkif);
318 xen_vbd_free(&blkif->vbd);
319
320 /* Make sure everything is drained before shutting down */
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400321 kmem_cache_free(xen_blkif_cachep, blkif);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400322}
323
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400324int __init xen_blkif_interface_init(void)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400325{
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400326 xen_blkif_cachep = kmem_cache_create("blkif_cache",
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400327 sizeof(struct xen_blkif),
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400328 0, 0, NULL);
329 if (!xen_blkif_cachep)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400330 return -ENOMEM;
331
332 return 0;
333}
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400334
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400335/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400336 * sysfs interface for VBD I/O requests
337 */
338
Bob Liudb6fbc12015-12-09 07:44:02 +0800339#define VBD_SHOW_ALLRING(name, format) \
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400340 static ssize_t show_##name(struct device *_dev, \
341 struct device_attribute *attr, \
342 char *buf) \
343 { \
344 struct xenbus_device *dev = to_xenbus_device(_dev); \
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800345 struct backend_info *be = dev_get_drvdata(&dev->dev); \
Bob Liudb6fbc12015-12-09 07:44:02 +0800346 struct xen_blkif *blkif = be->blkif; \
347 unsigned int i; \
348 unsigned long long result = 0; \
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400349 \
Bob Liudb6fbc12015-12-09 07:44:02 +0800350 if (!blkif->rings) \
351 goto out; \
352 \
353 for (i = 0; i < blkif->nr_rings; i++) { \
354 struct xen_blkif_ring *ring = &blkif->rings[i]; \
355 \
356 result += ring->st_##name; \
357 } \
358 \
359out: \
360 return sprintf(buf, format, result); \
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400361 } \
362 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
363
Bob Liudb6fbc12015-12-09 07:44:02 +0800364VBD_SHOW_ALLRING(oo_req, "%llu\n");
365VBD_SHOW_ALLRING(rd_req, "%llu\n");
366VBD_SHOW_ALLRING(wr_req, "%llu\n");
367VBD_SHOW_ALLRING(f_req, "%llu\n");
368VBD_SHOW_ALLRING(ds_req, "%llu\n");
369VBD_SHOW_ALLRING(rd_sect, "%llu\n");
370VBD_SHOW_ALLRING(wr_sect, "%llu\n");
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400371
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400372static struct attribute *xen_vbdstat_attrs[] = {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400373 &dev_attr_oo_req.attr,
374 &dev_attr_rd_req.attr,
375 &dev_attr_wr_req.attr,
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400376 &dev_attr_f_req.attr,
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800377 &dev_attr_ds_req.attr,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400378 &dev_attr_rd_sect.attr,
379 &dev_attr_wr_sect.attr,
380 NULL
381};
382
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400383static struct attribute_group xen_vbdstat_group = {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400384 .name = "statistics",
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400385 .attrs = xen_vbdstat_attrs,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400386};
387
Bob Liudb6fbc12015-12-09 07:44:02 +0800388#define VBD_SHOW(name, format, args...) \
389 static ssize_t show_##name(struct device *_dev, \
390 struct device_attribute *attr, \
391 char *buf) \
392 { \
393 struct xenbus_device *dev = to_xenbus_device(_dev); \
394 struct backend_info *be = dev_get_drvdata(&dev->dev); \
395 \
396 return sprintf(buf, format, ##args); \
397 } \
398 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
399
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400400VBD_SHOW(physical_device, "%x:%x\n", be->major, be->minor);
401VBD_SHOW(mode, "%s\n", be->mode);
402
Konrad Rzeszutek Wilk29117582012-08-13 10:53:17 -0400403static int xenvbd_sysfs_addif(struct xenbus_device *dev)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400404{
405 int error;
406
407 error = device_create_file(&dev->dev, &dev_attr_physical_device);
Konrad Rzeszutek Wilkd6091b22011-04-14 17:33:30 -0400408 if (error)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400409 goto fail1;
410
411 error = device_create_file(&dev->dev, &dev_attr_mode);
412 if (error)
413 goto fail2;
414
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400415 error = sysfs_create_group(&dev->dev.kobj, &xen_vbdstat_group);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400416 if (error)
417 goto fail3;
418
419 return 0;
420
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400421fail3: sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400422fail2: device_remove_file(&dev->dev, &dev_attr_mode);
423fail1: device_remove_file(&dev->dev, &dev_attr_physical_device);
424 return error;
425}
426
Konrad Rzeszutek Wilk29117582012-08-13 10:53:17 -0400427static void xenvbd_sysfs_delif(struct xenbus_device *dev)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400428{
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400429 sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400430 device_remove_file(&dev->dev, &dev_attr_mode);
431 device_remove_file(&dev->dev, &dev_attr_physical_device);
432}
433
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400434
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400435static void xen_vbd_free(struct xen_vbd *vbd)
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400436{
437 if (vbd->bdev)
438 blkdev_put(vbd->bdev, vbd->readonly ? FMODE_READ : FMODE_WRITE);
439 vbd->bdev = NULL;
440}
441
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400442static int xen_vbd_create(struct xen_blkif *blkif, blkif_vdev_t handle,
443 unsigned major, unsigned minor, int readonly,
444 int cdrom)
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400445{
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400446 struct xen_vbd *vbd;
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400447 struct block_device *bdev;
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400448 struct request_queue *q;
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400449
450 vbd = &blkif->vbd;
451 vbd->handle = handle;
452 vbd->readonly = readonly;
453 vbd->type = 0;
454
455 vbd->pdevice = MKDEV(major, minor);
456
457 bdev = blkdev_get_by_dev(vbd->pdevice, vbd->readonly ?
458 FMODE_READ : FMODE_WRITE, NULL);
459
460 if (IS_ERR(bdev)) {
Tao Chen77387b82015-04-01 15:04:22 +0000461 pr_warn("xen_vbd_create: device %08x could not be opened\n",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400462 vbd->pdevice);
463 return -ENOENT;
464 }
465
466 vbd->bdev = bdev;
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400467 if (vbd->bdev->bd_disk == NULL) {
Tao Chen77387b82015-04-01 15:04:22 +0000468 pr_warn("xen_vbd_create: device %08x doesn't exist\n",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400469 vbd->pdevice);
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400470 xen_vbd_free(vbd);
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400471 return -ENOENT;
472 }
Laszlo Ersek64649202011-05-25 12:24:25 +0200473 vbd->size = vbd_sz(vbd);
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400474
475 if (vbd->bdev->bd_disk->flags & GENHD_FL_CD || cdrom)
476 vbd->type |= VDISK_CDROM;
477 if (vbd->bdev->bd_disk->flags & GENHD_FL_REMOVABLE)
478 vbd->type |= VDISK_REMOVABLE;
479
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400480 q = bdev_get_queue(bdev);
481 if (q && q->flush_flags)
482 vbd->flush_support = true;
483
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400484 if (q && blk_queue_secdiscard(q))
485 vbd->discard_secure = true;
486
Tao Chen77387b82015-04-01 15:04:22 +0000487 pr_debug("Successful creation of handle=%04x (dom=%u)\n",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400488 handle, blkif->domid);
489 return 0;
490}
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400491static int xen_blkbk_remove(struct xenbus_device *dev)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400492{
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800493 struct backend_info *be = dev_get_drvdata(&dev->dev);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400494
Tao Chen77387b82015-04-01 15:04:22 +0000495 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400496
497 if (be->major || be->minor)
498 xenvbd_sysfs_delif(dev);
499
500 if (be->backend_watch.node) {
501 unregister_xenbus_watch(&be->backend_watch);
502 kfree(be->backend_watch.node);
503 be->backend_watch.node = NULL;
504 }
505
Valentin Priescu814d04e2014-05-20 22:28:50 +0200506 dev_set_drvdata(&dev->dev, NULL);
507
Bob Liu93bb2772015-12-10 09:16:48 +0800508 if (be->blkif)
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400509 xen_blkif_disconnect(be->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400510
Bob Liu93bb2772015-12-10 09:16:48 +0800511 /* Put the reference we set in xen_blkif_alloc(). */
512 xen_blkif_put(be->blkif);
Jan Beulich9d092602012-12-20 10:31:11 +0000513 kfree(be->mode);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400514 kfree(be);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400515 return 0;
516}
517
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400518int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt,
519 struct backend_info *be, int state)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400520{
521 struct xenbus_device *dev = be->dev;
522 int err;
523
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400524 err = xenbus_printf(xbt, dev->nodename, "feature-flush-cache",
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400525 "%d", state);
526 if (err)
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400527 dev_warn(&dev->dev, "writing feature-flush-cache (%d)", err);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400528
529 return err;
530}
531
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400532static void xen_blkbk_discard(struct xenbus_transaction xbt, struct backend_info *be)
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800533{
534 struct xenbus_device *dev = be->dev;
535 struct xen_blkif *blkif = be->blkif;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800536 int err;
Olaf Heringc926b702014-05-21 16:32:42 +0200537 int state = 0, discard_enable;
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400538 struct block_device *bdev = be->blkif->vbd.bdev;
539 struct request_queue *q = bdev_get_queue(bdev);
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800540
Olaf Heringc926b702014-05-21 16:32:42 +0200541 err = xenbus_scanf(XBT_NIL, dev->nodename, "discard-enable", "%d",
542 &discard_enable);
543 if (err == 1 && !discard_enable)
544 return;
545
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400546 if (blk_queue_discard(q)) {
547 err = xenbus_printf(xbt, dev->nodename,
548 "discard-granularity", "%u",
549 q->limits.discard_granularity);
550 if (err) {
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400551 dev_warn(&dev->dev, "writing discard-granularity (%d)", err);
552 return;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800553 }
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400554 err = xenbus_printf(xbt, dev->nodename,
555 "discard-alignment", "%u",
556 q->limits.discard_alignment);
557 if (err) {
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400558 dev_warn(&dev->dev, "writing discard-alignment (%d)", err);
559 return;
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400560 }
561 state = 1;
562 /* Optional. */
563 err = xenbus_printf(xbt, dev->nodename,
564 "discard-secure", "%d",
565 blkif->vbd.discard_secure);
566 if (err) {
Konrad Rzeszutek Wilka71e23d2012-04-16 21:55:04 -0400567 dev_warn(&dev->dev, "writing discard-secure (%d)", err);
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400568 return;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800569 }
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800570 }
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800571 err = xenbus_printf(xbt, dev->nodename, "feature-discard",
572 "%d", state);
573 if (err)
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400574 dev_warn(&dev->dev, "writing feature-discard (%d)", err);
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800575}
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400576int xen_blkbk_barrier(struct xenbus_transaction xbt,
577 struct backend_info *be, int state)
578{
579 struct xenbus_device *dev = be->dev;
580 int err;
581
582 err = xenbus_printf(xbt, dev->nodename, "feature-barrier",
583 "%d", state);
584 if (err)
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400585 dev_warn(&dev->dev, "writing feature-barrier (%d)", err);
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400586
587 return err;
588}
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800589
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400590/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400591 * Entry point to this code when a new device is created. Allocate the basic
592 * structures, and watch the store waiting for the hotplug scripts to tell us
593 * the device's physical major and minor numbers. Switch to InitWait.
594 */
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400595static int xen_blkbk_probe(struct xenbus_device *dev,
596 const struct xenbus_device_id *id)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400597{
598 int err;
599 struct backend_info *be = kzalloc(sizeof(struct backend_info),
600 GFP_KERNEL);
Tao Chen77387b82015-04-01 15:04:22 +0000601
602 /* match the pr_debug in xen_blkbk_remove */
603 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
604
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400605 if (!be) {
606 xenbus_dev_fatal(dev, -ENOMEM,
607 "allocating backend structure");
608 return -ENOMEM;
609 }
610 be->dev = dev;
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800611 dev_set_drvdata(&dev->dev, be);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400612
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400613 be->blkif = xen_blkif_alloc(dev->otherend_id);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400614 if (IS_ERR(be->blkif)) {
615 err = PTR_ERR(be->blkif);
616 be->blkif = NULL;
617 xenbus_dev_fatal(dev, err, "creating block interface");
618 goto fail;
619 }
620
Jan Beulich5a705842016-02-10 04:18:10 -0700621 err = xenbus_printf(XBT_NIL, dev->nodename,
622 "feature-max-indirect-segments", "%u",
623 MAX_INDIRECT_SEGMENTS);
624 if (err)
625 dev_warn(&dev->dev,
626 "writing %s/feature-max-indirect-segments (%d)",
627 dev->nodename, err);
628
Bob Liud62d8602015-11-14 11:12:17 +0800629 /* Multi-queue: advertise how many queues are supported by us.*/
630 err = xenbus_printf(XBT_NIL, dev->nodename,
631 "multi-queue-max-queues", "%u", xenblk_max_queues);
632 if (err)
633 pr_warn("Error writing multi-queue-max-queues\n");
634
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400635 /* setup back pointer */
636 be->blkif->be = be;
637
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -0800638 err = xenbus_watch_pathfmt(dev, &be->backend_watch, backend_changed,
639 "%s/%s", dev->nodename, "physical-device");
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400640 if (err)
641 goto fail;
642
Bob Liu86839c52015-06-03 13:40:03 +0800643 err = xenbus_printf(XBT_NIL, dev->nodename, "max-ring-page-order", "%u",
644 xen_blkif_max_ring_order);
645 if (err)
646 pr_warn("%s write out 'max-ring-page-order' failed\n", __func__);
647
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400648 err = xenbus_switch_state(dev, XenbusStateInitWait);
649 if (err)
650 goto fail;
651
652 return 0;
653
654fail:
Tao Chen77387b82015-04-01 15:04:22 +0000655 pr_warn("%s failed\n", __func__);
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400656 xen_blkbk_remove(dev);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400657 return err;
658}
659
660
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400661/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400662 * Callback received when the hotplug scripts have placed the physical-device
663 * node. Read it and the mode node, and create a vbd. If the frontend is
664 * ready, connect.
665 */
666static void backend_changed(struct xenbus_watch *watch,
667 const char **vec, unsigned int len)
668{
669 int err;
670 unsigned major;
671 unsigned minor;
672 struct backend_info *be
673 = container_of(watch, struct backend_info, backend_watch);
674 struct xenbus_device *dev = be->dev;
675 int cdrom = 0;
Jan Beulich9d092602012-12-20 10:31:11 +0000676 unsigned long handle;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400677 char *device_type;
678
Tao Chen77387b82015-04-01 15:04:22 +0000679 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400680
681 err = xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x:%x",
682 &major, &minor);
683 if (XENBUS_EXIST_ERR(err)) {
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400684 /*
685 * Since this watch will fire once immediately after it is
686 * registered, we expect this. Ignore it, and wait for the
687 * hotplug scripts.
688 */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400689 return;
690 }
691 if (err != 2) {
692 xenbus_dev_fatal(dev, err, "reading physical-device");
693 return;
694 }
695
Jan Beulich9d092602012-12-20 10:31:11 +0000696 if (be->major | be->minor) {
697 if (be->major != major || be->minor != minor)
Tao Chen77387b82015-04-01 15:04:22 +0000698 pr_warn("changing physical device (from %x:%x to %x:%x) not supported.\n",
Jan Beulich9d092602012-12-20 10:31:11 +0000699 be->major, be->minor, major, minor);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400700 return;
701 }
702
703 be->mode = xenbus_read(XBT_NIL, dev->nodename, "mode", NULL);
704 if (IS_ERR(be->mode)) {
705 err = PTR_ERR(be->mode);
706 be->mode = NULL;
707 xenbus_dev_fatal(dev, err, "reading mode");
708 return;
709 }
710
711 device_type = xenbus_read(XBT_NIL, dev->otherend, "device-type", NULL);
712 if (!IS_ERR(device_type)) {
713 cdrom = strcmp(device_type, "cdrom") == 0;
714 kfree(device_type);
715 }
716
Jan Beulich9d092602012-12-20 10:31:11 +0000717 /* Front end dir is a number, which is used as the handle. */
Jingoo Hanbb8e0e82013-09-11 14:20:07 -0700718 err = kstrtoul(strrchr(dev->otherend, '/') + 1, 0, &handle);
Jan Beulich9d092602012-12-20 10:31:11 +0000719 if (err)
720 return;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400721
Jan Beulich9d092602012-12-20 10:31:11 +0000722 be->major = major;
723 be->minor = minor;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400724
Jan Beulich9d092602012-12-20 10:31:11 +0000725 err = xen_vbd_create(be->blkif, handle, major, minor,
726 !strchr(be->mode, 'w'), cdrom);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400727
Jan Beulich9d092602012-12-20 10:31:11 +0000728 if (err)
729 xenbus_dev_fatal(dev, err, "creating vbd structure");
730 else {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400731 err = xenvbd_sysfs_addif(dev);
732 if (err) {
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400733 xen_vbd_free(&be->blkif->vbd);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400734 xenbus_dev_fatal(dev, err, "creating sysfs entries");
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400735 }
Jan Beulich9d092602012-12-20 10:31:11 +0000736 }
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400737
Jan Beulich9d092602012-12-20 10:31:11 +0000738 if (err) {
739 kfree(be->mode);
740 be->mode = NULL;
741 be->major = 0;
742 be->minor = 0;
743 } else {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400744 /* We're potentially connected now */
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400745 xen_update_blkif_status(be->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400746 }
747}
748
749
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400750/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400751 * Callback received when the frontend's state changes.
752 */
753static void frontend_changed(struct xenbus_device *dev,
754 enum xenbus_state frontend_state)
755{
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800756 struct backend_info *be = dev_get_drvdata(&dev->dev);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400757 int err;
758
Tao Chen77387b82015-04-01 15:04:22 +0000759 pr_debug("%s %p %s\n", __func__, dev, xenbus_strstate(frontend_state));
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400760
761 switch (frontend_state) {
762 case XenbusStateInitialising:
763 if (dev->state == XenbusStateClosed) {
Tao Chen77387b82015-04-01 15:04:22 +0000764 pr_info("%s: prepare for reconnect\n", dev->nodename);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400765 xenbus_switch_state(dev, XenbusStateInitWait);
766 }
767 break;
768
769 case XenbusStateInitialised:
770 case XenbusStateConnected:
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400771 /*
772 * Ensure we connect even when two watches fire in
Justin P. Mattock42b2aa82011-11-28 20:31:00 -0800773 * close succession and we miss the intermediate value
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400774 * of frontend_state.
775 */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400776 if (dev->state == XenbusStateConnected)
777 break;
778
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400779 /*
780 * Enforce precondition before potential leak point.
Joe Jin1bc05b02011-08-15 12:57:07 +0800781 * xen_blkif_disconnect() is idempotent.
Keir Fraser313d7b02010-11-24 22:08:20 -0800782 */
Valentin Priescu814d04e2014-05-20 22:28:50 +0200783 err = xen_blkif_disconnect(be->blkif);
784 if (err) {
785 xenbus_dev_fatal(dev, err, "pending I/O");
786 break;
787 }
Keir Fraser313d7b02010-11-24 22:08:20 -0800788
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400789 err = connect_ring(be);
Konrad Rzeszutek Wilk2d0382f2015-11-25 13:20:14 -0500790 if (err) {
791 /*
792 * Clean up so that memory resources can be used by
793 * other devices. connect_ring reported already error.
794 */
795 xen_blkif_disconnect(be->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400796 break;
Konrad Rzeszutek Wilk2d0382f2015-11-25 13:20:14 -0500797 }
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400798 xen_update_blkif_status(be->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400799 break;
800
801 case XenbusStateClosing:
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400802 xenbus_switch_state(dev, XenbusStateClosing);
803 break;
804
805 case XenbusStateClosed:
Joe Jin6f5986b2011-08-15 12:51:31 +0800806 xen_blkif_disconnect(be->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400807 xenbus_switch_state(dev, XenbusStateClosed);
808 if (xenbus_dev_is_online(dev))
809 break;
810 /* fall through if not online */
811 case XenbusStateUnknown:
Joe Jin1bc05b02011-08-15 12:57:07 +0800812 /* implies xen_blkif_disconnect() via xen_blkbk_remove() */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400813 device_unregister(&dev->dev);
814 break;
815
816 default:
817 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
818 frontend_state);
819 break;
820 }
821}
822
823
824/* ** Connection ** */
825
826
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400827/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400828 * Write the physical details regarding the block device to the store, and
829 * switch to Connected state.
830 */
831static void connect(struct backend_info *be)
832{
833 struct xenbus_transaction xbt;
834 int err;
835 struct xenbus_device *dev = be->dev;
836
Tao Chen77387b82015-04-01 15:04:22 +0000837 pr_debug("%s %s\n", __func__, dev->otherend);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400838
839 /* Supply the information about the device the frontend needs */
840again:
841 err = xenbus_transaction_start(&xbt);
842 if (err) {
843 xenbus_dev_fatal(dev, err, "starting transaction");
844 return;
845 }
846
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400847 /* If we can't advertise it is OK. */
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400848 xen_blkbk_flush_diskcache(xbt, be, be->blkif->vbd.flush_support);
849
850 xen_blkbk_discard(xbt, be);
851
852 xen_blkbk_barrier(xbt, be, be->blkif->vbd.flush_support);
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400853
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200854 err = xenbus_printf(xbt, dev->nodename, "feature-persistent", "%u", 1);
855 if (err) {
856 xenbus_dev_fatal(dev, err, "writing %s/feature-persistent",
857 dev->nodename);
858 goto abort;
859 }
860
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400861 err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400862 (unsigned long long)vbd_sz(&be->blkif->vbd));
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400863 if (err) {
864 xenbus_dev_fatal(dev, err, "writing %s/sectors",
865 dev->nodename);
866 goto abort;
867 }
868
869 /* FIXME: use a typename instead */
870 err = xenbus_printf(xbt, dev->nodename, "info", "%u",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400871 be->blkif->vbd.type |
872 (be->blkif->vbd.readonly ? VDISK_READONLY : 0));
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400873 if (err) {
874 xenbus_dev_fatal(dev, err, "writing %s/info",
875 dev->nodename);
876 goto abort;
877 }
878 err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400879 (unsigned long)
880 bdev_logical_block_size(be->blkif->vbd.bdev));
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400881 if (err) {
882 xenbus_dev_fatal(dev, err, "writing %s/sector-size",
883 dev->nodename);
884 goto abort;
885 }
Stefan Bader7c4d7d72013-05-13 16:28:15 +0200886 err = xenbus_printf(xbt, dev->nodename, "physical-sector-size", "%u",
887 bdev_physical_block_size(be->blkif->vbd.bdev));
888 if (err)
889 xenbus_dev_error(dev, err, "writing %s/physical-sector-size",
890 dev->nodename);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400891
892 err = xenbus_transaction_end(xbt, 0);
893 if (err == -EAGAIN)
894 goto again;
895 if (err)
896 xenbus_dev_fatal(dev, err, "ending transaction");
897
898 err = xenbus_switch_state(dev, XenbusStateConnected);
899 if (err)
Joe Perches08b8bfc2011-06-12 09:21:13 -0700900 xenbus_dev_fatal(dev, err, "%s: switching to Connected state",
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400901 dev->nodename);
902
903 return;
904 abort:
905 xenbus_transaction_end(xbt, 1);
906}
907
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500908/*
909 * Each ring may have multi pages, depends on "ring-page-order".
910 */
911static int read_per_ring_refs(struct xen_blkif_ring *ring, const char *dir)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400912{
Julien Grall9cce2912015-10-13 17:50:11 +0100913 unsigned int ring_ref[XENBUS_MAX_RING_GRANTS];
Bob Liu69b91ed2015-06-03 13:40:01 +0800914 struct pending_req *req, *n;
915 int err, i, j;
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500916 struct xen_blkif *blkif = ring->blkif;
917 struct xenbus_device *dev = blkif->be->dev;
918 unsigned int ring_page_order, nr_grefs, evtchn;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400919
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500920 err = xenbus_scanf(XBT_NIL, dir, "event-channel", "%u",
Bob Liu86839c52015-06-03 13:40:03 +0800921 &evtchn);
922 if (err != 1) {
923 err = -EINVAL;
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500924 xenbus_dev_fatal(dev, err, "reading %s/event-channel", dir);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400925 return err;
926 }
Bob Liu86839c52015-06-03 13:40:03 +0800927
928 err = xenbus_scanf(XBT_NIL, dev->otherend, "ring-page-order", "%u",
929 &ring_page_order);
930 if (err != 1) {
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500931 err = xenbus_scanf(XBT_NIL, dir, "ring-ref", "%u", &ring_ref[0]);
Bob Liu86839c52015-06-03 13:40:03 +0800932 if (err != 1) {
933 err = -EINVAL;
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500934 xenbus_dev_fatal(dev, err, "reading %s/ring-ref", dir);
Bob Liu86839c52015-06-03 13:40:03 +0800935 return err;
936 }
937 nr_grefs = 1;
Bob Liu86839c52015-06-03 13:40:03 +0800938 } else {
939 unsigned int i;
940
941 if (ring_page_order > xen_blkif_max_ring_order) {
942 err = -EINVAL;
943 xenbus_dev_fatal(dev, err, "%s/request %d ring page order exceed max:%d",
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500944 dir, ring_page_order,
Bob Liu86839c52015-06-03 13:40:03 +0800945 xen_blkif_max_ring_order);
946 return err;
947 }
948
949 nr_grefs = 1 << ring_page_order;
950 for (i = 0; i < nr_grefs; i++) {
951 char ring_ref_name[RINGREF_NAME_LEN];
952
953 snprintf(ring_ref_name, RINGREF_NAME_LEN, "ring-ref%u", i);
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500954 err = xenbus_scanf(XBT_NIL, dir, ring_ref_name,
Bob Liu86839c52015-06-03 13:40:03 +0800955 "%u", &ring_ref[i]);
956 if (err != 1) {
957 err = -EINVAL;
958 xenbus_dev_fatal(dev, err, "reading %s/%s",
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500959 dir, ring_ref_name);
Bob Liu86839c52015-06-03 13:40:03 +0800960 return err;
961 }
Bob Liu86839c52015-06-03 13:40:03 +0800962 }
963 }
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -0500964 blkif->nr_ring_pages = nr_grefs;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400965
Bob Liu86839c52015-06-03 13:40:03 +0800966 for (i = 0; i < nr_grefs * XEN_BLKIF_REQS_PER_PAGE; i++) {
Bob Liu69b91ed2015-06-03 13:40:01 +0800967 req = kzalloc(sizeof(*req), GFP_KERNEL);
968 if (!req)
969 goto fail;
Bob Liu59795702015-11-14 11:12:15 +0800970 list_add_tail(&req->free_list, &ring->pending_free);
Bob Liu69b91ed2015-06-03 13:40:01 +0800971 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++) {
972 req->segments[j] = kzalloc(sizeof(*req->segments[0]), GFP_KERNEL);
973 if (!req->segments[j])
974 goto fail;
975 }
976 for (j = 0; j < MAX_INDIRECT_PAGES; j++) {
977 req->indirect_pages[j] = kzalloc(sizeof(*req->indirect_pages[0]),
978 GFP_KERNEL);
979 if (!req->indirect_pages[j])
980 goto fail;
981 }
982 }
983
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400984 /* Map the shared frame, irq etc. */
Bob Liu59795702015-11-14 11:12:15 +0800985 err = xen_blkif_map(ring, ring_ref, nr_grefs, evtchn);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400986 if (err) {
Bob Liu86839c52015-06-03 13:40:03 +0800987 xenbus_dev_fatal(dev, err, "mapping ring-ref port %u", evtchn);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400988 return err;
989 }
990
991 return 0;
Bob Liu69b91ed2015-06-03 13:40:01 +0800992
993fail:
Bob Liu59795702015-11-14 11:12:15 +0800994 list_for_each_entry_safe(req, n, &ring->pending_free, free_list) {
Bob Liu69b91ed2015-06-03 13:40:01 +0800995 list_del(&req->free_list);
996 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++) {
997 if (!req->segments[j])
998 break;
999 kfree(req->segments[j]);
1000 }
1001 for (j = 0; j < MAX_INDIRECT_PAGES; j++) {
1002 if (!req->indirect_pages[j])
1003 break;
1004 kfree(req->indirect_pages[j]);
1005 }
1006 kfree(req);
1007 }
1008 return -ENOMEM;
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -05001009
1010}
1011
1012static int connect_ring(struct backend_info *be)
1013{
1014 struct xenbus_device *dev = be->dev;
1015 unsigned int pers_grants;
1016 char protocol[64] = "";
1017 int err, i;
1018 char *xspath;
1019 size_t xspathsize;
1020 const size_t xenstore_path_ext_size = 11; /* sufficient for "/queue-NNN" */
Bob Liud62d8602015-11-14 11:12:17 +08001021 unsigned int requested_num_queues = 0;
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -05001022
1023 pr_debug("%s %s\n", __func__, dev->otherend);
1024
1025 be->blkif->blk_protocol = BLKIF_PROTOCOL_DEFAULT;
1026 err = xenbus_gather(XBT_NIL, dev->otherend, "protocol",
1027 "%63s", protocol, NULL);
1028 if (err)
1029 strcpy(protocol, "unspecified, assuming default");
1030 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_NATIVE))
1031 be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
1032 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_32))
1033 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_32;
1034 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_64))
1035 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_64;
1036 else {
1037 xenbus_dev_fatal(dev, err, "unknown fe protocol %s", protocol);
Konrad Rzeszutek Wilkbde21f72015-11-25 13:07:39 -05001038 return -ENOSYS;
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -05001039 }
1040 err = xenbus_gather(XBT_NIL, dev->otherend,
1041 "feature-persistent", "%u",
1042 &pers_grants, NULL);
1043 if (err)
1044 pers_grants = 0;
1045
1046 be->blkif->vbd.feature_gnt_persistent = pers_grants;
1047 be->blkif->vbd.overflow_max_grants = 0;
1048
Bob Liud62d8602015-11-14 11:12:17 +08001049 /*
1050 * Read the number of hardware queues from frontend.
1051 */
1052 err = xenbus_scanf(XBT_NIL, dev->otherend, "multi-queue-num-queues",
1053 "%u", &requested_num_queues);
1054 if (err < 0) {
1055 requested_num_queues = 1;
1056 } else {
1057 if (requested_num_queues > xenblk_max_queues
1058 || requested_num_queues == 0) {
1059 /* Buggy or malicious guest. */
1060 xenbus_dev_fatal(dev, err,
1061 "guest requested %u queues, exceeding the maximum of %u.",
1062 requested_num_queues, xenblk_max_queues);
1063 return -ENOSYS;
1064 }
1065 }
1066 be->blkif->nr_rings = requested_num_queues;
1067 if (xen_blkif_alloc_rings(be->blkif))
1068 return -ENOMEM;
1069
Konrad Rzeszutek Wilk2fb1ef42015-12-11 12:08:48 -05001070 pr_info("%s: using %d queues, protocol %d (%s) %s\n", dev->nodename,
1071 be->blkif->nr_rings, be->blkif->blk_protocol, protocol,
1072 pers_grants ? "persistent grants" : "");
1073
1074 if (be->blkif->nr_rings == 1)
1075 return read_per_ring_refs(&be->blkif->rings[0], dev->otherend);
1076 else {
1077 xspathsize = strlen(dev->otherend) + xenstore_path_ext_size;
1078 xspath = kmalloc(xspathsize, GFP_KERNEL);
1079 if (!xspath) {
1080 xenbus_dev_fatal(dev, -ENOMEM, "reading ring references");
1081 return -ENOMEM;
1082 }
1083
1084 for (i = 0; i < be->blkif->nr_rings; i++) {
1085 memset(xspath, 0, xspathsize);
1086 snprintf(xspath, xspathsize, "%s/queue-%u", dev->otherend, i);
1087 err = read_per_ring_refs(&be->blkif->rings[i], xspath);
1088 if (err) {
1089 kfree(xspath);
1090 return err;
1091 }
1092 }
1093 kfree(xspath);
1094 }
1095 return 0;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001096}
1097
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001098static const struct xenbus_device_id xen_blkbk_ids[] = {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001099 { "vbd" },
1100 { "" }
1101};
1102
David Vrabel95afae42014-09-08 17:30:41 +01001103static struct xenbus_driver xen_blkbk_driver = {
1104 .ids = xen_blkbk_ids,
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001105 .probe = xen_blkbk_probe,
1106 .remove = xen_blkbk_remove,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001107 .otherend_changed = frontend_changed
David Vrabel95afae42014-09-08 17:30:41 +01001108};
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001109
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -04001110int xen_blkif_xenbus_init(void)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001111{
Jan Beulich73db1442011-12-22 09:08:13 +00001112 return xenbus_register_backend(&xen_blkbk_driver);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001113}