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 * max-requests         <uint32_t>
149 *      Default Value:  BLKIF_MAX_RING_REQUESTS(PAGE_SIZE)
150 *      Maximum Value:  BLKIF_MAX_RING_REQUESTS(PAGE_SIZE * max-ring-pages)
151 *
152 *      The maximum number of concurrent, logical requests supported by
153 *      the backend.
154 *
155 *      Note: A logical request may span multiple ring entries.
156 *
157 * max-request-segments
158 *      Values:         <uint8_t>
159 *      Default Value:  BLKIF_MAX_SEGMENTS_PER_HEADER_BLOCK
160 *      Maximum Value:  BLKIF_MAX_SEGMENTS_PER_REQUEST
161 *
162 *      The maximum value of blkif_request.nr_segments supported by
163 *      the backend.
164 *
165 * max-request-size
166 *      Values:         <uint32_t>
167 *      Default Value:  BLKIF_MAX_SEGMENTS_PER_HEADER_BLOCK * PAGE_SIZE
168 *      Maximum Value:  BLKIF_MAX_SEGMENTS_PER_REQUEST * PAGE_SIZE
169 *
170 *      The maximum amount of data, in bytes, that can be referenced by a
171 *      request type that accesses frontend memory (currently BLKIF_OP_READ,
172 *      BLKIF_OP_WRITE, or BLKIF_OP_WRITE_BARRIER).
173 *
174 *------------------------- Backend Device Properties -------------------------
175 *
176 * discard-alignment
177 *      Values:         <uint32_t>
178 *      Default Value:  0
179 *      Notes:          4, 5
180 *
181 *      The offset, in bytes from the beginning of the virtual block device,
182 *      to the first, addressable, discard extent on the underlying device.
183 *
184 * discard-granularity
185 *      Values:         <uint32_t>
186 *      Default Value:  <"sector-size">
187 *      Notes:          4
188 *
189 *      The size, in bytes, of the individually addressable discard extents
190 *      of the underlying device.
191 *
192 * discard-secure
193 *      Values:         0/1 (boolean)
194 *      Default Value:  0
195 *
196 *      A value of "1" indicates that the backend can process BLKIF_OP_DISCARD
197 *      requests with the BLKIF_DISCARD_SECURE flag set.
198 *
199 * info
200 *      Values:         <uint32_t> (bitmap)
201 *
202 *      A collection of bit flags describing attributes of the backing
203 *      device.  The VDISK_* macros define the meaning of each bit
204 *      location.
205 *
206 * sector-size
207 *      Values:         <uint32_t>
208 *
209 *      The size, in bytes, of the individually addressible data blocks
210 *      on the backend device.
211 *
212 * sectors
213 *      Values:         <uint64_t>
214 *
215 *      The size of the backend device, expressed in units of its native
216 *      sector size ("sector-size").
217 *
218 *****************************************************************************
219 *                            Frontend XenBus Nodes
220 *****************************************************************************
221 *
222 *----------------------- Request Transport Parameters -----------------------
223 *
224 * event-channel
225 *      Values:         <uint32_t>
226 *
227 *      The identifier of the Xen event channel used to signal activity
228 *      in the ring buffer.
229 *
230 * ring-ref
231 *      Values:         <uint32_t>
232 *      Notes:          6
233 *
234 *      The Xen grant reference granting permission for the backend to map
235 *      the sole page in a single page sized ring buffer.
236 *
237 * ring-ref%u
238 *      Values:         <uint32_t>
239 *      Notes:          6
240 *
241 *      For a frontend providing a multi-page ring, a "number of ring pages"
242 *      sized list of nodes, each containing a Xen grant reference granting
243 *      permission for the backend to map the page of the ring located
244 *      at page index "%u".  Page indexes are zero based.
245 *
246 * protocol
247 *      Values:         string (XEN_IO_PROTO_ABI_*)
248 *      Default Value:  XEN_IO_PROTO_ABI_NATIVE
249 *
250 *      The machine ABI rules governing the format of all ring request and
251 *      response structures.
252 *
253 * ring-page-order
254 *      Values:         <uint32_t>
255 *      Default Value:  0
256 *      Maximum Value:  MAX(ffs(max-ring-pages) - 1, max-ring-page-order)
257 *      Notes:          1, 3
258 *
259 *      The size of the frontend allocated request ring buffer in units
260 *      of lb(machine pages). (e.g. 0 == 1 page, 1 = 2 pages, 2 == 4 pages,
261 *      etc.).
262 *
263 * num-ring-pages
264 *      Values:         <uint32_t>
265 *      Default Value:  1
266 *      Maximum Value:  MAX(max-ring-pages,(0x1 << max-ring-page-order))
267 *      Notes:          DEPRECATED, 2, 3
268 *
269 *      The size of the frontend allocated request ring buffer in units of
270 *      machine pages.  The value must be a power of 2.
271 *
272 * max-requests
273 *      Values:         <uint32_t>
274 *      Default Value:  BLKIF_MAX_RING_REQUESTS(PAGE_SIZE)
275 *      Maximum Value:  BLKIF_MAX_RING_REQUESTS(PAGE_SIZE * max-ring-pages)
276 *
277 *      The maximum number of concurrent, logical requests that will be
278 *      issued by the frontend.
279 *
280 *      Note: A logical request may span multiple ring entries.
281 *
282 * max-request-segments
283 *      Values:         <uint8_t>
284 *      Default Value:  BLKIF_MAX_SEGMENTS_PER_HEADER_BLOCK
285 *      Maximum Value:  MIN(255, backend/max-request-segments)
286 *
287 *      The maximum value the frontend will set in the
288 *      blkif_request.nr_segments field.
289 *
290 * max-request-size
291 *      Values:         <uint32_t>
292 *      Default Value:  BLKIF_MAX_SEGMENTS_PER_HEADER_BLOCK * PAGE_SIZE
293 *      Maximum Value:  max-request-segments * PAGE_SIZE
294 *
295 *      The maximum amount of data, in bytes, that can be referenced by
296 *      a request type that accesses frontend memory (currently BLKIF_OP_READ,
297 *      BLKIF_OP_WRITE, or BLKIF_OP_WRITE_BARRIER).
298 *
299 *------------------------- Virtual Device Properties -------------------------
300 *
301 * device-type
302 *      Values:         "disk", "cdrom", "floppy", etc.
303 *
304 * virtual-device
305 *      Values:         <uint32_t>
306 *
307 *      A value indicating the physical device to virtualize within the
308 *      frontend's domain.  (e.g. "The first ATA disk", "The third SCSI
309 *      disk", etc.)
310 *
311 *      See docs/misc/vbd-interface.txt for details on the format of this
312 *      value.
313 *
314 * Notes
315 * -----
316 * (1) Multi-page ring buffer scheme first developed in the Citrix XenServer
317 *     PV drivers.
318 * (2) Multi-page ring buffer scheme first used in some Red Hat distributions
319 *     including a distribution deployed on certain nodes of the Amazon
320 *     EC2 cluster.
321 * (3) Support for multi-page ring buffers was implemented independently,
322 *     in slightly different forms, by both Citrix and Red Hat/Amazon.
323 *     For full interoperability, block front and backends should publish
324 *     identical ring parameters, adjusted for unit differences, to the
325 *     XenStore nodes used in both schemes.
326 * (4) Devices that support discard functionality may internally allocate
327 *     space (discardable extents) in units that are larger than the
328 *     exported logical block size.
329 * (5) The discard-alignment parameter allows a physical device to be
330 *     partitioned into virtual devices that do not necessarily begin or
331 *     end on a discardable extent boundary.
332 * (6) When there is only a single page allocated to the request ring,
333 *     'ring-ref' is used to communicate the grant reference for this
334 *     page to the backend.  When using a multi-page ring, the 'ring-ref'
335 *     node is not created.  Instead 'ring-ref0' - 'ring-refN' are used.
336 */
337
338/*
339 * STATE DIAGRAMS
340 *
341 *****************************************************************************
342 *                                   Startup                                 *
343 *****************************************************************************
344 *
345 * Tool stack creates front and back nodes with state XenbusStateInitialising.
346 *
347 * Front                                Back
348 * =================================    =====================================
349 * XenbusStateInitialising              XenbusStateInitialising
350 *  o Query virtual device               o Query backend device identification
351 *    properties.                          data.
352 *  o Setup OS device instance.          o Open and validate backend device.
353 *                                       o Publish backend features and
354 *                                         transport parameters.
355 *                                                      |
356 *                                                      |
357 *                                                      V
358 *                                      XenbusStateInitWait
359 *
360 * o Query backend features and
361 *   transport parameters.
362 * o Allocate and initialize the
363 *   request ring.
364 * o Publish transport parameters
365 *   that will be in effect during
366 *   this connection.
367 *              |
368 *              |
369 *              V
370 * XenbusStateInitialised
371 *
372 *                                       o Query frontend transport parameters.
373 *                                       o Connect to the request ring and
374 *                                         event channel.
375 *                                       o Publish backend device properties.
376 *                                                      |
377 *                                                      |
378 *                                                      V
379 *                                      XenbusStateConnected
380 *
381 *  o Query backend device properties.
382 *  o Finalize OS virtual device
383 *    instance.
384 *              |
385 *              |
386 *              V
387 * XenbusStateConnected
388 *
389 * Note: Drivers that do not support any optional features, or the negotiation
390 *       of transport parameters, can skip certain states in the state machine:
391 *
392 *       o A frontend may transition to XenbusStateInitialised without
393 *         waiting for the backend to enter XenbusStateInitWait.  In this
394 *         case, default transport parameters are in effect and any
395 *         transport parameters published by the frontend must contain
396 *         their default values.
397 *
398 *       o A backend may transition to XenbusStateInitialised, bypassing
399 *         XenbusStateInitWait, without waiting for the frontend to first
400 *         enter the XenbusStateInitialised state.  In this case, default
401 *         transport parameters are in effect and any transport parameters
402 *         published by the backend must contain their default values.
403 *
404 *       Drivers that support optional features and/or transport parameter
405 *       negotiation must tolerate these additional state transition paths.
406 *       In general this means performing the work of any skipped state
407 *       transition, if it has not already been performed, in addition to the
408 *       work associated with entry into the current state.
409 */
410
411/*
412 * REQUEST CODES.
413 */
414#define BLKIF_OP_READ              0
415#define BLKIF_OP_WRITE             1
416/*
417 * All writes issued prior to a request with the BLKIF_OP_WRITE_BARRIER
418 * operation code ("barrier request") must be completed prior to the
419 * execution of the barrier request.  All writes issued after the barrier
420 * request must not execute until after the completion of the barrier request.
421 *
422 * Optional.  See "feature-barrier" XenBus node documentation above.
423 */
424#define BLKIF_OP_WRITE_BARRIER     2
425/*
426 * Commit any uncommitted contents of the backing device's volatile cache
427 * to stable storage.
428 *
429 * Optional.  See "feature-flush-cache" XenBus node documentation above.
430 */
431#define BLKIF_OP_FLUSH_DISKCACHE   3
432/*
433 * Used in SLES sources for device specific command packet
434 * contained within the request. Reserved for that purpose.
435 */
436#define BLKIF_OP_RESERVED_1        4
437/*
438 * Indicate to the backend device that a region of storage is no longer in
439 * use, and may be discarded at any time without impact to the client.  If
440 * the BLKIF_DISCARD_SECURE flag is set on the request, all copies of the
441 * discarded region on the device must be rendered unrecoverable before the
442 * command returns.
443 *
444 * This operation is analogous to performing a trim (ATA) or unmap (SCSI),
445 * command on a native device.
446 *
447 * More information about trim/unmap operations can be found at:
448 * http://t13.org/Documents/UploadedDocuments/docs2008/
449 *     e07154r6-Data_Set_Management_Proposal_for_ATA-ACS2.doc
450 * http://www.seagate.com/staticfiles/support/disc/manuals/
451 *     Interface%20manuals/100293068c.pdf
452 *
453 * Optional.  See "feature-discard", "discard-alignment",
454 * "discard-granularity", and "discard-secure" in the XenBus node
455 * documentation above.
456 */
457#define BLKIF_OP_DISCARD           5
458
459/*
460 * Maximum scatter/gather segments per request (header + segment blocks).
461 */
462#define BLKIF_MAX_SEGMENTS_PER_REQUEST 11
463
464/*
465 * NB. first_sect and last_sect in blkif_request_segment, as well as
466 * sector_number in blkif_request, are always expressed in 512-byte units.
467 * However they must be properly aligned to the real sector size of the
468 * physical disk, which is reported in the "sector-size" node in the backend
469 * xenbus info. Also the xenbus "sectors" node is expressed in 512-byte units.
470 */
471struct blkif_request_segment {
472    grant_ref_t gref;        /* reference to I/O buffer frame        */
473    /* @first_sect: first sector in frame to transfer (inclusive).   */
474    /* @last_sect: last sector in frame to transfer (inclusive).     */
475    uint8_t     first_sect, last_sect;
476};
477typedef struct blkif_request_segment blkif_request_segment_t;
478
479/*
480 * Starting ring element for any I/O request.
481 *
482 * One or more segment blocks can be inserted into the request ring
483 * just after a blkif_request_t, allowing requests to operate on
484 * up to BLKIF_MAX_SEGMENTS_PER_REQUEST.
485 *
486 * BLKIF_SEGS_TO_BLOCKS() can be used on blkif_requst.nr_segments
487 * to determine the number of contiguous ring entries associated
488 * with this request.
489 *
490 * Note:  Due to the way Xen request rings operate, the producer and
491 *        consumer indices of the ring must be incremented by the
492 *        BLKIF_SEGS_TO_BLOCKS() value of the associated request.
493 *        (e.g. a response to a 3 ring entry request must also consume
494 *        3 entries in the ring, even though only the first ring entry
495 *        in the response has any data.)
496 */
497struct blkif_request {
498    uint8_t        operation;    /* BLKIF_OP_???                         */
499    uint8_t        nr_segments;  /* number of segments                   */
500    blkif_vdev_t   handle;       /* only for read/write requests         */
501    uint64_t       id;           /* private guest value, echoed in resp  */
502    blkif_sector_t sector_number;/* start sector idx on disk (r/w only)  */
503    blkif_request_segment_t seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
504};
505typedef struct blkif_request blkif_request_t;
506
507/*
508 * Cast to this structure when blkif_request.operation == BLKIF_OP_DISCARD
509 * sizeof(struct blkif_request_discard) <= sizeof(struct blkif_request)
510 */
511struct blkif_request_discard {
512    uint8_t        operation;    /* BLKIF_OP_DISCARD                     */
513    uint8_t        flag;         /* BLKIF_DISCARD_SECURE or zero         */
514#define BLKIF_DISCARD_SECURE (1<<0)  /* ignored if discard-secure=0      */
515    blkif_vdev_t   handle;       /* same as for read/write requests      */
516    uint64_t       id;           /* private guest value, echoed in resp  */
517    blkif_sector_t sector_number;/* start sector idx on disk             */
518    uint64_t       nr_sectors;   /* number of contiguous sectors to discard*/
519};
520typedef struct blkif_request_discard blkif_request_discard_t;
521
522struct blkif_response {
523    uint64_t        id;              /* copied from request */
524    uint8_t         operation;       /* copied from request */
525    int16_t         status;          /* BLKIF_RSP_???       */
526};
527typedef struct blkif_response blkif_response_t;
528
529/*
530 * STATUS RETURN CODES.
531 */
532 /* Operation not supported (only happens on barrier writes). */
533#define BLKIF_RSP_EOPNOTSUPP  -2
534 /* Operation failed for some unspecified reason (-EIO). */
535#define BLKIF_RSP_ERROR       -1
536 /* Operation completed successfully. */
537#define BLKIF_RSP_OKAY         0
538
539/*
540 * Generate blkif ring structures and types.
541 */
542DEFINE_RING_TYPES(blkif, struct blkif_request, struct blkif_response);
543
544#define VDISK_CDROM        0x1
545#define VDISK_REMOVABLE    0x2
546#define VDISK_READONLY     0x4
547
548#endif /* __XEN_PUBLIC_IO_BLKIF_H__ */
549
550/*
551 * Local variables:
552 * mode: C
553 * c-set-style: "BSD"
554 * c-basic-offset: 4
555 * tab-width: 4
556 * indent-tabs-mode: nil
557 * End:
558 */
559