libata-eh.c 56 KB

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