libata-eh.c 99 KB

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