aboutsummaryrefslogtreecommitdiffstats
path: root/include/linux/ata.h
diff options
context:
space:
mode:
Diffstat (limited to 'include/linux/ata.h')
-rw-r--r--include/linux/ata.h282
1 files changed, 235 insertions, 47 deletions
diff --git a/include/linux/ata.h b/include/linux/ata.h
index 1c622e2b0504..a53318b8cbd0 100644
--- a/include/linux/ata.h
+++ b/include/linux/ata.h
@@ -30,6 +30,7 @@
30#define __LINUX_ATA_H__ 30#define __LINUX_ATA_H__
31 31
32#include <linux/types.h> 32#include <linux/types.h>
33#include <asm/byteorder.h>
33 34
34/* defines only for the constants which don't work well as enums */ 35/* defines only for the constants which don't work well as enums */
35#define ATA_DMA_BOUNDARY 0xffffUL 36#define ATA_DMA_BOUNDARY 0xffffUL
@@ -46,18 +47,49 @@ enum {
46 ATA_MAX_SECTORS_TAPE = 65535, 47 ATA_MAX_SECTORS_TAPE = 65535,
47 48
48 ATA_ID_WORDS = 256, 49 ATA_ID_WORDS = 256,
50 ATA_ID_CONFIG = 0,
51 ATA_ID_CYLS = 1,
52 ATA_ID_HEADS = 3,
53 ATA_ID_SECTORS = 6,
49 ATA_ID_SERNO = 10, 54 ATA_ID_SERNO = 10,
55 ATA_ID_BUF_SIZE = 21,
50 ATA_ID_FW_REV = 23, 56 ATA_ID_FW_REV = 23,
51 ATA_ID_PROD = 27, 57 ATA_ID_PROD = 27,
58 ATA_ID_MAX_MULTSECT = 47,
59 ATA_ID_DWORD_IO = 48,
60 ATA_ID_CAPABILITY = 49,
52 ATA_ID_OLD_PIO_MODES = 51, 61 ATA_ID_OLD_PIO_MODES = 51,
62 ATA_ID_OLD_DMA_MODES = 52,
53 ATA_ID_FIELD_VALID = 53, 63 ATA_ID_FIELD_VALID = 53,
64 ATA_ID_CUR_CYLS = 54,
65 ATA_ID_CUR_HEADS = 55,
66 ATA_ID_CUR_SECTORS = 56,
67 ATA_ID_MULTSECT = 59,
68 ATA_ID_LBA_CAPACITY = 60,
69 ATA_ID_SWDMA_MODES = 62,
54 ATA_ID_MWDMA_MODES = 63, 70 ATA_ID_MWDMA_MODES = 63,
55 ATA_ID_PIO_MODES = 64, 71 ATA_ID_PIO_MODES = 64,
56 ATA_ID_EIDE_DMA_MIN = 65, 72 ATA_ID_EIDE_DMA_MIN = 65,
73 ATA_ID_EIDE_DMA_TIME = 66,
57 ATA_ID_EIDE_PIO = 67, 74 ATA_ID_EIDE_PIO = 67,
58 ATA_ID_EIDE_PIO_IORDY = 68, 75 ATA_ID_EIDE_PIO_IORDY = 68,
59 ATA_ID_UDMA_MODES = 88, 76 ATA_ID_QUEUE_DEPTH = 75,
60 ATA_ID_MAJOR_VER = 80, 77 ATA_ID_MAJOR_VER = 80,
78 ATA_ID_COMMAND_SET_1 = 82,
79 ATA_ID_COMMAND_SET_2 = 83,
80 ATA_ID_CFSSE = 84,
81 ATA_ID_CFS_ENABLE_1 = 85,
82 ATA_ID_CFS_ENABLE_2 = 86,
83 ATA_ID_CSF_DEFAULT = 87,
84 ATA_ID_UDMA_MODES = 88,
85 ATA_ID_HW_CONFIG = 93,
86 ATA_ID_SPG = 98,
87 ATA_ID_LBA_CAPACITY_2 = 100,
88 ATA_ID_LAST_LUN = 126,
89 ATA_ID_DLF = 128,
90 ATA_ID_CSFO = 129,
91 ATA_ID_CFA_POWER = 160,
92 ATA_ID_ROT_SPEED = 217,
61 ATA_ID_PIO4 = (1 << 1), 93 ATA_ID_PIO4 = (1 << 1),
62 94
63 ATA_ID_SERNO_LEN = 20, 95 ATA_ID_SERNO_LEN = 20,
@@ -123,13 +155,26 @@ enum {
123 ATA_BUSY = (1 << 7), /* BSY status bit */ 155 ATA_BUSY = (1 << 7), /* BSY status bit */
124 ATA_DRDY = (1 << 6), /* device ready */ 156 ATA_DRDY = (1 << 6), /* device ready */
125 ATA_DF = (1 << 5), /* device fault */ 157 ATA_DF = (1 << 5), /* device fault */
158 ATA_DSC = (1 << 4), /* drive seek complete */
126 ATA_DRQ = (1 << 3), /* data request i/o */ 159 ATA_DRQ = (1 << 3), /* data request i/o */
160 ATA_CORR = (1 << 2), /* corrected data error */
161 ATA_IDX = (1 << 1), /* index */
127 ATA_ERR = (1 << 0), /* have an error */ 162 ATA_ERR = (1 << 0), /* have an error */
128 ATA_SRST = (1 << 2), /* software reset */ 163 ATA_SRST = (1 << 2), /* software reset */
129 ATA_ICRC = (1 << 7), /* interface CRC error */ 164 ATA_ICRC = (1 << 7), /* interface CRC error */
165 ATA_BBK = ATA_ICRC, /* pre-EIDE: block marked bad */
130 ATA_UNC = (1 << 6), /* uncorrectable media error */ 166 ATA_UNC = (1 << 6), /* uncorrectable media error */
167 ATA_MC = (1 << 5), /* media changed */
131 ATA_IDNF = (1 << 4), /* ID not found */ 168 ATA_IDNF = (1 << 4), /* ID not found */
169 ATA_MCR = (1 << 3), /* media change requested */
132 ATA_ABORTED = (1 << 2), /* command aborted */ 170 ATA_ABORTED = (1 << 2), /* command aborted */
171 ATA_TRK0NF = (1 << 1), /* track 0 not found */
172 ATA_AMNF = (1 << 0), /* address mark not found */
173 ATAPI_LFS = 0xF0, /* last failed sense */
174 ATAPI_EOM = ATA_TRK0NF, /* end of media */
175 ATAPI_ILI = ATA_AMNF, /* illegal length indication */
176 ATAPI_IO = (1 << 1),
177 ATAPI_COD = (1 << 0),
133 178
134 /* ATA command block registers */ 179 /* ATA command block registers */
135 ATA_REG_DATA = 0x00, 180 ATA_REG_DATA = 0x00,
@@ -192,6 +237,13 @@ enum {
192 ATA_CMD_PMP_WRITE = 0xE8, 237 ATA_CMD_PMP_WRITE = 0xE8,
193 ATA_CMD_CONF_OVERLAY = 0xB1, 238 ATA_CMD_CONF_OVERLAY = 0xB1,
194 ATA_CMD_SEC_FREEZE_LOCK = 0xF5, 239 ATA_CMD_SEC_FREEZE_LOCK = 0xF5,
240 ATA_CMD_SMART = 0xB0,
241 ATA_CMD_MEDIA_LOCK = 0xDE,
242 ATA_CMD_MEDIA_UNLOCK = 0xDF,
243 /* marked obsolete in the ATA/ATAPI-7 spec */
244 ATA_CMD_RESTORE = 0x10,
245 /* EXABYTE specific */
246 ATA_EXABYTE_ENABLE_NEST = 0xF0,
195 247
196 /* READ_LOG_EXT pages */ 248 /* READ_LOG_EXT pages */
197 ATA_LOG_SATA_NCQ = 0x10, 249 ATA_LOG_SATA_NCQ = 0x10,
@@ -232,6 +284,10 @@ enum {
232 SETFEATURES_WC_ON = 0x02, /* Enable write cache */ 284 SETFEATURES_WC_ON = 0x02, /* Enable write cache */
233 SETFEATURES_WC_OFF = 0x82, /* Disable write cache */ 285 SETFEATURES_WC_OFF = 0x82, /* Disable write cache */
234 286
287 /* Enable/Disable Automatic Acoustic Management */
288 SETFEATURES_AAM_ON = 0x42,
289 SETFEATURES_AAM_OFF = 0xC2,
290
235 SETFEATURES_SPINUP = 0x07, /* Spin-up drive */ 291 SETFEATURES_SPINUP = 0x07, /* Spin-up drive */
236 292
237 SETFEATURES_SATA_ENABLE = 0x10, /* Enable use of SATA feature */ 293 SETFEATURES_SATA_ENABLE = 0x10, /* Enable use of SATA feature */
@@ -254,6 +310,15 @@ enum {
254 ATA_DCO_IDENTIFY = 0xC2, 310 ATA_DCO_IDENTIFY = 0xC2,
255 ATA_DCO_SET = 0xC3, 311 ATA_DCO_SET = 0xC3,
256 312
313 /* feature values for SMART */
314 ATA_SMART_ENABLE = 0xD8,
315 ATA_SMART_READ_VALUES = 0xD0,
316 ATA_SMART_READ_THRESHOLDS = 0xD1,
317
318 /* password used in LBA Mid / LBA High for executing SMART commands */
319 ATA_SMART_LBAM_PASS = 0x4F,
320 ATA_SMART_LBAH_PASS = 0xC2,
321
257 /* ATAPI stuff */ 322 /* ATAPI stuff */
258 ATAPI_PKT_DMA = (1 << 0), 323 ATAPI_PKT_DMA = (1 << 0),
259 ATAPI_DMADIR = (1 << 2), /* ATAPI data dir: 324 ATAPI_DMADIR = (1 << 2), /* ATAPI data dir:
@@ -438,17 +503,17 @@ static inline int ata_is_data(u8 prot)
438/* 503/*
439 * id tests 504 * id tests
440 */ 505 */
441#define ata_id_is_ata(id) (((id)[0] & (1 << 15)) == 0) 506#define ata_id_is_ata(id) (((id)[ATA_ID_CONFIG] & (1 << 15)) == 0)
442#define ata_id_has_lba(id) ((id)[49] & (1 << 9)) 507#define ata_id_has_lba(id) ((id)[ATA_ID_CAPABILITY] & (1 << 9))
443#define ata_id_has_dma(id) ((id)[49] & (1 << 8)) 508#define ata_id_has_dma(id) ((id)[ATA_ID_CAPABILITY] & (1 << 8))
444#define ata_id_has_ncq(id) ((id)[76] & (1 << 8)) 509#define ata_id_has_ncq(id) ((id)[76] & (1 << 8))
445#define ata_id_queue_depth(id) (((id)[75] & 0x1f) + 1) 510#define ata_id_queue_depth(id) (((id)[ATA_ID_QUEUE_DEPTH] & 0x1f) + 1)
446#define ata_id_removeable(id) ((id)[0] & (1 << 7)) 511#define ata_id_removeable(id) ((id)[ATA_ID_CONFIG] & (1 << 7))
447#define ata_id_has_atapi_AN(id) \ 512#define ata_id_has_atapi_AN(id) \
448 ( (((id)[76] != 0x0000) && ((id)[76] != 0xffff)) && \ 513 ( (((id)[76] != 0x0000) && ((id)[76] != 0xffff)) && \
449 ((id)[78] & (1 << 5)) ) 514 ((id)[78] & (1 << 5)) )
450#define ata_id_iordy_disable(id) ((id)[49] & (1 << 10)) 515#define ata_id_iordy_disable(id) ((id)[ATA_ID_CAPABILITY] & (1 << 10))
451#define ata_id_has_iordy(id) ((id)[49] & (1 << 11)) 516#define ata_id_has_iordy(id) ((id)[ATA_ID_CAPABILITY] & (1 << 11))
452#define ata_id_u32(id,n) \ 517#define ata_id_u32(id,n) \
453 (((u32) (id)[(n) + 1] << 16) | ((u32) (id)[(n)])) 518 (((u32) (id)[(n) + 1] << 16) | ((u32) (id)[(n)]))
454#define ata_id_u64(id,n) \ 519#define ata_id_u64(id,n) \
@@ -457,7 +522,7 @@ static inline int ata_is_data(u8 prot)
457 ((u64) (id)[(n) + 1] << 16) | \ 522 ((u64) (id)[(n) + 1] << 16) | \
458 ((u64) (id)[(n) + 0]) ) 523 ((u64) (id)[(n) + 0]) )
459 524
460#define ata_id_cdb_intr(id) (((id)[0] & 0x60) == 0x20) 525#define ata_id_cdb_intr(id) (((id)[ATA_ID_CONFIG] & 0x60) == 0x20)
461 526
462static inline bool ata_id_has_hipm(const u16 *id) 527static inline bool ata_id_has_hipm(const u16 *id)
463{ 528{
@@ -482,75 +547,106 @@ static inline bool ata_id_has_dipm(const u16 *id)
482 547
483static inline int ata_id_has_fua(const u16 *id) 548static inline int ata_id_has_fua(const u16 *id)
484{ 549{
485 if ((id[84] & 0xC000) != 0x4000) 550 if ((id[ATA_ID_CFSSE] & 0xC000) != 0x4000)
486 return 0; 551 return 0;
487 return id[84] & (1 << 6); 552 return id[ATA_ID_CFSSE] & (1 << 6);
488} 553}
489 554
490static inline int ata_id_has_flush(const u16 *id) 555static inline int ata_id_has_flush(const u16 *id)
491{ 556{
492 if ((id[83] & 0xC000) != 0x4000) 557 if ((id[ATA_ID_COMMAND_SET_2] & 0xC000) != 0x4000)
558 return 0;
559 return id[ATA_ID_COMMAND_SET_2] & (1 << 12);
560}
561
562static inline int ata_id_flush_enabled(const u16 *id)
563{
564 if (ata_id_has_flush(id) == 0)
493 return 0; 565 return 0;
494 return id[83] & (1 << 12); 566 if ((id[ATA_ID_CSF_DEFAULT] & 0xC000) != 0x4000)
567 return 0;
568 return id[ATA_ID_CFS_ENABLE_2] & (1 << 12);
495} 569}
496 570
497static inline int ata_id_has_flush_ext(const u16 *id) 571static inline int ata_id_has_flush_ext(const u16 *id)
498{ 572{
499 if ((id[83] & 0xC000) != 0x4000) 573 if ((id[ATA_ID_COMMAND_SET_2] & 0xC000) != 0x4000)
500 return 0; 574 return 0;
501 return id[83] & (1 << 13); 575 return id[ATA_ID_COMMAND_SET_2] & (1 << 13);
576}
577
578static inline int ata_id_flush_ext_enabled(const u16 *id)
579{
580 if (ata_id_has_flush_ext(id) == 0)
581 return 0;
582 if ((id[ATA_ID_CSF_DEFAULT] & 0xC000) != 0x4000)
583 return 0;
584 /*
585 * some Maxtor disks have bit 13 defined incorrectly
586 * so check bit 10 too
587 */
588 return (id[ATA_ID_CFS_ENABLE_2] & 0x2400) == 0x2400;
502} 589}
503 590
504static inline int ata_id_has_lba48(const u16 *id) 591static inline int ata_id_has_lba48(const u16 *id)
505{ 592{
506 if ((id[83] & 0xC000) != 0x4000) 593 if ((id[ATA_ID_COMMAND_SET_2] & 0xC000) != 0x4000)
507 return 0; 594 return 0;
508 if (!ata_id_u64(id, 100)) 595 if (!ata_id_u64(id, ATA_ID_LBA_CAPACITY_2))
509 return 0; 596 return 0;
510 return id[83] & (1 << 10); 597 return id[ATA_ID_COMMAND_SET_2] & (1 << 10);
598}
599
600static inline int ata_id_lba48_enabled(const u16 *id)
601{
602 if (ata_id_has_lba48(id) == 0)
603 return 0;
604 if ((id[ATA_ID_CSF_DEFAULT] & 0xC000) != 0x4000)
605 return 0;
606 return id[ATA_ID_CFS_ENABLE_2] & (1 << 10);
511} 607}
512 608
513static inline int ata_id_hpa_enabled(const u16 *id) 609static inline int ata_id_hpa_enabled(const u16 *id)
514{ 610{
515 /* Yes children, word 83 valid bits cover word 82 data */ 611 /* Yes children, word 83 valid bits cover word 82 data */
516 if ((id[83] & 0xC000) != 0x4000) 612 if ((id[ATA_ID_COMMAND_SET_2] & 0xC000) != 0x4000)
517 return 0; 613 return 0;
518 /* And 87 covers 85-87 */ 614 /* And 87 covers 85-87 */
519 if ((id[87] & 0xC000) != 0x4000) 615 if ((id[ATA_ID_CSF_DEFAULT] & 0xC000) != 0x4000)
520 return 0; 616 return 0;
521 /* Check command sets enabled as well as supported */ 617 /* Check command sets enabled as well as supported */
522 if ((id[85] & ( 1 << 10)) == 0) 618 if ((id[ATA_ID_CFS_ENABLE_1] & (1 << 10)) == 0)
523 return 0; 619 return 0;
524 return id[82] & (1 << 10); 620 return id[ATA_ID_COMMAND_SET_1] & (1 << 10);
525} 621}
526 622
527static inline int ata_id_has_wcache(const u16 *id) 623static inline int ata_id_has_wcache(const u16 *id)
528{ 624{
529 /* Yes children, word 83 valid bits cover word 82 data */ 625 /* Yes children, word 83 valid bits cover word 82 data */
530 if ((id[83] & 0xC000) != 0x4000) 626 if ((id[ATA_ID_COMMAND_SET_2] & 0xC000) != 0x4000)
531 return 0; 627 return 0;
532 return id[82] & (1 << 5); 628 return id[ATA_ID_COMMAND_SET_1] & (1 << 5);
533} 629}
534 630
535static inline int ata_id_has_pm(const u16 *id) 631static inline int ata_id_has_pm(const u16 *id)
536{ 632{
537 if ((id[83] & 0xC000) != 0x4000) 633 if ((id[ATA_ID_COMMAND_SET_2] & 0xC000) != 0x4000)
538 return 0; 634 return 0;
539 return id[82] & (1 << 3); 635 return id[ATA_ID_COMMAND_SET_1] & (1 << 3);
540} 636}
541 637
542static inline int ata_id_rahead_enabled(const u16 *id) 638static inline int ata_id_rahead_enabled(const u16 *id)
543{ 639{
544 if ((id[87] & 0xC000) != 0x4000) 640 if ((id[ATA_ID_CSF_DEFAULT] & 0xC000) != 0x4000)
545 return 0; 641 return 0;
546 return id[85] & (1 << 6); 642 return id[ATA_ID_CFS_ENABLE_1] & (1 << 6);
547} 643}
548 644
549static inline int ata_id_wcache_enabled(const u16 *id) 645static inline int ata_id_wcache_enabled(const u16 *id)
550{ 646{
551 if ((id[87] & 0xC000) != 0x4000) 647 if ((id[ATA_ID_CSF_DEFAULT] & 0xC000) != 0x4000)
552 return 0; 648 return 0;
553 return id[85] & (1 << 5); 649 return id[ATA_ID_CFS_ENABLE_1] & (1 << 5);
554} 650}
555 651
556/** 652/**
@@ -581,7 +677,15 @@ static inline unsigned int ata_id_major_version(const u16 *id)
581 677
582static inline int ata_id_is_sata(const u16 *id) 678static inline int ata_id_is_sata(const u16 *id)
583{ 679{
584 return ata_id_major_version(id) >= 5 && id[93] == 0; 680 /*
681 * See if word 93 is 0 AND drive is at least ATA-5 compatible
682 * verifying that word 80 by casting it to a signed type --
683 * this trick allows us to filter out the reserved values of
684 * 0x0000 and 0xffff along with the earlier ATA revisions...
685 */
686 if (id[ATA_ID_HW_CONFIG] == 0 && (short)id[ATA_ID_MAJOR_VER] >= 0x0020)
687 return 1;
688 return 0;
585} 689}
586 690
587static inline int ata_id_has_tpm(const u16 *id) 691static inline int ata_id_has_tpm(const u16 *id)
@@ -599,7 +703,16 @@ static inline int ata_id_has_dword_io(const u16 *id)
599 /* ATA 8 reuses this flag for "trusted" computing */ 703 /* ATA 8 reuses this flag for "trusted" computing */
600 if (ata_id_major_version(id) > 7) 704 if (ata_id_major_version(id) > 7)
601 return 0; 705 return 0;
602 if (id[48] & (1 << 0)) 706 if (id[ATA_ID_DWORD_IO] & (1 << 0))
707 return 1;
708 return 0;
709}
710
711static inline int ata_id_has_unload(const u16 *id)
712{
713 if (ata_id_major_version(id) >= 7 &&
714 (id[ATA_ID_CFSSE] & 0xC000) == 0x4000 &&
715 id[ATA_ID_CFSSE] & (1 << 13))
603 return 1; 716 return 1;
604 return 0; 717 return 0;
605} 718}
@@ -608,45 +721,50 @@ static inline int ata_id_current_chs_valid(const u16 *id)
608{ 721{
609 /* For ATA-1 devices, if the INITIALIZE DEVICE PARAMETERS command 722 /* For ATA-1 devices, if the INITIALIZE DEVICE PARAMETERS command
610 has not been issued to the device then the values of 723 has not been issued to the device then the values of
611 id[54] to id[56] are vendor specific. */ 724 id[ATA_ID_CUR_CYLS] to id[ATA_ID_CUR_SECTORS] are vendor specific. */
612 return (id[53] & 0x01) && /* Current translation valid */ 725 return (id[ATA_ID_FIELD_VALID] & 1) && /* Current translation valid */
613 id[54] && /* cylinders in current translation */ 726 id[ATA_ID_CUR_CYLS] && /* cylinders in current translation */
614 id[55] && /* heads in current translation */ 727 id[ATA_ID_CUR_HEADS] && /* heads in current translation */
615 id[55] <= 16 && 728 id[ATA_ID_CUR_HEADS] <= 16 &&
616 id[56]; /* sectors in current translation */ 729 id[ATA_ID_CUR_SECTORS]; /* sectors in current translation */
617} 730}
618 731
619static inline int ata_id_is_cfa(const u16 *id) 732static inline int ata_id_is_cfa(const u16 *id)
620{ 733{
621 u16 v = id[0]; 734 if (id[ATA_ID_CONFIG] == 0x848A) /* Standard CF */
622 if (v == 0x848A) /* Standard CF */
623 return 1; 735 return 1;
624 /* Could be CF hiding as standard ATA */ 736 /* Could be CF hiding as standard ATA */
625 if (ata_id_major_version(id) >= 3 && id[82] != 0xFFFF && 737 if (ata_id_major_version(id) >= 3 &&
626 (id[82] & ( 1 << 2))) 738 id[ATA_ID_COMMAND_SET_1] != 0xFFFF &&
739 (id[ATA_ID_COMMAND_SET_1] & (1 << 2)))
627 return 1; 740 return 1;
628 return 0; 741 return 0;
629} 742}
630 743
744static inline int ata_id_is_ssd(const u16 *id)
745{
746 return id[ATA_ID_ROT_SPEED] == 0x01;
747}
748
631static inline int ata_drive_40wire(const u16 *dev_id) 749static inline int ata_drive_40wire(const u16 *dev_id)
632{ 750{
633 if (ata_id_is_sata(dev_id)) 751 if (ata_id_is_sata(dev_id))
634 return 0; /* SATA */ 752 return 0; /* SATA */
635 if ((dev_id[93] & 0xE000) == 0x6000) 753 if ((dev_id[ATA_ID_HW_CONFIG] & 0xE000) == 0x6000)
636 return 0; /* 80 wire */ 754 return 0; /* 80 wire */
637 return 1; 755 return 1;
638} 756}
639 757
640static inline int ata_drive_40wire_relaxed(const u16 *dev_id) 758static inline int ata_drive_40wire_relaxed(const u16 *dev_id)
641{ 759{
642 if ((dev_id[93] & 0x2000) == 0x2000) 760 if ((dev_id[ATA_ID_HW_CONFIG] & 0x2000) == 0x2000)
643 return 0; /* 80 wire */ 761 return 0; /* 80 wire */
644 return 1; 762 return 1;
645} 763}
646 764
647static inline int atapi_cdb_len(const u16 *dev_id) 765static inline int atapi_cdb_len(const u16 *dev_id)
648{ 766{
649 u16 tmp = dev_id[0] & 0x3; 767 u16 tmp = dev_id[ATA_ID_CONFIG] & 0x3;
650 switch (tmp) { 768 switch (tmp) {
651 case 0: return 12; 769 case 0: return 12;
652 case 1: return 16; 770 case 1: return 16;
@@ -656,7 +774,7 @@ static inline int atapi_cdb_len(const u16 *dev_id)
656 774
657static inline int atapi_command_packet_set(const u16 *dev_id) 775static inline int atapi_command_packet_set(const u16 *dev_id)
658{ 776{
659 return (dev_id[0] >> 8) & 0x1f; 777 return (dev_id[ATA_ID_CONFIG] >> 8) & 0x1f;
660} 778}
661 779
662static inline int atapi_id_dmadir(const u16 *dev_id) 780static inline int atapi_id_dmadir(const u16 *dev_id)
@@ -664,6 +782,76 @@ static inline int atapi_id_dmadir(const u16 *dev_id)
664 return ata_id_major_version(dev_id) >= 7 && (dev_id[62] & 0x8000); 782 return ata_id_major_version(dev_id) >= 7 && (dev_id[62] & 0x8000);
665} 783}
666 784
785/*
786 * ata_id_is_lba_capacity_ok() performs a sanity check on
787 * the claimed LBA capacity value for the device.
788 *
789 * Returns 1 if LBA capacity looks sensible, 0 otherwise.
790 *
791 * It is called only once for each device.
792 */
793static inline int ata_id_is_lba_capacity_ok(u16 *id)
794{
795 unsigned long lba_sects, chs_sects, head, tail;
796
797 /* No non-LBA info .. so valid! */
798 if (id[ATA_ID_CYLS] == 0)
799 return 1;
800
801 lba_sects = ata_id_u32(id, ATA_ID_LBA_CAPACITY);
802
803 /*
804 * The ATA spec tells large drives to return
805 * C/H/S = 16383/16/63 independent of their size.
806 * Some drives can be jumpered to use 15 heads instead of 16.
807 * Some drives can be jumpered to use 4092 cyls instead of 16383.
808 */
809 if ((id[ATA_ID_CYLS] == 16383 ||
810 (id[ATA_ID_CYLS] == 4092 && id[ATA_ID_CUR_CYLS] == 16383)) &&
811 id[ATA_ID_SECTORS] == 63 &&
812 (id[ATA_ID_HEADS] == 15 || id[ATA_ID_HEADS] == 16) &&
813 (lba_sects >= 16383 * 63 * id[ATA_ID_HEADS]))
814 return 1;
815
816 chs_sects = id[ATA_ID_CYLS] * id[ATA_ID_HEADS] * id[ATA_ID_SECTORS];
817
818 /* perform a rough sanity check on lba_sects: within 10% is OK */
819 if (lba_sects - chs_sects < chs_sects/10)
820 return 1;
821
822 /* some drives have the word order reversed */
823 head = (lba_sects >> 16) & 0xffff;
824 tail = lba_sects & 0xffff;
825 lba_sects = head | (tail << 16);
826
827 if (lba_sects - chs_sects < chs_sects/10) {
828 *(__le32 *)&id[ATA_ID_LBA_CAPACITY] = __cpu_to_le32(lba_sects);
829 return 1; /* LBA capacity is (now) good */
830 }
831
832 return 0; /* LBA capacity value may be bad */
833}
834
835static inline void ata_id_to_hd_driveid(u16 *id)
836{
837#ifdef __BIG_ENDIAN
838 /* accessed in struct hd_driveid as 8-bit values */
839 id[ATA_ID_MAX_MULTSECT] = __cpu_to_le16(id[ATA_ID_MAX_MULTSECT]);
840 id[ATA_ID_CAPABILITY] = __cpu_to_le16(id[ATA_ID_CAPABILITY]);
841 id[ATA_ID_OLD_PIO_MODES] = __cpu_to_le16(id[ATA_ID_OLD_PIO_MODES]);
842 id[ATA_ID_OLD_DMA_MODES] = __cpu_to_le16(id[ATA_ID_OLD_DMA_MODES]);
843 id[ATA_ID_MULTSECT] = __cpu_to_le16(id[ATA_ID_MULTSECT]);
844
845 /* as 32-bit values */
846 *(u32 *)&id[ATA_ID_LBA_CAPACITY] = ata_id_u32(id, ATA_ID_LBA_CAPACITY);
847 *(u32 *)&id[ATA_ID_SPG] = ata_id_u32(id, ATA_ID_SPG);
848
849 /* as 64-bit value */
850 *(u64 *)&id[ATA_ID_LBA_CAPACITY_2] =
851 ata_id_u64(id, ATA_ID_LBA_CAPACITY_2);
852#endif
853}
854
667static inline int is_multi_taskfile(struct ata_taskfile *tf) 855static inline int is_multi_taskfile(struct ata_taskfile *tf)
668{ 856{
669 return (tf->command == ATA_CMD_READ_MULTI) || 857 return (tf->command == ATA_CMD_READ_MULTI) ||
@@ -682,7 +870,7 @@ static inline int ata_ok(u8 status)
682static inline int lba_28_ok(u64 block, u32 n_block) 870static inline int lba_28_ok(u64 block, u32 n_block)
683{ 871{
684 /* check the ending block number */ 872 /* check the ending block number */
685 return ((block + n_block - 1) < ((u64)1 << 28)) && (n_block <= 256); 873 return ((block + n_block) < ((u64)1 << 28)) && (n_block <= 256);
686} 874}
687 875
688static inline int lba_48_ok(u64 block, u32 n_block) 876static inline int lba_48_ok(u64 block, u32 n_block)