1214077Sgibbs/*
2214077Sgibbs * Permission is hereby granted, free of charge, to any person obtaining a copy
3214077Sgibbs * of this software and associated documentation files (the "Software"), to
4214077Sgibbs * deal in the Software without restriction, including without limitation the
5214077Sgibbs * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
6214077Sgibbs * sell copies of the Software, and to permit persons to whom the Software is
7214077Sgibbs * furnished to do so, subject to the following conditions:
8214077Sgibbs *
9214077Sgibbs * The above copyright notice and this permission notice shall be included in
10214077Sgibbs * all copies or substantial portions of the Software.
11214077Sgibbs *
12214077Sgibbs * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
13214077Sgibbs * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
14214077Sgibbs * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
15214077Sgibbs * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
16214077Sgibbs * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17214077Sgibbs * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
18214077Sgibbs * DEALINGS IN THE SOFTWARE.
19214077Sgibbs *
20214077Sgibbs * $FreeBSD$
21214077Sgibbs */
22214077Sgibbs
23214077Sgibbs#ifndef __XEN_BLKIF_H__
24214077Sgibbs#define __XEN_BLKIF_H__
25214077Sgibbs
26214077Sgibbs#include <xen/interface/io/ring.h>
27214077Sgibbs#include <xen/interface/io/blkif.h>
28214077Sgibbs#include <xen/interface/io/protocols.h>
29214077Sgibbs
30214077Sgibbs/* Not a real protocol.  Used to generate ring structs which contain
31214077Sgibbs * the elements common to all protocols only.  This way we get a
32214077Sgibbs * compiler-checkable way to use common struct elements, so we can
33214077Sgibbs * avoid using switch(protocol) in a number of places.  */
34214077Sgibbsstruct blkif_common_request {
35214077Sgibbs	char dummy;
36214077Sgibbs};
37214077Sgibbsstruct blkif_common_response {
38214077Sgibbs	char dummy;
39214077Sgibbs};
40214077Sgibbs
41214077Sgibbs/* i386 protocol version */
42214077Sgibbs#pragma pack(push, 4)
43214077Sgibbsstruct blkif_x86_32_request {
44214077Sgibbs	uint8_t        operation;    /* BLKIF_OP_???                         */
45214077Sgibbs	uint8_t        nr_segments;  /* number of segments                   */
46214077Sgibbs	blkif_vdev_t   handle;       /* only for read/write requests         */
47214077Sgibbs	uint64_t       id;           /* private guest value, echoed in resp  */
48214077Sgibbs	blkif_sector_t sector_number;/* start sector idx on disk (r/w only)  */
49284296Sroyger	struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
50214077Sgibbs};
51214077Sgibbsstruct blkif_x86_32_response {
52214077Sgibbs	uint64_t        id;              /* copied from request */
53214077Sgibbs	uint8_t         operation;       /* copied from request */
54214077Sgibbs	int16_t         status;          /* BLKIF_RSP_???       */
55214077Sgibbs};
56214077Sgibbstypedef struct blkif_x86_32_request blkif_x86_32_request_t;
57214077Sgibbstypedef struct blkif_x86_32_response blkif_x86_32_response_t;
58214077Sgibbs#pragma pack(pop)
59214077Sgibbs
60214077Sgibbs/* x86_64 protocol version */
61214077Sgibbsstruct blkif_x86_64_request {
62214077Sgibbs	uint8_t        operation;    /* BLKIF_OP_???                         */
63214077Sgibbs	uint8_t        nr_segments;  /* number of segments                   */
64214077Sgibbs	blkif_vdev_t   handle;       /* only for read/write requests         */
65214077Sgibbs	uint64_t       __attribute__((__aligned__(8))) id;
66214077Sgibbs	blkif_sector_t sector_number;/* start sector idx on disk (r/w only)  */
67284296Sroyger	struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
68214077Sgibbs};
69214077Sgibbsstruct blkif_x86_64_response {
70214077Sgibbs	uint64_t       __attribute__((__aligned__(8))) id;
71214077Sgibbs	uint8_t         operation;       /* copied from request */
72214077Sgibbs	int16_t         status;          /* BLKIF_RSP_???       */
73214077Sgibbs};
74214077Sgibbstypedef struct blkif_x86_64_request blkif_x86_64_request_t;
75214077Sgibbstypedef struct blkif_x86_64_response blkif_x86_64_response_t;
76214077Sgibbs
77214077SgibbsDEFINE_RING_TYPES(blkif_common, struct blkif_common_request, struct blkif_common_response);
78214077SgibbsDEFINE_RING_TYPES(blkif_x86_32, struct blkif_x86_32_request, struct blkif_x86_32_response);
79214077SgibbsDEFINE_RING_TYPES(blkif_x86_64, struct blkif_x86_64_request, struct blkif_x86_64_response);
80214077Sgibbs
81214077Sgibbs/*
82214077Sgibbs * Maximum number of requests that can be active for a given instance
83214077Sgibbs * regardless of the protocol in use, based on the ring size.  This constant
84214077Sgibbs * facilitates resource pre-allocation in backend drivers since the size is
85214077Sgibbs * known well in advance of attaching to a front end.
86214077Sgibbs */
87214077Sgibbs#define BLKIF_MAX_RING_REQUESTS(_sz) \
88214077Sgibbs	MAX(__RING_SIZE((blkif_x86_64_sring_t *)NULL, _sz),	\
89214077Sgibbs	    MAX(__RING_SIZE((blkif_x86_32_sring_t *)NULL, _sz),	\
90214077Sgibbs		__RING_SIZE((blkif_sring_t *)NULL, _sz)))
91214077Sgibbs
92214077Sgibbs/*
93214077Sgibbs * The number of ring pages required to support a given number of requests
94214077Sgibbs * for a given instance regardless of the protocol in use.
95214077Sgibbs */
96214077Sgibbs#define BLKIF_RING_PAGES(_entries) \
97214077Sgibbs	MAX(__RING_PAGES((blkif_x86_64_sring_t *)NULL, _entries),	\
98214077Sgibbs	    MAX(__RING_PAGES((blkif_x86_32_sring_t *)NULL, _entries),	\
99214077Sgibbs		__RING_PAGES((blkif_sring_t *)NULL, _entries)))
100214077Sgibbs
101214077Sgibbsunion blkif_back_rings {
102214077Sgibbs	blkif_back_ring_t        native;
103214077Sgibbs	blkif_common_back_ring_t common;
104214077Sgibbs	blkif_x86_32_back_ring_t x86_32;
105214077Sgibbs	blkif_x86_64_back_ring_t x86_64;
106214077Sgibbs};
107214077Sgibbstypedef union blkif_back_rings blkif_back_rings_t;
108214077Sgibbs
109214077Sgibbsenum blkif_protocol {
110214077Sgibbs	BLKIF_PROTOCOL_NATIVE = 1,
111214077Sgibbs	BLKIF_PROTOCOL_X86_32 = 2,
112214077Sgibbs	BLKIF_PROTOCOL_X86_64 = 3,
113214077Sgibbs};
114214077Sgibbs
115214077Sgibbsstatic void inline blkif_get_x86_32_req(blkif_request_t *dst, blkif_x86_32_request_t *src)
116214077Sgibbs{
117284296Sroyger	int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST;
118214077Sgibbs	dst->operation = src->operation;
119214077Sgibbs	dst->nr_segments = src->nr_segments;
120214077Sgibbs	dst->handle = src->handle;
121214077Sgibbs	dst->id = src->id;
122214077Sgibbs	dst->sector_number = src->sector_number;
123289686Sroyger	__compiler_membar();
124214077Sgibbs	if (n > dst->nr_segments)
125214077Sgibbs		n = dst->nr_segments;
126214077Sgibbs	for (i = 0; i < n; i++)
127214077Sgibbs		dst->seg[i] = src->seg[i];
128214077Sgibbs}
129214077Sgibbs
130214077Sgibbsstatic void inline blkif_get_x86_64_req(blkif_request_t *dst, blkif_x86_64_request_t *src)
131214077Sgibbs{
132284296Sroyger	int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST;
133214077Sgibbs	dst->operation = src->operation;
134214077Sgibbs	dst->nr_segments = src->nr_segments;
135214077Sgibbs	dst->handle = src->handle;
136214077Sgibbs	dst->id = src->id;
137214077Sgibbs	dst->sector_number = src->sector_number;
138289686Sroyger	__compiler_membar();
139214077Sgibbs	if (n > dst->nr_segments)
140214077Sgibbs		n = dst->nr_segments;
141214077Sgibbs	for (i = 0; i < n; i++)
142214077Sgibbs		dst->seg[i] = src->seg[i];
143214077Sgibbs}
144214077Sgibbs
145214077Sgibbs#endif /* __XEN_BLKIF_H__ */
146