libata-eh.c 61 KB

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