libahci.c 58 KB

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