libata-eh.c 114 KB

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