libahci.c 60 KB

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