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.9KB

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