libata-eh.c 59 KB

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