libata-eh.c 63 KB

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