blob: 9a547e6b6ebf02ab9bba0a48167f0b0b1b0915b2 [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);
38
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -040039struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be)
Jeremy Fitzhardinge98e036a2010-03-18 15:35:05 -070040{
41 return be->dev;
42}
43
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -040044static int blkback_name(struct xen_blkif *blkif, char *buf)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040045{
46 char *devpath, *devname;
47 struct xenbus_device *dev = blkif->be->dev;
48
49 devpath = xenbus_read(XBT_NIL, dev->nodename, "dev", NULL);
50 if (IS_ERR(devpath))
51 return PTR_ERR(devpath);
52
Konrad Rzeszutek Wilkd6091b22011-04-14 17:33:30 -040053 devname = strstr(devpath, "/dev/");
54 if (devname != NULL)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040055 devname += strlen("/dev/");
56 else
57 devname = devpath;
58
59 snprintf(buf, TASK_COMM_LEN, "blkback.%d.%s", blkif->domid, devname);
60 kfree(devpath);
61
62 return 0;
63}
64
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -040065static void xen_update_blkif_status(struct xen_blkif *blkif)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040066{
67 int err;
68 char name[TASK_COMM_LEN];
69
70 /* Not ready to connect? */
71 if (!blkif->irq || !blkif->vbd.bdev)
72 return;
73
74 /* Already connected? */
75 if (blkif->be->dev->state == XenbusStateConnected)
76 return;
77
78 /* Attempt to connect: exit if we fail to. */
79 connect(blkif->be);
80 if (blkif->be->dev->state != XenbusStateConnected)
81 return;
82
83 err = blkback_name(blkif, name);
84 if (err) {
85 xenbus_dev_error(blkif->be->dev, err, "get blkback dev name");
86 return;
87 }
88
Chris Lalancettecbf46292010-07-21 12:41:45 -070089 err = filemap_write_and_wait(blkif->vbd.bdev->bd_inode->i_mapping);
90 if (err) {
91 xenbus_dev_error(blkif->be->dev, err, "block flush");
92 return;
93 }
94 invalidate_inode_pages2(blkif->vbd.bdev->bd_inode->i_mapping);
95
Kees Cookf1701682013-07-03 15:04:58 -070096 blkif->xenblkd = kthread_run(xen_blkif_schedule, blkif, "%s", name);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040097 if (IS_ERR(blkif->xenblkd)) {
98 err = PTR_ERR(blkif->xenblkd);
99 blkif->xenblkd = NULL;
100 xenbus_dev_error(blkif->be->dev, err, "start xenblkd");
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200101 return;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400102 }
103}
104
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400105static struct xen_blkif *xen_blkif_alloc(domid_t domid)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400106{
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400107 struct xen_blkif *blkif;
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200108 struct pending_req *req, *n;
109 int i, j;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400110
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200111 BUILD_BUG_ON(MAX_INDIRECT_PAGES > BLKIF_MAX_INDIRECT_PAGES_PER_REQUEST);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400112
Wei Yongjun654dbef2012-08-27 12:28:57 +0800113 blkif = kmem_cache_zalloc(xen_blkif_cachep, GFP_KERNEL);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400114 if (!blkif)
115 return ERR_PTR(-ENOMEM);
116
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400117 blkif->domid = domid;
118 spin_lock_init(&blkif->blk_ring_lock);
119 atomic_set(&blkif->refcnt, 1);
120 init_waitqueue_head(&blkif->wq);
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400121 init_completion(&blkif->drain_complete);
122 atomic_set(&blkif->drain, 0);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400123 blkif->st_print = jiffies;
124 init_waitqueue_head(&blkif->waiting_to_free);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200125 blkif->persistent_gnts.rb_node = NULL;
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200126 spin_lock_init(&blkif->free_pages_lock);
127 INIT_LIST_HEAD(&blkif->free_pages);
Roger Pau Monneef753412014-02-04 11:26:13 +0100128 INIT_LIST_HEAD(&blkif->persistent_purge_list);
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200129 blkif->free_pages_num = 0;
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200130 atomic_set(&blkif->persistent_gnt_in_use, 0);
Roger Pau Monnec05f3e32014-02-04 11:26:14 +0100131 atomic_set(&blkif->inflight, 0);
Roger Pau Monneabb97b82014-02-11 20:34:03 -0700132 INIT_WORK(&blkif->persistent_purge_work, xen_blkbk_unmap_purged_grants);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400133
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200134 INIT_LIST_HEAD(&blkif->pending_free);
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200135
136 for (i = 0; i < XEN_BLKIF_REQS; i++) {
137 req = kzalloc(sizeof(*req), GFP_KERNEL);
138 if (!req)
139 goto fail;
140 list_add_tail(&req->free_list,
141 &blkif->pending_free);
142 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++) {
143 req->segments[j] = kzalloc(sizeof(*req->segments[0]),
144 GFP_KERNEL);
145 if (!req->segments[j])
146 goto fail;
147 }
148 for (j = 0; j < MAX_INDIRECT_PAGES; j++) {
149 req->indirect_pages[j] = kzalloc(sizeof(*req->indirect_pages[0]),
150 GFP_KERNEL);
151 if (!req->indirect_pages[j])
152 goto fail;
153 }
154 }
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200155 spin_lock_init(&blkif->pending_free_lock);
156 init_waitqueue_head(&blkif->pending_free_wq);
Konrad Rzeszutek Wilk8e3f8752013-01-23 16:54:32 -0500157 init_waitqueue_head(&blkif->shutdown_wq);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400158
159 return blkif;
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200160
161fail:
162 list_for_each_entry_safe(req, n, &blkif->pending_free, free_list) {
163 list_del(&req->free_list);
164 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++) {
165 if (!req->segments[j])
166 break;
167 kfree(req->segments[j]);
168 }
169 for (j = 0; j < MAX_INDIRECT_PAGES; j++) {
170 if (!req->indirect_pages[j])
171 break;
172 kfree(req->indirect_pages[j]);
173 }
174 kfree(req);
175 }
176
177 kmem_cache_free(xen_blkif_cachep, blkif);
178
179 return ERR_PTR(-ENOMEM);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400180}
181
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400182static int xen_blkif_map(struct xen_blkif *blkif, unsigned long shared_page,
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400183 unsigned int evtchn)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400184{
185 int err;
186
187 /* Already connected through? */
188 if (blkif->irq)
189 return 0;
190
David Vrabel2d073842011-09-29 16:53:30 +0100191 err = xenbus_map_ring_valloc(blkif->be->dev, shared_page, &blkif->blk_ring);
192 if (err < 0)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400193 return err;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400194
195 switch (blkif->blk_protocol) {
196 case BLKIF_PROTOCOL_NATIVE:
197 {
198 struct blkif_sring *sring;
David Vrabel2d073842011-09-29 16:53:30 +0100199 sring = (struct blkif_sring *)blkif->blk_ring;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400200 BACK_RING_INIT(&blkif->blk_rings.native, sring, PAGE_SIZE);
201 break;
202 }
203 case BLKIF_PROTOCOL_X86_32:
204 {
205 struct blkif_x86_32_sring *sring_x86_32;
David Vrabel2d073842011-09-29 16:53:30 +0100206 sring_x86_32 = (struct blkif_x86_32_sring *)blkif->blk_ring;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400207 BACK_RING_INIT(&blkif->blk_rings.x86_32, sring_x86_32, PAGE_SIZE);
208 break;
209 }
210 case BLKIF_PROTOCOL_X86_64:
211 {
212 struct blkif_x86_64_sring *sring_x86_64;
David Vrabel2d073842011-09-29 16:53:30 +0100213 sring_x86_64 = (struct blkif_x86_64_sring *)blkif->blk_ring;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400214 BACK_RING_INIT(&blkif->blk_rings.x86_64, sring_x86_64, PAGE_SIZE);
215 break;
216 }
217 default:
218 BUG();
219 }
220
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400221 err = bind_interdomain_evtchn_to_irqhandler(blkif->domid, evtchn,
222 xen_blkif_be_int, 0,
223 "blkif-backend", blkif);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400224 if (err < 0) {
David Vrabel2d073842011-09-29 16:53:30 +0100225 xenbus_unmap_ring_vfree(blkif->be->dev, blkif->blk_ring);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400226 blkif->blk_rings.common.sring = NULL;
227 return err;
228 }
229 blkif->irq = err;
230
231 return 0;
232}
233
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400234static void xen_blkif_disconnect(struct xen_blkif *blkif)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400235{
236 if (blkif->xenblkd) {
237 kthread_stop(blkif->xenblkd);
Konrad Rzeszutek Wilk8e3f8752013-01-23 16:54:32 -0500238 wake_up(&blkif->shutdown_wq);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400239 blkif->xenblkd = NULL;
240 }
241
242 atomic_dec(&blkif->refcnt);
243 wait_event(blkif->waiting_to_free, atomic_read(&blkif->refcnt) == 0);
244 atomic_inc(&blkif->refcnt);
245
246 if (blkif->irq) {
247 unbind_from_irqhandler(blkif->irq, blkif);
248 blkif->irq = 0;
249 }
250
251 if (blkif->blk_rings.common.sring) {
David Vrabel2d073842011-09-29 16:53:30 +0100252 xenbus_unmap_ring_vfree(blkif->be->dev, blkif->blk_ring);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400253 blkif->blk_rings.common.sring = NULL;
254 }
255}
256
Konrad Rzeszutek Wilk29117582012-08-13 10:53:17 -0400257static void xen_blkif_free(struct xen_blkif *blkif)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400258{
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200259 struct pending_req *req, *n;
260 int i = 0, j;
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200261
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400262 if (!atomic_dec_and_test(&blkif->refcnt))
263 BUG();
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200264
Roger Pau Monneef753412014-02-04 11:26:13 +0100265 /* Remove all persistent grants and the cache of ballooned pages. */
266 xen_blkbk_free_caches(blkif);
267
268 /* Make sure everything is drained before shutting down */
269 BUG_ON(blkif->persistent_gnt_c != 0);
270 BUG_ON(atomic_read(&blkif->persistent_gnt_in_use) != 0);
271 BUG_ON(blkif->free_pages_num != 0);
272 BUG_ON(!list_empty(&blkif->persistent_purge_list));
273 BUG_ON(!list_empty(&blkif->free_pages));
274 BUG_ON(!RB_EMPTY_ROOT(&blkif->persistent_gnts));
275
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200276 /* Check that there is no request in use */
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200277 list_for_each_entry_safe(req, n, &blkif->pending_free, free_list) {
278 list_del(&req->free_list);
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200279
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200280 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++)
281 kfree(req->segments[j]);
282
283 for (j = 0; j < MAX_INDIRECT_PAGES; j++)
284 kfree(req->indirect_pages[j]);
285
286 kfree(req);
287 i++;
288 }
289
290 WARN_ON(i != XEN_BLKIF_REQS);
291
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400292 kmem_cache_free(xen_blkif_cachep, blkif);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400293}
294
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400295int __init xen_blkif_interface_init(void)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400296{
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400297 xen_blkif_cachep = kmem_cache_create("blkif_cache",
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400298 sizeof(struct xen_blkif),
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400299 0, 0, NULL);
300 if (!xen_blkif_cachep)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400301 return -ENOMEM;
302
303 return 0;
304}
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400305
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400306/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400307 * sysfs interface for VBD I/O requests
308 */
309
310#define VBD_SHOW(name, format, args...) \
311 static ssize_t show_##name(struct device *_dev, \
312 struct device_attribute *attr, \
313 char *buf) \
314 { \
315 struct xenbus_device *dev = to_xenbus_device(_dev); \
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800316 struct backend_info *be = dev_get_drvdata(&dev->dev); \
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400317 \
318 return sprintf(buf, format, ##args); \
319 } \
320 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
321
Zoltan Kiss986cacb2013-03-11 16:15:50 +0000322VBD_SHOW(oo_req, "%llu\n", be->blkif->st_oo_req);
323VBD_SHOW(rd_req, "%llu\n", be->blkif->st_rd_req);
324VBD_SHOW(wr_req, "%llu\n", be->blkif->st_wr_req);
325VBD_SHOW(f_req, "%llu\n", be->blkif->st_f_req);
326VBD_SHOW(ds_req, "%llu\n", be->blkif->st_ds_req);
327VBD_SHOW(rd_sect, "%llu\n", be->blkif->st_rd_sect);
328VBD_SHOW(wr_sect, "%llu\n", be->blkif->st_wr_sect);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400329
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400330static struct attribute *xen_vbdstat_attrs[] = {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400331 &dev_attr_oo_req.attr,
332 &dev_attr_rd_req.attr,
333 &dev_attr_wr_req.attr,
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400334 &dev_attr_f_req.attr,
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800335 &dev_attr_ds_req.attr,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400336 &dev_attr_rd_sect.attr,
337 &dev_attr_wr_sect.attr,
338 NULL
339};
340
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400341static struct attribute_group xen_vbdstat_group = {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400342 .name = "statistics",
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400343 .attrs = xen_vbdstat_attrs,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400344};
345
346VBD_SHOW(physical_device, "%x:%x\n", be->major, be->minor);
347VBD_SHOW(mode, "%s\n", be->mode);
348
Konrad Rzeszutek Wilk29117582012-08-13 10:53:17 -0400349static int xenvbd_sysfs_addif(struct xenbus_device *dev)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400350{
351 int error;
352
353 error = device_create_file(&dev->dev, &dev_attr_physical_device);
Konrad Rzeszutek Wilkd6091b22011-04-14 17:33:30 -0400354 if (error)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400355 goto fail1;
356
357 error = device_create_file(&dev->dev, &dev_attr_mode);
358 if (error)
359 goto fail2;
360
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400361 error = sysfs_create_group(&dev->dev.kobj, &xen_vbdstat_group);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400362 if (error)
363 goto fail3;
364
365 return 0;
366
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400367fail3: sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400368fail2: device_remove_file(&dev->dev, &dev_attr_mode);
369fail1: device_remove_file(&dev->dev, &dev_attr_physical_device);
370 return error;
371}
372
Konrad Rzeszutek Wilk29117582012-08-13 10:53:17 -0400373static void xenvbd_sysfs_delif(struct xenbus_device *dev)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400374{
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400375 sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400376 device_remove_file(&dev->dev, &dev_attr_mode);
377 device_remove_file(&dev->dev, &dev_attr_physical_device);
378}
379
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400380
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400381static void xen_vbd_free(struct xen_vbd *vbd)
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400382{
383 if (vbd->bdev)
384 blkdev_put(vbd->bdev, vbd->readonly ? FMODE_READ : FMODE_WRITE);
385 vbd->bdev = NULL;
386}
387
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400388static int xen_vbd_create(struct xen_blkif *blkif, blkif_vdev_t handle,
389 unsigned major, unsigned minor, int readonly,
390 int cdrom)
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400391{
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400392 struct xen_vbd *vbd;
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400393 struct block_device *bdev;
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400394 struct request_queue *q;
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400395
396 vbd = &blkif->vbd;
397 vbd->handle = handle;
398 vbd->readonly = readonly;
399 vbd->type = 0;
400
401 vbd->pdevice = MKDEV(major, minor);
402
403 bdev = blkdev_get_by_dev(vbd->pdevice, vbd->readonly ?
404 FMODE_READ : FMODE_WRITE, NULL);
405
406 if (IS_ERR(bdev)) {
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400407 DPRINTK("xen_vbd_create: device %08x could not be opened.\n",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400408 vbd->pdevice);
409 return -ENOENT;
410 }
411
412 vbd->bdev = bdev;
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400413 if (vbd->bdev->bd_disk == NULL) {
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400414 DPRINTK("xen_vbd_create: device %08x doesn't exist.\n",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400415 vbd->pdevice);
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400416 xen_vbd_free(vbd);
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400417 return -ENOENT;
418 }
Laszlo Ersek64649202011-05-25 12:24:25 +0200419 vbd->size = vbd_sz(vbd);
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400420
421 if (vbd->bdev->bd_disk->flags & GENHD_FL_CD || cdrom)
422 vbd->type |= VDISK_CDROM;
423 if (vbd->bdev->bd_disk->flags & GENHD_FL_REMOVABLE)
424 vbd->type |= VDISK_REMOVABLE;
425
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400426 q = bdev_get_queue(bdev);
427 if (q && q->flush_flags)
428 vbd->flush_support = true;
429
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400430 if (q && blk_queue_secdiscard(q))
431 vbd->discard_secure = true;
432
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400433 DPRINTK("Successful creation of handle=%04x (dom=%u)\n",
434 handle, blkif->domid);
435 return 0;
436}
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400437static int xen_blkbk_remove(struct xenbus_device *dev)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400438{
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800439 struct backend_info *be = dev_get_drvdata(&dev->dev);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400440
441 DPRINTK("");
442
443 if (be->major || be->minor)
444 xenvbd_sysfs_delif(dev);
445
446 if (be->backend_watch.node) {
447 unregister_xenbus_watch(&be->backend_watch);
448 kfree(be->backend_watch.node);
449 be->backend_watch.node = NULL;
450 }
451
452 if (be->blkif) {
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400453 xen_blkif_disconnect(be->blkif);
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400454 xen_vbd_free(&be->blkif->vbd);
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400455 xen_blkif_free(be->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400456 be->blkif = NULL;
457 }
458
Jan Beulich9d092602012-12-20 10:31:11 +0000459 kfree(be->mode);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400460 kfree(be);
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800461 dev_set_drvdata(&dev->dev, NULL);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400462 return 0;
463}
464
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400465int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt,
466 struct backend_info *be, int state)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400467{
468 struct xenbus_device *dev = be->dev;
469 int err;
470
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400471 err = xenbus_printf(xbt, dev->nodename, "feature-flush-cache",
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400472 "%d", state);
473 if (err)
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400474 dev_warn(&dev->dev, "writing feature-flush-cache (%d)", err);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400475
476 return err;
477}
478
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400479static void xen_blkbk_discard(struct xenbus_transaction xbt, struct backend_info *be)
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800480{
481 struct xenbus_device *dev = be->dev;
482 struct xen_blkif *blkif = be->blkif;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800483 int err;
484 int state = 0;
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400485 struct block_device *bdev = be->blkif->vbd.bdev;
486 struct request_queue *q = bdev_get_queue(bdev);
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800487
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400488 if (blk_queue_discard(q)) {
489 err = xenbus_printf(xbt, dev->nodename,
490 "discard-granularity", "%u",
491 q->limits.discard_granularity);
492 if (err) {
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400493 dev_warn(&dev->dev, "writing discard-granularity (%d)", err);
494 return;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800495 }
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400496 err = xenbus_printf(xbt, dev->nodename,
497 "discard-alignment", "%u",
498 q->limits.discard_alignment);
499 if (err) {
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400500 dev_warn(&dev->dev, "writing discard-alignment (%d)", err);
501 return;
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400502 }
503 state = 1;
504 /* Optional. */
505 err = xenbus_printf(xbt, dev->nodename,
506 "discard-secure", "%d",
507 blkif->vbd.discard_secure);
508 if (err) {
Konrad Rzeszutek Wilka71e23d2012-04-16 21:55:04 -0400509 dev_warn(&dev->dev, "writing discard-secure (%d)", err);
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400510 return;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800511 }
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800512 }
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800513 err = xenbus_printf(xbt, dev->nodename, "feature-discard",
514 "%d", state);
515 if (err)
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400516 dev_warn(&dev->dev, "writing feature-discard (%d)", err);
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800517}
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400518int xen_blkbk_barrier(struct xenbus_transaction xbt,
519 struct backend_info *be, int state)
520{
521 struct xenbus_device *dev = be->dev;
522 int err;
523
524 err = xenbus_printf(xbt, dev->nodename, "feature-barrier",
525 "%d", state);
526 if (err)
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400527 dev_warn(&dev->dev, "writing feature-barrier (%d)", err);
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400528
529 return err;
530}
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800531
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400532/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400533 * Entry point to this code when a new device is created. Allocate the basic
534 * structures, and watch the store waiting for the hotplug scripts to tell us
535 * the device's physical major and minor numbers. Switch to InitWait.
536 */
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400537static int xen_blkbk_probe(struct xenbus_device *dev,
538 const struct xenbus_device_id *id)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400539{
540 int err;
541 struct backend_info *be = kzalloc(sizeof(struct backend_info),
542 GFP_KERNEL);
543 if (!be) {
544 xenbus_dev_fatal(dev, -ENOMEM,
545 "allocating backend structure");
546 return -ENOMEM;
547 }
548 be->dev = dev;
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800549 dev_set_drvdata(&dev->dev, be);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400550
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400551 be->blkif = xen_blkif_alloc(dev->otherend_id);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400552 if (IS_ERR(be->blkif)) {
553 err = PTR_ERR(be->blkif);
554 be->blkif = NULL;
555 xenbus_dev_fatal(dev, err, "creating block interface");
556 goto fail;
557 }
558
559 /* setup back pointer */
560 be->blkif->be = be;
561
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -0800562 err = xenbus_watch_pathfmt(dev, &be->backend_watch, backend_changed,
563 "%s/%s", dev->nodename, "physical-device");
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400564 if (err)
565 goto fail;
566
567 err = xenbus_switch_state(dev, XenbusStateInitWait);
568 if (err)
569 goto fail;
570
571 return 0;
572
573fail:
574 DPRINTK("failed");
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400575 xen_blkbk_remove(dev);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400576 return err;
577}
578
579
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400580/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400581 * Callback received when the hotplug scripts have placed the physical-device
582 * node. Read it and the mode node, and create a vbd. If the frontend is
583 * ready, connect.
584 */
585static void backend_changed(struct xenbus_watch *watch,
586 const char **vec, unsigned int len)
587{
588 int err;
589 unsigned major;
590 unsigned minor;
591 struct backend_info *be
592 = container_of(watch, struct backend_info, backend_watch);
593 struct xenbus_device *dev = be->dev;
594 int cdrom = 0;
Jan Beulich9d092602012-12-20 10:31:11 +0000595 unsigned long handle;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400596 char *device_type;
597
598 DPRINTK("");
599
600 err = xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x:%x",
601 &major, &minor);
602 if (XENBUS_EXIST_ERR(err)) {
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400603 /*
604 * Since this watch will fire once immediately after it is
605 * registered, we expect this. Ignore it, and wait for the
606 * hotplug scripts.
607 */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400608 return;
609 }
610 if (err != 2) {
611 xenbus_dev_fatal(dev, err, "reading physical-device");
612 return;
613 }
614
Jan Beulich9d092602012-12-20 10:31:11 +0000615 if (be->major | be->minor) {
616 if (be->major != major || be->minor != minor)
617 pr_warn(DRV_PFX "changing physical device (from %x:%x to %x:%x) not supported.\n",
618 be->major, be->minor, major, minor);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400619 return;
620 }
621
622 be->mode = xenbus_read(XBT_NIL, dev->nodename, "mode", NULL);
623 if (IS_ERR(be->mode)) {
624 err = PTR_ERR(be->mode);
625 be->mode = NULL;
626 xenbus_dev_fatal(dev, err, "reading mode");
627 return;
628 }
629
630 device_type = xenbus_read(XBT_NIL, dev->otherend, "device-type", NULL);
631 if (!IS_ERR(device_type)) {
632 cdrom = strcmp(device_type, "cdrom") == 0;
633 kfree(device_type);
634 }
635
Jan Beulich9d092602012-12-20 10:31:11 +0000636 /* Front end dir is a number, which is used as the handle. */
Jingoo Hanbb8e0e82013-09-11 14:20:07 -0700637 err = kstrtoul(strrchr(dev->otherend, '/') + 1, 0, &handle);
Jan Beulich9d092602012-12-20 10:31:11 +0000638 if (err)
639 return;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400640
Jan Beulich9d092602012-12-20 10:31:11 +0000641 be->major = major;
642 be->minor = minor;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400643
Jan Beulich9d092602012-12-20 10:31:11 +0000644 err = xen_vbd_create(be->blkif, handle, major, minor,
645 !strchr(be->mode, 'w'), cdrom);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400646
Jan Beulich9d092602012-12-20 10:31:11 +0000647 if (err)
648 xenbus_dev_fatal(dev, err, "creating vbd structure");
649 else {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400650 err = xenvbd_sysfs_addif(dev);
651 if (err) {
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400652 xen_vbd_free(&be->blkif->vbd);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400653 xenbus_dev_fatal(dev, err, "creating sysfs entries");
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400654 }
Jan Beulich9d092602012-12-20 10:31:11 +0000655 }
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400656
Jan Beulich9d092602012-12-20 10:31:11 +0000657 if (err) {
658 kfree(be->mode);
659 be->mode = NULL;
660 be->major = 0;
661 be->minor = 0;
662 } else {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400663 /* We're potentially connected now */
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400664 xen_update_blkif_status(be->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400665 }
666}
667
668
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400669/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400670 * Callback received when the frontend's state changes.
671 */
672static void frontend_changed(struct xenbus_device *dev,
673 enum xenbus_state frontend_state)
674{
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800675 struct backend_info *be = dev_get_drvdata(&dev->dev);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400676 int err;
677
678 DPRINTK("%s", xenbus_strstate(frontend_state));
679
680 switch (frontend_state) {
681 case XenbusStateInitialising:
682 if (dev->state == XenbusStateClosed) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400683 pr_info(DRV_PFX "%s: prepare for reconnect\n",
Konrad Rzeszutek Wilkebe81902011-05-12 16:42:31 -0400684 dev->nodename);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400685 xenbus_switch_state(dev, XenbusStateInitWait);
686 }
687 break;
688
689 case XenbusStateInitialised:
690 case XenbusStateConnected:
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400691 /*
692 * Ensure we connect even when two watches fire in
Justin P. Mattock42b2aa82011-11-28 20:31:00 -0800693 * close succession and we miss the intermediate value
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400694 * of frontend_state.
695 */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400696 if (dev->state == XenbusStateConnected)
697 break;
698
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400699 /*
700 * Enforce precondition before potential leak point.
Joe Jin1bc05b02011-08-15 12:57:07 +0800701 * xen_blkif_disconnect() is idempotent.
Keir Fraser313d7b02010-11-24 22:08:20 -0800702 */
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400703 xen_blkif_disconnect(be->blkif);
Keir Fraser313d7b02010-11-24 22:08:20 -0800704
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400705 err = connect_ring(be);
706 if (err)
707 break;
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400708 xen_update_blkif_status(be->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400709 break;
710
711 case XenbusStateClosing:
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400712 xenbus_switch_state(dev, XenbusStateClosing);
713 break;
714
715 case XenbusStateClosed:
Joe Jin6f5986b2011-08-15 12:51:31 +0800716 xen_blkif_disconnect(be->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400717 xenbus_switch_state(dev, XenbusStateClosed);
718 if (xenbus_dev_is_online(dev))
719 break;
720 /* fall through if not online */
721 case XenbusStateUnknown:
Joe Jin1bc05b02011-08-15 12:57:07 +0800722 /* implies xen_blkif_disconnect() via xen_blkbk_remove() */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400723 device_unregister(&dev->dev);
724 break;
725
726 default:
727 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
728 frontend_state);
729 break;
730 }
731}
732
733
734/* ** Connection ** */
735
736
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400737/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400738 * Write the physical details regarding the block device to the store, and
739 * switch to Connected state.
740 */
741static void connect(struct backend_info *be)
742{
743 struct xenbus_transaction xbt;
744 int err;
745 struct xenbus_device *dev = be->dev;
746
747 DPRINTK("%s", dev->otherend);
748
749 /* Supply the information about the device the frontend needs */
750again:
751 err = xenbus_transaction_start(&xbt);
752 if (err) {
753 xenbus_dev_fatal(dev, err, "starting transaction");
754 return;
755 }
756
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400757 /* If we can't advertise it is OK. */
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400758 xen_blkbk_flush_diskcache(xbt, be, be->blkif->vbd.flush_support);
759
760 xen_blkbk_discard(xbt, be);
761
762 xen_blkbk_barrier(xbt, be, be->blkif->vbd.flush_support);
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400763
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200764 err = xenbus_printf(xbt, dev->nodename, "feature-persistent", "%u", 1);
765 if (err) {
766 xenbus_dev_fatal(dev, err, "writing %s/feature-persistent",
767 dev->nodename);
768 goto abort;
769 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200770 err = xenbus_printf(xbt, dev->nodename, "feature-max-indirect-segments", "%u",
771 MAX_INDIRECT_SEGMENTS);
772 if (err)
773 dev_warn(&dev->dev, "writing %s/feature-max-indirect-segments (%d)",
774 dev->nodename, err);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200775
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400776 err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400777 (unsigned long long)vbd_sz(&be->blkif->vbd));
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400778 if (err) {
779 xenbus_dev_fatal(dev, err, "writing %s/sectors",
780 dev->nodename);
781 goto abort;
782 }
783
784 /* FIXME: use a typename instead */
785 err = xenbus_printf(xbt, dev->nodename, "info", "%u",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400786 be->blkif->vbd.type |
787 (be->blkif->vbd.readonly ? VDISK_READONLY : 0));
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400788 if (err) {
789 xenbus_dev_fatal(dev, err, "writing %s/info",
790 dev->nodename);
791 goto abort;
792 }
793 err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400794 (unsigned long)
795 bdev_logical_block_size(be->blkif->vbd.bdev));
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400796 if (err) {
797 xenbus_dev_fatal(dev, err, "writing %s/sector-size",
798 dev->nodename);
799 goto abort;
800 }
Stefan Bader7c4d7d72013-05-13 16:28:15 +0200801 err = xenbus_printf(xbt, dev->nodename, "physical-sector-size", "%u",
802 bdev_physical_block_size(be->blkif->vbd.bdev));
803 if (err)
804 xenbus_dev_error(dev, err, "writing %s/physical-sector-size",
805 dev->nodename);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400806
807 err = xenbus_transaction_end(xbt, 0);
808 if (err == -EAGAIN)
809 goto again;
810 if (err)
811 xenbus_dev_fatal(dev, err, "ending transaction");
812
813 err = xenbus_switch_state(dev, XenbusStateConnected);
814 if (err)
Joe Perches08b8bfc2011-06-12 09:21:13 -0700815 xenbus_dev_fatal(dev, err, "%s: switching to Connected state",
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400816 dev->nodename);
817
818 return;
819 abort:
820 xenbus_transaction_end(xbt, 1);
821}
822
823
824static int connect_ring(struct backend_info *be)
825{
826 struct xenbus_device *dev = be->dev;
827 unsigned long ring_ref;
828 unsigned int evtchn;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200829 unsigned int pers_grants;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400830 char protocol[64] = "";
831 int err;
832
833 DPRINTK("%s", dev->otherend);
834
Konrad Rzeszutek Wilkd6091b22011-04-14 17:33:30 -0400835 err = xenbus_gather(XBT_NIL, dev->otherend, "ring-ref", "%lu",
836 &ring_ref, "event-channel", "%u", &evtchn, NULL);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400837 if (err) {
838 xenbus_dev_fatal(dev, err,
839 "reading %s/ring-ref and event-channel",
840 dev->otherend);
841 return err;
842 }
843
844 be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
845 err = xenbus_gather(XBT_NIL, dev->otherend, "protocol",
846 "%63s", protocol, NULL);
847 if (err)
848 strcpy(protocol, "unspecified, assuming native");
849 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_NATIVE))
850 be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
851 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_32))
852 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_32;
853 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_64))
854 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_64;
855 else {
856 xenbus_dev_fatal(dev, err, "unknown fe protocol %s", protocol);
857 return -1;
858 }
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200859 err = xenbus_gather(XBT_NIL, dev->otherend,
Roger Pau Monnecb5bd4d2012-11-02 16:43:04 +0100860 "feature-persistent", "%u",
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200861 &pers_grants, NULL);
862 if (err)
863 pers_grants = 0;
864
865 be->blkif->vbd.feature_gnt_persistent = pers_grants;
866 be->blkif->vbd.overflow_max_grants = 0;
867
868 pr_info(DRV_PFX "ring-ref %ld, event-channel %d, protocol %d (%s) %s\n",
869 ring_ref, evtchn, be->blkif->blk_protocol, protocol,
870 pers_grants ? "persistent grants" : "");
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400871
872 /* Map the shared frame, irq etc. */
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400873 err = xen_blkif_map(be->blkif, ring_ref, evtchn);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400874 if (err) {
875 xenbus_dev_fatal(dev, err, "mapping ring-ref %lu port %u",
876 ring_ref, evtchn);
877 return err;
878 }
879
880 return 0;
881}
882
883
884/* ** Driver Registration ** */
885
886
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400887static const struct xenbus_device_id xen_blkbk_ids[] = {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400888 { "vbd" },
889 { "" }
890};
891
892
Jan Beulich73db1442011-12-22 09:08:13 +0000893static DEFINE_XENBUS_DRIVER(xen_blkbk, ,
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400894 .probe = xen_blkbk_probe,
895 .remove = xen_blkbk_remove,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400896 .otherend_changed = frontend_changed
Jan Beulich73db1442011-12-22 09:08:13 +0000897);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400898
899
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400900int xen_blkif_xenbus_init(void)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400901{
Jan Beulich73db1442011-12-22 09:08:13 +0000902 return xenbus_register_backend(&xen_blkbk_driver);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400903}