libata-eh.c 91 KB

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