libahci.c 56 KB

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