aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/scsi/scsi_lib.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/scsi/scsi_lib.c')
-rw-r--r--drivers/scsi/scsi_lib.c149
1 files changed, 86 insertions, 63 deletions
diff --git a/drivers/scsi/scsi_lib.c b/drivers/scsi/scsi_lib.c
index 148d3af92aef..f2f51e0333eb 100644
--- a/drivers/scsi/scsi_lib.c
+++ b/drivers/scsi/scsi_lib.c
@@ -183,13 +183,15 @@ int scsi_queue_insert(struct scsi_cmnd *cmd, int reason)
183 * @timeout: request timeout in seconds 183 * @timeout: request timeout in seconds
184 * @retries: number of times to retry request 184 * @retries: number of times to retry request
185 * @flags: or into request flags; 185 * @flags: or into request flags;
186 * @resid: optional residual length
186 * 187 *
187 * returns the req->errors value which is the scsi_cmnd result 188 * returns the req->errors value which is the scsi_cmnd result
188 * field. 189 * field.
189 */ 190 */
190int scsi_execute(struct scsi_device *sdev, const unsigned char *cmd, 191int scsi_execute(struct scsi_device *sdev, const unsigned char *cmd,
191 int data_direction, void *buffer, unsigned bufflen, 192 int data_direction, void *buffer, unsigned bufflen,
192 unsigned char *sense, int timeout, int retries, int flags) 193 unsigned char *sense, int timeout, int retries, int flags,
194 int *resid)
193{ 195{
194 struct request *req; 196 struct request *req;
195 int write = (data_direction == DMA_TO_DEVICE); 197 int write = (data_direction == DMA_TO_DEVICE);
@@ -224,6 +226,8 @@ int scsi_execute(struct scsi_device *sdev, const unsigned char *cmd,
224 if (unlikely(req->data_len > 0 && req->data_len <= bufflen)) 226 if (unlikely(req->data_len > 0 && req->data_len <= bufflen))
225 memset(buffer + (bufflen - req->data_len), 0, req->data_len); 227 memset(buffer + (bufflen - req->data_len), 0, req->data_len);
226 228
229 if (resid)
230 *resid = req->data_len;
227 ret = req->errors; 231 ret = req->errors;
228 out: 232 out:
229 blk_put_request(req); 233 blk_put_request(req);
@@ -235,7 +239,8 @@ EXPORT_SYMBOL(scsi_execute);
235 239
236int scsi_execute_req(struct scsi_device *sdev, const unsigned char *cmd, 240int scsi_execute_req(struct scsi_device *sdev, const unsigned char *cmd,
237 int data_direction, void *buffer, unsigned bufflen, 241 int data_direction, void *buffer, unsigned bufflen,
238 struct scsi_sense_hdr *sshdr, int timeout, int retries) 242 struct scsi_sense_hdr *sshdr, int timeout, int retries,
243 int *resid)
239{ 244{
240 char *sense = NULL; 245 char *sense = NULL;
241 int result; 246 int result;
@@ -246,7 +251,7 @@ int scsi_execute_req(struct scsi_device *sdev, const unsigned char *cmd,
246 return DRIVER_ERROR << 24; 251 return DRIVER_ERROR << 24;
247 } 252 }
248 result = scsi_execute(sdev, cmd, data_direction, buffer, bufflen, 253 result = scsi_execute(sdev, cmd, data_direction, buffer, bufflen,
249 sense, timeout, retries, 0); 254 sense, timeout, retries, 0, resid);
250 if (sshdr) 255 if (sshdr)
251 scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, sshdr); 256 scsi_normalize_sense(sense, SCSI_SENSE_BUFFERSIZE, sshdr);
252 257
@@ -875,16 +880,24 @@ static void scsi_end_bidi_request(struct scsi_cmnd *cmd)
875 * (the normal case for most drivers), we don't need 880 * (the normal case for most drivers), we don't need
876 * the logic to deal with cleaning up afterwards. 881 * the logic to deal with cleaning up afterwards.
877 * 882 *
878 * We must do one of several things here: 883 * We must call scsi_end_request(). This will finish off
884 * the specified number of sectors. If we are done, the
885 * command block will be released and the queue function
886 * will be goosed. If we are not done then we have to
887 * figure out what to do next:
879 * 888 *
880 * a) Call scsi_end_request. This will finish off the 889 * a) We can call scsi_requeue_command(). The request
881 * specified number of sectors. If we are done, the 890 * will be unprepared and put back on the queue. Then
882 * command block will be released, and the queue 891 * a new command will be created for it. This should
883 * function will be goosed. If we are not done, then 892 * be used if we made forward progress, or if we want
884 * scsi_end_request will directly goose the queue. 893 * to switch from READ(10) to READ(6) for example.
885 * 894 *
886 * b) We can just use scsi_requeue_command() here. This would 895 * b) We can call scsi_queue_insert(). The request will
887 * be used if we just wanted to retry, for example. 896 * be put back on the queue and retried using the same
897 * command as before, possibly after a delay.
898 *
899 * c) We can call blk_end_request() with -EIO to fail
900 * the remainder of the request.
888 */ 901 */
889void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes) 902void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes)
890{ 903{
@@ -896,6 +909,9 @@ void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes)
896 struct scsi_sense_hdr sshdr; 909 struct scsi_sense_hdr sshdr;
897 int sense_valid = 0; 910 int sense_valid = 0;
898 int sense_deferred = 0; 911 int sense_deferred = 0;
912 enum {ACTION_FAIL, ACTION_REPREP, ACTION_RETRY,
913 ACTION_DELAYED_RETRY} action;
914 char *description = NULL;
899 915
900 if (result) { 916 if (result) {
901 sense_valid = scsi_command_normalize_sense(cmd, &sshdr); 917 sense_valid = scsi_command_normalize_sense(cmd, &sshdr);
@@ -947,10 +963,13 @@ void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes)
947 return; 963 return;
948 this_count = blk_rq_bytes(req); 964 this_count = blk_rq_bytes(req);
949 965
950 /* good_bytes = 0, or (inclusive) there were leftovers and 966 if (host_byte(result) == DID_RESET) {
951 * result = 0, so scsi_end_request couldn't retry. 967 /* Third party bus reset or reset for error recovery
952 */ 968 * reasons. Just retry the command and see what
953 if (sense_valid && !sense_deferred) { 969 * happens.
970 */
971 action = ACTION_RETRY;
972 } else if (sense_valid && !sense_deferred) {
954 switch (sshdr.sense_key) { 973 switch (sshdr.sense_key) {
955 case UNIT_ATTENTION: 974 case UNIT_ATTENTION:
956 if (cmd->device->removable) { 975 if (cmd->device->removable) {
@@ -958,16 +977,15 @@ void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes)
958 * and quietly refuse further access. 977 * and quietly refuse further access.
959 */ 978 */
960 cmd->device->changed = 1; 979 cmd->device->changed = 1;
961 scsi_end_request(cmd, -EIO, this_count, 1); 980 description = "Media Changed";
962 return; 981 action = ACTION_FAIL;
963 } else { 982 } else {
964 /* Must have been a power glitch, or a 983 /* Must have been a power glitch, or a
965 * bus reset. Could not have been a 984 * bus reset. Could not have been a
966 * media change, so we just retry the 985 * media change, so we just retry the
967 * request and see what happens. 986 * command and see what happens.
968 */ 987 */
969 scsi_requeue_command(q, cmd); 988 action = ACTION_RETRY;
970 return;
971 } 989 }
972 break; 990 break;
973 case ILLEGAL_REQUEST: 991 case ILLEGAL_REQUEST:
@@ -983,21 +1001,18 @@ void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes)
983 sshdr.asc == 0x20 && sshdr.ascq == 0x00) && 1001 sshdr.asc == 0x20 && sshdr.ascq == 0x00) &&
984 (cmd->cmnd[0] == READ_10 || 1002 (cmd->cmnd[0] == READ_10 ||
985 cmd->cmnd[0] == WRITE_10)) { 1003 cmd->cmnd[0] == WRITE_10)) {
1004 /* This will issue a new 6-byte command. */
986 cmd->device->use_10_for_rw = 0; 1005 cmd->device->use_10_for_rw = 0;
987 /* This will cause a retry with a 1006 action = ACTION_REPREP;
988 * 6-byte command. 1007 } else
989 */ 1008 action = ACTION_FAIL;
990 scsi_requeue_command(q, cmd); 1009 break;
991 } else if (sshdr.asc == 0x10) /* DIX */
992 scsi_end_request(cmd, -EIO, this_count, 0);
993 else
994 scsi_end_request(cmd, -EIO, this_count, 1);
995 return;
996 case ABORTED_COMMAND: 1010 case ABORTED_COMMAND:
997 if (sshdr.asc == 0x10) { /* DIF */ 1011 if (sshdr.asc == 0x10) { /* DIF */
998 scsi_end_request(cmd, -EIO, this_count, 0); 1012 action = ACTION_FAIL;
999 return; 1013 description = "Data Integrity Failure";
1000 } 1014 } else
1015 action = ACTION_RETRY;
1001 break; 1016 break;
1002 case NOT_READY: 1017 case NOT_READY:
1003 /* If the device is in the process of becoming 1018 /* If the device is in the process of becoming
@@ -1012,49 +1027,57 @@ void scsi_io_completion(struct scsi_cmnd *cmd, unsigned int good_bytes)
1012 case 0x07: /* operation in progress */ 1027 case 0x07: /* operation in progress */
1013 case 0x08: /* Long write in progress */ 1028 case 0x08: /* Long write in progress */
1014 case 0x09: /* self test in progress */ 1029 case 0x09: /* self test in progress */
1015 scsi_requeue_command(q, cmd); 1030 action = ACTION_DELAYED_RETRY;
1016 return;
1017 default:
1018 break; 1031 break;
1019 } 1032 }
1033 } else {
1034 description = "Device not ready";
1035 action = ACTION_FAIL;
1020 } 1036 }
1021 if (!(req->cmd_flags & REQ_QUIET)) 1037 break;
1022 scsi_cmd_print_sense_hdr(cmd,
1023 "Device not ready",
1024 &sshdr);
1025
1026 scsi_end_request(cmd, -EIO, this_count, 1);
1027 return;
1028 case VOLUME_OVERFLOW: 1038 case VOLUME_OVERFLOW:
1029 if (!(req->cmd_flags & REQ_QUIET)) {
1030 scmd_printk(KERN_INFO, cmd,
1031 "Volume overflow, CDB: ");
1032 __scsi_print_command(cmd->cmnd);
1033 scsi_print_sense("", cmd);
1034 }
1035 /* See SSC3rXX or current. */ 1039 /* See SSC3rXX or current. */
1036 scsi_end_request(cmd, -EIO, this_count, 1); 1040 action = ACTION_FAIL;
1037 return; 1041 break;
1038 default: 1042 default:
1043 description = "Unhandled sense code";
1044 action = ACTION_FAIL;
1039 break; 1045 break;
1040 } 1046 }
1047 } else {
1048 description = "Unhandled error code";
1049 action = ACTION_FAIL;
1041 } 1050 }
1042 if (host_byte(result) == DID_RESET) { 1051
1043 /* Third party bus reset or reset for error recovery 1052 switch (action) {
1044 * reasons. Just retry the request and see what 1053 case ACTION_FAIL:
1045 * happens. 1054 /* Give up and fail the remainder of the request */
1046 */
1047 scsi_requeue_command(q, cmd);
1048 return;
1049 }
1050 if (result) {
1051 if (!(req->cmd_flags & REQ_QUIET)) { 1055 if (!(req->cmd_flags & REQ_QUIET)) {
1056 if (description)
1057 scmd_printk(KERN_INFO, cmd, "%s",
1058 description);
1052 scsi_print_result(cmd); 1059 scsi_print_result(cmd);
1053 if (driver_byte(result) & DRIVER_SENSE) 1060 if (driver_byte(result) & DRIVER_SENSE)
1054 scsi_print_sense("", cmd); 1061 scsi_print_sense("", cmd);
1055 } 1062 }
1063 blk_end_request(req, -EIO, blk_rq_bytes(req));
1064 scsi_next_command(cmd);
1065 break;
1066 case ACTION_REPREP:
1067 /* Unprep the request and put it back at the head of the queue.
1068 * A new command will be prepared and issued.
1069 */
1070 scsi_requeue_command(q, cmd);
1071 break;
1072 case ACTION_RETRY:
1073 /* Retry the same command immediately */
1074 scsi_queue_insert(cmd, SCSI_MLQUEUE_EH_RETRY);
1075 break;
1076 case ACTION_DELAYED_RETRY:
1077 /* Retry the same command after a delay */
1078 scsi_queue_insert(cmd, SCSI_MLQUEUE_DEVICE_BUSY);
1079 break;
1056 } 1080 }
1057 scsi_end_request(cmd, -EIO, this_count, !result);
1058} 1081}
1059 1082
1060static int scsi_init_sgtable(struct request *req, struct scsi_data_buffer *sdb, 1083static int scsi_init_sgtable(struct request *req, struct scsi_data_buffer *sdb,
@@ -1998,7 +2021,7 @@ scsi_mode_select(struct scsi_device *sdev, int pf, int sp, int modepage,
1998 } 2021 }
1999 2022
2000 ret = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, real_buffer, len, 2023 ret = scsi_execute_req(sdev, cmd, DMA_TO_DEVICE, real_buffer, len,
2001 sshdr, timeout, retries); 2024 sshdr, timeout, retries, NULL);
2002 kfree(real_buffer); 2025 kfree(real_buffer);
2003 return ret; 2026 return ret;
2004} 2027}
@@ -2063,7 +2086,7 @@ scsi_mode_sense(struct scsi_device *sdev, int dbd, int modepage,
2063 memset(buffer, 0, len); 2086 memset(buffer, 0, len);
2064 2087
2065 result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buffer, len, 2088 result = scsi_execute_req(sdev, cmd, DMA_FROM_DEVICE, buffer, len,
2066 sshdr, timeout, retries); 2089 sshdr, timeout, retries, NULL);
2067 2090
2068 /* This code looks awful: what it's doing is making sure an 2091 /* This code looks awful: what it's doing is making sure an
2069 * ILLEGAL REQUEST sense return identifies the actual command 2092 * ILLEGAL REQUEST sense return identifies the actual command
@@ -2145,7 +2168,7 @@ scsi_test_unit_ready(struct scsi_device *sdev, int timeout, int retries,
2145 /* try to eat the UNIT_ATTENTION if there are enough retries */ 2168 /* try to eat the UNIT_ATTENTION if there are enough retries */
2146 do { 2169 do {
2147 result = scsi_execute_req(sdev, cmd, DMA_NONE, NULL, 0, sshdr, 2170 result = scsi_execute_req(sdev, cmd, DMA_NONE, NULL, 0, sshdr,
2148 timeout, retries); 2171 timeout, retries, NULL);
2149 if (sdev->removable && scsi_sense_valid(sshdr) && 2172 if (sdev->removable && scsi_sense_valid(sshdr) &&
2150 sshdr->sense_key == UNIT_ATTENTION) 2173 sshdr->sense_key == UNIT_ATTENTION)
2151 sdev->changed = 1; 2174 sdev->changed = 1;