.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-only |
---|
1 | 2 | /* |
---|
2 | 3 | * scsi_error.c Copyright (C) 1997 Eric Youngdale |
---|
3 | 4 | * |
---|
.. | .. |
---|
115 | 116 | return 1; |
---|
116 | 117 | } |
---|
117 | 118 | |
---|
| 119 | +static bool scsi_cmd_retry_allowed(struct scsi_cmnd *cmd) |
---|
| 120 | +{ |
---|
| 121 | + if (cmd->allowed == SCSI_CMD_RETRIES_NO_LIMIT) |
---|
| 122 | + return true; |
---|
| 123 | + |
---|
| 124 | + return ++cmd->retries <= cmd->allowed; |
---|
| 125 | +} |
---|
| 126 | + |
---|
118 | 127 | /** |
---|
119 | 128 | * scmd_eh_abort_handler - Handle command aborts |
---|
120 | 129 | * @work: command to be aborted. |
---|
.. | .. |
---|
150 | 159 | "eh timeout, not retrying " |
---|
151 | 160 | "aborted command\n")); |
---|
152 | 161 | } else if (!scsi_noretry_cmd(scmd) && |
---|
153 | | - (++scmd->retries <= scmd->allowed)) { |
---|
| 162 | + scsi_cmd_retry_allowed(scmd)) { |
---|
154 | 163 | SCSI_LOG_ERROR_RECOVERY(3, |
---|
155 | 164 | scmd_printk(KERN_WARNING, scmd, |
---|
156 | 165 | "retry aborted command\n")); |
---|
.. | .. |
---|
297 | 306 | |
---|
298 | 307 | if (rtn == BLK_EH_DONE) { |
---|
299 | 308 | /* |
---|
300 | | - * For blk-mq, we must set the request state to complete now |
---|
301 | | - * before sending the request to the scsi error handler. This |
---|
302 | | - * will prevent a use-after-free in the event the LLD manages |
---|
303 | | - * to complete the request before the error handler finishes |
---|
304 | | - * processing this timed out request. |
---|
305 | | - * |
---|
306 | | - * If the request was already completed, then the LLD beat the |
---|
307 | | - * time out handler from transferring the request to the scsi |
---|
308 | | - * error handler. In that case we can return immediately as no |
---|
309 | | - * further action is required. |
---|
| 309 | + * If scsi_done() has already set SCMD_STATE_COMPLETE, do not |
---|
| 310 | + * modify *scmd. |
---|
310 | 311 | */ |
---|
311 | | - if (req->q->mq_ops && !blk_mq_mark_complete(req)) |
---|
312 | | - return rtn; |
---|
| 312 | + if (test_and_set_bit(SCMD_STATE_COMPLETE, &scmd->state)) |
---|
| 313 | + return BLK_EH_DONE; |
---|
313 | 314 | if (scsi_abort_command(scmd) != SUCCESS) { |
---|
314 | 315 | set_host_byte(scmd, DID_TIME_OUT); |
---|
315 | 316 | scsi_eh_scmd_add(scmd); |
---|
.. | .. |
---|
337 | 338 | wait_event(sdev->host->host_wait, !scsi_host_in_recovery(sdev->host)); |
---|
338 | 339 | |
---|
339 | 340 | online = scsi_device_online(sdev); |
---|
340 | | - |
---|
341 | | - SCSI_LOG_ERROR_RECOVERY(5, sdev_printk(KERN_INFO, sdev, |
---|
342 | | - "%s: rtn: %d\n", __func__, online)); |
---|
343 | 341 | |
---|
344 | 342 | return online; |
---|
345 | 343 | } |
---|
.. | .. |
---|
601 | 599 | set_host_byte(scmd, DID_ALLOC_FAILURE); |
---|
602 | 600 | return SUCCESS; |
---|
603 | 601 | } |
---|
604 | | - /* FALLTHROUGH */ |
---|
| 602 | + fallthrough; |
---|
605 | 603 | case COPY_ABORTED: |
---|
606 | 604 | case VOLUME_OVERFLOW: |
---|
607 | 605 | case MISCOMPARE: |
---|
.. | .. |
---|
623 | 621 | return ADD_TO_MLQUEUE; |
---|
624 | 622 | else |
---|
625 | 623 | set_host_byte(scmd, DID_TARGET_FAILURE); |
---|
626 | | - /* FALLTHROUGH */ |
---|
| 624 | + fallthrough; |
---|
627 | 625 | |
---|
628 | 626 | case ILLEGAL_REQUEST: |
---|
629 | 627 | if (sshdr.asc == 0x20 || /* Invalid command operation code */ |
---|
.. | .. |
---|
736 | 734 | switch (status_byte(scmd->result)) { |
---|
737 | 735 | case GOOD: |
---|
738 | 736 | scsi_handle_queue_ramp_up(scmd->device); |
---|
739 | | - /* FALLTHROUGH */ |
---|
| 737 | + fallthrough; |
---|
740 | 738 | case COMMAND_TERMINATED: |
---|
741 | 739 | return SUCCESS; |
---|
742 | 740 | case CHECK_CONDITION: |
---|
.. | .. |
---|
757 | 755 | return FAILED; |
---|
758 | 756 | case QUEUE_FULL: |
---|
759 | 757 | scsi_handle_queue_full(scmd->device); |
---|
760 | | - /* fall through */ |
---|
| 758 | + fallthrough; |
---|
761 | 759 | case BUSY: |
---|
762 | 760 | return NEEDS_RETRY; |
---|
763 | 761 | default: |
---|
.. | .. |
---|
968 | 966 | ses->cmnd = scmd->cmnd; |
---|
969 | 967 | ses->data_direction = scmd->sc_data_direction; |
---|
970 | 968 | ses->sdb = scmd->sdb; |
---|
971 | | - ses->next_rq = scmd->request->next_rq; |
---|
972 | 969 | ses->result = scmd->result; |
---|
973 | 970 | ses->resid_len = scmd->req.resid_len; |
---|
974 | 971 | ses->underflow = scmd->underflow; |
---|
.. | .. |
---|
980 | 977 | scmd->cmnd = ses->eh_cmnd; |
---|
981 | 978 | memset(scmd->cmnd, 0, BLK_MAX_CDB); |
---|
982 | 979 | memset(&scmd->sdb, 0, sizeof(scmd->sdb)); |
---|
983 | | - scmd->request->next_rq = NULL; |
---|
984 | 980 | scmd->result = 0; |
---|
985 | 981 | scmd->req.resid_len = 0; |
---|
986 | 982 | |
---|
.. | .. |
---|
1034 | 1030 | scmd->cmnd = ses->cmnd; |
---|
1035 | 1031 | scmd->sc_data_direction = ses->data_direction; |
---|
1036 | 1032 | scmd->sdb = ses->sdb; |
---|
1037 | | - scmd->request->next_rq = ses->next_rq; |
---|
1038 | 1033 | scmd->result = ses->result; |
---|
1039 | 1034 | scmd->req.resid_len = ses->resid_len; |
---|
1040 | 1035 | scmd->underflow = ses->underflow; |
---|
.. | .. |
---|
1063 | 1058 | struct scsi_device *sdev = scmd->device; |
---|
1064 | 1059 | struct Scsi_Host *shost = sdev->host; |
---|
1065 | 1060 | DECLARE_COMPLETION_ONSTACK(done); |
---|
1066 | | - unsigned long timeleft = timeout; |
---|
| 1061 | + unsigned long timeleft = timeout, delay; |
---|
1067 | 1062 | struct scsi_eh_save ses; |
---|
1068 | 1063 | const unsigned long stall_for = msecs_to_jiffies(100); |
---|
1069 | 1064 | int rtn; |
---|
.. | .. |
---|
1074 | 1069 | |
---|
1075 | 1070 | scsi_log_send(scmd); |
---|
1076 | 1071 | scmd->scsi_done = scsi_eh_done; |
---|
1077 | | - rtn = shost->hostt->queuecommand(shost, scmd); |
---|
| 1072 | + |
---|
| 1073 | + /* |
---|
| 1074 | + * Lock sdev->state_mutex to avoid that scsi_device_quiesce() can |
---|
| 1075 | + * change the SCSI device state after we have examined it and before |
---|
| 1076 | + * .queuecommand() is called. |
---|
| 1077 | + */ |
---|
| 1078 | + mutex_lock(&sdev->state_mutex); |
---|
| 1079 | + while (sdev->sdev_state == SDEV_BLOCK && timeleft > 0) { |
---|
| 1080 | + mutex_unlock(&sdev->state_mutex); |
---|
| 1081 | + SCSI_LOG_ERROR_RECOVERY(5, sdev_printk(KERN_DEBUG, sdev, |
---|
| 1082 | + "%s: state %d <> %d\n", __func__, sdev->sdev_state, |
---|
| 1083 | + SDEV_BLOCK)); |
---|
| 1084 | + delay = min(timeleft, stall_for); |
---|
| 1085 | + timeleft -= delay; |
---|
| 1086 | + msleep(jiffies_to_msecs(delay)); |
---|
| 1087 | + mutex_lock(&sdev->state_mutex); |
---|
| 1088 | + } |
---|
| 1089 | + if (sdev->sdev_state != SDEV_BLOCK) |
---|
| 1090 | + rtn = shost->hostt->queuecommand(shost, scmd); |
---|
| 1091 | + else |
---|
| 1092 | + rtn = SCSI_MLQUEUE_DEVICE_BUSY; |
---|
| 1093 | + mutex_unlock(&sdev->state_mutex); |
---|
| 1094 | + |
---|
1078 | 1095 | if (rtn) { |
---|
1079 | 1096 | if (timeleft > stall_for) { |
---|
1080 | 1097 | scsi_eh_restore_cmnd(scmd, &ses); |
---|
.. | .. |
---|
1247 | 1264 | * upper level. |
---|
1248 | 1265 | */ |
---|
1249 | 1266 | if (rtn == SUCCESS) |
---|
1250 | | - /* we don't want this command reissued, just |
---|
1251 | | - * finished with the sense data, so set |
---|
1252 | | - * retries to the max allowed to ensure it |
---|
1253 | | - * won't get reissued */ |
---|
1254 | | - scmd->retries = scmd->allowed; |
---|
| 1267 | + /* |
---|
| 1268 | + * We don't want this command reissued, just finished |
---|
| 1269 | + * with the sense data, so set retries to the max |
---|
| 1270 | + * allowed to ensure it won't get reissued. If the user |
---|
| 1271 | + * has requested infinite retries, we also want to |
---|
| 1272 | + * finish this command, so force completion by setting |
---|
| 1273 | + * retries and allowed to the same value. |
---|
| 1274 | + */ |
---|
| 1275 | + if (scmd->allowed == SCSI_CMD_RETRIES_NO_LIMIT) |
---|
| 1276 | + scmd->retries = scmd->allowed = 1; |
---|
| 1277 | + else |
---|
| 1278 | + scmd->retries = scmd->allowed; |
---|
1255 | 1279 | else if (rtn != NEEDS_RETRY) |
---|
1256 | 1280 | continue; |
---|
1257 | 1281 | |
---|
.. | .. |
---|
1285 | 1309 | case NEEDS_RETRY: |
---|
1286 | 1310 | if (retry_cnt--) |
---|
1287 | 1311 | goto retry_tur; |
---|
1288 | | - /*FALLTHRU*/ |
---|
| 1312 | + fallthrough; |
---|
1289 | 1313 | case SUCCESS: |
---|
1290 | 1314 | return 0; |
---|
1291 | 1315 | default: |
---|
.. | .. |
---|
1395 | 1419 | sdev_printk(KERN_INFO, sdev, |
---|
1396 | 1420 | "%s: skip START_UNIT, past eh deadline\n", |
---|
1397 | 1421 | current->comm)); |
---|
| 1422 | + scsi_device_put(sdev); |
---|
1398 | 1423 | break; |
---|
1399 | 1424 | } |
---|
1400 | 1425 | stu_scmd = NULL; |
---|
.. | .. |
---|
1461 | 1486 | sdev_printk(KERN_INFO, sdev, |
---|
1462 | 1487 | "%s: skip BDR, past eh deadline\n", |
---|
1463 | 1488 | current->comm)); |
---|
| 1489 | + scsi_device_put(sdev); |
---|
1464 | 1490 | break; |
---|
1465 | 1491 | } |
---|
1466 | 1492 | bdr_scmd = NULL; |
---|
.. | .. |
---|
1720 | 1746 | if (msg_byte(scmd->result) == COMMAND_COMPLETE && |
---|
1721 | 1747 | status_byte(scmd->result) == RESERVATION_CONFLICT) |
---|
1722 | 1748 | return 0; |
---|
1723 | | - /* fall through */ |
---|
| 1749 | + fallthrough; |
---|
1724 | 1750 | case DID_SOFT_ERROR: |
---|
1725 | 1751 | return (scmd->request->cmd_flags & REQ_FAILFAST_DRIVER); |
---|
1726 | 1752 | } |
---|
.. | .. |
---|
1736 | 1762 | if (scmd->request->cmd_flags & REQ_FAILFAST_DEV || |
---|
1737 | 1763 | blk_rq_is_passthrough(scmd->request)) |
---|
1738 | 1764 | return 1; |
---|
1739 | | - else |
---|
1740 | | - return 0; |
---|
| 1765 | + |
---|
| 1766 | + return 0; |
---|
1741 | 1767 | } |
---|
1742 | 1768 | |
---|
1743 | 1769 | /** |
---|
.. | .. |
---|
1791 | 1817 | set_host_byte(scmd, DID_TIME_OUT); |
---|
1792 | 1818 | return SUCCESS; |
---|
1793 | 1819 | } |
---|
1794 | | - /* FALLTHROUGH */ |
---|
| 1820 | + fallthrough; |
---|
1795 | 1821 | case DID_NO_CONNECT: |
---|
1796 | 1822 | case DID_BAD_TARGET: |
---|
1797 | 1823 | /* |
---|
.. | .. |
---|
1835 | 1861 | * lower down |
---|
1836 | 1862 | */ |
---|
1837 | 1863 | break; |
---|
1838 | | - /* fallthrough */ |
---|
| 1864 | + fallthrough; |
---|
1839 | 1865 | case DID_BUS_BUSY: |
---|
1840 | 1866 | case DID_PARITY: |
---|
1841 | 1867 | goto maybe_retry; |
---|
.. | .. |
---|
1873 | 1899 | * the case of trying to send too many commands to a |
---|
1874 | 1900 | * tagged queueing device. |
---|
1875 | 1901 | */ |
---|
1876 | | - /* FALLTHROUGH */ |
---|
| 1902 | + fallthrough; |
---|
1877 | 1903 | case BUSY: |
---|
1878 | 1904 | /* |
---|
1879 | 1905 | * device can't talk to us at the moment. Should only |
---|
.. | .. |
---|
1886 | 1912 | if (scmd->cmnd[0] == REPORT_LUNS) |
---|
1887 | 1913 | scmd->device->sdev_target->expecting_lun_change = 0; |
---|
1888 | 1914 | scsi_handle_queue_ramp_up(scmd->device); |
---|
1889 | | - /* FALLTHROUGH */ |
---|
| 1915 | + fallthrough; |
---|
1890 | 1916 | case COMMAND_TERMINATED: |
---|
1891 | 1917 | return SUCCESS; |
---|
1892 | 1918 | case TASK_ABORTED: |
---|
.. | .. |
---|
1925 | 1951 | * the request was not marked fast fail. Note that above, |
---|
1926 | 1952 | * even if the request is marked fast fail, we still requeue |
---|
1927 | 1953 | * for queue congestion conditions (QUEUE_FULL or BUSY) */ |
---|
1928 | | - if ((++scmd->retries) <= scmd->allowed |
---|
1929 | | - && !scsi_noretry_cmd(scmd)) { |
---|
| 1954 | + if (scsi_cmd_retry_allowed(scmd) && !scsi_noretry_cmd(scmd)) { |
---|
1930 | 1955 | return NEEDS_RETRY; |
---|
1931 | 1956 | } else { |
---|
1932 | 1957 | /* |
---|
.. | .. |
---|
1938 | 1963 | |
---|
1939 | 1964 | static void eh_lock_door_done(struct request *req, blk_status_t status) |
---|
1940 | 1965 | { |
---|
1941 | | - __blk_put_request(req->q, req); |
---|
| 1966 | + blk_put_request(req); |
---|
1942 | 1967 | } |
---|
1943 | 1968 | |
---|
1944 | 1969 | /** |
---|
.. | .. |
---|
2072 | 2097 | list_for_each_entry_safe(scmd, next, done_q, eh_entry) { |
---|
2073 | 2098 | list_del_init(&scmd->eh_entry); |
---|
2074 | 2099 | if (scsi_device_online(scmd->device) && |
---|
2075 | | - !scsi_noretry_cmd(scmd) && |
---|
2076 | | - (++scmd->retries <= scmd->allowed)) { |
---|
| 2100 | + !scsi_noretry_cmd(scmd) && scsi_cmd_retry_allowed(scmd)) { |
---|
2077 | 2101 | SCSI_LOG_ERROR_RECOVERY(3, |
---|
2078 | 2102 | scmd_printk(KERN_INFO, scmd, |
---|
2079 | 2103 | "%s: flush retry cmd\n", |
---|
.. | .. |
---|
2357 | 2381 | rtn = scsi_try_bus_device_reset(scmd); |
---|
2358 | 2382 | if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE)) |
---|
2359 | 2383 | break; |
---|
2360 | | - /* FALLTHROUGH */ |
---|
| 2384 | + fallthrough; |
---|
2361 | 2385 | case SG_SCSI_RESET_TARGET: |
---|
2362 | 2386 | rtn = scsi_try_target_reset(scmd); |
---|
2363 | 2387 | if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE)) |
---|
2364 | 2388 | break; |
---|
2365 | | - /* FALLTHROUGH */ |
---|
| 2389 | + fallthrough; |
---|
2366 | 2390 | case SG_SCSI_RESET_BUS: |
---|
2367 | 2391 | rtn = scsi_try_bus_reset(scmd); |
---|
2368 | 2392 | if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE)) |
---|
2369 | 2393 | break; |
---|
2370 | | - /* FALLTHROUGH */ |
---|
| 2394 | + fallthrough; |
---|
2371 | 2395 | case SG_SCSI_RESET_HOST: |
---|
2372 | 2396 | rtn = scsi_try_host_reset(scmd); |
---|
2373 | 2397 | if (rtn == SUCCESS) |
---|
2374 | 2398 | break; |
---|
2375 | | - /* FALLTHROUGH */ |
---|
| 2399 | + fallthrough; |
---|
2376 | 2400 | default: |
---|
2377 | 2401 | rtn = FAILED; |
---|
2378 | 2402 | break; |
---|
.. | .. |
---|
2395 | 2419 | wake_up(&shost->host_wait); |
---|
2396 | 2420 | scsi_run_host_queues(shost); |
---|
2397 | 2421 | |
---|
2398 | | - scsi_put_command(scmd); |
---|
2399 | 2422 | kfree(rq); |
---|
2400 | 2423 | |
---|
2401 | 2424 | out_put_autopm_host: |
---|
2402 | 2425 | scsi_autopm_put_host(shost); |
---|
2403 | 2426 | return error; |
---|
2404 | 2427 | } |
---|
2405 | | -EXPORT_SYMBOL(scsi_ioctl_reset); |
---|
2406 | 2428 | |
---|
2407 | 2429 | bool scsi_command_normalize_sense(const struct scsi_cmnd *cmd, |
---|
2408 | 2430 | struct scsi_sense_hdr *sshdr) |
---|