ahci.c 47 KB

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