libata-eh.c 71 KB

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