common.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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. /* buffer of free pages to map grant refs */
  198. spinlock_t free_pages_lock;
  199. int free_pages_num;
  200. struct list_head free_pages;
  201. /* statistics */
  202. unsigned long st_print;
  203. unsigned long long st_rd_req;
  204. unsigned long long st_wr_req;
  205. unsigned long long st_oo_req;
  206. unsigned long long st_f_req;
  207. unsigned long long st_ds_req;
  208. unsigned long long st_rd_sect;
  209. unsigned long long st_wr_sect;
  210. wait_queue_head_t waiting_to_free;
  211. };
  212. #define vbd_sz(_v) ((_v)->bdev->bd_part ? \
  213. (_v)->bdev->bd_part->nr_sects : \
  214. get_capacity((_v)->bdev->bd_disk))
  215. #define xen_blkif_get(_b) (atomic_inc(&(_b)->refcnt))
  216. #define xen_blkif_put(_b) \
  217. do { \
  218. if (atomic_dec_and_test(&(_b)->refcnt)) \
  219. wake_up(&(_b)->waiting_to_free);\
  220. } while (0)
  221. struct phys_req {
  222. unsigned short dev;
  223. blkif_sector_t nr_sects;
  224. struct block_device *bdev;
  225. blkif_sector_t sector_number;
  226. };
  227. int xen_blkif_interface_init(void);
  228. int xen_blkif_xenbus_init(void);
  229. irqreturn_t xen_blkif_be_int(int irq, void *dev_id);
  230. int xen_blkif_schedule(void *arg);
  231. int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt,
  232. struct backend_info *be, int state);
  233. int xen_blkbk_barrier(struct xenbus_transaction xbt,
  234. struct backend_info *be, int state);
  235. struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be);
  236. static inline void blkif_get_x86_32_req(struct blkif_request *dst,
  237. struct blkif_x86_32_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.id = src->u.discard.id;
  259. dst->u.discard.sector_number = src->u.discard.sector_number;
  260. dst->u.discard.nr_sectors = src->u.discard.nr_sectors;
  261. break;
  262. default:
  263. /*
  264. * Don't know how to translate this op. Only get the
  265. * ID so failure can be reported to the frontend.
  266. */
  267. dst->u.other.id = src->u.other.id;
  268. break;
  269. }
  270. }
  271. static inline void blkif_get_x86_64_req(struct blkif_request *dst,
  272. struct blkif_x86_64_request *src)
  273. {
  274. int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST;
  275. dst->operation = src->operation;
  276. switch (src->operation) {
  277. case BLKIF_OP_READ:
  278. case BLKIF_OP_WRITE:
  279. case BLKIF_OP_WRITE_BARRIER:
  280. case BLKIF_OP_FLUSH_DISKCACHE:
  281. dst->u.rw.nr_segments = src->u.rw.nr_segments;
  282. dst->u.rw.handle = src->u.rw.handle;
  283. dst->u.rw.id = src->u.rw.id;
  284. dst->u.rw.sector_number = src->u.rw.sector_number;
  285. barrier();
  286. if (n > dst->u.rw.nr_segments)
  287. n = dst->u.rw.nr_segments;
  288. for (i = 0; i < n; i++)
  289. dst->u.rw.seg[i] = src->u.rw.seg[i];
  290. break;
  291. case BLKIF_OP_DISCARD:
  292. dst->u.discard.flag = src->u.discard.flag;
  293. dst->u.discard.id = src->u.discard.id;
  294. dst->u.discard.sector_number = src->u.discard.sector_number;
  295. dst->u.discard.nr_sectors = src->u.discard.nr_sectors;
  296. break;
  297. default:
  298. /*
  299. * Don't know how to translate this op. Only get the
  300. * ID so failure can be reported to the frontend.
  301. */
  302. dst->u.other.id = src->u.other.id;
  303. break;
  304. }
  305. }
  306. #endif /* __XEN_BLKIF__BACKEND__COMMON_H__ */