libata-eh.c 74 KB

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