drv_virtio_blk.h 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. /*
  2. * Copyright (c) 2006-2021, 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. */
  10. #ifndef DRV_VIRTIO_BLK_H__
  11. #define DRV_VIRTIO_BLK_H__
  12. #include <rthw.h>
  13. #include <stdint.h>
  14. #include "virtio.h"
  15. #define VIRTIO_BLK_BUF_DATA_SIZE 512
  16. #define VIRTIO_BLK_BYTES_PER_SECTOR 512
  17. #define VIRTIO_BLK_BLOCK_SIZE 512
  18. #define VIRTIO_BLK_SECTOR_COUNT 0x40000 /* 128MB */
  19. #define VIRTIO_BLK_F_RO 5 /* Disk is read-only */
  20. #define VIRTIO_BLK_F_SCSI 7 /* Supports scsi command passthru */
  21. #define VIRTIO_BLK_F_CONFIG_WCE 11 /* Writeback mode available in config */
  22. #define VIRTIO_BLK_F_MQ 12 /* Support more than one vq */
  23. #define VIRTIO_BLK_T_IN 0 /* Read the blk */
  24. #define VIRTIO_BLK_T_OUT 1 /* Write the blk */
  25. #define VIRTIO_F_ANY_LAYOUT 27
  26. #define VIRTIO_RING_F_INDIRECT_DESC 28
  27. #define VIRTIO_RING_F_EVENT_IDX 29
  28. struct virtio_blk_buf
  29. {
  30. int valid;
  31. uint32_t block_no;
  32. uint8_t *data;
  33. };
  34. struct virtio_blk_req
  35. {
  36. uint32_t type;
  37. uint32_t reserved;
  38. uint64_t sector;
  39. };
  40. /*
  41. * virtio_blk must be a static variable because
  42. * pages must consist of two contiguous pages of
  43. * page-aligned physical memory
  44. */
  45. struct virtio_blk
  46. {
  47. char pages[2 * PAGE_SIZE];
  48. struct virtq_desc *desc;
  49. struct virtq_avail *avail;
  50. struct virtq_used *used;
  51. char free[QUEUE_SIZE];
  52. uint16_t used_idx;
  53. struct
  54. {
  55. struct virtio_blk_buf *buf;
  56. char status;
  57. } info[QUEUE_SIZE];
  58. struct virtio_blk_req ops[QUEUE_SIZE];
  59. } __attribute__ ((aligned (PAGE_SIZE)));
  60. struct virtio_blk_device
  61. {
  62. struct rt_device parent;
  63. struct virtio_blk *blk;
  64. uint32_t *mmio_base;
  65. #ifdef RT_USING_SMP
  66. struct rt_spinlock spinlock;
  67. #endif
  68. };
  69. int rt_hw_virtio_blk_init(void);
  70. #endif /* DRV_VIRTIO_BLK_H__ */