libata-eh.c 110 KB

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