libata-eh.c 53 KB

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