common.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. /* Number of available flags */
  165. #define PERSISTENT_GNT_FLAGS_SIZE 2
  166. /* This persistent grant is currently in use */
  167. #define PERSISTENT_GNT_ACTIVE 0
  168. /*
  169. * This persistent grant has been used, this flag is set when we remove the
  170. * PERSISTENT_GNT_ACTIVE, to know that this grant has been used recently.
  171. */
  172. #define PERSISTENT_GNT_WAS_ACTIVE 1
  173. /* Number of requests that we can fit in a ring */
  174. #define XEN_BLKIF_REQS 32
  175. struct persistent_gnt {
  176. struct page *page;
  177. grant_ref_t gnt;
  178. grant_handle_t handle;
  179. DECLARE_BITMAP(flags, PERSISTENT_GNT_FLAGS_SIZE);
  180. struct rb_node node;
  181. struct list_head remove_node;
  182. };
  183. struct xen_blkif {
  184. /* Unique identifier for this interface. */
  185. domid_t domid;
  186. unsigned int handle;
  187. /* Physical parameters of the comms window. */
  188. unsigned int irq;
  189. /* Comms information. */
  190. enum blkif_protocol blk_protocol;
  191. union blkif_back_rings blk_rings;
  192. void *blk_ring;
  193. /* The VBD attached to this interface. */
  194. struct xen_vbd vbd;
  195. /* Back pointer to the backend_info. */
  196. struct backend_info *be;
  197. /* Private fields. */
  198. spinlock_t blk_ring_lock;
  199. atomic_t refcnt;
  200. wait_queue_head_t wq;
  201. /* for barrier (drain) requests */
  202. struct completion drain_complete;
  203. atomic_t drain;
  204. /* One thread per one blkif. */
  205. struct task_struct *xenblkd;
  206. unsigned int waiting_reqs;
  207. /* tree to store persistent grants */
  208. struct rb_root persistent_gnts;
  209. unsigned int persistent_gnt_c;
  210. atomic_t persistent_gnt_in_use;
  211. unsigned long next_lru;
  212. /* used by the kworker that offload work from the persistent purge */
  213. struct list_head persistent_purge_list;
  214. struct work_struct persistent_purge_work;
  215. /* buffer of free pages to map grant refs */
  216. spinlock_t free_pages_lock;
  217. int free_pages_num;
  218. struct list_head free_pages;
  219. /* Allocation of pending_reqs */
  220. struct pending_req *pending_reqs;
  221. /* List of all 'pending_req' available */
  222. struct list_head pending_free;
  223. /* And its spinlock. */
  224. spinlock_t pending_free_lock;
  225. wait_queue_head_t pending_free_wq;
  226. /* statistics */
  227. unsigned long st_print;
  228. unsigned long long st_rd_req;
  229. unsigned long long st_wr_req;
  230. unsigned long long st_oo_req;
  231. unsigned long long st_f_req;
  232. unsigned long long st_ds_req;
  233. unsigned long long st_rd_sect;
  234. unsigned long long st_wr_sect;
  235. wait_queue_head_t waiting_to_free;
  236. };
  237. /*
  238. * Each outstanding request that we've passed to the lower device layers has a
  239. * 'pending_req' allocated to it. Each buffer_head that completes decrements
  240. * the pendcnt towards zero. When it hits zero, the specified domain has a
  241. * response queued for it, with the saved 'id' passed back.
  242. */
  243. struct pending_req {
  244. struct xen_blkif *blkif;
  245. u64 id;
  246. int nr_pages;
  247. atomic_t pendcnt;
  248. unsigned short operation;
  249. int status;
  250. struct list_head free_list;
  251. struct page *pages[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  252. struct persistent_gnt *persistent_gnts[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  253. grant_handle_t grant_handles[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  254. };
  255. #define vbd_sz(_v) ((_v)->bdev->bd_part ? \
  256. (_v)->bdev->bd_part->nr_sects : \
  257. get_capacity((_v)->bdev->bd_disk))
  258. #define xen_blkif_get(_b) (atomic_inc(&(_b)->refcnt))
  259. #define xen_blkif_put(_b) \
  260. do { \
  261. if (atomic_dec_and_test(&(_b)->refcnt)) \
  262. wake_up(&(_b)->waiting_to_free);\
  263. } while (0)
  264. struct phys_req {
  265. unsigned short dev;
  266. blkif_sector_t nr_sects;
  267. struct block_device *bdev;
  268. blkif_sector_t sector_number;
  269. };
  270. int xen_blkif_interface_init(void);
  271. int xen_blkif_xenbus_init(void);
  272. irqreturn_t xen_blkif_be_int(int irq, void *dev_id);
  273. int xen_blkif_schedule(void *arg);
  274. int xen_blkif_purge_persistent(void *arg);
  275. int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt,
  276. struct backend_info *be, int state);
  277. int xen_blkbk_barrier(struct xenbus_transaction xbt,
  278. struct backend_info *be, int state);
  279. struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be);
  280. static inline void blkif_get_x86_32_req(struct blkif_request *dst,
  281. struct blkif_x86_32_request *src)
  282. {
  283. int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST;
  284. dst->operation = src->operation;
  285. switch (src->operation) {
  286. case BLKIF_OP_READ:
  287. case BLKIF_OP_WRITE:
  288. case BLKIF_OP_WRITE_BARRIER:
  289. case BLKIF_OP_FLUSH_DISKCACHE:
  290. dst->u.rw.nr_segments = src->u.rw.nr_segments;
  291. dst->u.rw.handle = src->u.rw.handle;
  292. dst->u.rw.id = src->u.rw.id;
  293. dst->u.rw.sector_number = src->u.rw.sector_number;
  294. barrier();
  295. if (n > dst->u.rw.nr_segments)
  296. n = dst->u.rw.nr_segments;
  297. for (i = 0; i < n; i++)
  298. dst->u.rw.seg[i] = src->u.rw.seg[i];
  299. break;
  300. case BLKIF_OP_DISCARD:
  301. dst->u.discard.flag = src->u.discard.flag;
  302. dst->u.discard.id = src->u.discard.id;
  303. dst->u.discard.sector_number = src->u.discard.sector_number;
  304. dst->u.discard.nr_sectors = src->u.discard.nr_sectors;
  305. break;
  306. default:
  307. /*
  308. * Don't know how to translate this op. Only get the
  309. * ID so failure can be reported to the frontend.
  310. */
  311. dst->u.other.id = src->u.other.id;
  312. break;
  313. }
  314. }
  315. static inline void blkif_get_x86_64_req(struct blkif_request *dst,
  316. struct blkif_x86_64_request *src)
  317. {
  318. int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST;
  319. dst->operation = src->operation;
  320. switch (src->operation) {
  321. case BLKIF_OP_READ:
  322. case BLKIF_OP_WRITE:
  323. case BLKIF_OP_WRITE_BARRIER:
  324. case BLKIF_OP_FLUSH_DISKCACHE:
  325. dst->u.rw.nr_segments = src->u.rw.nr_segments;
  326. dst->u.rw.handle = src->u.rw.handle;
  327. dst->u.rw.id = src->u.rw.id;
  328. dst->u.rw.sector_number = src->u.rw.sector_number;
  329. barrier();
  330. if (n > dst->u.rw.nr_segments)
  331. n = dst->u.rw.nr_segments;
  332. for (i = 0; i < n; i++)
  333. dst->u.rw.seg[i] = src->u.rw.seg[i];
  334. break;
  335. case BLKIF_OP_DISCARD:
  336. dst->u.discard.flag = src->u.discard.flag;
  337. dst->u.discard.id = src->u.discard.id;
  338. dst->u.discard.sector_number = src->u.discard.sector_number;
  339. dst->u.discard.nr_sectors = src->u.discard.nr_sectors;
  340. break;
  341. default:
  342. /*
  343. * Don't know how to translate this op. Only get the
  344. * ID so failure can be reported to the frontend.
  345. */
  346. dst->u.other.id = src->u.other.id;
  347. break;
  348. }
  349. }
  350. #endif /* __XEN_BLKIF__BACKEND__COMMON_H__ */