blk_types.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #pragma once
  2. #include <common/glib.h>
  3. #include "stdint.h"
  4. #include <common/semaphore.h>
  5. #include <common/mutex.h>
  6. #define BLK_TYPE_AHCI 0
  7. #define DISK_NAME_LEN 32 // 磁盘名称的最大长度
  8. struct blk_gendisk;
  9. struct block_device_operation
  10. {
  11. long (*open)();
  12. long (*close)();
  13. long (*ioctl)(long cmd, long arg);
  14. /**
  15. * @brief 块设备驱动程序的传输函数
  16. *
  17. * @param gd 磁盘设备结构体
  18. * @param cmd 控制命令
  19. * @param base_addr 48位LBA地址
  20. * @param count total sectors to read
  21. * @param buf 缓冲区线性地址
  22. * @return long
  23. */
  24. long (*transfer)(struct blk_gendisk *gd, long cmd, uint64_t base_addr, uint64_t count, uint64_t buf);
  25. };
  26. /**
  27. * @brief 块设备请求队列内的packet
  28. *
  29. */
  30. struct block_device_request_packet
  31. {
  32. uchar cmd;
  33. uint64_t LBA_start;
  34. uint32_t count;
  35. uint64_t buffer_vaddr;
  36. uint8_t device_type; // 0: ahci
  37. void (*end_handler)(ul num, ul arg);
  38. wait_queue_node_t wait_queue;
  39. };
  40. /**
  41. * @brief 块设备的请求队列
  42. *
  43. */
  44. struct block_device_request_queue
  45. {
  46. wait_queue_node_t wait_queue_list;
  47. struct block_device_request_packet *in_service; // 正在请求的结点
  48. ul request_count;
  49. };
  50. /**
  51. * @brief 块设备结构体(对应磁盘的一个分区)
  52. *
  53. */
  54. struct block_device
  55. {
  56. sector_t bd_start_sector; // 该分区的起始扇区
  57. uint64_t bd_start_LBA; // 起始LBA号
  58. sector_t bd_sectors_num; // 该分区的扇区数
  59. struct vfs_superblock_t *bd_superblock; // 执行超级块的指针
  60. struct blk_gendisk *bd_disk; // 当前分区所属的磁盘
  61. struct block_device_request_queue *bd_queue; // 请求队列
  62. uint16_t bd_partno; // 在磁盘上的分区号
  63. };
  64. // 定义blk_gendisk中的标志位
  65. #define BLK_GF_AHCI (1 << 0)
  66. /**
  67. * @brief 磁盘设备结构体
  68. *
  69. */
  70. struct blk_gendisk
  71. {
  72. char disk_name[DISK_NAME_LEN]; // 磁盘驱动器名称
  73. uint16_t part_cnt; // 磁盘分区计数
  74. uint16_t flags;
  75. struct block_device *partition; // 磁盘分区数组
  76. const struct block_device_operation *fops; // 磁盘操作
  77. struct block_device_request_queue *request_queue; // 磁盘请求队列
  78. void *private_data;
  79. mutex_t open_mutex; // open()/close()操作的互斥锁
  80. };