libata-eh.c 112 KB

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