libata-eh.c 59 KB

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