libata-eh.c 56 KB

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