libata-eh.c 89 KB

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