libata-eh.c 77 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093
  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 = dev->link;
  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) || qc->dev->link != link)
  1611. continue;
  1612. /* inherit upper level err_mask */
  1613. qc->err_mask |= ehc->i.err_mask;
  1614. /* analyze TF */
  1615. ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
  1616. /* DEV errors are probably spurious in case of ATA_BUS error */
  1617. if (qc->err_mask & AC_ERR_ATA_BUS)
  1618. qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
  1619. AC_ERR_INVALID);
  1620. /* any real error trumps unknown error */
  1621. if (qc->err_mask & ~AC_ERR_OTHER)
  1622. qc->err_mask &= ~AC_ERR_OTHER;
  1623. /* SENSE_VALID trumps dev/unknown error and revalidation */
  1624. if (qc->flags & ATA_QCFLAG_SENSE_VALID)
  1625. qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
  1626. /* determine whether the command is worth retrying */
  1627. if (!(qc->err_mask & AC_ERR_INVALID) &&
  1628. ((qc->flags & ATA_QCFLAG_IO) || qc->err_mask != AC_ERR_DEV))
  1629. qc->flags |= ATA_QCFLAG_RETRY;
  1630. /* accumulate error info */
  1631. ehc->i.dev = qc->dev;
  1632. all_err_mask |= qc->err_mask;
  1633. if (qc->flags & ATA_QCFLAG_IO)
  1634. eflags |= ATA_EFLAG_IS_IO;
  1635. }
  1636. /* enforce default EH actions */
  1637. if (ap->pflags & ATA_PFLAG_FROZEN ||
  1638. all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
  1639. ehc->i.action |= ATA_EH_RESET;
  1640. else if (((eflags & ATA_EFLAG_IS_IO) && all_err_mask) ||
  1641. (!(eflags & ATA_EFLAG_IS_IO) && (all_err_mask & ~AC_ERR_DEV)))
  1642. ehc->i.action |= ATA_EH_REVALIDATE;
  1643. /* If we have offending qcs and the associated failed device,
  1644. * perform per-dev EH action only on the offending device.
  1645. */
  1646. if (ehc->i.dev) {
  1647. ehc->i.dev_action[ehc->i.dev->devno] |=
  1648. ehc->i.action & ATA_EH_PERDEV_MASK;
  1649. ehc->i.action &= ~ATA_EH_PERDEV_MASK;
  1650. }
  1651. /* propagate timeout to host link */
  1652. if ((all_err_mask & AC_ERR_TIMEOUT) && !ata_is_host_link(link))
  1653. ap->link.eh_context.i.err_mask |= AC_ERR_TIMEOUT;
  1654. /* record error and consider speeding down */
  1655. dev = ehc->i.dev;
  1656. if (!dev && ((ata_link_max_devices(link) == 1 &&
  1657. ata_dev_enabled(link->device))))
  1658. dev = link->device;
  1659. if (dev) {
  1660. if (dev->flags & ATA_DFLAG_DUBIOUS_XFER)
  1661. eflags |= ATA_EFLAG_DUBIOUS_XFER;
  1662. ehc->i.action |= ata_eh_speed_down(dev, eflags, all_err_mask);
  1663. }
  1664. DPRINTK("EXIT\n");
  1665. }
  1666. /**
  1667. * ata_eh_autopsy - analyze error and determine recovery action
  1668. * @ap: host port to perform autopsy on
  1669. *
  1670. * Analyze all links of @ap and determine why they failed and
  1671. * which recovery actions are needed.
  1672. *
  1673. * LOCKING:
  1674. * Kernel thread context (may sleep).
  1675. */
  1676. void ata_eh_autopsy(struct ata_port *ap)
  1677. {
  1678. struct ata_link *link;
  1679. ata_port_for_each_link(link, ap)
  1680. ata_eh_link_autopsy(link);
  1681. /* Autopsy of fanout ports can affect host link autopsy.
  1682. * Perform host link autopsy last.
  1683. */
  1684. if (sata_pmp_attached(ap))
  1685. ata_eh_link_autopsy(&ap->link);
  1686. }
  1687. /**
  1688. * ata_eh_link_report - report error handling to user
  1689. * @link: ATA link EH is going on
  1690. *
  1691. * Report EH to user.
  1692. *
  1693. * LOCKING:
  1694. * None.
  1695. */
  1696. static void ata_eh_link_report(struct ata_link *link)
  1697. {
  1698. struct ata_port *ap = link->ap;
  1699. struct ata_eh_context *ehc = &link->eh_context;
  1700. const char *frozen, *desc;
  1701. char tries_buf[6];
  1702. int tag, nr_failed = 0;
  1703. if (ehc->i.flags & ATA_EHI_QUIET)
  1704. return;
  1705. desc = NULL;
  1706. if (ehc->i.desc[0] != '\0')
  1707. desc = ehc->i.desc;
  1708. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  1709. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  1710. if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link ||
  1711. ((qc->flags & ATA_QCFLAG_QUIET) &&
  1712. qc->err_mask == AC_ERR_DEV))
  1713. continue;
  1714. if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
  1715. continue;
  1716. nr_failed++;
  1717. }
  1718. if (!nr_failed && !ehc->i.err_mask)
  1719. return;
  1720. frozen = "";
  1721. if (ap->pflags & ATA_PFLAG_FROZEN)
  1722. frozen = " frozen";
  1723. memset(tries_buf, 0, sizeof(tries_buf));
  1724. if (ap->eh_tries < ATA_EH_MAX_TRIES)
  1725. snprintf(tries_buf, sizeof(tries_buf) - 1, " t%d",
  1726. ap->eh_tries);
  1727. if (ehc->i.dev) {
  1728. ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
  1729. "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
  1730. ehc->i.err_mask, link->sactive, ehc->i.serror,
  1731. ehc->i.action, frozen, tries_buf);
  1732. if (desc)
  1733. ata_dev_printk(ehc->i.dev, KERN_ERR, "%s\n", desc);
  1734. } else {
  1735. ata_link_printk(link, KERN_ERR, "exception Emask 0x%x "
  1736. "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
  1737. ehc->i.err_mask, link->sactive, ehc->i.serror,
  1738. ehc->i.action, frozen, tries_buf);
  1739. if (desc)
  1740. ata_link_printk(link, KERN_ERR, "%s\n", desc);
  1741. }
  1742. if (ehc->i.serror)
  1743. ata_port_printk(ap, KERN_ERR,
  1744. "SError: { %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s}\n",
  1745. ehc->i.serror & SERR_DATA_RECOVERED ? "RecovData " : "",
  1746. ehc->i.serror & SERR_COMM_RECOVERED ? "RecovComm " : "",
  1747. ehc->i.serror & SERR_DATA ? "UnrecovData " : "",
  1748. ehc->i.serror & SERR_PERSISTENT ? "Persist " : "",
  1749. ehc->i.serror & SERR_PROTOCOL ? "Proto " : "",
  1750. ehc->i.serror & SERR_INTERNAL ? "HostInt " : "",
  1751. ehc->i.serror & SERR_PHYRDY_CHG ? "PHYRdyChg " : "",
  1752. ehc->i.serror & SERR_PHY_INT_ERR ? "PHYInt " : "",
  1753. ehc->i.serror & SERR_COMM_WAKE ? "CommWake " : "",
  1754. ehc->i.serror & SERR_10B_8B_ERR ? "10B8B " : "",
  1755. ehc->i.serror & SERR_DISPARITY ? "Dispar " : "",
  1756. ehc->i.serror & SERR_CRC ? "BadCRC " : "",
  1757. ehc->i.serror & SERR_HANDSHAKE ? "Handshk " : "",
  1758. ehc->i.serror & SERR_LINK_SEQ_ERR ? "LinkSeq " : "",
  1759. ehc->i.serror & SERR_TRANS_ST_ERROR ? "TrStaTrns " : "",
  1760. ehc->i.serror & SERR_UNRECOG_FIS ? "UnrecFIS " : "",
  1761. ehc->i.serror & SERR_DEV_XCHG ? "DevExch " : "");
  1762. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  1763. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  1764. struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
  1765. const u8 *cdb = qc->cdb;
  1766. char data_buf[20] = "";
  1767. char cdb_buf[70] = "";
  1768. if (!(qc->flags & ATA_QCFLAG_FAILED) ||
  1769. qc->dev->link != link || !qc->err_mask)
  1770. continue;
  1771. if (qc->dma_dir != DMA_NONE) {
  1772. static const char *dma_str[] = {
  1773. [DMA_BIDIRECTIONAL] = "bidi",
  1774. [DMA_TO_DEVICE] = "out",
  1775. [DMA_FROM_DEVICE] = "in",
  1776. };
  1777. static const char *prot_str[] = {
  1778. [ATA_PROT_PIO] = "pio",
  1779. [ATA_PROT_DMA] = "dma",
  1780. [ATA_PROT_NCQ] = "ncq",
  1781. [ATAPI_PROT_PIO] = "pio",
  1782. [ATAPI_PROT_DMA] = "dma",
  1783. };
  1784. snprintf(data_buf, sizeof(data_buf), " %s %u %s",
  1785. prot_str[qc->tf.protocol], qc->nbytes,
  1786. dma_str[qc->dma_dir]);
  1787. }
  1788. if (ata_is_atapi(qc->tf.protocol))
  1789. snprintf(cdb_buf, sizeof(cdb_buf),
  1790. "cdb %02x %02x %02x %02x %02x %02x %02x %02x "
  1791. "%02x %02x %02x %02x %02x %02x %02x %02x\n ",
  1792. cdb[0], cdb[1], cdb[2], cdb[3],
  1793. cdb[4], cdb[5], cdb[6], cdb[7],
  1794. cdb[8], cdb[9], cdb[10], cdb[11],
  1795. cdb[12], cdb[13], cdb[14], cdb[15]);
  1796. ata_dev_printk(qc->dev, KERN_ERR,
  1797. "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
  1798. "tag %d%s\n %s"
  1799. "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
  1800. "Emask 0x%x (%s)%s\n",
  1801. cmd->command, cmd->feature, cmd->nsect,
  1802. cmd->lbal, cmd->lbam, cmd->lbah,
  1803. cmd->hob_feature, cmd->hob_nsect,
  1804. cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
  1805. cmd->device, qc->tag, data_buf, cdb_buf,
  1806. res->command, res->feature, res->nsect,
  1807. res->lbal, res->lbam, res->lbah,
  1808. res->hob_feature, res->hob_nsect,
  1809. res->hob_lbal, res->hob_lbam, res->hob_lbah,
  1810. res->device, qc->err_mask, ata_err_string(qc->err_mask),
  1811. qc->err_mask & AC_ERR_NCQ ? " <F>" : "");
  1812. if (res->command & (ATA_BUSY | ATA_DRDY | ATA_DF | ATA_DRQ |
  1813. ATA_ERR)) {
  1814. if (res->command & ATA_BUSY)
  1815. ata_dev_printk(qc->dev, KERN_ERR,
  1816. "status: { Busy }\n");
  1817. else
  1818. ata_dev_printk(qc->dev, KERN_ERR,
  1819. "status: { %s%s%s%s}\n",
  1820. res->command & ATA_DRDY ? "DRDY " : "",
  1821. res->command & ATA_DF ? "DF " : "",
  1822. res->command & ATA_DRQ ? "DRQ " : "",
  1823. res->command & ATA_ERR ? "ERR " : "");
  1824. }
  1825. if (cmd->command != ATA_CMD_PACKET &&
  1826. (res->feature & (ATA_ICRC | ATA_UNC | ATA_IDNF |
  1827. ATA_ABORTED)))
  1828. ata_dev_printk(qc->dev, KERN_ERR,
  1829. "error: { %s%s%s%s}\n",
  1830. res->feature & ATA_ICRC ? "ICRC " : "",
  1831. res->feature & ATA_UNC ? "UNC " : "",
  1832. res->feature & ATA_IDNF ? "IDNF " : "",
  1833. res->feature & ATA_ABORTED ? "ABRT " : "");
  1834. }
  1835. }
  1836. /**
  1837. * ata_eh_report - report error handling to user
  1838. * @ap: ATA port to report EH about
  1839. *
  1840. * Report EH to user.
  1841. *
  1842. * LOCKING:
  1843. * None.
  1844. */
  1845. void ata_eh_report(struct ata_port *ap)
  1846. {
  1847. struct ata_link *link;
  1848. __ata_port_for_each_link(link, ap)
  1849. ata_eh_link_report(link);
  1850. }
  1851. static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset,
  1852. unsigned int *classes, unsigned long deadline)
  1853. {
  1854. struct ata_device *dev;
  1855. ata_link_for_each_dev(dev, link)
  1856. classes[dev->devno] = ATA_DEV_UNKNOWN;
  1857. return reset(link, classes, deadline);
  1858. }
  1859. static int ata_eh_followup_srst_needed(struct ata_link *link,
  1860. int rc, int classify,
  1861. const unsigned int *classes)
  1862. {
  1863. if ((link->flags & ATA_LFLAG_NO_SRST) || ata_link_offline(link))
  1864. return 0;
  1865. if (rc == -EAGAIN) {
  1866. if (classify)
  1867. return 1;
  1868. rc = 0;
  1869. }
  1870. if (rc != 0)
  1871. return 0;
  1872. if (sata_pmp_supported(link->ap) && ata_is_host_link(link))
  1873. return 1;
  1874. return 0;
  1875. }
  1876. int ata_eh_reset(struct ata_link *link, int classify,
  1877. ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
  1878. ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
  1879. {
  1880. struct ata_port *ap = link->ap;
  1881. struct ata_eh_context *ehc = &link->eh_context;
  1882. unsigned int *classes = ehc->classes;
  1883. unsigned int lflags = link->flags;
  1884. int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
  1885. int max_tries = 0, try = 0;
  1886. struct ata_device *dev;
  1887. unsigned long deadline, now;
  1888. ata_reset_fn_t reset;
  1889. unsigned long flags;
  1890. u32 sstatus;
  1891. int nr_known, rc;
  1892. /*
  1893. * Prepare to reset
  1894. */
  1895. while (ata_eh_reset_timeouts[max_tries] != ULONG_MAX)
  1896. max_tries++;
  1897. now = jiffies;
  1898. deadline = ata_deadline(ehc->last_reset, ATA_EH_RESET_COOL_DOWN);
  1899. if (time_before(now, deadline))
  1900. schedule_timeout_uninterruptible(deadline - now);
  1901. spin_lock_irqsave(ap->lock, flags);
  1902. ap->pflags |= ATA_PFLAG_RESETTING;
  1903. spin_unlock_irqrestore(ap->lock, flags);
  1904. ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
  1905. ehc->last_reset = jiffies;
  1906. ata_link_for_each_dev(dev, link) {
  1907. /* If we issue an SRST then an ATA drive (not ATAPI)
  1908. * may change configuration and be in PIO0 timing. If
  1909. * we do a hard reset (or are coming from power on)
  1910. * this is true for ATA or ATAPI. Until we've set a
  1911. * suitable controller mode we should not touch the
  1912. * bus as we may be talking too fast.
  1913. */
  1914. dev->pio_mode = XFER_PIO_0;
  1915. /* If the controller has a pio mode setup function
  1916. * then use it to set the chipset to rights. Don't
  1917. * touch the DMA setup as that will be dealt with when
  1918. * configuring devices.
  1919. */
  1920. if (ap->ops->set_piomode)
  1921. ap->ops->set_piomode(ap, dev);
  1922. }
  1923. /* prefer hardreset */
  1924. reset = NULL;
  1925. ehc->i.action &= ~ATA_EH_RESET;
  1926. if (hardreset) {
  1927. reset = hardreset;
  1928. ehc->i.action = ATA_EH_HARDRESET;
  1929. } else if (softreset) {
  1930. reset = softreset;
  1931. ehc->i.action = ATA_EH_SOFTRESET;
  1932. }
  1933. if (prereset) {
  1934. rc = prereset(link,
  1935. ata_deadline(jiffies, ATA_EH_PRERESET_TIMEOUT));
  1936. if (rc) {
  1937. if (rc == -ENOENT) {
  1938. ata_link_printk(link, KERN_DEBUG,
  1939. "port disabled. ignoring.\n");
  1940. ehc->i.action &= ~ATA_EH_RESET;
  1941. ata_link_for_each_dev(dev, link)
  1942. classes[dev->devno] = ATA_DEV_NONE;
  1943. rc = 0;
  1944. } else
  1945. ata_link_printk(link, KERN_ERR,
  1946. "prereset failed (errno=%d)\n", rc);
  1947. goto out;
  1948. }
  1949. /* prereset() might have cleared ATA_EH_RESET. If so,
  1950. * bang classes and return.
  1951. */
  1952. if (reset && !(ehc->i.action & ATA_EH_RESET)) {
  1953. ata_link_for_each_dev(dev, link)
  1954. classes[dev->devno] = ATA_DEV_NONE;
  1955. rc = 0;
  1956. goto out;
  1957. }
  1958. }
  1959. retry:
  1960. /*
  1961. * Perform reset
  1962. */
  1963. ehc->last_reset = jiffies;
  1964. if (ata_is_host_link(link))
  1965. ata_eh_freeze_port(ap);
  1966. deadline = ata_deadline(jiffies, ata_eh_reset_timeouts[try++]);
  1967. if (reset) {
  1968. if (verbose)
  1969. ata_link_printk(link, KERN_INFO, "%s resetting link\n",
  1970. reset == softreset ? "soft" : "hard");
  1971. /* mark that this EH session started with reset */
  1972. if (reset == hardreset)
  1973. ehc->i.flags |= ATA_EHI_DID_HARDRESET;
  1974. else
  1975. ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
  1976. rc = ata_do_reset(link, reset, classes, deadline);
  1977. if (reset == hardreset &&
  1978. ata_eh_followup_srst_needed(link, rc, classify, classes)) {
  1979. /* okay, let's do follow-up softreset */
  1980. reset = softreset;
  1981. if (!reset) {
  1982. ata_link_printk(link, KERN_ERR,
  1983. "follow-up softreset required "
  1984. "but no softreset avaliable\n");
  1985. rc = -EINVAL;
  1986. goto fail;
  1987. }
  1988. ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
  1989. rc = ata_do_reset(link, reset, classes, deadline);
  1990. }
  1991. /* -EAGAIN can happen if we skipped followup SRST */
  1992. if (rc && rc != -EAGAIN)
  1993. goto fail;
  1994. } else {
  1995. if (verbose)
  1996. ata_link_printk(link, KERN_INFO, "no reset method "
  1997. "available, skipping reset\n");
  1998. if (!(lflags & ATA_LFLAG_ASSUME_CLASS))
  1999. lflags |= ATA_LFLAG_ASSUME_ATA;
  2000. }
  2001. /*
  2002. * Post-reset processing
  2003. */
  2004. ata_link_for_each_dev(dev, link) {
  2005. /* After the reset, the device state is PIO 0 and the
  2006. * controller state is undefined. Reset also wakes up
  2007. * drives from sleeping mode.
  2008. */
  2009. dev->pio_mode = XFER_PIO_0;
  2010. dev->flags &= ~ATA_DFLAG_SLEEPING;
  2011. if (ata_link_offline(link))
  2012. continue;
  2013. /* apply class override */
  2014. if (lflags & ATA_LFLAG_ASSUME_ATA)
  2015. classes[dev->devno] = ATA_DEV_ATA;
  2016. else if (lflags & ATA_LFLAG_ASSUME_SEMB)
  2017. classes[dev->devno] = ATA_DEV_SEMB_UNSUP; /* not yet */
  2018. }
  2019. /* record current link speed */
  2020. if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0)
  2021. link->sata_spd = (sstatus >> 4) & 0xf;
  2022. /* thaw the port */
  2023. if (ata_is_host_link(link))
  2024. ata_eh_thaw_port(ap);
  2025. /* postreset() should clear hardware SError. Although SError
  2026. * is cleared during link resume, clearing SError here is
  2027. * necessary as some PHYs raise hotplug events after SRST.
  2028. * This introduces race condition where hotplug occurs between
  2029. * reset and here. This race is mediated by cross checking
  2030. * link onlineness and classification result later.
  2031. */
  2032. if (postreset)
  2033. postreset(link, classes);
  2034. /* clear cached SError */
  2035. spin_lock_irqsave(link->ap->lock, flags);
  2036. link->eh_info.serror = 0;
  2037. spin_unlock_irqrestore(link->ap->lock, flags);
  2038. /* Make sure onlineness and classification result correspond.
  2039. * Hotplug could have happened during reset and some
  2040. * controllers fail to wait while a drive is spinning up after
  2041. * being hotplugged causing misdetection. By cross checking
  2042. * link onlineness and classification result, those conditions
  2043. * can be reliably detected and retried.
  2044. */
  2045. nr_known = 0;
  2046. ata_link_for_each_dev(dev, link) {
  2047. /* convert all ATA_DEV_UNKNOWN to ATA_DEV_NONE */
  2048. if (classes[dev->devno] == ATA_DEV_UNKNOWN)
  2049. classes[dev->devno] = ATA_DEV_NONE;
  2050. else
  2051. nr_known++;
  2052. }
  2053. if (classify && !nr_known && ata_link_online(link)) {
  2054. if (try < max_tries) {
  2055. ata_link_printk(link, KERN_WARNING, "link online but "
  2056. "device misclassified, retrying\n");
  2057. rc = -EAGAIN;
  2058. goto fail;
  2059. }
  2060. ata_link_printk(link, KERN_WARNING,
  2061. "link online but device misclassified, "
  2062. "device detection might fail\n");
  2063. }
  2064. /* reset successful, schedule revalidation */
  2065. ata_eh_done(link, NULL, ATA_EH_RESET);
  2066. ehc->last_reset = jiffies;
  2067. ehc->i.action |= ATA_EH_REVALIDATE;
  2068. rc = 0;
  2069. out:
  2070. /* clear hotplug flag */
  2071. ehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
  2072. spin_lock_irqsave(ap->lock, flags);
  2073. ap->pflags &= ~ATA_PFLAG_RESETTING;
  2074. spin_unlock_irqrestore(ap->lock, flags);
  2075. return rc;
  2076. fail:
  2077. /* if SCR isn't accessible on a fan-out port, PMP needs to be reset */
  2078. if (!ata_is_host_link(link) &&
  2079. sata_scr_read(link, SCR_STATUS, &sstatus))
  2080. rc = -ERESTART;
  2081. if (rc == -ERESTART || try >= max_tries)
  2082. goto out;
  2083. now = jiffies;
  2084. if (time_before(now, deadline)) {
  2085. unsigned long delta = deadline - now;
  2086. ata_link_printk(link, KERN_WARNING,
  2087. "reset failed (errno=%d), retrying in %u secs\n",
  2088. rc, DIV_ROUND_UP(jiffies_to_msecs(delta), 1000));
  2089. while (delta)
  2090. delta = schedule_timeout_uninterruptible(delta);
  2091. }
  2092. if (rc == -EPIPE || try == max_tries - 1)
  2093. sata_down_spd_limit(link);
  2094. if (hardreset)
  2095. reset = hardreset;
  2096. goto retry;
  2097. }
  2098. static int ata_eh_revalidate_and_attach(struct ata_link *link,
  2099. struct ata_device **r_failed_dev)
  2100. {
  2101. struct ata_port *ap = link->ap;
  2102. struct ata_eh_context *ehc = &link->eh_context;
  2103. struct ata_device *dev;
  2104. unsigned int new_mask = 0;
  2105. unsigned long flags;
  2106. int rc = 0;
  2107. DPRINTK("ENTER\n");
  2108. /* For PATA drive side cable detection to work, IDENTIFY must
  2109. * be done backwards such that PDIAG- is released by the slave
  2110. * device before the master device is identified.
  2111. */
  2112. ata_link_for_each_dev_reverse(dev, link) {
  2113. unsigned int action = ata_eh_dev_action(dev);
  2114. unsigned int readid_flags = 0;
  2115. if (ehc->i.flags & ATA_EHI_DID_RESET)
  2116. readid_flags |= ATA_READID_POSTRESET;
  2117. if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
  2118. WARN_ON(dev->class == ATA_DEV_PMP);
  2119. if (ata_link_offline(link)) {
  2120. rc = -EIO;
  2121. goto err;
  2122. }
  2123. ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE);
  2124. rc = ata_dev_revalidate(dev, ehc->classes[dev->devno],
  2125. readid_flags);
  2126. if (rc)
  2127. goto err;
  2128. ata_eh_done(link, dev, ATA_EH_REVALIDATE);
  2129. /* Configuration may have changed, reconfigure
  2130. * transfer mode.
  2131. */
  2132. ehc->i.flags |= ATA_EHI_SETMODE;
  2133. /* schedule the scsi_rescan_device() here */
  2134. queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
  2135. } else if (dev->class == ATA_DEV_UNKNOWN &&
  2136. ehc->tries[dev->devno] &&
  2137. ata_class_enabled(ehc->classes[dev->devno])) {
  2138. dev->class = ehc->classes[dev->devno];
  2139. if (dev->class == ATA_DEV_PMP)
  2140. rc = sata_pmp_attach(dev);
  2141. else
  2142. rc = ata_dev_read_id(dev, &dev->class,
  2143. readid_flags, dev->id);
  2144. switch (rc) {
  2145. case 0:
  2146. new_mask |= 1 << dev->devno;
  2147. break;
  2148. case -ENOENT:
  2149. /* IDENTIFY was issued to non-existent
  2150. * device. No need to reset. Just
  2151. * thaw and kill the device.
  2152. */
  2153. ata_eh_thaw_port(ap);
  2154. dev->class = ATA_DEV_UNKNOWN;
  2155. break;
  2156. default:
  2157. dev->class = ATA_DEV_UNKNOWN;
  2158. goto err;
  2159. }
  2160. }
  2161. }
  2162. /* PDIAG- should have been released, ask cable type if post-reset */
  2163. if ((ehc->i.flags & ATA_EHI_DID_RESET) && ata_is_host_link(link)) {
  2164. if (ap->ops->cable_detect)
  2165. ap->cbl = ap->ops->cable_detect(ap);
  2166. ata_force_cbl(ap);
  2167. }
  2168. /* Configure new devices forward such that user doesn't see
  2169. * device detection messages backwards.
  2170. */
  2171. ata_link_for_each_dev(dev, link) {
  2172. if (!(new_mask & (1 << dev->devno)) ||
  2173. dev->class == ATA_DEV_PMP)
  2174. continue;
  2175. ehc->i.flags |= ATA_EHI_PRINTINFO;
  2176. rc = ata_dev_configure(dev);
  2177. ehc->i.flags &= ~ATA_EHI_PRINTINFO;
  2178. if (rc)
  2179. goto err;
  2180. spin_lock_irqsave(ap->lock, flags);
  2181. ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
  2182. spin_unlock_irqrestore(ap->lock, flags);
  2183. /* new device discovered, configure xfermode */
  2184. ehc->i.flags |= ATA_EHI_SETMODE;
  2185. }
  2186. return 0;
  2187. err:
  2188. *r_failed_dev = dev;
  2189. DPRINTK("EXIT rc=%d\n", rc);
  2190. return rc;
  2191. }
  2192. /**
  2193. * ata_set_mode - Program timings and issue SET FEATURES - XFER
  2194. * @link: link on which timings will be programmed
  2195. * @r_failed_dev: out paramter for failed device
  2196. *
  2197. * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
  2198. * ata_set_mode() fails, pointer to the failing device is
  2199. * returned in @r_failed_dev.
  2200. *
  2201. * LOCKING:
  2202. * PCI/etc. bus probe sem.
  2203. *
  2204. * RETURNS:
  2205. * 0 on success, negative errno otherwise
  2206. */
  2207. int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
  2208. {
  2209. struct ata_port *ap = link->ap;
  2210. struct ata_device *dev;
  2211. int rc;
  2212. /* if data transfer is verified, clear DUBIOUS_XFER on ering top */
  2213. ata_link_for_each_dev(dev, link) {
  2214. if (!(dev->flags & ATA_DFLAG_DUBIOUS_XFER)) {
  2215. struct ata_ering_entry *ent;
  2216. ent = ata_ering_top(&dev->ering);
  2217. if (ent)
  2218. ent->eflags &= ~ATA_EFLAG_DUBIOUS_XFER;
  2219. }
  2220. }
  2221. /* has private set_mode? */
  2222. if (ap->ops->set_mode)
  2223. rc = ap->ops->set_mode(link, r_failed_dev);
  2224. else
  2225. rc = ata_do_set_mode(link, r_failed_dev);
  2226. /* if transfer mode has changed, set DUBIOUS_XFER on device */
  2227. ata_link_for_each_dev(dev, link) {
  2228. struct ata_eh_context *ehc = &link->eh_context;
  2229. u8 saved_xfer_mode = ehc->saved_xfer_mode[dev->devno];
  2230. u8 saved_ncq = !!(ehc->saved_ncq_enabled & (1 << dev->devno));
  2231. if (dev->xfer_mode != saved_xfer_mode ||
  2232. ata_ncq_enabled(dev) != saved_ncq)
  2233. dev->flags |= ATA_DFLAG_DUBIOUS_XFER;
  2234. }
  2235. return rc;
  2236. }
  2237. static int ata_link_nr_enabled(struct ata_link *link)
  2238. {
  2239. struct ata_device *dev;
  2240. int cnt = 0;
  2241. ata_link_for_each_dev(dev, link)
  2242. if (ata_dev_enabled(dev))
  2243. cnt++;
  2244. return cnt;
  2245. }
  2246. static int ata_link_nr_vacant(struct ata_link *link)
  2247. {
  2248. struct ata_device *dev;
  2249. int cnt = 0;
  2250. ata_link_for_each_dev(dev, link)
  2251. if (dev->class == ATA_DEV_UNKNOWN)
  2252. cnt++;
  2253. return cnt;
  2254. }
  2255. static int ata_eh_skip_recovery(struct ata_link *link)
  2256. {
  2257. struct ata_port *ap = link->ap;
  2258. struct ata_eh_context *ehc = &link->eh_context;
  2259. struct ata_device *dev;
  2260. /* skip disabled links */
  2261. if (link->flags & ATA_LFLAG_DISABLED)
  2262. return 1;
  2263. /* thaw frozen port and recover failed devices */
  2264. if ((ap->pflags & ATA_PFLAG_FROZEN) || ata_link_nr_enabled(link))
  2265. return 0;
  2266. /* reset at least once if reset is requested */
  2267. if ((ehc->i.action & ATA_EH_RESET) &&
  2268. !(ehc->i.flags & ATA_EHI_DID_RESET))
  2269. return 0;
  2270. /* skip if class codes for all vacant slots are ATA_DEV_NONE */
  2271. ata_link_for_each_dev(dev, link) {
  2272. if (dev->class == ATA_DEV_UNKNOWN &&
  2273. ehc->classes[dev->devno] != ATA_DEV_NONE)
  2274. return 0;
  2275. }
  2276. return 1;
  2277. }
  2278. static int ata_eh_schedule_probe(struct ata_device *dev)
  2279. {
  2280. struct ata_eh_context *ehc = &dev->link->eh_context;
  2281. if (!(ehc->i.probe_mask & (1 << dev->devno)) ||
  2282. (ehc->did_probe_mask & (1 << dev->devno)))
  2283. return 0;
  2284. ata_eh_detach_dev(dev);
  2285. ata_dev_init(dev);
  2286. ehc->did_probe_mask |= (1 << dev->devno);
  2287. ehc->i.action |= ATA_EH_RESET;
  2288. ehc->saved_xfer_mode[dev->devno] = 0;
  2289. ehc->saved_ncq_enabled &= ~(1 << dev->devno);
  2290. return 1;
  2291. }
  2292. static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
  2293. {
  2294. struct ata_eh_context *ehc = &dev->link->eh_context;
  2295. ehc->tries[dev->devno]--;
  2296. switch (err) {
  2297. case -ENODEV:
  2298. /* device missing or wrong IDENTIFY data, schedule probing */
  2299. ehc->i.probe_mask |= (1 << dev->devno);
  2300. case -EINVAL:
  2301. /* give it just one more chance */
  2302. ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
  2303. case -EIO:
  2304. if (ehc->tries[dev->devno] == 1 && dev->pio_mode > XFER_PIO_0) {
  2305. /* This is the last chance, better to slow
  2306. * down than lose it.
  2307. */
  2308. sata_down_spd_limit(dev->link);
  2309. ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
  2310. }
  2311. }
  2312. if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
  2313. /* disable device if it has used up all its chances */
  2314. ata_dev_disable(dev);
  2315. /* detach if offline */
  2316. if (ata_link_offline(dev->link))
  2317. ata_eh_detach_dev(dev);
  2318. /* schedule probe if necessary */
  2319. if (ata_eh_schedule_probe(dev)) {
  2320. ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
  2321. memset(ehc->cmd_timeout_idx[dev->devno], 0,
  2322. sizeof(ehc->cmd_timeout_idx[dev->devno]));
  2323. }
  2324. return 1;
  2325. } else {
  2326. ehc->i.action |= ATA_EH_RESET;
  2327. return 0;
  2328. }
  2329. }
  2330. /**
  2331. * ata_eh_recover - recover host port after error
  2332. * @ap: host port to recover
  2333. * @prereset: prereset method (can be NULL)
  2334. * @softreset: softreset method (can be NULL)
  2335. * @hardreset: hardreset method (can be NULL)
  2336. * @postreset: postreset method (can be NULL)
  2337. * @r_failed_link: out parameter for failed link
  2338. *
  2339. * This is the alpha and omega, eum and yang, heart and soul of
  2340. * libata exception handling. On entry, actions required to
  2341. * recover each link and hotplug requests are recorded in the
  2342. * link's eh_context. This function executes all the operations
  2343. * with appropriate retrials and fallbacks to resurrect failed
  2344. * devices, detach goners and greet newcomers.
  2345. *
  2346. * LOCKING:
  2347. * Kernel thread context (may sleep).
  2348. *
  2349. * RETURNS:
  2350. * 0 on success, -errno on failure.
  2351. */
  2352. int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
  2353. ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
  2354. ata_postreset_fn_t postreset,
  2355. struct ata_link **r_failed_link)
  2356. {
  2357. struct ata_link *link;
  2358. struct ata_device *dev;
  2359. int nr_failed_devs;
  2360. int rc;
  2361. unsigned long flags;
  2362. DPRINTK("ENTER\n");
  2363. /* prep for recovery */
  2364. ata_port_for_each_link(link, ap) {
  2365. struct ata_eh_context *ehc = &link->eh_context;
  2366. /* re-enable link? */
  2367. if (ehc->i.action & ATA_EH_ENABLE_LINK) {
  2368. ata_eh_about_to_do(link, NULL, ATA_EH_ENABLE_LINK);
  2369. spin_lock_irqsave(ap->lock, flags);
  2370. link->flags &= ~ATA_LFLAG_DISABLED;
  2371. spin_unlock_irqrestore(ap->lock, flags);
  2372. ata_eh_done(link, NULL, ATA_EH_ENABLE_LINK);
  2373. }
  2374. ata_link_for_each_dev(dev, link) {
  2375. if (link->flags & ATA_LFLAG_NO_RETRY)
  2376. ehc->tries[dev->devno] = 1;
  2377. else
  2378. ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
  2379. /* collect port action mask recorded in dev actions */
  2380. ehc->i.action |= ehc->i.dev_action[dev->devno] &
  2381. ~ATA_EH_PERDEV_MASK;
  2382. ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK;
  2383. /* process hotplug request */
  2384. if (dev->flags & ATA_DFLAG_DETACH)
  2385. ata_eh_detach_dev(dev);
  2386. /* schedule probe if necessary */
  2387. if (!ata_dev_enabled(dev))
  2388. ata_eh_schedule_probe(dev);
  2389. }
  2390. }
  2391. retry:
  2392. rc = 0;
  2393. nr_failed_devs = 0;
  2394. /* if UNLOADING, finish immediately */
  2395. if (ap->pflags & ATA_PFLAG_UNLOADING)
  2396. goto out;
  2397. /* prep for EH */
  2398. ata_port_for_each_link(link, ap) {
  2399. struct ata_eh_context *ehc = &link->eh_context;
  2400. /* skip EH if possible. */
  2401. if (ata_eh_skip_recovery(link))
  2402. ehc->i.action = 0;
  2403. ata_link_for_each_dev(dev, link)
  2404. ehc->classes[dev->devno] = ATA_DEV_UNKNOWN;
  2405. }
  2406. /* reset */
  2407. ata_port_for_each_link(link, ap) {
  2408. struct ata_eh_context *ehc = &link->eh_context;
  2409. if (!(ehc->i.action & ATA_EH_RESET))
  2410. continue;
  2411. rc = ata_eh_reset(link, ata_link_nr_vacant(link),
  2412. prereset, softreset, hardreset, postreset);
  2413. if (rc) {
  2414. ata_link_printk(link, KERN_ERR,
  2415. "reset failed, giving up\n");
  2416. goto out;
  2417. }
  2418. }
  2419. /* the rest */
  2420. ata_port_for_each_link(link, ap) {
  2421. struct ata_eh_context *ehc = &link->eh_context;
  2422. /* revalidate existing devices and attach new ones */
  2423. rc = ata_eh_revalidate_and_attach(link, &dev);
  2424. if (rc)
  2425. goto dev_fail;
  2426. /* if PMP got attached, return, pmp EH will take care of it */
  2427. if (link->device->class == ATA_DEV_PMP) {
  2428. ehc->i.action = 0;
  2429. return 0;
  2430. }
  2431. /* configure transfer mode if necessary */
  2432. if (ehc->i.flags & ATA_EHI_SETMODE) {
  2433. rc = ata_set_mode(link, &dev);
  2434. if (rc)
  2435. goto dev_fail;
  2436. ehc->i.flags &= ~ATA_EHI_SETMODE;
  2437. }
  2438. if (ehc->i.action & ATA_EH_LPM)
  2439. ata_link_for_each_dev(dev, link)
  2440. ata_dev_enable_pm(dev, ap->pm_policy);
  2441. /* this link is okay now */
  2442. ehc->i.flags = 0;
  2443. continue;
  2444. dev_fail:
  2445. nr_failed_devs++;
  2446. ata_eh_handle_dev_fail(dev, rc);
  2447. if (ap->pflags & ATA_PFLAG_FROZEN) {
  2448. /* PMP reset requires working host port.
  2449. * Can't retry if it's frozen.
  2450. */
  2451. if (sata_pmp_attached(ap))
  2452. goto out;
  2453. break;
  2454. }
  2455. }
  2456. if (nr_failed_devs)
  2457. goto retry;
  2458. out:
  2459. if (rc && r_failed_link)
  2460. *r_failed_link = link;
  2461. DPRINTK("EXIT, rc=%d\n", rc);
  2462. return rc;
  2463. }
  2464. /**
  2465. * ata_eh_finish - finish up EH
  2466. * @ap: host port to finish EH for
  2467. *
  2468. * Recovery is complete. Clean up EH states and retry or finish
  2469. * failed qcs.
  2470. *
  2471. * LOCKING:
  2472. * None.
  2473. */
  2474. void ata_eh_finish(struct ata_port *ap)
  2475. {
  2476. int tag;
  2477. /* retry or finish qcs */
  2478. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  2479. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  2480. if (!(qc->flags & ATA_QCFLAG_FAILED))
  2481. continue;
  2482. if (qc->err_mask) {
  2483. /* FIXME: Once EH migration is complete,
  2484. * generate sense data in this function,
  2485. * considering both err_mask and tf.
  2486. */
  2487. if (qc->flags & ATA_QCFLAG_RETRY)
  2488. ata_eh_qc_retry(qc);
  2489. else
  2490. ata_eh_qc_complete(qc);
  2491. } else {
  2492. if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
  2493. ata_eh_qc_complete(qc);
  2494. } else {
  2495. /* feed zero TF to sense generation */
  2496. memset(&qc->result_tf, 0, sizeof(qc->result_tf));
  2497. ata_eh_qc_retry(qc);
  2498. }
  2499. }
  2500. }
  2501. /* make sure nr_active_links is zero after EH */
  2502. WARN_ON(ap->nr_active_links);
  2503. ap->nr_active_links = 0;
  2504. }
  2505. /**
  2506. * ata_do_eh - do standard error handling
  2507. * @ap: host port to handle error for
  2508. *
  2509. * @prereset: prereset method (can be NULL)
  2510. * @softreset: softreset method (can be NULL)
  2511. * @hardreset: hardreset method (can be NULL)
  2512. * @postreset: postreset method (can be NULL)
  2513. *
  2514. * Perform standard error handling sequence.
  2515. *
  2516. * LOCKING:
  2517. * Kernel thread context (may sleep).
  2518. */
  2519. void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
  2520. ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
  2521. ata_postreset_fn_t postreset)
  2522. {
  2523. struct ata_device *dev;
  2524. int rc;
  2525. ata_eh_autopsy(ap);
  2526. ata_eh_report(ap);
  2527. rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset,
  2528. NULL);
  2529. if (rc) {
  2530. ata_link_for_each_dev(dev, &ap->link)
  2531. ata_dev_disable(dev);
  2532. }
  2533. ata_eh_finish(ap);
  2534. }
  2535. /**
  2536. * ata_std_error_handler - standard error handler
  2537. * @ap: host port to handle error for
  2538. *
  2539. * Standard error handler
  2540. *
  2541. * LOCKING:
  2542. * Kernel thread context (may sleep).
  2543. */
  2544. void ata_std_error_handler(struct ata_port *ap)
  2545. {
  2546. struct ata_port_operations *ops = ap->ops;
  2547. ata_reset_fn_t hardreset = ops->hardreset;
  2548. /* ignore built-in hardreset if SCR access is not available */
  2549. if (ata_is_builtin_hardreset(hardreset) && !sata_scr_valid(&ap->link))
  2550. hardreset = NULL;
  2551. ata_do_eh(ap, ops->prereset, ops->softreset, hardreset, ops->postreset);
  2552. }
  2553. #ifdef CONFIG_PM
  2554. /**
  2555. * ata_eh_handle_port_suspend - perform port suspend operation
  2556. * @ap: port to suspend
  2557. *
  2558. * Suspend @ap.
  2559. *
  2560. * LOCKING:
  2561. * Kernel thread context (may sleep).
  2562. */
  2563. static void ata_eh_handle_port_suspend(struct ata_port *ap)
  2564. {
  2565. unsigned long flags;
  2566. int rc = 0;
  2567. /* are we suspending? */
  2568. spin_lock_irqsave(ap->lock, flags);
  2569. if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
  2570. ap->pm_mesg.event == PM_EVENT_ON) {
  2571. spin_unlock_irqrestore(ap->lock, flags);
  2572. return;
  2573. }
  2574. spin_unlock_irqrestore(ap->lock, flags);
  2575. WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
  2576. /* tell ACPI we're suspending */
  2577. rc = ata_acpi_on_suspend(ap);
  2578. if (rc)
  2579. goto out;
  2580. /* suspend */
  2581. ata_eh_freeze_port(ap);
  2582. if (ap->ops->port_suspend)
  2583. rc = ap->ops->port_suspend(ap, ap->pm_mesg);
  2584. ata_acpi_set_state(ap, PMSG_SUSPEND);
  2585. out:
  2586. /* report result */
  2587. spin_lock_irqsave(ap->lock, flags);
  2588. ap->pflags &= ~ATA_PFLAG_PM_PENDING;
  2589. if (rc == 0)
  2590. ap->pflags |= ATA_PFLAG_SUSPENDED;
  2591. else if (ap->pflags & ATA_PFLAG_FROZEN)
  2592. ata_port_schedule_eh(ap);
  2593. if (ap->pm_result) {
  2594. *ap->pm_result = rc;
  2595. ap->pm_result = NULL;
  2596. }
  2597. spin_unlock_irqrestore(ap->lock, flags);
  2598. return;
  2599. }
  2600. /**
  2601. * ata_eh_handle_port_resume - perform port resume operation
  2602. * @ap: port to resume
  2603. *
  2604. * Resume @ap.
  2605. *
  2606. * LOCKING:
  2607. * Kernel thread context (may sleep).
  2608. */
  2609. static void ata_eh_handle_port_resume(struct ata_port *ap)
  2610. {
  2611. unsigned long flags;
  2612. int rc = 0;
  2613. /* are we resuming? */
  2614. spin_lock_irqsave(ap->lock, flags);
  2615. if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
  2616. ap->pm_mesg.event != PM_EVENT_ON) {
  2617. spin_unlock_irqrestore(ap->lock, flags);
  2618. return;
  2619. }
  2620. spin_unlock_irqrestore(ap->lock, flags);
  2621. WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
  2622. ata_acpi_set_state(ap, PMSG_ON);
  2623. if (ap->ops->port_resume)
  2624. rc = ap->ops->port_resume(ap);
  2625. /* tell ACPI that we're resuming */
  2626. ata_acpi_on_resume(ap);
  2627. /* report result */
  2628. spin_lock_irqsave(ap->lock, flags);
  2629. ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
  2630. if (ap->pm_result) {
  2631. *ap->pm_result = rc;
  2632. ap->pm_result = NULL;
  2633. }
  2634. spin_unlock_irqrestore(ap->lock, flags);
  2635. }
  2636. #endif /* CONFIG_PM */