xref: /openbmc/linux/drivers/ata/libata-core.c (revision 6a87e42e)
1c6fd2807SJeff Garzik /*
2c6fd2807SJeff Garzik  *  libata-core.c - helper library for ATA
3c6fd2807SJeff Garzik  *
4c6fd2807SJeff Garzik  *  Maintained by:  Jeff Garzik <jgarzik@pobox.com>
5c6fd2807SJeff Garzik  *    		    Please ALWAYS copy linux-ide@vger.kernel.org
6c6fd2807SJeff Garzik  *		    on emails.
7c6fd2807SJeff Garzik  *
8c6fd2807SJeff Garzik  *  Copyright 2003-2004 Red Hat, Inc.  All rights reserved.
9c6fd2807SJeff Garzik  *  Copyright 2003-2004 Jeff Garzik
10c6fd2807SJeff Garzik  *
11c6fd2807SJeff Garzik  *
12c6fd2807SJeff Garzik  *  This program is free software; you can redistribute it and/or modify
13c6fd2807SJeff Garzik  *  it under the terms of the GNU General Public License as published by
14c6fd2807SJeff Garzik  *  the Free Software Foundation; either version 2, or (at your option)
15c6fd2807SJeff Garzik  *  any later version.
16c6fd2807SJeff Garzik  *
17c6fd2807SJeff Garzik  *  This program is distributed in the hope that it will be useful,
18c6fd2807SJeff Garzik  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19c6fd2807SJeff Garzik  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20c6fd2807SJeff Garzik  *  GNU General Public License for more details.
21c6fd2807SJeff Garzik  *
22c6fd2807SJeff Garzik  *  You should have received a copy of the GNU General Public License
23c6fd2807SJeff Garzik  *  along with this program; see the file COPYING.  If not, write to
24c6fd2807SJeff Garzik  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25c6fd2807SJeff Garzik  *
26c6fd2807SJeff Garzik  *
27c6fd2807SJeff Garzik  *  libata documentation is available via 'make {ps|pdf}docs',
28c6fd2807SJeff Garzik  *  as Documentation/DocBook/libata.*
29c6fd2807SJeff Garzik  *
30c6fd2807SJeff Garzik  *  Hardware documentation available from http://www.t13.org/ and
31c6fd2807SJeff Garzik  *  http://www.sata-io.org/
32c6fd2807SJeff Garzik  *
3392c52c52SAlan Cox  *  Standards documents from:
3492c52c52SAlan Cox  *	http://www.t13.org (ATA standards, PCI DMA IDE spec)
3592c52c52SAlan Cox  *	http://www.t10.org (SCSI MMC - for ATAPI MMC)
3692c52c52SAlan Cox  *	http://www.sata-io.org (SATA)
3792c52c52SAlan Cox  *	http://www.compactflash.org (CF)
3892c52c52SAlan Cox  *	http://www.qic.org (QIC157 - Tape and DSC)
3992c52c52SAlan Cox  *	http://www.ce-ata.org (CE-ATA: not supported)
4092c52c52SAlan Cox  *
41c6fd2807SJeff Garzik  */
42c6fd2807SJeff Garzik 
43c6fd2807SJeff Garzik #include <linux/kernel.h>
44c6fd2807SJeff Garzik #include <linux/module.h>
45c6fd2807SJeff Garzik #include <linux/pci.h>
46c6fd2807SJeff Garzik #include <linux/init.h>
47c6fd2807SJeff Garzik #include <linux/list.h>
48c6fd2807SJeff Garzik #include <linux/mm.h>
49c6fd2807SJeff Garzik #include <linux/spinlock.h>
50c6fd2807SJeff Garzik #include <linux/blkdev.h>
51c6fd2807SJeff Garzik #include <linux/delay.h>
52c6fd2807SJeff Garzik #include <linux/timer.h>
53c6fd2807SJeff Garzik #include <linux/interrupt.h>
54c6fd2807SJeff Garzik #include <linux/completion.h>
55c6fd2807SJeff Garzik #include <linux/suspend.h>
56c6fd2807SJeff Garzik #include <linux/workqueue.h>
57c6fd2807SJeff Garzik #include <linux/scatterlist.h>
582dcb407eSJeff Garzik #include <linux/io.h>
59c6fd2807SJeff Garzik #include <scsi/scsi.h>
60c6fd2807SJeff Garzik #include <scsi/scsi_cmnd.h>
61c6fd2807SJeff Garzik #include <scsi/scsi_host.h>
62c6fd2807SJeff Garzik #include <linux/libata.h>
63c6fd2807SJeff Garzik #include <asm/byteorder.h>
64140b5e59STejun Heo #include <linux/cdrom.h>
65c6fd2807SJeff Garzik 
66c6fd2807SJeff Garzik #include "libata.h"
67c6fd2807SJeff Garzik 
68fda0efc5SJeff Garzik 
69c6fd2807SJeff Garzik /* debounce timing parameters in msecs { interval, duration, timeout } */
70c6fd2807SJeff Garzik const unsigned long sata_deb_timing_normal[]		= {   5,  100, 2000 };
71c6fd2807SJeff Garzik const unsigned long sata_deb_timing_hotplug[]		= {  25,  500, 2000 };
72c6fd2807SJeff Garzik const unsigned long sata_deb_timing_long[]		= { 100, 2000, 5000 };
73c6fd2807SJeff Garzik 
74029cfd6bSTejun Heo const struct ata_port_operations ata_base_port_ops = {
750aa1113dSTejun Heo 	.prereset		= ata_std_prereset,
76203c75b8STejun Heo 	.postreset		= ata_std_postreset,
77a1efdabaSTejun Heo 	.error_handler		= ata_std_error_handler,
78029cfd6bSTejun Heo };
79029cfd6bSTejun Heo 
80029cfd6bSTejun Heo const struct ata_port_operations sata_port_ops = {
81029cfd6bSTejun Heo 	.inherits		= &ata_base_port_ops,
82029cfd6bSTejun Heo 
83029cfd6bSTejun Heo 	.qc_defer		= ata_std_qc_defer,
8457c9efdfSTejun Heo 	.hardreset		= sata_std_hardreset,
85029cfd6bSTejun Heo };
86029cfd6bSTejun Heo 
87c6fd2807SJeff Garzik static unsigned int ata_dev_init_params(struct ata_device *dev,
88c6fd2807SJeff Garzik 					u16 heads, u16 sectors);
89c6fd2807SJeff Garzik static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
90218f3d30SJeff Garzik static unsigned int ata_dev_set_feature(struct ata_device *dev,
91218f3d30SJeff Garzik 					u8 enable, u8 feature);
92c6fd2807SJeff Garzik static void ata_dev_xfermask(struct ata_device *dev);
9375683fe7STejun Heo static unsigned long ata_dev_blacklisted(const struct ata_device *dev);
94c6fd2807SJeff Garzik 
95f3187195STejun Heo unsigned int ata_print_id = 1;
96c6fd2807SJeff Garzik static struct workqueue_struct *ata_wq;
97c6fd2807SJeff Garzik 
98c6fd2807SJeff Garzik struct workqueue_struct *ata_aux_wq;
99c6fd2807SJeff Garzik 
10033267325STejun Heo struct ata_force_param {
10133267325STejun Heo 	const char	*name;
10233267325STejun Heo 	unsigned int	cbl;
10333267325STejun Heo 	int		spd_limit;
10433267325STejun Heo 	unsigned long	xfer_mask;
10533267325STejun Heo 	unsigned int	horkage_on;
10633267325STejun Heo 	unsigned int	horkage_off;
10705944bdfSTejun Heo 	unsigned int	lflags;
10833267325STejun Heo };
10933267325STejun Heo 
11033267325STejun Heo struct ata_force_ent {
11133267325STejun Heo 	int			port;
11233267325STejun Heo 	int			device;
11333267325STejun Heo 	struct ata_force_param	param;
11433267325STejun Heo };
11533267325STejun Heo 
11633267325STejun Heo static struct ata_force_ent *ata_force_tbl;
11733267325STejun Heo static int ata_force_tbl_size;
11833267325STejun Heo 
11933267325STejun Heo static char ata_force_param_buf[PAGE_SIZE] __initdata;
1207afb4222STejun Heo /* param_buf is thrown away after initialization, disallow read */
1217afb4222STejun Heo module_param_string(force, ata_force_param_buf, sizeof(ata_force_param_buf), 0);
12233267325STejun Heo MODULE_PARM_DESC(force, "Force ATA configurations including cable type, link speed and transfer mode (see Documentation/kernel-parameters.txt for details)");
12333267325STejun Heo 
1242486fa56STejun Heo static int atapi_enabled = 1;
125c6fd2807SJeff Garzik module_param(atapi_enabled, int, 0444);
126c6fd2807SJeff Garzik MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on)");
127c6fd2807SJeff Garzik 
128c5c61bdaSAdrian Bunk static int atapi_dmadir = 0;
129c6fd2807SJeff Garzik module_param(atapi_dmadir, int, 0444);
130c6fd2807SJeff Garzik MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off, 1=on)");
131c6fd2807SJeff Garzik 
132baf4fdfaSMark Lord int atapi_passthru16 = 1;
133baf4fdfaSMark Lord module_param(atapi_passthru16, int, 0444);
134baf4fdfaSMark Lord MODULE_PARM_DESC(atapi_passthru16, "Enable ATA_16 passthru for ATAPI devices; on by default (0=off, 1=on)");
135baf4fdfaSMark Lord 
136c6fd2807SJeff Garzik int libata_fua = 0;
137c6fd2807SJeff Garzik module_param_named(fua, libata_fua, int, 0444);
138c6fd2807SJeff Garzik MODULE_PARM_DESC(fua, "FUA support (0=off, 1=on)");
139c6fd2807SJeff Garzik 
1402dcb407eSJeff Garzik static int ata_ignore_hpa;
1411e999736SAlan Cox module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644);
1421e999736SAlan Cox MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)");
1431e999736SAlan Cox 
144b3a70601SAlan Cox static int libata_dma_mask = ATA_DMA_MASK_ATA|ATA_DMA_MASK_ATAPI|ATA_DMA_MASK_CFA;
145b3a70601SAlan Cox module_param_named(dma, libata_dma_mask, int, 0444);
146b3a70601SAlan Cox MODULE_PARM_DESC(dma, "DMA enable/disable (0x1==ATA, 0x2==ATAPI, 0x4==CF)");
147b3a70601SAlan Cox 
14887fbc5a0STejun Heo static int ata_probe_timeout;
149c6fd2807SJeff Garzik module_param(ata_probe_timeout, int, 0444);
150c6fd2807SJeff Garzik MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
151c6fd2807SJeff Garzik 
1526ebe9d86SJeff Garzik int libata_noacpi = 0;
153d7d0dad6SJeff Garzik module_param_named(noacpi, libata_noacpi, int, 0444);
1546ebe9d86SJeff Garzik MODULE_PARM_DESC(noacpi, "Disables the use of ACPI in probe/suspend/resume when set");
15511ef697bSKristen Carlson Accardi 
156ae8d4ee7SAlan Cox int libata_allow_tpm = 0;
157ae8d4ee7SAlan Cox module_param_named(allow_tpm, libata_allow_tpm, int, 0444);
158ae8d4ee7SAlan Cox MODULE_PARM_DESC(allow_tpm, "Permit the use of TPM commands");
159ae8d4ee7SAlan Cox 
160c6fd2807SJeff Garzik MODULE_AUTHOR("Jeff Garzik");
161c6fd2807SJeff Garzik MODULE_DESCRIPTION("Library module for ATA devices");
162c6fd2807SJeff Garzik MODULE_LICENSE("GPL");
163c6fd2807SJeff Garzik MODULE_VERSION(DRV_VERSION);
164c6fd2807SJeff Garzik 
165c6fd2807SJeff Garzik 
166aadffb68STejun Heo /*
167aadffb68STejun Heo  * Iterator helpers.  Don't use directly.
168aadffb68STejun Heo  *
169aadffb68STejun Heo  * LOCKING:
170aadffb68STejun Heo  * Host lock or EH context.
171aadffb68STejun Heo  */
172aadffb68STejun Heo struct ata_link *__ata_port_next_link(struct ata_port *ap,
173aadffb68STejun Heo 				      struct ata_link *link, bool dev_only)
174aadffb68STejun Heo {
175aadffb68STejun Heo 	/* NULL link indicates start of iteration */
176aadffb68STejun Heo 	if (!link) {
177aadffb68STejun Heo 		if (dev_only && sata_pmp_attached(ap))
178aadffb68STejun Heo 			return ap->pmp_link;
179aadffb68STejun Heo 		return &ap->link;
180aadffb68STejun Heo 	}
181aadffb68STejun Heo 
182b1c72916STejun Heo 	/* we just iterated over the host master link, what's next? */
183b1c72916STejun Heo 	if (link == &ap->link) {
184b1c72916STejun Heo 		if (!sata_pmp_attached(ap)) {
185b1c72916STejun Heo 			if (unlikely(ap->slave_link) && !dev_only)
186b1c72916STejun Heo 				return ap->slave_link;
187aadffb68STejun Heo 			return NULL;
188b1c72916STejun Heo 		}
189aadffb68STejun Heo 		return ap->pmp_link;
190aadffb68STejun Heo 	}
191aadffb68STejun Heo 
192b1c72916STejun Heo 	/* slave_link excludes PMP */
193b1c72916STejun Heo 	if (unlikely(link == ap->slave_link))
194b1c72916STejun Heo 		return NULL;
195b1c72916STejun Heo 
196aadffb68STejun Heo 	/* iterate to the next PMP link */
197aadffb68STejun Heo 	if (++link < ap->pmp_link + ap->nr_pmp_links)
198aadffb68STejun Heo 		return link;
199aadffb68STejun Heo 	return NULL;
200aadffb68STejun Heo }
201aadffb68STejun Heo 
202c6fd2807SJeff Garzik /**
203b1c72916STejun Heo  *	ata_dev_phys_link - find physical link for a device
204b1c72916STejun Heo  *	@dev: ATA device to look up physical link for
205b1c72916STejun Heo  *
206b1c72916STejun Heo  *	Look up physical link which @dev is attached to.  Note that
207b1c72916STejun Heo  *	this is different from @dev->link only when @dev is on slave
208b1c72916STejun Heo  *	link.  For all other cases, it's the same as @dev->link.
209b1c72916STejun Heo  *
210b1c72916STejun Heo  *	LOCKING:
211b1c72916STejun Heo  *	Don't care.
212b1c72916STejun Heo  *
213b1c72916STejun Heo  *	RETURNS:
214b1c72916STejun Heo  *	Pointer to the found physical link.
215b1c72916STejun Heo  */
216b1c72916STejun Heo struct ata_link *ata_dev_phys_link(struct ata_device *dev)
217b1c72916STejun Heo {
218b1c72916STejun Heo 	struct ata_port *ap = dev->link->ap;
219b1c72916STejun Heo 
220b1c72916STejun Heo 	if (!ap->slave_link)
221b1c72916STejun Heo 		return dev->link;
222b1c72916STejun Heo 	if (!dev->devno)
223b1c72916STejun Heo 		return &ap->link;
224b1c72916STejun Heo 	return ap->slave_link;
225b1c72916STejun Heo }
226b1c72916STejun Heo 
227b1c72916STejun Heo /**
22833267325STejun Heo  *	ata_force_cbl - force cable type according to libata.force
2294cdfa1b3SRandy Dunlap  *	@ap: ATA port of interest
23033267325STejun Heo  *
23133267325STejun Heo  *	Force cable type according to libata.force and whine about it.
23233267325STejun Heo  *	The last entry which has matching port number is used, so it
23333267325STejun Heo  *	can be specified as part of device force parameters.  For
23433267325STejun Heo  *	example, both "a:40c,1.00:udma4" and "1.00:40c,udma4" have the
23533267325STejun Heo  *	same effect.
23633267325STejun Heo  *
23733267325STejun Heo  *	LOCKING:
23833267325STejun Heo  *	EH context.
23933267325STejun Heo  */
24033267325STejun Heo void ata_force_cbl(struct ata_port *ap)
24133267325STejun Heo {
24233267325STejun Heo 	int i;
24333267325STejun Heo 
24433267325STejun Heo 	for (i = ata_force_tbl_size - 1; i >= 0; i--) {
24533267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
24633267325STejun Heo 
24733267325STejun Heo 		if (fe->port != -1 && fe->port != ap->print_id)
24833267325STejun Heo 			continue;
24933267325STejun Heo 
25033267325STejun Heo 		if (fe->param.cbl == ATA_CBL_NONE)
25133267325STejun Heo 			continue;
25233267325STejun Heo 
25333267325STejun Heo 		ap->cbl = fe->param.cbl;
25433267325STejun Heo 		ata_port_printk(ap, KERN_NOTICE,
25533267325STejun Heo 				"FORCE: cable set to %s\n", fe->param.name);
25633267325STejun Heo 		return;
25733267325STejun Heo 	}
25833267325STejun Heo }
25933267325STejun Heo 
26033267325STejun Heo /**
26105944bdfSTejun Heo  *	ata_force_link_limits - force link limits according to libata.force
26233267325STejun Heo  *	@link: ATA link of interest
26333267325STejun Heo  *
26405944bdfSTejun Heo  *	Force link flags and SATA spd limit according to libata.force
26505944bdfSTejun Heo  *	and whine about it.  When only the port part is specified
26605944bdfSTejun Heo  *	(e.g. 1:), the limit applies to all links connected to both
26705944bdfSTejun Heo  *	the host link and all fan-out ports connected via PMP.  If the
26805944bdfSTejun Heo  *	device part is specified as 0 (e.g. 1.00:), it specifies the
26905944bdfSTejun Heo  *	first fan-out link not the host link.  Device number 15 always
270b1c72916STejun Heo  *	points to the host link whether PMP is attached or not.  If the
271b1c72916STejun Heo  *	controller has slave link, device number 16 points to it.
27233267325STejun Heo  *
27333267325STejun Heo  *	LOCKING:
27433267325STejun Heo  *	EH context.
27533267325STejun Heo  */
27605944bdfSTejun Heo static void ata_force_link_limits(struct ata_link *link)
27733267325STejun Heo {
27805944bdfSTejun Heo 	bool did_spd = false;
279b1c72916STejun Heo 	int linkno = link->pmp;
280b1c72916STejun Heo 	int i;
28133267325STejun Heo 
28233267325STejun Heo 	if (ata_is_host_link(link))
283b1c72916STejun Heo 		linkno += 15;
28433267325STejun Heo 
28533267325STejun Heo 	for (i = ata_force_tbl_size - 1; i >= 0; i--) {
28633267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
28733267325STejun Heo 
28833267325STejun Heo 		if (fe->port != -1 && fe->port != link->ap->print_id)
28933267325STejun Heo 			continue;
29033267325STejun Heo 
29133267325STejun Heo 		if (fe->device != -1 && fe->device != linkno)
29233267325STejun Heo 			continue;
29333267325STejun Heo 
29405944bdfSTejun Heo 		/* only honor the first spd limit */
29505944bdfSTejun Heo 		if (!did_spd && fe->param.spd_limit) {
29633267325STejun Heo 			link->hw_sata_spd_limit = (1 << fe->param.spd_limit) - 1;
29733267325STejun Heo 			ata_link_printk(link, KERN_NOTICE,
29805944bdfSTejun Heo 					"FORCE: PHY spd limit set to %s\n",
29905944bdfSTejun Heo 					fe->param.name);
30005944bdfSTejun Heo 			did_spd = true;
30105944bdfSTejun Heo 		}
30205944bdfSTejun Heo 
30305944bdfSTejun Heo 		/* let lflags stack */
30405944bdfSTejun Heo 		if (fe->param.lflags) {
30505944bdfSTejun Heo 			link->flags |= fe->param.lflags;
30605944bdfSTejun Heo 			ata_link_printk(link, KERN_NOTICE,
30705944bdfSTejun Heo 					"FORCE: link flag 0x%x forced -> 0x%x\n",
30805944bdfSTejun Heo 					fe->param.lflags, link->flags);
30905944bdfSTejun Heo 		}
31033267325STejun Heo 	}
31133267325STejun Heo }
31233267325STejun Heo 
31333267325STejun Heo /**
31433267325STejun Heo  *	ata_force_xfermask - force xfermask according to libata.force
31533267325STejun Heo  *	@dev: ATA device of interest
31633267325STejun Heo  *
31733267325STejun Heo  *	Force xfer_mask according to libata.force and whine about it.
31833267325STejun Heo  *	For consistency with link selection, device number 15 selects
31933267325STejun Heo  *	the first device connected to the host link.
32033267325STejun Heo  *
32133267325STejun Heo  *	LOCKING:
32233267325STejun Heo  *	EH context.
32333267325STejun Heo  */
32433267325STejun Heo static void ata_force_xfermask(struct ata_device *dev)
32533267325STejun Heo {
32633267325STejun Heo 	int devno = dev->link->pmp + dev->devno;
32733267325STejun Heo 	int alt_devno = devno;
32833267325STejun Heo 	int i;
32933267325STejun Heo 
330b1c72916STejun Heo 	/* allow n.15/16 for devices attached to host port */
331b1c72916STejun Heo 	if (ata_is_host_link(dev->link))
332b1c72916STejun Heo 		alt_devno += 15;
33333267325STejun Heo 
33433267325STejun Heo 	for (i = ata_force_tbl_size - 1; i >= 0; i--) {
33533267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
33633267325STejun Heo 		unsigned long pio_mask, mwdma_mask, udma_mask;
33733267325STejun Heo 
33833267325STejun Heo 		if (fe->port != -1 && fe->port != dev->link->ap->print_id)
33933267325STejun Heo 			continue;
34033267325STejun Heo 
34133267325STejun Heo 		if (fe->device != -1 && fe->device != devno &&
34233267325STejun Heo 		    fe->device != alt_devno)
34333267325STejun Heo 			continue;
34433267325STejun Heo 
34533267325STejun Heo 		if (!fe->param.xfer_mask)
34633267325STejun Heo 			continue;
34733267325STejun Heo 
34833267325STejun Heo 		ata_unpack_xfermask(fe->param.xfer_mask,
34933267325STejun Heo 				    &pio_mask, &mwdma_mask, &udma_mask);
35033267325STejun Heo 		if (udma_mask)
35133267325STejun Heo 			dev->udma_mask = udma_mask;
35233267325STejun Heo 		else if (mwdma_mask) {
35333267325STejun Heo 			dev->udma_mask = 0;
35433267325STejun Heo 			dev->mwdma_mask = mwdma_mask;
35533267325STejun Heo 		} else {
35633267325STejun Heo 			dev->udma_mask = 0;
35733267325STejun Heo 			dev->mwdma_mask = 0;
35833267325STejun Heo 			dev->pio_mask = pio_mask;
35933267325STejun Heo 		}
36033267325STejun Heo 
36133267325STejun Heo 		ata_dev_printk(dev, KERN_NOTICE,
36233267325STejun Heo 			"FORCE: xfer_mask set to %s\n", fe->param.name);
36333267325STejun Heo 		return;
36433267325STejun Heo 	}
36533267325STejun Heo }
36633267325STejun Heo 
36733267325STejun Heo /**
36833267325STejun Heo  *	ata_force_horkage - force horkage according to libata.force
36933267325STejun Heo  *	@dev: ATA device of interest
37033267325STejun Heo  *
37133267325STejun Heo  *	Force horkage according to libata.force and whine about it.
37233267325STejun Heo  *	For consistency with link selection, device number 15 selects
37333267325STejun Heo  *	the first device connected to the host link.
37433267325STejun Heo  *
37533267325STejun Heo  *	LOCKING:
37633267325STejun Heo  *	EH context.
37733267325STejun Heo  */
37833267325STejun Heo static void ata_force_horkage(struct ata_device *dev)
37933267325STejun Heo {
38033267325STejun Heo 	int devno = dev->link->pmp + dev->devno;
38133267325STejun Heo 	int alt_devno = devno;
38233267325STejun Heo 	int i;
38333267325STejun Heo 
384b1c72916STejun Heo 	/* allow n.15/16 for devices attached to host port */
385b1c72916STejun Heo 	if (ata_is_host_link(dev->link))
386b1c72916STejun Heo 		alt_devno += 15;
38733267325STejun Heo 
38833267325STejun Heo 	for (i = 0; i < ata_force_tbl_size; i++) {
38933267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
39033267325STejun Heo 
39133267325STejun Heo 		if (fe->port != -1 && fe->port != dev->link->ap->print_id)
39233267325STejun Heo 			continue;
39333267325STejun Heo 
39433267325STejun Heo 		if (fe->device != -1 && fe->device != devno &&
39533267325STejun Heo 		    fe->device != alt_devno)
39633267325STejun Heo 			continue;
39733267325STejun Heo 
39833267325STejun Heo 		if (!(~dev->horkage & fe->param.horkage_on) &&
39933267325STejun Heo 		    !(dev->horkage & fe->param.horkage_off))
40033267325STejun Heo 			continue;
40133267325STejun Heo 
40233267325STejun Heo 		dev->horkage |= fe->param.horkage_on;
40333267325STejun Heo 		dev->horkage &= ~fe->param.horkage_off;
40433267325STejun Heo 
40533267325STejun Heo 		ata_dev_printk(dev, KERN_NOTICE,
40633267325STejun Heo 			"FORCE: horkage modified (%s)\n", fe->param.name);
40733267325STejun Heo 	}
40833267325STejun Heo }
40933267325STejun Heo 
41033267325STejun Heo /**
411436d34b3STejun Heo  *	atapi_cmd_type - Determine ATAPI command type from SCSI opcode
412436d34b3STejun Heo  *	@opcode: SCSI opcode
413436d34b3STejun Heo  *
414436d34b3STejun Heo  *	Determine ATAPI command type from @opcode.
415436d34b3STejun Heo  *
416436d34b3STejun Heo  *	LOCKING:
417436d34b3STejun Heo  *	None.
418436d34b3STejun Heo  *
419436d34b3STejun Heo  *	RETURNS:
420436d34b3STejun Heo  *	ATAPI_{READ|WRITE|READ_CD|PASS_THRU|MISC}
421436d34b3STejun Heo  */
422436d34b3STejun Heo int atapi_cmd_type(u8 opcode)
423436d34b3STejun Heo {
424436d34b3STejun Heo 	switch (opcode) {
425436d34b3STejun Heo 	case GPCMD_READ_10:
426436d34b3STejun Heo 	case GPCMD_READ_12:
427436d34b3STejun Heo 		return ATAPI_READ;
428436d34b3STejun Heo 
429436d34b3STejun Heo 	case GPCMD_WRITE_10:
430436d34b3STejun Heo 	case GPCMD_WRITE_12:
431436d34b3STejun Heo 	case GPCMD_WRITE_AND_VERIFY_10:
432436d34b3STejun Heo 		return ATAPI_WRITE;
433436d34b3STejun Heo 
434436d34b3STejun Heo 	case GPCMD_READ_CD:
435436d34b3STejun Heo 	case GPCMD_READ_CD_MSF:
436436d34b3STejun Heo 		return ATAPI_READ_CD;
437436d34b3STejun Heo 
438e52dcc48STejun Heo 	case ATA_16:
439e52dcc48STejun Heo 	case ATA_12:
440e52dcc48STejun Heo 		if (atapi_passthru16)
441e52dcc48STejun Heo 			return ATAPI_PASS_THRU;
442e52dcc48STejun Heo 		/* fall thru */
443436d34b3STejun Heo 	default:
444436d34b3STejun Heo 		return ATAPI_MISC;
445436d34b3STejun Heo 	}
446436d34b3STejun Heo }
447436d34b3STejun Heo 
448436d34b3STejun Heo /**
449c6fd2807SJeff Garzik  *	ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
450c6fd2807SJeff Garzik  *	@tf: Taskfile to convert
451c6fd2807SJeff Garzik  *	@pmp: Port multiplier port
4529977126cSTejun Heo  *	@is_cmd: This FIS is for command
4539977126cSTejun Heo  *	@fis: Buffer into which data will output
454c6fd2807SJeff Garzik  *
455c6fd2807SJeff Garzik  *	Converts a standard ATA taskfile to a Serial ATA
456c6fd2807SJeff Garzik  *	FIS structure (Register - Host to Device).
457c6fd2807SJeff Garzik  *
458c6fd2807SJeff Garzik  *	LOCKING:
459c6fd2807SJeff Garzik  *	Inherited from caller.
460c6fd2807SJeff Garzik  */
4619977126cSTejun Heo void ata_tf_to_fis(const struct ata_taskfile *tf, u8 pmp, int is_cmd, u8 *fis)
462c6fd2807SJeff Garzik {
463c6fd2807SJeff Garzik 	fis[0] = 0x27;			/* Register - Host to Device FIS */
4649977126cSTejun Heo 	fis[1] = pmp & 0xf;		/* Port multiplier number*/
4659977126cSTejun Heo 	if (is_cmd)
4669977126cSTejun Heo 		fis[1] |= (1 << 7);	/* bit 7 indicates Command FIS */
4679977126cSTejun Heo 
468c6fd2807SJeff Garzik 	fis[2] = tf->command;
469c6fd2807SJeff Garzik 	fis[3] = tf->feature;
470c6fd2807SJeff Garzik 
471c6fd2807SJeff Garzik 	fis[4] = tf->lbal;
472c6fd2807SJeff Garzik 	fis[5] = tf->lbam;
473c6fd2807SJeff Garzik 	fis[6] = tf->lbah;
474c6fd2807SJeff Garzik 	fis[7] = tf->device;
475c6fd2807SJeff Garzik 
476c6fd2807SJeff Garzik 	fis[8] = tf->hob_lbal;
477c6fd2807SJeff Garzik 	fis[9] = tf->hob_lbam;
478c6fd2807SJeff Garzik 	fis[10] = tf->hob_lbah;
479c6fd2807SJeff Garzik 	fis[11] = tf->hob_feature;
480c6fd2807SJeff Garzik 
481c6fd2807SJeff Garzik 	fis[12] = tf->nsect;
482c6fd2807SJeff Garzik 	fis[13] = tf->hob_nsect;
483c6fd2807SJeff Garzik 	fis[14] = 0;
484c6fd2807SJeff Garzik 	fis[15] = tf->ctl;
485c6fd2807SJeff Garzik 
486c6fd2807SJeff Garzik 	fis[16] = 0;
487c6fd2807SJeff Garzik 	fis[17] = 0;
488c6fd2807SJeff Garzik 	fis[18] = 0;
489c6fd2807SJeff Garzik 	fis[19] = 0;
490c6fd2807SJeff Garzik }
491c6fd2807SJeff Garzik 
492c6fd2807SJeff Garzik /**
493c6fd2807SJeff Garzik  *	ata_tf_from_fis - Convert SATA FIS to ATA taskfile
494c6fd2807SJeff Garzik  *	@fis: Buffer from which data will be input
495c6fd2807SJeff Garzik  *	@tf: Taskfile to output
496c6fd2807SJeff Garzik  *
497c6fd2807SJeff Garzik  *	Converts a serial ATA FIS structure to a standard ATA taskfile.
498c6fd2807SJeff Garzik  *
499c6fd2807SJeff Garzik  *	LOCKING:
500c6fd2807SJeff Garzik  *	Inherited from caller.
501c6fd2807SJeff Garzik  */
502c6fd2807SJeff Garzik 
503c6fd2807SJeff Garzik void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
504c6fd2807SJeff Garzik {
505c6fd2807SJeff Garzik 	tf->command	= fis[2];	/* status */
506c6fd2807SJeff Garzik 	tf->feature	= fis[3];	/* error */
507c6fd2807SJeff Garzik 
508c6fd2807SJeff Garzik 	tf->lbal	= fis[4];
509c6fd2807SJeff Garzik 	tf->lbam	= fis[5];
510c6fd2807SJeff Garzik 	tf->lbah	= fis[6];
511c6fd2807SJeff Garzik 	tf->device	= fis[7];
512c6fd2807SJeff Garzik 
513c6fd2807SJeff Garzik 	tf->hob_lbal	= fis[8];
514c6fd2807SJeff Garzik 	tf->hob_lbam	= fis[9];
515c6fd2807SJeff Garzik 	tf->hob_lbah	= fis[10];
516c6fd2807SJeff Garzik 
517c6fd2807SJeff Garzik 	tf->nsect	= fis[12];
518c6fd2807SJeff Garzik 	tf->hob_nsect	= fis[13];
519c6fd2807SJeff Garzik }
520c6fd2807SJeff Garzik 
521c6fd2807SJeff Garzik static const u8 ata_rw_cmds[] = {
522c6fd2807SJeff Garzik 	/* pio multi */
523c6fd2807SJeff Garzik 	ATA_CMD_READ_MULTI,
524c6fd2807SJeff Garzik 	ATA_CMD_WRITE_MULTI,
525c6fd2807SJeff Garzik 	ATA_CMD_READ_MULTI_EXT,
526c6fd2807SJeff Garzik 	ATA_CMD_WRITE_MULTI_EXT,
527c6fd2807SJeff Garzik 	0,
528c6fd2807SJeff Garzik 	0,
529c6fd2807SJeff Garzik 	0,
530c6fd2807SJeff Garzik 	ATA_CMD_WRITE_MULTI_FUA_EXT,
531c6fd2807SJeff Garzik 	/* pio */
532c6fd2807SJeff Garzik 	ATA_CMD_PIO_READ,
533c6fd2807SJeff Garzik 	ATA_CMD_PIO_WRITE,
534c6fd2807SJeff Garzik 	ATA_CMD_PIO_READ_EXT,
535c6fd2807SJeff Garzik 	ATA_CMD_PIO_WRITE_EXT,
536c6fd2807SJeff Garzik 	0,
537c6fd2807SJeff Garzik 	0,
538c6fd2807SJeff Garzik 	0,
539c6fd2807SJeff Garzik 	0,
540c6fd2807SJeff Garzik 	/* dma */
541c6fd2807SJeff Garzik 	ATA_CMD_READ,
542c6fd2807SJeff Garzik 	ATA_CMD_WRITE,
543c6fd2807SJeff Garzik 	ATA_CMD_READ_EXT,
544c6fd2807SJeff Garzik 	ATA_CMD_WRITE_EXT,
545c6fd2807SJeff Garzik 	0,
546c6fd2807SJeff Garzik 	0,
547c6fd2807SJeff Garzik 	0,
548c6fd2807SJeff Garzik 	ATA_CMD_WRITE_FUA_EXT
549c6fd2807SJeff Garzik };
550c6fd2807SJeff Garzik 
551c6fd2807SJeff Garzik /**
552c6fd2807SJeff Garzik  *	ata_rwcmd_protocol - set taskfile r/w commands and protocol
553bd056d7eSTejun Heo  *	@tf: command to examine and configure
554bd056d7eSTejun Heo  *	@dev: device tf belongs to
555c6fd2807SJeff Garzik  *
556c6fd2807SJeff Garzik  *	Examine the device configuration and tf->flags to calculate
557c6fd2807SJeff Garzik  *	the proper read/write commands and protocol to use.
558c6fd2807SJeff Garzik  *
559c6fd2807SJeff Garzik  *	LOCKING:
560c6fd2807SJeff Garzik  *	caller.
561c6fd2807SJeff Garzik  */
562bd056d7eSTejun Heo static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev)
563c6fd2807SJeff Garzik {
564c6fd2807SJeff Garzik 	u8 cmd;
565c6fd2807SJeff Garzik 
566c6fd2807SJeff Garzik 	int index, fua, lba48, write;
567c6fd2807SJeff Garzik 
568c6fd2807SJeff Garzik 	fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
569c6fd2807SJeff Garzik 	lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
570c6fd2807SJeff Garzik 	write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
571c6fd2807SJeff Garzik 
572c6fd2807SJeff Garzik 	if (dev->flags & ATA_DFLAG_PIO) {
573c6fd2807SJeff Garzik 		tf->protocol = ATA_PROT_PIO;
574c6fd2807SJeff Garzik 		index = dev->multi_count ? 0 : 8;
5759af5c9c9STejun Heo 	} else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) {
576c6fd2807SJeff Garzik 		/* Unable to use DMA due to host limitation */
577c6fd2807SJeff Garzik 		tf->protocol = ATA_PROT_PIO;
578c6fd2807SJeff Garzik 		index = dev->multi_count ? 0 : 8;
579c6fd2807SJeff Garzik 	} else {
580c6fd2807SJeff Garzik 		tf->protocol = ATA_PROT_DMA;
581c6fd2807SJeff Garzik 		index = 16;
582c6fd2807SJeff Garzik 	}
583c6fd2807SJeff Garzik 
584c6fd2807SJeff Garzik 	cmd = ata_rw_cmds[index + fua + lba48 + write];
585c6fd2807SJeff Garzik 	if (cmd) {
586c6fd2807SJeff Garzik 		tf->command = cmd;
587c6fd2807SJeff Garzik 		return 0;
588c6fd2807SJeff Garzik 	}
589c6fd2807SJeff Garzik 	return -1;
590c6fd2807SJeff Garzik }
591c6fd2807SJeff Garzik 
592c6fd2807SJeff Garzik /**
59335b649feSTejun Heo  *	ata_tf_read_block - Read block address from ATA taskfile
59435b649feSTejun Heo  *	@tf: ATA taskfile of interest
59535b649feSTejun Heo  *	@dev: ATA device @tf belongs to
59635b649feSTejun Heo  *
59735b649feSTejun Heo  *	LOCKING:
59835b649feSTejun Heo  *	None.
59935b649feSTejun Heo  *
60035b649feSTejun Heo  *	Read block address from @tf.  This function can handle all
60135b649feSTejun Heo  *	three address formats - LBA, LBA48 and CHS.  tf->protocol and
60235b649feSTejun Heo  *	flags select the address format to use.
60335b649feSTejun Heo  *
60435b649feSTejun Heo  *	RETURNS:
60535b649feSTejun Heo  *	Block address read from @tf.
60635b649feSTejun Heo  */
60735b649feSTejun Heo u64 ata_tf_read_block(struct ata_taskfile *tf, struct ata_device *dev)
60835b649feSTejun Heo {
60935b649feSTejun Heo 	u64 block = 0;
61035b649feSTejun Heo 
61135b649feSTejun Heo 	if (tf->flags & ATA_TFLAG_LBA) {
61235b649feSTejun Heo 		if (tf->flags & ATA_TFLAG_LBA48) {
61335b649feSTejun Heo 			block |= (u64)tf->hob_lbah << 40;
61435b649feSTejun Heo 			block |= (u64)tf->hob_lbam << 32;
61535b649feSTejun Heo 			block |= tf->hob_lbal << 24;
61635b649feSTejun Heo 		} else
61735b649feSTejun Heo 			block |= (tf->device & 0xf) << 24;
61835b649feSTejun Heo 
61935b649feSTejun Heo 		block |= tf->lbah << 16;
62035b649feSTejun Heo 		block |= tf->lbam << 8;
62135b649feSTejun Heo 		block |= tf->lbal;
62235b649feSTejun Heo 	} else {
62335b649feSTejun Heo 		u32 cyl, head, sect;
62435b649feSTejun Heo 
62535b649feSTejun Heo 		cyl = tf->lbam | (tf->lbah << 8);
62635b649feSTejun Heo 		head = tf->device & 0xf;
62735b649feSTejun Heo 		sect = tf->lbal;
62835b649feSTejun Heo 
62935b649feSTejun Heo 		block = (cyl * dev->heads + head) * dev->sectors + sect;
63035b649feSTejun Heo 	}
63135b649feSTejun Heo 
63235b649feSTejun Heo 	return block;
63335b649feSTejun Heo }
63435b649feSTejun Heo 
63535b649feSTejun Heo /**
636bd056d7eSTejun Heo  *	ata_build_rw_tf - Build ATA taskfile for given read/write request
637bd056d7eSTejun Heo  *	@tf: Target ATA taskfile
638bd056d7eSTejun Heo  *	@dev: ATA device @tf belongs to
639bd056d7eSTejun Heo  *	@block: Block address
640bd056d7eSTejun Heo  *	@n_block: Number of blocks
641bd056d7eSTejun Heo  *	@tf_flags: RW/FUA etc...
642bd056d7eSTejun Heo  *	@tag: tag
643bd056d7eSTejun Heo  *
644bd056d7eSTejun Heo  *	LOCKING:
645bd056d7eSTejun Heo  *	None.
646bd056d7eSTejun Heo  *
647bd056d7eSTejun Heo  *	Build ATA taskfile @tf for read/write request described by
648bd056d7eSTejun Heo  *	@block, @n_block, @tf_flags and @tag on @dev.
649bd056d7eSTejun Heo  *
650bd056d7eSTejun Heo  *	RETURNS:
651bd056d7eSTejun Heo  *
652bd056d7eSTejun Heo  *	0 on success, -ERANGE if the request is too large for @dev,
653bd056d7eSTejun Heo  *	-EINVAL if the request is invalid.
654bd056d7eSTejun Heo  */
655bd056d7eSTejun Heo int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev,
656bd056d7eSTejun Heo 		    u64 block, u32 n_block, unsigned int tf_flags,
657bd056d7eSTejun Heo 		    unsigned int tag)
658bd056d7eSTejun Heo {
659bd056d7eSTejun Heo 	tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
660bd056d7eSTejun Heo 	tf->flags |= tf_flags;
661bd056d7eSTejun Heo 
6626d1245bfSTejun Heo 	if (ata_ncq_enabled(dev) && likely(tag != ATA_TAG_INTERNAL)) {
663bd056d7eSTejun Heo 		/* yay, NCQ */
664bd056d7eSTejun Heo 		if (!lba_48_ok(block, n_block))
665bd056d7eSTejun Heo 			return -ERANGE;
666bd056d7eSTejun Heo 
667bd056d7eSTejun Heo 		tf->protocol = ATA_PROT_NCQ;
668bd056d7eSTejun Heo 		tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
669bd056d7eSTejun Heo 
670bd056d7eSTejun Heo 		if (tf->flags & ATA_TFLAG_WRITE)
671bd056d7eSTejun Heo 			tf->command = ATA_CMD_FPDMA_WRITE;
672bd056d7eSTejun Heo 		else
673bd056d7eSTejun Heo 			tf->command = ATA_CMD_FPDMA_READ;
674bd056d7eSTejun Heo 
675bd056d7eSTejun Heo 		tf->nsect = tag << 3;
676bd056d7eSTejun Heo 		tf->hob_feature = (n_block >> 8) & 0xff;
677bd056d7eSTejun Heo 		tf->feature = n_block & 0xff;
678bd056d7eSTejun Heo 
679bd056d7eSTejun Heo 		tf->hob_lbah = (block >> 40) & 0xff;
680bd056d7eSTejun Heo 		tf->hob_lbam = (block >> 32) & 0xff;
681bd056d7eSTejun Heo 		tf->hob_lbal = (block >> 24) & 0xff;
682bd056d7eSTejun Heo 		tf->lbah = (block >> 16) & 0xff;
683bd056d7eSTejun Heo 		tf->lbam = (block >> 8) & 0xff;
684bd056d7eSTejun Heo 		tf->lbal = block & 0xff;
685bd056d7eSTejun Heo 
686bd056d7eSTejun Heo 		tf->device = 1 << 6;
687bd056d7eSTejun Heo 		if (tf->flags & ATA_TFLAG_FUA)
688bd056d7eSTejun Heo 			tf->device |= 1 << 7;
689bd056d7eSTejun Heo 	} else if (dev->flags & ATA_DFLAG_LBA) {
690bd056d7eSTejun Heo 		tf->flags |= ATA_TFLAG_LBA;
691bd056d7eSTejun Heo 
692bd056d7eSTejun Heo 		if (lba_28_ok(block, n_block)) {
693bd056d7eSTejun Heo 			/* use LBA28 */
694bd056d7eSTejun Heo 			tf->device |= (block >> 24) & 0xf;
695bd056d7eSTejun Heo 		} else if (lba_48_ok(block, n_block)) {
696bd056d7eSTejun Heo 			if (!(dev->flags & ATA_DFLAG_LBA48))
697bd056d7eSTejun Heo 				return -ERANGE;
698bd056d7eSTejun Heo 
699bd056d7eSTejun Heo 			/* use LBA48 */
700bd056d7eSTejun Heo 			tf->flags |= ATA_TFLAG_LBA48;
701bd056d7eSTejun Heo 
702bd056d7eSTejun Heo 			tf->hob_nsect = (n_block >> 8) & 0xff;
703bd056d7eSTejun Heo 
704bd056d7eSTejun Heo 			tf->hob_lbah = (block >> 40) & 0xff;
705bd056d7eSTejun Heo 			tf->hob_lbam = (block >> 32) & 0xff;
706bd056d7eSTejun Heo 			tf->hob_lbal = (block >> 24) & 0xff;
707bd056d7eSTejun Heo 		} else
708bd056d7eSTejun Heo 			/* request too large even for LBA48 */
709bd056d7eSTejun Heo 			return -ERANGE;
710bd056d7eSTejun Heo 
711bd056d7eSTejun Heo 		if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
712bd056d7eSTejun Heo 			return -EINVAL;
713bd056d7eSTejun Heo 
714bd056d7eSTejun Heo 		tf->nsect = n_block & 0xff;
715bd056d7eSTejun Heo 
716bd056d7eSTejun Heo 		tf->lbah = (block >> 16) & 0xff;
717bd056d7eSTejun Heo 		tf->lbam = (block >> 8) & 0xff;
718bd056d7eSTejun Heo 		tf->lbal = block & 0xff;
719bd056d7eSTejun Heo 
720bd056d7eSTejun Heo 		tf->device |= ATA_LBA;
721bd056d7eSTejun Heo 	} else {
722bd056d7eSTejun Heo 		/* CHS */
723bd056d7eSTejun Heo 		u32 sect, head, cyl, track;
724bd056d7eSTejun Heo 
725bd056d7eSTejun Heo 		/* The request -may- be too large for CHS addressing. */
726bd056d7eSTejun Heo 		if (!lba_28_ok(block, n_block))
727bd056d7eSTejun Heo 			return -ERANGE;
728bd056d7eSTejun Heo 
729bd056d7eSTejun Heo 		if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
730bd056d7eSTejun Heo 			return -EINVAL;
731bd056d7eSTejun Heo 
732bd056d7eSTejun Heo 		/* Convert LBA to CHS */
733bd056d7eSTejun Heo 		track = (u32)block / dev->sectors;
734bd056d7eSTejun Heo 		cyl   = track / dev->heads;
735bd056d7eSTejun Heo 		head  = track % dev->heads;
736bd056d7eSTejun Heo 		sect  = (u32)block % dev->sectors + 1;
737bd056d7eSTejun Heo 
738bd056d7eSTejun Heo 		DPRINTK("block %u track %u cyl %u head %u sect %u\n",
739bd056d7eSTejun Heo 			(u32)block, track, cyl, head, sect);
740bd056d7eSTejun Heo 
741bd056d7eSTejun Heo 		/* Check whether the converted CHS can fit.
742bd056d7eSTejun Heo 		   Cylinder: 0-65535
743bd056d7eSTejun Heo 		   Head: 0-15
744bd056d7eSTejun Heo 		   Sector: 1-255*/
745bd056d7eSTejun Heo 		if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
746bd056d7eSTejun Heo 			return -ERANGE;
747bd056d7eSTejun Heo 
748bd056d7eSTejun Heo 		tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
749bd056d7eSTejun Heo 		tf->lbal = sect;
750bd056d7eSTejun Heo 		tf->lbam = cyl;
751bd056d7eSTejun Heo 		tf->lbah = cyl >> 8;
752bd056d7eSTejun Heo 		tf->device |= head;
753bd056d7eSTejun Heo 	}
754bd056d7eSTejun Heo 
755bd056d7eSTejun Heo 	return 0;
756bd056d7eSTejun Heo }
757bd056d7eSTejun Heo 
758bd056d7eSTejun Heo /**
759c6fd2807SJeff Garzik  *	ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
760c6fd2807SJeff Garzik  *	@pio_mask: pio_mask
761c6fd2807SJeff Garzik  *	@mwdma_mask: mwdma_mask
762c6fd2807SJeff Garzik  *	@udma_mask: udma_mask
763c6fd2807SJeff Garzik  *
764c6fd2807SJeff Garzik  *	Pack @pio_mask, @mwdma_mask and @udma_mask into a single
765c6fd2807SJeff Garzik  *	unsigned int xfer_mask.
766c6fd2807SJeff Garzik  *
767c6fd2807SJeff Garzik  *	LOCKING:
768c6fd2807SJeff Garzik  *	None.
769c6fd2807SJeff Garzik  *
770c6fd2807SJeff Garzik  *	RETURNS:
771c6fd2807SJeff Garzik  *	Packed xfer_mask.
772c6fd2807SJeff Garzik  */
7737dc951aeSTejun Heo unsigned long ata_pack_xfermask(unsigned long pio_mask,
7747dc951aeSTejun Heo 				unsigned long mwdma_mask,
7757dc951aeSTejun Heo 				unsigned long udma_mask)
776c6fd2807SJeff Garzik {
777c6fd2807SJeff Garzik 	return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
778c6fd2807SJeff Garzik 		((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
779c6fd2807SJeff Garzik 		((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
780c6fd2807SJeff Garzik }
781c6fd2807SJeff Garzik 
782c6fd2807SJeff Garzik /**
783c6fd2807SJeff Garzik  *	ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
784c6fd2807SJeff Garzik  *	@xfer_mask: xfer_mask to unpack
785c6fd2807SJeff Garzik  *	@pio_mask: resulting pio_mask
786c6fd2807SJeff Garzik  *	@mwdma_mask: resulting mwdma_mask
787c6fd2807SJeff Garzik  *	@udma_mask: resulting udma_mask
788c6fd2807SJeff Garzik  *
789c6fd2807SJeff Garzik  *	Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
790c6fd2807SJeff Garzik  *	Any NULL distination masks will be ignored.
791c6fd2807SJeff Garzik  */
7927dc951aeSTejun Heo void ata_unpack_xfermask(unsigned long xfer_mask, unsigned long *pio_mask,
7937dc951aeSTejun Heo 			 unsigned long *mwdma_mask, unsigned long *udma_mask)
794c6fd2807SJeff Garzik {
795c6fd2807SJeff Garzik 	if (pio_mask)
796c6fd2807SJeff Garzik 		*pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
797c6fd2807SJeff Garzik 	if (mwdma_mask)
798c6fd2807SJeff Garzik 		*mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
799c6fd2807SJeff Garzik 	if (udma_mask)
800c6fd2807SJeff Garzik 		*udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
801c6fd2807SJeff Garzik }
802c6fd2807SJeff Garzik 
803c6fd2807SJeff Garzik static const struct ata_xfer_ent {
804c6fd2807SJeff Garzik 	int shift, bits;
805c6fd2807SJeff Garzik 	u8 base;
806c6fd2807SJeff Garzik } ata_xfer_tbl[] = {
80770cd071eSTejun Heo 	{ ATA_SHIFT_PIO, ATA_NR_PIO_MODES, XFER_PIO_0 },
80870cd071eSTejun Heo 	{ ATA_SHIFT_MWDMA, ATA_NR_MWDMA_MODES, XFER_MW_DMA_0 },
80970cd071eSTejun Heo 	{ ATA_SHIFT_UDMA, ATA_NR_UDMA_MODES, XFER_UDMA_0 },
810c6fd2807SJeff Garzik 	{ -1, },
811c6fd2807SJeff Garzik };
812c6fd2807SJeff Garzik 
813c6fd2807SJeff Garzik /**
814c6fd2807SJeff Garzik  *	ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
815c6fd2807SJeff Garzik  *	@xfer_mask: xfer_mask of interest
816c6fd2807SJeff Garzik  *
817c6fd2807SJeff Garzik  *	Return matching XFER_* value for @xfer_mask.  Only the highest
818c6fd2807SJeff Garzik  *	bit of @xfer_mask is considered.
819c6fd2807SJeff Garzik  *
820c6fd2807SJeff Garzik  *	LOCKING:
821c6fd2807SJeff Garzik  *	None.
822c6fd2807SJeff Garzik  *
823c6fd2807SJeff Garzik  *	RETURNS:
82470cd071eSTejun Heo  *	Matching XFER_* value, 0xff if no match found.
825c6fd2807SJeff Garzik  */
8267dc951aeSTejun Heo u8 ata_xfer_mask2mode(unsigned long xfer_mask)
827c6fd2807SJeff Garzik {
828c6fd2807SJeff Garzik 	int highbit = fls(xfer_mask) - 1;
829c6fd2807SJeff Garzik 	const struct ata_xfer_ent *ent;
830c6fd2807SJeff Garzik 
831c6fd2807SJeff Garzik 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
832c6fd2807SJeff Garzik 		if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
833c6fd2807SJeff Garzik 			return ent->base + highbit - ent->shift;
83470cd071eSTejun Heo 	return 0xff;
835c6fd2807SJeff Garzik }
836c6fd2807SJeff Garzik 
837c6fd2807SJeff Garzik /**
838c6fd2807SJeff Garzik  *	ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
839c6fd2807SJeff Garzik  *	@xfer_mode: XFER_* of interest
840c6fd2807SJeff Garzik  *
841c6fd2807SJeff Garzik  *	Return matching xfer_mask for @xfer_mode.
842c6fd2807SJeff Garzik  *
843c6fd2807SJeff Garzik  *	LOCKING:
844c6fd2807SJeff Garzik  *	None.
845c6fd2807SJeff Garzik  *
846c6fd2807SJeff Garzik  *	RETURNS:
847c6fd2807SJeff Garzik  *	Matching xfer_mask, 0 if no match found.
848c6fd2807SJeff Garzik  */
8497dc951aeSTejun Heo unsigned long ata_xfer_mode2mask(u8 xfer_mode)
850c6fd2807SJeff Garzik {
851c6fd2807SJeff Garzik 	const struct ata_xfer_ent *ent;
852c6fd2807SJeff Garzik 
853c6fd2807SJeff Garzik 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
854c6fd2807SJeff Garzik 		if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
85570cd071eSTejun Heo 			return ((2 << (ent->shift + xfer_mode - ent->base)) - 1)
85670cd071eSTejun Heo 				& ~((1 << ent->shift) - 1);
857c6fd2807SJeff Garzik 	return 0;
858c6fd2807SJeff Garzik }
859c6fd2807SJeff Garzik 
860c6fd2807SJeff Garzik /**
861c6fd2807SJeff Garzik  *	ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
862c6fd2807SJeff Garzik  *	@xfer_mode: XFER_* of interest
863c6fd2807SJeff Garzik  *
864c6fd2807SJeff Garzik  *	Return matching xfer_shift for @xfer_mode.
865c6fd2807SJeff Garzik  *
866c6fd2807SJeff Garzik  *	LOCKING:
867c6fd2807SJeff Garzik  *	None.
868c6fd2807SJeff Garzik  *
869c6fd2807SJeff Garzik  *	RETURNS:
870c6fd2807SJeff Garzik  *	Matching xfer_shift, -1 if no match found.
871c6fd2807SJeff Garzik  */
8727dc951aeSTejun Heo int ata_xfer_mode2shift(unsigned long xfer_mode)
873c6fd2807SJeff Garzik {
874c6fd2807SJeff Garzik 	const struct ata_xfer_ent *ent;
875c6fd2807SJeff Garzik 
876c6fd2807SJeff Garzik 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
877c6fd2807SJeff Garzik 		if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
878c6fd2807SJeff Garzik 			return ent->shift;
879c6fd2807SJeff Garzik 	return -1;
880c6fd2807SJeff Garzik }
881c6fd2807SJeff Garzik 
882c6fd2807SJeff Garzik /**
883c6fd2807SJeff Garzik  *	ata_mode_string - convert xfer_mask to string
884c6fd2807SJeff Garzik  *	@xfer_mask: mask of bits supported; only highest bit counts.
885c6fd2807SJeff Garzik  *
886c6fd2807SJeff Garzik  *	Determine string which represents the highest speed
887c6fd2807SJeff Garzik  *	(highest bit in @modemask).
888c6fd2807SJeff Garzik  *
889c6fd2807SJeff Garzik  *	LOCKING:
890c6fd2807SJeff Garzik  *	None.
891c6fd2807SJeff Garzik  *
892c6fd2807SJeff Garzik  *	RETURNS:
893c6fd2807SJeff Garzik  *	Constant C string representing highest speed listed in
894c6fd2807SJeff Garzik  *	@mode_mask, or the constant C string "<n/a>".
895c6fd2807SJeff Garzik  */
8967dc951aeSTejun Heo const char *ata_mode_string(unsigned long xfer_mask)
897c6fd2807SJeff Garzik {
898c6fd2807SJeff Garzik 	static const char * const xfer_mode_str[] = {
899c6fd2807SJeff Garzik 		"PIO0",
900c6fd2807SJeff Garzik 		"PIO1",
901c6fd2807SJeff Garzik 		"PIO2",
902c6fd2807SJeff Garzik 		"PIO3",
903c6fd2807SJeff Garzik 		"PIO4",
904b352e57dSAlan Cox 		"PIO5",
905b352e57dSAlan Cox 		"PIO6",
906c6fd2807SJeff Garzik 		"MWDMA0",
907c6fd2807SJeff Garzik 		"MWDMA1",
908c6fd2807SJeff Garzik 		"MWDMA2",
909b352e57dSAlan Cox 		"MWDMA3",
910b352e57dSAlan Cox 		"MWDMA4",
911c6fd2807SJeff Garzik 		"UDMA/16",
912c6fd2807SJeff Garzik 		"UDMA/25",
913c6fd2807SJeff Garzik 		"UDMA/33",
914c6fd2807SJeff Garzik 		"UDMA/44",
915c6fd2807SJeff Garzik 		"UDMA/66",
916c6fd2807SJeff Garzik 		"UDMA/100",
917c6fd2807SJeff Garzik 		"UDMA/133",
918c6fd2807SJeff Garzik 		"UDMA7",
919c6fd2807SJeff Garzik 	};
920c6fd2807SJeff Garzik 	int highbit;
921c6fd2807SJeff Garzik 
922c6fd2807SJeff Garzik 	highbit = fls(xfer_mask) - 1;
923c6fd2807SJeff Garzik 	if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
924c6fd2807SJeff Garzik 		return xfer_mode_str[highbit];
925c6fd2807SJeff Garzik 	return "<n/a>";
926c6fd2807SJeff Garzik }
927c6fd2807SJeff Garzik 
928c6fd2807SJeff Garzik static const char *sata_spd_string(unsigned int spd)
929c6fd2807SJeff Garzik {
930c6fd2807SJeff Garzik 	static const char * const spd_str[] = {
931c6fd2807SJeff Garzik 		"1.5 Gbps",
932c6fd2807SJeff Garzik 		"3.0 Gbps",
933c6fd2807SJeff Garzik 	};
934c6fd2807SJeff Garzik 
935c6fd2807SJeff Garzik 	if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
936c6fd2807SJeff Garzik 		return "<unknown>";
937c6fd2807SJeff Garzik 	return spd_str[spd - 1];
938c6fd2807SJeff Garzik }
939c6fd2807SJeff Garzik 
940c6fd2807SJeff Garzik void ata_dev_disable(struct ata_device *dev)
941c6fd2807SJeff Garzik {
94209d7f9b0STejun Heo 	if (ata_dev_enabled(dev)) {
9439af5c9c9STejun Heo 		if (ata_msg_drv(dev->link->ap))
944c6fd2807SJeff Garzik 			ata_dev_printk(dev, KERN_WARNING, "disabled\n");
945562f0c2dSTejun Heo 		ata_acpi_on_disable(dev);
9464ae72a1eSTejun Heo 		ata_down_xfermask_limit(dev, ATA_DNXFER_FORCE_PIO0 |
9474ae72a1eSTejun Heo 					     ATA_DNXFER_QUIET);
948c6fd2807SJeff Garzik 		dev->class++;
949c6fd2807SJeff Garzik 	}
950c6fd2807SJeff Garzik }
951c6fd2807SJeff Garzik 
952ca77329fSKristen Carlson Accardi static int ata_dev_set_dipm(struct ata_device *dev, enum link_pm policy)
953ca77329fSKristen Carlson Accardi {
954ca77329fSKristen Carlson Accardi 	struct ata_link *link = dev->link;
955ca77329fSKristen Carlson Accardi 	struct ata_port *ap = link->ap;
956ca77329fSKristen Carlson Accardi 	u32 scontrol;
957ca77329fSKristen Carlson Accardi 	unsigned int err_mask;
958ca77329fSKristen Carlson Accardi 	int rc;
959ca77329fSKristen Carlson Accardi 
960ca77329fSKristen Carlson Accardi 	/*
961ca77329fSKristen Carlson Accardi 	 * disallow DIPM for drivers which haven't set
962ca77329fSKristen Carlson Accardi 	 * ATA_FLAG_IPM.  This is because when DIPM is enabled,
963ca77329fSKristen Carlson Accardi 	 * phy ready will be set in the interrupt status on
964ca77329fSKristen Carlson Accardi 	 * state changes, which will cause some drivers to
965ca77329fSKristen Carlson Accardi 	 * think there are errors - additionally drivers will
966ca77329fSKristen Carlson Accardi 	 * need to disable hot plug.
967ca77329fSKristen Carlson Accardi 	 */
968ca77329fSKristen Carlson Accardi 	if (!(ap->flags & ATA_FLAG_IPM) || !ata_dev_enabled(dev)) {
969ca77329fSKristen Carlson Accardi 		ap->pm_policy = NOT_AVAILABLE;
970ca77329fSKristen Carlson Accardi 		return -EINVAL;
971ca77329fSKristen Carlson Accardi 	}
972ca77329fSKristen Carlson Accardi 
973ca77329fSKristen Carlson Accardi 	/*
974ca77329fSKristen Carlson Accardi 	 * For DIPM, we will only enable it for the
975ca77329fSKristen Carlson Accardi 	 * min_power setting.
976ca77329fSKristen Carlson Accardi 	 *
977ca77329fSKristen Carlson Accardi 	 * Why?  Because Disks are too stupid to know that
978ca77329fSKristen Carlson Accardi 	 * If the host rejects a request to go to SLUMBER
979ca77329fSKristen Carlson Accardi 	 * they should retry at PARTIAL, and instead it
980ca77329fSKristen Carlson Accardi 	 * just would give up.  So, for medium_power to
981ca77329fSKristen Carlson Accardi 	 * work at all, we need to only allow HIPM.
982ca77329fSKristen Carlson Accardi 	 */
983ca77329fSKristen Carlson Accardi 	rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
984ca77329fSKristen Carlson Accardi 	if (rc)
985ca77329fSKristen Carlson Accardi 		return rc;
986ca77329fSKristen Carlson Accardi 
987ca77329fSKristen Carlson Accardi 	switch (policy) {
988ca77329fSKristen Carlson Accardi 	case MIN_POWER:
989ca77329fSKristen Carlson Accardi 		/* no restrictions on IPM transitions */
990ca77329fSKristen Carlson Accardi 		scontrol &= ~(0x3 << 8);
991ca77329fSKristen Carlson Accardi 		rc = sata_scr_write(link, SCR_CONTROL, scontrol);
992ca77329fSKristen Carlson Accardi 		if (rc)
993ca77329fSKristen Carlson Accardi 			return rc;
994ca77329fSKristen Carlson Accardi 
995ca77329fSKristen Carlson Accardi 		/* enable DIPM */
996ca77329fSKristen Carlson Accardi 		if (dev->flags & ATA_DFLAG_DIPM)
997ca77329fSKristen Carlson Accardi 			err_mask = ata_dev_set_feature(dev,
998ca77329fSKristen Carlson Accardi 					SETFEATURES_SATA_ENABLE, SATA_DIPM);
999ca77329fSKristen Carlson Accardi 		break;
1000ca77329fSKristen Carlson Accardi 	case MEDIUM_POWER:
1001ca77329fSKristen Carlson Accardi 		/* allow IPM to PARTIAL */
1002ca77329fSKristen Carlson Accardi 		scontrol &= ~(0x1 << 8);
1003ca77329fSKristen Carlson Accardi 		scontrol |= (0x2 << 8);
1004ca77329fSKristen Carlson Accardi 		rc = sata_scr_write(link, SCR_CONTROL, scontrol);
1005ca77329fSKristen Carlson Accardi 		if (rc)
1006ca77329fSKristen Carlson Accardi 			return rc;
1007ca77329fSKristen Carlson Accardi 
1008f5456b63SKristen Carlson Accardi 		/*
1009f5456b63SKristen Carlson Accardi 		 * we don't have to disable DIPM since IPM flags
1010f5456b63SKristen Carlson Accardi 		 * disallow transitions to SLUMBER, which effectively
1011f5456b63SKristen Carlson Accardi 		 * disable DIPM if it does not support PARTIAL
1012f5456b63SKristen Carlson Accardi 		 */
1013ca77329fSKristen Carlson Accardi 		break;
1014ca77329fSKristen Carlson Accardi 	case NOT_AVAILABLE:
1015ca77329fSKristen Carlson Accardi 	case MAX_PERFORMANCE:
1016ca77329fSKristen Carlson Accardi 		/* disable all IPM transitions */
1017ca77329fSKristen Carlson Accardi 		scontrol |= (0x3 << 8);
1018ca77329fSKristen Carlson Accardi 		rc = sata_scr_write(link, SCR_CONTROL, scontrol);
1019ca77329fSKristen Carlson Accardi 		if (rc)
1020ca77329fSKristen Carlson Accardi 			return rc;
1021ca77329fSKristen Carlson Accardi 
1022f5456b63SKristen Carlson Accardi 		/*
1023f5456b63SKristen Carlson Accardi 		 * we don't have to disable DIPM since IPM flags
1024f5456b63SKristen Carlson Accardi 		 * disallow all transitions which effectively
1025f5456b63SKristen Carlson Accardi 		 * disable DIPM anyway.
1026f5456b63SKristen Carlson Accardi 		 */
1027ca77329fSKristen Carlson Accardi 		break;
1028ca77329fSKristen Carlson Accardi 	}
1029ca77329fSKristen Carlson Accardi 
1030ca77329fSKristen Carlson Accardi 	/* FIXME: handle SET FEATURES failure */
1031ca77329fSKristen Carlson Accardi 	(void) err_mask;
1032ca77329fSKristen Carlson Accardi 
1033ca77329fSKristen Carlson Accardi 	return 0;
1034ca77329fSKristen Carlson Accardi }
1035ca77329fSKristen Carlson Accardi 
1036ca77329fSKristen Carlson Accardi /**
1037ca77329fSKristen Carlson Accardi  *	ata_dev_enable_pm - enable SATA interface power management
103848166fd9SStephen Hemminger  *	@dev:  device to enable power management
103948166fd9SStephen Hemminger  *	@policy: the link power management policy
1040ca77329fSKristen Carlson Accardi  *
1041ca77329fSKristen Carlson Accardi  *	Enable SATA Interface power management.  This will enable
1042ca77329fSKristen Carlson Accardi  *	Device Interface Power Management (DIPM) for min_power
1043ca77329fSKristen Carlson Accardi  * 	policy, and then call driver specific callbacks for
1044ca77329fSKristen Carlson Accardi  *	enabling Host Initiated Power management.
1045ca77329fSKristen Carlson Accardi  *
1046ca77329fSKristen Carlson Accardi  *	Locking: Caller.
1047ca77329fSKristen Carlson Accardi  *	Returns: -EINVAL if IPM is not supported, 0 otherwise.
1048ca77329fSKristen Carlson Accardi  */
1049ca77329fSKristen Carlson Accardi void ata_dev_enable_pm(struct ata_device *dev, enum link_pm policy)
1050ca77329fSKristen Carlson Accardi {
1051ca77329fSKristen Carlson Accardi 	int rc = 0;
1052ca77329fSKristen Carlson Accardi 	struct ata_port *ap = dev->link->ap;
1053ca77329fSKristen Carlson Accardi 
1054ca77329fSKristen Carlson Accardi 	/* set HIPM first, then DIPM */
1055ca77329fSKristen Carlson Accardi 	if (ap->ops->enable_pm)
1056ca77329fSKristen Carlson Accardi 		rc = ap->ops->enable_pm(ap, policy);
1057ca77329fSKristen Carlson Accardi 	if (rc)
1058ca77329fSKristen Carlson Accardi 		goto enable_pm_out;
1059ca77329fSKristen Carlson Accardi 	rc = ata_dev_set_dipm(dev, policy);
1060ca77329fSKristen Carlson Accardi 
1061ca77329fSKristen Carlson Accardi enable_pm_out:
1062ca77329fSKristen Carlson Accardi 	if (rc)
1063ca77329fSKristen Carlson Accardi 		ap->pm_policy = MAX_PERFORMANCE;
1064ca77329fSKristen Carlson Accardi 	else
1065ca77329fSKristen Carlson Accardi 		ap->pm_policy = policy;
1066ca77329fSKristen Carlson Accardi 	return /* rc */;	/* hopefully we can use 'rc' eventually */
1067ca77329fSKristen Carlson Accardi }
1068ca77329fSKristen Carlson Accardi 
10691992a5edSStephen Rothwell #ifdef CONFIG_PM
1070ca77329fSKristen Carlson Accardi /**
1071ca77329fSKristen Carlson Accardi  *	ata_dev_disable_pm - disable SATA interface power management
107248166fd9SStephen Hemminger  *	@dev: device to disable power management
1073ca77329fSKristen Carlson Accardi  *
1074ca77329fSKristen Carlson Accardi  *	Disable SATA Interface power management.  This will disable
1075ca77329fSKristen Carlson Accardi  *	Device Interface Power Management (DIPM) without changing
1076ca77329fSKristen Carlson Accardi  * 	policy,  call driver specific callbacks for disabling Host
1077ca77329fSKristen Carlson Accardi  * 	Initiated Power management.
1078ca77329fSKristen Carlson Accardi  *
1079ca77329fSKristen Carlson Accardi  *	Locking: Caller.
1080ca77329fSKristen Carlson Accardi  *	Returns: void
1081ca77329fSKristen Carlson Accardi  */
1082ca77329fSKristen Carlson Accardi static void ata_dev_disable_pm(struct ata_device *dev)
1083ca77329fSKristen Carlson Accardi {
1084ca77329fSKristen Carlson Accardi 	struct ata_port *ap = dev->link->ap;
1085ca77329fSKristen Carlson Accardi 
1086ca77329fSKristen Carlson Accardi 	ata_dev_set_dipm(dev, MAX_PERFORMANCE);
1087ca77329fSKristen Carlson Accardi 	if (ap->ops->disable_pm)
1088ca77329fSKristen Carlson Accardi 		ap->ops->disable_pm(ap);
1089ca77329fSKristen Carlson Accardi }
10901992a5edSStephen Rothwell #endif	/* CONFIG_PM */
1091ca77329fSKristen Carlson Accardi 
1092ca77329fSKristen Carlson Accardi void ata_lpm_schedule(struct ata_port *ap, enum link_pm policy)
1093ca77329fSKristen Carlson Accardi {
1094ca77329fSKristen Carlson Accardi 	ap->pm_policy = policy;
10953ec25ebdSTejun Heo 	ap->link.eh_info.action |= ATA_EH_LPM;
1096ca77329fSKristen Carlson Accardi 	ap->link.eh_info.flags |= ATA_EHI_NO_AUTOPSY;
1097ca77329fSKristen Carlson Accardi 	ata_port_schedule_eh(ap);
1098ca77329fSKristen Carlson Accardi }
1099ca77329fSKristen Carlson Accardi 
11001992a5edSStephen Rothwell #ifdef CONFIG_PM
1101ca77329fSKristen Carlson Accardi static void ata_lpm_enable(struct ata_host *host)
1102ca77329fSKristen Carlson Accardi {
1103ca77329fSKristen Carlson Accardi 	struct ata_link *link;
1104ca77329fSKristen Carlson Accardi 	struct ata_port *ap;
1105ca77329fSKristen Carlson Accardi 	struct ata_device *dev;
1106ca77329fSKristen Carlson Accardi 	int i;
1107ca77329fSKristen Carlson Accardi 
1108ca77329fSKristen Carlson Accardi 	for (i = 0; i < host->n_ports; i++) {
1109ca77329fSKristen Carlson Accardi 		ap = host->ports[i];
1110ca77329fSKristen Carlson Accardi 		ata_port_for_each_link(link, ap) {
1111ca77329fSKristen Carlson Accardi 			ata_link_for_each_dev(dev, link)
1112ca77329fSKristen Carlson Accardi 				ata_dev_disable_pm(dev);
1113ca77329fSKristen Carlson Accardi 		}
1114ca77329fSKristen Carlson Accardi 	}
1115ca77329fSKristen Carlson Accardi }
1116ca77329fSKristen Carlson Accardi 
1117ca77329fSKristen Carlson Accardi static void ata_lpm_disable(struct ata_host *host)
1118ca77329fSKristen Carlson Accardi {
1119ca77329fSKristen Carlson Accardi 	int i;
1120ca77329fSKristen Carlson Accardi 
1121ca77329fSKristen Carlson Accardi 	for (i = 0; i < host->n_ports; i++) {
1122ca77329fSKristen Carlson Accardi 		struct ata_port *ap = host->ports[i];
1123ca77329fSKristen Carlson Accardi 		ata_lpm_schedule(ap, ap->pm_policy);
1124ca77329fSKristen Carlson Accardi 	}
1125ca77329fSKristen Carlson Accardi }
11261992a5edSStephen Rothwell #endif	/* CONFIG_PM */
1127ca77329fSKristen Carlson Accardi 
1128c6fd2807SJeff Garzik /**
1129c6fd2807SJeff Garzik  *	ata_dev_classify - determine device type based on ATA-spec signature
1130c6fd2807SJeff Garzik  *	@tf: ATA taskfile register set for device to be identified
1131c6fd2807SJeff Garzik  *
1132c6fd2807SJeff Garzik  *	Determine from taskfile register contents whether a device is
1133c6fd2807SJeff Garzik  *	ATA or ATAPI, as per "Signature and persistence" section
1134c6fd2807SJeff Garzik  *	of ATA/PI spec (volume 1, sect 5.14).
1135c6fd2807SJeff Garzik  *
1136c6fd2807SJeff Garzik  *	LOCKING:
1137c6fd2807SJeff Garzik  *	None.
1138c6fd2807SJeff Garzik  *
1139c6fd2807SJeff Garzik  *	RETURNS:
1140633273a3STejun Heo  *	Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP or
1141633273a3STejun Heo  *	%ATA_DEV_UNKNOWN the event of failure.
1142c6fd2807SJeff Garzik  */
1143c6fd2807SJeff Garzik unsigned int ata_dev_classify(const struct ata_taskfile *tf)
1144c6fd2807SJeff Garzik {
1145c6fd2807SJeff Garzik 	/* Apple's open source Darwin code hints that some devices only
1146c6fd2807SJeff Garzik 	 * put a proper signature into the LBA mid/high registers,
1147c6fd2807SJeff Garzik 	 * So, we only check those.  It's sufficient for uniqueness.
1148633273a3STejun Heo 	 *
1149633273a3STejun Heo 	 * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate
1150633273a3STejun Heo 	 * signatures for ATA and ATAPI devices attached on SerialATA,
1151633273a3STejun Heo 	 * 0x3c/0xc3 and 0x69/0x96 respectively.  However, SerialATA
1152633273a3STejun Heo 	 * spec has never mentioned about using different signatures
1153633273a3STejun Heo 	 * for ATA/ATAPI devices.  Then, Serial ATA II: Port
1154633273a3STejun Heo 	 * Multiplier specification began to use 0x69/0x96 to identify
1155633273a3STejun Heo 	 * port multpliers and 0x3c/0xc3 to identify SEMB device.
1156633273a3STejun Heo 	 * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and
1157633273a3STejun Heo 	 * 0x69/0x96 shortly and described them as reserved for
1158633273a3STejun Heo 	 * SerialATA.
1159633273a3STejun Heo 	 *
1160633273a3STejun Heo 	 * We follow the current spec and consider that 0x69/0x96
1161633273a3STejun Heo 	 * identifies a port multiplier and 0x3c/0xc3 a SEMB device.
1162c6fd2807SJeff Garzik 	 */
1163633273a3STejun Heo 	if ((tf->lbam == 0) && (tf->lbah == 0)) {
1164c6fd2807SJeff Garzik 		DPRINTK("found ATA device by sig\n");
1165c6fd2807SJeff Garzik 		return ATA_DEV_ATA;
1166c6fd2807SJeff Garzik 	}
1167c6fd2807SJeff Garzik 
1168633273a3STejun Heo 	if ((tf->lbam == 0x14) && (tf->lbah == 0xeb)) {
1169c6fd2807SJeff Garzik 		DPRINTK("found ATAPI device by sig\n");
1170c6fd2807SJeff Garzik 		return ATA_DEV_ATAPI;
1171c6fd2807SJeff Garzik 	}
1172c6fd2807SJeff Garzik 
1173633273a3STejun Heo 	if ((tf->lbam == 0x69) && (tf->lbah == 0x96)) {
1174633273a3STejun Heo 		DPRINTK("found PMP device by sig\n");
1175633273a3STejun Heo 		return ATA_DEV_PMP;
1176633273a3STejun Heo 	}
1177633273a3STejun Heo 
1178633273a3STejun Heo 	if ((tf->lbam == 0x3c) && (tf->lbah == 0xc3)) {
11792dcb407eSJeff Garzik 		printk(KERN_INFO "ata: SEMB device ignored\n");
1180633273a3STejun Heo 		return ATA_DEV_SEMB_UNSUP; /* not yet */
1181633273a3STejun Heo 	}
1182633273a3STejun Heo 
1183c6fd2807SJeff Garzik 	DPRINTK("unknown device\n");
1184c6fd2807SJeff Garzik 	return ATA_DEV_UNKNOWN;
1185c6fd2807SJeff Garzik }
1186c6fd2807SJeff Garzik 
1187c6fd2807SJeff Garzik /**
1188c6fd2807SJeff Garzik  *	ata_id_string - Convert IDENTIFY DEVICE page into string
1189c6fd2807SJeff Garzik  *	@id: IDENTIFY DEVICE results we will examine
1190c6fd2807SJeff Garzik  *	@s: string into which data is output
1191c6fd2807SJeff Garzik  *	@ofs: offset into identify device page
1192c6fd2807SJeff Garzik  *	@len: length of string to return. must be an even number.
1193c6fd2807SJeff Garzik  *
1194c6fd2807SJeff Garzik  *	The strings in the IDENTIFY DEVICE page are broken up into
1195c6fd2807SJeff Garzik  *	16-bit chunks.  Run through the string, and output each
1196c6fd2807SJeff Garzik  *	8-bit chunk linearly, regardless of platform.
1197c6fd2807SJeff Garzik  *
1198c6fd2807SJeff Garzik  *	LOCKING:
1199c6fd2807SJeff Garzik  *	caller.
1200c6fd2807SJeff Garzik  */
1201c6fd2807SJeff Garzik 
1202c6fd2807SJeff Garzik void ata_id_string(const u16 *id, unsigned char *s,
1203c6fd2807SJeff Garzik 		   unsigned int ofs, unsigned int len)
1204c6fd2807SJeff Garzik {
1205c6fd2807SJeff Garzik 	unsigned int c;
1206c6fd2807SJeff Garzik 
1207963e4975SAlan Cox 	BUG_ON(len & 1);
1208963e4975SAlan Cox 
1209c6fd2807SJeff Garzik 	while (len > 0) {
1210c6fd2807SJeff Garzik 		c = id[ofs] >> 8;
1211c6fd2807SJeff Garzik 		*s = c;
1212c6fd2807SJeff Garzik 		s++;
1213c6fd2807SJeff Garzik 
1214c6fd2807SJeff Garzik 		c = id[ofs] & 0xff;
1215c6fd2807SJeff Garzik 		*s = c;
1216c6fd2807SJeff Garzik 		s++;
1217c6fd2807SJeff Garzik 
1218c6fd2807SJeff Garzik 		ofs++;
1219c6fd2807SJeff Garzik 		len -= 2;
1220c6fd2807SJeff Garzik 	}
1221c6fd2807SJeff Garzik }
1222c6fd2807SJeff Garzik 
1223c6fd2807SJeff Garzik /**
1224c6fd2807SJeff Garzik  *	ata_id_c_string - Convert IDENTIFY DEVICE page into C string
1225c6fd2807SJeff Garzik  *	@id: IDENTIFY DEVICE results we will examine
1226c6fd2807SJeff Garzik  *	@s: string into which data is output
1227c6fd2807SJeff Garzik  *	@ofs: offset into identify device page
1228c6fd2807SJeff Garzik  *	@len: length of string to return. must be an odd number.
1229c6fd2807SJeff Garzik  *
1230c6fd2807SJeff Garzik  *	This function is identical to ata_id_string except that it
1231c6fd2807SJeff Garzik  *	trims trailing spaces and terminates the resulting string with
1232c6fd2807SJeff Garzik  *	null.  @len must be actual maximum length (even number) + 1.
1233c6fd2807SJeff Garzik  *
1234c6fd2807SJeff Garzik  *	LOCKING:
1235c6fd2807SJeff Garzik  *	caller.
1236c6fd2807SJeff Garzik  */
1237c6fd2807SJeff Garzik void ata_id_c_string(const u16 *id, unsigned char *s,
1238c6fd2807SJeff Garzik 		     unsigned int ofs, unsigned int len)
1239c6fd2807SJeff Garzik {
1240c6fd2807SJeff Garzik 	unsigned char *p;
1241c6fd2807SJeff Garzik 
1242c6fd2807SJeff Garzik 	ata_id_string(id, s, ofs, len - 1);
1243c6fd2807SJeff Garzik 
1244c6fd2807SJeff Garzik 	p = s + strnlen(s, len - 1);
1245c6fd2807SJeff Garzik 	while (p > s && p[-1] == ' ')
1246c6fd2807SJeff Garzik 		p--;
1247c6fd2807SJeff Garzik 	*p = '\0';
1248c6fd2807SJeff Garzik }
1249c6fd2807SJeff Garzik 
1250db6f8759STejun Heo static u64 ata_id_n_sectors(const u16 *id)
1251db6f8759STejun Heo {
1252db6f8759STejun Heo 	if (ata_id_has_lba(id)) {
1253db6f8759STejun Heo 		if (ata_id_has_lba48(id))
1254db6f8759STejun Heo 			return ata_id_u64(id, 100);
1255db6f8759STejun Heo 		else
1256db6f8759STejun Heo 			return ata_id_u32(id, 60);
1257db6f8759STejun Heo 	} else {
1258db6f8759STejun Heo 		if (ata_id_current_chs_valid(id))
1259db6f8759STejun Heo 			return ata_id_u32(id, 57);
1260db6f8759STejun Heo 		else
1261db6f8759STejun Heo 			return id[1] * id[3] * id[6];
1262db6f8759STejun Heo 	}
1263db6f8759STejun Heo }
1264db6f8759STejun Heo 
1265a5987e0aSTejun Heo u64 ata_tf_to_lba48(const struct ata_taskfile *tf)
12661e999736SAlan Cox {
12671e999736SAlan Cox 	u64 sectors = 0;
12681e999736SAlan Cox 
12691e999736SAlan Cox 	sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40;
12701e999736SAlan Cox 	sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32;
1271ba14a9c2SRoland Dreier 	sectors |= ((u64)(tf->hob_lbal & 0xff)) << 24;
12721e999736SAlan Cox 	sectors |= (tf->lbah & 0xff) << 16;
12731e999736SAlan Cox 	sectors |= (tf->lbam & 0xff) << 8;
12741e999736SAlan Cox 	sectors |= (tf->lbal & 0xff);
12751e999736SAlan Cox 
1276a5987e0aSTejun Heo 	return sectors;
12771e999736SAlan Cox }
12781e999736SAlan Cox 
1279a5987e0aSTejun Heo u64 ata_tf_to_lba(const struct ata_taskfile *tf)
12801e999736SAlan Cox {
12811e999736SAlan Cox 	u64 sectors = 0;
12821e999736SAlan Cox 
12831e999736SAlan Cox 	sectors |= (tf->device & 0x0f) << 24;
12841e999736SAlan Cox 	sectors |= (tf->lbah & 0xff) << 16;
12851e999736SAlan Cox 	sectors |= (tf->lbam & 0xff) << 8;
12861e999736SAlan Cox 	sectors |= (tf->lbal & 0xff);
12871e999736SAlan Cox 
1288a5987e0aSTejun Heo 	return sectors;
12891e999736SAlan Cox }
12901e999736SAlan Cox 
12911e999736SAlan Cox /**
1292c728a914STejun Heo  *	ata_read_native_max_address - Read native max address
1293c728a914STejun Heo  *	@dev: target device
1294c728a914STejun Heo  *	@max_sectors: out parameter for the result native max address
12951e999736SAlan Cox  *
1296c728a914STejun Heo  *	Perform an LBA48 or LBA28 native size query upon the device in
1297c728a914STejun Heo  *	question.
1298c728a914STejun Heo  *
1299c728a914STejun Heo  *	RETURNS:
1300c728a914STejun Heo  *	0 on success, -EACCES if command is aborted by the drive.
1301c728a914STejun Heo  *	-EIO on other errors.
13021e999736SAlan Cox  */
1303c728a914STejun Heo static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
13041e999736SAlan Cox {
1305c728a914STejun Heo 	unsigned int err_mask;
13061e999736SAlan Cox 	struct ata_taskfile tf;
1307c728a914STejun Heo 	int lba48 = ata_id_has_lba48(dev->id);
13081e999736SAlan Cox 
13091e999736SAlan Cox 	ata_tf_init(dev, &tf);
13101e999736SAlan Cox 
1311c728a914STejun Heo 	/* always clear all address registers */
13121e999736SAlan Cox 	tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1313c728a914STejun Heo 
1314c728a914STejun Heo 	if (lba48) {
1315c728a914STejun Heo 		tf.command = ATA_CMD_READ_NATIVE_MAX_EXT;
1316c728a914STejun Heo 		tf.flags |= ATA_TFLAG_LBA48;
1317c728a914STejun Heo 	} else
1318c728a914STejun Heo 		tf.command = ATA_CMD_READ_NATIVE_MAX;
1319c728a914STejun Heo 
13201e999736SAlan Cox 	tf.protocol |= ATA_PROT_NODATA;
1321c728a914STejun Heo 	tf.device |= ATA_LBA;
13221e999736SAlan Cox 
13232b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1324c728a914STejun Heo 	if (err_mask) {
1325c728a914STejun Heo 		ata_dev_printk(dev, KERN_WARNING, "failed to read native "
1326c728a914STejun Heo 			       "max address (err_mask=0x%x)\n", err_mask);
1327c728a914STejun Heo 		if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
1328c728a914STejun Heo 			return -EACCES;
1329c728a914STejun Heo 		return -EIO;
1330c728a914STejun Heo 	}
1331c728a914STejun Heo 
1332c728a914STejun Heo 	if (lba48)
1333a5987e0aSTejun Heo 		*max_sectors = ata_tf_to_lba48(&tf) + 1;
1334c728a914STejun Heo 	else
1335a5987e0aSTejun Heo 		*max_sectors = ata_tf_to_lba(&tf) + 1;
133693328e11SAlan Cox 	if (dev->horkage & ATA_HORKAGE_HPA_SIZE)
133793328e11SAlan Cox 		(*max_sectors)--;
13381e999736SAlan Cox 	return 0;
13391e999736SAlan Cox }
13401e999736SAlan Cox 
13411e999736SAlan Cox /**
1342c728a914STejun Heo  *	ata_set_max_sectors - Set max sectors
1343c728a914STejun Heo  *	@dev: target device
13446b38d1d1SRandy Dunlap  *	@new_sectors: new max sectors value to set for the device
13451e999736SAlan Cox  *
1346c728a914STejun Heo  *	Set max sectors of @dev to @new_sectors.
1347c728a914STejun Heo  *
1348c728a914STejun Heo  *	RETURNS:
1349c728a914STejun Heo  *	0 on success, -EACCES if command is aborted or denied (due to
1350c728a914STejun Heo  *	previous non-volatile SET_MAX) by the drive.  -EIO on other
1351c728a914STejun Heo  *	errors.
13521e999736SAlan Cox  */
135305027adcSTejun Heo static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
13541e999736SAlan Cox {
1355c728a914STejun Heo 	unsigned int err_mask;
13561e999736SAlan Cox 	struct ata_taskfile tf;
1357c728a914STejun Heo 	int lba48 = ata_id_has_lba48(dev->id);
13581e999736SAlan Cox 
13591e999736SAlan Cox 	new_sectors--;
13601e999736SAlan Cox 
13611e999736SAlan Cox 	ata_tf_init(dev, &tf);
13621e999736SAlan Cox 
1363c728a914STejun Heo 	tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
13641e999736SAlan Cox 
1365c728a914STejun Heo 	if (lba48) {
1366c728a914STejun Heo 		tf.command = ATA_CMD_SET_MAX_EXT;
1367c728a914STejun Heo 		tf.flags |= ATA_TFLAG_LBA48;
13681e999736SAlan Cox 
13691e999736SAlan Cox 		tf.hob_lbal = (new_sectors >> 24) & 0xff;
13701e999736SAlan Cox 		tf.hob_lbam = (new_sectors >> 32) & 0xff;
13711e999736SAlan Cox 		tf.hob_lbah = (new_sectors >> 40) & 0xff;
13721e582ba4STejun Heo 	} else {
13731e999736SAlan Cox 		tf.command = ATA_CMD_SET_MAX;
1374c728a914STejun Heo 
13751e582ba4STejun Heo 		tf.device |= (new_sectors >> 24) & 0xf;
13761e582ba4STejun Heo 	}
13771e582ba4STejun Heo 
13781e999736SAlan Cox 	tf.protocol |= ATA_PROT_NODATA;
1379c728a914STejun Heo 	tf.device |= ATA_LBA;
13801e999736SAlan Cox 
13811e999736SAlan Cox 	tf.lbal = (new_sectors >> 0) & 0xff;
13821e999736SAlan Cox 	tf.lbam = (new_sectors >> 8) & 0xff;
13831e999736SAlan Cox 	tf.lbah = (new_sectors >> 16) & 0xff;
13841e999736SAlan Cox 
13852b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1386c728a914STejun Heo 	if (err_mask) {
1387c728a914STejun Heo 		ata_dev_printk(dev, KERN_WARNING, "failed to set "
1388c728a914STejun Heo 			       "max address (err_mask=0x%x)\n", err_mask);
1389c728a914STejun Heo 		if (err_mask == AC_ERR_DEV &&
1390c728a914STejun Heo 		    (tf.feature & (ATA_ABORTED | ATA_IDNF)))
1391c728a914STejun Heo 			return -EACCES;
1392c728a914STejun Heo 		return -EIO;
1393c728a914STejun Heo 	}
1394c728a914STejun Heo 
13951e999736SAlan Cox 	return 0;
13961e999736SAlan Cox }
13971e999736SAlan Cox 
13981e999736SAlan Cox /**
13991e999736SAlan Cox  *	ata_hpa_resize		-	Resize a device with an HPA set
14001e999736SAlan Cox  *	@dev: Device to resize
14011e999736SAlan Cox  *
14021e999736SAlan Cox  *	Read the size of an LBA28 or LBA48 disk with HPA features and resize
14031e999736SAlan Cox  *	it if required to the full size of the media. The caller must check
14041e999736SAlan Cox  *	the drive has the HPA feature set enabled.
140505027adcSTejun Heo  *
140605027adcSTejun Heo  *	RETURNS:
140705027adcSTejun Heo  *	0 on success, -errno on failure.
14081e999736SAlan Cox  */
140905027adcSTejun Heo static int ata_hpa_resize(struct ata_device *dev)
14101e999736SAlan Cox {
141105027adcSTejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
141205027adcSTejun Heo 	int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
141305027adcSTejun Heo 	u64 sectors = ata_id_n_sectors(dev->id);
141405027adcSTejun Heo 	u64 native_sectors;
1415c728a914STejun Heo 	int rc;
14161e999736SAlan Cox 
141705027adcSTejun Heo 	/* do we need to do it? */
141805027adcSTejun Heo 	if (dev->class != ATA_DEV_ATA ||
141905027adcSTejun Heo 	    !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) ||
142005027adcSTejun Heo 	    (dev->horkage & ATA_HORKAGE_BROKEN_HPA))
1421c728a914STejun Heo 		return 0;
14221e999736SAlan Cox 
142305027adcSTejun Heo 	/* read native max address */
142405027adcSTejun Heo 	rc = ata_read_native_max_address(dev, &native_sectors);
142505027adcSTejun Heo 	if (rc) {
1426dda7aba1STejun Heo 		/* If device aborted the command or HPA isn't going to
1427dda7aba1STejun Heo 		 * be unlocked, skip HPA resizing.
142805027adcSTejun Heo 		 */
1429dda7aba1STejun Heo 		if (rc == -EACCES || !ata_ignore_hpa) {
143005027adcSTejun Heo 			ata_dev_printk(dev, KERN_WARNING, "HPA support seems "
1431dda7aba1STejun Heo 				       "broken, skipping HPA handling\n");
143205027adcSTejun Heo 			dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
143305027adcSTejun Heo 
143405027adcSTejun Heo 			/* we can continue if device aborted the command */
143505027adcSTejun Heo 			if (rc == -EACCES)
143605027adcSTejun Heo 				rc = 0;
143705027adcSTejun Heo 		}
143805027adcSTejun Heo 
143905027adcSTejun Heo 		return rc;
144005027adcSTejun Heo 	}
144105027adcSTejun Heo 
144205027adcSTejun Heo 	/* nothing to do? */
144305027adcSTejun Heo 	if (native_sectors <= sectors || !ata_ignore_hpa) {
144405027adcSTejun Heo 		if (!print_info || native_sectors == sectors)
144505027adcSTejun Heo 			return 0;
144605027adcSTejun Heo 
144705027adcSTejun Heo 		if (native_sectors > sectors)
14481e999736SAlan Cox 			ata_dev_printk(dev, KERN_INFO,
144905027adcSTejun Heo 				"HPA detected: current %llu, native %llu\n",
145005027adcSTejun Heo 				(unsigned long long)sectors,
145105027adcSTejun Heo 				(unsigned long long)native_sectors);
145205027adcSTejun Heo 		else if (native_sectors < sectors)
145305027adcSTejun Heo 			ata_dev_printk(dev, KERN_WARNING,
145405027adcSTejun Heo 				"native sectors (%llu) is smaller than "
145505027adcSTejun Heo 				"sectors (%llu)\n",
145605027adcSTejun Heo 				(unsigned long long)native_sectors,
145705027adcSTejun Heo 				(unsigned long long)sectors);
145805027adcSTejun Heo 		return 0;
14591e999736SAlan Cox 	}
146037301a55STejun Heo 
146105027adcSTejun Heo 	/* let's unlock HPA */
146205027adcSTejun Heo 	rc = ata_set_max_sectors(dev, native_sectors);
146305027adcSTejun Heo 	if (rc == -EACCES) {
146405027adcSTejun Heo 		/* if device aborted the command, skip HPA resizing */
146505027adcSTejun Heo 		ata_dev_printk(dev, KERN_WARNING, "device aborted resize "
146605027adcSTejun Heo 			       "(%llu -> %llu), skipping HPA handling\n",
146705027adcSTejun Heo 			       (unsigned long long)sectors,
146805027adcSTejun Heo 			       (unsigned long long)native_sectors);
146905027adcSTejun Heo 		dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
147005027adcSTejun Heo 		return 0;
147105027adcSTejun Heo 	} else if (rc)
147205027adcSTejun Heo 		return rc;
147305027adcSTejun Heo 
147405027adcSTejun Heo 	/* re-read IDENTIFY data */
147505027adcSTejun Heo 	rc = ata_dev_reread_id(dev, 0);
147605027adcSTejun Heo 	if (rc) {
147705027adcSTejun Heo 		ata_dev_printk(dev, KERN_ERR, "failed to re-read IDENTIFY "
147805027adcSTejun Heo 			       "data after HPA resizing\n");
147905027adcSTejun Heo 		return rc;
148005027adcSTejun Heo 	}
148105027adcSTejun Heo 
148205027adcSTejun Heo 	if (print_info) {
148305027adcSTejun Heo 		u64 new_sectors = ata_id_n_sectors(dev->id);
148405027adcSTejun Heo 		ata_dev_printk(dev, KERN_INFO,
148505027adcSTejun Heo 			"HPA unlocked: %llu -> %llu, native %llu\n",
148605027adcSTejun Heo 			(unsigned long long)sectors,
148705027adcSTejun Heo 			(unsigned long long)new_sectors,
148805027adcSTejun Heo 			(unsigned long long)native_sectors);
148905027adcSTejun Heo 	}
149005027adcSTejun Heo 
149105027adcSTejun Heo 	return 0;
14921e999736SAlan Cox }
14931e999736SAlan Cox 
1494c6fd2807SJeff Garzik /**
1495c6fd2807SJeff Garzik  *	ata_dump_id - IDENTIFY DEVICE info debugging output
1496c6fd2807SJeff Garzik  *	@id: IDENTIFY DEVICE page to dump
1497c6fd2807SJeff Garzik  *
1498c6fd2807SJeff Garzik  *	Dump selected 16-bit words from the given IDENTIFY DEVICE
1499c6fd2807SJeff Garzik  *	page.
1500c6fd2807SJeff Garzik  *
1501c6fd2807SJeff Garzik  *	LOCKING:
1502c6fd2807SJeff Garzik  *	caller.
1503c6fd2807SJeff Garzik  */
1504c6fd2807SJeff Garzik 
1505c6fd2807SJeff Garzik static inline void ata_dump_id(const u16 *id)
1506c6fd2807SJeff Garzik {
1507c6fd2807SJeff Garzik 	DPRINTK("49==0x%04x  "
1508c6fd2807SJeff Garzik 		"53==0x%04x  "
1509c6fd2807SJeff Garzik 		"63==0x%04x  "
1510c6fd2807SJeff Garzik 		"64==0x%04x  "
1511c6fd2807SJeff Garzik 		"75==0x%04x  \n",
1512c6fd2807SJeff Garzik 		id[49],
1513c6fd2807SJeff Garzik 		id[53],
1514c6fd2807SJeff Garzik 		id[63],
1515c6fd2807SJeff Garzik 		id[64],
1516c6fd2807SJeff Garzik 		id[75]);
1517c6fd2807SJeff Garzik 	DPRINTK("80==0x%04x  "
1518c6fd2807SJeff Garzik 		"81==0x%04x  "
1519c6fd2807SJeff Garzik 		"82==0x%04x  "
1520c6fd2807SJeff Garzik 		"83==0x%04x  "
1521c6fd2807SJeff Garzik 		"84==0x%04x  \n",
1522c6fd2807SJeff Garzik 		id[80],
1523c6fd2807SJeff Garzik 		id[81],
1524c6fd2807SJeff Garzik 		id[82],
1525c6fd2807SJeff Garzik 		id[83],
1526c6fd2807SJeff Garzik 		id[84]);
1527c6fd2807SJeff Garzik 	DPRINTK("88==0x%04x  "
1528c6fd2807SJeff Garzik 		"93==0x%04x\n",
1529c6fd2807SJeff Garzik 		id[88],
1530c6fd2807SJeff Garzik 		id[93]);
1531c6fd2807SJeff Garzik }
1532c6fd2807SJeff Garzik 
1533c6fd2807SJeff Garzik /**
1534c6fd2807SJeff Garzik  *	ata_id_xfermask - Compute xfermask from the given IDENTIFY data
1535c6fd2807SJeff Garzik  *	@id: IDENTIFY data to compute xfer mask from
1536c6fd2807SJeff Garzik  *
1537c6fd2807SJeff Garzik  *	Compute the xfermask for this device. This is not as trivial
1538c6fd2807SJeff Garzik  *	as it seems if we must consider early devices correctly.
1539c6fd2807SJeff Garzik  *
1540c6fd2807SJeff Garzik  *	FIXME: pre IDE drive timing (do we care ?).
1541c6fd2807SJeff Garzik  *
1542c6fd2807SJeff Garzik  *	LOCKING:
1543c6fd2807SJeff Garzik  *	None.
1544c6fd2807SJeff Garzik  *
1545c6fd2807SJeff Garzik  *	RETURNS:
1546c6fd2807SJeff Garzik  *	Computed xfermask
1547c6fd2807SJeff Garzik  */
15487dc951aeSTejun Heo unsigned long ata_id_xfermask(const u16 *id)
1549c6fd2807SJeff Garzik {
15507dc951aeSTejun Heo 	unsigned long pio_mask, mwdma_mask, udma_mask;
1551c6fd2807SJeff Garzik 
1552c6fd2807SJeff Garzik 	/* Usual case. Word 53 indicates word 64 is valid */
1553c6fd2807SJeff Garzik 	if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
1554c6fd2807SJeff Garzik 		pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
1555c6fd2807SJeff Garzik 		pio_mask <<= 3;
1556c6fd2807SJeff Garzik 		pio_mask |= 0x7;
1557c6fd2807SJeff Garzik 	} else {
1558c6fd2807SJeff Garzik 		/* If word 64 isn't valid then Word 51 high byte holds
1559c6fd2807SJeff Garzik 		 * the PIO timing number for the maximum. Turn it into
1560c6fd2807SJeff Garzik 		 * a mask.
1561c6fd2807SJeff Garzik 		 */
15627a0f1c8aSLennert Buytenhek 		u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
156346767aebSAlan Cox 		if (mode < 5)	/* Valid PIO range */
156446767aebSAlan Cox 			pio_mask = (2 << mode) - 1;
156546767aebSAlan Cox 		else
156646767aebSAlan Cox 			pio_mask = 1;
1567c6fd2807SJeff Garzik 
1568c6fd2807SJeff Garzik 		/* But wait.. there's more. Design your standards by
1569c6fd2807SJeff Garzik 		 * committee and you too can get a free iordy field to
1570c6fd2807SJeff Garzik 		 * process. However its the speeds not the modes that
1571c6fd2807SJeff Garzik 		 * are supported... Note drivers using the timing API
1572c6fd2807SJeff Garzik 		 * will get this right anyway
1573c6fd2807SJeff Garzik 		 */
1574c6fd2807SJeff Garzik 	}
1575c6fd2807SJeff Garzik 
1576c6fd2807SJeff Garzik 	mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
1577c6fd2807SJeff Garzik 
1578b352e57dSAlan Cox 	if (ata_id_is_cfa(id)) {
1579b352e57dSAlan Cox 		/*
1580b352e57dSAlan Cox 		 *	Process compact flash extended modes
1581b352e57dSAlan Cox 		 */
1582b352e57dSAlan Cox 		int pio = id[163] & 0x7;
1583b352e57dSAlan Cox 		int dma = (id[163] >> 3) & 7;
1584b352e57dSAlan Cox 
1585b352e57dSAlan Cox 		if (pio)
1586b352e57dSAlan Cox 			pio_mask |= (1 << 5);
1587b352e57dSAlan Cox 		if (pio > 1)
1588b352e57dSAlan Cox 			pio_mask |= (1 << 6);
1589b352e57dSAlan Cox 		if (dma)
1590b352e57dSAlan Cox 			mwdma_mask |= (1 << 3);
1591b352e57dSAlan Cox 		if (dma > 1)
1592b352e57dSAlan Cox 			mwdma_mask |= (1 << 4);
1593b352e57dSAlan Cox 	}
1594b352e57dSAlan Cox 
1595c6fd2807SJeff Garzik 	udma_mask = 0;
1596c6fd2807SJeff Garzik 	if (id[ATA_ID_FIELD_VALID] & (1 << 2))
1597c6fd2807SJeff Garzik 		udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
1598c6fd2807SJeff Garzik 
1599c6fd2807SJeff Garzik 	return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
1600c6fd2807SJeff Garzik }
1601c6fd2807SJeff Garzik 
1602c6fd2807SJeff Garzik /**
1603442eacc3SJeff Garzik  *	ata_pio_queue_task - Queue port_task
1604c6fd2807SJeff Garzik  *	@ap: The ata_port to queue port_task for
160565f27f38SDavid Howells  *	@data: data for @fn to use
1606341c2c95STejun Heo  *	@delay: delay time in msecs for workqueue function
1607c6fd2807SJeff Garzik  *
1608c6fd2807SJeff Garzik  *	Schedule @fn(@data) for execution after @delay jiffies using
1609c6fd2807SJeff Garzik  *	port_task.  There is one port_task per port and it's the
1610c6fd2807SJeff Garzik  *	user(low level driver)'s responsibility to make sure that only
1611c6fd2807SJeff Garzik  *	one task is active at any given time.
1612c6fd2807SJeff Garzik  *
1613c6fd2807SJeff Garzik  *	libata core layer takes care of synchronization between
1614442eacc3SJeff Garzik  *	port_task and EH.  ata_pio_queue_task() may be ignored for EH
1615c6fd2807SJeff Garzik  *	synchronization.
1616c6fd2807SJeff Garzik  *
1617c6fd2807SJeff Garzik  *	LOCKING:
1618c6fd2807SJeff Garzik  *	Inherited from caller.
1619c6fd2807SJeff Garzik  */
1620624d5c51STejun Heo void ata_pio_queue_task(struct ata_port *ap, void *data, unsigned long delay)
1621c6fd2807SJeff Garzik {
162265f27f38SDavid Howells 	ap->port_task_data = data;
1623c6fd2807SJeff Garzik 
162445a66c1cSOleg Nesterov 	/* may fail if ata_port_flush_task() in progress */
1625341c2c95STejun Heo 	queue_delayed_work(ata_wq, &ap->port_task, msecs_to_jiffies(delay));
1626c6fd2807SJeff Garzik }
1627c6fd2807SJeff Garzik 
1628c6fd2807SJeff Garzik /**
1629c6fd2807SJeff Garzik  *	ata_port_flush_task - Flush port_task
1630c6fd2807SJeff Garzik  *	@ap: The ata_port to flush port_task for
1631c6fd2807SJeff Garzik  *
1632c6fd2807SJeff Garzik  *	After this function completes, port_task is guranteed not to
1633c6fd2807SJeff Garzik  *	be running or scheduled.
1634c6fd2807SJeff Garzik  *
1635c6fd2807SJeff Garzik  *	LOCKING:
1636c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
1637c6fd2807SJeff Garzik  */
1638c6fd2807SJeff Garzik void ata_port_flush_task(struct ata_port *ap)
1639c6fd2807SJeff Garzik {
1640c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
1641c6fd2807SJeff Garzik 
164245a66c1cSOleg Nesterov 	cancel_rearming_delayed_work(&ap->port_task);
1643c6fd2807SJeff Garzik 
1644c6fd2807SJeff Garzik 	if (ata_msg_ctl(ap))
16457f5e4e8dSHarvey Harrison 		ata_port_printk(ap, KERN_DEBUG, "%s: EXIT\n", __func__);
1646c6fd2807SJeff Garzik }
1647c6fd2807SJeff Garzik 
16487102d230SAdrian Bunk static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
1649c6fd2807SJeff Garzik {
1650c6fd2807SJeff Garzik 	struct completion *waiting = qc->private_data;
1651c6fd2807SJeff Garzik 
1652c6fd2807SJeff Garzik 	complete(waiting);
1653c6fd2807SJeff Garzik }
1654c6fd2807SJeff Garzik 
1655c6fd2807SJeff Garzik /**
16562432697bSTejun Heo  *	ata_exec_internal_sg - execute libata internal command
1657c6fd2807SJeff Garzik  *	@dev: Device to which the command is sent
1658c6fd2807SJeff Garzik  *	@tf: Taskfile registers for the command and the result
1659c6fd2807SJeff Garzik  *	@cdb: CDB for packet command
1660c6fd2807SJeff Garzik  *	@dma_dir: Data tranfer direction of the command
16615c1ad8b3SRandy Dunlap  *	@sgl: sg list for the data buffer of the command
16622432697bSTejun Heo  *	@n_elem: Number of sg entries
16632b789108STejun Heo  *	@timeout: Timeout in msecs (0 for default)
1664c6fd2807SJeff Garzik  *
1665c6fd2807SJeff Garzik  *	Executes libata internal command with timeout.  @tf contains
1666c6fd2807SJeff Garzik  *	command on entry and result on return.  Timeout and error
1667c6fd2807SJeff Garzik  *	conditions are reported via return value.  No recovery action
1668c6fd2807SJeff Garzik  *	is taken after a command times out.  It's caller's duty to
1669c6fd2807SJeff Garzik  *	clean up after timeout.
1670c6fd2807SJeff Garzik  *
1671c6fd2807SJeff Garzik  *	LOCKING:
1672c6fd2807SJeff Garzik  *	None.  Should be called with kernel context, might sleep.
1673c6fd2807SJeff Garzik  *
1674c6fd2807SJeff Garzik  *	RETURNS:
1675c6fd2807SJeff Garzik  *	Zero on success, AC_ERR_* mask on failure
1676c6fd2807SJeff Garzik  */
16772432697bSTejun Heo unsigned ata_exec_internal_sg(struct ata_device *dev,
1678c6fd2807SJeff Garzik 			      struct ata_taskfile *tf, const u8 *cdb,
167987260216SJens Axboe 			      int dma_dir, struct scatterlist *sgl,
16802b789108STejun Heo 			      unsigned int n_elem, unsigned long timeout)
1681c6fd2807SJeff Garzik {
16829af5c9c9STejun Heo 	struct ata_link *link = dev->link;
16839af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
1684c6fd2807SJeff Garzik 	u8 command = tf->command;
168587fbc5a0STejun Heo 	int auto_timeout = 0;
1686c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
1687c6fd2807SJeff Garzik 	unsigned int tag, preempted_tag;
1688c6fd2807SJeff Garzik 	u32 preempted_sactive, preempted_qc_active;
1689da917d69STejun Heo 	int preempted_nr_active_links;
1690c6fd2807SJeff Garzik 	DECLARE_COMPLETION_ONSTACK(wait);
1691c6fd2807SJeff Garzik 	unsigned long flags;
1692c6fd2807SJeff Garzik 	unsigned int err_mask;
1693c6fd2807SJeff Garzik 	int rc;
1694c6fd2807SJeff Garzik 
1695c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1696c6fd2807SJeff Garzik 
1697c6fd2807SJeff Garzik 	/* no internal command while frozen */
1698c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_FROZEN) {
1699c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
1700c6fd2807SJeff Garzik 		return AC_ERR_SYSTEM;
1701c6fd2807SJeff Garzik 	}
1702c6fd2807SJeff Garzik 
1703c6fd2807SJeff Garzik 	/* initialize internal qc */
1704c6fd2807SJeff Garzik 
1705c6fd2807SJeff Garzik 	/* XXX: Tag 0 is used for drivers with legacy EH as some
1706c6fd2807SJeff Garzik 	 * drivers choke if any other tag is given.  This breaks
1707c6fd2807SJeff Garzik 	 * ata_tag_internal() test for those drivers.  Don't use new
1708c6fd2807SJeff Garzik 	 * EH stuff without converting to it.
1709c6fd2807SJeff Garzik 	 */
1710c6fd2807SJeff Garzik 	if (ap->ops->error_handler)
1711c6fd2807SJeff Garzik 		tag = ATA_TAG_INTERNAL;
1712c6fd2807SJeff Garzik 	else
1713c6fd2807SJeff Garzik 		tag = 0;
1714c6fd2807SJeff Garzik 
1715c6fd2807SJeff Garzik 	qc = __ata_qc_from_tag(ap, tag);
1716c6fd2807SJeff Garzik 
1717c6fd2807SJeff Garzik 	qc->tag = tag;
1718c6fd2807SJeff Garzik 	qc->scsicmd = NULL;
1719c6fd2807SJeff Garzik 	qc->ap = ap;
1720c6fd2807SJeff Garzik 	qc->dev = dev;
1721c6fd2807SJeff Garzik 	ata_qc_reinit(qc);
1722c6fd2807SJeff Garzik 
17239af5c9c9STejun Heo 	preempted_tag = link->active_tag;
17249af5c9c9STejun Heo 	preempted_sactive = link->sactive;
1725c6fd2807SJeff Garzik 	preempted_qc_active = ap->qc_active;
1726da917d69STejun Heo 	preempted_nr_active_links = ap->nr_active_links;
17279af5c9c9STejun Heo 	link->active_tag = ATA_TAG_POISON;
17289af5c9c9STejun Heo 	link->sactive = 0;
1729c6fd2807SJeff Garzik 	ap->qc_active = 0;
1730da917d69STejun Heo 	ap->nr_active_links = 0;
1731c6fd2807SJeff Garzik 
1732c6fd2807SJeff Garzik 	/* prepare & issue qc */
1733c6fd2807SJeff Garzik 	qc->tf = *tf;
1734c6fd2807SJeff Garzik 	if (cdb)
1735c6fd2807SJeff Garzik 		memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
1736c6fd2807SJeff Garzik 	qc->flags |= ATA_QCFLAG_RESULT_TF;
1737c6fd2807SJeff Garzik 	qc->dma_dir = dma_dir;
1738c6fd2807SJeff Garzik 	if (dma_dir != DMA_NONE) {
17392432697bSTejun Heo 		unsigned int i, buflen = 0;
174087260216SJens Axboe 		struct scatterlist *sg;
17412432697bSTejun Heo 
174287260216SJens Axboe 		for_each_sg(sgl, sg, n_elem, i)
174387260216SJens Axboe 			buflen += sg->length;
17442432697bSTejun Heo 
174587260216SJens Axboe 		ata_sg_init(qc, sgl, n_elem);
174649c80429SBrian King 		qc->nbytes = buflen;
1747c6fd2807SJeff Garzik 	}
1748c6fd2807SJeff Garzik 
1749c6fd2807SJeff Garzik 	qc->private_data = &wait;
1750c6fd2807SJeff Garzik 	qc->complete_fn = ata_qc_complete_internal;
1751c6fd2807SJeff Garzik 
1752c6fd2807SJeff Garzik 	ata_qc_issue(qc);
1753c6fd2807SJeff Garzik 
1754c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1755c6fd2807SJeff Garzik 
175687fbc5a0STejun Heo 	if (!timeout) {
175787fbc5a0STejun Heo 		if (ata_probe_timeout)
1758341c2c95STejun Heo 			timeout = ata_probe_timeout * 1000;
175987fbc5a0STejun Heo 		else {
176087fbc5a0STejun Heo 			timeout = ata_internal_cmd_timeout(dev, command);
176187fbc5a0STejun Heo 			auto_timeout = 1;
176287fbc5a0STejun Heo 		}
176387fbc5a0STejun Heo 	}
17642b789108STejun Heo 
17652b789108STejun Heo 	rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
1766c6fd2807SJeff Garzik 
1767c6fd2807SJeff Garzik 	ata_port_flush_task(ap);
1768c6fd2807SJeff Garzik 
1769c6fd2807SJeff Garzik 	if (!rc) {
1770c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
1771c6fd2807SJeff Garzik 
1772c6fd2807SJeff Garzik 		/* We're racing with irq here.  If we lose, the
1773c6fd2807SJeff Garzik 		 * following test prevents us from completing the qc
1774c6fd2807SJeff Garzik 		 * twice.  If we win, the port is frozen and will be
1775c6fd2807SJeff Garzik 		 * cleaned up by ->post_internal_cmd().
1776c6fd2807SJeff Garzik 		 */
1777c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_ACTIVE) {
1778c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_TIMEOUT;
1779c6fd2807SJeff Garzik 
1780c6fd2807SJeff Garzik 			if (ap->ops->error_handler)
1781c6fd2807SJeff Garzik 				ata_port_freeze(ap);
1782c6fd2807SJeff Garzik 			else
1783c6fd2807SJeff Garzik 				ata_qc_complete(qc);
1784c6fd2807SJeff Garzik 
1785c6fd2807SJeff Garzik 			if (ata_msg_warn(ap))
1786c6fd2807SJeff Garzik 				ata_dev_printk(dev, KERN_WARNING,
1787c6fd2807SJeff Garzik 					"qc timeout (cmd 0x%x)\n", command);
1788c6fd2807SJeff Garzik 		}
1789c6fd2807SJeff Garzik 
1790c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
1791c6fd2807SJeff Garzik 	}
1792c6fd2807SJeff Garzik 
1793c6fd2807SJeff Garzik 	/* do post_internal_cmd */
1794c6fd2807SJeff Garzik 	if (ap->ops->post_internal_cmd)
1795c6fd2807SJeff Garzik 		ap->ops->post_internal_cmd(qc);
1796c6fd2807SJeff Garzik 
1797a51d644aSTejun Heo 	/* perform minimal error analysis */
1798a51d644aSTejun Heo 	if (qc->flags & ATA_QCFLAG_FAILED) {
1799a51d644aSTejun Heo 		if (qc->result_tf.command & (ATA_ERR | ATA_DF))
1800a51d644aSTejun Heo 			qc->err_mask |= AC_ERR_DEV;
1801a51d644aSTejun Heo 
1802a51d644aSTejun Heo 		if (!qc->err_mask)
1803c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_OTHER;
1804a51d644aSTejun Heo 
1805a51d644aSTejun Heo 		if (qc->err_mask & ~AC_ERR_OTHER)
1806a51d644aSTejun Heo 			qc->err_mask &= ~AC_ERR_OTHER;
1807c6fd2807SJeff Garzik 	}
1808c6fd2807SJeff Garzik 
1809c6fd2807SJeff Garzik 	/* finish up */
1810c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1811c6fd2807SJeff Garzik 
1812c6fd2807SJeff Garzik 	*tf = qc->result_tf;
1813c6fd2807SJeff Garzik 	err_mask = qc->err_mask;
1814c6fd2807SJeff Garzik 
1815c6fd2807SJeff Garzik 	ata_qc_free(qc);
18169af5c9c9STejun Heo 	link->active_tag = preempted_tag;
18179af5c9c9STejun Heo 	link->sactive = preempted_sactive;
1818c6fd2807SJeff Garzik 	ap->qc_active = preempted_qc_active;
1819da917d69STejun Heo 	ap->nr_active_links = preempted_nr_active_links;
1820c6fd2807SJeff Garzik 
1821c6fd2807SJeff Garzik 	/* XXX - Some LLDDs (sata_mv) disable port on command failure.
1822c6fd2807SJeff Garzik 	 * Until those drivers are fixed, we detect the condition
1823c6fd2807SJeff Garzik 	 * here, fail the command with AC_ERR_SYSTEM and reenable the
1824c6fd2807SJeff Garzik 	 * port.
1825c6fd2807SJeff Garzik 	 *
1826c6fd2807SJeff Garzik 	 * Note that this doesn't change any behavior as internal
1827c6fd2807SJeff Garzik 	 * command failure results in disabling the device in the
1828c6fd2807SJeff Garzik 	 * higher layer for LLDDs without new reset/EH callbacks.
1829c6fd2807SJeff Garzik 	 *
1830c6fd2807SJeff Garzik 	 * Kill the following code as soon as those drivers are fixed.
1831c6fd2807SJeff Garzik 	 */
1832c6fd2807SJeff Garzik 	if (ap->flags & ATA_FLAG_DISABLED) {
1833c6fd2807SJeff Garzik 		err_mask |= AC_ERR_SYSTEM;
1834c6fd2807SJeff Garzik 		ata_port_probe(ap);
1835c6fd2807SJeff Garzik 	}
1836c6fd2807SJeff Garzik 
1837c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1838c6fd2807SJeff Garzik 
183987fbc5a0STejun Heo 	if ((err_mask & AC_ERR_TIMEOUT) && auto_timeout)
184087fbc5a0STejun Heo 		ata_internal_cmd_timed_out(dev, command);
184187fbc5a0STejun Heo 
1842c6fd2807SJeff Garzik 	return err_mask;
1843c6fd2807SJeff Garzik }
1844c6fd2807SJeff Garzik 
1845c6fd2807SJeff Garzik /**
184633480a0eSTejun Heo  *	ata_exec_internal - execute libata internal command
18472432697bSTejun Heo  *	@dev: Device to which the command is sent
18482432697bSTejun Heo  *	@tf: Taskfile registers for the command and the result
18492432697bSTejun Heo  *	@cdb: CDB for packet command
18502432697bSTejun Heo  *	@dma_dir: Data tranfer direction of the command
18512432697bSTejun Heo  *	@buf: Data buffer of the command
18522432697bSTejun Heo  *	@buflen: Length of data buffer
18532b789108STejun Heo  *	@timeout: Timeout in msecs (0 for default)
18542432697bSTejun Heo  *
18552432697bSTejun Heo  *	Wrapper around ata_exec_internal_sg() which takes simple
18562432697bSTejun Heo  *	buffer instead of sg list.
18572432697bSTejun Heo  *
18582432697bSTejun Heo  *	LOCKING:
18592432697bSTejun Heo  *	None.  Should be called with kernel context, might sleep.
18602432697bSTejun Heo  *
18612432697bSTejun Heo  *	RETURNS:
18622432697bSTejun Heo  *	Zero on success, AC_ERR_* mask on failure
18632432697bSTejun Heo  */
18642432697bSTejun Heo unsigned ata_exec_internal(struct ata_device *dev,
18652432697bSTejun Heo 			   struct ata_taskfile *tf, const u8 *cdb,
18662b789108STejun Heo 			   int dma_dir, void *buf, unsigned int buflen,
18672b789108STejun Heo 			   unsigned long timeout)
18682432697bSTejun Heo {
186933480a0eSTejun Heo 	struct scatterlist *psg = NULL, sg;
187033480a0eSTejun Heo 	unsigned int n_elem = 0;
18712432697bSTejun Heo 
187233480a0eSTejun Heo 	if (dma_dir != DMA_NONE) {
187333480a0eSTejun Heo 		WARN_ON(!buf);
18742432697bSTejun Heo 		sg_init_one(&sg, buf, buflen);
187533480a0eSTejun Heo 		psg = &sg;
187633480a0eSTejun Heo 		n_elem++;
187733480a0eSTejun Heo 	}
18782432697bSTejun Heo 
18792b789108STejun Heo 	return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem,
18802b789108STejun Heo 				    timeout);
18812432697bSTejun Heo }
18822432697bSTejun Heo 
18832432697bSTejun Heo /**
1884c6fd2807SJeff Garzik  *	ata_do_simple_cmd - execute simple internal command
1885c6fd2807SJeff Garzik  *	@dev: Device to which the command is sent
1886c6fd2807SJeff Garzik  *	@cmd: Opcode to execute
1887c6fd2807SJeff Garzik  *
1888c6fd2807SJeff Garzik  *	Execute a 'simple' command, that only consists of the opcode
1889c6fd2807SJeff Garzik  *	'cmd' itself, without filling any other registers
1890c6fd2807SJeff Garzik  *
1891c6fd2807SJeff Garzik  *	LOCKING:
1892c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1893c6fd2807SJeff Garzik  *
1894c6fd2807SJeff Garzik  *	RETURNS:
1895c6fd2807SJeff Garzik  *	Zero on success, AC_ERR_* mask on failure
1896c6fd2807SJeff Garzik  */
1897c6fd2807SJeff Garzik unsigned int ata_do_simple_cmd(struct ata_device *dev, u8 cmd)
1898c6fd2807SJeff Garzik {
1899c6fd2807SJeff Garzik 	struct ata_taskfile tf;
1900c6fd2807SJeff Garzik 
1901c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
1902c6fd2807SJeff Garzik 
1903c6fd2807SJeff Garzik 	tf.command = cmd;
1904c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_DEVICE;
1905c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
1906c6fd2807SJeff Garzik 
19072b789108STejun Heo 	return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1908c6fd2807SJeff Garzik }
1909c6fd2807SJeff Garzik 
1910c6fd2807SJeff Garzik /**
1911c6fd2807SJeff Garzik  *	ata_pio_need_iordy	-	check if iordy needed
1912c6fd2807SJeff Garzik  *	@adev: ATA device
1913c6fd2807SJeff Garzik  *
1914c6fd2807SJeff Garzik  *	Check if the current speed of the device requires IORDY. Used
1915c6fd2807SJeff Garzik  *	by various controllers for chip configuration.
1916c6fd2807SJeff Garzik  */
1917c6fd2807SJeff Garzik 
1918c6fd2807SJeff Garzik unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1919c6fd2807SJeff Garzik {
1920432729f0SAlan Cox 	/* Controller doesn't support  IORDY. Probably a pointless check
1921432729f0SAlan Cox 	   as the caller should know this */
19229af5c9c9STejun Heo 	if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
1923c6fd2807SJeff Garzik 		return 0;
1924432729f0SAlan Cox 	/* PIO3 and higher it is mandatory */
1925432729f0SAlan Cox 	if (adev->pio_mode > XFER_PIO_2)
1926c6fd2807SJeff Garzik 		return 1;
1927432729f0SAlan Cox 	/* We turn it on when possible */
1928432729f0SAlan Cox 	if (ata_id_has_iordy(adev->id))
1929432729f0SAlan Cox 		return 1;
1930432729f0SAlan Cox 	return 0;
1931432729f0SAlan Cox }
1932c6fd2807SJeff Garzik 
1933432729f0SAlan Cox /**
1934432729f0SAlan Cox  *	ata_pio_mask_no_iordy	-	Return the non IORDY mask
1935432729f0SAlan Cox  *	@adev: ATA device
1936432729f0SAlan Cox  *
1937432729f0SAlan Cox  *	Compute the highest mode possible if we are not using iordy. Return
1938432729f0SAlan Cox  *	-1 if no iordy mode is available.
1939432729f0SAlan Cox  */
1940432729f0SAlan Cox 
1941432729f0SAlan Cox static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
1942432729f0SAlan Cox {
1943c6fd2807SJeff Garzik 	/* If we have no drive specific rule, then PIO 2 is non IORDY */
1944c6fd2807SJeff Garzik 	if (adev->id[ATA_ID_FIELD_VALID] & 2) {	/* EIDE */
1945432729f0SAlan Cox 		u16 pio = adev->id[ATA_ID_EIDE_PIO];
1946c6fd2807SJeff Garzik 		/* Is the speed faster than the drive allows non IORDY ? */
1947c6fd2807SJeff Garzik 		if (pio) {
1948c6fd2807SJeff Garzik 			/* This is cycle times not frequency - watch the logic! */
1949c6fd2807SJeff Garzik 			if (pio > 240)	/* PIO2 is 240nS per cycle */
1950432729f0SAlan Cox 				return 3 << ATA_SHIFT_PIO;
1951432729f0SAlan Cox 			return 7 << ATA_SHIFT_PIO;
1952c6fd2807SJeff Garzik 		}
1953c6fd2807SJeff Garzik 	}
1954432729f0SAlan Cox 	return 3 << ATA_SHIFT_PIO;
1955c6fd2807SJeff Garzik }
1956c6fd2807SJeff Garzik 
1957c6fd2807SJeff Garzik /**
1958963e4975SAlan Cox  *	ata_do_dev_read_id		-	default ID read method
1959963e4975SAlan Cox  *	@dev: device
1960963e4975SAlan Cox  *	@tf: proposed taskfile
1961963e4975SAlan Cox  *	@id: data buffer
1962963e4975SAlan Cox  *
1963963e4975SAlan Cox  *	Issue the identify taskfile and hand back the buffer containing
1964963e4975SAlan Cox  *	identify data. For some RAID controllers and for pre ATA devices
1965963e4975SAlan Cox  *	this function is wrapped or replaced by the driver
1966963e4975SAlan Cox  */
1967963e4975SAlan Cox unsigned int ata_do_dev_read_id(struct ata_device *dev,
1968963e4975SAlan Cox 					struct ata_taskfile *tf, u16 *id)
1969963e4975SAlan Cox {
1970963e4975SAlan Cox 	return ata_exec_internal(dev, tf, NULL, DMA_FROM_DEVICE,
1971963e4975SAlan Cox 				     id, sizeof(id[0]) * ATA_ID_WORDS, 0);
1972963e4975SAlan Cox }
1973963e4975SAlan Cox 
1974963e4975SAlan Cox /**
1975c6fd2807SJeff Garzik  *	ata_dev_read_id - Read ID data from the specified device
1976c6fd2807SJeff Garzik  *	@dev: target device
1977c6fd2807SJeff Garzik  *	@p_class: pointer to class of the target device (may be changed)
1978bff04647STejun Heo  *	@flags: ATA_READID_* flags
1979c6fd2807SJeff Garzik  *	@id: buffer to read IDENTIFY data into
1980c6fd2807SJeff Garzik  *
1981c6fd2807SJeff Garzik  *	Read ID data from the specified device.  ATA_CMD_ID_ATA is
1982c6fd2807SJeff Garzik  *	performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
1983c6fd2807SJeff Garzik  *	devices.  This function also issues ATA_CMD_INIT_DEV_PARAMS
1984c6fd2807SJeff Garzik  *	for pre-ATA4 drives.
1985c6fd2807SJeff Garzik  *
198650a99018SAlan Cox  *	FIXME: ATA_CMD_ID_ATA is optional for early drives and right
198750a99018SAlan Cox  *	now we abort if we hit that case.
198850a99018SAlan Cox  *
1989c6fd2807SJeff Garzik  *	LOCKING:
1990c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
1991c6fd2807SJeff Garzik  *
1992c6fd2807SJeff Garzik  *	RETURNS:
1993c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
1994c6fd2807SJeff Garzik  */
1995c6fd2807SJeff Garzik int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
1996bff04647STejun Heo 		    unsigned int flags, u16 *id)
1997c6fd2807SJeff Garzik {
19989af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
1999c6fd2807SJeff Garzik 	unsigned int class = *p_class;
2000c6fd2807SJeff Garzik 	struct ata_taskfile tf;
2001c6fd2807SJeff Garzik 	unsigned int err_mask = 0;
2002c6fd2807SJeff Garzik 	const char *reason;
200354936f8bSTejun Heo 	int may_fallback = 1, tried_spinup = 0;
2004c6fd2807SJeff Garzik 	int rc;
2005c6fd2807SJeff Garzik 
2006c6fd2807SJeff Garzik 	if (ata_msg_ctl(ap))
20077f5e4e8dSHarvey Harrison 		ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __func__);
2008c6fd2807SJeff Garzik 
2009c6fd2807SJeff Garzik retry:
2010c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
2011c6fd2807SJeff Garzik 
2012c6fd2807SJeff Garzik 	switch (class) {
2013c6fd2807SJeff Garzik 	case ATA_DEV_ATA:
2014c6fd2807SJeff Garzik 		tf.command = ATA_CMD_ID_ATA;
2015c6fd2807SJeff Garzik 		break;
2016c6fd2807SJeff Garzik 	case ATA_DEV_ATAPI:
2017c6fd2807SJeff Garzik 		tf.command = ATA_CMD_ID_ATAPI;
2018c6fd2807SJeff Garzik 		break;
2019c6fd2807SJeff Garzik 	default:
2020c6fd2807SJeff Garzik 		rc = -ENODEV;
2021c6fd2807SJeff Garzik 		reason = "unsupported class";
2022c6fd2807SJeff Garzik 		goto err_out;
2023c6fd2807SJeff Garzik 	}
2024c6fd2807SJeff Garzik 
2025c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_PIO;
202681afe893STejun Heo 
202781afe893STejun Heo 	/* Some devices choke if TF registers contain garbage.  Make
202881afe893STejun Heo 	 * sure those are properly initialized.
202981afe893STejun Heo 	 */
203081afe893STejun Heo 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
203181afe893STejun Heo 
203281afe893STejun Heo 	/* Device presence detection is unreliable on some
203381afe893STejun Heo 	 * controllers.  Always poll IDENTIFY if available.
203481afe893STejun Heo 	 */
203581afe893STejun Heo 	tf.flags |= ATA_TFLAG_POLLING;
2036c6fd2807SJeff Garzik 
2037963e4975SAlan Cox 	if (ap->ops->read_id)
2038963e4975SAlan Cox 		err_mask = ap->ops->read_id(dev, &tf, id);
2039963e4975SAlan Cox 	else
2040963e4975SAlan Cox 		err_mask = ata_do_dev_read_id(dev, &tf, id);
2041963e4975SAlan Cox 
2042c6fd2807SJeff Garzik 	if (err_mask) {
2043800b3996STejun Heo 		if (err_mask & AC_ERR_NODEV_HINT) {
20441ffc151fSTejun Heo 			ata_dev_printk(dev, KERN_DEBUG,
20451ffc151fSTejun Heo 				       "NODEV after polling detection\n");
204655a8e2c8STejun Heo 			return -ENOENT;
204755a8e2c8STejun Heo 		}
204855a8e2c8STejun Heo 
20491ffc151fSTejun Heo 		if ((err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
20501ffc151fSTejun Heo 			/* Device or controller might have reported
20511ffc151fSTejun Heo 			 * the wrong device class.  Give a shot at the
20521ffc151fSTejun Heo 			 * other IDENTIFY if the current one is
20531ffc151fSTejun Heo 			 * aborted by the device.
205454936f8bSTejun Heo 			 */
20551ffc151fSTejun Heo 			if (may_fallback) {
205654936f8bSTejun Heo 				may_fallback = 0;
205754936f8bSTejun Heo 
205854936f8bSTejun Heo 				if (class == ATA_DEV_ATA)
205954936f8bSTejun Heo 					class = ATA_DEV_ATAPI;
206054936f8bSTejun Heo 				else
206154936f8bSTejun Heo 					class = ATA_DEV_ATA;
206254936f8bSTejun Heo 				goto retry;
206354936f8bSTejun Heo 			}
206454936f8bSTejun Heo 
20651ffc151fSTejun Heo 			/* Control reaches here iff the device aborted
20661ffc151fSTejun Heo 			 * both flavors of IDENTIFYs which happens
20671ffc151fSTejun Heo 			 * sometimes with phantom devices.
20681ffc151fSTejun Heo 			 */
20691ffc151fSTejun Heo 			ata_dev_printk(dev, KERN_DEBUG,
20701ffc151fSTejun Heo 				       "both IDENTIFYs aborted, assuming NODEV\n");
20711ffc151fSTejun Heo 			return -ENOENT;
20721ffc151fSTejun Heo 		}
20731ffc151fSTejun Heo 
2074c6fd2807SJeff Garzik 		rc = -EIO;
2075c6fd2807SJeff Garzik 		reason = "I/O error";
2076c6fd2807SJeff Garzik 		goto err_out;
2077c6fd2807SJeff Garzik 	}
2078c6fd2807SJeff Garzik 
207954936f8bSTejun Heo 	/* Falling back doesn't make sense if ID data was read
208054936f8bSTejun Heo 	 * successfully at least once.
208154936f8bSTejun Heo 	 */
208254936f8bSTejun Heo 	may_fallback = 0;
208354936f8bSTejun Heo 
2084c6fd2807SJeff Garzik 	swap_buf_le16(id, ATA_ID_WORDS);
2085c6fd2807SJeff Garzik 
2086c6fd2807SJeff Garzik 	/* sanity check */
2087c6fd2807SJeff Garzik 	rc = -EINVAL;
20886070068bSAlan Cox 	reason = "device reports invalid type";
20894a3381feSJeff Garzik 
20904a3381feSJeff Garzik 	if (class == ATA_DEV_ATA) {
20914a3381feSJeff Garzik 		if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
20924a3381feSJeff Garzik 			goto err_out;
20934a3381feSJeff Garzik 	} else {
20944a3381feSJeff Garzik 		if (ata_id_is_ata(id))
2095c6fd2807SJeff Garzik 			goto err_out;
2096c6fd2807SJeff Garzik 	}
2097c6fd2807SJeff Garzik 
2098169439c2SMark Lord 	if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
2099169439c2SMark Lord 		tried_spinup = 1;
2100169439c2SMark Lord 		/*
2101169439c2SMark Lord 		 * Drive powered-up in standby mode, and requires a specific
2102169439c2SMark Lord 		 * SET_FEATURES spin-up subcommand before it will accept
2103169439c2SMark Lord 		 * anything other than the original IDENTIFY command.
2104169439c2SMark Lord 		 */
2105218f3d30SJeff Garzik 		err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
2106fb0582f9SRyan Power 		if (err_mask && id[2] != 0x738c) {
2107169439c2SMark Lord 			rc = -EIO;
2108169439c2SMark Lord 			reason = "SPINUP failed";
2109169439c2SMark Lord 			goto err_out;
2110169439c2SMark Lord 		}
2111169439c2SMark Lord 		/*
2112169439c2SMark Lord 		 * If the drive initially returned incomplete IDENTIFY info,
2113169439c2SMark Lord 		 * we now must reissue the IDENTIFY command.
2114169439c2SMark Lord 		 */
2115169439c2SMark Lord 		if (id[2] == 0x37c8)
2116169439c2SMark Lord 			goto retry;
2117169439c2SMark Lord 	}
2118169439c2SMark Lord 
2119bff04647STejun Heo 	if ((flags & ATA_READID_POSTRESET) && class == ATA_DEV_ATA) {
2120c6fd2807SJeff Garzik 		/*
2121c6fd2807SJeff Garzik 		 * The exact sequence expected by certain pre-ATA4 drives is:
2122c6fd2807SJeff Garzik 		 * SRST RESET
212350a99018SAlan Cox 		 * IDENTIFY (optional in early ATA)
212450a99018SAlan Cox 		 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
2125c6fd2807SJeff Garzik 		 * anything else..
2126c6fd2807SJeff Garzik 		 * Some drives were very specific about that exact sequence.
212750a99018SAlan Cox 		 *
212850a99018SAlan Cox 		 * Note that ATA4 says lba is mandatory so the second check
212950a99018SAlan Cox 		 * shoud never trigger.
2130c6fd2807SJeff Garzik 		 */
2131c6fd2807SJeff Garzik 		if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
2132c6fd2807SJeff Garzik 			err_mask = ata_dev_init_params(dev, id[3], id[6]);
2133c6fd2807SJeff Garzik 			if (err_mask) {
2134c6fd2807SJeff Garzik 				rc = -EIO;
2135c6fd2807SJeff Garzik 				reason = "INIT_DEV_PARAMS failed";
2136c6fd2807SJeff Garzik 				goto err_out;
2137c6fd2807SJeff Garzik 			}
2138c6fd2807SJeff Garzik 
2139c6fd2807SJeff Garzik 			/* current CHS translation info (id[53-58]) might be
2140c6fd2807SJeff Garzik 			 * changed. reread the identify device info.
2141c6fd2807SJeff Garzik 			 */
2142bff04647STejun Heo 			flags &= ~ATA_READID_POSTRESET;
2143c6fd2807SJeff Garzik 			goto retry;
2144c6fd2807SJeff Garzik 		}
2145c6fd2807SJeff Garzik 	}
2146c6fd2807SJeff Garzik 
2147c6fd2807SJeff Garzik 	*p_class = class;
2148c6fd2807SJeff Garzik 
2149c6fd2807SJeff Garzik 	return 0;
2150c6fd2807SJeff Garzik 
2151c6fd2807SJeff Garzik  err_out:
2152c6fd2807SJeff Garzik 	if (ata_msg_warn(ap))
2153c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_WARNING, "failed to IDENTIFY "
2154c6fd2807SJeff Garzik 			       "(%s, err_mask=0x%x)\n", reason, err_mask);
2155c6fd2807SJeff Garzik 	return rc;
2156c6fd2807SJeff Garzik }
2157c6fd2807SJeff Garzik 
2158c6fd2807SJeff Garzik static inline u8 ata_dev_knobble(struct ata_device *dev)
2159c6fd2807SJeff Garzik {
21609af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
21619ce8e307SJens Axboe 
21629ce8e307SJens Axboe 	if (ata_dev_blacklisted(dev) & ATA_HORKAGE_BRIDGE_OK)
21639ce8e307SJens Axboe 		return 0;
21649ce8e307SJens Axboe 
21659af5c9c9STejun Heo 	return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
2166c6fd2807SJeff Garzik }
2167c6fd2807SJeff Garzik 
2168c6fd2807SJeff Garzik static void ata_dev_config_ncq(struct ata_device *dev,
2169c6fd2807SJeff Garzik 			       char *desc, size_t desc_sz)
2170c6fd2807SJeff Garzik {
21719af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
2172c6fd2807SJeff Garzik 	int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
2173c6fd2807SJeff Garzik 
2174c6fd2807SJeff Garzik 	if (!ata_id_has_ncq(dev->id)) {
2175c6fd2807SJeff Garzik 		desc[0] = '\0';
2176c6fd2807SJeff Garzik 		return;
2177c6fd2807SJeff Garzik 	}
217875683fe7STejun Heo 	if (dev->horkage & ATA_HORKAGE_NONCQ) {
21796919a0a6SAlan Cox 		snprintf(desc, desc_sz, "NCQ (not used)");
21806919a0a6SAlan Cox 		return;
21816919a0a6SAlan Cox 	}
2182c6fd2807SJeff Garzik 	if (ap->flags & ATA_FLAG_NCQ) {
2183cca3974eSJeff Garzik 		hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE - 1);
2184c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_NCQ;
2185c6fd2807SJeff Garzik 	}
2186c6fd2807SJeff Garzik 
2187c6fd2807SJeff Garzik 	if (hdepth >= ddepth)
2188c6fd2807SJeff Garzik 		snprintf(desc, desc_sz, "NCQ (depth %d)", ddepth);
2189c6fd2807SJeff Garzik 	else
2190c6fd2807SJeff Garzik 		snprintf(desc, desc_sz, "NCQ (depth %d/%d)", hdepth, ddepth);
2191c6fd2807SJeff Garzik }
2192c6fd2807SJeff Garzik 
2193c6fd2807SJeff Garzik /**
2194c6fd2807SJeff Garzik  *	ata_dev_configure - Configure the specified ATA/ATAPI device
2195c6fd2807SJeff Garzik  *	@dev: Target device to configure
2196c6fd2807SJeff Garzik  *
2197c6fd2807SJeff Garzik  *	Configure @dev according to @dev->id.  Generic and low-level
2198c6fd2807SJeff Garzik  *	driver specific fixups are also applied.
2199c6fd2807SJeff Garzik  *
2200c6fd2807SJeff Garzik  *	LOCKING:
2201c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
2202c6fd2807SJeff Garzik  *
2203c6fd2807SJeff Garzik  *	RETURNS:
2204c6fd2807SJeff Garzik  *	0 on success, -errno otherwise
2205c6fd2807SJeff Garzik  */
2206efdaedc4STejun Heo int ata_dev_configure(struct ata_device *dev)
2207c6fd2807SJeff Garzik {
22089af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
22099af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
22106746544cSTejun Heo 	int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
2211c6fd2807SJeff Garzik 	const u16 *id = dev->id;
22127dc951aeSTejun Heo 	unsigned long xfer_mask;
2213b352e57dSAlan Cox 	char revbuf[7];		/* XYZ-99\0 */
22143f64f565SEric D. Mudama 	char fwrevbuf[ATA_ID_FW_REV_LEN+1];
22153f64f565SEric D. Mudama 	char modelbuf[ATA_ID_PROD_LEN+1];
2216c6fd2807SJeff Garzik 	int rc;
2217c6fd2807SJeff Garzik 
2218c6fd2807SJeff Garzik 	if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
221944877b4eSTejun Heo 		ata_dev_printk(dev, KERN_INFO, "%s: ENTER/EXIT -- nodev\n",
22207f5e4e8dSHarvey Harrison 			       __func__);
2221c6fd2807SJeff Garzik 		return 0;
2222c6fd2807SJeff Garzik 	}
2223c6fd2807SJeff Garzik 
2224c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
22257f5e4e8dSHarvey Harrison 		ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __func__);
2226c6fd2807SJeff Garzik 
222775683fe7STejun Heo 	/* set horkage */
222875683fe7STejun Heo 	dev->horkage |= ata_dev_blacklisted(dev);
222933267325STejun Heo 	ata_force_horkage(dev);
223075683fe7STejun Heo 
223150af2fa1STejun Heo 	if (dev->horkage & ATA_HORKAGE_DISABLE) {
223250af2fa1STejun Heo 		ata_dev_printk(dev, KERN_INFO,
223350af2fa1STejun Heo 			       "unsupported device, disabling\n");
223450af2fa1STejun Heo 		ata_dev_disable(dev);
223550af2fa1STejun Heo 		return 0;
223650af2fa1STejun Heo 	}
223750af2fa1STejun Heo 
22382486fa56STejun Heo 	if ((!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) &&
22392486fa56STejun Heo 	    dev->class == ATA_DEV_ATAPI) {
22402486fa56STejun Heo 		ata_dev_printk(dev, KERN_WARNING,
22412486fa56STejun Heo 			"WARNING: ATAPI is %s, device ignored.\n",
22422486fa56STejun Heo 			atapi_enabled ? "not supported with this driver"
22432486fa56STejun Heo 				      : "disabled");
22442486fa56STejun Heo 		ata_dev_disable(dev);
22452486fa56STejun Heo 		return 0;
22462486fa56STejun Heo 	}
22472486fa56STejun Heo 
22486746544cSTejun Heo 	/* let ACPI work its magic */
22496746544cSTejun Heo 	rc = ata_acpi_on_devcfg(dev);
22506746544cSTejun Heo 	if (rc)
22516746544cSTejun Heo 		return rc;
225208573a86SKristen Carlson Accardi 
225305027adcSTejun Heo 	/* massage HPA, do it early as it might change IDENTIFY data */
225405027adcSTejun Heo 	rc = ata_hpa_resize(dev);
225505027adcSTejun Heo 	if (rc)
225605027adcSTejun Heo 		return rc;
225705027adcSTejun Heo 
2258c6fd2807SJeff Garzik 	/* print device capabilities */
2259c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2260c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_DEBUG,
2261c6fd2807SJeff Garzik 			       "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
2262c6fd2807SJeff Garzik 			       "85:%04x 86:%04x 87:%04x 88:%04x\n",
22637f5e4e8dSHarvey Harrison 			       __func__,
2264c6fd2807SJeff Garzik 			       id[49], id[82], id[83], id[84],
2265c6fd2807SJeff Garzik 			       id[85], id[86], id[87], id[88]);
2266c6fd2807SJeff Garzik 
2267c6fd2807SJeff Garzik 	/* initialize to-be-configured parameters */
2268c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_CFG_MASK;
2269c6fd2807SJeff Garzik 	dev->max_sectors = 0;
2270c6fd2807SJeff Garzik 	dev->cdb_len = 0;
2271c6fd2807SJeff Garzik 	dev->n_sectors = 0;
2272c6fd2807SJeff Garzik 	dev->cylinders = 0;
2273c6fd2807SJeff Garzik 	dev->heads = 0;
2274c6fd2807SJeff Garzik 	dev->sectors = 0;
2275c6fd2807SJeff Garzik 
2276c6fd2807SJeff Garzik 	/*
2277c6fd2807SJeff Garzik 	 * common ATA, ATAPI feature tests
2278c6fd2807SJeff Garzik 	 */
2279c6fd2807SJeff Garzik 
2280c6fd2807SJeff Garzik 	/* find max transfer mode; for printk only */
2281c6fd2807SJeff Garzik 	xfer_mask = ata_id_xfermask(id);
2282c6fd2807SJeff Garzik 
2283c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2284c6fd2807SJeff Garzik 		ata_dump_id(id);
2285c6fd2807SJeff Garzik 
2286ef143d57SAlbert Lee 	/* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
2287ef143d57SAlbert Lee 	ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
2288ef143d57SAlbert Lee 			sizeof(fwrevbuf));
2289ef143d57SAlbert Lee 
2290ef143d57SAlbert Lee 	ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
2291ef143d57SAlbert Lee 			sizeof(modelbuf));
2292ef143d57SAlbert Lee 
2293c6fd2807SJeff Garzik 	/* ATA-specific feature tests */
2294c6fd2807SJeff Garzik 	if (dev->class == ATA_DEV_ATA) {
2295b352e57dSAlan Cox 		if (ata_id_is_cfa(id)) {
2296b352e57dSAlan Cox 			if (id[162] & 1) /* CPRM may make this media unusable */
229744877b4eSTejun Heo 				ata_dev_printk(dev, KERN_WARNING,
229844877b4eSTejun Heo 					       "supports DRM functions and may "
229944877b4eSTejun Heo 					       "not be fully accessable.\n");
2300b352e57dSAlan Cox 			snprintf(revbuf, 7, "CFA");
2301ae8d4ee7SAlan Cox 		} else {
2302b352e57dSAlan Cox 			snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
2303ae8d4ee7SAlan Cox 			/* Warn the user if the device has TPM extensions */
2304ae8d4ee7SAlan Cox 			if (ata_id_has_tpm(id))
2305ae8d4ee7SAlan Cox 				ata_dev_printk(dev, KERN_WARNING,
2306ae8d4ee7SAlan Cox 					       "supports DRM functions and may "
2307ae8d4ee7SAlan Cox 					       "not be fully accessable.\n");
2308ae8d4ee7SAlan Cox 		}
2309b352e57dSAlan Cox 
2310c6fd2807SJeff Garzik 		dev->n_sectors = ata_id_n_sectors(id);
2311c6fd2807SJeff Garzik 
23123f64f565SEric D. Mudama 		if (dev->id[59] & 0x100)
23133f64f565SEric D. Mudama 			dev->multi_count = dev->id[59] & 0xff;
23143f64f565SEric D. Mudama 
2315c6fd2807SJeff Garzik 		if (ata_id_has_lba(id)) {
2316c6fd2807SJeff Garzik 			const char *lba_desc;
2317c6fd2807SJeff Garzik 			char ncq_desc[20];
2318c6fd2807SJeff Garzik 
2319c6fd2807SJeff Garzik 			lba_desc = "LBA";
2320c6fd2807SJeff Garzik 			dev->flags |= ATA_DFLAG_LBA;
2321c6fd2807SJeff Garzik 			if (ata_id_has_lba48(id)) {
2322c6fd2807SJeff Garzik 				dev->flags |= ATA_DFLAG_LBA48;
2323c6fd2807SJeff Garzik 				lba_desc = "LBA48";
23246fc49adbSTejun Heo 
23256fc49adbSTejun Heo 				if (dev->n_sectors >= (1UL << 28) &&
23266fc49adbSTejun Heo 				    ata_id_has_flush_ext(id))
23276fc49adbSTejun Heo 					dev->flags |= ATA_DFLAG_FLUSH_EXT;
2328c6fd2807SJeff Garzik 			}
2329c6fd2807SJeff Garzik 
2330c6fd2807SJeff Garzik 			/* config NCQ */
2331c6fd2807SJeff Garzik 			ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
2332c6fd2807SJeff Garzik 
2333c6fd2807SJeff Garzik 			/* print device info to dmesg */
23343f64f565SEric D. Mudama 			if (ata_msg_drv(ap) && print_info) {
23353f64f565SEric D. Mudama 				ata_dev_printk(dev, KERN_INFO,
23363f64f565SEric D. Mudama 					"%s: %s, %s, max %s\n",
23373f64f565SEric D. Mudama 					revbuf, modelbuf, fwrevbuf,
23383f64f565SEric D. Mudama 					ata_mode_string(xfer_mask));
23393f64f565SEric D. Mudama 				ata_dev_printk(dev, KERN_INFO,
23403f64f565SEric D. Mudama 					"%Lu sectors, multi %u: %s %s\n",
2341c6fd2807SJeff Garzik 					(unsigned long long)dev->n_sectors,
23423f64f565SEric D. Mudama 					dev->multi_count, lba_desc, ncq_desc);
23433f64f565SEric D. Mudama 			}
2344c6fd2807SJeff Garzik 		} else {
2345c6fd2807SJeff Garzik 			/* CHS */
2346c6fd2807SJeff Garzik 
2347c6fd2807SJeff Garzik 			/* Default translation */
2348c6fd2807SJeff Garzik 			dev->cylinders	= id[1];
2349c6fd2807SJeff Garzik 			dev->heads	= id[3];
2350c6fd2807SJeff Garzik 			dev->sectors	= id[6];
2351c6fd2807SJeff Garzik 
2352c6fd2807SJeff Garzik 			if (ata_id_current_chs_valid(id)) {
2353c6fd2807SJeff Garzik 				/* Current CHS translation is valid. */
2354c6fd2807SJeff Garzik 				dev->cylinders = id[54];
2355c6fd2807SJeff Garzik 				dev->heads     = id[55];
2356c6fd2807SJeff Garzik 				dev->sectors   = id[56];
2357c6fd2807SJeff Garzik 			}
2358c6fd2807SJeff Garzik 
2359c6fd2807SJeff Garzik 			/* print device info to dmesg */
23603f64f565SEric D. Mudama 			if (ata_msg_drv(ap) && print_info) {
2361c6fd2807SJeff Garzik 				ata_dev_printk(dev, KERN_INFO,
23623f64f565SEric D. Mudama 					"%s: %s, %s, max %s\n",
23633f64f565SEric D. Mudama 					revbuf,	modelbuf, fwrevbuf,
23643f64f565SEric D. Mudama 					ata_mode_string(xfer_mask));
23653f64f565SEric D. Mudama 				ata_dev_printk(dev, KERN_INFO,
23663f64f565SEric D. Mudama 					"%Lu sectors, multi %u, CHS %u/%u/%u\n",
23673f64f565SEric D. Mudama 					(unsigned long long)dev->n_sectors,
23683f64f565SEric D. Mudama 					dev->multi_count, dev->cylinders,
23693f64f565SEric D. Mudama 					dev->heads, dev->sectors);
23703f64f565SEric D. Mudama 			}
2371c6fd2807SJeff Garzik 		}
2372c6fd2807SJeff Garzik 
2373c6fd2807SJeff Garzik 		dev->cdb_len = 16;
2374c6fd2807SJeff Garzik 	}
2375c6fd2807SJeff Garzik 
2376c6fd2807SJeff Garzik 	/* ATAPI-specific feature tests */
2377c6fd2807SJeff Garzik 	else if (dev->class == ATA_DEV_ATAPI) {
2378854c73a2STejun Heo 		const char *cdb_intr_string = "";
2379854c73a2STejun Heo 		const char *atapi_an_string = "";
238091163006STejun Heo 		const char *dma_dir_string = "";
23817d77b247STejun Heo 		u32 sntf;
2382c6fd2807SJeff Garzik 
2383c6fd2807SJeff Garzik 		rc = atapi_cdb_len(id);
2384c6fd2807SJeff Garzik 		if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
2385c6fd2807SJeff Garzik 			if (ata_msg_warn(ap))
2386c6fd2807SJeff Garzik 				ata_dev_printk(dev, KERN_WARNING,
2387c6fd2807SJeff Garzik 					       "unsupported CDB len\n");
2388c6fd2807SJeff Garzik 			rc = -EINVAL;
2389c6fd2807SJeff Garzik 			goto err_out_nosup;
2390c6fd2807SJeff Garzik 		}
2391c6fd2807SJeff Garzik 		dev->cdb_len = (unsigned int) rc;
2392c6fd2807SJeff Garzik 
23937d77b247STejun Heo 		/* Enable ATAPI AN if both the host and device have
23947d77b247STejun Heo 		 * the support.  If PMP is attached, SNTF is required
23957d77b247STejun Heo 		 * to enable ATAPI AN to discern between PHY status
23967d77b247STejun Heo 		 * changed notifications and ATAPI ANs.
23979f45cbd3SKristen Carlson Accardi 		 */
23987d77b247STejun Heo 		if ((ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
2399071f44b1STejun Heo 		    (!sata_pmp_attached(ap) ||
24007d77b247STejun Heo 		     sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
2401854c73a2STejun Heo 			unsigned int err_mask;
2402854c73a2STejun Heo 
24039f45cbd3SKristen Carlson Accardi 			/* issue SET feature command to turn this on */
2404218f3d30SJeff Garzik 			err_mask = ata_dev_set_feature(dev,
2405218f3d30SJeff Garzik 					SETFEATURES_SATA_ENABLE, SATA_AN);
2406854c73a2STejun Heo 			if (err_mask)
24079f45cbd3SKristen Carlson Accardi 				ata_dev_printk(dev, KERN_ERR,
2408854c73a2STejun Heo 					"failed to enable ATAPI AN "
2409854c73a2STejun Heo 					"(err_mask=0x%x)\n", err_mask);
2410854c73a2STejun Heo 			else {
24119f45cbd3SKristen Carlson Accardi 				dev->flags |= ATA_DFLAG_AN;
2412854c73a2STejun Heo 				atapi_an_string = ", ATAPI AN";
2413854c73a2STejun Heo 			}
24149f45cbd3SKristen Carlson Accardi 		}
24159f45cbd3SKristen Carlson Accardi 
2416c6fd2807SJeff Garzik 		if (ata_id_cdb_intr(dev->id)) {
2417c6fd2807SJeff Garzik 			dev->flags |= ATA_DFLAG_CDB_INTR;
2418c6fd2807SJeff Garzik 			cdb_intr_string = ", CDB intr";
2419c6fd2807SJeff Garzik 		}
2420c6fd2807SJeff Garzik 
242191163006STejun Heo 		if (atapi_dmadir || atapi_id_dmadir(dev->id)) {
242291163006STejun Heo 			dev->flags |= ATA_DFLAG_DMADIR;
242391163006STejun Heo 			dma_dir_string = ", DMADIR";
242491163006STejun Heo 		}
242591163006STejun Heo 
2426c6fd2807SJeff Garzik 		/* print device info to dmesg */
2427c6fd2807SJeff Garzik 		if (ata_msg_drv(ap) && print_info)
2428ef143d57SAlbert Lee 			ata_dev_printk(dev, KERN_INFO,
242991163006STejun Heo 				       "ATAPI: %s, %s, max %s%s%s%s\n",
2430ef143d57SAlbert Lee 				       modelbuf, fwrevbuf,
2431c6fd2807SJeff Garzik 				       ata_mode_string(xfer_mask),
243291163006STejun Heo 				       cdb_intr_string, atapi_an_string,
243391163006STejun Heo 				       dma_dir_string);
2434c6fd2807SJeff Garzik 	}
2435c6fd2807SJeff Garzik 
2436914ed354STejun Heo 	/* determine max_sectors */
2437914ed354STejun Heo 	dev->max_sectors = ATA_MAX_SECTORS;
2438914ed354STejun Heo 	if (dev->flags & ATA_DFLAG_LBA48)
2439914ed354STejun Heo 		dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2440914ed354STejun Heo 
2441ca77329fSKristen Carlson Accardi 	if (!(dev->horkage & ATA_HORKAGE_IPM)) {
2442ca77329fSKristen Carlson Accardi 		if (ata_id_has_hipm(dev->id))
2443ca77329fSKristen Carlson Accardi 			dev->flags |= ATA_DFLAG_HIPM;
2444ca77329fSKristen Carlson Accardi 		if (ata_id_has_dipm(dev->id))
2445ca77329fSKristen Carlson Accardi 			dev->flags |= ATA_DFLAG_DIPM;
2446ca77329fSKristen Carlson Accardi 	}
2447ca77329fSKristen Carlson Accardi 
2448c5038fc0SAlan Cox 	/* Limit PATA drive on SATA cable bridge transfers to udma5,
2449c5038fc0SAlan Cox 	   200 sectors */
2450c6fd2807SJeff Garzik 	if (ata_dev_knobble(dev)) {
2451c6fd2807SJeff Garzik 		if (ata_msg_drv(ap) && print_info)
2452c6fd2807SJeff Garzik 			ata_dev_printk(dev, KERN_INFO,
2453c6fd2807SJeff Garzik 				       "applying bridge limits\n");
2454c6fd2807SJeff Garzik 		dev->udma_mask &= ATA_UDMA5;
2455c6fd2807SJeff Garzik 		dev->max_sectors = ATA_MAX_SECTORS;
2456c6fd2807SJeff Garzik 	}
2457c6fd2807SJeff Garzik 
2458f8d8e579STony Battersby 	if ((dev->class == ATA_DEV_ATAPI) &&
2459f442cd86SAlbert Lee 	    (atapi_command_packet_set(id) == TYPE_TAPE)) {
2460f8d8e579STony Battersby 		dev->max_sectors = ATA_MAX_SECTORS_TAPE;
2461f442cd86SAlbert Lee 		dev->horkage |= ATA_HORKAGE_STUCK_ERR;
2462f442cd86SAlbert Lee 	}
2463f8d8e579STony Battersby 
246475683fe7STejun Heo 	if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
246503ec52deSTejun Heo 		dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
246603ec52deSTejun Heo 					 dev->max_sectors);
246718d6e9d5SAlbert Lee 
2468ca77329fSKristen Carlson Accardi 	if (ata_dev_blacklisted(dev) & ATA_HORKAGE_IPM) {
2469ca77329fSKristen Carlson Accardi 		dev->horkage |= ATA_HORKAGE_IPM;
2470ca77329fSKristen Carlson Accardi 
2471ca77329fSKristen Carlson Accardi 		/* reset link pm_policy for this port to no pm */
2472ca77329fSKristen Carlson Accardi 		ap->pm_policy = MAX_PERFORMANCE;
2473ca77329fSKristen Carlson Accardi 	}
2474ca77329fSKristen Carlson Accardi 
2475c6fd2807SJeff Garzik 	if (ap->ops->dev_config)
2476cd0d3bbcSAlan 		ap->ops->dev_config(dev);
2477c6fd2807SJeff Garzik 
2478c5038fc0SAlan Cox 	if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2479c5038fc0SAlan Cox 		/* Let the user know. We don't want to disallow opens for
2480c5038fc0SAlan Cox 		   rescue purposes, or in case the vendor is just a blithering
2481c5038fc0SAlan Cox 		   idiot. Do this after the dev_config call as some controllers
2482c5038fc0SAlan Cox 		   with buggy firmware may want to avoid reporting false device
2483c5038fc0SAlan Cox 		   bugs */
2484c5038fc0SAlan Cox 
2485c5038fc0SAlan Cox 		if (print_info) {
2486c5038fc0SAlan Cox 			ata_dev_printk(dev, KERN_WARNING,
2487c5038fc0SAlan Cox "Drive reports diagnostics failure. This may indicate a drive\n");
2488c5038fc0SAlan Cox 			ata_dev_printk(dev, KERN_WARNING,
2489c5038fc0SAlan Cox "fault or invalid emulation. Contact drive vendor for information.\n");
2490c5038fc0SAlan Cox 		}
2491c5038fc0SAlan Cox 	}
2492c5038fc0SAlan Cox 
2493c6fd2807SJeff Garzik 	return 0;
2494c6fd2807SJeff Garzik 
2495c6fd2807SJeff Garzik err_out_nosup:
2496c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2497c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_DEBUG,
24987f5e4e8dSHarvey Harrison 			       "%s: EXIT, err\n", __func__);
2499c6fd2807SJeff Garzik 	return rc;
2500c6fd2807SJeff Garzik }
2501c6fd2807SJeff Garzik 
2502c6fd2807SJeff Garzik /**
25032e41e8e6SAlan Cox  *	ata_cable_40wire	-	return 40 wire cable type
2504be0d18dfSAlan Cox  *	@ap: port
2505be0d18dfSAlan Cox  *
25062e41e8e6SAlan Cox  *	Helper method for drivers which want to hardwire 40 wire cable
2507be0d18dfSAlan Cox  *	detection.
2508be0d18dfSAlan Cox  */
2509be0d18dfSAlan Cox 
2510be0d18dfSAlan Cox int ata_cable_40wire(struct ata_port *ap)
2511be0d18dfSAlan Cox {
2512be0d18dfSAlan Cox 	return ATA_CBL_PATA40;
2513be0d18dfSAlan Cox }
2514be0d18dfSAlan Cox 
2515be0d18dfSAlan Cox /**
25162e41e8e6SAlan Cox  *	ata_cable_80wire	-	return 80 wire cable type
2517be0d18dfSAlan Cox  *	@ap: port
2518be0d18dfSAlan Cox  *
25192e41e8e6SAlan Cox  *	Helper method for drivers which want to hardwire 80 wire cable
2520be0d18dfSAlan Cox  *	detection.
2521be0d18dfSAlan Cox  */
2522be0d18dfSAlan Cox 
2523be0d18dfSAlan Cox int ata_cable_80wire(struct ata_port *ap)
2524be0d18dfSAlan Cox {
2525be0d18dfSAlan Cox 	return ATA_CBL_PATA80;
2526be0d18dfSAlan Cox }
2527be0d18dfSAlan Cox 
2528be0d18dfSAlan Cox /**
2529be0d18dfSAlan Cox  *	ata_cable_unknown	-	return unknown PATA cable.
2530be0d18dfSAlan Cox  *	@ap: port
2531be0d18dfSAlan Cox  *
2532be0d18dfSAlan Cox  *	Helper method for drivers which have no PATA cable detection.
2533be0d18dfSAlan Cox  */
2534be0d18dfSAlan Cox 
2535be0d18dfSAlan Cox int ata_cable_unknown(struct ata_port *ap)
2536be0d18dfSAlan Cox {
2537be0d18dfSAlan Cox 	return ATA_CBL_PATA_UNK;
2538be0d18dfSAlan Cox }
2539be0d18dfSAlan Cox 
2540be0d18dfSAlan Cox /**
2541c88f90c3STejun Heo  *	ata_cable_ignore	-	return ignored PATA cable.
2542c88f90c3STejun Heo  *	@ap: port
2543c88f90c3STejun Heo  *
2544c88f90c3STejun Heo  *	Helper method for drivers which don't use cable type to limit
2545c88f90c3STejun Heo  *	transfer mode.
2546c88f90c3STejun Heo  */
2547c88f90c3STejun Heo int ata_cable_ignore(struct ata_port *ap)
2548c88f90c3STejun Heo {
2549c88f90c3STejun Heo 	return ATA_CBL_PATA_IGN;
2550c88f90c3STejun Heo }
2551c88f90c3STejun Heo 
2552c88f90c3STejun Heo /**
2553be0d18dfSAlan Cox  *	ata_cable_sata	-	return SATA cable type
2554be0d18dfSAlan Cox  *	@ap: port
2555be0d18dfSAlan Cox  *
2556be0d18dfSAlan Cox  *	Helper method for drivers which have SATA cables
2557be0d18dfSAlan Cox  */
2558be0d18dfSAlan Cox 
2559be0d18dfSAlan Cox int ata_cable_sata(struct ata_port *ap)
2560be0d18dfSAlan Cox {
2561be0d18dfSAlan Cox 	return ATA_CBL_SATA;
2562be0d18dfSAlan Cox }
2563be0d18dfSAlan Cox 
2564be0d18dfSAlan Cox /**
2565c6fd2807SJeff Garzik  *	ata_bus_probe - Reset and probe ATA bus
2566c6fd2807SJeff Garzik  *	@ap: Bus to probe
2567c6fd2807SJeff Garzik  *
2568c6fd2807SJeff Garzik  *	Master ATA bus probing function.  Initiates a hardware-dependent
2569c6fd2807SJeff Garzik  *	bus reset, then attempts to identify any devices found on
2570c6fd2807SJeff Garzik  *	the bus.
2571c6fd2807SJeff Garzik  *
2572c6fd2807SJeff Garzik  *	LOCKING:
2573c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
2574c6fd2807SJeff Garzik  *
2575c6fd2807SJeff Garzik  *	RETURNS:
2576c6fd2807SJeff Garzik  *	Zero on success, negative errno otherwise.
2577c6fd2807SJeff Garzik  */
2578c6fd2807SJeff Garzik 
2579c6fd2807SJeff Garzik int ata_bus_probe(struct ata_port *ap)
2580c6fd2807SJeff Garzik {
2581c6fd2807SJeff Garzik 	unsigned int classes[ATA_MAX_DEVICES];
2582c6fd2807SJeff Garzik 	int tries[ATA_MAX_DEVICES];
2583f58229f8STejun Heo 	int rc;
2584c6fd2807SJeff Garzik 	struct ata_device *dev;
2585c6fd2807SJeff Garzik 
2586c6fd2807SJeff Garzik 	ata_port_probe(ap);
2587c6fd2807SJeff Garzik 
2588f58229f8STejun Heo 	ata_link_for_each_dev(dev, &ap->link)
2589f58229f8STejun Heo 		tries[dev->devno] = ATA_PROBE_MAX_TRIES;
2590c6fd2807SJeff Garzik 
2591c6fd2807SJeff Garzik  retry:
2592cdeab114STejun Heo 	ata_link_for_each_dev(dev, &ap->link) {
2593cdeab114STejun Heo 		/* If we issue an SRST then an ATA drive (not ATAPI)
2594cdeab114STejun Heo 		 * may change configuration and be in PIO0 timing. If
2595cdeab114STejun Heo 		 * we do a hard reset (or are coming from power on)
2596cdeab114STejun Heo 		 * this is true for ATA or ATAPI. Until we've set a
2597cdeab114STejun Heo 		 * suitable controller mode we should not touch the
2598cdeab114STejun Heo 		 * bus as we may be talking too fast.
2599cdeab114STejun Heo 		 */
2600cdeab114STejun Heo 		dev->pio_mode = XFER_PIO_0;
2601cdeab114STejun Heo 
2602cdeab114STejun Heo 		/* If the controller has a pio mode setup function
2603cdeab114STejun Heo 		 * then use it to set the chipset to rights. Don't
2604cdeab114STejun Heo 		 * touch the DMA setup as that will be dealt with when
2605cdeab114STejun Heo 		 * configuring devices.
2606cdeab114STejun Heo 		 */
2607cdeab114STejun Heo 		if (ap->ops->set_piomode)
2608cdeab114STejun Heo 			ap->ops->set_piomode(ap, dev);
2609cdeab114STejun Heo 	}
2610cdeab114STejun Heo 
2611c6fd2807SJeff Garzik 	/* reset and determine device classes */
2612c6fd2807SJeff Garzik 	ap->ops->phy_reset(ap);
2613c6fd2807SJeff Garzik 
2614f58229f8STejun Heo 	ata_link_for_each_dev(dev, &ap->link) {
2615c6fd2807SJeff Garzik 		if (!(ap->flags & ATA_FLAG_DISABLED) &&
2616c6fd2807SJeff Garzik 		    dev->class != ATA_DEV_UNKNOWN)
2617c6fd2807SJeff Garzik 			classes[dev->devno] = dev->class;
2618c6fd2807SJeff Garzik 		else
2619c6fd2807SJeff Garzik 			classes[dev->devno] = ATA_DEV_NONE;
2620c6fd2807SJeff Garzik 
2621c6fd2807SJeff Garzik 		dev->class = ATA_DEV_UNKNOWN;
2622c6fd2807SJeff Garzik 	}
2623c6fd2807SJeff Garzik 
2624c6fd2807SJeff Garzik 	ata_port_probe(ap);
2625c6fd2807SJeff Garzik 
2626f31f0cc2SJeff Garzik 	/* read IDENTIFY page and configure devices. We have to do the identify
2627f31f0cc2SJeff Garzik 	   specific sequence bass-ackwards so that PDIAG- is released by
2628f31f0cc2SJeff Garzik 	   the slave device */
2629f31f0cc2SJeff Garzik 
2630a4ba7fe2SBartlomiej Zolnierkiewicz 	ata_link_for_each_dev_reverse(dev, &ap->link) {
2631f58229f8STejun Heo 		if (tries[dev->devno])
2632f58229f8STejun Heo 			dev->class = classes[dev->devno];
2633c6fd2807SJeff Garzik 
2634c6fd2807SJeff Garzik 		if (!ata_dev_enabled(dev))
2635c6fd2807SJeff Garzik 			continue;
2636c6fd2807SJeff Garzik 
2637bff04647STejun Heo 		rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2638bff04647STejun Heo 				     dev->id);
2639c6fd2807SJeff Garzik 		if (rc)
2640c6fd2807SJeff Garzik 			goto fail;
2641f31f0cc2SJeff Garzik 	}
2642f31f0cc2SJeff Garzik 
2643be0d18dfSAlan Cox 	/* Now ask for the cable type as PDIAG- should have been released */
2644be0d18dfSAlan Cox 	if (ap->ops->cable_detect)
2645be0d18dfSAlan Cox 		ap->cbl = ap->ops->cable_detect(ap);
2646be0d18dfSAlan Cox 
2647614fe29bSAlan Cox 	/* We may have SATA bridge glue hiding here irrespective of the
2648614fe29bSAlan Cox 	   reported cable types and sensed types */
2649614fe29bSAlan Cox 	ata_link_for_each_dev(dev, &ap->link) {
2650614fe29bSAlan Cox 		if (!ata_dev_enabled(dev))
2651614fe29bSAlan Cox 			continue;
2652614fe29bSAlan Cox 		/* SATA drives indicate we have a bridge. We don't know which
2653614fe29bSAlan Cox 		   end of the link the bridge is which is a problem */
2654614fe29bSAlan Cox 		if (ata_id_is_sata(dev->id))
2655614fe29bSAlan Cox 			ap->cbl = ATA_CBL_SATA;
2656614fe29bSAlan Cox 	}
2657614fe29bSAlan Cox 
2658f31f0cc2SJeff Garzik 	/* After the identify sequence we can now set up the devices. We do
2659f31f0cc2SJeff Garzik 	   this in the normal order so that the user doesn't get confused */
2660f31f0cc2SJeff Garzik 
2661f58229f8STejun Heo 	ata_link_for_each_dev(dev, &ap->link) {
2662f31f0cc2SJeff Garzik 		if (!ata_dev_enabled(dev))
2663f31f0cc2SJeff Garzik 			continue;
2664c6fd2807SJeff Garzik 
26659af5c9c9STejun Heo 		ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
2666efdaedc4STejun Heo 		rc = ata_dev_configure(dev);
26679af5c9c9STejun Heo 		ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
2668c6fd2807SJeff Garzik 		if (rc)
2669c6fd2807SJeff Garzik 			goto fail;
2670c6fd2807SJeff Garzik 	}
2671c6fd2807SJeff Garzik 
2672c6fd2807SJeff Garzik 	/* configure transfer mode */
26730260731fSTejun Heo 	rc = ata_set_mode(&ap->link, &dev);
26744ae72a1eSTejun Heo 	if (rc)
2675c6fd2807SJeff Garzik 		goto fail;
2676c6fd2807SJeff Garzik 
2677f58229f8STejun Heo 	ata_link_for_each_dev(dev, &ap->link)
2678f58229f8STejun Heo 		if (ata_dev_enabled(dev))
2679c6fd2807SJeff Garzik 			return 0;
2680c6fd2807SJeff Garzik 
2681c6fd2807SJeff Garzik 	/* no device present, disable port */
2682c6fd2807SJeff Garzik 	ata_port_disable(ap);
2683c6fd2807SJeff Garzik 	return -ENODEV;
2684c6fd2807SJeff Garzik 
2685c6fd2807SJeff Garzik  fail:
26864ae72a1eSTejun Heo 	tries[dev->devno]--;
26874ae72a1eSTejun Heo 
2688c6fd2807SJeff Garzik 	switch (rc) {
2689c6fd2807SJeff Garzik 	case -EINVAL:
26904ae72a1eSTejun Heo 		/* eeek, something went very wrong, give up */
2691c6fd2807SJeff Garzik 		tries[dev->devno] = 0;
2692c6fd2807SJeff Garzik 		break;
26934ae72a1eSTejun Heo 
26944ae72a1eSTejun Heo 	case -ENODEV:
26954ae72a1eSTejun Heo 		/* give it just one more chance */
26964ae72a1eSTejun Heo 		tries[dev->devno] = min(tries[dev->devno], 1);
2697c6fd2807SJeff Garzik 	case -EIO:
26984ae72a1eSTejun Heo 		if (tries[dev->devno] == 1) {
26994ae72a1eSTejun Heo 			/* This is the last chance, better to slow
27004ae72a1eSTejun Heo 			 * down than lose it.
27014ae72a1eSTejun Heo 			 */
2702936fd732STejun Heo 			sata_down_spd_limit(&ap->link);
27034ae72a1eSTejun Heo 			ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
27044ae72a1eSTejun Heo 		}
2705c6fd2807SJeff Garzik 	}
2706c6fd2807SJeff Garzik 
27074ae72a1eSTejun Heo 	if (!tries[dev->devno])
2708c6fd2807SJeff Garzik 		ata_dev_disable(dev);
2709c6fd2807SJeff Garzik 
2710c6fd2807SJeff Garzik 	goto retry;
2711c6fd2807SJeff Garzik }
2712c6fd2807SJeff Garzik 
2713c6fd2807SJeff Garzik /**
2714c6fd2807SJeff Garzik  *	ata_port_probe - Mark port as enabled
2715c6fd2807SJeff Garzik  *	@ap: Port for which we indicate enablement
2716c6fd2807SJeff Garzik  *
2717c6fd2807SJeff Garzik  *	Modify @ap data structure such that the system
2718c6fd2807SJeff Garzik  *	thinks that the entire port is enabled.
2719c6fd2807SJeff Garzik  *
2720cca3974eSJeff Garzik  *	LOCKING: host lock, or some other form of
2721c6fd2807SJeff Garzik  *	serialization.
2722c6fd2807SJeff Garzik  */
2723c6fd2807SJeff Garzik 
2724c6fd2807SJeff Garzik void ata_port_probe(struct ata_port *ap)
2725c6fd2807SJeff Garzik {
2726c6fd2807SJeff Garzik 	ap->flags &= ~ATA_FLAG_DISABLED;
2727c6fd2807SJeff Garzik }
2728c6fd2807SJeff Garzik 
2729c6fd2807SJeff Garzik /**
2730c6fd2807SJeff Garzik  *	sata_print_link_status - Print SATA link status
2731936fd732STejun Heo  *	@link: SATA link to printk link status about
2732c6fd2807SJeff Garzik  *
2733c6fd2807SJeff Garzik  *	This function prints link speed and status of a SATA link.
2734c6fd2807SJeff Garzik  *
2735c6fd2807SJeff Garzik  *	LOCKING:
2736c6fd2807SJeff Garzik  *	None.
2737c6fd2807SJeff Garzik  */
27386bdb4fc9SAdrian Bunk static void sata_print_link_status(struct ata_link *link)
2739c6fd2807SJeff Garzik {
2740c6fd2807SJeff Garzik 	u32 sstatus, scontrol, tmp;
2741c6fd2807SJeff Garzik 
2742936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus))
2743c6fd2807SJeff Garzik 		return;
2744936fd732STejun Heo 	sata_scr_read(link, SCR_CONTROL, &scontrol);
2745c6fd2807SJeff Garzik 
2746b1c72916STejun Heo 	if (ata_phys_link_online(link)) {
2747c6fd2807SJeff Garzik 		tmp = (sstatus >> 4) & 0xf;
2748936fd732STejun Heo 		ata_link_printk(link, KERN_INFO,
2749c6fd2807SJeff Garzik 				"SATA link up %s (SStatus %X SControl %X)\n",
2750c6fd2807SJeff Garzik 				sata_spd_string(tmp), sstatus, scontrol);
2751c6fd2807SJeff Garzik 	} else {
2752936fd732STejun Heo 		ata_link_printk(link, KERN_INFO,
2753c6fd2807SJeff Garzik 				"SATA link down (SStatus %X SControl %X)\n",
2754c6fd2807SJeff Garzik 				sstatus, scontrol);
2755c6fd2807SJeff Garzik 	}
2756c6fd2807SJeff Garzik }
2757c6fd2807SJeff Garzik 
2758c6fd2807SJeff Garzik /**
2759c6fd2807SJeff Garzik  *	ata_dev_pair		-	return other device on cable
2760c6fd2807SJeff Garzik  *	@adev: device
2761c6fd2807SJeff Garzik  *
2762c6fd2807SJeff Garzik  *	Obtain the other device on the same cable, or if none is
2763c6fd2807SJeff Garzik  *	present NULL is returned
2764c6fd2807SJeff Garzik  */
2765c6fd2807SJeff Garzik 
2766c6fd2807SJeff Garzik struct ata_device *ata_dev_pair(struct ata_device *adev)
2767c6fd2807SJeff Garzik {
27689af5c9c9STejun Heo 	struct ata_link *link = adev->link;
27699af5c9c9STejun Heo 	struct ata_device *pair = &link->device[1 - adev->devno];
2770c6fd2807SJeff Garzik 	if (!ata_dev_enabled(pair))
2771c6fd2807SJeff Garzik 		return NULL;
2772c6fd2807SJeff Garzik 	return pair;
2773c6fd2807SJeff Garzik }
2774c6fd2807SJeff Garzik 
2775c6fd2807SJeff Garzik /**
2776c6fd2807SJeff Garzik  *	ata_port_disable - Disable port.
2777c6fd2807SJeff Garzik  *	@ap: Port to be disabled.
2778c6fd2807SJeff Garzik  *
2779c6fd2807SJeff Garzik  *	Modify @ap data structure such that the system
2780c6fd2807SJeff Garzik  *	thinks that the entire port is disabled, and should
2781c6fd2807SJeff Garzik  *	never attempt to probe or communicate with devices
2782c6fd2807SJeff Garzik  *	on this port.
2783c6fd2807SJeff Garzik  *
2784cca3974eSJeff Garzik  *	LOCKING: host lock, or some other form of
2785c6fd2807SJeff Garzik  *	serialization.
2786c6fd2807SJeff Garzik  */
2787c6fd2807SJeff Garzik 
2788c6fd2807SJeff Garzik void ata_port_disable(struct ata_port *ap)
2789c6fd2807SJeff Garzik {
27909af5c9c9STejun Heo 	ap->link.device[0].class = ATA_DEV_NONE;
27919af5c9c9STejun Heo 	ap->link.device[1].class = ATA_DEV_NONE;
2792c6fd2807SJeff Garzik 	ap->flags |= ATA_FLAG_DISABLED;
2793c6fd2807SJeff Garzik }
2794c6fd2807SJeff Garzik 
2795c6fd2807SJeff Garzik /**
2796c6fd2807SJeff Garzik  *	sata_down_spd_limit - adjust SATA spd limit downward
2797936fd732STejun Heo  *	@link: Link to adjust SATA spd limit for
2798c6fd2807SJeff Garzik  *
2799936fd732STejun Heo  *	Adjust SATA spd limit of @link downward.  Note that this
2800c6fd2807SJeff Garzik  *	function only adjusts the limit.  The change must be applied
2801c6fd2807SJeff Garzik  *	using sata_set_spd().
2802c6fd2807SJeff Garzik  *
2803c6fd2807SJeff Garzik  *	LOCKING:
2804c6fd2807SJeff Garzik  *	Inherited from caller.
2805c6fd2807SJeff Garzik  *
2806c6fd2807SJeff Garzik  *	RETURNS:
2807c6fd2807SJeff Garzik  *	0 on success, negative errno on failure
2808c6fd2807SJeff Garzik  */
2809936fd732STejun Heo int sata_down_spd_limit(struct ata_link *link)
2810c6fd2807SJeff Garzik {
2811c6fd2807SJeff Garzik 	u32 sstatus, spd, mask;
2812c6fd2807SJeff Garzik 	int rc, highbit;
2813c6fd2807SJeff Garzik 
2814936fd732STejun Heo 	if (!sata_scr_valid(link))
2815008a7896STejun Heo 		return -EOPNOTSUPP;
2816008a7896STejun Heo 
2817008a7896STejun Heo 	/* If SCR can be read, use it to determine the current SPD.
2818936fd732STejun Heo 	 * If not, use cached value in link->sata_spd.
2819008a7896STejun Heo 	 */
2820936fd732STejun Heo 	rc = sata_scr_read(link, SCR_STATUS, &sstatus);
2821008a7896STejun Heo 	if (rc == 0)
2822008a7896STejun Heo 		spd = (sstatus >> 4) & 0xf;
2823008a7896STejun Heo 	else
2824936fd732STejun Heo 		spd = link->sata_spd;
2825c6fd2807SJeff Garzik 
2826936fd732STejun Heo 	mask = link->sata_spd_limit;
2827c6fd2807SJeff Garzik 	if (mask <= 1)
2828c6fd2807SJeff Garzik 		return -EINVAL;
2829008a7896STejun Heo 
2830008a7896STejun Heo 	/* unconditionally mask off the highest bit */
2831c6fd2807SJeff Garzik 	highbit = fls(mask) - 1;
2832c6fd2807SJeff Garzik 	mask &= ~(1 << highbit);
2833c6fd2807SJeff Garzik 
2834008a7896STejun Heo 	/* Mask off all speeds higher than or equal to the current
2835008a7896STejun Heo 	 * one.  Force 1.5Gbps if current SPD is not available.
2836008a7896STejun Heo 	 */
2837008a7896STejun Heo 	if (spd > 1)
2838008a7896STejun Heo 		mask &= (1 << (spd - 1)) - 1;
2839008a7896STejun Heo 	else
2840008a7896STejun Heo 		mask &= 1;
2841008a7896STejun Heo 
2842008a7896STejun Heo 	/* were we already at the bottom? */
2843c6fd2807SJeff Garzik 	if (!mask)
2844c6fd2807SJeff Garzik 		return -EINVAL;
2845c6fd2807SJeff Garzik 
2846936fd732STejun Heo 	link->sata_spd_limit = mask;
2847c6fd2807SJeff Garzik 
2848936fd732STejun Heo 	ata_link_printk(link, KERN_WARNING, "limiting SATA link speed to %s\n",
2849c6fd2807SJeff Garzik 			sata_spd_string(fls(mask)));
2850c6fd2807SJeff Garzik 
2851c6fd2807SJeff Garzik 	return 0;
2852c6fd2807SJeff Garzik }
2853c6fd2807SJeff Garzik 
2854936fd732STejun Heo static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
2855c6fd2807SJeff Garzik {
28565270222fSTejun Heo 	struct ata_link *host_link = &link->ap->link;
28575270222fSTejun Heo 	u32 limit, target, spd;
2858c6fd2807SJeff Garzik 
28595270222fSTejun Heo 	limit = link->sata_spd_limit;
28605270222fSTejun Heo 
28615270222fSTejun Heo 	/* Don't configure downstream link faster than upstream link.
28625270222fSTejun Heo 	 * It doesn't speed up anything and some PMPs choke on such
28635270222fSTejun Heo 	 * configuration.
28645270222fSTejun Heo 	 */
28655270222fSTejun Heo 	if (!ata_is_host_link(link) && host_link->sata_spd)
28665270222fSTejun Heo 		limit &= (1 << host_link->sata_spd) - 1;
28675270222fSTejun Heo 
28685270222fSTejun Heo 	if (limit == UINT_MAX)
28695270222fSTejun Heo 		target = 0;
2870c6fd2807SJeff Garzik 	else
28715270222fSTejun Heo 		target = fls(limit);
2872c6fd2807SJeff Garzik 
2873c6fd2807SJeff Garzik 	spd = (*scontrol >> 4) & 0xf;
28745270222fSTejun Heo 	*scontrol = (*scontrol & ~0xf0) | ((target & 0xf) << 4);
2875c6fd2807SJeff Garzik 
28765270222fSTejun Heo 	return spd != target;
2877c6fd2807SJeff Garzik }
2878c6fd2807SJeff Garzik 
2879c6fd2807SJeff Garzik /**
2880c6fd2807SJeff Garzik  *	sata_set_spd_needed - is SATA spd configuration needed
2881936fd732STejun Heo  *	@link: Link in question
2882c6fd2807SJeff Garzik  *
2883c6fd2807SJeff Garzik  *	Test whether the spd limit in SControl matches
2884936fd732STejun Heo  *	@link->sata_spd_limit.  This function is used to determine
2885c6fd2807SJeff Garzik  *	whether hardreset is necessary to apply SATA spd
2886c6fd2807SJeff Garzik  *	configuration.
2887c6fd2807SJeff Garzik  *
2888c6fd2807SJeff Garzik  *	LOCKING:
2889c6fd2807SJeff Garzik  *	Inherited from caller.
2890c6fd2807SJeff Garzik  *
2891c6fd2807SJeff Garzik  *	RETURNS:
2892c6fd2807SJeff Garzik  *	1 if SATA spd configuration is needed, 0 otherwise.
2893c6fd2807SJeff Garzik  */
28941dc55e87SAdrian Bunk static int sata_set_spd_needed(struct ata_link *link)
2895c6fd2807SJeff Garzik {
2896c6fd2807SJeff Garzik 	u32 scontrol;
2897c6fd2807SJeff Garzik 
2898936fd732STejun Heo 	if (sata_scr_read(link, SCR_CONTROL, &scontrol))
2899db64bcf3STejun Heo 		return 1;
2900c6fd2807SJeff Garzik 
2901936fd732STejun Heo 	return __sata_set_spd_needed(link, &scontrol);
2902c6fd2807SJeff Garzik }
2903c6fd2807SJeff Garzik 
2904c6fd2807SJeff Garzik /**
2905c6fd2807SJeff Garzik  *	sata_set_spd - set SATA spd according to spd limit
2906936fd732STejun Heo  *	@link: Link to set SATA spd for
2907c6fd2807SJeff Garzik  *
2908936fd732STejun Heo  *	Set SATA spd of @link according to sata_spd_limit.
2909c6fd2807SJeff Garzik  *
2910c6fd2807SJeff Garzik  *	LOCKING:
2911c6fd2807SJeff Garzik  *	Inherited from caller.
2912c6fd2807SJeff Garzik  *
2913c6fd2807SJeff Garzik  *	RETURNS:
2914c6fd2807SJeff Garzik  *	0 if spd doesn't need to be changed, 1 if spd has been
2915c6fd2807SJeff Garzik  *	changed.  Negative errno if SCR registers are inaccessible.
2916c6fd2807SJeff Garzik  */
2917936fd732STejun Heo int sata_set_spd(struct ata_link *link)
2918c6fd2807SJeff Garzik {
2919c6fd2807SJeff Garzik 	u32 scontrol;
2920c6fd2807SJeff Garzik 	int rc;
2921c6fd2807SJeff Garzik 
2922936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
2923c6fd2807SJeff Garzik 		return rc;
2924c6fd2807SJeff Garzik 
2925936fd732STejun Heo 	if (!__sata_set_spd_needed(link, &scontrol))
2926c6fd2807SJeff Garzik 		return 0;
2927c6fd2807SJeff Garzik 
2928936fd732STejun Heo 	if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
2929c6fd2807SJeff Garzik 		return rc;
2930c6fd2807SJeff Garzik 
2931c6fd2807SJeff Garzik 	return 1;
2932c6fd2807SJeff Garzik }
2933c6fd2807SJeff Garzik 
2934c6fd2807SJeff Garzik /*
2935c6fd2807SJeff Garzik  * This mode timing computation functionality is ported over from
2936c6fd2807SJeff Garzik  * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
2937c6fd2807SJeff Garzik  */
2938c6fd2807SJeff Garzik /*
2939b352e57dSAlan Cox  * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
2940c6fd2807SJeff Garzik  * These were taken from ATA/ATAPI-6 standard, rev 0a, except
2941b352e57dSAlan Cox  * for UDMA6, which is currently supported only by Maxtor drives.
2942b352e57dSAlan Cox  *
2943b352e57dSAlan Cox  * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0.
2944c6fd2807SJeff Garzik  */
2945c6fd2807SJeff Garzik 
2946c6fd2807SJeff Garzik static const struct ata_timing ata_timing[] = {
294770cd071eSTejun Heo /*	{ XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960,   0 }, */
294870cd071eSTejun Heo 	{ XFER_PIO_0,     70, 290, 240, 600, 165, 150, 600,   0 },
294970cd071eSTejun Heo 	{ XFER_PIO_1,     50, 290,  93, 383, 125, 100, 383,   0 },
295070cd071eSTejun Heo 	{ XFER_PIO_2,     30, 290,  40, 330, 100,  90, 240,   0 },
295170cd071eSTejun Heo 	{ XFER_PIO_3,     30,  80,  70, 180,  80,  70, 180,   0 },
295270cd071eSTejun Heo 	{ XFER_PIO_4,     25,  70,  25, 120,  70,  25, 120,   0 },
295370cd071eSTejun Heo 	{ XFER_PIO_5,     15,  65,  25, 100,  65,  25, 100,   0 },
295470cd071eSTejun Heo 	{ XFER_PIO_6,     10,  55,  20,  80,  55,  20,  80,   0 },
2955c6fd2807SJeff Garzik 
295670cd071eSTejun Heo 	{ XFER_SW_DMA_0, 120,   0,   0,   0, 480, 480, 960,   0 },
295770cd071eSTejun Heo 	{ XFER_SW_DMA_1,  90,   0,   0,   0, 240, 240, 480,   0 },
295870cd071eSTejun Heo 	{ XFER_SW_DMA_2,  60,   0,   0,   0, 120, 120, 240,   0 },
2959c6fd2807SJeff Garzik 
296070cd071eSTejun Heo 	{ XFER_MW_DMA_0,  60,   0,   0,   0, 215, 215, 480,   0 },
296170cd071eSTejun Heo 	{ XFER_MW_DMA_1,  45,   0,   0,   0,  80,  50, 150,   0 },
296270cd071eSTejun Heo 	{ XFER_MW_DMA_2,  25,   0,   0,   0,  70,  25, 120,   0 },
2963b352e57dSAlan Cox 	{ XFER_MW_DMA_3,  25,   0,   0,   0,  65,  25, 100,   0 },
296470cd071eSTejun Heo 	{ XFER_MW_DMA_4,  25,   0,   0,   0,  55,  20,  80,   0 },
2965c6fd2807SJeff Garzik 
2966c6fd2807SJeff Garzik /*	{ XFER_UDMA_SLOW,  0,   0,   0,   0,   0,   0,   0, 150 }, */
296770cd071eSTejun Heo 	{ XFER_UDMA_0,     0,   0,   0,   0,   0,   0,   0, 120 },
296870cd071eSTejun Heo 	{ XFER_UDMA_1,     0,   0,   0,   0,   0,   0,   0,  80 },
296970cd071eSTejun Heo 	{ XFER_UDMA_2,     0,   0,   0,   0,   0,   0,   0,  60 },
297070cd071eSTejun Heo 	{ XFER_UDMA_3,     0,   0,   0,   0,   0,   0,   0,  45 },
297170cd071eSTejun Heo 	{ XFER_UDMA_4,     0,   0,   0,   0,   0,   0,   0,  30 },
297270cd071eSTejun Heo 	{ XFER_UDMA_5,     0,   0,   0,   0,   0,   0,   0,  20 },
297370cd071eSTejun Heo 	{ XFER_UDMA_6,     0,   0,   0,   0,   0,   0,   0,  15 },
2974c6fd2807SJeff Garzik 
2975c6fd2807SJeff Garzik 	{ 0xFF }
2976c6fd2807SJeff Garzik };
2977c6fd2807SJeff Garzik 
2978c6fd2807SJeff Garzik #define ENOUGH(v, unit)		(((v)-1)/(unit)+1)
2979c6fd2807SJeff Garzik #define EZ(v, unit)		((v)?ENOUGH(v, unit):0)
2980c6fd2807SJeff Garzik 
2981c6fd2807SJeff Garzik static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
2982c6fd2807SJeff Garzik {
2983c6fd2807SJeff Garzik 	q->setup   = EZ(t->setup   * 1000,  T);
2984c6fd2807SJeff Garzik 	q->act8b   = EZ(t->act8b   * 1000,  T);
2985c6fd2807SJeff Garzik 	q->rec8b   = EZ(t->rec8b   * 1000,  T);
2986c6fd2807SJeff Garzik 	q->cyc8b   = EZ(t->cyc8b   * 1000,  T);
2987c6fd2807SJeff Garzik 	q->active  = EZ(t->active  * 1000,  T);
2988c6fd2807SJeff Garzik 	q->recover = EZ(t->recover * 1000,  T);
2989c6fd2807SJeff Garzik 	q->cycle   = EZ(t->cycle   * 1000,  T);
2990c6fd2807SJeff Garzik 	q->udma    = EZ(t->udma    * 1000, UT);
2991c6fd2807SJeff Garzik }
2992c6fd2807SJeff Garzik 
2993c6fd2807SJeff Garzik void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
2994c6fd2807SJeff Garzik 		      struct ata_timing *m, unsigned int what)
2995c6fd2807SJeff Garzik {
2996c6fd2807SJeff Garzik 	if (what & ATA_TIMING_SETUP  ) m->setup   = max(a->setup,   b->setup);
2997c6fd2807SJeff Garzik 	if (what & ATA_TIMING_ACT8B  ) m->act8b   = max(a->act8b,   b->act8b);
2998c6fd2807SJeff Garzik 	if (what & ATA_TIMING_REC8B  ) m->rec8b   = max(a->rec8b,   b->rec8b);
2999c6fd2807SJeff Garzik 	if (what & ATA_TIMING_CYC8B  ) m->cyc8b   = max(a->cyc8b,   b->cyc8b);
3000c6fd2807SJeff Garzik 	if (what & ATA_TIMING_ACTIVE ) m->active  = max(a->active,  b->active);
3001c6fd2807SJeff Garzik 	if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
3002c6fd2807SJeff Garzik 	if (what & ATA_TIMING_CYCLE  ) m->cycle   = max(a->cycle,   b->cycle);
3003c6fd2807SJeff Garzik 	if (what & ATA_TIMING_UDMA   ) m->udma    = max(a->udma,    b->udma);
3004c6fd2807SJeff Garzik }
3005c6fd2807SJeff Garzik 
30066357357cSTejun Heo const struct ata_timing *ata_timing_find_mode(u8 xfer_mode)
3007c6fd2807SJeff Garzik {
300870cd071eSTejun Heo 	const struct ata_timing *t = ata_timing;
3009c6fd2807SJeff Garzik 
301070cd071eSTejun Heo 	while (xfer_mode > t->mode)
301170cd071eSTejun Heo 		t++;
301270cd071eSTejun Heo 
301370cd071eSTejun Heo 	if (xfer_mode == t->mode)
3014c6fd2807SJeff Garzik 		return t;
301570cd071eSTejun Heo 	return NULL;
3016c6fd2807SJeff Garzik }
3017c6fd2807SJeff Garzik 
3018c6fd2807SJeff Garzik int ata_timing_compute(struct ata_device *adev, unsigned short speed,
3019c6fd2807SJeff Garzik 		       struct ata_timing *t, int T, int UT)
3020c6fd2807SJeff Garzik {
3021c6fd2807SJeff Garzik 	const struct ata_timing *s;
3022c6fd2807SJeff Garzik 	struct ata_timing p;
3023c6fd2807SJeff Garzik 
3024c6fd2807SJeff Garzik 	/*
3025c6fd2807SJeff Garzik 	 * Find the mode.
3026c6fd2807SJeff Garzik 	 */
3027c6fd2807SJeff Garzik 
3028c6fd2807SJeff Garzik 	if (!(s = ata_timing_find_mode(speed)))
3029c6fd2807SJeff Garzik 		return -EINVAL;
3030c6fd2807SJeff Garzik 
3031c6fd2807SJeff Garzik 	memcpy(t, s, sizeof(*s));
3032c6fd2807SJeff Garzik 
3033c6fd2807SJeff Garzik 	/*
3034c6fd2807SJeff Garzik 	 * If the drive is an EIDE drive, it can tell us it needs extended
3035c6fd2807SJeff Garzik 	 * PIO/MW_DMA cycle timing.
3036c6fd2807SJeff Garzik 	 */
3037c6fd2807SJeff Garzik 
3038c6fd2807SJeff Garzik 	if (adev->id[ATA_ID_FIELD_VALID] & 2) {	/* EIDE drive */
3039c6fd2807SJeff Garzik 		memset(&p, 0, sizeof(p));
3040c6fd2807SJeff Garzik 		if (speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
3041c6fd2807SJeff Garzik 			if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
3042c6fd2807SJeff Garzik 					    else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
3043c6fd2807SJeff Garzik 		} else if (speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
3044c6fd2807SJeff Garzik 			p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
3045c6fd2807SJeff Garzik 		}
3046c6fd2807SJeff Garzik 		ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
3047c6fd2807SJeff Garzik 	}
3048c6fd2807SJeff Garzik 
3049c6fd2807SJeff Garzik 	/*
3050c6fd2807SJeff Garzik 	 * Convert the timing to bus clock counts.
3051c6fd2807SJeff Garzik 	 */
3052c6fd2807SJeff Garzik 
3053c6fd2807SJeff Garzik 	ata_timing_quantize(t, t, T, UT);
3054c6fd2807SJeff Garzik 
3055c6fd2807SJeff Garzik 	/*
3056c6fd2807SJeff Garzik 	 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
3057c6fd2807SJeff Garzik 	 * S.M.A.R.T * and some other commands. We have to ensure that the
3058c6fd2807SJeff Garzik 	 * DMA cycle timing is slower/equal than the fastest PIO timing.
3059c6fd2807SJeff Garzik 	 */
3060c6fd2807SJeff Garzik 
3061fd3367afSAlan 	if (speed > XFER_PIO_6) {
3062c6fd2807SJeff Garzik 		ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
3063c6fd2807SJeff Garzik 		ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
3064c6fd2807SJeff Garzik 	}
3065c6fd2807SJeff Garzik 
3066c6fd2807SJeff Garzik 	/*
3067c6fd2807SJeff Garzik 	 * Lengthen active & recovery time so that cycle time is correct.
3068c6fd2807SJeff Garzik 	 */
3069c6fd2807SJeff Garzik 
3070c6fd2807SJeff Garzik 	if (t->act8b + t->rec8b < t->cyc8b) {
3071c6fd2807SJeff Garzik 		t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
3072c6fd2807SJeff Garzik 		t->rec8b = t->cyc8b - t->act8b;
3073c6fd2807SJeff Garzik 	}
3074c6fd2807SJeff Garzik 
3075c6fd2807SJeff Garzik 	if (t->active + t->recover < t->cycle) {
3076c6fd2807SJeff Garzik 		t->active += (t->cycle - (t->active + t->recover)) / 2;
3077c6fd2807SJeff Garzik 		t->recover = t->cycle - t->active;
3078c6fd2807SJeff Garzik 	}
30794f701d1eSAlan Cox 
30804f701d1eSAlan Cox 	/* In a few cases quantisation may produce enough errors to
30814f701d1eSAlan Cox 	   leave t->cycle too low for the sum of active and recovery
30824f701d1eSAlan Cox 	   if so we must correct this */
30834f701d1eSAlan Cox 	if (t->active + t->recover > t->cycle)
30844f701d1eSAlan Cox 		t->cycle = t->active + t->recover;
3085c6fd2807SJeff Garzik 
3086c6fd2807SJeff Garzik 	return 0;
3087c6fd2807SJeff Garzik }
3088c6fd2807SJeff Garzik 
3089c6fd2807SJeff Garzik /**
3090a0f79b92STejun Heo  *	ata_timing_cycle2mode - find xfer mode for the specified cycle duration
3091a0f79b92STejun Heo  *	@xfer_shift: ATA_SHIFT_* value for transfer type to examine.
3092a0f79b92STejun Heo  *	@cycle: cycle duration in ns
3093a0f79b92STejun Heo  *
3094a0f79b92STejun Heo  *	Return matching xfer mode for @cycle.  The returned mode is of
3095a0f79b92STejun Heo  *	the transfer type specified by @xfer_shift.  If @cycle is too
3096a0f79b92STejun Heo  *	slow for @xfer_shift, 0xff is returned.  If @cycle is faster
3097a0f79b92STejun Heo  *	than the fastest known mode, the fasted mode is returned.
3098a0f79b92STejun Heo  *
3099a0f79b92STejun Heo  *	LOCKING:
3100a0f79b92STejun Heo  *	None.
3101a0f79b92STejun Heo  *
3102a0f79b92STejun Heo  *	RETURNS:
3103a0f79b92STejun Heo  *	Matching xfer_mode, 0xff if no match found.
3104a0f79b92STejun Heo  */
3105a0f79b92STejun Heo u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
3106a0f79b92STejun Heo {
3107a0f79b92STejun Heo 	u8 base_mode = 0xff, last_mode = 0xff;
3108a0f79b92STejun Heo 	const struct ata_xfer_ent *ent;
3109a0f79b92STejun Heo 	const struct ata_timing *t;
3110a0f79b92STejun Heo 
3111a0f79b92STejun Heo 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
3112a0f79b92STejun Heo 		if (ent->shift == xfer_shift)
3113a0f79b92STejun Heo 			base_mode = ent->base;
3114a0f79b92STejun Heo 
3115a0f79b92STejun Heo 	for (t = ata_timing_find_mode(base_mode);
3116a0f79b92STejun Heo 	     t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
3117a0f79b92STejun Heo 		unsigned short this_cycle;
3118a0f79b92STejun Heo 
3119a0f79b92STejun Heo 		switch (xfer_shift) {
3120a0f79b92STejun Heo 		case ATA_SHIFT_PIO:
3121a0f79b92STejun Heo 		case ATA_SHIFT_MWDMA:
3122a0f79b92STejun Heo 			this_cycle = t->cycle;
3123a0f79b92STejun Heo 			break;
3124a0f79b92STejun Heo 		case ATA_SHIFT_UDMA:
3125a0f79b92STejun Heo 			this_cycle = t->udma;
3126a0f79b92STejun Heo 			break;
3127a0f79b92STejun Heo 		default:
3128a0f79b92STejun Heo 			return 0xff;
3129a0f79b92STejun Heo 		}
3130a0f79b92STejun Heo 
3131a0f79b92STejun Heo 		if (cycle > this_cycle)
3132a0f79b92STejun Heo 			break;
3133a0f79b92STejun Heo 
3134a0f79b92STejun Heo 		last_mode = t->mode;
3135a0f79b92STejun Heo 	}
3136a0f79b92STejun Heo 
3137a0f79b92STejun Heo 	return last_mode;
3138a0f79b92STejun Heo }
3139a0f79b92STejun Heo 
3140a0f79b92STejun Heo /**
3141c6fd2807SJeff Garzik  *	ata_down_xfermask_limit - adjust dev xfer masks downward
3142c6fd2807SJeff Garzik  *	@dev: Device to adjust xfer masks
3143458337dbSTejun Heo  *	@sel: ATA_DNXFER_* selector
3144c6fd2807SJeff Garzik  *
3145c6fd2807SJeff Garzik  *	Adjust xfer masks of @dev downward.  Note that this function
3146c6fd2807SJeff Garzik  *	does not apply the change.  Invoking ata_set_mode() afterwards
3147c6fd2807SJeff Garzik  *	will apply the limit.
3148c6fd2807SJeff Garzik  *
3149c6fd2807SJeff Garzik  *	LOCKING:
3150c6fd2807SJeff Garzik  *	Inherited from caller.
3151c6fd2807SJeff Garzik  *
3152c6fd2807SJeff Garzik  *	RETURNS:
3153c6fd2807SJeff Garzik  *	0 on success, negative errno on failure
3154c6fd2807SJeff Garzik  */
3155458337dbSTejun Heo int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
3156c6fd2807SJeff Garzik {
3157458337dbSTejun Heo 	char buf[32];
31587dc951aeSTejun Heo 	unsigned long orig_mask, xfer_mask;
31597dc951aeSTejun Heo 	unsigned long pio_mask, mwdma_mask, udma_mask;
3160458337dbSTejun Heo 	int quiet, highbit;
3161c6fd2807SJeff Garzik 
3162458337dbSTejun Heo 	quiet = !!(sel & ATA_DNXFER_QUIET);
3163458337dbSTejun Heo 	sel &= ~ATA_DNXFER_QUIET;
3164458337dbSTejun Heo 
3165458337dbSTejun Heo 	xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
3166458337dbSTejun Heo 						  dev->mwdma_mask,
3167c6fd2807SJeff Garzik 						  dev->udma_mask);
3168458337dbSTejun Heo 	ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
3169c6fd2807SJeff Garzik 
3170458337dbSTejun Heo 	switch (sel) {
3171458337dbSTejun Heo 	case ATA_DNXFER_PIO:
3172458337dbSTejun Heo 		highbit = fls(pio_mask) - 1;
3173458337dbSTejun Heo 		pio_mask &= ~(1 << highbit);
3174458337dbSTejun Heo 		break;
3175458337dbSTejun Heo 
3176458337dbSTejun Heo 	case ATA_DNXFER_DMA:
3177458337dbSTejun Heo 		if (udma_mask) {
3178458337dbSTejun Heo 			highbit = fls(udma_mask) - 1;
3179458337dbSTejun Heo 			udma_mask &= ~(1 << highbit);
3180458337dbSTejun Heo 			if (!udma_mask)
3181458337dbSTejun Heo 				return -ENOENT;
3182458337dbSTejun Heo 		} else if (mwdma_mask) {
3183458337dbSTejun Heo 			highbit = fls(mwdma_mask) - 1;
3184458337dbSTejun Heo 			mwdma_mask &= ~(1 << highbit);
3185458337dbSTejun Heo 			if (!mwdma_mask)
3186458337dbSTejun Heo 				return -ENOENT;
3187458337dbSTejun Heo 		}
3188458337dbSTejun Heo 		break;
3189458337dbSTejun Heo 
3190458337dbSTejun Heo 	case ATA_DNXFER_40C:
3191458337dbSTejun Heo 		udma_mask &= ATA_UDMA_MASK_40C;
3192458337dbSTejun Heo 		break;
3193458337dbSTejun Heo 
3194458337dbSTejun Heo 	case ATA_DNXFER_FORCE_PIO0:
3195458337dbSTejun Heo 		pio_mask &= 1;
3196458337dbSTejun Heo 	case ATA_DNXFER_FORCE_PIO:
3197458337dbSTejun Heo 		mwdma_mask = 0;
3198458337dbSTejun Heo 		udma_mask = 0;
3199458337dbSTejun Heo 		break;
3200458337dbSTejun Heo 
3201458337dbSTejun Heo 	default:
3202458337dbSTejun Heo 		BUG();
3203458337dbSTejun Heo 	}
3204458337dbSTejun Heo 
3205458337dbSTejun Heo 	xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
3206458337dbSTejun Heo 
3207458337dbSTejun Heo 	if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
3208458337dbSTejun Heo 		return -ENOENT;
3209458337dbSTejun Heo 
3210458337dbSTejun Heo 	if (!quiet) {
3211458337dbSTejun Heo 		if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
3212458337dbSTejun Heo 			snprintf(buf, sizeof(buf), "%s:%s",
3213458337dbSTejun Heo 				 ata_mode_string(xfer_mask),
3214458337dbSTejun Heo 				 ata_mode_string(xfer_mask & ATA_MASK_PIO));
3215458337dbSTejun Heo 		else
3216458337dbSTejun Heo 			snprintf(buf, sizeof(buf), "%s",
3217458337dbSTejun Heo 				 ata_mode_string(xfer_mask));
3218458337dbSTejun Heo 
3219458337dbSTejun Heo 		ata_dev_printk(dev, KERN_WARNING,
3220458337dbSTejun Heo 			       "limiting speed to %s\n", buf);
3221458337dbSTejun Heo 	}
3222c6fd2807SJeff Garzik 
3223c6fd2807SJeff Garzik 	ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
3224c6fd2807SJeff Garzik 			    &dev->udma_mask);
3225c6fd2807SJeff Garzik 
3226c6fd2807SJeff Garzik 	return 0;
3227c6fd2807SJeff Garzik }
3228c6fd2807SJeff Garzik 
3229c6fd2807SJeff Garzik static int ata_dev_set_mode(struct ata_device *dev)
3230c6fd2807SJeff Garzik {
32319af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
32324055dee7STejun Heo 	const char *dev_err_whine = "";
32334055dee7STejun Heo 	int ign_dev_err = 0;
3234c6fd2807SJeff Garzik 	unsigned int err_mask;
3235c6fd2807SJeff Garzik 	int rc;
3236c6fd2807SJeff Garzik 
3237c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_PIO;
3238c6fd2807SJeff Garzik 	if (dev->xfer_shift == ATA_SHIFT_PIO)
3239c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_PIO;
3240c6fd2807SJeff Garzik 
3241c6fd2807SJeff Garzik 	err_mask = ata_dev_set_xfermode(dev);
32422dcb407eSJeff Garzik 
32434055dee7STejun Heo 	if (err_mask & ~AC_ERR_DEV)
32444055dee7STejun Heo 		goto fail;
32452dcb407eSJeff Garzik 
32464055dee7STejun Heo 	/* revalidate */
3247baa1e78aSTejun Heo 	ehc->i.flags |= ATA_EHI_POST_SETMODE;
3248422c9daaSTejun Heo 	rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
3249baa1e78aSTejun Heo 	ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
3250c6fd2807SJeff Garzik 	if (rc)
3251c6fd2807SJeff Garzik 		return rc;
3252c6fd2807SJeff Garzik 
3253b93fda12SAlan Cox 	if (dev->xfer_shift == ATA_SHIFT_PIO) {
32544055dee7STejun Heo 		/* Old CFA may refuse this command, which is just fine */
3255b93fda12SAlan Cox 		if (ata_id_is_cfa(dev->id))
32564055dee7STejun Heo 			ign_dev_err = 1;
3257b93fda12SAlan Cox 		/* Catch several broken garbage emulations plus some pre
3258b93fda12SAlan Cox 		   ATA devices */
3259b93fda12SAlan Cox 		if (ata_id_major_version(dev->id) == 0 &&
32604055dee7STejun Heo 					dev->pio_mode <= XFER_PIO_2)
32614055dee7STejun Heo 			ign_dev_err = 1;
3262b93fda12SAlan Cox 		/* Some very old devices and some bad newer ones fail
3263b93fda12SAlan Cox 		   any kind of SET_XFERMODE request but support PIO0-2
3264b93fda12SAlan Cox 		   timings and no IORDY */
3265b93fda12SAlan Cox 		if (!ata_id_has_iordy(dev->id) && dev->pio_mode <= XFER_PIO_2)
3266b93fda12SAlan Cox 			ign_dev_err = 1;
3267b93fda12SAlan Cox 	}
32684055dee7STejun Heo 	/* Early MWDMA devices do DMA but don't allow DMA mode setting.
32694055dee7STejun Heo 	   Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
32704055dee7STejun Heo 	if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
32714055dee7STejun Heo 	    dev->dma_mode == XFER_MW_DMA_0 &&
32724055dee7STejun Heo 	    (dev->id[63] >> 8) & 1)
32734055dee7STejun Heo 		ign_dev_err = 1;
32744055dee7STejun Heo 
32754055dee7STejun Heo 	/* if the device is actually configured correctly, ignore dev err */
32764055dee7STejun Heo 	if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id)))
32774055dee7STejun Heo 		ign_dev_err = 1;
32784055dee7STejun Heo 
32794055dee7STejun Heo 	if (err_mask & AC_ERR_DEV) {
32804055dee7STejun Heo 		if (!ign_dev_err)
32814055dee7STejun Heo 			goto fail;
32824055dee7STejun Heo 		else
32834055dee7STejun Heo 			dev_err_whine = " (device error ignored)";
32844055dee7STejun Heo 	}
32854055dee7STejun Heo 
3286c6fd2807SJeff Garzik 	DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
3287c6fd2807SJeff Garzik 		dev->xfer_shift, (int)dev->xfer_mode);
3288c6fd2807SJeff Garzik 
32894055dee7STejun Heo 	ata_dev_printk(dev, KERN_INFO, "configured for %s%s\n",
32904055dee7STejun Heo 		       ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)),
32914055dee7STejun Heo 		       dev_err_whine);
32924055dee7STejun Heo 
3293c6fd2807SJeff Garzik 	return 0;
32944055dee7STejun Heo 
32954055dee7STejun Heo  fail:
32964055dee7STejun Heo 	ata_dev_printk(dev, KERN_ERR, "failed to set xfermode "
32974055dee7STejun Heo 		       "(err_mask=0x%x)\n", err_mask);
32984055dee7STejun Heo 	return -EIO;
3299c6fd2807SJeff Garzik }
3300c6fd2807SJeff Garzik 
3301c6fd2807SJeff Garzik /**
330204351821SAlan  *	ata_do_set_mode - Program timings and issue SET FEATURES - XFER
33030260731fSTejun Heo  *	@link: link on which timings will be programmed
33041967b7ffSJoe Perches  *	@r_failed_dev: out parameter for failed device
3305c6fd2807SJeff Garzik  *
330604351821SAlan  *	Standard implementation of the function used to tune and set
330704351821SAlan  *	ATA device disk transfer mode (PIO3, UDMA6, etc.).  If
330804351821SAlan  *	ata_dev_set_mode() fails, pointer to the failing device is
3309c6fd2807SJeff Garzik  *	returned in @r_failed_dev.
3310c6fd2807SJeff Garzik  *
3311c6fd2807SJeff Garzik  *	LOCKING:
3312c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
3313c6fd2807SJeff Garzik  *
3314c6fd2807SJeff Garzik  *	RETURNS:
3315c6fd2807SJeff Garzik  *	0 on success, negative errno otherwise
3316c6fd2807SJeff Garzik  */
331704351821SAlan 
33180260731fSTejun Heo int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
3319c6fd2807SJeff Garzik {
33200260731fSTejun Heo 	struct ata_port *ap = link->ap;
3321c6fd2807SJeff Garzik 	struct ata_device *dev;
3322f58229f8STejun Heo 	int rc = 0, used_dma = 0, found = 0;
3323c6fd2807SJeff Garzik 
3324c6fd2807SJeff Garzik 	/* step 1: calculate xfer_mask */
3325f58229f8STejun Heo 	ata_link_for_each_dev(dev, link) {
33267dc951aeSTejun Heo 		unsigned long pio_mask, dma_mask;
3327b3a70601SAlan Cox 		unsigned int mode_mask;
3328c6fd2807SJeff Garzik 
3329c6fd2807SJeff Garzik 		if (!ata_dev_enabled(dev))
3330c6fd2807SJeff Garzik 			continue;
3331c6fd2807SJeff Garzik 
3332b3a70601SAlan Cox 		mode_mask = ATA_DMA_MASK_ATA;
3333b3a70601SAlan Cox 		if (dev->class == ATA_DEV_ATAPI)
3334b3a70601SAlan Cox 			mode_mask = ATA_DMA_MASK_ATAPI;
3335b3a70601SAlan Cox 		else if (ata_id_is_cfa(dev->id))
3336b3a70601SAlan Cox 			mode_mask = ATA_DMA_MASK_CFA;
3337b3a70601SAlan Cox 
3338c6fd2807SJeff Garzik 		ata_dev_xfermask(dev);
333933267325STejun Heo 		ata_force_xfermask(dev);
3340c6fd2807SJeff Garzik 
3341c6fd2807SJeff Garzik 		pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
3342c6fd2807SJeff Garzik 		dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
3343b3a70601SAlan Cox 
3344b3a70601SAlan Cox 		if (libata_dma_mask & mode_mask)
3345b3a70601SAlan Cox 			dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
3346b3a70601SAlan Cox 		else
3347b3a70601SAlan Cox 			dma_mask = 0;
3348b3a70601SAlan Cox 
3349c6fd2807SJeff Garzik 		dev->pio_mode = ata_xfer_mask2mode(pio_mask);
3350c6fd2807SJeff Garzik 		dev->dma_mode = ata_xfer_mask2mode(dma_mask);
3351c6fd2807SJeff Garzik 
3352c6fd2807SJeff Garzik 		found = 1;
3353b15b3ebaSAlan Cox 		if (ata_dma_enabled(dev))
3354c6fd2807SJeff Garzik 			used_dma = 1;
3355c6fd2807SJeff Garzik 	}
3356c6fd2807SJeff Garzik 	if (!found)
3357c6fd2807SJeff Garzik 		goto out;
3358c6fd2807SJeff Garzik 
3359c6fd2807SJeff Garzik 	/* step 2: always set host PIO timings */
3360f58229f8STejun Heo 	ata_link_for_each_dev(dev, link) {
3361c6fd2807SJeff Garzik 		if (!ata_dev_enabled(dev))
3362c6fd2807SJeff Garzik 			continue;
3363c6fd2807SJeff Garzik 
336470cd071eSTejun Heo 		if (dev->pio_mode == 0xff) {
3365c6fd2807SJeff Garzik 			ata_dev_printk(dev, KERN_WARNING, "no PIO support\n");
3366c6fd2807SJeff Garzik 			rc = -EINVAL;
3367c6fd2807SJeff Garzik 			goto out;
3368c6fd2807SJeff Garzik 		}
3369c6fd2807SJeff Garzik 
3370c6fd2807SJeff Garzik 		dev->xfer_mode = dev->pio_mode;
3371c6fd2807SJeff Garzik 		dev->xfer_shift = ATA_SHIFT_PIO;
3372c6fd2807SJeff Garzik 		if (ap->ops->set_piomode)
3373c6fd2807SJeff Garzik 			ap->ops->set_piomode(ap, dev);
3374c6fd2807SJeff Garzik 	}
3375c6fd2807SJeff Garzik 
3376c6fd2807SJeff Garzik 	/* step 3: set host DMA timings */
3377f58229f8STejun Heo 	ata_link_for_each_dev(dev, link) {
3378b15b3ebaSAlan Cox 		if (!ata_dev_enabled(dev) || !ata_dma_enabled(dev))
3379c6fd2807SJeff Garzik 			continue;
3380c6fd2807SJeff Garzik 
3381c6fd2807SJeff Garzik 		dev->xfer_mode = dev->dma_mode;
3382c6fd2807SJeff Garzik 		dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
3383c6fd2807SJeff Garzik 		if (ap->ops->set_dmamode)
3384c6fd2807SJeff Garzik 			ap->ops->set_dmamode(ap, dev);
3385c6fd2807SJeff Garzik 	}
3386c6fd2807SJeff Garzik 
3387c6fd2807SJeff Garzik 	/* step 4: update devices' xfer mode */
3388f58229f8STejun Heo 	ata_link_for_each_dev(dev, link) {
338918d90debSAlan 		/* don't update suspended devices' xfer mode */
33909666f400STejun Heo 		if (!ata_dev_enabled(dev))
3391c6fd2807SJeff Garzik 			continue;
3392c6fd2807SJeff Garzik 
3393c6fd2807SJeff Garzik 		rc = ata_dev_set_mode(dev);
3394c6fd2807SJeff Garzik 		if (rc)
3395c6fd2807SJeff Garzik 			goto out;
3396c6fd2807SJeff Garzik 	}
3397c6fd2807SJeff Garzik 
3398c6fd2807SJeff Garzik 	/* Record simplex status. If we selected DMA then the other
3399c6fd2807SJeff Garzik 	 * host channels are not permitted to do so.
3400c6fd2807SJeff Garzik 	 */
3401cca3974eSJeff Garzik 	if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
3402032af1ceSAlan 		ap->host->simplex_claimed = ap;
3403c6fd2807SJeff Garzik 
3404c6fd2807SJeff Garzik  out:
3405c6fd2807SJeff Garzik 	if (rc)
3406c6fd2807SJeff Garzik 		*r_failed_dev = dev;
3407c6fd2807SJeff Garzik 	return rc;
3408c6fd2807SJeff Garzik }
3409c6fd2807SJeff Garzik 
3410c6fd2807SJeff Garzik /**
3411aa2731adSTejun Heo  *	ata_wait_ready - wait for link to become ready
3412aa2731adSTejun Heo  *	@link: link to be waited on
3413aa2731adSTejun Heo  *	@deadline: deadline jiffies for the operation
3414aa2731adSTejun Heo  *	@check_ready: callback to check link readiness
3415aa2731adSTejun Heo  *
3416aa2731adSTejun Heo  *	Wait for @link to become ready.  @check_ready should return
3417aa2731adSTejun Heo  *	positive number if @link is ready, 0 if it isn't, -ENODEV if
3418aa2731adSTejun Heo  *	link doesn't seem to be occupied, other errno for other error
3419aa2731adSTejun Heo  *	conditions.
3420aa2731adSTejun Heo  *
3421aa2731adSTejun Heo  *	Transient -ENODEV conditions are allowed for
3422aa2731adSTejun Heo  *	ATA_TMOUT_FF_WAIT.
3423aa2731adSTejun Heo  *
3424aa2731adSTejun Heo  *	LOCKING:
3425aa2731adSTejun Heo  *	EH context.
3426aa2731adSTejun Heo  *
3427aa2731adSTejun Heo  *	RETURNS:
3428aa2731adSTejun Heo  *	0 if @linke is ready before @deadline; otherwise, -errno.
3429aa2731adSTejun Heo  */
3430aa2731adSTejun Heo int ata_wait_ready(struct ata_link *link, unsigned long deadline,
3431aa2731adSTejun Heo 		   int (*check_ready)(struct ata_link *link))
3432aa2731adSTejun Heo {
3433aa2731adSTejun Heo 	unsigned long start = jiffies;
3434341c2c95STejun Heo 	unsigned long nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT);
3435aa2731adSTejun Heo 	int warned = 0;
3436aa2731adSTejun Heo 
3437b1c72916STejun Heo 	/* Slave readiness can't be tested separately from master.  On
3438b1c72916STejun Heo 	 * M/S emulation configuration, this function should be called
3439b1c72916STejun Heo 	 * only on the master and it will handle both master and slave.
3440b1c72916STejun Heo 	 */
3441b1c72916STejun Heo 	WARN_ON(link == link->ap->slave_link);
3442b1c72916STejun Heo 
3443aa2731adSTejun Heo 	if (time_after(nodev_deadline, deadline))
3444aa2731adSTejun Heo 		nodev_deadline = deadline;
3445aa2731adSTejun Heo 
3446aa2731adSTejun Heo 	while (1) {
3447aa2731adSTejun Heo 		unsigned long now = jiffies;
3448aa2731adSTejun Heo 		int ready, tmp;
3449aa2731adSTejun Heo 
3450aa2731adSTejun Heo 		ready = tmp = check_ready(link);
3451aa2731adSTejun Heo 		if (ready > 0)
3452aa2731adSTejun Heo 			return 0;
3453aa2731adSTejun Heo 
3454aa2731adSTejun Heo 		/* -ENODEV could be transient.  Ignore -ENODEV if link
3455aa2731adSTejun Heo 		 * is online.  Also, some SATA devices take a long
3456aa2731adSTejun Heo 		 * time to clear 0xff after reset.  For example,
3457aa2731adSTejun Heo 		 * HHD424020F7SV00 iVDR needs >= 800ms while Quantum
3458aa2731adSTejun Heo 		 * GoVault needs even more than that.  Wait for
3459aa2731adSTejun Heo 		 * ATA_TMOUT_FF_WAIT on -ENODEV if link isn't offline.
3460aa2731adSTejun Heo 		 *
3461aa2731adSTejun Heo 		 * Note that some PATA controllers (pata_ali) explode
3462aa2731adSTejun Heo 		 * if status register is read more than once when
3463aa2731adSTejun Heo 		 * there's no device attached.
3464aa2731adSTejun Heo 		 */
3465aa2731adSTejun Heo 		if (ready == -ENODEV) {
3466aa2731adSTejun Heo 			if (ata_link_online(link))
3467aa2731adSTejun Heo 				ready = 0;
3468aa2731adSTejun Heo 			else if ((link->ap->flags & ATA_FLAG_SATA) &&
3469aa2731adSTejun Heo 				 !ata_link_offline(link) &&
3470aa2731adSTejun Heo 				 time_before(now, nodev_deadline))
3471aa2731adSTejun Heo 				ready = 0;
3472aa2731adSTejun Heo 		}
3473aa2731adSTejun Heo 
3474aa2731adSTejun Heo 		if (ready)
3475aa2731adSTejun Heo 			return ready;
3476aa2731adSTejun Heo 		if (time_after(now, deadline))
3477aa2731adSTejun Heo 			return -EBUSY;
3478aa2731adSTejun Heo 
3479aa2731adSTejun Heo 		if (!warned && time_after(now, start + 5 * HZ) &&
3480aa2731adSTejun Heo 		    (deadline - now > 3 * HZ)) {
3481aa2731adSTejun Heo 			ata_link_printk(link, KERN_WARNING,
3482aa2731adSTejun Heo 				"link is slow to respond, please be patient "
3483aa2731adSTejun Heo 				"(ready=%d)\n", tmp);
3484aa2731adSTejun Heo 			warned = 1;
3485aa2731adSTejun Heo 		}
3486aa2731adSTejun Heo 
3487aa2731adSTejun Heo 		msleep(50);
3488aa2731adSTejun Heo 	}
3489aa2731adSTejun Heo }
3490aa2731adSTejun Heo 
3491aa2731adSTejun Heo /**
3492aa2731adSTejun Heo  *	ata_wait_after_reset - wait for link to become ready after reset
3493aa2731adSTejun Heo  *	@link: link to be waited on
3494aa2731adSTejun Heo  *	@deadline: deadline jiffies for the operation
3495aa2731adSTejun Heo  *	@check_ready: callback to check link readiness
3496aa2731adSTejun Heo  *
3497aa2731adSTejun Heo  *	Wait for @link to become ready after reset.
3498aa2731adSTejun Heo  *
3499aa2731adSTejun Heo  *	LOCKING:
3500aa2731adSTejun Heo  *	EH context.
3501aa2731adSTejun Heo  *
3502aa2731adSTejun Heo  *	RETURNS:
3503aa2731adSTejun Heo  *	0 if @linke is ready before @deadline; otherwise, -errno.
3504aa2731adSTejun Heo  */
35052b4221bbSHarvey Harrison int ata_wait_after_reset(struct ata_link *link, unsigned long deadline,
3506aa2731adSTejun Heo 				int (*check_ready)(struct ata_link *link))
3507aa2731adSTejun Heo {
3508341c2c95STejun Heo 	msleep(ATA_WAIT_AFTER_RESET);
3509aa2731adSTejun Heo 
3510aa2731adSTejun Heo 	return ata_wait_ready(link, deadline, check_ready);
3511aa2731adSTejun Heo }
3512aa2731adSTejun Heo 
3513aa2731adSTejun Heo /**
3514936fd732STejun Heo  *	sata_link_debounce - debounce SATA phy status
3515936fd732STejun Heo  *	@link: ATA link to debounce SATA phy status for
3516c6fd2807SJeff Garzik  *	@params: timing parameters { interval, duratinon, timeout } in msec
3517d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
3518c6fd2807SJeff Garzik  *
3519936fd732STejun Heo *	Make sure SStatus of @link reaches stable state, determined by
3520c6fd2807SJeff Garzik  *	holding the same value where DET is not 1 for @duration polled
3521c6fd2807SJeff Garzik  *	every @interval, before @timeout.  Timeout constraints the
3522d4b2bab4STejun Heo  *	beginning of the stable state.  Because DET gets stuck at 1 on
3523d4b2bab4STejun Heo  *	some controllers after hot unplugging, this functions waits
3524c6fd2807SJeff Garzik  *	until timeout then returns 0 if DET is stable at 1.
3525c6fd2807SJeff Garzik  *
3526d4b2bab4STejun Heo  *	@timeout is further limited by @deadline.  The sooner of the
3527d4b2bab4STejun Heo  *	two is used.
3528d4b2bab4STejun Heo  *
3529c6fd2807SJeff Garzik  *	LOCKING:
3530c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3531c6fd2807SJeff Garzik  *
3532c6fd2807SJeff Garzik  *	RETURNS:
3533c6fd2807SJeff Garzik  *	0 on success, -errno on failure.
3534c6fd2807SJeff Garzik  */
3535936fd732STejun Heo int sata_link_debounce(struct ata_link *link, const unsigned long *params,
3536d4b2bab4STejun Heo 		       unsigned long deadline)
3537c6fd2807SJeff Garzik {
3538341c2c95STejun Heo 	unsigned long interval = params[0];
3539341c2c95STejun Heo 	unsigned long duration = params[1];
3540d4b2bab4STejun Heo 	unsigned long last_jiffies, t;
3541c6fd2807SJeff Garzik 	u32 last, cur;
3542c6fd2807SJeff Garzik 	int rc;
3543c6fd2807SJeff Garzik 
3544341c2c95STejun Heo 	t = ata_deadline(jiffies, params[2]);
3545d4b2bab4STejun Heo 	if (time_before(t, deadline))
3546d4b2bab4STejun Heo 		deadline = t;
3547d4b2bab4STejun Heo 
3548936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3549c6fd2807SJeff Garzik 		return rc;
3550c6fd2807SJeff Garzik 	cur &= 0xf;
3551c6fd2807SJeff Garzik 
3552c6fd2807SJeff Garzik 	last = cur;
3553c6fd2807SJeff Garzik 	last_jiffies = jiffies;
3554c6fd2807SJeff Garzik 
3555c6fd2807SJeff Garzik 	while (1) {
3556341c2c95STejun Heo 		msleep(interval);
3557936fd732STejun Heo 		if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3558c6fd2807SJeff Garzik 			return rc;
3559c6fd2807SJeff Garzik 		cur &= 0xf;
3560c6fd2807SJeff Garzik 
3561c6fd2807SJeff Garzik 		/* DET stable? */
3562c6fd2807SJeff Garzik 		if (cur == last) {
3563d4b2bab4STejun Heo 			if (cur == 1 && time_before(jiffies, deadline))
3564c6fd2807SJeff Garzik 				continue;
3565341c2c95STejun Heo 			if (time_after(jiffies,
3566341c2c95STejun Heo 				       ata_deadline(last_jiffies, duration)))
3567c6fd2807SJeff Garzik 				return 0;
3568c6fd2807SJeff Garzik 			continue;
3569c6fd2807SJeff Garzik 		}
3570c6fd2807SJeff Garzik 
3571c6fd2807SJeff Garzik 		/* unstable, start over */
3572c6fd2807SJeff Garzik 		last = cur;
3573c6fd2807SJeff Garzik 		last_jiffies = jiffies;
3574c6fd2807SJeff Garzik 
3575f1545154STejun Heo 		/* Check deadline.  If debouncing failed, return
3576f1545154STejun Heo 		 * -EPIPE to tell upper layer to lower link speed.
3577f1545154STejun Heo 		 */
3578d4b2bab4STejun Heo 		if (time_after(jiffies, deadline))
3579f1545154STejun Heo 			return -EPIPE;
3580c6fd2807SJeff Garzik 	}
3581c6fd2807SJeff Garzik }
3582c6fd2807SJeff Garzik 
3583c6fd2807SJeff Garzik /**
3584936fd732STejun Heo  *	sata_link_resume - resume SATA link
3585936fd732STejun Heo  *	@link: ATA link to resume SATA
3586c6fd2807SJeff Garzik  *	@params: timing parameters { interval, duratinon, timeout } in msec
3587d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
3588c6fd2807SJeff Garzik  *
3589936fd732STejun Heo  *	Resume SATA phy @link and debounce it.
3590c6fd2807SJeff Garzik  *
3591c6fd2807SJeff Garzik  *	LOCKING:
3592c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3593c6fd2807SJeff Garzik  *
3594c6fd2807SJeff Garzik  *	RETURNS:
3595c6fd2807SJeff Garzik  *	0 on success, -errno on failure.
3596c6fd2807SJeff Garzik  */
3597936fd732STejun Heo int sata_link_resume(struct ata_link *link, const unsigned long *params,
3598d4b2bab4STejun Heo 		     unsigned long deadline)
3599c6fd2807SJeff Garzik {
3600ac371987STejun Heo 	u32 scontrol, serror;
3601c6fd2807SJeff Garzik 	int rc;
3602c6fd2807SJeff Garzik 
3603936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3604c6fd2807SJeff Garzik 		return rc;
3605c6fd2807SJeff Garzik 
3606c6fd2807SJeff Garzik 	scontrol = (scontrol & 0x0f0) | 0x300;
3607c6fd2807SJeff Garzik 
3608936fd732STejun Heo 	if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3609c6fd2807SJeff Garzik 		return rc;
3610c6fd2807SJeff Garzik 
3611c6fd2807SJeff Garzik 	/* Some PHYs react badly if SStatus is pounded immediately
3612c6fd2807SJeff Garzik 	 * after resuming.  Delay 200ms before debouncing.
3613c6fd2807SJeff Garzik 	 */
3614c6fd2807SJeff Garzik 	msleep(200);
3615c6fd2807SJeff Garzik 
3616ac371987STejun Heo 	if ((rc = sata_link_debounce(link, params, deadline)))
3617ac371987STejun Heo 		return rc;
3618ac371987STejun Heo 
3619f046519fSTejun Heo 	/* clear SError, some PHYs require this even for SRST to work */
3620ac371987STejun Heo 	if (!(rc = sata_scr_read(link, SCR_ERROR, &serror)))
3621ac371987STejun Heo 		rc = sata_scr_write(link, SCR_ERROR, serror);
3622ac371987STejun Heo 
3623f046519fSTejun Heo 	return rc != -EINVAL ? rc : 0;
3624c6fd2807SJeff Garzik }
3625c6fd2807SJeff Garzik 
3626c6fd2807SJeff Garzik /**
36270aa1113dSTejun Heo  *	ata_std_prereset - prepare for reset
3628cc0680a5STejun Heo  *	@link: ATA link to be reset
3629d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
3630c6fd2807SJeff Garzik  *
3631cc0680a5STejun Heo  *	@link is about to be reset.  Initialize it.  Failure from
3632b8cffc6aSTejun Heo  *	prereset makes libata abort whole reset sequence and give up
3633b8cffc6aSTejun Heo  *	that port, so prereset should be best-effort.  It does its
3634b8cffc6aSTejun Heo  *	best to prepare for reset sequence but if things go wrong, it
3635b8cffc6aSTejun Heo  *	should just whine, not fail.
3636c6fd2807SJeff Garzik  *
3637c6fd2807SJeff Garzik  *	LOCKING:
3638c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3639c6fd2807SJeff Garzik  *
3640c6fd2807SJeff Garzik  *	RETURNS:
3641c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
3642c6fd2807SJeff Garzik  */
36430aa1113dSTejun Heo int ata_std_prereset(struct ata_link *link, unsigned long deadline)
3644c6fd2807SJeff Garzik {
3645cc0680a5STejun Heo 	struct ata_port *ap = link->ap;
3646936fd732STejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
3647c6fd2807SJeff Garzik 	const unsigned long *timing = sata_ehc_deb_timing(ehc);
3648c6fd2807SJeff Garzik 	int rc;
3649c6fd2807SJeff Garzik 
3650c6fd2807SJeff Garzik 	/* if we're about to do hardreset, nothing more to do */
3651c6fd2807SJeff Garzik 	if (ehc->i.action & ATA_EH_HARDRESET)
3652c6fd2807SJeff Garzik 		return 0;
3653c6fd2807SJeff Garzik 
3654936fd732STejun Heo 	/* if SATA, resume link */
3655a16abc0bSTejun Heo 	if (ap->flags & ATA_FLAG_SATA) {
3656936fd732STejun Heo 		rc = sata_link_resume(link, timing, deadline);
3657b8cffc6aSTejun Heo 		/* whine about phy resume failure but proceed */
3658b8cffc6aSTejun Heo 		if (rc && rc != -EOPNOTSUPP)
3659cc0680a5STejun Heo 			ata_link_printk(link, KERN_WARNING, "failed to resume "
3660c6fd2807SJeff Garzik 					"link for reset (errno=%d)\n", rc);
3661c6fd2807SJeff Garzik 	}
3662c6fd2807SJeff Garzik 
366345db2f6cSTejun Heo 	/* no point in trying softreset on offline link */
3664b1c72916STejun Heo 	if (ata_phys_link_offline(link))
366545db2f6cSTejun Heo 		ehc->i.action &= ~ATA_EH_SOFTRESET;
366645db2f6cSTejun Heo 
3667c6fd2807SJeff Garzik 	return 0;
3668c6fd2807SJeff Garzik }
3669c6fd2807SJeff Garzik 
3670c6fd2807SJeff Garzik /**
3671cc0680a5STejun Heo  *	sata_link_hardreset - reset link via SATA phy reset
3672cc0680a5STejun Heo  *	@link: link to reset
3673b6103f6dSTejun Heo  *	@timing: timing parameters { interval, duratinon, timeout } in msec
3674d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
36759dadd45bSTejun Heo  *	@online: optional out parameter indicating link onlineness
36769dadd45bSTejun Heo  *	@check_ready: optional callback to check link readiness
3677c6fd2807SJeff Garzik  *
3678cc0680a5STejun Heo  *	SATA phy-reset @link using DET bits of SControl register.
36799dadd45bSTejun Heo  *	After hardreset, link readiness is waited upon using
36809dadd45bSTejun Heo  *	ata_wait_ready() if @check_ready is specified.  LLDs are
36819dadd45bSTejun Heo  *	allowed to not specify @check_ready and wait itself after this
36829dadd45bSTejun Heo  *	function returns.  Device classification is LLD's
36839dadd45bSTejun Heo  *	responsibility.
36849dadd45bSTejun Heo  *
36859dadd45bSTejun Heo  *	*@online is set to one iff reset succeeded and @link is online
36869dadd45bSTejun Heo  *	after reset.
3687c6fd2807SJeff Garzik  *
3688c6fd2807SJeff Garzik  *	LOCKING:
3689c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3690c6fd2807SJeff Garzik  *
3691c6fd2807SJeff Garzik  *	RETURNS:
3692c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
3693c6fd2807SJeff Garzik  */
3694cc0680a5STejun Heo int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
36959dadd45bSTejun Heo 			unsigned long deadline,
36969dadd45bSTejun Heo 			bool *online, int (*check_ready)(struct ata_link *))
3697c6fd2807SJeff Garzik {
3698c6fd2807SJeff Garzik 	u32 scontrol;
3699c6fd2807SJeff Garzik 	int rc;
3700c6fd2807SJeff Garzik 
3701c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
3702c6fd2807SJeff Garzik 
37039dadd45bSTejun Heo 	if (online)
37049dadd45bSTejun Heo 		*online = false;
37059dadd45bSTejun Heo 
3706936fd732STejun Heo 	if (sata_set_spd_needed(link)) {
3707c6fd2807SJeff Garzik 		/* SATA spec says nothing about how to reconfigure
3708c6fd2807SJeff Garzik 		 * spd.  To be on the safe side, turn off phy during
3709c6fd2807SJeff Garzik 		 * reconfiguration.  This works for at least ICH7 AHCI
3710c6fd2807SJeff Garzik 		 * and Sil3124.
3711c6fd2807SJeff Garzik 		 */
3712936fd732STejun Heo 		if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3713b6103f6dSTejun Heo 			goto out;
3714c6fd2807SJeff Garzik 
3715cea0d336SJeff Garzik 		scontrol = (scontrol & 0x0f0) | 0x304;
3716c6fd2807SJeff Garzik 
3717936fd732STejun Heo 		if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3718b6103f6dSTejun Heo 			goto out;
3719c6fd2807SJeff Garzik 
3720936fd732STejun Heo 		sata_set_spd(link);
3721c6fd2807SJeff Garzik 	}
3722c6fd2807SJeff Garzik 
3723c6fd2807SJeff Garzik 	/* issue phy wake/reset */
3724936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3725b6103f6dSTejun Heo 		goto out;
3726c6fd2807SJeff Garzik 
3727c6fd2807SJeff Garzik 	scontrol = (scontrol & 0x0f0) | 0x301;
3728c6fd2807SJeff Garzik 
3729936fd732STejun Heo 	if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
3730b6103f6dSTejun Heo 		goto out;
3731c6fd2807SJeff Garzik 
3732c6fd2807SJeff Garzik 	/* Couldn't find anything in SATA I/II specs, but AHCI-1.1
3733c6fd2807SJeff Garzik 	 * 10.4.2 says at least 1 ms.
3734c6fd2807SJeff Garzik 	 */
3735c6fd2807SJeff Garzik 	msleep(1);
3736c6fd2807SJeff Garzik 
3737936fd732STejun Heo 	/* bring link back */
3738936fd732STejun Heo 	rc = sata_link_resume(link, timing, deadline);
37399dadd45bSTejun Heo 	if (rc)
37409dadd45bSTejun Heo 		goto out;
37419dadd45bSTejun Heo 	/* if link is offline nothing more to do */
3742b1c72916STejun Heo 	if (ata_phys_link_offline(link))
37439dadd45bSTejun Heo 		goto out;
37449dadd45bSTejun Heo 
37459dadd45bSTejun Heo 	/* Link is online.  From this point, -ENODEV too is an error. */
37469dadd45bSTejun Heo 	if (online)
37479dadd45bSTejun Heo 		*online = true;
37489dadd45bSTejun Heo 
3749071f44b1STejun Heo 	if (sata_pmp_supported(link->ap) && ata_is_host_link(link)) {
37509dadd45bSTejun Heo 		/* If PMP is supported, we have to do follow-up SRST.
37519dadd45bSTejun Heo 		 * Some PMPs don't send D2H Reg FIS after hardreset if
37529dadd45bSTejun Heo 		 * the first port is empty.  Wait only for
37539dadd45bSTejun Heo 		 * ATA_TMOUT_PMP_SRST_WAIT.
37549dadd45bSTejun Heo 		 */
37559dadd45bSTejun Heo 		if (check_ready) {
37569dadd45bSTejun Heo 			unsigned long pmp_deadline;
37579dadd45bSTejun Heo 
3758341c2c95STejun Heo 			pmp_deadline = ata_deadline(jiffies,
3759341c2c95STejun Heo 						    ATA_TMOUT_PMP_SRST_WAIT);
37609dadd45bSTejun Heo 			if (time_after(pmp_deadline, deadline))
37619dadd45bSTejun Heo 				pmp_deadline = deadline;
37629dadd45bSTejun Heo 			ata_wait_ready(link, pmp_deadline, check_ready);
37639dadd45bSTejun Heo 		}
37649dadd45bSTejun Heo 		rc = -EAGAIN;
37659dadd45bSTejun Heo 		goto out;
37669dadd45bSTejun Heo 	}
37679dadd45bSTejun Heo 
37689dadd45bSTejun Heo 	rc = 0;
37699dadd45bSTejun Heo 	if (check_ready)
37709dadd45bSTejun Heo 		rc = ata_wait_ready(link, deadline, check_ready);
3771b6103f6dSTejun Heo  out:
37720cbf0711STejun Heo 	if (rc && rc != -EAGAIN) {
37730cbf0711STejun Heo 		/* online is set iff link is online && reset succeeded */
37740cbf0711STejun Heo 		if (online)
37750cbf0711STejun Heo 			*online = false;
37769dadd45bSTejun Heo 		ata_link_printk(link, KERN_ERR,
37779dadd45bSTejun Heo 				"COMRESET failed (errno=%d)\n", rc);
37780cbf0711STejun Heo 	}
3779b6103f6dSTejun Heo 	DPRINTK("EXIT, rc=%d\n", rc);
3780b6103f6dSTejun Heo 	return rc;
3781b6103f6dSTejun Heo }
3782b6103f6dSTejun Heo 
3783b6103f6dSTejun Heo /**
378457c9efdfSTejun Heo  *	sata_std_hardreset - COMRESET w/o waiting or classification
378557c9efdfSTejun Heo  *	@link: link to reset
378657c9efdfSTejun Heo  *	@class: resulting class of attached device
378757c9efdfSTejun Heo  *	@deadline: deadline jiffies for the operation
378857c9efdfSTejun Heo  *
378957c9efdfSTejun Heo  *	Standard SATA COMRESET w/o waiting or classification.
379057c9efdfSTejun Heo  *
379157c9efdfSTejun Heo  *	LOCKING:
379257c9efdfSTejun Heo  *	Kernel thread context (may sleep)
379357c9efdfSTejun Heo  *
379457c9efdfSTejun Heo  *	RETURNS:
379557c9efdfSTejun Heo  *	0 if link offline, -EAGAIN if link online, -errno on errors.
379657c9efdfSTejun Heo  */
379757c9efdfSTejun Heo int sata_std_hardreset(struct ata_link *link, unsigned int *class,
379857c9efdfSTejun Heo 		       unsigned long deadline)
379957c9efdfSTejun Heo {
380057c9efdfSTejun Heo 	const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
380157c9efdfSTejun Heo 	bool online;
380257c9efdfSTejun Heo 	int rc;
380357c9efdfSTejun Heo 
380457c9efdfSTejun Heo 	/* do hardreset */
380557c9efdfSTejun Heo 	rc = sata_link_hardreset(link, timing, deadline, &online, NULL);
380657c9efdfSTejun Heo 	return online ? -EAGAIN : rc;
380757c9efdfSTejun Heo }
380857c9efdfSTejun Heo 
380957c9efdfSTejun Heo /**
3810203c75b8STejun Heo  *	ata_std_postreset - standard postreset callback
3811cc0680a5STejun Heo  *	@link: the target ata_link
3812c6fd2807SJeff Garzik  *	@classes: classes of attached devices
3813c6fd2807SJeff Garzik  *
3814c6fd2807SJeff Garzik  *	This function is invoked after a successful reset.  Note that
3815c6fd2807SJeff Garzik  *	the device might have been reset more than once using
3816c6fd2807SJeff Garzik  *	different reset methods before postreset is invoked.
3817c6fd2807SJeff Garzik  *
3818c6fd2807SJeff Garzik  *	LOCKING:
3819c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3820c6fd2807SJeff Garzik  */
3821203c75b8STejun Heo void ata_std_postreset(struct ata_link *link, unsigned int *classes)
3822c6fd2807SJeff Garzik {
3823f046519fSTejun Heo 	u32 serror;
3824f046519fSTejun Heo 
3825c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
3826c6fd2807SJeff Garzik 
3827f046519fSTejun Heo 	/* reset complete, clear SError */
3828f046519fSTejun Heo 	if (!sata_scr_read(link, SCR_ERROR, &serror))
3829f046519fSTejun Heo 		sata_scr_write(link, SCR_ERROR, serror);
3830f046519fSTejun Heo 
3831c6fd2807SJeff Garzik 	/* print link status */
3832936fd732STejun Heo 	sata_print_link_status(link);
3833c6fd2807SJeff Garzik 
3834c6fd2807SJeff Garzik 	DPRINTK("EXIT\n");
3835c6fd2807SJeff Garzik }
3836c6fd2807SJeff Garzik 
3837c6fd2807SJeff Garzik /**
3838c6fd2807SJeff Garzik  *	ata_dev_same_device - Determine whether new ID matches configured device
3839c6fd2807SJeff Garzik  *	@dev: device to compare against
3840c6fd2807SJeff Garzik  *	@new_class: class of the new device
3841c6fd2807SJeff Garzik  *	@new_id: IDENTIFY page of the new device
3842c6fd2807SJeff Garzik  *
3843c6fd2807SJeff Garzik  *	Compare @new_class and @new_id against @dev and determine
3844c6fd2807SJeff Garzik  *	whether @dev is the device indicated by @new_class and
3845c6fd2807SJeff Garzik  *	@new_id.
3846c6fd2807SJeff Garzik  *
3847c6fd2807SJeff Garzik  *	LOCKING:
3848c6fd2807SJeff Garzik  *	None.
3849c6fd2807SJeff Garzik  *
3850c6fd2807SJeff Garzik  *	RETURNS:
3851c6fd2807SJeff Garzik  *	1 if @dev matches @new_class and @new_id, 0 otherwise.
3852c6fd2807SJeff Garzik  */
3853c6fd2807SJeff Garzik static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
3854c6fd2807SJeff Garzik 			       const u16 *new_id)
3855c6fd2807SJeff Garzik {
3856c6fd2807SJeff Garzik 	const u16 *old_id = dev->id;
3857a0cf733bSTejun Heo 	unsigned char model[2][ATA_ID_PROD_LEN + 1];
3858a0cf733bSTejun Heo 	unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
3859c6fd2807SJeff Garzik 
3860c6fd2807SJeff Garzik 	if (dev->class != new_class) {
3861c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_INFO, "class mismatch %d != %d\n",
3862c6fd2807SJeff Garzik 			       dev->class, new_class);
3863c6fd2807SJeff Garzik 		return 0;
3864c6fd2807SJeff Garzik 	}
3865c6fd2807SJeff Garzik 
3866a0cf733bSTejun Heo 	ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
3867a0cf733bSTejun Heo 	ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
3868a0cf733bSTejun Heo 	ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
3869a0cf733bSTejun Heo 	ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
3870c6fd2807SJeff Garzik 
3871c6fd2807SJeff Garzik 	if (strcmp(model[0], model[1])) {
3872c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_INFO, "model number mismatch "
3873c6fd2807SJeff Garzik 			       "'%s' != '%s'\n", model[0], model[1]);
3874c6fd2807SJeff Garzik 		return 0;
3875c6fd2807SJeff Garzik 	}
3876c6fd2807SJeff Garzik 
3877c6fd2807SJeff Garzik 	if (strcmp(serial[0], serial[1])) {
3878c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_INFO, "serial number mismatch "
3879c6fd2807SJeff Garzik 			       "'%s' != '%s'\n", serial[0], serial[1]);
3880c6fd2807SJeff Garzik 		return 0;
3881c6fd2807SJeff Garzik 	}
3882c6fd2807SJeff Garzik 
3883c6fd2807SJeff Garzik 	return 1;
3884c6fd2807SJeff Garzik }
3885c6fd2807SJeff Garzik 
3886c6fd2807SJeff Garzik /**
3887fe30911bSTejun Heo  *	ata_dev_reread_id - Re-read IDENTIFY data
38883fae450cSHenrik Kretzschmar  *	@dev: target ATA device
3889bff04647STejun Heo  *	@readid_flags: read ID flags
3890c6fd2807SJeff Garzik  *
3891c6fd2807SJeff Garzik  *	Re-read IDENTIFY page and make sure @dev is still attached to
3892c6fd2807SJeff Garzik  *	the port.
3893c6fd2807SJeff Garzik  *
3894c6fd2807SJeff Garzik  *	LOCKING:
3895c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3896c6fd2807SJeff Garzik  *
3897c6fd2807SJeff Garzik  *	RETURNS:
3898c6fd2807SJeff Garzik  *	0 on success, negative errno otherwise
3899c6fd2807SJeff Garzik  */
3900fe30911bSTejun Heo int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
3901c6fd2807SJeff Garzik {
3902c6fd2807SJeff Garzik 	unsigned int class = dev->class;
39039af5c9c9STejun Heo 	u16 *id = (void *)dev->link->ap->sector_buf;
3904c6fd2807SJeff Garzik 	int rc;
3905c6fd2807SJeff Garzik 
3906c6fd2807SJeff Garzik 	/* read ID data */
3907bff04647STejun Heo 	rc = ata_dev_read_id(dev, &class, readid_flags, id);
3908c6fd2807SJeff Garzik 	if (rc)
3909fe30911bSTejun Heo 		return rc;
3910c6fd2807SJeff Garzik 
3911c6fd2807SJeff Garzik 	/* is the device still there? */
3912fe30911bSTejun Heo 	if (!ata_dev_same_device(dev, class, id))
3913fe30911bSTejun Heo 		return -ENODEV;
3914c6fd2807SJeff Garzik 
3915c6fd2807SJeff Garzik 	memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
3916fe30911bSTejun Heo 	return 0;
3917fe30911bSTejun Heo }
3918fe30911bSTejun Heo 
3919fe30911bSTejun Heo /**
3920fe30911bSTejun Heo  *	ata_dev_revalidate - Revalidate ATA device
3921fe30911bSTejun Heo  *	@dev: device to revalidate
3922422c9daaSTejun Heo  *	@new_class: new class code
3923fe30911bSTejun Heo  *	@readid_flags: read ID flags
3924fe30911bSTejun Heo  *
3925fe30911bSTejun Heo  *	Re-read IDENTIFY page, make sure @dev is still attached to the
3926fe30911bSTejun Heo  *	port and reconfigure it according to the new IDENTIFY page.
3927fe30911bSTejun Heo  *
3928fe30911bSTejun Heo  *	LOCKING:
3929fe30911bSTejun Heo  *	Kernel thread context (may sleep)
3930fe30911bSTejun Heo  *
3931fe30911bSTejun Heo  *	RETURNS:
3932fe30911bSTejun Heo  *	0 on success, negative errno otherwise
3933fe30911bSTejun Heo  */
3934422c9daaSTejun Heo int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
3935422c9daaSTejun Heo 		       unsigned int readid_flags)
3936fe30911bSTejun Heo {
39376ddcd3b0STejun Heo 	u64 n_sectors = dev->n_sectors;
3938fe30911bSTejun Heo 	int rc;
3939fe30911bSTejun Heo 
3940fe30911bSTejun Heo 	if (!ata_dev_enabled(dev))
3941fe30911bSTejun Heo 		return -ENODEV;
3942fe30911bSTejun Heo 
3943422c9daaSTejun Heo 	/* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
3944422c9daaSTejun Heo 	if (ata_class_enabled(new_class) &&
3945422c9daaSTejun Heo 	    new_class != ATA_DEV_ATA && new_class != ATA_DEV_ATAPI) {
3946422c9daaSTejun Heo 		ata_dev_printk(dev, KERN_INFO, "class mismatch %u != %u\n",
3947422c9daaSTejun Heo 			       dev->class, new_class);
3948422c9daaSTejun Heo 		rc = -ENODEV;
3949422c9daaSTejun Heo 		goto fail;
3950422c9daaSTejun Heo 	}
3951422c9daaSTejun Heo 
3952fe30911bSTejun Heo 	/* re-read ID */
3953fe30911bSTejun Heo 	rc = ata_dev_reread_id(dev, readid_flags);
3954fe30911bSTejun Heo 	if (rc)
3955fe30911bSTejun Heo 		goto fail;
3956c6fd2807SJeff Garzik 
3957c6fd2807SJeff Garzik 	/* configure device according to the new ID */
3958efdaedc4STejun Heo 	rc = ata_dev_configure(dev);
39596ddcd3b0STejun Heo 	if (rc)
39606ddcd3b0STejun Heo 		goto fail;
39616ddcd3b0STejun Heo 
39626ddcd3b0STejun Heo 	/* verify n_sectors hasn't changed */
3963b54eebd6STejun Heo 	if (dev->class == ATA_DEV_ATA && n_sectors &&
3964b54eebd6STejun Heo 	    dev->n_sectors != n_sectors) {
39656ddcd3b0STejun Heo 		ata_dev_printk(dev, KERN_INFO, "n_sectors mismatch "
39666ddcd3b0STejun Heo 			       "%llu != %llu\n",
39676ddcd3b0STejun Heo 			       (unsigned long long)n_sectors,
39686ddcd3b0STejun Heo 			       (unsigned long long)dev->n_sectors);
39698270bec4STejun Heo 
39708270bec4STejun Heo 		/* restore original n_sectors */
39718270bec4STejun Heo 		dev->n_sectors = n_sectors;
39728270bec4STejun Heo 
39736ddcd3b0STejun Heo 		rc = -ENODEV;
39746ddcd3b0STejun Heo 		goto fail;
39756ddcd3b0STejun Heo 	}
39766ddcd3b0STejun Heo 
3977c6fd2807SJeff Garzik 	return 0;
3978c6fd2807SJeff Garzik 
3979c6fd2807SJeff Garzik  fail:
3980c6fd2807SJeff Garzik 	ata_dev_printk(dev, KERN_ERR, "revalidation failed (errno=%d)\n", rc);
3981c6fd2807SJeff Garzik 	return rc;
3982c6fd2807SJeff Garzik }
3983c6fd2807SJeff Garzik 
39846919a0a6SAlan Cox struct ata_blacklist_entry {
39856919a0a6SAlan Cox 	const char *model_num;
39866919a0a6SAlan Cox 	const char *model_rev;
39876919a0a6SAlan Cox 	unsigned long horkage;
39886919a0a6SAlan Cox };
39896919a0a6SAlan Cox 
39906919a0a6SAlan Cox static const struct ata_blacklist_entry ata_device_blacklist [] = {
39916919a0a6SAlan Cox 	/* Devices with DMA related problems under Linux */
39926919a0a6SAlan Cox 	{ "WDC AC11000H",	NULL,		ATA_HORKAGE_NODMA },
39936919a0a6SAlan Cox 	{ "WDC AC22100H",	NULL,		ATA_HORKAGE_NODMA },
39946919a0a6SAlan Cox 	{ "WDC AC32500H",	NULL,		ATA_HORKAGE_NODMA },
39956919a0a6SAlan Cox 	{ "WDC AC33100H",	NULL,		ATA_HORKAGE_NODMA },
39966919a0a6SAlan Cox 	{ "WDC AC31600H",	NULL,		ATA_HORKAGE_NODMA },
39976919a0a6SAlan Cox 	{ "WDC AC32100H",	"24.09P07",	ATA_HORKAGE_NODMA },
39986919a0a6SAlan Cox 	{ "WDC AC23200L",	"21.10N21",	ATA_HORKAGE_NODMA },
39996919a0a6SAlan Cox 	{ "Compaq CRD-8241B", 	NULL,		ATA_HORKAGE_NODMA },
40006919a0a6SAlan Cox 	{ "CRD-8400B",		NULL, 		ATA_HORKAGE_NODMA },
40016919a0a6SAlan Cox 	{ "CRD-8480B",		NULL,		ATA_HORKAGE_NODMA },
40026919a0a6SAlan Cox 	{ "CRD-8482B",		NULL,		ATA_HORKAGE_NODMA },
40036919a0a6SAlan Cox 	{ "CRD-84",		NULL,		ATA_HORKAGE_NODMA },
40046919a0a6SAlan Cox 	{ "SanDisk SDP3B",	NULL,		ATA_HORKAGE_NODMA },
40056919a0a6SAlan Cox 	{ "SanDisk SDP3B-64",	NULL,		ATA_HORKAGE_NODMA },
40066919a0a6SAlan Cox 	{ "SANYO CD-ROM CRD",	NULL,		ATA_HORKAGE_NODMA },
40076919a0a6SAlan Cox 	{ "HITACHI CDR-8",	NULL,		ATA_HORKAGE_NODMA },
40086919a0a6SAlan Cox 	{ "HITACHI CDR-8335",	NULL,		ATA_HORKAGE_NODMA },
40096919a0a6SAlan Cox 	{ "HITACHI CDR-8435",	NULL,		ATA_HORKAGE_NODMA },
40106919a0a6SAlan Cox 	{ "Toshiba CD-ROM XM-6202B", NULL,	ATA_HORKAGE_NODMA },
40116919a0a6SAlan Cox 	{ "TOSHIBA CD-ROM XM-1702BC", NULL,	ATA_HORKAGE_NODMA },
40126919a0a6SAlan Cox 	{ "CD-532E-A", 		NULL,		ATA_HORKAGE_NODMA },
40136919a0a6SAlan Cox 	{ "E-IDE CD-ROM CR-840",NULL,		ATA_HORKAGE_NODMA },
40146919a0a6SAlan Cox 	{ "CD-ROM Drive/F5A",	NULL,		ATA_HORKAGE_NODMA },
40156919a0a6SAlan Cox 	{ "WPI CDD-820", 	NULL,		ATA_HORKAGE_NODMA },
40166919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SC-148C", NULL,	ATA_HORKAGE_NODMA },
40176919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SC",	NULL,		ATA_HORKAGE_NODMA },
40186919a0a6SAlan Cox 	{ "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
40196919a0a6SAlan Cox 	{ "_NEC DV5800A", 	NULL,		ATA_HORKAGE_NODMA },
40206919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SN-124", "N001",	ATA_HORKAGE_NODMA },
402139f19886SDave Jones 	{ "Seagate STT20000A", NULL,		ATA_HORKAGE_NODMA },
40223af9a77aSTejun Heo 	/* Odd clown on sil3726/4726 PMPs */
402350af2fa1STejun Heo 	{ "Config  Disk",	NULL,		ATA_HORKAGE_DISABLE },
40246919a0a6SAlan Cox 
402518d6e9d5SAlbert Lee 	/* Weird ATAPI devices */
402640a1d531STejun Heo 	{ "TORiSAN DVD-ROM DRD-N216", NULL,	ATA_HORKAGE_MAX_SEC_128 },
40276a87e42eSTejun Heo 	{ "QUANTUM DAT    DAT72-000", NULL,	ATA_HORKAGE_ATAPI_MOD16_DMA },
402818d6e9d5SAlbert Lee 
40296919a0a6SAlan Cox 	/* Devices we expect to fail diagnostics */
40306919a0a6SAlan Cox 
40316919a0a6SAlan Cox 	/* Devices where NCQ should be avoided */
40326919a0a6SAlan Cox 	/* NCQ is slow */
40336919a0a6SAlan Cox 	{ "WDC WD740ADFD-00",	NULL,		ATA_HORKAGE_NONCQ },
4034459ad688STejun Heo 	{ "WDC WD740ADFD-00NLR1", NULL,		ATA_HORKAGE_NONCQ, },
403509125ea6STejun Heo 	/* http://thread.gmane.org/gmane.linux.ide/14907 */
403609125ea6STejun Heo 	{ "FUJITSU MHT2060BH",	NULL,		ATA_HORKAGE_NONCQ },
40377acfaf30SPaul Rolland 	/* NCQ is broken */
4038539cc7c7SJeff Garzik 	{ "Maxtor *",		"BANC*",	ATA_HORKAGE_NONCQ },
40390e3dbc01SAlan Cox 	{ "Maxtor 7V300F0",	"VA111630",	ATA_HORKAGE_NONCQ },
4040da6f0ec2SPaolo Ornati 	{ "ST380817AS",		"3.42",		ATA_HORKAGE_NONCQ },
4041e41bd3e8STejun Heo 	{ "ST3160023AS",	"3.42",		ATA_HORKAGE_NONCQ },
4042539cc7c7SJeff Garzik 
404336e337d0SRobert Hancock 	/* Blacklist entries taken from Silicon Image 3124/3132
404436e337d0SRobert Hancock 	   Windows driver .inf file - also several Linux problem reports */
404536e337d0SRobert Hancock 	{ "HTS541060G9SA00",    "MB3OC60D",     ATA_HORKAGE_NONCQ, },
404636e337d0SRobert Hancock 	{ "HTS541080G9SA00",    "MB4OC60D",     ATA_HORKAGE_NONCQ, },
404736e337d0SRobert Hancock 	{ "HTS541010G9SA00",    "MBZOC60D",     ATA_HORKAGE_NONCQ, },
40486919a0a6SAlan Cox 
404916c55b03STejun Heo 	/* devices which puke on READ_NATIVE_MAX */
405016c55b03STejun Heo 	{ "HDS724040KLSA80",	"KFAOA20N",	ATA_HORKAGE_BROKEN_HPA, },
405116c55b03STejun Heo 	{ "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
405216c55b03STejun Heo 	{ "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
405316c55b03STejun Heo 	{ "MAXTOR 6L080L4",	"A93.0500",	ATA_HORKAGE_BROKEN_HPA },
40546919a0a6SAlan Cox 
405593328e11SAlan Cox 	/* Devices which report 1 sector over size HPA */
405693328e11SAlan Cox 	{ "ST340823A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
405793328e11SAlan Cox 	{ "ST320413A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
4058b152fcd3SMikko Rapeli 	{ "ST310211A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
405993328e11SAlan Cox 
40606bbfd53dSAlan Cox 	/* Devices which get the IVB wrong */
40616bbfd53dSAlan Cox 	{ "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB, },
4062a79067e5SAlan Cox 	/* Maybe we should just blacklist TSSTcorp... */
4063a79067e5SAlan Cox 	{ "TSSTcorp CDDVDW SH-S202H", "SB00",	  ATA_HORKAGE_IVB, },
4064a79067e5SAlan Cox 	{ "TSSTcorp CDDVDW SH-S202H", "SB01",	  ATA_HORKAGE_IVB, },
40656bbfd53dSAlan Cox 	{ "TSSTcorp CDDVDW SH-S202J", "SB00",	  ATA_HORKAGE_IVB, },
4066e9f33406SPeter Missel 	{ "TSSTcorp CDDVDW SH-S202J", "SB01",	  ATA_HORKAGE_IVB, },
4067e9f33406SPeter Missel 	{ "TSSTcorp CDDVDW SH-S202N", "SB00",	  ATA_HORKAGE_IVB, },
4068e9f33406SPeter Missel 	{ "TSSTcorp CDDVDW SH-S202N", "SB01",	  ATA_HORKAGE_IVB, },
40696bbfd53dSAlan Cox 
40709ce8e307SJens Axboe 	/* Devices that do not need bridging limits applied */
40719ce8e307SJens Axboe 	{ "MTRON MSP-SATA*",		NULL,	ATA_HORKAGE_BRIDGE_OK, },
40729ce8e307SJens Axboe 
40736919a0a6SAlan Cox 	/* End Marker */
40746919a0a6SAlan Cox 	{ }
4075c6fd2807SJeff Garzik };
4076c6fd2807SJeff Garzik 
4077741b7763SAdrian Bunk static int strn_pattern_cmp(const char *patt, const char *name, int wildchar)
4078539cc7c7SJeff Garzik {
4079539cc7c7SJeff Garzik 	const char *p;
4080539cc7c7SJeff Garzik 	int len;
4081539cc7c7SJeff Garzik 
4082539cc7c7SJeff Garzik 	/*
4083539cc7c7SJeff Garzik 	 * check for trailing wildcard: *\0
4084539cc7c7SJeff Garzik 	 */
4085539cc7c7SJeff Garzik 	p = strchr(patt, wildchar);
4086539cc7c7SJeff Garzik 	if (p && ((*(p + 1)) == 0))
4087539cc7c7SJeff Garzik 		len = p - patt;
4088317b50b8SAndrew Paprocki 	else {
4089539cc7c7SJeff Garzik 		len = strlen(name);
4090317b50b8SAndrew Paprocki 		if (!len) {
4091317b50b8SAndrew Paprocki 			if (!*patt)
4092317b50b8SAndrew Paprocki 				return 0;
4093317b50b8SAndrew Paprocki 			return -1;
4094317b50b8SAndrew Paprocki 		}
4095317b50b8SAndrew Paprocki 	}
4096539cc7c7SJeff Garzik 
4097539cc7c7SJeff Garzik 	return strncmp(patt, name, len);
4098539cc7c7SJeff Garzik }
4099539cc7c7SJeff Garzik 
410075683fe7STejun Heo static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
4101c6fd2807SJeff Garzik {
41028bfa79fcSTejun Heo 	unsigned char model_num[ATA_ID_PROD_LEN + 1];
41038bfa79fcSTejun Heo 	unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
41046919a0a6SAlan Cox 	const struct ata_blacklist_entry *ad = ata_device_blacklist;
4105c6fd2807SJeff Garzik 
41068bfa79fcSTejun Heo 	ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
41078bfa79fcSTejun Heo 	ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
4108c6fd2807SJeff Garzik 
41096919a0a6SAlan Cox 	while (ad->model_num) {
4110539cc7c7SJeff Garzik 		if (!strn_pattern_cmp(ad->model_num, model_num, '*')) {
41116919a0a6SAlan Cox 			if (ad->model_rev == NULL)
41126919a0a6SAlan Cox 				return ad->horkage;
4113539cc7c7SJeff Garzik 			if (!strn_pattern_cmp(ad->model_rev, model_rev, '*'))
41146919a0a6SAlan Cox 				return ad->horkage;
4115c6fd2807SJeff Garzik 		}
41166919a0a6SAlan Cox 		ad++;
4117c6fd2807SJeff Garzik 	}
4118c6fd2807SJeff Garzik 	return 0;
4119c6fd2807SJeff Garzik }
4120c6fd2807SJeff Garzik 
41216919a0a6SAlan Cox static int ata_dma_blacklisted(const struct ata_device *dev)
41226919a0a6SAlan Cox {
41236919a0a6SAlan Cox 	/* We don't support polling DMA.
41246919a0a6SAlan Cox 	 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
41256919a0a6SAlan Cox 	 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
41266919a0a6SAlan Cox 	 */
41279af5c9c9STejun Heo 	if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
41286919a0a6SAlan Cox 	    (dev->flags & ATA_DFLAG_CDB_INTR))
41296919a0a6SAlan Cox 		return 1;
413075683fe7STejun Heo 	return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
41316919a0a6SAlan Cox }
41326919a0a6SAlan Cox 
4133c6fd2807SJeff Garzik /**
41346bbfd53dSAlan Cox  *	ata_is_40wire		-	check drive side detection
41356bbfd53dSAlan Cox  *	@dev: device
41366bbfd53dSAlan Cox  *
41376bbfd53dSAlan Cox  *	Perform drive side detection decoding, allowing for device vendors
41386bbfd53dSAlan Cox  *	who can't follow the documentation.
41396bbfd53dSAlan Cox  */
41406bbfd53dSAlan Cox 
41416bbfd53dSAlan Cox static int ata_is_40wire(struct ata_device *dev)
41426bbfd53dSAlan Cox {
41436bbfd53dSAlan Cox 	if (dev->horkage & ATA_HORKAGE_IVB)
41446bbfd53dSAlan Cox 		return ata_drive_40wire_relaxed(dev->id);
41456bbfd53dSAlan Cox 	return ata_drive_40wire(dev->id);
41466bbfd53dSAlan Cox }
41476bbfd53dSAlan Cox 
41486bbfd53dSAlan Cox /**
414915a5551cSAlan Cox  *	cable_is_40wire		-	40/80/SATA decider
415015a5551cSAlan Cox  *	@ap: port to consider
415115a5551cSAlan Cox  *
415215a5551cSAlan Cox  *	This function encapsulates the policy for speed management
415315a5551cSAlan Cox  *	in one place. At the moment we don't cache the result but
415415a5551cSAlan Cox  *	there is a good case for setting ap->cbl to the result when
415515a5551cSAlan Cox  *	we are called with unknown cables (and figuring out if it
415615a5551cSAlan Cox  *	impacts hotplug at all).
415715a5551cSAlan Cox  *
415815a5551cSAlan Cox  *	Return 1 if the cable appears to be 40 wire.
415915a5551cSAlan Cox  */
416015a5551cSAlan Cox 
416115a5551cSAlan Cox static int cable_is_40wire(struct ata_port *ap)
416215a5551cSAlan Cox {
416315a5551cSAlan Cox 	struct ata_link *link;
416415a5551cSAlan Cox 	struct ata_device *dev;
416515a5551cSAlan Cox 
41664a9c7b33STejun Heo 	/* If the controller thinks we are 40 wire, we are. */
416715a5551cSAlan Cox 	if (ap->cbl == ATA_CBL_PATA40)
416815a5551cSAlan Cox 		return 1;
41694a9c7b33STejun Heo 
41704a9c7b33STejun Heo 	/* If the controller thinks we are 80 wire, we are. */
417115a5551cSAlan Cox 	if (ap->cbl == ATA_CBL_PATA80 || ap->cbl == ATA_CBL_SATA)
417215a5551cSAlan Cox 		return 0;
41734a9c7b33STejun Heo 
41744a9c7b33STejun Heo 	/* If the system is known to be 40 wire short cable (eg
41754a9c7b33STejun Heo 	 * laptop), then we allow 80 wire modes even if the drive
41764a9c7b33STejun Heo 	 * isn't sure.
41774a9c7b33STejun Heo 	 */
4178f792068eSAlan Cox 	if (ap->cbl == ATA_CBL_PATA40_SHORT)
4179f792068eSAlan Cox 		return 0;
418015a5551cSAlan Cox 
41814a9c7b33STejun Heo 	/* If the controller doesn't know, we scan.
41824a9c7b33STejun Heo 	 *
41834a9c7b33STejun Heo 	 * Note: We look for all 40 wire detects at this point.  Any
41844a9c7b33STejun Heo 	 *       80 wire detect is taken to be 80 wire cable because
41854a9c7b33STejun Heo 	 * - in many setups only the one drive (slave if present) will
41864a9c7b33STejun Heo 	 *   give a valid detect
41874a9c7b33STejun Heo 	 * - if you have a non detect capable drive you don't want it
41884a9c7b33STejun Heo 	 *   to colour the choice
418915a5551cSAlan Cox 	 */
419015a5551cSAlan Cox 	ata_port_for_each_link(link, ap) {
419115a5551cSAlan Cox 		ata_link_for_each_dev(dev, link) {
41924a9c7b33STejun Heo 			if (ata_dev_enabled(dev) && !ata_is_40wire(dev))
419315a5551cSAlan Cox 				return 0;
419415a5551cSAlan Cox 		}
419515a5551cSAlan Cox 	}
419615a5551cSAlan Cox 	return 1;
419715a5551cSAlan Cox }
419815a5551cSAlan Cox 
419915a5551cSAlan Cox /**
4200c6fd2807SJeff Garzik  *	ata_dev_xfermask - Compute supported xfermask of the given device
4201c6fd2807SJeff Garzik  *	@dev: Device to compute xfermask for
4202c6fd2807SJeff Garzik  *
4203c6fd2807SJeff Garzik  *	Compute supported xfermask of @dev and store it in
4204c6fd2807SJeff Garzik  *	dev->*_mask.  This function is responsible for applying all
4205c6fd2807SJeff Garzik  *	known limits including host controller limits, device
4206c6fd2807SJeff Garzik  *	blacklist, etc...
4207c6fd2807SJeff Garzik  *
4208c6fd2807SJeff Garzik  *	LOCKING:
4209c6fd2807SJeff Garzik  *	None.
4210c6fd2807SJeff Garzik  */
4211c6fd2807SJeff Garzik static void ata_dev_xfermask(struct ata_device *dev)
4212c6fd2807SJeff Garzik {
42139af5c9c9STejun Heo 	struct ata_link *link = dev->link;
42149af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
4215cca3974eSJeff Garzik 	struct ata_host *host = ap->host;
4216c6fd2807SJeff Garzik 	unsigned long xfer_mask;
4217c6fd2807SJeff Garzik 
4218c6fd2807SJeff Garzik 	/* controller modes available */
4219c6fd2807SJeff Garzik 	xfer_mask = ata_pack_xfermask(ap->pio_mask,
4220c6fd2807SJeff Garzik 				      ap->mwdma_mask, ap->udma_mask);
4221c6fd2807SJeff Garzik 
42228343f889SRobert Hancock 	/* drive modes available */
4223c6fd2807SJeff Garzik 	xfer_mask &= ata_pack_xfermask(dev->pio_mask,
4224c6fd2807SJeff Garzik 				       dev->mwdma_mask, dev->udma_mask);
4225c6fd2807SJeff Garzik 	xfer_mask &= ata_id_xfermask(dev->id);
4226c6fd2807SJeff Garzik 
4227b352e57dSAlan Cox 	/*
4228b352e57dSAlan Cox 	 *	CFA Advanced TrueIDE timings are not allowed on a shared
4229b352e57dSAlan Cox 	 *	cable
4230b352e57dSAlan Cox 	 */
4231b352e57dSAlan Cox 	if (ata_dev_pair(dev)) {
4232b352e57dSAlan Cox 		/* No PIO5 or PIO6 */
4233b352e57dSAlan Cox 		xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
4234b352e57dSAlan Cox 		/* No MWDMA3 or MWDMA 4 */
4235b352e57dSAlan Cox 		xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
4236b352e57dSAlan Cox 	}
4237b352e57dSAlan Cox 
4238c6fd2807SJeff Garzik 	if (ata_dma_blacklisted(dev)) {
4239c6fd2807SJeff Garzik 		xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4240c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_WARNING,
4241c6fd2807SJeff Garzik 			       "device is on DMA blacklist, disabling DMA\n");
4242c6fd2807SJeff Garzik 	}
4243c6fd2807SJeff Garzik 
424414d66ab7SPetr Vandrovec 	if ((host->flags & ATA_HOST_SIMPLEX) &&
424514d66ab7SPetr Vandrovec 	    host->simplex_claimed && host->simplex_claimed != ap) {
4246c6fd2807SJeff Garzik 		xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4247c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_WARNING, "simplex DMA is claimed by "
4248c6fd2807SJeff Garzik 			       "other device, disabling DMA\n");
4249c6fd2807SJeff Garzik 	}
4250c6fd2807SJeff Garzik 
4251e424675fSJeff Garzik 	if (ap->flags & ATA_FLAG_NO_IORDY)
4252e424675fSJeff Garzik 		xfer_mask &= ata_pio_mask_no_iordy(dev);
4253e424675fSJeff Garzik 
4254c6fd2807SJeff Garzik 	if (ap->ops->mode_filter)
4255a76b62caSAlan Cox 		xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
4256c6fd2807SJeff Garzik 
42578343f889SRobert Hancock 	/* Apply cable rule here.  Don't apply it early because when
42588343f889SRobert Hancock 	 * we handle hot plug the cable type can itself change.
42598343f889SRobert Hancock 	 * Check this last so that we know if the transfer rate was
42608343f889SRobert Hancock 	 * solely limited by the cable.
42618343f889SRobert Hancock 	 * Unknown or 80 wire cables reported host side are checked
42628343f889SRobert Hancock 	 * drive side as well. Cases where we know a 40wire cable
42638343f889SRobert Hancock 	 * is used safely for 80 are not checked here.
42648343f889SRobert Hancock 	 */
42658343f889SRobert Hancock 	if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
42668343f889SRobert Hancock 		/* UDMA/44 or higher would be available */
426715a5551cSAlan Cox 		if (cable_is_40wire(ap)) {
42688343f889SRobert Hancock 			ata_dev_printk(dev, KERN_WARNING,
42698343f889SRobert Hancock 				 "limited to UDMA/33 due to 40-wire cable\n");
42708343f889SRobert Hancock 			xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
42718343f889SRobert Hancock 		}
42728343f889SRobert Hancock 
4273c6fd2807SJeff Garzik 	ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
4274c6fd2807SJeff Garzik 			    &dev->mwdma_mask, &dev->udma_mask);
4275c6fd2807SJeff Garzik }
4276c6fd2807SJeff Garzik 
4277c6fd2807SJeff Garzik /**
4278c6fd2807SJeff Garzik  *	ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
4279c6fd2807SJeff Garzik  *	@dev: Device to which command will be sent
4280c6fd2807SJeff Garzik  *
4281c6fd2807SJeff Garzik  *	Issue SET FEATURES - XFER MODE command to device @dev
4282c6fd2807SJeff Garzik  *	on port @ap.
4283c6fd2807SJeff Garzik  *
4284c6fd2807SJeff Garzik  *	LOCKING:
4285c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
4286c6fd2807SJeff Garzik  *
4287c6fd2807SJeff Garzik  *	RETURNS:
4288c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
4289c6fd2807SJeff Garzik  */
4290c6fd2807SJeff Garzik 
4291c6fd2807SJeff Garzik static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
4292c6fd2807SJeff Garzik {
4293c6fd2807SJeff Garzik 	struct ata_taskfile tf;
4294c6fd2807SJeff Garzik 	unsigned int err_mask;
4295c6fd2807SJeff Garzik 
4296c6fd2807SJeff Garzik 	/* set up set-features taskfile */
4297c6fd2807SJeff Garzik 	DPRINTK("set features - xfer mode\n");
4298c6fd2807SJeff Garzik 
4299464cf177STejun Heo 	/* Some controllers and ATAPI devices show flaky interrupt
4300464cf177STejun Heo 	 * behavior after setting xfer mode.  Use polling instead.
4301464cf177STejun Heo 	 */
4302c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
4303c6fd2807SJeff Garzik 	tf.command = ATA_CMD_SET_FEATURES;
4304c6fd2807SJeff Garzik 	tf.feature = SETFEATURES_XFER;
4305464cf177STejun Heo 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
4306c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
4307b9f8ab2dSAlan Cox 	/* If we are using IORDY we must send the mode setting command */
4308b9f8ab2dSAlan Cox 	if (ata_pio_need_iordy(dev))
4309c6fd2807SJeff Garzik 		tf.nsect = dev->xfer_mode;
4310b9f8ab2dSAlan Cox 	/* If the device has IORDY and the controller does not - turn it off */
4311b9f8ab2dSAlan Cox  	else if (ata_id_has_iordy(dev->id))
4312b9f8ab2dSAlan Cox 		tf.nsect = 0x01;
4313b9f8ab2dSAlan Cox 	else /* In the ancient relic department - skip all of this */
4314b9f8ab2dSAlan Cox 		return 0;
4315c6fd2807SJeff Garzik 
43162b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
4317c6fd2807SJeff Garzik 
4318c6fd2807SJeff Garzik 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
4319c6fd2807SJeff Garzik 	return err_mask;
4320c6fd2807SJeff Garzik }
4321c6fd2807SJeff Garzik /**
4322218f3d30SJeff Garzik  *	ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES
43239f45cbd3SKristen Carlson Accardi  *	@dev: Device to which command will be sent
43249f45cbd3SKristen Carlson Accardi  *	@enable: Whether to enable or disable the feature
4325218f3d30SJeff Garzik  *	@feature: The sector count represents the feature to set
43269f45cbd3SKristen Carlson Accardi  *
43279f45cbd3SKristen Carlson Accardi  *	Issue SET FEATURES - SATA FEATURES command to device @dev
4328218f3d30SJeff Garzik  *	on port @ap with sector count
43299f45cbd3SKristen Carlson Accardi  *
43309f45cbd3SKristen Carlson Accardi  *	LOCKING:
43319f45cbd3SKristen Carlson Accardi  *	PCI/etc. bus probe sem.
43329f45cbd3SKristen Carlson Accardi  *
43339f45cbd3SKristen Carlson Accardi  *	RETURNS:
43349f45cbd3SKristen Carlson Accardi  *	0 on success, AC_ERR_* mask otherwise.
43359f45cbd3SKristen Carlson Accardi  */
4336218f3d30SJeff Garzik static unsigned int ata_dev_set_feature(struct ata_device *dev, u8 enable,
4337218f3d30SJeff Garzik 					u8 feature)
43389f45cbd3SKristen Carlson Accardi {
43399f45cbd3SKristen Carlson Accardi 	struct ata_taskfile tf;
43409f45cbd3SKristen Carlson Accardi 	unsigned int err_mask;
43419f45cbd3SKristen Carlson Accardi 
43429f45cbd3SKristen Carlson Accardi 	/* set up set-features taskfile */
43439f45cbd3SKristen Carlson Accardi 	DPRINTK("set features - SATA features\n");
43449f45cbd3SKristen Carlson Accardi 
43459f45cbd3SKristen Carlson Accardi 	ata_tf_init(dev, &tf);
43469f45cbd3SKristen Carlson Accardi 	tf.command = ATA_CMD_SET_FEATURES;
43479f45cbd3SKristen Carlson Accardi 	tf.feature = enable;
43489f45cbd3SKristen Carlson Accardi 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
43499f45cbd3SKristen Carlson Accardi 	tf.protocol = ATA_PROT_NODATA;
4350218f3d30SJeff Garzik 	tf.nsect = feature;
43519f45cbd3SKristen Carlson Accardi 
43522b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
43539f45cbd3SKristen Carlson Accardi 
43549f45cbd3SKristen Carlson Accardi 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
43559f45cbd3SKristen Carlson Accardi 	return err_mask;
43569f45cbd3SKristen Carlson Accardi }
43579f45cbd3SKristen Carlson Accardi 
43589f45cbd3SKristen Carlson Accardi /**
4359c6fd2807SJeff Garzik  *	ata_dev_init_params - Issue INIT DEV PARAMS command
4360c6fd2807SJeff Garzik  *	@dev: Device to which command will be sent
4361c6fd2807SJeff Garzik  *	@heads: Number of heads (taskfile parameter)
4362c6fd2807SJeff Garzik  *	@sectors: Number of sectors (taskfile parameter)
4363c6fd2807SJeff Garzik  *
4364c6fd2807SJeff Garzik  *	LOCKING:
4365c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
4366c6fd2807SJeff Garzik  *
4367c6fd2807SJeff Garzik  *	RETURNS:
4368c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
4369c6fd2807SJeff Garzik  */
4370c6fd2807SJeff Garzik static unsigned int ata_dev_init_params(struct ata_device *dev,
4371c6fd2807SJeff Garzik 					u16 heads, u16 sectors)
4372c6fd2807SJeff Garzik {
4373c6fd2807SJeff Garzik 	struct ata_taskfile tf;
4374c6fd2807SJeff Garzik 	unsigned int err_mask;
4375c6fd2807SJeff Garzik 
4376c6fd2807SJeff Garzik 	/* Number of sectors per track 1-255. Number of heads 1-16 */
4377c6fd2807SJeff Garzik 	if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
4378c6fd2807SJeff Garzik 		return AC_ERR_INVALID;
4379c6fd2807SJeff Garzik 
4380c6fd2807SJeff Garzik 	/* set up init dev params taskfile */
4381c6fd2807SJeff Garzik 	DPRINTK("init dev params \n");
4382c6fd2807SJeff Garzik 
4383c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
4384c6fd2807SJeff Garzik 	tf.command = ATA_CMD_INIT_DEV_PARAMS;
4385c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4386c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
4387c6fd2807SJeff Garzik 	tf.nsect = sectors;
4388c6fd2807SJeff Garzik 	tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
4389c6fd2807SJeff Garzik 
43902b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
439118b2466cSAlan Cox 	/* A clean abort indicates an original or just out of spec drive
439218b2466cSAlan Cox 	   and we should continue as we issue the setup based on the
439318b2466cSAlan Cox 	   drive reported working geometry */
439418b2466cSAlan Cox 	if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
439518b2466cSAlan Cox 		err_mask = 0;
4396c6fd2807SJeff Garzik 
4397c6fd2807SJeff Garzik 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
4398c6fd2807SJeff Garzik 	return err_mask;
4399c6fd2807SJeff Garzik }
4400c6fd2807SJeff Garzik 
4401c6fd2807SJeff Garzik /**
4402c6fd2807SJeff Garzik  *	ata_sg_clean - Unmap DMA memory associated with command
4403c6fd2807SJeff Garzik  *	@qc: Command containing DMA memory to be released
4404c6fd2807SJeff Garzik  *
4405c6fd2807SJeff Garzik  *	Unmap all mapped DMA memory associated with this command.
4406c6fd2807SJeff Garzik  *
4407c6fd2807SJeff Garzik  *	LOCKING:
4408cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4409c6fd2807SJeff Garzik  */
441070e6ad0cSTejun Heo void ata_sg_clean(struct ata_queued_cmd *qc)
4411c6fd2807SJeff Garzik {
4412c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4413ff2aeb1eSTejun Heo 	struct scatterlist *sg = qc->sg;
4414c6fd2807SJeff Garzik 	int dir = qc->dma_dir;
4415c6fd2807SJeff Garzik 
4416c6fd2807SJeff Garzik 	WARN_ON(sg == NULL);
4417c6fd2807SJeff Garzik 
4418dde20207SJames Bottomley 	VPRINTK("unmapping %u sg elements\n", qc->n_elem);
4419c6fd2807SJeff Garzik 
4420dde20207SJames Bottomley 	if (qc->n_elem)
4421dde20207SJames Bottomley 		dma_unmap_sg(ap->dev, sg, qc->n_elem, dir);
4422c6fd2807SJeff Garzik 
4423c6fd2807SJeff Garzik 	qc->flags &= ~ATA_QCFLAG_DMAMAP;
4424ff2aeb1eSTejun Heo 	qc->sg = NULL;
4425c6fd2807SJeff Garzik }
4426c6fd2807SJeff Garzik 
4427c6fd2807SJeff Garzik /**
44285895ef9aSTejun Heo  *	atapi_check_dma - Check whether ATAPI DMA can be supported
4429c6fd2807SJeff Garzik  *	@qc: Metadata associated with taskfile to check
4430c6fd2807SJeff Garzik  *
4431c6fd2807SJeff Garzik  *	Allow low-level driver to filter ATA PACKET commands, returning
4432c6fd2807SJeff Garzik  *	a status indicating whether or not it is OK to use DMA for the
4433c6fd2807SJeff Garzik  *	supplied PACKET command.
4434c6fd2807SJeff Garzik  *
4435c6fd2807SJeff Garzik  *	LOCKING:
4436cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4437c6fd2807SJeff Garzik  *
4438c6fd2807SJeff Garzik  *	RETURNS: 0 when ATAPI DMA can be used
4439c6fd2807SJeff Garzik  *               nonzero otherwise
4440c6fd2807SJeff Garzik  */
44415895ef9aSTejun Heo int atapi_check_dma(struct ata_queued_cmd *qc)
4442c6fd2807SJeff Garzik {
4443c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4444c6fd2807SJeff Garzik 
4445b9a4197eSTejun Heo 	/* Don't allow DMA if it isn't multiple of 16 bytes.  Quite a
4446b9a4197eSTejun Heo 	 * few ATAPI devices choke on such DMA requests.
4447b9a4197eSTejun Heo 	 */
44486a87e42eSTejun Heo 	if (!(qc->dev->horkage & ATA_HORKAGE_ATAPI_MOD16_DMA) &&
44496a87e42eSTejun Heo 	    unlikely(qc->nbytes & 15))
44506f23a31dSAlbert Lee 		return 1;
44516f23a31dSAlbert Lee 
4452c6fd2807SJeff Garzik 	if (ap->ops->check_atapi_dma)
4453b9a4197eSTejun Heo 		return ap->ops->check_atapi_dma(qc);
4454c6fd2807SJeff Garzik 
4455b9a4197eSTejun Heo 	return 0;
4456c6fd2807SJeff Garzik }
4457b9a4197eSTejun Heo 
4458c6fd2807SJeff Garzik /**
445931cc23b3STejun Heo  *	ata_std_qc_defer - Check whether a qc needs to be deferred
446031cc23b3STejun Heo  *	@qc: ATA command in question
446131cc23b3STejun Heo  *
446231cc23b3STejun Heo  *	Non-NCQ commands cannot run with any other command, NCQ or
446331cc23b3STejun Heo  *	not.  As upper layer only knows the queue depth, we are
446431cc23b3STejun Heo  *	responsible for maintaining exclusion.  This function checks
446531cc23b3STejun Heo  *	whether a new command @qc can be issued.
446631cc23b3STejun Heo  *
446731cc23b3STejun Heo  *	LOCKING:
446831cc23b3STejun Heo  *	spin_lock_irqsave(host lock)
446931cc23b3STejun Heo  *
447031cc23b3STejun Heo  *	RETURNS:
447131cc23b3STejun Heo  *	ATA_DEFER_* if deferring is needed, 0 otherwise.
447231cc23b3STejun Heo  */
447331cc23b3STejun Heo int ata_std_qc_defer(struct ata_queued_cmd *qc)
447431cc23b3STejun Heo {
447531cc23b3STejun Heo 	struct ata_link *link = qc->dev->link;
447631cc23b3STejun Heo 
447731cc23b3STejun Heo 	if (qc->tf.protocol == ATA_PROT_NCQ) {
447831cc23b3STejun Heo 		if (!ata_tag_valid(link->active_tag))
447931cc23b3STejun Heo 			return 0;
448031cc23b3STejun Heo 	} else {
448131cc23b3STejun Heo 		if (!ata_tag_valid(link->active_tag) && !link->sactive)
448231cc23b3STejun Heo 			return 0;
448331cc23b3STejun Heo 	}
448431cc23b3STejun Heo 
448531cc23b3STejun Heo 	return ATA_DEFER_LINK;
448631cc23b3STejun Heo }
448731cc23b3STejun Heo 
4488c6fd2807SJeff Garzik void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
4489c6fd2807SJeff Garzik 
4490c6fd2807SJeff Garzik /**
4491c6fd2807SJeff Garzik  *	ata_sg_init - Associate command with scatter-gather table.
4492c6fd2807SJeff Garzik  *	@qc: Command to be associated
4493c6fd2807SJeff Garzik  *	@sg: Scatter-gather table.
4494c6fd2807SJeff Garzik  *	@n_elem: Number of elements in s/g table.
4495c6fd2807SJeff Garzik  *
4496c6fd2807SJeff Garzik  *	Initialize the data-related elements of queued_cmd @qc
4497c6fd2807SJeff Garzik  *	to point to a scatter-gather table @sg, containing @n_elem
4498c6fd2807SJeff Garzik  *	elements.
4499c6fd2807SJeff Garzik  *
4500c6fd2807SJeff Garzik  *	LOCKING:
4501cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4502c6fd2807SJeff Garzik  */
4503c6fd2807SJeff Garzik void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
4504c6fd2807SJeff Garzik 		 unsigned int n_elem)
4505c6fd2807SJeff Garzik {
4506ff2aeb1eSTejun Heo 	qc->sg = sg;
4507c6fd2807SJeff Garzik 	qc->n_elem = n_elem;
4508ff2aeb1eSTejun Heo 	qc->cursg = qc->sg;
4509ff2aeb1eSTejun Heo }
4510ff2aeb1eSTejun Heo 
4511c6fd2807SJeff Garzik /**
4512c6fd2807SJeff Garzik  *	ata_sg_setup - DMA-map the scatter-gather table associated with a command.
4513c6fd2807SJeff Garzik  *	@qc: Command with scatter-gather table to be mapped.
4514c6fd2807SJeff Garzik  *
4515c6fd2807SJeff Garzik  *	DMA-map the scatter-gather table associated with queued_cmd @qc.
4516c6fd2807SJeff Garzik  *
4517c6fd2807SJeff Garzik  *	LOCKING:
4518cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4519c6fd2807SJeff Garzik  *
4520c6fd2807SJeff Garzik  *	RETURNS:
4521c6fd2807SJeff Garzik  *	Zero on success, negative on error.
4522c6fd2807SJeff Garzik  *
4523c6fd2807SJeff Garzik  */
4524c6fd2807SJeff Garzik static int ata_sg_setup(struct ata_queued_cmd *qc)
4525c6fd2807SJeff Garzik {
4526c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4527dde20207SJames Bottomley 	unsigned int n_elem;
4528c6fd2807SJeff Garzik 
452944877b4eSTejun Heo 	VPRINTK("ENTER, ata%u\n", ap->print_id);
4530c6fd2807SJeff Garzik 
4531dde20207SJames Bottomley 	n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir);
4532dde20207SJames Bottomley 	if (n_elem < 1)
4533c6fd2807SJeff Garzik 		return -1;
4534c6fd2807SJeff Garzik 
4535dde20207SJames Bottomley 	DPRINTK("%d sg elements mapped\n", n_elem);
4536dde20207SJames Bottomley 
4537dde20207SJames Bottomley 	qc->n_elem = n_elem;
4538f92a2636STejun Heo 	qc->flags |= ATA_QCFLAG_DMAMAP;
4539c6fd2807SJeff Garzik 
4540c6fd2807SJeff Garzik 	return 0;
4541c6fd2807SJeff Garzik }
4542c6fd2807SJeff Garzik 
4543c6fd2807SJeff Garzik /**
4544c6fd2807SJeff Garzik  *	swap_buf_le16 - swap halves of 16-bit words in place
4545c6fd2807SJeff Garzik  *	@buf:  Buffer to swap
4546c6fd2807SJeff Garzik  *	@buf_words:  Number of 16-bit words in buffer.
4547c6fd2807SJeff Garzik  *
4548c6fd2807SJeff Garzik  *	Swap halves of 16-bit words if needed to convert from
4549c6fd2807SJeff Garzik  *	little-endian byte order to native cpu byte order, or
4550c6fd2807SJeff Garzik  *	vice-versa.
4551c6fd2807SJeff Garzik  *
4552c6fd2807SJeff Garzik  *	LOCKING:
4553c6fd2807SJeff Garzik  *	Inherited from caller.
4554c6fd2807SJeff Garzik  */
4555c6fd2807SJeff Garzik void swap_buf_le16(u16 *buf, unsigned int buf_words)
4556c6fd2807SJeff Garzik {
4557c6fd2807SJeff Garzik #ifdef __BIG_ENDIAN
4558c6fd2807SJeff Garzik 	unsigned int i;
4559c6fd2807SJeff Garzik 
4560c6fd2807SJeff Garzik 	for (i = 0; i < buf_words; i++)
4561c6fd2807SJeff Garzik 		buf[i] = le16_to_cpu(buf[i]);
4562c6fd2807SJeff Garzik #endif /* __BIG_ENDIAN */
4563c6fd2807SJeff Garzik }
4564c6fd2807SJeff Garzik 
4565c6fd2807SJeff Garzik /**
4566c6fd2807SJeff Garzik  *	ata_qc_new_init - Request an available ATA command, and initialize it
4567c6fd2807SJeff Garzik  *	@dev: Device from whom we request an available command structure
45683cd8ddbdSRandy Dunlap  *	@tag: command tag
4569c6fd2807SJeff Garzik  *
4570c6fd2807SJeff Garzik  *	LOCKING:
4571c6fd2807SJeff Garzik  *	None.
4572c6fd2807SJeff Garzik  */
4573c6fd2807SJeff Garzik 
45742fca5ccfSJens Axboe struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev, int tag)
4575c6fd2807SJeff Garzik {
45769af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
4577c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
4578c6fd2807SJeff Garzik 
45792fca5ccfSJens Axboe 	if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
45802fca5ccfSJens Axboe 		return NULL;
45812fca5ccfSJens Axboe 
45822fca5ccfSJens Axboe 	qc = __ata_qc_from_tag(ap, tag);
4583c6fd2807SJeff Garzik 	if (qc) {
4584c6fd2807SJeff Garzik 		qc->scsicmd = NULL;
4585c6fd2807SJeff Garzik 		qc->ap = ap;
4586c6fd2807SJeff Garzik 		qc->dev = dev;
45872fca5ccfSJens Axboe 		qc->tag = tag;
4588c6fd2807SJeff Garzik 
4589c6fd2807SJeff Garzik 		ata_qc_reinit(qc);
4590c6fd2807SJeff Garzik 	}
4591c6fd2807SJeff Garzik 
4592c6fd2807SJeff Garzik 	return qc;
4593c6fd2807SJeff Garzik }
4594c6fd2807SJeff Garzik 
4595c6fd2807SJeff Garzik void __ata_qc_complete(struct ata_queued_cmd *qc)
4596c6fd2807SJeff Garzik {
4597c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
45989af5c9c9STejun Heo 	struct ata_link *link = qc->dev->link;
4599c6fd2807SJeff Garzik 
4600c6fd2807SJeff Garzik 	WARN_ON(qc == NULL);	/* ata_qc_from_tag _might_ return NULL */
4601c6fd2807SJeff Garzik 	WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
4602c6fd2807SJeff Garzik 
4603c6fd2807SJeff Garzik 	if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
4604c6fd2807SJeff Garzik 		ata_sg_clean(qc);
4605c6fd2807SJeff Garzik 
4606c6fd2807SJeff Garzik 	/* command should be marked inactive atomically with qc completion */
4607da917d69STejun Heo 	if (qc->tf.protocol == ATA_PROT_NCQ) {
46089af5c9c9STejun Heo 		link->sactive &= ~(1 << qc->tag);
4609da917d69STejun Heo 		if (!link->sactive)
4610da917d69STejun Heo 			ap->nr_active_links--;
4611da917d69STejun Heo 	} else {
46129af5c9c9STejun Heo 		link->active_tag = ATA_TAG_POISON;
4613da917d69STejun Heo 		ap->nr_active_links--;
4614da917d69STejun Heo 	}
4615da917d69STejun Heo 
4616da917d69STejun Heo 	/* clear exclusive status */
4617da917d69STejun Heo 	if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
4618da917d69STejun Heo 		     ap->excl_link == link))
4619da917d69STejun Heo 		ap->excl_link = NULL;
4620c6fd2807SJeff Garzik 
4621c6fd2807SJeff Garzik 	/* atapi: mark qc as inactive to prevent the interrupt handler
4622c6fd2807SJeff Garzik 	 * from completing the command twice later, before the error handler
4623c6fd2807SJeff Garzik 	 * is called. (when rc != 0 and atapi request sense is needed)
4624c6fd2807SJeff Garzik 	 */
4625c6fd2807SJeff Garzik 	qc->flags &= ~ATA_QCFLAG_ACTIVE;
4626c6fd2807SJeff Garzik 	ap->qc_active &= ~(1 << qc->tag);
4627c6fd2807SJeff Garzik 
4628c6fd2807SJeff Garzik 	/* call completion callback */
4629c6fd2807SJeff Garzik 	qc->complete_fn(qc);
4630c6fd2807SJeff Garzik }
4631c6fd2807SJeff Garzik 
463239599a53STejun Heo static void fill_result_tf(struct ata_queued_cmd *qc)
463339599a53STejun Heo {
463439599a53STejun Heo 	struct ata_port *ap = qc->ap;
463539599a53STejun Heo 
463639599a53STejun Heo 	qc->result_tf.flags = qc->tf.flags;
463722183bf5STejun Heo 	ap->ops->qc_fill_rtf(qc);
463839599a53STejun Heo }
463939599a53STejun Heo 
464000115e0fSTejun Heo static void ata_verify_xfer(struct ata_queued_cmd *qc)
464100115e0fSTejun Heo {
464200115e0fSTejun Heo 	struct ata_device *dev = qc->dev;
464300115e0fSTejun Heo 
464400115e0fSTejun Heo 	if (ata_tag_internal(qc->tag))
464500115e0fSTejun Heo 		return;
464600115e0fSTejun Heo 
464700115e0fSTejun Heo 	if (ata_is_nodata(qc->tf.protocol))
464800115e0fSTejun Heo 		return;
464900115e0fSTejun Heo 
465000115e0fSTejun Heo 	if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
465100115e0fSTejun Heo 		return;
465200115e0fSTejun Heo 
465300115e0fSTejun Heo 	dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
465400115e0fSTejun Heo }
465500115e0fSTejun Heo 
4656c6fd2807SJeff Garzik /**
4657c6fd2807SJeff Garzik  *	ata_qc_complete - Complete an active ATA command
4658c6fd2807SJeff Garzik  *	@qc: Command to complete
4659c6fd2807SJeff Garzik  *
4660c6fd2807SJeff Garzik  *	Indicate to the mid and upper layers that an ATA
4661c6fd2807SJeff Garzik  *	command has completed, with either an ok or not-ok status.
4662c6fd2807SJeff Garzik  *
4663c6fd2807SJeff Garzik  *	LOCKING:
4664cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4665c6fd2807SJeff Garzik  */
4666c6fd2807SJeff Garzik void ata_qc_complete(struct ata_queued_cmd *qc)
4667c6fd2807SJeff Garzik {
4668c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4669c6fd2807SJeff Garzik 
4670c6fd2807SJeff Garzik 	/* XXX: New EH and old EH use different mechanisms to
4671c6fd2807SJeff Garzik 	 * synchronize EH with regular execution path.
4672c6fd2807SJeff Garzik 	 *
4673c6fd2807SJeff Garzik 	 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
4674c6fd2807SJeff Garzik 	 * Normal execution path is responsible for not accessing a
4675c6fd2807SJeff Garzik 	 * failed qc.  libata core enforces the rule by returning NULL
4676c6fd2807SJeff Garzik 	 * from ata_qc_from_tag() for failed qcs.
4677c6fd2807SJeff Garzik 	 *
4678c6fd2807SJeff Garzik 	 * Old EH depends on ata_qc_complete() nullifying completion
4679c6fd2807SJeff Garzik 	 * requests if ATA_QCFLAG_EH_SCHEDULED is set.  Old EH does
4680c6fd2807SJeff Garzik 	 * not synchronize with interrupt handler.  Only PIO task is
4681c6fd2807SJeff Garzik 	 * taken care of.
4682c6fd2807SJeff Garzik 	 */
4683c6fd2807SJeff Garzik 	if (ap->ops->error_handler) {
46844dbfa39bSTejun Heo 		struct ata_device *dev = qc->dev;
46854dbfa39bSTejun Heo 		struct ata_eh_info *ehi = &dev->link->eh_info;
46864dbfa39bSTejun Heo 
4687c6fd2807SJeff Garzik 		WARN_ON(ap->pflags & ATA_PFLAG_FROZEN);
4688c6fd2807SJeff Garzik 
4689c6fd2807SJeff Garzik 		if (unlikely(qc->err_mask))
4690c6fd2807SJeff Garzik 			qc->flags |= ATA_QCFLAG_FAILED;
4691c6fd2807SJeff Garzik 
4692c6fd2807SJeff Garzik 		if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
4693c6fd2807SJeff Garzik 			if (!ata_tag_internal(qc->tag)) {
4694c6fd2807SJeff Garzik 				/* always fill result TF for failed qc */
469539599a53STejun Heo 				fill_result_tf(qc);
4696c6fd2807SJeff Garzik 				ata_qc_schedule_eh(qc);
4697c6fd2807SJeff Garzik 				return;
4698c6fd2807SJeff Garzik 			}
4699c6fd2807SJeff Garzik 		}
4700c6fd2807SJeff Garzik 
4701c6fd2807SJeff Garzik 		/* read result TF if requested */
4702c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_RESULT_TF)
470339599a53STejun Heo 			fill_result_tf(qc);
4704c6fd2807SJeff Garzik 
47054dbfa39bSTejun Heo 		/* Some commands need post-processing after successful
47064dbfa39bSTejun Heo 		 * completion.
47074dbfa39bSTejun Heo 		 */
47084dbfa39bSTejun Heo 		switch (qc->tf.command) {
47094dbfa39bSTejun Heo 		case ATA_CMD_SET_FEATURES:
47104dbfa39bSTejun Heo 			if (qc->tf.feature != SETFEATURES_WC_ON &&
47114dbfa39bSTejun Heo 			    qc->tf.feature != SETFEATURES_WC_OFF)
47124dbfa39bSTejun Heo 				break;
47134dbfa39bSTejun Heo 			/* fall through */
47144dbfa39bSTejun Heo 		case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
47154dbfa39bSTejun Heo 		case ATA_CMD_SET_MULTI: /* multi_count changed */
47164dbfa39bSTejun Heo 			/* revalidate device */
47174dbfa39bSTejun Heo 			ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE;
47184dbfa39bSTejun Heo 			ata_port_schedule_eh(ap);
47194dbfa39bSTejun Heo 			break;
4720054a5fbaSTejun Heo 
4721054a5fbaSTejun Heo 		case ATA_CMD_SLEEP:
4722054a5fbaSTejun Heo 			dev->flags |= ATA_DFLAG_SLEEPING;
4723054a5fbaSTejun Heo 			break;
47244dbfa39bSTejun Heo 		}
47254dbfa39bSTejun Heo 
472600115e0fSTejun Heo 		if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
472700115e0fSTejun Heo 			ata_verify_xfer(qc);
472800115e0fSTejun Heo 
4729c6fd2807SJeff Garzik 		__ata_qc_complete(qc);
4730c6fd2807SJeff Garzik 	} else {
4731c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
4732c6fd2807SJeff Garzik 			return;
4733c6fd2807SJeff Garzik 
4734c6fd2807SJeff Garzik 		/* read result TF if failed or requested */
4735c6fd2807SJeff Garzik 		if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
473639599a53STejun Heo 			fill_result_tf(qc);
4737c6fd2807SJeff Garzik 
4738c6fd2807SJeff Garzik 		__ata_qc_complete(qc);
4739c6fd2807SJeff Garzik 	}
4740c6fd2807SJeff Garzik }
4741c6fd2807SJeff Garzik 
4742c6fd2807SJeff Garzik /**
4743c6fd2807SJeff Garzik  *	ata_qc_complete_multiple - Complete multiple qcs successfully
4744c6fd2807SJeff Garzik  *	@ap: port in question
4745c6fd2807SJeff Garzik  *	@qc_active: new qc_active mask
4746c6fd2807SJeff Garzik  *
4747c6fd2807SJeff Garzik  *	Complete in-flight commands.  This functions is meant to be
4748c6fd2807SJeff Garzik  *	called from low-level driver's interrupt routine to complete
4749c6fd2807SJeff Garzik  *	requests normally.  ap->qc_active and @qc_active is compared
4750c6fd2807SJeff Garzik  *	and commands are completed accordingly.
4751c6fd2807SJeff Garzik  *
4752c6fd2807SJeff Garzik  *	LOCKING:
4753cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4754c6fd2807SJeff Garzik  *
4755c6fd2807SJeff Garzik  *	RETURNS:
4756c6fd2807SJeff Garzik  *	Number of completed commands on success, -errno otherwise.
4757c6fd2807SJeff Garzik  */
475879f97dadSTejun Heo int ata_qc_complete_multiple(struct ata_port *ap, u32 qc_active)
4759c6fd2807SJeff Garzik {
4760c6fd2807SJeff Garzik 	int nr_done = 0;
4761c6fd2807SJeff Garzik 	u32 done_mask;
4762c6fd2807SJeff Garzik 	int i;
4763c6fd2807SJeff Garzik 
4764c6fd2807SJeff Garzik 	done_mask = ap->qc_active ^ qc_active;
4765c6fd2807SJeff Garzik 
4766c6fd2807SJeff Garzik 	if (unlikely(done_mask & qc_active)) {
4767c6fd2807SJeff Garzik 		ata_port_printk(ap, KERN_ERR, "illegal qc_active transition "
4768c6fd2807SJeff Garzik 				"(%08x->%08x)\n", ap->qc_active, qc_active);
4769c6fd2807SJeff Garzik 		return -EINVAL;
4770c6fd2807SJeff Garzik 	}
4771c6fd2807SJeff Garzik 
4772c6fd2807SJeff Garzik 	for (i = 0; i < ATA_MAX_QUEUE; i++) {
4773c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc;
4774c6fd2807SJeff Garzik 
4775c6fd2807SJeff Garzik 		if (!(done_mask & (1 << i)))
4776c6fd2807SJeff Garzik 			continue;
4777c6fd2807SJeff Garzik 
4778c6fd2807SJeff Garzik 		if ((qc = ata_qc_from_tag(ap, i))) {
4779c6fd2807SJeff Garzik 			ata_qc_complete(qc);
4780c6fd2807SJeff Garzik 			nr_done++;
4781c6fd2807SJeff Garzik 		}
4782c6fd2807SJeff Garzik 	}
4783c6fd2807SJeff Garzik 
4784c6fd2807SJeff Garzik 	return nr_done;
4785c6fd2807SJeff Garzik }
4786c6fd2807SJeff Garzik 
4787c6fd2807SJeff Garzik /**
4788c6fd2807SJeff Garzik  *	ata_qc_issue - issue taskfile to device
4789c6fd2807SJeff Garzik  *	@qc: command to issue to device
4790c6fd2807SJeff Garzik  *
4791c6fd2807SJeff Garzik  *	Prepare an ATA command to submission to device.
4792c6fd2807SJeff Garzik  *	This includes mapping the data into a DMA-able
4793c6fd2807SJeff Garzik  *	area, filling in the S/G table, and finally
4794c6fd2807SJeff Garzik  *	writing the taskfile to hardware, starting the command.
4795c6fd2807SJeff Garzik  *
4796c6fd2807SJeff Garzik  *	LOCKING:
4797cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4798c6fd2807SJeff Garzik  */
4799c6fd2807SJeff Garzik void ata_qc_issue(struct ata_queued_cmd *qc)
4800c6fd2807SJeff Garzik {
4801c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
48029af5c9c9STejun Heo 	struct ata_link *link = qc->dev->link;
4803405e66b3STejun Heo 	u8 prot = qc->tf.protocol;
4804c6fd2807SJeff Garzik 
4805c6fd2807SJeff Garzik 	/* Make sure only one non-NCQ command is outstanding.  The
4806c6fd2807SJeff Garzik 	 * check is skipped for old EH because it reuses active qc to
4807c6fd2807SJeff Garzik 	 * request ATAPI sense.
4808c6fd2807SJeff Garzik 	 */
48099af5c9c9STejun Heo 	WARN_ON(ap->ops->error_handler && ata_tag_valid(link->active_tag));
4810c6fd2807SJeff Garzik 
48111973a023STejun Heo 	if (ata_is_ncq(prot)) {
48129af5c9c9STejun Heo 		WARN_ON(link->sactive & (1 << qc->tag));
4813da917d69STejun Heo 
4814da917d69STejun Heo 		if (!link->sactive)
4815da917d69STejun Heo 			ap->nr_active_links++;
48169af5c9c9STejun Heo 		link->sactive |= 1 << qc->tag;
4817c6fd2807SJeff Garzik 	} else {
48189af5c9c9STejun Heo 		WARN_ON(link->sactive);
4819da917d69STejun Heo 
4820da917d69STejun Heo 		ap->nr_active_links++;
48219af5c9c9STejun Heo 		link->active_tag = qc->tag;
4822c6fd2807SJeff Garzik 	}
4823c6fd2807SJeff Garzik 
4824c6fd2807SJeff Garzik 	qc->flags |= ATA_QCFLAG_ACTIVE;
4825c6fd2807SJeff Garzik 	ap->qc_active |= 1 << qc->tag;
4826c6fd2807SJeff Garzik 
4827f92a2636STejun Heo 	/* We guarantee to LLDs that they will have at least one
4828f92a2636STejun Heo 	 * non-zero sg if the command is a data command.
4829f92a2636STejun Heo 	 */
4830ff2aeb1eSTejun Heo 	BUG_ON(ata_is_data(prot) && (!qc->sg || !qc->n_elem || !qc->nbytes));
4831f92a2636STejun Heo 
4832405e66b3STejun Heo 	if (ata_is_dma(prot) || (ata_is_pio(prot) &&
4833f92a2636STejun Heo 				 (ap->flags & ATA_FLAG_PIO_DMA)))
4834c6fd2807SJeff Garzik 		if (ata_sg_setup(qc))
4835c6fd2807SJeff Garzik 			goto sg_err;
4836c6fd2807SJeff Garzik 
4837cf480626STejun Heo 	/* if device is sleeping, schedule reset and abort the link */
4838054a5fbaSTejun Heo 	if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
4839cf480626STejun Heo 		link->eh_info.action |= ATA_EH_RESET;
4840054a5fbaSTejun Heo 		ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
4841054a5fbaSTejun Heo 		ata_link_abort(link);
4842054a5fbaSTejun Heo 		return;
4843054a5fbaSTejun Heo 	}
4844054a5fbaSTejun Heo 
4845c6fd2807SJeff Garzik 	ap->ops->qc_prep(qc);
4846c6fd2807SJeff Garzik 
4847c6fd2807SJeff Garzik 	qc->err_mask |= ap->ops->qc_issue(qc);
4848c6fd2807SJeff Garzik 	if (unlikely(qc->err_mask))
4849c6fd2807SJeff Garzik 		goto err;
4850c6fd2807SJeff Garzik 	return;
4851c6fd2807SJeff Garzik 
4852c6fd2807SJeff Garzik sg_err:
4853c6fd2807SJeff Garzik 	qc->err_mask |= AC_ERR_SYSTEM;
4854c6fd2807SJeff Garzik err:
4855c6fd2807SJeff Garzik 	ata_qc_complete(qc);
4856c6fd2807SJeff Garzik }
4857c6fd2807SJeff Garzik 
4858c6fd2807SJeff Garzik /**
4859c6fd2807SJeff Garzik  *	sata_scr_valid - test whether SCRs are accessible
4860936fd732STejun Heo  *	@link: ATA link to test SCR accessibility for
4861c6fd2807SJeff Garzik  *
4862936fd732STejun Heo  *	Test whether SCRs are accessible for @link.
4863c6fd2807SJeff Garzik  *
4864c6fd2807SJeff Garzik  *	LOCKING:
4865c6fd2807SJeff Garzik  *	None.
4866c6fd2807SJeff Garzik  *
4867c6fd2807SJeff Garzik  *	RETURNS:
4868c6fd2807SJeff Garzik  *	1 if SCRs are accessible, 0 otherwise.
4869c6fd2807SJeff Garzik  */
4870936fd732STejun Heo int sata_scr_valid(struct ata_link *link)
4871c6fd2807SJeff Garzik {
4872936fd732STejun Heo 	struct ata_port *ap = link->ap;
4873936fd732STejun Heo 
4874a16abc0bSTejun Heo 	return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
4875c6fd2807SJeff Garzik }
4876c6fd2807SJeff Garzik 
4877c6fd2807SJeff Garzik /**
4878c6fd2807SJeff Garzik  *	sata_scr_read - read SCR register of the specified port
4879936fd732STejun Heo  *	@link: ATA link to read SCR for
4880c6fd2807SJeff Garzik  *	@reg: SCR to read
4881c6fd2807SJeff Garzik  *	@val: Place to store read value
4882c6fd2807SJeff Garzik  *
4883936fd732STejun Heo  *	Read SCR register @reg of @link into *@val.  This function is
4884633273a3STejun Heo  *	guaranteed to succeed if @link is ap->link, the cable type of
4885633273a3STejun Heo  *	the port is SATA and the port implements ->scr_read.
4886c6fd2807SJeff Garzik  *
4887c6fd2807SJeff Garzik  *	LOCKING:
4888633273a3STejun Heo  *	None if @link is ap->link.  Kernel thread context otherwise.
4889c6fd2807SJeff Garzik  *
4890c6fd2807SJeff Garzik  *	RETURNS:
4891c6fd2807SJeff Garzik  *	0 on success, negative errno on failure.
4892c6fd2807SJeff Garzik  */
4893936fd732STejun Heo int sata_scr_read(struct ata_link *link, int reg, u32 *val)
4894c6fd2807SJeff Garzik {
4895633273a3STejun Heo 	if (ata_is_host_link(link)) {
4896936fd732STejun Heo 		if (sata_scr_valid(link))
489782ef04fbSTejun Heo 			return link->ap->ops->scr_read(link, reg, val);
4898c6fd2807SJeff Garzik 		return -EOPNOTSUPP;
4899c6fd2807SJeff Garzik 	}
4900c6fd2807SJeff Garzik 
4901633273a3STejun Heo 	return sata_pmp_scr_read(link, reg, val);
4902633273a3STejun Heo }
4903633273a3STejun Heo 
4904c6fd2807SJeff Garzik /**
4905c6fd2807SJeff Garzik  *	sata_scr_write - write SCR register of the specified port
4906936fd732STejun Heo  *	@link: ATA link to write SCR for
4907c6fd2807SJeff Garzik  *	@reg: SCR to write
4908c6fd2807SJeff Garzik  *	@val: value to write
4909c6fd2807SJeff Garzik  *
4910936fd732STejun Heo  *	Write @val to SCR register @reg of @link.  This function is
4911633273a3STejun Heo  *	guaranteed to succeed if @link is ap->link, the cable type of
4912633273a3STejun Heo  *	the port is SATA and the port implements ->scr_read.
4913c6fd2807SJeff Garzik  *
4914c6fd2807SJeff Garzik  *	LOCKING:
4915633273a3STejun Heo  *	None if @link is ap->link.  Kernel thread context otherwise.
4916c6fd2807SJeff Garzik  *
4917c6fd2807SJeff Garzik  *	RETURNS:
4918c6fd2807SJeff Garzik  *	0 on success, negative errno on failure.
4919c6fd2807SJeff Garzik  */
4920936fd732STejun Heo int sata_scr_write(struct ata_link *link, int reg, u32 val)
4921c6fd2807SJeff Garzik {
4922633273a3STejun Heo 	if (ata_is_host_link(link)) {
4923936fd732STejun Heo 		if (sata_scr_valid(link))
492482ef04fbSTejun Heo 			return link->ap->ops->scr_write(link, reg, val);
4925c6fd2807SJeff Garzik 		return -EOPNOTSUPP;
4926c6fd2807SJeff Garzik 	}
4927c6fd2807SJeff Garzik 
4928633273a3STejun Heo 	return sata_pmp_scr_write(link, reg, val);
4929633273a3STejun Heo }
4930633273a3STejun Heo 
4931c6fd2807SJeff Garzik /**
4932c6fd2807SJeff Garzik  *	sata_scr_write_flush - write SCR register of the specified port and flush
4933936fd732STejun Heo  *	@link: ATA link to write SCR for
4934c6fd2807SJeff Garzik  *	@reg: SCR to write
4935c6fd2807SJeff Garzik  *	@val: value to write
4936c6fd2807SJeff Garzik  *
4937c6fd2807SJeff Garzik  *	This function is identical to sata_scr_write() except that this
4938c6fd2807SJeff Garzik  *	function performs flush after writing to the register.
4939c6fd2807SJeff Garzik  *
4940c6fd2807SJeff Garzik  *	LOCKING:
4941633273a3STejun Heo  *	None if @link is ap->link.  Kernel thread context otherwise.
4942c6fd2807SJeff Garzik  *
4943c6fd2807SJeff Garzik  *	RETURNS:
4944c6fd2807SJeff Garzik  *	0 on success, negative errno on failure.
4945c6fd2807SJeff Garzik  */
4946936fd732STejun Heo int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
4947c6fd2807SJeff Garzik {
4948633273a3STejun Heo 	if (ata_is_host_link(link)) {
4949da3dbb17STejun Heo 		int rc;
4950da3dbb17STejun Heo 
4951936fd732STejun Heo 		if (sata_scr_valid(link)) {
495282ef04fbSTejun Heo 			rc = link->ap->ops->scr_write(link, reg, val);
4953da3dbb17STejun Heo 			if (rc == 0)
495482ef04fbSTejun Heo 				rc = link->ap->ops->scr_read(link, reg, &val);
4955da3dbb17STejun Heo 			return rc;
4956c6fd2807SJeff Garzik 		}
4957c6fd2807SJeff Garzik 		return -EOPNOTSUPP;
4958c6fd2807SJeff Garzik 	}
4959c6fd2807SJeff Garzik 
4960633273a3STejun Heo 	return sata_pmp_scr_write(link, reg, val);
4961633273a3STejun Heo }
4962633273a3STejun Heo 
4963c6fd2807SJeff Garzik /**
4964b1c72916STejun Heo  *	ata_phys_link_online - test whether the given link is online
4965936fd732STejun Heo  *	@link: ATA link to test
4966c6fd2807SJeff Garzik  *
4967936fd732STejun Heo  *	Test whether @link is online.  Note that this function returns
4968936fd732STejun Heo  *	0 if online status of @link cannot be obtained, so
4969936fd732STejun Heo  *	ata_link_online(link) != !ata_link_offline(link).
4970c6fd2807SJeff Garzik  *
4971c6fd2807SJeff Garzik  *	LOCKING:
4972c6fd2807SJeff Garzik  *	None.
4973c6fd2807SJeff Garzik  *
4974c6fd2807SJeff Garzik  *	RETURNS:
4975b5b3fa38STejun Heo  *	True if the port online status is available and online.
4976c6fd2807SJeff Garzik  */
4977b1c72916STejun Heo bool ata_phys_link_online(struct ata_link *link)
4978c6fd2807SJeff Garzik {
4979c6fd2807SJeff Garzik 	u32 sstatus;
4980c6fd2807SJeff Garzik 
4981936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
4982936fd732STejun Heo 	    (sstatus & 0xf) == 0x3)
4983b5b3fa38STejun Heo 		return true;
4984b5b3fa38STejun Heo 	return false;
4985c6fd2807SJeff Garzik }
4986c6fd2807SJeff Garzik 
4987c6fd2807SJeff Garzik /**
4988b1c72916STejun Heo  *	ata_phys_link_offline - test whether the given link is offline
4989936fd732STejun Heo  *	@link: ATA link to test
4990c6fd2807SJeff Garzik  *
4991936fd732STejun Heo  *	Test whether @link is offline.  Note that this function
4992936fd732STejun Heo  *	returns 0 if offline status of @link cannot be obtained, so
4993936fd732STejun Heo  *	ata_link_online(link) != !ata_link_offline(link).
4994c6fd2807SJeff Garzik  *
4995c6fd2807SJeff Garzik  *	LOCKING:
4996c6fd2807SJeff Garzik  *	None.
4997c6fd2807SJeff Garzik  *
4998c6fd2807SJeff Garzik  *	RETURNS:
4999b5b3fa38STejun Heo  *	True if the port offline status is available and offline.
5000c6fd2807SJeff Garzik  */
5001b1c72916STejun Heo bool ata_phys_link_offline(struct ata_link *link)
5002c6fd2807SJeff Garzik {
5003c6fd2807SJeff Garzik 	u32 sstatus;
5004c6fd2807SJeff Garzik 
5005936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
5006936fd732STejun Heo 	    (sstatus & 0xf) != 0x3)
5007b5b3fa38STejun Heo 		return true;
5008b5b3fa38STejun Heo 	return false;
5009c6fd2807SJeff Garzik }
5010c6fd2807SJeff Garzik 
5011b1c72916STejun Heo /**
5012b1c72916STejun Heo  *	ata_link_online - test whether the given link is online
5013b1c72916STejun Heo  *	@link: ATA link to test
5014b1c72916STejun Heo  *
5015b1c72916STejun Heo  *	Test whether @link is online.  This is identical to
5016b1c72916STejun Heo  *	ata_phys_link_online() when there's no slave link.  When
5017b1c72916STejun Heo  *	there's a slave link, this function should only be called on
5018b1c72916STejun Heo  *	the master link and will return true if any of M/S links is
5019b1c72916STejun Heo  *	online.
5020b1c72916STejun Heo  *
5021b1c72916STejun Heo  *	LOCKING:
5022b1c72916STejun Heo  *	None.
5023b1c72916STejun Heo  *
5024b1c72916STejun Heo  *	RETURNS:
5025b1c72916STejun Heo  *	True if the port online status is available and online.
5026b1c72916STejun Heo  */
5027b1c72916STejun Heo bool ata_link_online(struct ata_link *link)
5028b1c72916STejun Heo {
5029b1c72916STejun Heo 	struct ata_link *slave = link->ap->slave_link;
5030b1c72916STejun Heo 
5031b1c72916STejun Heo 	WARN_ON(link == slave);	/* shouldn't be called on slave link */
5032b1c72916STejun Heo 
5033b1c72916STejun Heo 	return ata_phys_link_online(link) ||
5034b1c72916STejun Heo 		(slave && ata_phys_link_online(slave));
5035b1c72916STejun Heo }
5036b1c72916STejun Heo 
5037b1c72916STejun Heo /**
5038b1c72916STejun Heo  *	ata_link_offline - test whether the given link is offline
5039b1c72916STejun Heo  *	@link: ATA link to test
5040b1c72916STejun Heo  *
5041b1c72916STejun Heo  *	Test whether @link is offline.  This is identical to
5042b1c72916STejun Heo  *	ata_phys_link_offline() when there's no slave link.  When
5043b1c72916STejun Heo  *	there's a slave link, this function should only be called on
5044b1c72916STejun Heo  *	the master link and will return true if both M/S links are
5045b1c72916STejun Heo  *	offline.
5046b1c72916STejun Heo  *
5047b1c72916STejun Heo  *	LOCKING:
5048b1c72916STejun Heo  *	None.
5049b1c72916STejun Heo  *
5050b1c72916STejun Heo  *	RETURNS:
5051b1c72916STejun Heo  *	True if the port offline status is available and offline.
5052b1c72916STejun Heo  */
5053b1c72916STejun Heo bool ata_link_offline(struct ata_link *link)
5054b1c72916STejun Heo {
5055b1c72916STejun Heo 	struct ata_link *slave = link->ap->slave_link;
5056b1c72916STejun Heo 
5057b1c72916STejun Heo 	WARN_ON(link == slave);	/* shouldn't be called on slave link */
5058b1c72916STejun Heo 
5059b1c72916STejun Heo 	return ata_phys_link_offline(link) &&
5060b1c72916STejun Heo 		(!slave || ata_phys_link_offline(slave));
5061b1c72916STejun Heo }
5062b1c72916STejun Heo 
50636ffa01d8STejun Heo #ifdef CONFIG_PM
5064cca3974eSJeff Garzik static int ata_host_request_pm(struct ata_host *host, pm_message_t mesg,
5065cca3974eSJeff Garzik 			       unsigned int action, unsigned int ehi_flags,
5066cca3974eSJeff Garzik 			       int wait)
5067c6fd2807SJeff Garzik {
5068c6fd2807SJeff Garzik 	unsigned long flags;
5069c6fd2807SJeff Garzik 	int i, rc;
5070c6fd2807SJeff Garzik 
5071cca3974eSJeff Garzik 	for (i = 0; i < host->n_ports; i++) {
5072cca3974eSJeff Garzik 		struct ata_port *ap = host->ports[i];
5073e3667ebfSTejun Heo 		struct ata_link *link;
5074c6fd2807SJeff Garzik 
5075c6fd2807SJeff Garzik 		/* Previous resume operation might still be in
5076c6fd2807SJeff Garzik 		 * progress.  Wait for PM_PENDING to clear.
5077c6fd2807SJeff Garzik 		 */
5078c6fd2807SJeff Garzik 		if (ap->pflags & ATA_PFLAG_PM_PENDING) {
5079c6fd2807SJeff Garzik 			ata_port_wait_eh(ap);
5080c6fd2807SJeff Garzik 			WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5081c6fd2807SJeff Garzik 		}
5082c6fd2807SJeff Garzik 
5083c6fd2807SJeff Garzik 		/* request PM ops to EH */
5084c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
5085c6fd2807SJeff Garzik 
5086c6fd2807SJeff Garzik 		ap->pm_mesg = mesg;
5087c6fd2807SJeff Garzik 		if (wait) {
5088c6fd2807SJeff Garzik 			rc = 0;
5089c6fd2807SJeff Garzik 			ap->pm_result = &rc;
5090c6fd2807SJeff Garzik 		}
5091c6fd2807SJeff Garzik 
5092c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_PM_PENDING;
5093e3667ebfSTejun Heo 		__ata_port_for_each_link(link, ap) {
5094e3667ebfSTejun Heo 			link->eh_info.action |= action;
5095e3667ebfSTejun Heo 			link->eh_info.flags |= ehi_flags;
5096e3667ebfSTejun Heo 		}
5097c6fd2807SJeff Garzik 
5098c6fd2807SJeff Garzik 		ata_port_schedule_eh(ap);
5099c6fd2807SJeff Garzik 
5100c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
5101c6fd2807SJeff Garzik 
5102c6fd2807SJeff Garzik 		/* wait and check result */
5103c6fd2807SJeff Garzik 		if (wait) {
5104c6fd2807SJeff Garzik 			ata_port_wait_eh(ap);
5105c6fd2807SJeff Garzik 			WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5106c6fd2807SJeff Garzik 			if (rc)
5107c6fd2807SJeff Garzik 				return rc;
5108c6fd2807SJeff Garzik 		}
5109c6fd2807SJeff Garzik 	}
5110c6fd2807SJeff Garzik 
5111c6fd2807SJeff Garzik 	return 0;
5112c6fd2807SJeff Garzik }
5113c6fd2807SJeff Garzik 
5114c6fd2807SJeff Garzik /**
5115cca3974eSJeff Garzik  *	ata_host_suspend - suspend host
5116cca3974eSJeff Garzik  *	@host: host to suspend
5117c6fd2807SJeff Garzik  *	@mesg: PM message
5118c6fd2807SJeff Garzik  *
5119cca3974eSJeff Garzik  *	Suspend @host.  Actual operation is performed by EH.  This
5120c6fd2807SJeff Garzik  *	function requests EH to perform PM operations and waits for EH
5121c6fd2807SJeff Garzik  *	to finish.
5122c6fd2807SJeff Garzik  *
5123c6fd2807SJeff Garzik  *	LOCKING:
5124c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
5125c6fd2807SJeff Garzik  *
5126c6fd2807SJeff Garzik  *	RETURNS:
5127c6fd2807SJeff Garzik  *	0 on success, -errno on failure.
5128c6fd2807SJeff Garzik  */
5129cca3974eSJeff Garzik int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
5130c6fd2807SJeff Garzik {
51319666f400STejun Heo 	int rc;
5132c6fd2807SJeff Garzik 
5133ca77329fSKristen Carlson Accardi 	/*
5134ca77329fSKristen Carlson Accardi 	 * disable link pm on all ports before requesting
5135ca77329fSKristen Carlson Accardi 	 * any pm activity
5136ca77329fSKristen Carlson Accardi 	 */
5137ca77329fSKristen Carlson Accardi 	ata_lpm_enable(host);
5138ca77329fSKristen Carlson Accardi 
5139cca3974eSJeff Garzik 	rc = ata_host_request_pm(host, mesg, 0, ATA_EHI_QUIET, 1);
514072ad6ec4SJeff Garzik 	if (rc == 0)
514172ad6ec4SJeff Garzik 		host->dev->power.power_state = mesg;
5142c6fd2807SJeff Garzik 	return rc;
5143c6fd2807SJeff Garzik }
5144c6fd2807SJeff Garzik 
5145c6fd2807SJeff Garzik /**
5146cca3974eSJeff Garzik  *	ata_host_resume - resume host
5147cca3974eSJeff Garzik  *	@host: host to resume
5148c6fd2807SJeff Garzik  *
5149cca3974eSJeff Garzik  *	Resume @host.  Actual operation is performed by EH.  This
5150c6fd2807SJeff Garzik  *	function requests EH to perform PM operations and returns.
5151c6fd2807SJeff Garzik  *	Note that all resume operations are performed parallely.
5152c6fd2807SJeff Garzik  *
5153c6fd2807SJeff Garzik  *	LOCKING:
5154c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
5155c6fd2807SJeff Garzik  */
5156cca3974eSJeff Garzik void ata_host_resume(struct ata_host *host)
5157c6fd2807SJeff Garzik {
5158cf480626STejun Heo 	ata_host_request_pm(host, PMSG_ON, ATA_EH_RESET,
5159c6fd2807SJeff Garzik 			    ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET, 0);
516072ad6ec4SJeff Garzik 	host->dev->power.power_state = PMSG_ON;
5161ca77329fSKristen Carlson Accardi 
5162ca77329fSKristen Carlson Accardi 	/* reenable link pm */
5163ca77329fSKristen Carlson Accardi 	ata_lpm_disable(host);
5164c6fd2807SJeff Garzik }
51656ffa01d8STejun Heo #endif
5166c6fd2807SJeff Garzik 
5167c6fd2807SJeff Garzik /**
5168c6fd2807SJeff Garzik  *	ata_port_start - Set port up for dma.
5169c6fd2807SJeff Garzik  *	@ap: Port to initialize
5170c6fd2807SJeff Garzik  *
5171c6fd2807SJeff Garzik  *	Called just after data structures for each port are
5172c6fd2807SJeff Garzik  *	initialized.  Allocates space for PRD table.
5173c6fd2807SJeff Garzik  *
5174c6fd2807SJeff Garzik  *	May be used as the port_start() entry in ata_port_operations.
5175c6fd2807SJeff Garzik  *
5176c6fd2807SJeff Garzik  *	LOCKING:
5177c6fd2807SJeff Garzik  *	Inherited from caller.
5178c6fd2807SJeff Garzik  */
5179c6fd2807SJeff Garzik int ata_port_start(struct ata_port *ap)
5180c6fd2807SJeff Garzik {
5181c6fd2807SJeff Garzik 	struct device *dev = ap->dev;
5182c6fd2807SJeff Garzik 
5183f0d36efdSTejun Heo 	ap->prd = dmam_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma,
5184f0d36efdSTejun Heo 				      GFP_KERNEL);
5185c6fd2807SJeff Garzik 	if (!ap->prd)
5186c6fd2807SJeff Garzik 		return -ENOMEM;
5187c6fd2807SJeff Garzik 
5188c6fd2807SJeff Garzik 	return 0;
5189c6fd2807SJeff Garzik }
5190c6fd2807SJeff Garzik 
5191c6fd2807SJeff Garzik /**
5192c6fd2807SJeff Garzik  *	ata_dev_init - Initialize an ata_device structure
5193c6fd2807SJeff Garzik  *	@dev: Device structure to initialize
5194c6fd2807SJeff Garzik  *
5195c6fd2807SJeff Garzik  *	Initialize @dev in preparation for probing.
5196c6fd2807SJeff Garzik  *
5197c6fd2807SJeff Garzik  *	LOCKING:
5198c6fd2807SJeff Garzik  *	Inherited from caller.
5199c6fd2807SJeff Garzik  */
5200c6fd2807SJeff Garzik void ata_dev_init(struct ata_device *dev)
5201c6fd2807SJeff Garzik {
5202b1c72916STejun Heo 	struct ata_link *link = ata_dev_phys_link(dev);
52039af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
5204c6fd2807SJeff Garzik 	unsigned long flags;
5205c6fd2807SJeff Garzik 
5206b1c72916STejun Heo 	/* SATA spd limit is bound to the attached device, reset together */
52079af5c9c9STejun Heo 	link->sata_spd_limit = link->hw_sata_spd_limit;
52089af5c9c9STejun Heo 	link->sata_spd = 0;
5209c6fd2807SJeff Garzik 
5210c6fd2807SJeff Garzik 	/* High bits of dev->flags are used to record warm plug
5211c6fd2807SJeff Garzik 	 * requests which occur asynchronously.  Synchronize using
5212cca3974eSJeff Garzik 	 * host lock.
5213c6fd2807SJeff Garzik 	 */
5214c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
5215c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_INIT_MASK;
52163dcc323fSTejun Heo 	dev->horkage = 0;
5217c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
5218c6fd2807SJeff Garzik 
5219c6fd2807SJeff Garzik 	memset((void *)dev + ATA_DEVICE_CLEAR_OFFSET, 0,
5220c6fd2807SJeff Garzik 	       sizeof(*dev) - ATA_DEVICE_CLEAR_OFFSET);
5221c6fd2807SJeff Garzik 	dev->pio_mask = UINT_MAX;
5222c6fd2807SJeff Garzik 	dev->mwdma_mask = UINT_MAX;
5223c6fd2807SJeff Garzik 	dev->udma_mask = UINT_MAX;
5224c6fd2807SJeff Garzik }
5225c6fd2807SJeff Garzik 
5226c6fd2807SJeff Garzik /**
52274fb37a25STejun Heo  *	ata_link_init - Initialize an ata_link structure
52284fb37a25STejun Heo  *	@ap: ATA port link is attached to
52294fb37a25STejun Heo  *	@link: Link structure to initialize
52308989805dSTejun Heo  *	@pmp: Port multiplier port number
52314fb37a25STejun Heo  *
52324fb37a25STejun Heo  *	Initialize @link.
52334fb37a25STejun Heo  *
52344fb37a25STejun Heo  *	LOCKING:
52354fb37a25STejun Heo  *	Kernel thread context (may sleep)
52364fb37a25STejun Heo  */
5237fb7fd614STejun Heo void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
52384fb37a25STejun Heo {
52394fb37a25STejun Heo 	int i;
52404fb37a25STejun Heo 
52414fb37a25STejun Heo 	/* clear everything except for devices */
52424fb37a25STejun Heo 	memset(link, 0, offsetof(struct ata_link, device[0]));
52434fb37a25STejun Heo 
52444fb37a25STejun Heo 	link->ap = ap;
52458989805dSTejun Heo 	link->pmp = pmp;
52464fb37a25STejun Heo 	link->active_tag = ATA_TAG_POISON;
52474fb37a25STejun Heo 	link->hw_sata_spd_limit = UINT_MAX;
52484fb37a25STejun Heo 
52494fb37a25STejun Heo 	/* can't use iterator, ap isn't initialized yet */
52504fb37a25STejun Heo 	for (i = 0; i < ATA_MAX_DEVICES; i++) {
52514fb37a25STejun Heo 		struct ata_device *dev = &link->device[i];
52524fb37a25STejun Heo 
52534fb37a25STejun Heo 		dev->link = link;
52544fb37a25STejun Heo 		dev->devno = dev - link->device;
52554fb37a25STejun Heo 		ata_dev_init(dev);
52564fb37a25STejun Heo 	}
52574fb37a25STejun Heo }
52584fb37a25STejun Heo 
52594fb37a25STejun Heo /**
52604fb37a25STejun Heo  *	sata_link_init_spd - Initialize link->sata_spd_limit
52614fb37a25STejun Heo  *	@link: Link to configure sata_spd_limit for
52624fb37a25STejun Heo  *
52634fb37a25STejun Heo  *	Initialize @link->[hw_]sata_spd_limit to the currently
52644fb37a25STejun Heo  *	configured value.
52654fb37a25STejun Heo  *
52664fb37a25STejun Heo  *	LOCKING:
52674fb37a25STejun Heo  *	Kernel thread context (may sleep).
52684fb37a25STejun Heo  *
52694fb37a25STejun Heo  *	RETURNS:
52704fb37a25STejun Heo  *	0 on success, -errno on failure.
52714fb37a25STejun Heo  */
5272fb7fd614STejun Heo int sata_link_init_spd(struct ata_link *link)
52734fb37a25STejun Heo {
527433267325STejun Heo 	u8 spd;
52754fb37a25STejun Heo 	int rc;
52764fb37a25STejun Heo 
5277d127ea7bSTejun Heo 	rc = sata_scr_read(link, SCR_CONTROL, &link->saved_scontrol);
52784fb37a25STejun Heo 	if (rc)
52794fb37a25STejun Heo 		return rc;
52804fb37a25STejun Heo 
5281d127ea7bSTejun Heo 	spd = (link->saved_scontrol >> 4) & 0xf;
52824fb37a25STejun Heo 	if (spd)
52834fb37a25STejun Heo 		link->hw_sata_spd_limit &= (1 << spd) - 1;
52844fb37a25STejun Heo 
528505944bdfSTejun Heo 	ata_force_link_limits(link);
528633267325STejun Heo 
52874fb37a25STejun Heo 	link->sata_spd_limit = link->hw_sata_spd_limit;
52884fb37a25STejun Heo 
52894fb37a25STejun Heo 	return 0;
52904fb37a25STejun Heo }
52914fb37a25STejun Heo 
52924fb37a25STejun Heo /**
5293f3187195STejun Heo  *	ata_port_alloc - allocate and initialize basic ATA port resources
5294f3187195STejun Heo  *	@host: ATA host this allocated port belongs to
5295c6fd2807SJeff Garzik  *
5296f3187195STejun Heo  *	Allocate and initialize basic ATA port resources.
5297f3187195STejun Heo  *
5298f3187195STejun Heo  *	RETURNS:
5299f3187195STejun Heo  *	Allocate ATA port on success, NULL on failure.
5300c6fd2807SJeff Garzik  *
5301c6fd2807SJeff Garzik  *	LOCKING:
5302f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5303c6fd2807SJeff Garzik  */
5304f3187195STejun Heo struct ata_port *ata_port_alloc(struct ata_host *host)
5305c6fd2807SJeff Garzik {
5306f3187195STejun Heo 	struct ata_port *ap;
5307c6fd2807SJeff Garzik 
5308f3187195STejun Heo 	DPRINTK("ENTER\n");
5309f3187195STejun Heo 
5310f3187195STejun Heo 	ap = kzalloc(sizeof(*ap), GFP_KERNEL);
5311f3187195STejun Heo 	if (!ap)
5312f3187195STejun Heo 		return NULL;
5313f3187195STejun Heo 
5314f4d6d004STejun Heo 	ap->pflags |= ATA_PFLAG_INITIALIZING;
5315cca3974eSJeff Garzik 	ap->lock = &host->lock;
5316c6fd2807SJeff Garzik 	ap->flags = ATA_FLAG_DISABLED;
5317f3187195STejun Heo 	ap->print_id = -1;
5318c6fd2807SJeff Garzik 	ap->ctl = ATA_DEVCTL_OBS;
5319cca3974eSJeff Garzik 	ap->host = host;
5320f3187195STejun Heo 	ap->dev = host->dev;
5321c6fd2807SJeff Garzik 	ap->last_ctl = 0xFF;
5322c6fd2807SJeff Garzik 
5323c6fd2807SJeff Garzik #if defined(ATA_VERBOSE_DEBUG)
5324c6fd2807SJeff Garzik 	/* turn on all debugging levels */
5325c6fd2807SJeff Garzik 	ap->msg_enable = 0x00FF;
5326c6fd2807SJeff Garzik #elif defined(ATA_DEBUG)
5327c6fd2807SJeff Garzik 	ap->msg_enable = ATA_MSG_DRV | ATA_MSG_INFO | ATA_MSG_CTL | ATA_MSG_WARN | ATA_MSG_ERR;
5328c6fd2807SJeff Garzik #else
5329c6fd2807SJeff Garzik 	ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
5330c6fd2807SJeff Garzik #endif
5331c6fd2807SJeff Garzik 
5332127102aeSTejun Heo #ifdef CONFIG_ATA_SFF
5333442eacc3SJeff Garzik 	INIT_DELAYED_WORK(&ap->port_task, ata_pio_task);
5334f667fdbbSTejun Heo #else
5335f667fdbbSTejun Heo 	INIT_DELAYED_WORK(&ap->port_task, NULL);
5336127102aeSTejun Heo #endif
533765f27f38SDavid Howells 	INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
533865f27f38SDavid Howells 	INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
5339c6fd2807SJeff Garzik 	INIT_LIST_HEAD(&ap->eh_done_q);
5340c6fd2807SJeff Garzik 	init_waitqueue_head(&ap->eh_wait_q);
534145fabbb7SElias Oltmanns 	init_completion(&ap->park_req_pending);
53425ddf24c5STejun Heo 	init_timer_deferrable(&ap->fastdrain_timer);
53435ddf24c5STejun Heo 	ap->fastdrain_timer.function = ata_eh_fastdrain_timerfn;
53445ddf24c5STejun Heo 	ap->fastdrain_timer.data = (unsigned long)ap;
5345c6fd2807SJeff Garzik 
5346c6fd2807SJeff Garzik 	ap->cbl = ATA_CBL_NONE;
5347c6fd2807SJeff Garzik 
53488989805dSTejun Heo 	ata_link_init(ap, &ap->link, 0);
5349c6fd2807SJeff Garzik 
5350c6fd2807SJeff Garzik #ifdef ATA_IRQ_TRAP
5351c6fd2807SJeff Garzik 	ap->stats.unhandled_irq = 1;
5352c6fd2807SJeff Garzik 	ap->stats.idle_irq = 1;
5353c6fd2807SJeff Garzik #endif
5354c6fd2807SJeff Garzik 	return ap;
5355c6fd2807SJeff Garzik }
5356c6fd2807SJeff Garzik 
5357f0d36efdSTejun Heo static void ata_host_release(struct device *gendev, void *res)
5358f0d36efdSTejun Heo {
5359f0d36efdSTejun Heo 	struct ata_host *host = dev_get_drvdata(gendev);
5360f0d36efdSTejun Heo 	int i;
5361f0d36efdSTejun Heo 
5362f0d36efdSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
5363f0d36efdSTejun Heo 		struct ata_port *ap = host->ports[i];
5364f0d36efdSTejun Heo 
5365ecef7253STejun Heo 		if (!ap)
5366ecef7253STejun Heo 			continue;
5367ecef7253STejun Heo 
53684911487aSTejun Heo 		if (ap->scsi_host)
53691aa506e4STejun Heo 			scsi_host_put(ap->scsi_host);
53701aa506e4STejun Heo 
5371633273a3STejun Heo 		kfree(ap->pmp_link);
5372b1c72916STejun Heo 		kfree(ap->slave_link);
53734911487aSTejun Heo 		kfree(ap);
53741aa506e4STejun Heo 		host->ports[i] = NULL;
53751aa506e4STejun Heo 	}
53761aa506e4STejun Heo 
53771aa56ccaSTejun Heo 	dev_set_drvdata(gendev, NULL);
5378f0d36efdSTejun Heo }
5379f0d36efdSTejun Heo 
5380c6fd2807SJeff Garzik /**
5381f3187195STejun Heo  *	ata_host_alloc - allocate and init basic ATA host resources
5382f3187195STejun Heo  *	@dev: generic device this host is associated with
5383f3187195STejun Heo  *	@max_ports: maximum number of ATA ports associated with this host
5384f3187195STejun Heo  *
5385f3187195STejun Heo  *	Allocate and initialize basic ATA host resources.  LLD calls
5386f3187195STejun Heo  *	this function to allocate a host, initializes it fully and
5387f3187195STejun Heo  *	attaches it using ata_host_register().
5388f3187195STejun Heo  *
5389f3187195STejun Heo  *	@max_ports ports are allocated and host->n_ports is
5390f3187195STejun Heo  *	initialized to @max_ports.  The caller is allowed to decrease
5391f3187195STejun Heo  *	host->n_ports before calling ata_host_register().  The unused
5392f3187195STejun Heo  *	ports will be automatically freed on registration.
5393f3187195STejun Heo  *
5394f3187195STejun Heo  *	RETURNS:
5395f3187195STejun Heo  *	Allocate ATA host on success, NULL on failure.
5396f3187195STejun Heo  *
5397f3187195STejun Heo  *	LOCKING:
5398f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5399f3187195STejun Heo  */
5400f3187195STejun Heo struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
5401f3187195STejun Heo {
5402f3187195STejun Heo 	struct ata_host *host;
5403f3187195STejun Heo 	size_t sz;
5404f3187195STejun Heo 	int i;
5405f3187195STejun Heo 
5406f3187195STejun Heo 	DPRINTK("ENTER\n");
5407f3187195STejun Heo 
5408f3187195STejun Heo 	if (!devres_open_group(dev, NULL, GFP_KERNEL))
5409f3187195STejun Heo 		return NULL;
5410f3187195STejun Heo 
5411f3187195STejun Heo 	/* alloc a container for our list of ATA ports (buses) */
5412f3187195STejun Heo 	sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
5413f3187195STejun Heo 	/* alloc a container for our list of ATA ports (buses) */
5414f3187195STejun Heo 	host = devres_alloc(ata_host_release, sz, GFP_KERNEL);
5415f3187195STejun Heo 	if (!host)
5416f3187195STejun Heo 		goto err_out;
5417f3187195STejun Heo 
5418f3187195STejun Heo 	devres_add(dev, host);
5419f3187195STejun Heo 	dev_set_drvdata(dev, host);
5420f3187195STejun Heo 
5421f3187195STejun Heo 	spin_lock_init(&host->lock);
5422f3187195STejun Heo 	host->dev = dev;
5423f3187195STejun Heo 	host->n_ports = max_ports;
5424f3187195STejun Heo 
5425f3187195STejun Heo 	/* allocate ports bound to this host */
5426f3187195STejun Heo 	for (i = 0; i < max_ports; i++) {
5427f3187195STejun Heo 		struct ata_port *ap;
5428f3187195STejun Heo 
5429f3187195STejun Heo 		ap = ata_port_alloc(host);
5430f3187195STejun Heo 		if (!ap)
5431f3187195STejun Heo 			goto err_out;
5432f3187195STejun Heo 
5433f3187195STejun Heo 		ap->port_no = i;
5434f3187195STejun Heo 		host->ports[i] = ap;
5435f3187195STejun Heo 	}
5436f3187195STejun Heo 
5437f3187195STejun Heo 	devres_remove_group(dev, NULL);
5438f3187195STejun Heo 	return host;
5439f3187195STejun Heo 
5440f3187195STejun Heo  err_out:
5441f3187195STejun Heo 	devres_release_group(dev, NULL);
5442f3187195STejun Heo 	return NULL;
5443f3187195STejun Heo }
5444f3187195STejun Heo 
5445f3187195STejun Heo /**
5446f5cda257STejun Heo  *	ata_host_alloc_pinfo - alloc host and init with port_info array
5447f5cda257STejun Heo  *	@dev: generic device this host is associated with
5448f5cda257STejun Heo  *	@ppi: array of ATA port_info to initialize host with
5449f5cda257STejun Heo  *	@n_ports: number of ATA ports attached to this host
5450f5cda257STejun Heo  *
5451f5cda257STejun Heo  *	Allocate ATA host and initialize with info from @ppi.  If NULL
5452f5cda257STejun Heo  *	terminated, @ppi may contain fewer entries than @n_ports.  The
5453f5cda257STejun Heo  *	last entry will be used for the remaining ports.
5454f5cda257STejun Heo  *
5455f5cda257STejun Heo  *	RETURNS:
5456f5cda257STejun Heo  *	Allocate ATA host on success, NULL on failure.
5457f5cda257STejun Heo  *
5458f5cda257STejun Heo  *	LOCKING:
5459f5cda257STejun Heo  *	Inherited from calling layer (may sleep).
5460f5cda257STejun Heo  */
5461f5cda257STejun Heo struct ata_host *ata_host_alloc_pinfo(struct device *dev,
5462f5cda257STejun Heo 				      const struct ata_port_info * const * ppi,
5463f5cda257STejun Heo 				      int n_ports)
5464f5cda257STejun Heo {
5465f5cda257STejun Heo 	const struct ata_port_info *pi;
5466f5cda257STejun Heo 	struct ata_host *host;
5467f5cda257STejun Heo 	int i, j;
5468f5cda257STejun Heo 
5469f5cda257STejun Heo 	host = ata_host_alloc(dev, n_ports);
5470f5cda257STejun Heo 	if (!host)
5471f5cda257STejun Heo 		return NULL;
5472f5cda257STejun Heo 
5473f5cda257STejun Heo 	for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
5474f5cda257STejun Heo 		struct ata_port *ap = host->ports[i];
5475f5cda257STejun Heo 
5476f5cda257STejun Heo 		if (ppi[j])
5477f5cda257STejun Heo 			pi = ppi[j++];
5478f5cda257STejun Heo 
5479f5cda257STejun Heo 		ap->pio_mask = pi->pio_mask;
5480f5cda257STejun Heo 		ap->mwdma_mask = pi->mwdma_mask;
5481f5cda257STejun Heo 		ap->udma_mask = pi->udma_mask;
5482f5cda257STejun Heo 		ap->flags |= pi->flags;
54830c88758bSTejun Heo 		ap->link.flags |= pi->link_flags;
5484f5cda257STejun Heo 		ap->ops = pi->port_ops;
5485f5cda257STejun Heo 
5486f5cda257STejun Heo 		if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
5487f5cda257STejun Heo 			host->ops = pi->port_ops;
5488f5cda257STejun Heo 	}
5489f5cda257STejun Heo 
5490f5cda257STejun Heo 	return host;
5491f5cda257STejun Heo }
5492f5cda257STejun Heo 
5493b1c72916STejun Heo /**
5494b1c72916STejun Heo  *	ata_slave_link_init - initialize slave link
5495b1c72916STejun Heo  *	@ap: port to initialize slave link for
5496b1c72916STejun Heo  *
5497b1c72916STejun Heo  *	Create and initialize slave link for @ap.  This enables slave
5498b1c72916STejun Heo  *	link handling on the port.
5499b1c72916STejun Heo  *
5500b1c72916STejun Heo  *	In libata, a port contains links and a link contains devices.
5501b1c72916STejun Heo  *	There is single host link but if a PMP is attached to it,
5502b1c72916STejun Heo  *	there can be multiple fan-out links.  On SATA, there's usually
5503b1c72916STejun Heo  *	a single device connected to a link but PATA and SATA
5504b1c72916STejun Heo  *	controllers emulating TF based interface can have two - master
5505b1c72916STejun Heo  *	and slave.
5506b1c72916STejun Heo  *
5507b1c72916STejun Heo  *	However, there are a few controllers which don't fit into this
5508b1c72916STejun Heo  *	abstraction too well - SATA controllers which emulate TF
5509b1c72916STejun Heo  *	interface with both master and slave devices but also have
5510b1c72916STejun Heo  *	separate SCR register sets for each device.  These controllers
5511b1c72916STejun Heo  *	need separate links for physical link handling
5512b1c72916STejun Heo  *	(e.g. onlineness, link speed) but should be treated like a
5513b1c72916STejun Heo  *	traditional M/S controller for everything else (e.g. command
5514b1c72916STejun Heo  *	issue, softreset).
5515b1c72916STejun Heo  *
5516b1c72916STejun Heo  *	slave_link is libata's way of handling this class of
5517b1c72916STejun Heo  *	controllers without impacting core layer too much.  For
5518b1c72916STejun Heo  *	anything other than physical link handling, the default host
5519b1c72916STejun Heo  *	link is used for both master and slave.  For physical link
5520b1c72916STejun Heo  *	handling, separate @ap->slave_link is used.  All dirty details
5521b1c72916STejun Heo  *	are implemented inside libata core layer.  From LLD's POV, the
5522b1c72916STejun Heo  *	only difference is that prereset, hardreset and postreset are
5523b1c72916STejun Heo  *	called once more for the slave link, so the reset sequence
5524b1c72916STejun Heo  *	looks like the following.
5525b1c72916STejun Heo  *
5526b1c72916STejun Heo  *	prereset(M) -> prereset(S) -> hardreset(M) -> hardreset(S) ->
5527b1c72916STejun Heo  *	softreset(M) -> postreset(M) -> postreset(S)
5528b1c72916STejun Heo  *
5529b1c72916STejun Heo  *	Note that softreset is called only for the master.  Softreset
5530b1c72916STejun Heo  *	resets both M/S by definition, so SRST on master should handle
5531b1c72916STejun Heo  *	both (the standard method will work just fine).
5532b1c72916STejun Heo  *
5533b1c72916STejun Heo  *	LOCKING:
5534b1c72916STejun Heo  *	Should be called before host is registered.
5535b1c72916STejun Heo  *
5536b1c72916STejun Heo  *	RETURNS:
5537b1c72916STejun Heo  *	0 on success, -errno on failure.
5538b1c72916STejun Heo  */
5539b1c72916STejun Heo int ata_slave_link_init(struct ata_port *ap)
5540b1c72916STejun Heo {
5541b1c72916STejun Heo 	struct ata_link *link;
5542b1c72916STejun Heo 
5543b1c72916STejun Heo 	WARN_ON(ap->slave_link);
5544b1c72916STejun Heo 	WARN_ON(ap->flags & ATA_FLAG_PMP);
5545b1c72916STejun Heo 
5546b1c72916STejun Heo 	link = kzalloc(sizeof(*link), GFP_KERNEL);
5547b1c72916STejun Heo 	if (!link)
5548b1c72916STejun Heo 		return -ENOMEM;
5549b1c72916STejun Heo 
5550b1c72916STejun Heo 	ata_link_init(ap, link, 1);
5551b1c72916STejun Heo 	ap->slave_link = link;
5552b1c72916STejun Heo 	return 0;
5553b1c72916STejun Heo }
5554b1c72916STejun Heo 
555532ebbc0cSTejun Heo static void ata_host_stop(struct device *gendev, void *res)
555632ebbc0cSTejun Heo {
555732ebbc0cSTejun Heo 	struct ata_host *host = dev_get_drvdata(gendev);
555832ebbc0cSTejun Heo 	int i;
555932ebbc0cSTejun Heo 
556032ebbc0cSTejun Heo 	WARN_ON(!(host->flags & ATA_HOST_STARTED));
556132ebbc0cSTejun Heo 
556232ebbc0cSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
556332ebbc0cSTejun Heo 		struct ata_port *ap = host->ports[i];
556432ebbc0cSTejun Heo 
556532ebbc0cSTejun Heo 		if (ap->ops->port_stop)
556632ebbc0cSTejun Heo 			ap->ops->port_stop(ap);
556732ebbc0cSTejun Heo 	}
556832ebbc0cSTejun Heo 
556932ebbc0cSTejun Heo 	if (host->ops->host_stop)
557032ebbc0cSTejun Heo 		host->ops->host_stop(host);
557132ebbc0cSTejun Heo }
557232ebbc0cSTejun Heo 
5573f5cda257STejun Heo /**
5574029cfd6bSTejun Heo  *	ata_finalize_port_ops - finalize ata_port_operations
5575029cfd6bSTejun Heo  *	@ops: ata_port_operations to finalize
5576029cfd6bSTejun Heo  *
5577029cfd6bSTejun Heo  *	An ata_port_operations can inherit from another ops and that
5578029cfd6bSTejun Heo  *	ops can again inherit from another.  This can go on as many
5579029cfd6bSTejun Heo  *	times as necessary as long as there is no loop in the
5580029cfd6bSTejun Heo  *	inheritance chain.
5581029cfd6bSTejun Heo  *
5582029cfd6bSTejun Heo  *	Ops tables are finalized when the host is started.  NULL or
5583029cfd6bSTejun Heo  *	unspecified entries are inherited from the closet ancestor
5584029cfd6bSTejun Heo  *	which has the method and the entry is populated with it.
5585029cfd6bSTejun Heo  *	After finalization, the ops table directly points to all the
5586029cfd6bSTejun Heo  *	methods and ->inherits is no longer necessary and cleared.
5587029cfd6bSTejun Heo  *
5588029cfd6bSTejun Heo  *	Using ATA_OP_NULL, inheriting ops can force a method to NULL.
5589029cfd6bSTejun Heo  *
5590029cfd6bSTejun Heo  *	LOCKING:
5591029cfd6bSTejun Heo  *	None.
5592029cfd6bSTejun Heo  */
5593029cfd6bSTejun Heo static void ata_finalize_port_ops(struct ata_port_operations *ops)
5594029cfd6bSTejun Heo {
55952da67659SPradeep Singh Rautela 	static DEFINE_SPINLOCK(lock);
5596029cfd6bSTejun Heo 	const struct ata_port_operations *cur;
5597029cfd6bSTejun Heo 	void **begin = (void **)ops;
5598029cfd6bSTejun Heo 	void **end = (void **)&ops->inherits;
5599029cfd6bSTejun Heo 	void **pp;
5600029cfd6bSTejun Heo 
5601029cfd6bSTejun Heo 	if (!ops || !ops->inherits)
5602029cfd6bSTejun Heo 		return;
5603029cfd6bSTejun Heo 
5604029cfd6bSTejun Heo 	spin_lock(&lock);
5605029cfd6bSTejun Heo 
5606029cfd6bSTejun Heo 	for (cur = ops->inherits; cur; cur = cur->inherits) {
5607029cfd6bSTejun Heo 		void **inherit = (void **)cur;
5608029cfd6bSTejun Heo 
5609029cfd6bSTejun Heo 		for (pp = begin; pp < end; pp++, inherit++)
5610029cfd6bSTejun Heo 			if (!*pp)
5611029cfd6bSTejun Heo 				*pp = *inherit;
5612029cfd6bSTejun Heo 	}
5613029cfd6bSTejun Heo 
5614029cfd6bSTejun Heo 	for (pp = begin; pp < end; pp++)
5615029cfd6bSTejun Heo 		if (IS_ERR(*pp))
5616029cfd6bSTejun Heo 			*pp = NULL;
5617029cfd6bSTejun Heo 
5618029cfd6bSTejun Heo 	ops->inherits = NULL;
5619029cfd6bSTejun Heo 
5620029cfd6bSTejun Heo 	spin_unlock(&lock);
5621029cfd6bSTejun Heo }
5622029cfd6bSTejun Heo 
5623029cfd6bSTejun Heo /**
5624ecef7253STejun Heo  *	ata_host_start - start and freeze ports of an ATA host
5625ecef7253STejun Heo  *	@host: ATA host to start ports for
5626ecef7253STejun Heo  *
5627ecef7253STejun Heo  *	Start and then freeze ports of @host.  Started status is
5628ecef7253STejun Heo  *	recorded in host->flags, so this function can be called
5629ecef7253STejun Heo  *	multiple times.  Ports are guaranteed to get started only
5630f3187195STejun Heo  *	once.  If host->ops isn't initialized yet, its set to the
5631f3187195STejun Heo  *	first non-dummy port ops.
5632ecef7253STejun Heo  *
5633ecef7253STejun Heo  *	LOCKING:
5634ecef7253STejun Heo  *	Inherited from calling layer (may sleep).
5635ecef7253STejun Heo  *
5636ecef7253STejun Heo  *	RETURNS:
5637ecef7253STejun Heo  *	0 if all ports are started successfully, -errno otherwise.
5638ecef7253STejun Heo  */
5639ecef7253STejun Heo int ata_host_start(struct ata_host *host)
5640ecef7253STejun Heo {
564132ebbc0cSTejun Heo 	int have_stop = 0;
564232ebbc0cSTejun Heo 	void *start_dr = NULL;
5643ecef7253STejun Heo 	int i, rc;
5644ecef7253STejun Heo 
5645ecef7253STejun Heo 	if (host->flags & ATA_HOST_STARTED)
5646ecef7253STejun Heo 		return 0;
5647ecef7253STejun Heo 
5648029cfd6bSTejun Heo 	ata_finalize_port_ops(host->ops);
5649029cfd6bSTejun Heo 
5650ecef7253STejun Heo 	for (i = 0; i < host->n_ports; i++) {
5651ecef7253STejun Heo 		struct ata_port *ap = host->ports[i];
5652ecef7253STejun Heo 
5653029cfd6bSTejun Heo 		ata_finalize_port_ops(ap->ops);
5654029cfd6bSTejun Heo 
5655f3187195STejun Heo 		if (!host->ops && !ata_port_is_dummy(ap))
5656f3187195STejun Heo 			host->ops = ap->ops;
5657f3187195STejun Heo 
565832ebbc0cSTejun Heo 		if (ap->ops->port_stop)
565932ebbc0cSTejun Heo 			have_stop = 1;
566032ebbc0cSTejun Heo 	}
566132ebbc0cSTejun Heo 
566232ebbc0cSTejun Heo 	if (host->ops->host_stop)
566332ebbc0cSTejun Heo 		have_stop = 1;
566432ebbc0cSTejun Heo 
566532ebbc0cSTejun Heo 	if (have_stop) {
566632ebbc0cSTejun Heo 		start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
566732ebbc0cSTejun Heo 		if (!start_dr)
566832ebbc0cSTejun Heo 			return -ENOMEM;
566932ebbc0cSTejun Heo 	}
567032ebbc0cSTejun Heo 
567132ebbc0cSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
567232ebbc0cSTejun Heo 		struct ata_port *ap = host->ports[i];
567332ebbc0cSTejun Heo 
5674ecef7253STejun Heo 		if (ap->ops->port_start) {
5675ecef7253STejun Heo 			rc = ap->ops->port_start(ap);
5676ecef7253STejun Heo 			if (rc) {
56770f9fe9b7SAlan Cox 				if (rc != -ENODEV)
56780f757743SAndrew Morton 					dev_printk(KERN_ERR, host->dev,
56790f757743SAndrew Morton 						"failed to start port %d "
56800f757743SAndrew Morton 						"(errno=%d)\n", i, rc);
5681ecef7253STejun Heo 				goto err_out;
5682ecef7253STejun Heo 			}
5683ecef7253STejun Heo 		}
5684ecef7253STejun Heo 		ata_eh_freeze_port(ap);
5685ecef7253STejun Heo 	}
5686ecef7253STejun Heo 
568732ebbc0cSTejun Heo 	if (start_dr)
568832ebbc0cSTejun Heo 		devres_add(host->dev, start_dr);
5689ecef7253STejun Heo 	host->flags |= ATA_HOST_STARTED;
5690ecef7253STejun Heo 	return 0;
5691ecef7253STejun Heo 
5692ecef7253STejun Heo  err_out:
5693ecef7253STejun Heo 	while (--i >= 0) {
5694ecef7253STejun Heo 		struct ata_port *ap = host->ports[i];
5695ecef7253STejun Heo 
5696ecef7253STejun Heo 		if (ap->ops->port_stop)
5697ecef7253STejun Heo 			ap->ops->port_stop(ap);
5698ecef7253STejun Heo 	}
569932ebbc0cSTejun Heo 	devres_free(start_dr);
5700ecef7253STejun Heo 	return rc;
5701ecef7253STejun Heo }
5702ecef7253STejun Heo 
5703ecef7253STejun Heo /**
5704cca3974eSJeff Garzik  *	ata_sas_host_init - Initialize a host struct
5705cca3974eSJeff Garzik  *	@host:	host to initialize
5706cca3974eSJeff Garzik  *	@dev:	device host is attached to
5707cca3974eSJeff Garzik  *	@flags:	host flags
5708c6fd2807SJeff Garzik  *	@ops:	port_ops
5709c6fd2807SJeff Garzik  *
5710c6fd2807SJeff Garzik  *	LOCKING:
5711c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
5712c6fd2807SJeff Garzik  *
5713c6fd2807SJeff Garzik  */
5714f3187195STejun Heo /* KILLME - the only user left is ipr */
5715cca3974eSJeff Garzik void ata_host_init(struct ata_host *host, struct device *dev,
5716029cfd6bSTejun Heo 		   unsigned long flags, struct ata_port_operations *ops)
5717c6fd2807SJeff Garzik {
5718cca3974eSJeff Garzik 	spin_lock_init(&host->lock);
5719cca3974eSJeff Garzik 	host->dev = dev;
5720cca3974eSJeff Garzik 	host->flags = flags;
5721cca3974eSJeff Garzik 	host->ops = ops;
5722c6fd2807SJeff Garzik }
5723c6fd2807SJeff Garzik 
5724c6fd2807SJeff Garzik /**
5725f3187195STejun Heo  *	ata_host_register - register initialized ATA host
5726f3187195STejun Heo  *	@host: ATA host to register
5727f3187195STejun Heo  *	@sht: template for SCSI host
5728c6fd2807SJeff Garzik  *
5729f3187195STejun Heo  *	Register initialized ATA host.  @host is allocated using
5730f3187195STejun Heo  *	ata_host_alloc() and fully initialized by LLD.  This function
5731f3187195STejun Heo  *	starts ports, registers @host with ATA and SCSI layers and
5732f3187195STejun Heo  *	probe registered devices.
5733c6fd2807SJeff Garzik  *
5734c6fd2807SJeff Garzik  *	LOCKING:
5735f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5736c6fd2807SJeff Garzik  *
5737c6fd2807SJeff Garzik  *	RETURNS:
5738f3187195STejun Heo  *	0 on success, -errno otherwise.
5739c6fd2807SJeff Garzik  */
5740f3187195STejun Heo int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
5741c6fd2807SJeff Garzik {
5742f3187195STejun Heo 	int i, rc;
5743c6fd2807SJeff Garzik 
5744f3187195STejun Heo 	/* host must have been started */
5745f3187195STejun Heo 	if (!(host->flags & ATA_HOST_STARTED)) {
5746f3187195STejun Heo 		dev_printk(KERN_ERR, host->dev,
5747f3187195STejun Heo 			   "BUG: trying to register unstarted host\n");
5748f3187195STejun Heo 		WARN_ON(1);
5749f3187195STejun Heo 		return -EINVAL;
575002f076aaSAlan Cox 	}
5751f0d36efdSTejun Heo 
5752f3187195STejun Heo 	/* Blow away unused ports.  This happens when LLD can't
5753f3187195STejun Heo 	 * determine the exact number of ports to allocate at
5754f3187195STejun Heo 	 * allocation time.
5755f3187195STejun Heo 	 */
5756f3187195STejun Heo 	for (i = host->n_ports; host->ports[i]; i++)
5757f3187195STejun Heo 		kfree(host->ports[i]);
5758f0d36efdSTejun Heo 
5759f3187195STejun Heo 	/* give ports names and add SCSI hosts */
5760f3187195STejun Heo 	for (i = 0; i < host->n_ports; i++)
5761f3187195STejun Heo 		host->ports[i]->print_id = ata_print_id++;
5762c6fd2807SJeff Garzik 
5763f3187195STejun Heo 	rc = ata_scsi_add_hosts(host, sht);
5764ecef7253STejun Heo 	if (rc)
5765f3187195STejun Heo 		return rc;
5766ecef7253STejun Heo 
5767fafbae87STejun Heo 	/* associate with ACPI nodes */
5768fafbae87STejun Heo 	ata_acpi_associate(host);
5769fafbae87STejun Heo 
5770f3187195STejun Heo 	/* set cable, sata_spd_limit and report */
5771cca3974eSJeff Garzik 	for (i = 0; i < host->n_ports; i++) {
5772cca3974eSJeff Garzik 		struct ata_port *ap = host->ports[i];
5773f3187195STejun Heo 		unsigned long xfer_mask;
5774f3187195STejun Heo 
5775f3187195STejun Heo 		/* set SATA cable type if still unset */
5776f3187195STejun Heo 		if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
5777f3187195STejun Heo 			ap->cbl = ATA_CBL_SATA;
5778c6fd2807SJeff Garzik 
5779c6fd2807SJeff Garzik 		/* init sata_spd_limit to the current value */
57804fb37a25STejun Heo 		sata_link_init_spd(&ap->link);
5781b1c72916STejun Heo 		if (ap->slave_link)
5782b1c72916STejun Heo 			sata_link_init_spd(ap->slave_link);
5783c6fd2807SJeff Garzik 
5784cbcdd875STejun Heo 		/* print per-port info to dmesg */
5785f3187195STejun Heo 		xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
5786f3187195STejun Heo 					      ap->udma_mask);
5787f3187195STejun Heo 
5788abf6e8edSTejun Heo 		if (!ata_port_is_dummy(ap)) {
5789cbcdd875STejun Heo 			ata_port_printk(ap, KERN_INFO,
5790cbcdd875STejun Heo 					"%cATA max %s %s\n",
5791a16abc0bSTejun Heo 					(ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
5792f3187195STejun Heo 					ata_mode_string(xfer_mask),
5793cbcdd875STejun Heo 					ap->link.eh_info.desc);
5794abf6e8edSTejun Heo 			ata_ehi_clear_desc(&ap->link.eh_info);
5795abf6e8edSTejun Heo 		} else
5796f3187195STejun Heo 			ata_port_printk(ap, KERN_INFO, "DUMMY\n");
5797c6fd2807SJeff Garzik 	}
5798c6fd2807SJeff Garzik 
5799f3187195STejun Heo 	/* perform each probe synchronously */
5800f3187195STejun Heo 	DPRINTK("probe begin\n");
5801f3187195STejun Heo 	for (i = 0; i < host->n_ports; i++) {
5802f3187195STejun Heo 		struct ata_port *ap = host->ports[i];
5803f3187195STejun Heo 
5804f3187195STejun Heo 		/* probe */
5805c6fd2807SJeff Garzik 		if (ap->ops->error_handler) {
58069af5c9c9STejun Heo 			struct ata_eh_info *ehi = &ap->link.eh_info;
5807c6fd2807SJeff Garzik 			unsigned long flags;
5808c6fd2807SJeff Garzik 
5809c6fd2807SJeff Garzik 			ata_port_probe(ap);
5810c6fd2807SJeff Garzik 
5811c6fd2807SJeff Garzik 			/* kick EH for boot probing */
5812c6fd2807SJeff Garzik 			spin_lock_irqsave(ap->lock, flags);
5813c6fd2807SJeff Garzik 
5814b558edddSTejun Heo 			ehi->probe_mask |= ATA_ALL_DEVICES;
5815391191c1STejun Heo 			ehi->action |= ATA_EH_RESET | ATA_EH_LPM;
5816c6fd2807SJeff Garzik 			ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
5817c6fd2807SJeff Garzik 
5818f4d6d004STejun Heo 			ap->pflags &= ~ATA_PFLAG_INITIALIZING;
5819c6fd2807SJeff Garzik 			ap->pflags |= ATA_PFLAG_LOADING;
5820c6fd2807SJeff Garzik 			ata_port_schedule_eh(ap);
5821c6fd2807SJeff Garzik 
5822c6fd2807SJeff Garzik 			spin_unlock_irqrestore(ap->lock, flags);
5823c6fd2807SJeff Garzik 
5824c6fd2807SJeff Garzik 			/* wait for EH to finish */
5825c6fd2807SJeff Garzik 			ata_port_wait_eh(ap);
5826c6fd2807SJeff Garzik 		} else {
582744877b4eSTejun Heo 			DPRINTK("ata%u: bus probe begin\n", ap->print_id);
5828c6fd2807SJeff Garzik 			rc = ata_bus_probe(ap);
582944877b4eSTejun Heo 			DPRINTK("ata%u: bus probe end\n", ap->print_id);
5830c6fd2807SJeff Garzik 
5831c6fd2807SJeff Garzik 			if (rc) {
5832c6fd2807SJeff Garzik 				/* FIXME: do something useful here?
5833c6fd2807SJeff Garzik 				 * Current libata behavior will
5834c6fd2807SJeff Garzik 				 * tear down everything when
5835c6fd2807SJeff Garzik 				 * the module is removed
5836c6fd2807SJeff Garzik 				 * or the h/w is unplugged.
5837c6fd2807SJeff Garzik 				 */
5838c6fd2807SJeff Garzik 			}
5839c6fd2807SJeff Garzik 		}
5840c6fd2807SJeff Garzik 	}
5841c6fd2807SJeff Garzik 
5842c6fd2807SJeff Garzik 	/* probes are done, now scan each port's disk(s) */
5843c6fd2807SJeff Garzik 	DPRINTK("host probe begin\n");
5844cca3974eSJeff Garzik 	for (i = 0; i < host->n_ports; i++) {
5845cca3974eSJeff Garzik 		struct ata_port *ap = host->ports[i];
5846c6fd2807SJeff Garzik 
58471ae46317STejun Heo 		ata_scsi_scan_host(ap, 1);
5848c6fd2807SJeff Garzik 	}
5849c6fd2807SJeff Garzik 
5850f3187195STejun Heo 	return 0;
5851f3187195STejun Heo }
5852f3187195STejun Heo 
5853f3187195STejun Heo /**
5854f5cda257STejun Heo  *	ata_host_activate - start host, request IRQ and register it
5855f5cda257STejun Heo  *	@host: target ATA host
5856f5cda257STejun Heo  *	@irq: IRQ to request
5857f5cda257STejun Heo  *	@irq_handler: irq_handler used when requesting IRQ
5858f5cda257STejun Heo  *	@irq_flags: irq_flags used when requesting IRQ
5859f5cda257STejun Heo  *	@sht: scsi_host_template to use when registering the host
5860f5cda257STejun Heo  *
5861f5cda257STejun Heo  *	After allocating an ATA host and initializing it, most libata
5862f5cda257STejun Heo  *	LLDs perform three steps to activate the host - start host,
5863f5cda257STejun Heo  *	request IRQ and register it.  This helper takes necessasry
5864f5cda257STejun Heo  *	arguments and performs the three steps in one go.
5865f5cda257STejun Heo  *
58663d46b2e2SPaul Mundt  *	An invalid IRQ skips the IRQ registration and expects the host to
58673d46b2e2SPaul Mundt  *	have set polling mode on the port. In this case, @irq_handler
58683d46b2e2SPaul Mundt  *	should be NULL.
58693d46b2e2SPaul Mundt  *
5870f5cda257STejun Heo  *	LOCKING:
5871f5cda257STejun Heo  *	Inherited from calling layer (may sleep).
5872f5cda257STejun Heo  *
5873f5cda257STejun Heo  *	RETURNS:
5874f5cda257STejun Heo  *	0 on success, -errno otherwise.
5875f5cda257STejun Heo  */
5876f5cda257STejun Heo int ata_host_activate(struct ata_host *host, int irq,
5877f5cda257STejun Heo 		      irq_handler_t irq_handler, unsigned long irq_flags,
5878f5cda257STejun Heo 		      struct scsi_host_template *sht)
5879f5cda257STejun Heo {
5880cbcdd875STejun Heo 	int i, rc;
5881f5cda257STejun Heo 
5882f5cda257STejun Heo 	rc = ata_host_start(host);
5883f5cda257STejun Heo 	if (rc)
5884f5cda257STejun Heo 		return rc;
5885f5cda257STejun Heo 
58863d46b2e2SPaul Mundt 	/* Special case for polling mode */
58873d46b2e2SPaul Mundt 	if (!irq) {
58883d46b2e2SPaul Mundt 		WARN_ON(irq_handler);
58893d46b2e2SPaul Mundt 		return ata_host_register(host, sht);
58903d46b2e2SPaul Mundt 	}
58913d46b2e2SPaul Mundt 
5892f5cda257STejun Heo 	rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
5893f5cda257STejun Heo 			      dev_driver_string(host->dev), host);
5894f5cda257STejun Heo 	if (rc)
5895f5cda257STejun Heo 		return rc;
5896f5cda257STejun Heo 
5897cbcdd875STejun Heo 	for (i = 0; i < host->n_ports; i++)
5898cbcdd875STejun Heo 		ata_port_desc(host->ports[i], "irq %d", irq);
58994031826bSTejun Heo 
5900f5cda257STejun Heo 	rc = ata_host_register(host, sht);
5901f5cda257STejun Heo 	/* if failed, just free the IRQ and leave ports alone */
5902f5cda257STejun Heo 	if (rc)
5903f5cda257STejun Heo 		devm_free_irq(host->dev, irq, host);
5904f5cda257STejun Heo 
5905f5cda257STejun Heo 	return rc;
5906f5cda257STejun Heo }
5907f5cda257STejun Heo 
5908f5cda257STejun Heo /**
5909c6fd2807SJeff Garzik  *	ata_port_detach - Detach ATA port in prepration of device removal
5910c6fd2807SJeff Garzik  *	@ap: ATA port to be detached
5911c6fd2807SJeff Garzik  *
5912c6fd2807SJeff Garzik  *	Detach all ATA devices and the associated SCSI devices of @ap;
5913c6fd2807SJeff Garzik  *	then, remove the associated SCSI host.  @ap is guaranteed to
5914c6fd2807SJeff Garzik  *	be quiescent on return from this function.
5915c6fd2807SJeff Garzik  *
5916c6fd2807SJeff Garzik  *	LOCKING:
5917c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
5918c6fd2807SJeff Garzik  */
5919741b7763SAdrian Bunk static void ata_port_detach(struct ata_port *ap)
5920c6fd2807SJeff Garzik {
5921c6fd2807SJeff Garzik 	unsigned long flags;
592241bda9c9STejun Heo 	struct ata_link *link;
5923f58229f8STejun Heo 	struct ata_device *dev;
5924c6fd2807SJeff Garzik 
5925c6fd2807SJeff Garzik 	if (!ap->ops->error_handler)
5926c6fd2807SJeff Garzik 		goto skip_eh;
5927c6fd2807SJeff Garzik 
5928c6fd2807SJeff Garzik 	/* tell EH we're leaving & flush EH */
5929c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
5930c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_UNLOADING;
5931c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
5932c6fd2807SJeff Garzik 
5933c6fd2807SJeff Garzik 	ata_port_wait_eh(ap);
5934c6fd2807SJeff Garzik 
59357f9ad9b8STejun Heo 	/* EH is now guaranteed to see UNLOADING - EH context belongs
5936d127ea7bSTejun Heo 	 * to us.  Restore SControl and disable all existing devices.
5937c6fd2807SJeff Garzik 	 */
5938d127ea7bSTejun Heo 	__ata_port_for_each_link(link, ap) {
5939d127ea7bSTejun Heo 		sata_scr_write(link, SCR_CONTROL, link->saved_scontrol);
594041bda9c9STejun Heo 		ata_link_for_each_dev(dev, link)
5941f58229f8STejun Heo 			ata_dev_disable(dev);
594241bda9c9STejun Heo 	}
5943c6fd2807SJeff Garzik 
5944c6fd2807SJeff Garzik 	/* Final freeze & EH.  All in-flight commands are aborted.  EH
5945c6fd2807SJeff Garzik 	 * will be skipped and retrials will be terminated with bad
5946c6fd2807SJeff Garzik 	 * target.
5947c6fd2807SJeff Garzik 	 */
5948c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
5949c6fd2807SJeff Garzik 	ata_port_freeze(ap);	/* won't be thawed */
5950c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
5951c6fd2807SJeff Garzik 
5952c6fd2807SJeff Garzik 	ata_port_wait_eh(ap);
595345a66c1cSOleg Nesterov 	cancel_rearming_delayed_work(&ap->hotplug_task);
5954c6fd2807SJeff Garzik 
5955c6fd2807SJeff Garzik  skip_eh:
5956c6fd2807SJeff Garzik 	/* remove the associated SCSI host */
5957cca3974eSJeff Garzik 	scsi_remove_host(ap->scsi_host);
5958c6fd2807SJeff Garzik }
5959c6fd2807SJeff Garzik 
5960c6fd2807SJeff Garzik /**
59610529c159STejun Heo  *	ata_host_detach - Detach all ports of an ATA host
59620529c159STejun Heo  *	@host: Host to detach
59630529c159STejun Heo  *
59640529c159STejun Heo  *	Detach all ports of @host.
59650529c159STejun Heo  *
59660529c159STejun Heo  *	LOCKING:
59670529c159STejun Heo  *	Kernel thread context (may sleep).
59680529c159STejun Heo  */
59690529c159STejun Heo void ata_host_detach(struct ata_host *host)
59700529c159STejun Heo {
59710529c159STejun Heo 	int i;
59720529c159STejun Heo 
59730529c159STejun Heo 	for (i = 0; i < host->n_ports; i++)
59740529c159STejun Heo 		ata_port_detach(host->ports[i]);
5975562f0c2dSTejun Heo 
5976562f0c2dSTejun Heo 	/* the host is dead now, dissociate ACPI */
5977562f0c2dSTejun Heo 	ata_acpi_dissociate(host);
59780529c159STejun Heo }
59790529c159STejun Heo 
5980c6fd2807SJeff Garzik #ifdef CONFIG_PCI
5981c6fd2807SJeff Garzik 
5982c6fd2807SJeff Garzik /**
5983c6fd2807SJeff Garzik  *	ata_pci_remove_one - PCI layer callback for device removal
5984c6fd2807SJeff Garzik  *	@pdev: PCI device that was removed
5985c6fd2807SJeff Garzik  *
5986b878ca5dSTejun Heo  *	PCI layer indicates to libata via this hook that hot-unplug or
5987b878ca5dSTejun Heo  *	module unload event has occurred.  Detach all ports.  Resource
5988b878ca5dSTejun Heo  *	release is handled via devres.
5989c6fd2807SJeff Garzik  *
5990c6fd2807SJeff Garzik  *	LOCKING:
5991c6fd2807SJeff Garzik  *	Inherited from PCI layer (may sleep).
5992c6fd2807SJeff Garzik  */
5993c6fd2807SJeff Garzik void ata_pci_remove_one(struct pci_dev *pdev)
5994c6fd2807SJeff Garzik {
59952855568bSJeff Garzik 	struct device *dev = &pdev->dev;
5996cca3974eSJeff Garzik 	struct ata_host *host = dev_get_drvdata(dev);
5997c6fd2807SJeff Garzik 
5998f0d36efdSTejun Heo 	ata_host_detach(host);
5999c6fd2807SJeff Garzik }
6000c6fd2807SJeff Garzik 
6001c6fd2807SJeff Garzik /* move to PCI subsystem */
6002c6fd2807SJeff Garzik int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
6003c6fd2807SJeff Garzik {
6004c6fd2807SJeff Garzik 	unsigned long tmp = 0;
6005c6fd2807SJeff Garzik 
6006c6fd2807SJeff Garzik 	switch (bits->width) {
6007c6fd2807SJeff Garzik 	case 1: {
6008c6fd2807SJeff Garzik 		u8 tmp8 = 0;
6009c6fd2807SJeff Garzik 		pci_read_config_byte(pdev, bits->reg, &tmp8);
6010c6fd2807SJeff Garzik 		tmp = tmp8;
6011c6fd2807SJeff Garzik 		break;
6012c6fd2807SJeff Garzik 	}
6013c6fd2807SJeff Garzik 	case 2: {
6014c6fd2807SJeff Garzik 		u16 tmp16 = 0;
6015c6fd2807SJeff Garzik 		pci_read_config_word(pdev, bits->reg, &tmp16);
6016c6fd2807SJeff Garzik 		tmp = tmp16;
6017c6fd2807SJeff Garzik 		break;
6018c6fd2807SJeff Garzik 	}
6019c6fd2807SJeff Garzik 	case 4: {
6020c6fd2807SJeff Garzik 		u32 tmp32 = 0;
6021c6fd2807SJeff Garzik 		pci_read_config_dword(pdev, bits->reg, &tmp32);
6022c6fd2807SJeff Garzik 		tmp = tmp32;
6023c6fd2807SJeff Garzik 		break;
6024c6fd2807SJeff Garzik 	}
6025c6fd2807SJeff Garzik 
6026c6fd2807SJeff Garzik 	default:
6027c6fd2807SJeff Garzik 		return -EINVAL;
6028c6fd2807SJeff Garzik 	}
6029c6fd2807SJeff Garzik 
6030c6fd2807SJeff Garzik 	tmp &= bits->mask;
6031c6fd2807SJeff Garzik 
6032c6fd2807SJeff Garzik 	return (tmp == bits->val) ? 1 : 0;
6033c6fd2807SJeff Garzik }
6034c6fd2807SJeff Garzik 
60356ffa01d8STejun Heo #ifdef CONFIG_PM
6036c6fd2807SJeff Garzik void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
6037c6fd2807SJeff Garzik {
6038c6fd2807SJeff Garzik 	pci_save_state(pdev);
6039c6fd2807SJeff Garzik 	pci_disable_device(pdev);
60404c90d971STejun Heo 
60413a2d5b70SRafael J. Wysocki 	if (mesg.event & PM_EVENT_SLEEP)
6042c6fd2807SJeff Garzik 		pci_set_power_state(pdev, PCI_D3hot);
6043c6fd2807SJeff Garzik }
6044c6fd2807SJeff Garzik 
6045553c4aa6STejun Heo int ata_pci_device_do_resume(struct pci_dev *pdev)
6046c6fd2807SJeff Garzik {
6047553c4aa6STejun Heo 	int rc;
6048553c4aa6STejun Heo 
6049c6fd2807SJeff Garzik 	pci_set_power_state(pdev, PCI_D0);
6050c6fd2807SJeff Garzik 	pci_restore_state(pdev);
6051553c4aa6STejun Heo 
6052f0d36efdSTejun Heo 	rc = pcim_enable_device(pdev);
6053553c4aa6STejun Heo 	if (rc) {
6054553c4aa6STejun Heo 		dev_printk(KERN_ERR, &pdev->dev,
6055553c4aa6STejun Heo 			   "failed to enable device after resume (%d)\n", rc);
6056553c4aa6STejun Heo 		return rc;
6057553c4aa6STejun Heo 	}
6058553c4aa6STejun Heo 
6059c6fd2807SJeff Garzik 	pci_set_master(pdev);
6060553c4aa6STejun Heo 	return 0;
6061c6fd2807SJeff Garzik }
6062c6fd2807SJeff Garzik 
6063c6fd2807SJeff Garzik int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
6064c6fd2807SJeff Garzik {
6065cca3974eSJeff Garzik 	struct ata_host *host = dev_get_drvdata(&pdev->dev);
6066c6fd2807SJeff Garzik 	int rc = 0;
6067c6fd2807SJeff Garzik 
6068cca3974eSJeff Garzik 	rc = ata_host_suspend(host, mesg);
6069c6fd2807SJeff Garzik 	if (rc)
6070c6fd2807SJeff Garzik 		return rc;
6071c6fd2807SJeff Garzik 
6072c6fd2807SJeff Garzik 	ata_pci_device_do_suspend(pdev, mesg);
6073c6fd2807SJeff Garzik 
6074c6fd2807SJeff Garzik 	return 0;
6075c6fd2807SJeff Garzik }
6076c6fd2807SJeff Garzik 
6077c6fd2807SJeff Garzik int ata_pci_device_resume(struct pci_dev *pdev)
6078c6fd2807SJeff Garzik {
6079cca3974eSJeff Garzik 	struct ata_host *host = dev_get_drvdata(&pdev->dev);
6080553c4aa6STejun Heo 	int rc;
6081c6fd2807SJeff Garzik 
6082553c4aa6STejun Heo 	rc = ata_pci_device_do_resume(pdev);
6083553c4aa6STejun Heo 	if (rc == 0)
6084cca3974eSJeff Garzik 		ata_host_resume(host);
6085553c4aa6STejun Heo 	return rc;
6086c6fd2807SJeff Garzik }
60876ffa01d8STejun Heo #endif /* CONFIG_PM */
60886ffa01d8STejun Heo 
6089c6fd2807SJeff Garzik #endif /* CONFIG_PCI */
6090c6fd2807SJeff Garzik 
609133267325STejun Heo static int __init ata_parse_force_one(char **cur,
609233267325STejun Heo 				      struct ata_force_ent *force_ent,
609333267325STejun Heo 				      const char **reason)
609433267325STejun Heo {
609533267325STejun Heo 	/* FIXME: Currently, there's no way to tag init const data and
609633267325STejun Heo 	 * using __initdata causes build failure on some versions of
609733267325STejun Heo 	 * gcc.  Once __initdataconst is implemented, add const to the
609833267325STejun Heo 	 * following structure.
609933267325STejun Heo 	 */
610033267325STejun Heo 	static struct ata_force_param force_tbl[] __initdata = {
610133267325STejun Heo 		{ "40c",	.cbl		= ATA_CBL_PATA40 },
610233267325STejun Heo 		{ "80c",	.cbl		= ATA_CBL_PATA80 },
610333267325STejun Heo 		{ "short40c",	.cbl		= ATA_CBL_PATA40_SHORT },
610433267325STejun Heo 		{ "unk",	.cbl		= ATA_CBL_PATA_UNK },
610533267325STejun Heo 		{ "ign",	.cbl		= ATA_CBL_PATA_IGN },
610633267325STejun Heo 		{ "sata",	.cbl		= ATA_CBL_SATA },
610733267325STejun Heo 		{ "1.5Gbps",	.spd_limit	= 1 },
610833267325STejun Heo 		{ "3.0Gbps",	.spd_limit	= 2 },
610933267325STejun Heo 		{ "noncq",	.horkage_on	= ATA_HORKAGE_NONCQ },
611033267325STejun Heo 		{ "ncq",	.horkage_off	= ATA_HORKAGE_NONCQ },
611133267325STejun Heo 		{ "pio0",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 0) },
611233267325STejun Heo 		{ "pio1",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 1) },
611333267325STejun Heo 		{ "pio2",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 2) },
611433267325STejun Heo 		{ "pio3",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 3) },
611533267325STejun Heo 		{ "pio4",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 4) },
611633267325STejun Heo 		{ "pio5",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 5) },
611733267325STejun Heo 		{ "pio6",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 6) },
611833267325STejun Heo 		{ "mwdma0",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 0) },
611933267325STejun Heo 		{ "mwdma1",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 1) },
612033267325STejun Heo 		{ "mwdma2",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 2) },
612133267325STejun Heo 		{ "mwdma3",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 3) },
612233267325STejun Heo 		{ "mwdma4",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 4) },
612333267325STejun Heo 		{ "udma0",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
612433267325STejun Heo 		{ "udma16",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
612533267325STejun Heo 		{ "udma/16",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
612633267325STejun Heo 		{ "udma1",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
612733267325STejun Heo 		{ "udma25",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
612833267325STejun Heo 		{ "udma/25",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
612933267325STejun Heo 		{ "udma2",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
613033267325STejun Heo 		{ "udma33",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
613133267325STejun Heo 		{ "udma/33",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
613233267325STejun Heo 		{ "udma3",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
613333267325STejun Heo 		{ "udma44",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
613433267325STejun Heo 		{ "udma/44",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
613533267325STejun Heo 		{ "udma4",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
613633267325STejun Heo 		{ "udma66",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
613733267325STejun Heo 		{ "udma/66",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
613833267325STejun Heo 		{ "udma5",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
613933267325STejun Heo 		{ "udma100",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
614033267325STejun Heo 		{ "udma/100",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
614133267325STejun Heo 		{ "udma6",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
614233267325STejun Heo 		{ "udma133",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
614333267325STejun Heo 		{ "udma/133",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
614433267325STejun Heo 		{ "udma7",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 7) },
614505944bdfSTejun Heo 		{ "nohrst",	.lflags		= ATA_LFLAG_NO_HRST },
614605944bdfSTejun Heo 		{ "nosrst",	.lflags		= ATA_LFLAG_NO_SRST },
614705944bdfSTejun Heo 		{ "norst",	.lflags		= ATA_LFLAG_NO_HRST | ATA_LFLAG_NO_SRST },
614833267325STejun Heo 	};
614933267325STejun Heo 	char *start = *cur, *p = *cur;
615033267325STejun Heo 	char *id, *val, *endp;
615133267325STejun Heo 	const struct ata_force_param *match_fp = NULL;
615233267325STejun Heo 	int nr_matches = 0, i;
615333267325STejun Heo 
615433267325STejun Heo 	/* find where this param ends and update *cur */
615533267325STejun Heo 	while (*p != '\0' && *p != ',')
615633267325STejun Heo 		p++;
615733267325STejun Heo 
615833267325STejun Heo 	if (*p == '\0')
615933267325STejun Heo 		*cur = p;
616033267325STejun Heo 	else
616133267325STejun Heo 		*cur = p + 1;
616233267325STejun Heo 
616333267325STejun Heo 	*p = '\0';
616433267325STejun Heo 
616533267325STejun Heo 	/* parse */
616633267325STejun Heo 	p = strchr(start, ':');
616733267325STejun Heo 	if (!p) {
616833267325STejun Heo 		val = strstrip(start);
616933267325STejun Heo 		goto parse_val;
617033267325STejun Heo 	}
617133267325STejun Heo 	*p = '\0';
617233267325STejun Heo 
617333267325STejun Heo 	id = strstrip(start);
617433267325STejun Heo 	val = strstrip(p + 1);
617533267325STejun Heo 
617633267325STejun Heo 	/* parse id */
617733267325STejun Heo 	p = strchr(id, '.');
617833267325STejun Heo 	if (p) {
617933267325STejun Heo 		*p++ = '\0';
618033267325STejun Heo 		force_ent->device = simple_strtoul(p, &endp, 10);
618133267325STejun Heo 		if (p == endp || *endp != '\0') {
618233267325STejun Heo 			*reason = "invalid device";
618333267325STejun Heo 			return -EINVAL;
618433267325STejun Heo 		}
618533267325STejun Heo 	}
618633267325STejun Heo 
618733267325STejun Heo 	force_ent->port = simple_strtoul(id, &endp, 10);
618833267325STejun Heo 	if (p == endp || *endp != '\0') {
618933267325STejun Heo 		*reason = "invalid port/link";
619033267325STejun Heo 		return -EINVAL;
619133267325STejun Heo 	}
619233267325STejun Heo 
619333267325STejun Heo  parse_val:
619433267325STejun Heo 	/* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */
619533267325STejun Heo 	for (i = 0; i < ARRAY_SIZE(force_tbl); i++) {
619633267325STejun Heo 		const struct ata_force_param *fp = &force_tbl[i];
619733267325STejun Heo 
619833267325STejun Heo 		if (strncasecmp(val, fp->name, strlen(val)))
619933267325STejun Heo 			continue;
620033267325STejun Heo 
620133267325STejun Heo 		nr_matches++;
620233267325STejun Heo 		match_fp = fp;
620333267325STejun Heo 
620433267325STejun Heo 		if (strcasecmp(val, fp->name) == 0) {
620533267325STejun Heo 			nr_matches = 1;
620633267325STejun Heo 			break;
620733267325STejun Heo 		}
620833267325STejun Heo 	}
620933267325STejun Heo 
621033267325STejun Heo 	if (!nr_matches) {
621133267325STejun Heo 		*reason = "unknown value";
621233267325STejun Heo 		return -EINVAL;
621333267325STejun Heo 	}
621433267325STejun Heo 	if (nr_matches > 1) {
621533267325STejun Heo 		*reason = "ambigious value";
621633267325STejun Heo 		return -EINVAL;
621733267325STejun Heo 	}
621833267325STejun Heo 
621933267325STejun Heo 	force_ent->param = *match_fp;
622033267325STejun Heo 
622133267325STejun Heo 	return 0;
622233267325STejun Heo }
622333267325STejun Heo 
622433267325STejun Heo static void __init ata_parse_force_param(void)
622533267325STejun Heo {
622633267325STejun Heo 	int idx = 0, size = 1;
622733267325STejun Heo 	int last_port = -1, last_device = -1;
622833267325STejun Heo 	char *p, *cur, *next;
622933267325STejun Heo 
623033267325STejun Heo 	/* calculate maximum number of params and allocate force_tbl */
623133267325STejun Heo 	for (p = ata_force_param_buf; *p; p++)
623233267325STejun Heo 		if (*p == ',')
623333267325STejun Heo 			size++;
623433267325STejun Heo 
623533267325STejun Heo 	ata_force_tbl = kzalloc(sizeof(ata_force_tbl[0]) * size, GFP_KERNEL);
623633267325STejun Heo 	if (!ata_force_tbl) {
623733267325STejun Heo 		printk(KERN_WARNING "ata: failed to extend force table, "
623833267325STejun Heo 		       "libata.force ignored\n");
623933267325STejun Heo 		return;
624033267325STejun Heo 	}
624133267325STejun Heo 
624233267325STejun Heo 	/* parse and populate the table */
624333267325STejun Heo 	for (cur = ata_force_param_buf; *cur != '\0'; cur = next) {
624433267325STejun Heo 		const char *reason = "";
624533267325STejun Heo 		struct ata_force_ent te = { .port = -1, .device = -1 };
624633267325STejun Heo 
624733267325STejun Heo 		next = cur;
624833267325STejun Heo 		if (ata_parse_force_one(&next, &te, &reason)) {
624933267325STejun Heo 			printk(KERN_WARNING "ata: failed to parse force "
625033267325STejun Heo 			       "parameter \"%s\" (%s)\n",
625133267325STejun Heo 			       cur, reason);
625233267325STejun Heo 			continue;
625333267325STejun Heo 		}
625433267325STejun Heo 
625533267325STejun Heo 		if (te.port == -1) {
625633267325STejun Heo 			te.port = last_port;
625733267325STejun Heo 			te.device = last_device;
625833267325STejun Heo 		}
625933267325STejun Heo 
626033267325STejun Heo 		ata_force_tbl[idx++] = te;
626133267325STejun Heo 
626233267325STejun Heo 		last_port = te.port;
626333267325STejun Heo 		last_device = te.device;
626433267325STejun Heo 	}
626533267325STejun Heo 
626633267325STejun Heo 	ata_force_tbl_size = idx;
626733267325STejun Heo }
6268c6fd2807SJeff Garzik 
6269c6fd2807SJeff Garzik static int __init ata_init(void)
6270c6fd2807SJeff Garzik {
627133267325STejun Heo 	ata_parse_force_param();
627233267325STejun Heo 
6273c6fd2807SJeff Garzik 	ata_wq = create_workqueue("ata");
6274c6fd2807SJeff Garzik 	if (!ata_wq)
627549ea3b04SElias Oltmanns 		goto free_force_tbl;
6276c6fd2807SJeff Garzik 
6277c6fd2807SJeff Garzik 	ata_aux_wq = create_singlethread_workqueue("ata_aux");
627849ea3b04SElias Oltmanns 	if (!ata_aux_wq)
627949ea3b04SElias Oltmanns 		goto free_wq;
6280c6fd2807SJeff Garzik 
6281c6fd2807SJeff Garzik 	printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
6282c6fd2807SJeff Garzik 	return 0;
628349ea3b04SElias Oltmanns 
628449ea3b04SElias Oltmanns free_wq:
628549ea3b04SElias Oltmanns 	destroy_workqueue(ata_wq);
628649ea3b04SElias Oltmanns free_force_tbl:
628749ea3b04SElias Oltmanns 	kfree(ata_force_tbl);
628849ea3b04SElias Oltmanns 	return -ENOMEM;
6289c6fd2807SJeff Garzik }
6290c6fd2807SJeff Garzik 
6291c6fd2807SJeff Garzik static void __exit ata_exit(void)
6292c6fd2807SJeff Garzik {
629333267325STejun Heo 	kfree(ata_force_tbl);
6294c6fd2807SJeff Garzik 	destroy_workqueue(ata_wq);
6295c6fd2807SJeff Garzik 	destroy_workqueue(ata_aux_wq);
6296c6fd2807SJeff Garzik }
6297c6fd2807SJeff Garzik 
6298a4625085SBrian King subsys_initcall(ata_init);
6299c6fd2807SJeff Garzik module_exit(ata_exit);
6300c6fd2807SJeff Garzik 
6301c6fd2807SJeff Garzik static unsigned long ratelimit_time;
6302c6fd2807SJeff Garzik static DEFINE_SPINLOCK(ata_ratelimit_lock);
6303c6fd2807SJeff Garzik 
6304c6fd2807SJeff Garzik int ata_ratelimit(void)
6305c6fd2807SJeff Garzik {
6306c6fd2807SJeff Garzik 	int rc;
6307c6fd2807SJeff Garzik 	unsigned long flags;
6308c6fd2807SJeff Garzik 
6309c6fd2807SJeff Garzik 	spin_lock_irqsave(&ata_ratelimit_lock, flags);
6310c6fd2807SJeff Garzik 
6311c6fd2807SJeff Garzik 	if (time_after(jiffies, ratelimit_time)) {
6312c6fd2807SJeff Garzik 		rc = 1;
6313c6fd2807SJeff Garzik 		ratelimit_time = jiffies + (HZ/5);
6314c6fd2807SJeff Garzik 	} else
6315c6fd2807SJeff Garzik 		rc = 0;
6316c6fd2807SJeff Garzik 
6317c6fd2807SJeff Garzik 	spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
6318c6fd2807SJeff Garzik 
6319c6fd2807SJeff Garzik 	return rc;
6320c6fd2807SJeff Garzik }
6321c6fd2807SJeff Garzik 
6322c6fd2807SJeff Garzik /**
6323c6fd2807SJeff Garzik  *	ata_wait_register - wait until register value changes
6324c6fd2807SJeff Garzik  *	@reg: IO-mapped register
6325c6fd2807SJeff Garzik  *	@mask: Mask to apply to read register value
6326c6fd2807SJeff Garzik  *	@val: Wait condition
6327341c2c95STejun Heo  *	@interval: polling interval in milliseconds
6328341c2c95STejun Heo  *	@timeout: timeout in milliseconds
6329c6fd2807SJeff Garzik  *
6330c6fd2807SJeff Garzik  *	Waiting for some bits of register to change is a common
6331c6fd2807SJeff Garzik  *	operation for ATA controllers.  This function reads 32bit LE
6332c6fd2807SJeff Garzik  *	IO-mapped register @reg and tests for the following condition.
6333c6fd2807SJeff Garzik  *
6334c6fd2807SJeff Garzik  *	(*@reg & mask) != val
6335c6fd2807SJeff Garzik  *
6336c6fd2807SJeff Garzik  *	If the condition is met, it returns; otherwise, the process is
6337c6fd2807SJeff Garzik  *	repeated after @interval_msec until timeout.
6338c6fd2807SJeff Garzik  *
6339c6fd2807SJeff Garzik  *	LOCKING:
6340c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
6341c6fd2807SJeff Garzik  *
6342c6fd2807SJeff Garzik  *	RETURNS:
6343c6fd2807SJeff Garzik  *	The final register value.
6344c6fd2807SJeff Garzik  */
6345c6fd2807SJeff Garzik u32 ata_wait_register(void __iomem *reg, u32 mask, u32 val,
6346341c2c95STejun Heo 		      unsigned long interval, unsigned long timeout)
6347c6fd2807SJeff Garzik {
6348341c2c95STejun Heo 	unsigned long deadline;
6349c6fd2807SJeff Garzik 	u32 tmp;
6350c6fd2807SJeff Garzik 
6351c6fd2807SJeff Garzik 	tmp = ioread32(reg);
6352c6fd2807SJeff Garzik 
6353c6fd2807SJeff Garzik 	/* Calculate timeout _after_ the first read to make sure
6354c6fd2807SJeff Garzik 	 * preceding writes reach the controller before starting to
6355c6fd2807SJeff Garzik 	 * eat away the timeout.
6356c6fd2807SJeff Garzik 	 */
6357341c2c95STejun Heo 	deadline = ata_deadline(jiffies, timeout);
6358c6fd2807SJeff Garzik 
6359341c2c95STejun Heo 	while ((tmp & mask) == val && time_before(jiffies, deadline)) {
6360341c2c95STejun Heo 		msleep(interval);
6361c6fd2807SJeff Garzik 		tmp = ioread32(reg);
6362c6fd2807SJeff Garzik 	}
6363c6fd2807SJeff Garzik 
6364c6fd2807SJeff Garzik 	return tmp;
6365c6fd2807SJeff Garzik }
6366c6fd2807SJeff Garzik 
6367c6fd2807SJeff Garzik /*
6368c6fd2807SJeff Garzik  * Dummy port_ops
6369c6fd2807SJeff Garzik  */
6370c6fd2807SJeff Garzik static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
6371c6fd2807SJeff Garzik {
6372c6fd2807SJeff Garzik 	return AC_ERR_SYSTEM;
6373c6fd2807SJeff Garzik }
6374c6fd2807SJeff Garzik 
6375182d7bbaSTejun Heo static void ata_dummy_error_handler(struct ata_port *ap)
6376182d7bbaSTejun Heo {
6377182d7bbaSTejun Heo 	/* truly dummy */
6378182d7bbaSTejun Heo }
6379182d7bbaSTejun Heo 
6380029cfd6bSTejun Heo struct ata_port_operations ata_dummy_port_ops = {
6381c6fd2807SJeff Garzik 	.qc_prep		= ata_noop_qc_prep,
6382c6fd2807SJeff Garzik 	.qc_issue		= ata_dummy_qc_issue,
6383182d7bbaSTejun Heo 	.error_handler		= ata_dummy_error_handler,
6384c6fd2807SJeff Garzik };
6385c6fd2807SJeff Garzik 
638621b0ad4fSTejun Heo const struct ata_port_info ata_dummy_port_info = {
638721b0ad4fSTejun Heo 	.port_ops		= &ata_dummy_port_ops,
638821b0ad4fSTejun Heo };
638921b0ad4fSTejun Heo 
6390c6fd2807SJeff Garzik /*
6391c6fd2807SJeff Garzik  * libata is essentially a library of internal helper functions for
6392c6fd2807SJeff Garzik  * low-level ATA host controller drivers.  As such, the API/ABI is
6393c6fd2807SJeff Garzik  * likely to change as new drivers are added and updated.
6394c6fd2807SJeff Garzik  * Do not depend on ABI/API stability.
6395c6fd2807SJeff Garzik  */
6396c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
6397c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
6398c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_deb_timing_long);
6399029cfd6bSTejun Heo EXPORT_SYMBOL_GPL(ata_base_port_ops);
6400029cfd6bSTejun Heo EXPORT_SYMBOL_GPL(sata_port_ops);
6401c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
640221b0ad4fSTejun Heo EXPORT_SYMBOL_GPL(ata_dummy_port_info);
6403aadffb68STejun Heo EXPORT_SYMBOL_GPL(__ata_port_next_link);
6404c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_std_bios_param);
6405cca3974eSJeff Garzik EXPORT_SYMBOL_GPL(ata_host_init);
6406f3187195STejun Heo EXPORT_SYMBOL_GPL(ata_host_alloc);
6407f5cda257STejun Heo EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
6408b1c72916STejun Heo EXPORT_SYMBOL_GPL(ata_slave_link_init);
6409ecef7253STejun Heo EXPORT_SYMBOL_GPL(ata_host_start);
6410f3187195STejun Heo EXPORT_SYMBOL_GPL(ata_host_register);
6411f5cda257STejun Heo EXPORT_SYMBOL_GPL(ata_host_activate);
64120529c159STejun Heo EXPORT_SYMBOL_GPL(ata_host_detach);
6413c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_sg_init);
6414c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_qc_complete);
6415c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
6416436d34b3STejun Heo EXPORT_SYMBOL_GPL(atapi_cmd_type);
6417c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_tf_to_fis);
6418c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_tf_from_fis);
64196357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_pack_xfermask);
64206357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_unpack_xfermask);
64216357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
64226357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
64236357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
64246357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_mode_string);
64256357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_id_xfermask);
6426c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_start);
642704351821SAlan EXPORT_SYMBOL_GPL(ata_do_set_mode);
642831cc23b3STejun Heo EXPORT_SYMBOL_GPL(ata_std_qc_defer);
6429c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
6430c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_probe);
643110305f0fSAlan EXPORT_SYMBOL_GPL(ata_dev_disable);
6432c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_set_spd);
6433aa2731adSTejun Heo EXPORT_SYMBOL_GPL(ata_wait_after_reset);
6434936fd732STejun Heo EXPORT_SYMBOL_GPL(sata_link_debounce);
6435936fd732STejun Heo EXPORT_SYMBOL_GPL(sata_link_resume);
64360aa1113dSTejun Heo EXPORT_SYMBOL_GPL(ata_std_prereset);
6437cc0680a5STejun Heo EXPORT_SYMBOL_GPL(sata_link_hardreset);
643857c9efdfSTejun Heo EXPORT_SYMBOL_GPL(sata_std_hardreset);
6439203c75b8STejun Heo EXPORT_SYMBOL_GPL(ata_std_postreset);
6440c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_dev_classify);
6441c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_dev_pair);
6442c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_disable);
6443c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_ratelimit);
6444c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_wait_register);
6445c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
6446c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
6447c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
6448c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
6449c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
6450c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_valid);
6451c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_read);
6452c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_write);
6453c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_write_flush);
6454936fd732STejun Heo EXPORT_SYMBOL_GPL(ata_link_online);
6455936fd732STejun Heo EXPORT_SYMBOL_GPL(ata_link_offline);
64566ffa01d8STejun Heo #ifdef CONFIG_PM
6457cca3974eSJeff Garzik EXPORT_SYMBOL_GPL(ata_host_suspend);
6458cca3974eSJeff Garzik EXPORT_SYMBOL_GPL(ata_host_resume);
64596ffa01d8STejun Heo #endif /* CONFIG_PM */
6460c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_id_string);
6461c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_id_c_string);
6462963e4975SAlan Cox EXPORT_SYMBOL_GPL(ata_do_dev_read_id);
6463c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_simulate);
6464c6fd2807SJeff Garzik 
6465c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
64666357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_timing_find_mode);
6467c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_timing_compute);
6468c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_timing_merge);
6469a0f79b92STejun Heo EXPORT_SYMBOL_GPL(ata_timing_cycle2mode);
6470c6fd2807SJeff Garzik 
6471c6fd2807SJeff Garzik #ifdef CONFIG_PCI
6472c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(pci_test_config_bits);
6473c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_remove_one);
64746ffa01d8STejun Heo #ifdef CONFIG_PM
6475c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
6476c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
6477c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
6478c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_resume);
64796ffa01d8STejun Heo #endif /* CONFIG_PM */
6480c6fd2807SJeff Garzik #endif /* CONFIG_PCI */
6481c6fd2807SJeff Garzik 
6482b64bbc39STejun Heo EXPORT_SYMBOL_GPL(__ata_ehi_push_desc);
6483b64bbc39STejun Heo EXPORT_SYMBOL_GPL(ata_ehi_push_desc);
6484b64bbc39STejun Heo EXPORT_SYMBOL_GPL(ata_ehi_clear_desc);
6485cbcdd875STejun Heo EXPORT_SYMBOL_GPL(ata_port_desc);
6486cbcdd875STejun Heo #ifdef CONFIG_PCI
6487cbcdd875STejun Heo EXPORT_SYMBOL_GPL(ata_port_pbar_desc);
6488cbcdd875STejun Heo #endif /* CONFIG_PCI */
6489c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
6490dbd82616STejun Heo EXPORT_SYMBOL_GPL(ata_link_abort);
6491c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_abort);
6492c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_freeze);
64937d77b247STejun Heo EXPORT_SYMBOL_GPL(sata_async_notification);
6494c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
6495c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
6496c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
6497c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
649810acf3b0SMark Lord EXPORT_SYMBOL_GPL(ata_eh_analyze_ncq_error);
6499c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_do_eh);
6500a1efdabaSTejun Heo EXPORT_SYMBOL_GPL(ata_std_error_handler);
6501be0d18dfSAlan Cox 
6502be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_40wire);
6503be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_80wire);
6504be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_unknown);
6505c88f90c3STejun Heo EXPORT_SYMBOL_GPL(ata_cable_ignore);
6506be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_sata);
6507