PageRenderTime 70ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/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
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.0
  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 returns failed, it means the error handler thread
  1580. * is woken. In cases where the error code indicates an error that
  1581. * doesn't require the error handler read (i.e. we don't need to
  1582. * abort/reset), this function should return SUCCESS.
  1583. */
  1584. int scsi_decide_disposition(struct scsi_cmnd *scmd)
  1585. {
  1586. int rtn;
  1587. /*
  1588. * if the device is offline, then we clearly just pass the result back
  1589. * up to the top level.
  1590. */
  1591. if (!scsi_device_online(scmd->device)) {
  1592. SCSI_LOG_ERROR_RECOVERY(5, scmd_printk(KERN_INFO, scmd,
  1593. "%s: device offline - report as SUCCESS\n", __func__));
  1594. return SUCCESS;
  1595. }
  1596. /*
  1597. * first check the host byte, to see if there is anything in there
  1598. * that would indicate what we need to do.
  1599. */
  1600. switch (host_byte(scmd->result)) {
  1601. case DID_PASSTHROUGH:
  1602. /*
  1603. * no matter what, pass this through to the upper layer.
  1604. * nuke this special code so that it looks like we are saying
  1605. * did_ok.
  1606. */
  1607. scmd->result &= 0xff00ffff;
  1608. return SUCCESS;
  1609. case DID_OK:
  1610. /*
  1611. * looks good. drop through, and check the next byte.
  1612. */
  1613. break;
  1614. case DID_ABORT:
  1615. if (scmd->eh_eflags & SCSI_EH_ABORT_SCHEDULED) {
  1616. set_host_byte(scmd, DID_TIME_OUT);
  1617. return SUCCESS;
  1618. }
  1619. /* FALLTHROUGH */
  1620. case DID_NO_CONNECT:
  1621. case DID_BAD_TARGET:
  1622. /*
  1623. * note - this means that we just report the status back
  1624. * to the top level driver, not that we actually think
  1625. * that it indicates SUCCESS.
  1626. */
  1627. return SUCCESS;
  1628. case DID_SOFT_ERROR:
  1629. /*
  1630. * when the low level driver returns did_soft_error,
  1631. * it is responsible for keeping an internal retry counter
  1632. * in order to avoid endless loops (db)
  1633. */
  1634. goto maybe_retry;
  1635. case DID_IMM_RETRY:
  1636. return NEEDS_RETRY;
  1637. case DID_REQUEUE:
  1638. return ADD_TO_MLQUEUE;
  1639. case DID_TRANSPORT_DISRUPTED:
  1640. /*
  1641. * LLD/transport was disrupted during processing of the IO.
  1642. * The transport class is now blocked/blocking,
  1643. * and the transport will decide what to do with the IO
  1644. * based on its timers and recovery capablilities if
  1645. * there are enough retries.
  1646. */
  1647. goto maybe_retry;
  1648. case DID_TRANSPORT_FAILFAST:
  1649. /*
  1650. * The transport decided to failfast the IO (most likely
  1651. * the fast io fail tmo fired), so send IO directly upwards.
  1652. */
  1653. return SUCCESS;
  1654. case DID_ERROR:
  1655. if (msg_byte(scmd->result) == COMMAND_COMPLETE &&
  1656. status_byte(scmd->result) == RESERVATION_CONFLICT)
  1657. /*
  1658. * execute reservation conflict processing code
  1659. * lower down
  1660. */
  1661. break;
  1662. /* fallthrough */
  1663. case DID_BUS_BUSY:
  1664. case DID_PARITY:
  1665. goto maybe_retry;
  1666. case DID_TIME_OUT:
  1667. /*
  1668. * when we scan the bus, we get timeout messages for
  1669. * these commands if there is no device available.
  1670. * other hosts report did_no_connect for the same thing.
  1671. */
  1672. if ((scmd->cmnd[0] == TEST_UNIT_READY ||
  1673. scmd->cmnd[0] == INQUIRY)) {
  1674. return SUCCESS;
  1675. } else {
  1676. return FAILED;
  1677. }
  1678. case DID_RESET:
  1679. return SUCCESS;
  1680. default:
  1681. return FAILED;
  1682. }
  1683. /*
  1684. * next, check the message byte.
  1685. */
  1686. if (msg_byte(scmd->result) != COMMAND_COMPLETE)
  1687. return FAILED;
  1688. /*
  1689. * check the status byte to see if this indicates anything special.
  1690. */
  1691. switch (status_byte(scmd->result)) {
  1692. case QUEUE_FULL:
  1693. scsi_handle_queue_full(scmd->device);
  1694. /*
  1695. * the case of trying to send too many commands to a
  1696. * tagged queueing device.
  1697. */
  1698. /* FALLTHROUGH */
  1699. case BUSY:
  1700. /*
  1701. * device can't talk to us at the moment. Should only
  1702. * occur (SAM-3) when the task queue is empty, so will cause
  1703. * the empty queue handling to trigger a stall in the
  1704. * device.
  1705. */
  1706. return ADD_TO_MLQUEUE;
  1707. case GOOD:
  1708. if (scmd->cmnd[0] == REPORT_LUNS)
  1709. scmd->device->sdev_target->expecting_lun_change = 0;
  1710. scsi_handle_queue_ramp_up(scmd->device);
  1711. /* FALLTHROUGH */
  1712. case COMMAND_TERMINATED:
  1713. return SUCCESS;
  1714. case TASK_ABORTED:
  1715. goto maybe_retry;
  1716. case CHECK_CONDITION:
  1717. rtn = scsi_check_sense(scmd);
  1718. if (rtn == NEEDS_RETRY)
  1719. goto maybe_retry;
  1720. /* if rtn == FAILED, we have no sense information;
  1721. * returning FAILED will wake the error handler thread
  1722. * to collect the sense and redo the decide
  1723. * disposition */
  1724. return rtn;
  1725. case CONDITION_GOOD:
  1726. case INTERMEDIATE_GOOD:
  1727. case INTERMEDIATE_C_GOOD:
  1728. case ACA_ACTIVE:
  1729. /*
  1730. * who knows? FIXME(eric)
  1731. */
  1732. return SUCCESS;
  1733. case RESERVATION_CONFLICT:
  1734. sdev_printk(KERN_INFO, scmd->device,
  1735. "reservation conflict\n");
  1736. set_host_byte(scmd, DID_NEXUS_FAILURE);
  1737. return SUCCESS; /* causes immediate i/o error */
  1738. default:
  1739. return FAILED;
  1740. }
  1741. return FAILED;
  1742. maybe_retry:
  1743. /* we requeue for retry because the error was retryable, and
  1744. * the request was not marked fast fail. Note that above,
  1745. * even if the request is marked fast fail, we still requeue
  1746. * for queue congestion conditions (QUEUE_FULL or BUSY) */
  1747. if ((++scmd->retries) <= scmd->allowed
  1748. && !scsi_noretry_cmd(scmd)) {
  1749. return NEEDS_RETRY;
  1750. } else {
  1751. /*
  1752. * no more retries - report this one back to upper level.
  1753. */
  1754. return SUCCESS;
  1755. }
  1756. }
  1757. static void eh_lock_door_done(struct request *req, blk_status_t status)
  1758. {
  1759. blk_put_request(req);
  1760. }
  1761. /**
  1762. * scsi_eh_lock_door - Prevent medium removal for the specified device
  1763. * @sdev: SCSI device to prevent medium removal
  1764. *
  1765. * Locking:
  1766. * We must be called from process context.
  1767. *
  1768. * Notes:
  1769. * We queue up an asynchronous "ALLOW MEDIUM REMOVAL" request on the
  1770. * head of the devices request queue, and continue.
  1771. */
  1772. static void scsi_eh_lock_door(struct scsi_device *sdev)
  1773. {
  1774. struct request *req;
  1775. struct scsi_request *rq;
  1776. req = blk_get_request(sdev->request_queue, REQ_OP_SCSI_IN, 0);
  1777. if (IS_ERR(req))
  1778. return;
  1779. rq = scsi_req(req);
  1780. rq->cmd[0] = ALLOW_MEDIUM_REMOVAL;
  1781. rq->cmd[1] = 0;
  1782. rq->cmd[2] = 0;
  1783. rq->cmd[3] = 0;
  1784. rq->cmd[4] = SCSI_REMOVAL_PREVENT;
  1785. rq->cmd[5] = 0;
  1786. rq->cmd_len = COMMAND_SIZE(rq->cmd[0]);
  1787. req->rq_flags |= RQF_QUIET;
  1788. req->timeout = 10 * HZ;
  1789. rq->retries = 5;
  1790. blk_execute_rq_nowait(req->q, NULL, req, 1, eh_lock_door_done);
  1791. }
  1792. /**
  1793. * scsi_restart_operations - restart io operations to the specified host.
  1794. * @shost: Host we are restarting.
  1795. *
  1796. * Notes:
  1797. * When we entered the error handler, we blocked all further i/o to
  1798. * this device. we need to 'reverse' this process.
  1799. */
  1800. static void scsi_restart_operations(struct Scsi_Host *shost)
  1801. {
  1802. struct scsi_device *sdev;
  1803. unsigned long flags;
  1804. /*
  1805. * If the door was locked, we need to insert a door lock request
  1806. * onto the head of the SCSI request queue for the device. There
  1807. * is no point trying to lock the door of an off-line device.
  1808. */
  1809. shost_for_each_device(sdev, shost) {
  1810. if (scsi_device_online(sdev) && sdev->was_reset && sdev->locked) {
  1811. scsi_eh_lock_door(sdev);
  1812. sdev->was_reset = 0;
  1813. }
  1814. }
  1815. /*
  1816. * next free up anything directly waiting upon the host. this
  1817. * will be requests for character device operations, and also for
  1818. * ioctls to queued block devices.
  1819. */
  1820. SCSI_LOG_ERROR_RECOVERY(3,
  1821. shost_printk(KERN_INFO, shost, "waking up host to restart\n"));
  1822. spin_lock_irqsave(shost->host_lock, flags);
  1823. if (scsi_host_set_state(shost, SHOST_RUNNING))
  1824. if (scsi_host_set_state(shost, SHOST_CANCEL))
  1825. BUG_ON(scsi_host_set_state(shost, SHOST_DEL));
  1826. spin_unlock_irqrestore(shost->host_lock, flags);
  1827. wake_up(&shost->host_wait);
  1828. /*
  1829. * finally we need to re-initiate requests that may be pending. we will
  1830. * have had everything blocked while error handling is taking place, and
  1831. * now that error recovery is done, we will need to ensure that these
  1832. * requests are started.
  1833. */
  1834. scsi_run_host_queues(shost);
  1835. /*
  1836. * if eh is active and host_eh_scheduled is pending we need to re-run
  1837. * recovery. we do this check after scsi_run_host_queues() to allow
  1838. * everything pent up since the last eh run a chance to make forward
  1839. * progress before we sync again. Either we'll immediately re-run
  1840. * recovery or scsi_device_unbusy() will wake us again when these
  1841. * pending commands complete.
  1842. */
  1843. spin_lock_irqsave(shost->host_lock, flags);
  1844. if (shost->host_eh_scheduled)
  1845. if (scsi_host_set_state(shost, SHOST_RECOVERY))
  1846. WARN_ON(scsi_host_set_state(shost, SHOST_CANCEL_RECOVERY));
  1847. spin_unlock_irqrestore(shost->host_lock, flags);
  1848. }
  1849. /**
  1850. * scsi_eh_ready_devs - check device ready state and recover if not.
  1851. * @shost: host to be recovered.
  1852. * @work_q: &list_head for pending commands.
  1853. * @done_q: &list_head for processed commands.
  1854. */
  1855. void scsi_eh_ready_devs(struct Scsi_Host *shost,
  1856. struct list_head *work_q,
  1857. struct list_head *done_q)
  1858. {
  1859. if (!scsi_eh_stu(shost, work_q, done_q))
  1860. if (!scsi_eh_bus_device_reset(shost, work_q, done_q))
  1861. if (!scsi_eh_target_reset(shost, work_q, done_q))
  1862. if (!scsi_eh_bus_reset(shost, work_q, done_q))
  1863. if (!scsi_eh_host_reset(shost, work_q, done_q))
  1864. scsi_eh_offline_sdevs(work_q,
  1865. done_q);
  1866. }
  1867. EXPORT_SYMBOL_GPL(scsi_eh_ready_devs);
  1868. /**
  1869. * scsi_eh_flush_done_q - finish processed commands or retry them.
  1870. * @done_q: list_head of processed commands.
  1871. */
  1872. void scsi_eh_flush_done_q(struct list_head *done_q)
  1873. {
  1874. struct scsi_cmnd *scmd, *next;
  1875. list_for_each_entry_safe(scmd, next, done_q, eh_entry) {
  1876. list_del_init(&scmd->eh_entry);
  1877. if (scsi_device_online(scmd->device) &&
  1878. !scsi_noretry_cmd(scmd) &&
  1879. (++scmd->retries <= scmd->allowed)) {
  1880. SCSI_LOG_ERROR_RECOVERY(3,
  1881. scmd_printk(KERN_INFO, scmd,
  1882. "%s: flush retry cmd\n",
  1883. current->comm));
  1884. scsi_queue_insert(scmd, SCSI_MLQUEUE_EH_RETRY);
  1885. } else {
  1886. /*
  1887. * If just we got sense for the device (called
  1888. * scsi_eh_get_sense), scmd->result is already
  1889. * set, do not set DRIVER_TIMEOUT.
  1890. */
  1891. if (!scmd->result)
  1892. scmd->result |= (DRIVER_TIMEOUT << 24);
  1893. SCSI_LOG_ERROR_RECOVERY(3,
  1894. scmd_printk(KERN_INFO, scmd,
  1895. "%s: flush finish cmd\n",
  1896. current->comm));
  1897. scsi_finish_command(scmd);
  1898. }
  1899. }
  1900. }
  1901. EXPORT_SYMBOL(scsi_eh_flush_done_q);
  1902. /**
  1903. * scsi_unjam_host - Attempt to fix a host which has a cmd that failed.
  1904. * @shost: Host to unjam.
  1905. *
  1906. * Notes:
  1907. * When we come in here, we *know* that all commands on the bus have
  1908. * either completed, failed or timed out. we also know that no further
  1909. * commands are being sent to the host, so things are relatively quiet
  1910. * and we have freedom to fiddle with things as we wish.
  1911. *
  1912. * This is only the *default* implementation. it is possible for
  1913. * individual drivers to supply their own version of this function, and
  1914. * if the maintainer wishes to do this, it is strongly suggested that
  1915. * this function be taken as a template and modified. this function
  1916. * was designed to correctly handle problems for about 95% of the
  1917. * different cases out there, and it should always provide at least a
  1918. * reasonable amount of error recovery.
  1919. *
  1920. * Any command marked 'failed' or 'timeout' must eventually have
  1921. * scsi_finish_cmd() called for it. we do all of the retry stuff
  1922. * here, so when we restart the host after we return it should have an
  1923. * empty queue.
  1924. */
  1925. static void scsi_unjam_host(struct Scsi_Host *shost)
  1926. {
  1927. unsigned long flags;
  1928. LIST_HEAD(eh_work_q);
  1929. LIST_HEAD(eh_done_q);
  1930. spin_lock_irqsave(shost->host_lock, flags);
  1931. list_splice_init(&shost->eh_cmd_q, &eh_work_q);
  1932. spin_unlock_irqrestore(shost->host_lock, flags);
  1933. SCSI_LOG_ERROR_RECOVERY(1, scsi_eh_prt_fail_stats(shost, &eh_work_q));
  1934. if (!scsi_eh_get_sense(&eh_work_q, &eh_done_q))
  1935. scsi_eh_ready_devs(shost, &eh_work_q, &eh_done_q);
  1936. spin_lock_irqsave(shost->host_lock, flags);
  1937. if (shost->eh_deadline != -1)
  1938. shost->last_reset = 0;
  1939. spin_unlock_irqrestore(shost->host_lock, flags);
  1940. scsi_eh_flush_done_q(&eh_done_q);
  1941. }
  1942. /**
  1943. * scsi_error_handler - SCSI error handler thread
  1944. * @data: Host for which we are running.
  1945. *
  1946. * Notes:
  1947. * This is the main error handling loop. This is run as a kernel thread
  1948. * for every SCSI host and handles all error handling activity.
  1949. */
  1950. int scsi_error_handler(void *data)
  1951. {
  1952. struct Scsi_Host *shost = data;
  1953. /*
  1954. * We use TASK_INTERRUPTIBLE so that the thread is not
  1955. * counted against the load average as a running process.
  1956. * We never actually get interrupted because kthread_run
  1957. * disables signal delivery for the created thread.
  1958. */
  1959. while (true) {
  1960. /*
  1961. * The sequence in kthread_stop() sets the stop flag first
  1962. * then wakes the process. To avoid missed wakeups, the task
  1963. * should always be in a non running state before the stop
  1964. * flag is checked
  1965. */
  1966. set_current_state(TASK_INTERRUPTIBLE);
  1967. if (kthread_should_stop())
  1968. break;
  1969. if ((shost->host_failed == 0 && shost->host_eh_scheduled == 0) ||
  1970. shost->host_failed != scsi_host_busy(shost)) {
  1971. SCSI_LOG_ERROR_RECOVERY(1,
  1972. shost_printk(KERN_INFO, shost,
  1973. "scsi_eh_%d: sleeping\n",
  1974. shost->host_no));
  1975. schedule();
  1976. continue;
  1977. }
  1978. __set_current_state(TASK_RUNNING);
  1979. SCSI_LOG_ERROR_RECOVERY(1,
  1980. shost_printk(KERN_INFO, shost,
  1981. "scsi_eh_%d: waking up %d/%d/%d\n",
  1982. shost->host_no, shost->host_eh_scheduled,
  1983. shost->host_failed,
  1984. scsi_host_busy(shost)));
  1985. /*
  1986. * We have a host that is failing for some reason. Figure out
  1987. * what we need to do to get it up and online again (if we can).
  1988. * If we fail, we end up taking the thing offline.
  1989. */
  1990. if (!shost->eh_noresume && scsi_autopm_get_host(shost) != 0) {
  1991. SCSI_LOG_ERROR_RECOVERY(1,
  1992. shost_printk(KERN_ERR, shost,
  1993. "scsi_eh_%d: unable to autoresume\n",
  1994. shost->host_no));
  1995. continue;
  1996. }
  1997. if (shost->transportt->eh_strategy_handler)
  1998. shost->transportt->eh_strategy_handler(shost);
  1999. else
  2000. scsi_unjam_host(shost);
  2001. /* All scmds have been handled */
  2002. shost->host_failed = 0;
  2003. /*
  2004. * Note - if the above fails completely, the action is to take
  2005. * individual devices offline and flush the queue of any
  2006. * outstanding requests that may have been pending. When we
  2007. * restart, we restart any I/O to any other devices on the bus
  2008. * which are still online.
  2009. */
  2010. scsi_restart_operations(shost);
  2011. if (!shost->eh_noresume)
  2012. scsi_autopm_put_host(shost);
  2013. }
  2014. __set_current_state(TASK_RUNNING);
  2015. SCSI_LOG_ERROR_RECOVERY(1,
  2016. shost_printk(KERN_INFO, shost,
  2017. "Error handler scsi_eh_%d exiting\n",
  2018. shost->host_no));
  2019. shost->ehandler = NULL;
  2020. return 0;
  2021. }
  2022. /*
  2023. * Function: scsi_report_bus_reset()
  2024. *
  2025. * Purpose: Utility function used by low-level drivers to report that
  2026. * they have observed a bus reset on the bus being handled.
  2027. *
  2028. * Arguments: shost - Host in question
  2029. * channel - channel on which reset was observed.
  2030. *
  2031. * Returns: Nothing
  2032. *
  2033. * Lock status: Host lock must be held.
  2034. *
  2035. * Notes: This only needs to be called if the reset is one which
  2036. * originates from an unknown location. Resets originated
  2037. * by the mid-level itself don't need to call this, but there
  2038. * should be no harm.
  2039. *
  2040. * The main purpose of this is to make sure that a CHECK_CONDITION
  2041. * is properly treated.
  2042. */
  2043. void scsi_report_bus_reset(struct Scsi_Host *shost, int channel)
  2044. {
  2045. struct scsi_device *sdev;
  2046. __shost_for_each_device(sdev, shost) {
  2047. if (channel == sdev_channel(sdev))
  2048. __scsi_report_device_reset(sdev, NULL);
  2049. }
  2050. }
  2051. EXPORT_SYMBOL(scsi_report_bus_reset);
  2052. /*
  2053. * Function: scsi_report_device_reset()
  2054. *
  2055. * Purpose: Utility function used by low-level drivers to report that
  2056. * they have observed a device reset on the device being handled.
  2057. *
  2058. * Arguments: shost - Host in question
  2059. * channel - channel on which reset was observed
  2060. * target - target on which reset was observed
  2061. *
  2062. * Returns: Nothing
  2063. *
  2064. * Lock status: Host lock must be held
  2065. *
  2066. * Notes: This only needs to be called if the reset is one which
  2067. * originates from an unknown location. Resets originated
  2068. * by the mid-level itself don't need to call this, but there
  2069. * should be no harm.
  2070. *
  2071. * The main purpose of this is to make sure that a CHECK_CONDITION
  2072. * is properly treated.
  2073. */
  2074. void scsi_report_device_reset(struct Scsi_Host *shost, int channel, int target)
  2075. {
  2076. struct scsi_device *sdev;
  2077. __shost_for_each_device(sdev, shost) {
  2078. if (channel == sdev_channel(sdev) &&
  2079. target == sdev_id(sdev))
  2080. __scsi_report_device_reset(sdev, NULL);
  2081. }
  2082. }
  2083. EXPORT_SYMBOL(scsi_report_device_reset);
  2084. static void
  2085. scsi_reset_provider_done_command(struct scsi_cmnd *scmd)
  2086. {
  2087. }
  2088. /**
  2089. * scsi_ioctl_reset: explicitly reset a host/bus/target/device
  2090. * @dev: scsi_device to operate on
  2091. * @arg: reset type (see sg.h)
  2092. */
  2093. int
  2094. scsi_ioctl_reset(struct scsi_device *dev, int __user *arg)
  2095. {
  2096. struct scsi_cmnd *scmd;
  2097. struct Scsi_Host *shost = dev->host;
  2098. struct request *rq;
  2099. unsigned long flags;
  2100. int error = 0, rtn, val;
  2101. if (!capable(CAP_SYS_ADMIN) || !capable(CAP_SYS_RAWIO))
  2102. return -EACCES;
  2103. error = get_user(val, arg);
  2104. if (error)
  2105. return error;
  2106. if (scsi_autopm_get_host(shost) < 0)
  2107. return -EIO;
  2108. error = -EIO;
  2109. rq = kzalloc(sizeof(struct request) + sizeof(struct scsi_cmnd) +
  2110. shost->hostt->cmd_size, GFP_KERNEL);
  2111. if (!rq)
  2112. goto out_put_autopm_host;
  2113. blk_rq_init(NULL, rq);
  2114. scmd = (struct scsi_cmnd *)(rq + 1);
  2115. scsi_init_command(dev, scmd);
  2116. scmd->request = rq;
  2117. scmd->cmnd = scsi_req(rq)->cmd;
  2118. scmd->scsi_done = scsi_reset_provider_done_command;
  2119. memset(&scmd->sdb, 0, sizeof(scmd->sdb));
  2120. scmd->cmd_len = 0;
  2121. scmd->sc_data_direction = DMA_BIDIRECTIONAL;
  2122. spin_lock_irqsave(shost->host_lock, flags);
  2123. shost->tmf_in_progress = 1;
  2124. spin_unlock_irqrestore(shost->host_lock, flags);
  2125. switch (val & ~SG_SCSI_RESET_NO_ESCALATE) {
  2126. case SG_SCSI_RESET_NOTHING:
  2127. rtn = SUCCESS;
  2128. break;
  2129. case SG_SCSI_RESET_DEVICE:
  2130. rtn = scsi_try_bus_device_reset(scmd);
  2131. if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
  2132. break;
  2133. /* FALLTHROUGH */
  2134. case SG_SCSI_RESET_TARGET:
  2135. rtn = scsi_try_target_reset(scmd);
  2136. if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
  2137. break;
  2138. /* FALLTHROUGH */
  2139. case SG_SCSI_RESET_BUS:
  2140. rtn = scsi_try_bus_reset(scmd);
  2141. if (rtn == SUCCESS || (val & SG_SCSI_RESET_NO_ESCALATE))
  2142. break;
  2143. /* FALLTHROUGH */
  2144. case SG_SCSI_RESET_HOST:
  2145. rtn = scsi_try_host_reset(scmd);
  2146. if (rtn == SUCCESS)
  2147. break;
  2148. /* FALLTHROUGH */
  2149. default:
  2150. rtn = FAILED;
  2151. break;
  2152. }
  2153. error = (rtn == SUCCESS) ? 0 : -EIO;
  2154. spin_lock_irqsave(shost->host_lock, flags);
  2155. shost->tmf_in_progress = 0;
  2156. spin_unlock_irqrestore(shost->host_lock, flags);
  2157. /*
  2158. * be sure to wake up anyone who was sleeping or had their queue
  2159. * suspended while we performed the TMF.
  2160. */
  2161. SCSI_LOG_ERROR_RECOVERY(3,
  2162. shost_printk(KERN_INFO, shost,
  2163. "waking up host to restart after TMF\n"));
  2164. wake_up(&shost->host_wait);
  2165. scsi_run_host_queues(shost);
  2166. kfree(rq);
  2167. out_put_autopm_host:
  2168. scsi_autopm_put_host(shost);
  2169. return error;
  2170. }
  2171. bool scsi_command_normalize_sense(const struct scsi_cmnd *cmd,
  2172. struct scsi_sense_hdr *sshdr)
  2173. {
  2174. return scsi_normalize_sense(cmd->sense_buffer,
  2175. SCSI_SENSE_BUFFERSIZE, sshdr);
  2176. }
  2177. EXPORT_SYMBOL(scsi_command_normalize_sense);
  2178. /**
  2179. * scsi_get_sense_info_fld - get information field from sense data (either fixed or descriptor format)
  2180. * @sense_buffer: byte array of sense data
  2181. * @sb_len: number of valid bytes in sense_buffer
  2182. * @info_out: pointer to 64 integer where 8 or 4 byte information
  2183. * field will be placed if found.
  2184. *
  2185. * Return value:
  2186. * true if information field found, false if not found.
  2187. */
  2188. bool scsi_get_sense_info_fld(const u8 *sense_buffer, int sb_len,
  2189. u64 *info_out)
  2190. {
  2191. const u8 * ucp;
  2192. if (sb_len < 7)
  2193. return false;
  2194. switch (sense_buffer[0] & 0x7f) {
  2195. case 0x70:
  2196. case 0x71:
  2197. if (sense_buffer[0] & 0x80) {
  2198. *info_out = get_unaligned_be32(&sense_buffer[3]);
  2199. return true;
  2200. }
  2201. return false;
  2202. case 0x72:
  2203. case 0x73:
  2204. ucp = scsi_sense_desc_find(sense_buffer, sb_len,
  2205. 0 /* info desc */);
  2206. if (ucp && (0xa == ucp[1])) {
  2207. *info_out = get_unaligned_be64(&ucp[4]);
  2208. return true;
  2209. }
  2210. return false;
  2211. default:
  2212. return false;
  2213. }
  2214. }
  2215. EXPORT_SYMBOL(scsi_get_sense_info_fld);