common.h 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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/module.h>
  29. #include <linux/interrupt.h>
  30. #include <linux/slab.h>
  31. #include <linux/blkdev.h>
  32. #include <linux/vmalloc.h>
  33. #include <linux/wait.h>
  34. #include <linux/io.h>
  35. #include <asm/setup.h>
  36. #include <asm/pgalloc.h>
  37. #include <asm/hypervisor.h>
  38. #include <xen/grant_table.h>
  39. #include <xen/xenbus.h>
  40. #include <xen/interface/io/ring.h>
  41. #include <xen/interface/io/blkif.h>
  42. #include <xen/interface/io/protocols.h>
  43. #define DRV_PFX "xen-blkback:"
  44. #define DPRINTK(fmt, args...) \
  45. pr_debug(DRV_PFX "(%s:%d) " fmt ".\n", \
  46. __func__, __LINE__, ##args)
  47. /* Not a real protocol. Used to generate ring structs which contain
  48. * the elements common to all protocols only. This way we get a
  49. * compiler-checkable way to use common struct elements, so we can
  50. * avoid using switch(protocol) in a number of places. */
  51. struct blkif_common_request {
  52. char dummy;
  53. };
  54. struct blkif_common_response {
  55. char dummy;
  56. };
  57. /* i386 protocol version */
  58. #pragma pack(push, 4)
  59. struct blkif_x86_32_request_rw {
  60. blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */
  61. struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  62. };
  63. struct blkif_x86_32_request_discard {
  64. blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */
  65. uint64_t nr_sectors;
  66. };
  67. struct blkif_x86_32_request {
  68. uint8_t operation; /* BLKIF_OP_??? */
  69. uint8_t nr_segments; /* number of segments */
  70. blkif_vdev_t handle; /* only for read/write requests */
  71. uint64_t id; /* private guest value, echoed in resp */
  72. union {
  73. struct blkif_x86_32_request_rw rw;
  74. struct blkif_x86_32_request_discard discard;
  75. } u;
  76. };
  77. struct blkif_x86_32_response {
  78. uint64_t id; /* copied from request */
  79. uint8_t operation; /* copied from request */
  80. int16_t status; /* BLKIF_RSP_??? */
  81. };
  82. #pragma pack(pop)
  83. /* x86_64 protocol version */
  84. struct blkif_x86_64_request_rw {
  85. blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */
  86. struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  87. };
  88. struct blkif_x86_64_request_discard {
  89. blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */
  90. uint64_t nr_sectors;
  91. };
  92. struct blkif_x86_64_request {
  93. uint8_t operation; /* BLKIF_OP_??? */
  94. uint8_t nr_segments; /* number of segments */
  95. blkif_vdev_t handle; /* only for read/write requests */
  96. uint64_t __attribute__((__aligned__(8))) id;
  97. union {
  98. struct blkif_x86_64_request_rw rw;
  99. struct blkif_x86_64_request_discard discard;
  100. } u;
  101. };
  102. struct blkif_x86_64_response {
  103. uint64_t __attribute__((__aligned__(8))) id;
  104. uint8_t operation; /* copied from request */
  105. int16_t status; /* BLKIF_RSP_??? */
  106. };
  107. DEFINE_RING_TYPES(blkif_common, struct blkif_common_request,
  108. struct blkif_common_response);
  109. DEFINE_RING_TYPES(blkif_x86_32, struct blkif_x86_32_request,
  110. struct blkif_x86_32_response);
  111. DEFINE_RING_TYPES(blkif_x86_64, struct blkif_x86_64_request,
  112. struct blkif_x86_64_response);
  113. union blkif_back_rings {
  114. struct blkif_back_ring native;
  115. struct blkif_common_back_ring common;
  116. struct blkif_x86_32_back_ring x86_32;
  117. struct blkif_x86_64_back_ring x86_64;
  118. };
  119. enum blkif_protocol {
  120. BLKIF_PROTOCOL_NATIVE = 1,
  121. BLKIF_PROTOCOL_X86_32 = 2,
  122. BLKIF_PROTOCOL_X86_64 = 3,
  123. };
  124. enum blkif_backend_type {
  125. BLKIF_BACKEND_PHY = 1,
  126. BLKIF_BACKEND_FILE = 2,
  127. };
  128. struct xen_vbd {
  129. /* What the domain refers to this vbd as. */
  130. blkif_vdev_t handle;
  131. /* Non-zero -> read-only */
  132. unsigned char readonly;
  133. /* VDISK_xxx */
  134. unsigned char type;
  135. /* phys device that this vbd maps to. */
  136. u32 pdevice;
  137. struct block_device *bdev;
  138. /* Cached size parameter. */
  139. sector_t size;
  140. bool flush_support;
  141. };
  142. struct backend_info;
  143. struct xen_blkif {
  144. /* Unique identifier for this interface. */
  145. domid_t domid;
  146. unsigned int handle;
  147. /* Physical parameters of the comms window. */
  148. unsigned int irq;
  149. /* Comms information. */
  150. enum blkif_protocol blk_protocol;
  151. enum blkif_backend_type blk_backend_type;
  152. union blkif_back_rings blk_rings;
  153. void *blk_ring;
  154. /* The VBD attached to this interface. */
  155. struct xen_vbd vbd;
  156. /* Back pointer to the backend_info. */
  157. struct backend_info *be;
  158. /* Private fields. */
  159. spinlock_t blk_ring_lock;
  160. atomic_t refcnt;
  161. wait_queue_head_t wq;
  162. /* for barrier (drain) requests */
  163. struct completion drain_complete;
  164. atomic_t drain;
  165. /* One thread per one blkif. */
  166. struct task_struct *xenblkd;
  167. unsigned int waiting_reqs;
  168. /* statistics */
  169. unsigned long st_print;
  170. int st_rd_req;
  171. int st_wr_req;
  172. int st_oo_req;
  173. int st_f_req;
  174. int st_ds_req;
  175. int st_rd_sect;
  176. int st_wr_sect;
  177. wait_queue_head_t waiting_to_free;
  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. int xen_blkbk_barrier(struct xenbus_transaction xbt,
  201. struct backend_info *be, int state);
  202. struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be);
  203. static inline void blkif_get_x86_32_req(struct blkif_request *dst,
  204. struct blkif_x86_32_request *src)
  205. {
  206. int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST;
  207. dst->operation = src->operation;
  208. dst->nr_segments = src->nr_segments;
  209. dst->handle = src->handle;
  210. dst->id = src->id;
  211. switch (src->operation) {
  212. case BLKIF_OP_READ:
  213. case BLKIF_OP_WRITE:
  214. case BLKIF_OP_WRITE_BARRIER:
  215. case BLKIF_OP_FLUSH_DISKCACHE:
  216. dst->u.rw.sector_number = src->u.rw.sector_number;
  217. barrier();
  218. if (n > dst->nr_segments)
  219. n = dst->nr_segments;
  220. for (i = 0; i < n; i++)
  221. dst->u.rw.seg[i] = src->u.rw.seg[i];
  222. break;
  223. case BLKIF_OP_DISCARD:
  224. dst->u.discard.sector_number = src->u.discard.sector_number;
  225. dst->u.discard.nr_sectors = src->u.discard.nr_sectors;
  226. break;
  227. default:
  228. break;
  229. }
  230. }
  231. static inline void blkif_get_x86_64_req(struct blkif_request *dst,
  232. struct blkif_x86_64_request *src)
  233. {
  234. int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST;
  235. dst->operation = src->operation;
  236. dst->nr_segments = src->nr_segments;
  237. dst->handle = src->handle;
  238. dst->id = src->id;
  239. switch (src->operation) {
  240. case BLKIF_OP_READ:
  241. case BLKIF_OP_WRITE:
  242. case BLKIF_OP_WRITE_BARRIER:
  243. case BLKIF_OP_FLUSH_DISKCACHE:
  244. dst->u.rw.sector_number = src->u.rw.sector_number;
  245. barrier();
  246. if (n > dst->nr_segments)
  247. n = dst->nr_segments;
  248. for (i = 0; i < n; i++)
  249. dst->u.rw.seg[i] = src->u.rw.seg[i];
  250. break;
  251. case BLKIF_OP_DISCARD:
  252. dst->u.discard.sector_number = src->u.discard.sector_number;
  253. dst->u.discard.nr_sectors = src->u.discard.nr_sectors;
  254. break;
  255. default:
  256. break;
  257. }
  258. }
  259. #endif /* __XEN_BLKIF__BACKEND__COMMON_H__ */