libata-eh.c 110 KB

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