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