common.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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_rw {
  61. blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */
  62. struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  63. };
  64. struct blkif_x86_32_request_discard {
  65. blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */
  66. uint64_t nr_sectors;
  67. };
  68. struct blkif_x86_32_request {
  69. uint8_t operation; /* BLKIF_OP_??? */
  70. uint8_t nr_segments; /* number of segments */
  71. blkif_vdev_t handle; /* only for read/write requests */
  72. uint64_t id; /* private guest value, echoed in resp */
  73. union {
  74. struct blkif_x86_32_request_rw rw;
  75. struct blkif_x86_32_request_discard discard;
  76. } u;
  77. };
  78. struct blkif_x86_32_response {
  79. uint64_t id; /* copied from request */
  80. uint8_t operation; /* copied from request */
  81. int16_t status; /* BLKIF_RSP_??? */
  82. };
  83. #pragma pack(pop)
  84. /* x86_64 protocol version */
  85. struct blkif_x86_64_request_rw {
  86. blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */
  87. struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  88. };
  89. struct blkif_x86_64_request_discard {
  90. blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */
  91. uint64_t nr_sectors;
  92. };
  93. struct blkif_x86_64_request {
  94. uint8_t operation; /* BLKIF_OP_??? */
  95. uint8_t nr_segments; /* number of segments */
  96. blkif_vdev_t handle; /* only for read/write requests */
  97. uint64_t __attribute__((__aligned__(8))) id;
  98. union {
  99. struct blkif_x86_64_request_rw rw;
  100. struct blkif_x86_64_request_discard discard;
  101. } u;
  102. };
  103. struct blkif_x86_64_response {
  104. uint64_t __attribute__((__aligned__(8))) id;
  105. uint8_t operation; /* copied from request */
  106. int16_t status; /* BLKIF_RSP_??? */
  107. };
  108. DEFINE_RING_TYPES(blkif_common, struct blkif_common_request,
  109. struct blkif_common_response);
  110. DEFINE_RING_TYPES(blkif_x86_32, struct blkif_x86_32_request,
  111. struct blkif_x86_32_response);
  112. DEFINE_RING_TYPES(blkif_x86_64, struct blkif_x86_64_request,
  113. struct blkif_x86_64_response);
  114. union blkif_back_rings {
  115. struct blkif_back_ring native;
  116. struct blkif_common_back_ring common;
  117. struct blkif_x86_32_back_ring x86_32;
  118. struct blkif_x86_64_back_ring x86_64;
  119. };
  120. enum blkif_protocol {
  121. BLKIF_PROTOCOL_NATIVE = 1,
  122. BLKIF_PROTOCOL_X86_32 = 2,
  123. BLKIF_PROTOCOL_X86_64 = 3,
  124. };
  125. enum blkif_backend_type {
  126. BLKIF_BACKEND_PHY = 1,
  127. BLKIF_BACKEND_FILE = 2,
  128. };
  129. struct xen_vbd {
  130. /* What the domain refers to this vbd as. */
  131. blkif_vdev_t handle;
  132. /* Non-zero -> read-only */
  133. unsigned char readonly;
  134. /* VDISK_xxx */
  135. unsigned char type;
  136. /* phys device that this vbd maps to. */
  137. u32 pdevice;
  138. struct block_device *bdev;
  139. /* Cached size parameter. */
  140. sector_t size;
  141. bool flush_support;
  142. };
  143. struct backend_info;
  144. struct xen_blkif {
  145. /* Unique identifier for this interface. */
  146. domid_t domid;
  147. unsigned int handle;
  148. /* Physical parameters of the comms window. */
  149. unsigned int irq;
  150. /* Comms information. */
  151. enum blkif_protocol blk_protocol;
  152. enum blkif_backend_type blk_backend_type;
  153. union blkif_back_rings blk_rings;
  154. struct vm_struct *blk_ring_area;
  155. /* The VBD attached to this interface. */
  156. struct xen_vbd vbd;
  157. /* Back pointer to the backend_info. */
  158. struct backend_info *be;
  159. /* Private fields. */
  160. spinlock_t blk_ring_lock;
  161. atomic_t refcnt;
  162. wait_queue_head_t wq;
  163. /* One thread per one blkif. */
  164. struct task_struct *xenblkd;
  165. unsigned int waiting_reqs;
  166. /* statistics */
  167. unsigned long st_print;
  168. int st_rd_req;
  169. int st_wr_req;
  170. int st_oo_req;
  171. int st_f_req;
  172. int st_ds_req;
  173. int st_rd_sect;
  174. int st_wr_sect;
  175. wait_queue_head_t waiting_to_free;
  176. grant_handle_t shmem_handle;
  177. grant_ref_t shmem_ref;
  178. };
  179. #define vbd_sz(_v) ((_v)->bdev->bd_part ? \
  180. (_v)->bdev->bd_part->nr_sects : \
  181. get_capacity((_v)->bdev->bd_disk))
  182. #define xen_blkif_get(_b) (atomic_inc(&(_b)->refcnt))
  183. #define xen_blkif_put(_b) \
  184. do { \
  185. if (atomic_dec_and_test(&(_b)->refcnt)) \
  186. wake_up(&(_b)->waiting_to_free);\
  187. } while (0)
  188. struct phys_req {
  189. unsigned short dev;
  190. blkif_sector_t nr_sects;
  191. struct block_device *bdev;
  192. blkif_sector_t sector_number;
  193. };
  194. int xen_blkif_interface_init(void);
  195. int xen_blkif_xenbus_init(void);
  196. irqreturn_t xen_blkif_be_int(int irq, void *dev_id);
  197. int xen_blkif_schedule(void *arg);
  198. int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt,
  199. struct backend_info *be, int state);
  200. struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be);
  201. static inline void blkif_get_x86_32_req(struct blkif_request *dst,
  202. struct blkif_x86_32_request *src)
  203. {
  204. int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST;
  205. dst->operation = src->operation;
  206. dst->nr_segments = src->nr_segments;
  207. dst->handle = src->handle;
  208. dst->id = src->id;
  209. switch (src->operation) {
  210. case BLKIF_OP_READ:
  211. case BLKIF_OP_WRITE:
  212. case BLKIF_OP_WRITE_BARRIER:
  213. case BLKIF_OP_FLUSH_DISKCACHE:
  214. dst->u.rw.sector_number = src->u.rw.sector_number;
  215. barrier();
  216. if (n > dst->nr_segments)
  217. n = dst->nr_segments;
  218. for (i = 0; i < n; i++)
  219. dst->u.rw.seg[i] = src->u.rw.seg[i];
  220. break;
  221. case BLKIF_OP_DISCARD:
  222. dst->u.discard.sector_number = src->u.discard.sector_number;
  223. dst->u.discard.nr_sectors = src->u.discard.nr_sectors;
  224. break;
  225. default:
  226. break;
  227. }
  228. }
  229. static inline void blkif_get_x86_64_req(struct blkif_request *dst,
  230. struct blkif_x86_64_request *src)
  231. {
  232. int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST;
  233. dst->operation = src->operation;
  234. dst->nr_segments = src->nr_segments;
  235. dst->handle = src->handle;
  236. dst->id = src->id;
  237. switch (src->operation) {
  238. case BLKIF_OP_READ:
  239. case BLKIF_OP_WRITE:
  240. case BLKIF_OP_WRITE_BARRIER:
  241. case BLKIF_OP_FLUSH_DISKCACHE:
  242. dst->u.rw.sector_number = src->u.rw.sector_number;
  243. barrier();
  244. if (n > dst->nr_segments)
  245. n = dst->nr_segments;
  246. for (i = 0; i < n; i++)
  247. dst->u.rw.seg[i] = src->u.rw.seg[i];
  248. break;
  249. case BLKIF_OP_DISCARD:
  250. dst->u.discard.sector_number = src->u.discard.sector_number;
  251. dst->u.discard.nr_sectors = src->u.discard.nr_sectors;
  252. break;
  253. default:
  254. break;
  255. }
  256. }
  257. #endif /* __XEN_BLKIF__BACKEND__COMMON_H__ */