libahci.c 63 KB

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