libata-eh.c 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754
  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. #ifdef CONFIG_PM
  71. static void ata_eh_handle_port_suspend(struct ata_port *ap);
  72. static void ata_eh_handle_port_resume(struct ata_port *ap);
  73. #else /* CONFIG_PM */
  74. static void ata_eh_handle_port_suspend(struct ata_port *ap)
  75. { }
  76. static void ata_eh_handle_port_resume(struct ata_port *ap)
  77. { }
  78. #endif /* CONFIG_PM */
  79. static void __ata_ehi_pushv_desc(struct ata_eh_info *ehi, const char *fmt,
  80. va_list args)
  81. {
  82. ehi->desc_len += vscnprintf(ehi->desc + ehi->desc_len,
  83. ATA_EH_DESC_LEN - ehi->desc_len,
  84. fmt, args);
  85. }
  86. /**
  87. * __ata_ehi_push_desc - push error description without adding separator
  88. * @ehi: target EHI
  89. * @fmt: printf format string
  90. *
  91. * Format string according to @fmt and append it to @ehi->desc.
  92. *
  93. * LOCKING:
  94. * spin_lock_irqsave(host lock)
  95. */
  96. void __ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
  97. {
  98. va_list args;
  99. va_start(args, fmt);
  100. __ata_ehi_pushv_desc(ehi, fmt, args);
  101. va_end(args);
  102. }
  103. /**
  104. * ata_ehi_push_desc - push error description with separator
  105. * @ehi: target EHI
  106. * @fmt: printf format string
  107. *
  108. * Format string according to @fmt and append it to @ehi->desc.
  109. * If @ehi->desc is not empty, ", " is added in-between.
  110. *
  111. * LOCKING:
  112. * spin_lock_irqsave(host lock)
  113. */
  114. void ata_ehi_push_desc(struct ata_eh_info *ehi, const char *fmt, ...)
  115. {
  116. va_list args;
  117. if (ehi->desc_len)
  118. __ata_ehi_push_desc(ehi, ", ");
  119. va_start(args, fmt);
  120. __ata_ehi_pushv_desc(ehi, fmt, args);
  121. va_end(args);
  122. }
  123. /**
  124. * ata_ehi_clear_desc - clean error description
  125. * @ehi: target EHI
  126. *
  127. * Clear @ehi->desc.
  128. *
  129. * LOCKING:
  130. * spin_lock_irqsave(host lock)
  131. */
  132. void ata_ehi_clear_desc(struct ata_eh_info *ehi)
  133. {
  134. ehi->desc[0] = '\0';
  135. ehi->desc_len = 0;
  136. }
  137. /**
  138. * ata_port_desc - append port description
  139. * @ap: target ATA port
  140. * @fmt: printf format string
  141. *
  142. * Format string according to @fmt and append it to port
  143. * description. If port description is not empty, " " is added
  144. * in-between. This function is to be used while initializing
  145. * ata_host. The description is printed on host registration.
  146. *
  147. * LOCKING:
  148. * None.
  149. */
  150. void ata_port_desc(struct ata_port *ap, const char *fmt, ...)
  151. {
  152. va_list args;
  153. WARN_ON(!(ap->pflags & ATA_PFLAG_INITIALIZING));
  154. if (ap->link.eh_info.desc_len)
  155. __ata_ehi_push_desc(&ap->link.eh_info, " ");
  156. va_start(args, fmt);
  157. __ata_ehi_pushv_desc(&ap->link.eh_info, fmt, args);
  158. va_end(args);
  159. }
  160. #ifdef CONFIG_PCI
  161. /**
  162. * ata_port_pbar_desc - append PCI BAR description
  163. * @ap: target ATA port
  164. * @bar: target PCI BAR
  165. * @offset: offset into PCI BAR
  166. * @name: name of the area
  167. *
  168. * If @offset is negative, this function formats a string which
  169. * contains the name, address, size and type of the BAR and
  170. * appends it to the port description. If @offset is zero or
  171. * positive, only name and offsetted address is appended.
  172. *
  173. * LOCKING:
  174. * None.
  175. */
  176. void ata_port_pbar_desc(struct ata_port *ap, int bar, ssize_t offset,
  177. const char *name)
  178. {
  179. struct pci_dev *pdev = to_pci_dev(ap->host->dev);
  180. char *type = "";
  181. unsigned long long start, len;
  182. if (pci_resource_flags(pdev, bar) & IORESOURCE_MEM)
  183. type = "m";
  184. else if (pci_resource_flags(pdev, bar) & IORESOURCE_IO)
  185. type = "i";
  186. start = (unsigned long long)pci_resource_start(pdev, bar);
  187. len = (unsigned long long)pci_resource_len(pdev, bar);
  188. if (offset < 0)
  189. ata_port_desc(ap, "%s %s%llu@0x%llx", name, type, len, start);
  190. else
  191. ata_port_desc(ap, "%s 0x%llx", name, start + offset);
  192. }
  193. #endif /* CONFIG_PCI */
  194. static void ata_ering_record(struct ata_ering *ering, int is_io,
  195. unsigned int err_mask)
  196. {
  197. struct ata_ering_entry *ent;
  198. WARN_ON(!err_mask);
  199. ering->cursor++;
  200. ering->cursor %= ATA_ERING_SIZE;
  201. ent = &ering->ring[ering->cursor];
  202. ent->is_io = is_io;
  203. ent->err_mask = err_mask;
  204. ent->timestamp = get_jiffies_64();
  205. }
  206. static void ata_ering_clear(struct ata_ering *ering)
  207. {
  208. memset(ering, 0, sizeof(*ering));
  209. }
  210. static int ata_ering_map(struct ata_ering *ering,
  211. int (*map_fn)(struct ata_ering_entry *, void *),
  212. void *arg)
  213. {
  214. int idx, rc = 0;
  215. struct ata_ering_entry *ent;
  216. idx = ering->cursor;
  217. do {
  218. ent = &ering->ring[idx];
  219. if (!ent->err_mask)
  220. break;
  221. rc = map_fn(ent, arg);
  222. if (rc)
  223. break;
  224. idx = (idx - 1 + ATA_ERING_SIZE) % ATA_ERING_SIZE;
  225. } while (idx != ering->cursor);
  226. return rc;
  227. }
  228. static unsigned int ata_eh_dev_action(struct ata_device *dev)
  229. {
  230. struct ata_eh_context *ehc = &dev->link->eh_context;
  231. return ehc->i.action | ehc->i.dev_action[dev->devno];
  232. }
  233. static void ata_eh_clear_action(struct ata_link *link, struct ata_device *dev,
  234. struct ata_eh_info *ehi, unsigned int action)
  235. {
  236. struct ata_device *tdev;
  237. if (!dev) {
  238. ehi->action &= ~action;
  239. ata_link_for_each_dev(tdev, link)
  240. ehi->dev_action[tdev->devno] &= ~action;
  241. } else {
  242. /* doesn't make sense for port-wide EH actions */
  243. WARN_ON(!(action & ATA_EH_PERDEV_MASK));
  244. /* break ehi->action into ehi->dev_action */
  245. if (ehi->action & action) {
  246. ata_link_for_each_dev(tdev, link)
  247. ehi->dev_action[tdev->devno] |=
  248. ehi->action & action;
  249. ehi->action &= ~action;
  250. }
  251. /* turn off the specified per-dev action */
  252. ehi->dev_action[dev->devno] &= ~action;
  253. }
  254. }
  255. /**
  256. * ata_scsi_timed_out - SCSI layer time out callback
  257. * @cmd: timed out SCSI command
  258. *
  259. * Handles SCSI layer timeout. We race with normal completion of
  260. * the qc for @cmd. If the qc is already gone, we lose and let
  261. * the scsi command finish (EH_HANDLED). Otherwise, the qc has
  262. * timed out and EH should be invoked. Prevent ata_qc_complete()
  263. * from finishing it by setting EH_SCHEDULED and return
  264. * EH_NOT_HANDLED.
  265. *
  266. * TODO: kill this function once old EH is gone.
  267. *
  268. * LOCKING:
  269. * Called from timer context
  270. *
  271. * RETURNS:
  272. * EH_HANDLED or EH_NOT_HANDLED
  273. */
  274. enum scsi_eh_timer_return ata_scsi_timed_out(struct scsi_cmnd *cmd)
  275. {
  276. struct Scsi_Host *host = cmd->device->host;
  277. struct ata_port *ap = ata_shost_to_port(host);
  278. unsigned long flags;
  279. struct ata_queued_cmd *qc;
  280. enum scsi_eh_timer_return ret;
  281. DPRINTK("ENTER\n");
  282. if (ap->ops->error_handler) {
  283. ret = EH_NOT_HANDLED;
  284. goto out;
  285. }
  286. ret = EH_HANDLED;
  287. spin_lock_irqsave(ap->lock, flags);
  288. qc = ata_qc_from_tag(ap, ap->link.active_tag);
  289. if (qc) {
  290. WARN_ON(qc->scsicmd != cmd);
  291. qc->flags |= ATA_QCFLAG_EH_SCHEDULED;
  292. qc->err_mask |= AC_ERR_TIMEOUT;
  293. ret = EH_NOT_HANDLED;
  294. }
  295. spin_unlock_irqrestore(ap->lock, flags);
  296. out:
  297. DPRINTK("EXIT, ret=%d\n", ret);
  298. return ret;
  299. }
  300. /**
  301. * ata_scsi_error - SCSI layer error handler callback
  302. * @host: SCSI host on which error occurred
  303. *
  304. * Handles SCSI-layer-thrown error events.
  305. *
  306. * LOCKING:
  307. * Inherited from SCSI layer (none, can sleep)
  308. *
  309. * RETURNS:
  310. * Zero.
  311. */
  312. void ata_scsi_error(struct Scsi_Host *host)
  313. {
  314. struct ata_port *ap = ata_shost_to_port(host);
  315. int i;
  316. unsigned long flags;
  317. DPRINTK("ENTER\n");
  318. /* synchronize with port task */
  319. ata_port_flush_task(ap);
  320. /* synchronize with host lock and sort out timeouts */
  321. /* For new EH, all qcs are finished in one of three ways -
  322. * normal completion, error completion, and SCSI timeout.
  323. * Both cmpletions can race against SCSI timeout. When normal
  324. * completion wins, the qc never reaches EH. When error
  325. * completion wins, the qc has ATA_QCFLAG_FAILED set.
  326. *
  327. * When SCSI timeout wins, things are a bit more complex.
  328. * Normal or error completion can occur after the timeout but
  329. * before this point. In such cases, both types of
  330. * completions are honored. A scmd is determined to have
  331. * timed out iff its associated qc is active and not failed.
  332. */
  333. if (ap->ops->error_handler) {
  334. struct scsi_cmnd *scmd, *tmp;
  335. int nr_timedout = 0;
  336. spin_lock_irqsave(ap->lock, flags);
  337. list_for_each_entry_safe(scmd, tmp, &host->eh_cmd_q, eh_entry) {
  338. struct ata_queued_cmd *qc;
  339. for (i = 0; i < ATA_MAX_QUEUE; i++) {
  340. qc = __ata_qc_from_tag(ap, i);
  341. if (qc->flags & ATA_QCFLAG_ACTIVE &&
  342. qc->scsicmd == scmd)
  343. break;
  344. }
  345. if (i < ATA_MAX_QUEUE) {
  346. /* the scmd has an associated qc */
  347. if (!(qc->flags & ATA_QCFLAG_FAILED)) {
  348. /* which hasn't failed yet, timeout */
  349. qc->err_mask |= AC_ERR_TIMEOUT;
  350. qc->flags |= ATA_QCFLAG_FAILED;
  351. nr_timedout++;
  352. }
  353. } else {
  354. /* Normal completion occurred after
  355. * SCSI timeout but before this point.
  356. * Successfully complete it.
  357. */
  358. scmd->retries = scmd->allowed;
  359. scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
  360. }
  361. }
  362. /* If we have timed out qcs. They belong to EH from
  363. * this point but the state of the controller is
  364. * unknown. Freeze the port to make sure the IRQ
  365. * handler doesn't diddle with those qcs. This must
  366. * be done atomically w.r.t. setting QCFLAG_FAILED.
  367. */
  368. if (nr_timedout)
  369. __ata_port_freeze(ap);
  370. spin_unlock_irqrestore(ap->lock, flags);
  371. /* initialize eh_tries */
  372. ap->eh_tries = ATA_EH_MAX_TRIES;
  373. } else
  374. spin_unlock_wait(ap->lock);
  375. repeat:
  376. /* invoke error handler */
  377. if (ap->ops->error_handler) {
  378. struct ata_link *link;
  379. /* kill fast drain timer */
  380. del_timer_sync(&ap->fastdrain_timer);
  381. /* process port resume request */
  382. ata_eh_handle_port_resume(ap);
  383. /* fetch & clear EH info */
  384. spin_lock_irqsave(ap->lock, flags);
  385. __ata_port_for_each_link(link, ap) {
  386. memset(&link->eh_context, 0, sizeof(link->eh_context));
  387. link->eh_context.i = link->eh_info;
  388. memset(&link->eh_info, 0, sizeof(link->eh_info));
  389. }
  390. ap->pflags |= ATA_PFLAG_EH_IN_PROGRESS;
  391. ap->pflags &= ~ATA_PFLAG_EH_PENDING;
  392. ap->excl_link = NULL; /* don't maintain exclusion over EH */
  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. * sata_async_notification - SATA async notification handler
  764. * @ap: ATA port where async notification is received
  765. *
  766. * Handler to be called when async notification via SDB FIS is
  767. * received. This function schedules EH if necessary.
  768. *
  769. * LOCKING:
  770. * spin_lock_irqsave(host lock)
  771. *
  772. * RETURNS:
  773. * 1 if EH is scheduled, 0 otherwise.
  774. */
  775. int sata_async_notification(struct ata_port *ap)
  776. {
  777. u32 sntf;
  778. int rc;
  779. if (!(ap->flags & ATA_FLAG_AN))
  780. return 0;
  781. rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
  782. if (rc == 0)
  783. sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
  784. if (!ap->nr_pmp_links || rc) {
  785. /* PMP is not attached or SNTF is not available */
  786. if (!ap->nr_pmp_links) {
  787. /* PMP is not attached. Check whether ATAPI
  788. * AN is configured. If so, notify media
  789. * change.
  790. */
  791. struct ata_device *dev = ap->link.device;
  792. if ((dev->class == ATA_DEV_ATAPI) &&
  793. (dev->flags & ATA_DFLAG_AN))
  794. ata_scsi_media_change_notify(dev);
  795. return 0;
  796. } else {
  797. /* PMP is attached but SNTF is not available.
  798. * ATAPI async media change notification is
  799. * not used. The PMP must be reporting PHY
  800. * status change, schedule EH.
  801. */
  802. ata_port_schedule_eh(ap);
  803. return 1;
  804. }
  805. } else {
  806. /* PMP is attached and SNTF is available */
  807. struct ata_link *link;
  808. /* check and notify ATAPI AN */
  809. ata_port_for_each_link(link, ap) {
  810. if (!(sntf & (1 << link->pmp)))
  811. continue;
  812. if ((link->device->class == ATA_DEV_ATAPI) &&
  813. (link->device->flags & ATA_DFLAG_AN))
  814. ata_scsi_media_change_notify(link->device);
  815. }
  816. /* If PMP is reporting that PHY status of some
  817. * downstream ports has changed, schedule EH.
  818. */
  819. if (sntf & (1 << SATA_PMP_CTRL_PORT)) {
  820. ata_port_schedule_eh(ap);
  821. return 1;
  822. }
  823. return 0;
  824. }
  825. }
  826. /**
  827. * ata_eh_freeze_port - EH helper to freeze port
  828. * @ap: ATA port to freeze
  829. *
  830. * Freeze @ap.
  831. *
  832. * LOCKING:
  833. * None.
  834. */
  835. void ata_eh_freeze_port(struct ata_port *ap)
  836. {
  837. unsigned long flags;
  838. if (!ap->ops->error_handler)
  839. return;
  840. spin_lock_irqsave(ap->lock, flags);
  841. __ata_port_freeze(ap);
  842. spin_unlock_irqrestore(ap->lock, flags);
  843. }
  844. /**
  845. * ata_port_thaw_port - EH helper to thaw port
  846. * @ap: ATA port to thaw
  847. *
  848. * Thaw frozen port @ap.
  849. *
  850. * LOCKING:
  851. * None.
  852. */
  853. void ata_eh_thaw_port(struct ata_port *ap)
  854. {
  855. unsigned long flags;
  856. if (!ap->ops->error_handler)
  857. return;
  858. spin_lock_irqsave(ap->lock, flags);
  859. ap->pflags &= ~ATA_PFLAG_FROZEN;
  860. if (ap->ops->thaw)
  861. ap->ops->thaw(ap);
  862. spin_unlock_irqrestore(ap->lock, flags);
  863. DPRINTK("ata%u port thawed\n", ap->print_id);
  864. }
  865. static void ata_eh_scsidone(struct scsi_cmnd *scmd)
  866. {
  867. /* nada */
  868. }
  869. static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
  870. {
  871. struct ata_port *ap = qc->ap;
  872. struct scsi_cmnd *scmd = qc->scsicmd;
  873. unsigned long flags;
  874. spin_lock_irqsave(ap->lock, flags);
  875. qc->scsidone = ata_eh_scsidone;
  876. __ata_qc_complete(qc);
  877. WARN_ON(ata_tag_valid(qc->tag));
  878. spin_unlock_irqrestore(ap->lock, flags);
  879. scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
  880. }
  881. /**
  882. * ata_eh_qc_complete - Complete an active ATA command from EH
  883. * @qc: Command to complete
  884. *
  885. * Indicate to the mid and upper layers that an ATA command has
  886. * completed. To be used from EH.
  887. */
  888. void ata_eh_qc_complete(struct ata_queued_cmd *qc)
  889. {
  890. struct scsi_cmnd *scmd = qc->scsicmd;
  891. scmd->retries = scmd->allowed;
  892. __ata_eh_qc_complete(qc);
  893. }
  894. /**
  895. * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
  896. * @qc: Command to retry
  897. *
  898. * Indicate to the mid and upper layers that an ATA command
  899. * should be retried. To be used from EH.
  900. *
  901. * SCSI midlayer limits the number of retries to scmd->allowed.
  902. * scmd->retries is decremented for commands which get retried
  903. * due to unrelated failures (qc->err_mask is zero).
  904. */
  905. void ata_eh_qc_retry(struct ata_queued_cmd *qc)
  906. {
  907. struct scsi_cmnd *scmd = qc->scsicmd;
  908. if (!qc->err_mask && scmd->retries)
  909. scmd->retries--;
  910. __ata_eh_qc_complete(qc);
  911. }
  912. /**
  913. * ata_eh_detach_dev - detach ATA device
  914. * @dev: ATA device to detach
  915. *
  916. * Detach @dev.
  917. *
  918. * LOCKING:
  919. * None.
  920. */
  921. void ata_eh_detach_dev(struct ata_device *dev)
  922. {
  923. struct ata_link *link = dev->link;
  924. struct ata_port *ap = link->ap;
  925. unsigned long flags;
  926. ata_dev_disable(dev);
  927. spin_lock_irqsave(ap->lock, flags);
  928. dev->flags &= ~ATA_DFLAG_DETACH;
  929. if (ata_scsi_offline_dev(dev)) {
  930. dev->flags |= ATA_DFLAG_DETACHED;
  931. ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
  932. }
  933. /* clear per-dev EH actions */
  934. ata_eh_clear_action(link, dev, &link->eh_info, ATA_EH_PERDEV_MASK);
  935. ata_eh_clear_action(link, dev, &link->eh_context.i, ATA_EH_PERDEV_MASK);
  936. spin_unlock_irqrestore(ap->lock, flags);
  937. }
  938. /**
  939. * ata_eh_about_to_do - about to perform eh_action
  940. * @link: target ATA link
  941. * @dev: target ATA dev for per-dev action (can be NULL)
  942. * @action: action about to be performed
  943. *
  944. * Called just before performing EH actions to clear related bits
  945. * in @link->eh_info such that eh actions are not unnecessarily
  946. * repeated.
  947. *
  948. * LOCKING:
  949. * None.
  950. */
  951. void ata_eh_about_to_do(struct ata_link *link, struct ata_device *dev,
  952. unsigned int action)
  953. {
  954. struct ata_port *ap = link->ap;
  955. struct ata_eh_info *ehi = &link->eh_info;
  956. struct ata_eh_context *ehc = &link->eh_context;
  957. unsigned long flags;
  958. spin_lock_irqsave(ap->lock, flags);
  959. /* Reset is represented by combination of actions and EHI
  960. * flags. Suck in all related bits before clearing eh_info to
  961. * avoid losing requested action.
  962. */
  963. if (action & ATA_EH_RESET_MASK) {
  964. ehc->i.action |= ehi->action & ATA_EH_RESET_MASK;
  965. ehc->i.flags |= ehi->flags & ATA_EHI_RESET_MODIFIER_MASK;
  966. /* make sure all reset actions are cleared & clear EHI flags */
  967. action |= ATA_EH_RESET_MASK;
  968. ehi->flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
  969. }
  970. ata_eh_clear_action(link, dev, ehi, action);
  971. if (!(ehc->i.flags & ATA_EHI_QUIET))
  972. ap->pflags |= ATA_PFLAG_RECOVERED;
  973. spin_unlock_irqrestore(ap->lock, flags);
  974. }
  975. /**
  976. * ata_eh_done - EH action complete
  977. * @ap: target ATA port
  978. * @dev: target ATA dev for per-dev action (can be NULL)
  979. * @action: action just completed
  980. *
  981. * Called right after performing EH actions to clear related bits
  982. * in @link->eh_context.
  983. *
  984. * LOCKING:
  985. * None.
  986. */
  987. void ata_eh_done(struct ata_link *link, struct ata_device *dev,
  988. unsigned int action)
  989. {
  990. struct ata_eh_context *ehc = &link->eh_context;
  991. /* if reset is complete, clear all reset actions & reset modifier */
  992. if (action & ATA_EH_RESET_MASK) {
  993. action |= ATA_EH_RESET_MASK;
  994. ehc->i.flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
  995. }
  996. ata_eh_clear_action(link, dev, &ehc->i, action);
  997. }
  998. /**
  999. * ata_err_string - convert err_mask to descriptive string
  1000. * @err_mask: error mask to convert to string
  1001. *
  1002. * Convert @err_mask to descriptive string. Errors are
  1003. * prioritized according to severity and only the most severe
  1004. * error is reported.
  1005. *
  1006. * LOCKING:
  1007. * None.
  1008. *
  1009. * RETURNS:
  1010. * Descriptive string for @err_mask
  1011. */
  1012. static const char * ata_err_string(unsigned int err_mask)
  1013. {
  1014. if (err_mask & AC_ERR_HOST_BUS)
  1015. return "host bus error";
  1016. if (err_mask & AC_ERR_ATA_BUS)
  1017. return "ATA bus error";
  1018. if (err_mask & AC_ERR_TIMEOUT)
  1019. return "timeout";
  1020. if (err_mask & AC_ERR_HSM)
  1021. return "HSM violation";
  1022. if (err_mask & AC_ERR_SYSTEM)
  1023. return "internal error";
  1024. if (err_mask & AC_ERR_MEDIA)
  1025. return "media error";
  1026. if (err_mask & AC_ERR_INVALID)
  1027. return "invalid argument";
  1028. if (err_mask & AC_ERR_DEV)
  1029. return "device error";
  1030. return "unknown error";
  1031. }
  1032. /**
  1033. * ata_read_log_page - read a specific log page
  1034. * @dev: target device
  1035. * @page: page to read
  1036. * @buf: buffer to store read page
  1037. * @sectors: number of sectors to read
  1038. *
  1039. * Read log page using READ_LOG_EXT command.
  1040. *
  1041. * LOCKING:
  1042. * Kernel thread context (may sleep).
  1043. *
  1044. * RETURNS:
  1045. * 0 on success, AC_ERR_* mask otherwise.
  1046. */
  1047. static unsigned int ata_read_log_page(struct ata_device *dev,
  1048. u8 page, void *buf, unsigned int sectors)
  1049. {
  1050. struct ata_taskfile tf;
  1051. unsigned int err_mask;
  1052. DPRINTK("read log page - page %d\n", page);
  1053. ata_tf_init(dev, &tf);
  1054. tf.command = ATA_CMD_READ_LOG_EXT;
  1055. tf.lbal = page;
  1056. tf.nsect = sectors;
  1057. tf.hob_nsect = sectors >> 8;
  1058. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
  1059. tf.protocol = ATA_PROT_PIO;
  1060. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
  1061. buf, sectors * ATA_SECT_SIZE);
  1062. DPRINTK("EXIT, err_mask=%x\n", err_mask);
  1063. return err_mask;
  1064. }
  1065. /**
  1066. * ata_eh_read_log_10h - Read log page 10h for NCQ error details
  1067. * @dev: Device to read log page 10h from
  1068. * @tag: Resulting tag of the failed command
  1069. * @tf: Resulting taskfile registers of the failed command
  1070. *
  1071. * Read log page 10h to obtain NCQ error details and clear error
  1072. * condition.
  1073. *
  1074. * LOCKING:
  1075. * Kernel thread context (may sleep).
  1076. *
  1077. * RETURNS:
  1078. * 0 on success, -errno otherwise.
  1079. */
  1080. static int ata_eh_read_log_10h(struct ata_device *dev,
  1081. int *tag, struct ata_taskfile *tf)
  1082. {
  1083. u8 *buf = dev->link->ap->sector_buf;
  1084. unsigned int err_mask;
  1085. u8 csum;
  1086. int i;
  1087. err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
  1088. if (err_mask)
  1089. return -EIO;
  1090. csum = 0;
  1091. for (i = 0; i < ATA_SECT_SIZE; i++)
  1092. csum += buf[i];
  1093. if (csum)
  1094. ata_dev_printk(dev, KERN_WARNING,
  1095. "invalid checksum 0x%x on log page 10h\n", csum);
  1096. if (buf[0] & 0x80)
  1097. return -ENOENT;
  1098. *tag = buf[0] & 0x1f;
  1099. tf->command = buf[2];
  1100. tf->feature = buf[3];
  1101. tf->lbal = buf[4];
  1102. tf->lbam = buf[5];
  1103. tf->lbah = buf[6];
  1104. tf->device = buf[7];
  1105. tf->hob_lbal = buf[8];
  1106. tf->hob_lbam = buf[9];
  1107. tf->hob_lbah = buf[10];
  1108. tf->nsect = buf[12];
  1109. tf->hob_nsect = buf[13];
  1110. return 0;
  1111. }
  1112. /**
  1113. * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
  1114. * @dev: device to perform REQUEST_SENSE to
  1115. * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
  1116. *
  1117. * Perform ATAPI REQUEST_SENSE after the device reported CHECK
  1118. * SENSE. This function is EH helper.
  1119. *
  1120. * LOCKING:
  1121. * Kernel thread context (may sleep).
  1122. *
  1123. * RETURNS:
  1124. * 0 on success, AC_ERR_* mask on failure
  1125. */
  1126. static unsigned int atapi_eh_request_sense(struct ata_queued_cmd *qc)
  1127. {
  1128. struct ata_device *dev = qc->dev;
  1129. unsigned char *sense_buf = qc->scsicmd->sense_buffer;
  1130. struct ata_port *ap = dev->link->ap;
  1131. struct ata_taskfile tf;
  1132. u8 cdb[ATAPI_CDB_LEN];
  1133. DPRINTK("ATAPI request sense\n");
  1134. /* FIXME: is this needed? */
  1135. memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
  1136. /* initialize sense_buf with the error register,
  1137. * for the case where they are -not- overwritten
  1138. */
  1139. sense_buf[0] = 0x70;
  1140. sense_buf[2] = qc->result_tf.feature >> 4;
  1141. /* some devices time out if garbage left in tf */
  1142. ata_tf_init(dev, &tf);
  1143. memset(cdb, 0, ATAPI_CDB_LEN);
  1144. cdb[0] = REQUEST_SENSE;
  1145. cdb[4] = SCSI_SENSE_BUFFERSIZE;
  1146. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  1147. tf.command = ATA_CMD_PACKET;
  1148. /* is it pointless to prefer PIO for "safety reasons"? */
  1149. if (ap->flags & ATA_FLAG_PIO_DMA) {
  1150. tf.protocol = ATA_PROT_ATAPI_DMA;
  1151. tf.feature |= ATAPI_PKT_DMA;
  1152. } else {
  1153. tf.protocol = ATA_PROT_ATAPI;
  1154. tf.lbam = (8 * 1024) & 0xff;
  1155. tf.lbah = (8 * 1024) >> 8;
  1156. }
  1157. return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
  1158. sense_buf, SCSI_SENSE_BUFFERSIZE);
  1159. }
  1160. /**
  1161. * ata_eh_analyze_serror - analyze SError for a failed port
  1162. * @link: ATA link to analyze SError for
  1163. *
  1164. * Analyze SError if available and further determine cause of
  1165. * failure.
  1166. *
  1167. * LOCKING:
  1168. * None.
  1169. */
  1170. static void ata_eh_analyze_serror(struct ata_link *link)
  1171. {
  1172. struct ata_eh_context *ehc = &link->eh_context;
  1173. u32 serror = ehc->i.serror;
  1174. unsigned int err_mask = 0, action = 0;
  1175. u32 hotplug_mask;
  1176. if (serror & SERR_PERSISTENT) {
  1177. err_mask |= AC_ERR_ATA_BUS;
  1178. action |= ATA_EH_HARDRESET;
  1179. }
  1180. if (serror &
  1181. (SERR_DATA_RECOVERED | SERR_COMM_RECOVERED | SERR_DATA)) {
  1182. err_mask |= AC_ERR_ATA_BUS;
  1183. action |= ATA_EH_SOFTRESET;
  1184. }
  1185. if (serror & SERR_PROTOCOL) {
  1186. err_mask |= AC_ERR_HSM;
  1187. action |= ATA_EH_SOFTRESET;
  1188. }
  1189. if (serror & SERR_INTERNAL) {
  1190. err_mask |= AC_ERR_SYSTEM;
  1191. action |= ATA_EH_HARDRESET;
  1192. }
  1193. /* Determine whether a hotplug event has occurred. Both
  1194. * SError.N/X are considered hotplug events for enabled or
  1195. * host links. For disabled PMP links, only N bit is
  1196. * considered as X bit is left at 1 for link plugging.
  1197. */
  1198. hotplug_mask = 0;
  1199. if (!(link->flags & ATA_LFLAG_DISABLED) || ata_is_host_link(link))
  1200. hotplug_mask = SERR_PHYRDY_CHG | SERR_DEV_XCHG;
  1201. else
  1202. hotplug_mask = SERR_PHYRDY_CHG;
  1203. if (serror & hotplug_mask)
  1204. ata_ehi_hotplugged(&ehc->i);
  1205. ehc->i.err_mask |= err_mask;
  1206. ehc->i.action |= action;
  1207. }
  1208. /**
  1209. * ata_eh_analyze_ncq_error - analyze NCQ error
  1210. * @link: ATA link to analyze NCQ error for
  1211. *
  1212. * Read log page 10h, determine the offending qc and acquire
  1213. * error status TF. For NCQ device errors, all LLDDs have to do
  1214. * is setting AC_ERR_DEV in ehi->err_mask. This function takes
  1215. * care of the rest.
  1216. *
  1217. * LOCKING:
  1218. * Kernel thread context (may sleep).
  1219. */
  1220. static void ata_eh_analyze_ncq_error(struct ata_link *link)
  1221. {
  1222. struct ata_port *ap = link->ap;
  1223. struct ata_eh_context *ehc = &link->eh_context;
  1224. struct ata_device *dev = link->device;
  1225. struct ata_queued_cmd *qc;
  1226. struct ata_taskfile tf;
  1227. int tag, rc;
  1228. /* if frozen, we can't do much */
  1229. if (ap->pflags & ATA_PFLAG_FROZEN)
  1230. return;
  1231. /* is it NCQ device error? */
  1232. if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
  1233. return;
  1234. /* has LLDD analyzed already? */
  1235. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  1236. qc = __ata_qc_from_tag(ap, tag);
  1237. if (!(qc->flags & ATA_QCFLAG_FAILED))
  1238. continue;
  1239. if (qc->err_mask)
  1240. return;
  1241. }
  1242. /* okay, this error is ours */
  1243. rc = ata_eh_read_log_10h(dev, &tag, &tf);
  1244. if (rc) {
  1245. ata_link_printk(link, KERN_ERR, "failed to read log page 10h "
  1246. "(errno=%d)\n", rc);
  1247. return;
  1248. }
  1249. if (!(link->sactive & (1 << tag))) {
  1250. ata_link_printk(link, KERN_ERR, "log page 10h reported "
  1251. "inactive tag %d\n", tag);
  1252. return;
  1253. }
  1254. /* we've got the perpetrator, condemn it */
  1255. qc = __ata_qc_from_tag(ap, tag);
  1256. memcpy(&qc->result_tf, &tf, sizeof(tf));
  1257. qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
  1258. ehc->i.err_mask &= ~AC_ERR_DEV;
  1259. }
  1260. /**
  1261. * ata_eh_analyze_tf - analyze taskfile of a failed qc
  1262. * @qc: qc to analyze
  1263. * @tf: Taskfile registers to analyze
  1264. *
  1265. * Analyze taskfile of @qc and further determine cause of
  1266. * failure. This function also requests ATAPI sense data if
  1267. * avaliable.
  1268. *
  1269. * LOCKING:
  1270. * Kernel thread context (may sleep).
  1271. *
  1272. * RETURNS:
  1273. * Determined recovery action
  1274. */
  1275. static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
  1276. const struct ata_taskfile *tf)
  1277. {
  1278. unsigned int tmp, action = 0;
  1279. u8 stat = tf->command, err = tf->feature;
  1280. if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
  1281. qc->err_mask |= AC_ERR_HSM;
  1282. return ATA_EH_SOFTRESET;
  1283. }
  1284. if (stat & (ATA_ERR | ATA_DF))
  1285. qc->err_mask |= AC_ERR_DEV;
  1286. else
  1287. return 0;
  1288. switch (qc->dev->class) {
  1289. case ATA_DEV_ATA:
  1290. if (err & ATA_ICRC)
  1291. qc->err_mask |= AC_ERR_ATA_BUS;
  1292. if (err & ATA_UNC)
  1293. qc->err_mask |= AC_ERR_MEDIA;
  1294. if (err & ATA_IDNF)
  1295. qc->err_mask |= AC_ERR_INVALID;
  1296. break;
  1297. case ATA_DEV_ATAPI:
  1298. if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
  1299. tmp = atapi_eh_request_sense(qc);
  1300. if (!tmp) {
  1301. /* ATA_QCFLAG_SENSE_VALID is used to
  1302. * tell atapi_qc_complete() that sense
  1303. * data is already valid.
  1304. *
  1305. * TODO: interpret sense data and set
  1306. * appropriate err_mask.
  1307. */
  1308. qc->flags |= ATA_QCFLAG_SENSE_VALID;
  1309. } else
  1310. qc->err_mask |= tmp;
  1311. }
  1312. }
  1313. if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
  1314. action |= ATA_EH_SOFTRESET;
  1315. return action;
  1316. }
  1317. static int ata_eh_categorize_error(int is_io, unsigned int err_mask)
  1318. {
  1319. if (err_mask & AC_ERR_ATA_BUS)
  1320. return 1;
  1321. if (err_mask & AC_ERR_TIMEOUT)
  1322. return 2;
  1323. if (is_io) {
  1324. if (err_mask & AC_ERR_HSM)
  1325. return 2;
  1326. if ((err_mask &
  1327. (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
  1328. return 3;
  1329. }
  1330. return 0;
  1331. }
  1332. struct speed_down_verdict_arg {
  1333. u64 since;
  1334. int nr_errors[4];
  1335. };
  1336. static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
  1337. {
  1338. struct speed_down_verdict_arg *arg = void_arg;
  1339. int cat = ata_eh_categorize_error(ent->is_io, ent->err_mask);
  1340. if (ent->timestamp < arg->since)
  1341. return -1;
  1342. arg->nr_errors[cat]++;
  1343. return 0;
  1344. }
  1345. /**
  1346. * ata_eh_speed_down_verdict - Determine speed down verdict
  1347. * @dev: Device of interest
  1348. *
  1349. * This function examines error ring of @dev and determines
  1350. * whether NCQ needs to be turned off, transfer speed should be
  1351. * stepped down, or falling back to PIO is necessary.
  1352. *
  1353. * Cat-1 is ATA_BUS error for any command.
  1354. *
  1355. * Cat-2 is TIMEOUT for any command or HSM violation for known
  1356. * supported commands.
  1357. *
  1358. * Cat-3 is is unclassified DEV error for known supported
  1359. * command.
  1360. *
  1361. * NCQ needs to be turned off if there have been more than 3
  1362. * Cat-2 + Cat-3 errors during last 10 minutes.
  1363. *
  1364. * Speed down is necessary if there have been more than 3 Cat-1 +
  1365. * Cat-2 errors or 10 Cat-3 errors during last 10 minutes.
  1366. *
  1367. * Falling back to PIO mode is necessary if there have been more
  1368. * than 10 Cat-1 + Cat-2 + Cat-3 errors during last 5 minutes.
  1369. *
  1370. * LOCKING:
  1371. * Inherited from caller.
  1372. *
  1373. * RETURNS:
  1374. * OR of ATA_EH_SPDN_* flags.
  1375. */
  1376. static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
  1377. {
  1378. const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
  1379. u64 j64 = get_jiffies_64();
  1380. struct speed_down_verdict_arg arg;
  1381. unsigned int verdict = 0;
  1382. /* scan past 10 mins of error history */
  1383. memset(&arg, 0, sizeof(arg));
  1384. arg.since = j64 - min(j64, j10mins);
  1385. ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
  1386. if (arg.nr_errors[2] + arg.nr_errors[3] > 3)
  1387. verdict |= ATA_EH_SPDN_NCQ_OFF;
  1388. if (arg.nr_errors[1] + arg.nr_errors[2] > 3 || arg.nr_errors[3] > 10)
  1389. verdict |= ATA_EH_SPDN_SPEED_DOWN;
  1390. /* scan past 3 mins of error history */
  1391. memset(&arg, 0, sizeof(arg));
  1392. arg.since = j64 - min(j64, j5mins);
  1393. ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
  1394. if (arg.nr_errors[1] + arg.nr_errors[2] + arg.nr_errors[3] > 10)
  1395. verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
  1396. return verdict;
  1397. }
  1398. /**
  1399. * ata_eh_speed_down - record error and speed down if necessary
  1400. * @dev: Failed device
  1401. * @is_io: Did the device fail during normal IO?
  1402. * @err_mask: err_mask of the error
  1403. *
  1404. * Record error and examine error history to determine whether
  1405. * adjusting transmission speed is necessary. It also sets
  1406. * transmission limits appropriately if such adjustment is
  1407. * necessary.
  1408. *
  1409. * LOCKING:
  1410. * Kernel thread context (may sleep).
  1411. *
  1412. * RETURNS:
  1413. * Determined recovery action.
  1414. */
  1415. static unsigned int ata_eh_speed_down(struct ata_device *dev, int is_io,
  1416. unsigned int err_mask)
  1417. {
  1418. unsigned int verdict;
  1419. unsigned int action = 0;
  1420. /* don't bother if Cat-0 error */
  1421. if (ata_eh_categorize_error(is_io, err_mask) == 0)
  1422. return 0;
  1423. /* record error and determine whether speed down is necessary */
  1424. ata_ering_record(&dev->ering, is_io, err_mask);
  1425. verdict = ata_eh_speed_down_verdict(dev);
  1426. /* turn off NCQ? */
  1427. if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
  1428. (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
  1429. ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
  1430. dev->flags |= ATA_DFLAG_NCQ_OFF;
  1431. ata_dev_printk(dev, KERN_WARNING,
  1432. "NCQ disabled due to excessive errors\n");
  1433. goto done;
  1434. }
  1435. /* speed down? */
  1436. if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
  1437. /* speed down SATA link speed if possible */
  1438. if (sata_down_spd_limit(dev->link) == 0) {
  1439. action |= ATA_EH_HARDRESET;
  1440. goto done;
  1441. }
  1442. /* lower transfer mode */
  1443. if (dev->spdn_cnt < 2) {
  1444. static const int dma_dnxfer_sel[] =
  1445. { ATA_DNXFER_DMA, ATA_DNXFER_40C };
  1446. static const int pio_dnxfer_sel[] =
  1447. { ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
  1448. int sel;
  1449. if (dev->xfer_shift != ATA_SHIFT_PIO)
  1450. sel = dma_dnxfer_sel[dev->spdn_cnt];
  1451. else
  1452. sel = pio_dnxfer_sel[dev->spdn_cnt];
  1453. dev->spdn_cnt++;
  1454. if (ata_down_xfermask_limit(dev, sel) == 0) {
  1455. action |= ATA_EH_SOFTRESET;
  1456. goto done;
  1457. }
  1458. }
  1459. }
  1460. /* Fall back to PIO? Slowing down to PIO is meaningless for
  1461. * SATA. Consider it only for PATA.
  1462. */
  1463. if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
  1464. (dev->link->ap->cbl != ATA_CBL_SATA) &&
  1465. (dev->xfer_shift != ATA_SHIFT_PIO)) {
  1466. if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
  1467. dev->spdn_cnt = 0;
  1468. action |= ATA_EH_SOFTRESET;
  1469. goto done;
  1470. }
  1471. }
  1472. return 0;
  1473. done:
  1474. /* device has been slowed down, blow error history */
  1475. ata_ering_clear(&dev->ering);
  1476. return action;
  1477. }
  1478. /**
  1479. * ata_eh_link_autopsy - analyze error and determine recovery action
  1480. * @link: host link to perform autopsy on
  1481. *
  1482. * Analyze why @link failed and determine which recovery actions
  1483. * are needed. This function also sets more detailed AC_ERR_*
  1484. * values and fills sense data for ATAPI CHECK SENSE.
  1485. *
  1486. * LOCKING:
  1487. * Kernel thread context (may sleep).
  1488. */
  1489. static void ata_eh_link_autopsy(struct ata_link *link)
  1490. {
  1491. struct ata_port *ap = link->ap;
  1492. struct ata_eh_context *ehc = &link->eh_context;
  1493. unsigned int all_err_mask = 0;
  1494. int tag, is_io = 0;
  1495. u32 serror;
  1496. int rc;
  1497. DPRINTK("ENTER\n");
  1498. if (ehc->i.flags & ATA_EHI_NO_AUTOPSY)
  1499. return;
  1500. /* obtain and analyze SError */
  1501. rc = sata_scr_read(link, SCR_ERROR, &serror);
  1502. if (rc == 0) {
  1503. ehc->i.serror |= serror;
  1504. ata_eh_analyze_serror(link);
  1505. } else if (rc != -EOPNOTSUPP) {
  1506. /* SError read failed, force hardreset and probing */
  1507. ata_ehi_schedule_probe(&ehc->i);
  1508. ehc->i.action |= ATA_EH_HARDRESET;
  1509. ehc->i.err_mask |= AC_ERR_OTHER;
  1510. }
  1511. /* analyze NCQ failure */
  1512. ata_eh_analyze_ncq_error(link);
  1513. /* any real error trumps AC_ERR_OTHER */
  1514. if (ehc->i.err_mask & ~AC_ERR_OTHER)
  1515. ehc->i.err_mask &= ~AC_ERR_OTHER;
  1516. all_err_mask |= ehc->i.err_mask;
  1517. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  1518. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  1519. if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link)
  1520. continue;
  1521. /* inherit upper level err_mask */
  1522. qc->err_mask |= ehc->i.err_mask;
  1523. /* analyze TF */
  1524. ehc->i.action |= ata_eh_analyze_tf(qc, &qc->result_tf);
  1525. /* DEV errors are probably spurious in case of ATA_BUS error */
  1526. if (qc->err_mask & AC_ERR_ATA_BUS)
  1527. qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_MEDIA |
  1528. AC_ERR_INVALID);
  1529. /* any real error trumps unknown error */
  1530. if (qc->err_mask & ~AC_ERR_OTHER)
  1531. qc->err_mask &= ~AC_ERR_OTHER;
  1532. /* SENSE_VALID trumps dev/unknown error and revalidation */
  1533. if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
  1534. qc->err_mask &= ~(AC_ERR_DEV | AC_ERR_OTHER);
  1535. ehc->i.action &= ~ATA_EH_REVALIDATE;
  1536. }
  1537. /* accumulate error info */
  1538. ehc->i.dev = qc->dev;
  1539. all_err_mask |= qc->err_mask;
  1540. if (qc->flags & ATA_QCFLAG_IO)
  1541. is_io = 1;
  1542. }
  1543. /* enforce default EH actions */
  1544. if (ap->pflags & ATA_PFLAG_FROZEN ||
  1545. all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
  1546. ehc->i.action |= ATA_EH_SOFTRESET;
  1547. else if (all_err_mask)
  1548. ehc->i.action |= ATA_EH_REVALIDATE;
  1549. /* if we have offending qcs and the associated failed device */
  1550. if (ehc->i.dev) {
  1551. /* speed down */
  1552. ehc->i.action |= ata_eh_speed_down(ehc->i.dev, is_io,
  1553. all_err_mask);
  1554. /* perform per-dev EH action only on the offending device */
  1555. ehc->i.dev_action[ehc->i.dev->devno] |=
  1556. ehc->i.action & ATA_EH_PERDEV_MASK;
  1557. ehc->i.action &= ~ATA_EH_PERDEV_MASK;
  1558. }
  1559. DPRINTK("EXIT\n");
  1560. }
  1561. /**
  1562. * ata_eh_autopsy - analyze error and determine recovery action
  1563. * @ap: host port to perform autopsy on
  1564. *
  1565. * Analyze all links of @ap and determine why they failed and
  1566. * which recovery actions are needed.
  1567. *
  1568. * LOCKING:
  1569. * Kernel thread context (may sleep).
  1570. */
  1571. void ata_eh_autopsy(struct ata_port *ap)
  1572. {
  1573. struct ata_link *link;
  1574. __ata_port_for_each_link(link, ap)
  1575. ata_eh_link_autopsy(link);
  1576. }
  1577. /**
  1578. * ata_eh_link_report - report error handling to user
  1579. * @link: ATA link EH is going on
  1580. *
  1581. * Report EH to user.
  1582. *
  1583. * LOCKING:
  1584. * None.
  1585. */
  1586. static void ata_eh_link_report(struct ata_link *link)
  1587. {
  1588. struct ata_port *ap = link->ap;
  1589. struct ata_eh_context *ehc = &link->eh_context;
  1590. const char *frozen, *desc;
  1591. char tries_buf[6];
  1592. int tag, nr_failed = 0;
  1593. desc = NULL;
  1594. if (ehc->i.desc[0] != '\0')
  1595. desc = ehc->i.desc;
  1596. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  1597. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  1598. if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link)
  1599. continue;
  1600. if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
  1601. continue;
  1602. nr_failed++;
  1603. }
  1604. if (!nr_failed && !ehc->i.err_mask)
  1605. return;
  1606. frozen = "";
  1607. if (ap->pflags & ATA_PFLAG_FROZEN)
  1608. frozen = " frozen";
  1609. memset(tries_buf, 0, sizeof(tries_buf));
  1610. if (ap->eh_tries < ATA_EH_MAX_TRIES)
  1611. snprintf(tries_buf, sizeof(tries_buf) - 1, " t%d",
  1612. ap->eh_tries);
  1613. if (ehc->i.dev) {
  1614. ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
  1615. "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
  1616. ehc->i.err_mask, link->sactive, ehc->i.serror,
  1617. ehc->i.action, frozen, tries_buf);
  1618. if (desc)
  1619. ata_dev_printk(ehc->i.dev, KERN_ERR, "%s\n", desc);
  1620. } else {
  1621. ata_link_printk(link, KERN_ERR, "exception Emask 0x%x "
  1622. "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
  1623. ehc->i.err_mask, link->sactive, ehc->i.serror,
  1624. ehc->i.action, frozen, tries_buf);
  1625. if (desc)
  1626. ata_link_printk(link, KERN_ERR, "%s\n", desc);
  1627. }
  1628. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  1629. static const char *dma_str[] = {
  1630. [DMA_BIDIRECTIONAL] = "bidi",
  1631. [DMA_TO_DEVICE] = "out",
  1632. [DMA_FROM_DEVICE] = "in",
  1633. [DMA_NONE] = "",
  1634. };
  1635. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  1636. struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
  1637. if (!(qc->flags & ATA_QCFLAG_FAILED) ||
  1638. qc->dev->link != link || !qc->err_mask)
  1639. continue;
  1640. ata_dev_printk(qc->dev, KERN_ERR,
  1641. "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
  1642. "tag %d cdb 0x%x data %u %s\n "
  1643. "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
  1644. "Emask 0x%x (%s)%s\n",
  1645. cmd->command, cmd->feature, cmd->nsect,
  1646. cmd->lbal, cmd->lbam, cmd->lbah,
  1647. cmd->hob_feature, cmd->hob_nsect,
  1648. cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
  1649. cmd->device, qc->tag, qc->cdb[0], qc->nbytes,
  1650. dma_str[qc->dma_dir],
  1651. res->command, res->feature, res->nsect,
  1652. res->lbal, res->lbam, res->lbah,
  1653. res->hob_feature, res->hob_nsect,
  1654. res->hob_lbal, res->hob_lbam, res->hob_lbah,
  1655. res->device, qc->err_mask, ata_err_string(qc->err_mask),
  1656. qc->err_mask & AC_ERR_NCQ ? " <F>" : "");
  1657. }
  1658. }
  1659. /**
  1660. * ata_eh_report - report error handling to user
  1661. * @ap: ATA port to report EH about
  1662. *
  1663. * Report EH to user.
  1664. *
  1665. * LOCKING:
  1666. * None.
  1667. */
  1668. void ata_eh_report(struct ata_port *ap)
  1669. {
  1670. struct ata_link *link;
  1671. __ata_port_for_each_link(link, ap)
  1672. ata_eh_link_report(link);
  1673. }
  1674. static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset,
  1675. unsigned int *classes, unsigned long deadline)
  1676. {
  1677. struct ata_device *dev;
  1678. int rc;
  1679. ata_link_for_each_dev(dev, link)
  1680. classes[dev->devno] = ATA_DEV_UNKNOWN;
  1681. rc = reset(link, classes, deadline);
  1682. if (rc)
  1683. return rc;
  1684. /* If any class isn't ATA_DEV_UNKNOWN, consider classification
  1685. * is complete and convert all ATA_DEV_UNKNOWN to
  1686. * ATA_DEV_NONE.
  1687. */
  1688. ata_link_for_each_dev(dev, link)
  1689. if (classes[dev->devno] != ATA_DEV_UNKNOWN)
  1690. break;
  1691. if (dev) {
  1692. ata_link_for_each_dev(dev, link) {
  1693. if (classes[dev->devno] == ATA_DEV_UNKNOWN)
  1694. classes[dev->devno] = ATA_DEV_NONE;
  1695. }
  1696. }
  1697. return 0;
  1698. }
  1699. static int ata_eh_followup_srst_needed(struct ata_link *link,
  1700. int rc, int classify,
  1701. const unsigned int *classes)
  1702. {
  1703. if (link->flags & ATA_LFLAG_NO_SRST)
  1704. return 0;
  1705. if (rc == -EAGAIN)
  1706. return 1;
  1707. if (rc != 0)
  1708. return 0;
  1709. if ((link->ap->flags & ATA_FLAG_PMP) && ata_is_host_link(link))
  1710. return 1;
  1711. if (classify && !(link->flags & ATA_LFLAG_ASSUME_CLASS) &&
  1712. classes[0] == ATA_DEV_UNKNOWN)
  1713. return 1;
  1714. return 0;
  1715. }
  1716. int ata_eh_reset(struct ata_link *link, int classify,
  1717. ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
  1718. ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
  1719. {
  1720. struct ata_eh_context *ehc = &link->eh_context;
  1721. unsigned int *classes = ehc->classes;
  1722. int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
  1723. int try = 0;
  1724. struct ata_device *dev;
  1725. unsigned long deadline;
  1726. unsigned int action;
  1727. ata_reset_fn_t reset;
  1728. int rc;
  1729. /* about to reset */
  1730. ata_eh_about_to_do(link, NULL, ehc->i.action & ATA_EH_RESET_MASK);
  1731. /* Determine which reset to use and record in ehc->i.action.
  1732. * prereset() may examine and modify it.
  1733. */
  1734. action = ehc->i.action;
  1735. ehc->i.action &= ~ATA_EH_RESET_MASK;
  1736. if (softreset && (!hardreset || (!(link->flags & ATA_LFLAG_NO_SRST) &&
  1737. !sata_set_spd_needed(link) &&
  1738. !(action & ATA_EH_HARDRESET))))
  1739. ehc->i.action |= ATA_EH_SOFTRESET;
  1740. else
  1741. ehc->i.action |= ATA_EH_HARDRESET;
  1742. if (prereset) {
  1743. rc = prereset(link, jiffies + ATA_EH_PRERESET_TIMEOUT);
  1744. if (rc) {
  1745. if (rc == -ENOENT) {
  1746. ata_link_printk(link, KERN_DEBUG,
  1747. "port disabled. ignoring.\n");
  1748. ehc->i.action &= ~ATA_EH_RESET_MASK;
  1749. ata_link_for_each_dev(dev, link)
  1750. classes[dev->devno] = ATA_DEV_NONE;
  1751. rc = 0;
  1752. } else
  1753. ata_link_printk(link, KERN_ERR,
  1754. "prereset failed (errno=%d)\n", rc);
  1755. goto out;
  1756. }
  1757. }
  1758. /* prereset() might have modified ehc->i.action */
  1759. if (ehc->i.action & ATA_EH_HARDRESET)
  1760. reset = hardreset;
  1761. else if (ehc->i.action & ATA_EH_SOFTRESET)
  1762. reset = softreset;
  1763. else {
  1764. /* prereset told us not to reset, bang classes and return */
  1765. ata_link_for_each_dev(dev, link)
  1766. classes[dev->devno] = ATA_DEV_NONE;
  1767. rc = 0;
  1768. goto out;
  1769. }
  1770. /* did prereset() screw up? if so, fix up to avoid oopsing */
  1771. if (!reset) {
  1772. if (softreset)
  1773. reset = softreset;
  1774. else
  1775. reset = hardreset;
  1776. }
  1777. retry:
  1778. deadline = jiffies + ata_eh_reset_timeouts[try++];
  1779. /* shut up during boot probing */
  1780. if (verbose)
  1781. ata_link_printk(link, KERN_INFO, "%s resetting link\n",
  1782. reset == softreset ? "soft" : "hard");
  1783. /* mark that this EH session started with reset */
  1784. if (reset == hardreset)
  1785. ehc->i.flags |= ATA_EHI_DID_HARDRESET;
  1786. else
  1787. ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
  1788. rc = ata_do_reset(link, reset, classes, deadline);
  1789. if (reset == hardreset &&
  1790. ata_eh_followup_srst_needed(link, rc, classify, classes)) {
  1791. /* okay, let's do follow-up softreset */
  1792. reset = softreset;
  1793. if (!reset) {
  1794. ata_link_printk(link, KERN_ERR,
  1795. "follow-up softreset required "
  1796. "but no softreset avaliable\n");
  1797. rc = -EINVAL;
  1798. goto out;
  1799. }
  1800. ata_eh_about_to_do(link, NULL, ATA_EH_RESET_MASK);
  1801. rc = ata_do_reset(link, reset, classes, deadline);
  1802. if (rc == 0 && classify && classes[0] == ATA_DEV_UNKNOWN &&
  1803. !(link->flags & ATA_LFLAG_ASSUME_CLASS)) {
  1804. ata_link_printk(link, KERN_ERR,
  1805. "classification failed\n");
  1806. rc = -EINVAL;
  1807. goto out;
  1808. }
  1809. }
  1810. /* if we skipped follow-up srst, clear rc */
  1811. if (rc == -EAGAIN)
  1812. rc = 0;
  1813. if (rc && rc != -ERESTART && try < ARRAY_SIZE(ata_eh_reset_timeouts)) {
  1814. unsigned long now = jiffies;
  1815. if (time_before(now, deadline)) {
  1816. unsigned long delta = deadline - jiffies;
  1817. ata_link_printk(link, KERN_WARNING, "reset failed "
  1818. "(errno=%d), retrying in %u secs\n",
  1819. rc, (jiffies_to_msecs(delta) + 999) / 1000);
  1820. schedule_timeout_uninterruptible(delta);
  1821. }
  1822. if (rc == -EPIPE ||
  1823. try == ARRAY_SIZE(ata_eh_reset_timeouts) - 1)
  1824. sata_down_spd_limit(link);
  1825. if (hardreset)
  1826. reset = hardreset;
  1827. goto retry;
  1828. }
  1829. if (rc == 0) {
  1830. u32 sstatus;
  1831. ata_link_for_each_dev(dev, link) {
  1832. /* After the reset, the device state is PIO 0
  1833. * and the controller state is undefined.
  1834. * Record the mode.
  1835. */
  1836. dev->pio_mode = XFER_PIO_0;
  1837. if (ata_link_offline(link))
  1838. continue;
  1839. /* apply class override and convert UNKNOWN to NONE */
  1840. if (link->flags & ATA_LFLAG_ASSUME_ATA)
  1841. classes[dev->devno] = ATA_DEV_ATA;
  1842. else if (link->flags & ATA_LFLAG_ASSUME_SEMB)
  1843. classes[dev->devno] = ATA_DEV_SEMB_UNSUP; /* not yet */
  1844. else if (classes[dev->devno] == ATA_DEV_UNKNOWN)
  1845. classes[dev->devno] = ATA_DEV_NONE;
  1846. }
  1847. /* record current link speed */
  1848. if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0)
  1849. link->sata_spd = (sstatus >> 4) & 0xf;
  1850. if (postreset)
  1851. postreset(link, classes);
  1852. /* reset successful, schedule revalidation */
  1853. ata_eh_done(link, NULL, ehc->i.action & ATA_EH_RESET_MASK);
  1854. ehc->i.action |= ATA_EH_REVALIDATE;
  1855. }
  1856. out:
  1857. /* clear hotplug flag */
  1858. ehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
  1859. return rc;
  1860. }
  1861. static int ata_eh_revalidate_and_attach(struct ata_link *link,
  1862. struct ata_device **r_failed_dev)
  1863. {
  1864. struct ata_port *ap = link->ap;
  1865. struct ata_eh_context *ehc = &link->eh_context;
  1866. struct ata_device *dev;
  1867. unsigned int new_mask = 0;
  1868. unsigned long flags;
  1869. int rc = 0;
  1870. DPRINTK("ENTER\n");
  1871. /* For PATA drive side cable detection to work, IDENTIFY must
  1872. * be done backwards such that PDIAG- is released by the slave
  1873. * device before the master device is identified.
  1874. */
  1875. ata_link_for_each_dev_reverse(dev, link) {
  1876. unsigned int action = ata_eh_dev_action(dev);
  1877. unsigned int readid_flags = 0;
  1878. if (ehc->i.flags & ATA_EHI_DID_RESET)
  1879. readid_flags |= ATA_READID_POSTRESET;
  1880. if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
  1881. WARN_ON(dev->class == ATA_DEV_PMP);
  1882. if (ata_link_offline(link)) {
  1883. rc = -EIO;
  1884. goto err;
  1885. }
  1886. ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE);
  1887. rc = ata_dev_revalidate(dev, ehc->classes[dev->devno],
  1888. readid_flags);
  1889. if (rc)
  1890. goto err;
  1891. ata_eh_done(link, dev, ATA_EH_REVALIDATE);
  1892. /* Configuration may have changed, reconfigure
  1893. * transfer mode.
  1894. */
  1895. ehc->i.flags |= ATA_EHI_SETMODE;
  1896. /* schedule the scsi_rescan_device() here */
  1897. queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
  1898. } else if (dev->class == ATA_DEV_UNKNOWN &&
  1899. ehc->tries[dev->devno] &&
  1900. ata_class_enabled(ehc->classes[dev->devno])) {
  1901. dev->class = ehc->classes[dev->devno];
  1902. if (dev->class == ATA_DEV_PMP)
  1903. rc = sata_pmp_attach(dev);
  1904. else
  1905. rc = ata_dev_read_id(dev, &dev->class,
  1906. readid_flags, dev->id);
  1907. switch (rc) {
  1908. case 0:
  1909. new_mask |= 1 << dev->devno;
  1910. break;
  1911. case -ENOENT:
  1912. /* IDENTIFY was issued to non-existent
  1913. * device. No need to reset. Just
  1914. * thaw and kill the device.
  1915. */
  1916. ata_eh_thaw_port(ap);
  1917. dev->class = ATA_DEV_UNKNOWN;
  1918. break;
  1919. default:
  1920. dev->class = ATA_DEV_UNKNOWN;
  1921. goto err;
  1922. }
  1923. }
  1924. }
  1925. /* PDIAG- should have been released, ask cable type if post-reset */
  1926. if (ata_is_host_link(link) && ap->ops->cable_detect &&
  1927. (ehc->i.flags & ATA_EHI_DID_RESET))
  1928. ap->cbl = ap->ops->cable_detect(ap);
  1929. /* Configure new devices forward such that user doesn't see
  1930. * device detection messages backwards.
  1931. */
  1932. ata_link_for_each_dev(dev, link) {
  1933. if (!(new_mask & (1 << dev->devno)) ||
  1934. dev->class == ATA_DEV_PMP)
  1935. continue;
  1936. ehc->i.flags |= ATA_EHI_PRINTINFO;
  1937. rc = ata_dev_configure(dev);
  1938. ehc->i.flags &= ~ATA_EHI_PRINTINFO;
  1939. if (rc)
  1940. goto err;
  1941. spin_lock_irqsave(ap->lock, flags);
  1942. ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
  1943. spin_unlock_irqrestore(ap->lock, flags);
  1944. /* new device discovered, configure xfermode */
  1945. ehc->i.flags |= ATA_EHI_SETMODE;
  1946. }
  1947. return 0;
  1948. err:
  1949. *r_failed_dev = dev;
  1950. DPRINTK("EXIT rc=%d\n", rc);
  1951. return rc;
  1952. }
  1953. static int ata_link_nr_enabled(struct ata_link *link)
  1954. {
  1955. struct ata_device *dev;
  1956. int cnt = 0;
  1957. ata_link_for_each_dev(dev, link)
  1958. if (ata_dev_enabled(dev))
  1959. cnt++;
  1960. return cnt;
  1961. }
  1962. static int ata_link_nr_vacant(struct ata_link *link)
  1963. {
  1964. struct ata_device *dev;
  1965. int cnt = 0;
  1966. ata_link_for_each_dev(dev, link)
  1967. if (dev->class == ATA_DEV_UNKNOWN)
  1968. cnt++;
  1969. return cnt;
  1970. }
  1971. static int ata_eh_skip_recovery(struct ata_link *link)
  1972. {
  1973. struct ata_eh_context *ehc = &link->eh_context;
  1974. struct ata_device *dev;
  1975. /* skip disabled links */
  1976. if (link->flags & ATA_LFLAG_DISABLED)
  1977. return 1;
  1978. /* thaw frozen port, resume link and recover failed devices */
  1979. if ((link->ap->pflags & ATA_PFLAG_FROZEN) ||
  1980. (ehc->i.flags & ATA_EHI_RESUME_LINK) || ata_link_nr_enabled(link))
  1981. return 0;
  1982. /* skip if class codes for all vacant slots are ATA_DEV_NONE */
  1983. ata_link_for_each_dev(dev, link) {
  1984. if (dev->class == ATA_DEV_UNKNOWN &&
  1985. ehc->classes[dev->devno] != ATA_DEV_NONE)
  1986. return 0;
  1987. }
  1988. return 1;
  1989. }
  1990. static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
  1991. {
  1992. struct ata_eh_context *ehc = &dev->link->eh_context;
  1993. ehc->tries[dev->devno]--;
  1994. switch (err) {
  1995. case -ENODEV:
  1996. /* device missing or wrong IDENTIFY data, schedule probing */
  1997. ehc->i.probe_mask |= (1 << dev->devno);
  1998. case -EINVAL:
  1999. /* give it just one more chance */
  2000. ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
  2001. case -EIO:
  2002. if (ehc->tries[dev->devno] == 1) {
  2003. /* This is the last chance, better to slow
  2004. * down than lose it.
  2005. */
  2006. sata_down_spd_limit(dev->link);
  2007. ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
  2008. }
  2009. }
  2010. if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
  2011. /* disable device if it has used up all its chances */
  2012. ata_dev_disable(dev);
  2013. /* detach if offline */
  2014. if (ata_link_offline(dev->link))
  2015. ata_eh_detach_dev(dev);
  2016. /* probe if requested */
  2017. if ((ehc->i.probe_mask & (1 << dev->devno)) &&
  2018. !(ehc->did_probe_mask & (1 << dev->devno))) {
  2019. ata_eh_detach_dev(dev);
  2020. ata_dev_init(dev);
  2021. ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
  2022. ehc->did_probe_mask |= (1 << dev->devno);
  2023. ehc->i.action |= ATA_EH_SOFTRESET;
  2024. }
  2025. return 1;
  2026. } else {
  2027. /* soft didn't work? be haaaaard */
  2028. if (ehc->i.flags & ATA_EHI_DID_RESET)
  2029. ehc->i.action |= ATA_EH_HARDRESET;
  2030. else
  2031. ehc->i.action |= ATA_EH_SOFTRESET;
  2032. return 0;
  2033. }
  2034. }
  2035. /**
  2036. * ata_eh_recover - recover host port after error
  2037. * @ap: host port to recover
  2038. * @prereset: prereset method (can be NULL)
  2039. * @softreset: softreset method (can be NULL)
  2040. * @hardreset: hardreset method (can be NULL)
  2041. * @postreset: postreset method (can be NULL)
  2042. * @r_failed_link: out parameter for failed link
  2043. *
  2044. * This is the alpha and omega, eum and yang, heart and soul of
  2045. * libata exception handling. On entry, actions required to
  2046. * recover each link and hotplug requests are recorded in the
  2047. * link's eh_context. This function executes all the operations
  2048. * with appropriate retrials and fallbacks to resurrect failed
  2049. * devices, detach goners and greet newcomers.
  2050. *
  2051. * LOCKING:
  2052. * Kernel thread context (may sleep).
  2053. *
  2054. * RETURNS:
  2055. * 0 on success, -errno on failure.
  2056. */
  2057. int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
  2058. ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
  2059. ata_postreset_fn_t postreset,
  2060. struct ata_link **r_failed_link)
  2061. {
  2062. struct ata_link *link;
  2063. struct ata_device *dev;
  2064. int nr_failed_devs, nr_disabled_devs;
  2065. int reset, rc;
  2066. unsigned long flags;
  2067. DPRINTK("ENTER\n");
  2068. /* prep for recovery */
  2069. ata_port_for_each_link(link, ap) {
  2070. struct ata_eh_context *ehc = &link->eh_context;
  2071. /* re-enable link? */
  2072. if (ehc->i.action & ATA_EH_ENABLE_LINK) {
  2073. ata_eh_about_to_do(link, NULL, ATA_EH_ENABLE_LINK);
  2074. spin_lock_irqsave(ap->lock, flags);
  2075. link->flags &= ~ATA_LFLAG_DISABLED;
  2076. spin_unlock_irqrestore(ap->lock, flags);
  2077. ata_eh_done(link, NULL, ATA_EH_ENABLE_LINK);
  2078. }
  2079. ata_link_for_each_dev(dev, link) {
  2080. if (link->flags & ATA_LFLAG_NO_RETRY)
  2081. ehc->tries[dev->devno] = 1;
  2082. else
  2083. ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
  2084. /* collect port action mask recorded in dev actions */
  2085. ehc->i.action |= ehc->i.dev_action[dev->devno] &
  2086. ~ATA_EH_PERDEV_MASK;
  2087. ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK;
  2088. /* process hotplug request */
  2089. if (dev->flags & ATA_DFLAG_DETACH)
  2090. ata_eh_detach_dev(dev);
  2091. if (!ata_dev_enabled(dev) &&
  2092. ((ehc->i.probe_mask & (1 << dev->devno)) &&
  2093. !(ehc->did_probe_mask & (1 << dev->devno)))) {
  2094. ata_eh_detach_dev(dev);
  2095. ata_dev_init(dev);
  2096. ehc->did_probe_mask |= (1 << dev->devno);
  2097. ehc->i.action |= ATA_EH_SOFTRESET;
  2098. }
  2099. }
  2100. }
  2101. retry:
  2102. rc = 0;
  2103. nr_failed_devs = 0;
  2104. nr_disabled_devs = 0;
  2105. reset = 0;
  2106. /* if UNLOADING, finish immediately */
  2107. if (ap->pflags & ATA_PFLAG_UNLOADING)
  2108. goto out;
  2109. /* prep for EH */
  2110. ata_port_for_each_link(link, ap) {
  2111. struct ata_eh_context *ehc = &link->eh_context;
  2112. /* skip EH if possible. */
  2113. if (ata_eh_skip_recovery(link))
  2114. ehc->i.action = 0;
  2115. /* do we need to reset? */
  2116. if (ehc->i.action & ATA_EH_RESET_MASK)
  2117. reset = 1;
  2118. ata_link_for_each_dev(dev, link)
  2119. ehc->classes[dev->devno] = ATA_DEV_UNKNOWN;
  2120. }
  2121. /* reset */
  2122. if (reset) {
  2123. ata_eh_freeze_port(ap);
  2124. ata_port_for_each_link(link, ap) {
  2125. struct ata_eh_context *ehc = &link->eh_context;
  2126. if (!(ehc->i.action & ATA_EH_RESET_MASK))
  2127. continue;
  2128. rc = ata_eh_reset(link, ata_link_nr_vacant(link),
  2129. prereset, softreset, hardreset,
  2130. postreset);
  2131. if (rc) {
  2132. ata_link_printk(link, KERN_ERR,
  2133. "reset failed, giving up\n");
  2134. goto out;
  2135. }
  2136. }
  2137. ata_eh_thaw_port(ap);
  2138. }
  2139. /* the rest */
  2140. ata_port_for_each_link(link, ap) {
  2141. struct ata_eh_context *ehc = &link->eh_context;
  2142. /* revalidate existing devices and attach new ones */
  2143. rc = ata_eh_revalidate_and_attach(link, &dev);
  2144. if (rc)
  2145. goto dev_fail;
  2146. /* if PMP got attached, return, pmp EH will take care of it */
  2147. if (link->device->class == ATA_DEV_PMP) {
  2148. ehc->i.action = 0;
  2149. return 0;
  2150. }
  2151. /* configure transfer mode if necessary */
  2152. if (ehc->i.flags & ATA_EHI_SETMODE) {
  2153. rc = ata_set_mode(link, &dev);
  2154. if (rc)
  2155. goto dev_fail;
  2156. ehc->i.flags &= ~ATA_EHI_SETMODE;
  2157. }
  2158. /* this link is okay now */
  2159. ehc->i.flags = 0;
  2160. continue;
  2161. dev_fail:
  2162. nr_failed_devs++;
  2163. if (ata_eh_handle_dev_fail(dev, rc))
  2164. nr_disabled_devs++;
  2165. if (ap->pflags & ATA_PFLAG_FROZEN)
  2166. break;
  2167. }
  2168. if (nr_failed_devs) {
  2169. if (nr_failed_devs != nr_disabled_devs) {
  2170. ata_port_printk(ap, KERN_WARNING, "failed to recover "
  2171. "some devices, retrying in 5 secs\n");
  2172. ssleep(5);
  2173. } else {
  2174. /* no device left to recover, repeat fast */
  2175. msleep(500);
  2176. }
  2177. goto retry;
  2178. }
  2179. out:
  2180. if (rc && r_failed_link)
  2181. *r_failed_link = link;
  2182. DPRINTK("EXIT, rc=%d\n", rc);
  2183. return rc;
  2184. }
  2185. /**
  2186. * ata_eh_finish - finish up EH
  2187. * @ap: host port to finish EH for
  2188. *
  2189. * Recovery is complete. Clean up EH states and retry or finish
  2190. * failed qcs.
  2191. *
  2192. * LOCKING:
  2193. * None.
  2194. */
  2195. void ata_eh_finish(struct ata_port *ap)
  2196. {
  2197. int tag;
  2198. /* retry or finish qcs */
  2199. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  2200. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  2201. if (!(qc->flags & ATA_QCFLAG_FAILED))
  2202. continue;
  2203. if (qc->err_mask) {
  2204. /* FIXME: Once EH migration is complete,
  2205. * generate sense data in this function,
  2206. * considering both err_mask and tf.
  2207. */
  2208. if (qc->err_mask & AC_ERR_INVALID)
  2209. ata_eh_qc_complete(qc);
  2210. else
  2211. ata_eh_qc_retry(qc);
  2212. } else {
  2213. if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
  2214. ata_eh_qc_complete(qc);
  2215. } else {
  2216. /* feed zero TF to sense generation */
  2217. memset(&qc->result_tf, 0, sizeof(qc->result_tf));
  2218. ata_eh_qc_retry(qc);
  2219. }
  2220. }
  2221. }
  2222. /* make sure nr_active_links is zero after EH */
  2223. WARN_ON(ap->nr_active_links);
  2224. ap->nr_active_links = 0;
  2225. }
  2226. /**
  2227. * ata_do_eh - do standard error handling
  2228. * @ap: host port to handle error for
  2229. * @prereset: prereset method (can be NULL)
  2230. * @softreset: softreset method (can be NULL)
  2231. * @hardreset: hardreset method (can be NULL)
  2232. * @postreset: postreset method (can be NULL)
  2233. *
  2234. * Perform standard error handling sequence.
  2235. *
  2236. * LOCKING:
  2237. * Kernel thread context (may sleep).
  2238. */
  2239. void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
  2240. ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
  2241. ata_postreset_fn_t postreset)
  2242. {
  2243. struct ata_device *dev;
  2244. int rc;
  2245. ata_eh_autopsy(ap);
  2246. ata_eh_report(ap);
  2247. rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset,
  2248. NULL);
  2249. if (rc) {
  2250. ata_link_for_each_dev(dev, &ap->link)
  2251. ata_dev_disable(dev);
  2252. }
  2253. ata_eh_finish(ap);
  2254. }
  2255. #ifdef CONFIG_PM
  2256. /**
  2257. * ata_eh_handle_port_suspend - perform port suspend operation
  2258. * @ap: port to suspend
  2259. *
  2260. * Suspend @ap.
  2261. *
  2262. * LOCKING:
  2263. * Kernel thread context (may sleep).
  2264. */
  2265. static void ata_eh_handle_port_suspend(struct ata_port *ap)
  2266. {
  2267. unsigned long flags;
  2268. int rc = 0;
  2269. /* are we suspending? */
  2270. spin_lock_irqsave(ap->lock, flags);
  2271. if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
  2272. ap->pm_mesg.event == PM_EVENT_ON) {
  2273. spin_unlock_irqrestore(ap->lock, flags);
  2274. return;
  2275. }
  2276. spin_unlock_irqrestore(ap->lock, flags);
  2277. WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
  2278. /* tell ACPI we're suspending */
  2279. rc = ata_acpi_on_suspend(ap);
  2280. if (rc)
  2281. goto out;
  2282. /* suspend */
  2283. ata_eh_freeze_port(ap);
  2284. if (ap->ops->port_suspend)
  2285. rc = ap->ops->port_suspend(ap, ap->pm_mesg);
  2286. out:
  2287. /* report result */
  2288. spin_lock_irqsave(ap->lock, flags);
  2289. ap->pflags &= ~ATA_PFLAG_PM_PENDING;
  2290. if (rc == 0)
  2291. ap->pflags |= ATA_PFLAG_SUSPENDED;
  2292. else if (ap->pflags & ATA_PFLAG_FROZEN)
  2293. ata_port_schedule_eh(ap);
  2294. if (ap->pm_result) {
  2295. *ap->pm_result = rc;
  2296. ap->pm_result = NULL;
  2297. }
  2298. spin_unlock_irqrestore(ap->lock, flags);
  2299. return;
  2300. }
  2301. /**
  2302. * ata_eh_handle_port_resume - perform port resume operation
  2303. * @ap: port to resume
  2304. *
  2305. * Resume @ap.
  2306. *
  2307. * LOCKING:
  2308. * Kernel thread context (may sleep).
  2309. */
  2310. static void ata_eh_handle_port_resume(struct ata_port *ap)
  2311. {
  2312. unsigned long flags;
  2313. int rc = 0;
  2314. /* are we resuming? */
  2315. spin_lock_irqsave(ap->lock, flags);
  2316. if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
  2317. ap->pm_mesg.event != PM_EVENT_ON) {
  2318. spin_unlock_irqrestore(ap->lock, flags);
  2319. return;
  2320. }
  2321. spin_unlock_irqrestore(ap->lock, flags);
  2322. WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
  2323. if (ap->ops->port_resume)
  2324. rc = ap->ops->port_resume(ap);
  2325. /* tell ACPI that we're resuming */
  2326. ata_acpi_on_resume(ap);
  2327. /* report result */
  2328. spin_lock_irqsave(ap->lock, flags);
  2329. ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
  2330. if (ap->pm_result) {
  2331. *ap->pm_result = rc;
  2332. ap->pm_result = NULL;
  2333. }
  2334. spin_unlock_irqrestore(ap->lock, flags);
  2335. }
  2336. #endif /* CONFIG_PM */