17 #include <linux/module.h>
18 #include <linux/sched.h>
21 #include <linux/string.h>
22 #include <linux/kernel.h>
29 #include <scsi/scsi.h>
45 static void scsi_eh_done(
struct scsi_cmnd *scmd);
47 #define SENSE_TIMEOUT (10*HZ)
53 #define BUS_RESET_SETTLE_TIME (10)
54 #define HOST_RESET_SETTLE_TIME (10)
56 static int scsi_eh_try_stu(
struct scsi_cmnd *scmd);
62 trace_scsi_eh_wakeup(shost);
65 printk(
"Waking error handler thread\n"));
87 spin_unlock_irqrestore(shost->
host_lock, flags);
119 spin_unlock_irqrestore(shost->
host_lock, flags);
136 enum blk_eh_timer_return rtn = BLK_EH_NOT_HANDLED;
139 trace_scsi_dispatch_cmd_timeout(scmd);
144 else if (host->
hostt->eh_timed_out)
145 rtn = host->
hostt->eh_timed_out(scmd);
149 if (
unlikely(rtn == BLK_EH_NOT_HANDLED &&
151 rtn = BLK_EH_HANDLED;
173 online = scsi_device_online(sdev);
182 #ifdef CONFIG_SCSI_LOGGING
188 static inline void scsi_eh_prt_fail_stats(
struct Scsi_Host *
shost,
193 int total_failures = 0;
196 int devices_failed = 0;
200 if (scmd->
device == sdev) {
209 if (cmd_cancel || cmd_failed) {
212 "%s: cmds failed: %d, cancel: %d\n",
213 __func__, cmd_failed,
222 " devices require eh work\n",
223 total_failures, devices_failed));
238 static int scsi_check_sense(
struct scsi_cmnd *scmd)
254 if (scsi_sense_is_deferred(&sshdr))
261 rc = sdev->
scsi_dh_data->scsi_dh->check_sense(sdev, &sshdr);
271 if (sshdr.response_code == 0x70) {
281 if ((sshdr.additional_length > 3) &&
287 switch (sshdr.sense_key) {
294 if (sshdr.asc == 0x10)
306 if (scmd->
device->expecting_cc_ua) {
313 if (sshdr.asc != 0x28 || sshdr.ascq != 0x00) {
314 scmd->
device->expecting_cc_ua = 0;
322 if ((sshdr.asc == 0x04) && (sshdr.ascq == 0x01))
328 if (scmd->
device->allow_restart &&
329 (sshdr.asc == 0x04) && (sshdr.ascq == 0x02))
332 if (sshdr.asc == 0x3f && sshdr.ascq == 0x0e)
334 "Warning! Received an indication that the "
335 "LUN assignments on this target have "
336 "changed. The Linux SCSI layer does not "
337 "automatically remap LUN assignments.\n");
338 else if (sshdr.asc == 0x3f)
340 "Warning! Received an indication that the "
341 "operating parameters on this target have "
342 "changed. The Linux SCSI layer does not "
343 "automatically adjust these parameters.\n");
345 if (sshdr.asc == 0x38 && sshdr.ascq == 0x07)
347 "Warning! Received an indication that the "
348 "LUN reached a thin provisioning soft "
366 if (sshdr.asc == 0x11 ||
374 if (scmd->
device->retry_hwerror)
380 if (sshdr.asc == 0x20 ||
393 static void scsi_handle_queue_ramp_up(
struct scsi_device *sdev)
416 tmp_sdev->
id != sdev->
id ||
429 static void scsi_handle_queue_full(
struct scsi_device *sdev)
439 tmp_sdev->
id != sdev->
id)
461 static int scsi_eh_completed_normally(
struct scsi_cmnd *scmd)
474 return scsi_check_sense(scmd);
491 scsi_handle_queue_ramp_up(scmd->
device);
495 return scsi_check_sense(scmd);
511 scsi_handle_queue_full(scmd->
device);
525 static void scsi_eh_done(
struct scsi_cmnd *scmd)
530 printk(
"%s scmd: %p result: %x\n",
531 __func__, scmd, scmd->
result));
533 eh_action = scmd->
device->host->eh_action;
542 static int scsi_try_host_reset(
struct scsi_cmnd *scmd)
562 spin_unlock_irqrestore(host->
host_lock, flags);
572 static int scsi_try_bus_reset(
struct scsi_cmnd *scmd)
592 spin_unlock_irqrestore(host->
host_lock, flags);
598 static void __scsi_report_device_reset(
struct scsi_device *sdev,
void *
data)
614 static int scsi_try_target_reset(
struct scsi_cmnd *scmd)
628 __scsi_report_device_reset);
629 spin_unlock_irqrestore(host->
host_lock, flags);
645 static int scsi_try_bus_device_reset(
struct scsi_cmnd *scmd)
655 __scsi_report_device_reset(scmd->
device,
NULL);
667 static void scsi_abort_eh_cmnd(
struct scsi_cmnd *scmd)
669 if (scsi_try_to_abort_cmd(scmd->
device->host->hostt, scmd) !=
SUCCESS)
670 if (scsi_try_bus_device_reset(scmd) !=
SUCCESS)
671 if (scsi_try_target_reset(scmd) !=
SUCCESS)
672 if (scsi_try_bus_reset(scmd) !=
SUCCESS)
673 scsi_try_host_reset(scmd);
691 unsigned char *cmnd,
int cmnd_size,
unsigned sense_bytes)
724 scmd->
sdb.table.nents = 1;
726 scmd->
cmnd[4] = scmd->
sdb.length;
731 BUG_ON(cmnd_size > BLK_MAX_CDB);
740 scmd->
cmnd[1] = (scmd->
cmnd[1] & 0x1f) |
741 (sdev->
lun << 5 & 0xe0);
788 static int scsi_send_eh_cmnd(
struct scsi_cmnd *scmd,
unsigned char *cmnd,
789 int cmnd_size,
int timeout,
unsigned sense_bytes)
794 unsigned long timeleft;
803 shost->
hostt->queuecommand(shost, scmd);
809 scsi_log_completion(scmd,
SUCCESS);
812 printk(
"%s: scmd: %p, timeleft: %ld\n",
813 __func__, scmd, timeleft));
822 rtn = scsi_eh_completed_normally(scmd);
824 printk(
"%s: scsi_eh_completed_normally %x\n",
841 scsi_abort_eh_cmnd(scmd);
847 if (scmd->
request->cmd_type != REQ_TYPE_BLOCK_PC) {
848 struct scsi_driver *sdrv = scsi_cmd_to_driver(scmd);
850 rtn = sdrv->
eh_action(scmd, cmnd, cmnd_size, rtn);
865 static int scsi_request_sense(
struct scsi_cmnd *scmd)
884 scmd->
device->host->host_failed--;
886 list_move_tail(&scmd->
eh_entry, done_q);
922 "%s: requesting sense\n",
924 rtn = scsi_request_sense(scmd);
929 " result %x\n", scmd,
951 return list_empty(work_q);
962 static int scsi_eh_tur(
struct scsi_cmnd *scmd)
964 static unsigned char tur_command[6] = {
TEST_UNIT_READY, 0, 0, 0, 0, 0};
965 int retry_cnt = 1, rtn;
968 rtn = scsi_send_eh_cmnd(scmd, tur_command, 6,
SENSE_TIMEOUT, 0);
971 __func__, scmd, rtn));
998 static int scsi_eh_test_devices(
struct list_head *cmd_list,
1006 while (!list_empty(cmd_list)) {
1010 finish_cmds = !scsi_device_online(scmd->
device) ||
1011 (try_stu && !scsi_eh_try_stu(scmd) &&
1012 !scsi_eh_tur(scmd)) ||
1020 list_move_tail(&scmd->
eh_entry, work_q);
1023 return list_empty(work_q);
1039 static int scsi_eh_abort_cmds(
struct list_head *work_q,
1052 rtn = scsi_try_to_abort_cmd(scmd->
device->host->hostt, scmd);
1058 list_move_tail(&scmd->
eh_entry, &check_list);
1067 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
1077 static int scsi_eh_try_stu(
struct scsi_cmnd *scmd)
1079 static unsigned char stu_command[6] = {
START_STOP, 0, 0, 0, 1, 0};
1081 if (scmd->
device->allow_restart) {
1085 rtn = scsi_send_eh_cmnd(scmd, stu_command, 6, scmd->
device->request_queue->rq_timeout, 0);
1104 static int scsi_eh_stu(
struct Scsi_Host *shost,
1115 scsi_check_sense(scmd) ==
FAILED ) {
1124 " 0x%p\n",
current->comm, sdev));
1126 if (!scsi_eh_try_stu(stu_scmd)) {
1127 if (!scsi_device_online(sdev) ||
1128 !scsi_eh_tur(stu_scmd)) {
1131 if (scmd->
device == sdev)
1137 printk(
"%s: START_UNIT failed to sdev:"
1138 " 0x%p\n",
current->comm, sdev));
1142 return list_empty(work_q);
1158 static int scsi_eh_bus_device_reset(
struct Scsi_Host *shost,
1180 rtn = scsi_try_bus_device_reset(bdr_scmd);
1182 if (!scsi_device_online(sdev) ||
1184 !scsi_eh_tur(bdr_scmd)) {
1187 if (scmd->
device == sdev)
1201 return list_empty(work_q);
1213 static int scsi_eh_target_reset(
struct Scsi_Host *shost,
1220 list_splice_init(work_q, &tmp_list);
1222 while (!list_empty(&tmp_list)) {
1233 rtn = scsi_try_target_reset(scmd);
1244 list_move_tail(&scmd->
eh_entry, &check_list);
1249 list_move(&scmd->
eh_entry, work_q);
1253 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
1262 static int scsi_eh_bus_reset(
struct Scsi_Host *shost,
1278 for (channel = 0; channel <= shost->
max_channel; channel++) {
1296 rtn = scsi_try_bus_reset(chan_scmd);
1310 " failed chan: %d\n",
1315 return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
1323 static int scsi_eh_host_reset(
struct list_head *work_q,
1330 if (!list_empty(work_q)) {
1337 rtn = scsi_try_host_reset(scmd);
1339 list_splice_init(work_q, &check_list);
1350 return scsi_eh_test_devices(&check_list, work_q, done_q, 1);
1358 static void scsi_eh_offline_sdevs(
struct list_head *work_q,
1365 "not ready after error recovery\n");
1406 scmd->
request->cmd_type == REQ_TYPE_BLOCK_PC)
1435 if (!scsi_device_online(scmd->
device)) {
1453 scmd->
result &= 0xff00ffff;
1541 scsi_handle_queue_full(scmd->
device);
1555 scsi_handle_queue_ramp_up(scmd->
device);
1561 rtn = scsi_check_sense(scmd);
1588 "reservation conflict\n");
1613 static void eh_lock_door_done(
struct request *
req,
int uptodate)
1629 static void scsi_eh_lock_door(
struct scsi_device *sdev)
1648 req->cmd_type = REQ_TYPE_BLOCK_PC;
1650 req->timeout = 10 *
HZ;
1664 static void scsi_restart_operations(
struct Scsi_Host *shost)
1667 unsigned long flags;
1675 if (scsi_device_online(sdev) && sdev->
locked)
1676 scsi_eh_lock_door(sdev);
1691 spin_unlock_irqrestore(shost->
host_lock, flags);
1715 spin_unlock_irqrestore(shost->
host_lock, flags);
1728 if (!scsi_eh_stu(shost, work_q, done_q))
1729 if (!scsi_eh_bus_device_reset(shost, work_q, done_q))
1730 if (!scsi_eh_target_reset(shost, work_q, done_q))
1731 if (!scsi_eh_bus_reset(shost, work_q, done_q))
1732 if (!scsi_eh_host_reset(work_q, done_q))
1733 scsi_eh_offline_sdevs(work_q,
1748 if (scsi_device_online(scmd->
device) &&
1796 static void scsi_unjam_host(
struct Scsi_Host *shost)
1798 unsigned long flags;
1803 list_splice_init(&shost->
eh_cmd_q, &eh_work_q);
1804 spin_unlock_irqrestore(shost->
host_lock, flags);
1809 if (!scsi_eh_abort_cmds(&eh_work_q, &eh_done_q))
1838 printk(
"Error handler scsi_eh_%d sleeping\n",
1846 printk(
"Error handler scsi_eh_%d waking up\n",
1854 if (!shost->
eh_noresume && scsi_autopm_get_host(shost) != 0) {
1857 "unable to autoresume\n",
1863 shost->
transportt->eh_strategy_handler(shost);
1865 scsi_unjam_host(shost);
1874 scsi_restart_operations(shost);
1876 scsi_autopm_put_host(shost);
1881 printk(
"Error handler scsi_eh_%d exiting\n", shost->
host_no));
1912 if (channel == sdev_channel(sdev))
1913 __scsi_report_device_reset(sdev,
NULL);
1945 if (channel == sdev_channel(sdev) &&
1946 target == sdev_id(sdev))
1947 __scsi_report_device_reset(sdev,
NULL);
1953 scsi_reset_provider_done_command(
struct scsi_cmnd *scmd)
1976 unsigned long flags;
1979 if (scsi_autopm_get_host(shost) < 0)
1986 scmd->
cmnd = req.cmd;
1988 scmd->
scsi_done = scsi_reset_provider_done_command;
1997 spin_unlock_irqrestore(shost->
host_lock, flags);
2001 rtn = scsi_try_bus_device_reset(scmd);
2006 rtn = scsi_try_target_reset(scmd);
2011 rtn = scsi_try_bus_reset(scmd);
2016 rtn = scsi_try_host_reset(scmd);
2024 spin_unlock_irqrestore(shost->
host_lock, flags);
2031 printk(
"%s: waking up host to restart after TMF\n",
2039 scsi_autopm_put_host(shost);
2066 if (!sense_buffer || !sb_len)
2073 if (!scsi_sense_valid(sshdr))
2081 sshdr->
sense_key = (sense_buffer[1] & 0xf);
2083 sshdr->
asc = sense_buffer[2];
2085 sshdr->
ascq = sense_buffer[3];
2093 sshdr->
sense_key = (sense_buffer[2] & 0xf);
2095 sb_len = (sb_len < (sense_buffer[7] + 8)) ?
2096 sb_len : (sense_buffer[7] + 8);
2098 sshdr->
asc = sense_buffer[12];
2100 sshdr->
ascq = sense_buffer[13];
2132 int add_sen_len, add_len, desc_len,
k;
2135 if ((sb_len < 8) || (0 == (add_sen_len = sense_buffer[7])))
2137 if ((sense_buffer[0] < 0x72) || (sense_buffer[0] > 0x73))
2139 add_sen_len = (add_sen_len < (sb_len - 8)) ?
2140 add_sen_len : (sb_len - 8);
2141 descp = &sense_buffer[8];
2142 for (desc_len = 0, k = 0; k < add_sen_len; k += desc_len) {
2144 add_len = (k < (add_sen_len - 1)) ? descp[1]: -1;
2145 desc_len = add_len + 2;
2146 if (descp[0] == desc_type)
2174 switch (sense_buffer[0] & 0x7f) {
2177 if (sense_buffer[0] & 0x80) {
2178 *info_out = (sense_buffer[3] << 24) +
2179 (sense_buffer[4] << 16) +
2180 (sense_buffer[5] << 8) + sense_buffer[6];
2188 if (ucp && (0xa == ucp[1])) {
2190 for (j = 0; j < 8; ++
j) {