ide: remove unused #include <linux/version.h>
[safe/jmp/linux-2.6] / include / linux / ata.h
index 1ce19c1..cb79b7a 100644 (file)
 #ifndef __LINUX_ATA_H__
 #define __LINUX_ATA_H__
 
+#include <linux/kernel.h>
+#include <linux/string.h>
 #include <linux/types.h>
+#include <asm/byteorder.h>
 
 /* defines only for the constants which don't work well as enums */
 #define ATA_DMA_BOUNDARY       0xffffUL
@@ -88,6 +91,10 @@ enum {
        ATA_ID_DLF              = 128,
        ATA_ID_CSFO             = 129,
        ATA_ID_CFA_POWER        = 160,
+       ATA_ID_CFA_KEY_MGMT     = 162,
+       ATA_ID_CFA_MODES        = 163,
+       ATA_ID_DATA_SET_MGMT    = 169,
+       ATA_ID_ROT_SPEED        = 217,
        ATA_ID_PIO4             = (1 << 1),
 
        ATA_ID_SERNO_LEN        = 20,
@@ -104,6 +111,8 @@ enum {
        ATA_PIO5                = ATA_PIO4 | (1 << 5),
        ATA_PIO6                = ATA_PIO5 | (1 << 6),
 
+       ATA_PIO4_ONLY           = (1 << 4),
+
        ATA_SWDMA0              = (1 << 0),
        ATA_SWDMA1              = ATA_SWDMA0 | (1 << 1),
        ATA_SWDMA2              = ATA_SWDMA1 | (1 << 2),
@@ -113,6 +122,8 @@ enum {
        ATA_MWDMA0              = (1 << 0),
        ATA_MWDMA1              = ATA_MWDMA0 | (1 << 1),
        ATA_MWDMA2              = ATA_MWDMA1 | (1 << 2),
+       ATA_MWDMA3              = ATA_MWDMA2 | (1 << 3),
+       ATA_MWDMA4              = ATA_MWDMA3 | (1 << 4),
 
        ATA_MWDMA12_ONLY        = (1 << 1) | (1 << 2),
        ATA_MWDMA2_ONLY         = (1 << 2),
@@ -127,6 +138,8 @@ enum {
        ATA_UDMA7               = ATA_UDMA6 | (1 << 7),
        /* ATA_UDMA7 is just for completeness... doesn't exist (yet?).  */
 
+       ATA_UDMA24_ONLY         = (1 << 2) | (1 << 4),
+
        ATA_UDMA_MASK_40C       = ATA_UDMA2,    /* udma0-2 */
 
        /* DMA-related */
@@ -238,10 +251,9 @@ enum {
        ATA_CMD_SMART           = 0xB0,
        ATA_CMD_MEDIA_LOCK      = 0xDE,
        ATA_CMD_MEDIA_UNLOCK    = 0xDF,
+       ATA_CMD_DSM             = 0x06,
        /* marked obsolete in the ATA/ATAPI-7 spec */
        ATA_CMD_RESTORE         = 0x10,
-       /* EXABYTE specific */
-       ATA_EXABYTE_ENABLE_NEST = 0xF0,
 
        /* READ_LOG_EXT pages */
        ATA_LOG_SATA_NCQ        = 0x10,
@@ -313,6 +325,9 @@ enum {
        ATA_SMART_READ_VALUES   = 0xD0,
        ATA_SMART_READ_THRESHOLDS = 0xD1,
 
+       /* feature values for Data Set Management */
+       ATA_DSM_TRIM            = 0x01,
+
        /* password used in LBA Mid / LBA High for executing SMART commands */
        ATA_SMART_LBAM_PASS     = 0x4F,
        ATA_SMART_LBAH_PASS     = 0xC2,
@@ -557,6 +572,15 @@ static inline int ata_id_has_flush(const u16 *id)
        return id[ATA_ID_COMMAND_SET_2] & (1 << 12);
 }
 
+static inline int ata_id_flush_enabled(const u16 *id)
+{
+       if (ata_id_has_flush(id) == 0)
+               return 0;
+       if ((id[ATA_ID_CSF_DEFAULT] & 0xC000) != 0x4000)
+               return 0;
+       return id[ATA_ID_CFS_ENABLE_2] & (1 << 12);
+}
+
 static inline int ata_id_has_flush_ext(const u16 *id)
 {
        if ((id[ATA_ID_COMMAND_SET_2] & 0xC000) != 0x4000)
@@ -564,6 +588,19 @@ static inline int ata_id_has_flush_ext(const u16 *id)
        return id[ATA_ID_COMMAND_SET_2] & (1 << 13);
 }
 
+static inline int ata_id_flush_ext_enabled(const u16 *id)
+{
+       if (ata_id_has_flush_ext(id) == 0)
+               return 0;
+       if ((id[ATA_ID_CSF_DEFAULT] & 0xC000) != 0x4000)
+               return 0;
+       /*
+        * some Maxtor disks have bit 13 defined incorrectly
+        * so check bit 10 too
+        */
+       return (id[ATA_ID_CFS_ENABLE_2] & 0x2400) == 0x2400;
+}
+
 static inline int ata_id_has_lba48(const u16 *id)
 {
        if ((id[ATA_ID_COMMAND_SET_2] & 0xC000) != 0x4000)
@@ -573,6 +610,15 @@ static inline int ata_id_has_lba48(const u16 *id)
        return id[ATA_ID_COMMAND_SET_2] & (1 << 10);
 }
 
+static inline int ata_id_lba48_enabled(const u16 *id)
+{
+       if (ata_id_has_lba48(id) == 0)
+               return 0;
+       if ((id[ATA_ID_CSF_DEFAULT] & 0xC000) != 0x4000)
+               return 0;
+       return id[ATA_ID_CFS_ENABLE_2] & (1 << 10);
+}
+
 static inline int ata_id_hpa_enabled(const u16 *id)
 {
        /* Yes children, word 83 valid bits cover word 82 data */
@@ -644,7 +690,15 @@ static inline unsigned int ata_id_major_version(const u16 *id)
 
 static inline int ata_id_is_sata(const u16 *id)
 {
-       return ata_id_major_version(id) >= 5 && id[ATA_ID_HW_CONFIG] == 0;
+       /*
+        * See if word 93 is 0 AND drive is at least ATA-5 compatible
+        * verifying that word 80 by casting it to a signed type --
+        * this trick allows us to filter out the reserved values of
+        * 0x0000 and 0xffff along with the earlier ATA revisions...
+        */
+       if (id[ATA_ID_HW_CONFIG] == 0 && (short)id[ATA_ID_MAJOR_VER] >= 0x0020)
+               return 1;
+       return 0;
 }
 
 static inline int ata_id_has_tpm(const u16 *id)
@@ -667,6 +721,23 @@ static inline int ata_id_has_dword_io(const u16 *id)
        return 0;
 }
 
+static inline int ata_id_has_unload(const u16 *id)
+{
+       if (ata_id_major_version(id) >= 7 &&
+           (id[ATA_ID_CFSSE] & 0xC000) == 0x4000 &&
+           id[ATA_ID_CFSSE] & (1 << 13))
+               return 1;
+       return 0;
+}
+
+static inline int ata_id_has_trim(const u16 *id)
+{
+       if (ata_id_major_version(id) >= 7 &&
+           (id[ATA_ID_DATA_SET_MGMT] & 1))
+               return 1;
+       return 0;
+}
+
 static inline int ata_id_current_chs_valid(const u16 *id)
 {
        /* For ATA-1 devices, if the INITIALIZE DEVICE PARAMETERS command
@@ -681,16 +752,26 @@ static inline int ata_id_current_chs_valid(const u16 *id)
 
 static inline int ata_id_is_cfa(const u16 *id)
 {
-       if (id[ATA_ID_CONFIG] == 0x848A)        /* Standard CF */
+       if (id[ATA_ID_CONFIG] == 0x848A)        /* Traditional CF */
                return 1;
-       /* Could be CF hiding as standard ATA */
-       if (ata_id_major_version(id) >= 3 &&
-           id[ATA_ID_COMMAND_SET_1] != 0xFFFF &&
-          (id[ATA_ID_COMMAND_SET_1] & (1 << 2)))
+       /*
+        * CF specs don't require specific value in the word 0 anymore and yet
+        * they forbid to report the ATA version in the word 80 and require the
+        * CFA feature set support to be indicated in the word 83 in this case.
+        * Unfortunately, some cards only follow either of this requirements,
+        * and while those that don't indicate CFA feature support need some
+        * sort of quirk list, it seems impractical for the ones that do...
+        */
+       if ((id[ATA_ID_COMMAND_SET_2] & 0xC004) == 0x4004)
                return 1;
        return 0;
 }
 
+static inline int ata_id_is_ssd(const u16 *id)
+{
+       return id[ATA_ID_ROT_SPEED] == 0x01;
+}
+
 static inline int ata_drive_40wire(const u16 *dev_id)
 {
        if (ata_id_is_sata(dev_id))
@@ -727,6 +808,102 @@ static inline int atapi_id_dmadir(const u16 *dev_id)
        return ata_id_major_version(dev_id) >= 7 && (dev_id[62] & 0x8000);
 }
 
+/*
+ * ata_id_is_lba_capacity_ok() performs a sanity check on
+ * the claimed LBA capacity value for the device.
+ *
+ * Returns 1 if LBA capacity looks sensible, 0 otherwise.
+ *
+ * It is called only once for each device.
+ */
+static inline int ata_id_is_lba_capacity_ok(u16 *id)
+{
+       unsigned long lba_sects, chs_sects, head, tail;
+
+       /* No non-LBA info .. so valid! */
+       if (id[ATA_ID_CYLS] == 0)
+               return 1;
+
+       lba_sects = ata_id_u32(id, ATA_ID_LBA_CAPACITY);
+
+       /*
+        * The ATA spec tells large drives to return
+        * C/H/S = 16383/16/63 independent of their size.
+        * Some drives can be jumpered to use 15 heads instead of 16.
+        * Some drives can be jumpered to use 4092 cyls instead of 16383.
+        */
+       if ((id[ATA_ID_CYLS] == 16383 ||
+            (id[ATA_ID_CYLS] == 4092 && id[ATA_ID_CUR_CYLS] == 16383)) &&
+           id[ATA_ID_SECTORS] == 63 &&
+           (id[ATA_ID_HEADS] == 15 || id[ATA_ID_HEADS] == 16) &&
+           (lba_sects >= 16383 * 63 * id[ATA_ID_HEADS]))
+               return 1;
+
+       chs_sects = id[ATA_ID_CYLS] * id[ATA_ID_HEADS] * id[ATA_ID_SECTORS];
+
+       /* perform a rough sanity check on lba_sects: within 10% is OK */
+       if (lba_sects - chs_sects < chs_sects/10)
+               return 1;
+
+       /* some drives have the word order reversed */
+       head = (lba_sects >> 16) & 0xffff;
+       tail = lba_sects & 0xffff;
+       lba_sects = head | (tail << 16);
+
+       if (lba_sects - chs_sects < chs_sects/10) {
+               *(__le32 *)&id[ATA_ID_LBA_CAPACITY] = __cpu_to_le32(lba_sects);
+               return 1;       /* LBA capacity is (now) good */
+       }
+
+       return 0;       /* LBA capacity value may be bad */
+}
+
+static inline void ata_id_to_hd_driveid(u16 *id)
+{
+#ifdef __BIG_ENDIAN
+       /* accessed in struct hd_driveid as 8-bit values */
+       id[ATA_ID_MAX_MULTSECT]  = __cpu_to_le16(id[ATA_ID_MAX_MULTSECT]);
+       id[ATA_ID_CAPABILITY]    = __cpu_to_le16(id[ATA_ID_CAPABILITY]);
+       id[ATA_ID_OLD_PIO_MODES] = __cpu_to_le16(id[ATA_ID_OLD_PIO_MODES]);
+       id[ATA_ID_OLD_DMA_MODES] = __cpu_to_le16(id[ATA_ID_OLD_DMA_MODES]);
+       id[ATA_ID_MULTSECT]      = __cpu_to_le16(id[ATA_ID_MULTSECT]);
+
+       /* as 32-bit values */
+       *(u32 *)&id[ATA_ID_LBA_CAPACITY] = ata_id_u32(id, ATA_ID_LBA_CAPACITY);
+       *(u32 *)&id[ATA_ID_SPG]          = ata_id_u32(id, ATA_ID_SPG);
+
+       /* as 64-bit value */
+       *(u64 *)&id[ATA_ID_LBA_CAPACITY_2] =
+               ata_id_u64(id, ATA_ID_LBA_CAPACITY_2);
+#endif
+}
+
+/*
+ * Write up to 'max' LBA Range Entries to the buffer that will cover the
+ * extent from sector to sector + count.  This is used for TRIM and for
+ * ADD LBA(S) TO NV CACHE PINNED SET.
+ */
+static inline unsigned ata_set_lba_range_entries(void *_buffer, unsigned max,
+                                               u64 sector, unsigned long count)
+{
+       __le64 *buffer = _buffer;
+       unsigned i = 0;
+
+       while (i < max) {
+               u64 entry = sector |
+                       ((u64)(count > 0xffff ? 0xffff : count) << 48);
+               buffer[i++] = __cpu_to_le64(entry);
+               if (count <= 0xffff)
+                       break;
+               count -= 0xffff;
+               sector += 0xffff;
+       }
+
+       max = ALIGN(i * 8, 512);
+       memset(buffer + i, 0, max - i * 8);
+       return max;
+}
+
 static inline int is_multi_taskfile(struct ata_taskfile *tf)
 {
        return (tf->command == ATA_CMD_READ_MULTI) ||
@@ -745,7 +922,7 @@ static inline int ata_ok(u8 status)
 static inline int lba_28_ok(u64 block, u32 n_block)
 {
        /* check the ending block number */
-       return ((block + n_block - 1) < ((u64)1 << 28)) && (n_block <= 256);
+       return ((block + n_block) < ((u64)1 << 28)) && (n_block <= 256);
 }
 
 static inline int lba_48_ok(u64 block, u32 n_block)