common.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491
  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 requests that we can fit in a ring */
  211. #define XEN_BLKIF_REQS_PER_PAGE 32
  212. struct persistent_gnt {
  213. struct page *page;
  214. grant_ref_t gnt;
  215. grant_handle_t handle;
  216. unsigned long last_used;
  217. bool active;
  218. struct rb_node node;
  219. struct list_head remove_node;
  220. };
  221. /* Per-ring information. */
  222. struct xen_blkif_ring {
  223. /* Physical parameters of the comms window. */
  224. unsigned int irq;
  225. union blkif_back_rings blk_rings;
  226. void *blk_ring;
  227. /* Private fields. */
  228. spinlock_t blk_ring_lock;
  229. wait_queue_head_t wq;
  230. atomic_t inflight;
  231. bool active;
  232. /* One thread per blkif ring. */
  233. struct task_struct *xenblkd;
  234. unsigned int waiting_reqs;
  235. /* List of all 'pending_req' available */
  236. struct list_head pending_free;
  237. /* And its spinlock. */
  238. spinlock_t pending_free_lock;
  239. wait_queue_head_t pending_free_wq;
  240. /* Tree to store persistent grants. */
  241. struct rb_root persistent_gnts;
  242. unsigned int persistent_gnt_c;
  243. atomic_t persistent_gnt_in_use;
  244. unsigned long next_lru;
  245. /* Statistics. */
  246. unsigned long st_print;
  247. unsigned long long st_rd_req;
  248. unsigned long long st_wr_req;
  249. unsigned long long st_oo_req;
  250. unsigned long long st_f_req;
  251. unsigned long long st_ds_req;
  252. unsigned long long st_rd_sect;
  253. unsigned long long st_wr_sect;
  254. /* Used by the kworker that offload work from the persistent purge. */
  255. struct list_head persistent_purge_list;
  256. struct work_struct persistent_purge_work;
  257. /* Buffer of free pages to map grant refs. */
  258. spinlock_t free_pages_lock;
  259. int free_pages_num;
  260. struct list_head free_pages;
  261. struct work_struct free_work;
  262. /* Thread shutdown wait queue. */
  263. wait_queue_head_t shutdown_wq;
  264. struct xen_blkif *blkif;
  265. };
  266. struct xen_blkif {
  267. /* Unique identifier for this interface. */
  268. domid_t domid;
  269. unsigned int handle;
  270. /* Comms information. */
  271. enum blkif_protocol blk_protocol;
  272. /* The VBD attached to this interface. */
  273. struct xen_vbd vbd;
  274. /* Back pointer to the backend_info. */
  275. struct backend_info *be;
  276. atomic_t refcnt;
  277. /* for barrier (drain) requests */
  278. struct completion drain_complete;
  279. atomic_t drain;
  280. struct work_struct free_work;
  281. unsigned int nr_ring_pages;
  282. /* All rings for this device. */
  283. struct xen_blkif_ring *rings;
  284. unsigned int nr_rings;
  285. };
  286. struct seg_buf {
  287. unsigned long offset;
  288. unsigned int nsec;
  289. };
  290. struct grant_page {
  291. struct page *page;
  292. struct persistent_gnt *persistent_gnt;
  293. grant_handle_t handle;
  294. grant_ref_t gref;
  295. };
  296. /*
  297. * Each outstanding request that we've passed to the lower device layers has a
  298. * 'pending_req' allocated to it. Each buffer_head that completes decrements
  299. * the pendcnt towards zero. When it hits zero, the specified domain has a
  300. * response queued for it, with the saved 'id' passed back.
  301. */
  302. struct pending_req {
  303. struct xen_blkif_ring *ring;
  304. u64 id;
  305. int nr_segs;
  306. atomic_t pendcnt;
  307. unsigned short operation;
  308. int status;
  309. struct list_head free_list;
  310. struct grant_page *segments[MAX_INDIRECT_SEGMENTS];
  311. /* Indirect descriptors */
  312. struct grant_page *indirect_pages[MAX_INDIRECT_PAGES];
  313. struct seg_buf seg[MAX_INDIRECT_SEGMENTS];
  314. struct bio *biolist[MAX_INDIRECT_SEGMENTS];
  315. struct gnttab_unmap_grant_ref unmap[MAX_INDIRECT_SEGMENTS];
  316. struct page *unmap_pages[MAX_INDIRECT_SEGMENTS];
  317. struct gntab_unmap_queue_data gnttab_unmap_data;
  318. };
  319. #define vbd_sz(_v) ((_v)->bdev->bd_part ? \
  320. (_v)->bdev->bd_part->nr_sects : \
  321. get_capacity((_v)->bdev->bd_disk))
  322. #define xen_blkif_get(_b) (atomic_inc(&(_b)->refcnt))
  323. #define xen_blkif_put(_b) \
  324. do { \
  325. if (atomic_dec_and_test(&(_b)->refcnt)) \
  326. schedule_work(&(_b)->free_work);\
  327. } while (0)
  328. struct phys_req {
  329. unsigned short dev;
  330. blkif_sector_t nr_sects;
  331. struct block_device *bdev;
  332. blkif_sector_t sector_number;
  333. };
  334. int xen_blkif_interface_init(void);
  335. int xen_blkif_xenbus_init(void);
  336. irqreturn_t xen_blkif_be_int(int irq, void *dev_id);
  337. int xen_blkif_schedule(void *arg);
  338. int xen_blkif_purge_persistent(void *arg);
  339. void xen_blkbk_free_caches(struct xen_blkif_ring *ring);
  340. int xen_blkbk_flush_diskcache(struct xenbus_transaction xbt,
  341. struct backend_info *be, int state);
  342. int xen_blkbk_barrier(struct xenbus_transaction xbt,
  343. struct backend_info *be, int state);
  344. struct xenbus_device *xen_blkbk_xenbus(struct backend_info *be);
  345. void xen_blkbk_unmap_purged_grants(struct work_struct *work);
  346. static inline void blkif_get_x86_32_req(struct blkif_request *dst,
  347. struct blkif_x86_32_request *src)
  348. {
  349. int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST, j;
  350. dst->operation = READ_ONCE(src->operation);
  351. switch (dst->operation) {
  352. case BLKIF_OP_READ:
  353. case BLKIF_OP_WRITE:
  354. case BLKIF_OP_WRITE_BARRIER:
  355. case BLKIF_OP_FLUSH_DISKCACHE:
  356. dst->u.rw.nr_segments = src->u.rw.nr_segments;
  357. dst->u.rw.handle = src->u.rw.handle;
  358. dst->u.rw.id = src->u.rw.id;
  359. dst->u.rw.sector_number = src->u.rw.sector_number;
  360. barrier();
  361. if (n > dst->u.rw.nr_segments)
  362. n = dst->u.rw.nr_segments;
  363. for (i = 0; i < n; i++)
  364. dst->u.rw.seg[i] = src->u.rw.seg[i];
  365. break;
  366. case BLKIF_OP_DISCARD:
  367. dst->u.discard.flag = src->u.discard.flag;
  368. dst->u.discard.id = src->u.discard.id;
  369. dst->u.discard.sector_number = src->u.discard.sector_number;
  370. dst->u.discard.nr_sectors = src->u.discard.nr_sectors;
  371. break;
  372. case BLKIF_OP_INDIRECT:
  373. dst->u.indirect.indirect_op = src->u.indirect.indirect_op;
  374. dst->u.indirect.nr_segments = src->u.indirect.nr_segments;
  375. dst->u.indirect.handle = src->u.indirect.handle;
  376. dst->u.indirect.id = src->u.indirect.id;
  377. dst->u.indirect.sector_number = src->u.indirect.sector_number;
  378. barrier();
  379. j = min(MAX_INDIRECT_PAGES, INDIRECT_PAGES(dst->u.indirect.nr_segments));
  380. for (i = 0; i < j; i++)
  381. dst->u.indirect.indirect_grefs[i] =
  382. src->u.indirect.indirect_grefs[i];
  383. break;
  384. default:
  385. /*
  386. * Don't know how to translate this op. Only get the
  387. * ID so failure can be reported to the frontend.
  388. */
  389. dst->u.other.id = src->u.other.id;
  390. break;
  391. }
  392. }
  393. static inline void blkif_get_x86_64_req(struct blkif_request *dst,
  394. struct blkif_x86_64_request *src)
  395. {
  396. int i, n = BLKIF_MAX_SEGMENTS_PER_REQUEST, j;
  397. dst->operation = READ_ONCE(src->operation);
  398. switch (dst->operation) {
  399. case BLKIF_OP_READ:
  400. case BLKIF_OP_WRITE:
  401. case BLKIF_OP_WRITE_BARRIER:
  402. case BLKIF_OP_FLUSH_DISKCACHE:
  403. dst->u.rw.nr_segments = src->u.rw.nr_segments;
  404. dst->u.rw.handle = src->u.rw.handle;
  405. dst->u.rw.id = src->u.rw.id;
  406. dst->u.rw.sector_number = src->u.rw.sector_number;
  407. barrier();
  408. if (n > dst->u.rw.nr_segments)
  409. n = dst->u.rw.nr_segments;
  410. for (i = 0; i < n; i++)
  411. dst->u.rw.seg[i] = src->u.rw.seg[i];
  412. break;
  413. case BLKIF_OP_DISCARD:
  414. dst->u.discard.flag = src->u.discard.flag;
  415. dst->u.discard.id = src->u.discard.id;
  416. dst->u.discard.sector_number = src->u.discard.sector_number;
  417. dst->u.discard.nr_sectors = src->u.discard.nr_sectors;
  418. break;
  419. case BLKIF_OP_INDIRECT:
  420. dst->u.indirect.indirect_op = src->u.indirect.indirect_op;
  421. dst->u.indirect.nr_segments = src->u.indirect.nr_segments;
  422. dst->u.indirect.handle = src->u.indirect.handle;
  423. dst->u.indirect.id = src->u.indirect.id;
  424. dst->u.indirect.sector_number = src->u.indirect.sector_number;
  425. barrier();
  426. j = min(MAX_INDIRECT_PAGES, INDIRECT_PAGES(dst->u.indirect.nr_segments));
  427. for (i = 0; i < j; i++)
  428. dst->u.indirect.indirect_grefs[i] =
  429. src->u.indirect.indirect_grefs[i];
  430. break;
  431. default:
  432. /*
  433. * Don't know how to translate this op. Only get the
  434. * ID so failure can be reported to the frontend.
  435. */
  436. dst->u.other.id = src->u.other.id;
  437. break;
  438. }
  439. }
  440. #endif /* __XEN_BLKIF__BACKEND__COMMON_H__ */