libahci.c 66 KB

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