libahci.c 68 KB

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