libata-eh.c 33 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403
  1. /*
  2. * libata-eh.c - libata error handling
  3. *
  4. * Maintained by: Jeff Garzik <jgarzik@pobox.com>
  5. * Please ALWAYS copy linux-ide@vger.kernel.org
  6. * on emails.
  7. *
  8. * Copyright 2006 Tejun Heo <htejun@gmail.com>
  9. *
  10. *
  11. * This program is free software; you can redistribute it and/or
  12. * modify it under the terms of the GNU General Public License as
  13. * published by the Free Software Foundation; either version 2, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; see the file COPYING. If not, write to
  23. * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139,
  24. * USA.
  25. *
  26. *
  27. * libata documentation is available via 'make {ps|pdf}docs',
  28. * as Documentation/DocBook/libata.*
  29. *
  30. * Hardware documentation available from http://www.t13.org/ and
  31. * http://www.sata-io.org/
  32. *
  33. */
  34. #include <linux/config.h>
  35. #include <linux/kernel.h>
  36. #include <scsi/scsi.h>
  37. #include <scsi/scsi_host.h>
  38. #include <scsi/scsi_eh.h>
  39. #include <scsi/scsi_device.h>
  40. #include <scsi/scsi_cmnd.h>
  41. #include <linux/libata.h>
  42. #include "libata.h"
  43. static void __ata_port_freeze(struct ata_port *ap);
  44. static void ata_ering_record(struct ata_ering *ering, int is_io,
  45. unsigned int err_mask)
  46. {
  47. struct ata_ering_entry *ent;
  48. WARN_ON(!err_mask);
  49. ering->cursor++;
  50. ering->cursor %= ATA_ERING_SIZE;
  51. ent = &ering->ring[ering->cursor];
  52. ent->is_io = is_io;
  53. ent->err_mask = err_mask;
  54. ent->timestamp = get_jiffies_64();
  55. }
  56. static struct ata_ering_entry * ata_ering_top(struct ata_ering *ering)
  57. {
  58. struct ata_ering_entry *ent = &ering->ring[ering->cursor];
  59. if (!ent->err_mask)
  60. return NULL;
  61. return ent;
  62. }
  63. static int ata_ering_map(struct ata_ering *ering,
  64. int (*map_fn)(struct ata_ering_entry *, void *),
  65. void *arg)
  66. {
  67. int idx, rc = 0;
  68. struct ata_ering_entry *ent;
  69. idx = ering->cursor;
  70. do {
  71. ent = &ering->ring[idx];
  72. if (!ent->err_mask)
  73. break;
  74. rc = map_fn(ent, arg);
  75. if (rc)
  76. break;
  77. idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
  78. } while (idx != ering->cursor);
  79. return rc;
  80. }
  81. /**
  82. * ata_scsi_timed_out - SCSI layer time out callback
  83. * @cmd: timed out SCSI command
  84. *
  85. * Handles SCSI layer timeout. We race with normal completion of
  86. * the qc for @cmd. If the qc is already gone, we lose and let
  87. * the scsi command finish (EH_HANDLED). Otherwise, the qc has
  88. * timed out and EH should be invoked. Prevent ata_qc_complete()
  89. * from finishing it by setting EH_SCHEDULED and return
  90. * EH_NOT_HANDLED.
  91. *
  92. * TODO: kill this function once old EH is gone.
  93. *
  94. * LOCKING:
  95. * Called from timer context
  96. *
  97. * RETURNS:
  98. * EH_HANDLED or EH_NOT_HANDLED
  99. */
  100. enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
  101. {
  102. struct Scsi_Host *host = cmd->device->host;
  103. struct ata_port *ap = ata_shost_to_port(host);
  104. unsigned long flags;
  105. struct ata_queued_cmd *qc;
  106. enum scsi_eh_timer_return ret;
  107. DPRINTK("ENTER\n");
  108. if (ap->ops->error_handler) {
  109. ret = EH_NOT_HANDLED;
  110. goto out;
  111. }
  112. ret = EH_HANDLED;
  113. spin_lock_irqsave(&ap->host_set->lock, flags);
  114. qc = ata_qc_from_tag(ap, ap->active_tag);
  115. if (qc) {
  116. WARN_ON(qc->scsicmd != cmd);
  117. qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
  118. qc->err_mask |= AC_ERR_TIMEOUT;
  119. ret = EH_NOT_HANDLED;
  120. }
  121. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  122. out:
  123. DPRINTK("EXIT, ret=%d\n", ret);
  124. return ret;
  125. }
  126. /**
  127. * ata_scsi_error - SCSI layer error handler callback
  128. * @host: SCSI host on which error occurred
  129. *
  130. * Handles SCSI-layer-thrown error events.
  131. *
  132. * LOCKING:
  133. * Inherited from SCSI layer (none, can sleep)
  134. *
  135. * RETURNS:
  136. * Zero.
  137. */
  138. void ata_scsi_error(struct Scsi_Host *host)
  139. {
  140. struct ata_port *ap = ata_shost_to_port(host);
  141. spinlock_t *hs_lock = &ap->host_set->lock;
  142. int i, repeat_cnt = ATA_EH_MAX_REPEAT;
  143. unsigned long flags;
  144. DPRINTK("ENTER\n");
  145. /* synchronize with port task */
  146. ata_port_flush_task(ap);
  147. /* synchronize with host_set lock and sort out timeouts */
  148. /* For new EH, all qcs are finished in one of three ways -
  149. * normal completion, error completion, and SCSI timeout.
  150. * Both cmpletions can race against SCSI timeout. When normal
  151. * completion wins, the qc never reaches EH. When error
  152. * completion wins, the qc has ATA_QCFLAG_FAILED set.
  153. *
  154. * When SCSI timeout wins, things are a bit more complex.
  155. * Normal or error completion can occur after the timeout but
  156. * before this point. In such cases, both types of
  157. * completions are honored. A scmd is determined to have
  158. * timed out iff its associated qc is active and not failed.
  159. */
  160. if (ap->ops->error_handler) {
  161. struct scsi_cmnd *scmd, *tmp;
  162. int nr_timedout = 0;
  163. spin_lock_irqsave(hs_lock, flags);
  164. list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {
  165. struct ata_queued_cmd *qc;
  166. for (i = 0; i < ATA_MAX_QUEUE; i++) {
  167. qc = __ata_qc_from_tag(ap, i);
  168. if (qc->flags & ATA_QCFLAG_ACTIVE &&
  169. qc->scsicmd == scmd)
  170. break;
  171. }
  172. if (i < ATA_MAX_QUEUE) {
  173. /* the scmd has an associated qc */
  174. if (!(qc->flags & ATA_QCFLAG_FAILED)) {
  175. /* which hasn't failed yet, timeout */
  176. qc->err_mask |= AC_ERR_TIMEOUT;
  177. qc->flags |= ATA_QCFLAG_FAILED;
  178. nr_timedout++;
  179. }
  180. } else {
  181. /* Normal completion occurred after
  182. * SCSI timeout but before this point.
  183. * Successfully complete it.
  184. */
  185. scmd->retries = scmd->allowed;
  186. scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
  187. }
  188. }
  189. /* If we have timed out qcs. They belong to EH from
  190. * this point but the state of the controller is
  191. * unknown. Freeze the port to make sure the IRQ
  192. * handler doesn't diddle with those qcs. This must
  193. * be done atomically w.r.t. setting QCFLAG_FAILED.
  194. */
  195. if (nr_timedout)
  196. __ata_port_freeze(ap);
  197. spin_unlock_irqrestore(hs_lock, flags);
  198. } else
  199. spin_unlock_wait(hs_lock);
  200. repeat:
  201. /* invoke error handler */
  202. if (ap->ops->error_handler) {
  203. /* fetch & clear EH info */
  204. spin_lock_irqsave(hs_lock, flags);
  205. memset(&ap->eh_context, 0, sizeof(ap->eh_context));
  206. ap->eh_context.i = ap->eh_info;
  207. memset(&ap->eh_info, 0, sizeof(ap->eh_info));
  208. ap->flags &= ~ATA_FLAG_EH_PENDING;
  209. spin_unlock_irqrestore(hs_lock, flags);
  210. /* invoke EH */
  211. ap->ops->error_handler(ap);
  212. /* Exception might have happend after ->error_handler
  213. * recovered the port but before this point. Repeat
  214. * EH in such case.
  215. */
  216. spin_lock_irqsave(hs_lock, flags);
  217. if (ap->flags & ATA_FLAG_EH_PENDING) {
  218. if (--repeat_cnt) {
  219. ata_port_printk(ap, KERN_INFO,
  220. "EH pending after completion, "
  221. "repeating EH (cnt=%d)\n", repeat_cnt);
  222. spin_unlock_irqrestore(hs_lock, flags);
  223. goto repeat;
  224. }
  225. ata_port_printk(ap, KERN_ERR, "EH pending after %d "
  226. "tries, giving up\n", ATA_EH_MAX_REPEAT);
  227. }
  228. /* this run is complete, make sure EH info is clear */
  229. memset(&ap->eh_info, 0, sizeof(ap->eh_info));
  230. /* Clear host_eh_scheduled while holding hs_lock such
  231. * that if exception occurs after this point but
  232. * before EH completion, SCSI midlayer will
  233. * re-initiate EH.
  234. */
  235. host->host_eh_scheduled = 0;
  236. spin_unlock_irqrestore(hs_lock, flags);
  237. } else {
  238. WARN_ON(ata_qc_from_tag(ap, ap->active_tag) == NULL);
  239. ap->ops->eng_timeout(ap);
  240. }
  241. /* finish or retry handled scmd's and clean up */
  242. WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
  243. scsi_eh_flush_done_q(&ap->eh_done_q);
  244. /* clean up */
  245. spin_lock_irqsave(hs_lock, flags);
  246. if (ap->flags & ATA_FLAG_RECOVERED)
  247. ata_port_printk(ap, KERN_INFO, "EH complete\n");
  248. ap->flags &= ~ATA_FLAG_RECOVERED;
  249. spin_unlock_irqrestore(hs_lock, flags);
  250. DPRINTK("EXIT\n");
  251. }
  252. /**
  253. * ata_qc_timeout - Handle timeout of queued command
  254. * @qc: Command that timed out
  255. *
  256. * Some part of the kernel (currently, only the SCSI layer)
  257. * has noticed that the active command on port @ap has not
  258. * completed after a specified length of time. Handle this
  259. * condition by disabling DMA (if necessary) and completing
  260. * transactions, with error if necessary.
  261. *
  262. * This also handles the case of the "lost interrupt", where
  263. * for some reason (possibly hardware bug, possibly driver bug)
  264. * an interrupt was not delivered to the driver, even though the
  265. * transaction completed successfully.
  266. *
  267. * TODO: kill this function once old EH is gone.
  268. *
  269. * LOCKING:
  270. * Inherited from SCSI layer (none, can sleep)
  271. */
  272. static void ata_qc_timeout(struct ata_queued_cmd *qc)
  273. {
  274. struct ata_port *ap = qc->ap;
  275. struct ata_host_set *host_set = ap->host_set;
  276. u8 host_stat = 0, drv_stat;
  277. unsigned long flags;
  278. DPRINTK("ENTER\n");
  279. ap->hsm_task_state = HSM_ST_IDLE;
  280. spin_lock_irqsave(&host_set->lock, flags);
  281. switch (qc->tf.protocol) {
  282. case ATA_PROT_DMA:
  283. case ATA_PROT_ATAPI_DMA:
  284. host_stat = ap->ops->bmdma_status(ap);
  285. /* before we do anything else, clear DMA-Start bit */
  286. ap->ops->bmdma_stop(qc);
  287. /* fall through */
  288. default:
  289. ata_altstatus(ap);
  290. drv_stat = ata_chk_status(ap);
  291. /* ack bmdma irq events */
  292. ap->ops->irq_clear(ap);
  293. ata_dev_printk(qc->dev, KERN_ERR, "command 0x%x timeout, "
  294. "stat 0x%x host_stat 0x%x\n",
  295. qc->tf.command, drv_stat, host_stat);
  296. /* complete taskfile transaction */
  297. qc->err_mask |= AC_ERR_TIMEOUT;
  298. break;
  299. }
  300. spin_unlock_irqrestore(&host_set->lock, flags);
  301. ata_eh_qc_complete(qc);
  302. DPRINTK("EXIT\n");
  303. }
  304. /**
  305. * ata_eng_timeout - Handle timeout of queued command
  306. * @ap: Port on which timed-out command is active
  307. *
  308. * Some part of the kernel (currently, only the SCSI layer)
  309. * has noticed that the active command on port @ap has not
  310. * completed after a specified length of time. Handle this
  311. * condition by disabling DMA (if necessary) and completing
  312. * transactions, with error if necessary.
  313. *
  314. * This also handles the case of the "lost interrupt", where
  315. * for some reason (possibly hardware bug, possibly driver bug)
  316. * an interrupt was not delivered to the driver, even though the
  317. * transaction completed successfully.
  318. *
  319. * TODO: kill this function once old EH is gone.
  320. *
  321. * LOCKING:
  322. * Inherited from SCSI layer (none, can sleep)
  323. */
  324. void ata_eng_timeout(struct ata_port *ap)
  325. {
  326. DPRINTK("ENTER\n");
  327. ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
  328. DPRINTK("EXIT\n");
  329. }
  330. /**
  331. * ata_qc_schedule_eh - schedule qc for error handling
  332. * @qc: command to schedule error handling for
  333. *
  334. * Schedule error handling for @qc. EH will kick in as soon as
  335. * other commands are drained.
  336. *
  337. * LOCKING:
  338. * spin_lock_irqsave(host_set lock)
  339. */
  340. void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
  341. {
  342. struct ata_port *ap = qc->ap;
  343. WARN_ON(!ap->ops->error_handler);
  344. qc->flags |= ATA_QCFLAG_FAILED;
  345. qc->ap->flags |= ATA_FLAG_EH_PENDING;
  346. /* The following will fail if timeout has already expired.
  347. * ata_scsi_error() takes care of such scmds on EH entry.
  348. * Note that ATA_QCFLAG_FAILED is unconditionally set after
  349. * this function completes.
  350. */
  351. scsi_req_abort_cmd(qc->scsicmd);
  352. }
  353. /**
  354. * ata_port_schedule_eh - schedule error handling without a qc
  355. * @ap: ATA port to schedule EH for
  356. *
  357. * Schedule error handling for @ap. EH will kick in as soon as
  358. * all commands are drained.
  359. *
  360. * LOCKING:
  361. * spin_lock_irqsave(host_set lock)
  362. */
  363. void ata_port_schedule_eh(struct ata_port *ap)
  364. {
  365. WARN_ON(!ap->ops->error_handler);
  366. ap->flags |= ATA_FLAG_EH_PENDING;
  367. ata_schedule_scsi_eh(ap->host);
  368. DPRINTK("port EH scheduled\n");
  369. }
  370. /**
  371. * ata_port_abort - abort all qc's on the port
  372. * @ap: ATA port to abort qc's for
  373. *
  374. * Abort all active qc's of @ap and schedule EH.
  375. *
  376. * LOCKING:
  377. * spin_lock_irqsave(host_set lock)
  378. *
  379. * RETURNS:
  380. * Number of aborted qc's.
  381. */
  382. int ata_port_abort(struct ata_port *ap)
  383. {
  384. int tag, nr_aborted = 0;
  385. WARN_ON(!ap->ops->error_handler);
  386. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  387. struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
  388. if (qc) {
  389. qc->flags |= ATA_QCFLAG_FAILED;
  390. ata_qc_complete(qc);
  391. nr_aborted++;
  392. }
  393. }
  394. if (!nr_aborted)
  395. ata_port_schedule_eh(ap);
  396. return nr_aborted;
  397. }
  398. /**
  399. * __ata_port_freeze - freeze port
  400. * @ap: ATA port to freeze
  401. *
  402. * This function is called when HSM violation or some other
  403. * condition disrupts normal operation of the port. Frozen port
  404. * is not allowed to perform any operation until the port is
  405. * thawed, which usually follows a successful reset.
  406. *
  407. * ap->ops->freeze() callback can be used for freezing the port
  408. * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
  409. * port cannot be frozen hardware-wise, the interrupt handler
  410. * must ack and clear interrupts unconditionally while the port
  411. * is frozen.
  412. *
  413. * LOCKING:
  414. * spin_lock_irqsave(host_set lock)
  415. */
  416. static void __ata_port_freeze(struct ata_port *ap)
  417. {
  418. WARN_ON(!ap->ops->error_handler);
  419. if (ap->ops->freeze)
  420. ap->ops->freeze(ap);
  421. ap->flags |= ATA_FLAG_FROZEN;
  422. DPRINTK("ata%u port frozen\n", ap->id);
  423. }
  424. /**
  425. * ata_port_freeze - abort & freeze port
  426. * @ap: ATA port to freeze
  427. *
  428. * Abort and freeze @ap.
  429. *
  430. * LOCKING:
  431. * spin_lock_irqsave(host_set lock)
  432. *
  433. * RETURNS:
  434. * Number of aborted commands.
  435. */
  436. int ata_port_freeze(struct ata_port *ap)
  437. {
  438. int nr_aborted;
  439. WARN_ON(!ap->ops->error_handler);
  440. nr_aborted = ata_port_abort(ap);
  441. __ata_port_freeze(ap);
  442. return nr_aborted;
  443. }
  444. /**
  445. * ata_eh_freeze_port - EH helper to freeze port
  446. * @ap: ATA port to freeze
  447. *
  448. * Freeze @ap.
  449. *
  450. * LOCKING:
  451. * None.
  452. */
  453. void ata_eh_freeze_port(struct ata_port *ap)
  454. {
  455. unsigned long flags;
  456. if (!ap->ops->error_handler)
  457. return;
  458. spin_lock_irqsave(&ap->host_set->lock, flags);
  459. __ata_port_freeze(ap);
  460. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  461. }
  462. /**
  463. * ata_port_thaw_port - EH helper to thaw port
  464. * @ap: ATA port to thaw
  465. *
  466. * Thaw frozen port @ap.
  467. *
  468. * LOCKING:
  469. * None.
  470. */
  471. void ata_eh_thaw_port(struct ata_port *ap)
  472. {
  473. unsigned long flags;
  474. if (!ap->ops->error_handler)
  475. return;
  476. spin_lock_irqsave(&ap->host_set->lock, flags);
  477. ap->flags &= ~ATA_FLAG_FROZEN;
  478. if (ap->ops->thaw)
  479. ap->ops->thaw(ap);
  480. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  481. DPRINTK("ata%u port thawed\n", ap->id);
  482. }
  483. static void ata_eh_scsidone(struct scsi_cmnd *scmd)
  484. {
  485. /* nada */
  486. }
  487. static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
  488. {
  489. struct ata_port *ap = qc->ap;
  490. struct scsi_cmnd *scmd = qc->scsicmd;
  491. unsigned long flags;
  492. spin_lock_irqsave(&ap->host_set->lock, flags);
  493. qc->scsidone = ata_eh_scsidone;
  494. __ata_qc_complete(qc);
  495. WARN_ON(ata_tag_valid(qc->tag));
  496. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  497. scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
  498. }
  499. /**
  500. * ata_eh_qc_complete - Complete an active ATA command from EH
  501. * @qc: Command to complete
  502. *
  503. * Indicate to the mid and upper layers that an ATA command has
  504. * completed. To be used from EH.
  505. */
  506. void ata_eh_qc_complete(struct ata_queued_cmd *qc)
  507. {
  508. struct scsi_cmnd *scmd = qc->scsicmd;
  509. scmd->retries = scmd->allowed;
  510. __ata_eh_qc_complete(qc);
  511. }
  512. /**
  513. * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
  514. * @qc: Command to retry
  515. *
  516. * Indicate to the mid and upper layers that an ATA command
  517. * should be retried. To be used from EH.
  518. *
  519. * SCSI midlayer limits the number of retries to scmd->allowed.
  520. * scmd->retries is decremented for commands which get retried
  521. * due to unrelated failures (qc->err_mask is zero).
  522. */
  523. void ata_eh_qc_retry(struct ata_queued_cmd *qc)
  524. {
  525. struct scsi_cmnd *scmd = qc->scsicmd;
  526. if (!qc->err_mask && scmd->retries)
  527. scmd->retries--;
  528. __ata_eh_qc_complete(qc);
  529. }
  530. /**
  531. * ata_eh_about_to_do - about to perform eh_action
  532. * @ap: target ATA port
  533. * @action: action about to be performed
  534. *
  535. * Called just before performing EH actions to clear related bits
  536. * in @ap->eh_info such that eh actions are not unnecessarily
  537. * repeated.
  538. *
  539. * LOCKING:
  540. * None.
  541. */
  542. static void ata_eh_about_to_do(struct ata_port *ap, unsigned int action)
  543. {
  544. unsigned long flags;
  545. spin_lock_irqsave(&ap->host_set->lock, flags);
  546. ap->eh_info.action &= ~action;
  547. ap->flags |= ATA_FLAG_RECOVERED;
  548. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  549. }
  550. /**
  551. * ata_err_string - convert err_mask to descriptive string
  552. * @err_mask: error mask to convert to string
  553. *
  554. * Convert @err_mask to descriptive string. Errors are
  555. * prioritized according to severity and only the most severe
  556. * error is reported.
  557. *
  558. * LOCKING:
  559. * None.
  560. *
  561. * RETURNS:
  562. * Descriptive string for @err_mask
  563. */
  564. static const char * ata_err_string(unsigned int err_mask)
  565. {
  566. if (err_mask & AC_ERR_HOST_BUS)
  567. return "host bus error";
  568. if (err_mask & AC_ERR_ATA_BUS)
  569. return "ATA bus error";
  570. if (err_mask & AC_ERR_TIMEOUT)
  571. return "timeout";
  572. if (err_mask & AC_ERR_HSM)
  573. return "HSM violation";
  574. if (err_mask & AC_ERR_SYSTEM)
  575. return "internal error";
  576. if (err_mask & AC_ERR_MEDIA)
  577. return "media error";
  578. if (err_mask & AC_ERR_INVALID)
  579. return "invalid argument";
  580. if (err_mask & AC_ERR_DEV)
  581. return "device error";
  582. return "unknown error";
  583. }
  584. /**
  585. * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
  586. * @dev: device to perform REQUEST_SENSE to
  587. * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
  588. *
  589. * Perform ATAPI REQUEST_SENSE after the device reported CHECK
  590. * SENSE. This function is EH helper.
  591. *
  592. * LOCKING:
  593. * Kernel thread context (may sleep).
  594. *
  595. * RETURNS:
  596. * 0 on success, AC_ERR_* mask on failure
  597. */
  598. static unsigned int atapi_eh_request_sense(struct ata_device *dev,
  599. unsigned char *sense_buf)
  600. {
  601. struct ata_port *ap = dev->ap;
  602. struct ata_taskfile tf;
  603. u8 cdb[ATAPI_CDB_LEN];
  604. DPRINTK("ATAPI request sense\n");
  605. ata_tf_init(dev, &tf);
  606. /* FIXME: is this needed? */
  607. memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
  608. /* XXX: why tf_read here? */
  609. ap->ops->tf_read(ap, &tf);
  610. /* fill these in, for the case where they are -not- overwritten */
  611. sense_buf[0] = 0x70;
  612. sense_buf[2] = tf.feature >> 4;
  613. memset(cdb, 0, ATAPI_CDB_LEN);
  614. cdb[0] = REQUEST_SENSE;
  615. cdb[4] = SCSI_SENSE_BUFFERSIZE;
  616. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  617. tf.command = ATA_CMD_PACKET;
  618. /* is it pointless to prefer PIO for "safety reasons"? */
  619. if (ap->flags & ATA_FLAG_PIO_DMA) {
  620. tf.protocol = ATA_PROT_ATAPI_DMA;
  621. tf.feature |= ATAPI_PKT_DMA;
  622. } else {
  623. tf.protocol = ATA_PROT_ATAPI;
  624. tf.lbam = (8 * 1024) & 0xff;
  625. tf.lbah = (8 * 1024) >> 8;
  626. }
  627. return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
  628. sense_buf, SCSI_SENSE_BUFFERSIZE);
  629. }
  630. /**
  631. * ata_eh_analyze_serror - analyze SError for a failed port
  632. * @ap: ATA port to analyze SError for
  633. *
  634. * Analyze SError if available and further determine cause of
  635. * failure.
  636. *
  637. * LOCKING:
  638. * None.
  639. */
  640. static void ata_eh_analyze_serror(struct ata_port *ap)
  641. {
  642. struct ata_eh_context *ehc = &ap->eh_context;
  643. u32 serror = ehc->i.serror;
  644. unsigned int err_mask = 0, action = 0;
  645. if (serror & SERR_PERSISTENT) {
  646. err_mask |= AC_ERR_ATA_BUS;
  647. action |= ATA_EH_HARDRESET;
  648. }
  649. if (serror &
  650. (SERR_DATA_RECOVERED | SERR_COMM_RECOVERED | SERR_DATA)) {
  651. err_mask |= AC_ERR_ATA_BUS;
  652. action |= ATA_EH_SOFTRESET;
  653. }
  654. if (serror & SERR_PROTOCOL) {
  655. err_mask |= AC_ERR_HSM;
  656. action |= ATA_EH_SOFTRESET;
  657. }
  658. if (serror & SERR_INTERNAL) {
  659. err_mask |= AC_ERR_SYSTEM;
  660. action |= ATA_EH_SOFTRESET;
  661. }
  662. if (serror & (SERR_PHYRDY_CHG | SERR_DEV_XCHG)) {
  663. err_mask |= AC_ERR_ATA_BUS;
  664. action |= ATA_EH_HARDRESET;
  665. }
  666. ehc->i.err_mask |= err_mask;
  667. ehc->i.action |= action;
  668. }
  669. /**
  670. * ata_eh_analyze_tf - analyze taskfile of a failed qc
  671. * @qc: qc to analyze
  672. * @tf: Taskfile registers to analyze
  673. *
  674. * Analyze taskfile of @qc and further determine cause of
  675. * failure. This function also requests ATAPI sense data if
  676. * avaliable.
  677. *
  678. * LOCKING:
  679. * Kernel thread context (may sleep).
  680. *
  681. * RETURNS:
  682. * Determined recovery action
  683. */
  684. static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
  685. const struct ata_taskfile *tf)
  686. {
  687. unsigned int tmp, action = 0;
  688. u8 stat = tf->command, err = tf->feature;
  689. if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
  690. qc->err_mask |= AC_ERR_HSM;
  691. return ATA_EH_SOFTRESET;
  692. }
  693. if (!(qc->err_mask & AC_ERR_DEV))
  694. return 0;
  695. switch (qc->dev->class) {
  696. case ATA_DEV_ATA:
  697. if (err & ATA_ICRC)
  698. qc->err_mask |= AC_ERR_ATA_BUS;
  699. if (err & ATA_UNC)
  700. qc->err_mask |= AC_ERR_MEDIA;
  701. if (err & ATA_IDNF)
  702. qc->err_mask |= AC_ERR_INVALID;
  703. break;
  704. case ATA_DEV_ATAPI:
  705. tmp = atapi_eh_request_sense(qc->dev,
  706. qc->scsicmd->sense_buffer);
  707. if (!tmp) {
  708. /* ATA_QCFLAG_SENSE_VALID is used to tell
  709. * atapi_qc_complete() that sense data is
  710. * already valid.
  711. *
  712. * TODO: interpret sense data and set
  713. * appropriate err_mask.
  714. */
  715. qc->flags |= ATA_QCFLAG_SENSE_VALID;
  716. } else
  717. qc->err_mask |= tmp;
  718. }
  719. if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
  720. action |= ATA_EH_SOFTRESET;
  721. return action;
  722. }
  723. static int ata_eh_categorize_ering_entry(struct ata_ering_entry *ent)
  724. {
  725. if (ent->err_mask & (AC_ERR_ATA_BUS | AC_ERR_TIMEOUT))
  726. return 1;
  727. if (ent->is_io) {
  728. if (ent->err_mask & AC_ERR_HSM)
  729. return 1;
  730. if ((ent->err_mask &
  731. (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
  732. return 2;
  733. }
  734. return 0;
  735. }
  736. struct speed_down_needed_arg {
  737. u64 since;
  738. int nr_errors[3];
  739. };
  740. static int speed_down_needed_cb(struct ata_ering_entry *ent, void *void_arg)
  741. {
  742. struct speed_down_needed_arg *arg = void_arg;
  743. if (ent->timestamp < arg->since)
  744. return -1;
  745. arg->nr_errors[ata_eh_categorize_ering_entry(ent)]++;
  746. return 0;
  747. }
  748. /**
  749. * ata_eh_speed_down_needed - Determine wheter speed down is necessary
  750. * @dev: Device of interest
  751. *
  752. * This function examines error ring of @dev and determines
  753. * whether speed down is necessary. Speed down is necessary if
  754. * there have been more than 3 of Cat-1 errors or 10 of Cat-2
  755. * errors during last 15 minutes.
  756. *
  757. * Cat-1 errors are ATA_BUS, TIMEOUT for any command and HSM
  758. * violation for known supported commands.
  759. *
  760. * Cat-2 errors are unclassified DEV error for known supported
  761. * command.
  762. *
  763. * LOCKING:
  764. * Inherited from caller.
  765. *
  766. * RETURNS:
  767. * 1 if speed down is necessary, 0 otherwise
  768. */
  769. static int ata_eh_speed_down_needed(struct ata_device *dev)
  770. {
  771. const u64 interval = 15LLU * 60 * HZ;
  772. static const int err_limits[3] = { -1, 3, 10 };
  773. struct speed_down_needed_arg arg;
  774. struct ata_ering_entry *ent;
  775. int err_cat;
  776. u64 j64;
  777. ent = ata_ering_top(&dev->ering);
  778. if (!ent)
  779. return 0;
  780. err_cat = ata_eh_categorize_ering_entry(ent);
  781. if (err_cat == 0)
  782. return 0;
  783. memset(&arg, 0, sizeof(arg));
  784. j64 = get_jiffies_64();
  785. if (j64 >= interval)
  786. arg.since = j64 - interval;
  787. else
  788. arg.since = 0;
  789. ata_ering_map(&dev->ering, speed_down_needed_cb, &arg);
  790. return arg.nr_errors[err_cat] > err_limits[err_cat];
  791. }
  792. /**
  793. * ata_eh_speed_down - record error and speed down if necessary
  794. * @dev: Failed device
  795. * @is_io: Did the device fail during normal IO?
  796. * @err_mask: err_mask of the error
  797. *
  798. * Record error and examine error history to determine whether
  799. * adjusting transmission speed is necessary. It also sets
  800. * transmission limits appropriately if such adjustment is
  801. * necessary.
  802. *
  803. * LOCKING:
  804. * Kernel thread context (may sleep).
  805. *
  806. * RETURNS:
  807. * 0 on success, -errno otherwise
  808. */
  809. static int ata_eh_speed_down(struct ata_device *dev, int is_io,
  810. unsigned int err_mask)
  811. {
  812. if (!err_mask)
  813. return 0;
  814. /* record error and determine whether speed down is necessary */
  815. ata_ering_record(&dev->ering, is_io, err_mask);
  816. if (!ata_eh_speed_down_needed(dev))
  817. return 0;
  818. /* speed down SATA link speed if possible */
  819. if (sata_down_spd_limit(dev->ap) == 0)
  820. return ATA_EH_HARDRESET;
  821. /* lower transfer mode */
  822. if (ata_down_xfermask_limit(dev, 0) == 0)
  823. return ATA_EH_SOFTRESET;
  824. ata_dev_printk(dev, KERN_ERR,
  825. "speed down requested but no transfer mode left\n");
  826. return 0;
  827. }
  828. /**
  829. * ata_eh_autopsy - analyze error and determine recovery action
  830. * @ap: ATA port to perform autopsy on
  831. *
  832. * Analyze why @ap failed and determine which recovery action is
  833. * needed. This function also sets more detailed AC_ERR_* values
  834. * and fills sense data for ATAPI CHECK SENSE.
  835. *
  836. * LOCKING:
  837. * Kernel thread context (may sleep).
  838. */
  839. static void ata_eh_autopsy(struct ata_port *ap)
  840. {
  841. struct ata_eh_context *ehc = &ap->eh_context;
  842. unsigned int action = ehc->i.action;
  843. struct ata_device *failed_dev = NULL;
  844. unsigned int all_err_mask = 0;
  845. int tag, is_io = 0;
  846. u32 serror;
  847. int rc;
  848. DPRINTK("ENTER\n");
  849. /* obtain and analyze SError */
  850. rc = sata_scr_read(ap, SCR_ERROR, &serror);
  851. if (rc == 0) {
  852. ehc->i.serror |= serror;
  853. ata_eh_analyze_serror(ap);
  854. } else if (rc != -EOPNOTSUPP)
  855. action |= ATA_EH_HARDRESET;
  856. /* any real error trumps AC_ERR_OTHER */
  857. if (ehc->i.err_mask & ~AC_ERR_OTHER)
  858. ehc->i.err_mask &= ~AC_ERR_OTHER;
  859. all_err_mask |= ehc->i.err_mask;
  860. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  861. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  862. if (!(qc->flags & ATA_QCFLAG_FAILED))
  863. continue;
  864. /* inherit upper level err_mask */
  865. qc->err_mask |= ehc->i.err_mask;
  866. if (qc->err_mask & AC_ERR_TIMEOUT)
  867. action |= ATA_EH_SOFTRESET;
  868. /* analyze TF */
  869. action |= ata_eh_analyze_tf(qc, &qc->result_tf);
  870. /* DEV errors are probably spurious in case of ATA_BUS error */
  871. if (qc->err_mask & AC_ERR_ATA_BUS)
  872. qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
  873. AC_ERR_INVALID);
  874. /* any real error trumps unknown error */
  875. if (qc->err_mask & ~AC_ERR_OTHER)
  876. qc->err_mask &= ~AC_ERR_OTHER;
  877. /* SENSE_VALID trumps dev/unknown error and revalidation */
  878. if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
  879. qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
  880. action &= ~ATA_EH_REVALIDATE;
  881. }
  882. /* accumulate error info */
  883. failed_dev = qc->dev;
  884. all_err_mask |= qc->err_mask;
  885. if (qc->flags & ATA_QCFLAG_IO)
  886. is_io = 1;
  887. }
  888. /* speed down iff command was in progress */
  889. if (failed_dev)
  890. action |= ata_eh_speed_down(failed_dev, is_io, all_err_mask);
  891. if (all_err_mask)
  892. action |= ATA_EH_REVALIDATE;
  893. ehc->i.dev = failed_dev;
  894. ehc->i.action = action;
  895. DPRINTK("EXIT\n");
  896. }
  897. /**
  898. * ata_eh_report - report error handling to user
  899. * @ap: ATA port EH is going on
  900. *
  901. * Report EH to user.
  902. *
  903. * LOCKING:
  904. * None.
  905. */
  906. static void ata_eh_report(struct ata_port *ap)
  907. {
  908. struct ata_eh_context *ehc = &ap->eh_context;
  909. const char *frozen, *desc;
  910. int tag, nr_failed = 0;
  911. desc = NULL;
  912. if (ehc->i.desc[0] != '\0')
  913. desc = ehc->i.desc;
  914. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  915. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  916. if (!(qc->flags & ATA_QCFLAG_FAILED))
  917. continue;
  918. if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
  919. continue;
  920. nr_failed++;
  921. }
  922. if (!nr_failed && !ehc->i.err_mask)
  923. return;
  924. frozen = "";
  925. if (ap->flags & ATA_FLAG_FROZEN)
  926. frozen = " frozen";
  927. if (ehc->i.dev) {
  928. ata_dev_printk(ehc->i.dev, KERN_ERR,
  929. "exception Emask 0x%x SErr 0x%x action 0x%x%s\n",
  930. ehc->i.err_mask, ehc->i.serror, ehc->i.action,
  931. frozen);
  932. if (desc)
  933. ata_dev_printk(ehc->i.dev, KERN_ERR, "(%s)\n", desc);
  934. } else {
  935. ata_port_printk(ap, KERN_ERR,
  936. "exception Emask 0x%x SErr 0x%x action 0x%x%s\n",
  937. ehc->i.err_mask, ehc->i.serror, ehc->i.action,
  938. frozen);
  939. if (desc)
  940. ata_port_printk(ap, KERN_ERR, "(%s)\n", desc);
  941. }
  942. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  943. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  944. if (!(qc->flags & ATA_QCFLAG_FAILED) || !qc->err_mask)
  945. continue;
  946. ata_dev_printk(qc->dev, KERN_ERR, "tag %d cmd 0x%x "
  947. "Emask 0x%x stat 0x%x err 0x%x (%s)\n",
  948. qc->tag, qc->tf.command, qc->err_mask,
  949. qc->result_tf.command, qc->result_tf.feature,
  950. ata_err_string(qc->err_mask));
  951. }
  952. }
  953. static int ata_eh_reset(struct ata_port *ap, ata_reset_fn_t softreset,
  954. ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
  955. {
  956. struct ata_eh_context *ehc = &ap->eh_context;
  957. unsigned int classes[ATA_MAX_DEVICES];
  958. int tries = ATA_EH_RESET_TRIES;
  959. ata_reset_fn_t reset;
  960. int rc;
  961. if (softreset && (!hardreset || (!sata_set_spd_needed(ap) &&
  962. !(ehc->i.action & ATA_EH_HARDRESET))))
  963. reset = softreset;
  964. else
  965. reset = hardreset;
  966. retry:
  967. ata_port_printk(ap, KERN_INFO, "%s resetting port\n",
  968. reset == softreset ? "soft" : "hard");
  969. /* reset */
  970. ata_eh_about_to_do(ap, ATA_EH_RESET_MASK);
  971. ehc->i.flags |= ATA_EHI_DID_RESET;
  972. rc = ata_do_reset(ap, reset, classes);
  973. if (rc && --tries) {
  974. ata_port_printk(ap, KERN_WARNING,
  975. "%sreset failed, retrying in 5 secs\n",
  976. reset == softreset ? "soft" : "hard");
  977. ssleep(5);
  978. if (reset == hardreset)
  979. sata_down_spd_limit(ap);
  980. if (hardreset)
  981. reset = hardreset;
  982. goto retry;
  983. }
  984. if (rc == 0) {
  985. if (postreset)
  986. postreset(ap, classes);
  987. /* reset successful, schedule revalidation */
  988. ehc->i.dev = NULL;
  989. ehc->i.action &= ~ATA_EH_RESET_MASK;
  990. ehc->i.action |= ATA_EH_REVALIDATE;
  991. }
  992. return rc;
  993. }
  994. static int ata_eh_revalidate(struct ata_port *ap,
  995. struct ata_device **r_failed_dev)
  996. {
  997. struct ata_eh_context *ehc = &ap->eh_context;
  998. struct ata_device *dev;
  999. int i, rc = 0;
  1000. DPRINTK("ENTER\n");
  1001. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1002. dev = &ap->device[i];
  1003. if (ehc->i.action & ATA_EH_REVALIDATE && ata_dev_enabled(dev) &&
  1004. (!ehc->i.dev || ehc->i.dev == dev)) {
  1005. if (ata_port_offline(ap)) {
  1006. rc = -EIO;
  1007. break;
  1008. }
  1009. ata_eh_about_to_do(ap, ATA_EH_REVALIDATE);
  1010. rc = ata_dev_revalidate(dev,
  1011. ehc->i.flags & ATA_EHI_DID_RESET);
  1012. if (rc)
  1013. break;
  1014. ehc->i.action &= ~ATA_EH_REVALIDATE;
  1015. }
  1016. }
  1017. if (rc)
  1018. *r_failed_dev = dev;
  1019. DPRINTK("EXIT\n");
  1020. return rc;
  1021. }
  1022. static int ata_port_nr_enabled(struct ata_port *ap)
  1023. {
  1024. int i, cnt = 0;
  1025. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1026. if (ata_dev_enabled(&ap->device[i]))
  1027. cnt++;
  1028. return cnt;
  1029. }
  1030. /**
  1031. * ata_eh_recover - recover host port after error
  1032. * @ap: host port to recover
  1033. * @softreset: softreset method (can be NULL)
  1034. * @hardreset: hardreset method (can be NULL)
  1035. * @postreset: postreset method (can be NULL)
  1036. *
  1037. * This is the alpha and omega, eum and yang, heart and soul of
  1038. * libata exception handling. On entry, actions required to
  1039. * recover each devices are recorded in eh_context. This
  1040. * function executes all the operations with appropriate retrials
  1041. * and fallbacks to resurrect failed devices.
  1042. *
  1043. * LOCKING:
  1044. * Kernel thread context (may sleep).
  1045. *
  1046. * RETURNS:
  1047. * 0 on success, -errno on failure.
  1048. */
  1049. static int ata_eh_recover(struct ata_port *ap, ata_reset_fn_t softreset,
  1050. ata_reset_fn_t hardreset,
  1051. ata_postreset_fn_t postreset)
  1052. {
  1053. struct ata_eh_context *ehc = &ap->eh_context;
  1054. struct ata_device *dev;
  1055. int down_xfermask, i, rc;
  1056. DPRINTK("ENTER\n");
  1057. /* prep for recovery */
  1058. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1059. dev = &ap->device[i];
  1060. ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
  1061. }
  1062. retry:
  1063. down_xfermask = 0;
  1064. rc = 0;
  1065. /* skip EH if possible. */
  1066. if (!ata_port_nr_enabled(ap) && !(ap->flags & ATA_FLAG_FROZEN))
  1067. ehc->i.action = 0;
  1068. /* reset */
  1069. if (ehc->i.action & ATA_EH_RESET_MASK) {
  1070. ata_eh_freeze_port(ap);
  1071. rc = ata_eh_reset(ap, softreset, hardreset, postreset);
  1072. if (rc) {
  1073. ata_port_printk(ap, KERN_ERR,
  1074. "reset failed, giving up\n");
  1075. goto out;
  1076. }
  1077. ata_eh_thaw_port(ap);
  1078. }
  1079. /* revalidate existing devices */
  1080. rc = ata_eh_revalidate(ap, &dev);
  1081. if (rc)
  1082. goto dev_fail;
  1083. /* configure transfer mode if the port has been reset */
  1084. if (ehc->i.flags & ATA_EHI_DID_RESET) {
  1085. rc = ata_set_mode(ap, &dev);
  1086. if (rc) {
  1087. down_xfermask = 1;
  1088. goto dev_fail;
  1089. }
  1090. }
  1091. goto out;
  1092. dev_fail:
  1093. switch (rc) {
  1094. case -ENODEV:
  1095. case -EINVAL:
  1096. ehc->tries[dev->devno] = 0;
  1097. break;
  1098. case -EIO:
  1099. sata_down_spd_limit(ap);
  1100. default:
  1101. ehc->tries[dev->devno]--;
  1102. if (down_xfermask &&
  1103. ata_down_xfermask_limit(dev, ehc->tries[dev->devno] == 1))
  1104. ehc->tries[dev->devno] = 0;
  1105. }
  1106. /* disable device if it has used up all its chances */
  1107. if (ata_dev_enabled(dev) && !ehc->tries[dev->devno])
  1108. ata_dev_disable(dev);
  1109. /* soft didn't work? be haaaaard */
  1110. if (ehc->i.flags & ATA_EHI_DID_RESET)
  1111. ehc->i.action |= ATA_EH_HARDRESET;
  1112. else
  1113. ehc->i.action |= ATA_EH_SOFTRESET;
  1114. if (ata_port_nr_enabled(ap)) {
  1115. ata_port_printk(ap, KERN_WARNING, "failed to recover some "
  1116. "devices, retrying in 5 secs\n");
  1117. ssleep(5);
  1118. } else {
  1119. /* no device left, repeat fast */
  1120. msleep(500);
  1121. }
  1122. goto retry;
  1123. out:
  1124. if (rc) {
  1125. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1126. ata_dev_disable(&ap->device[i]);
  1127. }
  1128. DPRINTK("EXIT, rc=%d\n", rc);
  1129. return rc;
  1130. }
  1131. /**
  1132. * ata_eh_finish - finish up EH
  1133. * @ap: host port to finish EH for
  1134. *
  1135. * Recovery is complete. Clean up EH states and retry or finish
  1136. * failed qcs.
  1137. *
  1138. * LOCKING:
  1139. * None.
  1140. */
  1141. static void ata_eh_finish(struct ata_port *ap)
  1142. {
  1143. int tag;
  1144. /* retry or finish qcs */
  1145. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  1146. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  1147. if (!(qc->flags & ATA_QCFLAG_FAILED))
  1148. continue;
  1149. if (qc->err_mask) {
  1150. /* FIXME: Once EH migration is complete,
  1151. * generate sense data in this function,
  1152. * considering both err_mask and tf.
  1153. */
  1154. if (qc->err_mask & AC_ERR_INVALID)
  1155. ata_eh_qc_complete(qc);
  1156. else
  1157. ata_eh_qc_retry(qc);
  1158. } else {
  1159. if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
  1160. ata_eh_qc_complete(qc);
  1161. } else {
  1162. /* feed zero TF to sense generation */
  1163. memset(&qc->result_tf, 0, sizeof(qc->result_tf));
  1164. ata_eh_qc_retry(qc);
  1165. }
  1166. }
  1167. }
  1168. }
  1169. /**
  1170. * ata_do_eh - do standard error handling
  1171. * @ap: host port to handle error for
  1172. * @softreset: softreset method (can be NULL)
  1173. * @hardreset: hardreset method (can be NULL)
  1174. * @postreset: postreset method (can be NULL)
  1175. *
  1176. * Perform standard error handling sequence.
  1177. *
  1178. * LOCKING:
  1179. * Kernel thread context (may sleep).
  1180. */
  1181. void ata_do_eh(struct ata_port *ap, ata_reset_fn_t softreset,
  1182. ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
  1183. {
  1184. ata_eh_autopsy(ap);
  1185. ata_eh_report(ap);
  1186. ata_eh_recover(ap, softreset, hardreset, postreset);
  1187. ata_eh_finish(ap);
  1188. }