libata-eh.c 99 KB

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