ahci.c 46 KB

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