common.h 17 KB

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