libata-eh.c 88 KB

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