libata-eh.c 53 KB

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