libata-eh.c 74 KB

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