libata-eh.c 107 KB

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