libata-eh.c 96 KB

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