Merge branch 'next/cleanup3' of git://git.linaro.org/people/arnd/arm-soc
[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         struct vm_struct        *blk_ring_area;
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
202         grant_handle_t          shmem_handle;
203         grant_ref_t             shmem_ref;
204 };
205
206
207 #define vbd_sz(_v)      ((_v)->bdev->bd_part ? \
208                          (_v)->bdev->bd_part->nr_sects : \
209                           get_capacity((_v)->bdev->bd_disk))
210
211 #define xen_blkif_get(_b) (atomic_inc(&(_b)->refcnt))
212 #define xen_blkif_put(_b)                               \
213         do {                                            \
214                 if (atomic_dec_and_test(&(_b)->refcnt)) \
215                         wake_up(&(_b)->waiting_to_free);\
216         } while (0)
217
218 struct phys_req {
219         unsigned short          dev;
220         blkif_sector_t          nr_sects;
221         struct block_device     *bdev;
222         blkif_sector_t          sector_number;
223 };
224 int xen_blkif_interface_init(void);
225
226 int xen_blkif_xenbus_init(void);
227
228 irqreturn_t xen_blkif_be_int(int irq, void *dev_id);
229 int xen_blkif_schedule(void *arg);
230
231 int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt,
232                               struct backend_info *be, int state);
233
234 int xen_blkbk_barrier(struct xenbus_transaction xbt,
235                       struct backend_info *be, int state);
236 struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be);
237
238 static inline void blkif_get_x86_32_req(struct blkif_request *dst,
239                                         struct blkif_x86_32_request *src)
240 {
241         int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST;
242         dst->operation = src->operation;
243         dst->nr_segments = src->nr_segments;
244         dst->handle = src->handle;
245         dst->id = src->id;
246         switch (src->operation) {
247         case BLKIF_OP_READ:
248         case BLKIF_OP_WRITE:
249         case BLKIF_OP_WRITE_BARRIER:
250         case BLKIF_OP_FLUSH_DISKCACHE:
251                 dst->u.rw.sector_number = src->u.rw.sector_number;
252                 barrier();
253                 if (n > dst->nr_segments)
254                         n = dst->nr_segments;
255                 for (i = 0; i < n; i++)
256                         dst->u.rw.seg[i] = src->u.rw.seg[i];
257                 break;
258         case BLKIF_OP_DISCARD:
259                 dst->u.discard.sector_number = src->u.discard.sector_number;
260                 dst->u.discard.nr_sectors = src->u.discard.nr_sectors;
261                 break;
262         default:
263                 break;
264         }
265 }
266
267 static inline void blkif_get_x86_64_req(struct blkif_request *dst,
268                                         struct blkif_x86_64_request *src)
269 {
270         int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST;
271         dst->operation = src->operation;
272         dst->nr_segments = src->nr_segments;
273         dst->handle = src->handle;
274         dst->id = src->id;
275         switch (src->operation) {
276         case BLKIF_OP_READ:
277         case BLKIF_OP_WRITE:
278         case BLKIF_OP_WRITE_BARRIER:
279         case BLKIF_OP_FLUSH_DISKCACHE:
280                 dst->u.rw.sector_number = src->u.rw.sector_number;
281                 barrier();
282                 if (n > dst->nr_segments)
283                         n = dst->nr_segments;
284                 for (i = 0; i < n; i++)
285                         dst->u.rw.seg[i] = src->u.rw.seg[i];
286                 break;
287         case BLKIF_OP_DISCARD:
288                 dst->u.discard.sector_number = src->u.discard.sector_number;
289                 dst->u.discard.nr_sectors = src->u.discard.nr_sectors;
290                 break;
291         default:
292                 break;
293         }
294 }
295
296 #endif /* __XEN_BLKIF__BACKEND__COMMON_H__ */