libata-eh.c 58 KB

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