common.h 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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 __BLKIF__BACKEND__COMMON_H__
  27. #define __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. typedef struct blkif_x86_32_request blkif_x86_32_request_t;
  74. typedef struct blkif_x86_32_response blkif_x86_32_response_t;
  75. #pragma pack(pop)
  76. /* x86_64 protocol version */
  77. struct blkif_x86_64_request {
  78. uint8_t operation; /* BLKIF_OP_??? */
  79. uint8_t nr_segments; /* number of segments */
  80. blkif_vdev_t handle; /* only for read/write requests */
  81. uint64_t __attribute__((__aligned__(8))) id;
  82. blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */
  83. struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  84. };
  85. struct blkif_x86_64_response {
  86. uint64_t __attribute__((__aligned__(8))) id;
  87. uint8_t operation; /* copied from request */
  88. int16_t status; /* BLKIF_RSP_??? */
  89. };
  90. typedef struct blkif_x86_64_request blkif_x86_64_request_t;
  91. typedef struct blkif_x86_64_response blkif_x86_64_response_t;
  92. DEFINE_RING_TYPES(blkif_common, struct blkif_common_request,
  93. struct blkif_common_response);
  94. DEFINE_RING_TYPES(blkif_x86_32, struct blkif_x86_32_request,
  95. struct blkif_x86_32_response);
  96. DEFINE_RING_TYPES(blkif_x86_64, struct blkif_x86_64_request,
  97. struct blkif_x86_64_response);
  98. union blkif_back_rings {
  99. struct blkif_back_ring native;
  100. struct blkif_common_back_ring common;
  101. struct blkif_x86_32_back_ring x86_32;
  102. struct blkif_x86_64_back_ring x86_64;
  103. };
  104. enum blkif_protocol {
  105. BLKIF_PROTOCOL_NATIVE = 1,
  106. BLKIF_PROTOCOL_X86_32 = 2,
  107. BLKIF_PROTOCOL_X86_64 = 3,
  108. };
  109. struct vbd {
  110. /* What the domain refers to this vbd as. */
  111. blkif_vdev_t handle;
  112. /* Non-zero -> read-only */
  113. unsigned char readonly;
  114. /* VDISK_xxx */
  115. unsigned char type;
  116. /* phys device that this vbd maps to. */
  117. u32 pdevice;
  118. struct block_device *bdev;
  119. /* Cached size parameter. */
  120. sector_t size;
  121. bool flush_support;
  122. };
  123. struct backend_info;
  124. struct blkif_st {
  125. /* Unique identifier for this interface. */
  126. domid_t domid;
  127. unsigned int handle;
  128. /* Physical parameters of the comms window. */
  129. unsigned int irq;
  130. /* Comms information. */
  131. enum blkif_protocol blk_protocol;
  132. union blkif_back_rings blk_rings;
  133. struct vm_struct *blk_ring_area;
  134. /* The VBD attached to this interface. */
  135. struct vbd vbd;
  136. /* Back pointer to the backend_info. */
  137. struct backend_info *be;
  138. /* Private fields. */
  139. spinlock_t blk_ring_lock;
  140. atomic_t refcnt;
  141. wait_queue_head_t wq;
  142. /* One thread per one blkif. */
  143. struct task_struct *xenblkd;
  144. unsigned int waiting_reqs;
  145. /* statistics */
  146. unsigned long st_print;
  147. int st_rd_req;
  148. int st_wr_req;
  149. int st_oo_req;
  150. int st_f_req;
  151. int st_rd_sect;
  152. int st_wr_sect;
  153. wait_queue_head_t waiting_to_free;
  154. grant_handle_t shmem_handle;
  155. grant_ref_t shmem_ref;
  156. };
  157. #define vbd_sz(_v) ((_v)->bdev->bd_part ? \
  158. (_v)->bdev->bd_part->nr_sects : \
  159. get_capacity((_v)->bdev->bd_disk))
  160. #define xen_blkif_get(_b) (atomic_inc(&(_b)->refcnt))
  161. #define xen_blkif_put(_b) \
  162. do { \
  163. if (atomic_dec_and_test(&(_b)->refcnt)) \
  164. wake_up(&(_b)->waiting_to_free);\
  165. } while (0)
  166. struct phys_req {
  167. unsigned short dev;
  168. unsigned short nr_sects;
  169. struct block_device *bdev;
  170. blkif_sector_t sector_number;
  171. };
  172. int xen_blkif_interface_init(void);
  173. int xen_blkif_xenbus_init(void);
  174. irqreturn_t xen_blkif_be_int(int irq, void *dev_id);
  175. int xen_blkif_schedule(void *arg);
  176. int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt,
  177. struct backend_info *be, int state);
  178. struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be);
  179. static inline void blkif_get_x86_32_req(struct blkif_request *dst,
  180. struct blkif_x86_32_request *src)
  181. {
  182. int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST;
  183. dst->operation = src->operation;
  184. dst->nr_segments = src->nr_segments;
  185. dst->handle = src->handle;
  186. dst->id = src->id;
  187. dst->u.rw.sector_number = src->sector_number;
  188. barrier();
  189. if (n > dst->nr_segments)
  190. n = dst->nr_segments;
  191. for (i = 0; i < n; i++)
  192. dst->u.rw.seg[i] = src->seg[i];
  193. }
  194. static inline void blkif_get_x86_64_req(struct blkif_request *dst,
  195. struct blkif_x86_64_request *src)
  196. {
  197. int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST;
  198. dst->operation = src->operation;
  199. dst->nr_segments = src->nr_segments;
  200. dst->handle = src->handle;
  201. dst->id = src->id;
  202. dst->u.rw.sector_number = src->sector_number;
  203. barrier();
  204. if (n > dst->nr_segments)
  205. n = dst->nr_segments;
  206. for (i = 0; i < n; i++)
  207. dst->u.rw.seg[i] = src->seg[i];
  208. }
  209. #endif /* __BLKIF__BACKEND__COMMON_H__ */