blob: dba55e3a4a864655849e082141d8cd4c577520a0 [file] [log] [blame]
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04001/******************************************************************************
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04002 *
3 * Back-end of the driver for virtual block devices. This portion of the
4 * driver exports a 'unified' block-device interface that can be accessed
5 * by any operating system that implements a compatible front end. A
6 * reference front-end implementation can be found in:
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -04007 * drivers/block/xen-blkfront.c
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -04008 *
9 * Copyright (c) 2003-2004, Keir Fraser & Steve Hand
10 * Copyright (c) 2005, Christopher Clark
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License version 2
14 * as published by the Free Software Foundation; or, when distributed
15 * separately from the Linux kernel or incorporated into other
16 * software packages, subject to the following license:
17 *
18 * Permission is hereby granted, free of charge, to any person obtaining a copy
19 * of this source file (the "Software"), to deal in the Software without
20 * restriction, including without limitation the rights to use, copy, modify,
21 * merge, publish, distribute, sublicense, and/or sell copies of the Software,
22 * and to permit persons to whom the Software is furnished to do so, subject to
23 * the following conditions:
24 *
25 * The above copyright notice and this permission notice shall be included in
26 * all copies or substantial portions of the Software.
27 *
28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
29 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
30 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
31 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
32 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
33 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
34 * IN THE SOFTWARE.
35 */
36
37#include <linux/spinlock.h>
38#include <linux/kthread.h>
39#include <linux/list.h>
40#include <linux/delay.h>
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -080041#include <linux/freezer.h>
Jeremy Fitzhardingeafd91d02009-09-15 14:12:37 -070042
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -080043#include <xen/events.h>
44#include <xen/page.h>
45#include <asm/xen/hypervisor.h>
46#include <asm/xen/hypercall.h>
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040047#include "common.h"
48
49/*
50 * These are rather arbitrary. They are fairly large because adjacent requests
51 * pulled from a communication ring are quite likely to end up being part of
52 * the same scatter/gather request at the disc.
53 *
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -040054 * ** TRY INCREASING 'xen_blkif_reqs' IF WRITE SPEEDS SEEM TOO LOW **
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040055 *
56 * This will increase the chances of being able to write whole tracks.
57 * 64 should be enough to keep us competitive with Linux.
58 */
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -040059static int xen_blkif_reqs = 64;
60module_param_named(reqs, xen_blkif_reqs, int, 0);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040061MODULE_PARM_DESC(reqs, "Number of blkback requests to allocate");
62
63/* Run-time switchable: /sys/module/blkback/parameters/ */
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -040064static unsigned int log_stats;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040065module_param(log_stats, int, 0644);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040066
67/*
68 * Each outstanding request that we've passed to the lower device layers has a
69 * 'pending_req' allocated to it. Each buffer_head that completes decrements
70 * the pendcnt towards zero. When it hits zero, the specified domain has a
71 * response queued for it, with the saved 'id' passed back.
72 */
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -040073struct pending_req {
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -040074 struct xen_blkif *blkif;
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -040075 u64 id;
76 int nr_pages;
77 atomic_t pendcnt;
78 unsigned short operation;
79 int status;
80 struct list_head free_list;
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -040081};
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040082
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040083#define BLKBACK_INVALID_HANDLE (~0)
84
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -050085struct xen_blkbk {
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -040086 struct pending_req *pending_reqs;
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -040087 /* List of all 'pending_req' available */
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -050088 struct list_head pending_free;
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -040089 /* And its spinlock. */
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -050090 spinlock_t pending_free_lock;
91 wait_queue_head_t pending_free_wq;
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -040092 /* The list of all pages that are available. */
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -050093 struct page **pending_pages;
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -040094 /* And the grant handles that are available. */
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -050095 grant_handle_t *pending_grant_handles;
96};
97
98static struct xen_blkbk *blkbk;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -040099
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400100/*
101 * Little helpful macro to figure out the index and virtual address of the
102 * pending_pages[..]. For each 'pending_req' we have have up to
103 * BLKIF_MAX_SEGMENTS_PER_REQUEST (11) pages. The seg would be from 0 through
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400104 * 10 and would index in the pending_pages[..].
105 */
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400106static inline int vaddr_pagenr(struct pending_req *req, int seg)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400107{
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400108 return (req - blkbk->pending_reqs) *
109 BLKIF_MAX_SEGMENTS_PER_REQUEST + seg;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400110}
111
Jan Beulichefe08a32010-02-05 14:19:33 -0500112#define pending_page(req, seg) pending_pages[vaddr_pagenr(req, seg)]
113
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400114static inline unsigned long vaddr(struct pending_req *req, int seg)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400115{
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500116 unsigned long pfn = page_to_pfn(blkbk->pending_page(req, seg));
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400117 return (unsigned long)pfn_to_kaddr(pfn);
118}
119
120#define pending_handle(_req, _seg) \
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500121 (blkbk->pending_grant_handles[vaddr_pagenr(_req, _seg)])
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400122
123
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400124static int do_block_io_op(struct xen_blkif *blkif);
125static int dispatch_rw_block_io(struct xen_blkif *blkif,
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -0400126 struct blkif_request *req,
127 struct pending_req *pending_req);
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400128static void make_response(struct xen_blkif *blkif, u64 id,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400129 unsigned short op, int st);
130
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400131/*
132 * Retrieve from the 'pending_reqs' a free pending_req structure to be used.
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400133 */
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400134static struct pending_req *alloc_req(void)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400135{
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400136 struct pending_req *req = NULL;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400137 unsigned long flags;
138
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500139 spin_lock_irqsave(&blkbk->pending_free_lock, flags);
140 if (!list_empty(&blkbk->pending_free)) {
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400141 req = list_entry(blkbk->pending_free.next, struct pending_req,
142 free_list);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400143 list_del(&req->free_list);
144 }
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500145 spin_unlock_irqrestore(&blkbk->pending_free_lock, flags);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400146 return req;
147}
148
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400149/*
150 * Return the 'pending_req' structure back to the freepool. We also
151 * wake up the thread if it was waiting for a free page.
152 */
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400153static void free_req(struct pending_req *req)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400154{
155 unsigned long flags;
156 int was_empty;
157
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500158 spin_lock_irqsave(&blkbk->pending_free_lock, flags);
159 was_empty = list_empty(&blkbk->pending_free);
160 list_add(&req->free_list, &blkbk->pending_free);
161 spin_unlock_irqrestore(&blkbk->pending_free_lock, flags);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400162 if (was_empty)
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500163 wake_up(&blkbk->pending_free_wq);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400164}
165
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400166/*
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400167 * Routines for managing virtual block devices (vbds).
168 */
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400169static int xen_vbd_translate(struct phys_req *req, struct xen_blkif *blkif,
170 int operation)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400171{
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400172 struct xen_vbd *vbd = &blkif->vbd;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400173 int rc = -EACCES;
174
175 if ((operation != READ) && vbd->readonly)
176 goto out;
177
178 if (unlikely((req->sector_number + req->nr_sects) > vbd_sz(vbd)))
179 goto out;
180
181 req->dev = vbd->pdevice;
182 req->bdev = vbd->bdev;
183 rc = 0;
184
185 out:
186 return rc;
187}
188
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400189static void xen_vbd_resize(struct xen_blkif *blkif)
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400190{
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400191 struct xen_vbd *vbd = &blkif->vbd;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400192 struct xenbus_transaction xbt;
193 int err;
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400194 struct xenbus_device *dev = xen_blkbk_xenbus(blkif->be);
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400195 unsigned long long new_size = vbd_sz(vbd);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400196
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400197 pr_info(DRV_PFX "VBD Resize: Domid: %d, Device: (%d, %d)\n",
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400198 blkif->domid, MAJOR(vbd->pdevice), MINOR(vbd->pdevice));
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400199 pr_info(DRV_PFX "VBD Resize: new size %llu\n", new_size);
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400200 vbd->size = new_size;
201again:
202 err = xenbus_transaction_start(&xbt);
203 if (err) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400204 pr_warn(DRV_PFX "Error starting transaction");
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400205 return;
206 }
207 err = xenbus_printf(xbt, dev->nodename, "sectors", "%llu",
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400208 (unsigned long long)vbd_sz(vbd));
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400209 if (err) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400210 pr_warn(DRV_PFX "Error writing new size");
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400211 goto abort;
212 }
213 /*
214 * Write the current state; we will use this to synchronize
215 * the front-end. If the current state is "connected" the
216 * front-end will get the new size information online.
217 */
218 err = xenbus_printf(xbt, dev->nodename, "state", "%d", dev->state);
219 if (err) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400220 pr_warn(DRV_PFX "Error writing the state");
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400221 goto abort;
222 }
223
224 err = xenbus_transaction_end(xbt, 0);
225 if (err == -EAGAIN)
226 goto again;
227 if (err)
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400228 pr_warn(DRV_PFX "Error ending transaction");
Laszlo Ersek496b3182011-05-13 09:45:40 -0400229 return;
Konrad Rzeszutek Wilkee9ff852011-04-20 10:57:29 -0400230abort:
231 xenbus_transaction_end(xbt, 1);
232}
233
234/*
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400235 * Notification from the guest OS.
236 */
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400237static void blkif_notify_work(struct xen_blkif *blkif)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400238{
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400239 blkif->waiting_reqs = 1;
240 wake_up(&blkif->wq);
241}
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400242
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400243irqreturn_t xen_blkif_be_int(int irq, void *dev_id)
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400244{
245 blkif_notify_work(dev_id);
246 return IRQ_HANDLED;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400247}
248
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400249/*
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400250 * SCHEDULER FUNCTIONS
251 */
252
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400253static void print_stats(struct xen_blkif *blkif)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400254{
Konrad Rzeszutek Wilkcca537a2011-05-12 17:23:30 -0400255 pr_info("xen-blkback (%s): oo %3d | rd %4d | wr %4d | f %4d\n",
Konrad Rzeszutek Wilkebe81902011-05-12 16:42:31 -0400256 current->comm, blkif->st_oo_req,
257 blkif->st_rd_req, blkif->st_wr_req, blkif->st_f_req);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400258 blkif->st_print = jiffies + msecs_to_jiffies(10 * 1000);
259 blkif->st_rd_req = 0;
260 blkif->st_wr_req = 0;
261 blkif->st_oo_req = 0;
262}
263
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400264int xen_blkif_schedule(void *arg)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400265{
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400266 struct xen_blkif *blkif = arg;
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400267 struct xen_vbd *vbd = &blkif->vbd;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400268
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400269 xen_blkif_get(blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400270
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400271 while (!kthread_should_stop()) {
272 if (try_to_freeze())
273 continue;
Konrad Rzeszutek Wilk42c78412011-04-20 11:21:43 -0400274 if (unlikely(vbd->size != vbd_sz(vbd)))
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400275 xen_vbd_resize(blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400276
277 wait_event_interruptible(
278 blkif->wq,
279 blkif->waiting_reqs || kthread_should_stop());
280 wait_event_interruptible(
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500281 blkbk->pending_free_wq,
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400282 !list_empty(&blkbk->pending_free) ||
283 kthread_should_stop());
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400284
285 blkif->waiting_reqs = 0;
286 smp_mb(); /* clear flag *before* checking for work */
287
288 if (do_block_io_op(blkif))
289 blkif->waiting_reqs = 1;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400290
291 if (log_stats && time_after(jiffies, blkif->st_print))
292 print_stats(blkif);
293 }
294
295 if (log_stats)
296 print_stats(blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400297
298 blkif->xenblkd = NULL;
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400299 xen_blkif_put(blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400300
301 return 0;
302}
303
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400304struct seg_buf {
305 unsigned long buf;
306 unsigned int nsec;
307};
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400308/*
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400309 * Unmap the grant references, and also remove the M2P over-rides
310 * used in the 'pending_req'.
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400311 */
Konrad Rzeszutek Wilk9f3aedf2011-04-15 11:50:34 -0400312static void xen_blkbk_unmap(struct pending_req *req)
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400313{
314 struct gnttab_unmap_grant_ref unmap[BLKIF_MAX_SEGMENTS_PER_REQUEST];
315 unsigned int i, invcount = 0;
316 grant_handle_t handle;
317 int ret;
318
319 for (i = 0; i < req->nr_pages; i++) {
320 handle = pending_handle(req, i);
321 if (handle == BLKBACK_INVALID_HANDLE)
322 continue;
323 gnttab_set_unmap_op(&unmap[invcount], vaddr(req, i),
324 GNTMAP_host_map, handle);
325 pending_handle(req, i) = BLKBACK_INVALID_HANDLE;
326 invcount++;
327 }
328
329 ret = HYPERVISOR_grant_table_op(
330 GNTTABOP_unmap_grant_ref, unmap, invcount);
331 BUG_ON(ret);
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400332 /*
333 * Note, we use invcount, so nr->pages, so we can't index
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400334 * using vaddr(req, i).
335 */
336 for (i = 0; i < invcount; i++) {
337 ret = m2p_remove_override(
338 virt_to_page(unmap[i].host_addr), false);
339 if (ret) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400340 pr_alert(DRV_PFX "Failed to remove M2P override for %lx\n",
Konrad Rzeszutek Wilkebe81902011-05-12 16:42:31 -0400341 (unsigned long)unmap[i].host_addr);
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400342 continue;
343 }
344 }
345}
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400346
347static int xen_blkbk_map(struct blkif_request *req,
348 struct pending_req *pending_req,
Konrad Rzeszutek Wilk9f3aedf2011-04-15 11:50:34 -0400349 struct seg_buf seg[])
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400350{
351 struct gnttab_map_grant_ref map[BLKIF_MAX_SEGMENTS_PER_REQUEST];
352 int i;
353 int nseg = req->nr_segments;
354 int ret = 0;
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400355
356 /*
357 * Fill out preq.nr_sects with proper amount of sectors, and setup
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400358 * assign map[..] with the PFN of the page in our domain with the
359 * corresponding grant reference for each page.
360 */
361 for (i = 0; i < nseg; i++) {
362 uint32_t flags;
363
364 flags = GNTMAP_host_map;
365 if (pending_req->operation != BLKIF_OP_READ)
366 flags |= GNTMAP_readonly;
367 gnttab_set_map_op(&map[i], vaddr(pending_req, i), flags,
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400368 req->u.rw.seg[i].gref,
369 pending_req->blkif->domid);
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400370 }
371
372 ret = HYPERVISOR_grant_table_op(GNTTABOP_map_grant_ref, map, nseg);
373 BUG_ON(ret);
374
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400375 /*
376 * Now swizzle the MFN in our domain with the MFN from the other domain
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400377 * so that when we access vaddr(pending_req,i) it has the contents of
378 * the page from the other domain.
379 */
380 for (i = 0; i < nseg; i++) {
381 if (unlikely(map[i].status != 0)) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400382 pr_debug(DRV_PFX "invalid buffer -- could not remap it\n");
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400383 map[i].handle = BLKBACK_INVALID_HANDLE;
384 ret |= 1;
385 }
386
387 pending_handle(pending_req, i) = map[i].handle;
388
389 if (ret)
390 continue;
391
392 ret = m2p_add_override(PFN_DOWN(map[i].dev_bus_addr),
393 blkbk->pending_page(pending_req, i), false);
394 if (ret) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400395 pr_alert(DRV_PFX "Failed to install M2P override for %lx (ret: %d)\n",
Konrad Rzeszutek Wilkebe81902011-05-12 16:42:31 -0400396 (unsigned long)map[i].dev_bus_addr, ret);
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400397 /* We could switch over to GNTTABOP_copy */
398 continue;
399 }
400
401 seg[i].buf = map[i].dev_bus_addr |
402 (req->u.rw.seg[i].first_sect << 9);
403 }
404 return ret;
405}
406
Konrad Rzeszutek Wilkb0aef172011-04-15 10:58:05 -0400407/*
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400408 * Completion callback on the bio's. Called as bh->b_end_io()
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400409 */
410
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400411static void __end_block_io_op(struct pending_req *pending_req, int error)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400412{
413 /* An error fails the entire request. */
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400414 if ((pending_req->operation == BLKIF_OP_FLUSH_DISKCACHE) &&
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400415 (error == -EOPNOTSUPP)) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400416 pr_debug(DRV_PFX "flush diskcache op failed, not supported\n");
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400417 xen_blkbk_flush_diskcache(XBT_NIL, pending_req->blkif->be, 0);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400418 pending_req->status = BLKIF_RSP_EOPNOTSUPP;
419 } else if (error) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400420 pr_debug(DRV_PFX "Buffer not up-to-date at end of operation,"
Konrad Rzeszutek Wilkebe81902011-05-12 16:42:31 -0400421 " error=%d\n", error);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400422 pending_req->status = BLKIF_RSP_ERROR;
423 }
424
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400425 /*
426 * If all of the bio's have completed it is time to unmap
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400427 * the grant references associated with 'request' and provide
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400428 * the proper response on the ring.
429 */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400430 if (atomic_dec_and_test(&pending_req->pendcnt)) {
Konrad Rzeszutek Wilk9f3aedf2011-04-15 11:50:34 -0400431 xen_blkbk_unmap(pending_req);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400432 make_response(pending_req->blkif, pending_req->id,
433 pending_req->operation, pending_req->status);
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400434 xen_blkif_put(pending_req->blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400435 free_req(pending_req);
436 }
437}
438
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400439/*
440 * bio callback.
441 */
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -0800442static void end_block_io_op(struct bio *bio, int error)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400443{
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400444 __end_block_io_op(bio->bi_private, error);
445 bio_put(bio);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400446}
447
448
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400449
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400450/*
451 * Function to copy the from the ring buffer the 'struct blkif_request'
452 * (which has the sectors we want, number of them, grant references, etc),
453 * and transmute it to the block API to hand it over to the proper block disk.
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400454 */
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400455static int do_block_io_op(struct xen_blkif *blkif)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400456{
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -0800457 union blkif_back_rings *blk_rings = &blkif->blk_rings;
458 struct blkif_request req;
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400459 struct pending_req *pending_req;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400460 RING_IDX rc, rp;
461 int more_to_do = 0;
462
463 rc = blk_rings->common.req_cons;
464 rp = blk_rings->common.sring->req_prod;
465 rmb(); /* Ensure we see queued requests up to 'rp'. */
466
467 while (rc != rp) {
468
469 if (RING_REQUEST_CONS_OVERFLOW(&blk_rings->common, rc))
470 break;
471
Keir Fraser8270b452009-03-06 08:29:15 +0000472 if (kthread_should_stop()) {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400473 more_to_do = 1;
474 break;
475 }
476
Keir Fraser8270b452009-03-06 08:29:15 +0000477 pending_req = alloc_req();
478 if (NULL == pending_req) {
479 blkif->st_oo_req++;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400480 more_to_do = 1;
481 break;
482 }
483
484 switch (blkif->blk_protocol) {
485 case BLKIF_PROTOCOL_NATIVE:
486 memcpy(&req, RING_GET_REQUEST(&blk_rings->native, rc), sizeof(req));
487 break;
488 case BLKIF_PROTOCOL_X86_32:
489 blkif_get_x86_32_req(&req, RING_GET_REQUEST(&blk_rings->x86_32, rc));
490 break;
491 case BLKIF_PROTOCOL_X86_64:
492 blkif_get_x86_64_req(&req, RING_GET_REQUEST(&blk_rings->x86_64, rc));
493 break;
494 default:
495 BUG();
496 }
497 blk_rings->common.req_cons = ++rc; /* before make_response() */
498
499 /* Apply all sanity checks to /private copy/ of request. */
500 barrier();
501
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -0400502 if (dispatch_rw_block_io(blkif, &req, pending_req))
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400503 break;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400504
505 /* Yield point for this unbounded loop. */
506 cond_resched();
507 }
508
509 return more_to_do;
510}
511
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400512/*
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400513 * Transmutation of the 'struct blkif_request' to a proper 'struct bio'
514 * and call the 'submit_bio' to pass it to the underlying storage.
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400515 */
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400516static int dispatch_rw_block_io(struct xen_blkif *blkif,
517 struct blkif_request *req,
518 struct pending_req *pending_req)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400519{
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400520 struct phys_req preq;
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400521 struct seg_buf seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400522 unsigned int nseg;
523 struct bio *bio = NULL;
Konrad Rzeszutek Wilk77089922011-04-15 10:51:27 -0400524 struct bio *biolist[BLKIF_MAX_SEGMENTS_PER_REQUEST];
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400525 int i, nbio = 0;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400526 int operation;
Konrad Rzeszutek Wilka19be5f2011-04-27 12:40:11 -0400527 struct blk_plug plug;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400528
529 switch (req->operation) {
530 case BLKIF_OP_READ:
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -0400531 blkif->st_rd_req++;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400532 operation = READ;
533 break;
534 case BLKIF_OP_WRITE:
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -0400535 blkif->st_wr_req++;
Konrad Rzeszutek Wilk013c3ca2011-04-26 16:24:18 -0400536 operation = WRITE_ODIRECT;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400537 break;
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400538 case BLKIF_OP_FLUSH_DISKCACHE:
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -0400539 blkif->st_f_req++;
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400540 operation = WRITE_FLUSH;
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400541 /*
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400542 * The frontend likes to set this to -1, which xen_vbd_translate
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400543 * is alergic too.
544 */
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400545 req->u.rw.sector_number = 0;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400546 break;
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400547 case BLKIF_OP_WRITE_BARRIER:
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400548 default:
549 operation = 0; /* make gcc happy */
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -0400550 goto fail_response;
551 break;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400552 }
553
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400554 /* Check that the number of segments is sane. */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400555 nseg = req->nr_segments;
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400556 if (unlikely(nseg == 0 && operation != WRITE_FLUSH) ||
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400557 unlikely(nseg > BLKIF_MAX_SEGMENTS_PER_REQUEST)) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400558 pr_debug(DRV_PFX "Bad number of segments in request (%d)\n",
Konrad Rzeszutek Wilkebe81902011-05-12 16:42:31 -0400559 nseg);
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400560 /* Haven't submitted any bio's yet. */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400561 goto fail_response;
562 }
563
564 preq.dev = req->handle;
Konrad Rzeszutek Wilkc35950b2011-03-01 16:22:28 -0500565 preq.sector_number = req->u.rw.sector_number;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400566 preq.nr_sects = 0;
567
568 pending_req->blkif = blkif;
569 pending_req->id = req->id;
570 pending_req->operation = req->operation;
571 pending_req->status = BLKIF_RSP_OKAY;
572 pending_req->nr_pages = nseg;
Konrad Rzeszutek Wilke9350492011-04-18 11:34:55 -0400573
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400574 for (i = 0; i < nseg; i++) {
Konrad Rzeszutek Wilkc35950b2011-03-01 16:22:28 -0500575 seg[i].nsec = req->u.rw.seg[i].last_sect -
576 req->u.rw.seg[i].first_sect + 1;
Konrad Rzeszutek Wilkc35950b2011-03-01 16:22:28 -0500577 if ((req->u.rw.seg[i].last_sect >= (PAGE_SIZE >> 9)) ||
578 (req->u.rw.seg[i].last_sect < req->u.rw.seg[i].first_sect))
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400579 goto fail_response;
580 preq.nr_sects += seg[i].nsec;
Konrad Rzeszutek Wilk976222e2011-04-15 11:38:29 -0400581
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400582 }
583
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400584 if (xen_vbd_translate(&preq, blkif, operation) != 0) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400585 pr_debug(DRV_PFX "access denied: %s of [%llu,%llu] on dev=%04x\n",
Konrad Rzeszutek Wilkebe81902011-05-12 16:42:31 -0400586 operation == READ ? "read" : "write",
587 preq.sector_number,
588 preq.sector_number + preq.nr_sects, preq.dev);
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400589 goto fail_response;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400590 }
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400591
592 /*
Konrad Rzeszutek Wilk3d814732011-05-12 16:53:56 -0400593 * This check _MUST_ be done after xen_vbd_translate as the preq.bdev
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400594 * is set there.
595 */
Konrad Rzeszutek Wilke9350492011-04-18 11:34:55 -0400596 for (i = 0; i < nseg; i++) {
597 if (((int)preq.sector_number|(int)seg[i].nsec) &
598 ((bdev_logical_block_size(preq.bdev) >> 9) - 1)) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400599 pr_debug(DRV_PFX "Misaligned I/O request from domain %d",
Konrad Rzeszutek Wilkebe81902011-05-12 16:42:31 -0400600 blkif->domid);
Konrad Rzeszutek Wilke9350492011-04-18 11:34:55 -0400601 goto fail_response;
602 }
603 }
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400604
605 /*
606 * If we have failed at this point, we need to undo the M2P override,
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400607 * set gnttab_set_unmap_op on all of the grant references and perform
608 * the hypercall to unmap the grants - that is all done in
Konrad Rzeszutek Wilk9f3aedf2011-04-15 11:50:34 -0400609 * xen_blkbk_unmap.
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400610 */
Konrad Rzeszutek Wilk9f3aedf2011-04-15 11:50:34 -0400611 if (xen_blkbk_map(req, pending_req, seg))
Konrad Rzeszutek Wilk1a95fe62011-04-15 11:35:13 -0400612 goto fail_flush;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400613
Konrad Rzeszutek Wilk3d68b392011-05-05 13:42:10 -0400614 /* This corresponding xen_blkif_put is done in __end_block_io_op */
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400615 xen_blkif_get(blkif);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400616
617 for (i = 0; i < nseg; i++) {
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400618 while ((bio == NULL) ||
619 (bio_add_page(bio,
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500620 blkbk->pending_page(pending_req, i),
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400621 seg[i].nsec << 9,
622 seg[i].buf & ~PAGE_MASK) == 0)) {
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400623
Konrad Rzeszutek Wilk03e0edf2011-05-12 16:19:23 -0400624 bio = bio_alloc(GFP_KERNEL, nseg-i);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400625 if (unlikely(bio == NULL))
626 goto fail_put_bio;
627
Konrad Rzeszutek Wilk03e0edf2011-05-12 16:19:23 -0400628 biolist[nbio++] = bio;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400629 bio->bi_bdev = preq.bdev;
630 bio->bi_private = pending_req;
631 bio->bi_end_io = end_block_io_op;
632 bio->bi_sector = preq.sector_number;
633 }
634
635 preq.sector_number += seg[i].nsec;
636 }
637
Konrad Rzeszutek Wilk3d68b392011-05-05 13:42:10 -0400638 /* This will be hit if the operation was a flush. */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400639 if (!bio) {
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400640 BUG_ON(operation != WRITE_FLUSH);
Konrad Rzeszutek Wilkb0f80122011-05-12 16:23:06 -0400641
Konrad Rzeszutek Wilk03e0edf2011-05-12 16:19:23 -0400642 bio = bio_alloc(GFP_KERNEL, 0);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400643 if (unlikely(bio == NULL))
644 goto fail_put_bio;
645
Konrad Rzeszutek Wilk03e0edf2011-05-12 16:19:23 -0400646 biolist[nbio++] = bio;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400647 bio->bi_bdev = preq.bdev;
648 bio->bi_private = pending_req;
649 bio->bi_end_io = end_block_io_op;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400650 }
651
Konrad Rzeszutek Wilk01f37f22011-05-11 15:57:09 -0400652 /*
653 * We set it one so that the last submit_bio does not have to call
Konrad Rzeszutek Wilk77089922011-04-15 10:51:27 -0400654 * atomic_inc.
655 */
656 atomic_set(&pending_req->pendcnt, nbio);
657
Konrad Rzeszutek Wilka19be5f2011-04-27 12:40:11 -0400658 /* Get a reference count for the disk queue and start sending I/O */
659 blk_start_plug(&plug);
660
Konrad Rzeszutek Wilk77089922011-04-15 10:51:27 -0400661 for (i = 0; i < nbio; i++)
662 submit_bio(operation, biolist[i]);
663
Konrad Rzeszutek Wilka19be5f2011-04-27 12:40:11 -0400664 /* Let the I/Os go.. */
Konrad Rzeszutek Wilk3d68b392011-05-05 13:42:10 -0400665 blk_finish_plug(&plug);
Konrad Rzeszutek Wilka19be5f2011-04-27 12:40:11 -0400666
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400667 if (operation == READ)
668 blkif->st_rd_sect += preq.nr_sects;
Konrad Rzeszutek Wilk24f567f2011-05-04 17:07:27 -0400669 else if (operation == WRITE || operation == WRITE_FLUSH)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400670 blkif->st_wr_sect += preq.nr_sects;
671
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -0400672 return 0;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400673
674 fail_flush:
Konrad Rzeszutek Wilk9f3aedf2011-04-15 11:50:34 -0400675 xen_blkbk_unmap(pending_req);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400676 fail_response:
Konrad Rzeszutek Wilk0faa8cc2011-04-14 17:58:19 -0400677 /* Haven't submitted any bio's yet. */
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400678 make_response(blkif, req->id, req->operation, BLKIF_RSP_ERROR);
679 free_req(pending_req);
680 msleep(1); /* back off a bit */
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -0400681 return -EIO;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400682
683 fail_put_bio:
Konrad Rzeszutek Wilk03e0edf2011-05-12 16:19:23 -0400684 for (i = 0; i < nbio; i++)
Konrad Rzeszutek Wilk77089922011-04-15 10:51:27 -0400685 bio_put(biolist[i]);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400686 __end_block_io_op(pending_req, -EINVAL);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400687 msleep(1); /* back off a bit */
Konrad Rzeszutek Wilkfc53bf72011-05-05 13:37:23 -0400688 return -EIO;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400689}
690
691
692
Konrad Rzeszutek Wilka1397fa2011-04-14 17:05:23 -0400693/*
694 * Put a response on the ring on how the operation fared.
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400695 */
Konrad Rzeszutek Wilk30fd1502011-05-12 16:47:48 -0400696static void make_response(struct xen_blkif *blkif, u64 id,
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400697 unsigned short op, int st)
698{
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -0800699 struct blkif_response resp;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400700 unsigned long flags;
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -0800701 union blkif_back_rings *blk_rings = &blkif->blk_rings;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400702 int more_to_do = 0;
703 int notify;
704
705 resp.id = id;
706 resp.operation = op;
707 resp.status = st;
708
709 spin_lock_irqsave(&blkif->blk_ring_lock, flags);
710 /* Place on the response ring for the relevant domain. */
711 switch (blkif->blk_protocol) {
712 case BLKIF_PROTOCOL_NATIVE:
713 memcpy(RING_GET_RESPONSE(&blk_rings->native, blk_rings->native.rsp_prod_pvt),
714 &resp, sizeof(resp));
715 break;
716 case BLKIF_PROTOCOL_X86_32:
717 memcpy(RING_GET_RESPONSE(&blk_rings->x86_32, blk_rings->x86_32.rsp_prod_pvt),
718 &resp, sizeof(resp));
719 break;
720 case BLKIF_PROTOCOL_X86_64:
721 memcpy(RING_GET_RESPONSE(&blk_rings->x86_64, blk_rings->x86_64.rsp_prod_pvt),
722 &resp, sizeof(resp));
723 break;
724 default:
725 BUG();
726 }
727 blk_rings->common.rsp_prod_pvt++;
728 RING_PUSH_RESPONSES_AND_CHECK_NOTIFY(&blk_rings->common, notify);
729 if (blk_rings->common.rsp_prod_pvt == blk_rings->common.req_cons) {
730 /*
731 * Tail check for pending requests. Allows frontend to avoid
732 * notifications if requests are already in flight (lower
733 * overheads and promotes batching).
734 */
735 RING_FINAL_CHECK_FOR_REQUESTS(&blk_rings->common, more_to_do);
736
737 } else if (RING_HAS_UNCONSUMED_REQUESTS(&blk_rings->common)) {
738 more_to_do = 1;
739 }
740
741 spin_unlock_irqrestore(&blkif->blk_ring_lock, flags);
742
743 if (more_to_do)
744 blkif_notify_work(blkif);
745 if (notify)
746 notify_remote_via_irq(blkif->irq);
747}
748
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400749static int __init xen_blkif_init(void)
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400750{
751 int i, mmap_pages;
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -0400752 int rc = 0;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400753
Jeremy Fitzhardinge88122932009-02-09 12:05:51 -0800754 if (!xen_pv_domain())
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400755 return -ENODEV;
756
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400757 blkbk = kzalloc(sizeof(struct xen_blkbk), GFP_KERNEL);
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500758 if (!blkbk) {
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400759 pr_alert(DRV_PFX "%s: out of memory!\n", __func__);
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500760 return -ENOMEM;
761 }
762
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400763 mmap_pages = xen_blkif_reqs * BLKIF_MAX_SEGMENTS_PER_REQUEST;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400764
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500765 blkbk->pending_reqs = kmalloc(sizeof(blkbk->pending_reqs[0]) *
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400766 xen_blkif_reqs, GFP_KERNEL);
Konrad Rzeszutek Wilka742b022011-03-14 12:41:26 -0400767 blkbk->pending_grant_handles = kzalloc(sizeof(blkbk->pending_grant_handles[0]) *
768 mmap_pages, GFP_KERNEL);
769 blkbk->pending_pages = kzalloc(sizeof(blkbk->pending_pages[0]) *
770 mmap_pages, GFP_KERNEL);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400771
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400772 if (!blkbk->pending_reqs || !blkbk->pending_grant_handles ||
773 !blkbk->pending_pages) {
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -0400774 rc = -ENOMEM;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400775 goto out_of_memory;
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -0400776 }
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400777
Konrad Rzeszutek Wilk464fb412011-03-01 16:26:10 -0500778 for (i = 0; i < mmap_pages; i++) {
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500779 blkbk->pending_grant_handles[i] = BLKBACK_INVALID_HANDLE;
Konrad Rzeszutek Wilka742b022011-03-14 12:41:26 -0400780 blkbk->pending_pages[i] = alloc_page(GFP_KERNEL);
Konrad Rzeszutek Wilk464fb412011-03-01 16:26:10 -0500781 if (blkbk->pending_pages[i] == NULL) {
782 rc = -ENOMEM;
783 goto out_of_memory;
784 }
785 }
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400786 rc = xen_blkif_interface_init();
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -0400787 if (rc)
788 goto failed_init;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400789
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500790 memset(blkbk->pending_reqs, 0, sizeof(blkbk->pending_reqs));
791
792 INIT_LIST_HEAD(&blkbk->pending_free);
793 spin_lock_init(&blkbk->pending_free_lock);
794 init_waitqueue_head(&blkbk->pending_free_wq);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400795
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400796 for (i = 0; i < xen_blkif_reqs; i++)
Konrad Rzeszutek Wilk2e9977c2011-04-14 17:42:07 -0400797 list_add_tail(&blkbk->pending_reqs[i].free_list,
798 &blkbk->pending_free);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400799
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400800 rc = xen_blkif_xenbus_init();
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -0400801 if (rc)
802 goto failed_init;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400803
804 return 0;
805
806 out_of_memory:
Konrad Rzeszutek Wilk22b20f22011-05-12 16:43:12 -0400807 pr_alert(DRV_PFX "%s: out of memory\n", __func__);
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -0400808 failed_init:
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500809 kfree(blkbk->pending_reqs);
Konrad Rzeszutek Wilka742b022011-03-14 12:41:26 -0400810 kfree(blkbk->pending_grant_handles);
Konrad Rzeszutek Wilk464fb412011-03-01 16:26:10 -0500811 for (i = 0; i < mmap_pages; i++) {
812 if (blkbk->pending_pages[i])
813 __free_page(blkbk->pending_pages[i]);
814 }
Konrad Rzeszutek Wilka742b022011-03-14 12:41:26 -0400815 kfree(blkbk->pending_pages);
816 kfree(blkbk);
Konrad Rzeszutek Wilke8e28872011-02-25 10:51:29 -0500817 blkbk = NULL;
Konrad Rzeszutek Wilk8770b262009-10-08 13:23:09 -0400818 return rc;
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400819}
820
Konrad Rzeszutek Wilk8b6bf742011-04-20 11:50:43 -0400821module_init(xen_blkif_init);
Konrad Rzeszutek Wilk4d05a282011-04-14 18:25:47 -0400822
823MODULE_LICENSE("Dual BSD/GPL");