libahci.c 68 KB

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