libata-eh.c 63 KB

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