libahci.c 67 KB

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