libata-eh.c 58 KB

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