libahci.c 57 KB

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