common.h 9.8 KB

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