libata-eh.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802
  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 <linux/pci.h>
  36. #include <scsi/scsi.h>
  37. #include <scsi/scsi_host.h>
  38. #include <scsi/scsi_eh.h>
  39. #include <scsi/scsi_device.h>
  40. #include <scsi/scsi_cmnd.h>
  41. #include "../scsi/scsi_transport_api.h"
  42. #include <linux/libata.h>
  43. #include "libata.h"
  44. enum {
  45. ATA_EH_SPDN_NCQ_OFF = (1 << 0),
  46. ATA_EH_SPDN_SPEED_DOWN = (1 << 1),
  47. ATA_EH_SPDN_FALLBACK_TO_PIO = (1 << 2),
  48. };
  49. /* Waiting in ->prereset can never be reliable. It's sometimes nice
  50. * to wait there but it can't be depended upon; otherwise, we wouldn't
  51. * be resetting. Just give it enough time for most drives to spin up.
  52. */
  53. enum {
  54. ATA_EH_PRERESET_TIMEOUT = 10 * HZ,
  55. ATA_EH_FASTDRAIN_INTERVAL = 3 * HZ,
  56. };
  57. /* The following table determines how we sequence resets. Each entry
  58. * represents timeout for that try. The first try can be soft or
  59. * hardreset. All others are hardreset if available. In most cases
  60. * the first reset w/ 10sec timeout should succeed. Following entries
  61. * are mostly for error handling, hotplug and retarded devices.
  62. */
  63. static const unsigned long ata_eh_reset_timeouts[] = {
  64. 10 * HZ, /* most drives spin up by 10sec */
  65. 10 * HZ, /* > 99% working drives spin up before 20sec */
  66. 35 * HZ, /* give > 30 secs of idleness for retarded devices */
  67. 5 * HZ, /* and sweet one last chance */
  68. /* > 1 min has elapsed, give up */
  69. };
  70. static void __ata_port_freeze(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. ap->excl_link = NULL; /* don't maintain exclusion over EH */
  394. spin_unlock_irqrestore(ap->lock, flags);
  395. /* invoke EH, skip if unloading or suspended */
  396. if (!(ap->pflags & (ATA_PFLAG_UNLOADING | ATA_PFLAG_SUSPENDED)))
  397. ap->ops->error_handler(ap);
  398. else
  399. ata_eh_finish(ap);
  400. /* process port suspend request */
  401. ata_eh_handle_port_suspend(ap);
  402. /* Exception might have happend after ->error_handler
  403. * recovered the port but before this point. Repeat
  404. * EH in such case.
  405. */
  406. spin_lock_irqsave(ap->lock, flags);
  407. if (ap->pflags & ATA_PFLAG_EH_PENDING) {
  408. if (--ap->eh_tries) {
  409. spin_unlock_irqrestore(ap->lock, flags);
  410. goto repeat;
  411. }
  412. ata_port_printk(ap, KERN_ERR, "EH pending after %d "
  413. "tries, giving up\n", ATA_EH_MAX_TRIES);
  414. ap->pflags &= ~ATA_PFLAG_EH_PENDING;
  415. }
  416. /* this run is complete, make sure EH info is clear */
  417. __ata_port_for_each_link(link, ap)
  418. memset(&link->eh_info, 0, sizeof(link->eh_info));
  419. /* Clear host_eh_scheduled while holding ap->lock such
  420. * that if exception occurs after this point but
  421. * before EH completion, SCSI midlayer will
  422. * re-initiate EH.
  423. */
  424. host->host_eh_scheduled = 0;
  425. spin_unlock_irqrestore(ap->lock, flags);
  426. } else {
  427. WARN_ON(ata_qc_from_tag(ap, ap->link.active_tag) == NULL);
  428. ap->ops->eng_timeout(ap);
  429. }
  430. /* finish or retry handled scmd's and clean up */
  431. WARN_ON(host->host_failed || !list_empty(&host->eh_cmd_q));
  432. scsi_eh_flush_done_q(&ap->eh_done_q);
  433. /* clean up */
  434. spin_lock_irqsave(ap->lock, flags);
  435. if (ap->pflags & ATA_PFLAG_LOADING)
  436. ap->pflags &= ~ATA_PFLAG_LOADING;
  437. else if (ap->pflags & ATA_PFLAG_SCSI_HOTPLUG)
  438. queue_delayed_work(ata_aux_wq, &ap->hotplug_task, 0);
  439. if (ap->pflags & ATA_PFLAG_RECOVERED)
  440. ata_port_printk(ap, KERN_INFO, "EH complete\n");
  441. ap->pflags &= ~(ATA_PFLAG_SCSI_HOTPLUG | ATA_PFLAG_RECOVERED);
  442. /* tell wait_eh that we're done */
  443. ap->pflags &= ~ATA_PFLAG_EH_IN_PROGRESS;
  444. wake_up_all(&ap->eh_wait_q);
  445. spin_unlock_irqrestore(ap->lock, flags);
  446. DPRINTK("EXIT\n");
  447. }
  448. /**
  449. * ata_port_wait_eh - Wait for the currently pending EH to complete
  450. * @ap: Port to wait EH for
  451. *
  452. * Wait until the currently pending EH is complete.
  453. *
  454. * LOCKING:
  455. * Kernel thread context (may sleep).
  456. */
  457. void ata_port_wait_eh(struct ata_port *ap)
  458. {
  459. unsigned long flags;
  460. DEFINE_WAIT(wait);
  461. retry:
  462. spin_lock_irqsave(ap->lock, flags);
  463. while (ap->pflags & (ATA_PFLAG_EH_PENDING | ATA_PFLAG_EH_IN_PROGRESS)) {
  464. prepare_to_wait(&ap->eh_wait_q, &wait, TASK_UNINTERRUPTIBLE);
  465. spin_unlock_irqrestore(ap->lock, flags);
  466. schedule();
  467. spin_lock_irqsave(ap->lock, flags);
  468. }
  469. finish_wait(&ap->eh_wait_q, &wait);
  470. spin_unlock_irqrestore(ap->lock, flags);
  471. /* make sure SCSI EH is complete */
  472. if (scsi_host_in_recovery(ap->scsi_host)) {
  473. msleep(10);
  474. goto retry;
  475. }
  476. }
  477. static int ata_eh_nr_in_flight(struct ata_port *ap)
  478. {
  479. unsigned int tag;
  480. int nr = 0;
  481. /* count only non-internal commands */
  482. for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++)
  483. if (ata_qc_from_tag(ap, tag))
  484. nr++;
  485. return nr;
  486. }
  487. void ata_eh_fastdrain_timerfn(unsigned long arg)
  488. {
  489. struct ata_port *ap = (void *)arg;
  490. unsigned long flags;
  491. int cnt;
  492. spin_lock_irqsave(ap->lock, flags);
  493. cnt = ata_eh_nr_in_flight(ap);
  494. /* are we done? */
  495. if (!cnt)
  496. goto out_unlock;
  497. if (cnt == ap->fastdrain_cnt) {
  498. unsigned int tag;
  499. /* No progress during the last interval, tag all
  500. * in-flight qcs as timed out and freeze the port.
  501. */
  502. for (tag = 0; tag < ATA_MAX_QUEUE - 1; tag++) {
  503. struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
  504. if (qc)
  505. qc->err_mask |= AC_ERR_TIMEOUT;
  506. }
  507. ata_port_freeze(ap);
  508. } else {
  509. /* some qcs have finished, give it another chance */
  510. ap->fastdrain_cnt = cnt;
  511. ap->fastdrain_timer.expires =
  512. jiffies + ATA_EH_FASTDRAIN_INTERVAL;
  513. add_timer(&ap->fastdrain_timer);
  514. }
  515. out_unlock:
  516. spin_unlock_irqrestore(ap->lock, flags);
  517. }
  518. /**
  519. * ata_eh_set_pending - set ATA_PFLAG_EH_PENDING and activate fast drain
  520. * @ap: target ATA port
  521. * @fastdrain: activate fast drain
  522. *
  523. * Set ATA_PFLAG_EH_PENDING and activate fast drain if @fastdrain
  524. * is non-zero and EH wasn't pending before. Fast drain ensures
  525. * that EH kicks in in timely manner.
  526. *
  527. * LOCKING:
  528. * spin_lock_irqsave(host lock)
  529. */
  530. static void ata_eh_set_pending(struct ata_port *ap, int fastdrain)
  531. {
  532. int cnt;
  533. /* already scheduled? */
  534. if (ap->pflags & ATA_PFLAG_EH_PENDING)
  535. return;
  536. ap->pflags |= ATA_PFLAG_EH_PENDING;
  537. if (!fastdrain)
  538. return;
  539. /* do we have in-flight qcs? */
  540. cnt = ata_eh_nr_in_flight(ap);
  541. if (!cnt)
  542. return;
  543. /* activate fast drain */
  544. ap->fastdrain_cnt = cnt;
  545. ap->fastdrain_timer.expires = jiffies + ATA_EH_FASTDRAIN_INTERVAL;
  546. add_timer(&ap->fastdrain_timer);
  547. }
  548. /**
  549. * ata_qc_schedule_eh - schedule qc for error handling
  550. * @qc: command to schedule error handling for
  551. *
  552. * Schedule error handling for @qc. EH will kick in as soon as
  553. * other commands are drained.
  554. *
  555. * LOCKING:
  556. * spin_lock_irqsave(host lock)
  557. */
  558. void ata_qc_schedule_eh(struct ata_queued_cmd *qc)
  559. {
  560. struct ata_port *ap = qc->ap;
  561. WARN_ON(!ap->ops->error_handler);
  562. qc->flags |= ATA_QCFLAG_FAILED;
  563. ata_eh_set_pending(ap, 1);
  564. /* The following will fail if timeout has already expired.
  565. * ata_scsi_error() takes care of such scmds on EH entry.
  566. * Note that ATA_QCFLAG_FAILED is unconditionally set after
  567. * this function completes.
  568. */
  569. scsi_req_abort_cmd(qc->scsicmd);
  570. }
  571. /**
  572. * ata_port_schedule_eh - schedule error handling without a qc
  573. * @ap: ATA port to schedule EH for
  574. *
  575. * Schedule error handling for @ap. EH will kick in as soon as
  576. * all commands are drained.
  577. *
  578. * LOCKING:
  579. * spin_lock_irqsave(host lock)
  580. */
  581. void ata_port_schedule_eh(struct ata_port *ap)
  582. {
  583. WARN_ON(!ap->ops->error_handler);
  584. if (ap->pflags & ATA_PFLAG_INITIALIZING)
  585. return;
  586. ata_eh_set_pending(ap, 1);
  587. scsi_schedule_eh(ap->scsi_host);
  588. DPRINTK("port EH scheduled\n");
  589. }
  590. static int ata_do_link_abort(struct ata_port *ap, struct ata_link *link)
  591. {
  592. int tag, nr_aborted = 0;
  593. WARN_ON(!ap->ops->error_handler);
  594. /* we're gonna abort all commands, no need for fast drain */
  595. ata_eh_set_pending(ap, 0);
  596. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  597. struct ata_queued_cmd *qc = ata_qc_from_tag(ap, tag);
  598. if (qc && (!link || qc->dev->link == link)) {
  599. qc->flags |= ATA_QCFLAG_FAILED;
  600. ata_qc_complete(qc);
  601. nr_aborted++;
  602. }
  603. }
  604. if (!nr_aborted)
  605. ata_port_schedule_eh(ap);
  606. return nr_aborted;
  607. }
  608. /**
  609. * ata_link_abort - abort all qc's on the link
  610. * @link: ATA link to abort qc's for
  611. *
  612. * Abort all active qc's active on @link and schedule EH.
  613. *
  614. * LOCKING:
  615. * spin_lock_irqsave(host lock)
  616. *
  617. * RETURNS:
  618. * Number of aborted qc's.
  619. */
  620. int ata_link_abort(struct ata_link *link)
  621. {
  622. return ata_do_link_abort(link->ap, link);
  623. }
  624. /**
  625. * ata_port_abort - abort all qc's on the port
  626. * @ap: ATA port to abort qc's for
  627. *
  628. * Abort all active qc's of @ap and schedule EH.
  629. *
  630. * LOCKING:
  631. * spin_lock_irqsave(host_set lock)
  632. *
  633. * RETURNS:
  634. * Number of aborted qc's.
  635. */
  636. int ata_port_abort(struct ata_port *ap)
  637. {
  638. return ata_do_link_abort(ap, NULL);
  639. }
  640. /**
  641. * __ata_port_freeze - freeze port
  642. * @ap: ATA port to freeze
  643. *
  644. * This function is called when HSM violation or some other
  645. * condition disrupts normal operation of the port. Frozen port
  646. * is not allowed to perform any operation until the port is
  647. * thawed, which usually follows a successful reset.
  648. *
  649. * ap->ops->freeze() callback can be used for freezing the port
  650. * hardware-wise (e.g. mask interrupt and stop DMA engine). If a
  651. * port cannot be frozen hardware-wise, the interrupt handler
  652. * must ack and clear interrupts unconditionally while the port
  653. * is frozen.
  654. *
  655. * LOCKING:
  656. * spin_lock_irqsave(host lock)
  657. */
  658. static void __ata_port_freeze(struct ata_port *ap)
  659. {
  660. WARN_ON(!ap->ops->error_handler);
  661. if (ap->ops->freeze)
  662. ap->ops->freeze(ap);
  663. ap->pflags |= ATA_PFLAG_FROZEN;
  664. DPRINTK("ata%u port frozen\n", ap->print_id);
  665. }
  666. /**
  667. * ata_port_freeze - abort & freeze port
  668. * @ap: ATA port to freeze
  669. *
  670. * Abort and freeze @ap.
  671. *
  672. * LOCKING:
  673. * spin_lock_irqsave(host lock)
  674. *
  675. * RETURNS:
  676. * Number of aborted commands.
  677. */
  678. int ata_port_freeze(struct ata_port *ap)
  679. {
  680. int nr_aborted;
  681. WARN_ON(!ap->ops->error_handler);
  682. nr_aborted = ata_port_abort(ap);
  683. __ata_port_freeze(ap);
  684. return nr_aborted;
  685. }
  686. /**
  687. * sata_async_notification - SATA async notification handler
  688. * @ap: ATA port where async notification is received
  689. *
  690. * Handler to be called when async notification via SDB FIS is
  691. * received. This function schedules EH if necessary.
  692. *
  693. * LOCKING:
  694. * spin_lock_irqsave(host lock)
  695. *
  696. * RETURNS:
  697. * 1 if EH is scheduled, 0 otherwise.
  698. */
  699. int sata_async_notification(struct ata_port *ap)
  700. {
  701. u32 sntf;
  702. int rc;
  703. if (!(ap->flags & ATA_FLAG_AN))
  704. return 0;
  705. rc = sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf);
  706. if (rc == 0)
  707. sata_scr_write(&ap->link, SCR_NOTIFICATION, sntf);
  708. if (!ap->nr_pmp_links || rc) {
  709. /* PMP is not attached or SNTF is not available */
  710. if (!ap->nr_pmp_links) {
  711. /* PMP is not attached. Check whether ATAPI
  712. * AN is configured. If so, notify media
  713. * change.
  714. */
  715. struct ata_device *dev = ap->link.device;
  716. if ((dev->class == ATA_DEV_ATAPI) &&
  717. (dev->flags & ATA_DFLAG_AN))
  718. ata_scsi_media_change_notify(dev);
  719. return 0;
  720. } else {
  721. /* PMP is attached but SNTF is not available.
  722. * ATAPI async media change notification is
  723. * not used. The PMP must be reporting PHY
  724. * status change, schedule EH.
  725. */
  726. ata_port_schedule_eh(ap);
  727. return 1;
  728. }
  729. } else {
  730. /* PMP is attached and SNTF is available */
  731. struct ata_link *link;
  732. /* check and notify ATAPI AN */
  733. ata_port_for_each_link(link, ap) {
  734. if (!(sntf & (1 << link->pmp)))
  735. continue;
  736. if ((link->device->class == ATA_DEV_ATAPI) &&
  737. (link->device->flags & ATA_DFLAG_AN))
  738. ata_scsi_media_change_notify(link->device);
  739. }
  740. /* If PMP is reporting that PHY status of some
  741. * downstream ports has changed, schedule EH.
  742. */
  743. if (sntf & (1 << SATA_PMP_CTRL_PORT)) {
  744. ata_port_schedule_eh(ap);
  745. return 1;
  746. }
  747. return 0;
  748. }
  749. }
  750. /**
  751. * ata_eh_freeze_port - EH helper to freeze port
  752. * @ap: ATA port to freeze
  753. *
  754. * Freeze @ap.
  755. *
  756. * LOCKING:
  757. * None.
  758. */
  759. void ata_eh_freeze_port(struct ata_port *ap)
  760. {
  761. unsigned long flags;
  762. if (!ap->ops->error_handler)
  763. return;
  764. spin_lock_irqsave(ap->lock, flags);
  765. __ata_port_freeze(ap);
  766. spin_unlock_irqrestore(ap->lock, flags);
  767. }
  768. /**
  769. * ata_port_thaw_port - EH helper to thaw port
  770. * @ap: ATA port to thaw
  771. *
  772. * Thaw frozen port @ap.
  773. *
  774. * LOCKING:
  775. * None.
  776. */
  777. void ata_eh_thaw_port(struct ata_port *ap)
  778. {
  779. unsigned long flags;
  780. if (!ap->ops->error_handler)
  781. return;
  782. spin_lock_irqsave(ap->lock, flags);
  783. ap->pflags &= ~ATA_PFLAG_FROZEN;
  784. if (ap->ops->thaw)
  785. ap->ops->thaw(ap);
  786. spin_unlock_irqrestore(ap->lock, flags);
  787. DPRINTK("ata%u port thawed\n", ap->print_id);
  788. }
  789. static void ata_eh_scsidone(struct scsi_cmnd *scmd)
  790. {
  791. /* nada */
  792. }
  793. static void __ata_eh_qc_complete(struct ata_queued_cmd *qc)
  794. {
  795. struct ata_port *ap = qc->ap;
  796. struct scsi_cmnd *scmd = qc->scsicmd;
  797. unsigned long flags;
  798. spin_lock_irqsave(ap->lock, flags);
  799. qc->scsidone = ata_eh_scsidone;
  800. __ata_qc_complete(qc);
  801. WARN_ON(ata_tag_valid(qc->tag));
  802. spin_unlock_irqrestore(ap->lock, flags);
  803. scsi_eh_finish_cmd(scmd, &ap->eh_done_q);
  804. }
  805. /**
  806. * ata_eh_qc_complete - Complete an active ATA command from EH
  807. * @qc: Command to complete
  808. *
  809. * Indicate to the mid and upper layers that an ATA command has
  810. * completed. To be used from EH.
  811. */
  812. void ata_eh_qc_complete(struct ata_queued_cmd *qc)
  813. {
  814. struct scsi_cmnd *scmd = qc->scsicmd;
  815. scmd->retries = scmd->allowed;
  816. __ata_eh_qc_complete(qc);
  817. }
  818. /**
  819. * ata_eh_qc_retry - Tell midlayer to retry an ATA command after EH
  820. * @qc: Command to retry
  821. *
  822. * Indicate to the mid and upper layers that an ATA command
  823. * should be retried. To be used from EH.
  824. *
  825. * SCSI midlayer limits the number of retries to scmd->allowed.
  826. * scmd->retries is decremented for commands which get retried
  827. * due to unrelated failures (qc->err_mask is zero).
  828. */
  829. void ata_eh_qc_retry(struct ata_queued_cmd *qc)
  830. {
  831. struct scsi_cmnd *scmd = qc->scsicmd;
  832. if (!qc->err_mask && scmd->retries)
  833. scmd->retries--;
  834. __ata_eh_qc_complete(qc);
  835. }
  836. /**
  837. * ata_eh_detach_dev - detach ATA device
  838. * @dev: ATA device to detach
  839. *
  840. * Detach @dev.
  841. *
  842. * LOCKING:
  843. * None.
  844. */
  845. void ata_eh_detach_dev(struct ata_device *dev)
  846. {
  847. struct ata_link *link = dev->link;
  848. struct ata_port *ap = link->ap;
  849. unsigned long flags;
  850. ata_dev_disable(dev);
  851. spin_lock_irqsave(ap->lock, flags);
  852. dev->flags &= ~ATA_DFLAG_DETACH;
  853. if (ata_scsi_offline_dev(dev)) {
  854. dev->flags |= ATA_DFLAG_DETACHED;
  855. ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
  856. }
  857. /* clear per-dev EH actions */
  858. ata_eh_clear_action(link, dev, &link->eh_info, ATA_EH_PERDEV_MASK);
  859. ata_eh_clear_action(link, dev, &link->eh_context.i, ATA_EH_PERDEV_MASK);
  860. spin_unlock_irqrestore(ap->lock, flags);
  861. }
  862. /**
  863. * ata_eh_about_to_do - about to perform eh_action
  864. * @link: target ATA link
  865. * @dev: target ATA dev for per-dev action (can be NULL)
  866. * @action: action about to be performed
  867. *
  868. * Called just before performing EH actions to clear related bits
  869. * in @link->eh_info such that eh actions are not unnecessarily
  870. * repeated.
  871. *
  872. * LOCKING:
  873. * None.
  874. */
  875. void ata_eh_about_to_do(struct ata_link *link, struct ata_device *dev,
  876. unsigned int action)
  877. {
  878. struct ata_port *ap = link->ap;
  879. struct ata_eh_info *ehi = &link->eh_info;
  880. struct ata_eh_context *ehc = &link->eh_context;
  881. unsigned long flags;
  882. spin_lock_irqsave(ap->lock, flags);
  883. /* Reset is represented by combination of actions and EHI
  884. * flags. Suck in all related bits before clearing eh_info to
  885. * avoid losing requested action.
  886. */
  887. if (action & ATA_EH_RESET_MASK) {
  888. ehc->i.action |= ehi->action & ATA_EH_RESET_MASK;
  889. ehc->i.flags |= ehi->flags & ATA_EHI_RESET_MODIFIER_MASK;
  890. /* make sure all reset actions are cleared & clear EHI flags */
  891. action |= ATA_EH_RESET_MASK;
  892. ehi->flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
  893. }
  894. ata_eh_clear_action(link, dev, ehi, action);
  895. if (!(ehc->i.flags & ATA_EHI_QUIET))
  896. ap->pflags |= ATA_PFLAG_RECOVERED;
  897. spin_unlock_irqrestore(ap->lock, flags);
  898. }
  899. /**
  900. * ata_eh_done - EH action complete
  901. * @ap: target ATA port
  902. * @dev: target ATA dev for per-dev action (can be NULL)
  903. * @action: action just completed
  904. *
  905. * Called right after performing EH actions to clear related bits
  906. * in @link->eh_context.
  907. *
  908. * LOCKING:
  909. * None.
  910. */
  911. void ata_eh_done(struct ata_link *link, struct ata_device *dev,
  912. unsigned int action)
  913. {
  914. struct ata_eh_context *ehc = &link->eh_context;
  915. /* if reset is complete, clear all reset actions & reset modifier */
  916. if (action & ATA_EH_RESET_MASK) {
  917. action |= ATA_EH_RESET_MASK;
  918. ehc->i.flags &= ~ATA_EHI_RESET_MODIFIER_MASK;
  919. }
  920. ata_eh_clear_action(link, dev, &ehc->i, action);
  921. }
  922. /**
  923. * ata_err_string - convert err_mask to descriptive string
  924. * @err_mask: error mask to convert to string
  925. *
  926. * Convert @err_mask to descriptive string. Errors are
  927. * prioritized according to severity and only the most severe
  928. * error is reported.
  929. *
  930. * LOCKING:
  931. * None.
  932. *
  933. * RETURNS:
  934. * Descriptive string for @err_mask
  935. */
  936. static const char *ata_err_string(unsigned int err_mask)
  937. {
  938. if (err_mask & AC_ERR_HOST_BUS)
  939. return "host bus error";
  940. if (err_mask & AC_ERR_ATA_BUS)
  941. return "ATA bus error";
  942. if (err_mask & AC_ERR_TIMEOUT)
  943. return "timeout";
  944. if (err_mask & AC_ERR_HSM)
  945. return "HSM violation";
  946. if (err_mask & AC_ERR_SYSTEM)
  947. return "internal error";
  948. if (err_mask & AC_ERR_MEDIA)
  949. return "media error";
  950. if (err_mask & AC_ERR_INVALID)
  951. return "invalid argument";
  952. if (err_mask & AC_ERR_DEV)
  953. return "device error";
  954. return "unknown error";
  955. }
  956. /**
  957. * ata_read_log_page - read a specific log page
  958. * @dev: target device
  959. * @page: page to read
  960. * @buf: buffer to store read page
  961. * @sectors: number of sectors to read
  962. *
  963. * Read log page using READ_LOG_EXT command.
  964. *
  965. * LOCKING:
  966. * Kernel thread context (may sleep).
  967. *
  968. * RETURNS:
  969. * 0 on success, AC_ERR_* mask otherwise.
  970. */
  971. static unsigned int ata_read_log_page(struct ata_device *dev,
  972. u8 page, void *buf, unsigned int sectors)
  973. {
  974. struct ata_taskfile tf;
  975. unsigned int err_mask;
  976. DPRINTK("read log page - page %d\n", page);
  977. ata_tf_init(dev, &tf);
  978. tf.command = ATA_CMD_READ_LOG_EXT;
  979. tf.lbal = page;
  980. tf.nsect = sectors;
  981. tf.hob_nsect = sectors >> 8;
  982. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
  983. tf.protocol = ATA_PROT_PIO;
  984. err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
  985. buf, sectors * ATA_SECT_SIZE, 0);
  986. DPRINTK("EXIT, err_mask=%x\n", err_mask);
  987. return err_mask;
  988. }
  989. /**
  990. * ata_eh_read_log_10h - Read log page 10h for NCQ error details
  991. * @dev: Device to read log page 10h from
  992. * @tag: Resulting tag of the failed command
  993. * @tf: Resulting taskfile registers of the failed command
  994. *
  995. * Read log page 10h to obtain NCQ error details and clear error
  996. * condition.
  997. *
  998. * LOCKING:
  999. * Kernel thread context (may sleep).
  1000. *
  1001. * RETURNS:
  1002. * 0 on success, -errno otherwise.
  1003. */
  1004. static int ata_eh_read_log_10h(struct ata_device *dev,
  1005. int *tag, struct ata_taskfile *tf)
  1006. {
  1007. u8 *buf = dev->link->ap->sector_buf;
  1008. unsigned int err_mask;
  1009. u8 csum;
  1010. int i;
  1011. err_mask = ata_read_log_page(dev, ATA_LOG_SATA_NCQ, buf, 1);
  1012. if (err_mask)
  1013. return -EIO;
  1014. csum = 0;
  1015. for (i = 0; i < ATA_SECT_SIZE; i++)
  1016. csum += buf[i];
  1017. if (csum)
  1018. ata_dev_printk(dev, KERN_WARNING,
  1019. "invalid checksum 0x%x on log page 10h\n", csum);
  1020. if (buf[0] & 0x80)
  1021. return -ENOENT;
  1022. *tag = buf[0] & 0x1f;
  1023. tf->command = buf[2];
  1024. tf->feature = buf[3];
  1025. tf->lbal = buf[4];
  1026. tf->lbam = buf[5];
  1027. tf->lbah = buf[6];
  1028. tf->device = buf[7];
  1029. tf->hob_lbal = buf[8];
  1030. tf->hob_lbam = buf[9];
  1031. tf->hob_lbah = buf[10];
  1032. tf->nsect = buf[12];
  1033. tf->hob_nsect = buf[13];
  1034. return 0;
  1035. }
  1036. /**
  1037. * atapi_eh_request_sense - perform ATAPI REQUEST_SENSE
  1038. * @dev: device to perform REQUEST_SENSE to
  1039. * @sense_buf: result sense data buffer (SCSI_SENSE_BUFFERSIZE bytes long)
  1040. *
  1041. * Perform ATAPI REQUEST_SENSE after the device reported CHECK
  1042. * SENSE. This function is EH helper.
  1043. *
  1044. * LOCKING:
  1045. * Kernel thread context (may sleep).
  1046. *
  1047. * RETURNS:
  1048. * 0 on success, AC_ERR_* mask on failure
  1049. */
  1050. static unsigned int atapi_eh_request_sense(struct ata_queued_cmd *qc)
  1051. {
  1052. struct ata_device *dev = qc->dev;
  1053. unsigned char *sense_buf = qc->scsicmd->sense_buffer;
  1054. struct ata_port *ap = dev->link->ap;
  1055. struct ata_taskfile tf;
  1056. u8 cdb[ATAPI_CDB_LEN];
  1057. DPRINTK("ATAPI request sense\n");
  1058. /* FIXME: is this needed? */
  1059. memset(sense_buf, 0, SCSI_SENSE_BUFFERSIZE);
  1060. /* initialize sense_buf with the error register,
  1061. * for the case where they are -not- overwritten
  1062. */
  1063. sense_buf[0] = 0x70;
  1064. sense_buf[2] = qc->result_tf.feature >> 4;
  1065. /* some devices time out if garbage left in tf */
  1066. ata_tf_init(dev, &tf);
  1067. memset(cdb, 0, ATAPI_CDB_LEN);
  1068. cdb[0] = REQUEST_SENSE;
  1069. cdb[4] = SCSI_SENSE_BUFFERSIZE;
  1070. tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
  1071. tf.command = ATA_CMD_PACKET;
  1072. /* is it pointless to prefer PIO for "safety reasons"? */
  1073. if (ap->flags & ATA_FLAG_PIO_DMA) {
  1074. tf.protocol = ATA_PROT_ATAPI_DMA;
  1075. tf.feature |= ATAPI_PKT_DMA;
  1076. } else {
  1077. tf.protocol = ATA_PROT_ATAPI;
  1078. tf.lbam = SCSI_SENSE_BUFFERSIZE;
  1079. tf.lbah = 0;
  1080. }
  1081. return ata_exec_internal(dev, &tf, cdb, DMA_FROM_DEVICE,
  1082. sense_buf, SCSI_SENSE_BUFFERSIZE, 0);
  1083. }
  1084. /**
  1085. * ata_eh_analyze_serror - analyze SError for a failed port
  1086. * @link: ATA link to analyze SError for
  1087. *
  1088. * Analyze SError if available and further determine cause of
  1089. * failure.
  1090. *
  1091. * LOCKING:
  1092. * None.
  1093. */
  1094. static void ata_eh_analyze_serror(struct ata_link *link)
  1095. {
  1096. struct ata_eh_context *ehc = &link->eh_context;
  1097. u32 serror = ehc->i.serror;
  1098. unsigned int err_mask = 0, action = 0;
  1099. u32 hotplug_mask;
  1100. if (serror & SERR_PERSISTENT) {
  1101. err_mask |= AC_ERR_ATA_BUS;
  1102. action |= ATA_EH_HARDRESET;
  1103. }
  1104. if (serror &
  1105. (SERR_DATA_RECOVERED | SERR_COMM_RECOVERED | SERR_DATA)) {
  1106. err_mask |= AC_ERR_ATA_BUS;
  1107. action |= ATA_EH_SOFTRESET;
  1108. }
  1109. if (serror & SERR_PROTOCOL) {
  1110. err_mask |= AC_ERR_HSM;
  1111. action |= ATA_EH_SOFTRESET;
  1112. }
  1113. if (serror & SERR_INTERNAL) {
  1114. err_mask |= AC_ERR_SYSTEM;
  1115. action |= ATA_EH_HARDRESET;
  1116. }
  1117. /* Determine whether a hotplug event has occurred. Both
  1118. * SError.N/X are considered hotplug events for enabled or
  1119. * host links. For disabled PMP links, only N bit is
  1120. * considered as X bit is left at 1 for link plugging.
  1121. */
  1122. hotplug_mask = 0;
  1123. if (!(link->flags & ATA_LFLAG_DISABLED) || ata_is_host_link(link))
  1124. hotplug_mask = SERR_PHYRDY_CHG | SERR_DEV_XCHG;
  1125. else
  1126. hotplug_mask = SERR_PHYRDY_CHG;
  1127. if (serror & hotplug_mask)
  1128. ata_ehi_hotplugged(&ehc->i);
  1129. ehc->i.err_mask |= err_mask;
  1130. ehc->i.action |= action;
  1131. }
  1132. /**
  1133. * ata_eh_analyze_ncq_error - analyze NCQ error
  1134. * @link: ATA link to analyze NCQ error for
  1135. *
  1136. * Read log page 10h, determine the offending qc and acquire
  1137. * error status TF. For NCQ device errors, all LLDDs have to do
  1138. * is setting AC_ERR_DEV in ehi->err_mask. This function takes
  1139. * care of the rest.
  1140. *
  1141. * LOCKING:
  1142. * Kernel thread context (may sleep).
  1143. */
  1144. static void ata_eh_analyze_ncq_error(struct ata_link *link)
  1145. {
  1146. struct ata_port *ap = link->ap;
  1147. struct ata_eh_context *ehc = &link->eh_context;
  1148. struct ata_device *dev = link->device;
  1149. struct ata_queued_cmd *qc;
  1150. struct ata_taskfile tf;
  1151. int tag, rc;
  1152. /* if frozen, we can't do much */
  1153. if (ap->pflags & ATA_PFLAG_FROZEN)
  1154. return;
  1155. /* is it NCQ device error? */
  1156. if (!link->sactive || !(ehc->i.err_mask & AC_ERR_DEV))
  1157. return;
  1158. /* has LLDD analyzed already? */
  1159. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  1160. qc = __ata_qc_from_tag(ap, tag);
  1161. if (!(qc->flags & ATA_QCFLAG_FAILED))
  1162. continue;
  1163. if (qc->err_mask)
  1164. return;
  1165. }
  1166. /* okay, this error is ours */
  1167. rc = ata_eh_read_log_10h(dev, &tag, &tf);
  1168. if (rc) {
  1169. ata_link_printk(link, KERN_ERR, "failed to read log page 10h "
  1170. "(errno=%d)\n", rc);
  1171. return;
  1172. }
  1173. if (!(link->sactive & (1 << tag))) {
  1174. ata_link_printk(link, KERN_ERR, "log page 10h reported "
  1175. "inactive tag %d\n", tag);
  1176. return;
  1177. }
  1178. /* we've got the perpetrator, condemn it */
  1179. qc = __ata_qc_from_tag(ap, tag);
  1180. memcpy(&qc->result_tf, &tf, sizeof(tf));
  1181. qc->err_mask |= AC_ERR_DEV | AC_ERR_NCQ;
  1182. ehc->i.err_mask &= ~AC_ERR_DEV;
  1183. }
  1184. /**
  1185. * ata_eh_analyze_tf - analyze taskfile of a failed qc
  1186. * @qc: qc to analyze
  1187. * @tf: Taskfile registers to analyze
  1188. *
  1189. * Analyze taskfile of @qc and further determine cause of
  1190. * failure. This function also requests ATAPI sense data if
  1191. * avaliable.
  1192. *
  1193. * LOCKING:
  1194. * Kernel thread context (may sleep).
  1195. *
  1196. * RETURNS:
  1197. * Determined recovery action
  1198. */
  1199. static unsigned int ata_eh_analyze_tf(struct ata_queued_cmd *qc,
  1200. const struct ata_taskfile *tf)
  1201. {
  1202. unsigned int tmp, action = 0;
  1203. u8 stat = tf->command, err = tf->feature;
  1204. if ((stat & (ATA_BUSY | ATA_DRQ | ATA_DRDY)) != ATA_DRDY) {
  1205. qc->err_mask |= AC_ERR_HSM;
  1206. return ATA_EH_SOFTRESET;
  1207. }
  1208. if (stat & (ATA_ERR | ATA_DF))
  1209. qc->err_mask |= AC_ERR_DEV;
  1210. else
  1211. return 0;
  1212. switch (qc->dev->class) {
  1213. case ATA_DEV_ATA:
  1214. if (err & ATA_ICRC)
  1215. qc->err_mask |= AC_ERR_ATA_BUS;
  1216. if (err & ATA_UNC)
  1217. qc->err_mask |= AC_ERR_MEDIA;
  1218. if (err & ATA_IDNF)
  1219. qc->err_mask |= AC_ERR_INVALID;
  1220. break;
  1221. case ATA_DEV_ATAPI:
  1222. if (!(qc->ap->pflags & ATA_PFLAG_FROZEN)) {
  1223. tmp = atapi_eh_request_sense(qc);
  1224. if (!tmp) {
  1225. /* ATA_QCFLAG_SENSE_VALID is used to
  1226. * tell atapi_qc_complete() that sense
  1227. * data is already valid.
  1228. *
  1229. * TODO: interpret sense data and set
  1230. * appropriate err_mask.
  1231. */
  1232. qc->flags |= ATA_QCFLAG_SENSE_VALID;
  1233. } else
  1234. qc->err_mask |= tmp;
  1235. }
  1236. }
  1237. if (qc->err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT | AC_ERR_ATA_BUS))
  1238. action |= ATA_EH_SOFTRESET;
  1239. return action;
  1240. }
  1241. static int ata_eh_categorize_error(int is_io, unsigned int err_mask)
  1242. {
  1243. if (err_mask & AC_ERR_ATA_BUS)
  1244. return 1;
  1245. if (err_mask & AC_ERR_TIMEOUT)
  1246. return 2;
  1247. if (is_io) {
  1248. if (err_mask & AC_ERR_HSM)
  1249. return 2;
  1250. if ((err_mask &
  1251. (AC_ERR_DEV|AC_ERR_MEDIA|AC_ERR_INVALID)) == AC_ERR_DEV)
  1252. return 3;
  1253. }
  1254. return 0;
  1255. }
  1256. struct speed_down_verdict_arg {
  1257. u64 since;
  1258. int nr_errors[4];
  1259. };
  1260. static int speed_down_verdict_cb(struct ata_ering_entry *ent, void *void_arg)
  1261. {
  1262. struct speed_down_verdict_arg *arg = void_arg;
  1263. int cat = ata_eh_categorize_error(ent->is_io, ent->err_mask);
  1264. if (ent->timestamp < arg->since)
  1265. return -1;
  1266. arg->nr_errors[cat]++;
  1267. return 0;
  1268. }
  1269. /**
  1270. * ata_eh_speed_down_verdict - Determine speed down verdict
  1271. * @dev: Device of interest
  1272. *
  1273. * This function examines error ring of @dev and determines
  1274. * whether NCQ needs to be turned off, transfer speed should be
  1275. * stepped down, or falling back to PIO is necessary.
  1276. *
  1277. * Cat-1 is ATA_BUS error for any command.
  1278. *
  1279. * Cat-2 is TIMEOUT for any command or HSM violation for known
  1280. * supported commands.
  1281. *
  1282. * Cat-3 is is unclassified DEV error for known supported
  1283. * command.
  1284. *
  1285. * NCQ needs to be turned off if there have been more than 3
  1286. * Cat-2 + Cat-3 errors during last 10 minutes.
  1287. *
  1288. * Speed down is necessary if there have been more than 3 Cat-1 +
  1289. * Cat-2 errors or 10 Cat-3 errors during last 10 minutes.
  1290. *
  1291. * Falling back to PIO mode is necessary if there have been more
  1292. * than 10 Cat-1 + Cat-2 + Cat-3 errors during last 5 minutes.
  1293. *
  1294. * LOCKING:
  1295. * Inherited from caller.
  1296. *
  1297. * RETURNS:
  1298. * OR of ATA_EH_SPDN_* flags.
  1299. */
  1300. static unsigned int ata_eh_speed_down_verdict(struct ata_device *dev)
  1301. {
  1302. const u64 j5mins = 5LLU * 60 * HZ, j10mins = 10LLU * 60 * HZ;
  1303. u64 j64 = get_jiffies_64();
  1304. struct speed_down_verdict_arg arg;
  1305. unsigned int verdict = 0;
  1306. /* scan past 10 mins of error history */
  1307. memset(&arg, 0, sizeof(arg));
  1308. arg.since = j64 - min(j64, j10mins);
  1309. ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
  1310. if (arg.nr_errors[2] + arg.nr_errors[3] > 3)
  1311. verdict |= ATA_EH_SPDN_NCQ_OFF;
  1312. if (arg.nr_errors[1] + arg.nr_errors[2] > 3 || arg.nr_errors[3] > 10)
  1313. verdict |= ATA_EH_SPDN_SPEED_DOWN;
  1314. /* scan past 3 mins of error history */
  1315. memset(&arg, 0, sizeof(arg));
  1316. arg.since = j64 - min(j64, j5mins);
  1317. ata_ering_map(&dev->ering, speed_down_verdict_cb, &arg);
  1318. if (arg.nr_errors[1] + arg.nr_errors[2] + arg.nr_errors[3] > 10)
  1319. verdict |= ATA_EH_SPDN_FALLBACK_TO_PIO;
  1320. return verdict;
  1321. }
  1322. /**
  1323. * ata_eh_speed_down - record error and speed down if necessary
  1324. * @dev: Failed device
  1325. * @is_io: Did the device fail during normal IO?
  1326. * @err_mask: err_mask of the error
  1327. *
  1328. * Record error and examine error history to determine whether
  1329. * adjusting transmission speed is necessary. It also sets
  1330. * transmission limits appropriately if such adjustment is
  1331. * necessary.
  1332. *
  1333. * LOCKING:
  1334. * Kernel thread context (may sleep).
  1335. *
  1336. * RETURNS:
  1337. * Determined recovery action.
  1338. */
  1339. static unsigned int ata_eh_speed_down(struct ata_device *dev, int is_io,
  1340. unsigned int err_mask)
  1341. {
  1342. unsigned int verdict;
  1343. unsigned int action = 0;
  1344. /* don't bother if Cat-0 error */
  1345. if (ata_eh_categorize_error(is_io, err_mask) == 0)
  1346. return 0;
  1347. /* record error and determine whether speed down is necessary */
  1348. ata_ering_record(&dev->ering, is_io, err_mask);
  1349. verdict = ata_eh_speed_down_verdict(dev);
  1350. /* turn off NCQ? */
  1351. if ((verdict & ATA_EH_SPDN_NCQ_OFF) &&
  1352. (dev->flags & (ATA_DFLAG_PIO | ATA_DFLAG_NCQ |
  1353. ATA_DFLAG_NCQ_OFF)) == ATA_DFLAG_NCQ) {
  1354. dev->flags |= ATA_DFLAG_NCQ_OFF;
  1355. ata_dev_printk(dev, KERN_WARNING,
  1356. "NCQ disabled due to excessive errors\n");
  1357. goto done;
  1358. }
  1359. /* speed down? */
  1360. if (verdict & ATA_EH_SPDN_SPEED_DOWN) {
  1361. /* speed down SATA link speed if possible */
  1362. if (sata_down_spd_limit(dev->link) == 0) {
  1363. action |= ATA_EH_HARDRESET;
  1364. goto done;
  1365. }
  1366. /* lower transfer mode */
  1367. if (dev->spdn_cnt < 2) {
  1368. static const int dma_dnxfer_sel[] =
  1369. { ATA_DNXFER_DMA, ATA_DNXFER_40C };
  1370. static const int pio_dnxfer_sel[] =
  1371. { ATA_DNXFER_PIO, ATA_DNXFER_FORCE_PIO0 };
  1372. int sel;
  1373. if (dev->xfer_shift != ATA_SHIFT_PIO)
  1374. sel = dma_dnxfer_sel[dev->spdn_cnt];
  1375. else
  1376. sel = pio_dnxfer_sel[dev->spdn_cnt];
  1377. dev->spdn_cnt++;
  1378. if (ata_down_xfermask_limit(dev, sel) == 0) {
  1379. action |= ATA_EH_SOFTRESET;
  1380. goto done;
  1381. }
  1382. }
  1383. }
  1384. /* Fall back to PIO? Slowing down to PIO is meaningless for
  1385. * SATA. Consider it only for PATA.
  1386. */
  1387. if ((verdict & ATA_EH_SPDN_FALLBACK_TO_PIO) && (dev->spdn_cnt >= 2) &&
  1388. (dev->link->ap->cbl != ATA_CBL_SATA) &&
  1389. (dev->xfer_shift != ATA_SHIFT_PIO)) {
  1390. if (ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO) == 0) {
  1391. dev->spdn_cnt = 0;
  1392. action |= ATA_EH_SOFTRESET;
  1393. goto done;
  1394. }
  1395. }
  1396. return 0;
  1397. done:
  1398. /* device has been slowed down, blow error history */
  1399. ata_ering_clear(&dev->ering);
  1400. return action;
  1401. }
  1402. /**
  1403. * ata_eh_link_autopsy - analyze error and determine recovery action
  1404. * @link: host link to perform autopsy on
  1405. *
  1406. * Analyze why @link failed and determine which recovery actions
  1407. * are needed. This function also sets more detailed AC_ERR_*
  1408. * values and fills sense data for ATAPI CHECK SENSE.
  1409. *
  1410. * LOCKING:
  1411. * Kernel thread context (may sleep).
  1412. */
  1413. static void ata_eh_link_autopsy(struct ata_link *link)
  1414. {
  1415. struct ata_port *ap = link->ap;
  1416. struct ata_eh_context *ehc = &link->eh_context;
  1417. struct ata_device *dev;
  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. /* accumulate error info */
  1461. ehc->i.dev = qc->dev;
  1462. all_err_mask |= qc->err_mask;
  1463. if (qc->flags & ATA_QCFLAG_IO)
  1464. is_io = 1;
  1465. }
  1466. /* enforce default EH actions */
  1467. if (ap->pflags & ATA_PFLAG_FROZEN ||
  1468. all_err_mask & (AC_ERR_HSM | AC_ERR_TIMEOUT))
  1469. ehc->i.action |= ATA_EH_SOFTRESET;
  1470. else if ((is_io && all_err_mask) ||
  1471. (!is_io && (all_err_mask & ~AC_ERR_DEV)))
  1472. ehc->i.action |= ATA_EH_REVALIDATE;
  1473. /* If we have offending qcs and the associated failed device,
  1474. * perform per-dev EH action only on the offending device.
  1475. */
  1476. if (ehc->i.dev) {
  1477. ehc->i.dev_action[ehc->i.dev->devno] |=
  1478. ehc->i.action & ATA_EH_PERDEV_MASK;
  1479. ehc->i.action &= ~ATA_EH_PERDEV_MASK;
  1480. }
  1481. /* consider speeding down */
  1482. dev = ehc->i.dev;
  1483. if (!dev && ata_link_max_devices(link) == 1 &&
  1484. ata_dev_enabled(link->device))
  1485. dev = link->device;
  1486. if (dev)
  1487. ehc->i.action |= ata_eh_speed_down(dev, is_io, all_err_mask);
  1488. DPRINTK("EXIT\n");
  1489. }
  1490. /**
  1491. * ata_eh_autopsy - analyze error and determine recovery action
  1492. * @ap: host port to perform autopsy on
  1493. *
  1494. * Analyze all links of @ap and determine why they failed and
  1495. * which recovery actions are needed.
  1496. *
  1497. * LOCKING:
  1498. * Kernel thread context (may sleep).
  1499. */
  1500. void ata_eh_autopsy(struct ata_port *ap)
  1501. {
  1502. struct ata_link *link;
  1503. __ata_port_for_each_link(link, ap)
  1504. ata_eh_link_autopsy(link);
  1505. }
  1506. /**
  1507. * ata_eh_link_report - report error handling to user
  1508. * @link: ATA link EH is going on
  1509. *
  1510. * Report EH to user.
  1511. *
  1512. * LOCKING:
  1513. * None.
  1514. */
  1515. static void ata_eh_link_report(struct ata_link *link)
  1516. {
  1517. struct ata_port *ap = link->ap;
  1518. struct ata_eh_context *ehc = &link->eh_context;
  1519. const char *frozen, *desc;
  1520. char tries_buf[6];
  1521. int tag, nr_failed = 0;
  1522. if (ehc->i.flags & ATA_EHI_QUIET)
  1523. return;
  1524. desc = NULL;
  1525. if (ehc->i.desc[0] != '\0')
  1526. desc = ehc->i.desc;
  1527. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  1528. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  1529. if (!(qc->flags & ATA_QCFLAG_FAILED) || qc->dev->link != link ||
  1530. ((qc->flags & ATA_QCFLAG_QUIET) &&
  1531. qc->err_mask == AC_ERR_DEV))
  1532. continue;
  1533. if (qc->flags & ATA_QCFLAG_SENSE_VALID && !qc->err_mask)
  1534. continue;
  1535. nr_failed++;
  1536. }
  1537. if (!nr_failed && !ehc->i.err_mask)
  1538. return;
  1539. frozen = "";
  1540. if (ap->pflags & ATA_PFLAG_FROZEN)
  1541. frozen = " frozen";
  1542. memset(tries_buf, 0, sizeof(tries_buf));
  1543. if (ap->eh_tries < ATA_EH_MAX_TRIES)
  1544. snprintf(tries_buf, sizeof(tries_buf) - 1, " t%d",
  1545. ap->eh_tries);
  1546. if (ehc->i.dev) {
  1547. ata_dev_printk(ehc->i.dev, KERN_ERR, "exception Emask 0x%x "
  1548. "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
  1549. ehc->i.err_mask, link->sactive, ehc->i.serror,
  1550. ehc->i.action, frozen, tries_buf);
  1551. if (desc)
  1552. ata_dev_printk(ehc->i.dev, KERN_ERR, "%s\n", desc);
  1553. } else {
  1554. ata_link_printk(link, KERN_ERR, "exception Emask 0x%x "
  1555. "SAct 0x%x SErr 0x%x action 0x%x%s%s\n",
  1556. ehc->i.err_mask, link->sactive, ehc->i.serror,
  1557. ehc->i.action, frozen, tries_buf);
  1558. if (desc)
  1559. ata_link_printk(link, KERN_ERR, "%s\n", desc);
  1560. }
  1561. if (ehc->i.serror)
  1562. ata_port_printk(ap, KERN_ERR,
  1563. "SError: { %s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s%s}\n",
  1564. ehc->i.serror & SERR_DATA_RECOVERED ? "RecovData " : "",
  1565. ehc->i.serror & SERR_COMM_RECOVERED ? "RecovComm " : "",
  1566. ehc->i.serror & SERR_DATA ? "UnrecovData " : "",
  1567. ehc->i.serror & SERR_PERSISTENT ? "Persist " : "",
  1568. ehc->i.serror & SERR_PROTOCOL ? "Proto " : "",
  1569. ehc->i.serror & SERR_INTERNAL ? "HostInt " : "",
  1570. ehc->i.serror & SERR_PHYRDY_CHG ? "PHYRdyChg " : "",
  1571. ehc->i.serror & SERR_PHY_INT_ERR ? "PHYInt " : "",
  1572. ehc->i.serror & SERR_COMM_WAKE ? "CommWake " : "",
  1573. ehc->i.serror & SERR_10B_8B_ERR ? "10B8B " : "",
  1574. ehc->i.serror & SERR_DISPARITY ? "Dispar " : "",
  1575. ehc->i.serror & SERR_CRC ? "BadCRC " : "",
  1576. ehc->i.serror & SERR_HANDSHAKE ? "Handshk " : "",
  1577. ehc->i.serror & SERR_LINK_SEQ_ERR ? "LinkSeq " : "",
  1578. ehc->i.serror & SERR_TRANS_ST_ERROR ? "TrStaTrns " : "",
  1579. ehc->i.serror & SERR_UNRECOG_FIS ? "UnrecFIS " : "",
  1580. ehc->i.serror & SERR_DEV_XCHG ? "DevExch " : "");
  1581. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  1582. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  1583. struct ata_taskfile *cmd = &qc->tf, *res = &qc->result_tf;
  1584. const u8 *cdb = qc->cdb;
  1585. char data_buf[20] = "";
  1586. char cdb_buf[70] = "";
  1587. if (!(qc->flags & ATA_QCFLAG_FAILED) ||
  1588. qc->dev->link != link || !qc->err_mask)
  1589. continue;
  1590. if (qc->dma_dir != DMA_NONE) {
  1591. static const char *dma_str[] = {
  1592. [DMA_BIDIRECTIONAL] = "bidi",
  1593. [DMA_TO_DEVICE] = "out",
  1594. [DMA_FROM_DEVICE] = "in",
  1595. };
  1596. static const char *prot_str[] = {
  1597. [ATA_PROT_PIO] = "pio",
  1598. [ATA_PROT_DMA] = "dma",
  1599. [ATA_PROT_NCQ] = "ncq",
  1600. [ATA_PROT_ATAPI] = "pio",
  1601. [ATA_PROT_ATAPI_DMA] = "dma",
  1602. };
  1603. snprintf(data_buf, sizeof(data_buf), " %s %u %s",
  1604. prot_str[qc->tf.protocol], qc->nbytes,
  1605. dma_str[qc->dma_dir]);
  1606. }
  1607. if (is_atapi_taskfile(&qc->tf))
  1608. snprintf(cdb_buf, sizeof(cdb_buf),
  1609. "cdb %02x %02x %02x %02x %02x %02x %02x %02x "
  1610. "%02x %02x %02x %02x %02x %02x %02x %02x\n ",
  1611. cdb[0], cdb[1], cdb[2], cdb[3],
  1612. cdb[4], cdb[5], cdb[6], cdb[7],
  1613. cdb[8], cdb[9], cdb[10], cdb[11],
  1614. cdb[12], cdb[13], cdb[14], cdb[15]);
  1615. ata_dev_printk(qc->dev, KERN_ERR,
  1616. "cmd %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
  1617. "tag %d%s\n %s"
  1618. "res %02x/%02x:%02x:%02x:%02x:%02x/%02x:%02x:%02x:%02x:%02x/%02x "
  1619. "Emask 0x%x (%s)%s\n",
  1620. cmd->command, cmd->feature, cmd->nsect,
  1621. cmd->lbal, cmd->lbam, cmd->lbah,
  1622. cmd->hob_feature, cmd->hob_nsect,
  1623. cmd->hob_lbal, cmd->hob_lbam, cmd->hob_lbah,
  1624. cmd->device, qc->tag, data_buf, cdb_buf,
  1625. res->command, res->feature, res->nsect,
  1626. res->lbal, res->lbam, res->lbah,
  1627. res->hob_feature, res->hob_nsect,
  1628. res->hob_lbal, res->hob_lbam, res->hob_lbah,
  1629. res->device, qc->err_mask, ata_err_string(qc->err_mask),
  1630. qc->err_mask & AC_ERR_NCQ ? " <F>" : "");
  1631. if (res->command & (ATA_BUSY | ATA_DRDY | ATA_DF | ATA_DRQ |
  1632. ATA_ERR)) {
  1633. if (res->command & ATA_BUSY)
  1634. ata_dev_printk(qc->dev, KERN_ERR,
  1635. "status: { Busy }\n");
  1636. else
  1637. ata_dev_printk(qc->dev, KERN_ERR,
  1638. "status: { %s%s%s%s}\n",
  1639. res->command & ATA_DRDY ? "DRDY " : "",
  1640. res->command & ATA_DF ? "DF " : "",
  1641. res->command & ATA_DRQ ? "DRQ " : "",
  1642. res->command & ATA_ERR ? "ERR " : "");
  1643. }
  1644. if (cmd->command != ATA_CMD_PACKET &&
  1645. (res->feature & (ATA_ICRC | ATA_UNC | ATA_IDNF |
  1646. ATA_ABORTED)))
  1647. ata_dev_printk(qc->dev, KERN_ERR,
  1648. "error: { %s%s%s%s}\n",
  1649. res->feature & ATA_ICRC ? "ICRC " : "",
  1650. res->feature & ATA_UNC ? "UNC " : "",
  1651. res->feature & ATA_IDNF ? "IDNF " : "",
  1652. res->feature & ATA_ABORTED ? "ABRT " : "");
  1653. }
  1654. }
  1655. /**
  1656. * ata_eh_report - report error handling to user
  1657. * @ap: ATA port to report EH about
  1658. *
  1659. * Report EH to user.
  1660. *
  1661. * LOCKING:
  1662. * None.
  1663. */
  1664. void ata_eh_report(struct ata_port *ap)
  1665. {
  1666. struct ata_link *link;
  1667. __ata_port_for_each_link(link, ap)
  1668. ata_eh_link_report(link);
  1669. }
  1670. static int ata_do_reset(struct ata_link *link, ata_reset_fn_t reset,
  1671. unsigned int *classes, unsigned long deadline)
  1672. {
  1673. struct ata_device *dev;
  1674. int rc;
  1675. ata_link_for_each_dev(dev, link)
  1676. classes[dev->devno] = ATA_DEV_UNKNOWN;
  1677. rc = reset(link, classes, deadline);
  1678. if (rc)
  1679. return rc;
  1680. /* If any class isn't ATA_DEV_UNKNOWN, consider classification
  1681. * is complete and convert all ATA_DEV_UNKNOWN to
  1682. * ATA_DEV_NONE.
  1683. */
  1684. ata_link_for_each_dev(dev, link)
  1685. if (classes[dev->devno] != ATA_DEV_UNKNOWN)
  1686. break;
  1687. if (dev) {
  1688. ata_link_for_each_dev(dev, link) {
  1689. if (classes[dev->devno] == ATA_DEV_UNKNOWN)
  1690. classes[dev->devno] = ATA_DEV_NONE;
  1691. }
  1692. }
  1693. return 0;
  1694. }
  1695. static int ata_eh_followup_srst_needed(struct ata_link *link,
  1696. int rc, int classify,
  1697. const unsigned int *classes)
  1698. {
  1699. if (link->flags & ATA_LFLAG_NO_SRST)
  1700. return 0;
  1701. if (rc == -EAGAIN)
  1702. return 1;
  1703. if (rc != 0)
  1704. return 0;
  1705. if ((link->ap->flags & ATA_FLAG_PMP) && ata_is_host_link(link))
  1706. return 1;
  1707. if (classify && !(link->flags & ATA_LFLAG_ASSUME_CLASS) &&
  1708. classes[0] == ATA_DEV_UNKNOWN)
  1709. return 1;
  1710. return 0;
  1711. }
  1712. int ata_eh_reset(struct ata_link *link, int classify,
  1713. ata_prereset_fn_t prereset, ata_reset_fn_t softreset,
  1714. ata_reset_fn_t hardreset, ata_postreset_fn_t postreset)
  1715. {
  1716. const int max_tries = ARRAY_SIZE(ata_eh_reset_timeouts);
  1717. struct ata_port *ap = link->ap;
  1718. struct ata_eh_context *ehc = &link->eh_context;
  1719. unsigned int *classes = ehc->classes;
  1720. unsigned int lflags = link->flags;
  1721. int verbose = !(ehc->i.flags & ATA_EHI_QUIET);
  1722. int try = 0;
  1723. struct ata_device *dev;
  1724. unsigned long deadline, now;
  1725. unsigned int tmp_action;
  1726. ata_reset_fn_t reset;
  1727. unsigned long flags;
  1728. u32 sstatus;
  1729. int rc;
  1730. /* about to reset */
  1731. spin_lock_irqsave(ap->lock, flags);
  1732. ap->pflags |= ATA_PFLAG_RESETTING;
  1733. spin_unlock_irqrestore(ap->lock, flags);
  1734. ata_eh_about_to_do(link, NULL, ehc->i.action & ATA_EH_RESET_MASK);
  1735. ata_link_for_each_dev(dev, link) {
  1736. /* If we issue an SRST then an ATA drive (not ATAPI)
  1737. * may change configuration and be in PIO0 timing. If
  1738. * we do a hard reset (or are coming from power on)
  1739. * this is true for ATA or ATAPI. Until we've set a
  1740. * suitable controller mode we should not touch the
  1741. * bus as we may be talking too fast.
  1742. */
  1743. dev->pio_mode = XFER_PIO_0;
  1744. /* If the controller has a pio mode setup function
  1745. * then use it to set the chipset to rights. Don't
  1746. * touch the DMA setup as that will be dealt with when
  1747. * configuring devices.
  1748. */
  1749. if (ap->ops->set_piomode)
  1750. ap->ops->set_piomode(ap, dev);
  1751. }
  1752. /* Determine which reset to use and record in ehc->i.action.
  1753. * prereset() may examine and modify it.
  1754. */
  1755. if (softreset && (!hardreset || (!(lflags & ATA_LFLAG_NO_SRST) &&
  1756. !sata_set_spd_needed(link) &&
  1757. !(ehc->i.action & ATA_EH_HARDRESET))))
  1758. tmp_action = ATA_EH_SOFTRESET;
  1759. else
  1760. tmp_action = ATA_EH_HARDRESET;
  1761. ehc->i.action = (ehc->i.action & ~ATA_EH_RESET_MASK) | tmp_action;
  1762. if (prereset) {
  1763. rc = prereset(link, jiffies + ATA_EH_PRERESET_TIMEOUT);
  1764. if (rc) {
  1765. if (rc == -ENOENT) {
  1766. ata_link_printk(link, KERN_DEBUG,
  1767. "port disabled. ignoring.\n");
  1768. ehc->i.action &= ~ATA_EH_RESET_MASK;
  1769. ata_link_for_each_dev(dev, link)
  1770. classes[dev->devno] = ATA_DEV_NONE;
  1771. rc = 0;
  1772. } else
  1773. ata_link_printk(link, KERN_ERR,
  1774. "prereset failed (errno=%d)\n", rc);
  1775. goto out;
  1776. }
  1777. }
  1778. /* prereset() might have modified ehc->i.action */
  1779. if (ehc->i.action & ATA_EH_HARDRESET)
  1780. reset = hardreset;
  1781. else if (ehc->i.action & ATA_EH_SOFTRESET)
  1782. reset = softreset;
  1783. else {
  1784. /* prereset told us not to reset, bang classes and return */
  1785. ata_link_for_each_dev(dev, link)
  1786. classes[dev->devno] = ATA_DEV_NONE;
  1787. rc = 0;
  1788. goto out;
  1789. }
  1790. /* did prereset() screw up? if so, fix up to avoid oopsing */
  1791. if (!reset) {
  1792. if (softreset)
  1793. reset = softreset;
  1794. else
  1795. reset = hardreset;
  1796. }
  1797. retry:
  1798. deadline = jiffies + ata_eh_reset_timeouts[try++];
  1799. /* shut up during boot probing */
  1800. if (verbose)
  1801. ata_link_printk(link, KERN_INFO, "%s resetting link\n",
  1802. reset == softreset ? "soft" : "hard");
  1803. /* mark that this EH session started with reset */
  1804. if (reset == hardreset)
  1805. ehc->i.flags |= ATA_EHI_DID_HARDRESET;
  1806. else
  1807. ehc->i.flags |= ATA_EHI_DID_SOFTRESET;
  1808. rc = ata_do_reset(link, reset, classes, deadline);
  1809. if (reset == hardreset &&
  1810. ata_eh_followup_srst_needed(link, rc, classify, classes)) {
  1811. /* okay, let's do follow-up softreset */
  1812. reset = softreset;
  1813. if (!reset) {
  1814. ata_link_printk(link, KERN_ERR,
  1815. "follow-up softreset required "
  1816. "but no softreset avaliable\n");
  1817. rc = -EINVAL;
  1818. goto fail;
  1819. }
  1820. ata_eh_about_to_do(link, NULL, ATA_EH_RESET_MASK);
  1821. rc = ata_do_reset(link, reset, classes, deadline);
  1822. }
  1823. /* -EAGAIN can happen if we skipped followup SRST */
  1824. if (rc && rc != -EAGAIN)
  1825. goto fail;
  1826. /* was classification successful? */
  1827. if (classify && classes[0] == ATA_DEV_UNKNOWN &&
  1828. !(lflags & ATA_LFLAG_ASSUME_CLASS)) {
  1829. if (try < max_tries) {
  1830. ata_link_printk(link, KERN_WARNING,
  1831. "classification failed\n");
  1832. rc = -EINVAL;
  1833. goto fail;
  1834. }
  1835. ata_link_printk(link, KERN_WARNING,
  1836. "classfication failed, assuming ATA\n");
  1837. lflags |= ATA_LFLAG_ASSUME_ATA;
  1838. }
  1839. ata_link_for_each_dev(dev, link) {
  1840. /* After the reset, the device state is PIO 0 and the
  1841. * controller state is undefined. Reset also wakes up
  1842. * drives from sleeping mode.
  1843. */
  1844. dev->pio_mode = XFER_PIO_0;
  1845. dev->flags &= ~ATA_DFLAG_SLEEPING;
  1846. if (ata_link_offline(link))
  1847. continue;
  1848. /* apply class override and convert UNKNOWN to NONE */
  1849. if (lflags & ATA_LFLAG_ASSUME_ATA)
  1850. classes[dev->devno] = ATA_DEV_ATA;
  1851. else if (lflags & ATA_LFLAG_ASSUME_SEMB)
  1852. classes[dev->devno] = ATA_DEV_SEMB_UNSUP; /* not yet */
  1853. else if (classes[dev->devno] == ATA_DEV_UNKNOWN)
  1854. classes[dev->devno] = ATA_DEV_NONE;
  1855. }
  1856. /* record current link speed */
  1857. if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0)
  1858. link->sata_spd = (sstatus >> 4) & 0xf;
  1859. if (postreset)
  1860. postreset(link, classes);
  1861. /* reset successful, schedule revalidation */
  1862. ata_eh_done(link, NULL, ehc->i.action & ATA_EH_RESET_MASK);
  1863. ehc->i.action |= ATA_EH_REVALIDATE;
  1864. rc = 0;
  1865. out:
  1866. /* clear hotplug flag */
  1867. ehc->i.flags &= ~ATA_EHI_HOTPLUGGED;
  1868. spin_lock_irqsave(ap->lock, flags);
  1869. ap->pflags &= ~ATA_PFLAG_RESETTING;
  1870. spin_unlock_irqrestore(ap->lock, flags);
  1871. return rc;
  1872. fail:
  1873. if (rc == -ERESTART || try >= max_tries)
  1874. goto out;
  1875. now = jiffies;
  1876. if (time_before(now, deadline)) {
  1877. unsigned long delta = deadline - now;
  1878. ata_link_printk(link, KERN_WARNING, "reset failed "
  1879. "(errno=%d), retrying in %u secs\n",
  1880. rc, (jiffies_to_msecs(delta) + 999) / 1000);
  1881. while (delta)
  1882. delta = schedule_timeout_uninterruptible(delta);
  1883. }
  1884. if (rc == -EPIPE || try == max_tries - 1)
  1885. sata_down_spd_limit(link);
  1886. if (hardreset)
  1887. reset = hardreset;
  1888. goto retry;
  1889. }
  1890. static int ata_eh_revalidate_and_attach(struct ata_link *link,
  1891. struct ata_device **r_failed_dev)
  1892. {
  1893. struct ata_port *ap = link->ap;
  1894. struct ata_eh_context *ehc = &link->eh_context;
  1895. struct ata_device *dev;
  1896. unsigned int new_mask = 0;
  1897. unsigned long flags;
  1898. int rc = 0;
  1899. DPRINTK("ENTER\n");
  1900. /* For PATA drive side cable detection to work, IDENTIFY must
  1901. * be done backwards such that PDIAG- is released by the slave
  1902. * device before the master device is identified.
  1903. */
  1904. ata_link_for_each_dev_reverse(dev, link) {
  1905. unsigned int action = ata_eh_dev_action(dev);
  1906. unsigned int readid_flags = 0;
  1907. if (ehc->i.flags & ATA_EHI_DID_RESET)
  1908. readid_flags |= ATA_READID_POSTRESET;
  1909. if ((action & ATA_EH_REVALIDATE) && ata_dev_enabled(dev)) {
  1910. WARN_ON(dev->class == ATA_DEV_PMP);
  1911. if (ata_link_offline(link)) {
  1912. rc = -EIO;
  1913. goto err;
  1914. }
  1915. ata_eh_about_to_do(link, dev, ATA_EH_REVALIDATE);
  1916. rc = ata_dev_revalidate(dev, ehc->classes[dev->devno],
  1917. readid_flags);
  1918. if (rc)
  1919. goto err;
  1920. ata_eh_done(link, dev, ATA_EH_REVALIDATE);
  1921. /* Configuration may have changed, reconfigure
  1922. * transfer mode.
  1923. */
  1924. ehc->i.flags |= ATA_EHI_SETMODE;
  1925. /* schedule the scsi_rescan_device() here */
  1926. queue_work(ata_aux_wq, &(ap->scsi_rescan_task));
  1927. } else if (dev->class == ATA_DEV_UNKNOWN &&
  1928. ehc->tries[dev->devno] &&
  1929. ata_class_enabled(ehc->classes[dev->devno])) {
  1930. dev->class = ehc->classes[dev->devno];
  1931. if (dev->class == ATA_DEV_PMP)
  1932. rc = sata_pmp_attach(dev);
  1933. else
  1934. rc = ata_dev_read_id(dev, &dev->class,
  1935. readid_flags, dev->id);
  1936. switch (rc) {
  1937. case 0:
  1938. new_mask |= 1 << dev->devno;
  1939. break;
  1940. case -ENOENT:
  1941. /* IDENTIFY was issued to non-existent
  1942. * device. No need to reset. Just
  1943. * thaw and kill the device.
  1944. */
  1945. ata_eh_thaw_port(ap);
  1946. dev->class = ATA_DEV_UNKNOWN;
  1947. break;
  1948. default:
  1949. dev->class = ATA_DEV_UNKNOWN;
  1950. goto err;
  1951. }
  1952. }
  1953. }
  1954. /* PDIAG- should have been released, ask cable type if post-reset */
  1955. if (ata_is_host_link(link) && ap->ops->cable_detect &&
  1956. (ehc->i.flags & ATA_EHI_DID_RESET))
  1957. ap->cbl = ap->ops->cable_detect(ap);
  1958. /* Configure new devices forward such that user doesn't see
  1959. * device detection messages backwards.
  1960. */
  1961. ata_link_for_each_dev(dev, link) {
  1962. if (!(new_mask & (1 << dev->devno)) ||
  1963. dev->class == ATA_DEV_PMP)
  1964. continue;
  1965. ehc->i.flags |= ATA_EHI_PRINTINFO;
  1966. rc = ata_dev_configure(dev);
  1967. ehc->i.flags &= ~ATA_EHI_PRINTINFO;
  1968. if (rc)
  1969. goto err;
  1970. spin_lock_irqsave(ap->lock, flags);
  1971. ap->pflags |= ATA_PFLAG_SCSI_HOTPLUG;
  1972. spin_unlock_irqrestore(ap->lock, flags);
  1973. /* new device discovered, configure xfermode */
  1974. ehc->i.flags |= ATA_EHI_SETMODE;
  1975. }
  1976. return 0;
  1977. err:
  1978. *r_failed_dev = dev;
  1979. DPRINTK("EXIT rc=%d\n", rc);
  1980. return rc;
  1981. }
  1982. static int ata_link_nr_enabled(struct ata_link *link)
  1983. {
  1984. struct ata_device *dev;
  1985. int cnt = 0;
  1986. ata_link_for_each_dev(dev, link)
  1987. if (ata_dev_enabled(dev))
  1988. cnt++;
  1989. return cnt;
  1990. }
  1991. static int ata_link_nr_vacant(struct ata_link *link)
  1992. {
  1993. struct ata_device *dev;
  1994. int cnt = 0;
  1995. ata_link_for_each_dev(dev, link)
  1996. if (dev->class == ATA_DEV_UNKNOWN)
  1997. cnt++;
  1998. return cnt;
  1999. }
  2000. static int ata_eh_skip_recovery(struct ata_link *link)
  2001. {
  2002. struct ata_eh_context *ehc = &link->eh_context;
  2003. struct ata_device *dev;
  2004. /* skip disabled links */
  2005. if (link->flags & ATA_LFLAG_DISABLED)
  2006. return 1;
  2007. /* thaw frozen port, resume link and recover failed devices */
  2008. if ((link->ap->pflags & ATA_PFLAG_FROZEN) ||
  2009. (ehc->i.flags & ATA_EHI_RESUME_LINK) || ata_link_nr_enabled(link))
  2010. return 0;
  2011. /* skip if class codes for all vacant slots are ATA_DEV_NONE */
  2012. ata_link_for_each_dev(dev, link) {
  2013. if (dev->class == ATA_DEV_UNKNOWN &&
  2014. ehc->classes[dev->devno] != ATA_DEV_NONE)
  2015. return 0;
  2016. }
  2017. return 1;
  2018. }
  2019. static int ata_eh_handle_dev_fail(struct ata_device *dev, int err)
  2020. {
  2021. struct ata_eh_context *ehc = &dev->link->eh_context;
  2022. ehc->tries[dev->devno]--;
  2023. switch (err) {
  2024. case -ENODEV:
  2025. /* device missing or wrong IDENTIFY data, schedule probing */
  2026. ehc->i.probe_mask |= (1 << dev->devno);
  2027. case -EINVAL:
  2028. /* give it just one more chance */
  2029. ehc->tries[dev->devno] = min(ehc->tries[dev->devno], 1);
  2030. case -EIO:
  2031. if (ehc->tries[dev->devno] == 1 && dev->pio_mode > XFER_PIO_0) {
  2032. /* This is the last chance, better to slow
  2033. * down than lose it.
  2034. */
  2035. sata_down_spd_limit(dev->link);
  2036. ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
  2037. }
  2038. }
  2039. if (ata_dev_enabled(dev) && !ehc->tries[dev->devno]) {
  2040. /* disable device if it has used up all its chances */
  2041. ata_dev_disable(dev);
  2042. /* detach if offline */
  2043. if (ata_link_offline(dev->link))
  2044. ata_eh_detach_dev(dev);
  2045. /* probe if requested */
  2046. if ((ehc->i.probe_mask & (1 << dev->devno)) &&
  2047. !(ehc->did_probe_mask & (1 << dev->devno))) {
  2048. ata_eh_detach_dev(dev);
  2049. ata_dev_init(dev);
  2050. ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
  2051. ehc->did_probe_mask |= (1 << dev->devno);
  2052. ehc->i.action |= ATA_EH_SOFTRESET;
  2053. }
  2054. return 1;
  2055. } else {
  2056. /* soft didn't work? be haaaaard */
  2057. if (ehc->i.flags & ATA_EHI_DID_RESET)
  2058. ehc->i.action |= ATA_EH_HARDRESET;
  2059. else
  2060. ehc->i.action |= ATA_EH_SOFTRESET;
  2061. return 0;
  2062. }
  2063. }
  2064. /**
  2065. * ata_eh_recover - recover host port after error
  2066. * @ap: host port to recover
  2067. * @prereset: prereset method (can be NULL)
  2068. * @softreset: softreset method (can be NULL)
  2069. * @hardreset: hardreset method (can be NULL)
  2070. * @postreset: postreset method (can be NULL)
  2071. * @r_failed_link: out parameter for failed link
  2072. *
  2073. * This is the alpha and omega, eum and yang, heart and soul of
  2074. * libata exception handling. On entry, actions required to
  2075. * recover each link and hotplug requests are recorded in the
  2076. * link's eh_context. This function executes all the operations
  2077. * with appropriate retrials and fallbacks to resurrect failed
  2078. * devices, detach goners and greet newcomers.
  2079. *
  2080. * LOCKING:
  2081. * Kernel thread context (may sleep).
  2082. *
  2083. * RETURNS:
  2084. * 0 on success, -errno on failure.
  2085. */
  2086. int ata_eh_recover(struct ata_port *ap, ata_prereset_fn_t prereset,
  2087. ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
  2088. ata_postreset_fn_t postreset,
  2089. struct ata_link **r_failed_link)
  2090. {
  2091. struct ata_link *link;
  2092. struct ata_device *dev;
  2093. int nr_failed_devs, nr_disabled_devs;
  2094. int reset, rc;
  2095. unsigned long flags;
  2096. DPRINTK("ENTER\n");
  2097. /* prep for recovery */
  2098. ata_port_for_each_link(link, ap) {
  2099. struct ata_eh_context *ehc = &link->eh_context;
  2100. /* re-enable link? */
  2101. if (ehc->i.action & ATA_EH_ENABLE_LINK) {
  2102. ata_eh_about_to_do(link, NULL, ATA_EH_ENABLE_LINK);
  2103. spin_lock_irqsave(ap->lock, flags);
  2104. link->flags &= ~ATA_LFLAG_DISABLED;
  2105. spin_unlock_irqrestore(ap->lock, flags);
  2106. ata_eh_done(link, NULL, ATA_EH_ENABLE_LINK);
  2107. }
  2108. ata_link_for_each_dev(dev, link) {
  2109. if (link->flags & ATA_LFLAG_NO_RETRY)
  2110. ehc->tries[dev->devno] = 1;
  2111. else
  2112. ehc->tries[dev->devno] = ATA_EH_DEV_TRIES;
  2113. /* collect port action mask recorded in dev actions */
  2114. ehc->i.action |= ehc->i.dev_action[dev->devno] &
  2115. ~ATA_EH_PERDEV_MASK;
  2116. ehc->i.dev_action[dev->devno] &= ATA_EH_PERDEV_MASK;
  2117. /* process hotplug request */
  2118. if (dev->flags & ATA_DFLAG_DETACH)
  2119. ata_eh_detach_dev(dev);
  2120. if (!ata_dev_enabled(dev) &&
  2121. ((ehc->i.probe_mask & (1 << dev->devno)) &&
  2122. !(ehc->did_probe_mask & (1 << dev->devno)))) {
  2123. ata_eh_detach_dev(dev);
  2124. ata_dev_init(dev);
  2125. ehc->did_probe_mask |= (1 << dev->devno);
  2126. ehc->i.action |= ATA_EH_SOFTRESET;
  2127. }
  2128. }
  2129. }
  2130. retry:
  2131. rc = 0;
  2132. nr_failed_devs = 0;
  2133. nr_disabled_devs = 0;
  2134. reset = 0;
  2135. /* if UNLOADING, finish immediately */
  2136. if (ap->pflags & ATA_PFLAG_UNLOADING)
  2137. goto out;
  2138. /* prep for EH */
  2139. ata_port_for_each_link(link, ap) {
  2140. struct ata_eh_context *ehc = &link->eh_context;
  2141. /* skip EH if possible. */
  2142. if (ata_eh_skip_recovery(link))
  2143. ehc->i.action = 0;
  2144. /* do we need to reset? */
  2145. if (ehc->i.action & ATA_EH_RESET_MASK)
  2146. reset = 1;
  2147. ata_link_for_each_dev(dev, link)
  2148. ehc->classes[dev->devno] = ATA_DEV_UNKNOWN;
  2149. }
  2150. /* reset */
  2151. if (reset) {
  2152. /* if PMP is attached, this function only deals with
  2153. * downstream links, port should stay thawed.
  2154. */
  2155. if (!ap->nr_pmp_links)
  2156. ata_eh_freeze_port(ap);
  2157. ata_port_for_each_link(link, ap) {
  2158. struct ata_eh_context *ehc = &link->eh_context;
  2159. if (!(ehc->i.action & ATA_EH_RESET_MASK))
  2160. continue;
  2161. rc = ata_eh_reset(link, ata_link_nr_vacant(link),
  2162. prereset, softreset, hardreset,
  2163. postreset);
  2164. if (rc) {
  2165. ata_link_printk(link, KERN_ERR,
  2166. "reset failed, giving up\n");
  2167. goto out;
  2168. }
  2169. }
  2170. if (!ap->nr_pmp_links)
  2171. ata_eh_thaw_port(ap);
  2172. }
  2173. /* the rest */
  2174. ata_port_for_each_link(link, ap) {
  2175. struct ata_eh_context *ehc = &link->eh_context;
  2176. /* revalidate existing devices and attach new ones */
  2177. rc = ata_eh_revalidate_and_attach(link, &dev);
  2178. if (rc)
  2179. goto dev_fail;
  2180. /* if PMP got attached, return, pmp EH will take care of it */
  2181. if (link->device->class == ATA_DEV_PMP) {
  2182. ehc->i.action = 0;
  2183. return 0;
  2184. }
  2185. /* configure transfer mode if necessary */
  2186. if (ehc->i.flags & ATA_EHI_SETMODE) {
  2187. rc = ata_set_mode(link, &dev);
  2188. if (rc)
  2189. goto dev_fail;
  2190. ehc->i.flags &= ~ATA_EHI_SETMODE;
  2191. }
  2192. if (ehc->i.action & ATA_EHI_LPM)
  2193. ata_link_for_each_dev(dev, link)
  2194. ata_dev_enable_pm(dev, ap->pm_policy);
  2195. /* this link is okay now */
  2196. ehc->i.flags = 0;
  2197. continue;
  2198. dev_fail:
  2199. nr_failed_devs++;
  2200. if (ata_eh_handle_dev_fail(dev, rc))
  2201. nr_disabled_devs++;
  2202. if (ap->pflags & ATA_PFLAG_FROZEN) {
  2203. /* PMP reset requires working host port.
  2204. * Can't retry if it's frozen.
  2205. */
  2206. if (ap->nr_pmp_links)
  2207. goto out;
  2208. break;
  2209. }
  2210. }
  2211. if (nr_failed_devs) {
  2212. if (nr_failed_devs != nr_disabled_devs) {
  2213. ata_port_printk(ap, KERN_WARNING, "failed to recover "
  2214. "some devices, retrying in 5 secs\n");
  2215. ssleep(5);
  2216. } else {
  2217. /* no device left to recover, repeat fast */
  2218. msleep(500);
  2219. }
  2220. goto retry;
  2221. }
  2222. out:
  2223. if (rc && r_failed_link)
  2224. *r_failed_link = link;
  2225. DPRINTK("EXIT, rc=%d\n", rc);
  2226. return rc;
  2227. }
  2228. /**
  2229. * ata_eh_finish - finish up EH
  2230. * @ap: host port to finish EH for
  2231. *
  2232. * Recovery is complete. Clean up EH states and retry or finish
  2233. * failed qcs.
  2234. *
  2235. * LOCKING:
  2236. * None.
  2237. */
  2238. void ata_eh_finish(struct ata_port *ap)
  2239. {
  2240. int tag;
  2241. /* retry or finish qcs */
  2242. for (tag = 0; tag < ATA_MAX_QUEUE; tag++) {
  2243. struct ata_queued_cmd *qc = __ata_qc_from_tag(ap, tag);
  2244. if (!(qc->flags & ATA_QCFLAG_FAILED))
  2245. continue;
  2246. if (qc->err_mask) {
  2247. /* FIXME: Once EH migration is complete,
  2248. * generate sense data in this function,
  2249. * considering both err_mask and tf.
  2250. *
  2251. * There's no point in retrying invalid
  2252. * (detected by libata) and non-IO device
  2253. * errors (rejected by device). Finish them
  2254. * immediately.
  2255. */
  2256. if ((qc->err_mask & AC_ERR_INVALID) ||
  2257. (!(qc->flags & ATA_QCFLAG_IO) &&
  2258. qc->err_mask == AC_ERR_DEV))
  2259. ata_eh_qc_complete(qc);
  2260. else
  2261. ata_eh_qc_retry(qc);
  2262. } else {
  2263. if (qc->flags & ATA_QCFLAG_SENSE_VALID) {
  2264. ata_eh_qc_complete(qc);
  2265. } else {
  2266. /* feed zero TF to sense generation */
  2267. memset(&qc->result_tf, 0, sizeof(qc->result_tf));
  2268. ata_eh_qc_retry(qc);
  2269. }
  2270. }
  2271. }
  2272. /* make sure nr_active_links is zero after EH */
  2273. WARN_ON(ap->nr_active_links);
  2274. ap->nr_active_links = 0;
  2275. }
  2276. /**
  2277. * ata_do_eh - do standard error handling
  2278. * @ap: host port to handle error for
  2279. * @prereset: prereset method (can be NULL)
  2280. * @softreset: softreset method (can be NULL)
  2281. * @hardreset: hardreset method (can be NULL)
  2282. * @postreset: postreset method (can be NULL)
  2283. *
  2284. * Perform standard error handling sequence.
  2285. *
  2286. * LOCKING:
  2287. * Kernel thread context (may sleep).
  2288. */
  2289. void ata_do_eh(struct ata_port *ap, ata_prereset_fn_t prereset,
  2290. ata_reset_fn_t softreset, ata_reset_fn_t hardreset,
  2291. ata_postreset_fn_t postreset)
  2292. {
  2293. struct ata_device *dev;
  2294. int rc;
  2295. ata_eh_autopsy(ap);
  2296. ata_eh_report(ap);
  2297. rc = ata_eh_recover(ap, prereset, softreset, hardreset, postreset,
  2298. NULL);
  2299. if (rc) {
  2300. ata_link_for_each_dev(dev, &ap->link)
  2301. ata_dev_disable(dev);
  2302. }
  2303. ata_eh_finish(ap);
  2304. }
  2305. #ifdef CONFIG_PM
  2306. /**
  2307. * ata_eh_handle_port_suspend - perform port suspend operation
  2308. * @ap: port to suspend
  2309. *
  2310. * Suspend @ap.
  2311. *
  2312. * LOCKING:
  2313. * Kernel thread context (may sleep).
  2314. */
  2315. static void ata_eh_handle_port_suspend(struct ata_port *ap)
  2316. {
  2317. unsigned long flags;
  2318. int rc = 0;
  2319. /* are we suspending? */
  2320. spin_lock_irqsave(ap->lock, flags);
  2321. if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
  2322. ap->pm_mesg.event == PM_EVENT_ON) {
  2323. spin_unlock_irqrestore(ap->lock, flags);
  2324. return;
  2325. }
  2326. spin_unlock_irqrestore(ap->lock, flags);
  2327. WARN_ON(ap->pflags & ATA_PFLAG_SUSPENDED);
  2328. /* tell ACPI we're suspending */
  2329. rc = ata_acpi_on_suspend(ap);
  2330. if (rc)
  2331. goto out;
  2332. /* suspend */
  2333. ata_eh_freeze_port(ap);
  2334. if (ap->ops->port_suspend)
  2335. rc = ap->ops->port_suspend(ap, ap->pm_mesg);
  2336. out:
  2337. /* report result */
  2338. spin_lock_irqsave(ap->lock, flags);
  2339. ap->pflags &= ~ATA_PFLAG_PM_PENDING;
  2340. if (rc == 0)
  2341. ap->pflags |= ATA_PFLAG_SUSPENDED;
  2342. else if (ap->pflags & ATA_PFLAG_FROZEN)
  2343. ata_port_schedule_eh(ap);
  2344. if (ap->pm_result) {
  2345. *ap->pm_result = rc;
  2346. ap->pm_result = NULL;
  2347. }
  2348. spin_unlock_irqrestore(ap->lock, flags);
  2349. return;
  2350. }
  2351. /**
  2352. * ata_eh_handle_port_resume - perform port resume operation
  2353. * @ap: port to resume
  2354. *
  2355. * Resume @ap.
  2356. *
  2357. * LOCKING:
  2358. * Kernel thread context (may sleep).
  2359. */
  2360. static void ata_eh_handle_port_resume(struct ata_port *ap)
  2361. {
  2362. unsigned long flags;
  2363. int rc = 0;
  2364. /* are we resuming? */
  2365. spin_lock_irqsave(ap->lock, flags);
  2366. if (!(ap->pflags & ATA_PFLAG_PM_PENDING) ||
  2367. ap->pm_mesg.event != PM_EVENT_ON) {
  2368. spin_unlock_irqrestore(ap->lock, flags);
  2369. return;
  2370. }
  2371. spin_unlock_irqrestore(ap->lock, flags);
  2372. WARN_ON(!(ap->pflags & ATA_PFLAG_SUSPENDED));
  2373. if (ap->ops->port_resume)
  2374. rc = ap->ops->port_resume(ap);
  2375. /* tell ACPI that we're resuming */
  2376. ata_acpi_on_resume(ap);
  2377. /* report result */
  2378. spin_lock_irqsave(ap->lock, flags);
  2379. ap->pflags &= ~(ATA_PFLAG_PM_PENDING | ATA_PFLAG_SUSPENDED);
  2380. if (ap->pm_result) {
  2381. *ap->pm_result = rc;
  2382. ap->pm_result = NULL;
  2383. }
  2384. spin_unlock_irqrestore(ap->lock, flags);
  2385. }
  2386. #endif /* CONFIG_PM */