libata-eh.c 109 KB

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