libata-eh.c 55 KB

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