libata-eh.c 83 KB

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