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 <contrib/xen/io/ring.h>
25#include <contrib/xen/io/blkif.h>
26#include <contrib/xen/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.  */
32struct blkif_common_request {
33	char dummy;
34};
35struct blkif_common_response {
36	char dummy;
37};
38
39/* i386 protocol version */
40#pragma pack(push, 4)
41struct 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};
49struct blkif_x86_32_response {
50	uint64_t        id;              /* copied from request */
51	uint8_t         operation;       /* copied from request */
52	int16_t         status;          /* BLKIF_RSP_???       */
53};
54typedef struct blkif_x86_32_request blkif_x86_32_request_t;
55typedef struct blkif_x86_32_response blkif_x86_32_response_t;
56#pragma pack(pop)
57
58/* x86_64 protocol version */
59struct blkif_x86_64_request {
60	uint8_t        operation;    /* BLKIF_OP_???                         */
61	uint8_t        nr_segments;  /* number of segments                   */
62	blkif_vdev_t   handle;       /* only for read/write requests         */
63	uint64_t       __attribute__((__aligned__(8))) id;
64	blkif_sector_t sector_number;/* start sector idx on disk (r/w only)  */
65	struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
66};
67struct blkif_x86_64_response {
68	uint64_t       __attribute__((__aligned__(8))) id;
69	uint8_t         operation;       /* copied from request */
70	int16_t         status;          /* BLKIF_RSP_???       */
71};
72typedef struct blkif_x86_64_request blkif_x86_64_request_t;
73typedef struct blkif_x86_64_response blkif_x86_64_response_t;
74
75DEFINE_RING_TYPES(blkif_common, struct blkif_common_request, struct blkif_common_response);
76DEFINE_RING_TYPES(blkif_x86_32, struct blkif_x86_32_request, struct blkif_x86_32_response);
77DEFINE_RING_TYPES(blkif_x86_64, struct blkif_x86_64_request, struct blkif_x86_64_response);
78
79/*
80 * Maximum number of requests that can be active for a given instance
81 * regardless of the protocol in use, based on the ring size.  This constant
82 * facilitates resource pre-allocation in backend drivers since the size is
83 * known well in advance of attaching to a front end.
84 */
85#define BLKIF_MAX_RING_REQUESTS(_sz) \
86	MAX(__RING_SIZE((blkif_x86_64_sring_t *)NULL, _sz),	\
87	    MAX(__RING_SIZE((blkif_x86_32_sring_t *)NULL, _sz),	\
88		__RING_SIZE((blkif_sring_t *)NULL, _sz)))
89
90/*
91 * The number of ring pages required to support a given number of requests
92 * for a given instance regardless of the protocol in use.
93 */
94#define BLKIF_RING_PAGES(_entries) \
95	MAX(__RING_PAGES((blkif_x86_64_sring_t *)NULL, _entries),	\
96	    MAX(__RING_PAGES((blkif_x86_32_sring_t *)NULL, _entries),	\
97		__RING_PAGES((blkif_sring_t *)NULL, _entries)))
98
99union blkif_back_rings {
100	blkif_back_ring_t        native;
101	blkif_common_back_ring_t common;
102	blkif_x86_32_back_ring_t x86_32;
103	blkif_x86_64_back_ring_t x86_64;
104};
105typedef union blkif_back_rings blkif_back_rings_t;
106
107enum blkif_protocol {
108	BLKIF_PROTOCOL_NATIVE = 1,
109	BLKIF_PROTOCOL_X86_32 = 2,
110	BLKIF_PROTOCOL_X86_64 = 3,
111};
112
113static void inline blkif_get_x86_32_req(blkif_request_t *dst, blkif_x86_32_request_t *src)
114{
115	int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST;
116	dst->operation = src->operation;
117	dst->nr_segments = src->nr_segments;
118	dst->handle = src->handle;
119	dst->id = src->id;
120	dst->sector_number = src->sector_number;
121	__compiler_membar();
122	if (n > dst->nr_segments)
123		n = dst->nr_segments;
124	for (i = 0; i < n; i++)
125		dst->seg[i] = src->seg[i];
126}
127
128static void inline blkif_get_x86_64_req(blkif_request_t *dst, blkif_x86_64_request_t *src)
129{
130	int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST;
131	dst->operation = src->operation;
132	dst->nr_segments = src->nr_segments;
133	dst->handle = src->handle;
134	dst->id = src->id;
135	dst->sector_number = src->sector_number;
136	__compiler_membar();
137	if (n > dst->nr_segments)
138		n = dst->nr_segments;
139	for (i = 0; i < n; i++)
140		dst->seg[i] = src->seg[i];
141}
142
143#endif /* __XEN_BLKIF_H__ */
144