blkif.h revision 287801
1/******************************************************************************
2 * blkif.h
3 *
4 * Unified block-device I/O interface for Xen guest OSes.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *
24 * Copyright (c) 2003-2004, Keir Fraser
25 * Copyright (c) 2012, Spectra Logic Corporation
26 */
27
28#ifndef __XEN_PUBLIC_IO_BLKIF_H__
29#define __XEN_PUBLIC_IO_BLKIF_H__
30
31#include "ring.h"
32#include "../grant_table.h"
33
34/*
35 * Front->back notifications: When enqueuing a new request, sending a
36 * notification can be made conditional on req_event (i.e., the generic
37 * hold-off mechanism provided by the ring macros). Backends must set
38 * req_event appropriately (e.g., using RING_FINAL_CHECK_FOR_REQUESTS()).
39 *
40 * Back->front notifications: When enqueuing a new response, sending a
41 * notification can be made conditional on rsp_event (i.e., the generic
42 * hold-off mechanism provided by the ring macros). Frontends must set
43 * rsp_event appropriately (e.g., using RING_FINAL_CHECK_FOR_RESPONSES()).
44 */
45
46#ifndef blkif_vdev_t
47#define blkif_vdev_t   uint16_t
48#endif
49#define blkif_sector_t uint64_t
50
51/*
52 * Feature and Parameter Negotiation
53 * =================================
54 * The two halves of a Xen block driver utilize nodes within the XenStore to
55 * communicate capabilities and to negotiate operating parameters.  This
56 * section enumerates these nodes which reside in the respective front and
57 * backend portions of the XenStore, following the XenBus convention.
58 *
59 * All data in the XenStore is stored as strings.  Nodes specifying numeric
60 * values are encoded in decimal.  Integer value ranges listed below are
61 * expressed as fixed sized integer types capable of storing the conversion
62 * of a properly formatted node string, without loss of information.
63 *
64 * Any specified default value is in effect if the corresponding XenBus node
65 * is not present in the XenStore.
66 *
67 * XenStore nodes in sections marked "PRIVATE" are solely for use by the
68 * driver side whose XenBus tree contains them.
69 *
70 * XenStore nodes marked "DEPRECATED" in their notes section should only be
71 * used to provide interoperability with legacy implementations.
72 *
73 * See the XenBus state transition diagram below for details on when XenBus
74 * nodes must be published and when they can be queried.
75 *
76 *****************************************************************************
77 *                            Backend XenBus Nodes
78 *****************************************************************************
79 *
80 *------------------ Backend Device Identification (PRIVATE) ------------------
81 *
82 * mode
83 *      Values:         "r" (read only), "w" (writable)
84 *
85 *      The read or write access permissions to the backing store to be
86 *      granted to the frontend.
87 *
88 * params
89 *      Values:         string
90 *
91 *      Data used by the backend driver to locate and configure the backing
92 *      device.  The format and semantics of this data vary according to the
93 *      backing device in use and are outside the scope of this specification.
94 *
95 * type
96 *      Values:         "file", "phy", "tap"
97 *
98 *      The type of the backing device/object.
99 *
100 *--------------------------------- Features ---------------------------------
101 *
102 * feature-barrier
103 *      Values:         0/1 (boolean)
104 *      Default Value:  0
105 *
106 *      A value of "1" indicates that the backend can process requests
107 *      containing the BLKIF_OP_WRITE_BARRIER request opcode.  Requests
108 *      of this type may still be returned at any time with the
109 *      BLKIF_RSP_EOPNOTSUPP result code.
110 *
111 * feature-flush-cache
112 *      Values:         0/1 (boolean)
113 *      Default Value:  0
114 *
115 *      A value of "1" indicates that the backend can process requests
116 *      containing the BLKIF_OP_FLUSH_DISKCACHE request opcode.  Requests
117 *      of this type may still be returned at any time with the
118 *      BLKIF_RSP_EOPNOTSUPP result code.
119 *
120 * feature-discard
121 *      Values:         0/1 (boolean)
122 *      Default Value:  0
123 *
124 *      A value of "1" indicates that the backend can process requests
125 *      containing the BLKIF_OP_DISCARD request opcode.  Requests
126 *      of this type may still be returned at any time with the
127 *      BLKIF_RSP_EOPNOTSUPP result code.
128 *
129 *----------------------- Request Transport Parameters ------------------------
130 *
131 * max-ring-page-order
132 *      Values:         <uint32_t>
133 *      Default Value:  0
134 *      Notes:          1, 3
135 *
136 *      The maximum supported size of the request ring buffer in units of
137 *      lb(machine pages). (e.g. 0 == 1 page,  1 = 2 pages, 2 == 4 pages,
138 *      etc.).
139 *
140 * max-ring-pages
141 *      Values:         <uint32_t>
142 *      Default Value:  1
143 *      Notes:          DEPRECATED, 2, 3
144 *
145 *      The maximum supported size of the request ring buffer in units of
146 *      machine pages.  The value must be a power of 2.
147 *
148 *------------------------- Backend Device Properties -------------------------
149 *
150 * discard-alignment
151 *      Values:         <uint32_t>
152 *      Default Value:  0
153 *      Notes:          4, 5
154 *
155 *      The offset, in bytes from the beginning of the virtual block device,
156 *      to the first, addressable, discard extent on the underlying device.
157 *
158 * discard-granularity
159 *      Values:         <uint32_t>
160 *      Default Value:  <"sector-size">
161 *      Notes:          4
162 *
163 *      The size, in bytes, of the individually addressable discard extents
164 *      of the underlying device.
165 *
166 * discard-secure
167 *      Values:         0/1 (boolean)
168 *      Default Value:  0
169 *
170 *      A value of "1" indicates that the backend can process BLKIF_OP_DISCARD
171 *      requests with the BLKIF_DISCARD_SECURE flag set.
172 *
173 * info
174 *      Values:         <uint32_t> (bitmap)
175 *
176 *      A collection of bit flags describing attributes of the backing
177 *      device.  The VDISK_* macros define the meaning of each bit
178 *      location.
179 *
180 * sector-size
181 *      Values:         <uint32_t>
182 *
183 *      The size, in bytes, of the individually addressible data blocks
184 *      on the backend device.
185 *
186 * sectors
187 *      Values:         <uint64_t>
188 *
189 *      The size of the backend device, expressed in units of its native
190 *      sector size ("sector-size").
191 *
192 *****************************************************************************
193 *                            Frontend XenBus Nodes
194 *****************************************************************************
195 *
196 *----------------------- Request Transport Parameters -----------------------
197 *
198 * event-channel
199 *      Values:         <uint32_t>
200 *
201 *      The identifier of the Xen event channel used to signal activity
202 *      in the ring buffer.
203 *
204 * ring-ref
205 *      Values:         <uint32_t>
206 *      Notes:          6
207 *
208 *      The Xen grant reference granting permission for the backend to map
209 *      the sole page in a single page sized ring buffer.
210 *
211 * ring-ref%u
212 *      Values:         <uint32_t>
213 *      Notes:          6
214 *
215 *      For a frontend providing a multi-page ring, a "number of ring pages"
216 *      sized list of nodes, each containing a Xen grant reference granting
217 *      permission for the backend to map the page of the ring located
218 *      at page index "%u".  Page indexes are zero based.
219 *
220 * protocol
221 *      Values:         string (XEN_IO_PROTO_ABI_*)
222 *      Default Value:  XEN_IO_PROTO_ABI_NATIVE
223 *
224 *      The machine ABI rules governing the format of all ring request and
225 *      response structures.
226 *
227 * ring-page-order
228 *      Values:         <uint32_t>
229 *      Default Value:  0
230 *      Maximum Value:  MAX(ffs(max-ring-pages) - 1, max-ring-page-order)
231 *      Notes:          1, 3
232 *
233 *      The size of the frontend allocated request ring buffer in units
234 *      of lb(machine pages). (e.g. 0 == 1 page, 1 = 2 pages, 2 == 4 pages,
235 *      etc.).
236 *
237 * num-ring-pages
238 *      Values:         <uint32_t>
239 *      Default Value:  1
240 *      Maximum Value:  MAX(max-ring-pages,(0x1 << max-ring-page-order))
241 *      Notes:          DEPRECATED, 2, 3
242 *
243 *      The size of the frontend allocated request ring buffer in units of
244 *      machine pages.  The value must be a power of 2.
245 *
246 *------------------------- Virtual Device Properties -------------------------
247 *
248 * device-type
249 *      Values:         "disk", "cdrom", "floppy", etc.
250 *
251 * virtual-device
252 *      Values:         <uint32_t>
253 *
254 *      A value indicating the physical device to virtualize within the
255 *      frontend's domain.  (e.g. "The first ATA disk", "The third SCSI
256 *      disk", etc.)
257 *
258 *      See docs/misc/vbd-interface.txt for details on the format of this
259 *      value.
260 *
261 * Notes
262 * -----
263 * (1) Multi-page ring buffer scheme first developed in the Citrix XenServer
264 *     PV drivers.
265 * (2) Multi-page ring buffer scheme first used in some Red Hat distributions
266 *     including a distribution deployed on certain nodes of the Amazon
267 *     EC2 cluster.
268 * (3) Support for multi-page ring buffers was implemented independently,
269 *     in slightly different forms, by both Citrix and Red Hat/Amazon.
270 *     For full interoperability, block front and backends should publish
271 *     identical ring parameters, adjusted for unit differences, to the
272 *     XenStore nodes used in both schemes.
273 * (4) Devices that support discard functionality may internally allocate
274 *     space (discardable extents) in units that are larger than the
275 *     exported logical block size.
276 * (5) The discard-alignment parameter allows a physical device to be
277 *     partitioned into virtual devices that do not necessarily begin or
278 *     end on a discardable extent boundary.
279 * (6) When there is only a single page allocated to the request ring,
280 *     'ring-ref' is used to communicate the grant reference for this
281 *     page to the backend.  When using a multi-page ring, the 'ring-ref'
282 *     node is not created.  Instead 'ring-ref0' - 'ring-refN' are used.
283 */
284
285/*
286 * STATE DIAGRAMS
287 *
288 *****************************************************************************
289 *                                   Startup                                 *
290 *****************************************************************************
291 *
292 * Tool stack creates front and back nodes with state XenbusStateInitialising.
293 *
294 * Front                                Back
295 * =================================    =====================================
296 * XenbusStateInitialising              XenbusStateInitialising
297 *  o Query virtual device               o Query backend device identification
298 *    properties.                          data.
299 *  o Setup OS device instance.          o Open and validate backend device.
300 *                                       o Publish backend features and
301 *                                         transport parameters.
302 *                                                      |
303 *                                                      |
304 *                                                      V
305 *                                      XenbusStateInitWait
306 *
307 * o Query backend features and
308 *   transport parameters.
309 * o Allocate and initialize the
310 *   request ring.
311 * o Publish transport parameters
312 *   that will be in effect during
313 *   this connection.
314 *              |
315 *              |
316 *              V
317 * XenbusStateInitialised
318 *
319 *                                       o Query frontend transport parameters.
320 *                                       o Connect to the request ring and
321 *                                         event channel.
322 *                                       o Publish backend device properties.
323 *                                                      |
324 *                                                      |
325 *                                                      V
326 *                                      XenbusStateConnected
327 *
328 *  o Query backend device properties.
329 *  o Finalize OS virtual device
330 *    instance.
331 *              |
332 *              |
333 *              V
334 * XenbusStateConnected
335 *
336 * Note: Drivers that do not support any optional features, or the negotiation
337 *       of transport parameters, can skip certain states in the state machine:
338 *
339 *       o A frontend may transition to XenbusStateInitialised without
340 *         waiting for the backend to enter XenbusStateInitWait.  In this
341 *         case, default transport parameters are in effect and any
342 *         transport parameters published by the frontend must contain
343 *         their default values.
344 *
345 *       o A backend may transition to XenbusStateInitialised, bypassing
346 *         XenbusStateInitWait, without waiting for the frontend to first
347 *         enter the XenbusStateInitialised state.  In this case, default
348 *         transport parameters are in effect and any transport parameters
349 *         published by the backend must contain their default values.
350 *
351 *       Drivers that support optional features and/or transport parameter
352 *       negotiation must tolerate these additional state transition paths.
353 *       In general this means performing the work of any skipped state
354 *       transition, if it has not already been performed, in addition to the
355 *       work associated with entry into the current state.
356 */
357
358/*
359 * REQUEST CODES.
360 */
361#define BLKIF_OP_READ              0
362#define BLKIF_OP_WRITE             1
363/*
364 * All writes issued prior to a request with the BLKIF_OP_WRITE_BARRIER
365 * operation code ("barrier request") must be completed prior to the
366 * execution of the barrier request.  All writes issued after the barrier
367 * request must not execute until after the completion of the barrier request.
368 *
369 * Optional.  See "feature-barrier" XenBus node documentation above.
370 */
371#define BLKIF_OP_WRITE_BARRIER     2
372/*
373 * Commit any uncommitted contents of the backing device's volatile cache
374 * to stable storage.
375 *
376 * Optional.  See "feature-flush-cache" XenBus node documentation above.
377 */
378#define BLKIF_OP_FLUSH_DISKCACHE   3
379/*
380 * Used in SLES sources for device specific command packet
381 * contained within the request. Reserved for that purpose.
382 */
383#define BLKIF_OP_RESERVED_1        4
384/*
385 * Indicate to the backend device that a region of storage is no longer in
386 * use, and may be discarded at any time without impact to the client.  If
387 * the BLKIF_DISCARD_SECURE flag is set on the request, all copies of the
388 * discarded region on the device must be rendered unrecoverable before the
389 * command returns.
390 *
391 * This operation is analogous to performing a trim (ATA) or unmap (SCSI),
392 * command on a native device.
393 *
394 * More information about trim/unmap operations can be found at:
395 * http://t13.org/Documents/UploadedDocuments/docs2008/
396 *     e07154r6-Data_Set_Management_Proposal_for_ATA-ACS2.doc
397 * http://www.seagate.com/staticfiles/support/disc/manuals/
398 *     Interface%20manuals/100293068c.pdf
399 *
400 * Optional.  See "feature-discard", "discard-alignment",
401 * "discard-granularity", and "discard-secure" in the XenBus node
402 * documentation above.
403 */
404#define BLKIF_OP_DISCARD           5
405
406/*
407 * Maximum scatter/gather segments per request.
408 * This is carefully chosen so that sizeof(blkif_ring_t) <= PAGE_SIZE.
409 * NB. This could be 12 if the ring indexes weren't stored in the same page.
410 */
411#define BLKIF_MAX_SEGMENTS_PER_REQUEST 11
412
413/*
414 * NB. first_sect and last_sect in blkif_request_segment, as well as
415 * sector_number in blkif_request, are always expressed in 512-byte units.
416 * However they must be properly aligned to the real sector size of the
417 * physical disk, which is reported in the "sector-size" node in the backend
418 * xenbus info. Also the xenbus "sectors" node is expressed in 512-byte units.
419 */
420struct blkif_request_segment {
421    grant_ref_t gref;        /* reference to I/O buffer frame        */
422    /* @first_sect: first sector in frame to transfer (inclusive).   */
423    /* @last_sect: last sector in frame to transfer (inclusive).     */
424    uint8_t     first_sect, last_sect;
425};
426typedef struct blkif_request_segment blkif_request_segment_t;
427
428/*
429 * Starting ring element for any I/O request.
430 */
431struct blkif_request {
432    uint8_t        operation;    /* BLKIF_OP_???                         */
433    uint8_t        nr_segments;  /* number of segments                   */
434    blkif_vdev_t   handle;       /* only for read/write requests         */
435    uint64_t       id;           /* private guest value, echoed in resp  */
436    blkif_sector_t sector_number;/* start sector idx on disk (r/w only)  */
437    blkif_request_segment_t seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
438};
439typedef struct blkif_request blkif_request_t;
440
441/*
442 * Cast to this structure when blkif_request.operation == BLKIF_OP_DISCARD
443 * sizeof(struct blkif_request_discard) <= sizeof(struct blkif_request)
444 */
445struct blkif_request_discard {
446    uint8_t        operation;    /* BLKIF_OP_DISCARD                     */
447    uint8_t        flag;         /* BLKIF_DISCARD_SECURE or zero         */
448#define BLKIF_DISCARD_SECURE (1<<0)  /* ignored if discard-secure=0      */
449    blkif_vdev_t   handle;       /* same as for read/write requests      */
450    uint64_t       id;           /* private guest value, echoed in resp  */
451    blkif_sector_t sector_number;/* start sector idx on disk             */
452    uint64_t       nr_sectors;   /* number of contiguous sectors to discard*/
453};
454typedef struct blkif_request_discard blkif_request_discard_t;
455
456struct blkif_response {
457    uint64_t        id;              /* copied from request */
458    uint8_t         operation;       /* copied from request */
459    int16_t         status;          /* BLKIF_RSP_???       */
460};
461typedef struct blkif_response blkif_response_t;
462
463/*
464 * STATUS RETURN CODES.
465 */
466 /* Operation not supported (only happens on barrier writes). */
467#define BLKIF_RSP_EOPNOTSUPP  -2
468 /* Operation failed for some unspecified reason (-EIO). */
469#define BLKIF_RSP_ERROR       -1
470 /* Operation completed successfully. */
471#define BLKIF_RSP_OKAY         0
472
473/*
474 * Generate blkif ring structures and types.
475 */
476DEFINE_RING_TYPES(blkif, struct blkif_request, struct blkif_response);
477
478#define VDISK_CDROM        0x1
479#define VDISK_REMOVABLE    0x2
480#define VDISK_READONLY     0x4
481
482#endif /* __XEN_PUBLIC_IO_BLKIF_H__ */
483
484/*
485 * Local variables:
486 * mode: C
487 * c-set-style: "BSD"
488 * c-basic-offset: 4
489 * tab-width: 4
490 * indent-tabs-mode: nil
491 * End:
492 */
493