libahci.c 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * libahci.c - Common AHCI SATA low-level routines
  4. *
  5. * Maintained by: Tejun Heo <[email protected]>
  6. * Please ALWAYS copy [email protected]
  7. * on emails.
  8. *
  9. * Copyright 2004-2005 Red Hat, Inc.
  10. *
  11. * libata documentation is available via 'make {ps|pdf}docs',
  12. * as Documentation/driver-api/libata.rst
  13. *
  14. * AHCI hardware documentation:
  15. * http://www.intel.com/technology/serialata/pdf/rev1_0.pdf
  16. * http://www.intel.com/technology/serialata/pdf/rev1_1.pdf
  17. */
  18. #include <linux/bitops.h>
  19. #include <linux/kernel.h>
  20. #include <linux/gfp.h>
  21. #include <linux/module.h>
  22. #include <linux/nospec.h>
  23. #include <linux/blkdev.h>
  24. #include <linux/delay.h>
  25. #include <linux/interrupt.h>
  26. #include <linux/dma-mapping.h>
  27. #include <linux/device.h>
  28. #include <scsi/scsi_host.h>
  29. #include <scsi/scsi_cmnd.h>
  30. #include <linux/libata.h>
  31. #include <linux/pci.h>
  32. #include "ahci.h"
  33. #include "libata.h"
  34. static int ahci_skip_host_reset;
  35. int ahci_ignore_sss;
  36. EXPORT_SYMBOL_GPL(ahci_ignore_sss);
  37. module_param_named(skip_host_reset, ahci_skip_host_reset, int, 0444);
  38. MODULE_PARM_DESC(skip_host_reset, "skip global host reset (0=don't skip, 1=skip)");
  39. module_param_named(ignore_sss, ahci_ignore_sss, int, 0444);
  40. MODULE_PARM_DESC(ignore_sss, "Ignore staggered spinup flag (0=don't ignore, 1=ignore)");
  41. static int ahci_set_lpm(struct ata_link *link, enum ata_lpm_policy policy,
  42. unsigned hints);
  43. static ssize_t ahci_led_show(struct ata_port *ap, char *buf);
  44. static ssize_t ahci_led_store(struct ata_port *ap, const char *buf,
  45. size_t size);
  46. static ssize_t ahci_transmit_led_message(struct ata_port *ap, u32 state,
  47. ssize_t size);
  48. static int ahci_scr_read(struct ata_link *link, unsigned int sc_reg, u32 *val);
  49. static int ahci_scr_write(struct ata_link *link, unsigned int sc_reg, u32 val);
  50. static bool ahci_qc_fill_rtf(struct ata_queued_cmd *qc);
  51. static int ahci_port_start(struct ata_port *ap);
  52. static void ahci_port_stop(struct ata_port *ap);
  53. static enum ata_completion_errors ahci_qc_prep(struct ata_queued_cmd *qc);
  54. static int ahci_pmp_qc_defer(struct ata_queued_cmd *qc);
  55. static void ahci_freeze(struct ata_port *ap);
  56. static void ahci_thaw(struct ata_port *ap);
  57. static void ahci_set_aggressive_devslp(struct ata_port *ap, bool sleep);
  58. static void ahci_enable_fbs(struct ata_port *ap);
  59. static void ahci_disable_fbs(struct ata_port *ap);
  60. static void ahci_pmp_attach(struct ata_port *ap);
  61. static void ahci_pmp_detach(struct ata_port *ap);
  62. static int ahci_softreset(struct ata_link *link, unsigned int *class,
  63. unsigned long deadline);
  64. static int ahci_pmp_retry_softreset(struct ata_link *link, unsigned int *class,
  65. unsigned long deadline);
  66. static int ahci_hardreset(struct ata_link *link, unsigned int *class,
  67. unsigned long deadline);
  68. static void ahci_postreset(struct ata_link *link, unsigned int *class);
  69. static void ahci_post_internal_cmd(struct ata_queued_cmd *qc);
  70. static void ahci_dev_config(struct ata_device *dev);
  71. #ifdef CONFIG_PM
  72. static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg);
  73. #endif
  74. static ssize_t ahci_activity_show(struct ata_device *dev, char *buf);
  75. static ssize_t ahci_activity_store(struct ata_device *dev,
  76. enum sw_activity val);
  77. static void ahci_init_sw_activity(struct ata_link *link);
  78. static ssize_t ahci_show_host_caps(struct device *dev,
  79. struct device_attribute *attr, char *buf);
  80. static ssize_t ahci_show_host_cap2(struct device *dev,
  81. struct device_attribute *attr, char *buf);
  82. static ssize_t ahci_show_host_version(struct device *dev,
  83. struct device_attribute *attr, char *buf);
  84. static ssize_t ahci_show_port_cmd(struct device *dev,
  85. struct device_attribute *attr, char *buf);
  86. static ssize_t ahci_read_em_buffer(struct device *dev,
  87. struct device_attribute *attr, char *buf);
  88. static ssize_t ahci_store_em_buffer(struct device *dev,
  89. struct device_attribute *attr,
  90. const char *buf, size_t size);
  91. static ssize_t ahci_show_em_supported(struct device *dev,
  92. struct device_attribute *attr, char *buf);
  93. static irqreturn_t ahci_single_level_irq_intr(int irq, void *dev_instance);
  94. static DEVICE_ATTR(ahci_host_caps, S_IRUGO, ahci_show_host_caps, NULL);
  95. static DEVICE_ATTR(ahci_host_cap2, S_IRUGO, ahci_show_host_cap2, NULL);
  96. static DEVICE_ATTR(ahci_host_version, S_IRUGO, ahci_show_host_version, NULL);
  97. static DEVICE_ATTR(ahci_port_cmd, S_IRUGO, ahci_show_port_cmd, NULL);
  98. static DEVICE_ATTR(em_buffer, S_IWUSR | S_IRUGO,
  99. ahci_read_em_buffer, ahci_store_em_buffer);
  100. static DEVICE_ATTR(em_message_supported, S_IRUGO, ahci_show_em_supported, NULL);
  101. static struct attribute *ahci_shost_attrs[] = {
  102. &dev_attr_link_power_management_policy.attr,
  103. &dev_attr_em_message_type.attr,
  104. &dev_attr_em_message.attr,
  105. &dev_attr_ahci_host_caps.attr,
  106. &dev_attr_ahci_host_cap2.attr,
  107. &dev_attr_ahci_host_version.attr,
  108. &dev_attr_ahci_port_cmd.attr,
  109. &dev_attr_em_buffer.attr,
  110. &dev_attr_em_message_supported.attr,
  111. NULL
  112. };
  113. static const struct attribute_group ahci_shost_attr_group = {
  114. .attrs = ahci_shost_attrs
  115. };
  116. const struct attribute_group *ahci_shost_groups[] = {
  117. &ahci_shost_attr_group,
  118. NULL
  119. };
  120. EXPORT_SYMBOL_GPL(ahci_shost_groups);
  121. static struct attribute *ahci_sdev_attrs[] = {
  122. &dev_attr_sw_activity.attr,
  123. &dev_attr_unload_heads.attr,
  124. &dev_attr_ncq_prio_supported.attr,
  125. &dev_attr_ncq_prio_enable.attr,
  126. NULL
  127. };
  128. static const struct attribute_group ahci_sdev_attr_group = {
  129. .attrs = ahci_sdev_attrs
  130. };
  131. const struct attribute_group *ahci_sdev_groups[] = {
  132. &ahci_sdev_attr_group,
  133. NULL
  134. };
  135. EXPORT_SYMBOL_GPL(ahci_sdev_groups);
  136. struct ata_port_operations ahci_ops = {
  137. .inherits = &sata_pmp_port_ops,
  138. .qc_defer = ahci_pmp_qc_defer,
  139. .qc_prep = ahci_qc_prep,
  140. .qc_issue = ahci_qc_issue,
  141. .qc_fill_rtf = ahci_qc_fill_rtf,
  142. .freeze = ahci_freeze,
  143. .thaw = ahci_thaw,
  144. .softreset = ahci_softreset,
  145. .hardreset = ahci_hardreset,
  146. .postreset = ahci_postreset,
  147. .pmp_softreset = ahci_softreset,
  148. .error_handler = ahci_error_handler,
  149. .post_internal_cmd = ahci_post_internal_cmd,
  150. .dev_config = ahci_dev_config,
  151. .scr_read = ahci_scr_read,
  152. .scr_write = ahci_scr_write,
  153. .pmp_attach = ahci_pmp_attach,
  154. .pmp_detach = ahci_pmp_detach,
  155. .set_lpm = ahci_set_lpm,
  156. .em_show = ahci_led_show,
  157. .em_store = ahci_led_store,
  158. .sw_activity_show = ahci_activity_show,
  159. .sw_activity_store = ahci_activity_store,
  160. .transmit_led_message = ahci_transmit_led_message,
  161. #ifdef CONFIG_PM
  162. .port_suspend = ahci_port_suspend,
  163. .port_resume = ahci_port_resume,
  164. #endif
  165. .port_start = ahci_port_start,
  166. .port_stop = ahci_port_stop,
  167. };
  168. EXPORT_SYMBOL_GPL(ahci_ops);
  169. struct ata_port_operations ahci_pmp_retry_srst_ops = {
  170. .inherits = &ahci_ops,
  171. .softreset = ahci_pmp_retry_softreset,
  172. };
  173. EXPORT_SYMBOL_GPL(ahci_pmp_retry_srst_ops);
  174. static bool ahci_em_messages __read_mostly = true;
  175. module_param(ahci_em_messages, bool, 0444);
  176. /* add other LED protocol types when they become supported */
  177. MODULE_PARM_DESC(ahci_em_messages,
  178. "AHCI Enclosure Management Message control (0 = off, 1 = on)");
  179. /* device sleep idle timeout in ms */
  180. static int devslp_idle_timeout __read_mostly = 1000;
  181. module_param(devslp_idle_timeout, int, 0644);
  182. MODULE_PARM_DESC(devslp_idle_timeout, "device sleep idle timeout");
  183. static void ahci_enable_ahci(void __iomem *mmio)
  184. {
  185. int i;
  186. u32 tmp;
  187. /* turn on AHCI_EN */
  188. tmp = readl(mmio + HOST_CTL);
  189. if (tmp & HOST_AHCI_EN)
  190. return;
  191. /* Some controllers need AHCI_EN to be written multiple times.
  192. * Try a few times before giving up.
  193. */
  194. for (i = 0; i < 5; i++) {
  195. tmp |= HOST_AHCI_EN;
  196. writel(tmp, mmio + HOST_CTL);
  197. tmp = readl(mmio + HOST_CTL); /* flush && sanity check */
  198. if (tmp & HOST_AHCI_EN)
  199. return;
  200. msleep(10);
  201. }
  202. WARN_ON(1);
  203. }
  204. /**
  205. * ahci_rpm_get_port - Make sure the port is powered on
  206. * @ap: Port to power on
  207. *
  208. * Whenever there is need to access the AHCI host registers outside of
  209. * normal execution paths, call this function to make sure the host is
  210. * actually powered on.
  211. */
  212. static int ahci_rpm_get_port(struct ata_port *ap)
  213. {
  214. return pm_runtime_get_sync(ap->dev);
  215. }
  216. /**
  217. * ahci_rpm_put_port - Undoes ahci_rpm_get_port()
  218. * @ap: Port to power down
  219. *
  220. * Undoes ahci_rpm_get_port() and possibly powers down the AHCI host
  221. * if it has no more active users.
  222. */
  223. static void ahci_rpm_put_port(struct ata_port *ap)
  224. {
  225. pm_runtime_put(ap->dev);
  226. }
  227. static ssize_t ahci_show_host_caps(struct device *dev,
  228. struct device_attribute *attr, char *buf)
  229. {
  230. struct Scsi_Host *shost = class_to_shost(dev);
  231. struct ata_port *ap = ata_shost_to_port(shost);
  232. struct ahci_host_priv *hpriv = ap->host->private_data;
  233. return sprintf(buf, "%x\n", hpriv->cap);
  234. }
  235. static ssize_t ahci_show_host_cap2(struct device *dev,
  236. struct device_attribute *attr, char *buf)
  237. {
  238. struct Scsi_Host *shost = class_to_shost(dev);
  239. struct ata_port *ap = ata_shost_to_port(shost);
  240. struct ahci_host_priv *hpriv = ap->host->private_data;
  241. return sprintf(buf, "%x\n", hpriv->cap2);
  242. }
  243. static ssize_t ahci_show_host_version(struct device *dev,
  244. struct device_attribute *attr, char *buf)
  245. {
  246. struct Scsi_Host *shost = class_to_shost(dev);
  247. struct ata_port *ap = ata_shost_to_port(shost);
  248. struct ahci_host_priv *hpriv = ap->host->private_data;
  249. return sprintf(buf, "%x\n", hpriv->version);
  250. }
  251. static ssize_t ahci_show_port_cmd(struct device *dev,
  252. struct device_attribute *attr, char *buf)
  253. {
  254. struct Scsi_Host *shost = class_to_shost(dev);
  255. struct ata_port *ap = ata_shost_to_port(shost);
  256. void __iomem *port_mmio = ahci_port_base(ap);
  257. ssize_t ret;
  258. ahci_rpm_get_port(ap);
  259. ret = sprintf(buf, "%x\n", readl(port_mmio + PORT_CMD));
  260. ahci_rpm_put_port(ap);
  261. return ret;
  262. }
  263. static ssize_t ahci_read_em_buffer(struct device *dev,
  264. struct device_attribute *attr, char *buf)
  265. {
  266. struct Scsi_Host *shost = class_to_shost(dev);
  267. struct ata_port *ap = ata_shost_to_port(shost);
  268. struct ahci_host_priv *hpriv = ap->host->private_data;
  269. void __iomem *mmio = hpriv->mmio;
  270. void __iomem *em_mmio = mmio + hpriv->em_loc;
  271. u32 em_ctl, msg;
  272. unsigned long flags;
  273. size_t count;
  274. int i;
  275. ahci_rpm_get_port(ap);
  276. spin_lock_irqsave(ap->lock, flags);
  277. em_ctl = readl(mmio + HOST_EM_CTL);
  278. if (!(ap->flags & ATA_FLAG_EM) || em_ctl & EM_CTL_XMT ||
  279. !(hpriv->em_msg_type & EM_MSG_TYPE_SGPIO)) {
  280. spin_unlock_irqrestore(ap->lock, flags);
  281. ahci_rpm_put_port(ap);
  282. return -EINVAL;
  283. }
  284. if (!(em_ctl & EM_CTL_MR)) {
  285. spin_unlock_irqrestore(ap->lock, flags);
  286. ahci_rpm_put_port(ap);
  287. return -EAGAIN;
  288. }
  289. if (!(em_ctl & EM_CTL_SMB))
  290. em_mmio += hpriv->em_buf_sz;
  291. count = hpriv->em_buf_sz;
  292. /* the count should not be larger than PAGE_SIZE */
  293. if (count > PAGE_SIZE) {
  294. if (printk_ratelimit())
  295. ata_port_warn(ap,
  296. "EM read buffer size too large: "
  297. "buffer size %u, page size %lu\n",
  298. hpriv->em_buf_sz, PAGE_SIZE);
  299. count = PAGE_SIZE;
  300. }
  301. for (i = 0; i < count; i += 4) {
  302. msg = readl(em_mmio + i);
  303. buf[i] = msg & 0xff;
  304. buf[i + 1] = (msg >> 8) & 0xff;
  305. buf[i + 2] = (msg >> 16) & 0xff;
  306. buf[i + 3] = (msg >> 24) & 0xff;
  307. }
  308. spin_unlock_irqrestore(ap->lock, flags);
  309. ahci_rpm_put_port(ap);
  310. return i;
  311. }
  312. static ssize_t ahci_store_em_buffer(struct device *dev,
  313. struct device_attribute *attr,
  314. const char *buf, size_t size)
  315. {
  316. struct Scsi_Host *shost = class_to_shost(dev);
  317. struct ata_port *ap = ata_shost_to_port(shost);
  318. struct ahci_host_priv *hpriv = ap->host->private_data;
  319. void __iomem *mmio = hpriv->mmio;
  320. void __iomem *em_mmio = mmio + hpriv->em_loc;
  321. const unsigned char *msg_buf = buf;
  322. u32 em_ctl, msg;
  323. unsigned long flags;
  324. int i;
  325. /* check size validity */
  326. if (!(ap->flags & ATA_FLAG_EM) ||
  327. !(hpriv->em_msg_type & EM_MSG_TYPE_SGPIO) ||
  328. size % 4 || size > hpriv->em_buf_sz)
  329. return -EINVAL;
  330. ahci_rpm_get_port(ap);
  331. spin_lock_irqsave(ap->lock, flags);
  332. em_ctl = readl(mmio + HOST_EM_CTL);
  333. if (em_ctl & EM_CTL_TM) {
  334. spin_unlock_irqrestore(ap->lock, flags);
  335. ahci_rpm_put_port(ap);
  336. return -EBUSY;
  337. }
  338. for (i = 0; i < size; i += 4) {
  339. msg = msg_buf[i] | msg_buf[i + 1] << 8 |
  340. msg_buf[i + 2] << 16 | msg_buf[i + 3] << 24;
  341. writel(msg, em_mmio + i);
  342. }
  343. writel(em_ctl | EM_CTL_TM, mmio + HOST_EM_CTL);
  344. spin_unlock_irqrestore(ap->lock, flags);
  345. ahci_rpm_put_port(ap);
  346. return size;
  347. }
  348. static ssize_t ahci_show_em_supported(struct device *dev,
  349. struct device_attribute *attr, char *buf)
  350. {
  351. struct Scsi_Host *shost = class_to_shost(dev);
  352. struct ata_port *ap = ata_shost_to_port(shost);
  353. struct ahci_host_priv *hpriv = ap->host->private_data;
  354. void __iomem *mmio = hpriv->mmio;
  355. u32 em_ctl;
  356. ahci_rpm_get_port(ap);
  357. em_ctl = readl(mmio + HOST_EM_CTL);
  358. ahci_rpm_put_port(ap);
  359. return sprintf(buf, "%s%s%s%s\n",
  360. em_ctl & EM_CTL_LED ? "led " : "",
  361. em_ctl & EM_CTL_SAFTE ? "saf-te " : "",
  362. em_ctl & EM_CTL_SES ? "ses-2 " : "",
  363. em_ctl & EM_CTL_SGPIO ? "sgpio " : "");
  364. }
  365. /**
  366. * ahci_save_initial_config - Save and fixup initial config values
  367. * @dev: target AHCI device
  368. * @hpriv: host private area to store config values
  369. *
  370. * Some registers containing configuration info might be setup by
  371. * BIOS and might be cleared on reset. This function saves the
  372. * initial values of those registers into @hpriv such that they
  373. * can be restored after controller reset.
  374. *
  375. * If inconsistent, config values are fixed up by this function.
  376. *
  377. * If it is not set already this function sets hpriv->start_engine to
  378. * ahci_start_engine.
  379. *
  380. * LOCKING:
  381. * None.
  382. */
  383. void ahci_save_initial_config(struct device *dev, struct ahci_host_priv *hpriv)
  384. {
  385. void __iomem *mmio = hpriv->mmio;
  386. void __iomem *port_mmio;
  387. unsigned long port_map;
  388. u32 cap, cap2, vers;
  389. int i;
  390. /* make sure AHCI mode is enabled before accessing CAP */
  391. ahci_enable_ahci(mmio);
  392. /*
  393. * Values prefixed with saved_ are written back to the HBA and ports
  394. * registers after reset. Values without are used for driver operation.
  395. */
  396. /*
  397. * Override HW-init HBA capability fields with the platform-specific
  398. * values. The rest of the HBA capabilities are defined as Read-only
  399. * and can't be modified in CSR anyway.
  400. */
  401. cap = readl(mmio + HOST_CAP);
  402. if (hpriv->saved_cap)
  403. cap = (cap & ~(HOST_CAP_SSS | HOST_CAP_MPS)) | hpriv->saved_cap;
  404. hpriv->saved_cap = cap;
  405. /* CAP2 register is only defined for AHCI 1.2 and later */
  406. vers = readl(mmio + HOST_VERSION);
  407. if ((vers >> 16) > 1 ||
  408. ((vers >> 16) == 1 && (vers & 0xFFFF) >= 0x200))
  409. hpriv->saved_cap2 = cap2 = readl(mmio + HOST_CAP2);
  410. else
  411. hpriv->saved_cap2 = cap2 = 0;
  412. /* some chips have errata preventing 64bit use */
  413. if ((cap & HOST_CAP_64) && (hpriv->flags & AHCI_HFLAG_32BIT_ONLY)) {
  414. dev_info(dev, "controller can't do 64bit DMA, forcing 32bit\n");
  415. cap &= ~HOST_CAP_64;
  416. }
  417. if ((cap & HOST_CAP_NCQ) && (hpriv->flags & AHCI_HFLAG_NO_NCQ)) {
  418. dev_info(dev, "controller can't do NCQ, turning off CAP_NCQ\n");
  419. cap &= ~HOST_CAP_NCQ;
  420. }
  421. if (!(cap & HOST_CAP_NCQ) && (hpriv->flags & AHCI_HFLAG_YES_NCQ)) {
  422. dev_info(dev, "controller can do NCQ, turning on CAP_NCQ\n");
  423. cap |= HOST_CAP_NCQ;
  424. }
  425. if ((cap & HOST_CAP_PMP) && (hpriv->flags & AHCI_HFLAG_NO_PMP)) {
  426. dev_info(dev, "controller can't do PMP, turning off CAP_PMP\n");
  427. cap &= ~HOST_CAP_PMP;
  428. }
  429. if ((cap & HOST_CAP_SNTF) && (hpriv->flags & AHCI_HFLAG_NO_SNTF)) {
  430. dev_info(dev,
  431. "controller can't do SNTF, turning off CAP_SNTF\n");
  432. cap &= ~HOST_CAP_SNTF;
  433. }
  434. if ((cap2 & HOST_CAP2_SDS) && (hpriv->flags & AHCI_HFLAG_NO_DEVSLP)) {
  435. dev_info(dev,
  436. "controller can't do DEVSLP, turning off\n");
  437. cap2 &= ~HOST_CAP2_SDS;
  438. cap2 &= ~HOST_CAP2_SADM;
  439. }
  440. if (!(cap & HOST_CAP_FBS) && (hpriv->flags & AHCI_HFLAG_YES_FBS)) {
  441. dev_info(dev, "controller can do FBS, turning on CAP_FBS\n");
  442. cap |= HOST_CAP_FBS;
  443. }
  444. if ((cap & HOST_CAP_FBS) && (hpriv->flags & AHCI_HFLAG_NO_FBS)) {
  445. dev_info(dev, "controller can't do FBS, turning off CAP_FBS\n");
  446. cap &= ~HOST_CAP_FBS;
  447. }
  448. if (!(cap & HOST_CAP_ALPM) && (hpriv->flags & AHCI_HFLAG_YES_ALPM)) {
  449. dev_info(dev, "controller can do ALPM, turning on CAP_ALPM\n");
  450. cap |= HOST_CAP_ALPM;
  451. }
  452. if ((cap & HOST_CAP_SXS) && (hpriv->flags & AHCI_HFLAG_NO_SXS)) {
  453. dev_info(dev, "controller does not support SXS, disabling CAP_SXS\n");
  454. cap &= ~HOST_CAP_SXS;
  455. }
  456. /* Override the HBA ports mapping if the platform needs it */
  457. port_map = readl(mmio + HOST_PORTS_IMPL);
  458. if (hpriv->saved_port_map && port_map != hpriv->saved_port_map) {
  459. dev_info(dev, "forcing port_map 0x%lx -> 0x%x\n",
  460. port_map, hpriv->saved_port_map);
  461. port_map = hpriv->saved_port_map;
  462. } else {
  463. hpriv->saved_port_map = port_map;
  464. }
  465. if (hpriv->mask_port_map) {
  466. dev_warn(dev, "masking port_map 0x%lx -> 0x%lx\n",
  467. port_map,
  468. port_map & hpriv->mask_port_map);
  469. port_map &= hpriv->mask_port_map;
  470. }
  471. /* cross check port_map and cap.n_ports */
  472. if (port_map) {
  473. int map_ports = 0;
  474. for (i = 0; i < AHCI_MAX_PORTS; i++)
  475. if (port_map & (1 << i))
  476. map_ports++;
  477. /* If PI has more ports than n_ports, whine, clear
  478. * port_map and let it be generated from n_ports.
  479. */
  480. if (map_ports > ahci_nr_ports(cap)) {
  481. dev_warn(dev,
  482. "implemented port map (0x%lx) contains more ports than nr_ports (%u), using nr_ports\n",
  483. port_map, ahci_nr_ports(cap));
  484. port_map = 0;
  485. }
  486. }
  487. /* fabricate port_map from cap.nr_ports for < AHCI 1.3 */
  488. if (!port_map && vers < 0x10300) {
  489. port_map = (1 << ahci_nr_ports(cap)) - 1;
  490. dev_warn(dev, "forcing PORTS_IMPL to 0x%lx\n", port_map);
  491. /* write the fixed up value to the PI register */
  492. hpriv->saved_port_map = port_map;
  493. }
  494. /*
  495. * Preserve the ports capabilities defined by the platform. Note there
  496. * is no need in storing the rest of the P#.CMD fields since they are
  497. * volatile.
  498. */
  499. for_each_set_bit(i, &port_map, AHCI_MAX_PORTS) {
  500. if (hpriv->saved_port_cap[i])
  501. continue;
  502. port_mmio = __ahci_port_base(hpriv, i);
  503. hpriv->saved_port_cap[i] =
  504. readl(port_mmio + PORT_CMD) & PORT_CMD_CAP;
  505. }
  506. /* record values to use during operation */
  507. hpriv->cap = cap;
  508. hpriv->cap2 = cap2;
  509. hpriv->version = vers;
  510. hpriv->port_map = port_map;
  511. if (!hpriv->start_engine)
  512. hpriv->start_engine = ahci_start_engine;
  513. if (!hpriv->stop_engine)
  514. hpriv->stop_engine = ahci_stop_engine;
  515. if (!hpriv->irq_handler)
  516. hpriv->irq_handler = ahci_single_level_irq_intr;
  517. }
  518. EXPORT_SYMBOL_GPL(ahci_save_initial_config);
  519. /**
  520. * ahci_restore_initial_config - Restore initial config
  521. * @host: target ATA host
  522. *
  523. * Restore initial config stored by ahci_save_initial_config().
  524. *
  525. * LOCKING:
  526. * None.
  527. */
  528. static void ahci_restore_initial_config(struct ata_host *host)
  529. {
  530. struct ahci_host_priv *hpriv = host->private_data;
  531. unsigned long port_map = hpriv->port_map;
  532. void __iomem *mmio = hpriv->mmio;
  533. void __iomem *port_mmio;
  534. int i;
  535. writel(hpriv->saved_cap, mmio + HOST_CAP);
  536. if (hpriv->saved_cap2)
  537. writel(hpriv->saved_cap2, mmio + HOST_CAP2);
  538. writel(hpriv->saved_port_map, mmio + HOST_PORTS_IMPL);
  539. (void) readl(mmio + HOST_PORTS_IMPL); /* flush */
  540. for_each_set_bit(i, &port_map, AHCI_MAX_PORTS) {
  541. port_mmio = __ahci_port_base(hpriv, i);
  542. writel(hpriv->saved_port_cap[i], port_mmio + PORT_CMD);
  543. }
  544. }
  545. static unsigned ahci_scr_offset(struct ata_port *ap, unsigned int sc_reg)
  546. {
  547. static const int offset[] = {
  548. [SCR_STATUS] = PORT_SCR_STAT,
  549. [SCR_CONTROL] = PORT_SCR_CTL,
  550. [SCR_ERROR] = PORT_SCR_ERR,
  551. [SCR_ACTIVE] = PORT_SCR_ACT,
  552. [SCR_NOTIFICATION] = PORT_SCR_NTF,
  553. };
  554. struct ahci_host_priv *hpriv = ap->host->private_data;
  555. if (sc_reg < ARRAY_SIZE(offset) &&
  556. (sc_reg != SCR_NOTIFICATION || (hpriv->cap & HOST_CAP_SNTF)))
  557. return offset[sc_reg];
  558. return 0;
  559. }
  560. static int ahci_scr_read(struct ata_link *link, unsigned int sc_reg, u32 *val)
  561. {
  562. void __iomem *port_mmio = ahci_port_base(link->ap);
  563. int offset = ahci_scr_offset(link->ap, sc_reg);
  564. if (offset) {
  565. *val = readl(port_mmio + offset);
  566. return 0;
  567. }
  568. return -EINVAL;
  569. }
  570. static int ahci_scr_write(struct ata_link *link, unsigned int sc_reg, u32 val)
  571. {
  572. void __iomem *port_mmio = ahci_port_base(link->ap);
  573. int offset = ahci_scr_offset(link->ap, sc_reg);
  574. if (offset) {
  575. writel(val, port_mmio + offset);
  576. return 0;
  577. }
  578. return -EINVAL;
  579. }
  580. void ahci_start_engine(struct ata_port *ap)
  581. {
  582. void __iomem *port_mmio = ahci_port_base(ap);
  583. u32 tmp;
  584. /* start DMA */
  585. tmp = readl(port_mmio + PORT_CMD);
  586. tmp |= PORT_CMD_START;
  587. writel(tmp, port_mmio + PORT_CMD);
  588. readl(port_mmio + PORT_CMD); /* flush */
  589. }
  590. EXPORT_SYMBOL_GPL(ahci_start_engine);
  591. int ahci_stop_engine(struct ata_port *ap)
  592. {
  593. void __iomem *port_mmio = ahci_port_base(ap);
  594. struct ahci_host_priv *hpriv = ap->host->private_data;
  595. u32 tmp;
  596. /*
  597. * On some controllers, stopping a port's DMA engine while the port
  598. * is in ALPM state (partial or slumber) results in failures on
  599. * subsequent DMA engine starts. For those controllers, put the
  600. * port back in active state before stopping its DMA engine.
  601. */
  602. if ((hpriv->flags & AHCI_HFLAG_WAKE_BEFORE_STOP) &&
  603. (ap->link.lpm_policy > ATA_LPM_MAX_POWER) &&
  604. ahci_set_lpm(&ap->link, ATA_LPM_MAX_POWER, ATA_LPM_WAKE_ONLY)) {
  605. dev_err(ap->host->dev, "Failed to wake up port before engine stop\n");
  606. return -EIO;
  607. }
  608. tmp = readl(port_mmio + PORT_CMD);
  609. /* check if the HBA is idle */
  610. if ((tmp & (PORT_CMD_START | PORT_CMD_LIST_ON)) == 0)
  611. return 0;
  612. /*
  613. * Don't try to issue commands but return with ENODEV if the
  614. * AHCI controller not available anymore (e.g. due to PCIe hot
  615. * unplugging). Otherwise a 500ms delay for each port is added.
  616. */
  617. if (tmp == 0xffffffff) {
  618. dev_err(ap->host->dev, "AHCI controller unavailable!\n");
  619. return -ENODEV;
  620. }
  621. /* setting HBA to idle */
  622. tmp &= ~PORT_CMD_START;
  623. writel(tmp, port_mmio + PORT_CMD);
  624. /* wait for engine to stop. This could be as long as 500 msec */
  625. tmp = ata_wait_register(ap, port_mmio + PORT_CMD,
  626. PORT_CMD_LIST_ON, PORT_CMD_LIST_ON, 1, 500);
  627. if (tmp & PORT_CMD_LIST_ON)
  628. return -EIO;
  629. return 0;
  630. }
  631. EXPORT_SYMBOL_GPL(ahci_stop_engine);
  632. void ahci_start_fis_rx(struct ata_port *ap)
  633. {
  634. void __iomem *port_mmio = ahci_port_base(ap);
  635. struct ahci_host_priv *hpriv = ap->host->private_data;
  636. struct ahci_port_priv *pp = ap->private_data;
  637. u32 tmp;
  638. /* set FIS registers */
  639. if (hpriv->cap & HOST_CAP_64)
  640. writel((pp->cmd_slot_dma >> 16) >> 16,
  641. port_mmio + PORT_LST_ADDR_HI);
  642. writel(pp->cmd_slot_dma & 0xffffffff, port_mmio + PORT_LST_ADDR);
  643. if (hpriv->cap & HOST_CAP_64)
  644. writel((pp->rx_fis_dma >> 16) >> 16,
  645. port_mmio + PORT_FIS_ADDR_HI);
  646. writel(pp->rx_fis_dma & 0xffffffff, port_mmio + PORT_FIS_ADDR);
  647. /* enable FIS reception */
  648. tmp = readl(port_mmio + PORT_CMD);
  649. tmp |= PORT_CMD_FIS_RX;
  650. writel(tmp, port_mmio + PORT_CMD);
  651. /* flush */
  652. readl(port_mmio + PORT_CMD);
  653. }
  654. EXPORT_SYMBOL_GPL(ahci_start_fis_rx);
  655. static int ahci_stop_fis_rx(struct ata_port *ap)
  656. {
  657. void __iomem *port_mmio = ahci_port_base(ap);
  658. u32 tmp;
  659. /* disable FIS reception */
  660. tmp = readl(port_mmio + PORT_CMD);
  661. tmp &= ~PORT_CMD_FIS_RX;
  662. writel(tmp, port_mmio + PORT_CMD);
  663. /* wait for completion, spec says 500ms, give it 1000 */
  664. tmp = ata_wait_register(ap, port_mmio + PORT_CMD, PORT_CMD_FIS_ON,
  665. PORT_CMD_FIS_ON, 10, 1000);
  666. if (tmp & PORT_CMD_FIS_ON)
  667. return -EBUSY;
  668. return 0;
  669. }
  670. static void ahci_power_up(struct ata_port *ap)
  671. {
  672. struct ahci_host_priv *hpriv = ap->host->private_data;
  673. void __iomem *port_mmio = ahci_port_base(ap);
  674. u32 cmd;
  675. cmd = readl(port_mmio + PORT_CMD) & ~PORT_CMD_ICC_MASK;
  676. /* spin up device */
  677. if (hpriv->cap & HOST_CAP_SSS) {
  678. cmd |= PORT_CMD_SPIN_UP;
  679. writel(cmd, port_mmio + PORT_CMD);
  680. }
  681. /* wake up link */
  682. writel(cmd | PORT_CMD_ICC_ACTIVE, port_mmio + PORT_CMD);
  683. }
  684. static int ahci_set_lpm(struct ata_link *link, enum ata_lpm_policy policy,
  685. unsigned int hints)
  686. {
  687. struct ata_port *ap = link->ap;
  688. struct ahci_host_priv *hpriv = ap->host->private_data;
  689. struct ahci_port_priv *pp = ap->private_data;
  690. void __iomem *port_mmio = ahci_port_base(ap);
  691. if (policy != ATA_LPM_MAX_POWER) {
  692. /* wakeup flag only applies to the max power policy */
  693. hints &= ~ATA_LPM_WAKE_ONLY;
  694. /*
  695. * Disable interrupts on Phy Ready. This keeps us from
  696. * getting woken up due to spurious phy ready
  697. * interrupts.
  698. */
  699. pp->intr_mask &= ~PORT_IRQ_PHYRDY;
  700. writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
  701. sata_link_scr_lpm(link, policy, false);
  702. }
  703. if (hpriv->cap & HOST_CAP_ALPM) {
  704. u32 cmd = readl(port_mmio + PORT_CMD);
  705. if (policy == ATA_LPM_MAX_POWER || !(hints & ATA_LPM_HIPM)) {
  706. if (!(hints & ATA_LPM_WAKE_ONLY))
  707. cmd &= ~(PORT_CMD_ASP | PORT_CMD_ALPE);
  708. cmd |= PORT_CMD_ICC_ACTIVE;
  709. writel(cmd, port_mmio + PORT_CMD);
  710. readl(port_mmio + PORT_CMD);
  711. /* wait 10ms to be sure we've come out of LPM state */
  712. ata_msleep(ap, 10);
  713. if (hints & ATA_LPM_WAKE_ONLY)
  714. return 0;
  715. } else {
  716. cmd |= PORT_CMD_ALPE;
  717. if (policy == ATA_LPM_MIN_POWER)
  718. cmd |= PORT_CMD_ASP;
  719. else if (policy == ATA_LPM_MIN_POWER_WITH_PARTIAL)
  720. cmd &= ~PORT_CMD_ASP;
  721. /* write out new cmd value */
  722. writel(cmd, port_mmio + PORT_CMD);
  723. }
  724. }
  725. /* set aggressive device sleep */
  726. if ((hpriv->cap2 & HOST_CAP2_SDS) &&
  727. (hpriv->cap2 & HOST_CAP2_SADM) &&
  728. (link->device->flags & ATA_DFLAG_DEVSLP)) {
  729. if (policy == ATA_LPM_MIN_POWER ||
  730. policy == ATA_LPM_MIN_POWER_WITH_PARTIAL)
  731. ahci_set_aggressive_devslp(ap, true);
  732. else
  733. ahci_set_aggressive_devslp(ap, false);
  734. }
  735. if (policy == ATA_LPM_MAX_POWER) {
  736. sata_link_scr_lpm(link, policy, false);
  737. /* turn PHYRDY IRQ back on */
  738. pp->intr_mask |= PORT_IRQ_PHYRDY;
  739. writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
  740. }
  741. return 0;
  742. }
  743. #ifdef CONFIG_PM
  744. static void ahci_power_down(struct ata_port *ap)
  745. {
  746. struct ahci_host_priv *hpriv = ap->host->private_data;
  747. void __iomem *port_mmio = ahci_port_base(ap);
  748. u32 cmd, scontrol;
  749. if (!(hpriv->cap & HOST_CAP_SSS))
  750. return;
  751. /* put device into listen mode, first set PxSCTL.DET to 0 */
  752. scontrol = readl(port_mmio + PORT_SCR_CTL);
  753. scontrol &= ~0xf;
  754. writel(scontrol, port_mmio + PORT_SCR_CTL);
  755. /* then set PxCMD.SUD to 0 */
  756. cmd = readl(port_mmio + PORT_CMD) & ~PORT_CMD_ICC_MASK;
  757. cmd &= ~PORT_CMD_SPIN_UP;
  758. writel(cmd, port_mmio + PORT_CMD);
  759. }
  760. #endif
  761. static void ahci_start_port(struct ata_port *ap)
  762. {
  763. struct ahci_host_priv *hpriv = ap->host->private_data;
  764. struct ahci_port_priv *pp = ap->private_data;
  765. struct ata_link *link;
  766. struct ahci_em_priv *emp;
  767. ssize_t rc;
  768. int i;
  769. /* enable FIS reception */
  770. ahci_start_fis_rx(ap);
  771. /* enable DMA */
  772. if (!(hpriv->flags & AHCI_HFLAG_DELAY_ENGINE))
  773. hpriv->start_engine(ap);
  774. /* turn on LEDs */
  775. if (ap->flags & ATA_FLAG_EM) {
  776. ata_for_each_link(link, ap, EDGE) {
  777. emp = &pp->em_priv[link->pmp];
  778. /* EM Transmit bit maybe busy during init */
  779. for (i = 0; i < EM_MAX_RETRY; i++) {
  780. rc = ap->ops->transmit_led_message(ap,
  781. emp->led_state,
  782. 4);
  783. /*
  784. * If busy, give a breather but do not
  785. * release EH ownership by using msleep()
  786. * instead of ata_msleep(). EM Transmit
  787. * bit is busy for the whole host and
  788. * releasing ownership will cause other
  789. * ports to fail the same way.
  790. */
  791. if (rc == -EBUSY)
  792. msleep(1);
  793. else
  794. break;
  795. }
  796. }
  797. }
  798. if (ap->flags & ATA_FLAG_SW_ACTIVITY)
  799. ata_for_each_link(link, ap, EDGE)
  800. ahci_init_sw_activity(link);
  801. }
  802. static int ahci_deinit_port(struct ata_port *ap, const char **emsg)
  803. {
  804. int rc;
  805. struct ahci_host_priv *hpriv = ap->host->private_data;
  806. /* disable DMA */
  807. rc = hpriv->stop_engine(ap);
  808. if (rc) {
  809. *emsg = "failed to stop engine";
  810. return rc;
  811. }
  812. /* disable FIS reception */
  813. rc = ahci_stop_fis_rx(ap);
  814. if (rc) {
  815. *emsg = "failed stop FIS RX";
  816. return rc;
  817. }
  818. return 0;
  819. }
  820. int ahci_reset_controller(struct ata_host *host)
  821. {
  822. struct ahci_host_priv *hpriv = host->private_data;
  823. void __iomem *mmio = hpriv->mmio;
  824. u32 tmp;
  825. /* we must be in AHCI mode, before using anything
  826. * AHCI-specific, such as HOST_RESET.
  827. */
  828. ahci_enable_ahci(mmio);
  829. /* global controller reset */
  830. if (!ahci_skip_host_reset) {
  831. tmp = readl(mmio + HOST_CTL);
  832. if ((tmp & HOST_RESET) == 0) {
  833. writel(tmp | HOST_RESET, mmio + HOST_CTL);
  834. readl(mmio + HOST_CTL); /* flush */
  835. }
  836. /*
  837. * to perform host reset, OS should set HOST_RESET
  838. * and poll until this bit is read to be "0".
  839. * reset must complete within 1 second, or
  840. * the hardware should be considered fried.
  841. */
  842. tmp = ata_wait_register(NULL, mmio + HOST_CTL, HOST_RESET,
  843. HOST_RESET, 10, 1000);
  844. if (tmp & HOST_RESET) {
  845. dev_err(host->dev, "controller reset failed (0x%x)\n",
  846. tmp);
  847. return -EIO;
  848. }
  849. /* turn on AHCI mode */
  850. ahci_enable_ahci(mmio);
  851. /* Some registers might be cleared on reset. Restore
  852. * initial values.
  853. */
  854. if (!(hpriv->flags & AHCI_HFLAG_NO_WRITE_TO_RO))
  855. ahci_restore_initial_config(host);
  856. } else
  857. dev_info(host->dev, "skipping global host reset\n");
  858. return 0;
  859. }
  860. EXPORT_SYMBOL_GPL(ahci_reset_controller);
  861. static void ahci_sw_activity(struct ata_link *link)
  862. {
  863. struct ata_port *ap = link->ap;
  864. struct ahci_port_priv *pp = ap->private_data;
  865. struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
  866. if (!(link->flags & ATA_LFLAG_SW_ACTIVITY))
  867. return;
  868. emp->activity++;
  869. if (!timer_pending(&emp->timer))
  870. mod_timer(&emp->timer, jiffies + msecs_to_jiffies(10));
  871. }
  872. static void ahci_sw_activity_blink(struct timer_list *t)
  873. {
  874. struct ahci_em_priv *emp = from_timer(emp, t, timer);
  875. struct ata_link *link = emp->link;
  876. struct ata_port *ap = link->ap;
  877. unsigned long led_message = emp->led_state;
  878. u32 activity_led_state;
  879. unsigned long flags;
  880. led_message &= EM_MSG_LED_VALUE;
  881. led_message |= ap->port_no | (link->pmp << 8);
  882. /* check to see if we've had activity. If so,
  883. * toggle state of LED and reset timer. If not,
  884. * turn LED to desired idle state.
  885. */
  886. spin_lock_irqsave(ap->lock, flags);
  887. if (emp->saved_activity != emp->activity) {
  888. emp->saved_activity = emp->activity;
  889. /* get the current LED state */
  890. activity_led_state = led_message & EM_MSG_LED_VALUE_ON;
  891. if (activity_led_state)
  892. activity_led_state = 0;
  893. else
  894. activity_led_state = 1;
  895. /* clear old state */
  896. led_message &= ~EM_MSG_LED_VALUE_ACTIVITY;
  897. /* toggle state */
  898. led_message |= (activity_led_state << 16);
  899. mod_timer(&emp->timer, jiffies + msecs_to_jiffies(100));
  900. } else {
  901. /* switch to idle */
  902. led_message &= ~EM_MSG_LED_VALUE_ACTIVITY;
  903. if (emp->blink_policy == BLINK_OFF)
  904. led_message |= (1 << 16);
  905. }
  906. spin_unlock_irqrestore(ap->lock, flags);
  907. ap->ops->transmit_led_message(ap, led_message, 4);
  908. }
  909. static void ahci_init_sw_activity(struct ata_link *link)
  910. {
  911. struct ata_port *ap = link->ap;
  912. struct ahci_port_priv *pp = ap->private_data;
  913. struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
  914. /* init activity stats, setup timer */
  915. emp->saved_activity = emp->activity = 0;
  916. emp->link = link;
  917. timer_setup(&emp->timer, ahci_sw_activity_blink, 0);
  918. /* check our blink policy and set flag for link if it's enabled */
  919. if (emp->blink_policy)
  920. link->flags |= ATA_LFLAG_SW_ACTIVITY;
  921. }
  922. int ahci_reset_em(struct ata_host *host)
  923. {
  924. struct ahci_host_priv *hpriv = host->private_data;
  925. void __iomem *mmio = hpriv->mmio;
  926. u32 em_ctl;
  927. em_ctl = readl(mmio + HOST_EM_CTL);
  928. if ((em_ctl & EM_CTL_TM) || (em_ctl & EM_CTL_RST))
  929. return -EINVAL;
  930. writel(em_ctl | EM_CTL_RST, mmio + HOST_EM_CTL);
  931. return 0;
  932. }
  933. EXPORT_SYMBOL_GPL(ahci_reset_em);
  934. static ssize_t ahci_transmit_led_message(struct ata_port *ap, u32 state,
  935. ssize_t size)
  936. {
  937. struct ahci_host_priv *hpriv = ap->host->private_data;
  938. struct ahci_port_priv *pp = ap->private_data;
  939. void __iomem *mmio = hpriv->mmio;
  940. u32 em_ctl;
  941. u32 message[] = {0, 0};
  942. unsigned long flags;
  943. int pmp;
  944. struct ahci_em_priv *emp;
  945. /* get the slot number from the message */
  946. pmp = (state & EM_MSG_LED_PMP_SLOT) >> 8;
  947. if (pmp < EM_MAX_SLOTS)
  948. emp = &pp->em_priv[pmp];
  949. else
  950. return -EINVAL;
  951. ahci_rpm_get_port(ap);
  952. spin_lock_irqsave(ap->lock, flags);
  953. /*
  954. * if we are still busy transmitting a previous message,
  955. * do not allow
  956. */
  957. em_ctl = readl(mmio + HOST_EM_CTL);
  958. if (em_ctl & EM_CTL_TM) {
  959. spin_unlock_irqrestore(ap->lock, flags);
  960. ahci_rpm_put_port(ap);
  961. return -EBUSY;
  962. }
  963. if (hpriv->em_msg_type & EM_MSG_TYPE_LED) {
  964. /*
  965. * create message header - this is all zero except for
  966. * the message size, which is 4 bytes.
  967. */
  968. message[0] |= (4 << 8);
  969. /* ignore 0:4 of byte zero, fill in port info yourself */
  970. message[1] = ((state & ~EM_MSG_LED_HBA_PORT) | ap->port_no);
  971. /* write message to EM_LOC */
  972. writel(message[0], mmio + hpriv->em_loc);
  973. writel(message[1], mmio + hpriv->em_loc+4);
  974. /*
  975. * tell hardware to transmit the message
  976. */
  977. writel(em_ctl | EM_CTL_TM, mmio + HOST_EM_CTL);
  978. }
  979. /* save off new led state for port/slot */
  980. emp->led_state = state;
  981. spin_unlock_irqrestore(ap->lock, flags);
  982. ahci_rpm_put_port(ap);
  983. return size;
  984. }
  985. static ssize_t ahci_led_show(struct ata_port *ap, char *buf)
  986. {
  987. struct ahci_port_priv *pp = ap->private_data;
  988. struct ata_link *link;
  989. struct ahci_em_priv *emp;
  990. int rc = 0;
  991. ata_for_each_link(link, ap, EDGE) {
  992. emp = &pp->em_priv[link->pmp];
  993. rc += sprintf(buf, "%lx\n", emp->led_state);
  994. }
  995. return rc;
  996. }
  997. static ssize_t ahci_led_store(struct ata_port *ap, const char *buf,
  998. size_t size)
  999. {
  1000. unsigned int state;
  1001. int pmp;
  1002. struct ahci_port_priv *pp = ap->private_data;
  1003. struct ahci_em_priv *emp;
  1004. if (kstrtouint(buf, 0, &state) < 0)
  1005. return -EINVAL;
  1006. /* get the slot number from the message */
  1007. pmp = (state & EM_MSG_LED_PMP_SLOT) >> 8;
  1008. if (pmp < EM_MAX_SLOTS) {
  1009. pmp = array_index_nospec(pmp, EM_MAX_SLOTS);
  1010. emp = &pp->em_priv[pmp];
  1011. } else {
  1012. return -EINVAL;
  1013. }
  1014. /* mask off the activity bits if we are in sw_activity
  1015. * mode, user should turn off sw_activity before setting
  1016. * activity led through em_message
  1017. */
  1018. if (emp->blink_policy)
  1019. state &= ~EM_MSG_LED_VALUE_ACTIVITY;
  1020. return ap->ops->transmit_led_message(ap, state, size);
  1021. }
  1022. static ssize_t ahci_activity_store(struct ata_device *dev, enum sw_activity val)
  1023. {
  1024. struct ata_link *link = dev->link;
  1025. struct ata_port *ap = link->ap;
  1026. struct ahci_port_priv *pp = ap->private_data;
  1027. struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
  1028. u32 port_led_state = emp->led_state;
  1029. /* save the desired Activity LED behavior */
  1030. if (val == OFF) {
  1031. /* clear LFLAG */
  1032. link->flags &= ~(ATA_LFLAG_SW_ACTIVITY);
  1033. /* set the LED to OFF */
  1034. port_led_state &= EM_MSG_LED_VALUE_OFF;
  1035. port_led_state |= (ap->port_no | (link->pmp << 8));
  1036. ap->ops->transmit_led_message(ap, port_led_state, 4);
  1037. } else {
  1038. link->flags |= ATA_LFLAG_SW_ACTIVITY;
  1039. if (val == BLINK_OFF) {
  1040. /* set LED to ON for idle */
  1041. port_led_state &= EM_MSG_LED_VALUE_OFF;
  1042. port_led_state |= (ap->port_no | (link->pmp << 8));
  1043. port_led_state |= EM_MSG_LED_VALUE_ON; /* check this */
  1044. ap->ops->transmit_led_message(ap, port_led_state, 4);
  1045. }
  1046. }
  1047. emp->blink_policy = val;
  1048. return 0;
  1049. }
  1050. static ssize_t ahci_activity_show(struct ata_device *dev, char *buf)
  1051. {
  1052. struct ata_link *link = dev->link;
  1053. struct ata_port *ap = link->ap;
  1054. struct ahci_port_priv *pp = ap->private_data;
  1055. struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
  1056. /* display the saved value of activity behavior for this
  1057. * disk.
  1058. */
  1059. return sprintf(buf, "%d\n", emp->blink_policy);
  1060. }
  1061. static void ahci_port_clear_pending_irq(struct ata_port *ap)
  1062. {
  1063. struct ahci_host_priv *hpriv = ap->host->private_data;
  1064. void __iomem *port_mmio = ahci_port_base(ap);
  1065. u32 tmp;
  1066. /* clear SError */
  1067. tmp = readl(port_mmio + PORT_SCR_ERR);
  1068. dev_dbg(ap->host->dev, "PORT_SCR_ERR 0x%x\n", tmp);
  1069. writel(tmp, port_mmio + PORT_SCR_ERR);
  1070. /* clear port IRQ */
  1071. tmp = readl(port_mmio + PORT_IRQ_STAT);
  1072. dev_dbg(ap->host->dev, "PORT_IRQ_STAT 0x%x\n", tmp);
  1073. if (tmp)
  1074. writel(tmp, port_mmio + PORT_IRQ_STAT);
  1075. writel(1 << ap->port_no, hpriv->mmio + HOST_IRQ_STAT);
  1076. }
  1077. static void ahci_port_init(struct device *dev, struct ata_port *ap,
  1078. int port_no, void __iomem *mmio,
  1079. void __iomem *port_mmio)
  1080. {
  1081. struct ahci_host_priv *hpriv = ap->host->private_data;
  1082. const char *emsg = NULL;
  1083. int rc;
  1084. u32 tmp;
  1085. /* make sure port is not active */
  1086. rc = ahci_deinit_port(ap, &emsg);
  1087. if (rc)
  1088. dev_warn(dev, "%s (%d)\n", emsg, rc);
  1089. ahci_port_clear_pending_irq(ap);
  1090. /* mark esata ports */
  1091. tmp = readl(port_mmio + PORT_CMD);
  1092. if ((tmp & PORT_CMD_ESP) && (hpriv->cap & HOST_CAP_SXS))
  1093. ap->pflags |= ATA_PFLAG_EXTERNAL;
  1094. }
  1095. void ahci_init_controller(struct ata_host *host)
  1096. {
  1097. struct ahci_host_priv *hpriv = host->private_data;
  1098. void __iomem *mmio = hpriv->mmio;
  1099. int i;
  1100. void __iomem *port_mmio;
  1101. u32 tmp;
  1102. for (i = 0; i < host->n_ports; i++) {
  1103. struct ata_port *ap = host->ports[i];
  1104. port_mmio = ahci_port_base(ap);
  1105. if (ata_port_is_dummy(ap))
  1106. continue;
  1107. ahci_port_init(host->dev, ap, i, mmio, port_mmio);
  1108. }
  1109. tmp = readl(mmio + HOST_CTL);
  1110. dev_dbg(host->dev, "HOST_CTL 0x%x\n", tmp);
  1111. writel(tmp | HOST_IRQ_EN, mmio + HOST_CTL);
  1112. tmp = readl(mmio + HOST_CTL);
  1113. dev_dbg(host->dev, "HOST_CTL 0x%x\n", tmp);
  1114. }
  1115. EXPORT_SYMBOL_GPL(ahci_init_controller);
  1116. static void ahci_dev_config(struct ata_device *dev)
  1117. {
  1118. struct ahci_host_priv *hpriv = dev->link->ap->host->private_data;
  1119. if (hpriv->flags & AHCI_HFLAG_SECT255) {
  1120. dev->max_sectors = 255;
  1121. ata_dev_info(dev,
  1122. "SB600 AHCI: limiting to 255 sectors per cmd\n");
  1123. }
  1124. }
  1125. unsigned int ahci_dev_classify(struct ata_port *ap)
  1126. {
  1127. void __iomem *port_mmio = ahci_port_base(ap);
  1128. struct ata_taskfile tf;
  1129. u32 tmp;
  1130. tmp = readl(port_mmio + PORT_SIG);
  1131. tf.lbah = (tmp >> 24) & 0xff;
  1132. tf.lbam = (tmp >> 16) & 0xff;
  1133. tf.lbal = (tmp >> 8) & 0xff;
  1134. tf.nsect = (tmp) & 0xff;
  1135. return ata_port_classify(ap, &tf);
  1136. }
  1137. EXPORT_SYMBOL_GPL(ahci_dev_classify);
  1138. void ahci_fill_cmd_slot(struct ahci_port_priv *pp, unsigned int tag,
  1139. u32 opts)
  1140. {
  1141. dma_addr_t cmd_tbl_dma;
  1142. cmd_tbl_dma = pp->cmd_tbl_dma + tag * AHCI_CMD_TBL_SZ;
  1143. pp->cmd_slot[tag].opts = cpu_to_le32(opts);
  1144. pp->cmd_slot[tag].status = 0;
  1145. pp->cmd_slot[tag].tbl_addr = cpu_to_le32(cmd_tbl_dma & 0xffffffff);
  1146. pp->cmd_slot[tag].tbl_addr_hi = cpu_to_le32((cmd_tbl_dma >> 16) >> 16);
  1147. }
  1148. EXPORT_SYMBOL_GPL(ahci_fill_cmd_slot);
  1149. int ahci_kick_engine(struct ata_port *ap)
  1150. {
  1151. void __iomem *port_mmio = ahci_port_base(ap);
  1152. struct ahci_host_priv *hpriv = ap->host->private_data;
  1153. u8 status = readl(port_mmio + PORT_TFDATA) & 0xFF;
  1154. u32 tmp;
  1155. int busy, rc;
  1156. /* stop engine */
  1157. rc = hpriv->stop_engine(ap);
  1158. if (rc)
  1159. goto out_restart;
  1160. /* need to do CLO?
  1161. * always do CLO if PMP is attached (AHCI-1.3 9.2)
  1162. */
  1163. busy = status & (ATA_BUSY | ATA_DRQ);
  1164. if (!busy && !sata_pmp_attached(ap)) {
  1165. rc = 0;
  1166. goto out_restart;
  1167. }
  1168. if (!(hpriv->cap & HOST_CAP_CLO)) {
  1169. rc = -EOPNOTSUPP;
  1170. goto out_restart;
  1171. }
  1172. /* perform CLO */
  1173. tmp = readl(port_mmio + PORT_CMD);
  1174. tmp |= PORT_CMD_CLO;
  1175. writel(tmp, port_mmio + PORT_CMD);
  1176. rc = 0;
  1177. tmp = ata_wait_register(ap, port_mmio + PORT_CMD,
  1178. PORT_CMD_CLO, PORT_CMD_CLO, 1, 500);
  1179. if (tmp & PORT_CMD_CLO)
  1180. rc = -EIO;
  1181. /* restart engine */
  1182. out_restart:
  1183. hpriv->start_engine(ap);
  1184. return rc;
  1185. }
  1186. EXPORT_SYMBOL_GPL(ahci_kick_engine);
  1187. static int ahci_exec_polled_cmd(struct ata_port *ap, int pmp,
  1188. struct ata_taskfile *tf, int is_cmd, u16 flags,
  1189. unsigned long timeout_msec)
  1190. {
  1191. const u32 cmd_fis_len = 5; /* five dwords */
  1192. struct ahci_port_priv *pp = ap->private_data;
  1193. void __iomem *port_mmio = ahci_port_base(ap);
  1194. u8 *fis = pp->cmd_tbl;
  1195. u32 tmp;
  1196. /* prep the command */
  1197. ata_tf_to_fis(tf, pmp, is_cmd, fis);
  1198. ahci_fill_cmd_slot(pp, 0, cmd_fis_len | flags | (pmp << 12));
  1199. /* set port value for softreset of Port Multiplier */
  1200. if (pp->fbs_enabled && pp->fbs_last_dev != pmp) {
  1201. tmp = readl(port_mmio + PORT_FBS);
  1202. tmp &= ~(PORT_FBS_DEV_MASK | PORT_FBS_DEC);
  1203. tmp |= pmp << PORT_FBS_DEV_OFFSET;
  1204. writel(tmp, port_mmio + PORT_FBS);
  1205. pp->fbs_last_dev = pmp;
  1206. }
  1207. /* issue & wait */
  1208. writel(1, port_mmio + PORT_CMD_ISSUE);
  1209. if (timeout_msec) {
  1210. tmp = ata_wait_register(ap, port_mmio + PORT_CMD_ISSUE,
  1211. 0x1, 0x1, 1, timeout_msec);
  1212. if (tmp & 0x1) {
  1213. ahci_kick_engine(ap);
  1214. return -EBUSY;
  1215. }
  1216. } else
  1217. readl(port_mmio + PORT_CMD_ISSUE); /* flush */
  1218. return 0;
  1219. }
  1220. int ahci_do_softreset(struct ata_link *link, unsigned int *class,
  1221. int pmp, unsigned long deadline,
  1222. int (*check_ready)(struct ata_link *link))
  1223. {
  1224. struct ata_port *ap = link->ap;
  1225. struct ahci_host_priv *hpriv = ap->host->private_data;
  1226. struct ahci_port_priv *pp = ap->private_data;
  1227. const char *reason = NULL;
  1228. unsigned long now, msecs;
  1229. struct ata_taskfile tf;
  1230. bool fbs_disabled = false;
  1231. int rc;
  1232. /* prepare for SRST (AHCI-1.1 10.4.1) */
  1233. rc = ahci_kick_engine(ap);
  1234. if (rc && rc != -EOPNOTSUPP)
  1235. ata_link_warn(link, "failed to reset engine (errno=%d)\n", rc);
  1236. /*
  1237. * According to AHCI-1.2 9.3.9: if FBS is enable, software shall
  1238. * clear PxFBS.EN to '0' prior to issuing software reset to devices
  1239. * that is attached to port multiplier.
  1240. */
  1241. if (!ata_is_host_link(link) && pp->fbs_enabled) {
  1242. ahci_disable_fbs(ap);
  1243. fbs_disabled = true;
  1244. }
  1245. ata_tf_init(link->device, &tf);
  1246. /* issue the first H2D Register FIS */
  1247. msecs = 0;
  1248. now = jiffies;
  1249. if (time_after(deadline, now))
  1250. msecs = jiffies_to_msecs(deadline - now);
  1251. tf.ctl |= ATA_SRST;
  1252. if (ahci_exec_polled_cmd(ap, pmp, &tf, 0,
  1253. AHCI_CMD_RESET | AHCI_CMD_CLR_BUSY, msecs)) {
  1254. rc = -EIO;
  1255. reason = "1st FIS failed";
  1256. goto fail;
  1257. }
  1258. /* spec says at least 5us, but be generous and sleep for 1ms */
  1259. ata_msleep(ap, 1);
  1260. /* issue the second H2D Register FIS */
  1261. tf.ctl &= ~ATA_SRST;
  1262. ahci_exec_polled_cmd(ap, pmp, &tf, 0, 0, 0);
  1263. /* wait for link to become ready */
  1264. rc = ata_wait_after_reset(link, deadline, check_ready);
  1265. if (rc == -EBUSY && hpriv->flags & AHCI_HFLAG_SRST_TOUT_IS_OFFLINE) {
  1266. /*
  1267. * Workaround for cases where link online status can't
  1268. * be trusted. Treat device readiness timeout as link
  1269. * offline.
  1270. */
  1271. ata_link_info(link, "device not ready, treating as offline\n");
  1272. *class = ATA_DEV_NONE;
  1273. } else if (rc) {
  1274. /* link occupied, -ENODEV too is an error */
  1275. reason = "device not ready";
  1276. goto fail;
  1277. } else
  1278. *class = ahci_dev_classify(ap);
  1279. /* re-enable FBS if disabled before */
  1280. if (fbs_disabled)
  1281. ahci_enable_fbs(ap);
  1282. return 0;
  1283. fail:
  1284. ata_link_err(link, "softreset failed (%s)\n", reason);
  1285. return rc;
  1286. }
  1287. int ahci_check_ready(struct ata_link *link)
  1288. {
  1289. void __iomem *port_mmio = ahci_port_base(link->ap);
  1290. u8 status = readl(port_mmio + PORT_TFDATA) & 0xFF;
  1291. return ata_check_ready(status);
  1292. }
  1293. EXPORT_SYMBOL_GPL(ahci_check_ready);
  1294. static int ahci_softreset(struct ata_link *link, unsigned int *class,
  1295. unsigned long deadline)
  1296. {
  1297. int pmp = sata_srst_pmp(link);
  1298. return ahci_do_softreset(link, class, pmp, deadline, ahci_check_ready);
  1299. }
  1300. EXPORT_SYMBOL_GPL(ahci_do_softreset);
  1301. static int ahci_bad_pmp_check_ready(struct ata_link *link)
  1302. {
  1303. void __iomem *port_mmio = ahci_port_base(link->ap);
  1304. u8 status = readl(port_mmio + PORT_TFDATA) & 0xFF;
  1305. u32 irq_status = readl(port_mmio + PORT_IRQ_STAT);
  1306. /*
  1307. * There is no need to check TFDATA if BAD PMP is found due to HW bug,
  1308. * which can save timeout delay.
  1309. */
  1310. if (irq_status & PORT_IRQ_BAD_PMP)
  1311. return -EIO;
  1312. return ata_check_ready(status);
  1313. }
  1314. static int ahci_pmp_retry_softreset(struct ata_link *link, unsigned int *class,
  1315. unsigned long deadline)
  1316. {
  1317. struct ata_port *ap = link->ap;
  1318. void __iomem *port_mmio = ahci_port_base(ap);
  1319. int pmp = sata_srst_pmp(link);
  1320. int rc;
  1321. u32 irq_sts;
  1322. rc = ahci_do_softreset(link, class, pmp, deadline,
  1323. ahci_bad_pmp_check_ready);
  1324. /*
  1325. * Soft reset fails with IPMS set when PMP is enabled but
  1326. * SATA HDD/ODD is connected to SATA port, do soft reset
  1327. * again to port 0.
  1328. */
  1329. if (rc == -EIO) {
  1330. irq_sts = readl(port_mmio + PORT_IRQ_STAT);
  1331. if (irq_sts & PORT_IRQ_BAD_PMP) {
  1332. ata_link_warn(link,
  1333. "applying PMP SRST workaround "
  1334. "and retrying\n");
  1335. rc = ahci_do_softreset(link, class, 0, deadline,
  1336. ahci_check_ready);
  1337. }
  1338. }
  1339. return rc;
  1340. }
  1341. int ahci_do_hardreset(struct ata_link *link, unsigned int *class,
  1342. unsigned long deadline, bool *online)
  1343. {
  1344. const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
  1345. struct ata_port *ap = link->ap;
  1346. struct ahci_port_priv *pp = ap->private_data;
  1347. struct ahci_host_priv *hpriv = ap->host->private_data;
  1348. u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
  1349. struct ata_taskfile tf;
  1350. int rc;
  1351. hpriv->stop_engine(ap);
  1352. /* clear D2H reception area to properly wait for D2H FIS */
  1353. ata_tf_init(link->device, &tf);
  1354. tf.status = ATA_BUSY;
  1355. ata_tf_to_fis(&tf, 0, 0, d2h_fis);
  1356. ahci_port_clear_pending_irq(ap);
  1357. rc = sata_link_hardreset(link, timing, deadline, online,
  1358. ahci_check_ready);
  1359. hpriv->start_engine(ap);
  1360. if (*online)
  1361. *class = ahci_dev_classify(ap);
  1362. return rc;
  1363. }
  1364. EXPORT_SYMBOL_GPL(ahci_do_hardreset);
  1365. static int ahci_hardreset(struct ata_link *link, unsigned int *class,
  1366. unsigned long deadline)
  1367. {
  1368. bool online;
  1369. return ahci_do_hardreset(link, class, deadline, &online);
  1370. }
  1371. static void ahci_postreset(struct ata_link *link, unsigned int *class)
  1372. {
  1373. struct ata_port *ap = link->ap;
  1374. void __iomem *port_mmio = ahci_port_base(ap);
  1375. u32 new_tmp, tmp;
  1376. ata_std_postreset(link, class);
  1377. /* Make sure port's ATAPI bit is set appropriately */
  1378. new_tmp = tmp = readl(port_mmio + PORT_CMD);
  1379. if (*class == ATA_DEV_ATAPI)
  1380. new_tmp |= PORT_CMD_ATAPI;
  1381. else
  1382. new_tmp &= ~PORT_CMD_ATAPI;
  1383. if (new_tmp != tmp) {
  1384. writel(new_tmp, port_mmio + PORT_CMD);
  1385. readl(port_mmio + PORT_CMD); /* flush */
  1386. }
  1387. }
  1388. static unsigned int ahci_fill_sg(struct ata_queued_cmd *qc, void *cmd_tbl)
  1389. {
  1390. struct scatterlist *sg;
  1391. struct ahci_sg *ahci_sg = cmd_tbl + AHCI_CMD_TBL_HDR_SZ;
  1392. unsigned int si;
  1393. /*
  1394. * Next, the S/G list.
  1395. */
  1396. for_each_sg(qc->sg, sg, qc->n_elem, si) {
  1397. dma_addr_t addr = sg_dma_address(sg);
  1398. u32 sg_len = sg_dma_len(sg);
  1399. ahci_sg[si].addr = cpu_to_le32(addr & 0xffffffff);
  1400. ahci_sg[si].addr_hi = cpu_to_le32((addr >> 16) >> 16);
  1401. ahci_sg[si].flags_size = cpu_to_le32(sg_len - 1);
  1402. }
  1403. return si;
  1404. }
  1405. static int ahci_pmp_qc_defer(struct ata_queued_cmd *qc)
  1406. {
  1407. struct ata_port *ap = qc->ap;
  1408. struct ahci_port_priv *pp = ap->private_data;
  1409. if (!sata_pmp_attached(ap) || pp->fbs_enabled)
  1410. return ata_std_qc_defer(qc);
  1411. else
  1412. return sata_pmp_qc_defer_cmd_switch(qc);
  1413. }
  1414. static enum ata_completion_errors ahci_qc_prep(struct ata_queued_cmd *qc)
  1415. {
  1416. struct ata_port *ap = qc->ap;
  1417. struct ahci_port_priv *pp = ap->private_data;
  1418. int is_atapi = ata_is_atapi(qc->tf.protocol);
  1419. void *cmd_tbl;
  1420. u32 opts;
  1421. const u32 cmd_fis_len = 5; /* five dwords */
  1422. unsigned int n_elem;
  1423. /*
  1424. * Fill in command table information. First, the header,
  1425. * a SATA Register - Host to Device command FIS.
  1426. */
  1427. cmd_tbl = pp->cmd_tbl + qc->hw_tag * AHCI_CMD_TBL_SZ;
  1428. ata_tf_to_fis(&qc->tf, qc->dev->link->pmp, 1, cmd_tbl);
  1429. if (is_atapi) {
  1430. memset(cmd_tbl + AHCI_CMD_TBL_CDB, 0, 32);
  1431. memcpy(cmd_tbl + AHCI_CMD_TBL_CDB, qc->cdb, qc->dev->cdb_len);
  1432. }
  1433. n_elem = 0;
  1434. if (qc->flags & ATA_QCFLAG_DMAMAP)
  1435. n_elem = ahci_fill_sg(qc, cmd_tbl);
  1436. /*
  1437. * Fill in command slot information.
  1438. */
  1439. opts = cmd_fis_len | n_elem << 16 | (qc->dev->link->pmp << 12);
  1440. if (qc->tf.flags & ATA_TFLAG_WRITE)
  1441. opts |= AHCI_CMD_WRITE;
  1442. if (is_atapi)
  1443. opts |= AHCI_CMD_ATAPI | AHCI_CMD_PREFETCH;
  1444. ahci_fill_cmd_slot(pp, qc->hw_tag, opts);
  1445. return AC_ERR_OK;
  1446. }
  1447. static void ahci_fbs_dec_intr(struct ata_port *ap)
  1448. {
  1449. struct ahci_port_priv *pp = ap->private_data;
  1450. void __iomem *port_mmio = ahci_port_base(ap);
  1451. u32 fbs = readl(port_mmio + PORT_FBS);
  1452. int retries = 3;
  1453. BUG_ON(!pp->fbs_enabled);
  1454. /* time to wait for DEC is not specified by AHCI spec,
  1455. * add a retry loop for safety.
  1456. */
  1457. writel(fbs | PORT_FBS_DEC, port_mmio + PORT_FBS);
  1458. fbs = readl(port_mmio + PORT_FBS);
  1459. while ((fbs & PORT_FBS_DEC) && retries--) {
  1460. udelay(1);
  1461. fbs = readl(port_mmio + PORT_FBS);
  1462. }
  1463. if (fbs & PORT_FBS_DEC)
  1464. dev_err(ap->host->dev, "failed to clear device error\n");
  1465. }
  1466. static void ahci_error_intr(struct ata_port *ap, u32 irq_stat)
  1467. {
  1468. struct ahci_host_priv *hpriv = ap->host->private_data;
  1469. struct ahci_port_priv *pp = ap->private_data;
  1470. struct ata_eh_info *host_ehi = &ap->link.eh_info;
  1471. struct ata_link *link = NULL;
  1472. struct ata_queued_cmd *active_qc;
  1473. struct ata_eh_info *active_ehi;
  1474. bool fbs_need_dec = false;
  1475. u32 serror;
  1476. /* determine active link with error */
  1477. if (pp->fbs_enabled) {
  1478. void __iomem *port_mmio = ahci_port_base(ap);
  1479. u32 fbs = readl(port_mmio + PORT_FBS);
  1480. int pmp = fbs >> PORT_FBS_DWE_OFFSET;
  1481. if ((fbs & PORT_FBS_SDE) && (pmp < ap->nr_pmp_links)) {
  1482. link = &ap->pmp_link[pmp];
  1483. fbs_need_dec = true;
  1484. }
  1485. } else
  1486. ata_for_each_link(link, ap, EDGE)
  1487. if (ata_link_active(link))
  1488. break;
  1489. if (!link)
  1490. link = &ap->link;
  1491. active_qc = ata_qc_from_tag(ap, link->active_tag);
  1492. active_ehi = &link->eh_info;
  1493. /* record irq stat */
  1494. ata_ehi_clear_desc(host_ehi);
  1495. ata_ehi_push_desc(host_ehi, "irq_stat 0x%08x", irq_stat);
  1496. /* AHCI needs SError cleared; otherwise, it might lock up */
  1497. ahci_scr_read(&ap->link, SCR_ERROR, &serror);
  1498. ahci_scr_write(&ap->link, SCR_ERROR, serror);
  1499. host_ehi->serror |= serror;
  1500. /* some controllers set IRQ_IF_ERR on device errors, ignore it */
  1501. if (hpriv->flags & AHCI_HFLAG_IGN_IRQ_IF_ERR)
  1502. irq_stat &= ~PORT_IRQ_IF_ERR;
  1503. if (irq_stat & PORT_IRQ_TF_ERR) {
  1504. /* If qc is active, charge it; otherwise, the active
  1505. * link. There's no active qc on NCQ errors. It will
  1506. * be determined by EH by reading log page 10h.
  1507. */
  1508. if (active_qc)
  1509. active_qc->err_mask |= AC_ERR_DEV;
  1510. else
  1511. active_ehi->err_mask |= AC_ERR_DEV;
  1512. if (hpriv->flags & AHCI_HFLAG_IGN_SERR_INTERNAL)
  1513. host_ehi->serror &= ~SERR_INTERNAL;
  1514. }
  1515. if (irq_stat & PORT_IRQ_UNK_FIS) {
  1516. u32 *unk = pp->rx_fis + RX_FIS_UNK;
  1517. active_ehi->err_mask |= AC_ERR_HSM;
  1518. active_ehi->action |= ATA_EH_RESET;
  1519. ata_ehi_push_desc(active_ehi,
  1520. "unknown FIS %08x %08x %08x %08x" ,
  1521. unk[0], unk[1], unk[2], unk[3]);
  1522. }
  1523. if (sata_pmp_attached(ap) && (irq_stat & PORT_IRQ_BAD_PMP)) {
  1524. active_ehi->err_mask |= AC_ERR_HSM;
  1525. active_ehi->action |= ATA_EH_RESET;
  1526. ata_ehi_push_desc(active_ehi, "incorrect PMP");
  1527. }
  1528. if (irq_stat & (PORT_IRQ_HBUS_ERR | PORT_IRQ_HBUS_DATA_ERR)) {
  1529. host_ehi->err_mask |= AC_ERR_HOST_BUS;
  1530. host_ehi->action |= ATA_EH_RESET;
  1531. ata_ehi_push_desc(host_ehi, "host bus error");
  1532. }
  1533. if (irq_stat & PORT_IRQ_IF_ERR) {
  1534. if (fbs_need_dec)
  1535. active_ehi->err_mask |= AC_ERR_DEV;
  1536. else {
  1537. host_ehi->err_mask |= AC_ERR_ATA_BUS;
  1538. host_ehi->action |= ATA_EH_RESET;
  1539. }
  1540. ata_ehi_push_desc(host_ehi, "interface fatal error");
  1541. }
  1542. if (irq_stat & (PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)) {
  1543. ata_ehi_hotplugged(host_ehi);
  1544. ata_ehi_push_desc(host_ehi, "%s",
  1545. irq_stat & PORT_IRQ_CONNECT ?
  1546. "connection status changed" : "PHY RDY changed");
  1547. }
  1548. /* okay, let's hand over to EH */
  1549. if (irq_stat & PORT_IRQ_FREEZE)
  1550. ata_port_freeze(ap);
  1551. else if (fbs_need_dec) {
  1552. ata_link_abort(link);
  1553. ahci_fbs_dec_intr(ap);
  1554. } else
  1555. ata_port_abort(ap);
  1556. }
  1557. static void ahci_handle_port_interrupt(struct ata_port *ap,
  1558. void __iomem *port_mmio, u32 status)
  1559. {
  1560. struct ata_eh_info *ehi = &ap->link.eh_info;
  1561. struct ahci_port_priv *pp = ap->private_data;
  1562. struct ahci_host_priv *hpriv = ap->host->private_data;
  1563. int resetting = !!(ap->pflags & ATA_PFLAG_RESETTING);
  1564. u32 qc_active = 0;
  1565. int rc;
  1566. /* ignore BAD_PMP while resetting */
  1567. if (unlikely(resetting))
  1568. status &= ~PORT_IRQ_BAD_PMP;
  1569. if (sata_lpm_ignore_phy_events(&ap->link)) {
  1570. status &= ~PORT_IRQ_PHYRDY;
  1571. ahci_scr_write(&ap->link, SCR_ERROR, SERR_PHYRDY_CHG);
  1572. }
  1573. if (unlikely(status & PORT_IRQ_ERROR)) {
  1574. ahci_error_intr(ap, status);
  1575. return;
  1576. }
  1577. if (status & PORT_IRQ_SDB_FIS) {
  1578. /* If SNotification is available, leave notification
  1579. * handling to sata_async_notification(). If not,
  1580. * emulate it by snooping SDB FIS RX area.
  1581. *
  1582. * Snooping FIS RX area is probably cheaper than
  1583. * poking SNotification but some constrollers which
  1584. * implement SNotification, ICH9 for example, don't
  1585. * store AN SDB FIS into receive area.
  1586. */
  1587. if (hpriv->cap & HOST_CAP_SNTF)
  1588. sata_async_notification(ap);
  1589. else {
  1590. /* If the 'N' bit in word 0 of the FIS is set,
  1591. * we just received asynchronous notification.
  1592. * Tell libata about it.
  1593. *
  1594. * Lack of SNotification should not appear in
  1595. * ahci 1.2, so the workaround is unnecessary
  1596. * when FBS is enabled.
  1597. */
  1598. if (pp->fbs_enabled)
  1599. WARN_ON_ONCE(1);
  1600. else {
  1601. const __le32 *f = pp->rx_fis + RX_FIS_SDB;
  1602. u32 f0 = le32_to_cpu(f[0]);
  1603. if (f0 & (1 << 15))
  1604. sata_async_notification(ap);
  1605. }
  1606. }
  1607. }
  1608. /* pp->active_link is not reliable once FBS is enabled, both
  1609. * PORT_SCR_ACT and PORT_CMD_ISSUE should be checked because
  1610. * NCQ and non-NCQ commands may be in flight at the same time.
  1611. */
  1612. if (pp->fbs_enabled) {
  1613. if (ap->qc_active) {
  1614. qc_active = readl(port_mmio + PORT_SCR_ACT);
  1615. qc_active |= readl(port_mmio + PORT_CMD_ISSUE);
  1616. }
  1617. } else {
  1618. /* pp->active_link is valid iff any command is in flight */
  1619. if (ap->qc_active && pp->active_link->sactive)
  1620. qc_active = readl(port_mmio + PORT_SCR_ACT);
  1621. else
  1622. qc_active = readl(port_mmio + PORT_CMD_ISSUE);
  1623. }
  1624. rc = ata_qc_complete_multiple(ap, qc_active);
  1625. /* while resetting, invalid completions are expected */
  1626. if (unlikely(rc < 0 && !resetting)) {
  1627. ehi->err_mask |= AC_ERR_HSM;
  1628. ehi->action |= ATA_EH_RESET;
  1629. ata_port_freeze(ap);
  1630. }
  1631. }
  1632. static void ahci_port_intr(struct ata_port *ap)
  1633. {
  1634. void __iomem *port_mmio = ahci_port_base(ap);
  1635. u32 status;
  1636. status = readl(port_mmio + PORT_IRQ_STAT);
  1637. writel(status, port_mmio + PORT_IRQ_STAT);
  1638. ahci_handle_port_interrupt(ap, port_mmio, status);
  1639. }
  1640. static irqreturn_t ahci_multi_irqs_intr_hard(int irq, void *dev_instance)
  1641. {
  1642. struct ata_port *ap = dev_instance;
  1643. void __iomem *port_mmio = ahci_port_base(ap);
  1644. u32 status;
  1645. status = readl(port_mmio + PORT_IRQ_STAT);
  1646. writel(status, port_mmio + PORT_IRQ_STAT);
  1647. spin_lock(ap->lock);
  1648. ahci_handle_port_interrupt(ap, port_mmio, status);
  1649. spin_unlock(ap->lock);
  1650. return IRQ_HANDLED;
  1651. }
  1652. u32 ahci_handle_port_intr(struct ata_host *host, u32 irq_masked)
  1653. {
  1654. unsigned int i, handled = 0;
  1655. for (i = 0; i < host->n_ports; i++) {
  1656. struct ata_port *ap;
  1657. if (!(irq_masked & (1 << i)))
  1658. continue;
  1659. ap = host->ports[i];
  1660. if (ap) {
  1661. ahci_port_intr(ap);
  1662. } else {
  1663. if (ata_ratelimit())
  1664. dev_warn(host->dev,
  1665. "interrupt on disabled port %u\n", i);
  1666. }
  1667. handled = 1;
  1668. }
  1669. return handled;
  1670. }
  1671. EXPORT_SYMBOL_GPL(ahci_handle_port_intr);
  1672. static irqreturn_t ahci_single_level_irq_intr(int irq, void *dev_instance)
  1673. {
  1674. struct ata_host *host = dev_instance;
  1675. struct ahci_host_priv *hpriv;
  1676. unsigned int rc = 0;
  1677. void __iomem *mmio;
  1678. u32 irq_stat, irq_masked;
  1679. hpriv = host->private_data;
  1680. mmio = hpriv->mmio;
  1681. /* sigh. 0xffffffff is a valid return from h/w */
  1682. irq_stat = readl(mmio + HOST_IRQ_STAT);
  1683. if (!irq_stat)
  1684. return IRQ_NONE;
  1685. irq_masked = irq_stat & hpriv->port_map;
  1686. spin_lock(&host->lock);
  1687. rc = ahci_handle_port_intr(host, irq_masked);
  1688. /* HOST_IRQ_STAT behaves as level triggered latch meaning that
  1689. * it should be cleared after all the port events are cleared;
  1690. * otherwise, it will raise a spurious interrupt after each
  1691. * valid one. Please read section 10.6.2 of ahci 1.1 for more
  1692. * information.
  1693. *
  1694. * Also, use the unmasked value to clear interrupt as spurious
  1695. * pending event on a dummy port might cause screaming IRQ.
  1696. */
  1697. writel(irq_stat, mmio + HOST_IRQ_STAT);
  1698. spin_unlock(&host->lock);
  1699. return IRQ_RETVAL(rc);
  1700. }
  1701. unsigned int ahci_qc_issue(struct ata_queued_cmd *qc)
  1702. {
  1703. struct ata_port *ap = qc->ap;
  1704. void __iomem *port_mmio = ahci_port_base(ap);
  1705. struct ahci_port_priv *pp = ap->private_data;
  1706. /* Keep track of the currently active link. It will be used
  1707. * in completion path to determine whether NCQ phase is in
  1708. * progress.
  1709. */
  1710. pp->active_link = qc->dev->link;
  1711. if (ata_is_ncq(qc->tf.protocol))
  1712. writel(1 << qc->hw_tag, port_mmio + PORT_SCR_ACT);
  1713. if (pp->fbs_enabled && pp->fbs_last_dev != qc->dev->link->pmp) {
  1714. u32 fbs = readl(port_mmio + PORT_FBS);
  1715. fbs &= ~(PORT_FBS_DEV_MASK | PORT_FBS_DEC);
  1716. fbs |= qc->dev->link->pmp << PORT_FBS_DEV_OFFSET;
  1717. writel(fbs, port_mmio + PORT_FBS);
  1718. pp->fbs_last_dev = qc->dev->link->pmp;
  1719. }
  1720. writel(1 << qc->hw_tag, port_mmio + PORT_CMD_ISSUE);
  1721. ahci_sw_activity(qc->dev->link);
  1722. return 0;
  1723. }
  1724. EXPORT_SYMBOL_GPL(ahci_qc_issue);
  1725. static bool ahci_qc_fill_rtf(struct ata_queued_cmd *qc)
  1726. {
  1727. struct ahci_port_priv *pp = qc->ap->private_data;
  1728. u8 *rx_fis = pp->rx_fis;
  1729. if (pp->fbs_enabled)
  1730. rx_fis += qc->dev->link->pmp * AHCI_RX_FIS_SZ;
  1731. /*
  1732. * After a successful execution of an ATA PIO data-in command,
  1733. * the device doesn't send D2H Reg FIS to update the TF and
  1734. * the host should take TF and E_Status from the preceding PIO
  1735. * Setup FIS.
  1736. */
  1737. if (qc->tf.protocol == ATA_PROT_PIO && qc->dma_dir == DMA_FROM_DEVICE &&
  1738. !(qc->flags & ATA_QCFLAG_FAILED)) {
  1739. ata_tf_from_fis(rx_fis + RX_FIS_PIO_SETUP, &qc->result_tf);
  1740. qc->result_tf.status = (rx_fis + RX_FIS_PIO_SETUP)[15];
  1741. } else
  1742. ata_tf_from_fis(rx_fis + RX_FIS_D2H_REG, &qc->result_tf);
  1743. return true;
  1744. }
  1745. static void ahci_freeze(struct ata_port *ap)
  1746. {
  1747. void __iomem *port_mmio = ahci_port_base(ap);
  1748. /* turn IRQ off */
  1749. writel(0, port_mmio + PORT_IRQ_MASK);
  1750. }
  1751. static void ahci_thaw(struct ata_port *ap)
  1752. {
  1753. struct ahci_host_priv *hpriv = ap->host->private_data;
  1754. void __iomem *mmio = hpriv->mmio;
  1755. void __iomem *port_mmio = ahci_port_base(ap);
  1756. u32 tmp;
  1757. struct ahci_port_priv *pp = ap->private_data;
  1758. /* clear IRQ */
  1759. tmp = readl(port_mmio + PORT_IRQ_STAT);
  1760. writel(tmp, port_mmio + PORT_IRQ_STAT);
  1761. writel(1 << ap->port_no, mmio + HOST_IRQ_STAT);
  1762. /* turn IRQ back on */
  1763. writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
  1764. }
  1765. void ahci_error_handler(struct ata_port *ap)
  1766. {
  1767. struct ahci_host_priv *hpriv = ap->host->private_data;
  1768. if (!(ap->pflags & ATA_PFLAG_FROZEN)) {
  1769. /* restart engine */
  1770. hpriv->stop_engine(ap);
  1771. hpriv->start_engine(ap);
  1772. }
  1773. sata_pmp_error_handler(ap);
  1774. if (!ata_dev_enabled(ap->link.device))
  1775. hpriv->stop_engine(ap);
  1776. }
  1777. EXPORT_SYMBOL_GPL(ahci_error_handler);
  1778. static void ahci_post_internal_cmd(struct ata_queued_cmd *qc)
  1779. {
  1780. struct ata_port *ap = qc->ap;
  1781. /* make DMA engine forget about the failed command */
  1782. if (qc->flags & ATA_QCFLAG_FAILED)
  1783. ahci_kick_engine(ap);
  1784. }
  1785. static void ahci_set_aggressive_devslp(struct ata_port *ap, bool sleep)
  1786. {
  1787. struct ahci_host_priv *hpriv = ap->host->private_data;
  1788. void __iomem *port_mmio = ahci_port_base(ap);
  1789. struct ata_device *dev = ap->link.device;
  1790. u32 devslp, dm, dito, mdat, deto, dito_conf;
  1791. int rc;
  1792. unsigned int err_mask;
  1793. devslp = readl(port_mmio + PORT_DEVSLP);
  1794. if (!(devslp & PORT_DEVSLP_DSP)) {
  1795. dev_info(ap->host->dev, "port does not support device sleep\n");
  1796. return;
  1797. }
  1798. /* disable device sleep */
  1799. if (!sleep) {
  1800. if (devslp & PORT_DEVSLP_ADSE) {
  1801. writel(devslp & ~PORT_DEVSLP_ADSE,
  1802. port_mmio + PORT_DEVSLP);
  1803. err_mask = ata_dev_set_feature(dev,
  1804. SETFEATURES_SATA_DISABLE,
  1805. SATA_DEVSLP);
  1806. if (err_mask && err_mask != AC_ERR_DEV)
  1807. ata_dev_warn(dev, "failed to disable DEVSLP\n");
  1808. }
  1809. return;
  1810. }
  1811. dm = (devslp & PORT_DEVSLP_DM_MASK) >> PORT_DEVSLP_DM_OFFSET;
  1812. dito = devslp_idle_timeout / (dm + 1);
  1813. if (dito > 0x3ff)
  1814. dito = 0x3ff;
  1815. dito_conf = (devslp >> PORT_DEVSLP_DITO_OFFSET) & 0x3FF;
  1816. /* device sleep was already enabled and same dito */
  1817. if ((devslp & PORT_DEVSLP_ADSE) && (dito_conf == dito))
  1818. return;
  1819. /* set DITO, MDAT, DETO and enable DevSlp, need to stop engine first */
  1820. rc = hpriv->stop_engine(ap);
  1821. if (rc)
  1822. return;
  1823. /* Use the nominal value 10 ms if the read MDAT is zero,
  1824. * the nominal value of DETO is 20 ms.
  1825. */
  1826. if (dev->devslp_timing[ATA_LOG_DEVSLP_VALID] &
  1827. ATA_LOG_DEVSLP_VALID_MASK) {
  1828. mdat = dev->devslp_timing[ATA_LOG_DEVSLP_MDAT] &
  1829. ATA_LOG_DEVSLP_MDAT_MASK;
  1830. if (!mdat)
  1831. mdat = 10;
  1832. deto = dev->devslp_timing[ATA_LOG_DEVSLP_DETO];
  1833. if (!deto)
  1834. deto = 20;
  1835. } else {
  1836. mdat = 10;
  1837. deto = 20;
  1838. }
  1839. /* Make dito, mdat, deto bits to 0s */
  1840. devslp &= ~GENMASK_ULL(24, 2);
  1841. devslp |= ((dito << PORT_DEVSLP_DITO_OFFSET) |
  1842. (mdat << PORT_DEVSLP_MDAT_OFFSET) |
  1843. (deto << PORT_DEVSLP_DETO_OFFSET) |
  1844. PORT_DEVSLP_ADSE);
  1845. writel(devslp, port_mmio + PORT_DEVSLP);
  1846. hpriv->start_engine(ap);
  1847. /* enable device sleep feature for the drive */
  1848. err_mask = ata_dev_set_feature(dev,
  1849. SETFEATURES_SATA_ENABLE,
  1850. SATA_DEVSLP);
  1851. if (err_mask && err_mask != AC_ERR_DEV)
  1852. ata_dev_warn(dev, "failed to enable DEVSLP\n");
  1853. }
  1854. static void ahci_enable_fbs(struct ata_port *ap)
  1855. {
  1856. struct ahci_host_priv *hpriv = ap->host->private_data;
  1857. struct ahci_port_priv *pp = ap->private_data;
  1858. void __iomem *port_mmio = ahci_port_base(ap);
  1859. u32 fbs;
  1860. int rc;
  1861. if (!pp->fbs_supported)
  1862. return;
  1863. fbs = readl(port_mmio + PORT_FBS);
  1864. if (fbs & PORT_FBS_EN) {
  1865. pp->fbs_enabled = true;
  1866. pp->fbs_last_dev = -1; /* initialization */
  1867. return;
  1868. }
  1869. rc = hpriv->stop_engine(ap);
  1870. if (rc)
  1871. return;
  1872. writel(fbs | PORT_FBS_EN, port_mmio + PORT_FBS);
  1873. fbs = readl(port_mmio + PORT_FBS);
  1874. if (fbs & PORT_FBS_EN) {
  1875. dev_info(ap->host->dev, "FBS is enabled\n");
  1876. pp->fbs_enabled = true;
  1877. pp->fbs_last_dev = -1; /* initialization */
  1878. } else
  1879. dev_err(ap->host->dev, "Failed to enable FBS\n");
  1880. hpriv->start_engine(ap);
  1881. }
  1882. static void ahci_disable_fbs(struct ata_port *ap)
  1883. {
  1884. struct ahci_host_priv *hpriv = ap->host->private_data;
  1885. struct ahci_port_priv *pp = ap->private_data;
  1886. void __iomem *port_mmio = ahci_port_base(ap);
  1887. u32 fbs;
  1888. int rc;
  1889. if (!pp->fbs_supported)
  1890. return;
  1891. fbs = readl(port_mmio + PORT_FBS);
  1892. if ((fbs & PORT_FBS_EN) == 0) {
  1893. pp->fbs_enabled = false;
  1894. return;
  1895. }
  1896. rc = hpriv->stop_engine(ap);
  1897. if (rc)
  1898. return;
  1899. writel(fbs & ~PORT_FBS_EN, port_mmio + PORT_FBS);
  1900. fbs = readl(port_mmio + PORT_FBS);
  1901. if (fbs & PORT_FBS_EN)
  1902. dev_err(ap->host->dev, "Failed to disable FBS\n");
  1903. else {
  1904. dev_info(ap->host->dev, "FBS is disabled\n");
  1905. pp->fbs_enabled = false;
  1906. }
  1907. hpriv->start_engine(ap);
  1908. }
  1909. static void ahci_pmp_attach(struct ata_port *ap)
  1910. {
  1911. void __iomem *port_mmio = ahci_port_base(ap);
  1912. struct ahci_port_priv *pp = ap->private_data;
  1913. u32 cmd;
  1914. cmd = readl(port_mmio + PORT_CMD);
  1915. cmd |= PORT_CMD_PMP;
  1916. writel(cmd, port_mmio + PORT_CMD);
  1917. ahci_enable_fbs(ap);
  1918. pp->intr_mask |= PORT_IRQ_BAD_PMP;
  1919. /*
  1920. * We must not change the port interrupt mask register if the
  1921. * port is marked frozen, the value in pp->intr_mask will be
  1922. * restored later when the port is thawed.
  1923. *
  1924. * Note that during initialization, the port is marked as
  1925. * frozen since the irq handler is not yet registered.
  1926. */
  1927. if (!(ap->pflags & ATA_PFLAG_FROZEN))
  1928. writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
  1929. }
  1930. static void ahci_pmp_detach(struct ata_port *ap)
  1931. {
  1932. void __iomem *port_mmio = ahci_port_base(ap);
  1933. struct ahci_port_priv *pp = ap->private_data;
  1934. u32 cmd;
  1935. ahci_disable_fbs(ap);
  1936. cmd = readl(port_mmio + PORT_CMD);
  1937. cmd &= ~PORT_CMD_PMP;
  1938. writel(cmd, port_mmio + PORT_CMD);
  1939. pp->intr_mask &= ~PORT_IRQ_BAD_PMP;
  1940. /* see comment above in ahci_pmp_attach() */
  1941. if (!(ap->pflags & ATA_PFLAG_FROZEN))
  1942. writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
  1943. }
  1944. int ahci_port_resume(struct ata_port *ap)
  1945. {
  1946. ahci_rpm_get_port(ap);
  1947. ahci_power_up(ap);
  1948. ahci_start_port(ap);
  1949. if (sata_pmp_attached(ap))
  1950. ahci_pmp_attach(ap);
  1951. else
  1952. ahci_pmp_detach(ap);
  1953. return 0;
  1954. }
  1955. EXPORT_SYMBOL_GPL(ahci_port_resume);
  1956. #ifdef CONFIG_PM
  1957. static void ahci_handle_s2idle(struct ata_port *ap)
  1958. {
  1959. void __iomem *port_mmio = ahci_port_base(ap);
  1960. u32 devslp;
  1961. if (pm_suspend_via_firmware())
  1962. return;
  1963. devslp = readl(port_mmio + PORT_DEVSLP);
  1964. if ((devslp & PORT_DEVSLP_ADSE))
  1965. ata_msleep(ap, devslp_idle_timeout);
  1966. }
  1967. static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg)
  1968. {
  1969. const char *emsg = NULL;
  1970. int rc;
  1971. rc = ahci_deinit_port(ap, &emsg);
  1972. if (rc == 0)
  1973. ahci_power_down(ap);
  1974. else {
  1975. ata_port_err(ap, "%s (%d)\n", emsg, rc);
  1976. ata_port_freeze(ap);
  1977. }
  1978. if (acpi_storage_d3(ap->host->dev))
  1979. ahci_handle_s2idle(ap);
  1980. ahci_rpm_put_port(ap);
  1981. return rc;
  1982. }
  1983. #endif
  1984. static int ahci_port_start(struct ata_port *ap)
  1985. {
  1986. struct ahci_host_priv *hpriv = ap->host->private_data;
  1987. struct device *dev = ap->host->dev;
  1988. struct ahci_port_priv *pp;
  1989. void *mem;
  1990. dma_addr_t mem_dma;
  1991. size_t dma_sz, rx_fis_sz;
  1992. pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
  1993. if (!pp)
  1994. return -ENOMEM;
  1995. if (ap->host->n_ports > 1) {
  1996. pp->irq_desc = devm_kzalloc(dev, 8, GFP_KERNEL);
  1997. if (!pp->irq_desc) {
  1998. devm_kfree(dev, pp);
  1999. return -ENOMEM;
  2000. }
  2001. snprintf(pp->irq_desc, 8,
  2002. "%s%d", dev_driver_string(dev), ap->port_no);
  2003. }
  2004. /* check FBS capability */
  2005. if ((hpriv->cap & HOST_CAP_FBS) && sata_pmp_supported(ap)) {
  2006. void __iomem *port_mmio = ahci_port_base(ap);
  2007. u32 cmd = readl(port_mmio + PORT_CMD);
  2008. if (cmd & PORT_CMD_FBSCP)
  2009. pp->fbs_supported = true;
  2010. else if (hpriv->flags & AHCI_HFLAG_YES_FBS) {
  2011. dev_info(dev, "port %d can do FBS, forcing FBSCP\n",
  2012. ap->port_no);
  2013. pp->fbs_supported = true;
  2014. } else
  2015. dev_warn(dev, "port %d is not capable of FBS\n",
  2016. ap->port_no);
  2017. }
  2018. if (pp->fbs_supported) {
  2019. dma_sz = AHCI_PORT_PRIV_FBS_DMA_SZ;
  2020. rx_fis_sz = AHCI_RX_FIS_SZ * 16;
  2021. } else {
  2022. dma_sz = AHCI_PORT_PRIV_DMA_SZ;
  2023. rx_fis_sz = AHCI_RX_FIS_SZ;
  2024. }
  2025. mem = dmam_alloc_coherent(dev, dma_sz, &mem_dma, GFP_KERNEL);
  2026. if (!mem)
  2027. return -ENOMEM;
  2028. /*
  2029. * First item in chunk of DMA memory: 32-slot command table,
  2030. * 32 bytes each in size
  2031. */
  2032. pp->cmd_slot = mem;
  2033. pp->cmd_slot_dma = mem_dma;
  2034. mem += AHCI_CMD_SLOT_SZ;
  2035. mem_dma += AHCI_CMD_SLOT_SZ;
  2036. /*
  2037. * Second item: Received-FIS area
  2038. */
  2039. pp->rx_fis = mem;
  2040. pp->rx_fis_dma = mem_dma;
  2041. mem += rx_fis_sz;
  2042. mem_dma += rx_fis_sz;
  2043. /*
  2044. * Third item: data area for storing a single command
  2045. * and its scatter-gather table
  2046. */
  2047. pp->cmd_tbl = mem;
  2048. pp->cmd_tbl_dma = mem_dma;
  2049. /*
  2050. * Save off initial list of interrupts to be enabled.
  2051. * This could be changed later
  2052. */
  2053. pp->intr_mask = DEF_PORT_IRQ;
  2054. /*
  2055. * Switch to per-port locking in case each port has its own MSI vector.
  2056. */
  2057. if (hpriv->flags & AHCI_HFLAG_MULTI_MSI) {
  2058. spin_lock_init(&pp->lock);
  2059. ap->lock = &pp->lock;
  2060. }
  2061. ap->private_data = pp;
  2062. /* engage engines, captain */
  2063. return ahci_port_resume(ap);
  2064. }
  2065. static void ahci_port_stop(struct ata_port *ap)
  2066. {
  2067. const char *emsg = NULL;
  2068. struct ahci_host_priv *hpriv = ap->host->private_data;
  2069. void __iomem *host_mmio = hpriv->mmio;
  2070. int rc;
  2071. /* de-initialize port */
  2072. rc = ahci_deinit_port(ap, &emsg);
  2073. if (rc)
  2074. ata_port_warn(ap, "%s (%d)\n", emsg, rc);
  2075. /*
  2076. * Clear GHC.IS to prevent stuck INTx after disabling MSI and
  2077. * re-enabling INTx.
  2078. */
  2079. writel(1 << ap->port_no, host_mmio + HOST_IRQ_STAT);
  2080. ahci_rpm_put_port(ap);
  2081. }
  2082. void ahci_print_info(struct ata_host *host, const char *scc_s)
  2083. {
  2084. struct ahci_host_priv *hpriv = host->private_data;
  2085. u32 vers, cap, cap2, impl, speed;
  2086. const char *speed_s;
  2087. vers = hpriv->version;
  2088. cap = hpriv->cap;
  2089. cap2 = hpriv->cap2;
  2090. impl = hpriv->port_map;
  2091. speed = (cap >> 20) & 0xf;
  2092. if (speed == 1)
  2093. speed_s = "1.5";
  2094. else if (speed == 2)
  2095. speed_s = "3";
  2096. else if (speed == 3)
  2097. speed_s = "6";
  2098. else
  2099. speed_s = "?";
  2100. dev_info(host->dev,
  2101. "AHCI %02x%02x.%02x%02x "
  2102. "%u slots %u ports %s Gbps 0x%x impl %s mode\n"
  2103. ,
  2104. (vers >> 24) & 0xff,
  2105. (vers >> 16) & 0xff,
  2106. (vers >> 8) & 0xff,
  2107. vers & 0xff,
  2108. ((cap >> 8) & 0x1f) + 1,
  2109. (cap & 0x1f) + 1,
  2110. speed_s,
  2111. impl,
  2112. scc_s);
  2113. dev_info(host->dev,
  2114. "flags: "
  2115. "%s%s%s%s%s%s%s"
  2116. "%s%s%s%s%s%s%s"
  2117. "%s%s%s%s%s%s%s"
  2118. "%s%s\n"
  2119. ,
  2120. cap & HOST_CAP_64 ? "64bit " : "",
  2121. cap & HOST_CAP_NCQ ? "ncq " : "",
  2122. cap & HOST_CAP_SNTF ? "sntf " : "",
  2123. cap & HOST_CAP_MPS ? "ilck " : "",
  2124. cap & HOST_CAP_SSS ? "stag " : "",
  2125. cap & HOST_CAP_ALPM ? "pm " : "",
  2126. cap & HOST_CAP_LED ? "led " : "",
  2127. cap & HOST_CAP_CLO ? "clo " : "",
  2128. cap & HOST_CAP_ONLY ? "only " : "",
  2129. cap & HOST_CAP_PMP ? "pmp " : "",
  2130. cap & HOST_CAP_FBS ? "fbs " : "",
  2131. cap & HOST_CAP_PIO_MULTI ? "pio " : "",
  2132. cap & HOST_CAP_SSC ? "slum " : "",
  2133. cap & HOST_CAP_PART ? "part " : "",
  2134. cap & HOST_CAP_CCC ? "ccc " : "",
  2135. cap & HOST_CAP_EMS ? "ems " : "",
  2136. cap & HOST_CAP_SXS ? "sxs " : "",
  2137. cap2 & HOST_CAP2_DESO ? "deso " : "",
  2138. cap2 & HOST_CAP2_SADM ? "sadm " : "",
  2139. cap2 & HOST_CAP2_SDS ? "sds " : "",
  2140. cap2 & HOST_CAP2_APST ? "apst " : "",
  2141. cap2 & HOST_CAP2_NVMHCI ? "nvmp " : "",
  2142. cap2 & HOST_CAP2_BOH ? "boh " : ""
  2143. );
  2144. }
  2145. EXPORT_SYMBOL_GPL(ahci_print_info);
  2146. void ahci_set_em_messages(struct ahci_host_priv *hpriv,
  2147. struct ata_port_info *pi)
  2148. {
  2149. u8 messages;
  2150. void __iomem *mmio = hpriv->mmio;
  2151. u32 em_loc = readl(mmio + HOST_EM_LOC);
  2152. u32 em_ctl = readl(mmio + HOST_EM_CTL);
  2153. if (!ahci_em_messages || !(hpriv->cap & HOST_CAP_EMS))
  2154. return;
  2155. messages = (em_ctl & EM_CTRL_MSG_TYPE) >> 16;
  2156. if (messages) {
  2157. /* store em_loc */
  2158. hpriv->em_loc = ((em_loc >> 16) * 4);
  2159. hpriv->em_buf_sz = ((em_loc & 0xff) * 4);
  2160. hpriv->em_msg_type = messages;
  2161. pi->flags |= ATA_FLAG_EM;
  2162. if (!(em_ctl & EM_CTL_ALHD))
  2163. pi->flags |= ATA_FLAG_SW_ACTIVITY;
  2164. }
  2165. }
  2166. EXPORT_SYMBOL_GPL(ahci_set_em_messages);
  2167. static int ahci_host_activate_multi_irqs(struct ata_host *host,
  2168. struct scsi_host_template *sht)
  2169. {
  2170. struct ahci_host_priv *hpriv = host->private_data;
  2171. int i, rc;
  2172. rc = ata_host_start(host);
  2173. if (rc)
  2174. return rc;
  2175. /*
  2176. * Requests IRQs according to AHCI-1.1 when multiple MSIs were
  2177. * allocated. That is one MSI per port, starting from @irq.
  2178. */
  2179. for (i = 0; i < host->n_ports; i++) {
  2180. struct ahci_port_priv *pp = host->ports[i]->private_data;
  2181. int irq = hpriv->get_irq_vector(host, i);
  2182. /* Do not receive interrupts sent by dummy ports */
  2183. if (!pp) {
  2184. disable_irq(irq);
  2185. continue;
  2186. }
  2187. rc = devm_request_irq(host->dev, irq, ahci_multi_irqs_intr_hard,
  2188. 0, pp->irq_desc, host->ports[i]);
  2189. if (rc)
  2190. return rc;
  2191. ata_port_desc(host->ports[i], "irq %d", irq);
  2192. }
  2193. return ata_host_register(host, sht);
  2194. }
  2195. /**
  2196. * ahci_host_activate - start AHCI host, request IRQs and register it
  2197. * @host: target ATA host
  2198. * @sht: scsi_host_template to use when registering the host
  2199. *
  2200. * LOCKING:
  2201. * Inherited from calling layer (may sleep).
  2202. *
  2203. * RETURNS:
  2204. * 0 on success, -errno otherwise.
  2205. */
  2206. int ahci_host_activate(struct ata_host *host, struct scsi_host_template *sht)
  2207. {
  2208. struct ahci_host_priv *hpriv = host->private_data;
  2209. int irq = hpriv->irq;
  2210. int rc;
  2211. if (hpriv->flags & AHCI_HFLAG_MULTI_MSI) {
  2212. if (hpriv->irq_handler &&
  2213. hpriv->irq_handler != ahci_single_level_irq_intr)
  2214. dev_warn(host->dev,
  2215. "both AHCI_HFLAG_MULTI_MSI flag set and custom irq handler implemented\n");
  2216. if (!hpriv->get_irq_vector) {
  2217. dev_err(host->dev,
  2218. "AHCI_HFLAG_MULTI_MSI requires ->get_irq_vector!\n");
  2219. return -EIO;
  2220. }
  2221. rc = ahci_host_activate_multi_irqs(host, sht);
  2222. } else {
  2223. rc = ata_host_activate(host, irq, hpriv->irq_handler,
  2224. IRQF_SHARED, sht);
  2225. }
  2226. return rc;
  2227. }
  2228. EXPORT_SYMBOL_GPL(ahci_host_activate);
  2229. MODULE_AUTHOR("Jeff Garzik");
  2230. MODULE_DESCRIPTION("Common AHCI SATA low-level routines");
  2231. MODULE_LICENSE("GPL");