libata-eh.c 73 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951
  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 (!sata_pmp_attached(ap) || rc) {
  740. /* PMP is not attached or SNTF is not available */
  741. if (!sata_pmp_attached(ap)) {
  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. ata_eh_clear_action(link, dev, ehi, action);
  915. if (!(ehc->i.flags & ATA_EHI_QUIET))
  916. ap->pflags |= ATA_PFLAG_RECOVERED;
  917. spin_unlock_irqrestore(ap->lock, flags);
  918. }
  919. /**
  920. * ata_eh_done - EH action complete
  921. * @ap: target ATA port
  922. * @dev: target ATA dev for per-dev action (can be NULL)
  923. * @action: action just completed
  924. *
  925. * Called right after performing EH actions to clear related bits
  926. * in @link->eh_context.
  927. *
  928. * LOCKING:
  929. * None.
  930. */
  931. void ata_eh_done(struct ata_link *link, struct ata_device *dev,
  932. unsigned int action)
  933. {
  934. struct ata_eh_context *ehc = &link->eh_context;
  935. ata_eh_clear_action(link, dev, &ehc->i, action);
  936. }
  937. /**
  938. * ata_err_string - convert err_mask to descriptive string
  939. * @err_mask: error mask to convert to string
  940. *
  941. * Convert @err_mask to descriptive string. Errors are
  942. * prioritized according to severity and only the most severe
  943. * error is reported.
  944. *
  945. * LOCKING:
  946. * None.
  947. *
  948. * RETURNS:
  949. * Descriptive string for @err_mask
  950. */
  951. static const char *ata_err_string(unsigned int err_mask)
  952. {
  953. if (err_mask & AC_ERR_HOST_BUS)
  954. return "host bus error";
  955. if (err_mask & AC_ERR_ATA_BUS)
  956. return "ATA bus error";
  957. if (err_mask & AC_ERR_TIMEOUT)
  958. return "timeout";
  959. if (err_mask & AC_ERR_HSM)
  960. return "HSM violation";
  961. if (err_mask & AC_ERR_SYSTEM)
  962. return "internal error";
  963. if (err_mask & AC_ERR_MEDIA)
  964. return "media error";
  965. if (err_mask & AC_ERR_INVALID)
  966. return "invalid argument";
  967. if (err_mask & AC_ERR_DEV)
  968. return "device error";
  969. return "unknown error";
  970. }
  971. /**
  972. * ata_read_log_page - read a specific log page
  973. * @dev: target device
  974. * @page: page to read
  975. * @buf: buffer to store read page
  976. * @sectors: number of sectors to read
  977. *
  978. * Read log page using READ_LOG_EXT command.
  979. *
  980. * LOCKING:
  981. * Kernel thread context (may sleep).
  982. *
  983. * RETURNS:
  984. * 0 on success, AC_ERR_* mask otherwise.
  985. */
  986. static unsigned int ata_read_log_page(struct ata_device *dev,
  987. u8 page, void *buf, unsigned int sectors)
  988. {
  989. struct ata_taskfile tf;
  990. unsigned int err_mask;
  991. DPRINTK("read log page - page %d\n", page);
  992. ata_tf_init(dev, &tf);
  993. tf.command = ATA_CMD_READ_LOG_EXT;
  994. tf.lbal = page;
  995. tf.nsect = sectors;
  996. tf.hob_nsect = sectors >> 8;
  997. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
  998. tf.protocol = ATA_PROT_PIO;
  999. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
  1000. buf, sectors * ATA_SECT_SIZE, 0);
  1001. DPRINTK("EXIT, err_mask=%x\n", err_mask);
  1002. return err_mask;
  1003. }
  1004. /**
  1005. * ata_eh_read_log_10h - Read log page 10h for NCQ error details
  1006. * @dev: Device to read log page 10h from
  1007. * @tag: Resulting tag of the failed command
  1008. * @tf: Resulting taskfile registers of the failed command
  1009. *
  1010. * Read log page 10h to obtain NCQ error details and clear error
  1011. * condition.
  1012. *
  1013. * LOCKING:
  1014. * Kernel thread context (may sleep).
  1015. *
  1016. * RETURNS:
  1017. * 0 on success, -errno otherwise.
  1018. */
  1019. static int ata_eh_read_log_10h(struct ata_device *dev,
  1020. int *tag, struct ata_taskfile *tf)
  1021. {
  1022. u8 *buf = dev->link->ap->sector_buf;
  1023. unsigned int err_mask;
  1024. u8 csum;
  1025. int i;
  1026. err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
  1027. if (err_mask)
  1028. return -EIO;
  1029. csum = 0;
  1030. for (i = 0; i < ATA_SECT_SIZE; i++)
  1031. csum += buf[i];
  1032. if (csum)
  1033. ata_dev_printk(dev, KERN_WARNING,
  1034. "invalid checksum 0x%x on log page 10h\n", csum);
  1035. if (buf[0] & 0x80)
  1036. return -ENOENT;
  1037. *tag = buf[0] & 0x1f;
  1038. tf->command = buf[2];
  1039. tf->feature = buf[3];
  1040. tf->lbal = buf[4];
  1041. tf->lbam = buf[5];
  1042. tf->lbah = buf[6];
  1043. tf->device = buf[7];
  1044. tf->hob_lbal = buf[8];
  1045. tf->hob_lbam = buf[9];
  1046. tf->hob_lbah = buf[10];
  1047. tf->nsect = buf[12];
  1048. tf->hob_nsect = buf[13];
  1049. return 0;
  1050. }
  1051. /**
  1052. * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
  1053. * @dev: device to perform REQUEST_SENSE to
  1054. * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
  1055. *
  1056. * Perform ATAPI REQUEST_SENSE after the device reported CHECK
  1057. * SENSE. This function is EH helper.
  1058. *
  1059. * LOCKING:
  1060. * Kernel thread context (may sleep).
  1061. *
  1062. * RETURNS:
  1063. * 0 on success, AC_ERR_* mask on failure
  1064. */
  1065. static unsigned int atapi_eh_request_sense(struct ata_queued_cmd *qc)
  1066. {
  1067. struct ata_device *dev = qc->dev;
  1068. unsigned char *sense_buf = qc->scsicmd->sense_buffer;
  1069. struct ata_port *ap = dev->link->ap;
  1070. struct ata_taskfile tf;
  1071. u8 cdb[ATAPI_CDB_LEN];
  1072. DPRINTK("ATAPI request sense\n");
  1073. /* FIXME: is this needed? */
  1074. memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
  1075. /* initialize sense_buf with the error register,
  1076. * for the case where they are -not- overwritten
  1077. */
  1078. sense_buf[0] = 0x70;
  1079. sense_buf[2] = qc->result_tf.feature >> 4;
  1080. /* some devices time out if garbage left in tf */
  1081. ata_tf_init(dev, &tf);
  1082. memset(cdb, 0, ATAPI_CDB_LEN);
  1083. cdb[0] = REQUEST_SENSE;
  1084. cdb[4] = SCSI_SENSE_BUFFERSIZE;
  1085. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  1086. tf.command = ATA_CMD_PACKET;
  1087. /* is it pointless to prefer PIO for "safety reasons"? */
  1088. if (ap->flags & ATA_FLAG_PIO_DMA) {
  1089. tf.protocol = ATAPI_PROT_DMA;
  1090. tf.feature |= ATAPI_PKT_DMA;
  1091. } else {
  1092. tf.protocol = ATAPI_PROT_PIO;
  1093. tf.lbam = SCSI_SENSE_BUFFERSIZE;
  1094. tf.lbah = 0;
  1095. }
  1096. return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
  1097. sense_buf, SCSI_SENSE_BUFFERSIZE, 0);
  1098. }
  1099. /**
  1100. * ata_eh_analyze_serror - analyze SError for a failed port
  1101. * @link: ATA link to analyze SError for
  1102. *
  1103. * Analyze SError if available and further determine cause of
  1104. * failure.
  1105. *
  1106. * LOCKING:
  1107. * None.
  1108. */
  1109. static void ata_eh_analyze_serror(struct ata_link *link)
  1110. {
  1111. struct ata_eh_context *ehc = &link->eh_context;
  1112. u32 serror = ehc->i.serror;
  1113. unsigned int err_mask = 0, action = 0;
  1114. u32 hotplug_mask;
  1115. if (serror & SERR_PERSISTENT) {
  1116. err_mask |= AC_ERR_ATA_BUS;
  1117. action |= ATA_EH_RESET;
  1118. }
  1119. if (serror &
  1120. (SERR_DATA_RECOVERED | SERR_COMM_RECOVERED | SERR_DATA)) {
  1121. err_mask |= AC_ERR_ATA_BUS;
  1122. action |= ATA_EH_RESET;
  1123. }
  1124. if (serror & SERR_PROTOCOL) {
  1125. err_mask |= AC_ERR_HSM;
  1126. action |= ATA_EH_RESET;
  1127. }
  1128. if (serror & SERR_INTERNAL) {
  1129. err_mask |= AC_ERR_SYSTEM;
  1130. action |= ATA_EH_RESET;
  1131. }
  1132. /* Determine whether a hotplug event has occurred. Both
  1133. * SError.N/X are considered hotplug events for enabled or
  1134. * host links. For disabled PMP links, only N bit is
  1135. * considered as X bit is left at 1 for link plugging.
  1136. */
  1137. hotplug_mask = 0;
  1138. if (!(link->flags & ATA_LFLAG_DISABLED) || ata_is_host_link(link))
  1139. hotplug_mask = SERR_PHYRDY_CHG | SERR_DEV_XCHG;
  1140. else
  1141. hotplug_mask = SERR_PHYRDY_CHG;
  1142. if (serror & hotplug_mask)
  1143. ata_ehi_hotplugged(&ehc->i);
  1144. ehc->i.err_mask |= err_mask;
  1145. ehc->i.action |= action;
  1146. }
  1147. /**
  1148. * ata_eh_analyze_ncq_error - analyze NCQ error
  1149. * @link: ATA link to analyze NCQ error for
  1150. *
  1151. * Read log page 10h, determine the offending qc and acquire
  1152. * error status TF. For NCQ device errors, all LLDDs have to do
  1153. * is setting AC_ERR_DEV in ehi->err_mask. This function takes
  1154. * care of the rest.
  1155. *
  1156. * LOCKING:
  1157. * Kernel thread context (may sleep).
  1158. */
  1159. static void ata_eh_analyze_ncq_error(struct ata_link *link)
  1160. {
  1161. struct ata_port *ap = link->ap;
  1162. struct ata_eh_context *ehc = &link->eh_context;
  1163. struct ata_device *dev = link->device;
  1164. struct ata_queued_cmd *qc;
  1165. struct ata_taskfile tf;
  1166. int tag, rc;
  1167. /* if frozen, we can't do much */
  1168. if (ap->pflags & ATA_PFLAG_FROZEN)
  1169. return;
  1170. /* is it NCQ device error? */
  1171. if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
  1172. return;
  1173. /* has LLDD analyzed already? */
  1174. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  1175. qc = __ata_qc_from_tag(ap, tag);
  1176. if (!(qc->flags & ATA_QCFLAG_FAILED))
  1177. continue;
  1178. if (qc->err_mask)
  1179. return;
  1180. }
  1181. /* okay, this error is ours */
  1182. rc = ata_eh_read_log_10h(dev, &tag, &tf);
  1183. if (rc) {
  1184. ata_link_printk(link, KERN_ERR, "failed to read log page 10h "
  1185. "(errno=%d)\n", rc);
  1186. return;
  1187. }
  1188. if (!(link->sactive & (1 << tag))) {
  1189. ata_link_printk(link, KERN_ERR, "log page 10h reported "
  1190. "inactive tag %d\n", tag);
  1191. return;
  1192. }
  1193. /* we've got the perpetrator, condemn it */
  1194. qc = __ata_qc_from_tag(ap, tag);
  1195. memcpy(&qc->result_tf, &tf, sizeof(tf));
  1196. qc->result_tf.flags = ATA_TFLAG_ISADDR | ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
  1197. qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
  1198. ehc->i.err_mask &= ~AC_ERR_DEV;
  1199. }
  1200. /**
  1201. * ata_eh_analyze_tf - analyze taskfile of a failed qc
  1202. * @qc: qc to analyze
  1203. * @tf: Taskfile registers to analyze
  1204. *
  1205. * Analyze taskfile of @qc and further determine cause of
  1206. * failure. This function also requests ATAPI sense data if
  1207. * avaliable.
  1208. *
  1209. * LOCKING:
  1210. * Kernel thread context (may sleep).
  1211. *
  1212. * RETURNS:
  1213. * Determined recovery action
  1214. */
  1215. static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
  1216. const struct ata_taskfile *tf)
  1217. {
  1218. unsigned int tmp, action = 0;
  1219. u8 stat = tf->command, err = tf->feature;
  1220. if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
  1221. qc->err_mask |= AC_ERR_HSM;
  1222. return ATA_EH_RESET;
  1223. }
  1224. if (stat & (ATA_ERR | ATA_DF))
  1225. qc->err_mask |= AC_ERR_DEV;
  1226. else
  1227. return 0;
  1228. switch (qc->dev->class) {
  1229. case ATA_DEV_ATA:
  1230. if (err & ATA_ICRC)
  1231. qc->err_mask |= AC_ERR_ATA_BUS;
  1232. if (err & ATA_UNC)
  1233. qc->err_mask |= AC_ERR_MEDIA;
  1234. if (err & ATA_IDNF)
  1235. qc->err_mask |= AC_ERR_INVALID;
  1236. break;
  1237. case ATA_DEV_ATAPI:
  1238. if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
  1239. tmp = atapi_eh_request_sense(qc);
  1240. if (!tmp) {
  1241. /* ATA_QCFLAG_SENSE_VALID is used to
  1242. * tell atapi_qc_complete() that sense
  1243. * data is already valid.
  1244. *
  1245. * TODO: interpret sense data and set
  1246. * appropriate err_mask.
  1247. */
  1248. qc->flags |= ATA_QCFLAG_SENSE_VALID;
  1249. } else
  1250. qc->err_mask |= tmp;
  1251. }
  1252. }
  1253. if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
  1254. action |= ATA_EH_RESET;
  1255. return action;
  1256. }
  1257. static int ata_eh_categorize_error(unsigned int eflags, unsigned int err_mask,
  1258. int *xfer_ok)
  1259. {
  1260. int base = 0;
  1261. if (!(eflags & ATA_EFLAG_DUBIOUS_XFER))
  1262. *xfer_ok = 1;
  1263. if (!*xfer_ok)
  1264. base = ATA_ECAT_DUBIOUS_NONE;
  1265. if (err_mask & AC_ERR_ATA_BUS)
  1266. return base + ATA_ECAT_ATA_BUS;
  1267. if (err_mask & AC_ERR_TIMEOUT)
  1268. return base + ATA_ECAT_TOUT_HSM;
  1269. if (eflags & ATA_EFLAG_IS_IO) {
  1270. if (err_mask & AC_ERR_HSM)
  1271. return base + ATA_ECAT_TOUT_HSM;
  1272. if ((err_mask &
  1273. (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
  1274. return base + ATA_ECAT_UNK_DEV;
  1275. }
  1276. return 0;
  1277. }
  1278. struct speed_down_verdict_arg {
  1279. u64 since;
  1280. int xfer_ok;
  1281. int nr_errors[ATA_ECAT_NR];
  1282. };
  1283. static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
  1284. {
  1285. struct speed_down_verdict_arg *arg = void_arg;
  1286. int cat;
  1287. if (ent->timestamp < arg->since)
  1288. return -1;
  1289. cat = ata_eh_categorize_error(ent->eflags, ent->err_mask,
  1290. &arg->xfer_ok);
  1291. arg->nr_errors[cat]++;
  1292. return 0;
  1293. }
  1294. /**
  1295. * ata_eh_speed_down_verdict - Determine speed down verdict
  1296. * @dev: Device of interest
  1297. *
  1298. * This function examines error ring of @dev and determines
  1299. * whether NCQ needs to be turned off, transfer speed should be
  1300. * stepped down, or falling back to PIO is necessary.
  1301. *
  1302. * ECAT_ATA_BUS : ATA_BUS error for any command
  1303. *
  1304. * ECAT_TOUT_HSM : TIMEOUT for any command or HSM violation for
  1305. * IO commands
  1306. *
  1307. * ECAT_UNK_DEV : Unknown DEV error for IO commands
  1308. *
  1309. * ECAT_DUBIOUS_* : Identical to above three but occurred while
  1310. * data transfer hasn't been verified.
  1311. *
  1312. * Verdicts are
  1313. *
  1314. * NCQ_OFF : Turn off NCQ.
  1315. *
  1316. * SPEED_DOWN : Speed down transfer speed but don't fall back
  1317. * to PIO.
  1318. *
  1319. * FALLBACK_TO_PIO : Fall back to PIO.
  1320. *
  1321. * Even if multiple verdicts are returned, only one action is
  1322. * taken per error. An action triggered by non-DUBIOUS errors
  1323. * clears ering, while one triggered by DUBIOUS_* errors doesn't.
  1324. * This is to expedite speed down decisions right after device is
  1325. * initially configured.
  1326. *
  1327. * The followings are speed down rules. #1 and #2 deal with
  1328. * DUBIOUS errors.
  1329. *
  1330. * 1. If more than one DUBIOUS_ATA_BUS or DUBIOUS_TOUT_HSM errors
  1331. * occurred during last 5 mins, SPEED_DOWN and FALLBACK_TO_PIO.
  1332. *
  1333. * 2. If more than one DUBIOUS_TOUT_HSM or DUBIOUS_UNK_DEV errors
  1334. * occurred during last 5 mins, NCQ_OFF.
  1335. *
  1336. * 3. If more than 8 ATA_BUS, TOUT_HSM or UNK_DEV errors
  1337. * ocurred during last 5 mins, FALLBACK_TO_PIO
  1338. *
  1339. * 4. If more than 3 TOUT_HSM or UNK_DEV errors occurred
  1340. * during last 10 mins, NCQ_OFF.
  1341. *
  1342. * 5. If more than 3 ATA_BUS or TOUT_HSM errors, or more than 6
  1343. * UNK_DEV errors occurred during last 10 mins, SPEED_DOWN.
  1344. *
  1345. * LOCKING:
  1346. * Inherited from caller.
  1347. *
  1348. * RETURNS:
  1349. * OR of ATA_EH_SPDN_* flags.
  1350. */
  1351. static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
  1352. {
  1353. const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
  1354. u64 j64 = get_jiffies_64();
  1355. struct speed_down_verdict_arg arg;
  1356. unsigned int verdict = 0;
  1357. /* scan past 5 mins of error history */
  1358. memset(&arg, 0, sizeof(arg));
  1359. arg.since = j64 - min(j64, j5mins);
  1360. ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
  1361. if (arg.nr_errors[ATA_ECAT_DUBIOUS_ATA_BUS] +
  1362. arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] > 1)
  1363. verdict |= ATA_EH_SPDN_SPEED_DOWN |
  1364. ATA_EH_SPDN_FALLBACK_TO_PIO | ATA_EH_SPDN_KEEP_ERRORS;
  1365. if (arg.nr_errors[ATA_ECAT_DUBIOUS_TOUT_HSM] +
  1366. arg.nr_errors[ATA_ECAT_DUBIOUS_UNK_DEV] > 1)
  1367. verdict |= ATA_EH_SPDN_NCQ_OFF | ATA_EH_SPDN_KEEP_ERRORS;
  1368. if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
  1369. arg.nr_errors[ATA_ECAT_TOUT_HSM] +
  1370. arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
  1371. verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
  1372. /* scan past 10 mins of error history */
  1373. memset(&arg, 0, sizeof(arg));
  1374. arg.since = j64 - min(j64, j10mins);
  1375. ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
  1376. if (arg.nr_errors[ATA_ECAT_TOUT_HSM] +
  1377. arg.nr_errors[ATA_ECAT_UNK_DEV] > 3)
  1378. verdict |= ATA_EH_SPDN_NCQ_OFF;
  1379. if (arg.nr_errors[ATA_ECAT_ATA_BUS] +
  1380. arg.nr_errors[ATA_ECAT_TOUT_HSM] > 3 ||
  1381. arg.nr_errors[ATA_ECAT_UNK_DEV] > 6)
  1382. verdict |= ATA_EH_SPDN_SPEED_DOWN;
  1383. return verdict;
  1384. }
  1385. /**
  1386. * ata_eh_speed_down - record error and speed down if necessary
  1387. * @dev: Failed device
  1388. * @eflags: mask of ATA_EFLAG_* flags
  1389. * @err_mask: err_mask of the error
  1390. *
  1391. * Record error and examine error history to determine whether
  1392. * adjusting transmission speed is necessary. It also sets
  1393. * transmission limits appropriately if such adjustment is
  1394. * necessary.
  1395. *
  1396. * LOCKING:
  1397. * Kernel thread context (may sleep).
  1398. *
  1399. * RETURNS:
  1400. * Determined recovery action.
  1401. */
  1402. static unsigned int ata_eh_speed_down(struct ata_device *dev,
  1403. unsigned int eflags, unsigned int err_mask)
  1404. {
  1405. struct ata_link *link = dev->link;
  1406. int xfer_ok = 0;
  1407. unsigned int verdict;
  1408. unsigned int action = 0;
  1409. /* don't bother if Cat-0 error */
  1410. if (ata_eh_categorize_error(eflags, err_mask, &xfer_ok) == 0)
  1411. return 0;
  1412. /* record error and determine whether speed down is necessary */
  1413. ata_ering_record(&dev->ering, eflags, err_mask);
  1414. verdict = ata_eh_speed_down_verdict(dev);
  1415. /* turn off NCQ? */
  1416. if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
  1417. (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
  1418. ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
  1419. dev->flags |= ATA_DFLAG_NCQ_OFF;
  1420. ata_dev_printk(dev, KERN_WARNING,
  1421. "NCQ disabled due to excessive errors\n");
  1422. goto done;
  1423. }
  1424. /* speed down? */
  1425. if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
  1426. /* speed down SATA link speed if possible */
  1427. if (sata_down_spd_limit(link) == 0) {
  1428. action |= ATA_EH_RESET;
  1429. goto done;
  1430. }
  1431. /* lower transfer mode */
  1432. if (dev->spdn_cnt < 2) {
  1433. static const int dma_dnxfer_sel[] =
  1434. { ATA_DNXFER_DMA, ATA_DNXFER_40C };
  1435. static const int pio_dnxfer_sel[] =
  1436. { ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
  1437. int sel;
  1438. if (dev->xfer_shift != ATA_SHIFT_PIO)
  1439. sel = dma_dnxfer_sel[dev->spdn_cnt];
  1440. else
  1441. sel = pio_dnxfer_sel[dev->spdn_cnt];
  1442. dev->spdn_cnt++;
  1443. if (ata_down_xfermask_limit(dev, sel) == 0) {
  1444. action |= ATA_EH_RESET;
  1445. goto done;
  1446. }
  1447. }
  1448. }
  1449. /* Fall back to PIO? Slowing down to PIO is meaningless for
  1450. * SATA ATA devices. Consider it only for PATA and SATAPI.
  1451. */
  1452. if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
  1453. (link->ap->cbl != ATA_CBL_SATA || dev->class == ATA_DEV_ATAPI) &&
  1454. (dev->xfer_shift != ATA_SHIFT_PIO)) {
  1455. if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
  1456. dev->spdn_cnt = 0;
  1457. action |= ATA_EH_RESET;
  1458. goto done;
  1459. }
  1460. }
  1461. return 0;
  1462. done:
  1463. /* device has been slowed down, blow error history */
  1464. if (!(verdict & ATA_EH_SPDN_KEEP_ERRORS))
  1465. ata_ering_clear(&dev->ering);
  1466. return action;
  1467. }
  1468. /**
  1469. * ata_eh_link_autopsy - analyze error and determine recovery action
  1470. * @link: host link to perform autopsy on
  1471. *
  1472. * Analyze why @link failed and determine which recovery actions
  1473. * are needed. This function also sets more detailed AC_ERR_*
  1474. * values and fills sense data for ATAPI CHECK SENSE.
  1475. *
  1476. * LOCKING:
  1477. * Kernel thread context (may sleep).
  1478. */
  1479. static void ata_eh_link_autopsy(struct ata_link *link)
  1480. {
  1481. struct ata_port *ap = link->ap;
  1482. struct ata_eh_context *ehc = &link->eh_context;
  1483. struct ata_device *dev;
  1484. unsigned int all_err_mask = 0, eflags = 0;
  1485. int tag;
  1486. u32 serror;
  1487. int rc;
  1488. DPRINTK("ENTER\n");
  1489. if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
  1490. return;
  1491. /* obtain and analyze SError */
  1492. rc = sata_scr_read(link, SCR_ERROR, &serror);
  1493. if (rc == 0) {
  1494. ehc->i.serror |= serror;
  1495. ata_eh_analyze_serror(link);
  1496. } else if (rc != -EOPNOTSUPP) {
  1497. /* SError read failed, force reset and probing */
  1498. ehc->i.probe_mask |= ATA_ALL_DEVICES;
  1499. ehc->i.action |= ATA_EH_RESET;
  1500. ehc->i.err_mask |= AC_ERR_OTHER;
  1501. }
  1502. /* analyze NCQ failure */
  1503. ata_eh_analyze_ncq_error(link);
  1504. /* any real error trumps AC_ERR_OTHER */
  1505. if (ehc->i.err_mask & ~AC_ERR_OTHER)
  1506. ehc->i.err_mask &= ~AC_ERR_OTHER;
  1507. all_err_mask |= ehc->i.err_mask;
  1508. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  1509. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  1510. if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link)
  1511. continue;
  1512. /* inherit upper level err_mask */
  1513. qc->err_mask |= ehc->i.err_mask;
  1514. /* analyze TF */
  1515. ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
  1516. /* DEV errors are probably spurious in case of ATA_BUS error */
  1517. if (qc->err_mask & AC_ERR_ATA_BUS)
  1518. qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
  1519. AC_ERR_INVALID);
  1520. /* any real error trumps unknown error */
  1521. if (qc->err_mask & ~AC_ERR_OTHER)
  1522. qc->err_mask &= ~AC_ERR_OTHER;
  1523. /* SENSE_VALID trumps dev/unknown error and revalidation */
  1524. if (qc->flags & ATA_QCFLAG_SENSE_VALID)
  1525. qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
  1526. /* determine whether the command is worth retrying */
  1527. if (!(qc->err_mask & AC_ERR_INVALID) &&
  1528. ((qc->flags & ATA_QCFLAG_IO) || qc->err_mask != AC_ERR_DEV))
  1529. qc->flags |= ATA_QCFLAG_RETRY;
  1530. /* accumulate error info */
  1531. ehc->i.dev = qc->dev;
  1532. all_err_mask |= qc->err_mask;
  1533. if (qc->flags & ATA_QCFLAG_IO)
  1534. eflags |= ATA_EFLAG_IS_IO;
  1535. }
  1536. /* enforce default EH actions */
  1537. if (ap->pflags & ATA_PFLAG_FROZEN ||
  1538. all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
  1539. ehc->i.action |= ATA_EH_RESET;
  1540. else if (((eflags & ATA_EFLAG_IS_IO) && all_err_mask) ||
  1541. (!(eflags & ATA_EFLAG_IS_IO) && (all_err_mask & ~AC_ERR_DEV)))
  1542. ehc->i.action |= ATA_EH_REVALIDATE;
  1543. /* If we have offending qcs and the associated failed device,
  1544. * perform per-dev EH action only on the offending device.
  1545. */
  1546. if (ehc->i.dev) {
  1547. ehc->i.dev_action[ehc->i.dev->devno] |=
  1548. ehc->i.action & ATA_EH_PERDEV_MASK;
  1549. ehc->i.action &= ~ATA_EH_PERDEV_MASK;
  1550. }
  1551. /* propagate timeout to host link */
  1552. if ((all_err_mask & AC_ERR_TIMEOUT) && !ata_is_host_link(link))
  1553. ap->link.eh_context.i.err_mask |= AC_ERR_TIMEOUT;
  1554. /* record error and consider speeding down */
  1555. dev = ehc->i.dev;
  1556. if (!dev && ((ata_link_max_devices(link) == 1 &&
  1557. ata_dev_enabled(link->device))))
  1558. dev = link->device;
  1559. if (dev) {
  1560. if (dev->flags & ATA_DFLAG_DUBIOUS_XFER)
  1561. eflags |= ATA_EFLAG_DUBIOUS_XFER;
  1562. ehc->i.action |= ata_eh_speed_down(dev, eflags, all_err_mask);
  1563. }
  1564. DPRINTK("EXIT\n");
  1565. }
  1566. /**
  1567. * ata_eh_autopsy - analyze error and determine recovery action
  1568. * @ap: host port to perform autopsy on
  1569. *
  1570. * Analyze all links of @ap and determine why they failed and
  1571. * which recovery actions are needed.
  1572. *
  1573. * LOCKING:
  1574. * Kernel thread context (may sleep).
  1575. */
  1576. void ata_eh_autopsy(struct ata_port *ap)
  1577. {
  1578. struct ata_link *link;
  1579. ata_port_for_each_link(link, ap)
  1580. ata_eh_link_autopsy(link);
  1581. /* Autopsy of fanout ports can affect host link autopsy.
  1582. * Perform host link autopsy last.
  1583. */
  1584. if (sata_pmp_attached(ap))
  1585. ata_eh_link_autopsy(&ap->link);
  1586. }
  1587. /**
  1588. * ata_eh_link_report - report error handling to user
  1589. * @link: ATA link EH is going on
  1590. *
  1591. * Report EH to user.
  1592. *
  1593. * LOCKING:
  1594. * None.
  1595. */
  1596. static void ata_eh_link_report(struct ata_link *link)
  1597. {
  1598. struct ata_port *ap = link->ap;
  1599. struct ata_eh_context *ehc = &link->eh_context;
  1600. const char *frozen, *desc;
  1601. char tries_buf[6];
  1602. int tag, nr_failed = 0;
  1603. if (ehc->i.flags & ATA_EHI_QUIET)
  1604. return;
  1605. desc = NULL;
  1606. if (ehc->i.desc[0] != '\0')
  1607. desc = ehc->i.desc;
  1608. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  1609. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  1610. if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link ||
  1611. ((qc->flags & ATA_QCFLAG_QUIET) &&
  1612. qc->err_mask == AC_ERR_DEV))
  1613. continue;
  1614. if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
  1615. continue;
  1616. nr_failed++;
  1617. }
  1618. if (!nr_failed && !ehc->i.err_mask)
  1619. return;
  1620. frozen = "";
  1621. if (ap->pflags & ATA_PFLAG_FROZEN)
  1622. frozen = " frozen";
  1623. memset(tries_buf, 0, sizeof(tries_buf));
  1624. if (ap->eh_tries < ATA_EH_MAX_TRIES)
  1625. snprintf(tries_buf, sizeof(tries_buf) - 1, " t%d",
  1626. ap->eh_tries);
  1627. if (ehc->i.dev) {
  1628. ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
  1629. "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
  1630. ehc->i.err_mask, link->sactive, ehc->i.serror,
  1631. ehc->i.action, frozen, tries_buf);
  1632. if (desc)
  1633. ata_dev_printk(ehc->i.dev, KERN_ERR, "%s\n", desc);
  1634. } else {
  1635. ata_link_printk(link, KERN_ERR, "exception Emask 0x%x "
  1636. "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
  1637. ehc->i.err_mask, link->sactive, ehc->i.serror,
  1638. ehc->i.action, frozen, tries_buf);
  1639. if (desc)
  1640. ata_link_printk(link, KERN_ERR, "%s\n", desc);
  1641. }
  1642. if (ehc->i.serror)
  1643. ata_port_printk(ap, KERN_ERR,
  1644. "SError: { %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s}\n",
  1645. ehc->i.serror & SERR_DATA_RECOVERED ? "RecovData " : "",
  1646. ehc->i.serror & SERR_COMM_RECOVERED ? "RecovComm " : "",
  1647. ehc->i.serror & SERR_DATA ? "UnrecovData " : "",
  1648. ehc->i.serror & SERR_PERSISTENT ? "Persist " : "",
  1649. ehc->i.serror & SERR_PROTOCOL ? "Proto " : "",
  1650. ehc->i.serror & SERR_INTERNAL ? "HostInt " : "",
  1651. ehc->i.serror & SERR_PHYRDY_CHG ? "PHYRdyChg " : "",
  1652. ehc->i.serror & SERR_PHY_INT_ERR ? "PHYInt " : "",
  1653. ehc->i.serror & SERR_COMM_WAKE ? "CommWake " : "",
  1654. ehc->i.serror & SERR_10B_8B_ERR ? "10B8B " : "",
  1655. ehc->i.serror & SERR_DISPARITY ? "Dispar " : "",
  1656. ehc->i.serror & SERR_CRC ? "BadCRC " : "",
  1657. ehc->i.serror & SERR_HANDSHAKE ? "Handshk " : "",
  1658. ehc->i.serror & SERR_LINK_SEQ_ERR ? "LinkSeq " : "",
  1659. ehc->i.serror & SERR_TRANS_ST_ERROR ? "TrStaTrns " : "",
  1660. ehc->i.serror & SERR_UNRECOG_FIS ? "UnrecFIS " : "",
  1661. ehc->i.serror & SERR_DEV_XCHG ? "DevExch " : "");
  1662. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  1663. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  1664. struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
  1665. const u8 *cdb = qc->cdb;
  1666. char data_buf[20] = "";
  1667. char cdb_buf[70] = "";
  1668. if (!(qc->flags & ATA_QCFLAG_FAILED) ||
  1669. qc->dev->link != link || !qc->err_mask)
  1670. continue;
  1671. if (qc->dma_dir != DMA_NONE) {
  1672. static const char *dma_str[] = {
  1673. [DMA_BIDIRECTIONAL] = "bidi",
  1674. [DMA_TO_DEVICE] = "out",
  1675. [DMA_FROM_DEVICE] = "in",
  1676. };
  1677. static const char *prot_str[] = {
  1678. [ATA_PROT_PIO] = "pio",
  1679. [ATA_PROT_DMA] = "dma",
  1680. [ATA_PROT_NCQ] = "ncq",
  1681. [ATAPI_PROT_PIO] = "pio",
  1682. [ATAPI_PROT_DMA] = "dma",
  1683. };
  1684. snprintf(data_buf, sizeof(data_buf), " %s %u %s",
  1685. prot_str[qc->tf.protocol], qc->nbytes,
  1686. dma_str[qc->dma_dir]);
  1687. }
  1688. if (ata_is_atapi(qc->tf.protocol))
  1689. snprintf(cdb_buf, sizeof(cdb_buf),
  1690. "cdb %02x %02x %02x %02x %02x %02x %02x %02x "
  1691. "%02x %02x %02x %02x %02x %02x %02x %02x\n ",
  1692. cdb[0], cdb[1], cdb[2], cdb[3],
  1693. cdb[4], cdb[5], cdb[6], cdb[7],
  1694. cdb[8], cdb[9], cdb[10], cdb[11],
  1695. cdb[12], cdb[13], cdb[14], cdb[15]);
  1696. ata_dev_printk(qc->dev, KERN_ERR,
  1697. "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
  1698. "tag %d%s\n %s"
  1699. "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
  1700. "Emask 0x%x (%s)%s\n",
  1701. cmd->command, cmd->feature, cmd->nsect,
  1702. cmd->lbal, cmd->lbam, cmd->lbah,
  1703. cmd->hob_feature, cmd->hob_nsect,
  1704. cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
  1705. cmd->device, qc->tag, data_buf, cdb_buf,
  1706. res->command, res->feature, res->nsect,
  1707. res->lbal, res->lbam, res->lbah,
  1708. res->hob_feature, res->hob_nsect,
  1709. res->hob_lbal, res->hob_lbam, res->hob_lbah,
  1710. res->device, qc->err_mask, ata_err_string(qc->err_mask),
  1711. qc->err_mask & AC_ERR_NCQ ? " <F>" : "");
  1712. if (res->command & (ATA_BUSY | ATA_DRDY | ATA_DF | ATA_DRQ |
  1713. ATA_ERR)) {
  1714. if (res->command & ATA_BUSY)
  1715. ata_dev_printk(qc->dev, KERN_ERR,
  1716. "status: { Busy }\n");
  1717. else
  1718. ata_dev_printk(qc->dev, KERN_ERR,
  1719. "status: { %s%s%s%s}\n",
  1720. res->command & ATA_DRDY ? "DRDY " : "",
  1721. res->command & ATA_DF ? "DF " : "",
  1722. res->command & ATA_DRQ ? "DRQ " : "",
  1723. res->command & ATA_ERR ? "ERR " : "");
  1724. }
  1725. if (cmd->command != ATA_CMD_PACKET &&
  1726. (res->feature & (ATA_ICRC | ATA_UNC | ATA_IDNF |
  1727. ATA_ABORTED)))
  1728. ata_dev_printk(qc->dev, KERN_ERR,
  1729. "error: { %s%s%s%s}\n",
  1730. res->feature & ATA_ICRC ? "ICRC " : "",
  1731. res->feature & ATA_UNC ? "UNC " : "",
  1732. res->feature & ATA_IDNF ? "IDNF " : "",
  1733. res->feature & ATA_ABORTED ? "ABRT " : "");
  1734. }
  1735. }
  1736. /**
  1737. * ata_eh_report - report error handling to user
  1738. * @ap: ATA port to report EH about
  1739. *
  1740. * Report EH to user.
  1741. *
  1742. * LOCKING:
  1743. * None.
  1744. */
  1745. void ata_eh_report(struct ata_port *ap)
  1746. {
  1747. struct ata_link *link;
  1748. __ata_port_for_each_link(link, ap)
  1749. ata_eh_link_report(link);
  1750. }
  1751. static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset,
  1752. unsigned int *classes, unsigned long deadline)
  1753. {
  1754. struct ata_device *dev;
  1755. int rc;
  1756. ata_link_for_each_dev(dev, link)
  1757. classes[dev->devno] = ATA_DEV_UNKNOWN;
  1758. rc = reset(link, classes, deadline);
  1759. /* convert all ATA_DEV_UNKNOWN to ATA_DEV_NONE */
  1760. ata_link_for_each_dev(dev, link)
  1761. if (classes[dev->devno] == ATA_DEV_UNKNOWN)
  1762. classes[dev->devno] = ATA_DEV_NONE;
  1763. return rc;
  1764. }
  1765. static int ata_eh_followup_srst_needed(struct ata_link *link,
  1766. int rc, int classify,
  1767. const unsigned int *classes)
  1768. {
  1769. if ((link->flags & ATA_LFLAG_NO_SRST) || ata_link_offline(link))
  1770. return 0;
  1771. if (rc == -EAGAIN) {
  1772. if (classify)
  1773. return 1;
  1774. rc = 0;
  1775. }
  1776. if (rc != 0)
  1777. return 0;
  1778. if (sata_pmp_supported(link->ap) && ata_is_host_link(link))
  1779. return 1;
  1780. return 0;
  1781. }
  1782. int ata_eh_reset(struct ata_link *link, int classify,
  1783. ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
  1784. ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
  1785. {
  1786. const int max_tries = ARRAY_SIZE(ata_eh_reset_timeouts);
  1787. struct ata_port *ap = link->ap;
  1788. struct ata_eh_context *ehc = &link->eh_context;
  1789. unsigned int *classes = ehc->classes;
  1790. unsigned int lflags = link->flags;
  1791. int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
  1792. int try = 0;
  1793. struct ata_device *dev;
  1794. unsigned long deadline, now;
  1795. ata_reset_fn_t reset;
  1796. unsigned long flags;
  1797. u32 sstatus;
  1798. int rc;
  1799. /* about to reset */
  1800. spin_lock_irqsave(ap->lock, flags);
  1801. ap->pflags |= ATA_PFLAG_RESETTING;
  1802. spin_unlock_irqrestore(ap->lock, flags);
  1803. ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
  1804. ata_link_for_each_dev(dev, link) {
  1805. /* If we issue an SRST then an ATA drive (not ATAPI)
  1806. * may change configuration and be in PIO0 timing. If
  1807. * we do a hard reset (or are coming from power on)
  1808. * this is true for ATA or ATAPI. Until we've set a
  1809. * suitable controller mode we should not touch the
  1810. * bus as we may be talking too fast.
  1811. */
  1812. dev->pio_mode = XFER_PIO_0;
  1813. /* If the controller has a pio mode setup function
  1814. * then use it to set the chipset to rights. Don't
  1815. * touch the DMA setup as that will be dealt with when
  1816. * configuring devices.
  1817. */
  1818. if (ap->ops->set_piomode)
  1819. ap->ops->set_piomode(ap, dev);
  1820. }
  1821. if (!softreset && !hardreset) {
  1822. if (verbose)
  1823. ata_link_printk(link, KERN_INFO, "no reset method "
  1824. "available, skipping reset\n");
  1825. if (!(lflags & ATA_LFLAG_ASSUME_CLASS))
  1826. lflags |= ATA_LFLAG_ASSUME_ATA;
  1827. goto done;
  1828. }
  1829. /* prefer hardreset */
  1830. ehc->i.action &= ~ATA_EH_RESET;
  1831. if (hardreset) {
  1832. reset = hardreset;
  1833. ehc->i.action = ATA_EH_HARDRESET;
  1834. } else if (softreset) {
  1835. reset = softreset;
  1836. ehc->i.action = ATA_EH_SOFTRESET;
  1837. } else {
  1838. ata_link_printk(link, KERN_ERR, "BUG: no reset method, "
  1839. "please report to linux-ide@vger.kernel.org\n");
  1840. dump_stack();
  1841. return -EINVAL;
  1842. }
  1843. if (prereset) {
  1844. rc = prereset(link, jiffies + ATA_EH_PRERESET_TIMEOUT);
  1845. if (rc) {
  1846. if (rc == -ENOENT) {
  1847. ata_link_printk(link, KERN_DEBUG,
  1848. "port disabled. ignoring.\n");
  1849. ehc->i.action &= ~ATA_EH_RESET;
  1850. ata_link_for_each_dev(dev, link)
  1851. classes[dev->devno] = ATA_DEV_NONE;
  1852. rc = 0;
  1853. } else
  1854. ata_link_printk(link, KERN_ERR,
  1855. "prereset failed (errno=%d)\n", rc);
  1856. goto out;
  1857. }
  1858. }
  1859. /* prereset() might have cleared ATA_EH_RESET */
  1860. if (!(ehc->i.action & ATA_EH_RESET)) {
  1861. /* prereset told us not to reset, bang classes and return */
  1862. ata_link_for_each_dev(dev, link)
  1863. classes[dev->devno] = ATA_DEV_NONE;
  1864. rc = 0;
  1865. goto out;
  1866. }
  1867. retry:
  1868. deadline = jiffies + ata_eh_reset_timeouts[try++];
  1869. /* shut up during boot probing */
  1870. if (verbose)
  1871. ata_link_printk(link, KERN_INFO, "%s resetting link\n",
  1872. reset == softreset ? "soft" : "hard");
  1873. /* mark that this EH session started with reset */
  1874. if (reset == hardreset)
  1875. ehc->i.flags |= ATA_EHI_DID_HARDRESET;
  1876. else
  1877. ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
  1878. rc = ata_do_reset(link, reset, classes, deadline);
  1879. if (reset == hardreset &&
  1880. ata_eh_followup_srst_needed(link, rc, classify, classes)) {
  1881. /* okay, let's do follow-up softreset */
  1882. reset = softreset;
  1883. if (!reset) {
  1884. ata_link_printk(link, KERN_ERR,
  1885. "follow-up softreset required "
  1886. "but no softreset avaliable\n");
  1887. rc = -EINVAL;
  1888. goto fail;
  1889. }
  1890. ata_eh_about_to_do(link, NULL, ATA_EH_RESET);
  1891. rc = ata_do_reset(link, reset, classes, deadline);
  1892. }
  1893. /* -EAGAIN can happen if we skipped followup SRST */
  1894. if (rc && rc != -EAGAIN)
  1895. goto fail;
  1896. done:
  1897. ata_link_for_each_dev(dev, link) {
  1898. /* After the reset, the device state is PIO 0 and the
  1899. * controller state is undefined. Reset also wakes up
  1900. * drives from sleeping mode.
  1901. */
  1902. dev->pio_mode = XFER_PIO_0;
  1903. dev->flags &= ~ATA_DFLAG_SLEEPING;
  1904. if (ata_link_offline(link))
  1905. continue;
  1906. /* apply class override */
  1907. if (lflags & ATA_LFLAG_ASSUME_ATA)
  1908. classes[dev->devno] = ATA_DEV_ATA;
  1909. else if (lflags & ATA_LFLAG_ASSUME_SEMB)
  1910. classes[dev->devno] = ATA_DEV_SEMB_UNSUP; /* not yet */
  1911. }
  1912. /* record current link speed */
  1913. if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0)
  1914. link->sata_spd = (sstatus >> 4) & 0xf;
  1915. if (postreset)
  1916. postreset(link, classes);
  1917. /* reset successful, schedule revalidation */
  1918. ata_eh_done(link, NULL, ATA_EH_RESET);
  1919. ehc->i.action |= ATA_EH_REVALIDATE;
  1920. rc = 0;
  1921. out:
  1922. /* clear hotplug flag */
  1923. ehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
  1924. spin_lock_irqsave(ap->lock, flags);
  1925. ap->pflags &= ~ATA_PFLAG_RESETTING;
  1926. spin_unlock_irqrestore(ap->lock, flags);
  1927. return rc;
  1928. fail:
  1929. /* if SCR isn't accessible on a fan-out port, PMP needs to be reset */
  1930. if (!ata_is_host_link(link) &&
  1931. sata_scr_read(link, SCR_STATUS, &sstatus))
  1932. rc = -ERESTART;
  1933. if (rc == -ERESTART || try >= max_tries)
  1934. goto out;
  1935. now = jiffies;
  1936. if (time_before(now, deadline)) {
  1937. unsigned long delta = deadline - now;
  1938. ata_link_printk(link, KERN_WARNING, "reset failed "
  1939. "(errno=%d), retrying in %u secs\n",
  1940. rc, (jiffies_to_msecs(delta) + 999) / 1000);
  1941. while (delta)
  1942. delta = schedule_timeout_uninterruptible(delta);
  1943. }
  1944. if (rc == -EPIPE || try == max_tries - 1)
  1945. sata_down_spd_limit(link);
  1946. if (hardreset)
  1947. reset = hardreset;
  1948. goto retry;
  1949. }
  1950. static int ata_eh_revalidate_and_attach(struct ata_link *link,
  1951. struct ata_device **r_failed_dev)
  1952. {
  1953. struct ata_port *ap = link->ap;
  1954. struct ata_eh_context *ehc = &link->eh_context;
  1955. struct ata_device *dev;
  1956. unsigned int new_mask = 0;
  1957. unsigned long flags;
  1958. int rc = 0;
  1959. DPRINTK("ENTER\n");
  1960. /* For PATA drive side cable detection to work, IDENTIFY must
  1961. * be done backwards such that PDIAG- is released by the slave
  1962. * device before the master device is identified.
  1963. */
  1964. ata_link_for_each_dev_reverse(dev, link) {
  1965. unsigned int action = ata_eh_dev_action(dev);
  1966. unsigned int readid_flags = 0;
  1967. if (ehc->i.flags & ATA_EHI_DID_RESET)
  1968. readid_flags |= ATA_READID_POSTRESET;
  1969. if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
  1970. WARN_ON(dev->class == ATA_DEV_PMP);
  1971. if (ata_link_offline(link)) {
  1972. rc = -EIO;
  1973. goto err;
  1974. }
  1975. ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE);
  1976. rc = ata_dev_revalidate(dev, ehc->classes[dev->devno],
  1977. readid_flags);
  1978. if (rc)
  1979. goto err;
  1980. ata_eh_done(link, dev, ATA_EH_REVALIDATE);
  1981. /* Configuration may have changed, reconfigure
  1982. * transfer mode.
  1983. */
  1984. ehc->i.flags |= ATA_EHI_SETMODE;
  1985. /* schedule the scsi_rescan_device() here */
  1986. queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
  1987. } else if (dev->class == ATA_DEV_UNKNOWN &&
  1988. ehc->tries[dev->devno] &&
  1989. ata_class_enabled(ehc->classes[dev->devno])) {
  1990. dev->class = ehc->classes[dev->devno];
  1991. if (dev->class == ATA_DEV_PMP)
  1992. rc = sata_pmp_attach(dev);
  1993. else
  1994. rc = ata_dev_read_id(dev, &dev->class,
  1995. readid_flags, dev->id);
  1996. switch (rc) {
  1997. case 0:
  1998. new_mask |= 1 << dev->devno;
  1999. break;
  2000. case -ENOENT:
  2001. /* IDENTIFY was issued to non-existent
  2002. * device. No need to reset. Just
  2003. * thaw and kill the device.
  2004. */
  2005. ata_eh_thaw_port(ap);
  2006. dev->class = ATA_DEV_UNKNOWN;
  2007. break;
  2008. default:
  2009. dev->class = ATA_DEV_UNKNOWN;
  2010. goto err;
  2011. }
  2012. }
  2013. }
  2014. /* PDIAG- should have been released, ask cable type if post-reset */
  2015. if ((ehc->i.flags & ATA_EHI_DID_RESET) && ata_is_host_link(link)) {
  2016. if (ap->ops->cable_detect)
  2017. ap->cbl = ap->ops->cable_detect(ap);
  2018. ata_force_cbl(ap);
  2019. }
  2020. /* Configure new devices forward such that user doesn't see
  2021. * device detection messages backwards.
  2022. */
  2023. ata_link_for_each_dev(dev, link) {
  2024. if (!(new_mask & (1 << dev->devno)) ||
  2025. dev->class == ATA_DEV_PMP)
  2026. continue;
  2027. ehc->i.flags |= ATA_EHI_PRINTINFO;
  2028. rc = ata_dev_configure(dev);
  2029. ehc->i.flags &= ~ATA_EHI_PRINTINFO;
  2030. if (rc)
  2031. goto err;
  2032. spin_lock_irqsave(ap->lock, flags);
  2033. ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
  2034. spin_unlock_irqrestore(ap->lock, flags);
  2035. /* new device discovered, configure xfermode */
  2036. ehc->i.flags |= ATA_EHI_SETMODE;
  2037. }
  2038. return 0;
  2039. err:
  2040. *r_failed_dev = dev;
  2041. DPRINTK("EXIT rc=%d\n", rc);
  2042. return rc;
  2043. }
  2044. /**
  2045. * ata_set_mode - Program timings and issue SET FEATURES - XFER
  2046. * @link: link on which timings will be programmed
  2047. * @r_failed_dev: out paramter for failed device
  2048. *
  2049. * Set ATA device disk transfer mode (PIO3, UDMA6, etc.). If
  2050. * ata_set_mode() fails, pointer to the failing device is
  2051. * returned in @r_failed_dev.
  2052. *
  2053. * LOCKING:
  2054. * PCI/etc. bus probe sem.
  2055. *
  2056. * RETURNS:
  2057. * 0 on success, negative errno otherwise
  2058. */
  2059. int ata_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
  2060. {
  2061. struct ata_port *ap = link->ap;
  2062. struct ata_device *dev;
  2063. int rc;
  2064. /* if data transfer is verified, clear DUBIOUS_XFER on ering top */
  2065. ata_link_for_each_dev(dev, link) {
  2066. if (!(dev->flags & ATA_DFLAG_DUBIOUS_XFER)) {
  2067. struct ata_ering_entry *ent;
  2068. ent = ata_ering_top(&dev->ering);
  2069. if (ent)
  2070. ent->eflags &= ~ATA_EFLAG_DUBIOUS_XFER;
  2071. }
  2072. }
  2073. /* has private set_mode? */
  2074. if (ap->ops->set_mode)
  2075. rc = ap->ops->set_mode(link, r_failed_dev);
  2076. else
  2077. rc = ata_do_set_mode(link, r_failed_dev);
  2078. /* if transfer mode has changed, set DUBIOUS_XFER on device */
  2079. ata_link_for_each_dev(dev, link) {
  2080. struct ata_eh_context *ehc = &link->eh_context;
  2081. u8 saved_xfer_mode = ehc->saved_xfer_mode[dev->devno];
  2082. u8 saved_ncq = !!(ehc->saved_ncq_enabled & (1 << dev->devno));
  2083. if (dev->xfer_mode != saved_xfer_mode ||
  2084. ata_ncq_enabled(dev) != saved_ncq)
  2085. dev->flags |= ATA_DFLAG_DUBIOUS_XFER;
  2086. }
  2087. return rc;
  2088. }
  2089. static int ata_link_nr_enabled(struct ata_link *link)
  2090. {
  2091. struct ata_device *dev;
  2092. int cnt = 0;
  2093. ata_link_for_each_dev(dev, link)
  2094. if (ata_dev_enabled(dev))
  2095. cnt++;
  2096. return cnt;
  2097. }
  2098. static int ata_link_nr_vacant(struct ata_link *link)
  2099. {
  2100. struct ata_device *dev;
  2101. int cnt = 0;
  2102. ata_link_for_each_dev(dev, link)
  2103. if (dev->class == ATA_DEV_UNKNOWN)
  2104. cnt++;
  2105. return cnt;
  2106. }
  2107. static int ata_eh_skip_recovery(struct ata_link *link)
  2108. {
  2109. struct ata_port *ap = link->ap;
  2110. struct ata_eh_context *ehc = &link->eh_context;
  2111. struct ata_device *dev;
  2112. /* skip disabled links */
  2113. if (link->flags & ATA_LFLAG_DISABLED)
  2114. return 1;
  2115. /* thaw frozen port and recover failed devices */
  2116. if ((ap->pflags & ATA_PFLAG_FROZEN) || ata_link_nr_enabled(link))
  2117. return 0;
  2118. /* reset at least once if reset is requested */
  2119. if ((ehc->i.action & ATA_EH_RESET) &&
  2120. !(ehc->i.flags & ATA_EHI_DID_RESET))
  2121. return 0;
  2122. /* skip if class codes for all vacant slots are ATA_DEV_NONE */
  2123. ata_link_for_each_dev(dev, link) {
  2124. if (dev->class == ATA_DEV_UNKNOWN &&
  2125. ehc->classes[dev->devno] != ATA_DEV_NONE)
  2126. return 0;
  2127. }
  2128. return 1;
  2129. }
  2130. static int ata_eh_schedule_probe(struct ata_device *dev)
  2131. {
  2132. struct ata_eh_context *ehc = &dev->link->eh_context;
  2133. if (!(ehc->i.probe_mask & (1 << dev->devno)) ||
  2134. (ehc->did_probe_mask & (1 << dev->devno)))
  2135. return 0;
  2136. ata_eh_detach_dev(dev);
  2137. ata_dev_init(dev);
  2138. ehc->did_probe_mask |= (1 << dev->devno);
  2139. ehc->i.action |= ATA_EH_RESET;
  2140. ehc->saved_xfer_mode[dev->devno] = 0;
  2141. ehc->saved_ncq_enabled &= ~(1 << dev->devno);
  2142. return 1;
  2143. }
  2144. static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
  2145. {
  2146. struct ata_eh_context *ehc = &dev->link->eh_context;
  2147. ehc->tries[dev->devno]--;
  2148. switch (err) {
  2149. case -ENODEV:
  2150. /* device missing or wrong IDENTIFY data, schedule probing */
  2151. ehc->i.probe_mask |= (1 << dev->devno);
  2152. case -EINVAL:
  2153. /* give it just one more chance */
  2154. ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
  2155. case -EIO:
  2156. if (ehc->tries[dev->devno] == 1 && dev->pio_mode > XFER_PIO_0) {
  2157. /* This is the last chance, better to slow
  2158. * down than lose it.
  2159. */
  2160. sata_down_spd_limit(dev->link);
  2161. ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
  2162. }
  2163. }
  2164. if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
  2165. /* disable device if it has used up all its chances */
  2166. ata_dev_disable(dev);
  2167. /* detach if offline */
  2168. if (ata_link_offline(dev->link))
  2169. ata_eh_detach_dev(dev);
  2170. /* schedule probe if necessary */
  2171. if (ata_eh_schedule_probe(dev))
  2172. ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
  2173. return 1;
  2174. } else {
  2175. ehc->i.action |= ATA_EH_RESET;
  2176. return 0;
  2177. }
  2178. }
  2179. /**
  2180. * ata_eh_recover - recover host port after error
  2181. * @ap: host port to recover
  2182. * @prereset: prereset method (can be NULL)
  2183. * @softreset: softreset method (can be NULL)
  2184. * @hardreset: hardreset method (can be NULL)
  2185. * @postreset: postreset method (can be NULL)
  2186. * @r_failed_link: out parameter for failed link
  2187. *
  2188. * This is the alpha and omega, eum and yang, heart and soul of
  2189. * libata exception handling. On entry, actions required to
  2190. * recover each link and hotplug requests are recorded in the
  2191. * link's eh_context. This function executes all the operations
  2192. * with appropriate retrials and fallbacks to resurrect failed
  2193. * devices, detach goners and greet newcomers.
  2194. *
  2195. * LOCKING:
  2196. * Kernel thread context (may sleep).
  2197. *
  2198. * RETURNS:
  2199. * 0 on success, -errno on failure.
  2200. */
  2201. int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
  2202. ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
  2203. ata_postreset_fn_t postreset,
  2204. struct ata_link **r_failed_link)
  2205. {
  2206. struct ata_link *link;
  2207. struct ata_device *dev;
  2208. int nr_failed_devs, nr_disabled_devs;
  2209. int reset, rc;
  2210. unsigned long flags;
  2211. DPRINTK("ENTER\n");
  2212. /* prep for recovery */
  2213. ata_port_for_each_link(link, ap) {
  2214. struct ata_eh_context *ehc = &link->eh_context;
  2215. /* re-enable link? */
  2216. if (ehc->i.action & ATA_EH_ENABLE_LINK) {
  2217. ata_eh_about_to_do(link, NULL, ATA_EH_ENABLE_LINK);
  2218. spin_lock_irqsave(ap->lock, flags);
  2219. link->flags &= ~ATA_LFLAG_DISABLED;
  2220. spin_unlock_irqrestore(ap->lock, flags);
  2221. ata_eh_done(link, NULL, ATA_EH_ENABLE_LINK);
  2222. }
  2223. ata_link_for_each_dev(dev, link) {
  2224. if (link->flags & ATA_LFLAG_NO_RETRY)
  2225. ehc->tries[dev->devno] = 1;
  2226. else
  2227. ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
  2228. /* collect port action mask recorded in dev actions */
  2229. ehc->i.action |= ehc->i.dev_action[dev->devno] &
  2230. ~ATA_EH_PERDEV_MASK;
  2231. ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK;
  2232. /* process hotplug request */
  2233. if (dev->flags & ATA_DFLAG_DETACH)
  2234. ata_eh_detach_dev(dev);
  2235. /* schedule probe if necessary */
  2236. if (!ata_dev_enabled(dev))
  2237. ata_eh_schedule_probe(dev);
  2238. }
  2239. }
  2240. retry:
  2241. rc = 0;
  2242. nr_failed_devs = 0;
  2243. nr_disabled_devs = 0;
  2244. reset = 0;
  2245. /* if UNLOADING, finish immediately */
  2246. if (ap->pflags & ATA_PFLAG_UNLOADING)
  2247. goto out;
  2248. /* prep for EH */
  2249. ata_port_for_each_link(link, ap) {
  2250. struct ata_eh_context *ehc = &link->eh_context;
  2251. /* skip EH if possible. */
  2252. if (ata_eh_skip_recovery(link))
  2253. ehc->i.action = 0;
  2254. /* do we need to reset? */
  2255. if (ehc->i.action & ATA_EH_RESET)
  2256. reset = 1;
  2257. ata_link_for_each_dev(dev, link)
  2258. ehc->classes[dev->devno] = ATA_DEV_UNKNOWN;
  2259. }
  2260. /* reset */
  2261. if (reset) {
  2262. /* if PMP is attached, this function only deals with
  2263. * downstream links, port should stay thawed.
  2264. */
  2265. if (!sata_pmp_attached(ap))
  2266. ata_eh_freeze_port(ap);
  2267. ata_port_for_each_link(link, ap) {
  2268. struct ata_eh_context *ehc = &link->eh_context;
  2269. if (!(ehc->i.action & ATA_EH_RESET))
  2270. continue;
  2271. rc = ata_eh_reset(link, ata_link_nr_vacant(link),
  2272. prereset, softreset, hardreset,
  2273. postreset);
  2274. if (rc) {
  2275. ata_link_printk(link, KERN_ERR,
  2276. "reset failed, giving up\n");
  2277. goto out;
  2278. }
  2279. }
  2280. if (!sata_pmp_attached(ap))
  2281. ata_eh_thaw_port(ap);
  2282. }
  2283. /* the rest */
  2284. ata_port_for_each_link(link, ap) {
  2285. struct ata_eh_context *ehc = &link->eh_context;
  2286. /* revalidate existing devices and attach new ones */
  2287. rc = ata_eh_revalidate_and_attach(link, &dev);
  2288. if (rc)
  2289. goto dev_fail;
  2290. /* if PMP got attached, return, pmp EH will take care of it */
  2291. if (link->device->class == ATA_DEV_PMP) {
  2292. ehc->i.action = 0;
  2293. return 0;
  2294. }
  2295. /* configure transfer mode if necessary */
  2296. if (ehc->i.flags & ATA_EHI_SETMODE) {
  2297. rc = ata_set_mode(link, &dev);
  2298. if (rc)
  2299. goto dev_fail;
  2300. ehc->i.flags &= ~ATA_EHI_SETMODE;
  2301. }
  2302. if (ehc->i.action & ATA_EH_LPM)
  2303. ata_link_for_each_dev(dev, link)
  2304. ata_dev_enable_pm(dev, ap->pm_policy);
  2305. /* this link is okay now */
  2306. ehc->i.flags = 0;
  2307. continue;
  2308. dev_fail:
  2309. nr_failed_devs++;
  2310. if (ata_eh_handle_dev_fail(dev, rc))
  2311. nr_disabled_devs++;
  2312. if (ap->pflags & ATA_PFLAG_FROZEN) {
  2313. /* PMP reset requires working host port.
  2314. * Can't retry if it's frozen.
  2315. */
  2316. if (sata_pmp_attached(ap))
  2317. goto out;
  2318. break;
  2319. }
  2320. }
  2321. if (nr_failed_devs) {
  2322. if (nr_failed_devs != nr_disabled_devs) {
  2323. ata_port_printk(ap, KERN_WARNING, "failed to recover "
  2324. "some devices, retrying in 5 secs\n");
  2325. ssleep(5);
  2326. } else {
  2327. /* no device left to recover, repeat fast */
  2328. msleep(500);
  2329. }
  2330. goto retry;
  2331. }
  2332. out:
  2333. if (rc && r_failed_link)
  2334. *r_failed_link = link;
  2335. DPRINTK("EXIT, rc=%d\n", rc);
  2336. return rc;
  2337. }
  2338. /**
  2339. * ata_eh_finish - finish up EH
  2340. * @ap: host port to finish EH for
  2341. *
  2342. * Recovery is complete. Clean up EH states and retry or finish
  2343. * failed qcs.
  2344. *
  2345. * LOCKING:
  2346. * None.
  2347. */
  2348. void ata_eh_finish(struct ata_port *ap)
  2349. {
  2350. int tag;
  2351. /* retry or finish qcs */
  2352. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  2353. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  2354. if (!(qc->flags & ATA_QCFLAG_FAILED))
  2355. continue;
  2356. if (qc->err_mask) {
  2357. /* FIXME: Once EH migration is complete,
  2358. * generate sense data in this function,
  2359. * considering both err_mask and tf.
  2360. */
  2361. if (qc->flags & ATA_QCFLAG_RETRY)
  2362. ata_eh_qc_retry(qc);
  2363. else
  2364. ata_eh_qc_complete(qc);
  2365. } else {
  2366. if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
  2367. ata_eh_qc_complete(qc);
  2368. } else {
  2369. /* feed zero TF to sense generation */
  2370. memset(&qc->result_tf, 0, sizeof(qc->result_tf));
  2371. ata_eh_qc_retry(qc);
  2372. }
  2373. }
  2374. }
  2375. /* make sure nr_active_links is zero after EH */
  2376. WARN_ON(ap->nr_active_links);
  2377. ap->nr_active_links = 0;
  2378. }
  2379. /**
  2380. * ata_do_eh - do standard error handling
  2381. * @ap: host port to handle error for
  2382. *
  2383. * @prereset: prereset method (can be NULL)
  2384. * @softreset: softreset method (can be NULL)
  2385. * @hardreset: hardreset method (can be NULL)
  2386. * @postreset: postreset method (can be NULL)
  2387. *
  2388. * Perform standard error handling sequence.
  2389. *
  2390. * LOCKING:
  2391. * Kernel thread context (may sleep).
  2392. */
  2393. void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
  2394. ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
  2395. ata_postreset_fn_t postreset)
  2396. {
  2397. struct ata_device *dev;
  2398. int rc;
  2399. ata_eh_autopsy(ap);
  2400. ata_eh_report(ap);
  2401. rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset,
  2402. NULL);
  2403. if (rc) {
  2404. ata_link_for_each_dev(dev, &ap->link)
  2405. ata_dev_disable(dev);
  2406. }
  2407. ata_eh_finish(ap);
  2408. }
  2409. /**
  2410. * ata_std_error_handler - standard error handler
  2411. * @ap: host port to handle error for
  2412. *
  2413. * Standard error handler
  2414. *
  2415. * LOCKING:
  2416. * Kernel thread context (may sleep).
  2417. */
  2418. void ata_std_error_handler(struct ata_port *ap)
  2419. {
  2420. struct ata_port_operations *ops = ap->ops;
  2421. ata_reset_fn_t hardreset = ops->hardreset;
  2422. /* ignore built-in hardreset if SCR access is not available */
  2423. if (ata_is_builtin_hardreset(hardreset) && !sata_scr_valid(&ap->link))
  2424. hardreset = NULL;
  2425. ata_do_eh(ap, ops->prereset, ops->softreset, hardreset, ops->postreset);
  2426. }
  2427. #ifdef CONFIG_PM
  2428. /**
  2429. * ata_eh_handle_port_suspend - perform port suspend operation
  2430. * @ap: port to suspend
  2431. *
  2432. * Suspend @ap.
  2433. *
  2434. * LOCKING:
  2435. * Kernel thread context (may sleep).
  2436. */
  2437. static void ata_eh_handle_port_suspend(struct ata_port *ap)
  2438. {
  2439. unsigned long flags;
  2440. int rc = 0;
  2441. /* are we suspending? */
  2442. spin_lock_irqsave(ap->lock, flags);
  2443. if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
  2444. ap->pm_mesg.event == PM_EVENT_ON) {
  2445. spin_unlock_irqrestore(ap->lock, flags);
  2446. return;
  2447. }
  2448. spin_unlock_irqrestore(ap->lock, flags);
  2449. WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
  2450. /* tell ACPI we're suspending */
  2451. rc = ata_acpi_on_suspend(ap);
  2452. if (rc)
  2453. goto out;
  2454. /* suspend */
  2455. ata_eh_freeze_port(ap);
  2456. if (ap->ops->port_suspend)
  2457. rc = ap->ops->port_suspend(ap, ap->pm_mesg);
  2458. ata_acpi_set_state(ap, PMSG_SUSPEND);
  2459. out:
  2460. /* report result */
  2461. spin_lock_irqsave(ap->lock, flags);
  2462. ap->pflags &= ~ATA_PFLAG_PM_PENDING;
  2463. if (rc == 0)
  2464. ap->pflags |= ATA_PFLAG_SUSPENDED;
  2465. else if (ap->pflags & ATA_PFLAG_FROZEN)
  2466. ata_port_schedule_eh(ap);
  2467. if (ap->pm_result) {
  2468. *ap->pm_result = rc;
  2469. ap->pm_result = NULL;
  2470. }
  2471. spin_unlock_irqrestore(ap->lock, flags);
  2472. return;
  2473. }
  2474. /**
  2475. * ata_eh_handle_port_resume - perform port resume operation
  2476. * @ap: port to resume
  2477. *
  2478. * Resume @ap.
  2479. *
  2480. * LOCKING:
  2481. * Kernel thread context (may sleep).
  2482. */
  2483. static void ata_eh_handle_port_resume(struct ata_port *ap)
  2484. {
  2485. unsigned long flags;
  2486. int rc = 0;
  2487. /* are we resuming? */
  2488. spin_lock_irqsave(ap->lock, flags);
  2489. if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
  2490. ap->pm_mesg.event != PM_EVENT_ON) {
  2491. spin_unlock_irqrestore(ap->lock, flags);
  2492. return;
  2493. }
  2494. spin_unlock_irqrestore(ap->lock, flags);
  2495. WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
  2496. ata_acpi_set_state(ap, PMSG_ON);
  2497. if (ap->ops->port_resume)
  2498. rc = ap->ops->port_resume(ap);
  2499. /* tell ACPI that we're resuming */
  2500. ata_acpi_on_resume(ap);
  2501. /* report result */
  2502. spin_lock_irqsave(ap->lock, flags);
  2503. ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
  2504. if (ap->pm_result) {
  2505. *ap->pm_result = rc;
  2506. ap->pm_result = NULL;
  2507. }
  2508. spin_unlock_irqrestore(ap->lock, flags);
  2509. }
  2510. #endif /* CONFIG_PM */