aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/scsi/libata-scsi.c
diff options
context:
space:
mode:
authorJeff Garzik <jgarzik@pobox.com>2005-10-09 10:40:44 -0400
committerJeff Garzik <jgarzik@pobox.com>2005-10-09 10:40:44 -0400
commitffe75ef650e1aec26a84bd82a620b5e27df9ed34 (patch)
tree8ed31fa093cbf6f6d6142e0e2118bee26aa71fa7 /drivers/scsi/libata-scsi.c
parent9dc263239bb2cfeb242d8ef758fca66a968822d5 (diff)
parente710245bb0f980adfb1dfe850e43761a8117c6be (diff)
Merge branch 'upstream'
Diffstat (limited to 'drivers/scsi/libata-scsi.c')
-rw-r--r--drivers/scsi/libata-scsi.c664
1 files changed, 494 insertions, 170 deletions
diff --git a/drivers/scsi/libata-scsi.c b/drivers/scsi/libata-scsi.c
index 1b00bc618e15..698203bf4f69 100644
--- a/drivers/scsi/libata-scsi.c
+++ b/drivers/scsi/libata-scsi.c
@@ -52,6 +52,14 @@ static struct ata_device *
52ata_scsi_find_dev(struct ata_port *ap, struct scsi_device *scsidev); 52ata_scsi_find_dev(struct ata_port *ap, struct scsi_device *scsidev);
53 53
54 54
55static void ata_scsi_invalid_field(struct scsi_cmnd *cmd,
56 void (*done)(struct scsi_cmnd *))
57{
58 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x24, 0x0);
59 /* "Invalid field in cbd" */
60 done(cmd);
61}
62
55/** 63/**
56 * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd. 64 * ata_std_bios_param - generic bios head/sector/cylinder calculator used by sd.
57 * @sdev: SCSI device for which BIOS geometry is to be determined 65 * @sdev: SCSI device for which BIOS geometry is to be determined
@@ -385,6 +393,7 @@ void ata_to_sense_error(unsigned id, u8 drv_stat, u8 drv_err, u8 *sk, u8 *asc,
385 u8 *ascq) 393 u8 *ascq)
386{ 394{
387 int i; 395 int i;
396
388 /* Based on the 3ware driver translation table */ 397 /* Based on the 3ware driver translation table */
389 static unsigned char sense_table[][4] = { 398 static unsigned char sense_table[][4] = {
390 /* BBD|ECC|ID|MAR */ 399 /* BBD|ECC|ID|MAR */
@@ -596,10 +605,7 @@ void ata_gen_fixed_sense(struct ata_queued_cmd *qc)
596 sb[0] = 0x70; 605 sb[0] = 0x70;
597 sb[7] = 0x0a; 606 sb[7] = 0x0a;
598 607
599#if 0 /* when C/H/S support is merged */
600 if (tf->flags & ATA_TFLAG_LBA && !(tf->flags & ATA_TFLAG_LBA48)) { 608 if (tf->flags & ATA_TFLAG_LBA && !(tf->flags & ATA_TFLAG_LBA48)) {
601#endif
602 if (!(tf->flags & ATA_TFLAG_LBA48)) {
603 /* A small (28b) LBA will fit in the 32b info field */ 609 /* A small (28b) LBA will fit in the 32b info field */
604 sb[0] |= 0x80; /* set valid bit */ 610 sb[0] |= 0x80; /* set valid bit */
605 sb[3] = tf->device & 0x0f; 611 sb[3] = tf->device & 0x0f;
@@ -714,15 +720,26 @@ static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc,
714 ; /* ignore IMMED bit, violates sat-r05 */ 720 ; /* ignore IMMED bit, violates sat-r05 */
715 } 721 }
716 if (scsicmd[4] & 0x2) 722 if (scsicmd[4] & 0x2)
717 return 1; /* LOEJ bit set not supported */ 723 goto invalid_fld; /* LOEJ bit set not supported */
718 if (((scsicmd[4] >> 4) & 0xf) != 0) 724 if (((scsicmd[4] >> 4) & 0xf) != 0)
719 return 1; /* power conditions not supported */ 725 goto invalid_fld; /* power conditions not supported */
720 if (scsicmd[4] & 0x1) { 726 if (scsicmd[4] & 0x1) {
721 tf->nsect = 1; /* 1 sector, lba=0 */ 727 tf->nsect = 1; /* 1 sector, lba=0 */
722 tf->lbah = 0x0; 728
723 tf->lbam = 0x0; 729 if (qc->dev->flags & ATA_DFLAG_LBA) {
724 tf->lbal = 0x0; 730 qc->tf.flags |= ATA_TFLAG_LBA;
725 tf->device |= ATA_LBA; 731
732 tf->lbah = 0x0;
733 tf->lbam = 0x0;
734 tf->lbal = 0x0;
735 tf->device |= ATA_LBA;
736 } else {
737 /* CHS */
738 tf->lbal = 0x1; /* sect */
739 tf->lbam = 0x0; /* cyl low */
740 tf->lbah = 0x0; /* cyl high */
741 }
742
726 tf->command = ATA_CMD_VERIFY; /* READ VERIFY */ 743 tf->command = ATA_CMD_VERIFY; /* READ VERIFY */
727 } else { 744 } else {
728 tf->nsect = 0; /* time period value (0 implies now) */ 745 tf->nsect = 0; /* time period value (0 implies now) */
@@ -737,6 +754,11 @@ static unsigned int ata_scsi_start_stop_xlat(struct ata_queued_cmd *qc,
737 */ 754 */
738 755
739 return 0; 756 return 0;
757
758invalid_fld:
759 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
760 /* "Invalid field in cbd" */
761 return 1;
740} 762}
741 763
742 764
@@ -772,6 +794,99 @@ static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
772} 794}
773 795
774/** 796/**
797 * scsi_6_lba_len - Get LBA and transfer length
798 * @scsicmd: SCSI command to translate
799 *
800 * Calculate LBA and transfer length for 6-byte commands.
801 *
802 * RETURNS:
803 * @plba: the LBA
804 * @plen: the transfer length
805 */
806
807static void scsi_6_lba_len(u8 *scsicmd, u64 *plba, u32 *plen)
808{
809 u64 lba = 0;
810 u32 len = 0;
811
812 VPRINTK("six-byte command\n");
813
814 lba |= ((u64)scsicmd[2]) << 8;
815 lba |= ((u64)scsicmd[3]);
816
817 len |= ((u32)scsicmd[4]);
818
819 *plba = lba;
820 *plen = len;
821}
822
823/**
824 * scsi_10_lba_len - Get LBA and transfer length
825 * @scsicmd: SCSI command to translate
826 *
827 * Calculate LBA and transfer length for 10-byte commands.
828 *
829 * RETURNS:
830 * @plba: the LBA
831 * @plen: the transfer length
832 */
833
834static void scsi_10_lba_len(u8 *scsicmd, u64 *plba, u32 *plen)
835{
836 u64 lba = 0;
837 u32 len = 0;
838
839 VPRINTK("ten-byte command\n");
840
841 lba |= ((u64)scsicmd[2]) << 24;
842 lba |= ((u64)scsicmd[3]) << 16;
843 lba |= ((u64)scsicmd[4]) << 8;
844 lba |= ((u64)scsicmd[5]);
845
846 len |= ((u32)scsicmd[7]) << 8;
847 len |= ((u32)scsicmd[8]);
848
849 *plba = lba;
850 *plen = len;
851}
852
853/**
854 * scsi_16_lba_len - Get LBA and transfer length
855 * @scsicmd: SCSI command to translate
856 *
857 * Calculate LBA and transfer length for 16-byte commands.
858 *
859 * RETURNS:
860 * @plba: the LBA
861 * @plen: the transfer length
862 */
863
864static void scsi_16_lba_len(u8 *scsicmd, u64 *plba, u32 *plen)
865{
866 u64 lba = 0;
867 u32 len = 0;
868
869 VPRINTK("sixteen-byte command\n");
870
871 lba |= ((u64)scsicmd[2]) << 56;
872 lba |= ((u64)scsicmd[3]) << 48;
873 lba |= ((u64)scsicmd[4]) << 40;
874 lba |= ((u64)scsicmd[5]) << 32;
875 lba |= ((u64)scsicmd[6]) << 24;
876 lba |= ((u64)scsicmd[7]) << 16;
877 lba |= ((u64)scsicmd[8]) << 8;
878 lba |= ((u64)scsicmd[9]);
879
880 len |= ((u32)scsicmd[10]) << 24;
881 len |= ((u32)scsicmd[11]) << 16;
882 len |= ((u32)scsicmd[12]) << 8;
883 len |= ((u32)scsicmd[13]);
884
885 *plba = lba;
886 *plen = len;
887}
888
889/**
775 * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one 890 * ata_scsi_verify_xlat - Translate SCSI VERIFY command into an ATA one
776 * @qc: Storage for translated ATA taskfile 891 * @qc: Storage for translated ATA taskfile
777 * @scsicmd: SCSI command to translate 892 * @scsicmd: SCSI command to translate
@@ -788,79 +903,102 @@ static unsigned int ata_scsi_flush_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
788static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc, u8 *scsicmd) 903static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
789{ 904{
790 struct ata_taskfile *tf = &qc->tf; 905 struct ata_taskfile *tf = &qc->tf;
906 struct ata_device *dev = qc->dev;
907 unsigned int lba = tf->flags & ATA_TFLAG_LBA;
791 unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48; 908 unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48;
792 u64 dev_sectors = qc->dev->n_sectors; 909 u64 dev_sectors = qc->dev->n_sectors;
793 u64 sect = 0; 910 u64 block;
794 u32 n_sect = 0; 911 u32 n_block;
795 912
796 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; 913 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
797 tf->protocol = ATA_PROT_NODATA; 914 tf->protocol = ATA_PROT_NODATA;
798 tf->device |= ATA_LBA;
799
800 if (scsicmd[0] == VERIFY) {
801 sect |= ((u64)scsicmd[2]) << 24;
802 sect |= ((u64)scsicmd[3]) << 16;
803 sect |= ((u64)scsicmd[4]) << 8;
804 sect |= ((u64)scsicmd[5]);
805
806 n_sect |= ((u32)scsicmd[7]) << 8;
807 n_sect |= ((u32)scsicmd[8]);
808 }
809
810 else if (scsicmd[0] == VERIFY_16) {
811 sect |= ((u64)scsicmd[2]) << 56;
812 sect |= ((u64)scsicmd[3]) << 48;
813 sect |= ((u64)scsicmd[4]) << 40;
814 sect |= ((u64)scsicmd[5]) << 32;
815 sect |= ((u64)scsicmd[6]) << 24;
816 sect |= ((u64)scsicmd[7]) << 16;
817 sect |= ((u64)scsicmd[8]) << 8;
818 sect |= ((u64)scsicmd[9]);
819
820 n_sect |= ((u32)scsicmd[10]) << 24;
821 n_sect |= ((u32)scsicmd[11]) << 16;
822 n_sect |= ((u32)scsicmd[12]) << 8;
823 n_sect |= ((u32)scsicmd[13]);
824 }
825 915
916 if (scsicmd[0] == VERIFY)
917 scsi_10_lba_len(scsicmd, &block, &n_block);
918 else if (scsicmd[0] == VERIFY_16)
919 scsi_16_lba_len(scsicmd, &block, &n_block);
826 else 920 else
827 return 1; 921 goto invalid_fld;
828 922
829 if (!n_sect) 923 if (!n_block)
830 return 1; 924 goto nothing_to_do;
831 if (sect >= dev_sectors) 925 if (block >= dev_sectors)
832 return 1; 926 goto out_of_range;
833 if ((sect + n_sect) > dev_sectors) 927 if ((block + n_block) > dev_sectors)
834 return 1; 928 goto out_of_range;
835 if (lba48) { 929 if (lba48) {
836 if (n_sect > (64 * 1024)) 930 if (n_block > (64 * 1024))
837 return 1; 931 goto invalid_fld;
838 } else { 932 } else {
839 if (n_sect > 256) 933 if (n_block > 256)
840 return 1; 934 goto invalid_fld;
841 } 935 }
842 936
843 if (lba48) { 937 if (lba) {
844 tf->command = ATA_CMD_VERIFY_EXT; 938 if (lba48) {
939 tf->command = ATA_CMD_VERIFY_EXT;
845 940
846 tf->hob_nsect = (n_sect >> 8) & 0xff; 941 tf->hob_nsect = (n_block >> 8) & 0xff;
847 942
848 tf->hob_lbah = (sect >> 40) & 0xff; 943 tf->hob_lbah = (block >> 40) & 0xff;
849 tf->hob_lbam = (sect >> 32) & 0xff; 944 tf->hob_lbam = (block >> 32) & 0xff;
850 tf->hob_lbal = (sect >> 24) & 0xff; 945 tf->hob_lbal = (block >> 24) & 0xff;
946 } else {
947 tf->command = ATA_CMD_VERIFY;
948
949 tf->device |= (block >> 24) & 0xf;
950 }
951
952 tf->nsect = n_block & 0xff;
953
954 tf->lbah = (block >> 16) & 0xff;
955 tf->lbam = (block >> 8) & 0xff;
956 tf->lbal = block & 0xff;
957
958 tf->device |= ATA_LBA;
851 } else { 959 } else {
960 /* CHS */
961 u32 sect, head, cyl, track;
962
963 /* Convert LBA to CHS */
964 track = (u32)block / dev->sectors;
965 cyl = track / dev->heads;
966 head = track % dev->heads;
967 sect = (u32)block % dev->sectors + 1;
968
969 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
970 (u32)block, track, cyl, head, sect);
971
972 /* Check whether the converted CHS can fit.
973 Cylinder: 0-65535
974 Head: 0-15
975 Sector: 1-255*/
976 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
977 goto out_of_range;
978
852 tf->command = ATA_CMD_VERIFY; 979 tf->command = ATA_CMD_VERIFY;
853 980 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
854 tf->device |= (sect >> 24) & 0xf; 981 tf->lbal = sect;
982 tf->lbam = cyl;
983 tf->lbah = cyl >> 8;
984 tf->device |= head;
855 } 985 }
856 986
857 tf->nsect = n_sect & 0xff; 987 return 0;
988
989invalid_fld:
990 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
991 /* "Invalid field in cbd" */
992 return 1;
858 993
859 tf->lbah = (sect >> 16) & 0xff; 994out_of_range:
860 tf->lbam = (sect >> 8) & 0xff; 995 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x21, 0x0);
861 tf->lbal = sect & 0xff; 996 /* "Logical Block Address out of range" */
997 return 1;
862 998
863 return 0; 999nothing_to_do:
1000 qc->scsicmd->result = SAM_STAT_GOOD;
1001 return 1;
864} 1002}
865 1003
866/** 1004/**
@@ -886,11 +1024,14 @@ static unsigned int ata_scsi_verify_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
886static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, u8 *scsicmd) 1024static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
887{ 1025{
888 struct ata_taskfile *tf = &qc->tf; 1026 struct ata_taskfile *tf = &qc->tf;
1027 struct ata_device *dev = qc->dev;
1028 unsigned int lba = tf->flags & ATA_TFLAG_LBA;
889 unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48; 1029 unsigned int lba48 = tf->flags & ATA_TFLAG_LBA48;
1030 u64 block;
1031 u32 n_block;
890 1032
891 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE; 1033 tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
892 tf->protocol = qc->dev->xfer_protocol; 1034 tf->protocol = qc->dev->xfer_protocol;
893 tf->device |= ATA_LBA;
894 1035
895 if (scsicmd[0] == READ_10 || scsicmd[0] == READ_6 || 1036 if (scsicmd[0] == READ_10 || scsicmd[0] == READ_6 ||
896 scsicmd[0] == READ_16) { 1037 scsicmd[0] == READ_16) {
@@ -900,89 +1041,115 @@ static unsigned int ata_scsi_rw_xlat(struct ata_queued_cmd *qc, u8 *scsicmd)
900 tf->flags |= ATA_TFLAG_WRITE; 1041 tf->flags |= ATA_TFLAG_WRITE;
901 } 1042 }
902 1043
903 if (scsicmd[0] == READ_10 || scsicmd[0] == WRITE_10) { 1044 /* Calculate the SCSI LBA and transfer length. */
904 if (lba48) { 1045 switch (scsicmd[0]) {
905 tf->hob_nsect = scsicmd[7]; 1046 case READ_10:
906 tf->hob_lbal = scsicmd[2]; 1047 case WRITE_10:
907 1048 scsi_10_lba_len(scsicmd, &block, &n_block);
908 qc->nsect = ((unsigned int)scsicmd[7] << 8) | 1049 break;
909 scsicmd[8]; 1050 case READ_6:
910 } else { 1051 case WRITE_6:
911 /* if we don't support LBA48 addressing, the request 1052 scsi_6_lba_len(scsicmd, &block, &n_block);
912 * -may- be too large. */
913 if ((scsicmd[2] & 0xf0) || scsicmd[7])
914 return 1;
915
916 /* stores LBA27:24 in lower 4 bits of device reg */
917 tf->device |= scsicmd[2];
918 1053
919 qc->nsect = scsicmd[8]; 1054 /* for 6-byte r/w commands, transfer length 0
920 } 1055 * means 256 blocks of data, not 0 block.
1056 */
1057 if (!n_block)
1058 n_block = 256;
1059 break;
1060 case READ_16:
1061 case WRITE_16:
1062 scsi_16_lba_len(scsicmd, &block, &n_block);
1063 break;
1064 default:
1065 DPRINTK("no-byte command\n");
1066 goto invalid_fld;
1067 }
921 1068
922 tf->nsect = scsicmd[8]; 1069 /* Check and compose ATA command */
923 tf->lbal = scsicmd[5]; 1070 if (!n_block)
924 tf->lbam = scsicmd[4]; 1071 /* For 10-byte and 16-byte SCSI R/W commands, transfer
925 tf->lbah = scsicmd[3]; 1072 * length 0 means transfer 0 block of data.
1073 * However, for ATA R/W commands, sector count 0 means
1074 * 256 or 65536 sectors, not 0 sectors as in SCSI.
1075 */
1076 goto nothing_to_do;
926 1077
927 VPRINTK("ten-byte command\n"); 1078 if (lba) {
928 if (qc->nsect == 0) /* we don't support length==0 cmds */ 1079 if (lba48) {
929 return 1; 1080 /* The request -may- be too large for LBA48. */
930 return 0; 1081 if ((block >> 48) || (n_block > 65536))
931 } 1082 goto out_of_range;
932 1083
933 if (scsicmd[0] == READ_6 || scsicmd[0] == WRITE_6) { 1084 tf->hob_nsect = (n_block >> 8) & 0xff;
934 qc->nsect = tf->nsect = scsicmd[4];
935 if (!qc->nsect) {
936 qc->nsect = 256;
937 if (lba48)
938 tf->hob_nsect = 1;
939 }
940 1085
941 tf->lbal = scsicmd[3]; 1086 tf->hob_lbah = (block >> 40) & 0xff;
942 tf->lbam = scsicmd[2]; 1087 tf->hob_lbam = (block >> 32) & 0xff;
943 tf->lbah = scsicmd[1] & 0x1f; /* mask out reserved bits */ 1088 tf->hob_lbal = (block >> 24) & 0xff;
1089 } else {
1090 /* LBA28 */
944 1091
945 VPRINTK("six-byte command\n"); 1092 /* The request -may- be too large for LBA28. */
946 return 0; 1093 if ((block >> 28) || (n_block > 256))
947 } 1094 goto out_of_range;
948 1095
949 if (scsicmd[0] == READ_16 || scsicmd[0] == WRITE_16) { 1096 tf->device |= (block >> 24) & 0xf;
950 /* rule out impossible LBAs and sector counts */ 1097 }
951 if (scsicmd[2] || scsicmd[3] || scsicmd[10] || scsicmd[11])
952 return 1;
953 1098
954 if (lba48) { 1099 qc->nsect = n_block;
955 tf->hob_nsect = scsicmd[12]; 1100 tf->nsect = n_block & 0xff;
956 tf->hob_lbal = scsicmd[6];
957 tf->hob_lbam = scsicmd[5];
958 tf->hob_lbah = scsicmd[4];
959 1101
960 qc->nsect = ((unsigned int)scsicmd[12] << 8) | 1102 tf->lbah = (block >> 16) & 0xff;
961 scsicmd[13]; 1103 tf->lbam = (block >> 8) & 0xff;
962 } else { 1104 tf->lbal = block & 0xff;
963 /* once again, filter out impossible non-zero values */
964 if (scsicmd[4] || scsicmd[5] || scsicmd[12] ||
965 (scsicmd[6] & 0xf0))
966 return 1;
967 1105
968 /* stores LBA27:24 in lower 4 bits of device reg */ 1106 tf->device |= ATA_LBA;
969 tf->device |= scsicmd[6]; 1107 } else {
1108 /* CHS */
1109 u32 sect, head, cyl, track;
1110
1111 /* The request -may- be too large for CHS addressing. */
1112 if ((block >> 28) || (n_block > 256))
1113 goto out_of_range;
1114
1115 /* Convert LBA to CHS */
1116 track = (u32)block / dev->sectors;
1117 cyl = track / dev->heads;
1118 head = track % dev->heads;
1119 sect = (u32)block % dev->sectors + 1;
1120
1121 DPRINTK("block %u track %u cyl %u head %u sect %u\n",
1122 (u32)block, track, cyl, head, sect);
1123
1124 /* Check whether the converted CHS can fit.
1125 Cylinder: 0-65535
1126 Head: 0-15
1127 Sector: 1-255*/
1128 if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
1129 goto out_of_range;
1130
1131 qc->nsect = n_block;
1132 tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
1133 tf->lbal = sect;
1134 tf->lbam = cyl;
1135 tf->lbah = cyl >> 8;
1136 tf->device |= head;
1137 }
970 1138
971 qc->nsect = scsicmd[13]; 1139 return 0;
972 }
973 1140
974 tf->nsect = scsicmd[13]; 1141invalid_fld:
975 tf->lbal = scsicmd[9]; 1142 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x24, 0x0);
976 tf->lbam = scsicmd[8]; 1143 /* "Invalid field in cbd" */
977 tf->lbah = scsicmd[7]; 1144 return 1;
978 1145
979 VPRINTK("sixteen-byte command\n"); 1146out_of_range:
980 if (qc->nsect == 0) /* we don't support length==0 cmds */ 1147 ata_scsi_set_sense(qc->scsicmd, ILLEGAL_REQUEST, 0x21, 0x0);
981 return 1; 1148 /* "Logical Block Address out of range" */
982 return 0; 1149 return 1;
983 }
984 1150
985 DPRINTK("no-byte command\n"); 1151nothing_to_do:
1152 qc->scsicmd->result = SAM_STAT_GOOD;
986 return 1; 1153 return 1;
987} 1154}
988 1155
@@ -1040,6 +1207,12 @@ static int ata_scsi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
1040 * This function sets up an ata_queued_cmd structure for the 1207 * This function sets up an ata_queued_cmd structure for the
1041 * SCSI command, and sends that ata_queued_cmd to the hardware. 1208 * SCSI command, and sends that ata_queued_cmd to the hardware.
1042 * 1209 *
1210 * The xlat_func argument (actor) returns 0 if ready to execute
1211 * ATA command, else 1 to finish translation. If 1 is returned
1212 * then cmd->result (and possibly cmd->sense_buffer) are assumed
1213 * to be set reflecting an error condition or clean (early)
1214 * termination.
1215 *
1043 * LOCKING: 1216 * LOCKING:
1044 * spin_lock_irqsave(host_set lock) 1217 * spin_lock_irqsave(host_set lock)
1045 */ 1218 */
@@ -1056,7 +1229,7 @@ static void ata_scsi_translate(struct ata_port *ap, struct ata_device *dev,
1056 1229
1057 qc = ata_scsi_qc_new(ap, dev, cmd, done); 1230 qc = ata_scsi_qc_new(ap, dev, cmd, done);
1058 if (!qc) 1231 if (!qc)
1059 return; 1232 goto err_mem;
1060 1233
1061 /* data is present; dma-map it */ 1234 /* data is present; dma-map it */
1062 if (cmd->sc_data_direction == DMA_FROM_DEVICE || 1235 if (cmd->sc_data_direction == DMA_FROM_DEVICE ||
@@ -1064,7 +1237,7 @@ static void ata_scsi_translate(struct ata_port *ap, struct ata_device *dev,
1064 if (unlikely(cmd->request_bufflen < 1)) { 1237 if (unlikely(cmd->request_bufflen < 1)) {
1065 printk(KERN_WARNING "ata%u(%u): WARNING: zero len r/w req\n", 1238 printk(KERN_WARNING "ata%u(%u): WARNING: zero len r/w req\n",
1066 ap->id, dev->devno); 1239 ap->id, dev->devno);
1067 goto err_out; 1240 goto err_did;
1068 } 1241 }
1069 1242
1070 if (cmd->use_sg) 1243 if (cmd->use_sg)
@@ -1079,18 +1252,28 @@ static void ata_scsi_translate(struct ata_port *ap, struct ata_device *dev,
1079 qc->complete_fn = ata_scsi_qc_complete; 1252 qc->complete_fn = ata_scsi_qc_complete;
1080 1253
1081 if (xlat_func(qc, scsicmd)) 1254 if (xlat_func(qc, scsicmd))
1082 goto err_out; 1255 goto early_finish;
1256
1083 /* select device, send command to hardware */ 1257 /* select device, send command to hardware */
1084 if (ata_qc_issue(qc)) 1258 if (ata_qc_issue(qc))
1085 goto err_out; 1259 goto err_did;
1086 1260
1087 VPRINTK("EXIT\n"); 1261 VPRINTK("EXIT\n");
1088 return; 1262 return;
1089 1263
1090err_out: 1264early_finish:
1265 ata_qc_free(qc);
1266 done(cmd);
1267 DPRINTK("EXIT - early finish (good or error)\n");
1268 return;
1269
1270err_did:
1091 ata_qc_free(qc); 1271 ata_qc_free(qc);
1092 ata_bad_cdb(cmd, done); 1272err_mem:
1093 DPRINTK("EXIT - badcmd\n"); 1273 cmd->result = (DID_ERROR << 16);
1274 done(cmd);
1275 DPRINTK("EXIT - internal\n");
1276 return;
1094} 1277}
1095 1278
1096/** 1279/**
@@ -1157,7 +1340,8 @@ static inline void ata_scsi_rbuf_put(struct scsi_cmnd *cmd, u8 *buf)
1157 * Mapping the response buffer, calling the command's handler, 1340 * Mapping the response buffer, calling the command's handler,
1158 * and handling the handler's return value. This return value 1341 * and handling the handler's return value. This return value
1159 * indicates whether the handler wishes the SCSI command to be 1342 * indicates whether the handler wishes the SCSI command to be
1160 * completed successfully, or not. 1343 * completed successfully (0), or not (in which case cmd->result
1344 * and sense buffer are assumed to be set).
1161 * 1345 *
1162 * LOCKING: 1346 * LOCKING:
1163 * spin_lock_irqsave(host_set lock) 1347 * spin_lock_irqsave(host_set lock)
@@ -1176,12 +1360,9 @@ void ata_scsi_rbuf_fill(struct ata_scsi_args *args,
1176 rc = actor(args, rbuf, buflen); 1360 rc = actor(args, rbuf, buflen);
1177 ata_scsi_rbuf_put(cmd, rbuf); 1361 ata_scsi_rbuf_put(cmd, rbuf);
1178 1362
1179 if (rc) 1363 if (rc == 0)
1180 ata_bad_cdb(cmd, args->done);
1181 else {
1182 cmd->result = SAM_STAT_GOOD; 1364 cmd->result = SAM_STAT_GOOD;
1183 args->done(cmd); 1365 args->done(cmd);
1184 }
1185} 1366}
1186 1367
1187/** 1368/**
@@ -1487,8 +1668,16 @@ unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
1487 * in the same manner) 1668 * in the same manner)
1488 */ 1669 */
1489 page_control = scsicmd[2] >> 6; 1670 page_control = scsicmd[2] >> 6;
1490 if ((page_control != 0) && (page_control != 3)) 1671 switch (page_control) {
1491 return 1; 1672 case 0: /* current */
1673 break; /* supported */
1674 case 3: /* saved */
1675 goto saving_not_supp;
1676 case 1: /* changeable */
1677 case 2: /* defaults */
1678 default:
1679 goto invalid_fld;
1680 }
1492 1681
1493 if (six_byte) 1682 if (six_byte)
1494 output_len = 4; 1683 output_len = 4;
@@ -1519,7 +1708,7 @@ unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
1519 break; 1708 break;
1520 1709
1521 default: /* invalid page code */ 1710 default: /* invalid page code */
1522 return 1; 1711 goto invalid_fld;
1523 } 1712 }
1524 1713
1525 if (six_byte) { 1714 if (six_byte) {
@@ -1532,6 +1721,16 @@ unsigned int ata_scsiop_mode_sense(struct ata_scsi_args *args, u8 *rbuf,
1532 } 1721 }
1533 1722
1534 return 0; 1723 return 0;
1724
1725invalid_fld:
1726 ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x24, 0x0);
1727 /* "Invalid field in cbd" */
1728 return 1;
1729
1730saving_not_supp:
1731 ata_scsi_set_sense(args->cmd, ILLEGAL_REQUEST, 0x39, 0x0);
1732 /* "Saving parameters not supported" */
1733 return 1;
1535} 1734}
1536 1735
1537/** 1736/**
@@ -1554,10 +1753,20 @@ unsigned int ata_scsiop_read_cap(struct ata_scsi_args *args, u8 *rbuf,
1554 1753
1555 VPRINTK("ENTER\n"); 1754 VPRINTK("ENTER\n");
1556 1755
1557 if (ata_id_has_lba48(args->id)) 1756 if (ata_id_has_lba(args->id)) {
1558 n_sectors = ata_id_u64(args->id, 100); 1757 if (ata_id_has_lba48(args->id))
1559 else 1758 n_sectors = ata_id_u64(args->id, 100);
1560 n_sectors = ata_id_u32(args->id, 60); 1759 else
1760 n_sectors = ata_id_u32(args->id, 60);
1761 } else {
1762 /* CHS default translation */
1763 n_sectors = args->id[1] * args->id[3] * args->id[6];
1764
1765 if (ata_id_current_chs_valid(args->id))
1766 /* CHS current translation */
1767 n_sectors = ata_id_u32(args->id, 57);
1768 }
1769
1561 n_sectors--; /* ATA TotalUserSectors - 1 */ 1770 n_sectors--; /* ATA TotalUserSectors - 1 */
1562 1771
1563 if (args->cmd->cmnd[0] == READ_CAPACITY) { 1772 if (args->cmd->cmnd[0] == READ_CAPACITY) {
@@ -1621,6 +1830,34 @@ unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf,
1621} 1830}
1622 1831
1623/** 1832/**
1833 * ata_scsi_set_sense - Set SCSI sense data and status
1834 * @cmd: SCSI request to be handled
1835 * @sk: SCSI-defined sense key
1836 * @asc: SCSI-defined additional sense code
1837 * @ascq: SCSI-defined additional sense code qualifier
1838 *
1839 * Helper function that builds a valid fixed format, current
1840 * response code and the given sense key (sk), additional sense
1841 * code (asc) and additional sense code qualifier (ascq) with
1842 * a SCSI command status of %SAM_STAT_CHECK_CONDITION and
1843 * DRIVER_SENSE set in the upper bits of scsi_cmnd::result .
1844 *
1845 * LOCKING:
1846 * Not required
1847 */
1848
1849void ata_scsi_set_sense(struct scsi_cmnd *cmd, u8 sk, u8 asc, u8 ascq)
1850{
1851 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION;
1852
1853 cmd->sense_buffer[0] = 0x70; /* fixed format, current */
1854 cmd->sense_buffer[2] = sk;
1855 cmd->sense_buffer[7] = 18 - 8; /* additional sense length */
1856 cmd->sense_buffer[12] = asc;
1857 cmd->sense_buffer[13] = ascq;
1858}
1859
1860/**
1624 * ata_scsi_badcmd - End a SCSI request with an error 1861 * ata_scsi_badcmd - End a SCSI request with an error
1625 * @cmd: SCSI request to be handled 1862 * @cmd: SCSI request to be handled
1626 * @done: SCSI command completion function 1863 * @done: SCSI command completion function
@@ -1638,30 +1875,84 @@ unsigned int ata_scsiop_report_luns(struct ata_scsi_args *args, u8 *rbuf,
1638void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8 asc, u8 ascq) 1875void ata_scsi_badcmd(struct scsi_cmnd *cmd, void (*done)(struct scsi_cmnd *), u8 asc, u8 ascq)
1639{ 1876{
1640 DPRINTK("ENTER\n"); 1877 DPRINTK("ENTER\n");
1641 cmd->result = (DRIVER_SENSE << 24) | SAM_STAT_CHECK_CONDITION; 1878 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, asc, ascq);
1642
1643 cmd->sense_buffer[0] = 0x70;
1644 cmd->sense_buffer[2] = ILLEGAL_REQUEST;
1645 cmd->sense_buffer[7] = 14 - 8; /* addnl. sense len. FIXME: correct? */
1646 cmd->sense_buffer[12] = asc;
1647 cmd->sense_buffer[13] = ascq;
1648 1879
1649 done(cmd); 1880 done(cmd);
1650} 1881}
1651 1882
1883void atapi_request_sense(struct ata_port *ap, struct ata_device *dev,
1884 struct scsi_cmnd *cmd)
1885{
1886 DECLARE_COMPLETION(wait);
1887 struct ata_queued_cmd *qc;
1888 unsigned long flags;
1889 int rc;
1890
1891 DPRINTK("ATAPI request sense\n");
1892
1893 qc = ata_qc_new_init(ap, dev);
1894 BUG_ON(qc == NULL);
1895
1896 /* FIXME: is this needed? */
1897 memset(cmd->sense_buffer, 0, sizeof(cmd->sense_buffer));
1898
1899 ata_sg_init_one(qc, cmd->sense_buffer, sizeof(cmd->sense_buffer));
1900 qc->dma_dir = DMA_FROM_DEVICE;
1901
1902 memset(&qc->cdb, 0, ap->cdb_len);
1903 qc->cdb[0] = REQUEST_SENSE;
1904 qc->cdb[4] = SCSI_SENSE_BUFFERSIZE;
1905
1906 qc->tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
1907 qc->tf.command = ATA_CMD_PACKET;
1908
1909 qc->tf.protocol = ATA_PROT_ATAPI;
1910 qc->tf.lbam = (8 * 1024) & 0xff;
1911 qc->tf.lbah = (8 * 1024) >> 8;
1912 qc->nbytes = SCSI_SENSE_BUFFERSIZE;
1913
1914 qc->waiting = &wait;
1915 qc->complete_fn = ata_qc_complete_noop;
1916
1917 spin_lock_irqsave(&ap->host_set->lock, flags);
1918 rc = ata_qc_issue(qc);
1919 spin_unlock_irqrestore(&ap->host_set->lock, flags);
1920
1921 if (rc)
1922 ata_port_disable(ap);
1923 else
1924 wait_for_completion(&wait);
1925
1926 DPRINTK("EXIT\n");
1927}
1928
1652static int atapi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat) 1929static int atapi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
1653{ 1930{
1654 struct scsi_cmnd *cmd = qc->scsicmd; 1931 struct scsi_cmnd *cmd = qc->scsicmd;
1655 1932
1656 if (unlikely(drv_stat & (ATA_ERR | ATA_BUSY | ATA_DRQ))) { 1933 VPRINTK("ENTER, drv_stat == 0x%x\n", drv_stat);
1934
1935 if (unlikely(drv_stat & (ATA_BUSY | ATA_DRQ)))
1936 ata_to_sense_error(qc, drv_stat);
1937
1938 else if (unlikely(drv_stat & ATA_ERR)) {
1657 DPRINTK("request check condition\n"); 1939 DPRINTK("request check condition\n");
1658 1940
1941 /* FIXME: command completion with check condition
1942 * but no sense causes the error handler to run,
1943 * which then issues REQUEST SENSE, fills in the sense
1944 * buffer, and completes the command (for the second
1945 * time). We need to issue REQUEST SENSE some other
1946 * way, to avoid completing the command twice.
1947 */
1659 cmd->result = SAM_STAT_CHECK_CONDITION; 1948 cmd->result = SAM_STAT_CHECK_CONDITION;
1660 1949
1661 qc->scsidone(cmd); 1950 qc->scsidone(cmd);
1662 1951
1663 return 1; 1952 return 1;
1664 } else { 1953 }
1954
1955 else {
1665 u8 *scsicmd = cmd->cmnd; 1956 u8 *scsicmd = cmd->cmnd;
1666 1957
1667 if (scsicmd[0] == INQUIRY) { 1958 if (scsicmd[0] == INQUIRY) {
@@ -1669,15 +1960,30 @@ static int atapi_qc_complete(struct ata_queued_cmd *qc, u8 drv_stat)
1669 unsigned int buflen; 1960 unsigned int buflen;
1670 1961
1671 buflen = ata_scsi_rbuf_get(cmd, &buf); 1962 buflen = ata_scsi_rbuf_get(cmd, &buf);
1672 buf[2] = 0x5; 1963
1673 buf[3] = (buf[3] & 0xf0) | 2; 1964 /* ATAPI devices typically report zero for their SCSI version,
1965 * and sometimes deviate from the spec WRT response data
1966 * format. If SCSI version is reported as zero like normal,
1967 * then we make the following fixups: 1) Fake MMC-5 version,
1968 * to indicate to the Linux scsi midlayer this is a modern
1969 * device. 2) Ensure response data format / ATAPI information
1970 * are always correct.
1971 */
1972 /* FIXME: do we ever override EVPD pages and the like, with
1973 * this code?
1974 */
1975 if (buf[2] == 0) {
1976 buf[2] = 0x5;
1977 buf[3] = 0x32;
1978 }
1979
1674 ata_scsi_rbuf_put(cmd, buf); 1980 ata_scsi_rbuf_put(cmd, buf);
1675 } 1981 }
1982
1676 cmd->result = SAM_STAT_GOOD; 1983 cmd->result = SAM_STAT_GOOD;
1677 } 1984 }
1678 1985
1679 qc->scsidone(cmd); 1986 qc->scsidone(cmd);
1680
1681 return 0; 1987 return 0;
1682} 1988}
1683/** 1989/**
@@ -2080,7 +2386,7 @@ void ata_scsi_simulate(u16 *id,
2080 2386
2081 case INQUIRY: 2387 case INQUIRY:
2082 if (scsicmd[1] & 2) /* is CmdDt set? */ 2388 if (scsicmd[1] & 2) /* is CmdDt set? */
2083 ata_bad_cdb(cmd, done); 2389 ata_scsi_invalid_field(cmd, done);
2084 else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */ 2390 else if ((scsicmd[1] & 1) == 0) /* is EVPD clear? */
2085 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std); 2391 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_std);
2086 else if (scsicmd[2] == 0x00) 2392 else if (scsicmd[2] == 0x00)
@@ -2090,7 +2396,7 @@ void ata_scsi_simulate(u16 *id,
2090 else if (scsicmd[2] == 0x83) 2396 else if (scsicmd[2] == 0x83)
2091 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83); 2397 ata_scsi_rbuf_fill(&args, ata_scsiop_inq_83);
2092 else 2398 else
2093 ata_bad_cdb(cmd, done); 2399 ata_scsi_invalid_field(cmd, done);
2094 break; 2400 break;
2095 2401
2096 case MODE_SENSE: 2402 case MODE_SENSE:
@@ -2100,7 +2406,7 @@ void ata_scsi_simulate(u16 *id,
2100 2406
2101 case MODE_SELECT: /* unconditionally return */ 2407 case MODE_SELECT: /* unconditionally return */
2102 case MODE_SELECT_10: /* bad-field-in-cdb */ 2408 case MODE_SELECT_10: /* bad-field-in-cdb */
2103 ata_bad_cdb(cmd, done); 2409 ata_scsi_invalid_field(cmd, done);
2104 break; 2410 break;
2105 2411
2106 case READ_CAPACITY: 2412 case READ_CAPACITY:
@@ -2111,7 +2417,7 @@ void ata_scsi_simulate(u16 *id,
2111 if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16) 2417 if ((scsicmd[1] & 0x1f) == SAI_READ_CAPACITY_16)
2112 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap); 2418 ata_scsi_rbuf_fill(&args, ata_scsiop_read_cap);
2113 else 2419 else
2114 ata_bad_cdb(cmd, done); 2420 ata_scsi_invalid_field(cmd, done);
2115 break; 2421 break;
2116 2422
2117 case REPORT_LUNS: 2423 case REPORT_LUNS:
@@ -2123,8 +2429,26 @@ void ata_scsi_simulate(u16 *id,
2123 2429
2124 /* all other commands */ 2430 /* all other commands */
2125 default: 2431 default:
2126 ata_bad_scsiop(cmd, done); 2432 ata_scsi_set_sense(cmd, ILLEGAL_REQUEST, 0x20, 0x0);
2433 /* "Invalid command operation code" */
2434 done(cmd);
2127 break; 2435 break;
2128 } 2436 }
2129} 2437}
2130 2438
2439void ata_scsi_scan_host(struct ata_port *ap)
2440{
2441 struct ata_device *dev;
2442 unsigned int i;
2443
2444 if (ap->flags & ATA_FLAG_PORT_DISABLED)
2445 return;
2446
2447 for (i = 0; i < ATA_MAX_DEVICES; i++) {
2448 dev = &ap->device[i];
2449
2450 if (ata_dev_present(dev))
2451 scsi_scan_target(&ap->host->shost_gendev, 0, i, 0, 0);
2452 }
2453}
2454