libata-eh.c 55 KB

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