libata-eh.c 49 KB

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