xref: /illumos-gate/usr/src/uts/common/xen/public/io/blkif.h (revision 55fea89d)
1843e1988Sjohnlev /******************************************************************************
2843e1988Sjohnlev  * blkif.h
3*55fea89dSDan Cross  *
4843e1988Sjohnlev  * Unified block-device I/O interface for Xen guest OSes.
5*55fea89dSDan Cross  *
6843e1988Sjohnlev  * Permission is hereby granted, free of charge, to any person obtaining a copy
7843e1988Sjohnlev  * of this software and associated documentation files (the "Software"), to
8843e1988Sjohnlev  * deal in the Software without restriction, including without limitation the
9843e1988Sjohnlev  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10843e1988Sjohnlev  * sell copies of the Software, and to permit persons to whom the Software is
11843e1988Sjohnlev  * furnished to do so, subject to the following conditions:
12843e1988Sjohnlev  *
13843e1988Sjohnlev  * The above copyright notice and this permission notice shall be included in
14843e1988Sjohnlev  * all copies or substantial portions of the Software.
15843e1988Sjohnlev  *
16843e1988Sjohnlev  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17843e1988Sjohnlev  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18843e1988Sjohnlev  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19843e1988Sjohnlev  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20843e1988Sjohnlev  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21843e1988Sjohnlev  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22843e1988Sjohnlev  * DEALINGS IN THE SOFTWARE.
23843e1988Sjohnlev  *
24843e1988Sjohnlev  * Copyright (c) 2003-2004, Keir Fraser
25843e1988Sjohnlev  */
26843e1988Sjohnlev 
27843e1988Sjohnlev #ifndef __XEN_PUBLIC_IO_BLKIF_H__
28843e1988Sjohnlev #define __XEN_PUBLIC_IO_BLKIF_H__
29843e1988Sjohnlev 
30843e1988Sjohnlev #include "ring.h"
31843e1988Sjohnlev #include "../grant_table.h"
32843e1988Sjohnlev 
33843e1988Sjohnlev /*
34843e1988Sjohnlev  * Front->back notifications: When enqueuing a new request, sending a
35843e1988Sjohnlev  * notification can be made conditional on req_event (i.e., the generic
36843e1988Sjohnlev  * hold-off mechanism provided by the ring macros). Backends must set
37843e1988Sjohnlev  * req_event appropriately (e.g., using RING_FINAL_CHECK_FOR_REQUESTS()).
38*55fea89dSDan Cross  *
39843e1988Sjohnlev  * Back->front notifications: When enqueuing a new response, sending a
40843e1988Sjohnlev  * notification can be made conditional on rsp_event (i.e., the generic
41843e1988Sjohnlev  * hold-off mechanism provided by the ring macros). Frontends must set
42843e1988Sjohnlev  * rsp_event appropriately (e.g., using RING_FINAL_CHECK_FOR_RESPONSES()).
43843e1988Sjohnlev  */
44843e1988Sjohnlev 
45843e1988Sjohnlev #ifndef blkif_vdev_t
46843e1988Sjohnlev #define blkif_vdev_t   uint16_t
47843e1988Sjohnlev #endif
48843e1988Sjohnlev #define blkif_sector_t uint64_t
49843e1988Sjohnlev 
50843e1988Sjohnlev /*
51843e1988Sjohnlev  * REQUEST CODES.
52843e1988Sjohnlev  */
53843e1988Sjohnlev #define BLKIF_OP_READ              0
54843e1988Sjohnlev #define BLKIF_OP_WRITE             1
55843e1988Sjohnlev /*
56843e1988Sjohnlev  * Recognised only if "feature-barrier" is present in backend xenbus info.
57349b53ddSStuart Maybee  * The "feature-barrier" node contains a boolean indicating whether barrier
58349b53ddSStuart Maybee  * requests are likely to succeed or fail. Either way, a barrier request
59843e1988Sjohnlev  * may fail at any time with BLKIF_RSP_EOPNOTSUPP if it is unsupported by
60843e1988Sjohnlev  * the underlying block-device hardware. The boolean simply indicates whether
61349b53ddSStuart Maybee  * or not it is worthwhile for the frontend to attempt barrier requests.
62843e1988Sjohnlev  * If a backend does not recognise BLKIF_OP_WRITE_BARRIER, it should *not*
63843e1988Sjohnlev  * create the "feature-barrier" node!
64843e1988Sjohnlev  */
65843e1988Sjohnlev #define BLKIF_OP_WRITE_BARRIER     2
66349b53ddSStuart Maybee /*
67349b53ddSStuart Maybee  * Recognised if "feature-flush-cache" is present in backend xenbus
68349b53ddSStuart Maybee  * info.  A flush will ask the underlying storage hardware to flush its
69349b53ddSStuart Maybee  * non-volatile caches as appropriate.  The "feature-flush-cache" node
70349b53ddSStuart Maybee  * contains a boolean indicating whether flush requests are likely to
71349b53ddSStuart Maybee  * succeed or fail. Either way, a flush request may fail at any time
72349b53ddSStuart Maybee  * with BLKIF_RSP_EOPNOTSUPP if it is unsupported by the underlying
73349b53ddSStuart Maybee  * block-device hardware. The boolean simply indicates whether or not it
74349b53ddSStuart Maybee  * is worthwhile for the frontend to attempt flushes.  If a backend does
75349b53ddSStuart Maybee  * not recognise BLKIF_OP_WRITE_FLUSH_CACHE, it should *not* create the
76349b53ddSStuart Maybee  * "feature-flush-cache" node!
77349b53ddSStuart Maybee  */
78843e1988Sjohnlev #define BLKIF_OP_FLUSH_DISKCACHE   3
79843e1988Sjohnlev 
80843e1988Sjohnlev /*
81843e1988Sjohnlev  * Maximum scatter/gather segments per request.
82843e1988Sjohnlev  * This is carefully chosen so that sizeof(blkif_ring_t) <= PAGE_SIZE.
83843e1988Sjohnlev  * NB. This could be 12 if the ring indexes weren't stored in the same page.
84843e1988Sjohnlev  */
85843e1988Sjohnlev #define BLKIF_MAX_SEGMENTS_PER_REQUEST 11
86843e1988Sjohnlev 
87*55fea89dSDan Cross /*
88ad09f8b8SMark Johnson  * NB. first_sect and last_sect in blkif_request_segment, as well as
89ad09f8b8SMark Johnson  * sector_number in blkif_request, are always expressed in 512-byte units.
90ad09f8b8SMark Johnson  * However they must be properly aligned to the real sector size of the
91ad09f8b8SMark Johnson  * physical disk, which is reported in the "sector-size" node in the backend
92ad09f8b8SMark Johnson  * xenbus info. Also the xenbus "sectors" node is expressed in 512-byte units.
93ad09f8b8SMark Johnson  */
94a576ab5bSrab struct blkif_request_segment {
95a576ab5bSrab     grant_ref_t gref;        /* reference to I/O buffer frame        */
96a576ab5bSrab     /* @first_sect: first sector in frame to transfer (inclusive).   */
97a576ab5bSrab     /* @last_sect: last sector in frame to transfer (inclusive).     */
98a576ab5bSrab     uint8_t     first_sect, last_sect;
99a576ab5bSrab };
100a576ab5bSrab 
101843e1988Sjohnlev struct blkif_request {
102843e1988Sjohnlev     uint8_t        operation;    /* BLKIF_OP_???                         */
103843e1988Sjohnlev     uint8_t        nr_segments;  /* number of segments                   */
104843e1988Sjohnlev     blkif_vdev_t   handle;       /* only for read/write requests         */
105843e1988Sjohnlev     uint64_t       id;           /* private guest value, echoed in resp  */
106843e1988Sjohnlev     blkif_sector_t sector_number;/* start sector idx on disk (r/w only)  */
107a576ab5bSrab     struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
108843e1988Sjohnlev };
109843e1988Sjohnlev typedef struct blkif_request blkif_request_t;
110843e1988Sjohnlev 
111843e1988Sjohnlev struct blkif_response {
112843e1988Sjohnlev     uint64_t        id;              /* copied from request */
113843e1988Sjohnlev     uint8_t         operation;       /* copied from request */
114843e1988Sjohnlev     int16_t         status;          /* BLKIF_RSP_???       */
115843e1988Sjohnlev };
116843e1988Sjohnlev typedef struct blkif_response blkif_response_t;
117843e1988Sjohnlev 
118843e1988Sjohnlev /*
119843e1988Sjohnlev  * STATUS RETURN CODES.
120843e1988Sjohnlev  */
121843e1988Sjohnlev  /* Operation not supported (only happens on barrier writes). */
122843e1988Sjohnlev #define BLKIF_RSP_EOPNOTSUPP  -2
123843e1988Sjohnlev  /* Operation failed for some unspecified reason (-EIO). */
124843e1988Sjohnlev #define BLKIF_RSP_ERROR       -1
125843e1988Sjohnlev  /* Operation completed successfully. */
126843e1988Sjohnlev #define BLKIF_RSP_OKAY         0
127843e1988Sjohnlev 
128843e1988Sjohnlev /*
129843e1988Sjohnlev  * Generate blkif ring structures and types.
130843e1988Sjohnlev  */
131843e1988Sjohnlev 
132843e1988Sjohnlev DEFINE_RING_TYPES(blkif, struct blkif_request, struct blkif_response);
133843e1988Sjohnlev 
134843e1988Sjohnlev #define VDISK_CDROM        0x1
135843e1988Sjohnlev #define VDISK_REMOVABLE    0x2
136843e1988Sjohnlev #define VDISK_READONLY     0x4
137843e1988Sjohnlev 
138843e1988Sjohnlev #endif /* __XEN_PUBLIC_IO_BLKIF_H__ */
139843e1988Sjohnlev 
140843e1988Sjohnlev /*
141843e1988Sjohnlev  * Local variables:
142843e1988Sjohnlev  * mode: C
143843e1988Sjohnlev  * c-set-style: "BSD"
144843e1988Sjohnlev  * c-basic-offset: 4
145843e1988Sjohnlev  * tab-width: 4
146843e1988Sjohnlev  * indent-tabs-mode: nil
147843e1988Sjohnlev  * End:
148843e1988Sjohnlev  */
149