libata-eh.c 74 KB

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