libata-eh.c 86 KB

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