1 /******************************************************************************
2  * blkif.h
3  *
4  * Unified block-device I/O interface for Xen guest OSes.
5  *
6  * SPDX-License-Identifier: MIT
7  *
8  * Copyright (c) 2003-2004, Keir Fraser
9  * Copyright (c) 2012, Spectra Logic Corporation
10  */
11 
12 #ifndef __XEN_PUBLIC_IO_BLKIF_H__
13 #define __XEN_PUBLIC_IO_BLKIF_H__
14 
15 #include "ring.h"
16 #include "../grant_table.h"
17 
18 /*
19  * Front->back notifications: When enqueuing a new request, sending a
20  * notification can be made conditional on req_event (i.e., the generic
21  * hold-off mechanism provided by the ring macros). Backends must set
22  * req_event appropriately (e.g., using RING_FINAL_CHECK_FOR_REQUESTS()).
23  *
24  * Back->front notifications: When enqueuing a new response, sending a
25  * notification can be made conditional on rsp_event (i.e., the generic
26  * hold-off mechanism provided by the ring macros). Frontends must set
27  * rsp_event appropriately (e.g., using RING_FINAL_CHECK_FOR_RESPONSES()).
28  */
29 
30 #ifndef blkif_vdev_t
31 #define blkif_vdev_t   UINT16
32 #endif
33 #define blkif_sector_t UINT64
34 
35 /*
36  * Feature and Parameter Negotiation
37  * =================================
38  * The two halves of a Xen block driver utilize nodes within the XenStore to
39  * communicate capabilities and to negotiate operating parameters.  This
40  * section enumerates these nodes which reside in the respective front and
41  * backend portions of the XenStore, following the XenBus convention.
42  *
43  * All data in the XenStore is stored as strings.  Nodes specifying numeric
44  * values are encoded in decimal.  Integer value ranges listed below are
45  * expressed as fixed sized integer types capable of storing the conversion
46  * of a properly formatted node string, without loss of information.
47  *
48  * Any specified default value is in effect if the corresponding XenBus node
49  * is not present in the XenStore.
50  *
51  * XenStore nodes in sections marked "PRIVATE" are solely for use by the
52  * driver side whose XenBus tree contains them.
53  *
54  * XenStore nodes marked "DEPRECATED" in their notes section should only be
55  * used to provide interoperability with legacy implementations.
56  *
57  * See the XenBus state transition diagram below for details on when XenBus
58  * nodes must be published and when they can be queried.
59  *
60  *****************************************************************************
61  *                            Backend XenBus Nodes
62  *****************************************************************************
63  *
64  *------------------ Backend Device Identification (PRIVATE) ------------------
65  *
66  * mode
67  *      Values:         "r" (read only), "w" (writable)
68  *
69  *      The read or write access permissions to the backing store to be
70  *      granted to the frontend.
71  *
72  * params
73  *      Values:         string
74  *
75  *      A free formatted string providing sufficient information for the
76  *      backend driver to open the backing device.  (e.g. the path to the
77  *      file or block device representing the backing store.)
78  *
79  * type
80  *      Values:         "file", "phy", "tap"
81  *
82  *      The type of the backing device/object.
83  *
84  *--------------------------------- Features ---------------------------------
85  *
86  * feature-barrier
87  *      Values:         0/1 (boolean)
88  *      Default Value:  0
89  *
90  *      A value of "1" indicates that the backend can process requests
91  *      containing the BLKIF_OP_WRITE_BARRIER request opcode.  Requests
92  *      of this type may still be returned at any time with the
93  *      BLKIF_RSP_EOPNOTSUPP result code.
94  *
95  * feature-flush-cache
96  *      Values:         0/1 (boolean)
97  *      Default Value:  0
98  *
99  *      A value of "1" indicates that the backend can process requests
100  *      containing the BLKIF_OP_FLUSH_DISKCACHE request opcode.  Requests
101  *      of this type may still be returned at any time with the
102  *      BLKIF_RSP_EOPNOTSUPP result code.
103  *
104  * feature-discard
105  *      Values:         0/1 (boolean)
106  *      Default Value:  0
107  *
108  *      A value of "1" indicates that the backend can process requests
109  *      containing the BLKIF_OP_DISCARD request opcode.  Requests
110  *      of this type may still be returned at any time with the
111  *      BLKIF_RSP_EOPNOTSUPP result code.
112  *
113  * feature-persistent
114  *      Values:         0/1 (boolean)
115  *      Default Value:  0
116  *      Notes: 7
117  *
118  *      A value of "1" indicates that the backend can keep the grants used
119  *      by the frontend driver mapped, so the same set of grants should be
120  *      used in all transactions. The maximum number of grants the backend
121  *      can map persistently depends on the implementation, but ideally it
122  *      should be RING_SIZE * BLKIF_MAX_SEGMENTS_PER_REQUEST. Using this
123  *      feature the backend doesn't need to unmap each grant, preventing
124  *      costly TLB flushes. The backend driver should only map grants
125  *      persistently if the frontend supports it. If a backend driver chooses
126  *      to use the persistent protocol when the frontend doesn't support it,
127  *      it will probably hit the maximum number of persistently mapped grants
128  *      (due to the fact that the frontend won't be reusing the same grants),
129  *      and fall back to non-persistent mode. Backend implementations may
130  *      shrink or expand the number of persistently mapped grants without
131  *      notifying the frontend depending on memory constraints (this might
132  *      cause a performance degradation).
133  *
134  *      If a backend driver wants to limit the maximum number of persistently
135  *      mapped grants to a value less than RING_SIZE *
136  *      BLKIF_MAX_SEGMENTS_PER_REQUEST a LRU strategy should be used to
137  *      discard the grants that are less commonly used. Using a LRU in the
138  *      backend driver paired with a LIFO queue in the frontend will
139  *      allow us to have better performance in this scenario.
140  *
141  *----------------------- Request Transport Parameters ------------------------
142  *
143  * max-ring-page-order
144  *      Values:         <UINT32>
145  *      Default Value:  0
146  *      Notes:          1, 3
147  *
148  *      The maximum supported size of the request ring buffer in units of
149  *      lb(machine pages). (e.g. 0 == 1 page,  1 = 2 pages, 2 == 4 pages,
150  *      etc.).
151  *
152  * max-ring-pages
153  *      Values:         <UINT32>
154  *      Default Value:  1
155  *      Notes:          DEPRECATED, 2, 3
156  *
157  *      The maximum supported size of the request ring buffer in units of
158  *      machine pages.  The value must be a power of 2.
159  *
160  *------------------------- Backend Device Properties -------------------------
161  *
162  * discard-alignment
163  *      Values:         <UINT32>
164  *      Default Value:  0
165  *      Notes:          4, 5
166  *
167  *      The offset, in bytes from the beginning of the virtual block device,
168  *      to the first, addressable, discard extent on the underlying device.
169  *
170  * discard-granularity
171  *      Values:         <UINT32>
172  *      Default Value:  <"sector-size">
173  *      Notes:          4
174  *
175  *      The size, in bytes, of the individually addressable discard extents
176  *      of the underlying device.
177  *
178  * discard-secure
179  *      Values:         0/1 (boolean)
180  *      Default Value:  0
181  *      Notes:          10
182  *
183  *      A value of "1" indicates that the backend can process BLKIF_OP_DISCARD
184  *      requests with the BLKIF_DISCARD_SECURE flag set.
185  *
186  * info
187  *      Values:         <UINT32> (bitmap)
188  *
189  *      A collection of bit flags describing attributes of the backing
190  *      device.  The VDISK_* macros define the meaning of each bit
191  *      location.
192  *
193  * sector-size
194  *      Values:         <UINT32>
195  *
196  *      The logical sector size, in bytes, of the backend device.
197  *
198  * physical-sector-size
199  *      Values:         <UINT32>
200  *
201  *      The physical sector size, in bytes, of the backend device.
202  *
203  * sectors
204  *      Values:         <UINT64>
205  *
206  *      The size of the backend device, expressed in units of its logical
207  *      sector size ("sector-size").
208  *
209  *****************************************************************************
210  *                            Frontend XenBus Nodes
211  *****************************************************************************
212  *
213  *----------------------- Request Transport Parameters -----------------------
214  *
215  * event-channel
216  *      Values:         <UINT32>
217  *
218  *      The identifier of the Xen event channel used to signal activity
219  *      in the ring buffer.
220  *
221  * ring-ref
222  *      Values:         <UINT32>
223  *      Notes:          6
224  *
225  *      The Xen grant reference granting permission for the backend to map
226  *      the sole page in a single page sized ring buffer.
227  *
228  * ring-ref%u
229  *      Values:         <UINT32>
230  *      Notes:          6
231  *
232  *      For a frontend providing a multi-page ring, a "number of ring pages"
233  *      sized list of nodes, each containing a Xen grant reference granting
234  *      permission for the backend to map the page of the ring located
235  *      at page index "%u".  Page indexes are zero based.
236  *
237  * protocol
238  *      Values:         string (XEN_IO_PROTO_ABI_*)
239  *      Default Value:  XEN_IO_PROTO_ABI_NATIVE
240  *
241  *      The machine ABI rules governing the format of all ring request and
242  *      response structures.
243  *
244  * ring-page-order
245  *      Values:         <UINT32>
246  *      Default Value:  0
247  *      Maximum Value:  MAX(ffs(max-ring-pages) - 1, max-ring-page-order)
248  *      Notes:          1, 3
249  *
250  *      The size of the frontend allocated request ring buffer in units
251  *      of lb(machine pages). (e.g. 0 == 1 page, 1 = 2 pages, 2 == 4 pages,
252  *      etc.).
253  *
254  * num-ring-pages
255  *      Values:         <UINT32>
256  *      Default Value:  1
257  *      Maximum Value:  MAX(max-ring-pages,(0x1 << max-ring-page-order))
258  *      Notes:          DEPRECATED, 2, 3
259  *
260  *      The size of the frontend allocated request ring buffer in units of
261  *      machine pages.  The value must be a power of 2.
262  *
263  * feature-persistent
264  *      Values:         0/1 (boolean)
265  *      Default Value:  0
266  *      Notes: 7, 8, 9
267  *
268  *      A value of "1" indicates that the frontend will reuse the same grants
269  *      for all transactions, allowing the backend to map them with write
270  *      access (even when it should be read-only). If the frontend hits the
271  *      maximum number of allowed persistently mapped grants, it can fallback
272  *      to non persistent mode. This will cause a performance degradation,
273  *      since the the backend driver will still try to map those grants
274  *      persistently. Since the persistent grants protocol is compatible with
275  *      the previous protocol, a frontend driver can choose to work in
276  *      persistent mode even when the backend doesn't support it.
277  *
278  *      It is recommended that the frontend driver stores the persistently
279  *      mapped grants in a LIFO queue, so a subset of all persistently mapped
280  *      grants gets used commonly. This is done in case the backend driver
281  *      decides to limit the maximum number of persistently mapped grants
282  *      to a value less than RING_SIZE * BLKIF_MAX_SEGMENTS_PER_REQUEST.
283  *
284  *------------------------- Virtual Device Properties -------------------------
285  *
286  * device-type
287  *      Values:         "disk", "cdrom", "floppy", etc.
288  *
289  * virtual-device
290  *      Values:         <UINT32>
291  *
292  *      A value indicating the physical device to virtualize within the
293  *      frontend's domain.  (e.g. "The first ATA disk", "The third SCSI
294  *      disk", etc.)
295  *
296  *      See docs/misc/vbd-interface.txt for details on the format of this
297  *      value.
298  *
299  * Notes
300  * -----
301  * (1) Multi-page ring buffer scheme first developed in the Citrix XenServer
302  *     PV drivers.
303  * (2) Multi-page ring buffer scheme first used in some RedHat distributions
304  *     including a distribution deployed on certain nodes of the Amazon
305  *     EC2 cluster.
306  * (3) Support for multi-page ring buffers was implemented independently,
307  *     in slightly different forms, by both Citrix and RedHat/Amazon.
308  *     For full interoperability, block front and backends should publish
309  *     identical ring parameters, adjusted for unit differences, to the
310  *     XenStore nodes used in both schemes.
311  * (4) Devices that support discard functionality may internally allocate space
312  *     (discardable extents) in units that are larger than the exported logical
313  *     block size. If the backing device has such discardable extents the
314  *     backend should provide both discard-granularity and discard-alignment.
315  *     Providing just one of the two may be considered an error by the frontend.
316  *     Backends supporting discard should include discard-granularity and
317  *     discard-alignment even if it supports discarding individual sectors.
318  *     Frontends should assume discard-alignment == 0 and discard-granularity
319  *     == sector size if these keys are missing.
320  * (5) The discard-alignment parameter allows a physical device to be
321  *     partitioned into virtual devices that do not necessarily begin or
322  *     end on a discardable extent boundary.
323  * (6) When there is only a single page allocated to the request ring,
324  *     'ring-ref' is used to communicate the grant reference for this
325  *     page to the backend.  When using a multi-page ring, the 'ring-ref'
326  *     node is not created.  Instead 'ring-ref0' - 'ring-refN' are used.
327  * (7) When using persistent grants data has to be copied from/to the page
328  *     where the grant is currently mapped. The overhead of doing this copy
329  *     however doesn't suppress the speed improvement of not having to unmap
330  *     the grants.
331  * (8) The frontend driver has to allow the backend driver to map all grants
332  *     with write access, even when they should be mapped read-only, since
333  *     further requests may reuse these grants and require write permissions.
334  * (9) Linux implementation doesn't have a limit on the maximum number of
335  *     grants that can be persistently mapped in the frontend driver, but
336  *     due to the frontent driver implementation it should never be bigger
337  *     than RING_SIZE * BLKIF_MAX_SEGMENTS_PER_REQUEST.
338  *(10) The discard-secure property may be present and will be set to 1 if the
339  *     backing device supports secure discard.
340  */
341 
342 /*
343  * STATE DIAGRAMS
344  *
345  *****************************************************************************
346  *                                   Startup                                 *
347  *****************************************************************************
348  *
349  * Tool stack creates front and back nodes with state XenbusStateInitialising.
350  *
351  * Front                                Back
352  * =================================    =====================================
353  * XenbusStateInitialising              XenbusStateInitialising
354  *  o Query virtual device               o Query backend device identification
355  *    properties.                          data.
356  *  o Setup OS device instance.          o Open and validate backend device.
357  *                                       o Publish backend features and
358  *                                         transport parameters.
359  *                                                      |
360  *                                                      |
361  *                                                      V
362  *                                      XenbusStateInitWait
363  *
364  * o Query backend features and
365  *   transport parameters.
366  * o Allocate and initialize the
367  *   request ring.
368  * o Publish transport parameters
369  *   that will be in effect during
370  *   this connection.
371  *              |
372  *              |
373  *              V
374  * XenbusStateInitialised
375  *
376  *                                       o Query frontend transport parameters.
377  *                                       o Connect to the request ring and
378  *                                         event channel.
379  *                                       o Publish backend device properties.
380  *                                                      |
381  *                                                      |
382  *                                                      V
383  *                                      XenbusStateConnected
384  *
385  *  o Query backend device properties.
386  *  o Finalize OS virtual device
387  *    instance.
388  *              |
389  *              |
390  *              V
391  * XenbusStateConnected
392  *
393  * Note: Drivers that do not support any optional features, or the negotiation
394  *       of transport parameters, can skip certain states in the state machine:
395  *
396  *       o A frontend may transition to XenbusStateInitialised without
397  *         waiting for the backend to enter XenbusStateInitWait.  In this
398  *         case, default transport parameters are in effect and any
399  *         transport parameters published by the frontend must contain
400  *         their default values.
401  *
402  *       o A backend may transition to XenbusStateInitialised, bypassing
403  *         XenbusStateInitWait, without waiting for the frontend to first
404  *         enter the XenbusStateInitialised state.  In this case, default
405  *         transport parameters are in effect and any transport parameters
406  *         published by the backend must contain their default values.
407  *
408  *       Drivers that support optional features and/or transport parameter
409  *       negotiation must tolerate these additional state transition paths.
410  *       In general this means performing the work of any skipped state
411  *       transition, if it has not already been performed, in addition to the
412  *       work associated with entry into the current state.
413  */
414 
415 /*
416  * REQUEST CODES.
417  */
418 #define BLKIF_OP_READ              0
419 #define BLKIF_OP_WRITE             1
420 /*
421  * All writes issued prior to a request with the BLKIF_OP_WRITE_BARRIER
422  * operation code ("barrier request") must be completed prior to the
423  * execution of the barrier request.  All writes issued after the barrier
424  * request must not execute until after the completion of the barrier request.
425  *
426  * Optional.  See "feature-barrier" XenBus node documentation above.
427  */
428 #define BLKIF_OP_WRITE_BARRIER     2
429 /*
430  * Commit any uncommitted contents of the backing device's volatile cache
431  * to stable storage.
432  *
433  * Optional.  See "feature-flush-cache" XenBus node documentation above.
434  */
435 #define BLKIF_OP_FLUSH_DISKCACHE   3
436 /*
437  * Used in SLES sources for device specific command packet
438  * contained within the request. Reserved for that purpose.
439  */
440 #define BLKIF_OP_RESERVED_1        4
441 /*
442  * Indicate to the backend device that a region of storage is no longer in
443  * use, and may be discarded at any time without impact to the client.  If
444  * the BLKIF_DISCARD_SECURE flag is set on the request, all copies of the
445  * discarded region on the device must be rendered unrecoverable before the
446  * command returns.
447  *
448  * This operation is analogous to performing a trim (ATA) or unamp (SCSI),
449  * command on a native device.
450  *
451  * More information about trim/unmap operations can be found at:
452  * http://t13.org/Documents/UploadedDocuments/docs2008/
453  *     e07154r6-Data_Set_Management_Proposal_for_ATA-ACS2.doc
454  * http://www.seagate.com/staticfiles/support/disc/manuals/
455  *     Interface%20manuals/100293068c.pdf
456  *
457  * Optional.  See "feature-discard", "discard-alignment",
458  * "discard-granularity", and "discard-secure" in the XenBus node
459  * documentation above.
460  */
461 #define BLKIF_OP_DISCARD           5
462 
463 /*
464  * Recognized if "feature-max-indirect-segments" in present in the backend
465  * xenbus info. The "feature-max-indirect-segments" node contains the maximum
466  * number of segments allowed by the backend per request. If the node is
467  * present, the frontend might use blkif_request_indirect structs in order to
468  * issue requests with more than BLKIF_MAX_SEGMENTS_PER_REQUEST (11). The
469  * maximum number of indirect segments is fixed by the backend, but the
470  * frontend can issue requests with any number of indirect segments as long as
471  * it's less than the number provided by the backend. The indirect_grefs field
472  * in blkif_request_indirect should be filled by the frontend with the
473  * grant references of the pages that are holding the indirect segments.
474  * These pages are filled with an array of blkif_request_segment that hold the
475  * information about the segments. The number of indirect pages to use is
476  * determined by the number of segments an indirect request contains. Every
477  * indirect page can contain a maximum of
478  * (PAGE_SIZE / sizeof(struct blkif_request_segment)) segments, so to
479  * calculate the number of indirect pages to use we have to do
480  * ceil(indirect_segments / (PAGE_SIZE / sizeof(struct blkif_request_segment))).
481  *
482  * If a backend does not recognize BLKIF_OP_INDIRECT, it should *not*
483  * create the "feature-max-indirect-segments" node!
484  */
485 #define BLKIF_OP_INDIRECT          6
486 
487 /*
488  * Maximum scatter/gather segments per request.
489  * This is carefully chosen so that sizeof(blkif_ring_t) <= PAGE_SIZE.
490  * NB. This could be 12 if the ring indexes weren't stored in the same page.
491  */
492 #define BLKIF_MAX_SEGMENTS_PER_REQUEST 11
493 
494 /*
495  * Maximum number of indirect pages to use per request.
496  */
497 #define BLKIF_MAX_INDIRECT_PAGES_PER_REQUEST 8
498 
499 /*
500  * NB. first_sect and last_sect in blkif_request_segment, as well as
501  * sector_number in blkif_request, are always expressed in 512-byte units.
502  * However they must be properly aligned to the real sector size of the
503  * physical disk, which is reported in the "physical-sector-size" node in
504  * the backend xenbus info. Also the xenbus "sectors" node is expressed in
505  * 512-byte units.
506  */
507 struct blkif_request_segment {
508     grant_ref_t gref;        /* reference to I/O buffer frame        */
509     /* @first_sect: first sector in frame to transfer (inclusive).   */
510     /* @last_sect: last sector in frame to transfer (inclusive).     */
511     UINT8     first_sect, last_sect;
512 };
513 
514 /*
515  * Starting ring element for any I/O request.
516  */
517 #if defined(MDE_CPU_IA32)
518 //
519 // pack(4) is necessary when these structs are compiled for Ia32.
520 // Without it, the struct will have a different alignment than the one
521 // a backend expect for a 32bit guest.
522 //
523 #pragma pack(4)
524 #endif
525 struct blkif_request {
526     UINT8        operation;    /* BLKIF_OP_???                         */
527     UINT8        nr_segments;  /* number of segments                   */
528     blkif_vdev_t   handle;       /* only for read/write requests         */
529     UINT64       id;           /* private guest value, echoed in resp  */
530     blkif_sector_t sector_number;/* start sector idx on disk (r/w only)  */
531     struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
532 };
533 typedef struct blkif_request blkif_request_t;
534 
535 /*
536  * Cast to this structure when blkif_request.operation == BLKIF_OP_DISCARD
537  * sizeof(struct blkif_request_discard) <= sizeof(struct blkif_request)
538  */
539 struct blkif_request_discard {
540     UINT8        operation;    /* BLKIF_OP_DISCARD                     */
541     UINT8        flag;         /* BLKIF_DISCARD_SECURE or zero         */
542 #define BLKIF_DISCARD_SECURE (1<<0)  /* ignored if discard-secure=0      */
543     blkif_vdev_t   handle;       /* same as for read/write requests      */
544     UINT64       id;           /* private guest value, echoed in resp  */
545     blkif_sector_t sector_number;/* start sector idx on disk             */
546     UINT64       nr_sectors;   /* number of contiguous sectors to discard*/
547 };
548 typedef struct blkif_request_discard blkif_request_discard_t;
549 
550 struct blkif_request_indirect {
551     UINT8        operation;    /* BLKIF_OP_INDIRECT                    */
552     UINT8        indirect_op;  /* BLKIF_OP_{READ/WRITE}                */
553     UINT16       nr_segments;  /* number of segments                   */
554     UINT64       id;           /* private guest value, echoed in resp  */
555     blkif_sector_t sector_number;/* start sector idx on disk (r/w only)  */
556     blkif_vdev_t   handle;       /* same as for read/write requests      */
557     grant_ref_t    indirect_grefs[BLKIF_MAX_INDIRECT_PAGES_PER_REQUEST];
558 #ifdef MDE_CPU_IA32
559     UINT64       pad;          /* Make it 64 byte aligned on i386      */
560 #endif
561 };
562 typedef struct blkif_request_indirect blkif_request_indirect_t;
563 
564 struct blkif_response {
565     UINT64        id;              /* copied from request */
566     UINT8         operation;       /* copied from request */
567     INT16         status;          /* BLKIF_RSP_???       */
568 };
569 typedef struct blkif_response blkif_response_t;
570 #if defined(MDE_CPU_IA32)
571 #pragma pack()
572 #endif
573 
574 /*
575  * STATUS RETURN CODES.
576  */
577  /* Operation not supported (only happens on barrier writes). */
578 #define BLKIF_RSP_EOPNOTSUPP  -2
579  /* Operation failed for some unspecified reason (-EIO). */
580 #define BLKIF_RSP_ERROR       -1
581  /* Operation completed successfully. */
582 #define BLKIF_RSP_OKAY         0
583 
584 /*
585  * Generate blkif ring structures and types.
586  */
587 DEFINE_RING_TYPES(blkif, struct blkif_request, struct blkif_response);
588 
589 #define VDISK_CDROM        0x1
590 #define VDISK_REMOVABLE    0x2
591 #define VDISK_READONLY     0x4
592 
593 #endif /* __XEN_PUBLIC_IO_BLKIF_H__ */
594 
595 /*
596  * Local variables:
597  * mode: C
598  * c-file-style: "BSD"
599  * c-basic-offset: 4
600  * tab-width: 4
601  * indent-tabs-mode: nil
602  * End:
603  */
604