blob: 705bd8b1ea671512e3c640d03d48f520ac40cd46 [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>
domen@coderock.org87507cf2005-04-08 09:53:06 +020042#include <linux/dma-mapping.h>
Jeff Garzika9524a72005-10-30 14:39:11 -050043#include <linux/device.h>
Tejun Heoedc93052007-10-25 14:59:16 +090044#include <linux/dmi.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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#define DRV_NAME "ahci"
Tejun Heo7d50b602007-09-23 13:19:54 +090050#define DRV_VERSION "3.0"
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
David Milburn87943ac2008-10-13 14:38:36 -050052/* Enclosure Management Control */
53#define EM_CTRL_MSG_TYPE 0x000f0000
54
55/* Enclosure Management LED Message Type */
56#define EM_MSG_LED_HBA_PORT 0x0000000f
57#define EM_MSG_LED_PMP_SLOT 0x0000ff00
58#define EM_MSG_LED_VALUE 0xffff0000
59#define EM_MSG_LED_VALUE_ACTIVITY 0x00070000
60#define EM_MSG_LED_VALUE_OFF 0xfff80000
61#define EM_MSG_LED_VALUE_ON 0x00010000
62
Tejun Heoa22e6442008-03-10 10:25:25 +090063static int ahci_skip_host_reset;
Arjan van de Venf3d7f232009-01-26 02:05:44 -080064static int ahci_ignore_sss;
65
Tejun Heoa22e6442008-03-10 10:25:25 +090066module_param_named(skip_host_reset, ahci_skip_host_reset, int, 0444);
67MODULE_PARM_DESC(skip_host_reset, "skip global host reset (0=don't skip, 1=skip)");
68
Arjan van de Venf3d7f232009-01-26 02:05:44 -080069module_param_named(ignore_sss, ahci_ignore_sss, int, 0444);
70MODULE_PARM_DESC(ignore_sss, "Ignore staggered spinup flag (0=don't ignore, 1=ignore)");
71
Kristen Carlson Accardi31556592007-10-25 01:33:26 -040072static int ahci_enable_alpm(struct ata_port *ap,
73 enum link_pm policy);
74static void ahci_disable_alpm(struct ata_port *ap);
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -070075static ssize_t ahci_led_show(struct ata_port *ap, char *buf);
76static ssize_t ahci_led_store(struct ata_port *ap, const char *buf,
77 size_t size);
78static ssize_t ahci_transmit_led_message(struct ata_port *ap, u32 state,
79 ssize_t size);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
81enum {
82 AHCI_PCI_BAR = 5,
Tejun Heo648a88b2006-11-09 15:08:40 +090083 AHCI_MAX_PORTS = 32,
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 AHCI_MAX_SG = 168, /* hardware max is 64K */
85 AHCI_DMA_BOUNDARY = 0xffffffff,
Tejun Heo12fad3f2006-05-15 21:03:55 +090086 AHCI_MAX_CMDS = 32,
Tejun Heodd410ff2006-05-15 21:03:50 +090087 AHCI_CMD_SZ = 32,
Tejun Heo12fad3f2006-05-15 21:03:55 +090088 AHCI_CMD_SLOT_SZ = AHCI_MAX_CMDS * AHCI_CMD_SZ,
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 AHCI_RX_FIS_SZ = 256,
Jeff Garzika0ea7322005-06-04 01:13:15 -040090 AHCI_CMD_TBL_CDB = 0x40,
Tejun Heodd410ff2006-05-15 21:03:50 +090091 AHCI_CMD_TBL_HDR_SZ = 0x80,
92 AHCI_CMD_TBL_SZ = AHCI_CMD_TBL_HDR_SZ + (AHCI_MAX_SG * 16),
93 AHCI_CMD_TBL_AR_SZ = AHCI_CMD_TBL_SZ * AHCI_MAX_CMDS,
94 AHCI_PORT_PRIV_DMA_SZ = AHCI_CMD_SLOT_SZ + AHCI_CMD_TBL_AR_SZ +
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 AHCI_RX_FIS_SZ,
Shane Huangd6ef3152009-12-09 17:23:04 +080096 AHCI_PORT_PRIV_FBS_DMA_SZ = AHCI_CMD_SLOT_SZ +
97 AHCI_CMD_TBL_AR_SZ +
98 (AHCI_RX_FIS_SZ * 16),
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 AHCI_IRQ_ON_SG = (1 << 31),
100 AHCI_CMD_ATAPI = (1 << 5),
101 AHCI_CMD_WRITE = (1 << 6),
Tejun Heo4b10e552006-03-12 11:25:27 +0900102 AHCI_CMD_PREFETCH = (1 << 7),
Tejun Heo22b49982006-01-23 21:38:44 +0900103 AHCI_CMD_RESET = (1 << 8),
104 AHCI_CMD_CLR_BUSY = (1 << 10),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106 RX_FIS_D2H_REG = 0x40, /* offset of D2H Register FIS data */
Tejun Heo0291f952007-01-25 19:16:28 +0900107 RX_FIS_SDB = 0x58, /* offset of SDB FIS data */
Tejun Heo78cd52d2006-05-15 20:58:29 +0900108 RX_FIS_UNK = 0x60, /* offset of Unknown FIS data */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109
110 board_ahci = 0,
Tejun Heo7a234af2007-09-03 12:44:57 +0900111 board_ahci_vt8251 = 1,
112 board_ahci_ign_iferr = 2,
113 board_ahci_sb600 = 3,
114 board_ahci_mv = 4,
Shane Huange427fe02008-12-30 10:53:41 +0800115 board_ahci_sb700 = 5, /* for SB700 and SB800 */
Tejun Heoe297d992008-06-10 00:13:04 +0900116 board_ahci_mcp65 = 6,
Tejun Heo9a3b1032008-06-18 20:56:58 -0400117 board_ahci_nopmp = 7,
Tejun Heoaa431dd2009-04-08 14:25:31 -0700118 board_ahci_yesncq = 8,
Shaohua Li1b677af2009-11-16 09:56:05 +0800119 board_ahci_nosntf = 9,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120
121 /* global controller registers */
122 HOST_CAP = 0x00, /* host capabilities */
123 HOST_CTL = 0x04, /* global host control */
124 HOST_IRQ_STAT = 0x08, /* interrupt status */
125 HOST_PORTS_IMPL = 0x0c, /* bitmap of implemented ports */
126 HOST_VERSION = 0x10, /* AHCI spec. version compliancy */
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700127 HOST_EM_LOC = 0x1c, /* Enclosure Management location */
128 HOST_EM_CTL = 0x20, /* Enclosure Management Control */
Robert Hancock4c521c82009-09-20 17:02:31 -0600129 HOST_CAP2 = 0x24, /* host capabilities, extended */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 /* HOST_CTL bits */
132 HOST_RESET = (1 << 0), /* reset controller; self-clear */
133 HOST_IRQ_EN = (1 << 1), /* global IRQ enable */
134 HOST_AHCI_EN = (1 << 31), /* AHCI enabled */
135
136 /* HOST_CAP bits */
Robert Hancock4c521c82009-09-20 17:02:31 -0600137 HOST_CAP_SXS = (1 << 5), /* Supports External SATA */
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700138 HOST_CAP_EMS = (1 << 6), /* Enclosure Management support */
Robert Hancock4c521c82009-09-20 17:02:31 -0600139 HOST_CAP_CCC = (1 << 7), /* Command Completion Coalescing */
140 HOST_CAP_PART = (1 << 13), /* Partial state capable */
141 HOST_CAP_SSC = (1 << 14), /* Slumber state capable */
142 HOST_CAP_PIO_MULTI = (1 << 15), /* PIO multiple DRQ support */
143 HOST_CAP_FBS = (1 << 16), /* FIS-based switching support */
Tejun Heo7d50b602007-09-23 13:19:54 +0900144 HOST_CAP_PMP = (1 << 17), /* Port Multiplier support */
Robert Hancock4c521c82009-09-20 17:02:31 -0600145 HOST_CAP_ONLY = (1 << 18), /* Supports AHCI mode only */
Tejun Heo22b49982006-01-23 21:38:44 +0900146 HOST_CAP_CLO = (1 << 24), /* Command List Override support */
Robert Hancock4c521c82009-09-20 17:02:31 -0600147 HOST_CAP_LED = (1 << 25), /* Supports activity LED */
Kristen Carlson Accardi31556592007-10-25 01:33:26 -0400148 HOST_CAP_ALPM = (1 << 26), /* Aggressive Link PM support */
Tejun Heo0be0aa92006-07-26 15:59:26 +0900149 HOST_CAP_SSS = (1 << 27), /* Staggered Spin-up */
Robert Hancock4c521c82009-09-20 17:02:31 -0600150 HOST_CAP_MPS = (1 << 28), /* Mechanical presence switch */
Tejun Heo203ef6c2007-07-16 14:29:40 +0900151 HOST_CAP_SNTF = (1 << 29), /* SNotification register */
Tejun Heo979db802006-05-15 21:03:52 +0900152 HOST_CAP_NCQ = (1 << 30), /* Native Command Queueing */
Tejun Heodd410ff2006-05-15 21:03:50 +0900153 HOST_CAP_64 = (1 << 31), /* PCI DAC (64-bit DMA) support */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154
Robert Hancock4c521c82009-09-20 17:02:31 -0600155 /* HOST_CAP2 bits */
156 HOST_CAP2_BOH = (1 << 0), /* BIOS/OS handoff supported */
157 HOST_CAP2_NVMHCI = (1 << 1), /* NVMHCI supported */
158 HOST_CAP2_APST = (1 << 2), /* Automatic partial to slumber */
159
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 /* registers for each SATA port */
161 PORT_LST_ADDR = 0x00, /* command list DMA addr */
162 PORT_LST_ADDR_HI = 0x04, /* command list DMA addr hi */
163 PORT_FIS_ADDR = 0x08, /* FIS rx buf addr */
164 PORT_FIS_ADDR_HI = 0x0c, /* FIS rx buf addr hi */
165 PORT_IRQ_STAT = 0x10, /* interrupt status */
166 PORT_IRQ_MASK = 0x14, /* interrupt enable/disable mask */
167 PORT_CMD = 0x18, /* port command */
168 PORT_TFDATA = 0x20, /* taskfile data */
169 PORT_SIG = 0x24, /* device TF signature */
170 PORT_CMD_ISSUE = 0x38, /* command issue */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 PORT_SCR_STAT = 0x28, /* SATA phy register: SStatus */
172 PORT_SCR_CTL = 0x2c, /* SATA phy register: SControl */
173 PORT_SCR_ERR = 0x30, /* SATA phy register: SError */
174 PORT_SCR_ACT = 0x34, /* SATA phy register: SActive */
Tejun Heo203ef6c2007-07-16 14:29:40 +0900175 PORT_SCR_NTF = 0x3c, /* SATA phy register: SNotification */
Shane Huangd6ef3152009-12-09 17:23:04 +0800176 PORT_FBS = 0x40, /* FIS-based Switching */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177
178 /* PORT_IRQ_{STAT,MASK} bits */
179 PORT_IRQ_COLD_PRES = (1 << 31), /* cold presence detect */
180 PORT_IRQ_TF_ERR = (1 << 30), /* task file error */
181 PORT_IRQ_HBUS_ERR = (1 << 29), /* host bus fatal error */
182 PORT_IRQ_HBUS_DATA_ERR = (1 << 28), /* host bus data error */
183 PORT_IRQ_IF_ERR = (1 << 27), /* interface fatal error */
184 PORT_IRQ_IF_NONFATAL = (1 << 26), /* interface non-fatal error */
185 PORT_IRQ_OVERFLOW = (1 << 24), /* xfer exhausted available S/G */
186 PORT_IRQ_BAD_PMP = (1 << 23), /* incorrect port multiplier */
187
188 PORT_IRQ_PHYRDY = (1 << 22), /* PhyRdy changed */
189 PORT_IRQ_DEV_ILCK = (1 << 7), /* device interlock */
190 PORT_IRQ_CONNECT = (1 << 6), /* port connect change status */
191 PORT_IRQ_SG_DONE = (1 << 5), /* descriptor processed */
192 PORT_IRQ_UNK_FIS = (1 << 4), /* unknown FIS rx'd */
193 PORT_IRQ_SDB_FIS = (1 << 3), /* Set Device Bits FIS rx'd */
194 PORT_IRQ_DMAS_FIS = (1 << 2), /* DMA Setup FIS rx'd */
195 PORT_IRQ_PIOS_FIS = (1 << 1), /* PIO Setup FIS rx'd */
196 PORT_IRQ_D2H_REG_FIS = (1 << 0), /* D2H Register FIS rx'd */
197
Tejun Heo78cd52d2006-05-15 20:58:29 +0900198 PORT_IRQ_FREEZE = PORT_IRQ_HBUS_ERR |
199 PORT_IRQ_IF_ERR |
200 PORT_IRQ_CONNECT |
Tejun Heo42969712006-05-31 18:28:18 +0900201 PORT_IRQ_PHYRDY |
Tejun Heo7d50b602007-09-23 13:19:54 +0900202 PORT_IRQ_UNK_FIS |
203 PORT_IRQ_BAD_PMP,
Tejun Heo78cd52d2006-05-15 20:58:29 +0900204 PORT_IRQ_ERROR = PORT_IRQ_FREEZE |
205 PORT_IRQ_TF_ERR |
206 PORT_IRQ_HBUS_DATA_ERR,
207 DEF_PORT_IRQ = PORT_IRQ_ERROR | PORT_IRQ_SG_DONE |
208 PORT_IRQ_SDB_FIS | PORT_IRQ_DMAS_FIS |
209 PORT_IRQ_PIOS_FIS | PORT_IRQ_D2H_REG_FIS,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210
211 /* PORT_CMD bits */
Kristen Carlson Accardi31556592007-10-25 01:33:26 -0400212 PORT_CMD_ASP = (1 << 27), /* Aggressive Slumber/Partial */
213 PORT_CMD_ALPE = (1 << 26), /* Aggressive Link PM enable */
Jeff Garzik02eaa662005-11-12 01:32:19 -0500214 PORT_CMD_ATAPI = (1 << 24), /* Device is ATAPI */
Shane Huangd6ef3152009-12-09 17:23:04 +0800215 PORT_CMD_FBSCP = (1 << 22), /* FBS Capable Port */
Tejun Heo7d50b602007-09-23 13:19:54 +0900216 PORT_CMD_PMP = (1 << 17), /* PMP attached */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 PORT_CMD_LIST_ON = (1 << 15), /* cmd list DMA engine running */
218 PORT_CMD_FIS_ON = (1 << 14), /* FIS DMA engine running */
219 PORT_CMD_FIS_RX = (1 << 4), /* Enable FIS receive DMA engine */
Tejun Heo22b49982006-01-23 21:38:44 +0900220 PORT_CMD_CLO = (1 << 3), /* Command list override */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 PORT_CMD_POWER_ON = (1 << 2), /* Power up device */
222 PORT_CMD_SPIN_UP = (1 << 1), /* Spin up device */
223 PORT_CMD_START = (1 << 0), /* Enable port DMA engine */
224
Tejun Heo0be0aa92006-07-26 15:59:26 +0900225 PORT_CMD_ICC_MASK = (0xf << 28), /* i/f ICC state mask */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226 PORT_CMD_ICC_ACTIVE = (0x1 << 28), /* Put i/f in active state */
227 PORT_CMD_ICC_PARTIAL = (0x2 << 28), /* Put i/f in partial state */
228 PORT_CMD_ICC_SLUMBER = (0x6 << 28), /* Put i/f in slumber state */
Jeff Garzik4b0060f2005-06-04 00:50:22 -0400229
Shane Huangd6ef3152009-12-09 17:23:04 +0800230 PORT_FBS_DWE_OFFSET = 16, /* FBS device with error offset */
231 PORT_FBS_ADO_OFFSET = 12, /* FBS active dev optimization offset */
232 PORT_FBS_DEV_OFFSET = 8, /* FBS device to issue offset */
233 PORT_FBS_DEV_MASK = (0xf << PORT_FBS_DEV_OFFSET), /* FBS.DEV */
234 PORT_FBS_SDE = (1 << 2), /* FBS single device error */
235 PORT_FBS_DEC = (1 << 1), /* FBS device error clear */
236 PORT_FBS_EN = (1 << 0), /* Enable FBS */
237
Tejun Heo417a1a62007-09-23 13:19:55 +0900238 /* hpriv->flags bits */
239 AHCI_HFLAG_NO_NCQ = (1 << 0),
240 AHCI_HFLAG_IGN_IRQ_IF_ERR = (1 << 1), /* ignore IRQ_IF_ERR */
241 AHCI_HFLAG_IGN_SERR_INTERNAL = (1 << 2), /* ignore SERR_INTERNAL */
242 AHCI_HFLAG_32BIT_ONLY = (1 << 3), /* force 32bit */
243 AHCI_HFLAG_MV_PATA = (1 << 4), /* PATA port */
244 AHCI_HFLAG_NO_MSI = (1 << 5), /* no PCI MSI */
Tejun Heo6949b912007-09-23 13:19:55 +0900245 AHCI_HFLAG_NO_PMP = (1 << 6), /* no PMP */
Kristen Carlson Accardi31556592007-10-25 01:33:26 -0400246 AHCI_HFLAG_NO_HOTPLUG = (1 << 7), /* ignore PxSERR.DIAG.N */
Jeff Garzika8785392008-02-28 15:43:48 -0500247 AHCI_HFLAG_SECT255 = (1 << 8), /* max 255 sectors */
Tejun Heoe297d992008-06-10 00:13:04 +0900248 AHCI_HFLAG_YES_NCQ = (1 << 9), /* force NCQ cap on */
Tejun Heo9b10ae82009-05-30 20:50:12 +0900249 AHCI_HFLAG_NO_SUSPEND = (1 << 10), /* don't suspend */
Tejun Heo55946392009-08-04 14:30:08 +0900250 AHCI_HFLAG_SRST_TOUT_IS_OFFLINE = (1 << 11), /* treat SRST timeout as
251 link offline */
Shaohua Li1b677af2009-11-16 09:56:05 +0800252 AHCI_HFLAG_NO_SNTF = (1 << 12), /* no sntf */
Tejun Heo417a1a62007-09-23 13:19:55 +0900253
Bastiaan Jacquesbf2af2a2006-04-17 14:17:59 +0200254 /* ap->flags bits */
Tejun Heo1188c0d2007-04-23 02:41:05 +0900255
256 AHCI_FLAG_COMMON = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
257 ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA |
Kristen Carlson Accardi31556592007-10-25 01:33:26 -0400258 ATA_FLAG_ACPI_SATA | ATA_FLAG_AN |
259 ATA_FLAG_IPM,
Tejun Heoc4f77922007-12-06 15:09:43 +0900260
261 ICH_MAP = 0x90, /* ICH MAP register */
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700262
Tejun Heod50ce072009-05-12 10:57:41 +0900263 /* em constants */
264 EM_MAX_SLOTS = 8,
265 EM_MAX_RETRY = 5,
266
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700267 /* em_ctl bits */
268 EM_CTL_RST = (1 << 9), /* Reset */
269 EM_CTL_TM = (1 << 8), /* Transmit Message */
270 EM_CTL_ALHD = (1 << 26), /* Activity LED */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271};
272
273struct ahci_cmd_hdr {
Al Viro4ca4e432007-12-30 09:32:22 +0000274 __le32 opts;
275 __le32 status;
276 __le32 tbl_addr;
277 __le32 tbl_addr_hi;
278 __le32 reserved[4];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279};
280
281struct ahci_sg {
Al Viro4ca4e432007-12-30 09:32:22 +0000282 __le32 addr;
283 __le32 addr_hi;
284 __le32 reserved;
285 __le32 flags_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286};
287
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700288struct ahci_em_priv {
289 enum sw_activity blink_policy;
290 struct timer_list timer;
291 unsigned long saved_activity;
292 unsigned long activity;
293 unsigned long led_state;
294};
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296struct ahci_host_priv {
Tejun Heo417a1a62007-09-23 13:19:55 +0900297 unsigned int flags; /* AHCI_HFLAG_* */
Tejun Heod447df12007-03-18 22:15:33 +0900298 u32 cap; /* cap to use */
Robert Hancock4c521c82009-09-20 17:02:31 -0600299 u32 cap2; /* cap2 to use */
Tejun Heod447df12007-03-18 22:15:33 +0900300 u32 port_map; /* port map to use */
301 u32 saved_cap; /* saved initial cap */
Robert Hancock4c521c82009-09-20 17:02:31 -0600302 u32 saved_cap2; /* saved initial cap2 */
Tejun Heod447df12007-03-18 22:15:33 +0900303 u32 saved_port_map; /* saved initial port_map */
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700304 u32 em_loc; /* enclosure management location */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305};
306
307struct ahci_port_priv {
Tejun Heo7d50b602007-09-23 13:19:54 +0900308 struct ata_link *active_link;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 struct ahci_cmd_hdr *cmd_slot;
310 dma_addr_t cmd_slot_dma;
311 void *cmd_tbl;
312 dma_addr_t cmd_tbl_dma;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 void *rx_fis;
314 dma_addr_t rx_fis_dma;
Tejun Heo0291f952007-01-25 19:16:28 +0900315 /* for NCQ spurious interrupt analysis */
Tejun Heo0291f952007-01-25 19:16:28 +0900316 unsigned int ncq_saw_d2h:1;
317 unsigned int ncq_saw_dmas:1;
Tejun Heoafb2d552007-02-27 13:24:19 +0900318 unsigned int ncq_saw_sdb:1;
Kristen Carlson Accardia7384922007-08-09 14:23:41 -0700319 u32 intr_mask; /* interrupts to enable */
Shane Huangd6ef3152009-12-09 17:23:04 +0800320 bool fbs_supported; /* set iff FBS is supported */
321 bool fbs_enabled; /* set iff FBS is enabled */
322 int fbs_last_dev; /* save FBS.DEV of last FIS */
Tejun Heod50ce072009-05-12 10:57:41 +0900323 /* enclosure management info per PM slot */
324 struct ahci_em_priv em_priv[EM_MAX_SLOTS];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325};
326
Tejun Heo82ef04f2008-07-31 17:02:40 +0900327static int ahci_scr_read(struct ata_link *link, unsigned int sc_reg, u32 *val);
328static int ahci_scr_write(struct ata_link *link, unsigned int sc_reg, u32 val);
Jeff Garzik2dcb4072007-10-19 06:42:56 -0400329static int ahci_init_one(struct pci_dev *pdev, const struct pci_device_id *ent);
Tejun Heo9a3d9eb2006-01-23 13:09:36 +0900330static unsigned int ahci_qc_issue(struct ata_queued_cmd *qc);
Tejun Heo4c9bf4e2008-04-07 22:47:20 +0900331static bool ahci_qc_fill_rtf(struct ata_queued_cmd *qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332static int ahci_port_start(struct ata_port *ap);
333static void ahci_port_stop(struct ata_port *ap);
Shane Huangd6ef3152009-12-09 17:23:04 +0800334static int ahci_pmp_qc_defer(struct ata_queued_cmd *qc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335static void ahci_qc_prep(struct ata_queued_cmd *qc);
Tejun Heo78cd52d2006-05-15 20:58:29 +0900336static void ahci_freeze(struct ata_port *ap);
337static void ahci_thaw(struct ata_port *ap);
Shane Huangd6ef3152009-12-09 17:23:04 +0800338static void ahci_enable_fbs(struct ata_port *ap);
339static void ahci_disable_fbs(struct ata_port *ap);
Tejun Heo7d50b602007-09-23 13:19:54 +0900340static void ahci_pmp_attach(struct ata_port *ap);
341static void ahci_pmp_detach(struct ata_port *ap);
Tejun Heoa1efdab2008-03-25 12:22:50 +0900342static int ahci_softreset(struct ata_link *link, unsigned int *class,
343 unsigned long deadline);
Shane Huangbd172432008-06-10 15:52:04 +0800344static int ahci_sb600_softreset(struct ata_link *link, unsigned int *class,
345 unsigned long deadline);
Tejun Heoa1efdab2008-03-25 12:22:50 +0900346static int ahci_hardreset(struct ata_link *link, unsigned int *class,
347 unsigned long deadline);
348static int ahci_vt8251_hardreset(struct ata_link *link, unsigned int *class,
349 unsigned long deadline);
350static int ahci_p5wdh_hardreset(struct ata_link *link, unsigned int *class,
351 unsigned long deadline);
352static void ahci_postreset(struct ata_link *link, unsigned int *class);
Tejun Heo78cd52d2006-05-15 20:58:29 +0900353static void ahci_error_handler(struct ata_port *ap);
354static void ahci_post_internal_cmd(struct ata_queued_cmd *qc);
Jeff Garzikdf69c9c2007-05-26 20:46:51 -0400355static int ahci_port_resume(struct ata_port *ap);
Jeff Garzika8785392008-02-28 15:43:48 -0500356static void ahci_dev_config(struct ata_device *dev);
Jeff Garzikdab632e2007-05-28 08:33:01 -0400357static void ahci_fill_cmd_slot(struct ahci_port_priv *pp, unsigned int tag,
358 u32 opts);
Tejun Heo438ac6d2007-03-02 17:31:26 +0900359#ifdef CONFIG_PM
Tejun Heoc1332872006-07-26 15:59:26 +0900360static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg);
Tejun Heoc1332872006-07-26 15:59:26 +0900361static int ahci_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg);
362static int ahci_pci_device_resume(struct pci_dev *pdev);
Tejun Heo438ac6d2007-03-02 17:31:26 +0900363#endif
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700364static ssize_t ahci_activity_show(struct ata_device *dev, char *buf);
365static ssize_t ahci_activity_store(struct ata_device *dev,
366 enum sw_activity val);
367static void ahci_init_sw_activity(struct ata_link *link);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368
Matthew Garrett77cdec12009-07-17 19:13:47 +0100369static ssize_t ahci_show_host_caps(struct device *dev,
370 struct device_attribute *attr, char *buf);
Robert Hancock4c521c82009-09-20 17:02:31 -0600371static ssize_t ahci_show_host_cap2(struct device *dev,
372 struct device_attribute *attr, char *buf);
Matthew Garrett77cdec12009-07-17 19:13:47 +0100373static ssize_t ahci_show_host_version(struct device *dev,
374 struct device_attribute *attr, char *buf);
375static ssize_t ahci_show_port_cmd(struct device *dev,
376 struct device_attribute *attr, char *buf);
377
378DEVICE_ATTR(ahci_host_caps, S_IRUGO, ahci_show_host_caps, NULL);
Robert Hancock4c521c82009-09-20 17:02:31 -0600379DEVICE_ATTR(ahci_host_cap2, S_IRUGO, ahci_show_host_cap2, NULL);
Matthew Garrett77cdec12009-07-17 19:13:47 +0100380DEVICE_ATTR(ahci_host_version, S_IRUGO, ahci_show_host_version, NULL);
381DEVICE_ATTR(ahci_port_cmd, S_IRUGO, ahci_show_port_cmd, NULL);
382
Tony Jonesee959b02008-02-22 00:13:36 +0100383static struct device_attribute *ahci_shost_attrs[] = {
384 &dev_attr_link_power_management_policy,
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700385 &dev_attr_em_message_type,
386 &dev_attr_em_message,
Matthew Garrett77cdec12009-07-17 19:13:47 +0100387 &dev_attr_ahci_host_caps,
Robert Hancock4c521c82009-09-20 17:02:31 -0600388 &dev_attr_ahci_host_cap2,
Matthew Garrett77cdec12009-07-17 19:13:47 +0100389 &dev_attr_ahci_host_version,
390 &dev_attr_ahci_port_cmd,
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700391 NULL
392};
393
394static struct device_attribute *ahci_sdev_attrs[] = {
395 &dev_attr_sw_activity,
Elias Oltmanns45fabbb2008-09-21 11:54:08 +0200396 &dev_attr_unload_heads,
Kristen Carlson Accardi31556592007-10-25 01:33:26 -0400397 NULL
398};
399
Jeff Garzik193515d2005-11-07 00:59:37 -0500400static struct scsi_host_template ahci_sht = {
Tejun Heo68d1d072008-03-25 12:22:49 +0900401 ATA_NCQ_SHT(DRV_NAME),
Tejun Heo12fad3f2006-05-15 21:03:55 +0900402 .can_queue = AHCI_MAX_CMDS - 1,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 .sg_tablesize = AHCI_MAX_SG,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 .dma_boundary = AHCI_DMA_BOUNDARY,
Kristen Carlson Accardi31556592007-10-25 01:33:26 -0400405 .shost_attrs = ahci_shost_attrs,
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700406 .sdev_attrs = ahci_sdev_attrs,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407};
408
Tejun Heo029cfd62008-03-25 12:22:49 +0900409static struct ata_port_operations ahci_ops = {
410 .inherits = &sata_pmp_port_ops,
411
Shane Huangd6ef3152009-12-09 17:23:04 +0800412 .qc_defer = ahci_pmp_qc_defer,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700413 .qc_prep = ahci_qc_prep,
414 .qc_issue = ahci_qc_issue,
Tejun Heo4c9bf4e2008-04-07 22:47:20 +0900415 .qc_fill_rtf = ahci_qc_fill_rtf,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416
Tejun Heo78cd52d2006-05-15 20:58:29 +0900417 .freeze = ahci_freeze,
418 .thaw = ahci_thaw,
Tejun Heoa1efdab2008-03-25 12:22:50 +0900419 .softreset = ahci_softreset,
420 .hardreset = ahci_hardreset,
421 .postreset = ahci_postreset,
Tejun Heo071f44b2008-04-07 22:47:22 +0900422 .pmp_softreset = ahci_softreset,
Tejun Heo78cd52d2006-05-15 20:58:29 +0900423 .error_handler = ahci_error_handler,
424 .post_internal_cmd = ahci_post_internal_cmd,
Tejun Heo029cfd62008-03-25 12:22:49 +0900425 .dev_config = ahci_dev_config,
Tejun Heo78cd52d2006-05-15 20:58:29 +0900426
Tejun Heo029cfd62008-03-25 12:22:49 +0900427 .scr_read = ahci_scr_read,
428 .scr_write = ahci_scr_write,
Tejun Heo7d50b602007-09-23 13:19:54 +0900429 .pmp_attach = ahci_pmp_attach,
430 .pmp_detach = ahci_pmp_detach,
Tejun Heo7d50b602007-09-23 13:19:54 +0900431
Tejun Heo029cfd62008-03-25 12:22:49 +0900432 .enable_pm = ahci_enable_alpm,
433 .disable_pm = ahci_disable_alpm,
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700434 .em_show = ahci_led_show,
435 .em_store = ahci_led_store,
436 .sw_activity_show = ahci_activity_show,
437 .sw_activity_store = ahci_activity_store,
Tejun Heo438ac6d2007-03-02 17:31:26 +0900438#ifdef CONFIG_PM
Tejun Heoc1332872006-07-26 15:59:26 +0900439 .port_suspend = ahci_port_suspend,
440 .port_resume = ahci_port_resume,
Tejun Heo438ac6d2007-03-02 17:31:26 +0900441#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 .port_start = ahci_port_start,
443 .port_stop = ahci_port_stop,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444};
445
Tejun Heo029cfd62008-03-25 12:22:49 +0900446static struct ata_port_operations ahci_vt8251_ops = {
447 .inherits = &ahci_ops,
Tejun Heoa1efdab2008-03-25 12:22:50 +0900448 .hardreset = ahci_vt8251_hardreset,
Tejun Heoad616ff2006-11-01 18:00:24 +0900449};
450
Tejun Heo029cfd62008-03-25 12:22:49 +0900451static struct ata_port_operations ahci_p5wdh_ops = {
452 .inherits = &ahci_ops,
Tejun Heoa1efdab2008-03-25 12:22:50 +0900453 .hardreset = ahci_p5wdh_hardreset,
Tejun Heoedc93052007-10-25 14:59:16 +0900454};
455
Shane Huangbd172432008-06-10 15:52:04 +0800456static struct ata_port_operations ahci_sb600_ops = {
457 .inherits = &ahci_ops,
458 .softreset = ahci_sb600_softreset,
459 .pmp_softreset = ahci_sb600_softreset,
460};
461
Tejun Heo417a1a62007-09-23 13:19:55 +0900462#define AHCI_HFLAGS(flags) .private_data = (void *)(flags)
463
Arjan van de Ven98ac62d2005-11-28 10:06:23 +0100464static const struct ata_port_info ahci_port_info[] = {
Jeff Garzik4da646b2009-04-08 02:00:13 -0400465 [board_ahci] =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466 {
Tejun Heo1188c0d2007-04-23 02:41:05 +0900467 .flags = AHCI_FLAG_COMMON,
Erik Inge Bolsø14bdef92009-03-14 21:38:24 +0100468 .pio_mask = ATA_PIO4,
Jeff Garzik469248a2007-07-08 01:13:16 -0400469 .udma_mask = ATA_UDMA6,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 .port_ops = &ahci_ops,
471 },
Jeff Garzik4da646b2009-04-08 02:00:13 -0400472 [board_ahci_vt8251] =
Bastiaan Jacquesbf2af2a2006-04-17 14:17:59 +0200473 {
Tejun Heo6949b912007-09-23 13:19:55 +0900474 AHCI_HFLAGS (AHCI_HFLAG_NO_NCQ | AHCI_HFLAG_NO_PMP),
Tejun Heo417a1a62007-09-23 13:19:55 +0900475 .flags = AHCI_FLAG_COMMON,
Erik Inge Bolsø14bdef92009-03-14 21:38:24 +0100476 .pio_mask = ATA_PIO4,
Jeff Garzik469248a2007-07-08 01:13:16 -0400477 .udma_mask = ATA_UDMA6,
Tejun Heoad616ff2006-11-01 18:00:24 +0900478 .port_ops = &ahci_vt8251_ops,
Bastiaan Jacquesbf2af2a2006-04-17 14:17:59 +0200479 },
Jeff Garzik4da646b2009-04-08 02:00:13 -0400480 [board_ahci_ign_iferr] =
Tejun Heo41669552006-11-29 11:33:14 +0900481 {
Tejun Heo417a1a62007-09-23 13:19:55 +0900482 AHCI_HFLAGS (AHCI_HFLAG_IGN_IRQ_IF_ERR),
483 .flags = AHCI_FLAG_COMMON,
Erik Inge Bolsø14bdef92009-03-14 21:38:24 +0100484 .pio_mask = ATA_PIO4,
Jeff Garzik469248a2007-07-08 01:13:16 -0400485 .udma_mask = ATA_UDMA6,
Tejun Heo41669552006-11-29 11:33:14 +0900486 .port_ops = &ahci_ops,
487 },
Jeff Garzik4da646b2009-04-08 02:00:13 -0400488 [board_ahci_sb600] =
Conke Hu55a61602007-03-27 18:33:05 +0800489 {
Tejun Heo417a1a62007-09-23 13:19:55 +0900490 AHCI_HFLAGS (AHCI_HFLAG_IGN_SERR_INTERNAL |
Tejun Heo2fcad9d2009-10-03 18:27:29 +0900491 AHCI_HFLAG_NO_MSI | AHCI_HFLAG_SECT255 |
492 AHCI_HFLAG_32BIT_ONLY),
Tejun Heo417a1a62007-09-23 13:19:55 +0900493 .flags = AHCI_FLAG_COMMON,
Erik Inge Bolsø14bdef92009-03-14 21:38:24 +0100494 .pio_mask = ATA_PIO4,
Jeff Garzik469248a2007-07-08 01:13:16 -0400495 .udma_mask = ATA_UDMA6,
Shane Huangbd172432008-06-10 15:52:04 +0800496 .port_ops = &ahci_sb600_ops,
Conke Hu55a61602007-03-27 18:33:05 +0800497 },
Jeff Garzik4da646b2009-04-08 02:00:13 -0400498 [board_ahci_mv] =
Jeff Garzikcd70c262007-07-08 02:29:42 -0400499 {
Tejun Heo417a1a62007-09-23 13:19:55 +0900500 AHCI_HFLAGS (AHCI_HFLAG_NO_NCQ | AHCI_HFLAG_NO_MSI |
Tejun Heo17248462008-08-29 16:03:59 +0200501 AHCI_HFLAG_MV_PATA | AHCI_HFLAG_NO_PMP),
Jeff Garzikcd70c262007-07-08 02:29:42 -0400502 .flags = ATA_FLAG_SATA | ATA_FLAG_NO_LEGACY |
Tejun Heo417a1a62007-09-23 13:19:55 +0900503 ATA_FLAG_MMIO | ATA_FLAG_PIO_DMA,
Erik Inge Bolsø14bdef92009-03-14 21:38:24 +0100504 .pio_mask = ATA_PIO4,
Jeff Garzikcd70c262007-07-08 02:29:42 -0400505 .udma_mask = ATA_UDMA6,
506 .port_ops = &ahci_ops,
507 },
Jeff Garzik4da646b2009-04-08 02:00:13 -0400508 [board_ahci_sb700] = /* for SB700 and SB800 */
Shane Huange39fc8c2008-02-22 05:00:31 -0800509 {
Shane Huangbd172432008-06-10 15:52:04 +0800510 AHCI_HFLAGS (AHCI_HFLAG_IGN_SERR_INTERNAL),
Shane Huange39fc8c2008-02-22 05:00:31 -0800511 .flags = AHCI_FLAG_COMMON,
Erik Inge Bolsø14bdef92009-03-14 21:38:24 +0100512 .pio_mask = ATA_PIO4,
Shane Huange39fc8c2008-02-22 05:00:31 -0800513 .udma_mask = ATA_UDMA6,
Shane Huangbd172432008-06-10 15:52:04 +0800514 .port_ops = &ahci_sb600_ops,
Shane Huange39fc8c2008-02-22 05:00:31 -0800515 },
Jeff Garzik4da646b2009-04-08 02:00:13 -0400516 [board_ahci_mcp65] =
Tejun Heoe297d992008-06-10 00:13:04 +0900517 {
518 AHCI_HFLAGS (AHCI_HFLAG_YES_NCQ),
519 .flags = AHCI_FLAG_COMMON,
Erik Inge Bolsø14bdef92009-03-14 21:38:24 +0100520 .pio_mask = ATA_PIO4,
Tejun Heoe297d992008-06-10 00:13:04 +0900521 .udma_mask = ATA_UDMA6,
522 .port_ops = &ahci_ops,
523 },
Jeff Garzik4da646b2009-04-08 02:00:13 -0400524 [board_ahci_nopmp] =
Tejun Heo9a3b1032008-06-18 20:56:58 -0400525 {
526 AHCI_HFLAGS (AHCI_HFLAG_NO_PMP),
527 .flags = AHCI_FLAG_COMMON,
Erik Inge Bolsø14bdef92009-03-14 21:38:24 +0100528 .pio_mask = ATA_PIO4,
Tejun Heo9a3b1032008-06-18 20:56:58 -0400529 .udma_mask = ATA_UDMA6,
530 .port_ops = &ahci_ops,
531 },
Shaohua Li1b677af2009-11-16 09:56:05 +0800532 [board_ahci_yesncq] =
Tejun Heoaa431dd2009-04-08 14:25:31 -0700533 {
534 AHCI_HFLAGS (AHCI_HFLAG_YES_NCQ),
535 .flags = AHCI_FLAG_COMMON,
536 .pio_mask = ATA_PIO4,
537 .udma_mask = ATA_UDMA6,
538 .port_ops = &ahci_ops,
539 },
Shaohua Li1b677af2009-11-16 09:56:05 +0800540 [board_ahci_nosntf] =
541 {
542 AHCI_HFLAGS (AHCI_HFLAG_NO_SNTF),
543 .flags = AHCI_FLAG_COMMON,
544 .pio_mask = ATA_PIO4,
545 .udma_mask = ATA_UDMA6,
546 .port_ops = &ahci_ops,
547 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700548};
549
Jeff Garzik3b7d6972005-11-10 11:04:11 -0500550static const struct pci_device_id ahci_pci_tbl[] = {
Jeff Garzikfe7fa312006-06-22 23:05:36 -0400551 /* Intel */
Jeff Garzik54bb3a942006-09-27 22:20:11 -0400552 { PCI_VDEVICE(INTEL, 0x2652), board_ahci }, /* ICH6 */
553 { PCI_VDEVICE(INTEL, 0x2653), board_ahci }, /* ICH6M */
554 { PCI_VDEVICE(INTEL, 0x27c1), board_ahci }, /* ICH7 */
555 { PCI_VDEVICE(INTEL, 0x27c5), board_ahci }, /* ICH7M */
556 { PCI_VDEVICE(INTEL, 0x27c3), board_ahci }, /* ICH7R */
Tejun Heo82490c02007-01-23 15:13:39 +0900557 { PCI_VDEVICE(AL, 0x5288), board_ahci_ign_iferr }, /* ULi M5288 */
Jeff Garzik54bb3a942006-09-27 22:20:11 -0400558 { PCI_VDEVICE(INTEL, 0x2681), board_ahci }, /* ESB2 */
559 { PCI_VDEVICE(INTEL, 0x2682), board_ahci }, /* ESB2 */
560 { PCI_VDEVICE(INTEL, 0x2683), board_ahci }, /* ESB2 */
561 { PCI_VDEVICE(INTEL, 0x27c6), board_ahci }, /* ICH7-M DH */
Tejun Heo7a234af2007-09-03 12:44:57 +0900562 { PCI_VDEVICE(INTEL, 0x2821), board_ahci }, /* ICH8 */
Shaohua Li1b677af2009-11-16 09:56:05 +0800563 { PCI_VDEVICE(INTEL, 0x2822), board_ahci_nosntf }, /* ICH8 */
Tejun Heo7a234af2007-09-03 12:44:57 +0900564 { PCI_VDEVICE(INTEL, 0x2824), board_ahci }, /* ICH8 */
565 { PCI_VDEVICE(INTEL, 0x2829), board_ahci }, /* ICH8M */
566 { PCI_VDEVICE(INTEL, 0x282a), board_ahci }, /* ICH8M */
567 { PCI_VDEVICE(INTEL, 0x2922), board_ahci }, /* ICH9 */
568 { PCI_VDEVICE(INTEL, 0x2923), board_ahci }, /* ICH9 */
569 { PCI_VDEVICE(INTEL, 0x2924), board_ahci }, /* ICH9 */
570 { PCI_VDEVICE(INTEL, 0x2925), board_ahci }, /* ICH9 */
571 { PCI_VDEVICE(INTEL, 0x2927), board_ahci }, /* ICH9 */
572 { PCI_VDEVICE(INTEL, 0x2929), board_ahci }, /* ICH9M */
573 { PCI_VDEVICE(INTEL, 0x292a), board_ahci }, /* ICH9M */
574 { PCI_VDEVICE(INTEL, 0x292b), board_ahci }, /* ICH9M */
575 { PCI_VDEVICE(INTEL, 0x292c), board_ahci }, /* ICH9M */
576 { PCI_VDEVICE(INTEL, 0x292f), board_ahci }, /* ICH9M */
577 { PCI_VDEVICE(INTEL, 0x294d), board_ahci }, /* ICH9 */
578 { PCI_VDEVICE(INTEL, 0x294e), board_ahci }, /* ICH9M */
Jason Gastond4155e62007-09-20 17:35:00 -0400579 { PCI_VDEVICE(INTEL, 0x502a), board_ahci }, /* Tolapai */
580 { PCI_VDEVICE(INTEL, 0x502b), board_ahci }, /* Tolapai */
Jason Gaston16ad1ad2008-01-28 17:34:14 -0800581 { PCI_VDEVICE(INTEL, 0x3a05), board_ahci }, /* ICH10 */
Mark Goodwinb2dde6a2009-06-26 10:44:11 -0500582 { PCI_VDEVICE(INTEL, 0x3a22), board_ahci }, /* ICH10 */
Jason Gaston16ad1ad2008-01-28 17:34:14 -0800583 { PCI_VDEVICE(INTEL, 0x3a25), board_ahci }, /* ICH10 */
David Milburnc1f57d92009-07-22 15:15:56 -0500584 { PCI_VDEVICE(INTEL, 0x3b22), board_ahci }, /* PCH AHCI */
585 { PCI_VDEVICE(INTEL, 0x3b23), board_ahci }, /* PCH AHCI */
Seth Heasleyadcb5302008-08-11 17:03:09 -0700586 { PCI_VDEVICE(INTEL, 0x3b24), board_ahci }, /* PCH RAID */
Seth Heasley8e48b6b2008-08-27 16:47:22 -0700587 { PCI_VDEVICE(INTEL, 0x3b25), board_ahci }, /* PCH RAID */
David Milburnc1f57d92009-07-22 15:15:56 -0500588 { PCI_VDEVICE(INTEL, 0x3b29), board_ahci }, /* PCH AHCI */
Seth Heasleyadcb5302008-08-11 17:03:09 -0700589 { PCI_VDEVICE(INTEL, 0x3b2b), board_ahci }, /* PCH RAID */
Seth Heasley8e48b6b2008-08-27 16:47:22 -0700590 { PCI_VDEVICE(INTEL, 0x3b2c), board_ahci }, /* PCH RAID */
David Milburnc1f57d92009-07-22 15:15:56 -0500591 { PCI_VDEVICE(INTEL, 0x3b2f), board_ahci }, /* PCH AHCI */
Jeff Garzikfe7fa312006-06-22 23:05:36 -0400592
Tejun Heoe34bb372007-02-26 20:24:03 +0900593 /* JMicron 360/1/3/5/6, match class to avoid IDE function */
594 { PCI_VENDOR_ID_JMICRON, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
595 PCI_CLASS_STORAGE_SATA_AHCI, 0xffffff, board_ahci_ign_iferr },
Jeff Garzikfe7fa312006-06-22 23:05:36 -0400596
597 /* ATI */
Conke Huc65ec1c2007-04-11 18:23:14 +0800598 { PCI_VDEVICE(ATI, 0x4380), board_ahci_sb600 }, /* ATI SB600 */
Shane Huange39fc8c2008-02-22 05:00:31 -0800599 { PCI_VDEVICE(ATI, 0x4390), board_ahci_sb700 }, /* ATI SB700/800 */
600 { PCI_VDEVICE(ATI, 0x4391), board_ahci_sb700 }, /* ATI SB700/800 */
601 { PCI_VDEVICE(ATI, 0x4392), board_ahci_sb700 }, /* ATI SB700/800 */
602 { PCI_VDEVICE(ATI, 0x4393), board_ahci_sb700 }, /* ATI SB700/800 */
603 { PCI_VDEVICE(ATI, 0x4394), board_ahci_sb700 }, /* ATI SB700/800 */
604 { PCI_VDEVICE(ATI, 0x4395), board_ahci_sb700 }, /* ATI SB700/800 */
Jeff Garzikfe7fa312006-06-22 23:05:36 -0400605
Shane Huange2dd90b2009-07-29 11:34:49 +0800606 /* AMD */
Shane Huang5deab532009-10-13 11:14:00 +0800607 { PCI_VDEVICE(AMD, 0x7800), board_ahci }, /* AMD Hudson-2 */
Shane Huange2dd90b2009-07-29 11:34:49 +0800608 /* AMD is using RAID class only for ahci controllers */
609 { PCI_VENDOR_ID_AMD, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
610 PCI_CLASS_STORAGE_RAID << 8, 0xffffff, board_ahci },
611
Jeff Garzikfe7fa312006-06-22 23:05:36 -0400612 /* VIA */
Jeff Garzik54bb3a942006-09-27 22:20:11 -0400613 { PCI_VDEVICE(VIA, 0x3349), board_ahci_vt8251 }, /* VIA VT8251 */
Tejun Heobf335542007-04-11 17:27:14 +0900614 { PCI_VDEVICE(VIA, 0x6287), board_ahci_vt8251 }, /* VIA VT8251 */
Jeff Garzikfe7fa312006-06-22 23:05:36 -0400615
616 /* NVIDIA */
Tejun Heoe297d992008-06-10 00:13:04 +0900617 { PCI_VDEVICE(NVIDIA, 0x044c), board_ahci_mcp65 }, /* MCP65 */
618 { PCI_VDEVICE(NVIDIA, 0x044d), board_ahci_mcp65 }, /* MCP65 */
619 { PCI_VDEVICE(NVIDIA, 0x044e), board_ahci_mcp65 }, /* MCP65 */
620 { PCI_VDEVICE(NVIDIA, 0x044f), board_ahci_mcp65 }, /* MCP65 */
621 { PCI_VDEVICE(NVIDIA, 0x045c), board_ahci_mcp65 }, /* MCP65 */
622 { PCI_VDEVICE(NVIDIA, 0x045d), board_ahci_mcp65 }, /* MCP65 */
623 { PCI_VDEVICE(NVIDIA, 0x045e), board_ahci_mcp65 }, /* MCP65 */
624 { PCI_VDEVICE(NVIDIA, 0x045f), board_ahci_mcp65 }, /* MCP65 */
Tejun Heoaa431dd2009-04-08 14:25:31 -0700625 { PCI_VDEVICE(NVIDIA, 0x0550), board_ahci_yesncq }, /* MCP67 */
626 { PCI_VDEVICE(NVIDIA, 0x0551), board_ahci_yesncq }, /* MCP67 */
627 { PCI_VDEVICE(NVIDIA, 0x0552), board_ahci_yesncq }, /* MCP67 */
628 { PCI_VDEVICE(NVIDIA, 0x0553), board_ahci_yesncq }, /* MCP67 */
629 { PCI_VDEVICE(NVIDIA, 0x0554), board_ahci_yesncq }, /* MCP67 */
630 { PCI_VDEVICE(NVIDIA, 0x0555), board_ahci_yesncq }, /* MCP67 */
631 { PCI_VDEVICE(NVIDIA, 0x0556), board_ahci_yesncq }, /* MCP67 */
632 { PCI_VDEVICE(NVIDIA, 0x0557), board_ahci_yesncq }, /* MCP67 */
633 { PCI_VDEVICE(NVIDIA, 0x0558), board_ahci_yesncq }, /* MCP67 */
634 { PCI_VDEVICE(NVIDIA, 0x0559), board_ahci_yesncq }, /* MCP67 */
635 { PCI_VDEVICE(NVIDIA, 0x055a), board_ahci_yesncq }, /* MCP67 */
636 { PCI_VDEVICE(NVIDIA, 0x055b), board_ahci_yesncq }, /* MCP67 */
peer chen726206f2009-10-15 16:34:56 +0800637 { PCI_VDEVICE(NVIDIA, 0x0580), board_ahci_yesncq }, /* Linux ID */
Tejun Heoaa431dd2009-04-08 14:25:31 -0700638 { PCI_VDEVICE(NVIDIA, 0x07f0), board_ahci_yesncq }, /* MCP73 */
639 { PCI_VDEVICE(NVIDIA, 0x07f1), board_ahci_yesncq }, /* MCP73 */
640 { PCI_VDEVICE(NVIDIA, 0x07f2), board_ahci_yesncq }, /* MCP73 */
641 { PCI_VDEVICE(NVIDIA, 0x07f3), board_ahci_yesncq }, /* MCP73 */
642 { PCI_VDEVICE(NVIDIA, 0x07f4), board_ahci_yesncq }, /* MCP73 */
643 { PCI_VDEVICE(NVIDIA, 0x07f5), board_ahci_yesncq }, /* MCP73 */
644 { PCI_VDEVICE(NVIDIA, 0x07f6), board_ahci_yesncq }, /* MCP73 */
645 { PCI_VDEVICE(NVIDIA, 0x07f7), board_ahci_yesncq }, /* MCP73 */
646 { PCI_VDEVICE(NVIDIA, 0x07f8), board_ahci_yesncq }, /* MCP73 */
647 { PCI_VDEVICE(NVIDIA, 0x07f9), board_ahci_yesncq }, /* MCP73 */
648 { PCI_VDEVICE(NVIDIA, 0x07fa), board_ahci_yesncq }, /* MCP73 */
649 { PCI_VDEVICE(NVIDIA, 0x07fb), board_ahci_yesncq }, /* MCP73 */
Peer Chen0522b282007-06-07 18:05:12 +0800650 { PCI_VDEVICE(NVIDIA, 0x0ad0), board_ahci }, /* MCP77 */
651 { PCI_VDEVICE(NVIDIA, 0x0ad1), board_ahci }, /* MCP77 */
652 { PCI_VDEVICE(NVIDIA, 0x0ad2), board_ahci }, /* MCP77 */
653 { PCI_VDEVICE(NVIDIA, 0x0ad3), board_ahci }, /* MCP77 */
654 { PCI_VDEVICE(NVIDIA, 0x0ad4), board_ahci }, /* MCP77 */
655 { PCI_VDEVICE(NVIDIA, 0x0ad5), board_ahci }, /* MCP77 */
656 { PCI_VDEVICE(NVIDIA, 0x0ad6), board_ahci }, /* MCP77 */
657 { PCI_VDEVICE(NVIDIA, 0x0ad7), board_ahci }, /* MCP77 */
658 { PCI_VDEVICE(NVIDIA, 0x0ad8), board_ahci }, /* MCP77 */
659 { PCI_VDEVICE(NVIDIA, 0x0ad9), board_ahci }, /* MCP77 */
660 { PCI_VDEVICE(NVIDIA, 0x0ada), board_ahci }, /* MCP77 */
661 { PCI_VDEVICE(NVIDIA, 0x0adb), board_ahci }, /* MCP77 */
peerchen6ba86952007-12-03 22:20:37 +0800662 { PCI_VDEVICE(NVIDIA, 0x0ab4), board_ahci }, /* MCP79 */
663 { PCI_VDEVICE(NVIDIA, 0x0ab5), board_ahci }, /* MCP79 */
664 { PCI_VDEVICE(NVIDIA, 0x0ab6), board_ahci }, /* MCP79 */
665 { PCI_VDEVICE(NVIDIA, 0x0ab7), board_ahci }, /* MCP79 */
Peer Chen71008192007-09-24 10:16:25 +0800666 { PCI_VDEVICE(NVIDIA, 0x0ab8), board_ahci }, /* MCP79 */
667 { PCI_VDEVICE(NVIDIA, 0x0ab9), board_ahci }, /* MCP79 */
668 { PCI_VDEVICE(NVIDIA, 0x0aba), board_ahci }, /* MCP79 */
669 { PCI_VDEVICE(NVIDIA, 0x0abb), board_ahci }, /* MCP79 */
670 { PCI_VDEVICE(NVIDIA, 0x0abc), board_ahci }, /* MCP79 */
671 { PCI_VDEVICE(NVIDIA, 0x0abd), board_ahci }, /* MCP79 */
672 { PCI_VDEVICE(NVIDIA, 0x0abe), board_ahci }, /* MCP79 */
673 { PCI_VDEVICE(NVIDIA, 0x0abf), board_ahci }, /* MCP79 */
peerchen7adbe462009-02-27 16:58:41 +0800674 { PCI_VDEVICE(NVIDIA, 0x0d84), board_ahci }, /* MCP89 */
675 { PCI_VDEVICE(NVIDIA, 0x0d85), board_ahci }, /* MCP89 */
676 { PCI_VDEVICE(NVIDIA, 0x0d86), board_ahci }, /* MCP89 */
677 { PCI_VDEVICE(NVIDIA, 0x0d87), board_ahci }, /* MCP89 */
678 { PCI_VDEVICE(NVIDIA, 0x0d88), board_ahci }, /* MCP89 */
679 { PCI_VDEVICE(NVIDIA, 0x0d89), board_ahci }, /* MCP89 */
680 { PCI_VDEVICE(NVIDIA, 0x0d8a), board_ahci }, /* MCP89 */
681 { PCI_VDEVICE(NVIDIA, 0x0d8b), board_ahci }, /* MCP89 */
682 { PCI_VDEVICE(NVIDIA, 0x0d8c), board_ahci }, /* MCP89 */
683 { PCI_VDEVICE(NVIDIA, 0x0d8d), board_ahci }, /* MCP89 */
684 { PCI_VDEVICE(NVIDIA, 0x0d8e), board_ahci }, /* MCP89 */
685 { PCI_VDEVICE(NVIDIA, 0x0d8f), board_ahci }, /* MCP89 */
Jeff Garzikfe7fa312006-06-22 23:05:36 -0400686
Jeff Garzik95916ed2006-07-29 04:10:14 -0400687 /* SiS */
Tejun Heo20e2de42008-08-01 12:51:43 +0900688 { PCI_VDEVICE(SI, 0x1184), board_ahci }, /* SiS 966 */
689 { PCI_VDEVICE(SI, 0x1185), board_ahci }, /* SiS 968 */
690 { PCI_VDEVICE(SI, 0x0186), board_ahci }, /* SiS 968 */
Jeff Garzik95916ed2006-07-29 04:10:14 -0400691
Jeff Garzikcd70c262007-07-08 02:29:42 -0400692 /* Marvell */
693 { PCI_VDEVICE(MARVELL, 0x6145), board_ahci_mv }, /* 6145 */
Jose Alberto Regueroc40e7cb2008-03-13 23:22:24 +0100694 { PCI_VDEVICE(MARVELL, 0x6121), board_ahci_mv }, /* 6121 */
Jeff Garzikcd70c262007-07-08 02:29:42 -0400695
Mark Nelsonc77a0362008-10-23 14:08:16 +1100696 /* Promise */
697 { PCI_VDEVICE(PROMISE, 0x3f20), board_ahci }, /* PDC42819 */
698
Jeff Garzik415ae2b2006-11-01 05:10:42 -0500699 /* Generic, PCI class code for AHCI */
700 { PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID, PCI_ANY_ID,
Conke Huc9f89472007-01-09 05:32:51 -0500701 PCI_CLASS_STORAGE_SATA_AHCI, 0xffffff, board_ahci },
Jeff Garzik415ae2b2006-11-01 05:10:42 -0500702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 { } /* terminate list */
704};
705
706
707static struct pci_driver ahci_pci_driver = {
708 .name = DRV_NAME,
709 .id_table = ahci_pci_tbl,
710 .probe = ahci_init_one,
Tejun Heo24dc5f32007-01-20 16:00:28 +0900711 .remove = ata_pci_remove_one,
Tejun Heo438ac6d2007-03-02 17:31:26 +0900712#ifdef CONFIG_PM
Tejun Heoc1332872006-07-26 15:59:26 +0900713 .suspend = ahci_pci_device_suspend,
714 .resume = ahci_pci_device_resume,
Tejun Heo438ac6d2007-03-02 17:31:26 +0900715#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716};
717
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -0700718static int ahci_em_messages = 1;
719module_param(ahci_em_messages, int, 0444);
720/* add other LED protocol types when they become supported */
721MODULE_PARM_DESC(ahci_em_messages,
722 "Set AHCI Enclosure Management Message type (0 = disabled, 1 = LED");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Alan Cox5b66c822008-09-03 14:48:34 +0100724#if defined(CONFIG_PATA_MARVELL) || defined(CONFIG_PATA_MARVELL_MODULE)
725static int marvell_enable;
726#else
727static int marvell_enable = 1;
728#endif
729module_param(marvell_enable, int, 0644);
730MODULE_PARM_DESC(marvell_enable, "Marvell SATA via AHCI (1 = enabled)");
731
732
Tejun Heo98fa4b62006-11-02 12:17:23 +0900733static inline int ahci_nr_ports(u32 cap)
734{
735 return (cap & 0x1f) + 1;
736}
737
Jeff Garzikdab632e2007-05-28 08:33:01 -0400738static inline void __iomem *__ahci_port_base(struct ata_host *host,
739 unsigned int port_no)
740{
741 void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
742
743 return mmio + 0x100 + (port_no * 0x80);
744}
745
Tejun Heo4447d352007-04-17 23:44:08 +0900746static inline void __iomem *ahci_port_base(struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747{
Jeff Garzikdab632e2007-05-28 08:33:01 -0400748 return __ahci_port_base(ap->host, ap->port_no);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749}
750
Tejun Heob710a1f2008-01-05 23:11:57 +0900751static void ahci_enable_ahci(void __iomem *mmio)
752{
Tejun Heo15fe9822008-04-23 20:52:58 +0900753 int i;
Tejun Heob710a1f2008-01-05 23:11:57 +0900754 u32 tmp;
755
756 /* turn on AHCI_EN */
757 tmp = readl(mmio + HOST_CTL);
Tejun Heo15fe9822008-04-23 20:52:58 +0900758 if (tmp & HOST_AHCI_EN)
759 return;
760
761 /* Some controllers need AHCI_EN to be written multiple times.
762 * Try a few times before giving up.
763 */
764 for (i = 0; i < 5; i++) {
Tejun Heob710a1f2008-01-05 23:11:57 +0900765 tmp |= HOST_AHCI_EN;
766 writel(tmp, mmio + HOST_CTL);
767 tmp = readl(mmio + HOST_CTL); /* flush && sanity check */
Tejun Heo15fe9822008-04-23 20:52:58 +0900768 if (tmp & HOST_AHCI_EN)
769 return;
770 msleep(10);
Tejun Heob710a1f2008-01-05 23:11:57 +0900771 }
Tejun Heo15fe9822008-04-23 20:52:58 +0900772
773 WARN_ON(1);
Tejun Heob710a1f2008-01-05 23:11:57 +0900774}
775
Matthew Garrett77cdec12009-07-17 19:13:47 +0100776static ssize_t ahci_show_host_caps(struct device *dev,
777 struct device_attribute *attr, char *buf)
778{
779 struct Scsi_Host *shost = class_to_shost(dev);
780 struct ata_port *ap = ata_shost_to_port(shost);
781 struct ahci_host_priv *hpriv = ap->host->private_data;
782
783 return sprintf(buf, "%x\n", hpriv->cap);
784}
785
Robert Hancock4c521c82009-09-20 17:02:31 -0600786static ssize_t ahci_show_host_cap2(struct device *dev,
787 struct device_attribute *attr, char *buf)
788{
789 struct Scsi_Host *shost = class_to_shost(dev);
790 struct ata_port *ap = ata_shost_to_port(shost);
791 struct ahci_host_priv *hpriv = ap->host->private_data;
792
793 return sprintf(buf, "%x\n", hpriv->cap2);
794}
795
Matthew Garrett77cdec12009-07-17 19:13:47 +0100796static ssize_t ahci_show_host_version(struct device *dev,
797 struct device_attribute *attr, char *buf)
798{
799 struct Scsi_Host *shost = class_to_shost(dev);
800 struct ata_port *ap = ata_shost_to_port(shost);
801 void __iomem *mmio = ap->host->iomap[AHCI_PCI_BAR];
802
803 return sprintf(buf, "%x\n", readl(mmio + HOST_VERSION));
804}
805
806static ssize_t ahci_show_port_cmd(struct device *dev,
807 struct device_attribute *attr, char *buf)
808{
809 struct Scsi_Host *shost = class_to_shost(dev);
810 struct ata_port *ap = ata_shost_to_port(shost);
811 void __iomem *port_mmio = ahci_port_base(ap);
812
813 return sprintf(buf, "%x\n", readl(port_mmio + PORT_CMD));
814}
815
Tejun Heod447df12007-03-18 22:15:33 +0900816/**
817 * ahci_save_initial_config - Save and fixup initial config values
Tejun Heo4447d352007-04-17 23:44:08 +0900818 * @pdev: target PCI device
Tejun Heo4447d352007-04-17 23:44:08 +0900819 * @hpriv: host private area to store config values
Tejun Heod447df12007-03-18 22:15:33 +0900820 *
821 * Some registers containing configuration info might be setup by
822 * BIOS and might be cleared on reset. This function saves the
823 * initial values of those registers into @hpriv such that they
824 * can be restored after controller reset.
825 *
826 * If inconsistent, config values are fixed up by this function.
827 *
828 * LOCKING:
829 * None.
830 */
Tejun Heo4447d352007-04-17 23:44:08 +0900831static void ahci_save_initial_config(struct pci_dev *pdev,
Tejun Heo4447d352007-04-17 23:44:08 +0900832 struct ahci_host_priv *hpriv)
Tejun Heod447df12007-03-18 22:15:33 +0900833{
Tejun Heo4447d352007-04-17 23:44:08 +0900834 void __iomem *mmio = pcim_iomap_table(pdev)[AHCI_PCI_BAR];
Robert Hancock4c521c82009-09-20 17:02:31 -0600835 u32 cap, cap2, vers, port_map;
Tejun Heo17199b12007-03-18 22:26:53 +0900836 int i;
Jose Alberto Regueroc40e7cb2008-03-13 23:22:24 +0100837 int mv;
Tejun Heod447df12007-03-18 22:15:33 +0900838
Tejun Heob710a1f2008-01-05 23:11:57 +0900839 /* make sure AHCI mode is enabled before accessing CAP */
840 ahci_enable_ahci(mmio);
841
Tejun Heod447df12007-03-18 22:15:33 +0900842 /* Values prefixed with saved_ are written back to host after
843 * reset. Values without are used for driver operation.
844 */
845 hpriv->saved_cap = cap = readl(mmio + HOST_CAP);
846 hpriv->saved_port_map = port_map = readl(mmio + HOST_PORTS_IMPL);
847
Robert Hancock4c521c82009-09-20 17:02:31 -0600848 /* CAP2 register is only defined for AHCI 1.2 and later */
849 vers = readl(mmio + HOST_VERSION);
850 if ((vers >> 16) > 1 ||
851 ((vers >> 16) == 1 && (vers & 0xFFFF) >= 0x200))
852 hpriv->saved_cap2 = cap2 = readl(mmio + HOST_CAP2);
853 else
854 hpriv->saved_cap2 = cap2 = 0;
855
Tejun Heo274c1fd2007-07-16 14:29:40 +0900856 /* some chips have errata preventing 64bit use */
Tejun Heo417a1a62007-09-23 13:19:55 +0900857 if ((cap & HOST_CAP_64) && (hpriv->flags & AHCI_HFLAG_32BIT_ONLY)) {
Tejun Heoc7a42152007-05-18 16:23:19 +0200858 dev_printk(KERN_INFO, &pdev->dev,
859 "controller can't do 64bit DMA, forcing 32bit\n");
860 cap &= ~HOST_CAP_64;
861 }
862
Tejun Heo417a1a62007-09-23 13:19:55 +0900863 if ((cap & HOST_CAP_NCQ) && (hpriv->flags & AHCI_HFLAG_NO_NCQ)) {
Tejun Heo274c1fd2007-07-16 14:29:40 +0900864 dev_printk(KERN_INFO, &pdev->dev,
865 "controller can't do NCQ, turning off CAP_NCQ\n");
866 cap &= ~HOST_CAP_NCQ;
867 }
868
Tejun Heoe297d992008-06-10 00:13:04 +0900869 if (!(cap & HOST_CAP_NCQ) && (hpriv->flags & AHCI_HFLAG_YES_NCQ)) {
870 dev_printk(KERN_INFO, &pdev->dev,
871 "controller can do NCQ, turning on CAP_NCQ\n");
872 cap |= HOST_CAP_NCQ;
873 }
874
Roel Kluin258cd842008-03-09 21:42:40 +0100875 if ((cap & HOST_CAP_PMP) && (hpriv->flags & AHCI_HFLAG_NO_PMP)) {
Tejun Heo6949b912007-09-23 13:19:55 +0900876 dev_printk(KERN_INFO, &pdev->dev,
877 "controller can't do PMP, turning off CAP_PMP\n");
878 cap &= ~HOST_CAP_PMP;
879 }
880
Shaohua Li1b677af2009-11-16 09:56:05 +0800881 if ((cap & HOST_CAP_SNTF) && (hpriv->flags & AHCI_HFLAG_NO_SNTF)) {
882 dev_printk(KERN_INFO, &pdev->dev,
883 "controller can't do SNTF, turning off CAP_SNTF\n");
884 cap &= ~HOST_CAP_SNTF;
885 }
886
Tejun Heod799e082008-06-17 12:46:30 +0900887 if (pdev->vendor == PCI_VENDOR_ID_JMICRON && pdev->device == 0x2361 &&
888 port_map != 1) {
889 dev_printk(KERN_INFO, &pdev->dev,
890 "JMB361 has only one port, port_map 0x%x -> 0x%x\n",
891 port_map, 1);
892 port_map = 1;
893 }
894
Jeff Garzikcd70c262007-07-08 02:29:42 -0400895 /*
896 * Temporary Marvell 6145 hack: PATA port presence
897 * is asserted through the standard AHCI port
898 * presence register, as bit 4 (counting from 0)
899 */
Tejun Heo417a1a62007-09-23 13:19:55 +0900900 if (hpriv->flags & AHCI_HFLAG_MV_PATA) {
Jose Alberto Regueroc40e7cb2008-03-13 23:22:24 +0100901 if (pdev->device == 0x6121)
902 mv = 0x3;
903 else
904 mv = 0xf;
Jeff Garzikcd70c262007-07-08 02:29:42 -0400905 dev_printk(KERN_ERR, &pdev->dev,
906 "MV_AHCI HACK: port_map %x -> %x\n",
Jose Alberto Regueroc40e7cb2008-03-13 23:22:24 +0100907 port_map,
908 port_map & mv);
Alan Cox5b66c822008-09-03 14:48:34 +0100909 dev_printk(KERN_ERR, &pdev->dev,
910 "Disabling your PATA port. Use the boot option 'ahci.marvell_enable=0' to avoid this.\n");
Jeff Garzikcd70c262007-07-08 02:29:42 -0400911
Jose Alberto Regueroc40e7cb2008-03-13 23:22:24 +0100912 port_map &= mv;
Jeff Garzikcd70c262007-07-08 02:29:42 -0400913 }
914
Tejun Heo17199b12007-03-18 22:26:53 +0900915 /* cross check port_map and cap.n_ports */
Tejun Heo7a234af2007-09-03 12:44:57 +0900916 if (port_map) {
Tejun Heo837f5f82008-02-06 15:13:51 +0900917 int map_ports = 0;
Tejun Heo17199b12007-03-18 22:26:53 +0900918
Tejun Heo837f5f82008-02-06 15:13:51 +0900919 for (i = 0; i < AHCI_MAX_PORTS; i++)
920 if (port_map & (1 << i))
921 map_ports++;
Tejun Heo17199b12007-03-18 22:26:53 +0900922
Tejun Heo837f5f82008-02-06 15:13:51 +0900923 /* If PI has more ports than n_ports, whine, clear
924 * port_map and let it be generated from n_ports.
Tejun Heo17199b12007-03-18 22:26:53 +0900925 */
Tejun Heo837f5f82008-02-06 15:13:51 +0900926 if (map_ports > ahci_nr_ports(cap)) {
Tejun Heo4447d352007-04-17 23:44:08 +0900927 dev_printk(KERN_WARNING, &pdev->dev,
Tejun Heo837f5f82008-02-06 15:13:51 +0900928 "implemented port map (0x%x) contains more "
929 "ports than nr_ports (%u), using nr_ports\n",
930 port_map, ahci_nr_ports(cap));
Tejun Heo7a234af2007-09-03 12:44:57 +0900931 port_map = 0;
932 }
933 }
934
935 /* fabricate port_map from cap.nr_ports */
936 if (!port_map) {
Tejun Heo17199b12007-03-18 22:26:53 +0900937 port_map = (1 << ahci_nr_ports(cap)) - 1;
Tejun Heo7a234af2007-09-03 12:44:57 +0900938 dev_printk(KERN_WARNING, &pdev->dev,
939 "forcing PORTS_IMPL to 0x%x\n", port_map);
940
941 /* write the fixed up value to the PI register */
942 hpriv->saved_port_map = port_map;
Tejun Heo17199b12007-03-18 22:26:53 +0900943 }
944
Tejun Heod447df12007-03-18 22:15:33 +0900945 /* record values to use during operation */
946 hpriv->cap = cap;
Robert Hancock4c521c82009-09-20 17:02:31 -0600947 hpriv->cap2 = cap2;
Tejun Heod447df12007-03-18 22:15:33 +0900948 hpriv->port_map = port_map;
949}
950
951/**
952 * ahci_restore_initial_config - Restore initial config
Tejun Heo4447d352007-04-17 23:44:08 +0900953 * @host: target ATA host
Tejun Heod447df12007-03-18 22:15:33 +0900954 *
955 * Restore initial config stored by ahci_save_initial_config().
956 *
957 * LOCKING:
958 * None.
959 */
Tejun Heo4447d352007-04-17 23:44:08 +0900960static void ahci_restore_initial_config(struct ata_host *host)
Tejun Heod447df12007-03-18 22:15:33 +0900961{
Tejun Heo4447d352007-04-17 23:44:08 +0900962 struct ahci_host_priv *hpriv = host->private_data;
963 void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
964
Tejun Heod447df12007-03-18 22:15:33 +0900965 writel(hpriv->saved_cap, mmio + HOST_CAP);
Robert Hancock4c521c82009-09-20 17:02:31 -0600966 if (hpriv->saved_cap2)
967 writel(hpriv->saved_cap2, mmio + HOST_CAP2);
Tejun Heod447df12007-03-18 22:15:33 +0900968 writel(hpriv->saved_port_map, mmio + HOST_PORTS_IMPL);
969 (void) readl(mmio + HOST_PORTS_IMPL); /* flush */
970}
971
Tejun Heo203ef6c2007-07-16 14:29:40 +0900972static unsigned ahci_scr_offset(struct ata_port *ap, unsigned int sc_reg)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700973{
Tejun Heo203ef6c2007-07-16 14:29:40 +0900974 static const int offset[] = {
975 [SCR_STATUS] = PORT_SCR_STAT,
976 [SCR_CONTROL] = PORT_SCR_CTL,
977 [SCR_ERROR] = PORT_SCR_ERR,
978 [SCR_ACTIVE] = PORT_SCR_ACT,
979 [SCR_NOTIFICATION] = PORT_SCR_NTF,
980 };
981 struct ahci_host_priv *hpriv = ap->host->private_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982
Tejun Heo203ef6c2007-07-16 14:29:40 +0900983 if (sc_reg < ARRAY_SIZE(offset) &&
984 (sc_reg != SCR_NOTIFICATION || (hpriv->cap & HOST_CAP_SNTF)))
985 return offset[sc_reg];
Tejun Heoda3dbb12007-07-16 14:29:40 +0900986 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987}
988
Tejun Heo82ef04f2008-07-31 17:02:40 +0900989static int ahci_scr_read(struct ata_link *link, unsigned int sc_reg, u32 *val)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990{
Tejun Heo82ef04f2008-07-31 17:02:40 +0900991 void __iomem *port_mmio = ahci_port_base(link->ap);
992 int offset = ahci_scr_offset(link->ap, sc_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993
Tejun Heo203ef6c2007-07-16 14:29:40 +0900994 if (offset) {
995 *val = readl(port_mmio + offset);
996 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 }
Tejun Heo203ef6c2007-07-16 14:29:40 +0900998 return -EINVAL;
999}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000
Tejun Heo82ef04f2008-07-31 17:02:40 +09001001static int ahci_scr_write(struct ata_link *link, unsigned int sc_reg, u32 val)
Tejun Heo203ef6c2007-07-16 14:29:40 +09001002{
Tejun Heo82ef04f2008-07-31 17:02:40 +09001003 void __iomem *port_mmio = ahci_port_base(link->ap);
1004 int offset = ahci_scr_offset(link->ap, sc_reg);
Tejun Heo203ef6c2007-07-16 14:29:40 +09001005
1006 if (offset) {
1007 writel(val, port_mmio + offset);
1008 return 0;
1009 }
1010 return -EINVAL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011}
1012
Tejun Heo4447d352007-04-17 23:44:08 +09001013static void ahci_start_engine(struct ata_port *ap)
Tejun Heo7c76d1e2005-12-19 22:36:34 +09001014{
Tejun Heo4447d352007-04-17 23:44:08 +09001015 void __iomem *port_mmio = ahci_port_base(ap);
Tejun Heo7c76d1e2005-12-19 22:36:34 +09001016 u32 tmp;
1017
Tejun Heod8fcd112006-07-26 15:59:25 +09001018 /* start DMA */
Tejun Heo9f592052006-07-26 15:59:26 +09001019 tmp = readl(port_mmio + PORT_CMD);
Tejun Heo7c76d1e2005-12-19 22:36:34 +09001020 tmp |= PORT_CMD_START;
1021 writel(tmp, port_mmio + PORT_CMD);
1022 readl(port_mmio + PORT_CMD); /* flush */
1023}
1024
Tejun Heo4447d352007-04-17 23:44:08 +09001025static int ahci_stop_engine(struct ata_port *ap)
Tejun Heo254950c2006-07-26 15:59:25 +09001026{
Tejun Heo4447d352007-04-17 23:44:08 +09001027 void __iomem *port_mmio = ahci_port_base(ap);
Tejun Heo254950c2006-07-26 15:59:25 +09001028 u32 tmp;
1029
1030 tmp = readl(port_mmio + PORT_CMD);
1031
Tejun Heod8fcd112006-07-26 15:59:25 +09001032 /* check if the HBA is idle */
Tejun Heo254950c2006-07-26 15:59:25 +09001033 if ((tmp & (PORT_CMD_START | PORT_CMD_LIST_ON)) == 0)
1034 return 0;
1035
Tejun Heod8fcd112006-07-26 15:59:25 +09001036 /* setting HBA to idle */
Tejun Heo254950c2006-07-26 15:59:25 +09001037 tmp &= ~PORT_CMD_START;
1038 writel(tmp, port_mmio + PORT_CMD);
1039
Tejun Heod8fcd112006-07-26 15:59:25 +09001040 /* wait for engine to stop. This could be as long as 500 msec */
Tejun Heo254950c2006-07-26 15:59:25 +09001041 tmp = ata_wait_register(port_mmio + PORT_CMD,
Jeff Garzik2dcb4072007-10-19 06:42:56 -04001042 PORT_CMD_LIST_ON, PORT_CMD_LIST_ON, 1, 500);
Tejun Heod8fcd112006-07-26 15:59:25 +09001043 if (tmp & PORT_CMD_LIST_ON)
Tejun Heo254950c2006-07-26 15:59:25 +09001044 return -EIO;
1045
1046 return 0;
1047}
1048
Tejun Heo4447d352007-04-17 23:44:08 +09001049static void ahci_start_fis_rx(struct ata_port *ap)
Tejun Heo0be0aa92006-07-26 15:59:26 +09001050{
Tejun Heo4447d352007-04-17 23:44:08 +09001051 void __iomem *port_mmio = ahci_port_base(ap);
1052 struct ahci_host_priv *hpriv = ap->host->private_data;
1053 struct ahci_port_priv *pp = ap->private_data;
Tejun Heo0be0aa92006-07-26 15:59:26 +09001054 u32 tmp;
1055
1056 /* set FIS registers */
Tejun Heo4447d352007-04-17 23:44:08 +09001057 if (hpriv->cap & HOST_CAP_64)
1058 writel((pp->cmd_slot_dma >> 16) >> 16,
1059 port_mmio + PORT_LST_ADDR_HI);
1060 writel(pp->cmd_slot_dma & 0xffffffff, port_mmio + PORT_LST_ADDR);
Tejun Heo0be0aa92006-07-26 15:59:26 +09001061
Tejun Heo4447d352007-04-17 23:44:08 +09001062 if (hpriv->cap & HOST_CAP_64)
1063 writel((pp->rx_fis_dma >> 16) >> 16,
1064 port_mmio + PORT_FIS_ADDR_HI);
1065 writel(pp->rx_fis_dma & 0xffffffff, port_mmio + PORT_FIS_ADDR);
Tejun Heo0be0aa92006-07-26 15:59:26 +09001066
1067 /* enable FIS reception */
1068 tmp = readl(port_mmio + PORT_CMD);
1069 tmp |= PORT_CMD_FIS_RX;
1070 writel(tmp, port_mmio + PORT_CMD);
1071
1072 /* flush */
1073 readl(port_mmio + PORT_CMD);
1074}
1075
Tejun Heo4447d352007-04-17 23:44:08 +09001076static int ahci_stop_fis_rx(struct ata_port *ap)
Tejun Heo0be0aa92006-07-26 15:59:26 +09001077{
Tejun Heo4447d352007-04-17 23:44:08 +09001078 void __iomem *port_mmio = ahci_port_base(ap);
Tejun Heo0be0aa92006-07-26 15:59:26 +09001079 u32 tmp;
1080
1081 /* disable FIS reception */
1082 tmp = readl(port_mmio + PORT_CMD);
1083 tmp &= ~PORT_CMD_FIS_RX;
1084 writel(tmp, port_mmio + PORT_CMD);
1085
1086 /* wait for completion, spec says 500ms, give it 1000 */
1087 tmp = ata_wait_register(port_mmio + PORT_CMD, PORT_CMD_FIS_ON,
1088 PORT_CMD_FIS_ON, 10, 1000);
1089 if (tmp & PORT_CMD_FIS_ON)
1090 return -EBUSY;
1091
1092 return 0;
1093}
1094
Tejun Heo4447d352007-04-17 23:44:08 +09001095static void ahci_power_up(struct ata_port *ap)
Tejun Heo0be0aa92006-07-26 15:59:26 +09001096{
Tejun Heo4447d352007-04-17 23:44:08 +09001097 struct ahci_host_priv *hpriv = ap->host->private_data;
1098 void __iomem *port_mmio = ahci_port_base(ap);
Tejun Heo0be0aa92006-07-26 15:59:26 +09001099 u32 cmd;
1100
1101 cmd = readl(port_mmio + PORT_CMD) & ~PORT_CMD_ICC_MASK;
1102
1103 /* spin up device */
Tejun Heo4447d352007-04-17 23:44:08 +09001104 if (hpriv->cap & HOST_CAP_SSS) {
Tejun Heo0be0aa92006-07-26 15:59:26 +09001105 cmd |= PORT_CMD_SPIN_UP;
1106 writel(cmd, port_mmio + PORT_CMD);
1107 }
1108
1109 /* wake up link */
1110 writel(cmd | PORT_CMD_ICC_ACTIVE, port_mmio + PORT_CMD);
1111}
1112
Kristen Carlson Accardi31556592007-10-25 01:33:26 -04001113static void ahci_disable_alpm(struct ata_port *ap)
1114{
1115 struct ahci_host_priv *hpriv = ap->host->private_data;
1116 void __iomem *port_mmio = ahci_port_base(ap);
1117 u32 cmd;
1118 struct ahci_port_priv *pp = ap->private_data;
1119
1120 /* IPM bits should be disabled by libata-core */
1121 /* get the existing command bits */
1122 cmd = readl(port_mmio + PORT_CMD);
1123
1124 /* disable ALPM and ASP */
1125 cmd &= ~PORT_CMD_ASP;
1126 cmd &= ~PORT_CMD_ALPE;
1127
1128 /* force the interface back to active */
1129 cmd |= PORT_CMD_ICC_ACTIVE;
1130
1131 /* write out new cmd value */
1132 writel(cmd, port_mmio + PORT_CMD);
1133 cmd = readl(port_mmio + PORT_CMD);
1134
1135 /* wait 10ms to be sure we've come out of any low power state */
1136 msleep(10);
1137
1138 /* clear out any PhyRdy stuff from interrupt status */
1139 writel(PORT_IRQ_PHYRDY, port_mmio + PORT_IRQ_STAT);
1140
1141 /* go ahead and clean out PhyRdy Change from Serror too */
Tejun Heo82ef04f2008-07-31 17:02:40 +09001142 ahci_scr_write(&ap->link, SCR_ERROR, ((1 << 16) | (1 << 18)));
Kristen Carlson Accardi31556592007-10-25 01:33:26 -04001143
1144 /*
1145 * Clear flag to indicate that we should ignore all PhyRdy
1146 * state changes
1147 */
1148 hpriv->flags &= ~AHCI_HFLAG_NO_HOTPLUG;
1149
1150 /*
1151 * Enable interrupts on Phy Ready.
1152 */
1153 pp->intr_mask |= PORT_IRQ_PHYRDY;
1154 writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
1155
1156 /*
1157 * don't change the link pm policy - we can be called
1158 * just to turn of link pm temporarily
1159 */
1160}
1161
1162static int ahci_enable_alpm(struct ata_port *ap,
1163 enum link_pm policy)
1164{
1165 struct ahci_host_priv *hpriv = ap->host->private_data;
1166 void __iomem *port_mmio = ahci_port_base(ap);
1167 u32 cmd;
1168 struct ahci_port_priv *pp = ap->private_data;
1169 u32 asp;
1170
1171 /* Make sure the host is capable of link power management */
1172 if (!(hpriv->cap & HOST_CAP_ALPM))
1173 return -EINVAL;
1174
1175 switch (policy) {
1176 case MAX_PERFORMANCE:
1177 case NOT_AVAILABLE:
1178 /*
1179 * if we came here with NOT_AVAILABLE,
1180 * it just means this is the first time we
1181 * have tried to enable - default to max performance,
1182 * and let the user go to lower power modes on request.
1183 */
1184 ahci_disable_alpm(ap);
1185 return 0;
1186 case MIN_POWER:
1187 /* configure HBA to enter SLUMBER */
1188 asp = PORT_CMD_ASP;
1189 break;
1190 case MEDIUM_POWER:
1191 /* configure HBA to enter PARTIAL */
1192 asp = 0;
1193 break;
1194 default:
1195 return -EINVAL;
1196 }
1197
1198 /*
1199 * Disable interrupts on Phy Ready. This keeps us from
1200 * getting woken up due to spurious phy ready interrupts
1201 * TBD - Hot plug should be done via polling now, is
1202 * that even supported?
1203 */
1204 pp->intr_mask &= ~PORT_IRQ_PHYRDY;
1205 writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
1206
1207 /*
1208 * Set a flag to indicate that we should ignore all PhyRdy
1209 * state changes since these can happen now whenever we
1210 * change link state
1211 */
1212 hpriv->flags |= AHCI_HFLAG_NO_HOTPLUG;
1213
1214 /* get the existing command bits */
1215 cmd = readl(port_mmio + PORT_CMD);
1216
1217 /*
1218 * Set ASP based on Policy
1219 */
1220 cmd |= asp;
1221
1222 /*
1223 * Setting this bit will instruct the HBA to aggressively
1224 * enter a lower power link state when it's appropriate and
1225 * based on the value set above for ASP
1226 */
1227 cmd |= PORT_CMD_ALPE;
1228
1229 /* write out new cmd value */
1230 writel(cmd, port_mmio + PORT_CMD);
1231 cmd = readl(port_mmio + PORT_CMD);
1232
1233 /* IPM bits should be set by libata-core */
1234 return 0;
1235}
1236
Tejun Heo438ac6d2007-03-02 17:31:26 +09001237#ifdef CONFIG_PM
Tejun Heo4447d352007-04-17 23:44:08 +09001238static void ahci_power_down(struct ata_port *ap)
Tejun Heo0be0aa92006-07-26 15:59:26 +09001239{
Tejun Heo4447d352007-04-17 23:44:08 +09001240 struct ahci_host_priv *hpriv = ap->host->private_data;
1241 void __iomem *port_mmio = ahci_port_base(ap);
Tejun Heo0be0aa92006-07-26 15:59:26 +09001242 u32 cmd, scontrol;
1243
Tejun Heo4447d352007-04-17 23:44:08 +09001244 if (!(hpriv->cap & HOST_CAP_SSS))
Tejun Heo07c53da2007-01-21 02:10:11 +09001245 return;
1246
1247 /* put device into listen mode, first set PxSCTL.DET to 0 */
1248 scontrol = readl(port_mmio + PORT_SCR_CTL);
1249 scontrol &= ~0xf;
1250 writel(scontrol, port_mmio + PORT_SCR_CTL);
1251
1252 /* then set PxCMD.SUD to 0 */
Tejun Heo0be0aa92006-07-26 15:59:26 +09001253 cmd = readl(port_mmio + PORT_CMD) & ~PORT_CMD_ICC_MASK;
Tejun Heo07c53da2007-01-21 02:10:11 +09001254 cmd &= ~PORT_CMD_SPIN_UP;
1255 writel(cmd, port_mmio + PORT_CMD);
Tejun Heo0be0aa92006-07-26 15:59:26 +09001256}
Tejun Heo438ac6d2007-03-02 17:31:26 +09001257#endif
Tejun Heo0be0aa92006-07-26 15:59:26 +09001258
Jeff Garzikdf69c9c2007-05-26 20:46:51 -04001259static void ahci_start_port(struct ata_port *ap)
Tejun Heo0be0aa92006-07-26 15:59:26 +09001260{
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -07001261 struct ahci_port_priv *pp = ap->private_data;
1262 struct ata_link *link;
1263 struct ahci_em_priv *emp;
David Milburn4c1e9aa2009-04-03 15:36:41 -05001264 ssize_t rc;
1265 int i;
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -07001266
Tejun Heo0be0aa92006-07-26 15:59:26 +09001267 /* enable FIS reception */
Tejun Heo4447d352007-04-17 23:44:08 +09001268 ahci_start_fis_rx(ap);
Tejun Heo0be0aa92006-07-26 15:59:26 +09001269
1270 /* enable DMA */
Tejun Heo4447d352007-04-17 23:44:08 +09001271 ahci_start_engine(ap);
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -07001272
1273 /* turn on LEDs */
1274 if (ap->flags & ATA_FLAG_EM) {
Tejun Heo1eca4362008-11-03 20:03:17 +09001275 ata_for_each_link(link, ap, EDGE) {
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -07001276 emp = &pp->em_priv[link->pmp];
David Milburn4c1e9aa2009-04-03 15:36:41 -05001277
1278 /* EM Transmit bit maybe busy during init */
Tejun Heod50ce072009-05-12 10:57:41 +09001279 for (i = 0; i < EM_MAX_RETRY; i++) {
David Milburn4c1e9aa2009-04-03 15:36:41 -05001280 rc = ahci_transmit_led_message(ap,
1281 emp->led_state,
1282 4);
1283 if (rc == -EBUSY)
Tejun Heod50ce072009-05-12 10:57:41 +09001284 msleep(1);
David Milburn4c1e9aa2009-04-03 15:36:41 -05001285 else
1286 break;
1287 }
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -07001288 }
1289 }
1290
1291 if (ap->flags & ATA_FLAG_SW_ACTIVITY)
Tejun Heo1eca4362008-11-03 20:03:17 +09001292 ata_for_each_link(link, ap, EDGE)
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -07001293 ahci_init_sw_activity(link);
1294
Tejun Heo0be0aa92006-07-26 15:59:26 +09001295}
1296
Tejun Heo4447d352007-04-17 23:44:08 +09001297static int ahci_deinit_port(struct ata_port *ap, const char **emsg)
Tejun Heo0be0aa92006-07-26 15:59:26 +09001298{
1299 int rc;
1300
1301 /* disable DMA */
Tejun Heo4447d352007-04-17 23:44:08 +09001302 rc = ahci_stop_engine(ap);
Tejun Heo0be0aa92006-07-26 15:59:26 +09001303 if (rc) {
1304 *emsg = "failed to stop engine";
1305 return rc;
1306 }
1307
1308 /* disable FIS reception */
Tejun Heo4447d352007-04-17 23:44:08 +09001309 rc = ahci_stop_fis_rx(ap);
Tejun Heo0be0aa92006-07-26 15:59:26 +09001310 if (rc) {
1311 *emsg = "failed stop FIS RX";
1312 return rc;
1313 }
1314
Tejun Heo0be0aa92006-07-26 15:59:26 +09001315 return 0;
1316}
1317
Tejun Heo4447d352007-04-17 23:44:08 +09001318static int ahci_reset_controller(struct ata_host *host)
Tejun Heod91542c2006-07-26 15:59:26 +09001319{
Tejun Heo4447d352007-04-17 23:44:08 +09001320 struct pci_dev *pdev = to_pci_dev(host->dev);
Tejun Heo49f29092007-11-19 16:03:44 +09001321 struct ahci_host_priv *hpriv = host->private_data;
Tejun Heo4447d352007-04-17 23:44:08 +09001322 void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
Tejun Heod447df12007-03-18 22:15:33 +09001323 u32 tmp;
Tejun Heod91542c2006-07-26 15:59:26 +09001324
Jeff Garzik3cc3eb12007-09-26 00:02:41 -04001325 /* we must be in AHCI mode, before using anything
1326 * AHCI-specific, such as HOST_RESET.
1327 */
Tejun Heob710a1f2008-01-05 23:11:57 +09001328 ahci_enable_ahci(mmio);
Jeff Garzik3cc3eb12007-09-26 00:02:41 -04001329
1330 /* global controller reset */
Tejun Heoa22e6442008-03-10 10:25:25 +09001331 if (!ahci_skip_host_reset) {
1332 tmp = readl(mmio + HOST_CTL);
1333 if ((tmp & HOST_RESET) == 0) {
1334 writel(tmp | HOST_RESET, mmio + HOST_CTL);
1335 readl(mmio + HOST_CTL); /* flush */
1336 }
Tejun Heod91542c2006-07-26 15:59:26 +09001337
Zhang Rui24920c82008-07-04 13:32:17 +08001338 /*
1339 * to perform host reset, OS should set HOST_RESET
1340 * and poll until this bit is read to be "0".
1341 * reset must complete within 1 second, or
Tejun Heoa22e6442008-03-10 10:25:25 +09001342 * the hardware should be considered fried.
1343 */
Zhang Rui24920c82008-07-04 13:32:17 +08001344 tmp = ata_wait_register(mmio + HOST_CTL, HOST_RESET,
1345 HOST_RESET, 10, 1000);
Tejun Heod91542c2006-07-26 15:59:26 +09001346
Tejun Heoa22e6442008-03-10 10:25:25 +09001347 if (tmp & HOST_RESET) {
1348 dev_printk(KERN_ERR, host->dev,
1349 "controller reset failed (0x%x)\n", tmp);
1350 return -EIO;
1351 }
Tejun Heod91542c2006-07-26 15:59:26 +09001352
Tejun Heoa22e6442008-03-10 10:25:25 +09001353 /* turn on AHCI mode */
1354 ahci_enable_ahci(mmio);
Tejun Heo98fa4b62006-11-02 12:17:23 +09001355
Tejun Heoa22e6442008-03-10 10:25:25 +09001356 /* Some registers might be cleared on reset. Restore
1357 * initial values.
1358 */
1359 ahci_restore_initial_config(host);
1360 } else
1361 dev_printk(KERN_INFO, host->dev,
1362 "skipping global host reset\n");
Tejun Heod91542c2006-07-26 15:59:26 +09001363
1364 if (pdev->vendor == PCI_VENDOR_ID_INTEL) {
1365 u16 tmp16;
1366
1367 /* configure PCS */
1368 pci_read_config_word(pdev, 0x92, &tmp16);
Tejun Heo49f29092007-11-19 16:03:44 +09001369 if ((tmp16 & hpriv->port_map) != hpriv->port_map) {
1370 tmp16 |= hpriv->port_map;
1371 pci_write_config_word(pdev, 0x92, tmp16);
1372 }
Tejun Heod91542c2006-07-26 15:59:26 +09001373 }
1374
1375 return 0;
1376}
1377
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -07001378static void ahci_sw_activity(struct ata_link *link)
1379{
1380 struct ata_port *ap = link->ap;
1381 struct ahci_port_priv *pp = ap->private_data;
1382 struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
1383
1384 if (!(link->flags & ATA_LFLAG_SW_ACTIVITY))
1385 return;
1386
1387 emp->activity++;
1388 if (!timer_pending(&emp->timer))
1389 mod_timer(&emp->timer, jiffies + msecs_to_jiffies(10));
1390}
1391
1392static void ahci_sw_activity_blink(unsigned long arg)
1393{
1394 struct ata_link *link = (struct ata_link *)arg;
1395 struct ata_port *ap = link->ap;
1396 struct ahci_port_priv *pp = ap->private_data;
1397 struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
1398 unsigned long led_message = emp->led_state;
1399 u32 activity_led_state;
David Milburneb409632008-10-16 09:26:19 -05001400 unsigned long flags;
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -07001401
David Milburn87943ac2008-10-13 14:38:36 -05001402 led_message &= EM_MSG_LED_VALUE;
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -07001403 led_message |= ap->port_no | (link->pmp << 8);
1404
1405 /* check to see if we've had activity. If so,
1406 * toggle state of LED and reset timer. If not,
1407 * turn LED to desired idle state.
1408 */
David Milburneb409632008-10-16 09:26:19 -05001409 spin_lock_irqsave(ap->lock, flags);
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -07001410 if (emp->saved_activity != emp->activity) {
1411 emp->saved_activity = emp->activity;
1412 /* get the current LED state */
David Milburn87943ac2008-10-13 14:38:36 -05001413 activity_led_state = led_message & EM_MSG_LED_VALUE_ON;
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -07001414
1415 if (activity_led_state)
1416 activity_led_state = 0;
1417 else
1418 activity_led_state = 1;
1419
1420 /* clear old state */
David Milburn87943ac2008-10-13 14:38:36 -05001421 led_message &= ~EM_MSG_LED_VALUE_ACTIVITY;
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -07001422
1423 /* toggle state */
1424 led_message |= (activity_led_state << 16);
1425 mod_timer(&emp->timer, jiffies + msecs_to_jiffies(100));
1426 } else {
1427 /* switch to idle */
David Milburn87943ac2008-10-13 14:38:36 -05001428 led_message &= ~EM_MSG_LED_VALUE_ACTIVITY;
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -07001429 if (emp->blink_policy == BLINK_OFF)
1430 led_message |= (1 << 16);
1431 }
David Milburneb409632008-10-16 09:26:19 -05001432 spin_unlock_irqrestore(ap->lock, flags);
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -07001433 ahci_transmit_led_message(ap, led_message, 4);
1434}
1435
1436static void ahci_init_sw_activity(struct ata_link *link)
1437{
1438 struct ata_port *ap = link->ap;
1439 struct ahci_port_priv *pp = ap->private_data;
1440 struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
1441
1442 /* init activity stats, setup timer */
1443 emp->saved_activity = emp->activity = 0;
1444 setup_timer(&emp->timer, ahci_sw_activity_blink, (unsigned long)link);
1445
1446 /* check our blink policy and set flag for link if it's enabled */
1447 if (emp->blink_policy)
1448 link->flags |= ATA_LFLAG_SW_ACTIVITY;
1449}
1450
1451static int ahci_reset_em(struct ata_host *host)
1452{
1453 void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
1454 u32 em_ctl;
1455
1456 em_ctl = readl(mmio + HOST_EM_CTL);
1457 if ((em_ctl & EM_CTL_TM) || (em_ctl & EM_CTL_RST))
1458 return -EINVAL;
1459
1460 writel(em_ctl | EM_CTL_RST, mmio + HOST_EM_CTL);
1461 return 0;
1462}
1463
1464static ssize_t ahci_transmit_led_message(struct ata_port *ap, u32 state,
1465 ssize_t size)
1466{
1467 struct ahci_host_priv *hpriv = ap->host->private_data;
1468 struct ahci_port_priv *pp = ap->private_data;
1469 void __iomem *mmio = ap->host->iomap[AHCI_PCI_BAR];
1470 u32 em_ctl;
1471 u32 message[] = {0, 0};
Linus Torvalds93082f02008-07-25 10:56:36 -07001472 unsigned long flags;
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -07001473 int pmp;
1474 struct ahci_em_priv *emp;
1475
1476 /* get the slot number from the message */
David Milburn87943ac2008-10-13 14:38:36 -05001477 pmp = (state & EM_MSG_LED_PMP_SLOT) >> 8;
Tejun Heod50ce072009-05-12 10:57:41 +09001478 if (pmp < EM_MAX_SLOTS)
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -07001479 emp = &pp->em_priv[pmp];
1480 else
1481 return -EINVAL;
1482
1483 spin_lock_irqsave(ap->lock, flags);
1484
1485 /*
1486 * if we are still busy transmitting a previous message,
1487 * do not allow
1488 */
1489 em_ctl = readl(mmio + HOST_EM_CTL);
1490 if (em_ctl & EM_CTL_TM) {
1491 spin_unlock_irqrestore(ap->lock, flags);
David Milburn4c1e9aa2009-04-03 15:36:41 -05001492 return -EBUSY;
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -07001493 }
1494
1495 /*
1496 * create message header - this is all zero except for
1497 * the message size, which is 4 bytes.
1498 */
1499 message[0] |= (4 << 8);
1500
1501 /* ignore 0:4 of byte zero, fill in port info yourself */
David Milburn87943ac2008-10-13 14:38:36 -05001502 message[1] = ((state & ~EM_MSG_LED_HBA_PORT) | ap->port_no);
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -07001503
1504 /* write message to EM_LOC */
1505 writel(message[0], mmio + hpriv->em_loc);
1506 writel(message[1], mmio + hpriv->em_loc+4);
1507
1508 /* save off new led state for port/slot */
David Milburn208f2a82009-03-20 14:14:23 -05001509 emp->led_state = state;
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -07001510
1511 /*
1512 * tell hardware to transmit the message
1513 */
1514 writel(em_ctl | EM_CTL_TM, mmio + HOST_EM_CTL);
1515
1516 spin_unlock_irqrestore(ap->lock, flags);
1517 return size;
1518}
1519
1520static ssize_t ahci_led_show(struct ata_port *ap, char *buf)
1521{
1522 struct ahci_port_priv *pp = ap->private_data;
1523 struct ata_link *link;
1524 struct ahci_em_priv *emp;
1525 int rc = 0;
1526
Tejun Heo1eca4362008-11-03 20:03:17 +09001527 ata_for_each_link(link, ap, EDGE) {
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -07001528 emp = &pp->em_priv[link->pmp];
1529 rc += sprintf(buf, "%lx\n", emp->led_state);
1530 }
1531 return rc;
1532}
1533
1534static ssize_t ahci_led_store(struct ata_port *ap, const char *buf,
1535 size_t size)
1536{
1537 int state;
1538 int pmp;
1539 struct ahci_port_priv *pp = ap->private_data;
1540 struct ahci_em_priv *emp;
1541
1542 state = simple_strtoul(buf, NULL, 0);
1543
1544 /* get the slot number from the message */
David Milburn87943ac2008-10-13 14:38:36 -05001545 pmp = (state & EM_MSG_LED_PMP_SLOT) >> 8;
Tejun Heod50ce072009-05-12 10:57:41 +09001546 if (pmp < EM_MAX_SLOTS)
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -07001547 emp = &pp->em_priv[pmp];
1548 else
1549 return -EINVAL;
1550
1551 /* mask off the activity bits if we are in sw_activity
1552 * mode, user should turn off sw_activity before setting
1553 * activity led through em_message
1554 */
1555 if (emp->blink_policy)
David Milburn87943ac2008-10-13 14:38:36 -05001556 state &= ~EM_MSG_LED_VALUE_ACTIVITY;
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -07001557
1558 return ahci_transmit_led_message(ap, state, size);
1559}
1560
1561static ssize_t ahci_activity_store(struct ata_device *dev, enum sw_activity val)
1562{
1563 struct ata_link *link = dev->link;
1564 struct ata_port *ap = link->ap;
1565 struct ahci_port_priv *pp = ap->private_data;
1566 struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
1567 u32 port_led_state = emp->led_state;
1568
1569 /* save the desired Activity LED behavior */
1570 if (val == OFF) {
1571 /* clear LFLAG */
1572 link->flags &= ~(ATA_LFLAG_SW_ACTIVITY);
1573
1574 /* set the LED to OFF */
David Milburn87943ac2008-10-13 14:38:36 -05001575 port_led_state &= EM_MSG_LED_VALUE_OFF;
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -07001576 port_led_state |= (ap->port_no | (link->pmp << 8));
1577 ahci_transmit_led_message(ap, port_led_state, 4);
1578 } else {
1579 link->flags |= ATA_LFLAG_SW_ACTIVITY;
1580 if (val == BLINK_OFF) {
1581 /* set LED to ON for idle */
David Milburn87943ac2008-10-13 14:38:36 -05001582 port_led_state &= EM_MSG_LED_VALUE_OFF;
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -07001583 port_led_state |= (ap->port_no | (link->pmp << 8));
David Milburn87943ac2008-10-13 14:38:36 -05001584 port_led_state |= EM_MSG_LED_VALUE_ON; /* check this */
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -07001585 ahci_transmit_led_message(ap, port_led_state, 4);
1586 }
1587 }
1588 emp->blink_policy = val;
1589 return 0;
1590}
1591
1592static ssize_t ahci_activity_show(struct ata_device *dev, char *buf)
1593{
1594 struct ata_link *link = dev->link;
1595 struct ata_port *ap = link->ap;
1596 struct ahci_port_priv *pp = ap->private_data;
1597 struct ahci_em_priv *emp = &pp->em_priv[link->pmp];
1598
1599 /* display the saved value of activity behavior for this
1600 * disk.
1601 */
1602 return sprintf(buf, "%d\n", emp->blink_policy);
1603}
1604
Jeff Garzik2bcd8662007-05-28 07:45:27 -04001605static void ahci_port_init(struct pci_dev *pdev, struct ata_port *ap,
1606 int port_no, void __iomem *mmio,
1607 void __iomem *port_mmio)
1608{
1609 const char *emsg = NULL;
1610 int rc;
1611 u32 tmp;
1612
1613 /* make sure port is not active */
1614 rc = ahci_deinit_port(ap, &emsg);
1615 if (rc)
1616 dev_printk(KERN_WARNING, &pdev->dev,
1617 "%s (%d)\n", emsg, rc);
1618
1619 /* clear SError */
1620 tmp = readl(port_mmio + PORT_SCR_ERR);
1621 VPRINTK("PORT_SCR_ERR 0x%x\n", tmp);
1622 writel(tmp, port_mmio + PORT_SCR_ERR);
1623
1624 /* clear port IRQ */
1625 tmp = readl(port_mmio + PORT_IRQ_STAT);
1626 VPRINTK("PORT_IRQ_STAT 0x%x\n", tmp);
1627 if (tmp)
1628 writel(tmp, port_mmio + PORT_IRQ_STAT);
1629
1630 writel(1 << port_no, mmio + HOST_IRQ_STAT);
1631}
1632
Tejun Heo4447d352007-04-17 23:44:08 +09001633static void ahci_init_controller(struct ata_host *host)
Tejun Heod91542c2006-07-26 15:59:26 +09001634{
Tejun Heo417a1a62007-09-23 13:19:55 +09001635 struct ahci_host_priv *hpriv = host->private_data;
Tejun Heo4447d352007-04-17 23:44:08 +09001636 struct pci_dev *pdev = to_pci_dev(host->dev);
1637 void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
Jeff Garzik2bcd8662007-05-28 07:45:27 -04001638 int i;
Jeff Garzikcd70c262007-07-08 02:29:42 -04001639 void __iomem *port_mmio;
Tejun Heod91542c2006-07-26 15:59:26 +09001640 u32 tmp;
Jose Alberto Regueroc40e7cb2008-03-13 23:22:24 +01001641 int mv;
Tejun Heod91542c2006-07-26 15:59:26 +09001642
Tejun Heo417a1a62007-09-23 13:19:55 +09001643 if (hpriv->flags & AHCI_HFLAG_MV_PATA) {
Jose Alberto Regueroc40e7cb2008-03-13 23:22:24 +01001644 if (pdev->device == 0x6121)
1645 mv = 2;
1646 else
1647 mv = 4;
1648 port_mmio = __ahci_port_base(host, mv);
Jeff Garzikcd70c262007-07-08 02:29:42 -04001649
1650 writel(0, port_mmio + PORT_IRQ_MASK);
1651
1652 /* clear port IRQ */
1653 tmp = readl(port_mmio + PORT_IRQ_STAT);
1654 VPRINTK("PORT_IRQ_STAT 0x%x\n", tmp);
1655 if (tmp)
1656 writel(tmp, port_mmio + PORT_IRQ_STAT);
1657 }
1658
Tejun Heo4447d352007-04-17 23:44:08 +09001659 for (i = 0; i < host->n_ports; i++) {
1660 struct ata_port *ap = host->ports[i];
Tejun Heod91542c2006-07-26 15:59:26 +09001661
Jeff Garzikcd70c262007-07-08 02:29:42 -04001662 port_mmio = ahci_port_base(ap);
Tejun Heo4447d352007-04-17 23:44:08 +09001663 if (ata_port_is_dummy(ap))
Tejun Heod91542c2006-07-26 15:59:26 +09001664 continue;
Tejun Heod91542c2006-07-26 15:59:26 +09001665
Jeff Garzik2bcd8662007-05-28 07:45:27 -04001666 ahci_port_init(pdev, ap, i, mmio, port_mmio);
Tejun Heod91542c2006-07-26 15:59:26 +09001667 }
1668
1669 tmp = readl(mmio + HOST_CTL);
1670 VPRINTK("HOST_CTL 0x%x\n", tmp);
1671 writel(tmp | HOST_IRQ_EN, mmio + HOST_CTL);
1672 tmp = readl(mmio + HOST_CTL);
1673 VPRINTK("HOST_CTL 0x%x\n", tmp);
1674}
1675
Jeff Garzika8785392008-02-28 15:43:48 -05001676static void ahci_dev_config(struct ata_device *dev)
1677{
1678 struct ahci_host_priv *hpriv = dev->link->ap->host->private_data;
1679
Jeff Garzik4cde32f2008-03-24 22:40:40 -04001680 if (hpriv->flags & AHCI_HFLAG_SECT255) {
Jeff Garzika8785392008-02-28 15:43:48 -05001681 dev->max_sectors = 255;
Jeff Garzik4cde32f2008-03-24 22:40:40 -04001682 ata_dev_printk(dev, KERN_INFO,
1683 "SB600 AHCI: limiting to 255 sectors per cmd\n");
1684 }
Jeff Garzika8785392008-02-28 15:43:48 -05001685}
1686
Tejun Heo422b7592005-12-19 22:37:17 +09001687static unsigned int ahci_dev_classify(struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688{
Tejun Heo4447d352007-04-17 23:44:08 +09001689 void __iomem *port_mmio = ahci_port_base(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001690 struct ata_taskfile tf;
Tejun Heo422b7592005-12-19 22:37:17 +09001691 u32 tmp;
1692
1693 tmp = readl(port_mmio + PORT_SIG);
1694 tf.lbah = (tmp >> 24) & 0xff;
1695 tf.lbam = (tmp >> 16) & 0xff;
1696 tf.lbal = (tmp >> 8) & 0xff;
1697 tf.nsect = (tmp) & 0xff;
1698
1699 return ata_dev_classify(&tf);
1700}
1701
Tejun Heo12fad3f2006-05-15 21:03:55 +09001702static void ahci_fill_cmd_slot(struct ahci_port_priv *pp, unsigned int tag,
1703 u32 opts)
Tejun Heocc9278e2006-02-10 17:25:47 +09001704{
Tejun Heo12fad3f2006-05-15 21:03:55 +09001705 dma_addr_t cmd_tbl_dma;
1706
1707 cmd_tbl_dma = pp->cmd_tbl_dma + tag * AHCI_CMD_TBL_SZ;
1708
1709 pp->cmd_slot[tag].opts = cpu_to_le32(opts);
1710 pp->cmd_slot[tag].status = 0;
1711 pp->cmd_slot[tag].tbl_addr = cpu_to_le32(cmd_tbl_dma & 0xffffffff);
1712 pp->cmd_slot[tag].tbl_addr_hi = cpu_to_le32((cmd_tbl_dma >> 16) >> 16);
Tejun Heocc9278e2006-02-10 17:25:47 +09001713}
1714
Shane Huang78d5ae32009-08-07 15:05:52 +08001715static int ahci_kick_engine(struct ata_port *ap)
Bastiaan Jacquesbf2af2a2006-04-17 14:17:59 +02001716{
Tejun Heo350756f2008-04-07 22:47:21 +09001717 void __iomem *port_mmio = ahci_port_base(ap);
Jeff Garzikcca39742006-08-24 03:19:22 -04001718 struct ahci_host_priv *hpriv = ap->host->private_data;
Tejun Heo520d06f2008-04-07 22:47:21 +09001719 u8 status = readl(port_mmio + PORT_TFDATA) & 0xFF;
Bastiaan Jacquesbf2af2a2006-04-17 14:17:59 +02001720 u32 tmp;
Tejun Heod2e75df2007-07-16 14:29:39 +09001721 int busy, rc;
Bastiaan Jacquesbf2af2a2006-04-17 14:17:59 +02001722
Tejun Heod2e75df2007-07-16 14:29:39 +09001723 /* stop engine */
1724 rc = ahci_stop_engine(ap);
1725 if (rc)
1726 goto out_restart;
1727
Shane Huang78d5ae32009-08-07 15:05:52 +08001728 /* need to do CLO?
1729 * always do CLO if PMP is attached (AHCI-1.3 9.2)
1730 */
1731 busy = status & (ATA_BUSY | ATA_DRQ);
1732 if (!busy && !sata_pmp_attached(ap)) {
Tejun Heod2e75df2007-07-16 14:29:39 +09001733 rc = 0;
1734 goto out_restart;
1735 }
1736
1737 if (!(hpriv->cap & HOST_CAP_CLO)) {
1738 rc = -EOPNOTSUPP;
1739 goto out_restart;
1740 }
1741
1742 /* perform CLO */
Bastiaan Jacquesbf2af2a2006-04-17 14:17:59 +02001743 tmp = readl(port_mmio + PORT_CMD);
1744 tmp |= PORT_CMD_CLO;
1745 writel(tmp, port_mmio + PORT_CMD);
1746
Tejun Heod2e75df2007-07-16 14:29:39 +09001747 rc = 0;
Bastiaan Jacquesbf2af2a2006-04-17 14:17:59 +02001748 tmp = ata_wait_register(port_mmio + PORT_CMD,
1749 PORT_CMD_CLO, PORT_CMD_CLO, 1, 500);
1750 if (tmp & PORT_CMD_CLO)
Tejun Heod2e75df2007-07-16 14:29:39 +09001751 rc = -EIO;
Bastiaan Jacquesbf2af2a2006-04-17 14:17:59 +02001752
Tejun Heod2e75df2007-07-16 14:29:39 +09001753 /* restart engine */
1754 out_restart:
1755 ahci_start_engine(ap);
1756 return rc;
Bastiaan Jacquesbf2af2a2006-04-17 14:17:59 +02001757}
1758
Tejun Heo91c4a2e2007-07-16 14:29:39 +09001759static int ahci_exec_polled_cmd(struct ata_port *ap, int pmp,
1760 struct ata_taskfile *tf, int is_cmd, u16 flags,
1761 unsigned long timeout_msec)
1762{
1763 const u32 cmd_fis_len = 5; /* five dwords */
1764 struct ahci_port_priv *pp = ap->private_data;
1765 void __iomem *port_mmio = ahci_port_base(ap);
1766 u8 *fis = pp->cmd_tbl;
1767 u32 tmp;
1768
1769 /* prep the command */
1770 ata_tf_to_fis(tf, pmp, is_cmd, fis);
1771 ahci_fill_cmd_slot(pp, 0, cmd_fis_len | flags | (pmp << 12));
1772
1773 /* issue & wait */
1774 writel(1, port_mmio + PORT_CMD_ISSUE);
1775
1776 if (timeout_msec) {
1777 tmp = ata_wait_register(port_mmio + PORT_CMD_ISSUE, 0x1, 0x1,
1778 1, timeout_msec);
1779 if (tmp & 0x1) {
Shane Huang78d5ae32009-08-07 15:05:52 +08001780 ahci_kick_engine(ap);
Tejun Heo91c4a2e2007-07-16 14:29:39 +09001781 return -EBUSY;
1782 }
1783 } else
1784 readl(port_mmio + PORT_CMD_ISSUE); /* flush */
1785
1786 return 0;
1787}
1788
Shane Huangbd172432008-06-10 15:52:04 +08001789static int ahci_do_softreset(struct ata_link *link, unsigned int *class,
1790 int pmp, unsigned long deadline,
1791 int (*check_ready)(struct ata_link *link))
Tejun Heo4658f792006-03-22 21:07:03 +09001792{
Tejun Heocc0680a2007-08-06 18:36:23 +09001793 struct ata_port *ap = link->ap;
Tejun Heo55946392009-08-04 14:30:08 +09001794 struct ahci_host_priv *hpriv = ap->host->private_data;
Tejun Heo4658f792006-03-22 21:07:03 +09001795 const char *reason = NULL;
Tejun Heo2cbb79e2007-07-16 14:29:38 +09001796 unsigned long now, msecs;
Tejun Heo4658f792006-03-22 21:07:03 +09001797 struct ata_taskfile tf;
Tejun Heo4658f792006-03-22 21:07:03 +09001798 int rc;
1799
1800 DPRINTK("ENTER\n");
1801
1802 /* prepare for SRST (AHCI-1.1 10.4.1) */
Shane Huang78d5ae32009-08-07 15:05:52 +08001803 rc = ahci_kick_engine(ap);
Tejun Heo994056d2007-12-06 15:02:48 +09001804 if (rc && rc != -EOPNOTSUPP)
Tejun Heocc0680a2007-08-06 18:36:23 +09001805 ata_link_printk(link, KERN_WARNING,
Tejun Heo994056d2007-12-06 15:02:48 +09001806 "failed to reset engine (errno=%d)\n", rc);
Tejun Heo4658f792006-03-22 21:07:03 +09001807
Tejun Heocc0680a2007-08-06 18:36:23 +09001808 ata_tf_init(link->device, &tf);
Tejun Heo4658f792006-03-22 21:07:03 +09001809
1810 /* issue the first D2H Register FIS */
Tejun Heo2cbb79e2007-07-16 14:29:38 +09001811 msecs = 0;
1812 now = jiffies;
1813 if (time_after(now, deadline))
1814 msecs = jiffies_to_msecs(deadline - now);
1815
Tejun Heo4658f792006-03-22 21:07:03 +09001816 tf.ctl |= ATA_SRST;
Tejun Heoa9cf5e82007-07-16 14:29:39 +09001817 if (ahci_exec_polled_cmd(ap, pmp, &tf, 0,
Tejun Heo91c4a2e2007-07-16 14:29:39 +09001818 AHCI_CMD_RESET | AHCI_CMD_CLR_BUSY, msecs)) {
Tejun Heo4658f792006-03-22 21:07:03 +09001819 rc = -EIO;
1820 reason = "1st FIS failed";
1821 goto fail;
1822 }
1823
1824 /* spec says at least 5us, but be generous and sleep for 1ms */
1825 msleep(1);
1826
1827 /* issue the second D2H Register FIS */
Tejun Heo4658f792006-03-22 21:07:03 +09001828 tf.ctl &= ~ATA_SRST;
Tejun Heoa9cf5e82007-07-16 14:29:39 +09001829 ahci_exec_polled_cmd(ap, pmp, &tf, 0, 0, 0);
Tejun Heo4658f792006-03-22 21:07:03 +09001830
Tejun Heo705e76b2008-04-07 22:47:19 +09001831 /* wait for link to become ready */
Shane Huangbd172432008-06-10 15:52:04 +08001832 rc = ata_wait_after_reset(link, deadline, check_ready);
Tejun Heo55946392009-08-04 14:30:08 +09001833 if (rc == -EBUSY && hpriv->flags & AHCI_HFLAG_SRST_TOUT_IS_OFFLINE) {
1834 /*
1835 * Workaround for cases where link online status can't
1836 * be trusted. Treat device readiness timeout as link
1837 * offline.
1838 */
1839 ata_link_printk(link, KERN_INFO,
1840 "device not ready, treating as offline\n");
1841 *class = ATA_DEV_NONE;
1842 } else if (rc) {
1843 /* link occupied, -ENODEV too is an error */
Tejun Heo9b893912007-02-02 16:50:52 +09001844 reason = "device not ready";
1845 goto fail;
Tejun Heo55946392009-08-04 14:30:08 +09001846 } else
1847 *class = ahci_dev_classify(ap);
Tejun Heo4658f792006-03-22 21:07:03 +09001848
1849 DPRINTK("EXIT, class=%u\n", *class);
1850 return 0;
1851
Tejun Heo4658f792006-03-22 21:07:03 +09001852 fail:
Tejun Heocc0680a2007-08-06 18:36:23 +09001853 ata_link_printk(link, KERN_ERR, "softreset failed (%s)\n", reason);
Tejun Heo4658f792006-03-22 21:07:03 +09001854 return rc;
1855}
1856
Shane Huangbd172432008-06-10 15:52:04 +08001857static int ahci_check_ready(struct ata_link *link)
1858{
1859 void __iomem *port_mmio = ahci_port_base(link->ap);
1860 u8 status = readl(port_mmio + PORT_TFDATA) & 0xFF;
1861
1862 return ata_check_ready(status);
1863}
1864
1865static int ahci_softreset(struct ata_link *link, unsigned int *class,
1866 unsigned long deadline)
1867{
1868 int pmp = sata_srst_pmp(link);
1869
1870 DPRINTK("ENTER\n");
1871
1872 return ahci_do_softreset(link, class, pmp, deadline, ahci_check_ready);
1873}
1874
1875static int ahci_sb600_check_ready(struct ata_link *link)
1876{
1877 void __iomem *port_mmio = ahci_port_base(link->ap);
1878 u8 status = readl(port_mmio + PORT_TFDATA) & 0xFF;
1879 u32 irq_status = readl(port_mmio + PORT_IRQ_STAT);
1880
1881 /*
1882 * There is no need to check TFDATA if BAD PMP is found due to HW bug,
1883 * which can save timeout delay.
1884 */
1885 if (irq_status & PORT_IRQ_BAD_PMP)
1886 return -EIO;
1887
1888 return ata_check_ready(status);
1889}
1890
1891static int ahci_sb600_softreset(struct ata_link *link, unsigned int *class,
1892 unsigned long deadline)
1893{
1894 struct ata_port *ap = link->ap;
1895 void __iomem *port_mmio = ahci_port_base(ap);
1896 int pmp = sata_srst_pmp(link);
1897 int rc;
1898 u32 irq_sts;
1899
1900 DPRINTK("ENTER\n");
1901
1902 rc = ahci_do_softreset(link, class, pmp, deadline,
1903 ahci_sb600_check_ready);
1904
1905 /*
1906 * Soft reset fails on some ATI chips with IPMS set when PMP
1907 * is enabled but SATA HDD/ODD is connected to SATA port,
1908 * do soft reset again to port 0.
1909 */
1910 if (rc == -EIO) {
1911 irq_sts = readl(port_mmio + PORT_IRQ_STAT);
1912 if (irq_sts & PORT_IRQ_BAD_PMP) {
1913 ata_link_printk(link, KERN_WARNING,
Shane Huangb6931c12009-08-05 10:10:41 +08001914 "applying SB600 PMP SRST workaround "
1915 "and retrying\n");
Shane Huangbd172432008-06-10 15:52:04 +08001916 rc = ahci_do_softreset(link, class, 0, deadline,
1917 ahci_check_ready);
1918 }
1919 }
1920
1921 return rc;
1922}
1923
Tejun Heocc0680a2007-08-06 18:36:23 +09001924static int ahci_hardreset(struct ata_link *link, unsigned int *class,
Tejun Heod4b2bab2007-02-02 16:50:52 +09001925 unsigned long deadline)
Tejun Heo422b7592005-12-19 22:37:17 +09001926{
Tejun Heo9dadd452008-04-07 22:47:19 +09001927 const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
Tejun Heocc0680a2007-08-06 18:36:23 +09001928 struct ata_port *ap = link->ap;
Tejun Heo42969712006-05-31 18:28:18 +09001929 struct ahci_port_priv *pp = ap->private_data;
1930 u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
1931 struct ata_taskfile tf;
Tejun Heo9dadd452008-04-07 22:47:19 +09001932 bool online;
Tejun Heo4bd00f62006-02-11 16:26:02 +09001933 int rc;
1934
1935 DPRINTK("ENTER\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07001936
Tejun Heo4447d352007-04-17 23:44:08 +09001937 ahci_stop_engine(ap);
Tejun Heo42969712006-05-31 18:28:18 +09001938
1939 /* clear D2H reception area to properly wait for D2H FIS */
Tejun Heocc0680a2007-08-06 18:36:23 +09001940 ata_tf_init(link->device, &tf);
Tejun Heodfd7a3d2007-01-26 15:37:20 +09001941 tf.command = 0x80;
Tejun Heo99771262007-07-16 14:29:38 +09001942 ata_tf_to_fis(&tf, 0, 0, d2h_fis);
Tejun Heo42969712006-05-31 18:28:18 +09001943
Tejun Heo9dadd452008-04-07 22:47:19 +09001944 rc = sata_link_hardreset(link, timing, deadline, &online,
1945 ahci_check_ready);
Tejun Heo42969712006-05-31 18:28:18 +09001946
Tejun Heo4447d352007-04-17 23:44:08 +09001947 ahci_start_engine(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948
Tejun Heo9dadd452008-04-07 22:47:19 +09001949 if (online)
Tejun Heo4bd00f62006-02-11 16:26:02 +09001950 *class = ahci_dev_classify(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951
Tejun Heo4bd00f62006-02-11 16:26:02 +09001952 DPRINTK("EXIT, rc=%d, class=%u\n", rc, *class);
1953 return rc;
1954}
1955
Tejun Heocc0680a2007-08-06 18:36:23 +09001956static int ahci_vt8251_hardreset(struct ata_link *link, unsigned int *class,
Tejun Heod4b2bab2007-02-02 16:50:52 +09001957 unsigned long deadline)
Tejun Heoad616ff2006-11-01 18:00:24 +09001958{
Tejun Heocc0680a2007-08-06 18:36:23 +09001959 struct ata_port *ap = link->ap;
Tejun Heo9dadd452008-04-07 22:47:19 +09001960 bool online;
Tejun Heoad616ff2006-11-01 18:00:24 +09001961 int rc;
1962
1963 DPRINTK("ENTER\n");
1964
Tejun Heo4447d352007-04-17 23:44:08 +09001965 ahci_stop_engine(ap);
Tejun Heoad616ff2006-11-01 18:00:24 +09001966
Tejun Heocc0680a2007-08-06 18:36:23 +09001967 rc = sata_link_hardreset(link, sata_ehc_deb_timing(&link->eh_context),
Tejun Heo9dadd452008-04-07 22:47:19 +09001968 deadline, &online, NULL);
Tejun Heoad616ff2006-11-01 18:00:24 +09001969
Tejun Heo4447d352007-04-17 23:44:08 +09001970 ahci_start_engine(ap);
Tejun Heoad616ff2006-11-01 18:00:24 +09001971
1972 DPRINTK("EXIT, rc=%d, class=%u\n", rc, *class);
1973
1974 /* vt8251 doesn't clear BSY on signature FIS reception,
1975 * request follow-up softreset.
1976 */
Tejun Heo9dadd452008-04-07 22:47:19 +09001977 return online ? -EAGAIN : rc;
Tejun Heoad616ff2006-11-01 18:00:24 +09001978}
1979
Tejun Heoedc93052007-10-25 14:59:16 +09001980static int ahci_p5wdh_hardreset(struct ata_link *link, unsigned int *class,
1981 unsigned long deadline)
1982{
1983 struct ata_port *ap = link->ap;
1984 struct ahci_port_priv *pp = ap->private_data;
1985 u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
1986 struct ata_taskfile tf;
Tejun Heo9dadd452008-04-07 22:47:19 +09001987 bool online;
Tejun Heoedc93052007-10-25 14:59:16 +09001988 int rc;
1989
1990 ahci_stop_engine(ap);
1991
1992 /* clear D2H reception area to properly wait for D2H FIS */
1993 ata_tf_init(link->device, &tf);
1994 tf.command = 0x80;
1995 ata_tf_to_fis(&tf, 0, 0, d2h_fis);
1996
1997 rc = sata_link_hardreset(link, sata_ehc_deb_timing(&link->eh_context),
Tejun Heo9dadd452008-04-07 22:47:19 +09001998 deadline, &online, NULL);
Tejun Heoedc93052007-10-25 14:59:16 +09001999
2000 ahci_start_engine(ap);
2001
Tejun Heoedc93052007-10-25 14:59:16 +09002002 /* The pseudo configuration device on SIMG4726 attached to
2003 * ASUS P5W-DH Deluxe doesn't send signature FIS after
2004 * hardreset if no device is attached to the first downstream
2005 * port && the pseudo device locks up on SRST w/ PMP==0. To
2006 * work around this, wait for !BSY only briefly. If BSY isn't
2007 * cleared, perform CLO and proceed to IDENTIFY (achieved by
2008 * ATA_LFLAG_NO_SRST and ATA_LFLAG_ASSUME_ATA).
2009 *
2010 * Wait for two seconds. Devices attached to downstream port
2011 * which can't process the following IDENTIFY after this will
2012 * have to be reset again. For most cases, this should
2013 * suffice while making probing snappish enough.
2014 */
Tejun Heo9dadd452008-04-07 22:47:19 +09002015 if (online) {
2016 rc = ata_wait_after_reset(link, jiffies + 2 * HZ,
2017 ahci_check_ready);
2018 if (rc)
Shane Huang78d5ae32009-08-07 15:05:52 +08002019 ahci_kick_engine(ap);
Tejun Heo9dadd452008-04-07 22:47:19 +09002020 }
Tejun Heo9dadd452008-04-07 22:47:19 +09002021 return rc;
Tejun Heoedc93052007-10-25 14:59:16 +09002022}
2023
Tejun Heocc0680a2007-08-06 18:36:23 +09002024static void ahci_postreset(struct ata_link *link, unsigned int *class)
Tejun Heo4bd00f62006-02-11 16:26:02 +09002025{
Tejun Heocc0680a2007-08-06 18:36:23 +09002026 struct ata_port *ap = link->ap;
Tejun Heo4447d352007-04-17 23:44:08 +09002027 void __iomem *port_mmio = ahci_port_base(ap);
Tejun Heo4bd00f62006-02-11 16:26:02 +09002028 u32 new_tmp, tmp;
2029
Tejun Heo203c75b2008-04-07 22:47:18 +09002030 ata_std_postreset(link, class);
Jeff Garzik02eaa662005-11-12 01:32:19 -05002031
2032 /* Make sure port's ATAPI bit is set appropriately */
2033 new_tmp = tmp = readl(port_mmio + PORT_CMD);
Tejun Heo4bd00f62006-02-11 16:26:02 +09002034 if (*class == ATA_DEV_ATAPI)
Jeff Garzik02eaa662005-11-12 01:32:19 -05002035 new_tmp |= PORT_CMD_ATAPI;
2036 else
2037 new_tmp &= ~PORT_CMD_ATAPI;
2038 if (new_tmp != tmp) {
2039 writel(new_tmp, port_mmio + PORT_CMD);
2040 readl(port_mmio + PORT_CMD); /* flush */
2041 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042}
2043
Tejun Heo12fad3f2006-05-15 21:03:55 +09002044static unsigned int ahci_fill_sg(struct ata_queued_cmd *qc, void *cmd_tbl)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045{
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002046 struct scatterlist *sg;
Tejun Heoff2aeb12007-12-05 16:43:11 +09002047 struct ahci_sg *ahci_sg = cmd_tbl + AHCI_CMD_TBL_HDR_SZ;
2048 unsigned int si;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049
2050 VPRINTK("ENTER\n");
2051
2052 /*
2053 * Next, the S/G list.
2054 */
Tejun Heoff2aeb12007-12-05 16:43:11 +09002055 for_each_sg(qc->sg, sg, qc->n_elem, si) {
Jeff Garzikcedc9a42005-10-05 07:13:30 -04002056 dma_addr_t addr = sg_dma_address(sg);
2057 u32 sg_len = sg_dma_len(sg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002058
Tejun Heoff2aeb12007-12-05 16:43:11 +09002059 ahci_sg[si].addr = cpu_to_le32(addr & 0xffffffff);
2060 ahci_sg[si].addr_hi = cpu_to_le32((addr >> 16) >> 16);
2061 ahci_sg[si].flags_size = cpu_to_le32(sg_len - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 }
Jeff Garzik828d09d2005-11-12 01:27:07 -05002063
Tejun Heoff2aeb12007-12-05 16:43:11 +09002064 return si;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065}
2066
Shane Huangd6ef3152009-12-09 17:23:04 +08002067static int ahci_pmp_qc_defer(struct ata_queued_cmd *qc)
2068{
2069 struct ata_port *ap = qc->ap;
2070 struct ahci_port_priv *pp = ap->private_data;
2071
2072 if (!sata_pmp_attached(ap) || pp->fbs_enabled)
2073 return ata_std_qc_defer(qc);
2074 else
2075 return sata_pmp_qc_defer_cmd_switch(qc);
2076}
2077
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078static void ahci_qc_prep(struct ata_queued_cmd *qc)
2079{
Jeff Garzika0ea7322005-06-04 01:13:15 -04002080 struct ata_port *ap = qc->ap;
2081 struct ahci_port_priv *pp = ap->private_data;
Tejun Heo405e66b2007-11-27 19:28:53 +09002082 int is_atapi = ata_is_atapi(qc->tf.protocol);
Tejun Heo12fad3f2006-05-15 21:03:55 +09002083 void *cmd_tbl;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 u32 opts;
2085 const u32 cmd_fis_len = 5; /* five dwords */
Jeff Garzik828d09d2005-11-12 01:27:07 -05002086 unsigned int n_elem;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087
2088 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089 * Fill in command table information. First, the header,
2090 * a SATA Register - Host to Device command FIS.
2091 */
Tejun Heo12fad3f2006-05-15 21:03:55 +09002092 cmd_tbl = pp->cmd_tbl + qc->tag * AHCI_CMD_TBL_SZ;
2093
Tejun Heo7d50b602007-09-23 13:19:54 +09002094 ata_tf_to_fis(&qc->tf, qc->dev->link->pmp, 1, cmd_tbl);
Tejun Heocc9278e2006-02-10 17:25:47 +09002095 if (is_atapi) {
Tejun Heo12fad3f2006-05-15 21:03:55 +09002096 memset(cmd_tbl + AHCI_CMD_TBL_CDB, 0, 32);
2097 memcpy(cmd_tbl + AHCI_CMD_TBL_CDB, qc->cdb, qc->dev->cdb_len);
Jeff Garzika0ea7322005-06-04 01:13:15 -04002098 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002099
Tejun Heocc9278e2006-02-10 17:25:47 +09002100 n_elem = 0;
2101 if (qc->flags & ATA_QCFLAG_DMAMAP)
Tejun Heo12fad3f2006-05-15 21:03:55 +09002102 n_elem = ahci_fill_sg(qc, cmd_tbl);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002103
Tejun Heocc9278e2006-02-10 17:25:47 +09002104 /*
2105 * Fill in command slot information.
2106 */
Tejun Heo7d50b602007-09-23 13:19:54 +09002107 opts = cmd_fis_len | n_elem << 16 | (qc->dev->link->pmp << 12);
Tejun Heocc9278e2006-02-10 17:25:47 +09002108 if (qc->tf.flags & ATA_TFLAG_WRITE)
2109 opts |= AHCI_CMD_WRITE;
2110 if (is_atapi)
Tejun Heo4b10e552006-03-12 11:25:27 +09002111 opts |= AHCI_CMD_ATAPI | AHCI_CMD_PREFETCH;
Jeff Garzik828d09d2005-11-12 01:27:07 -05002112
Tejun Heo12fad3f2006-05-15 21:03:55 +09002113 ahci_fill_cmd_slot(pp, qc->tag, opts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114}
2115
Shane Huangd6ef3152009-12-09 17:23:04 +08002116static void ahci_fbs_dec_intr(struct ata_port *ap)
2117{
2118 struct ahci_port_priv *pp = ap->private_data;
2119 void __iomem *port_mmio = ahci_port_base(ap);
2120 u32 fbs = readl(port_mmio + PORT_FBS);
2121 int retries = 3;
2122
2123 DPRINTK("ENTER\n");
2124 BUG_ON(!pp->fbs_enabled);
2125
2126 /* time to wait for DEC is not specified by AHCI spec,
2127 * add a retry loop for safety.
2128 */
2129 writel(fbs | PORT_FBS_DEC, port_mmio + PORT_FBS);
2130 fbs = readl(port_mmio + PORT_FBS);
2131 while ((fbs & PORT_FBS_DEC) && retries--) {
2132 udelay(1);
2133 fbs = readl(port_mmio + PORT_FBS);
2134 }
2135
2136 if (fbs & PORT_FBS_DEC)
2137 dev_printk(KERN_ERR, ap->host->dev,
2138 "failed to clear device error\n");
2139}
2140
Tejun Heo78cd52d2006-05-15 20:58:29 +09002141static void ahci_error_intr(struct ata_port *ap, u32 irq_stat)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142{
Tejun Heo417a1a62007-09-23 13:19:55 +09002143 struct ahci_host_priv *hpriv = ap->host->private_data;
Tejun Heo78cd52d2006-05-15 20:58:29 +09002144 struct ahci_port_priv *pp = ap->private_data;
Tejun Heo7d50b602007-09-23 13:19:54 +09002145 struct ata_eh_info *host_ehi = &ap->link.eh_info;
2146 struct ata_link *link = NULL;
2147 struct ata_queued_cmd *active_qc;
2148 struct ata_eh_info *active_ehi;
Shane Huangd6ef3152009-12-09 17:23:04 +08002149 bool fbs_need_dec = false;
Tejun Heo78cd52d2006-05-15 20:58:29 +09002150 u32 serror;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002151
Shane Huangd6ef3152009-12-09 17:23:04 +08002152 /* determine active link with error */
2153 if (pp->fbs_enabled) {
2154 void __iomem *port_mmio = ahci_port_base(ap);
2155 u32 fbs = readl(port_mmio + PORT_FBS);
2156 int pmp = fbs >> PORT_FBS_DWE_OFFSET;
2157
2158 if ((fbs & PORT_FBS_SDE) && (pmp < ap->nr_pmp_links) &&
2159 ata_link_online(&ap->pmp_link[pmp])) {
2160 link = &ap->pmp_link[pmp];
2161 fbs_need_dec = true;
2162 }
2163
2164 } else
2165 ata_for_each_link(link, ap, EDGE)
2166 if (ata_link_active(link))
2167 break;
2168
Tejun Heo7d50b602007-09-23 13:19:54 +09002169 if (!link)
2170 link = &ap->link;
2171
2172 active_qc = ata_qc_from_tag(ap, link->active_tag);
2173 active_ehi = &link->eh_info;
2174
2175 /* record irq stat */
2176 ata_ehi_clear_desc(host_ehi);
2177 ata_ehi_push_desc(host_ehi, "irq_stat 0x%08x", irq_stat);
Jeff Garzik9f68a242005-11-15 14:03:47 -05002178
Tejun Heo78cd52d2006-05-15 20:58:29 +09002179 /* AHCI needs SError cleared; otherwise, it might lock up */
Tejun Heo82ef04f2008-07-31 17:02:40 +09002180 ahci_scr_read(&ap->link, SCR_ERROR, &serror);
2181 ahci_scr_write(&ap->link, SCR_ERROR, serror);
Tejun Heo7d50b602007-09-23 13:19:54 +09002182 host_ehi->serror |= serror;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183
Tejun Heo41669552006-11-29 11:33:14 +09002184 /* some controllers set IRQ_IF_ERR on device errors, ignore it */
Tejun Heo417a1a62007-09-23 13:19:55 +09002185 if (hpriv->flags & AHCI_HFLAG_IGN_IRQ_IF_ERR)
Tejun Heo41669552006-11-29 11:33:14 +09002186 irq_stat &= ~PORT_IRQ_IF_ERR;
2187
Conke Hu55a61602007-03-27 18:33:05 +08002188 if (irq_stat & PORT_IRQ_TF_ERR) {
Tejun Heo7d50b602007-09-23 13:19:54 +09002189 /* If qc is active, charge it; otherwise, the active
2190 * link. There's no active qc on NCQ errors. It will
2191 * be determined by EH by reading log page 10h.
2192 */
2193 if (active_qc)
2194 active_qc->err_mask |= AC_ERR_DEV;
2195 else
2196 active_ehi->err_mask |= AC_ERR_DEV;
2197
Tejun Heo417a1a62007-09-23 13:19:55 +09002198 if (hpriv->flags & AHCI_HFLAG_IGN_SERR_INTERNAL)
Tejun Heo7d50b602007-09-23 13:19:54 +09002199 host_ehi->serror &= ~SERR_INTERNAL;
Tejun Heo78cd52d2006-05-15 20:58:29 +09002200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201
Tejun Heo78cd52d2006-05-15 20:58:29 +09002202 if (irq_stat & PORT_IRQ_UNK_FIS) {
2203 u32 *unk = (u32 *)(pp->rx_fis + RX_FIS_UNK);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002204
Tejun Heo7d50b602007-09-23 13:19:54 +09002205 active_ehi->err_mask |= AC_ERR_HSM;
Tejun Heocf480622008-01-24 00:05:14 +09002206 active_ehi->action |= ATA_EH_RESET;
Tejun Heo7d50b602007-09-23 13:19:54 +09002207 ata_ehi_push_desc(active_ehi,
2208 "unknown FIS %08x %08x %08x %08x" ,
Tejun Heo78cd52d2006-05-15 20:58:29 +09002209 unk[0], unk[1], unk[2], unk[3]);
2210 }
Jeff Garzikb8f61532005-08-25 22:01:20 -04002211
Tejun Heo071f44b2008-04-07 22:47:22 +09002212 if (sata_pmp_attached(ap) && (irq_stat & PORT_IRQ_BAD_PMP)) {
Tejun Heo7d50b602007-09-23 13:19:54 +09002213 active_ehi->err_mask |= AC_ERR_HSM;
Tejun Heocf480622008-01-24 00:05:14 +09002214 active_ehi->action |= ATA_EH_RESET;
Tejun Heo7d50b602007-09-23 13:19:54 +09002215 ata_ehi_push_desc(active_ehi, "incorrect PMP");
2216 }
Tejun Heo78cd52d2006-05-15 20:58:29 +09002217
Tejun Heo7d50b602007-09-23 13:19:54 +09002218 if (irq_stat & (PORT_IRQ_HBUS_ERR | PORT_IRQ_HBUS_DATA_ERR)) {
2219 host_ehi->err_mask |= AC_ERR_HOST_BUS;
Tejun Heocf480622008-01-24 00:05:14 +09002220 host_ehi->action |= ATA_EH_RESET;
Tejun Heo7d50b602007-09-23 13:19:54 +09002221 ata_ehi_push_desc(host_ehi, "host bus error");
2222 }
2223
2224 if (irq_stat & PORT_IRQ_IF_ERR) {
Shane Huangd6ef3152009-12-09 17:23:04 +08002225 if (fbs_need_dec)
2226 active_ehi->err_mask |= AC_ERR_DEV;
2227 else {
2228 host_ehi->err_mask |= AC_ERR_ATA_BUS;
2229 host_ehi->action |= ATA_EH_RESET;
2230 }
2231
Tejun Heo7d50b602007-09-23 13:19:54 +09002232 ata_ehi_push_desc(host_ehi, "interface fatal error");
2233 }
2234
2235 if (irq_stat & (PORT_IRQ_CONNECT | PORT_IRQ_PHYRDY)) {
2236 ata_ehi_hotplugged(host_ehi);
2237 ata_ehi_push_desc(host_ehi, "%s",
2238 irq_stat & PORT_IRQ_CONNECT ?
2239 "connection status changed" : "PHY RDY changed");
2240 }
2241
2242 /* okay, let's hand over to EH */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002243
Tejun Heo78cd52d2006-05-15 20:58:29 +09002244 if (irq_stat & PORT_IRQ_FREEZE)
2245 ata_port_freeze(ap);
Shane Huangd6ef3152009-12-09 17:23:04 +08002246 else if (fbs_need_dec) {
2247 ata_link_abort(link);
2248 ahci_fbs_dec_intr(ap);
2249 } else
Tejun Heo78cd52d2006-05-15 20:58:29 +09002250 ata_port_abort(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002251}
2252
Jeff Garzikdf69c9c2007-05-26 20:46:51 -04002253static void ahci_port_intr(struct ata_port *ap)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002254{
Tejun Heo350756f2008-04-07 22:47:21 +09002255 void __iomem *port_mmio = ahci_port_base(ap);
Tejun Heo9af5c9c2007-08-06 18:36:22 +09002256 struct ata_eh_info *ehi = &ap->link.eh_info;
Tejun Heo0291f952007-01-25 19:16:28 +09002257 struct ahci_port_priv *pp = ap->private_data;
Tejun Heo5f226c62007-10-09 15:02:23 +09002258 struct ahci_host_priv *hpriv = ap->host->private_data;
Tejun Heob06ce3e2007-10-09 15:06:48 +09002259 int resetting = !!(ap->pflags & ATA_PFLAG_RESETTING);
Tejun Heo12fad3f2006-05-15 21:03:55 +09002260 u32 status, qc_active;
Tejun Heo459ad682007-12-07 12:46:23 +09002261 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002262
2263 status = readl(port_mmio + PORT_IRQ_STAT);
2264 writel(status, port_mmio + PORT_IRQ_STAT);
2265
Tejun Heob06ce3e2007-10-09 15:06:48 +09002266 /* ignore BAD_PMP while resetting */
2267 if (unlikely(resetting))
2268 status &= ~PORT_IRQ_BAD_PMP;
2269
Kristen Carlson Accardi31556592007-10-25 01:33:26 -04002270 /* If we are getting PhyRdy, this is
2271 * just a power state change, we should
2272 * clear out this, plus the PhyRdy/Comm
2273 * Wake bits from Serror
2274 */
2275 if ((hpriv->flags & AHCI_HFLAG_NO_HOTPLUG) &&
2276 (status & PORT_IRQ_PHYRDY)) {
2277 status &= ~PORT_IRQ_PHYRDY;
Tejun Heo82ef04f2008-07-31 17:02:40 +09002278 ahci_scr_write(&ap->link, SCR_ERROR, ((1 << 16) | (1 << 18)));
Kristen Carlson Accardi31556592007-10-25 01:33:26 -04002279 }
2280
Tejun Heo78cd52d2006-05-15 20:58:29 +09002281 if (unlikely(status & PORT_IRQ_ERROR)) {
2282 ahci_error_intr(ap, status);
2283 return;
2284 }
2285
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04002286 if (status & PORT_IRQ_SDB_FIS) {
Tejun Heo5f226c62007-10-09 15:02:23 +09002287 /* If SNotification is available, leave notification
2288 * handling to sata_async_notification(). If not,
2289 * emulate it by snooping SDB FIS RX area.
2290 *
2291 * Snooping FIS RX area is probably cheaper than
2292 * poking SNotification but some constrollers which
2293 * implement SNotification, ICH9 for example, don't
2294 * store AN SDB FIS into receive area.
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04002295 */
Tejun Heo5f226c62007-10-09 15:02:23 +09002296 if (hpriv->cap & HOST_CAP_SNTF)
Tejun Heo7d77b242007-09-23 13:14:13 +09002297 sata_async_notification(ap);
Tejun Heo5f226c62007-10-09 15:02:23 +09002298 else {
2299 /* If the 'N' bit in word 0 of the FIS is set,
2300 * we just received asynchronous notification.
2301 * Tell libata about it.
Shane Huangd6ef3152009-12-09 17:23:04 +08002302 *
2303 * Lack of SNotification should not appear in
2304 * ahci 1.2, so the workaround is unnecessary
2305 * when FBS is enabled.
Tejun Heo5f226c62007-10-09 15:02:23 +09002306 */
Shane Huangd6ef3152009-12-09 17:23:04 +08002307 if (pp->fbs_enabled)
2308 WARN_ON_ONCE(1);
2309 else {
2310 const __le32 *f = pp->rx_fis + RX_FIS_SDB;
2311 u32 f0 = le32_to_cpu(f[0]);
2312 if (f0 & (1 << 15))
2313 sata_async_notification(ap);
2314 }
Tejun Heo5f226c62007-10-09 15:02:23 +09002315 }
Kristen Carlson Accardi2f294962007-08-15 04:11:25 -04002316 }
2317
Tejun Heo7d50b602007-09-23 13:19:54 +09002318 /* pp->active_link is valid iff any command is in flight */
2319 if (ap->qc_active && pp->active_link->sactive)
Tejun Heo12fad3f2006-05-15 21:03:55 +09002320 qc_active = readl(port_mmio + PORT_SCR_ACT);
2321 else
2322 qc_active = readl(port_mmio + PORT_CMD_ISSUE);
2323
Tejun Heo79f97da2008-04-07 22:47:20 +09002324 rc = ata_qc_complete_multiple(ap, qc_active);
Tejun Heob06ce3e2007-10-09 15:06:48 +09002325
Tejun Heo459ad682007-12-07 12:46:23 +09002326 /* while resetting, invalid completions are expected */
2327 if (unlikely(rc < 0 && !resetting)) {
Tejun Heo12fad3f2006-05-15 21:03:55 +09002328 ehi->err_mask |= AC_ERR_HSM;
Tejun Heocf480622008-01-24 00:05:14 +09002329 ehi->action |= ATA_EH_RESET;
Tejun Heo12fad3f2006-05-15 21:03:55 +09002330 ata_port_freeze(ap);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002331 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002332}
2333
David Howells7d12e782006-10-05 14:55:46 +01002334static irqreturn_t ahci_interrupt(int irq, void *dev_instance)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002335{
Jeff Garzikcca39742006-08-24 03:19:22 -04002336 struct ata_host *host = dev_instance;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002337 struct ahci_host_priv *hpriv;
2338 unsigned int i, handled = 0;
Jeff Garzikea6ba102005-08-30 05:18:18 -04002339 void __iomem *mmio;
Tejun Heod28f87a2008-07-05 13:10:50 +09002340 u32 irq_stat, irq_masked;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
2342 VPRINTK("ENTER\n");
2343
Jeff Garzikcca39742006-08-24 03:19:22 -04002344 hpriv = host->private_data;
Tejun Heo0d5ff562007-02-01 15:06:36 +09002345 mmio = host->iomap[AHCI_PCI_BAR];
Linus Torvalds1da177e2005-04-16 15:20:36 -07002346
2347 /* sigh. 0xffffffff is a valid return from h/w */
2348 irq_stat = readl(mmio + HOST_IRQ_STAT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 if (!irq_stat)
2350 return IRQ_NONE;
2351
Tejun Heod28f87a2008-07-05 13:10:50 +09002352 irq_masked = irq_stat & hpriv->port_map;
2353
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002354 spin_lock(&host->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002356 for (i = 0; i < host->n_ports; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002357 struct ata_port *ap;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002358
Tejun Heod28f87a2008-07-05 13:10:50 +09002359 if (!(irq_masked & (1 << i)))
Jeff Garzik67846b32005-10-05 02:58:32 -04002360 continue;
2361
Jeff Garzikcca39742006-08-24 03:19:22 -04002362 ap = host->ports[i];
Jeff Garzik67846b32005-10-05 02:58:32 -04002363 if (ap) {
Jeff Garzikdf69c9c2007-05-26 20:46:51 -04002364 ahci_port_intr(ap);
Jeff Garzik67846b32005-10-05 02:58:32 -04002365 VPRINTK("port %u\n", i);
2366 } else {
2367 VPRINTK("port %u (no irq)\n", i);
Tejun Heo6971ed12006-03-11 12:47:54 +09002368 if (ata_ratelimit())
Jeff Garzikcca39742006-08-24 03:19:22 -04002369 dev_printk(KERN_WARNING, host->dev,
Jeff Garzika9524a72005-10-30 14:39:11 -05002370 "interrupt on disabled port %u\n", i);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 }
Jeff Garzik67846b32005-10-05 02:58:32 -04002372
Linus Torvalds1da177e2005-04-16 15:20:36 -07002373 handled = 1;
2374 }
2375
Tejun Heod28f87a2008-07-05 13:10:50 +09002376 /* HOST_IRQ_STAT behaves as level triggered latch meaning that
2377 * it should be cleared after all the port events are cleared;
2378 * otherwise, it will raise a spurious interrupt after each
2379 * valid one. Please read section 10.6.2 of ahci 1.1 for more
2380 * information.
2381 *
2382 * Also, use the unmasked value to clear interrupt as spurious
2383 * pending event on a dummy port might cause screaming IRQ.
2384 */
Tejun Heoea0c62f2008-06-28 01:49:02 +09002385 writel(irq_stat, mmio + HOST_IRQ_STAT);
2386
Jeff Garzikcca39742006-08-24 03:19:22 -04002387 spin_unlock(&host->lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002388
2389 VPRINTK("EXIT\n");
2390
2391 return IRQ_RETVAL(handled);
2392}
2393
Tejun Heo9a3d9eb2006-01-23 13:09:36 +09002394static unsigned int ahci_qc_issue(struct ata_queued_cmd *qc)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002395{
2396 struct ata_port *ap = qc->ap;
Tejun Heo4447d352007-04-17 23:44:08 +09002397 void __iomem *port_mmio = ahci_port_base(ap);
Tejun Heo7d50b602007-09-23 13:19:54 +09002398 struct ahci_port_priv *pp = ap->private_data;
2399
2400 /* Keep track of the currently active link. It will be used
2401 * in completion path to determine whether NCQ phase is in
2402 * progress.
2403 */
2404 pp->active_link = qc->dev->link;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405
Tejun Heo12fad3f2006-05-15 21:03:55 +09002406 if (qc->tf.protocol == ATA_PROT_NCQ)
2407 writel(1 << qc->tag, port_mmio + PORT_SCR_ACT);
Shane Huangd6ef3152009-12-09 17:23:04 +08002408
2409 if (pp->fbs_enabled && pp->fbs_last_dev != qc->dev->link->pmp) {
2410 u32 fbs = readl(port_mmio + PORT_FBS);
2411 fbs &= ~(PORT_FBS_DEV_MASK | PORT_FBS_DEC);
2412 fbs |= qc->dev->link->pmp << PORT_FBS_DEV_OFFSET;
2413 writel(fbs, port_mmio + PORT_FBS);
2414 pp->fbs_last_dev = qc->dev->link->pmp;
2415 }
2416
Tejun Heo12fad3f2006-05-15 21:03:55 +09002417 writel(1 << qc->tag, port_mmio + PORT_CMD_ISSUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002418
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -07002419 ahci_sw_activity(qc->dev->link);
2420
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 return 0;
2422}
2423
Tejun Heo4c9bf4e2008-04-07 22:47:20 +09002424static bool ahci_qc_fill_rtf(struct ata_queued_cmd *qc)
2425{
2426 struct ahci_port_priv *pp = qc->ap->private_data;
2427 u8 *d2h_fis = pp->rx_fis + RX_FIS_D2H_REG;
2428
Shane Huangd6ef3152009-12-09 17:23:04 +08002429 if (pp->fbs_enabled)
2430 d2h_fis += qc->dev->link->pmp * AHCI_RX_FIS_SZ;
2431
Tejun Heo4c9bf4e2008-04-07 22:47:20 +09002432 ata_tf_from_fis(d2h_fis, &qc->result_tf);
2433 return true;
2434}
2435
Tejun Heo78cd52d2006-05-15 20:58:29 +09002436static void ahci_freeze(struct ata_port *ap)
2437{
Tejun Heo4447d352007-04-17 23:44:08 +09002438 void __iomem *port_mmio = ahci_port_base(ap);
Tejun Heo78cd52d2006-05-15 20:58:29 +09002439
2440 /* turn IRQ off */
2441 writel(0, port_mmio + PORT_IRQ_MASK);
2442}
2443
2444static void ahci_thaw(struct ata_port *ap)
2445{
Tejun Heo0d5ff562007-02-01 15:06:36 +09002446 void __iomem *mmio = ap->host->iomap[AHCI_PCI_BAR];
Tejun Heo4447d352007-04-17 23:44:08 +09002447 void __iomem *port_mmio = ahci_port_base(ap);
Tejun Heo78cd52d2006-05-15 20:58:29 +09002448 u32 tmp;
Kristen Carlson Accardia7384922007-08-09 14:23:41 -07002449 struct ahci_port_priv *pp = ap->private_data;
Tejun Heo78cd52d2006-05-15 20:58:29 +09002450
2451 /* clear IRQ */
2452 tmp = readl(port_mmio + PORT_IRQ_STAT);
2453 writel(tmp, port_mmio + PORT_IRQ_STAT);
Tejun Heoa7187282007-01-27 11:04:26 +09002454 writel(1 << ap->port_no, mmio + HOST_IRQ_STAT);
Tejun Heo78cd52d2006-05-15 20:58:29 +09002455
Tejun Heo1c954a42007-10-09 15:01:37 +09002456 /* turn IRQ back on */
2457 writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
Tejun Heo78cd52d2006-05-15 20:58:29 +09002458}
2459
2460static void ahci_error_handler(struct ata_port *ap)
2461{
Tejun Heob51e9e52006-06-29 01:29:30 +09002462 if (!(ap->pflags & ATA_PFLAG_FROZEN)) {
Tejun Heo78cd52d2006-05-15 20:58:29 +09002463 /* restart engine */
Tejun Heo4447d352007-04-17 23:44:08 +09002464 ahci_stop_engine(ap);
2465 ahci_start_engine(ap);
Tejun Heo78cd52d2006-05-15 20:58:29 +09002466 }
2467
Tejun Heoa1efdab2008-03-25 12:22:50 +09002468 sata_pmp_error_handler(ap);
Tejun Heoedc93052007-10-25 14:59:16 +09002469}
2470
Tejun Heo78cd52d2006-05-15 20:58:29 +09002471static void ahci_post_internal_cmd(struct ata_queued_cmd *qc)
2472{
2473 struct ata_port *ap = qc->ap;
2474
Tejun Heod2e75df2007-07-16 14:29:39 +09002475 /* make DMA engine forget about the failed command */
2476 if (qc->flags & ATA_QCFLAG_FAILED)
Shane Huang78d5ae32009-08-07 15:05:52 +08002477 ahci_kick_engine(ap);
Tejun Heo78cd52d2006-05-15 20:58:29 +09002478}
2479
Shane Huangd6ef3152009-12-09 17:23:04 +08002480static void ahci_enable_fbs(struct ata_port *ap)
2481{
2482 struct ahci_port_priv *pp = ap->private_data;
2483 void __iomem *port_mmio = ahci_port_base(ap);
2484 u32 fbs;
2485 int rc;
2486
2487 if (!pp->fbs_supported)
2488 return;
2489
2490 fbs = readl(port_mmio + PORT_FBS);
2491 if (fbs & PORT_FBS_EN) {
2492 pp->fbs_enabled = true;
2493 pp->fbs_last_dev = -1; /* initialization */
2494 return;
2495 }
2496
2497 rc = ahci_stop_engine(ap);
2498 if (rc)
2499 return;
2500
2501 writel(fbs | PORT_FBS_EN, port_mmio + PORT_FBS);
2502 fbs = readl(port_mmio + PORT_FBS);
2503 if (fbs & PORT_FBS_EN) {
2504 dev_printk(KERN_INFO, ap->host->dev, "FBS is enabled.\n");
2505 pp->fbs_enabled = true;
2506 pp->fbs_last_dev = -1; /* initialization */
2507 } else
2508 dev_printk(KERN_ERR, ap->host->dev, "Failed to enable FBS\n");
2509
2510 ahci_start_engine(ap);
2511}
2512
2513static void ahci_disable_fbs(struct ata_port *ap)
2514{
2515 struct ahci_port_priv *pp = ap->private_data;
2516 void __iomem *port_mmio = ahci_port_base(ap);
2517 u32 fbs;
2518 int rc;
2519
2520 if (!pp->fbs_supported)
2521 return;
2522
2523 fbs = readl(port_mmio + PORT_FBS);
2524 if ((fbs & PORT_FBS_EN) == 0) {
2525 pp->fbs_enabled = false;
2526 return;
2527 }
2528
2529 rc = ahci_stop_engine(ap);
2530 if (rc)
2531 return;
2532
2533 writel(fbs & ~PORT_FBS_EN, port_mmio + PORT_FBS);
2534 fbs = readl(port_mmio + PORT_FBS);
2535 if (fbs & PORT_FBS_EN)
2536 dev_printk(KERN_ERR, ap->host->dev, "Failed to disable FBS\n");
2537 else {
2538 dev_printk(KERN_INFO, ap->host->dev, "FBS is disabled.\n");
2539 pp->fbs_enabled = false;
2540 }
2541
2542 ahci_start_engine(ap);
2543}
2544
Tejun Heo7d50b602007-09-23 13:19:54 +09002545static void ahci_pmp_attach(struct ata_port *ap)
2546{
2547 void __iomem *port_mmio = ahci_port_base(ap);
Tejun Heo1c954a42007-10-09 15:01:37 +09002548 struct ahci_port_priv *pp = ap->private_data;
Tejun Heo7d50b602007-09-23 13:19:54 +09002549 u32 cmd;
2550
2551 cmd = readl(port_mmio + PORT_CMD);
2552 cmd |= PORT_CMD_PMP;
2553 writel(cmd, port_mmio + PORT_CMD);
Tejun Heo1c954a42007-10-09 15:01:37 +09002554
Shane Huangd6ef3152009-12-09 17:23:04 +08002555 ahci_enable_fbs(ap);
2556
Tejun Heo1c954a42007-10-09 15:01:37 +09002557 pp->intr_mask |= PORT_IRQ_BAD_PMP;
2558 writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
Tejun Heo7d50b602007-09-23 13:19:54 +09002559}
2560
2561static void ahci_pmp_detach(struct ata_port *ap)
2562{
2563 void __iomem *port_mmio = ahci_port_base(ap);
Tejun Heo1c954a42007-10-09 15:01:37 +09002564 struct ahci_port_priv *pp = ap->private_data;
Tejun Heo7d50b602007-09-23 13:19:54 +09002565 u32 cmd;
2566
Shane Huangd6ef3152009-12-09 17:23:04 +08002567 ahci_disable_fbs(ap);
2568
Tejun Heo7d50b602007-09-23 13:19:54 +09002569 cmd = readl(port_mmio + PORT_CMD);
2570 cmd &= ~PORT_CMD_PMP;
2571 writel(cmd, port_mmio + PORT_CMD);
Tejun Heo1c954a42007-10-09 15:01:37 +09002572
2573 pp->intr_mask &= ~PORT_IRQ_BAD_PMP;
2574 writel(pp->intr_mask, port_mmio + PORT_IRQ_MASK);
Tejun Heo7d50b602007-09-23 13:19:54 +09002575}
2576
Alexey Dobriyan028a2592007-07-17 23:48:48 +04002577static int ahci_port_resume(struct ata_port *ap)
2578{
2579 ahci_power_up(ap);
2580 ahci_start_port(ap);
2581
Tejun Heo071f44b2008-04-07 22:47:22 +09002582 if (sata_pmp_attached(ap))
Tejun Heo7d50b602007-09-23 13:19:54 +09002583 ahci_pmp_attach(ap);
2584 else
2585 ahci_pmp_detach(ap);
2586
Alexey Dobriyan028a2592007-07-17 23:48:48 +04002587 return 0;
2588}
2589
Tejun Heo438ac6d2007-03-02 17:31:26 +09002590#ifdef CONFIG_PM
Tejun Heoc1332872006-07-26 15:59:26 +09002591static int ahci_port_suspend(struct ata_port *ap, pm_message_t mesg)
2592{
Tejun Heoc1332872006-07-26 15:59:26 +09002593 const char *emsg = NULL;
2594 int rc;
2595
Tejun Heo4447d352007-04-17 23:44:08 +09002596 rc = ahci_deinit_port(ap, &emsg);
Tejun Heo8e16f942006-11-20 15:42:36 +09002597 if (rc == 0)
Tejun Heo4447d352007-04-17 23:44:08 +09002598 ahci_power_down(ap);
Tejun Heo8e16f942006-11-20 15:42:36 +09002599 else {
Tejun Heoc1332872006-07-26 15:59:26 +09002600 ata_port_printk(ap, KERN_ERR, "%s (%d)\n", emsg, rc);
Jeff Garzikdf69c9c2007-05-26 20:46:51 -04002601 ahci_start_port(ap);
Tejun Heoc1332872006-07-26 15:59:26 +09002602 }
2603
2604 return rc;
2605}
2606
Tejun Heoc1332872006-07-26 15:59:26 +09002607static int ahci_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
2608{
Jeff Garzikcca39742006-08-24 03:19:22 -04002609 struct ata_host *host = dev_get_drvdata(&pdev->dev);
Tejun Heo9b10ae82009-05-30 20:50:12 +09002610 struct ahci_host_priv *hpriv = host->private_data;
Tejun Heo0d5ff562007-02-01 15:06:36 +09002611 void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
Tejun Heoc1332872006-07-26 15:59:26 +09002612 u32 ctl;
2613
Tejun Heo9b10ae82009-05-30 20:50:12 +09002614 if (mesg.event & PM_EVENT_SUSPEND &&
2615 hpriv->flags & AHCI_HFLAG_NO_SUSPEND) {
2616 dev_printk(KERN_ERR, &pdev->dev,
2617 "BIOS update required for suspend/resume\n");
2618 return -EIO;
2619 }
2620
Rafael J. Wysocki3a2d5b72008-02-23 19:13:25 +01002621 if (mesg.event & PM_EVENT_SLEEP) {
Tejun Heoc1332872006-07-26 15:59:26 +09002622 /* AHCI spec rev1.1 section 8.3.3:
2623 * Software must disable interrupts prior to requesting a
2624 * transition of the HBA to D3 state.
2625 */
2626 ctl = readl(mmio + HOST_CTL);
2627 ctl &= ~HOST_IRQ_EN;
2628 writel(ctl, mmio + HOST_CTL);
2629 readl(mmio + HOST_CTL); /* flush */
2630 }
2631
2632 return ata_pci_device_suspend(pdev, mesg);
2633}
2634
2635static int ahci_pci_device_resume(struct pci_dev *pdev)
2636{
Jeff Garzikcca39742006-08-24 03:19:22 -04002637 struct ata_host *host = dev_get_drvdata(&pdev->dev);
Tejun Heoc1332872006-07-26 15:59:26 +09002638 int rc;
2639
Tejun Heo553c4aa2006-12-26 19:39:50 +09002640 rc = ata_pci_device_do_resume(pdev);
2641 if (rc)
2642 return rc;
Tejun Heoc1332872006-07-26 15:59:26 +09002643
2644 if (pdev->dev.power.power_state.event == PM_EVENT_SUSPEND) {
Tejun Heo4447d352007-04-17 23:44:08 +09002645 rc = ahci_reset_controller(host);
Tejun Heoc1332872006-07-26 15:59:26 +09002646 if (rc)
2647 return rc;
2648
Tejun Heo4447d352007-04-17 23:44:08 +09002649 ahci_init_controller(host);
Tejun Heoc1332872006-07-26 15:59:26 +09002650 }
2651
Jeff Garzikcca39742006-08-24 03:19:22 -04002652 ata_host_resume(host);
Tejun Heoc1332872006-07-26 15:59:26 +09002653
2654 return 0;
2655}
Tejun Heo438ac6d2007-03-02 17:31:26 +09002656#endif
Tejun Heoc1332872006-07-26 15:59:26 +09002657
Tejun Heo254950c2006-07-26 15:59:25 +09002658static int ahci_port_start(struct ata_port *ap)
2659{
Shane Huangd6ef3152009-12-09 17:23:04 +08002660 struct ahci_host_priv *hpriv = ap->host->private_data;
Jeff Garzikcca39742006-08-24 03:19:22 -04002661 struct device *dev = ap->host->dev;
Tejun Heo254950c2006-07-26 15:59:25 +09002662 struct ahci_port_priv *pp;
Tejun Heo254950c2006-07-26 15:59:25 +09002663 void *mem;
2664 dma_addr_t mem_dma;
Shane Huangd6ef3152009-12-09 17:23:04 +08002665 size_t dma_sz, rx_fis_sz;
Tejun Heo254950c2006-07-26 15:59:25 +09002666
Tejun Heo24dc5f32007-01-20 16:00:28 +09002667 pp = devm_kzalloc(dev, sizeof(*pp), GFP_KERNEL);
Tejun Heo254950c2006-07-26 15:59:25 +09002668 if (!pp)
2669 return -ENOMEM;
Tejun Heo254950c2006-07-26 15:59:25 +09002670
Shane Huangd6ef3152009-12-09 17:23:04 +08002671 /* check FBS capability */
2672 if ((hpriv->cap & HOST_CAP_FBS) && sata_pmp_supported(ap)) {
2673 void __iomem *port_mmio = ahci_port_base(ap);
2674 u32 cmd = readl(port_mmio + PORT_CMD);
2675 if (cmd & PORT_CMD_FBSCP)
2676 pp->fbs_supported = true;
2677 else
2678 dev_printk(KERN_WARNING, dev,
2679 "The port is not capable of FBS\n");
2680 }
2681
2682 if (pp->fbs_supported) {
2683 dma_sz = AHCI_PORT_PRIV_FBS_DMA_SZ;
2684 rx_fis_sz = AHCI_RX_FIS_SZ * 16;
2685 } else {
2686 dma_sz = AHCI_PORT_PRIV_DMA_SZ;
2687 rx_fis_sz = AHCI_RX_FIS_SZ;
2688 }
2689
2690 mem = dmam_alloc_coherent(dev, dma_sz, &mem_dma, GFP_KERNEL);
Tejun Heo24dc5f32007-01-20 16:00:28 +09002691 if (!mem)
Tejun Heo254950c2006-07-26 15:59:25 +09002692 return -ENOMEM;
Shane Huangd6ef3152009-12-09 17:23:04 +08002693 memset(mem, 0, dma_sz);
Tejun Heo254950c2006-07-26 15:59:25 +09002694
2695 /*
2696 * First item in chunk of DMA memory: 32-slot command table,
2697 * 32 bytes each in size
2698 */
2699 pp->cmd_slot = mem;
2700 pp->cmd_slot_dma = mem_dma;
2701
2702 mem += AHCI_CMD_SLOT_SZ;
2703 mem_dma += AHCI_CMD_SLOT_SZ;
2704
2705 /*
2706 * Second item: Received-FIS area
2707 */
2708 pp->rx_fis = mem;
2709 pp->rx_fis_dma = mem_dma;
2710
Shane Huangd6ef3152009-12-09 17:23:04 +08002711 mem += rx_fis_sz;
2712 mem_dma += rx_fis_sz;
Tejun Heo254950c2006-07-26 15:59:25 +09002713
2714 /*
2715 * Third item: data area for storing a single command
2716 * and its scatter-gather table
2717 */
2718 pp->cmd_tbl = mem;
2719 pp->cmd_tbl_dma = mem_dma;
2720
Kristen Carlson Accardia7384922007-08-09 14:23:41 -07002721 /*
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002722 * Save off initial list of interrupts to be enabled.
2723 * This could be changed later
2724 */
Kristen Carlson Accardia7384922007-08-09 14:23:41 -07002725 pp->intr_mask = DEF_PORT_IRQ;
2726
Tejun Heo254950c2006-07-26 15:59:25 +09002727 ap->private_data = pp;
2728
Jeff Garzikdf69c9c2007-05-26 20:46:51 -04002729 /* engage engines, captain */
2730 return ahci_port_resume(ap);
Tejun Heo254950c2006-07-26 15:59:25 +09002731}
2732
2733static void ahci_port_stop(struct ata_port *ap)
2734{
Tejun Heo0be0aa92006-07-26 15:59:26 +09002735 const char *emsg = NULL;
2736 int rc;
Tejun Heo254950c2006-07-26 15:59:25 +09002737
Tejun Heo0be0aa92006-07-26 15:59:26 +09002738 /* de-initialize port */
Tejun Heo4447d352007-04-17 23:44:08 +09002739 rc = ahci_deinit_port(ap, &emsg);
Tejun Heo0be0aa92006-07-26 15:59:26 +09002740 if (rc)
2741 ata_port_printk(ap, KERN_WARNING, "%s (%d)\n", emsg, rc);
Tejun Heo254950c2006-07-26 15:59:25 +09002742}
2743
Tejun Heo4447d352007-04-17 23:44:08 +09002744static int ahci_configure_dma_masks(struct pci_dev *pdev, int using_dac)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002745{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 int rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002747
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 if (using_dac &&
Yang Hongyang6a355282009-04-06 19:01:13 -07002749 !pci_set_dma_mask(pdev, DMA_BIT_MASK(64))) {
2750 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(64));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751 if (rc) {
Yang Hongyang284901a2009-04-06 19:01:15 -07002752 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002753 if (rc) {
Jeff Garzika9524a72005-10-30 14:39:11 -05002754 dev_printk(KERN_ERR, &pdev->dev,
2755 "64-bit DMA enable failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756 return rc;
2757 }
2758 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002759 } else {
Yang Hongyang284901a2009-04-06 19:01:15 -07002760 rc = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002761 if (rc) {
Jeff Garzika9524a72005-10-30 14:39:11 -05002762 dev_printk(KERN_ERR, &pdev->dev,
2763 "32-bit DMA enable failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002764 return rc;
2765 }
Yang Hongyang284901a2009-04-06 19:01:15 -07002766 rc = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 if (rc) {
Jeff Garzika9524a72005-10-30 14:39:11 -05002768 dev_printk(KERN_ERR, &pdev->dev,
2769 "32-bit consistent DMA enable failed\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770 return rc;
2771 }
2772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 return 0;
2774}
2775
Tejun Heo4447d352007-04-17 23:44:08 +09002776static void ahci_print_info(struct ata_host *host)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777{
Tejun Heo4447d352007-04-17 23:44:08 +09002778 struct ahci_host_priv *hpriv = host->private_data;
2779 struct pci_dev *pdev = to_pci_dev(host->dev);
2780 void __iomem *mmio = host->iomap[AHCI_PCI_BAR];
Robert Hancock4c521c82009-09-20 17:02:31 -06002781 u32 vers, cap, cap2, impl, speed;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002782 const char *speed_s;
2783 u16 cc;
2784 const char *scc_s;
2785
2786 vers = readl(mmio + HOST_VERSION);
2787 cap = hpriv->cap;
Robert Hancock4c521c82009-09-20 17:02:31 -06002788 cap2 = hpriv->cap2;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002789 impl = hpriv->port_map;
2790
2791 speed = (cap >> 20) & 0xf;
2792 if (speed == 1)
2793 speed_s = "1.5";
2794 else if (speed == 2)
2795 speed_s = "3";
Shane Huang8522ee22008-12-30 11:00:37 +08002796 else if (speed == 3)
2797 speed_s = "6";
Linus Torvalds1da177e2005-04-16 15:20:36 -07002798 else
2799 speed_s = "?";
2800
2801 pci_read_config_word(pdev, 0x0a, &cc);
Conke Huc9f89472007-01-09 05:32:51 -05002802 if (cc == PCI_CLASS_STORAGE_IDE)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803 scc_s = "IDE";
Conke Huc9f89472007-01-09 05:32:51 -05002804 else if (cc == PCI_CLASS_STORAGE_SATA)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002805 scc_s = "SATA";
Conke Huc9f89472007-01-09 05:32:51 -05002806 else if (cc == PCI_CLASS_STORAGE_RAID)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002807 scc_s = "RAID";
2808 else
2809 scc_s = "unknown";
2810
Jeff Garzika9524a72005-10-30 14:39:11 -05002811 dev_printk(KERN_INFO, &pdev->dev,
2812 "AHCI %02x%02x.%02x%02x "
Linus Torvalds1da177e2005-04-16 15:20:36 -07002813 "%u slots %u ports %s Gbps 0x%x impl %s mode\n"
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002814 ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002815
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002816 (vers >> 24) & 0xff,
2817 (vers >> 16) & 0xff,
2818 (vers >> 8) & 0xff,
2819 vers & 0xff,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002820
2821 ((cap >> 8) & 0x1f) + 1,
2822 (cap & 0x1f) + 1,
2823 speed_s,
2824 impl,
2825 scc_s);
2826
Jeff Garzika9524a72005-10-30 14:39:11 -05002827 dev_printk(KERN_INFO, &pdev->dev,
2828 "flags: "
Tejun Heo203ef6c2007-07-16 14:29:40 +09002829 "%s%s%s%s%s%s%s"
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -07002830 "%s%s%s%s%s%s%s"
Robert Hancock4c521c82009-09-20 17:02:31 -06002831 "%s%s%s%s%s%s\n"
Jeff Garzik2dcb4072007-10-19 06:42:56 -04002832 ,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002833
Robert Hancock4c521c82009-09-20 17:02:31 -06002834 cap & HOST_CAP_64 ? "64bit " : "",
2835 cap & HOST_CAP_NCQ ? "ncq " : "",
2836 cap & HOST_CAP_SNTF ? "sntf " : "",
2837 cap & HOST_CAP_MPS ? "ilck " : "",
2838 cap & HOST_CAP_SSS ? "stag " : "",
2839 cap & HOST_CAP_ALPM ? "pm " : "",
2840 cap & HOST_CAP_LED ? "led " : "",
2841 cap & HOST_CAP_CLO ? "clo " : "",
2842 cap & HOST_CAP_ONLY ? "only " : "",
2843 cap & HOST_CAP_PMP ? "pmp " : "",
2844 cap & HOST_CAP_FBS ? "fbs " : "",
2845 cap & HOST_CAP_PIO_MULTI ? "pio " : "",
2846 cap & HOST_CAP_SSC ? "slum " : "",
2847 cap & HOST_CAP_PART ? "part " : "",
2848 cap & HOST_CAP_CCC ? "ccc " : "",
2849 cap & HOST_CAP_EMS ? "ems " : "",
2850 cap & HOST_CAP_SXS ? "sxs " : "",
2851 cap2 & HOST_CAP2_APST ? "apst " : "",
2852 cap2 & HOST_CAP2_NVMHCI ? "nvmp " : "",
2853 cap2 & HOST_CAP2_BOH ? "boh " : ""
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 );
2855}
2856
Tejun Heoedc93052007-10-25 14:59:16 +09002857/* On ASUS P5W DH Deluxe, the second port of PCI device 00:1f.2 is
2858 * hardwired to on-board SIMG 4726. The chipset is ICH8 and doesn't
2859 * support PMP and the 4726 either directly exports the device
2860 * attached to the first downstream port or acts as a hardware storage
2861 * controller and emulate a single ATA device (can be RAID 0/1 or some
2862 * other configuration).
2863 *
2864 * When there's no device attached to the first downstream port of the
2865 * 4726, "Config Disk" appears, which is a pseudo ATA device to
2866 * configure the 4726. However, ATA emulation of the device is very
2867 * lame. It doesn't send signature D2H Reg FIS after the initial
2868 * hardreset, pukes on SRST w/ PMP==0 and has bunch of other issues.
2869 *
2870 * The following function works around the problem by always using
2871 * hardreset on the port and not depending on receiving signature FIS
2872 * afterward. If signature FIS isn't received soon, ATA class is
2873 * assumed without follow-up softreset.
2874 */
2875static void ahci_p5wdh_workaround(struct ata_host *host)
2876{
2877 static struct dmi_system_id sysids[] = {
2878 {
2879 .ident = "P5W DH Deluxe",
2880 .matches = {
2881 DMI_MATCH(DMI_SYS_VENDOR,
2882 "ASUSTEK COMPUTER INC"),
2883 DMI_MATCH(DMI_PRODUCT_NAME, "P5W DH Deluxe"),
2884 },
2885 },
2886 { }
2887 };
2888 struct pci_dev *pdev = to_pci_dev(host->dev);
2889
2890 if (pdev->bus->number == 0 && pdev->devfn == PCI_DEVFN(0x1f, 2) &&
2891 dmi_check_system(sysids)) {
2892 struct ata_port *ap = host->ports[1];
2893
2894 dev_printk(KERN_INFO, &pdev->dev, "enabling ASUS P5W DH "
2895 "Deluxe on-board SIMG4726 workaround\n");
2896
2897 ap->ops = &ahci_p5wdh_ops;
2898 ap->link.flags |= ATA_LFLAG_NO_SRST | ATA_LFLAG_ASSUME_ATA;
2899 }
2900}
2901
Tejun Heo2fcad9d2009-10-03 18:27:29 +09002902/* only some SB600 ahci controllers can do 64bit DMA */
2903static bool ahci_sb600_enable_64bit(struct pci_dev *pdev)
Shane Huang58a09b32009-05-27 15:04:43 +08002904{
2905 static const struct dmi_system_id sysids[] = {
Tejun Heo03d783b2009-08-16 21:04:02 +09002906 /*
2907 * The oldest version known to be broken is 0901 and
2908 * working is 1501 which was released on 2007-10-26.
Tejun Heo2fcad9d2009-10-03 18:27:29 +09002909 * Enable 64bit DMA on 1501 and anything newer.
2910 *
Tejun Heo03d783b2009-08-16 21:04:02 +09002911 * Please read bko#9412 for more info.
2912 */
Shane Huang58a09b32009-05-27 15:04:43 +08002913 {
2914 .ident = "ASUS M2A-VM",
2915 .matches = {
2916 DMI_MATCH(DMI_BOARD_VENDOR,
2917 "ASUSTeK Computer INC."),
2918 DMI_MATCH(DMI_BOARD_NAME, "M2A-VM"),
2919 },
Tejun Heo03d783b2009-08-16 21:04:02 +09002920 .driver_data = "20071026", /* yyyymmdd */
Shane Huang58a09b32009-05-27 15:04:43 +08002921 },
Mark Nelsone65cc192009-11-03 20:06:48 +11002922 /*
2923 * All BIOS versions for the MSI K9A2 Platinum (MS-7376)
2924 * support 64bit DMA.
2925 *
2926 * BIOS versions earlier than 1.5 had the Manufacturer DMI
2927 * fields as "MICRO-STAR INTERANTIONAL CO.,LTD".
2928 * This spelling mistake was fixed in BIOS version 1.5, so
2929 * 1.5 and later have the Manufacturer as
2930 * "MICRO-STAR INTERNATIONAL CO.,LTD".
2931 * So try to match on DMI_BOARD_VENDOR of "MICRO-STAR INTER".
2932 *
2933 * BIOS versions earlier than 1.9 had a Board Product Name
2934 * DMI field of "MS-7376". This was changed to be
2935 * "K9A2 Platinum (MS-7376)" in version 1.9, but we can still
2936 * match on DMI_BOARD_NAME of "MS-7376".
2937 */
2938 {
2939 .ident = "MSI K9A2 Platinum",
2940 .matches = {
2941 DMI_MATCH(DMI_BOARD_VENDOR,
2942 "MICRO-STAR INTER"),
2943 DMI_MATCH(DMI_BOARD_NAME, "MS-7376"),
2944 },
2945 },
Shane Huang58a09b32009-05-27 15:04:43 +08002946 { }
2947 };
Tejun Heo03d783b2009-08-16 21:04:02 +09002948 const struct dmi_system_id *match;
Tejun Heo2fcad9d2009-10-03 18:27:29 +09002949 int year, month, date;
2950 char buf[9];
Shane Huang58a09b32009-05-27 15:04:43 +08002951
Tejun Heo03d783b2009-08-16 21:04:02 +09002952 match = dmi_first_match(sysids);
Shane Huang58a09b32009-05-27 15:04:43 +08002953 if (pdev->bus->number != 0 || pdev->devfn != PCI_DEVFN(0x12, 0) ||
Tejun Heo03d783b2009-08-16 21:04:02 +09002954 !match)
Shane Huang58a09b32009-05-27 15:04:43 +08002955 return false;
2956
Mark Nelsone65cc192009-11-03 20:06:48 +11002957 if (!match->driver_data)
2958 goto enable_64bit;
2959
Tejun Heo2fcad9d2009-10-03 18:27:29 +09002960 dmi_get_date(DMI_BIOS_DATE, &year, &month, &date);
2961 snprintf(buf, sizeof(buf), "%04d%02d%02d", year, month, date);
Shane Huang58a09b32009-05-27 15:04:43 +08002962
Mark Nelsone65cc192009-11-03 20:06:48 +11002963 if (strcmp(buf, match->driver_data) >= 0)
2964 goto enable_64bit;
2965 else {
Tejun Heo03d783b2009-08-16 21:04:02 +09002966 dev_printk(KERN_WARNING, &pdev->dev, "%s: BIOS too old, "
2967 "forcing 32bit DMA, update BIOS\n", match->ident);
Tejun Heo2fcad9d2009-10-03 18:27:29 +09002968 return false;
2969 }
Mark Nelsone65cc192009-11-03 20:06:48 +11002970
2971enable_64bit:
2972 dev_printk(KERN_WARNING, &pdev->dev, "%s: enabling 64bit DMA\n",
2973 match->ident);
2974 return true;
Shane Huang58a09b32009-05-27 15:04:43 +08002975}
2976
Rafael J. Wysocki1fd68432009-01-19 20:57:36 +01002977static bool ahci_broken_system_poweroff(struct pci_dev *pdev)
2978{
2979 static const struct dmi_system_id broken_systems[] = {
2980 {
2981 .ident = "HP Compaq nx6310",
2982 .matches = {
2983 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
2984 DMI_MATCH(DMI_PRODUCT_NAME, "HP Compaq nx6310"),
2985 },
2986 /* PCI slot number of the controller */
2987 .driver_data = (void *)0x1FUL,
2988 },
Maciej Ruteckid2f9c062009-03-20 00:06:46 +01002989 {
2990 .ident = "HP Compaq 6720s",
2991 .matches = {
2992 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
2993 DMI_MATCH(DMI_PRODUCT_NAME, "HP Compaq 6720s"),
2994 },
2995 /* PCI slot number of the controller */
2996 .driver_data = (void *)0x1FUL,
2997 },
Rafael J. Wysocki1fd68432009-01-19 20:57:36 +01002998
2999 { } /* terminate list */
3000 };
3001 const struct dmi_system_id *dmi = dmi_first_match(broken_systems);
3002
3003 if (dmi) {
3004 unsigned long slot = (unsigned long)dmi->driver_data;
3005 /* apply the quirk only to on-board controllers */
3006 return slot == PCI_SLOT(pdev->devfn);
3007 }
3008
3009 return false;
3010}
3011
Tejun Heo9b10ae82009-05-30 20:50:12 +09003012static bool ahci_broken_suspend(struct pci_dev *pdev)
3013{
3014 static const struct dmi_system_id sysids[] = {
3015 /*
3016 * On HP dv[4-6] and HDX18 with earlier BIOSen, link
3017 * to the harddisk doesn't become online after
3018 * resuming from STR. Warn and fail suspend.
3019 */
3020 {
3021 .ident = "dv4",
3022 .matches = {
3023 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
3024 DMI_MATCH(DMI_PRODUCT_NAME,
3025 "HP Pavilion dv4 Notebook PC"),
3026 },
3027 .driver_data = "F.30", /* cutoff BIOS version */
3028 },
3029 {
3030 .ident = "dv5",
3031 .matches = {
3032 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
3033 DMI_MATCH(DMI_PRODUCT_NAME,
3034 "HP Pavilion dv5 Notebook PC"),
3035 },
3036 .driver_data = "F.16", /* cutoff BIOS version */
3037 },
3038 {
3039 .ident = "dv6",
3040 .matches = {
3041 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
3042 DMI_MATCH(DMI_PRODUCT_NAME,
3043 "HP Pavilion dv6 Notebook PC"),
3044 },
3045 .driver_data = "F.21", /* cutoff BIOS version */
3046 },
3047 {
3048 .ident = "HDX18",
3049 .matches = {
3050 DMI_MATCH(DMI_SYS_VENDOR, "Hewlett-Packard"),
3051 DMI_MATCH(DMI_PRODUCT_NAME,
3052 "HP HDX18 Notebook PC"),
3053 },
3054 .driver_data = "F.23", /* cutoff BIOS version */
3055 },
Tejun Heocedc9bf2010-01-28 16:04:15 +09003056 /*
3057 * Acer eMachines G725 has the same problem. BIOS
3058 * V1.03 is known to be broken. V3.04 is known to
3059 * work. Inbetween, there are V1.06, V2.06 and V3.03
3060 * that we don't have much idea about. For now,
3061 * blacklist anything older than V3.04.
3062 */
3063 {
3064 .ident = "G725",
3065 .matches = {
3066 DMI_MATCH(DMI_SYS_VENDOR, "eMachines"),
3067 DMI_MATCH(DMI_PRODUCT_NAME, "eMachines G725"),
3068 },
3069 .driver_data = "V3.04", /* cutoff BIOS version */
3070 },
Tejun Heo9b10ae82009-05-30 20:50:12 +09003071 { } /* terminate list */
3072 };
3073 const struct dmi_system_id *dmi = dmi_first_match(sysids);
3074 const char *ver;
3075
3076 if (!dmi || pdev->bus->number || pdev->devfn != PCI_DEVFN(0x1f, 2))
3077 return false;
3078
3079 ver = dmi_get_system_info(DMI_BIOS_VERSION);
3080
3081 return !ver || strcmp(ver, dmi->driver_data) < 0;
3082}
3083
Tejun Heo55946392009-08-04 14:30:08 +09003084static bool ahci_broken_online(struct pci_dev *pdev)
3085{
3086#define ENCODE_BUSDEVFN(bus, slot, func) \
3087 (void *)(unsigned long)(((bus) << 8) | PCI_DEVFN((slot), (func)))
3088 static const struct dmi_system_id sysids[] = {
3089 /*
3090 * There are several gigabyte boards which use
3091 * SIMG5723s configured as hardware RAID. Certain
3092 * 5723 firmware revisions shipped there keep the link
3093 * online but fail to answer properly to SRST or
3094 * IDENTIFY when no device is attached downstream
3095 * causing libata to retry quite a few times leading
3096 * to excessive detection delay.
3097 *
3098 * As these firmwares respond to the second reset try
3099 * with invalid device signature, considering unknown
3100 * sig as offline works around the problem acceptably.
3101 */
3102 {
3103 .ident = "EP45-DQ6",
3104 .matches = {
3105 DMI_MATCH(DMI_BOARD_VENDOR,
3106 "Gigabyte Technology Co., Ltd."),
3107 DMI_MATCH(DMI_BOARD_NAME, "EP45-DQ6"),
3108 },
3109 .driver_data = ENCODE_BUSDEVFN(0x0a, 0x00, 0),
3110 },
3111 {
3112 .ident = "EP45-DS5",
3113 .matches = {
3114 DMI_MATCH(DMI_BOARD_VENDOR,
3115 "Gigabyte Technology Co., Ltd."),
3116 DMI_MATCH(DMI_BOARD_NAME, "EP45-DS5"),
3117 },
3118 .driver_data = ENCODE_BUSDEVFN(0x03, 0x00, 0),
3119 },
3120 { } /* terminate list */
3121 };
3122#undef ENCODE_BUSDEVFN
3123 const struct dmi_system_id *dmi = dmi_first_match(sysids);
3124 unsigned int val;
3125
3126 if (!dmi)
3127 return false;
3128
3129 val = (unsigned long)dmi->driver_data;
3130
3131 return pdev->bus->number == (val >> 8) && pdev->devfn == (val & 0xff);
3132}
3133
Markus Trippelsdorf8e513212009-10-09 05:41:47 +02003134#ifdef CONFIG_ATA_ACPI
Tejun Heof80ae7e2009-09-16 04:18:03 +09003135static void ahci_gtf_filter_workaround(struct ata_host *host)
3136{
3137 static const struct dmi_system_id sysids[] = {
3138 /*
3139 * Aspire 3810T issues a bunch of SATA enable commands
3140 * via _GTF including an invalid one and one which is
3141 * rejected by the device. Among the successful ones
3142 * is FPDMA non-zero offset enable which when enabled
3143 * only on the drive side leads to NCQ command
3144 * failures. Filter it out.
3145 */
3146 {
3147 .ident = "Aspire 3810T",
3148 .matches = {
3149 DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
3150 DMI_MATCH(DMI_PRODUCT_NAME, "Aspire 3810T"),
3151 },
3152 .driver_data = (void *)ATA_ACPI_FILTER_FPDMA_OFFSET,
3153 },
3154 { }
3155 };
3156 const struct dmi_system_id *dmi = dmi_first_match(sysids);
3157 unsigned int filter;
3158 int i;
3159
3160 if (!dmi)
3161 return;
3162
3163 filter = (unsigned long)dmi->driver_data;
3164 dev_printk(KERN_INFO, host->dev,
3165 "applying extra ACPI _GTF filter 0x%x for %s\n",
3166 filter, dmi->ident);
3167
3168 for (i = 0; i < host->n_ports; i++) {
3169 struct ata_port *ap = host->ports[i];
3170 struct ata_link *link;
3171 struct ata_device *dev;
3172
3173 ata_for_each_link(link, ap, EDGE)
3174 ata_for_each_dev(dev, link, ALL)
3175 dev->gtf_filter |= filter;
3176 }
3177}
Markus Trippelsdorf8e513212009-10-09 05:41:47 +02003178#else
3179static inline void ahci_gtf_filter_workaround(struct ata_host *host)
3180{}
3181#endif
Tejun Heof80ae7e2009-09-16 04:18:03 +09003182
Tejun Heo24dc5f32007-01-20 16:00:28 +09003183static int ahci_init_one(struct pci_dev *pdev, const struct pci_device_id *ent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184{
3185 static int printed_version;
Tejun Heoe297d992008-06-10 00:13:04 +09003186 unsigned int board_id = ent->driver_data;
3187 struct ata_port_info pi = ahci_port_info[board_id];
Tejun Heo4447d352007-04-17 23:44:08 +09003188 const struct ata_port_info *ppi[] = { &pi, NULL };
Tejun Heo24dc5f32007-01-20 16:00:28 +09003189 struct device *dev = &pdev->dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003190 struct ahci_host_priv *hpriv;
Tejun Heo4447d352007-04-17 23:44:08 +09003191 struct ata_host *host;
Tejun Heo837f5f82008-02-06 15:13:51 +09003192 int n_ports, i, rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193
3194 VPRINTK("ENTER\n");
3195
Tejun Heo12fad3f2006-05-15 21:03:55 +09003196 WARN_ON(ATA_MAX_QUEUE > AHCI_MAX_CMDS);
3197
Linus Torvalds1da177e2005-04-16 15:20:36 -07003198 if (!printed_version++)
Jeff Garzika9524a72005-10-30 14:39:11 -05003199 dev_printk(KERN_DEBUG, &pdev->dev, "version " DRV_VERSION "\n");
Linus Torvalds1da177e2005-04-16 15:20:36 -07003200
Alan Cox5b66c822008-09-03 14:48:34 +01003201 /* The AHCI driver can only drive the SATA ports, the PATA driver
3202 can drive them all so if both drivers are selected make sure
3203 AHCI stays out of the way */
3204 if (pdev->vendor == PCI_VENDOR_ID_MARVELL && !marvell_enable)
3205 return -ENODEV;
3206
Mark Nelson7a022672009-11-22 12:07:41 +11003207 /* Promise's PDC42819 is a SAS/SATA controller that has an AHCI mode.
3208 * At the moment, we can only use the AHCI mode. Let the users know
3209 * that for SAS drives they're out of luck.
3210 */
3211 if (pdev->vendor == PCI_VENDOR_ID_PROMISE)
3212 dev_printk(KERN_INFO, &pdev->dev, "PDC42819 "
3213 "can only drive SATA devices with this driver\n");
3214
Tejun Heo4447d352007-04-17 23:44:08 +09003215 /* acquire resources */
Tejun Heo24dc5f32007-01-20 16:00:28 +09003216 rc = pcim_enable_device(pdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 if (rc)
3218 return rc;
3219
Tejun Heodea55132008-03-11 19:52:31 +09003220 /* AHCI controllers often implement SFF compatible interface.
3221 * Grab all PCI BARs just in case.
3222 */
3223 rc = pcim_iomap_regions_request_all(pdev, 1 << AHCI_PCI_BAR, DRV_NAME);
Tejun Heo0d5ff562007-02-01 15:06:36 +09003224 if (rc == -EBUSY)
Tejun Heo24dc5f32007-01-20 16:00:28 +09003225 pcim_pin_device(pdev);
Tejun Heo0d5ff562007-02-01 15:06:36 +09003226 if (rc)
Tejun Heo24dc5f32007-01-20 16:00:28 +09003227 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003228
Tejun Heoc4f77922007-12-06 15:09:43 +09003229 if (pdev->vendor == PCI_VENDOR_ID_INTEL &&
3230 (pdev->device == 0x2652 || pdev->device == 0x2653)) {
3231 u8 map;
3232
3233 /* ICH6s share the same PCI ID for both piix and ahci
3234 * modes. Enabling ahci mode while MAP indicates
3235 * combined mode is a bad idea. Yield to ata_piix.
3236 */
3237 pci_read_config_byte(pdev, ICH_MAP, &map);
3238 if (map & 0x3) {
3239 dev_printk(KERN_INFO, &pdev->dev, "controller is in "
3240 "combined mode, can't enable AHCI mode\n");
3241 return -ENODEV;
3242 }
3243 }
3244
Tejun Heo24dc5f32007-01-20 16:00:28 +09003245 hpriv = devm_kzalloc(dev, sizeof(*hpriv), GFP_KERNEL);
3246 if (!hpriv)
3247 return -ENOMEM;
Tejun Heo417a1a62007-09-23 13:19:55 +09003248 hpriv->flags |= (unsigned long)pi.private_data;
3249
Tejun Heoe297d992008-06-10 00:13:04 +09003250 /* MCP65 revision A1 and A2 can't do MSI */
3251 if (board_id == board_ahci_mcp65 &&
3252 (pdev->revision == 0xa1 || pdev->revision == 0xa2))
3253 hpriv->flags |= AHCI_HFLAG_NO_MSI;
3254
Shane Huange427fe02008-12-30 10:53:41 +08003255 /* SB800 does NOT need the workaround to ignore SERR_INTERNAL */
3256 if (board_id == board_ahci_sb700 && pdev->revision >= 0x40)
3257 hpriv->flags &= ~AHCI_HFLAG_IGN_SERR_INTERNAL;
3258
Tejun Heo2fcad9d2009-10-03 18:27:29 +09003259 /* only some SB600s can do 64bit DMA */
3260 if (ahci_sb600_enable_64bit(pdev))
3261 hpriv->flags &= ~AHCI_HFLAG_32BIT_ONLY;
Shane Huang58a09b32009-05-27 15:04:43 +08003262
Tejun Heo31b239a2009-09-17 00:34:39 +09003263 if ((hpriv->flags & AHCI_HFLAG_NO_MSI) || pci_enable_msi(pdev))
3264 pci_intx(pdev, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265
Tejun Heo4447d352007-04-17 23:44:08 +09003266 /* save initial config */
Tejun Heo417a1a62007-09-23 13:19:55 +09003267 ahci_save_initial_config(pdev, hpriv);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268
Tejun Heo4447d352007-04-17 23:44:08 +09003269 /* prepare host */
Robert Hancock453d3132010-01-26 22:33:23 -06003270 if (hpriv->cap & HOST_CAP_NCQ) {
3271 pi.flags |= ATA_FLAG_NCQ;
3272 /* Auto-activate optimization is supposed to be supported on
3273 all AHCI controllers indicating NCQ support, but it seems
3274 to be broken at least on some NVIDIA MCP79 chipsets.
3275 Until we get info on which NVIDIA chipsets don't have this
3276 issue, if any, disable AA on all NVIDIA AHCIs. */
3277 if (pdev->vendor != PCI_VENDOR_ID_NVIDIA)
3278 pi.flags |= ATA_FLAG_FPDMA_AA;
3279 }
Tejun Heo4447d352007-04-17 23:44:08 +09003280
Tejun Heo7d50b602007-09-23 13:19:54 +09003281 if (hpriv->cap & HOST_CAP_PMP)
3282 pi.flags |= ATA_FLAG_PMP;
3283
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -07003284 if (ahci_em_messages && (hpriv->cap & HOST_CAP_EMS)) {
3285 u8 messages;
3286 void __iomem *mmio = pcim_iomap_table(pdev)[AHCI_PCI_BAR];
3287 u32 em_loc = readl(mmio + HOST_EM_LOC);
3288 u32 em_ctl = readl(mmio + HOST_EM_CTL);
3289
David Milburn87943ac2008-10-13 14:38:36 -05003290 messages = (em_ctl & EM_CTRL_MSG_TYPE) >> 16;
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -07003291
3292 /* we only support LED message type right now */
3293 if ((messages & 0x01) && (ahci_em_messages == 1)) {
3294 /* store em_loc */
3295 hpriv->em_loc = ((em_loc >> 16) * 4);
3296 pi.flags |= ATA_FLAG_EM;
3297 if (!(em_ctl & EM_CTL_ALHD))
3298 pi.flags |= ATA_FLAG_SW_ACTIVITY;
3299 }
3300 }
3301
Rafael J. Wysocki1fd68432009-01-19 20:57:36 +01003302 if (ahci_broken_system_poweroff(pdev)) {
3303 pi.flags |= ATA_FLAG_NO_POWEROFF_SPINDOWN;
3304 dev_info(&pdev->dev,
3305 "quirky BIOS, skipping spindown on poweroff\n");
3306 }
3307
Tejun Heo9b10ae82009-05-30 20:50:12 +09003308 if (ahci_broken_suspend(pdev)) {
3309 hpriv->flags |= AHCI_HFLAG_NO_SUSPEND;
3310 dev_printk(KERN_WARNING, &pdev->dev,
3311 "BIOS update required for suspend/resume\n");
3312 }
3313
Tejun Heo55946392009-08-04 14:30:08 +09003314 if (ahci_broken_online(pdev)) {
3315 hpriv->flags |= AHCI_HFLAG_SRST_TOUT_IS_OFFLINE;
3316 dev_info(&pdev->dev,
3317 "online status unreliable, applying workaround\n");
3318 }
3319
Tejun Heo837f5f82008-02-06 15:13:51 +09003320 /* CAP.NP sometimes indicate the index of the last enabled
3321 * port, at other times, that of the last possible port, so
3322 * determining the maximum port number requires looking at
3323 * both CAP.NP and port_map.
3324 */
3325 n_ports = max(ahci_nr_ports(hpriv->cap), fls(hpriv->port_map));
3326
3327 host = ata_host_alloc_pinfo(&pdev->dev, ppi, n_ports);
Tejun Heo4447d352007-04-17 23:44:08 +09003328 if (!host)
3329 return -ENOMEM;
3330 host->iomap = pcim_iomap_table(pdev);
3331 host->private_data = hpriv;
3332
Arjan van de Venf3d7f232009-01-26 02:05:44 -08003333 if (!(hpriv->cap & HOST_CAP_SSS) || ahci_ignore_sss)
Arjan van de Ven886ad092009-01-09 15:54:07 -08003334 host->flags |= ATA_HOST_PARALLEL_SCAN;
Arjan van de Venf3d7f232009-01-26 02:05:44 -08003335 else
3336 printk(KERN_INFO "ahci: SSS flag set, parallel bus scan disabled\n");
Arjan van de Ven886ad092009-01-09 15:54:07 -08003337
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -07003338 if (pi.flags & ATA_FLAG_EM)
3339 ahci_reset_em(host);
3340
Tejun Heo4447d352007-04-17 23:44:08 +09003341 for (i = 0; i < host->n_ports; i++) {
Jeff Garzikdab632e2007-05-28 08:33:01 -04003342 struct ata_port *ap = host->ports[i];
Tejun Heo4447d352007-04-17 23:44:08 +09003343
Tejun Heocbcdd872007-08-18 13:14:55 +09003344 ata_port_pbar_desc(ap, AHCI_PCI_BAR, -1, "abar");
3345 ata_port_pbar_desc(ap, AHCI_PCI_BAR,
3346 0x100 + ap->port_no * 0x80, "port");
3347
Kristen Carlson Accardi31556592007-10-25 01:33:26 -04003348 /* set initial link pm policy */
3349 ap->pm_policy = NOT_AVAILABLE;
3350
Kristen Carlson Accardi18f7ba42008-06-03 10:33:55 -07003351 /* set enclosure management message type */
3352 if (ap->flags & ATA_FLAG_EM)
3353 ap->em_message_type = ahci_em_messages;
3354
3355
Jeff Garzikdab632e2007-05-28 08:33:01 -04003356 /* disabled/not-implemented port */
Tejun Heo350756f2008-04-07 22:47:21 +09003357 if (!(hpriv->port_map & (1 << i)))
Jeff Garzikdab632e2007-05-28 08:33:01 -04003358 ap->ops = &ata_dummy_port_ops;
Tejun Heo4447d352007-04-17 23:44:08 +09003359 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003360
Tejun Heoedc93052007-10-25 14:59:16 +09003361 /* apply workaround for ASUS P5W DH Deluxe mainboard */
3362 ahci_p5wdh_workaround(host);
3363
Tejun Heof80ae7e2009-09-16 04:18:03 +09003364 /* apply gtf filter quirk */
3365 ahci_gtf_filter_workaround(host);
3366
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 /* initialize adapter */
Tejun Heo4447d352007-04-17 23:44:08 +09003368 rc = ahci_configure_dma_masks(pdev, hpriv->cap & HOST_CAP_64);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 if (rc)
Tejun Heo24dc5f32007-01-20 16:00:28 +09003370 return rc;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371
Tejun Heo4447d352007-04-17 23:44:08 +09003372 rc = ahci_reset_controller(host);
3373 if (rc)
3374 return rc;
Tejun Heo12fad3f2006-05-15 21:03:55 +09003375
Tejun Heo4447d352007-04-17 23:44:08 +09003376 ahci_init_controller(host);
3377 ahci_print_info(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003378
Tejun Heo4447d352007-04-17 23:44:08 +09003379 pci_set_master(pdev);
3380 return ata_host_activate(host, pdev->irq, ahci_interrupt, IRQF_SHARED,
3381 &ahci_sht);
Jeff Garzik907f4672005-05-12 15:03:42 -04003382}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383
3384static int __init ahci_init(void)
3385{
Pavel Roskinb7887192006-08-10 18:13:18 +09003386 return pci_register_driver(&ahci_pci_driver);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387}
3388
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389static void __exit ahci_exit(void)
3390{
3391 pci_unregister_driver(&ahci_pci_driver);
3392}
3393
3394
3395MODULE_AUTHOR("Jeff Garzik");
3396MODULE_DESCRIPTION("AHCI SATA low-level driver");
3397MODULE_LICENSE("GPL");
3398MODULE_DEVICE_TABLE(pci, ahci_pci_tbl);
Jeff Garzik68854332005-08-23 02:53:51 -04003399MODULE_VERSION(DRV_VERSION);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003400
3401module_init(ahci_init);
3402module_exit(ahci_exit);