libata-eh.c 71 KB

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