blkif.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /******************************************************************************
  2. * blkif.h
  3. *
  4. * Unified block-device I/O interface for Xen guest OSes.
  5. *
  6. * Copyright (c) 2003-2004, Keir Fraser
  7. */
  8. #ifndef __XEN_PUBLIC_IO_BLKIF_H__
  9. #define __XEN_PUBLIC_IO_BLKIF_H__
  10. #include <xen/interface/io/ring.h>
  11. #include <xen/interface/grant_table.h>
  12. /*
  13. * Front->back notifications: When enqueuing a new request, sending a
  14. * notification can be made conditional on req_event (i.e., the generic
  15. * hold-off mechanism provided by the ring macros). Backends must set
  16. * req_event appropriately (e.g., using RING_FINAL_CHECK_FOR_REQUESTS()).
  17. *
  18. * Back->front notifications: When enqueuing a new response, sending a
  19. * notification can be made conditional on rsp_event (i.e., the generic
  20. * hold-off mechanism provided by the ring macros). Frontends must set
  21. * rsp_event appropriately (e.g., using RING_FINAL_CHECK_FOR_RESPONSES()).
  22. */
  23. typedef uint16_t blkif_vdev_t;
  24. typedef uint64_t blkif_sector_t;
  25. /*
  26. * REQUEST CODES.
  27. */
  28. #define BLKIF_OP_READ 0
  29. #define BLKIF_OP_WRITE 1
  30. /*
  31. * Recognised only if "feature-barrier" is present in backend xenbus info.
  32. * The "feature_barrier" node contains a boolean indicating whether barrier
  33. * requests are likely to succeed or fail. Either way, a barrier request
  34. * may fail at any time with BLKIF_RSP_EOPNOTSUPP if it is unsupported by
  35. * the underlying block-device hardware. The boolean simply indicates whether
  36. * or not it is worthwhile for the frontend to attempt barrier requests.
  37. * If a backend does not recognise BLKIF_OP_WRITE_BARRIER, it should *not*
  38. * create the "feature-barrier" node!
  39. */
  40. #define BLKIF_OP_WRITE_BARRIER 2
  41. /*
  42. * Recognised if "feature-flush-cache" is present in backend xenbus
  43. * info. A flush will ask the underlying storage hardware to flush its
  44. * non-volatile caches as appropriate. The "feature-flush-cache" node
  45. * contains a boolean indicating whether flush requests are likely to
  46. * succeed or fail. Either way, a flush request may fail at any time
  47. * with BLKIF_RSP_EOPNOTSUPP if it is unsupported by the underlying
  48. * block-device hardware. The boolean simply indicates whether or not it
  49. * is worthwhile for the frontend to attempt flushes. If a backend does
  50. * not recognise BLKIF_OP_WRITE_FLUSH_CACHE, it should *not* create the
  51. * "feature-flush-cache" node!
  52. */
  53. #define BLKIF_OP_FLUSH_DISKCACHE 3
  54. /*
  55. * Recognised only if "feature-discard" is present in backend xenbus info.
  56. * The "feature-discard" node contains a boolean indicating whether trim
  57. * (ATA) or unmap (SCSI) - conviently called discard requests are likely
  58. * to succeed or fail. Either way, a discard request
  59. * may fail at any time with BLKIF_RSP_EOPNOTSUPP if it is unsupported by
  60. * the underlying block-device hardware. The boolean simply indicates whether
  61. * or not it is worthwhile for the frontend to attempt discard requests.
  62. * If a backend does not recognise BLKIF_OP_DISCARD, it should *not*
  63. * create the "feature-discard" node!
  64. *
  65. * Discard operation is a request for the underlying block device to mark
  66. * extents to be erased. However, discard does not guarantee that the blocks
  67. * will be erased from the device - it is just a hint to the device
  68. * controller that these blocks are no longer in use. What the device
  69. * controller does with that information is left to the controller.
  70. * Discard operations are passed with sector_number as the
  71. * sector index to begin discard operations at and nr_sectors as the number of
  72. * sectors to be discarded. The specified sectors should be discarded if the
  73. * underlying block device supports trim (ATA) or unmap (SCSI) operations,
  74. * or a BLKIF_RSP_EOPNOTSUPP should be returned.
  75. * More information about trim/unmap operations at:
  76. * http://t13.org/Documents/UploadedDocuments/docs2008/
  77. * e07154r6-Data_Set_Management_Proposal_for_ATA-ACS2.doc
  78. * http://www.seagate.com/staticfiles/support/disc/manuals/
  79. * Interface%20manuals/100293068c.pdf
  80. * The backend can optionally provide three extra XenBus attributes to
  81. * further optimize the discard functionality:
  82. * 'discard-aligment' - Devices that support discard functionality may
  83. * internally allocate space in units that are bigger than the exported
  84. * logical block size. The discard-alignment parameter indicates how many bytes
  85. * the beginning of the partition is offset from the internal allocation unit's
  86. * natural alignment.
  87. * 'discard-granularity' - Devices that support discard functionality may
  88. * internally allocate space using units that are bigger than the logical block
  89. * size. The discard-granularity parameter indicates the size of the internal
  90. * allocation unit in bytes if reported by the device. Otherwise the
  91. * discard-granularity will be set to match the device's physical block size.
  92. * 'discard-secure' - All copies of the discarded sectors (potentially created
  93. * by garbage collection) must also be erased. To use this feature, the flag
  94. * BLKIF_DISCARD_SECURE must be set in the blkif_request_trim.
  95. */
  96. #define BLKIF_OP_DISCARD 5
  97. /*
  98. * Recognized if "feature-max-indirect-segments" in present in the backend
  99. * xenbus info. The "feature-max-indirect-segments" node contains the maximum
  100. * number of segments allowed by the backend per request. If the node is
  101. * present, the frontend might use blkif_request_indirect structs in order to
  102. * issue requests with more than BLKIF_MAX_SEGMENTS_PER_REQUEST (11). The
  103. * maximum number of indirect segments is fixed by the backend, but the
  104. * frontend can issue requests with any number of indirect segments as long as
  105. * it's less than the number provided by the backend. The indirect_grefs field
  106. * in blkif_request_indirect should be filled by the frontend with the
  107. * grant references of the pages that are holding the indirect segments.
  108. * This pages are filled with an array of blkif_request_segment_aligned
  109. * that hold the information about the segments. The number of indirect
  110. * pages to use is determined by the maximum number of segments
  111. * a indirect request contains. Every indirect page can contain a maximum
  112. * of 512 segments (PAGE_SIZE/sizeof(blkif_request_segment_aligned)),
  113. * so to calculate the number of indirect pages to use we have to do
  114. * ceil(indirect_segments/512).
  115. *
  116. * If a backend does not recognize BLKIF_OP_INDIRECT, it should *not*
  117. * create the "feature-max-indirect-segments" node!
  118. */
  119. #define BLKIF_OP_INDIRECT 6
  120. /*
  121. * Maximum scatter/gather segments per request.
  122. * This is carefully chosen so that sizeof(struct blkif_ring) <= PAGE_SIZE.
  123. * NB. This could be 12 if the ring indexes weren't stored in the same page.
  124. */
  125. #define BLKIF_MAX_SEGMENTS_PER_REQUEST 11
  126. #define BLKIF_MAX_INDIRECT_PAGES_PER_REQUEST 8
  127. struct blkif_request_segment_aligned {
  128. grant_ref_t gref; /* reference to I/O buffer frame */
  129. /* @first_sect: first sector in frame to transfer (inclusive). */
  130. /* @last_sect: last sector in frame to transfer (inclusive). */
  131. uint8_t first_sect, last_sect;
  132. uint16_t _pad; /* padding to make it 8 bytes, so it's cache-aligned */
  133. } __attribute__((__packed__));
  134. struct blkif_request_rw {
  135. uint8_t nr_segments; /* number of segments */
  136. blkif_vdev_t handle; /* only for read/write requests */
  137. #ifdef CONFIG_X86_64
  138. uint32_t _pad1; /* offsetof(blkif_request,u.rw.id) == 8 */
  139. #endif
  140. uint64_t id; /* private guest value, echoed in resp */
  141. blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */
  142. struct blkif_request_segment {
  143. grant_ref_t gref; /* reference to I/O buffer frame */
  144. /* @first_sect: first sector in frame to transfer (inclusive). */
  145. /* @last_sect: last sector in frame to transfer (inclusive). */
  146. uint8_t first_sect, last_sect;
  147. } seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  148. } __attribute__((__packed__));
  149. struct blkif_request_discard {
  150. uint8_t flag; /* BLKIF_DISCARD_SECURE or zero. */
  151. #define BLKIF_DISCARD_SECURE (1<<0) /* ignored if discard-secure=0 */
  152. blkif_vdev_t _pad1; /* only for read/write requests */
  153. #ifdef CONFIG_X86_64
  154. uint32_t _pad2; /* offsetof(blkif_req..,u.discard.id)==8*/
  155. #endif
  156. uint64_t id; /* private guest value, echoed in resp */
  157. blkif_sector_t sector_number;
  158. uint64_t nr_sectors;
  159. uint8_t _pad3;
  160. } __attribute__((__packed__));
  161. struct blkif_request_other {
  162. uint8_t _pad1;
  163. blkif_vdev_t _pad2; /* only for read/write requests */
  164. #ifdef CONFIG_X86_64
  165. uint32_t _pad3; /* offsetof(blkif_req..,u.other.id)==8*/
  166. #endif
  167. uint64_t id; /* private guest value, echoed in resp */
  168. } __attribute__((__packed__));
  169. struct blkif_request_indirect {
  170. uint8_t indirect_op;
  171. uint16_t nr_segments;
  172. #ifdef CONFIG_X86_64
  173. uint32_t _pad1; /* offsetof(blkif_...,u.indirect.id) == 8 */
  174. #endif
  175. uint64_t id;
  176. blkif_sector_t sector_number;
  177. blkif_vdev_t handle;
  178. uint16_t _pad2;
  179. grant_ref_t indirect_grefs[BLKIF_MAX_INDIRECT_PAGES_PER_REQUEST];
  180. #ifdef CONFIG_X86_64
  181. uint32_t _pad3; /* make it 64 byte aligned */
  182. #else
  183. uint64_t _pad3; /* make it 64 byte aligned */
  184. #endif
  185. } __attribute__((__packed__));
  186. struct blkif_request {
  187. uint8_t operation; /* BLKIF_OP_??? */
  188. union {
  189. struct blkif_request_rw rw;
  190. struct blkif_request_discard discard;
  191. struct blkif_request_other other;
  192. struct blkif_request_indirect indirect;
  193. } u;
  194. } __attribute__((__packed__));
  195. struct blkif_response {
  196. uint64_t id; /* copied from request */
  197. uint8_t operation; /* copied from request */
  198. int16_t status; /* BLKIF_RSP_??? */
  199. };
  200. /*
  201. * STATUS RETURN CODES.
  202. */
  203. /* Operation not supported (only happens on barrier writes). */
  204. #define BLKIF_RSP_EOPNOTSUPP -2
  205. /* Operation failed for some unspecified reason (-EIO). */
  206. #define BLKIF_RSP_ERROR -1
  207. /* Operation completed successfully. */
  208. #define BLKIF_RSP_OKAY 0
  209. /*
  210. * Generate blkif ring structures and types.
  211. */
  212. DEFINE_RING_TYPES(blkif, struct blkif_request, struct blkif_response);
  213. #define VDISK_CDROM 0x1
  214. #define VDISK_REMOVABLE 0x2
  215. #define VDISK_READONLY 0x4
  216. /* Xen-defined major numbers for virtual disks, they look strangely
  217. * familiar */
  218. #define XEN_IDE0_MAJOR 3
  219. #define XEN_IDE1_MAJOR 22
  220. #define XEN_SCSI_DISK0_MAJOR 8
  221. #define XEN_SCSI_DISK1_MAJOR 65
  222. #define XEN_SCSI_DISK2_MAJOR 66
  223. #define XEN_SCSI_DISK3_MAJOR 67
  224. #define XEN_SCSI_DISK4_MAJOR 68
  225. #define XEN_SCSI_DISK5_MAJOR 69
  226. #define XEN_SCSI_DISK6_MAJOR 70
  227. #define XEN_SCSI_DISK7_MAJOR 71
  228. #define XEN_SCSI_DISK8_MAJOR 128
  229. #define XEN_SCSI_DISK9_MAJOR 129
  230. #define XEN_SCSI_DISK10_MAJOR 130
  231. #define XEN_SCSI_DISK11_MAJOR 131
  232. #define XEN_SCSI_DISK12_MAJOR 132
  233. #define XEN_SCSI_DISK13_MAJOR 133
  234. #define XEN_SCSI_DISK14_MAJOR 134
  235. #define XEN_SCSI_DISK15_MAJOR 135
  236. #endif /* __XEN_PUBLIC_IO_BLKIF_H__ */