1ff662b5cSJustin T. Gibbs /*
2ff662b5cSJustin T. Gibbs * Permission is hereby granted, free of charge, to any person obtaining a copy
3ff662b5cSJustin T. Gibbs * of this software and associated documentation files (the "Software"), to
4ff662b5cSJustin T. Gibbs * deal in the Software without restriction, including without limitation the
5ff662b5cSJustin T. Gibbs * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6ff662b5cSJustin T. Gibbs * sell copies of the Software, and to permit persons to whom the Software is
7ff662b5cSJustin T. Gibbs * furnished to do so, subject to the following conditions:
8ff662b5cSJustin T. Gibbs *
9ff662b5cSJustin T. Gibbs * The above copyright notice and this permission notice shall be included in
10ff662b5cSJustin T. Gibbs * all copies or substantial portions of the Software.
11ff662b5cSJustin T. Gibbs *
12ff662b5cSJustin T. Gibbs * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13ff662b5cSJustin T. Gibbs * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14ff662b5cSJustin T. Gibbs * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15ff662b5cSJustin T. Gibbs * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16ff662b5cSJustin T. Gibbs * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17ff662b5cSJustin T. Gibbs * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
18ff662b5cSJustin T. Gibbs * DEALINGS IN THE SOFTWARE.
19ff662b5cSJustin T. Gibbs */
20ff662b5cSJustin T. Gibbs
21ff662b5cSJustin T. Gibbs #ifndef __XEN_BLKIF_H__
22ff662b5cSJustin T. Gibbs #define __XEN_BLKIF_H__
23ff662b5cSJustin T. Gibbs
24*ad7dd514SElliott Mitchell #include <contrib/xen/io/ring.h>
25*ad7dd514SElliott Mitchell #include <contrib/xen/io/blkif.h>
26*ad7dd514SElliott Mitchell #include <contrib/xen/io/protocols.h>
27ff662b5cSJustin T. Gibbs
28ff662b5cSJustin T. Gibbs /* Not a real protocol. Used to generate ring structs which contain
29ff662b5cSJustin T. Gibbs * the elements common to all protocols only. This way we get a
30ff662b5cSJustin T. Gibbs * compiler-checkable way to use common struct elements, so we can
31ff662b5cSJustin T. Gibbs * avoid using switch(protocol) in a number of places. */
32ff662b5cSJustin T. Gibbs struct blkif_common_request {
33ff662b5cSJustin T. Gibbs char dummy;
34ff662b5cSJustin T. Gibbs };
35ff662b5cSJustin T. Gibbs struct blkif_common_response {
36ff662b5cSJustin T. Gibbs char dummy;
37ff662b5cSJustin T. Gibbs };
38ff662b5cSJustin T. Gibbs
39ff662b5cSJustin T. Gibbs /* i386 protocol version */
40ff662b5cSJustin T. Gibbs #pragma pack(push, 4)
41ff662b5cSJustin T. Gibbs struct blkif_x86_32_request {
42ff662b5cSJustin T. Gibbs uint8_t operation; /* BLKIF_OP_??? */
43ff662b5cSJustin T. Gibbs uint8_t nr_segments; /* number of segments */
44ff662b5cSJustin T. Gibbs blkif_vdev_t handle; /* only for read/write requests */
45ff662b5cSJustin T. Gibbs uint64_t id; /* private guest value, echoed in resp */
46ff662b5cSJustin T. Gibbs blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */
47112cacaeSRoger Pau Monné struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
48ff662b5cSJustin T. Gibbs };
49ff662b5cSJustin T. Gibbs struct blkif_x86_32_response {
50ff662b5cSJustin T. Gibbs uint64_t id; /* copied from request */
51ff662b5cSJustin T. Gibbs uint8_t operation; /* copied from request */
52ff662b5cSJustin T. Gibbs int16_t status; /* BLKIF_RSP_??? */
53ff662b5cSJustin T. Gibbs };
54ff662b5cSJustin T. Gibbs typedef struct blkif_x86_32_request blkif_x86_32_request_t;
55ff662b5cSJustin T. Gibbs typedef struct blkif_x86_32_response blkif_x86_32_response_t;
56ff662b5cSJustin T. Gibbs #pragma pack(pop)
57ff662b5cSJustin T. Gibbs
58ff662b5cSJustin T. Gibbs /* x86_64 protocol version */
59ff662b5cSJustin T. Gibbs struct blkif_x86_64_request {
60ff662b5cSJustin T. Gibbs uint8_t operation; /* BLKIF_OP_??? */
61ff662b5cSJustin T. Gibbs uint8_t nr_segments; /* number of segments */
62ff662b5cSJustin T. Gibbs blkif_vdev_t handle; /* only for read/write requests */
63ff662b5cSJustin T. Gibbs uint64_t __attribute__((__aligned__(8))) id;
64ff662b5cSJustin T. Gibbs blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */
65112cacaeSRoger Pau Monné struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
66ff662b5cSJustin T. Gibbs };
67ff662b5cSJustin T. Gibbs struct blkif_x86_64_response {
68ff662b5cSJustin T. Gibbs uint64_t __attribute__((__aligned__(8))) id;
69ff662b5cSJustin T. Gibbs uint8_t operation; /* copied from request */
70ff662b5cSJustin T. Gibbs int16_t status; /* BLKIF_RSP_??? */
71ff662b5cSJustin T. Gibbs };
72ff662b5cSJustin T. Gibbs typedef struct blkif_x86_64_request blkif_x86_64_request_t;
73ff662b5cSJustin T. Gibbs typedef struct blkif_x86_64_response blkif_x86_64_response_t;
74ff662b5cSJustin T. Gibbs
75ff662b5cSJustin T. Gibbs DEFINE_RING_TYPES(blkif_common, struct blkif_common_request, struct blkif_common_response);
76ff662b5cSJustin T. Gibbs DEFINE_RING_TYPES(blkif_x86_32, struct blkif_x86_32_request, struct blkif_x86_32_response);
77ff662b5cSJustin T. Gibbs DEFINE_RING_TYPES(blkif_x86_64, struct blkif_x86_64_request, struct blkif_x86_64_response);
78ff662b5cSJustin T. Gibbs
79ff662b5cSJustin T. Gibbs /*
80ff662b5cSJustin T. Gibbs * Maximum number of requests that can be active for a given instance
81ff662b5cSJustin T. Gibbs * regardless of the protocol in use, based on the ring size. This constant
82ff662b5cSJustin T. Gibbs * facilitates resource pre-allocation in backend drivers since the size is
83ff662b5cSJustin T. Gibbs * known well in advance of attaching to a front end.
84ff662b5cSJustin T. Gibbs */
85ff662b5cSJustin T. Gibbs #define BLKIF_MAX_RING_REQUESTS(_sz) \
86ff662b5cSJustin T. Gibbs MAX(__RING_SIZE((blkif_x86_64_sring_t *)NULL, _sz), \
87ff662b5cSJustin T. Gibbs MAX(__RING_SIZE((blkif_x86_32_sring_t *)NULL, _sz), \
88ff662b5cSJustin T. Gibbs __RING_SIZE((blkif_sring_t *)NULL, _sz)))
89ff662b5cSJustin T. Gibbs
90ff662b5cSJustin T. Gibbs /*
91ff662b5cSJustin T. Gibbs * The number of ring pages required to support a given number of requests
92ff662b5cSJustin T. Gibbs * for a given instance regardless of the protocol in use.
93ff662b5cSJustin T. Gibbs */
94ff662b5cSJustin T. Gibbs #define BLKIF_RING_PAGES(_entries) \
95ff662b5cSJustin T. Gibbs MAX(__RING_PAGES((blkif_x86_64_sring_t *)NULL, _entries), \
96ff662b5cSJustin T. Gibbs MAX(__RING_PAGES((blkif_x86_32_sring_t *)NULL, _entries), \
97ff662b5cSJustin T. Gibbs __RING_PAGES((blkif_sring_t *)NULL, _entries)))
98ff662b5cSJustin T. Gibbs
99ff662b5cSJustin T. Gibbs union blkif_back_rings {
100ff662b5cSJustin T. Gibbs blkif_back_ring_t native;
101ff662b5cSJustin T. Gibbs blkif_common_back_ring_t common;
102ff662b5cSJustin T. Gibbs blkif_x86_32_back_ring_t x86_32;
103ff662b5cSJustin T. Gibbs blkif_x86_64_back_ring_t x86_64;
104ff662b5cSJustin T. Gibbs };
105ff662b5cSJustin T. Gibbs typedef union blkif_back_rings blkif_back_rings_t;
106ff662b5cSJustin T. Gibbs
107ff662b5cSJustin T. Gibbs enum blkif_protocol {
108ff662b5cSJustin T. Gibbs BLKIF_PROTOCOL_NATIVE = 1,
109ff662b5cSJustin T. Gibbs BLKIF_PROTOCOL_X86_32 = 2,
110ff662b5cSJustin T. Gibbs BLKIF_PROTOCOL_X86_64 = 3,
111ff662b5cSJustin T. Gibbs };
112ff662b5cSJustin T. Gibbs
blkif_get_x86_32_req(blkif_request_t * dst,blkif_x86_32_request_t * src)113ff662b5cSJustin T. Gibbs static void inline blkif_get_x86_32_req(blkif_request_t *dst, blkif_x86_32_request_t *src)
114ff662b5cSJustin T. Gibbs {
115112cacaeSRoger Pau Monné int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST;
116ff662b5cSJustin T. Gibbs dst->operation = src->operation;
117ff662b5cSJustin T. Gibbs dst->nr_segments = src->nr_segments;
118ff662b5cSJustin T. Gibbs dst->handle = src->handle;
119ff662b5cSJustin T. Gibbs dst->id = src->id;
120ff662b5cSJustin T. Gibbs dst->sector_number = src->sector_number;
1212f9ec994SRoger Pau Monné __compiler_membar();
122ff662b5cSJustin T. Gibbs if (n > dst->nr_segments)
123ff662b5cSJustin T. Gibbs n = dst->nr_segments;
124ff662b5cSJustin T. Gibbs for (i = 0; i < n; i++)
125ff662b5cSJustin T. Gibbs dst->seg[i] = src->seg[i];
126ff662b5cSJustin T. Gibbs }
127ff662b5cSJustin T. Gibbs
blkif_get_x86_64_req(blkif_request_t * dst,blkif_x86_64_request_t * src)128ff662b5cSJustin T. Gibbs static void inline blkif_get_x86_64_req(blkif_request_t *dst, blkif_x86_64_request_t *src)
129ff662b5cSJustin T. Gibbs {
130112cacaeSRoger Pau Monné int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST;
131ff662b5cSJustin T. Gibbs dst->operation = src->operation;
132ff662b5cSJustin T. Gibbs dst->nr_segments = src->nr_segments;
133ff662b5cSJustin T. Gibbs dst->handle = src->handle;
134ff662b5cSJustin T. Gibbs dst->id = src->id;
135ff662b5cSJustin T. Gibbs dst->sector_number = src->sector_number;
1362f9ec994SRoger Pau Monné __compiler_membar();
137ff662b5cSJustin T. Gibbs if (n > dst->nr_segments)
138ff662b5cSJustin T. Gibbs n = dst->nr_segments;
139ff662b5cSJustin T. Gibbs for (i = 0; i < n; i++)
140ff662b5cSJustin T. Gibbs dst->seg[i] = src->seg[i];
141ff662b5cSJustin T. Gibbs }
142ff662b5cSJustin T. Gibbs
143ff662b5cSJustin T. Gibbs #endif /* __XEN_BLKIF_H__ */
144