virtio_blk.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /* SPDX-License-Identifier: BSD-3-Clause */
  2. /*
  3. * Copyright (C) 2018, Tuomas Tynkkynen <tuomas.tynkkynen@iki.fi>
  4. * Copyright (C) 2018, Bin Meng <bmeng.cn@gmail.com>
  5. *
  6. * From Linux kernel include/uapi/linux/virtio_blk.h
  7. */
  8. #ifndef _LINUX_VIRTIO_BLK_H
  9. #define _LINUX_VIRTIO_BLK_H
  10. /* Feature bits */
  11. #define VIRTIO_BLK_F_SIZE_MAX 1 /* Indicates maximum segment size */
  12. #define VIRTIO_BLK_F_SEG_MAX 2 /* Indicates maximum # of segments */
  13. #define VIRTIO_BLK_F_GEOMETRY 4 /* Legacy geometry available */
  14. #define VIRTIO_BLK_F_RO 5 /* Disk is read-only */
  15. #define VIRTIO_BLK_F_BLK_SIZE 6 /* Block size of disk is available */
  16. #define VIRTIO_BLK_F_TOPOLOGY 10 /* Topology information is available */
  17. #define VIRTIO_BLK_F_MQ 12 /* Support more than one vq */
  18. /* Legacy feature bits */
  19. #ifndef VIRTIO_BLK_NO_LEGACY
  20. #define VIRTIO_BLK_F_BARRIER 0 /* Does host support barriers? */
  21. #define VIRTIO_BLK_F_SCSI 7 /* Supports scsi command passthru */
  22. #define VIRTIO_BLK_F_FLUSH 9 /* Flush command supported */
  23. #define VIRTIO_BLK_F_CONFIG_WCE 11 /* Writeback mode available in config */
  24. #ifndef __KERNEL__
  25. /* Old (deprecated) name for VIRTIO_BLK_F_FLUSH */
  26. #define VIRTIO_BLK_F_WCE VIRTIO_BLK_F_FLUSH
  27. #endif
  28. #endif /* !VIRTIO_BLK_NO_LEGACY */
  29. #define VIRTIO_BLK_ID_BYTES 20 /* ID string length */
  30. struct __packed virtio_blk_config {
  31. /* The capacity (in 512-byte sectors) */
  32. __u64 capacity;
  33. /* The maximum segment size (if VIRTIO_BLK_F_SIZE_MAX) */
  34. __u32 size_max;
  35. /* The maximum number of segments (if VIRTIO_BLK_F_SEG_MAX) */
  36. __u32 seg_max;
  37. /* geometry of the device (if VIRTIO_BLK_F_GEOMETRY) */
  38. struct virtio_blk_geometry {
  39. __u16 cylinders;
  40. __u8 heads;
  41. __u8 sectors;
  42. } geometry;
  43. /* block size of device (if VIRTIO_BLK_F_BLK_SIZE) */
  44. __u32 blk_size;
  45. /* the next 4 entries are guarded by VIRTIO_BLK_F_TOPOLOGY */
  46. /* exponent for physical block per logical block */
  47. __u8 physical_block_exp;
  48. /* alignment offset in logical blocks */
  49. __u8 alignment_offset;
  50. /* minimum I/O size without performance penalty in logical blocks */
  51. __u16 min_io_size;
  52. /* optimal sustained I/O size in logical blocks */
  53. __u32 opt_io_size;
  54. /* writeback mode (if VIRTIO_BLK_F_CONFIG_WCE) */
  55. __u8 wce;
  56. __u8 unused;
  57. /* number of vqs, only available when VIRTIO_BLK_F_MQ is set */
  58. __u16 num_queues;
  59. };
  60. /*
  61. * Command types
  62. *
  63. * Usage is a bit tricky as some bits are used as flags and some are not.
  64. *
  65. * Rules:
  66. * VIRTIO_BLK_T_OUT may be combined with VIRTIO_BLK_T_SCSI_CMD or
  67. * VIRTIO_BLK_T_BARRIER. VIRTIO_BLK_T_FLUSH is a command of its own
  68. * and may not be combined with any of the other flags.
  69. */
  70. /* These two define direction */
  71. #define VIRTIO_BLK_T_IN 0
  72. #define VIRTIO_BLK_T_OUT 1
  73. #ifndef VIRTIO_BLK_NO_LEGACY
  74. /* This bit says it's a scsi command, not an actual read or write */
  75. #define VIRTIO_BLK_T_SCSI_CMD 2
  76. #endif /* VIRTIO_BLK_NO_LEGACY */
  77. /* Cache flush command */
  78. #define VIRTIO_BLK_T_FLUSH 4
  79. /* Get device ID command */
  80. #define VIRTIO_BLK_T_GET_ID 8
  81. #ifndef VIRTIO_BLK_NO_LEGACY
  82. /* Barrier before this op */
  83. #define VIRTIO_BLK_T_BARRIER 0x80000000
  84. #endif /* !VIRTIO_BLK_NO_LEGACY */
  85. /*
  86. * This comes first in the read scatter-gather list.
  87. * For legacy virtio, if VIRTIO_F_ANY_LAYOUT is not negotiated,
  88. * this is the first element of the read scatter-gather list.
  89. */
  90. struct virtio_blk_outhdr {
  91. /* VIRTIO_BLK_T* */
  92. __virtio32 type;
  93. /* io priority */
  94. __virtio32 ioprio;
  95. /* Sector (ie. 512 byte offset) */
  96. __virtio64 sector;
  97. };
  98. #ifndef VIRTIO_BLK_NO_LEGACY
  99. struct virtio_scsi_inhdr {
  100. __virtio32 errors;
  101. __virtio32 data_len;
  102. __virtio32 sense_len;
  103. __virtio32 residual;
  104. };
  105. #endif /* !VIRTIO_BLK_NO_LEGACY */
  106. /* And this is the final byte of the write scatter-gather list */
  107. #define VIRTIO_BLK_S_OK 0
  108. #define VIRTIO_BLK_S_IOERR 1
  109. #define VIRTIO_BLK_S_UNSUPP 2
  110. #endif /* _LINUX_VIRTIO_BLK_H */