You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ata.h 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. #ifndef _GPXE_ATA_H
  2. #define _GPXE_ATA_H
  3. #include <stdint.h>
  4. #include <gpxe/blockdev.h>
  5. #include <gpxe/uaccess.h>
  6. /** @file
  7. *
  8. * ATA devices
  9. *
  10. */
  11. /**
  12. * An ATA Logical Block Address
  13. *
  14. * ATA controllers have three byte-wide registers for specifying the
  15. * block address: LBA Low, LBA Mid and LBA High. This allows for a
  16. * 24-bit address. Some devices support the "48-bit address feature
  17. * set" (LBA48), in which case each of these byte-wide registers is
  18. * actually a two-entry FIFO, and the "previous" byte pushed into the
  19. * FIFO is used as the corresponding high-order byte. So, to set up
  20. * the 48-bit address 0x123456abcdef, you would issue
  21. *
  22. * 0x56 -> LBA Low register
  23. * 0xef -> LBA Low register
  24. * 0x34 -> LBA Mid register
  25. * 0xcd -> LBA Mid register
  26. * 0x12 -> LBA High register
  27. * 0xab -> LBA High register
  28. *
  29. * This structure encapsulates this information by providing a single
  30. * 64-bit integer in native byte order, unioned with bytes named so
  31. * that the sequence becomes
  32. *
  33. * low_prev -> LBA Low register
  34. * low_cur -> LBA Low register
  35. * mid_prev -> LBA Mid register
  36. * mid_cur -> LBA Mid register
  37. * high_prev -> LBA High register
  38. * high_cur -> LBA High register
  39. *
  40. * Just to complicate matters further, in non-LBA48 mode it is
  41. * possible to have a 28-bit address, in which case bits 27:24 must be
  42. * written into the low four bits of the Device register.
  43. */
  44. union ata_lba {
  45. /** LBA as a 64-bit integer in native-endian order */
  46. uint64_t native;
  47. /** ATA registers */
  48. struct {
  49. #if __BYTE_ORDER == __LITTLE_ENDIAN
  50. uint8_t low_cur;
  51. uint8_t mid_cur;
  52. uint8_t high_cur;
  53. uint8_t low_prev;
  54. uint8_t mid_prev;
  55. uint8_t high_prev;
  56. uint16_t pad;
  57. #elif __BYTE_ORDER == __BIG_ENDIAN
  58. uint16_t pad;
  59. uint8_t high_prev;
  60. uint8_t mid_prev;
  61. uint8_t low_prev;
  62. uint8_t high_cur;
  63. uint8_t mid_cur;
  64. uint8_t low_cur;
  65. #else
  66. #error "I need a byte order"
  67. #endif
  68. } bytes;
  69. };
  70. /** An ATA 2-byte FIFO register */
  71. union ata_fifo {
  72. /** Value in native-endian order */
  73. uint16_t native;
  74. /** ATA registers */
  75. struct {
  76. #if __BYTE_ORDER == __LITTLE_ENDIAN
  77. uint8_t cur;
  78. uint8_t prev;
  79. #elif __BYTE_ORDER == __BIG_ENDIAN
  80. uint8_t prev;
  81. uint8_t cur;
  82. #else
  83. #error "I need a byte order"
  84. #endif
  85. } bytes;
  86. };
  87. /** ATA command block */
  88. struct ata_cb {
  89. /** Logical block address */
  90. union ata_lba lba;
  91. /** Sector count */
  92. union ata_fifo count;
  93. /** Error/feature register */
  94. union ata_fifo err_feat;
  95. /** Device register */
  96. uint8_t device;
  97. /** Command/status register */
  98. uint8_t cmd_stat;
  99. /** LBA48 addressing flag */
  100. int lba48;
  101. };
  102. /** Obsolete bits in the ATA device register */
  103. #define ATA_DEV_OBSOLETE 0xa0
  104. /** LBA flag in the ATA device register */
  105. #define ATA_DEV_LBA 0x40
  106. /** Slave ("device 1") flag in the ATA device register */
  107. #define ATA_DEV_SLAVE 0x10
  108. /** Master ("device 0") flag in the ATA device register */
  109. #define ATA_DEV_MASTER 0x00
  110. /** Mask of non-LBA portion of device register */
  111. #define ATA_DEV_MASK 0xf0
  112. /** "Read sectors" command */
  113. #define ATA_CMD_READ 0x20
  114. /** "Read sectors (ext)" command */
  115. #define ATA_CMD_READ_EXT 0x24
  116. /** "Write sectors" command */
  117. #define ATA_CMD_WRITE 0x30
  118. /** "Write sectors (ext)" command */
  119. #define ATA_CMD_WRITE_EXT 0x34
  120. /** "Identify" command */
  121. #define ATA_CMD_IDENTIFY 0xec
  122. /** An ATA command */
  123. struct ata_command {
  124. /** ATA command block */
  125. struct ata_cb cb;
  126. /** Data-out buffer (may be NULL)
  127. *
  128. * If non-NULL, this buffer must be ata_command::cb::count
  129. * sectors in size.
  130. */
  131. userptr_t data_out;
  132. /** Data-in buffer (may be NULL)
  133. *
  134. * If non-NULL, this buffer must be ata_command::cb::count
  135. * sectors in size.
  136. */
  137. userptr_t data_in;
  138. };
  139. /**
  140. * Structure returned by ATA IDENTIFY command
  141. *
  142. * This is a huge structure with many fields that we don't care about,
  143. * so we implement only a few fields.
  144. */
  145. struct ata_identity {
  146. uint16_t ignore_a[60]; /* words 0-59 */
  147. uint32_t lba_sectors; /* words 60-61 */
  148. uint16_t ignore_b[21]; /* words 62-82 */
  149. uint16_t supports_lba48; /* word 83 */
  150. uint16_t ignore_c[16]; /* words 84-99 */
  151. uint64_t lba48_sectors; /* words 100-103 */
  152. uint16_t ignore_d[152]; /* words 104-255 */
  153. };
  154. /** Supports LBA48 flag */
  155. #define ATA_SUPPORTS_LBA48 ( 1 << 10 )
  156. /** ATA sector size */
  157. #define ATA_SECTOR_SIZE 512
  158. /** An ATA device */
  159. struct ata_device {
  160. /** Block device interface */
  161. struct block_device blockdev;
  162. /** Device number
  163. *
  164. * Must be ATA_DEV_MASTER or ATA_DEV_SLAVE.
  165. */
  166. int device;
  167. /** LBA48 extended addressing */
  168. int lba48;
  169. /**
  170. * Issue ATA command
  171. *
  172. * @v ata ATA device
  173. * @v command ATA command
  174. * @ret rc Return status code
  175. */
  176. int ( * command ) ( struct ata_device *ata,
  177. struct ata_command *command );
  178. };
  179. extern int init_atadev ( struct ata_device *ata );
  180. #endif /* _GPXE_ATA_H */