libata-eh.c 52 KB

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