libata-eh.c 43 KB

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