virtio_blk.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2021-9-16 GuEe-GUI the first version
  9. * 2021-11-11 GuEe-GUI using virtio common interface
  10. */
  11. #ifndef __VIRTIO_BLK_H__
  12. #define __VIRTIO_BLK_H__
  13. #include <rtdef.h>
  14. #include <virtio.h>
  15. #define VIRTIO_BLK_QUEUE 0
  16. #define VIRTIO_BLK_BYTES_PER_SECTOR 512
  17. #define VIRTIO_BLK_QUEUE_RING_SIZE 4
  18. #define VIRTIO_BLK_F_RO 5 /* Disk is read-only */
  19. #define VIRTIO_BLK_F_SCSI 7 /* Supports scsi command passthru */
  20. #define VIRTIO_BLK_F_CONFIG_WCE 11 /* Writeback mode available in config */
  21. #define VIRTIO_BLK_F_MQ 12 /* Support more than one vq */
  22. #define VIRTIO_BLK_T_IN 0 /* Read the blk */
  23. #define VIRTIO_BLK_T_OUT 1 /* Write the blk */
  24. #define VIRTIO_BLK_T_SCSI_CMD 2
  25. #define VIRTIO_BLK_T_SCSI_CMD_OUT 3
  26. #define VIRTIO_BLK_T_FLUSH 4
  27. #define VIRTIO_BLK_T_FLUSH_OUT 5
  28. struct virtio_blk_req
  29. {
  30. rt_uint32_t type;
  31. rt_uint32_t ioprio;
  32. rt_uint64_t sector;
  33. };
  34. struct virtio_blk_config
  35. {
  36. rt_uint64_t capacity; /* The capacity (in 512-byte sectors). */
  37. rt_uint32_t size_max; /* The maximum segment size (if VIRTIO_BLK_F_SIZE_MAX) */
  38. rt_uint32_t seg_max; /* The maximum number of segments (if VIRTIO_BLK_F_SEG_MAX) */
  39. /* Geometry of the device (if VIRTIO_BLK_F_GEOMETRY) */
  40. struct virtio_blk_geometry
  41. {
  42. rt_uint16_t cylinders;
  43. rt_uint8_t heads;
  44. rt_uint8_t sectors;
  45. } geometry;
  46. rt_uint32_t blk_size; /* Block size of device (if VIRTIO_BLK_F_BLK_SIZE) */
  47. struct virtio_blk_topology
  48. {
  49. /* # Of logical blocks per physical block (log2) */
  50. rt_uint8_t physical_block_exp;
  51. /* Offset of first aligned logical block */
  52. rt_uint8_t alignment_offset;
  53. /* Suggested minimum I/O size in blocks */
  54. rt_uint16_t min_io_size;
  55. /* Optimal (suggested maximum) I/O size in blocks */
  56. rt_uint32_t opt_io_size;
  57. } topology;
  58. rt_uint8_t writeback;
  59. rt_uint8_t unused0;
  60. rt_uint16_t num_queues;
  61. rt_uint32_t max_discard_sectors;
  62. rt_uint32_t max_discard_seg;
  63. rt_uint32_t discard_sector_alignment;
  64. rt_uint32_t max_write_zeroes_sectors;
  65. rt_uint32_t max_write_zeroes_seg;
  66. rt_uint8_t write_zeroes_may_unmap;
  67. rt_uint8_t unused1[3];
  68. rt_uint32_t max_secure_erase_sectors;
  69. rt_uint32_t max_secure_erase_seg;
  70. rt_uint32_t secure_erase_sector_alignment;
  71. } __attribute__((packed));
  72. struct virtio_blk_device
  73. {
  74. struct rt_device parent;
  75. struct virtio_device virtio_dev;
  76. struct virtio_blk_config *config;
  77. struct
  78. {
  79. rt_bool_t valid;
  80. rt_uint8_t status;
  81. struct virtio_blk_req req;
  82. } info[VIRTIO_BLK_QUEUE_RING_SIZE];
  83. };
  84. rt_err_t rt_virtio_blk_init(rt_ubase_t *mmio_base, rt_uint32_t irq);
  85. #endif /* __VIRTIO_BLK_H__ */