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_queued_cmd *qc)
  836. {
  837. struct ata_device *dev = qc->dev;
  838. unsigned char *sense_buf = qc->scsicmd->sense_buffer;
  839. struct ata_port *ap = dev->ap;
  840. struct ata_taskfile tf;
  841. u8 cdb[ATAPI_CDB_LEN];
  842. DPRINTK("ATAPI request sense\n");
  843. /* FIXME: is this needed? */
  844. memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
  845. /* initialize sense_buf with the error register,
  846. * for the case where they are -not- overwritten
  847. */
  848. sense_buf[0] = 0x70;
  849. sense_buf[2] = qc->result_tf.feature >> 4;
  850. /* some devices time out if garbage left in tf */
  851. ata_tf_init(dev, &tf);
  852. memset(cdb, 0, ATAPI_CDB_LEN);
  853. cdb[0] = REQUEST_SENSE;
  854. cdb[4] = SCSI_SENSE_BUFFERSIZE;
  855. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  856. tf.command = ATA_CMD_PACKET;
  857. /* is it pointless to prefer PIO for "safety reasons"? */
  858. if (ap->flags & ATA_FLAG_PIO_DMA) {
  859. tf.protocol = ATA_PROT_ATAPI_DMA;
  860. tf.feature |= ATAPI_PKT_DMA;
  861. } else {
  862. tf.protocol = ATA_PROT_ATAPI;
  863. tf.lbam = (8 * 1024) & 0xff;
  864. tf.lbah = (8 * 1024) >> 8;
  865. }
  866. return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
  867. sense_buf, SCSI_SENSE_BUFFERSIZE);
  868. }
  869. /**
  870. * ata_eh_analyze_serror - analyze SError for a failed port
  871. * @ap: ATA port to analyze SError for
  872. *
  873. * Analyze SError if available and further determine cause of
  874. * failure.
  875. *
  876. * LOCKING:
  877. * None.
  878. */
  879. static void ata_eh_analyze_serror(struct ata_port *ap)
  880. {
  881. struct ata_eh_context *ehc = &ap->eh_context;
  882. u32 serror = ehc->i.serror;
  883. unsigned int err_mask = 0, action = 0;
  884. if (serror & SERR_PERSISTENT) {
  885. err_mask |= AC_ERR_ATA_BUS;
  886. action |= ATA_EH_HARDRESET;
  887. }
  888. if (serror &
  889. (SERR_DATA_RECOVERED | SERR_COMM_RECOVERED | SERR_DATA)) {
  890. err_mask |= AC_ERR_ATA_BUS;
  891. action |= ATA_EH_SOFTRESET;
  892. }
  893. if (serror & SERR_PROTOCOL) {
  894. err_mask |= AC_ERR_HSM;
  895. action |= ATA_EH_SOFTRESET;
  896. }
  897. if (serror & SERR_INTERNAL) {
  898. err_mask |= AC_ERR_SYSTEM;
  899. action |= ATA_EH_SOFTRESET;
  900. }
  901. if (serror & (SERR_PHYRDY_CHG | SERR_DEV_XCHG))
  902. ata_ehi_hotplugged(&ehc->i);
  903. ehc->i.err_mask |= err_mask;
  904. ehc->i.action |= action;
  905. }
  906. /**
  907. * ata_eh_analyze_ncq_error - analyze NCQ error
  908. * @ap: ATA port to analyze NCQ error for
  909. *
  910. * Read log page 10h, determine the offending qc and acquire
  911. * error status TF. For NCQ device errors, all LLDDs have to do
  912. * is setting AC_ERR_DEV in ehi->err_mask. This function takes
  913. * care of the rest.
  914. *
  915. * LOCKING:
  916. * Kernel thread context (may sleep).
  917. */
  918. static void ata_eh_analyze_ncq_error(struct ata_port *ap)
  919. {
  920. struct ata_eh_context *ehc = &ap->eh_context;
  921. struct ata_device *dev = ap->device;
  922. struct ata_queued_cmd *qc;
  923. struct ata_taskfile tf;
  924. int tag, rc;
  925. /* if frozen, we can't do much */
  926. if (ap->pflags & ATA_PFLAG_FROZEN)
  927. return;
  928. /* is it NCQ device error? */
  929. if (!ap->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
  930. return;
  931. /* has LLDD analyzed already? */
  932. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  933. qc = __ata_qc_from_tag(ap, tag);
  934. if (!(qc->flags & ATA_QCFLAG_FAILED))
  935. continue;
  936. if (qc->err_mask)
  937. return;
  938. }
  939. /* okay, this error is ours */
  940. rc = ata_eh_read_log_10h(dev, &tag, &tf);
  941. if (rc) {
  942. ata_port_printk(ap, KERN_ERR, "failed to read log page 10h "
  943. "(errno=%d)\n", rc);
  944. return;
  945. }
  946. if (!(ap->sactive & (1 << tag))) {
  947. ata_port_printk(ap, KERN_ERR, "log page 10h reported "
  948. "inactive tag %d\n", tag);
  949. return;
  950. }
  951. /* we've got the perpetrator, condemn it */
  952. qc = __ata_qc_from_tag(ap, tag);
  953. memcpy(&qc->result_tf, &tf, sizeof(tf));
  954. qc->err_mask |= AC_ERR_DEV;
  955. ehc->i.err_mask &= ~AC_ERR_DEV;
  956. }
  957. /**
  958. * ata_eh_analyze_tf - analyze taskfile of a failed qc
  959. * @qc: qc to analyze
  960. * @tf: Taskfile registers to analyze
  961. *
  962. * Analyze taskfile of @qc and further determine cause of
  963. * failure. This function also requests ATAPI sense data if
  964. * avaliable.
  965. *
  966. * LOCKING:
  967. * Kernel thread context (may sleep).
  968. *
  969. * RETURNS:
  970. * Determined recovery action
  971. */
  972. static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
  973. const struct ata_taskfile *tf)
  974. {
  975. unsigned int tmp, action = 0;
  976. u8 stat = tf->command, err = tf->feature;
  977. if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
  978. qc->err_mask |= AC_ERR_HSM;
  979. return ATA_EH_SOFTRESET;
  980. }
  981. if (!(qc->err_mask & AC_ERR_DEV))
  982. return 0;
  983. switch (qc->dev->class) {
  984. case ATA_DEV_ATA:
  985. if (err & ATA_ICRC)
  986. qc->err_mask |= AC_ERR_ATA_BUS;
  987. if (err & ATA_UNC)
  988. qc->err_mask |= AC_ERR_MEDIA;
  989. if (err & ATA_IDNF)
  990. qc->err_mask |= AC_ERR_INVALID;
  991. break;
  992. case ATA_DEV_ATAPI:
  993. if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
  994. tmp = atapi_eh_request_sense(qc);
  995. if (!tmp) {
  996. /* ATA_QCFLAG_SENSE_VALID is used to
  997. * tell atapi_qc_complete() that sense
  998. * data is already valid.
  999. *
  1000. * TODO: interpret sense data and set
  1001. * appropriate err_mask.
  1002. */
  1003. qc->flags |= ATA_QCFLAG_SENSE_VALID;
  1004. } else
  1005. qc->err_mask |= tmp;
  1006. }
  1007. }
  1008. if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
  1009. action |= ATA_EH_SOFTRESET;
  1010. return action;
  1011. }
  1012. static int ata_eh_categorize_error(int is_io, unsigned int err_mask)
  1013. {
  1014. if (err_mask & AC_ERR_ATA_BUS)
  1015. return 1;
  1016. if (err_mask & AC_ERR_TIMEOUT)
  1017. return 2;
  1018. if (is_io) {
  1019. if (err_mask & AC_ERR_HSM)
  1020. return 2;
  1021. if ((err_mask &
  1022. (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
  1023. return 3;
  1024. }
  1025. return 0;
  1026. }
  1027. struct speed_down_verdict_arg {
  1028. u64 since;
  1029. int nr_errors[4];
  1030. };
  1031. static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
  1032. {
  1033. struct speed_down_verdict_arg *arg = void_arg;
  1034. int cat = ata_eh_categorize_error(ent->is_io, ent->err_mask);
  1035. if (ent->timestamp < arg->since)
  1036. return -1;
  1037. arg->nr_errors[cat]++;
  1038. return 0;
  1039. }
  1040. /**
  1041. * ata_eh_speed_down_verdict - Determine speed down verdict
  1042. * @dev: Device of interest
  1043. *
  1044. * This function examines error ring of @dev and determines
  1045. * whether NCQ needs to be turned off, transfer speed should be
  1046. * stepped down, or falling back to PIO is necessary.
  1047. *
  1048. * Cat-1 is ATA_BUS error for any command.
  1049. *
  1050. * Cat-2 is TIMEOUT for any command or HSM violation for known
  1051. * supported commands.
  1052. *
  1053. * Cat-3 is is unclassified DEV error for known supported
  1054. * command.
  1055. *
  1056. * NCQ needs to be turned off if there have been more than 3
  1057. * Cat-2 + Cat-3 errors during last 10 minutes.
  1058. *
  1059. * Speed down is necessary if there have been more than 3 Cat-1 +
  1060. * Cat-2 errors or 10 Cat-3 errors during last 10 minutes.
  1061. *
  1062. * Falling back to PIO mode is necessary if there have been more
  1063. * than 10 Cat-1 + Cat-2 + Cat-3 errors during last 5 minutes.
  1064. *
  1065. * LOCKING:
  1066. * Inherited from caller.
  1067. *
  1068. * RETURNS:
  1069. * OR of ATA_EH_SPDN_* flags.
  1070. */
  1071. static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
  1072. {
  1073. const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
  1074. u64 j64 = get_jiffies_64();
  1075. struct speed_down_verdict_arg arg;
  1076. unsigned int verdict = 0;
  1077. /* scan past 10 mins of error history */
  1078. memset(&arg, 0, sizeof(arg));
  1079. arg.since = j64 - min(j64, j10mins);
  1080. ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
  1081. if (arg.nr_errors[2] + arg.nr_errors[3] > 3)
  1082. verdict |= ATA_EH_SPDN_NCQ_OFF;
  1083. if (arg.nr_errors[1] + arg.nr_errors[2] > 3 || arg.nr_errors[3] > 10)
  1084. verdict |= ATA_EH_SPDN_SPEED_DOWN;
  1085. /* scan past 3 mins of error history */
  1086. memset(&arg, 0, sizeof(arg));
  1087. arg.since = j64 - min(j64, j5mins);
  1088. ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
  1089. if (arg.nr_errors[1] + arg.nr_errors[2] + arg.nr_errors[3] > 10)
  1090. verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
  1091. return verdict;
  1092. }
  1093. /**
  1094. * ata_eh_speed_down - record error and speed down if necessary
  1095. * @dev: Failed device
  1096. * @is_io: Did the device fail during normal IO?
  1097. * @err_mask: err_mask of the error
  1098. *
  1099. * Record error and examine error history to determine whether
  1100. * adjusting transmission speed is necessary. It also sets
  1101. * transmission limits appropriately if such adjustment is
  1102. * necessary.
  1103. *
  1104. * LOCKING:
  1105. * Kernel thread context (may sleep).
  1106. *
  1107. * RETURNS:
  1108. * Determined recovery action.
  1109. */
  1110. static unsigned int ata_eh_speed_down(struct ata_device *dev, int is_io,
  1111. unsigned int err_mask)
  1112. {
  1113. unsigned int verdict;
  1114. unsigned int action = 0;
  1115. /* don't bother if Cat-0 error */
  1116. if (ata_eh_categorize_error(is_io, err_mask) == 0)
  1117. return 0;
  1118. /* record error and determine whether speed down is necessary */
  1119. ata_ering_record(&dev->ering, is_io, err_mask);
  1120. verdict = ata_eh_speed_down_verdict(dev);
  1121. /* turn off NCQ? */
  1122. if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
  1123. (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
  1124. ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
  1125. dev->flags |= ATA_DFLAG_NCQ_OFF;
  1126. ata_dev_printk(dev, KERN_WARNING,
  1127. "NCQ disabled due to excessive errors\n");
  1128. goto done;
  1129. }
  1130. /* speed down? */
  1131. if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
  1132. /* speed down SATA link speed if possible */
  1133. if (sata_down_spd_limit(dev->ap) == 0) {
  1134. action |= ATA_EH_HARDRESET;
  1135. goto done;
  1136. }
  1137. /* lower transfer mode */
  1138. if (dev->spdn_cnt < 2) {
  1139. static const int dma_dnxfer_sel[] =
  1140. { ATA_DNXFER_DMA, ATA_DNXFER_40C };
  1141. static const int pio_dnxfer_sel[] =
  1142. { ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
  1143. int sel;
  1144. if (dev->xfer_shift != ATA_SHIFT_PIO)
  1145. sel = dma_dnxfer_sel[dev->spdn_cnt];
  1146. else
  1147. sel = pio_dnxfer_sel[dev->spdn_cnt];
  1148. dev->spdn_cnt++;
  1149. if (ata_down_xfermask_limit(dev, sel) == 0) {
  1150. action |= ATA_EH_SOFTRESET;
  1151. goto done;
  1152. }
  1153. }
  1154. }
  1155. /* Fall back to PIO? Slowing down to PIO is meaningless for
  1156. * SATA. Consider it only for PATA.
  1157. */
  1158. if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
  1159. (dev->ap->cbl != ATA_CBL_SATA) &&
  1160. (dev->xfer_shift != ATA_SHIFT_PIO)) {
  1161. if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
  1162. dev->spdn_cnt = 0;
  1163. action |= ATA_EH_SOFTRESET;
  1164. goto done;
  1165. }
  1166. }
  1167. return 0;
  1168. done:
  1169. /* device has been slowed down, blow error history */
  1170. ata_ering_clear(&dev->ering);
  1171. return action;
  1172. }
  1173. /**
  1174. * ata_eh_autopsy - analyze error and determine recovery action
  1175. * @ap: ATA port to perform autopsy on
  1176. *
  1177. * Analyze why @ap failed and determine which recovery action is
  1178. * needed. This function also sets more detailed AC_ERR_* values
  1179. * and fills sense data for ATAPI CHECK SENSE.
  1180. *
  1181. * LOCKING:
  1182. * Kernel thread context (may sleep).
  1183. */
  1184. static void ata_eh_autopsy(struct ata_port *ap)
  1185. {
  1186. struct ata_eh_context *ehc = &ap->eh_context;
  1187. unsigned int all_err_mask = 0;
  1188. int tag, is_io = 0;
  1189. u32 serror;
  1190. int rc;
  1191. DPRINTK("ENTER\n");
  1192. if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
  1193. return;
  1194. /* obtain and analyze SError */
  1195. rc = sata_scr_read(ap, SCR_ERROR, &serror);
  1196. if (rc == 0) {
  1197. ehc->i.serror |= serror;
  1198. ata_eh_analyze_serror(ap);
  1199. } else if (rc != -EOPNOTSUPP)
  1200. ehc->i.action |= ATA_EH_HARDRESET;
  1201. /* analyze NCQ failure */
  1202. ata_eh_analyze_ncq_error(ap);
  1203. /* any real error trumps AC_ERR_OTHER */
  1204. if (ehc->i.err_mask & ~AC_ERR_OTHER)
  1205. ehc->i.err_mask &= ~AC_ERR_OTHER;
  1206. all_err_mask |= ehc->i.err_mask;
  1207. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  1208. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  1209. if (!(qc->flags & ATA_QCFLAG_FAILED))
  1210. continue;
  1211. /* inherit upper level err_mask */
  1212. qc->err_mask |= ehc->i.err_mask;
  1213. /* analyze TF */
  1214. ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
  1215. /* DEV errors are probably spurious in case of ATA_BUS error */
  1216. if (qc->err_mask & AC_ERR_ATA_BUS)
  1217. qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
  1218. AC_ERR_INVALID);
  1219. /* any real error trumps unknown error */
  1220. if (qc->err_mask & ~AC_ERR_OTHER)
  1221. qc->err_mask &= ~AC_ERR_OTHER;
  1222. /* SENSE_VALID trumps dev/unknown error and revalidation */
  1223. if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
  1224. qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
  1225. ehc->i.action &= ~ATA_EH_REVALIDATE;
  1226. }
  1227. /* accumulate error info */
  1228. ehc->i.dev = qc->dev;
  1229. all_err_mask |= qc->err_mask;
  1230. if (qc->flags & ATA_QCFLAG_IO)
  1231. is_io = 1;
  1232. }
  1233. /* enforce default EH actions */
  1234. if (ap->pflags & ATA_PFLAG_FROZEN ||
  1235. all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
  1236. ehc->i.action |= ATA_EH_SOFTRESET;
  1237. else if (all_err_mask)
  1238. ehc->i.action |= ATA_EH_REVALIDATE;
  1239. /* if we have offending qcs and the associated failed device */
  1240. if (ehc->i.dev) {
  1241. /* speed down */
  1242. ehc->i.action |= ata_eh_speed_down(ehc->i.dev, is_io,
  1243. all_err_mask);
  1244. /* perform per-dev EH action only on the offending device */
  1245. ehc->i.dev_action[ehc->i.dev->devno] |=
  1246. ehc->i.action & ATA_EH_PERDEV_MASK;
  1247. ehc->i.action &= ~ATA_EH_PERDEV_MASK;
  1248. }
  1249. DPRINTK("EXIT\n");
  1250. }
  1251. /**
  1252. * ata_eh_report - report error handling to user
  1253. * @ap: ATA port EH is going on
  1254. *
  1255. * Report EH to user.
  1256. *
  1257. * LOCKING:
  1258. * None.
  1259. */
  1260. static void ata_eh_report(struct ata_port *ap)
  1261. {
  1262. struct ata_eh_context *ehc = &ap->eh_context;
  1263. const char *frozen, *desc;
  1264. int tag, nr_failed = 0;
  1265. desc = NULL;
  1266. if (ehc->i.desc[0] != '\0')
  1267. desc = ehc->i.desc;
  1268. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  1269. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  1270. if (!(qc->flags & ATA_QCFLAG_FAILED))
  1271. continue;
  1272. if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
  1273. continue;
  1274. nr_failed++;
  1275. }
  1276. if (!nr_failed && !ehc->i.err_mask)
  1277. return;
  1278. frozen = "";
  1279. if (ap->pflags & ATA_PFLAG_FROZEN)
  1280. frozen = " frozen";
  1281. if (ehc->i.dev) {
  1282. ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
  1283. "SAct 0x%x SErr 0x%x action 0x%x%s\n",
  1284. ehc->i.err_mask, ap->sactive, ehc->i.serror,
  1285. ehc->i.action, frozen);
  1286. if (desc)
  1287. ata_dev_printk(ehc->i.dev, KERN_ERR, "(%s)\n", desc);
  1288. } else {
  1289. ata_port_printk(ap, KERN_ERR, "exception Emask 0x%x "
  1290. "SAct 0x%x SErr 0x%x action 0x%x%s\n",
  1291. ehc->i.err_mask, ap->sactive, ehc->i.serror,
  1292. ehc->i.action, frozen);
  1293. if (desc)
  1294. ata_port_printk(ap, KERN_ERR, "(%s)\n", desc);
  1295. }
  1296. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  1297. static const char *dma_str[] = {
  1298. [DMA_BIDIRECTIONAL] = "bidi",
  1299. [DMA_TO_DEVICE] = "out",
  1300. [DMA_FROM_DEVICE] = "in",
  1301. [DMA_NONE] = "",
  1302. };
  1303. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  1304. struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
  1305. if (!(qc->flags & ATA_QCFLAG_FAILED) || !qc->err_mask)
  1306. continue;
  1307. ata_dev_printk(qc->dev, KERN_ERR,
  1308. "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
  1309. "tag %d cdb 0x%x data %u %s\n "
  1310. "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
  1311. "Emask 0x%x (%s)\n",
  1312. cmd->command, cmd->feature, cmd->nsect,
  1313. cmd->lbal, cmd->lbam, cmd->lbah,
  1314. cmd->hob_feature, cmd->hob_nsect,
  1315. cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
  1316. cmd->device, qc->tag, qc->cdb[0], qc->nbytes,
  1317. dma_str[qc->dma_dir],
  1318. res->command, res->feature, res->nsect,
  1319. res->lbal, res->lbam, res->lbah,
  1320. res->hob_feature, res->hob_nsect,
  1321. res->hob_lbal, res->hob_lbam, res->hob_lbah,
  1322. res->device, qc->err_mask, ata_err_string(qc->err_mask));
  1323. }
  1324. }
  1325. static int ata_do_reset(struct ata_port *ap, ata_reset_fn_t reset,
  1326. unsigned int *classes)
  1327. {
  1328. int i, rc;
  1329. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1330. classes[i] = ATA_DEV_UNKNOWN;
  1331. rc = reset(ap, classes);
  1332. if (rc)
  1333. return rc;
  1334. /* If any class isn't ATA_DEV_UNKNOWN, consider classification
  1335. * is complete and convert all ATA_DEV_UNKNOWN to
  1336. * ATA_DEV_NONE.
  1337. */
  1338. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1339. if (classes[i] != ATA_DEV_UNKNOWN)
  1340. break;
  1341. if (i < ATA_MAX_DEVICES)
  1342. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1343. if (classes[i] == ATA_DEV_UNKNOWN)
  1344. classes[i] = ATA_DEV_NONE;
  1345. return 0;
  1346. }
  1347. static int ata_eh_followup_srst_needed(int rc, int classify,
  1348. const unsigned int *classes)
  1349. {
  1350. if (rc == -EAGAIN)
  1351. return 1;
  1352. if (rc != 0)
  1353. return 0;
  1354. if (classify && classes[0] == ATA_DEV_UNKNOWN)
  1355. return 1;
  1356. return 0;
  1357. }
  1358. static int ata_eh_reset(struct ata_port *ap, int classify,
  1359. ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
  1360. ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
  1361. {
  1362. struct ata_eh_context *ehc = &ap->eh_context;
  1363. unsigned int *classes = ehc->classes;
  1364. int tries = ATA_EH_RESET_TRIES;
  1365. int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
  1366. unsigned int action;
  1367. ata_reset_fn_t reset;
  1368. int i, did_followup_srst, rc;
  1369. /* about to reset */
  1370. ata_eh_about_to_do(ap, NULL, ehc->i.action & ATA_EH_RESET_MASK);
  1371. /* Determine which reset to use and record in ehc->i.action.
  1372. * prereset() may examine and modify it.
  1373. */
  1374. action = ehc->i.action;
  1375. ehc->i.action &= ~ATA_EH_RESET_MASK;
  1376. if (softreset && (!hardreset || (!sata_set_spd_needed(ap) &&
  1377. !(action & ATA_EH_HARDRESET))))
  1378. ehc->i.action |= ATA_EH_SOFTRESET;
  1379. else
  1380. ehc->i.action |= ATA_EH_HARDRESET;
  1381. if (prereset) {
  1382. rc = prereset(ap);
  1383. if (rc) {
  1384. if (rc == -ENOENT) {
  1385. ata_port_printk(ap, KERN_DEBUG,
  1386. "port disabled. ignoring.\n");
  1387. ap->eh_context.i.action &= ~ATA_EH_RESET_MASK;
  1388. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1389. classes[i] = ATA_DEV_NONE;
  1390. rc = 0;
  1391. } else
  1392. ata_port_printk(ap, KERN_ERR,
  1393. "prereset failed (errno=%d)\n", rc);
  1394. return rc;
  1395. }
  1396. }
  1397. /* prereset() might have modified ehc->i.action */
  1398. if (ehc->i.action & ATA_EH_HARDRESET)
  1399. reset = hardreset;
  1400. else if (ehc->i.action & ATA_EH_SOFTRESET)
  1401. reset = softreset;
  1402. else {
  1403. /* prereset told us not to reset, bang classes and return */
  1404. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1405. classes[i] = ATA_DEV_NONE;
  1406. return 0;
  1407. }
  1408. /* did prereset() screw up? if so, fix up to avoid oopsing */
  1409. if (!reset) {
  1410. ata_port_printk(ap, KERN_ERR, "BUG: prereset() requested "
  1411. "invalid reset type\n");
  1412. if (softreset)
  1413. reset = softreset;
  1414. else
  1415. reset = hardreset;
  1416. }
  1417. retry:
  1418. /* shut up during boot probing */
  1419. if (verbose)
  1420. ata_port_printk(ap, KERN_INFO, "%s resetting port\n",
  1421. reset == softreset ? "soft" : "hard");
  1422. /* mark that this EH session started with reset */
  1423. ehc->i.flags |= ATA_EHI_DID_RESET;
  1424. rc = ata_do_reset(ap, reset, classes);
  1425. did_followup_srst = 0;
  1426. if (reset == hardreset &&
  1427. ata_eh_followup_srst_needed(rc, classify, classes)) {
  1428. /* okay, let's do follow-up softreset */
  1429. did_followup_srst = 1;
  1430. reset = softreset;
  1431. if (!reset) {
  1432. ata_port_printk(ap, KERN_ERR,
  1433. "follow-up softreset required "
  1434. "but no softreset avaliable\n");
  1435. return -EINVAL;
  1436. }
  1437. ata_eh_about_to_do(ap, NULL, ATA_EH_RESET_MASK);
  1438. rc = ata_do_reset(ap, reset, classes);
  1439. if (rc == 0 && classify &&
  1440. classes[0] == ATA_DEV_UNKNOWN) {
  1441. ata_port_printk(ap, KERN_ERR,
  1442. "classification failed\n");
  1443. return -EINVAL;
  1444. }
  1445. }
  1446. if (rc && --tries) {
  1447. const char *type;
  1448. if (reset == softreset) {
  1449. if (did_followup_srst)
  1450. type = "follow-up soft";
  1451. else
  1452. type = "soft";
  1453. } else
  1454. type = "hard";
  1455. ata_port_printk(ap, KERN_WARNING,
  1456. "%sreset failed, retrying in 5 secs\n", type);
  1457. ssleep(5);
  1458. if (reset == hardreset)
  1459. sata_down_spd_limit(ap);
  1460. if (hardreset)
  1461. reset = hardreset;
  1462. goto retry;
  1463. }
  1464. if (rc == 0) {
  1465. /* After the reset, the device state is PIO 0 and the
  1466. * controller state is undefined. Record the mode.
  1467. */
  1468. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1469. ap->device[i].pio_mode = XFER_PIO_0;
  1470. if (postreset)
  1471. postreset(ap, classes);
  1472. /* reset successful, schedule revalidation */
  1473. ata_eh_done(ap, NULL, ehc->i.action & ATA_EH_RESET_MASK);
  1474. ehc->i.action |= ATA_EH_REVALIDATE;
  1475. }
  1476. return rc;
  1477. }
  1478. static int ata_eh_revalidate_and_attach(struct ata_port *ap,
  1479. struct ata_device **r_failed_dev)
  1480. {
  1481. struct ata_eh_context *ehc = &ap->eh_context;
  1482. struct ata_device *dev;
  1483. unsigned int new_mask = 0;
  1484. unsigned long flags;
  1485. int i, rc = 0;
  1486. DPRINTK("ENTER\n");
  1487. /* For PATA drive side cable detection to work, IDENTIFY must
  1488. * be done backwards such that PDIAG- is released by the slave
  1489. * device before the master device is identified.
  1490. */
  1491. for (i = ATA_MAX_DEVICES - 1; i >= 0; i--) {
  1492. unsigned int action, readid_flags = 0;
  1493. dev = &ap->device[i];
  1494. action = ata_eh_dev_action(dev);
  1495. if (ehc->i.flags & ATA_EHI_DID_RESET)
  1496. readid_flags |= ATA_READID_POSTRESET;
  1497. if (action & ATA_EH_REVALIDATE && ata_dev_ready(dev)) {
  1498. if (ata_port_offline(ap)) {
  1499. rc = -EIO;
  1500. goto err;
  1501. }
  1502. ata_eh_about_to_do(ap, dev, ATA_EH_REVALIDATE);
  1503. rc = ata_dev_revalidate(dev, readid_flags);
  1504. if (rc)
  1505. goto err;
  1506. ata_eh_done(ap, dev, ATA_EH_REVALIDATE);
  1507. /* Configuration may have changed, reconfigure
  1508. * transfer mode.
  1509. */
  1510. ehc->i.flags |= ATA_EHI_SETMODE;
  1511. /* schedule the scsi_rescan_device() here */
  1512. queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
  1513. } else if (dev->class == ATA_DEV_UNKNOWN &&
  1514. ehc->tries[dev->devno] &&
  1515. ata_class_enabled(ehc->classes[dev->devno])) {
  1516. dev->class = ehc->classes[dev->devno];
  1517. rc = ata_dev_read_id(dev, &dev->class, readid_flags,
  1518. dev->id);
  1519. switch (rc) {
  1520. case 0:
  1521. new_mask |= 1 << i;
  1522. break;
  1523. case -ENOENT:
  1524. /* IDENTIFY was issued to non-existent
  1525. * device. No need to reset. Just
  1526. * thaw and kill the device.
  1527. */
  1528. ata_eh_thaw_port(ap);
  1529. dev->class = ATA_DEV_UNKNOWN;
  1530. break;
  1531. default:
  1532. dev->class = ATA_DEV_UNKNOWN;
  1533. goto err;
  1534. }
  1535. }
  1536. }
  1537. /* Configure new devices forward such that user doesn't see
  1538. * device detection messages backwards.
  1539. */
  1540. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1541. dev = &ap->device[i];
  1542. if (!(new_mask & (1 << i)))
  1543. continue;
  1544. ehc->i.flags |= ATA_EHI_PRINTINFO;
  1545. rc = ata_dev_configure(dev);
  1546. ehc->i.flags &= ~ATA_EHI_PRINTINFO;
  1547. if (rc)
  1548. goto err;
  1549. spin_lock_irqsave(ap->lock, flags);
  1550. ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
  1551. spin_unlock_irqrestore(ap->lock, flags);
  1552. /* new device discovered, configure xfermode */
  1553. ehc->i.flags |= ATA_EHI_SETMODE;
  1554. }
  1555. return 0;
  1556. err:
  1557. *r_failed_dev = dev;
  1558. DPRINTK("EXIT rc=%d\n", rc);
  1559. return rc;
  1560. }
  1561. #ifdef CONFIG_PM
  1562. /**
  1563. * ata_eh_suspend - handle suspend EH action
  1564. * @ap: target host port
  1565. * @r_failed_dev: result parameter to indicate failing device
  1566. *
  1567. * Handle suspend EH action. Disk devices are spinned down and
  1568. * other types of devices are just marked suspended. Once
  1569. * suspended, no EH action to the device is allowed until it is
  1570. * resumed.
  1571. *
  1572. * LOCKING:
  1573. * Kernel thread context (may sleep).
  1574. *
  1575. * RETURNS:
  1576. * 0 on success, -errno otherwise
  1577. */
  1578. static int ata_eh_suspend(struct ata_port *ap, struct ata_device **r_failed_dev)
  1579. {
  1580. struct ata_device *dev;
  1581. int i, rc = 0;
  1582. DPRINTK("ENTER\n");
  1583. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1584. unsigned long flags;
  1585. unsigned int action, err_mask;
  1586. dev = &ap->device[i];
  1587. action = ata_eh_dev_action(dev);
  1588. if (!ata_dev_enabled(dev) || !(action & ATA_EH_SUSPEND))
  1589. continue;
  1590. WARN_ON(dev->flags & ATA_DFLAG_SUSPENDED);
  1591. ata_eh_about_to_do(ap, dev, ATA_EH_SUSPEND);
  1592. if (dev->class == ATA_DEV_ATA && !(action & ATA_EH_PM_FREEZE)) {
  1593. /* flush cache */
  1594. rc = ata_flush_cache(dev);
  1595. if (rc)
  1596. break;
  1597. /* spin down */
  1598. err_mask = ata_do_simple_cmd(dev, ATA_CMD_STANDBYNOW1);
  1599. if (err_mask) {
  1600. ata_dev_printk(dev, KERN_ERR, "failed to "
  1601. "spin down (err_mask=0x%x)\n",
  1602. err_mask);
  1603. rc = -EIO;
  1604. break;
  1605. }
  1606. }
  1607. spin_lock_irqsave(ap->lock, flags);
  1608. dev->flags |= ATA_DFLAG_SUSPENDED;
  1609. spin_unlock_irqrestore(ap->lock, flags);
  1610. ata_eh_done(ap, dev, ATA_EH_SUSPEND);
  1611. }
  1612. if (rc)
  1613. *r_failed_dev = dev;
  1614. DPRINTK("EXIT\n");
  1615. return rc;
  1616. }
  1617. /**
  1618. * ata_eh_prep_resume - prep for resume EH action
  1619. * @ap: target host port
  1620. *
  1621. * Clear SUSPENDED in preparation for scheduled resume actions.
  1622. * This allows other parts of EH to access the devices being
  1623. * resumed.
  1624. *
  1625. * LOCKING:
  1626. * Kernel thread context (may sleep).
  1627. */
  1628. static void ata_eh_prep_resume(struct ata_port *ap)
  1629. {
  1630. struct ata_device *dev;
  1631. unsigned long flags;
  1632. int i;
  1633. DPRINTK("ENTER\n");
  1634. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1635. unsigned int action;
  1636. dev = &ap->device[i];
  1637. action = ata_eh_dev_action(dev);
  1638. if (!ata_dev_enabled(dev) || !(action & ATA_EH_RESUME))
  1639. continue;
  1640. spin_lock_irqsave(ap->lock, flags);
  1641. dev->flags &= ~ATA_DFLAG_SUSPENDED;
  1642. spin_unlock_irqrestore(ap->lock, flags);
  1643. }
  1644. DPRINTK("EXIT\n");
  1645. }
  1646. /**
  1647. * ata_eh_resume - handle resume EH action
  1648. * @ap: target host port
  1649. * @r_failed_dev: result parameter to indicate failing device
  1650. *
  1651. * Handle resume EH action. Target devices are already reset and
  1652. * revalidated. Spinning up is the only operation left.
  1653. *
  1654. * LOCKING:
  1655. * Kernel thread context (may sleep).
  1656. *
  1657. * RETURNS:
  1658. * 0 on success, -errno otherwise
  1659. */
  1660. static int ata_eh_resume(struct ata_port *ap, struct ata_device **r_failed_dev)
  1661. {
  1662. struct ata_device *dev;
  1663. int i, rc = 0;
  1664. DPRINTK("ENTER\n");
  1665. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1666. unsigned int action, err_mask;
  1667. dev = &ap->device[i];
  1668. action = ata_eh_dev_action(dev);
  1669. if (!ata_dev_enabled(dev) || !(action & ATA_EH_RESUME))
  1670. continue;
  1671. ata_eh_about_to_do(ap, dev, ATA_EH_RESUME);
  1672. if (dev->class == ATA_DEV_ATA && !(action & ATA_EH_PM_FREEZE)) {
  1673. err_mask = ata_do_simple_cmd(dev,
  1674. ATA_CMD_IDLEIMMEDIATE);
  1675. if (err_mask) {
  1676. ata_dev_printk(dev, KERN_ERR, "failed to "
  1677. "spin up (err_mask=0x%x)\n",
  1678. err_mask);
  1679. rc = -EIO;
  1680. break;
  1681. }
  1682. }
  1683. ata_eh_done(ap, dev, ATA_EH_RESUME);
  1684. }
  1685. if (rc)
  1686. *r_failed_dev = dev;
  1687. DPRINTK("EXIT\n");
  1688. return 0;
  1689. }
  1690. #endif /* CONFIG_PM */
  1691. static int ata_port_nr_enabled(struct ata_port *ap)
  1692. {
  1693. int i, cnt = 0;
  1694. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1695. if (ata_dev_enabled(&ap->device[i]))
  1696. cnt++;
  1697. return cnt;
  1698. }
  1699. static int ata_port_nr_vacant(struct ata_port *ap)
  1700. {
  1701. int i, cnt = 0;
  1702. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1703. if (ap->device[i].class == ATA_DEV_UNKNOWN)
  1704. cnt++;
  1705. return cnt;
  1706. }
  1707. static int ata_eh_skip_recovery(struct ata_port *ap)
  1708. {
  1709. struct ata_eh_context *ehc = &ap->eh_context;
  1710. int i;
  1711. /* skip if all possible devices are suspended */
  1712. for (i = 0; i < ata_port_max_devices(ap); i++) {
  1713. struct ata_device *dev = &ap->device[i];
  1714. if (!(dev->flags & ATA_DFLAG_SUSPENDED))
  1715. break;
  1716. }
  1717. if (i == ata_port_max_devices(ap))
  1718. return 1;
  1719. /* thaw frozen port, resume link and recover failed devices */
  1720. if ((ap->pflags & ATA_PFLAG_FROZEN) ||
  1721. (ehc->i.flags & ATA_EHI_RESUME_LINK) || ata_port_nr_enabled(ap))
  1722. return 0;
  1723. /* skip if class codes for all vacant slots are ATA_DEV_NONE */
  1724. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1725. struct ata_device *dev = &ap->device[i];
  1726. if (dev->class == ATA_DEV_UNKNOWN &&
  1727. ehc->classes[dev->devno] != ATA_DEV_NONE)
  1728. return 0;
  1729. }
  1730. return 1;
  1731. }
  1732. /**
  1733. * ata_eh_recover - recover host port after error
  1734. * @ap: host port to recover
  1735. * @prereset: prereset method (can be NULL)
  1736. * @softreset: softreset method (can be NULL)
  1737. * @hardreset: hardreset method (can be NULL)
  1738. * @postreset: postreset method (can be NULL)
  1739. *
  1740. * This is the alpha and omega, eum and yang, heart and soul of
  1741. * libata exception handling. On entry, actions required to
  1742. * recover the port and hotplug requests are recorded in
  1743. * eh_context. This function executes all the operations with
  1744. * appropriate retrials and fallbacks to resurrect failed
  1745. * devices, detach goners and greet newcomers.
  1746. *
  1747. * LOCKING:
  1748. * Kernel thread context (may sleep).
  1749. *
  1750. * RETURNS:
  1751. * 0 on success, -errno on failure.
  1752. */
  1753. static int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
  1754. ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
  1755. ata_postreset_fn_t postreset)
  1756. {
  1757. struct ata_eh_context *ehc = &ap->eh_context;
  1758. struct ata_device *dev;
  1759. int i, rc;
  1760. DPRINTK("ENTER\n");
  1761. /* prep for recovery */
  1762. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1763. dev = &ap->device[i];
  1764. ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
  1765. /* collect port action mask recorded in dev actions */
  1766. ehc->i.action |= ehc->i.dev_action[i] & ~ATA_EH_PERDEV_MASK;
  1767. ehc->i.dev_action[i] &= ATA_EH_PERDEV_MASK;
  1768. /* process hotplug request */
  1769. if (dev->flags & ATA_DFLAG_DETACH)
  1770. ata_eh_detach_dev(dev);
  1771. if (!ata_dev_enabled(dev) &&
  1772. ((ehc->i.probe_mask & (1 << dev->devno)) &&
  1773. !(ehc->did_probe_mask & (1 << dev->devno)))) {
  1774. ata_eh_detach_dev(dev);
  1775. ata_dev_init(dev);
  1776. ehc->did_probe_mask |= (1 << dev->devno);
  1777. ehc->i.action |= ATA_EH_SOFTRESET;
  1778. }
  1779. }
  1780. retry:
  1781. rc = 0;
  1782. /* if UNLOADING, finish immediately */
  1783. if (ap->pflags & ATA_PFLAG_UNLOADING)
  1784. goto out;
  1785. /* prep for resume */
  1786. ata_eh_prep_resume(ap);
  1787. /* skip EH if possible. */
  1788. if (ata_eh_skip_recovery(ap))
  1789. ehc->i.action = 0;
  1790. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1791. ehc->classes[i] = ATA_DEV_UNKNOWN;
  1792. /* reset */
  1793. if (ehc->i.action & ATA_EH_RESET_MASK) {
  1794. ata_eh_freeze_port(ap);
  1795. rc = ata_eh_reset(ap, ata_port_nr_vacant(ap), prereset,
  1796. softreset, hardreset, postreset);
  1797. if (rc) {
  1798. ata_port_printk(ap, KERN_ERR,
  1799. "reset failed, giving up\n");
  1800. goto out;
  1801. }
  1802. ata_eh_thaw_port(ap);
  1803. }
  1804. /* revalidate existing devices and attach new ones */
  1805. rc = ata_eh_revalidate_and_attach(ap, &dev);
  1806. if (rc)
  1807. goto dev_fail;
  1808. /* resume devices */
  1809. rc = ata_eh_resume(ap, &dev);
  1810. if (rc)
  1811. goto dev_fail;
  1812. /* configure transfer mode if necessary */
  1813. if (ehc->i.flags & ATA_EHI_SETMODE) {
  1814. rc = ata_set_mode(ap, &dev);
  1815. if (rc)
  1816. goto dev_fail;
  1817. ehc->i.flags &= ~ATA_EHI_SETMODE;
  1818. }
  1819. /* suspend devices */
  1820. rc = ata_eh_suspend(ap, &dev);
  1821. if (rc)
  1822. goto dev_fail;
  1823. goto out;
  1824. dev_fail:
  1825. ehc->tries[dev->devno]--;
  1826. switch (rc) {
  1827. case -EINVAL:
  1828. /* eeek, something went very wrong, give up */
  1829. ehc->tries[dev->devno] = 0;
  1830. break;
  1831. case -ENODEV:
  1832. /* device missing or wrong IDENTIFY data, schedule probing */
  1833. ehc->i.probe_mask |= (1 << dev->devno);
  1834. /* give it just one more chance */
  1835. ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
  1836. case -EIO:
  1837. if (ehc->tries[dev->devno] == 1) {
  1838. /* This is the last chance, better to slow
  1839. * down than lose it.
  1840. */
  1841. sata_down_spd_limit(ap);
  1842. ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
  1843. }
  1844. }
  1845. if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
  1846. /* disable device if it has used up all its chances */
  1847. ata_dev_disable(dev);
  1848. /* detach if offline */
  1849. if (ata_port_offline(ap))
  1850. ata_eh_detach_dev(dev);
  1851. /* probe if requested */
  1852. if ((ehc->i.probe_mask & (1 << dev->devno)) &&
  1853. !(ehc->did_probe_mask & (1 << dev->devno))) {
  1854. ata_eh_detach_dev(dev);
  1855. ata_dev_init(dev);
  1856. ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
  1857. ehc->did_probe_mask |= (1 << dev->devno);
  1858. ehc->i.action |= ATA_EH_SOFTRESET;
  1859. }
  1860. } else {
  1861. /* soft didn't work? be haaaaard */
  1862. if (ehc->i.flags & ATA_EHI_DID_RESET)
  1863. ehc->i.action |= ATA_EH_HARDRESET;
  1864. else
  1865. ehc->i.action |= ATA_EH_SOFTRESET;
  1866. }
  1867. if (ata_port_nr_enabled(ap)) {
  1868. ata_port_printk(ap, KERN_WARNING, "failed to recover some "
  1869. "devices, retrying in 5 secs\n");
  1870. ssleep(5);
  1871. } else {
  1872. /* no device left, repeat fast */
  1873. msleep(500);
  1874. }
  1875. goto retry;
  1876. out:
  1877. if (rc) {
  1878. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1879. ata_dev_disable(&ap->device[i]);
  1880. }
  1881. DPRINTK("EXIT, rc=%d\n", rc);
  1882. return rc;
  1883. }
  1884. /**
  1885. * ata_eh_finish - finish up EH
  1886. * @ap: host port to finish EH for
  1887. *
  1888. * Recovery is complete. Clean up EH states and retry or finish
  1889. * failed qcs.
  1890. *
  1891. * LOCKING:
  1892. * None.
  1893. */
  1894. static void ata_eh_finish(struct ata_port *ap)
  1895. {
  1896. int tag;
  1897. /* retry or finish qcs */
  1898. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  1899. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  1900. if (!(qc->flags & ATA_QCFLAG_FAILED))
  1901. continue;
  1902. if (qc->err_mask) {
  1903. /* FIXME: Once EH migration is complete,
  1904. * generate sense data in this function,
  1905. * considering both err_mask and tf.
  1906. */
  1907. if (qc->err_mask & AC_ERR_INVALID)
  1908. ata_eh_qc_complete(qc);
  1909. else
  1910. ata_eh_qc_retry(qc);
  1911. } else {
  1912. if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
  1913. ata_eh_qc_complete(qc);
  1914. } else {
  1915. /* feed zero TF to sense generation */
  1916. memset(&qc->result_tf, 0, sizeof(qc->result_tf));
  1917. ata_eh_qc_retry(qc);
  1918. }
  1919. }
  1920. }
  1921. }
  1922. /**
  1923. * ata_do_eh - do standard error handling
  1924. * @ap: host port to handle error for
  1925. * @prereset: prereset method (can be NULL)
  1926. * @softreset: softreset method (can be NULL)
  1927. * @hardreset: hardreset method (can be NULL)
  1928. * @postreset: postreset method (can be NULL)
  1929. *
  1930. * Perform standard error handling sequence.
  1931. *
  1932. * LOCKING:
  1933. * Kernel thread context (may sleep).
  1934. */
  1935. void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
  1936. ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
  1937. ata_postreset_fn_t postreset)
  1938. {
  1939. ata_eh_autopsy(ap);
  1940. ata_eh_report(ap);
  1941. ata_eh_recover(ap, prereset, softreset, hardreset, postreset);
  1942. ata_eh_finish(ap);
  1943. }
  1944. #ifdef CONFIG_PM
  1945. /**
  1946. * ata_eh_handle_port_suspend - perform port suspend operation
  1947. * @ap: port to suspend
  1948. *
  1949. * Suspend @ap.
  1950. *
  1951. * LOCKING:
  1952. * Kernel thread context (may sleep).
  1953. */
  1954. static void ata_eh_handle_port_suspend(struct ata_port *ap)
  1955. {
  1956. unsigned long flags;
  1957. int rc = 0;
  1958. /* are we suspending? */
  1959. spin_lock_irqsave(ap->lock, flags);
  1960. if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
  1961. ap->pm_mesg.event == PM_EVENT_ON) {
  1962. spin_unlock_irqrestore(ap->lock, flags);
  1963. return;
  1964. }
  1965. spin_unlock_irqrestore(ap->lock, flags);
  1966. WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
  1967. /* suspend */
  1968. ata_eh_freeze_port(ap);
  1969. if (ap->ops->port_suspend)
  1970. rc = ap->ops->port_suspend(ap, ap->pm_mesg);
  1971. /* report result */
  1972. spin_lock_irqsave(ap->lock, flags);
  1973. ap->pflags &= ~ATA_PFLAG_PM_PENDING;
  1974. if (rc == 0)
  1975. ap->pflags |= ATA_PFLAG_SUSPENDED;
  1976. else
  1977. ata_port_schedule_eh(ap);
  1978. if (ap->pm_result) {
  1979. *ap->pm_result = rc;
  1980. ap->pm_result = NULL;
  1981. }
  1982. spin_unlock_irqrestore(ap->lock, flags);
  1983. return;
  1984. }
  1985. /**
  1986. * ata_eh_handle_port_resume - perform port resume operation
  1987. * @ap: port to resume
  1988. *
  1989. * Resume @ap.
  1990. *
  1991. * This function also waits upto one second until all devices
  1992. * hanging off this port requests resume EH action. This is to
  1993. * prevent invoking EH and thus reset multiple times on resume.
  1994. *
  1995. * On DPM resume, where some of devices might not be resumed
  1996. * together, this may delay port resume upto one second, but such
  1997. * DPM resumes are rare and 1 sec delay isn't too bad.
  1998. *
  1999. * LOCKING:
  2000. * Kernel thread context (may sleep).
  2001. */
  2002. static void ata_eh_handle_port_resume(struct ata_port *ap)
  2003. {
  2004. unsigned long timeout;
  2005. unsigned long flags;
  2006. int i, rc = 0;
  2007. /* are we resuming? */
  2008. spin_lock_irqsave(ap->lock, flags);
  2009. if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
  2010. ap->pm_mesg.event != PM_EVENT_ON) {
  2011. spin_unlock_irqrestore(ap->lock, flags);
  2012. return;
  2013. }
  2014. spin_unlock_irqrestore(ap->lock, flags);
  2015. /* spurious? */
  2016. if (!(ap->pflags & ATA_PFLAG_SUSPENDED))
  2017. goto done;
  2018. if (ap->ops->port_resume)
  2019. rc = ap->ops->port_resume(ap);
  2020. /* give devices time to request EH */
  2021. timeout = jiffies + HZ; /* 1s max */
  2022. while (1) {
  2023. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  2024. struct ata_device *dev = &ap->device[i];
  2025. unsigned int action = ata_eh_dev_action(dev);
  2026. if ((dev->flags & ATA_DFLAG_SUSPENDED) &&
  2027. !(action & ATA_EH_RESUME))
  2028. break;
  2029. }
  2030. if (i == ATA_MAX_DEVICES || time_after(jiffies, timeout))
  2031. break;
  2032. msleep(10);
  2033. }
  2034. done:
  2035. spin_lock_irqsave(ap->lock, flags);
  2036. ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
  2037. if (ap->pm_result) {
  2038. *ap->pm_result = rc;
  2039. ap->pm_result = NULL;
  2040. }
  2041. spin_unlock_irqrestore(ap->lock, flags);
  2042. }
  2043. #endif /* CONFIG_PM */