libata-eh.c 106 KB

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