common.h 11 KB

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