ahci.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062
  1. /*
  2. * ahci.c - AHCI SATA support
  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/module.h>
  36. #include <linux/pci.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. #define DRV_NAME "ahci"
  47. #define DRV_VERSION "3.0"
  48. enum {
  49. AHCI_PCI_BAR = 5,
  50. AHCI_MAX_PORTS = 32,
  51. AHCI_MAX_SG = 168, /* hardware max is 64K */
  52. AHCI_DMA_BOUNDARY = 0xffffffff,
  53. AHCI_USE_CLUSTERING = 1,
  54. AHCI_MAX_CMDS = 32,
  55. AHCI_CMD_SZ = 32,
  56. AHCI_CMD_SLOT_SZ = AHCI_MAX_CMDS * AHCI_CMD_SZ,
  57. AHCI_RX_FIS_SZ = 256,
  58. AHCI_CMD_TBL_CDB = 0x40,
  59. AHCI_CMD_TBL_HDR_SZ = 0x80,
  60. AHCI_CMD_TBL_SZ = AHCI_CMD_TBL_HDR_SZ + (AHCI_MAX_SG * 16),
  61. AHCI_CMD_TBL_AR_SZ = AHCI_CMD_TBL_SZ * AHCI_MAX_CMDS,
  62. AHCI_PORT_PRIV_DMA_SZ = AHCI_CMD_SLOT_SZ + AHCI_CMD_TBL_AR_SZ +
  63. AHCI_RX_FIS_SZ,
  64. AHCI_IRQ_ON_SG = (1 << 31),
  65. AHCI_CMD_ATAPI = (1 << 5),
  66. AHCI_CMD_WRITE = (1 << 6),
  67. AHCI_CMD_PREFETCH = (1 << 7),
  68. AHCI_CMD_RESET = (1 << 8),
  69. AHCI_CMD_CLR_BUSY = (1 << 10),
  70. RX_FIS_D2H_REG = 0x40, /* offset of D2H Register FIS data */
  71. RX_FIS_SDB = 0x58, /* offset of SDB FIS data */
  72. RX_FIS_UNK = 0x60, /* offset of Unknown FIS data */
  73. board_ahci = 0,
  74. board_ahci_vt8251 = 1,
  75. board_ahci_ign_iferr = 2,
  76. board_ahci_sb600 = 3,
  77. board_ahci_mv = 4,
  78. /* global controller registers */
  79. HOST_CAP = 0x00, /* host capabilities */
  80. HOST_CTL = 0x04, /* global host control */
  81. HOST_IRQ_STAT = 0x08, /* interrupt status */
  82. HOST_PORTS_IMPL = 0x0c, /* bitmap of implemented ports */
  83. HOST_VERSION = 0x10, /* AHCI spec. version compliancy */
  84. /* HOST_CTL bits */
  85. HOST_RESET = (1 << 0), /* reset controller; self-clear */
  86. HOST_IRQ_EN = (1 << 1), /* global IRQ enable */
  87. HOST_AHCI_EN = (1 << 31), /* AHCI enabled */
  88. /* HOST_CAP bits */
  89. HOST_CAP_SSC = (1 << 14), /* Slumber capable */
  90. HOST_CAP_PMP = (1 << 17), /* Port Multiplier support */
  91. HOST_CAP_CLO = (1 << 24), /* Command List Override support */
  92. HOST_CAP_SSS = (1 << 27), /* Staggered Spin-up */
  93. HOST_CAP_SNTF = (1 << 29), /* SNotification register */
  94. HOST_CAP_NCQ = (1 << 30), /* Native Command Queueing */
  95. HOST_CAP_64 = (1 << 31), /* PCI DAC (64-bit DMA) support */
  96. /* registers for each SATA port */
  97. PORT_LST_ADDR = 0x00, /* command list DMA addr */
  98. PORT_LST_ADDR_HI = 0x04, /* command list DMA addr hi */
  99. PORT_FIS_ADDR = 0x08, /* FIS rx buf addr */
  100. PORT_FIS_ADDR_HI = 0x0c, /* FIS rx buf addr hi */
  101. PORT_IRQ_STAT = 0x10, /* interrupt status */
  102. PORT_IRQ_MASK = 0x14, /* interrupt enable/disable mask */
  103. PORT_CMD = 0x18, /* port command */
  104. PORT_TFDATA = 0x20, /* taskfile data */
  105. PORT_SIG = 0x24, /* device TF signature */
  106. PORT_CMD_ISSUE = 0x38, /* command issue */
  107. PORT_SCR_STAT = 0x28, /* SATA phy register: SStatus */
  108. PORT_SCR_CTL = 0x2c, /* SATA phy register: SControl */
  109. PORT_SCR_ERR = 0x30, /* SATA phy register: SError */
  110. PORT_SCR_ACT = 0x34, /* SATA phy register: SActive */
  111. PORT_SCR_NTF = 0x3c, /* SATA phy register: SNotification */
  112. /* PORT_IRQ_{STAT,MASK} bits */
  113. PORT_IRQ_COLD_PRES = (1 << 31), /* cold presence detect */
  114. PORT_IRQ_TF_ERR = (1 << 30), /* task file error */
  115. PORT_IRQ_HBUS_ERR = (1 << 29), /* host bus fatal error */
  116. PORT_IRQ_HBUS_DATA_ERR = (1 << 28), /* host bus data error */
  117. PORT_IRQ_IF_ERR = (1 << 27), /* interface fatal error */
  118. PORT_IRQ_IF_NONFATAL = (1 << 26), /* interface non-fatal error */
  119. PORT_IRQ_OVERFLOW = (1 << 24), /* xfer exhausted available S/G */
  120. PORT_IRQ_BAD_PMP = (1 << 23), /* incorrect port multiplier */
  121. PORT_IRQ_PHYRDY = (1 << 22), /* PhyRdy changed */
  122. PORT_IRQ_DEV_ILCK = (1 << 7), /* device interlock */
  123. PORT_IRQ_CONNECT = (1 << 6), /* port connect change status */
  124. PORT_IRQ_SG_DONE = (1 << 5), /* descriptor processed */
  125. PORT_IRQ_UNK_FIS = (1 << 4), /* unknown FIS rx'd */
  126. PORT_IRQ_SDB_FIS = (1 << 3), /* Set Device Bits FIS rx'd */
  127. PORT_IRQ_DMAS_FIS = (1 << 2), /* DMA Setup FIS rx'd */
  128. PORT_IRQ_PIOS_FIS = (1 << 1), /* PIO Setup FIS rx'd */
  129. PORT_IRQ_D2H_REG_FIS = (1 << 0), /* D2H Register FIS rx'd */
  130. PORT_IRQ_FREEZE = PORT_IRQ_HBUS_ERR |
  131. PORT_IRQ_IF_ERR |
  132. PORT_IRQ_CONNECT |
  133. PORT_IRQ_PHYRDY |
  134. PORT_IRQ_UNK_FIS |
  135. PORT_IRQ_BAD_PMP,
  136. PORT_IRQ_ERROR = PORT_IRQ_FREEZE |
  137. PORT_IRQ_TF_ERR |
  138. PORT_IRQ_HBUS_DATA_ERR,
  139. DEF_PORT_IRQ = PORT_IRQ_ERROR | PORT_IRQ_SG_DONE |
  140. PORT_IRQ_SDB_FIS | PORT_IRQ_DMAS_FIS |
  141. PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS,
  142. /* PORT_CMD bits */
  143. PORT_CMD_ATAPI = (1 << 24), /* Device is ATAPI */
  144. PORT_CMD_PMP = (1 << 17), /* PMP attached */
  145. PORT_CMD_LIST_ON = (1 << 15), /* cmd list DMA engine running */
  146. PORT_CMD_FIS_ON = (1 << 14), /* FIS DMA engine running */
  147. PORT_CMD_FIS_RX = (1 << 4), /* Enable FIS receive DMA engine */
  148. PORT_CMD_CLO = (1 << 3), /* Command list override */
  149. PORT_CMD_POWER_ON = (1 << 2), /* Power up device */
  150. PORT_CMD_SPIN_UP = (1 << 1), /* Spin up device */
  151. PORT_CMD_START = (1 << 0), /* Enable port DMA engine */
  152. PORT_CMD_ICC_MASK = (0xf << 28), /* i/f ICC state mask */
  153. PORT_CMD_ICC_ACTIVE = (0x1 << 28), /* Put i/f in active state */
  154. PORT_CMD_ICC_PARTIAL = (0x2 << 28), /* Put i/f in partial state */
  155. PORT_CMD_ICC_SLUMBER = (0x6 << 28), /* Put i/f in slumber state */
  156. /* hpriv->flags bits */
  157. AHCI_HFLAG_NO_NCQ = (1 << 0),
  158. AHCI_HFLAG_IGN_IRQ_IF_ERR = (1 << 1), /* ignore IRQ_IF_ERR */
  159. AHCI_HFLAG_IGN_SERR_INTERNAL = (1 << 2), /* ignore SERR_INTERNAL */
  160. AHCI_HFLAG_32BIT_ONLY = (1 << 3), /* force 32bit */
  161. AHCI_HFLAG_MV_PATA = (1 << 4), /* PATA port */
  162. AHCI_HFLAG_NO_MSI = (1 << 5), /* no PCI MSI */
  163. /* ap->flags bits */
  164. AHCI_FLAG_NO_HOTPLUG = (1 << 24), /* ignore PxSERR.DIAG.N */
  165. AHCI_FLAG_COMMON = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  166. ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA |
  167. ATA_FLAG_ACPI_SATA | ATA_FLAG_AN,
  168. AHCI_LFLAG_COMMON = ATA_LFLAG_SKIP_D2H_BSY,
  169. };
  170. struct ahci_cmd_hdr {
  171. u32 opts;
  172. u32 status;
  173. u32 tbl_addr;
  174. u32 tbl_addr_hi;
  175. u32 reserved[4];
  176. };
  177. struct ahci_sg {
  178. u32 addr;
  179. u32 addr_hi;
  180. u32 reserved;
  181. u32 flags_size;
  182. };
  183. struct ahci_host_priv {
  184. unsigned int flags; /* AHCI_HFLAG_* */
  185. u32 cap; /* cap to use */
  186. u32 port_map; /* port map to use */
  187. u32 saved_cap; /* saved initial cap */
  188. u32 saved_port_map; /* saved initial port_map */
  189. };
  190. struct ahci_port_priv {
  191. struct ata_link *active_link;
  192. struct ahci_cmd_hdr *cmd_slot;
  193. dma_addr_t cmd_slot_dma;
  194. void *cmd_tbl;
  195. dma_addr_t cmd_tbl_dma;
  196. void *rx_fis;
  197. dma_addr_t rx_fis_dma;
  198. /* for NCQ spurious interrupt analysis */
  199. unsigned int ncq_saw_d2h:1;
  200. unsigned int ncq_saw_dmas:1;
  201. unsigned int ncq_saw_sdb:1;
  202. u32 intr_mask; /* interrupts to enable */
  203. };
  204. static int ahci_scr_read(struct ata_port *ap, unsigned int sc_reg, u32 *val);
  205. static int ahci_scr_write(struct ata_port *ap, unsigned int sc_reg, u32 val);
  206. static int ahci_init_one (struct pci_dev *pdev, const struct pci_device_id *ent);
  207. static unsigned int ahci_qc_issue(struct ata_queued_cmd *qc);
  208. static void ahci_irq_clear(struct ata_port *ap);
  209. static int ahci_port_start(struct ata_port *ap);
  210. static void ahci_port_stop(struct ata_port *ap);
  211. static void ahci_tf_read(struct ata_port *ap, struct ata_taskfile *tf);
  212. static void ahci_qc_prep(struct ata_queued_cmd *qc);
  213. static u8 ahci_check_status(struct ata_port *ap);
  214. static void ahci_freeze(struct ata_port *ap);
  215. static void ahci_thaw(struct ata_port *ap);
  216. static void ahci_pmp_attach(struct ata_port *ap);
  217. static void ahci_pmp_detach(struct ata_port *ap);
  218. static int ahci_pmp_read(struct ata_device *dev, int pmp, int reg, u32 *r_val);
  219. static int ahci_pmp_write(struct ata_device *dev, int pmp, int reg, u32 val);
  220. static void ahci_error_handler(struct ata_port *ap);
  221. static void ahci_vt8251_error_handler(struct ata_port *ap);
  222. static void ahci_post_internal_cmd(struct ata_queued_cmd *qc);
  223. static int ahci_port_resume(struct ata_port *ap);
  224. static unsigned int ahci_fill_sg(struct ata_queued_cmd *qc, void *cmd_tbl);
  225. static void ahci_fill_cmd_slot(struct ahci_port_priv *pp, unsigned int tag,
  226. u32 opts);
  227. #ifdef CONFIG_PM
  228. static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg);
  229. static int ahci_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg);
  230. static int ahci_pci_device_resume(struct pci_dev *pdev);
  231. #endif
  232. static struct scsi_host_template ahci_sht = {
  233. .module = THIS_MODULE,
  234. .name = DRV_NAME,
  235. .ioctl = ata_scsi_ioctl,
  236. .queuecommand = ata_scsi_queuecmd,
  237. .change_queue_depth = ata_scsi_change_queue_depth,
  238. .can_queue = AHCI_MAX_CMDS - 1,
  239. .this_id = ATA_SHT_THIS_ID,
  240. .sg_tablesize = AHCI_MAX_SG,
  241. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  242. .emulated = ATA_SHT_EMULATED,
  243. .use_clustering = AHCI_USE_CLUSTERING,
  244. .proc_name = DRV_NAME,
  245. .dma_boundary = AHCI_DMA_BOUNDARY,
  246. .slave_configure = ata_scsi_slave_config,
  247. .slave_destroy = ata_scsi_slave_destroy,
  248. .bios_param = ata_std_bios_param,
  249. };
  250. static const struct ata_port_operations ahci_ops = {
  251. .check_status = ahci_check_status,
  252. .check_altstatus = ahci_check_status,
  253. .dev_select = ata_noop_dev_select,
  254. .tf_read = ahci_tf_read,
  255. .qc_defer = sata_pmp_qc_defer_cmd_switch,
  256. .qc_prep = ahci_qc_prep,
  257. .qc_issue = ahci_qc_issue,
  258. .irq_clear = ahci_irq_clear,
  259. .scr_read = ahci_scr_read,
  260. .scr_write = ahci_scr_write,
  261. .freeze = ahci_freeze,
  262. .thaw = ahci_thaw,
  263. .error_handler = ahci_error_handler,
  264. .post_internal_cmd = ahci_post_internal_cmd,
  265. .pmp_attach = ahci_pmp_attach,
  266. .pmp_detach = ahci_pmp_detach,
  267. .pmp_read = ahci_pmp_read,
  268. .pmp_write = ahci_pmp_write,
  269. #ifdef CONFIG_PM
  270. .port_suspend = ahci_port_suspend,
  271. .port_resume = ahci_port_resume,
  272. #endif
  273. .port_start = ahci_port_start,
  274. .port_stop = ahci_port_stop,
  275. };
  276. static const struct ata_port_operations ahci_vt8251_ops = {
  277. .check_status = ahci_check_status,
  278. .check_altstatus = ahci_check_status,
  279. .dev_select = ata_noop_dev_select,
  280. .tf_read = ahci_tf_read,
  281. .qc_defer = sata_pmp_qc_defer_cmd_switch,
  282. .qc_prep = ahci_qc_prep,
  283. .qc_issue = ahci_qc_issue,
  284. .irq_clear = ahci_irq_clear,
  285. .scr_read = ahci_scr_read,
  286. .scr_write = ahci_scr_write,
  287. .freeze = ahci_freeze,
  288. .thaw = ahci_thaw,
  289. .error_handler = ahci_vt8251_error_handler,
  290. .post_internal_cmd = ahci_post_internal_cmd,
  291. .pmp_attach = ahci_pmp_attach,
  292. .pmp_detach = ahci_pmp_detach,
  293. .pmp_read = ahci_pmp_read,
  294. .pmp_write = ahci_pmp_write,
  295. #ifdef CONFIG_PM
  296. .port_suspend = ahci_port_suspend,
  297. .port_resume = ahci_port_resume,
  298. #endif
  299. .port_start = ahci_port_start,
  300. .port_stop = ahci_port_stop,
  301. };
  302. #define AHCI_HFLAGS(flags) .private_data = (void *)(flags)
  303. static const struct ata_port_info ahci_port_info[] = {
  304. /* board_ahci */
  305. {
  306. .flags = AHCI_FLAG_COMMON,
  307. .link_flags = AHCI_LFLAG_COMMON,
  308. .pio_mask = 0x1f, /* pio0-4 */
  309. .udma_mask = ATA_UDMA6,
  310. .port_ops = &ahci_ops,
  311. },
  312. /* board_ahci_vt8251 */
  313. {
  314. AHCI_HFLAGS (AHCI_HFLAG_NO_NCQ),
  315. .flags = AHCI_FLAG_COMMON,
  316. .link_flags = AHCI_LFLAG_COMMON | ATA_LFLAG_HRST_TO_RESUME,
  317. .pio_mask = 0x1f, /* pio0-4 */
  318. .udma_mask = ATA_UDMA6,
  319. .port_ops = &ahci_vt8251_ops,
  320. },
  321. /* board_ahci_ign_iferr */
  322. {
  323. AHCI_HFLAGS (AHCI_HFLAG_IGN_IRQ_IF_ERR),
  324. .flags = AHCI_FLAG_COMMON,
  325. .link_flags = AHCI_LFLAG_COMMON,
  326. .pio_mask = 0x1f, /* pio0-4 */
  327. .udma_mask = ATA_UDMA6,
  328. .port_ops = &ahci_ops,
  329. },
  330. /* board_ahci_sb600 */
  331. {
  332. AHCI_HFLAGS (AHCI_HFLAG_IGN_SERR_INTERNAL |
  333. AHCI_HFLAG_32BIT_ONLY),
  334. .flags = AHCI_FLAG_COMMON,
  335. .link_flags = AHCI_LFLAG_COMMON,
  336. .pio_mask = 0x1f, /* pio0-4 */
  337. .udma_mask = ATA_UDMA6,
  338. .port_ops = &ahci_ops,
  339. },
  340. /* board_ahci_mv */
  341. {
  342. AHCI_HFLAGS (AHCI_HFLAG_NO_NCQ | AHCI_HFLAG_NO_MSI |
  343. AHCI_HFLAG_MV_PATA),
  344. .flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  345. ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA,
  346. .link_flags = AHCI_LFLAG_COMMON,
  347. .pio_mask = 0x1f, /* pio0-4 */
  348. .udma_mask = ATA_UDMA6,
  349. .port_ops = &ahci_ops,
  350. },
  351. };
  352. static const struct pci_device_id ahci_pci_tbl[] = {
  353. /* Intel */
  354. { PCI_VDEVICE(INTEL, 0x2652), board_ahci }, /* ICH6 */
  355. { PCI_VDEVICE(INTEL, 0x2653), board_ahci }, /* ICH6M */
  356. { PCI_VDEVICE(INTEL, 0x27c1), board_ahci }, /* ICH7 */
  357. { PCI_VDEVICE(INTEL, 0x27c5), board_ahci }, /* ICH7M */
  358. { PCI_VDEVICE(INTEL, 0x27c3), board_ahci }, /* ICH7R */
  359. { PCI_VDEVICE(AL, 0x5288), board_ahci_ign_iferr }, /* ULi M5288 */
  360. { PCI_VDEVICE(INTEL, 0x2681), board_ahci }, /* ESB2 */
  361. { PCI_VDEVICE(INTEL, 0x2682), board_ahci }, /* ESB2 */
  362. { PCI_VDEVICE(INTEL, 0x2683), board_ahci }, /* ESB2 */
  363. { PCI_VDEVICE(INTEL, 0x27c6), board_ahci }, /* ICH7-M DH */
  364. { PCI_VDEVICE(INTEL, 0x2821), board_ahci }, /* ICH8 */
  365. { PCI_VDEVICE(INTEL, 0x2822), board_ahci }, /* ICH8 */
  366. { PCI_VDEVICE(INTEL, 0x2824), board_ahci }, /* ICH8 */
  367. { PCI_VDEVICE(INTEL, 0x2829), board_ahci }, /* ICH8M */
  368. { PCI_VDEVICE(INTEL, 0x282a), board_ahci }, /* ICH8M */
  369. { PCI_VDEVICE(INTEL, 0x2922), board_ahci }, /* ICH9 */
  370. { PCI_VDEVICE(INTEL, 0x2923), board_ahci }, /* ICH9 */
  371. { PCI_VDEVICE(INTEL, 0x2924), board_ahci }, /* ICH9 */
  372. { PCI_VDEVICE(INTEL, 0x2925), board_ahci }, /* ICH9 */
  373. { PCI_VDEVICE(INTEL, 0x2927), board_ahci }, /* ICH9 */
  374. { PCI_VDEVICE(INTEL, 0x2929), board_ahci }, /* ICH9M */
  375. { PCI_VDEVICE(INTEL, 0x292a), board_ahci }, /* ICH9M */
  376. { PCI_VDEVICE(INTEL, 0x292b), board_ahci }, /* ICH9M */
  377. { PCI_VDEVICE(INTEL, 0x292c), board_ahci }, /* ICH9M */
  378. { PCI_VDEVICE(INTEL, 0x292f), board_ahci }, /* ICH9M */
  379. { PCI_VDEVICE(INTEL, 0x294d), board_ahci }, /* ICH9 */
  380. { PCI_VDEVICE(INTEL, 0x294e), board_ahci }, /* ICH9M */
  381. { PCI_VDEVICE(INTEL, 0x502a), board_ahci }, /* Tolapai */
  382. { PCI_VDEVICE(INTEL, 0x502b), board_ahci }, /* Tolapai */
  383. /* JMicron 360/1/3/5/6, match class to avoid IDE function */
  384. { PCI_VENDOR_ID_JMICRON, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
  385. PCI_CLASS_STORAGE_SATA_AHCI, 0xffffff, board_ahci_ign_iferr },
  386. /* ATI */
  387. { PCI_VDEVICE(ATI, 0x4380), board_ahci_sb600 }, /* ATI SB600 */
  388. { PCI_VDEVICE(ATI, 0x4390), board_ahci_sb600 }, /* ATI SB700/800 */
  389. { PCI_VDEVICE(ATI, 0x4391), board_ahci_sb600 }, /* ATI SB700/800 */
  390. { PCI_VDEVICE(ATI, 0x4392), board_ahci_sb600 }, /* ATI SB700/800 */
  391. { PCI_VDEVICE(ATI, 0x4393), board_ahci_sb600 }, /* ATI SB700/800 */
  392. { PCI_VDEVICE(ATI, 0x4394), board_ahci_sb600 }, /* ATI SB700/800 */
  393. { PCI_VDEVICE(ATI, 0x4395), board_ahci_sb600 }, /* ATI SB700/800 */
  394. /* VIA */
  395. { PCI_VDEVICE(VIA, 0x3349), board_ahci_vt8251 }, /* VIA VT8251 */
  396. { PCI_VDEVICE(VIA, 0x6287), board_ahci_vt8251 }, /* VIA VT8251 */
  397. /* NVIDIA */
  398. { PCI_VDEVICE(NVIDIA, 0x044c), board_ahci }, /* MCP65 */
  399. { PCI_VDEVICE(NVIDIA, 0x044d), board_ahci }, /* MCP65 */
  400. { PCI_VDEVICE(NVIDIA, 0x044e), board_ahci }, /* MCP65 */
  401. { PCI_VDEVICE(NVIDIA, 0x044f), board_ahci }, /* MCP65 */
  402. { PCI_VDEVICE(NVIDIA, 0x045c), board_ahci }, /* MCP65 */
  403. { PCI_VDEVICE(NVIDIA, 0x045d), board_ahci }, /* MCP65 */
  404. { PCI_VDEVICE(NVIDIA, 0x045e), board_ahci }, /* MCP65 */
  405. { PCI_VDEVICE(NVIDIA, 0x045f), board_ahci }, /* MCP65 */
  406. { PCI_VDEVICE(NVIDIA, 0x0550), board_ahci }, /* MCP67 */
  407. { PCI_VDEVICE(NVIDIA, 0x0551), board_ahci }, /* MCP67 */
  408. { PCI_VDEVICE(NVIDIA, 0x0552), board_ahci }, /* MCP67 */
  409. { PCI_VDEVICE(NVIDIA, 0x0553), board_ahci }, /* MCP67 */
  410. { PCI_VDEVICE(NVIDIA, 0x0554), board_ahci }, /* MCP67 */
  411. { PCI_VDEVICE(NVIDIA, 0x0555), board_ahci }, /* MCP67 */
  412. { PCI_VDEVICE(NVIDIA, 0x0556), board_ahci }, /* MCP67 */
  413. { PCI_VDEVICE(NVIDIA, 0x0557), board_ahci }, /* MCP67 */
  414. { PCI_VDEVICE(NVIDIA, 0x0558), board_ahci }, /* MCP67 */
  415. { PCI_VDEVICE(NVIDIA, 0x0559), board_ahci }, /* MCP67 */
  416. { PCI_VDEVICE(NVIDIA, 0x055a), board_ahci }, /* MCP67 */
  417. { PCI_VDEVICE(NVIDIA, 0x055b), board_ahci }, /* MCP67 */
  418. { PCI_VDEVICE(NVIDIA, 0x07f0), board_ahci }, /* MCP73 */
  419. { PCI_VDEVICE(NVIDIA, 0x07f1), board_ahci }, /* MCP73 */
  420. { PCI_VDEVICE(NVIDIA, 0x07f2), board_ahci }, /* MCP73 */
  421. { PCI_VDEVICE(NVIDIA, 0x07f3), board_ahci }, /* MCP73 */
  422. { PCI_VDEVICE(NVIDIA, 0x07f4), board_ahci }, /* MCP73 */
  423. { PCI_VDEVICE(NVIDIA, 0x07f5), board_ahci }, /* MCP73 */
  424. { PCI_VDEVICE(NVIDIA, 0x07f6), board_ahci }, /* MCP73 */
  425. { PCI_VDEVICE(NVIDIA, 0x07f7), board_ahci }, /* MCP73 */
  426. { PCI_VDEVICE(NVIDIA, 0x07f8), board_ahci }, /* MCP73 */
  427. { PCI_VDEVICE(NVIDIA, 0x07f9), board_ahci }, /* MCP73 */
  428. { PCI_VDEVICE(NVIDIA, 0x07fa), board_ahci }, /* MCP73 */
  429. { PCI_VDEVICE(NVIDIA, 0x07fb), board_ahci }, /* MCP73 */
  430. { PCI_VDEVICE(NVIDIA, 0x0ad0), board_ahci }, /* MCP77 */
  431. { PCI_VDEVICE(NVIDIA, 0x0ad1), board_ahci }, /* MCP77 */
  432. { PCI_VDEVICE(NVIDIA, 0x0ad2), board_ahci }, /* MCP77 */
  433. { PCI_VDEVICE(NVIDIA, 0x0ad3), board_ahci }, /* MCP77 */
  434. { PCI_VDEVICE(NVIDIA, 0x0ad4), board_ahci }, /* MCP77 */
  435. { PCI_VDEVICE(NVIDIA, 0x0ad5), board_ahci }, /* MCP77 */
  436. { PCI_VDEVICE(NVIDIA, 0x0ad6), board_ahci }, /* MCP77 */
  437. { PCI_VDEVICE(NVIDIA, 0x0ad7), board_ahci }, /* MCP77 */
  438. { PCI_VDEVICE(NVIDIA, 0x0ad8), board_ahci }, /* MCP77 */
  439. { PCI_VDEVICE(NVIDIA, 0x0ad9), board_ahci }, /* MCP77 */
  440. { PCI_VDEVICE(NVIDIA, 0x0ada), board_ahci }, /* MCP77 */
  441. { PCI_VDEVICE(NVIDIA, 0x0adb), board_ahci }, /* MCP77 */
  442. /* SiS */
  443. { PCI_VDEVICE(SI, 0x1184), board_ahci }, /* SiS 966 */
  444. { PCI_VDEVICE(SI, 0x1185), board_ahci }, /* SiS 966 */
  445. { PCI_VDEVICE(SI, 0x0186), board_ahci }, /* SiS 968 */
  446. /* Marvell */
  447. { PCI_VDEVICE(MARVELL, 0x6145), board_ahci_mv }, /* 6145 */
  448. /* Generic, PCI class code for AHCI */
  449. { PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
  450. PCI_CLASS_STORAGE_SATA_AHCI, 0xffffff, board_ahci },
  451. { } /* terminate list */
  452. };
  453. static struct pci_driver ahci_pci_driver = {
  454. .name = DRV_NAME,
  455. .id_table = ahci_pci_tbl,
  456. .probe = ahci_init_one,
  457. .remove = ata_pci_remove_one,
  458. #ifdef CONFIG_PM
  459. .suspend = ahci_pci_device_suspend,
  460. .resume = ahci_pci_device_resume,
  461. #endif
  462. };
  463. static inline int ahci_nr_ports(u32 cap)
  464. {
  465. return (cap & 0x1f) + 1;
  466. }
  467. static inline void __iomem *__ahci_port_base(struct ata_host *host,
  468. unsigned int port_no)
  469. {
  470. void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
  471. return mmio + 0x100 + (port_no * 0x80);
  472. }
  473. static inline void __iomem *ahci_port_base(struct ata_port *ap)
  474. {
  475. return __ahci_port_base(ap->host, ap->port_no);
  476. }
  477. /**
  478. * ahci_save_initial_config - Save and fixup initial config values
  479. * @pdev: target PCI device
  480. * @hpriv: host private area to store config values
  481. *
  482. * Some registers containing configuration info might be setup by
  483. * BIOS and might be cleared on reset. This function saves the
  484. * initial values of those registers into @hpriv such that they
  485. * can be restored after controller reset.
  486. *
  487. * If inconsistent, config values are fixed up by this function.
  488. *
  489. * LOCKING:
  490. * None.
  491. */
  492. static void ahci_save_initial_config(struct pci_dev *pdev,
  493. struct ahci_host_priv *hpriv)
  494. {
  495. void __iomem *mmio = pcim_iomap_table(pdev)[AHCI_PCI_BAR];
  496. u32 cap, port_map;
  497. int i;
  498. /* Values prefixed with saved_ are written back to host after
  499. * reset. Values without are used for driver operation.
  500. */
  501. hpriv->saved_cap = cap = readl(mmio + HOST_CAP);
  502. hpriv->saved_port_map = port_map = readl(mmio + HOST_PORTS_IMPL);
  503. /* some chips have errata preventing 64bit use */
  504. if ((cap & HOST_CAP_64) && (hpriv->flags & AHCI_HFLAG_32BIT_ONLY)) {
  505. dev_printk(KERN_INFO, &pdev->dev,
  506. "controller can't do 64bit DMA, forcing 32bit\n");
  507. cap &= ~HOST_CAP_64;
  508. }
  509. if ((cap & HOST_CAP_NCQ) && (hpriv->flags & AHCI_HFLAG_NO_NCQ)) {
  510. dev_printk(KERN_INFO, &pdev->dev,
  511. "controller can't do NCQ, turning off CAP_NCQ\n");
  512. cap &= ~HOST_CAP_NCQ;
  513. }
  514. /*
  515. * Temporary Marvell 6145 hack: PATA port presence
  516. * is asserted through the standard AHCI port
  517. * presence register, as bit 4 (counting from 0)
  518. */
  519. if (hpriv->flags & AHCI_HFLAG_MV_PATA) {
  520. dev_printk(KERN_ERR, &pdev->dev,
  521. "MV_AHCI HACK: port_map %x -> %x\n",
  522. hpriv->port_map,
  523. hpriv->port_map & 0xf);
  524. port_map &= 0xf;
  525. }
  526. /* cross check port_map and cap.n_ports */
  527. if (port_map) {
  528. u32 tmp_port_map = port_map;
  529. int n_ports = ahci_nr_ports(cap);
  530. for (i = 0; i < AHCI_MAX_PORTS && n_ports; i++) {
  531. if (tmp_port_map & (1 << i)) {
  532. n_ports--;
  533. tmp_port_map &= ~(1 << i);
  534. }
  535. }
  536. /* If n_ports and port_map are inconsistent, whine and
  537. * clear port_map and let it be generated from n_ports.
  538. */
  539. if (n_ports || tmp_port_map) {
  540. dev_printk(KERN_WARNING, &pdev->dev,
  541. "nr_ports (%u) and implemented port map "
  542. "(0x%x) don't match, using nr_ports\n",
  543. ahci_nr_ports(cap), port_map);
  544. port_map = 0;
  545. }
  546. }
  547. /* fabricate port_map from cap.nr_ports */
  548. if (!port_map) {
  549. port_map = (1 << ahci_nr_ports(cap)) - 1;
  550. dev_printk(KERN_WARNING, &pdev->dev,
  551. "forcing PORTS_IMPL to 0x%x\n", port_map);
  552. /* write the fixed up value to the PI register */
  553. hpriv->saved_port_map = port_map;
  554. }
  555. /* record values to use during operation */
  556. hpriv->cap = cap;
  557. hpriv->port_map = port_map;
  558. }
  559. /**
  560. * ahci_restore_initial_config - Restore initial config
  561. * @host: target ATA host
  562. *
  563. * Restore initial config stored by ahci_save_initial_config().
  564. *
  565. * LOCKING:
  566. * None.
  567. */
  568. static void ahci_restore_initial_config(struct ata_host *host)
  569. {
  570. struct ahci_host_priv *hpriv = host->private_data;
  571. void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
  572. writel(hpriv->saved_cap, mmio + HOST_CAP);
  573. writel(hpriv->saved_port_map, mmio + HOST_PORTS_IMPL);
  574. (void) readl(mmio + HOST_PORTS_IMPL); /* flush */
  575. }
  576. static unsigned ahci_scr_offset(struct ata_port *ap, unsigned int sc_reg)
  577. {
  578. static const int offset[] = {
  579. [SCR_STATUS] = PORT_SCR_STAT,
  580. [SCR_CONTROL] = PORT_SCR_CTL,
  581. [SCR_ERROR] = PORT_SCR_ERR,
  582. [SCR_ACTIVE] = PORT_SCR_ACT,
  583. [SCR_NOTIFICATION] = PORT_SCR_NTF,
  584. };
  585. struct ahci_host_priv *hpriv = ap->host->private_data;
  586. if (sc_reg < ARRAY_SIZE(offset) &&
  587. (sc_reg != SCR_NOTIFICATION || (hpriv->cap & HOST_CAP_SNTF)))
  588. return offset[sc_reg];
  589. return 0;
  590. }
  591. static int ahci_scr_read(struct ata_port *ap, unsigned int sc_reg, u32 *val)
  592. {
  593. void __iomem *port_mmio = ahci_port_base(ap);
  594. int offset = ahci_scr_offset(ap, sc_reg);
  595. if (offset) {
  596. *val = readl(port_mmio + offset);
  597. return 0;
  598. }
  599. return -EINVAL;
  600. }
  601. static int ahci_scr_write(struct ata_port *ap, unsigned int sc_reg, u32 val)
  602. {
  603. void __iomem *port_mmio = ahci_port_base(ap);
  604. int offset = ahci_scr_offset(ap, sc_reg);
  605. if (offset) {
  606. writel(val, port_mmio + offset);
  607. return 0;
  608. }
  609. return -EINVAL;
  610. }
  611. static void ahci_start_engine(struct ata_port *ap)
  612. {
  613. void __iomem *port_mmio = ahci_port_base(ap);
  614. u32 tmp;
  615. /* start DMA */
  616. tmp = readl(port_mmio + PORT_CMD);
  617. tmp |= PORT_CMD_START;
  618. writel(tmp, port_mmio + PORT_CMD);
  619. readl(port_mmio + PORT_CMD); /* flush */
  620. }
  621. static int ahci_stop_engine(struct ata_port *ap)
  622. {
  623. void __iomem *port_mmio = ahci_port_base(ap);
  624. u32 tmp;
  625. tmp = readl(port_mmio + PORT_CMD);
  626. /* check if the HBA is idle */
  627. if ((tmp & (PORT_CMD_START | PORT_CMD_LIST_ON)) == 0)
  628. return 0;
  629. /* setting HBA to idle */
  630. tmp &= ~PORT_CMD_START;
  631. writel(tmp, port_mmio + PORT_CMD);
  632. /* wait for engine to stop. This could be as long as 500 msec */
  633. tmp = ata_wait_register(port_mmio + PORT_CMD,
  634. PORT_CMD_LIST_ON, PORT_CMD_LIST_ON, 1, 500);
  635. if (tmp & PORT_CMD_LIST_ON)
  636. return -EIO;
  637. return 0;
  638. }
  639. static void ahci_start_fis_rx(struct ata_port *ap)
  640. {
  641. void __iomem *port_mmio = ahci_port_base(ap);
  642. struct ahci_host_priv *hpriv = ap->host->private_data;
  643. struct ahci_port_priv *pp = ap->private_data;
  644. u32 tmp;
  645. /* set FIS registers */
  646. if (hpriv->cap & HOST_CAP_64)
  647. writel((pp->cmd_slot_dma >> 16) >> 16,
  648. port_mmio + PORT_LST_ADDR_HI);
  649. writel(pp->cmd_slot_dma & 0xffffffff, port_mmio + PORT_LST_ADDR);
  650. if (hpriv->cap & HOST_CAP_64)
  651. writel((pp->rx_fis_dma >> 16) >> 16,
  652. port_mmio + PORT_FIS_ADDR_HI);
  653. writel(pp->rx_fis_dma & 0xffffffff, port_mmio + PORT_FIS_ADDR);
  654. /* enable FIS reception */
  655. tmp = readl(port_mmio + PORT_CMD);
  656. tmp |= PORT_CMD_FIS_RX;
  657. writel(tmp, port_mmio + PORT_CMD);
  658. /* flush */
  659. readl(port_mmio + PORT_CMD);
  660. }
  661. static int ahci_stop_fis_rx(struct ata_port *ap)
  662. {
  663. void __iomem *port_mmio = ahci_port_base(ap);
  664. u32 tmp;
  665. /* disable FIS reception */
  666. tmp = readl(port_mmio + PORT_CMD);
  667. tmp &= ~PORT_CMD_FIS_RX;
  668. writel(tmp, port_mmio + PORT_CMD);
  669. /* wait for completion, spec says 500ms, give it 1000 */
  670. tmp = ata_wait_register(port_mmio + PORT_CMD, PORT_CMD_FIS_ON,
  671. PORT_CMD_FIS_ON, 10, 1000);
  672. if (tmp & PORT_CMD_FIS_ON)
  673. return -EBUSY;
  674. return 0;
  675. }
  676. static void ahci_power_up(struct ata_port *ap)
  677. {
  678. struct ahci_host_priv *hpriv = ap->host->private_data;
  679. void __iomem *port_mmio = ahci_port_base(ap);
  680. u32 cmd;
  681. cmd = readl(port_mmio + PORT_CMD) & ~PORT_CMD_ICC_MASK;
  682. /* spin up device */
  683. if (hpriv->cap & HOST_CAP_SSS) {
  684. cmd |= PORT_CMD_SPIN_UP;
  685. writel(cmd, port_mmio + PORT_CMD);
  686. }
  687. /* wake up link */
  688. writel(cmd | PORT_CMD_ICC_ACTIVE, port_mmio + PORT_CMD);
  689. }
  690. #ifdef CONFIG_PM
  691. static void ahci_power_down(struct ata_port *ap)
  692. {
  693. struct ahci_host_priv *hpriv = ap->host->private_data;
  694. void __iomem *port_mmio = ahci_port_base(ap);
  695. u32 cmd, scontrol;
  696. if (!(hpriv->cap & HOST_CAP_SSS))
  697. return;
  698. /* put device into listen mode, first set PxSCTL.DET to 0 */
  699. scontrol = readl(port_mmio + PORT_SCR_CTL);
  700. scontrol &= ~0xf;
  701. writel(scontrol, port_mmio + PORT_SCR_CTL);
  702. /* then set PxCMD.SUD to 0 */
  703. cmd = readl(port_mmio + PORT_CMD) & ~PORT_CMD_ICC_MASK;
  704. cmd &= ~PORT_CMD_SPIN_UP;
  705. writel(cmd, port_mmio + PORT_CMD);
  706. }
  707. #endif
  708. static void ahci_start_port(struct ata_port *ap)
  709. {
  710. /* enable FIS reception */
  711. ahci_start_fis_rx(ap);
  712. /* enable DMA */
  713. ahci_start_engine(ap);
  714. }
  715. static int ahci_deinit_port(struct ata_port *ap, const char **emsg)
  716. {
  717. int rc;
  718. /* disable DMA */
  719. rc = ahci_stop_engine(ap);
  720. if (rc) {
  721. *emsg = "failed to stop engine";
  722. return rc;
  723. }
  724. /* disable FIS reception */
  725. rc = ahci_stop_fis_rx(ap);
  726. if (rc) {
  727. *emsg = "failed stop FIS RX";
  728. return rc;
  729. }
  730. return 0;
  731. }
  732. static int ahci_reset_controller(struct ata_host *host)
  733. {
  734. struct pci_dev *pdev = to_pci_dev(host->dev);
  735. void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
  736. u32 tmp;
  737. /* we must be in AHCI mode, before using anything
  738. * AHCI-specific, such as HOST_RESET.
  739. */
  740. tmp = readl(mmio + HOST_CTL);
  741. if (!(tmp & HOST_AHCI_EN))
  742. writel(tmp | HOST_AHCI_EN, mmio + HOST_CTL);
  743. /* global controller reset */
  744. if ((tmp & HOST_RESET) == 0) {
  745. writel(tmp | HOST_RESET, mmio + HOST_CTL);
  746. readl(mmio + HOST_CTL); /* flush */
  747. }
  748. /* reset must complete within 1 second, or
  749. * the hardware should be considered fried.
  750. */
  751. ssleep(1);
  752. tmp = readl(mmio + HOST_CTL);
  753. if (tmp & HOST_RESET) {
  754. dev_printk(KERN_ERR, host->dev,
  755. "controller reset failed (0x%x)\n", tmp);
  756. return -EIO;
  757. }
  758. /* turn on AHCI mode */
  759. writel(HOST_AHCI_EN, mmio + HOST_CTL);
  760. (void) readl(mmio + HOST_CTL); /* flush */
  761. /* some registers might be cleared on reset. restore initial values */
  762. ahci_restore_initial_config(host);
  763. if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
  764. u16 tmp16;
  765. /* configure PCS */
  766. pci_read_config_word(pdev, 0x92, &tmp16);
  767. tmp16 |= 0xf;
  768. pci_write_config_word(pdev, 0x92, tmp16);
  769. }
  770. return 0;
  771. }
  772. static void ahci_port_init(struct pci_dev *pdev, struct ata_port *ap,
  773. int port_no, void __iomem *mmio,
  774. void __iomem *port_mmio)
  775. {
  776. const char *emsg = NULL;
  777. int rc;
  778. u32 tmp;
  779. /* make sure port is not active */
  780. rc = ahci_deinit_port(ap, &emsg);
  781. if (rc)
  782. dev_printk(KERN_WARNING, &pdev->dev,
  783. "%s (%d)\n", emsg, rc);
  784. /* clear SError */
  785. tmp = readl(port_mmio + PORT_SCR_ERR);
  786. VPRINTK("PORT_SCR_ERR 0x%x\n", tmp);
  787. writel(tmp, port_mmio + PORT_SCR_ERR);
  788. /* clear port IRQ */
  789. tmp = readl(port_mmio + PORT_IRQ_STAT);
  790. VPRINTK("PORT_IRQ_STAT 0x%x\n", tmp);
  791. if (tmp)
  792. writel(tmp, port_mmio + PORT_IRQ_STAT);
  793. writel(1 << port_no, mmio + HOST_IRQ_STAT);
  794. }
  795. static void ahci_init_controller(struct ata_host *host)
  796. {
  797. struct ahci_host_priv *hpriv = host->private_data;
  798. struct pci_dev *pdev = to_pci_dev(host->dev);
  799. void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
  800. int i;
  801. void __iomem *port_mmio;
  802. u32 tmp;
  803. if (hpriv->flags & AHCI_HFLAG_MV_PATA) {
  804. port_mmio = __ahci_port_base(host, 4);
  805. writel(0, port_mmio + PORT_IRQ_MASK);
  806. /* clear port IRQ */
  807. tmp = readl(port_mmio + PORT_IRQ_STAT);
  808. VPRINTK("PORT_IRQ_STAT 0x%x\n", tmp);
  809. if (tmp)
  810. writel(tmp, port_mmio + PORT_IRQ_STAT);
  811. }
  812. for (i = 0; i < host->n_ports; i++) {
  813. struct ata_port *ap = host->ports[i];
  814. port_mmio = ahci_port_base(ap);
  815. if (ata_port_is_dummy(ap))
  816. continue;
  817. ahci_port_init(pdev, ap, i, mmio, port_mmio);
  818. }
  819. tmp = readl(mmio + HOST_CTL);
  820. VPRINTK("HOST_CTL 0x%x\n", tmp);
  821. writel(tmp | HOST_IRQ_EN, mmio + HOST_CTL);
  822. tmp = readl(mmio + HOST_CTL);
  823. VPRINTK("HOST_CTL 0x%x\n", tmp);
  824. }
  825. static unsigned int ahci_dev_classify(struct ata_port *ap)
  826. {
  827. void __iomem *port_mmio = ahci_port_base(ap);
  828. struct ata_taskfile tf;
  829. u32 tmp;
  830. tmp = readl(port_mmio + PORT_SIG);
  831. tf.lbah = (tmp >> 24) & 0xff;
  832. tf.lbam = (tmp >> 16) & 0xff;
  833. tf.lbal = (tmp >> 8) & 0xff;
  834. tf.nsect = (tmp) & 0xff;
  835. return ata_dev_classify(&tf);
  836. }
  837. static void ahci_fill_cmd_slot(struct ahci_port_priv *pp, unsigned int tag,
  838. u32 opts)
  839. {
  840. dma_addr_t cmd_tbl_dma;
  841. cmd_tbl_dma = pp->cmd_tbl_dma + tag * AHCI_CMD_TBL_SZ;
  842. pp->cmd_slot[tag].opts = cpu_to_le32(opts);
  843. pp->cmd_slot[tag].status = 0;
  844. pp->cmd_slot[tag].tbl_addr = cpu_to_le32(cmd_tbl_dma & 0xffffffff);
  845. pp->cmd_slot[tag].tbl_addr_hi = cpu_to_le32((cmd_tbl_dma >> 16) >> 16);
  846. }
  847. static int ahci_kick_engine(struct ata_port *ap, int force_restart)
  848. {
  849. void __iomem *port_mmio = ap->ioaddr.cmd_addr;
  850. struct ahci_host_priv *hpriv = ap->host->private_data;
  851. u32 tmp;
  852. int busy, rc;
  853. /* do we need to kick the port? */
  854. busy = ahci_check_status(ap) & (ATA_BUSY | ATA_DRQ);
  855. if (!busy && !force_restart)
  856. return 0;
  857. /* stop engine */
  858. rc = ahci_stop_engine(ap);
  859. if (rc)
  860. goto out_restart;
  861. /* need to do CLO? */
  862. if (!busy) {
  863. rc = 0;
  864. goto out_restart;
  865. }
  866. if (!(hpriv->cap & HOST_CAP_CLO)) {
  867. rc = -EOPNOTSUPP;
  868. goto out_restart;
  869. }
  870. /* perform CLO */
  871. tmp = readl(port_mmio + PORT_CMD);
  872. tmp |= PORT_CMD_CLO;
  873. writel(tmp, port_mmio + PORT_CMD);
  874. rc = 0;
  875. tmp = ata_wait_register(port_mmio + PORT_CMD,
  876. PORT_CMD_CLO, PORT_CMD_CLO, 1, 500);
  877. if (tmp & PORT_CMD_CLO)
  878. rc = -EIO;
  879. /* restart engine */
  880. out_restart:
  881. ahci_start_engine(ap);
  882. return rc;
  883. }
  884. static int ahci_exec_polled_cmd(struct ata_port *ap, int pmp,
  885. struct ata_taskfile *tf, int is_cmd, u16 flags,
  886. unsigned long timeout_msec)
  887. {
  888. const u32 cmd_fis_len = 5; /* five dwords */
  889. struct ahci_port_priv *pp = ap->private_data;
  890. void __iomem *port_mmio = ahci_port_base(ap);
  891. u8 *fis = pp->cmd_tbl;
  892. u32 tmp;
  893. /* prep the command */
  894. ata_tf_to_fis(tf, pmp, is_cmd, fis);
  895. ahci_fill_cmd_slot(pp, 0, cmd_fis_len | flags | (pmp << 12));
  896. /* issue & wait */
  897. writel(1, port_mmio + PORT_CMD_ISSUE);
  898. if (timeout_msec) {
  899. tmp = ata_wait_register(port_mmio + PORT_CMD_ISSUE, 0x1, 0x1,
  900. 1, timeout_msec);
  901. if (tmp & 0x1) {
  902. ahci_kick_engine(ap, 1);
  903. return -EBUSY;
  904. }
  905. } else
  906. readl(port_mmio + PORT_CMD_ISSUE); /* flush */
  907. return 0;
  908. }
  909. static int ahci_do_softreset(struct ata_link *link, unsigned int *class,
  910. int pmp, unsigned long deadline)
  911. {
  912. struct ata_port *ap = link->ap;
  913. const char *reason = NULL;
  914. unsigned long now, msecs;
  915. struct ata_taskfile tf;
  916. int rc;
  917. DPRINTK("ENTER\n");
  918. if (ata_link_offline(link)) {
  919. DPRINTK("PHY reports no device\n");
  920. *class = ATA_DEV_NONE;
  921. return 0;
  922. }
  923. /* prepare for SRST (AHCI-1.1 10.4.1) */
  924. rc = ahci_kick_engine(ap, 1);
  925. if (rc)
  926. ata_link_printk(link, KERN_WARNING,
  927. "failed to reset engine (errno=%d)", rc);
  928. ata_tf_init(link->device, &tf);
  929. /* issue the first D2H Register FIS */
  930. msecs = 0;
  931. now = jiffies;
  932. if (time_after(now, deadline))
  933. msecs = jiffies_to_msecs(deadline - now);
  934. tf.ctl |= ATA_SRST;
  935. if (ahci_exec_polled_cmd(ap, pmp, &tf, 0,
  936. AHCI_CMD_RESET | AHCI_CMD_CLR_BUSY, msecs)) {
  937. rc = -EIO;
  938. reason = "1st FIS failed";
  939. goto fail;
  940. }
  941. /* spec says at least 5us, but be generous and sleep for 1ms */
  942. msleep(1);
  943. /* issue the second D2H Register FIS */
  944. tf.ctl &= ~ATA_SRST;
  945. ahci_exec_polled_cmd(ap, pmp, &tf, 0, 0, 0);
  946. /* spec mandates ">= 2ms" before checking status.
  947. * We wait 150ms, because that was the magic delay used for
  948. * ATAPI devices in Hale Landis's ATADRVR, for the period of time
  949. * between when the ATA command register is written, and then
  950. * status is checked. Because waiting for "a while" before
  951. * checking status is fine, post SRST, we perform this magic
  952. * delay here as well.
  953. */
  954. msleep(150);
  955. rc = ata_wait_ready(ap, deadline);
  956. /* link occupied, -ENODEV too is an error */
  957. if (rc) {
  958. reason = "device not ready";
  959. goto fail;
  960. }
  961. *class = ahci_dev_classify(ap);
  962. DPRINTK("EXIT, class=%u\n", *class);
  963. return 0;
  964. fail:
  965. ata_link_printk(link, KERN_ERR, "softreset failed (%s)\n", reason);
  966. return rc;
  967. }
  968. static int ahci_softreset(struct ata_link *link, unsigned int *class,
  969. unsigned long deadline)
  970. {
  971. int pmp = 0;
  972. if (link->ap->flags & ATA_FLAG_PMP)
  973. pmp = SATA_PMP_CTRL_PORT;
  974. return ahci_do_softreset(link, class, pmp, deadline);
  975. }
  976. static int ahci_hardreset(struct ata_link *link, unsigned int *class,
  977. unsigned long deadline)
  978. {
  979. struct ata_port *ap = link->ap;
  980. struct ahci_port_priv *pp = ap->private_data;
  981. u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
  982. struct ata_taskfile tf;
  983. int rc;
  984. DPRINTK("ENTER\n");
  985. ahci_stop_engine(ap);
  986. /* clear D2H reception area to properly wait for D2H FIS */
  987. ata_tf_init(link->device, &tf);
  988. tf.command = 0x80;
  989. ata_tf_to_fis(&tf, 0, 0, d2h_fis);
  990. rc = sata_std_hardreset(link, class, deadline);
  991. ahci_start_engine(ap);
  992. if (rc == 0 && ata_link_online(link))
  993. *class = ahci_dev_classify(ap);
  994. if (rc != -EAGAIN && *class == ATA_DEV_UNKNOWN)
  995. *class = ATA_DEV_NONE;
  996. DPRINTK("EXIT, rc=%d, class=%u\n", rc, *class);
  997. return rc;
  998. }
  999. static int ahci_vt8251_hardreset(struct ata_link *link, unsigned int *class,
  1000. unsigned long deadline)
  1001. {
  1002. struct ata_port *ap = link->ap;
  1003. u32 serror;
  1004. int rc;
  1005. DPRINTK("ENTER\n");
  1006. ahci_stop_engine(ap);
  1007. rc = sata_link_hardreset(link, sata_ehc_deb_timing(&link->eh_context),
  1008. deadline);
  1009. /* vt8251 needs SError cleared for the port to operate */
  1010. ahci_scr_read(ap, SCR_ERROR, &serror);
  1011. ahci_scr_write(ap, SCR_ERROR, serror);
  1012. ahci_start_engine(ap);
  1013. DPRINTK("EXIT, rc=%d, class=%u\n", rc, *class);
  1014. /* vt8251 doesn't clear BSY on signature FIS reception,
  1015. * request follow-up softreset.
  1016. */
  1017. return rc ?: -EAGAIN;
  1018. }
  1019. static void ahci_postreset(struct ata_link *link, unsigned int *class)
  1020. {
  1021. struct ata_port *ap = link->ap;
  1022. void __iomem *port_mmio = ahci_port_base(ap);
  1023. u32 new_tmp, tmp;
  1024. ata_std_postreset(link, class);
  1025. /* Make sure port's ATAPI bit is set appropriately */
  1026. new_tmp = tmp = readl(port_mmio + PORT_CMD);
  1027. if (*class == ATA_DEV_ATAPI)
  1028. new_tmp |= PORT_CMD_ATAPI;
  1029. else
  1030. new_tmp &= ~PORT_CMD_ATAPI;
  1031. if (new_tmp != tmp) {
  1032. writel(new_tmp, port_mmio + PORT_CMD);
  1033. readl(port_mmio + PORT_CMD); /* flush */
  1034. }
  1035. }
  1036. static int ahci_pmp_softreset(struct ata_link *link, unsigned int *class,
  1037. unsigned long deadline)
  1038. {
  1039. return ahci_do_softreset(link, class, link->pmp, deadline);
  1040. }
  1041. static u8 ahci_check_status(struct ata_port *ap)
  1042. {
  1043. void __iomem *mmio = ap->ioaddr.cmd_addr;
  1044. return readl(mmio + PORT_TFDATA) & 0xFF;
  1045. }
  1046. static void ahci_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  1047. {
  1048. struct ahci_port_priv *pp = ap->private_data;
  1049. u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
  1050. ata_tf_from_fis(d2h_fis, tf);
  1051. }
  1052. static unsigned int ahci_fill_sg(struct ata_queued_cmd *qc, void *cmd_tbl)
  1053. {
  1054. struct scatterlist *sg;
  1055. struct ahci_sg *ahci_sg;
  1056. unsigned int n_sg = 0;
  1057. VPRINTK("ENTER\n");
  1058. /*
  1059. * Next, the S/G list.
  1060. */
  1061. ahci_sg = cmd_tbl + AHCI_CMD_TBL_HDR_SZ;
  1062. ata_for_each_sg(sg, qc) {
  1063. dma_addr_t addr = sg_dma_address(sg);
  1064. u32 sg_len = sg_dma_len(sg);
  1065. ahci_sg->addr = cpu_to_le32(addr & 0xffffffff);
  1066. ahci_sg->addr_hi = cpu_to_le32((addr >> 16) >> 16);
  1067. ahci_sg->flags_size = cpu_to_le32(sg_len - 1);
  1068. ahci_sg++;
  1069. n_sg++;
  1070. }
  1071. return n_sg;
  1072. }
  1073. static void ahci_qc_prep(struct ata_queued_cmd *qc)
  1074. {
  1075. struct ata_port *ap = qc->ap;
  1076. struct ahci_port_priv *pp = ap->private_data;
  1077. int is_atapi = is_atapi_taskfile(&qc->tf);
  1078. void *cmd_tbl;
  1079. u32 opts;
  1080. const u32 cmd_fis_len = 5; /* five dwords */
  1081. unsigned int n_elem;
  1082. /*
  1083. * Fill in command table information. First, the header,
  1084. * a SATA Register - Host to Device command FIS.
  1085. */
  1086. cmd_tbl = pp->cmd_tbl + qc->tag * AHCI_CMD_TBL_SZ;
  1087. ata_tf_to_fis(&qc->tf, qc->dev->link->pmp, 1, cmd_tbl);
  1088. if (is_atapi) {
  1089. memset(cmd_tbl + AHCI_CMD_TBL_CDB, 0, 32);
  1090. memcpy(cmd_tbl + AHCI_CMD_TBL_CDB, qc->cdb, qc->dev->cdb_len);
  1091. }
  1092. n_elem = 0;
  1093. if (qc->flags & ATA_QCFLAG_DMAMAP)
  1094. n_elem = ahci_fill_sg(qc, cmd_tbl);
  1095. /*
  1096. * Fill in command slot information.
  1097. */
  1098. opts = cmd_fis_len | n_elem << 16 | (qc->dev->link->pmp << 12);
  1099. if (qc->tf.flags & ATA_TFLAG_WRITE)
  1100. opts |= AHCI_CMD_WRITE;
  1101. if (is_atapi)
  1102. opts |= AHCI_CMD_ATAPI | AHCI_CMD_PREFETCH;
  1103. ahci_fill_cmd_slot(pp, qc->tag, opts);
  1104. }
  1105. static void ahci_error_intr(struct ata_port *ap, u32 irq_stat)
  1106. {
  1107. struct ahci_host_priv *hpriv = ap->host->private_data;
  1108. struct ahci_port_priv *pp = ap->private_data;
  1109. struct ata_eh_info *host_ehi = &ap->link.eh_info;
  1110. struct ata_link *link = NULL;
  1111. struct ata_queued_cmd *active_qc;
  1112. struct ata_eh_info *active_ehi;
  1113. u32 serror;
  1114. /* determine active link */
  1115. ata_port_for_each_link(link, ap)
  1116. if (ata_link_active(link))
  1117. break;
  1118. if (!link)
  1119. link = &ap->link;
  1120. active_qc = ata_qc_from_tag(ap, link->active_tag);
  1121. active_ehi = &link->eh_info;
  1122. /* record irq stat */
  1123. ata_ehi_clear_desc(host_ehi);
  1124. ata_ehi_push_desc(host_ehi, "irq_stat 0x%08x", irq_stat);
  1125. /* AHCI needs SError cleared; otherwise, it might lock up */
  1126. ahci_scr_read(ap, SCR_ERROR, &serror);
  1127. ahci_scr_write(ap, SCR_ERROR, serror);
  1128. host_ehi->serror |= serror;
  1129. /* some controllers set IRQ_IF_ERR on device errors, ignore it */
  1130. if (hpriv->flags & AHCI_HFLAG_IGN_IRQ_IF_ERR)
  1131. irq_stat &= ~PORT_IRQ_IF_ERR;
  1132. if (irq_stat & PORT_IRQ_TF_ERR) {
  1133. /* If qc is active, charge it; otherwise, the active
  1134. * link. There's no active qc on NCQ errors. It will
  1135. * be determined by EH by reading log page 10h.
  1136. */
  1137. if (active_qc)
  1138. active_qc->err_mask |= AC_ERR_DEV;
  1139. else
  1140. active_ehi->err_mask |= AC_ERR_DEV;
  1141. if (hpriv->flags & AHCI_HFLAG_IGN_SERR_INTERNAL)
  1142. host_ehi->serror &= ~SERR_INTERNAL;
  1143. }
  1144. if (irq_stat & PORT_IRQ_UNK_FIS) {
  1145. u32 *unk = (u32 *)(pp->rx_fis + RX_FIS_UNK);
  1146. active_ehi->err_mask |= AC_ERR_HSM;
  1147. active_ehi->action |= ATA_EH_SOFTRESET;
  1148. ata_ehi_push_desc(active_ehi,
  1149. "unknown FIS %08x %08x %08x %08x" ,
  1150. unk[0], unk[1], unk[2], unk[3]);
  1151. }
  1152. if (ap->nr_pmp_links && (irq_stat & PORT_IRQ_BAD_PMP)) {
  1153. active_ehi->err_mask |= AC_ERR_HSM;
  1154. active_ehi->action |= ATA_EH_SOFTRESET;
  1155. ata_ehi_push_desc(active_ehi, "incorrect PMP");
  1156. }
  1157. if (irq_stat & (PORT_IRQ_HBUS_ERR | PORT_IRQ_HBUS_DATA_ERR)) {
  1158. host_ehi->err_mask |= AC_ERR_HOST_BUS;
  1159. host_ehi->action |= ATA_EH_SOFTRESET;
  1160. ata_ehi_push_desc(host_ehi, "host bus error");
  1161. }
  1162. if (irq_stat & PORT_IRQ_IF_ERR) {
  1163. host_ehi->err_mask |= AC_ERR_ATA_BUS;
  1164. host_ehi->action |= ATA_EH_SOFTRESET;
  1165. ata_ehi_push_desc(host_ehi, "interface fatal error");
  1166. }
  1167. if (irq_stat & (PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)) {
  1168. ata_ehi_hotplugged(host_ehi);
  1169. ata_ehi_push_desc(host_ehi, "%s",
  1170. irq_stat & PORT_IRQ_CONNECT ?
  1171. "connection status changed" : "PHY RDY changed");
  1172. }
  1173. /* okay, let's hand over to EH */
  1174. if (irq_stat & PORT_IRQ_FREEZE)
  1175. ata_port_freeze(ap);
  1176. else
  1177. ata_port_abort(ap);
  1178. }
  1179. static void ahci_port_intr(struct ata_port *ap)
  1180. {
  1181. void __iomem *port_mmio = ap->ioaddr.cmd_addr;
  1182. struct ata_eh_info *ehi = &ap->link.eh_info;
  1183. struct ahci_port_priv *pp = ap->private_data;
  1184. u32 status, qc_active;
  1185. int rc, known_irq = 0;
  1186. status = readl(port_mmio + PORT_IRQ_STAT);
  1187. writel(status, port_mmio + PORT_IRQ_STAT);
  1188. if (unlikely(status & PORT_IRQ_ERROR)) {
  1189. ahci_error_intr(ap, status);
  1190. return;
  1191. }
  1192. if (status & PORT_IRQ_SDB_FIS) {
  1193. /* If the 'N' bit in word 0 of the FIS is set, we just
  1194. * received asynchronous notification. Tell libata
  1195. * about it. Note that as the SDB FIS itself is
  1196. * accessible, SNotification can be emulated by the
  1197. * driver but don't bother for the time being.
  1198. */
  1199. const __le32 *f = pp->rx_fis + RX_FIS_SDB;
  1200. u32 f0 = le32_to_cpu(f[0]);
  1201. if (f0 & (1 << 15))
  1202. sata_async_notification(ap);
  1203. }
  1204. /* pp->active_link is valid iff any command is in flight */
  1205. if (ap->qc_active && pp->active_link->sactive)
  1206. qc_active = readl(port_mmio + PORT_SCR_ACT);
  1207. else
  1208. qc_active = readl(port_mmio + PORT_CMD_ISSUE);
  1209. rc = ata_qc_complete_multiple(ap, qc_active, NULL);
  1210. if (rc > 0)
  1211. return;
  1212. if (rc < 0) {
  1213. ehi->err_mask |= AC_ERR_HSM;
  1214. ehi->action |= ATA_EH_SOFTRESET;
  1215. ata_port_freeze(ap);
  1216. return;
  1217. }
  1218. /* hmmm... a spurious interupt */
  1219. /* if !NCQ, ignore. No modern ATA device has broken HSM
  1220. * implementation for non-NCQ commands.
  1221. */
  1222. if (!ap->link.sactive)
  1223. return;
  1224. if (status & PORT_IRQ_D2H_REG_FIS) {
  1225. if (!pp->ncq_saw_d2h)
  1226. ata_port_printk(ap, KERN_INFO,
  1227. "D2H reg with I during NCQ, "
  1228. "this message won't be printed again\n");
  1229. pp->ncq_saw_d2h = 1;
  1230. known_irq = 1;
  1231. }
  1232. if (status & PORT_IRQ_DMAS_FIS) {
  1233. if (!pp->ncq_saw_dmas)
  1234. ata_port_printk(ap, KERN_INFO,
  1235. "DMAS FIS during NCQ, "
  1236. "this message won't be printed again\n");
  1237. pp->ncq_saw_dmas = 1;
  1238. known_irq = 1;
  1239. }
  1240. if (status & PORT_IRQ_SDB_FIS) {
  1241. const __le32 *f = pp->rx_fis + RX_FIS_SDB;
  1242. if (le32_to_cpu(f[1])) {
  1243. /* SDB FIS containing spurious completions
  1244. * might be dangerous, whine and fail commands
  1245. * with HSM violation. EH will turn off NCQ
  1246. * after several such failures.
  1247. */
  1248. ata_ehi_push_desc(ehi,
  1249. "spurious completions during NCQ "
  1250. "issue=0x%x SAct=0x%x FIS=%08x:%08x",
  1251. readl(port_mmio + PORT_CMD_ISSUE),
  1252. readl(port_mmio + PORT_SCR_ACT),
  1253. le32_to_cpu(f[0]), le32_to_cpu(f[1]));
  1254. ehi->err_mask |= AC_ERR_HSM;
  1255. ehi->action |= ATA_EH_SOFTRESET;
  1256. ata_port_freeze(ap);
  1257. } else {
  1258. if (!pp->ncq_saw_sdb)
  1259. ata_port_printk(ap, KERN_INFO,
  1260. "spurious SDB FIS %08x:%08x during NCQ, "
  1261. "this message won't be printed again\n",
  1262. le32_to_cpu(f[0]), le32_to_cpu(f[1]));
  1263. pp->ncq_saw_sdb = 1;
  1264. }
  1265. known_irq = 1;
  1266. }
  1267. if (!known_irq)
  1268. ata_port_printk(ap, KERN_INFO, "spurious interrupt "
  1269. "(irq_stat 0x%x active_tag 0x%x sactive 0x%x)\n",
  1270. status, ap->link.active_tag, ap->link.sactive);
  1271. }
  1272. static void ahci_irq_clear(struct ata_port *ap)
  1273. {
  1274. /* TODO */
  1275. }
  1276. static irqreturn_t ahci_interrupt(int irq, void *dev_instance)
  1277. {
  1278. struct ata_host *host = dev_instance;
  1279. struct ahci_host_priv *hpriv;
  1280. unsigned int i, handled = 0;
  1281. void __iomem *mmio;
  1282. u32 irq_stat, irq_ack = 0;
  1283. VPRINTK("ENTER\n");
  1284. hpriv = host->private_data;
  1285. mmio = host->iomap[AHCI_PCI_BAR];
  1286. /* sigh. 0xffffffff is a valid return from h/w */
  1287. irq_stat = readl(mmio + HOST_IRQ_STAT);
  1288. irq_stat &= hpriv->port_map;
  1289. if (!irq_stat)
  1290. return IRQ_NONE;
  1291. spin_lock(&host->lock);
  1292. for (i = 0; i < host->n_ports; i++) {
  1293. struct ata_port *ap;
  1294. if (!(irq_stat & (1 << i)))
  1295. continue;
  1296. ap = host->ports[i];
  1297. if (ap) {
  1298. ahci_port_intr(ap);
  1299. VPRINTK("port %u\n", i);
  1300. } else {
  1301. VPRINTK("port %u (no irq)\n", i);
  1302. if (ata_ratelimit())
  1303. dev_printk(KERN_WARNING, host->dev,
  1304. "interrupt on disabled port %u\n", i);
  1305. }
  1306. irq_ack |= (1 << i);
  1307. }
  1308. if (irq_ack) {
  1309. writel(irq_ack, mmio + HOST_IRQ_STAT);
  1310. handled = 1;
  1311. }
  1312. spin_unlock(&host->lock);
  1313. VPRINTK("EXIT\n");
  1314. return IRQ_RETVAL(handled);
  1315. }
  1316. static unsigned int ahci_qc_issue(struct ata_queued_cmd *qc)
  1317. {
  1318. struct ata_port *ap = qc->ap;
  1319. void __iomem *port_mmio = ahci_port_base(ap);
  1320. struct ahci_port_priv *pp = ap->private_data;
  1321. /* Keep track of the currently active link. It will be used
  1322. * in completion path to determine whether NCQ phase is in
  1323. * progress.
  1324. */
  1325. pp->active_link = qc->dev->link;
  1326. if (qc->tf.protocol == ATA_PROT_NCQ)
  1327. writel(1 << qc->tag, port_mmio + PORT_SCR_ACT);
  1328. writel(1 << qc->tag, port_mmio + PORT_CMD_ISSUE);
  1329. readl(port_mmio + PORT_CMD_ISSUE); /* flush */
  1330. return 0;
  1331. }
  1332. static void ahci_freeze(struct ata_port *ap)
  1333. {
  1334. void __iomem *port_mmio = ahci_port_base(ap);
  1335. /* turn IRQ off */
  1336. writel(0, port_mmio + PORT_IRQ_MASK);
  1337. }
  1338. static void ahci_thaw(struct ata_port *ap)
  1339. {
  1340. void __iomem *mmio = ap->host->iomap[AHCI_PCI_BAR];
  1341. void __iomem *port_mmio = ahci_port_base(ap);
  1342. u32 tmp;
  1343. struct ahci_port_priv *pp = ap->private_data;
  1344. /* clear IRQ */
  1345. tmp = readl(port_mmio + PORT_IRQ_STAT);
  1346. writel(tmp, port_mmio + PORT_IRQ_STAT);
  1347. writel(1 << ap->port_no, mmio + HOST_IRQ_STAT);
  1348. /* turn IRQ back on, ignore BAD_PMP if PMP isn't attached */
  1349. tmp = pp->intr_mask;
  1350. if (!ap->nr_pmp_links)
  1351. tmp &= ~PORT_IRQ_BAD_PMP;
  1352. writel(tmp, port_mmio + PORT_IRQ_MASK);
  1353. }
  1354. static void ahci_error_handler(struct ata_port *ap)
  1355. {
  1356. if (!(ap->pflags & ATA_PFLAG_FROZEN)) {
  1357. /* restart engine */
  1358. ahci_stop_engine(ap);
  1359. ahci_start_engine(ap);
  1360. }
  1361. /* perform recovery */
  1362. sata_pmp_do_eh(ap, ata_std_prereset, ahci_softreset,
  1363. ahci_hardreset, ahci_postreset,
  1364. sata_pmp_std_prereset, ahci_pmp_softreset,
  1365. sata_pmp_std_hardreset, sata_pmp_std_postreset);
  1366. }
  1367. static void ahci_vt8251_error_handler(struct ata_port *ap)
  1368. {
  1369. if (!(ap->pflags & ATA_PFLAG_FROZEN)) {
  1370. /* restart engine */
  1371. ahci_stop_engine(ap);
  1372. ahci_start_engine(ap);
  1373. }
  1374. /* perform recovery */
  1375. ata_do_eh(ap, ata_std_prereset, ahci_softreset, ahci_vt8251_hardreset,
  1376. ahci_postreset);
  1377. }
  1378. static void ahci_post_internal_cmd(struct ata_queued_cmd *qc)
  1379. {
  1380. struct ata_port *ap = qc->ap;
  1381. /* make DMA engine forget about the failed command */
  1382. if (qc->flags & ATA_QCFLAG_FAILED)
  1383. ahci_kick_engine(ap, 1);
  1384. }
  1385. static void ahci_pmp_attach(struct ata_port *ap)
  1386. {
  1387. void __iomem *port_mmio = ahci_port_base(ap);
  1388. u32 cmd;
  1389. cmd = readl(port_mmio + PORT_CMD);
  1390. cmd |= PORT_CMD_PMP;
  1391. writel(cmd, port_mmio + PORT_CMD);
  1392. }
  1393. static void ahci_pmp_detach(struct ata_port *ap)
  1394. {
  1395. void __iomem *port_mmio = ahci_port_base(ap);
  1396. struct ahci_host_priv *hpriv = ap->host->private_data;
  1397. unsigned long flags;
  1398. u32 cmd;
  1399. cmd = readl(port_mmio + PORT_CMD);
  1400. cmd &= ~PORT_CMD_PMP;
  1401. writel(cmd, port_mmio + PORT_CMD);
  1402. if (hpriv->cap & HOST_CAP_NCQ) {
  1403. spin_lock_irqsave(ap->lock, flags);
  1404. ap->flags |= ATA_FLAG_NCQ;
  1405. spin_unlock_irqrestore(ap->lock, flags);
  1406. }
  1407. }
  1408. static int ahci_pmp_read(struct ata_device *dev, int pmp, int reg, u32 *r_val)
  1409. {
  1410. struct ata_port *ap = dev->link->ap;
  1411. struct ata_taskfile tf;
  1412. int rc;
  1413. ahci_kick_engine(ap, 0);
  1414. sata_pmp_read_init_tf(&tf, dev, pmp, reg);
  1415. rc = ahci_exec_polled_cmd(ap, SATA_PMP_CTRL_PORT, &tf, 1, 0,
  1416. SATA_PMP_SCR_TIMEOUT);
  1417. if (rc == 0) {
  1418. ahci_tf_read(ap, &tf);
  1419. *r_val = sata_pmp_read_val(&tf);
  1420. }
  1421. return rc;
  1422. }
  1423. static int ahci_pmp_write(struct ata_device *dev, int pmp, int reg, u32 val)
  1424. {
  1425. struct ata_port *ap = dev->link->ap;
  1426. struct ata_taskfile tf;
  1427. ahci_kick_engine(ap, 0);
  1428. sata_pmp_write_init_tf(&tf, dev, pmp, reg, val);
  1429. return ahci_exec_polled_cmd(ap, SATA_PMP_CTRL_PORT, &tf, 1, 0,
  1430. SATA_PMP_SCR_TIMEOUT);
  1431. }
  1432. static int ahci_port_resume(struct ata_port *ap)
  1433. {
  1434. ahci_power_up(ap);
  1435. ahci_start_port(ap);
  1436. if (ap->nr_pmp_links)
  1437. ahci_pmp_attach(ap);
  1438. else
  1439. ahci_pmp_detach(ap);
  1440. return 0;
  1441. }
  1442. #ifdef CONFIG_PM
  1443. static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg)
  1444. {
  1445. const char *emsg = NULL;
  1446. int rc;
  1447. rc = ahci_deinit_port(ap, &emsg);
  1448. if (rc == 0)
  1449. ahci_power_down(ap);
  1450. else {
  1451. ata_port_printk(ap, KERN_ERR, "%s (%d)\n", emsg, rc);
  1452. ahci_start_port(ap);
  1453. }
  1454. return rc;
  1455. }
  1456. static int ahci_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
  1457. {
  1458. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  1459. void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
  1460. u32 ctl;
  1461. if (mesg.event == PM_EVENT_SUSPEND) {
  1462. /* AHCI spec rev1.1 section 8.3.3:
  1463. * Software must disable interrupts prior to requesting a
  1464. * transition of the HBA to D3 state.
  1465. */
  1466. ctl = readl(mmio + HOST_CTL);
  1467. ctl &= ~HOST_IRQ_EN;
  1468. writel(ctl, mmio + HOST_CTL);
  1469. readl(mmio + HOST_CTL); /* flush */
  1470. }
  1471. return ata_pci_device_suspend(pdev, mesg);
  1472. }
  1473. static int ahci_pci_device_resume(struct pci_dev *pdev)
  1474. {
  1475. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  1476. int rc;
  1477. rc = ata_pci_device_do_resume(pdev);
  1478. if (rc)
  1479. return rc;
  1480. if (pdev->dev.power.power_state.event == PM_EVENT_SUSPEND) {
  1481. rc = ahci_reset_controller(host);
  1482. if (rc)
  1483. return rc;
  1484. ahci_init_controller(host);
  1485. }
  1486. ata_host_resume(host);
  1487. return 0;
  1488. }
  1489. #endif
  1490. static int ahci_port_start(struct ata_port *ap)
  1491. {
  1492. struct device *dev = ap->host->dev;
  1493. struct ahci_port_priv *pp;
  1494. void *mem;
  1495. dma_addr_t mem_dma;
  1496. int rc;
  1497. pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
  1498. if (!pp)
  1499. return -ENOMEM;
  1500. rc = ata_pad_alloc(ap, dev);
  1501. if (rc)
  1502. return rc;
  1503. mem = dmam_alloc_coherent(dev, AHCI_PORT_PRIV_DMA_SZ, &mem_dma,
  1504. GFP_KERNEL);
  1505. if (!mem)
  1506. return -ENOMEM;
  1507. memset(mem, 0, AHCI_PORT_PRIV_DMA_SZ);
  1508. /*
  1509. * First item in chunk of DMA memory: 32-slot command table,
  1510. * 32 bytes each in size
  1511. */
  1512. pp->cmd_slot = mem;
  1513. pp->cmd_slot_dma = mem_dma;
  1514. mem += AHCI_CMD_SLOT_SZ;
  1515. mem_dma += AHCI_CMD_SLOT_SZ;
  1516. /*
  1517. * Second item: Received-FIS area
  1518. */
  1519. pp->rx_fis = mem;
  1520. pp->rx_fis_dma = mem_dma;
  1521. mem += AHCI_RX_FIS_SZ;
  1522. mem_dma += AHCI_RX_FIS_SZ;
  1523. /*
  1524. * Third item: data area for storing a single command
  1525. * and its scatter-gather table
  1526. */
  1527. pp->cmd_tbl = mem;
  1528. pp->cmd_tbl_dma = mem_dma;
  1529. /*
  1530. * Save off initial list of interrupts to be enabled.
  1531. * This could be changed later
  1532. */
  1533. pp->intr_mask = DEF_PORT_IRQ;
  1534. ap->private_data = pp;
  1535. /* engage engines, captain */
  1536. return ahci_port_resume(ap);
  1537. }
  1538. static void ahci_port_stop(struct ata_port *ap)
  1539. {
  1540. const char *emsg = NULL;
  1541. int rc;
  1542. /* de-initialize port */
  1543. rc = ahci_deinit_port(ap, &emsg);
  1544. if (rc)
  1545. ata_port_printk(ap, KERN_WARNING, "%s (%d)\n", emsg, rc);
  1546. }
  1547. static int ahci_configure_dma_masks(struct pci_dev *pdev, int using_dac)
  1548. {
  1549. int rc;
  1550. if (using_dac &&
  1551. !pci_set_dma_mask(pdev, DMA_64BIT_MASK)) {
  1552. rc = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
  1553. if (rc) {
  1554. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  1555. if (rc) {
  1556. dev_printk(KERN_ERR, &pdev->dev,
  1557. "64-bit DMA enable failed\n");
  1558. return rc;
  1559. }
  1560. }
  1561. } else {
  1562. rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  1563. if (rc) {
  1564. dev_printk(KERN_ERR, &pdev->dev,
  1565. "32-bit DMA enable failed\n");
  1566. return rc;
  1567. }
  1568. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  1569. if (rc) {
  1570. dev_printk(KERN_ERR, &pdev->dev,
  1571. "32-bit consistent DMA enable failed\n");
  1572. return rc;
  1573. }
  1574. }
  1575. return 0;
  1576. }
  1577. static void ahci_print_info(struct ata_host *host)
  1578. {
  1579. struct ahci_host_priv *hpriv = host->private_data;
  1580. struct pci_dev *pdev = to_pci_dev(host->dev);
  1581. void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
  1582. u32 vers, cap, impl, speed;
  1583. const char *speed_s;
  1584. u16 cc;
  1585. const char *scc_s;
  1586. vers = readl(mmio + HOST_VERSION);
  1587. cap = hpriv->cap;
  1588. impl = hpriv->port_map;
  1589. speed = (cap >> 20) & 0xf;
  1590. if (speed == 1)
  1591. speed_s = "1.5";
  1592. else if (speed == 2)
  1593. speed_s = "3";
  1594. else
  1595. speed_s = "?";
  1596. pci_read_config_word(pdev, 0x0a, &cc);
  1597. if (cc == PCI_CLASS_STORAGE_IDE)
  1598. scc_s = "IDE";
  1599. else if (cc == PCI_CLASS_STORAGE_SATA)
  1600. scc_s = "SATA";
  1601. else if (cc == PCI_CLASS_STORAGE_RAID)
  1602. scc_s = "RAID";
  1603. else
  1604. scc_s = "unknown";
  1605. dev_printk(KERN_INFO, &pdev->dev,
  1606. "AHCI %02x%02x.%02x%02x "
  1607. "%u slots %u ports %s Gbps 0x%x impl %s mode\n"
  1608. ,
  1609. (vers >> 24) & 0xff,
  1610. (vers >> 16) & 0xff,
  1611. (vers >> 8) & 0xff,
  1612. vers & 0xff,
  1613. ((cap >> 8) & 0x1f) + 1,
  1614. (cap & 0x1f) + 1,
  1615. speed_s,
  1616. impl,
  1617. scc_s);
  1618. dev_printk(KERN_INFO, &pdev->dev,
  1619. "flags: "
  1620. "%s%s%s%s%s%s%s"
  1621. "%s%s%s%s%s%s%s\n"
  1622. ,
  1623. cap & (1 << 31) ? "64bit " : "",
  1624. cap & (1 << 30) ? "ncq " : "",
  1625. cap & (1 << 29) ? "sntf " : "",
  1626. cap & (1 << 28) ? "ilck " : "",
  1627. cap & (1 << 27) ? "stag " : "",
  1628. cap & (1 << 26) ? "pm " : "",
  1629. cap & (1 << 25) ? "led " : "",
  1630. cap & (1 << 24) ? "clo " : "",
  1631. cap & (1 << 19) ? "nz " : "",
  1632. cap & (1 << 18) ? "only " : "",
  1633. cap & (1 << 17) ? "pmp " : "",
  1634. cap & (1 << 15) ? "pio " : "",
  1635. cap & (1 << 14) ? "slum " : "",
  1636. cap & (1 << 13) ? "part " : ""
  1637. );
  1638. }
  1639. static int ahci_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
  1640. {
  1641. static int printed_version;
  1642. struct ata_port_info pi = ahci_port_info[ent->driver_data];
  1643. const struct ata_port_info *ppi[] = { &pi, NULL };
  1644. struct device *dev = &pdev->dev;
  1645. struct ahci_host_priv *hpriv;
  1646. struct ata_host *host;
  1647. int i, rc;
  1648. VPRINTK("ENTER\n");
  1649. WARN_ON(ATA_MAX_QUEUE > AHCI_MAX_CMDS);
  1650. if (!printed_version++)
  1651. dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
  1652. /* acquire resources */
  1653. rc = pcim_enable_device(pdev);
  1654. if (rc)
  1655. return rc;
  1656. rc = pcim_iomap_regions(pdev, 1 << AHCI_PCI_BAR, DRV_NAME);
  1657. if (rc == -EBUSY)
  1658. pcim_pin_device(pdev);
  1659. if (rc)
  1660. return rc;
  1661. hpriv = devm_kzalloc(dev, sizeof(*hpriv), GFP_KERNEL);
  1662. if (!hpriv)
  1663. return -ENOMEM;
  1664. hpriv->flags |= (unsigned long)pi.private_data;
  1665. if ((hpriv->flags & AHCI_HFLAG_NO_MSI) || pci_enable_msi(pdev))
  1666. pci_intx(pdev, 1);
  1667. /* save initial config */
  1668. ahci_save_initial_config(pdev, hpriv);
  1669. /* prepare host */
  1670. if (hpriv->cap & HOST_CAP_NCQ)
  1671. pi.flags |= ATA_FLAG_NCQ;
  1672. if (hpriv->cap & HOST_CAP_PMP)
  1673. pi.flags |= ATA_FLAG_PMP;
  1674. host = ata_host_alloc_pinfo(&pdev->dev, ppi, fls(hpriv->port_map));
  1675. if (!host)
  1676. return -ENOMEM;
  1677. host->iomap = pcim_iomap_table(pdev);
  1678. host->private_data = hpriv;
  1679. for (i = 0; i < host->n_ports; i++) {
  1680. struct ata_port *ap = host->ports[i];
  1681. void __iomem *port_mmio = ahci_port_base(ap);
  1682. ata_port_pbar_desc(ap, AHCI_PCI_BAR, -1, "abar");
  1683. ata_port_pbar_desc(ap, AHCI_PCI_BAR,
  1684. 0x100 + ap->port_no * 0x80, "port");
  1685. /* standard SATA port setup */
  1686. if (hpriv->port_map & (1 << i))
  1687. ap->ioaddr.cmd_addr = port_mmio;
  1688. /* disabled/not-implemented port */
  1689. else
  1690. ap->ops = &ata_dummy_port_ops;
  1691. }
  1692. /* initialize adapter */
  1693. rc = ahci_configure_dma_masks(pdev, hpriv->cap & HOST_CAP_64);
  1694. if (rc)
  1695. return rc;
  1696. rc = ahci_reset_controller(host);
  1697. if (rc)
  1698. return rc;
  1699. ahci_init_controller(host);
  1700. ahci_print_info(host);
  1701. pci_set_master(pdev);
  1702. return ata_host_activate(host, pdev->irq, ahci_interrupt, IRQF_SHARED,
  1703. &ahci_sht);
  1704. }
  1705. static int __init ahci_init(void)
  1706. {
  1707. return pci_register_driver(&ahci_pci_driver);
  1708. }
  1709. static void __exit ahci_exit(void)
  1710. {
  1711. pci_unregister_driver(&ahci_pci_driver);
  1712. }
  1713. MODULE_AUTHOR("Jeff Garzik");
  1714. MODULE_DESCRIPTION("AHCI SATA low-level driver");
  1715. MODULE_LICENSE("GPL");
  1716. MODULE_DEVICE_TABLE(pci, ahci_pci_tbl);
  1717. MODULE_VERSION(DRV_VERSION);
  1718. module_init(ahci_init);
  1719. module_exit(ahci_exit);