libata-eh.c 99 KB

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