libata-eh.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222
  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. spin_lock_irqsave(ap->lock, flags);
  645. ata_eh_clear_action(dev, &ap->eh_info, action);
  646. if (!(ap->eh_context.i.flags & ATA_EHI_QUIET))
  647. ap->pflags |= ATA_PFLAG_RECOVERED;
  648. spin_unlock_irqrestore(ap->lock, flags);
  649. }
  650. /**
  651. * ata_eh_done - EH action complete
  652. * @ap: target ATA port
  653. * @dev: target ATA dev for per-dev action (can be NULL)
  654. * @action: action just completed
  655. *
  656. * Called right after performing EH actions to clear related bits
  657. * in @ap->eh_context.
  658. *
  659. * LOCKING:
  660. * None.
  661. */
  662. static void ata_eh_done(struct ata_port *ap, struct ata_device *dev,
  663. unsigned int action)
  664. {
  665. ata_eh_clear_action(dev, &ap->eh_context.i, action);
  666. }
  667. /**
  668. * ata_err_string - convert err_mask to descriptive string
  669. * @err_mask: error mask to convert to string
  670. *
  671. * Convert @err_mask to descriptive string. Errors are
  672. * prioritized according to severity and only the most severe
  673. * error is reported.
  674. *
  675. * LOCKING:
  676. * None.
  677. *
  678. * RETURNS:
  679. * Descriptive string for @err_mask
  680. */
  681. static const char * ata_err_string(unsigned int err_mask)
  682. {
  683. if (err_mask & AC_ERR_HOST_BUS)
  684. return "host bus error";
  685. if (err_mask & AC_ERR_ATA_BUS)
  686. return "ATA bus error";
  687. if (err_mask & AC_ERR_TIMEOUT)
  688. return "timeout";
  689. if (err_mask & AC_ERR_HSM)
  690. return "HSM violation";
  691. if (err_mask & AC_ERR_SYSTEM)
  692. return "internal error";
  693. if (err_mask & AC_ERR_MEDIA)
  694. return "media error";
  695. if (err_mask & AC_ERR_INVALID)
  696. return "invalid argument";
  697. if (err_mask & AC_ERR_DEV)
  698. return "device error";
  699. return "unknown error";
  700. }
  701. /**
  702. * ata_read_log_page - read a specific log page
  703. * @dev: target device
  704. * @page: page to read
  705. * @buf: buffer to store read page
  706. * @sectors: number of sectors to read
  707. *
  708. * Read log page using READ_LOG_EXT command.
  709. *
  710. * LOCKING:
  711. * Kernel thread context (may sleep).
  712. *
  713. * RETURNS:
  714. * 0 on success, AC_ERR_* mask otherwise.
  715. */
  716. static unsigned int ata_read_log_page(struct ata_device *dev,
  717. u8 page, void *buf, unsigned int sectors)
  718. {
  719. struct ata_taskfile tf;
  720. unsigned int err_mask;
  721. DPRINTK("read log page - page %d\n", page);
  722. ata_tf_init(dev, &tf);
  723. tf.command = ATA_CMD_READ_LOG_EXT;
  724. tf.lbal = page;
  725. tf.nsect = sectors;
  726. tf.hob_nsect = sectors >> 8;
  727. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
  728. tf.protocol = ATA_PROT_PIO;
  729. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
  730. buf, sectors * ATA_SECT_SIZE);
  731. DPRINTK("EXIT, err_mask=%x\n", err_mask);
  732. return err_mask;
  733. }
  734. /**
  735. * ata_eh_read_log_10h - Read log page 10h for NCQ error details
  736. * @dev: Device to read log page 10h from
  737. * @tag: Resulting tag of the failed command
  738. * @tf: Resulting taskfile registers of the failed command
  739. *
  740. * Read log page 10h to obtain NCQ error details and clear error
  741. * condition.
  742. *
  743. * LOCKING:
  744. * Kernel thread context (may sleep).
  745. *
  746. * RETURNS:
  747. * 0 on success, -errno otherwise.
  748. */
  749. static int ata_eh_read_log_10h(struct ata_device *dev,
  750. int *tag, struct ata_taskfile *tf)
  751. {
  752. u8 *buf = dev->ap->sector_buf;
  753. unsigned int err_mask;
  754. u8 csum;
  755. int i;
  756. err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
  757. if (err_mask)
  758. return -EIO;
  759. csum = 0;
  760. for (i = 0; i < ATA_SECT_SIZE; i++)
  761. csum += buf[i];
  762. if (csum)
  763. ata_dev_printk(dev, KERN_WARNING,
  764. "invalid checksum 0x%x on log page 10h\n", csum);
  765. if (buf[0] & 0x80)
  766. return -ENOENT;
  767. *tag = buf[0] & 0x1f;
  768. tf->command = buf[2];
  769. tf->feature = buf[3];
  770. tf->lbal = buf[4];
  771. tf->lbam = buf[5];
  772. tf->lbah = buf[6];
  773. tf->device = buf[7];
  774. tf->hob_lbal = buf[8];
  775. tf->hob_lbam = buf[9];
  776. tf->hob_lbah = buf[10];
  777. tf->nsect = buf[12];
  778. tf->hob_nsect = buf[13];
  779. return 0;
  780. }
  781. /**
  782. * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
  783. * @dev: device to perform REQUEST_SENSE to
  784. * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
  785. *
  786. * Perform ATAPI REQUEST_SENSE after the device reported CHECK
  787. * SENSE. This function is EH helper.
  788. *
  789. * LOCKING:
  790. * Kernel thread context (may sleep).
  791. *
  792. * RETURNS:
  793. * 0 on success, AC_ERR_* mask on failure
  794. */
  795. static unsigned int atapi_eh_request_sense(struct ata_device *dev,
  796. unsigned char *sense_buf)
  797. {
  798. struct ata_port *ap = dev->ap;
  799. struct ata_taskfile tf;
  800. u8 cdb[ATAPI_CDB_LEN];
  801. DPRINTK("ATAPI request sense\n");
  802. ata_tf_init(dev, &tf);
  803. /* FIXME: is this needed? */
  804. memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
  805. /* XXX: why tf_read here? */
  806. ap->ops->tf_read(ap, &tf);
  807. /* fill these in, for the case where they are -not- overwritten */
  808. sense_buf[0] = 0x70;
  809. sense_buf[2] = tf.feature >> 4;
  810. memset(cdb, 0, ATAPI_CDB_LEN);
  811. cdb[0] = REQUEST_SENSE;
  812. cdb[4] = SCSI_SENSE_BUFFERSIZE;
  813. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  814. tf.command = ATA_CMD_PACKET;
  815. /* is it pointless to prefer PIO for "safety reasons"? */
  816. if (ap->flags & ATA_FLAG_PIO_DMA) {
  817. tf.protocol = ATA_PROT_ATAPI_DMA;
  818. tf.feature |= ATAPI_PKT_DMA;
  819. } else {
  820. tf.protocol = ATA_PROT_ATAPI;
  821. tf.lbam = (8 * 1024) & 0xff;
  822. tf.lbah = (8 * 1024) >> 8;
  823. }
  824. return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
  825. sense_buf, SCSI_SENSE_BUFFERSIZE);
  826. }
  827. /**
  828. * ata_eh_analyze_serror - analyze SError for a failed port
  829. * @ap: ATA port to analyze SError for
  830. *
  831. * Analyze SError if available and further determine cause of
  832. * failure.
  833. *
  834. * LOCKING:
  835. * None.
  836. */
  837. static void ata_eh_analyze_serror(struct ata_port *ap)
  838. {
  839. struct ata_eh_context *ehc = &ap->eh_context;
  840. u32 serror = ehc->i.serror;
  841. unsigned int err_mask = 0, action = 0;
  842. if (serror & SERR_PERSISTENT) {
  843. err_mask |= AC_ERR_ATA_BUS;
  844. action |= ATA_EH_HARDRESET;
  845. }
  846. if (serror &
  847. (SERR_DATA_RECOVERED | SERR_COMM_RECOVERED | SERR_DATA)) {
  848. err_mask |= AC_ERR_ATA_BUS;
  849. action |= ATA_EH_SOFTRESET;
  850. }
  851. if (serror & SERR_PROTOCOL) {
  852. err_mask |= AC_ERR_HSM;
  853. action |= ATA_EH_SOFTRESET;
  854. }
  855. if (serror & SERR_INTERNAL) {
  856. err_mask |= AC_ERR_SYSTEM;
  857. action |= ATA_EH_SOFTRESET;
  858. }
  859. if (serror & (SERR_PHYRDY_CHG | SERR_DEV_XCHG))
  860. ata_ehi_hotplugged(&ehc->i);
  861. ehc->i.err_mask |= err_mask;
  862. ehc->i.action |= action;
  863. }
  864. /**
  865. * ata_eh_analyze_ncq_error - analyze NCQ error
  866. * @ap: ATA port to analyze NCQ error for
  867. *
  868. * Read log page 10h, determine the offending qc and acquire
  869. * error status TF. For NCQ device errors, all LLDDs have to do
  870. * is setting AC_ERR_DEV in ehi->err_mask. This function takes
  871. * care of the rest.
  872. *
  873. * LOCKING:
  874. * Kernel thread context (may sleep).
  875. */
  876. static void ata_eh_analyze_ncq_error(struct ata_port *ap)
  877. {
  878. struct ata_eh_context *ehc = &ap->eh_context;
  879. struct ata_device *dev = ap->device;
  880. struct ata_queued_cmd *qc;
  881. struct ata_taskfile tf;
  882. int tag, rc;
  883. /* if frozen, we can't do much */
  884. if (ap->pflags & ATA_PFLAG_FROZEN)
  885. return;
  886. /* is it NCQ device error? */
  887. if (!ap->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
  888. return;
  889. /* has LLDD analyzed already? */
  890. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  891. qc = __ata_qc_from_tag(ap, tag);
  892. if (!(qc->flags & ATA_QCFLAG_FAILED))
  893. continue;
  894. if (qc->err_mask)
  895. return;
  896. }
  897. /* okay, this error is ours */
  898. rc = ata_eh_read_log_10h(dev, &tag, &tf);
  899. if (rc) {
  900. ata_port_printk(ap, KERN_ERR, "failed to read log page 10h "
  901. "(errno=%d)\n", rc);
  902. return;
  903. }
  904. if (!(ap->sactive & (1 << tag))) {
  905. ata_port_printk(ap, KERN_ERR, "log page 10h reported "
  906. "inactive tag %d\n", tag);
  907. return;
  908. }
  909. /* we've got the perpetrator, condemn it */
  910. qc = __ata_qc_from_tag(ap, tag);
  911. memcpy(&qc->result_tf, &tf, sizeof(tf));
  912. qc->err_mask |= AC_ERR_DEV;
  913. ehc->i.err_mask &= ~AC_ERR_DEV;
  914. }
  915. /**
  916. * ata_eh_analyze_tf - analyze taskfile of a failed qc
  917. * @qc: qc to analyze
  918. * @tf: Taskfile registers to analyze
  919. *
  920. * Analyze taskfile of @qc and further determine cause of
  921. * failure. This function also requests ATAPI sense data if
  922. * avaliable.
  923. *
  924. * LOCKING:
  925. * Kernel thread context (may sleep).
  926. *
  927. * RETURNS:
  928. * Determined recovery action
  929. */
  930. static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
  931. const struct ata_taskfile *tf)
  932. {
  933. unsigned int tmp, action = 0;
  934. u8 stat = tf->command, err = tf->feature;
  935. if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
  936. qc->err_mask |= AC_ERR_HSM;
  937. return ATA_EH_SOFTRESET;
  938. }
  939. if (!(qc->err_mask & AC_ERR_DEV))
  940. return 0;
  941. switch (qc->dev->class) {
  942. case ATA_DEV_ATA:
  943. if (err & ATA_ICRC)
  944. qc->err_mask |= AC_ERR_ATA_BUS;
  945. if (err & ATA_UNC)
  946. qc->err_mask |= AC_ERR_MEDIA;
  947. if (err & ATA_IDNF)
  948. qc->err_mask |= AC_ERR_INVALID;
  949. break;
  950. case ATA_DEV_ATAPI:
  951. tmp = atapi_eh_request_sense(qc->dev,
  952. qc->scsicmd->sense_buffer);
  953. if (!tmp) {
  954. /* ATA_QCFLAG_SENSE_VALID is used to tell
  955. * atapi_qc_complete() that sense data is
  956. * already valid.
  957. *
  958. * TODO: interpret sense data and set
  959. * appropriate err_mask.
  960. */
  961. qc->flags |= ATA_QCFLAG_SENSE_VALID;
  962. } else
  963. qc->err_mask |= tmp;
  964. }
  965. if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
  966. action |= ATA_EH_SOFTRESET;
  967. return action;
  968. }
  969. static int ata_eh_categorize_ering_entry(struct ata_ering_entry *ent)
  970. {
  971. if (ent->err_mask & (AC_ERR_ATA_BUS | AC_ERR_TIMEOUT))
  972. return 1;
  973. if (ent->is_io) {
  974. if (ent->err_mask & AC_ERR_HSM)
  975. return 1;
  976. if ((ent->err_mask &
  977. (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
  978. return 2;
  979. }
  980. return 0;
  981. }
  982. struct speed_down_needed_arg {
  983. u64 since;
  984. int nr_errors[3];
  985. };
  986. static int speed_down_needed_cb(struct ata_ering_entry *ent, void *void_arg)
  987. {
  988. struct speed_down_needed_arg *arg = void_arg;
  989. if (ent->timestamp < arg->since)
  990. return -1;
  991. arg->nr_errors[ata_eh_categorize_ering_entry(ent)]++;
  992. return 0;
  993. }
  994. /**
  995. * ata_eh_speed_down_needed - Determine wheter speed down is necessary
  996. * @dev: Device of interest
  997. *
  998. * This function examines error ring of @dev and determines
  999. * whether speed down is necessary. Speed down is necessary if
  1000. * there have been more than 3 of Cat-1 errors or 10 of Cat-2
  1001. * errors during last 15 minutes.
  1002. *
  1003. * Cat-1 errors are ATA_BUS, TIMEOUT for any command and HSM
  1004. * violation for known supported commands.
  1005. *
  1006. * Cat-2 errors are unclassified DEV error for known supported
  1007. * command.
  1008. *
  1009. * LOCKING:
  1010. * Inherited from caller.
  1011. *
  1012. * RETURNS:
  1013. * 1 if speed down is necessary, 0 otherwise
  1014. */
  1015. static int ata_eh_speed_down_needed(struct ata_device *dev)
  1016. {
  1017. const u64 interval = 15LLU * 60 * HZ;
  1018. static const int err_limits[3] = { -1, 3, 10 };
  1019. struct speed_down_needed_arg arg;
  1020. struct ata_ering_entry *ent;
  1021. int err_cat;
  1022. u64 j64;
  1023. ent = ata_ering_top(&dev->ering);
  1024. if (!ent)
  1025. return 0;
  1026. err_cat = ata_eh_categorize_ering_entry(ent);
  1027. if (err_cat == 0)
  1028. return 0;
  1029. memset(&arg, 0, sizeof(arg));
  1030. j64 = get_jiffies_64();
  1031. if (j64 >= interval)
  1032. arg.since = j64 - interval;
  1033. else
  1034. arg.since = 0;
  1035. ata_ering_map(&dev->ering, speed_down_needed_cb, &arg);
  1036. return arg.nr_errors[err_cat] > err_limits[err_cat];
  1037. }
  1038. /**
  1039. * ata_eh_speed_down - record error and speed down if necessary
  1040. * @dev: Failed device
  1041. * @is_io: Did the device fail during normal IO?
  1042. * @err_mask: err_mask of the error
  1043. *
  1044. * Record error and examine error history to determine whether
  1045. * adjusting transmission speed is necessary. It also sets
  1046. * transmission limits appropriately if such adjustment is
  1047. * necessary.
  1048. *
  1049. * LOCKING:
  1050. * Kernel thread context (may sleep).
  1051. *
  1052. * RETURNS:
  1053. * 0 on success, -errno otherwise
  1054. */
  1055. static int ata_eh_speed_down(struct ata_device *dev, int is_io,
  1056. unsigned int err_mask)
  1057. {
  1058. if (!err_mask)
  1059. return 0;
  1060. /* record error and determine whether speed down is necessary */
  1061. ata_ering_record(&dev->ering, is_io, err_mask);
  1062. if (!ata_eh_speed_down_needed(dev))
  1063. return 0;
  1064. /* speed down SATA link speed if possible */
  1065. if (sata_down_spd_limit(dev->ap) == 0)
  1066. return ATA_EH_HARDRESET;
  1067. /* lower transfer mode */
  1068. if (ata_down_xfermask_limit(dev, 0) == 0)
  1069. return ATA_EH_SOFTRESET;
  1070. ata_dev_printk(dev, KERN_ERR,
  1071. "speed down requested but no transfer mode left\n");
  1072. return 0;
  1073. }
  1074. /**
  1075. * ata_eh_autopsy - analyze error and determine recovery action
  1076. * @ap: ATA port to perform autopsy on
  1077. *
  1078. * Analyze why @ap failed and determine which recovery action is
  1079. * needed. This function also sets more detailed AC_ERR_* values
  1080. * and fills sense data for ATAPI CHECK SENSE.
  1081. *
  1082. * LOCKING:
  1083. * Kernel thread context (may sleep).
  1084. */
  1085. static void ata_eh_autopsy(struct ata_port *ap)
  1086. {
  1087. struct ata_eh_context *ehc = &ap->eh_context;
  1088. unsigned int all_err_mask = 0;
  1089. int tag, is_io = 0;
  1090. u32 serror;
  1091. int rc;
  1092. DPRINTK("ENTER\n");
  1093. if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
  1094. return;
  1095. /* obtain and analyze SError */
  1096. rc = sata_scr_read(ap, SCR_ERROR, &serror);
  1097. if (rc == 0) {
  1098. ehc->i.serror |= serror;
  1099. ata_eh_analyze_serror(ap);
  1100. } else if (rc != -EOPNOTSUPP)
  1101. ehc->i.action |= ATA_EH_HARDRESET;
  1102. /* analyze NCQ failure */
  1103. ata_eh_analyze_ncq_error(ap);
  1104. /* any real error trumps AC_ERR_OTHER */
  1105. if (ehc->i.err_mask & ~AC_ERR_OTHER)
  1106. ehc->i.err_mask &= ~AC_ERR_OTHER;
  1107. all_err_mask |= ehc->i.err_mask;
  1108. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  1109. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  1110. if (!(qc->flags & ATA_QCFLAG_FAILED))
  1111. continue;
  1112. /* inherit upper level err_mask */
  1113. qc->err_mask |= ehc->i.err_mask;
  1114. /* analyze TF */
  1115. ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
  1116. /* DEV errors are probably spurious in case of ATA_BUS error */
  1117. if (qc->err_mask & AC_ERR_ATA_BUS)
  1118. qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
  1119. AC_ERR_INVALID);
  1120. /* any real error trumps unknown error */
  1121. if (qc->err_mask & ~AC_ERR_OTHER)
  1122. qc->err_mask &= ~AC_ERR_OTHER;
  1123. /* SENSE_VALID trumps dev/unknown error and revalidation */
  1124. if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
  1125. qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
  1126. ehc->i.action &= ~ATA_EH_REVALIDATE;
  1127. }
  1128. /* accumulate error info */
  1129. ehc->i.dev = qc->dev;
  1130. all_err_mask |= qc->err_mask;
  1131. if (qc->flags & ATA_QCFLAG_IO)
  1132. is_io = 1;
  1133. }
  1134. /* enforce default EH actions */
  1135. if (ap->pflags & ATA_PFLAG_FROZEN ||
  1136. all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
  1137. ehc->i.action |= ATA_EH_SOFTRESET;
  1138. else if (all_err_mask)
  1139. ehc->i.action |= ATA_EH_REVALIDATE;
  1140. /* if we have offending qcs and the associated failed device */
  1141. if (ehc->i.dev) {
  1142. /* speed down */
  1143. ehc->i.action |= ata_eh_speed_down(ehc->i.dev, is_io,
  1144. all_err_mask);
  1145. /* perform per-dev EH action only on the offending device */
  1146. ehc->i.dev_action[ehc->i.dev->devno] |=
  1147. ehc->i.action & ATA_EH_PERDEV_MASK;
  1148. ehc->i.action &= ~ATA_EH_PERDEV_MASK;
  1149. }
  1150. DPRINTK("EXIT\n");
  1151. }
  1152. /**
  1153. * ata_eh_report - report error handling to user
  1154. * @ap: ATA port EH is going on
  1155. *
  1156. * Report EH to user.
  1157. *
  1158. * LOCKING:
  1159. * None.
  1160. */
  1161. static void ata_eh_report(struct ata_port *ap)
  1162. {
  1163. struct ata_eh_context *ehc = &ap->eh_context;
  1164. const char *frozen, *desc;
  1165. int tag, nr_failed = 0;
  1166. desc = NULL;
  1167. if (ehc->i.desc[0] != '\0')
  1168. desc = ehc->i.desc;
  1169. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  1170. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  1171. if (!(qc->flags & ATA_QCFLAG_FAILED))
  1172. continue;
  1173. if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
  1174. continue;
  1175. nr_failed++;
  1176. }
  1177. if (!nr_failed && !ehc->i.err_mask)
  1178. return;
  1179. frozen = "";
  1180. if (ap->pflags & ATA_PFLAG_FROZEN)
  1181. frozen = " frozen";
  1182. if (ehc->i.dev) {
  1183. ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
  1184. "SAct 0x%x SErr 0x%x action 0x%x%s\n",
  1185. ehc->i.err_mask, ap->sactive, ehc->i.serror,
  1186. ehc->i.action, frozen);
  1187. if (desc)
  1188. ata_dev_printk(ehc->i.dev, KERN_ERR, "(%s)\n", desc);
  1189. } else {
  1190. ata_port_printk(ap, KERN_ERR, "exception Emask 0x%x "
  1191. "SAct 0x%x SErr 0x%x action 0x%x%s\n",
  1192. ehc->i.err_mask, ap->sactive, ehc->i.serror,
  1193. ehc->i.action, frozen);
  1194. if (desc)
  1195. ata_port_printk(ap, KERN_ERR, "(%s)\n", desc);
  1196. }
  1197. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  1198. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  1199. if (!(qc->flags & ATA_QCFLAG_FAILED) || !qc->err_mask)
  1200. continue;
  1201. ata_dev_printk(qc->dev, KERN_ERR, "tag %d cmd 0x%x "
  1202. "Emask 0x%x stat 0x%x err 0x%x (%s)\n",
  1203. qc->tag, qc->tf.command, qc->err_mask,
  1204. qc->result_tf.command, qc->result_tf.feature,
  1205. ata_err_string(qc->err_mask));
  1206. }
  1207. }
  1208. static int ata_do_reset(struct ata_port *ap, ata_reset_fn_t reset,
  1209. unsigned int *classes)
  1210. {
  1211. int i, rc;
  1212. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1213. classes[i] = ATA_DEV_UNKNOWN;
  1214. rc = reset(ap, classes);
  1215. if (rc)
  1216. return rc;
  1217. /* If any class isn't ATA_DEV_UNKNOWN, consider classification
  1218. * is complete and convert all ATA_DEV_UNKNOWN to
  1219. * ATA_DEV_NONE.
  1220. */
  1221. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1222. if (classes[i] != ATA_DEV_UNKNOWN)
  1223. break;
  1224. if (i < ATA_MAX_DEVICES)
  1225. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1226. if (classes[i] == ATA_DEV_UNKNOWN)
  1227. classes[i] = ATA_DEV_NONE;
  1228. return 0;
  1229. }
  1230. static int ata_eh_followup_srst_needed(int rc, int classify,
  1231. const unsigned int *classes)
  1232. {
  1233. if (rc == -EAGAIN)
  1234. return 1;
  1235. if (rc != 0)
  1236. return 0;
  1237. if (classify && classes[0] == ATA_DEV_UNKNOWN)
  1238. return 1;
  1239. return 0;
  1240. }
  1241. static int ata_eh_reset(struct ata_port *ap, int classify,
  1242. ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
  1243. ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
  1244. {
  1245. struct ata_eh_context *ehc = &ap->eh_context;
  1246. unsigned int *classes = ehc->classes;
  1247. int tries = ATA_EH_RESET_TRIES;
  1248. int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
  1249. unsigned int action;
  1250. ata_reset_fn_t reset;
  1251. int i, did_followup_srst, rc;
  1252. /* Determine which reset to use and record in ehc->i.action.
  1253. * prereset() may examine and modify it.
  1254. */
  1255. action = ehc->i.action;
  1256. ehc->i.action &= ~ATA_EH_RESET_MASK;
  1257. if (softreset && (!hardreset || (!sata_set_spd_needed(ap) &&
  1258. !(action & ATA_EH_HARDRESET))))
  1259. ehc->i.action |= ATA_EH_SOFTRESET;
  1260. else
  1261. ehc->i.action |= ATA_EH_HARDRESET;
  1262. if (prereset) {
  1263. rc = prereset(ap);
  1264. if (rc) {
  1265. ata_port_printk(ap, KERN_ERR,
  1266. "prereset failed (errno=%d)\n", rc);
  1267. return rc;
  1268. }
  1269. }
  1270. /* prereset() might have modified ehc->i.action */
  1271. if (ehc->i.action & ATA_EH_HARDRESET)
  1272. reset = hardreset;
  1273. else if (ehc->i.action & ATA_EH_SOFTRESET)
  1274. reset = softreset;
  1275. else {
  1276. /* prereset told us not to reset, bang classes and return */
  1277. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1278. classes[i] = ATA_DEV_NONE;
  1279. return 0;
  1280. }
  1281. /* did prereset() screw up? if so, fix up to avoid oopsing */
  1282. if (!reset) {
  1283. ata_port_printk(ap, KERN_ERR, "BUG: prereset() requested "
  1284. "invalid reset type\n");
  1285. if (softreset)
  1286. reset = softreset;
  1287. else
  1288. reset = hardreset;
  1289. }
  1290. retry:
  1291. /* shut up during boot probing */
  1292. if (verbose)
  1293. ata_port_printk(ap, KERN_INFO, "%s resetting port\n",
  1294. reset == softreset ? "soft" : "hard");
  1295. /* reset */
  1296. ata_eh_about_to_do(ap, NULL, ATA_EH_RESET_MASK);
  1297. ehc->i.flags |= ATA_EHI_DID_RESET;
  1298. rc = ata_do_reset(ap, reset, classes);
  1299. did_followup_srst = 0;
  1300. if (reset == hardreset &&
  1301. ata_eh_followup_srst_needed(rc, classify, classes)) {
  1302. /* okay, let's do follow-up softreset */
  1303. did_followup_srst = 1;
  1304. reset = softreset;
  1305. if (!reset) {
  1306. ata_port_printk(ap, KERN_ERR,
  1307. "follow-up softreset required "
  1308. "but no softreset avaliable\n");
  1309. return -EINVAL;
  1310. }
  1311. ata_eh_about_to_do(ap, NULL, ATA_EH_RESET_MASK);
  1312. rc = ata_do_reset(ap, reset, classes);
  1313. if (rc == 0 && classify &&
  1314. classes[0] == ATA_DEV_UNKNOWN) {
  1315. ata_port_printk(ap, KERN_ERR,
  1316. "classification failed\n");
  1317. return -EINVAL;
  1318. }
  1319. }
  1320. if (rc && --tries) {
  1321. const char *type;
  1322. if (reset == softreset) {
  1323. if (did_followup_srst)
  1324. type = "follow-up soft";
  1325. else
  1326. type = "soft";
  1327. } else
  1328. type = "hard";
  1329. ata_port_printk(ap, KERN_WARNING,
  1330. "%sreset failed, retrying in 5 secs\n", type);
  1331. ssleep(5);
  1332. if (reset == hardreset)
  1333. sata_down_spd_limit(ap);
  1334. if (hardreset)
  1335. reset = hardreset;
  1336. goto retry;
  1337. }
  1338. if (rc == 0) {
  1339. /* After the reset, the device state is PIO 0 and the
  1340. * controller state is undefined. Record the mode.
  1341. */
  1342. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1343. ap->device[i].pio_mode = XFER_PIO_0;
  1344. if (postreset)
  1345. postreset(ap, classes);
  1346. /* reset successful, schedule revalidation */
  1347. ata_eh_done(ap, NULL, ATA_EH_RESET_MASK);
  1348. ehc->i.action |= ATA_EH_REVALIDATE;
  1349. }
  1350. return rc;
  1351. }
  1352. static int ata_eh_revalidate_and_attach(struct ata_port *ap,
  1353. struct ata_device **r_failed_dev)
  1354. {
  1355. struct ata_eh_context *ehc = &ap->eh_context;
  1356. struct ata_device *dev;
  1357. unsigned long flags;
  1358. int i, rc = 0;
  1359. DPRINTK("ENTER\n");
  1360. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1361. unsigned int action;
  1362. dev = &ap->device[i];
  1363. action = ata_eh_dev_action(dev);
  1364. if (action & ATA_EH_REVALIDATE && ata_dev_ready(dev)) {
  1365. if (ata_port_offline(ap)) {
  1366. rc = -EIO;
  1367. break;
  1368. }
  1369. ata_eh_about_to_do(ap, dev, ATA_EH_REVALIDATE);
  1370. rc = ata_dev_revalidate(dev,
  1371. ehc->i.flags & ATA_EHI_DID_RESET);
  1372. if (rc)
  1373. break;
  1374. ata_eh_done(ap, dev, ATA_EH_REVALIDATE);
  1375. /* schedule the scsi_rescan_device() here */
  1376. queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
  1377. } else if (dev->class == ATA_DEV_UNKNOWN &&
  1378. ehc->tries[dev->devno] &&
  1379. ata_class_enabled(ehc->classes[dev->devno])) {
  1380. dev->class = ehc->classes[dev->devno];
  1381. rc = ata_dev_read_id(dev, &dev->class, 1, dev->id);
  1382. if (rc == 0)
  1383. rc = ata_dev_configure(dev, 1);
  1384. if (rc) {
  1385. dev->class = ATA_DEV_UNKNOWN;
  1386. break;
  1387. }
  1388. spin_lock_irqsave(ap->lock, flags);
  1389. ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
  1390. spin_unlock_irqrestore(ap->lock, flags);
  1391. }
  1392. }
  1393. if (rc)
  1394. *r_failed_dev = dev;
  1395. DPRINTK("EXIT\n");
  1396. return rc;
  1397. }
  1398. /**
  1399. * ata_eh_suspend - handle suspend EH action
  1400. * @ap: target host port
  1401. * @r_failed_dev: result parameter to indicate failing device
  1402. *
  1403. * Handle suspend EH action. Disk devices are spinned down and
  1404. * other types of devices are just marked suspended. Once
  1405. * suspended, no EH action to the device is allowed until it is
  1406. * resumed.
  1407. *
  1408. * LOCKING:
  1409. * Kernel thread context (may sleep).
  1410. *
  1411. * RETURNS:
  1412. * 0 on success, -errno otherwise
  1413. */
  1414. static int ata_eh_suspend(struct ata_port *ap, struct ata_device **r_failed_dev)
  1415. {
  1416. struct ata_device *dev;
  1417. int i, rc = 0;
  1418. DPRINTK("ENTER\n");
  1419. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1420. unsigned long flags;
  1421. unsigned int action, err_mask;
  1422. dev = &ap->device[i];
  1423. action = ata_eh_dev_action(dev);
  1424. if (!ata_dev_enabled(dev) || !(action & ATA_EH_SUSPEND))
  1425. continue;
  1426. WARN_ON(dev->flags & ATA_DFLAG_SUSPENDED);
  1427. ata_eh_about_to_do(ap, dev, ATA_EH_SUSPEND);
  1428. if (dev->class == ATA_DEV_ATA && !(action & ATA_EH_PM_FREEZE)) {
  1429. /* flush cache */
  1430. rc = ata_flush_cache(dev);
  1431. if (rc)
  1432. break;
  1433. /* spin down */
  1434. err_mask = ata_do_simple_cmd(dev, ATA_CMD_STANDBYNOW1);
  1435. if (err_mask) {
  1436. ata_dev_printk(dev, KERN_ERR, "failed to "
  1437. "spin down (err_mask=0x%x)\n",
  1438. err_mask);
  1439. rc = -EIO;
  1440. break;
  1441. }
  1442. }
  1443. spin_lock_irqsave(ap->lock, flags);
  1444. dev->flags |= ATA_DFLAG_SUSPENDED;
  1445. spin_unlock_irqrestore(ap->lock, flags);
  1446. ata_eh_done(ap, dev, ATA_EH_SUSPEND);
  1447. }
  1448. if (rc)
  1449. *r_failed_dev = dev;
  1450. DPRINTK("EXIT\n");
  1451. return 0;
  1452. }
  1453. /**
  1454. * ata_eh_prep_resume - prep for resume EH action
  1455. * @ap: target host port
  1456. *
  1457. * Clear SUSPENDED in preparation for scheduled resume actions.
  1458. * This allows other parts of EH to access the devices being
  1459. * resumed.
  1460. *
  1461. * LOCKING:
  1462. * Kernel thread context (may sleep).
  1463. */
  1464. static void ata_eh_prep_resume(struct ata_port *ap)
  1465. {
  1466. struct ata_device *dev;
  1467. unsigned long flags;
  1468. int i;
  1469. DPRINTK("ENTER\n");
  1470. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1471. unsigned int action;
  1472. dev = &ap->device[i];
  1473. action = ata_eh_dev_action(dev);
  1474. if (!ata_dev_enabled(dev) || !(action & ATA_EH_RESUME))
  1475. continue;
  1476. spin_lock_irqsave(ap->lock, flags);
  1477. dev->flags &= ~ATA_DFLAG_SUSPENDED;
  1478. spin_unlock_irqrestore(ap->lock, flags);
  1479. }
  1480. DPRINTK("EXIT\n");
  1481. }
  1482. /**
  1483. * ata_eh_resume - handle resume EH action
  1484. * @ap: target host port
  1485. * @r_failed_dev: result parameter to indicate failing device
  1486. *
  1487. * Handle resume EH action. Target devices are already reset and
  1488. * revalidated. Spinning up is the only operation left.
  1489. *
  1490. * LOCKING:
  1491. * Kernel thread context (may sleep).
  1492. *
  1493. * RETURNS:
  1494. * 0 on success, -errno otherwise
  1495. */
  1496. static int ata_eh_resume(struct ata_port *ap, struct ata_device **r_failed_dev)
  1497. {
  1498. struct ata_device *dev;
  1499. int i, rc = 0;
  1500. DPRINTK("ENTER\n");
  1501. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1502. unsigned int action, err_mask;
  1503. dev = &ap->device[i];
  1504. action = ata_eh_dev_action(dev);
  1505. if (!ata_dev_enabled(dev) || !(action & ATA_EH_RESUME))
  1506. continue;
  1507. ata_eh_about_to_do(ap, dev, ATA_EH_RESUME);
  1508. if (dev->class == ATA_DEV_ATA && !(action & ATA_EH_PM_FREEZE)) {
  1509. err_mask = ata_do_simple_cmd(dev,
  1510. ATA_CMD_IDLEIMMEDIATE);
  1511. if (err_mask) {
  1512. ata_dev_printk(dev, KERN_ERR, "failed to "
  1513. "spin up (err_mask=0x%x)\n",
  1514. err_mask);
  1515. rc = -EIO;
  1516. break;
  1517. }
  1518. }
  1519. ata_eh_done(ap, dev, ATA_EH_RESUME);
  1520. }
  1521. if (rc)
  1522. *r_failed_dev = dev;
  1523. DPRINTK("EXIT\n");
  1524. return 0;
  1525. }
  1526. static int ata_port_nr_enabled(struct ata_port *ap)
  1527. {
  1528. int i, cnt = 0;
  1529. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1530. if (ata_dev_enabled(&ap->device[i]))
  1531. cnt++;
  1532. return cnt;
  1533. }
  1534. static int ata_port_nr_vacant(struct ata_port *ap)
  1535. {
  1536. int i, cnt = 0;
  1537. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1538. if (ap->device[i].class == ATA_DEV_UNKNOWN)
  1539. cnt++;
  1540. return cnt;
  1541. }
  1542. static int ata_eh_skip_recovery(struct ata_port *ap)
  1543. {
  1544. struct ata_eh_context *ehc = &ap->eh_context;
  1545. int i;
  1546. /* skip if all possible devices are suspended */
  1547. for (i = 0; i < ata_port_max_devices(ap); i++) {
  1548. struct ata_device *dev = &ap->device[i];
  1549. if (ata_dev_absent(dev) || ata_dev_ready(dev))
  1550. break;
  1551. }
  1552. if (i == ata_port_max_devices(ap))
  1553. return 1;
  1554. /* always thaw frozen port and recover failed devices */
  1555. if (ap->pflags & ATA_PFLAG_FROZEN || ata_port_nr_enabled(ap))
  1556. return 0;
  1557. /* skip if class codes for all vacant slots are ATA_DEV_NONE */
  1558. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1559. struct ata_device *dev = &ap->device[i];
  1560. if (dev->class == ATA_DEV_UNKNOWN &&
  1561. ehc->classes[dev->devno] != ATA_DEV_NONE)
  1562. return 0;
  1563. }
  1564. return 1;
  1565. }
  1566. /**
  1567. * ata_eh_recover - recover host port after error
  1568. * @ap: host port to recover
  1569. * @prereset: prereset method (can be NULL)
  1570. * @softreset: softreset method (can be NULL)
  1571. * @hardreset: hardreset method (can be NULL)
  1572. * @postreset: postreset method (can be NULL)
  1573. *
  1574. * This is the alpha and omega, eum and yang, heart and soul of
  1575. * libata exception handling. On entry, actions required to
  1576. * recover the port and hotplug requests are recorded in
  1577. * eh_context. This function executes all the operations with
  1578. * appropriate retrials and fallbacks to resurrect failed
  1579. * devices, detach goners and greet newcomers.
  1580. *
  1581. * LOCKING:
  1582. * Kernel thread context (may sleep).
  1583. *
  1584. * RETURNS:
  1585. * 0 on success, -errno on failure.
  1586. */
  1587. static int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
  1588. ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
  1589. ata_postreset_fn_t postreset)
  1590. {
  1591. struct ata_eh_context *ehc = &ap->eh_context;
  1592. struct ata_device *dev;
  1593. int down_xfermask, i, rc;
  1594. DPRINTK("ENTER\n");
  1595. /* prep for recovery */
  1596. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1597. dev = &ap->device[i];
  1598. ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
  1599. /* process hotplug request */
  1600. if (dev->flags & ATA_DFLAG_DETACH)
  1601. ata_eh_detach_dev(dev);
  1602. if (!ata_dev_enabled(dev) &&
  1603. ((ehc->i.probe_mask & (1 << dev->devno)) &&
  1604. !(ehc->did_probe_mask & (1 << dev->devno)))) {
  1605. ata_eh_detach_dev(dev);
  1606. ata_dev_init(dev);
  1607. ehc->did_probe_mask |= (1 << dev->devno);
  1608. ehc->i.action |= ATA_EH_SOFTRESET;
  1609. }
  1610. }
  1611. retry:
  1612. down_xfermask = 0;
  1613. rc = 0;
  1614. /* if UNLOADING, finish immediately */
  1615. if (ap->pflags & ATA_PFLAG_UNLOADING)
  1616. goto out;
  1617. /* prep for resume */
  1618. ata_eh_prep_resume(ap);
  1619. /* skip EH if possible. */
  1620. if (ata_eh_skip_recovery(ap))
  1621. ehc->i.action = 0;
  1622. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1623. ehc->classes[i] = ATA_DEV_UNKNOWN;
  1624. /* reset */
  1625. if (ehc->i.action & ATA_EH_RESET_MASK) {
  1626. ata_eh_freeze_port(ap);
  1627. rc = ata_eh_reset(ap, ata_port_nr_vacant(ap), prereset,
  1628. softreset, hardreset, postreset);
  1629. if (rc) {
  1630. ata_port_printk(ap, KERN_ERR,
  1631. "reset failed, giving up\n");
  1632. goto out;
  1633. }
  1634. ata_eh_thaw_port(ap);
  1635. }
  1636. /* revalidate existing devices and attach new ones */
  1637. rc = ata_eh_revalidate_and_attach(ap, &dev);
  1638. if (rc)
  1639. goto dev_fail;
  1640. /* resume devices */
  1641. rc = ata_eh_resume(ap, &dev);
  1642. if (rc)
  1643. goto dev_fail;
  1644. /* configure transfer mode if the port has been reset */
  1645. if (ehc->i.flags & ATA_EHI_DID_RESET) {
  1646. rc = ata_set_mode(ap, &dev);
  1647. if (rc) {
  1648. down_xfermask = 1;
  1649. goto dev_fail;
  1650. }
  1651. }
  1652. /* suspend devices */
  1653. rc = ata_eh_suspend(ap, &dev);
  1654. if (rc)
  1655. goto dev_fail;
  1656. goto out;
  1657. dev_fail:
  1658. switch (rc) {
  1659. case -ENODEV:
  1660. /* device missing, schedule probing */
  1661. ehc->i.probe_mask |= (1 << dev->devno);
  1662. case -EINVAL:
  1663. ehc->tries[dev->devno] = 0;
  1664. break;
  1665. case -EIO:
  1666. sata_down_spd_limit(ap);
  1667. default:
  1668. ehc->tries[dev->devno]--;
  1669. if (down_xfermask &&
  1670. ata_down_xfermask_limit(dev, ehc->tries[dev->devno] == 1))
  1671. ehc->tries[dev->devno] = 0;
  1672. }
  1673. if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
  1674. /* disable device if it has used up all its chances */
  1675. ata_dev_disable(dev);
  1676. /* detach if offline */
  1677. if (ata_port_offline(ap))
  1678. ata_eh_detach_dev(dev);
  1679. /* probe if requested */
  1680. if ((ehc->i.probe_mask & (1 << dev->devno)) &&
  1681. !(ehc->did_probe_mask & (1 << dev->devno))) {
  1682. ata_eh_detach_dev(dev);
  1683. ata_dev_init(dev);
  1684. ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
  1685. ehc->did_probe_mask |= (1 << dev->devno);
  1686. ehc->i.action |= ATA_EH_SOFTRESET;
  1687. }
  1688. } else {
  1689. /* soft didn't work? be haaaaard */
  1690. if (ehc->i.flags & ATA_EHI_DID_RESET)
  1691. ehc->i.action |= ATA_EH_HARDRESET;
  1692. else
  1693. ehc->i.action |= ATA_EH_SOFTRESET;
  1694. }
  1695. if (ata_port_nr_enabled(ap)) {
  1696. ata_port_printk(ap, KERN_WARNING, "failed to recover some "
  1697. "devices, retrying in 5 secs\n");
  1698. ssleep(5);
  1699. } else {
  1700. /* no device left, repeat fast */
  1701. msleep(500);
  1702. }
  1703. goto retry;
  1704. out:
  1705. if (rc) {
  1706. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1707. ata_dev_disable(&ap->device[i]);
  1708. }
  1709. DPRINTK("EXIT, rc=%d\n", rc);
  1710. return rc;
  1711. }
  1712. /**
  1713. * ata_eh_finish - finish up EH
  1714. * @ap: host port to finish EH for
  1715. *
  1716. * Recovery is complete. Clean up EH states and retry or finish
  1717. * failed qcs.
  1718. *
  1719. * LOCKING:
  1720. * None.
  1721. */
  1722. static void ata_eh_finish(struct ata_port *ap)
  1723. {
  1724. int tag;
  1725. /* retry or finish qcs */
  1726. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  1727. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  1728. if (!(qc->flags & ATA_QCFLAG_FAILED))
  1729. continue;
  1730. if (qc->err_mask) {
  1731. /* FIXME: Once EH migration is complete,
  1732. * generate sense data in this function,
  1733. * considering both err_mask and tf.
  1734. */
  1735. if (qc->err_mask & AC_ERR_INVALID)
  1736. ata_eh_qc_complete(qc);
  1737. else
  1738. ata_eh_qc_retry(qc);
  1739. } else {
  1740. if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
  1741. ata_eh_qc_complete(qc);
  1742. } else {
  1743. /* feed zero TF to sense generation */
  1744. memset(&qc->result_tf, 0, sizeof(qc->result_tf));
  1745. ata_eh_qc_retry(qc);
  1746. }
  1747. }
  1748. }
  1749. }
  1750. /**
  1751. * ata_do_eh - do standard error handling
  1752. * @ap: host port to handle error for
  1753. * @prereset: prereset method (can be NULL)
  1754. * @softreset: softreset method (can be NULL)
  1755. * @hardreset: hardreset method (can be NULL)
  1756. * @postreset: postreset method (can be NULL)
  1757. *
  1758. * Perform standard error handling sequence.
  1759. *
  1760. * LOCKING:
  1761. * Kernel thread context (may sleep).
  1762. */
  1763. void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
  1764. ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
  1765. ata_postreset_fn_t postreset)
  1766. {
  1767. ata_eh_autopsy(ap);
  1768. ata_eh_report(ap);
  1769. ata_eh_recover(ap, prereset, softreset, hardreset, postreset);
  1770. ata_eh_finish(ap);
  1771. }
  1772. /**
  1773. * ata_eh_handle_port_suspend - perform port suspend operation
  1774. * @ap: port to suspend
  1775. *
  1776. * Suspend @ap.
  1777. *
  1778. * LOCKING:
  1779. * Kernel thread context (may sleep).
  1780. */
  1781. static void ata_eh_handle_port_suspend(struct ata_port *ap)
  1782. {
  1783. unsigned long flags;
  1784. int rc = 0;
  1785. /* are we suspending? */
  1786. spin_lock_irqsave(ap->lock, flags);
  1787. if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
  1788. ap->pm_mesg.event == PM_EVENT_ON) {
  1789. spin_unlock_irqrestore(ap->lock, flags);
  1790. return;
  1791. }
  1792. spin_unlock_irqrestore(ap->lock, flags);
  1793. WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
  1794. /* suspend */
  1795. ata_eh_freeze_port(ap);
  1796. if (ap->ops->port_suspend)
  1797. rc = ap->ops->port_suspend(ap, ap->pm_mesg);
  1798. /* report result */
  1799. spin_lock_irqsave(ap->lock, flags);
  1800. ap->pflags &= ~ATA_PFLAG_PM_PENDING;
  1801. if (rc == 0)
  1802. ap->pflags |= ATA_PFLAG_SUSPENDED;
  1803. else
  1804. ata_port_schedule_eh(ap);
  1805. if (ap->pm_result) {
  1806. *ap->pm_result = rc;
  1807. ap->pm_result = NULL;
  1808. }
  1809. spin_unlock_irqrestore(ap->lock, flags);
  1810. return;
  1811. }
  1812. /**
  1813. * ata_eh_handle_port_resume - perform port resume operation
  1814. * @ap: port to resume
  1815. *
  1816. * Resume @ap.
  1817. *
  1818. * This function also waits upto one second until all devices
  1819. * hanging off this port requests resume EH action. This is to
  1820. * prevent invoking EH and thus reset multiple times on resume.
  1821. *
  1822. * On DPM resume, where some of devices might not be resumed
  1823. * together, this may delay port resume upto one second, but such
  1824. * DPM resumes are rare and 1 sec delay isn't too bad.
  1825. *
  1826. * LOCKING:
  1827. * Kernel thread context (may sleep).
  1828. */
  1829. static void ata_eh_handle_port_resume(struct ata_port *ap)
  1830. {
  1831. unsigned long timeout;
  1832. unsigned long flags;
  1833. int i, rc = 0;
  1834. /* are we resuming? */
  1835. spin_lock_irqsave(ap->lock, flags);
  1836. if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
  1837. ap->pm_mesg.event != PM_EVENT_ON) {
  1838. spin_unlock_irqrestore(ap->lock, flags);
  1839. return;
  1840. }
  1841. spin_unlock_irqrestore(ap->lock, flags);
  1842. /* spurious? */
  1843. if (!(ap->pflags & ATA_PFLAG_SUSPENDED))
  1844. goto done;
  1845. if (ap->ops->port_resume)
  1846. rc = ap->ops->port_resume(ap);
  1847. /* give devices time to request EH */
  1848. timeout = jiffies + HZ; /* 1s max */
  1849. while (1) {
  1850. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1851. struct ata_device *dev = &ap->device[i];
  1852. unsigned int action = ata_eh_dev_action(dev);
  1853. if ((dev->flags & ATA_DFLAG_SUSPENDED) &&
  1854. !(action & ATA_EH_RESUME))
  1855. break;
  1856. }
  1857. if (i == ATA_MAX_DEVICES || time_after(jiffies, timeout))
  1858. break;
  1859. msleep(10);
  1860. }
  1861. done:
  1862. spin_lock_irqsave(ap->lock, flags);
  1863. ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
  1864. if (ap->pm_result) {
  1865. *ap->pm_result = rc;
  1866. ap->pm_result = NULL;
  1867. }
  1868. spin_unlock_irqrestore(ap->lock, flags);
  1869. }