libata-eh.c 86 KB

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