libata-eh.c 54 KB

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