common.h 10 KB

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