libata-eh.c 77 KB

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