common.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * This program is free software; you can redistribute it and/or
  3. * modify it under the terms of the GNU General Public License version 2
  4. * as published by the Free Software Foundation; or, when distributed
  5. * separately from the Linux kernel or incorporated into other
  6. * software packages, subject to the following license:
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this source file (the "Software"), to deal in the Software without
  10. * restriction, including without limitation the rights to use, copy, modify,
  11. * merge, publish, distribute, sublicense, and/or sell copies of the Software,
  12. * and to permit persons to whom the Software is furnished to do so, subject to
  13. * the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in
  16. * all copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  23. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  24. * IN THE SOFTWARE.
  25. */
  26. #ifndef __XEN_BLKIF__BACKEND__COMMON_H__
  27. #define __XEN_BLKIF__BACKEND__COMMON_H__
  28. #include <linux/version.h>
  29. #include <linux/module.h>
  30. #include <linux/interrupt.h>
  31. #include <linux/slab.h>
  32. #include <linux/blkdev.h>
  33. #include <linux/vmalloc.h>
  34. #include <linux/wait.h>
  35. #include <linux/io.h>
  36. #include <asm/setup.h>
  37. #include <asm/pgalloc.h>
  38. #include <asm/hypervisor.h>
  39. #include <xen/grant_table.h>
  40. #include <xen/xenbus.h>
  41. #include <xen/interface/io/ring.h>
  42. #include <xen/interface/io/blkif.h>
  43. #include <xen/interface/io/protocols.h>
  44. #define DRV_PFX "xen-blkback:"
  45. #define DPRINTK(fmt, args...) \
  46. pr_debug(DRV_PFX "(%s:%d) " fmt ".\n", \
  47. __func__, __LINE__, ##args)
  48. /* Not a real protocol. Used to generate ring structs which contain
  49. * the elements common to all protocols only. This way we get a
  50. * compiler-checkable way to use common struct elements, so we can
  51. * avoid using switch(protocol) in a number of places. */
  52. struct blkif_common_request {
  53. char dummy;
  54. };
  55. struct blkif_common_response {
  56. char dummy;
  57. };
  58. /* i386 protocol version */
  59. #pragma pack(push, 4)
  60. struct blkif_x86_32_request {
  61. uint8_t operation; /* BLKIF_OP_??? */
  62. uint8_t nr_segments; /* number of segments */
  63. blkif_vdev_t handle; /* only for read/write requests */
  64. uint64_t id; /* private guest value, echoed in resp */
  65. blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */
  66. struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  67. };
  68. struct blkif_x86_32_response {
  69. uint64_t id; /* copied from request */
  70. uint8_t operation; /* copied from request */
  71. int16_t status; /* BLKIF_RSP_??? */
  72. };
  73. #pragma pack(pop)
  74. /* x86_64 protocol version */
  75. struct blkif_x86_64_request {
  76. uint8_t operation; /* BLKIF_OP_??? */
  77. uint8_t nr_segments; /* number of segments */
  78. blkif_vdev_t handle; /* only for read/write requests */
  79. uint64_t __attribute__((__aligned__(8))) id;
  80. blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */
  81. struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  82. };
  83. struct blkif_x86_64_response {
  84. uint64_t __attribute__((__aligned__(8))) id;
  85. uint8_t operation; /* copied from request */
  86. int16_t status; /* BLKIF_RSP_??? */
  87. };
  88. DEFINE_RING_TYPES(blkif_common, struct blkif_common_request,
  89. struct blkif_common_response);
  90. DEFINE_RING_TYPES(blkif_x86_32, struct blkif_x86_32_request,
  91. struct blkif_x86_32_response);
  92. DEFINE_RING_TYPES(blkif_x86_64, struct blkif_x86_64_request,
  93. struct blkif_x86_64_response);
  94. union blkif_back_rings {
  95. struct blkif_back_ring native;
  96. struct blkif_common_back_ring common;
  97. struct blkif_x86_32_back_ring x86_32;
  98. struct blkif_x86_64_back_ring x86_64;
  99. };
  100. enum blkif_protocol {
  101. BLKIF_PROTOCOL_NATIVE = 1,
  102. BLKIF_PROTOCOL_X86_32 = 2,
  103. BLKIF_PROTOCOL_X86_64 = 3,
  104. };
  105. struct xen_vbd {
  106. /* What the domain refers to this vbd as. */
  107. blkif_vdev_t handle;
  108. /* Non-zero -> read-only */
  109. unsigned char readonly;
  110. /* VDISK_xxx */
  111. unsigned char type;
  112. /* phys device that this vbd maps to. */
  113. u32 pdevice;
  114. struct block_device *bdev;
  115. /* Cached size parameter. */
  116. sector_t size;
  117. bool flush_support;
  118. };
  119. struct backend_info;
  120. struct xen_blkif {
  121. /* Unique identifier for this interface. */
  122. domid_t domid;
  123. unsigned int handle;
  124. /* Physical parameters of the comms window. */
  125. unsigned int irq;
  126. /* Comms information. */
  127. enum blkif_protocol blk_protocol;
  128. union blkif_back_rings blk_rings;
  129. struct vm_struct *blk_ring_area;
  130. /* The VBD attached to this interface. */
  131. struct xen_vbd vbd;
  132. /* Back pointer to the backend_info. */
  133. struct backend_info *be;
  134. /* Private fields. */
  135. spinlock_t blk_ring_lock;
  136. atomic_t refcnt;
  137. wait_queue_head_t wq;
  138. /* One thread per one blkif. */
  139. struct task_struct *xenblkd;
  140. unsigned int waiting_reqs;
  141. /* statistics */
  142. unsigned long st_print;
  143. int st_rd_req;
  144. int st_wr_req;
  145. int st_oo_req;
  146. int st_f_req;
  147. int st_rd_sect;
  148. int st_wr_sect;
  149. wait_queue_head_t waiting_to_free;
  150. grant_handle_t shmem_handle;
  151. grant_ref_t shmem_ref;
  152. };
  153. #define vbd_sz(_v) ((_v)->bdev->bd_part ? \
  154. (_v)->bdev->bd_part->nr_sects : \
  155. get_capacity((_v)->bdev->bd_disk))
  156. #define xen_blkif_get(_b) (atomic_inc(&(_b)->refcnt))
  157. #define xen_blkif_put(_b) \
  158. do { \
  159. if (atomic_dec_and_test(&(_b)->refcnt)) \
  160. wake_up(&(_b)->waiting_to_free);\
  161. } while (0)
  162. struct phys_req {
  163. unsigned short dev;
  164. unsigned short nr_sects;
  165. struct block_device *bdev;
  166. blkif_sector_t sector_number;
  167. };
  168. int xen_blkif_interface_init(void);
  169. int xen_blkif_xenbus_init(void);
  170. irqreturn_t xen_blkif_be_int(int irq, void *dev_id);
  171. int xen_blkif_schedule(void *arg);
  172. int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt,
  173. struct backend_info *be, int state);
  174. struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be);
  175. static inline void blkif_get_x86_32_req(struct blkif_request *dst,
  176. struct blkif_x86_32_request *src)
  177. {
  178. int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST;
  179. dst->operation = src->operation;
  180. dst->nr_segments = src->nr_segments;
  181. dst->handle = src->handle;
  182. dst->id = src->id;
  183. dst->u.rw.sector_number = src->sector_number;
  184. barrier();
  185. if (n > dst->nr_segments)
  186. n = dst->nr_segments;
  187. for (i = 0; i < n; i++)
  188. dst->u.rw.seg[i] = src->seg[i];
  189. }
  190. static inline void blkif_get_x86_64_req(struct blkif_request *dst,
  191. struct blkif_x86_64_request *src)
  192. {
  193. int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST;
  194. dst->operation = src->operation;
  195. dst->nr_segments = src->nr_segments;
  196. dst->handle = src->handle;
  197. dst->id = src->id;
  198. dst->u.rw.sector_number = src->sector_number;
  199. barrier();
  200. if (n > dst->nr_segments)
  201. n = dst->nr_segments;
  202. for (i = 0; i < n; i++)
  203. dst->u.rw.seg[i] = src->seg[i];
  204. }
  205. #endif /* __XEN_BLKIF__BACKEND__COMMON_H__ */