libata-eh.c 109 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173
  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(!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. unsigned long ap_flags = dev->link->ap->flags;
  1291. struct ata_taskfile tf;
  1292. unsigned int err_mask;
  1293. bool dma = false;
  1294. DPRINTK("read log page - log 0x%x, page 0x%x\n", log, page);
  1295. /*
  1296. * Return error without actually issuing the command on controllers
  1297. * which e.g. lockup on a read log page.
  1298. */
  1299. if (ap_flags & ATA_FLAG_NO_LOG_PAGE)
  1300. return AC_ERR_DEV;
  1301. retry:
  1302. ata_tf_init(dev, &tf);
  1303. if (dev->dma_mode && ata_id_has_read_log_dma_ext(dev->id) &&
  1304. !(dev->horkage & ATA_HORKAGE_NO_NCQ_LOG)) {
  1305. tf.command = ATA_CMD_READ_LOG_DMA_EXT;
  1306. tf.protocol = ATA_PROT_DMA;
  1307. dma = true;
  1308. } else {
  1309. tf.command = ATA_CMD_READ_LOG_EXT;
  1310. tf.protocol = ATA_PROT_PIO;
  1311. dma = false;
  1312. }
  1313. tf.lbal = log;
  1314. tf.lbam = page;
  1315. tf.nsect = sectors;
  1316. tf.hob_nsect = sectors >> 8;
  1317. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
  1318. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
  1319. buf, sectors * ATA_SECT_SIZE, 0);
  1320. if (err_mask && dma) {
  1321. dev->horkage |= ATA_HORKAGE_NO_NCQ_LOG;
  1322. ata_dev_warn(dev, "READ LOG DMA EXT failed, trying unqueued\n");
  1323. goto retry;
  1324. }
  1325. DPRINTK("EXIT, err_mask=%x\n", err_mask);
  1326. return err_mask;
  1327. }
  1328. /**
  1329. * ata_eh_read_log_10h - Read log page 10h for NCQ error details
  1330. * @dev: Device to read log page 10h from
  1331. * @tag: Resulting tag of the failed command
  1332. * @tf: Resulting taskfile registers of the failed command
  1333. *
  1334. * Read log page 10h to obtain NCQ error details and clear error
  1335. * condition.
  1336. *
  1337. * LOCKING:
  1338. * Kernel thread context (may sleep).
  1339. *
  1340. * RETURNS:
  1341. * 0 on success, -errno otherwise.
  1342. */
  1343. static int ata_eh_read_log_10h(struct ata_device *dev,
  1344. int *tag, struct ata_taskfile *tf)
  1345. {
  1346. u8 *buf = dev->link->ap->sector_buf;
  1347. unsigned int err_mask;
  1348. u8 csum;
  1349. int i;
  1350. err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, 0, buf, 1);
  1351. if (err_mask)
  1352. return -EIO;
  1353. csum = 0;
  1354. for (i = 0; i < ATA_SECT_SIZE; i++)
  1355. csum += buf[i];
  1356. if (csum)
  1357. ata_dev_warn(dev, "invalid checksum 0x%x on log page 10h\n",
  1358. csum);
  1359. if (buf[0] & 0x80)
  1360. return -ENOENT;
  1361. *tag = buf[0] & 0x1f;
  1362. tf->command = buf[2];
  1363. tf->feature = buf[3];
  1364. tf->lbal = buf[4];
  1365. tf->lbam = buf[5];
  1366. tf->lbah = buf[6];
  1367. tf->device = buf[7];
  1368. tf->hob_lbal = buf[8];
  1369. tf->hob_lbam = buf[9];
  1370. tf->hob_lbah = buf[10];
  1371. tf->nsect = buf[12];
  1372. tf->hob_nsect = buf[13];
  1373. return 0;
  1374. }
  1375. /**
  1376. * atapi_eh_tur - perform ATAPI TEST_UNIT_READY
  1377. * @dev: target ATAPI device
  1378. * @r_sense_key: out parameter for sense_key
  1379. *
  1380. * Perform ATAPI TEST_UNIT_READY.
  1381. *
  1382. * LOCKING:
  1383. * EH context (may sleep).
  1384. *
  1385. * RETURNS:
  1386. * 0 on success, AC_ERR_* mask on failure.
  1387. */
  1388. unsigned int atapi_eh_tur(struct ata_device *dev, u8 *r_sense_key)
  1389. {
  1390. u8 cdb[ATAPI_CDB_LEN] = { TEST_UNIT_READY, 0, 0, 0, 0, 0 };
  1391. struct ata_taskfile tf;
  1392. unsigned int err_mask;
  1393. ata_tf_init(dev, &tf);
  1394. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  1395. tf.command = ATA_CMD_PACKET;
  1396. tf.protocol = ATAPI_PROT_NODATA;
  1397. err_mask = ata_exec_internal(dev, &tf, cdb, DMA_NONE, NULL, 0, 0);
  1398. if (err_mask == AC_ERR_DEV)
  1399. *r_sense_key = tf.feature >> 4;
  1400. return err_mask;
  1401. }
  1402. /**
  1403. * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
  1404. * @dev: device to perform REQUEST_SENSE to
  1405. * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
  1406. * @dfl_sense_key: default sense key to use
  1407. *
  1408. * Perform ATAPI REQUEST_SENSE after the device reported CHECK
  1409. * SENSE. This function is EH helper.
  1410. *
  1411. * LOCKING:
  1412. * Kernel thread context (may sleep).
  1413. *
  1414. * RETURNS:
  1415. * 0 on success, AC_ERR_* mask on failure
  1416. */
  1417. unsigned int atapi_eh_request_sense(struct ata_device *dev,
  1418. u8 *sense_buf, u8 dfl_sense_key)
  1419. {
  1420. u8 cdb[ATAPI_CDB_LEN] =
  1421. { REQUEST_SENSE, 0, 0, 0, SCSI_SENSE_BUFFERSIZE, 0 };
  1422. struct ata_port *ap = dev->link->ap;
  1423. struct ata_taskfile tf;
  1424. DPRINTK("ATAPI request sense\n");
  1425. memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
  1426. /* initialize sense_buf with the error register,
  1427. * for the case where they are -not- overwritten
  1428. */
  1429. sense_buf[0] = 0x70;
  1430. sense_buf[2] = dfl_sense_key;
  1431. /* some devices time out if garbage left in tf */
  1432. ata_tf_init(dev, &tf);
  1433. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  1434. tf.command = ATA_CMD_PACKET;
  1435. /* is it pointless to prefer PIO for "safety reasons"? */
  1436. if (ap->flags & ATA_FLAG_PIO_DMA) {
  1437. tf.protocol = ATAPI_PROT_DMA;
  1438. tf.feature |= ATAPI_PKT_DMA;
  1439. } else {
  1440. tf.protocol = ATAPI_PROT_PIO;
  1441. tf.lbam = SCSI_SENSE_BUFFERSIZE;
  1442. tf.lbah = 0;
  1443. }
  1444. return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
  1445. sense_buf, SCSI_SENSE_BUFFERSIZE, 0);
  1446. }
  1447. /**
  1448. * ata_eh_analyze_serror - analyze SError for a failed port
  1449. * @link: ATA link to analyze SError for
  1450. *
  1451. * Analyze SError if available and further determine cause of
  1452. * failure.
  1453. *
  1454. * LOCKING:
  1455. * None.
  1456. */
  1457. static void ata_eh_analyze_serror(struct ata_link *link)
  1458. {
  1459. struct ata_eh_context *ehc = &link->eh_context;
  1460. u32 serror = ehc->i.serror;
  1461. unsigned int err_mask = 0, action = 0;
  1462. u32 hotplug_mask;
  1463. if (serror & (SERR_PERSISTENT | SERR_DATA)) {
  1464. err_mask |= AC_ERR_ATA_BUS;
  1465. action |= ATA_EH_RESET;
  1466. }
  1467. if (serror & SERR_PROTOCOL) {
  1468. err_mask |= AC_ERR_HSM;
  1469. action |= ATA_EH_RESET;
  1470. }
  1471. if (serror & SERR_INTERNAL) {
  1472. err_mask |= AC_ERR_SYSTEM;
  1473. action |= ATA_EH_RESET;
  1474. }
  1475. /* Determine whether a hotplug event has occurred. Both
  1476. * SError.N/X are considered hotplug events for enabled or
  1477. * host links. For disabled PMP links, only N bit is
  1478. * considered as X bit is left at 1 for link plugging.
  1479. */
  1480. if (link->lpm_policy > ATA_LPM_MAX_POWER)
  1481. hotplug_mask = 0; /* hotplug doesn't work w/ LPM */
  1482. else if (!(link->flags & ATA_LFLAG_DISABLED) || ata_is_host_link(link))
  1483. hotplug_mask = SERR_PHYRDY_CHG | SERR_DEV_XCHG;
  1484. else
  1485. hotplug_mask = SERR_PHYRDY_CHG;
  1486. if (serror & hotplug_mask)
  1487. ata_ehi_hotplugged(&ehc->i);
  1488. ehc->i.err_mask |= err_mask;
  1489. ehc->i.action |= action;
  1490. }
  1491. /**
  1492. * ata_eh_analyze_ncq_error - analyze NCQ error
  1493. * @link: ATA link to analyze NCQ error for
  1494. *
  1495. * Read log page 10h, determine the offending qc and acquire
  1496. * error status TF. For NCQ device errors, all LLDDs have to do
  1497. * is setting AC_ERR_DEV in ehi->err_mask. This function takes
  1498. * care of the rest.
  1499. *
  1500. * LOCKING:
  1501. * Kernel thread context (may sleep).
  1502. */
  1503. void ata_eh_analyze_ncq_error(struct ata_link *link)
  1504. {
  1505. struct ata_port *ap = link->ap;
  1506. struct ata_eh_context *ehc = &link->eh_context;
  1507. struct ata_device *dev = link->device;
  1508. struct ata_queued_cmd *qc;
  1509. struct ata_taskfile tf;
  1510. int tag, rc;
  1511. /* if frozen, we can't do much */
  1512. if (ap->pflags & ATA_PFLAG_FROZEN)
  1513. return;
  1514. /* is it NCQ device error? */
  1515. if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
  1516. return;
  1517. /* has LLDD analyzed already? */
  1518. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  1519. qc = __ata_qc_from_tag(ap, tag);
  1520. if (!(qc->flags & ATA_QCFLAG_FAILED))
  1521. continue;
  1522. if (qc->err_mask)
  1523. return;
  1524. }
  1525. /* okay, this error is ours */
  1526. memset(&tf, 0, sizeof(tf));
  1527. rc = ata_eh_read_log_10h(dev, &tag, &tf);
  1528. if (rc) {
  1529. ata_link_err(link, "failed to read log page 10h (errno=%d)\n",
  1530. rc);
  1531. return;
  1532. }
  1533. if (!(link->sactive & (1 << tag))) {
  1534. ata_link_err(link, "log page 10h reported inactive tag %d\n",
  1535. tag);
  1536. return;
  1537. }
  1538. /* we've got the perpetrator, condemn it */
  1539. qc = __ata_qc_from_tag(ap, tag);
  1540. memcpy(&qc->result_tf, &tf, sizeof(tf));
  1541. qc->result_tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
  1542. qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
  1543. ehc->i.err_mask &= ~AC_ERR_DEV;
  1544. }
  1545. /**
  1546. * ata_eh_analyze_tf - analyze taskfile of a failed qc
  1547. * @qc: qc to analyze
  1548. * @tf: Taskfile registers to analyze
  1549. *
  1550. * Analyze taskfile of @qc and further determine cause of
  1551. * failure. This function also requests ATAPI sense data if
  1552. * available.
  1553. *
  1554. * LOCKING:
  1555. * Kernel thread context (may sleep).
  1556. *
  1557. * RETURNS:
  1558. * Determined recovery action
  1559. */
  1560. static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
  1561. const struct ata_taskfile *tf)
  1562. {
  1563. unsigned int tmp, action = 0;
  1564. u8 stat = tf->command, err = tf->feature;
  1565. if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
  1566. qc->err_mask |= AC_ERR_HSM;
  1567. return ATA_EH_RESET;
  1568. }
  1569. if (stat & (ATA_ERR | ATA_DF))
  1570. qc->err_mask |= AC_ERR_DEV;
  1571. else
  1572. return 0;
  1573. switch (qc->dev->class) {
  1574. case ATA_DEV_ATA:
  1575. case ATA_DEV_ZAC:
  1576. if (err & ATA_ICRC)
  1577. qc->err_mask |= AC_ERR_ATA_BUS;
  1578. if (err & (ATA_UNC | ATA_AMNF))
  1579. qc->err_mask |= AC_ERR_MEDIA;
  1580. if (err & ATA_IDNF)
  1581. qc->err_mask |= AC_ERR_INVALID;
  1582. break;
  1583. case ATA_DEV_ATAPI:
  1584. if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
  1585. tmp = atapi_eh_request_sense(qc->dev,
  1586. qc->scsicmd->sense_buffer,
  1587. qc->result_tf.feature >> 4);
  1588. if (!tmp) {
  1589. /* ATA_QCFLAG_SENSE_VALID is used to
  1590. * tell atapi_qc_complete() that sense
  1591. * data is already valid.
  1592. *
  1593. * TODO: interpret sense data and set
  1594. * appropriate err_mask.
  1595. */
  1596. qc->flags |= ATA_QCFLAG_SENSE_VALID;
  1597. } else
  1598. qc->err_mask |= tmp;
  1599. }
  1600. }
  1601. if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
  1602. action |= ATA_EH_RESET;
  1603. return action;
  1604. }
  1605. static int ata_eh_categorize_error(unsigned int eflags, unsigned int err_mask,
  1606. int *xfer_ok)
  1607. {
  1608. int base = 0;
  1609. if (!(eflags & ATA_EFLAG_DUBIOUS_XFER))
  1610. *xfer_ok = 1;
  1611. if (!*xfer_ok)
  1612. base = ATA_ECAT_DUBIOUS_NONE;
  1613. if (err_mask & AC_ERR_ATA_BUS)
  1614. return base + ATA_ECAT_ATA_BUS;
  1615. if (err_mask & AC_ERR_TIMEOUT)
  1616. return base + ATA_ECAT_TOUT_HSM;
  1617. if (eflags & ATA_EFLAG_IS_IO) {
  1618. if (err_mask & AC_ERR_HSM)
  1619. return base + ATA_ECAT_TOUT_HSM;
  1620. if ((err_mask &
  1621. (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
  1622. return base + ATA_ECAT_UNK_DEV;
  1623. }
  1624. return 0;
  1625. }
  1626. struct speed_down_verdict_arg {
  1627. u64 since;
  1628. int xfer_ok;
  1629. int nr_errors[ATA_ECAT_NR];
  1630. };
  1631. static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
  1632. {
  1633. struct speed_down_verdict_arg *arg = void_arg;
  1634. int cat;
  1635. if ((ent->eflags & ATA_EFLAG_OLD_ER) || (ent->timestamp < arg->since))
  1636. return -1;
  1637. cat = ata_eh_categorize_error(ent->eflags, ent->err_mask,
  1638. &arg->xfer_ok);
  1639. arg->nr_errors[cat]++;
  1640. return 0;
  1641. }
  1642. /**
  1643. * ata_eh_speed_down_verdict - Determine speed down verdict
  1644. * @dev: Device of interest
  1645. *
  1646. * This function examines error ring of @dev and determines
  1647. * whether NCQ needs to be turned off, transfer speed should be
  1648. * stepped down, or falling back to PIO is necessary.
  1649. *
  1650. * ECAT_ATA_BUS : ATA_BUS error for any command
  1651. *
  1652. * ECAT_TOUT_HSM : TIMEOUT for any command or HSM violation for
  1653. * IO commands
  1654. *
  1655. * ECAT_UNK_DEV : Unknown DEV error for IO commands
  1656. *
  1657. * ECAT_DUBIOUS_* : Identical to above three but occurred while
  1658. * data transfer hasn't been verified.
  1659. *
  1660. * Verdicts are
  1661. *
  1662. * NCQ_OFF : Turn off NCQ.
  1663. *
  1664. * SPEED_DOWN : Speed down transfer speed but don't fall back
  1665. * to PIO.
  1666. *
  1667. * FALLBACK_TO_PIO : Fall back to PIO.
  1668. *
  1669. * Even if multiple verdicts are returned, only one action is
  1670. * taken per error. An action triggered by non-DUBIOUS errors
  1671. * clears ering, while one triggered by DUBIOUS_* errors doesn't.
  1672. * This is to expedite speed down decisions right after device is
  1673. * initially configured.
  1674. *
  1675. * The followings are speed down rules. #1 and #2 deal with
  1676. * DUBIOUS errors.
  1677. *
  1678. * 1. If more than one DUBIOUS_ATA_BUS or DUBIOUS_TOUT_HSM errors
  1679. * occurred during last 5 mins, SPEED_DOWN and FALLBACK_TO_PIO.
  1680. *
  1681. * 2. If more than one DUBIOUS_TOUT_HSM or DUBIOUS_UNK_DEV errors
  1682. * occurred during last 5 mins, NCQ_OFF.
  1683. *
  1684. * 3. If more than 8 ATA_BUS, TOUT_HSM or UNK_DEV errors
  1685. * occurred during last 5 mins, FALLBACK_TO_PIO
  1686. *
  1687. * 4. If more than 3 TOUT_HSM or UNK_DEV errors occurred
  1688. * during last 10 mins, NCQ_OFF.
  1689. *
  1690. * 5. If more than 3 ATA_BUS or TOUT_HSM errors, or more than 6
  1691. * UNK_DEV errors occurred during last 10 mins, SPEED_DOWN.
  1692. *
  1693. * LOCKING:
  1694. * Inherited from caller.
  1695. *
  1696. * RETURNS:
  1697. * OR of ATA_EH_SPDN_* flags.
  1698. */
  1699. static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
  1700. {
  1701. const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
  1702. u64 j64 = get_jiffies_64();
  1703. struct speed_down_verdict_arg arg;
  1704. unsigned int verdict = 0;
  1705. /* scan past 5 mins of error history */
  1706. memset(&arg, 0, sizeof(arg));
  1707. arg.since = j64 - min(j64, j5mins);
  1708. ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
  1709. if (arg.nr_errors[ATA_ECAT_DUBIOUS_ATA_BUS] +
  1710. arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] > 1)
  1711. verdict |= ATA_EH_SPDN_SPEED_DOWN |
  1712. ATA_EH_SPDN_FALLBACK_TO_PIO | ATA_EH_SPDN_KEEP_ERRORS;
  1713. if (arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] +
  1714. arg.nr_errors[ATA_ECAT_DUBIOUS_UNK_DEV] > 1)
  1715. verdict |= ATA_EH_SPDN_NCQ_OFF | ATA_EH_SPDN_KEEP_ERRORS;
  1716. if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
  1717. arg.nr_errors[ATA_ECAT_TOUT_HSM] +
  1718. arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
  1719. verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
  1720. /* scan past 10 mins of error history */
  1721. memset(&arg, 0, sizeof(arg));
  1722. arg.since = j64 - min(j64, j10mins);
  1723. ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
  1724. if (arg.nr_errors[ATA_ECAT_TOUT_HSM] +
  1725. arg.nr_errors[ATA_ECAT_UNK_DEV] > 3)
  1726. verdict |= ATA_EH_SPDN_NCQ_OFF;
  1727. if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
  1728. arg.nr_errors[ATA_ECAT_TOUT_HSM] > 3 ||
  1729. arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
  1730. verdict |= ATA_EH_SPDN_SPEED_DOWN;
  1731. return verdict;
  1732. }
  1733. /**
  1734. * ata_eh_speed_down - record error and speed down if necessary
  1735. * @dev: Failed device
  1736. * @eflags: mask of ATA_EFLAG_* flags
  1737. * @err_mask: err_mask of the error
  1738. *
  1739. * Record error and examine error history to determine whether
  1740. * adjusting transmission speed is necessary. It also sets
  1741. * transmission limits appropriately if such adjustment is
  1742. * necessary.
  1743. *
  1744. * LOCKING:
  1745. * Kernel thread context (may sleep).
  1746. *
  1747. * RETURNS:
  1748. * Determined recovery action.
  1749. */
  1750. static unsigned int ata_eh_speed_down(struct ata_device *dev,
  1751. unsigned int eflags, unsigned int err_mask)
  1752. {
  1753. struct ata_link *link = ata_dev_phys_link(dev);
  1754. int xfer_ok = 0;
  1755. unsigned int verdict;
  1756. unsigned int action = 0;
  1757. /* don't bother if Cat-0 error */
  1758. if (ata_eh_categorize_error(eflags, err_mask, &xfer_ok) == 0)
  1759. return 0;
  1760. /* record error and determine whether speed down is necessary */
  1761. ata_ering_record(&dev->ering, eflags, err_mask);
  1762. verdict = ata_eh_speed_down_verdict(dev);
  1763. /* turn off NCQ? */
  1764. if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
  1765. (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
  1766. ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
  1767. dev->flags |= ATA_DFLAG_NCQ_OFF;
  1768. ata_dev_warn(dev, "NCQ disabled due to excessive errors\n");
  1769. goto done;
  1770. }
  1771. /* speed down? */
  1772. if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
  1773. /* speed down SATA link speed if possible */
  1774. if (sata_down_spd_limit(link, 0) == 0) {
  1775. action |= ATA_EH_RESET;
  1776. goto done;
  1777. }
  1778. /* lower transfer mode */
  1779. if (dev->spdn_cnt < 2) {
  1780. static const int dma_dnxfer_sel[] =
  1781. { ATA_DNXFER_DMA, ATA_DNXFER_40C };
  1782. static const int pio_dnxfer_sel[] =
  1783. { ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
  1784. int sel;
  1785. if (dev->xfer_shift != ATA_SHIFT_PIO)
  1786. sel = dma_dnxfer_sel[dev->spdn_cnt];
  1787. else
  1788. sel = pio_dnxfer_sel[dev->spdn_cnt];
  1789. dev->spdn_cnt++;
  1790. if (ata_down_xfermask_limit(dev, sel) == 0) {
  1791. action |= ATA_EH_RESET;
  1792. goto done;
  1793. }
  1794. }
  1795. }
  1796. /* Fall back to PIO? Slowing down to PIO is meaningless for
  1797. * SATA ATA devices. Consider it only for PATA and SATAPI.
  1798. */
  1799. if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
  1800. (link->ap->cbl != ATA_CBL_SATA || dev->class == ATA_DEV_ATAPI) &&
  1801. (dev->xfer_shift != ATA_SHIFT_PIO)) {
  1802. if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
  1803. dev->spdn_cnt = 0;
  1804. action |= ATA_EH_RESET;
  1805. goto done;
  1806. }
  1807. }
  1808. return 0;
  1809. done:
  1810. /* device has been slowed down, blow error history */
  1811. if (!(verdict & ATA_EH_SPDN_KEEP_ERRORS))
  1812. ata_ering_clear(&dev->ering);
  1813. return action;
  1814. }
  1815. /**
  1816. * ata_eh_worth_retry - analyze error and decide whether to retry
  1817. * @qc: qc to possibly retry
  1818. *
  1819. * Look at the cause of the error and decide if a retry
  1820. * might be useful or not. We don't want to retry media errors
  1821. * because the drive itself has probably already taken 10-30 seconds
  1822. * doing its own internal retries before reporting the failure.
  1823. */
  1824. static inline int ata_eh_worth_retry(struct ata_queued_cmd *qc)
  1825. {
  1826. if (qc->err_mask & AC_ERR_MEDIA)
  1827. return 0; /* don't retry media errors */
  1828. if (qc->flags & ATA_QCFLAG_IO)
  1829. return 1; /* otherwise retry anything from fs stack */
  1830. if (qc->err_mask & AC_ERR_INVALID)
  1831. return 0; /* don't retry these */
  1832. return qc->err_mask != AC_ERR_DEV; /* retry if not dev error */
  1833. }
  1834. /**
  1835. * ata_eh_link_autopsy - analyze error and determine recovery action
  1836. * @link: host link to perform autopsy on
  1837. *
  1838. * Analyze why @link failed and determine which recovery actions
  1839. * are needed. This function also sets more detailed AC_ERR_*
  1840. * values and fills sense data for ATAPI CHECK SENSE.
  1841. *
  1842. * LOCKING:
  1843. * Kernel thread context (may sleep).
  1844. */
  1845. static void ata_eh_link_autopsy(struct ata_link *link)
  1846. {
  1847. struct ata_port *ap = link->ap;
  1848. struct ata_eh_context *ehc = &link->eh_context;
  1849. struct ata_device *dev;
  1850. unsigned int all_err_mask = 0, eflags = 0;
  1851. int tag;
  1852. u32 serror;
  1853. int rc;
  1854. DPRINTK("ENTER\n");
  1855. if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
  1856. return;
  1857. /* obtain and analyze SError */
  1858. rc = sata_scr_read(link, SCR_ERROR, &serror);
  1859. if (rc == 0) {
  1860. ehc->i.serror |= serror;
  1861. ata_eh_analyze_serror(link);
  1862. } else if (rc != -EOPNOTSUPP) {
  1863. /* SError read failed, force reset and probing */
  1864. ehc->i.probe_mask |= ATA_ALL_DEVICES;
  1865. ehc->i.action |= ATA_EH_RESET;
  1866. ehc->i.err_mask |= AC_ERR_OTHER;
  1867. }
  1868. /* analyze NCQ failure */
  1869. ata_eh_analyze_ncq_error(link);
  1870. /* any real error trumps AC_ERR_OTHER */
  1871. if (ehc->i.err_mask & ~AC_ERR_OTHER)
  1872. ehc->i.err_mask &= ~AC_ERR_OTHER;
  1873. all_err_mask |= ehc->i.err_mask;
  1874. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  1875. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  1876. if (!(qc->flags & ATA_QCFLAG_FAILED) ||
  1877. ata_dev_phys_link(qc->dev) != link)
  1878. continue;
  1879. /* inherit upper level err_mask */
  1880. qc->err_mask |= ehc->i.err_mask;
  1881. /* analyze TF */
  1882. ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
  1883. /* DEV errors are probably spurious in case of ATA_BUS error */
  1884. if (qc->err_mask & AC_ERR_ATA_BUS)
  1885. qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
  1886. AC_ERR_INVALID);
  1887. /* any real error trumps unknown error */
  1888. if (qc->err_mask & ~AC_ERR_OTHER)
  1889. qc->err_mask &= ~AC_ERR_OTHER;
  1890. /*
  1891. * SENSE_VALID trumps dev/unknown error and revalidation. Upper
  1892. * layers will determine whether the command is worth retrying
  1893. * based on the sense data and device class/type. Otherwise,
  1894. * determine directly if the command is worth retrying using its
  1895. * error mask and flags.
  1896. */
  1897. if (qc->flags & ATA_QCFLAG_SENSE_VALID)
  1898. qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
  1899. else if (ata_eh_worth_retry(qc))
  1900. qc->flags |= ATA_QCFLAG_RETRY;
  1901. /* accumulate error info */
  1902. ehc->i.dev = qc->dev;
  1903. all_err_mask |= qc->err_mask;
  1904. if (qc->flags & ATA_QCFLAG_IO)
  1905. eflags |= ATA_EFLAG_IS_IO;
  1906. trace_ata_eh_link_autopsy_qc(qc);
  1907. }
  1908. /* enforce default EH actions */
  1909. if (ap->pflags & ATA_PFLAG_FROZEN ||
  1910. all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
  1911. ehc->i.action |= ATA_EH_RESET;
  1912. else if (((eflags & ATA_EFLAG_IS_IO) && all_err_mask) ||
  1913. (!(eflags & ATA_EFLAG_IS_IO) && (all_err_mask & ~AC_ERR_DEV)))
  1914. ehc->i.action |= ATA_EH_REVALIDATE;
  1915. /* If we have offending qcs and the associated failed device,
  1916. * perform per-dev EH action only on the offending device.
  1917. */
  1918. if (ehc->i.dev) {
  1919. ehc->i.dev_action[ehc->i.dev->devno] |=
  1920. ehc->i.action & ATA_EH_PERDEV_MASK;
  1921. ehc->i.action &= ~ATA_EH_PERDEV_MASK;
  1922. }
  1923. /* propagate timeout to host link */
  1924. if ((all_err_mask & AC_ERR_TIMEOUT) && !ata_is_host_link(link))
  1925. ap->link.eh_context.i.err_mask |= AC_ERR_TIMEOUT;
  1926. /* record error and consider speeding down */
  1927. dev = ehc->i.dev;
  1928. if (!dev && ((ata_link_max_devices(link) == 1 &&
  1929. ata_dev_enabled(link->device))))
  1930. dev = link->device;
  1931. if (dev) {
  1932. if (dev->flags & ATA_DFLAG_DUBIOUS_XFER)
  1933. eflags |= ATA_EFLAG_DUBIOUS_XFER;
  1934. ehc->i.action |= ata_eh_speed_down(dev, eflags, all_err_mask);
  1935. trace_ata_eh_link_autopsy(dev, ehc->i.action, all_err_mask);
  1936. }
  1937. DPRINTK("EXIT\n");
  1938. }
  1939. /**
  1940. * ata_eh_autopsy - analyze error and determine recovery action
  1941. * @ap: host port to perform autopsy on
  1942. *
  1943. * Analyze all links of @ap and determine why they failed and
  1944. * which recovery actions are needed.
  1945. *
  1946. * LOCKING:
  1947. * Kernel thread context (may sleep).
  1948. */
  1949. void ata_eh_autopsy(struct ata_port *ap)
  1950. {
  1951. struct ata_link *link;
  1952. ata_for_each_link(link, ap, EDGE)
  1953. ata_eh_link_autopsy(link);
  1954. /* Handle the frigging slave link. Autopsy is done similarly
  1955. * but actions and flags are transferred over to the master
  1956. * link and handled from there.
  1957. */
  1958. if (ap->slave_link) {
  1959. struct ata_eh_context *mehc = &ap->link.eh_context;
  1960. struct ata_eh_context *sehc = &ap->slave_link->eh_context;
  1961. /* transfer control flags from master to slave */
  1962. sehc->i.flags |= mehc->i.flags & ATA_EHI_TO_SLAVE_MASK;
  1963. /* perform autopsy on the slave link */
  1964. ata_eh_link_autopsy(ap->slave_link);
  1965. /* transfer actions from slave to master and clear slave */
  1966. ata_eh_about_to_do(ap->slave_link, NULL, ATA_EH_ALL_ACTIONS);
  1967. mehc->i.action |= sehc->i.action;
  1968. mehc->i.dev_action[1] |= sehc->i.dev_action[1];
  1969. mehc->i.flags |= sehc->i.flags;
  1970. ata_eh_done(ap->slave_link, NULL, ATA_EH_ALL_ACTIONS);
  1971. }
  1972. /* Autopsy of fanout ports can affect host link autopsy.
  1973. * Perform host link autopsy last.
  1974. */
  1975. if (sata_pmp_attached(ap))
  1976. ata_eh_link_autopsy(&ap->link);
  1977. }
  1978. /**
  1979. * ata_get_cmd_descript - get description for ATA command
  1980. * @command: ATA command code to get description for
  1981. *
  1982. * Return a textual description of the given command, or NULL if the
  1983. * command is not known.
  1984. *
  1985. * LOCKING:
  1986. * None
  1987. */
  1988. const char *ata_get_cmd_descript(u8 command)
  1989. {
  1990. #ifdef CONFIG_ATA_VERBOSE_ERROR
  1991. static const struct
  1992. {
  1993. u8 command;
  1994. const char *text;
  1995. } cmd_descr[] = {
  1996. { ATA_CMD_DEV_RESET, "DEVICE RESET" },
  1997. { ATA_CMD_CHK_POWER, "CHECK POWER MODE" },
  1998. { ATA_CMD_STANDBY, "STANDBY" },
  1999. { ATA_CMD_IDLE, "IDLE" },
  2000. { ATA_CMD_EDD, "EXECUTE DEVICE DIAGNOSTIC" },
  2001. { ATA_CMD_DOWNLOAD_MICRO, "DOWNLOAD MICROCODE" },
  2002. { ATA_CMD_DOWNLOAD_MICRO_DMA, "DOWNLOAD MICROCODE DMA" },
  2003. { ATA_CMD_NOP, "NOP" },
  2004. { ATA_CMD_FLUSH, "FLUSH CACHE" },
  2005. { ATA_CMD_FLUSH_EXT, "FLUSH CACHE EXT" },
  2006. { ATA_CMD_ID_ATA, "IDENTIFY DEVICE" },
  2007. { ATA_CMD_ID_ATAPI, "IDENTIFY PACKET DEVICE" },
  2008. { ATA_CMD_SERVICE, "SERVICE" },
  2009. { ATA_CMD_READ, "READ DMA" },
  2010. { ATA_CMD_READ_EXT, "READ DMA EXT" },
  2011. { ATA_CMD_READ_QUEUED, "READ DMA QUEUED" },
  2012. { ATA_CMD_READ_STREAM_EXT, "READ STREAM EXT" },
  2013. { ATA_CMD_READ_STREAM_DMA_EXT, "READ STREAM DMA EXT" },
  2014. { ATA_CMD_WRITE, "WRITE DMA" },
  2015. { ATA_CMD_WRITE_EXT, "WRITE DMA EXT" },
  2016. { ATA_CMD_WRITE_QUEUED, "WRITE DMA QUEUED EXT" },
  2017. { ATA_CMD_WRITE_STREAM_EXT, "WRITE STREAM EXT" },
  2018. { ATA_CMD_WRITE_STREAM_DMA_EXT, "WRITE STREAM DMA EXT" },
  2019. { ATA_CMD_WRITE_FUA_EXT, "WRITE DMA FUA EXT" },
  2020. { ATA_CMD_WRITE_QUEUED_FUA_EXT, "WRITE DMA QUEUED FUA EXT" },
  2021. { ATA_CMD_FPDMA_READ, "READ FPDMA QUEUED" },
  2022. { ATA_CMD_FPDMA_WRITE, "WRITE FPDMA QUEUED" },
  2023. { ATA_CMD_FPDMA_SEND, "SEND FPDMA QUEUED" },
  2024. { ATA_CMD_FPDMA_RECV, "RECEIVE FPDMA QUEUED" },
  2025. { ATA_CMD_PIO_READ, "READ SECTOR(S)" },
  2026. { ATA_CMD_PIO_READ_EXT, "READ SECTOR(S) EXT" },
  2027. { ATA_CMD_PIO_WRITE, "WRITE SECTOR(S)" },
  2028. { ATA_CMD_PIO_WRITE_EXT, "WRITE SECTOR(S) EXT" },
  2029. { ATA_CMD_READ_MULTI, "READ MULTIPLE" },
  2030. { ATA_CMD_READ_MULTI_EXT, "READ MULTIPLE EXT" },
  2031. { ATA_CMD_WRITE_MULTI, "WRITE MULTIPLE" },
  2032. { ATA_CMD_WRITE_MULTI_EXT, "WRITE MULTIPLE EXT" },
  2033. { ATA_CMD_WRITE_MULTI_FUA_EXT, "WRITE MULTIPLE FUA EXT" },
  2034. { ATA_CMD_SET_FEATURES, "SET FEATURES" },
  2035. { ATA_CMD_SET_MULTI, "SET MULTIPLE MODE" },
  2036. { ATA_CMD_VERIFY, "READ VERIFY SECTOR(S)" },
  2037. { ATA_CMD_VERIFY_EXT, "READ VERIFY SECTOR(S) EXT" },
  2038. { ATA_CMD_WRITE_UNCORR_EXT, "WRITE UNCORRECTABLE EXT" },
  2039. { ATA_CMD_STANDBYNOW1, "STANDBY IMMEDIATE" },
  2040. { ATA_CMD_IDLEIMMEDIATE, "IDLE IMMEDIATE" },
  2041. { ATA_CMD_SLEEP, "SLEEP" },
  2042. { ATA_CMD_INIT_DEV_PARAMS, "INITIALIZE DEVICE PARAMETERS" },
  2043. { ATA_CMD_READ_NATIVE_MAX, "READ NATIVE MAX ADDRESS" },
  2044. { ATA_CMD_READ_NATIVE_MAX_EXT, "READ NATIVE MAX ADDRESS EXT" },
  2045. { ATA_CMD_SET_MAX, "SET MAX ADDRESS" },
  2046. { ATA_CMD_SET_MAX_EXT, "SET MAX ADDRESS EXT" },
  2047. { ATA_CMD_READ_LOG_EXT, "READ LOG EXT" },
  2048. { ATA_CMD_WRITE_LOG_EXT, "WRITE LOG EXT" },
  2049. { ATA_CMD_READ_LOG_DMA_EXT, "READ LOG DMA EXT" },
  2050. { ATA_CMD_WRITE_LOG_DMA_EXT, "WRITE LOG DMA EXT" },
  2051. { ATA_CMD_TRUSTED_NONDATA, "TRUSTED NON-DATA" },
  2052. { ATA_CMD_TRUSTED_RCV, "TRUSTED RECEIVE" },
  2053. { ATA_CMD_TRUSTED_RCV_DMA, "TRUSTED RECEIVE DMA" },
  2054. { ATA_CMD_TRUSTED_SND, "TRUSTED SEND" },
  2055. { ATA_CMD_TRUSTED_SND_DMA, "TRUSTED SEND DMA" },
  2056. { ATA_CMD_PMP_READ, "READ BUFFER" },
  2057. { ATA_CMD_PMP_READ_DMA, "READ BUFFER DMA" },
  2058. { ATA_CMD_PMP_WRITE, "WRITE BUFFER" },
  2059. { ATA_CMD_PMP_WRITE_DMA, "WRITE BUFFER DMA" },
  2060. { ATA_CMD_CONF_OVERLAY, "DEVICE CONFIGURATION OVERLAY" },
  2061. { ATA_CMD_SEC_SET_PASS, "SECURITY SET PASSWORD" },
  2062. { ATA_CMD_SEC_UNLOCK, "SECURITY UNLOCK" },
  2063. { ATA_CMD_SEC_ERASE_PREP, "SECURITY ERASE PREPARE" },
  2064. { ATA_CMD_SEC_ERASE_UNIT, "SECURITY ERASE UNIT" },
  2065. { ATA_CMD_SEC_FREEZE_LOCK, "SECURITY FREEZE LOCK" },
  2066. { ATA_CMD_SEC_DISABLE_PASS, "SECURITY DISABLE PASSWORD" },
  2067. { ATA_CMD_CONFIG_STREAM, "CONFIGURE STREAM" },
  2068. { ATA_CMD_SMART, "SMART" },
  2069. { ATA_CMD_MEDIA_LOCK, "DOOR LOCK" },
  2070. { ATA_CMD_MEDIA_UNLOCK, "DOOR UNLOCK" },
  2071. { ATA_CMD_DSM, "DATA SET MANAGEMENT" },
  2072. { ATA_CMD_CHK_MED_CRD_TYP, "CHECK MEDIA CARD TYPE" },
  2073. { ATA_CMD_CFA_REQ_EXT_ERR, "CFA REQUEST EXTENDED ERROR" },
  2074. { ATA_CMD_CFA_WRITE_NE, "CFA WRITE SECTORS WITHOUT ERASE" },
  2075. { ATA_CMD_CFA_TRANS_SECT, "CFA TRANSLATE SECTOR" },
  2076. { ATA_CMD_CFA_ERASE, "CFA ERASE SECTORS" },
  2077. { ATA_CMD_CFA_WRITE_MULT_NE, "CFA WRITE MULTIPLE WITHOUT ERASE" },
  2078. { ATA_CMD_REQ_SENSE_DATA, "REQUEST SENSE DATA EXT" },
  2079. { ATA_CMD_SANITIZE_DEVICE, "SANITIZE DEVICE" },
  2080. { ATA_CMD_READ_LONG, "READ LONG (with retries)" },
  2081. { ATA_CMD_READ_LONG_ONCE, "READ LONG (without retries)" },
  2082. { ATA_CMD_WRITE_LONG, "WRITE LONG (with retries)" },
  2083. { ATA_CMD_WRITE_LONG_ONCE, "WRITE LONG (without retries)" },
  2084. { ATA_CMD_RESTORE, "RECALIBRATE" },
  2085. { 0, NULL } /* terminate list */
  2086. };
  2087. unsigned int i;
  2088. for (i = 0; cmd_descr[i].text; i++)
  2089. if (cmd_descr[i].command == command)
  2090. return cmd_descr[i].text;
  2091. #endif
  2092. return NULL;
  2093. }
  2094. EXPORT_SYMBOL_GPL(ata_get_cmd_descript);
  2095. /**
  2096. * ata_eh_link_report - report error handling to user
  2097. * @link: ATA link EH is going on
  2098. *
  2099. * Report EH to user.
  2100. *
  2101. * LOCKING:
  2102. * None.
  2103. */
  2104. static void ata_eh_link_report(struct ata_link *link)
  2105. {
  2106. struct ata_port *ap = link->ap;
  2107. struct ata_eh_context *ehc = &link->eh_context;
  2108. const char *frozen, *desc;
  2109. char tries_buf[6] = "";
  2110. int tag, nr_failed = 0;
  2111. if (ehc->i.flags & ATA_EHI_QUIET)
  2112. return;
  2113. desc = NULL;
  2114. if (ehc->i.desc[0] != '\0')
  2115. desc = ehc->i.desc;
  2116. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  2117. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  2118. if (!(qc->flags & ATA_QCFLAG_FAILED) ||
  2119. ata_dev_phys_link(qc->dev) != link ||
  2120. ((qc->flags & ATA_QCFLAG_QUIET) &&
  2121. qc->err_mask == AC_ERR_DEV))
  2122. continue;
  2123. if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
  2124. continue;
  2125. nr_failed++;
  2126. }
  2127. if (!nr_failed && !ehc->i.err_mask)
  2128. return;
  2129. frozen = "";
  2130. if (ap->pflags & ATA_PFLAG_FROZEN)
  2131. frozen = " frozen";
  2132. if (ap->eh_tries < ATA_EH_MAX_TRIES)
  2133. snprintf(tries_buf, sizeof(tries_buf), " t%d",
  2134. ap->eh_tries);
  2135. if (ehc->i.dev) {
  2136. ata_dev_err(ehc->i.dev, "exception Emask 0x%x "
  2137. "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
  2138. ehc->i.err_mask, link->sactive, ehc->i.serror,
  2139. ehc->i.action, frozen, tries_buf);
  2140. if (desc)
  2141. ata_dev_err(ehc->i.dev, "%s\n", desc);
  2142. } else {
  2143. ata_link_err(link, "exception Emask 0x%x "
  2144. "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
  2145. ehc->i.err_mask, link->sactive, ehc->i.serror,
  2146. ehc->i.action, frozen, tries_buf);
  2147. if (desc)
  2148. ata_link_err(link, "%s\n", desc);
  2149. }
  2150. #ifdef CONFIG_ATA_VERBOSE_ERROR
  2151. if (ehc->i.serror)
  2152. ata_link_err(link,
  2153. "SError: { %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s}\n",
  2154. ehc->i.serror & SERR_DATA_RECOVERED ? "RecovData " : "",
  2155. ehc->i.serror & SERR_COMM_RECOVERED ? "RecovComm " : "",
  2156. ehc->i.serror & SERR_DATA ? "UnrecovData " : "",
  2157. ehc->i.serror & SERR_PERSISTENT ? "Persist " : "",
  2158. ehc->i.serror & SERR_PROTOCOL ? "Proto " : "",
  2159. ehc->i.serror & SERR_INTERNAL ? "HostInt " : "",
  2160. ehc->i.serror & SERR_PHYRDY_CHG ? "PHYRdyChg " : "",
  2161. ehc->i.serror & SERR_PHY_INT_ERR ? "PHYInt " : "",
  2162. ehc->i.serror & SERR_COMM_WAKE ? "CommWake " : "",
  2163. ehc->i.serror & SERR_10B_8B_ERR ? "10B8B " : "",
  2164. ehc->i.serror & SERR_DISPARITY ? "Dispar " : "",
  2165. ehc->i.serror & SERR_CRC ? "BadCRC " : "",
  2166. ehc->i.serror & SERR_HANDSHAKE ? "Handshk " : "",
  2167. ehc->i.serror & SERR_LINK_SEQ_ERR ? "LinkSeq " : "",
  2168. ehc->i.serror & SERR_TRANS_ST_ERROR ? "TrStaTrns " : "",
  2169. ehc->i.serror & SERR_UNRECOG_FIS ? "UnrecFIS " : "",
  2170. ehc->i.serror & SERR_DEV_XCHG ? "DevExch " : "");
  2171. #endif
  2172. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  2173. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  2174. struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
  2175. char data_buf[20] = "";
  2176. char cdb_buf[70] = "";
  2177. if (!(qc->flags & ATA_QCFLAG_FAILED) ||
  2178. ata_dev_phys_link(qc->dev) != link || !qc->err_mask)
  2179. continue;
  2180. if (qc->dma_dir != DMA_NONE) {
  2181. static const char *dma_str[] = {
  2182. [DMA_BIDIRECTIONAL] = "bidi",
  2183. [DMA_TO_DEVICE] = "out",
  2184. [DMA_FROM_DEVICE] = "in",
  2185. };
  2186. static const char *prot_str[] = {
  2187. [ATA_PROT_PIO] = "pio",
  2188. [ATA_PROT_DMA] = "dma",
  2189. [ATA_PROT_NCQ] = "ncq",
  2190. [ATAPI_PROT_PIO] = "pio",
  2191. [ATAPI_PROT_DMA] = "dma",
  2192. };
  2193. snprintf(data_buf, sizeof(data_buf), " %s %u %s",
  2194. prot_str[qc->tf.protocol], qc->nbytes,
  2195. dma_str[qc->dma_dir]);
  2196. }
  2197. if (ata_is_atapi(qc->tf.protocol)) {
  2198. const u8 *cdb = qc->cdb;
  2199. size_t cdb_len = qc->dev->cdb_len;
  2200. if (qc->scsicmd) {
  2201. cdb = qc->scsicmd->cmnd;
  2202. cdb_len = qc->scsicmd->cmd_len;
  2203. }
  2204. __scsi_format_command(cdb_buf, sizeof(cdb_buf),
  2205. cdb, cdb_len);
  2206. } else {
  2207. const char *descr = ata_get_cmd_descript(cmd->command);
  2208. if (descr)
  2209. ata_dev_err(qc->dev, "failed command: %s\n",
  2210. descr);
  2211. }
  2212. ata_dev_err(qc->dev,
  2213. "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
  2214. "tag %d%s\n %s"
  2215. "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
  2216. "Emask 0x%x (%s)%s\n",
  2217. cmd->command, cmd->feature, cmd->nsect,
  2218. cmd->lbal, cmd->lbam, cmd->lbah,
  2219. cmd->hob_feature, cmd->hob_nsect,
  2220. cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
  2221. cmd->device, qc->tag, data_buf, cdb_buf,
  2222. res->command, res->feature, res->nsect,
  2223. res->lbal, res->lbam, res->lbah,
  2224. res->hob_feature, res->hob_nsect,
  2225. res->hob_lbal, res->hob_lbam, res->hob_lbah,
  2226. res->device, qc->err_mask, ata_err_string(qc->err_mask),
  2227. qc->err_mask & AC_ERR_NCQ ? " <F>" : "");
  2228. #ifdef CONFIG_ATA_VERBOSE_ERROR
  2229. if (res->command & (ATA_BUSY | ATA_DRDY | ATA_DF | ATA_DRQ |
  2230. ATA_ERR)) {
  2231. if (res->command & ATA_BUSY)
  2232. ata_dev_err(qc->dev, "status: { Busy }\n");
  2233. else
  2234. ata_dev_err(qc->dev, "status: { %s%s%s%s}\n",
  2235. res->command & ATA_DRDY ? "DRDY " : "",
  2236. res->command & ATA_DF ? "DF " : "",
  2237. res->command & ATA_DRQ ? "DRQ " : "",
  2238. res->command & ATA_ERR ? "ERR " : "");
  2239. }
  2240. if (cmd->command != ATA_CMD_PACKET &&
  2241. (res->feature & (ATA_ICRC | ATA_UNC | ATA_AMNF |
  2242. ATA_IDNF | ATA_ABORTED)))
  2243. ata_dev_err(qc->dev, "error: { %s%s%s%s%s}\n",
  2244. res->feature & ATA_ICRC ? "ICRC " : "",
  2245. res->feature & ATA_UNC ? "UNC " : "",
  2246. res->feature & ATA_AMNF ? "AMNF " : "",
  2247. res->feature & ATA_IDNF ? "IDNF " : "",
  2248. res->feature & ATA_ABORTED ? "ABRT " : "");
  2249. #endif
  2250. }
  2251. }
  2252. /**
  2253. * ata_eh_report - report error handling to user
  2254. * @ap: ATA port to report EH about
  2255. *
  2256. * Report EH to user.
  2257. *
  2258. * LOCKING:
  2259. * None.
  2260. */
  2261. void ata_eh_report(struct ata_port *ap)
  2262. {
  2263. struct ata_link *link;
  2264. ata_for_each_link(link, ap, HOST_FIRST)
  2265. ata_eh_link_report(link);
  2266. }
  2267. static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset,
  2268. unsigned int *classes, unsigned long deadline,
  2269. bool clear_classes)
  2270. {
  2271. struct ata_device *dev;
  2272. if (clear_classes)
  2273. ata_for_each_dev(dev, link, ALL)
  2274. classes[dev->devno] = ATA_DEV_UNKNOWN;
  2275. return reset(link, classes, deadline);
  2276. }
  2277. static int ata_eh_followup_srst_needed(struct ata_link *link, int rc)
  2278. {
  2279. if ((link->flags & ATA_LFLAG_NO_SRST) || ata_link_offline(link))
  2280. return 0;
  2281. if (rc == -EAGAIN)
  2282. return 1;
  2283. if (sata_pmp_supported(link->ap) && ata_is_host_link(link))
  2284. return 1;
  2285. return 0;
  2286. }
  2287. int ata_eh_reset(struct ata_link *link, int classify,
  2288. ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
  2289. ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
  2290. {
  2291. struct ata_port *ap = link->ap;
  2292. struct ata_link *slave = ap->slave_link;
  2293. struct ata_eh_context *ehc = &link->eh_context;
  2294. struct ata_eh_context *sehc = slave ? &slave->eh_context : NULL;
  2295. unsigned int *classes = ehc->classes;
  2296. unsigned int lflags = link->flags;
  2297. int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
  2298. int max_tries = 0, try = 0;
  2299. struct ata_link *failed_link;
  2300. struct ata_device *dev;
  2301. unsigned long deadline, now;
  2302. ata_reset_fn_t reset;
  2303. unsigned long flags;
  2304. u32 sstatus;
  2305. int nr_unknown, rc;
  2306. /*
  2307. * Prepare to reset
  2308. */
  2309. while (ata_eh_reset_timeouts[max_tries] != ULONG_MAX)
  2310. max_tries++;
  2311. if (link->flags & ATA_LFLAG_RST_ONCE)
  2312. max_tries = 1;
  2313. if (link->flags & ATA_LFLAG_NO_HRST)
  2314. hardreset = NULL;
  2315. if (link->flags & ATA_LFLAG_NO_SRST)
  2316. softreset = NULL;
  2317. /* make sure each reset attempt is at least COOL_DOWN apart */
  2318. if (ehc->i.flags & ATA_EHI_DID_RESET) {
  2319. now = jiffies;
  2320. WARN_ON(time_after(ehc->last_reset, now));
  2321. deadline = ata_deadline(ehc->last_reset,
  2322. ATA_EH_RESET_COOL_DOWN);
  2323. if (time_before(now, deadline))
  2324. schedule_timeout_uninterruptible(deadline - now);
  2325. }
  2326. spin_lock_irqsave(ap->lock, flags);
  2327. ap->pflags |= ATA_PFLAG_RESETTING;
  2328. spin_unlock_irqrestore(ap->lock, flags);
  2329. ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
  2330. ata_for_each_dev(dev, link, ALL) {
  2331. /* If we issue an SRST then an ATA drive (not ATAPI)
  2332. * may change configuration and be in PIO0 timing. If
  2333. * we do a hard reset (or are coming from power on)
  2334. * this is true for ATA or ATAPI. Until we've set a
  2335. * suitable controller mode we should not touch the
  2336. * bus as we may be talking too fast.
  2337. */
  2338. dev->pio_mode = XFER_PIO_0;
  2339. dev->dma_mode = 0xff;
  2340. /* If the controller has a pio mode setup function
  2341. * then use it to set the chipset to rights. Don't
  2342. * touch the DMA setup as that will be dealt with when
  2343. * configuring devices.
  2344. */
  2345. if (ap->ops->set_piomode)
  2346. ap->ops->set_piomode(ap, dev);
  2347. }
  2348. /* prefer hardreset */
  2349. reset = NULL;
  2350. ehc->i.action &= ~ATA_EH_RESET;
  2351. if (hardreset) {
  2352. reset = hardreset;
  2353. ehc->i.action |= ATA_EH_HARDRESET;
  2354. } else if (softreset) {
  2355. reset = softreset;
  2356. ehc->i.action |= ATA_EH_SOFTRESET;
  2357. }
  2358. if (prereset) {
  2359. unsigned long deadline = ata_deadline(jiffies,
  2360. ATA_EH_PRERESET_TIMEOUT);
  2361. if (slave) {
  2362. sehc->i.action &= ~ATA_EH_RESET;
  2363. sehc->i.action |= ehc->i.action;
  2364. }
  2365. rc = prereset(link, deadline);
  2366. /* If present, do prereset on slave link too. Reset
  2367. * is skipped iff both master and slave links report
  2368. * -ENOENT or clear ATA_EH_RESET.
  2369. */
  2370. if (slave && (rc == 0 || rc == -ENOENT)) {
  2371. int tmp;
  2372. tmp = prereset(slave, deadline);
  2373. if (tmp != -ENOENT)
  2374. rc = tmp;
  2375. ehc->i.action |= sehc->i.action;
  2376. }
  2377. if (rc) {
  2378. if (rc == -ENOENT) {
  2379. ata_link_dbg(link, "port disabled--ignoring\n");
  2380. ehc->i.action &= ~ATA_EH_RESET;
  2381. ata_for_each_dev(dev, link, ALL)
  2382. classes[dev->devno] = ATA_DEV_NONE;
  2383. rc = 0;
  2384. } else
  2385. ata_link_err(link,
  2386. "prereset failed (errno=%d)\n",
  2387. rc);
  2388. goto out;
  2389. }
  2390. /* prereset() might have cleared ATA_EH_RESET. If so,
  2391. * bang classes, thaw and return.
  2392. */
  2393. if (reset && !(ehc->i.action & ATA_EH_RESET)) {
  2394. ata_for_each_dev(dev, link, ALL)
  2395. classes[dev->devno] = ATA_DEV_NONE;
  2396. if ((ap->pflags & ATA_PFLAG_FROZEN) &&
  2397. ata_is_host_link(link))
  2398. ata_eh_thaw_port(ap);
  2399. rc = 0;
  2400. goto out;
  2401. }
  2402. }
  2403. retry:
  2404. /*
  2405. * Perform reset
  2406. */
  2407. if (ata_is_host_link(link))
  2408. ata_eh_freeze_port(ap);
  2409. deadline = ata_deadline(jiffies, ata_eh_reset_timeouts[try++]);
  2410. if (reset) {
  2411. if (verbose)
  2412. ata_link_info(link, "%s resetting link\n",
  2413. reset == softreset ? "soft" : "hard");
  2414. /* mark that this EH session started with reset */
  2415. ehc->last_reset = jiffies;
  2416. if (reset == hardreset)
  2417. ehc->i.flags |= ATA_EHI_DID_HARDRESET;
  2418. else
  2419. ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
  2420. rc = ata_do_reset(link, reset, classes, deadline, true);
  2421. if (rc && rc != -EAGAIN) {
  2422. failed_link = link;
  2423. goto fail;
  2424. }
  2425. /* hardreset slave link if existent */
  2426. if (slave && reset == hardreset) {
  2427. int tmp;
  2428. if (verbose)
  2429. ata_link_info(slave, "hard resetting link\n");
  2430. ata_eh_about_to_do(slave, NULL, ATA_EH_RESET);
  2431. tmp = ata_do_reset(slave, reset, classes, deadline,
  2432. false);
  2433. switch (tmp) {
  2434. case -EAGAIN:
  2435. rc = -EAGAIN;
  2436. case 0:
  2437. break;
  2438. default:
  2439. failed_link = slave;
  2440. rc = tmp;
  2441. goto fail;
  2442. }
  2443. }
  2444. /* perform follow-up SRST if necessary */
  2445. if (reset == hardreset &&
  2446. ata_eh_followup_srst_needed(link, rc)) {
  2447. reset = softreset;
  2448. if (!reset) {
  2449. ata_link_err(link,
  2450. "follow-up softreset required but no softreset available\n");
  2451. failed_link = link;
  2452. rc = -EINVAL;
  2453. goto fail;
  2454. }
  2455. ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
  2456. rc = ata_do_reset(link, reset, classes, deadline, true);
  2457. if (rc) {
  2458. failed_link = link;
  2459. goto fail;
  2460. }
  2461. }
  2462. } else {
  2463. if (verbose)
  2464. ata_link_info(link,
  2465. "no reset method available, skipping reset\n");
  2466. if (!(lflags & ATA_LFLAG_ASSUME_CLASS))
  2467. lflags |= ATA_LFLAG_ASSUME_ATA;
  2468. }
  2469. /*
  2470. * Post-reset processing
  2471. */
  2472. ata_for_each_dev(dev, link, ALL) {
  2473. /* After the reset, the device state is PIO 0 and the
  2474. * controller state is undefined. Reset also wakes up
  2475. * drives from sleeping mode.
  2476. */
  2477. dev->pio_mode = XFER_PIO_0;
  2478. dev->flags &= ~ATA_DFLAG_SLEEPING;
  2479. if (ata_phys_link_offline(ata_dev_phys_link(dev)))
  2480. continue;
  2481. /* apply class override */
  2482. if (lflags & ATA_LFLAG_ASSUME_ATA)
  2483. classes[dev->devno] = ATA_DEV_ATA;
  2484. else if (lflags & ATA_LFLAG_ASSUME_SEMB)
  2485. classes[dev->devno] = ATA_DEV_SEMB_UNSUP;
  2486. }
  2487. /* record current link speed */
  2488. if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0)
  2489. link->sata_spd = (sstatus >> 4) & 0xf;
  2490. if (slave && sata_scr_read(slave, SCR_STATUS, &sstatus) == 0)
  2491. slave->sata_spd = (sstatus >> 4) & 0xf;
  2492. /* thaw the port */
  2493. if (ata_is_host_link(link))
  2494. ata_eh_thaw_port(ap);
  2495. /* postreset() should clear hardware SError. Although SError
  2496. * is cleared during link resume, clearing SError here is
  2497. * necessary as some PHYs raise hotplug events after SRST.
  2498. * This introduces race condition where hotplug occurs between
  2499. * reset and here. This race is mediated by cross checking
  2500. * link onlineness and classification result later.
  2501. */
  2502. if (postreset) {
  2503. postreset(link, classes);
  2504. if (slave)
  2505. postreset(slave, classes);
  2506. }
  2507. /*
  2508. * Some controllers can't be frozen very well and may set spurious
  2509. * error conditions during reset. Clear accumulated error
  2510. * information and re-thaw the port if frozen. As reset is the
  2511. * final recovery action and we cross check link onlineness against
  2512. * device classification later, no hotplug event is lost by this.
  2513. */
  2514. spin_lock_irqsave(link->ap->lock, flags);
  2515. memset(&link->eh_info, 0, sizeof(link->eh_info));
  2516. if (slave)
  2517. memset(&slave->eh_info, 0, sizeof(link->eh_info));
  2518. ap->pflags &= ~ATA_PFLAG_EH_PENDING;
  2519. spin_unlock_irqrestore(link->ap->lock, flags);
  2520. if (ap->pflags & ATA_PFLAG_FROZEN)
  2521. ata_eh_thaw_port(ap);
  2522. /*
  2523. * Make sure onlineness and classification result correspond.
  2524. * Hotplug could have happened during reset and some
  2525. * controllers fail to wait while a drive is spinning up after
  2526. * being hotplugged causing misdetection. By cross checking
  2527. * link on/offlineness and classification result, those
  2528. * conditions can be reliably detected and retried.
  2529. */
  2530. nr_unknown = 0;
  2531. ata_for_each_dev(dev, link, ALL) {
  2532. if (ata_phys_link_online(ata_dev_phys_link(dev))) {
  2533. if (classes[dev->devno] == ATA_DEV_UNKNOWN) {
  2534. ata_dev_dbg(dev, "link online but device misclassified\n");
  2535. classes[dev->devno] = ATA_DEV_NONE;
  2536. nr_unknown++;
  2537. }
  2538. } else if (ata_phys_link_offline(ata_dev_phys_link(dev))) {
  2539. if (ata_class_enabled(classes[dev->devno]))
  2540. ata_dev_dbg(dev,
  2541. "link offline, clearing class %d to NONE\n",
  2542. classes[dev->devno]);
  2543. classes[dev->devno] = ATA_DEV_NONE;
  2544. } else if (classes[dev->devno] == ATA_DEV_UNKNOWN) {
  2545. ata_dev_dbg(dev,
  2546. "link status unknown, clearing UNKNOWN to NONE\n");
  2547. classes[dev->devno] = ATA_DEV_NONE;
  2548. }
  2549. }
  2550. if (classify && nr_unknown) {
  2551. if (try < max_tries) {
  2552. ata_link_warn(link,
  2553. "link online but %d devices misclassified, retrying\n",
  2554. nr_unknown);
  2555. failed_link = link;
  2556. rc = -EAGAIN;
  2557. goto fail;
  2558. }
  2559. ata_link_warn(link,
  2560. "link online but %d devices misclassified, "
  2561. "device detection might fail\n", nr_unknown);
  2562. }
  2563. /* reset successful, schedule revalidation */
  2564. ata_eh_done(link, NULL, ATA_EH_RESET);
  2565. if (slave)
  2566. ata_eh_done(slave, NULL, ATA_EH_RESET);
  2567. ehc->last_reset = jiffies; /* update to completion time */
  2568. ehc->i.action |= ATA_EH_REVALIDATE;
  2569. link->lpm_policy = ATA_LPM_UNKNOWN; /* reset LPM state */
  2570. rc = 0;
  2571. out:
  2572. /* clear hotplug flag */
  2573. ehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
  2574. if (slave)
  2575. sehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
  2576. spin_lock_irqsave(ap->lock, flags);
  2577. ap->pflags &= ~ATA_PFLAG_RESETTING;
  2578. spin_unlock_irqrestore(ap->lock, flags);
  2579. return rc;
  2580. fail:
  2581. /* if SCR isn't accessible on a fan-out port, PMP needs to be reset */
  2582. if (!ata_is_host_link(link) &&
  2583. sata_scr_read(link, SCR_STATUS, &sstatus))
  2584. rc = -ERESTART;
  2585. if (try >= max_tries) {
  2586. /*
  2587. * Thaw host port even if reset failed, so that the port
  2588. * can be retried on the next phy event. This risks
  2589. * repeated EH runs but seems to be a better tradeoff than
  2590. * shutting down a port after a botched hotplug attempt.
  2591. */
  2592. if (ata_is_host_link(link))
  2593. ata_eh_thaw_port(ap);
  2594. goto out;
  2595. }
  2596. now = jiffies;
  2597. if (time_before(now, deadline)) {
  2598. unsigned long delta = deadline - now;
  2599. ata_link_warn(failed_link,
  2600. "reset failed (errno=%d), retrying in %u secs\n",
  2601. rc, DIV_ROUND_UP(jiffies_to_msecs(delta), 1000));
  2602. ata_eh_release(ap);
  2603. while (delta)
  2604. delta = schedule_timeout_uninterruptible(delta);
  2605. ata_eh_acquire(ap);
  2606. }
  2607. /*
  2608. * While disks spinup behind PMP, some controllers fail sending SRST.
  2609. * They need to be reset - as well as the PMP - before retrying.
  2610. */
  2611. if (rc == -ERESTART) {
  2612. if (ata_is_host_link(link))
  2613. ata_eh_thaw_port(ap);
  2614. goto out;
  2615. }
  2616. if (try == max_tries - 1) {
  2617. sata_down_spd_limit(link, 0);
  2618. if (slave)
  2619. sata_down_spd_limit(slave, 0);
  2620. } else if (rc == -EPIPE)
  2621. sata_down_spd_limit(failed_link, 0);
  2622. if (hardreset)
  2623. reset = hardreset;
  2624. goto retry;
  2625. }
  2626. static inline void ata_eh_pull_park_action(struct ata_port *ap)
  2627. {
  2628. struct ata_link *link;
  2629. struct ata_device *dev;
  2630. unsigned long flags;
  2631. /*
  2632. * This function can be thought of as an extended version of
  2633. * ata_eh_about_to_do() specially crafted to accommodate the
  2634. * requirements of ATA_EH_PARK handling. Since the EH thread
  2635. * does not leave the do {} while () loop in ata_eh_recover as
  2636. * long as the timeout for a park request to *one* device on
  2637. * the port has not expired, and since we still want to pick
  2638. * up park requests to other devices on the same port or
  2639. * timeout updates for the same device, we have to pull
  2640. * ATA_EH_PARK actions from eh_info into eh_context.i
  2641. * ourselves at the beginning of each pass over the loop.
  2642. *
  2643. * Additionally, all write accesses to &ap->park_req_pending
  2644. * through reinit_completion() (see below) or complete_all()
  2645. * (see ata_scsi_park_store()) are protected by the host lock.
  2646. * As a result we have that park_req_pending.done is zero on
  2647. * exit from this function, i.e. when ATA_EH_PARK actions for
  2648. * *all* devices on port ap have been pulled into the
  2649. * respective eh_context structs. If, and only if,
  2650. * park_req_pending.done is non-zero by the time we reach
  2651. * wait_for_completion_timeout(), another ATA_EH_PARK action
  2652. * has been scheduled for at least one of the devices on port
  2653. * ap and we have to cycle over the do {} while () loop in
  2654. * ata_eh_recover() again.
  2655. */
  2656. spin_lock_irqsave(ap->lock, flags);
  2657. reinit_completion(&ap->park_req_pending);
  2658. ata_for_each_link(link, ap, EDGE) {
  2659. ata_for_each_dev(dev, link, ALL) {
  2660. struct ata_eh_info *ehi = &link->eh_info;
  2661. link->eh_context.i.dev_action[dev->devno] |=
  2662. ehi->dev_action[dev->devno] & ATA_EH_PARK;
  2663. ata_eh_clear_action(link, dev, ehi, ATA_EH_PARK);
  2664. }
  2665. }
  2666. spin_unlock_irqrestore(ap->lock, flags);
  2667. }
  2668. static void ata_eh_park_issue_cmd(struct ata_device *dev, int park)
  2669. {
  2670. struct ata_eh_context *ehc = &dev->link->eh_context;
  2671. struct ata_taskfile tf;
  2672. unsigned int err_mask;
  2673. ata_tf_init(dev, &tf);
  2674. if (park) {
  2675. ehc->unloaded_mask |= 1 << dev->devno;
  2676. tf.command = ATA_CMD_IDLEIMMEDIATE;
  2677. tf.feature = 0x44;
  2678. tf.lbal = 0x4c;
  2679. tf.lbam = 0x4e;
  2680. tf.lbah = 0x55;
  2681. } else {
  2682. ehc->unloaded_mask &= ~(1 << dev->devno);
  2683. tf.command = ATA_CMD_CHK_POWER;
  2684. }
  2685. tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
  2686. tf.protocol |= ATA_PROT_NODATA;
  2687. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
  2688. if (park && (err_mask || tf.lbal != 0xc4)) {
  2689. ata_dev_err(dev, "head unload failed!\n");
  2690. ehc->unloaded_mask &= ~(1 << dev->devno);
  2691. }
  2692. }
  2693. static int ata_eh_revalidate_and_attach(struct ata_link *link,
  2694. struct ata_device **r_failed_dev)
  2695. {
  2696. struct ata_port *ap = link->ap;
  2697. struct ata_eh_context *ehc = &link->eh_context;
  2698. struct ata_device *dev;
  2699. unsigned int new_mask = 0;
  2700. unsigned long flags;
  2701. int rc = 0;
  2702. DPRINTK("ENTER\n");
  2703. /* For PATA drive side cable detection to work, IDENTIFY must
  2704. * be done backwards such that PDIAG- is released by the slave
  2705. * device before the master device is identified.
  2706. */
  2707. ata_for_each_dev(dev, link, ALL_REVERSE) {
  2708. unsigned int action = ata_eh_dev_action(dev);
  2709. unsigned int readid_flags = 0;
  2710. if (ehc->i.flags & ATA_EHI_DID_RESET)
  2711. readid_flags |= ATA_READID_POSTRESET;
  2712. if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
  2713. WARN_ON(dev->class == ATA_DEV_PMP);
  2714. if (ata_phys_link_offline(ata_dev_phys_link(dev))) {
  2715. rc = -EIO;
  2716. goto err;
  2717. }
  2718. ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE);
  2719. rc = ata_dev_revalidate(dev, ehc->classes[dev->devno],
  2720. readid_flags);
  2721. if (rc)
  2722. goto err;
  2723. ata_eh_done(link, dev, ATA_EH_REVALIDATE);
  2724. /* Configuration may have changed, reconfigure
  2725. * transfer mode.
  2726. */
  2727. ehc->i.flags |= ATA_EHI_SETMODE;
  2728. /* schedule the scsi_rescan_device() here */
  2729. schedule_work(&(ap->scsi_rescan_task));
  2730. } else if (dev->class == ATA_DEV_UNKNOWN &&
  2731. ehc->tries[dev->devno] &&
  2732. ata_class_enabled(ehc->classes[dev->devno])) {
  2733. /* Temporarily set dev->class, it will be
  2734. * permanently set once all configurations are
  2735. * complete. This is necessary because new
  2736. * device configuration is done in two
  2737. * separate loops.
  2738. */
  2739. dev->class = ehc->classes[dev->devno];
  2740. if (dev->class == ATA_DEV_PMP)
  2741. rc = sata_pmp_attach(dev);
  2742. else
  2743. rc = ata_dev_read_id(dev, &dev->class,
  2744. readid_flags, dev->id);
  2745. /* read_id might have changed class, store and reset */
  2746. ehc->classes[dev->devno] = dev->class;
  2747. dev->class = ATA_DEV_UNKNOWN;
  2748. switch (rc) {
  2749. case 0:
  2750. /* clear error info accumulated during probe */
  2751. ata_ering_clear(&dev->ering);
  2752. new_mask |= 1 << dev->devno;
  2753. break;
  2754. case -ENOENT:
  2755. /* IDENTIFY was issued to non-existent
  2756. * device. No need to reset. Just
  2757. * thaw and ignore the device.
  2758. */
  2759. ata_eh_thaw_port(ap);
  2760. break;
  2761. default:
  2762. goto err;
  2763. }
  2764. }
  2765. }
  2766. /* PDIAG- should have been released, ask cable type if post-reset */
  2767. if ((ehc->i.flags & ATA_EHI_DID_RESET) && ata_is_host_link(link)) {
  2768. if (ap->ops->cable_detect)
  2769. ap->cbl = ap->ops->cable_detect(ap);
  2770. ata_force_cbl(ap);
  2771. }
  2772. /* Configure new devices forward such that user doesn't see
  2773. * device detection messages backwards.
  2774. */
  2775. ata_for_each_dev(dev, link, ALL) {
  2776. if (!(new_mask & (1 << dev->devno)))
  2777. continue;
  2778. dev->class = ehc->classes[dev->devno];
  2779. if (dev->class == ATA_DEV_PMP)
  2780. continue;
  2781. ehc->i.flags |= ATA_EHI_PRINTINFO;
  2782. rc = ata_dev_configure(dev);
  2783. ehc->i.flags &= ~ATA_EHI_PRINTINFO;
  2784. if (rc) {
  2785. dev->class = ATA_DEV_UNKNOWN;
  2786. goto err;
  2787. }
  2788. spin_lock_irqsave(ap->lock, flags);
  2789. ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
  2790. spin_unlock_irqrestore(ap->lock, flags);
  2791. /* new device discovered, configure xfermode */
  2792. ehc->i.flags |= ATA_EHI_SETMODE;
  2793. }
  2794. return 0;
  2795. err:
  2796. *r_failed_dev = dev;
  2797. DPRINTK("EXIT rc=%d\n", rc);
  2798. return rc;
  2799. }
  2800. /**
  2801. * ata_set_mode - Program timings and issue SET FEATURES - XFER
  2802. * @link: link on which timings will be programmed
  2803. * @r_failed_dev: out parameter for failed device
  2804. *
  2805. * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
  2806. * ata_set_mode() fails, pointer to the failing device is
  2807. * returned in @r_failed_dev.
  2808. *
  2809. * LOCKING:
  2810. * PCI/etc. bus probe sem.
  2811. *
  2812. * RETURNS:
  2813. * 0 on success, negative errno otherwise
  2814. */
  2815. int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
  2816. {
  2817. struct ata_port *ap = link->ap;
  2818. struct ata_device *dev;
  2819. int rc;
  2820. /* if data transfer is verified, clear DUBIOUS_XFER on ering top */
  2821. ata_for_each_dev(dev, link, ENABLED) {
  2822. if (!(dev->flags & ATA_DFLAG_DUBIOUS_XFER)) {
  2823. struct ata_ering_entry *ent;
  2824. ent = ata_ering_top(&dev->ering);
  2825. if (ent)
  2826. ent->eflags &= ~ATA_EFLAG_DUBIOUS_XFER;
  2827. }
  2828. }
  2829. /* has private set_mode? */
  2830. if (ap->ops->set_mode)
  2831. rc = ap->ops->set_mode(link, r_failed_dev);
  2832. else
  2833. rc = ata_do_set_mode(link, r_failed_dev);
  2834. /* if transfer mode has changed, set DUBIOUS_XFER on device */
  2835. ata_for_each_dev(dev, link, ENABLED) {
  2836. struct ata_eh_context *ehc = &link->eh_context;
  2837. u8 saved_xfer_mode = ehc->saved_xfer_mode[dev->devno];
  2838. u8 saved_ncq = !!(ehc->saved_ncq_enabled & (1 << dev->devno));
  2839. if (dev->xfer_mode != saved_xfer_mode ||
  2840. ata_ncq_enabled(dev) != saved_ncq)
  2841. dev->flags |= ATA_DFLAG_DUBIOUS_XFER;
  2842. }
  2843. return rc;
  2844. }
  2845. /**
  2846. * atapi_eh_clear_ua - Clear ATAPI UNIT ATTENTION after reset
  2847. * @dev: ATAPI device to clear UA for
  2848. *
  2849. * Resets and other operations can make an ATAPI device raise
  2850. * UNIT ATTENTION which causes the next operation to fail. This
  2851. * function clears UA.
  2852. *
  2853. * LOCKING:
  2854. * EH context (may sleep).
  2855. *
  2856. * RETURNS:
  2857. * 0 on success, -errno on failure.
  2858. */
  2859. static int atapi_eh_clear_ua(struct ata_device *dev)
  2860. {
  2861. int i;
  2862. for (i = 0; i < ATA_EH_UA_TRIES; i++) {
  2863. u8 *sense_buffer = dev->link->ap->sector_buf;
  2864. u8 sense_key = 0;
  2865. unsigned int err_mask;
  2866. err_mask = atapi_eh_tur(dev, &sense_key);
  2867. if (err_mask != 0 && err_mask != AC_ERR_DEV) {
  2868. ata_dev_warn(dev,
  2869. "TEST_UNIT_READY failed (err_mask=0x%x)\n",
  2870. err_mask);
  2871. return -EIO;
  2872. }
  2873. if (!err_mask || sense_key != UNIT_ATTENTION)
  2874. return 0;
  2875. err_mask = atapi_eh_request_sense(dev, sense_buffer, sense_key);
  2876. if (err_mask) {
  2877. ata_dev_warn(dev, "failed to clear "
  2878. "UNIT ATTENTION (err_mask=0x%x)\n", err_mask);
  2879. return -EIO;
  2880. }
  2881. }
  2882. ata_dev_warn(dev, "UNIT ATTENTION persists after %d tries\n",
  2883. ATA_EH_UA_TRIES);
  2884. return 0;
  2885. }
  2886. /**
  2887. * ata_eh_maybe_retry_flush - Retry FLUSH if necessary
  2888. * @dev: ATA device which may need FLUSH retry
  2889. *
  2890. * If @dev failed FLUSH, it needs to be reported upper layer
  2891. * immediately as it means that @dev failed to remap and already
  2892. * lost at least a sector and further FLUSH retrials won't make
  2893. * any difference to the lost sector. However, if FLUSH failed
  2894. * for other reasons, for example transmission error, FLUSH needs
  2895. * to be retried.
  2896. *
  2897. * This function determines whether FLUSH failure retry is
  2898. * necessary and performs it if so.
  2899. *
  2900. * RETURNS:
  2901. * 0 if EH can continue, -errno if EH needs to be repeated.
  2902. */
  2903. static int ata_eh_maybe_retry_flush(struct ata_device *dev)
  2904. {
  2905. struct ata_link *link = dev->link;
  2906. struct ata_port *ap = link->ap;
  2907. struct ata_queued_cmd *qc;
  2908. struct ata_taskfile tf;
  2909. unsigned int err_mask;
  2910. int rc = 0;
  2911. /* did flush fail for this device? */
  2912. if (!ata_tag_valid(link->active_tag))
  2913. return 0;
  2914. qc = __ata_qc_from_tag(ap, link->active_tag);
  2915. if (qc->dev != dev || (qc->tf.command != ATA_CMD_FLUSH_EXT &&
  2916. qc->tf.command != ATA_CMD_FLUSH))
  2917. return 0;
  2918. /* if the device failed it, it should be reported to upper layers */
  2919. if (qc->err_mask & AC_ERR_DEV)
  2920. return 0;
  2921. /* flush failed for some other reason, give it another shot */
  2922. ata_tf_init(dev, &tf);
  2923. tf.command = qc->tf.command;
  2924. tf.flags |= ATA_TFLAG_DEVICE;
  2925. tf.protocol = ATA_PROT_NODATA;
  2926. ata_dev_warn(dev, "retrying FLUSH 0x%x Emask 0x%x\n",
  2927. tf.command, qc->err_mask);
  2928. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
  2929. if (!err_mask) {
  2930. /*
  2931. * FLUSH is complete but there's no way to
  2932. * successfully complete a failed command from EH.
  2933. * Making sure retry is allowed at least once and
  2934. * retrying it should do the trick - whatever was in
  2935. * the cache is already on the platter and this won't
  2936. * cause infinite loop.
  2937. */
  2938. qc->scsicmd->allowed = max(qc->scsicmd->allowed, 1);
  2939. } else {
  2940. ata_dev_warn(dev, "FLUSH failed Emask 0x%x\n",
  2941. err_mask);
  2942. rc = -EIO;
  2943. /* if device failed it, report it to upper layers */
  2944. if (err_mask & AC_ERR_DEV) {
  2945. qc->err_mask |= AC_ERR_DEV;
  2946. qc->result_tf = tf;
  2947. if (!(ap->pflags & ATA_PFLAG_FROZEN))
  2948. rc = 0;
  2949. }
  2950. }
  2951. return rc;
  2952. }
  2953. /**
  2954. * ata_eh_set_lpm - configure SATA interface power management
  2955. * @link: link to configure power management
  2956. * @policy: the link power management policy
  2957. * @r_failed_dev: out parameter for failed device
  2958. *
  2959. * Enable SATA Interface power management. This will enable
  2960. * Device Interface Power Management (DIPM) for min_power
  2961. * policy, and then call driver specific callbacks for
  2962. * enabling Host Initiated Power management.
  2963. *
  2964. * LOCKING:
  2965. * EH context.
  2966. *
  2967. * RETURNS:
  2968. * 0 on success, -errno on failure.
  2969. */
  2970. static int ata_eh_set_lpm(struct ata_link *link, enum ata_lpm_policy policy,
  2971. struct ata_device **r_failed_dev)
  2972. {
  2973. struct ata_port *ap = ata_is_host_link(link) ? link->ap : NULL;
  2974. struct ata_eh_context *ehc = &link->eh_context;
  2975. struct ata_device *dev, *link_dev = NULL, *lpm_dev = NULL;
  2976. enum ata_lpm_policy old_policy = link->lpm_policy;
  2977. bool no_dipm = link->ap->flags & ATA_FLAG_NO_DIPM;
  2978. unsigned int hints = ATA_LPM_EMPTY | ATA_LPM_HIPM;
  2979. unsigned int err_mask;
  2980. int rc;
  2981. /* if the link or host doesn't do LPM, noop */
  2982. if ((link->flags & ATA_LFLAG_NO_LPM) || (ap && !ap->ops->set_lpm))
  2983. return 0;
  2984. /*
  2985. * DIPM is enabled only for MIN_POWER as some devices
  2986. * misbehave when the host NACKs transition to SLUMBER. Order
  2987. * device and link configurations such that the host always
  2988. * allows DIPM requests.
  2989. */
  2990. ata_for_each_dev(dev, link, ENABLED) {
  2991. bool hipm = ata_id_has_hipm(dev->id);
  2992. bool dipm = ata_id_has_dipm(dev->id) && !no_dipm;
  2993. /* find the first enabled and LPM enabled devices */
  2994. if (!link_dev)
  2995. link_dev = dev;
  2996. if (!lpm_dev && (hipm || dipm))
  2997. lpm_dev = dev;
  2998. hints &= ~ATA_LPM_EMPTY;
  2999. if (!hipm)
  3000. hints &= ~ATA_LPM_HIPM;
  3001. /* disable DIPM before changing link config */
  3002. if (policy != ATA_LPM_MIN_POWER && dipm) {
  3003. err_mask = ata_dev_set_feature(dev,
  3004. SETFEATURES_SATA_DISABLE, SATA_DIPM);
  3005. if (err_mask && err_mask != AC_ERR_DEV) {
  3006. ata_dev_warn(dev,
  3007. "failed to disable DIPM, Emask 0x%x\n",
  3008. err_mask);
  3009. rc = -EIO;
  3010. goto fail;
  3011. }
  3012. }
  3013. }
  3014. if (ap) {
  3015. rc = ap->ops->set_lpm(link, policy, hints);
  3016. if (!rc && ap->slave_link)
  3017. rc = ap->ops->set_lpm(ap->slave_link, policy, hints);
  3018. } else
  3019. rc = sata_pmp_set_lpm(link, policy, hints);
  3020. /*
  3021. * Attribute link config failure to the first (LPM) enabled
  3022. * device on the link.
  3023. */
  3024. if (rc) {
  3025. if (rc == -EOPNOTSUPP) {
  3026. link->flags |= ATA_LFLAG_NO_LPM;
  3027. return 0;
  3028. }
  3029. dev = lpm_dev ? lpm_dev : link_dev;
  3030. goto fail;
  3031. }
  3032. /*
  3033. * Low level driver acked the transition. Issue DIPM command
  3034. * with the new policy set.
  3035. */
  3036. link->lpm_policy = policy;
  3037. if (ap && ap->slave_link)
  3038. ap->slave_link->lpm_policy = policy;
  3039. /* host config updated, enable DIPM if transitioning to MIN_POWER */
  3040. ata_for_each_dev(dev, link, ENABLED) {
  3041. if (policy == ATA_LPM_MIN_POWER && !no_dipm &&
  3042. ata_id_has_dipm(dev->id)) {
  3043. err_mask = ata_dev_set_feature(dev,
  3044. SETFEATURES_SATA_ENABLE, SATA_DIPM);
  3045. if (err_mask && err_mask != AC_ERR_DEV) {
  3046. ata_dev_warn(dev,
  3047. "failed to enable DIPM, Emask 0x%x\n",
  3048. err_mask);
  3049. rc = -EIO;
  3050. goto fail;
  3051. }
  3052. }
  3053. }
  3054. link->last_lpm_change = jiffies;
  3055. link->flags |= ATA_LFLAG_CHANGED;
  3056. return 0;
  3057. fail:
  3058. /* restore the old policy */
  3059. link->lpm_policy = old_policy;
  3060. if (ap && ap->slave_link)
  3061. ap->slave_link->lpm_policy = old_policy;
  3062. /* if no device or only one more chance is left, disable LPM */
  3063. if (!dev || ehc->tries[dev->devno] <= 2) {
  3064. ata_link_warn(link, "disabling LPM on the link\n");
  3065. link->flags |= ATA_LFLAG_NO_LPM;
  3066. }
  3067. if (r_failed_dev)
  3068. *r_failed_dev = dev;
  3069. return rc;
  3070. }
  3071. int ata_link_nr_enabled(struct ata_link *link)
  3072. {
  3073. struct ata_device *dev;
  3074. int cnt = 0;
  3075. ata_for_each_dev(dev, link, ENABLED)
  3076. cnt++;
  3077. return cnt;
  3078. }
  3079. static int ata_link_nr_vacant(struct ata_link *link)
  3080. {
  3081. struct ata_device *dev;
  3082. int cnt = 0;
  3083. ata_for_each_dev(dev, link, ALL)
  3084. if (dev->class == ATA_DEV_UNKNOWN)
  3085. cnt++;
  3086. return cnt;
  3087. }
  3088. static int ata_eh_skip_recovery(struct ata_link *link)
  3089. {
  3090. struct ata_port *ap = link->ap;
  3091. struct ata_eh_context *ehc = &link->eh_context;
  3092. struct ata_device *dev;
  3093. /* skip disabled links */
  3094. if (link->flags & ATA_LFLAG_DISABLED)
  3095. return 1;
  3096. /* skip if explicitly requested */
  3097. if (ehc->i.flags & ATA_EHI_NO_RECOVERY)
  3098. return 1;
  3099. /* thaw frozen port and recover failed devices */
  3100. if ((ap->pflags & ATA_PFLAG_FROZEN) || ata_link_nr_enabled(link))
  3101. return 0;
  3102. /* reset at least once if reset is requested */
  3103. if ((ehc->i.action & ATA_EH_RESET) &&
  3104. !(ehc->i.flags & ATA_EHI_DID_RESET))
  3105. return 0;
  3106. /* skip if class codes for all vacant slots are ATA_DEV_NONE */
  3107. ata_for_each_dev(dev, link, ALL) {
  3108. if (dev->class == ATA_DEV_UNKNOWN &&
  3109. ehc->classes[dev->devno] != ATA_DEV_NONE)
  3110. return 0;
  3111. }
  3112. return 1;
  3113. }
  3114. static int ata_count_probe_trials_cb(struct ata_ering_entry *ent, void *void_arg)
  3115. {
  3116. u64 interval = msecs_to_jiffies(ATA_EH_PROBE_TRIAL_INTERVAL);
  3117. u64 now = get_jiffies_64();
  3118. int *trials = void_arg;
  3119. if ((ent->eflags & ATA_EFLAG_OLD_ER) ||
  3120. (ent->timestamp < now - min(now, interval)))
  3121. return -1;
  3122. (*trials)++;
  3123. return 0;
  3124. }
  3125. static int ata_eh_schedule_probe(struct ata_device *dev)
  3126. {
  3127. struct ata_eh_context *ehc = &dev->link->eh_context;
  3128. struct ata_link *link = ata_dev_phys_link(dev);
  3129. int trials = 0;
  3130. if (!(ehc->i.probe_mask & (1 << dev->devno)) ||
  3131. (ehc->did_probe_mask & (1 << dev->devno)))
  3132. return 0;
  3133. ata_eh_detach_dev(dev);
  3134. ata_dev_init(dev);
  3135. ehc->did_probe_mask |= (1 << dev->devno);
  3136. ehc->i.action |= ATA_EH_RESET;
  3137. ehc->saved_xfer_mode[dev->devno] = 0;
  3138. ehc->saved_ncq_enabled &= ~(1 << dev->devno);
  3139. /* the link maybe in a deep sleep, wake it up */
  3140. if (link->lpm_policy > ATA_LPM_MAX_POWER) {
  3141. if (ata_is_host_link(link))
  3142. link->ap->ops->set_lpm(link, ATA_LPM_MAX_POWER,
  3143. ATA_LPM_EMPTY);
  3144. else
  3145. sata_pmp_set_lpm(link, ATA_LPM_MAX_POWER,
  3146. ATA_LPM_EMPTY);
  3147. }
  3148. /* Record and count probe trials on the ering. The specific
  3149. * error mask used is irrelevant. Because a successful device
  3150. * detection clears the ering, this count accumulates only if
  3151. * there are consecutive failed probes.
  3152. *
  3153. * If the count is equal to or higher than ATA_EH_PROBE_TRIALS
  3154. * in the last ATA_EH_PROBE_TRIAL_INTERVAL, link speed is
  3155. * forced to 1.5Gbps.
  3156. *
  3157. * This is to work around cases where failed link speed
  3158. * negotiation results in device misdetection leading to
  3159. * infinite DEVXCHG or PHRDY CHG events.
  3160. */
  3161. ata_ering_record(&dev->ering, 0, AC_ERR_OTHER);
  3162. ata_ering_map(&dev->ering, ata_count_probe_trials_cb, &trials);
  3163. if (trials > ATA_EH_PROBE_TRIALS)
  3164. sata_down_spd_limit(link, 1);
  3165. return 1;
  3166. }
  3167. static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
  3168. {
  3169. struct ata_eh_context *ehc = &dev->link->eh_context;
  3170. /* -EAGAIN from EH routine indicates retry without prejudice.
  3171. * The requester is responsible for ensuring forward progress.
  3172. */
  3173. if (err != -EAGAIN)
  3174. ehc->tries[dev->devno]--;
  3175. switch (err) {
  3176. case -ENODEV:
  3177. /* device missing or wrong IDENTIFY data, schedule probing */
  3178. ehc->i.probe_mask |= (1 << dev->devno);
  3179. case -EINVAL:
  3180. /* give it just one more chance */
  3181. ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
  3182. case -EIO:
  3183. if (ehc->tries[dev->devno] == 1) {
  3184. /* This is the last chance, better to slow
  3185. * down than lose it.
  3186. */
  3187. sata_down_spd_limit(ata_dev_phys_link(dev), 0);
  3188. if (dev->pio_mode > XFER_PIO_0)
  3189. ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
  3190. }
  3191. }
  3192. if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
  3193. /* disable device if it has used up all its chances */
  3194. ata_dev_disable(dev);
  3195. /* detach if offline */
  3196. if (ata_phys_link_offline(ata_dev_phys_link(dev)))
  3197. ata_eh_detach_dev(dev);
  3198. /* schedule probe if necessary */
  3199. if (ata_eh_schedule_probe(dev)) {
  3200. ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
  3201. memset(ehc->cmd_timeout_idx[dev->devno], 0,
  3202. sizeof(ehc->cmd_timeout_idx[dev->devno]));
  3203. }
  3204. return 1;
  3205. } else {
  3206. ehc->i.action |= ATA_EH_RESET;
  3207. return 0;
  3208. }
  3209. }
  3210. /**
  3211. * ata_eh_recover - recover host port after error
  3212. * @ap: host port to recover
  3213. * @prereset: prereset method (can be NULL)
  3214. * @softreset: softreset method (can be NULL)
  3215. * @hardreset: hardreset method (can be NULL)
  3216. * @postreset: postreset method (can be NULL)
  3217. * @r_failed_link: out parameter for failed link
  3218. *
  3219. * This is the alpha and omega, eum and yang, heart and soul of
  3220. * libata exception handling. On entry, actions required to
  3221. * recover each link and hotplug requests are recorded in the
  3222. * link's eh_context. This function executes all the operations
  3223. * with appropriate retrials and fallbacks to resurrect failed
  3224. * devices, detach goners and greet newcomers.
  3225. *
  3226. * LOCKING:
  3227. * Kernel thread context (may sleep).
  3228. *
  3229. * RETURNS:
  3230. * 0 on success, -errno on failure.
  3231. */
  3232. int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
  3233. ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
  3234. ata_postreset_fn_t postreset,
  3235. struct ata_link **r_failed_link)
  3236. {
  3237. struct ata_link *link;
  3238. struct ata_device *dev;
  3239. int rc, nr_fails;
  3240. unsigned long flags, deadline;
  3241. DPRINTK("ENTER\n");
  3242. /* prep for recovery */
  3243. ata_for_each_link(link, ap, EDGE) {
  3244. struct ata_eh_context *ehc = &link->eh_context;
  3245. /* re-enable link? */
  3246. if (ehc->i.action & ATA_EH_ENABLE_LINK) {
  3247. ata_eh_about_to_do(link, NULL, ATA_EH_ENABLE_LINK);
  3248. spin_lock_irqsave(ap->lock, flags);
  3249. link->flags &= ~ATA_LFLAG_DISABLED;
  3250. spin_unlock_irqrestore(ap->lock, flags);
  3251. ata_eh_done(link, NULL, ATA_EH_ENABLE_LINK);
  3252. }
  3253. ata_for_each_dev(dev, link, ALL) {
  3254. if (link->flags & ATA_LFLAG_NO_RETRY)
  3255. ehc->tries[dev->devno] = 1;
  3256. else
  3257. ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
  3258. /* collect port action mask recorded in dev actions */
  3259. ehc->i.action |= ehc->i.dev_action[dev->devno] &
  3260. ~ATA_EH_PERDEV_MASK;
  3261. ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK;
  3262. /* process hotplug request */
  3263. if (dev->flags & ATA_DFLAG_DETACH)
  3264. ata_eh_detach_dev(dev);
  3265. /* schedule probe if necessary */
  3266. if (!ata_dev_enabled(dev))
  3267. ata_eh_schedule_probe(dev);
  3268. }
  3269. }
  3270. retry:
  3271. rc = 0;
  3272. /* if UNLOADING, finish immediately */
  3273. if (ap->pflags & ATA_PFLAG_UNLOADING)
  3274. goto out;
  3275. /* prep for EH */
  3276. ata_for_each_link(link, ap, EDGE) {
  3277. struct ata_eh_context *ehc = &link->eh_context;
  3278. /* skip EH if possible. */
  3279. if (ata_eh_skip_recovery(link))
  3280. ehc->i.action = 0;
  3281. ata_for_each_dev(dev, link, ALL)
  3282. ehc->classes[dev->devno] = ATA_DEV_UNKNOWN;
  3283. }
  3284. /* reset */
  3285. ata_for_each_link(link, ap, EDGE) {
  3286. struct ata_eh_context *ehc = &link->eh_context;
  3287. if (!(ehc->i.action & ATA_EH_RESET))
  3288. continue;
  3289. rc = ata_eh_reset(link, ata_link_nr_vacant(link),
  3290. prereset, softreset, hardreset, postreset);
  3291. if (rc) {
  3292. ata_link_err(link, "reset failed, giving up\n");
  3293. goto out;
  3294. }
  3295. }
  3296. do {
  3297. unsigned long now;
  3298. /*
  3299. * clears ATA_EH_PARK in eh_info and resets
  3300. * ap->park_req_pending
  3301. */
  3302. ata_eh_pull_park_action(ap);
  3303. deadline = jiffies;
  3304. ata_for_each_link(link, ap, EDGE) {
  3305. ata_for_each_dev(dev, link, ALL) {
  3306. struct ata_eh_context *ehc = &link->eh_context;
  3307. unsigned long tmp;
  3308. if (dev->class != ATA_DEV_ATA &&
  3309. dev->class != ATA_DEV_ZAC)
  3310. continue;
  3311. if (!(ehc->i.dev_action[dev->devno] &
  3312. ATA_EH_PARK))
  3313. continue;
  3314. tmp = dev->unpark_deadline;
  3315. if (time_before(deadline, tmp))
  3316. deadline = tmp;
  3317. else if (time_before_eq(tmp, jiffies))
  3318. continue;
  3319. if (ehc->unloaded_mask & (1 << dev->devno))
  3320. continue;
  3321. ata_eh_park_issue_cmd(dev, 1);
  3322. }
  3323. }
  3324. now = jiffies;
  3325. if (time_before_eq(deadline, now))
  3326. break;
  3327. ata_eh_release(ap);
  3328. deadline = wait_for_completion_timeout(&ap->park_req_pending,
  3329. deadline - now);
  3330. ata_eh_acquire(ap);
  3331. } while (deadline);
  3332. ata_for_each_link(link, ap, EDGE) {
  3333. ata_for_each_dev(dev, link, ALL) {
  3334. if (!(link->eh_context.unloaded_mask &
  3335. (1 << dev->devno)))
  3336. continue;
  3337. ata_eh_park_issue_cmd(dev, 0);
  3338. ata_eh_done(link, dev, ATA_EH_PARK);
  3339. }
  3340. }
  3341. /* the rest */
  3342. nr_fails = 0;
  3343. ata_for_each_link(link, ap, PMP_FIRST) {
  3344. struct ata_eh_context *ehc = &link->eh_context;
  3345. if (sata_pmp_attached(ap) && ata_is_host_link(link))
  3346. goto config_lpm;
  3347. /* revalidate existing devices and attach new ones */
  3348. rc = ata_eh_revalidate_and_attach(link, &dev);
  3349. if (rc)
  3350. goto rest_fail;
  3351. /* if PMP got attached, return, pmp EH will take care of it */
  3352. if (link->device->class == ATA_DEV_PMP) {
  3353. ehc->i.action = 0;
  3354. return 0;
  3355. }
  3356. /* configure transfer mode if necessary */
  3357. if (ehc->i.flags & ATA_EHI_SETMODE) {
  3358. rc = ata_set_mode(link, &dev);
  3359. if (rc)
  3360. goto rest_fail;
  3361. ehc->i.flags &= ~ATA_EHI_SETMODE;
  3362. }
  3363. /* If reset has been issued, clear UA to avoid
  3364. * disrupting the current users of the device.
  3365. */
  3366. if (ehc->i.flags & ATA_EHI_DID_RESET) {
  3367. ata_for_each_dev(dev, link, ALL) {
  3368. if (dev->class != ATA_DEV_ATAPI)
  3369. continue;
  3370. rc = atapi_eh_clear_ua(dev);
  3371. if (rc)
  3372. goto rest_fail;
  3373. if (zpodd_dev_enabled(dev))
  3374. zpodd_post_poweron(dev);
  3375. }
  3376. }
  3377. /* retry flush if necessary */
  3378. ata_for_each_dev(dev, link, ALL) {
  3379. if (dev->class != ATA_DEV_ATA &&
  3380. dev->class != ATA_DEV_ZAC)
  3381. continue;
  3382. rc = ata_eh_maybe_retry_flush(dev);
  3383. if (rc)
  3384. goto rest_fail;
  3385. }
  3386. config_lpm:
  3387. /* configure link power saving */
  3388. if (link->lpm_policy != ap->target_lpm_policy) {
  3389. rc = ata_eh_set_lpm(link, ap->target_lpm_policy, &dev);
  3390. if (rc)
  3391. goto rest_fail;
  3392. }
  3393. /* this link is okay now */
  3394. ehc->i.flags = 0;
  3395. continue;
  3396. rest_fail:
  3397. nr_fails++;
  3398. if (dev)
  3399. ata_eh_handle_dev_fail(dev, rc);
  3400. if (ap->pflags & ATA_PFLAG_FROZEN) {
  3401. /* PMP reset requires working host port.
  3402. * Can't retry if it's frozen.
  3403. */
  3404. if (sata_pmp_attached(ap))
  3405. goto out;
  3406. break;
  3407. }
  3408. }
  3409. if (nr_fails)
  3410. goto retry;
  3411. out:
  3412. if (rc && r_failed_link)
  3413. *r_failed_link = link;
  3414. DPRINTK("EXIT, rc=%d\n", rc);
  3415. return rc;
  3416. }
  3417. /**
  3418. * ata_eh_finish - finish up EH
  3419. * @ap: host port to finish EH for
  3420. *
  3421. * Recovery is complete. Clean up EH states and retry or finish
  3422. * failed qcs.
  3423. *
  3424. * LOCKING:
  3425. * None.
  3426. */
  3427. void ata_eh_finish(struct ata_port *ap)
  3428. {
  3429. int tag;
  3430. /* retry or finish qcs */
  3431. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  3432. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  3433. if (!(qc->flags & ATA_QCFLAG_FAILED))
  3434. continue;
  3435. if (qc->err_mask) {
  3436. /* FIXME: Once EH migration is complete,
  3437. * generate sense data in this function,
  3438. * considering both err_mask and tf.
  3439. */
  3440. if (qc->flags & ATA_QCFLAG_RETRY)
  3441. ata_eh_qc_retry(qc);
  3442. else
  3443. ata_eh_qc_complete(qc);
  3444. } else {
  3445. if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
  3446. ata_eh_qc_complete(qc);
  3447. } else {
  3448. /* feed zero TF to sense generation */
  3449. memset(&qc->result_tf, 0, sizeof(qc->result_tf));
  3450. ata_eh_qc_retry(qc);
  3451. }
  3452. }
  3453. }
  3454. /* make sure nr_active_links is zero after EH */
  3455. WARN_ON(ap->nr_active_links);
  3456. ap->nr_active_links = 0;
  3457. }
  3458. /**
  3459. * ata_do_eh - do standard error handling
  3460. * @ap: host port to handle error for
  3461. *
  3462. * @prereset: prereset method (can be NULL)
  3463. * @softreset: softreset method (can be NULL)
  3464. * @hardreset: hardreset method (can be NULL)
  3465. * @postreset: postreset method (can be NULL)
  3466. *
  3467. * Perform standard error handling sequence.
  3468. *
  3469. * LOCKING:
  3470. * Kernel thread context (may sleep).
  3471. */
  3472. void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
  3473. ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
  3474. ata_postreset_fn_t postreset)
  3475. {
  3476. struct ata_device *dev;
  3477. int rc;
  3478. ata_eh_autopsy(ap);
  3479. ata_eh_report(ap);
  3480. rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset,
  3481. NULL);
  3482. if (rc) {
  3483. ata_for_each_dev(dev, &ap->link, ALL)
  3484. ata_dev_disable(dev);
  3485. }
  3486. ata_eh_finish(ap);
  3487. }
  3488. /**
  3489. * ata_std_error_handler - standard error handler
  3490. * @ap: host port to handle error for
  3491. *
  3492. * Standard error handler
  3493. *
  3494. * LOCKING:
  3495. * Kernel thread context (may sleep).
  3496. */
  3497. void ata_std_error_handler(struct ata_port *ap)
  3498. {
  3499. struct ata_port_operations *ops = ap->ops;
  3500. ata_reset_fn_t hardreset = ops->hardreset;
  3501. /* ignore built-in hardreset if SCR access is not available */
  3502. if (hardreset == sata_std_hardreset && !sata_scr_valid(&ap->link))
  3503. hardreset = NULL;
  3504. ata_do_eh(ap, ops->prereset, ops->softreset, hardreset, ops->postreset);
  3505. }
  3506. #ifdef CONFIG_PM
  3507. /**
  3508. * ata_eh_handle_port_suspend - perform port suspend operation
  3509. * @ap: port to suspend
  3510. *
  3511. * Suspend @ap.
  3512. *
  3513. * LOCKING:
  3514. * Kernel thread context (may sleep).
  3515. */
  3516. static void ata_eh_handle_port_suspend(struct ata_port *ap)
  3517. {
  3518. unsigned long flags;
  3519. int rc = 0;
  3520. struct ata_device *dev;
  3521. /* are we suspending? */
  3522. spin_lock_irqsave(ap->lock, flags);
  3523. if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
  3524. ap->pm_mesg.event & PM_EVENT_RESUME) {
  3525. spin_unlock_irqrestore(ap->lock, flags);
  3526. return;
  3527. }
  3528. spin_unlock_irqrestore(ap->lock, flags);
  3529. WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
  3530. /*
  3531. * If we have a ZPODD attached, check its zero
  3532. * power ready status before the port is frozen.
  3533. * Only needed for runtime suspend.
  3534. */
  3535. if (PMSG_IS_AUTO(ap->pm_mesg)) {
  3536. ata_for_each_dev(dev, &ap->link, ENABLED) {
  3537. if (zpodd_dev_enabled(dev))
  3538. zpodd_on_suspend(dev);
  3539. }
  3540. }
  3541. /* tell ACPI we're suspending */
  3542. rc = ata_acpi_on_suspend(ap);
  3543. if (rc)
  3544. goto out;
  3545. /* suspend */
  3546. ata_eh_freeze_port(ap);
  3547. if (ap->ops->port_suspend)
  3548. rc = ap->ops->port_suspend(ap, ap->pm_mesg);
  3549. ata_acpi_set_state(ap, ap->pm_mesg);
  3550. out:
  3551. /* update the flags */
  3552. spin_lock_irqsave(ap->lock, flags);
  3553. ap->pflags &= ~ATA_PFLAG_PM_PENDING;
  3554. if (rc == 0)
  3555. ap->pflags |= ATA_PFLAG_SUSPENDED;
  3556. else if (ap->pflags & ATA_PFLAG_FROZEN)
  3557. ata_port_schedule_eh(ap);
  3558. spin_unlock_irqrestore(ap->lock, flags);
  3559. return;
  3560. }
  3561. /**
  3562. * ata_eh_handle_port_resume - perform port resume operation
  3563. * @ap: port to resume
  3564. *
  3565. * Resume @ap.
  3566. *
  3567. * LOCKING:
  3568. * Kernel thread context (may sleep).
  3569. */
  3570. static void ata_eh_handle_port_resume(struct ata_port *ap)
  3571. {
  3572. struct ata_link *link;
  3573. struct ata_device *dev;
  3574. unsigned long flags;
  3575. int rc = 0;
  3576. /* are we resuming? */
  3577. spin_lock_irqsave(ap->lock, flags);
  3578. if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
  3579. !(ap->pm_mesg.event & PM_EVENT_RESUME)) {
  3580. spin_unlock_irqrestore(ap->lock, flags);
  3581. return;
  3582. }
  3583. spin_unlock_irqrestore(ap->lock, flags);
  3584. WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
  3585. /*
  3586. * Error timestamps are in jiffies which doesn't run while
  3587. * suspended and PHY events during resume isn't too uncommon.
  3588. * When the two are combined, it can lead to unnecessary speed
  3589. * downs if the machine is suspended and resumed repeatedly.
  3590. * Clear error history.
  3591. */
  3592. ata_for_each_link(link, ap, HOST_FIRST)
  3593. ata_for_each_dev(dev, link, ALL)
  3594. ata_ering_clear(&dev->ering);
  3595. ata_acpi_set_state(ap, ap->pm_mesg);
  3596. if (ap->ops->port_resume)
  3597. rc = ap->ops->port_resume(ap);
  3598. /* tell ACPI that we're resuming */
  3599. ata_acpi_on_resume(ap);
  3600. /* update the flags */
  3601. spin_lock_irqsave(ap->lock, flags);
  3602. ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
  3603. spin_unlock_irqrestore(ap->lock, flags);
  3604. }
  3605. #endif /* CONFIG_PM */