iPXE
ata.h
Go to the documentation of this file.
00001 #ifndef _IPXE_ATA_H
00002 #define _IPXE_ATA_H
00003 
00004 #include <stdint.h>
00005 #include <ipxe/uaccess.h>
00006 #include <ipxe/interface.h>
00007 
00008 /** @file
00009  *
00010  * ATA devices
00011  *
00012  */
00013 
00014 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00015 
00016 /**
00017  * An ATA Logical Block Address
00018  *
00019  * ATA controllers have three byte-wide registers for specifying the
00020  * block address: LBA Low, LBA Mid and LBA High.  This allows for a
00021  * 24-bit address.  Some devices support the "48-bit address feature
00022  * set" (LBA48), in which case each of these byte-wide registers is
00023  * actually a two-entry FIFO, and the "previous" byte pushed into the
00024  * FIFO is used as the corresponding high-order byte.  So, to set up
00025  * the 48-bit address 0x123456abcdef, you would issue
00026  *
00027  *     0x56 -> LBA Low register
00028  *     0xef -> LBA Low register
00029  *     0x34 -> LBA Mid register
00030  *     0xcd -> LBA Mid register
00031  *     0x12 -> LBA High register
00032  *     0xab -> LBA High register
00033  *
00034  * This structure encapsulates this information by providing a single
00035  * 64-bit integer in native byte order, unioned with bytes named so
00036  * that the sequence becomes
00037  *
00038  *     low_prev  -> LBA Low register
00039  *     low_cur   -> LBA Low register
00040  *     mid_prev  -> LBA Mid register
00041  *     mid_cur   -> LBA Mid register
00042  *     high_prev -> LBA High register
00043  *     high_cur  -> LBA High register
00044  *
00045  * Just to complicate matters further, in non-LBA48 mode it is
00046  * possible to have a 28-bit address, in which case bits 27:24 must be
00047  * written into the low four bits of the Device register.
00048  */
00049 union ata_lba {
00050         /** LBA as a 64-bit integer in native-endian order */
00051         uint64_t native;
00052         /** ATA registers */
00053         struct {
00054 #if __BYTE_ORDER == __LITTLE_ENDIAN
00055                 uint8_t low_cur;
00056                 uint8_t mid_cur;
00057                 uint8_t high_cur;
00058                 uint8_t low_prev;
00059                 uint8_t mid_prev;
00060                 uint8_t high_prev;
00061                 uint16_t pad;
00062 #elif __BYTE_ORDER == __BIG_ENDIAN
00063                 uint16_t pad;
00064                 uint8_t high_prev;
00065                 uint8_t mid_prev;
00066                 uint8_t low_prev;
00067                 uint8_t high_cur;
00068                 uint8_t mid_cur;
00069                 uint8_t low_cur;
00070 #else
00071 #error "I need a byte order"
00072 #endif
00073         } bytes;
00074 };
00075 
00076 /** An ATA 2-byte FIFO register */
00077 union ata_fifo {
00078         /** Value in native-endian order */
00079         uint16_t native;
00080         /** ATA registers */
00081         struct {
00082 #if __BYTE_ORDER == __LITTLE_ENDIAN
00083                 uint8_t cur;
00084                 uint8_t prev;
00085 #elif __BYTE_ORDER == __BIG_ENDIAN
00086                 uint8_t prev;
00087                 uint8_t cur;
00088 #else
00089 #error "I need a byte order"
00090 #endif
00091         } bytes;
00092 };
00093 
00094 /** ATA command block */
00095 struct ata_cb {
00096         /** Logical block address */
00097         union ata_lba lba;
00098         /** Sector count */
00099         union ata_fifo count;
00100         /** Error/feature register */
00101         union ata_fifo err_feat;
00102         /** Device register */
00103         uint8_t device;
00104         /** Command/status register */
00105         uint8_t cmd_stat;
00106         /** Use LBA48 extended addressing */
00107         int lba48;
00108 };
00109 
00110 /** Obsolete bits in the ATA device register */
00111 #define ATA_DEV_OBSOLETE 0xa0
00112 
00113 /** LBA flag in the ATA device register */
00114 #define ATA_DEV_LBA 0x40
00115 
00116 /** Slave ("device 1") flag in the ATA device register */
00117 #define ATA_DEV_SLAVE 0x10
00118 
00119 /** Master ("device 0") flag in the ATA device register */
00120 #define ATA_DEV_MASTER 0x00
00121 
00122 /** Mask of non-LBA portion of device register */
00123 #define ATA_DEV_MASK 0xf0
00124 
00125 /** "Read sectors" command */
00126 #define ATA_CMD_READ 0x20
00127 
00128 /** "Read sectors (ext)" command */
00129 #define ATA_CMD_READ_EXT 0x24
00130 
00131 /** "Write sectors" command */
00132 #define ATA_CMD_WRITE 0x30
00133 
00134 /** "Write sectors (ext)" command */
00135 #define ATA_CMD_WRITE_EXT 0x34
00136 
00137 /** "Identify" command */
00138 #define ATA_CMD_IDENTIFY 0xec
00139 
00140 /** Command completed in error */
00141 #define ATA_STAT_ERR 0x01
00142 
00143 /**
00144  * Structure returned by ATA IDENTIFY command
00145  *
00146  * This is a huge structure with many fields that we don't care about,
00147  * so we implement only a few fields.
00148  */
00149 struct ata_identity {
00150         uint16_t ignore_a[27]; /* words 0-26 */
00151         uint16_t model[20]; /* words 27-46 */
00152         uint16_t ignore_b[13]; /* words 47-59 */
00153         uint32_t lba_sectors; /* words 60-61 */
00154         uint16_t ignore_c[21]; /* words 62-82 */
00155         uint16_t supports_lba48; /* word 83 */
00156         uint16_t ignore_d[16]; /* words 84-99 */
00157         uint64_t lba48_sectors; /* words 100-103 */
00158         uint16_t ignore_e[152]; /* words 104-255 */
00159 };
00160 
00161 /** Supports LBA48 flag */
00162 #define ATA_SUPPORTS_LBA48 ( 1 << 10 )
00163 
00164 /** ATA sector size */
00165 #define ATA_SECTOR_SIZE 512
00166 
00167 /** An ATA command information unit */
00168 struct ata_cmd {
00169         /** ATA command block */
00170         struct ata_cb cb;
00171         /** Data-out buffer (may be NULL)
00172          *
00173          * If non-NULL, this buffer must be ata_command::cb::count
00174          * sectors in size.
00175          */
00176         userptr_t data_out;
00177         /** Data-out buffer length
00178          *
00179          * Must be zero if @c data_out is NULL
00180          */
00181         size_t data_out_len;
00182         /** Data-in buffer (may be NULL)
00183          *
00184          * If non-NULL, this buffer must be ata_command::cb::count
00185          * sectors in size.
00186          */
00187         userptr_t data_in;
00188         /** Data-in buffer length
00189          *
00190          * Must be zero if @c data_in is NULL
00191          */
00192         size_t data_in_len;
00193 };
00194 
00195 extern int ata_command ( struct interface *control, struct interface *data,
00196                          struct ata_cmd *command );
00197 #define ata_command_TYPE( object_type )                                 \
00198         typeof ( int ( object_type, struct interface *data,             \
00199                        struct ata_cmd *command ) )
00200 
00201 extern int ata_open ( struct interface *block, struct interface *ata,
00202                       unsigned int device, unsigned int max_count );
00203 
00204 #endif /* _IPXE_ATA_H */