libata-eh.c 57 KB

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