libahci.c 56 KB

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