libata-eh.c 44 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847
  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/config.h>
  35. #include <linux/kernel.h>
  36. #include <scsi/scsi.h>
  37. #include <scsi/scsi_host.h>
  38. #include <scsi/scsi_eh.h>
  39. #include <scsi/scsi_device.h>
  40. #include <scsi/scsi_cmnd.h>
  41. #include "scsi_transport_api.h"
  42. #include <linux/libata.h>
  43. #include "libata.h"
  44. static void __ata_port_freeze(struct ata_port *ap);
  45. static void ata_eh_finish(struct ata_port *ap);
  46. static void ata_ering_record(struct ata_ering *ering, int is_io,
  47. unsigned int err_mask)
  48. {
  49. struct ata_ering_entry *ent;
  50. WARN_ON(!err_mask);
  51. ering->cursor++;
  52. ering->cursor %= ATA_ERING_SIZE;
  53. ent = &ering->ring[ering->cursor];
  54. ent->is_io = is_io;
  55. ent->err_mask = err_mask;
  56. ent->timestamp = get_jiffies_64();
  57. }
  58. static struct ata_ering_entry * ata_ering_top(struct ata_ering *ering)
  59. {
  60. struct ata_ering_entry *ent = &ering->ring[ering->cursor];
  61. if (!ent->err_mask)
  62. return NULL;
  63. return ent;
  64. }
  65. static int ata_ering_map(struct ata_ering *ering,
  66. int (*map_fn)(struct ata_ering_entry *, void *),
  67. void *arg)
  68. {
  69. int idx, rc = 0;
  70. struct ata_ering_entry *ent;
  71. idx = ering->cursor;
  72. do {
  73. ent = &ering->ring[idx];
  74. if (!ent->err_mask)
  75. break;
  76. rc = map_fn(ent, arg);
  77. if (rc)
  78. break;
  79. idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
  80. } while (idx != ering->cursor);
  81. return rc;
  82. }
  83. /**
  84. * ata_scsi_timed_out - SCSI layer time out callback
  85. * @cmd: timed out SCSI command
  86. *
  87. * Handles SCSI layer timeout. We race with normal completion of
  88. * the qc for @cmd. If the qc is already gone, we lose and let
  89. * the scsi command finish (EH_HANDLED). Otherwise, the qc has
  90. * timed out and EH should be invoked. Prevent ata_qc_complete()
  91. * from finishing it by setting EH_SCHEDULED and return
  92. * EH_NOT_HANDLED.
  93. *
  94. * TODO: kill this function once old EH is gone.
  95. *
  96. * LOCKING:
  97. * Called from timer context
  98. *
  99. * RETURNS:
  100. * EH_HANDLED or EH_NOT_HANDLED
  101. */
  102. enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
  103. {
  104. struct Scsi_Host *host = cmd->device->host;
  105. struct ata_port *ap = ata_shost_to_port(host);
  106. unsigned long flags;
  107. struct ata_queued_cmd *qc;
  108. enum scsi_eh_timer_return ret;
  109. DPRINTK("ENTER\n");
  110. if (ap->ops->error_handler) {
  111. ret = EH_NOT_HANDLED;
  112. goto out;
  113. }
  114. ret = EH_HANDLED;
  115. spin_lock_irqsave(&ap->host_set->lock, flags);
  116. qc = ata_qc_from_tag(ap, ap->active_tag);
  117. if (qc) {
  118. WARN_ON(qc->scsicmd != cmd);
  119. qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
  120. qc->err_mask |= AC_ERR_TIMEOUT;
  121. ret = EH_NOT_HANDLED;
  122. }
  123. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  124. out:
  125. DPRINTK("EXIT, ret=%d\n", ret);
  126. return ret;
  127. }
  128. /**
  129. * ata_scsi_error - SCSI layer error handler callback
  130. * @host: SCSI host on which error occurred
  131. *
  132. * Handles SCSI-layer-thrown error events.
  133. *
  134. * LOCKING:
  135. * Inherited from SCSI layer (none, can sleep)
  136. *
  137. * RETURNS:
  138. * Zero.
  139. */
  140. void ata_scsi_error(struct Scsi_Host *host)
  141. {
  142. struct ata_port *ap = ata_shost_to_port(host);
  143. spinlock_t *hs_lock = &ap->host_set->lock;
  144. int i, repeat_cnt = ATA_EH_MAX_REPEAT;
  145. unsigned long flags;
  146. DPRINTK("ENTER\n");
  147. /* synchronize with port task */
  148. ata_port_flush_task(ap);
  149. /* synchronize with host_set lock and sort out timeouts */
  150. /* For new EH, all qcs are finished in one of three ways -
  151. * normal completion, error completion, and SCSI timeout.
  152. * Both cmpletions can race against SCSI timeout. When normal
  153. * completion wins, the qc never reaches EH. When error
  154. * completion wins, the qc has ATA_QCFLAG_FAILED set.
  155. *
  156. * When SCSI timeout wins, things are a bit more complex.
  157. * Normal or error completion can occur after the timeout but
  158. * before this point. In such cases, both types of
  159. * completions are honored. A scmd is determined to have
  160. * timed out iff its associated qc is active and not failed.
  161. */
  162. if (ap->ops->error_handler) {
  163. struct scsi_cmnd *scmd, *tmp;
  164. int nr_timedout = 0;
  165. spin_lock_irqsave(hs_lock, flags);
  166. list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {
  167. struct ata_queued_cmd *qc;
  168. for (i = 0; i < ATA_MAX_QUEUE; i++) {
  169. qc = __ata_qc_from_tag(ap, i);
  170. if (qc->flags & ATA_QCFLAG_ACTIVE &&
  171. qc->scsicmd == scmd)
  172. break;
  173. }
  174. if (i < ATA_MAX_QUEUE) {
  175. /* the scmd has an associated qc */
  176. if (!(qc->flags & ATA_QCFLAG_FAILED)) {
  177. /* which hasn't failed yet, timeout */
  178. qc->err_mask |= AC_ERR_TIMEOUT;
  179. qc->flags |= ATA_QCFLAG_FAILED;
  180. nr_timedout++;
  181. }
  182. } else {
  183. /* Normal completion occurred after
  184. * SCSI timeout but before this point.
  185. * Successfully complete it.
  186. */
  187. scmd->retries = scmd->allowed;
  188. scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
  189. }
  190. }
  191. /* If we have timed out qcs. They belong to EH from
  192. * this point but the state of the controller is
  193. * unknown. Freeze the port to make sure the IRQ
  194. * handler doesn't diddle with those qcs. This must
  195. * be done atomically w.r.t. setting QCFLAG_FAILED.
  196. */
  197. if (nr_timedout)
  198. __ata_port_freeze(ap);
  199. spin_unlock_irqrestore(hs_lock, flags);
  200. } else
  201. spin_unlock_wait(hs_lock);
  202. repeat:
  203. /* invoke error handler */
  204. if (ap->ops->error_handler) {
  205. /* fetch & clear EH info */
  206. spin_lock_irqsave(hs_lock, flags);
  207. memset(&ap->eh_context, 0, sizeof(ap->eh_context));
  208. ap->eh_context.i = ap->eh_info;
  209. memset(&ap->eh_info, 0, sizeof(ap->eh_info));
  210. ap->flags |= ATA_FLAG_EH_IN_PROGRESS;
  211. ap->flags &= ~ATA_FLAG_EH_PENDING;
  212. spin_unlock_irqrestore(hs_lock, flags);
  213. /* invoke EH. if unloading, just finish failed qcs */
  214. if (!(ap->flags & ATA_FLAG_UNLOADING))
  215. ap->ops->error_handler(ap);
  216. else
  217. ata_eh_finish(ap);
  218. /* Exception might have happend after ->error_handler
  219. * recovered the port but before this point. Repeat
  220. * EH in such case.
  221. */
  222. spin_lock_irqsave(hs_lock, flags);
  223. if (ap->flags & ATA_FLAG_EH_PENDING) {
  224. if (--repeat_cnt) {
  225. ata_port_printk(ap, KERN_INFO,
  226. "EH pending after completion, "
  227. "repeating EH (cnt=%d)\n", repeat_cnt);
  228. spin_unlock_irqrestore(hs_lock, flags);
  229. goto repeat;
  230. }
  231. ata_port_printk(ap, KERN_ERR, "EH pending after %d "
  232. "tries, giving up\n", ATA_EH_MAX_REPEAT);
  233. }
  234. /* this run is complete, make sure EH info is clear */
  235. memset(&ap->eh_info, 0, sizeof(ap->eh_info));
  236. /* Clear host_eh_scheduled while holding hs_lock such
  237. * that if exception occurs after this point but
  238. * before EH completion, SCSI midlayer will
  239. * re-initiate EH.
  240. */
  241. host->host_eh_scheduled = 0;
  242. spin_unlock_irqrestore(hs_lock, flags);
  243. } else {
  244. WARN_ON(ata_qc_from_tag(ap, ap->active_tag) == NULL);
  245. ap->ops->eng_timeout(ap);
  246. }
  247. /* finish or retry handled scmd's and clean up */
  248. WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
  249. scsi_eh_flush_done_q(&ap->eh_done_q);
  250. /* clean up */
  251. spin_lock_irqsave(hs_lock, flags);
  252. if (ap->flags & ATA_FLAG_LOADING) {
  253. ap->flags &= ~ATA_FLAG_LOADING;
  254. } else {
  255. if (ap->flags & ATA_FLAG_SCSI_HOTPLUG)
  256. queue_work(ata_aux_wq, &ap->hotplug_task);
  257. if (ap->flags & ATA_FLAG_RECOVERED)
  258. ata_port_printk(ap, KERN_INFO, "EH complete\n");
  259. }
  260. ap->flags &= ~(ATA_FLAG_SCSI_HOTPLUG | ATA_FLAG_RECOVERED);
  261. /* tell wait_eh that we're done */
  262. ap->flags &= ~ATA_FLAG_EH_IN_PROGRESS;
  263. wake_up_all(&ap->eh_wait_q);
  264. spin_unlock_irqrestore(hs_lock, flags);
  265. DPRINTK("EXIT\n");
  266. }
  267. /**
  268. * ata_port_wait_eh - Wait for the currently pending EH to complete
  269. * @ap: Port to wait EH for
  270. *
  271. * Wait until the currently pending EH is complete.
  272. *
  273. * LOCKING:
  274. * Kernel thread context (may sleep).
  275. */
  276. void ata_port_wait_eh(struct ata_port *ap)
  277. {
  278. unsigned long flags;
  279. DEFINE_WAIT(wait);
  280. retry:
  281. spin_lock_irqsave(&ap->host_set->lock, flags);
  282. while (ap->flags & (ATA_FLAG_EH_PENDING | ATA_FLAG_EH_IN_PROGRESS)) {
  283. prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
  284. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  285. schedule();
  286. spin_lock_irqsave(&ap->host_set->lock, flags);
  287. }
  288. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  289. /* make sure SCSI EH is complete */
  290. if (scsi_host_in_recovery(ap->host)) {
  291. msleep(10);
  292. goto retry;
  293. }
  294. }
  295. /**
  296. * ata_qc_timeout - Handle timeout of queued command
  297. * @qc: Command that timed out
  298. *
  299. * Some part of the kernel (currently, only the SCSI layer)
  300. * has noticed that the active command on port @ap has not
  301. * completed after a specified length of time. Handle this
  302. * condition by disabling DMA (if necessary) and completing
  303. * transactions, with error if necessary.
  304. *
  305. * This also handles the case of the "lost interrupt", where
  306. * for some reason (possibly hardware bug, possibly driver bug)
  307. * an interrupt was not delivered to the driver, even though the
  308. * transaction completed successfully.
  309. *
  310. * TODO: kill this function once old EH is gone.
  311. *
  312. * LOCKING:
  313. * Inherited from SCSI layer (none, can sleep)
  314. */
  315. static void ata_qc_timeout(struct ata_queued_cmd *qc)
  316. {
  317. struct ata_port *ap = qc->ap;
  318. struct ata_host_set *host_set = ap->host_set;
  319. u8 host_stat = 0, drv_stat;
  320. unsigned long flags;
  321. DPRINTK("ENTER\n");
  322. ap->hsm_task_state = HSM_ST_IDLE;
  323. spin_lock_irqsave(&host_set->lock, flags);
  324. switch (qc->tf.protocol) {
  325. case ATA_PROT_DMA:
  326. case ATA_PROT_ATAPI_DMA:
  327. host_stat = ap->ops->bmdma_status(ap);
  328. /* before we do anything else, clear DMA-Start bit */
  329. ap->ops->bmdma_stop(qc);
  330. /* fall through */
  331. default:
  332. ata_altstatus(ap);
  333. drv_stat = ata_chk_status(ap);
  334. /* ack bmdma irq events */
  335. ap->ops->irq_clear(ap);
  336. ata_dev_printk(qc->dev, KERN_ERR, "command 0x%x timeout, "
  337. "stat 0x%x host_stat 0x%x\n",
  338. qc->tf.command, drv_stat, host_stat);
  339. /* complete taskfile transaction */
  340. qc->err_mask |= AC_ERR_TIMEOUT;
  341. break;
  342. }
  343. spin_unlock_irqrestore(&host_set->lock, flags);
  344. ata_eh_qc_complete(qc);
  345. DPRINTK("EXIT\n");
  346. }
  347. /**
  348. * ata_eng_timeout - Handle timeout of queued command
  349. * @ap: Port on which timed-out command is active
  350. *
  351. * Some part of the kernel (currently, only the SCSI layer)
  352. * has noticed that the active command on port @ap has not
  353. * completed after a specified length of time. Handle this
  354. * condition by disabling DMA (if necessary) and completing
  355. * transactions, with error if necessary.
  356. *
  357. * This also handles the case of the "lost interrupt", where
  358. * for some reason (possibly hardware bug, possibly driver bug)
  359. * an interrupt was not delivered to the driver, even though the
  360. * transaction completed successfully.
  361. *
  362. * TODO: kill this function once old EH is gone.
  363. *
  364. * LOCKING:
  365. * Inherited from SCSI layer (none, can sleep)
  366. */
  367. void ata_eng_timeout(struct ata_port *ap)
  368. {
  369. DPRINTK("ENTER\n");
  370. ata_qc_timeout(ata_qc_from_tag(ap, ap->active_tag));
  371. DPRINTK("EXIT\n");
  372. }
  373. /**
  374. * ata_qc_schedule_eh - schedule qc for error handling
  375. * @qc: command to schedule error handling for
  376. *
  377. * Schedule error handling for @qc. EH will kick in as soon as
  378. * other commands are drained.
  379. *
  380. * LOCKING:
  381. * spin_lock_irqsave(host_set lock)
  382. */
  383. void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
  384. {
  385. struct ata_port *ap = qc->ap;
  386. WARN_ON(!ap->ops->error_handler);
  387. qc->flags |= ATA_QCFLAG_FAILED;
  388. qc->ap->flags |= ATA_FLAG_EH_PENDING;
  389. /* The following will fail if timeout has already expired.
  390. * ata_scsi_error() takes care of such scmds on EH entry.
  391. * Note that ATA_QCFLAG_FAILED is unconditionally set after
  392. * this function completes.
  393. */
  394. scsi_req_abort_cmd(qc->scsicmd);
  395. }
  396. /**
  397. * ata_port_schedule_eh - schedule error handling without a qc
  398. * @ap: ATA port to schedule EH for
  399. *
  400. * Schedule error handling for @ap. EH will kick in as soon as
  401. * all commands are drained.
  402. *
  403. * LOCKING:
  404. * spin_lock_irqsave(host_set lock)
  405. */
  406. void ata_port_schedule_eh(struct ata_port *ap)
  407. {
  408. WARN_ON(!ap->ops->error_handler);
  409. ap->flags |= ATA_FLAG_EH_PENDING;
  410. scsi_schedule_eh(ap->host);
  411. DPRINTK("port EH scheduled\n");
  412. }
  413. /**
  414. * ata_port_abort - abort all qc's on the port
  415. * @ap: ATA port to abort qc's for
  416. *
  417. * Abort all active qc's of @ap and schedule EH.
  418. *
  419. * LOCKING:
  420. * spin_lock_irqsave(host_set lock)
  421. *
  422. * RETURNS:
  423. * Number of aborted qc's.
  424. */
  425. int ata_port_abort(struct ata_port *ap)
  426. {
  427. int tag, nr_aborted = 0;
  428. WARN_ON(!ap->ops->error_handler);
  429. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  430. struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
  431. if (qc) {
  432. qc->flags |= ATA_QCFLAG_FAILED;
  433. ata_qc_complete(qc);
  434. nr_aborted++;
  435. }
  436. }
  437. if (!nr_aborted)
  438. ata_port_schedule_eh(ap);
  439. return nr_aborted;
  440. }
  441. /**
  442. * __ata_port_freeze - freeze port
  443. * @ap: ATA port to freeze
  444. *
  445. * This function is called when HSM violation or some other
  446. * condition disrupts normal operation of the port. Frozen port
  447. * is not allowed to perform any operation until the port is
  448. * thawed, which usually follows a successful reset.
  449. *
  450. * ap->ops->freeze() callback can be used for freezing the port
  451. * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
  452. * port cannot be frozen hardware-wise, the interrupt handler
  453. * must ack and clear interrupts unconditionally while the port
  454. * is frozen.
  455. *
  456. * LOCKING:
  457. * spin_lock_irqsave(host_set lock)
  458. */
  459. static void __ata_port_freeze(struct ata_port *ap)
  460. {
  461. WARN_ON(!ap->ops->error_handler);
  462. if (ap->ops->freeze)
  463. ap->ops->freeze(ap);
  464. ap->flags |= ATA_FLAG_FROZEN;
  465. DPRINTK("ata%u port frozen\n", ap->id);
  466. }
  467. /**
  468. * ata_port_freeze - abort & freeze port
  469. * @ap: ATA port to freeze
  470. *
  471. * Abort and freeze @ap.
  472. *
  473. * LOCKING:
  474. * spin_lock_irqsave(host_set lock)
  475. *
  476. * RETURNS:
  477. * Number of aborted commands.
  478. */
  479. int ata_port_freeze(struct ata_port *ap)
  480. {
  481. int nr_aborted;
  482. WARN_ON(!ap->ops->error_handler);
  483. nr_aborted = ata_port_abort(ap);
  484. __ata_port_freeze(ap);
  485. return nr_aborted;
  486. }
  487. /**
  488. * ata_eh_freeze_port - EH helper to freeze port
  489. * @ap: ATA port to freeze
  490. *
  491. * Freeze @ap.
  492. *
  493. * LOCKING:
  494. * None.
  495. */
  496. void ata_eh_freeze_port(struct ata_port *ap)
  497. {
  498. unsigned long flags;
  499. if (!ap->ops->error_handler)
  500. return;
  501. spin_lock_irqsave(&ap->host_set->lock, flags);
  502. __ata_port_freeze(ap);
  503. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  504. }
  505. /**
  506. * ata_port_thaw_port - EH helper to thaw port
  507. * @ap: ATA port to thaw
  508. *
  509. * Thaw frozen port @ap.
  510. *
  511. * LOCKING:
  512. * None.
  513. */
  514. void ata_eh_thaw_port(struct ata_port *ap)
  515. {
  516. unsigned long flags;
  517. if (!ap->ops->error_handler)
  518. return;
  519. spin_lock_irqsave(&ap->host_set->lock, flags);
  520. ap->flags &= ~ATA_FLAG_FROZEN;
  521. if (ap->ops->thaw)
  522. ap->ops->thaw(ap);
  523. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  524. DPRINTK("ata%u port thawed\n", ap->id);
  525. }
  526. static void ata_eh_scsidone(struct scsi_cmnd *scmd)
  527. {
  528. /* nada */
  529. }
  530. static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
  531. {
  532. struct ata_port *ap = qc->ap;
  533. struct scsi_cmnd *scmd = qc->scsicmd;
  534. unsigned long flags;
  535. spin_lock_irqsave(&ap->host_set->lock, flags);
  536. qc->scsidone = ata_eh_scsidone;
  537. __ata_qc_complete(qc);
  538. WARN_ON(ata_tag_valid(qc->tag));
  539. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  540. scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
  541. }
  542. /**
  543. * ata_eh_qc_complete - Complete an active ATA command from EH
  544. * @qc: Command to complete
  545. *
  546. * Indicate to the mid and upper layers that an ATA command has
  547. * completed. To be used from EH.
  548. */
  549. void ata_eh_qc_complete(struct ata_queued_cmd *qc)
  550. {
  551. struct scsi_cmnd *scmd = qc->scsicmd;
  552. scmd->retries = scmd->allowed;
  553. __ata_eh_qc_complete(qc);
  554. }
  555. /**
  556. * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
  557. * @qc: Command to retry
  558. *
  559. * Indicate to the mid and upper layers that an ATA command
  560. * should be retried. To be used from EH.
  561. *
  562. * SCSI midlayer limits the number of retries to scmd->allowed.
  563. * scmd->retries is decremented for commands which get retried
  564. * due to unrelated failures (qc->err_mask is zero).
  565. */
  566. void ata_eh_qc_retry(struct ata_queued_cmd *qc)
  567. {
  568. struct scsi_cmnd *scmd = qc->scsicmd;
  569. if (!qc->err_mask && scmd->retries)
  570. scmd->retries--;
  571. __ata_eh_qc_complete(qc);
  572. }
  573. /**
  574. * ata_eh_detach_dev - detach ATA device
  575. * @dev: ATA device to detach
  576. *
  577. * Detach @dev.
  578. *
  579. * LOCKING:
  580. * None.
  581. */
  582. static void ata_eh_detach_dev(struct ata_device *dev)
  583. {
  584. struct ata_port *ap = dev->ap;
  585. unsigned long flags;
  586. ata_dev_disable(dev);
  587. spin_lock_irqsave(&ap->host_set->lock, flags);
  588. dev->flags &= ~ATA_DFLAG_DETACH;
  589. if (ata_scsi_offline_dev(dev)) {
  590. dev->flags |= ATA_DFLAG_DETACHED;
  591. ap->flags |= ATA_FLAG_SCSI_HOTPLUG;
  592. }
  593. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  594. }
  595. /**
  596. * ata_eh_about_to_do - about to perform eh_action
  597. * @ap: target ATA port
  598. * @action: action about to be performed
  599. *
  600. * Called just before performing EH actions to clear related bits
  601. * in @ap->eh_info such that eh actions are not unnecessarily
  602. * repeated.
  603. *
  604. * LOCKING:
  605. * None.
  606. */
  607. static void ata_eh_about_to_do(struct ata_port *ap, unsigned int action)
  608. {
  609. unsigned long flags;
  610. spin_lock_irqsave(&ap->host_set->lock, flags);
  611. ap->eh_info.action &= ~action;
  612. ap->flags |= ATA_FLAG_RECOVERED;
  613. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  614. }
  615. /**
  616. * ata_err_string - convert err_mask to descriptive string
  617. * @err_mask: error mask to convert to string
  618. *
  619. * Convert @err_mask to descriptive string. Errors are
  620. * prioritized according to severity and only the most severe
  621. * error is reported.
  622. *
  623. * LOCKING:
  624. * None.
  625. *
  626. * RETURNS:
  627. * Descriptive string for @err_mask
  628. */
  629. static const char * ata_err_string(unsigned int err_mask)
  630. {
  631. if (err_mask & AC_ERR_HOST_BUS)
  632. return "host bus error";
  633. if (err_mask & AC_ERR_ATA_BUS)
  634. return "ATA bus error";
  635. if (err_mask & AC_ERR_TIMEOUT)
  636. return "timeout";
  637. if (err_mask & AC_ERR_HSM)
  638. return "HSM violation";
  639. if (err_mask & AC_ERR_SYSTEM)
  640. return "internal error";
  641. if (err_mask & AC_ERR_MEDIA)
  642. return "media error";
  643. if (err_mask & AC_ERR_INVALID)
  644. return "invalid argument";
  645. if (err_mask & AC_ERR_DEV)
  646. return "device error";
  647. return "unknown error";
  648. }
  649. /**
  650. * ata_read_log_page - read a specific log page
  651. * @dev: target device
  652. * @page: page to read
  653. * @buf: buffer to store read page
  654. * @sectors: number of sectors to read
  655. *
  656. * Read log page using READ_LOG_EXT command.
  657. *
  658. * LOCKING:
  659. * Kernel thread context (may sleep).
  660. *
  661. * RETURNS:
  662. * 0 on success, AC_ERR_* mask otherwise.
  663. */
  664. static unsigned int ata_read_log_page(struct ata_device *dev,
  665. u8 page, void *buf, unsigned int sectors)
  666. {
  667. struct ata_taskfile tf;
  668. unsigned int err_mask;
  669. DPRINTK("read log page - page %d\n", page);
  670. ata_tf_init(dev, &tf);
  671. tf.command = ATA_CMD_READ_LOG_EXT;
  672. tf.lbal = page;
  673. tf.nsect = sectors;
  674. tf.hob_nsect = sectors >> 8;
  675. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
  676. tf.protocol = ATA_PROT_PIO;
  677. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
  678. buf, sectors * ATA_SECT_SIZE);
  679. DPRINTK("EXIT, err_mask=%x\n", err_mask);
  680. return err_mask;
  681. }
  682. /**
  683. * ata_eh_read_log_10h - Read log page 10h for NCQ error details
  684. * @dev: Device to read log page 10h from
  685. * @tag: Resulting tag of the failed command
  686. * @tf: Resulting taskfile registers of the failed command
  687. *
  688. * Read log page 10h to obtain NCQ error details and clear error
  689. * condition.
  690. *
  691. * LOCKING:
  692. * Kernel thread context (may sleep).
  693. *
  694. * RETURNS:
  695. * 0 on success, -errno otherwise.
  696. */
  697. static int ata_eh_read_log_10h(struct ata_device *dev,
  698. int *tag, struct ata_taskfile *tf)
  699. {
  700. u8 *buf = dev->ap->sector_buf;
  701. unsigned int err_mask;
  702. u8 csum;
  703. int i;
  704. err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
  705. if (err_mask)
  706. return -EIO;
  707. csum = 0;
  708. for (i = 0; i < ATA_SECT_SIZE; i++)
  709. csum += buf[i];
  710. if (csum)
  711. ata_dev_printk(dev, KERN_WARNING,
  712. "invalid checksum 0x%x on log page 10h\n", csum);
  713. if (buf[0] & 0x80)
  714. return -ENOENT;
  715. *tag = buf[0] & 0x1f;
  716. tf->command = buf[2];
  717. tf->feature = buf[3];
  718. tf->lbal = buf[4];
  719. tf->lbam = buf[5];
  720. tf->lbah = buf[6];
  721. tf->device = buf[7];
  722. tf->hob_lbal = buf[8];
  723. tf->hob_lbam = buf[9];
  724. tf->hob_lbah = buf[10];
  725. tf->nsect = buf[12];
  726. tf->hob_nsect = buf[13];
  727. return 0;
  728. }
  729. /**
  730. * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
  731. * @dev: device to perform REQUEST_SENSE to
  732. * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
  733. *
  734. * Perform ATAPI REQUEST_SENSE after the device reported CHECK
  735. * SENSE. This function is EH helper.
  736. *
  737. * LOCKING:
  738. * Kernel thread context (may sleep).
  739. *
  740. * RETURNS:
  741. * 0 on success, AC_ERR_* mask on failure
  742. */
  743. static unsigned int atapi_eh_request_sense(struct ata_device *dev,
  744. unsigned char *sense_buf)
  745. {
  746. struct ata_port *ap = dev->ap;
  747. struct ata_taskfile tf;
  748. u8 cdb[ATAPI_CDB_LEN];
  749. DPRINTK("ATAPI request sense\n");
  750. ata_tf_init(dev, &tf);
  751. /* FIXME: is this needed? */
  752. memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
  753. /* XXX: why tf_read here? */
  754. ap->ops->tf_read(ap, &tf);
  755. /* fill these in, for the case where they are -not- overwritten */
  756. sense_buf[0] = 0x70;
  757. sense_buf[2] = tf.feature >> 4;
  758. memset(cdb, 0, ATAPI_CDB_LEN);
  759. cdb[0] = REQUEST_SENSE;
  760. cdb[4] = SCSI_SENSE_BUFFERSIZE;
  761. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  762. tf.command = ATA_CMD_PACKET;
  763. /* is it pointless to prefer PIO for "safety reasons"? */
  764. if (ap->flags & ATA_FLAG_PIO_DMA) {
  765. tf.protocol = ATA_PROT_ATAPI_DMA;
  766. tf.feature |= ATAPI_PKT_DMA;
  767. } else {
  768. tf.protocol = ATA_PROT_ATAPI;
  769. tf.lbam = (8 * 1024) & 0xff;
  770. tf.lbah = (8 * 1024) >> 8;
  771. }
  772. return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
  773. sense_buf, SCSI_SENSE_BUFFERSIZE);
  774. }
  775. /**
  776. * ata_eh_analyze_serror - analyze SError for a failed port
  777. * @ap: ATA port to analyze SError for
  778. *
  779. * Analyze SError if available and further determine cause of
  780. * failure.
  781. *
  782. * LOCKING:
  783. * None.
  784. */
  785. static void ata_eh_analyze_serror(struct ata_port *ap)
  786. {
  787. struct ata_eh_context *ehc = &ap->eh_context;
  788. u32 serror = ehc->i.serror;
  789. unsigned int err_mask = 0, action = 0;
  790. if (serror & SERR_PERSISTENT) {
  791. err_mask |= AC_ERR_ATA_BUS;
  792. action |= ATA_EH_HARDRESET;
  793. }
  794. if (serror &
  795. (SERR_DATA_RECOVERED | SERR_COMM_RECOVERED | SERR_DATA)) {
  796. err_mask |= AC_ERR_ATA_BUS;
  797. action |= ATA_EH_SOFTRESET;
  798. }
  799. if (serror & SERR_PROTOCOL) {
  800. err_mask |= AC_ERR_HSM;
  801. action |= ATA_EH_SOFTRESET;
  802. }
  803. if (serror & SERR_INTERNAL) {
  804. err_mask |= AC_ERR_SYSTEM;
  805. action |= ATA_EH_SOFTRESET;
  806. }
  807. if (serror & (SERR_PHYRDY_CHG | SERR_DEV_XCHG))
  808. ata_ehi_hotplugged(&ehc->i);
  809. ehc->i.err_mask |= err_mask;
  810. ehc->i.action |= action;
  811. }
  812. /**
  813. * ata_eh_analyze_ncq_error - analyze NCQ error
  814. * @ap: ATA port to analyze NCQ error for
  815. *
  816. * Read log page 10h, determine the offending qc and acquire
  817. * error status TF. For NCQ device errors, all LLDDs have to do
  818. * is setting AC_ERR_DEV in ehi->err_mask. This function takes
  819. * care of the rest.
  820. *
  821. * LOCKING:
  822. * Kernel thread context (may sleep).
  823. */
  824. static void ata_eh_analyze_ncq_error(struct ata_port *ap)
  825. {
  826. struct ata_eh_context *ehc = &ap->eh_context;
  827. struct ata_device *dev = ap->device;
  828. struct ata_queued_cmd *qc;
  829. struct ata_taskfile tf;
  830. int tag, rc;
  831. /* if frozen, we can't do much */
  832. if (ap->flags & ATA_FLAG_FROZEN)
  833. return;
  834. /* is it NCQ device error? */
  835. if (!ap->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
  836. return;
  837. /* has LLDD analyzed already? */
  838. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  839. qc = __ata_qc_from_tag(ap, tag);
  840. if (!(qc->flags & ATA_QCFLAG_FAILED))
  841. continue;
  842. if (qc->err_mask)
  843. return;
  844. }
  845. /* okay, this error is ours */
  846. rc = ata_eh_read_log_10h(dev, &tag, &tf);
  847. if (rc) {
  848. ata_port_printk(ap, KERN_ERR, "failed to read log page 10h "
  849. "(errno=%d)\n", rc);
  850. return;
  851. }
  852. if (!(ap->sactive & (1 << tag))) {
  853. ata_port_printk(ap, KERN_ERR, "log page 10h reported "
  854. "inactive tag %d\n", tag);
  855. return;
  856. }
  857. /* we've got the perpetrator, condemn it */
  858. qc = __ata_qc_from_tag(ap, tag);
  859. memcpy(&qc->result_tf, &tf, sizeof(tf));
  860. qc->err_mask |= AC_ERR_DEV;
  861. ehc->i.err_mask &= ~AC_ERR_DEV;
  862. }
  863. /**
  864. * ata_eh_analyze_tf - analyze taskfile of a failed qc
  865. * @qc: qc to analyze
  866. * @tf: Taskfile registers to analyze
  867. *
  868. * Analyze taskfile of @qc and further determine cause of
  869. * failure. This function also requests ATAPI sense data if
  870. * avaliable.
  871. *
  872. * LOCKING:
  873. * Kernel thread context (may sleep).
  874. *
  875. * RETURNS:
  876. * Determined recovery action
  877. */
  878. static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
  879. const struct ata_taskfile *tf)
  880. {
  881. unsigned int tmp, action = 0;
  882. u8 stat = tf->command, err = tf->feature;
  883. if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
  884. qc->err_mask |= AC_ERR_HSM;
  885. return ATA_EH_SOFTRESET;
  886. }
  887. if (!(qc->err_mask & AC_ERR_DEV))
  888. return 0;
  889. switch (qc->dev->class) {
  890. case ATA_DEV_ATA:
  891. if (err & ATA_ICRC)
  892. qc->err_mask |= AC_ERR_ATA_BUS;
  893. if (err & ATA_UNC)
  894. qc->err_mask |= AC_ERR_MEDIA;
  895. if (err & ATA_IDNF)
  896. qc->err_mask |= AC_ERR_INVALID;
  897. break;
  898. case ATA_DEV_ATAPI:
  899. tmp = atapi_eh_request_sense(qc->dev,
  900. qc->scsicmd->sense_buffer);
  901. if (!tmp) {
  902. /* ATA_QCFLAG_SENSE_VALID is used to tell
  903. * atapi_qc_complete() that sense data is
  904. * already valid.
  905. *
  906. * TODO: interpret sense data and set
  907. * appropriate err_mask.
  908. */
  909. qc->flags |= ATA_QCFLAG_SENSE_VALID;
  910. } else
  911. qc->err_mask |= tmp;
  912. }
  913. if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
  914. action |= ATA_EH_SOFTRESET;
  915. return action;
  916. }
  917. static int ata_eh_categorize_ering_entry(struct ata_ering_entry *ent)
  918. {
  919. if (ent->err_mask & (AC_ERR_ATA_BUS | AC_ERR_TIMEOUT))
  920. return 1;
  921. if (ent->is_io) {
  922. if (ent->err_mask & AC_ERR_HSM)
  923. return 1;
  924. if ((ent->err_mask &
  925. (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
  926. return 2;
  927. }
  928. return 0;
  929. }
  930. struct speed_down_needed_arg {
  931. u64 since;
  932. int nr_errors[3];
  933. };
  934. static int speed_down_needed_cb(struct ata_ering_entry *ent, void *void_arg)
  935. {
  936. struct speed_down_needed_arg *arg = void_arg;
  937. if (ent->timestamp < arg->since)
  938. return -1;
  939. arg->nr_errors[ata_eh_categorize_ering_entry(ent)]++;
  940. return 0;
  941. }
  942. /**
  943. * ata_eh_speed_down_needed - Determine wheter speed down is necessary
  944. * @dev: Device of interest
  945. *
  946. * This function examines error ring of @dev and determines
  947. * whether speed down is necessary. Speed down is necessary if
  948. * there have been more than 3 of Cat-1 errors or 10 of Cat-2
  949. * errors during last 15 minutes.
  950. *
  951. * Cat-1 errors are ATA_BUS, TIMEOUT for any command and HSM
  952. * violation for known supported commands.
  953. *
  954. * Cat-2 errors are unclassified DEV error for known supported
  955. * command.
  956. *
  957. * LOCKING:
  958. * Inherited from caller.
  959. *
  960. * RETURNS:
  961. * 1 if speed down is necessary, 0 otherwise
  962. */
  963. static int ata_eh_speed_down_needed(struct ata_device *dev)
  964. {
  965. const u64 interval = 15LLU * 60 * HZ;
  966. static const int err_limits[3] = { -1, 3, 10 };
  967. struct speed_down_needed_arg arg;
  968. struct ata_ering_entry *ent;
  969. int err_cat;
  970. u64 j64;
  971. ent = ata_ering_top(&dev->ering);
  972. if (!ent)
  973. return 0;
  974. err_cat = ata_eh_categorize_ering_entry(ent);
  975. if (err_cat == 0)
  976. return 0;
  977. memset(&arg, 0, sizeof(arg));
  978. j64 = get_jiffies_64();
  979. if (j64 >= interval)
  980. arg.since = j64 - interval;
  981. else
  982. arg.since = 0;
  983. ata_ering_map(&dev->ering, speed_down_needed_cb, &arg);
  984. return arg.nr_errors[err_cat] > err_limits[err_cat];
  985. }
  986. /**
  987. * ata_eh_speed_down - record error and speed down if necessary
  988. * @dev: Failed device
  989. * @is_io: Did the device fail during normal IO?
  990. * @err_mask: err_mask of the error
  991. *
  992. * Record error and examine error history to determine whether
  993. * adjusting transmission speed is necessary. It also sets
  994. * transmission limits appropriately if such adjustment is
  995. * necessary.
  996. *
  997. * LOCKING:
  998. * Kernel thread context (may sleep).
  999. *
  1000. * RETURNS:
  1001. * 0 on success, -errno otherwise
  1002. */
  1003. static int ata_eh_speed_down(struct ata_device *dev, int is_io,
  1004. unsigned int err_mask)
  1005. {
  1006. if (!err_mask)
  1007. return 0;
  1008. /* record error and determine whether speed down is necessary */
  1009. ata_ering_record(&dev->ering, is_io, err_mask);
  1010. if (!ata_eh_speed_down_needed(dev))
  1011. return 0;
  1012. /* speed down SATA link speed if possible */
  1013. if (sata_down_spd_limit(dev->ap) == 0)
  1014. return ATA_EH_HARDRESET;
  1015. /* lower transfer mode */
  1016. if (ata_down_xfermask_limit(dev, 0) == 0)
  1017. return ATA_EH_SOFTRESET;
  1018. ata_dev_printk(dev, KERN_ERR,
  1019. "speed down requested but no transfer mode left\n");
  1020. return 0;
  1021. }
  1022. /**
  1023. * ata_eh_autopsy - analyze error and determine recovery action
  1024. * @ap: ATA port to perform autopsy on
  1025. *
  1026. * Analyze why @ap failed and determine which recovery action is
  1027. * needed. This function also sets more detailed AC_ERR_* values
  1028. * and fills sense data for ATAPI CHECK SENSE.
  1029. *
  1030. * LOCKING:
  1031. * Kernel thread context (may sleep).
  1032. */
  1033. static void ata_eh_autopsy(struct ata_port *ap)
  1034. {
  1035. struct ata_eh_context *ehc = &ap->eh_context;
  1036. unsigned int action = ehc->i.action;
  1037. struct ata_device *failed_dev = NULL;
  1038. unsigned int all_err_mask = 0;
  1039. int tag, is_io = 0;
  1040. u32 serror;
  1041. int rc;
  1042. DPRINTK("ENTER\n");
  1043. /* obtain and analyze SError */
  1044. rc = sata_scr_read(ap, SCR_ERROR, &serror);
  1045. if (rc == 0) {
  1046. ehc->i.serror |= serror;
  1047. ata_eh_analyze_serror(ap);
  1048. } else if (rc != -EOPNOTSUPP)
  1049. action |= ATA_EH_HARDRESET;
  1050. /* analyze NCQ failure */
  1051. ata_eh_analyze_ncq_error(ap);
  1052. /* any real error trumps AC_ERR_OTHER */
  1053. if (ehc->i.err_mask & ~AC_ERR_OTHER)
  1054. ehc->i.err_mask &= ~AC_ERR_OTHER;
  1055. all_err_mask |= ehc->i.err_mask;
  1056. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  1057. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  1058. if (!(qc->flags & ATA_QCFLAG_FAILED))
  1059. continue;
  1060. /* inherit upper level err_mask */
  1061. qc->err_mask |= ehc->i.err_mask;
  1062. /* analyze TF */
  1063. action |= ata_eh_analyze_tf(qc, &qc->result_tf);
  1064. /* DEV errors are probably spurious in case of ATA_BUS error */
  1065. if (qc->err_mask & AC_ERR_ATA_BUS)
  1066. qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
  1067. AC_ERR_INVALID);
  1068. /* any real error trumps unknown error */
  1069. if (qc->err_mask & ~AC_ERR_OTHER)
  1070. qc->err_mask &= ~AC_ERR_OTHER;
  1071. /* SENSE_VALID trumps dev/unknown error and revalidation */
  1072. if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
  1073. qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
  1074. action &= ~ATA_EH_REVALIDATE;
  1075. }
  1076. /* accumulate error info */
  1077. failed_dev = qc->dev;
  1078. all_err_mask |= qc->err_mask;
  1079. if (qc->flags & ATA_QCFLAG_IO)
  1080. is_io = 1;
  1081. }
  1082. /* speed down iff command was in progress */
  1083. if (failed_dev)
  1084. action |= ata_eh_speed_down(failed_dev, is_io, all_err_mask);
  1085. /* enforce default EH actions */
  1086. if (ap->flags & ATA_FLAG_FROZEN ||
  1087. all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
  1088. action |= ATA_EH_SOFTRESET;
  1089. else if (all_err_mask)
  1090. action |= ATA_EH_REVALIDATE;
  1091. /* record autopsy result */
  1092. ehc->i.dev = failed_dev;
  1093. ehc->i.action = action;
  1094. DPRINTK("EXIT\n");
  1095. }
  1096. /**
  1097. * ata_eh_report - report error handling to user
  1098. * @ap: ATA port EH is going on
  1099. *
  1100. * Report EH to user.
  1101. *
  1102. * LOCKING:
  1103. * None.
  1104. */
  1105. static void ata_eh_report(struct ata_port *ap)
  1106. {
  1107. struct ata_eh_context *ehc = &ap->eh_context;
  1108. const char *frozen, *desc;
  1109. int tag, nr_failed = 0;
  1110. desc = NULL;
  1111. if (ehc->i.desc[0] != '\0')
  1112. desc = ehc->i.desc;
  1113. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  1114. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  1115. if (!(qc->flags & ATA_QCFLAG_FAILED))
  1116. continue;
  1117. if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
  1118. continue;
  1119. nr_failed++;
  1120. }
  1121. if (!nr_failed && !ehc->i.err_mask)
  1122. return;
  1123. frozen = "";
  1124. if (ap->flags & ATA_FLAG_FROZEN)
  1125. frozen = " frozen";
  1126. if (ehc->i.dev) {
  1127. ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
  1128. "SAct 0x%x SErr 0x%x action 0x%x%s\n",
  1129. ehc->i.err_mask, ap->sactive, ehc->i.serror,
  1130. ehc->i.action, frozen);
  1131. if (desc)
  1132. ata_dev_printk(ehc->i.dev, KERN_ERR, "(%s)\n", desc);
  1133. } else {
  1134. ata_port_printk(ap, KERN_ERR, "exception Emask 0x%x "
  1135. "SAct 0x%x SErr 0x%x action 0x%x%s\n",
  1136. ehc->i.err_mask, ap->sactive, ehc->i.serror,
  1137. ehc->i.action, frozen);
  1138. if (desc)
  1139. ata_port_printk(ap, KERN_ERR, "(%s)\n", desc);
  1140. }
  1141. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  1142. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  1143. if (!(qc->flags & ATA_QCFLAG_FAILED) || !qc->err_mask)
  1144. continue;
  1145. ata_dev_printk(qc->dev, KERN_ERR, "tag %d cmd 0x%x "
  1146. "Emask 0x%x stat 0x%x err 0x%x (%s)\n",
  1147. qc->tag, qc->tf.command, qc->err_mask,
  1148. qc->result_tf.command, qc->result_tf.feature,
  1149. ata_err_string(qc->err_mask));
  1150. }
  1151. }
  1152. static int ata_do_reset(struct ata_port *ap, ata_reset_fn_t reset,
  1153. unsigned int *classes)
  1154. {
  1155. int i, rc;
  1156. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1157. classes[i] = ATA_DEV_UNKNOWN;
  1158. rc = reset(ap, classes);
  1159. if (rc)
  1160. return rc;
  1161. /* If any class isn't ATA_DEV_UNKNOWN, consider classification
  1162. * is complete and convert all ATA_DEV_UNKNOWN to
  1163. * ATA_DEV_NONE.
  1164. */
  1165. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1166. if (classes[i] != ATA_DEV_UNKNOWN)
  1167. break;
  1168. if (i < ATA_MAX_DEVICES)
  1169. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1170. if (classes[i] == ATA_DEV_UNKNOWN)
  1171. classes[i] = ATA_DEV_NONE;
  1172. return 0;
  1173. }
  1174. static int ata_eh_followup_srst_needed(int rc, int classify,
  1175. const unsigned int *classes)
  1176. {
  1177. if (rc == -EAGAIN)
  1178. return 1;
  1179. if (rc != 0)
  1180. return 0;
  1181. if (classify && classes[0] == ATA_DEV_UNKNOWN)
  1182. return 1;
  1183. return 0;
  1184. }
  1185. static int ata_eh_reset(struct ata_port *ap, int classify,
  1186. ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
  1187. ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
  1188. {
  1189. struct ata_eh_context *ehc = &ap->eh_context;
  1190. unsigned int *classes = ehc->classes;
  1191. int tries = ATA_EH_RESET_TRIES;
  1192. int verbose = !(ap->flags & ATA_FLAG_LOADING);
  1193. unsigned int action;
  1194. ata_reset_fn_t reset;
  1195. int i, did_followup_srst, rc;
  1196. /* Determine which reset to use and record in ehc->i.action.
  1197. * prereset() may examine and modify it.
  1198. */
  1199. action = ehc->i.action;
  1200. ehc->i.action &= ~ATA_EH_RESET_MASK;
  1201. if (softreset && (!hardreset || (!sata_set_spd_needed(ap) &&
  1202. !(action & ATA_EH_HARDRESET))))
  1203. ehc->i.action |= ATA_EH_SOFTRESET;
  1204. else
  1205. ehc->i.action |= ATA_EH_HARDRESET;
  1206. if (prereset) {
  1207. rc = prereset(ap);
  1208. if (rc) {
  1209. ata_port_printk(ap, KERN_ERR,
  1210. "prereset failed (errno=%d)\n", rc);
  1211. return rc;
  1212. }
  1213. }
  1214. /* prereset() might have modified ehc->i.action */
  1215. if (ehc->i.action & ATA_EH_HARDRESET)
  1216. reset = hardreset;
  1217. else if (ehc->i.action & ATA_EH_SOFTRESET)
  1218. reset = softreset;
  1219. else {
  1220. /* prereset told us not to reset, bang classes and return */
  1221. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1222. classes[i] = ATA_DEV_NONE;
  1223. return 0;
  1224. }
  1225. /* did prereset() screw up? if so, fix up to avoid oopsing */
  1226. if (!reset) {
  1227. ata_port_printk(ap, KERN_ERR, "BUG: prereset() requested "
  1228. "invalid reset type\n");
  1229. if (softreset)
  1230. reset = softreset;
  1231. else
  1232. reset = hardreset;
  1233. }
  1234. retry:
  1235. /* shut up during boot probing */
  1236. if (verbose)
  1237. ata_port_printk(ap, KERN_INFO, "%s resetting port\n",
  1238. reset == softreset ? "soft" : "hard");
  1239. /* reset */
  1240. ata_eh_about_to_do(ap, ATA_EH_RESET_MASK);
  1241. ehc->i.flags |= ATA_EHI_DID_RESET;
  1242. rc = ata_do_reset(ap, reset, classes);
  1243. did_followup_srst = 0;
  1244. if (reset == hardreset &&
  1245. ata_eh_followup_srst_needed(rc, classify, classes)) {
  1246. /* okay, let's do follow-up softreset */
  1247. did_followup_srst = 1;
  1248. reset = softreset;
  1249. if (!reset) {
  1250. ata_port_printk(ap, KERN_ERR,
  1251. "follow-up softreset required "
  1252. "but no softreset avaliable\n");
  1253. return -EINVAL;
  1254. }
  1255. ata_eh_about_to_do(ap, ATA_EH_RESET_MASK);
  1256. rc = ata_do_reset(ap, reset, classes);
  1257. if (rc == 0 && classify &&
  1258. classes[0] == ATA_DEV_UNKNOWN) {
  1259. ata_port_printk(ap, KERN_ERR,
  1260. "classification failed\n");
  1261. return -EINVAL;
  1262. }
  1263. }
  1264. if (rc && --tries) {
  1265. const char *type;
  1266. if (reset == softreset) {
  1267. if (did_followup_srst)
  1268. type = "follow-up soft";
  1269. else
  1270. type = "soft";
  1271. } else
  1272. type = "hard";
  1273. ata_port_printk(ap, KERN_WARNING,
  1274. "%sreset failed, retrying in 5 secs\n", type);
  1275. ssleep(5);
  1276. if (reset == hardreset)
  1277. sata_down_spd_limit(ap);
  1278. if (hardreset)
  1279. reset = hardreset;
  1280. goto retry;
  1281. }
  1282. if (rc == 0) {
  1283. /* After the reset, the device state is PIO 0 and the
  1284. * controller state is undefined. Record the mode.
  1285. */
  1286. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1287. ap->device[i].pio_mode = XFER_PIO_0;
  1288. if (postreset)
  1289. postreset(ap, classes);
  1290. /* reset successful, schedule revalidation */
  1291. ehc->i.dev = NULL;
  1292. ehc->i.action &= ~ATA_EH_RESET_MASK;
  1293. ehc->i.action |= ATA_EH_REVALIDATE;
  1294. }
  1295. return rc;
  1296. }
  1297. static int ata_eh_revalidate_and_attach(struct ata_port *ap,
  1298. struct ata_device **r_failed_dev)
  1299. {
  1300. struct ata_eh_context *ehc = &ap->eh_context;
  1301. struct ata_device *dev;
  1302. unsigned long flags;
  1303. int i, rc = 0;
  1304. DPRINTK("ENTER\n");
  1305. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1306. dev = &ap->device[i];
  1307. if (ehc->i.action & ATA_EH_REVALIDATE && ata_dev_enabled(dev) &&
  1308. (!ehc->i.dev || ehc->i.dev == dev)) {
  1309. if (ata_port_offline(ap)) {
  1310. rc = -EIO;
  1311. break;
  1312. }
  1313. ata_eh_about_to_do(ap, ATA_EH_REVALIDATE);
  1314. rc = ata_dev_revalidate(dev,
  1315. ehc->i.flags & ATA_EHI_DID_RESET);
  1316. if (rc)
  1317. break;
  1318. ehc->i.action &= ~ATA_EH_REVALIDATE;
  1319. } else if (dev->class == ATA_DEV_UNKNOWN &&
  1320. ehc->tries[dev->devno] &&
  1321. ata_class_enabled(ehc->classes[dev->devno])) {
  1322. dev->class = ehc->classes[dev->devno];
  1323. rc = ata_dev_read_id(dev, &dev->class, 1, dev->id);
  1324. if (rc == 0)
  1325. rc = ata_dev_configure(dev, 1);
  1326. if (rc) {
  1327. dev->class = ATA_DEV_UNKNOWN;
  1328. break;
  1329. }
  1330. spin_lock_irqsave(&ap->host_set->lock, flags);
  1331. ap->flags |= ATA_FLAG_SCSI_HOTPLUG;
  1332. spin_unlock_irqrestore(&ap->host_set->lock, flags);
  1333. }
  1334. }
  1335. if (rc)
  1336. *r_failed_dev = dev;
  1337. DPRINTK("EXIT\n");
  1338. return rc;
  1339. }
  1340. static int ata_port_nr_enabled(struct ata_port *ap)
  1341. {
  1342. int i, cnt = 0;
  1343. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1344. if (ata_dev_enabled(&ap->device[i]))
  1345. cnt++;
  1346. return cnt;
  1347. }
  1348. static int ata_port_nr_vacant(struct ata_port *ap)
  1349. {
  1350. int i, cnt = 0;
  1351. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1352. if (ap->device[i].class == ATA_DEV_UNKNOWN)
  1353. cnt++;
  1354. return cnt;
  1355. }
  1356. static int ata_eh_skip_recovery(struct ata_port *ap)
  1357. {
  1358. struct ata_eh_context *ehc = &ap->eh_context;
  1359. int i;
  1360. if (ap->flags & ATA_FLAG_FROZEN || ata_port_nr_enabled(ap))
  1361. return 0;
  1362. /* skip if class codes for all vacant slots are ATA_DEV_NONE */
  1363. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1364. struct ata_device *dev = &ap->device[i];
  1365. if (dev->class == ATA_DEV_UNKNOWN &&
  1366. ehc->classes[dev->devno] != ATA_DEV_NONE)
  1367. return 0;
  1368. }
  1369. return 1;
  1370. }
  1371. /**
  1372. * ata_eh_recover - recover host port after error
  1373. * @ap: host port to recover
  1374. * @prereset: prereset method (can be NULL)
  1375. * @softreset: softreset method (can be NULL)
  1376. * @hardreset: hardreset method (can be NULL)
  1377. * @postreset: postreset method (can be NULL)
  1378. *
  1379. * This is the alpha and omega, eum and yang, heart and soul of
  1380. * libata exception handling. On entry, actions required to
  1381. * recover the port and hotplug requests are recorded in
  1382. * eh_context. This function executes all the operations with
  1383. * appropriate retrials and fallbacks to resurrect failed
  1384. * devices, detach goners and greet newcomers.
  1385. *
  1386. * LOCKING:
  1387. * Kernel thread context (may sleep).
  1388. *
  1389. * RETURNS:
  1390. * 0 on success, -errno on failure.
  1391. */
  1392. static int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
  1393. ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
  1394. ata_postreset_fn_t postreset)
  1395. {
  1396. struct ata_eh_context *ehc = &ap->eh_context;
  1397. struct ata_device *dev;
  1398. int down_xfermask, i, rc;
  1399. DPRINTK("ENTER\n");
  1400. /* prep for recovery */
  1401. for (i = 0; i < ATA_MAX_DEVICES; i++) {
  1402. dev = &ap->device[i];
  1403. ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
  1404. /* process hotplug request */
  1405. if (dev->flags & ATA_DFLAG_DETACH)
  1406. ata_eh_detach_dev(dev);
  1407. if (!ata_dev_enabled(dev) &&
  1408. ((ehc->i.probe_mask & (1 << dev->devno)) &&
  1409. !(ehc->did_probe_mask & (1 << dev->devno)))) {
  1410. ata_eh_detach_dev(dev);
  1411. ata_dev_init(dev);
  1412. ehc->did_probe_mask |= (1 << dev->devno);
  1413. ehc->i.action |= ATA_EH_SOFTRESET;
  1414. }
  1415. }
  1416. retry:
  1417. down_xfermask = 0;
  1418. rc = 0;
  1419. /* skip EH if possible. */
  1420. if (ata_eh_skip_recovery(ap))
  1421. ehc->i.action = 0;
  1422. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1423. ehc->classes[i] = ATA_DEV_UNKNOWN;
  1424. /* reset */
  1425. if (ehc->i.action & ATA_EH_RESET_MASK) {
  1426. ata_eh_freeze_port(ap);
  1427. rc = ata_eh_reset(ap, ata_port_nr_vacant(ap), prereset,
  1428. softreset, hardreset, postreset);
  1429. if (rc) {
  1430. ata_port_printk(ap, KERN_ERR,
  1431. "reset failed, giving up\n");
  1432. goto out;
  1433. }
  1434. ata_eh_thaw_port(ap);
  1435. }
  1436. /* revalidate existing devices and attach new ones */
  1437. rc = ata_eh_revalidate_and_attach(ap, &dev);
  1438. if (rc)
  1439. goto dev_fail;
  1440. /* configure transfer mode if the port has been reset */
  1441. if (ehc->i.flags & ATA_EHI_DID_RESET) {
  1442. rc = ata_set_mode(ap, &dev);
  1443. if (rc) {
  1444. down_xfermask = 1;
  1445. goto dev_fail;
  1446. }
  1447. }
  1448. goto out;
  1449. dev_fail:
  1450. switch (rc) {
  1451. case -ENODEV:
  1452. /* device missing, schedule probing */
  1453. ehc->i.probe_mask |= (1 << dev->devno);
  1454. case -EINVAL:
  1455. ehc->tries[dev->devno] = 0;
  1456. break;
  1457. case -EIO:
  1458. sata_down_spd_limit(ap);
  1459. default:
  1460. ehc->tries[dev->devno]--;
  1461. if (down_xfermask &&
  1462. ata_down_xfermask_limit(dev, ehc->tries[dev->devno] == 1))
  1463. ehc->tries[dev->devno] = 0;
  1464. }
  1465. if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
  1466. /* disable device if it has used up all its chances */
  1467. ata_dev_disable(dev);
  1468. /* detach if offline */
  1469. if (ata_port_offline(ap))
  1470. ata_eh_detach_dev(dev);
  1471. /* probe if requested */
  1472. if ((ehc->i.probe_mask & (1 << dev->devno)) &&
  1473. !(ehc->did_probe_mask & (1 << dev->devno))) {
  1474. ata_eh_detach_dev(dev);
  1475. ata_dev_init(dev);
  1476. ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
  1477. ehc->did_probe_mask |= (1 << dev->devno);
  1478. ehc->i.action |= ATA_EH_SOFTRESET;
  1479. }
  1480. } else {
  1481. /* soft didn't work? be haaaaard */
  1482. if (ehc->i.flags & ATA_EHI_DID_RESET)
  1483. ehc->i.action |= ATA_EH_HARDRESET;
  1484. else
  1485. ehc->i.action |= ATA_EH_SOFTRESET;
  1486. }
  1487. if (ata_port_nr_enabled(ap)) {
  1488. ata_port_printk(ap, KERN_WARNING, "failed to recover some "
  1489. "devices, retrying in 5 secs\n");
  1490. ssleep(5);
  1491. } else {
  1492. /* no device left, repeat fast */
  1493. msleep(500);
  1494. }
  1495. goto retry;
  1496. out:
  1497. if (rc) {
  1498. for (i = 0; i < ATA_MAX_DEVICES; i++)
  1499. ata_dev_disable(&ap->device[i]);
  1500. }
  1501. DPRINTK("EXIT, rc=%d\n", rc);
  1502. return rc;
  1503. }
  1504. /**
  1505. * ata_eh_finish - finish up EH
  1506. * @ap: host port to finish EH for
  1507. *
  1508. * Recovery is complete. Clean up EH states and retry or finish
  1509. * failed qcs.
  1510. *
  1511. * LOCKING:
  1512. * None.
  1513. */
  1514. static void ata_eh_finish(struct ata_port *ap)
  1515. {
  1516. int tag;
  1517. /* retry or finish qcs */
  1518. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  1519. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  1520. if (!(qc->flags & ATA_QCFLAG_FAILED))
  1521. continue;
  1522. if (qc->err_mask) {
  1523. /* FIXME: Once EH migration is complete,
  1524. * generate sense data in this function,
  1525. * considering both err_mask and tf.
  1526. */
  1527. if (qc->err_mask & AC_ERR_INVALID)
  1528. ata_eh_qc_complete(qc);
  1529. else
  1530. ata_eh_qc_retry(qc);
  1531. } else {
  1532. if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
  1533. ata_eh_qc_complete(qc);
  1534. } else {
  1535. /* feed zero TF to sense generation */
  1536. memset(&qc->result_tf, 0, sizeof(qc->result_tf));
  1537. ata_eh_qc_retry(qc);
  1538. }
  1539. }
  1540. }
  1541. }
  1542. /**
  1543. * ata_do_eh - do standard error handling
  1544. * @ap: host port to handle error for
  1545. * @prereset: prereset method (can be NULL)
  1546. * @softreset: softreset method (can be NULL)
  1547. * @hardreset: hardreset method (can be NULL)
  1548. * @postreset: postreset method (can be NULL)
  1549. *
  1550. * Perform standard error handling sequence.
  1551. *
  1552. * LOCKING:
  1553. * Kernel thread context (may sleep).
  1554. */
  1555. void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
  1556. ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
  1557. ata_postreset_fn_t postreset)
  1558. {
  1559. if (!(ap->flags & ATA_FLAG_LOADING)) {
  1560. ata_eh_autopsy(ap);
  1561. ata_eh_report(ap);
  1562. }
  1563. ata_eh_recover(ap, prereset, softreset, hardreset, postreset);
  1564. ata_eh_finish(ap);
  1565. }