libahci.c 72 KB

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