libata-eh.c 108 KB

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