Update to 3.4-final.
[linux-flexiantxendom0-3.2.10.git] / include / xen / interface / io / blkif.h
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 formated 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  *      A free formatted string providing sufficient information for the
92  *      backend driver to open the backing device.  (e.g. the path to the
93  *      file or block device representing the backing store.)
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-aligment
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 native sector size, in bytes, of the backend device.
184  *
185  * sectors
186  *      Values:         <uint64_t>
187  *
188  *      The size of the backend device, expressed in units of its native
189  *      sector size ("sector-size").
190  *
191  *****************************************************************************
192  *                            Frontend XenBus Nodes
193  *****************************************************************************
194  *
195  *----------------------- Request Transport Parameters -----------------------
196  *
197  * event-channel
198  *      Values:         <uint32_t>
199  *
200  *      The identifier of the Xen event channel used to signal activity
201  *      in the ring buffer.
202  *
203  * ring-ref
204  *      Values:         <uint32_t>
205  *      Notes:          6
206  *
207  *      The Xen grant reference granting permission for the backend to map
208  *      the sole page in a single page sized ring buffer.
209  *
210  * ring-ref%u
211  *      Values:         <uint32_t>
212  *      Notes:          6
213  *
214  *      For a frontend providing a multi-page ring, a "number of ring pages"
215  *      sized list of nodes, each containing a Xen grant reference granting
216  *      permission for the backend to map the page of the ring located
217  *      at page index "%u".  Page indexes are zero based.
218  *
219  * protocol
220  *      Values:         string (XEN_IO_PROTO_ABI_*)
221  *      Default Value:  XEN_IO_PROTO_ABI_NATIVE
222  *
223  *      The machine ABI rules governing the format of all ring request and
224  *      response structures.
225  *
226  * ring-page-order
227  *      Values:         <uint32_t>
228  *      Default Value:  0
229  *      Maximum Value:  MAX(ffs(max-ring-pages) - 1, max-ring-page-order)
230  *      Notes:          1, 3
231  *
232  *      The size of the frontend allocated request ring buffer in units
233  *      of lb(machine pages). (e.g. 0 == 1 page, 1 = 2 pages, 2 == 4 pages,
234  *      etc.).
235  *
236  * num-ring-pages
237  *      Values:         <uint32_t>
238  *      Default Value:  1
239  *      Maximum Value:  MAX(max-ring-pages,(0x1 << max-ring-page-order))
240  *      Notes:          DEPRECATED, 2, 3
241  *
242  *      The size of the frontend allocated request ring buffer in units of
243  *      machine pages.  The value must be a power of 2.
244  *
245  *------------------------- Virtual Device Properties -------------------------
246  *
247  * device-type
248  *      Values:         "disk", "cdrom", "floppy", etc.
249  *
250  * virtual-device
251  *      Values:         <uint32_t>
252  *
253  *      A value indicating the physical device to virtualize within the
254  *      frontend's domain.  (e.g. "The first ATA disk", "The third SCSI
255  *      disk", etc.)
256  *
257  *      See docs/misc/vbd-interface.txt for details on the format of this
258  *      value.
259  *
260  * Notes
261  * -----
262  * (1) Multi-page ring buffer scheme first developed in the Citrix XenServer
263  *     PV drivers.
264  * (2) Multi-page ring buffer scheme first used in some RedHat distributions
265  *     including a distribution deployed on certain nodes of the Amazon
266  *     EC2 cluster.
267  * (3) Support for multi-page ring buffers was implemented independently,
268  *     in slightly different forms, by both Citrix and RedHat/Amazon.
269  *     For full interoperability, block front and backends should publish
270  *     identical ring parameters, adjusted for unit differences, to the
271  *     XenStore nodes used in both schemes.
272  * (4) Devices that support discard functionality may internally allocate
273  *     space (discardable extents) in units that are larger than the
274  *     exported logical block size.
275  * (5) The discard-alignment parameter allows a physical device to be
276  *     partitioned into virtual devices that do not necessarily begin or
277  *     end on a discardable extent boundary.
278  * (6) When there is only a single page allocated to the request ring,
279  *     'ring-ref' is used to communicate the grant reference for this
280  *     page to the backend.  When using a multi-page ring, the 'ring-ref'
281  *     node is not created.  Instead 'ring-ref0' - 'ring-refN' are used.
282  */
283
284 /*
285  * STATE DIAGRAMS
286  *
287  *****************************************************************************
288  *                                   Startup                                 *
289  *****************************************************************************
290  *
291  * Tool stack creates front and back nodes with state XenbusStateInitialising.
292  *
293  * Front                                Back
294  * =================================    =====================================
295  * XenbusStateInitialising              XenbusStateInitialising
296  *  o Query virtual device               o Query backend device identification
297  *    properties.                          data.
298  *  o Setup OS device instance.          o Open and validate backend device.
299  *                                       o Publish backend features and
300  *                                         transport parameters.
301  *                                                      |
302  *                                                      |
303  *                                                      V
304  *                                      XenbusStateInitWait
305  *
306  * o Query backend features and
307  *   transport parameters.
308  * o Allocate and initialize the
309  *   request ring.
310  * o Publish transport parameters
311  *   that will be in effect during
312  *   this connection.
313  *              |
314  *              |
315  *              V
316  * XenbusStateInitialised
317  *
318  *                                       o Query frontend transport parameters.
319  *                                       o Connect to the request ring and
320  *                                         event channel.
321  *                                       o Publish backend device properties.
322  *                                                      |
323  *                                                      |
324  *                                                      V
325  *                                      XenbusStateConnected
326  *
327  *  o Query backend device properties.
328  *  o Finalize OS virtual device
329  *    instance.
330  *              |
331  *              |
332  *              V
333  * XenbusStateConnected
334  *
335  * Note: Drivers that do not support any optional features, or the negotiation
336  *       of transport parameters, can skip certain states in the state machine:
337  *
338  *       o A frontend may transition to XenbusStateInitialised without
339  *         waiting for the backend to enter XenbusStateInitWait.  In this
340  *         case, default transport parameters are in effect and any
341  *         transport parameters published by the frontend must contain
342  *         their default values.
343  *
344  *       o A backend may transition to XenbusStateInitialised, bypassing
345  *         XenbusStateInitWait, without waiting for the frontend to first
346  *         enter the XenbusStateInitialised state.  In this case, default
347  *         transport parameters are in effect and any transport parameters
348  *         published by the backend must contain their default values.
349  *
350  *       Drivers that support optional features and/or transport parameter
351  *       negotiation must tolerate these additional state transition paths.
352  *       In general this means performing the work of any skipped state
353  *       transition, if it has not already been performed, in addition to the
354  *       work associated with entry into the current state.
355  */
356
357 /*
358  * REQUEST CODES.
359  */
360 #define BLKIF_OP_READ              0
361 #define BLKIF_OP_WRITE             1
362 /*
363  * All writes issued prior to a request with the BLKIF_OP_WRITE_BARRIER
364  * operation code ("barrier request") must be completed prior to the
365  * execution of the barrier request.  All writes issued after the barrier
366  * request must not execute until after the completion of the barrier request.
367  *
368  * Optional.  See "feature-barrier" XenBus node documentation above.
369  */
370 #define BLKIF_OP_WRITE_BARRIER     2
371 /*
372  * Commit any uncommitted contents of the backing device's volatile cache
373  * to stable storage.
374  *
375  * Optional.  See "feature-flush-cache" XenBus node documentation above.
376  */
377 #define BLKIF_OP_FLUSH_DISKCACHE   3
378 /*
379  * Device specific command packet contained within the request
380  */
381 #define BLKIF_OP_PACKET            4
382 /*
383  * Indicate to the backend device that a region of storage is no longer in
384  * use, and may be discarded at any time without impact to the client.  If
385  * the BLKIF_DISCARD_SECURE flag is set on the request, all copies of the
386  * discarded region on the device must be rendered unrecoverable before the
387  * command returns.
388  *
389  * This operation is analogous to performing a trim (ATA) or unamp (SCSI),
390  * command on a native device.
391  *
392  * More information about trim/unmap operations can be found at:
393  * http://t13.org/Documents/UploadedDocuments/docs2008/
394  *     e07154r6-Data_Set_Management_Proposal_for_ATA-ACS2.doc
395  * http://www.seagate.com/staticfiles/support/disc/manuals/
396  *     Interface%20manuals/100293068c.pdf
397  *
398  * Optional.  See "feature-discard", "discard-alignment",
399  * "discard-granularity", and "discard-secure" in the XenBus node
400  * documentation above.
401  */
402 #define BLKIF_OP_DISCARD           5
403
404 /*
405  * Maximum scatter/gather segments per request.
406  * This is carefully chosen so that sizeof(struct blkif_ring) <= PAGE_SIZE.
407  * NB. This could be 12 if the ring indexes weren't stored in the same page.
408  */
409 #define BLKIF_MAX_SEGMENTS_PER_REQUEST 11
410
411 /*
412  * NB. first_sect and last_sect in blkif_request_segment, as well as
413  * sector_number in blkif_request, are always expressed in 512-byte units.
414  * However they must be properly aligned to the real sector size of the
415  * physical disk, which is reported in the "sector-size" node in the backend
416  * xenbus info. Also the xenbus "sectors" node is expressed in 512-byte units.
417  */
418 struct blkif_request_segment {
419     grant_ref_t gref;        /* reference to I/O buffer frame        */
420     /* @first_sect: first sector in frame to transfer (inclusive).   */
421     /* @last_sect: last sector in frame to transfer (inclusive).     */
422     uint8_t     first_sect, last_sect;
423 };
424
425 /*
426  * Starting ring element for any I/O request.
427  */
428 struct blkif_request {
429     uint8_t        operation;    /* BLKIF_OP_???                         */
430 #if !defined(CONFIG_PARAVIRT_XEN) || defined(HAVE_XEN_PLATFORM_COMPAT_H)
431     uint8_t        nr_segments;  /* number of segments                   */
432     blkif_vdev_t   handle;       /* only for read/write requests         */
433     uint64_t       id;           /* private guest value, echoed in resp  */
434     blkif_sector_t sector_number;/* start sector idx on disk (r/w only)  */
435     struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
436 };
437 #else
438     union {
439         struct __attribute__((__packed__)) blkif_request_rw {
440             uint8_t        nr_segments;  /* number of segments                  */
441             blkif_vdev_t   handle;       /* only for read/write requests        */
442 #ifdef CONFIG_X86_64
443             uint32_t       _pad1;        /* offsetof(blkif_request,u.rw.id) == 8 */
444 #endif
445             uint64_t       id;           /* private guest value, echoed in resp */
446             blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */
447             struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
448         } rw;
449         struct __attribute__((__packed__)) blkif_request_discard {
450             uint8_t        flag;         /* BLKIF_DISCARD_SECURE or zero.        */
451 #define BLKIF_DISCARD_SECURE (1<<0)      /* ignored if discard-secure=0          */
452             blkif_vdev_t   _pad1;        /* only for read/write requests         */
453 #ifdef CONFIG_X86_64
454             uint32_t       _pad2;        /* offsetof(blkif_req..,u.discard.id)==8*/
455 #endif
456             uint64_t       id;           /* private guest value, echoed in resp  */
457             blkif_sector_t sector_number;
458             uint64_t       nr_sectors;
459             uint8_t        _pad3;
460         } discard;
461     } u;
462 } __attribute__((__packed__));
463 #endif
464 typedef struct blkif_request blkif_request_t;
465
466 #if !defined(CONFIG_PARAVIRT_XEN) || defined(HAVE_XEN_PLATFORM_COMPAT_H)
467 /*
468  * Cast to this structure when blkif_request.operation == BLKIF_OP_DISCARD
469  * sizeof(struct blkif_request_discard) <= sizeof(struct blkif_request)
470  */
471 struct blkif_request_discard {
472     uint8_t        operation;    /* BLKIF_OP_DISCARD                     */
473     uint8_t        flag;         /* BLKIF_DISCARD_SECURE or zero         */
474 #define BLKIF_DISCARD_SECURE (1<<0)  /* ignored if discard-secure=0      */
475     blkif_vdev_t   handle;       /* same as for read/write requests      */
476     uint64_t       id;           /* private guest value, echoed in resp  */
477     blkif_sector_t sector_number;/* start sector idx on disk             */
478     uint64_t       nr_sectors;   /* number of contiguous sectors to discard*/
479 };
480 typedef struct blkif_request_discard blkif_request_discard_t;
481 #endif
482
483 struct blkif_response {
484     uint64_t        id;              /* copied from request */
485     uint8_t         operation;       /* copied from request */
486     int16_t         status;          /* BLKIF_RSP_???       */
487 };
488 typedef struct blkif_response blkif_response_t;
489
490 /*
491  * STATUS RETURN CODES.
492  */
493  /* Operation not supported (only happens on barrier writes). */
494 #define BLKIF_RSP_EOPNOTSUPP  -2
495  /* Operation failed for some unspecified reason (-EIO). */
496 #define BLKIF_RSP_ERROR       -1
497  /* Operation completed successfully. */
498 #define BLKIF_RSP_OKAY         0
499
500 /*
501  * Generate blkif ring structures and types.
502  */
503 DEFINE_RING_TYPES(blkif, struct blkif_request, struct blkif_response);
504
505 #define VDISK_CDROM        0x1
506 #define VDISK_REMOVABLE    0x2
507 #define VDISK_READONLY     0x4
508
509 /* Xen-defined major numbers for virtual disks, they look strangely
510  * familiar */
511 #define XEN_IDE0_MAJOR  3
512 #define XEN_IDE1_MAJOR  22
513 #define XEN_SCSI_DISK0_MAJOR    8
514 #define XEN_SCSI_DISK1_MAJOR    65
515 #define XEN_SCSI_DISK2_MAJOR    66
516 #define XEN_SCSI_DISK3_MAJOR    67
517 #define XEN_SCSI_DISK4_MAJOR    68
518 #define XEN_SCSI_DISK5_MAJOR    69
519 #define XEN_SCSI_DISK6_MAJOR    70
520 #define XEN_SCSI_DISK7_MAJOR    71
521 #define XEN_SCSI_DISK8_MAJOR    128
522 #define XEN_SCSI_DISK9_MAJOR    129
523 #define XEN_SCSI_DISK10_MAJOR   130
524 #define XEN_SCSI_DISK11_MAJOR   131
525 #define XEN_SCSI_DISK12_MAJOR   132
526 #define XEN_SCSI_DISK13_MAJOR   133
527 #define XEN_SCSI_DISK14_MAJOR   134
528 #define XEN_SCSI_DISK15_MAJOR   135
529
530 #endif /* __XEN_PUBLIC_IO_BLKIF_H__ */