blob: c212d41fd5bd4c025daf09cd21b08067065d9153 [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)
28
Konrad Rzeszutek Wilkd6091b22011-04-14 17:33:30 -040029struct backend_info {
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -040030 struct xenbus_device *dev;
Konrad Rzeszutek Wilk51854322011-05-12 18:02:28 -040031 struct xen_blkif *blkif;
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -040032 struct xenbus_watch backend_watch;
33 unsigned major;
34 unsigned minor;
35 char *mode;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040036};
37
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -040038static struct kmem_cache *xen_blkif_cachep;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040039static void connect(struct backend_info *);
40static int connect_ring(struct backend_info *);
41static void backend_changed(struct xenbus_watch *, const char **,
42 unsigned int);
Valentin Priescu814d04e2014-05-20 22:28:50 +020043static void xen_blkif_free(struct xen_blkif *blkif);
44static void xen_vbd_free(struct xen_vbd *vbd);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040045
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -040046struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be)
Jeremy Fitzhardinge98e036a2010-03-18 15:35:05 -070047{
48 return be->dev;
49}
50
Valentin Priescu814d04e2014-05-20 22:28:50 +020051/*
52 * The last request could free the device from softirq context and
53 * xen_blkif_free() can sleep.
54 */
55static void xen_blkif_deferred_free(struct work_struct *work)
56{
57 struct xen_blkif *blkif;
58
59 blkif = container_of(work, struct xen_blkif, free_work);
60 xen_blkif_free(blkif);
61}
62
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -040063static int blkback_name(struct xen_blkif *blkif, char *buf)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040064{
65 char *devpath, *devname;
66 struct xenbus_device *dev = blkif->be->dev;
67
68 devpath = xenbus_read(XBT_NIL, dev->nodename, "dev", NULL);
69 if (IS_ERR(devpath))
70 return PTR_ERR(devpath);
71
Konrad Rzeszutek Wilkd6091b22011-04-14 17:33:30 -040072 devname = strstr(devpath, "/dev/");
73 if (devname != NULL)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040074 devname += strlen("/dev/");
75 else
76 devname = devpath;
77
Tao Chen13755902015-03-27 13:15:54 +000078 snprintf(buf, BLKBACK_NAME_LEN, "blkback.%d.%s", blkif->domid, devname);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040079 kfree(devpath);
80
81 return 0;
82}
83
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -040084static void xen_update_blkif_status(struct xen_blkif *blkif)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040085{
86 int err;
Tao Chen13755902015-03-27 13:15:54 +000087 char name[BLKBACK_NAME_LEN];
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040088
89 /* Not ready to connect? */
90 if (!blkif->irq || !blkif->vbd.bdev)
91 return;
92
93 /* Already connected? */
94 if (blkif->be->dev->state == XenbusStateConnected)
95 return;
96
97 /* Attempt to connect: exit if we fail to. */
98 connect(blkif->be);
99 if (blkif->be->dev->state != XenbusStateConnected)
100 return;
101
102 err = blkback_name(blkif, name);
103 if (err) {
104 xenbus_dev_error(blkif->be->dev, err, "get blkback dev name");
105 return;
106 }
107
Chris Lalancettecbf46292010-07-21 12:41:45 -0700108 err = filemap_write_and_wait(blkif->vbd.bdev->bd_inode->i_mapping);
109 if (err) {
110 xenbus_dev_error(blkif->be->dev, err, "block flush");
111 return;
112 }
113 invalidate_inode_pages2(blkif->vbd.bdev->bd_inode->i_mapping);
114
Kees Cookf1701682013-07-03 15:04:58 -0700115 blkif->xenblkd = kthread_run(xen_blkif_schedule, blkif, "%s", name);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400116 if (IS_ERR(blkif->xenblkd)) {
117 err = PTR_ERR(blkif->xenblkd);
118 blkif->xenblkd = NULL;
119 xenbus_dev_error(blkif->be->dev, err, "start xenblkd");
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200120 return;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400121 }
122}
123
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400124static struct xen_blkif *xen_blkif_alloc(domid_t domid)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400125{
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400126 struct xen_blkif *blkif;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400127
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200128 BUILD_BUG_ON(MAX_INDIRECT_PAGES > BLKIF_MAX_INDIRECT_PAGES_PER_REQUEST);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400129
Wei Yongjun654dbef2012-08-27 12:28:57 +0800130 blkif = kmem_cache_zalloc(xen_blkif_cachep, GFP_KERNEL);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400131 if (!blkif)
132 return ERR_PTR(-ENOMEM);
133
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400134 blkif->domid = domid;
135 spin_lock_init(&blkif->blk_ring_lock);
136 atomic_set(&blkif->refcnt, 1);
137 init_waitqueue_head(&blkif->wq);
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400138 init_completion(&blkif->drain_complete);
139 atomic_set(&blkif->drain, 0);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400140 blkif->st_print = jiffies;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200141 blkif->persistent_gnts.rb_node = NULL;
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200142 spin_lock_init(&blkif->free_pages_lock);
143 INIT_LIST_HEAD(&blkif->free_pages);
Roger Pau Monneef753412014-02-04 11:26:13 +0100144 INIT_LIST_HEAD(&blkif->persistent_purge_list);
Roger Pau Monnec6cc1422013-04-17 20:18:56 +0200145 blkif->free_pages_num = 0;
Roger Pau Monne3f3aad52013-04-17 20:18:57 +0200146 atomic_set(&blkif->persistent_gnt_in_use, 0);
Roger Pau Monnec05f3e32014-02-04 11:26:14 +0100147 atomic_set(&blkif->inflight, 0);
Roger Pau Monneabb97b82014-02-11 20:34:03 -0700148 INIT_WORK(&blkif->persistent_purge_work, xen_blkbk_unmap_purged_grants);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400149
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200150 INIT_LIST_HEAD(&blkif->pending_free);
Valentin Priescu814d04e2014-05-20 22:28:50 +0200151 INIT_WORK(&blkif->free_work, xen_blkif_deferred_free);
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200152 spin_lock_init(&blkif->pending_free_lock);
153 init_waitqueue_head(&blkif->pending_free_wq);
Konrad Rzeszutek Wilk8e3f8752013-01-23 16:54:32 -0500154 init_waitqueue_head(&blkif->shutdown_wq);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400155
156 return blkif;
157}
158
Wei Liuccc9d902015-04-03 14:44:59 +0800159static int xen_blkif_map(struct xen_blkif *blkif, grant_ref_t gref,
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400160 unsigned int evtchn)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400161{
162 int err;
163
164 /* Already connected through? */
165 if (blkif->irq)
166 return 0;
167
Wei Liuccc9d902015-04-03 14:44:59 +0800168 err = xenbus_map_ring_valloc(blkif->be->dev, &gref, 1,
169 &blkif->blk_ring);
David Vrabel2d073842011-09-29 16:53:30 +0100170 if (err < 0)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400171 return err;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400172
173 switch (blkif->blk_protocol) {
174 case BLKIF_PROTOCOL_NATIVE:
175 {
176 struct blkif_sring *sring;
David Vrabel2d073842011-09-29 16:53:30 +0100177 sring = (struct blkif_sring *)blkif->blk_ring;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400178 BACK_RING_INIT(&blkif->blk_rings.native, sring, PAGE_SIZE);
179 break;
180 }
181 case BLKIF_PROTOCOL_X86_32:
182 {
183 struct blkif_x86_32_sring *sring_x86_32;
David Vrabel2d073842011-09-29 16:53:30 +0100184 sring_x86_32 = (struct blkif_x86_32_sring *)blkif->blk_ring;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400185 BACK_RING_INIT(&blkif->blk_rings.x86_32, sring_x86_32, PAGE_SIZE);
186 break;
187 }
188 case BLKIF_PROTOCOL_X86_64:
189 {
190 struct blkif_x86_64_sring *sring_x86_64;
David Vrabel2d073842011-09-29 16:53:30 +0100191 sring_x86_64 = (struct blkif_x86_64_sring *)blkif->blk_ring;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400192 BACK_RING_INIT(&blkif->blk_rings.x86_64, sring_x86_64, PAGE_SIZE);
193 break;
194 }
195 default:
196 BUG();
197 }
198
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400199 err = bind_interdomain_evtchn_to_irqhandler(blkif->domid, evtchn,
200 xen_blkif_be_int, 0,
201 "blkif-backend", blkif);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400202 if (err < 0) {
David Vrabel2d073842011-09-29 16:53:30 +0100203 xenbus_unmap_ring_vfree(blkif->be->dev, blkif->blk_ring);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400204 blkif->blk_rings.common.sring = NULL;
205 return err;
206 }
207 blkif->irq = err;
208
209 return 0;
210}
211
Valentin Priescu814d04e2014-05-20 22:28:50 +0200212static int xen_blkif_disconnect(struct xen_blkif *blkif)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400213{
214 if (blkif->xenblkd) {
215 kthread_stop(blkif->xenblkd);
Konrad Rzeszutek Wilk8e3f8752013-01-23 16:54:32 -0500216 wake_up(&blkif->shutdown_wq);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400217 blkif->xenblkd = NULL;
218 }
219
Valentin Priescu814d04e2014-05-20 22:28:50 +0200220 /* The above kthread_stop() guarantees that at this point we
221 * don't have any discard_io or other_io requests. So, checking
222 * for inflight IO is enough.
223 */
224 if (atomic_read(&blkif->inflight) > 0)
225 return -EBUSY;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400226
227 if (blkif->irq) {
228 unbind_from_irqhandler(blkif->irq, blkif);
229 blkif->irq = 0;
230 }
231
232 if (blkif->blk_rings.common.sring) {
David Vrabel2d073842011-09-29 16:53:30 +0100233 xenbus_unmap_ring_vfree(blkif->be->dev, blkif->blk_ring);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400234 blkif->blk_rings.common.sring = NULL;
235 }
Valentin Priescu814d04e2014-05-20 22:28:50 +0200236
Vitaly Kuznetsov12ea7292014-09-08 15:21:33 +0200237 /* Remove all persistent grants and the cache of ballooned pages. */
238 xen_blkbk_free_caches(blkif);
239
Valentin Priescu814d04e2014-05-20 22:28:50 +0200240 return 0;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400241}
242
Konrad Rzeszutek Wilk29117582012-08-13 10:53:17 -0400243static void xen_blkif_free(struct xen_blkif *blkif)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400244{
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200245 struct pending_req *req, *n;
246 int i = 0, j;
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200247
Valentin Priescu814d04e2014-05-20 22:28:50 +0200248 xen_blkif_disconnect(blkif);
249 xen_vbd_free(&blkif->vbd);
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200250
Roger Pau Monneef753412014-02-04 11:26:13 +0100251 /* Make sure everything is drained before shutting down */
252 BUG_ON(blkif->persistent_gnt_c != 0);
253 BUG_ON(atomic_read(&blkif->persistent_gnt_in_use) != 0);
254 BUG_ON(blkif->free_pages_num != 0);
255 BUG_ON(!list_empty(&blkif->persistent_purge_list));
256 BUG_ON(!list_empty(&blkif->free_pages));
257 BUG_ON(!RB_EMPTY_ROOT(&blkif->persistent_gnts));
258
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200259 /* Check that there is no request in use */
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200260 list_for_each_entry_safe(req, n, &blkif->pending_free, free_list) {
261 list_del(&req->free_list);
Roger Pau Monnebf0720c2013-04-17 20:18:59 +0200262
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200263 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++)
264 kfree(req->segments[j]);
265
266 for (j = 0; j < MAX_INDIRECT_PAGES; j++)
267 kfree(req->indirect_pages[j]);
268
269 kfree(req);
270 i++;
271 }
272
Bob Liu69b91ed2015-06-03 13:40:01 +0800273 WARN_ON(i != XEN_BLKIF_REQS_PER_PAGE);
Roger Pau Monnebb642e82013-05-02 10:21:17 +0200274
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400275 kmem_cache_free(xen_blkif_cachep, blkif);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400276}
277
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400278int __init xen_blkif_interface_init(void)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400279{
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400280 xen_blkif_cachep = kmem_cache_create("blkif_cache",
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400281 sizeof(struct xen_blkif),
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400282 0, 0, NULL);
283 if (!xen_blkif_cachep)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400284 return -ENOMEM;
285
286 return 0;
287}
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400288
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400289/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400290 * sysfs interface for VBD I/O requests
291 */
292
293#define VBD_SHOW(name, format, args...) \
294 static ssize_t show_##name(struct device *_dev, \
295 struct device_attribute *attr, \
296 char *buf) \
297 { \
298 struct xenbus_device *dev = to_xenbus_device(_dev); \
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800299 struct backend_info *be = dev_get_drvdata(&dev->dev); \
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400300 \
301 return sprintf(buf, format, ##args); \
302 } \
303 static DEVICE_ATTR(name, S_IRUGO, show_##name, NULL)
304
Zoltan Kiss986cacb2013-03-11 16:15:50 +0000305VBD_SHOW(oo_req, "%llu\n", be->blkif->st_oo_req);
306VBD_SHOW(rd_req, "%llu\n", be->blkif->st_rd_req);
307VBD_SHOW(wr_req, "%llu\n", be->blkif->st_wr_req);
308VBD_SHOW(f_req, "%llu\n", be->blkif->st_f_req);
309VBD_SHOW(ds_req, "%llu\n", be->blkif->st_ds_req);
310VBD_SHOW(rd_sect, "%llu\n", be->blkif->st_rd_sect);
311VBD_SHOW(wr_sect, "%llu\n", be->blkif->st_wr_sect);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400312
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400313static struct attribute *xen_vbdstat_attrs[] = {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400314 &dev_attr_oo_req.attr,
315 &dev_attr_rd_req.attr,
316 &dev_attr_wr_req.attr,
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400317 &dev_attr_f_req.attr,
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800318 &dev_attr_ds_req.attr,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400319 &dev_attr_rd_sect.attr,
320 &dev_attr_wr_sect.attr,
321 NULL
322};
323
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400324static struct attribute_group xen_vbdstat_group = {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400325 .name = "statistics",
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400326 .attrs = xen_vbdstat_attrs,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400327};
328
329VBD_SHOW(physical_device, "%x:%x\n", be->major, be->minor);
330VBD_SHOW(mode, "%s\n", be->mode);
331
Konrad Rzeszutek Wilk29117582012-08-13 10:53:17 -0400332static int xenvbd_sysfs_addif(struct xenbus_device *dev)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400333{
334 int error;
335
336 error = device_create_file(&dev->dev, &dev_attr_physical_device);
Konrad Rzeszutek Wilkd6091b22011-04-14 17:33:30 -0400337 if (error)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400338 goto fail1;
339
340 error = device_create_file(&dev->dev, &dev_attr_mode);
341 if (error)
342 goto fail2;
343
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400344 error = sysfs_create_group(&dev->dev.kobj, &xen_vbdstat_group);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400345 if (error)
346 goto fail3;
347
348 return 0;
349
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400350fail3: sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400351fail2: device_remove_file(&dev->dev, &dev_attr_mode);
352fail1: device_remove_file(&dev->dev, &dev_attr_physical_device);
353 return error;
354}
355
Konrad Rzeszutek Wilk29117582012-08-13 10:53:17 -0400356static void xenvbd_sysfs_delif(struct xenbus_device *dev)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400357{
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400358 sysfs_remove_group(&dev->dev.kobj, &xen_vbdstat_group);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400359 device_remove_file(&dev->dev, &dev_attr_mode);
360 device_remove_file(&dev->dev, &dev_attr_physical_device);
361}
362
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400363
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400364static void xen_vbd_free(struct xen_vbd *vbd)
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400365{
366 if (vbd->bdev)
367 blkdev_put(vbd->bdev, vbd->readonly ? FMODE_READ : FMODE_WRITE);
368 vbd->bdev = NULL;
369}
370
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400371static int xen_vbd_create(struct xen_blkif *blkif, blkif_vdev_t handle,
372 unsigned major, unsigned minor, int readonly,
373 int cdrom)
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400374{
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400375 struct xen_vbd *vbd;
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400376 struct block_device *bdev;
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400377 struct request_queue *q;
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400378
379 vbd = &blkif->vbd;
380 vbd->handle = handle;
381 vbd->readonly = readonly;
382 vbd->type = 0;
383
384 vbd->pdevice = MKDEV(major, minor);
385
386 bdev = blkdev_get_by_dev(vbd->pdevice, vbd->readonly ?
387 FMODE_READ : FMODE_WRITE, NULL);
388
389 if (IS_ERR(bdev)) {
Tao Chen77387b82015-04-01 15:04:22 +0000390 pr_warn("xen_vbd_create: device %08x could not be opened\n",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400391 vbd->pdevice);
392 return -ENOENT;
393 }
394
395 vbd->bdev = bdev;
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400396 if (vbd->bdev->bd_disk == NULL) {
Tao Chen77387b82015-04-01 15:04:22 +0000397 pr_warn("xen_vbd_create: device %08x doesn't exist\n",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400398 vbd->pdevice);
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400399 xen_vbd_free(vbd);
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400400 return -ENOENT;
401 }
Laszlo Ersek64649202011-05-25 12:24:25 +0200402 vbd->size = vbd_sz(vbd);
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400403
404 if (vbd->bdev->bd_disk->flags & GENHD_FL_CD || cdrom)
405 vbd->type |= VDISK_CDROM;
406 if (vbd->bdev->bd_disk->flags & GENHD_FL_REMOVABLE)
407 vbd->type |= VDISK_REMOVABLE;
408
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400409 q = bdev_get_queue(bdev);
410 if (q && q->flush_flags)
411 vbd->flush_support = true;
412
Konrad Rzeszutek Wilk5ea42982011-10-12 16:23:30 -0400413 if (q && blk_queue_secdiscard(q))
414 vbd->discard_secure = true;
415
Tao Chen77387b82015-04-01 15:04:22 +0000416 pr_debug("Successful creation of handle=%04x (dom=%u)\n",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400417 handle, blkif->domid);
418 return 0;
419}
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400420static int xen_blkbk_remove(struct xenbus_device *dev)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400421{
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800422 struct backend_info *be = dev_get_drvdata(&dev->dev);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400423
Tao Chen77387b82015-04-01 15:04:22 +0000424 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400425
426 if (be->major || be->minor)
427 xenvbd_sysfs_delif(dev);
428
429 if (be->backend_watch.node) {
430 unregister_xenbus_watch(&be->backend_watch);
431 kfree(be->backend_watch.node);
432 be->backend_watch.node = NULL;
433 }
434
Valentin Priescu814d04e2014-05-20 22:28:50 +0200435 dev_set_drvdata(&dev->dev, NULL);
436
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400437 if (be->blkif) {
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400438 xen_blkif_disconnect(be->blkif);
Valentin Priescu814d04e2014-05-20 22:28:50 +0200439 xen_blkif_put(be->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400440 }
441
Jan Beulich9d092602012-12-20 10:31:11 +0000442 kfree(be->mode);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400443 kfree(be);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400444 return 0;
445}
446
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400447int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt,
448 struct backend_info *be, int state)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400449{
450 struct xenbus_device *dev = be->dev;
451 int err;
452
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400453 err = xenbus_printf(xbt, dev->nodename, "feature-flush-cache",
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400454 "%d", state);
455 if (err)
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400456 dev_warn(&dev->dev, "writing feature-flush-cache (%d)", err);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400457
458 return err;
459}
460
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400461static void xen_blkbk_discard(struct xenbus_transaction xbt, struct backend_info *be)
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800462{
463 struct xenbus_device *dev = be->dev;
464 struct xen_blkif *blkif = be->blkif;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800465 int err;
Olaf Heringc926b702014-05-21 16:32:42 +0200466 int state = 0, discard_enable;
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400467 struct block_device *bdev = be->blkif->vbd.bdev;
468 struct request_queue *q = bdev_get_queue(bdev);
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800469
Olaf Heringc926b702014-05-21 16:32:42 +0200470 err = xenbus_scanf(XBT_NIL, dev->nodename, "discard-enable", "%d",
471 &discard_enable);
472 if (err == 1 && !discard_enable)
473 return;
474
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400475 if (blk_queue_discard(q)) {
476 err = xenbus_printf(xbt, dev->nodename,
477 "discard-granularity", "%u",
478 q->limits.discard_granularity);
479 if (err) {
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400480 dev_warn(&dev->dev, "writing discard-granularity (%d)", err);
481 return;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800482 }
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400483 err = xenbus_printf(xbt, dev->nodename,
484 "discard-alignment", "%u",
485 q->limits.discard_alignment);
486 if (err) {
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400487 dev_warn(&dev->dev, "writing discard-alignment (%d)", err);
488 return;
Konrad Rzeszutek Wilk4dae7672012-03-13 18:43:23 -0400489 }
490 state = 1;
491 /* Optional. */
492 err = xenbus_printf(xbt, dev->nodename,
493 "discard-secure", "%d",
494 blkif->vbd.discard_secure);
495 if (err) {
Konrad Rzeszutek Wilka71e23d2012-04-16 21:55:04 -0400496 dev_warn(&dev->dev, "writing discard-secure (%d)", err);
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400497 return;
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800498 }
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800499 }
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800500 err = xenbus_printf(xbt, dev->nodename, "feature-discard",
501 "%d", state);
502 if (err)
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400503 dev_warn(&dev->dev, "writing feature-discard (%d)", err);
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800504}
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400505int xen_blkbk_barrier(struct xenbus_transaction xbt,
506 struct backend_info *be, int state)
507{
508 struct xenbus_device *dev = be->dev;
509 int err;
510
511 err = xenbus_printf(xbt, dev->nodename, "feature-barrier",
512 "%d", state);
513 if (err)
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400514 dev_warn(&dev->dev, "writing feature-barrier (%d)", err);
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400515
516 return err;
517}
Li Dongyangb3cb0d62011-09-01 18:39:10 +0800518
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400519/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400520 * Entry point to this code when a new device is created. Allocate the basic
521 * structures, and watch the store waiting for the hotplug scripts to tell us
522 * the device's physical major and minor numbers. Switch to InitWait.
523 */
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400524static int xen_blkbk_probe(struct xenbus_device *dev,
525 const struct xenbus_device_id *id)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400526{
527 int err;
528 struct backend_info *be = kzalloc(sizeof(struct backend_info),
529 GFP_KERNEL);
Tao Chen77387b82015-04-01 15:04:22 +0000530
531 /* match the pr_debug in xen_blkbk_remove */
532 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
533
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400534 if (!be) {
535 xenbus_dev_fatal(dev, -ENOMEM,
536 "allocating backend structure");
537 return -ENOMEM;
538 }
539 be->dev = dev;
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800540 dev_set_drvdata(&dev->dev, be);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400541
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400542 be->blkif = xen_blkif_alloc(dev->otherend_id);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400543 if (IS_ERR(be->blkif)) {
544 err = PTR_ERR(be->blkif);
545 be->blkif = NULL;
546 xenbus_dev_fatal(dev, err, "creating block interface");
547 goto fail;
548 }
549
550 /* setup back pointer */
551 be->blkif->be = be;
552
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -0800553 err = xenbus_watch_pathfmt(dev, &be->backend_watch, backend_changed,
554 "%s/%s", dev->nodename, "physical-device");
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400555 if (err)
556 goto fail;
557
558 err = xenbus_switch_state(dev, XenbusStateInitWait);
559 if (err)
560 goto fail;
561
562 return 0;
563
564fail:
Tao Chen77387b82015-04-01 15:04:22 +0000565 pr_warn("%s failed\n", __func__);
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400566 xen_blkbk_remove(dev);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400567 return err;
568}
569
570
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400571/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400572 * Callback received when the hotplug scripts have placed the physical-device
573 * node. Read it and the mode node, and create a vbd. If the frontend is
574 * ready, connect.
575 */
576static void backend_changed(struct xenbus_watch *watch,
577 const char **vec, unsigned int len)
578{
579 int err;
580 unsigned major;
581 unsigned minor;
582 struct backend_info *be
583 = container_of(watch, struct backend_info, backend_watch);
584 struct xenbus_device *dev = be->dev;
585 int cdrom = 0;
Jan Beulich9d092602012-12-20 10:31:11 +0000586 unsigned long handle;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400587 char *device_type;
588
Tao Chen77387b82015-04-01 15:04:22 +0000589 pr_debug("%s %p %d\n", __func__, dev, dev->otherend_id);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400590
591 err = xenbus_scanf(XBT_NIL, dev->nodename, "physical-device", "%x:%x",
592 &major, &minor);
593 if (XENBUS_EXIST_ERR(err)) {
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400594 /*
595 * Since this watch will fire once immediately after it is
596 * registered, we expect this. Ignore it, and wait for the
597 * hotplug scripts.
598 */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400599 return;
600 }
601 if (err != 2) {
602 xenbus_dev_fatal(dev, err, "reading physical-device");
603 return;
604 }
605
Jan Beulich9d092602012-12-20 10:31:11 +0000606 if (be->major | be->minor) {
607 if (be->major != major || be->minor != minor)
Tao Chen77387b82015-04-01 15:04:22 +0000608 pr_warn("changing physical device (from %x:%x to %x:%x) not supported.\n",
Jan Beulich9d092602012-12-20 10:31:11 +0000609 be->major, be->minor, major, minor);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400610 return;
611 }
612
613 be->mode = xenbus_read(XBT_NIL, dev->nodename, "mode", NULL);
614 if (IS_ERR(be->mode)) {
615 err = PTR_ERR(be->mode);
616 be->mode = NULL;
617 xenbus_dev_fatal(dev, err, "reading mode");
618 return;
619 }
620
621 device_type = xenbus_read(XBT_NIL, dev->otherend, "device-type", NULL);
622 if (!IS_ERR(device_type)) {
623 cdrom = strcmp(device_type, "cdrom") == 0;
624 kfree(device_type);
625 }
626
Jan Beulich9d092602012-12-20 10:31:11 +0000627 /* Front end dir is a number, which is used as the handle. */
Jingoo Hanbb8e0e82013-09-11 14:20:07 -0700628 err = kstrtoul(strrchr(dev->otherend, '/') + 1, 0, &handle);
Jan Beulich9d092602012-12-20 10:31:11 +0000629 if (err)
630 return;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400631
Jan Beulich9d092602012-12-20 10:31:11 +0000632 be->major = major;
633 be->minor = minor;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400634
Jan Beulich9d092602012-12-20 10:31:11 +0000635 err = xen_vbd_create(be->blkif, handle, major, minor,
636 !strchr(be->mode, 'w'), cdrom);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400637
Jan Beulich9d092602012-12-20 10:31:11 +0000638 if (err)
639 xenbus_dev_fatal(dev, err, "creating vbd structure");
640 else {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400641 err = xenvbd_sysfs_addif(dev);
642 if (err) {
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400643 xen_vbd_free(&be->blkif->vbd);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400644 xenbus_dev_fatal(dev, err, "creating sysfs entries");
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400645 }
Jan Beulich9d092602012-12-20 10:31:11 +0000646 }
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400647
Jan Beulich9d092602012-12-20 10:31:11 +0000648 if (err) {
649 kfree(be->mode);
650 be->mode = NULL;
651 be->major = 0;
652 be->minor = 0;
653 } else {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400654 /* We're potentially connected now */
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400655 xen_update_blkif_status(be->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400656 }
657}
658
659
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400660/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400661 * Callback received when the frontend's state changes.
662 */
663static void frontend_changed(struct xenbus_device *dev,
664 enum xenbus_state frontend_state)
665{
Jeremy Fitzhardinge5cf6e4f2010-02-11 16:07:31 -0800666 struct backend_info *be = dev_get_drvdata(&dev->dev);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400667 int err;
668
Tao Chen77387b82015-04-01 15:04:22 +0000669 pr_debug("%s %p %s\n", __func__, dev, xenbus_strstate(frontend_state));
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400670
671 switch (frontend_state) {
672 case XenbusStateInitialising:
673 if (dev->state == XenbusStateClosed) {
Tao Chen77387b82015-04-01 15:04:22 +0000674 pr_info("%s: prepare for reconnect\n", dev->nodename);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400675 xenbus_switch_state(dev, XenbusStateInitWait);
676 }
677 break;
678
679 case XenbusStateInitialised:
680 case XenbusStateConnected:
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400681 /*
682 * Ensure we connect even when two watches fire in
Justin P. Mattock42b2aa82011-11-28 20:31:00 -0800683 * close succession and we miss the intermediate value
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400684 * of frontend_state.
685 */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400686 if (dev->state == XenbusStateConnected)
687 break;
688
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400689 /*
690 * Enforce precondition before potential leak point.
Joe Jin1bc05b02011-08-15 12:57:07 +0800691 * xen_blkif_disconnect() is idempotent.
Keir Fraser313d7b02010-11-24 22:08:20 -0800692 */
Valentin Priescu814d04e2014-05-20 22:28:50 +0200693 err = xen_blkif_disconnect(be->blkif);
694 if (err) {
695 xenbus_dev_fatal(dev, err, "pending I/O");
696 break;
697 }
Keir Fraser313d7b02010-11-24 22:08:20 -0800698
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400699 err = connect_ring(be);
700 if (err)
701 break;
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400702 xen_update_blkif_status(be->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400703 break;
704
705 case XenbusStateClosing:
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400706 xenbus_switch_state(dev, XenbusStateClosing);
707 break;
708
709 case XenbusStateClosed:
Joe Jin6f5986b2011-08-15 12:51:31 +0800710 xen_blkif_disconnect(be->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400711 xenbus_switch_state(dev, XenbusStateClosed);
712 if (xenbus_dev_is_online(dev))
713 break;
714 /* fall through if not online */
715 case XenbusStateUnknown:
Joe Jin1bc05b02011-08-15 12:57:07 +0800716 /* implies xen_blkif_disconnect() via xen_blkbk_remove() */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400717 device_unregister(&dev->dev);
718 break;
719
720 default:
721 xenbus_dev_fatal(dev, -EINVAL, "saw state %d at frontend",
722 frontend_state);
723 break;
724 }
725}
726
727
728/* ** Connection ** */
729
730
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400731/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400732 * Write the physical details regarding the block device to the store, and
733 * switch to Connected state.
734 */
735static void connect(struct backend_info *be)
736{
737 struct xenbus_transaction xbt;
738 int err;
739 struct xenbus_device *dev = be->dev;
740
Tao Chen77387b82015-04-01 15:04:22 +0000741 pr_debug("%s %s\n", __func__, dev->otherend);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400742
743 /* Supply the information about the device the frontend needs */
744again:
745 err = xenbus_transaction_start(&xbt);
746 if (err) {
747 xenbus_dev_fatal(dev, err, "starting transaction");
748 return;
749 }
750
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400751 /* If we can't advertise it is OK. */
Konrad Rzeszutek Wilk3389bb82012-03-14 13:04:00 -0400752 xen_blkbk_flush_diskcache(xbt, be, be->blkif->vbd.flush_support);
753
754 xen_blkbk_discard(xbt, be);
755
756 xen_blkbk_barrier(xbt, be, be->blkif->vbd.flush_support);
Konrad Rzeszutek Wilk29bde092011-10-10 00:42:22 -0400757
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200758 err = xenbus_printf(xbt, dev->nodename, "feature-persistent", "%u", 1);
759 if (err) {
760 xenbus_dev_fatal(dev, err, "writing %s/feature-persistent",
761 dev->nodename);
762 goto abort;
763 }
Roger Pau Monne402b27f2013-04-18 16:06:54 +0200764 err = xenbus_printf(xbt, dev->nodename, "feature-max-indirect-segments", "%u",
765 MAX_INDIRECT_SEGMENTS);
766 if (err)
767 dev_warn(&dev->dev, "writing %s/feature-max-indirect-segments (%d)",
768 dev->nodename, err);
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200769
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400770 err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400771 (unsigned long long)vbd_sz(&be->blkif->vbd));
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400772 if (err) {
773 xenbus_dev_fatal(dev, err, "writing %s/sectors",
774 dev->nodename);
775 goto abort;
776 }
777
778 /* FIXME: use a typename instead */
779 err = xenbus_printf(xbt, dev->nodename, "info", "%u",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400780 be->blkif->vbd.type |
781 (be->blkif->vbd.readonly ? VDISK_READONLY : 0));
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400782 if (err) {
783 xenbus_dev_fatal(dev, err, "writing %s/info",
784 dev->nodename);
785 goto abort;
786 }
787 err = xenbus_printf(xbt, dev->nodename, "sector-size", "%lu",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400788 (unsigned long)
789 bdev_logical_block_size(be->blkif->vbd.bdev));
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400790 if (err) {
791 xenbus_dev_fatal(dev, err, "writing %s/sector-size",
792 dev->nodename);
793 goto abort;
794 }
Stefan Bader7c4d7d72013-05-13 16:28:15 +0200795 err = xenbus_printf(xbt, dev->nodename, "physical-sector-size", "%u",
796 bdev_physical_block_size(be->blkif->vbd.bdev));
797 if (err)
798 xenbus_dev_error(dev, err, "writing %s/physical-sector-size",
799 dev->nodename);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400800
801 err = xenbus_transaction_end(xbt, 0);
802 if (err == -EAGAIN)
803 goto again;
804 if (err)
805 xenbus_dev_fatal(dev, err, "ending transaction");
806
807 err = xenbus_switch_state(dev, XenbusStateConnected);
808 if (err)
Joe Perches08b8bfc2011-06-12 09:21:13 -0700809 xenbus_dev_fatal(dev, err, "%s: switching to Connected state",
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400810 dev->nodename);
811
812 return;
813 abort:
814 xenbus_transaction_end(xbt, 1);
815}
816
817
818static int connect_ring(struct backend_info *be)
819{
820 struct xenbus_device *dev = be->dev;
821 unsigned long ring_ref;
822 unsigned int evtchn;
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200823 unsigned int pers_grants;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400824 char protocol[64] = "";
Bob Liu69b91ed2015-06-03 13:40:01 +0800825 struct pending_req *req, *n;
826 int err, i, j;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400827
Tao Chen77387b82015-04-01 15:04:22 +0000828 pr_debug("%s %s\n", __func__, dev->otherend);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400829
Konrad Rzeszutek Wilkd6091b22011-04-14 17:33:30 -0400830 err = xenbus_gather(XBT_NIL, dev->otherend, "ring-ref", "%lu",
831 &ring_ref, "event-channel", "%u", &evtchn, NULL);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400832 if (err) {
833 xenbus_dev_fatal(dev, err,
834 "reading %s/ring-ref and event-channel",
835 dev->otherend);
836 return err;
837 }
838
David Vrabelb042a3c2015-02-05 17:09:56 +0000839 be->blkif->blk_protocol = BLKIF_PROTOCOL_DEFAULT;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400840 err = xenbus_gather(XBT_NIL, dev->otherend, "protocol",
841 "%63s", protocol, NULL);
842 if (err)
David Vrabelb042a3c2015-02-05 17:09:56 +0000843 strcpy(protocol, "unspecified, assuming default");
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400844 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_NATIVE))
845 be->blkif->blk_protocol = BLKIF_PROTOCOL_NATIVE;
846 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_32))
847 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_32;
848 else if (0 == strcmp(protocol, XEN_IO_PROTO_ABI_X86_64))
849 be->blkif->blk_protocol = BLKIF_PROTOCOL_X86_64;
850 else {
851 xenbus_dev_fatal(dev, err, "unknown fe protocol %s", protocol);
852 return -1;
853 }
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200854 err = xenbus_gather(XBT_NIL, dev->otherend,
Roger Pau Monnecb5bd4d2012-11-02 16:43:04 +0100855 "feature-persistent", "%u",
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200856 &pers_grants, NULL);
857 if (err)
858 pers_grants = 0;
859
860 be->blkif->vbd.feature_gnt_persistent = pers_grants;
861 be->blkif->vbd.overflow_max_grants = 0;
862
Tao Chen77387b82015-04-01 15:04:22 +0000863 pr_info("ring-ref %ld, event-channel %d, protocol %d (%s) %s\n",
Roger Pau Monne0a8704a2012-10-24 18:58:45 +0200864 ring_ref, evtchn, be->blkif->blk_protocol, protocol,
865 pers_grants ? "persistent grants" : "");
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400866
Bob Liu69b91ed2015-06-03 13:40:01 +0800867 for (i = 0; i < XEN_BLKIF_REQS_PER_PAGE; i++) {
868 req = kzalloc(sizeof(*req), GFP_KERNEL);
869 if (!req)
870 goto fail;
871 list_add_tail(&req->free_list, &be->blkif->pending_free);
872 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++) {
873 req->segments[j] = kzalloc(sizeof(*req->segments[0]), GFP_KERNEL);
874 if (!req->segments[j])
875 goto fail;
876 }
877 for (j = 0; j < MAX_INDIRECT_PAGES; j++) {
878 req->indirect_pages[j] = kzalloc(sizeof(*req->indirect_pages[0]),
879 GFP_KERNEL);
880 if (!req->indirect_pages[j])
881 goto fail;
882 }
883 }
884
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400885 /* Map the shared frame, irq etc. */
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400886 err = xen_blkif_map(be->blkif, ring_ref, evtchn);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400887 if (err) {
888 xenbus_dev_fatal(dev, err, "mapping ring-ref %lu port %u",
889 ring_ref, evtchn);
890 return err;
891 }
892
893 return 0;
Bob Liu69b91ed2015-06-03 13:40:01 +0800894
895fail:
896 list_for_each_entry_safe(req, n, &be->blkif->pending_free, free_list) {
897 list_del(&req->free_list);
898 for (j = 0; j < MAX_INDIRECT_SEGMENTS; j++) {
899 if (!req->segments[j])
900 break;
901 kfree(req->segments[j]);
902 }
903 for (j = 0; j < MAX_INDIRECT_PAGES; j++) {
904 if (!req->indirect_pages[j])
905 break;
906 kfree(req->indirect_pages[j]);
907 }
908 kfree(req);
909 }
910 return -ENOMEM;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400911}
912
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400913static const struct xenbus_device_id xen_blkbk_ids[] = {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400914 { "vbd" },
915 { "" }
916};
917
David Vrabel95afae42014-09-08 17:30:41 +0100918static struct xenbus_driver xen_blkbk_driver = {
919 .ids = xen_blkbk_ids,
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400920 .probe = xen_blkbk_probe,
921 .remove = xen_blkbk_remove,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400922 .otherend_changed = frontend_changed
David Vrabel95afae42014-09-08 17:30:41 +0100923};
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400924
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400925int xen_blkif_xenbus_init(void)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400926{
Jan Beulich73db1442011-12-22 09:08:13 +0000927 return xenbus_register_backend(&xen_blkbk_driver);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400928}