Update to 3.4-final.
[linux-flexiantxendom0-3.2.10.git] / include / xen / blkif.h
1 /* 
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:
8  *
9  * The above copyright notice and this permission notice shall be included in
10  * all copies or substantial portions of the Software.
11  *
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.
19  */
20
21 #ifndef __XEN_BLKIF_H__
22 #define __XEN_BLKIF_H__
23
24 #include <xen/interface/io/ring.h>
25 #include <xen/interface/io/blkif.h>
26 #include <xen/interface/io/protocols.h>
27
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 {
33         char dummy;
34 };
35 struct blkif_common_response {
36         char dummy;
37 };
38
39 /* i386 protocol version */
40 #pragma pack(push, 4)
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];
48 };
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         */
56 };
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_???       */
61 };
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;
65 #pragma pack(pop)
66
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];
75 };
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         */
83 };
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_???       */
88 };
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;
92
93 #define blkif_native_sring blkif_sring
94 DEFINE_RING_TYPES(blkif_common, struct blkif_common_request, struct blkif_common_response);
95 DEFINE_RING_TYPES(blkif_x86_32, struct blkif_x86_32_request, struct blkif_x86_32_response);
96 DEFINE_RING_TYPES(blkif_x86_64, struct blkif_x86_64_request, struct blkif_x86_64_response);
97
98 union blkif_back_rings {
99         blkif_back_ring_t        native;
100         blkif_common_back_ring_t common;
101         blkif_x86_32_back_ring_t x86_32;
102         blkif_x86_64_back_ring_t x86_64;
103 };
104 typedef union blkif_back_rings blkif_back_rings_t;
105
106 enum blkif_protocol {
107         BLKIF_PROTOCOL_NATIVE = 1,
108         BLKIF_PROTOCOL_X86_32 = 2,
109         BLKIF_PROTOCOL_X86_64 = 3,
110 };
111
112 static void inline blkif_get_x86_32_req(blkif_request_t *dst, blkif_x86_32_request_t *src)
113 {
114         int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST;
115         dst->operation = src->operation;
116         dst->nr_segments = src->nr_segments;
117         dst->handle = src->handle;
118         dst->id = src->id;
119         dst->sector_number = src->sector_number;
120         barrier();
121         if (unlikely(dst->operation == BLKIF_OP_DISCARD)) {
122                 blkif_request_discard_t *d = (void *)dst;
123                 const blkif_x86_32_discard_t *s = (const void *)src;
124
125                 /* We should be doing "d->flag = s->flag;" but the
126                  * copying of nr_segments does it for us already. */
127                 d->nr_sectors = s->nr_sectors;
128                 return;
129         }
130         if (n > dst->nr_segments)
131                 n = dst->nr_segments;
132         for (i = 0; i < n; i++)
133                 dst->seg[i] = src->seg[i];
134 }
135
136 static void inline blkif_get_x86_64_req(blkif_request_t *dst, blkif_x86_64_request_t *src)
137 {
138         int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST;
139         dst->operation = src->operation;
140         dst->nr_segments = src->nr_segments;
141         dst->handle = src->handle;
142         dst->id = src->id;
143         dst->sector_number = src->sector_number;
144         barrier();
145         if (unlikely(dst->operation == BLKIF_OP_DISCARD)) {
146                 blkif_request_discard_t *d = (void *)dst;
147                 const blkif_x86_64_discard_t *s = (const void *)src;
148
149                 /* We should be doing "d->flag = s->flag" but the
150                  * copying of nr_segments does it for us already. */
151                 d->nr_sectors = s->nr_sectors;
152                 return;
153         }
154         if (n > dst->nr_segments)
155                 n = dst->nr_segments;
156         for (i = 0; i < n; i++)
157                 dst->seg[i] = src->seg[i];
158 }
159
160 #endif /* __XEN_BLKIF_H__ */