libahci.c 55 KB

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