libahci.c 55 KB

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