ahci.c 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654
  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/sched.h>
  42. #include <linux/dma-mapping.h>
  43. #include <linux/device.h>
  44. #include <scsi/scsi_host.h>
  45. #include <scsi/scsi_cmnd.h>
  46. #include <linux/libata.h>
  47. #include <asm/io.h>
  48. #define DRV_NAME "ahci"
  49. #define DRV_VERSION "2.0"
  50. enum {
  51. AHCI_PCI_BAR = 5,
  52. AHCI_MAX_SG = 168, /* hardware max is 64K */
  53. AHCI_DMA_BOUNDARY = 0xffffffff,
  54. AHCI_USE_CLUSTERING = 0,
  55. AHCI_MAX_CMDS = 32,
  56. AHCI_CMD_SZ = 32,
  57. AHCI_CMD_SLOT_SZ = AHCI_MAX_CMDS * AHCI_CMD_SZ,
  58. AHCI_RX_FIS_SZ = 256,
  59. AHCI_CMD_TBL_CDB = 0x40,
  60. AHCI_CMD_TBL_HDR_SZ = 0x80,
  61. AHCI_CMD_TBL_SZ = AHCI_CMD_TBL_HDR_SZ + (AHCI_MAX_SG * 16),
  62. AHCI_CMD_TBL_AR_SZ = AHCI_CMD_TBL_SZ * AHCI_MAX_CMDS,
  63. AHCI_PORT_PRIV_DMA_SZ = AHCI_CMD_SLOT_SZ + AHCI_CMD_TBL_AR_SZ +
  64. AHCI_RX_FIS_SZ,
  65. AHCI_IRQ_ON_SG = (1 << 31),
  66. AHCI_CMD_ATAPI = (1 << 5),
  67. AHCI_CMD_WRITE = (1 << 6),
  68. AHCI_CMD_PREFETCH = (1 << 7),
  69. AHCI_CMD_RESET = (1 << 8),
  70. AHCI_CMD_CLR_BUSY = (1 << 10),
  71. RX_FIS_D2H_REG = 0x40, /* offset of D2H Register FIS data */
  72. RX_FIS_UNK = 0x60, /* offset of Unknown FIS data */
  73. board_ahci = 0,
  74. board_ahci_vt8251 = 1,
  75. /* global controller registers */
  76. HOST_CAP = 0x00, /* host capabilities */
  77. HOST_CTL = 0x04, /* global host control */
  78. HOST_IRQ_STAT = 0x08, /* interrupt status */
  79. HOST_PORTS_IMPL = 0x0c, /* bitmap of implemented ports */
  80. HOST_VERSION = 0x10, /* AHCI spec. version compliancy */
  81. /* HOST_CTL bits */
  82. HOST_RESET = (1 << 0), /* reset controller; self-clear */
  83. HOST_IRQ_EN = (1 << 1), /* global IRQ enable */
  84. HOST_AHCI_EN = (1 << 31), /* AHCI enabled */
  85. /* HOST_CAP bits */
  86. HOST_CAP_SSC = (1 << 14), /* Slumber capable */
  87. HOST_CAP_CLO = (1 << 24), /* Command List Override support */
  88. HOST_CAP_SSS = (1 << 27), /* Staggered Spin-up */
  89. HOST_CAP_NCQ = (1 << 30), /* Native Command Queueing */
  90. HOST_CAP_64 = (1 << 31), /* PCI DAC (64-bit DMA) support */
  91. /* registers for each SATA port */
  92. PORT_LST_ADDR = 0x00, /* command list DMA addr */
  93. PORT_LST_ADDR_HI = 0x04, /* command list DMA addr hi */
  94. PORT_FIS_ADDR = 0x08, /* FIS rx buf addr */
  95. PORT_FIS_ADDR_HI = 0x0c, /* FIS rx buf addr hi */
  96. PORT_IRQ_STAT = 0x10, /* interrupt status */
  97. PORT_IRQ_MASK = 0x14, /* interrupt enable/disable mask */
  98. PORT_CMD = 0x18, /* port command */
  99. PORT_TFDATA = 0x20, /* taskfile data */
  100. PORT_SIG = 0x24, /* device TF signature */
  101. PORT_CMD_ISSUE = 0x38, /* command issue */
  102. PORT_SCR = 0x28, /* SATA phy register block */
  103. PORT_SCR_STAT = 0x28, /* SATA phy register: SStatus */
  104. PORT_SCR_CTL = 0x2c, /* SATA phy register: SControl */
  105. PORT_SCR_ERR = 0x30, /* SATA phy register: SError */
  106. PORT_SCR_ACT = 0x34, /* SATA phy register: SActive */
  107. /* PORT_IRQ_{STAT,MASK} bits */
  108. PORT_IRQ_COLD_PRES = (1 << 31), /* cold presence detect */
  109. PORT_IRQ_TF_ERR = (1 << 30), /* task file error */
  110. PORT_IRQ_HBUS_ERR = (1 << 29), /* host bus fatal error */
  111. PORT_IRQ_HBUS_DATA_ERR = (1 << 28), /* host bus data error */
  112. PORT_IRQ_IF_ERR = (1 << 27), /* interface fatal error */
  113. PORT_IRQ_IF_NONFATAL = (1 << 26), /* interface non-fatal error */
  114. PORT_IRQ_OVERFLOW = (1 << 24), /* xfer exhausted available S/G */
  115. PORT_IRQ_BAD_PMP = (1 << 23), /* incorrect port multiplier */
  116. PORT_IRQ_PHYRDY = (1 << 22), /* PhyRdy changed */
  117. PORT_IRQ_DEV_ILCK = (1 << 7), /* device interlock */
  118. PORT_IRQ_CONNECT = (1 << 6), /* port connect change status */
  119. PORT_IRQ_SG_DONE = (1 << 5), /* descriptor processed */
  120. PORT_IRQ_UNK_FIS = (1 << 4), /* unknown FIS rx'd */
  121. PORT_IRQ_SDB_FIS = (1 << 3), /* Set Device Bits FIS rx'd */
  122. PORT_IRQ_DMAS_FIS = (1 << 2), /* DMA Setup FIS rx'd */
  123. PORT_IRQ_PIOS_FIS = (1 << 1), /* PIO Setup FIS rx'd */
  124. PORT_IRQ_D2H_REG_FIS = (1 << 0), /* D2H Register FIS rx'd */
  125. PORT_IRQ_FREEZE = PORT_IRQ_HBUS_ERR |
  126. PORT_IRQ_IF_ERR |
  127. PORT_IRQ_CONNECT |
  128. PORT_IRQ_PHYRDY |
  129. PORT_IRQ_UNK_FIS,
  130. PORT_IRQ_ERROR = PORT_IRQ_FREEZE |
  131. PORT_IRQ_TF_ERR |
  132. PORT_IRQ_HBUS_DATA_ERR,
  133. DEF_PORT_IRQ = PORT_IRQ_ERROR | PORT_IRQ_SG_DONE |
  134. PORT_IRQ_SDB_FIS | PORT_IRQ_DMAS_FIS |
  135. PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS,
  136. /* PORT_CMD bits */
  137. PORT_CMD_ATAPI = (1 << 24), /* Device is ATAPI */
  138. PORT_CMD_LIST_ON = (1 << 15), /* cmd list DMA engine running */
  139. PORT_CMD_FIS_ON = (1 << 14), /* FIS DMA engine running */
  140. PORT_CMD_FIS_RX = (1 << 4), /* Enable FIS receive DMA engine */
  141. PORT_CMD_CLO = (1 << 3), /* Command list override */
  142. PORT_CMD_POWER_ON = (1 << 2), /* Power up device */
  143. PORT_CMD_SPIN_UP = (1 << 1), /* Spin up device */
  144. PORT_CMD_START = (1 << 0), /* Enable port DMA engine */
  145. PORT_CMD_ICC_MASK = (0xf << 28), /* i/f ICC state mask */
  146. PORT_CMD_ICC_ACTIVE = (0x1 << 28), /* Put i/f in active state */
  147. PORT_CMD_ICC_PARTIAL = (0x2 << 28), /* Put i/f in partial state */
  148. PORT_CMD_ICC_SLUMBER = (0x6 << 28), /* Put i/f in slumber state */
  149. /* hpriv->flags bits */
  150. AHCI_FLAG_MSI = (1 << 0),
  151. /* ap->flags bits */
  152. AHCI_FLAG_RESET_NEEDS_CLO = (1 << 24),
  153. AHCI_FLAG_NO_NCQ = (1 << 25),
  154. };
  155. struct ahci_cmd_hdr {
  156. u32 opts;
  157. u32 status;
  158. u32 tbl_addr;
  159. u32 tbl_addr_hi;
  160. u32 reserved[4];
  161. };
  162. struct ahci_sg {
  163. u32 addr;
  164. u32 addr_hi;
  165. u32 reserved;
  166. u32 flags_size;
  167. };
  168. struct ahci_host_priv {
  169. unsigned long flags;
  170. u32 cap; /* cache of HOST_CAP register */
  171. u32 port_map; /* cache of HOST_PORTS_IMPL reg */
  172. };
  173. struct ahci_port_priv {
  174. struct ahci_cmd_hdr *cmd_slot;
  175. dma_addr_t cmd_slot_dma;
  176. void *cmd_tbl;
  177. dma_addr_t cmd_tbl_dma;
  178. void *rx_fis;
  179. dma_addr_t rx_fis_dma;
  180. };
  181. static u32 ahci_scr_read (struct ata_port *ap, unsigned int sc_reg);
  182. static void ahci_scr_write (struct ata_port *ap, unsigned int sc_reg, u32 val);
  183. static int ahci_init_one (struct pci_dev *pdev, const struct pci_device_id *ent);
  184. static unsigned int ahci_qc_issue(struct ata_queued_cmd *qc);
  185. static irqreturn_t ahci_interrupt (int irq, void *dev_instance);
  186. static void ahci_irq_clear(struct ata_port *ap);
  187. static int ahci_port_start(struct ata_port *ap);
  188. static void ahci_port_stop(struct ata_port *ap);
  189. static void ahci_tf_read(struct ata_port *ap, struct ata_taskfile *tf);
  190. static void ahci_qc_prep(struct ata_queued_cmd *qc);
  191. static u8 ahci_check_status(struct ata_port *ap);
  192. static void ahci_freeze(struct ata_port *ap);
  193. static void ahci_thaw(struct ata_port *ap);
  194. static void ahci_error_handler(struct ata_port *ap);
  195. static void ahci_post_internal_cmd(struct ata_queued_cmd *qc);
  196. static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg);
  197. static int ahci_port_resume(struct ata_port *ap);
  198. static int ahci_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg);
  199. static int ahci_pci_device_resume(struct pci_dev *pdev);
  200. static void ahci_remove_one (struct pci_dev *pdev);
  201. static struct scsi_host_template ahci_sht = {
  202. .module = THIS_MODULE,
  203. .name = DRV_NAME,
  204. .ioctl = ata_scsi_ioctl,
  205. .queuecommand = ata_scsi_queuecmd,
  206. .change_queue_depth = ata_scsi_change_queue_depth,
  207. .can_queue = AHCI_MAX_CMDS - 1,
  208. .this_id = ATA_SHT_THIS_ID,
  209. .sg_tablesize = AHCI_MAX_SG,
  210. .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
  211. .emulated = ATA_SHT_EMULATED,
  212. .use_clustering = AHCI_USE_CLUSTERING,
  213. .proc_name = DRV_NAME,
  214. .dma_boundary = AHCI_DMA_BOUNDARY,
  215. .slave_configure = ata_scsi_slave_config,
  216. .slave_destroy = ata_scsi_slave_destroy,
  217. .bios_param = ata_std_bios_param,
  218. .suspend = ata_scsi_device_suspend,
  219. .resume = ata_scsi_device_resume,
  220. };
  221. static const struct ata_port_operations ahci_ops = {
  222. .port_disable = ata_port_disable,
  223. .check_status = ahci_check_status,
  224. .check_altstatus = ahci_check_status,
  225. .dev_select = ata_noop_dev_select,
  226. .tf_read = ahci_tf_read,
  227. .qc_prep = ahci_qc_prep,
  228. .qc_issue = ahci_qc_issue,
  229. .irq_handler = ahci_interrupt,
  230. .irq_clear = ahci_irq_clear,
  231. .scr_read = ahci_scr_read,
  232. .scr_write = ahci_scr_write,
  233. .freeze = ahci_freeze,
  234. .thaw = ahci_thaw,
  235. .error_handler = ahci_error_handler,
  236. .post_internal_cmd = ahci_post_internal_cmd,
  237. .port_suspend = ahci_port_suspend,
  238. .port_resume = ahci_port_resume,
  239. .port_start = ahci_port_start,
  240. .port_stop = ahci_port_stop,
  241. };
  242. static const struct ata_port_info ahci_port_info[] = {
  243. /* board_ahci */
  244. {
  245. .sht = &ahci_sht,
  246. .flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  247. ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA |
  248. ATA_FLAG_SKIP_D2H_BSY,
  249. .pio_mask = 0x1f, /* pio0-4 */
  250. .udma_mask = 0x7f, /* udma0-6 ; FIXME */
  251. .port_ops = &ahci_ops,
  252. },
  253. /* board_ahci_vt8251 */
  254. {
  255. .sht = &ahci_sht,
  256. .flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
  257. ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA |
  258. ATA_FLAG_SKIP_D2H_BSY |
  259. AHCI_FLAG_RESET_NEEDS_CLO | AHCI_FLAG_NO_NCQ,
  260. .pio_mask = 0x1f, /* pio0-4 */
  261. .udma_mask = 0x7f, /* udma0-6 ; FIXME */
  262. .port_ops = &ahci_ops,
  263. },
  264. };
  265. static const struct pci_device_id ahci_pci_tbl[] = {
  266. /* Intel */
  267. { PCI_VDEVICE(INTEL, 0x2652), board_ahci }, /* ICH6 */
  268. { PCI_VDEVICE(INTEL, 0x2653), board_ahci }, /* ICH6M */
  269. { PCI_VDEVICE(INTEL, 0x27c1), board_ahci }, /* ICH7 */
  270. { PCI_VDEVICE(INTEL, 0x27c5), board_ahci }, /* ICH7M */
  271. { PCI_VDEVICE(INTEL, 0x27c3), board_ahci }, /* ICH7R */
  272. { PCI_VDEVICE(AL, 0x5288), board_ahci }, /* ULi M5288 */
  273. { PCI_VDEVICE(INTEL, 0x2681), board_ahci }, /* ESB2 */
  274. { PCI_VDEVICE(INTEL, 0x2682), board_ahci }, /* ESB2 */
  275. { PCI_VDEVICE(INTEL, 0x2683), board_ahci }, /* ESB2 */
  276. { PCI_VDEVICE(INTEL, 0x27c6), board_ahci }, /* ICH7-M DH */
  277. { PCI_VDEVICE(INTEL, 0x2821), board_ahci }, /* ICH8 */
  278. { PCI_VDEVICE(INTEL, 0x2822), board_ahci }, /* ICH8 */
  279. { PCI_VDEVICE(INTEL, 0x2824), board_ahci }, /* ICH8 */
  280. { PCI_VDEVICE(INTEL, 0x2829), board_ahci }, /* ICH8M */
  281. { PCI_VDEVICE(INTEL, 0x282a), board_ahci }, /* ICH8M */
  282. /* JMicron */
  283. { PCI_VDEVICE(JMICRON, 0x2360), board_ahci }, /* JMicron JMB360 */
  284. { PCI_VDEVICE(JMICRON, 0x2361), board_ahci }, /* JMicron JMB361 */
  285. { PCI_VDEVICE(JMICRON, 0x2363), board_ahci }, /* JMicron JMB363 */
  286. { PCI_VDEVICE(JMICRON, 0x2365), board_ahci }, /* JMicron JMB365 */
  287. { PCI_VDEVICE(JMICRON, 0x2366), board_ahci }, /* JMicron JMB366 */
  288. /* ATI */
  289. { PCI_VDEVICE(ATI, 0x4380), board_ahci }, /* ATI SB600 non-raid */
  290. { PCI_VDEVICE(ATI, 0x4381), board_ahci }, /* ATI SB600 raid */
  291. /* VIA */
  292. { PCI_VDEVICE(VIA, 0x3349), board_ahci_vt8251 }, /* VIA VT8251 */
  293. /* NVIDIA */
  294. { PCI_VDEVICE(NVIDIA, 0x044c), board_ahci }, /* MCP65 */
  295. { PCI_VDEVICE(NVIDIA, 0x044d), board_ahci }, /* MCP65 */
  296. { PCI_VDEVICE(NVIDIA, 0x044e), board_ahci }, /* MCP65 */
  297. { PCI_VDEVICE(NVIDIA, 0x044f), board_ahci }, /* MCP65 */
  298. /* SiS */
  299. { PCI_VDEVICE(SI, 0x1184), board_ahci }, /* SiS 966 */
  300. { PCI_VDEVICE(SI, 0x1185), board_ahci }, /* SiS 966 */
  301. { PCI_VDEVICE(SI, 0x0186), board_ahci }, /* SiS 968 */
  302. { } /* terminate list */
  303. };
  304. static struct pci_driver ahci_pci_driver = {
  305. .name = DRV_NAME,
  306. .id_table = ahci_pci_tbl,
  307. .probe = ahci_init_one,
  308. .suspend = ahci_pci_device_suspend,
  309. .resume = ahci_pci_device_resume,
  310. .remove = ahci_remove_one,
  311. };
  312. static inline unsigned long ahci_port_base_ul (unsigned long base, unsigned int port)
  313. {
  314. return base + 0x100 + (port * 0x80);
  315. }
  316. static inline void __iomem *ahci_port_base (void __iomem *base, unsigned int port)
  317. {
  318. return (void __iomem *) ahci_port_base_ul((unsigned long)base, port);
  319. }
  320. static u32 ahci_scr_read (struct ata_port *ap, unsigned int sc_reg_in)
  321. {
  322. unsigned int sc_reg;
  323. switch (sc_reg_in) {
  324. case SCR_STATUS: sc_reg = 0; break;
  325. case SCR_CONTROL: sc_reg = 1; break;
  326. case SCR_ERROR: sc_reg = 2; break;
  327. case SCR_ACTIVE: sc_reg = 3; break;
  328. default:
  329. return 0xffffffffU;
  330. }
  331. return readl((void __iomem *) ap->ioaddr.scr_addr + (sc_reg * 4));
  332. }
  333. static void ahci_scr_write (struct ata_port *ap, unsigned int sc_reg_in,
  334. u32 val)
  335. {
  336. unsigned int sc_reg;
  337. switch (sc_reg_in) {
  338. case SCR_STATUS: sc_reg = 0; break;
  339. case SCR_CONTROL: sc_reg = 1; break;
  340. case SCR_ERROR: sc_reg = 2; break;
  341. case SCR_ACTIVE: sc_reg = 3; break;
  342. default:
  343. return;
  344. }
  345. writel(val, (void __iomem *) ap->ioaddr.scr_addr + (sc_reg * 4));
  346. }
  347. static void ahci_start_engine(void __iomem *port_mmio)
  348. {
  349. u32 tmp;
  350. /* start DMA */
  351. tmp = readl(port_mmio + PORT_CMD);
  352. tmp |= PORT_CMD_START;
  353. writel(tmp, port_mmio + PORT_CMD);
  354. readl(port_mmio + PORT_CMD); /* flush */
  355. }
  356. static int ahci_stop_engine(void __iomem *port_mmio)
  357. {
  358. u32 tmp;
  359. tmp = readl(port_mmio + PORT_CMD);
  360. /* check if the HBA is idle */
  361. if ((tmp & (PORT_CMD_START | PORT_CMD_LIST_ON)) == 0)
  362. return 0;
  363. /* setting HBA to idle */
  364. tmp &= ~PORT_CMD_START;
  365. writel(tmp, port_mmio + PORT_CMD);
  366. /* wait for engine to stop. This could be as long as 500 msec */
  367. tmp = ata_wait_register(port_mmio + PORT_CMD,
  368. PORT_CMD_LIST_ON, PORT_CMD_LIST_ON, 1, 500);
  369. if (tmp & PORT_CMD_LIST_ON)
  370. return -EIO;
  371. return 0;
  372. }
  373. static void ahci_start_fis_rx(void __iomem *port_mmio, u32 cap,
  374. dma_addr_t cmd_slot_dma, dma_addr_t rx_fis_dma)
  375. {
  376. u32 tmp;
  377. /* set FIS registers */
  378. if (cap & HOST_CAP_64)
  379. writel((cmd_slot_dma >> 16) >> 16, port_mmio + PORT_LST_ADDR_HI);
  380. writel(cmd_slot_dma & 0xffffffff, port_mmio + PORT_LST_ADDR);
  381. if (cap & HOST_CAP_64)
  382. writel((rx_fis_dma >> 16) >> 16, port_mmio + PORT_FIS_ADDR_HI);
  383. writel(rx_fis_dma & 0xffffffff, port_mmio + PORT_FIS_ADDR);
  384. /* enable FIS reception */
  385. tmp = readl(port_mmio + PORT_CMD);
  386. tmp |= PORT_CMD_FIS_RX;
  387. writel(tmp, port_mmio + PORT_CMD);
  388. /* flush */
  389. readl(port_mmio + PORT_CMD);
  390. }
  391. static int ahci_stop_fis_rx(void __iomem *port_mmio)
  392. {
  393. u32 tmp;
  394. /* disable FIS reception */
  395. tmp = readl(port_mmio + PORT_CMD);
  396. tmp &= ~PORT_CMD_FIS_RX;
  397. writel(tmp, port_mmio + PORT_CMD);
  398. /* wait for completion, spec says 500ms, give it 1000 */
  399. tmp = ata_wait_register(port_mmio + PORT_CMD, PORT_CMD_FIS_ON,
  400. PORT_CMD_FIS_ON, 10, 1000);
  401. if (tmp & PORT_CMD_FIS_ON)
  402. return -EBUSY;
  403. return 0;
  404. }
  405. static void ahci_power_up(void __iomem *port_mmio, u32 cap)
  406. {
  407. u32 cmd;
  408. cmd = readl(port_mmio + PORT_CMD) & ~PORT_CMD_ICC_MASK;
  409. /* spin up device */
  410. if (cap & HOST_CAP_SSS) {
  411. cmd |= PORT_CMD_SPIN_UP;
  412. writel(cmd, port_mmio + PORT_CMD);
  413. }
  414. /* wake up link */
  415. writel(cmd | PORT_CMD_ICC_ACTIVE, port_mmio + PORT_CMD);
  416. }
  417. static void ahci_power_down(void __iomem *port_mmio, u32 cap)
  418. {
  419. u32 cmd, scontrol;
  420. cmd = readl(port_mmio + PORT_CMD) & ~PORT_CMD_ICC_MASK;
  421. if (cap & HOST_CAP_SSC) {
  422. /* enable transitions to slumber mode */
  423. scontrol = readl(port_mmio + PORT_SCR_CTL);
  424. if ((scontrol & 0x0f00) > 0x100) {
  425. scontrol &= ~0xf00;
  426. writel(scontrol, port_mmio + PORT_SCR_CTL);
  427. }
  428. /* put device into slumber mode */
  429. writel(cmd | PORT_CMD_ICC_SLUMBER, port_mmio + PORT_CMD);
  430. /* wait for the transition to complete */
  431. ata_wait_register(port_mmio + PORT_CMD, PORT_CMD_ICC_SLUMBER,
  432. PORT_CMD_ICC_SLUMBER, 1, 50);
  433. }
  434. /* put device into listen mode */
  435. if (cap & HOST_CAP_SSS) {
  436. /* first set PxSCTL.DET to 0 */
  437. scontrol = readl(port_mmio + PORT_SCR_CTL);
  438. scontrol &= ~0xf;
  439. writel(scontrol, port_mmio + PORT_SCR_CTL);
  440. /* then set PxCMD.SUD to 0 */
  441. cmd &= ~PORT_CMD_SPIN_UP;
  442. writel(cmd, port_mmio + PORT_CMD);
  443. }
  444. }
  445. static void ahci_init_port(void __iomem *port_mmio, u32 cap,
  446. dma_addr_t cmd_slot_dma, dma_addr_t rx_fis_dma)
  447. {
  448. /* power up */
  449. ahci_power_up(port_mmio, cap);
  450. /* enable FIS reception */
  451. ahci_start_fis_rx(port_mmio, cap, cmd_slot_dma, rx_fis_dma);
  452. /* enable DMA */
  453. ahci_start_engine(port_mmio);
  454. }
  455. static int ahci_deinit_port(void __iomem *port_mmio, u32 cap, const char **emsg)
  456. {
  457. int rc;
  458. /* disable DMA */
  459. rc = ahci_stop_engine(port_mmio);
  460. if (rc) {
  461. *emsg = "failed to stop engine";
  462. return rc;
  463. }
  464. /* disable FIS reception */
  465. rc = ahci_stop_fis_rx(port_mmio);
  466. if (rc) {
  467. *emsg = "failed stop FIS RX";
  468. return rc;
  469. }
  470. /* put device into slumber mode */
  471. ahci_power_down(port_mmio, cap);
  472. return 0;
  473. }
  474. static int ahci_reset_controller(void __iomem *mmio, struct pci_dev *pdev)
  475. {
  476. u32 cap_save, tmp;
  477. cap_save = readl(mmio + HOST_CAP);
  478. cap_save &= ( (1<<28) | (1<<17) );
  479. cap_save |= (1 << 27);
  480. /* global controller reset */
  481. tmp = readl(mmio + HOST_CTL);
  482. if ((tmp & HOST_RESET) == 0) {
  483. writel(tmp | HOST_RESET, mmio + HOST_CTL);
  484. readl(mmio + HOST_CTL); /* flush */
  485. }
  486. /* reset must complete within 1 second, or
  487. * the hardware should be considered fried.
  488. */
  489. ssleep(1);
  490. tmp = readl(mmio + HOST_CTL);
  491. if (tmp & HOST_RESET) {
  492. dev_printk(KERN_ERR, &pdev->dev,
  493. "controller reset failed (0x%x)\n", tmp);
  494. return -EIO;
  495. }
  496. writel(HOST_AHCI_EN, mmio + HOST_CTL);
  497. (void) readl(mmio + HOST_CTL); /* flush */
  498. writel(cap_save, mmio + HOST_CAP);
  499. writel(0xf, mmio + HOST_PORTS_IMPL);
  500. (void) readl(mmio + HOST_PORTS_IMPL); /* flush */
  501. if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
  502. u16 tmp16;
  503. /* configure PCS */
  504. pci_read_config_word(pdev, 0x92, &tmp16);
  505. tmp16 |= 0xf;
  506. pci_write_config_word(pdev, 0x92, tmp16);
  507. }
  508. return 0;
  509. }
  510. static void ahci_init_controller(void __iomem *mmio, struct pci_dev *pdev,
  511. int n_ports, u32 cap)
  512. {
  513. int i, rc;
  514. u32 tmp;
  515. for (i = 0; i < n_ports; i++) {
  516. void __iomem *port_mmio = ahci_port_base(mmio, i);
  517. const char *emsg = NULL;
  518. #if 0 /* BIOSen initialize this incorrectly */
  519. if (!(hpriv->port_map & (1 << i)))
  520. continue;
  521. #endif
  522. /* make sure port is not active */
  523. rc = ahci_deinit_port(port_mmio, cap, &emsg);
  524. if (rc)
  525. dev_printk(KERN_WARNING, &pdev->dev,
  526. "%s (%d)\n", emsg, rc);
  527. /* clear SError */
  528. tmp = readl(port_mmio + PORT_SCR_ERR);
  529. VPRINTK("PORT_SCR_ERR 0x%x\n", tmp);
  530. writel(tmp, port_mmio + PORT_SCR_ERR);
  531. /* clear port IRQ */
  532. tmp = readl(port_mmio + PORT_IRQ_STAT);
  533. VPRINTK("PORT_IRQ_STAT 0x%x\n", tmp);
  534. if (tmp)
  535. writel(tmp, port_mmio + PORT_IRQ_STAT);
  536. writel(1 << i, mmio + HOST_IRQ_STAT);
  537. }
  538. tmp = readl(mmio + HOST_CTL);
  539. VPRINTK("HOST_CTL 0x%x\n", tmp);
  540. writel(tmp | HOST_IRQ_EN, mmio + HOST_CTL);
  541. tmp = readl(mmio + HOST_CTL);
  542. VPRINTK("HOST_CTL 0x%x\n", tmp);
  543. }
  544. static unsigned int ahci_dev_classify(struct ata_port *ap)
  545. {
  546. void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
  547. struct ata_taskfile tf;
  548. u32 tmp;
  549. tmp = readl(port_mmio + PORT_SIG);
  550. tf.lbah = (tmp >> 24) & 0xff;
  551. tf.lbam = (tmp >> 16) & 0xff;
  552. tf.lbal = (tmp >> 8) & 0xff;
  553. tf.nsect = (tmp) & 0xff;
  554. return ata_dev_classify(&tf);
  555. }
  556. static void ahci_fill_cmd_slot(struct ahci_port_priv *pp, unsigned int tag,
  557. u32 opts)
  558. {
  559. dma_addr_t cmd_tbl_dma;
  560. cmd_tbl_dma = pp->cmd_tbl_dma + tag * AHCI_CMD_TBL_SZ;
  561. pp->cmd_slot[tag].opts = cpu_to_le32(opts);
  562. pp->cmd_slot[tag].status = 0;
  563. pp->cmd_slot[tag].tbl_addr = cpu_to_le32(cmd_tbl_dma & 0xffffffff);
  564. pp->cmd_slot[tag].tbl_addr_hi = cpu_to_le32((cmd_tbl_dma >> 16) >> 16);
  565. }
  566. static int ahci_clo(struct ata_port *ap)
  567. {
  568. void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
  569. struct ahci_host_priv *hpriv = ap->host->private_data;
  570. u32 tmp;
  571. if (!(hpriv->cap & HOST_CAP_CLO))
  572. return -EOPNOTSUPP;
  573. tmp = readl(port_mmio + PORT_CMD);
  574. tmp |= PORT_CMD_CLO;
  575. writel(tmp, port_mmio + PORT_CMD);
  576. tmp = ata_wait_register(port_mmio + PORT_CMD,
  577. PORT_CMD_CLO, PORT_CMD_CLO, 1, 500);
  578. if (tmp & PORT_CMD_CLO)
  579. return -EIO;
  580. return 0;
  581. }
  582. static int ahci_prereset(struct ata_port *ap)
  583. {
  584. if ((ap->flags & AHCI_FLAG_RESET_NEEDS_CLO) &&
  585. (ata_busy_wait(ap, ATA_BUSY, 1000) & ATA_BUSY)) {
  586. /* ATA_BUSY hasn't cleared, so send a CLO */
  587. ahci_clo(ap);
  588. }
  589. return ata_std_prereset(ap);
  590. }
  591. static int ahci_softreset(struct ata_port *ap, unsigned int *class)
  592. {
  593. struct ahci_port_priv *pp = ap->private_data;
  594. void __iomem *mmio = ap->host->mmio_base;
  595. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  596. const u32 cmd_fis_len = 5; /* five dwords */
  597. const char *reason = NULL;
  598. struct ata_taskfile tf;
  599. u32 tmp;
  600. u8 *fis;
  601. int rc;
  602. DPRINTK("ENTER\n");
  603. if (ata_port_offline(ap)) {
  604. DPRINTK("PHY reports no device\n");
  605. *class = ATA_DEV_NONE;
  606. return 0;
  607. }
  608. /* prepare for SRST (AHCI-1.1 10.4.1) */
  609. rc = ahci_stop_engine(port_mmio);
  610. if (rc) {
  611. reason = "failed to stop engine";
  612. goto fail_restart;
  613. }
  614. /* check BUSY/DRQ, perform Command List Override if necessary */
  615. ahci_tf_read(ap, &tf);
  616. if (tf.command & (ATA_BUSY | ATA_DRQ)) {
  617. rc = ahci_clo(ap);
  618. if (rc == -EOPNOTSUPP) {
  619. reason = "port busy but CLO unavailable";
  620. goto fail_restart;
  621. } else if (rc) {
  622. reason = "port busy but CLO failed";
  623. goto fail_restart;
  624. }
  625. }
  626. /* restart engine */
  627. ahci_start_engine(port_mmio);
  628. ata_tf_init(ap->device, &tf);
  629. fis = pp->cmd_tbl;
  630. /* issue the first D2H Register FIS */
  631. ahci_fill_cmd_slot(pp, 0,
  632. cmd_fis_len | AHCI_CMD_RESET | AHCI_CMD_CLR_BUSY);
  633. tf.ctl |= ATA_SRST;
  634. ata_tf_to_fis(&tf, fis, 0);
  635. fis[1] &= ~(1 << 7); /* turn off Command FIS bit */
  636. writel(1, port_mmio + PORT_CMD_ISSUE);
  637. tmp = ata_wait_register(port_mmio + PORT_CMD_ISSUE, 0x1, 0x1, 1, 500);
  638. if (tmp & 0x1) {
  639. rc = -EIO;
  640. reason = "1st FIS failed";
  641. goto fail;
  642. }
  643. /* spec says at least 5us, but be generous and sleep for 1ms */
  644. msleep(1);
  645. /* issue the second D2H Register FIS */
  646. ahci_fill_cmd_slot(pp, 0, cmd_fis_len);
  647. tf.ctl &= ~ATA_SRST;
  648. ata_tf_to_fis(&tf, fis, 0);
  649. fis[1] &= ~(1 << 7); /* turn off Command FIS bit */
  650. writel(1, port_mmio + PORT_CMD_ISSUE);
  651. readl(port_mmio + PORT_CMD_ISSUE); /* flush */
  652. /* spec mandates ">= 2ms" before checking status.
  653. * We wait 150ms, because that was the magic delay used for
  654. * ATAPI devices in Hale Landis's ATADRVR, for the period of time
  655. * between when the ATA command register is written, and then
  656. * status is checked. Because waiting for "a while" before
  657. * checking status is fine, post SRST, we perform this magic
  658. * delay here as well.
  659. */
  660. msleep(150);
  661. *class = ATA_DEV_NONE;
  662. if (ata_port_online(ap)) {
  663. if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
  664. rc = -EIO;
  665. reason = "device not ready";
  666. goto fail;
  667. }
  668. *class = ahci_dev_classify(ap);
  669. }
  670. DPRINTK("EXIT, class=%u\n", *class);
  671. return 0;
  672. fail_restart:
  673. ahci_start_engine(port_mmio);
  674. fail:
  675. ata_port_printk(ap, KERN_ERR, "softreset failed (%s)\n", reason);
  676. return rc;
  677. }
  678. static int ahci_hardreset(struct ata_port *ap, unsigned int *class)
  679. {
  680. struct ahci_port_priv *pp = ap->private_data;
  681. u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
  682. struct ata_taskfile tf;
  683. void __iomem *mmio = ap->host->mmio_base;
  684. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  685. int rc;
  686. DPRINTK("ENTER\n");
  687. ahci_stop_engine(port_mmio);
  688. /* clear D2H reception area to properly wait for D2H FIS */
  689. ata_tf_init(ap->device, &tf);
  690. tf.command = 0xff;
  691. ata_tf_to_fis(&tf, d2h_fis, 0);
  692. rc = sata_std_hardreset(ap, class);
  693. ahci_start_engine(port_mmio);
  694. if (rc == 0 && ata_port_online(ap))
  695. *class = ahci_dev_classify(ap);
  696. if (*class == ATA_DEV_UNKNOWN)
  697. *class = ATA_DEV_NONE;
  698. DPRINTK("EXIT, rc=%d, class=%u\n", rc, *class);
  699. return rc;
  700. }
  701. static void ahci_postreset(struct ata_port *ap, unsigned int *class)
  702. {
  703. void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
  704. u32 new_tmp, tmp;
  705. ata_std_postreset(ap, class);
  706. /* Make sure port's ATAPI bit is set appropriately */
  707. new_tmp = tmp = readl(port_mmio + PORT_CMD);
  708. if (*class == ATA_DEV_ATAPI)
  709. new_tmp |= PORT_CMD_ATAPI;
  710. else
  711. new_tmp &= ~PORT_CMD_ATAPI;
  712. if (new_tmp != tmp) {
  713. writel(new_tmp, port_mmio + PORT_CMD);
  714. readl(port_mmio + PORT_CMD); /* flush */
  715. }
  716. }
  717. static u8 ahci_check_status(struct ata_port *ap)
  718. {
  719. void __iomem *mmio = (void __iomem *) ap->ioaddr.cmd_addr;
  720. return readl(mmio + PORT_TFDATA) & 0xFF;
  721. }
  722. static void ahci_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
  723. {
  724. struct ahci_port_priv *pp = ap->private_data;
  725. u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
  726. ata_tf_from_fis(d2h_fis, tf);
  727. }
  728. static unsigned int ahci_fill_sg(struct ata_queued_cmd *qc, void *cmd_tbl)
  729. {
  730. struct scatterlist *sg;
  731. struct ahci_sg *ahci_sg;
  732. unsigned int n_sg = 0;
  733. VPRINTK("ENTER\n");
  734. /*
  735. * Next, the S/G list.
  736. */
  737. ahci_sg = cmd_tbl + AHCI_CMD_TBL_HDR_SZ;
  738. ata_for_each_sg(sg, qc) {
  739. dma_addr_t addr = sg_dma_address(sg);
  740. u32 sg_len = sg_dma_len(sg);
  741. ahci_sg->addr = cpu_to_le32(addr & 0xffffffff);
  742. ahci_sg->addr_hi = cpu_to_le32((addr >> 16) >> 16);
  743. ahci_sg->flags_size = cpu_to_le32(sg_len - 1);
  744. ahci_sg++;
  745. n_sg++;
  746. }
  747. return n_sg;
  748. }
  749. static void ahci_qc_prep(struct ata_queued_cmd *qc)
  750. {
  751. struct ata_port *ap = qc->ap;
  752. struct ahci_port_priv *pp = ap->private_data;
  753. int is_atapi = is_atapi_taskfile(&qc->tf);
  754. void *cmd_tbl;
  755. u32 opts;
  756. const u32 cmd_fis_len = 5; /* five dwords */
  757. unsigned int n_elem;
  758. /*
  759. * Fill in command table information. First, the header,
  760. * a SATA Register - Host to Device command FIS.
  761. */
  762. cmd_tbl = pp->cmd_tbl + qc->tag * AHCI_CMD_TBL_SZ;
  763. ata_tf_to_fis(&qc->tf, cmd_tbl, 0);
  764. if (is_atapi) {
  765. memset(cmd_tbl + AHCI_CMD_TBL_CDB, 0, 32);
  766. memcpy(cmd_tbl + AHCI_CMD_TBL_CDB, qc->cdb, qc->dev->cdb_len);
  767. }
  768. n_elem = 0;
  769. if (qc->flags & ATA_QCFLAG_DMAMAP)
  770. n_elem = ahci_fill_sg(qc, cmd_tbl);
  771. /*
  772. * Fill in command slot information.
  773. */
  774. opts = cmd_fis_len | n_elem << 16;
  775. if (qc->tf.flags & ATA_TFLAG_WRITE)
  776. opts |= AHCI_CMD_WRITE;
  777. if (is_atapi)
  778. opts |= AHCI_CMD_ATAPI | AHCI_CMD_PREFETCH;
  779. ahci_fill_cmd_slot(pp, qc->tag, opts);
  780. }
  781. static void ahci_error_intr(struct ata_port *ap, u32 irq_stat)
  782. {
  783. struct ahci_port_priv *pp = ap->private_data;
  784. struct ata_eh_info *ehi = &ap->eh_info;
  785. unsigned int err_mask = 0, action = 0;
  786. struct ata_queued_cmd *qc;
  787. u32 serror;
  788. ata_ehi_clear_desc(ehi);
  789. /* AHCI needs SError cleared; otherwise, it might lock up */
  790. serror = ahci_scr_read(ap, SCR_ERROR);
  791. ahci_scr_write(ap, SCR_ERROR, serror);
  792. /* analyze @irq_stat */
  793. ata_ehi_push_desc(ehi, "irq_stat 0x%08x", irq_stat);
  794. if (irq_stat & PORT_IRQ_TF_ERR)
  795. err_mask |= AC_ERR_DEV;
  796. if (irq_stat & (PORT_IRQ_HBUS_ERR | PORT_IRQ_HBUS_DATA_ERR)) {
  797. err_mask |= AC_ERR_HOST_BUS;
  798. action |= ATA_EH_SOFTRESET;
  799. }
  800. if (irq_stat & PORT_IRQ_IF_ERR) {
  801. err_mask |= AC_ERR_ATA_BUS;
  802. action |= ATA_EH_SOFTRESET;
  803. ata_ehi_push_desc(ehi, ", interface fatal error");
  804. }
  805. if (irq_stat & (PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)) {
  806. ata_ehi_hotplugged(ehi);
  807. ata_ehi_push_desc(ehi, ", %s", irq_stat & PORT_IRQ_CONNECT ?
  808. "connection status changed" : "PHY RDY changed");
  809. }
  810. if (irq_stat & PORT_IRQ_UNK_FIS) {
  811. u32 *unk = (u32 *)(pp->rx_fis + RX_FIS_UNK);
  812. err_mask |= AC_ERR_HSM;
  813. action |= ATA_EH_SOFTRESET;
  814. ata_ehi_push_desc(ehi, ", unknown FIS %08x %08x %08x %08x",
  815. unk[0], unk[1], unk[2], unk[3]);
  816. }
  817. /* okay, let's hand over to EH */
  818. ehi->serror |= serror;
  819. ehi->action |= action;
  820. qc = ata_qc_from_tag(ap, ap->active_tag);
  821. if (qc)
  822. qc->err_mask |= err_mask;
  823. else
  824. ehi->err_mask |= err_mask;
  825. if (irq_stat & PORT_IRQ_FREEZE)
  826. ata_port_freeze(ap);
  827. else
  828. ata_port_abort(ap);
  829. }
  830. static void ahci_host_intr(struct ata_port *ap)
  831. {
  832. void __iomem *mmio = ap->host->mmio_base;
  833. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  834. struct ata_eh_info *ehi = &ap->eh_info;
  835. u32 status, qc_active;
  836. int rc;
  837. status = readl(port_mmio + PORT_IRQ_STAT);
  838. writel(status, port_mmio + PORT_IRQ_STAT);
  839. if (unlikely(status & PORT_IRQ_ERROR)) {
  840. ahci_error_intr(ap, status);
  841. return;
  842. }
  843. if (ap->sactive)
  844. qc_active = readl(port_mmio + PORT_SCR_ACT);
  845. else
  846. qc_active = readl(port_mmio + PORT_CMD_ISSUE);
  847. rc = ata_qc_complete_multiple(ap, qc_active, NULL);
  848. if (rc > 0)
  849. return;
  850. if (rc < 0) {
  851. ehi->err_mask |= AC_ERR_HSM;
  852. ehi->action |= ATA_EH_SOFTRESET;
  853. ata_port_freeze(ap);
  854. return;
  855. }
  856. /* hmmm... a spurious interupt */
  857. /* some devices send D2H reg with I bit set during NCQ command phase */
  858. if (ap->sactive && (status & PORT_IRQ_D2H_REG_FIS))
  859. return;
  860. /* ignore interim PIO setup fis interrupts */
  861. if (ata_tag_valid(ap->active_tag) && (status & PORT_IRQ_PIOS_FIS))
  862. return;
  863. if (ata_ratelimit())
  864. ata_port_printk(ap, KERN_INFO, "spurious interrupt "
  865. "(irq_stat 0x%x active_tag %d sactive 0x%x)\n",
  866. status, ap->active_tag, ap->sactive);
  867. }
  868. static void ahci_irq_clear(struct ata_port *ap)
  869. {
  870. /* TODO */
  871. }
  872. static irqreturn_t ahci_interrupt(int irq, void *dev_instance)
  873. {
  874. struct ata_host *host = dev_instance;
  875. struct ahci_host_priv *hpriv;
  876. unsigned int i, handled = 0;
  877. void __iomem *mmio;
  878. u32 irq_stat, irq_ack = 0;
  879. VPRINTK("ENTER\n");
  880. hpriv = host->private_data;
  881. mmio = host->mmio_base;
  882. /* sigh. 0xffffffff is a valid return from h/w */
  883. irq_stat = readl(mmio + HOST_IRQ_STAT);
  884. irq_stat &= hpriv->port_map;
  885. if (!irq_stat)
  886. return IRQ_NONE;
  887. spin_lock(&host->lock);
  888. for (i = 0; i < host->n_ports; i++) {
  889. struct ata_port *ap;
  890. if (!(irq_stat & (1 << i)))
  891. continue;
  892. ap = host->ports[i];
  893. if (ap) {
  894. ahci_host_intr(ap);
  895. VPRINTK("port %u\n", i);
  896. } else {
  897. VPRINTK("port %u (no irq)\n", i);
  898. if (ata_ratelimit())
  899. dev_printk(KERN_WARNING, host->dev,
  900. "interrupt on disabled port %u\n", i);
  901. }
  902. irq_ack |= (1 << i);
  903. }
  904. if (irq_ack) {
  905. writel(irq_ack, mmio + HOST_IRQ_STAT);
  906. handled = 1;
  907. }
  908. spin_unlock(&host->lock);
  909. VPRINTK("EXIT\n");
  910. return IRQ_RETVAL(handled);
  911. }
  912. static unsigned int ahci_qc_issue(struct ata_queued_cmd *qc)
  913. {
  914. struct ata_port *ap = qc->ap;
  915. void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
  916. if (qc->tf.protocol == ATA_PROT_NCQ)
  917. writel(1 << qc->tag, port_mmio + PORT_SCR_ACT);
  918. writel(1 << qc->tag, port_mmio + PORT_CMD_ISSUE);
  919. readl(port_mmio + PORT_CMD_ISSUE); /* flush */
  920. return 0;
  921. }
  922. static void ahci_freeze(struct ata_port *ap)
  923. {
  924. void __iomem *mmio = ap->host->mmio_base;
  925. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  926. /* turn IRQ off */
  927. writel(0, port_mmio + PORT_IRQ_MASK);
  928. }
  929. static void ahci_thaw(struct ata_port *ap)
  930. {
  931. void __iomem *mmio = ap->host->mmio_base;
  932. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  933. u32 tmp;
  934. /* clear IRQ */
  935. tmp = readl(port_mmio + PORT_IRQ_STAT);
  936. writel(tmp, port_mmio + PORT_IRQ_STAT);
  937. writel(1 << ap->id, mmio + HOST_IRQ_STAT);
  938. /* turn IRQ back on */
  939. writel(DEF_PORT_IRQ, port_mmio + PORT_IRQ_MASK);
  940. }
  941. static void ahci_error_handler(struct ata_port *ap)
  942. {
  943. void __iomem *mmio = ap->host->mmio_base;
  944. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  945. if (!(ap->pflags & ATA_PFLAG_FROZEN)) {
  946. /* restart engine */
  947. ahci_stop_engine(port_mmio);
  948. ahci_start_engine(port_mmio);
  949. }
  950. /* perform recovery */
  951. ata_do_eh(ap, ahci_prereset, ahci_softreset, ahci_hardreset,
  952. ahci_postreset);
  953. }
  954. static void ahci_post_internal_cmd(struct ata_queued_cmd *qc)
  955. {
  956. struct ata_port *ap = qc->ap;
  957. void __iomem *mmio = ap->host->mmio_base;
  958. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  959. if (qc->flags & ATA_QCFLAG_FAILED)
  960. qc->err_mask |= AC_ERR_OTHER;
  961. if (qc->err_mask) {
  962. /* make DMA engine forget about the failed command */
  963. ahci_stop_engine(port_mmio);
  964. ahci_start_engine(port_mmio);
  965. }
  966. }
  967. static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg)
  968. {
  969. struct ahci_host_priv *hpriv = ap->host->private_data;
  970. struct ahci_port_priv *pp = ap->private_data;
  971. void __iomem *mmio = ap->host->mmio_base;
  972. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  973. const char *emsg = NULL;
  974. int rc;
  975. rc = ahci_deinit_port(port_mmio, hpriv->cap, &emsg);
  976. if (rc) {
  977. ata_port_printk(ap, KERN_ERR, "%s (%d)\n", emsg, rc);
  978. ahci_init_port(port_mmio, hpriv->cap,
  979. pp->cmd_slot_dma, pp->rx_fis_dma);
  980. }
  981. return rc;
  982. }
  983. static int ahci_port_resume(struct ata_port *ap)
  984. {
  985. struct ahci_port_priv *pp = ap->private_data;
  986. struct ahci_host_priv *hpriv = ap->host->private_data;
  987. void __iomem *mmio = ap->host->mmio_base;
  988. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  989. ahci_init_port(port_mmio, hpriv->cap, pp->cmd_slot_dma, pp->rx_fis_dma);
  990. return 0;
  991. }
  992. static int ahci_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
  993. {
  994. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  995. void __iomem *mmio = host->mmio_base;
  996. u32 ctl;
  997. if (mesg.event == PM_EVENT_SUSPEND) {
  998. /* AHCI spec rev1.1 section 8.3.3:
  999. * Software must disable interrupts prior to requesting a
  1000. * transition of the HBA to D3 state.
  1001. */
  1002. ctl = readl(mmio + HOST_CTL);
  1003. ctl &= ~HOST_IRQ_EN;
  1004. writel(ctl, mmio + HOST_CTL);
  1005. readl(mmio + HOST_CTL); /* flush */
  1006. }
  1007. return ata_pci_device_suspend(pdev, mesg);
  1008. }
  1009. static int ahci_pci_device_resume(struct pci_dev *pdev)
  1010. {
  1011. struct ata_host *host = dev_get_drvdata(&pdev->dev);
  1012. struct ahci_host_priv *hpriv = host->private_data;
  1013. void __iomem *mmio = host->mmio_base;
  1014. int rc;
  1015. ata_pci_device_do_resume(pdev);
  1016. if (pdev->dev.power.power_state.event == PM_EVENT_SUSPEND) {
  1017. rc = ahci_reset_controller(mmio, pdev);
  1018. if (rc)
  1019. return rc;
  1020. ahci_init_controller(mmio, pdev, host->n_ports, hpriv->cap);
  1021. }
  1022. ata_host_resume(host);
  1023. return 0;
  1024. }
  1025. static int ahci_port_start(struct ata_port *ap)
  1026. {
  1027. struct device *dev = ap->host->dev;
  1028. struct ahci_host_priv *hpriv = ap->host->private_data;
  1029. struct ahci_port_priv *pp;
  1030. void __iomem *mmio = ap->host->mmio_base;
  1031. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  1032. void *mem;
  1033. dma_addr_t mem_dma;
  1034. int rc;
  1035. pp = kmalloc(sizeof(*pp), GFP_KERNEL);
  1036. if (!pp)
  1037. return -ENOMEM;
  1038. memset(pp, 0, sizeof(*pp));
  1039. rc = ata_pad_alloc(ap, dev);
  1040. if (rc) {
  1041. kfree(pp);
  1042. return rc;
  1043. }
  1044. mem = dma_alloc_coherent(dev, AHCI_PORT_PRIV_DMA_SZ, &mem_dma, GFP_KERNEL);
  1045. if (!mem) {
  1046. ata_pad_free(ap, dev);
  1047. kfree(pp);
  1048. return -ENOMEM;
  1049. }
  1050. memset(mem, 0, AHCI_PORT_PRIV_DMA_SZ);
  1051. /*
  1052. * First item in chunk of DMA memory: 32-slot command table,
  1053. * 32 bytes each in size
  1054. */
  1055. pp->cmd_slot = mem;
  1056. pp->cmd_slot_dma = mem_dma;
  1057. mem += AHCI_CMD_SLOT_SZ;
  1058. mem_dma += AHCI_CMD_SLOT_SZ;
  1059. /*
  1060. * Second item: Received-FIS area
  1061. */
  1062. pp->rx_fis = mem;
  1063. pp->rx_fis_dma = mem_dma;
  1064. mem += AHCI_RX_FIS_SZ;
  1065. mem_dma += AHCI_RX_FIS_SZ;
  1066. /*
  1067. * Third item: data area for storing a single command
  1068. * and its scatter-gather table
  1069. */
  1070. pp->cmd_tbl = mem;
  1071. pp->cmd_tbl_dma = mem_dma;
  1072. ap->private_data = pp;
  1073. /* initialize port */
  1074. ahci_init_port(port_mmio, hpriv->cap, pp->cmd_slot_dma, pp->rx_fis_dma);
  1075. return 0;
  1076. }
  1077. static void ahci_port_stop(struct ata_port *ap)
  1078. {
  1079. struct device *dev = ap->host->dev;
  1080. struct ahci_host_priv *hpriv = ap->host->private_data;
  1081. struct ahci_port_priv *pp = ap->private_data;
  1082. void __iomem *mmio = ap->host->mmio_base;
  1083. void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
  1084. const char *emsg = NULL;
  1085. int rc;
  1086. /* de-initialize port */
  1087. rc = ahci_deinit_port(port_mmio, hpriv->cap, &emsg);
  1088. if (rc)
  1089. ata_port_printk(ap, KERN_WARNING, "%s (%d)\n", emsg, rc);
  1090. ap->private_data = NULL;
  1091. dma_free_coherent(dev, AHCI_PORT_PRIV_DMA_SZ,
  1092. pp->cmd_slot, pp->cmd_slot_dma);
  1093. ata_pad_free(ap, dev);
  1094. kfree(pp);
  1095. }
  1096. static void ahci_setup_port(struct ata_ioports *port, unsigned long base,
  1097. unsigned int port_idx)
  1098. {
  1099. VPRINTK("ENTER, base==0x%lx, port_idx %u\n", base, port_idx);
  1100. base = ahci_port_base_ul(base, port_idx);
  1101. VPRINTK("base now==0x%lx\n", base);
  1102. port->cmd_addr = base;
  1103. port->scr_addr = base + PORT_SCR;
  1104. VPRINTK("EXIT\n");
  1105. }
  1106. static int ahci_host_init(struct ata_probe_ent *probe_ent)
  1107. {
  1108. struct ahci_host_priv *hpriv = probe_ent->private_data;
  1109. struct pci_dev *pdev = to_pci_dev(probe_ent->dev);
  1110. void __iomem *mmio = probe_ent->mmio_base;
  1111. unsigned int i, using_dac;
  1112. int rc;
  1113. rc = ahci_reset_controller(mmio, pdev);
  1114. if (rc)
  1115. return rc;
  1116. hpriv->cap = readl(mmio + HOST_CAP);
  1117. hpriv->port_map = readl(mmio + HOST_PORTS_IMPL);
  1118. probe_ent->n_ports = (hpriv->cap & 0x1f) + 1;
  1119. VPRINTK("cap 0x%x port_map 0x%x n_ports %d\n",
  1120. hpriv->cap, hpriv->port_map, probe_ent->n_ports);
  1121. using_dac = hpriv->cap & HOST_CAP_64;
  1122. if (using_dac &&
  1123. !pci_set_dma_mask(pdev, DMA_64BIT_MASK)) {
  1124. rc = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
  1125. if (rc) {
  1126. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  1127. if (rc) {
  1128. dev_printk(KERN_ERR, &pdev->dev,
  1129. "64-bit DMA enable failed\n");
  1130. return rc;
  1131. }
  1132. }
  1133. } else {
  1134. rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
  1135. if (rc) {
  1136. dev_printk(KERN_ERR, &pdev->dev,
  1137. "32-bit DMA enable failed\n");
  1138. return rc;
  1139. }
  1140. rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
  1141. if (rc) {
  1142. dev_printk(KERN_ERR, &pdev->dev,
  1143. "32-bit consistent DMA enable failed\n");
  1144. return rc;
  1145. }
  1146. }
  1147. for (i = 0; i < probe_ent->n_ports; i++)
  1148. ahci_setup_port(&probe_ent->port[i], (unsigned long) mmio, i);
  1149. ahci_init_controller(mmio, pdev, probe_ent->n_ports, hpriv->cap);
  1150. pci_set_master(pdev);
  1151. return 0;
  1152. }
  1153. static void ahci_print_info(struct ata_probe_ent *probe_ent)
  1154. {
  1155. struct ahci_host_priv *hpriv = probe_ent->private_data;
  1156. struct pci_dev *pdev = to_pci_dev(probe_ent->dev);
  1157. void __iomem *mmio = probe_ent->mmio_base;
  1158. u32 vers, cap, impl, speed;
  1159. const char *speed_s;
  1160. u16 cc;
  1161. const char *scc_s;
  1162. vers = readl(mmio + HOST_VERSION);
  1163. cap = hpriv->cap;
  1164. impl = hpriv->port_map;
  1165. speed = (cap >> 20) & 0xf;
  1166. if (speed == 1)
  1167. speed_s = "1.5";
  1168. else if (speed == 2)
  1169. speed_s = "3";
  1170. else
  1171. speed_s = "?";
  1172. pci_read_config_word(pdev, 0x0a, &cc);
  1173. if (cc == 0x0101)
  1174. scc_s = "IDE";
  1175. else if (cc == 0x0106)
  1176. scc_s = "SATA";
  1177. else if (cc == 0x0104)
  1178. scc_s = "RAID";
  1179. else
  1180. scc_s = "unknown";
  1181. dev_printk(KERN_INFO, &pdev->dev,
  1182. "AHCI %02x%02x.%02x%02x "
  1183. "%u slots %u ports %s Gbps 0x%x impl %s mode\n"
  1184. ,
  1185. (vers >> 24) & 0xff,
  1186. (vers >> 16) & 0xff,
  1187. (vers >> 8) & 0xff,
  1188. vers & 0xff,
  1189. ((cap >> 8) & 0x1f) + 1,
  1190. (cap & 0x1f) + 1,
  1191. speed_s,
  1192. impl,
  1193. scc_s);
  1194. dev_printk(KERN_INFO, &pdev->dev,
  1195. "flags: "
  1196. "%s%s%s%s%s%s"
  1197. "%s%s%s%s%s%s%s\n"
  1198. ,
  1199. cap & (1 << 31) ? "64bit " : "",
  1200. cap & (1 << 30) ? "ncq " : "",
  1201. cap & (1 << 28) ? "ilck " : "",
  1202. cap & (1 << 27) ? "stag " : "",
  1203. cap & (1 << 26) ? "pm " : "",
  1204. cap & (1 << 25) ? "led " : "",
  1205. cap & (1 << 24) ? "clo " : "",
  1206. cap & (1 << 19) ? "nz " : "",
  1207. cap & (1 << 18) ? "only " : "",
  1208. cap & (1 << 17) ? "pmp " : "",
  1209. cap & (1 << 15) ? "pio " : "",
  1210. cap & (1 << 14) ? "slum " : "",
  1211. cap & (1 << 13) ? "part " : ""
  1212. );
  1213. }
  1214. static int ahci_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
  1215. {
  1216. static int printed_version;
  1217. struct ata_probe_ent *probe_ent = NULL;
  1218. struct ahci_host_priv *hpriv;
  1219. unsigned long base;
  1220. void __iomem *mmio_base;
  1221. unsigned int board_idx = (unsigned int) ent->driver_data;
  1222. int have_msi, pci_dev_busy = 0;
  1223. int rc;
  1224. VPRINTK("ENTER\n");
  1225. WARN_ON(ATA_MAX_QUEUE > AHCI_MAX_CMDS);
  1226. if (!printed_version++)
  1227. dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
  1228. /* JMicron-specific fixup: make sure we're in AHCI mode */
  1229. /* This is protected from races with ata_jmicron by the pci probe
  1230. locking */
  1231. if (pdev->vendor == PCI_VENDOR_ID_JMICRON) {
  1232. /* AHCI enable, AHCI on function 0 */
  1233. pci_write_config_byte(pdev, 0x41, 0xa1);
  1234. /* Function 1 is the PATA controller */
  1235. if (PCI_FUNC(pdev->devfn))
  1236. return -ENODEV;
  1237. }
  1238. rc = pci_enable_device(pdev);
  1239. if (rc)
  1240. return rc;
  1241. rc = pci_request_regions(pdev, DRV_NAME);
  1242. if (rc) {
  1243. pci_dev_busy = 1;
  1244. goto err_out;
  1245. }
  1246. if (pci_enable_msi(pdev) == 0)
  1247. have_msi = 1;
  1248. else {
  1249. pci_intx(pdev, 1);
  1250. have_msi = 0;
  1251. }
  1252. probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
  1253. if (probe_ent == NULL) {
  1254. rc = -ENOMEM;
  1255. goto err_out_msi;
  1256. }
  1257. memset(probe_ent, 0, sizeof(*probe_ent));
  1258. probe_ent->dev = pci_dev_to_dev(pdev);
  1259. INIT_LIST_HEAD(&probe_ent->node);
  1260. mmio_base = pci_iomap(pdev, AHCI_PCI_BAR, 0);
  1261. if (mmio_base == NULL) {
  1262. rc = -ENOMEM;
  1263. goto err_out_free_ent;
  1264. }
  1265. base = (unsigned long) mmio_base;
  1266. hpriv = kmalloc(sizeof(*hpriv), GFP_KERNEL);
  1267. if (!hpriv) {
  1268. rc = -ENOMEM;
  1269. goto err_out_iounmap;
  1270. }
  1271. memset(hpriv, 0, sizeof(*hpriv));
  1272. probe_ent->sht = ahci_port_info[board_idx].sht;
  1273. probe_ent->port_flags = ahci_port_info[board_idx].flags;
  1274. probe_ent->pio_mask = ahci_port_info[board_idx].pio_mask;
  1275. probe_ent->udma_mask = ahci_port_info[board_idx].udma_mask;
  1276. probe_ent->port_ops = ahci_port_info[board_idx].port_ops;
  1277. probe_ent->irq = pdev->irq;
  1278. probe_ent->irq_flags = IRQF_SHARED;
  1279. probe_ent->mmio_base = mmio_base;
  1280. probe_ent->private_data = hpriv;
  1281. if (have_msi)
  1282. hpriv->flags |= AHCI_FLAG_MSI;
  1283. /* initialize adapter */
  1284. rc = ahci_host_init(probe_ent);
  1285. if (rc)
  1286. goto err_out_hpriv;
  1287. if (!(probe_ent->port_flags & AHCI_FLAG_NO_NCQ) &&
  1288. (hpriv->cap & HOST_CAP_NCQ))
  1289. probe_ent->port_flags |= ATA_FLAG_NCQ;
  1290. ahci_print_info(probe_ent);
  1291. /* FIXME: check ata_device_add return value */
  1292. ata_device_add(probe_ent);
  1293. kfree(probe_ent);
  1294. return 0;
  1295. err_out_hpriv:
  1296. kfree(hpriv);
  1297. err_out_iounmap:
  1298. pci_iounmap(pdev, mmio_base);
  1299. err_out_free_ent:
  1300. kfree(probe_ent);
  1301. err_out_msi:
  1302. if (have_msi)
  1303. pci_disable_msi(pdev);
  1304. else
  1305. pci_intx(pdev, 0);
  1306. pci_release_regions(pdev);
  1307. err_out:
  1308. if (!pci_dev_busy)
  1309. pci_disable_device(pdev);
  1310. return rc;
  1311. }
  1312. static void ahci_remove_one (struct pci_dev *pdev)
  1313. {
  1314. struct device *dev = pci_dev_to_dev(pdev);
  1315. struct ata_host *host = dev_get_drvdata(dev);
  1316. struct ahci_host_priv *hpriv = host->private_data;
  1317. unsigned int i;
  1318. int have_msi;
  1319. for (i = 0; i < host->n_ports; i++)
  1320. ata_port_detach(host->ports[i]);
  1321. have_msi = hpriv->flags & AHCI_FLAG_MSI;
  1322. free_irq(host->irq, host);
  1323. for (i = 0; i < host->n_ports; i++) {
  1324. struct ata_port *ap = host->ports[i];
  1325. ata_scsi_release(ap->scsi_host);
  1326. scsi_host_put(ap->scsi_host);
  1327. }
  1328. kfree(hpriv);
  1329. pci_iounmap(pdev, host->mmio_base);
  1330. kfree(host);
  1331. if (have_msi)
  1332. pci_disable_msi(pdev);
  1333. else
  1334. pci_intx(pdev, 0);
  1335. pci_release_regions(pdev);
  1336. pci_disable_device(pdev);
  1337. dev_set_drvdata(dev, NULL);
  1338. }
  1339. static int __init ahci_init(void)
  1340. {
  1341. return pci_register_driver(&ahci_pci_driver);
  1342. }
  1343. static void __exit ahci_exit(void)
  1344. {
  1345. pci_unregister_driver(&ahci_pci_driver);
  1346. }
  1347. MODULE_AUTHOR("Jeff Garzik");
  1348. MODULE_DESCRIPTION("AHCI SATA low-level driver");
  1349. MODULE_LICENSE("GPL");
  1350. MODULE_DEVICE_TABLE(pci, ahci_pci_tbl);
  1351. MODULE_VERSION(DRV_VERSION);
  1352. module_init(ahci_init);
  1353. module_exit(ahci_exit);