libata-eh.c 86 KB

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