libata-eh.c 99 KB

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