Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/trivial
[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 struct blkif_x86_32_request {
66         uint8_t        operation;    /* BLKIF_OP_???                         */
67         uint8_t        nr_segments;  /* number of segments                   */
68         blkif_vdev_t   handle;       /* only for read/write requests         */
69         uint64_t       id;           /* private guest value, echoed in resp  */
70         blkif_sector_t sector_number;/* start sector idx on disk (r/w only)  */
71         struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
72 };
73 struct blkif_x86_32_response {
74         uint64_t        id;              /* copied from request */
75         uint8_t         operation;       /* copied from request */
76         int16_t         status;          /* BLKIF_RSP_???       */
77 };
78 #pragma pack(pop)
79
80 /* x86_64 protocol version */
81 struct blkif_x86_64_request {
82         uint8_t        operation;    /* BLKIF_OP_???                         */
83         uint8_t        nr_segments;  /* number of segments                   */
84         blkif_vdev_t   handle;       /* only for read/write requests         */
85         uint64_t       __attribute__((__aligned__(8))) id;
86         blkif_sector_t sector_number;/* start sector idx on disk (r/w only)  */
87         struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
88 };
89 struct blkif_x86_64_response {
90         uint64_t       __attribute__((__aligned__(8))) id;
91         uint8_t         operation;       /* copied from request */
92         int16_t         status;          /* BLKIF_RSP_???       */
93 };
94
95 DEFINE_RING_TYPES(blkif_common, struct blkif_common_request,
96                   struct blkif_common_response);
97 DEFINE_RING_TYPES(blkif_x86_32, struct blkif_x86_32_request,
98                   struct blkif_x86_32_response);
99 DEFINE_RING_TYPES(blkif_x86_64, struct blkif_x86_64_request,
100                   struct blkif_x86_64_response);
101
102 union blkif_back_rings {
103         struct blkif_back_ring        native;
104         struct blkif_common_back_ring common;
105         struct blkif_x86_32_back_ring x86_32;
106         struct blkif_x86_64_back_ring x86_64;
107 };
108
109 enum blkif_protocol {
110         BLKIF_PROTOCOL_NATIVE = 1,
111         BLKIF_PROTOCOL_X86_32 = 2,
112         BLKIF_PROTOCOL_X86_64 = 3,
113 };
114
115 struct xen_vbd {
116         /* What the domain refers to this vbd as. */
117         blkif_vdev_t            handle;
118         /* Non-zero -> read-only */
119         unsigned char           readonly;
120         /* VDISK_xxx */
121         unsigned char           type;
122         /* phys device that this vbd maps to. */
123         u32                     pdevice;
124         struct block_device     *bdev;
125         /* Cached size parameter. */
126         sector_t                size;
127         bool                    flush_support;
128 };
129
130 struct backend_info;
131
132 struct xen_blkif {
133         /* Unique identifier for this interface. */
134         domid_t                 domid;
135         unsigned int            handle;
136         /* Physical parameters of the comms window. */
137         unsigned int            irq;
138         /* Comms information. */
139         enum blkif_protocol     blk_protocol;
140         union blkif_back_rings  blk_rings;
141         struct vm_struct        *blk_ring_area;
142         /* The VBD attached to this interface. */
143         struct xen_vbd          vbd;
144         /* Back pointer to the backend_info. */
145         struct backend_info     *be;
146         /* Private fields. */
147         spinlock_t              blk_ring_lock;
148         atomic_t                refcnt;
149
150         wait_queue_head_t       wq;
151         /* One thread per one blkif. */
152         struct task_struct      *xenblkd;
153         unsigned int            waiting_reqs;
154
155         /* statistics */
156         unsigned long           st_print;
157         int                     st_rd_req;
158         int                     st_wr_req;
159         int                     st_oo_req;
160         int                     st_f_req;
161         int                     st_rd_sect;
162         int                     st_wr_sect;
163
164         wait_queue_head_t       waiting_to_free;
165
166         grant_handle_t          shmem_handle;
167         grant_ref_t             shmem_ref;
168 };
169
170
171 #define vbd_sz(_v)      ((_v)->bdev->bd_part ? \
172                          (_v)->bdev->bd_part->nr_sects : \
173                           get_capacity((_v)->bdev->bd_disk))
174
175 #define xen_blkif_get(_b) (atomic_inc(&(_b)->refcnt))
176 #define xen_blkif_put(_b)                               \
177         do {                                            \
178                 if (atomic_dec_and_test(&(_b)->refcnt)) \
179                         wake_up(&(_b)->waiting_to_free);\
180         } while (0)
181
182 struct phys_req {
183         unsigned short          dev;
184         unsigned short          nr_sects;
185         struct block_device     *bdev;
186         blkif_sector_t          sector_number;
187 };
188 int xen_blkif_interface_init(void);
189
190 int xen_blkif_xenbus_init(void);
191
192 irqreturn_t xen_blkif_be_int(int irq, void *dev_id);
193 int xen_blkif_schedule(void *arg);
194
195 int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt,
196                               struct backend_info *be, int state);
197
198 struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be);
199
200 static inline void blkif_get_x86_32_req(struct blkif_request *dst,
201                                         struct blkif_x86_32_request *src)
202 {
203         int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST;
204         dst->operation = src->operation;
205         dst->nr_segments = src->nr_segments;
206         dst->handle = src->handle;
207         dst->id = src->id;
208         dst->u.rw.sector_number = src->sector_number;
209         barrier();
210         if (n > dst->nr_segments)
211                 n = dst->nr_segments;
212         for (i = 0; i < n; i++)
213                 dst->u.rw.seg[i] = src->seg[i];
214 }
215
216 static inline void blkif_get_x86_64_req(struct blkif_request *dst,
217                                         struct blkif_x86_64_request *src)
218 {
219         int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST;
220         dst->operation = src->operation;
221         dst->nr_segments = src->nr_segments;
222         dst->handle = src->handle;
223         dst->id = src->id;
224         dst->u.rw.sector_number = src->sector_number;
225         barrier();
226         if (n > dst->nr_segments)
227                 n = dst->nr_segments;
228         for (i = 0; i < n; i++)
229                 dst->u.rw.seg[i] = src->seg[i];
230 }
231
232 #endif /* __XEN_BLKIF__BACKEND__COMMON_H__ */