common.h 9.3 KB

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