libata-eh.c 78 KB

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