blkif.h 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. /*
  2. * Permission is hereby granted, free of charge, to any person obtaining a copy
  3. * of this software and associated documentation files (the "Software"), to
  4. * deal in the Software without restriction, including without limitation the
  5. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  6. * sell copies of the Software, and to permit persons to whom the Software is
  7. * furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in
  10. * all copies or substantial portions of the Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  17. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  18. * DEALINGS IN THE SOFTWARE.
  19. */
  20. #ifndef __XEN_BLKIF_H__
  21. #define __XEN_BLKIF_H__
  22. #include <xen/interface/io/ring.h>
  23. #include <xen/interface/io/blkif.h>
  24. #include <xen/interface/io/protocols.h>
  25. /* Not a real protocol. Used to generate ring structs which contain
  26. * the elements common to all protocols only. This way we get a
  27. * compiler-checkable way to use common struct elements, so we can
  28. * avoid using switch(protocol) in a number of places. */
  29. struct blkif_common_request {
  30. char dummy;
  31. };
  32. struct blkif_common_response {
  33. char dummy;
  34. };
  35. /* i386 protocol version */
  36. #pragma pack(push, 4)
  37. struct blkif_x86_32_request {
  38. uint8_t operation; /* BLKIF_OP_??? */
  39. uint8_t nr_segments; /* number of segments */
  40. blkif_vdev_t handle; /* only for read/write requests */
  41. uint64_t id; /* private guest value, echoed in resp */
  42. blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */
  43. struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  44. };
  45. struct blkif_x86_32_response {
  46. uint64_t id; /* copied from request */
  47. uint8_t operation; /* copied from request */
  48. int16_t status; /* BLKIF_RSP_??? */
  49. };
  50. typedef struct blkif_x86_32_request blkif_x86_32_request_t;
  51. typedef struct blkif_x86_32_response blkif_x86_32_response_t;
  52. #pragma pack(pop)
  53. /* x86_64 protocol version */
  54. struct blkif_x86_64_request {
  55. uint8_t operation; /* BLKIF_OP_??? */
  56. uint8_t nr_segments; /* number of segments */
  57. blkif_vdev_t handle; /* only for read/write requests */
  58. uint64_t __attribute__((__aligned__(8))) id;
  59. blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */
  60. struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  61. };
  62. struct blkif_x86_64_response {
  63. uint64_t __attribute__((__aligned__(8))) id;
  64. uint8_t operation; /* copied from request */
  65. int16_t status; /* BLKIF_RSP_??? */
  66. };
  67. typedef struct blkif_x86_64_request blkif_x86_64_request_t;
  68. typedef struct blkif_x86_64_response blkif_x86_64_response_t;
  69. DEFINE_RING_TYPES(blkif_common, struct blkif_common_request, struct blkif_common_response);
  70. DEFINE_RING_TYPES(blkif_x86_32, struct blkif_x86_32_request, struct blkif_x86_32_response);
  71. DEFINE_RING_TYPES(blkif_x86_64, struct blkif_x86_64_request, struct blkif_x86_64_response);
  72. union blkif_back_rings {
  73. struct blkif_back_ring native;
  74. struct blkif_common_back_ring common;
  75. struct blkif_x86_32_back_ring x86_32;
  76. struct blkif_x86_64_back_ring x86_64;
  77. };
  78. enum blkif_protocol {
  79. BLKIF_PROTOCOL_NATIVE = 1,
  80. BLKIF_PROTOCOL_X86_32 = 2,
  81. BLKIF_PROTOCOL_X86_64 = 3,
  82. };
  83. static void inline blkif_get_x86_32_req(struct blkif_request *dst, struct blkif_x86_32_request *src)
  84. {
  85. int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST;
  86. dst->operation = src->operation;
  87. dst->nr_segments = src->nr_segments;
  88. dst->handle = src->handle;
  89. dst->id = src->id;
  90. dst->sector_number = src->sector_number;
  91. barrier();
  92. if (n > dst->nr_segments)
  93. n = dst->nr_segments;
  94. for (i = 0; i < n; i++)
  95. dst->seg[i] = src->seg[i];
  96. }
  97. static void inline blkif_get_x86_64_req(struct blkif_request *dst, struct blkif_x86_64_request *src)
  98. {
  99. int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST;
  100. dst->operation = src->operation;
  101. dst->nr_segments = src->nr_segments;
  102. dst->handle = src->handle;
  103. dst->id = src->id;
  104. dst->sector_number = src->sector_number;
  105. barrier();
  106. if (n > dst->nr_segments)
  107. n = dst->nr_segments;
  108. for (i = 0; i < n; i++)
  109. dst->seg[i] = src->seg[i];
  110. }
  111. #endif /* __XEN_BLKIF_H__ */