libata-eh.c 80 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179
  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 <linux/pci.h>
  36. #include <scsi/scsi.h>
  37. #include <scsi/scsi_host.h>
  38. #include <scsi/scsi_eh.h>
  39. #include <scsi/scsi_device.h>
  40. #include <scsi/scsi_cmnd.h>
  41. #include "../scsi/scsi_transport_api.h"
  42. #include <linux/libata.h>
  43. #include "libata.h"
  44. enum {
  45. /* speed down verdicts */
  46. ATA_EH_SPDN_NCQ_OFF = (1 << 0),
  47. ATA_EH_SPDN_SPEED_DOWN = (1 << 1),
  48. ATA_EH_SPDN_FALLBACK_TO_PIO = (1 << 2),
  49. ATA_EH_SPDN_KEEP_ERRORS = (1 << 3),
  50. /* error flags */
  51. ATA_EFLAG_IS_IO = (1 << 0),
  52. ATA_EFLAG_DUBIOUS_XFER = (1 << 1),
  53. /* error categories */
  54. ATA_ECAT_NONE = 0,
  55. ATA_ECAT_ATA_BUS = 1,
  56. ATA_ECAT_TOUT_HSM = 2,
  57. ATA_ECAT_UNK_DEV = 3,
  58. ATA_ECAT_DUBIOUS_NONE = 4,
  59. ATA_ECAT_DUBIOUS_ATA_BUS = 5,
  60. ATA_ECAT_DUBIOUS_TOUT_HSM = 6,
  61. ATA_ECAT_DUBIOUS_UNK_DEV = 7,
  62. ATA_ECAT_NR = 8,
  63. ATA_EH_CMD_DFL_TIMEOUT = 5000,
  64. /* always put at least this amount of time between resets */
  65. ATA_EH_RESET_COOL_DOWN = 5000,
  66. /* Waiting in ->prereset can never be reliable. It's
  67. * sometimes nice to wait there but it can't be depended upon;
  68. * otherwise, we wouldn't be resetting. Just give it enough
  69. * time for most drives to spin up.
  70. */
  71. ATA_EH_PRERESET_TIMEOUT = 10000,
  72. ATA_EH_FASTDRAIN_INTERVAL = 3000,
  73. };
  74. /* The following table determines how we sequence resets. Each entry
  75. * represents timeout for that try. The first try can be soft or
  76. * hardreset. All others are hardreset if available. In most cases
  77. * the first reset w/ 10sec timeout should succeed. Following entries
  78. * are mostly for error handling, hotplug and retarded devices.
  79. */
  80. static const unsigned long ata_eh_reset_timeouts[] = {
  81. 10000, /* most drives spin up by 10sec */
  82. 10000, /* > 99% working drives spin up before 20sec */
  83. 35000, /* give > 30 secs of idleness for retarded devices */
  84. 5000, /* and sweet one last chance */
  85. ULONG_MAX, /* > 1 min has elapsed, give up */
  86. };
  87. static const unsigned long ata_eh_identify_timeouts[] = {
  88. 5000, /* covers > 99% of successes and not too boring on failures */
  89. 10000, /* combined time till here is enough even for media access */
  90. 30000, /* for true idiots */
  91. ULONG_MAX,
  92. };
  93. static const unsigned long ata_eh_other_timeouts[] = {
  94. 5000, /* same rationale as identify timeout */
  95. 10000, /* ditto */
  96. /* but no merciful 30sec for other commands, it just isn't worth it */
  97. ULONG_MAX,
  98. };
  99. struct ata_eh_cmd_timeout_ent {
  100. const u8 *commands;
  101. const unsigned long *timeouts;
  102. };
  103. /* The following table determines timeouts to use for EH internal
  104. * commands. Each table entry is a command class and matches the
  105. * commands the entry applies to and the timeout table to use.
  106. *
  107. * On the retry after a command timed out, the next timeout value from
  108. * the table is used. If the table doesn't contain further entries,
  109. * the last value is used.
  110. *
  111. * ehc->cmd_timeout_idx keeps track of which timeout to use per
  112. * command class, so if SET_FEATURES times out on the first try, the
  113. * next try will use the second timeout value only for that class.
  114. */
  115. #define CMDS(cmds...) (const u8 []){ cmds, 0 }
  116. static const struct ata_eh_cmd_timeout_ent
  117. ata_eh_cmd_timeout_table[ATA_EH_CMD_TIMEOUT_TABLE_SIZE] = {
  118. { .commands = CMDS(ATA_CMD_ID_ATA, ATA_CMD_ID_ATAPI),
  119. .timeouts = ata_eh_identify_timeouts, },
  120. { .commands = CMDS(ATA_CMD_READ_NATIVE_MAX, ATA_CMD_READ_NATIVE_MAX_EXT),
  121. .timeouts = ata_eh_other_timeouts, },
  122. { .commands = CMDS(ATA_CMD_SET_MAX, ATA_CMD_SET_MAX_EXT),
  123. .timeouts = ata_eh_other_timeouts, },
  124. { .commands = CMDS(ATA_CMD_SET_FEATURES),
  125. .timeouts = ata_eh_other_timeouts, },
  126. { .commands = CMDS(ATA_CMD_INIT_DEV_PARAMS),
  127. .timeouts = ata_eh_other_timeouts, },
  128. };
  129. #undef CMDS
  130. static void __ata_port_freeze(struct ata_port *ap);
  131. #ifdef CONFIG_PM
  132. static void ata_eh_handle_port_suspend(struct ata_port *ap);
  133. static void ata_eh_handle_port_resume(struct ata_port *ap);
  134. #else /* CONFIG_PM */
  135. static void ata_eh_handle_port_suspend(struct ata_port *ap)
  136. { }
  137. static void ata_eh_handle_port_resume(struct ata_port *ap)
  138. { }
  139. #endif /* CONFIG_PM */
  140. static void __ata_ehi_pushv_desc(struct ata_eh_info *ehi, const char *fmt,
  141. va_list args)
  142. {
  143. ehi->desc_len += vscnprintf(ehi->desc + ehi->desc_len,
  144. ATA_EH_DESC_LEN - ehi->desc_len,
  145. fmt, args);
  146. }
  147. /**
  148. * __ata_ehi_push_desc - push error description without adding separator
  149. * @ehi: target EHI
  150. * @fmt: printf format string
  151. *
  152. * Format string according to @fmt and append it to @ehi->desc.
  153. *
  154. * LOCKING:
  155. * spin_lock_irqsave(host lock)
  156. */
  157. void __ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
  158. {
  159. va_list args;
  160. va_start(args, fmt);
  161. __ata_ehi_pushv_desc(ehi, fmt, args);
  162. va_end(args);
  163. }
  164. /**
  165. * ata_ehi_push_desc - push error description with separator
  166. * @ehi: target EHI
  167. * @fmt: printf format string
  168. *
  169. * Format string according to @fmt and append it to @ehi->desc.
  170. * If @ehi->desc is not empty, ", " is added in-between.
  171. *
  172. * LOCKING:
  173. * spin_lock_irqsave(host lock)
  174. */
  175. void ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
  176. {
  177. va_list args;
  178. if (ehi->desc_len)
  179. __ata_ehi_push_desc(ehi, ", ");
  180. va_start(args, fmt);
  181. __ata_ehi_pushv_desc(ehi, fmt, args);
  182. va_end(args);
  183. }
  184. /**
  185. * ata_ehi_clear_desc - clean error description
  186. * @ehi: target EHI
  187. *
  188. * Clear @ehi->desc.
  189. *
  190. * LOCKING:
  191. * spin_lock_irqsave(host lock)
  192. */
  193. void ata_ehi_clear_desc(struct ata_eh_info *ehi)
  194. {
  195. ehi->desc[0] = '\0';
  196. ehi->desc_len = 0;
  197. }
  198. /**
  199. * ata_port_desc - append port description
  200. * @ap: target ATA port
  201. * @fmt: printf format string
  202. *
  203. * Format string according to @fmt and append it to port
  204. * description. If port description is not empty, " " is added
  205. * in-between. This function is to be used while initializing
  206. * ata_host. The description is printed on host registration.
  207. *
  208. * LOCKING:
  209. * None.
  210. */
  211. void ata_port_desc(struct ata_port *ap, const char *fmt, ...)
  212. {
  213. va_list args;
  214. WARN_ON(!(ap->pflags & ATA_PFLAG_INITIALIZING));
  215. if (ap->link.eh_info.desc_len)
  216. __ata_ehi_push_desc(&ap->link.eh_info, " ");
  217. va_start(args, fmt);
  218. __ata_ehi_pushv_desc(&ap->link.eh_info, fmt, args);
  219. va_end(args);
  220. }
  221. #ifdef CONFIG_PCI
  222. /**
  223. * ata_port_pbar_desc - append PCI BAR description
  224. * @ap: target ATA port
  225. * @bar: target PCI BAR
  226. * @offset: offset into PCI BAR
  227. * @name: name of the area
  228. *
  229. * If @offset is negative, this function formats a string which
  230. * contains the name, address, size and type of the BAR and
  231. * appends it to the port description. If @offset is zero or
  232. * positive, only name and offsetted address is appended.
  233. *
  234. * LOCKING:
  235. * None.
  236. */
  237. void ata_port_pbar_desc(struct ata_port *ap, int bar, ssize_t offset,
  238. const char *name)
  239. {
  240. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  241. char *type = "";
  242. unsigned long long start, len;
  243. if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
  244. type = "m";
  245. else if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
  246. type = "i";
  247. start = (unsigned long long)pci_resource_start(pdev, bar);
  248. len = (unsigned long long)pci_resource_len(pdev, bar);
  249. if (offset < 0)
  250. ata_port_desc(ap, "%s %s%llu@0x%llx", name, type, len, start);
  251. else
  252. ata_port_desc(ap, "%s 0x%llx", name,
  253. start + (unsigned long long)offset);
  254. }
  255. #endif /* CONFIG_PCI */
  256. static int ata_lookup_timeout_table(u8 cmd)
  257. {
  258. int i;
  259. for (i = 0; i < ATA_EH_CMD_TIMEOUT_TABLE_SIZE; i++) {
  260. const u8 *cur;
  261. for (cur = ata_eh_cmd_timeout_table[i].commands; *cur; cur++)
  262. if (*cur == cmd)
  263. return i;
  264. }
  265. return -1;
  266. }
  267. /**
  268. * ata_internal_cmd_timeout - determine timeout for an internal command
  269. * @dev: target device
  270. * @cmd: internal command to be issued
  271. *
  272. * Determine timeout for internal command @cmd for @dev.
  273. *
  274. * LOCKING:
  275. * EH context.
  276. *
  277. * RETURNS:
  278. * Determined timeout.
  279. */
  280. unsigned long ata_internal_cmd_timeout(struct ata_device *dev, u8 cmd)
  281. {
  282. struct ata_eh_context *ehc = &dev->link->eh_context;
  283. int ent = ata_lookup_timeout_table(cmd);
  284. int idx;
  285. if (ent < 0)
  286. return ATA_EH_CMD_DFL_TIMEOUT;
  287. idx = ehc->cmd_timeout_idx[dev->devno][ent];
  288. return ata_eh_cmd_timeout_table[ent].timeouts[idx];
  289. }
  290. /**
  291. * ata_internal_cmd_timed_out - notification for internal command timeout
  292. * @dev: target device
  293. * @cmd: internal command which timed out
  294. *
  295. * Notify EH that internal command @cmd for @dev timed out. This
  296. * function should be called only for commands whose timeouts are
  297. * determined using ata_internal_cmd_timeout().
  298. *
  299. * LOCKING:
  300. * EH context.
  301. */
  302. void ata_internal_cmd_timed_out(struct ata_device *dev, u8 cmd)
  303. {
  304. struct ata_eh_context *ehc = &dev->link->eh_context;
  305. int ent = ata_lookup_timeout_table(cmd);
  306. int idx;
  307. if (ent < 0)
  308. return;
  309. idx = ehc->cmd_timeout_idx[dev->devno][ent];
  310. if (ata_eh_cmd_timeout_table[ent].timeouts[idx + 1] != ULONG_MAX)
  311. ehc->cmd_timeout_idx[dev->devno][ent]++;
  312. }
  313. static void ata_ering_record(struct ata_ering *ering, unsigned int eflags,
  314. unsigned int err_mask)
  315. {
  316. struct ata_ering_entry *ent;
  317. WARN_ON(!err_mask);
  318. ering->cursor++;
  319. ering->cursor %= ATA_ERING_SIZE;
  320. ent = &ering->ring[ering->cursor];
  321. ent->eflags = eflags;
  322. ent->err_mask = err_mask;
  323. ent->timestamp = get_jiffies_64();
  324. }
  325. static struct ata_ering_entry *ata_ering_top(struct ata_ering *ering)
  326. {
  327. struct ata_ering_entry *ent = &ering->ring[ering->cursor];
  328. if (ent->err_mask)
  329. return ent;
  330. return NULL;
  331. }
  332. static void ata_ering_clear(struct ata_ering *ering)
  333. {
  334. memset(ering, 0, sizeof(*ering));
  335. }
  336. static int ata_ering_map(struct ata_ering *ering,
  337. int (*map_fn)(struct ata_ering_entry *, void *),
  338. void *arg)
  339. {
  340. int idx, rc = 0;
  341. struct ata_ering_entry *ent;
  342. idx = ering->cursor;
  343. do {
  344. ent = &ering->ring[idx];
  345. if (!ent->err_mask)
  346. break;
  347. rc = map_fn(ent, arg);
  348. if (rc)
  349. break;
  350. idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
  351. } while (idx != ering->cursor);
  352. return rc;
  353. }
  354. static unsigned int ata_eh_dev_action(struct ata_device *dev)
  355. {
  356. struct ata_eh_context *ehc = &dev->link->eh_context;
  357. return ehc->i.action | ehc->i.dev_action[dev->devno];
  358. }
  359. static void ata_eh_clear_action(struct ata_link *link, struct ata_device *dev,
  360. struct ata_eh_info *ehi, unsigned int action)
  361. {
  362. struct ata_device *tdev;
  363. if (!dev) {
  364. ehi->action &= ~action;
  365. ata_link_for_each_dev(tdev, link)
  366. ehi->dev_action[tdev->devno] &= ~action;
  367. } else {
  368. /* doesn't make sense for port-wide EH actions */
  369. WARN_ON(!(action & ATA_EH_PERDEV_MASK));
  370. /* break ehi->action into ehi->dev_action */
  371. if (ehi->action & action) {
  372. ata_link_for_each_dev(tdev, link)
  373. ehi->dev_action[tdev->devno] |=
  374. ehi->action & action;
  375. ehi->action &= ~action;
  376. }
  377. /* turn off the specified per-dev action */
  378. ehi->dev_action[dev->devno] &= ~action;
  379. }
  380. }
  381. /**
  382. * ata_scsi_timed_out - SCSI layer time out callback
  383. * @cmd: timed out SCSI command
  384. *
  385. * Handles SCSI layer timeout. We race with normal completion of
  386. * the qc for @cmd. If the qc is already gone, we lose and let
  387. * the scsi command finish (EH_HANDLED). Otherwise, the qc has
  388. * timed out and EH should be invoked. Prevent ata_qc_complete()
  389. * from finishing it by setting EH_SCHEDULED and return
  390. * EH_NOT_HANDLED.
  391. *
  392. * TODO: kill this function once old EH is gone.
  393. *
  394. * LOCKING:
  395. * Called from timer context
  396. *
  397. * RETURNS:
  398. * EH_HANDLED or EH_NOT_HANDLED
  399. */
  400. enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
  401. {
  402. struct Scsi_Host *host = cmd->device->host;
  403. struct ata_port *ap = ata_shost_to_port(host);
  404. unsigned long flags;
  405. struct ata_queued_cmd *qc;
  406. enum scsi_eh_timer_return ret;
  407. DPRINTK("ENTER\n");
  408. if (ap->ops->error_handler) {
  409. ret = EH_NOT_HANDLED;
  410. goto out;
  411. }
  412. ret = EH_HANDLED;
  413. spin_lock_irqsave(ap->lock, flags);
  414. qc = ata_qc_from_tag(ap, ap->link.active_tag);
  415. if (qc) {
  416. WARN_ON(qc->scsicmd != cmd);
  417. qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
  418. qc->err_mask |= AC_ERR_TIMEOUT;
  419. ret = EH_NOT_HANDLED;
  420. }
  421. spin_unlock_irqrestore(ap->lock, flags);
  422. out:
  423. DPRINTK("EXIT, ret=%d\n", ret);
  424. return ret;
  425. }
  426. /**
  427. * ata_scsi_error - SCSI layer error handler callback
  428. * @host: SCSI host on which error occurred
  429. *
  430. * Handles SCSI-layer-thrown error events.
  431. *
  432. * LOCKING:
  433. * Inherited from SCSI layer (none, can sleep)
  434. *
  435. * RETURNS:
  436. * Zero.
  437. */
  438. void ata_scsi_error(struct Scsi_Host *host)
  439. {
  440. struct ata_port *ap = ata_shost_to_port(host);
  441. int i;
  442. unsigned long flags;
  443. DPRINTK("ENTER\n");
  444. /* synchronize with port task */
  445. ata_port_flush_task(ap);
  446. /* synchronize with host lock and sort out timeouts */
  447. /* For new EH, all qcs are finished in one of three ways -
  448. * normal completion, error completion, and SCSI timeout.
  449. * Both cmpletions can race against SCSI timeout. When normal
  450. * completion wins, the qc never reaches EH. When error
  451. * completion wins, the qc has ATA_QCFLAG_FAILED set.
  452. *
  453. * When SCSI timeout wins, things are a bit more complex.
  454. * Normal or error completion can occur after the timeout but
  455. * before this point. In such cases, both types of
  456. * completions are honored. A scmd is determined to have
  457. * timed out iff its associated qc is active and not failed.
  458. */
  459. if (ap->ops->error_handler) {
  460. struct scsi_cmnd *scmd, *tmp;
  461. int nr_timedout = 0;
  462. spin_lock_irqsave(ap->lock, flags);
  463. list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {
  464. struct ata_queued_cmd *qc;
  465. for (i = 0; i < ATA_MAX_QUEUE; i++) {
  466. qc = __ata_qc_from_tag(ap, i);
  467. if (qc->flags & ATA_QCFLAG_ACTIVE &&
  468. qc->scsicmd == scmd)
  469. break;
  470. }
  471. if (i < ATA_MAX_QUEUE) {
  472. /* the scmd has an associated qc */
  473. if (!(qc->flags & ATA_QCFLAG_FAILED)) {
  474. /* which hasn't failed yet, timeout */
  475. qc->err_mask |= AC_ERR_TIMEOUT;
  476. qc->flags |= ATA_QCFLAG_FAILED;
  477. nr_timedout++;
  478. }
  479. } else {
  480. /* Normal completion occurred after
  481. * SCSI timeout but before this point.
  482. * Successfully complete it.
  483. */
  484. scmd->retries = scmd->allowed;
  485. scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
  486. }
  487. }
  488. /* If we have timed out qcs. They belong to EH from
  489. * this point but the state of the controller is
  490. * unknown. Freeze the port to make sure the IRQ
  491. * handler doesn't diddle with those qcs. This must
  492. * be done atomically w.r.t. setting QCFLAG_FAILED.
  493. */
  494. if (nr_timedout)
  495. __ata_port_freeze(ap);
  496. spin_unlock_irqrestore(ap->lock, flags);
  497. /* initialize eh_tries */
  498. ap->eh_tries = ATA_EH_MAX_TRIES;
  499. } else
  500. spin_unlock_wait(ap->lock);
  501. repeat:
  502. /* invoke error handler */
  503. if (ap->ops->error_handler) {
  504. struct ata_link *link;
  505. /* kill fast drain timer */
  506. del_timer_sync(&ap->fastdrain_timer);
  507. /* process port resume request */
  508. ata_eh_handle_port_resume(ap);
  509. /* fetch & clear EH info */
  510. spin_lock_irqsave(ap->lock, flags);
  511. __ata_port_for_each_link(link, ap) {
  512. struct ata_eh_context *ehc = &link->eh_context;
  513. struct ata_device *dev;
  514. memset(&link->eh_context, 0, sizeof(link->eh_context));
  515. link->eh_context.i = link->eh_info;
  516. memset(&link->eh_info, 0, sizeof(link->eh_info));
  517. ata_link_for_each_dev(dev, link) {
  518. int devno = dev->devno;
  519. ehc->saved_xfer_mode[devno] = dev->xfer_mode;
  520. if (ata_ncq_enabled(dev))
  521. ehc->saved_ncq_enabled |= 1 << devno;
  522. }
  523. /* set last reset timestamp to some time in the past */
  524. ehc->last_reset = jiffies - 60 * HZ;
  525. }
  526. ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
  527. ap->pflags &= ~ATA_PFLAG_EH_PENDING;
  528. ap->excl_link = NULL; /* don't maintain exclusion over EH */
  529. spin_unlock_irqrestore(ap->lock, flags);
  530. /* invoke EH, skip if unloading or suspended */
  531. if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
  532. ap->ops->error_handler(ap);
  533. else
  534. ata_eh_finish(ap);
  535. /* process port suspend request */
  536. ata_eh_handle_port_suspend(ap);
  537. /* Exception might have happend after ->error_handler
  538. * recovered the port but before this point. Repeat
  539. * EH in such case.
  540. */
  541. spin_lock_irqsave(ap->lock, flags);
  542. if (ap->pflags & ATA_PFLAG_EH_PENDING) {
  543. if (--ap->eh_tries) {
  544. spin_unlock_irqrestore(ap->lock, flags);
  545. goto repeat;
  546. }
  547. ata_port_printk(ap, KERN_ERR, "EH pending after %d "
  548. "tries, giving up\n", ATA_EH_MAX_TRIES);
  549. ap->pflags &= ~ATA_PFLAG_EH_PENDING;
  550. }
  551. /* this run is complete, make sure EH info is clear */
  552. __ata_port_for_each_link(link, ap)
  553. memset(&link->eh_info, 0, sizeof(link->eh_info));
  554. /* Clear host_eh_scheduled while holding ap->lock such
  555. * that if exception occurs after this point but
  556. * before EH completion, SCSI midlayer will
  557. * re-initiate EH.
  558. */
  559. host->host_eh_scheduled = 0;
  560. spin_unlock_irqrestore(ap->lock, flags);
  561. } else {
  562. WARN_ON(ata_qc_from_tag(ap, ap->link.active_tag) == NULL);
  563. ap->ops->eng_timeout(ap);
  564. }
  565. /* finish or retry handled scmd's and clean up */
  566. WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
  567. scsi_eh_flush_done_q(&ap->eh_done_q);
  568. /* clean up */
  569. spin_lock_irqsave(ap->lock, flags);
  570. if (ap->pflags & ATA_PFLAG_LOADING)
  571. ap->pflags &= ~ATA_PFLAG_LOADING;
  572. else if (ap->pflags & ATA_PFLAG_SCSI_HOTPLUG)
  573. queue_delayed_work(ata_aux_wq, &ap->hotplug_task, 0);
  574. if (ap->pflags & ATA_PFLAG_RECOVERED)
  575. ata_port_printk(ap, KERN_INFO, "EH complete\n");
  576. ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
  577. /* tell wait_eh that we're done */
  578. ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
  579. wake_up_all(&ap->eh_wait_q);
  580. spin_unlock_irqrestore(ap->lock, flags);
  581. DPRINTK("EXIT\n");
  582. }
  583. /**
  584. * ata_port_wait_eh - Wait for the currently pending EH to complete
  585. * @ap: Port to wait EH for
  586. *
  587. * Wait until the currently pending EH is complete.
  588. *
  589. * LOCKING:
  590. * Kernel thread context (may sleep).
  591. */
  592. void ata_port_wait_eh(struct ata_port *ap)
  593. {
  594. unsigned long flags;
  595. DEFINE_WAIT(wait);
  596. retry:
  597. spin_lock_irqsave(ap->lock, flags);
  598. while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
  599. prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
  600. spin_unlock_irqrestore(ap->lock, flags);
  601. schedule();
  602. spin_lock_irqsave(ap->lock, flags);
  603. }
  604. finish_wait(&ap->eh_wait_q, &wait);
  605. spin_unlock_irqrestore(ap->lock, flags);
  606. /* make sure SCSI EH is complete */
  607. if (scsi_host_in_recovery(ap->scsi_host)) {
  608. msleep(10);
  609. goto retry;
  610. }
  611. }
  612. static int ata_eh_nr_in_flight(struct ata_port *ap)
  613. {
  614. unsigned int tag;
  615. int nr = 0;
  616. /* count only non-internal commands */
  617. for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++)
  618. if (ata_qc_from_tag(ap, tag))
  619. nr++;
  620. return nr;
  621. }
  622. void ata_eh_fastdrain_timerfn(unsigned long arg)
  623. {
  624. struct ata_port *ap = (void *)arg;
  625. unsigned long flags;
  626. int cnt;
  627. spin_lock_irqsave(ap->lock, flags);
  628. cnt = ata_eh_nr_in_flight(ap);
  629. /* are we done? */
  630. if (!cnt)
  631. goto out_unlock;
  632. if (cnt == ap->fastdrain_cnt) {
  633. unsigned int tag;
  634. /* No progress during the last interval, tag all
  635. * in-flight qcs as timed out and freeze the port.
  636. */
  637. for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++) {
  638. struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
  639. if (qc)
  640. qc->err_mask |= AC_ERR_TIMEOUT;
  641. }
  642. ata_port_freeze(ap);
  643. } else {
  644. /* some qcs have finished, give it another chance */
  645. ap->fastdrain_cnt = cnt;
  646. ap->fastdrain_timer.expires =
  647. ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL);
  648. add_timer(&ap->fastdrain_timer);
  649. }
  650. out_unlock:
  651. spin_unlock_irqrestore(ap->lock, flags);
  652. }
  653. /**
  654. * ata_eh_set_pending - set ATA_PFLAG_EH_PENDING and activate fast drain
  655. * @ap: target ATA port
  656. * @fastdrain: activate fast drain
  657. *
  658. * Set ATA_PFLAG_EH_PENDING and activate fast drain if @fastdrain
  659. * is non-zero and EH wasn't pending before. Fast drain ensures
  660. * that EH kicks in in timely manner.
  661. *
  662. * LOCKING:
  663. * spin_lock_irqsave(host lock)
  664. */
  665. static void ata_eh_set_pending(struct ata_port *ap, int fastdrain)
  666. {
  667. int cnt;
  668. /* already scheduled? */
  669. if (ap->pflags & ATA_PFLAG_EH_PENDING)
  670. return;
  671. ap->pflags |= ATA_PFLAG_EH_PENDING;
  672. if (!fastdrain)
  673. return;
  674. /* do we have in-flight qcs? */
  675. cnt = ata_eh_nr_in_flight(ap);
  676. if (!cnt)
  677. return;
  678. /* activate fast drain */
  679. ap->fastdrain_cnt = cnt;
  680. ap->fastdrain_timer.expires =
  681. ata_deadline(jiffies, ATA_EH_FASTDRAIN_INTERVAL);
  682. add_timer(&ap->fastdrain_timer);
  683. }
  684. /**
  685. * ata_qc_schedule_eh - schedule qc for error handling
  686. * @qc: command to schedule error handling for
  687. *
  688. * Schedule error handling for @qc. EH will kick in as soon as
  689. * other commands are drained.
  690. *
  691. * LOCKING:
  692. * spin_lock_irqsave(host lock)
  693. */
  694. void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
  695. {
  696. struct ata_port *ap = qc->ap;
  697. WARN_ON(!ap->ops->error_handler);
  698. qc->flags |= ATA_QCFLAG_FAILED;
  699. ata_eh_set_pending(ap, 1);
  700. /* The following will fail if timeout has already expired.
  701. * ata_scsi_error() takes care of such scmds on EH entry.
  702. * Note that ATA_QCFLAG_FAILED is unconditionally set after
  703. * this function completes.
  704. */
  705. scsi_req_abort_cmd(qc->scsicmd);
  706. }
  707. /**
  708. * ata_port_schedule_eh - schedule error handling without a qc
  709. * @ap: ATA port to schedule EH for
  710. *
  711. * Schedule error handling for @ap. EH will kick in as soon as
  712. * all commands are drained.
  713. *
  714. * LOCKING:
  715. * spin_lock_irqsave(host lock)
  716. */
  717. void ata_port_schedule_eh(struct ata_port *ap)
  718. {
  719. WARN_ON(!ap->ops->error_handler);
  720. if (ap->pflags & ATA_PFLAG_INITIALIZING)
  721. return;
  722. ata_eh_set_pending(ap, 1);
  723. scsi_schedule_eh(ap->scsi_host);
  724. DPRINTK("port EH scheduled\n");
  725. }
  726. static int ata_do_link_abort(struct ata_port *ap, struct ata_link *link)
  727. {
  728. int tag, nr_aborted = 0;
  729. WARN_ON(!ap->ops->error_handler);
  730. /* we're gonna abort all commands, no need for fast drain */
  731. ata_eh_set_pending(ap, 0);
  732. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  733. struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
  734. if (qc && (!link || qc->dev->link == link)) {
  735. qc->flags |= ATA_QCFLAG_FAILED;
  736. ata_qc_complete(qc);
  737. nr_aborted++;
  738. }
  739. }
  740. if (!nr_aborted)
  741. ata_port_schedule_eh(ap);
  742. return nr_aborted;
  743. }
  744. /**
  745. * ata_link_abort - abort all qc's on the link
  746. * @link: ATA link to abort qc's for
  747. *
  748. * Abort all active qc's active on @link and schedule EH.
  749. *
  750. * LOCKING:
  751. * spin_lock_irqsave(host lock)
  752. *
  753. * RETURNS:
  754. * Number of aborted qc's.
  755. */
  756. int ata_link_abort(struct ata_link *link)
  757. {
  758. return ata_do_link_abort(link->ap, link);
  759. }
  760. /**
  761. * ata_port_abort - abort all qc's on the port
  762. * @ap: ATA port to abort qc's for
  763. *
  764. * Abort all active qc's of @ap and schedule EH.
  765. *
  766. * LOCKING:
  767. * spin_lock_irqsave(host_set lock)
  768. *
  769. * RETURNS:
  770. * Number of aborted qc's.
  771. */
  772. int ata_port_abort(struct ata_port *ap)
  773. {
  774. return ata_do_link_abort(ap, NULL);
  775. }
  776. /**
  777. * __ata_port_freeze - freeze port
  778. * @ap: ATA port to freeze
  779. *
  780. * This function is called when HSM violation or some other
  781. * condition disrupts normal operation of the port. Frozen port
  782. * is not allowed to perform any operation until the port is
  783. * thawed, which usually follows a successful reset.
  784. *
  785. * ap->ops->freeze() callback can be used for freezing the port
  786. * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
  787. * port cannot be frozen hardware-wise, the interrupt handler
  788. * must ack and clear interrupts unconditionally while the port
  789. * is frozen.
  790. *
  791. * LOCKING:
  792. * spin_lock_irqsave(host lock)
  793. */
  794. static void __ata_port_freeze(struct ata_port *ap)
  795. {
  796. WARN_ON(!ap->ops->error_handler);
  797. if (ap->ops->freeze)
  798. ap->ops->freeze(ap);
  799. ap->pflags |= ATA_PFLAG_FROZEN;
  800. DPRINTK("ata%u port frozen\n", ap->print_id);
  801. }
  802. /**
  803. * ata_port_freeze - abort & freeze port
  804. * @ap: ATA port to freeze
  805. *
  806. * Abort and freeze @ap.
  807. *
  808. * LOCKING:
  809. * spin_lock_irqsave(host lock)
  810. *
  811. * RETURNS:
  812. * Number of aborted commands.
  813. */
  814. int ata_port_freeze(struct ata_port *ap)
  815. {
  816. int nr_aborted;
  817. WARN_ON(!ap->ops->error_handler);
  818. nr_aborted = ata_port_abort(ap);
  819. __ata_port_freeze(ap);
  820. return nr_aborted;
  821. }
  822. /**
  823. * sata_async_notification - SATA async notification handler
  824. * @ap: ATA port where async notification is received
  825. *
  826. * Handler to be called when async notification via SDB FIS is
  827. * received. This function schedules EH if necessary.
  828. *
  829. * LOCKING:
  830. * spin_lock_irqsave(host lock)
  831. *
  832. * RETURNS:
  833. * 1 if EH is scheduled, 0 otherwise.
  834. */
  835. int sata_async_notification(struct ata_port *ap)
  836. {
  837. u32 sntf;
  838. int rc;
  839. if (!(ap->flags & ATA_FLAG_AN))
  840. return 0;
  841. rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
  842. if (rc == 0)
  843. sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
  844. if (!sata_pmp_attached(ap) || rc) {
  845. /* PMP is not attached or SNTF is not available */
  846. if (!sata_pmp_attached(ap)) {
  847. /* PMP is not attached. Check whether ATAPI
  848. * AN is configured. If so, notify media
  849. * change.
  850. */
  851. struct ata_device *dev = ap->link.device;
  852. if ((dev->class == ATA_DEV_ATAPI) &&
  853. (dev->flags & ATA_DFLAG_AN))
  854. ata_scsi_media_change_notify(dev);
  855. return 0;
  856. } else {
  857. /* PMP is attached but SNTF is not available.
  858. * ATAPI async media change notification is
  859. * not used. The PMP must be reporting PHY
  860. * status change, schedule EH.
  861. */
  862. ata_port_schedule_eh(ap);
  863. return 1;
  864. }
  865. } else {
  866. /* PMP is attached and SNTF is available */
  867. struct ata_link *link;
  868. /* check and notify ATAPI AN */
  869. ata_port_for_each_link(link, ap) {
  870. if (!(sntf & (1 << link->pmp)))
  871. continue;
  872. if ((link->device->class == ATA_DEV_ATAPI) &&
  873. (link->device->flags & ATA_DFLAG_AN))
  874. ata_scsi_media_change_notify(link->device);
  875. }
  876. /* If PMP is reporting that PHY status of some
  877. * downstream ports has changed, schedule EH.
  878. */
  879. if (sntf & (1 << SATA_PMP_CTRL_PORT)) {
  880. ata_port_schedule_eh(ap);
  881. return 1;
  882. }
  883. return 0;
  884. }
  885. }
  886. /**
  887. * ata_eh_freeze_port - EH helper to freeze port
  888. * @ap: ATA port to freeze
  889. *
  890. * Freeze @ap.
  891. *
  892. * LOCKING:
  893. * None.
  894. */
  895. void ata_eh_freeze_port(struct ata_port *ap)
  896. {
  897. unsigned long flags;
  898. if (!ap->ops->error_handler)
  899. return;
  900. spin_lock_irqsave(ap->lock, flags);
  901. __ata_port_freeze(ap);
  902. spin_unlock_irqrestore(ap->lock, flags);
  903. }
  904. /**
  905. * ata_port_thaw_port - EH helper to thaw port
  906. * @ap: ATA port to thaw
  907. *
  908. * Thaw frozen port @ap.
  909. *
  910. * LOCKING:
  911. * None.
  912. */
  913. void ata_eh_thaw_port(struct ata_port *ap)
  914. {
  915. unsigned long flags;
  916. if (!ap->ops->error_handler)
  917. return;
  918. spin_lock_irqsave(ap->lock, flags);
  919. ap->pflags &= ~ATA_PFLAG_FROZEN;
  920. if (ap->ops->thaw)
  921. ap->ops->thaw(ap);
  922. spin_unlock_irqrestore(ap->lock, flags);
  923. DPRINTK("ata%u port thawed\n", ap->print_id);
  924. }
  925. static void ata_eh_scsidone(struct scsi_cmnd *scmd)
  926. {
  927. /* nada */
  928. }
  929. static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
  930. {
  931. struct ata_port *ap = qc->ap;
  932. struct scsi_cmnd *scmd = qc->scsicmd;
  933. unsigned long flags;
  934. spin_lock_irqsave(ap->lock, flags);
  935. qc->scsidone = ata_eh_scsidone;
  936. __ata_qc_complete(qc);
  937. WARN_ON(ata_tag_valid(qc->tag));
  938. spin_unlock_irqrestore(ap->lock, flags);
  939. scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
  940. }
  941. /**
  942. * ata_eh_qc_complete - Complete an active ATA command from EH
  943. * @qc: Command to complete
  944. *
  945. * Indicate to the mid and upper layers that an ATA command has
  946. * completed. To be used from EH.
  947. */
  948. void ata_eh_qc_complete(struct ata_queued_cmd *qc)
  949. {
  950. struct scsi_cmnd *scmd = qc->scsicmd;
  951. scmd->retries = scmd->allowed;
  952. __ata_eh_qc_complete(qc);
  953. }
  954. /**
  955. * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
  956. * @qc: Command to retry
  957. *
  958. * Indicate to the mid and upper layers that an ATA command
  959. * should be retried. To be used from EH.
  960. *
  961. * SCSI midlayer limits the number of retries to scmd->allowed.
  962. * scmd->retries is decremented for commands which get retried
  963. * due to unrelated failures (qc->err_mask is zero).
  964. */
  965. void ata_eh_qc_retry(struct ata_queued_cmd *qc)
  966. {
  967. struct scsi_cmnd *scmd = qc->scsicmd;
  968. if (!qc->err_mask && scmd->retries)
  969. scmd->retries--;
  970. __ata_eh_qc_complete(qc);
  971. }
  972. /**
  973. * ata_eh_detach_dev - detach ATA device
  974. * @dev: ATA device to detach
  975. *
  976. * Detach @dev.
  977. *
  978. * LOCKING:
  979. * None.
  980. */
  981. void ata_eh_detach_dev(struct ata_device *dev)
  982. {
  983. struct ata_link *link = dev->link;
  984. struct ata_port *ap = link->ap;
  985. unsigned long flags;
  986. ata_dev_disable(dev);
  987. spin_lock_irqsave(ap->lock, flags);
  988. dev->flags &= ~ATA_DFLAG_DETACH;
  989. if (ata_scsi_offline_dev(dev)) {
  990. dev->flags |= ATA_DFLAG_DETACHED;
  991. ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
  992. }
  993. /* clear per-dev EH actions */
  994. ata_eh_clear_action(link, dev, &link->eh_info, ATA_EH_PERDEV_MASK);
  995. ata_eh_clear_action(link, dev, &link->eh_context.i, ATA_EH_PERDEV_MASK);
  996. spin_unlock_irqrestore(ap->lock, flags);
  997. }
  998. /**
  999. * ata_eh_about_to_do - about to perform eh_action
  1000. * @link: target ATA link
  1001. * @dev: target ATA dev for per-dev action (can be NULL)
  1002. * @action: action about to be performed
  1003. *
  1004. * Called just before performing EH actions to clear related bits
  1005. * in @link->eh_info such that eh actions are not unnecessarily
  1006. * repeated.
  1007. *
  1008. * LOCKING:
  1009. * None.
  1010. */
  1011. void ata_eh_about_to_do(struct ata_link *link, struct ata_device *dev,
  1012. unsigned int action)
  1013. {
  1014. struct ata_port *ap = link->ap;
  1015. struct ata_eh_info *ehi = &link->eh_info;
  1016. struct ata_eh_context *ehc = &link->eh_context;
  1017. unsigned long flags;
  1018. spin_lock_irqsave(ap->lock, flags);
  1019. ata_eh_clear_action(link, dev, ehi, action);
  1020. if (!(ehc->i.flags & ATA_EHI_QUIET))
  1021. ap->pflags |= ATA_PFLAG_RECOVERED;
  1022. spin_unlock_irqrestore(ap->lock, flags);
  1023. }
  1024. /**
  1025. * ata_eh_done - EH action complete
  1026. * @ap: target ATA port
  1027. * @dev: target ATA dev for per-dev action (can be NULL)
  1028. * @action: action just completed
  1029. *
  1030. * Called right after performing EH actions to clear related bits
  1031. * in @link->eh_context.
  1032. *
  1033. * LOCKING:
  1034. * None.
  1035. */
  1036. void ata_eh_done(struct ata_link *link, struct ata_device *dev,
  1037. unsigned int action)
  1038. {
  1039. struct ata_eh_context *ehc = &link->eh_context;
  1040. ata_eh_clear_action(link, dev, &ehc->i, action);
  1041. }
  1042. /**
  1043. * ata_err_string - convert err_mask to descriptive string
  1044. * @err_mask: error mask to convert to string
  1045. *
  1046. * Convert @err_mask to descriptive string. Errors are
  1047. * prioritized according to severity and only the most severe
  1048. * error is reported.
  1049. *
  1050. * LOCKING:
  1051. * None.
  1052. *
  1053. * RETURNS:
  1054. * Descriptive string for @err_mask
  1055. */
  1056. static const char *ata_err_string(unsigned int err_mask)
  1057. {
  1058. if (err_mask & AC_ERR_HOST_BUS)
  1059. return "host bus error";
  1060. if (err_mask & AC_ERR_ATA_BUS)
  1061. return "ATA bus error";
  1062. if (err_mask & AC_ERR_TIMEOUT)
  1063. return "timeout";
  1064. if (err_mask & AC_ERR_HSM)
  1065. return "HSM violation";
  1066. if (err_mask & AC_ERR_SYSTEM)
  1067. return "internal error";
  1068. if (err_mask & AC_ERR_MEDIA)
  1069. return "media error";
  1070. if (err_mask & AC_ERR_INVALID)
  1071. return "invalid argument";
  1072. if (err_mask & AC_ERR_DEV)
  1073. return "device error";
  1074. return "unknown error";
  1075. }
  1076. /**
  1077. * ata_read_log_page - read a specific log page
  1078. * @dev: target device
  1079. * @page: page to read
  1080. * @buf: buffer to store read page
  1081. * @sectors: number of sectors to read
  1082. *
  1083. * Read log page using READ_LOG_EXT command.
  1084. *
  1085. * LOCKING:
  1086. * Kernel thread context (may sleep).
  1087. *
  1088. * RETURNS:
  1089. * 0 on success, AC_ERR_* mask otherwise.
  1090. */
  1091. static unsigned int ata_read_log_page(struct ata_device *dev,
  1092. u8 page, void *buf, unsigned int sectors)
  1093. {
  1094. struct ata_taskfile tf;
  1095. unsigned int err_mask;
  1096. DPRINTK("read log page - page %d\n", page);
  1097. ata_tf_init(dev, &tf);
  1098. tf.command = ATA_CMD_READ_LOG_EXT;
  1099. tf.lbal = page;
  1100. tf.nsect = sectors;
  1101. tf.hob_nsect = sectors >> 8;
  1102. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
  1103. tf.protocol = ATA_PROT_PIO;
  1104. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
  1105. buf, sectors * ATA_SECT_SIZE, 0);
  1106. DPRINTK("EXIT, err_mask=%x\n", err_mask);
  1107. return err_mask;
  1108. }
  1109. /**
  1110. * ata_eh_read_log_10h - Read log page 10h for NCQ error details
  1111. * @dev: Device to read log page 10h from
  1112. * @tag: Resulting tag of the failed command
  1113. * @tf: Resulting taskfile registers of the failed command
  1114. *
  1115. * Read log page 10h to obtain NCQ error details and clear error
  1116. * condition.
  1117. *
  1118. * LOCKING:
  1119. * Kernel thread context (may sleep).
  1120. *
  1121. * RETURNS:
  1122. * 0 on success, -errno otherwise.
  1123. */
  1124. static int ata_eh_read_log_10h(struct ata_device *dev,
  1125. int *tag, struct ata_taskfile *tf)
  1126. {
  1127. u8 *buf = dev->link->ap->sector_buf;
  1128. unsigned int err_mask;
  1129. u8 csum;
  1130. int i;
  1131. err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
  1132. if (err_mask)
  1133. return -EIO;
  1134. csum = 0;
  1135. for (i = 0; i < ATA_SECT_SIZE; i++)
  1136. csum += buf[i];
  1137. if (csum)
  1138. ata_dev_printk(dev, KERN_WARNING,
  1139. "invalid checksum 0x%x on log page 10h\n", csum);
  1140. if (buf[0] & 0x80)
  1141. return -ENOENT;
  1142. *tag = buf[0] & 0x1f;
  1143. tf->command = buf[2];
  1144. tf->feature = buf[3];
  1145. tf->lbal = buf[4];
  1146. tf->lbam = buf[5];
  1147. tf->lbah = buf[6];
  1148. tf->device = buf[7];
  1149. tf->hob_lbal = buf[8];
  1150. tf->hob_lbam = buf[9];
  1151. tf->hob_lbah = buf[10];
  1152. tf->nsect = buf[12];
  1153. tf->hob_nsect = buf[13];
  1154. return 0;
  1155. }
  1156. /**
  1157. * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
  1158. * @dev: device to perform REQUEST_SENSE to
  1159. * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
  1160. * @dfl_sense_key: default sense key to use
  1161. *
  1162. * Perform ATAPI REQUEST_SENSE after the device reported CHECK
  1163. * SENSE. This function is EH helper.
  1164. *
  1165. * LOCKING:
  1166. * Kernel thread context (may sleep).
  1167. *
  1168. * RETURNS:
  1169. * 0 on success, AC_ERR_* mask on failure
  1170. */
  1171. static unsigned int atapi_eh_request_sense(struct ata_device *dev,
  1172. u8 *sense_buf, u8 dfl_sense_key)
  1173. {
  1174. u8 cdb[ATAPI_CDB_LEN] =
  1175. { REQUEST_SENSE, 0, 0, 0, SCSI_SENSE_BUFFERSIZE, 0 };
  1176. struct ata_port *ap = dev->link->ap;
  1177. struct ata_taskfile tf;
  1178. DPRINTK("ATAPI request sense\n");
  1179. /* FIXME: is this needed? */
  1180. memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
  1181. /* initialize sense_buf with the error register,
  1182. * for the case where they are -not- overwritten
  1183. */
  1184. sense_buf[0] = 0x70;
  1185. sense_buf[2] = dfl_sense_key;
  1186. /* some devices time out if garbage left in tf */
  1187. ata_tf_init(dev, &tf);
  1188. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  1189. tf.command = ATA_CMD_PACKET;
  1190. /* is it pointless to prefer PIO for "safety reasons"? */
  1191. if (ap->flags & ATA_FLAG_PIO_DMA) {
  1192. tf.protocol = ATAPI_PROT_DMA;
  1193. tf.feature |= ATAPI_PKT_DMA;
  1194. } else {
  1195. tf.protocol = ATAPI_PROT_PIO;
  1196. tf.lbam = SCSI_SENSE_BUFFERSIZE;
  1197. tf.lbah = 0;
  1198. }
  1199. return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
  1200. sense_buf, SCSI_SENSE_BUFFERSIZE, 0);
  1201. }
  1202. /**
  1203. * ata_eh_analyze_serror - analyze SError for a failed port
  1204. * @link: ATA link to analyze SError for
  1205. *
  1206. * Analyze SError if available and further determine cause of
  1207. * failure.
  1208. *
  1209. * LOCKING:
  1210. * None.
  1211. */
  1212. static void ata_eh_analyze_serror(struct ata_link *link)
  1213. {
  1214. struct ata_eh_context *ehc = &link->eh_context;
  1215. u32 serror = ehc->i.serror;
  1216. unsigned int err_mask = 0, action = 0;
  1217. u32 hotplug_mask;
  1218. if (serror & (SERR_PERSISTENT | SERR_DATA)) {
  1219. err_mask |= AC_ERR_ATA_BUS;
  1220. action |= ATA_EH_RESET;
  1221. }
  1222. if (serror & SERR_PROTOCOL) {
  1223. err_mask |= AC_ERR_HSM;
  1224. action |= ATA_EH_RESET;
  1225. }
  1226. if (serror & SERR_INTERNAL) {
  1227. err_mask |= AC_ERR_SYSTEM;
  1228. action |= ATA_EH_RESET;
  1229. }
  1230. /* Determine whether a hotplug event has occurred. Both
  1231. * SError.N/X are considered hotplug events for enabled or
  1232. * host links. For disabled PMP links, only N bit is
  1233. * considered as X bit is left at 1 for link plugging.
  1234. */
  1235. hotplug_mask = 0;
  1236. if (!(link->flags & ATA_LFLAG_DISABLED) || ata_is_host_link(link))
  1237. hotplug_mask = SERR_PHYRDY_CHG | SERR_DEV_XCHG;
  1238. else
  1239. hotplug_mask = SERR_PHYRDY_CHG;
  1240. if (serror & hotplug_mask)
  1241. ata_ehi_hotplugged(&ehc->i);
  1242. ehc->i.err_mask |= err_mask;
  1243. ehc->i.action |= action;
  1244. }
  1245. /**
  1246. * ata_eh_analyze_ncq_error - analyze NCQ error
  1247. * @link: ATA link to analyze NCQ error for
  1248. *
  1249. * Read log page 10h, determine the offending qc and acquire
  1250. * error status TF. For NCQ device errors, all LLDDs have to do
  1251. * is setting AC_ERR_DEV in ehi->err_mask. This function takes
  1252. * care of the rest.
  1253. *
  1254. * LOCKING:
  1255. * Kernel thread context (may sleep).
  1256. */
  1257. void ata_eh_analyze_ncq_error(struct ata_link *link)
  1258. {
  1259. struct ata_port *ap = link->ap;
  1260. struct ata_eh_context *ehc = &link->eh_context;
  1261. struct ata_device *dev = link->device;
  1262. struct ata_queued_cmd *qc;
  1263. struct ata_taskfile tf;
  1264. int tag, rc;
  1265. /* if frozen, we can't do much */
  1266. if (ap->pflags & ATA_PFLAG_FROZEN)
  1267. return;
  1268. /* is it NCQ device error? */
  1269. if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
  1270. return;
  1271. /* has LLDD analyzed already? */
  1272. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  1273. qc = __ata_qc_from_tag(ap, tag);
  1274. if (!(qc->flags & ATA_QCFLAG_FAILED))
  1275. continue;
  1276. if (qc->err_mask)
  1277. return;
  1278. }
  1279. /* okay, this error is ours */
  1280. rc = ata_eh_read_log_10h(dev, &tag, &tf);
  1281. if (rc) {
  1282. ata_link_printk(link, KERN_ERR, "failed to read log page 10h "
  1283. "(errno=%d)\n", rc);
  1284. return;
  1285. }
  1286. if (!(link->sactive & (1 << tag))) {
  1287. ata_link_printk(link, KERN_ERR, "log page 10h reported "
  1288. "inactive tag %d\n", tag);
  1289. return;
  1290. }
  1291. /* we've got the perpetrator, condemn it */
  1292. qc = __ata_qc_from_tag(ap, tag);
  1293. memcpy(&qc->result_tf, &tf, sizeof(tf));
  1294. qc->result_tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
  1295. qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
  1296. ehc->i.err_mask &= ~AC_ERR_DEV;
  1297. }
  1298. /**
  1299. * ata_eh_analyze_tf - analyze taskfile of a failed qc
  1300. * @qc: qc to analyze
  1301. * @tf: Taskfile registers to analyze
  1302. *
  1303. * Analyze taskfile of @qc and further determine cause of
  1304. * failure. This function also requests ATAPI sense data if
  1305. * avaliable.
  1306. *
  1307. * LOCKING:
  1308. * Kernel thread context (may sleep).
  1309. *
  1310. * RETURNS:
  1311. * Determined recovery action
  1312. */
  1313. static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
  1314. const struct ata_taskfile *tf)
  1315. {
  1316. unsigned int tmp, action = 0;
  1317. u8 stat = tf->command, err = tf->feature;
  1318. if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
  1319. qc->err_mask |= AC_ERR_HSM;
  1320. return ATA_EH_RESET;
  1321. }
  1322. if (stat & (ATA_ERR | ATA_DF))
  1323. qc->err_mask |= AC_ERR_DEV;
  1324. else
  1325. return 0;
  1326. switch (qc->dev->class) {
  1327. case ATA_DEV_ATA:
  1328. if (err & ATA_ICRC)
  1329. qc->err_mask |= AC_ERR_ATA_BUS;
  1330. if (err & ATA_UNC)
  1331. qc->err_mask |= AC_ERR_MEDIA;
  1332. if (err & ATA_IDNF)
  1333. qc->err_mask |= AC_ERR_INVALID;
  1334. break;
  1335. case ATA_DEV_ATAPI:
  1336. if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
  1337. tmp = atapi_eh_request_sense(qc->dev,
  1338. qc->scsicmd->sense_buffer,
  1339. qc->result_tf.feature >> 4);
  1340. if (!tmp) {
  1341. /* ATA_QCFLAG_SENSE_VALID is used to
  1342. * tell atapi_qc_complete() that sense
  1343. * data is already valid.
  1344. *
  1345. * TODO: interpret sense data and set
  1346. * appropriate err_mask.
  1347. */
  1348. qc->flags |= ATA_QCFLAG_SENSE_VALID;
  1349. } else
  1350. qc->err_mask |= tmp;
  1351. }
  1352. }
  1353. if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
  1354. action |= ATA_EH_RESET;
  1355. return action;
  1356. }
  1357. static int ata_eh_categorize_error(unsigned int eflags, unsigned int err_mask,
  1358. int *xfer_ok)
  1359. {
  1360. int base = 0;
  1361. if (!(eflags & ATA_EFLAG_DUBIOUS_XFER))
  1362. *xfer_ok = 1;
  1363. if (!*xfer_ok)
  1364. base = ATA_ECAT_DUBIOUS_NONE;
  1365. if (err_mask & AC_ERR_ATA_BUS)
  1366. return base + ATA_ECAT_ATA_BUS;
  1367. if (err_mask & AC_ERR_TIMEOUT)
  1368. return base + ATA_ECAT_TOUT_HSM;
  1369. if (eflags & ATA_EFLAG_IS_IO) {
  1370. if (err_mask & AC_ERR_HSM)
  1371. return base + ATA_ECAT_TOUT_HSM;
  1372. if ((err_mask &
  1373. (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
  1374. return base + ATA_ECAT_UNK_DEV;
  1375. }
  1376. return 0;
  1377. }
  1378. struct speed_down_verdict_arg {
  1379. u64 since;
  1380. int xfer_ok;
  1381. int nr_errors[ATA_ECAT_NR];
  1382. };
  1383. static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
  1384. {
  1385. struct speed_down_verdict_arg *arg = void_arg;
  1386. int cat;
  1387. if (ent->timestamp < arg->since)
  1388. return -1;
  1389. cat = ata_eh_categorize_error(ent->eflags, ent->err_mask,
  1390. &arg->xfer_ok);
  1391. arg->nr_errors[cat]++;
  1392. return 0;
  1393. }
  1394. /**
  1395. * ata_eh_speed_down_verdict - Determine speed down verdict
  1396. * @dev: Device of interest
  1397. *
  1398. * This function examines error ring of @dev and determines
  1399. * whether NCQ needs to be turned off, transfer speed should be
  1400. * stepped down, or falling back to PIO is necessary.
  1401. *
  1402. * ECAT_ATA_BUS : ATA_BUS error for any command
  1403. *
  1404. * ECAT_TOUT_HSM : TIMEOUT for any command or HSM violation for
  1405. * IO commands
  1406. *
  1407. * ECAT_UNK_DEV : Unknown DEV error for IO commands
  1408. *
  1409. * ECAT_DUBIOUS_* : Identical to above three but occurred while
  1410. * data transfer hasn't been verified.
  1411. *
  1412. * Verdicts are
  1413. *
  1414. * NCQ_OFF : Turn off NCQ.
  1415. *
  1416. * SPEED_DOWN : Speed down transfer speed but don't fall back
  1417. * to PIO.
  1418. *
  1419. * FALLBACK_TO_PIO : Fall back to PIO.
  1420. *
  1421. * Even if multiple verdicts are returned, only one action is
  1422. * taken per error. An action triggered by non-DUBIOUS errors
  1423. * clears ering, while one triggered by DUBIOUS_* errors doesn't.
  1424. * This is to expedite speed down decisions right after device is
  1425. * initially configured.
  1426. *
  1427. * The followings are speed down rules. #1 and #2 deal with
  1428. * DUBIOUS errors.
  1429. *
  1430. * 1. If more than one DUBIOUS_ATA_BUS or DUBIOUS_TOUT_HSM errors
  1431. * occurred during last 5 mins, SPEED_DOWN and FALLBACK_TO_PIO.
  1432. *
  1433. * 2. If more than one DUBIOUS_TOUT_HSM or DUBIOUS_UNK_DEV errors
  1434. * occurred during last 5 mins, NCQ_OFF.
  1435. *
  1436. * 3. If more than 8 ATA_BUS, TOUT_HSM or UNK_DEV errors
  1437. * ocurred during last 5 mins, FALLBACK_TO_PIO
  1438. *
  1439. * 4. If more than 3 TOUT_HSM or UNK_DEV errors occurred
  1440. * during last 10 mins, NCQ_OFF.
  1441. *
  1442. * 5. If more than 3 ATA_BUS or TOUT_HSM errors, or more than 6
  1443. * UNK_DEV errors occurred during last 10 mins, SPEED_DOWN.
  1444. *
  1445. * LOCKING:
  1446. * Inherited from caller.
  1447. *
  1448. * RETURNS:
  1449. * OR of ATA_EH_SPDN_* flags.
  1450. */
  1451. static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
  1452. {
  1453. const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
  1454. u64 j64 = get_jiffies_64();
  1455. struct speed_down_verdict_arg arg;
  1456. unsigned int verdict = 0;
  1457. /* scan past 5 mins of error history */
  1458. memset(&arg, 0, sizeof(arg));
  1459. arg.since = j64 - min(j64, j5mins);
  1460. ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
  1461. if (arg.nr_errors[ATA_ECAT_DUBIOUS_ATA_BUS] +
  1462. arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] > 1)
  1463. verdict |= ATA_EH_SPDN_SPEED_DOWN |
  1464. ATA_EH_SPDN_FALLBACK_TO_PIO | ATA_EH_SPDN_KEEP_ERRORS;
  1465. if (arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] +
  1466. arg.nr_errors[ATA_ECAT_DUBIOUS_UNK_DEV] > 1)
  1467. verdict |= ATA_EH_SPDN_NCQ_OFF | ATA_EH_SPDN_KEEP_ERRORS;
  1468. if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
  1469. arg.nr_errors[ATA_ECAT_TOUT_HSM] +
  1470. arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
  1471. verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
  1472. /* scan past 10 mins of error history */
  1473. memset(&arg, 0, sizeof(arg));
  1474. arg.since = j64 - min(j64, j10mins);
  1475. ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
  1476. if (arg.nr_errors[ATA_ECAT_TOUT_HSM] +
  1477. arg.nr_errors[ATA_ECAT_UNK_DEV] > 3)
  1478. verdict |= ATA_EH_SPDN_NCQ_OFF;
  1479. if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
  1480. arg.nr_errors[ATA_ECAT_TOUT_HSM] > 3 ||
  1481. arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
  1482. verdict |= ATA_EH_SPDN_SPEED_DOWN;
  1483. return verdict;
  1484. }
  1485. /**
  1486. * ata_eh_speed_down - record error and speed down if necessary
  1487. * @dev: Failed device
  1488. * @eflags: mask of ATA_EFLAG_* flags
  1489. * @err_mask: err_mask of the error
  1490. *
  1491. * Record error and examine error history to determine whether
  1492. * adjusting transmission speed is necessary. It also sets
  1493. * transmission limits appropriately if such adjustment is
  1494. * necessary.
  1495. *
  1496. * LOCKING:
  1497. * Kernel thread context (may sleep).
  1498. *
  1499. * RETURNS:
  1500. * Determined recovery action.
  1501. */
  1502. static unsigned int ata_eh_speed_down(struct ata_device *dev,
  1503. unsigned int eflags, unsigned int err_mask)
  1504. {
  1505. struct ata_link *link = ata_dev_phys_link(dev);
  1506. int xfer_ok = 0;
  1507. unsigned int verdict;
  1508. unsigned int action = 0;
  1509. /* don't bother if Cat-0 error */
  1510. if (ata_eh_categorize_error(eflags, err_mask, &xfer_ok) == 0)
  1511. return 0;
  1512. /* record error and determine whether speed down is necessary */
  1513. ata_ering_record(&dev->ering, eflags, err_mask);
  1514. verdict = ata_eh_speed_down_verdict(dev);
  1515. /* turn off NCQ? */
  1516. if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
  1517. (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
  1518. ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
  1519. dev->flags |= ATA_DFLAG_NCQ_OFF;
  1520. ata_dev_printk(dev, KERN_WARNING,
  1521. "NCQ disabled due to excessive errors\n");
  1522. goto done;
  1523. }
  1524. /* speed down? */
  1525. if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
  1526. /* speed down SATA link speed if possible */
  1527. if (sata_down_spd_limit(link) == 0) {
  1528. action |= ATA_EH_RESET;
  1529. goto done;
  1530. }
  1531. /* lower transfer mode */
  1532. if (dev->spdn_cnt < 2) {
  1533. static const int dma_dnxfer_sel[] =
  1534. { ATA_DNXFER_DMA, ATA_DNXFER_40C };
  1535. static const int pio_dnxfer_sel[] =
  1536. { ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
  1537. int sel;
  1538. if (dev->xfer_shift != ATA_SHIFT_PIO)
  1539. sel = dma_dnxfer_sel[dev->spdn_cnt];
  1540. else
  1541. sel = pio_dnxfer_sel[dev->spdn_cnt];
  1542. dev->spdn_cnt++;
  1543. if (ata_down_xfermask_limit(dev, sel) == 0) {
  1544. action |= ATA_EH_RESET;
  1545. goto done;
  1546. }
  1547. }
  1548. }
  1549. /* Fall back to PIO? Slowing down to PIO is meaningless for
  1550. * SATA ATA devices. Consider it only for PATA and SATAPI.
  1551. */
  1552. if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
  1553. (link->ap->cbl != ATA_CBL_SATA || dev->class == ATA_DEV_ATAPI) &&
  1554. (dev->xfer_shift != ATA_SHIFT_PIO)) {
  1555. if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
  1556. dev->spdn_cnt = 0;
  1557. action |= ATA_EH_RESET;
  1558. goto done;
  1559. }
  1560. }
  1561. return 0;
  1562. done:
  1563. /* device has been slowed down, blow error history */
  1564. if (!(verdict & ATA_EH_SPDN_KEEP_ERRORS))
  1565. ata_ering_clear(&dev->ering);
  1566. return action;
  1567. }
  1568. /**
  1569. * ata_eh_link_autopsy - analyze error and determine recovery action
  1570. * @link: host link to perform autopsy on
  1571. *
  1572. * Analyze why @link failed and determine which recovery actions
  1573. * are needed. This function also sets more detailed AC_ERR_*
  1574. * values and fills sense data for ATAPI CHECK SENSE.
  1575. *
  1576. * LOCKING:
  1577. * Kernel thread context (may sleep).
  1578. */
  1579. static void ata_eh_link_autopsy(struct ata_link *link)
  1580. {
  1581. struct ata_port *ap = link->ap;
  1582. struct ata_eh_context *ehc = &link->eh_context;
  1583. struct ata_device *dev;
  1584. unsigned int all_err_mask = 0, eflags = 0;
  1585. int tag;
  1586. u32 serror;
  1587. int rc;
  1588. DPRINTK("ENTER\n");
  1589. if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
  1590. return;
  1591. /* obtain and analyze SError */
  1592. rc = sata_scr_read(link, SCR_ERROR, &serror);
  1593. if (rc == 0) {
  1594. ehc->i.serror |= serror;
  1595. ata_eh_analyze_serror(link);
  1596. } else if (rc != -EOPNOTSUPP) {
  1597. /* SError read failed, force reset and probing */
  1598. ehc->i.probe_mask |= ATA_ALL_DEVICES;
  1599. ehc->i.action |= ATA_EH_RESET;
  1600. ehc->i.err_mask |= AC_ERR_OTHER;
  1601. }
  1602. /* analyze NCQ failure */
  1603. ata_eh_analyze_ncq_error(link);
  1604. /* any real error trumps AC_ERR_OTHER */
  1605. if (ehc->i.err_mask & ~AC_ERR_OTHER)
  1606. ehc->i.err_mask &= ~AC_ERR_OTHER;
  1607. all_err_mask |= ehc->i.err_mask;
  1608. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  1609. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  1610. if (!(qc->flags & ATA_QCFLAG_FAILED) ||
  1611. ata_dev_phys_link(qc->dev) != link)
  1612. continue;
  1613. /* inherit upper level err_mask */
  1614. qc->err_mask |= ehc->i.err_mask;
  1615. /* analyze TF */
  1616. ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
  1617. /* DEV errors are probably spurious in case of ATA_BUS error */
  1618. if (qc->err_mask & AC_ERR_ATA_BUS)
  1619. qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
  1620. AC_ERR_INVALID);
  1621. /* any real error trumps unknown error */
  1622. if (qc->err_mask & ~AC_ERR_OTHER)
  1623. qc->err_mask &= ~AC_ERR_OTHER;
  1624. /* SENSE_VALID trumps dev/unknown error and revalidation */
  1625. if (qc->flags & ATA_QCFLAG_SENSE_VALID)
  1626. qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
  1627. /* determine whether the command is worth retrying */
  1628. if (!(qc->err_mask & AC_ERR_INVALID) &&
  1629. ((qc->flags & ATA_QCFLAG_IO) || qc->err_mask != AC_ERR_DEV))
  1630. qc->flags |= ATA_QCFLAG_RETRY;
  1631. /* accumulate error info */
  1632. ehc->i.dev = qc->dev;
  1633. all_err_mask |= qc->err_mask;
  1634. if (qc->flags & ATA_QCFLAG_IO)
  1635. eflags |= ATA_EFLAG_IS_IO;
  1636. }
  1637. /* enforce default EH actions */
  1638. if (ap->pflags & ATA_PFLAG_FROZEN ||
  1639. all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
  1640. ehc->i.action |= ATA_EH_RESET;
  1641. else if (((eflags & ATA_EFLAG_IS_IO) && all_err_mask) ||
  1642. (!(eflags & ATA_EFLAG_IS_IO) && (all_err_mask & ~AC_ERR_DEV)))
  1643. ehc->i.action |= ATA_EH_REVALIDATE;
  1644. /* If we have offending qcs and the associated failed device,
  1645. * perform per-dev EH action only on the offending device.
  1646. */
  1647. if (ehc->i.dev) {
  1648. ehc->i.dev_action[ehc->i.dev->devno] |=
  1649. ehc->i.action & ATA_EH_PERDEV_MASK;
  1650. ehc->i.action &= ~ATA_EH_PERDEV_MASK;
  1651. }
  1652. /* propagate timeout to host link */
  1653. if ((all_err_mask & AC_ERR_TIMEOUT) && !ata_is_host_link(link))
  1654. ap->link.eh_context.i.err_mask |= AC_ERR_TIMEOUT;
  1655. /* record error and consider speeding down */
  1656. dev = ehc->i.dev;
  1657. if (!dev && ((ata_link_max_devices(link) == 1 &&
  1658. ata_dev_enabled(link->device))))
  1659. dev = link->device;
  1660. if (dev) {
  1661. if (dev->flags & ATA_DFLAG_DUBIOUS_XFER)
  1662. eflags |= ATA_EFLAG_DUBIOUS_XFER;
  1663. ehc->i.action |= ata_eh_speed_down(dev, eflags, all_err_mask);
  1664. }
  1665. DPRINTK("EXIT\n");
  1666. }
  1667. /**
  1668. * ata_eh_autopsy - analyze error and determine recovery action
  1669. * @ap: host port to perform autopsy on
  1670. *
  1671. * Analyze all links of @ap and determine why they failed and
  1672. * which recovery actions are needed.
  1673. *
  1674. * LOCKING:
  1675. * Kernel thread context (may sleep).
  1676. */
  1677. void ata_eh_autopsy(struct ata_port *ap)
  1678. {
  1679. struct ata_link *link;
  1680. ata_port_for_each_link(link, ap)
  1681. ata_eh_link_autopsy(link);
  1682. /* Handle the frigging slave link. Autopsy is done similarly
  1683. * but actions and flags are transferred over to the master
  1684. * link and handled from there.
  1685. */
  1686. if (ap->slave_link) {
  1687. struct ata_eh_context *mehc = &ap->link.eh_context;
  1688. struct ata_eh_context *sehc = &ap->slave_link->eh_context;
  1689. ata_eh_link_autopsy(ap->slave_link);
  1690. ata_eh_about_to_do(ap->slave_link, NULL, ATA_EH_ALL_ACTIONS);
  1691. mehc->i.action |= sehc->i.action;
  1692. mehc->i.dev_action[1] |= sehc->i.dev_action[1];
  1693. mehc->i.flags |= sehc->i.flags;
  1694. ata_eh_done(ap->slave_link, NULL, ATA_EH_ALL_ACTIONS);
  1695. }
  1696. /* Autopsy of fanout ports can affect host link autopsy.
  1697. * Perform host link autopsy last.
  1698. */
  1699. if (sata_pmp_attached(ap))
  1700. ata_eh_link_autopsy(&ap->link);
  1701. }
  1702. /**
  1703. * ata_eh_link_report - report error handling to user
  1704. * @link: ATA link EH is going on
  1705. *
  1706. * Report EH to user.
  1707. *
  1708. * LOCKING:
  1709. * None.
  1710. */
  1711. static void ata_eh_link_report(struct ata_link *link)
  1712. {
  1713. struct ata_port *ap = link->ap;
  1714. struct ata_eh_context *ehc = &link->eh_context;
  1715. const char *frozen, *desc;
  1716. char tries_buf[6];
  1717. int tag, nr_failed = 0;
  1718. if (ehc->i.flags & ATA_EHI_QUIET)
  1719. return;
  1720. desc = NULL;
  1721. if (ehc->i.desc[0] != '\0')
  1722. desc = ehc->i.desc;
  1723. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  1724. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  1725. if (!(qc->flags & ATA_QCFLAG_FAILED) ||
  1726. ata_dev_phys_link(qc->dev) != link ||
  1727. ((qc->flags & ATA_QCFLAG_QUIET) &&
  1728. qc->err_mask == AC_ERR_DEV))
  1729. continue;
  1730. if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
  1731. continue;
  1732. nr_failed++;
  1733. }
  1734. if (!nr_failed && !ehc->i.err_mask)
  1735. return;
  1736. frozen = "";
  1737. if (ap->pflags & ATA_PFLAG_FROZEN)
  1738. frozen = " frozen";
  1739. memset(tries_buf, 0, sizeof(tries_buf));
  1740. if (ap->eh_tries < ATA_EH_MAX_TRIES)
  1741. snprintf(tries_buf, sizeof(tries_buf) - 1, " t%d",
  1742. ap->eh_tries);
  1743. if (ehc->i.dev) {
  1744. ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
  1745. "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
  1746. ehc->i.err_mask, link->sactive, ehc->i.serror,
  1747. ehc->i.action, frozen, tries_buf);
  1748. if (desc)
  1749. ata_dev_printk(ehc->i.dev, KERN_ERR, "%s\n", desc);
  1750. } else {
  1751. ata_link_printk(link, KERN_ERR, "exception Emask 0x%x "
  1752. "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
  1753. ehc->i.err_mask, link->sactive, ehc->i.serror,
  1754. ehc->i.action, frozen, tries_buf);
  1755. if (desc)
  1756. ata_link_printk(link, KERN_ERR, "%s\n", desc);
  1757. }
  1758. if (ehc->i.serror)
  1759. ata_link_printk(link, KERN_ERR,
  1760. "SError: { %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s}\n",
  1761. ehc->i.serror & SERR_DATA_RECOVERED ? "RecovData " : "",
  1762. ehc->i.serror & SERR_COMM_RECOVERED ? "RecovComm " : "",
  1763. ehc->i.serror & SERR_DATA ? "UnrecovData " : "",
  1764. ehc->i.serror & SERR_PERSISTENT ? "Persist " : "",
  1765. ehc->i.serror & SERR_PROTOCOL ? "Proto " : "",
  1766. ehc->i.serror & SERR_INTERNAL ? "HostInt " : "",
  1767. ehc->i.serror & SERR_PHYRDY_CHG ? "PHYRdyChg " : "",
  1768. ehc->i.serror & SERR_PHY_INT_ERR ? "PHYInt " : "",
  1769. ehc->i.serror & SERR_COMM_WAKE ? "CommWake " : "",
  1770. ehc->i.serror & SERR_10B_8B_ERR ? "10B8B " : "",
  1771. ehc->i.serror & SERR_DISPARITY ? "Dispar " : "",
  1772. ehc->i.serror & SERR_CRC ? "BadCRC " : "",
  1773. ehc->i.serror & SERR_HANDSHAKE ? "Handshk " : "",
  1774. ehc->i.serror & SERR_LINK_SEQ_ERR ? "LinkSeq " : "",
  1775. ehc->i.serror & SERR_TRANS_ST_ERROR ? "TrStaTrns " : "",
  1776. ehc->i.serror & SERR_UNRECOG_FIS ? "UnrecFIS " : "",
  1777. ehc->i.serror & SERR_DEV_XCHG ? "DevExch " : "");
  1778. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  1779. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  1780. struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
  1781. const u8 *cdb = qc->cdb;
  1782. char data_buf[20] = "";
  1783. char cdb_buf[70] = "";
  1784. if (!(qc->flags & ATA_QCFLAG_FAILED) ||
  1785. ata_dev_phys_link(qc->dev) != link || !qc->err_mask)
  1786. continue;
  1787. if (qc->dma_dir != DMA_NONE) {
  1788. static const char *dma_str[] = {
  1789. [DMA_BIDIRECTIONAL] = "bidi",
  1790. [DMA_TO_DEVICE] = "out",
  1791. [DMA_FROM_DEVICE] = "in",
  1792. };
  1793. static const char *prot_str[] = {
  1794. [ATA_PROT_PIO] = "pio",
  1795. [ATA_PROT_DMA] = "dma",
  1796. [ATA_PROT_NCQ] = "ncq",
  1797. [ATAPI_PROT_PIO] = "pio",
  1798. [ATAPI_PROT_DMA] = "dma",
  1799. };
  1800. snprintf(data_buf, sizeof(data_buf), " %s %u %s",
  1801. prot_str[qc->tf.protocol], qc->nbytes,
  1802. dma_str[qc->dma_dir]);
  1803. }
  1804. if (ata_is_atapi(qc->tf.protocol))
  1805. snprintf(cdb_buf, sizeof(cdb_buf),
  1806. "cdb %02x %02x %02x %02x %02x %02x %02x %02x "
  1807. "%02x %02x %02x %02x %02x %02x %02x %02x\n ",
  1808. cdb[0], cdb[1], cdb[2], cdb[3],
  1809. cdb[4], cdb[5], cdb[6], cdb[7],
  1810. cdb[8], cdb[9], cdb[10], cdb[11],
  1811. cdb[12], cdb[13], cdb[14], cdb[15]);
  1812. ata_dev_printk(qc->dev, KERN_ERR,
  1813. "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
  1814. "tag %d%s\n %s"
  1815. "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
  1816. "Emask 0x%x (%s)%s\n",
  1817. cmd->command, cmd->feature, cmd->nsect,
  1818. cmd->lbal, cmd->lbam, cmd->lbah,
  1819. cmd->hob_feature, cmd->hob_nsect,
  1820. cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
  1821. cmd->device, qc->tag, data_buf, cdb_buf,
  1822. res->command, res->feature, res->nsect,
  1823. res->lbal, res->lbam, res->lbah,
  1824. res->hob_feature, res->hob_nsect,
  1825. res->hob_lbal, res->hob_lbam, res->hob_lbah,
  1826. res->device, qc->err_mask, ata_err_string(qc->err_mask),
  1827. qc->err_mask & AC_ERR_NCQ ? " <F>" : "");
  1828. if (res->command & (ATA_BUSY | ATA_DRDY | ATA_DF | ATA_DRQ |
  1829. ATA_ERR)) {
  1830. if (res->command & ATA_BUSY)
  1831. ata_dev_printk(qc->dev, KERN_ERR,
  1832. "status: { Busy }\n");
  1833. else
  1834. ata_dev_printk(qc->dev, KERN_ERR,
  1835. "status: { %s%s%s%s}\n",
  1836. res->command & ATA_DRDY ? "DRDY " : "",
  1837. res->command & ATA_DF ? "DF " : "",
  1838. res->command & ATA_DRQ ? "DRQ " : "",
  1839. res->command & ATA_ERR ? "ERR " : "");
  1840. }
  1841. if (cmd->command != ATA_CMD_PACKET &&
  1842. (res->feature & (ATA_ICRC | ATA_UNC | ATA_IDNF |
  1843. ATA_ABORTED)))
  1844. ata_dev_printk(qc->dev, KERN_ERR,
  1845. "error: { %s%s%s%s}\n",
  1846. res->feature & ATA_ICRC ? "ICRC " : "",
  1847. res->feature & ATA_UNC ? "UNC " : "",
  1848. res->feature & ATA_IDNF ? "IDNF " : "",
  1849. res->feature & ATA_ABORTED ? "ABRT " : "");
  1850. }
  1851. }
  1852. /**
  1853. * ata_eh_report - report error handling to user
  1854. * @ap: ATA port to report EH about
  1855. *
  1856. * Report EH to user.
  1857. *
  1858. * LOCKING:
  1859. * None.
  1860. */
  1861. void ata_eh_report(struct ata_port *ap)
  1862. {
  1863. struct ata_link *link;
  1864. __ata_port_for_each_link(link, ap)
  1865. ata_eh_link_report(link);
  1866. }
  1867. static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset,
  1868. unsigned int *classes, unsigned long deadline,
  1869. bool clear_classes)
  1870. {
  1871. struct ata_device *dev;
  1872. if (clear_classes)
  1873. ata_link_for_each_dev(dev, link)
  1874. classes[dev->devno] = ATA_DEV_UNKNOWN;
  1875. return reset(link, classes, deadline);
  1876. }
  1877. static int ata_eh_followup_srst_needed(struct ata_link *link,
  1878. int rc, const unsigned int *classes)
  1879. {
  1880. if ((link->flags & ATA_LFLAG_NO_SRST) || ata_link_offline(link))
  1881. return 0;
  1882. if (rc == -EAGAIN)
  1883. return 1;
  1884. if (sata_pmp_supported(link->ap) && ata_is_host_link(link))
  1885. return 1;
  1886. return 0;
  1887. }
  1888. int ata_eh_reset(struct ata_link *link, int classify,
  1889. ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
  1890. ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
  1891. {
  1892. struct ata_port *ap = link->ap;
  1893. struct ata_link *slave = ap->slave_link;
  1894. struct ata_eh_context *ehc = &link->eh_context;
  1895. struct ata_eh_context *sehc = &slave->eh_context;
  1896. unsigned int *classes = ehc->classes;
  1897. unsigned int lflags = link->flags;
  1898. int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
  1899. int max_tries = 0, try = 0;
  1900. struct ata_link *failed_link;
  1901. struct ata_device *dev;
  1902. unsigned long deadline, now;
  1903. ata_reset_fn_t reset;
  1904. unsigned long flags;
  1905. u32 sstatus;
  1906. int nr_unknown, rc;
  1907. /*
  1908. * Prepare to reset
  1909. */
  1910. while (ata_eh_reset_timeouts[max_tries] != ULONG_MAX)
  1911. max_tries++;
  1912. if (link->flags & ATA_LFLAG_NO_HRST)
  1913. hardreset = NULL;
  1914. if (link->flags & ATA_LFLAG_NO_SRST)
  1915. softreset = NULL;
  1916. now = jiffies;
  1917. deadline = ata_deadline(ehc->last_reset, ATA_EH_RESET_COOL_DOWN);
  1918. if (time_before(now, deadline))
  1919. schedule_timeout_uninterruptible(deadline - now);
  1920. spin_lock_irqsave(ap->lock, flags);
  1921. ap->pflags |= ATA_PFLAG_RESETTING;
  1922. spin_unlock_irqrestore(ap->lock, flags);
  1923. ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
  1924. ehc->last_reset = jiffies;
  1925. ata_link_for_each_dev(dev, link) {
  1926. /* If we issue an SRST then an ATA drive (not ATAPI)
  1927. * may change configuration and be in PIO0 timing. If
  1928. * we do a hard reset (or are coming from power on)
  1929. * this is true for ATA or ATAPI. Until we've set a
  1930. * suitable controller mode we should not touch the
  1931. * bus as we may be talking too fast.
  1932. */
  1933. dev->pio_mode = XFER_PIO_0;
  1934. /* If the controller has a pio mode setup function
  1935. * then use it to set the chipset to rights. Don't
  1936. * touch the DMA setup as that will be dealt with when
  1937. * configuring devices.
  1938. */
  1939. if (ap->ops->set_piomode)
  1940. ap->ops->set_piomode(ap, dev);
  1941. }
  1942. /* prefer hardreset */
  1943. reset = NULL;
  1944. ehc->i.action &= ~ATA_EH_RESET;
  1945. if (hardreset) {
  1946. reset = hardreset;
  1947. ehc->i.action |= ATA_EH_HARDRESET;
  1948. } else if (softreset) {
  1949. reset = softreset;
  1950. ehc->i.action |= ATA_EH_SOFTRESET;
  1951. }
  1952. if (prereset) {
  1953. unsigned long deadline = ata_deadline(jiffies,
  1954. ATA_EH_PRERESET_TIMEOUT);
  1955. if (slave) {
  1956. sehc->i.action &= ~ATA_EH_RESET;
  1957. sehc->i.action |= ehc->i.action;
  1958. }
  1959. rc = prereset(link, deadline);
  1960. /* If present, do prereset on slave link too. Reset
  1961. * is skipped iff both master and slave links report
  1962. * -ENOENT or clear ATA_EH_RESET.
  1963. */
  1964. if (slave && (rc == 0 || rc == -ENOENT)) {
  1965. int tmp;
  1966. tmp = prereset(slave, deadline);
  1967. if (tmp != -ENOENT)
  1968. rc = tmp;
  1969. ehc->i.action |= sehc->i.action;
  1970. }
  1971. if (rc) {
  1972. if (rc == -ENOENT) {
  1973. ata_link_printk(link, KERN_DEBUG,
  1974. "port disabled. ignoring.\n");
  1975. ehc->i.action &= ~ATA_EH_RESET;
  1976. ata_link_for_each_dev(dev, link)
  1977. classes[dev->devno] = ATA_DEV_NONE;
  1978. rc = 0;
  1979. } else
  1980. ata_link_printk(link, KERN_ERR,
  1981. "prereset failed (errno=%d)\n", rc);
  1982. goto out;
  1983. }
  1984. /* prereset() might have cleared ATA_EH_RESET. If so,
  1985. * bang classes and return.
  1986. */
  1987. if (reset && !(ehc->i.action & ATA_EH_RESET)) {
  1988. ata_link_for_each_dev(dev, link)
  1989. classes[dev->devno] = ATA_DEV_NONE;
  1990. rc = 0;
  1991. goto out;
  1992. }
  1993. }
  1994. retry:
  1995. /*
  1996. * Perform reset
  1997. */
  1998. ehc->last_reset = jiffies;
  1999. if (ata_is_host_link(link))
  2000. ata_eh_freeze_port(ap);
  2001. deadline = ata_deadline(jiffies, ata_eh_reset_timeouts[try++]);
  2002. if (reset) {
  2003. if (verbose)
  2004. ata_link_printk(link, KERN_INFO, "%s resetting link\n",
  2005. reset == softreset ? "soft" : "hard");
  2006. /* mark that this EH session started with reset */
  2007. if (reset == hardreset)
  2008. ehc->i.flags |= ATA_EHI_DID_HARDRESET;
  2009. else
  2010. ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
  2011. rc = ata_do_reset(link, reset, classes, deadline, true);
  2012. if (rc && rc != -EAGAIN) {
  2013. failed_link = link;
  2014. goto fail;
  2015. }
  2016. /* hardreset slave link if existent */
  2017. if (slave && reset == hardreset) {
  2018. int tmp;
  2019. if (verbose)
  2020. ata_link_printk(slave, KERN_INFO,
  2021. "hard resetting link\n");
  2022. ata_eh_about_to_do(slave, NULL, ATA_EH_RESET);
  2023. tmp = ata_do_reset(slave, reset, classes, deadline,
  2024. false);
  2025. switch (tmp) {
  2026. case -EAGAIN:
  2027. rc = -EAGAIN;
  2028. case 0:
  2029. break;
  2030. default:
  2031. failed_link = slave;
  2032. rc = tmp;
  2033. goto fail;
  2034. }
  2035. }
  2036. /* perform follow-up SRST if necessary */
  2037. if (reset == hardreset &&
  2038. ata_eh_followup_srst_needed(link, rc, classes)) {
  2039. reset = softreset;
  2040. if (!reset) {
  2041. ata_link_printk(link, KERN_ERR,
  2042. "follow-up softreset required "
  2043. "but no softreset avaliable\n");
  2044. failed_link = link;
  2045. rc = -EINVAL;
  2046. goto fail;
  2047. }
  2048. ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
  2049. rc = ata_do_reset(link, reset, classes, deadline, true);
  2050. }
  2051. } else {
  2052. if (verbose)
  2053. ata_link_printk(link, KERN_INFO, "no reset method "
  2054. "available, skipping reset\n");
  2055. if (!(lflags & ATA_LFLAG_ASSUME_CLASS))
  2056. lflags |= ATA_LFLAG_ASSUME_ATA;
  2057. }
  2058. /*
  2059. * Post-reset processing
  2060. */
  2061. ata_link_for_each_dev(dev, link) {
  2062. /* After the reset, the device state is PIO 0 and the
  2063. * controller state is undefined. Reset also wakes up
  2064. * drives from sleeping mode.
  2065. */
  2066. dev->pio_mode = XFER_PIO_0;
  2067. dev->flags &= ~ATA_DFLAG_SLEEPING;
  2068. if (ata_phys_link_offline(ata_dev_phys_link(dev)))
  2069. continue;
  2070. /* apply class override */
  2071. if (lflags & ATA_LFLAG_ASSUME_ATA)
  2072. classes[dev->devno] = ATA_DEV_ATA;
  2073. else if (lflags & ATA_LFLAG_ASSUME_SEMB)
  2074. classes[dev->devno] = ATA_DEV_SEMB_UNSUP; /* not yet */
  2075. }
  2076. /* record current link speed */
  2077. if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0)
  2078. link->sata_spd = (sstatus >> 4) & 0xf;
  2079. if (slave && sata_scr_read(slave, SCR_STATUS, &sstatus) == 0)
  2080. slave->sata_spd = (sstatus >> 4) & 0xf;
  2081. /* thaw the port */
  2082. if (ata_is_host_link(link))
  2083. ata_eh_thaw_port(ap);
  2084. /* postreset() should clear hardware SError. Although SError
  2085. * is cleared during link resume, clearing SError here is
  2086. * necessary as some PHYs raise hotplug events after SRST.
  2087. * This introduces race condition where hotplug occurs between
  2088. * reset and here. This race is mediated by cross checking
  2089. * link onlineness and classification result later.
  2090. */
  2091. if (postreset) {
  2092. postreset(link, classes);
  2093. if (slave)
  2094. postreset(slave, classes);
  2095. }
  2096. /* clear cached SError */
  2097. spin_lock_irqsave(link->ap->lock, flags);
  2098. link->eh_info.serror = 0;
  2099. if (slave)
  2100. slave->eh_info.serror = 0;
  2101. spin_unlock_irqrestore(link->ap->lock, flags);
  2102. /* Make sure onlineness and classification result correspond.
  2103. * Hotplug could have happened during reset and some
  2104. * controllers fail to wait while a drive is spinning up after
  2105. * being hotplugged causing misdetection. By cross checking
  2106. * link onlineness and classification result, those conditions
  2107. * can be reliably detected and retried.
  2108. */
  2109. nr_unknown = 0;
  2110. ata_link_for_each_dev(dev, link) {
  2111. /* convert all ATA_DEV_UNKNOWN to ATA_DEV_NONE */
  2112. if (classes[dev->devno] == ATA_DEV_UNKNOWN) {
  2113. classes[dev->devno] = ATA_DEV_NONE;
  2114. if (ata_phys_link_online(ata_dev_phys_link(dev)))
  2115. nr_unknown++;
  2116. }
  2117. }
  2118. if (classify && nr_unknown) {
  2119. if (try < max_tries) {
  2120. ata_link_printk(link, KERN_WARNING, "link online but "
  2121. "device misclassified, retrying\n");
  2122. failed_link = link;
  2123. rc = -EAGAIN;
  2124. goto fail;
  2125. }
  2126. ata_link_printk(link, KERN_WARNING,
  2127. "link online but device misclassified, "
  2128. "device detection might fail\n");
  2129. }
  2130. /* reset successful, schedule revalidation */
  2131. ata_eh_done(link, NULL, ATA_EH_RESET);
  2132. if (slave)
  2133. ata_eh_done(slave, NULL, ATA_EH_RESET);
  2134. ehc->last_reset = jiffies;
  2135. ehc->i.action |= ATA_EH_REVALIDATE;
  2136. rc = 0;
  2137. out:
  2138. /* clear hotplug flag */
  2139. ehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
  2140. if (slave)
  2141. sehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
  2142. spin_lock_irqsave(ap->lock, flags);
  2143. ap->pflags &= ~ATA_PFLAG_RESETTING;
  2144. spin_unlock_irqrestore(ap->lock, flags);
  2145. return rc;
  2146. fail:
  2147. /* if SCR isn't accessible on a fan-out port, PMP needs to be reset */
  2148. if (!ata_is_host_link(link) &&
  2149. sata_scr_read(link, SCR_STATUS, &sstatus))
  2150. rc = -ERESTART;
  2151. if (rc == -ERESTART || try >= max_tries)
  2152. goto out;
  2153. now = jiffies;
  2154. if (time_before(now, deadline)) {
  2155. unsigned long delta = deadline - now;
  2156. ata_link_printk(failed_link, KERN_WARNING,
  2157. "reset failed (errno=%d), retrying in %u secs\n",
  2158. rc, DIV_ROUND_UP(jiffies_to_msecs(delta), 1000));
  2159. while (delta)
  2160. delta = schedule_timeout_uninterruptible(delta);
  2161. }
  2162. if (try == max_tries - 1) {
  2163. sata_down_spd_limit(link);
  2164. if (slave)
  2165. sata_down_spd_limit(slave);
  2166. } else if (rc == -EPIPE)
  2167. sata_down_spd_limit(failed_link);
  2168. if (hardreset)
  2169. reset = hardreset;
  2170. goto retry;
  2171. }
  2172. static int ata_eh_revalidate_and_attach(struct ata_link *link,
  2173. struct ata_device **r_failed_dev)
  2174. {
  2175. struct ata_port *ap = link->ap;
  2176. struct ata_eh_context *ehc = &link->eh_context;
  2177. struct ata_device *dev;
  2178. unsigned int new_mask = 0;
  2179. unsigned long flags;
  2180. int rc = 0;
  2181. DPRINTK("ENTER\n");
  2182. /* For PATA drive side cable detection to work, IDENTIFY must
  2183. * be done backwards such that PDIAG- is released by the slave
  2184. * device before the master device is identified.
  2185. */
  2186. ata_link_for_each_dev_reverse(dev, link) {
  2187. unsigned int action = ata_eh_dev_action(dev);
  2188. unsigned int readid_flags = 0;
  2189. if (ehc->i.flags & ATA_EHI_DID_RESET)
  2190. readid_flags |= ATA_READID_POSTRESET;
  2191. if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
  2192. WARN_ON(dev->class == ATA_DEV_PMP);
  2193. if (ata_phys_link_offline(ata_dev_phys_link(dev))) {
  2194. rc = -EIO;
  2195. goto err;
  2196. }
  2197. ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE);
  2198. rc = ata_dev_revalidate(dev, ehc->classes[dev->devno],
  2199. readid_flags);
  2200. if (rc)
  2201. goto err;
  2202. ata_eh_done(link, dev, ATA_EH_REVALIDATE);
  2203. /* Configuration may have changed, reconfigure
  2204. * transfer mode.
  2205. */
  2206. ehc->i.flags |= ATA_EHI_SETMODE;
  2207. /* schedule the scsi_rescan_device() here */
  2208. queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
  2209. } else if (dev->class == ATA_DEV_UNKNOWN &&
  2210. ehc->tries[dev->devno] &&
  2211. ata_class_enabled(ehc->classes[dev->devno])) {
  2212. dev->class = ehc->classes[dev->devno];
  2213. if (dev->class == ATA_DEV_PMP)
  2214. rc = sata_pmp_attach(dev);
  2215. else
  2216. rc = ata_dev_read_id(dev, &dev->class,
  2217. readid_flags, dev->id);
  2218. switch (rc) {
  2219. case 0:
  2220. new_mask |= 1 << dev->devno;
  2221. break;
  2222. case -ENOENT:
  2223. /* IDENTIFY was issued to non-existent
  2224. * device. No need to reset. Just
  2225. * thaw and kill the device.
  2226. */
  2227. ata_eh_thaw_port(ap);
  2228. dev->class = ATA_DEV_UNKNOWN;
  2229. break;
  2230. default:
  2231. dev->class = ATA_DEV_UNKNOWN;
  2232. goto err;
  2233. }
  2234. }
  2235. }
  2236. /* PDIAG- should have been released, ask cable type if post-reset */
  2237. if ((ehc->i.flags & ATA_EHI_DID_RESET) && ata_is_host_link(link)) {
  2238. if (ap->ops->cable_detect)
  2239. ap->cbl = ap->ops->cable_detect(ap);
  2240. ata_force_cbl(ap);
  2241. }
  2242. /* Configure new devices forward such that user doesn't see
  2243. * device detection messages backwards.
  2244. */
  2245. ata_link_for_each_dev(dev, link) {
  2246. if (!(new_mask & (1 << dev->devno)) ||
  2247. dev->class == ATA_DEV_PMP)
  2248. continue;
  2249. ehc->i.flags |= ATA_EHI_PRINTINFO;
  2250. rc = ata_dev_configure(dev);
  2251. ehc->i.flags &= ~ATA_EHI_PRINTINFO;
  2252. if (rc)
  2253. goto err;
  2254. spin_lock_irqsave(ap->lock, flags);
  2255. ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
  2256. spin_unlock_irqrestore(ap->lock, flags);
  2257. /* new device discovered, configure xfermode */
  2258. ehc->i.flags |= ATA_EHI_SETMODE;
  2259. }
  2260. return 0;
  2261. err:
  2262. *r_failed_dev = dev;
  2263. DPRINTK("EXIT rc=%d\n", rc);
  2264. return rc;
  2265. }
  2266. /**
  2267. * ata_set_mode - Program timings and issue SET FEATURES - XFER
  2268. * @link: link on which timings will be programmed
  2269. * @r_failed_dev: out paramter for failed device
  2270. *
  2271. * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
  2272. * ata_set_mode() fails, pointer to the failing device is
  2273. * returned in @r_failed_dev.
  2274. *
  2275. * LOCKING:
  2276. * PCI/etc. bus probe sem.
  2277. *
  2278. * RETURNS:
  2279. * 0 on success, negative errno otherwise
  2280. */
  2281. int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
  2282. {
  2283. struct ata_port *ap = link->ap;
  2284. struct ata_device *dev;
  2285. int rc;
  2286. /* if data transfer is verified, clear DUBIOUS_XFER on ering top */
  2287. ata_link_for_each_dev(dev, link) {
  2288. if (!(dev->flags & ATA_DFLAG_DUBIOUS_XFER)) {
  2289. struct ata_ering_entry *ent;
  2290. ent = ata_ering_top(&dev->ering);
  2291. if (ent)
  2292. ent->eflags &= ~ATA_EFLAG_DUBIOUS_XFER;
  2293. }
  2294. }
  2295. /* has private set_mode? */
  2296. if (ap->ops->set_mode)
  2297. rc = ap->ops->set_mode(link, r_failed_dev);
  2298. else
  2299. rc = ata_do_set_mode(link, r_failed_dev);
  2300. /* if transfer mode has changed, set DUBIOUS_XFER on device */
  2301. ata_link_for_each_dev(dev, link) {
  2302. struct ata_eh_context *ehc = &link->eh_context;
  2303. u8 saved_xfer_mode = ehc->saved_xfer_mode[dev->devno];
  2304. u8 saved_ncq = !!(ehc->saved_ncq_enabled & (1 << dev->devno));
  2305. if (dev->xfer_mode != saved_xfer_mode ||
  2306. ata_ncq_enabled(dev) != saved_ncq)
  2307. dev->flags |= ATA_DFLAG_DUBIOUS_XFER;
  2308. }
  2309. return rc;
  2310. }
  2311. static int ata_link_nr_enabled(struct ata_link *link)
  2312. {
  2313. struct ata_device *dev;
  2314. int cnt = 0;
  2315. ata_link_for_each_dev(dev, link)
  2316. if (ata_dev_enabled(dev))
  2317. cnt++;
  2318. return cnt;
  2319. }
  2320. static int ata_link_nr_vacant(struct ata_link *link)
  2321. {
  2322. struct ata_device *dev;
  2323. int cnt = 0;
  2324. ata_link_for_each_dev(dev, link)
  2325. if (dev->class == ATA_DEV_UNKNOWN)
  2326. cnt++;
  2327. return cnt;
  2328. }
  2329. static int ata_eh_skip_recovery(struct ata_link *link)
  2330. {
  2331. struct ata_port *ap = link->ap;
  2332. struct ata_eh_context *ehc = &link->eh_context;
  2333. struct ata_device *dev;
  2334. /* skip disabled links */
  2335. if (link->flags & ATA_LFLAG_DISABLED)
  2336. return 1;
  2337. /* thaw frozen port and recover failed devices */
  2338. if ((ap->pflags & ATA_PFLAG_FROZEN) || ata_link_nr_enabled(link))
  2339. return 0;
  2340. /* reset at least once if reset is requested */
  2341. if ((ehc->i.action & ATA_EH_RESET) &&
  2342. !(ehc->i.flags & ATA_EHI_DID_RESET))
  2343. return 0;
  2344. /* skip if class codes for all vacant slots are ATA_DEV_NONE */
  2345. ata_link_for_each_dev(dev, link) {
  2346. if (dev->class == ATA_DEV_UNKNOWN &&
  2347. ehc->classes[dev->devno] != ATA_DEV_NONE)
  2348. return 0;
  2349. }
  2350. return 1;
  2351. }
  2352. static int ata_eh_schedule_probe(struct ata_device *dev)
  2353. {
  2354. struct ata_eh_context *ehc = &dev->link->eh_context;
  2355. if (!(ehc->i.probe_mask & (1 << dev->devno)) ||
  2356. (ehc->did_probe_mask & (1 << dev->devno)))
  2357. return 0;
  2358. ata_eh_detach_dev(dev);
  2359. ata_dev_init(dev);
  2360. ehc->did_probe_mask |= (1 << dev->devno);
  2361. ehc->i.action |= ATA_EH_RESET;
  2362. ehc->saved_xfer_mode[dev->devno] = 0;
  2363. ehc->saved_ncq_enabled &= ~(1 << dev->devno);
  2364. return 1;
  2365. }
  2366. static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
  2367. {
  2368. struct ata_eh_context *ehc = &dev->link->eh_context;
  2369. ehc->tries[dev->devno]--;
  2370. switch (err) {
  2371. case -ENODEV:
  2372. /* device missing or wrong IDENTIFY data, schedule probing */
  2373. ehc->i.probe_mask |= (1 << dev->devno);
  2374. case -EINVAL:
  2375. /* give it just one more chance */
  2376. ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
  2377. case -EIO:
  2378. if (ehc->tries[dev->devno] == 1 && dev->pio_mode > XFER_PIO_0) {
  2379. /* This is the last chance, better to slow
  2380. * down than lose it.
  2381. */
  2382. sata_down_spd_limit(ata_dev_phys_link(dev));
  2383. ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
  2384. }
  2385. }
  2386. if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
  2387. /* disable device if it has used up all its chances */
  2388. ata_dev_disable(dev);
  2389. /* detach if offline */
  2390. if (ata_phys_link_offline(ata_dev_phys_link(dev)))
  2391. ata_eh_detach_dev(dev);
  2392. /* schedule probe if necessary */
  2393. if (ata_eh_schedule_probe(dev)) {
  2394. ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
  2395. memset(ehc->cmd_timeout_idx[dev->devno], 0,
  2396. sizeof(ehc->cmd_timeout_idx[dev->devno]));
  2397. }
  2398. return 1;
  2399. } else {
  2400. ehc->i.action |= ATA_EH_RESET;
  2401. return 0;
  2402. }
  2403. }
  2404. /**
  2405. * ata_eh_recover - recover host port after error
  2406. * @ap: host port to recover
  2407. * @prereset: prereset method (can be NULL)
  2408. * @softreset: softreset method (can be NULL)
  2409. * @hardreset: hardreset method (can be NULL)
  2410. * @postreset: postreset method (can be NULL)
  2411. * @r_failed_link: out parameter for failed link
  2412. *
  2413. * This is the alpha and omega, eum and yang, heart and soul of
  2414. * libata exception handling. On entry, actions required to
  2415. * recover each link and hotplug requests are recorded in the
  2416. * link's eh_context. This function executes all the operations
  2417. * with appropriate retrials and fallbacks to resurrect failed
  2418. * devices, detach goners and greet newcomers.
  2419. *
  2420. * LOCKING:
  2421. * Kernel thread context (may sleep).
  2422. *
  2423. * RETURNS:
  2424. * 0 on success, -errno on failure.
  2425. */
  2426. int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
  2427. ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
  2428. ata_postreset_fn_t postreset,
  2429. struct ata_link **r_failed_link)
  2430. {
  2431. struct ata_link *link;
  2432. struct ata_device *dev;
  2433. int nr_failed_devs;
  2434. int rc;
  2435. unsigned long flags;
  2436. DPRINTK("ENTER\n");
  2437. /* prep for recovery */
  2438. ata_port_for_each_link(link, ap) {
  2439. struct ata_eh_context *ehc = &link->eh_context;
  2440. /* re-enable link? */
  2441. if (ehc->i.action & ATA_EH_ENABLE_LINK) {
  2442. ata_eh_about_to_do(link, NULL, ATA_EH_ENABLE_LINK);
  2443. spin_lock_irqsave(ap->lock, flags);
  2444. link->flags &= ~ATA_LFLAG_DISABLED;
  2445. spin_unlock_irqrestore(ap->lock, flags);
  2446. ata_eh_done(link, NULL, ATA_EH_ENABLE_LINK);
  2447. }
  2448. ata_link_for_each_dev(dev, link) {
  2449. if (link->flags & ATA_LFLAG_NO_RETRY)
  2450. ehc->tries[dev->devno] = 1;
  2451. else
  2452. ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
  2453. /* collect port action mask recorded in dev actions */
  2454. ehc->i.action |= ehc->i.dev_action[dev->devno] &
  2455. ~ATA_EH_PERDEV_MASK;
  2456. ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK;
  2457. /* process hotplug request */
  2458. if (dev->flags & ATA_DFLAG_DETACH)
  2459. ata_eh_detach_dev(dev);
  2460. /* schedule probe if necessary */
  2461. if (!ata_dev_enabled(dev))
  2462. ata_eh_schedule_probe(dev);
  2463. }
  2464. }
  2465. retry:
  2466. rc = 0;
  2467. nr_failed_devs = 0;
  2468. /* if UNLOADING, finish immediately */
  2469. if (ap->pflags & ATA_PFLAG_UNLOADING)
  2470. goto out;
  2471. /* prep for EH */
  2472. ata_port_for_each_link(link, ap) {
  2473. struct ata_eh_context *ehc = &link->eh_context;
  2474. /* skip EH if possible. */
  2475. if (ata_eh_skip_recovery(link))
  2476. ehc->i.action = 0;
  2477. ata_link_for_each_dev(dev, link)
  2478. ehc->classes[dev->devno] = ATA_DEV_UNKNOWN;
  2479. }
  2480. /* reset */
  2481. ata_port_for_each_link(link, ap) {
  2482. struct ata_eh_context *ehc = &link->eh_context;
  2483. if (!(ehc->i.action & ATA_EH_RESET))
  2484. continue;
  2485. rc = ata_eh_reset(link, ata_link_nr_vacant(link),
  2486. prereset, softreset, hardreset, postreset);
  2487. if (rc) {
  2488. ata_link_printk(link, KERN_ERR,
  2489. "reset failed, giving up\n");
  2490. goto out;
  2491. }
  2492. }
  2493. /* the rest */
  2494. ata_port_for_each_link(link, ap) {
  2495. struct ata_eh_context *ehc = &link->eh_context;
  2496. /* revalidate existing devices and attach new ones */
  2497. rc = ata_eh_revalidate_and_attach(link, &dev);
  2498. if (rc)
  2499. goto dev_fail;
  2500. /* if PMP got attached, return, pmp EH will take care of it */
  2501. if (link->device->class == ATA_DEV_PMP) {
  2502. ehc->i.action = 0;
  2503. return 0;
  2504. }
  2505. /* configure transfer mode if necessary */
  2506. if (ehc->i.flags & ATA_EHI_SETMODE) {
  2507. rc = ata_set_mode(link, &dev);
  2508. if (rc)
  2509. goto dev_fail;
  2510. ehc->i.flags &= ~ATA_EHI_SETMODE;
  2511. }
  2512. if (ehc->i.action & ATA_EH_LPM)
  2513. ata_link_for_each_dev(dev, link)
  2514. ata_dev_enable_pm(dev, ap->pm_policy);
  2515. /* this link is okay now */
  2516. ehc->i.flags = 0;
  2517. continue;
  2518. dev_fail:
  2519. nr_failed_devs++;
  2520. ata_eh_handle_dev_fail(dev, rc);
  2521. if (ap->pflags & ATA_PFLAG_FROZEN) {
  2522. /* PMP reset requires working host port.
  2523. * Can't retry if it's frozen.
  2524. */
  2525. if (sata_pmp_attached(ap))
  2526. goto out;
  2527. break;
  2528. }
  2529. }
  2530. if (nr_failed_devs)
  2531. goto retry;
  2532. out:
  2533. if (rc && r_failed_link)
  2534. *r_failed_link = link;
  2535. DPRINTK("EXIT, rc=%d\n", rc);
  2536. return rc;
  2537. }
  2538. /**
  2539. * ata_eh_finish - finish up EH
  2540. * @ap: host port to finish EH for
  2541. *
  2542. * Recovery is complete. Clean up EH states and retry or finish
  2543. * failed qcs.
  2544. *
  2545. * LOCKING:
  2546. * None.
  2547. */
  2548. void ata_eh_finish(struct ata_port *ap)
  2549. {
  2550. int tag;
  2551. /* retry or finish qcs */
  2552. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  2553. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  2554. if (!(qc->flags & ATA_QCFLAG_FAILED))
  2555. continue;
  2556. if (qc->err_mask) {
  2557. /* FIXME: Once EH migration is complete,
  2558. * generate sense data in this function,
  2559. * considering both err_mask and tf.
  2560. */
  2561. if (qc->flags & ATA_QCFLAG_RETRY)
  2562. ata_eh_qc_retry(qc);
  2563. else
  2564. ata_eh_qc_complete(qc);
  2565. } else {
  2566. if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
  2567. ata_eh_qc_complete(qc);
  2568. } else {
  2569. /* feed zero TF to sense generation */
  2570. memset(&qc->result_tf, 0, sizeof(qc->result_tf));
  2571. ata_eh_qc_retry(qc);
  2572. }
  2573. }
  2574. }
  2575. /* make sure nr_active_links is zero after EH */
  2576. WARN_ON(ap->nr_active_links);
  2577. ap->nr_active_links = 0;
  2578. }
  2579. /**
  2580. * ata_do_eh - do standard error handling
  2581. * @ap: host port to handle error for
  2582. *
  2583. * @prereset: prereset method (can be NULL)
  2584. * @softreset: softreset method (can be NULL)
  2585. * @hardreset: hardreset method (can be NULL)
  2586. * @postreset: postreset method (can be NULL)
  2587. *
  2588. * Perform standard error handling sequence.
  2589. *
  2590. * LOCKING:
  2591. * Kernel thread context (may sleep).
  2592. */
  2593. void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
  2594. ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
  2595. ata_postreset_fn_t postreset)
  2596. {
  2597. struct ata_device *dev;
  2598. int rc;
  2599. ata_eh_autopsy(ap);
  2600. ata_eh_report(ap);
  2601. rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset,
  2602. NULL);
  2603. if (rc) {
  2604. ata_link_for_each_dev(dev, &ap->link)
  2605. ata_dev_disable(dev);
  2606. }
  2607. ata_eh_finish(ap);
  2608. }
  2609. /**
  2610. * ata_std_error_handler - standard error handler
  2611. * @ap: host port to handle error for
  2612. *
  2613. * Standard error handler
  2614. *
  2615. * LOCKING:
  2616. * Kernel thread context (may sleep).
  2617. */
  2618. void ata_std_error_handler(struct ata_port *ap)
  2619. {
  2620. struct ata_port_operations *ops = ap->ops;
  2621. ata_reset_fn_t hardreset = ops->hardreset;
  2622. /* ignore built-in hardreset if SCR access is not available */
  2623. if (ata_is_builtin_hardreset(hardreset) && !sata_scr_valid(&ap->link))
  2624. hardreset = NULL;
  2625. ata_do_eh(ap, ops->prereset, ops->softreset, hardreset, ops->postreset);
  2626. }
  2627. #ifdef CONFIG_PM
  2628. /**
  2629. * ata_eh_handle_port_suspend - perform port suspend operation
  2630. * @ap: port to suspend
  2631. *
  2632. * Suspend @ap.
  2633. *
  2634. * LOCKING:
  2635. * Kernel thread context (may sleep).
  2636. */
  2637. static void ata_eh_handle_port_suspend(struct ata_port *ap)
  2638. {
  2639. unsigned long flags;
  2640. int rc = 0;
  2641. /* are we suspending? */
  2642. spin_lock_irqsave(ap->lock, flags);
  2643. if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
  2644. ap->pm_mesg.event == PM_EVENT_ON) {
  2645. spin_unlock_irqrestore(ap->lock, flags);
  2646. return;
  2647. }
  2648. spin_unlock_irqrestore(ap->lock, flags);
  2649. WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
  2650. /* tell ACPI we're suspending */
  2651. rc = ata_acpi_on_suspend(ap);
  2652. if (rc)
  2653. goto out;
  2654. /* suspend */
  2655. ata_eh_freeze_port(ap);
  2656. if (ap->ops->port_suspend)
  2657. rc = ap->ops->port_suspend(ap, ap->pm_mesg);
  2658. ata_acpi_set_state(ap, PMSG_SUSPEND);
  2659. out:
  2660. /* report result */
  2661. spin_lock_irqsave(ap->lock, flags);
  2662. ap->pflags &= ~ATA_PFLAG_PM_PENDING;
  2663. if (rc == 0)
  2664. ap->pflags |= ATA_PFLAG_SUSPENDED;
  2665. else if (ap->pflags & ATA_PFLAG_FROZEN)
  2666. ata_port_schedule_eh(ap);
  2667. if (ap->pm_result) {
  2668. *ap->pm_result = rc;
  2669. ap->pm_result = NULL;
  2670. }
  2671. spin_unlock_irqrestore(ap->lock, flags);
  2672. return;
  2673. }
  2674. /**
  2675. * ata_eh_handle_port_resume - perform port resume operation
  2676. * @ap: port to resume
  2677. *
  2678. * Resume @ap.
  2679. *
  2680. * LOCKING:
  2681. * Kernel thread context (may sleep).
  2682. */
  2683. static void ata_eh_handle_port_resume(struct ata_port *ap)
  2684. {
  2685. unsigned long flags;
  2686. int rc = 0;
  2687. /* are we resuming? */
  2688. spin_lock_irqsave(ap->lock, flags);
  2689. if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
  2690. ap->pm_mesg.event != PM_EVENT_ON) {
  2691. spin_unlock_irqrestore(ap->lock, flags);
  2692. return;
  2693. }
  2694. spin_unlock_irqrestore(ap->lock, flags);
  2695. WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
  2696. ata_acpi_set_state(ap, PMSG_ON);
  2697. if (ap->ops->port_resume)
  2698. rc = ap->ops->port_resume(ap);
  2699. /* tell ACPI that we're resuming */
  2700. ata_acpi_on_resume(ap);
  2701. /* report result */
  2702. spin_lock_irqsave(ap->lock, flags);
  2703. ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
  2704. if (ap->pm_result) {
  2705. *ap->pm_result = rc;
  2706. ap->pm_result = NULL;
  2707. }
  2708. spin_unlock_irqrestore(ap->lock, flags);
  2709. }
  2710. #endif /* CONFIG_PM */