blkif.h 13 KB

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