libata-eh.c 104 KB

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