/drivers/scsi/scsi_error.c

http://github.com/mirrors/linux · C · 2468 lines · 1450 code · 253 blank · 765 comment · 387 complexity · eed5a215adcec46748f2f488268d2e94 MD5 · raw file

Large files are truncated click here to view the full file

  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * scsi_error.c Copyright (C) 1997 Eric Youngdale
  4. *
  5. * SCSI error/timeout handling
  6. * Initial versions: Eric Youngdale. Based upon conversations with
  7. * Leonard Zubkoff and David Miller at Linux Expo,
  8. * ideas originating from all over the place.
  9. *
  10. * Restructured scsi_unjam_host and associated functions.
  11. * September 04, 2002 Mike Anderson (andmike@us.ibm.com)
  12. *
  13. * Forward port of Russell King's (rmk@arm.linux.org.uk) changes and
  14. * minor cleanups.
  15. * September 30, 2002 Mike Anderson (andmike@us.ibm.com)
  16. */
  17. #include <linux/module.h>
  18. #include <linux/sched.h>
  19. #include <linux/gfp.h>
  20. #include <linux/timer.h>
  21. #include <linux/string.h>
  22. #include <linux/kernel.h>
  23. #include <linux/freezer.h>
  24. #include <linux/kthread.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/blkdev.h>
  27. #include <linux/delay.h>
  28. #include <linux/jiffies.h>
  29. #include <scsi/scsi.h>
  30. #include <scsi/scsi_cmnd.h>
  31. #include <scsi/scsi_dbg.h>
  32. #include <scsi/scsi_device.h>
  33. #include <scsi/scsi_driver.h>
  34. #include <scsi/scsi_eh.h>
  35. #include <scsi/scsi_common.h>
  36. #include <scsi/scsi_transport.h>
  37. #include <scsi/scsi_host.h>
  38. #include <scsi/scsi_ioctl.h>
  39. #include <scsi/scsi_dh.h>
  40. #include <scsi/scsi_devinfo.h>
  41. #include <scsi/sg.h>
  42. #include "scsi_priv.h"
  43. #include "scsi_logging.h"
  44. #include "scsi_transport_api.h"
  45. #include <trace/events/scsi.h>
  46. #include <asm/unaligned.h>
  47. static void scsi_eh_done(struct scsi_cmnd *scmd);
  48. /*
  49. * These should *probably* be handled by the host itself.
  50. * Since it is allowed to sleep, it probably should.
  51. */
  52. #define BUS_RESET_SETTLE_TIME (10)
  53. #define HOST_RESET_SETTLE_TIME (10)
  54. static int scsi_eh_try_stu(struct scsi_cmnd *scmd);
  55. static int scsi_try_to_abort_cmd(struct scsi_host_template *,
  56. struct scsi_cmnd *);
  57. void scsi_eh_wakeup(struct Scsi_Host *shost)
  58. {
  59. lockdep_assert_held(shost->host_lock);
  60. if (scsi_host_busy(shost) == shost->host_failed) {
  61. trace_scsi_eh_wakeup(shost);
  62. wake_up_process(shost->ehandler);
  63. SCSI_LOG_ERROR_RECOVERY(5, shost_printk(KERN_INFO, shost,
  64. "Waking error handler thread\n"));
  65. }
  66. }
  67. /**
  68. * scsi_schedule_eh - schedule EH for SCSI host
  69. * @shost: SCSI host to invoke error handling on.
  70. *
  71. * Schedule SCSI EH without scmd.
  72. */
  73. void scsi_schedule_eh(struct Scsi_Host *shost)
  74. {
  75. unsigned long flags;
  76. spin_lock_irqsave(shost->host_lock, flags);
  77. if (scsi_host_set_state(shost, SHOST_RECOVERY) == 0 ||
  78. scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY) == 0) {
  79. shost->host_eh_scheduled++;
  80. scsi_eh_wakeup(shost);
  81. }
  82. spin_unlock_irqrestore(shost->host_lock, flags);
  83. }
  84. EXPORT_SYMBOL_GPL(scsi_schedule_eh);
  85. static int scsi_host_eh_past_deadline(struct Scsi_Host *shost)
  86. {
  87. if (!shost->last_reset || shost->eh_deadline == -1)
  88. return 0;
  89. /*
  90. * 32bit accesses are guaranteed to be atomic
  91. * (on all supported architectures), so instead
  92. * of using a spinlock we can as well double check
  93. * if eh_deadline has been set to 'off' during the
  94. * time_before call.
  95. */
  96. if (time_before(jiffies, shost->last_reset + shost->eh_deadline) &&
  97. shost->eh_deadline > -1)
  98. return 0;
  99. return 1;
  100. }
  101. /**
  102. * scmd_eh_abort_handler - Handle command aborts
  103. * @work: command to be aborted.
  104. *
  105. * Note: this function must be called only for a command that has timed out.
  106. * Because the block layer marks a request as complete before it calls
  107. * scsi_times_out(), a .scsi_done() call from the LLD for a command that has
  108. * timed out do not have any effect. Hence it is safe to call
  109. * scsi_finish_command() from this function.
  110. */
  111. void
  112. scmd_eh_abort_handler(struct work_struct *work)
  113. {
  114. struct scsi_cmnd *scmd =
  115. container_of(work, struct scsi_cmnd, abort_work.work);
  116. struct scsi_device *sdev = scmd->device;
  117. int rtn;
  118. if (scsi_host_eh_past_deadline(sdev->host)) {
  119. SCSI_LOG_ERROR_RECOVERY(3,
  120. scmd_printk(KERN_INFO, scmd,
  121. "eh timeout, not aborting\n"));
  122. } else {
  123. SCSI_LOG_ERROR_RECOVERY(3,
  124. scmd_printk(KERN_INFO, scmd,
  125. "aborting command\n"));
  126. rtn = scsi_try_to_abort_cmd(sdev->host->hostt, scmd);
  127. if (rtn == SUCCESS) {
  128. set_host_byte(scmd, DID_TIME_OUT);
  129. if (scsi_host_eh_past_deadline(sdev->host)) {
  130. SCSI_LOG_ERROR_RECOVERY(3,
  131. scmd_printk(KERN_INFO, scmd,
  132. "eh timeout, not retrying "
  133. "aborted command\n"));
  134. } else if (!scsi_noretry_cmd(scmd) &&
  135. (++scmd->retries <= scmd->allowed)) {
  136. SCSI_LOG_ERROR_RECOVERY(3,
  137. scmd_printk(KERN_WARNING, scmd,
  138. "retry aborted command\n"));
  139. scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
  140. return;
  141. } else {
  142. SCSI_LOG_ERROR_RECOVERY(3,
  143. scmd_printk(KERN_WARNING, scmd,
  144. "finish aborted command\n"));
  145. scsi_finish_command(scmd);
  146. return;
  147. }
  148. } else {
  149. SCSI_LOG_ERROR_RECOVERY(3,
  150. scmd_printk(KERN_INFO, scmd,
  151. "cmd abort %s\n",
  152. (rtn == FAST_IO_FAIL) ?
  153. "not send" : "failed"));
  154. }
  155. }
  156. scsi_eh_scmd_add(scmd);
  157. }
  158. /**
  159. * scsi_abort_command - schedule a command abort
  160. * @scmd: scmd to abort.
  161. *
  162. * We only need to abort commands after a command timeout
  163. */
  164. static int
  165. scsi_abort_command(struct scsi_cmnd *scmd)
  166. {
  167. struct scsi_device *sdev = scmd->device;
  168. struct Scsi_Host *shost = sdev->host;
  169. unsigned long flags;
  170. if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) {
  171. /*
  172. * Retry after abort failed, escalate to next level.
  173. */
  174. SCSI_LOG_ERROR_RECOVERY(3,
  175. scmd_printk(KERN_INFO, scmd,
  176. "previous abort failed\n"));
  177. BUG_ON(delayed_work_pending(&scmd->abort_work));
  178. return FAILED;
  179. }
  180. spin_lock_irqsave(shost->host_lock, flags);
  181. if (shost->eh_deadline != -1 && !shost->last_reset)
  182. shost->last_reset = jiffies;
  183. spin_unlock_irqrestore(shost->host_lock, flags);
  184. scmd->eh_eflags |= SCSI_EH_ABORT_SCHEDULED;
  185. SCSI_LOG_ERROR_RECOVERY(3,
  186. scmd_printk(KERN_INFO, scmd, "abort scheduled\n"));
  187. queue_delayed_work(shost->tmf_work_q, &scmd->abort_work, HZ / 100);
  188. return SUCCESS;
  189. }
  190. /**
  191. * scsi_eh_reset - call into ->eh_action to reset internal counters
  192. * @scmd: scmd to run eh on.
  193. *
  194. * The scsi driver might be carrying internal state about the
  195. * devices, so we need to call into the driver to reset the
  196. * internal state once the error handler is started.
  197. */
  198. static void scsi_eh_reset(struct scsi_cmnd *scmd)
  199. {
  200. if (!blk_rq_is_passthrough(scmd->request)) {
  201. struct scsi_driver *sdrv = scsi_cmd_to_driver(scmd);
  202. if (sdrv->eh_reset)
  203. sdrv->eh_reset(scmd);
  204. }
  205. }
  206. static void scsi_eh_inc_host_failed(struct rcu_head *head)
  207. {
  208. struct scsi_cmnd *scmd = container_of(head, typeof(*scmd), rcu);
  209. struct Scsi_Host *shost = scmd->device->host;
  210. unsigned long flags;
  211. spin_lock_irqsave(shost->host_lock, flags);
  212. shost->host_failed++;
  213. scsi_eh_wakeup(shost);
  214. spin_unlock_irqrestore(shost->host_lock, flags);
  215. }
  216. /**
  217. * scsi_eh_scmd_add - add scsi cmd to error handling.
  218. * @scmd: scmd to run eh on.
  219. */
  220. void scsi_eh_scmd_add(struct scsi_cmnd *scmd)
  221. {
  222. struct Scsi_Host *shost = scmd->device->host;
  223. unsigned long flags;
  224. int ret;
  225. WARN_ON_ONCE(!shost->ehandler);
  226. spin_lock_irqsave(shost->host_lock, flags);
  227. if (scsi_host_set_state(shost, SHOST_RECOVERY)) {
  228. ret = scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY);
  229. WARN_ON_ONCE(ret);
  230. }
  231. if (shost->eh_deadline != -1 && !shost->last_reset)
  232. shost->last_reset = jiffies;
  233. scsi_eh_reset(scmd);
  234. list_add_tail(&scmd->eh_entry, &shost->eh_cmd_q);
  235. spin_unlock_irqrestore(shost->host_lock, flags);
  236. /*
  237. * Ensure that all tasks observe the host state change before the
  238. * host_failed change.
  239. */
  240. call_rcu(&scmd->rcu, scsi_eh_inc_host_failed);
  241. }
  242. /**
  243. * scsi_times_out - Timeout function for normal scsi commands.
  244. * @req: request that is timing out.
  245. *
  246. * Notes:
  247. * We do not need to lock this. There is the potential for a race
  248. * only in that the normal completion handling might run, but if the
  249. * normal completion function determines that the timer has already
  250. * fired, then it mustn't do anything.
  251. */
  252. enum blk_eh_timer_return scsi_times_out(struct request *req)
  253. {
  254. struct scsi_cmnd *scmd = blk_mq_rq_to_pdu(req);
  255. enum blk_eh_timer_return rtn = BLK_EH_DONE;
  256. struct Scsi_Host *host = scmd->device->host;
  257. trace_scsi_dispatch_cmd_timeout(scmd);
  258. scsi_log_completion(scmd, TIMEOUT_ERROR);
  259. if (host->eh_deadline != -1 && !host->last_reset)
  260. host->last_reset = jiffies;
  261. if (host->hostt->eh_timed_out)
  262. rtn = host->hostt->eh_timed_out(scmd);
  263. if (rtn == BLK_EH_DONE) {
  264. /*
  265. * Set the command to complete first in order to prevent a real
  266. * completion from releasing the command while error handling
  267. * is using it. If the command was already completed, then the
  268. * lower level driver beat the timeout handler, and it is safe
  269. * to return without escalating error recovery.
  270. *
  271. * If timeout handling lost the race to a real completion, the
  272. * block layer may ignore that due to a fake timeout injection,
  273. * so return RESET_TIMER to allow error handling another shot
  274. * at this command.
  275. */
  276. if (test_and_set_bit(SCMD_STATE_COMPLETE, &scmd->state))
  277. return BLK_EH_RESET_TIMER;
  278. if (scsi_abort_command(scmd) != SUCCESS) {
  279. set_host_byte(scmd, DID_TIME_OUT);
  280. scsi_eh_scmd_add(scmd);
  281. }
  282. }
  283. return rtn;
  284. }
  285. /**
  286. * scsi_block_when_processing_errors - Prevent cmds from being queued.
  287. * @sdev: Device on which we are performing recovery.
  288. *
  289. * Description:
  290. * We block until the host is out of error recovery, and then check to
  291. * see whether the host or the device is offline.
  292. *
  293. * Return value:
  294. * 0 when dev was taken offline by error recovery. 1 OK to proceed.
  295. */
  296. int scsi_block_when_processing_errors(struct scsi_device *sdev)
  297. {
  298. int online;
  299. wait_event(sdev->host->host_wait, !scsi_host_in_recovery(sdev->host));
  300. online = scsi_device_online(sdev);
  301. return online;
  302. }
  303. EXPORT_SYMBOL(scsi_block_when_processing_errors);
  304. #ifdef CONFIG_SCSI_LOGGING
  305. /**
  306. * scsi_eh_prt_fail_stats - Log info on failures.
  307. * @shost: scsi host being recovered.
  308. * @work_q: Queue of scsi cmds to process.
  309. */
  310. static inline void scsi_eh_prt_fail_stats(struct Scsi_Host *shost,
  311. struct list_head *work_q)
  312. {
  313. struct scsi_cmnd *scmd;
  314. struct scsi_device *sdev;
  315. int total_failures = 0;
  316. int cmd_failed = 0;
  317. int cmd_cancel = 0;
  318. int devices_failed = 0;
  319. shost_for_each_device(sdev, shost) {
  320. list_for_each_entry(scmd, work_q, eh_entry) {
  321. if (scmd->device == sdev) {
  322. ++total_failures;
  323. if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED)
  324. ++cmd_cancel;
  325. else
  326. ++cmd_failed;
  327. }
  328. }
  329. if (cmd_cancel || cmd_failed) {
  330. SCSI_LOG_ERROR_RECOVERY(3,
  331. shost_printk(KERN_INFO, shost,
  332. "%s: cmds failed: %d, cancel: %d\n",
  333. __func__, cmd_failed,
  334. cmd_cancel));
  335. cmd_cancel = 0;
  336. cmd_failed = 0;
  337. ++devices_failed;
  338. }
  339. }
  340. SCSI_LOG_ERROR_RECOVERY(2, shost_printk(KERN_INFO, shost,
  341. "Total of %d commands on %d"
  342. " devices require eh work\n",
  343. total_failures, devices_failed));
  344. }
  345. #endif
  346. /**
  347. * scsi_report_lun_change - Set flag on all *other* devices on the same target
  348. * to indicate that a UNIT ATTENTION is expected.
  349. * @sdev: Device reporting the UNIT ATTENTION
  350. */
  351. static void scsi_report_lun_change(struct scsi_device *sdev)
  352. {
  353. sdev->sdev_target->expecting_lun_change = 1;
  354. }
  355. /**
  356. * scsi_report_sense - Examine scsi sense information and log messages for
  357. * certain conditions, also issue uevents for some of them.
  358. * @sdev: Device reporting the sense code
  359. * @sshdr: sshdr to be examined
  360. */
  361. static void scsi_report_sense(struct scsi_device *sdev,
  362. struct scsi_sense_hdr *sshdr)
  363. {
  364. enum scsi_device_event evt_type = SDEV_EVT_MAXBITS; /* i.e. none */
  365. if (sshdr->sense_key == UNIT_ATTENTION) {
  366. if (sshdr->asc == 0x3f && sshdr->ascq == 0x03) {
  367. evt_type = SDEV_EVT_INQUIRY_CHANGE_REPORTED;
  368. sdev_printk(KERN_WARNING, sdev,
  369. "Inquiry data has changed");
  370. } else if (sshdr->asc == 0x3f && sshdr->ascq == 0x0e) {
  371. evt_type = SDEV_EVT_LUN_CHANGE_REPORTED;
  372. scsi_report_lun_change(sdev);
  373. sdev_printk(KERN_WARNING, sdev,
  374. "Warning! Received an indication that the "
  375. "LUN assignments on this target have "
  376. "changed. The Linux SCSI layer does not "
  377. "automatically remap LUN assignments.\n");
  378. } else if (sshdr->asc == 0x3f)
  379. sdev_printk(KERN_WARNING, sdev,
  380. "Warning! Received an indication that the "
  381. "operating parameters on this target have "
  382. "changed. The Linux SCSI layer does not "
  383. "automatically adjust these parameters.\n");
  384. if (sshdr->asc == 0x38 && sshdr->ascq == 0x07) {
  385. evt_type = SDEV_EVT_SOFT_THRESHOLD_REACHED_REPORTED;
  386. sdev_printk(KERN_WARNING, sdev,
  387. "Warning! Received an indication that the "
  388. "LUN reached a thin provisioning soft "
  389. "threshold.\n");
  390. }
  391. if (sshdr->asc == 0x29) {
  392. evt_type = SDEV_EVT_POWER_ON_RESET_OCCURRED;
  393. sdev_printk(KERN_WARNING, sdev,
  394. "Power-on or device reset occurred\n");
  395. }
  396. if (sshdr->asc == 0x2a && sshdr->ascq == 0x01) {
  397. evt_type = SDEV_EVT_MODE_PARAMETER_CHANGE_REPORTED;
  398. sdev_printk(KERN_WARNING, sdev,
  399. "Mode parameters changed");
  400. } else if (sshdr->asc == 0x2a && sshdr->ascq == 0x06) {
  401. evt_type = SDEV_EVT_ALUA_STATE_CHANGE_REPORTED;
  402. sdev_printk(KERN_WARNING, sdev,
  403. "Asymmetric access state changed");
  404. } else if (sshdr->asc == 0x2a && sshdr->ascq == 0x09) {
  405. evt_type = SDEV_EVT_CAPACITY_CHANGE_REPORTED;
  406. sdev_printk(KERN_WARNING, sdev,
  407. "Capacity data has changed");
  408. } else if (sshdr->asc == 0x2a)
  409. sdev_printk(KERN_WARNING, sdev,
  410. "Parameters changed");
  411. }
  412. if (evt_type != SDEV_EVT_MAXBITS) {
  413. set_bit(evt_type, sdev->pending_events);
  414. schedule_work(&sdev->event_work);
  415. }
  416. }
  417. /**
  418. * scsi_check_sense - Examine scsi cmd sense
  419. * @scmd: Cmd to have sense checked.
  420. *
  421. * Return value:
  422. * SUCCESS or FAILED or NEEDS_RETRY or ADD_TO_MLQUEUE
  423. *
  424. * Notes:
  425. * When a deferred error is detected the current command has
  426. * not been executed and needs retrying.
  427. */
  428. int scsi_check_sense(struct scsi_cmnd *scmd)
  429. {
  430. struct scsi_device *sdev = scmd->device;
  431. struct scsi_sense_hdr sshdr;
  432. if (! scsi_command_normalize_sense(scmd, &sshdr))
  433. return FAILED; /* no valid sense data */
  434. scsi_report_sense(sdev, &sshdr);
  435. if (scsi_sense_is_deferred(&sshdr))
  436. return NEEDS_RETRY;
  437. if (sdev->handler && sdev->handler->check_sense) {
  438. int rc;
  439. rc = sdev->handler->check_sense(sdev, &sshdr);
  440. if (rc != SCSI_RETURN_NOT_HANDLED)
  441. return rc;
  442. /* handler does not care. Drop down to default handling */
  443. }
  444. if (scmd->cmnd[0] == TEST_UNIT_READY && scmd->scsi_done != scsi_eh_done)
  445. /*
  446. * nasty: for mid-layer issued TURs, we need to return the
  447. * actual sense data without any recovery attempt. For eh
  448. * issued ones, we need to try to recover and interpret
  449. */
  450. return SUCCESS;
  451. /*
  452. * Previous logic looked for FILEMARK, EOM or ILI which are
  453. * mainly associated with tapes and returned SUCCESS.
  454. */
  455. if (sshdr.response_code == 0x70) {
  456. /* fixed format */
  457. if (scmd->sense_buffer[2] & 0xe0)
  458. return SUCCESS;
  459. } else {
  460. /*
  461. * descriptor format: look for "stream commands sense data
  462. * descriptor" (see SSC-3). Assume single sense data
  463. * descriptor. Ignore ILI from SBC-2 READ LONG and WRITE LONG.
  464. */
  465. if ((sshdr.additional_length > 3) &&
  466. (scmd->sense_buffer[8] == 0x4) &&
  467. (scmd->sense_buffer[11] & 0xe0))
  468. return SUCCESS;
  469. }
  470. switch (sshdr.sense_key) {
  471. case NO_SENSE:
  472. return SUCCESS;
  473. case RECOVERED_ERROR:
  474. return /* soft_error */ SUCCESS;
  475. case ABORTED_COMMAND:
  476. if (sshdr.asc == 0x10) /* DIF */
  477. return SUCCESS;
  478. if (sshdr.asc == 0x44 && sdev->sdev_bflags & BLIST_RETRY_ITF)
  479. return ADD_TO_MLQUEUE;
  480. if (sshdr.asc == 0xc1 && sshdr.ascq == 0x01 &&
  481. sdev->sdev_bflags & BLIST_RETRY_ASC_C1)
  482. return ADD_TO_MLQUEUE;
  483. return NEEDS_RETRY;
  484. case NOT_READY:
  485. case UNIT_ATTENTION:
  486. /*
  487. * if we are expecting a cc/ua because of a bus reset that we
  488. * performed, treat this just as a retry. otherwise this is
  489. * information that we should pass up to the upper-level driver
  490. * so that we can deal with it there.
  491. */
  492. if (scmd->device->expecting_cc_ua) {
  493. /*
  494. * Because some device does not queue unit
  495. * attentions correctly, we carefully check
  496. * additional sense code and qualifier so as
  497. * not to squash media change unit attention.
  498. */
  499. if (sshdr.asc != 0x28 || sshdr.ascq != 0x00) {
  500. scmd->device->expecting_cc_ua = 0;
  501. return NEEDS_RETRY;
  502. }
  503. }
  504. /*
  505. * we might also expect a cc/ua if another LUN on the target
  506. * reported a UA with an ASC/ASCQ of 3F 0E -
  507. * REPORTED LUNS DATA HAS CHANGED.
  508. */
  509. if (scmd->device->sdev_target->expecting_lun_change &&
  510. sshdr.asc == 0x3f && sshdr.ascq == 0x0e)
  511. return NEEDS_RETRY;
  512. /*
  513. * if the device is in the process of becoming ready, we
  514. * should retry.
  515. */
  516. if ((sshdr.asc == 0x04) && (sshdr.ascq == 0x01))
  517. return NEEDS_RETRY;
  518. /*
  519. * if the device is not started, we need to wake
  520. * the error handler to start the motor
  521. */
  522. if (scmd->device->allow_restart &&
  523. (sshdr.asc == 0x04) && (sshdr.ascq == 0x02))
  524. return FAILED;
  525. /*
  526. * Pass the UA upwards for a determination in the completion
  527. * functions.
  528. */
  529. return SUCCESS;
  530. /* these are not supported */
  531. case DATA_PROTECT:
  532. if (sshdr.asc == 0x27 && sshdr.ascq == 0x07) {
  533. /* Thin provisioning hard threshold reached */
  534. set_host_byte(scmd, DID_ALLOC_FAILURE);
  535. return SUCCESS;
  536. }
  537. /* FALLTHROUGH */
  538. case COPY_ABORTED:
  539. case VOLUME_OVERFLOW:
  540. case MISCOMPARE:
  541. case BLANK_CHECK:
  542. set_host_byte(scmd, DID_TARGET_FAILURE);
  543. return SUCCESS;
  544. case MEDIUM_ERROR:
  545. if (sshdr.asc == 0x11 || /* UNRECOVERED READ ERR */
  546. sshdr.asc == 0x13 || /* AMNF DATA FIELD */
  547. sshdr.asc == 0x14) { /* RECORD NOT FOUND */
  548. set_host_byte(scmd, DID_MEDIUM_ERROR);
  549. return SUCCESS;
  550. }
  551. return NEEDS_RETRY;
  552. case HARDWARE_ERROR:
  553. if (scmd->device->retry_hwerror)
  554. return ADD_TO_MLQUEUE;
  555. else
  556. set_host_byte(scmd, DID_TARGET_FAILURE);
  557. /* FALLTHROUGH */
  558. case ILLEGAL_REQUEST:
  559. if (sshdr.asc == 0x20 || /* Invalid command operation code */
  560. sshdr.asc == 0x21 || /* Logical block address out of range */
  561. sshdr.asc == 0x22 || /* Invalid function */
  562. sshdr.asc == 0x24 || /* Invalid field in cdb */
  563. sshdr.asc == 0x26 || /* Parameter value invalid */
  564. sshdr.asc == 0x27) { /* Write protected */
  565. set_host_byte(scmd, DID_TARGET_FAILURE);
  566. }
  567. return SUCCESS;
  568. default:
  569. return SUCCESS;
  570. }
  571. }
  572. EXPORT_SYMBOL_GPL(scsi_check_sense);
  573. static void scsi_handle_queue_ramp_up(struct scsi_device *sdev)
  574. {
  575. struct scsi_host_template *sht = sdev->host->hostt;
  576. struct scsi_device *tmp_sdev;
  577. if (!sht->track_queue_depth ||
  578. sdev->queue_depth >= sdev->max_queue_depth)
  579. return;
  580. if (time_before(jiffies,
  581. sdev->last_queue_ramp_up + sdev->queue_ramp_up_period))
  582. return;
  583. if (time_before(jiffies,
  584. sdev->last_queue_full_time + sdev->queue_ramp_up_period))
  585. return;
  586. /*
  587. * Walk all devices of a target and do
  588. * ramp up on them.
  589. */
  590. shost_for_each_device(tmp_sdev, sdev->host) {
  591. if (tmp_sdev->channel != sdev->channel ||
  592. tmp_sdev->id != sdev->id ||
  593. tmp_sdev->queue_depth == sdev->max_queue_depth)
  594. continue;
  595. scsi_change_queue_depth(tmp_sdev, tmp_sdev->queue_depth + 1);
  596. sdev->last_queue_ramp_up = jiffies;
  597. }
  598. }
  599. static void scsi_handle_queue_full(struct scsi_device *sdev)
  600. {
  601. struct scsi_host_template *sht = sdev->host->hostt;
  602. struct scsi_device *tmp_sdev;
  603. if (!sht->track_queue_depth)
  604. return;
  605. shost_for_each_device(tmp_sdev, sdev->host) {
  606. if (tmp_sdev->channel != sdev->channel ||
  607. tmp_sdev->id != sdev->id)
  608. continue;
  609. /*
  610. * We do not know the number of commands that were at
  611. * the device when we got the queue full so we start
  612. * from the highest possible value and work our way down.
  613. */
  614. scsi_track_queue_full(tmp_sdev, tmp_sdev->queue_depth - 1);
  615. }
  616. }
  617. /**
  618. * scsi_eh_completed_normally - Disposition a eh cmd on return from LLD.
  619. * @scmd: SCSI cmd to examine.
  620. *
  621. * Notes:
  622. * This is *only* called when we are examining the status of commands
  623. * queued during error recovery. the main difference here is that we
  624. * don't allow for the possibility of retries here, and we are a lot
  625. * more restrictive about what we consider acceptable.
  626. */
  627. static int scsi_eh_completed_normally(struct scsi_cmnd *scmd)
  628. {
  629. /*
  630. * first check the host byte, to see if there is anything in there
  631. * that would indicate what we need to do.
  632. */
  633. if (host_byte(scmd->result) == DID_RESET) {
  634. /*
  635. * rats. we are already in the error handler, so we now
  636. * get to try and figure out what to do next. if the sense
  637. * is valid, we have a pretty good idea of what to do.
  638. * if not, we mark it as FAILED.
  639. */
  640. return scsi_check_sense(scmd);
  641. }
  642. if (host_byte(scmd->result) != DID_OK)
  643. return FAILED;
  644. /*
  645. * next, check the message byte.
  646. */
  647. if (msg_byte(scmd->result) != COMMAND_COMPLETE)
  648. return FAILED;
  649. /*
  650. * now, check the status byte to see if this indicates
  651. * anything special.
  652. */
  653. switch (status_byte(scmd->result)) {
  654. case GOOD:
  655. scsi_handle_queue_ramp_up(scmd->device);
  656. /* FALLTHROUGH */
  657. case COMMAND_TERMINATED:
  658. return SUCCESS;
  659. case CHECK_CONDITION:
  660. return scsi_check_sense(scmd);
  661. case CONDITION_GOOD:
  662. case INTERMEDIATE_GOOD:
  663. case INTERMEDIATE_C_GOOD:
  664. /*
  665. * who knows? FIXME(eric)
  666. */
  667. return SUCCESS;
  668. case RESERVATION_CONFLICT:
  669. if (scmd->cmnd[0] == TEST_UNIT_READY)
  670. /* it is a success, we probed the device and
  671. * found it */
  672. return SUCCESS;
  673. /* otherwise, we failed to send the command */
  674. return FAILED;
  675. case QUEUE_FULL:
  676. scsi_handle_queue_full(scmd->device);
  677. /* fall through */
  678. case BUSY:
  679. return NEEDS_RETRY;
  680. default:
  681. return FAILED;
  682. }
  683. return FAILED;
  684. }
  685. /**
  686. * scsi_eh_done - Completion function for error handling.
  687. * @scmd: Cmd that is done.
  688. */
  689. static void scsi_eh_done(struct scsi_cmnd *scmd)
  690. {
  691. struct completion *eh_action;
  692. SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
  693. "%s result: %x\n", __func__, scmd->result));
  694. eh_action = scmd->device->host->eh_action;
  695. if (eh_action)
  696. complete(eh_action);
  697. }
  698. /**
  699. * scsi_try_host_reset - ask host adapter to reset itself
  700. * @scmd: SCSI cmd to send host reset.
  701. */
  702. static int scsi_try_host_reset(struct scsi_cmnd *scmd)
  703. {
  704. unsigned long flags;
  705. int rtn;
  706. struct Scsi_Host *host = scmd->device->host;
  707. struct scsi_host_template *hostt = host->hostt;
  708. SCSI_LOG_ERROR_RECOVERY(3,
  709. shost_printk(KERN_INFO, host, "Snd Host RST\n"));
  710. if (!hostt->eh_host_reset_handler)
  711. return FAILED;
  712. rtn = hostt->eh_host_reset_handler(scmd);
  713. if (rtn == SUCCESS) {
  714. if (!hostt->skip_settle_delay)
  715. ssleep(HOST_RESET_SETTLE_TIME);
  716. spin_lock_irqsave(host->host_lock, flags);
  717. scsi_report_bus_reset(host, scmd_channel(scmd));
  718. spin_unlock_irqrestore(host->host_lock, flags);
  719. }
  720. return rtn;
  721. }
  722. /**
  723. * scsi_try_bus_reset - ask host to perform a bus reset
  724. * @scmd: SCSI cmd to send bus reset.
  725. */
  726. static int scsi_try_bus_reset(struct scsi_cmnd *scmd)
  727. {
  728. unsigned long flags;
  729. int rtn;
  730. struct Scsi_Host *host = scmd->device->host;
  731. struct scsi_host_template *hostt = host->hostt;
  732. SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
  733. "%s: Snd Bus RST\n", __func__));
  734. if (!hostt->eh_bus_reset_handler)
  735. return FAILED;
  736. rtn = hostt->eh_bus_reset_handler(scmd);
  737. if (rtn == SUCCESS) {
  738. if (!hostt->skip_settle_delay)
  739. ssleep(BUS_RESET_SETTLE_TIME);
  740. spin_lock_irqsave(host->host_lock, flags);
  741. scsi_report_bus_reset(host, scmd_channel(scmd));
  742. spin_unlock_irqrestore(host->host_lock, flags);
  743. }
  744. return rtn;
  745. }
  746. static void __scsi_report_device_reset(struct scsi_device *sdev, void *data)
  747. {
  748. sdev->was_reset = 1;
  749. sdev->expecting_cc_ua = 1;
  750. }
  751. /**
  752. * scsi_try_target_reset - Ask host to perform a target reset
  753. * @scmd: SCSI cmd used to send a target reset
  754. *
  755. * Notes:
  756. * There is no timeout for this operation. if this operation is
  757. * unreliable for a given host, then the host itself needs to put a
  758. * timer on it, and set the host back to a consistent state prior to
  759. * returning.
  760. */
  761. static int scsi_try_target_reset(struct scsi_cmnd *scmd)
  762. {
  763. unsigned long flags;
  764. int rtn;
  765. struct Scsi_Host *host = scmd->device->host;
  766. struct scsi_host_template *hostt = host->hostt;
  767. if (!hostt->eh_target_reset_handler)
  768. return FAILED;
  769. rtn = hostt->eh_target_reset_handler(scmd);
  770. if (rtn == SUCCESS) {
  771. spin_lock_irqsave(host->host_lock, flags);
  772. __starget_for_each_device(scsi_target(scmd->device), NULL,
  773. __scsi_report_device_reset);
  774. spin_unlock_irqrestore(host->host_lock, flags);
  775. }
  776. return rtn;
  777. }
  778. /**
  779. * scsi_try_bus_device_reset - Ask host to perform a BDR on a dev
  780. * @scmd: SCSI cmd used to send BDR
  781. *
  782. * Notes:
  783. * There is no timeout for this operation. if this operation is
  784. * unreliable for a given host, then the host itself needs to put a
  785. * timer on it, and set the host back to a consistent state prior to
  786. * returning.
  787. */
  788. static int scsi_try_bus_device_reset(struct scsi_cmnd *scmd)
  789. {
  790. int rtn;
  791. struct scsi_host_template *hostt = scmd->device->host->hostt;
  792. if (!hostt->eh_device_reset_handler)
  793. return FAILED;
  794. rtn = hostt->eh_device_reset_handler(scmd);
  795. if (rtn == SUCCESS)
  796. __scsi_report_device_reset(scmd->device, NULL);
  797. return rtn;
  798. }
  799. /**
  800. * scsi_try_to_abort_cmd - Ask host to abort a SCSI command
  801. * @hostt: SCSI driver host template
  802. * @scmd: SCSI cmd used to send a target reset
  803. *
  804. * Return value:
  805. * SUCCESS, FAILED, or FAST_IO_FAIL
  806. *
  807. * Notes:
  808. * SUCCESS does not necessarily indicate that the command
  809. * has been aborted; it only indicates that the LLDDs
  810. * has cleared all references to that command.
  811. * LLDDs should return FAILED only if an abort was required
  812. * but could not be executed. LLDDs should return FAST_IO_FAIL
  813. * if the device is temporarily unavailable (eg due to a
  814. * link down on FibreChannel)
  815. */
  816. static int scsi_try_to_abort_cmd(struct scsi_host_template *hostt,
  817. struct scsi_cmnd *scmd)
  818. {
  819. if (!hostt->eh_abort_handler)
  820. return FAILED;
  821. return hostt->eh_abort_handler(scmd);
  822. }
  823. static void scsi_abort_eh_cmnd(struct scsi_cmnd *scmd)
  824. {
  825. if (scsi_try_to_abort_cmd(scmd->device->host->hostt, scmd) != SUCCESS)
  826. if (scsi_try_bus_device_reset(scmd) != SUCCESS)
  827. if (scsi_try_target_reset(scmd) != SUCCESS)
  828. if (scsi_try_bus_reset(scmd) != SUCCESS)
  829. scsi_try_host_reset(scmd);
  830. }
  831. /**
  832. * scsi_eh_prep_cmnd - Save a scsi command info as part of error recovery
  833. * @scmd: SCSI command structure to hijack
  834. * @ses: structure to save restore information
  835. * @cmnd: CDB to send. Can be NULL if no new cmnd is needed
  836. * @cmnd_size: size in bytes of @cmnd (must be <= BLK_MAX_CDB)
  837. * @sense_bytes: size of sense data to copy. or 0 (if != 0 @cmnd is ignored)
  838. *
  839. * This function is used to save a scsi command information before re-execution
  840. * as part of the error recovery process. If @sense_bytes is 0 the command
  841. * sent must be one that does not transfer any data. If @sense_bytes != 0
  842. * @cmnd is ignored and this functions sets up a REQUEST_SENSE command
  843. * and cmnd buffers to read @sense_bytes into @scmd->sense_buffer.
  844. */
  845. void scsi_eh_prep_cmnd(struct scsi_cmnd *scmd, struct scsi_eh_save *ses,
  846. unsigned char *cmnd, int cmnd_size, unsigned sense_bytes)
  847. {
  848. struct scsi_device *sdev = scmd->device;
  849. /*
  850. * We need saved copies of a number of fields - this is because
  851. * error handling may need to overwrite these with different values
  852. * to run different commands, and once error handling is complete,
  853. * we will need to restore these values prior to running the actual
  854. * command.
  855. */
  856. ses->cmd_len = scmd->cmd_len;
  857. ses->cmnd = scmd->cmnd;
  858. ses->data_direction = scmd->sc_data_direction;
  859. ses->sdb = scmd->sdb;
  860. ses->result = scmd->result;
  861. ses->resid_len = scmd->req.resid_len;
  862. ses->underflow = scmd->underflow;
  863. ses->prot_op = scmd->prot_op;
  864. ses->eh_eflags = scmd->eh_eflags;
  865. scmd->prot_op = SCSI_PROT_NORMAL;
  866. scmd->eh_eflags = 0;
  867. scmd->cmnd = ses->eh_cmnd;
  868. memset(scmd->cmnd, 0, BLK_MAX_CDB);
  869. memset(&scmd->sdb, 0, sizeof(scmd->sdb));
  870. scmd->result = 0;
  871. scmd->req.resid_len = 0;
  872. if (sense_bytes) {
  873. scmd->sdb.length = min_t(unsigned, SCSI_SENSE_BUFFERSIZE,
  874. sense_bytes);
  875. sg_init_one(&ses->sense_sgl, scmd->sense_buffer,
  876. scmd->sdb.length);
  877. scmd->sdb.table.sgl = &ses->sense_sgl;
  878. scmd->sc_data_direction = DMA_FROM_DEVICE;
  879. scmd->sdb.table.nents = scmd->sdb.table.orig_nents = 1;
  880. scmd->cmnd[0] = REQUEST_SENSE;
  881. scmd->cmnd[4] = scmd->sdb.length;
  882. scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
  883. } else {
  884. scmd->sc_data_direction = DMA_NONE;
  885. if (cmnd) {
  886. BUG_ON(cmnd_size > BLK_MAX_CDB);
  887. memcpy(scmd->cmnd, cmnd, cmnd_size);
  888. scmd->cmd_len = COMMAND_SIZE(scmd->cmnd[0]);
  889. }
  890. }
  891. scmd->underflow = 0;
  892. if (sdev->scsi_level <= SCSI_2 && sdev->scsi_level != SCSI_UNKNOWN)
  893. scmd->cmnd[1] = (scmd->cmnd[1] & 0x1f) |
  894. (sdev->lun << 5 & 0xe0);
  895. /*
  896. * Zero the sense buffer. The scsi spec mandates that any
  897. * untransferred sense data should be interpreted as being zero.
  898. */
  899. memset(scmd->sense_buffer, 0, SCSI_SENSE_BUFFERSIZE);
  900. }
  901. EXPORT_SYMBOL(scsi_eh_prep_cmnd);
  902. /**
  903. * scsi_eh_restore_cmnd - Restore a scsi command info as part of error recovery
  904. * @scmd: SCSI command structure to restore
  905. * @ses: saved information from a coresponding call to scsi_eh_prep_cmnd
  906. *
  907. * Undo any damage done by above scsi_eh_prep_cmnd().
  908. */
  909. void scsi_eh_restore_cmnd(struct scsi_cmnd* scmd, struct scsi_eh_save *ses)
  910. {
  911. /*
  912. * Restore original data
  913. */
  914. scmd->cmd_len = ses->cmd_len;
  915. scmd->cmnd = ses->cmnd;
  916. scmd->sc_data_direction = ses->data_direction;
  917. scmd->sdb = ses->sdb;
  918. scmd->result = ses->result;
  919. scmd->req.resid_len = ses->resid_len;
  920. scmd->underflow = ses->underflow;
  921. scmd->prot_op = ses->prot_op;
  922. scmd->eh_eflags = ses->eh_eflags;
  923. }
  924. EXPORT_SYMBOL(scsi_eh_restore_cmnd);
  925. /**
  926. * scsi_send_eh_cmnd - submit a scsi command as part of error recovery
  927. * @scmd: SCSI command structure to hijack
  928. * @cmnd: CDB to send
  929. * @cmnd_size: size in bytes of @cmnd
  930. * @timeout: timeout for this request
  931. * @sense_bytes: size of sense data to copy or 0
  932. *
  933. * This function is used to send a scsi command down to a target device
  934. * as part of the error recovery process. See also scsi_eh_prep_cmnd() above.
  935. *
  936. * Return value:
  937. * SUCCESS or FAILED or NEEDS_RETRY
  938. */
  939. static int scsi_send_eh_cmnd(struct scsi_cmnd *scmd, unsigned char *cmnd,
  940. int cmnd_size, int timeout, unsigned sense_bytes)
  941. {
  942. struct scsi_device *sdev = scmd->device;
  943. struct Scsi_Host *shost = sdev->host;
  944. DECLARE_COMPLETION_ONSTACK(done);
  945. unsigned long timeleft = timeout, delay;
  946. struct scsi_eh_save ses;
  947. const unsigned long stall_for = msecs_to_jiffies(100);
  948. int rtn;
  949. retry:
  950. scsi_eh_prep_cmnd(scmd, &ses, cmnd, cmnd_size, sense_bytes);
  951. shost->eh_action = &done;
  952. scsi_log_send(scmd);
  953. scmd->scsi_done = scsi_eh_done;
  954. /*
  955. * Lock sdev->state_mutex to avoid that scsi_device_quiesce() can
  956. * change the SCSI device state after we have examined it and before
  957. * .queuecommand() is called.
  958. */
  959. mutex_lock(&sdev->state_mutex);
  960. while (sdev->sdev_state == SDEV_BLOCK && timeleft > 0) {
  961. mutex_unlock(&sdev->state_mutex);
  962. SCSI_LOG_ERROR_RECOVERY(5, sdev_printk(KERN_DEBUG, sdev,
  963. "%s: state %d <> %d\n", __func__, sdev->sdev_state,
  964. SDEV_BLOCK));
  965. delay = min(timeleft, stall_for);
  966. timeleft -= delay;
  967. msleep(jiffies_to_msecs(delay));
  968. mutex_lock(&sdev->state_mutex);
  969. }
  970. if (sdev->sdev_state != SDEV_BLOCK)
  971. rtn = shost->hostt->queuecommand(shost, scmd);
  972. else
  973. rtn = SCSI_MLQUEUE_DEVICE_BUSY;
  974. mutex_unlock(&sdev->state_mutex);
  975. if (rtn) {
  976. if (timeleft > stall_for) {
  977. scsi_eh_restore_cmnd(scmd, &ses);
  978. timeleft -= stall_for;
  979. msleep(jiffies_to_msecs(stall_for));
  980. goto retry;
  981. }
  982. /* signal not to enter either branch of the if () below */
  983. timeleft = 0;
  984. rtn = FAILED;
  985. } else {
  986. timeleft = wait_for_completion_timeout(&done, timeout);
  987. rtn = SUCCESS;
  988. }
  989. shost->eh_action = NULL;
  990. scsi_log_completion(scmd, rtn);
  991. SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
  992. "%s timeleft: %ld\n",
  993. __func__, timeleft));
  994. /*
  995. * If there is time left scsi_eh_done got called, and we will examine
  996. * the actual status codes to see whether the command actually did
  997. * complete normally, else if we have a zero return and no time left,
  998. * the command must still be pending, so abort it and return FAILED.
  999. * If we never actually managed to issue the command, because
  1000. * ->queuecommand() kept returning non zero, use the rtn = FAILED
  1001. * value above (so don't execute either branch of the if)
  1002. */
  1003. if (timeleft) {
  1004. rtn = scsi_eh_completed_normally(scmd);
  1005. SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
  1006. "%s: scsi_eh_completed_normally %x\n", __func__, rtn));
  1007. switch (rtn) {
  1008. case SUCCESS:
  1009. case NEEDS_RETRY:
  1010. case FAILED:
  1011. break;
  1012. case ADD_TO_MLQUEUE:
  1013. rtn = NEEDS_RETRY;
  1014. break;
  1015. default:
  1016. rtn = FAILED;
  1017. break;
  1018. }
  1019. } else if (rtn != FAILED) {
  1020. scsi_abort_eh_cmnd(scmd);
  1021. rtn = FAILED;
  1022. }
  1023. scsi_eh_restore_cmnd(scmd, &ses);
  1024. return rtn;
  1025. }
  1026. /**
  1027. * scsi_request_sense - Request sense data from a particular target.
  1028. * @scmd: SCSI cmd for request sense.
  1029. *
  1030. * Notes:
  1031. * Some hosts automatically obtain this information, others require
  1032. * that we obtain it on our own. This function will *not* return until
  1033. * the command either times out, or it completes.
  1034. */
  1035. static int scsi_request_sense(struct scsi_cmnd *scmd)
  1036. {
  1037. return scsi_send_eh_cmnd(scmd, NULL, 0, scmd->device->eh_timeout, ~0);
  1038. }
  1039. static int scsi_eh_action(struct scsi_cmnd *scmd, int rtn)
  1040. {
  1041. if (!blk_rq_is_passthrough(scmd->request)) {
  1042. struct scsi_driver *sdrv = scsi_cmd_to_driver(scmd);
  1043. if (sdrv->eh_action)
  1044. rtn = sdrv->eh_action(scmd, rtn);
  1045. }
  1046. return rtn;
  1047. }
  1048. /**
  1049. * scsi_eh_finish_cmd - Handle a cmd that eh is finished with.
  1050. * @scmd: Original SCSI cmd that eh has finished.
  1051. * @done_q: Queue for processed commands.
  1052. *
  1053. * Notes:
  1054. * We don't want to use the normal command completion while we are are
  1055. * still handling errors - it may cause other commands to be queued,
  1056. * and that would disturb what we are doing. Thus we really want to
  1057. * keep a list of pending commands for final completion, and once we
  1058. * are ready to leave error handling we handle completion for real.
  1059. */
  1060. void scsi_eh_finish_cmd(struct scsi_cmnd *scmd, struct list_head *done_q)
  1061. {
  1062. list_move_tail(&scmd->eh_entry, done_q);
  1063. }
  1064. EXPORT_SYMBOL(scsi_eh_finish_cmd);
  1065. /**
  1066. * scsi_eh_get_sense - Get device sense data.
  1067. * @work_q: Queue of commands to process.
  1068. * @done_q: Queue of processed commands.
  1069. *
  1070. * Description:
  1071. * See if we need to request sense information. if so, then get it
  1072. * now, so we have a better idea of what to do.
  1073. *
  1074. * Notes:
  1075. * This has the unfortunate side effect that if a shost adapter does
  1076. * not automatically request sense information, we end up shutting
  1077. * it down before we request it.
  1078. *
  1079. * All drivers should request sense information internally these days,
  1080. * so for now all I have to say is tough noogies if you end up in here.
  1081. *
  1082. * XXX: Long term this code should go away, but that needs an audit of
  1083. * all LLDDs first.
  1084. */
  1085. int scsi_eh_get_sense(struct list_head *work_q,
  1086. struct list_head *done_q)
  1087. {
  1088. struct scsi_cmnd *scmd, *next;
  1089. struct Scsi_Host *shost;
  1090. int rtn;
  1091. /*
  1092. * If SCSI_EH_ABORT_SCHEDULED has been set, it is timeout IO,
  1093. * should not get sense.
  1094. */
  1095. list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
  1096. if ((scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) ||
  1097. SCSI_SENSE_VALID(scmd))
  1098. continue;
  1099. shost = scmd->device->host;
  1100. if (scsi_host_eh_past_deadline(shost)) {
  1101. SCSI_LOG_ERROR_RECOVERY(3,
  1102. scmd_printk(KERN_INFO, scmd,
  1103. "%s: skip request sense, past eh deadline\n",
  1104. current->comm));
  1105. break;
  1106. }
  1107. if (status_byte(scmd->result) != CHECK_CONDITION)
  1108. /*
  1109. * don't request sense if there's no check condition
  1110. * status because the error we're processing isn't one
  1111. * that has a sense code (and some devices get
  1112. * confused by sense requests out of the blue)
  1113. */
  1114. continue;
  1115. SCSI_LOG_ERROR_RECOVERY(2, scmd_printk(KERN_INFO, scmd,
  1116. "%s: requesting sense\n",
  1117. current->comm));
  1118. rtn = scsi_request_sense(scmd);
  1119. if (rtn != SUCCESS)
  1120. continue;
  1121. SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
  1122. "sense requested, result %x\n", scmd->result));
  1123. SCSI_LOG_ERROR_RECOVERY(3, scsi_print_sense(scmd));
  1124. rtn = scsi_decide_disposition(scmd);
  1125. /*
  1126. * if the result was normal, then just pass it along to the
  1127. * upper level.
  1128. */
  1129. if (rtn == SUCCESS)
  1130. /* we don't want this command reissued, just
  1131. * finished with the sense data, so set
  1132. * retries to the max allowed to ensure it
  1133. * won't get reissued */
  1134. scmd->retries = scmd->allowed;
  1135. else if (rtn != NEEDS_RETRY)
  1136. continue;
  1137. scsi_eh_finish_cmd(scmd, done_q);
  1138. }
  1139. return list_empty(work_q);
  1140. }
  1141. EXPORT_SYMBOL_GPL(scsi_eh_get_sense);
  1142. /**
  1143. * scsi_eh_tur - Send TUR to device.
  1144. * @scmd: &scsi_cmnd to send TUR
  1145. *
  1146. * Return value:
  1147. * 0 - Device is ready. 1 - Device NOT ready.
  1148. */
  1149. static int scsi_eh_tur(struct scsi_cmnd *scmd)
  1150. {
  1151. static unsigned char tur_command[6] = {TEST_UNIT_READY, 0, 0, 0, 0, 0};
  1152. int retry_cnt = 1, rtn;
  1153. retry_tur:
  1154. rtn = scsi_send_eh_cmnd(scmd, tur_command, 6,
  1155. scmd->device->eh_timeout, 0);
  1156. SCSI_LOG_ERROR_RECOVERY(3, scmd_printk(KERN_INFO, scmd,
  1157. "%s return: %x\n", __func__, rtn));
  1158. switch (rtn) {
  1159. case NEEDS_RETRY:
  1160. if (retry_cnt--)
  1161. goto retry_tur;
  1162. /*FALLTHRU*/
  1163. case SUCCESS:
  1164. return 0;
  1165. default:
  1166. return 1;
  1167. }
  1168. }
  1169. /**
  1170. * scsi_eh_test_devices - check if devices are responding from error recovery.
  1171. * @cmd_list: scsi commands in error recovery.
  1172. * @work_q: queue for commands which still need more error recovery
  1173. * @done_q: queue for commands which are finished
  1174. * @try_stu: boolean on if a STU command should be tried in addition to TUR.
  1175. *
  1176. * Decription:
  1177. * Tests if devices are in a working state. Commands to devices now in
  1178. * a working state are sent to the done_q while commands to devices which
  1179. * are still failing to respond are returned to the work_q for more
  1180. * processing.
  1181. **/
  1182. static int scsi_eh_test_devices(struct list_head *cmd_list,
  1183. struct list_head *work_q,
  1184. struct list_head *done_q, int try_stu)
  1185. {
  1186. struct scsi_cmnd *scmd, *next;
  1187. struct scsi_device *sdev;
  1188. int finish_cmds;
  1189. while (!list_empty(cmd_list)) {
  1190. scmd = list_entry(cmd_list->next, struct scsi_cmnd, eh_entry);
  1191. sdev = scmd->device;
  1192. if (!try_stu) {
  1193. if (scsi_host_eh_past_deadline(sdev->host)) {
  1194. /* Push items back onto work_q */
  1195. list_splice_init(cmd_list, work_q);
  1196. SCSI_LOG_ERROR_RECOVERY(3,
  1197. sdev_printk(KERN_INFO, sdev,
  1198. "%s: skip test device, past eh deadline",
  1199. current->comm));
  1200. break;
  1201. }
  1202. }
  1203. finish_cmds = !scsi_device_online(scmd->device) ||
  1204. (try_stu && !scsi_eh_try_stu(scmd) &&
  1205. !scsi_eh_tur(scmd)) ||
  1206. !scsi_eh_tur(scmd);
  1207. list_for_each_entry_safe(scmd, next, cmd_list, eh_entry)
  1208. if (scmd->device == sdev) {
  1209. if (finish_cmds &&
  1210. (try_stu ||
  1211. scsi_eh_action(scmd, SUCCESS) == SUCCESS))
  1212. scsi_eh_finish_cmd(scmd, done_q);
  1213. else
  1214. list_move_tail(&scmd->eh_entry, work_q);
  1215. }
  1216. }
  1217. return list_empty(work_q);
  1218. }
  1219. /**
  1220. * scsi_eh_try_stu - Send START_UNIT to device.
  1221. * @scmd: &scsi_cmnd to send START_UNIT
  1222. *
  1223. * Return value:
  1224. * 0 - Device is ready. 1 - Device NOT ready.
  1225. */
  1226. static int scsi_eh_try_stu(struct scsi_cmnd *scmd)
  1227. {
  1228. static unsigned char stu_command[6] = {START_STOP, 0, 0, 0, 1, 0};
  1229. if (scmd->device->allow_restart) {
  1230. int i, rtn = NEEDS_RETRY;
  1231. for (i = 0; rtn == NEEDS_RETRY && i < 2; i++)
  1232. rtn = scsi_send_eh_cmnd(scmd, stu_command, 6, scmd->device->request_queue->rq_timeout, 0);
  1233. if (rtn == SUCCESS)
  1234. return 0;
  1235. }
  1236. return 1;
  1237. }
  1238. /**
  1239. * scsi_eh_stu - send START_UNIT if needed
  1240. * @shost: &scsi host being recovered.
  1241. * @work_q: &list_head for pending commands.
  1242. * @done_q: &list_head for processed commands.
  1243. *
  1244. * Notes:
  1245. * If commands are failing due to not ready, initializing command required,
  1246. * try revalidating the device, which will end up sending a start unit.
  1247. */
  1248. static int scsi_eh_stu(struct Scsi_Host *shost,
  1249. struct list_head *work_q,
  1250. struct list_head *done_q)
  1251. {
  1252. struct scsi_cmnd *scmd, *stu_scmd, *next;
  1253. struct scsi_device *sdev;
  1254. shost_for_each_device(sdev, shost) {
  1255. if (scsi_host_eh_past_deadline(shost)) {
  1256. SCSI_LOG_ERROR_RECOVERY(3,
  1257. sdev_printk(KERN_INFO, sdev,
  1258. "%s: skip START_UNIT, past eh deadline\n",
  1259. current->comm));
  1260. break;
  1261. }
  1262. stu_scmd = NULL;
  1263. list_for_each_entry(scmd, work_q, eh_entry)
  1264. if (scmd->device == sdev && SCSI_SENSE_VALID(scmd) &&
  1265. scsi_check_sense(scmd) == FAILED ) {
  1266. stu_scmd = scmd;
  1267. break;
  1268. }
  1269. if (!stu_scmd)
  1270. continue;
  1271. SCSI_LOG_ERROR_RECOVERY(3,
  1272. sdev_printk(KERN_INFO, sdev,
  1273. "%s: Sending START_UNIT\n",
  1274. current->comm));
  1275. if (!scsi_eh_try_stu(stu_scmd)) {
  1276. if (!scsi_device_online(sdev) ||
  1277. !scsi_eh_tur(stu_scmd)) {
  1278. list_for_each_entry_safe(scmd, next,
  1279. work_q, eh_entry) {
  1280. if (scmd->device == sdev &&
  1281. scsi_eh_action(scmd, SUCCESS) == SUCCESS)
  1282. scsi_eh_finish_cmd(scmd, done_q);
  1283. }
  1284. }
  1285. } else {
  1286. SCSI_LOG_ERROR_RECOVERY(3,
  1287. sdev_printk(KERN_INFO, sdev,
  1288. "%s: START_UNIT failed\n",
  1289. current->comm));
  1290. }
  1291. }
  1292. return list_empty(work_q);
  1293. }
  1294. /**
  1295. * scsi_eh_bus_device_reset - send bdr if needed
  1296. * @shost: scsi host being recovered.
  1297. * @work_q: &list_head for pending commands.
  1298. * @done_q: &list_head for processed commands.
  1299. *
  1300. * Notes:
  1301. * Try a bus device reset. Still, look to see whether we have multiple
  1302. * devices that are jammed or not - if we have multiple devices, it
  1303. * makes no sense to try bus_device_reset - we really would need to try
  1304. * a bus_reset instead.
  1305. */
  1306. static int scsi_eh_bus_device_reset(struct Scsi_Host *shost,
  1307. struct list_head *work_q,
  1308. struct list_head *done_q)
  1309. {
  1310. struct scsi_cmnd *scmd, *bdr_scmd, *next;
  1311. struct scsi_device *sdev;
  1312. int rtn;
  1313. shost_for_each_device(sdev, shost) {
  1314. if (scsi_host_eh_past_deadline(shost)) {
  1315. SCSI_LOG_ERROR_RECOVERY(3,
  1316. sdev_printk(KERN_INFO, sdev,
  1317. "%s: skip BDR, past eh deadline\n",
  1318. current->comm));
  1319. break;
  1320. }
  1321. bdr_scmd = NULL;
  1322. list_for_each_entry(scmd, work_q, eh_entry)
  1323. if (scmd->device == sdev) {
  1324. bdr_scmd = scmd;
  1325. break;
  1326. }
  1327. if (!bdr_scmd)
  1328. continue;
  1329. SCSI_LOG_ERROR_RECOVERY(3,
  1330. sdev_printk(KERN_INFO, sdev,
  1331. "%s: Sending BDR\n", current->comm));
  1332. rtn = scsi_try_bus_device_reset(bdr_scmd);
  1333. if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
  1334. if (!scsi_device_online(sdev) ||
  1335. rtn == FAST_IO_FAIL ||
  1336. !scsi_eh_tur(bdr_scmd)) {
  1337. list_for_each_entry_safe(scmd, next,
  1338. work_q, eh_entry) {
  1339. if (scmd->device == sdev &&
  1340. scsi_eh_action(scmd, rtn) != FAILED)
  1341. scsi_eh_finish_cmd(scmd,
  1342. done_q);
  1343. }
  1344. }
  1345. } else {
  1346. SCSI_LOG_ERROR_RECOVERY(3,
  1347. sdev_printk(KERN_INFO, sdev,
  1348. "%s: BDR failed\n", current->comm));
  1349. }
  1350. }
  1351. return list_empty(work_q);
  1352. }
  1353. /**
  1354. * scsi_eh_target_reset - send target reset if needed
  1355. * @shost: scsi host being recovered.
  1356. * @work_q: &list_head for pending commands.
  1357. * @done_q: &list_head for processed commands.
  1358. *
  1359. * Notes:
  1360. * Try a target reset.
  1361. */
  1362. static int scsi_eh_target_reset(struct Scsi_Host *shost,
  1363. struct list_head *work_q,
  1364. struct list_head *done_q)
  1365. {
  1366. LIST_HEAD(tmp_list);
  1367. LIST_HEAD(check_list);
  1368. list_splice_init(work_q, &tmp_list);
  1369. while (!list_empty(&tmp_list)) {
  1370. struct scsi_cmnd *next, *scmd;
  1371. int rtn;
  1372. unsigned int id;
  1373. if (scsi_host_eh_past_deadline(shost)) {
  1374. /* push back on work queue for further processing */
  1375. list_splice_init(&check_list, work_q);
  1376. list_splice_init(&tmp_list, work_q);
  1377. SCSI_LOG_ERROR_RECOVERY(3,
  1378. shost_printk(KERN_INFO, shost,
  1379. "%s: Skip target reset, past eh deadline\n",
  1380. current->comm));
  1381. return list_empty(work_q);
  1382. }
  1383. scmd = list_entry(tmp_list.next, struct scsi_cmnd, eh_entry);
  1384. id = scmd_id(scmd);
  1385. SCSI_LOG_ERROR_RECOVERY(3,
  1386. shost_printk(KERN_INFO, shost,
  1387. "%s: Sending target reset to target %d\n",
  1388. current->comm, id));
  1389. rtn = scsi_try_target_reset(scmd);
  1390. if (rtn != SUCCESS && rtn != FAST_IO_FAIL)
  1391. SCSI_LOG_ERROR_RECOVERY(3,
  1392. shost_printk(KERN_INFO, shost,
  1393. "%s: Target reset failed"
  1394. " target: %d\n",
  1395. current->comm, id));
  1396. list_for_each_entry_safe(scmd, next, &tmp_list, eh_entry) {
  1397. if (scmd_id(scmd) != id)
  1398. continue;
  1399. if (rtn == SUCCESS)
  1400. list_move_tail(&scmd->eh_entry, &check_list);
  1401. else if (rtn == FAST_IO_FAIL)
  1402. scsi_eh_finish_cmd(scmd, done_q);
  1403. else
  1404. /* push back on work queue for further processing */
  1405. list_move(&scmd->eh_entry, work_q);
  1406. }
  1407. }
  1408. return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
  1409. }
  1410. /**
  1411. * scsi_eh_bus_reset - send a bus reset
  1412. * @shost: &scsi host being recovered.
  1413. * @work_q: &list_head for pending commands.
  1414. * @done_q: &list_head for processed commands.
  1415. */
  1416. static int scsi_eh_bus_reset(struct Scsi_Host *shost,
  1417. struct list_head *work_q,
  1418. struct list_head *done_q)
  1419. {
  1420. struct scsi_cmnd *scmd, *chan_scmd, *next;
  1421. LIST_HEAD(check_list);
  1422. unsigned int channel;
  1423. int rtn;
  1424. /*
  1425. * we really want to loop over the various channels, and do this on
  1426. * a channel by channel basis. we should also check to see if any
  1427. * of the failed commands are on soft_reset devices, and if so, skip
  1428. * the reset.
  1429. */
  1430. for (channel = 0; channel <= shost->max_channel; channel++) {
  1431. if (scsi_host_eh_past_deadline(shost)) {
  1432. list_splice_init(&check_list, work_q);
  1433. SCSI_LOG_ERROR_RECOVERY(3,
  1434. shost_printk(KERN_INFO, shost,
  1435. "%s: skip BRST, past eh deadline\n",
  1436. current->comm));
  1437. return list_empty(work_q);
  1438. }
  1439. chan_scmd = NULL;
  1440. list_for_each_entry(scmd, work_q, eh_entry) {
  1441. if (channel == scmd_channel(scmd)) {
  1442. chan_scmd = scmd;
  1443. break;
  1444. /*
  1445. * FIXME add back in some support for
  1446. * soft_reset devices.
  1447. */
  1448. }
  1449. }
  1450. if (!chan_scmd)
  1451. continue;
  1452. SCSI_LOG_ERROR_RECOVERY(3,
  1453. shost_printk(KERN_INFO, shost,
  1454. "%s: Sending BRST chan: %d\n",
  1455. current->comm, channel));
  1456. rtn = scsi_try_bus_reset(chan_scmd);
  1457. if (rtn == SUCCESS || rtn == FAST_IO_FAIL) {
  1458. list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
  1459. if (channel == scmd_channel(scmd)) {
  1460. if (rtn == FAST_IO_FAIL)
  1461. scsi_eh_finish_cmd(scmd,
  1462. done_q);
  1463. else
  1464. list_move_tail(&scmd->eh_entry,
  1465. &check_list);
  1466. }
  1467. }
  1468. } else {
  1469. SCSI_LOG_ERROR_RECOVERY(3,
  1470. shost_printk(KERN_INFO, shost,
  1471. "%s: BRST failed chan: %d\n",
  1472. current->comm, channel));
  1473. }
  1474. }
  1475. return scsi_eh_test_devices(&check_list, work_q, done_q, 0);
  1476. }
  1477. /**
  1478. * scsi_eh_host_reset - send a host reset
  1479. * @shost: host to be reset.
  1480. * @work_q: &list_head for pending commands.
  1481. * @done_q: &list_head for processed commands.
  1482. */
  1483. static int scsi_eh_host_reset(struct Scsi_Host *shost,
  1484. struct list_head *work_q,
  1485. struct list_head *done_q)
  1486. {
  1487. struct scsi_cmnd *scmd, *next;
  1488. LIST_HEAD(check_list);
  1489. int rtn;
  1490. if (!list_empty(work_q)) {
  1491. scmd = list_entry(work_q->next,
  1492. struct scsi_cmnd, eh_entry);
  1493. SCSI_LOG_ERROR_RECOVERY(3,
  1494. shost_printk(KERN_INFO, shost,
  1495. "%s: Sending HRST\n",
  1496. current->comm));
  1497. rtn = scsi_try_host_reset(scmd);
  1498. if (rtn == SUCCESS) {
  1499. list_splice_init(work_q, &check_list);
  1500. } else if (rtn == FAST_IO_FAIL) {
  1501. list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
  1502. scsi_eh_finish_cmd(scmd, done_q);
  1503. }
  1504. } else {
  1505. SCSI_LOG_ERROR_RECOVERY(3,
  1506. shost_printk(KERN_INFO, shost,
  1507. "%s: HRST failed\n",
  1508. current->comm));
  1509. }
  1510. }
  1511. return scsi_eh_test_devices(&check_list, work_q, done_q, 1);
  1512. }
  1513. /**
  1514. * scsi_eh_offline_sdevs - offline scsi devices that fail to recover
  1515. * @work_q: &list_head for pending commands.
  1516. * @done_q: &list_head for processed commands.
  1517. */
  1518. static void scsi_eh_offline_sdevs(struct list_head *work_q,
  1519. struct list_head *done_q)
  1520. {
  1521. struct scsi_cmnd *scmd, *next;
  1522. struct scsi_device *sdev;
  1523. list_for_each_entry_safe(scmd, next, work_q, eh_entry) {
  1524. sdev_printk(KERN_INFO, scmd->device, "Device offlined - "
  1525. "not ready after error recovery\n");
  1526. sdev = scmd->device;
  1527. mutex_lock(&sdev->state_mutex);
  1528. scsi_device_set_state(sdev, SDEV_OFFLINE);
  1529. mutex_unlock(&sdev->state_mutex);
  1530. scsi_eh_finish_cmd(scmd, done_q);
  1531. }
  1532. return;
  1533. }
  1534. /**
  1535. * scsi_noretry_cmd - determine if command should be failed fast
  1536. * @scmd: SCSI cmd to examine.
  1537. */
  1538. int scsi_noretry_cmd(struct scsi_cmnd *scmd)
  1539. {
  1540. switch (host_byte(scmd->result)) {
  1541. case DID_OK:
  1542. break;
  1543. case DID_TIME_OUT:
  1544. goto check_type;
  1545. case DID_BUS_BUSY:
  1546. return (scmd->request->cmd_flags & REQ_FAILFAST_TRANSPORT);
  1547. case DID_PARITY:
  1548. return (scmd->request->cmd_flags & REQ_FAILFAST_DEV);
  1549. case DID_ERROR:
  1550. if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
  1551. status_byte(scmd->result) == RESERVATION_CONFLICT)
  1552. return 0;
  1553. /* fall through */
  1554. case DID_SOFT_ERROR:
  1555. return (scmd->request->cmd_flags & REQ_FAILFAST_DRIVER);
  1556. }
  1557. if (status_byte(scmd->result) != CHECK_CONDITION)
  1558. return 0;
  1559. check_type:
  1560. /*
  1561. * assume caller has checked sense and determined
  1562. * the check condition was retryable.
  1563. */
  1564. if (scmd->request->cmd_flags & REQ_FAILFAST_DEV ||
  1565. blk_rq_is_passthrough(scmd->request))
  1566. return 1;
  1567. else
  1568. return 0;
  1569. }
  1570. /**
  1571. * scsi_decide_disposition - Disposition a cmd on return from LLD.
  1572. * @scmd: SCSI cmd to examine.
  1573. *
  1574. * Notes:
  1575. * This is *only* called when we are examining the status after sending
  1576. * out the actual data command. any commands that are queued for error
  1577. * recovery (e.g. test_unit_ready) do *not* come through here.
  1578. *
  1579. * When this routine r…