common.h 16 KB

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