libata-eh.c 112 KB

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