libahci.c 56 KB

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