libata-eh.c 69 KB

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