libata-eh.c 105 KB

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