libata-eh.c 90 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554
  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.
  845. *
  846. * LOCKING:
  847. * spin_lock_irqsave(host lock)
  848. *
  849. * RETURNS:
  850. * Number of aborted commands.
  851. */
  852. int ata_port_freeze(struct ata_port *ap)
  853. {
  854. int nr_aborted;
  855. WARN_ON(!ap->ops->error_handler);
  856. nr_aborted = ata_port_abort(ap);
  857. __ata_port_freeze(ap);
  858. return nr_aborted;
  859. }
  860. /**
  861. * sata_async_notification - SATA async notification handler
  862. * @ap: ATA port where async notification is received
  863. *
  864. * Handler to be called when async notification via SDB FIS is
  865. * received. This function schedules EH if necessary.
  866. *
  867. * LOCKING:
  868. * spin_lock_irqsave(host lock)
  869. *
  870. * RETURNS:
  871. * 1 if EH is scheduled, 0 otherwise.
  872. */
  873. int sata_async_notification(struct ata_port *ap)
  874. {
  875. u32 sntf;
  876. int rc;
  877. if (!(ap->flags & ATA_FLAG_AN))
  878. return 0;
  879. rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
  880. if (rc == 0)
  881. sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
  882. if (!sata_pmp_attached(ap) || rc) {
  883. /* PMP is not attached or SNTF is not available */
  884. if (!sata_pmp_attached(ap)) {
  885. /* PMP is not attached. Check whether ATAPI
  886. * AN is configured. If so, notify media
  887. * change.
  888. */
  889. struct ata_device *dev = ap->link.device;
  890. if ((dev->class == ATA_DEV_ATAPI) &&
  891. (dev->flags & ATA_DFLAG_AN))
  892. ata_scsi_media_change_notify(dev);
  893. return 0;
  894. } else {
  895. /* PMP is attached but SNTF is not available.
  896. * ATAPI async media change notification is
  897. * not used. The PMP must be reporting PHY
  898. * status change, schedule EH.
  899. */
  900. ata_port_schedule_eh(ap);
  901. return 1;
  902. }
  903. } else {
  904. /* PMP is attached and SNTF is available */
  905. struct ata_link *link;
  906. /* check and notify ATAPI AN */
  907. ata_for_each_link(link, ap, EDGE) {
  908. if (!(sntf & (1 << link->pmp)))
  909. continue;
  910. if ((link->device->class == ATA_DEV_ATAPI) &&
  911. (link->device->flags & ATA_DFLAG_AN))
  912. ata_scsi_media_change_notify(link->device);
  913. }
  914. /* If PMP is reporting that PHY status of some
  915. * downstream ports has changed, schedule EH.
  916. */
  917. if (sntf & (1 << SATA_PMP_CTRL_PORT)) {
  918. ata_port_schedule_eh(ap);
  919. return 1;
  920. }
  921. return 0;
  922. }
  923. }
  924. /**
  925. * ata_eh_freeze_port - EH helper to freeze port
  926. * @ap: ATA port to freeze
  927. *
  928. * Freeze @ap.
  929. *
  930. * LOCKING:
  931. * None.
  932. */
  933. void ata_eh_freeze_port(struct ata_port *ap)
  934. {
  935. unsigned long flags;
  936. if (!ap->ops->error_handler)
  937. return;
  938. spin_lock_irqsave(ap->lock, flags);
  939. __ata_port_freeze(ap);
  940. spin_unlock_irqrestore(ap->lock, flags);
  941. }
  942. /**
  943. * ata_port_thaw_port - EH helper to thaw port
  944. * @ap: ATA port to thaw
  945. *
  946. * Thaw frozen port @ap.
  947. *
  948. * LOCKING:
  949. * None.
  950. */
  951. void ata_eh_thaw_port(struct ata_port *ap)
  952. {
  953. unsigned long flags;
  954. if (!ap->ops->error_handler)
  955. return;
  956. spin_lock_irqsave(ap->lock, flags);
  957. ap->pflags &= ~ATA_PFLAG_FROZEN;
  958. if (ap->ops->thaw)
  959. ap->ops->thaw(ap);
  960. spin_unlock_irqrestore(ap->lock, flags);
  961. DPRINTK("ata%u port thawed\n", ap->print_id);
  962. }
  963. static void ata_eh_scsidone(struct scsi_cmnd *scmd)
  964. {
  965. /* nada */
  966. }
  967. static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
  968. {
  969. struct ata_port *ap = qc->ap;
  970. struct scsi_cmnd *scmd = qc->scsicmd;
  971. unsigned long flags;
  972. spin_lock_irqsave(ap->lock, flags);
  973. qc->scsidone = ata_eh_scsidone;
  974. __ata_qc_complete(qc);
  975. WARN_ON(ata_tag_valid(qc->tag));
  976. spin_unlock_irqrestore(ap->lock, flags);
  977. scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
  978. }
  979. /**
  980. * ata_eh_qc_complete - Complete an active ATA command from EH
  981. * @qc: Command to complete
  982. *
  983. * Indicate to the mid and upper layers that an ATA command has
  984. * completed. To be used from EH.
  985. */
  986. void ata_eh_qc_complete(struct ata_queued_cmd *qc)
  987. {
  988. struct scsi_cmnd *scmd = qc->scsicmd;
  989. scmd->retries = scmd->allowed;
  990. __ata_eh_qc_complete(qc);
  991. }
  992. /**
  993. * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
  994. * @qc: Command to retry
  995. *
  996. * Indicate to the mid and upper layers that an ATA command
  997. * should be retried. To be used from EH.
  998. *
  999. * SCSI midlayer limits the number of retries to scmd->allowed.
  1000. * scmd->retries is decremented for commands which get retried
  1001. * due to unrelated failures (qc->err_mask is zero).
  1002. */
  1003. void ata_eh_qc_retry(struct ata_queued_cmd *qc)
  1004. {
  1005. struct scsi_cmnd *scmd = qc->scsicmd;
  1006. if (!qc->err_mask && scmd->retries)
  1007. scmd->retries--;
  1008. __ata_eh_qc_complete(qc);
  1009. }
  1010. /**
  1011. * ata_dev_disable - disable ATA device
  1012. * @dev: ATA device to disable
  1013. *
  1014. * Disable @dev.
  1015. *
  1016. * Locking:
  1017. * EH context.
  1018. */
  1019. void ata_dev_disable(struct ata_device *dev)
  1020. {
  1021. if (!ata_dev_enabled(dev))
  1022. return;
  1023. if (ata_msg_drv(dev->link->ap))
  1024. ata_dev_printk(dev, KERN_WARNING, "disabled\n");
  1025. ata_acpi_on_disable(dev);
  1026. ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO0 | ATA_DNXFER_QUIET);
  1027. dev->class++;
  1028. /* From now till the next successful probe, ering is used to
  1029. * track probe failures. Clear accumulated device error info.
  1030. */
  1031. ata_ering_clear(&dev->ering);
  1032. }
  1033. /**
  1034. * ata_eh_detach_dev - detach ATA device
  1035. * @dev: ATA device to detach
  1036. *
  1037. * Detach @dev.
  1038. *
  1039. * LOCKING:
  1040. * None.
  1041. */
  1042. void ata_eh_detach_dev(struct ata_device *dev)
  1043. {
  1044. struct ata_link *link = dev->link;
  1045. struct ata_port *ap = link->ap;
  1046. struct ata_eh_context *ehc = &link->eh_context;
  1047. unsigned long flags;
  1048. ata_dev_disable(dev);
  1049. spin_lock_irqsave(ap->lock, flags);
  1050. dev->flags &= ~ATA_DFLAG_DETACH;
  1051. if (ata_scsi_offline_dev(dev)) {
  1052. dev->flags |= ATA_DFLAG_DETACHED;
  1053. ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
  1054. }
  1055. /* clear per-dev EH info */
  1056. ata_eh_clear_action(link, dev, &link->eh_info, ATA_EH_PERDEV_MASK);
  1057. ata_eh_clear_action(link, dev, &link->eh_context.i, ATA_EH_PERDEV_MASK);
  1058. ehc->saved_xfer_mode[dev->devno] = 0;
  1059. ehc->saved_ncq_enabled &= ~(1 << dev->devno);
  1060. spin_unlock_irqrestore(ap->lock, flags);
  1061. }
  1062. /**
  1063. * ata_eh_about_to_do - about to perform eh_action
  1064. * @link: target ATA link
  1065. * @dev: target ATA dev for per-dev action (can be NULL)
  1066. * @action: action about to be performed
  1067. *
  1068. * Called just before performing EH actions to clear related bits
  1069. * in @link->eh_info such that eh actions are not unnecessarily
  1070. * repeated.
  1071. *
  1072. * LOCKING:
  1073. * None.
  1074. */
  1075. void ata_eh_about_to_do(struct ata_link *link, struct ata_device *dev,
  1076. unsigned int action)
  1077. {
  1078. struct ata_port *ap = link->ap;
  1079. struct ata_eh_info *ehi = &link->eh_info;
  1080. struct ata_eh_context *ehc = &link->eh_context;
  1081. unsigned long flags;
  1082. spin_lock_irqsave(ap->lock, flags);
  1083. ata_eh_clear_action(link, dev, ehi, action);
  1084. /* About to take EH action, set RECOVERED. Ignore actions on
  1085. * slave links as master will do them again.
  1086. */
  1087. if (!(ehc->i.flags & ATA_EHI_QUIET) && link != ap->slave_link)
  1088. ap->pflags |= ATA_PFLAG_RECOVERED;
  1089. spin_unlock_irqrestore(ap->lock, flags);
  1090. }
  1091. /**
  1092. * ata_eh_done - EH action complete
  1093. * @ap: target ATA port
  1094. * @dev: target ATA dev for per-dev action (can be NULL)
  1095. * @action: action just completed
  1096. *
  1097. * Called right after performing EH actions to clear related bits
  1098. * in @link->eh_context.
  1099. *
  1100. * LOCKING:
  1101. * None.
  1102. */
  1103. void ata_eh_done(struct ata_link *link, struct ata_device *dev,
  1104. unsigned int action)
  1105. {
  1106. struct ata_eh_context *ehc = &link->eh_context;
  1107. ata_eh_clear_action(link, dev, &ehc->i, action);
  1108. }
  1109. /**
  1110. * ata_err_string - convert err_mask to descriptive string
  1111. * @err_mask: error mask to convert to string
  1112. *
  1113. * Convert @err_mask to descriptive string. Errors are
  1114. * prioritized according to severity and only the most severe
  1115. * error is reported.
  1116. *
  1117. * LOCKING:
  1118. * None.
  1119. *
  1120. * RETURNS:
  1121. * Descriptive string for @err_mask
  1122. */
  1123. static const char *ata_err_string(unsigned int err_mask)
  1124. {
  1125. if (err_mask & AC_ERR_HOST_BUS)
  1126. return "host bus error";
  1127. if (err_mask & AC_ERR_ATA_BUS)
  1128. return "ATA bus error";
  1129. if (err_mask & AC_ERR_TIMEOUT)
  1130. return "timeout";
  1131. if (err_mask & AC_ERR_HSM)
  1132. return "HSM violation";
  1133. if (err_mask & AC_ERR_SYSTEM)
  1134. return "internal error";
  1135. if (err_mask & AC_ERR_MEDIA)
  1136. return "media error";
  1137. if (err_mask & AC_ERR_INVALID)
  1138. return "invalid argument";
  1139. if (err_mask & AC_ERR_DEV)
  1140. return "device error";
  1141. return "unknown error";
  1142. }
  1143. /**
  1144. * ata_read_log_page - read a specific log page
  1145. * @dev: target device
  1146. * @page: page to read
  1147. * @buf: buffer to store read page
  1148. * @sectors: number of sectors to read
  1149. *
  1150. * Read log page using READ_LOG_EXT command.
  1151. *
  1152. * LOCKING:
  1153. * Kernel thread context (may sleep).
  1154. *
  1155. * RETURNS:
  1156. * 0 on success, AC_ERR_* mask otherwise.
  1157. */
  1158. static unsigned int ata_read_log_page(struct ata_device *dev,
  1159. u8 page, void *buf, unsigned int sectors)
  1160. {
  1161. struct ata_taskfile tf;
  1162. unsigned int err_mask;
  1163. DPRINTK("read log page - page %d\n", page);
  1164. ata_tf_init(dev, &tf);
  1165. tf.command = ATA_CMD_READ_LOG_EXT;
  1166. tf.lbal = page;
  1167. tf.nsect = sectors;
  1168. tf.hob_nsect = sectors >> 8;
  1169. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
  1170. tf.protocol = ATA_PROT_PIO;
  1171. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
  1172. buf, sectors * ATA_SECT_SIZE, 0);
  1173. DPRINTK("EXIT, err_mask=%x\n", err_mask);
  1174. return err_mask;
  1175. }
  1176. /**
  1177. * ata_eh_read_log_10h - Read log page 10h for NCQ error details
  1178. * @dev: Device to read log page 10h from
  1179. * @tag: Resulting tag of the failed command
  1180. * @tf: Resulting taskfile registers of the failed command
  1181. *
  1182. * Read log page 10h to obtain NCQ error details and clear error
  1183. * condition.
  1184. *
  1185. * LOCKING:
  1186. * Kernel thread context (may sleep).
  1187. *
  1188. * RETURNS:
  1189. * 0 on success, -errno otherwise.
  1190. */
  1191. static int ata_eh_read_log_10h(struct ata_device *dev,
  1192. int *tag, struct ata_taskfile *tf)
  1193. {
  1194. u8 *buf = dev->link->ap->sector_buf;
  1195. unsigned int err_mask;
  1196. u8 csum;
  1197. int i;
  1198. err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
  1199. if (err_mask)
  1200. return -EIO;
  1201. csum = 0;
  1202. for (i = 0; i < ATA_SECT_SIZE; i++)
  1203. csum += buf[i];
  1204. if (csum)
  1205. ata_dev_printk(dev, KERN_WARNING,
  1206. "invalid checksum 0x%x on log page 10h\n", csum);
  1207. if (buf[0] & 0x80)
  1208. return -ENOENT;
  1209. *tag = buf[0] & 0x1f;
  1210. tf->command = buf[2];
  1211. tf->feature = buf[3];
  1212. tf->lbal = buf[4];
  1213. tf->lbam = buf[5];
  1214. tf->lbah = buf[6];
  1215. tf->device = buf[7];
  1216. tf->hob_lbal = buf[8];
  1217. tf->hob_lbam = buf[9];
  1218. tf->hob_lbah = buf[10];
  1219. tf->nsect = buf[12];
  1220. tf->hob_nsect = buf[13];
  1221. return 0;
  1222. }
  1223. /**
  1224. * atapi_eh_tur - perform ATAPI TEST_UNIT_READY
  1225. * @dev: target ATAPI device
  1226. * @r_sense_key: out parameter for sense_key
  1227. *
  1228. * Perform ATAPI TEST_UNIT_READY.
  1229. *
  1230. * LOCKING:
  1231. * EH context (may sleep).
  1232. *
  1233. * RETURNS:
  1234. * 0 on success, AC_ERR_* mask on failure.
  1235. */
  1236. static unsigned int atapi_eh_tur(struct ata_device *dev, u8 *r_sense_key)
  1237. {
  1238. u8 cdb[ATAPI_CDB_LEN] = { TEST_UNIT_READY, 0, 0, 0, 0, 0 };
  1239. struct ata_taskfile tf;
  1240. unsigned int err_mask;
  1241. ata_tf_init(dev, &tf);
  1242. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  1243. tf.command = ATA_CMD_PACKET;
  1244. tf.protocol = ATAPI_PROT_NODATA;
  1245. err_mask = ata_exec_internal(dev, &tf, cdb, DMA_NONE, NULL, 0, 0);
  1246. if (err_mask == AC_ERR_DEV)
  1247. *r_sense_key = tf.feature >> 4;
  1248. return err_mask;
  1249. }
  1250. /**
  1251. * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
  1252. * @dev: device to perform REQUEST_SENSE to
  1253. * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
  1254. * @dfl_sense_key: default sense key to use
  1255. *
  1256. * Perform ATAPI REQUEST_SENSE after the device reported CHECK
  1257. * SENSE. This function is EH helper.
  1258. *
  1259. * LOCKING:
  1260. * Kernel thread context (may sleep).
  1261. *
  1262. * RETURNS:
  1263. * 0 on success, AC_ERR_* mask on failure
  1264. */
  1265. static unsigned int atapi_eh_request_sense(struct ata_device *dev,
  1266. u8 *sense_buf, u8 dfl_sense_key)
  1267. {
  1268. u8 cdb[ATAPI_CDB_LEN] =
  1269. { REQUEST_SENSE, 0, 0, 0, SCSI_SENSE_BUFFERSIZE, 0 };
  1270. struct ata_port *ap = dev->link->ap;
  1271. struct ata_taskfile tf;
  1272. DPRINTK("ATAPI request sense\n");
  1273. /* FIXME: is this needed? */
  1274. memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
  1275. /* initialize sense_buf with the error register,
  1276. * for the case where they are -not- overwritten
  1277. */
  1278. sense_buf[0] = 0x70;
  1279. sense_buf[2] = dfl_sense_key;
  1280. /* some devices time out if garbage left in tf */
  1281. ata_tf_init(dev, &tf);
  1282. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  1283. tf.command = ATA_CMD_PACKET;
  1284. /* is it pointless to prefer PIO for "safety reasons"? */
  1285. if (ap->flags & ATA_FLAG_PIO_DMA) {
  1286. tf.protocol = ATAPI_PROT_DMA;
  1287. tf.feature |= ATAPI_PKT_DMA;
  1288. } else {
  1289. tf.protocol = ATAPI_PROT_PIO;
  1290. tf.lbam = SCSI_SENSE_BUFFERSIZE;
  1291. tf.lbah = 0;
  1292. }
  1293. return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
  1294. sense_buf, SCSI_SENSE_BUFFERSIZE, 0);
  1295. }
  1296. /**
  1297. * ata_eh_analyze_serror - analyze SError for a failed port
  1298. * @link: ATA link to analyze SError for
  1299. *
  1300. * Analyze SError if available and further determine cause of
  1301. * failure.
  1302. *
  1303. * LOCKING:
  1304. * None.
  1305. */
  1306. static void ata_eh_analyze_serror(struct ata_link *link)
  1307. {
  1308. struct ata_eh_context *ehc = &link->eh_context;
  1309. u32 serror = ehc->i.serror;
  1310. unsigned int err_mask = 0, action = 0;
  1311. u32 hotplug_mask;
  1312. if (serror & (SERR_PERSISTENT | SERR_DATA)) {
  1313. err_mask |= AC_ERR_ATA_BUS;
  1314. action |= ATA_EH_RESET;
  1315. }
  1316. if (serror & SERR_PROTOCOL) {
  1317. err_mask |= AC_ERR_HSM;
  1318. action |= ATA_EH_RESET;
  1319. }
  1320. if (serror & SERR_INTERNAL) {
  1321. err_mask |= AC_ERR_SYSTEM;
  1322. action |= ATA_EH_RESET;
  1323. }
  1324. /* Determine whether a hotplug event has occurred. Both
  1325. * SError.N/X are considered hotplug events for enabled or
  1326. * host links. For disabled PMP links, only N bit is
  1327. * considered as X bit is left at 1 for link plugging.
  1328. */
  1329. hotplug_mask = 0;
  1330. if (!(link->flags & ATA_LFLAG_DISABLED) || ata_is_host_link(link))
  1331. hotplug_mask = SERR_PHYRDY_CHG | SERR_DEV_XCHG;
  1332. else
  1333. hotplug_mask = SERR_PHYRDY_CHG;
  1334. if (serror & hotplug_mask)
  1335. ata_ehi_hotplugged(&ehc->i);
  1336. ehc->i.err_mask |= err_mask;
  1337. ehc->i.action |= action;
  1338. }
  1339. /**
  1340. * ata_eh_analyze_ncq_error - analyze NCQ error
  1341. * @link: ATA link to analyze NCQ error for
  1342. *
  1343. * Read log page 10h, determine the offending qc and acquire
  1344. * error status TF. For NCQ device errors, all LLDDs have to do
  1345. * is setting AC_ERR_DEV in ehi->err_mask. This function takes
  1346. * care of the rest.
  1347. *
  1348. * LOCKING:
  1349. * Kernel thread context (may sleep).
  1350. */
  1351. void ata_eh_analyze_ncq_error(struct ata_link *link)
  1352. {
  1353. struct ata_port *ap = link->ap;
  1354. struct ata_eh_context *ehc = &link->eh_context;
  1355. struct ata_device *dev = link->device;
  1356. struct ata_queued_cmd *qc;
  1357. struct ata_taskfile tf;
  1358. int tag, rc;
  1359. /* if frozen, we can't do much */
  1360. if (ap->pflags & ATA_PFLAG_FROZEN)
  1361. return;
  1362. /* is it NCQ device error? */
  1363. if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
  1364. return;
  1365. /* has LLDD analyzed already? */
  1366. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  1367. qc = __ata_qc_from_tag(ap, tag);
  1368. if (!(qc->flags & ATA_QCFLAG_FAILED))
  1369. continue;
  1370. if (qc->err_mask)
  1371. return;
  1372. }
  1373. /* okay, this error is ours */
  1374. rc = ata_eh_read_log_10h(dev, &tag, &tf);
  1375. if (rc) {
  1376. ata_link_printk(link, KERN_ERR, "failed to read log page 10h "
  1377. "(errno=%d)\n", rc);
  1378. return;
  1379. }
  1380. if (!(link->sactive & (1 << tag))) {
  1381. ata_link_printk(link, KERN_ERR, "log page 10h reported "
  1382. "inactive tag %d\n", tag);
  1383. return;
  1384. }
  1385. /* we've got the perpetrator, condemn it */
  1386. qc = __ata_qc_from_tag(ap, tag);
  1387. memcpy(&qc->result_tf, &tf, sizeof(tf));
  1388. qc->result_tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
  1389. qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
  1390. ehc->i.err_mask &= ~AC_ERR_DEV;
  1391. }
  1392. /**
  1393. * ata_eh_analyze_tf - analyze taskfile of a failed qc
  1394. * @qc: qc to analyze
  1395. * @tf: Taskfile registers to analyze
  1396. *
  1397. * Analyze taskfile of @qc and further determine cause of
  1398. * failure. This function also requests ATAPI sense data if
  1399. * avaliable.
  1400. *
  1401. * LOCKING:
  1402. * Kernel thread context (may sleep).
  1403. *
  1404. * RETURNS:
  1405. * Determined recovery action
  1406. */
  1407. static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
  1408. const struct ata_taskfile *tf)
  1409. {
  1410. unsigned int tmp, action = 0;
  1411. u8 stat = tf->command, err = tf->feature;
  1412. if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
  1413. qc->err_mask |= AC_ERR_HSM;
  1414. return ATA_EH_RESET;
  1415. }
  1416. if (stat & (ATA_ERR | ATA_DF))
  1417. qc->err_mask |= AC_ERR_DEV;
  1418. else
  1419. return 0;
  1420. switch (qc->dev->class) {
  1421. case ATA_DEV_ATA:
  1422. if (err & ATA_ICRC)
  1423. qc->err_mask |= AC_ERR_ATA_BUS;
  1424. if (err & ATA_UNC)
  1425. qc->err_mask |= AC_ERR_MEDIA;
  1426. if (err & ATA_IDNF)
  1427. qc->err_mask |= AC_ERR_INVALID;
  1428. break;
  1429. case ATA_DEV_ATAPI:
  1430. if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
  1431. tmp = atapi_eh_request_sense(qc->dev,
  1432. qc->scsicmd->sense_buffer,
  1433. qc->result_tf.feature >> 4);
  1434. if (!tmp) {
  1435. /* ATA_QCFLAG_SENSE_VALID is used to
  1436. * tell atapi_qc_complete() that sense
  1437. * data is already valid.
  1438. *
  1439. * TODO: interpret sense data and set
  1440. * appropriate err_mask.
  1441. */
  1442. qc->flags |= ATA_QCFLAG_SENSE_VALID;
  1443. } else
  1444. qc->err_mask |= tmp;
  1445. }
  1446. }
  1447. if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
  1448. action |= ATA_EH_RESET;
  1449. return action;
  1450. }
  1451. static int ata_eh_categorize_error(unsigned int eflags, unsigned int err_mask,
  1452. int *xfer_ok)
  1453. {
  1454. int base = 0;
  1455. if (!(eflags & ATA_EFLAG_DUBIOUS_XFER))
  1456. *xfer_ok = 1;
  1457. if (!*xfer_ok)
  1458. base = ATA_ECAT_DUBIOUS_NONE;
  1459. if (err_mask & AC_ERR_ATA_BUS)
  1460. return base + ATA_ECAT_ATA_BUS;
  1461. if (err_mask & AC_ERR_TIMEOUT)
  1462. return base + ATA_ECAT_TOUT_HSM;
  1463. if (eflags & ATA_EFLAG_IS_IO) {
  1464. if (err_mask & AC_ERR_HSM)
  1465. return base + ATA_ECAT_TOUT_HSM;
  1466. if ((err_mask &
  1467. (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
  1468. return base + ATA_ECAT_UNK_DEV;
  1469. }
  1470. return 0;
  1471. }
  1472. struct speed_down_verdict_arg {
  1473. u64 since;
  1474. int xfer_ok;
  1475. int nr_errors[ATA_ECAT_NR];
  1476. };
  1477. static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
  1478. {
  1479. struct speed_down_verdict_arg *arg = void_arg;
  1480. int cat;
  1481. if (ent->timestamp < arg->since)
  1482. return -1;
  1483. cat = ata_eh_categorize_error(ent->eflags, ent->err_mask,
  1484. &arg->xfer_ok);
  1485. arg->nr_errors[cat]++;
  1486. return 0;
  1487. }
  1488. /**
  1489. * ata_eh_speed_down_verdict - Determine speed down verdict
  1490. * @dev: Device of interest
  1491. *
  1492. * This function examines error ring of @dev and determines
  1493. * whether NCQ needs to be turned off, transfer speed should be
  1494. * stepped down, or falling back to PIO is necessary.
  1495. *
  1496. * ECAT_ATA_BUS : ATA_BUS error for any command
  1497. *
  1498. * ECAT_TOUT_HSM : TIMEOUT for any command or HSM violation for
  1499. * IO commands
  1500. *
  1501. * ECAT_UNK_DEV : Unknown DEV error for IO commands
  1502. *
  1503. * ECAT_DUBIOUS_* : Identical to above three but occurred while
  1504. * data transfer hasn't been verified.
  1505. *
  1506. * Verdicts are
  1507. *
  1508. * NCQ_OFF : Turn off NCQ.
  1509. *
  1510. * SPEED_DOWN : Speed down transfer speed but don't fall back
  1511. * to PIO.
  1512. *
  1513. * FALLBACK_TO_PIO : Fall back to PIO.
  1514. *
  1515. * Even if multiple verdicts are returned, only one action is
  1516. * taken per error. An action triggered by non-DUBIOUS errors
  1517. * clears ering, while one triggered by DUBIOUS_* errors doesn't.
  1518. * This is to expedite speed down decisions right after device is
  1519. * initially configured.
  1520. *
  1521. * The followings are speed down rules. #1 and #2 deal with
  1522. * DUBIOUS errors.
  1523. *
  1524. * 1. If more than one DUBIOUS_ATA_BUS or DUBIOUS_TOUT_HSM errors
  1525. * occurred during last 5 mins, SPEED_DOWN and FALLBACK_TO_PIO.
  1526. *
  1527. * 2. If more than one DUBIOUS_TOUT_HSM or DUBIOUS_UNK_DEV errors
  1528. * occurred during last 5 mins, NCQ_OFF.
  1529. *
  1530. * 3. If more than 8 ATA_BUS, TOUT_HSM or UNK_DEV errors
  1531. * ocurred during last 5 mins, FALLBACK_TO_PIO
  1532. *
  1533. * 4. If more than 3 TOUT_HSM or UNK_DEV errors occurred
  1534. * during last 10 mins, NCQ_OFF.
  1535. *
  1536. * 5. If more than 3 ATA_BUS or TOUT_HSM errors, or more than 6
  1537. * UNK_DEV errors occurred during last 10 mins, SPEED_DOWN.
  1538. *
  1539. * LOCKING:
  1540. * Inherited from caller.
  1541. *
  1542. * RETURNS:
  1543. * OR of ATA_EH_SPDN_* flags.
  1544. */
  1545. static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
  1546. {
  1547. const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
  1548. u64 j64 = get_jiffies_64();
  1549. struct speed_down_verdict_arg arg;
  1550. unsigned int verdict = 0;
  1551. /* scan past 5 mins of error history */
  1552. memset(&arg, 0, sizeof(arg));
  1553. arg.since = j64 - min(j64, j5mins);
  1554. ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
  1555. if (arg.nr_errors[ATA_ECAT_DUBIOUS_ATA_BUS] +
  1556. arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] > 1)
  1557. verdict |= ATA_EH_SPDN_SPEED_DOWN |
  1558. ATA_EH_SPDN_FALLBACK_TO_PIO | ATA_EH_SPDN_KEEP_ERRORS;
  1559. if (arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] +
  1560. arg.nr_errors[ATA_ECAT_DUBIOUS_UNK_DEV] > 1)
  1561. verdict |= ATA_EH_SPDN_NCQ_OFF | ATA_EH_SPDN_KEEP_ERRORS;
  1562. if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
  1563. arg.nr_errors[ATA_ECAT_TOUT_HSM] +
  1564. arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
  1565. verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
  1566. /* scan past 10 mins of error history */
  1567. memset(&arg, 0, sizeof(arg));
  1568. arg.since = j64 - min(j64, j10mins);
  1569. ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
  1570. if (arg.nr_errors[ATA_ECAT_TOUT_HSM] +
  1571. arg.nr_errors[ATA_ECAT_UNK_DEV] > 3)
  1572. verdict |= ATA_EH_SPDN_NCQ_OFF;
  1573. if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
  1574. arg.nr_errors[ATA_ECAT_TOUT_HSM] > 3 ||
  1575. arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
  1576. verdict |= ATA_EH_SPDN_SPEED_DOWN;
  1577. return verdict;
  1578. }
  1579. /**
  1580. * ata_eh_speed_down - record error and speed down if necessary
  1581. * @dev: Failed device
  1582. * @eflags: mask of ATA_EFLAG_* flags
  1583. * @err_mask: err_mask of the error
  1584. *
  1585. * Record error and examine error history to determine whether
  1586. * adjusting transmission speed is necessary. It also sets
  1587. * transmission limits appropriately if such adjustment is
  1588. * necessary.
  1589. *
  1590. * LOCKING:
  1591. * Kernel thread context (may sleep).
  1592. *
  1593. * RETURNS:
  1594. * Determined recovery action.
  1595. */
  1596. static unsigned int ata_eh_speed_down(struct ata_device *dev,
  1597. unsigned int eflags, unsigned int err_mask)
  1598. {
  1599. struct ata_link *link = ata_dev_phys_link(dev);
  1600. int xfer_ok = 0;
  1601. unsigned int verdict;
  1602. unsigned int action = 0;
  1603. /* don't bother if Cat-0 error */
  1604. if (ata_eh_categorize_error(eflags, err_mask, &xfer_ok) == 0)
  1605. return 0;
  1606. /* record error and determine whether speed down is necessary */
  1607. ata_ering_record(&dev->ering, eflags, err_mask);
  1608. verdict = ata_eh_speed_down_verdict(dev);
  1609. /* turn off NCQ? */
  1610. if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
  1611. (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
  1612. ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
  1613. dev->flags |= ATA_DFLAG_NCQ_OFF;
  1614. ata_dev_printk(dev, KERN_WARNING,
  1615. "NCQ disabled due to excessive errors\n");
  1616. goto done;
  1617. }
  1618. /* speed down? */
  1619. if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
  1620. /* speed down SATA link speed if possible */
  1621. if (sata_down_spd_limit(link, 0) == 0) {
  1622. action |= ATA_EH_RESET;
  1623. goto done;
  1624. }
  1625. /* lower transfer mode */
  1626. if (dev->spdn_cnt < 2) {
  1627. static const int dma_dnxfer_sel[] =
  1628. { ATA_DNXFER_DMA, ATA_DNXFER_40C };
  1629. static const int pio_dnxfer_sel[] =
  1630. { ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
  1631. int sel;
  1632. if (dev->xfer_shift != ATA_SHIFT_PIO)
  1633. sel = dma_dnxfer_sel[dev->spdn_cnt];
  1634. else
  1635. sel = pio_dnxfer_sel[dev->spdn_cnt];
  1636. dev->spdn_cnt++;
  1637. if (ata_down_xfermask_limit(dev, sel) == 0) {
  1638. action |= ATA_EH_RESET;
  1639. goto done;
  1640. }
  1641. }
  1642. }
  1643. /* Fall back to PIO? Slowing down to PIO is meaningless for
  1644. * SATA ATA devices. Consider it only for PATA and SATAPI.
  1645. */
  1646. if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
  1647. (link->ap->cbl != ATA_CBL_SATA || dev->class == ATA_DEV_ATAPI) &&
  1648. (dev->xfer_shift != ATA_SHIFT_PIO)) {
  1649. if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
  1650. dev->spdn_cnt = 0;
  1651. action |= ATA_EH_RESET;
  1652. goto done;
  1653. }
  1654. }
  1655. return 0;
  1656. done:
  1657. /* device has been slowed down, blow error history */
  1658. if (!(verdict & ATA_EH_SPDN_KEEP_ERRORS))
  1659. ata_ering_clear(&dev->ering);
  1660. return action;
  1661. }
  1662. /**
  1663. * ata_eh_link_autopsy - analyze error and determine recovery action
  1664. * @link: host link to perform autopsy on
  1665. *
  1666. * Analyze why @link failed and determine which recovery actions
  1667. * are needed. This function also sets more detailed AC_ERR_*
  1668. * values and fills sense data for ATAPI CHECK SENSE.
  1669. *
  1670. * LOCKING:
  1671. * Kernel thread context (may sleep).
  1672. */
  1673. static void ata_eh_link_autopsy(struct ata_link *link)
  1674. {
  1675. struct ata_port *ap = link->ap;
  1676. struct ata_eh_context *ehc = &link->eh_context;
  1677. struct ata_device *dev;
  1678. unsigned int all_err_mask = 0, eflags = 0;
  1679. int tag;
  1680. u32 serror;
  1681. int rc;
  1682. DPRINTK("ENTER\n");
  1683. if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
  1684. return;
  1685. /* obtain and analyze SError */
  1686. rc = sata_scr_read(link, SCR_ERROR, &serror);
  1687. if (rc == 0) {
  1688. ehc->i.serror |= serror;
  1689. ata_eh_analyze_serror(link);
  1690. } else if (rc != -EOPNOTSUPP) {
  1691. /* SError read failed, force reset and probing */
  1692. ehc->i.probe_mask |= ATA_ALL_DEVICES;
  1693. ehc->i.action |= ATA_EH_RESET;
  1694. ehc->i.err_mask |= AC_ERR_OTHER;
  1695. }
  1696. /* analyze NCQ failure */
  1697. ata_eh_analyze_ncq_error(link);
  1698. /* any real error trumps AC_ERR_OTHER */
  1699. if (ehc->i.err_mask & ~AC_ERR_OTHER)
  1700. ehc->i.err_mask &= ~AC_ERR_OTHER;
  1701. all_err_mask |= ehc->i.err_mask;
  1702. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  1703. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  1704. if (!(qc->flags & ATA_QCFLAG_FAILED) ||
  1705. ata_dev_phys_link(qc->dev) != link)
  1706. continue;
  1707. /* inherit upper level err_mask */
  1708. qc->err_mask |= ehc->i.err_mask;
  1709. /* analyze TF */
  1710. ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
  1711. /* DEV errors are probably spurious in case of ATA_BUS error */
  1712. if (qc->err_mask & AC_ERR_ATA_BUS)
  1713. qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
  1714. AC_ERR_INVALID);
  1715. /* any real error trumps unknown error */
  1716. if (qc->err_mask & ~AC_ERR_OTHER)
  1717. qc->err_mask &= ~AC_ERR_OTHER;
  1718. /* SENSE_VALID trumps dev/unknown error and revalidation */
  1719. if (qc->flags & ATA_QCFLAG_SENSE_VALID)
  1720. qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
  1721. /* determine whether the command is worth retrying */
  1722. if (!(qc->err_mask & AC_ERR_INVALID) &&
  1723. ((qc->flags & ATA_QCFLAG_IO) || qc->err_mask != AC_ERR_DEV))
  1724. qc->flags |= ATA_QCFLAG_RETRY;
  1725. /* accumulate error info */
  1726. ehc->i.dev = qc->dev;
  1727. all_err_mask |= qc->err_mask;
  1728. if (qc->flags & ATA_QCFLAG_IO)
  1729. eflags |= ATA_EFLAG_IS_IO;
  1730. }
  1731. /* enforce default EH actions */
  1732. if (ap->pflags & ATA_PFLAG_FROZEN ||
  1733. all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
  1734. ehc->i.action |= ATA_EH_RESET;
  1735. else if (((eflags & ATA_EFLAG_IS_IO) && all_err_mask) ||
  1736. (!(eflags & ATA_EFLAG_IS_IO) && (all_err_mask & ~AC_ERR_DEV)))
  1737. ehc->i.action |= ATA_EH_REVALIDATE;
  1738. /* If we have offending qcs and the associated failed device,
  1739. * perform per-dev EH action only on the offending device.
  1740. */
  1741. if (ehc->i.dev) {
  1742. ehc->i.dev_action[ehc->i.dev->devno] |=
  1743. ehc->i.action & ATA_EH_PERDEV_MASK;
  1744. ehc->i.action &= ~ATA_EH_PERDEV_MASK;
  1745. }
  1746. /* propagate timeout to host link */
  1747. if ((all_err_mask & AC_ERR_TIMEOUT) && !ata_is_host_link(link))
  1748. ap->link.eh_context.i.err_mask |= AC_ERR_TIMEOUT;
  1749. /* record error and consider speeding down */
  1750. dev = ehc->i.dev;
  1751. if (!dev && ((ata_link_max_devices(link) == 1 &&
  1752. ata_dev_enabled(link->device))))
  1753. dev = link->device;
  1754. if (dev) {
  1755. if (dev->flags & ATA_DFLAG_DUBIOUS_XFER)
  1756. eflags |= ATA_EFLAG_DUBIOUS_XFER;
  1757. ehc->i.action |= ata_eh_speed_down(dev, eflags, all_err_mask);
  1758. }
  1759. DPRINTK("EXIT\n");
  1760. }
  1761. /**
  1762. * ata_eh_autopsy - analyze error and determine recovery action
  1763. * @ap: host port to perform autopsy on
  1764. *
  1765. * Analyze all links of @ap and determine why they failed and
  1766. * which recovery actions are needed.
  1767. *
  1768. * LOCKING:
  1769. * Kernel thread context (may sleep).
  1770. */
  1771. void ata_eh_autopsy(struct ata_port *ap)
  1772. {
  1773. struct ata_link *link;
  1774. ata_for_each_link(link, ap, EDGE)
  1775. ata_eh_link_autopsy(link);
  1776. /* Handle the frigging slave link. Autopsy is done similarly
  1777. * but actions and flags are transferred over to the master
  1778. * link and handled from there.
  1779. */
  1780. if (ap->slave_link) {
  1781. struct ata_eh_context *mehc = &ap->link.eh_context;
  1782. struct ata_eh_context *sehc = &ap->slave_link->eh_context;
  1783. /* transfer control flags from master to slave */
  1784. sehc->i.flags |= mehc->i.flags & ATA_EHI_TO_SLAVE_MASK;
  1785. /* perform autopsy on the slave link */
  1786. ata_eh_link_autopsy(ap->slave_link);
  1787. /* transfer actions from slave to master and clear slave */
  1788. ata_eh_about_to_do(ap->slave_link, NULL, ATA_EH_ALL_ACTIONS);
  1789. mehc->i.action |= sehc->i.action;
  1790. mehc->i.dev_action[1] |= sehc->i.dev_action[1];
  1791. mehc->i.flags |= sehc->i.flags;
  1792. ata_eh_done(ap->slave_link, NULL, ATA_EH_ALL_ACTIONS);
  1793. }
  1794. /* Autopsy of fanout ports can affect host link autopsy.
  1795. * Perform host link autopsy last.
  1796. */
  1797. if (sata_pmp_attached(ap))
  1798. ata_eh_link_autopsy(&ap->link);
  1799. }
  1800. /**
  1801. * ata_eh_link_report - report error handling to user
  1802. * @link: ATA link EH is going on
  1803. *
  1804. * Report EH to user.
  1805. *
  1806. * LOCKING:
  1807. * None.
  1808. */
  1809. static void ata_eh_link_report(struct ata_link *link)
  1810. {
  1811. struct ata_port *ap = link->ap;
  1812. struct ata_eh_context *ehc = &link->eh_context;
  1813. const char *frozen, *desc;
  1814. char tries_buf[6];
  1815. int tag, nr_failed = 0;
  1816. if (ehc->i.flags & ATA_EHI_QUIET)
  1817. return;
  1818. desc = NULL;
  1819. if (ehc->i.desc[0] != '\0')
  1820. desc = ehc->i.desc;
  1821. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  1822. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  1823. if (!(qc->flags & ATA_QCFLAG_FAILED) ||
  1824. ata_dev_phys_link(qc->dev) != link ||
  1825. ((qc->flags & ATA_QCFLAG_QUIET) &&
  1826. qc->err_mask == AC_ERR_DEV))
  1827. continue;
  1828. if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
  1829. continue;
  1830. nr_failed++;
  1831. }
  1832. if (!nr_failed && !ehc->i.err_mask)
  1833. return;
  1834. frozen = "";
  1835. if (ap->pflags & ATA_PFLAG_FROZEN)
  1836. frozen = " frozen";
  1837. memset(tries_buf, 0, sizeof(tries_buf));
  1838. if (ap->eh_tries < ATA_EH_MAX_TRIES)
  1839. snprintf(tries_buf, sizeof(tries_buf) - 1, " t%d",
  1840. ap->eh_tries);
  1841. if (ehc->i.dev) {
  1842. ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
  1843. "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
  1844. ehc->i.err_mask, link->sactive, ehc->i.serror,
  1845. ehc->i.action, frozen, tries_buf);
  1846. if (desc)
  1847. ata_dev_printk(ehc->i.dev, KERN_ERR, "%s\n", desc);
  1848. } else {
  1849. ata_link_printk(link, KERN_ERR, "exception Emask 0x%x "
  1850. "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
  1851. ehc->i.err_mask, link->sactive, ehc->i.serror,
  1852. ehc->i.action, frozen, tries_buf);
  1853. if (desc)
  1854. ata_link_printk(link, KERN_ERR, "%s\n", desc);
  1855. }
  1856. if (ehc->i.serror)
  1857. ata_link_printk(link, KERN_ERR,
  1858. "SError: { %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s}\n",
  1859. ehc->i.serror & SERR_DATA_RECOVERED ? "RecovData " : "",
  1860. ehc->i.serror & SERR_COMM_RECOVERED ? "RecovComm " : "",
  1861. ehc->i.serror & SERR_DATA ? "UnrecovData " : "",
  1862. ehc->i.serror & SERR_PERSISTENT ? "Persist " : "",
  1863. ehc->i.serror & SERR_PROTOCOL ? "Proto " : "",
  1864. ehc->i.serror & SERR_INTERNAL ? "HostInt " : "",
  1865. ehc->i.serror & SERR_PHYRDY_CHG ? "PHYRdyChg " : "",
  1866. ehc->i.serror & SERR_PHY_INT_ERR ? "PHYInt " : "",
  1867. ehc->i.serror & SERR_COMM_WAKE ? "CommWake " : "",
  1868. ehc->i.serror & SERR_10B_8B_ERR ? "10B8B " : "",
  1869. ehc->i.serror & SERR_DISPARITY ? "Dispar " : "",
  1870. ehc->i.serror & SERR_CRC ? "BadCRC " : "",
  1871. ehc->i.serror & SERR_HANDSHAKE ? "Handshk " : "",
  1872. ehc->i.serror & SERR_LINK_SEQ_ERR ? "LinkSeq " : "",
  1873. ehc->i.serror & SERR_TRANS_ST_ERROR ? "TrStaTrns " : "",
  1874. ehc->i.serror & SERR_UNRECOG_FIS ? "UnrecFIS " : "",
  1875. ehc->i.serror & SERR_DEV_XCHG ? "DevExch " : "");
  1876. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  1877. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  1878. struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
  1879. const u8 *cdb = qc->cdb;
  1880. char data_buf[20] = "";
  1881. char cdb_buf[70] = "";
  1882. if (!(qc->flags & ATA_QCFLAG_FAILED) ||
  1883. ata_dev_phys_link(qc->dev) != link || !qc->err_mask)
  1884. continue;
  1885. if (qc->dma_dir != DMA_NONE) {
  1886. static const char *dma_str[] = {
  1887. [DMA_BIDIRECTIONAL] = "bidi",
  1888. [DMA_TO_DEVICE] = "out",
  1889. [DMA_FROM_DEVICE] = "in",
  1890. };
  1891. static const char *prot_str[] = {
  1892. [ATA_PROT_PIO] = "pio",
  1893. [ATA_PROT_DMA] = "dma",
  1894. [ATA_PROT_NCQ] = "ncq",
  1895. [ATAPI_PROT_PIO] = "pio",
  1896. [ATAPI_PROT_DMA] = "dma",
  1897. };
  1898. snprintf(data_buf, sizeof(data_buf), " %s %u %s",
  1899. prot_str[qc->tf.protocol], qc->nbytes,
  1900. dma_str[qc->dma_dir]);
  1901. }
  1902. if (ata_is_atapi(qc->tf.protocol))
  1903. snprintf(cdb_buf, sizeof(cdb_buf),
  1904. "cdb %02x %02x %02x %02x %02x %02x %02x %02x "
  1905. "%02x %02x %02x %02x %02x %02x %02x %02x\n ",
  1906. cdb[0], cdb[1], cdb[2], cdb[3],
  1907. cdb[4], cdb[5], cdb[6], cdb[7],
  1908. cdb[8], cdb[9], cdb[10], cdb[11],
  1909. cdb[12], cdb[13], cdb[14], cdb[15]);
  1910. ata_dev_printk(qc->dev, KERN_ERR,
  1911. "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
  1912. "tag %d%s\n %s"
  1913. "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
  1914. "Emask 0x%x (%s)%s\n",
  1915. cmd->command, cmd->feature, cmd->nsect,
  1916. cmd->lbal, cmd->lbam, cmd->lbah,
  1917. cmd->hob_feature, cmd->hob_nsect,
  1918. cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
  1919. cmd->device, qc->tag, data_buf, cdb_buf,
  1920. res->command, res->feature, res->nsect,
  1921. res->lbal, res->lbam, res->lbah,
  1922. res->hob_feature, res->hob_nsect,
  1923. res->hob_lbal, res->hob_lbam, res->hob_lbah,
  1924. res->device, qc->err_mask, ata_err_string(qc->err_mask),
  1925. qc->err_mask & AC_ERR_NCQ ? " <F>" : "");
  1926. if (res->command & (ATA_BUSY | ATA_DRDY | ATA_DF | ATA_DRQ |
  1927. ATA_ERR)) {
  1928. if (res->command & ATA_BUSY)
  1929. ata_dev_printk(qc->dev, KERN_ERR,
  1930. "status: { Busy }\n");
  1931. else
  1932. ata_dev_printk(qc->dev, KERN_ERR,
  1933. "status: { %s%s%s%s}\n",
  1934. res->command & ATA_DRDY ? "DRDY " : "",
  1935. res->command & ATA_DF ? "DF " : "",
  1936. res->command & ATA_DRQ ? "DRQ " : "",
  1937. res->command & ATA_ERR ? "ERR " : "");
  1938. }
  1939. if (cmd->command != ATA_CMD_PACKET &&
  1940. (res->feature & (ATA_ICRC | ATA_UNC | ATA_IDNF |
  1941. ATA_ABORTED)))
  1942. ata_dev_printk(qc->dev, KERN_ERR,
  1943. "error: { %s%s%s%s}\n",
  1944. res->feature & ATA_ICRC ? "ICRC " : "",
  1945. res->feature & ATA_UNC ? "UNC " : "",
  1946. res->feature & ATA_IDNF ? "IDNF " : "",
  1947. res->feature & ATA_ABORTED ? "ABRT " : "");
  1948. }
  1949. }
  1950. /**
  1951. * ata_eh_report - report error handling to user
  1952. * @ap: ATA port to report EH about
  1953. *
  1954. * Report EH to user.
  1955. *
  1956. * LOCKING:
  1957. * None.
  1958. */
  1959. void ata_eh_report(struct ata_port *ap)
  1960. {
  1961. struct ata_link *link;
  1962. ata_for_each_link(link, ap, HOST_FIRST)
  1963. ata_eh_link_report(link);
  1964. }
  1965. static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset,
  1966. unsigned int *classes, unsigned long deadline,
  1967. bool clear_classes)
  1968. {
  1969. struct ata_device *dev;
  1970. if (clear_classes)
  1971. ata_for_each_dev(dev, link, ALL)
  1972. classes[dev->devno] = ATA_DEV_UNKNOWN;
  1973. return reset(link, classes, deadline);
  1974. }
  1975. static int ata_eh_followup_srst_needed(struct ata_link *link,
  1976. int rc, const unsigned int *classes)
  1977. {
  1978. if ((link->flags & ATA_LFLAG_NO_SRST) || ata_link_offline(link))
  1979. return 0;
  1980. if (rc == -EAGAIN)
  1981. return 1;
  1982. if (sata_pmp_supported(link->ap) && ata_is_host_link(link))
  1983. return 1;
  1984. return 0;
  1985. }
  1986. int ata_eh_reset(struct ata_link *link, int classify,
  1987. ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
  1988. ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
  1989. {
  1990. struct ata_port *ap = link->ap;
  1991. struct ata_link *slave = ap->slave_link;
  1992. struct ata_eh_context *ehc = &link->eh_context;
  1993. struct ata_eh_context *sehc = &slave->eh_context;
  1994. unsigned int *classes = ehc->classes;
  1995. unsigned int lflags = link->flags;
  1996. int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
  1997. int max_tries = 0, try = 0;
  1998. struct ata_link *failed_link;
  1999. struct ata_device *dev;
  2000. unsigned long deadline, now;
  2001. ata_reset_fn_t reset;
  2002. unsigned long flags;
  2003. u32 sstatus;
  2004. int nr_unknown, rc;
  2005. /*
  2006. * Prepare to reset
  2007. */
  2008. while (ata_eh_reset_timeouts[max_tries] != ULONG_MAX)
  2009. max_tries++;
  2010. if (link->flags & ATA_LFLAG_NO_HRST)
  2011. hardreset = NULL;
  2012. if (link->flags & ATA_LFLAG_NO_SRST)
  2013. softreset = NULL;
  2014. /* make sure each reset attemp is at least COOL_DOWN apart */
  2015. if (ehc->i.flags & ATA_EHI_DID_RESET) {
  2016. now = jiffies;
  2017. WARN_ON(time_after(ehc->last_reset, now));
  2018. deadline = ata_deadline(ehc->last_reset,
  2019. ATA_EH_RESET_COOL_DOWN);
  2020. if (time_before(now, deadline))
  2021. schedule_timeout_uninterruptible(deadline - now);
  2022. }
  2023. spin_lock_irqsave(ap->lock, flags);
  2024. ap->pflags |= ATA_PFLAG_RESETTING;
  2025. spin_unlock_irqrestore(ap->lock, flags);
  2026. ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
  2027. ata_for_each_dev(dev, link, ALL) {
  2028. /* If we issue an SRST then an ATA drive (not ATAPI)
  2029. * may change configuration and be in PIO0 timing. If
  2030. * we do a hard reset (or are coming from power on)
  2031. * this is true for ATA or ATAPI. Until we've set a
  2032. * suitable controller mode we should not touch the
  2033. * bus as we may be talking too fast.
  2034. */
  2035. dev->pio_mode = XFER_PIO_0;
  2036. /* If the controller has a pio mode setup function
  2037. * then use it to set the chipset to rights. Don't
  2038. * touch the DMA setup as that will be dealt with when
  2039. * configuring devices.
  2040. */
  2041. if (ap->ops->set_piomode)
  2042. ap->ops->set_piomode(ap, dev);
  2043. }
  2044. /* prefer hardreset */
  2045. reset = NULL;
  2046. ehc->i.action &= ~ATA_EH_RESET;
  2047. if (hardreset) {
  2048. reset = hardreset;
  2049. ehc->i.action |= ATA_EH_HARDRESET;
  2050. } else if (softreset) {
  2051. reset = softreset;
  2052. ehc->i.action |= ATA_EH_SOFTRESET;
  2053. }
  2054. if (prereset) {
  2055. unsigned long deadline = ata_deadline(jiffies,
  2056. ATA_EH_PRERESET_TIMEOUT);
  2057. if (slave) {
  2058. sehc->i.action &= ~ATA_EH_RESET;
  2059. sehc->i.action |= ehc->i.action;
  2060. }
  2061. rc = prereset(link, deadline);
  2062. /* If present, do prereset on slave link too. Reset
  2063. * is skipped iff both master and slave links report
  2064. * -ENOENT or clear ATA_EH_RESET.
  2065. */
  2066. if (slave && (rc == 0 || rc == -ENOENT)) {
  2067. int tmp;
  2068. tmp = prereset(slave, deadline);
  2069. if (tmp != -ENOENT)
  2070. rc = tmp;
  2071. ehc->i.action |= sehc->i.action;
  2072. }
  2073. if (rc) {
  2074. if (rc == -ENOENT) {
  2075. ata_link_printk(link, KERN_DEBUG,
  2076. "port disabled. ignoring.\n");
  2077. ehc->i.action &= ~ATA_EH_RESET;
  2078. ata_for_each_dev(dev, link, ALL)
  2079. classes[dev->devno] = ATA_DEV_NONE;
  2080. rc = 0;
  2081. } else
  2082. ata_link_printk(link, KERN_ERR,
  2083. "prereset failed (errno=%d)\n", rc);
  2084. goto out;
  2085. }
  2086. /* prereset() might have cleared ATA_EH_RESET. If so,
  2087. * bang classes, thaw and return.
  2088. */
  2089. if (reset && !(ehc->i.action & ATA_EH_RESET)) {
  2090. ata_for_each_dev(dev, link, ALL)
  2091. classes[dev->devno] = ATA_DEV_NONE;
  2092. if ((ap->pflags & ATA_PFLAG_FROZEN) &&
  2093. ata_is_host_link(link))
  2094. ata_eh_thaw_port(ap);
  2095. rc = 0;
  2096. goto out;
  2097. }
  2098. }
  2099. retry:
  2100. /*
  2101. * Perform reset
  2102. */
  2103. if (ata_is_host_link(link))
  2104. ata_eh_freeze_port(ap);
  2105. deadline = ata_deadline(jiffies, ata_eh_reset_timeouts[try++]);
  2106. if (reset) {
  2107. if (verbose)
  2108. ata_link_printk(link, KERN_INFO, "%s resetting link\n",
  2109. reset == softreset ? "soft" : "hard");
  2110. /* mark that this EH session started with reset */
  2111. ehc->last_reset = jiffies;
  2112. if (reset == hardreset)
  2113. ehc->i.flags |= ATA_EHI_DID_HARDRESET;
  2114. else
  2115. ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
  2116. rc = ata_do_reset(link, reset, classes, deadline, true);
  2117. if (rc && rc != -EAGAIN) {
  2118. failed_link = link;
  2119. goto fail;
  2120. }
  2121. /* hardreset slave link if existent */
  2122. if (slave && reset == hardreset) {
  2123. int tmp;
  2124. if (verbose)
  2125. ata_link_printk(slave, KERN_INFO,
  2126. "hard resetting link\n");
  2127. ata_eh_about_to_do(slave, NULL, ATA_EH_RESET);
  2128. tmp = ata_do_reset(slave, reset, classes, deadline,
  2129. false);
  2130. switch (tmp) {
  2131. case -EAGAIN:
  2132. rc = -EAGAIN;
  2133. case 0:
  2134. break;
  2135. default:
  2136. failed_link = slave;
  2137. rc = tmp;
  2138. goto fail;
  2139. }
  2140. }
  2141. /* perform follow-up SRST if necessary */
  2142. if (reset == hardreset &&
  2143. ata_eh_followup_srst_needed(link, rc, classes)) {
  2144. reset = softreset;
  2145. if (!reset) {
  2146. ata_link_printk(link, KERN_ERR,
  2147. "follow-up softreset required "
  2148. "but no softreset avaliable\n");
  2149. failed_link = link;
  2150. rc = -EINVAL;
  2151. goto fail;
  2152. }
  2153. ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
  2154. rc = ata_do_reset(link, reset, classes, deadline, true);
  2155. }
  2156. } else {
  2157. if (verbose)
  2158. ata_link_printk(link, KERN_INFO, "no reset method "
  2159. "available, skipping reset\n");
  2160. if (!(lflags & ATA_LFLAG_ASSUME_CLASS))
  2161. lflags |= ATA_LFLAG_ASSUME_ATA;
  2162. }
  2163. /*
  2164. * Post-reset processing
  2165. */
  2166. ata_for_each_dev(dev, link, ALL) {
  2167. /* After the reset, the device state is PIO 0 and the
  2168. * controller state is undefined. Reset also wakes up
  2169. * drives from sleeping mode.
  2170. */
  2171. dev->pio_mode = XFER_PIO_0;
  2172. dev->flags &= ~ATA_DFLAG_SLEEPING;
  2173. if (!ata_phys_link_offline(ata_dev_phys_link(dev))) {
  2174. /* apply class override */
  2175. if (lflags & ATA_LFLAG_ASSUME_ATA)
  2176. classes[dev->devno] = ATA_DEV_ATA;
  2177. else if (lflags & ATA_LFLAG_ASSUME_SEMB)
  2178. classes[dev->devno] = ATA_DEV_SEMB_UNSUP;
  2179. } else
  2180. classes[dev->devno] = ATA_DEV_NONE;
  2181. }
  2182. /* record current link speed */
  2183. if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0)
  2184. link->sata_spd = (sstatus >> 4) & 0xf;
  2185. if (slave && sata_scr_read(slave, SCR_STATUS, &sstatus) == 0)
  2186. slave->sata_spd = (sstatus >> 4) & 0xf;
  2187. /* thaw the port */
  2188. if (ata_is_host_link(link))
  2189. ata_eh_thaw_port(ap);
  2190. /* postreset() should clear hardware SError. Although SError
  2191. * is cleared during link resume, clearing SError here is
  2192. * necessary as some PHYs raise hotplug events after SRST.
  2193. * This introduces race condition where hotplug occurs between
  2194. * reset and here. This race is mediated by cross checking
  2195. * link onlineness and classification result later.
  2196. */
  2197. if (postreset) {
  2198. postreset(link, classes);
  2199. if (slave)
  2200. postreset(slave, classes);
  2201. }
  2202. /* clear cached SError */
  2203. spin_lock_irqsave(link->ap->lock, flags);
  2204. link->eh_info.serror = 0;
  2205. if (slave)
  2206. slave->eh_info.serror = 0;
  2207. spin_unlock_irqrestore(link->ap->lock, flags);
  2208. /* Make sure onlineness and classification result correspond.
  2209. * Hotplug could have happened during reset and some
  2210. * controllers fail to wait while a drive is spinning up after
  2211. * being hotplugged causing misdetection. By cross checking
  2212. * link onlineness and classification result, those conditions
  2213. * can be reliably detected and retried.
  2214. */
  2215. nr_unknown = 0;
  2216. ata_for_each_dev(dev, link, ALL) {
  2217. /* convert all ATA_DEV_UNKNOWN to ATA_DEV_NONE */
  2218. if (classes[dev->devno] == ATA_DEV_UNKNOWN) {
  2219. classes[dev->devno] = ATA_DEV_NONE;
  2220. if (ata_phys_link_online(ata_dev_phys_link(dev)))
  2221. nr_unknown++;
  2222. }
  2223. }
  2224. if (classify && nr_unknown) {
  2225. if (try < max_tries) {
  2226. ata_link_printk(link, KERN_WARNING, "link online but "
  2227. "device misclassified, retrying\n");
  2228. failed_link = link;
  2229. rc = -EAGAIN;
  2230. goto fail;
  2231. }
  2232. ata_link_printk(link, KERN_WARNING,
  2233. "link online but device misclassified, "
  2234. "device detection might fail\n");
  2235. }
  2236. /* reset successful, schedule revalidation */
  2237. ata_eh_done(link, NULL, ATA_EH_RESET);
  2238. if (slave)
  2239. ata_eh_done(slave, NULL, ATA_EH_RESET);
  2240. ehc->last_reset = jiffies; /* update to completion time */
  2241. ehc->i.action |= ATA_EH_REVALIDATE;
  2242. rc = 0;
  2243. out:
  2244. /* clear hotplug flag */
  2245. ehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
  2246. if (slave)
  2247. sehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
  2248. spin_lock_irqsave(ap->lock, flags);
  2249. ap->pflags &= ~ATA_PFLAG_RESETTING;
  2250. spin_unlock_irqrestore(ap->lock, flags);
  2251. return rc;
  2252. fail:
  2253. /* if SCR isn't accessible on a fan-out port, PMP needs to be reset */
  2254. if (!ata_is_host_link(link) &&
  2255. sata_scr_read(link, SCR_STATUS, &sstatus))
  2256. rc = -ERESTART;
  2257. if (rc == -ERESTART || try >= max_tries)
  2258. goto out;
  2259. now = jiffies;
  2260. if (time_before(now, deadline)) {
  2261. unsigned long delta = deadline - now;
  2262. ata_link_printk(failed_link, KERN_WARNING,
  2263. "reset failed (errno=%d), retrying in %u secs\n",
  2264. rc, DIV_ROUND_UP(jiffies_to_msecs(delta), 1000));
  2265. while (delta)
  2266. delta = schedule_timeout_uninterruptible(delta);
  2267. }
  2268. if (try == max_tries - 1) {
  2269. sata_down_spd_limit(link, 0);
  2270. if (slave)
  2271. sata_down_spd_limit(slave, 0);
  2272. } else if (rc == -EPIPE)
  2273. sata_down_spd_limit(failed_link, 0);
  2274. if (hardreset)
  2275. reset = hardreset;
  2276. goto retry;
  2277. }
  2278. static inline void ata_eh_pull_park_action(struct ata_port *ap)
  2279. {
  2280. struct ata_link *link;
  2281. struct ata_device *dev;
  2282. unsigned long flags;
  2283. /*
  2284. * This function can be thought of as an extended version of
  2285. * ata_eh_about_to_do() specially crafted to accommodate the
  2286. * requirements of ATA_EH_PARK handling. Since the EH thread
  2287. * does not leave the do {} while () loop in ata_eh_recover as
  2288. * long as the timeout for a park request to *one* device on
  2289. * the port has not expired, and since we still want to pick
  2290. * up park requests to other devices on the same port or
  2291. * timeout updates for the same device, we have to pull
  2292. * ATA_EH_PARK actions from eh_info into eh_context.i
  2293. * ourselves at the beginning of each pass over the loop.
  2294. *
  2295. * Additionally, all write accesses to &ap->park_req_pending
  2296. * through INIT_COMPLETION() (see below) or complete_all()
  2297. * (see ata_scsi_park_store()) are protected by the host lock.
  2298. * As a result we have that park_req_pending.done is zero on
  2299. * exit from this function, i.e. when ATA_EH_PARK actions for
  2300. * *all* devices on port ap have been pulled into the
  2301. * respective eh_context structs. If, and only if,
  2302. * park_req_pending.done is non-zero by the time we reach
  2303. * wait_for_completion_timeout(), another ATA_EH_PARK action
  2304. * has been scheduled for at least one of the devices on port
  2305. * ap and we have to cycle over the do {} while () loop in
  2306. * ata_eh_recover() again.
  2307. */
  2308. spin_lock_irqsave(ap->lock, flags);
  2309. INIT_COMPLETION(ap->park_req_pending);
  2310. ata_for_each_link(link, ap, EDGE) {
  2311. ata_for_each_dev(dev, link, ALL) {
  2312. struct ata_eh_info *ehi = &link->eh_info;
  2313. link->eh_context.i.dev_action[dev->devno] |=
  2314. ehi->dev_action[dev->devno] & ATA_EH_PARK;
  2315. ata_eh_clear_action(link, dev, ehi, ATA_EH_PARK);
  2316. }
  2317. }
  2318. spin_unlock_irqrestore(ap->lock, flags);
  2319. }
  2320. static void ata_eh_park_issue_cmd(struct ata_device *dev, int park)
  2321. {
  2322. struct ata_eh_context *ehc = &dev->link->eh_context;
  2323. struct ata_taskfile tf;
  2324. unsigned int err_mask;
  2325. ata_tf_init(dev, &tf);
  2326. if (park) {
  2327. ehc->unloaded_mask |= 1 << dev->devno;
  2328. tf.command = ATA_CMD_IDLEIMMEDIATE;
  2329. tf.feature = 0x44;
  2330. tf.lbal = 0x4c;
  2331. tf.lbam = 0x4e;
  2332. tf.lbah = 0x55;
  2333. } else {
  2334. ehc->unloaded_mask &= ~(1 << dev->devno);
  2335. tf.command = ATA_CMD_CHK_POWER;
  2336. }
  2337. tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
  2338. tf.protocol |= ATA_PROT_NODATA;
  2339. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
  2340. if (park && (err_mask || tf.lbal != 0xc4)) {
  2341. ata_dev_printk(dev, KERN_ERR, "head unload failed!\n");
  2342. ehc->unloaded_mask &= ~(1 << dev->devno);
  2343. }
  2344. }
  2345. static int ata_eh_revalidate_and_attach(struct ata_link *link,
  2346. struct ata_device **r_failed_dev)
  2347. {
  2348. struct ata_port *ap = link->ap;
  2349. struct ata_eh_context *ehc = &link->eh_context;
  2350. struct ata_device *dev;
  2351. unsigned int new_mask = 0;
  2352. unsigned long flags;
  2353. int rc = 0;
  2354. DPRINTK("ENTER\n");
  2355. /* For PATA drive side cable detection to work, IDENTIFY must
  2356. * be done backwards such that PDIAG- is released by the slave
  2357. * device before the master device is identified.
  2358. */
  2359. ata_for_each_dev(dev, link, ALL_REVERSE) {
  2360. unsigned int action = ata_eh_dev_action(dev);
  2361. unsigned int readid_flags = 0;
  2362. if (ehc->i.flags & ATA_EHI_DID_RESET)
  2363. readid_flags |= ATA_READID_POSTRESET;
  2364. if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
  2365. WARN_ON(dev->class == ATA_DEV_PMP);
  2366. if (ata_phys_link_offline(ata_dev_phys_link(dev))) {
  2367. rc = -EIO;
  2368. goto err;
  2369. }
  2370. ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE);
  2371. rc = ata_dev_revalidate(dev, ehc->classes[dev->devno],
  2372. readid_flags);
  2373. if (rc)
  2374. goto err;
  2375. ata_eh_done(link, dev, ATA_EH_REVALIDATE);
  2376. /* Configuration may have changed, reconfigure
  2377. * transfer mode.
  2378. */
  2379. ehc->i.flags |= ATA_EHI_SETMODE;
  2380. /* schedule the scsi_rescan_device() here */
  2381. queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
  2382. } else if (dev->class == ATA_DEV_UNKNOWN &&
  2383. ehc->tries[dev->devno] &&
  2384. ata_class_enabled(ehc->classes[dev->devno])) {
  2385. /* Temporarily set dev->class, it will be
  2386. * permanently set once all configurations are
  2387. * complete. This is necessary because new
  2388. * device configuration is done in two
  2389. * separate loops.
  2390. */
  2391. dev->class = ehc->classes[dev->devno];
  2392. if (dev->class == ATA_DEV_PMP)
  2393. rc = sata_pmp_attach(dev);
  2394. else
  2395. rc = ata_dev_read_id(dev, &dev->class,
  2396. readid_flags, dev->id);
  2397. /* read_id might have changed class, store and reset */
  2398. ehc->classes[dev->devno] = dev->class;
  2399. dev->class = ATA_DEV_UNKNOWN;
  2400. switch (rc) {
  2401. case 0:
  2402. /* clear error info accumulated during probe */
  2403. ata_ering_clear(&dev->ering);
  2404. new_mask |= 1 << dev->devno;
  2405. break;
  2406. case -ENOENT:
  2407. /* IDENTIFY was issued to non-existent
  2408. * device. No need to reset. Just
  2409. * thaw and ignore the device.
  2410. */
  2411. ata_eh_thaw_port(ap);
  2412. break;
  2413. default:
  2414. goto err;
  2415. }
  2416. }
  2417. }
  2418. /* PDIAG- should have been released, ask cable type if post-reset */
  2419. if ((ehc->i.flags & ATA_EHI_DID_RESET) && ata_is_host_link(link)) {
  2420. if (ap->ops->cable_detect)
  2421. ap->cbl = ap->ops->cable_detect(ap);
  2422. ata_force_cbl(ap);
  2423. }
  2424. /* Configure new devices forward such that user doesn't see
  2425. * device detection messages backwards.
  2426. */
  2427. ata_for_each_dev(dev, link, ALL) {
  2428. if (!(new_mask & (1 << dev->devno)) ||
  2429. dev->class == ATA_DEV_PMP)
  2430. continue;
  2431. dev->class = ehc->classes[dev->devno];
  2432. ehc->i.flags |= ATA_EHI_PRINTINFO;
  2433. rc = ata_dev_configure(dev);
  2434. ehc->i.flags &= ~ATA_EHI_PRINTINFO;
  2435. if (rc) {
  2436. dev->class = ATA_DEV_UNKNOWN;
  2437. goto err;
  2438. }
  2439. spin_lock_irqsave(ap->lock, flags);
  2440. ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
  2441. spin_unlock_irqrestore(ap->lock, flags);
  2442. /* new device discovered, configure xfermode */
  2443. ehc->i.flags |= ATA_EHI_SETMODE;
  2444. }
  2445. return 0;
  2446. err:
  2447. *r_failed_dev = dev;
  2448. DPRINTK("EXIT rc=%d\n", rc);
  2449. return rc;
  2450. }
  2451. /**
  2452. * ata_set_mode - Program timings and issue SET FEATURES - XFER
  2453. * @link: link on which timings will be programmed
  2454. * @r_failed_dev: out paramter for failed device
  2455. *
  2456. * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
  2457. * ata_set_mode() fails, pointer to the failing device is
  2458. * returned in @r_failed_dev.
  2459. *
  2460. * LOCKING:
  2461. * PCI/etc. bus probe sem.
  2462. *
  2463. * RETURNS:
  2464. * 0 on success, negative errno otherwise
  2465. */
  2466. int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
  2467. {
  2468. struct ata_port *ap = link->ap;
  2469. struct ata_device *dev;
  2470. int rc;
  2471. /* if data transfer is verified, clear DUBIOUS_XFER on ering top */
  2472. ata_for_each_dev(dev, link, ENABLED) {
  2473. if (!(dev->flags & ATA_DFLAG_DUBIOUS_XFER)) {
  2474. struct ata_ering_entry *ent;
  2475. ent = ata_ering_top(&dev->ering);
  2476. if (ent)
  2477. ent->eflags &= ~ATA_EFLAG_DUBIOUS_XFER;
  2478. }
  2479. }
  2480. /* has private set_mode? */
  2481. if (ap->ops->set_mode)
  2482. rc = ap->ops->set_mode(link, r_failed_dev);
  2483. else
  2484. rc = ata_do_set_mode(link, r_failed_dev);
  2485. /* if transfer mode has changed, set DUBIOUS_XFER on device */
  2486. ata_for_each_dev(dev, link, ENABLED) {
  2487. struct ata_eh_context *ehc = &link->eh_context;
  2488. u8 saved_xfer_mode = ehc->saved_xfer_mode[dev->devno];
  2489. u8 saved_ncq = !!(ehc->saved_ncq_enabled & (1 << dev->devno));
  2490. if (dev->xfer_mode != saved_xfer_mode ||
  2491. ata_ncq_enabled(dev) != saved_ncq)
  2492. dev->flags |= ATA_DFLAG_DUBIOUS_XFER;
  2493. }
  2494. return rc;
  2495. }
  2496. /**
  2497. * atapi_eh_clear_ua - Clear ATAPI UNIT ATTENTION after reset
  2498. * @dev: ATAPI device to clear UA for
  2499. *
  2500. * Resets and other operations can make an ATAPI device raise
  2501. * UNIT ATTENTION which causes the next operation to fail. This
  2502. * function clears UA.
  2503. *
  2504. * LOCKING:
  2505. * EH context (may sleep).
  2506. *
  2507. * RETURNS:
  2508. * 0 on success, -errno on failure.
  2509. */
  2510. static int atapi_eh_clear_ua(struct ata_device *dev)
  2511. {
  2512. int i;
  2513. for (i = 0; i < ATA_EH_UA_TRIES; i++) {
  2514. u8 *sense_buffer = dev->link->ap->sector_buf;
  2515. u8 sense_key = 0;
  2516. unsigned int err_mask;
  2517. err_mask = atapi_eh_tur(dev, &sense_key);
  2518. if (err_mask != 0 && err_mask != AC_ERR_DEV) {
  2519. ata_dev_printk(dev, KERN_WARNING, "TEST_UNIT_READY "
  2520. "failed (err_mask=0x%x)\n", err_mask);
  2521. return -EIO;
  2522. }
  2523. if (!err_mask || sense_key != UNIT_ATTENTION)
  2524. return 0;
  2525. err_mask = atapi_eh_request_sense(dev, sense_buffer, sense_key);
  2526. if (err_mask) {
  2527. ata_dev_printk(dev, KERN_WARNING, "failed to clear "
  2528. "UNIT ATTENTION (err_mask=0x%x)\n", err_mask);
  2529. return -EIO;
  2530. }
  2531. }
  2532. ata_dev_printk(dev, KERN_WARNING,
  2533. "UNIT ATTENTION persists after %d tries\n", ATA_EH_UA_TRIES);
  2534. return 0;
  2535. }
  2536. static int ata_link_nr_enabled(struct ata_link *link)
  2537. {
  2538. struct ata_device *dev;
  2539. int cnt = 0;
  2540. ata_for_each_dev(dev, link, ENABLED)
  2541. cnt++;
  2542. return cnt;
  2543. }
  2544. static int ata_link_nr_vacant(struct ata_link *link)
  2545. {
  2546. struct ata_device *dev;
  2547. int cnt = 0;
  2548. ata_for_each_dev(dev, link, ALL)
  2549. if (dev->class == ATA_DEV_UNKNOWN)
  2550. cnt++;
  2551. return cnt;
  2552. }
  2553. static int ata_eh_skip_recovery(struct ata_link *link)
  2554. {
  2555. struct ata_port *ap = link->ap;
  2556. struct ata_eh_context *ehc = &link->eh_context;
  2557. struct ata_device *dev;
  2558. /* skip disabled links */
  2559. if (link->flags & ATA_LFLAG_DISABLED)
  2560. return 1;
  2561. /* thaw frozen port and recover failed devices */
  2562. if ((ap->pflags & ATA_PFLAG_FROZEN) || ata_link_nr_enabled(link))
  2563. return 0;
  2564. /* reset at least once if reset is requested */
  2565. if ((ehc->i.action & ATA_EH_RESET) &&
  2566. !(ehc->i.flags & ATA_EHI_DID_RESET))
  2567. return 0;
  2568. /* skip if class codes for all vacant slots are ATA_DEV_NONE */
  2569. ata_for_each_dev(dev, link, ALL) {
  2570. if (dev->class == ATA_DEV_UNKNOWN &&
  2571. ehc->classes[dev->devno] != ATA_DEV_NONE)
  2572. return 0;
  2573. }
  2574. return 1;
  2575. }
  2576. static int ata_count_probe_trials_cb(struct ata_ering_entry *ent, void *void_arg)
  2577. {
  2578. u64 interval = msecs_to_jiffies(ATA_EH_PROBE_TRIAL_INTERVAL);
  2579. u64 now = get_jiffies_64();
  2580. int *trials = void_arg;
  2581. if (ent->timestamp < now - min(now, interval))
  2582. return -1;
  2583. (*trials)++;
  2584. return 0;
  2585. }
  2586. static int ata_eh_schedule_probe(struct ata_device *dev)
  2587. {
  2588. struct ata_eh_context *ehc = &dev->link->eh_context;
  2589. struct ata_link *link = ata_dev_phys_link(dev);
  2590. int trials = 0;
  2591. if (!(ehc->i.probe_mask & (1 << dev->devno)) ||
  2592. (ehc->did_probe_mask & (1 << dev->devno)))
  2593. return 0;
  2594. ata_eh_detach_dev(dev);
  2595. ata_dev_init(dev);
  2596. ehc->did_probe_mask |= (1 << dev->devno);
  2597. ehc->i.action |= ATA_EH_RESET;
  2598. ehc->saved_xfer_mode[dev->devno] = 0;
  2599. ehc->saved_ncq_enabled &= ~(1 << dev->devno);
  2600. /* Record and count probe trials on the ering. The specific
  2601. * error mask used is irrelevant. Because a successful device
  2602. * detection clears the ering, this count accumulates only if
  2603. * there are consecutive failed probes.
  2604. *
  2605. * If the count is equal to or higher than ATA_EH_PROBE_TRIALS
  2606. * in the last ATA_EH_PROBE_TRIAL_INTERVAL, link speed is
  2607. * forced to 1.5Gbps.
  2608. *
  2609. * This is to work around cases where failed link speed
  2610. * negotiation results in device misdetection leading to
  2611. * infinite DEVXCHG or PHRDY CHG events.
  2612. */
  2613. ata_ering_record(&dev->ering, 0, AC_ERR_OTHER);
  2614. ata_ering_map(&dev->ering, ata_count_probe_trials_cb, &trials);
  2615. if (trials > ATA_EH_PROBE_TRIALS)
  2616. sata_down_spd_limit(link, 1);
  2617. return 1;
  2618. }
  2619. static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
  2620. {
  2621. struct ata_eh_context *ehc = &dev->link->eh_context;
  2622. /* -EAGAIN from EH routine indicates retry without prejudice.
  2623. * The requester is responsible for ensuring forward progress.
  2624. */
  2625. if (err != -EAGAIN)
  2626. ehc->tries[dev->devno]--;
  2627. switch (err) {
  2628. case -ENODEV:
  2629. /* device missing or wrong IDENTIFY data, schedule probing */
  2630. ehc->i.probe_mask |= (1 << dev->devno);
  2631. case -EINVAL:
  2632. /* give it just one more chance */
  2633. ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
  2634. case -EIO:
  2635. if (ehc->tries[dev->devno] == 1) {
  2636. /* This is the last chance, better to slow
  2637. * down than lose it.
  2638. */
  2639. sata_down_spd_limit(ata_dev_phys_link(dev), 0);
  2640. if (dev->pio_mode > XFER_PIO_0)
  2641. ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
  2642. }
  2643. }
  2644. if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
  2645. /* disable device if it has used up all its chances */
  2646. ata_dev_disable(dev);
  2647. /* detach if offline */
  2648. if (ata_phys_link_offline(ata_dev_phys_link(dev)))
  2649. ata_eh_detach_dev(dev);
  2650. /* schedule probe if necessary */
  2651. if (ata_eh_schedule_probe(dev)) {
  2652. ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
  2653. memset(ehc->cmd_timeout_idx[dev->devno], 0,
  2654. sizeof(ehc->cmd_timeout_idx[dev->devno]));
  2655. }
  2656. return 1;
  2657. } else {
  2658. ehc->i.action |= ATA_EH_RESET;
  2659. return 0;
  2660. }
  2661. }
  2662. /**
  2663. * ata_eh_recover - recover host port after error
  2664. * @ap: host port to recover
  2665. * @prereset: prereset method (can be NULL)
  2666. * @softreset: softreset method (can be NULL)
  2667. * @hardreset: hardreset method (can be NULL)
  2668. * @postreset: postreset method (can be NULL)
  2669. * @r_failed_link: out parameter for failed link
  2670. *
  2671. * This is the alpha and omega, eum and yang, heart and soul of
  2672. * libata exception handling. On entry, actions required to
  2673. * recover each link and hotplug requests are recorded in the
  2674. * link's eh_context. This function executes all the operations
  2675. * with appropriate retrials and fallbacks to resurrect failed
  2676. * devices, detach goners and greet newcomers.
  2677. *
  2678. * LOCKING:
  2679. * Kernel thread context (may sleep).
  2680. *
  2681. * RETURNS:
  2682. * 0 on success, -errno on failure.
  2683. */
  2684. int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
  2685. ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
  2686. ata_postreset_fn_t postreset,
  2687. struct ata_link **r_failed_link)
  2688. {
  2689. struct ata_link *link;
  2690. struct ata_device *dev;
  2691. int nr_failed_devs;
  2692. int rc;
  2693. unsigned long flags, deadline;
  2694. DPRINTK("ENTER\n");
  2695. /* prep for recovery */
  2696. ata_for_each_link(link, ap, EDGE) {
  2697. struct ata_eh_context *ehc = &link->eh_context;
  2698. /* re-enable link? */
  2699. if (ehc->i.action & ATA_EH_ENABLE_LINK) {
  2700. ata_eh_about_to_do(link, NULL, ATA_EH_ENABLE_LINK);
  2701. spin_lock_irqsave(ap->lock, flags);
  2702. link->flags &= ~ATA_LFLAG_DISABLED;
  2703. spin_unlock_irqrestore(ap->lock, flags);
  2704. ata_eh_done(link, NULL, ATA_EH_ENABLE_LINK);
  2705. }
  2706. ata_for_each_dev(dev, link, ALL) {
  2707. if (link->flags & ATA_LFLAG_NO_RETRY)
  2708. ehc->tries[dev->devno] = 1;
  2709. else
  2710. ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
  2711. /* collect port action mask recorded in dev actions */
  2712. ehc->i.action |= ehc->i.dev_action[dev->devno] &
  2713. ~ATA_EH_PERDEV_MASK;
  2714. ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK;
  2715. /* process hotplug request */
  2716. if (dev->flags & ATA_DFLAG_DETACH)
  2717. ata_eh_detach_dev(dev);
  2718. /* schedule probe if necessary */
  2719. if (!ata_dev_enabled(dev))
  2720. ata_eh_schedule_probe(dev);
  2721. }
  2722. }
  2723. retry:
  2724. rc = 0;
  2725. nr_failed_devs = 0;
  2726. /* if UNLOADING, finish immediately */
  2727. if (ap->pflags & ATA_PFLAG_UNLOADING)
  2728. goto out;
  2729. /* prep for EH */
  2730. ata_for_each_link(link, ap, EDGE) {
  2731. struct ata_eh_context *ehc = &link->eh_context;
  2732. /* skip EH if possible. */
  2733. if (ata_eh_skip_recovery(link))
  2734. ehc->i.action = 0;
  2735. ata_for_each_dev(dev, link, ALL)
  2736. ehc->classes[dev->devno] = ATA_DEV_UNKNOWN;
  2737. }
  2738. /* reset */
  2739. ata_for_each_link(link, ap, EDGE) {
  2740. struct ata_eh_context *ehc = &link->eh_context;
  2741. if (!(ehc->i.action & ATA_EH_RESET))
  2742. continue;
  2743. rc = ata_eh_reset(link, ata_link_nr_vacant(link),
  2744. prereset, softreset, hardreset, postreset);
  2745. if (rc) {
  2746. ata_link_printk(link, KERN_ERR,
  2747. "reset failed, giving up\n");
  2748. goto out;
  2749. }
  2750. }
  2751. do {
  2752. unsigned long now;
  2753. /*
  2754. * clears ATA_EH_PARK in eh_info and resets
  2755. * ap->park_req_pending
  2756. */
  2757. ata_eh_pull_park_action(ap);
  2758. deadline = jiffies;
  2759. ata_for_each_link(link, ap, EDGE) {
  2760. ata_for_each_dev(dev, link, ALL) {
  2761. struct ata_eh_context *ehc = &link->eh_context;
  2762. unsigned long tmp;
  2763. if (dev->class != ATA_DEV_ATA)
  2764. continue;
  2765. if (!(ehc->i.dev_action[dev->devno] &
  2766. ATA_EH_PARK))
  2767. continue;
  2768. tmp = dev->unpark_deadline;
  2769. if (time_before(deadline, tmp))
  2770. deadline = tmp;
  2771. else if (time_before_eq(tmp, jiffies))
  2772. continue;
  2773. if (ehc->unloaded_mask & (1 << dev->devno))
  2774. continue;
  2775. ata_eh_park_issue_cmd(dev, 1);
  2776. }
  2777. }
  2778. now = jiffies;
  2779. if (time_before_eq(deadline, now))
  2780. break;
  2781. deadline = wait_for_completion_timeout(&ap->park_req_pending,
  2782. deadline - now);
  2783. } while (deadline);
  2784. ata_for_each_link(link, ap, EDGE) {
  2785. ata_for_each_dev(dev, link, ALL) {
  2786. if (!(link->eh_context.unloaded_mask &
  2787. (1 << dev->devno)))
  2788. continue;
  2789. ata_eh_park_issue_cmd(dev, 0);
  2790. ata_eh_done(link, dev, ATA_EH_PARK);
  2791. }
  2792. }
  2793. /* the rest */
  2794. ata_for_each_link(link, ap, EDGE) {
  2795. struct ata_eh_context *ehc = &link->eh_context;
  2796. /* revalidate existing devices and attach new ones */
  2797. rc = ata_eh_revalidate_and_attach(link, &dev);
  2798. if (rc)
  2799. goto dev_fail;
  2800. /* if PMP got attached, return, pmp EH will take care of it */
  2801. if (link->device->class == ATA_DEV_PMP) {
  2802. ehc->i.action = 0;
  2803. return 0;
  2804. }
  2805. /* configure transfer mode if necessary */
  2806. if (ehc->i.flags & ATA_EHI_SETMODE) {
  2807. rc = ata_set_mode(link, &dev);
  2808. if (rc)
  2809. goto dev_fail;
  2810. ehc->i.flags &= ~ATA_EHI_SETMODE;
  2811. }
  2812. /* If reset has been issued, clear UA to avoid
  2813. * disrupting the current users of the device.
  2814. */
  2815. if (ehc->i.flags & ATA_EHI_DID_RESET) {
  2816. ata_for_each_dev(dev, link, ALL) {
  2817. if (dev->class != ATA_DEV_ATAPI)
  2818. continue;
  2819. rc = atapi_eh_clear_ua(dev);
  2820. if (rc)
  2821. goto dev_fail;
  2822. }
  2823. }
  2824. /* configure link power saving */
  2825. if (ehc->i.action & ATA_EH_LPM)
  2826. ata_for_each_dev(dev, link, ALL)
  2827. ata_dev_enable_pm(dev, ap->pm_policy);
  2828. /* this link is okay now */
  2829. ehc->i.flags = 0;
  2830. continue;
  2831. dev_fail:
  2832. nr_failed_devs++;
  2833. ata_eh_handle_dev_fail(dev, rc);
  2834. if (ap->pflags & ATA_PFLAG_FROZEN) {
  2835. /* PMP reset requires working host port.
  2836. * Can't retry if it's frozen.
  2837. */
  2838. if (sata_pmp_attached(ap))
  2839. goto out;
  2840. break;
  2841. }
  2842. }
  2843. if (nr_failed_devs)
  2844. goto retry;
  2845. out:
  2846. if (rc && r_failed_link)
  2847. *r_failed_link = link;
  2848. DPRINTK("EXIT, rc=%d\n", rc);
  2849. return rc;
  2850. }
  2851. /**
  2852. * ata_eh_finish - finish up EH
  2853. * @ap: host port to finish EH for
  2854. *
  2855. * Recovery is complete. Clean up EH states and retry or finish
  2856. * failed qcs.
  2857. *
  2858. * LOCKING:
  2859. * None.
  2860. */
  2861. void ata_eh_finish(struct ata_port *ap)
  2862. {
  2863. int tag;
  2864. /* retry or finish qcs */
  2865. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  2866. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  2867. if (!(qc->flags & ATA_QCFLAG_FAILED))
  2868. continue;
  2869. if (qc->err_mask) {
  2870. /* FIXME: Once EH migration is complete,
  2871. * generate sense data in this function,
  2872. * considering both err_mask and tf.
  2873. */
  2874. if (qc->flags & ATA_QCFLAG_RETRY)
  2875. ata_eh_qc_retry(qc);
  2876. else
  2877. ata_eh_qc_complete(qc);
  2878. } else {
  2879. if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
  2880. ata_eh_qc_complete(qc);
  2881. } else {
  2882. /* feed zero TF to sense generation */
  2883. memset(&qc->result_tf, 0, sizeof(qc->result_tf));
  2884. ata_eh_qc_retry(qc);
  2885. }
  2886. }
  2887. }
  2888. /* make sure nr_active_links is zero after EH */
  2889. WARN_ON(ap->nr_active_links);
  2890. ap->nr_active_links = 0;
  2891. }
  2892. /**
  2893. * ata_do_eh - do standard error handling
  2894. * @ap: host port to handle error for
  2895. *
  2896. * @prereset: prereset method (can be NULL)
  2897. * @softreset: softreset method (can be NULL)
  2898. * @hardreset: hardreset method (can be NULL)
  2899. * @postreset: postreset method (can be NULL)
  2900. *
  2901. * Perform standard error handling sequence.
  2902. *
  2903. * LOCKING:
  2904. * Kernel thread context (may sleep).
  2905. */
  2906. void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
  2907. ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
  2908. ata_postreset_fn_t postreset)
  2909. {
  2910. struct ata_device *dev;
  2911. int rc;
  2912. ata_eh_autopsy(ap);
  2913. ata_eh_report(ap);
  2914. rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset,
  2915. NULL);
  2916. if (rc) {
  2917. ata_for_each_dev(dev, &ap->link, ALL)
  2918. ata_dev_disable(dev);
  2919. }
  2920. ata_eh_finish(ap);
  2921. }
  2922. /**
  2923. * ata_std_error_handler - standard error handler
  2924. * @ap: host port to handle error for
  2925. *
  2926. * Standard error handler
  2927. *
  2928. * LOCKING:
  2929. * Kernel thread context (may sleep).
  2930. */
  2931. void ata_std_error_handler(struct ata_port *ap)
  2932. {
  2933. struct ata_port_operations *ops = ap->ops;
  2934. ata_reset_fn_t hardreset = ops->hardreset;
  2935. /* ignore built-in hardreset if SCR access is not available */
  2936. if (ata_is_builtin_hardreset(hardreset) && !sata_scr_valid(&ap->link))
  2937. hardreset = NULL;
  2938. ata_do_eh(ap, ops->prereset, ops->softreset, hardreset, ops->postreset);
  2939. }
  2940. #ifdef CONFIG_PM
  2941. /**
  2942. * ata_eh_handle_port_suspend - perform port suspend operation
  2943. * @ap: port to suspend
  2944. *
  2945. * Suspend @ap.
  2946. *
  2947. * LOCKING:
  2948. * Kernel thread context (may sleep).
  2949. */
  2950. static void ata_eh_handle_port_suspend(struct ata_port *ap)
  2951. {
  2952. unsigned long flags;
  2953. int rc = 0;
  2954. /* are we suspending? */
  2955. spin_lock_irqsave(ap->lock, flags);
  2956. if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
  2957. ap->pm_mesg.event == PM_EVENT_ON) {
  2958. spin_unlock_irqrestore(ap->lock, flags);
  2959. return;
  2960. }
  2961. spin_unlock_irqrestore(ap->lock, flags);
  2962. WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
  2963. /* tell ACPI we're suspending */
  2964. rc = ata_acpi_on_suspend(ap);
  2965. if (rc)
  2966. goto out;
  2967. /* suspend */
  2968. ata_eh_freeze_port(ap);
  2969. if (ap->ops->port_suspend)
  2970. rc = ap->ops->port_suspend(ap, ap->pm_mesg);
  2971. ata_acpi_set_state(ap, PMSG_SUSPEND);
  2972. out:
  2973. /* report result */
  2974. spin_lock_irqsave(ap->lock, flags);
  2975. ap->pflags &= ~ATA_PFLAG_PM_PENDING;
  2976. if (rc == 0)
  2977. ap->pflags |= ATA_PFLAG_SUSPENDED;
  2978. else if (ap->pflags & ATA_PFLAG_FROZEN)
  2979. ata_port_schedule_eh(ap);
  2980. if (ap->pm_result) {
  2981. *ap->pm_result = rc;
  2982. ap->pm_result = NULL;
  2983. }
  2984. spin_unlock_irqrestore(ap->lock, flags);
  2985. return;
  2986. }
  2987. /**
  2988. * ata_eh_handle_port_resume - perform port resume operation
  2989. * @ap: port to resume
  2990. *
  2991. * Resume @ap.
  2992. *
  2993. * LOCKING:
  2994. * Kernel thread context (may sleep).
  2995. */
  2996. static void ata_eh_handle_port_resume(struct ata_port *ap)
  2997. {
  2998. struct ata_link *link;
  2999. struct ata_device *dev;
  3000. unsigned long flags;
  3001. int rc = 0;
  3002. /* are we resuming? */
  3003. spin_lock_irqsave(ap->lock, flags);
  3004. if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
  3005. ap->pm_mesg.event != PM_EVENT_ON) {
  3006. spin_unlock_irqrestore(ap->lock, flags);
  3007. return;
  3008. }
  3009. spin_unlock_irqrestore(ap->lock, flags);
  3010. WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
  3011. /*
  3012. * Error timestamps are in jiffies which doesn't run while
  3013. * suspended and PHY events during resume isn't too uncommon.
  3014. * When the two are combined, it can lead to unnecessary speed
  3015. * downs if the machine is suspended and resumed repeatedly.
  3016. * Clear error history.
  3017. */
  3018. ata_for_each_link(link, ap, HOST_FIRST)
  3019. ata_for_each_dev(dev, link, ALL)
  3020. ata_ering_clear(&dev->ering);
  3021. ata_acpi_set_state(ap, PMSG_ON);
  3022. if (ap->ops->port_resume)
  3023. rc = ap->ops->port_resume(ap);
  3024. /* tell ACPI that we're resuming */
  3025. ata_acpi_on_resume(ap);
  3026. /* report result */
  3027. spin_lock_irqsave(ap->lock, flags);
  3028. ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
  3029. if (ap->pm_result) {
  3030. *ap->pm_result = rc;
  3031. ap->pm_result = NULL;
  3032. }
  3033. spin_unlock_irqrestore(ap->lock, flags);
  3034. }
  3035. #endif /* CONFIG_PM */