libata-eh.c 112 KB

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