libahci.c 66 KB

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