1a42089ddSJeremy Fitzhardinge /****************************************************************************** 2a42089ddSJeremy Fitzhardinge * blkif.h 3a42089ddSJeremy Fitzhardinge * 4a42089ddSJeremy Fitzhardinge * Unified block-device I/O interface for Xen guest OSes. 5a42089ddSJeremy Fitzhardinge * 6a42089ddSJeremy Fitzhardinge * Copyright (c) 2003-2004, Keir Fraser 7a42089ddSJeremy Fitzhardinge */ 8a42089ddSJeremy Fitzhardinge 9a42089ddSJeremy Fitzhardinge #ifndef __XEN_PUBLIC_IO_BLKIF_H__ 10a42089ddSJeremy Fitzhardinge #define __XEN_PUBLIC_IO_BLKIF_H__ 11a42089ddSJeremy Fitzhardinge 12a1ce3928SDavid Howells #include <xen/interface/io/ring.h> 13a1ce3928SDavid Howells #include <xen/interface/grant_table.h> 14a42089ddSJeremy Fitzhardinge 15a42089ddSJeremy Fitzhardinge /* 16a42089ddSJeremy Fitzhardinge * Front->back notifications: When enqueuing a new request, sending a 17a42089ddSJeremy Fitzhardinge * notification can be made conditional on req_event (i.e., the generic 18a42089ddSJeremy Fitzhardinge * hold-off mechanism provided by the ring macros). Backends must set 19a42089ddSJeremy Fitzhardinge * req_event appropriately (e.g., using RING_FINAL_CHECK_FOR_REQUESTS()). 20a42089ddSJeremy Fitzhardinge * 21a42089ddSJeremy Fitzhardinge * Back->front notifications: When enqueuing a new response, sending a 22a42089ddSJeremy Fitzhardinge * notification can be made conditional on rsp_event (i.e., the generic 23a42089ddSJeremy Fitzhardinge * hold-off mechanism provided by the ring macros). Frontends must set 24a42089ddSJeremy Fitzhardinge * rsp_event appropriately (e.g., using RING_FINAL_CHECK_FOR_RESPONSES()). 25a42089ddSJeremy Fitzhardinge */ 26a42089ddSJeremy Fitzhardinge 27a42089ddSJeremy Fitzhardinge typedef uint16_t blkif_vdev_t; 28a42089ddSJeremy Fitzhardinge typedef uint64_t blkif_sector_t; 29a42089ddSJeremy Fitzhardinge 30a42089ddSJeremy Fitzhardinge /* 31a42089ddSJeremy Fitzhardinge * REQUEST CODES. 32a42089ddSJeremy Fitzhardinge */ 33a42089ddSJeremy Fitzhardinge #define BLKIF_OP_READ 0 34a42089ddSJeremy Fitzhardinge #define BLKIF_OP_WRITE 1 35a42089ddSJeremy Fitzhardinge /* 36a42089ddSJeremy Fitzhardinge * Recognised only if "feature-barrier" is present in backend xenbus info. 37a42089ddSJeremy Fitzhardinge * The "feature_barrier" node contains a boolean indicating whether barrier 38a42089ddSJeremy Fitzhardinge * requests are likely to succeed or fail. Either way, a barrier request 39a42089ddSJeremy Fitzhardinge * may fail at any time with BLKIF_RSP_EOPNOTSUPP if it is unsupported by 40a42089ddSJeremy Fitzhardinge * the underlying block-device hardware. The boolean simply indicates whether 41a42089ddSJeremy Fitzhardinge * or not it is worthwhile for the frontend to attempt barrier requests. 42a42089ddSJeremy Fitzhardinge * If a backend does not recognise BLKIF_OP_WRITE_BARRIER, it should *not* 43a42089ddSJeremy Fitzhardinge * create the "feature-barrier" node! 44a42089ddSJeremy Fitzhardinge */ 45a42089ddSJeremy Fitzhardinge #define BLKIF_OP_WRITE_BARRIER 2 46a42089ddSJeremy Fitzhardinge 47a42089ddSJeremy Fitzhardinge /* 486dcfb751SKonrad Rzeszutek Wilk * Recognised if "feature-flush-cache" is present in backend xenbus 496dcfb751SKonrad Rzeszutek Wilk * info. A flush will ask the underlying storage hardware to flush its 506dcfb751SKonrad Rzeszutek Wilk * non-volatile caches as appropriate. The "feature-flush-cache" node 516dcfb751SKonrad Rzeszutek Wilk * contains a boolean indicating whether flush requests are likely to 526dcfb751SKonrad Rzeszutek Wilk * succeed or fail. Either way, a flush request may fail at any time 536dcfb751SKonrad Rzeszutek Wilk * with BLKIF_RSP_EOPNOTSUPP if it is unsupported by the underlying 546dcfb751SKonrad Rzeszutek Wilk * block-device hardware. The boolean simply indicates whether or not it 556dcfb751SKonrad Rzeszutek Wilk * is worthwhile for the frontend to attempt flushes. If a backend does 566dcfb751SKonrad Rzeszutek Wilk * not recognise BLKIF_OP_WRITE_FLUSH_CACHE, it should *not* create the 576dcfb751SKonrad Rzeszutek Wilk * "feature-flush-cache" node! 586dcfb751SKonrad Rzeszutek Wilk */ 596dcfb751SKonrad Rzeszutek Wilk #define BLKIF_OP_FLUSH_DISKCACHE 3 6032a8d26cSLi Dongyang 6132a8d26cSLi Dongyang /* 6232a8d26cSLi Dongyang * Recognised only if "feature-discard" is present in backend xenbus info. 6332a8d26cSLi Dongyang * The "feature-discard" node contains a boolean indicating whether trim 6432a8d26cSLi Dongyang * (ATA) or unmap (SCSI) - conviently called discard requests are likely 6532a8d26cSLi Dongyang * to succeed or fail. Either way, a discard request 6632a8d26cSLi Dongyang * may fail at any time with BLKIF_RSP_EOPNOTSUPP if it is unsupported by 6732a8d26cSLi Dongyang * the underlying block-device hardware. The boolean simply indicates whether 6832a8d26cSLi Dongyang * or not it is worthwhile for the frontend to attempt discard requests. 6932a8d26cSLi Dongyang * If a backend does not recognise BLKIF_OP_DISCARD, it should *not* 7032a8d26cSLi Dongyang * create the "feature-discard" node! 7132a8d26cSLi Dongyang * 7232a8d26cSLi Dongyang * Discard operation is a request for the underlying block device to mark 7332a8d26cSLi Dongyang * extents to be erased. However, discard does not guarantee that the blocks 7432a8d26cSLi Dongyang * will be erased from the device - it is just a hint to the device 7532a8d26cSLi Dongyang * controller that these blocks are no longer in use. What the device 7632a8d26cSLi Dongyang * controller does with that information is left to the controller. 7732a8d26cSLi Dongyang * Discard operations are passed with sector_number as the 7832a8d26cSLi Dongyang * sector index to begin discard operations at and nr_sectors as the number of 7932a8d26cSLi Dongyang * sectors to be discarded. The specified sectors should be discarded if the 8032a8d26cSLi Dongyang * underlying block device supports trim (ATA) or unmap (SCSI) operations, 8132a8d26cSLi Dongyang * or a BLKIF_RSP_EOPNOTSUPP should be returned. 8232a8d26cSLi Dongyang * More information about trim/unmap operations at: 8332a8d26cSLi Dongyang * http://t13.org/Documents/UploadedDocuments/docs2008/ 8432a8d26cSLi Dongyang * e07154r6-Data_Set_Management_Proposal_for_ATA-ACS2.doc 8532a8d26cSLi Dongyang * http://www.seagate.com/staticfiles/support/disc/manuals/ 8632a8d26cSLi Dongyang * Interface%20manuals/100293068c.pdf 875ea42986SKonrad Rzeszutek Wilk * The backend can optionally provide three extra XenBus attributes to 885ea42986SKonrad Rzeszutek Wilk * further optimize the discard functionality: 895ea42986SKonrad Rzeszutek Wilk * 'discard-aligment' - Devices that support discard functionality may 905ea42986SKonrad Rzeszutek Wilk * internally allocate space in units that are bigger than the exported 915ea42986SKonrad Rzeszutek Wilk * logical block size. The discard-alignment parameter indicates how many bytes 925ea42986SKonrad Rzeszutek Wilk * the beginning of the partition is offset from the internal allocation unit's 935ea42986SKonrad Rzeszutek Wilk * natural alignment. 945ea42986SKonrad Rzeszutek Wilk * 'discard-granularity' - Devices that support discard functionality may 955ea42986SKonrad Rzeszutek Wilk * internally allocate space using units that are bigger than the logical block 965ea42986SKonrad Rzeszutek Wilk * size. The discard-granularity parameter indicates the size of the internal 975ea42986SKonrad Rzeszutek Wilk * allocation unit in bytes if reported by the device. Otherwise the 985ea42986SKonrad Rzeszutek Wilk * discard-granularity will be set to match the device's physical block size. 995ea42986SKonrad Rzeszutek Wilk * 'discard-secure' - All copies of the discarded sectors (potentially created 1005ea42986SKonrad Rzeszutek Wilk * by garbage collection) must also be erased. To use this feature, the flag 1015ea42986SKonrad Rzeszutek Wilk * BLKIF_DISCARD_SECURE must be set in the blkif_request_trim. 10232a8d26cSLi Dongyang */ 10332a8d26cSLi Dongyang #define BLKIF_OP_DISCARD 5 10432a8d26cSLi Dongyang 1056dcfb751SKonrad Rzeszutek Wilk /* 106402b27f9SRoger Pau Monne * Recognized if "feature-max-indirect-segments" in present in the backend 107402b27f9SRoger Pau Monne * xenbus info. The "feature-max-indirect-segments" node contains the maximum 108402b27f9SRoger Pau Monne * number of segments allowed by the backend per request. If the node is 109402b27f9SRoger Pau Monne * present, the frontend might use blkif_request_indirect structs in order to 110402b27f9SRoger Pau Monne * issue requests with more than BLKIF_MAX_SEGMENTS_PER_REQUEST (11). The 111402b27f9SRoger Pau Monne * maximum number of indirect segments is fixed by the backend, but the 112402b27f9SRoger Pau Monne * frontend can issue requests with any number of indirect segments as long as 113402b27f9SRoger Pau Monne * it's less than the number provided by the backend. The indirect_grefs field 114402b27f9SRoger Pau Monne * in blkif_request_indirect should be filled by the frontend with the 115402b27f9SRoger Pau Monne * grant references of the pages that are holding the indirect segments. 116402b27f9SRoger Pau Monne * This pages are filled with an array of blkif_request_segment_aligned 117402b27f9SRoger Pau Monne * that hold the information about the segments. The number of indirect 118402b27f9SRoger Pau Monne * pages to use is determined by the maximum number of segments 119402b27f9SRoger Pau Monne * a indirect request contains. Every indirect page can contain a maximum 120402b27f9SRoger Pau Monne * of 512 segments (PAGE_SIZE/sizeof(blkif_request_segment_aligned)), 121402b27f9SRoger Pau Monne * so to calculate the number of indirect pages to use we have to do 122402b27f9SRoger Pau Monne * ceil(indirect_segments/512). 123402b27f9SRoger Pau Monne * 124402b27f9SRoger Pau Monne * If a backend does not recognize BLKIF_OP_INDIRECT, it should *not* 125402b27f9SRoger Pau Monne * create the "feature-max-indirect-segments" node! 126402b27f9SRoger Pau Monne */ 127402b27f9SRoger Pau Monne #define BLKIF_OP_INDIRECT 6 128402b27f9SRoger Pau Monne 129402b27f9SRoger Pau Monne /* 130a42089ddSJeremy Fitzhardinge * Maximum scatter/gather segments per request. 131a42089ddSJeremy Fitzhardinge * This is carefully chosen so that sizeof(struct blkif_ring) <= PAGE_SIZE. 132a42089ddSJeremy Fitzhardinge * NB. This could be 12 if the ring indexes weren't stored in the same page. 133a42089ddSJeremy Fitzhardinge */ 134a42089ddSJeremy Fitzhardinge #define BLKIF_MAX_SEGMENTS_PER_REQUEST 11 135a42089ddSJeremy Fitzhardinge 136402b27f9SRoger Pau Monne #define BLKIF_MAX_INDIRECT_PAGES_PER_REQUEST 8 137402b27f9SRoger Pau Monne 138402b27f9SRoger Pau Monne struct blkif_request_segment_aligned { 139402b27f9SRoger Pau Monne grant_ref_t gref; /* reference to I/O buffer frame */ 140402b27f9SRoger Pau Monne /* @first_sect: first sector in frame to transfer (inclusive). */ 141402b27f9SRoger Pau Monne /* @last_sect: last sector in frame to transfer (inclusive). */ 142402b27f9SRoger Pau Monne uint8_t first_sect, last_sect; 143402b27f9SRoger Pau Monne uint16_t _pad; /* padding to make it 8 bytes, so it's cache-aligned */ 144402b27f9SRoger Pau Monne } __attribute__((__packed__)); 145402b27f9SRoger Pau Monne 14651de6952SOwen Smith struct blkif_request_rw { 14797e36834SKonrad Rzeszutek Wilk uint8_t nr_segments; /* number of segments */ 14897e36834SKonrad Rzeszutek Wilk blkif_vdev_t handle; /* only for read/write requests */ 149*380108d8SJulien Grall #ifndef CONFIG_X86_32 15097e36834SKonrad Rzeszutek Wilk uint32_t _pad1; /* offsetof(blkif_request,u.rw.id) == 8 */ 15197e36834SKonrad Rzeszutek Wilk #endif 15297e36834SKonrad Rzeszutek Wilk uint64_t id; /* private guest value, echoed in resp */ 153a42089ddSJeremy Fitzhardinge blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */ 154a42089ddSJeremy Fitzhardinge struct blkif_request_segment { 155a42089ddSJeremy Fitzhardinge grant_ref_t gref; /* reference to I/O buffer frame */ 156a42089ddSJeremy Fitzhardinge /* @first_sect: first sector in frame to transfer (inclusive). */ 157a42089ddSJeremy Fitzhardinge /* @last_sect: last sector in frame to transfer (inclusive). */ 158a42089ddSJeremy Fitzhardinge uint8_t first_sect, last_sect; 159a42089ddSJeremy Fitzhardinge } seg[BLKIF_MAX_SEGMENTS_PER_REQUEST]; 16097e36834SKonrad Rzeszutek Wilk } __attribute__((__packed__)); 161a42089ddSJeremy Fitzhardinge 16232a8d26cSLi Dongyang struct blkif_request_discard { 1635ea42986SKonrad Rzeszutek Wilk uint8_t flag; /* BLKIF_DISCARD_SECURE or zero. */ 1645ea42986SKonrad Rzeszutek Wilk #define BLKIF_DISCARD_SECURE (1<<0) /* ignored if discard-secure=0 */ 16597e36834SKonrad Rzeszutek Wilk blkif_vdev_t _pad1; /* only for read/write requests */ 166*380108d8SJulien Grall #ifndef CONFIG_X86_32 16797e36834SKonrad Rzeszutek Wilk uint32_t _pad2; /* offsetof(blkif_req..,u.discard.id)==8*/ 16897e36834SKonrad Rzeszutek Wilk #endif 16997e36834SKonrad Rzeszutek Wilk uint64_t id; /* private guest value, echoed in resp */ 17032a8d26cSLi Dongyang blkif_sector_t sector_number; 17132a8d26cSLi Dongyang uint64_t nr_sectors; 17297e36834SKonrad Rzeszutek Wilk uint8_t _pad3; 17397e36834SKonrad Rzeszutek Wilk } __attribute__((__packed__)); 17432a8d26cSLi Dongyang 1750e367ae4SDavid Vrabel struct blkif_request_other { 1760e367ae4SDavid Vrabel uint8_t _pad1; 1770e367ae4SDavid Vrabel blkif_vdev_t _pad2; /* only for read/write requests */ 178*380108d8SJulien Grall #ifndef CONFIG_X86_32 1790e367ae4SDavid Vrabel uint32_t _pad3; /* offsetof(blkif_req..,u.other.id)==8*/ 1800e367ae4SDavid Vrabel #endif 1810e367ae4SDavid Vrabel uint64_t id; /* private guest value, echoed in resp */ 1820e367ae4SDavid Vrabel } __attribute__((__packed__)); 1830e367ae4SDavid Vrabel 184402b27f9SRoger Pau Monne struct blkif_request_indirect { 185402b27f9SRoger Pau Monne uint8_t indirect_op; 186402b27f9SRoger Pau Monne uint16_t nr_segments; 187*380108d8SJulien Grall #ifndef CONFIG_X86_32 188402b27f9SRoger Pau Monne uint32_t _pad1; /* offsetof(blkif_...,u.indirect.id) == 8 */ 189402b27f9SRoger Pau Monne #endif 190402b27f9SRoger Pau Monne uint64_t id; 191402b27f9SRoger Pau Monne blkif_sector_t sector_number; 192402b27f9SRoger Pau Monne blkif_vdev_t handle; 193402b27f9SRoger Pau Monne uint16_t _pad2; 194402b27f9SRoger Pau Monne grant_ref_t indirect_grefs[BLKIF_MAX_INDIRECT_PAGES_PER_REQUEST]; 195*380108d8SJulien Grall #ifndef CONFIG_X86_32 196402b27f9SRoger Pau Monne uint32_t _pad3; /* make it 64 byte aligned */ 197402b27f9SRoger Pau Monne #else 198402b27f9SRoger Pau Monne uint64_t _pad3; /* make it 64 byte aligned */ 199402b27f9SRoger Pau Monne #endif 200402b27f9SRoger Pau Monne } __attribute__((__packed__)); 201402b27f9SRoger Pau Monne 20251de6952SOwen Smith struct blkif_request { 20351de6952SOwen Smith uint8_t operation; /* BLKIF_OP_??? */ 20451de6952SOwen Smith union { 20551de6952SOwen Smith struct blkif_request_rw rw; 20632a8d26cSLi Dongyang struct blkif_request_discard discard; 2070e367ae4SDavid Vrabel struct blkif_request_other other; 208402b27f9SRoger Pau Monne struct blkif_request_indirect indirect; 20951de6952SOwen Smith } u; 21097e36834SKonrad Rzeszutek Wilk } __attribute__((__packed__)); 21151de6952SOwen Smith 212a42089ddSJeremy Fitzhardinge struct blkif_response { 213a42089ddSJeremy Fitzhardinge uint64_t id; /* copied from request */ 214a42089ddSJeremy Fitzhardinge uint8_t operation; /* copied from request */ 215a42089ddSJeremy Fitzhardinge int16_t status; /* BLKIF_RSP_??? */ 216a42089ddSJeremy Fitzhardinge }; 217a42089ddSJeremy Fitzhardinge 218a42089ddSJeremy Fitzhardinge /* 219a42089ddSJeremy Fitzhardinge * STATUS RETURN CODES. 220a42089ddSJeremy Fitzhardinge */ 221a42089ddSJeremy Fitzhardinge /* Operation not supported (only happens on barrier writes). */ 222a42089ddSJeremy Fitzhardinge #define BLKIF_RSP_EOPNOTSUPP -2 223a42089ddSJeremy Fitzhardinge /* Operation failed for some unspecified reason (-EIO). */ 224a42089ddSJeremy Fitzhardinge #define BLKIF_RSP_ERROR -1 225a42089ddSJeremy Fitzhardinge /* Operation completed successfully. */ 226a42089ddSJeremy Fitzhardinge #define BLKIF_RSP_OKAY 0 227a42089ddSJeremy Fitzhardinge 228a42089ddSJeremy Fitzhardinge /* 229a42089ddSJeremy Fitzhardinge * Generate blkif ring structures and types. 230a42089ddSJeremy Fitzhardinge */ 231a42089ddSJeremy Fitzhardinge 232a42089ddSJeremy Fitzhardinge DEFINE_RING_TYPES(blkif, struct blkif_request, struct blkif_response); 233a42089ddSJeremy Fitzhardinge 234a42089ddSJeremy Fitzhardinge #define VDISK_CDROM 0x1 235a42089ddSJeremy Fitzhardinge #define VDISK_REMOVABLE 0x2 236a42089ddSJeremy Fitzhardinge #define VDISK_READONLY 0x4 237a42089ddSJeremy Fitzhardinge 238c80a4209SStefano Stabellini /* Xen-defined major numbers for virtual disks, they look strangely 239c80a4209SStefano Stabellini * familiar */ 240c80a4209SStefano Stabellini #define XEN_IDE0_MAJOR 3 241c80a4209SStefano Stabellini #define XEN_IDE1_MAJOR 22 242c80a4209SStefano Stabellini #define XEN_SCSI_DISK0_MAJOR 8 243c80a4209SStefano Stabellini #define XEN_SCSI_DISK1_MAJOR 65 244c80a4209SStefano Stabellini #define XEN_SCSI_DISK2_MAJOR 66 245c80a4209SStefano Stabellini #define XEN_SCSI_DISK3_MAJOR 67 246c80a4209SStefano Stabellini #define XEN_SCSI_DISK4_MAJOR 68 247c80a4209SStefano Stabellini #define XEN_SCSI_DISK5_MAJOR 69 248c80a4209SStefano Stabellini #define XEN_SCSI_DISK6_MAJOR 70 249c80a4209SStefano Stabellini #define XEN_SCSI_DISK7_MAJOR 71 250c80a4209SStefano Stabellini #define XEN_SCSI_DISK8_MAJOR 128 251c80a4209SStefano Stabellini #define XEN_SCSI_DISK9_MAJOR 129 252c80a4209SStefano Stabellini #define XEN_SCSI_DISK10_MAJOR 130 253c80a4209SStefano Stabellini #define XEN_SCSI_DISK11_MAJOR 131 254c80a4209SStefano Stabellini #define XEN_SCSI_DISK12_MAJOR 132 255c80a4209SStefano Stabellini #define XEN_SCSI_DISK13_MAJOR 133 256c80a4209SStefano Stabellini #define XEN_SCSI_DISK14_MAJOR 134 257c80a4209SStefano Stabellini #define XEN_SCSI_DISK15_MAJOR 135 258c80a4209SStefano Stabellini 259a42089ddSJeremy Fitzhardinge #endif /* __XEN_PUBLIC_IO_BLKIF_H__ */ 260