libata-eh.c 112 KB

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