libahci.c 64 KB

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