common.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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/page.h>
  41. #include <xen/xenbus.h>
  42. #include <xen/interface/io/ring.h>
  43. #include <xen/interface/io/blkif.h>
  44. #include <xen/interface/io/protocols.h>
  45. extern unsigned int xen_blkif_max_ring_order;
  46. extern unsigned int xenblk_max_queues;
  47. /*
  48. * This is the maximum number of segments that would be allowed in indirect
  49. * requests. This value will also be passed to the frontend.
  50. */
  51. #define MAX_INDIRECT_SEGMENTS 256
  52. /*
  53. * Xen use 4K pages. The guest may use different page size (4K or 64K)
  54. * Number of Xen pages per segment
  55. */
  56. #define XEN_PAGES_PER_SEGMENT (PAGE_SIZE / XEN_PAGE_SIZE)
  57. #define XEN_PAGES_PER_INDIRECT_FRAME \
  58. (XEN_PAGE_SIZE/sizeof(struct blkif_request_segment))
  59. #define SEGS_PER_INDIRECT_FRAME \
  60. (XEN_PAGES_PER_INDIRECT_FRAME / XEN_PAGES_PER_SEGMENT)
  61. #define MAX_INDIRECT_PAGES \
  62. ((MAX_INDIRECT_SEGMENTS + SEGS_PER_INDIRECT_FRAME - 1)/SEGS_PER_INDIRECT_FRAME)
  63. #define INDIRECT_PAGES(_segs) DIV_ROUND_UP(_segs, XEN_PAGES_PER_INDIRECT_FRAME)
  64. /* Not a real protocol. Used to generate ring structs which contain
  65. * the elements common to all protocols only. This way we get a
  66. * compiler-checkable way to use common struct elements, so we can
  67. * avoid using switch(protocol) in a number of places. */
  68. struct blkif_common_request {
  69. char dummy;
  70. };
  71. /* i386 protocol version */
  72. struct blkif_x86_32_request_rw {
  73. uint8_t nr_segments; /* number of segments */
  74. blkif_vdev_t handle; /* only for read/write requests */
  75. uint64_t id; /* private guest value, echoed in resp */
  76. blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */
  77. struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  78. } __attribute__((__packed__));
  79. struct blkif_x86_32_request_discard {
  80. uint8_t flag; /* BLKIF_DISCARD_SECURE or zero */
  81. blkif_vdev_t _pad1; /* was "handle" for read/write requests */
  82. uint64_t id; /* private guest value, echoed in resp */
  83. blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */
  84. uint64_t nr_sectors;
  85. } __attribute__((__packed__));
  86. struct blkif_x86_32_request_other {
  87. uint8_t _pad1;
  88. blkif_vdev_t _pad2;
  89. uint64_t id; /* private guest value, echoed in resp */
  90. } __attribute__((__packed__));
  91. struct blkif_x86_32_request_indirect {
  92. uint8_t indirect_op;
  93. uint16_t nr_segments;
  94. uint64_t id;
  95. blkif_sector_t sector_number;
  96. blkif_vdev_t handle;
  97. uint16_t _pad1;
  98. grant_ref_t indirect_grefs[BLKIF_MAX_INDIRECT_PAGES_PER_REQUEST];
  99. /*
  100. * The maximum number of indirect segments (and pages) that will
  101. * be used is determined by MAX_INDIRECT_SEGMENTS, this value
  102. * is also exported to the guest (via xenstore
  103. * feature-max-indirect-segments entry), so the frontend knows how
  104. * many indirect segments the backend supports.
  105. */
  106. uint64_t _pad2; /* make it 64 byte aligned */
  107. } __attribute__((__packed__));
  108. struct blkif_x86_32_request {
  109. uint8_t operation; /* BLKIF_OP_??? */
  110. union {
  111. struct blkif_x86_32_request_rw rw;
  112. struct blkif_x86_32_request_discard discard;
  113. struct blkif_x86_32_request_other other;
  114. struct blkif_x86_32_request_indirect indirect;
  115. } u;
  116. } __attribute__((__packed__));
  117. /* x86_64 protocol version */
  118. struct blkif_x86_64_request_rw {
  119. uint8_t nr_segments; /* number of segments */
  120. blkif_vdev_t handle; /* only for read/write requests */
  121. uint32_t _pad1; /* offsetof(blkif_reqest..,u.rw.id)==8 */
  122. uint64_t id;
  123. blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */
  124. struct blkif_request_segment seg[BLKIF_MAX_SEGMENTS_PER_REQUEST];
  125. } __attribute__((__packed__));
  126. struct blkif_x86_64_request_discard {
  127. uint8_t flag; /* BLKIF_DISCARD_SECURE or zero */
  128. blkif_vdev_t _pad1; /* was "handle" for read/write requests */
  129. uint32_t _pad2; /* offsetof(blkif_..,u.discard.id)==8 */
  130. uint64_t id;
  131. blkif_sector_t sector_number;/* start sector idx on disk (r/w only) */
  132. uint64_t nr_sectors;
  133. } __attribute__((__packed__));
  134. struct blkif_x86_64_request_other {
  135. uint8_t _pad1;
  136. blkif_vdev_t _pad2;
  137. uint32_t _pad3; /* offsetof(blkif_..,u.discard.id)==8 */
  138. uint64_t id; /* private guest value, echoed in resp */
  139. } __attribute__((__packed__));
  140. struct blkif_x86_64_request_indirect {
  141. uint8_t indirect_op;
  142. uint16_t nr_segments;
  143. uint32_t _pad1; /* offsetof(blkif_..,u.indirect.id)==8 */
  144. uint64_t id;
  145. blkif_sector_t sector_number;
  146. blkif_vdev_t handle;
  147. uint16_t _pad2;
  148. grant_ref_t indirect_grefs[BLKIF_MAX_INDIRECT_PAGES_PER_REQUEST];
  149. /*
  150. * The maximum number of indirect segments (and pages) that will
  151. * be used is determined by MAX_INDIRECT_SEGMENTS, this value
  152. * is also exported to the guest (via xenstore
  153. * feature-max-indirect-segments entry), so the frontend knows how
  154. * many indirect segments the backend supports.
  155. */
  156. uint32_t _pad3; /* make it 64 byte aligned */
  157. } __attribute__((__packed__));
  158. struct blkif_x86_64_request {
  159. uint8_t operation; /* BLKIF_OP_??? */
  160. union {
  161. struct blkif_x86_64_request_rw rw;
  162. struct blkif_x86_64_request_discard discard;
  163. struct blkif_x86_64_request_other other;
  164. struct blkif_x86_64_request_indirect indirect;
  165. } u;
  166. } __attribute__((__packed__));
  167. DEFINE_RING_TYPES(blkif_common, struct blkif_common_request,
  168. struct blkif_response);
  169. DEFINE_RING_TYPES(blkif_x86_32, struct blkif_x86_32_request,
  170. struct blkif_response __packed);
  171. DEFINE_RING_TYPES(blkif_x86_64, struct blkif_x86_64_request,
  172. struct blkif_response);
  173. union blkif_back_rings {
  174. struct blkif_back_ring native;
  175. struct blkif_common_back_ring common;
  176. struct blkif_x86_32_back_ring x86_32;
  177. struct blkif_x86_64_back_ring x86_64;
  178. };
  179. enum blkif_protocol {
  180. BLKIF_PROTOCOL_NATIVE = 1,
  181. BLKIF_PROTOCOL_X86_32 = 2,
  182. BLKIF_PROTOCOL_X86_64 = 3,
  183. };
  184. /*
  185. * Default protocol if the frontend doesn't specify one.
  186. */
  187. #ifdef CONFIG_X86
  188. # define BLKIF_PROTOCOL_DEFAULT BLKIF_PROTOCOL_X86_32
  189. #else
  190. # define BLKIF_PROTOCOL_DEFAULT BLKIF_PROTOCOL_NATIVE
  191. #endif
  192. struct xen_vbd {
  193. /* What the domain refers to this vbd as. */
  194. blkif_vdev_t handle;
  195. /* Non-zero -> read-only */
  196. unsigned char readonly;
  197. /* VDISK_xxx */
  198. unsigned char type;
  199. /* phys device that this vbd maps to. */
  200. u32 pdevice;
  201. struct block_device *bdev;
  202. /* Cached size parameter. */
  203. sector_t size;
  204. unsigned int flush_support:1;
  205. unsigned int discard_secure:1;
  206. unsigned int feature_gnt_persistent:1;
  207. unsigned int overflow_max_grants:1;
  208. };
  209. struct backend_info;
  210. /* Number of available flags */
  211. #define PERSISTENT_GNT_FLAGS_SIZE 2
  212. /* This persistent grant is currently in use */
  213. #define PERSISTENT_GNT_ACTIVE 0
  214. /*
  215. * This persistent grant has been used, this flag is set when we remove the
  216. * PERSISTENT_GNT_ACTIVE, to know that this grant has been used recently.
  217. */
  218. #define PERSISTENT_GNT_WAS_ACTIVE 1
  219. /* Number of requests that we can fit in a ring */
  220. #define XEN_BLKIF_REQS_PER_PAGE 32
  221. struct persistent_gnt {
  222. struct page *page;
  223. grant_ref_t gnt;
  224. grant_handle_t handle;
  225. DECLARE_BITMAP(flags, PERSISTENT_GNT_FLAGS_SIZE);
  226. struct rb_node node;
  227. struct list_head remove_node;
  228. };
  229. /* Per-ring information. */
  230. struct xen_blkif_ring {
  231. /* Physical parameters of the comms window. */
  232. unsigned int irq;
  233. union blkif_back_rings blk_rings;
  234. void *blk_ring;
  235. /* Private fields. */
  236. spinlock_t blk_ring_lock;
  237. wait_queue_head_t wq;
  238. atomic_t inflight;
  239. bool active;
  240. /* One thread per blkif ring. */
  241. struct task_struct *xenblkd;
  242. unsigned int waiting_reqs;
  243. /* List of all 'pending_req' available */
  244. struct list_head pending_free;
  245. /* And its spinlock. */
  246. spinlock_t pending_free_lock;
  247. wait_queue_head_t pending_free_wq;
  248. /* Tree to store persistent grants. */
  249. spinlock_t pers_gnts_lock;
  250. struct rb_root persistent_gnts;
  251. unsigned int persistent_gnt_c;
  252. atomic_t persistent_gnt_in_use;
  253. unsigned long next_lru;
  254. /* Statistics. */
  255. unsigned long st_print;
  256. unsigned long long st_rd_req;
  257. unsigned long long st_wr_req;
  258. unsigned long long st_oo_req;
  259. unsigned long long st_f_req;
  260. unsigned long long st_ds_req;
  261. unsigned long long st_rd_sect;
  262. unsigned long long st_wr_sect;
  263. /* Used by the kworker that offload work from the persistent purge. */
  264. struct list_head persistent_purge_list;
  265. struct work_struct persistent_purge_work;
  266. /* Buffer of free pages to map grant refs. */
  267. spinlock_t free_pages_lock;
  268. int free_pages_num;
  269. struct list_head free_pages;
  270. struct work_struct free_work;
  271. /* Thread shutdown wait queue. */
  272. wait_queue_head_t shutdown_wq;
  273. struct xen_blkif *blkif;
  274. };
  275. struct xen_blkif {
  276. /* Unique identifier for this interface. */
  277. domid_t domid;
  278. unsigned int handle;
  279. /* Comms information. */
  280. enum blkif_protocol blk_protocol;
  281. /* The VBD attached to this interface. */
  282. struct xen_vbd vbd;
  283. /* Back pointer to the backend_info. */
  284. struct backend_info *be;
  285. atomic_t refcnt;
  286. /* for barrier (drain) requests */
  287. struct completion drain_complete;
  288. atomic_t drain;
  289. struct work_struct free_work;
  290. unsigned int nr_ring_pages;
  291. /* All rings for this device. */
  292. struct xen_blkif_ring *rings;
  293. unsigned int nr_rings;
  294. };
  295. struct seg_buf {
  296. unsigned long offset;
  297. unsigned int nsec;
  298. };
  299. struct grant_page {
  300. struct page *page;
  301. struct persistent_gnt *persistent_gnt;
  302. grant_handle_t handle;
  303. grant_ref_t gref;
  304. };
  305. /*
  306. * Each outstanding request that we've passed to the lower device layers has a
  307. * 'pending_req' allocated to it. Each buffer_head that completes decrements
  308. * the pendcnt towards zero. When it hits zero, the specified domain has a
  309. * response queued for it, with the saved 'id' passed back.
  310. */
  311. struct pending_req {
  312. struct xen_blkif_ring *ring;
  313. u64 id;
  314. int nr_segs;
  315. atomic_t pendcnt;
  316. unsigned short operation;
  317. int status;
  318. struct list_head free_list;
  319. struct grant_page *segments[MAX_INDIRECT_SEGMENTS];
  320. /* Indirect descriptors */
  321. struct grant_page *indirect_pages[MAX_INDIRECT_PAGES];
  322. struct seg_buf seg[MAX_INDIRECT_SEGMENTS];
  323. struct bio *biolist[MAX_INDIRECT_SEGMENTS];
  324. struct gnttab_unmap_grant_ref unmap[MAX_INDIRECT_SEGMENTS];
  325. struct page *unmap_pages[MAX_INDIRECT_SEGMENTS];
  326. struct gntab_unmap_queue_data gnttab_unmap_data;
  327. };
  328. #define vbd_sz(_v) ((_v)->bdev->bd_part ? \
  329. (_v)->bdev->bd_part->nr_sects : \
  330. get_capacity((_v)->bdev->bd_disk))
  331. #define xen_blkif_get(_b) (atomic_inc(&(_b)->refcnt))
  332. #define xen_blkif_put(_b) \
  333. do { \
  334. if (atomic_dec_and_test(&(_b)->refcnt)) \
  335. schedule_work(&(_b)->free_work);\
  336. } while (0)
  337. struct phys_req {
  338. unsigned short dev;
  339. blkif_sector_t nr_sects;
  340. struct block_device *bdev;
  341. blkif_sector_t sector_number;
  342. };
  343. int xen_blkif_interface_init(void);
  344. int xen_blkif_xenbus_init(void);
  345. irqreturn_t xen_blkif_be_int(int irq, void *dev_id);
  346. int xen_blkif_schedule(void *arg);
  347. int xen_blkif_purge_persistent(void *arg);
  348. void xen_blkbk_free_caches(struct xen_blkif_ring *ring);
  349. int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt,
  350. struct backend_info *be, int state);
  351. int xen_blkbk_barrier(struct xenbus_transaction xbt,
  352. struct backend_info *be, int state);
  353. struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be);
  354. void xen_blkbk_unmap_purged_grants(struct work_struct *work);
  355. static inline void blkif_get_x86_32_req(struct blkif_request *dst,
  356. struct blkif_x86_32_request *src)
  357. {
  358. int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST, j;
  359. dst->operation = READ_ONCE(src->operation);
  360. switch (dst->operation) {
  361. case BLKIF_OP_READ:
  362. case BLKIF_OP_WRITE:
  363. case BLKIF_OP_WRITE_BARRIER:
  364. case BLKIF_OP_FLUSH_DISKCACHE:
  365. dst->u.rw.nr_segments = src->u.rw.nr_segments;
  366. dst->u.rw.handle = src->u.rw.handle;
  367. dst->u.rw.id = src->u.rw.id;
  368. dst->u.rw.sector_number = src->u.rw.sector_number;
  369. barrier();
  370. if (n > dst->u.rw.nr_segments)
  371. n = dst->u.rw.nr_segments;
  372. for (i = 0; i < n; i++)
  373. dst->u.rw.seg[i] = src->u.rw.seg[i];
  374. break;
  375. case BLKIF_OP_DISCARD:
  376. dst->u.discard.flag = src->u.discard.flag;
  377. dst->u.discard.id = src->u.discard.id;
  378. dst->u.discard.sector_number = src->u.discard.sector_number;
  379. dst->u.discard.nr_sectors = src->u.discard.nr_sectors;
  380. break;
  381. case BLKIF_OP_INDIRECT:
  382. dst->u.indirect.indirect_op = src->u.indirect.indirect_op;
  383. dst->u.indirect.nr_segments = src->u.indirect.nr_segments;
  384. dst->u.indirect.handle = src->u.indirect.handle;
  385. dst->u.indirect.id = src->u.indirect.id;
  386. dst->u.indirect.sector_number = src->u.indirect.sector_number;
  387. barrier();
  388. j = min(MAX_INDIRECT_PAGES, INDIRECT_PAGES(dst->u.indirect.nr_segments));
  389. for (i = 0; i < j; i++)
  390. dst->u.indirect.indirect_grefs[i] =
  391. src->u.indirect.indirect_grefs[i];
  392. break;
  393. default:
  394. /*
  395. * Don't know how to translate this op. Only get the
  396. * ID so failure can be reported to the frontend.
  397. */
  398. dst->u.other.id = src->u.other.id;
  399. break;
  400. }
  401. }
  402. static inline void blkif_get_x86_64_req(struct blkif_request *dst,
  403. struct blkif_x86_64_request *src)
  404. {
  405. int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST, j;
  406. dst->operation = READ_ONCE(src->operation);
  407. switch (dst->operation) {
  408. case BLKIF_OP_READ:
  409. case BLKIF_OP_WRITE:
  410. case BLKIF_OP_WRITE_BARRIER:
  411. case BLKIF_OP_FLUSH_DISKCACHE:
  412. dst->u.rw.nr_segments = src->u.rw.nr_segments;
  413. dst->u.rw.handle = src->u.rw.handle;
  414. dst->u.rw.id = src->u.rw.id;
  415. dst->u.rw.sector_number = src->u.rw.sector_number;
  416. barrier();
  417. if (n > dst->u.rw.nr_segments)
  418. n = dst->u.rw.nr_segments;
  419. for (i = 0; i < n; i++)
  420. dst->u.rw.seg[i] = src->u.rw.seg[i];
  421. break;
  422. case BLKIF_OP_DISCARD:
  423. dst->u.discard.flag = src->u.discard.flag;
  424. dst->u.discard.id = src->u.discard.id;
  425. dst->u.discard.sector_number = src->u.discard.sector_number;
  426. dst->u.discard.nr_sectors = src->u.discard.nr_sectors;
  427. break;
  428. case BLKIF_OP_INDIRECT:
  429. dst->u.indirect.indirect_op = src->u.indirect.indirect_op;
  430. dst->u.indirect.nr_segments = src->u.indirect.nr_segments;
  431. dst->u.indirect.handle = src->u.indirect.handle;
  432. dst->u.indirect.id = src->u.indirect.id;
  433. dst->u.indirect.sector_number = src->u.indirect.sector_number;
  434. barrier();
  435. j = min(MAX_INDIRECT_PAGES, INDIRECT_PAGES(dst->u.indirect.nr_segments));
  436. for (i = 0; i < j; i++)
  437. dst->u.indirect.indirect_grefs[i] =
  438. src->u.indirect.indirect_grefs[i];
  439. break;
  440. default:
  441. /*
  442. * Don't know how to translate this op. Only get the
  443. * ID so failure can be reported to the frontend.
  444. */
  445. dst->u.other.id = src->u.other.id;
  446. break;
  447. }
  448. }
  449. #endif /* __XEN_BLKIF__BACKEND__COMMON_H__ */