common.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. /* for barrier (drain) requests */
  164. struct completion drain_complete;
  165. atomic_t drain;
  166. /* One thread per one blkif. */
  167. struct task_struct *xenblkd;
  168. unsigned int waiting_reqs;
  169. /* statistics */
  170. unsigned long st_print;
  171. int st_rd_req;
  172. int st_wr_req;
  173. int st_oo_req;
  174. int st_f_req;
  175. int st_ds_req;
  176. int st_rd_sect;
  177. int st_wr_sect;
  178. wait_queue_head_t waiting_to_free;
  179. grant_handle_t shmem_handle;
  180. grant_ref_t shmem_ref;
  181. };
  182. #define vbd_sz(_v) ((_v)->bdev->bd_part ? \
  183. (_v)->bdev->bd_part->nr_sects : \
  184. get_capacity((_v)->bdev->bd_disk))
  185. #define xen_blkif_get(_b) (atomic_inc(&(_b)->refcnt))
  186. #define xen_blkif_put(_b) \
  187. do { \
  188. if (atomic_dec_and_test(&(_b)->refcnt)) \
  189. wake_up(&(_b)->waiting_to_free);\
  190. } while (0)
  191. struct phys_req {
  192. unsigned short dev;
  193. blkif_sector_t nr_sects;
  194. struct block_device *bdev;
  195. blkif_sector_t sector_number;
  196. };
  197. int xen_blkif_interface_init(void);
  198. int xen_blkif_xenbus_init(void);
  199. irqreturn_t xen_blkif_be_int(int irq, void *dev_id);
  200. int xen_blkif_schedule(void *arg);
  201. int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt,
  202. struct backend_info *be, int state);
  203. int xen_blkbk_barrier(struct xenbus_transaction xbt,
  204. struct backend_info *be, int state);
  205. struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be);
  206. static inline void blkif_get_x86_32_req(struct blkif_request *dst,
  207. struct blkif_x86_32_request *src)
  208. {
  209. int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST;
  210. dst->operation = src->operation;
  211. dst->nr_segments = src->nr_segments;
  212. dst->handle = src->handle;
  213. dst->id = src->id;
  214. switch (src->operation) {
  215. case BLKIF_OP_READ:
  216. case BLKIF_OP_WRITE:
  217. case BLKIF_OP_WRITE_BARRIER:
  218. case BLKIF_OP_FLUSH_DISKCACHE:
  219. dst->u.rw.sector_number = src->u.rw.sector_number;
  220. barrier();
  221. if (n > dst->nr_segments)
  222. n = dst->nr_segments;
  223. for (i = 0; i < n; i++)
  224. dst->u.rw.seg[i] = src->u.rw.seg[i];
  225. break;
  226. case BLKIF_OP_DISCARD:
  227. dst->u.discard.sector_number = src->u.discard.sector_number;
  228. dst->u.discard.nr_sectors = src->u.discard.nr_sectors;
  229. break;
  230. default:
  231. break;
  232. }
  233. }
  234. static inline void blkif_get_x86_64_req(struct blkif_request *dst,
  235. struct blkif_x86_64_request *src)
  236. {
  237. int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST;
  238. dst->operation = src->operation;
  239. dst->nr_segments = src->nr_segments;
  240. dst->handle = src->handle;
  241. dst->id = src->id;
  242. switch (src->operation) {
  243. case BLKIF_OP_READ:
  244. case BLKIF_OP_WRITE:
  245. case BLKIF_OP_WRITE_BARRIER:
  246. case BLKIF_OP_FLUSH_DISKCACHE:
  247. dst->u.rw.sector_number = src->u.rw.sector_number;
  248. barrier();
  249. if (n > dst->nr_segments)
  250. n = dst->nr_segments;
  251. for (i = 0; i < n; i++)
  252. dst->u.rw.seg[i] = src->u.rw.seg[i];
  253. break;
  254. case BLKIF_OP_DISCARD:
  255. dst->u.discard.sector_number = src->u.discard.sector_number;
  256. dst->u.discard.nr_sectors = src->u.discard.nr_sectors;
  257. break;
  258. default:
  259. break;
  260. }
  261. }
  262. #endif /* __XEN_BLKIF__BACKEND__COMMON_H__ */