libahci.c 62 KB

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