libata-eh.c 108 KB

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