libata-eh.c 60 KB

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