libata-eh.c 73 KB

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