libata-eh.c 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908
  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 "scsi_transport_api.h"
  42. #include <linux/libata.h>
  43. #include "libata.h"
  44. static void __ata_port_freeze(struct ata_port *ap);
  45. static void ata_eh_finish(struct ata_port *ap);
  46. static void ata_ering_record(struct ata_ering *ering, int is_io,
  47. unsigned int err_mask)
  48. {
  49. struct ata_ering_entry *ent;
  50. WARN_ON(!err_mask);
  51. ering->cursor++;
  52. ering->cursor %= ATA_ERING_SIZE;
  53. ent = &ering->ring[ering->cursor];
  54. ent->is_io = is_io;
  55. ent->err_mask = err_mask;
  56. ent->timestamp = get_jiffies_64();
  57. }
  58. static struct ata_ering_entry * ata_ering_top(struct ata_ering *ering)
  59. {
  60. struct ata_ering_entry *ent = &ering->ring[ering->cursor];
  61. if (!ent->err_mask)
  62. return NULL;
  63. return ent;
  64. }
  65. static int ata_ering_map(struct ata_ering *ering,
  66. int (*map_fn)(struct ata_ering_entry *, void *),
  67. void *arg)
  68. {
  69. int idx, rc = 0;
  70. struct ata_ering_entry *ent;
  71. idx = ering->cursor;
  72. do {
  73. ent = &ering->ring[idx];
  74. if (!ent->err_mask)
  75. break;
  76. rc = map_fn(ent, arg);
  77. if (rc)
  78. break;
  79. idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
  80. } while (idx != ering->cursor);
  81. return rc;
  82. }
  83. /**
  84. * ata_scsi_timed_out - SCSI layer time out callback
  85. * @cmd: timed out SCSI command
  86. *
  87. * Handles SCSI layer timeout. We race with normal completion of
  88. * the qc for @cmd. If the qc is already gone, we lose and let
  89. * the scsi command finish (EH_HANDLED). Otherwise, the qc has
  90. * timed out and EH should be invoked. Prevent ata_qc_complete()
  91. * from finishing it by setting EH_SCHEDULED and return
  92. * EH_NOT_HANDLED.
  93. *
  94. * TODO: kill this function once old EH is gone.
  95. *
  96. * LOCKING:
  97. * Called from timer context
  98. *
  99. * RETURNS:
  100. * EH_HANDLED or EH_NOT_HANDLED
  101. */
  102. enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
  103. {
  104. struct Scsi_Host *host = cmd->device->host;
  105. struct ata_port *ap = ata_shost_to_port(host);
  106. unsigned long flags;
  107. struct ata_queued_cmd *qc;
  108. enum scsi_eh_timer_return ret;
  109. DPRINTK("ENTER\n");
  110. if (ap->ops->error_handler) {
  111. ret = EH_NOT_HANDLED;
  112. goto out;
  113. }
  114. ret = EH_HANDLED;
  115. spin_lock_irqsave(&ap->host_set->lock, flags);
  116. qc = ata_qc_from_tag(ap, ap->active_tag);
  117. if (qc) {
  118. WARN_ON(qc->scsicmd != cmd);
  119. qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
  120. qc->err_mask |= AC_ERR_TIMEOUT;
  121. ret = EH_NOT_HANDLED;
  122. }
  123. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  124. out:
  125. DPRINTK("EXIT, ret=%d\n", ret);
  126. return ret;
  127. }
  128. /**
  129. * ata_scsi_error - SCSI layer error handler callback
  130. * @host: SCSI host on which error occurred
  131. *
  132. * Handles SCSI-layer-thrown error events.
  133. *
  134. * LOCKING:
  135. * Inherited from SCSI layer (none, can sleep)
  136. *
  137. * RETURNS:
  138. * Zero.
  139. */
  140. void ata_scsi_error(struct Scsi_Host *host)
  141. {
  142. struct ata_port *ap = ata_shost_to_port(host);
  143. spinlock_t *hs_lock = &ap->host_set->lock;
  144. int i, repeat_cnt = ATA_EH_MAX_REPEAT;
  145. unsigned long flags;
  146. DPRINTK("ENTER\n");
  147. /* synchronize with port task */
  148. ata_port_flush_task(ap);
  149. /* synchronize with host_set lock and sort out timeouts */
  150. /* For new EH, all qcs are finished in one of three ways -
  151. * normal completion, error completion, and SCSI timeout.
  152. * Both cmpletions can race against SCSI timeout. When normal
  153. * completion wins, the qc never reaches EH. When error
  154. * completion wins, the qc has ATA_QCFLAG_FAILED set.
  155. *
  156. * When SCSI timeout wins, things are a bit more complex.
  157. * Normal or error completion can occur after the timeout but
  158. * before this point. In such cases, both types of
  159. * completions are honored. A scmd is determined to have
  160. * timed out iff its associated qc is active and not failed.
  161. */
  162. if (ap->ops->error_handler) {
  163. struct scsi_cmnd *scmd, *tmp;
  164. int nr_timedout = 0;
  165. spin_lock_irqsave(hs_lock, flags);
  166. list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {
  167. struct ata_queued_cmd *qc;
  168. for (i = 0; i < ATA_MAX_QUEUE; i++) {
  169. qc = __ata_qc_from_tag(ap, i);
  170. if (qc->flags & ATA_QCFLAG_ACTIVE &&
  171. qc->scsicmd == scmd)
  172. break;
  173. }
  174. if (i < ATA_MAX_QUEUE) {
  175. /* the scmd has an associated qc */
  176. if (!(qc->flags & ATA_QCFLAG_FAILED)) {
  177. /* which hasn't failed yet, timeout */
  178. qc->err_mask |= AC_ERR_TIMEOUT;
  179. qc->flags |= ATA_QCFLAG_FAILED;
  180. nr_timedout++;
  181. }
  182. } else {
  183. /* Normal completion occurred after
  184. * SCSI timeout but before this point.
  185. * Successfully complete it.
  186. */
  187. scmd->retries = scmd->allowed;
  188. scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
  189. }
  190. }
  191. /* If we have timed out qcs. They belong to EH from
  192. * this point but the state of the controller is
  193. * unknown. Freeze the port to make sure the IRQ
  194. * handler doesn't diddle with those qcs. This must
  195. * be done atomically w.r.t. setting QCFLAG_FAILED.
  196. */
  197. if (nr_timedout)
  198. __ata_port_freeze(ap);
  199. spin_unlock_irqrestore(hs_lock, flags);
  200. } else
  201. spin_unlock_wait(hs_lock);
  202. repeat:
  203. /* invoke error handler */
  204. if (ap->ops->error_handler) {
  205. /* fetch & clear EH info */
  206. spin_lock_irqsave(hs_lock, flags);
  207. memset(&ap->eh_context, 0, sizeof(ap->eh_context));
  208. ap->eh_context.i = ap->eh_info;
  209. memset(&ap->eh_info, 0, sizeof(ap->eh_info));
  210. ap->flags |= ATA_FLAG_EH_IN_PROGRESS;
  211. ap->flags &= ~ATA_FLAG_EH_PENDING;
  212. spin_unlock_irqrestore(hs_lock, flags);
  213. /* invoke EH. if unloading, just finish failed qcs */
  214. if (!(ap->flags & ATA_FLAG_UNLOADING))
  215. ap->ops->error_handler(ap);
  216. else
  217. ata_eh_finish(ap);
  218. /* Exception might have happend after ->error_handler
  219. * recovered the port but before this point. Repeat
  220. * EH in such case.
  221. */
  222. spin_lock_irqsave(hs_lock, flags);
  223. if (ap->flags & ATA_FLAG_EH_PENDING) {
  224. if (--repeat_cnt) {
  225. ata_port_printk(ap, KERN_INFO,
  226. "EH pending after completion, "
  227. "repeating EH (cnt=%d)\n", repeat_cnt);
  228. spin_unlock_irqrestore(hs_lock, flags);
  229. goto repeat;
  230. }
  231. ata_port_printk(ap, KERN_ERR, "EH pending after %d "
  232. "tries, giving up\n", ATA_EH_MAX_REPEAT);
  233. }
  234. /* this run is complete, make sure EH info is clear */
  235. memset(&ap->eh_info, 0, sizeof(ap->eh_info));
  236. /* Clear host_eh_scheduled while holding hs_lock such
  237. * that if exception occurs after this point but
  238. * before EH completion, SCSI midlayer will
  239. * re-initiate EH.
  240. */
  241. host->host_eh_scheduled = 0;
  242. spin_unlock_irqrestore(hs_lock, flags);
  243. } else {
  244. WARN_ON(ata_qc_from_tag(ap, ap->active_tag) == NULL);
  245. ap->ops->eng_timeout(ap);
  246. }
  247. /* finish or retry handled scmd's and clean up */
  248. WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
  249. scsi_eh_flush_done_q(&ap->eh_done_q);
  250. /* clean up */
  251. spin_lock_irqsave(hs_lock, flags);
  252. if (ap->flags & ATA_FLAG_LOADING) {
  253. ap->flags &= ~ATA_FLAG_LOADING;
  254. } else {
  255. if (ap->flags & ATA_FLAG_SCSI_HOTPLUG)
  256. queue_work(ata_aux_wq, &ap->hotplug_task);
  257. if (ap->flags & ATA_FLAG_RECOVERED)
  258. ata_port_printk(ap, KERN_INFO, "EH complete\n");
  259. }
  260. ap->flags &= ~(ATA_FLAG_SCSI_HOTPLUG | ATA_FLAG_RECOVERED);
  261. /* tell wait_eh that we're done */
  262. ap->flags &= ~ATA_FLAG_EH_IN_PROGRESS;
  263. wake_up_all(&ap->eh_wait_q);
  264. spin_unlock_irqrestore(hs_lock, flags);
  265. DPRINTK("EXIT\n");
  266. }
  267. /**
  268. * ata_port_wait_eh - Wait for the currently pending EH to complete
  269. * @ap: Port to wait EH for
  270. *
  271. * Wait until the currently pending EH is complete.
  272. *
  273. * LOCKING:
  274. * Kernel thread context (may sleep).
  275. */
  276. void ata_port_wait_eh(struct ata_port *ap)
  277. {
  278. unsigned long flags;
  279. DEFINE_WAIT(wait);
  280. retry:
  281. spin_lock_irqsave(&ap->host_set->lock, flags);
  282. while (ap->flags & (ATA_FLAG_EH_PENDING | ATA_FLAG_EH_IN_PROGRESS)) {
  283. prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
  284. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  285. schedule();
  286. spin_lock_irqsave(&ap->host_set->lock, flags);
  287. }
  288. finish_wait(&ap->eh_wait_q, &wait);
  289. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  290. /* make sure SCSI EH is complete */
  291. if (scsi_host_in_recovery(ap->host)) {
  292. msleep(10);
  293. goto retry;
  294. }
  295. }
  296. /**
  297. * ata_qc_timeout - Handle timeout of queued command
  298. * @qc: Command that timed out
  299. *
  300. * Some part of the kernel (currently, only the SCSI layer)
  301. * has noticed that the active command on port @ap has not
  302. * completed after a specified length of time. Handle this
  303. * condition by disabling DMA (if necessary) and completing
  304. * transactions, with error if necessary.
  305. *
  306. * This also handles the case of the "lost interrupt", where
  307. * for some reason (possibly hardware bug, possibly driver bug)
  308. * an interrupt was not delivered to the driver, even though the
  309. * transaction completed successfully.
  310. *
  311. * TODO: kill this function once old EH is gone.
  312. *
  313. * LOCKING:
  314. * Inherited from SCSI layer (none, can sleep)
  315. */
  316. static void ata_qc_timeout(struct ata_queued_cmd *qc)
  317. {
  318. struct ata_port *ap = qc->ap;
  319. struct ata_host_set *host_set = ap->host_set;
  320. u8 host_stat = 0, drv_stat;
  321. unsigned long flags;
  322. DPRINTK("ENTER\n");
  323. ap->hsm_task_state = HSM_ST_IDLE;
  324. spin_lock_irqsave(&host_set->lock, flags);
  325. switch (qc->tf.protocol) {
  326. case ATA_PROT_DMA:
  327. case ATA_PROT_ATAPI_DMA:
  328. host_stat = ap->ops->bmdma_status(ap);
  329. /* before we do anything else, clear DMA-Start bit */
  330. ap->ops->bmdma_stop(qc);
  331. /* fall through */
  332. default:
  333. ata_altstatus(ap);
  334. drv_stat = ata_chk_status(ap);
  335. /* ack bmdma irq events */
  336. ap->ops->irq_clear(ap);
  337. ata_dev_printk(qc->dev, KERN_ERR, "command 0x%x timeout, "
  338. "stat 0x%x host_stat 0x%x\n",
  339. qc->tf.command, drv_stat, host_stat);
  340. /* complete taskfile transaction */
  341. qc->err_mask |= AC_ERR_TIMEOUT;
  342. break;
  343. }
  344. spin_unlock_irqrestore(&host_set->lock, flags);
  345. ata_eh_qc_complete(qc);
  346. DPRINTK("EXIT\n");
  347. }
  348. /**
  349. * ata_eng_timeout - Handle timeout of queued command
  350. * @ap: Port on which timed-out command is active
  351. *
  352. * Some part of the kernel (currently, only the SCSI layer)
  353. * has noticed that the active command on port @ap has not
  354. * completed after a specified length of time. Handle this
  355. * condition by disabling DMA (if necessary) and completing
  356. * transactions, with error if necessary.
  357. *
  358. * This also handles the case of the "lost interrupt", where
  359. * for some reason (possibly hardware bug, possibly driver bug)
  360. * an interrupt was not delivered to the driver, even though the
  361. * transaction completed successfully.
  362. *
  363. * TODO: kill this function once old EH is gone.
  364. *
  365. * LOCKING:
  366. * Inherited from SCSI layer (none, can sleep)
  367. */
  368. void ata_eng_timeout(struct ata_port *ap)
  369. {
  370. DPRINTK("ENTER\n");
  371. ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
  372. DPRINTK("EXIT\n");
  373. }
  374. /**
  375. * ata_qc_schedule_eh - schedule qc for error handling
  376. * @qc: command to schedule error handling for
  377. *
  378. * Schedule error handling for @qc. EH will kick in as soon as
  379. * other commands are drained.
  380. *
  381. * LOCKING:
  382. * spin_lock_irqsave(host_set lock)
  383. */
  384. void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
  385. {
  386. struct ata_port *ap = qc->ap;
  387. WARN_ON(!ap->ops->error_handler);
  388. qc->flags |= ATA_QCFLAG_FAILED;
  389. qc->ap->flags |= ATA_FLAG_EH_PENDING;
  390. /* The following will fail if timeout has already expired.
  391. * ata_scsi_error() takes care of such scmds on EH entry.
  392. * Note that ATA_QCFLAG_FAILED is unconditionally set after
  393. * this function completes.
  394. */
  395. scsi_req_abort_cmd(qc->scsicmd);
  396. }
  397. /**
  398. * ata_port_schedule_eh - schedule error handling without a qc
  399. * @ap: ATA port to schedule EH for
  400. *
  401. * Schedule error handling for @ap. EH will kick in as soon as
  402. * all commands are drained.
  403. *
  404. * LOCKING:
  405. * spin_lock_irqsave(host_set lock)
  406. */
  407. void ata_port_schedule_eh(struct ata_port *ap)
  408. {
  409. WARN_ON(!ap->ops->error_handler);
  410. ap->flags |= ATA_FLAG_EH_PENDING;
  411. scsi_schedule_eh(ap->host);
  412. DPRINTK("port EH scheduled\n");
  413. }
  414. /**
  415. * ata_port_abort - abort all qc's on the port
  416. * @ap: ATA port to abort qc's for
  417. *
  418. * Abort all active qc's of @ap and schedule EH.
  419. *
  420. * LOCKING:
  421. * spin_lock_irqsave(host_set lock)
  422. *
  423. * RETURNS:
  424. * Number of aborted qc's.
  425. */
  426. int ata_port_abort(struct ata_port *ap)
  427. {
  428. int tag, nr_aborted = 0;
  429. WARN_ON(!ap->ops->error_handler);
  430. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  431. struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
  432. if (qc) {
  433. qc->flags |= ATA_QCFLAG_FAILED;
  434. ata_qc_complete(qc);
  435. nr_aborted++;
  436. }
  437. }
  438. if (!nr_aborted)
  439. ata_port_schedule_eh(ap);
  440. return nr_aborted;
  441. }
  442. /**
  443. * __ata_port_freeze - freeze port
  444. * @ap: ATA port to freeze
  445. *
  446. * This function is called when HSM violation or some other
  447. * condition disrupts normal operation of the port. Frozen port
  448. * is not allowed to perform any operation until the port is
  449. * thawed, which usually follows a successful reset.
  450. *
  451. * ap->ops->freeze() callback can be used for freezing the port
  452. * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
  453. * port cannot be frozen hardware-wise, the interrupt handler
  454. * must ack and clear interrupts unconditionally while the port
  455. * is frozen.
  456. *
  457. * LOCKING:
  458. * spin_lock_irqsave(host_set lock)
  459. */
  460. static void __ata_port_freeze(struct ata_port *ap)
  461. {
  462. WARN_ON(!ap->ops->error_handler);
  463. if (ap->ops->freeze)
  464. ap->ops->freeze(ap);
  465. ap->flags |= ATA_FLAG_FROZEN;
  466. DPRINTK("ata%u port frozen\n", ap->id);
  467. }
  468. /**
  469. * ata_port_freeze - abort & freeze port
  470. * @ap: ATA port to freeze
  471. *
  472. * Abort and freeze @ap.
  473. *
  474. * LOCKING:
  475. * spin_lock_irqsave(host_set lock)
  476. *
  477. * RETURNS:
  478. * Number of aborted commands.
  479. */
  480. int ata_port_freeze(struct ata_port *ap)
  481. {
  482. int nr_aborted;
  483. WARN_ON(!ap->ops->error_handler);
  484. nr_aborted = ata_port_abort(ap);
  485. __ata_port_freeze(ap);
  486. return nr_aborted;
  487. }
  488. /**
  489. * ata_eh_freeze_port - EH helper to freeze port
  490. * @ap: ATA port to freeze
  491. *
  492. * Freeze @ap.
  493. *
  494. * LOCKING:
  495. * None.
  496. */
  497. void ata_eh_freeze_port(struct ata_port *ap)
  498. {
  499. unsigned long flags;
  500. if (!ap->ops->error_handler)
  501. return;
  502. spin_lock_irqsave(&ap->host_set->lock, flags);
  503. __ata_port_freeze(ap);
  504. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  505. }
  506. /**
  507. * ata_port_thaw_port - EH helper to thaw port
  508. * @ap: ATA port to thaw
  509. *
  510. * Thaw frozen port @ap.
  511. *
  512. * LOCKING:
  513. * None.
  514. */
  515. void ata_eh_thaw_port(struct ata_port *ap)
  516. {
  517. unsigned long flags;
  518. if (!ap->ops->error_handler)
  519. return;
  520. spin_lock_irqsave(&ap->host_set->lock, flags);
  521. ap->flags &= ~ATA_FLAG_FROZEN;
  522. if (ap->ops->thaw)
  523. ap->ops->thaw(ap);
  524. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  525. DPRINTK("ata%u port thawed\n", ap->id);
  526. }
  527. static void ata_eh_scsidone(struct scsi_cmnd *scmd)
  528. {
  529. /* nada */
  530. }
  531. static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
  532. {
  533. struct ata_port *ap = qc->ap;
  534. struct scsi_cmnd *scmd = qc->scsicmd;
  535. unsigned long flags;
  536. spin_lock_irqsave(&ap->host_set->lock, flags);
  537. qc->scsidone = ata_eh_scsidone;
  538. __ata_qc_complete(qc);
  539. WARN_ON(ata_tag_valid(qc->tag));
  540. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  541. scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
  542. }
  543. /**
  544. * ata_eh_qc_complete - Complete an active ATA command from EH
  545. * @qc: Command to complete
  546. *
  547. * Indicate to the mid and upper layers that an ATA command has
  548. * completed. To be used from EH.
  549. */
  550. void ata_eh_qc_complete(struct ata_queued_cmd *qc)
  551. {
  552. struct scsi_cmnd *scmd = qc->scsicmd;
  553. scmd->retries = scmd->allowed;
  554. __ata_eh_qc_complete(qc);
  555. }
  556. /**
  557. * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
  558. * @qc: Command to retry
  559. *
  560. * Indicate to the mid and upper layers that an ATA command
  561. * should be retried. To be used from EH.
  562. *
  563. * SCSI midlayer limits the number of retries to scmd->allowed.
  564. * scmd->retries is decremented for commands which get retried
  565. * due to unrelated failures (qc->err_mask is zero).
  566. */
  567. void ata_eh_qc_retry(struct ata_queued_cmd *qc)
  568. {
  569. struct scsi_cmnd *scmd = qc->scsicmd;
  570. if (!qc->err_mask && scmd->retries)
  571. scmd->retries--;
  572. __ata_eh_qc_complete(qc);
  573. }
  574. /**
  575. * ata_eh_detach_dev - detach ATA device
  576. * @dev: ATA device to detach
  577. *
  578. * Detach @dev.
  579. *
  580. * LOCKING:
  581. * None.
  582. */
  583. static void ata_eh_detach_dev(struct ata_device *dev)
  584. {
  585. struct ata_port *ap = dev->ap;
  586. unsigned long flags;
  587. ata_dev_disable(dev);
  588. spin_lock_irqsave(&ap->host_set->lock, flags);
  589. dev->flags &= ~ATA_DFLAG_DETACH;
  590. if (ata_scsi_offline_dev(dev)) {
  591. dev->flags |= ATA_DFLAG_DETACHED;
  592. ap->flags |= ATA_FLAG_SCSI_HOTPLUG;
  593. }
  594. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  595. }
  596. static void ata_eh_clear_action(struct ata_device *dev,
  597. struct ata_eh_info *ehi, unsigned int action)
  598. {
  599. int i;
  600. if (!dev) {
  601. ehi->action &= ~action;
  602. for (i = 0; i < ATA_MAX_DEVICES; i++)
  603. ehi->dev_action[i] &= ~action;
  604. } else {
  605. /* doesn't make sense for port-wide EH actions */
  606. WARN_ON(!(action & ATA_EH_PERDEV_MASK));
  607. /* break ehi->action into ehi->dev_action */
  608. if (ehi->action & action) {
  609. for (i = 0; i < ATA_MAX_DEVICES; i++)
  610. ehi->dev_action[i] |= ehi->action & action;
  611. ehi->action &= ~action;
  612. }
  613. /* turn off the specified per-dev action */
  614. ehi->dev_action[dev->devno] &= ~action;
  615. }
  616. }
  617. /**
  618. * ata_eh_about_to_do - about to perform eh_action
  619. * @ap: target ATA port
  620. * @dev: target ATA dev for per-dev action (can be NULL)
  621. * @action: action about to be performed
  622. *
  623. * Called just before performing EH actions to clear related bits
  624. * in @ap->eh_info such that eh actions are not unnecessarily
  625. * repeated.
  626. *
  627. * LOCKING:
  628. * None.
  629. */
  630. static void ata_eh_about_to_do(struct ata_port *ap, struct ata_device *dev,
  631. unsigned int action)
  632. {
  633. unsigned long flags;
  634. spin_lock_irqsave(&ap->host_set->lock, flags);
  635. ata_eh_clear_action(dev, &ap->eh_info, action);
  636. ap->flags |= ATA_FLAG_RECOVERED;
  637. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  638. }
  639. /**
  640. * ata_eh_done - EH action complete
  641. * @ap: target ATA port
  642. * @dev: target ATA dev for per-dev action (can be NULL)
  643. * @action: action just completed
  644. *
  645. * Called right after performing EH actions to clear related bits
  646. * in @ap->eh_context.
  647. *
  648. * LOCKING:
  649. * None.
  650. */
  651. static void ata_eh_done(struct ata_port *ap, struct ata_device *dev,
  652. unsigned int action)
  653. {
  654. ata_eh_clear_action(dev, &ap->eh_context.i, action);
  655. }
  656. /**
  657. * ata_err_string - convert err_mask to descriptive string
  658. * @err_mask: error mask to convert to string
  659. *
  660. * Convert @err_mask to descriptive string. Errors are
  661. * prioritized according to severity and only the most severe
  662. * error is reported.
  663. *
  664. * LOCKING:
  665. * None.
  666. *
  667. * RETURNS:
  668. * Descriptive string for @err_mask
  669. */
  670. static const char * ata_err_string(unsigned int err_mask)
  671. {
  672. if (err_mask & AC_ERR_HOST_BUS)
  673. return "host bus error";
  674. if (err_mask & AC_ERR_ATA_BUS)
  675. return "ATA bus error";
  676. if (err_mask & AC_ERR_TIMEOUT)
  677. return "timeout";
  678. if (err_mask & AC_ERR_HSM)
  679. return "HSM violation";
  680. if (err_mask & AC_ERR_SYSTEM)
  681. return "internal error";
  682. if (err_mask & AC_ERR_MEDIA)
  683. return "media error";
  684. if (err_mask & AC_ERR_INVALID)
  685. return "invalid argument";
  686. if (err_mask & AC_ERR_DEV)
  687. return "device error";
  688. return "unknown error";
  689. }
  690. /**
  691. * ata_read_log_page - read a specific log page
  692. * @dev: target device
  693. * @page: page to read
  694. * @buf: buffer to store read page
  695. * @sectors: number of sectors to read
  696. *
  697. * Read log page using READ_LOG_EXT command.
  698. *
  699. * LOCKING:
  700. * Kernel thread context (may sleep).
  701. *
  702. * RETURNS:
  703. * 0 on success, AC_ERR_* mask otherwise.
  704. */
  705. static unsigned int ata_read_log_page(struct ata_device *dev,
  706. u8 page, void *buf, unsigned int sectors)
  707. {
  708. struct ata_taskfile tf;
  709. unsigned int err_mask;
  710. DPRINTK("read log page - page %d\n", page);
  711. ata_tf_init(dev, &tf);
  712. tf.command = ATA_CMD_READ_LOG_EXT;
  713. tf.lbal = page;
  714. tf.nsect = sectors;
  715. tf.hob_nsect = sectors >> 8;
  716. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
  717. tf.protocol = ATA_PROT_PIO;
  718. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
  719. buf, sectors * ATA_SECT_SIZE);
  720. DPRINTK("EXIT, err_mask=%x\n", err_mask);
  721. return err_mask;
  722. }
  723. /**
  724. * ata_eh_read_log_10h - Read log page 10h for NCQ error details
  725. * @dev: Device to read log page 10h from
  726. * @tag: Resulting tag of the failed command
  727. * @tf: Resulting taskfile registers of the failed command
  728. *
  729. * Read log page 10h to obtain NCQ error details and clear error
  730. * condition.
  731. *
  732. * LOCKING:
  733. * Kernel thread context (may sleep).
  734. *
  735. * RETURNS:
  736. * 0 on success, -errno otherwise.
  737. */
  738. static int ata_eh_read_log_10h(struct ata_device *dev,
  739. int *tag, struct ata_taskfile *tf)
  740. {
  741. u8 *buf = dev->ap->sector_buf;
  742. unsigned int err_mask;
  743. u8 csum;
  744. int i;
  745. err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
  746. if (err_mask)
  747. return -EIO;
  748. csum = 0;
  749. for (i = 0; i < ATA_SECT_SIZE; i++)
  750. csum += buf[i];
  751. if (csum)
  752. ata_dev_printk(dev, KERN_WARNING,
  753. "invalid checksum 0x%x on log page 10h\n", csum);
  754. if (buf[0] & 0x80)
  755. return -ENOENT;
  756. *tag = buf[0] & 0x1f;
  757. tf->command = buf[2];
  758. tf->feature = buf[3];
  759. tf->lbal = buf[4];
  760. tf->lbam = buf[5];
  761. tf->lbah = buf[6];
  762. tf->device = buf[7];
  763. tf->hob_lbal = buf[8];
  764. tf->hob_lbam = buf[9];
  765. tf->hob_lbah = buf[10];
  766. tf->nsect = buf[12];
  767. tf->hob_nsect = buf[13];
  768. return 0;
  769. }
  770. /**
  771. * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
  772. * @dev: device to perform REQUEST_SENSE to
  773. * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
  774. *
  775. * Perform ATAPI REQUEST_SENSE after the device reported CHECK
  776. * SENSE. This function is EH helper.
  777. *
  778. * LOCKING:
  779. * Kernel thread context (may sleep).
  780. *
  781. * RETURNS:
  782. * 0 on success, AC_ERR_* mask on failure
  783. */
  784. static unsigned int atapi_eh_request_sense(struct ata_device *dev,
  785. unsigned char *sense_buf)
  786. {
  787. struct ata_port *ap = dev->ap;
  788. struct ata_taskfile tf;
  789. u8 cdb[ATAPI_CDB_LEN];
  790. DPRINTK("ATAPI request sense\n");
  791. ata_tf_init(dev, &tf);
  792. /* FIXME: is this needed? */
  793. memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
  794. /* XXX: why tf_read here? */
  795. ap->ops->tf_read(ap, &tf);
  796. /* fill these in, for the case where they are -not- overwritten */
  797. sense_buf[0] = 0x70;
  798. sense_buf[2] = tf.feature >> 4;
  799. memset(cdb, 0, ATAPI_CDB_LEN);
  800. cdb[0] = REQUEST_SENSE;
  801. cdb[4] = SCSI_SENSE_BUFFERSIZE;
  802. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  803. tf.command = ATA_CMD_PACKET;
  804. /* is it pointless to prefer PIO for "safety reasons"? */
  805. if (ap->flags & ATA_FLAG_PIO_DMA) {
  806. tf.protocol = ATA_PROT_ATAPI_DMA;
  807. tf.feature |= ATAPI_PKT_DMA;
  808. } else {
  809. tf.protocol = ATA_PROT_ATAPI;
  810. tf.lbam = (8 * 1024) & 0xff;
  811. tf.lbah = (8 * 1024) >> 8;
  812. }
  813. return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
  814. sense_buf, SCSI_SENSE_BUFFERSIZE);
  815. }
  816. /**
  817. * ata_eh_analyze_serror - analyze SError for a failed port
  818. * @ap: ATA port to analyze SError for
  819. *
  820. * Analyze SError if available and further determine cause of
  821. * failure.
  822. *
  823. * LOCKING:
  824. * None.
  825. */
  826. static void ata_eh_analyze_serror(struct ata_port *ap)
  827. {
  828. struct ata_eh_context *ehc = &ap->eh_context;
  829. u32 serror = ehc->i.serror;
  830. unsigned int err_mask = 0, action = 0;
  831. if (serror & SERR_PERSISTENT) {
  832. err_mask |= AC_ERR_ATA_BUS;
  833. action |= ATA_EH_HARDRESET;
  834. }
  835. if (serror &
  836. (SERR_DATA_RECOVERED | SERR_COMM_RECOVERED | SERR_DATA)) {
  837. err_mask |= AC_ERR_ATA_BUS;
  838. action |= ATA_EH_SOFTRESET;
  839. }
  840. if (serror & SERR_PROTOCOL) {
  841. err_mask |= AC_ERR_HSM;
  842. action |= ATA_EH_SOFTRESET;
  843. }
  844. if (serror & SERR_INTERNAL) {
  845. err_mask |= AC_ERR_SYSTEM;
  846. action |= ATA_EH_SOFTRESET;
  847. }
  848. if (serror & (SERR_PHYRDY_CHG | SERR_DEV_XCHG))
  849. ata_ehi_hotplugged(&ehc->i);
  850. ehc->i.err_mask |= err_mask;
  851. ehc->i.action |= action;
  852. }
  853. /**
  854. * ata_eh_analyze_ncq_error - analyze NCQ error
  855. * @ap: ATA port to analyze NCQ error for
  856. *
  857. * Read log page 10h, determine the offending qc and acquire
  858. * error status TF. For NCQ device errors, all LLDDs have to do
  859. * is setting AC_ERR_DEV in ehi->err_mask. This function takes
  860. * care of the rest.
  861. *
  862. * LOCKING:
  863. * Kernel thread context (may sleep).
  864. */
  865. static void ata_eh_analyze_ncq_error(struct ata_port *ap)
  866. {
  867. struct ata_eh_context *ehc = &ap->eh_context;
  868. struct ata_device *dev = ap->device;
  869. struct ata_queued_cmd *qc;
  870. struct ata_taskfile tf;
  871. int tag, rc;
  872. /* if frozen, we can't do much */
  873. if (ap->flags & ATA_FLAG_FROZEN)
  874. return;
  875. /* is it NCQ device error? */
  876. if (!ap->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
  877. return;
  878. /* has LLDD analyzed already? */
  879. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  880. qc = __ata_qc_from_tag(ap, tag);
  881. if (!(qc->flags & ATA_QCFLAG_FAILED))
  882. continue;
  883. if (qc->err_mask)
  884. return;
  885. }
  886. /* okay, this error is ours */
  887. rc = ata_eh_read_log_10h(dev, &tag, &tf);
  888. if (rc) {
  889. ata_port_printk(ap, KERN_ERR, "failed to read log page 10h "
  890. "(errno=%d)\n", rc);
  891. return;
  892. }
  893. if (!(ap->sactive & (1 << tag))) {
  894. ata_port_printk(ap, KERN_ERR, "log page 10h reported "
  895. "inactive tag %d\n", tag);
  896. return;
  897. }
  898. /* we've got the perpetrator, condemn it */
  899. qc = __ata_qc_from_tag(ap, tag);
  900. memcpy(&qc->result_tf, &tf, sizeof(tf));
  901. qc->err_mask |= AC_ERR_DEV;
  902. ehc->i.err_mask &= ~AC_ERR_DEV;
  903. }
  904. /**
  905. * ata_eh_analyze_tf - analyze taskfile of a failed qc
  906. * @qc: qc to analyze
  907. * @tf: Taskfile registers to analyze
  908. *
  909. * Analyze taskfile of @qc and further determine cause of
  910. * failure. This function also requests ATAPI sense data if
  911. * avaliable.
  912. *
  913. * LOCKING:
  914. * Kernel thread context (may sleep).
  915. *
  916. * RETURNS:
  917. * Determined recovery action
  918. */
  919. static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
  920. const struct ata_taskfile *tf)
  921. {
  922. unsigned int tmp, action = 0;
  923. u8 stat = tf->command, err = tf->feature;
  924. if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
  925. qc->err_mask |= AC_ERR_HSM;
  926. return ATA_EH_SOFTRESET;
  927. }
  928. if (!(qc->err_mask & AC_ERR_DEV))
  929. return 0;
  930. switch (qc->dev->class) {
  931. case ATA_DEV_ATA:
  932. if (err & ATA_ICRC)
  933. qc->err_mask |= AC_ERR_ATA_BUS;
  934. if (err & ATA_UNC)
  935. qc->err_mask |= AC_ERR_MEDIA;
  936. if (err & ATA_IDNF)
  937. qc->err_mask |= AC_ERR_INVALID;
  938. break;
  939. case ATA_DEV_ATAPI:
  940. tmp = atapi_eh_request_sense(qc->dev,
  941. qc->scsicmd->sense_buffer);
  942. if (!tmp) {
  943. /* ATA_QCFLAG_SENSE_VALID is used to tell
  944. * atapi_qc_complete() that sense data is
  945. * already valid.
  946. *
  947. * TODO: interpret sense data and set
  948. * appropriate err_mask.
  949. */
  950. qc->flags |= ATA_QCFLAG_SENSE_VALID;
  951. } else
  952. qc->err_mask |= tmp;
  953. }
  954. if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
  955. action |= ATA_EH_SOFTRESET;
  956. return action;
  957. }
  958. static int ata_eh_categorize_ering_entry(struct ata_ering_entry *ent)
  959. {
  960. if (ent->err_mask & (AC_ERR_ATA_BUS | AC_ERR_TIMEOUT))
  961. return 1;
  962. if (ent->is_io) {
  963. if (ent->err_mask & AC_ERR_HSM)
  964. return 1;
  965. if ((ent->err_mask &
  966. (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
  967. return 2;
  968. }
  969. return 0;
  970. }
  971. struct speed_down_needed_arg {
  972. u64 since;
  973. int nr_errors[3];
  974. };
  975. static int speed_down_needed_cb(struct ata_ering_entry *ent, void *void_arg)
  976. {
  977. struct speed_down_needed_arg *arg = void_arg;
  978. if (ent->timestamp < arg->since)
  979. return -1;
  980. arg->nr_errors[ata_eh_categorize_ering_entry(ent)]++;
  981. return 0;
  982. }
  983. /**
  984. * ata_eh_speed_down_needed - Determine wheter speed down is necessary
  985. * @dev: Device of interest
  986. *
  987. * This function examines error ring of @dev and determines
  988. * whether speed down is necessary. Speed down is necessary if
  989. * there have been more than 3 of Cat-1 errors or 10 of Cat-2
  990. * errors during last 15 minutes.
  991. *
  992. * Cat-1 errors are ATA_BUS, TIMEOUT for any command and HSM
  993. * violation for known supported commands.
  994. *
  995. * Cat-2 errors are unclassified DEV error for known supported
  996. * command.
  997. *
  998. * LOCKING:
  999. * Inherited from caller.
  1000. *
  1001. * RETURNS:
  1002. * 1 if speed down is necessary, 0 otherwise
  1003. */
  1004. static int ata_eh_speed_down_needed(struct ata_device *dev)
  1005. {
  1006. const u64 interval = 15LLU * 60 * HZ;
  1007. static const int err_limits[3] = { -1, 3, 10 };
  1008. struct speed_down_needed_arg arg;
  1009. struct ata_ering_entry *ent;
  1010. int err_cat;
  1011. u64 j64;
  1012. ent = ata_ering_top(&dev->ering);
  1013. if (!ent)
  1014. return 0;
  1015. err_cat = ata_eh_categorize_ering_entry(ent);
  1016. if (err_cat == 0)
  1017. return 0;
  1018. memset(&arg, 0, sizeof(arg));
  1019. j64 = get_jiffies_64();
  1020. if (j64 >= interval)
  1021. arg.since = j64 - interval;
  1022. else
  1023. arg.since = 0;
  1024. ata_ering_map(&dev->ering, speed_down_needed_cb, &arg);
  1025. return arg.nr_errors[err_cat] > err_limits[err_cat];
  1026. }
  1027. /**
  1028. * ata_eh_speed_down - record error and speed down if necessary
  1029. * @dev: Failed device
  1030. * @is_io: Did the device fail during normal IO?
  1031. * @err_mask: err_mask of the error
  1032. *
  1033. * Record error and examine error history to determine whether
  1034. * adjusting transmission speed is necessary. It also sets
  1035. * transmission limits appropriately if such adjustment is
  1036. * necessary.
  1037. *
  1038. * LOCKING:
  1039. * Kernel thread context (may sleep).
  1040. *
  1041. * RETURNS:
  1042. * 0 on success, -errno otherwise
  1043. */
  1044. static int ata_eh_speed_down(struct ata_device *dev, int is_io,
  1045. unsigned int err_mask)
  1046. {
  1047. if (!err_mask)
  1048. return 0;
  1049. /* record error and determine whether speed down is necessary */
  1050. ata_ering_record(&dev->ering, is_io, err_mask);
  1051. if (!ata_eh_speed_down_needed(dev))
  1052. return 0;
  1053. /* speed down SATA link speed if possible */
  1054. if (sata_down_spd_limit(dev->ap) == 0)
  1055. return ATA_EH_HARDRESET;
  1056. /* lower transfer mode */
  1057. if (ata_down_xfermask_limit(dev, 0) == 0)
  1058. return ATA_EH_SOFTRESET;
  1059. ata_dev_printk(dev, KERN_ERR,
  1060. "speed down requested but no transfer mode left\n");
  1061. return 0;
  1062. }
  1063. /**
  1064. * ata_eh_autopsy - analyze error and determine recovery action
  1065. * @ap: ATA port to perform autopsy on
  1066. *
  1067. * Analyze why @ap failed and determine which recovery action is
  1068. * needed. This function also sets more detailed AC_ERR_* values
  1069. * and fills sense data for ATAPI CHECK SENSE.
  1070. *
  1071. * LOCKING:
  1072. * Kernel thread context (may sleep).
  1073. */
  1074. static void ata_eh_autopsy(struct ata_port *ap)
  1075. {
  1076. struct ata_eh_context *ehc = &ap->eh_context;
  1077. unsigned int action = ehc->i.action;
  1078. struct ata_device *failed_dev = NULL;
  1079. unsigned int all_err_mask = 0;
  1080. int tag, is_io = 0;
  1081. u32 serror;
  1082. int rc;
  1083. DPRINTK("ENTER\n");
  1084. /* obtain and analyze SError */
  1085. rc = sata_scr_read(ap, SCR_ERROR, &serror);
  1086. if (rc == 0) {
  1087. ehc->i.serror |= serror;
  1088. ata_eh_analyze_serror(ap);
  1089. } else if (rc != -EOPNOTSUPP)
  1090. action |= ATA_EH_HARDRESET;
  1091. /* analyze NCQ failure */
  1092. ata_eh_analyze_ncq_error(ap);
  1093. /* any real error trumps AC_ERR_OTHER */
  1094. if (ehc->i.err_mask & ~AC_ERR_OTHER)
  1095. ehc->i.err_mask &= ~AC_ERR_OTHER;
  1096. all_err_mask |= ehc->i.err_mask;
  1097. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  1098. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  1099. if (!(qc->flags & ATA_QCFLAG_FAILED))
  1100. continue;
  1101. /* inherit upper level err_mask */
  1102. qc->err_mask |= ehc->i.err_mask;
  1103. /* analyze TF */
  1104. action |= ata_eh_analyze_tf(qc, &qc->result_tf);
  1105. /* DEV errors are probably spurious in case of ATA_BUS error */
  1106. if (qc->err_mask & AC_ERR_ATA_BUS)
  1107. qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
  1108. AC_ERR_INVALID);
  1109. /* any real error trumps unknown error */
  1110. if (qc->err_mask & ~AC_ERR_OTHER)
  1111. qc->err_mask &= ~AC_ERR_OTHER;
  1112. /* SENSE_VALID trumps dev/unknown error and revalidation */
  1113. if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
  1114. qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
  1115. action &= ~ATA_EH_REVALIDATE;
  1116. }
  1117. /* accumulate error info */
  1118. failed_dev = qc->dev;
  1119. all_err_mask |= qc->err_mask;
  1120. if (qc->flags & ATA_QCFLAG_IO)
  1121. is_io = 1;
  1122. }
  1123. /* enforce default EH actions */
  1124. if (ap->flags & ATA_FLAG_FROZEN ||
  1125. all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
  1126. action |= ATA_EH_SOFTRESET;
  1127. else if (all_err_mask)
  1128. action |= ATA_EH_REVALIDATE;
  1129. /* if we have offending qcs and the associated failed device */
  1130. if (failed_dev) {
  1131. /* speed down */
  1132. action |= ata_eh_speed_down(failed_dev, is_io, all_err_mask);
  1133. /* perform per-dev EH action only on the offending device */
  1134. ehc->i.dev_action[failed_dev->devno] |=
  1135. action & ATA_EH_PERDEV_MASK;
  1136. action &= ~ATA_EH_PERDEV_MASK;
  1137. }
  1138. /* record autopsy result */
  1139. ehc->i.dev = failed_dev;
  1140. ehc->i.action = action;
  1141. DPRINTK("EXIT\n");
  1142. }
  1143. /**
  1144. * ata_eh_report - report error handling to user
  1145. * @ap: ATA port EH is going on
  1146. *
  1147. * Report EH to user.
  1148. *
  1149. * LOCKING:
  1150. * None.
  1151. */
  1152. static void ata_eh_report(struct ata_port *ap)
  1153. {
  1154. struct ata_eh_context *ehc = &ap->eh_context;
  1155. const char *frozen, *desc;
  1156. int tag, nr_failed = 0;
  1157. desc = NULL;
  1158. if (ehc->i.desc[0] != '\0')
  1159. desc = ehc->i.desc;
  1160. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  1161. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  1162. if (!(qc->flags & ATA_QCFLAG_FAILED))
  1163. continue;
  1164. if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
  1165. continue;
  1166. nr_failed++;
  1167. }
  1168. if (!nr_failed && !ehc->i.err_mask)
  1169. return;
  1170. frozen = "";
  1171. if (ap->flags & ATA_FLAG_FROZEN)
  1172. frozen = " frozen";
  1173. if (ehc->i.dev) {
  1174. ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
  1175. "SAct 0x%x SErr 0x%x action 0x%x%s\n",
  1176. ehc->i.err_mask, ap->sactive, ehc->i.serror,
  1177. ehc->i.action, frozen);
  1178. if (desc)
  1179. ata_dev_printk(ehc->i.dev, KERN_ERR, "(%s)\n", desc);
  1180. } else {
  1181. ata_port_printk(ap, KERN_ERR, "exception Emask 0x%x "
  1182. "SAct 0x%x SErr 0x%x action 0x%x%s\n",
  1183. ehc->i.err_mask, ap->sactive, ehc->i.serror,
  1184. ehc->i.action, frozen);
  1185. if (desc)
  1186. ata_port_printk(ap, KERN_ERR, "(%s)\n", desc);
  1187. }
  1188. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  1189. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  1190. if (!(qc->flags & ATA_QCFLAG_FAILED) || !qc->err_mask)
  1191. continue;
  1192. ata_dev_printk(qc->dev, KERN_ERR, "tag %d cmd 0x%x "
  1193. "Emask 0x%x stat 0x%x err 0x%x (%s)\n",
  1194. qc->tag, qc->tf.command, qc->err_mask,
  1195. qc->result_tf.command, qc->result_tf.feature,
  1196. ata_err_string(qc->err_mask));
  1197. }
  1198. }
  1199. static int ata_do_reset(struct ata_port *ap, ata_reset_fn_t reset,
  1200. unsigned int *classes)
  1201. {
  1202. int i, rc;
  1203. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1204. classes[i] = ATA_DEV_UNKNOWN;
  1205. rc = reset(ap, classes);
  1206. if (rc)
  1207. return rc;
  1208. /* If any class isn't ATA_DEV_UNKNOWN, consider classification
  1209. * is complete and convert all ATA_DEV_UNKNOWN to
  1210. * ATA_DEV_NONE.
  1211. */
  1212. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1213. if (classes[i] != ATA_DEV_UNKNOWN)
  1214. break;
  1215. if (i < ATA_MAX_DEVICES)
  1216. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1217. if (classes[i] == ATA_DEV_UNKNOWN)
  1218. classes[i] = ATA_DEV_NONE;
  1219. return 0;
  1220. }
  1221. static int ata_eh_followup_srst_needed(int rc, int classify,
  1222. const unsigned int *classes)
  1223. {
  1224. if (rc == -EAGAIN)
  1225. return 1;
  1226. if (rc != 0)
  1227. return 0;
  1228. if (classify && classes[0] == ATA_DEV_UNKNOWN)
  1229. return 1;
  1230. return 0;
  1231. }
  1232. static int ata_eh_reset(struct ata_port *ap, int classify,
  1233. ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
  1234. ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
  1235. {
  1236. struct ata_eh_context *ehc = &ap->eh_context;
  1237. unsigned int *classes = ehc->classes;
  1238. int tries = ATA_EH_RESET_TRIES;
  1239. int verbose = !(ap->flags & ATA_FLAG_LOADING);
  1240. unsigned int action;
  1241. ata_reset_fn_t reset;
  1242. int i, did_followup_srst, rc;
  1243. /* Determine which reset to use and record in ehc->i.action.
  1244. * prereset() may examine and modify it.
  1245. */
  1246. action = ehc->i.action;
  1247. ehc->i.action &= ~ATA_EH_RESET_MASK;
  1248. if (softreset && (!hardreset || (!sata_set_spd_needed(ap) &&
  1249. !(action & ATA_EH_HARDRESET))))
  1250. ehc->i.action |= ATA_EH_SOFTRESET;
  1251. else
  1252. ehc->i.action |= ATA_EH_HARDRESET;
  1253. if (prereset) {
  1254. rc = prereset(ap);
  1255. if (rc) {
  1256. ata_port_printk(ap, KERN_ERR,
  1257. "prereset failed (errno=%d)\n", rc);
  1258. return rc;
  1259. }
  1260. }
  1261. /* prereset() might have modified ehc->i.action */
  1262. if (ehc->i.action & ATA_EH_HARDRESET)
  1263. reset = hardreset;
  1264. else if (ehc->i.action & ATA_EH_SOFTRESET)
  1265. reset = softreset;
  1266. else {
  1267. /* prereset told us not to reset, bang classes and return */
  1268. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1269. classes[i] = ATA_DEV_NONE;
  1270. return 0;
  1271. }
  1272. /* did prereset() screw up? if so, fix up to avoid oopsing */
  1273. if (!reset) {
  1274. ata_port_printk(ap, KERN_ERR, "BUG: prereset() requested "
  1275. "invalid reset type\n");
  1276. if (softreset)
  1277. reset = softreset;
  1278. else
  1279. reset = hardreset;
  1280. }
  1281. retry:
  1282. /* shut up during boot probing */
  1283. if (verbose)
  1284. ata_port_printk(ap, KERN_INFO, "%s resetting port\n",
  1285. reset == softreset ? "soft" : "hard");
  1286. /* reset */
  1287. ata_eh_about_to_do(ap, NULL, ATA_EH_RESET_MASK);
  1288. ehc->i.flags |= ATA_EHI_DID_RESET;
  1289. rc = ata_do_reset(ap, reset, classes);
  1290. did_followup_srst = 0;
  1291. if (reset == hardreset &&
  1292. ata_eh_followup_srst_needed(rc, classify, classes)) {
  1293. /* okay, let's do follow-up softreset */
  1294. did_followup_srst = 1;
  1295. reset = softreset;
  1296. if (!reset) {
  1297. ata_port_printk(ap, KERN_ERR,
  1298. "follow-up softreset required "
  1299. "but no softreset avaliable\n");
  1300. return -EINVAL;
  1301. }
  1302. ata_eh_about_to_do(ap, NULL, ATA_EH_RESET_MASK);
  1303. rc = ata_do_reset(ap, reset, classes);
  1304. if (rc == 0 && classify &&
  1305. classes[0] == ATA_DEV_UNKNOWN) {
  1306. ata_port_printk(ap, KERN_ERR,
  1307. "classification failed\n");
  1308. return -EINVAL;
  1309. }
  1310. }
  1311. if (rc && --tries) {
  1312. const char *type;
  1313. if (reset == softreset) {
  1314. if (did_followup_srst)
  1315. type = "follow-up soft";
  1316. else
  1317. type = "soft";
  1318. } else
  1319. type = "hard";
  1320. ata_port_printk(ap, KERN_WARNING,
  1321. "%sreset failed, retrying in 5 secs\n", type);
  1322. ssleep(5);
  1323. if (reset == hardreset)
  1324. sata_down_spd_limit(ap);
  1325. if (hardreset)
  1326. reset = hardreset;
  1327. goto retry;
  1328. }
  1329. if (rc == 0) {
  1330. /* After the reset, the device state is PIO 0 and the
  1331. * controller state is undefined. Record the mode.
  1332. */
  1333. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1334. ap->device[i].pio_mode = XFER_PIO_0;
  1335. if (postreset)
  1336. postreset(ap, classes);
  1337. /* reset successful, schedule revalidation */
  1338. ata_eh_done(ap, NULL, ATA_EH_RESET_MASK);
  1339. ehc->i.action |= ATA_EH_REVALIDATE;
  1340. }
  1341. return rc;
  1342. }
  1343. static int ata_eh_revalidate_and_attach(struct ata_port *ap,
  1344. struct ata_device **r_failed_dev)
  1345. {
  1346. struct ata_eh_context *ehc = &ap->eh_context;
  1347. struct ata_device *dev;
  1348. unsigned long flags;
  1349. int i, rc = 0;
  1350. DPRINTK("ENTER\n");
  1351. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1352. unsigned int action;
  1353. dev = &ap->device[i];
  1354. action = ehc->i.action | ehc->i.dev_action[dev->devno];
  1355. if (action & ATA_EH_REVALIDATE && ata_dev_enabled(dev)) {
  1356. if (ata_port_offline(ap)) {
  1357. rc = -EIO;
  1358. break;
  1359. }
  1360. ata_eh_about_to_do(ap, dev, ATA_EH_REVALIDATE);
  1361. rc = ata_dev_revalidate(dev,
  1362. ehc->i.flags & ATA_EHI_DID_RESET);
  1363. if (rc)
  1364. break;
  1365. ata_eh_done(ap, dev, ATA_EH_REVALIDATE);
  1366. /* schedule the scsi_rescan_device() here */
  1367. queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
  1368. } else if (dev->class == ATA_DEV_UNKNOWN &&
  1369. ehc->tries[dev->devno] &&
  1370. ata_class_enabled(ehc->classes[dev->devno])) {
  1371. dev->class = ehc->classes[dev->devno];
  1372. rc = ata_dev_read_id(dev, &dev->class, 1, dev->id);
  1373. if (rc == 0)
  1374. rc = ata_dev_configure(dev, 1);
  1375. if (rc) {
  1376. dev->class = ATA_DEV_UNKNOWN;
  1377. break;
  1378. }
  1379. spin_lock_irqsave(&ap->host_set->lock, flags);
  1380. ap->flags |= ATA_FLAG_SCSI_HOTPLUG;
  1381. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  1382. }
  1383. }
  1384. if (rc)
  1385. *r_failed_dev = dev;
  1386. DPRINTK("EXIT\n");
  1387. return rc;
  1388. }
  1389. static int ata_port_nr_enabled(struct ata_port *ap)
  1390. {
  1391. int i, cnt = 0;
  1392. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1393. if (ata_dev_enabled(&ap->device[i]))
  1394. cnt++;
  1395. return cnt;
  1396. }
  1397. static int ata_port_nr_vacant(struct ata_port *ap)
  1398. {
  1399. int i, cnt = 0;
  1400. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1401. if (ap->device[i].class == ATA_DEV_UNKNOWN)
  1402. cnt++;
  1403. return cnt;
  1404. }
  1405. static int ata_eh_skip_recovery(struct ata_port *ap)
  1406. {
  1407. struct ata_eh_context *ehc = &ap->eh_context;
  1408. int i;
  1409. if (ap->flags & ATA_FLAG_FROZEN || ata_port_nr_enabled(ap))
  1410. return 0;
  1411. /* skip if class codes for all vacant slots are ATA_DEV_NONE */
  1412. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1413. struct ata_device *dev = &ap->device[i];
  1414. if (dev->class == ATA_DEV_UNKNOWN &&
  1415. ehc->classes[dev->devno] != ATA_DEV_NONE)
  1416. return 0;
  1417. }
  1418. return 1;
  1419. }
  1420. /**
  1421. * ata_eh_recover - recover host port after error
  1422. * @ap: host port to recover
  1423. * @prereset: prereset method (can be NULL)
  1424. * @softreset: softreset method (can be NULL)
  1425. * @hardreset: hardreset method (can be NULL)
  1426. * @postreset: postreset method (can be NULL)
  1427. *
  1428. * This is the alpha and omega, eum and yang, heart and soul of
  1429. * libata exception handling. On entry, actions required to
  1430. * recover the port and hotplug requests are recorded in
  1431. * eh_context. This function executes all the operations with
  1432. * appropriate retrials and fallbacks to resurrect failed
  1433. * devices, detach goners and greet newcomers.
  1434. *
  1435. * LOCKING:
  1436. * Kernel thread context (may sleep).
  1437. *
  1438. * RETURNS:
  1439. * 0 on success, -errno on failure.
  1440. */
  1441. static int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
  1442. ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
  1443. ata_postreset_fn_t postreset)
  1444. {
  1445. struct ata_eh_context *ehc = &ap->eh_context;
  1446. struct ata_device *dev;
  1447. int down_xfermask, i, rc;
  1448. DPRINTK("ENTER\n");
  1449. /* prep for recovery */
  1450. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1451. dev = &ap->device[i];
  1452. ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
  1453. /* process hotplug request */
  1454. if (dev->flags & ATA_DFLAG_DETACH)
  1455. ata_eh_detach_dev(dev);
  1456. if (!ata_dev_enabled(dev) &&
  1457. ((ehc->i.probe_mask & (1 << dev->devno)) &&
  1458. !(ehc->did_probe_mask & (1 << dev->devno)))) {
  1459. ata_eh_detach_dev(dev);
  1460. ata_dev_init(dev);
  1461. ehc->did_probe_mask |= (1 << dev->devno);
  1462. ehc->i.action |= ATA_EH_SOFTRESET;
  1463. }
  1464. }
  1465. retry:
  1466. down_xfermask = 0;
  1467. rc = 0;
  1468. /* if UNLOADING, finish immediately */
  1469. if (ap->flags & ATA_FLAG_UNLOADING)
  1470. goto out;
  1471. /* skip EH if possible. */
  1472. if (ata_eh_skip_recovery(ap))
  1473. ehc->i.action = 0;
  1474. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1475. ehc->classes[i] = ATA_DEV_UNKNOWN;
  1476. /* reset */
  1477. if (ehc->i.action & ATA_EH_RESET_MASK) {
  1478. ata_eh_freeze_port(ap);
  1479. rc = ata_eh_reset(ap, ata_port_nr_vacant(ap), prereset,
  1480. softreset, hardreset, postreset);
  1481. if (rc) {
  1482. ata_port_printk(ap, KERN_ERR,
  1483. "reset failed, giving up\n");
  1484. goto out;
  1485. }
  1486. ata_eh_thaw_port(ap);
  1487. }
  1488. /* revalidate existing devices and attach new ones */
  1489. rc = ata_eh_revalidate_and_attach(ap, &dev);
  1490. if (rc)
  1491. goto dev_fail;
  1492. /* configure transfer mode if the port has been reset */
  1493. if (ehc->i.flags & ATA_EHI_DID_RESET) {
  1494. rc = ata_set_mode(ap, &dev);
  1495. if (rc) {
  1496. down_xfermask = 1;
  1497. goto dev_fail;
  1498. }
  1499. }
  1500. goto out;
  1501. dev_fail:
  1502. switch (rc) {
  1503. case -ENODEV:
  1504. /* device missing, schedule probing */
  1505. ehc->i.probe_mask |= (1 << dev->devno);
  1506. case -EINVAL:
  1507. ehc->tries[dev->devno] = 0;
  1508. break;
  1509. case -EIO:
  1510. sata_down_spd_limit(ap);
  1511. default:
  1512. ehc->tries[dev->devno]--;
  1513. if (down_xfermask &&
  1514. ata_down_xfermask_limit(dev, ehc->tries[dev->devno] == 1))
  1515. ehc->tries[dev->devno] = 0;
  1516. }
  1517. if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
  1518. /* disable device if it has used up all its chances */
  1519. ata_dev_disable(dev);
  1520. /* detach if offline */
  1521. if (ata_port_offline(ap))
  1522. ata_eh_detach_dev(dev);
  1523. /* probe if requested */
  1524. if ((ehc->i.probe_mask & (1 << dev->devno)) &&
  1525. !(ehc->did_probe_mask & (1 << dev->devno))) {
  1526. ata_eh_detach_dev(dev);
  1527. ata_dev_init(dev);
  1528. ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
  1529. ehc->did_probe_mask |= (1 << dev->devno);
  1530. ehc->i.action |= ATA_EH_SOFTRESET;
  1531. }
  1532. } else {
  1533. /* soft didn't work? be haaaaard */
  1534. if (ehc->i.flags & ATA_EHI_DID_RESET)
  1535. ehc->i.action |= ATA_EH_HARDRESET;
  1536. else
  1537. ehc->i.action |= ATA_EH_SOFTRESET;
  1538. }
  1539. if (ata_port_nr_enabled(ap)) {
  1540. ata_port_printk(ap, KERN_WARNING, "failed to recover some "
  1541. "devices, retrying in 5 secs\n");
  1542. ssleep(5);
  1543. } else {
  1544. /* no device left, repeat fast */
  1545. msleep(500);
  1546. }
  1547. goto retry;
  1548. out:
  1549. if (rc) {
  1550. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1551. ata_dev_disable(&ap->device[i]);
  1552. }
  1553. DPRINTK("EXIT, rc=%d\n", rc);
  1554. return rc;
  1555. }
  1556. /**
  1557. * ata_eh_finish - finish up EH
  1558. * @ap: host port to finish EH for
  1559. *
  1560. * Recovery is complete. Clean up EH states and retry or finish
  1561. * failed qcs.
  1562. *
  1563. * LOCKING:
  1564. * None.
  1565. */
  1566. static void ata_eh_finish(struct ata_port *ap)
  1567. {
  1568. int tag;
  1569. /* retry or finish qcs */
  1570. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  1571. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  1572. if (!(qc->flags & ATA_QCFLAG_FAILED))
  1573. continue;
  1574. if (qc->err_mask) {
  1575. /* FIXME: Once EH migration is complete,
  1576. * generate sense data in this function,
  1577. * considering both err_mask and tf.
  1578. */
  1579. if (qc->err_mask & AC_ERR_INVALID)
  1580. ata_eh_qc_complete(qc);
  1581. else
  1582. ata_eh_qc_retry(qc);
  1583. } else {
  1584. if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
  1585. ata_eh_qc_complete(qc);
  1586. } else {
  1587. /* feed zero TF to sense generation */
  1588. memset(&qc->result_tf, 0, sizeof(qc->result_tf));
  1589. ata_eh_qc_retry(qc);
  1590. }
  1591. }
  1592. }
  1593. }
  1594. /**
  1595. * ata_do_eh - do standard error handling
  1596. * @ap: host port to handle error for
  1597. * @prereset: prereset method (can be NULL)
  1598. * @softreset: softreset method (can be NULL)
  1599. * @hardreset: hardreset method (can be NULL)
  1600. * @postreset: postreset method (can be NULL)
  1601. *
  1602. * Perform standard error handling sequence.
  1603. *
  1604. * LOCKING:
  1605. * Kernel thread context (may sleep).
  1606. */
  1607. void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
  1608. ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
  1609. ata_postreset_fn_t postreset)
  1610. {
  1611. if (!(ap->flags & ATA_FLAG_LOADING)) {
  1612. ata_eh_autopsy(ap);
  1613. ata_eh_report(ap);
  1614. }
  1615. ata_eh_recover(ap, prereset, softreset, hardreset, postreset);
  1616. ata_eh_finish(ap);
  1617. }