libahci.c 56 KB

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