libahci.c 72 KB

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