PageRenderTime 73ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

/drivers/ata/libata-eh.c

http://github.com/mirrors/linux
C | 3976 lines | 2313 code | 532 blank | 1131 comment | 500 complexity | a860a0d0ecf0a8a4d070f5c92e87c4d4 MD5 | raw file
Possible License(s): AGPL-1.0, GPL-2.0, LGPL-2.0
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * libata-eh.c - libata error handling
  4. *
  5. * Copyright 2006 Tejun Heo <htejun@gmail.com>
  6. *
  7. * libata documentation is available via 'make {ps|pdf}docs',
  8. * as Documentation/driver-api/libata.rst
  9. *
  10. * Hardware documentation available from http://www.t13.org/ and
  11. * http://www.sata-io.org/
  12. */
  13. #include <linux/kernel.h>
  14. #include <linux/blkdev.h>
  15. #include <linux/export.h>
  16. #include <linux/pci.h>
  17. #include <scsi/scsi.h>
  18. #include <scsi/scsi_host.h>
  19. #include <scsi/scsi_eh.h>
  20. #include <scsi/scsi_device.h>
  21. #include <scsi/scsi_cmnd.h>
  22. #include <scsi/scsi_dbg.h>
  23. #include "../scsi/scsi_transport_api.h"
  24. #include <linux/libata.h>
  25. #include <trace/events/libata.h>
  26. #include "libata.h"
  27. enum {
  28. /* speed down verdicts */
  29. ATA_EH_SPDN_NCQ_OFF = (1 << 0),
  30. ATA_EH_SPDN_SPEED_DOWN = (1 << 1),
  31. ATA_EH_SPDN_FALLBACK_TO_PIO = (1 << 2),
  32. ATA_EH_SPDN_KEEP_ERRORS = (1 << 3),
  33. /* error flags */
  34. ATA_EFLAG_IS_IO = (1 << 0),
  35. ATA_EFLAG_DUBIOUS_XFER = (1 << 1),
  36. ATA_EFLAG_OLD_ER = (1 << 31),
  37. /* error categories */
  38. ATA_ECAT_NONE = 0,
  39. ATA_ECAT_ATA_BUS = 1,
  40. ATA_ECAT_TOUT_HSM = 2,
  41. ATA_ECAT_UNK_DEV = 3,
  42. ATA_ECAT_DUBIOUS_NONE = 4,
  43. ATA_ECAT_DUBIOUS_ATA_BUS = 5,
  44. ATA_ECAT_DUBIOUS_TOUT_HSM = 6,
  45. ATA_ECAT_DUBIOUS_UNK_DEV = 7,
  46. ATA_ECAT_NR = 8,
  47. ATA_EH_CMD_DFL_TIMEOUT = 5000,
  48. /* always put at least this amount of time between resets */
  49. ATA_EH_RESET_COOL_DOWN = 5000,
  50. /* Waiting in ->prereset can never be reliable. It's
  51. * sometimes nice to wait there but it can't be depended upon;
  52. * otherwise, we wouldn't be resetting. Just give it enough
  53. * time for most drives to spin up.
  54. */
  55. ATA_EH_PRERESET_TIMEOUT = 10000,
  56. ATA_EH_FASTDRAIN_INTERVAL = 3000,
  57. ATA_EH_UA_TRIES = 5,
  58. /* probe speed down parameters, see ata_eh_schedule_probe() */
  59. ATA_EH_PROBE_TRIAL_INTERVAL = 60000, /* 1 min */
  60. ATA_EH_PROBE_TRIALS = 2,
  61. };
  62. /* The following table determines how we sequence resets. Each entry
  63. * represents timeout for that try. The first try can be soft or
  64. * hardreset. All others are hardreset if available. In most cases
  65. * the first reset w/ 10sec timeout should succeed. Following entries
  66. * are mostly for error handling, hotplug and those outlier devices that
  67. * take an exceptionally long time to recover from reset.
  68. */
  69. static const unsigned long ata_eh_reset_timeouts[] = {
  70. 10000, /* most drives spin up by 10sec */
  71. 10000, /* > 99% working drives spin up before 20sec */
  72. 35000, /* give > 30 secs of idleness for outlier devices */
  73. 5000, /* and sweet one last chance */
  74. ULONG_MAX, /* > 1 min has elapsed, give up */
  75. };
  76. static const unsigned long ata_eh_identify_timeouts[] = {
  77. 5000, /* covers > 99% of successes and not too boring on failures */
  78. 10000, /* combined time till here is enough even for media access */
  79. 30000, /* for true idiots */
  80. ULONG_MAX,
  81. };
  82. static const unsigned long ata_eh_flush_timeouts[] = {
  83. 15000, /* be generous with flush */
  84. 15000, /* ditto */
  85. 30000, /* and even more generous */
  86. ULONG_MAX,
  87. };
  88. static const unsigned long ata_eh_other_timeouts[] = {
  89. 5000, /* same rationale as identify timeout */
  90. 10000, /* ditto */
  91. /* but no merciful 30sec for other commands, it just isn't worth it */
  92. ULONG_MAX,
  93. };
  94. struct ata_eh_cmd_timeout_ent {
  95. const u8 *commands;
  96. const unsigned long *timeouts;
  97. };
  98. /* The following table determines timeouts to use for EH internal
  99. * commands. Each table entry is a command class and matches the
  100. * commands the entry applies to and the timeout table to use.
  101. *
  102. * On the retry after a command timed out, the next timeout value from
  103. * the table is used. If the table doesn't contain further entries,
  104. * the last value is used.
  105. *
  106. * ehc->cmd_timeout_idx keeps track of which timeout to use per
  107. * command class, so if SET_FEATURES times out on the first try, the
  108. * next try will use the second timeout value only for that class.
  109. */
  110. #define CMDS(cmds...) (const u8 []){ cmds, 0 }
  111. static const struct ata_eh_cmd_timeout_ent
  112. ata_eh_cmd_timeout_table[ATA_EH_CMD_TIMEOUT_TABLE_SIZE] = {
  113. { .commands = CMDS(ATA_CMD_ID_ATA, ATA_CMD_ID_ATAPI),
  114. .timeouts = ata_eh_identify_timeouts, },
  115. { .commands = CMDS(ATA_CMD_READ_NATIVE_MAX, ATA_CMD_READ_NATIVE_MAX_EXT),
  116. .timeouts = ata_eh_other_timeouts, },
  117. { .commands = CMDS(ATA_CMD_SET_MAX, ATA_CMD_SET_MAX_EXT),
  118. .timeouts = ata_eh_other_timeouts, },
  119. { .commands = CMDS(ATA_CMD_SET_FEATURES),
  120. .timeouts = ata_eh_other_timeouts, },
  121. { .commands = CMDS(ATA_CMD_INIT_DEV_PARAMS),
  122. .timeouts = ata_eh_other_timeouts, },
  123. { .commands = CMDS(ATA_CMD_FLUSH, ATA_CMD_FLUSH_EXT),
  124. .timeouts = ata_eh_flush_timeouts },
  125. };
  126. #undef CMDS
  127. static void __ata_port_freeze(struct ata_port *ap);
  128. #ifdef CONFIG_PM
  129. static void ata_eh_handle_port_suspend(struct ata_port *ap);
  130. static void ata_eh_handle_port_resume(struct ata_port *ap);
  131. #else /* CONFIG_PM */
  132. static void ata_eh_handle_port_suspend(struct ata_port *ap)
  133. { }
  134. static void ata_eh_handle_port_resume(struct ata_port *ap)
  135. { }
  136. #endif /* CONFIG_PM */
  137. static __printf(2, 0) void __ata_ehi_pushv_desc(struct ata_eh_info *ehi,
  138. const char *fmt, va_list args)
  139. {
  140. ehi->desc_len += vscnprintf(ehi->desc + ehi->desc_len,
  141. ATA_EH_DESC_LEN - ehi->desc_len,
  142. fmt, args);
  143. }
  144. /**
  145. * __ata_ehi_push_desc - push error description without adding separator
  146. * @ehi: target EHI
  147. * @fmt: printf format string
  148. *
  149. * Format string according to @fmt and append it to @ehi->desc.
  150. *
  151. * LOCKING:
  152. * spin_lock_irqsave(host lock)
  153. */
  154. void __ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
  155. {
  156. va_list args;
  157. va_start(args, fmt);
  158. __ata_ehi_pushv_desc(ehi, fmt, args);
  159. va_end(args);
  160. }
  161. EXPORT_SYMBOL_GPL(__ata_ehi_push_desc);
  162. /**
  163. * ata_ehi_push_desc - push error description with separator
  164. * @ehi: target EHI
  165. * @fmt: printf format string
  166. *
  167. * Format string according to @fmt and append it to @ehi->desc.
  168. * If @ehi->desc is not empty, ", " is added in-between.
  169. *
  170. * LOCKING:
  171. * spin_lock_irqsave(host lock)
  172. */
  173. void ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
  174. {
  175. va_list args;
  176. if (ehi->desc_len)
  177. __ata_ehi_push_desc(ehi, ", ");
  178. va_start(args, fmt);
  179. __ata_ehi_pushv_desc(ehi, fmt, args);
  180. va_end(args);
  181. }
  182. EXPORT_SYMBOL_GPL(ata_ehi_push_desc);
  183. /**
  184. * ata_ehi_clear_desc - clean error description
  185. * @ehi: target EHI
  186. *
  187. * Clear @ehi->desc.
  188. *
  189. * LOCKING:
  190. * spin_lock_irqsave(host lock)
  191. */
  192. void ata_ehi_clear_desc(struct ata_eh_info *ehi)
  193. {
  194. ehi->desc[0] = '\0';
  195. ehi->desc_len = 0;
  196. }
  197. EXPORT_SYMBOL_GPL(ata_ehi_clear_desc);
  198. /**
  199. * ata_port_desc - append port description
  200. * @ap: target ATA port
  201. * @fmt: printf format string
  202. *
  203. * Format string according to @fmt and append it to port
  204. * description. If port description is not empty, " " is added
  205. * in-between. This function is to be used while initializing
  206. * ata_host. The description is printed on host registration.
  207. *
  208. * LOCKING:
  209. * None.
  210. */
  211. void ata_port_desc(struct ata_port *ap, const char *fmt, ...)
  212. {
  213. va_list args;
  214. WARN_ON(!(ap->pflags & ATA_PFLAG_INITIALIZING));
  215. if (ap->link.eh_info.desc_len)
  216. __ata_ehi_push_desc(&ap->link.eh_info, " ");
  217. va_start(args, fmt);
  218. __ata_ehi_pushv_desc(&ap->link.eh_info, fmt, args);
  219. va_end(args);
  220. }
  221. EXPORT_SYMBOL_GPL(ata_port_desc);
  222. #ifdef CONFIG_PCI
  223. /**
  224. * ata_port_pbar_desc - append PCI BAR description
  225. * @ap: target ATA port
  226. * @bar: target PCI BAR
  227. * @offset: offset into PCI BAR
  228. * @name: name of the area
  229. *
  230. * If @offset is negative, this function formats a string which
  231. * contains the name, address, size and type of the BAR and
  232. * appends it to the port description. If @offset is zero or
  233. * positive, only name and offsetted address is appended.
  234. *
  235. * LOCKING:
  236. * None.
  237. */
  238. void ata_port_pbar_desc(struct ata_port *ap, int bar, ssize_t offset,
  239. const char *name)
  240. {
  241. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  242. char *type = "";
  243. unsigned long long start, len;
  244. if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
  245. type = "m";
  246. else if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
  247. type = "i";
  248. start = (unsigned long long)pci_resource_start(pdev, bar);
  249. len = (unsigned long long)pci_resource_len(pdev, bar);
  250. if (offset < 0)
  251. ata_port_desc(ap, "%s %s%llu@0x%llx", name, type, len, start);
  252. else
  253. ata_port_desc(ap, "%s 0x%llx", name,
  254. start + (unsigned long long)offset);
  255. }
  256. EXPORT_SYMBOL_GPL(ata_port_pbar_desc);
  257. #endif /* CONFIG_PCI */
  258. static int ata_lookup_timeout_table(u8 cmd)
  259. {
  260. int i;
  261. for (i = 0; i < ATA_EH_CMD_TIMEOUT_TABLE_SIZE; i++) {
  262. const u8 *cur;
  263. for (cur = ata_eh_cmd_timeout_table[i].commands; *cur; cur++)
  264. if (*cur == cmd)
  265. return i;
  266. }
  267. return -1;
  268. }
  269. /**
  270. * ata_internal_cmd_timeout - determine timeout for an internal command
  271. * @dev: target device
  272. * @cmd: internal command to be issued
  273. *
  274. * Determine timeout for internal command @cmd for @dev.
  275. *
  276. * LOCKING:
  277. * EH context.
  278. *
  279. * RETURNS:
  280. * Determined timeout.
  281. */
  282. unsigned long ata_internal_cmd_timeout(struct ata_device *dev, u8 cmd)
  283. {
  284. struct ata_eh_context *ehc = &dev->link->eh_context;
  285. int ent = ata_lookup_timeout_table(cmd);
  286. int idx;
  287. if (ent < 0)
  288. return ATA_EH_CMD_DFL_TIMEOUT;
  289. idx = ehc->cmd_timeout_idx[dev->devno][ent];
  290. return ata_eh_cmd_timeout_table[ent].timeouts[idx];
  291. }
  292. /**
  293. * ata_internal_cmd_timed_out - notification for internal command timeout
  294. * @dev: target device
  295. * @cmd: internal command which timed out
  296. *
  297. * Notify EH that internal command @cmd for @dev timed out. This
  298. * function should be called only for commands whose timeouts are
  299. * determined using ata_internal_cmd_timeout().
  300. *
  301. * LOCKING:
  302. * EH context.
  303. */
  304. void ata_internal_cmd_timed_out(struct ata_device *dev, u8 cmd)
  305. {
  306. struct ata_eh_context *ehc = &dev->link->eh_context;
  307. int ent = ata_lookup_timeout_table(cmd);
  308. int idx;
  309. if (ent < 0)
  310. return;
  311. idx = ehc->cmd_timeout_idx[dev->devno][ent];
  312. if (ata_eh_cmd_timeout_table[ent].timeouts[idx + 1] != ULONG_MAX)
  313. ehc->cmd_timeout_idx[dev->devno][ent]++;
  314. }
  315. static void ata_ering_record(struct ata_ering *ering, unsigned int eflags,
  316. unsigned int err_mask)
  317. {
  318. struct ata_ering_entry *ent;
  319. WARN_ON(!err_mask);
  320. ering->cursor++;
  321. ering->cursor %= ATA_ERING_SIZE;
  322. ent = &ering->ring[ering->cursor];
  323. ent->eflags = eflags;
  324. ent->err_mask = err_mask;
  325. ent->timestamp = get_jiffies_64();
  326. }
  327. static struct ata_ering_entry *ata_ering_top(struct ata_ering *ering)
  328. {
  329. struct ata_ering_entry *ent = &ering->ring[ering->cursor];
  330. if (ent->err_mask)
  331. return ent;
  332. return NULL;
  333. }
  334. int ata_ering_map(struct ata_ering *ering,
  335. int (*map_fn)(struct ata_ering_entry *, void *),
  336. void *arg)
  337. {
  338. int idx, rc = 0;
  339. struct ata_ering_entry *ent;
  340. idx = ering->cursor;
  341. do {
  342. ent = &ering->ring[idx];
  343. if (!ent->err_mask)
  344. break;
  345. rc = map_fn(ent, arg);
  346. if (rc)
  347. break;
  348. idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
  349. } while (idx != ering->cursor);
  350. return rc;
  351. }
  352. static int ata_ering_clear_cb(struct ata_ering_entry *ent, void *void_arg)
  353. {
  354. ent->eflags |= ATA_EFLAG_OLD_ER;
  355. return 0;
  356. }
  357. static void ata_ering_clear(struct ata_ering *ering)
  358. {
  359. ata_ering_map(ering, ata_ering_clear_cb, NULL);
  360. }
  361. static unsigned int ata_eh_dev_action(struct ata_device *dev)
  362. {
  363. struct ata_eh_context *ehc = &dev->link->eh_context;
  364. return ehc->i.action | ehc->i.dev_action[dev->devno];
  365. }
  366. static void ata_eh_clear_action(struct ata_link *link, struct ata_device *dev,
  367. struct ata_eh_info *ehi, unsigned int action)
  368. {
  369. struct ata_device *tdev;
  370. if (!dev) {
  371. ehi->action &= ~action;
  372. ata_for_each_dev(tdev, link, ALL)
  373. ehi->dev_action[tdev->devno] &= ~action;
  374. } else {
  375. /* doesn't make sense for port-wide EH actions */
  376. WARN_ON(!(action & ATA_EH_PERDEV_MASK));
  377. /* break ehi->action into ehi->dev_action */
  378. if (ehi->action & action) {
  379. ata_for_each_dev(tdev, link, ALL)
  380. ehi->dev_action[tdev->devno] |=
  381. ehi->action & action;
  382. ehi->action &= ~action;
  383. }
  384. /* turn off the specified per-dev action */
  385. ehi->dev_action[dev->devno] &= ~action;
  386. }
  387. }
  388. /**
  389. * ata_eh_acquire - acquire EH ownership
  390. * @ap: ATA port to acquire EH ownership for
  391. *
  392. * Acquire EH ownership for @ap. This is the basic exclusion
  393. * mechanism for ports sharing a host. Only one port hanging off
  394. * the same host can claim the ownership of EH.
  395. *
  396. * LOCKING:
  397. * EH context.
  398. */
  399. void ata_eh_acquire(struct ata_port *ap)
  400. {
  401. mutex_lock(&ap->host->eh_mutex);
  402. WARN_ON_ONCE(ap->host->eh_owner);
  403. ap->host->eh_owner = current;
  404. }
  405. /**
  406. * ata_eh_release - release EH ownership
  407. * @ap: ATA port to release EH ownership for
  408. *
  409. * Release EH ownership for @ap if the caller. The caller must
  410. * have acquired EH ownership using ata_eh_acquire() previously.
  411. *
  412. * LOCKING:
  413. * EH context.
  414. */
  415. void ata_eh_release(struct ata_port *ap)
  416. {
  417. WARN_ON_ONCE(ap->host->eh_owner != current);
  418. ap->host->eh_owner = NULL;
  419. mutex_unlock(&ap->host->eh_mutex);
  420. }
  421. static void ata_eh_unload(struct ata_port *ap)
  422. {
  423. struct ata_link *link;
  424. struct ata_device *dev;
  425. unsigned long flags;
  426. /* Restore SControl IPM and SPD for the next driver and
  427. * disable attached devices.
  428. */
  429. ata_for_each_link(link, ap, PMP_FIRST) {
  430. sata_scr_write(link, SCR_CONTROL, link->saved_scontrol & 0xff0);
  431. ata_for_each_dev(dev, link, ALL)
  432. ata_dev_disable(dev);
  433. }
  434. /* freeze and set UNLOADED */
  435. spin_lock_irqsave(ap->lock, flags);
  436. ata_port_freeze(ap); /* won't be thawed */
  437. ap->pflags &= ~ATA_PFLAG_EH_PENDING; /* clear pending from freeze */
  438. ap->pflags |= ATA_PFLAG_UNLOADED;
  439. spin_unlock_irqrestore(ap->lock, flags);
  440. }
  441. /**
  442. * ata_scsi_error - SCSI layer error handler callback
  443. * @host: SCSI host on which error occurred
  444. *
  445. * Handles SCSI-layer-thrown error events.
  446. *
  447. * LOCKING:
  448. * Inherited from SCSI layer (none, can sleep)
  449. *
  450. * RETURNS:
  451. * Zero.
  452. */
  453. void ata_scsi_error(struct Scsi_Host *host)
  454. {
  455. struct ata_port *ap = ata_shost_to_port(host);
  456. unsigned long flags;
  457. LIST_HEAD(eh_work_q);
  458. DPRINTK("ENTER\n");
  459. spin_lock_irqsave(host->host_lock, flags);
  460. list_splice_init(&host->eh_cmd_q, &eh_work_q);
  461. spin_unlock_irqrestore(host->host_lock, flags);
  462. ata_scsi_cmd_error_handler(host, ap, &eh_work_q);
  463. /* If we timed raced normal completion and there is nothing to
  464. recover nr_timedout == 0 why exactly are we doing error recovery ? */
  465. ata_scsi_port_error_handler(host, ap);
  466. /* finish or retry handled scmd's and clean up */
  467. WARN_ON(!list_empty(&eh_work_q));
  468. DPRINTK("EXIT\n");
  469. }
  470. /**
  471. * ata_scsi_cmd_error_handler - error callback for a list of commands
  472. * @host: scsi host containing the port
  473. * @ap: ATA port within the host
  474. * @eh_work_q: list of commands to process
  475. *
  476. * process the given list of commands and return those finished to the
  477. * ap->eh_done_q. This function is the first part of the libata error
  478. * handler which processes a given list of failed commands.
  479. */
  480. void ata_scsi_cmd_error_handler(struct Scsi_Host *host, struct ata_port *ap,
  481. struct list_head *eh_work_q)
  482. {
  483. int i;
  484. unsigned long flags;
  485. /* make sure sff pio task is not running */
  486. ata_sff_flush_pio_task(ap);
  487. /* synchronize with host lock and sort out timeouts */
  488. /* For new EH, all qcs are finished in one of three ways -
  489. * normal completion, error completion, and SCSI timeout.
  490. * Both completions can race against SCSI timeout. When normal
  491. * completion wins, the qc never reaches EH. When error
  492. * completion wins, the qc has ATA_QCFLAG_FAILED set.
  493. *
  494. * When SCSI timeout wins, things are a bit more complex.
  495. * Normal or error completion can occur after the timeout but
  496. * before this point. In such cases, both types of
  497. * completions are honored. A scmd is determined to have
  498. * timed out iff its associated qc is active and not failed.
  499. */
  500. spin_lock_irqsave(ap->lock, flags);
  501. if (ap->ops->error_handler) {
  502. struct scsi_cmnd *scmd, *tmp;
  503. int nr_timedout = 0;
  504. /* This must occur under the ap->lock as we don't want
  505. a polled recovery to race the real interrupt handler
  506. The lost_interrupt handler checks for any completed but
  507. non-notified command and completes much like an IRQ handler.
  508. We then fall into the error recovery code which will treat
  509. this as if normal completion won the race */
  510. if (ap->ops->lost_interrupt)
  511. ap->ops->lost_interrupt(ap);
  512. list_for_each_entry_safe(scmd, tmp, eh_work_q, eh_entry) {
  513. struct ata_queued_cmd *qc;
  514. ata_qc_for_each_raw(ap, qc, i) {
  515. if (qc->flags & ATA_QCFLAG_ACTIVE &&
  516. qc->scsicmd == scmd)
  517. break;
  518. }
  519. if (i < ATA_MAX_QUEUE) {
  520. /* the scmd has an associated qc */
  521. if (!(qc->flags & ATA_QCFLAG_FAILED)) {
  522. /* which hasn't failed yet, timeout */
  523. qc->err_mask |= AC_ERR_TIMEOUT;
  524. qc->flags |= ATA_QCFLAG_FAILED;
  525. nr_timedout++;
  526. }
  527. } else {
  528. /* Normal completion occurred after
  529. * SCSI timeout but before this point.
  530. * Successfully complete it.
  531. */
  532. scmd->retries = scmd->allowed;
  533. scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
  534. }
  535. }
  536. /* If we have timed out qcs. They belong to EH from
  537. * this point but the state of the controller is
  538. * unknown. Freeze the port to make sure the IRQ
  539. * handler doesn't diddle with those qcs. This must
  540. * be done atomically w.r.t. setting QCFLAG_FAILED.
  541. */
  542. if (nr_timedout)
  543. __ata_port_freeze(ap);
  544. /* initialize eh_tries */
  545. ap->eh_tries = ATA_EH_MAX_TRIES;
  546. }
  547. spin_unlock_irqrestore(ap->lock, flags);
  548. }
  549. EXPORT_SYMBOL(ata_scsi_cmd_error_handler);
  550. /**
  551. * ata_scsi_port_error_handler - recover the port after the commands
  552. * @host: SCSI host containing the port
  553. * @ap: the ATA port
  554. *
  555. * Handle the recovery of the port @ap after all the commands
  556. * have been recovered.
  557. */
  558. void ata_scsi_port_error_handler(struct Scsi_Host *host, struct ata_port *ap)
  559. {
  560. unsigned long flags;
  561. /* invoke error handler */
  562. if (ap->ops->error_handler) {
  563. struct ata_link *link;
  564. /* acquire EH ownership */
  565. ata_eh_acquire(ap);
  566. repeat:
  567. /* kill fast drain timer */
  568. del_timer_sync(&ap->fastdrain_timer);
  569. /* process port resume request */
  570. ata_eh_handle_port_resume(ap);
  571. /* fetch & clear EH info */
  572. spin_lock_irqsave(ap->lock, flags);
  573. ata_for_each_link(link, ap, HOST_FIRST) {
  574. struct ata_eh_context *ehc = &link->eh_context;
  575. struct ata_device *dev;
  576. memset(&link->eh_context, 0, sizeof(link->eh_context));
  577. link->eh_context.i = link->eh_info;
  578. memset(&link->eh_info, 0, sizeof(link->eh_info));
  579. ata_for_each_dev(dev, link, ENABLED) {
  580. int devno = dev->devno;
  581. ehc->saved_xfer_mode[devno] = dev->xfer_mode;
  582. if (ata_ncq_enabled(dev))
  583. ehc->saved_ncq_enabled |= 1 << devno;
  584. }
  585. }
  586. ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
  587. ap->pflags &= ~ATA_PFLAG_EH_PENDING;
  588. ap->excl_link = NULL; /* don't maintain exclusion over EH */
  589. spin_unlock_irqrestore(ap->lock, flags);
  590. /* invoke EH, skip if unloading or suspended */
  591. if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
  592. ap->ops->error_handler(ap);
  593. else {
  594. /* if unloading, commence suicide */
  595. if ((ap->pflags & ATA_PFLAG_UNLOADING) &&
  596. !(ap->pflags & ATA_PFLAG_UNLOADED))
  597. ata_eh_unload(ap);
  598. ata_eh_finish(ap);
  599. }
  600. /* process port suspend request */
  601. ata_eh_handle_port_suspend(ap);
  602. /* Exception might have happened after ->error_handler
  603. * recovered the port but before this point. Repeat
  604. * EH in such case.
  605. */
  606. spin_lock_irqsave(ap->lock, flags);
  607. if (ap->pflags & ATA_PFLAG_EH_PENDING) {
  608. if (--ap->eh_tries) {
  609. spin_unlock_irqrestore(ap->lock, flags);
  610. goto repeat;
  611. }
  612. ata_port_err(ap,
  613. "EH pending after %d tries, giving up\n",
  614. ATA_EH_MAX_TRIES);
  615. ap->pflags &= ~ATA_PFLAG_EH_PENDING;
  616. }
  617. /* this run is complete, make sure EH info is clear */
  618. ata_for_each_link(link, ap, HOST_FIRST)
  619. memset(&link->eh_info, 0, sizeof(link->eh_info));
  620. /* end eh (clear host_eh_scheduled) while holding
  621. * ap->lock such that if exception occurs after this
  622. * point but before EH completion, SCSI midlayer will
  623. * re-initiate EH.
  624. */
  625. ap->ops->end_eh(ap);
  626. spin_unlock_irqrestore(ap->lock, flags);
  627. ata_eh_release(ap);
  628. } else {
  629. WARN_ON(ata_qc_from_tag(ap, ap->link.active_tag) == NULL);
  630. ap->ops->eng_timeout(ap);
  631. }
  632. scsi_eh_flush_done_q(&ap->eh_done_q);
  633. /* clean up */
  634. spin_lock_irqsave(ap->lock, flags);
  635. if (ap->pflags & ATA_PFLAG_LOADING)
  636. ap->pflags &= ~ATA_PFLAG_LOADING;
  637. else if ((ap->pflags & ATA_PFLAG_SCSI_HOTPLUG) &&
  638. !(ap->flags & ATA_FLAG_SAS_HOST))
  639. schedule_delayed_work(&ap->hotplug_task, 0);
  640. if (ap->pflags & ATA_PFLAG_RECOVERED)
  641. ata_port_info(ap, "EH complete\n");
  642. ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
  643. /* tell wait_eh that we're done */
  644. ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
  645. wake_up_all(&ap->eh_wait_q);
  646. spin_unlock_irqrestore(ap->lock, flags);
  647. }
  648. EXPORT_SYMBOL_GPL(ata_scsi_port_error_handler);
  649. /**
  650. * ata_port_wait_eh - Wait for the currently pending EH to complete
  651. * @ap: Port to wait EH for
  652. *
  653. * Wait until the currently pending EH is complete.
  654. *
  655. * LOCKING:
  656. * Kernel thread context (may sleep).
  657. */
  658. void ata_port_wait_eh(struct ata_port *ap)
  659. {
  660. unsigned long flags;
  661. DEFINE_WAIT(wait);
  662. retry:
  663. spin_lock_irqsave(ap->lock, flags);
  664. while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
  665. prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
  666. spin_unlock_irqrestore(ap->lock, flags);
  667. schedule();
  668. spin_lock_irqsave(ap->lock, flags);
  669. }
  670. finish_wait(&ap->eh_wait_q, &wait);
  671. spin_unlock_irqrestore(ap->lock, flags);
  672. /* make sure SCSI EH is complete */
  673. if (scsi_host_in_recovery(ap->scsi_host)) {
  674. ata_msleep(ap, 10);
  675. goto retry;
  676. }
  677. }
  678. EXPORT_SYMBOL_GPL(ata_port_wait_eh);
  679. static int ata_eh_nr_in_flight(struct ata_port *ap)
  680. {
  681. struct ata_queued_cmd *qc;
  682. unsigned int tag;
  683. int nr = 0;
  684. /* count only non-internal commands */
  685. ata_qc_for_each(ap, qc, tag) {
  686. if (qc)
  687. nr++;
  688. }
  689. return nr;
  690. }
  691. void ata_eh_fastdrain_timerfn(struct timer_list *t)
  692. {
  693. struct ata_port *ap = from_timer(ap, t, fastdrain_timer);
  694. unsigned long flags;
  695. int cnt;
  696. spin_lock_irqsave(ap->lock, flags);
  697. cnt = ata_eh_nr_in_flight(ap);
  698. /* are we done? */
  699. if (!cnt)
  700. goto out_unlock;
  701. if (cnt == ap->fastdrain_cnt) {
  702. struct ata_queued_cmd *qc;
  703. unsigned int tag;
  704. /* No progress during the last interval, tag all
  705. * in-flight qcs as timed out and freeze the port.
  706. */
  707. ata_qc_for_each(ap, qc, tag) {
  708. if (qc)
  709. qc->err_mask |= AC_ERR_TIMEOUT;
  710. }
  711. ata_port_freeze(ap);
  712. } else {
  713. /* some qcs have finished, give it another chance */
  714. ap->fastdrain_cnt = cnt;
  715. ap->fastdrain_timer.expires =
  716. ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL);
  717. add_timer(&ap->fastdrain_timer);
  718. }
  719. out_unlock:
  720. spin_unlock_irqrestore(ap->lock, flags);
  721. }
  722. /**
  723. * ata_eh_set_pending - set ATA_PFLAG_EH_PENDING and activate fast drain
  724. * @ap: target ATA port
  725. * @fastdrain: activate fast drain
  726. *
  727. * Set ATA_PFLAG_EH_PENDING and activate fast drain if @fastdrain
  728. * is non-zero and EH wasn't pending before. Fast drain ensures
  729. * that EH kicks in in timely manner.
  730. *
  731. * LOCKING:
  732. * spin_lock_irqsave(host lock)
  733. */
  734. static void ata_eh_set_pending(struct ata_port *ap, int fastdrain)
  735. {
  736. int cnt;
  737. /* already scheduled? */
  738. if (ap->pflags & ATA_PFLAG_EH_PENDING)
  739. return;
  740. ap->pflags |= ATA_PFLAG_EH_PENDING;
  741. if (!fastdrain)
  742. return;
  743. /* do we have in-flight qcs? */
  744. cnt = ata_eh_nr_in_flight(ap);
  745. if (!cnt)
  746. return;
  747. /* activate fast drain */
  748. ap->fastdrain_cnt = cnt;
  749. ap->fastdrain_timer.expires =
  750. ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL);
  751. add_timer(&ap->fastdrain_timer);
  752. }
  753. /**
  754. * ata_qc_schedule_eh - schedule qc for error handling
  755. * @qc: command to schedule error handling for
  756. *
  757. * Schedule error handling for @qc. EH will kick in as soon as
  758. * other commands are drained.
  759. *
  760. * LOCKING:
  761. * spin_lock_irqsave(host lock)
  762. */
  763. void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
  764. {
  765. struct ata_port *ap = qc->ap;
  766. WARN_ON(!ap->ops->error_handler);
  767. qc->flags |= ATA_QCFLAG_FAILED;
  768. ata_eh_set_pending(ap, 1);
  769. /* The following will fail if timeout has already expired.
  770. * ata_scsi_error() takes care of such scmds on EH entry.
  771. * Note that ATA_QCFLAG_FAILED is unconditionally set after
  772. * this function completes.
  773. */
  774. blk_abort_request(qc->scsicmd->request);
  775. }
  776. /**
  777. * ata_std_sched_eh - non-libsas ata_ports issue eh with this common routine
  778. * @ap: ATA port to schedule EH for
  779. *
  780. * LOCKING: inherited from ata_port_schedule_eh
  781. * spin_lock_irqsave(host lock)
  782. */
  783. void ata_std_sched_eh(struct ata_port *ap)
  784. {
  785. WARN_ON(!ap->ops->error_handler);
  786. if (ap->pflags & ATA_PFLAG_INITIALIZING)
  787. return;
  788. ata_eh_set_pending(ap, 1);
  789. scsi_schedule_eh(ap->scsi_host);
  790. DPRINTK("port EH scheduled\n");
  791. }
  792. EXPORT_SYMBOL_GPL(ata_std_sched_eh);
  793. /**
  794. * ata_std_end_eh - non-libsas ata_ports complete eh with this common routine
  795. * @ap: ATA port to end EH for
  796. *
  797. * In the libata object model there is a 1:1 mapping of ata_port to
  798. * shost, so host fields can be directly manipulated under ap->lock, in
  799. * the libsas case we need to hold a lock at the ha->level to coordinate
  800. * these events.
  801. *
  802. * LOCKING:
  803. * spin_lock_irqsave(host lock)
  804. */
  805. void ata_std_end_eh(struct ata_port *ap)
  806. {
  807. struct Scsi_Host *host = ap->scsi_host;
  808. host->host_eh_scheduled = 0;
  809. }
  810. EXPORT_SYMBOL(ata_std_end_eh);
  811. /**
  812. * ata_port_schedule_eh - schedule error handling without a qc
  813. * @ap: ATA port to schedule EH for
  814. *
  815. * Schedule error handling for @ap. EH will kick in as soon as
  816. * all commands are drained.
  817. *
  818. * LOCKING:
  819. * spin_lock_irqsave(host lock)
  820. */
  821. void ata_port_schedule_eh(struct ata_port *ap)
  822. {
  823. /* see: ata_std_sched_eh, unless you know better */
  824. ap->ops->sched_eh(ap);
  825. }
  826. EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
  827. static int ata_do_link_abort(struct ata_port *ap, struct ata_link *link)
  828. {
  829. struct ata_queued_cmd *qc;
  830. int tag, nr_aborted = 0;
  831. WARN_ON(!ap->ops->error_handler);
  832. /* we're gonna abort all commands, no need for fast drain */
  833. ata_eh_set_pending(ap, 0);
  834. /* include internal tag in iteration */
  835. ata_qc_for_each_with_internal(ap, qc, tag) {
  836. if (qc && (!link || qc->dev->link == link)) {
  837. qc->flags |= ATA_QCFLAG_FAILED;
  838. ata_qc_complete(qc);
  839. nr_aborted++;
  840. }
  841. }
  842. if (!nr_aborted)
  843. ata_port_schedule_eh(ap);
  844. return nr_aborted;
  845. }
  846. /**
  847. * ata_link_abort - abort all qc's on the link
  848. * @link: ATA link to abort qc's for
  849. *
  850. * Abort all active qc's active on @link and schedule EH.
  851. *
  852. * LOCKING:
  853. * spin_lock_irqsave(host lock)
  854. *
  855. * RETURNS:
  856. * Number of aborted qc's.
  857. */
  858. int ata_link_abort(struct ata_link *link)
  859. {
  860. return ata_do_link_abort(link->ap, link);
  861. }
  862. EXPORT_SYMBOL_GPL(ata_link_abort);
  863. /**
  864. * ata_port_abort - abort all qc's on the port
  865. * @ap: ATA port to abort qc's for
  866. *
  867. * Abort all active qc's of @ap and schedule EH.
  868. *
  869. * LOCKING:
  870. * spin_lock_irqsave(host_set lock)
  871. *
  872. * RETURNS:
  873. * Number of aborted qc's.
  874. */
  875. int ata_port_abort(struct ata_port *ap)
  876. {
  877. return ata_do_link_abort(ap, NULL);
  878. }
  879. EXPORT_SYMBOL_GPL(ata_port_abort);
  880. /**
  881. * __ata_port_freeze - freeze port
  882. * @ap: ATA port to freeze
  883. *
  884. * This function is called when HSM violation or some other
  885. * condition disrupts normal operation of the port. Frozen port
  886. * is not allowed to perform any operation until the port is
  887. * thawed, which usually follows a successful reset.
  888. *
  889. * ap->ops->freeze() callback can be used for freezing the port
  890. * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
  891. * port cannot be frozen hardware-wise, the interrupt handler
  892. * must ack and clear interrupts unconditionally while the port
  893. * is frozen.
  894. *
  895. * LOCKING:
  896. * spin_lock_irqsave(host lock)
  897. */
  898. static void __ata_port_freeze(struct ata_port *ap)
  899. {
  900. WARN_ON(!ap->ops->error_handler);
  901. if (ap->ops->freeze)
  902. ap->ops->freeze(ap);
  903. ap->pflags |= ATA_PFLAG_FROZEN;
  904. DPRINTK("ata%u port frozen\n", ap->print_id);
  905. }
  906. /**
  907. * ata_port_freeze - abort & freeze port
  908. * @ap: ATA port to freeze
  909. *
  910. * Abort and freeze @ap. The freeze operation must be called
  911. * first, because some hardware requires special operations
  912. * before the taskfile registers are accessible.
  913. *
  914. * LOCKING:
  915. * spin_lock_irqsave(host lock)
  916. *
  917. * RETURNS:
  918. * Number of aborted commands.
  919. */
  920. int ata_port_freeze(struct ata_port *ap)
  921. {
  922. int nr_aborted;
  923. WARN_ON(!ap->ops->error_handler);
  924. __ata_port_freeze(ap);
  925. nr_aborted = ata_port_abort(ap);
  926. return nr_aborted;
  927. }
  928. EXPORT_SYMBOL_GPL(ata_port_freeze);
  929. /**
  930. * ata_eh_freeze_port - EH helper to freeze port
  931. * @ap: ATA port to freeze
  932. *
  933. * Freeze @ap.
  934. *
  935. * LOCKING:
  936. * None.
  937. */
  938. void ata_eh_freeze_port(struct ata_port *ap)
  939. {
  940. unsigned long flags;
  941. if (!ap->ops->error_handler)
  942. return;
  943. spin_lock_irqsave(ap->lock, flags);
  944. __ata_port_freeze(ap);
  945. spin_unlock_irqrestore(ap->lock, flags);
  946. }
  947. EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
  948. /**
  949. * ata_port_thaw_port - EH helper to thaw port
  950. * @ap: ATA port to thaw
  951. *
  952. * Thaw frozen port @ap.
  953. *
  954. * LOCKING:
  955. * None.
  956. */
  957. void ata_eh_thaw_port(struct ata_port *ap)
  958. {
  959. unsigned long flags;
  960. if (!ap->ops->error_handler)
  961. return;
  962. spin_lock_irqsave(ap->lock, flags);
  963. ap->pflags &= ~ATA_PFLAG_FROZEN;
  964. if (ap->ops->thaw)
  965. ap->ops->thaw(ap);
  966. spin_unlock_irqrestore(ap->lock, flags);
  967. DPRINTK("ata%u port thawed\n", ap->print_id);
  968. }
  969. static void ata_eh_scsidone(struct scsi_cmnd *scmd)
  970. {
  971. /* nada */
  972. }
  973. static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
  974. {
  975. struct ata_port *ap = qc->ap;
  976. struct scsi_cmnd *scmd = qc->scsicmd;
  977. unsigned long flags;
  978. spin_lock_irqsave(ap->lock, flags);
  979. qc->scsidone = ata_eh_scsidone;
  980. __ata_qc_complete(qc);
  981. WARN_ON(ata_tag_valid(qc->tag));
  982. spin_unlock_irqrestore(ap->lock, flags);
  983. scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
  984. }
  985. /**
  986. * ata_eh_qc_complete - Complete an active ATA command from EH
  987. * @qc: Command to complete
  988. *
  989. * Indicate to the mid and upper layers that an ATA command has
  990. * completed. To be used from EH.
  991. */
  992. void ata_eh_qc_complete(struct ata_queued_cmd *qc)
  993. {
  994. struct scsi_cmnd *scmd = qc->scsicmd;
  995. scmd->retries = scmd->allowed;
  996. __ata_eh_qc_complete(qc);
  997. }
  998. /**
  999. * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
  1000. * @qc: Command to retry
  1001. *
  1002. * Indicate to the mid and upper layers that an ATA command
  1003. * should be retried. To be used from EH.
  1004. *
  1005. * SCSI midlayer limits the number of retries to scmd->allowed.
  1006. * scmd->allowed is incremented for commands which get retried
  1007. * due to unrelated failures (qc->err_mask is zero).
  1008. */
  1009. void ata_eh_qc_retry(struct ata_queued_cmd *qc)
  1010. {
  1011. struct scsi_cmnd *scmd = qc->scsicmd;
  1012. if (!qc->err_mask)
  1013. scmd->allowed++;
  1014. __ata_eh_qc_complete(qc);
  1015. }
  1016. /**
  1017. * ata_dev_disable - disable ATA device
  1018. * @dev: ATA device to disable
  1019. *
  1020. * Disable @dev.
  1021. *
  1022. * Locking:
  1023. * EH context.
  1024. */
  1025. void ata_dev_disable(struct ata_device *dev)
  1026. {
  1027. if (!ata_dev_enabled(dev))
  1028. return;
  1029. if (ata_msg_drv(dev->link->ap))
  1030. ata_dev_warn(dev, "disabled\n");
  1031. ata_acpi_on_disable(dev);
  1032. ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO0 | ATA_DNXFER_QUIET);
  1033. dev->class++;
  1034. /* From now till the next successful probe, ering is used to
  1035. * track probe failures. Clear accumulated device error info.
  1036. */
  1037. ata_ering_clear(&dev->ering);
  1038. }
  1039. EXPORT_SYMBOL_GPL(ata_dev_disable);
  1040. /**
  1041. * ata_eh_detach_dev - detach ATA device
  1042. * @dev: ATA device to detach
  1043. *
  1044. * Detach @dev.
  1045. *
  1046. * LOCKING:
  1047. * None.
  1048. */
  1049. void ata_eh_detach_dev(struct ata_device *dev)
  1050. {
  1051. struct ata_link *link = dev->link;
  1052. struct ata_port *ap = link->ap;
  1053. struct ata_eh_context *ehc = &link->eh_context;
  1054. unsigned long flags;
  1055. ata_dev_disable(dev);
  1056. spin_lock_irqsave(ap->lock, flags);
  1057. dev->flags &= ~ATA_DFLAG_DETACH;
  1058. if (ata_scsi_offline_dev(dev)) {
  1059. dev->flags |= ATA_DFLAG_DETACHED;
  1060. ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
  1061. }
  1062. /* clear per-dev EH info */
  1063. ata_eh_clear_action(link, dev, &link->eh_info, ATA_EH_PERDEV_MASK);
  1064. ata_eh_clear_action(link, dev, &link->eh_context.i, ATA_EH_PERDEV_MASK);
  1065. ehc->saved_xfer_mode[dev->devno] = 0;
  1066. ehc->saved_ncq_enabled &= ~(1 << dev->devno);
  1067. spin_unlock_irqrestore(ap->lock, flags);
  1068. }
  1069. /**
  1070. * ata_eh_about_to_do - about to perform eh_action
  1071. * @link: target ATA link
  1072. * @dev: target ATA dev for per-dev action (can be NULL)
  1073. * @action: action about to be performed
  1074. *
  1075. * Called just before performing EH actions to clear related bits
  1076. * in @link->eh_info such that eh actions are not unnecessarily
  1077. * repeated.
  1078. *
  1079. * LOCKING:
  1080. * None.
  1081. */
  1082. void ata_eh_about_to_do(struct ata_link *link, struct ata_device *dev,
  1083. unsigned int action)
  1084. {
  1085. struct ata_port *ap = link->ap;
  1086. struct ata_eh_info *ehi = &link->eh_info;
  1087. struct ata_eh_context *ehc = &link->eh_context;
  1088. unsigned long flags;
  1089. spin_lock_irqsave(ap->lock, flags);
  1090. ata_eh_clear_action(link, dev, ehi, action);
  1091. /* About to take EH action, set RECOVERED. Ignore actions on
  1092. * slave links as master will do them again.
  1093. */
  1094. if (!(ehc->i.flags & ATA_EHI_QUIET) && link != ap->slave_link)
  1095. ap->pflags |= ATA_PFLAG_RECOVERED;
  1096. spin_unlock_irqrestore(ap->lock, flags);
  1097. }
  1098. /**
  1099. * ata_eh_done - EH action complete
  1100. * @link: ATA link for which EH actions are complete
  1101. * @dev: target ATA dev for per-dev action (can be NULL)
  1102. * @action: action just completed
  1103. *
  1104. * Called right after performing EH actions to clear related bits
  1105. * in @link->eh_context.
  1106. *
  1107. * LOCKING:
  1108. * None.
  1109. */
  1110. void ata_eh_done(struct ata_link *link, struct ata_device *dev,
  1111. unsigned int action)
  1112. {
  1113. struct ata_eh_context *ehc = &link->eh_context;
  1114. ata_eh_clear_action(link, dev, &ehc->i, action);
  1115. }
  1116. /**
  1117. * ata_err_string - convert err_mask to descriptive string
  1118. * @err_mask: error mask to convert to string
  1119. *
  1120. * Convert @err_mask to descriptive string. Errors are
  1121. * prioritized according to severity and only the most severe
  1122. * error is reported.
  1123. *
  1124. * LOCKING:
  1125. * None.
  1126. *
  1127. * RETURNS:
  1128. * Descriptive string for @err_mask
  1129. */
  1130. static const char *ata_err_string(unsigned int err_mask)
  1131. {
  1132. if (err_mask & AC_ERR_HOST_BUS)
  1133. return "host bus error";
  1134. if (err_mask & AC_ERR_ATA_BUS)
  1135. return "ATA bus error";
  1136. if (err_mask & AC_ERR_TIMEOUT)
  1137. return "timeout";
  1138. if (err_mask & AC_ERR_HSM)
  1139. return "HSM violation";
  1140. if (err_mask & AC_ERR_SYSTEM)
  1141. return "internal error";
  1142. if (err_mask & AC_ERR_MEDIA)
  1143. return "media error";
  1144. if (err_mask & AC_ERR_INVALID)
  1145. return "invalid argument";
  1146. if (err_mask & AC_ERR_DEV)
  1147. return "device error";
  1148. if (err_mask & AC_ERR_NCQ)
  1149. return "NCQ error";
  1150. if (err_mask & AC_ERR_NODEV_HINT)
  1151. return "Polling detection error";
  1152. return "unknown error";
  1153. }
  1154. /**
  1155. * atapi_eh_tur - perform ATAPI TEST_UNIT_READY
  1156. * @dev: target ATAPI device
  1157. * @r_sense_key: out parameter for sense_key
  1158. *
  1159. * Perform ATAPI TEST_UNIT_READY.
  1160. *
  1161. * LOCKING:
  1162. * EH context (may sleep).
  1163. *
  1164. * RETURNS:
  1165. * 0 on success, AC_ERR_* mask on failure.
  1166. */
  1167. unsigned int atapi_eh_tur(struct ata_device *dev, u8 *r_sense_key)
  1168. {
  1169. u8 cdb[ATAPI_CDB_LEN] = { TEST_UNIT_READY, 0, 0, 0, 0, 0 };
  1170. struct ata_taskfile tf;
  1171. unsigned int err_mask;
  1172. ata_tf_init(dev, &tf);
  1173. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  1174. tf.command = ATA_CMD_PACKET;
  1175. tf.protocol = ATAPI_PROT_NODATA;
  1176. err_mask = ata_exec_internal(dev, &tf, cdb, DMA_NONE, NULL, 0, 0);
  1177. if (err_mask == AC_ERR_DEV)
  1178. *r_sense_key = tf.feature >> 4;
  1179. return err_mask;
  1180. }
  1181. /**
  1182. * ata_eh_request_sense - perform REQUEST_SENSE_DATA_EXT
  1183. * @qc: qc to perform REQUEST_SENSE_SENSE_DATA_EXT to
  1184. * @cmd: scsi command for which the sense code should be set
  1185. *
  1186. * Perform REQUEST_SENSE_DATA_EXT after the device reported CHECK
  1187. * SENSE. This function is an EH helper.
  1188. *
  1189. * LOCKING:
  1190. * Kernel thread context (may sleep).
  1191. */
  1192. static void ata_eh_request_sense(struct ata_queued_cmd *qc,
  1193. struct scsi_cmnd *cmd)
  1194. {
  1195. struct ata_device *dev = qc->dev;
  1196. struct ata_taskfile tf;
  1197. unsigned int err_mask;
  1198. if (qc->ap->pflags & ATA_PFLAG_FROZEN) {
  1199. ata_dev_warn(dev, "sense data available but port frozen\n");
  1200. return;
  1201. }
  1202. if (!cmd || qc->flags & ATA_QCFLAG_SENSE_VALID)
  1203. return;
  1204. if (!ata_id_sense_reporting_enabled(dev->id)) {
  1205. ata_dev_warn(qc->dev, "sense data reporting disabled\n");
  1206. return;
  1207. }
  1208. DPRINTK("ATA request sense\n");
  1209. ata_tf_init(dev, &tf);
  1210. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  1211. tf.flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
  1212. tf.command = ATA_CMD_REQ_SENSE_DATA;
  1213. tf.protocol = ATA_PROT_NODATA;
  1214. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
  1215. /* Ignore err_mask; ATA_ERR might be set */
  1216. if (tf.command & ATA_SENSE) {
  1217. ata_scsi_set_sense(dev, cmd, tf.lbah, tf.lbam, tf.lbal);
  1218. qc->flags |= ATA_QCFLAG_SENSE_VALID;
  1219. } else {
  1220. ata_dev_warn(dev, "request sense failed stat %02x emask %x\n",
  1221. tf.command, err_mask);
  1222. }
  1223. }
  1224. /**
  1225. * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
  1226. * @dev: device to perform REQUEST_SENSE to
  1227. * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
  1228. * @dfl_sense_key: default sense key to use
  1229. *
  1230. * Perform ATAPI REQUEST_SENSE after the device reported CHECK
  1231. * SENSE. This function is EH helper.
  1232. *
  1233. * LOCKING:
  1234. * Kernel thread context (may sleep).
  1235. *
  1236. * RETURNS:
  1237. * 0 on success, AC_ERR_* mask on failure
  1238. */
  1239. unsigned int atapi_eh_request_sense(struct ata_device *dev,
  1240. u8 *sense_buf, u8 dfl_sense_key)
  1241. {
  1242. u8 cdb[ATAPI_CDB_LEN] =
  1243. { REQUEST_SENSE, 0, 0, 0, SCSI_SENSE_BUFFERSIZE, 0 };
  1244. struct ata_port *ap = dev->link->ap;
  1245. struct ata_taskfile tf;
  1246. DPRINTK("ATAPI request sense\n");
  1247. memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
  1248. /* initialize sense_buf with the error register,
  1249. * for the case where they are -not- overwritten
  1250. */
  1251. sense_buf[0] = 0x70;
  1252. sense_buf[2] = dfl_sense_key;
  1253. /* some devices time out if garbage left in tf */
  1254. ata_tf_init(dev, &tf);
  1255. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  1256. tf.command = ATA_CMD_PACKET;
  1257. /* is it pointless to prefer PIO for "safety reasons"? */
  1258. if (ap->flags & ATA_FLAG_PIO_DMA) {
  1259. tf.protocol = ATAPI_PROT_DMA;
  1260. tf.feature |= ATAPI_PKT_DMA;
  1261. } else {
  1262. tf.protocol = ATAPI_PROT_PIO;
  1263. tf.lbam = SCSI_SENSE_BUFFERSIZE;
  1264. tf.lbah = 0;
  1265. }
  1266. return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
  1267. sense_buf, SCSI_SENSE_BUFFERSIZE, 0);
  1268. }
  1269. /**
  1270. * ata_eh_analyze_serror - analyze SError for a failed port
  1271. * @link: ATA link to analyze SError for
  1272. *
  1273. * Analyze SError if available and further determine cause of
  1274. * failure.
  1275. *
  1276. * LOCKING:
  1277. * None.
  1278. */
  1279. static void ata_eh_analyze_serror(struct ata_link *link)
  1280. {
  1281. struct ata_eh_context *ehc = &link->eh_context;
  1282. u32 serror = ehc->i.serror;
  1283. unsigned int err_mask = 0, action = 0;
  1284. u32 hotplug_mask;
  1285. if (serror & (SERR_PERSISTENT | SERR_DATA)) {
  1286. err_mask |= AC_ERR_ATA_BUS;
  1287. action |= ATA_EH_RESET;
  1288. }
  1289. if (serror & SERR_PROTOCOL) {
  1290. err_mask |= AC_ERR_HSM;
  1291. action |= ATA_EH_RESET;
  1292. }
  1293. if (serror & SERR_INTERNAL) {
  1294. err_mask |= AC_ERR_SYSTEM;
  1295. action |= ATA_EH_RESET;
  1296. }
  1297. /* Determine whether a hotplug event has occurred. Both
  1298. * SError.N/X are considered hotplug events for enabled or
  1299. * host links. For disabled PMP links, only N bit is
  1300. * considered as X bit is left at 1 for link plugging.
  1301. */
  1302. if (link->lpm_policy > ATA_LPM_MAX_POWER)
  1303. hotplug_mask = 0; /* hotplug doesn't work w/ LPM */
  1304. else if (!(link->flags & ATA_LFLAG_DISABLED) || ata_is_host_link(link))
  1305. hotplug_mask = SERR_PHYRDY_CHG | SERR_DEV_XCHG;
  1306. else
  1307. hotplug_mask = SERR_PHYRDY_CHG;
  1308. if (serror & hotplug_mask)
  1309. ata_ehi_hotplugged(&ehc->i);
  1310. ehc->i.err_mask |= err_mask;
  1311. ehc->i.action |= action;
  1312. }
  1313. /**
  1314. * ata_eh_analyze_tf - analyze taskfile of a failed qc
  1315. * @qc: qc to analyze
  1316. * @tf: Taskfile registers to analyze
  1317. *
  1318. * Analyze taskfile of @qc and further determine cause of
  1319. * failure. This function also requests ATAPI sense data if
  1320. * available.
  1321. *
  1322. * LOCKING:
  1323. * Kernel thread context (may sleep).
  1324. *
  1325. * RETURNS:
  1326. * Determined recovery action
  1327. */
  1328. static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
  1329. const struct ata_taskfile *tf)
  1330. {
  1331. unsigned int tmp, action = 0;
  1332. u8 stat = tf->command, err = tf->feature;
  1333. if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
  1334. qc->err_mask |= AC_ERR_HSM;
  1335. return ATA_EH_RESET;
  1336. }
  1337. if (stat & (ATA_ERR | ATA_DF)) {
  1338. qc->err_mask |= AC_ERR_DEV;
  1339. /*
  1340. * Sense data reporting does not work if the
  1341. * device fault bit is set.
  1342. */
  1343. if (stat & ATA_DF)
  1344. stat &= ~ATA_SENSE;
  1345. } else {
  1346. return 0;
  1347. }
  1348. switch (qc->dev->class) {
  1349. case ATA_DEV_ZAC:
  1350. if (stat & ATA_SENSE)
  1351. ata_eh_request_sense(qc, qc->scsicmd);
  1352. /* fall through */
  1353. case ATA_DEV_ATA:
  1354. if (err & ATA_ICRC)
  1355. qc->err_mask |= AC_ERR_ATA_BUS;
  1356. if (err & (ATA_UNC | ATA_AMNF))
  1357. qc->err_mask |= AC_ERR_MEDIA;
  1358. if (err & ATA_IDNF)
  1359. qc->err_mask |= AC_ERR_INVALID;
  1360. break;
  1361. case ATA_DEV_ATAPI:
  1362. if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
  1363. tmp = atapi_eh_request_sense(qc->dev,
  1364. qc->scsicmd->sense_buffer,
  1365. qc->result_tf.feature >> 4);
  1366. if (!tmp)
  1367. qc->flags |= ATA_QCFLAG_SENSE_VALID;
  1368. else
  1369. qc->err_mask |= tmp;
  1370. }
  1371. }
  1372. if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
  1373. int ret = scsi_check_sense(qc->scsicmd);
  1374. /*
  1375. * SUCCESS here means that the sense code could be
  1376. * evaluated and should be passed to the upper layers
  1377. * for correct evaluation.
  1378. * FAILED means the sense code could not be interpreted
  1379. * and the device would need to be reset.
  1380. * NEEDS_RETRY and ADD_TO_MLQUEUE means that the
  1381. * command would need to be retried.
  1382. */
  1383. if (ret == NEEDS_RETRY || ret == ADD_TO_MLQUEUE) {
  1384. qc->flags |= ATA_QCFLAG_RETRY;
  1385. qc->err_mask |= AC_ERR_OTHER;
  1386. } else if (ret != SUCCESS) {
  1387. qc->err_mask |= AC_ERR_HSM;
  1388. }
  1389. }
  1390. if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
  1391. action |= ATA_EH_RESET;
  1392. return action;
  1393. }
  1394. static int ata_eh_categorize_error(unsigned int eflags, unsigned int err_mask,
  1395. int *xfer_ok)
  1396. {
  1397. int base = 0;
  1398. if (!(eflags & ATA_EFLAG_DUBIOUS_XFER))
  1399. *xfer_ok = 1;
  1400. if (!*xfer_ok)
  1401. base = ATA_ECAT_DUBIOUS_NONE;
  1402. if (err_mask & AC_ERR_ATA_BUS)
  1403. return base + ATA_ECAT_ATA_BUS;
  1404. if (err_mask & AC_ERR_TIMEOUT)
  1405. return base + ATA_ECAT_TOUT_HSM;
  1406. if (eflags & ATA_EFLAG_IS_IO) {
  1407. if (err_mask & AC_ERR_HSM)
  1408. return base + ATA_ECAT_TOUT_HSM;
  1409. if ((err_mask &
  1410. (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
  1411. return base + ATA_ECAT_UNK_DEV;
  1412. }
  1413. return 0;
  1414. }
  1415. struct speed_down_verdict_arg {
  1416. u64 since;
  1417. int xfer_ok;
  1418. int nr_errors[ATA_ECAT_NR];
  1419. };
  1420. static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
  1421. {
  1422. struct speed_down_verdict_arg *arg = void_arg;
  1423. int cat;
  1424. if ((ent->eflags & ATA_EFLAG_OLD_ER) || (ent->timestamp < arg->since))
  1425. return -1;
  1426. cat = ata_eh_categorize_error(ent->eflags, ent->err_mask,
  1427. &arg->xfer_ok);
  1428. arg->nr_errors[cat]++;
  1429. return 0;
  1430. }
  1431. /**
  1432. * ata_eh_speed_down_verdict - Determine speed down verdict
  1433. * @dev: Device of interest
  1434. *
  1435. * This function examines error ring of @dev and determines
  1436. * whether NCQ needs to be turned off, transfer speed should be
  1437. * stepped down, or falling back to PIO is necessary.
  1438. *
  1439. * ECAT_ATA_BUS : ATA_BUS error for any command
  1440. *
  1441. * ECAT_TOUT_HSM : TIMEOUT for any command or HSM violation for
  1442. * IO commands
  1443. *
  1444. * ECAT_UNK_DEV : Unknown DEV error for IO commands
  1445. *
  1446. * ECAT_DUBIOUS_* : Identical to above three but occurred while
  1447. * data transfer hasn't been verified.
  1448. *
  1449. * Verdicts are
  1450. *
  1451. * NCQ_OFF : Turn off NCQ.
  1452. *
  1453. * SPEED_DOWN : Speed down transfer speed but don't fall back
  1454. * to PIO.
  1455. *
  1456. * FALLBACK_TO_PIO : Fall back to PIO.
  1457. *
  1458. * Even if multiple verdicts are returned, only one action is
  1459. * taken per error. An action triggered by non-DUBIOUS errors
  1460. * clears ering, while one triggered by DUBIOUS_* errors doesn't.
  1461. * This is to expedite speed down decisions right after device is
  1462. * initially configured.
  1463. *
  1464. * The following are speed down rules. #1 and #2 deal with
  1465. * DUBIOUS errors.
  1466. *
  1467. * 1. If more than one DUBIOUS_ATA_BUS or DUBIOUS_TOUT_HSM errors
  1468. * occurred during last 5 mins, SPEED_DOWN and FALLBACK_TO_PIO.
  1469. *
  1470. * 2. If more than one DUBIOUS_TOUT_HSM or DUBIOUS_UNK_DEV errors
  1471. * occurred during last 5 mins, NCQ_OFF.
  1472. *
  1473. * 3. If more than 8 ATA_BUS, TOUT_HSM or UNK_DEV errors
  1474. * occurred during last 5 mins, FALLBACK_TO_PIO
  1475. *
  1476. * 4. If more than 3 TOUT_HSM or UNK_DEV errors occurred
  1477. * during last 10 mins, NCQ_OFF.
  1478. *
  1479. * 5. If more than 3 ATA_BUS or TOUT_HSM errors, or more than 6
  1480. * UNK_DEV errors occurred during last 10 mins, SPEED_DOWN.
  1481. *
  1482. * LOCKING:
  1483. * Inherited from caller.
  1484. *
  1485. * RETURNS:
  1486. * OR of ATA_EH_SPDN_* flags.
  1487. */
  1488. static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
  1489. {
  1490. const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
  1491. u64 j64 = get_jiffies_64();
  1492. struct speed_down_verdict_arg arg;
  1493. unsigned int verdict = 0;
  1494. /* scan past 5 mins of error history */
  1495. memset(&arg, 0, sizeof(arg));
  1496. arg.since = j64 - min(j64, j5mins);
  1497. ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
  1498. if (arg.nr_errors[ATA_ECAT_DUBIOUS_ATA_BUS] +
  1499. arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] > 1)
  1500. verdict |= ATA_EH_SPDN_SPEED_DOWN |
  1501. ATA_EH_SPDN_FALLBACK_TO_PIO | ATA_EH_SPDN_KEEP_ERRORS;
  1502. if (arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] +
  1503. arg.nr_errors[ATA_ECAT_DUBIOUS_UNK_DEV] > 1)
  1504. verdict |= ATA_EH_SPDN_NCQ_OFF | ATA_EH_SPDN_KEEP_ERRORS;
  1505. if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
  1506. arg.nr_errors[ATA_ECAT_TOUT_HSM] +
  1507. arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
  1508. verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
  1509. /* scan past 10 mins of error history */
  1510. memset(&arg, 0, sizeof(arg));
  1511. arg.since = j64 - min(j64, j10mins);
  1512. ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
  1513. if (arg.nr_errors[ATA_ECAT_TOUT_HSM] +
  1514. arg.nr_errors[ATA_ECAT_UNK_DEV] > 3)
  1515. verdict |= ATA_EH_SPDN_NCQ_OFF;
  1516. if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
  1517. arg.nr_errors[ATA_ECAT_TOUT_HSM] > 3 ||
  1518. arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
  1519. verdict |= ATA_EH_SPDN_SPEED_DOWN;
  1520. return verdict;
  1521. }
  1522. /**
  1523. * ata_eh_speed_down - record error and speed down if necessary
  1524. * @dev: Failed device
  1525. * @eflags: mask of ATA_EFLAG_* flags
  1526. * @err_mask: err_mask of the error
  1527. *
  1528. * Record error and examine error history to determine whether
  1529. * adjusting transmission speed is necessary. It also sets
  1530. * transmission limits appropriately if such adjustment is
  1531. * necessary.
  1532. *
  1533. * LOCKING:
  1534. * Kernel thread context (may sleep).
  1535. *
  1536. * RETURNS:
  1537. * Determined recovery action.
  1538. */
  1539. static unsigned int ata_eh_speed_down(struct ata_device *dev,
  1540. unsigned int eflags, unsigned int err_mask)
  1541. {
  1542. struct ata_link *link = ata_dev_phys_link(dev);
  1543. int xfer_ok = 0;
  1544. unsigned int verdict;
  1545. unsigned int action = 0;
  1546. /* don't bother if Cat-0 error */
  1547. if (ata_eh_categorize_error(eflags, err_mask, &xfer_ok) == 0)
  1548. return 0;
  1549. /* record error and determine whether speed down is necessary */
  1550. ata_ering_record(&dev->ering, eflags, err_mask);
  1551. verdict = ata_eh_speed_down_verdict(dev);
  1552. /* turn off NCQ? */
  1553. if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
  1554. (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
  1555. ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
  1556. dev->flags |= ATA_DFLAG_NCQ_OFF;
  1557. ata_dev_warn(dev, "NCQ disabled due to excessive errors\n");
  1558. goto done;
  1559. }
  1560. /* speed down? */
  1561. if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
  1562. /* speed down SATA link speed if possible */
  1563. if (sata_down_spd_limit(link, 0) == 0) {
  1564. action |= ATA_EH_RESET;
  1565. goto done;
  1566. }
  1567. /* lower transfer mode */
  1568. if (dev->spdn_cnt < 2) {
  1569. static const int dma_dnxfer_sel[] =
  1570. { ATA_DNXFER_DMA, ATA_DNXFER_40C };
  1571. static const int pio_dnxfer_sel[] =
  1572. { ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
  1573. int sel;
  1574. if (dev->xfer_shift != ATA_SHIFT_PIO)
  1575. sel = dma_dnxfer_sel[dev->spdn_cnt];
  1576. else
  1577. sel = pio_dnxfer_sel[dev->spdn_cnt];
  1578. dev->spdn_cnt++;
  1579. if (ata_down_xfermask_limit(dev, sel) == 0) {
  1580. action |= ATA_EH_RESET;
  1581. goto done;
  1582. }
  1583. }
  1584. }
  1585. /* Fall back to PIO? Slowing down to PIO is meaningless for
  1586. * SATA ATA devices. Consider it only for PATA and SATAPI.
  1587. */
  1588. if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
  1589. (link->ap->cbl != ATA_CBL_SATA || dev->class == ATA_DEV_ATAPI) &&
  1590. (dev->xfer_shift != ATA_SHIFT_PIO)) {
  1591. if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
  1592. dev->spdn_cnt = 0;
  1593. action |= ATA_EH_RESET;
  1594. goto done;
  1595. }
  1596. }
  1597. return 0;
  1598. done:
  1599. /* device has been slowed down, blow error history */
  1600. if (!(verdict & ATA_EH_SPDN_KEEP_ERRORS))
  1601. ata_ering_clear(&dev->ering);
  1602. return action;
  1603. }
  1604. /**
  1605. * ata_eh_worth_retry - analyze error and decide whether to retry
  1606. * @qc: qc to possibly retry
  1607. *
  1608. * Look at the cause of the error and decide if a retry
  1609. * might be useful or not. We don't want to retry media errors
  1610. * because the drive itself has probably already taken 10-30 seconds
  1611. * doing its own internal retries before reporting the failure.
  1612. */
  1613. static inline int ata_eh_worth_retry(struct ata_queued_cmd *qc)
  1614. {
  1615. if (qc->err_mask & AC_ERR_MEDIA)
  1616. return 0; /* don't retry media errors */
  1617. if (qc->flags & ATA_QCFLAG_IO)
  1618. return 1; /* otherwise retry anything from fs stack */
  1619. if (qc->err_mask & AC_ERR_INVALID)
  1620. return 0; /* don't retry these */
  1621. return qc->err_mask != AC_ERR_DEV; /* retry if not dev error */
  1622. }
  1623. /**
  1624. * ata_eh_quiet - check if we need to be quiet about a command error
  1625. * @qc: qc to check
  1626. *
  1627. * Look at the qc flags anbd its scsi command request flags to determine
  1628. * if we need to be quiet about the command failure.
  1629. */
  1630. static inline bool ata_eh_quiet(struct ata_queued_cmd *qc)
  1631. {
  1632. if (qc->scsicmd &&
  1633. qc->scsicmd->request->rq_flags & RQF_QUIET)
  1634. qc->flags |= ATA_QCFLAG_QUIET;
  1635. return qc->flags & ATA_QCFLAG_QUIET;
  1636. }
  1637. /**
  1638. * ata_eh_link_autopsy - analyze error and determine recovery action
  1639. * @link: host link to perform autopsy on
  1640. *
  1641. * Analyze why @link failed and determine which recovery actions
  1642. * are needed. This function also sets more detailed AC_ERR_*
  1643. * values and fills sense data for ATAPI CHECK SENSE.
  1644. *
  1645. * LOCKING:
  1646. * Kernel thread context (may sleep).
  1647. */
  1648. static void ata_eh_link_autopsy(struct ata_link *link)
  1649. {
  1650. struct ata_port *ap = link->ap;
  1651. struct ata_eh_context *ehc = &link->eh_context;
  1652. struct ata_queued_cmd *qc;
  1653. struct ata_device *dev;
  1654. unsigned int all_err_mask = 0, eflags = 0;
  1655. int tag, nr_failed = 0, nr_quiet = 0;
  1656. u32 serror;
  1657. int rc;
  1658. DPRINTK("ENTER\n");
  1659. if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
  1660. return;
  1661. /* obtain and analyze SError */
  1662. rc = sata_scr_read(link, SCR_ERROR, &serror);
  1663. if (rc == 0) {
  1664. ehc->i.serror |= serror;
  1665. ata_eh_analyze_serror(link);
  1666. } else if (rc != -EOPNOTSUPP) {
  1667. /* SError read failed, force reset and probing */
  1668. ehc->i.probe_mask |= ATA_ALL_DEVICES;
  1669. ehc->i.action |= ATA_EH_RESET;
  1670. ehc->i.err_mask |= AC_ERR_OTHER;
  1671. }
  1672. /* analyze NCQ failure */
  1673. ata_eh_analyze_ncq_error(link);
  1674. /* any real error trumps AC_ERR_OTHER */
  1675. if (ehc->i.err_mask & ~AC_ERR_OTHER)
  1676. ehc->i.err_mask &= ~AC_ERR_OTHER;
  1677. all_err_mask |= ehc->i.err_mask;
  1678. ata_qc_for_each_raw(ap, qc, tag) {
  1679. if (!(qc->flags & ATA_QCFLAG_FAILED) ||
  1680. ata_dev_phys_link(qc->dev) != link)
  1681. continue;
  1682. /* inherit upper level err_mask */
  1683. qc->err_mask |= ehc->i.err_mask;
  1684. /* analyze TF */
  1685. ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
  1686. /* DEV errors are probably spurious in case of ATA_BUS error */
  1687. if (qc->err_mask & AC_ERR_ATA_BUS)
  1688. qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
  1689. AC_ERR_INVALID);
  1690. /* any real error trumps unknown error */
  1691. if (qc->err_mask & ~AC_ERR_OTHER)
  1692. qc->err_mask &= ~AC_ERR_OTHER;
  1693. /*
  1694. * SENSE_VALID trumps dev/unknown error and revalidation. Upper
  1695. * layers will determine whether the command is worth retrying
  1696. * based on the sense data and device class/type. Otherwise,
  1697. * determine directly if the command is worth retrying using its
  1698. * error mask and flags.
  1699. */
  1700. if (qc->flags & ATA_QCFLAG_SENSE_VALID)
  1701. qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
  1702. else if (ata_eh_worth_retry(qc))
  1703. qc->flags |= ATA_QCFLAG_RETRY;
  1704. /* accumulate error info */
  1705. ehc->i.dev = qc->dev;
  1706. all_err_mask |= qc->err_mask;
  1707. if (qc->flags & ATA_QCFLAG_IO)
  1708. eflags |= ATA_EFLAG_IS_IO;
  1709. trace_ata_eh_link_autopsy_qc(qc);
  1710. /* Count quiet errors */
  1711. if (ata_eh_quiet(qc))
  1712. nr_quiet++;
  1713. nr_failed++;
  1714. }
  1715. /* If all failed commands requested silence, then be quiet */
  1716. if (nr_quiet == nr_failed)
  1717. ehc->i.flags |= ATA_EHI_QUIET;
  1718. /* enforce default EH actions */
  1719. if (ap->pflags & ATA_PFLAG_FROZEN ||
  1720. all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
  1721. ehc->i.action |= ATA_EH_RESET;
  1722. else if (((eflags & ATA_EFLAG_IS_IO) && all_err_mask) ||
  1723. (!(eflags & ATA_EFLAG_IS_IO) && (all_err_mask & ~AC_ERR_DEV)))
  1724. ehc->i.action |= ATA_EH_REVALIDATE;
  1725. /* If we have offending qcs and the associated failed device,
  1726. * perform per-dev EH action only on the offending device.
  1727. */
  1728. if (ehc->i.dev) {
  1729. ehc->i.dev_action[ehc->i.dev->devno] |=
  1730. ehc->i.action & ATA_EH_PERDEV_MASK;
  1731. ehc->i.action &= ~ATA_EH_PERDEV_MASK;
  1732. }
  1733. /* propagate timeout to host link */
  1734. if ((all_err_mask & AC_ERR_TIMEOUT) && !ata_is_host_link(link))
  1735. ap->link.eh_context.i.err_mask |= AC_ERR_TIMEOUT;
  1736. /* record error and consider speeding down */
  1737. dev = ehc->i.dev;
  1738. if (!dev && ((ata_link_max_devices(link) == 1 &&
  1739. ata_dev_enabled(link->device))))
  1740. dev = link->device;
  1741. if (dev) {
  1742. if (dev->flags & ATA_DFLAG_DUBIOUS_XFER)
  1743. eflags |= ATA_EFLAG_DUBIOUS_XFER;
  1744. ehc->i.action |= ata_eh_speed_down(dev, eflags, all_err_mask);
  1745. trace_ata_eh_link_autopsy(dev, ehc->i.action, all_err_mask);
  1746. }
  1747. DPRINTK("EXIT\n");
  1748. }
  1749. /**
  1750. * ata_eh_autopsy - analyze error and determine recovery action
  1751. * @ap: host port to perform autopsy on
  1752. *
  1753. * Analyze all links of @ap and determine why they failed and
  1754. * which recovery actions are needed.
  1755. *
  1756. * LOCKING:
  1757. * Kernel thread context (may sleep).
  1758. */
  1759. void ata_eh_autopsy(struct ata_port *ap)
  1760. {
  1761. struct ata_link *link;
  1762. ata_for_each_link(link, ap, EDGE)
  1763. ata_eh_link_autopsy(link);
  1764. /* Handle the frigging slave link. Autopsy is done similarly
  1765. * but actions and flags are transferred over to the master
  1766. * link and handled from there.
  1767. */
  1768. if (ap->slave_link) {
  1769. struct ata_eh_context *mehc = &ap->link.eh_context;
  1770. struct ata_eh_context *sehc = &ap->slave_link->eh_context;
  1771. /* transfer control flags from master to slave */
  1772. sehc->i.flags |= mehc->i.flags & ATA_EHI_TO_SLAVE_MASK;
  1773. /* perform autopsy on the slave link */
  1774. ata_eh_link_autopsy(ap->slave_link);
  1775. /* transfer actions from slave to master and clear slave */
  1776. ata_eh_about_to_do(ap->slave_link, NULL, ATA_EH_ALL_ACTIONS);
  1777. mehc->i.action |= sehc->i.action;
  1778. mehc->i.dev_action[1] |= sehc->i.dev_action[1];
  1779. mehc->i.flags |= sehc->i.flags;
  1780. ata_eh_done(ap->slave_link, NULL, ATA_EH_ALL_ACTIONS);
  1781. }
  1782. /* Autopsy of fanout ports can affect host link autopsy.
  1783. * Perform host link autopsy last.
  1784. */
  1785. if (sata_pmp_attached(ap))
  1786. ata_eh_link_autopsy(&ap->link);
  1787. }
  1788. /**
  1789. * ata_get_cmd_descript - get description for ATA command
  1790. * @command: ATA command code to get description for
  1791. *
  1792. * Return a textual description of the given command, or NULL if the
  1793. * command is not known.
  1794. *
  1795. * LOCKING:
  1796. * None
  1797. */
  1798. const char *ata_get_cmd_descript(u8 command)
  1799. {
  1800. #ifdef CONFIG_ATA_VERBOSE_ERROR
  1801. static const struct
  1802. {
  1803. u8 command;
  1804. const char *text;
  1805. } cmd_descr[] = {
  1806. { ATA_CMD_DEV_RESET, "DEVICE RESET" },
  1807. { ATA_CMD_CHK_POWER, "CHECK POWER MODE" },
  1808. { ATA_CMD_STANDBY, "STANDBY" },
  1809. { ATA_CMD_IDLE, "IDLE" },
  1810. { ATA_CMD_EDD, "EXECUTE DEVICE DIAGNOSTIC" },
  1811. { ATA_CMD_DOWNLOAD_MICRO, "DOWNLOAD MICROCODE" },
  1812. { ATA_CMD_DOWNLOAD_MICRO_DMA, "DOWNLOAD MICROCODE DMA" },
  1813. { ATA_CMD_NOP, "NOP" },
  1814. { ATA_CMD_FLUSH, "FLUSH CACHE" },
  1815. { ATA_CMD_FLUSH_EXT, "FLUSH CACHE EXT" },
  1816. { ATA_CMD_ID_ATA, "IDENTIFY DEVICE" },
  1817. { ATA_CMD_ID_ATAPI, "IDENTIFY PACKET DEVICE" },
  1818. { ATA_CMD_SERVICE, "SERVICE" },
  1819. { ATA_CMD_READ, "READ DMA" },
  1820. { ATA_CMD_READ_EXT, "READ DMA EXT" },
  1821. { ATA_CMD_READ_QUEUED, "READ DMA QUEUED" },
  1822. { ATA_CMD_READ_STREAM_EXT, "READ STREAM EXT" },
  1823. { ATA_CMD_READ_STREAM_DMA_EXT, "READ STREAM DMA EXT" },
  1824. { ATA_CMD_WRITE, "WRITE DMA" },
  1825. { ATA_CMD_WRITE_EXT, "WRITE DMA EXT" },
  1826. { ATA_CMD_WRITE_QUEUED, "WRITE DMA QUEUED EXT" },
  1827. { ATA_CMD_WRITE_STREAM_EXT, "WRITE STREAM EXT" },
  1828. { ATA_CMD_WRITE_STREAM_DMA_EXT, "WRITE STREAM DMA EXT" },
  1829. { ATA_CMD_WRITE_FUA_EXT, "WRITE DMA FUA EXT" },
  1830. { ATA_CMD_WRITE_QUEUED_FUA_EXT, "WRITE DMA QUEUED FUA EXT" },
  1831. { ATA_CMD_FPDMA_READ, "READ FPDMA QUEUED" },
  1832. { ATA_CMD_FPDMA_WRITE, "WRITE FPDMA QUEUED" },
  1833. { ATA_CMD_FPDMA_SEND, "SEND FPDMA QUEUED" },
  1834. { ATA_CMD_FPDMA_RECV, "RECEIVE FPDMA QUEUED" },
  1835. { ATA_CMD_PIO_READ, "READ SECTOR(S)" },
  1836. { ATA_CMD_PIO_READ_EXT, "READ SECTOR(S) EXT" },
  1837. { ATA_CMD_PIO_WRITE, "WRITE SECTOR(S)" },
  1838. { ATA_CMD_PIO_WRITE_EXT, "WRITE SECTOR(S) EXT" },
  1839. { ATA_CMD_READ_MULTI, "READ MULTIPLE" },
  1840. { ATA_CMD_READ_MULTI_EXT, "READ MULTIPLE EXT" },
  1841. { ATA_CMD_WRITE_MULTI, "WRITE MULTIPLE" },
  1842. { ATA_CMD_WRITE_MULTI_EXT, "WRITE MULTIPLE EXT" },
  1843. { ATA_CMD_WRITE_MULTI_FUA_EXT, "WRITE MULTIPLE FUA EXT" },
  1844. { ATA_CMD_SET_FEATURES, "SET FEATURES" },
  1845. { ATA_CMD_SET_MULTI, "SET MULTIPLE MODE" },
  1846. { ATA_CMD_VERIFY, "READ VERIFY SECTOR(S)" },
  1847. { ATA_CMD_VERIFY_EXT, "READ VERIFY SECTOR(S) EXT" },
  1848. { ATA_CMD_WRITE_UNCORR_EXT, "WRITE UNCORRECTABLE EXT" },
  1849. { ATA_CMD_STANDBYNOW1, "STANDBY IMMEDIATE" },
  1850. { ATA_CMD_IDLEIMMEDIATE, "IDLE IMMEDIATE" },
  1851. { ATA_CMD_SLEEP, "SLEEP" },
  1852. { ATA_CMD_INIT_DEV_PARAMS, "INITIALIZE DEVICE PARAMETERS" },
  1853. { ATA_CMD_READ_NATIVE_MAX, "READ NATIVE MAX ADDRESS" },
  1854. { ATA_CMD_READ_NATIVE_MAX_EXT, "READ NATIVE MAX ADDRESS EXT" },
  1855. { ATA_CMD_SET_MAX, "SET MAX ADDRESS" },
  1856. { ATA_CMD_SET_MAX_EXT, "SET MAX ADDRESS EXT" },
  1857. { ATA_CMD_READ_LOG_EXT, "READ LOG EXT" },
  1858. { ATA_CMD_WRITE_LOG_EXT, "WRITE LOG EXT" },
  1859. { ATA_CMD_READ_LOG_DMA_EXT, "READ LOG DMA EXT" },
  1860. { ATA_CMD_WRITE_LOG_DMA_EXT, "WRITE LOG DMA EXT" },
  1861. { ATA_CMD_TRUSTED_NONDATA, "TRUSTED NON-DATA" },
  1862. { ATA_CMD_TRUSTED_RCV, "TRUSTED RECEIVE" },
  1863. { ATA_CMD_TRUSTED_RCV_DMA, "TRUSTED RECEIVE DMA" },
  1864. { ATA_CMD_TRUSTED_SND, "TRUSTED SEND" },
  1865. { ATA_CMD_TRUSTED_SND_DMA, "TRUSTED SEND DMA" },
  1866. { ATA_CMD_PMP_READ, "READ BUFFER" },
  1867. { ATA_CMD_PMP_READ_DMA, "READ BUFFER DMA" },
  1868. { ATA_CMD_PMP_WRITE, "WRITE BUFFER" },
  1869. { ATA_CMD_PMP_WRITE_DMA, "WRITE BUFFER DMA" },
  1870. { ATA_CMD_CONF_OVERLAY, "DEVICE CONFIGURATION OVERLAY" },
  1871. { ATA_CMD_SEC_SET_PASS, "SECURITY SET PASSWORD" },
  1872. { ATA_CMD_SEC_UNLOCK, "SECURITY UNLOCK" },
  1873. { ATA_CMD_SEC_ERASE_PREP, "SECURITY ERASE PREPARE" },
  1874. { ATA_CMD_SEC_ERASE_UNIT, "SECURITY ERASE UNIT" },
  1875. { ATA_CMD_SEC_FREEZE_LOCK, "SECURITY FREEZE LOCK" },
  1876. { ATA_CMD_SEC_DISABLE_PASS, "SECURITY DISABLE PASSWORD" },
  1877. { ATA_CMD_CONFIG_STREAM, "CONFIGURE STREAM" },
  1878. { ATA_CMD_SMART, "SMART" },
  1879. { ATA_CMD_MEDIA_LOCK, "DOOR LOCK" },
  1880. { ATA_CMD_MEDIA_UNLOCK, "DOOR UNLOCK" },
  1881. { ATA_CMD_DSM, "DATA SET MANAGEMENT" },
  1882. { ATA_CMD_CHK_MED_CRD_TYP, "CHECK MEDIA CARD TYPE" },
  1883. { ATA_CMD_CFA_REQ_EXT_ERR, "CFA REQUEST EXTENDED ERROR" },
  1884. { ATA_CMD_CFA_WRITE_NE, "CFA WRITE SECTORS WITHOUT ERASE" },
  1885. { ATA_CMD_CFA_TRANS_SECT, "CFA TRANSLATE SECTOR" },
  1886. { ATA_CMD_CFA_ERASE, "CFA ERASE SECTORS" },
  1887. { ATA_CMD_CFA_WRITE_MULT_NE, "CFA WRITE MULTIPLE WITHOUT ERASE" },
  1888. { ATA_CMD_REQ_SENSE_DATA, "REQUEST SENSE DATA EXT" },
  1889. { ATA_CMD_SANITIZE_DEVICE, "SANITIZE DEVICE" },
  1890. { ATA_CMD_ZAC_MGMT_IN, "ZAC MANAGEMENT IN" },
  1891. { ATA_CMD_ZAC_MGMT_OUT, "ZAC MANAGEMENT OUT" },
  1892. { ATA_CMD_READ_LONG, "READ LONG (with retries)" },
  1893. { ATA_CMD_READ_LONG_ONCE, "READ LONG (without retries)" },
  1894. { ATA_CMD_WRITE_LONG, "WRITE LONG (with retries)" },
  1895. { ATA_CMD_WRITE_LONG_ONCE, "WRITE LONG (without retries)" },
  1896. { ATA_CMD_RESTORE, "RECALIBRATE" },
  1897. { 0, NULL } /* terminate list */
  1898. };
  1899. unsigned int i;
  1900. for (i = 0; cmd_descr[i].text; i++)
  1901. if (cmd_descr[i].command == command)
  1902. return cmd_descr[i].text;
  1903. #endif
  1904. return NULL;
  1905. }
  1906. EXPORT_SYMBOL_GPL(ata_get_cmd_descript);
  1907. /**
  1908. * ata_eh_link_report - report error handling to user
  1909. * @link: ATA link EH is going on
  1910. *
  1911. * Report EH to user.
  1912. *
  1913. * LOCKING:
  1914. * None.
  1915. */
  1916. static void ata_eh_link_report(struct ata_link *link)
  1917. {
  1918. struct ata_port *ap = link->ap;
  1919. struct ata_eh_context *ehc = &link->eh_context;
  1920. struct ata_queued_cmd *qc;
  1921. const char *frozen, *desc;
  1922. char tries_buf[6] = "";
  1923. int tag, nr_failed = 0;
  1924. if (ehc->i.flags & ATA_EHI_QUIET)
  1925. return;
  1926. desc = NULL;
  1927. if (ehc->i.desc[0] != '\0')
  1928. desc = ehc->i.desc;
  1929. ata_qc_for_each_raw(ap, qc, tag) {
  1930. if (!(qc->flags & ATA_QCFLAG_FAILED) ||
  1931. ata_dev_phys_link(qc->dev) != link ||
  1932. ((qc->flags & ATA_QCFLAG_QUIET) &&
  1933. qc->err_mask == AC_ERR_DEV))
  1934. continue;
  1935. if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
  1936. continue;
  1937. nr_failed++;
  1938. }
  1939. if (!nr_failed && !ehc->i.err_mask)
  1940. return;
  1941. frozen = "";
  1942. if (ap->pflags & ATA_PFLAG_FROZEN)
  1943. frozen = " frozen";
  1944. if (ap->eh_tries < ATA_EH_MAX_TRIES)
  1945. snprintf(tries_buf, sizeof(tries_buf), " t%d",
  1946. ap->eh_tries);
  1947. if (ehc->i.dev) {
  1948. ata_dev_err(ehc->i.dev, "exception Emask 0x%x "
  1949. "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
  1950. ehc->i.err_mask, link->sactive, ehc->i.serror,
  1951. ehc->i.action, frozen, tries_buf);
  1952. if (desc)
  1953. ata_dev_err(ehc->i.dev, "%s\n", desc);
  1954. } else {
  1955. ata_link_err(link, "exception Emask 0x%x "
  1956. "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
  1957. ehc->i.err_mask, link->sactive, ehc->i.serror,
  1958. ehc->i.action, frozen, tries_buf);
  1959. if (desc)
  1960. ata_link_err(link, "%s\n", desc);
  1961. }
  1962. #ifdef CONFIG_ATA_VERBOSE_ERROR
  1963. if (ehc->i.serror)
  1964. ata_link_err(link,
  1965. "SError: { %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s}\n",
  1966. ehc->i.serror & SERR_DATA_RECOVERED ? "RecovData " : "",
  1967. ehc->i.serror & SERR_COMM_RECOVERED ? "RecovComm " : "",
  1968. ehc->i.serror & SERR_DATA ? "UnrecovData " : "",
  1969. ehc->i.serror & SERR_PERSISTENT ? "Persist " : "",
  1970. ehc->i.serror & SERR_PROTOCOL ? "Proto " : "",
  1971. ehc->i.serror & SERR_INTERNAL ? "HostInt " : "",
  1972. ehc->i.serror & SERR_PHYRDY_CHG ? "PHYRdyChg " : "",
  1973. ehc->i.serror & SERR_PHY_INT_ERR ? "PHYInt " : "",
  1974. ehc->i.serror & SERR_COMM_WAKE ? "CommWake " : "",
  1975. ehc->i.serror & SERR_10B_8B_ERR ? "10B8B " : "",
  1976. ehc->i.serror & SERR_DISPARITY ? "Dispar " : "",
  1977. ehc->i.serror & SERR_CRC ? "BadCRC " : "",
  1978. ehc->i.serror & SERR_HANDSHAKE ? "Handshk " : "",
  1979. ehc->i.serror & SERR_LINK_SEQ_ERR ? "LinkSeq " : "",
  1980. ehc->i.serror & SERR_TRANS_ST_ERROR ? "TrStaTrns " : "",
  1981. ehc->i.serror & SERR_UNRECOG_FIS ? "UnrecFIS " : "",
  1982. ehc->i.serror & SERR_DEV_XCHG ? "DevExch " : "");
  1983. #endif
  1984. ata_qc_for_each_raw(ap, qc, tag) {
  1985. struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
  1986. char data_buf[20] = "";
  1987. char cdb_buf[70] = "";
  1988. if (!(qc->flags & ATA_QCFLAG_FAILED) ||
  1989. ata_dev_phys_link(qc->dev) != link || !qc->err_mask)
  1990. continue;
  1991. if (qc->dma_dir != DMA_NONE) {
  1992. static const char *dma_str[] = {
  1993. [DMA_BIDIRECTIONAL] = "bidi",
  1994. [DMA_TO_DEVICE] = "out",
  1995. [DMA_FROM_DEVICE] = "in",
  1996. };
  1997. const char *prot_str = NULL;
  1998. switch (qc->tf.protocol) {
  1999. case ATA_PROT_UNKNOWN:
  2000. prot_str = "unknown";
  2001. break;
  2002. case ATA_PROT_NODATA:
  2003. prot_str = "nodata";
  2004. break;
  2005. case ATA_PROT_PIO:
  2006. prot_str = "pio";
  2007. break;
  2008. case ATA_PROT_DMA:
  2009. prot_str = "dma";
  2010. break;
  2011. case ATA_PROT_NCQ:
  2012. prot_str = "ncq dma";
  2013. break;
  2014. case ATA_PROT_NCQ_NODATA:
  2015. prot_str = "ncq nodata";
  2016. break;
  2017. case ATAPI_PROT_NODATA:
  2018. prot_str = "nodata";
  2019. break;
  2020. case ATAPI_PROT_PIO:
  2021. prot_str = "pio";
  2022. break;
  2023. case ATAPI_PROT_DMA:
  2024. prot_str = "dma";
  2025. break;
  2026. }
  2027. snprintf(data_buf, sizeof(data_buf), " %s %u %s",
  2028. prot_str, qc->nbytes, dma_str[qc->dma_dir]);
  2029. }
  2030. if (ata_is_atapi(qc->tf.protocol)) {
  2031. const u8 *cdb = qc->cdb;
  2032. size_t cdb_len = qc->dev->cdb_len;
  2033. if (qc->scsicmd) {
  2034. cdb = qc->scsicmd->cmnd;
  2035. cdb_len = qc->scsicmd->cmd_len;
  2036. }
  2037. __scsi_format_command(cdb_buf, sizeof(cdb_buf),
  2038. cdb, cdb_len);
  2039. } else {
  2040. const char *descr = ata_get_cmd_descript(cmd->command);
  2041. if (descr)
  2042. ata_dev_err(qc->dev, "failed command: %s\n",
  2043. descr);
  2044. }
  2045. ata_dev_err(qc->dev,
  2046. "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
  2047. "tag %d%s\n %s"
  2048. "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
  2049. "Emask 0x%x (%s)%s\n",
  2050. cmd->command, cmd->feature, cmd->nsect,
  2051. cmd->lbal, cmd->lbam, cmd->lbah,
  2052. cmd->hob_feature, cmd->hob_nsect,
  2053. cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
  2054. cmd->device, qc->tag, data_buf, cdb_buf,
  2055. res->command, res->feature, res->nsect,
  2056. res->lbal, res->lbam, res->lbah,
  2057. res->hob_feature, res->hob_nsect,
  2058. res->hob_lbal, res->hob_lbam, res->hob_lbah,
  2059. res->device, qc->err_mask, ata_err_string(qc->err_mask),
  2060. qc->err_mask & AC_ERR_NCQ ? " <F>" : "");
  2061. #ifdef CONFIG_ATA_VERBOSE_ERROR
  2062. if (res->command & (ATA_BUSY | ATA_DRDY | ATA_DF | ATA_DRQ |
  2063. ATA_SENSE | ATA_ERR)) {
  2064. if (res->command & ATA_BUSY)
  2065. ata_dev_err(qc->dev, "status: { Busy }\n");
  2066. else
  2067. ata_dev_err(qc->dev, "status: { %s%s%s%s%s}\n",
  2068. res->command & ATA_DRDY ? "DRDY " : "",
  2069. res->command & ATA_DF ? "DF " : "",
  2070. res->command & ATA_DRQ ? "DRQ " : "",
  2071. res->command & ATA_SENSE ? "SENSE " : "",
  2072. res->command & ATA_ERR ? "ERR " : "");
  2073. }
  2074. if (cmd->command != ATA_CMD_PACKET &&
  2075. (res->feature & (ATA_ICRC | ATA_UNC | ATA_AMNF |
  2076. ATA_IDNF | ATA_ABORTED)))
  2077. ata_dev_err(qc->dev, "error: { %s%s%s%s%s}\n",
  2078. res->feature & ATA_ICRC ? "ICRC " : "",
  2079. res->feature & ATA_UNC ? "UNC " : "",
  2080. res->feature & ATA_AMNF ? "AMNF " : "",
  2081. res->feature & ATA_IDNF ? "IDNF " : "",
  2082. res->feature & ATA_ABORTED ? "ABRT " : "");
  2083. #endif
  2084. }
  2085. }
  2086. /**
  2087. * ata_eh_report - report error handling to user
  2088. * @ap: ATA port to report EH about
  2089. *
  2090. * Report EH to user.
  2091. *
  2092. * LOCKING:
  2093. * None.
  2094. */
  2095. void ata_eh_report(struct ata_port *ap)
  2096. {
  2097. struct ata_link *link;
  2098. ata_for_each_link(link, ap, HOST_FIRST)
  2099. ata_eh_link_report(link);
  2100. }
  2101. static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset,
  2102. unsigned int *classes, unsigned long deadline,
  2103. bool clear_classes)
  2104. {
  2105. struct ata_device *dev;
  2106. if (clear_classes)
  2107. ata_for_each_dev(dev, link, ALL)
  2108. classes[dev->devno] = ATA_DEV_UNKNOWN;
  2109. return reset(link, classes, deadline);
  2110. }
  2111. static int ata_eh_followup_srst_needed(struct ata_link *link, int rc)
  2112. {
  2113. if ((link->flags & ATA_LFLAG_NO_SRST) || ata_link_offline(link))
  2114. return 0;
  2115. if (rc == -EAGAIN)
  2116. return 1;
  2117. if (sata_pmp_supported(link->ap) && ata_is_host_link(link))
  2118. return 1;
  2119. return 0;
  2120. }
  2121. int ata_eh_reset(struct ata_link *link, int classify,
  2122. ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
  2123. ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
  2124. {
  2125. struct ata_port *ap = link->ap;
  2126. struct ata_link *slave = ap->slave_link;
  2127. struct ata_eh_context *ehc = &link->eh_context;
  2128. struct ata_eh_context *sehc = slave ? &slave->eh_context : NULL;
  2129. unsigned int *classes = ehc->classes;
  2130. unsigned int lflags = link->flags;
  2131. int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
  2132. int max_tries = 0, try = 0;
  2133. struct ata_link *failed_link;
  2134. struct ata_device *dev;
  2135. unsigned long deadline, now;
  2136. ata_reset_fn_t reset;
  2137. unsigned long flags;
  2138. u32 sstatus;
  2139. int nr_unknown, rc;
  2140. /*
  2141. * Prepare to reset
  2142. */
  2143. while (ata_eh_reset_timeouts[max_tries] != ULONG_MAX)
  2144. max_tries++;
  2145. if (link->flags & ATA_LFLAG_RST_ONCE)
  2146. max_tries = 1;
  2147. if (link->flags & ATA_LFLAG_NO_HRST)
  2148. hardreset = NULL;
  2149. if (link->flags & ATA_LFLAG_NO_SRST)
  2150. softreset = NULL;
  2151. /* make sure each reset attempt is at least COOL_DOWN apart */
  2152. if (ehc->i.flags & ATA_EHI_DID_RESET) {
  2153. now = jiffies;
  2154. WARN_ON(time_after(ehc->last_reset, now));
  2155. deadline = ata_deadline(ehc->last_reset,
  2156. ATA_EH_RESET_COOL_DOWN);
  2157. if (time_before(now, deadline))
  2158. schedule_timeout_uninterruptible(deadline - now);
  2159. }
  2160. spin_lock_irqsave(ap->lock, flags);
  2161. ap->pflags |= ATA_PFLAG_RESETTING;
  2162. spin_unlock_irqrestore(ap->lock, flags);
  2163. ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
  2164. ata_for_each_dev(dev, link, ALL) {
  2165. /* If we issue an SRST then an ATA drive (not ATAPI)
  2166. * may change configuration and be in PIO0 timing. If
  2167. * we do a hard reset (or are coming from power on)
  2168. * this is true for ATA or ATAPI. Until we've set a
  2169. * suitable controller mode we should not touch the
  2170. * bus as we may be talking too fast.
  2171. */
  2172. dev->pio_mode = XFER_PIO_0;
  2173. dev->dma_mode = 0xff;
  2174. /* If the controller has a pio mode setup function
  2175. * then use it to set the chipset to rights. Don't
  2176. * touch the DMA setup as that will be dealt with when
  2177. * configuring devices.
  2178. */
  2179. if (ap->ops->set_piomode)
  2180. ap->ops->set_piomode(ap, dev);
  2181. }
  2182. /* prefer hardreset */
  2183. reset = NULL;
  2184. ehc->i.action &= ~ATA_EH_RESET;
  2185. if (hardreset) {
  2186. reset = hardreset;
  2187. ehc->i.action |= ATA_EH_HARDRESET;
  2188. } else if (softreset) {
  2189. reset = softreset;
  2190. ehc->i.action |= ATA_EH_SOFTRESET;
  2191. }
  2192. if (prereset) {
  2193. unsigned long deadline = ata_deadline(jiffies,
  2194. ATA_EH_PRERESET_TIMEOUT);
  2195. if (slave) {
  2196. sehc->i.action &= ~ATA_EH_RESET;
  2197. sehc->i.action |= ehc->i.action;
  2198. }
  2199. rc = prereset(link, deadline);
  2200. /* If present, do prereset on slave link too. Reset
  2201. * is skipped iff both master and slave links report
  2202. * -ENOENT or clear ATA_EH_RESET.
  2203. */
  2204. if (slave && (rc == 0 || rc == -ENOENT)) {
  2205. int tmp;
  2206. tmp = prereset(slave, deadline);
  2207. if (tmp != -ENOENT)
  2208. rc = tmp;
  2209. ehc->i.action |= sehc->i.action;
  2210. }
  2211. if (rc) {
  2212. if (rc == -ENOENT) {
  2213. ata_link_dbg(link, "port disabled--ignoring\n");
  2214. ehc->i.action &= ~ATA_EH_RESET;
  2215. ata_for_each_dev(dev, link, ALL)
  2216. classes[dev->devno] = ATA_DEV_NONE;
  2217. rc = 0;
  2218. } else
  2219. ata_link_err(link,
  2220. "prereset failed (errno=%d)\n",
  2221. rc);
  2222. goto out;
  2223. }
  2224. /* prereset() might have cleared ATA_EH_RESET. If so,
  2225. * bang classes, thaw and return.
  2226. */
  2227. if (reset && !(ehc->i.action & ATA_EH_RESET)) {
  2228. ata_for_each_dev(dev, link, ALL)
  2229. classes[dev->devno] = ATA_DEV_NONE;
  2230. if ((ap->pflags & ATA_PFLAG_FROZEN) &&
  2231. ata_is_host_link(link))
  2232. ata_eh_thaw_port(ap);
  2233. rc = 0;
  2234. goto out;
  2235. }
  2236. }
  2237. retry:
  2238. /*
  2239. * Perform reset
  2240. */
  2241. if (ata_is_host_link(link))
  2242. ata_eh_freeze_port(ap);
  2243. deadline = ata_deadline(jiffies, ata_eh_reset_timeouts[try++]);
  2244. if (reset) {
  2245. if (verbose)
  2246. ata_link_info(link, "%s resetting link\n",
  2247. reset == softreset ? "soft" : "hard");
  2248. /* mark that this EH session started with reset */
  2249. ehc->last_reset = jiffies;
  2250. if (reset == hardreset)
  2251. ehc->i.flags |= ATA_EHI_DID_HARDRESET;
  2252. else
  2253. ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
  2254. rc = ata_do_reset(link, reset, classes, deadline, true);
  2255. if (rc && rc != -EAGAIN) {
  2256. failed_link = link;
  2257. goto fail;
  2258. }
  2259. /* hardreset slave link if existent */
  2260. if (slave && reset == hardreset) {
  2261. int tmp;
  2262. if (verbose)
  2263. ata_link_info(slave, "hard resetting link\n");
  2264. ata_eh_about_to_do(slave, NULL, ATA_EH_RESET);
  2265. tmp = ata_do_reset(slave, reset, classes, deadline,
  2266. false);
  2267. switch (tmp) {
  2268. case -EAGAIN:
  2269. rc = -EAGAIN;
  2270. case 0:
  2271. break;
  2272. default:
  2273. failed_link = slave;
  2274. rc = tmp;
  2275. goto fail;
  2276. }
  2277. }
  2278. /* perform follow-up SRST if necessary */
  2279. if (reset == hardreset &&
  2280. ata_eh_followup_srst_needed(link, rc)) {
  2281. reset = softreset;
  2282. if (!reset) {
  2283. ata_link_err(link,
  2284. "follow-up softreset required but no softreset available\n");
  2285. failed_link = link;
  2286. rc = -EINVAL;
  2287. goto fail;
  2288. }
  2289. ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
  2290. rc = ata_do_reset(link, reset, classes, deadline, true);
  2291. if (rc) {
  2292. failed_link = link;
  2293. goto fail;
  2294. }
  2295. }
  2296. } else {
  2297. if (verbose)
  2298. ata_link_info(link,
  2299. "no reset method available, skipping reset\n");
  2300. if (!(lflags & ATA_LFLAG_ASSUME_CLASS))
  2301. lflags |= ATA_LFLAG_ASSUME_ATA;
  2302. }
  2303. /*
  2304. * Post-reset processing
  2305. */
  2306. ata_for_each_dev(dev, link, ALL) {
  2307. /* After the reset, the device state is PIO 0 and the
  2308. * controller state is undefined. Reset also wakes up
  2309. * drives from sleeping mode.
  2310. */
  2311. dev->pio_mode = XFER_PIO_0;
  2312. dev->flags &= ~ATA_DFLAG_SLEEPING;
  2313. if (ata_phys_link_offline(ata_dev_phys_link(dev)))
  2314. continue;
  2315. /* apply class override */
  2316. if (lflags & ATA_LFLAG_ASSUME_ATA)
  2317. classes[dev->devno] = ATA_DEV_ATA;
  2318. else if (lflags & ATA_LFLAG_ASSUME_SEMB)
  2319. classes[dev->devno] = ATA_DEV_SEMB_UNSUP;
  2320. }
  2321. /* record current link speed */
  2322. if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0)
  2323. link->sata_spd = (sstatus >> 4) & 0xf;
  2324. if (slave && sata_scr_read(slave, SCR_STATUS, &sstatus) == 0)
  2325. slave->sata_spd = (sstatus >> 4) & 0xf;
  2326. /* thaw the port */
  2327. if (ata_is_host_link(link))
  2328. ata_eh_thaw_port(ap);
  2329. /* postreset() should clear hardware SError. Although SError
  2330. * is cleared during link resume, clearing SError here is
  2331. * necessary as some PHYs raise hotplug events after SRST.
  2332. * This introduces race condition where hotplug occurs between
  2333. * reset and here. This race is mediated by cross checking
  2334. * link onlineness and classification result later.
  2335. */
  2336. if (postreset) {
  2337. postreset(link, classes);
  2338. if (slave)
  2339. postreset(slave, classes);
  2340. }
  2341. /*
  2342. * Some controllers can't be frozen very well and may set spurious
  2343. * error conditions during reset. Clear accumulated error
  2344. * information and re-thaw the port if frozen. As reset is the
  2345. * final recovery action and we cross check link onlineness against
  2346. * device classification later, no hotplug event is lost by this.
  2347. */
  2348. spin_lock_irqsave(link->ap->lock, flags);
  2349. memset(&link->eh_info, 0, sizeof(link->eh_info));
  2350. if (slave)
  2351. memset(&slave->eh_info, 0, sizeof(link->eh_info));
  2352. ap->pflags &= ~ATA_PFLAG_EH_PENDING;
  2353. spin_unlock_irqrestore(link->ap->lock, flags);
  2354. if (ap->pflags & ATA_PFLAG_FROZEN)
  2355. ata_eh_thaw_port(ap);
  2356. /*
  2357. * Make sure onlineness and classification result correspond.
  2358. * Hotplug could have happened during reset and some
  2359. * controllers fail to wait while a drive is spinning up after
  2360. * being hotplugged causing misdetection. By cross checking
  2361. * link on/offlineness and classification result, those
  2362. * conditions can be reliably detected and retried.
  2363. */
  2364. nr_unknown = 0;
  2365. ata_for_each_dev(dev, link, ALL) {
  2366. if (ata_phys_link_online(ata_dev_phys_link(dev))) {
  2367. if (classes[dev->devno] == ATA_DEV_UNKNOWN) {
  2368. ata_dev_dbg(dev, "link online but device misclassified\n");
  2369. classes[dev->devno] = ATA_DEV_NONE;
  2370. nr_unknown++;
  2371. }
  2372. } else if (ata_phys_link_offline(ata_dev_phys_link(dev))) {
  2373. if (ata_class_enabled(classes[dev->devno]))
  2374. ata_dev_dbg(dev,
  2375. "link offline, clearing class %d to NONE\n",
  2376. classes[dev->devno]);
  2377. classes[dev->devno] = ATA_DEV_NONE;
  2378. } else if (classes[dev->devno] == ATA_DEV_UNKNOWN) {
  2379. ata_dev_dbg(dev,
  2380. "link status unknown, clearing UNKNOWN to NONE\n");
  2381. classes[dev->devno] = ATA_DEV_NONE;
  2382. }
  2383. }
  2384. if (classify && nr_unknown) {
  2385. if (try < max_tries) {
  2386. ata_link_warn(link,
  2387. "link online but %d devices misclassified, retrying\n",
  2388. nr_unknown);
  2389. failed_link = link;
  2390. rc = -EAGAIN;
  2391. goto fail;
  2392. }
  2393. ata_link_warn(link,
  2394. "link online but %d devices misclassified, "
  2395. "device detection might fail\n", nr_unknown);
  2396. }
  2397. /* reset successful, schedule revalidation */
  2398. ata_eh_done(link, NULL, ATA_EH_RESET);
  2399. if (slave)
  2400. ata_eh_done(slave, NULL, ATA_EH_RESET);
  2401. ehc->last_reset = jiffies; /* update to completion time */
  2402. ehc->i.action |= ATA_EH_REVALIDATE;
  2403. link->lpm_policy = ATA_LPM_UNKNOWN; /* reset LPM state */
  2404. rc = 0;
  2405. out:
  2406. /* clear hotplug flag */
  2407. ehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
  2408. if (slave)
  2409. sehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
  2410. spin_lock_irqsave(ap->lock, flags);
  2411. ap->pflags &= ~ATA_PFLAG_RESETTING;
  2412. spin_unlock_irqrestore(ap->lock, flags);
  2413. return rc;
  2414. fail:
  2415. /* if SCR isn't accessible on a fan-out port, PMP needs to be reset */
  2416. if (!ata_is_host_link(link) &&
  2417. sata_scr_read(link, SCR_STATUS, &sstatus))
  2418. rc = -ERESTART;
  2419. if (try >= max_tries) {
  2420. /*
  2421. * Thaw host port even if reset failed, so that the port
  2422. * can be retried on the next phy event. This risks
  2423. * repeated EH runs but seems to be a better tradeoff than
  2424. * shutting down a port after a botched hotplug attempt.
  2425. */
  2426. if (ata_is_host_link(link))
  2427. ata_eh_thaw_port(ap);
  2428. goto out;
  2429. }
  2430. now = jiffies;
  2431. if (time_before(now, deadline)) {
  2432. unsigned long delta = deadline - now;
  2433. ata_link_warn(failed_link,
  2434. "reset failed (errno=%d), retrying in %u secs\n",
  2435. rc, DIV_ROUND_UP(jiffies_to_msecs(delta), 1000));
  2436. ata_eh_release(ap);
  2437. while (delta)
  2438. delta = schedule_timeout_uninterruptible(delta);
  2439. ata_eh_acquire(ap);
  2440. }
  2441. /*
  2442. * While disks spinup behind PMP, some controllers fail sending SRST.
  2443. * They need to be reset - as well as the PMP - before retrying.
  2444. */
  2445. if (rc == -ERESTART) {
  2446. if (ata_is_host_link(link))
  2447. ata_eh_thaw_port(ap);
  2448. goto out;
  2449. }
  2450. if (try == max_tries - 1) {
  2451. sata_down_spd_limit(link, 0);
  2452. if (slave)
  2453. sata_down_spd_limit(slave, 0);
  2454. } else if (rc == -EPIPE)
  2455. sata_down_spd_limit(failed_link, 0);
  2456. if (hardreset)
  2457. reset = hardreset;
  2458. goto retry;
  2459. }
  2460. static inline void ata_eh_pull_park_action(struct ata_port *ap)
  2461. {
  2462. struct ata_link *link;
  2463. struct ata_device *dev;
  2464. unsigned long flags;
  2465. /*
  2466. * This function can be thought of as an extended version of
  2467. * ata_eh_about_to_do() specially crafted to accommodate the
  2468. * requirements of ATA_EH_PARK handling. Since the EH thread
  2469. * does not leave the do {} while () loop in ata_eh_recover as
  2470. * long as the timeout for a park request to *one* device on
  2471. * the port has not expired, and since we still want to pick
  2472. * up park requests to other devices on the same port or
  2473. * timeout updates for the same device, we have to pull
  2474. * ATA_EH_PARK actions from eh_info into eh_context.i
  2475. * ourselves at the beginning of each pass over the loop.
  2476. *
  2477. * Additionally, all write accesses to &ap->park_req_pending
  2478. * through reinit_completion() (see below) or complete_all()
  2479. * (see ata_scsi_park_store()) are protected by the host lock.
  2480. * As a result we have that park_req_pending.done is zero on
  2481. * exit from this function, i.e. when ATA_EH_PARK actions for
  2482. * *all* devices on port ap have been pulled into the
  2483. * respective eh_context structs. If, and only if,
  2484. * park_req_pending.done is non-zero by the time we reach
  2485. * wait_for_completion_timeout(), another ATA_EH_PARK action
  2486. * has been scheduled for at least one of the devices on port
  2487. * ap and we have to cycle over the do {} while () loop in
  2488. * ata_eh_recover() again.
  2489. */
  2490. spin_lock_irqsave(ap->lock, flags);
  2491. reinit_completion(&ap->park_req_pending);
  2492. ata_for_each_link(link, ap, EDGE) {
  2493. ata_for_each_dev(dev, link, ALL) {
  2494. struct ata_eh_info *ehi = &link->eh_info;
  2495. link->eh_context.i.dev_action[dev->devno] |=
  2496. ehi->dev_action[dev->devno] & ATA_EH_PARK;
  2497. ata_eh_clear_action(link, dev, ehi, ATA_EH_PARK);
  2498. }
  2499. }
  2500. spin_unlock_irqrestore(ap->lock, flags);
  2501. }
  2502. static void ata_eh_park_issue_cmd(struct ata_device *dev, int park)
  2503. {
  2504. struct ata_eh_context *ehc = &dev->link->eh_context;
  2505. struct ata_taskfile tf;
  2506. unsigned int err_mask;
  2507. ata_tf_init(dev, &tf);
  2508. if (park) {
  2509. ehc->unloaded_mask |= 1 << dev->devno;
  2510. tf.command = ATA_CMD_IDLEIMMEDIATE;
  2511. tf.feature = 0x44;
  2512. tf.lbal = 0x4c;
  2513. tf.lbam = 0x4e;
  2514. tf.lbah = 0x55;
  2515. } else {
  2516. ehc->unloaded_mask &= ~(1 << dev->devno);
  2517. tf.command = ATA_CMD_CHK_POWER;
  2518. }
  2519. tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
  2520. tf.protocol = ATA_PROT_NODATA;
  2521. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
  2522. if (park && (err_mask || tf.lbal != 0xc4)) {
  2523. ata_dev_err(dev, "head unload failed!\n");
  2524. ehc->unloaded_mask &= ~(1 << dev->devno);
  2525. }
  2526. }
  2527. static int ata_eh_revalidate_and_attach(struct ata_link *link,
  2528. struct ata_device **r_failed_dev)
  2529. {
  2530. struct ata_port *ap = link->ap;
  2531. struct ata_eh_context *ehc = &link->eh_context;
  2532. struct ata_device *dev;
  2533. unsigned int new_mask = 0;
  2534. unsigned long flags;
  2535. int rc = 0;
  2536. DPRINTK("ENTER\n");
  2537. /* For PATA drive side cable detection to work, IDENTIFY must
  2538. * be done backwards such that PDIAG- is released by the slave
  2539. * device before the master device is identified.
  2540. */
  2541. ata_for_each_dev(dev, link, ALL_REVERSE) {
  2542. unsigned int action = ata_eh_dev_action(dev);
  2543. unsigned int readid_flags = 0;
  2544. if (ehc->i.flags & ATA_EHI_DID_RESET)
  2545. readid_flags |= ATA_READID_POSTRESET;
  2546. if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
  2547. WARN_ON(dev->class == ATA_DEV_PMP);
  2548. if (ata_phys_link_offline(ata_dev_phys_link(dev))) {
  2549. rc = -EIO;
  2550. goto err;
  2551. }
  2552. ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE);
  2553. rc = ata_dev_revalidate(dev, ehc->classes[dev->devno],
  2554. readid_flags);
  2555. if (rc)
  2556. goto err;
  2557. ata_eh_done(link, dev, ATA_EH_REVALIDATE);
  2558. /* Configuration may have changed, reconfigure
  2559. * transfer mode.
  2560. */
  2561. ehc->i.flags |= ATA_EHI_SETMODE;
  2562. /* schedule the scsi_rescan_device() here */
  2563. schedule_work(&(ap->scsi_rescan_task));
  2564. } else if (dev->class == ATA_DEV_UNKNOWN &&
  2565. ehc->tries[dev->devno] &&
  2566. ata_class_enabled(ehc->classes[dev->devno])) {
  2567. /* Temporarily set dev->class, it will be
  2568. * permanently set once all configurations are
  2569. * complete. This is necessary because new
  2570. * device configuration is done in two
  2571. * separate loops.
  2572. */
  2573. dev->class = ehc->classes[dev->devno];
  2574. if (dev->class == ATA_DEV_PMP)
  2575. rc = sata_pmp_attach(dev);
  2576. else
  2577. rc = ata_dev_read_id(dev, &dev->class,
  2578. readid_flags, dev->id);
  2579. /* read_id might have changed class, store and reset */
  2580. ehc->classes[dev->devno] = dev->class;
  2581. dev->class = ATA_DEV_UNKNOWN;
  2582. switch (rc) {
  2583. case 0:
  2584. /* clear error info accumulated during probe */
  2585. ata_ering_clear(&dev->ering);
  2586. new_mask |= 1 << dev->devno;
  2587. break;
  2588. case -ENOENT:
  2589. /* IDENTIFY was issued to non-existent
  2590. * device. No need to reset. Just
  2591. * thaw and ignore the device.
  2592. */
  2593. ata_eh_thaw_port(ap);
  2594. break;
  2595. default:
  2596. goto err;
  2597. }
  2598. }
  2599. }
  2600. /* PDIAG- should have been released, ask cable type if post-reset */
  2601. if ((ehc->i.flags & ATA_EHI_DID_RESET) && ata_is_host_link(link)) {
  2602. if (ap->ops->cable_detect)
  2603. ap->cbl = ap->ops->cable_detect(ap);
  2604. ata_force_cbl(ap);
  2605. }
  2606. /* Configure new devices forward such that user doesn't see
  2607. * device detection messages backwards.
  2608. */
  2609. ata_for_each_dev(dev, link, ALL) {
  2610. if (!(new_mask & (1 << dev->devno)))
  2611. continue;
  2612. dev->class = ehc->classes[dev->devno];
  2613. if (dev->class == ATA_DEV_PMP)
  2614. continue;
  2615. ehc->i.flags |= ATA_EHI_PRINTINFO;
  2616. rc = ata_dev_configure(dev);
  2617. ehc->i.flags &= ~ATA_EHI_PRINTINFO;
  2618. if (rc) {
  2619. dev->class = ATA_DEV_UNKNOWN;
  2620. goto err;
  2621. }
  2622. spin_lock_irqsave(ap->lock, flags);
  2623. ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
  2624. spin_unlock_irqrestore(ap->lock, flags);
  2625. /* new device discovered, configure xfermode */
  2626. ehc->i.flags |= ATA_EHI_SETMODE;
  2627. }
  2628. return 0;
  2629. err:
  2630. *r_failed_dev = dev;
  2631. DPRINTK("EXIT rc=%d\n", rc);
  2632. return rc;
  2633. }
  2634. /**
  2635. * ata_set_mode - Program timings and issue SET FEATURES - XFER
  2636. * @link: link on which timings will be programmed
  2637. * @r_failed_dev: out parameter for failed device
  2638. *
  2639. * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
  2640. * ata_set_mode() fails, pointer to the failing device is
  2641. * returned in @r_failed_dev.
  2642. *
  2643. * LOCKING:
  2644. * PCI/etc. bus probe sem.
  2645. *
  2646. * RETURNS:
  2647. * 0 on success, negative errno otherwise
  2648. */
  2649. int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
  2650. {
  2651. struct ata_port *ap = link->ap;
  2652. struct ata_device *dev;
  2653. int rc;
  2654. /* if data transfer is verified, clear DUBIOUS_XFER on ering top */
  2655. ata_for_each_dev(dev, link, ENABLED) {
  2656. if (!(dev->flags & ATA_DFLAG_DUBIOUS_XFER)) {
  2657. struct ata_ering_entry *ent;
  2658. ent = ata_ering_top(&dev->ering);
  2659. if (ent)
  2660. ent->eflags &= ~ATA_EFLAG_DUBIOUS_XFER;
  2661. }
  2662. }
  2663. /* has private set_mode? */
  2664. if (ap->ops->set_mode)
  2665. rc = ap->ops->set_mode(link, r_failed_dev);
  2666. else
  2667. rc = ata_do_set_mode(link, r_failed_dev);
  2668. /* if transfer mode has changed, set DUBIOUS_XFER on device */
  2669. ata_for_each_dev(dev, link, ENABLED) {
  2670. struct ata_eh_context *ehc = &link->eh_context;
  2671. u8 saved_xfer_mode = ehc->saved_xfer_mode[dev->devno];
  2672. u8 saved_ncq = !!(ehc->saved_ncq_enabled & (1 << dev->devno));
  2673. if (dev->xfer_mode != saved_xfer_mode ||
  2674. ata_ncq_enabled(dev) != saved_ncq)
  2675. dev->flags |= ATA_DFLAG_DUBIOUS_XFER;
  2676. }
  2677. return rc;
  2678. }
  2679. /**
  2680. * atapi_eh_clear_ua - Clear ATAPI UNIT ATTENTION after reset
  2681. * @dev: ATAPI device to clear UA for
  2682. *
  2683. * Resets and other operations can make an ATAPI device raise
  2684. * UNIT ATTENTION which causes the next operation to fail. This
  2685. * function clears UA.
  2686. *
  2687. * LOCKING:
  2688. * EH context (may sleep).
  2689. *
  2690. * RETURNS:
  2691. * 0 on success, -errno on failure.
  2692. */
  2693. static int atapi_eh_clear_ua(struct ata_device *dev)
  2694. {
  2695. int i;
  2696. for (i = 0; i < ATA_EH_UA_TRIES; i++) {
  2697. u8 *sense_buffer = dev->link->ap->sector_buf;
  2698. u8 sense_key = 0;
  2699. unsigned int err_mask;
  2700. err_mask = atapi_eh_tur(dev, &sense_key);
  2701. if (err_mask != 0 && err_mask != AC_ERR_DEV) {
  2702. ata_dev_warn(dev,
  2703. "TEST_UNIT_READY failed (err_mask=0x%x)\n",
  2704. err_mask);
  2705. return -EIO;
  2706. }
  2707. if (!err_mask || sense_key != UNIT_ATTENTION)
  2708. return 0;
  2709. err_mask = atapi_eh_request_sense(dev, sense_buffer, sense_key);
  2710. if (err_mask) {
  2711. ata_dev_warn(dev, "failed to clear "
  2712. "UNIT ATTENTION (err_mask=0x%x)\n", err_mask);
  2713. return -EIO;
  2714. }
  2715. }
  2716. ata_dev_warn(dev, "UNIT ATTENTION persists after %d tries\n",
  2717. ATA_EH_UA_TRIES);
  2718. return 0;
  2719. }
  2720. /**
  2721. * ata_eh_maybe_retry_flush - Retry FLUSH if necessary
  2722. * @dev: ATA device which may need FLUSH retry
  2723. *
  2724. * If @dev failed FLUSH, it needs to be reported upper layer
  2725. * immediately as it means that @dev failed to remap and already
  2726. * lost at least a sector and further FLUSH retrials won't make
  2727. * any difference to the lost sector. However, if FLUSH failed
  2728. * for other reasons, for example transmission error, FLUSH needs
  2729. * to be retried.
  2730. *
  2731. * This function determines whether FLUSH failure retry is
  2732. * necessary and performs it if so.
  2733. *
  2734. * RETURNS:
  2735. * 0 if EH can continue, -errno if EH needs to be repeated.
  2736. */
  2737. static int ata_eh_maybe_retry_flush(struct ata_device *dev)
  2738. {
  2739. struct ata_link *link = dev->link;
  2740. struct ata_port *ap = link->ap;
  2741. struct ata_queued_cmd *qc;
  2742. struct ata_taskfile tf;
  2743. unsigned int err_mask;
  2744. int rc = 0;
  2745. /* did flush fail for this device? */
  2746. if (!ata_tag_valid(link->active_tag))
  2747. return 0;
  2748. qc = __ata_qc_from_tag(ap, link->active_tag);
  2749. if (qc->dev != dev || (qc->tf.command != ATA_CMD_FLUSH_EXT &&
  2750. qc->tf.command != ATA_CMD_FLUSH))
  2751. return 0;
  2752. /* if the device failed it, it should be reported to upper layers */
  2753. if (qc->err_mask & AC_ERR_DEV)
  2754. return 0;
  2755. /* flush failed for some other reason, give it another shot */
  2756. ata_tf_init(dev, &tf);
  2757. tf.command = qc->tf.command;
  2758. tf.flags |= ATA_TFLAG_DEVICE;
  2759. tf.protocol = ATA_PROT_NODATA;
  2760. ata_dev_warn(dev, "retrying FLUSH 0x%x Emask 0x%x\n",
  2761. tf.command, qc->err_mask);
  2762. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
  2763. if (!err_mask) {
  2764. /*
  2765. * FLUSH is complete but there's no way to
  2766. * successfully complete a failed command from EH.
  2767. * Making sure retry is allowed at least once and
  2768. * retrying it should do the trick - whatever was in
  2769. * the cache is already on the platter and this won't
  2770. * cause infinite loop.
  2771. */
  2772. qc->scsicmd->allowed = max(qc->scsicmd->allowed, 1);
  2773. } else {
  2774. ata_dev_warn(dev, "FLUSH failed Emask 0x%x\n",
  2775. err_mask);
  2776. rc = -EIO;
  2777. /* if device failed it, report it to upper layers */
  2778. if (err_mask & AC_ERR_DEV) {
  2779. qc->err_mask |= AC_ERR_DEV;
  2780. qc->result_tf = tf;
  2781. if (!(ap->pflags & ATA_PFLAG_FROZEN))
  2782. rc = 0;
  2783. }
  2784. }
  2785. return rc;
  2786. }
  2787. /**
  2788. * ata_eh_set_lpm - configure SATA interface power management
  2789. * @link: link to configure power management
  2790. * @policy: the link power management policy
  2791. * @r_failed_dev: out parameter for failed device
  2792. *
  2793. * Enable SATA Interface power management. This will enable
  2794. * Device Interface Power Management (DIPM) for min_power and
  2795. * medium_power_with_dipm policies, and then call driver specific
  2796. * callbacks for enabling Host Initiated Power management.
  2797. *
  2798. * LOCKING:
  2799. * EH context.
  2800. *
  2801. * RETURNS:
  2802. * 0 on success, -errno on failure.
  2803. */
  2804. static int ata_eh_set_lpm(struct ata_link *link, enum ata_lpm_policy policy,
  2805. struct ata_device **r_failed_dev)
  2806. {
  2807. struct ata_port *ap = ata_is_host_link(link) ? link->ap : NULL;
  2808. struct ata_eh_context *ehc = &link->eh_context;
  2809. struct ata_device *dev, *link_dev = NULL, *lpm_dev = NULL;
  2810. enum ata_lpm_policy old_policy = link->lpm_policy;
  2811. bool no_dipm = link->ap->flags & ATA_FLAG_NO_DIPM;
  2812. unsigned int hints = ATA_LPM_EMPTY | ATA_LPM_HIPM;
  2813. unsigned int err_mask;
  2814. int rc;
  2815. /* if the link or host doesn't do LPM, noop */
  2816. if (!IS_ENABLED(CONFIG_SATA_HOST) ||
  2817. (link->flags & ATA_LFLAG_NO_LPM) || (ap && !ap->ops->set_lpm))
  2818. return 0;
  2819. /*
  2820. * DIPM is enabled only for MIN_POWER as some devices
  2821. * misbehave when the host NACKs transition to SLUMBER. Order
  2822. * device and link configurations such that the host always
  2823. * allows DIPM requests.
  2824. */
  2825. ata_for_each_dev(dev, link, ENABLED) {
  2826. bool hipm = ata_id_has_hipm(dev->id);
  2827. bool dipm = ata_id_has_dipm(dev->id) && !no_dipm;
  2828. /* find the first enabled and LPM enabled devices */
  2829. if (!link_dev)
  2830. link_dev = dev;
  2831. if (!lpm_dev && (hipm || dipm))
  2832. lpm_dev = dev;
  2833. hints &= ~ATA_LPM_EMPTY;
  2834. if (!hipm)
  2835. hints &= ~ATA_LPM_HIPM;
  2836. /* disable DIPM before changing link config */
  2837. if (policy < ATA_LPM_MED_POWER_WITH_DIPM && dipm) {
  2838. err_mask = ata_dev_set_feature(dev,
  2839. SETFEATURES_SATA_DISABLE, SATA_DIPM);
  2840. if (err_mask && err_mask != AC_ERR_DEV) {
  2841. ata_dev_warn(dev,
  2842. "failed to disable DIPM, Emask 0x%x\n",
  2843. err_mask);
  2844. rc = -EIO;
  2845. goto fail;
  2846. }
  2847. }
  2848. }
  2849. if (ap) {
  2850. rc = ap->ops->set_lpm(link, policy, hints);
  2851. if (!rc && ap->slave_link)
  2852. rc = ap->ops->set_lpm(ap->slave_link, policy, hints);
  2853. } else
  2854. rc = sata_pmp_set_lpm(link, policy, hints);
  2855. /*
  2856. * Attribute link config failure to the first (LPM) enabled
  2857. * device on the link.
  2858. */
  2859. if (rc) {
  2860. if (rc == -EOPNOTSUPP) {
  2861. link->flags |= ATA_LFLAG_NO_LPM;
  2862. return 0;
  2863. }
  2864. dev = lpm_dev ? lpm_dev : link_dev;
  2865. goto fail;
  2866. }
  2867. /*
  2868. * Low level driver acked the transition. Issue DIPM command
  2869. * with the new policy set.
  2870. */
  2871. link->lpm_policy = policy;
  2872. if (ap && ap->slave_link)
  2873. ap->slave_link->lpm_policy = policy;
  2874. /* host config updated, enable DIPM if transitioning to MIN_POWER */
  2875. ata_for_each_dev(dev, link, ENABLED) {
  2876. if (policy >= ATA_LPM_MED_POWER_WITH_DIPM && !no_dipm &&
  2877. ata_id_has_dipm(dev->id)) {
  2878. err_mask = ata_dev_set_feature(dev,
  2879. SETFEATURES_SATA_ENABLE, SATA_DIPM);
  2880. if (err_mask && err_mask != AC_ERR_DEV) {
  2881. ata_dev_warn(dev,
  2882. "failed to enable DIPM, Emask 0x%x\n",
  2883. err_mask);
  2884. rc = -EIO;
  2885. goto fail;
  2886. }
  2887. }
  2888. }
  2889. link->last_lpm_change = jiffies;
  2890. link->flags |= ATA_LFLAG_CHANGED;
  2891. return 0;
  2892. fail:
  2893. /* restore the old policy */
  2894. link->lpm_policy = old_policy;
  2895. if (ap && ap->slave_link)
  2896. ap->slave_link->lpm_policy = old_policy;
  2897. /* if no device or only one more chance is left, disable LPM */
  2898. if (!dev || ehc->tries[dev->devno] <= 2) {
  2899. ata_link_warn(link, "disabling LPM on the link\n");
  2900. link->flags |= ATA_LFLAG_NO_LPM;
  2901. }
  2902. if (r_failed_dev)
  2903. *r_failed_dev = dev;
  2904. return rc;
  2905. }
  2906. int ata_link_nr_enabled(struct ata_link *link)
  2907. {
  2908. struct ata_device *dev;
  2909. int cnt = 0;
  2910. ata_for_each_dev(dev, link, ENABLED)
  2911. cnt++;
  2912. return cnt;
  2913. }
  2914. static int ata_link_nr_vacant(struct ata_link *link)
  2915. {
  2916. struct ata_device *dev;
  2917. int cnt = 0;
  2918. ata_for_each_dev(dev, link, ALL)
  2919. if (dev->class == ATA_DEV_UNKNOWN)
  2920. cnt++;
  2921. return cnt;
  2922. }
  2923. static int ata_eh_skip_recovery(struct ata_link *link)
  2924. {
  2925. struct ata_port *ap = link->ap;
  2926. struct ata_eh_context *ehc = &link->eh_context;
  2927. struct ata_device *dev;
  2928. /* skip disabled links */
  2929. if (link->flags & ATA_LFLAG_DISABLED)
  2930. return 1;
  2931. /* skip if explicitly requested */
  2932. if (ehc->i.flags & ATA_EHI_NO_RECOVERY)
  2933. return 1;
  2934. /* thaw frozen port and recover failed devices */
  2935. if ((ap->pflags & ATA_PFLAG_FROZEN) || ata_link_nr_enabled(link))
  2936. return 0;
  2937. /* reset at least once if reset is requested */
  2938. if ((ehc->i.action & ATA_EH_RESET) &&
  2939. !(ehc->i.flags & ATA_EHI_DID_RESET))
  2940. return 0;
  2941. /* skip if class codes for all vacant slots are ATA_DEV_NONE */
  2942. ata_for_each_dev(dev, link, ALL) {
  2943. if (dev->class == ATA_DEV_UNKNOWN &&
  2944. ehc->classes[dev->devno] != ATA_DEV_NONE)
  2945. return 0;
  2946. }
  2947. return 1;
  2948. }
  2949. static int ata_count_probe_trials_cb(struct ata_ering_entry *ent, void *void_arg)
  2950. {
  2951. u64 interval = msecs_to_jiffies(ATA_EH_PROBE_TRIAL_INTERVAL);
  2952. u64 now = get_jiffies_64();
  2953. int *trials = void_arg;
  2954. if ((ent->eflags & ATA_EFLAG_OLD_ER) ||
  2955. (ent->timestamp < now - min(now, interval)))
  2956. return -1;
  2957. (*trials)++;
  2958. return 0;
  2959. }
  2960. static int ata_eh_schedule_probe(struct ata_device *dev)
  2961. {
  2962. struct ata_eh_context *ehc = &dev->link->eh_context;
  2963. struct ata_link *link = ata_dev_phys_link(dev);
  2964. int trials = 0;
  2965. if (!(ehc->i.probe_mask & (1 << dev->devno)) ||
  2966. (ehc->did_probe_mask & (1 << dev->devno)))
  2967. return 0;
  2968. ata_eh_detach_dev(dev);
  2969. ata_dev_init(dev);
  2970. ehc->did_probe_mask |= (1 << dev->devno);
  2971. ehc->i.action |= ATA_EH_RESET;
  2972. ehc->saved_xfer_mode[dev->devno] = 0;
  2973. ehc->saved_ncq_enabled &= ~(1 << dev->devno);
  2974. /* the link maybe in a deep sleep, wake it up */
  2975. if (link->lpm_policy > ATA_LPM_MAX_POWER) {
  2976. if (ata_is_host_link(link))
  2977. link->ap->ops->set_lpm(link, ATA_LPM_MAX_POWER,
  2978. ATA_LPM_EMPTY);
  2979. else
  2980. sata_pmp_set_lpm(link, ATA_LPM_MAX_POWER,
  2981. ATA_LPM_EMPTY);
  2982. }
  2983. /* Record and count probe trials on the ering. The specific
  2984. * error mask used is irrelevant. Because a successful device
  2985. * detection clears the ering, this count accumulates only if
  2986. * there are consecutive failed probes.
  2987. *
  2988. * If the count is equal to or higher than ATA_EH_PROBE_TRIALS
  2989. * in the last ATA_EH_PROBE_TRIAL_INTERVAL, link speed is
  2990. * forced to 1.5Gbps.
  2991. *
  2992. * This is to work around cases where failed link speed
  2993. * negotiation results in device misdetection leading to
  2994. * infinite DEVXCHG or PHRDY CHG events.
  2995. */
  2996. ata_ering_record(&dev->ering, 0, AC_ERR_OTHER);
  2997. ata_ering_map(&dev->ering, ata_count_probe_trials_cb, &trials);
  2998. if (trials > ATA_EH_PROBE_TRIALS)
  2999. sata_down_spd_limit(link, 1);
  3000. return 1;
  3001. }
  3002. static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
  3003. {
  3004. struct ata_eh_context *ehc = &dev->link->eh_context;
  3005. /* -EAGAIN from EH routine indicates retry without prejudice.
  3006. * The requester is responsible for ensuring forward progress.
  3007. */
  3008. if (err != -EAGAIN)
  3009. ehc->tries[dev->devno]--;
  3010. switch (err) {
  3011. case -ENODEV:
  3012. /* device missing or wrong IDENTIFY data, schedule probing */
  3013. ehc->i.probe_mask |= (1 << dev->devno);
  3014. /* fall through */
  3015. case -EINVAL:
  3016. /* give it just one more chance */
  3017. ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
  3018. /* fall through */
  3019. case -EIO:
  3020. if (ehc->tries[dev->devno] == 1) {
  3021. /* This is the last chance, better to slow
  3022. * down than lose it.
  3023. */
  3024. sata_down_spd_limit(ata_dev_phys_link(dev), 0);
  3025. if (dev->pio_mode > XFER_PIO_0)
  3026. ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
  3027. }
  3028. }
  3029. if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
  3030. /* disable device if it has used up all its chances */
  3031. ata_dev_disable(dev);
  3032. /* detach if offline */
  3033. if (ata_phys_link_offline(ata_dev_phys_link(dev)))
  3034. ata_eh_detach_dev(dev);
  3035. /* schedule probe if necessary */
  3036. if (ata_eh_schedule_probe(dev)) {
  3037. ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
  3038. memset(ehc->cmd_timeout_idx[dev->devno], 0,
  3039. sizeof(ehc->cmd_timeout_idx[dev->devno]));
  3040. }
  3041. return 1;
  3042. } else {
  3043. ehc->i.action |= ATA_EH_RESET;
  3044. return 0;
  3045. }
  3046. }
  3047. /**
  3048. * ata_eh_recover - recover host port after error
  3049. * @ap: host port to recover
  3050. * @prereset: prereset method (can be NULL)
  3051. * @softreset: softreset method (can be NULL)
  3052. * @hardreset: hardreset method (can be NULL)
  3053. * @postreset: postreset method (can be NULL)
  3054. * @r_failed_link: out parameter for failed link
  3055. *
  3056. * This is the alpha and omega, eum and yang, heart and soul of
  3057. * libata exception handling. On entry, actions required to
  3058. * recover each link and hotplug requests are recorded in the
  3059. * link's eh_context. This function executes all the operations
  3060. * with appropriate retrials and fallbacks to resurrect failed
  3061. * devices, detach goners and greet newcomers.
  3062. *
  3063. * LOCKING:
  3064. * Kernel thread context (may sleep).
  3065. *
  3066. * RETURNS:
  3067. * 0 on success, -errno on failure.
  3068. */
  3069. int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
  3070. ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
  3071. ata_postreset_fn_t postreset,
  3072. struct ata_link **r_failed_link)
  3073. {
  3074. struct ata_link *link;
  3075. struct ata_device *dev;
  3076. int rc, nr_fails;
  3077. unsigned long flags, deadline;
  3078. DPRINTK("ENTER\n");
  3079. /* prep for recovery */
  3080. ata_for_each_link(link, ap, EDGE) {
  3081. struct ata_eh_context *ehc = &link->eh_context;
  3082. /* re-enable link? */
  3083. if (ehc->i.action & ATA_EH_ENABLE_LINK) {
  3084. ata_eh_about_to_do(link, NULL, ATA_EH_ENABLE_LINK);
  3085. spin_lock_irqsave(ap->lock, flags);
  3086. link->flags &= ~ATA_LFLAG_DISABLED;
  3087. spin_unlock_irqrestore(ap->lock, flags);
  3088. ata_eh_done(link, NULL, ATA_EH_ENABLE_LINK);
  3089. }
  3090. ata_for_each_dev(dev, link, ALL) {
  3091. if (link->flags & ATA_LFLAG_NO_RETRY)
  3092. ehc->tries[dev->devno] = 1;
  3093. else
  3094. ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
  3095. /* collect port action mask recorded in dev actions */
  3096. ehc->i.action |= ehc->i.dev_action[dev->devno] &
  3097. ~ATA_EH_PERDEV_MASK;
  3098. ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK;
  3099. /* process hotplug request */
  3100. if (dev->flags & ATA_DFLAG_DETACH)
  3101. ata_eh_detach_dev(dev);
  3102. /* schedule probe if necessary */
  3103. if (!ata_dev_enabled(dev))
  3104. ata_eh_schedule_probe(dev);
  3105. }
  3106. }
  3107. retry:
  3108. rc = 0;
  3109. /* if UNLOADING, finish immediately */
  3110. if (ap->pflags & ATA_PFLAG_UNLOADING)
  3111. goto out;
  3112. /* prep for EH */
  3113. ata_for_each_link(link, ap, EDGE) {
  3114. struct ata_eh_context *ehc = &link->eh_context;
  3115. /* skip EH if possible. */
  3116. if (ata_eh_skip_recovery(link))
  3117. ehc->i.action = 0;
  3118. ata_for_each_dev(dev, link, ALL)
  3119. ehc->classes[dev->devno] = ATA_DEV_UNKNOWN;
  3120. }
  3121. /* reset */
  3122. ata_for_each_link(link, ap, EDGE) {
  3123. struct ata_eh_context *ehc = &link->eh_context;
  3124. if (!(ehc->i.action & ATA_EH_RESET))
  3125. continue;
  3126. rc = ata_eh_reset(link, ata_link_nr_vacant(link),
  3127. prereset, softreset, hardreset, postreset);
  3128. if (rc) {
  3129. ata_link_err(link, "reset failed, giving up\n");
  3130. goto out;
  3131. }
  3132. }
  3133. do {
  3134. unsigned long now;
  3135. /*
  3136. * clears ATA_EH_PARK in eh_info and resets
  3137. * ap->park_req_pending
  3138. */
  3139. ata_eh_pull_park_action(ap);
  3140. deadline = jiffies;
  3141. ata_for_each_link(link, ap, EDGE) {
  3142. ata_for_each_dev(dev, link, ALL) {
  3143. struct ata_eh_context *ehc = &link->eh_context;
  3144. unsigned long tmp;
  3145. if (dev->class != ATA_DEV_ATA &&
  3146. dev->class != ATA_DEV_ZAC)
  3147. continue;
  3148. if (!(ehc->i.dev_action[dev->devno] &
  3149. ATA_EH_PARK))
  3150. continue;
  3151. tmp = dev->unpark_deadline;
  3152. if (time_before(deadline, tmp))
  3153. deadline = tmp;
  3154. else if (time_before_eq(tmp, jiffies))
  3155. continue;
  3156. if (ehc->unloaded_mask & (1 << dev->devno))
  3157. continue;
  3158. ata_eh_park_issue_cmd(dev, 1);
  3159. }
  3160. }
  3161. now = jiffies;
  3162. if (time_before_eq(deadline, now))
  3163. break;
  3164. ata_eh_release(ap);
  3165. deadline = wait_for_completion_timeout(&ap->park_req_pending,
  3166. deadline - now);
  3167. ata_eh_acquire(ap);
  3168. } while (deadline);
  3169. ata_for_each_link(link, ap, EDGE) {
  3170. ata_for_each_dev(dev, link, ALL) {
  3171. if (!(link->eh_context.unloaded_mask &
  3172. (1 << dev->devno)))
  3173. continue;
  3174. ata_eh_park_issue_cmd(dev, 0);
  3175. ata_eh_done(link, dev, ATA_EH_PARK);
  3176. }
  3177. }
  3178. /* the rest */
  3179. nr_fails = 0;
  3180. ata_for_each_link(link, ap, PMP_FIRST) {
  3181. struct ata_eh_context *ehc = &link->eh_context;
  3182. if (sata_pmp_attached(ap) && ata_is_host_link(link))
  3183. goto config_lpm;
  3184. /* revalidate existing devices and attach new ones */
  3185. rc = ata_eh_revalidate_and_attach(link, &dev);
  3186. if (rc)
  3187. goto rest_fail;
  3188. /* if PMP got attached, return, pmp EH will take care of it */
  3189. if (link->device->class == ATA_DEV_PMP) {
  3190. ehc->i.action = 0;
  3191. return 0;
  3192. }
  3193. /* configure transfer mode if necessary */
  3194. if (ehc->i.flags & ATA_EHI_SETMODE) {
  3195. rc = ata_set_mode(link, &dev);
  3196. if (rc)
  3197. goto rest_fail;
  3198. ehc->i.flags &= ~ATA_EHI_SETMODE;
  3199. }
  3200. /* If reset has been issued, clear UA to avoid
  3201. * disrupting the current users of the device.
  3202. */
  3203. if (ehc->i.flags & ATA_EHI_DID_RESET) {
  3204. ata_for_each_dev(dev, link, ALL) {
  3205. if (dev->class != ATA_DEV_ATAPI)
  3206. continue;
  3207. rc = atapi_eh_clear_ua(dev);
  3208. if (rc)
  3209. goto rest_fail;
  3210. if (zpodd_dev_enabled(dev))
  3211. zpodd_post_poweron(dev);
  3212. }
  3213. }
  3214. /* retry flush if necessary */
  3215. ata_for_each_dev(dev, link, ALL) {
  3216. if (dev->class != ATA_DEV_ATA &&
  3217. dev->class != ATA_DEV_ZAC)
  3218. continue;
  3219. rc = ata_eh_maybe_retry_flush(dev);
  3220. if (rc)
  3221. goto rest_fail;
  3222. }
  3223. config_lpm:
  3224. /* configure link power saving */
  3225. if (link->lpm_policy != ap->target_lpm_policy) {
  3226. rc = ata_eh_set_lpm(link, ap->target_lpm_policy, &dev);
  3227. if (rc)
  3228. goto rest_fail;
  3229. }
  3230. /* this link is okay now */
  3231. ehc->i.flags = 0;
  3232. continue;
  3233. rest_fail:
  3234. nr_fails++;
  3235. if (dev)
  3236. ata_eh_handle_dev_fail(dev, rc);
  3237. if (ap->pflags & ATA_PFLAG_FROZEN) {
  3238. /* PMP reset requires working host port.
  3239. * Can't retry if it's frozen.
  3240. */
  3241. if (sata_pmp_attached(ap))
  3242. goto out;
  3243. break;
  3244. }
  3245. }
  3246. if (nr_fails)
  3247. goto retry;
  3248. out:
  3249. if (rc && r_failed_link)
  3250. *r_failed_link = link;
  3251. DPRINTK("EXIT, rc=%d\n", rc);
  3252. return rc;
  3253. }
  3254. /**
  3255. * ata_eh_finish - finish up EH
  3256. * @ap: host port to finish EH for
  3257. *
  3258. * Recovery is complete. Clean up EH states and retry or finish
  3259. * failed qcs.
  3260. *
  3261. * LOCKING:
  3262. * None.
  3263. */
  3264. void ata_eh_finish(struct ata_port *ap)
  3265. {
  3266. struct ata_queued_cmd *qc;
  3267. int tag;
  3268. /* retry or finish qcs */
  3269. ata_qc_for_each_raw(ap, qc, tag) {
  3270. if (!(qc->flags & ATA_QCFLAG_FAILED))
  3271. continue;
  3272. if (qc->err_mask) {
  3273. /* FIXME: Once EH migration is complete,
  3274. * generate sense data in this function,
  3275. * considering both err_mask and tf.
  3276. */
  3277. if (qc->flags & ATA_QCFLAG_RETRY)
  3278. ata_eh_qc_retry(qc);
  3279. else
  3280. ata_eh_qc_complete(qc);
  3281. } else {
  3282. if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
  3283. ata_eh_qc_complete(qc);
  3284. } else {
  3285. /* feed zero TF to sense generation */
  3286. memset(&qc->result_tf, 0, sizeof(qc->result_tf));
  3287. ata_eh_qc_retry(qc);
  3288. }
  3289. }
  3290. }
  3291. /* make sure nr_active_links is zero after EH */
  3292. WARN_ON(ap->nr_active_links);
  3293. ap->nr_active_links = 0;
  3294. }
  3295. /**
  3296. * ata_do_eh - do standard error handling
  3297. * @ap: host port to handle error for
  3298. *
  3299. * @prereset: prereset method (can be NULL)
  3300. * @softreset: softreset method (can be NULL)
  3301. * @hardreset: hardreset method (can be NULL)
  3302. * @postreset: postreset method (can be NULL)
  3303. *
  3304. * Perform standard error handling sequence.
  3305. *
  3306. * LOCKING:
  3307. * Kernel thread context (may sleep).
  3308. */
  3309. void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
  3310. ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
  3311. ata_postreset_fn_t postreset)
  3312. {
  3313. struct ata_device *dev;
  3314. int rc;
  3315. ata_eh_autopsy(ap);
  3316. ata_eh_report(ap);
  3317. rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset,
  3318. NULL);
  3319. if (rc) {
  3320. ata_for_each_dev(dev, &ap->link, ALL)
  3321. ata_dev_disable(dev);
  3322. }
  3323. ata_eh_finish(ap);
  3324. }
  3325. /**
  3326. * ata_std_error_handler - standard error handler
  3327. * @ap: host port to handle error for
  3328. *
  3329. * Standard error handler
  3330. *
  3331. * LOCKING:
  3332. * Kernel thread context (may sleep).
  3333. */
  3334. void ata_std_error_handler(struct ata_port *ap)
  3335. {
  3336. struct ata_port_operations *ops = ap->ops;
  3337. ata_reset_fn_t hardreset = ops->hardreset;
  3338. /* ignore built-in hardreset if SCR access is not available */
  3339. if (hardreset == sata_std_hardreset && !sata_scr_valid(&ap->link))
  3340. hardreset = NULL;
  3341. ata_do_eh(ap, ops->prereset, ops->softreset, hardreset, ops->postreset);
  3342. }
  3343. EXPORT_SYMBOL_GPL(ata_std_error_handler);
  3344. #ifdef CONFIG_PM
  3345. /**
  3346. * ata_eh_handle_port_suspend - perform port suspend operation
  3347. * @ap: port to suspend
  3348. *
  3349. * Suspend @ap.
  3350. *
  3351. * LOCKING:
  3352. * Kernel thread context (may sleep).
  3353. */
  3354. static void ata_eh_handle_port_suspend(struct ata_port *ap)
  3355. {
  3356. unsigned long flags;
  3357. int rc = 0;
  3358. struct ata_device *dev;
  3359. /* are we suspending? */
  3360. spin_lock_irqsave(ap->lock, flags);
  3361. if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
  3362. ap->pm_mesg.event & PM_EVENT_RESUME) {
  3363. spin_unlock_irqrestore(ap->lock, flags);
  3364. return;
  3365. }
  3366. spin_unlock_irqrestore(ap->lock, flags);
  3367. WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
  3368. /*
  3369. * If we have a ZPODD attached, check its zero
  3370. * power ready status before the port is frozen.
  3371. * Only needed for runtime suspend.
  3372. */
  3373. if (PMSG_IS_AUTO(ap->pm_mesg)) {
  3374. ata_for_each_dev(dev, &ap->link, ENABLED) {
  3375. if (zpodd_dev_enabled(dev))
  3376. zpodd_on_suspend(dev);
  3377. }
  3378. }
  3379. /* tell ACPI we're suspending */
  3380. rc = ata_acpi_on_suspend(ap);
  3381. if (rc)
  3382. goto out;
  3383. /* suspend */
  3384. ata_eh_freeze_port(ap);
  3385. if (ap->ops->port_suspend)
  3386. rc = ap->ops->port_suspend(ap, ap->pm_mesg);
  3387. ata_acpi_set_state(ap, ap->pm_mesg);
  3388. out:
  3389. /* update the flags */
  3390. spin_lock_irqsave(ap->lock, flags);
  3391. ap->pflags &= ~ATA_PFLAG_PM_PENDING;
  3392. if (rc == 0)
  3393. ap->pflags |= ATA_PFLAG_SUSPENDED;
  3394. else if (ap->pflags & ATA_PFLAG_FROZEN)
  3395. ata_port_schedule_eh(ap);
  3396. spin_unlock_irqrestore(ap->lock, flags);
  3397. return;
  3398. }
  3399. /**
  3400. * ata_eh_handle_port_resume - perform port resume operation
  3401. * @ap: port to resume
  3402. *
  3403. * Resume @ap.
  3404. *
  3405. * LOCKING:
  3406. * Kernel thread context (may sleep).
  3407. */
  3408. static void ata_eh_handle_port_resume(struct ata_port *ap)
  3409. {
  3410. struct ata_link *link;
  3411. struct ata_device *dev;
  3412. unsigned long flags;
  3413. /* are we resuming? */
  3414. spin_lock_irqsave(ap->lock, flags);
  3415. if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
  3416. !(ap->pm_mesg.event & PM_EVENT_RESUME)) {
  3417. spin_unlock_irqrestore(ap->lock, flags);
  3418. return;
  3419. }
  3420. spin_unlock_irqrestore(ap->lock, flags);
  3421. WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
  3422. /*
  3423. * Error timestamps are in jiffies which doesn't run while
  3424. * suspended and PHY events during resume isn't too uncommon.
  3425. * When the two are combined, it can lead to unnecessary speed
  3426. * downs if the machine is suspended and resumed repeatedly.
  3427. * Clear error history.
  3428. */
  3429. ata_for_each_link(link, ap, HOST_FIRST)
  3430. ata_for_each_dev(dev, link, ALL)
  3431. ata_ering_clear(&dev->ering);
  3432. ata_acpi_set_state(ap, ap->pm_mesg);
  3433. if (ap->ops->port_resume)
  3434. ap->ops->port_resume(ap);
  3435. /* tell ACPI that we're resuming */
  3436. ata_acpi_on_resume(ap);
  3437. /* update the flags */
  3438. spin_lock_irqsave(ap->lock, flags);
  3439. ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
  3440. spin_unlock_irqrestore(ap->lock, flags);
  3441. }
  3442. #endif /* CONFIG_PM */