libahci.c 56 KB

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