libahci.c 72 KB

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