2 * Permission is hereby granted, free of charge, to any person obtaining a copy
3 * of this software and associated documentation files (the "Software"), to
4 * deal in the Software without restriction, including without limitation the
5 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6 * sell copies of the Software, and to permit persons to whom the Software is
7 * furnished to do so, subject to the following conditions:
9 * The above copyright notice and this permission notice shall be included in
10 * all copies or substantial portions of the Software.
12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
18 * DEALINGS IN THE SOFTWARE.
21 #ifndef __XEN_BLKIF_H__
22 #define __XEN_BLKIF_H__
24 #include <xen/interface/io/ring.h>
25 #include <xen/interface/io/blkif.h>
26 #include <xen/interface/io/protocols.h>
28 /* Not a real protocol. Used to generate ring structs which contain
29 * the elements common to all protocols only. This way we get a
30 * compiler-checkable way to use common struct elements, so we can
31 * avoid using switch(protocol) in a number of places. */
32 struct blkif_common_request {
35 struct blkif_common_response {
39 /* i386 protocol version */
41 struct blkif_x86_32_request {
42 uint8_t operation; /* BLKIF_OP_??? */
43 uint8_t nr_segments; /* number of segments */
44 blkif_vdev_t handle; /* only for read/write requests */
45 uint64_t id; /* private guest value, echoed in resp */
46 blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */
47 struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
49 struct blkif_x86_32_discard {
50 uint8_t operation; /* BLKIF_OP_DISCARD */
51 uint8_t flag; /* BLKIF_DISCARD_* */
52 blkif_vdev_t handle; /* same as for read/write requests */
53 uint64_t id; /* private guest value, echoed in resp */
54 blkif_sector_t sector_number;/* start sector idx on disk */
55 uint64_t nr_sectors; /* number of contiguous sectors */
57 struct blkif_x86_32_response {
58 uint64_t id; /* copied from request */
59 uint8_t operation; /* copied from request */
60 int16_t status; /* BLKIF_RSP_??? */
62 typedef struct blkif_x86_32_request blkif_x86_32_request_t;
63 typedef struct blkif_x86_32_discard blkif_x86_32_discard_t;
64 typedef struct blkif_x86_32_response blkif_x86_32_response_t;
67 /* x86_64 protocol version */
68 struct blkif_x86_64_request {
69 uint8_t operation; /* BLKIF_OP_??? */
70 uint8_t nr_segments; /* number of segments */
71 blkif_vdev_t handle; /* only for read/write requests */
72 uint64_t __attribute__((__aligned__(8))) id;
73 blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */
74 struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
76 struct blkif_x86_64_discard {
77 uint8_t operation; /* BLKIF_OP_DISCARD */
78 uint8_t flag; /* BLKIF_DISCARD_* */
79 blkif_vdev_t handle; /* sane as for read/write requests */
80 uint64_t __attribute__((__aligned__(8))) id;
81 blkif_sector_t sector_number;/* start sector idx on disk */
82 uint64_t nr_sectors; /* number of contiguous sectors */
84 struct blkif_x86_64_response {
85 uint64_t __attribute__((__aligned__(8))) id;
86 uint8_t operation; /* copied from request */
87 int16_t status; /* BLKIF_RSP_??? */
89 typedef struct blkif_x86_64_request blkif_x86_64_request_t;
90 typedef struct blkif_x86_64_discard blkif_x86_64_discard_t;
91 typedef struct blkif_x86_64_response blkif_x86_64_response_t;
93 DEFINE_RING_TYPES(blkif_common, struct blkif_common_request, struct blkif_common_response);
94 DEFINE_RING_TYPES(blkif_x86_32, struct blkif_x86_32_request, struct blkif_x86_32_response);
95 DEFINE_RING_TYPES(blkif_x86_64, struct blkif_x86_64_request, struct blkif_x86_64_response);
97 union blkif_back_rings {
98 blkif_back_ring_t native;
99 blkif_common_back_ring_t common;
100 blkif_x86_32_back_ring_t x86_32;
101 blkif_x86_64_back_ring_t x86_64;
103 typedef union blkif_back_rings blkif_back_rings_t;
105 enum blkif_protocol {
106 BLKIF_PROTOCOL_NATIVE = 1,
107 BLKIF_PROTOCOL_X86_32 = 2,
108 BLKIF_PROTOCOL_X86_64 = 3,
111 static void inline blkif_get_x86_32_req(blkif_request_t *dst, blkif_x86_32_request_t *src)
113 int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST;
114 dst->operation = src->operation;
115 dst->nr_segments = src->nr_segments;
116 dst->handle = src->handle;
118 dst->sector_number = src->sector_number;
120 if (unlikely(dst->operation == BLKIF_OP_DISCARD)) {
121 blkif_request_discard_t *d = (void *)dst;
122 const blkif_x86_32_discard_t *s = (const void *)src;
124 /* We should be doing "d->flag = s->flag;" but the
125 * copying of nr_segments does it for us already. */
126 d->nr_sectors = s->nr_sectors;
129 if (n > dst->nr_segments)
130 n = dst->nr_segments;
131 for (i = 0; i < n; i++)
132 dst->seg[i] = src->seg[i];
135 static void inline blkif_get_x86_64_req(blkif_request_t *dst, blkif_x86_64_request_t *src)
137 int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST;
138 dst->operation = src->operation;
139 dst->nr_segments = src->nr_segments;
140 dst->handle = src->handle;
142 dst->sector_number = src->sector_number;
144 if (unlikely(dst->operation == BLKIF_OP_DISCARD)) {
145 blkif_request_discard_t *d = (void *)dst;
146 const blkif_x86_64_discard_t *s = (const void *)src;
148 /* We should be doing "d->flag = s->flag" but the
149 * copying of nr_segments does it for us already. */
150 d->nr_sectors = s->nr_sectors;
153 if (n > dst->nr_segments)
154 n = dst->nr_segments;
155 for (i = 0; i < n; i++)
156 dst->seg[i] = src->seg[i];
159 #endif /* __XEN_BLKIF_H__ */