blob: 8a1f4f059a43c9e18aef27df6c0c75c811eaf79c [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * ahci.c - AHCI SATA support
3 *
Jeff Garzikaf36d7f2005-08-28 20:18:39 -04004 * Maintained by: Jeff Garzik <jgarzik@pobox.com>
5 * Please ALWAYS copy linux-ide@vger.kernel.org
6 * on emails.
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
Jeff Garzikaf36d7f2005-08-28 20:18:39 -04008 * Copyright 2004-2005 Red Hat, Inc.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 *
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 *
Jeff Garzikaf36d7f2005-08-28 20:18:39 -040011 * 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:
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * http://www.intel.com/technology/serialata/pdf/rev1_0.pdf
Jeff Garzikaf36d7f2005-08-28 20:18:39 -040031 * http://www.intel.com/technology/serialata/pdf/rev1_1.pdf
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 *
33 */
34
35#include <linux/kernel.h>
36#include <linux/module.h>
37#include <linux/pci.h>
38#include <linux/init.h>
39#include <linux/blkdev.h>
40#include <linux/delay.h>
41#include <linux/interrupt.h>
42#include <linux/sched.h>
domen@coderock.org87507cf2005-04-08 09:53:06 +020043#include <linux/dma-mapping.h>
Jeff Garzika9524a72005-10-30 14:39:11 -050044#include <linux/device.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <scsi/scsi_host.h>
Jeff Garzik193515d2005-11-07 00:59:37 -050046#include <scsi/scsi_cmnd.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070047#include <linux/libata.h>
48#include <asm/io.h>
49
50#define DRV_NAME "ahci"
Jeff Garzik8676ce02006-06-26 20:41:33 -040051#define DRV_VERSION "2.0"
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53
54enum {
55 AHCI_PCI_BAR = 5,
56 AHCI_MAX_SG = 168, /* hardware max is 64K */
57 AHCI_DMA_BOUNDARY = 0xffffffff,
58 AHCI_USE_CLUSTERING = 0,
Tejun Heo12fad3f2006-05-15 21:03:55 +090059 AHCI_MAX_CMDS = 32,
Tejun Heodd410ff2006-05-15 21:03:50 +090060 AHCI_CMD_SZ = 32,
Tejun Heo12fad3f2006-05-15 21:03:55 +090061 AHCI_CMD_SLOT_SZ = AHCI_MAX_CMDS * AHCI_CMD_SZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -070062 AHCI_RX_FIS_SZ = 256,
Jeff Garzika0ea7322005-06-04 01:13:15 -040063 AHCI_CMD_TBL_CDB = 0x40,
Tejun Heodd410ff2006-05-15 21:03:50 +090064 AHCI_CMD_TBL_HDR_SZ = 0x80,
65 AHCI_CMD_TBL_SZ = AHCI_CMD_TBL_HDR_SZ + (AHCI_MAX_SG * 16),
66 AHCI_CMD_TBL_AR_SZ = AHCI_CMD_TBL_SZ * AHCI_MAX_CMDS,
67 AHCI_PORT_PRIV_DMA_SZ = AHCI_CMD_SLOT_SZ + AHCI_CMD_TBL_AR_SZ +
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 AHCI_RX_FIS_SZ,
69 AHCI_IRQ_ON_SG = (1 << 31),
70 AHCI_CMD_ATAPI = (1 << 5),
71 AHCI_CMD_WRITE = (1 << 6),
Tejun Heo4b10e552006-03-12 11:25:27 +090072 AHCI_CMD_PREFETCH = (1 << 7),
Tejun Heo22b49982006-01-23 21:38:44 +090073 AHCI_CMD_RESET = (1 << 8),
74 AHCI_CMD_CLR_BUSY = (1 << 10),
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 RX_FIS_D2H_REG = 0x40, /* offset of D2H Register FIS data */
Tejun Heo78cd52d2006-05-15 20:58:29 +090077 RX_FIS_UNK = 0x60, /* offset of Unknown FIS data */
Linus Torvalds1da177e2005-04-16 15:20:36 -070078
79 board_ahci = 0,
Bastiaan Jacquesbf2af2a2006-04-17 14:17:59 +020080 board_ahci_vt8251 = 1,
Tejun Heo41669552006-11-29 11:33:14 +090081 board_ahci_ign_iferr = 2,
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
83 /* global controller registers */
84 HOST_CAP = 0x00, /* host capabilities */
85 HOST_CTL = 0x04, /* global host control */
86 HOST_IRQ_STAT = 0x08, /* interrupt status */
87 HOST_PORTS_IMPL = 0x0c, /* bitmap of implemented ports */
88 HOST_VERSION = 0x10, /* AHCI spec. version compliancy */
89
90 /* HOST_CTL bits */
91 HOST_RESET = (1 << 0), /* reset controller; self-clear */
92 HOST_IRQ_EN = (1 << 1), /* global IRQ enable */
93 HOST_AHCI_EN = (1 << 31), /* AHCI enabled */
94
95 /* HOST_CAP bits */
Tejun Heo0be0aa92006-07-26 15:59:26 +090096 HOST_CAP_SSC = (1 << 14), /* Slumber capable */
Tejun Heo22b49982006-01-23 21:38:44 +090097 HOST_CAP_CLO = (1 << 24), /* Command List Override support */
Tejun Heo0be0aa92006-07-26 15:59:26 +090098 HOST_CAP_SSS = (1 << 27), /* Staggered Spin-up */
Tejun Heo979db802006-05-15 21:03:52 +090099 HOST_CAP_NCQ = (1 << 30), /* Native Command Queueing */
Tejun Heodd410ff2006-05-15 21:03:50 +0900100 HOST_CAP_64 = (1 << 31), /* PCI DAC (64-bit DMA) support */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102 /* registers for each SATA port */
103 PORT_LST_ADDR = 0x00, /* command list DMA addr */
104 PORT_LST_ADDR_HI = 0x04, /* command list DMA addr hi */
105 PORT_FIS_ADDR = 0x08, /* FIS rx buf addr */
106 PORT_FIS_ADDR_HI = 0x0c, /* FIS rx buf addr hi */
107 PORT_IRQ_STAT = 0x10, /* interrupt status */
108 PORT_IRQ_MASK = 0x14, /* interrupt enable/disable mask */
109 PORT_CMD = 0x18, /* port command */
110 PORT_TFDATA = 0x20, /* taskfile data */
111 PORT_SIG = 0x24, /* device TF signature */
112 PORT_CMD_ISSUE = 0x38, /* command issue */
113 PORT_SCR = 0x28, /* SATA phy register block */
114 PORT_SCR_STAT = 0x28, /* SATA phy register: SStatus */
115 PORT_SCR_CTL = 0x2c, /* SATA phy register: SControl */
116 PORT_SCR_ERR = 0x30, /* SATA phy register: SError */
117 PORT_SCR_ACT = 0x34, /* SATA phy register: SActive */
118
119 /* PORT_IRQ_{STAT,MASK} bits */
120 PORT_IRQ_COLD_PRES = (1 << 31), /* cold presence detect */
121 PORT_IRQ_TF_ERR = (1 << 30), /* task file error */
122 PORT_IRQ_HBUS_ERR = (1 << 29), /* host bus fatal error */
123 PORT_IRQ_HBUS_DATA_ERR = (1 << 28), /* host bus data error */
124 PORT_IRQ_IF_ERR = (1 << 27), /* interface fatal error */
125 PORT_IRQ_IF_NONFATAL = (1 << 26), /* interface non-fatal error */
126 PORT_IRQ_OVERFLOW = (1 << 24), /* xfer exhausted available S/G */
127 PORT_IRQ_BAD_PMP = (1 << 23), /* incorrect port multiplier */
128
129 PORT_IRQ_PHYRDY = (1 << 22), /* PhyRdy changed */
130 PORT_IRQ_DEV_ILCK = (1 << 7), /* device interlock */
131 PORT_IRQ_CONNECT = (1 << 6), /* port connect change status */
132 PORT_IRQ_SG_DONE = (1 << 5), /* descriptor processed */
133 PORT_IRQ_UNK_FIS = (1 << 4), /* unknown FIS rx'd */
134 PORT_IRQ_SDB_FIS = (1 << 3), /* Set Device Bits FIS rx'd */
135 PORT_IRQ_DMAS_FIS = (1 << 2), /* DMA Setup FIS rx'd */
136 PORT_IRQ_PIOS_FIS = (1 << 1), /* PIO Setup FIS rx'd */
137 PORT_IRQ_D2H_REG_FIS = (1 << 0), /* D2H Register FIS rx'd */
138
Tejun Heo78cd52d2006-05-15 20:58:29 +0900139 PORT_IRQ_FREEZE = PORT_IRQ_HBUS_ERR |
140 PORT_IRQ_IF_ERR |
141 PORT_IRQ_CONNECT |
Tejun Heo42969712006-05-31 18:28:18 +0900142 PORT_IRQ_PHYRDY |
Tejun Heo78cd52d2006-05-15 20:58:29 +0900143 PORT_IRQ_UNK_FIS,
144 PORT_IRQ_ERROR = PORT_IRQ_FREEZE |
145 PORT_IRQ_TF_ERR |
146 PORT_IRQ_HBUS_DATA_ERR,
147 DEF_PORT_IRQ = PORT_IRQ_ERROR | PORT_IRQ_SG_DONE |
148 PORT_IRQ_SDB_FIS | PORT_IRQ_DMAS_FIS |
149 PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150
151 /* PORT_CMD bits */
Jeff Garzik02eaa662005-11-12 01:32:19 -0500152 PORT_CMD_ATAPI = (1 << 24), /* Device is ATAPI */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153 PORT_CMD_LIST_ON = (1 << 15), /* cmd list DMA engine running */
154 PORT_CMD_FIS_ON = (1 << 14), /* FIS DMA engine running */
155 PORT_CMD_FIS_RX = (1 << 4), /* Enable FIS receive DMA engine */
Tejun Heo22b49982006-01-23 21:38:44 +0900156 PORT_CMD_CLO = (1 << 3), /* Command list override */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 PORT_CMD_POWER_ON = (1 << 2), /* Power up device */
158 PORT_CMD_SPIN_UP = (1 << 1), /* Spin up device */
159 PORT_CMD_START = (1 << 0), /* Enable port DMA engine */
160
Tejun Heo0be0aa92006-07-26 15:59:26 +0900161 PORT_CMD_ICC_MASK = (0xf << 28), /* i/f ICC state mask */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162 PORT_CMD_ICC_ACTIVE = (0x1 << 28), /* Put i/f in active state */
163 PORT_CMD_ICC_PARTIAL = (0x2 << 28), /* Put i/f in partial state */
164 PORT_CMD_ICC_SLUMBER = (0x6 << 28), /* Put i/f in slumber state */
Jeff Garzik4b0060f2005-06-04 00:50:22 -0400165
166 /* hpriv->flags bits */
167 AHCI_FLAG_MSI = (1 << 0),
Bastiaan Jacquesbf2af2a2006-04-17 14:17:59 +0200168
169 /* ap->flags bits */
Tejun Heo4aeb0e32006-11-01 17:58:33 +0900170 AHCI_FLAG_NO_NCQ = (1 << 24),
171 AHCI_FLAG_IGN_IRQ_IF_ERR = (1 << 25), /* ignore IRQ_IF_ERR */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172};
173
174struct ahci_cmd_hdr {
175 u32 opts;
176 u32 status;
177 u32 tbl_addr;
178 u32 tbl_addr_hi;
179 u32 reserved[4];
180};
181
182struct ahci_sg {
183 u32 addr;
184 u32 addr_hi;
185 u32 reserved;
186 u32 flags_size;
187};
188
189struct ahci_host_priv {
190 unsigned long flags;
191 u32 cap; /* cache of HOST_CAP register */
192 u32 port_map; /* cache of HOST_PORTS_IMPL reg */
193};
194
195struct ahci_port_priv {
196 struct ahci_cmd_hdr *cmd_slot;
197 dma_addr_t cmd_slot_dma;
198 void *cmd_tbl;
199 dma_addr_t cmd_tbl_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 void *rx_fis;
201 dma_addr_t rx_fis_dma;
202};
203
204static u32 ahci_scr_read (struct ata_port *ap, unsigned int sc_reg);
205static void ahci_scr_write (struct ata_port *ap, unsigned int sc_reg, u32 val);
206static int ahci_init_one (struct pci_dev *pdev, const struct pci_device_id *ent);
Tejun Heo9a3d9eb2006-01-23 13:09:36 +0900207static unsigned int ahci_qc_issue(struct ata_queued_cmd *qc);
David Howells7d12e782006-10-05 14:55:46 +0100208static irqreturn_t ahci_interrupt (int irq, void *dev_instance);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209static void ahci_irq_clear(struct ata_port *ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210static int ahci_port_start(struct ata_port *ap);
211static void ahci_port_stop(struct ata_port *ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212static void ahci_tf_read(struct ata_port *ap, struct ata_taskfile *tf);
213static void ahci_qc_prep(struct ata_queued_cmd *qc);
214static u8 ahci_check_status(struct ata_port *ap);
Tejun Heo78cd52d2006-05-15 20:58:29 +0900215static void ahci_freeze(struct ata_port *ap);
216static void ahci_thaw(struct ata_port *ap);
217static void ahci_error_handler(struct ata_port *ap);
Tejun Heoad616ff2006-11-01 18:00:24 +0900218static void ahci_vt8251_error_handler(struct ata_port *ap);
Tejun Heo78cd52d2006-05-15 20:58:29 +0900219static void ahci_post_internal_cmd(struct ata_queued_cmd *qc);
Tejun Heoc1332872006-07-26 15:59:26 +0900220static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg);
221static int ahci_port_resume(struct ata_port *ap);
222static int ahci_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg);
223static int ahci_pci_device_resume(struct pci_dev *pdev);
Jeff Garzik907f4672005-05-12 15:03:42 -0400224static void ahci_remove_one (struct pci_dev *pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225
Jeff Garzik193515d2005-11-07 00:59:37 -0500226static struct scsi_host_template ahci_sht = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 .module = THIS_MODULE,
228 .name = DRV_NAME,
229 .ioctl = ata_scsi_ioctl,
230 .queuecommand = ata_scsi_queuecmd,
Tejun Heo12fad3f2006-05-15 21:03:55 +0900231 .change_queue_depth = ata_scsi_change_queue_depth,
232 .can_queue = AHCI_MAX_CMDS - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 .this_id = ATA_SHT_THIS_ID,
234 .sg_tablesize = AHCI_MAX_SG,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 .cmd_per_lun = ATA_SHT_CMD_PER_LUN,
236 .emulated = ATA_SHT_EMULATED,
237 .use_clustering = AHCI_USE_CLUSTERING,
238 .proc_name = DRV_NAME,
239 .dma_boundary = AHCI_DMA_BOUNDARY,
240 .slave_configure = ata_scsi_slave_config,
Tejun Heoccf68c32006-05-31 18:28:09 +0900241 .slave_destroy = ata_scsi_slave_destroy,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 .bios_param = ata_std_bios_param,
Tejun Heoc1332872006-07-26 15:59:26 +0900243 .suspend = ata_scsi_device_suspend,
244 .resume = ata_scsi_device_resume,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245};
246
Jeff Garzik057ace52005-10-22 14:27:05 -0400247static const struct ata_port_operations ahci_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 .port_disable = ata_port_disable,
249
250 .check_status = ahci_check_status,
251 .check_altstatus = ahci_check_status,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 .dev_select = ata_noop_dev_select,
253
254 .tf_read = ahci_tf_read,
255
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 .qc_prep = ahci_qc_prep,
257 .qc_issue = ahci_qc_issue,
258
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 .irq_handler = ahci_interrupt,
260 .irq_clear = ahci_irq_clear,
261
262 .scr_read = ahci_scr_read,
263 .scr_write = ahci_scr_write,
264
Tejun Heo78cd52d2006-05-15 20:58:29 +0900265 .freeze = ahci_freeze,
266 .thaw = ahci_thaw,
267
268 .error_handler = ahci_error_handler,
269 .post_internal_cmd = ahci_post_internal_cmd,
270
Tejun Heoc1332872006-07-26 15:59:26 +0900271 .port_suspend = ahci_port_suspend,
272 .port_resume = ahci_port_resume,
273
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 .port_start = ahci_port_start,
275 .port_stop = ahci_port_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276};
277
Tejun Heoad616ff2006-11-01 18:00:24 +0900278static const struct ata_port_operations ahci_vt8251_ops = {
279 .port_disable = ata_port_disable,
280
281 .check_status = ahci_check_status,
282 .check_altstatus = ahci_check_status,
283 .dev_select = ata_noop_dev_select,
284
285 .tf_read = ahci_tf_read,
286
287 .qc_prep = ahci_qc_prep,
288 .qc_issue = ahci_qc_issue,
289
290 .irq_handler = ahci_interrupt,
291 .irq_clear = ahci_irq_clear,
292
293 .scr_read = ahci_scr_read,
294 .scr_write = ahci_scr_write,
295
296 .freeze = ahci_freeze,
297 .thaw = ahci_thaw,
298
299 .error_handler = ahci_vt8251_error_handler,
300 .post_internal_cmd = ahci_post_internal_cmd,
301
302 .port_suspend = ahci_port_suspend,
303 .port_resume = ahci_port_resume,
304
305 .port_start = ahci_port_start,
306 .port_stop = ahci_port_stop,
307};
308
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100309static const struct ata_port_info ahci_port_info[] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 /* board_ahci */
311 {
312 .sht = &ahci_sht,
Jeff Garzikcca39742006-08-24 03:19:22 -0400313 .flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
Tejun Heo42969712006-05-31 18:28:18 +0900314 ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA |
315 ATA_FLAG_SKIP_D2H_BSY,
Brett Russ7da79312005-09-01 21:53:34 -0400316 .pio_mask = 0x1f, /* pio0-4 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 .udma_mask = 0x7f, /* udma0-6 ; FIXME */
318 .port_ops = &ahci_ops,
319 },
Bastiaan Jacquesbf2af2a2006-04-17 14:17:59 +0200320 /* board_ahci_vt8251 */
321 {
322 .sht = &ahci_sht,
Jeff Garzikcca39742006-08-24 03:19:22 -0400323 .flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
Bastiaan Jacquesbf2af2a2006-04-17 14:17:59 +0200324 ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA |
Tejun Heoad616ff2006-11-01 18:00:24 +0900325 ATA_FLAG_SKIP_D2H_BSY |
326 ATA_FLAG_HRST_TO_RESUME | AHCI_FLAG_NO_NCQ,
Bastiaan Jacquesbf2af2a2006-04-17 14:17:59 +0200327 .pio_mask = 0x1f, /* pio0-4 */
328 .udma_mask = 0x7f, /* udma0-6 ; FIXME */
Tejun Heoad616ff2006-11-01 18:00:24 +0900329 .port_ops = &ahci_vt8251_ops,
Bastiaan Jacquesbf2af2a2006-04-17 14:17:59 +0200330 },
Tejun Heo41669552006-11-29 11:33:14 +0900331 /* board_ahci_ign_iferr */
332 {
333 .sht = &ahci_sht,
334 .flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
335 ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA |
336 ATA_FLAG_SKIP_D2H_BSY |
337 AHCI_FLAG_IGN_IRQ_IF_ERR,
338 .pio_mask = 0x1f, /* pio0-4 */
339 .udma_mask = 0x7f, /* udma0-6 ; FIXME */
340 .port_ops = &ahci_ops,
341 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342};
343
Jeff Garzik3b7d6972005-11-10 11:04:11 -0500344static const struct pci_device_id ahci_pci_tbl[] = {
Jeff Garzikfe7fa312006-06-22 23:05:36 -0400345 /* Intel */
Jeff Garzik54bb3a92006-09-27 22:20:11 -0400346 { PCI_VDEVICE(INTEL, 0x2652), board_ahci }, /* ICH6 */
347 { PCI_VDEVICE(INTEL, 0x2653), board_ahci }, /* ICH6M */
348 { PCI_VDEVICE(INTEL, 0x27c1), board_ahci }, /* ICH7 */
349 { PCI_VDEVICE(INTEL, 0x27c5), board_ahci }, /* ICH7M */
350 { PCI_VDEVICE(INTEL, 0x27c3), board_ahci }, /* ICH7R */
351 { PCI_VDEVICE(AL, 0x5288), board_ahci }, /* ULi M5288 */
352 { PCI_VDEVICE(INTEL, 0x2681), board_ahci }, /* ESB2 */
353 { PCI_VDEVICE(INTEL, 0x2682), board_ahci }, /* ESB2 */
354 { PCI_VDEVICE(INTEL, 0x2683), board_ahci }, /* ESB2 */
355 { PCI_VDEVICE(INTEL, 0x27c6), board_ahci }, /* ICH7-M DH */
356 { PCI_VDEVICE(INTEL, 0x2821), board_ahci }, /* ICH8 */
357 { PCI_VDEVICE(INTEL, 0x2822), board_ahci }, /* ICH8 */
358 { PCI_VDEVICE(INTEL, 0x2824), board_ahci }, /* ICH8 */
359 { PCI_VDEVICE(INTEL, 0x2829), board_ahci }, /* ICH8M */
360 { PCI_VDEVICE(INTEL, 0x282a), board_ahci }, /* ICH8M */
Jason Gastonf33d6252006-11-21 16:55:58 -0800361 { PCI_VDEVICE(INTEL, 0x2922), board_ahci }, /* ICH9 */
362 { PCI_VDEVICE(INTEL, 0x2923), board_ahci }, /* ICH9 */
363 { PCI_VDEVICE(INTEL, 0x2924), board_ahci }, /* ICH9 */
364 { PCI_VDEVICE(INTEL, 0x2925), board_ahci }, /* ICH9 */
365 { PCI_VDEVICE(INTEL, 0x2927), board_ahci }, /* ICH9 */
366 { PCI_VDEVICE(INTEL, 0x2929), board_ahci }, /* ICH9M */
367 { PCI_VDEVICE(INTEL, 0x292a), board_ahci }, /* ICH9M */
368 { PCI_VDEVICE(INTEL, 0x292b), board_ahci }, /* ICH9M */
369 { PCI_VDEVICE(INTEL, 0x292f), board_ahci }, /* ICH9M */
370 { PCI_VDEVICE(INTEL, 0x294d), board_ahci }, /* ICH9 */
371 { PCI_VDEVICE(INTEL, 0x294e), board_ahci }, /* ICH9M */
Jeff Garzikfe7fa312006-06-22 23:05:36 -0400372
373 /* JMicron */
Tejun Heo41669552006-11-29 11:33:14 +0900374 { PCI_VDEVICE(JMICRON, 0x2360), board_ahci_ign_iferr }, /* JMB360 */
375 { PCI_VDEVICE(JMICRON, 0x2361), board_ahci_ign_iferr }, /* JMB361 */
376 { PCI_VDEVICE(JMICRON, 0x2363), board_ahci_ign_iferr }, /* JMB363 */
377 { PCI_VDEVICE(JMICRON, 0x2365), board_ahci_ign_iferr }, /* JMB365 */
378 { PCI_VDEVICE(JMICRON, 0x2366), board_ahci_ign_iferr }, /* JMB366 */
Jeff Garzikfe7fa312006-06-22 23:05:36 -0400379
380 /* ATI */
Jeff Garzik54bb3a92006-09-27 22:20:11 -0400381 { PCI_VDEVICE(ATI, 0x4380), board_ahci }, /* ATI SB600 non-raid */
382 { PCI_VDEVICE(ATI, 0x4381), board_ahci }, /* ATI SB600 raid */
Jeff Garzikfe7fa312006-06-22 23:05:36 -0400383
384 /* VIA */
Jeff Garzik54bb3a92006-09-27 22:20:11 -0400385 { PCI_VDEVICE(VIA, 0x3349), board_ahci_vt8251 }, /* VIA VT8251 */
Jeff Garzikfe7fa312006-06-22 23:05:36 -0400386
387 /* NVIDIA */
Jeff Garzik54bb3a92006-09-27 22:20:11 -0400388 { PCI_VDEVICE(NVIDIA, 0x044c), board_ahci }, /* MCP65 */
389 { PCI_VDEVICE(NVIDIA, 0x044d), board_ahci }, /* MCP65 */
390 { PCI_VDEVICE(NVIDIA, 0x044e), board_ahci }, /* MCP65 */
391 { PCI_VDEVICE(NVIDIA, 0x044f), board_ahci }, /* MCP65 */
Peer Chen895663c2006-11-02 17:59:46 -0500392 { PCI_VDEVICE(NVIDIA, 0x0554), board_ahci }, /* MCP67 */
393 { PCI_VDEVICE(NVIDIA, 0x0555), board_ahci }, /* MCP67 */
394 { PCI_VDEVICE(NVIDIA, 0x0556), board_ahci }, /* MCP67 */
395 { PCI_VDEVICE(NVIDIA, 0x0557), board_ahci }, /* MCP67 */
396 { PCI_VDEVICE(NVIDIA, 0x0558), board_ahci }, /* MCP67 */
397 { PCI_VDEVICE(NVIDIA, 0x0559), board_ahci }, /* MCP67 */
398 { PCI_VDEVICE(NVIDIA, 0x055a), board_ahci }, /* MCP67 */
399 { PCI_VDEVICE(NVIDIA, 0x055b), board_ahci }, /* MCP67 */
Jeff Garzikfe7fa312006-06-22 23:05:36 -0400400
Jeff Garzik95916ed2006-07-29 04:10:14 -0400401 /* SiS */
Jeff Garzik54bb3a92006-09-27 22:20:11 -0400402 { PCI_VDEVICE(SI, 0x1184), board_ahci }, /* SiS 966 */
403 { PCI_VDEVICE(SI, 0x1185), board_ahci }, /* SiS 966 */
404 { PCI_VDEVICE(SI, 0x0186), board_ahci }, /* SiS 968 */
Jeff Garzik95916ed2006-07-29 04:10:14 -0400405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 { } /* terminate list */
407};
408
409
410static struct pci_driver ahci_pci_driver = {
411 .name = DRV_NAME,
412 .id_table = ahci_pci_tbl,
413 .probe = ahci_init_one,
Tejun Heoc1332872006-07-26 15:59:26 +0900414 .suspend = ahci_pci_device_suspend,
415 .resume = ahci_pci_device_resume,
Jeff Garzik907f4672005-05-12 15:03:42 -0400416 .remove = ahci_remove_one,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417};
418
419
420static inline unsigned long ahci_port_base_ul (unsigned long base, unsigned int port)
421{
422 return base + 0x100 + (port * 0x80);
423}
424
Jeff Garzikea6ba102005-08-30 05:18:18 -0400425static inline void __iomem *ahci_port_base (void __iomem *base, unsigned int port)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426{
Jeff Garzikea6ba102005-08-30 05:18:18 -0400427 return (void __iomem *) ahci_port_base_ul((unsigned long)base, port);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428}
429
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430static u32 ahci_scr_read (struct ata_port *ap, unsigned int sc_reg_in)
431{
432 unsigned int sc_reg;
433
434 switch (sc_reg_in) {
435 case SCR_STATUS: sc_reg = 0; break;
436 case SCR_CONTROL: sc_reg = 1; break;
437 case SCR_ERROR: sc_reg = 2; break;
438 case SCR_ACTIVE: sc_reg = 3; break;
439 default:
440 return 0xffffffffU;
441 }
442
Al Viro1e4f2a92005-10-21 06:46:02 +0100443 return readl((void __iomem *) ap->ioaddr.scr_addr + (sc_reg * 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444}
445
446
447static void ahci_scr_write (struct ata_port *ap, unsigned int sc_reg_in,
448 u32 val)
449{
450 unsigned int sc_reg;
451
452 switch (sc_reg_in) {
453 case SCR_STATUS: sc_reg = 0; break;
454 case SCR_CONTROL: sc_reg = 1; break;
455 case SCR_ERROR: sc_reg = 2; break;
456 case SCR_ACTIVE: sc_reg = 3; break;
457 default:
458 return;
459 }
460
Al Viro1e4f2a92005-10-21 06:46:02 +0100461 writel(val, (void __iomem *) ap->ioaddr.scr_addr + (sc_reg * 4));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462}
463
Tejun Heo9f592052006-07-26 15:59:26 +0900464static void ahci_start_engine(void __iomem *port_mmio)
Tejun Heo7c76d1e2005-12-19 22:36:34 +0900465{
Tejun Heo7c76d1e2005-12-19 22:36:34 +0900466 u32 tmp;
467
Tejun Heod8fcd112006-07-26 15:59:25 +0900468 /* start DMA */
Tejun Heo9f592052006-07-26 15:59:26 +0900469 tmp = readl(port_mmio + PORT_CMD);
Tejun Heo7c76d1e2005-12-19 22:36:34 +0900470 tmp |= PORT_CMD_START;
471 writel(tmp, port_mmio + PORT_CMD);
472 readl(port_mmio + PORT_CMD); /* flush */
473}
474
Tejun Heo254950c2006-07-26 15:59:25 +0900475static int ahci_stop_engine(void __iomem *port_mmio)
476{
477 u32 tmp;
478
479 tmp = readl(port_mmio + PORT_CMD);
480
Tejun Heod8fcd112006-07-26 15:59:25 +0900481 /* check if the HBA is idle */
Tejun Heo254950c2006-07-26 15:59:25 +0900482 if ((tmp & (PORT_CMD_START | PORT_CMD_LIST_ON)) == 0)
483 return 0;
484
Tejun Heod8fcd112006-07-26 15:59:25 +0900485 /* setting HBA to idle */
Tejun Heo254950c2006-07-26 15:59:25 +0900486 tmp &= ~PORT_CMD_START;
487 writel(tmp, port_mmio + PORT_CMD);
488
Tejun Heod8fcd112006-07-26 15:59:25 +0900489 /* wait for engine to stop. This could be as long as 500 msec */
Tejun Heo254950c2006-07-26 15:59:25 +0900490 tmp = ata_wait_register(port_mmio + PORT_CMD,
491 PORT_CMD_LIST_ON, PORT_CMD_LIST_ON, 1, 500);
Tejun Heod8fcd112006-07-26 15:59:25 +0900492 if (tmp & PORT_CMD_LIST_ON)
Tejun Heo254950c2006-07-26 15:59:25 +0900493 return -EIO;
494
495 return 0;
496}
497
Tejun Heo0be0aa92006-07-26 15:59:26 +0900498static void ahci_start_fis_rx(void __iomem *port_mmio, u32 cap,
499 dma_addr_t cmd_slot_dma, dma_addr_t rx_fis_dma)
500{
501 u32 tmp;
502
503 /* set FIS registers */
504 if (cap & HOST_CAP_64)
505 writel((cmd_slot_dma >> 16) >> 16, port_mmio + PORT_LST_ADDR_HI);
506 writel(cmd_slot_dma & 0xffffffff, port_mmio + PORT_LST_ADDR);
507
508 if (cap & HOST_CAP_64)
509 writel((rx_fis_dma >> 16) >> 16, port_mmio + PORT_FIS_ADDR_HI);
510 writel(rx_fis_dma & 0xffffffff, port_mmio + PORT_FIS_ADDR);
511
512 /* enable FIS reception */
513 tmp = readl(port_mmio + PORT_CMD);
514 tmp |= PORT_CMD_FIS_RX;
515 writel(tmp, port_mmio + PORT_CMD);
516
517 /* flush */
518 readl(port_mmio + PORT_CMD);
519}
520
521static int ahci_stop_fis_rx(void __iomem *port_mmio)
522{
523 u32 tmp;
524
525 /* disable FIS reception */
526 tmp = readl(port_mmio + PORT_CMD);
527 tmp &= ~PORT_CMD_FIS_RX;
528 writel(tmp, port_mmio + PORT_CMD);
529
530 /* wait for completion, spec says 500ms, give it 1000 */
531 tmp = ata_wait_register(port_mmio + PORT_CMD, PORT_CMD_FIS_ON,
532 PORT_CMD_FIS_ON, 10, 1000);
533 if (tmp & PORT_CMD_FIS_ON)
534 return -EBUSY;
535
536 return 0;
537}
538
539static void ahci_power_up(void __iomem *port_mmio, u32 cap)
540{
541 u32 cmd;
542
543 cmd = readl(port_mmio + PORT_CMD) & ~PORT_CMD_ICC_MASK;
544
545 /* spin up device */
546 if (cap & HOST_CAP_SSS) {
547 cmd |= PORT_CMD_SPIN_UP;
548 writel(cmd, port_mmio + PORT_CMD);
549 }
550
551 /* wake up link */
552 writel(cmd | PORT_CMD_ICC_ACTIVE, port_mmio + PORT_CMD);
553}
554
555static void ahci_power_down(void __iomem *port_mmio, u32 cap)
556{
557 u32 cmd, scontrol;
558
559 cmd = readl(port_mmio + PORT_CMD) & ~PORT_CMD_ICC_MASK;
560
561 if (cap & HOST_CAP_SSC) {
562 /* enable transitions to slumber mode */
563 scontrol = readl(port_mmio + PORT_SCR_CTL);
564 if ((scontrol & 0x0f00) > 0x100) {
565 scontrol &= ~0xf00;
566 writel(scontrol, port_mmio + PORT_SCR_CTL);
567 }
568
569 /* put device into slumber mode */
570 writel(cmd | PORT_CMD_ICC_SLUMBER, port_mmio + PORT_CMD);
571
572 /* wait for the transition to complete */
573 ata_wait_register(port_mmio + PORT_CMD, PORT_CMD_ICC_SLUMBER,
574 PORT_CMD_ICC_SLUMBER, 1, 50);
575 }
576
577 /* put device into listen mode */
578 if (cap & HOST_CAP_SSS) {
579 /* first set PxSCTL.DET to 0 */
580 scontrol = readl(port_mmio + PORT_SCR_CTL);
581 scontrol &= ~0xf;
582 writel(scontrol, port_mmio + PORT_SCR_CTL);
583
584 /* then set PxCMD.SUD to 0 */
585 cmd &= ~PORT_CMD_SPIN_UP;
586 writel(cmd, port_mmio + PORT_CMD);
587 }
588}
589
590static void ahci_init_port(void __iomem *port_mmio, u32 cap,
591 dma_addr_t cmd_slot_dma, dma_addr_t rx_fis_dma)
592{
593 /* power up */
594 ahci_power_up(port_mmio, cap);
595
596 /* enable FIS reception */
597 ahci_start_fis_rx(port_mmio, cap, cmd_slot_dma, rx_fis_dma);
598
599 /* enable DMA */
600 ahci_start_engine(port_mmio);
601}
602
603static int ahci_deinit_port(void __iomem *port_mmio, u32 cap, const char **emsg)
604{
605 int rc;
606
607 /* disable DMA */
608 rc = ahci_stop_engine(port_mmio);
609 if (rc) {
610 *emsg = "failed to stop engine";
611 return rc;
612 }
613
614 /* disable FIS reception */
615 rc = ahci_stop_fis_rx(port_mmio);
616 if (rc) {
617 *emsg = "failed stop FIS RX";
618 return rc;
619 }
620
621 /* put device into slumber mode */
622 ahci_power_down(port_mmio, cap);
623
624 return 0;
625}
626
Tejun Heod91542c2006-07-26 15:59:26 +0900627static int ahci_reset_controller(void __iomem *mmio, struct pci_dev *pdev)
628{
629 u32 cap_save, tmp;
630
631 cap_save = readl(mmio + HOST_CAP);
632 cap_save &= ( (1<<28) | (1<<17) );
633 cap_save |= (1 << 27);
634
635 /* global controller reset */
636 tmp = readl(mmio + HOST_CTL);
637 if ((tmp & HOST_RESET) == 0) {
638 writel(tmp | HOST_RESET, mmio + HOST_CTL);
639 readl(mmio + HOST_CTL); /* flush */
640 }
641
642 /* reset must complete within 1 second, or
643 * the hardware should be considered fried.
644 */
645 ssleep(1);
646
647 tmp = readl(mmio + HOST_CTL);
648 if (tmp & HOST_RESET) {
649 dev_printk(KERN_ERR, &pdev->dev,
650 "controller reset failed (0x%x)\n", tmp);
651 return -EIO;
652 }
653
654 writel(HOST_AHCI_EN, mmio + HOST_CTL);
655 (void) readl(mmio + HOST_CTL); /* flush */
656 writel(cap_save, mmio + HOST_CAP);
657 writel(0xf, mmio + HOST_PORTS_IMPL);
658 (void) readl(mmio + HOST_PORTS_IMPL); /* flush */
659
660 if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
661 u16 tmp16;
662
663 /* configure PCS */
664 pci_read_config_word(pdev, 0x92, &tmp16);
665 tmp16 |= 0xf;
666 pci_write_config_word(pdev, 0x92, tmp16);
667 }
668
669 return 0;
670}
671
672static void ahci_init_controller(void __iomem *mmio, struct pci_dev *pdev,
673 int n_ports, u32 cap)
674{
675 int i, rc;
676 u32 tmp;
677
678 for (i = 0; i < n_ports; i++) {
679 void __iomem *port_mmio = ahci_port_base(mmio, i);
680 const char *emsg = NULL;
681
682#if 0 /* BIOSen initialize this incorrectly */
683 if (!(hpriv->port_map & (1 << i)))
684 continue;
685#endif
686
687 /* make sure port is not active */
688 rc = ahci_deinit_port(port_mmio, cap, &emsg);
689 if (rc)
690 dev_printk(KERN_WARNING, &pdev->dev,
691 "%s (%d)\n", emsg, rc);
692
693 /* clear SError */
694 tmp = readl(port_mmio + PORT_SCR_ERR);
695 VPRINTK("PORT_SCR_ERR 0x%x\n", tmp);
696 writel(tmp, port_mmio + PORT_SCR_ERR);
697
Tejun Heof4b5cc82006-08-07 11:39:04 +0900698 /* clear port IRQ */
Tejun Heod91542c2006-07-26 15:59:26 +0900699 tmp = readl(port_mmio + PORT_IRQ_STAT);
700 VPRINTK("PORT_IRQ_STAT 0x%x\n", tmp);
701 if (tmp)
702 writel(tmp, port_mmio + PORT_IRQ_STAT);
703
704 writel(1 << i, mmio + HOST_IRQ_STAT);
Tejun Heod91542c2006-07-26 15:59:26 +0900705 }
706
707 tmp = readl(mmio + HOST_CTL);
708 VPRINTK("HOST_CTL 0x%x\n", tmp);
709 writel(tmp | HOST_IRQ_EN, mmio + HOST_CTL);
710 tmp = readl(mmio + HOST_CTL);
711 VPRINTK("HOST_CTL 0x%x\n", tmp);
712}
713
Tejun Heo422b7592005-12-19 22:37:17 +0900714static unsigned int ahci_dev_classify(struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715{
716 void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
717 struct ata_taskfile tf;
Tejun Heo422b7592005-12-19 22:37:17 +0900718 u32 tmp;
719
720 tmp = readl(port_mmio + PORT_SIG);
721 tf.lbah = (tmp >> 24) & 0xff;
722 tf.lbam = (tmp >> 16) & 0xff;
723 tf.lbal = (tmp >> 8) & 0xff;
724 tf.nsect = (tmp) & 0xff;
725
726 return ata_dev_classify(&tf);
727}
728
Tejun Heo12fad3f2006-05-15 21:03:55 +0900729static void ahci_fill_cmd_slot(struct ahci_port_priv *pp, unsigned int tag,
730 u32 opts)
Tejun Heocc9278e2006-02-10 17:25:47 +0900731{
Tejun Heo12fad3f2006-05-15 21:03:55 +0900732 dma_addr_t cmd_tbl_dma;
733
734 cmd_tbl_dma = pp->cmd_tbl_dma + tag * AHCI_CMD_TBL_SZ;
735
736 pp->cmd_slot[tag].opts = cpu_to_le32(opts);
737 pp->cmd_slot[tag].status = 0;
738 pp->cmd_slot[tag].tbl_addr = cpu_to_le32(cmd_tbl_dma & 0xffffffff);
739 pp->cmd_slot[tag].tbl_addr_hi = cpu_to_le32((cmd_tbl_dma >> 16) >> 16);
Tejun Heocc9278e2006-02-10 17:25:47 +0900740}
741
Bastiaan Jacquesbf2af2a2006-04-17 14:17:59 +0200742static int ahci_clo(struct ata_port *ap)
743{
744 void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
Jeff Garzikcca39742006-08-24 03:19:22 -0400745 struct ahci_host_priv *hpriv = ap->host->private_data;
Bastiaan Jacquesbf2af2a2006-04-17 14:17:59 +0200746 u32 tmp;
747
748 if (!(hpriv->cap & HOST_CAP_CLO))
749 return -EOPNOTSUPP;
750
751 tmp = readl(port_mmio + PORT_CMD);
752 tmp |= PORT_CMD_CLO;
753 writel(tmp, port_mmio + PORT_CMD);
754
755 tmp = ata_wait_register(port_mmio + PORT_CMD,
756 PORT_CMD_CLO, PORT_CMD_CLO, 1, 500);
757 if (tmp & PORT_CMD_CLO)
758 return -EIO;
759
760 return 0;
761}
762
Tejun Heo2bf2cb22006-04-11 22:16:45 +0900763static int ahci_softreset(struct ata_port *ap, unsigned int *class)
Tejun Heo4658f792006-03-22 21:07:03 +0900764{
Tejun Heo4658f792006-03-22 21:07:03 +0900765 struct ahci_port_priv *pp = ap->private_data;
Jeff Garzikcca39742006-08-24 03:19:22 -0400766 void __iomem *mmio = ap->host->mmio_base;
Tejun Heo4658f792006-03-22 21:07:03 +0900767 void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
768 const u32 cmd_fis_len = 5; /* five dwords */
769 const char *reason = NULL;
770 struct ata_taskfile tf;
Tejun Heo75fe1802006-04-11 22:22:29 +0900771 u32 tmp;
Tejun Heo4658f792006-03-22 21:07:03 +0900772 u8 *fis;
773 int rc;
774
775 DPRINTK("ENTER\n");
776
Tejun Heo81952c52006-05-15 20:57:47 +0900777 if (ata_port_offline(ap)) {
Tejun Heoc2a65852006-04-03 01:58:06 +0900778 DPRINTK("PHY reports no device\n");
779 *class = ATA_DEV_NONE;
780 return 0;
781 }
782
Tejun Heo4658f792006-03-22 21:07:03 +0900783 /* prepare for SRST (AHCI-1.1 10.4.1) */
zhao, forrest5457f212006-07-13 13:38:32 +0800784 rc = ahci_stop_engine(port_mmio);
Tejun Heo4658f792006-03-22 21:07:03 +0900785 if (rc) {
786 reason = "failed to stop engine";
787 goto fail_restart;
788 }
789
790 /* check BUSY/DRQ, perform Command List Override if necessary */
Tejun Heo1244a192006-11-01 17:19:18 +0900791 if (ahci_check_status(ap) & (ATA_BUSY | ATA_DRQ)) {
Bastiaan Jacquesbf2af2a2006-04-17 14:17:59 +0200792 rc = ahci_clo(ap);
793
794 if (rc == -EOPNOTSUPP) {
795 reason = "port busy but CLO unavailable";
Tejun Heo4658f792006-03-22 21:07:03 +0900796 goto fail_restart;
Bastiaan Jacquesbf2af2a2006-04-17 14:17:59 +0200797 } else if (rc) {
798 reason = "port busy but CLO failed";
Tejun Heo4658f792006-03-22 21:07:03 +0900799 goto fail_restart;
800 }
801 }
802
803 /* restart engine */
zhao, forrest5457f212006-07-13 13:38:32 +0800804 ahci_start_engine(port_mmio);
Tejun Heo4658f792006-03-22 21:07:03 +0900805
Tejun Heo3373efd2006-05-15 20:57:53 +0900806 ata_tf_init(ap->device, &tf);
Tejun Heo4658f792006-03-22 21:07:03 +0900807 fis = pp->cmd_tbl;
808
809 /* issue the first D2H Register FIS */
Tejun Heo12fad3f2006-05-15 21:03:55 +0900810 ahci_fill_cmd_slot(pp, 0,
811 cmd_fis_len | AHCI_CMD_RESET | AHCI_CMD_CLR_BUSY);
Tejun Heo4658f792006-03-22 21:07:03 +0900812
813 tf.ctl |= ATA_SRST;
814 ata_tf_to_fis(&tf, fis, 0);
815 fis[1] &= ~(1 << 7); /* turn off Command FIS bit */
816
817 writel(1, port_mmio + PORT_CMD_ISSUE);
Tejun Heo4658f792006-03-22 21:07:03 +0900818
Tejun Heo75fe1802006-04-11 22:22:29 +0900819 tmp = ata_wait_register(port_mmio + PORT_CMD_ISSUE, 0x1, 0x1, 1, 500);
820 if (tmp & 0x1) {
Tejun Heo4658f792006-03-22 21:07:03 +0900821 rc = -EIO;
822 reason = "1st FIS failed";
823 goto fail;
824 }
825
826 /* spec says at least 5us, but be generous and sleep for 1ms */
827 msleep(1);
828
829 /* issue the second D2H Register FIS */
Tejun Heo12fad3f2006-05-15 21:03:55 +0900830 ahci_fill_cmd_slot(pp, 0, cmd_fis_len);
Tejun Heo4658f792006-03-22 21:07:03 +0900831
832 tf.ctl &= ~ATA_SRST;
833 ata_tf_to_fis(&tf, fis, 0);
834 fis[1] &= ~(1 << 7); /* turn off Command FIS bit */
835
836 writel(1, port_mmio + PORT_CMD_ISSUE);
837 readl(port_mmio + PORT_CMD_ISSUE); /* flush */
838
839 /* spec mandates ">= 2ms" before checking status.
840 * We wait 150ms, because that was the magic delay used for
841 * ATAPI devices in Hale Landis's ATADRVR, for the period of time
842 * between when the ATA command register is written, and then
843 * status is checked. Because waiting for "a while" before
844 * checking status is fine, post SRST, we perform this magic
845 * delay here as well.
846 */
847 msleep(150);
848
849 *class = ATA_DEV_NONE;
Tejun Heo81952c52006-05-15 20:57:47 +0900850 if (ata_port_online(ap)) {
Tejun Heo4658f792006-03-22 21:07:03 +0900851 if (ata_busy_sleep(ap, ATA_TMOUT_BOOT_QUICK, ATA_TMOUT_BOOT)) {
852 rc = -EIO;
853 reason = "device not ready";
854 goto fail;
855 }
856 *class = ahci_dev_classify(ap);
857 }
858
859 DPRINTK("EXIT, class=%u\n", *class);
860 return 0;
861
862 fail_restart:
zhao, forrest5457f212006-07-13 13:38:32 +0800863 ahci_start_engine(port_mmio);
Tejun Heo4658f792006-03-22 21:07:03 +0900864 fail:
Tejun Heof15a1da2006-05-15 20:57:56 +0900865 ata_port_printk(ap, KERN_ERR, "softreset failed (%s)\n", reason);
Tejun Heo4658f792006-03-22 21:07:03 +0900866 return rc;
867}
868
Tejun Heo2bf2cb22006-04-11 22:16:45 +0900869static int ahci_hardreset(struct ata_port *ap, unsigned int *class)
Tejun Heo422b7592005-12-19 22:37:17 +0900870{
Tejun Heo42969712006-05-31 18:28:18 +0900871 struct ahci_port_priv *pp = ap->private_data;
872 u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
873 struct ata_taskfile tf;
Jeff Garzikcca39742006-08-24 03:19:22 -0400874 void __iomem *mmio = ap->host->mmio_base;
zhao, forrest5457f212006-07-13 13:38:32 +0800875 void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
Tejun Heo4bd00f62006-02-11 16:26:02 +0900876 int rc;
877
878 DPRINTK("ENTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879
zhao, forrest5457f212006-07-13 13:38:32 +0800880 ahci_stop_engine(port_mmio);
Tejun Heo42969712006-05-31 18:28:18 +0900881
882 /* clear D2H reception area to properly wait for D2H FIS */
883 ata_tf_init(ap->device, &tf);
884 tf.command = 0xff;
885 ata_tf_to_fis(&tf, d2h_fis, 0);
886
Tejun Heo2bf2cb22006-04-11 22:16:45 +0900887 rc = sata_std_hardreset(ap, class);
Tejun Heo42969712006-05-31 18:28:18 +0900888
zhao, forrest5457f212006-07-13 13:38:32 +0800889 ahci_start_engine(port_mmio);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700890
Tejun Heo81952c52006-05-15 20:57:47 +0900891 if (rc == 0 && ata_port_online(ap))
Tejun Heo4bd00f62006-02-11 16:26:02 +0900892 *class = ahci_dev_classify(ap);
893 if (*class == ATA_DEV_UNKNOWN)
894 *class = ATA_DEV_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700895
Tejun Heo4bd00f62006-02-11 16:26:02 +0900896 DPRINTK("EXIT, rc=%d, class=%u\n", rc, *class);
897 return rc;
898}
899
Tejun Heoad616ff2006-11-01 18:00:24 +0900900static int ahci_vt8251_hardreset(struct ata_port *ap, unsigned int *class)
901{
902 void __iomem *mmio = ap->host->mmio_base;
903 void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
904 int rc;
905
906 DPRINTK("ENTER\n");
907
908 ahci_stop_engine(port_mmio);
909
910 rc = sata_port_hardreset(ap, sata_ehc_deb_timing(&ap->eh_context));
911
912 /* vt8251 needs SError cleared for the port to operate */
913 ahci_scr_write(ap, SCR_ERROR, ahci_scr_read(ap, SCR_ERROR));
914
915 ahci_start_engine(port_mmio);
916
917 DPRINTK("EXIT, rc=%d, class=%u\n", rc, *class);
918
919 /* vt8251 doesn't clear BSY on signature FIS reception,
920 * request follow-up softreset.
921 */
922 return rc ?: -EAGAIN;
923}
924
Tejun Heo4bd00f62006-02-11 16:26:02 +0900925static void ahci_postreset(struct ata_port *ap, unsigned int *class)
926{
927 void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
928 u32 new_tmp, tmp;
929
930 ata_std_postreset(ap, class);
Jeff Garzik02eaa662005-11-12 01:32:19 -0500931
932 /* Make sure port's ATAPI bit is set appropriately */
933 new_tmp = tmp = readl(port_mmio + PORT_CMD);
Tejun Heo4bd00f62006-02-11 16:26:02 +0900934 if (*class == ATA_DEV_ATAPI)
Jeff Garzik02eaa662005-11-12 01:32:19 -0500935 new_tmp |= PORT_CMD_ATAPI;
936 else
937 new_tmp &= ~PORT_CMD_ATAPI;
938 if (new_tmp != tmp) {
939 writel(new_tmp, port_mmio + PORT_CMD);
940 readl(port_mmio + PORT_CMD); /* flush */
941 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700942}
943
944static u8 ahci_check_status(struct ata_port *ap)
945{
Al Viro1e4f2a92005-10-21 06:46:02 +0100946 void __iomem *mmio = (void __iomem *) ap->ioaddr.cmd_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947
948 return readl(mmio + PORT_TFDATA) & 0xFF;
949}
950
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951static void ahci_tf_read(struct ata_port *ap, struct ata_taskfile *tf)
952{
953 struct ahci_port_priv *pp = ap->private_data;
954 u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
955
956 ata_tf_from_fis(d2h_fis, tf);
957}
958
Tejun Heo12fad3f2006-05-15 21:03:55 +0900959static unsigned int ahci_fill_sg(struct ata_queued_cmd *qc, void *cmd_tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700960{
Jeff Garzikcedc9a42005-10-05 07:13:30 -0400961 struct scatterlist *sg;
962 struct ahci_sg *ahci_sg;
Jeff Garzik828d09d2005-11-12 01:27:07 -0500963 unsigned int n_sg = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700964
965 VPRINTK("ENTER\n");
966
967 /*
968 * Next, the S/G list.
969 */
Tejun Heo12fad3f2006-05-15 21:03:55 +0900970 ahci_sg = cmd_tbl + AHCI_CMD_TBL_HDR_SZ;
Jeff Garzikcedc9a42005-10-05 07:13:30 -0400971 ata_for_each_sg(sg, qc) {
972 dma_addr_t addr = sg_dma_address(sg);
973 u32 sg_len = sg_dma_len(sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974
Jeff Garzikcedc9a42005-10-05 07:13:30 -0400975 ahci_sg->addr = cpu_to_le32(addr & 0xffffffff);
976 ahci_sg->addr_hi = cpu_to_le32((addr >> 16) >> 16);
977 ahci_sg->flags_size = cpu_to_le32(sg_len - 1);
Jeff Garzik828d09d2005-11-12 01:27:07 -0500978
Jeff Garzikcedc9a42005-10-05 07:13:30 -0400979 ahci_sg++;
Jeff Garzik828d09d2005-11-12 01:27:07 -0500980 n_sg++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 }
Jeff Garzik828d09d2005-11-12 01:27:07 -0500982
983 return n_sg;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984}
985
986static void ahci_qc_prep(struct ata_queued_cmd *qc)
987{
Jeff Garzika0ea7322005-06-04 01:13:15 -0400988 struct ata_port *ap = qc->ap;
989 struct ahci_port_priv *pp = ap->private_data;
Tejun Heocc9278e2006-02-10 17:25:47 +0900990 int is_atapi = is_atapi_taskfile(&qc->tf);
Tejun Heo12fad3f2006-05-15 21:03:55 +0900991 void *cmd_tbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 u32 opts;
993 const u32 cmd_fis_len = 5; /* five dwords */
Jeff Garzik828d09d2005-11-12 01:27:07 -0500994 unsigned int n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700995
996 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 * Fill in command table information. First, the header,
998 * a SATA Register - Host to Device command FIS.
999 */
Tejun Heo12fad3f2006-05-15 21:03:55 +09001000 cmd_tbl = pp->cmd_tbl + qc->tag * AHCI_CMD_TBL_SZ;
1001
1002 ata_tf_to_fis(&qc->tf, cmd_tbl, 0);
Tejun Heocc9278e2006-02-10 17:25:47 +09001003 if (is_atapi) {
Tejun Heo12fad3f2006-05-15 21:03:55 +09001004 memset(cmd_tbl + AHCI_CMD_TBL_CDB, 0, 32);
1005 memcpy(cmd_tbl + AHCI_CMD_TBL_CDB, qc->cdb, qc->dev->cdb_len);
Jeff Garzika0ea7322005-06-04 01:13:15 -04001006 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007
Tejun Heocc9278e2006-02-10 17:25:47 +09001008 n_elem = 0;
1009 if (qc->flags & ATA_QCFLAG_DMAMAP)
Tejun Heo12fad3f2006-05-15 21:03:55 +09001010 n_elem = ahci_fill_sg(qc, cmd_tbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011
Tejun Heocc9278e2006-02-10 17:25:47 +09001012 /*
1013 * Fill in command slot information.
1014 */
1015 opts = cmd_fis_len | n_elem << 16;
1016 if (qc->tf.flags & ATA_TFLAG_WRITE)
1017 opts |= AHCI_CMD_WRITE;
1018 if (is_atapi)
Tejun Heo4b10e552006-03-12 11:25:27 +09001019 opts |= AHCI_CMD_ATAPI | AHCI_CMD_PREFETCH;
Jeff Garzik828d09d2005-11-12 01:27:07 -05001020
Tejun Heo12fad3f2006-05-15 21:03:55 +09001021 ahci_fill_cmd_slot(pp, qc->tag, opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022}
1023
Tejun Heo78cd52d2006-05-15 20:58:29 +09001024static void ahci_error_intr(struct ata_port *ap, u32 irq_stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025{
Tejun Heo78cd52d2006-05-15 20:58:29 +09001026 struct ahci_port_priv *pp = ap->private_data;
1027 struct ata_eh_info *ehi = &ap->eh_info;
1028 unsigned int err_mask = 0, action = 0;
1029 struct ata_queued_cmd *qc;
1030 u32 serror;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
Tejun Heo78cd52d2006-05-15 20:58:29 +09001032 ata_ehi_clear_desc(ehi);
Jeff Garzik9f68a242005-11-15 14:03:47 -05001033
Tejun Heo78cd52d2006-05-15 20:58:29 +09001034 /* AHCI needs SError cleared; otherwise, it might lock up */
1035 serror = ahci_scr_read(ap, SCR_ERROR);
1036 ahci_scr_write(ap, SCR_ERROR, serror);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Tejun Heo78cd52d2006-05-15 20:58:29 +09001038 /* analyze @irq_stat */
1039 ata_ehi_push_desc(ehi, "irq_stat 0x%08x", irq_stat);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040
Tejun Heo41669552006-11-29 11:33:14 +09001041 /* some controllers set IRQ_IF_ERR on device errors, ignore it */
1042 if (ap->flags & AHCI_FLAG_IGN_IRQ_IF_ERR)
1043 irq_stat &= ~PORT_IRQ_IF_ERR;
1044
Tejun Heo78cd52d2006-05-15 20:58:29 +09001045 if (irq_stat & PORT_IRQ_TF_ERR)
1046 err_mask |= AC_ERR_DEV;
1047
1048 if (irq_stat & (PORT_IRQ_HBUS_ERR | PORT_IRQ_HBUS_DATA_ERR)) {
1049 err_mask |= AC_ERR_HOST_BUS;
1050 action |= ATA_EH_SOFTRESET;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 }
1052
Tejun Heo78cd52d2006-05-15 20:58:29 +09001053 if (irq_stat & PORT_IRQ_IF_ERR) {
1054 err_mask |= AC_ERR_ATA_BUS;
1055 action |= ATA_EH_SOFTRESET;
1056 ata_ehi_push_desc(ehi, ", interface fatal error");
1057 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058
Tejun Heo78cd52d2006-05-15 20:58:29 +09001059 if (irq_stat & (PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)) {
Tejun Heo42969712006-05-31 18:28:18 +09001060 ata_ehi_hotplugged(ehi);
Tejun Heo78cd52d2006-05-15 20:58:29 +09001061 ata_ehi_push_desc(ehi, ", %s", irq_stat & PORT_IRQ_CONNECT ?
1062 "connection status changed" : "PHY RDY changed");
1063 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064
Tejun Heo78cd52d2006-05-15 20:58:29 +09001065 if (irq_stat & PORT_IRQ_UNK_FIS) {
1066 u32 *unk = (u32 *)(pp->rx_fis + RX_FIS_UNK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067
Tejun Heo78cd52d2006-05-15 20:58:29 +09001068 err_mask |= AC_ERR_HSM;
1069 action |= ATA_EH_SOFTRESET;
1070 ata_ehi_push_desc(ehi, ", unknown FIS %08x %08x %08x %08x",
1071 unk[0], unk[1], unk[2], unk[3]);
1072 }
Jeff Garzikb8f61532005-08-25 22:01:20 -04001073
Tejun Heo78cd52d2006-05-15 20:58:29 +09001074 /* okay, let's hand over to EH */
1075 ehi->serror |= serror;
1076 ehi->action |= action;
1077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 qc = ata_qc_from_tag(ap, ap->active_tag);
Tejun Heo78cd52d2006-05-15 20:58:29 +09001079 if (qc)
1080 qc->err_mask |= err_mask;
1081 else
1082 ehi->err_mask |= err_mask;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Tejun Heo78cd52d2006-05-15 20:58:29 +09001084 if (irq_stat & PORT_IRQ_FREEZE)
1085 ata_port_freeze(ap);
1086 else
1087 ata_port_abort(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001088}
1089
Tejun Heo78cd52d2006-05-15 20:58:29 +09001090static void ahci_host_intr(struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001091{
Jeff Garzikcca39742006-08-24 03:19:22 -04001092 void __iomem *mmio = ap->host->mmio_base;
Jeff Garzikea6ba102005-08-30 05:18:18 -04001093 void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
Tejun Heo12fad3f2006-05-15 21:03:55 +09001094 struct ata_eh_info *ehi = &ap->eh_info;
1095 u32 status, qc_active;
1096 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001097
1098 status = readl(port_mmio + PORT_IRQ_STAT);
1099 writel(status, port_mmio + PORT_IRQ_STAT);
1100
Tejun Heo78cd52d2006-05-15 20:58:29 +09001101 if (unlikely(status & PORT_IRQ_ERROR)) {
1102 ahci_error_intr(ap, status);
1103 return;
1104 }
1105
Tejun Heo12fad3f2006-05-15 21:03:55 +09001106 if (ap->sactive)
1107 qc_active = readl(port_mmio + PORT_SCR_ACT);
1108 else
1109 qc_active = readl(port_mmio + PORT_CMD_ISSUE);
1110
1111 rc = ata_qc_complete_multiple(ap, qc_active, NULL);
1112 if (rc > 0)
1113 return;
1114 if (rc < 0) {
1115 ehi->err_mask |= AC_ERR_HSM;
1116 ehi->action |= ATA_EH_SOFTRESET;
1117 ata_port_freeze(ap);
1118 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001119 }
1120
Tejun Heo2a3917a2006-05-15 20:58:30 +09001121 /* hmmm... a spurious interupt */
1122
Tejun Heo12fad3f2006-05-15 21:03:55 +09001123 /* some devices send D2H reg with I bit set during NCQ command phase */
Alan Cox12a87d32006-10-16 16:21:40 +01001124 if (ap->sactive && (status & PORT_IRQ_D2H_REG_FIS))
Tejun Heo12fad3f2006-05-15 21:03:55 +09001125 return;
1126
Tejun Heo2a3917a2006-05-15 20:58:30 +09001127 /* ignore interim PIO setup fis interrupts */
Jeff Garzik9bec2e32006-08-31 00:02:15 -04001128 if (ata_tag_valid(ap->active_tag) && (status & PORT_IRQ_PIOS_FIS))
Unicorn Changf1d39b22006-08-01 12:18:07 +08001129 return;
Tejun Heo2a3917a2006-05-15 20:58:30 +09001130
Tejun Heo78cd52d2006-05-15 20:58:29 +09001131 if (ata_ratelimit())
1132 ata_port_printk(ap, KERN_INFO, "spurious interrupt "
Tejun Heo12fad3f2006-05-15 21:03:55 +09001133 "(irq_stat 0x%x active_tag %d sactive 0x%x)\n",
1134 status, ap->active_tag, ap->sactive);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135}
1136
1137static void ahci_irq_clear(struct ata_port *ap)
1138{
1139 /* TODO */
1140}
1141
David Howells7d12e782006-10-05 14:55:46 +01001142static irqreturn_t ahci_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001143{
Jeff Garzikcca39742006-08-24 03:19:22 -04001144 struct ata_host *host = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 struct ahci_host_priv *hpriv;
1146 unsigned int i, handled = 0;
Jeff Garzikea6ba102005-08-30 05:18:18 -04001147 void __iomem *mmio;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148 u32 irq_stat, irq_ack = 0;
1149
1150 VPRINTK("ENTER\n");
1151
Jeff Garzikcca39742006-08-24 03:19:22 -04001152 hpriv = host->private_data;
1153 mmio = host->mmio_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154
1155 /* sigh. 0xffffffff is a valid return from h/w */
1156 irq_stat = readl(mmio + HOST_IRQ_STAT);
1157 irq_stat &= hpriv->port_map;
1158 if (!irq_stat)
1159 return IRQ_NONE;
1160
Jeff Garzikcca39742006-08-24 03:19:22 -04001161 spin_lock(&host->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Jeff Garzikcca39742006-08-24 03:19:22 -04001163 for (i = 0; i < host->n_ports; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 struct ata_port *ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165
Jeff Garzik67846b32005-10-05 02:58:32 -04001166 if (!(irq_stat & (1 << i)))
1167 continue;
1168
Jeff Garzikcca39742006-08-24 03:19:22 -04001169 ap = host->ports[i];
Jeff Garzik67846b32005-10-05 02:58:32 -04001170 if (ap) {
Tejun Heo78cd52d2006-05-15 20:58:29 +09001171 ahci_host_intr(ap);
Jeff Garzik67846b32005-10-05 02:58:32 -04001172 VPRINTK("port %u\n", i);
1173 } else {
1174 VPRINTK("port %u (no irq)\n", i);
Tejun Heo6971ed12006-03-11 12:47:54 +09001175 if (ata_ratelimit())
Jeff Garzikcca39742006-08-24 03:19:22 -04001176 dev_printk(KERN_WARNING, host->dev,
Jeff Garzika9524a72005-10-30 14:39:11 -05001177 "interrupt on disabled port %u\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001178 }
Jeff Garzik67846b32005-10-05 02:58:32 -04001179
1180 irq_ack |= (1 << i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181 }
1182
1183 if (irq_ack) {
1184 writel(irq_ack, mmio + HOST_IRQ_STAT);
1185 handled = 1;
1186 }
1187
Jeff Garzikcca39742006-08-24 03:19:22 -04001188 spin_unlock(&host->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189
1190 VPRINTK("EXIT\n");
1191
1192 return IRQ_RETVAL(handled);
1193}
1194
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09001195static unsigned int ahci_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001196{
1197 struct ata_port *ap = qc->ap;
Jeff Garzikea6ba102005-08-30 05:18:18 -04001198 void __iomem *port_mmio = (void __iomem *) ap->ioaddr.cmd_addr;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199
Tejun Heo12fad3f2006-05-15 21:03:55 +09001200 if (qc->tf.protocol == ATA_PROT_NCQ)
1201 writel(1 << qc->tag, port_mmio + PORT_SCR_ACT);
1202 writel(1 << qc->tag, port_mmio + PORT_CMD_ISSUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001203 readl(port_mmio + PORT_CMD_ISSUE); /* flush */
1204
1205 return 0;
1206}
1207
Tejun Heo78cd52d2006-05-15 20:58:29 +09001208static void ahci_freeze(struct ata_port *ap)
1209{
Jeff Garzikcca39742006-08-24 03:19:22 -04001210 void __iomem *mmio = ap->host->mmio_base;
Tejun Heo78cd52d2006-05-15 20:58:29 +09001211 void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
1212
1213 /* turn IRQ off */
1214 writel(0, port_mmio + PORT_IRQ_MASK);
1215}
1216
1217static void ahci_thaw(struct ata_port *ap)
1218{
Jeff Garzikcca39742006-08-24 03:19:22 -04001219 void __iomem *mmio = ap->host->mmio_base;
Tejun Heo78cd52d2006-05-15 20:58:29 +09001220 void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
1221 u32 tmp;
1222
1223 /* clear IRQ */
1224 tmp = readl(port_mmio + PORT_IRQ_STAT);
1225 writel(tmp, port_mmio + PORT_IRQ_STAT);
1226 writel(1 << ap->id, mmio + HOST_IRQ_STAT);
1227
1228 /* turn IRQ back on */
1229 writel(DEF_PORT_IRQ, port_mmio + PORT_IRQ_MASK);
1230}
1231
1232static void ahci_error_handler(struct ata_port *ap)
1233{
Jeff Garzikcca39742006-08-24 03:19:22 -04001234 void __iomem *mmio = ap->host->mmio_base;
zhao, forrest5457f212006-07-13 13:38:32 +08001235 void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
1236
Tejun Heob51e9e52006-06-29 01:29:30 +09001237 if (!(ap->pflags & ATA_PFLAG_FROZEN)) {
Tejun Heo78cd52d2006-05-15 20:58:29 +09001238 /* restart engine */
zhao, forrest5457f212006-07-13 13:38:32 +08001239 ahci_stop_engine(port_mmio);
1240 ahci_start_engine(port_mmio);
Tejun Heo78cd52d2006-05-15 20:58:29 +09001241 }
1242
1243 /* perform recovery */
Tejun Heo4aeb0e32006-11-01 17:58:33 +09001244 ata_do_eh(ap, ata_std_prereset, ahci_softreset, ahci_hardreset,
Tejun Heof5914a42006-05-31 18:27:48 +09001245 ahci_postreset);
Tejun Heo78cd52d2006-05-15 20:58:29 +09001246}
1247
Tejun Heoad616ff2006-11-01 18:00:24 +09001248static void ahci_vt8251_error_handler(struct ata_port *ap)
1249{
1250 void __iomem *mmio = ap->host->mmio_base;
1251 void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
1252
1253 if (!(ap->pflags & ATA_PFLAG_FROZEN)) {
1254 /* restart engine */
1255 ahci_stop_engine(port_mmio);
1256 ahci_start_engine(port_mmio);
1257 }
1258
1259 /* perform recovery */
1260 ata_do_eh(ap, ata_std_prereset, ahci_softreset, ahci_vt8251_hardreset,
1261 ahci_postreset);
1262}
1263
Tejun Heo78cd52d2006-05-15 20:58:29 +09001264static void ahci_post_internal_cmd(struct ata_queued_cmd *qc)
1265{
1266 struct ata_port *ap = qc->ap;
Jeff Garzikcca39742006-08-24 03:19:22 -04001267 void __iomem *mmio = ap->host->mmio_base;
zhao, forrest5457f212006-07-13 13:38:32 +08001268 void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
Tejun Heo78cd52d2006-05-15 20:58:29 +09001269
1270 if (qc->flags & ATA_QCFLAG_FAILED)
1271 qc->err_mask |= AC_ERR_OTHER;
1272
1273 if (qc->err_mask) {
1274 /* make DMA engine forget about the failed command */
zhao, forrest5457f212006-07-13 13:38:32 +08001275 ahci_stop_engine(port_mmio);
1276 ahci_start_engine(port_mmio);
Tejun Heo78cd52d2006-05-15 20:58:29 +09001277 }
1278}
1279
Tejun Heoc1332872006-07-26 15:59:26 +09001280static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg)
1281{
Jeff Garzikcca39742006-08-24 03:19:22 -04001282 struct ahci_host_priv *hpriv = ap->host->private_data;
Tejun Heoc1332872006-07-26 15:59:26 +09001283 struct ahci_port_priv *pp = ap->private_data;
Jeff Garzikcca39742006-08-24 03:19:22 -04001284 void __iomem *mmio = ap->host->mmio_base;
Tejun Heoc1332872006-07-26 15:59:26 +09001285 void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
1286 const char *emsg = NULL;
1287 int rc;
1288
1289 rc = ahci_deinit_port(port_mmio, hpriv->cap, &emsg);
1290 if (rc) {
1291 ata_port_printk(ap, KERN_ERR, "%s (%d)\n", emsg, rc);
1292 ahci_init_port(port_mmio, hpriv->cap,
1293 pp->cmd_slot_dma, pp->rx_fis_dma);
1294 }
1295
1296 return rc;
1297}
1298
1299static int ahci_port_resume(struct ata_port *ap)
1300{
1301 struct ahci_port_priv *pp = ap->private_data;
Jeff Garzikcca39742006-08-24 03:19:22 -04001302 struct ahci_host_priv *hpriv = ap->host->private_data;
1303 void __iomem *mmio = ap->host->mmio_base;
Tejun Heoc1332872006-07-26 15:59:26 +09001304 void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
1305
1306 ahci_init_port(port_mmio, hpriv->cap, pp->cmd_slot_dma, pp->rx_fis_dma);
1307
1308 return 0;
1309}
1310
1311static int ahci_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
1312{
Jeff Garzikcca39742006-08-24 03:19:22 -04001313 struct ata_host *host = dev_get_drvdata(&pdev->dev);
1314 void __iomem *mmio = host->mmio_base;
Tejun Heoc1332872006-07-26 15:59:26 +09001315 u32 ctl;
1316
1317 if (mesg.event == PM_EVENT_SUSPEND) {
1318 /* AHCI spec rev1.1 section 8.3.3:
1319 * Software must disable interrupts prior to requesting a
1320 * transition of the HBA to D3 state.
1321 */
1322 ctl = readl(mmio + HOST_CTL);
1323 ctl &= ~HOST_IRQ_EN;
1324 writel(ctl, mmio + HOST_CTL);
1325 readl(mmio + HOST_CTL); /* flush */
1326 }
1327
1328 return ata_pci_device_suspend(pdev, mesg);
1329}
1330
1331static int ahci_pci_device_resume(struct pci_dev *pdev)
1332{
Jeff Garzikcca39742006-08-24 03:19:22 -04001333 struct ata_host *host = dev_get_drvdata(&pdev->dev);
1334 struct ahci_host_priv *hpriv = host->private_data;
1335 void __iomem *mmio = host->mmio_base;
Tejun Heoc1332872006-07-26 15:59:26 +09001336 int rc;
1337
1338 ata_pci_device_do_resume(pdev);
1339
1340 if (pdev->dev.power.power_state.event == PM_EVENT_SUSPEND) {
1341 rc = ahci_reset_controller(mmio, pdev);
1342 if (rc)
1343 return rc;
1344
Jeff Garzikcca39742006-08-24 03:19:22 -04001345 ahci_init_controller(mmio, pdev, host->n_ports, hpriv->cap);
Tejun Heoc1332872006-07-26 15:59:26 +09001346 }
1347
Jeff Garzikcca39742006-08-24 03:19:22 -04001348 ata_host_resume(host);
Tejun Heoc1332872006-07-26 15:59:26 +09001349
1350 return 0;
1351}
1352
Tejun Heo254950c2006-07-26 15:59:25 +09001353static int ahci_port_start(struct ata_port *ap)
1354{
Jeff Garzikcca39742006-08-24 03:19:22 -04001355 struct device *dev = ap->host->dev;
1356 struct ahci_host_priv *hpriv = ap->host->private_data;
Tejun Heo254950c2006-07-26 15:59:25 +09001357 struct ahci_port_priv *pp;
Jeff Garzikcca39742006-08-24 03:19:22 -04001358 void __iomem *mmio = ap->host->mmio_base;
Tejun Heo254950c2006-07-26 15:59:25 +09001359 void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
1360 void *mem;
1361 dma_addr_t mem_dma;
1362 int rc;
1363
1364 pp = kmalloc(sizeof(*pp), GFP_KERNEL);
1365 if (!pp)
1366 return -ENOMEM;
1367 memset(pp, 0, sizeof(*pp));
1368
1369 rc = ata_pad_alloc(ap, dev);
1370 if (rc) {
1371 kfree(pp);
1372 return rc;
1373 }
1374
1375 mem = dma_alloc_coherent(dev, AHCI_PORT_PRIV_DMA_SZ, &mem_dma, GFP_KERNEL);
1376 if (!mem) {
1377 ata_pad_free(ap, dev);
1378 kfree(pp);
1379 return -ENOMEM;
1380 }
1381 memset(mem, 0, AHCI_PORT_PRIV_DMA_SZ);
1382
1383 /*
1384 * First item in chunk of DMA memory: 32-slot command table,
1385 * 32 bytes each in size
1386 */
1387 pp->cmd_slot = mem;
1388 pp->cmd_slot_dma = mem_dma;
1389
1390 mem += AHCI_CMD_SLOT_SZ;
1391 mem_dma += AHCI_CMD_SLOT_SZ;
1392
1393 /*
1394 * Second item: Received-FIS area
1395 */
1396 pp->rx_fis = mem;
1397 pp->rx_fis_dma = mem_dma;
1398
1399 mem += AHCI_RX_FIS_SZ;
1400 mem_dma += AHCI_RX_FIS_SZ;
1401
1402 /*
1403 * Third item: data area for storing a single command
1404 * and its scatter-gather table
1405 */
1406 pp->cmd_tbl = mem;
1407 pp->cmd_tbl_dma = mem_dma;
1408
1409 ap->private_data = pp;
1410
Tejun Heo0be0aa92006-07-26 15:59:26 +09001411 /* initialize port */
1412 ahci_init_port(port_mmio, hpriv->cap, pp->cmd_slot_dma, pp->rx_fis_dma);
Tejun Heo254950c2006-07-26 15:59:25 +09001413
1414 return 0;
1415}
1416
1417static void ahci_port_stop(struct ata_port *ap)
1418{
Jeff Garzikcca39742006-08-24 03:19:22 -04001419 struct device *dev = ap->host->dev;
1420 struct ahci_host_priv *hpriv = ap->host->private_data;
Tejun Heo254950c2006-07-26 15:59:25 +09001421 struct ahci_port_priv *pp = ap->private_data;
Jeff Garzikcca39742006-08-24 03:19:22 -04001422 void __iomem *mmio = ap->host->mmio_base;
Tejun Heo254950c2006-07-26 15:59:25 +09001423 void __iomem *port_mmio = ahci_port_base(mmio, ap->port_no);
Tejun Heo0be0aa92006-07-26 15:59:26 +09001424 const char *emsg = NULL;
1425 int rc;
Tejun Heo254950c2006-07-26 15:59:25 +09001426
Tejun Heo0be0aa92006-07-26 15:59:26 +09001427 /* de-initialize port */
1428 rc = ahci_deinit_port(port_mmio, hpriv->cap, &emsg);
1429 if (rc)
1430 ata_port_printk(ap, KERN_WARNING, "%s (%d)\n", emsg, rc);
Tejun Heo254950c2006-07-26 15:59:25 +09001431
1432 ap->private_data = NULL;
1433 dma_free_coherent(dev, AHCI_PORT_PRIV_DMA_SZ,
1434 pp->cmd_slot, pp->cmd_slot_dma);
1435 ata_pad_free(ap, dev);
1436 kfree(pp);
1437}
1438
Linus Torvalds1da177e2005-04-16 15:20:36 -07001439static void ahci_setup_port(struct ata_ioports *port, unsigned long base,
1440 unsigned int port_idx)
1441{
1442 VPRINTK("ENTER, base==0x%lx, port_idx %u\n", base, port_idx);
1443 base = ahci_port_base_ul(base, port_idx);
1444 VPRINTK("base now==0x%lx\n", base);
1445
1446 port->cmd_addr = base;
1447 port->scr_addr = base + PORT_SCR;
1448
1449 VPRINTK("EXIT\n");
1450}
1451
1452static int ahci_host_init(struct ata_probe_ent *probe_ent)
1453{
1454 struct ahci_host_priv *hpriv = probe_ent->private_data;
1455 struct pci_dev *pdev = to_pci_dev(probe_ent->dev);
1456 void __iomem *mmio = probe_ent->mmio_base;
Tejun Heo0be0aa92006-07-26 15:59:26 +09001457 unsigned int i, using_dac;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001458 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001459
Tejun Heod91542c2006-07-26 15:59:26 +09001460 rc = ahci_reset_controller(mmio, pdev);
1461 if (rc)
1462 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463
1464 hpriv->cap = readl(mmio + HOST_CAP);
1465 hpriv->port_map = readl(mmio + HOST_PORTS_IMPL);
1466 probe_ent->n_ports = (hpriv->cap & 0x1f) + 1;
1467
1468 VPRINTK("cap 0x%x port_map 0x%x n_ports %d\n",
1469 hpriv->cap, hpriv->port_map, probe_ent->n_ports);
1470
1471 using_dac = hpriv->cap & HOST_CAP_64;
1472 if (using_dac &&
1473 !pci_set_dma_mask(pdev, DMA_64BIT_MASK)) {
1474 rc = pci_set_consistent_dma_mask(pdev, DMA_64BIT_MASK);
1475 if (rc) {
1476 rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
1477 if (rc) {
Jeff Garzika9524a72005-10-30 14:39:11 -05001478 dev_printk(KERN_ERR, &pdev->dev,
1479 "64-bit DMA enable failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480 return rc;
1481 }
1482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001483 } else {
1484 rc = pci_set_dma_mask(pdev, DMA_32BIT_MASK);
1485 if (rc) {
Jeff Garzika9524a72005-10-30 14:39:11 -05001486 dev_printk(KERN_ERR, &pdev->dev,
1487 "32-bit DMA enable failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001488 return rc;
1489 }
1490 rc = pci_set_consistent_dma_mask(pdev, DMA_32BIT_MASK);
1491 if (rc) {
Jeff Garzika9524a72005-10-30 14:39:11 -05001492 dev_printk(KERN_ERR, &pdev->dev,
1493 "32-bit consistent DMA enable failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 return rc;
1495 }
1496 }
1497
Tejun Heod91542c2006-07-26 15:59:26 +09001498 for (i = 0; i < probe_ent->n_ports; i++)
1499 ahci_setup_port(&probe_ent->port[i], (unsigned long) mmio, i);
Tejun Heo0be0aa92006-07-26 15:59:26 +09001500
Tejun Heod91542c2006-07-26 15:59:26 +09001501 ahci_init_controller(mmio, pdev, probe_ent->n_ports, hpriv->cap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001502
1503 pci_set_master(pdev);
1504
1505 return 0;
1506}
1507
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508static void ahci_print_info(struct ata_probe_ent *probe_ent)
1509{
1510 struct ahci_host_priv *hpriv = probe_ent->private_data;
1511 struct pci_dev *pdev = to_pci_dev(probe_ent->dev);
Jeff Garzikea6ba102005-08-30 05:18:18 -04001512 void __iomem *mmio = probe_ent->mmio_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001513 u32 vers, cap, impl, speed;
1514 const char *speed_s;
1515 u16 cc;
1516 const char *scc_s;
1517
1518 vers = readl(mmio + HOST_VERSION);
1519 cap = hpriv->cap;
1520 impl = hpriv->port_map;
1521
1522 speed = (cap >> 20) & 0xf;
1523 if (speed == 1)
1524 speed_s = "1.5";
1525 else if (speed == 2)
1526 speed_s = "3";
1527 else
1528 speed_s = "?";
1529
1530 pci_read_config_word(pdev, 0x0a, &cc);
1531 if (cc == 0x0101)
1532 scc_s = "IDE";
1533 else if (cc == 0x0106)
1534 scc_s = "SATA";
1535 else if (cc == 0x0104)
1536 scc_s = "RAID";
1537 else
1538 scc_s = "unknown";
1539
Jeff Garzika9524a72005-10-30 14:39:11 -05001540 dev_printk(KERN_INFO, &pdev->dev,
1541 "AHCI %02x%02x.%02x%02x "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 "%u slots %u ports %s Gbps 0x%x impl %s mode\n"
1543 ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
1545 (vers >> 24) & 0xff,
1546 (vers >> 16) & 0xff,
1547 (vers >> 8) & 0xff,
1548 vers & 0xff,
1549
1550 ((cap >> 8) & 0x1f) + 1,
1551 (cap & 0x1f) + 1,
1552 speed_s,
1553 impl,
1554 scc_s);
1555
Jeff Garzika9524a72005-10-30 14:39:11 -05001556 dev_printk(KERN_INFO, &pdev->dev,
1557 "flags: "
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558 "%s%s%s%s%s%s"
1559 "%s%s%s%s%s%s%s\n"
1560 ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561
1562 cap & (1 << 31) ? "64bit " : "",
1563 cap & (1 << 30) ? "ncq " : "",
1564 cap & (1 << 28) ? "ilck " : "",
1565 cap & (1 << 27) ? "stag " : "",
1566 cap & (1 << 26) ? "pm " : "",
1567 cap & (1 << 25) ? "led " : "",
1568
1569 cap & (1 << 24) ? "clo " : "",
1570 cap & (1 << 19) ? "nz " : "",
1571 cap & (1 << 18) ? "only " : "",
1572 cap & (1 << 17) ? "pmp " : "",
1573 cap & (1 << 15) ? "pio " : "",
1574 cap & (1 << 14) ? "slum " : "",
1575 cap & (1 << 13) ? "part " : ""
1576 );
1577}
1578
1579static int ahci_init_one (struct pci_dev *pdev, const struct pci_device_id *ent)
1580{
1581 static int printed_version;
1582 struct ata_probe_ent *probe_ent = NULL;
1583 struct ahci_host_priv *hpriv;
1584 unsigned long base;
Jeff Garzikea6ba102005-08-30 05:18:18 -04001585 void __iomem *mmio_base;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001586 unsigned int board_idx = (unsigned int) ent->driver_data;
Jeff Garzik907f4672005-05-12 15:03:42 -04001587 int have_msi, pci_dev_busy = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001588 int rc;
1589
1590 VPRINTK("ENTER\n");
1591
Tejun Heo12fad3f2006-05-15 21:03:55 +09001592 WARN_ON(ATA_MAX_QUEUE > AHCI_MAX_CMDS);
1593
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594 if (!printed_version++)
Jeff Garzika9524a72005-10-30 14:39:11 -05001595 dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596
root9545b572006-07-05 22:58:20 -04001597 /* JMicron-specific fixup: make sure we're in AHCI mode */
1598 /* This is protected from races with ata_jmicron by the pci probe
1599 locking */
1600 if (pdev->vendor == PCI_VENDOR_ID_JMICRON) {
1601 /* AHCI enable, AHCI on function 0 */
1602 pci_write_config_byte(pdev, 0x41, 0xa1);
1603 /* Function 1 is the PATA controller */
1604 if (PCI_FUNC(pdev->devfn))
1605 return -ENODEV;
1606 }
1607
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 rc = pci_enable_device(pdev);
1609 if (rc)
1610 return rc;
1611
1612 rc = pci_request_regions(pdev, DRV_NAME);
1613 if (rc) {
1614 pci_dev_busy = 1;
1615 goto err_out;
1616 }
1617
Jeff Garzik907f4672005-05-12 15:03:42 -04001618 if (pci_enable_msi(pdev) == 0)
1619 have_msi = 1;
1620 else {
1621 pci_intx(pdev, 1);
1622 have_msi = 0;
1623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624
1625 probe_ent = kmalloc(sizeof(*probe_ent), GFP_KERNEL);
1626 if (probe_ent == NULL) {
1627 rc = -ENOMEM;
Jeff Garzik907f4672005-05-12 15:03:42 -04001628 goto err_out_msi;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001629 }
1630
1631 memset(probe_ent, 0, sizeof(*probe_ent));
1632 probe_ent->dev = pci_dev_to_dev(pdev);
1633 INIT_LIST_HEAD(&probe_ent->node);
1634
Jeff Garzik374b1872005-08-30 05:42:52 -04001635 mmio_base = pci_iomap(pdev, AHCI_PCI_BAR, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636 if (mmio_base == NULL) {
1637 rc = -ENOMEM;
1638 goto err_out_free_ent;
1639 }
1640 base = (unsigned long) mmio_base;
1641
1642 hpriv = kmalloc(sizeof(*hpriv), GFP_KERNEL);
1643 if (!hpriv) {
1644 rc = -ENOMEM;
1645 goto err_out_iounmap;
1646 }
1647 memset(hpriv, 0, sizeof(*hpriv));
1648
1649 probe_ent->sht = ahci_port_info[board_idx].sht;
Jeff Garzikcca39742006-08-24 03:19:22 -04001650 probe_ent->port_flags = ahci_port_info[board_idx].flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 probe_ent->pio_mask = ahci_port_info[board_idx].pio_mask;
1652 probe_ent->udma_mask = ahci_port_info[board_idx].udma_mask;
1653 probe_ent->port_ops = ahci_port_info[board_idx].port_ops;
1654
1655 probe_ent->irq = pdev->irq;
Thomas Gleixner1d6f3592006-07-01 19:29:42 -07001656 probe_ent->irq_flags = IRQF_SHARED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001657 probe_ent->mmio_base = mmio_base;
1658 probe_ent->private_data = hpriv;
1659
Jeff Garzik4b0060f2005-06-04 00:50:22 -04001660 if (have_msi)
1661 hpriv->flags |= AHCI_FLAG_MSI;
Jeff Garzik907f4672005-05-12 15:03:42 -04001662
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 /* initialize adapter */
1664 rc = ahci_host_init(probe_ent);
1665 if (rc)
1666 goto err_out_hpriv;
1667
Jeff Garzikcca39742006-08-24 03:19:22 -04001668 if (!(probe_ent->port_flags & AHCI_FLAG_NO_NCQ) &&
Tejun Heo71f07372006-06-21 23:12:48 +09001669 (hpriv->cap & HOST_CAP_NCQ))
Jeff Garzikcca39742006-08-24 03:19:22 -04001670 probe_ent->port_flags |= ATA_FLAG_NCQ;
Tejun Heo12fad3f2006-05-15 21:03:55 +09001671
Linus Torvalds1da177e2005-04-16 15:20:36 -07001672 ahci_print_info(probe_ent);
1673
1674 /* FIXME: check ata_device_add return value */
1675 ata_device_add(probe_ent);
1676 kfree(probe_ent);
1677
1678 return 0;
1679
1680err_out_hpriv:
1681 kfree(hpriv);
1682err_out_iounmap:
Jeff Garzik374b1872005-08-30 05:42:52 -04001683 pci_iounmap(pdev, mmio_base);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684err_out_free_ent:
1685 kfree(probe_ent);
Jeff Garzik907f4672005-05-12 15:03:42 -04001686err_out_msi:
1687 if (have_msi)
1688 pci_disable_msi(pdev);
1689 else
1690 pci_intx(pdev, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 pci_release_regions(pdev);
1692err_out:
1693 if (!pci_dev_busy)
1694 pci_disable_device(pdev);
1695 return rc;
1696}
1697
Jeff Garzik907f4672005-05-12 15:03:42 -04001698static void ahci_remove_one (struct pci_dev *pdev)
1699{
1700 struct device *dev = pci_dev_to_dev(pdev);
Jeff Garzikcca39742006-08-24 03:19:22 -04001701 struct ata_host *host = dev_get_drvdata(dev);
1702 struct ahci_host_priv *hpriv = host->private_data;
Jeff Garzik907f4672005-05-12 15:03:42 -04001703 unsigned int i;
1704 int have_msi;
1705
Jeff Garzikcca39742006-08-24 03:19:22 -04001706 for (i = 0; i < host->n_ports; i++)
1707 ata_port_detach(host->ports[i]);
Jeff Garzik907f4672005-05-12 15:03:42 -04001708
Jeff Garzik4b0060f2005-06-04 00:50:22 -04001709 have_msi = hpriv->flags & AHCI_FLAG_MSI;
Jeff Garzikcca39742006-08-24 03:19:22 -04001710 free_irq(host->irq, host);
Jeff Garzik907f4672005-05-12 15:03:42 -04001711
Jeff Garzikcca39742006-08-24 03:19:22 -04001712 for (i = 0; i < host->n_ports; i++) {
1713 struct ata_port *ap = host->ports[i];
Jeff Garzik907f4672005-05-12 15:03:42 -04001714
Jeff Garzikcca39742006-08-24 03:19:22 -04001715 ata_scsi_release(ap->scsi_host);
1716 scsi_host_put(ap->scsi_host);
Jeff Garzik907f4672005-05-12 15:03:42 -04001717 }
1718
Jeff Garzike005f012005-08-30 04:18:28 -04001719 kfree(hpriv);
Jeff Garzikcca39742006-08-24 03:19:22 -04001720 pci_iounmap(pdev, host->mmio_base);
1721 kfree(host);
Jeff Garzikead5de92005-05-31 11:53:57 -04001722
Jeff Garzik907f4672005-05-12 15:03:42 -04001723 if (have_msi)
1724 pci_disable_msi(pdev);
1725 else
1726 pci_intx(pdev, 0);
1727 pci_release_regions(pdev);
Jeff Garzik907f4672005-05-12 15:03:42 -04001728 pci_disable_device(pdev);
1729 dev_set_drvdata(dev, NULL);
1730}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731
1732static int __init ahci_init(void)
1733{
Pavel Roskinb7887192006-08-10 18:13:18 +09001734 return pci_register_driver(&ahci_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735}
1736
Linus Torvalds1da177e2005-04-16 15:20:36 -07001737static void __exit ahci_exit(void)
1738{
1739 pci_unregister_driver(&ahci_pci_driver);
1740}
1741
1742
1743MODULE_AUTHOR("Jeff Garzik");
1744MODULE_DESCRIPTION("AHCI SATA low-level driver");
1745MODULE_LICENSE("GPL");
1746MODULE_DEVICE_TABLE(pci, ahci_pci_tbl);
Jeff Garzik68854332005-08-23 02:53:51 -04001747MODULE_VERSION(DRV_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748
1749module_init(ahci_init);
1750module_exit(ahci_exit);