libata-eh.c 77 KB

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