libata-eh.c 106 KB

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