libata-eh.c 46 KB

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