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:
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:
15 * The above copyright notice and this permission notice shall be included in
16 * all copies or substantial portions of the Software.
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
27 #ifndef __XEN_BLKIF__BACKEND__COMMON_H__
28 #define __XEN_BLKIF__BACKEND__COMMON_H__
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>
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>
46 #define DRV_PFX "xen-blkback:"
47 #define DPRINTK(fmt, args...) \
48 pr_debug(DRV_PFX "(%s:%d) " fmt ".\n", \
49 __func__, __LINE__, ##args)
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
{
59 struct blkif_common_response
{
63 /* i386 protocol version */
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
];
71 struct blkif_x86_32_request_discard
{
72 blkif_sector_t sector_number
;/* start sector idx on disk (r/w only) */
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 */
82 struct blkif_x86_32_request_rw rw
;
83 struct blkif_x86_32_request_discard discard
;
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_??? */
93 /* x86_64 protocol version */
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
];
100 struct blkif_x86_64_request_discard
{
101 blkif_sector_t sector_number
;/* start sector idx on disk (r/w only) */
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
;
111 struct blkif_x86_64_request_rw rw
;
112 struct blkif_x86_64_request_discard discard
;
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_??? */
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
);
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
;
135 enum blkif_protocol
{
136 BLKIF_PROTOCOL_NATIVE
= 1,
137 BLKIF_PROTOCOL_X86_32
= 2,
138 BLKIF_PROTOCOL_X86_64
= 3,
141 enum blkif_backend_type
{
142 BLKIF_BACKEND_PHY
= 1,
143 BLKIF_BACKEND_FILE
= 2,
147 /* What the domain refers to this vbd as. */
149 /* Non-zero -> read-only */
150 unsigned char readonly
;
153 /* phys device that this vbd maps to. */
155 struct block_device
*bdev
;
156 /* Cached size parameter. */
164 /* Unique identifier for this interface. */
167 /* Physical parameters of the comms window. */
169 /* Comms information. */
170 enum blkif_protocol blk_protocol
;
171 enum blkif_backend_type blk_backend_type
;
172 union blkif_back_rings blk_rings
;
174 /* The VBD attached to this interface. */
176 /* Back pointer to the backend_info. */
177 struct backend_info
*be
;
178 /* Private fields. */
179 spinlock_t blk_ring_lock
;
182 wait_queue_head_t wq
;
183 /* for barrier (drain) requests */
184 struct completion drain_complete
;
186 /* One thread per one blkif. */
187 struct task_struct
*xenblkd
;
188 unsigned int waiting_reqs
;
191 unsigned long st_print
;
200 wait_queue_head_t waiting_to_free
;
204 #define vbd_sz(_v) ((_v)->bdev->bd_part ? \
205 (_v)->bdev->bd_part->nr_sects : \
206 get_capacity((_v)->bdev->bd_disk))
208 #define xen_blkif_get(_b) (atomic_inc(&(_b)->refcnt))
209 #define xen_blkif_put(_b) \
211 if (atomic_dec_and_test(&(_b)->refcnt)) \
212 wake_up(&(_b)->waiting_to_free);\
217 blkif_sector_t nr_sects
;
218 struct block_device
*bdev
;
219 blkif_sector_t sector_number
;
221 int xen_blkif_interface_init(void);
223 int xen_blkif_xenbus_init(void);
225 irqreturn_t
xen_blkif_be_int(int irq
, void *dev_id
);
226 int xen_blkif_schedule(void *arg
);
228 int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt
,
229 struct backend_info
*be
, int state
);
231 int xen_blkbk_barrier(struct xenbus_transaction xbt
,
232 struct backend_info
*be
, int state
);
233 struct xenbus_device
*xen_blkbk_xenbus(struct backend_info
*be
);
235 static inline void blkif_get_x86_32_req(struct blkif_request
*dst
,
236 struct blkif_x86_32_request
*src
)
238 int i
, n
= BLKIF_MAX_SEGMENTS_PER_REQUEST
;
239 dst
->operation
= src
->operation
;
240 dst
->nr_segments
= src
->nr_segments
;
241 dst
->handle
= src
->handle
;
243 switch (src
->operation
) {
246 case BLKIF_OP_WRITE_BARRIER
:
247 case BLKIF_OP_FLUSH_DISKCACHE
:
248 dst
->u
.rw
.sector_number
= src
->u
.rw
.sector_number
;
250 if (n
> dst
->nr_segments
)
251 n
= dst
->nr_segments
;
252 for (i
= 0; i
< n
; i
++)
253 dst
->u
.rw
.seg
[i
] = src
->u
.rw
.seg
[i
];
255 case BLKIF_OP_DISCARD
:
256 dst
->u
.discard
.sector_number
= src
->u
.discard
.sector_number
;
257 dst
->u
.discard
.nr_sectors
= src
->u
.discard
.nr_sectors
;
264 static inline void blkif_get_x86_64_req(struct blkif_request
*dst
,
265 struct blkif_x86_64_request
*src
)
267 int i
, n
= BLKIF_MAX_SEGMENTS_PER_REQUEST
;
268 dst
->operation
= src
->operation
;
269 dst
->nr_segments
= src
->nr_segments
;
270 dst
->handle
= src
->handle
;
272 switch (src
->operation
) {
275 case BLKIF_OP_WRITE_BARRIER
:
276 case BLKIF_OP_FLUSH_DISKCACHE
:
277 dst
->u
.rw
.sector_number
= src
->u
.rw
.sector_number
;
279 if (n
> dst
->nr_segments
)
280 n
= dst
->nr_segments
;
281 for (i
= 0; i
< n
; i
++)
282 dst
->u
.rw
.seg
[i
] = src
->u
.rw
.seg
[i
];
284 case BLKIF_OP_DISCARD
:
285 dst
->u
.discard
.sector_number
= src
->u
.discard
.sector_number
;
286 dst
->u
.discard
.nr_sectors
= src
->u
.discard
.nr_sectors
;
293 #endif /* __XEN_BLKIF__BACKEND__COMMON_H__ */