xen-blkback: only read request operation from shared ring once
[pandora-kernel.git] / drivers / block / xen-blkback / common.h
1 /*
2  * This program is free software; you can redistribute it and/or
3  * modify it under the terms of the GNU General Public License version 2
4  * as published by the Free Software Foundation; or, when distributed
5  * separately from the Linux kernel or incorporated into other
6  * software packages, subject to the following license:
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining a copy
9  * of this source file (the "Software"), to deal in the Software without
10  * restriction, including without limitation the rights to use, copy, modify,
11  * merge, publish, distribute, sublicense, and/or sell copies of the Software,
12  * and to permit persons to whom the Software is furnished to do so, subject to
13  * the following conditions:
14  *
15  * The above copyright notice and this permission notice shall be included in
16  * all copies or substantial portions of the Software.
17  *
18  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
24  * IN THE SOFTWARE.
25  */
26
27 #ifndef __XEN_BLKIF__BACKEND__COMMON_H__
28 #define __XEN_BLKIF__BACKEND__COMMON_H__
29
30 #include <linux/module.h>
31 #include <linux/interrupt.h>
32 #include <linux/slab.h>
33 #include <linux/blkdev.h>
34 #include <linux/vmalloc.h>
35 #include <linux/wait.h>
36 #include <linux/io.h>
37 #include <asm/setup.h>
38 #include <asm/pgalloc.h>
39 #include <asm/hypervisor.h>
40 #include <xen/grant_table.h>
41 #include <xen/xenbus.h>
42 #include <xen/interface/io/ring.h>
43 #include <xen/interface/io/blkif.h>
44 #include <xen/interface/io/protocols.h>
45
46 #define DRV_PFX "xen-blkback:"
47 #define DPRINTK(fmt, args...)                           \
48         pr_debug(DRV_PFX "(%s:%d) " fmt ".\n",          \
49                  __func__, __LINE__, ##args)
50
51
52 /* Not a real protocol.  Used to generate ring structs which contain
53  * the elements common to all protocols only.  This way we get a
54  * compiler-checkable way to use common struct elements, so we can
55  * avoid using switch(protocol) in a number of places.  */
56 struct blkif_common_request {
57         char dummy;
58 };
59 struct blkif_common_response {
60         char dummy;
61 };
62
63 /* i386 protocol version */
64 #pragma pack(push, 4)
65
66 struct blkif_x86_32_request_rw {
67         blkif_sector_t sector_number;/* start sector idx on disk (r/w only)  */
68         struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
69 };
70
71 struct blkif_x86_32_request_discard {
72         blkif_sector_t sector_number;/* start sector idx on disk (r/w only)  */
73         uint64_t nr_sectors;
74 };
75
76 struct blkif_x86_32_request {
77         uint8_t        operation;    /* BLKIF_OP_???                         */
78         uint8_t        nr_segments;  /* number of segments                   */
79         blkif_vdev_t   handle;       /* only for read/write requests         */
80         uint64_t       id;           /* private guest value, echoed in resp  */
81         union {
82                 struct blkif_x86_32_request_rw rw;
83                 struct blkif_x86_32_request_discard discard;
84         } u;
85 };
86 struct blkif_x86_32_response {
87         uint64_t        id;              /* copied from request */
88         uint8_t         operation;       /* copied from request */
89         int16_t         status;          /* BLKIF_RSP_???       */
90 };
91 #pragma pack(pop)
92
93 /* x86_64 protocol version */
94
95 struct blkif_x86_64_request_rw {
96         blkif_sector_t sector_number;/* start sector idx on disk (r/w only)  */
97         struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
98 };
99
100 struct blkif_x86_64_request_discard {
101         blkif_sector_t sector_number;/* start sector idx on disk (r/w only)  */
102         uint64_t nr_sectors;
103 };
104
105 struct blkif_x86_64_request {
106         uint8_t        operation;    /* BLKIF_OP_???                         */
107         uint8_t        nr_segments;  /* number of segments                   */
108         blkif_vdev_t   handle;       /* only for read/write requests         */
109         uint64_t       __attribute__((__aligned__(8))) id;
110         union {
111                 struct blkif_x86_64_request_rw rw;
112                 struct blkif_x86_64_request_discard discard;
113         } u;
114 };
115 struct blkif_x86_64_response {
116         uint64_t       __attribute__((__aligned__(8))) id;
117         uint8_t         operation;       /* copied from request */
118         int16_t         status;          /* BLKIF_RSP_???       */
119 };
120
121 DEFINE_RING_TYPES(blkif_common, struct blkif_common_request,
122                   struct blkif_common_response);
123 DEFINE_RING_TYPES(blkif_x86_32, struct blkif_x86_32_request,
124                   struct blkif_x86_32_response);
125 DEFINE_RING_TYPES(blkif_x86_64, struct blkif_x86_64_request,
126                   struct blkif_x86_64_response);
127
128 union blkif_back_rings {
129         struct blkif_back_ring        native;
130         struct blkif_common_back_ring common;
131         struct blkif_x86_32_back_ring x86_32;
132         struct blkif_x86_64_back_ring x86_64;
133 };
134
135 enum blkif_protocol {
136         BLKIF_PROTOCOL_NATIVE = 1,
137         BLKIF_PROTOCOL_X86_32 = 2,
138         BLKIF_PROTOCOL_X86_64 = 3,
139 };
140
141 enum blkif_backend_type {
142         BLKIF_BACKEND_PHY  = 1,
143         BLKIF_BACKEND_FILE = 2,
144 };
145
146 struct xen_vbd {
147         /* What the domain refers to this vbd as. */
148         blkif_vdev_t            handle;
149         /* Non-zero -> read-only */
150         unsigned char           readonly;
151         /* VDISK_xxx */
152         unsigned char           type;
153         /* phys device that this vbd maps to. */
154         u32                     pdevice;
155         struct block_device     *bdev;
156         /* Cached size parameter. */
157         sector_t                size;
158         bool                    flush_support;
159 };
160
161 struct backend_info;
162
163 struct xen_blkif {
164         /* Unique identifier for this interface. */
165         domid_t                 domid;
166         unsigned int            handle;
167         /* Physical parameters of the comms window. */
168         unsigned int            irq;
169         /* Comms information. */
170         enum blkif_protocol     blk_protocol;
171         enum blkif_backend_type blk_backend_type;
172         union blkif_back_rings  blk_rings;
173         void                    *blk_ring;
174         /* The VBD attached to this interface. */
175         struct xen_vbd          vbd;
176         /* Back pointer to the backend_info. */
177         struct backend_info     *be;
178         /* Private fields. */
179         spinlock_t              blk_ring_lock;
180         atomic_t                refcnt;
181
182         wait_queue_head_t       wq;
183         /* for barrier (drain) requests */
184         struct completion       drain_complete;
185         atomic_t                drain;
186         /* One thread per one blkif. */
187         struct task_struct      *xenblkd;
188         unsigned int            waiting_reqs;
189
190         /* statistics */
191         unsigned long           st_print;
192         int                     st_rd_req;
193         int                     st_wr_req;
194         int                     st_oo_req;
195         int                     st_f_req;
196         int                     st_ds_req;
197         int                     st_rd_sect;
198         int                     st_wr_sect;
199
200         wait_queue_head_t       waiting_to_free;
201         /* Thread shutdown wait queue. */
202         wait_queue_head_t       shutdown_wq;
203 };
204
205
206 #define vbd_sz(_v)      ((_v)->bdev->bd_part ? \
207                          (_v)->bdev->bd_part->nr_sects : \
208                           get_capacity((_v)->bdev->bd_disk))
209
210 #define xen_blkif_get(_b) (atomic_inc(&(_b)->refcnt))
211 #define xen_blkif_put(_b)                               \
212         do {                                            \
213                 if (atomic_dec_and_test(&(_b)->refcnt)) \
214                         wake_up(&(_b)->waiting_to_free);\
215         } while (0)
216
217 struct phys_req {
218         unsigned short          dev;
219         blkif_sector_t          nr_sects;
220         struct block_device     *bdev;
221         blkif_sector_t          sector_number;
222 };
223 int xen_blkif_interface_init(void);
224
225 int xen_blkif_xenbus_init(void);
226
227 irqreturn_t xen_blkif_be_int(int irq, void *dev_id);
228 int xen_blkif_schedule(void *arg);
229
230 int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt,
231                               struct backend_info *be, int state);
232
233 int xen_blkbk_barrier(struct xenbus_transaction xbt,
234                       struct backend_info *be, int state);
235 struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be);
236
237 static inline void blkif_get_x86_32_req(struct blkif_request *dst,
238                                         struct blkif_x86_32_request *src)
239 {
240         int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST;
241         dst->operation = ACCESS_ONCE(src->operation);
242         dst->nr_segments = src->nr_segments;
243         dst->handle = src->handle;
244         dst->id = src->id;
245         switch (dst->operation) {
246         case BLKIF_OP_READ:
247         case BLKIF_OP_WRITE:
248         case BLKIF_OP_WRITE_BARRIER:
249         case BLKIF_OP_FLUSH_DISKCACHE:
250                 dst->u.rw.sector_number = src->u.rw.sector_number;
251                 barrier();
252                 if (n > dst->nr_segments)
253                         n = dst->nr_segments;
254                 for (i = 0; i < n; i++)
255                         dst->u.rw.seg[i] = src->u.rw.seg[i];
256                 break;
257         case BLKIF_OP_DISCARD:
258                 dst->u.discard.sector_number = src->u.discard.sector_number;
259                 dst->u.discard.nr_sectors = src->u.discard.nr_sectors;
260                 break;
261         default:
262                 break;
263         }
264 }
265
266 static inline void blkif_get_x86_64_req(struct blkif_request *dst,
267                                         struct blkif_x86_64_request *src)
268 {
269         int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST;
270         dst->operation = ACCESS_ONCE(src->operation);
271         dst->nr_segments = src->nr_segments;
272         dst->handle = src->handle;
273         dst->id = src->id;
274         switch (dst->operation) {
275         case BLKIF_OP_READ:
276         case BLKIF_OP_WRITE:
277         case BLKIF_OP_WRITE_BARRIER:
278         case BLKIF_OP_FLUSH_DISKCACHE:
279                 dst->u.rw.sector_number = src->u.rw.sector_number;
280                 barrier();
281                 if (n > dst->nr_segments)
282                         n = dst->nr_segments;
283                 for (i = 0; i < n; i++)
284                         dst->u.rw.seg[i] = src->u.rw.seg[i];
285                 break;
286         case BLKIF_OP_DISCARD:
287                 dst->u.discard.sector_number = src->u.discard.sector_number;
288                 dst->u.discard.nr_sectors = src->u.discard.nr_sectors;
289                 break;
290         default:
291                 break;
292         }
293 }
294
295 #endif /* __XEN_BLKIF__BACKEND__COMMON_H__ */