blob: 630a489e757d61eb9fdb2b9f928ac77898a8dd48 [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
17#include <stdarg.h>
18#include <linux/module.h>
19#include <linux/kthread.h>
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -040020#include <xen/events.h>
21#include <xen/grant_table.h>
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040022#include "common.h"
23
Konrad Rzeszutek Wilkd6091b22011-04-14 17:33:30 -040024struct backend_info {
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -040025 struct xenbus_device *dev;
Konrad Rzeszutek Wilk51854322011-05-12 18:02:28 -040026 struct xen_blkif *blkif;
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -040027 struct xenbus_watch backend_watch;
28 unsigned major;
29 unsigned minor;
30 char *mode;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040031};
32
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -040033static struct kmem_cache *xen_blkif_cachep;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040034static void connect(struct backend_info *);
35static int connect_ring(struct backend_info *);
36static void backend_changed(struct xenbus_watch *, const char **,
37 unsigned int);
Valentin Priescu814d04e2014-05-20 22:28:50 +020038static void xen_blkif_free(struct xen_blkif *blkif);
39static void xen_vbd_free(struct xen_vbd *vbd);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040040
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -040041struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be)
Jeremy Fitzhardinge98e036a2010-03-18 15:35:05 -070042{
43 return be->dev;
44}
45
Valentin Priescu814d04e2014-05-20 22:28:50 +020046/*
47 * The last request could free the device from softirq context and
48 * xen_blkif_free() can sleep.
49 */
50static void xen_blkif_deferred_free(struct work_struct *work)
51{
52 struct xen_blkif *blkif;
53
54 blkif = container_of(work, struct xen_blkif, free_work);
55 xen_blkif_free(blkif);
56}
57
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -040058static int blkback_name(struct xen_blkif *blkif, char *buf)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040059{
60 char *devpath, *devname;
61 struct xenbus_device *dev = blkif->be->dev;
62
63 devpath = xenbus_read(XBT_NIL, dev->nodename, "dev", NULL);
64 if (IS_ERR(devpath))
65 return PTR_ERR(devpath);
66
Konrad Rzeszutek Wilkd6091b22011-04-14 17:33:30 -040067 devname = strstr(devpath, "/dev/");
68 if (devname != NULL)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040069 devname += strlen("/dev/");
70 else
71 devname = devpath;
72
73 snprintf(buf, TASK_COMM_LEN, "blkback.%d.%s", blkif->domid, devname);
74 kfree(devpath);
75
76 return 0;
77}
78
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -040079static void xen_update_blkif_status(struct xen_blkif *blkif)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040080{
81 int err;
82 char name[TASK_COMM_LEN];
83
84 /* Not ready to connect? */
85 if (!blkif->irq || !blkif->vbd.bdev)
86 return;
87
88 /* Already connected? */
89 if (blkif->be->dev->state == XenbusStateConnected)
90 return;
91
92 /* Attempt to connect: exit if we fail to. */
93 connect(blkif->be);
94 if (blkif->be->dev->state != XenbusStateConnected)
95 return;
96
97 err = blkback_name(blkif, name);
98 if (err) {
99 xenbus_dev_error(blkif->be->dev, err, "get blkback dev name");
100 return;
101 }
102
Chris Lalancettecbf46292010-07-21 12:41:45 -0700103 err = filemap_write_and_wait(blkif->vbd.bdev->bd_inode->i_mapping);
104 if (err) {
105 xenbus_dev_error(blkif->be->dev, err, "block flush");
106 return;
107 }
108 invalidate_inode_pages2(blkif->vbd.bdev->bd_inode->i_mapping);
109
Kees Cookf1701682013-07-03 15:04:58 -0700110 blkif->xenblkd = kthread_run(xen_blkif_schedule, blkif, "%s", name);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400111 if (IS_ERR(blkif->xenblkd)) {
112 err = PTR_ERR(blkif->xenblkd);
113 blkif->xenblkd = NULL;
114 xenbus_dev_error(blkif->be->dev, err, "start xenblkd");
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200115 return;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400116 }
117}
118
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400119static struct xen_blkif *xen_blkif_alloc(domid_t domid)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400120{
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400121 struct xen_blkif *blkif;
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200122 struct pending_req *req, *n;
123 int i, j;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400124
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200125 BUILD_BUG_ON(MAX_INDIRECT_PAGES > BLKIF_MAX_INDIRECT_PAGES_PER_REQUEST);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400126
Wei Yongjun654dbef2012-08-27 12:28:57 +0800127 blkif = kmem_cache_zalloc(xen_blkif_cachep, GFP_KERNEL);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400128 if (!blkif)
129 return ERR_PTR(-ENOMEM);
130
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400131 blkif->domid = domid;
132 spin_lock_init(&blkif->blk_ring_lock);
133 atomic_set(&blkif->refcnt, 1);
134 init_waitqueue_head(&blkif->wq);
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400135 init_completion(&blkif->drain_complete);
136 atomic_set(&blkif->drain, 0);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400137 blkif->st_print = jiffies;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200138 blkif->persistent_gnts.rb_node = NULL;
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200139 spin_lock_init(&blkif->free_pages_lock);
140 INIT_LIST_HEAD(&blkif->free_pages);
Roger Pau Monneef753412014-02-04 11:26:13 +0100141 INIT_LIST_HEAD(&blkif->persistent_purge_list);
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200142 blkif->free_pages_num = 0;
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200143 atomic_set(&blkif->persistent_gnt_in_use, 0);
Roger Pau Monnec05f3e32014-02-04 11:26:14 +0100144 atomic_set(&blkif->inflight, 0);
Roger Pau Monneabb97b82014-02-11 20:34:03 -0700145 INIT_WORK(&blkif->persistent_purge_work, xen_blkbk_unmap_purged_grants);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400146
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200147 INIT_LIST_HEAD(&blkif->pending_free);
Valentin Priescu814d04e2014-05-20 22:28:50 +0200148 INIT_WORK(&blkif->free_work, xen_blkif_deferred_free);
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200149
150 for (i = 0; i < XEN_BLKIF_REQS; i++) {
151 req = kzalloc(sizeof(*req), GFP_KERNEL);
152 if (!req)
153 goto fail;
154 list_add_tail(&req->free_list,
155 &blkif->pending_free);
156 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++) {
157 req->segments[j] = kzalloc(sizeof(*req->segments[0]),
158 GFP_KERNEL);
159 if (!req->segments[j])
160 goto fail;
161 }
162 for (j = 0; j < MAX_INDIRECT_PAGES; j++) {
163 req->indirect_pages[j] = kzalloc(sizeof(*req->indirect_pages[0]),
164 GFP_KERNEL);
165 if (!req->indirect_pages[j])
166 goto fail;
167 }
168 }
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200169 spin_lock_init(&blkif->pending_free_lock);
170 init_waitqueue_head(&blkif->pending_free_wq);
Konrad Rzeszutek Wilk8e3f8752013-01-23 16:54:32 -0500171 init_waitqueue_head(&blkif->shutdown_wq);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400172
173 return blkif;
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200174
175fail:
176 list_for_each_entry_safe(req, n, &blkif->pending_free, free_list) {
177 list_del(&req->free_list);
178 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++) {
179 if (!req->segments[j])
180 break;
181 kfree(req->segments[j]);
182 }
183 for (j = 0; j < MAX_INDIRECT_PAGES; j++) {
184 if (!req->indirect_pages[j])
185 break;
186 kfree(req->indirect_pages[j]);
187 }
188 kfree(req);
189 }
190
191 kmem_cache_free(xen_blkif_cachep, blkif);
192
193 return ERR_PTR(-ENOMEM);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400194}
195
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400196static int xen_blkif_map(struct xen_blkif *blkif, unsigned long shared_page,
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400197 unsigned int evtchn)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400198{
199 int err;
200
201 /* Already connected through? */
202 if (blkif->irq)
203 return 0;
204
David Vrabel2d073842011-09-29 16:53:30 +0100205 err = xenbus_map_ring_valloc(blkif->be->dev, shared_page, &blkif->blk_ring);
206 if (err < 0)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400207 return err;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400208
209 switch (blkif->blk_protocol) {
210 case BLKIF_PROTOCOL_NATIVE:
211 {
212 struct blkif_sring *sring;
David Vrabel2d073842011-09-29 16:53:30 +0100213 sring = (struct blkif_sring *)blkif->blk_ring;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400214 BACK_RING_INIT(&blkif->blk_rings.native, sring, PAGE_SIZE);
215 break;
216 }
217 case BLKIF_PROTOCOL_X86_32:
218 {
219 struct blkif_x86_32_sring *sring_x86_32;
David Vrabel2d073842011-09-29 16:53:30 +0100220 sring_x86_32 = (struct blkif_x86_32_sring *)blkif->blk_ring;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400221 BACK_RING_INIT(&blkif->blk_rings.x86_32, sring_x86_32, PAGE_SIZE);
222 break;
223 }
224 case BLKIF_PROTOCOL_X86_64:
225 {
226 struct blkif_x86_64_sring *sring_x86_64;
David Vrabel2d073842011-09-29 16:53:30 +0100227 sring_x86_64 = (struct blkif_x86_64_sring *)blkif->blk_ring;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400228 BACK_RING_INIT(&blkif->blk_rings.x86_64, sring_x86_64, PAGE_SIZE);
229 break;
230 }
231 default:
232 BUG();
233 }
234
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400235 err = bind_interdomain_evtchn_to_irqhandler(blkif->domid, evtchn,
236 xen_blkif_be_int, 0,
237 "blkif-backend", blkif);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400238 if (err < 0) {
David Vrabel2d073842011-09-29 16:53:30 +0100239 xenbus_unmap_ring_vfree(blkif->be->dev, blkif->blk_ring);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400240 blkif->blk_rings.common.sring = NULL;
241 return err;
242 }
243 blkif->irq = err;
244
245 return 0;
246}
247
Valentin Priescu814d04e2014-05-20 22:28:50 +0200248static int xen_blkif_disconnect(struct xen_blkif *blkif)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400249{
250 if (blkif->xenblkd) {
251 kthread_stop(blkif->xenblkd);
Konrad Rzeszutek Wilk8e3f8752013-01-23 16:54:32 -0500252 wake_up(&blkif->shutdown_wq);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400253 blkif->xenblkd = NULL;
254 }
255
Valentin Priescu814d04e2014-05-20 22:28:50 +0200256 /* The above kthread_stop() guarantees that at this point we
257 * don't have any discard_io or other_io requests. So, checking
258 * for inflight IO is enough.
259 */
260 if (atomic_read(&blkif->inflight) > 0)
261 return -EBUSY;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400262
263 if (blkif->irq) {
264 unbind_from_irqhandler(blkif->irq, blkif);
265 blkif->irq = 0;
266 }
267
268 if (blkif->blk_rings.common.sring) {
David Vrabel2d073842011-09-29 16:53:30 +0100269 xenbus_unmap_ring_vfree(blkif->be->dev, blkif->blk_ring);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400270 blkif->blk_rings.common.sring = NULL;
271 }
Valentin Priescu814d04e2014-05-20 22:28:50 +0200272
Vitaly Kuznetsov12ea7292014-09-08 15:21:33 +0200273 /* Remove all persistent grants and the cache of ballooned pages. */
274 xen_blkbk_free_caches(blkif);
275
Valentin Priescu814d04e2014-05-20 22:28:50 +0200276 return 0;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400277}
278
Konrad Rzeszutek Wilk29117582012-08-13 10:53:17 -0400279static void xen_blkif_free(struct xen_blkif *blkif)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400280{
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200281 struct pending_req *req, *n;
282 int i = 0, j;
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200283
Valentin Priescu814d04e2014-05-20 22:28:50 +0200284 xen_blkif_disconnect(blkif);
285 xen_vbd_free(&blkif->vbd);
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200286
Roger Pau Monneef753412014-02-04 11:26:13 +0100287 /* Make sure everything is drained before shutting down */
288 BUG_ON(blkif->persistent_gnt_c != 0);
289 BUG_ON(atomic_read(&blkif->persistent_gnt_in_use) != 0);
290 BUG_ON(blkif->free_pages_num != 0);
291 BUG_ON(!list_empty(&blkif->persistent_purge_list));
292 BUG_ON(!list_empty(&blkif->free_pages));
293 BUG_ON(!RB_EMPTY_ROOT(&blkif->persistent_gnts));
294
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200295 /* Check that there is no request in use */
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200296 list_for_each_entry_safe(req, n, &blkif->pending_free, free_list) {
297 list_del(&req->free_list);
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200298
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200299 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++)
300 kfree(req->segments[j]);
301
302 for (j = 0; j < MAX_INDIRECT_PAGES; j++)
303 kfree(req->indirect_pages[j]);
304
305 kfree(req);
306 i++;
307 }
308
309 WARN_ON(i != XEN_BLKIF_REQS);
310
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400311 kmem_cache_free(xen_blkif_cachep, blkif);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400312}
313
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400314int __init xen_blkif_interface_init(void)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400315{
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400316 xen_blkif_cachep = kmem_cache_create("blkif_cache",
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400317 sizeof(struct xen_blkif),
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400318 0, 0, NULL);
319 if (!xen_blkif_cachep)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400320 return -ENOMEM;
321
322 return 0;
323}
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400324
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400325/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400326 * sysfs interface for VBD I/O requests
327 */
328
329#define VBD_SHOW(name, format, args...) \
330 static ssize_t show_##name(struct device *_dev, \
331 struct device_attribute *attr, \
332 char *buf) \
333 { \
334 struct xenbus_device *dev = to_xenbus_device(_dev); \
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800335 struct backend_info *be = dev_get_drvdata(&dev->dev); \
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400336 \
337 return sprintf(buf, format, ##args); \
338 } \
339 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
340
Zoltan Kiss986cacb2013-03-11 16:15:50 +0000341VBD_SHOW(oo_req, "%llu\n", be->blkif->st_oo_req);
342VBD_SHOW(rd_req, "%llu\n", be->blkif->st_rd_req);
343VBD_SHOW(wr_req, "%llu\n", be->blkif->st_wr_req);
344VBD_SHOW(f_req, "%llu\n", be->blkif->st_f_req);
345VBD_SHOW(ds_req, "%llu\n", be->blkif->st_ds_req);
346VBD_SHOW(rd_sect, "%llu\n", be->blkif->st_rd_sect);
347VBD_SHOW(wr_sect, "%llu\n", be->blkif->st_wr_sect);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400348
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400349static struct attribute *xen_vbdstat_attrs[] = {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400350 &dev_attr_oo_req.attr,
351 &dev_attr_rd_req.attr,
352 &dev_attr_wr_req.attr,
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400353 &dev_attr_f_req.attr,
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800354 &dev_attr_ds_req.attr,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400355 &dev_attr_rd_sect.attr,
356 &dev_attr_wr_sect.attr,
357 NULL
358};
359
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400360static struct attribute_group xen_vbdstat_group = {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400361 .name = "statistics",
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400362 .attrs = xen_vbdstat_attrs,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400363};
364
365VBD_SHOW(physical_device, "%x:%x\n", be->major, be->minor);
366VBD_SHOW(mode, "%s\n", be->mode);
367
Konrad Rzeszutek Wilk29117582012-08-13 10:53:17 -0400368static int xenvbd_sysfs_addif(struct xenbus_device *dev)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400369{
370 int error;
371
372 error = device_create_file(&dev->dev, &dev_attr_physical_device);
Konrad Rzeszutek Wilkd6091b22011-04-14 17:33:30 -0400373 if (error)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400374 goto fail1;
375
376 error = device_create_file(&dev->dev, &dev_attr_mode);
377 if (error)
378 goto fail2;
379
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400380 error = sysfs_create_group(&dev->dev.kobj, &xen_vbdstat_group);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400381 if (error)
382 goto fail3;
383
384 return 0;
385
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400386fail3: sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400387fail2: device_remove_file(&dev->dev, &dev_attr_mode);
388fail1: device_remove_file(&dev->dev, &dev_attr_physical_device);
389 return error;
390}
391
Konrad Rzeszutek Wilk29117582012-08-13 10:53:17 -0400392static void xenvbd_sysfs_delif(struct xenbus_device *dev)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400393{
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400394 sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400395 device_remove_file(&dev->dev, &dev_attr_mode);
396 device_remove_file(&dev->dev, &dev_attr_physical_device);
397}
398
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400399
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400400static void xen_vbd_free(struct xen_vbd *vbd)
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400401{
402 if (vbd->bdev)
403 blkdev_put(vbd->bdev, vbd->readonly ? FMODE_READ : FMODE_WRITE);
404 vbd->bdev = NULL;
405}
406
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400407static int xen_vbd_create(struct xen_blkif *blkif, blkif_vdev_t handle,
408 unsigned major, unsigned minor, int readonly,
409 int cdrom)
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400410{
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400411 struct xen_vbd *vbd;
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400412 struct block_device *bdev;
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400413 struct request_queue *q;
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400414
415 vbd = &blkif->vbd;
416 vbd->handle = handle;
417 vbd->readonly = readonly;
418 vbd->type = 0;
419
420 vbd->pdevice = MKDEV(major, minor);
421
422 bdev = blkdev_get_by_dev(vbd->pdevice, vbd->readonly ?
423 FMODE_READ : FMODE_WRITE, NULL);
424
425 if (IS_ERR(bdev)) {
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400426 DPRINTK("xen_vbd_create: device %08x could not be opened.\n",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400427 vbd->pdevice);
428 return -ENOENT;
429 }
430
431 vbd->bdev = bdev;
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400432 if (vbd->bdev->bd_disk == NULL) {
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400433 DPRINTK("xen_vbd_create: device %08x doesn't exist.\n",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400434 vbd->pdevice);
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400435 xen_vbd_free(vbd);
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400436 return -ENOENT;
437 }
Laszlo Ersek64649202011-05-25 12:24:25 +0200438 vbd->size = vbd_sz(vbd);
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400439
440 if (vbd->bdev->bd_disk->flags & GENHD_FL_CD || cdrom)
441 vbd->type |= VDISK_CDROM;
442 if (vbd->bdev->bd_disk->flags & GENHD_FL_REMOVABLE)
443 vbd->type |= VDISK_REMOVABLE;
444
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400445 q = bdev_get_queue(bdev);
446 if (q && q->flush_flags)
447 vbd->flush_support = true;
448
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400449 if (q && blk_queue_secdiscard(q))
450 vbd->discard_secure = true;
451
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400452 DPRINTK("Successful creation of handle=%04x (dom=%u)\n",
453 handle, blkif->domid);
454 return 0;
455}
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400456static int xen_blkbk_remove(struct xenbus_device *dev)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400457{
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800458 struct backend_info *be = dev_get_drvdata(&dev->dev);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400459
460 DPRINTK("");
461
462 if (be->major || be->minor)
463 xenvbd_sysfs_delif(dev);
464
465 if (be->backend_watch.node) {
466 unregister_xenbus_watch(&be->backend_watch);
467 kfree(be->backend_watch.node);
468 be->backend_watch.node = NULL;
469 }
470
Valentin Priescu814d04e2014-05-20 22:28:50 +0200471 dev_set_drvdata(&dev->dev, NULL);
472
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400473 if (be->blkif) {
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400474 xen_blkif_disconnect(be->blkif);
Valentin Priescu814d04e2014-05-20 22:28:50 +0200475 xen_blkif_put(be->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400476 }
477
Jan Beulich9d092602012-12-20 10:31:11 +0000478 kfree(be->mode);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400479 kfree(be);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400480 return 0;
481}
482
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400483int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt,
484 struct backend_info *be, int state)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400485{
486 struct xenbus_device *dev = be->dev;
487 int err;
488
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400489 err = xenbus_printf(xbt, dev->nodename, "feature-flush-cache",
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400490 "%d", state);
491 if (err)
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400492 dev_warn(&dev->dev, "writing feature-flush-cache (%d)", err);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400493
494 return err;
495}
496
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400497static void xen_blkbk_discard(struct xenbus_transaction xbt, struct backend_info *be)
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800498{
499 struct xenbus_device *dev = be->dev;
500 struct xen_blkif *blkif = be->blkif;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800501 int err;
Olaf Heringc926b702014-05-21 16:32:42 +0200502 int state = 0, discard_enable;
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400503 struct block_device *bdev = be->blkif->vbd.bdev;
504 struct request_queue *q = bdev_get_queue(bdev);
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800505
Olaf Heringc926b702014-05-21 16:32:42 +0200506 err = xenbus_scanf(XBT_NIL, dev->nodename, "discard-enable", "%d",
507 &discard_enable);
508 if (err == 1 && !discard_enable)
509 return;
510
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400511 if (blk_queue_discard(q)) {
512 err = xenbus_printf(xbt, dev->nodename,
513 "discard-granularity", "%u",
514 q->limits.discard_granularity);
515 if (err) {
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400516 dev_warn(&dev->dev, "writing discard-granularity (%d)", err);
517 return;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800518 }
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400519 err = xenbus_printf(xbt, dev->nodename,
520 "discard-alignment", "%u",
521 q->limits.discard_alignment);
522 if (err) {
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400523 dev_warn(&dev->dev, "writing discard-alignment (%d)", err);
524 return;
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400525 }
526 state = 1;
527 /* Optional. */
528 err = xenbus_printf(xbt, dev->nodename,
529 "discard-secure", "%d",
530 blkif->vbd.discard_secure);
531 if (err) {
Konrad Rzeszutek Wilka71e23d2012-04-16 21:55:04 -0400532 dev_warn(&dev->dev, "writing discard-secure (%d)", err);
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400533 return;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800534 }
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800535 }
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800536 err = xenbus_printf(xbt, dev->nodename, "feature-discard",
537 "%d", state);
538 if (err)
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400539 dev_warn(&dev->dev, "writing feature-discard (%d)", err);
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800540}
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400541int xen_blkbk_barrier(struct xenbus_transaction xbt,
542 struct backend_info *be, int state)
543{
544 struct xenbus_device *dev = be->dev;
545 int err;
546
547 err = xenbus_printf(xbt, dev->nodename, "feature-barrier",
548 "%d", state);
549 if (err)
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400550 dev_warn(&dev->dev, "writing feature-barrier (%d)", err);
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400551
552 return err;
553}
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800554
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400555/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400556 * Entry point to this code when a new device is created. Allocate the basic
557 * structures, and watch the store waiting for the hotplug scripts to tell us
558 * the device's physical major and minor numbers. Switch to InitWait.
559 */
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400560static int xen_blkbk_probe(struct xenbus_device *dev,
561 const struct xenbus_device_id *id)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400562{
563 int err;
564 struct backend_info *be = kzalloc(sizeof(struct backend_info),
565 GFP_KERNEL);
566 if (!be) {
567 xenbus_dev_fatal(dev, -ENOMEM,
568 "allocating backend structure");
569 return -ENOMEM;
570 }
571 be->dev = dev;
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800572 dev_set_drvdata(&dev->dev, be);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400573
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400574 be->blkif = xen_blkif_alloc(dev->otherend_id);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400575 if (IS_ERR(be->blkif)) {
576 err = PTR_ERR(be->blkif);
577 be->blkif = NULL;
578 xenbus_dev_fatal(dev, err, "creating block interface");
579 goto fail;
580 }
581
582 /* setup back pointer */
583 be->blkif->be = be;
584
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -0800585 err = xenbus_watch_pathfmt(dev, &be->backend_watch, backend_changed,
586 "%s/%s", dev->nodename, "physical-device");
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400587 if (err)
588 goto fail;
589
590 err = xenbus_switch_state(dev, XenbusStateInitWait);
591 if (err)
592 goto fail;
593
594 return 0;
595
596fail:
597 DPRINTK("failed");
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400598 xen_blkbk_remove(dev);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400599 return err;
600}
601
602
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400603/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400604 * Callback received when the hotplug scripts have placed the physical-device
605 * node. Read it and the mode node, and create a vbd. If the frontend is
606 * ready, connect.
607 */
608static void backend_changed(struct xenbus_watch *watch,
609 const char **vec, unsigned int len)
610{
611 int err;
612 unsigned major;
613 unsigned minor;
614 struct backend_info *be
615 = container_of(watch, struct backend_info, backend_watch);
616 struct xenbus_device *dev = be->dev;
617 int cdrom = 0;
Jan Beulich9d092602012-12-20 10:31:11 +0000618 unsigned long handle;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400619 char *device_type;
620
621 DPRINTK("");
622
623 err = xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x:%x",
624 &major, &minor);
625 if (XENBUS_EXIST_ERR(err)) {
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400626 /*
627 * Since this watch will fire once immediately after it is
628 * registered, we expect this. Ignore it, and wait for the
629 * hotplug scripts.
630 */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400631 return;
632 }
633 if (err != 2) {
634 xenbus_dev_fatal(dev, err, "reading physical-device");
635 return;
636 }
637
Jan Beulich9d092602012-12-20 10:31:11 +0000638 if (be->major | be->minor) {
639 if (be->major != major || be->minor != minor)
640 pr_warn(DRV_PFX "changing physical device (from %x:%x to %x:%x) not supported.\n",
641 be->major, be->minor, major, minor);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400642 return;
643 }
644
645 be->mode = xenbus_read(XBT_NIL, dev->nodename, "mode", NULL);
646 if (IS_ERR(be->mode)) {
647 err = PTR_ERR(be->mode);
648 be->mode = NULL;
649 xenbus_dev_fatal(dev, err, "reading mode");
650 return;
651 }
652
653 device_type = xenbus_read(XBT_NIL, dev->otherend, "device-type", NULL);
654 if (!IS_ERR(device_type)) {
655 cdrom = strcmp(device_type, "cdrom") == 0;
656 kfree(device_type);
657 }
658
Jan Beulich9d092602012-12-20 10:31:11 +0000659 /* Front end dir is a number, which is used as the handle. */
Jingoo Hanbb8e0e82013-09-11 14:20:07 -0700660 err = kstrtoul(strrchr(dev->otherend, '/') + 1, 0, &handle);
Jan Beulich9d092602012-12-20 10:31:11 +0000661 if (err)
662 return;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400663
Jan Beulich9d092602012-12-20 10:31:11 +0000664 be->major = major;
665 be->minor = minor;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400666
Jan Beulich9d092602012-12-20 10:31:11 +0000667 err = xen_vbd_create(be->blkif, handle, major, minor,
668 !strchr(be->mode, 'w'), cdrom);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400669
Jan Beulich9d092602012-12-20 10:31:11 +0000670 if (err)
671 xenbus_dev_fatal(dev, err, "creating vbd structure");
672 else {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400673 err = xenvbd_sysfs_addif(dev);
674 if (err) {
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400675 xen_vbd_free(&be->blkif->vbd);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400676 xenbus_dev_fatal(dev, err, "creating sysfs entries");
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400677 }
Jan Beulich9d092602012-12-20 10:31:11 +0000678 }
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400679
Jan Beulich9d092602012-12-20 10:31:11 +0000680 if (err) {
681 kfree(be->mode);
682 be->mode = NULL;
683 be->major = 0;
684 be->minor = 0;
685 } else {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400686 /* We're potentially connected now */
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400687 xen_update_blkif_status(be->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400688 }
689}
690
691
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400692/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400693 * Callback received when the frontend's state changes.
694 */
695static void frontend_changed(struct xenbus_device *dev,
696 enum xenbus_state frontend_state)
697{
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800698 struct backend_info *be = dev_get_drvdata(&dev->dev);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400699 int err;
700
701 DPRINTK("%s", xenbus_strstate(frontend_state));
702
703 switch (frontend_state) {
704 case XenbusStateInitialising:
705 if (dev->state == XenbusStateClosed) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400706 pr_info(DRV_PFX "%s: prepare for reconnect\n",
Konrad Rzeszutek Wilkebe81902011-05-12 16:42:31 -0400707 dev->nodename);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400708 xenbus_switch_state(dev, XenbusStateInitWait);
709 }
710 break;
711
712 case XenbusStateInitialised:
713 case XenbusStateConnected:
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400714 /*
715 * Ensure we connect even when two watches fire in
Justin P. Mattock42b2aa82011-11-28 20:31:00 -0800716 * close succession and we miss the intermediate value
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400717 * of frontend_state.
718 */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400719 if (dev->state == XenbusStateConnected)
720 break;
721
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400722 /*
723 * Enforce precondition before potential leak point.
Joe Jin1bc05b02011-08-15 12:57:07 +0800724 * xen_blkif_disconnect() is idempotent.
Keir Fraser313d7b02010-11-24 22:08:20 -0800725 */
Valentin Priescu814d04e2014-05-20 22:28:50 +0200726 err = xen_blkif_disconnect(be->blkif);
727 if (err) {
728 xenbus_dev_fatal(dev, err, "pending I/O");
729 break;
730 }
Keir Fraser313d7b02010-11-24 22:08:20 -0800731
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400732 err = connect_ring(be);
733 if (err)
734 break;
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400735 xen_update_blkif_status(be->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400736 break;
737
738 case XenbusStateClosing:
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400739 xenbus_switch_state(dev, XenbusStateClosing);
740 break;
741
742 case XenbusStateClosed:
Joe Jin6f5986b2011-08-15 12:51:31 +0800743 xen_blkif_disconnect(be->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400744 xenbus_switch_state(dev, XenbusStateClosed);
745 if (xenbus_dev_is_online(dev))
746 break;
747 /* fall through if not online */
748 case XenbusStateUnknown:
Joe Jin1bc05b02011-08-15 12:57:07 +0800749 /* implies xen_blkif_disconnect() via xen_blkbk_remove() */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400750 device_unregister(&dev->dev);
751 break;
752
753 default:
754 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
755 frontend_state);
756 break;
757 }
758}
759
760
761/* ** Connection ** */
762
763
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400764/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400765 * Write the physical details regarding the block device to the store, and
766 * switch to Connected state.
767 */
768static void connect(struct backend_info *be)
769{
770 struct xenbus_transaction xbt;
771 int err;
772 struct xenbus_device *dev = be->dev;
773
774 DPRINTK("%s", dev->otherend);
775
776 /* Supply the information about the device the frontend needs */
777again:
778 err = xenbus_transaction_start(&xbt);
779 if (err) {
780 xenbus_dev_fatal(dev, err, "starting transaction");
781 return;
782 }
783
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400784 /* If we can't advertise it is OK. */
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400785 xen_blkbk_flush_diskcache(xbt, be, be->blkif->vbd.flush_support);
786
787 xen_blkbk_discard(xbt, be);
788
789 xen_blkbk_barrier(xbt, be, be->blkif->vbd.flush_support);
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400790
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200791 err = xenbus_printf(xbt, dev->nodename, "feature-persistent", "%u", 1);
792 if (err) {
793 xenbus_dev_fatal(dev, err, "writing %s/feature-persistent",
794 dev->nodename);
795 goto abort;
796 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200797 err = xenbus_printf(xbt, dev->nodename, "feature-max-indirect-segments", "%u",
798 MAX_INDIRECT_SEGMENTS);
799 if (err)
800 dev_warn(&dev->dev, "writing %s/feature-max-indirect-segments (%d)",
801 dev->nodename, err);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200802
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400803 err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400804 (unsigned long long)vbd_sz(&be->blkif->vbd));
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400805 if (err) {
806 xenbus_dev_fatal(dev, err, "writing %s/sectors",
807 dev->nodename);
808 goto abort;
809 }
810
811 /* FIXME: use a typename instead */
812 err = xenbus_printf(xbt, dev->nodename, "info", "%u",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400813 be->blkif->vbd.type |
814 (be->blkif->vbd.readonly ? VDISK_READONLY : 0));
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400815 if (err) {
816 xenbus_dev_fatal(dev, err, "writing %s/info",
817 dev->nodename);
818 goto abort;
819 }
820 err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400821 (unsigned long)
822 bdev_logical_block_size(be->blkif->vbd.bdev));
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400823 if (err) {
824 xenbus_dev_fatal(dev, err, "writing %s/sector-size",
825 dev->nodename);
826 goto abort;
827 }
Stefan Bader7c4d7d72013-05-13 16:28:15 +0200828 err = xenbus_printf(xbt, dev->nodename, "physical-sector-size", "%u",
829 bdev_physical_block_size(be->blkif->vbd.bdev));
830 if (err)
831 xenbus_dev_error(dev, err, "writing %s/physical-sector-size",
832 dev->nodename);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400833
834 err = xenbus_transaction_end(xbt, 0);
835 if (err == -EAGAIN)
836 goto again;
837 if (err)
838 xenbus_dev_fatal(dev, err, "ending transaction");
839
840 err = xenbus_switch_state(dev, XenbusStateConnected);
841 if (err)
Joe Perches08b8bfc2011-06-12 09:21:13 -0700842 xenbus_dev_fatal(dev, err, "%s: switching to Connected state",
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400843 dev->nodename);
844
845 return;
846 abort:
847 xenbus_transaction_end(xbt, 1);
848}
849
850
851static int connect_ring(struct backend_info *be)
852{
853 struct xenbus_device *dev = be->dev;
854 unsigned long ring_ref;
855 unsigned int evtchn;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200856 unsigned int pers_grants;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400857 char protocol[64] = "";
858 int err;
859
860 DPRINTK("%s", dev->otherend);
861
Konrad Rzeszutek Wilkd6091b22011-04-14 17:33:30 -0400862 err = xenbus_gather(XBT_NIL, dev->otherend, "ring-ref", "%lu",
863 &ring_ref, "event-channel", "%u", &evtchn, NULL);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400864 if (err) {
865 xenbus_dev_fatal(dev, err,
866 "reading %s/ring-ref and event-channel",
867 dev->otherend);
868 return err;
869 }
870
871 be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
872 err = xenbus_gather(XBT_NIL, dev->otherend, "protocol",
873 "%63s", protocol, NULL);
874 if (err)
875 strcpy(protocol, "unspecified, assuming native");
876 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_NATIVE))
877 be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
878 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_32))
879 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_32;
880 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_64))
881 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_64;
882 else {
883 xenbus_dev_fatal(dev, err, "unknown fe protocol %s", protocol);
884 return -1;
885 }
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200886 err = xenbus_gather(XBT_NIL, dev->otherend,
Roger Pau Monnecb5bd4d2012-11-02 16:43:04 +0100887 "feature-persistent", "%u",
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200888 &pers_grants, NULL);
889 if (err)
890 pers_grants = 0;
891
892 be->blkif->vbd.feature_gnt_persistent = pers_grants;
893 be->blkif->vbd.overflow_max_grants = 0;
894
895 pr_info(DRV_PFX "ring-ref %ld, event-channel %d, protocol %d (%s) %s\n",
896 ring_ref, evtchn, be->blkif->blk_protocol, protocol,
897 pers_grants ? "persistent grants" : "");
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400898
899 /* Map the shared frame, irq etc. */
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400900 err = xen_blkif_map(be->blkif, ring_ref, evtchn);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400901 if (err) {
902 xenbus_dev_fatal(dev, err, "mapping ring-ref %lu port %u",
903 ring_ref, evtchn);
904 return err;
905 }
906
907 return 0;
908}
909
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400910static const struct xenbus_device_id xen_blkbk_ids[] = {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400911 { "vbd" },
912 { "" }
913};
914
David Vrabel95afae42014-09-08 17:30:41 +0100915static struct xenbus_driver xen_blkbk_driver = {
916 .ids = xen_blkbk_ids,
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400917 .probe = xen_blkbk_probe,
918 .remove = xen_blkbk_remove,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400919 .otherend_changed = frontend_changed
David Vrabel95afae42014-09-08 17:30:41 +0100920};
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400921
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400922int xen_blkif_xenbus_init(void)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400923{
Jan Beulich73db1442011-12-22 09:08:13 +0000924 return xenbus_register_backend(&xen_blkbk_driver);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400925}