libata-eh.c 112 KB

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