libata-eh.c 99 KB

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