xref: /openbmc/linux/drivers/ata/libata-core.c (revision 2fca5ccf)
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;
12711e999736SAlan Cox 	sectors |= (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
1605c6fd2807SJeff Garzik  *	@fn: workqueue function to be scheduled
160665f27f38SDavid Howells  *	@data: data for @fn to use
1607341c2c95STejun Heo  *	@delay: delay time in msecs for workqueue function
1608c6fd2807SJeff Garzik  *
1609c6fd2807SJeff Garzik  *	Schedule @fn(@data) for execution after @delay jiffies using
1610c6fd2807SJeff Garzik  *	port_task.  There is one port_task per port and it's the
1611c6fd2807SJeff Garzik  *	user(low level driver)'s responsibility to make sure that only
1612c6fd2807SJeff Garzik  *	one task is active at any given time.
1613c6fd2807SJeff Garzik  *
1614c6fd2807SJeff Garzik  *	libata core layer takes care of synchronization between
1615442eacc3SJeff Garzik  *	port_task and EH.  ata_pio_queue_task() may be ignored for EH
1616c6fd2807SJeff Garzik  *	synchronization.
1617c6fd2807SJeff Garzik  *
1618c6fd2807SJeff Garzik  *	LOCKING:
1619c6fd2807SJeff Garzik  *	Inherited from caller.
1620c6fd2807SJeff Garzik  */
1621624d5c51STejun Heo void ata_pio_queue_task(struct ata_port *ap, void *data, unsigned long delay)
1622c6fd2807SJeff Garzik {
162365f27f38SDavid Howells 	ap->port_task_data = data;
1624c6fd2807SJeff Garzik 
162545a66c1cSOleg Nesterov 	/* may fail if ata_port_flush_task() in progress */
1626341c2c95STejun Heo 	queue_delayed_work(ata_wq, &ap->port_task, msecs_to_jiffies(delay));
1627c6fd2807SJeff Garzik }
1628c6fd2807SJeff Garzik 
1629c6fd2807SJeff Garzik /**
1630c6fd2807SJeff Garzik  *	ata_port_flush_task - Flush port_task
1631c6fd2807SJeff Garzik  *	@ap: The ata_port to flush port_task for
1632c6fd2807SJeff Garzik  *
1633c6fd2807SJeff Garzik  *	After this function completes, port_task is guranteed not to
1634c6fd2807SJeff Garzik  *	be running or scheduled.
1635c6fd2807SJeff Garzik  *
1636c6fd2807SJeff Garzik  *	LOCKING:
1637c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
1638c6fd2807SJeff Garzik  */
1639c6fd2807SJeff Garzik void ata_port_flush_task(struct ata_port *ap)
1640c6fd2807SJeff Garzik {
1641c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
1642c6fd2807SJeff Garzik 
164345a66c1cSOleg Nesterov 	cancel_rearming_delayed_work(&ap->port_task);
1644c6fd2807SJeff Garzik 
1645c6fd2807SJeff Garzik 	if (ata_msg_ctl(ap))
16467f5e4e8dSHarvey Harrison 		ata_port_printk(ap, KERN_DEBUG, "%s: EXIT\n", __func__);
1647c6fd2807SJeff Garzik }
1648c6fd2807SJeff Garzik 
16497102d230SAdrian Bunk static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
1650c6fd2807SJeff Garzik {
1651c6fd2807SJeff Garzik 	struct completion *waiting = qc->private_data;
1652c6fd2807SJeff Garzik 
1653c6fd2807SJeff Garzik 	complete(waiting);
1654c6fd2807SJeff Garzik }
1655c6fd2807SJeff Garzik 
1656c6fd2807SJeff Garzik /**
16572432697bSTejun Heo  *	ata_exec_internal_sg - execute libata internal command
1658c6fd2807SJeff Garzik  *	@dev: Device to which the command is sent
1659c6fd2807SJeff Garzik  *	@tf: Taskfile registers for the command and the result
1660c6fd2807SJeff Garzik  *	@cdb: CDB for packet command
1661c6fd2807SJeff Garzik  *	@dma_dir: Data tranfer direction of the command
16625c1ad8b3SRandy Dunlap  *	@sgl: sg list for the data buffer of the command
16632432697bSTejun Heo  *	@n_elem: Number of sg entries
16642b789108STejun Heo  *	@timeout: Timeout in msecs (0 for default)
1665c6fd2807SJeff Garzik  *
1666c6fd2807SJeff Garzik  *	Executes libata internal command with timeout.  @tf contains
1667c6fd2807SJeff Garzik  *	command on entry and result on return.  Timeout and error
1668c6fd2807SJeff Garzik  *	conditions are reported via return value.  No recovery action
1669c6fd2807SJeff Garzik  *	is taken after a command times out.  It's caller's duty to
1670c6fd2807SJeff Garzik  *	clean up after timeout.
1671c6fd2807SJeff Garzik  *
1672c6fd2807SJeff Garzik  *	LOCKING:
1673c6fd2807SJeff Garzik  *	None.  Should be called with kernel context, might sleep.
1674c6fd2807SJeff Garzik  *
1675c6fd2807SJeff Garzik  *	RETURNS:
1676c6fd2807SJeff Garzik  *	Zero on success, AC_ERR_* mask on failure
1677c6fd2807SJeff Garzik  */
16782432697bSTejun Heo unsigned ata_exec_internal_sg(struct ata_device *dev,
1679c6fd2807SJeff Garzik 			      struct ata_taskfile *tf, const u8 *cdb,
168087260216SJens Axboe 			      int dma_dir, struct scatterlist *sgl,
16812b789108STejun Heo 			      unsigned int n_elem, unsigned long timeout)
1682c6fd2807SJeff Garzik {
16839af5c9c9STejun Heo 	struct ata_link *link = dev->link;
16849af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
1685c6fd2807SJeff Garzik 	u8 command = tf->command;
168687fbc5a0STejun Heo 	int auto_timeout = 0;
1687c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
1688c6fd2807SJeff Garzik 	unsigned int tag, preempted_tag;
1689c6fd2807SJeff Garzik 	u32 preempted_sactive, preempted_qc_active;
1690da917d69STejun Heo 	int preempted_nr_active_links;
1691c6fd2807SJeff Garzik 	DECLARE_COMPLETION_ONSTACK(wait);
1692c6fd2807SJeff Garzik 	unsigned long flags;
1693c6fd2807SJeff Garzik 	unsigned int err_mask;
1694c6fd2807SJeff Garzik 	int rc;
1695c6fd2807SJeff Garzik 
1696c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1697c6fd2807SJeff Garzik 
1698c6fd2807SJeff Garzik 	/* no internal command while frozen */
1699c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_FROZEN) {
1700c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
1701c6fd2807SJeff Garzik 		return AC_ERR_SYSTEM;
1702c6fd2807SJeff Garzik 	}
1703c6fd2807SJeff Garzik 
1704c6fd2807SJeff Garzik 	/* initialize internal qc */
1705c6fd2807SJeff Garzik 
1706c6fd2807SJeff Garzik 	/* XXX: Tag 0 is used for drivers with legacy EH as some
1707c6fd2807SJeff Garzik 	 * drivers choke if any other tag is given.  This breaks
1708c6fd2807SJeff Garzik 	 * ata_tag_internal() test for those drivers.  Don't use new
1709c6fd2807SJeff Garzik 	 * EH stuff without converting to it.
1710c6fd2807SJeff Garzik 	 */
1711c6fd2807SJeff Garzik 	if (ap->ops->error_handler)
1712c6fd2807SJeff Garzik 		tag = ATA_TAG_INTERNAL;
1713c6fd2807SJeff Garzik 	else
1714c6fd2807SJeff Garzik 		tag = 0;
1715c6fd2807SJeff Garzik 
1716c6fd2807SJeff Garzik 	qc = __ata_qc_from_tag(ap, tag);
1717c6fd2807SJeff Garzik 
1718c6fd2807SJeff Garzik 	qc->tag = tag;
1719c6fd2807SJeff Garzik 	qc->scsicmd = NULL;
1720c6fd2807SJeff Garzik 	qc->ap = ap;
1721c6fd2807SJeff Garzik 	qc->dev = dev;
1722c6fd2807SJeff Garzik 	ata_qc_reinit(qc);
1723c6fd2807SJeff Garzik 
17249af5c9c9STejun Heo 	preempted_tag = link->active_tag;
17259af5c9c9STejun Heo 	preempted_sactive = link->sactive;
1726c6fd2807SJeff Garzik 	preempted_qc_active = ap->qc_active;
1727da917d69STejun Heo 	preempted_nr_active_links = ap->nr_active_links;
17289af5c9c9STejun Heo 	link->active_tag = ATA_TAG_POISON;
17299af5c9c9STejun Heo 	link->sactive = 0;
1730c6fd2807SJeff Garzik 	ap->qc_active = 0;
1731da917d69STejun Heo 	ap->nr_active_links = 0;
1732c6fd2807SJeff Garzik 
1733c6fd2807SJeff Garzik 	/* prepare & issue qc */
1734c6fd2807SJeff Garzik 	qc->tf = *tf;
1735c6fd2807SJeff Garzik 	if (cdb)
1736c6fd2807SJeff Garzik 		memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
1737c6fd2807SJeff Garzik 	qc->flags |= ATA_QCFLAG_RESULT_TF;
1738c6fd2807SJeff Garzik 	qc->dma_dir = dma_dir;
1739c6fd2807SJeff Garzik 	if (dma_dir != DMA_NONE) {
17402432697bSTejun Heo 		unsigned int i, buflen = 0;
174187260216SJens Axboe 		struct scatterlist *sg;
17422432697bSTejun Heo 
174387260216SJens Axboe 		for_each_sg(sgl, sg, n_elem, i)
174487260216SJens Axboe 			buflen += sg->length;
17452432697bSTejun Heo 
174687260216SJens Axboe 		ata_sg_init(qc, sgl, n_elem);
174749c80429SBrian King 		qc->nbytes = buflen;
1748c6fd2807SJeff Garzik 	}
1749c6fd2807SJeff Garzik 
1750c6fd2807SJeff Garzik 	qc->private_data = &wait;
1751c6fd2807SJeff Garzik 	qc->complete_fn = ata_qc_complete_internal;
1752c6fd2807SJeff Garzik 
1753c6fd2807SJeff Garzik 	ata_qc_issue(qc);
1754c6fd2807SJeff Garzik 
1755c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1756c6fd2807SJeff Garzik 
175787fbc5a0STejun Heo 	if (!timeout) {
175887fbc5a0STejun Heo 		if (ata_probe_timeout)
1759341c2c95STejun Heo 			timeout = ata_probe_timeout * 1000;
176087fbc5a0STejun Heo 		else {
176187fbc5a0STejun Heo 			timeout = ata_internal_cmd_timeout(dev, command);
176287fbc5a0STejun Heo 			auto_timeout = 1;
176387fbc5a0STejun Heo 		}
176487fbc5a0STejun Heo 	}
17652b789108STejun Heo 
17662b789108STejun Heo 	rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
1767c6fd2807SJeff Garzik 
1768c6fd2807SJeff Garzik 	ata_port_flush_task(ap);
1769c6fd2807SJeff Garzik 
1770c6fd2807SJeff Garzik 	if (!rc) {
1771c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
1772c6fd2807SJeff Garzik 
1773c6fd2807SJeff Garzik 		/* We're racing with irq here.  If we lose, the
1774c6fd2807SJeff Garzik 		 * following test prevents us from completing the qc
1775c6fd2807SJeff Garzik 		 * twice.  If we win, the port is frozen and will be
1776c6fd2807SJeff Garzik 		 * cleaned up by ->post_internal_cmd().
1777c6fd2807SJeff Garzik 		 */
1778c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_ACTIVE) {
1779c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_TIMEOUT;
1780c6fd2807SJeff Garzik 
1781c6fd2807SJeff Garzik 			if (ap->ops->error_handler)
1782c6fd2807SJeff Garzik 				ata_port_freeze(ap);
1783c6fd2807SJeff Garzik 			else
1784c6fd2807SJeff Garzik 				ata_qc_complete(qc);
1785c6fd2807SJeff Garzik 
1786c6fd2807SJeff Garzik 			if (ata_msg_warn(ap))
1787c6fd2807SJeff Garzik 				ata_dev_printk(dev, KERN_WARNING,
1788c6fd2807SJeff Garzik 					"qc timeout (cmd 0x%x)\n", command);
1789c6fd2807SJeff Garzik 		}
1790c6fd2807SJeff Garzik 
1791c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
1792c6fd2807SJeff Garzik 	}
1793c6fd2807SJeff Garzik 
1794c6fd2807SJeff Garzik 	/* do post_internal_cmd */
1795c6fd2807SJeff Garzik 	if (ap->ops->post_internal_cmd)
1796c6fd2807SJeff Garzik 		ap->ops->post_internal_cmd(qc);
1797c6fd2807SJeff Garzik 
1798a51d644aSTejun Heo 	/* perform minimal error analysis */
1799a51d644aSTejun Heo 	if (qc->flags & ATA_QCFLAG_FAILED) {
1800a51d644aSTejun Heo 		if (qc->result_tf.command & (ATA_ERR | ATA_DF))
1801a51d644aSTejun Heo 			qc->err_mask |= AC_ERR_DEV;
1802a51d644aSTejun Heo 
1803a51d644aSTejun Heo 		if (!qc->err_mask)
1804c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_OTHER;
1805a51d644aSTejun Heo 
1806a51d644aSTejun Heo 		if (qc->err_mask & ~AC_ERR_OTHER)
1807a51d644aSTejun Heo 			qc->err_mask &= ~AC_ERR_OTHER;
1808c6fd2807SJeff Garzik 	}
1809c6fd2807SJeff Garzik 
1810c6fd2807SJeff Garzik 	/* finish up */
1811c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1812c6fd2807SJeff Garzik 
1813c6fd2807SJeff Garzik 	*tf = qc->result_tf;
1814c6fd2807SJeff Garzik 	err_mask = qc->err_mask;
1815c6fd2807SJeff Garzik 
1816c6fd2807SJeff Garzik 	ata_qc_free(qc);
18179af5c9c9STejun Heo 	link->active_tag = preempted_tag;
18189af5c9c9STejun Heo 	link->sactive = preempted_sactive;
1819c6fd2807SJeff Garzik 	ap->qc_active = preempted_qc_active;
1820da917d69STejun Heo 	ap->nr_active_links = preempted_nr_active_links;
1821c6fd2807SJeff Garzik 
1822c6fd2807SJeff Garzik 	/* XXX - Some LLDDs (sata_mv) disable port on command failure.
1823c6fd2807SJeff Garzik 	 * Until those drivers are fixed, we detect the condition
1824c6fd2807SJeff Garzik 	 * here, fail the command with AC_ERR_SYSTEM and reenable the
1825c6fd2807SJeff Garzik 	 * port.
1826c6fd2807SJeff Garzik 	 *
1827c6fd2807SJeff Garzik 	 * Note that this doesn't change any behavior as internal
1828c6fd2807SJeff Garzik 	 * command failure results in disabling the device in the
1829c6fd2807SJeff Garzik 	 * higher layer for LLDDs without new reset/EH callbacks.
1830c6fd2807SJeff Garzik 	 *
1831c6fd2807SJeff Garzik 	 * Kill the following code as soon as those drivers are fixed.
1832c6fd2807SJeff Garzik 	 */
1833c6fd2807SJeff Garzik 	if (ap->flags & ATA_FLAG_DISABLED) {
1834c6fd2807SJeff Garzik 		err_mask |= AC_ERR_SYSTEM;
1835c6fd2807SJeff Garzik 		ata_port_probe(ap);
1836c6fd2807SJeff Garzik 	}
1837c6fd2807SJeff Garzik 
1838c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1839c6fd2807SJeff Garzik 
184087fbc5a0STejun Heo 	if ((err_mask & AC_ERR_TIMEOUT) && auto_timeout)
184187fbc5a0STejun Heo 		ata_internal_cmd_timed_out(dev, command);
184287fbc5a0STejun Heo 
1843c6fd2807SJeff Garzik 	return err_mask;
1844c6fd2807SJeff Garzik }
1845c6fd2807SJeff Garzik 
1846c6fd2807SJeff Garzik /**
184733480a0eSTejun Heo  *	ata_exec_internal - execute libata internal command
18482432697bSTejun Heo  *	@dev: Device to which the command is sent
18492432697bSTejun Heo  *	@tf: Taskfile registers for the command and the result
18502432697bSTejun Heo  *	@cdb: CDB for packet command
18512432697bSTejun Heo  *	@dma_dir: Data tranfer direction of the command
18522432697bSTejun Heo  *	@buf: Data buffer of the command
18532432697bSTejun Heo  *	@buflen: Length of data buffer
18542b789108STejun Heo  *	@timeout: Timeout in msecs (0 for default)
18552432697bSTejun Heo  *
18562432697bSTejun Heo  *	Wrapper around ata_exec_internal_sg() which takes simple
18572432697bSTejun Heo  *	buffer instead of sg list.
18582432697bSTejun Heo  *
18592432697bSTejun Heo  *	LOCKING:
18602432697bSTejun Heo  *	None.  Should be called with kernel context, might sleep.
18612432697bSTejun Heo  *
18622432697bSTejun Heo  *	RETURNS:
18632432697bSTejun Heo  *	Zero on success, AC_ERR_* mask on failure
18642432697bSTejun Heo  */
18652432697bSTejun Heo unsigned ata_exec_internal(struct ata_device *dev,
18662432697bSTejun Heo 			   struct ata_taskfile *tf, const u8 *cdb,
18672b789108STejun Heo 			   int dma_dir, void *buf, unsigned int buflen,
18682b789108STejun Heo 			   unsigned long timeout)
18692432697bSTejun Heo {
187033480a0eSTejun Heo 	struct scatterlist *psg = NULL, sg;
187133480a0eSTejun Heo 	unsigned int n_elem = 0;
18722432697bSTejun Heo 
187333480a0eSTejun Heo 	if (dma_dir != DMA_NONE) {
187433480a0eSTejun Heo 		WARN_ON(!buf);
18752432697bSTejun Heo 		sg_init_one(&sg, buf, buflen);
187633480a0eSTejun Heo 		psg = &sg;
187733480a0eSTejun Heo 		n_elem++;
187833480a0eSTejun Heo 	}
18792432697bSTejun Heo 
18802b789108STejun Heo 	return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem,
18812b789108STejun Heo 				    timeout);
18822432697bSTejun Heo }
18832432697bSTejun Heo 
18842432697bSTejun Heo /**
1885c6fd2807SJeff Garzik  *	ata_do_simple_cmd - execute simple internal command
1886c6fd2807SJeff Garzik  *	@dev: Device to which the command is sent
1887c6fd2807SJeff Garzik  *	@cmd: Opcode to execute
1888c6fd2807SJeff Garzik  *
1889c6fd2807SJeff Garzik  *	Execute a 'simple' command, that only consists of the opcode
1890c6fd2807SJeff Garzik  *	'cmd' itself, without filling any other registers
1891c6fd2807SJeff Garzik  *
1892c6fd2807SJeff Garzik  *	LOCKING:
1893c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1894c6fd2807SJeff Garzik  *
1895c6fd2807SJeff Garzik  *	RETURNS:
1896c6fd2807SJeff Garzik  *	Zero on success, AC_ERR_* mask on failure
1897c6fd2807SJeff Garzik  */
1898c6fd2807SJeff Garzik unsigned int ata_do_simple_cmd(struct ata_device *dev, u8 cmd)
1899c6fd2807SJeff Garzik {
1900c6fd2807SJeff Garzik 	struct ata_taskfile tf;
1901c6fd2807SJeff Garzik 
1902c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
1903c6fd2807SJeff Garzik 
1904c6fd2807SJeff Garzik 	tf.command = cmd;
1905c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_DEVICE;
1906c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
1907c6fd2807SJeff Garzik 
19082b789108STejun Heo 	return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1909c6fd2807SJeff Garzik }
1910c6fd2807SJeff Garzik 
1911c6fd2807SJeff Garzik /**
1912c6fd2807SJeff Garzik  *	ata_pio_need_iordy	-	check if iordy needed
1913c6fd2807SJeff Garzik  *	@adev: ATA device
1914c6fd2807SJeff Garzik  *
1915c6fd2807SJeff Garzik  *	Check if the current speed of the device requires IORDY. Used
1916c6fd2807SJeff Garzik  *	by various controllers for chip configuration.
1917c6fd2807SJeff Garzik  */
1918c6fd2807SJeff Garzik 
1919c6fd2807SJeff Garzik unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1920c6fd2807SJeff Garzik {
1921432729f0SAlan Cox 	/* Controller doesn't support  IORDY. Probably a pointless check
1922432729f0SAlan Cox 	   as the caller should know this */
19239af5c9c9STejun Heo 	if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
1924c6fd2807SJeff Garzik 		return 0;
1925432729f0SAlan Cox 	/* PIO3 and higher it is mandatory */
1926432729f0SAlan Cox 	if (adev->pio_mode > XFER_PIO_2)
1927c6fd2807SJeff Garzik 		return 1;
1928432729f0SAlan Cox 	/* We turn it on when possible */
1929432729f0SAlan Cox 	if (ata_id_has_iordy(adev->id))
1930432729f0SAlan Cox 		return 1;
1931432729f0SAlan Cox 	return 0;
1932432729f0SAlan Cox }
1933c6fd2807SJeff Garzik 
1934432729f0SAlan Cox /**
1935432729f0SAlan Cox  *	ata_pio_mask_no_iordy	-	Return the non IORDY mask
1936432729f0SAlan Cox  *	@adev: ATA device
1937432729f0SAlan Cox  *
1938432729f0SAlan Cox  *	Compute the highest mode possible if we are not using iordy. Return
1939432729f0SAlan Cox  *	-1 if no iordy mode is available.
1940432729f0SAlan Cox  */
1941432729f0SAlan Cox 
1942432729f0SAlan Cox static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
1943432729f0SAlan Cox {
1944c6fd2807SJeff Garzik 	/* If we have no drive specific rule, then PIO 2 is non IORDY */
1945c6fd2807SJeff Garzik 	if (adev->id[ATA_ID_FIELD_VALID] & 2) {	/* EIDE */
1946432729f0SAlan Cox 		u16 pio = adev->id[ATA_ID_EIDE_PIO];
1947c6fd2807SJeff Garzik 		/* Is the speed faster than the drive allows non IORDY ? */
1948c6fd2807SJeff Garzik 		if (pio) {
1949c6fd2807SJeff Garzik 			/* This is cycle times not frequency - watch the logic! */
1950c6fd2807SJeff Garzik 			if (pio > 240)	/* PIO2 is 240nS per cycle */
1951432729f0SAlan Cox 				return 3 << ATA_SHIFT_PIO;
1952432729f0SAlan Cox 			return 7 << ATA_SHIFT_PIO;
1953c6fd2807SJeff Garzik 		}
1954c6fd2807SJeff Garzik 	}
1955432729f0SAlan Cox 	return 3 << ATA_SHIFT_PIO;
1956c6fd2807SJeff Garzik }
1957c6fd2807SJeff Garzik 
1958c6fd2807SJeff Garzik /**
1959963e4975SAlan Cox  *	ata_do_dev_read_id		-	default ID read method
1960963e4975SAlan Cox  *	@dev: device
1961963e4975SAlan Cox  *	@tf: proposed taskfile
1962963e4975SAlan Cox  *	@id: data buffer
1963963e4975SAlan Cox  *
1964963e4975SAlan Cox  *	Issue the identify taskfile and hand back the buffer containing
1965963e4975SAlan Cox  *	identify data. For some RAID controllers and for pre ATA devices
1966963e4975SAlan Cox  *	this function is wrapped or replaced by the driver
1967963e4975SAlan Cox  */
1968963e4975SAlan Cox unsigned int ata_do_dev_read_id(struct ata_device *dev,
1969963e4975SAlan Cox 					struct ata_taskfile *tf, u16 *id)
1970963e4975SAlan Cox {
1971963e4975SAlan Cox 	return ata_exec_internal(dev, tf, NULL, DMA_FROM_DEVICE,
1972963e4975SAlan Cox 				     id, sizeof(id[0]) * ATA_ID_WORDS, 0);
1973963e4975SAlan Cox }
1974963e4975SAlan Cox 
1975963e4975SAlan Cox /**
1976c6fd2807SJeff Garzik  *	ata_dev_read_id - Read ID data from the specified device
1977c6fd2807SJeff Garzik  *	@dev: target device
1978c6fd2807SJeff Garzik  *	@p_class: pointer to class of the target device (may be changed)
1979bff04647STejun Heo  *	@flags: ATA_READID_* flags
1980c6fd2807SJeff Garzik  *	@id: buffer to read IDENTIFY data into
1981c6fd2807SJeff Garzik  *
1982c6fd2807SJeff Garzik  *	Read ID data from the specified device.  ATA_CMD_ID_ATA is
1983c6fd2807SJeff Garzik  *	performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
1984c6fd2807SJeff Garzik  *	devices.  This function also issues ATA_CMD_INIT_DEV_PARAMS
1985c6fd2807SJeff Garzik  *	for pre-ATA4 drives.
1986c6fd2807SJeff Garzik  *
198750a99018SAlan Cox  *	FIXME: ATA_CMD_ID_ATA is optional for early drives and right
198850a99018SAlan Cox  *	now we abort if we hit that case.
198950a99018SAlan Cox  *
1990c6fd2807SJeff Garzik  *	LOCKING:
1991c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
1992c6fd2807SJeff Garzik  *
1993c6fd2807SJeff Garzik  *	RETURNS:
1994c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
1995c6fd2807SJeff Garzik  */
1996c6fd2807SJeff Garzik int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
1997bff04647STejun Heo 		    unsigned int flags, u16 *id)
1998c6fd2807SJeff Garzik {
19999af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
2000c6fd2807SJeff Garzik 	unsigned int class = *p_class;
2001c6fd2807SJeff Garzik 	struct ata_taskfile tf;
2002c6fd2807SJeff Garzik 	unsigned int err_mask = 0;
2003c6fd2807SJeff Garzik 	const char *reason;
200454936f8bSTejun Heo 	int may_fallback = 1, tried_spinup = 0;
2005c6fd2807SJeff Garzik 	int rc;
2006c6fd2807SJeff Garzik 
2007c6fd2807SJeff Garzik 	if (ata_msg_ctl(ap))
20087f5e4e8dSHarvey Harrison 		ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __func__);
2009c6fd2807SJeff Garzik 
2010c6fd2807SJeff Garzik retry:
2011c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
2012c6fd2807SJeff Garzik 
2013c6fd2807SJeff Garzik 	switch (class) {
2014c6fd2807SJeff Garzik 	case ATA_DEV_ATA:
2015c6fd2807SJeff Garzik 		tf.command = ATA_CMD_ID_ATA;
2016c6fd2807SJeff Garzik 		break;
2017c6fd2807SJeff Garzik 	case ATA_DEV_ATAPI:
2018c6fd2807SJeff Garzik 		tf.command = ATA_CMD_ID_ATAPI;
2019c6fd2807SJeff Garzik 		break;
2020c6fd2807SJeff Garzik 	default:
2021c6fd2807SJeff Garzik 		rc = -ENODEV;
2022c6fd2807SJeff Garzik 		reason = "unsupported class";
2023c6fd2807SJeff Garzik 		goto err_out;
2024c6fd2807SJeff Garzik 	}
2025c6fd2807SJeff Garzik 
2026c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_PIO;
202781afe893STejun Heo 
202881afe893STejun Heo 	/* Some devices choke if TF registers contain garbage.  Make
202981afe893STejun Heo 	 * sure those are properly initialized.
203081afe893STejun Heo 	 */
203181afe893STejun Heo 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
203281afe893STejun Heo 
203381afe893STejun Heo 	/* Device presence detection is unreliable on some
203481afe893STejun Heo 	 * controllers.  Always poll IDENTIFY if available.
203581afe893STejun Heo 	 */
203681afe893STejun Heo 	tf.flags |= ATA_TFLAG_POLLING;
2037c6fd2807SJeff Garzik 
2038963e4975SAlan Cox 	if (ap->ops->read_id)
2039963e4975SAlan Cox 		err_mask = ap->ops->read_id(dev, &tf, id);
2040963e4975SAlan Cox 	else
2041963e4975SAlan Cox 		err_mask = ata_do_dev_read_id(dev, &tf, id);
2042963e4975SAlan Cox 
2043c6fd2807SJeff Garzik 	if (err_mask) {
2044800b3996STejun Heo 		if (err_mask & AC_ERR_NODEV_HINT) {
20451ffc151fSTejun Heo 			ata_dev_printk(dev, KERN_DEBUG,
20461ffc151fSTejun Heo 				       "NODEV after polling detection\n");
204755a8e2c8STejun Heo 			return -ENOENT;
204855a8e2c8STejun Heo 		}
204955a8e2c8STejun Heo 
20501ffc151fSTejun Heo 		if ((err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
20511ffc151fSTejun Heo 			/* Device or controller might have reported
20521ffc151fSTejun Heo 			 * the wrong device class.  Give a shot at the
20531ffc151fSTejun Heo 			 * other IDENTIFY if the current one is
20541ffc151fSTejun Heo 			 * aborted by the device.
205554936f8bSTejun Heo 			 */
20561ffc151fSTejun Heo 			if (may_fallback) {
205754936f8bSTejun Heo 				may_fallback = 0;
205854936f8bSTejun Heo 
205954936f8bSTejun Heo 				if (class == ATA_DEV_ATA)
206054936f8bSTejun Heo 					class = ATA_DEV_ATAPI;
206154936f8bSTejun Heo 				else
206254936f8bSTejun Heo 					class = ATA_DEV_ATA;
206354936f8bSTejun Heo 				goto retry;
206454936f8bSTejun Heo 			}
206554936f8bSTejun Heo 
20661ffc151fSTejun Heo 			/* Control reaches here iff the device aborted
20671ffc151fSTejun Heo 			 * both flavors of IDENTIFYs which happens
20681ffc151fSTejun Heo 			 * sometimes with phantom devices.
20691ffc151fSTejun Heo 			 */
20701ffc151fSTejun Heo 			ata_dev_printk(dev, KERN_DEBUG,
20711ffc151fSTejun Heo 				       "both IDENTIFYs aborted, assuming NODEV\n");
20721ffc151fSTejun Heo 			return -ENOENT;
20731ffc151fSTejun Heo 		}
20741ffc151fSTejun Heo 
2075c6fd2807SJeff Garzik 		rc = -EIO;
2076c6fd2807SJeff Garzik 		reason = "I/O error";
2077c6fd2807SJeff Garzik 		goto err_out;
2078c6fd2807SJeff Garzik 	}
2079c6fd2807SJeff Garzik 
208054936f8bSTejun Heo 	/* Falling back doesn't make sense if ID data was read
208154936f8bSTejun Heo 	 * successfully at least once.
208254936f8bSTejun Heo 	 */
208354936f8bSTejun Heo 	may_fallback = 0;
208454936f8bSTejun Heo 
2085c6fd2807SJeff Garzik 	swap_buf_le16(id, ATA_ID_WORDS);
2086c6fd2807SJeff Garzik 
2087c6fd2807SJeff Garzik 	/* sanity check */
2088c6fd2807SJeff Garzik 	rc = -EINVAL;
20896070068bSAlan Cox 	reason = "device reports invalid type";
20904a3381feSJeff Garzik 
20914a3381feSJeff Garzik 	if (class == ATA_DEV_ATA) {
20924a3381feSJeff Garzik 		if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
20934a3381feSJeff Garzik 			goto err_out;
20944a3381feSJeff Garzik 	} else {
20954a3381feSJeff Garzik 		if (ata_id_is_ata(id))
2096c6fd2807SJeff Garzik 			goto err_out;
2097c6fd2807SJeff Garzik 	}
2098c6fd2807SJeff Garzik 
2099169439c2SMark Lord 	if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
2100169439c2SMark Lord 		tried_spinup = 1;
2101169439c2SMark Lord 		/*
2102169439c2SMark Lord 		 * Drive powered-up in standby mode, and requires a specific
2103169439c2SMark Lord 		 * SET_FEATURES spin-up subcommand before it will accept
2104169439c2SMark Lord 		 * anything other than the original IDENTIFY command.
2105169439c2SMark Lord 		 */
2106218f3d30SJeff Garzik 		err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
2107fb0582f9SRyan Power 		if (err_mask && id[2] != 0x738c) {
2108169439c2SMark Lord 			rc = -EIO;
2109169439c2SMark Lord 			reason = "SPINUP failed";
2110169439c2SMark Lord 			goto err_out;
2111169439c2SMark Lord 		}
2112169439c2SMark Lord 		/*
2113169439c2SMark Lord 		 * If the drive initially returned incomplete IDENTIFY info,
2114169439c2SMark Lord 		 * we now must reissue the IDENTIFY command.
2115169439c2SMark Lord 		 */
2116169439c2SMark Lord 		if (id[2] == 0x37c8)
2117169439c2SMark Lord 			goto retry;
2118169439c2SMark Lord 	}
2119169439c2SMark Lord 
2120bff04647STejun Heo 	if ((flags & ATA_READID_POSTRESET) && class == ATA_DEV_ATA) {
2121c6fd2807SJeff Garzik 		/*
2122c6fd2807SJeff Garzik 		 * The exact sequence expected by certain pre-ATA4 drives is:
2123c6fd2807SJeff Garzik 		 * SRST RESET
212450a99018SAlan Cox 		 * IDENTIFY (optional in early ATA)
212550a99018SAlan Cox 		 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
2126c6fd2807SJeff Garzik 		 * anything else..
2127c6fd2807SJeff Garzik 		 * Some drives were very specific about that exact sequence.
212850a99018SAlan Cox 		 *
212950a99018SAlan Cox 		 * Note that ATA4 says lba is mandatory so the second check
213050a99018SAlan Cox 		 * shoud never trigger.
2131c6fd2807SJeff Garzik 		 */
2132c6fd2807SJeff Garzik 		if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
2133c6fd2807SJeff Garzik 			err_mask = ata_dev_init_params(dev, id[3], id[6]);
2134c6fd2807SJeff Garzik 			if (err_mask) {
2135c6fd2807SJeff Garzik 				rc = -EIO;
2136c6fd2807SJeff Garzik 				reason = "INIT_DEV_PARAMS failed";
2137c6fd2807SJeff Garzik 				goto err_out;
2138c6fd2807SJeff Garzik 			}
2139c6fd2807SJeff Garzik 
2140c6fd2807SJeff Garzik 			/* current CHS translation info (id[53-58]) might be
2141c6fd2807SJeff Garzik 			 * changed. reread the identify device info.
2142c6fd2807SJeff Garzik 			 */
2143bff04647STejun Heo 			flags &= ~ATA_READID_POSTRESET;
2144c6fd2807SJeff Garzik 			goto retry;
2145c6fd2807SJeff Garzik 		}
2146c6fd2807SJeff Garzik 	}
2147c6fd2807SJeff Garzik 
2148c6fd2807SJeff Garzik 	*p_class = class;
2149c6fd2807SJeff Garzik 
2150c6fd2807SJeff Garzik 	return 0;
2151c6fd2807SJeff Garzik 
2152c6fd2807SJeff Garzik  err_out:
2153c6fd2807SJeff Garzik 	if (ata_msg_warn(ap))
2154c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_WARNING, "failed to IDENTIFY "
2155c6fd2807SJeff Garzik 			       "(%s, err_mask=0x%x)\n", reason, err_mask);
2156c6fd2807SJeff Garzik 	return rc;
2157c6fd2807SJeff Garzik }
2158c6fd2807SJeff Garzik 
2159c6fd2807SJeff Garzik static inline u8 ata_dev_knobble(struct ata_device *dev)
2160c6fd2807SJeff Garzik {
21619af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
21629af5c9c9STejun Heo 	return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
2163c6fd2807SJeff Garzik }
2164c6fd2807SJeff Garzik 
2165c6fd2807SJeff Garzik static void ata_dev_config_ncq(struct ata_device *dev,
2166c6fd2807SJeff Garzik 			       char *desc, size_t desc_sz)
2167c6fd2807SJeff Garzik {
21689af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
2169c6fd2807SJeff Garzik 	int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
2170c6fd2807SJeff Garzik 
2171c6fd2807SJeff Garzik 	if (!ata_id_has_ncq(dev->id)) {
2172c6fd2807SJeff Garzik 		desc[0] = '\0';
2173c6fd2807SJeff Garzik 		return;
2174c6fd2807SJeff Garzik 	}
217575683fe7STejun Heo 	if (dev->horkage & ATA_HORKAGE_NONCQ) {
21766919a0a6SAlan Cox 		snprintf(desc, desc_sz, "NCQ (not used)");
21776919a0a6SAlan Cox 		return;
21786919a0a6SAlan Cox 	}
2179c6fd2807SJeff Garzik 	if (ap->flags & ATA_FLAG_NCQ) {
2180cca3974eSJeff Garzik 		hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE - 1);
2181c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_NCQ;
2182c6fd2807SJeff Garzik 	}
2183c6fd2807SJeff Garzik 
2184c6fd2807SJeff Garzik 	if (hdepth >= ddepth)
2185c6fd2807SJeff Garzik 		snprintf(desc, desc_sz, "NCQ (depth %d)", ddepth);
2186c6fd2807SJeff Garzik 	else
2187c6fd2807SJeff Garzik 		snprintf(desc, desc_sz, "NCQ (depth %d/%d)", hdepth, ddepth);
2188c6fd2807SJeff Garzik }
2189c6fd2807SJeff Garzik 
2190c6fd2807SJeff Garzik /**
2191c6fd2807SJeff Garzik  *	ata_dev_configure - Configure the specified ATA/ATAPI device
2192c6fd2807SJeff Garzik  *	@dev: Target device to configure
2193c6fd2807SJeff Garzik  *
2194c6fd2807SJeff Garzik  *	Configure @dev according to @dev->id.  Generic and low-level
2195c6fd2807SJeff Garzik  *	driver specific fixups are also applied.
2196c6fd2807SJeff Garzik  *
2197c6fd2807SJeff Garzik  *	LOCKING:
2198c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
2199c6fd2807SJeff Garzik  *
2200c6fd2807SJeff Garzik  *	RETURNS:
2201c6fd2807SJeff Garzik  *	0 on success, -errno otherwise
2202c6fd2807SJeff Garzik  */
2203efdaedc4STejun Heo int ata_dev_configure(struct ata_device *dev)
2204c6fd2807SJeff Garzik {
22059af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
22069af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
22076746544cSTejun Heo 	int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
2208c6fd2807SJeff Garzik 	const u16 *id = dev->id;
22097dc951aeSTejun Heo 	unsigned long xfer_mask;
2210b352e57dSAlan Cox 	char revbuf[7];		/* XYZ-99\0 */
22113f64f565SEric D. Mudama 	char fwrevbuf[ATA_ID_FW_REV_LEN+1];
22123f64f565SEric D. Mudama 	char modelbuf[ATA_ID_PROD_LEN+1];
2213c6fd2807SJeff Garzik 	int rc;
2214c6fd2807SJeff Garzik 
2215c6fd2807SJeff Garzik 	if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
221644877b4eSTejun Heo 		ata_dev_printk(dev, KERN_INFO, "%s: ENTER/EXIT -- nodev\n",
22177f5e4e8dSHarvey Harrison 			       __func__);
2218c6fd2807SJeff Garzik 		return 0;
2219c6fd2807SJeff Garzik 	}
2220c6fd2807SJeff Garzik 
2221c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
22227f5e4e8dSHarvey Harrison 		ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __func__);
2223c6fd2807SJeff Garzik 
222475683fe7STejun Heo 	/* set horkage */
222575683fe7STejun Heo 	dev->horkage |= ata_dev_blacklisted(dev);
222633267325STejun Heo 	ata_force_horkage(dev);
222775683fe7STejun Heo 
222850af2fa1STejun Heo 	if (dev->horkage & ATA_HORKAGE_DISABLE) {
222950af2fa1STejun Heo 		ata_dev_printk(dev, KERN_INFO,
223050af2fa1STejun Heo 			       "unsupported device, disabling\n");
223150af2fa1STejun Heo 		ata_dev_disable(dev);
223250af2fa1STejun Heo 		return 0;
223350af2fa1STejun Heo 	}
223450af2fa1STejun Heo 
22352486fa56STejun Heo 	if ((!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) &&
22362486fa56STejun Heo 	    dev->class == ATA_DEV_ATAPI) {
22372486fa56STejun Heo 		ata_dev_printk(dev, KERN_WARNING,
22382486fa56STejun Heo 			"WARNING: ATAPI is %s, device ignored.\n",
22392486fa56STejun Heo 			atapi_enabled ? "not supported with this driver"
22402486fa56STejun Heo 				      : "disabled");
22412486fa56STejun Heo 		ata_dev_disable(dev);
22422486fa56STejun Heo 		return 0;
22432486fa56STejun Heo 	}
22442486fa56STejun Heo 
22456746544cSTejun Heo 	/* let ACPI work its magic */
22466746544cSTejun Heo 	rc = ata_acpi_on_devcfg(dev);
22476746544cSTejun Heo 	if (rc)
22486746544cSTejun Heo 		return rc;
224908573a86SKristen Carlson Accardi 
225005027adcSTejun Heo 	/* massage HPA, do it early as it might change IDENTIFY data */
225105027adcSTejun Heo 	rc = ata_hpa_resize(dev);
225205027adcSTejun Heo 	if (rc)
225305027adcSTejun Heo 		return rc;
225405027adcSTejun Heo 
2255c6fd2807SJeff Garzik 	/* print device capabilities */
2256c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2257c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_DEBUG,
2258c6fd2807SJeff Garzik 			       "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
2259c6fd2807SJeff Garzik 			       "85:%04x 86:%04x 87:%04x 88:%04x\n",
22607f5e4e8dSHarvey Harrison 			       __func__,
2261c6fd2807SJeff Garzik 			       id[49], id[82], id[83], id[84],
2262c6fd2807SJeff Garzik 			       id[85], id[86], id[87], id[88]);
2263c6fd2807SJeff Garzik 
2264c6fd2807SJeff Garzik 	/* initialize to-be-configured parameters */
2265c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_CFG_MASK;
2266c6fd2807SJeff Garzik 	dev->max_sectors = 0;
2267c6fd2807SJeff Garzik 	dev->cdb_len = 0;
2268c6fd2807SJeff Garzik 	dev->n_sectors = 0;
2269c6fd2807SJeff Garzik 	dev->cylinders = 0;
2270c6fd2807SJeff Garzik 	dev->heads = 0;
2271c6fd2807SJeff Garzik 	dev->sectors = 0;
2272c6fd2807SJeff Garzik 
2273c6fd2807SJeff Garzik 	/*
2274c6fd2807SJeff Garzik 	 * common ATA, ATAPI feature tests
2275c6fd2807SJeff Garzik 	 */
2276c6fd2807SJeff Garzik 
2277c6fd2807SJeff Garzik 	/* find max transfer mode; for printk only */
2278c6fd2807SJeff Garzik 	xfer_mask = ata_id_xfermask(id);
2279c6fd2807SJeff Garzik 
2280c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2281c6fd2807SJeff Garzik 		ata_dump_id(id);
2282c6fd2807SJeff Garzik 
2283ef143d57SAlbert Lee 	/* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
2284ef143d57SAlbert Lee 	ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
2285ef143d57SAlbert Lee 			sizeof(fwrevbuf));
2286ef143d57SAlbert Lee 
2287ef143d57SAlbert Lee 	ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
2288ef143d57SAlbert Lee 			sizeof(modelbuf));
2289ef143d57SAlbert Lee 
2290c6fd2807SJeff Garzik 	/* ATA-specific feature tests */
2291c6fd2807SJeff Garzik 	if (dev->class == ATA_DEV_ATA) {
2292b352e57dSAlan Cox 		if (ata_id_is_cfa(id)) {
2293b352e57dSAlan Cox 			if (id[162] & 1) /* CPRM may make this media unusable */
229444877b4eSTejun Heo 				ata_dev_printk(dev, KERN_WARNING,
229544877b4eSTejun Heo 					       "supports DRM functions and may "
229644877b4eSTejun Heo 					       "not be fully accessable.\n");
2297b352e57dSAlan Cox 			snprintf(revbuf, 7, "CFA");
2298ae8d4ee7SAlan Cox 		} else {
2299b352e57dSAlan Cox 			snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
2300ae8d4ee7SAlan Cox 			/* Warn the user if the device has TPM extensions */
2301ae8d4ee7SAlan Cox 			if (ata_id_has_tpm(id))
2302ae8d4ee7SAlan Cox 				ata_dev_printk(dev, KERN_WARNING,
2303ae8d4ee7SAlan Cox 					       "supports DRM functions and may "
2304ae8d4ee7SAlan Cox 					       "not be fully accessable.\n");
2305ae8d4ee7SAlan Cox 		}
2306b352e57dSAlan Cox 
2307c6fd2807SJeff Garzik 		dev->n_sectors = ata_id_n_sectors(id);
2308c6fd2807SJeff Garzik 
23093f64f565SEric D. Mudama 		if (dev->id[59] & 0x100)
23103f64f565SEric D. Mudama 			dev->multi_count = dev->id[59] & 0xff;
23113f64f565SEric D. Mudama 
2312c6fd2807SJeff Garzik 		if (ata_id_has_lba(id)) {
2313c6fd2807SJeff Garzik 			const char *lba_desc;
2314c6fd2807SJeff Garzik 			char ncq_desc[20];
2315c6fd2807SJeff Garzik 
2316c6fd2807SJeff Garzik 			lba_desc = "LBA";
2317c6fd2807SJeff Garzik 			dev->flags |= ATA_DFLAG_LBA;
2318c6fd2807SJeff Garzik 			if (ata_id_has_lba48(id)) {
2319c6fd2807SJeff Garzik 				dev->flags |= ATA_DFLAG_LBA48;
2320c6fd2807SJeff Garzik 				lba_desc = "LBA48";
23216fc49adbSTejun Heo 
23226fc49adbSTejun Heo 				if (dev->n_sectors >= (1UL << 28) &&
23236fc49adbSTejun Heo 				    ata_id_has_flush_ext(id))
23246fc49adbSTejun Heo 					dev->flags |= ATA_DFLAG_FLUSH_EXT;
2325c6fd2807SJeff Garzik 			}
2326c6fd2807SJeff Garzik 
2327c6fd2807SJeff Garzik 			/* config NCQ */
2328c6fd2807SJeff Garzik 			ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
2329c6fd2807SJeff Garzik 
2330c6fd2807SJeff Garzik 			/* print device info to dmesg */
23313f64f565SEric D. Mudama 			if (ata_msg_drv(ap) && print_info) {
23323f64f565SEric D. Mudama 				ata_dev_printk(dev, KERN_INFO,
23333f64f565SEric D. Mudama 					"%s: %s, %s, max %s\n",
23343f64f565SEric D. Mudama 					revbuf, modelbuf, fwrevbuf,
23353f64f565SEric D. Mudama 					ata_mode_string(xfer_mask));
23363f64f565SEric D. Mudama 				ata_dev_printk(dev, KERN_INFO,
23373f64f565SEric D. Mudama 					"%Lu sectors, multi %u: %s %s\n",
2338c6fd2807SJeff Garzik 					(unsigned long long)dev->n_sectors,
23393f64f565SEric D. Mudama 					dev->multi_count, lba_desc, ncq_desc);
23403f64f565SEric D. Mudama 			}
2341c6fd2807SJeff Garzik 		} else {
2342c6fd2807SJeff Garzik 			/* CHS */
2343c6fd2807SJeff Garzik 
2344c6fd2807SJeff Garzik 			/* Default translation */
2345c6fd2807SJeff Garzik 			dev->cylinders	= id[1];
2346c6fd2807SJeff Garzik 			dev->heads	= id[3];
2347c6fd2807SJeff Garzik 			dev->sectors	= id[6];
2348c6fd2807SJeff Garzik 
2349c6fd2807SJeff Garzik 			if (ata_id_current_chs_valid(id)) {
2350c6fd2807SJeff Garzik 				/* Current CHS translation is valid. */
2351c6fd2807SJeff Garzik 				dev->cylinders = id[54];
2352c6fd2807SJeff Garzik 				dev->heads     = id[55];
2353c6fd2807SJeff Garzik 				dev->sectors   = id[56];
2354c6fd2807SJeff Garzik 			}
2355c6fd2807SJeff Garzik 
2356c6fd2807SJeff Garzik 			/* print device info to dmesg */
23573f64f565SEric D. Mudama 			if (ata_msg_drv(ap) && print_info) {
2358c6fd2807SJeff Garzik 				ata_dev_printk(dev, KERN_INFO,
23593f64f565SEric D. Mudama 					"%s: %s, %s, max %s\n",
23603f64f565SEric D. Mudama 					revbuf,	modelbuf, fwrevbuf,
23613f64f565SEric D. Mudama 					ata_mode_string(xfer_mask));
23623f64f565SEric D. Mudama 				ata_dev_printk(dev, KERN_INFO,
23633f64f565SEric D. Mudama 					"%Lu sectors, multi %u, CHS %u/%u/%u\n",
23643f64f565SEric D. Mudama 					(unsigned long long)dev->n_sectors,
23653f64f565SEric D. Mudama 					dev->multi_count, dev->cylinders,
23663f64f565SEric D. Mudama 					dev->heads, dev->sectors);
23673f64f565SEric D. Mudama 			}
2368c6fd2807SJeff Garzik 		}
2369c6fd2807SJeff Garzik 
2370c6fd2807SJeff Garzik 		dev->cdb_len = 16;
2371c6fd2807SJeff Garzik 	}
2372c6fd2807SJeff Garzik 
2373c6fd2807SJeff Garzik 	/* ATAPI-specific feature tests */
2374c6fd2807SJeff Garzik 	else if (dev->class == ATA_DEV_ATAPI) {
2375854c73a2STejun Heo 		const char *cdb_intr_string = "";
2376854c73a2STejun Heo 		const char *atapi_an_string = "";
237791163006STejun Heo 		const char *dma_dir_string = "";
23787d77b247STejun Heo 		u32 sntf;
2379c6fd2807SJeff Garzik 
2380c6fd2807SJeff Garzik 		rc = atapi_cdb_len(id);
2381c6fd2807SJeff Garzik 		if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
2382c6fd2807SJeff Garzik 			if (ata_msg_warn(ap))
2383c6fd2807SJeff Garzik 				ata_dev_printk(dev, KERN_WARNING,
2384c6fd2807SJeff Garzik 					       "unsupported CDB len\n");
2385c6fd2807SJeff Garzik 			rc = -EINVAL;
2386c6fd2807SJeff Garzik 			goto err_out_nosup;
2387c6fd2807SJeff Garzik 		}
2388c6fd2807SJeff Garzik 		dev->cdb_len = (unsigned int) rc;
2389c6fd2807SJeff Garzik 
23907d77b247STejun Heo 		/* Enable ATAPI AN if both the host and device have
23917d77b247STejun Heo 		 * the support.  If PMP is attached, SNTF is required
23927d77b247STejun Heo 		 * to enable ATAPI AN to discern between PHY status
23937d77b247STejun Heo 		 * changed notifications and ATAPI ANs.
23949f45cbd3SKristen Carlson Accardi 		 */
23957d77b247STejun Heo 		if ((ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
2396071f44b1STejun Heo 		    (!sata_pmp_attached(ap) ||
23977d77b247STejun Heo 		     sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
2398854c73a2STejun Heo 			unsigned int err_mask;
2399854c73a2STejun Heo 
24009f45cbd3SKristen Carlson Accardi 			/* issue SET feature command to turn this on */
2401218f3d30SJeff Garzik 			err_mask = ata_dev_set_feature(dev,
2402218f3d30SJeff Garzik 					SETFEATURES_SATA_ENABLE, SATA_AN);
2403854c73a2STejun Heo 			if (err_mask)
24049f45cbd3SKristen Carlson Accardi 				ata_dev_printk(dev, KERN_ERR,
2405854c73a2STejun Heo 					"failed to enable ATAPI AN "
2406854c73a2STejun Heo 					"(err_mask=0x%x)\n", err_mask);
2407854c73a2STejun Heo 			else {
24089f45cbd3SKristen Carlson Accardi 				dev->flags |= ATA_DFLAG_AN;
2409854c73a2STejun Heo 				atapi_an_string = ", ATAPI AN";
2410854c73a2STejun Heo 			}
24119f45cbd3SKristen Carlson Accardi 		}
24129f45cbd3SKristen Carlson Accardi 
2413c6fd2807SJeff Garzik 		if (ata_id_cdb_intr(dev->id)) {
2414c6fd2807SJeff Garzik 			dev->flags |= ATA_DFLAG_CDB_INTR;
2415c6fd2807SJeff Garzik 			cdb_intr_string = ", CDB intr";
2416c6fd2807SJeff Garzik 		}
2417c6fd2807SJeff Garzik 
241891163006STejun Heo 		if (atapi_dmadir || atapi_id_dmadir(dev->id)) {
241991163006STejun Heo 			dev->flags |= ATA_DFLAG_DMADIR;
242091163006STejun Heo 			dma_dir_string = ", DMADIR";
242191163006STejun Heo 		}
242291163006STejun Heo 
2423c6fd2807SJeff Garzik 		/* print device info to dmesg */
2424c6fd2807SJeff Garzik 		if (ata_msg_drv(ap) && print_info)
2425ef143d57SAlbert Lee 			ata_dev_printk(dev, KERN_INFO,
242691163006STejun Heo 				       "ATAPI: %s, %s, max %s%s%s%s\n",
2427ef143d57SAlbert Lee 				       modelbuf, fwrevbuf,
2428c6fd2807SJeff Garzik 				       ata_mode_string(xfer_mask),
242991163006STejun Heo 				       cdb_intr_string, atapi_an_string,
243091163006STejun Heo 				       dma_dir_string);
2431c6fd2807SJeff Garzik 	}
2432c6fd2807SJeff Garzik 
2433914ed354STejun Heo 	/* determine max_sectors */
2434914ed354STejun Heo 	dev->max_sectors = ATA_MAX_SECTORS;
2435914ed354STejun Heo 	if (dev->flags & ATA_DFLAG_LBA48)
2436914ed354STejun Heo 		dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2437914ed354STejun Heo 
2438ca77329fSKristen Carlson Accardi 	if (!(dev->horkage & ATA_HORKAGE_IPM)) {
2439ca77329fSKristen Carlson Accardi 		if (ata_id_has_hipm(dev->id))
2440ca77329fSKristen Carlson Accardi 			dev->flags |= ATA_DFLAG_HIPM;
2441ca77329fSKristen Carlson Accardi 		if (ata_id_has_dipm(dev->id))
2442ca77329fSKristen Carlson Accardi 			dev->flags |= ATA_DFLAG_DIPM;
2443ca77329fSKristen Carlson Accardi 	}
2444ca77329fSKristen Carlson Accardi 
2445c5038fc0SAlan Cox 	/* Limit PATA drive on SATA cable bridge transfers to udma5,
2446c5038fc0SAlan Cox 	   200 sectors */
2447c6fd2807SJeff Garzik 	if (ata_dev_knobble(dev)) {
2448c6fd2807SJeff Garzik 		if (ata_msg_drv(ap) && print_info)
2449c6fd2807SJeff Garzik 			ata_dev_printk(dev, KERN_INFO,
2450c6fd2807SJeff Garzik 				       "applying bridge limits\n");
2451c6fd2807SJeff Garzik 		dev->udma_mask &= ATA_UDMA5;
2452c6fd2807SJeff Garzik 		dev->max_sectors = ATA_MAX_SECTORS;
2453c6fd2807SJeff Garzik 	}
2454c6fd2807SJeff Garzik 
2455f8d8e579STony Battersby 	if ((dev->class == ATA_DEV_ATAPI) &&
2456f442cd86SAlbert Lee 	    (atapi_command_packet_set(id) == TYPE_TAPE)) {
2457f8d8e579STony Battersby 		dev->max_sectors = ATA_MAX_SECTORS_TAPE;
2458f442cd86SAlbert Lee 		dev->horkage |= ATA_HORKAGE_STUCK_ERR;
2459f442cd86SAlbert Lee 	}
2460f8d8e579STony Battersby 
246175683fe7STejun Heo 	if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
246203ec52deSTejun Heo 		dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
246303ec52deSTejun Heo 					 dev->max_sectors);
246418d6e9d5SAlbert Lee 
2465ca77329fSKristen Carlson Accardi 	if (ata_dev_blacklisted(dev) & ATA_HORKAGE_IPM) {
2466ca77329fSKristen Carlson Accardi 		dev->horkage |= ATA_HORKAGE_IPM;
2467ca77329fSKristen Carlson Accardi 
2468ca77329fSKristen Carlson Accardi 		/* reset link pm_policy for this port to no pm */
2469ca77329fSKristen Carlson Accardi 		ap->pm_policy = MAX_PERFORMANCE;
2470ca77329fSKristen Carlson Accardi 	}
2471ca77329fSKristen Carlson Accardi 
2472c6fd2807SJeff Garzik 	if (ap->ops->dev_config)
2473cd0d3bbcSAlan 		ap->ops->dev_config(dev);
2474c6fd2807SJeff Garzik 
2475c5038fc0SAlan Cox 	if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2476c5038fc0SAlan Cox 		/* Let the user know. We don't want to disallow opens for
2477c5038fc0SAlan Cox 		   rescue purposes, or in case the vendor is just a blithering
2478c5038fc0SAlan Cox 		   idiot. Do this after the dev_config call as some controllers
2479c5038fc0SAlan Cox 		   with buggy firmware may want to avoid reporting false device
2480c5038fc0SAlan Cox 		   bugs */
2481c5038fc0SAlan Cox 
2482c5038fc0SAlan Cox 		if (print_info) {
2483c5038fc0SAlan Cox 			ata_dev_printk(dev, KERN_WARNING,
2484c5038fc0SAlan Cox "Drive reports diagnostics failure. This may indicate a drive\n");
2485c5038fc0SAlan Cox 			ata_dev_printk(dev, KERN_WARNING,
2486c5038fc0SAlan Cox "fault or invalid emulation. Contact drive vendor for information.\n");
2487c5038fc0SAlan Cox 		}
2488c5038fc0SAlan Cox 	}
2489c5038fc0SAlan Cox 
2490c6fd2807SJeff Garzik 	return 0;
2491c6fd2807SJeff Garzik 
2492c6fd2807SJeff Garzik err_out_nosup:
2493c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2494c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_DEBUG,
24957f5e4e8dSHarvey Harrison 			       "%s: EXIT, err\n", __func__);
2496c6fd2807SJeff Garzik 	return rc;
2497c6fd2807SJeff Garzik }
2498c6fd2807SJeff Garzik 
2499c6fd2807SJeff Garzik /**
25002e41e8e6SAlan Cox  *	ata_cable_40wire	-	return 40 wire cable type
2501be0d18dfSAlan Cox  *	@ap: port
2502be0d18dfSAlan Cox  *
25032e41e8e6SAlan Cox  *	Helper method for drivers which want to hardwire 40 wire cable
2504be0d18dfSAlan Cox  *	detection.
2505be0d18dfSAlan Cox  */
2506be0d18dfSAlan Cox 
2507be0d18dfSAlan Cox int ata_cable_40wire(struct ata_port *ap)
2508be0d18dfSAlan Cox {
2509be0d18dfSAlan Cox 	return ATA_CBL_PATA40;
2510be0d18dfSAlan Cox }
2511be0d18dfSAlan Cox 
2512be0d18dfSAlan Cox /**
25132e41e8e6SAlan Cox  *	ata_cable_80wire	-	return 80 wire cable type
2514be0d18dfSAlan Cox  *	@ap: port
2515be0d18dfSAlan Cox  *
25162e41e8e6SAlan Cox  *	Helper method for drivers which want to hardwire 80 wire cable
2517be0d18dfSAlan Cox  *	detection.
2518be0d18dfSAlan Cox  */
2519be0d18dfSAlan Cox 
2520be0d18dfSAlan Cox int ata_cable_80wire(struct ata_port *ap)
2521be0d18dfSAlan Cox {
2522be0d18dfSAlan Cox 	return ATA_CBL_PATA80;
2523be0d18dfSAlan Cox }
2524be0d18dfSAlan Cox 
2525be0d18dfSAlan Cox /**
2526be0d18dfSAlan Cox  *	ata_cable_unknown	-	return unknown PATA cable.
2527be0d18dfSAlan Cox  *	@ap: port
2528be0d18dfSAlan Cox  *
2529be0d18dfSAlan Cox  *	Helper method for drivers which have no PATA cable detection.
2530be0d18dfSAlan Cox  */
2531be0d18dfSAlan Cox 
2532be0d18dfSAlan Cox int ata_cable_unknown(struct ata_port *ap)
2533be0d18dfSAlan Cox {
2534be0d18dfSAlan Cox 	return ATA_CBL_PATA_UNK;
2535be0d18dfSAlan Cox }
2536be0d18dfSAlan Cox 
2537be0d18dfSAlan Cox /**
2538c88f90c3STejun Heo  *	ata_cable_ignore	-	return ignored PATA cable.
2539c88f90c3STejun Heo  *	@ap: port
2540c88f90c3STejun Heo  *
2541c88f90c3STejun Heo  *	Helper method for drivers which don't use cable type to limit
2542c88f90c3STejun Heo  *	transfer mode.
2543c88f90c3STejun Heo  */
2544c88f90c3STejun Heo int ata_cable_ignore(struct ata_port *ap)
2545c88f90c3STejun Heo {
2546c88f90c3STejun Heo 	return ATA_CBL_PATA_IGN;
2547c88f90c3STejun Heo }
2548c88f90c3STejun Heo 
2549c88f90c3STejun Heo /**
2550be0d18dfSAlan Cox  *	ata_cable_sata	-	return SATA cable type
2551be0d18dfSAlan Cox  *	@ap: port
2552be0d18dfSAlan Cox  *
2553be0d18dfSAlan Cox  *	Helper method for drivers which have SATA cables
2554be0d18dfSAlan Cox  */
2555be0d18dfSAlan Cox 
2556be0d18dfSAlan Cox int ata_cable_sata(struct ata_port *ap)
2557be0d18dfSAlan Cox {
2558be0d18dfSAlan Cox 	return ATA_CBL_SATA;
2559be0d18dfSAlan Cox }
2560be0d18dfSAlan Cox 
2561be0d18dfSAlan Cox /**
2562c6fd2807SJeff Garzik  *	ata_bus_probe - Reset and probe ATA bus
2563c6fd2807SJeff Garzik  *	@ap: Bus to probe
2564c6fd2807SJeff Garzik  *
2565c6fd2807SJeff Garzik  *	Master ATA bus probing function.  Initiates a hardware-dependent
2566c6fd2807SJeff Garzik  *	bus reset, then attempts to identify any devices found on
2567c6fd2807SJeff Garzik  *	the bus.
2568c6fd2807SJeff Garzik  *
2569c6fd2807SJeff Garzik  *	LOCKING:
2570c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
2571c6fd2807SJeff Garzik  *
2572c6fd2807SJeff Garzik  *	RETURNS:
2573c6fd2807SJeff Garzik  *	Zero on success, negative errno otherwise.
2574c6fd2807SJeff Garzik  */
2575c6fd2807SJeff Garzik 
2576c6fd2807SJeff Garzik int ata_bus_probe(struct ata_port *ap)
2577c6fd2807SJeff Garzik {
2578c6fd2807SJeff Garzik 	unsigned int classes[ATA_MAX_DEVICES];
2579c6fd2807SJeff Garzik 	int tries[ATA_MAX_DEVICES];
2580f58229f8STejun Heo 	int rc;
2581c6fd2807SJeff Garzik 	struct ata_device *dev;
2582c6fd2807SJeff Garzik 
2583c6fd2807SJeff Garzik 	ata_port_probe(ap);
2584c6fd2807SJeff Garzik 
2585f58229f8STejun Heo 	ata_link_for_each_dev(dev, &ap->link)
2586f58229f8STejun Heo 		tries[dev->devno] = ATA_PROBE_MAX_TRIES;
2587c6fd2807SJeff Garzik 
2588c6fd2807SJeff Garzik  retry:
2589cdeab114STejun Heo 	ata_link_for_each_dev(dev, &ap->link) {
2590cdeab114STejun Heo 		/* If we issue an SRST then an ATA drive (not ATAPI)
2591cdeab114STejun Heo 		 * may change configuration and be in PIO0 timing. If
2592cdeab114STejun Heo 		 * we do a hard reset (or are coming from power on)
2593cdeab114STejun Heo 		 * this is true for ATA or ATAPI. Until we've set a
2594cdeab114STejun Heo 		 * suitable controller mode we should not touch the
2595cdeab114STejun Heo 		 * bus as we may be talking too fast.
2596cdeab114STejun Heo 		 */
2597cdeab114STejun Heo 		dev->pio_mode = XFER_PIO_0;
2598cdeab114STejun Heo 
2599cdeab114STejun Heo 		/* If the controller has a pio mode setup function
2600cdeab114STejun Heo 		 * then use it to set the chipset to rights. Don't
2601cdeab114STejun Heo 		 * touch the DMA setup as that will be dealt with when
2602cdeab114STejun Heo 		 * configuring devices.
2603cdeab114STejun Heo 		 */
2604cdeab114STejun Heo 		if (ap->ops->set_piomode)
2605cdeab114STejun Heo 			ap->ops->set_piomode(ap, dev);
2606cdeab114STejun Heo 	}
2607cdeab114STejun Heo 
2608c6fd2807SJeff Garzik 	/* reset and determine device classes */
2609c6fd2807SJeff Garzik 	ap->ops->phy_reset(ap);
2610c6fd2807SJeff Garzik 
2611f58229f8STejun Heo 	ata_link_for_each_dev(dev, &ap->link) {
2612c6fd2807SJeff Garzik 		if (!(ap->flags & ATA_FLAG_DISABLED) &&
2613c6fd2807SJeff Garzik 		    dev->class != ATA_DEV_UNKNOWN)
2614c6fd2807SJeff Garzik 			classes[dev->devno] = dev->class;
2615c6fd2807SJeff Garzik 		else
2616c6fd2807SJeff Garzik 			classes[dev->devno] = ATA_DEV_NONE;
2617c6fd2807SJeff Garzik 
2618c6fd2807SJeff Garzik 		dev->class = ATA_DEV_UNKNOWN;
2619c6fd2807SJeff Garzik 	}
2620c6fd2807SJeff Garzik 
2621c6fd2807SJeff Garzik 	ata_port_probe(ap);
2622c6fd2807SJeff Garzik 
2623f31f0cc2SJeff Garzik 	/* read IDENTIFY page and configure devices. We have to do the identify
2624f31f0cc2SJeff Garzik 	   specific sequence bass-ackwards so that PDIAG- is released by
2625f31f0cc2SJeff Garzik 	   the slave device */
2626f31f0cc2SJeff Garzik 
2627a4ba7fe2SBartlomiej Zolnierkiewicz 	ata_link_for_each_dev_reverse(dev, &ap->link) {
2628f58229f8STejun Heo 		if (tries[dev->devno])
2629f58229f8STejun Heo 			dev->class = classes[dev->devno];
2630c6fd2807SJeff Garzik 
2631c6fd2807SJeff Garzik 		if (!ata_dev_enabled(dev))
2632c6fd2807SJeff Garzik 			continue;
2633c6fd2807SJeff Garzik 
2634bff04647STejun Heo 		rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2635bff04647STejun Heo 				     dev->id);
2636c6fd2807SJeff Garzik 		if (rc)
2637c6fd2807SJeff Garzik 			goto fail;
2638f31f0cc2SJeff Garzik 	}
2639f31f0cc2SJeff Garzik 
2640be0d18dfSAlan Cox 	/* Now ask for the cable type as PDIAG- should have been released */
2641be0d18dfSAlan Cox 	if (ap->ops->cable_detect)
2642be0d18dfSAlan Cox 		ap->cbl = ap->ops->cable_detect(ap);
2643be0d18dfSAlan Cox 
2644614fe29bSAlan Cox 	/* We may have SATA bridge glue hiding here irrespective of the
2645614fe29bSAlan Cox 	   reported cable types and sensed types */
2646614fe29bSAlan Cox 	ata_link_for_each_dev(dev, &ap->link) {
2647614fe29bSAlan Cox 		if (!ata_dev_enabled(dev))
2648614fe29bSAlan Cox 			continue;
2649614fe29bSAlan Cox 		/* SATA drives indicate we have a bridge. We don't know which
2650614fe29bSAlan Cox 		   end of the link the bridge is which is a problem */
2651614fe29bSAlan Cox 		if (ata_id_is_sata(dev->id))
2652614fe29bSAlan Cox 			ap->cbl = ATA_CBL_SATA;
2653614fe29bSAlan Cox 	}
2654614fe29bSAlan Cox 
2655f31f0cc2SJeff Garzik 	/* After the identify sequence we can now set up the devices. We do
2656f31f0cc2SJeff Garzik 	   this in the normal order so that the user doesn't get confused */
2657f31f0cc2SJeff Garzik 
2658f58229f8STejun Heo 	ata_link_for_each_dev(dev, &ap->link) {
2659f31f0cc2SJeff Garzik 		if (!ata_dev_enabled(dev))
2660f31f0cc2SJeff Garzik 			continue;
2661c6fd2807SJeff Garzik 
26629af5c9c9STejun Heo 		ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
2663efdaedc4STejun Heo 		rc = ata_dev_configure(dev);
26649af5c9c9STejun Heo 		ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
2665c6fd2807SJeff Garzik 		if (rc)
2666c6fd2807SJeff Garzik 			goto fail;
2667c6fd2807SJeff Garzik 	}
2668c6fd2807SJeff Garzik 
2669c6fd2807SJeff Garzik 	/* configure transfer mode */
26700260731fSTejun Heo 	rc = ata_set_mode(&ap->link, &dev);
26714ae72a1eSTejun Heo 	if (rc)
2672c6fd2807SJeff Garzik 		goto fail;
2673c6fd2807SJeff Garzik 
2674f58229f8STejun Heo 	ata_link_for_each_dev(dev, &ap->link)
2675f58229f8STejun Heo 		if (ata_dev_enabled(dev))
2676c6fd2807SJeff Garzik 			return 0;
2677c6fd2807SJeff Garzik 
2678c6fd2807SJeff Garzik 	/* no device present, disable port */
2679c6fd2807SJeff Garzik 	ata_port_disable(ap);
2680c6fd2807SJeff Garzik 	return -ENODEV;
2681c6fd2807SJeff Garzik 
2682c6fd2807SJeff Garzik  fail:
26834ae72a1eSTejun Heo 	tries[dev->devno]--;
26844ae72a1eSTejun Heo 
2685c6fd2807SJeff Garzik 	switch (rc) {
2686c6fd2807SJeff Garzik 	case -EINVAL:
26874ae72a1eSTejun Heo 		/* eeek, something went very wrong, give up */
2688c6fd2807SJeff Garzik 		tries[dev->devno] = 0;
2689c6fd2807SJeff Garzik 		break;
26904ae72a1eSTejun Heo 
26914ae72a1eSTejun Heo 	case -ENODEV:
26924ae72a1eSTejun Heo 		/* give it just one more chance */
26934ae72a1eSTejun Heo 		tries[dev->devno] = min(tries[dev->devno], 1);
2694c6fd2807SJeff Garzik 	case -EIO:
26954ae72a1eSTejun Heo 		if (tries[dev->devno] == 1) {
26964ae72a1eSTejun Heo 			/* This is the last chance, better to slow
26974ae72a1eSTejun Heo 			 * down than lose it.
26984ae72a1eSTejun Heo 			 */
2699936fd732STejun Heo 			sata_down_spd_limit(&ap->link);
27004ae72a1eSTejun Heo 			ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
27014ae72a1eSTejun Heo 		}
2702c6fd2807SJeff Garzik 	}
2703c6fd2807SJeff Garzik 
27044ae72a1eSTejun Heo 	if (!tries[dev->devno])
2705c6fd2807SJeff Garzik 		ata_dev_disable(dev);
2706c6fd2807SJeff Garzik 
2707c6fd2807SJeff Garzik 	goto retry;
2708c6fd2807SJeff Garzik }
2709c6fd2807SJeff Garzik 
2710c6fd2807SJeff Garzik /**
2711c6fd2807SJeff Garzik  *	ata_port_probe - Mark port as enabled
2712c6fd2807SJeff Garzik  *	@ap: Port for which we indicate enablement
2713c6fd2807SJeff Garzik  *
2714c6fd2807SJeff Garzik  *	Modify @ap data structure such that the system
2715c6fd2807SJeff Garzik  *	thinks that the entire port is enabled.
2716c6fd2807SJeff Garzik  *
2717cca3974eSJeff Garzik  *	LOCKING: host lock, or some other form of
2718c6fd2807SJeff Garzik  *	serialization.
2719c6fd2807SJeff Garzik  */
2720c6fd2807SJeff Garzik 
2721c6fd2807SJeff Garzik void ata_port_probe(struct ata_port *ap)
2722c6fd2807SJeff Garzik {
2723c6fd2807SJeff Garzik 	ap->flags &= ~ATA_FLAG_DISABLED;
2724c6fd2807SJeff Garzik }
2725c6fd2807SJeff Garzik 
2726c6fd2807SJeff Garzik /**
2727c6fd2807SJeff Garzik  *	sata_print_link_status - Print SATA link status
2728936fd732STejun Heo  *	@link: SATA link to printk link status about
2729c6fd2807SJeff Garzik  *
2730c6fd2807SJeff Garzik  *	This function prints link speed and status of a SATA link.
2731c6fd2807SJeff Garzik  *
2732c6fd2807SJeff Garzik  *	LOCKING:
2733c6fd2807SJeff Garzik  *	None.
2734c6fd2807SJeff Garzik  */
27356bdb4fc9SAdrian Bunk static void sata_print_link_status(struct ata_link *link)
2736c6fd2807SJeff Garzik {
2737c6fd2807SJeff Garzik 	u32 sstatus, scontrol, tmp;
2738c6fd2807SJeff Garzik 
2739936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus))
2740c6fd2807SJeff Garzik 		return;
2741936fd732STejun Heo 	sata_scr_read(link, SCR_CONTROL, &scontrol);
2742c6fd2807SJeff Garzik 
2743b1c72916STejun Heo 	if (ata_phys_link_online(link)) {
2744c6fd2807SJeff Garzik 		tmp = (sstatus >> 4) & 0xf;
2745936fd732STejun Heo 		ata_link_printk(link, KERN_INFO,
2746c6fd2807SJeff Garzik 				"SATA link up %s (SStatus %X SControl %X)\n",
2747c6fd2807SJeff Garzik 				sata_spd_string(tmp), sstatus, scontrol);
2748c6fd2807SJeff Garzik 	} else {
2749936fd732STejun Heo 		ata_link_printk(link, KERN_INFO,
2750c6fd2807SJeff Garzik 				"SATA link down (SStatus %X SControl %X)\n",
2751c6fd2807SJeff Garzik 				sstatus, scontrol);
2752c6fd2807SJeff Garzik 	}
2753c6fd2807SJeff Garzik }
2754c6fd2807SJeff Garzik 
2755c6fd2807SJeff Garzik /**
2756c6fd2807SJeff Garzik  *	ata_dev_pair		-	return other device on cable
2757c6fd2807SJeff Garzik  *	@adev: device
2758c6fd2807SJeff Garzik  *
2759c6fd2807SJeff Garzik  *	Obtain the other device on the same cable, or if none is
2760c6fd2807SJeff Garzik  *	present NULL is returned
2761c6fd2807SJeff Garzik  */
2762c6fd2807SJeff Garzik 
2763c6fd2807SJeff Garzik struct ata_device *ata_dev_pair(struct ata_device *adev)
2764c6fd2807SJeff Garzik {
27659af5c9c9STejun Heo 	struct ata_link *link = adev->link;
27669af5c9c9STejun Heo 	struct ata_device *pair = &link->device[1 - adev->devno];
2767c6fd2807SJeff Garzik 	if (!ata_dev_enabled(pair))
2768c6fd2807SJeff Garzik 		return NULL;
2769c6fd2807SJeff Garzik 	return pair;
2770c6fd2807SJeff Garzik }
2771c6fd2807SJeff Garzik 
2772c6fd2807SJeff Garzik /**
2773c6fd2807SJeff Garzik  *	ata_port_disable - Disable port.
2774c6fd2807SJeff Garzik  *	@ap: Port to be disabled.
2775c6fd2807SJeff Garzik  *
2776c6fd2807SJeff Garzik  *	Modify @ap data structure such that the system
2777c6fd2807SJeff Garzik  *	thinks that the entire port is disabled, and should
2778c6fd2807SJeff Garzik  *	never attempt to probe or communicate with devices
2779c6fd2807SJeff Garzik  *	on this port.
2780c6fd2807SJeff Garzik  *
2781cca3974eSJeff Garzik  *	LOCKING: host lock, or some other form of
2782c6fd2807SJeff Garzik  *	serialization.
2783c6fd2807SJeff Garzik  */
2784c6fd2807SJeff Garzik 
2785c6fd2807SJeff Garzik void ata_port_disable(struct ata_port *ap)
2786c6fd2807SJeff Garzik {
27879af5c9c9STejun Heo 	ap->link.device[0].class = ATA_DEV_NONE;
27889af5c9c9STejun Heo 	ap->link.device[1].class = ATA_DEV_NONE;
2789c6fd2807SJeff Garzik 	ap->flags |= ATA_FLAG_DISABLED;
2790c6fd2807SJeff Garzik }
2791c6fd2807SJeff Garzik 
2792c6fd2807SJeff Garzik /**
2793c6fd2807SJeff Garzik  *	sata_down_spd_limit - adjust SATA spd limit downward
2794936fd732STejun Heo  *	@link: Link to adjust SATA spd limit for
2795c6fd2807SJeff Garzik  *
2796936fd732STejun Heo  *	Adjust SATA spd limit of @link downward.  Note that this
2797c6fd2807SJeff Garzik  *	function only adjusts the limit.  The change must be applied
2798c6fd2807SJeff Garzik  *	using sata_set_spd().
2799c6fd2807SJeff Garzik  *
2800c6fd2807SJeff Garzik  *	LOCKING:
2801c6fd2807SJeff Garzik  *	Inherited from caller.
2802c6fd2807SJeff Garzik  *
2803c6fd2807SJeff Garzik  *	RETURNS:
2804c6fd2807SJeff Garzik  *	0 on success, negative errno on failure
2805c6fd2807SJeff Garzik  */
2806936fd732STejun Heo int sata_down_spd_limit(struct ata_link *link)
2807c6fd2807SJeff Garzik {
2808c6fd2807SJeff Garzik 	u32 sstatus, spd, mask;
2809c6fd2807SJeff Garzik 	int rc, highbit;
2810c6fd2807SJeff Garzik 
2811936fd732STejun Heo 	if (!sata_scr_valid(link))
2812008a7896STejun Heo 		return -EOPNOTSUPP;
2813008a7896STejun Heo 
2814008a7896STejun Heo 	/* If SCR can be read, use it to determine the current SPD.
2815936fd732STejun Heo 	 * If not, use cached value in link->sata_spd.
2816008a7896STejun Heo 	 */
2817936fd732STejun Heo 	rc = sata_scr_read(link, SCR_STATUS, &sstatus);
2818008a7896STejun Heo 	if (rc == 0)
2819008a7896STejun Heo 		spd = (sstatus >> 4) & 0xf;
2820008a7896STejun Heo 	else
2821936fd732STejun Heo 		spd = link->sata_spd;
2822c6fd2807SJeff Garzik 
2823936fd732STejun Heo 	mask = link->sata_spd_limit;
2824c6fd2807SJeff Garzik 	if (mask <= 1)
2825c6fd2807SJeff Garzik 		return -EINVAL;
2826008a7896STejun Heo 
2827008a7896STejun Heo 	/* unconditionally mask off the highest bit */
2828c6fd2807SJeff Garzik 	highbit = fls(mask) - 1;
2829c6fd2807SJeff Garzik 	mask &= ~(1 << highbit);
2830c6fd2807SJeff Garzik 
2831008a7896STejun Heo 	/* Mask off all speeds higher than or equal to the current
2832008a7896STejun Heo 	 * one.  Force 1.5Gbps if current SPD is not available.
2833008a7896STejun Heo 	 */
2834008a7896STejun Heo 	if (spd > 1)
2835008a7896STejun Heo 		mask &= (1 << (spd - 1)) - 1;
2836008a7896STejun Heo 	else
2837008a7896STejun Heo 		mask &= 1;
2838008a7896STejun Heo 
2839008a7896STejun Heo 	/* were we already at the bottom? */
2840c6fd2807SJeff Garzik 	if (!mask)
2841c6fd2807SJeff Garzik 		return -EINVAL;
2842c6fd2807SJeff Garzik 
2843936fd732STejun Heo 	link->sata_spd_limit = mask;
2844c6fd2807SJeff Garzik 
2845936fd732STejun Heo 	ata_link_printk(link, KERN_WARNING, "limiting SATA link speed to %s\n",
2846c6fd2807SJeff Garzik 			sata_spd_string(fls(mask)));
2847c6fd2807SJeff Garzik 
2848c6fd2807SJeff Garzik 	return 0;
2849c6fd2807SJeff Garzik }
2850c6fd2807SJeff Garzik 
2851936fd732STejun Heo static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
2852c6fd2807SJeff Garzik {
28535270222fSTejun Heo 	struct ata_link *host_link = &link->ap->link;
28545270222fSTejun Heo 	u32 limit, target, spd;
2855c6fd2807SJeff Garzik 
28565270222fSTejun Heo 	limit = link->sata_spd_limit;
28575270222fSTejun Heo 
28585270222fSTejun Heo 	/* Don't configure downstream link faster than upstream link.
28595270222fSTejun Heo 	 * It doesn't speed up anything and some PMPs choke on such
28605270222fSTejun Heo 	 * configuration.
28615270222fSTejun Heo 	 */
28625270222fSTejun Heo 	if (!ata_is_host_link(link) && host_link->sata_spd)
28635270222fSTejun Heo 		limit &= (1 << host_link->sata_spd) - 1;
28645270222fSTejun Heo 
28655270222fSTejun Heo 	if (limit == UINT_MAX)
28665270222fSTejun Heo 		target = 0;
2867c6fd2807SJeff Garzik 	else
28685270222fSTejun Heo 		target = fls(limit);
2869c6fd2807SJeff Garzik 
2870c6fd2807SJeff Garzik 	spd = (*scontrol >> 4) & 0xf;
28715270222fSTejun Heo 	*scontrol = (*scontrol & ~0xf0) | ((target & 0xf) << 4);
2872c6fd2807SJeff Garzik 
28735270222fSTejun Heo 	return spd != target;
2874c6fd2807SJeff Garzik }
2875c6fd2807SJeff Garzik 
2876c6fd2807SJeff Garzik /**
2877c6fd2807SJeff Garzik  *	sata_set_spd_needed - is SATA spd configuration needed
2878936fd732STejun Heo  *	@link: Link in question
2879c6fd2807SJeff Garzik  *
2880c6fd2807SJeff Garzik  *	Test whether the spd limit in SControl matches
2881936fd732STejun Heo  *	@link->sata_spd_limit.  This function is used to determine
2882c6fd2807SJeff Garzik  *	whether hardreset is necessary to apply SATA spd
2883c6fd2807SJeff Garzik  *	configuration.
2884c6fd2807SJeff Garzik  *
2885c6fd2807SJeff Garzik  *	LOCKING:
2886c6fd2807SJeff Garzik  *	Inherited from caller.
2887c6fd2807SJeff Garzik  *
2888c6fd2807SJeff Garzik  *	RETURNS:
2889c6fd2807SJeff Garzik  *	1 if SATA spd configuration is needed, 0 otherwise.
2890c6fd2807SJeff Garzik  */
28911dc55e87SAdrian Bunk static int sata_set_spd_needed(struct ata_link *link)
2892c6fd2807SJeff Garzik {
2893c6fd2807SJeff Garzik 	u32 scontrol;
2894c6fd2807SJeff Garzik 
2895936fd732STejun Heo 	if (sata_scr_read(link, SCR_CONTROL, &scontrol))
2896db64bcf3STejun Heo 		return 1;
2897c6fd2807SJeff Garzik 
2898936fd732STejun Heo 	return __sata_set_spd_needed(link, &scontrol);
2899c6fd2807SJeff Garzik }
2900c6fd2807SJeff Garzik 
2901c6fd2807SJeff Garzik /**
2902c6fd2807SJeff Garzik  *	sata_set_spd - set SATA spd according to spd limit
2903936fd732STejun Heo  *	@link: Link to set SATA spd for
2904c6fd2807SJeff Garzik  *
2905936fd732STejun Heo  *	Set SATA spd of @link according to sata_spd_limit.
2906c6fd2807SJeff Garzik  *
2907c6fd2807SJeff Garzik  *	LOCKING:
2908c6fd2807SJeff Garzik  *	Inherited from caller.
2909c6fd2807SJeff Garzik  *
2910c6fd2807SJeff Garzik  *	RETURNS:
2911c6fd2807SJeff Garzik  *	0 if spd doesn't need to be changed, 1 if spd has been
2912c6fd2807SJeff Garzik  *	changed.  Negative errno if SCR registers are inaccessible.
2913c6fd2807SJeff Garzik  */
2914936fd732STejun Heo int sata_set_spd(struct ata_link *link)
2915c6fd2807SJeff Garzik {
2916c6fd2807SJeff Garzik 	u32 scontrol;
2917c6fd2807SJeff Garzik 	int rc;
2918c6fd2807SJeff Garzik 
2919936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
2920c6fd2807SJeff Garzik 		return rc;
2921c6fd2807SJeff Garzik 
2922936fd732STejun Heo 	if (!__sata_set_spd_needed(link, &scontrol))
2923c6fd2807SJeff Garzik 		return 0;
2924c6fd2807SJeff Garzik 
2925936fd732STejun Heo 	if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
2926c6fd2807SJeff Garzik 		return rc;
2927c6fd2807SJeff Garzik 
2928c6fd2807SJeff Garzik 	return 1;
2929c6fd2807SJeff Garzik }
2930c6fd2807SJeff Garzik 
2931c6fd2807SJeff Garzik /*
2932c6fd2807SJeff Garzik  * This mode timing computation functionality is ported over from
2933c6fd2807SJeff Garzik  * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
2934c6fd2807SJeff Garzik  */
2935c6fd2807SJeff Garzik /*
2936b352e57dSAlan Cox  * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
2937c6fd2807SJeff Garzik  * These were taken from ATA/ATAPI-6 standard, rev 0a, except
2938b352e57dSAlan Cox  * for UDMA6, which is currently supported only by Maxtor drives.
2939b352e57dSAlan Cox  *
2940b352e57dSAlan Cox  * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0.
2941c6fd2807SJeff Garzik  */
2942c6fd2807SJeff Garzik 
2943c6fd2807SJeff Garzik static const struct ata_timing ata_timing[] = {
294470cd071eSTejun Heo /*	{ XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 960,   0 }, */
294570cd071eSTejun Heo 	{ XFER_PIO_0,     70, 290, 240, 600, 165, 150, 600,   0 },
294670cd071eSTejun Heo 	{ XFER_PIO_1,     50, 290,  93, 383, 125, 100, 383,   0 },
294770cd071eSTejun Heo 	{ XFER_PIO_2,     30, 290,  40, 330, 100,  90, 240,   0 },
294870cd071eSTejun Heo 	{ XFER_PIO_3,     30,  80,  70, 180,  80,  70, 180,   0 },
294970cd071eSTejun Heo 	{ XFER_PIO_4,     25,  70,  25, 120,  70,  25, 120,   0 },
295070cd071eSTejun Heo 	{ XFER_PIO_5,     15,  65,  25, 100,  65,  25, 100,   0 },
295170cd071eSTejun Heo 	{ XFER_PIO_6,     10,  55,  20,  80,  55,  20,  80,   0 },
2952c6fd2807SJeff Garzik 
295370cd071eSTejun Heo 	{ XFER_SW_DMA_0, 120,   0,   0,   0, 480, 480, 960,   0 },
295470cd071eSTejun Heo 	{ XFER_SW_DMA_1,  90,   0,   0,   0, 240, 240, 480,   0 },
295570cd071eSTejun Heo 	{ XFER_SW_DMA_2,  60,   0,   0,   0, 120, 120, 240,   0 },
2956c6fd2807SJeff Garzik 
295770cd071eSTejun Heo 	{ XFER_MW_DMA_0,  60,   0,   0,   0, 215, 215, 480,   0 },
295870cd071eSTejun Heo 	{ XFER_MW_DMA_1,  45,   0,   0,   0,  80,  50, 150,   0 },
295970cd071eSTejun Heo 	{ XFER_MW_DMA_2,  25,   0,   0,   0,  70,  25, 120,   0 },
2960b352e57dSAlan Cox 	{ XFER_MW_DMA_3,  25,   0,   0,   0,  65,  25, 100,   0 },
296170cd071eSTejun Heo 	{ XFER_MW_DMA_4,  25,   0,   0,   0,  55,  20,  80,   0 },
2962c6fd2807SJeff Garzik 
2963c6fd2807SJeff Garzik /*	{ XFER_UDMA_SLOW,  0,   0,   0,   0,   0,   0,   0, 150 }, */
296470cd071eSTejun Heo 	{ XFER_UDMA_0,     0,   0,   0,   0,   0,   0,   0, 120 },
296570cd071eSTejun Heo 	{ XFER_UDMA_1,     0,   0,   0,   0,   0,   0,   0,  80 },
296670cd071eSTejun Heo 	{ XFER_UDMA_2,     0,   0,   0,   0,   0,   0,   0,  60 },
296770cd071eSTejun Heo 	{ XFER_UDMA_3,     0,   0,   0,   0,   0,   0,   0,  45 },
296870cd071eSTejun Heo 	{ XFER_UDMA_4,     0,   0,   0,   0,   0,   0,   0,  30 },
296970cd071eSTejun Heo 	{ XFER_UDMA_5,     0,   0,   0,   0,   0,   0,   0,  20 },
297070cd071eSTejun Heo 	{ XFER_UDMA_6,     0,   0,   0,   0,   0,   0,   0,  15 },
2971c6fd2807SJeff Garzik 
2972c6fd2807SJeff Garzik 	{ 0xFF }
2973c6fd2807SJeff Garzik };
2974c6fd2807SJeff Garzik 
2975c6fd2807SJeff Garzik #define ENOUGH(v, unit)		(((v)-1)/(unit)+1)
2976c6fd2807SJeff Garzik #define EZ(v, unit)		((v)?ENOUGH(v, unit):0)
2977c6fd2807SJeff Garzik 
2978c6fd2807SJeff Garzik static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
2979c6fd2807SJeff Garzik {
2980c6fd2807SJeff Garzik 	q->setup   = EZ(t->setup   * 1000,  T);
2981c6fd2807SJeff Garzik 	q->act8b   = EZ(t->act8b   * 1000,  T);
2982c6fd2807SJeff Garzik 	q->rec8b   = EZ(t->rec8b   * 1000,  T);
2983c6fd2807SJeff Garzik 	q->cyc8b   = EZ(t->cyc8b   * 1000,  T);
2984c6fd2807SJeff Garzik 	q->active  = EZ(t->active  * 1000,  T);
2985c6fd2807SJeff Garzik 	q->recover = EZ(t->recover * 1000,  T);
2986c6fd2807SJeff Garzik 	q->cycle   = EZ(t->cycle   * 1000,  T);
2987c6fd2807SJeff Garzik 	q->udma    = EZ(t->udma    * 1000, UT);
2988c6fd2807SJeff Garzik }
2989c6fd2807SJeff Garzik 
2990c6fd2807SJeff Garzik void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
2991c6fd2807SJeff Garzik 		      struct ata_timing *m, unsigned int what)
2992c6fd2807SJeff Garzik {
2993c6fd2807SJeff Garzik 	if (what & ATA_TIMING_SETUP  ) m->setup   = max(a->setup,   b->setup);
2994c6fd2807SJeff Garzik 	if (what & ATA_TIMING_ACT8B  ) m->act8b   = max(a->act8b,   b->act8b);
2995c6fd2807SJeff Garzik 	if (what & ATA_TIMING_REC8B  ) m->rec8b   = max(a->rec8b,   b->rec8b);
2996c6fd2807SJeff Garzik 	if (what & ATA_TIMING_CYC8B  ) m->cyc8b   = max(a->cyc8b,   b->cyc8b);
2997c6fd2807SJeff Garzik 	if (what & ATA_TIMING_ACTIVE ) m->active  = max(a->active,  b->active);
2998c6fd2807SJeff Garzik 	if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
2999c6fd2807SJeff Garzik 	if (what & ATA_TIMING_CYCLE  ) m->cycle   = max(a->cycle,   b->cycle);
3000c6fd2807SJeff Garzik 	if (what & ATA_TIMING_UDMA   ) m->udma    = max(a->udma,    b->udma);
3001c6fd2807SJeff Garzik }
3002c6fd2807SJeff Garzik 
30036357357cSTejun Heo const struct ata_timing *ata_timing_find_mode(u8 xfer_mode)
3004c6fd2807SJeff Garzik {
300570cd071eSTejun Heo 	const struct ata_timing *t = ata_timing;
3006c6fd2807SJeff Garzik 
300770cd071eSTejun Heo 	while (xfer_mode > t->mode)
300870cd071eSTejun Heo 		t++;
300970cd071eSTejun Heo 
301070cd071eSTejun Heo 	if (xfer_mode == t->mode)
3011c6fd2807SJeff Garzik 		return t;
301270cd071eSTejun Heo 	return NULL;
3013c6fd2807SJeff Garzik }
3014c6fd2807SJeff Garzik 
3015c6fd2807SJeff Garzik int ata_timing_compute(struct ata_device *adev, unsigned short speed,
3016c6fd2807SJeff Garzik 		       struct ata_timing *t, int T, int UT)
3017c6fd2807SJeff Garzik {
3018c6fd2807SJeff Garzik 	const struct ata_timing *s;
3019c6fd2807SJeff Garzik 	struct ata_timing p;
3020c6fd2807SJeff Garzik 
3021c6fd2807SJeff Garzik 	/*
3022c6fd2807SJeff Garzik 	 * Find the mode.
3023c6fd2807SJeff Garzik 	 */
3024c6fd2807SJeff Garzik 
3025c6fd2807SJeff Garzik 	if (!(s = ata_timing_find_mode(speed)))
3026c6fd2807SJeff Garzik 		return -EINVAL;
3027c6fd2807SJeff Garzik 
3028c6fd2807SJeff Garzik 	memcpy(t, s, sizeof(*s));
3029c6fd2807SJeff Garzik 
3030c6fd2807SJeff Garzik 	/*
3031c6fd2807SJeff Garzik 	 * If the drive is an EIDE drive, it can tell us it needs extended
3032c6fd2807SJeff Garzik 	 * PIO/MW_DMA cycle timing.
3033c6fd2807SJeff Garzik 	 */
3034c6fd2807SJeff Garzik 
3035c6fd2807SJeff Garzik 	if (adev->id[ATA_ID_FIELD_VALID] & 2) {	/* EIDE drive */
3036c6fd2807SJeff Garzik 		memset(&p, 0, sizeof(p));
3037c6fd2807SJeff Garzik 		if (speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
3038c6fd2807SJeff Garzik 			if (speed <= XFER_PIO_2) p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO];
3039c6fd2807SJeff Garzik 					    else p.cycle = p.cyc8b = adev->id[ATA_ID_EIDE_PIO_IORDY];
3040c6fd2807SJeff Garzik 		} else if (speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2) {
3041c6fd2807SJeff Garzik 			p.cycle = adev->id[ATA_ID_EIDE_DMA_MIN];
3042c6fd2807SJeff Garzik 		}
3043c6fd2807SJeff Garzik 		ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
3044c6fd2807SJeff Garzik 	}
3045c6fd2807SJeff Garzik 
3046c6fd2807SJeff Garzik 	/*
3047c6fd2807SJeff Garzik 	 * Convert the timing to bus clock counts.
3048c6fd2807SJeff Garzik 	 */
3049c6fd2807SJeff Garzik 
3050c6fd2807SJeff Garzik 	ata_timing_quantize(t, t, T, UT);
3051c6fd2807SJeff Garzik 
3052c6fd2807SJeff Garzik 	/*
3053c6fd2807SJeff Garzik 	 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
3054c6fd2807SJeff Garzik 	 * S.M.A.R.T * and some other commands. We have to ensure that the
3055c6fd2807SJeff Garzik 	 * DMA cycle timing is slower/equal than the fastest PIO timing.
3056c6fd2807SJeff Garzik 	 */
3057c6fd2807SJeff Garzik 
3058fd3367afSAlan 	if (speed > XFER_PIO_6) {
3059c6fd2807SJeff Garzik 		ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
3060c6fd2807SJeff Garzik 		ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
3061c6fd2807SJeff Garzik 	}
3062c6fd2807SJeff Garzik 
3063c6fd2807SJeff Garzik 	/*
3064c6fd2807SJeff Garzik 	 * Lengthen active & recovery time so that cycle time is correct.
3065c6fd2807SJeff Garzik 	 */
3066c6fd2807SJeff Garzik 
3067c6fd2807SJeff Garzik 	if (t->act8b + t->rec8b < t->cyc8b) {
3068c6fd2807SJeff Garzik 		t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
3069c6fd2807SJeff Garzik 		t->rec8b = t->cyc8b - t->act8b;
3070c6fd2807SJeff Garzik 	}
3071c6fd2807SJeff Garzik 
3072c6fd2807SJeff Garzik 	if (t->active + t->recover < t->cycle) {
3073c6fd2807SJeff Garzik 		t->active += (t->cycle - (t->active + t->recover)) / 2;
3074c6fd2807SJeff Garzik 		t->recover = t->cycle - t->active;
3075c6fd2807SJeff Garzik 	}
30764f701d1eSAlan Cox 
30774f701d1eSAlan Cox 	/* In a few cases quantisation may produce enough errors to
30784f701d1eSAlan Cox 	   leave t->cycle too low for the sum of active and recovery
30794f701d1eSAlan Cox 	   if so we must correct this */
30804f701d1eSAlan Cox 	if (t->active + t->recover > t->cycle)
30814f701d1eSAlan Cox 		t->cycle = t->active + t->recover;
3082c6fd2807SJeff Garzik 
3083c6fd2807SJeff Garzik 	return 0;
3084c6fd2807SJeff Garzik }
3085c6fd2807SJeff Garzik 
3086c6fd2807SJeff Garzik /**
3087a0f79b92STejun Heo  *	ata_timing_cycle2mode - find xfer mode for the specified cycle duration
3088a0f79b92STejun Heo  *	@xfer_shift: ATA_SHIFT_* value for transfer type to examine.
3089a0f79b92STejun Heo  *	@cycle: cycle duration in ns
3090a0f79b92STejun Heo  *
3091a0f79b92STejun Heo  *	Return matching xfer mode for @cycle.  The returned mode is of
3092a0f79b92STejun Heo  *	the transfer type specified by @xfer_shift.  If @cycle is too
3093a0f79b92STejun Heo  *	slow for @xfer_shift, 0xff is returned.  If @cycle is faster
3094a0f79b92STejun Heo  *	than the fastest known mode, the fasted mode is returned.
3095a0f79b92STejun Heo  *
3096a0f79b92STejun Heo  *	LOCKING:
3097a0f79b92STejun Heo  *	None.
3098a0f79b92STejun Heo  *
3099a0f79b92STejun Heo  *	RETURNS:
3100a0f79b92STejun Heo  *	Matching xfer_mode, 0xff if no match found.
3101a0f79b92STejun Heo  */
3102a0f79b92STejun Heo u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
3103a0f79b92STejun Heo {
3104a0f79b92STejun Heo 	u8 base_mode = 0xff, last_mode = 0xff;
3105a0f79b92STejun Heo 	const struct ata_xfer_ent *ent;
3106a0f79b92STejun Heo 	const struct ata_timing *t;
3107a0f79b92STejun Heo 
3108a0f79b92STejun Heo 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
3109a0f79b92STejun Heo 		if (ent->shift == xfer_shift)
3110a0f79b92STejun Heo 			base_mode = ent->base;
3111a0f79b92STejun Heo 
3112a0f79b92STejun Heo 	for (t = ata_timing_find_mode(base_mode);
3113a0f79b92STejun Heo 	     t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
3114a0f79b92STejun Heo 		unsigned short this_cycle;
3115a0f79b92STejun Heo 
3116a0f79b92STejun Heo 		switch (xfer_shift) {
3117a0f79b92STejun Heo 		case ATA_SHIFT_PIO:
3118a0f79b92STejun Heo 		case ATA_SHIFT_MWDMA:
3119a0f79b92STejun Heo 			this_cycle = t->cycle;
3120a0f79b92STejun Heo 			break;
3121a0f79b92STejun Heo 		case ATA_SHIFT_UDMA:
3122a0f79b92STejun Heo 			this_cycle = t->udma;
3123a0f79b92STejun Heo 			break;
3124a0f79b92STejun Heo 		default:
3125a0f79b92STejun Heo 			return 0xff;
3126a0f79b92STejun Heo 		}
3127a0f79b92STejun Heo 
3128a0f79b92STejun Heo 		if (cycle > this_cycle)
3129a0f79b92STejun Heo 			break;
3130a0f79b92STejun Heo 
3131a0f79b92STejun Heo 		last_mode = t->mode;
3132a0f79b92STejun Heo 	}
3133a0f79b92STejun Heo 
3134a0f79b92STejun Heo 	return last_mode;
3135a0f79b92STejun Heo }
3136a0f79b92STejun Heo 
3137a0f79b92STejun Heo /**
3138c6fd2807SJeff Garzik  *	ata_down_xfermask_limit - adjust dev xfer masks downward
3139c6fd2807SJeff Garzik  *	@dev: Device to adjust xfer masks
3140458337dbSTejun Heo  *	@sel: ATA_DNXFER_* selector
3141c6fd2807SJeff Garzik  *
3142c6fd2807SJeff Garzik  *	Adjust xfer masks of @dev downward.  Note that this function
3143c6fd2807SJeff Garzik  *	does not apply the change.  Invoking ata_set_mode() afterwards
3144c6fd2807SJeff Garzik  *	will apply the limit.
3145c6fd2807SJeff Garzik  *
3146c6fd2807SJeff Garzik  *	LOCKING:
3147c6fd2807SJeff Garzik  *	Inherited from caller.
3148c6fd2807SJeff Garzik  *
3149c6fd2807SJeff Garzik  *	RETURNS:
3150c6fd2807SJeff Garzik  *	0 on success, negative errno on failure
3151c6fd2807SJeff Garzik  */
3152458337dbSTejun Heo int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
3153c6fd2807SJeff Garzik {
3154458337dbSTejun Heo 	char buf[32];
31557dc951aeSTejun Heo 	unsigned long orig_mask, xfer_mask;
31567dc951aeSTejun Heo 	unsigned long pio_mask, mwdma_mask, udma_mask;
3157458337dbSTejun Heo 	int quiet, highbit;
3158c6fd2807SJeff Garzik 
3159458337dbSTejun Heo 	quiet = !!(sel & ATA_DNXFER_QUIET);
3160458337dbSTejun Heo 	sel &= ~ATA_DNXFER_QUIET;
3161458337dbSTejun Heo 
3162458337dbSTejun Heo 	xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
3163458337dbSTejun Heo 						  dev->mwdma_mask,
3164c6fd2807SJeff Garzik 						  dev->udma_mask);
3165458337dbSTejun Heo 	ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
3166c6fd2807SJeff Garzik 
3167458337dbSTejun Heo 	switch (sel) {
3168458337dbSTejun Heo 	case ATA_DNXFER_PIO:
3169458337dbSTejun Heo 		highbit = fls(pio_mask) - 1;
3170458337dbSTejun Heo 		pio_mask &= ~(1 << highbit);
3171458337dbSTejun Heo 		break;
3172458337dbSTejun Heo 
3173458337dbSTejun Heo 	case ATA_DNXFER_DMA:
3174458337dbSTejun Heo 		if (udma_mask) {
3175458337dbSTejun Heo 			highbit = fls(udma_mask) - 1;
3176458337dbSTejun Heo 			udma_mask &= ~(1 << highbit);
3177458337dbSTejun Heo 			if (!udma_mask)
3178458337dbSTejun Heo 				return -ENOENT;
3179458337dbSTejun Heo 		} else if (mwdma_mask) {
3180458337dbSTejun Heo 			highbit = fls(mwdma_mask) - 1;
3181458337dbSTejun Heo 			mwdma_mask &= ~(1 << highbit);
3182458337dbSTejun Heo 			if (!mwdma_mask)
3183458337dbSTejun Heo 				return -ENOENT;
3184458337dbSTejun Heo 		}
3185458337dbSTejun Heo 		break;
3186458337dbSTejun Heo 
3187458337dbSTejun Heo 	case ATA_DNXFER_40C:
3188458337dbSTejun Heo 		udma_mask &= ATA_UDMA_MASK_40C;
3189458337dbSTejun Heo 		break;
3190458337dbSTejun Heo 
3191458337dbSTejun Heo 	case ATA_DNXFER_FORCE_PIO0:
3192458337dbSTejun Heo 		pio_mask &= 1;
3193458337dbSTejun Heo 	case ATA_DNXFER_FORCE_PIO:
3194458337dbSTejun Heo 		mwdma_mask = 0;
3195458337dbSTejun Heo 		udma_mask = 0;
3196458337dbSTejun Heo 		break;
3197458337dbSTejun Heo 
3198458337dbSTejun Heo 	default:
3199458337dbSTejun Heo 		BUG();
3200458337dbSTejun Heo 	}
3201458337dbSTejun Heo 
3202458337dbSTejun Heo 	xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
3203458337dbSTejun Heo 
3204458337dbSTejun Heo 	if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
3205458337dbSTejun Heo 		return -ENOENT;
3206458337dbSTejun Heo 
3207458337dbSTejun Heo 	if (!quiet) {
3208458337dbSTejun Heo 		if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
3209458337dbSTejun Heo 			snprintf(buf, sizeof(buf), "%s:%s",
3210458337dbSTejun Heo 				 ata_mode_string(xfer_mask),
3211458337dbSTejun Heo 				 ata_mode_string(xfer_mask & ATA_MASK_PIO));
3212458337dbSTejun Heo 		else
3213458337dbSTejun Heo 			snprintf(buf, sizeof(buf), "%s",
3214458337dbSTejun Heo 				 ata_mode_string(xfer_mask));
3215458337dbSTejun Heo 
3216458337dbSTejun Heo 		ata_dev_printk(dev, KERN_WARNING,
3217458337dbSTejun Heo 			       "limiting speed to %s\n", buf);
3218458337dbSTejun Heo 	}
3219c6fd2807SJeff Garzik 
3220c6fd2807SJeff Garzik 	ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
3221c6fd2807SJeff Garzik 			    &dev->udma_mask);
3222c6fd2807SJeff Garzik 
3223c6fd2807SJeff Garzik 	return 0;
3224c6fd2807SJeff Garzik }
3225c6fd2807SJeff Garzik 
3226c6fd2807SJeff Garzik static int ata_dev_set_mode(struct ata_device *dev)
3227c6fd2807SJeff Garzik {
32289af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
32294055dee7STejun Heo 	const char *dev_err_whine = "";
32304055dee7STejun Heo 	int ign_dev_err = 0;
3231c6fd2807SJeff Garzik 	unsigned int err_mask;
3232c6fd2807SJeff Garzik 	int rc;
3233c6fd2807SJeff Garzik 
3234c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_PIO;
3235c6fd2807SJeff Garzik 	if (dev->xfer_shift == ATA_SHIFT_PIO)
3236c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_PIO;
3237c6fd2807SJeff Garzik 
3238c6fd2807SJeff Garzik 	err_mask = ata_dev_set_xfermode(dev);
32392dcb407eSJeff Garzik 
32404055dee7STejun Heo 	if (err_mask & ~AC_ERR_DEV)
32414055dee7STejun Heo 		goto fail;
32422dcb407eSJeff Garzik 
32434055dee7STejun Heo 	/* revalidate */
3244baa1e78aSTejun Heo 	ehc->i.flags |= ATA_EHI_POST_SETMODE;
3245422c9daaSTejun Heo 	rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
3246baa1e78aSTejun Heo 	ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
3247c6fd2807SJeff Garzik 	if (rc)
3248c6fd2807SJeff Garzik 		return rc;
3249c6fd2807SJeff Garzik 
3250b93fda12SAlan Cox 	if (dev->xfer_shift == ATA_SHIFT_PIO) {
32514055dee7STejun Heo 		/* Old CFA may refuse this command, which is just fine */
3252b93fda12SAlan Cox 		if (ata_id_is_cfa(dev->id))
32534055dee7STejun Heo 			ign_dev_err = 1;
3254b93fda12SAlan Cox 		/* Catch several broken garbage emulations plus some pre
3255b93fda12SAlan Cox 		   ATA devices */
3256b93fda12SAlan Cox 		if (ata_id_major_version(dev->id) == 0 &&
32574055dee7STejun Heo 					dev->pio_mode <= XFER_PIO_2)
32584055dee7STejun Heo 			ign_dev_err = 1;
3259b93fda12SAlan Cox 		/* Some very old devices and some bad newer ones fail
3260b93fda12SAlan Cox 		   any kind of SET_XFERMODE request but support PIO0-2
3261b93fda12SAlan Cox 		   timings and no IORDY */
3262b93fda12SAlan Cox 		if (!ata_id_has_iordy(dev->id) && dev->pio_mode <= XFER_PIO_2)
3263b93fda12SAlan Cox 			ign_dev_err = 1;
3264b93fda12SAlan Cox 	}
32654055dee7STejun Heo 	/* Early MWDMA devices do DMA but don't allow DMA mode setting.
32664055dee7STejun Heo 	   Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
32674055dee7STejun Heo 	if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
32684055dee7STejun Heo 	    dev->dma_mode == XFER_MW_DMA_0 &&
32694055dee7STejun Heo 	    (dev->id[63] >> 8) & 1)
32704055dee7STejun Heo 		ign_dev_err = 1;
32714055dee7STejun Heo 
32724055dee7STejun Heo 	/* if the device is actually configured correctly, ignore dev err */
32734055dee7STejun Heo 	if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id)))
32744055dee7STejun Heo 		ign_dev_err = 1;
32754055dee7STejun Heo 
32764055dee7STejun Heo 	if (err_mask & AC_ERR_DEV) {
32774055dee7STejun Heo 		if (!ign_dev_err)
32784055dee7STejun Heo 			goto fail;
32794055dee7STejun Heo 		else
32804055dee7STejun Heo 			dev_err_whine = " (device error ignored)";
32814055dee7STejun Heo 	}
32824055dee7STejun Heo 
3283c6fd2807SJeff Garzik 	DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
3284c6fd2807SJeff Garzik 		dev->xfer_shift, (int)dev->xfer_mode);
3285c6fd2807SJeff Garzik 
32864055dee7STejun Heo 	ata_dev_printk(dev, KERN_INFO, "configured for %s%s\n",
32874055dee7STejun Heo 		       ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)),
32884055dee7STejun Heo 		       dev_err_whine);
32894055dee7STejun Heo 
3290c6fd2807SJeff Garzik 	return 0;
32914055dee7STejun Heo 
32924055dee7STejun Heo  fail:
32934055dee7STejun Heo 	ata_dev_printk(dev, KERN_ERR, "failed to set xfermode "
32944055dee7STejun Heo 		       "(err_mask=0x%x)\n", err_mask);
32954055dee7STejun Heo 	return -EIO;
3296c6fd2807SJeff Garzik }
3297c6fd2807SJeff Garzik 
3298c6fd2807SJeff Garzik /**
329904351821SAlan  *	ata_do_set_mode - Program timings and issue SET FEATURES - XFER
33000260731fSTejun Heo  *	@link: link on which timings will be programmed
33011967b7ffSJoe Perches  *	@r_failed_dev: out parameter for failed device
3302c6fd2807SJeff Garzik  *
330304351821SAlan  *	Standard implementation of the function used to tune and set
330404351821SAlan  *	ATA device disk transfer mode (PIO3, UDMA6, etc.).  If
330504351821SAlan  *	ata_dev_set_mode() fails, pointer to the failing device is
3306c6fd2807SJeff Garzik  *	returned in @r_failed_dev.
3307c6fd2807SJeff Garzik  *
3308c6fd2807SJeff Garzik  *	LOCKING:
3309c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
3310c6fd2807SJeff Garzik  *
3311c6fd2807SJeff Garzik  *	RETURNS:
3312c6fd2807SJeff Garzik  *	0 on success, negative errno otherwise
3313c6fd2807SJeff Garzik  */
331404351821SAlan 
33150260731fSTejun Heo int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
3316c6fd2807SJeff Garzik {
33170260731fSTejun Heo 	struct ata_port *ap = link->ap;
3318c6fd2807SJeff Garzik 	struct ata_device *dev;
3319f58229f8STejun Heo 	int rc = 0, used_dma = 0, found = 0;
3320c6fd2807SJeff Garzik 
3321c6fd2807SJeff Garzik 	/* step 1: calculate xfer_mask */
3322f58229f8STejun Heo 	ata_link_for_each_dev(dev, link) {
33237dc951aeSTejun Heo 		unsigned long pio_mask, dma_mask;
3324b3a70601SAlan Cox 		unsigned int mode_mask;
3325c6fd2807SJeff Garzik 
3326c6fd2807SJeff Garzik 		if (!ata_dev_enabled(dev))
3327c6fd2807SJeff Garzik 			continue;
3328c6fd2807SJeff Garzik 
3329b3a70601SAlan Cox 		mode_mask = ATA_DMA_MASK_ATA;
3330b3a70601SAlan Cox 		if (dev->class == ATA_DEV_ATAPI)
3331b3a70601SAlan Cox 			mode_mask = ATA_DMA_MASK_ATAPI;
3332b3a70601SAlan Cox 		else if (ata_id_is_cfa(dev->id))
3333b3a70601SAlan Cox 			mode_mask = ATA_DMA_MASK_CFA;
3334b3a70601SAlan Cox 
3335c6fd2807SJeff Garzik 		ata_dev_xfermask(dev);
333633267325STejun Heo 		ata_force_xfermask(dev);
3337c6fd2807SJeff Garzik 
3338c6fd2807SJeff Garzik 		pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
3339c6fd2807SJeff Garzik 		dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
3340b3a70601SAlan Cox 
3341b3a70601SAlan Cox 		if (libata_dma_mask & mode_mask)
3342b3a70601SAlan Cox 			dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
3343b3a70601SAlan Cox 		else
3344b3a70601SAlan Cox 			dma_mask = 0;
3345b3a70601SAlan Cox 
3346c6fd2807SJeff Garzik 		dev->pio_mode = ata_xfer_mask2mode(pio_mask);
3347c6fd2807SJeff Garzik 		dev->dma_mode = ata_xfer_mask2mode(dma_mask);
3348c6fd2807SJeff Garzik 
3349c6fd2807SJeff Garzik 		found = 1;
3350b15b3ebaSAlan Cox 		if (ata_dma_enabled(dev))
3351c6fd2807SJeff Garzik 			used_dma = 1;
3352c6fd2807SJeff Garzik 	}
3353c6fd2807SJeff Garzik 	if (!found)
3354c6fd2807SJeff Garzik 		goto out;
3355c6fd2807SJeff Garzik 
3356c6fd2807SJeff Garzik 	/* step 2: always set host PIO timings */
3357f58229f8STejun Heo 	ata_link_for_each_dev(dev, link) {
3358c6fd2807SJeff Garzik 		if (!ata_dev_enabled(dev))
3359c6fd2807SJeff Garzik 			continue;
3360c6fd2807SJeff Garzik 
336170cd071eSTejun Heo 		if (dev->pio_mode == 0xff) {
3362c6fd2807SJeff Garzik 			ata_dev_printk(dev, KERN_WARNING, "no PIO support\n");
3363c6fd2807SJeff Garzik 			rc = -EINVAL;
3364c6fd2807SJeff Garzik 			goto out;
3365c6fd2807SJeff Garzik 		}
3366c6fd2807SJeff Garzik 
3367c6fd2807SJeff Garzik 		dev->xfer_mode = dev->pio_mode;
3368c6fd2807SJeff Garzik 		dev->xfer_shift = ATA_SHIFT_PIO;
3369c6fd2807SJeff Garzik 		if (ap->ops->set_piomode)
3370c6fd2807SJeff Garzik 			ap->ops->set_piomode(ap, dev);
3371c6fd2807SJeff Garzik 	}
3372c6fd2807SJeff Garzik 
3373c6fd2807SJeff Garzik 	/* step 3: set host DMA timings */
3374f58229f8STejun Heo 	ata_link_for_each_dev(dev, link) {
3375b15b3ebaSAlan Cox 		if (!ata_dev_enabled(dev) || !ata_dma_enabled(dev))
3376c6fd2807SJeff Garzik 			continue;
3377c6fd2807SJeff Garzik 
3378c6fd2807SJeff Garzik 		dev->xfer_mode = dev->dma_mode;
3379c6fd2807SJeff Garzik 		dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
3380c6fd2807SJeff Garzik 		if (ap->ops->set_dmamode)
3381c6fd2807SJeff Garzik 			ap->ops->set_dmamode(ap, dev);
3382c6fd2807SJeff Garzik 	}
3383c6fd2807SJeff Garzik 
3384c6fd2807SJeff Garzik 	/* step 4: update devices' xfer mode */
3385f58229f8STejun Heo 	ata_link_for_each_dev(dev, link) {
338618d90debSAlan 		/* don't update suspended devices' xfer mode */
33879666f400STejun Heo 		if (!ata_dev_enabled(dev))
3388c6fd2807SJeff Garzik 			continue;
3389c6fd2807SJeff Garzik 
3390c6fd2807SJeff Garzik 		rc = ata_dev_set_mode(dev);
3391c6fd2807SJeff Garzik 		if (rc)
3392c6fd2807SJeff Garzik 			goto out;
3393c6fd2807SJeff Garzik 	}
3394c6fd2807SJeff Garzik 
3395c6fd2807SJeff Garzik 	/* Record simplex status. If we selected DMA then the other
3396c6fd2807SJeff Garzik 	 * host channels are not permitted to do so.
3397c6fd2807SJeff Garzik 	 */
3398cca3974eSJeff Garzik 	if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
3399032af1ceSAlan 		ap->host->simplex_claimed = ap;
3400c6fd2807SJeff Garzik 
3401c6fd2807SJeff Garzik  out:
3402c6fd2807SJeff Garzik 	if (rc)
3403c6fd2807SJeff Garzik 		*r_failed_dev = dev;
3404c6fd2807SJeff Garzik 	return rc;
3405c6fd2807SJeff Garzik }
3406c6fd2807SJeff Garzik 
3407c6fd2807SJeff Garzik /**
3408aa2731adSTejun Heo  *	ata_wait_ready - wait for link to become ready
3409aa2731adSTejun Heo  *	@link: link to be waited on
3410aa2731adSTejun Heo  *	@deadline: deadline jiffies for the operation
3411aa2731adSTejun Heo  *	@check_ready: callback to check link readiness
3412aa2731adSTejun Heo  *
3413aa2731adSTejun Heo  *	Wait for @link to become ready.  @check_ready should return
3414aa2731adSTejun Heo  *	positive number if @link is ready, 0 if it isn't, -ENODEV if
3415aa2731adSTejun Heo  *	link doesn't seem to be occupied, other errno for other error
3416aa2731adSTejun Heo  *	conditions.
3417aa2731adSTejun Heo  *
3418aa2731adSTejun Heo  *	Transient -ENODEV conditions are allowed for
3419aa2731adSTejun Heo  *	ATA_TMOUT_FF_WAIT.
3420aa2731adSTejun Heo  *
3421aa2731adSTejun Heo  *	LOCKING:
3422aa2731adSTejun Heo  *	EH context.
3423aa2731adSTejun Heo  *
3424aa2731adSTejun Heo  *	RETURNS:
3425aa2731adSTejun Heo  *	0 if @linke is ready before @deadline; otherwise, -errno.
3426aa2731adSTejun Heo  */
3427aa2731adSTejun Heo int ata_wait_ready(struct ata_link *link, unsigned long deadline,
3428aa2731adSTejun Heo 		   int (*check_ready)(struct ata_link *link))
3429aa2731adSTejun Heo {
3430aa2731adSTejun Heo 	unsigned long start = jiffies;
3431341c2c95STejun Heo 	unsigned long nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT);
3432aa2731adSTejun Heo 	int warned = 0;
3433aa2731adSTejun Heo 
3434b1c72916STejun Heo 	/* Slave readiness can't be tested separately from master.  On
3435b1c72916STejun Heo 	 * M/S emulation configuration, this function should be called
3436b1c72916STejun Heo 	 * only on the master and it will handle both master and slave.
3437b1c72916STejun Heo 	 */
3438b1c72916STejun Heo 	WARN_ON(link == link->ap->slave_link);
3439b1c72916STejun Heo 
3440aa2731adSTejun Heo 	if (time_after(nodev_deadline, deadline))
3441aa2731adSTejun Heo 		nodev_deadline = deadline;
3442aa2731adSTejun Heo 
3443aa2731adSTejun Heo 	while (1) {
3444aa2731adSTejun Heo 		unsigned long now = jiffies;
3445aa2731adSTejun Heo 		int ready, tmp;
3446aa2731adSTejun Heo 
3447aa2731adSTejun Heo 		ready = tmp = check_ready(link);
3448aa2731adSTejun Heo 		if (ready > 0)
3449aa2731adSTejun Heo 			return 0;
3450aa2731adSTejun Heo 
3451aa2731adSTejun Heo 		/* -ENODEV could be transient.  Ignore -ENODEV if link
3452aa2731adSTejun Heo 		 * is online.  Also, some SATA devices take a long
3453aa2731adSTejun Heo 		 * time to clear 0xff after reset.  For example,
3454aa2731adSTejun Heo 		 * HHD424020F7SV00 iVDR needs >= 800ms while Quantum
3455aa2731adSTejun Heo 		 * GoVault needs even more than that.  Wait for
3456aa2731adSTejun Heo 		 * ATA_TMOUT_FF_WAIT on -ENODEV if link isn't offline.
3457aa2731adSTejun Heo 		 *
3458aa2731adSTejun Heo 		 * Note that some PATA controllers (pata_ali) explode
3459aa2731adSTejun Heo 		 * if status register is read more than once when
3460aa2731adSTejun Heo 		 * there's no device attached.
3461aa2731adSTejun Heo 		 */
3462aa2731adSTejun Heo 		if (ready == -ENODEV) {
3463aa2731adSTejun Heo 			if (ata_link_online(link))
3464aa2731adSTejun Heo 				ready = 0;
3465aa2731adSTejun Heo 			else if ((link->ap->flags & ATA_FLAG_SATA) &&
3466aa2731adSTejun Heo 				 !ata_link_offline(link) &&
3467aa2731adSTejun Heo 				 time_before(now, nodev_deadline))
3468aa2731adSTejun Heo 				ready = 0;
3469aa2731adSTejun Heo 		}
3470aa2731adSTejun Heo 
3471aa2731adSTejun Heo 		if (ready)
3472aa2731adSTejun Heo 			return ready;
3473aa2731adSTejun Heo 		if (time_after(now, deadline))
3474aa2731adSTejun Heo 			return -EBUSY;
3475aa2731adSTejun Heo 
3476aa2731adSTejun Heo 		if (!warned && time_after(now, start + 5 * HZ) &&
3477aa2731adSTejun Heo 		    (deadline - now > 3 * HZ)) {
3478aa2731adSTejun Heo 			ata_link_printk(link, KERN_WARNING,
3479aa2731adSTejun Heo 				"link is slow to respond, please be patient "
3480aa2731adSTejun Heo 				"(ready=%d)\n", tmp);
3481aa2731adSTejun Heo 			warned = 1;
3482aa2731adSTejun Heo 		}
3483aa2731adSTejun Heo 
3484aa2731adSTejun Heo 		msleep(50);
3485aa2731adSTejun Heo 	}
3486aa2731adSTejun Heo }
3487aa2731adSTejun Heo 
3488aa2731adSTejun Heo /**
3489aa2731adSTejun Heo  *	ata_wait_after_reset - wait for link to become ready after reset
3490aa2731adSTejun Heo  *	@link: link to be waited on
3491aa2731adSTejun Heo  *	@deadline: deadline jiffies for the operation
3492aa2731adSTejun Heo  *	@check_ready: callback to check link readiness
3493aa2731adSTejun Heo  *
3494aa2731adSTejun Heo  *	Wait for @link to become ready after reset.
3495aa2731adSTejun Heo  *
3496aa2731adSTejun Heo  *	LOCKING:
3497aa2731adSTejun Heo  *	EH context.
3498aa2731adSTejun Heo  *
3499aa2731adSTejun Heo  *	RETURNS:
3500aa2731adSTejun Heo  *	0 if @linke is ready before @deadline; otherwise, -errno.
3501aa2731adSTejun Heo  */
35022b4221bbSHarvey Harrison int ata_wait_after_reset(struct ata_link *link, unsigned long deadline,
3503aa2731adSTejun Heo 				int (*check_ready)(struct ata_link *link))
3504aa2731adSTejun Heo {
3505341c2c95STejun Heo 	msleep(ATA_WAIT_AFTER_RESET);
3506aa2731adSTejun Heo 
3507aa2731adSTejun Heo 	return ata_wait_ready(link, deadline, check_ready);
3508aa2731adSTejun Heo }
3509aa2731adSTejun Heo 
3510aa2731adSTejun Heo /**
3511936fd732STejun Heo  *	sata_link_debounce - debounce SATA phy status
3512936fd732STejun Heo  *	@link: ATA link to debounce SATA phy status for
3513c6fd2807SJeff Garzik  *	@params: timing parameters { interval, duratinon, timeout } in msec
3514d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
3515c6fd2807SJeff Garzik  *
3516936fd732STejun Heo *	Make sure SStatus of @link reaches stable state, determined by
3517c6fd2807SJeff Garzik  *	holding the same value where DET is not 1 for @duration polled
3518c6fd2807SJeff Garzik  *	every @interval, before @timeout.  Timeout constraints the
3519d4b2bab4STejun Heo  *	beginning of the stable state.  Because DET gets stuck at 1 on
3520d4b2bab4STejun Heo  *	some controllers after hot unplugging, this functions waits
3521c6fd2807SJeff Garzik  *	until timeout then returns 0 if DET is stable at 1.
3522c6fd2807SJeff Garzik  *
3523d4b2bab4STejun Heo  *	@timeout is further limited by @deadline.  The sooner of the
3524d4b2bab4STejun Heo  *	two is used.
3525d4b2bab4STejun Heo  *
3526c6fd2807SJeff Garzik  *	LOCKING:
3527c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3528c6fd2807SJeff Garzik  *
3529c6fd2807SJeff Garzik  *	RETURNS:
3530c6fd2807SJeff Garzik  *	0 on success, -errno on failure.
3531c6fd2807SJeff Garzik  */
3532936fd732STejun Heo int sata_link_debounce(struct ata_link *link, const unsigned long *params,
3533d4b2bab4STejun Heo 		       unsigned long deadline)
3534c6fd2807SJeff Garzik {
3535341c2c95STejun Heo 	unsigned long interval = params[0];
3536341c2c95STejun Heo 	unsigned long duration = params[1];
3537d4b2bab4STejun Heo 	unsigned long last_jiffies, t;
3538c6fd2807SJeff Garzik 	u32 last, cur;
3539c6fd2807SJeff Garzik 	int rc;
3540c6fd2807SJeff Garzik 
3541341c2c95STejun Heo 	t = ata_deadline(jiffies, params[2]);
3542d4b2bab4STejun Heo 	if (time_before(t, deadline))
3543d4b2bab4STejun Heo 		deadline = t;
3544d4b2bab4STejun Heo 
3545936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3546c6fd2807SJeff Garzik 		return rc;
3547c6fd2807SJeff Garzik 	cur &= 0xf;
3548c6fd2807SJeff Garzik 
3549c6fd2807SJeff Garzik 	last = cur;
3550c6fd2807SJeff Garzik 	last_jiffies = jiffies;
3551c6fd2807SJeff Garzik 
3552c6fd2807SJeff Garzik 	while (1) {
3553341c2c95STejun Heo 		msleep(interval);
3554936fd732STejun Heo 		if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3555c6fd2807SJeff Garzik 			return rc;
3556c6fd2807SJeff Garzik 		cur &= 0xf;
3557c6fd2807SJeff Garzik 
3558c6fd2807SJeff Garzik 		/* DET stable? */
3559c6fd2807SJeff Garzik 		if (cur == last) {
3560d4b2bab4STejun Heo 			if (cur == 1 && time_before(jiffies, deadline))
3561c6fd2807SJeff Garzik 				continue;
3562341c2c95STejun Heo 			if (time_after(jiffies,
3563341c2c95STejun Heo 				       ata_deadline(last_jiffies, duration)))
3564c6fd2807SJeff Garzik 				return 0;
3565c6fd2807SJeff Garzik 			continue;
3566c6fd2807SJeff Garzik 		}
3567c6fd2807SJeff Garzik 
3568c6fd2807SJeff Garzik 		/* unstable, start over */
3569c6fd2807SJeff Garzik 		last = cur;
3570c6fd2807SJeff Garzik 		last_jiffies = jiffies;
3571c6fd2807SJeff Garzik 
3572f1545154STejun Heo 		/* Check deadline.  If debouncing failed, return
3573f1545154STejun Heo 		 * -EPIPE to tell upper layer to lower link speed.
3574f1545154STejun Heo 		 */
3575d4b2bab4STejun Heo 		if (time_after(jiffies, deadline))
3576f1545154STejun Heo 			return -EPIPE;
3577c6fd2807SJeff Garzik 	}
3578c6fd2807SJeff Garzik }
3579c6fd2807SJeff Garzik 
3580c6fd2807SJeff Garzik /**
3581936fd732STejun Heo  *	sata_link_resume - resume SATA link
3582936fd732STejun Heo  *	@link: ATA link to resume SATA
3583c6fd2807SJeff Garzik  *	@params: timing parameters { interval, duratinon, timeout } in msec
3584d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
3585c6fd2807SJeff Garzik  *
3586936fd732STejun Heo  *	Resume SATA phy @link and debounce it.
3587c6fd2807SJeff Garzik  *
3588c6fd2807SJeff Garzik  *	LOCKING:
3589c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3590c6fd2807SJeff Garzik  *
3591c6fd2807SJeff Garzik  *	RETURNS:
3592c6fd2807SJeff Garzik  *	0 on success, -errno on failure.
3593c6fd2807SJeff Garzik  */
3594936fd732STejun Heo int sata_link_resume(struct ata_link *link, const unsigned long *params,
3595d4b2bab4STejun Heo 		     unsigned long deadline)
3596c6fd2807SJeff Garzik {
3597ac371987STejun Heo 	u32 scontrol, serror;
3598c6fd2807SJeff Garzik 	int rc;
3599c6fd2807SJeff Garzik 
3600936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3601c6fd2807SJeff Garzik 		return rc;
3602c6fd2807SJeff Garzik 
3603c6fd2807SJeff Garzik 	scontrol = (scontrol & 0x0f0) | 0x300;
3604c6fd2807SJeff Garzik 
3605936fd732STejun Heo 	if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3606c6fd2807SJeff Garzik 		return rc;
3607c6fd2807SJeff Garzik 
3608c6fd2807SJeff Garzik 	/* Some PHYs react badly if SStatus is pounded immediately
3609c6fd2807SJeff Garzik 	 * after resuming.  Delay 200ms before debouncing.
3610c6fd2807SJeff Garzik 	 */
3611c6fd2807SJeff Garzik 	msleep(200);
3612c6fd2807SJeff Garzik 
3613ac371987STejun Heo 	if ((rc = sata_link_debounce(link, params, deadline)))
3614ac371987STejun Heo 		return rc;
3615ac371987STejun Heo 
3616f046519fSTejun Heo 	/* clear SError, some PHYs require this even for SRST to work */
3617ac371987STejun Heo 	if (!(rc = sata_scr_read(link, SCR_ERROR, &serror)))
3618ac371987STejun Heo 		rc = sata_scr_write(link, SCR_ERROR, serror);
3619ac371987STejun Heo 
3620f046519fSTejun Heo 	return rc != -EINVAL ? rc : 0;
3621c6fd2807SJeff Garzik }
3622c6fd2807SJeff Garzik 
3623c6fd2807SJeff Garzik /**
36240aa1113dSTejun Heo  *	ata_std_prereset - prepare for reset
3625cc0680a5STejun Heo  *	@link: ATA link to be reset
3626d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
3627c6fd2807SJeff Garzik  *
3628cc0680a5STejun Heo  *	@link is about to be reset.  Initialize it.  Failure from
3629b8cffc6aSTejun Heo  *	prereset makes libata abort whole reset sequence and give up
3630b8cffc6aSTejun Heo  *	that port, so prereset should be best-effort.  It does its
3631b8cffc6aSTejun Heo  *	best to prepare for reset sequence but if things go wrong, it
3632b8cffc6aSTejun Heo  *	should just whine, not fail.
3633c6fd2807SJeff Garzik  *
3634c6fd2807SJeff Garzik  *	LOCKING:
3635c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3636c6fd2807SJeff Garzik  *
3637c6fd2807SJeff Garzik  *	RETURNS:
3638c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
3639c6fd2807SJeff Garzik  */
36400aa1113dSTejun Heo int ata_std_prereset(struct ata_link *link, unsigned long deadline)
3641c6fd2807SJeff Garzik {
3642cc0680a5STejun Heo 	struct ata_port *ap = link->ap;
3643936fd732STejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
3644c6fd2807SJeff Garzik 	const unsigned long *timing = sata_ehc_deb_timing(ehc);
3645c6fd2807SJeff Garzik 	int rc;
3646c6fd2807SJeff Garzik 
3647c6fd2807SJeff Garzik 	/* if we're about to do hardreset, nothing more to do */
3648c6fd2807SJeff Garzik 	if (ehc->i.action & ATA_EH_HARDRESET)
3649c6fd2807SJeff Garzik 		return 0;
3650c6fd2807SJeff Garzik 
3651936fd732STejun Heo 	/* if SATA, resume link */
3652a16abc0bSTejun Heo 	if (ap->flags & ATA_FLAG_SATA) {
3653936fd732STejun Heo 		rc = sata_link_resume(link, timing, deadline);
3654b8cffc6aSTejun Heo 		/* whine about phy resume failure but proceed */
3655b8cffc6aSTejun Heo 		if (rc && rc != -EOPNOTSUPP)
3656cc0680a5STejun Heo 			ata_link_printk(link, KERN_WARNING, "failed to resume "
3657c6fd2807SJeff Garzik 					"link for reset (errno=%d)\n", rc);
3658c6fd2807SJeff Garzik 	}
3659c6fd2807SJeff Garzik 
366045db2f6cSTejun Heo 	/* no point in trying softreset on offline link */
3661b1c72916STejun Heo 	if (ata_phys_link_offline(link))
366245db2f6cSTejun Heo 		ehc->i.action &= ~ATA_EH_SOFTRESET;
366345db2f6cSTejun Heo 
3664c6fd2807SJeff Garzik 	return 0;
3665c6fd2807SJeff Garzik }
3666c6fd2807SJeff Garzik 
3667c6fd2807SJeff Garzik /**
3668cc0680a5STejun Heo  *	sata_link_hardreset - reset link via SATA phy reset
3669cc0680a5STejun Heo  *	@link: link to reset
3670b6103f6dSTejun Heo  *	@timing: timing parameters { interval, duratinon, timeout } in msec
3671d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
36729dadd45bSTejun Heo  *	@online: optional out parameter indicating link onlineness
36739dadd45bSTejun Heo  *	@check_ready: optional callback to check link readiness
3674c6fd2807SJeff Garzik  *
3675cc0680a5STejun Heo  *	SATA phy-reset @link using DET bits of SControl register.
36769dadd45bSTejun Heo  *	After hardreset, link readiness is waited upon using
36779dadd45bSTejun Heo  *	ata_wait_ready() if @check_ready is specified.  LLDs are
36789dadd45bSTejun Heo  *	allowed to not specify @check_ready and wait itself after this
36799dadd45bSTejun Heo  *	function returns.  Device classification is LLD's
36809dadd45bSTejun Heo  *	responsibility.
36819dadd45bSTejun Heo  *
36829dadd45bSTejun Heo  *	*@online is set to one iff reset succeeded and @link is online
36839dadd45bSTejun Heo  *	after reset.
3684c6fd2807SJeff Garzik  *
3685c6fd2807SJeff Garzik  *	LOCKING:
3686c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3687c6fd2807SJeff Garzik  *
3688c6fd2807SJeff Garzik  *	RETURNS:
3689c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
3690c6fd2807SJeff Garzik  */
3691cc0680a5STejun Heo int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
36929dadd45bSTejun Heo 			unsigned long deadline,
36939dadd45bSTejun Heo 			bool *online, int (*check_ready)(struct ata_link *))
3694c6fd2807SJeff Garzik {
3695c6fd2807SJeff Garzik 	u32 scontrol;
3696c6fd2807SJeff Garzik 	int rc;
3697c6fd2807SJeff Garzik 
3698c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
3699c6fd2807SJeff Garzik 
37009dadd45bSTejun Heo 	if (online)
37019dadd45bSTejun Heo 		*online = false;
37029dadd45bSTejun Heo 
3703936fd732STejun Heo 	if (sata_set_spd_needed(link)) {
3704c6fd2807SJeff Garzik 		/* SATA spec says nothing about how to reconfigure
3705c6fd2807SJeff Garzik 		 * spd.  To be on the safe side, turn off phy during
3706c6fd2807SJeff Garzik 		 * reconfiguration.  This works for at least ICH7 AHCI
3707c6fd2807SJeff Garzik 		 * and Sil3124.
3708c6fd2807SJeff Garzik 		 */
3709936fd732STejun Heo 		if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3710b6103f6dSTejun Heo 			goto out;
3711c6fd2807SJeff Garzik 
3712cea0d336SJeff Garzik 		scontrol = (scontrol & 0x0f0) | 0x304;
3713c6fd2807SJeff Garzik 
3714936fd732STejun Heo 		if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3715b6103f6dSTejun Heo 			goto out;
3716c6fd2807SJeff Garzik 
3717936fd732STejun Heo 		sata_set_spd(link);
3718c6fd2807SJeff Garzik 	}
3719c6fd2807SJeff Garzik 
3720c6fd2807SJeff Garzik 	/* issue phy wake/reset */
3721936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3722b6103f6dSTejun Heo 		goto out;
3723c6fd2807SJeff Garzik 
3724c6fd2807SJeff Garzik 	scontrol = (scontrol & 0x0f0) | 0x301;
3725c6fd2807SJeff Garzik 
3726936fd732STejun Heo 	if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
3727b6103f6dSTejun Heo 		goto out;
3728c6fd2807SJeff Garzik 
3729c6fd2807SJeff Garzik 	/* Couldn't find anything in SATA I/II specs, but AHCI-1.1
3730c6fd2807SJeff Garzik 	 * 10.4.2 says at least 1 ms.
3731c6fd2807SJeff Garzik 	 */
3732c6fd2807SJeff Garzik 	msleep(1);
3733c6fd2807SJeff Garzik 
3734936fd732STejun Heo 	/* bring link back */
3735936fd732STejun Heo 	rc = sata_link_resume(link, timing, deadline);
37369dadd45bSTejun Heo 	if (rc)
37379dadd45bSTejun Heo 		goto out;
37389dadd45bSTejun Heo 	/* if link is offline nothing more to do */
3739b1c72916STejun Heo 	if (ata_phys_link_offline(link))
37409dadd45bSTejun Heo 		goto out;
37419dadd45bSTejun Heo 
37429dadd45bSTejun Heo 	/* Link is online.  From this point, -ENODEV too is an error. */
37439dadd45bSTejun Heo 	if (online)
37449dadd45bSTejun Heo 		*online = true;
37459dadd45bSTejun Heo 
3746071f44b1STejun Heo 	if (sata_pmp_supported(link->ap) && ata_is_host_link(link)) {
37479dadd45bSTejun Heo 		/* If PMP is supported, we have to do follow-up SRST.
37489dadd45bSTejun Heo 		 * Some PMPs don't send D2H Reg FIS after hardreset if
37499dadd45bSTejun Heo 		 * the first port is empty.  Wait only for
37509dadd45bSTejun Heo 		 * ATA_TMOUT_PMP_SRST_WAIT.
37519dadd45bSTejun Heo 		 */
37529dadd45bSTejun Heo 		if (check_ready) {
37539dadd45bSTejun Heo 			unsigned long pmp_deadline;
37549dadd45bSTejun Heo 
3755341c2c95STejun Heo 			pmp_deadline = ata_deadline(jiffies,
3756341c2c95STejun Heo 						    ATA_TMOUT_PMP_SRST_WAIT);
37579dadd45bSTejun Heo 			if (time_after(pmp_deadline, deadline))
37589dadd45bSTejun Heo 				pmp_deadline = deadline;
37599dadd45bSTejun Heo 			ata_wait_ready(link, pmp_deadline, check_ready);
37609dadd45bSTejun Heo 		}
37619dadd45bSTejun Heo 		rc = -EAGAIN;
37629dadd45bSTejun Heo 		goto out;
37639dadd45bSTejun Heo 	}
37649dadd45bSTejun Heo 
37659dadd45bSTejun Heo 	rc = 0;
37669dadd45bSTejun Heo 	if (check_ready)
37679dadd45bSTejun Heo 		rc = ata_wait_ready(link, deadline, check_ready);
3768b6103f6dSTejun Heo  out:
37690cbf0711STejun Heo 	if (rc && rc != -EAGAIN) {
37700cbf0711STejun Heo 		/* online is set iff link is online && reset succeeded */
37710cbf0711STejun Heo 		if (online)
37720cbf0711STejun Heo 			*online = false;
37739dadd45bSTejun Heo 		ata_link_printk(link, KERN_ERR,
37749dadd45bSTejun Heo 				"COMRESET failed (errno=%d)\n", rc);
37750cbf0711STejun Heo 	}
3776b6103f6dSTejun Heo 	DPRINTK("EXIT, rc=%d\n", rc);
3777b6103f6dSTejun Heo 	return rc;
3778b6103f6dSTejun Heo }
3779b6103f6dSTejun Heo 
3780b6103f6dSTejun Heo /**
378157c9efdfSTejun Heo  *	sata_std_hardreset - COMRESET w/o waiting or classification
378257c9efdfSTejun Heo  *	@link: link to reset
378357c9efdfSTejun Heo  *	@class: resulting class of attached device
378457c9efdfSTejun Heo  *	@deadline: deadline jiffies for the operation
378557c9efdfSTejun Heo  *
378657c9efdfSTejun Heo  *	Standard SATA COMRESET w/o waiting or classification.
378757c9efdfSTejun Heo  *
378857c9efdfSTejun Heo  *	LOCKING:
378957c9efdfSTejun Heo  *	Kernel thread context (may sleep)
379057c9efdfSTejun Heo  *
379157c9efdfSTejun Heo  *	RETURNS:
379257c9efdfSTejun Heo  *	0 if link offline, -EAGAIN if link online, -errno on errors.
379357c9efdfSTejun Heo  */
379457c9efdfSTejun Heo int sata_std_hardreset(struct ata_link *link, unsigned int *class,
379557c9efdfSTejun Heo 		       unsigned long deadline)
379657c9efdfSTejun Heo {
379757c9efdfSTejun Heo 	const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
379857c9efdfSTejun Heo 	bool online;
379957c9efdfSTejun Heo 	int rc;
380057c9efdfSTejun Heo 
380157c9efdfSTejun Heo 	/* do hardreset */
380257c9efdfSTejun Heo 	rc = sata_link_hardreset(link, timing, deadline, &online, NULL);
380357c9efdfSTejun Heo 	return online ? -EAGAIN : rc;
380457c9efdfSTejun Heo }
380557c9efdfSTejun Heo 
380657c9efdfSTejun Heo /**
3807203c75b8STejun Heo  *	ata_std_postreset - standard postreset callback
3808cc0680a5STejun Heo  *	@link: the target ata_link
3809c6fd2807SJeff Garzik  *	@classes: classes of attached devices
3810c6fd2807SJeff Garzik  *
3811c6fd2807SJeff Garzik  *	This function is invoked after a successful reset.  Note that
3812c6fd2807SJeff Garzik  *	the device might have been reset more than once using
3813c6fd2807SJeff Garzik  *	different reset methods before postreset is invoked.
3814c6fd2807SJeff Garzik  *
3815c6fd2807SJeff Garzik  *	LOCKING:
3816c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3817c6fd2807SJeff Garzik  */
3818203c75b8STejun Heo void ata_std_postreset(struct ata_link *link, unsigned int *classes)
3819c6fd2807SJeff Garzik {
3820f046519fSTejun Heo 	u32 serror;
3821f046519fSTejun Heo 
3822c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
3823c6fd2807SJeff Garzik 
3824f046519fSTejun Heo 	/* reset complete, clear SError */
3825f046519fSTejun Heo 	if (!sata_scr_read(link, SCR_ERROR, &serror))
3826f046519fSTejun Heo 		sata_scr_write(link, SCR_ERROR, serror);
3827f046519fSTejun Heo 
3828c6fd2807SJeff Garzik 	/* print link status */
3829936fd732STejun Heo 	sata_print_link_status(link);
3830c6fd2807SJeff Garzik 
3831c6fd2807SJeff Garzik 	DPRINTK("EXIT\n");
3832c6fd2807SJeff Garzik }
3833c6fd2807SJeff Garzik 
3834c6fd2807SJeff Garzik /**
3835c6fd2807SJeff Garzik  *	ata_dev_same_device - Determine whether new ID matches configured device
3836c6fd2807SJeff Garzik  *	@dev: device to compare against
3837c6fd2807SJeff Garzik  *	@new_class: class of the new device
3838c6fd2807SJeff Garzik  *	@new_id: IDENTIFY page of the new device
3839c6fd2807SJeff Garzik  *
3840c6fd2807SJeff Garzik  *	Compare @new_class and @new_id against @dev and determine
3841c6fd2807SJeff Garzik  *	whether @dev is the device indicated by @new_class and
3842c6fd2807SJeff Garzik  *	@new_id.
3843c6fd2807SJeff Garzik  *
3844c6fd2807SJeff Garzik  *	LOCKING:
3845c6fd2807SJeff Garzik  *	None.
3846c6fd2807SJeff Garzik  *
3847c6fd2807SJeff Garzik  *	RETURNS:
3848c6fd2807SJeff Garzik  *	1 if @dev matches @new_class and @new_id, 0 otherwise.
3849c6fd2807SJeff Garzik  */
3850c6fd2807SJeff Garzik static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
3851c6fd2807SJeff Garzik 			       const u16 *new_id)
3852c6fd2807SJeff Garzik {
3853c6fd2807SJeff Garzik 	const u16 *old_id = dev->id;
3854a0cf733bSTejun Heo 	unsigned char model[2][ATA_ID_PROD_LEN + 1];
3855a0cf733bSTejun Heo 	unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
3856c6fd2807SJeff Garzik 
3857c6fd2807SJeff Garzik 	if (dev->class != new_class) {
3858c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_INFO, "class mismatch %d != %d\n",
3859c6fd2807SJeff Garzik 			       dev->class, new_class);
3860c6fd2807SJeff Garzik 		return 0;
3861c6fd2807SJeff Garzik 	}
3862c6fd2807SJeff Garzik 
3863a0cf733bSTejun Heo 	ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
3864a0cf733bSTejun Heo 	ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
3865a0cf733bSTejun Heo 	ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
3866a0cf733bSTejun Heo 	ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
3867c6fd2807SJeff Garzik 
3868c6fd2807SJeff Garzik 	if (strcmp(model[0], model[1])) {
3869c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_INFO, "model number mismatch "
3870c6fd2807SJeff Garzik 			       "'%s' != '%s'\n", model[0], model[1]);
3871c6fd2807SJeff Garzik 		return 0;
3872c6fd2807SJeff Garzik 	}
3873c6fd2807SJeff Garzik 
3874c6fd2807SJeff Garzik 	if (strcmp(serial[0], serial[1])) {
3875c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_INFO, "serial number mismatch "
3876c6fd2807SJeff Garzik 			       "'%s' != '%s'\n", serial[0], serial[1]);
3877c6fd2807SJeff Garzik 		return 0;
3878c6fd2807SJeff Garzik 	}
3879c6fd2807SJeff Garzik 
3880c6fd2807SJeff Garzik 	return 1;
3881c6fd2807SJeff Garzik }
3882c6fd2807SJeff Garzik 
3883c6fd2807SJeff Garzik /**
3884fe30911bSTejun Heo  *	ata_dev_reread_id - Re-read IDENTIFY data
38853fae450cSHenrik Kretzschmar  *	@dev: target ATA device
3886bff04647STejun Heo  *	@readid_flags: read ID flags
3887c6fd2807SJeff Garzik  *
3888c6fd2807SJeff Garzik  *	Re-read IDENTIFY page and make sure @dev is still attached to
3889c6fd2807SJeff Garzik  *	the port.
3890c6fd2807SJeff Garzik  *
3891c6fd2807SJeff Garzik  *	LOCKING:
3892c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3893c6fd2807SJeff Garzik  *
3894c6fd2807SJeff Garzik  *	RETURNS:
3895c6fd2807SJeff Garzik  *	0 on success, negative errno otherwise
3896c6fd2807SJeff Garzik  */
3897fe30911bSTejun Heo int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
3898c6fd2807SJeff Garzik {
3899c6fd2807SJeff Garzik 	unsigned int class = dev->class;
39009af5c9c9STejun Heo 	u16 *id = (void *)dev->link->ap->sector_buf;
3901c6fd2807SJeff Garzik 	int rc;
3902c6fd2807SJeff Garzik 
3903c6fd2807SJeff Garzik 	/* read ID data */
3904bff04647STejun Heo 	rc = ata_dev_read_id(dev, &class, readid_flags, id);
3905c6fd2807SJeff Garzik 	if (rc)
3906fe30911bSTejun Heo 		return rc;
3907c6fd2807SJeff Garzik 
3908c6fd2807SJeff Garzik 	/* is the device still there? */
3909fe30911bSTejun Heo 	if (!ata_dev_same_device(dev, class, id))
3910fe30911bSTejun Heo 		return -ENODEV;
3911c6fd2807SJeff Garzik 
3912c6fd2807SJeff Garzik 	memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
3913fe30911bSTejun Heo 	return 0;
3914fe30911bSTejun Heo }
3915fe30911bSTejun Heo 
3916fe30911bSTejun Heo /**
3917fe30911bSTejun Heo  *	ata_dev_revalidate - Revalidate ATA device
3918fe30911bSTejun Heo  *	@dev: device to revalidate
3919422c9daaSTejun Heo  *	@new_class: new class code
3920fe30911bSTejun Heo  *	@readid_flags: read ID flags
3921fe30911bSTejun Heo  *
3922fe30911bSTejun Heo  *	Re-read IDENTIFY page, make sure @dev is still attached to the
3923fe30911bSTejun Heo  *	port and reconfigure it according to the new IDENTIFY page.
3924fe30911bSTejun Heo  *
3925fe30911bSTejun Heo  *	LOCKING:
3926fe30911bSTejun Heo  *	Kernel thread context (may sleep)
3927fe30911bSTejun Heo  *
3928fe30911bSTejun Heo  *	RETURNS:
3929fe30911bSTejun Heo  *	0 on success, negative errno otherwise
3930fe30911bSTejun Heo  */
3931422c9daaSTejun Heo int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
3932422c9daaSTejun Heo 		       unsigned int readid_flags)
3933fe30911bSTejun Heo {
39346ddcd3b0STejun Heo 	u64 n_sectors = dev->n_sectors;
3935fe30911bSTejun Heo 	int rc;
3936fe30911bSTejun Heo 
3937fe30911bSTejun Heo 	if (!ata_dev_enabled(dev))
3938fe30911bSTejun Heo 		return -ENODEV;
3939fe30911bSTejun Heo 
3940422c9daaSTejun Heo 	/* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
3941422c9daaSTejun Heo 	if (ata_class_enabled(new_class) &&
3942422c9daaSTejun Heo 	    new_class != ATA_DEV_ATA && new_class != ATA_DEV_ATAPI) {
3943422c9daaSTejun Heo 		ata_dev_printk(dev, KERN_INFO, "class mismatch %u != %u\n",
3944422c9daaSTejun Heo 			       dev->class, new_class);
3945422c9daaSTejun Heo 		rc = -ENODEV;
3946422c9daaSTejun Heo 		goto fail;
3947422c9daaSTejun Heo 	}
3948422c9daaSTejun Heo 
3949fe30911bSTejun Heo 	/* re-read ID */
3950fe30911bSTejun Heo 	rc = ata_dev_reread_id(dev, readid_flags);
3951fe30911bSTejun Heo 	if (rc)
3952fe30911bSTejun Heo 		goto fail;
3953c6fd2807SJeff Garzik 
3954c6fd2807SJeff Garzik 	/* configure device according to the new ID */
3955efdaedc4STejun Heo 	rc = ata_dev_configure(dev);
39566ddcd3b0STejun Heo 	if (rc)
39576ddcd3b0STejun Heo 		goto fail;
39586ddcd3b0STejun Heo 
39596ddcd3b0STejun Heo 	/* verify n_sectors hasn't changed */
3960b54eebd6STejun Heo 	if (dev->class == ATA_DEV_ATA && n_sectors &&
3961b54eebd6STejun Heo 	    dev->n_sectors != n_sectors) {
39626ddcd3b0STejun Heo 		ata_dev_printk(dev, KERN_INFO, "n_sectors mismatch "
39636ddcd3b0STejun Heo 			       "%llu != %llu\n",
39646ddcd3b0STejun Heo 			       (unsigned long long)n_sectors,
39656ddcd3b0STejun Heo 			       (unsigned long long)dev->n_sectors);
39668270bec4STejun Heo 
39678270bec4STejun Heo 		/* restore original n_sectors */
39688270bec4STejun Heo 		dev->n_sectors = n_sectors;
39698270bec4STejun Heo 
39706ddcd3b0STejun Heo 		rc = -ENODEV;
39716ddcd3b0STejun Heo 		goto fail;
39726ddcd3b0STejun Heo 	}
39736ddcd3b0STejun Heo 
3974c6fd2807SJeff Garzik 	return 0;
3975c6fd2807SJeff Garzik 
3976c6fd2807SJeff Garzik  fail:
3977c6fd2807SJeff Garzik 	ata_dev_printk(dev, KERN_ERR, "revalidation failed (errno=%d)\n", rc);
3978c6fd2807SJeff Garzik 	return rc;
3979c6fd2807SJeff Garzik }
3980c6fd2807SJeff Garzik 
39816919a0a6SAlan Cox struct ata_blacklist_entry {
39826919a0a6SAlan Cox 	const char *model_num;
39836919a0a6SAlan Cox 	const char *model_rev;
39846919a0a6SAlan Cox 	unsigned long horkage;
39856919a0a6SAlan Cox };
39866919a0a6SAlan Cox 
39876919a0a6SAlan Cox static const struct ata_blacklist_entry ata_device_blacklist [] = {
39886919a0a6SAlan Cox 	/* Devices with DMA related problems under Linux */
39896919a0a6SAlan Cox 	{ "WDC AC11000H",	NULL,		ATA_HORKAGE_NODMA },
39906919a0a6SAlan Cox 	{ "WDC AC22100H",	NULL,		ATA_HORKAGE_NODMA },
39916919a0a6SAlan Cox 	{ "WDC AC32500H",	NULL,		ATA_HORKAGE_NODMA },
39926919a0a6SAlan Cox 	{ "WDC AC33100H",	NULL,		ATA_HORKAGE_NODMA },
39936919a0a6SAlan Cox 	{ "WDC AC31600H",	NULL,		ATA_HORKAGE_NODMA },
39946919a0a6SAlan Cox 	{ "WDC AC32100H",	"24.09P07",	ATA_HORKAGE_NODMA },
39956919a0a6SAlan Cox 	{ "WDC AC23200L",	"21.10N21",	ATA_HORKAGE_NODMA },
39966919a0a6SAlan Cox 	{ "Compaq CRD-8241B", 	NULL,		ATA_HORKAGE_NODMA },
39976919a0a6SAlan Cox 	{ "CRD-8400B",		NULL, 		ATA_HORKAGE_NODMA },
39986919a0a6SAlan Cox 	{ "CRD-8480B",		NULL,		ATA_HORKAGE_NODMA },
39996919a0a6SAlan Cox 	{ "CRD-8482B",		NULL,		ATA_HORKAGE_NODMA },
40006919a0a6SAlan Cox 	{ "CRD-84",		NULL,		ATA_HORKAGE_NODMA },
40016919a0a6SAlan Cox 	{ "SanDisk SDP3B",	NULL,		ATA_HORKAGE_NODMA },
40026919a0a6SAlan Cox 	{ "SanDisk SDP3B-64",	NULL,		ATA_HORKAGE_NODMA },
40036919a0a6SAlan Cox 	{ "SANYO CD-ROM CRD",	NULL,		ATA_HORKAGE_NODMA },
40046919a0a6SAlan Cox 	{ "HITACHI CDR-8",	NULL,		ATA_HORKAGE_NODMA },
40056919a0a6SAlan Cox 	{ "HITACHI CDR-8335",	NULL,		ATA_HORKAGE_NODMA },
40066919a0a6SAlan Cox 	{ "HITACHI CDR-8435",	NULL,		ATA_HORKAGE_NODMA },
40076919a0a6SAlan Cox 	{ "Toshiba CD-ROM XM-6202B", NULL,	ATA_HORKAGE_NODMA },
40086919a0a6SAlan Cox 	{ "TOSHIBA CD-ROM XM-1702BC", NULL,	ATA_HORKAGE_NODMA },
40096919a0a6SAlan Cox 	{ "CD-532E-A", 		NULL,		ATA_HORKAGE_NODMA },
40106919a0a6SAlan Cox 	{ "E-IDE CD-ROM CR-840",NULL,		ATA_HORKAGE_NODMA },
40116919a0a6SAlan Cox 	{ "CD-ROM Drive/F5A",	NULL,		ATA_HORKAGE_NODMA },
40126919a0a6SAlan Cox 	{ "WPI CDD-820", 	NULL,		ATA_HORKAGE_NODMA },
40136919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SC-148C", NULL,	ATA_HORKAGE_NODMA },
40146919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SC",	NULL,		ATA_HORKAGE_NODMA },
40156919a0a6SAlan Cox 	{ "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
40166919a0a6SAlan Cox 	{ "_NEC DV5800A", 	NULL,		ATA_HORKAGE_NODMA },
40176919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SN-124", "N001",	ATA_HORKAGE_NODMA },
401839f19886SDave Jones 	{ "Seagate STT20000A", NULL,		ATA_HORKAGE_NODMA },
40193af9a77aSTejun Heo 	/* Odd clown on sil3726/4726 PMPs */
402050af2fa1STejun Heo 	{ "Config  Disk",	NULL,		ATA_HORKAGE_DISABLE },
40216919a0a6SAlan Cox 
402218d6e9d5SAlbert Lee 	/* Weird ATAPI devices */
402340a1d531STejun Heo 	{ "TORiSAN DVD-ROM DRD-N216", NULL,	ATA_HORKAGE_MAX_SEC_128 },
402418d6e9d5SAlbert Lee 
40256919a0a6SAlan Cox 	/* Devices we expect to fail diagnostics */
40266919a0a6SAlan Cox 
40276919a0a6SAlan Cox 	/* Devices where NCQ should be avoided */
40286919a0a6SAlan Cox 	/* NCQ is slow */
40296919a0a6SAlan Cox 	{ "WDC WD740ADFD-00",	NULL,		ATA_HORKAGE_NONCQ },
4030459ad688STejun Heo 	{ "WDC WD740ADFD-00NLR1", NULL,		ATA_HORKAGE_NONCQ, },
403109125ea6STejun Heo 	/* http://thread.gmane.org/gmane.linux.ide/14907 */
403209125ea6STejun Heo 	{ "FUJITSU MHT2060BH",	NULL,		ATA_HORKAGE_NONCQ },
40337acfaf30SPaul Rolland 	/* NCQ is broken */
4034539cc7c7SJeff Garzik 	{ "Maxtor *",		"BANC*",	ATA_HORKAGE_NONCQ },
40350e3dbc01SAlan Cox 	{ "Maxtor 7V300F0",	"VA111630",	ATA_HORKAGE_NONCQ },
4036da6f0ec2SPaolo Ornati 	{ "ST380817AS",		"3.42",		ATA_HORKAGE_NONCQ },
4037e41bd3e8STejun Heo 	{ "ST3160023AS",	"3.42",		ATA_HORKAGE_NONCQ },
4038539cc7c7SJeff Garzik 
403936e337d0SRobert Hancock 	/* Blacklist entries taken from Silicon Image 3124/3132
404036e337d0SRobert Hancock 	   Windows driver .inf file - also several Linux problem reports */
404136e337d0SRobert Hancock 	{ "HTS541060G9SA00",    "MB3OC60D",     ATA_HORKAGE_NONCQ, },
404236e337d0SRobert Hancock 	{ "HTS541080G9SA00",    "MB4OC60D",     ATA_HORKAGE_NONCQ, },
404336e337d0SRobert Hancock 	{ "HTS541010G9SA00",    "MBZOC60D",     ATA_HORKAGE_NONCQ, },
40446919a0a6SAlan Cox 
404516c55b03STejun Heo 	/* devices which puke on READ_NATIVE_MAX */
404616c55b03STejun Heo 	{ "HDS724040KLSA80",	"KFAOA20N",	ATA_HORKAGE_BROKEN_HPA, },
404716c55b03STejun Heo 	{ "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
404816c55b03STejun Heo 	{ "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
404916c55b03STejun Heo 	{ "MAXTOR 6L080L4",	"A93.0500",	ATA_HORKAGE_BROKEN_HPA },
40506919a0a6SAlan Cox 
405193328e11SAlan Cox 	/* Devices which report 1 sector over size HPA */
405293328e11SAlan Cox 	{ "ST340823A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
405393328e11SAlan Cox 	{ "ST320413A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
4054b152fcd3SMikko Rapeli 	{ "ST310211A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
405593328e11SAlan Cox 
40566bbfd53dSAlan Cox 	/* Devices which get the IVB wrong */
40576bbfd53dSAlan Cox 	{ "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB, },
4058a79067e5SAlan Cox 	/* Maybe we should just blacklist TSSTcorp... */
4059a79067e5SAlan Cox 	{ "TSSTcorp CDDVDW SH-S202H", "SB00",	  ATA_HORKAGE_IVB, },
4060a79067e5SAlan Cox 	{ "TSSTcorp CDDVDW SH-S202H", "SB01",	  ATA_HORKAGE_IVB, },
40616bbfd53dSAlan Cox 	{ "TSSTcorp CDDVDW SH-S202J", "SB00",	  ATA_HORKAGE_IVB, },
4062e9f33406SPeter Missel 	{ "TSSTcorp CDDVDW SH-S202J", "SB01",	  ATA_HORKAGE_IVB, },
4063e9f33406SPeter Missel 	{ "TSSTcorp CDDVDW SH-S202N", "SB00",	  ATA_HORKAGE_IVB, },
4064e9f33406SPeter Missel 	{ "TSSTcorp CDDVDW SH-S202N", "SB01",	  ATA_HORKAGE_IVB, },
40656bbfd53dSAlan Cox 
40666919a0a6SAlan Cox 	/* End Marker */
40676919a0a6SAlan Cox 	{ }
4068c6fd2807SJeff Garzik };
4069c6fd2807SJeff Garzik 
4070741b7763SAdrian Bunk static int strn_pattern_cmp(const char *patt, const char *name, int wildchar)
4071539cc7c7SJeff Garzik {
4072539cc7c7SJeff Garzik 	const char *p;
4073539cc7c7SJeff Garzik 	int len;
4074539cc7c7SJeff Garzik 
4075539cc7c7SJeff Garzik 	/*
4076539cc7c7SJeff Garzik 	 * check for trailing wildcard: *\0
4077539cc7c7SJeff Garzik 	 */
4078539cc7c7SJeff Garzik 	p = strchr(patt, wildchar);
4079539cc7c7SJeff Garzik 	if (p && ((*(p + 1)) == 0))
4080539cc7c7SJeff Garzik 		len = p - patt;
4081317b50b8SAndrew Paprocki 	else {
4082539cc7c7SJeff Garzik 		len = strlen(name);
4083317b50b8SAndrew Paprocki 		if (!len) {
4084317b50b8SAndrew Paprocki 			if (!*patt)
4085317b50b8SAndrew Paprocki 				return 0;
4086317b50b8SAndrew Paprocki 			return -1;
4087317b50b8SAndrew Paprocki 		}
4088317b50b8SAndrew Paprocki 	}
4089539cc7c7SJeff Garzik 
4090539cc7c7SJeff Garzik 	return strncmp(patt, name, len);
4091539cc7c7SJeff Garzik }
4092539cc7c7SJeff Garzik 
409375683fe7STejun Heo static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
4094c6fd2807SJeff Garzik {
40958bfa79fcSTejun Heo 	unsigned char model_num[ATA_ID_PROD_LEN + 1];
40968bfa79fcSTejun Heo 	unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
40976919a0a6SAlan Cox 	const struct ata_blacklist_entry *ad = ata_device_blacklist;
4098c6fd2807SJeff Garzik 
40998bfa79fcSTejun Heo 	ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
41008bfa79fcSTejun Heo 	ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
4101c6fd2807SJeff Garzik 
41026919a0a6SAlan Cox 	while (ad->model_num) {
4103539cc7c7SJeff Garzik 		if (!strn_pattern_cmp(ad->model_num, model_num, '*')) {
41046919a0a6SAlan Cox 			if (ad->model_rev == NULL)
41056919a0a6SAlan Cox 				return ad->horkage;
4106539cc7c7SJeff Garzik 			if (!strn_pattern_cmp(ad->model_rev, model_rev, '*'))
41076919a0a6SAlan Cox 				return ad->horkage;
4108c6fd2807SJeff Garzik 		}
41096919a0a6SAlan Cox 		ad++;
4110c6fd2807SJeff Garzik 	}
4111c6fd2807SJeff Garzik 	return 0;
4112c6fd2807SJeff Garzik }
4113c6fd2807SJeff Garzik 
41146919a0a6SAlan Cox static int ata_dma_blacklisted(const struct ata_device *dev)
41156919a0a6SAlan Cox {
41166919a0a6SAlan Cox 	/* We don't support polling DMA.
41176919a0a6SAlan Cox 	 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
41186919a0a6SAlan Cox 	 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
41196919a0a6SAlan Cox 	 */
41209af5c9c9STejun Heo 	if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
41216919a0a6SAlan Cox 	    (dev->flags & ATA_DFLAG_CDB_INTR))
41226919a0a6SAlan Cox 		return 1;
412375683fe7STejun Heo 	return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
41246919a0a6SAlan Cox }
41256919a0a6SAlan Cox 
4126c6fd2807SJeff Garzik /**
41276bbfd53dSAlan Cox  *	ata_is_40wire		-	check drive side detection
41286bbfd53dSAlan Cox  *	@dev: device
41296bbfd53dSAlan Cox  *
41306bbfd53dSAlan Cox  *	Perform drive side detection decoding, allowing for device vendors
41316bbfd53dSAlan Cox  *	who can't follow the documentation.
41326bbfd53dSAlan Cox  */
41336bbfd53dSAlan Cox 
41346bbfd53dSAlan Cox static int ata_is_40wire(struct ata_device *dev)
41356bbfd53dSAlan Cox {
41366bbfd53dSAlan Cox 	if (dev->horkage & ATA_HORKAGE_IVB)
41376bbfd53dSAlan Cox 		return ata_drive_40wire_relaxed(dev->id);
41386bbfd53dSAlan Cox 	return ata_drive_40wire(dev->id);
41396bbfd53dSAlan Cox }
41406bbfd53dSAlan Cox 
41416bbfd53dSAlan Cox /**
414215a5551cSAlan Cox  *	cable_is_40wire		-	40/80/SATA decider
414315a5551cSAlan Cox  *	@ap: port to consider
414415a5551cSAlan Cox  *
414515a5551cSAlan Cox  *	This function encapsulates the policy for speed management
414615a5551cSAlan Cox  *	in one place. At the moment we don't cache the result but
414715a5551cSAlan Cox  *	there is a good case for setting ap->cbl to the result when
414815a5551cSAlan Cox  *	we are called with unknown cables (and figuring out if it
414915a5551cSAlan Cox  *	impacts hotplug at all).
415015a5551cSAlan Cox  *
415115a5551cSAlan Cox  *	Return 1 if the cable appears to be 40 wire.
415215a5551cSAlan Cox  */
415315a5551cSAlan Cox 
415415a5551cSAlan Cox static int cable_is_40wire(struct ata_port *ap)
415515a5551cSAlan Cox {
415615a5551cSAlan Cox 	struct ata_link *link;
415715a5551cSAlan Cox 	struct ata_device *dev;
415815a5551cSAlan Cox 
415915a5551cSAlan Cox 	/* If the controller thinks we are 40 wire, we are */
416015a5551cSAlan Cox 	if (ap->cbl == ATA_CBL_PATA40)
416115a5551cSAlan Cox 		return 1;
416215a5551cSAlan Cox 	/* If the controller thinks we are 80 wire, we are */
416315a5551cSAlan Cox 	if (ap->cbl == ATA_CBL_PATA80 || ap->cbl == ATA_CBL_SATA)
416415a5551cSAlan Cox 		return 0;
4165f792068eSAlan Cox 	/* If the system is known to be 40 wire short cable (eg laptop),
4166f792068eSAlan Cox 	   then we allow 80 wire modes even if the drive isn't sure */
4167f792068eSAlan Cox 	if (ap->cbl == ATA_CBL_PATA40_SHORT)
4168f792068eSAlan Cox 		return 0;
416915a5551cSAlan Cox 	/* If the controller doesn't know we scan
417015a5551cSAlan Cox 
417115a5551cSAlan Cox 	   - Note: We look for all 40 wire detects at this point.
417215a5551cSAlan Cox 	     Any 80 wire detect is taken to be 80 wire cable
417315a5551cSAlan Cox 	     because
417415a5551cSAlan Cox 	     - In many setups only the one drive (slave if present)
417515a5551cSAlan Cox                will give a valid detect
417615a5551cSAlan Cox              - If you have a non detect capable drive you don't
417715a5551cSAlan Cox                want it to colour the choice
417815a5551cSAlan Cox         */
417915a5551cSAlan Cox 	ata_port_for_each_link(link, ap) {
418015a5551cSAlan Cox 		ata_link_for_each_dev(dev, link) {
418115a5551cSAlan Cox 			if (!ata_is_40wire(dev))
418215a5551cSAlan Cox 				return 0;
418315a5551cSAlan Cox 		}
418415a5551cSAlan Cox 	}
418515a5551cSAlan Cox 	return 1;
418615a5551cSAlan Cox }
418715a5551cSAlan Cox 
418815a5551cSAlan Cox /**
4189c6fd2807SJeff Garzik  *	ata_dev_xfermask - Compute supported xfermask of the given device
4190c6fd2807SJeff Garzik  *	@dev: Device to compute xfermask for
4191c6fd2807SJeff Garzik  *
4192c6fd2807SJeff Garzik  *	Compute supported xfermask of @dev and store it in
4193c6fd2807SJeff Garzik  *	dev->*_mask.  This function is responsible for applying all
4194c6fd2807SJeff Garzik  *	known limits including host controller limits, device
4195c6fd2807SJeff Garzik  *	blacklist, etc...
4196c6fd2807SJeff Garzik  *
4197c6fd2807SJeff Garzik  *	LOCKING:
4198c6fd2807SJeff Garzik  *	None.
4199c6fd2807SJeff Garzik  */
4200c6fd2807SJeff Garzik static void ata_dev_xfermask(struct ata_device *dev)
4201c6fd2807SJeff Garzik {
42029af5c9c9STejun Heo 	struct ata_link *link = dev->link;
42039af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
4204cca3974eSJeff Garzik 	struct ata_host *host = ap->host;
4205c6fd2807SJeff Garzik 	unsigned long xfer_mask;
4206c6fd2807SJeff Garzik 
4207c6fd2807SJeff Garzik 	/* controller modes available */
4208c6fd2807SJeff Garzik 	xfer_mask = ata_pack_xfermask(ap->pio_mask,
4209c6fd2807SJeff Garzik 				      ap->mwdma_mask, ap->udma_mask);
4210c6fd2807SJeff Garzik 
42118343f889SRobert Hancock 	/* drive modes available */
4212c6fd2807SJeff Garzik 	xfer_mask &= ata_pack_xfermask(dev->pio_mask,
4213c6fd2807SJeff Garzik 				       dev->mwdma_mask, dev->udma_mask);
4214c6fd2807SJeff Garzik 	xfer_mask &= ata_id_xfermask(dev->id);
4215c6fd2807SJeff Garzik 
4216b352e57dSAlan Cox 	/*
4217b352e57dSAlan Cox 	 *	CFA Advanced TrueIDE timings are not allowed on a shared
4218b352e57dSAlan Cox 	 *	cable
4219b352e57dSAlan Cox 	 */
4220b352e57dSAlan Cox 	if (ata_dev_pair(dev)) {
4221b352e57dSAlan Cox 		/* No PIO5 or PIO6 */
4222b352e57dSAlan Cox 		xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
4223b352e57dSAlan Cox 		/* No MWDMA3 or MWDMA 4 */
4224b352e57dSAlan Cox 		xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
4225b352e57dSAlan Cox 	}
4226b352e57dSAlan Cox 
4227c6fd2807SJeff Garzik 	if (ata_dma_blacklisted(dev)) {
4228c6fd2807SJeff Garzik 		xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4229c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_WARNING,
4230c6fd2807SJeff Garzik 			       "device is on DMA blacklist, disabling DMA\n");
4231c6fd2807SJeff Garzik 	}
4232c6fd2807SJeff Garzik 
423314d66ab7SPetr Vandrovec 	if ((host->flags & ATA_HOST_SIMPLEX) &&
423414d66ab7SPetr Vandrovec 	    host->simplex_claimed && host->simplex_claimed != ap) {
4235c6fd2807SJeff Garzik 		xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4236c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_WARNING, "simplex DMA is claimed by "
4237c6fd2807SJeff Garzik 			       "other device, disabling DMA\n");
4238c6fd2807SJeff Garzik 	}
4239c6fd2807SJeff Garzik 
4240e424675fSJeff Garzik 	if (ap->flags & ATA_FLAG_NO_IORDY)
4241e424675fSJeff Garzik 		xfer_mask &= ata_pio_mask_no_iordy(dev);
4242e424675fSJeff Garzik 
4243c6fd2807SJeff Garzik 	if (ap->ops->mode_filter)
4244a76b62caSAlan Cox 		xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
4245c6fd2807SJeff Garzik 
42468343f889SRobert Hancock 	/* Apply cable rule here.  Don't apply it early because when
42478343f889SRobert Hancock 	 * we handle hot plug the cable type can itself change.
42488343f889SRobert Hancock 	 * Check this last so that we know if the transfer rate was
42498343f889SRobert Hancock 	 * solely limited by the cable.
42508343f889SRobert Hancock 	 * Unknown or 80 wire cables reported host side are checked
42518343f889SRobert Hancock 	 * drive side as well. Cases where we know a 40wire cable
42528343f889SRobert Hancock 	 * is used safely for 80 are not checked here.
42538343f889SRobert Hancock 	 */
42548343f889SRobert Hancock 	if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
42558343f889SRobert Hancock 		/* UDMA/44 or higher would be available */
425615a5551cSAlan Cox 		if (cable_is_40wire(ap)) {
42578343f889SRobert Hancock 			ata_dev_printk(dev, KERN_WARNING,
42588343f889SRobert Hancock 				 "limited to UDMA/33 due to 40-wire cable\n");
42598343f889SRobert Hancock 			xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
42608343f889SRobert Hancock 		}
42618343f889SRobert Hancock 
4262c6fd2807SJeff Garzik 	ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
4263c6fd2807SJeff Garzik 			    &dev->mwdma_mask, &dev->udma_mask);
4264c6fd2807SJeff Garzik }
4265c6fd2807SJeff Garzik 
4266c6fd2807SJeff Garzik /**
4267c6fd2807SJeff Garzik  *	ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
4268c6fd2807SJeff Garzik  *	@dev: Device to which command will be sent
4269c6fd2807SJeff Garzik  *
4270c6fd2807SJeff Garzik  *	Issue SET FEATURES - XFER MODE command to device @dev
4271c6fd2807SJeff Garzik  *	on port @ap.
4272c6fd2807SJeff Garzik  *
4273c6fd2807SJeff Garzik  *	LOCKING:
4274c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
4275c6fd2807SJeff Garzik  *
4276c6fd2807SJeff Garzik  *	RETURNS:
4277c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
4278c6fd2807SJeff Garzik  */
4279c6fd2807SJeff Garzik 
4280c6fd2807SJeff Garzik static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
4281c6fd2807SJeff Garzik {
4282c6fd2807SJeff Garzik 	struct ata_taskfile tf;
4283c6fd2807SJeff Garzik 	unsigned int err_mask;
4284c6fd2807SJeff Garzik 
4285c6fd2807SJeff Garzik 	/* set up set-features taskfile */
4286c6fd2807SJeff Garzik 	DPRINTK("set features - xfer mode\n");
4287c6fd2807SJeff Garzik 
4288464cf177STejun Heo 	/* Some controllers and ATAPI devices show flaky interrupt
4289464cf177STejun Heo 	 * behavior after setting xfer mode.  Use polling instead.
4290464cf177STejun Heo 	 */
4291c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
4292c6fd2807SJeff Garzik 	tf.command = ATA_CMD_SET_FEATURES;
4293c6fd2807SJeff Garzik 	tf.feature = SETFEATURES_XFER;
4294464cf177STejun Heo 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
4295c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
4296b9f8ab2dSAlan Cox 	/* If we are using IORDY we must send the mode setting command */
4297b9f8ab2dSAlan Cox 	if (ata_pio_need_iordy(dev))
4298c6fd2807SJeff Garzik 		tf.nsect = dev->xfer_mode;
4299b9f8ab2dSAlan Cox 	/* If the device has IORDY and the controller does not - turn it off */
4300b9f8ab2dSAlan Cox  	else if (ata_id_has_iordy(dev->id))
4301b9f8ab2dSAlan Cox 		tf.nsect = 0x01;
4302b9f8ab2dSAlan Cox 	else /* In the ancient relic department - skip all of this */
4303b9f8ab2dSAlan Cox 		return 0;
4304c6fd2807SJeff Garzik 
43052b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
4306c6fd2807SJeff Garzik 
4307c6fd2807SJeff Garzik 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
4308c6fd2807SJeff Garzik 	return err_mask;
4309c6fd2807SJeff Garzik }
4310c6fd2807SJeff Garzik /**
4311218f3d30SJeff Garzik  *	ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES
43129f45cbd3SKristen Carlson Accardi  *	@dev: Device to which command will be sent
43139f45cbd3SKristen Carlson Accardi  *	@enable: Whether to enable or disable the feature
4314218f3d30SJeff Garzik  *	@feature: The sector count represents the feature to set
43159f45cbd3SKristen Carlson Accardi  *
43169f45cbd3SKristen Carlson Accardi  *	Issue SET FEATURES - SATA FEATURES command to device @dev
4317218f3d30SJeff Garzik  *	on port @ap with sector count
43189f45cbd3SKristen Carlson Accardi  *
43199f45cbd3SKristen Carlson Accardi  *	LOCKING:
43209f45cbd3SKristen Carlson Accardi  *	PCI/etc. bus probe sem.
43219f45cbd3SKristen Carlson Accardi  *
43229f45cbd3SKristen Carlson Accardi  *	RETURNS:
43239f45cbd3SKristen Carlson Accardi  *	0 on success, AC_ERR_* mask otherwise.
43249f45cbd3SKristen Carlson Accardi  */
4325218f3d30SJeff Garzik static unsigned int ata_dev_set_feature(struct ata_device *dev, u8 enable,
4326218f3d30SJeff Garzik 					u8 feature)
43279f45cbd3SKristen Carlson Accardi {
43289f45cbd3SKristen Carlson Accardi 	struct ata_taskfile tf;
43299f45cbd3SKristen Carlson Accardi 	unsigned int err_mask;
43309f45cbd3SKristen Carlson Accardi 
43319f45cbd3SKristen Carlson Accardi 	/* set up set-features taskfile */
43329f45cbd3SKristen Carlson Accardi 	DPRINTK("set features - SATA features\n");
43339f45cbd3SKristen Carlson Accardi 
43349f45cbd3SKristen Carlson Accardi 	ata_tf_init(dev, &tf);
43359f45cbd3SKristen Carlson Accardi 	tf.command = ATA_CMD_SET_FEATURES;
43369f45cbd3SKristen Carlson Accardi 	tf.feature = enable;
43379f45cbd3SKristen Carlson Accardi 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
43389f45cbd3SKristen Carlson Accardi 	tf.protocol = ATA_PROT_NODATA;
4339218f3d30SJeff Garzik 	tf.nsect = feature;
43409f45cbd3SKristen Carlson Accardi 
43412b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
43429f45cbd3SKristen Carlson Accardi 
43439f45cbd3SKristen Carlson Accardi 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
43449f45cbd3SKristen Carlson Accardi 	return err_mask;
43459f45cbd3SKristen Carlson Accardi }
43469f45cbd3SKristen Carlson Accardi 
43479f45cbd3SKristen Carlson Accardi /**
4348c6fd2807SJeff Garzik  *	ata_dev_init_params - Issue INIT DEV PARAMS command
4349c6fd2807SJeff Garzik  *	@dev: Device to which command will be sent
4350c6fd2807SJeff Garzik  *	@heads: Number of heads (taskfile parameter)
4351c6fd2807SJeff Garzik  *	@sectors: Number of sectors (taskfile parameter)
4352c6fd2807SJeff Garzik  *
4353c6fd2807SJeff Garzik  *	LOCKING:
4354c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
4355c6fd2807SJeff Garzik  *
4356c6fd2807SJeff Garzik  *	RETURNS:
4357c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
4358c6fd2807SJeff Garzik  */
4359c6fd2807SJeff Garzik static unsigned int ata_dev_init_params(struct ata_device *dev,
4360c6fd2807SJeff Garzik 					u16 heads, u16 sectors)
4361c6fd2807SJeff Garzik {
4362c6fd2807SJeff Garzik 	struct ata_taskfile tf;
4363c6fd2807SJeff Garzik 	unsigned int err_mask;
4364c6fd2807SJeff Garzik 
4365c6fd2807SJeff Garzik 	/* Number of sectors per track 1-255. Number of heads 1-16 */
4366c6fd2807SJeff Garzik 	if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
4367c6fd2807SJeff Garzik 		return AC_ERR_INVALID;
4368c6fd2807SJeff Garzik 
4369c6fd2807SJeff Garzik 	/* set up init dev params taskfile */
4370c6fd2807SJeff Garzik 	DPRINTK("init dev params \n");
4371c6fd2807SJeff Garzik 
4372c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
4373c6fd2807SJeff Garzik 	tf.command = ATA_CMD_INIT_DEV_PARAMS;
4374c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4375c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
4376c6fd2807SJeff Garzik 	tf.nsect = sectors;
4377c6fd2807SJeff Garzik 	tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
4378c6fd2807SJeff Garzik 
43792b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
438018b2466cSAlan Cox 	/* A clean abort indicates an original or just out of spec drive
438118b2466cSAlan Cox 	   and we should continue as we issue the setup based on the
438218b2466cSAlan Cox 	   drive reported working geometry */
438318b2466cSAlan Cox 	if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
438418b2466cSAlan Cox 		err_mask = 0;
4385c6fd2807SJeff Garzik 
4386c6fd2807SJeff Garzik 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
4387c6fd2807SJeff Garzik 	return err_mask;
4388c6fd2807SJeff Garzik }
4389c6fd2807SJeff Garzik 
4390c6fd2807SJeff Garzik /**
4391c6fd2807SJeff Garzik  *	ata_sg_clean - Unmap DMA memory associated with command
4392c6fd2807SJeff Garzik  *	@qc: Command containing DMA memory to be released
4393c6fd2807SJeff Garzik  *
4394c6fd2807SJeff Garzik  *	Unmap all mapped DMA memory associated with this command.
4395c6fd2807SJeff Garzik  *
4396c6fd2807SJeff Garzik  *	LOCKING:
4397cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4398c6fd2807SJeff Garzik  */
439970e6ad0cSTejun Heo void ata_sg_clean(struct ata_queued_cmd *qc)
4400c6fd2807SJeff Garzik {
4401c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4402ff2aeb1eSTejun Heo 	struct scatterlist *sg = qc->sg;
4403c6fd2807SJeff Garzik 	int dir = qc->dma_dir;
4404c6fd2807SJeff Garzik 
4405c6fd2807SJeff Garzik 	WARN_ON(sg == NULL);
4406c6fd2807SJeff Garzik 
4407dde20207SJames Bottomley 	VPRINTK("unmapping %u sg elements\n", qc->n_elem);
4408c6fd2807SJeff Garzik 
4409dde20207SJames Bottomley 	if (qc->n_elem)
4410dde20207SJames Bottomley 		dma_unmap_sg(ap->dev, sg, qc->n_elem, dir);
4411c6fd2807SJeff Garzik 
4412c6fd2807SJeff Garzik 	qc->flags &= ~ATA_QCFLAG_DMAMAP;
4413ff2aeb1eSTejun Heo 	qc->sg = NULL;
4414c6fd2807SJeff Garzik }
4415c6fd2807SJeff Garzik 
4416c6fd2807SJeff Garzik /**
44175895ef9aSTejun Heo  *	atapi_check_dma - Check whether ATAPI DMA can be supported
4418c6fd2807SJeff Garzik  *	@qc: Metadata associated with taskfile to check
4419c6fd2807SJeff Garzik  *
4420c6fd2807SJeff Garzik  *	Allow low-level driver to filter ATA PACKET commands, returning
4421c6fd2807SJeff Garzik  *	a status indicating whether or not it is OK to use DMA for the
4422c6fd2807SJeff Garzik  *	supplied PACKET command.
4423c6fd2807SJeff Garzik  *
4424c6fd2807SJeff Garzik  *	LOCKING:
4425cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4426c6fd2807SJeff Garzik  *
4427c6fd2807SJeff Garzik  *	RETURNS: 0 when ATAPI DMA can be used
4428c6fd2807SJeff Garzik  *               nonzero otherwise
4429c6fd2807SJeff Garzik  */
44305895ef9aSTejun Heo int atapi_check_dma(struct ata_queued_cmd *qc)
4431c6fd2807SJeff Garzik {
4432c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4433c6fd2807SJeff Garzik 
4434b9a4197eSTejun Heo 	/* Don't allow DMA if it isn't multiple of 16 bytes.  Quite a
4435b9a4197eSTejun Heo 	 * few ATAPI devices choke on such DMA requests.
4436b9a4197eSTejun Heo 	 */
4437b9a4197eSTejun Heo 	if (unlikely(qc->nbytes & 15))
44386f23a31dSAlbert Lee 		return 1;
44396f23a31dSAlbert Lee 
4440c6fd2807SJeff Garzik 	if (ap->ops->check_atapi_dma)
4441b9a4197eSTejun Heo 		return ap->ops->check_atapi_dma(qc);
4442c6fd2807SJeff Garzik 
4443b9a4197eSTejun Heo 	return 0;
4444c6fd2807SJeff Garzik }
4445b9a4197eSTejun Heo 
4446c6fd2807SJeff Garzik /**
444731cc23b3STejun Heo  *	ata_std_qc_defer - Check whether a qc needs to be deferred
444831cc23b3STejun Heo  *	@qc: ATA command in question
444931cc23b3STejun Heo  *
445031cc23b3STejun Heo  *	Non-NCQ commands cannot run with any other command, NCQ or
445131cc23b3STejun Heo  *	not.  As upper layer only knows the queue depth, we are
445231cc23b3STejun Heo  *	responsible for maintaining exclusion.  This function checks
445331cc23b3STejun Heo  *	whether a new command @qc can be issued.
445431cc23b3STejun Heo  *
445531cc23b3STejun Heo  *	LOCKING:
445631cc23b3STejun Heo  *	spin_lock_irqsave(host lock)
445731cc23b3STejun Heo  *
445831cc23b3STejun Heo  *	RETURNS:
445931cc23b3STejun Heo  *	ATA_DEFER_* if deferring is needed, 0 otherwise.
446031cc23b3STejun Heo  */
446131cc23b3STejun Heo int ata_std_qc_defer(struct ata_queued_cmd *qc)
446231cc23b3STejun Heo {
446331cc23b3STejun Heo 	struct ata_link *link = qc->dev->link;
446431cc23b3STejun Heo 
446531cc23b3STejun Heo 	if (qc->tf.protocol == ATA_PROT_NCQ) {
446631cc23b3STejun Heo 		if (!ata_tag_valid(link->active_tag))
446731cc23b3STejun Heo 			return 0;
446831cc23b3STejun Heo 	} else {
446931cc23b3STejun Heo 		if (!ata_tag_valid(link->active_tag) && !link->sactive)
447031cc23b3STejun Heo 			return 0;
447131cc23b3STejun Heo 	}
447231cc23b3STejun Heo 
447331cc23b3STejun Heo 	return ATA_DEFER_LINK;
447431cc23b3STejun Heo }
447531cc23b3STejun Heo 
4476c6fd2807SJeff Garzik void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
4477c6fd2807SJeff Garzik 
4478c6fd2807SJeff Garzik /**
4479c6fd2807SJeff Garzik  *	ata_sg_init - Associate command with scatter-gather table.
4480c6fd2807SJeff Garzik  *	@qc: Command to be associated
4481c6fd2807SJeff Garzik  *	@sg: Scatter-gather table.
4482c6fd2807SJeff Garzik  *	@n_elem: Number of elements in s/g table.
4483c6fd2807SJeff Garzik  *
4484c6fd2807SJeff Garzik  *	Initialize the data-related elements of queued_cmd @qc
4485c6fd2807SJeff Garzik  *	to point to a scatter-gather table @sg, containing @n_elem
4486c6fd2807SJeff Garzik  *	elements.
4487c6fd2807SJeff Garzik  *
4488c6fd2807SJeff Garzik  *	LOCKING:
4489cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4490c6fd2807SJeff Garzik  */
4491c6fd2807SJeff Garzik void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
4492c6fd2807SJeff Garzik 		 unsigned int n_elem)
4493c6fd2807SJeff Garzik {
4494ff2aeb1eSTejun Heo 	qc->sg = sg;
4495c6fd2807SJeff Garzik 	qc->n_elem = n_elem;
4496ff2aeb1eSTejun Heo 	qc->cursg = qc->sg;
4497ff2aeb1eSTejun Heo }
4498ff2aeb1eSTejun Heo 
4499c6fd2807SJeff Garzik /**
4500c6fd2807SJeff Garzik  *	ata_sg_setup - DMA-map the scatter-gather table associated with a command.
4501c6fd2807SJeff Garzik  *	@qc: Command with scatter-gather table to be mapped.
4502c6fd2807SJeff Garzik  *
4503c6fd2807SJeff Garzik  *	DMA-map the scatter-gather table associated with queued_cmd @qc.
4504c6fd2807SJeff Garzik  *
4505c6fd2807SJeff Garzik  *	LOCKING:
4506cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4507c6fd2807SJeff Garzik  *
4508c6fd2807SJeff Garzik  *	RETURNS:
4509c6fd2807SJeff Garzik  *	Zero on success, negative on error.
4510c6fd2807SJeff Garzik  *
4511c6fd2807SJeff Garzik  */
4512c6fd2807SJeff Garzik static int ata_sg_setup(struct ata_queued_cmd *qc)
4513c6fd2807SJeff Garzik {
4514c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4515dde20207SJames Bottomley 	unsigned int n_elem;
4516c6fd2807SJeff Garzik 
451744877b4eSTejun Heo 	VPRINTK("ENTER, ata%u\n", ap->print_id);
4518c6fd2807SJeff Garzik 
4519dde20207SJames Bottomley 	n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir);
4520dde20207SJames Bottomley 	if (n_elem < 1)
4521c6fd2807SJeff Garzik 		return -1;
4522c6fd2807SJeff Garzik 
4523dde20207SJames Bottomley 	DPRINTK("%d sg elements mapped\n", n_elem);
4524dde20207SJames Bottomley 
4525dde20207SJames Bottomley 	qc->n_elem = n_elem;
4526f92a2636STejun Heo 	qc->flags |= ATA_QCFLAG_DMAMAP;
4527c6fd2807SJeff Garzik 
4528c6fd2807SJeff Garzik 	return 0;
4529c6fd2807SJeff Garzik }
4530c6fd2807SJeff Garzik 
4531c6fd2807SJeff Garzik /**
4532c6fd2807SJeff Garzik  *	swap_buf_le16 - swap halves of 16-bit words in place
4533c6fd2807SJeff Garzik  *	@buf:  Buffer to swap
4534c6fd2807SJeff Garzik  *	@buf_words:  Number of 16-bit words in buffer.
4535c6fd2807SJeff Garzik  *
4536c6fd2807SJeff Garzik  *	Swap halves of 16-bit words if needed to convert from
4537c6fd2807SJeff Garzik  *	little-endian byte order to native cpu byte order, or
4538c6fd2807SJeff Garzik  *	vice-versa.
4539c6fd2807SJeff Garzik  *
4540c6fd2807SJeff Garzik  *	LOCKING:
4541c6fd2807SJeff Garzik  *	Inherited from caller.
4542c6fd2807SJeff Garzik  */
4543c6fd2807SJeff Garzik void swap_buf_le16(u16 *buf, unsigned int buf_words)
4544c6fd2807SJeff Garzik {
4545c6fd2807SJeff Garzik #ifdef __BIG_ENDIAN
4546c6fd2807SJeff Garzik 	unsigned int i;
4547c6fd2807SJeff Garzik 
4548c6fd2807SJeff Garzik 	for (i = 0; i < buf_words; i++)
4549c6fd2807SJeff Garzik 		buf[i] = le16_to_cpu(buf[i]);
4550c6fd2807SJeff Garzik #endif /* __BIG_ENDIAN */
4551c6fd2807SJeff Garzik }
4552c6fd2807SJeff Garzik 
4553c6fd2807SJeff Garzik /**
4554c6fd2807SJeff Garzik  *	ata_qc_new_init - Request an available ATA command, and initialize it
4555c6fd2807SJeff Garzik  *	@dev: Device from whom we request an available command structure
4556c6fd2807SJeff Garzik  *
4557c6fd2807SJeff Garzik  *	LOCKING:
4558c6fd2807SJeff Garzik  *	None.
4559c6fd2807SJeff Garzik  */
4560c6fd2807SJeff Garzik 
45612fca5ccfSJens Axboe struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev, int tag)
4562c6fd2807SJeff Garzik {
45639af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
4564c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
4565c6fd2807SJeff Garzik 
45662fca5ccfSJens Axboe 	if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
45672fca5ccfSJens Axboe 		return NULL;
45682fca5ccfSJens Axboe 
45692fca5ccfSJens Axboe 	qc = __ata_qc_from_tag(ap, tag);
4570c6fd2807SJeff Garzik 	if (qc) {
4571c6fd2807SJeff Garzik 		qc->scsicmd = NULL;
4572c6fd2807SJeff Garzik 		qc->ap = ap;
4573c6fd2807SJeff Garzik 		qc->dev = dev;
45742fca5ccfSJens Axboe 		qc->tag = tag;
4575c6fd2807SJeff Garzik 
4576c6fd2807SJeff Garzik 		ata_qc_reinit(qc);
4577c6fd2807SJeff Garzik 	}
4578c6fd2807SJeff Garzik 
4579c6fd2807SJeff Garzik 	return qc;
4580c6fd2807SJeff Garzik }
4581c6fd2807SJeff Garzik 
4582c6fd2807SJeff Garzik void __ata_qc_complete(struct ata_queued_cmd *qc)
4583c6fd2807SJeff Garzik {
4584c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
45859af5c9c9STejun Heo 	struct ata_link *link = qc->dev->link;
4586c6fd2807SJeff Garzik 
4587c6fd2807SJeff Garzik 	WARN_ON(qc == NULL);	/* ata_qc_from_tag _might_ return NULL */
4588c6fd2807SJeff Garzik 	WARN_ON(!(qc->flags & ATA_QCFLAG_ACTIVE));
4589c6fd2807SJeff Garzik 
4590c6fd2807SJeff Garzik 	if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
4591c6fd2807SJeff Garzik 		ata_sg_clean(qc);
4592c6fd2807SJeff Garzik 
4593c6fd2807SJeff Garzik 	/* command should be marked inactive atomically with qc completion */
4594da917d69STejun Heo 	if (qc->tf.protocol == ATA_PROT_NCQ) {
45959af5c9c9STejun Heo 		link->sactive &= ~(1 << qc->tag);
4596da917d69STejun Heo 		if (!link->sactive)
4597da917d69STejun Heo 			ap->nr_active_links--;
4598da917d69STejun Heo 	} else {
45999af5c9c9STejun Heo 		link->active_tag = ATA_TAG_POISON;
4600da917d69STejun Heo 		ap->nr_active_links--;
4601da917d69STejun Heo 	}
4602da917d69STejun Heo 
4603da917d69STejun Heo 	/* clear exclusive status */
4604da917d69STejun Heo 	if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
4605da917d69STejun Heo 		     ap->excl_link == link))
4606da917d69STejun Heo 		ap->excl_link = NULL;
4607c6fd2807SJeff Garzik 
4608c6fd2807SJeff Garzik 	/* atapi: mark qc as inactive to prevent the interrupt handler
4609c6fd2807SJeff Garzik 	 * from completing the command twice later, before the error handler
4610c6fd2807SJeff Garzik 	 * is called. (when rc != 0 and atapi request sense is needed)
4611c6fd2807SJeff Garzik 	 */
4612c6fd2807SJeff Garzik 	qc->flags &= ~ATA_QCFLAG_ACTIVE;
4613c6fd2807SJeff Garzik 	ap->qc_active &= ~(1 << qc->tag);
4614c6fd2807SJeff Garzik 
4615c6fd2807SJeff Garzik 	/* call completion callback */
4616c6fd2807SJeff Garzik 	qc->complete_fn(qc);
4617c6fd2807SJeff Garzik }
4618c6fd2807SJeff Garzik 
461939599a53STejun Heo static void fill_result_tf(struct ata_queued_cmd *qc)
462039599a53STejun Heo {
462139599a53STejun Heo 	struct ata_port *ap = qc->ap;
462239599a53STejun Heo 
462339599a53STejun Heo 	qc->result_tf.flags = qc->tf.flags;
462422183bf5STejun Heo 	ap->ops->qc_fill_rtf(qc);
462539599a53STejun Heo }
462639599a53STejun Heo 
462700115e0fSTejun Heo static void ata_verify_xfer(struct ata_queued_cmd *qc)
462800115e0fSTejun Heo {
462900115e0fSTejun Heo 	struct ata_device *dev = qc->dev;
463000115e0fSTejun Heo 
463100115e0fSTejun Heo 	if (ata_tag_internal(qc->tag))
463200115e0fSTejun Heo 		return;
463300115e0fSTejun Heo 
463400115e0fSTejun Heo 	if (ata_is_nodata(qc->tf.protocol))
463500115e0fSTejun Heo 		return;
463600115e0fSTejun Heo 
463700115e0fSTejun Heo 	if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
463800115e0fSTejun Heo 		return;
463900115e0fSTejun Heo 
464000115e0fSTejun Heo 	dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
464100115e0fSTejun Heo }
464200115e0fSTejun Heo 
4643c6fd2807SJeff Garzik /**
4644c6fd2807SJeff Garzik  *	ata_qc_complete - Complete an active ATA command
4645c6fd2807SJeff Garzik  *	@qc: Command to complete
4646c6fd2807SJeff Garzik  *	@err_mask: ATA Status register contents
4647c6fd2807SJeff Garzik  *
4648c6fd2807SJeff Garzik  *	Indicate to the mid and upper layers that an ATA
4649c6fd2807SJeff Garzik  *	command has completed, with either an ok or not-ok status.
4650c6fd2807SJeff Garzik  *
4651c6fd2807SJeff Garzik  *	LOCKING:
4652cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4653c6fd2807SJeff Garzik  */
4654c6fd2807SJeff Garzik void ata_qc_complete(struct ata_queued_cmd *qc)
4655c6fd2807SJeff Garzik {
4656c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4657c6fd2807SJeff Garzik 
4658c6fd2807SJeff Garzik 	/* XXX: New EH and old EH use different mechanisms to
4659c6fd2807SJeff Garzik 	 * synchronize EH with regular execution path.
4660c6fd2807SJeff Garzik 	 *
4661c6fd2807SJeff Garzik 	 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
4662c6fd2807SJeff Garzik 	 * Normal execution path is responsible for not accessing a
4663c6fd2807SJeff Garzik 	 * failed qc.  libata core enforces the rule by returning NULL
4664c6fd2807SJeff Garzik 	 * from ata_qc_from_tag() for failed qcs.
4665c6fd2807SJeff Garzik 	 *
4666c6fd2807SJeff Garzik 	 * Old EH depends on ata_qc_complete() nullifying completion
4667c6fd2807SJeff Garzik 	 * requests if ATA_QCFLAG_EH_SCHEDULED is set.  Old EH does
4668c6fd2807SJeff Garzik 	 * not synchronize with interrupt handler.  Only PIO task is
4669c6fd2807SJeff Garzik 	 * taken care of.
4670c6fd2807SJeff Garzik 	 */
4671c6fd2807SJeff Garzik 	if (ap->ops->error_handler) {
46724dbfa39bSTejun Heo 		struct ata_device *dev = qc->dev;
46734dbfa39bSTejun Heo 		struct ata_eh_info *ehi = &dev->link->eh_info;
46744dbfa39bSTejun Heo 
4675c6fd2807SJeff Garzik 		WARN_ON(ap->pflags & ATA_PFLAG_FROZEN);
4676c6fd2807SJeff Garzik 
4677c6fd2807SJeff Garzik 		if (unlikely(qc->err_mask))
4678c6fd2807SJeff Garzik 			qc->flags |= ATA_QCFLAG_FAILED;
4679c6fd2807SJeff Garzik 
4680c6fd2807SJeff Garzik 		if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
4681c6fd2807SJeff Garzik 			if (!ata_tag_internal(qc->tag)) {
4682c6fd2807SJeff Garzik 				/* always fill result TF for failed qc */
468339599a53STejun Heo 				fill_result_tf(qc);
4684c6fd2807SJeff Garzik 				ata_qc_schedule_eh(qc);
4685c6fd2807SJeff Garzik 				return;
4686c6fd2807SJeff Garzik 			}
4687c6fd2807SJeff Garzik 		}
4688c6fd2807SJeff Garzik 
4689c6fd2807SJeff Garzik 		/* read result TF if requested */
4690c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_RESULT_TF)
469139599a53STejun Heo 			fill_result_tf(qc);
4692c6fd2807SJeff Garzik 
46934dbfa39bSTejun Heo 		/* Some commands need post-processing after successful
46944dbfa39bSTejun Heo 		 * completion.
46954dbfa39bSTejun Heo 		 */
46964dbfa39bSTejun Heo 		switch (qc->tf.command) {
46974dbfa39bSTejun Heo 		case ATA_CMD_SET_FEATURES:
46984dbfa39bSTejun Heo 			if (qc->tf.feature != SETFEATURES_WC_ON &&
46994dbfa39bSTejun Heo 			    qc->tf.feature != SETFEATURES_WC_OFF)
47004dbfa39bSTejun Heo 				break;
47014dbfa39bSTejun Heo 			/* fall through */
47024dbfa39bSTejun Heo 		case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
47034dbfa39bSTejun Heo 		case ATA_CMD_SET_MULTI: /* multi_count changed */
47044dbfa39bSTejun Heo 			/* revalidate device */
47054dbfa39bSTejun Heo 			ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE;
47064dbfa39bSTejun Heo 			ata_port_schedule_eh(ap);
47074dbfa39bSTejun Heo 			break;
4708054a5fbaSTejun Heo 
4709054a5fbaSTejun Heo 		case ATA_CMD_SLEEP:
4710054a5fbaSTejun Heo 			dev->flags |= ATA_DFLAG_SLEEPING;
4711054a5fbaSTejun Heo 			break;
47124dbfa39bSTejun Heo 		}
47134dbfa39bSTejun Heo 
471400115e0fSTejun Heo 		if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
471500115e0fSTejun Heo 			ata_verify_xfer(qc);
471600115e0fSTejun Heo 
4717c6fd2807SJeff Garzik 		__ata_qc_complete(qc);
4718c6fd2807SJeff Garzik 	} else {
4719c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
4720c6fd2807SJeff Garzik 			return;
4721c6fd2807SJeff Garzik 
4722c6fd2807SJeff Garzik 		/* read result TF if failed or requested */
4723c6fd2807SJeff Garzik 		if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
472439599a53STejun Heo 			fill_result_tf(qc);
4725c6fd2807SJeff Garzik 
4726c6fd2807SJeff Garzik 		__ata_qc_complete(qc);
4727c6fd2807SJeff Garzik 	}
4728c6fd2807SJeff Garzik }
4729c6fd2807SJeff Garzik 
4730c6fd2807SJeff Garzik /**
4731c6fd2807SJeff Garzik  *	ata_qc_complete_multiple - Complete multiple qcs successfully
4732c6fd2807SJeff Garzik  *	@ap: port in question
4733c6fd2807SJeff Garzik  *	@qc_active: new qc_active mask
4734c6fd2807SJeff Garzik  *
4735c6fd2807SJeff Garzik  *	Complete in-flight commands.  This functions is meant to be
4736c6fd2807SJeff Garzik  *	called from low-level driver's interrupt routine to complete
4737c6fd2807SJeff Garzik  *	requests normally.  ap->qc_active and @qc_active is compared
4738c6fd2807SJeff Garzik  *	and commands are completed accordingly.
4739c6fd2807SJeff Garzik  *
4740c6fd2807SJeff Garzik  *	LOCKING:
4741cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4742c6fd2807SJeff Garzik  *
4743c6fd2807SJeff Garzik  *	RETURNS:
4744c6fd2807SJeff Garzik  *	Number of completed commands on success, -errno otherwise.
4745c6fd2807SJeff Garzik  */
474679f97dadSTejun Heo int ata_qc_complete_multiple(struct ata_port *ap, u32 qc_active)
4747c6fd2807SJeff Garzik {
4748c6fd2807SJeff Garzik 	int nr_done = 0;
4749c6fd2807SJeff Garzik 	u32 done_mask;
4750c6fd2807SJeff Garzik 	int i;
4751c6fd2807SJeff Garzik 
4752c6fd2807SJeff Garzik 	done_mask = ap->qc_active ^ qc_active;
4753c6fd2807SJeff Garzik 
4754c6fd2807SJeff Garzik 	if (unlikely(done_mask & qc_active)) {
4755c6fd2807SJeff Garzik 		ata_port_printk(ap, KERN_ERR, "illegal qc_active transition "
4756c6fd2807SJeff Garzik 				"(%08x->%08x)\n", ap->qc_active, qc_active);
4757c6fd2807SJeff Garzik 		return -EINVAL;
4758c6fd2807SJeff Garzik 	}
4759c6fd2807SJeff Garzik 
4760c6fd2807SJeff Garzik 	for (i = 0; i < ATA_MAX_QUEUE; i++) {
4761c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc;
4762c6fd2807SJeff Garzik 
4763c6fd2807SJeff Garzik 		if (!(done_mask & (1 << i)))
4764c6fd2807SJeff Garzik 			continue;
4765c6fd2807SJeff Garzik 
4766c6fd2807SJeff Garzik 		if ((qc = ata_qc_from_tag(ap, i))) {
4767c6fd2807SJeff Garzik 			ata_qc_complete(qc);
4768c6fd2807SJeff Garzik 			nr_done++;
4769c6fd2807SJeff Garzik 		}
4770c6fd2807SJeff Garzik 	}
4771c6fd2807SJeff Garzik 
4772c6fd2807SJeff Garzik 	return nr_done;
4773c6fd2807SJeff Garzik }
4774c6fd2807SJeff Garzik 
4775c6fd2807SJeff Garzik /**
4776c6fd2807SJeff Garzik  *	ata_qc_issue - issue taskfile to device
4777c6fd2807SJeff Garzik  *	@qc: command to issue to device
4778c6fd2807SJeff Garzik  *
4779c6fd2807SJeff Garzik  *	Prepare an ATA command to submission to device.
4780c6fd2807SJeff Garzik  *	This includes mapping the data into a DMA-able
4781c6fd2807SJeff Garzik  *	area, filling in the S/G table, and finally
4782c6fd2807SJeff Garzik  *	writing the taskfile to hardware, starting the command.
4783c6fd2807SJeff Garzik  *
4784c6fd2807SJeff Garzik  *	LOCKING:
4785cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4786c6fd2807SJeff Garzik  */
4787c6fd2807SJeff Garzik void ata_qc_issue(struct ata_queued_cmd *qc)
4788c6fd2807SJeff Garzik {
4789c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
47909af5c9c9STejun Heo 	struct ata_link *link = qc->dev->link;
4791405e66b3STejun Heo 	u8 prot = qc->tf.protocol;
4792c6fd2807SJeff Garzik 
4793c6fd2807SJeff Garzik 	/* Make sure only one non-NCQ command is outstanding.  The
4794c6fd2807SJeff Garzik 	 * check is skipped for old EH because it reuses active qc to
4795c6fd2807SJeff Garzik 	 * request ATAPI sense.
4796c6fd2807SJeff Garzik 	 */
47979af5c9c9STejun Heo 	WARN_ON(ap->ops->error_handler && ata_tag_valid(link->active_tag));
4798c6fd2807SJeff Garzik 
47991973a023STejun Heo 	if (ata_is_ncq(prot)) {
48009af5c9c9STejun Heo 		WARN_ON(link->sactive & (1 << qc->tag));
4801da917d69STejun Heo 
4802da917d69STejun Heo 		if (!link->sactive)
4803da917d69STejun Heo 			ap->nr_active_links++;
48049af5c9c9STejun Heo 		link->sactive |= 1 << qc->tag;
4805c6fd2807SJeff Garzik 	} else {
48069af5c9c9STejun Heo 		WARN_ON(link->sactive);
4807da917d69STejun Heo 
4808da917d69STejun Heo 		ap->nr_active_links++;
48099af5c9c9STejun Heo 		link->active_tag = qc->tag;
4810c6fd2807SJeff Garzik 	}
4811c6fd2807SJeff Garzik 
4812c6fd2807SJeff Garzik 	qc->flags |= ATA_QCFLAG_ACTIVE;
4813c6fd2807SJeff Garzik 	ap->qc_active |= 1 << qc->tag;
4814c6fd2807SJeff Garzik 
4815f92a2636STejun Heo 	/* We guarantee to LLDs that they will have at least one
4816f92a2636STejun Heo 	 * non-zero sg if the command is a data command.
4817f92a2636STejun Heo 	 */
4818ff2aeb1eSTejun Heo 	BUG_ON(ata_is_data(prot) && (!qc->sg || !qc->n_elem || !qc->nbytes));
4819f92a2636STejun Heo 
4820405e66b3STejun Heo 	if (ata_is_dma(prot) || (ata_is_pio(prot) &&
4821f92a2636STejun Heo 				 (ap->flags & ATA_FLAG_PIO_DMA)))
4822c6fd2807SJeff Garzik 		if (ata_sg_setup(qc))
4823c6fd2807SJeff Garzik 			goto sg_err;
4824c6fd2807SJeff Garzik 
4825cf480626STejun Heo 	/* if device is sleeping, schedule reset and abort the link */
4826054a5fbaSTejun Heo 	if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
4827cf480626STejun Heo 		link->eh_info.action |= ATA_EH_RESET;
4828054a5fbaSTejun Heo 		ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
4829054a5fbaSTejun Heo 		ata_link_abort(link);
4830054a5fbaSTejun Heo 		return;
4831054a5fbaSTejun Heo 	}
4832054a5fbaSTejun Heo 
4833c6fd2807SJeff Garzik 	ap->ops->qc_prep(qc);
4834c6fd2807SJeff Garzik 
4835c6fd2807SJeff Garzik 	qc->err_mask |= ap->ops->qc_issue(qc);
4836c6fd2807SJeff Garzik 	if (unlikely(qc->err_mask))
4837c6fd2807SJeff Garzik 		goto err;
4838c6fd2807SJeff Garzik 	return;
4839c6fd2807SJeff Garzik 
4840c6fd2807SJeff Garzik sg_err:
4841c6fd2807SJeff Garzik 	qc->err_mask |= AC_ERR_SYSTEM;
4842c6fd2807SJeff Garzik err:
4843c6fd2807SJeff Garzik 	ata_qc_complete(qc);
4844c6fd2807SJeff Garzik }
4845c6fd2807SJeff Garzik 
4846c6fd2807SJeff Garzik /**
4847c6fd2807SJeff Garzik  *	sata_scr_valid - test whether SCRs are accessible
4848936fd732STejun Heo  *	@link: ATA link to test SCR accessibility for
4849c6fd2807SJeff Garzik  *
4850936fd732STejun Heo  *	Test whether SCRs are accessible for @link.
4851c6fd2807SJeff Garzik  *
4852c6fd2807SJeff Garzik  *	LOCKING:
4853c6fd2807SJeff Garzik  *	None.
4854c6fd2807SJeff Garzik  *
4855c6fd2807SJeff Garzik  *	RETURNS:
4856c6fd2807SJeff Garzik  *	1 if SCRs are accessible, 0 otherwise.
4857c6fd2807SJeff Garzik  */
4858936fd732STejun Heo int sata_scr_valid(struct ata_link *link)
4859c6fd2807SJeff Garzik {
4860936fd732STejun Heo 	struct ata_port *ap = link->ap;
4861936fd732STejun Heo 
4862a16abc0bSTejun Heo 	return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
4863c6fd2807SJeff Garzik }
4864c6fd2807SJeff Garzik 
4865c6fd2807SJeff Garzik /**
4866c6fd2807SJeff Garzik  *	sata_scr_read - read SCR register of the specified port
4867936fd732STejun Heo  *	@link: ATA link to read SCR for
4868c6fd2807SJeff Garzik  *	@reg: SCR to read
4869c6fd2807SJeff Garzik  *	@val: Place to store read value
4870c6fd2807SJeff Garzik  *
4871936fd732STejun Heo  *	Read SCR register @reg of @link into *@val.  This function is
4872633273a3STejun Heo  *	guaranteed to succeed if @link is ap->link, the cable type of
4873633273a3STejun Heo  *	the port is SATA and the port implements ->scr_read.
4874c6fd2807SJeff Garzik  *
4875c6fd2807SJeff Garzik  *	LOCKING:
4876633273a3STejun Heo  *	None if @link is ap->link.  Kernel thread context otherwise.
4877c6fd2807SJeff Garzik  *
4878c6fd2807SJeff Garzik  *	RETURNS:
4879c6fd2807SJeff Garzik  *	0 on success, negative errno on failure.
4880c6fd2807SJeff Garzik  */
4881936fd732STejun Heo int sata_scr_read(struct ata_link *link, int reg, u32 *val)
4882c6fd2807SJeff Garzik {
4883633273a3STejun Heo 	if (ata_is_host_link(link)) {
4884936fd732STejun Heo 		if (sata_scr_valid(link))
488582ef04fbSTejun Heo 			return link->ap->ops->scr_read(link, reg, val);
4886c6fd2807SJeff Garzik 		return -EOPNOTSUPP;
4887c6fd2807SJeff Garzik 	}
4888c6fd2807SJeff Garzik 
4889633273a3STejun Heo 	return sata_pmp_scr_read(link, reg, val);
4890633273a3STejun Heo }
4891633273a3STejun Heo 
4892c6fd2807SJeff Garzik /**
4893c6fd2807SJeff Garzik  *	sata_scr_write - write SCR register of the specified port
4894936fd732STejun Heo  *	@link: ATA link to write SCR for
4895c6fd2807SJeff Garzik  *	@reg: SCR to write
4896c6fd2807SJeff Garzik  *	@val: value to write
4897c6fd2807SJeff Garzik  *
4898936fd732STejun Heo  *	Write @val to SCR register @reg of @link.  This function is
4899633273a3STejun Heo  *	guaranteed to succeed if @link is ap->link, the cable type of
4900633273a3STejun Heo  *	the port is SATA and the port implements ->scr_read.
4901c6fd2807SJeff Garzik  *
4902c6fd2807SJeff Garzik  *	LOCKING:
4903633273a3STejun Heo  *	None if @link is ap->link.  Kernel thread context otherwise.
4904c6fd2807SJeff Garzik  *
4905c6fd2807SJeff Garzik  *	RETURNS:
4906c6fd2807SJeff Garzik  *	0 on success, negative errno on failure.
4907c6fd2807SJeff Garzik  */
4908936fd732STejun Heo int sata_scr_write(struct ata_link *link, int reg, u32 val)
4909c6fd2807SJeff Garzik {
4910633273a3STejun Heo 	if (ata_is_host_link(link)) {
4911936fd732STejun Heo 		if (sata_scr_valid(link))
491282ef04fbSTejun Heo 			return link->ap->ops->scr_write(link, reg, val);
4913c6fd2807SJeff Garzik 		return -EOPNOTSUPP;
4914c6fd2807SJeff Garzik 	}
4915c6fd2807SJeff Garzik 
4916633273a3STejun Heo 	return sata_pmp_scr_write(link, reg, val);
4917633273a3STejun Heo }
4918633273a3STejun Heo 
4919c6fd2807SJeff Garzik /**
4920c6fd2807SJeff Garzik  *	sata_scr_write_flush - write SCR register of the specified port and flush
4921936fd732STejun Heo  *	@link: ATA link to write SCR for
4922c6fd2807SJeff Garzik  *	@reg: SCR to write
4923c6fd2807SJeff Garzik  *	@val: value to write
4924c6fd2807SJeff Garzik  *
4925c6fd2807SJeff Garzik  *	This function is identical to sata_scr_write() except that this
4926c6fd2807SJeff Garzik  *	function performs flush after writing to the register.
4927c6fd2807SJeff Garzik  *
4928c6fd2807SJeff Garzik  *	LOCKING:
4929633273a3STejun Heo  *	None if @link is ap->link.  Kernel thread context otherwise.
4930c6fd2807SJeff Garzik  *
4931c6fd2807SJeff Garzik  *	RETURNS:
4932c6fd2807SJeff Garzik  *	0 on success, negative errno on failure.
4933c6fd2807SJeff Garzik  */
4934936fd732STejun Heo int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
4935c6fd2807SJeff Garzik {
4936633273a3STejun Heo 	if (ata_is_host_link(link)) {
4937da3dbb17STejun Heo 		int rc;
4938da3dbb17STejun Heo 
4939936fd732STejun Heo 		if (sata_scr_valid(link)) {
494082ef04fbSTejun Heo 			rc = link->ap->ops->scr_write(link, reg, val);
4941da3dbb17STejun Heo 			if (rc == 0)
494282ef04fbSTejun Heo 				rc = link->ap->ops->scr_read(link, reg, &val);
4943da3dbb17STejun Heo 			return rc;
4944c6fd2807SJeff Garzik 		}
4945c6fd2807SJeff Garzik 		return -EOPNOTSUPP;
4946c6fd2807SJeff Garzik 	}
4947c6fd2807SJeff Garzik 
4948633273a3STejun Heo 	return sata_pmp_scr_write(link, reg, val);
4949633273a3STejun Heo }
4950633273a3STejun Heo 
4951c6fd2807SJeff Garzik /**
4952b1c72916STejun Heo  *	ata_phys_link_online - test whether the given link is online
4953936fd732STejun Heo  *	@link: ATA link to test
4954c6fd2807SJeff Garzik  *
4955936fd732STejun Heo  *	Test whether @link is online.  Note that this function returns
4956936fd732STejun Heo  *	0 if online status of @link cannot be obtained, so
4957936fd732STejun Heo  *	ata_link_online(link) != !ata_link_offline(link).
4958c6fd2807SJeff Garzik  *
4959c6fd2807SJeff Garzik  *	LOCKING:
4960c6fd2807SJeff Garzik  *	None.
4961c6fd2807SJeff Garzik  *
4962c6fd2807SJeff Garzik  *	RETURNS:
4963b5b3fa38STejun Heo  *	True if the port online status is available and online.
4964c6fd2807SJeff Garzik  */
4965b1c72916STejun Heo bool ata_phys_link_online(struct ata_link *link)
4966c6fd2807SJeff Garzik {
4967c6fd2807SJeff Garzik 	u32 sstatus;
4968c6fd2807SJeff Garzik 
4969936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
4970936fd732STejun Heo 	    (sstatus & 0xf) == 0x3)
4971b5b3fa38STejun Heo 		return true;
4972b5b3fa38STejun Heo 	return false;
4973c6fd2807SJeff Garzik }
4974c6fd2807SJeff Garzik 
4975c6fd2807SJeff Garzik /**
4976b1c72916STejun Heo  *	ata_phys_link_offline - test whether the given link is offline
4977936fd732STejun Heo  *	@link: ATA link to test
4978c6fd2807SJeff Garzik  *
4979936fd732STejun Heo  *	Test whether @link is offline.  Note that this function
4980936fd732STejun Heo  *	returns 0 if offline status of @link cannot be obtained, so
4981936fd732STejun Heo  *	ata_link_online(link) != !ata_link_offline(link).
4982c6fd2807SJeff Garzik  *
4983c6fd2807SJeff Garzik  *	LOCKING:
4984c6fd2807SJeff Garzik  *	None.
4985c6fd2807SJeff Garzik  *
4986c6fd2807SJeff Garzik  *	RETURNS:
4987b5b3fa38STejun Heo  *	True if the port offline status is available and offline.
4988c6fd2807SJeff Garzik  */
4989b1c72916STejun Heo bool ata_phys_link_offline(struct ata_link *link)
4990c6fd2807SJeff Garzik {
4991c6fd2807SJeff Garzik 	u32 sstatus;
4992c6fd2807SJeff Garzik 
4993936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
4994936fd732STejun Heo 	    (sstatus & 0xf) != 0x3)
4995b5b3fa38STejun Heo 		return true;
4996b5b3fa38STejun Heo 	return false;
4997c6fd2807SJeff Garzik }
4998c6fd2807SJeff Garzik 
4999b1c72916STejun Heo /**
5000b1c72916STejun Heo  *	ata_link_online - test whether the given link is online
5001b1c72916STejun Heo  *	@link: ATA link to test
5002b1c72916STejun Heo  *
5003b1c72916STejun Heo  *	Test whether @link is online.  This is identical to
5004b1c72916STejun Heo  *	ata_phys_link_online() when there's no slave link.  When
5005b1c72916STejun Heo  *	there's a slave link, this function should only be called on
5006b1c72916STejun Heo  *	the master link and will return true if any of M/S links is
5007b1c72916STejun Heo  *	online.
5008b1c72916STejun Heo  *
5009b1c72916STejun Heo  *	LOCKING:
5010b1c72916STejun Heo  *	None.
5011b1c72916STejun Heo  *
5012b1c72916STejun Heo  *	RETURNS:
5013b1c72916STejun Heo  *	True if the port online status is available and online.
5014b1c72916STejun Heo  */
5015b1c72916STejun Heo bool ata_link_online(struct ata_link *link)
5016b1c72916STejun Heo {
5017b1c72916STejun Heo 	struct ata_link *slave = link->ap->slave_link;
5018b1c72916STejun Heo 
5019b1c72916STejun Heo 	WARN_ON(link == slave);	/* shouldn't be called on slave link */
5020b1c72916STejun Heo 
5021b1c72916STejun Heo 	return ata_phys_link_online(link) ||
5022b1c72916STejun Heo 		(slave && ata_phys_link_online(slave));
5023b1c72916STejun Heo }
5024b1c72916STejun Heo 
5025b1c72916STejun Heo /**
5026b1c72916STejun Heo  *	ata_link_offline - test whether the given link is offline
5027b1c72916STejun Heo  *	@link: ATA link to test
5028b1c72916STejun Heo  *
5029b1c72916STejun Heo  *	Test whether @link is offline.  This is identical to
5030b1c72916STejun Heo  *	ata_phys_link_offline() when there's no slave link.  When
5031b1c72916STejun Heo  *	there's a slave link, this function should only be called on
5032b1c72916STejun Heo  *	the master link and will return true if both M/S links are
5033b1c72916STejun Heo  *	offline.
5034b1c72916STejun Heo  *
5035b1c72916STejun Heo  *	LOCKING:
5036b1c72916STejun Heo  *	None.
5037b1c72916STejun Heo  *
5038b1c72916STejun Heo  *	RETURNS:
5039b1c72916STejun Heo  *	True if the port offline status is available and offline.
5040b1c72916STejun Heo  */
5041b1c72916STejun Heo bool ata_link_offline(struct ata_link *link)
5042b1c72916STejun Heo {
5043b1c72916STejun Heo 	struct ata_link *slave = link->ap->slave_link;
5044b1c72916STejun Heo 
5045b1c72916STejun Heo 	WARN_ON(link == slave);	/* shouldn't be called on slave link */
5046b1c72916STejun Heo 
5047b1c72916STejun Heo 	return ata_phys_link_offline(link) &&
5048b1c72916STejun Heo 		(!slave || ata_phys_link_offline(slave));
5049b1c72916STejun Heo }
5050b1c72916STejun Heo 
50516ffa01d8STejun Heo #ifdef CONFIG_PM
5052cca3974eSJeff Garzik static int ata_host_request_pm(struct ata_host *host, pm_message_t mesg,
5053cca3974eSJeff Garzik 			       unsigned int action, unsigned int ehi_flags,
5054cca3974eSJeff Garzik 			       int wait)
5055c6fd2807SJeff Garzik {
5056c6fd2807SJeff Garzik 	unsigned long flags;
5057c6fd2807SJeff Garzik 	int i, rc;
5058c6fd2807SJeff Garzik 
5059cca3974eSJeff Garzik 	for (i = 0; i < host->n_ports; i++) {
5060cca3974eSJeff Garzik 		struct ata_port *ap = host->ports[i];
5061e3667ebfSTejun Heo 		struct ata_link *link;
5062c6fd2807SJeff Garzik 
5063c6fd2807SJeff Garzik 		/* Previous resume operation might still be in
5064c6fd2807SJeff Garzik 		 * progress.  Wait for PM_PENDING to clear.
5065c6fd2807SJeff Garzik 		 */
5066c6fd2807SJeff Garzik 		if (ap->pflags & ATA_PFLAG_PM_PENDING) {
5067c6fd2807SJeff Garzik 			ata_port_wait_eh(ap);
5068c6fd2807SJeff Garzik 			WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5069c6fd2807SJeff Garzik 		}
5070c6fd2807SJeff Garzik 
5071c6fd2807SJeff Garzik 		/* request PM ops to EH */
5072c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
5073c6fd2807SJeff Garzik 
5074c6fd2807SJeff Garzik 		ap->pm_mesg = mesg;
5075c6fd2807SJeff Garzik 		if (wait) {
5076c6fd2807SJeff Garzik 			rc = 0;
5077c6fd2807SJeff Garzik 			ap->pm_result = &rc;
5078c6fd2807SJeff Garzik 		}
5079c6fd2807SJeff Garzik 
5080c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_PM_PENDING;
5081e3667ebfSTejun Heo 		__ata_port_for_each_link(link, ap) {
5082e3667ebfSTejun Heo 			link->eh_info.action |= action;
5083e3667ebfSTejun Heo 			link->eh_info.flags |= ehi_flags;
5084e3667ebfSTejun Heo 		}
5085c6fd2807SJeff Garzik 
5086c6fd2807SJeff Garzik 		ata_port_schedule_eh(ap);
5087c6fd2807SJeff Garzik 
5088c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
5089c6fd2807SJeff Garzik 
5090c6fd2807SJeff Garzik 		/* wait and check result */
5091c6fd2807SJeff Garzik 		if (wait) {
5092c6fd2807SJeff Garzik 			ata_port_wait_eh(ap);
5093c6fd2807SJeff Garzik 			WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5094c6fd2807SJeff Garzik 			if (rc)
5095c6fd2807SJeff Garzik 				return rc;
5096c6fd2807SJeff Garzik 		}
5097c6fd2807SJeff Garzik 	}
5098c6fd2807SJeff Garzik 
5099c6fd2807SJeff Garzik 	return 0;
5100c6fd2807SJeff Garzik }
5101c6fd2807SJeff Garzik 
5102c6fd2807SJeff Garzik /**
5103cca3974eSJeff Garzik  *	ata_host_suspend - suspend host
5104cca3974eSJeff Garzik  *	@host: host to suspend
5105c6fd2807SJeff Garzik  *	@mesg: PM message
5106c6fd2807SJeff Garzik  *
5107cca3974eSJeff Garzik  *	Suspend @host.  Actual operation is performed by EH.  This
5108c6fd2807SJeff Garzik  *	function requests EH to perform PM operations and waits for EH
5109c6fd2807SJeff Garzik  *	to finish.
5110c6fd2807SJeff Garzik  *
5111c6fd2807SJeff Garzik  *	LOCKING:
5112c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
5113c6fd2807SJeff Garzik  *
5114c6fd2807SJeff Garzik  *	RETURNS:
5115c6fd2807SJeff Garzik  *	0 on success, -errno on failure.
5116c6fd2807SJeff Garzik  */
5117cca3974eSJeff Garzik int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
5118c6fd2807SJeff Garzik {
51199666f400STejun Heo 	int rc;
5120c6fd2807SJeff Garzik 
5121ca77329fSKristen Carlson Accardi 	/*
5122ca77329fSKristen Carlson Accardi 	 * disable link pm on all ports before requesting
5123ca77329fSKristen Carlson Accardi 	 * any pm activity
5124ca77329fSKristen Carlson Accardi 	 */
5125ca77329fSKristen Carlson Accardi 	ata_lpm_enable(host);
5126ca77329fSKristen Carlson Accardi 
5127cca3974eSJeff Garzik 	rc = ata_host_request_pm(host, mesg, 0, ATA_EHI_QUIET, 1);
512872ad6ec4SJeff Garzik 	if (rc == 0)
512972ad6ec4SJeff Garzik 		host->dev->power.power_state = mesg;
5130c6fd2807SJeff Garzik 	return rc;
5131c6fd2807SJeff Garzik }
5132c6fd2807SJeff Garzik 
5133c6fd2807SJeff Garzik /**
5134cca3974eSJeff Garzik  *	ata_host_resume - resume host
5135cca3974eSJeff Garzik  *	@host: host to resume
5136c6fd2807SJeff Garzik  *
5137cca3974eSJeff Garzik  *	Resume @host.  Actual operation is performed by EH.  This
5138c6fd2807SJeff Garzik  *	function requests EH to perform PM operations and returns.
5139c6fd2807SJeff Garzik  *	Note that all resume operations are performed parallely.
5140c6fd2807SJeff Garzik  *
5141c6fd2807SJeff Garzik  *	LOCKING:
5142c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
5143c6fd2807SJeff Garzik  */
5144cca3974eSJeff Garzik void ata_host_resume(struct ata_host *host)
5145c6fd2807SJeff Garzik {
5146cf480626STejun Heo 	ata_host_request_pm(host, PMSG_ON, ATA_EH_RESET,
5147c6fd2807SJeff Garzik 			    ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET, 0);
514872ad6ec4SJeff Garzik 	host->dev->power.power_state = PMSG_ON;
5149ca77329fSKristen Carlson Accardi 
5150ca77329fSKristen Carlson Accardi 	/* reenable link pm */
5151ca77329fSKristen Carlson Accardi 	ata_lpm_disable(host);
5152c6fd2807SJeff Garzik }
51536ffa01d8STejun Heo #endif
5154c6fd2807SJeff Garzik 
5155c6fd2807SJeff Garzik /**
5156c6fd2807SJeff Garzik  *	ata_port_start - Set port up for dma.
5157c6fd2807SJeff Garzik  *	@ap: Port to initialize
5158c6fd2807SJeff Garzik  *
5159c6fd2807SJeff Garzik  *	Called just after data structures for each port are
5160c6fd2807SJeff Garzik  *	initialized.  Allocates space for PRD table.
5161c6fd2807SJeff Garzik  *
5162c6fd2807SJeff Garzik  *	May be used as the port_start() entry in ata_port_operations.
5163c6fd2807SJeff Garzik  *
5164c6fd2807SJeff Garzik  *	LOCKING:
5165c6fd2807SJeff Garzik  *	Inherited from caller.
5166c6fd2807SJeff Garzik  */
5167c6fd2807SJeff Garzik int ata_port_start(struct ata_port *ap)
5168c6fd2807SJeff Garzik {
5169c6fd2807SJeff Garzik 	struct device *dev = ap->dev;
5170c6fd2807SJeff Garzik 
5171f0d36efdSTejun Heo 	ap->prd = dmam_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma,
5172f0d36efdSTejun Heo 				      GFP_KERNEL);
5173c6fd2807SJeff Garzik 	if (!ap->prd)
5174c6fd2807SJeff Garzik 		return -ENOMEM;
5175c6fd2807SJeff Garzik 
5176c6fd2807SJeff Garzik 	return 0;
5177c6fd2807SJeff Garzik }
5178c6fd2807SJeff Garzik 
5179c6fd2807SJeff Garzik /**
5180c6fd2807SJeff Garzik  *	ata_dev_init - Initialize an ata_device structure
5181c6fd2807SJeff Garzik  *	@dev: Device structure to initialize
5182c6fd2807SJeff Garzik  *
5183c6fd2807SJeff Garzik  *	Initialize @dev in preparation for probing.
5184c6fd2807SJeff Garzik  *
5185c6fd2807SJeff Garzik  *	LOCKING:
5186c6fd2807SJeff Garzik  *	Inherited from caller.
5187c6fd2807SJeff Garzik  */
5188c6fd2807SJeff Garzik void ata_dev_init(struct ata_device *dev)
5189c6fd2807SJeff Garzik {
5190b1c72916STejun Heo 	struct ata_link *link = ata_dev_phys_link(dev);
51919af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
5192c6fd2807SJeff Garzik 	unsigned long flags;
5193c6fd2807SJeff Garzik 
5194b1c72916STejun Heo 	/* SATA spd limit is bound to the attached device, reset together */
51959af5c9c9STejun Heo 	link->sata_spd_limit = link->hw_sata_spd_limit;
51969af5c9c9STejun Heo 	link->sata_spd = 0;
5197c6fd2807SJeff Garzik 
5198c6fd2807SJeff Garzik 	/* High bits of dev->flags are used to record warm plug
5199c6fd2807SJeff Garzik 	 * requests which occur asynchronously.  Synchronize using
5200cca3974eSJeff Garzik 	 * host lock.
5201c6fd2807SJeff Garzik 	 */
5202c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
5203c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_INIT_MASK;
52043dcc323fSTejun Heo 	dev->horkage = 0;
5205c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
5206c6fd2807SJeff Garzik 
5207c6fd2807SJeff Garzik 	memset((void *)dev + ATA_DEVICE_CLEAR_OFFSET, 0,
5208c6fd2807SJeff Garzik 	       sizeof(*dev) - ATA_DEVICE_CLEAR_OFFSET);
5209c6fd2807SJeff Garzik 	dev->pio_mask = UINT_MAX;
5210c6fd2807SJeff Garzik 	dev->mwdma_mask = UINT_MAX;
5211c6fd2807SJeff Garzik 	dev->udma_mask = UINT_MAX;
5212c6fd2807SJeff Garzik }
5213c6fd2807SJeff Garzik 
5214c6fd2807SJeff Garzik /**
52154fb37a25STejun Heo  *	ata_link_init - Initialize an ata_link structure
52164fb37a25STejun Heo  *	@ap: ATA port link is attached to
52174fb37a25STejun Heo  *	@link: Link structure to initialize
52188989805dSTejun Heo  *	@pmp: Port multiplier port number
52194fb37a25STejun Heo  *
52204fb37a25STejun Heo  *	Initialize @link.
52214fb37a25STejun Heo  *
52224fb37a25STejun Heo  *	LOCKING:
52234fb37a25STejun Heo  *	Kernel thread context (may sleep)
52244fb37a25STejun Heo  */
5225fb7fd614STejun Heo void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
52264fb37a25STejun Heo {
52274fb37a25STejun Heo 	int i;
52284fb37a25STejun Heo 
52294fb37a25STejun Heo 	/* clear everything except for devices */
52304fb37a25STejun Heo 	memset(link, 0, offsetof(struct ata_link, device[0]));
52314fb37a25STejun Heo 
52324fb37a25STejun Heo 	link->ap = ap;
52338989805dSTejun Heo 	link->pmp = pmp;
52344fb37a25STejun Heo 	link->active_tag = ATA_TAG_POISON;
52354fb37a25STejun Heo 	link->hw_sata_spd_limit = UINT_MAX;
52364fb37a25STejun Heo 
52374fb37a25STejun Heo 	/* can't use iterator, ap isn't initialized yet */
52384fb37a25STejun Heo 	for (i = 0; i < ATA_MAX_DEVICES; i++) {
52394fb37a25STejun Heo 		struct ata_device *dev = &link->device[i];
52404fb37a25STejun Heo 
52414fb37a25STejun Heo 		dev->link = link;
52424fb37a25STejun Heo 		dev->devno = dev - link->device;
52434fb37a25STejun Heo 		ata_dev_init(dev);
52444fb37a25STejun Heo 	}
52454fb37a25STejun Heo }
52464fb37a25STejun Heo 
52474fb37a25STejun Heo /**
52484fb37a25STejun Heo  *	sata_link_init_spd - Initialize link->sata_spd_limit
52494fb37a25STejun Heo  *	@link: Link to configure sata_spd_limit for
52504fb37a25STejun Heo  *
52514fb37a25STejun Heo  *	Initialize @link->[hw_]sata_spd_limit to the currently
52524fb37a25STejun Heo  *	configured value.
52534fb37a25STejun Heo  *
52544fb37a25STejun Heo  *	LOCKING:
52554fb37a25STejun Heo  *	Kernel thread context (may sleep).
52564fb37a25STejun Heo  *
52574fb37a25STejun Heo  *	RETURNS:
52584fb37a25STejun Heo  *	0 on success, -errno on failure.
52594fb37a25STejun Heo  */
5260fb7fd614STejun Heo int sata_link_init_spd(struct ata_link *link)
52614fb37a25STejun Heo {
526233267325STejun Heo 	u8 spd;
52634fb37a25STejun Heo 	int rc;
52644fb37a25STejun Heo 
5265d127ea7bSTejun Heo 	rc = sata_scr_read(link, SCR_CONTROL, &link->saved_scontrol);
52664fb37a25STejun Heo 	if (rc)
52674fb37a25STejun Heo 		return rc;
52684fb37a25STejun Heo 
5269d127ea7bSTejun Heo 	spd = (link->saved_scontrol >> 4) & 0xf;
52704fb37a25STejun Heo 	if (spd)
52714fb37a25STejun Heo 		link->hw_sata_spd_limit &= (1 << spd) - 1;
52724fb37a25STejun Heo 
527305944bdfSTejun Heo 	ata_force_link_limits(link);
527433267325STejun Heo 
52754fb37a25STejun Heo 	link->sata_spd_limit = link->hw_sata_spd_limit;
52764fb37a25STejun Heo 
52774fb37a25STejun Heo 	return 0;
52784fb37a25STejun Heo }
52794fb37a25STejun Heo 
52804fb37a25STejun Heo /**
5281f3187195STejun Heo  *	ata_port_alloc - allocate and initialize basic ATA port resources
5282f3187195STejun Heo  *	@host: ATA host this allocated port belongs to
5283c6fd2807SJeff Garzik  *
5284f3187195STejun Heo  *	Allocate and initialize basic ATA port resources.
5285f3187195STejun Heo  *
5286f3187195STejun Heo  *	RETURNS:
5287f3187195STejun Heo  *	Allocate ATA port on success, NULL on failure.
5288c6fd2807SJeff Garzik  *
5289c6fd2807SJeff Garzik  *	LOCKING:
5290f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5291c6fd2807SJeff Garzik  */
5292f3187195STejun Heo struct ata_port *ata_port_alloc(struct ata_host *host)
5293c6fd2807SJeff Garzik {
5294f3187195STejun Heo 	struct ata_port *ap;
5295c6fd2807SJeff Garzik 
5296f3187195STejun Heo 	DPRINTK("ENTER\n");
5297f3187195STejun Heo 
5298f3187195STejun Heo 	ap = kzalloc(sizeof(*ap), GFP_KERNEL);
5299f3187195STejun Heo 	if (!ap)
5300f3187195STejun Heo 		return NULL;
5301f3187195STejun Heo 
5302f4d6d004STejun Heo 	ap->pflags |= ATA_PFLAG_INITIALIZING;
5303cca3974eSJeff Garzik 	ap->lock = &host->lock;
5304c6fd2807SJeff Garzik 	ap->flags = ATA_FLAG_DISABLED;
5305f3187195STejun Heo 	ap->print_id = -1;
5306c6fd2807SJeff Garzik 	ap->ctl = ATA_DEVCTL_OBS;
5307cca3974eSJeff Garzik 	ap->host = host;
5308f3187195STejun Heo 	ap->dev = host->dev;
5309c6fd2807SJeff Garzik 	ap->last_ctl = 0xFF;
5310c6fd2807SJeff Garzik 
5311c6fd2807SJeff Garzik #if defined(ATA_VERBOSE_DEBUG)
5312c6fd2807SJeff Garzik 	/* turn on all debugging levels */
5313c6fd2807SJeff Garzik 	ap->msg_enable = 0x00FF;
5314c6fd2807SJeff Garzik #elif defined(ATA_DEBUG)
5315c6fd2807SJeff Garzik 	ap->msg_enable = ATA_MSG_DRV | ATA_MSG_INFO | ATA_MSG_CTL | ATA_MSG_WARN | ATA_MSG_ERR;
5316c6fd2807SJeff Garzik #else
5317c6fd2807SJeff Garzik 	ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
5318c6fd2807SJeff Garzik #endif
5319c6fd2807SJeff Garzik 
5320127102aeSTejun Heo #ifdef CONFIG_ATA_SFF
5321442eacc3SJeff Garzik 	INIT_DELAYED_WORK(&ap->port_task, ata_pio_task);
5322f667fdbbSTejun Heo #else
5323f667fdbbSTejun Heo 	INIT_DELAYED_WORK(&ap->port_task, NULL);
5324127102aeSTejun Heo #endif
532565f27f38SDavid Howells 	INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
532665f27f38SDavid Howells 	INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
5327c6fd2807SJeff Garzik 	INIT_LIST_HEAD(&ap->eh_done_q);
5328c6fd2807SJeff Garzik 	init_waitqueue_head(&ap->eh_wait_q);
532945fabbb7SElias Oltmanns 	init_completion(&ap->park_req_pending);
53305ddf24c5STejun Heo 	init_timer_deferrable(&ap->fastdrain_timer);
53315ddf24c5STejun Heo 	ap->fastdrain_timer.function = ata_eh_fastdrain_timerfn;
53325ddf24c5STejun Heo 	ap->fastdrain_timer.data = (unsigned long)ap;
5333c6fd2807SJeff Garzik 
5334c6fd2807SJeff Garzik 	ap->cbl = ATA_CBL_NONE;
5335c6fd2807SJeff Garzik 
53368989805dSTejun Heo 	ata_link_init(ap, &ap->link, 0);
5337c6fd2807SJeff Garzik 
5338c6fd2807SJeff Garzik #ifdef ATA_IRQ_TRAP
5339c6fd2807SJeff Garzik 	ap->stats.unhandled_irq = 1;
5340c6fd2807SJeff Garzik 	ap->stats.idle_irq = 1;
5341c6fd2807SJeff Garzik #endif
5342c6fd2807SJeff Garzik 	return ap;
5343c6fd2807SJeff Garzik }
5344c6fd2807SJeff Garzik 
5345f0d36efdSTejun Heo static void ata_host_release(struct device *gendev, void *res)
5346f0d36efdSTejun Heo {
5347f0d36efdSTejun Heo 	struct ata_host *host = dev_get_drvdata(gendev);
5348f0d36efdSTejun Heo 	int i;
5349f0d36efdSTejun Heo 
5350f0d36efdSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
5351f0d36efdSTejun Heo 		struct ata_port *ap = host->ports[i];
5352f0d36efdSTejun Heo 
5353ecef7253STejun Heo 		if (!ap)
5354ecef7253STejun Heo 			continue;
5355ecef7253STejun Heo 
53564911487aSTejun Heo 		if (ap->scsi_host)
53571aa506e4STejun Heo 			scsi_host_put(ap->scsi_host);
53581aa506e4STejun Heo 
5359633273a3STejun Heo 		kfree(ap->pmp_link);
5360b1c72916STejun Heo 		kfree(ap->slave_link);
53614911487aSTejun Heo 		kfree(ap);
53621aa506e4STejun Heo 		host->ports[i] = NULL;
53631aa506e4STejun Heo 	}
53641aa506e4STejun Heo 
53651aa56ccaSTejun Heo 	dev_set_drvdata(gendev, NULL);
5366f0d36efdSTejun Heo }
5367f0d36efdSTejun Heo 
5368c6fd2807SJeff Garzik /**
5369f3187195STejun Heo  *	ata_host_alloc - allocate and init basic ATA host resources
5370f3187195STejun Heo  *	@dev: generic device this host is associated with
5371f3187195STejun Heo  *	@max_ports: maximum number of ATA ports associated with this host
5372f3187195STejun Heo  *
5373f3187195STejun Heo  *	Allocate and initialize basic ATA host resources.  LLD calls
5374f3187195STejun Heo  *	this function to allocate a host, initializes it fully and
5375f3187195STejun Heo  *	attaches it using ata_host_register().
5376f3187195STejun Heo  *
5377f3187195STejun Heo  *	@max_ports ports are allocated and host->n_ports is
5378f3187195STejun Heo  *	initialized to @max_ports.  The caller is allowed to decrease
5379f3187195STejun Heo  *	host->n_ports before calling ata_host_register().  The unused
5380f3187195STejun Heo  *	ports will be automatically freed on registration.
5381f3187195STejun Heo  *
5382f3187195STejun Heo  *	RETURNS:
5383f3187195STejun Heo  *	Allocate ATA host on success, NULL on failure.
5384f3187195STejun Heo  *
5385f3187195STejun Heo  *	LOCKING:
5386f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5387f3187195STejun Heo  */
5388f3187195STejun Heo struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
5389f3187195STejun Heo {
5390f3187195STejun Heo 	struct ata_host *host;
5391f3187195STejun Heo 	size_t sz;
5392f3187195STejun Heo 	int i;
5393f3187195STejun Heo 
5394f3187195STejun Heo 	DPRINTK("ENTER\n");
5395f3187195STejun Heo 
5396f3187195STejun Heo 	if (!devres_open_group(dev, NULL, GFP_KERNEL))
5397f3187195STejun Heo 		return NULL;
5398f3187195STejun Heo 
5399f3187195STejun Heo 	/* alloc a container for our list of ATA ports (buses) */
5400f3187195STejun Heo 	sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
5401f3187195STejun Heo 	/* alloc a container for our list of ATA ports (buses) */
5402f3187195STejun Heo 	host = devres_alloc(ata_host_release, sz, GFP_KERNEL);
5403f3187195STejun Heo 	if (!host)
5404f3187195STejun Heo 		goto err_out;
5405f3187195STejun Heo 
5406f3187195STejun Heo 	devres_add(dev, host);
5407f3187195STejun Heo 	dev_set_drvdata(dev, host);
5408f3187195STejun Heo 
5409f3187195STejun Heo 	spin_lock_init(&host->lock);
5410f3187195STejun Heo 	host->dev = dev;
5411f3187195STejun Heo 	host->n_ports = max_ports;
5412f3187195STejun Heo 
5413f3187195STejun Heo 	/* allocate ports bound to this host */
5414f3187195STejun Heo 	for (i = 0; i < max_ports; i++) {
5415f3187195STejun Heo 		struct ata_port *ap;
5416f3187195STejun Heo 
5417f3187195STejun Heo 		ap = ata_port_alloc(host);
5418f3187195STejun Heo 		if (!ap)
5419f3187195STejun Heo 			goto err_out;
5420f3187195STejun Heo 
5421f3187195STejun Heo 		ap->port_no = i;
5422f3187195STejun Heo 		host->ports[i] = ap;
5423f3187195STejun Heo 	}
5424f3187195STejun Heo 
5425f3187195STejun Heo 	devres_remove_group(dev, NULL);
5426f3187195STejun Heo 	return host;
5427f3187195STejun Heo 
5428f3187195STejun Heo  err_out:
5429f3187195STejun Heo 	devres_release_group(dev, NULL);
5430f3187195STejun Heo 	return NULL;
5431f3187195STejun Heo }
5432f3187195STejun Heo 
5433f3187195STejun Heo /**
5434f5cda257STejun Heo  *	ata_host_alloc_pinfo - alloc host and init with port_info array
5435f5cda257STejun Heo  *	@dev: generic device this host is associated with
5436f5cda257STejun Heo  *	@ppi: array of ATA port_info to initialize host with
5437f5cda257STejun Heo  *	@n_ports: number of ATA ports attached to this host
5438f5cda257STejun Heo  *
5439f5cda257STejun Heo  *	Allocate ATA host and initialize with info from @ppi.  If NULL
5440f5cda257STejun Heo  *	terminated, @ppi may contain fewer entries than @n_ports.  The
5441f5cda257STejun Heo  *	last entry will be used for the remaining ports.
5442f5cda257STejun Heo  *
5443f5cda257STejun Heo  *	RETURNS:
5444f5cda257STejun Heo  *	Allocate ATA host on success, NULL on failure.
5445f5cda257STejun Heo  *
5446f5cda257STejun Heo  *	LOCKING:
5447f5cda257STejun Heo  *	Inherited from calling layer (may sleep).
5448f5cda257STejun Heo  */
5449f5cda257STejun Heo struct ata_host *ata_host_alloc_pinfo(struct device *dev,
5450f5cda257STejun Heo 				      const struct ata_port_info * const * ppi,
5451f5cda257STejun Heo 				      int n_ports)
5452f5cda257STejun Heo {
5453f5cda257STejun Heo 	const struct ata_port_info *pi;
5454f5cda257STejun Heo 	struct ata_host *host;
5455f5cda257STejun Heo 	int i, j;
5456f5cda257STejun Heo 
5457f5cda257STejun Heo 	host = ata_host_alloc(dev, n_ports);
5458f5cda257STejun Heo 	if (!host)
5459f5cda257STejun Heo 		return NULL;
5460f5cda257STejun Heo 
5461f5cda257STejun Heo 	for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
5462f5cda257STejun Heo 		struct ata_port *ap = host->ports[i];
5463f5cda257STejun Heo 
5464f5cda257STejun Heo 		if (ppi[j])
5465f5cda257STejun Heo 			pi = ppi[j++];
5466f5cda257STejun Heo 
5467f5cda257STejun Heo 		ap->pio_mask = pi->pio_mask;
5468f5cda257STejun Heo 		ap->mwdma_mask = pi->mwdma_mask;
5469f5cda257STejun Heo 		ap->udma_mask = pi->udma_mask;
5470f5cda257STejun Heo 		ap->flags |= pi->flags;
54710c88758bSTejun Heo 		ap->link.flags |= pi->link_flags;
5472f5cda257STejun Heo 		ap->ops = pi->port_ops;
5473f5cda257STejun Heo 
5474f5cda257STejun Heo 		if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
5475f5cda257STejun Heo 			host->ops = pi->port_ops;
5476f5cda257STejun Heo 	}
5477f5cda257STejun Heo 
5478f5cda257STejun Heo 	return host;
5479f5cda257STejun Heo }
5480f5cda257STejun Heo 
5481b1c72916STejun Heo /**
5482b1c72916STejun Heo  *	ata_slave_link_init - initialize slave link
5483b1c72916STejun Heo  *	@ap: port to initialize slave link for
5484b1c72916STejun Heo  *
5485b1c72916STejun Heo  *	Create and initialize slave link for @ap.  This enables slave
5486b1c72916STejun Heo  *	link handling on the port.
5487b1c72916STejun Heo  *
5488b1c72916STejun Heo  *	In libata, a port contains links and a link contains devices.
5489b1c72916STejun Heo  *	There is single host link but if a PMP is attached to it,
5490b1c72916STejun Heo  *	there can be multiple fan-out links.  On SATA, there's usually
5491b1c72916STejun Heo  *	a single device connected to a link but PATA and SATA
5492b1c72916STejun Heo  *	controllers emulating TF based interface can have two - master
5493b1c72916STejun Heo  *	and slave.
5494b1c72916STejun Heo  *
5495b1c72916STejun Heo  *	However, there are a few controllers which don't fit into this
5496b1c72916STejun Heo  *	abstraction too well - SATA controllers which emulate TF
5497b1c72916STejun Heo  *	interface with both master and slave devices but also have
5498b1c72916STejun Heo  *	separate SCR register sets for each device.  These controllers
5499b1c72916STejun Heo  *	need separate links for physical link handling
5500b1c72916STejun Heo  *	(e.g. onlineness, link speed) but should be treated like a
5501b1c72916STejun Heo  *	traditional M/S controller for everything else (e.g. command
5502b1c72916STejun Heo  *	issue, softreset).
5503b1c72916STejun Heo  *
5504b1c72916STejun Heo  *	slave_link is libata's way of handling this class of
5505b1c72916STejun Heo  *	controllers without impacting core layer too much.  For
5506b1c72916STejun Heo  *	anything other than physical link handling, the default host
5507b1c72916STejun Heo  *	link is used for both master and slave.  For physical link
5508b1c72916STejun Heo  *	handling, separate @ap->slave_link is used.  All dirty details
5509b1c72916STejun Heo  *	are implemented inside libata core layer.  From LLD's POV, the
5510b1c72916STejun Heo  *	only difference is that prereset, hardreset and postreset are
5511b1c72916STejun Heo  *	called once more for the slave link, so the reset sequence
5512b1c72916STejun Heo  *	looks like the following.
5513b1c72916STejun Heo  *
5514b1c72916STejun Heo  *	prereset(M) -> prereset(S) -> hardreset(M) -> hardreset(S) ->
5515b1c72916STejun Heo  *	softreset(M) -> postreset(M) -> postreset(S)
5516b1c72916STejun Heo  *
5517b1c72916STejun Heo  *	Note that softreset is called only for the master.  Softreset
5518b1c72916STejun Heo  *	resets both M/S by definition, so SRST on master should handle
5519b1c72916STejun Heo  *	both (the standard method will work just fine).
5520b1c72916STejun Heo  *
5521b1c72916STejun Heo  *	LOCKING:
5522b1c72916STejun Heo  *	Should be called before host is registered.
5523b1c72916STejun Heo  *
5524b1c72916STejun Heo  *	RETURNS:
5525b1c72916STejun Heo  *	0 on success, -errno on failure.
5526b1c72916STejun Heo  */
5527b1c72916STejun Heo int ata_slave_link_init(struct ata_port *ap)
5528b1c72916STejun Heo {
5529b1c72916STejun Heo 	struct ata_link *link;
5530b1c72916STejun Heo 
5531b1c72916STejun Heo 	WARN_ON(ap->slave_link);
5532b1c72916STejun Heo 	WARN_ON(ap->flags & ATA_FLAG_PMP);
5533b1c72916STejun Heo 
5534b1c72916STejun Heo 	link = kzalloc(sizeof(*link), GFP_KERNEL);
5535b1c72916STejun Heo 	if (!link)
5536b1c72916STejun Heo 		return -ENOMEM;
5537b1c72916STejun Heo 
5538b1c72916STejun Heo 	ata_link_init(ap, link, 1);
5539b1c72916STejun Heo 	ap->slave_link = link;
5540b1c72916STejun Heo 	return 0;
5541b1c72916STejun Heo }
5542b1c72916STejun Heo 
554332ebbc0cSTejun Heo static void ata_host_stop(struct device *gendev, void *res)
554432ebbc0cSTejun Heo {
554532ebbc0cSTejun Heo 	struct ata_host *host = dev_get_drvdata(gendev);
554632ebbc0cSTejun Heo 	int i;
554732ebbc0cSTejun Heo 
554832ebbc0cSTejun Heo 	WARN_ON(!(host->flags & ATA_HOST_STARTED));
554932ebbc0cSTejun Heo 
555032ebbc0cSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
555132ebbc0cSTejun Heo 		struct ata_port *ap = host->ports[i];
555232ebbc0cSTejun Heo 
555332ebbc0cSTejun Heo 		if (ap->ops->port_stop)
555432ebbc0cSTejun Heo 			ap->ops->port_stop(ap);
555532ebbc0cSTejun Heo 	}
555632ebbc0cSTejun Heo 
555732ebbc0cSTejun Heo 	if (host->ops->host_stop)
555832ebbc0cSTejun Heo 		host->ops->host_stop(host);
555932ebbc0cSTejun Heo }
556032ebbc0cSTejun Heo 
5561f5cda257STejun Heo /**
5562029cfd6bSTejun Heo  *	ata_finalize_port_ops - finalize ata_port_operations
5563029cfd6bSTejun Heo  *	@ops: ata_port_operations to finalize
5564029cfd6bSTejun Heo  *
5565029cfd6bSTejun Heo  *	An ata_port_operations can inherit from another ops and that
5566029cfd6bSTejun Heo  *	ops can again inherit from another.  This can go on as many
5567029cfd6bSTejun Heo  *	times as necessary as long as there is no loop in the
5568029cfd6bSTejun Heo  *	inheritance chain.
5569029cfd6bSTejun Heo  *
5570029cfd6bSTejun Heo  *	Ops tables are finalized when the host is started.  NULL or
5571029cfd6bSTejun Heo  *	unspecified entries are inherited from the closet ancestor
5572029cfd6bSTejun Heo  *	which has the method and the entry is populated with it.
5573029cfd6bSTejun Heo  *	After finalization, the ops table directly points to all the
5574029cfd6bSTejun Heo  *	methods and ->inherits is no longer necessary and cleared.
5575029cfd6bSTejun Heo  *
5576029cfd6bSTejun Heo  *	Using ATA_OP_NULL, inheriting ops can force a method to NULL.
5577029cfd6bSTejun Heo  *
5578029cfd6bSTejun Heo  *	LOCKING:
5579029cfd6bSTejun Heo  *	None.
5580029cfd6bSTejun Heo  */
5581029cfd6bSTejun Heo static void ata_finalize_port_ops(struct ata_port_operations *ops)
5582029cfd6bSTejun Heo {
55832da67659SPradeep Singh Rautela 	static DEFINE_SPINLOCK(lock);
5584029cfd6bSTejun Heo 	const struct ata_port_operations *cur;
5585029cfd6bSTejun Heo 	void **begin = (void **)ops;
5586029cfd6bSTejun Heo 	void **end = (void **)&ops->inherits;
5587029cfd6bSTejun Heo 	void **pp;
5588029cfd6bSTejun Heo 
5589029cfd6bSTejun Heo 	if (!ops || !ops->inherits)
5590029cfd6bSTejun Heo 		return;
5591029cfd6bSTejun Heo 
5592029cfd6bSTejun Heo 	spin_lock(&lock);
5593029cfd6bSTejun Heo 
5594029cfd6bSTejun Heo 	for (cur = ops->inherits; cur; cur = cur->inherits) {
5595029cfd6bSTejun Heo 		void **inherit = (void **)cur;
5596029cfd6bSTejun Heo 
5597029cfd6bSTejun Heo 		for (pp = begin; pp < end; pp++, inherit++)
5598029cfd6bSTejun Heo 			if (!*pp)
5599029cfd6bSTejun Heo 				*pp = *inherit;
5600029cfd6bSTejun Heo 	}
5601029cfd6bSTejun Heo 
5602029cfd6bSTejun Heo 	for (pp = begin; pp < end; pp++)
5603029cfd6bSTejun Heo 		if (IS_ERR(*pp))
5604029cfd6bSTejun Heo 			*pp = NULL;
5605029cfd6bSTejun Heo 
5606029cfd6bSTejun Heo 	ops->inherits = NULL;
5607029cfd6bSTejun Heo 
5608029cfd6bSTejun Heo 	spin_unlock(&lock);
5609029cfd6bSTejun Heo }
5610029cfd6bSTejun Heo 
5611029cfd6bSTejun Heo /**
5612ecef7253STejun Heo  *	ata_host_start - start and freeze ports of an ATA host
5613ecef7253STejun Heo  *	@host: ATA host to start ports for
5614ecef7253STejun Heo  *
5615ecef7253STejun Heo  *	Start and then freeze ports of @host.  Started status is
5616ecef7253STejun Heo  *	recorded in host->flags, so this function can be called
5617ecef7253STejun Heo  *	multiple times.  Ports are guaranteed to get started only
5618f3187195STejun Heo  *	once.  If host->ops isn't initialized yet, its set to the
5619f3187195STejun Heo  *	first non-dummy port ops.
5620ecef7253STejun Heo  *
5621ecef7253STejun Heo  *	LOCKING:
5622ecef7253STejun Heo  *	Inherited from calling layer (may sleep).
5623ecef7253STejun Heo  *
5624ecef7253STejun Heo  *	RETURNS:
5625ecef7253STejun Heo  *	0 if all ports are started successfully, -errno otherwise.
5626ecef7253STejun Heo  */
5627ecef7253STejun Heo int ata_host_start(struct ata_host *host)
5628ecef7253STejun Heo {
562932ebbc0cSTejun Heo 	int have_stop = 0;
563032ebbc0cSTejun Heo 	void *start_dr = NULL;
5631ecef7253STejun Heo 	int i, rc;
5632ecef7253STejun Heo 
5633ecef7253STejun Heo 	if (host->flags & ATA_HOST_STARTED)
5634ecef7253STejun Heo 		return 0;
5635ecef7253STejun Heo 
5636029cfd6bSTejun Heo 	ata_finalize_port_ops(host->ops);
5637029cfd6bSTejun Heo 
5638ecef7253STejun Heo 	for (i = 0; i < host->n_ports; i++) {
5639ecef7253STejun Heo 		struct ata_port *ap = host->ports[i];
5640ecef7253STejun Heo 
5641029cfd6bSTejun Heo 		ata_finalize_port_ops(ap->ops);
5642029cfd6bSTejun Heo 
5643f3187195STejun Heo 		if (!host->ops && !ata_port_is_dummy(ap))
5644f3187195STejun Heo 			host->ops = ap->ops;
5645f3187195STejun Heo 
564632ebbc0cSTejun Heo 		if (ap->ops->port_stop)
564732ebbc0cSTejun Heo 			have_stop = 1;
564832ebbc0cSTejun Heo 	}
564932ebbc0cSTejun Heo 
565032ebbc0cSTejun Heo 	if (host->ops->host_stop)
565132ebbc0cSTejun Heo 		have_stop = 1;
565232ebbc0cSTejun Heo 
565332ebbc0cSTejun Heo 	if (have_stop) {
565432ebbc0cSTejun Heo 		start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
565532ebbc0cSTejun Heo 		if (!start_dr)
565632ebbc0cSTejun Heo 			return -ENOMEM;
565732ebbc0cSTejun Heo 	}
565832ebbc0cSTejun Heo 
565932ebbc0cSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
566032ebbc0cSTejun Heo 		struct ata_port *ap = host->ports[i];
566132ebbc0cSTejun Heo 
5662ecef7253STejun Heo 		if (ap->ops->port_start) {
5663ecef7253STejun Heo 			rc = ap->ops->port_start(ap);
5664ecef7253STejun Heo 			if (rc) {
56650f9fe9b7SAlan Cox 				if (rc != -ENODEV)
56660f757743SAndrew Morton 					dev_printk(KERN_ERR, host->dev,
56670f757743SAndrew Morton 						"failed to start port %d "
56680f757743SAndrew Morton 						"(errno=%d)\n", i, rc);
5669ecef7253STejun Heo 				goto err_out;
5670ecef7253STejun Heo 			}
5671ecef7253STejun Heo 		}
5672ecef7253STejun Heo 		ata_eh_freeze_port(ap);
5673ecef7253STejun Heo 	}
5674ecef7253STejun Heo 
567532ebbc0cSTejun Heo 	if (start_dr)
567632ebbc0cSTejun Heo 		devres_add(host->dev, start_dr);
5677ecef7253STejun Heo 	host->flags |= ATA_HOST_STARTED;
5678ecef7253STejun Heo 	return 0;
5679ecef7253STejun Heo 
5680ecef7253STejun Heo  err_out:
5681ecef7253STejun Heo 	while (--i >= 0) {
5682ecef7253STejun Heo 		struct ata_port *ap = host->ports[i];
5683ecef7253STejun Heo 
5684ecef7253STejun Heo 		if (ap->ops->port_stop)
5685ecef7253STejun Heo 			ap->ops->port_stop(ap);
5686ecef7253STejun Heo 	}
568732ebbc0cSTejun Heo 	devres_free(start_dr);
5688ecef7253STejun Heo 	return rc;
5689ecef7253STejun Heo }
5690ecef7253STejun Heo 
5691ecef7253STejun Heo /**
5692cca3974eSJeff Garzik  *	ata_sas_host_init - Initialize a host struct
5693cca3974eSJeff Garzik  *	@host:	host to initialize
5694cca3974eSJeff Garzik  *	@dev:	device host is attached to
5695cca3974eSJeff Garzik  *	@flags:	host flags
5696c6fd2807SJeff Garzik  *	@ops:	port_ops
5697c6fd2807SJeff Garzik  *
5698c6fd2807SJeff Garzik  *	LOCKING:
5699c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
5700c6fd2807SJeff Garzik  *
5701c6fd2807SJeff Garzik  */
5702f3187195STejun Heo /* KILLME - the only user left is ipr */
5703cca3974eSJeff Garzik void ata_host_init(struct ata_host *host, struct device *dev,
5704029cfd6bSTejun Heo 		   unsigned long flags, struct ata_port_operations *ops)
5705c6fd2807SJeff Garzik {
5706cca3974eSJeff Garzik 	spin_lock_init(&host->lock);
5707cca3974eSJeff Garzik 	host->dev = dev;
5708cca3974eSJeff Garzik 	host->flags = flags;
5709cca3974eSJeff Garzik 	host->ops = ops;
5710c6fd2807SJeff Garzik }
5711c6fd2807SJeff Garzik 
5712c6fd2807SJeff Garzik /**
5713f3187195STejun Heo  *	ata_host_register - register initialized ATA host
5714f3187195STejun Heo  *	@host: ATA host to register
5715f3187195STejun Heo  *	@sht: template for SCSI host
5716c6fd2807SJeff Garzik  *
5717f3187195STejun Heo  *	Register initialized ATA host.  @host is allocated using
5718f3187195STejun Heo  *	ata_host_alloc() and fully initialized by LLD.  This function
5719f3187195STejun Heo  *	starts ports, registers @host with ATA and SCSI layers and
5720f3187195STejun Heo  *	probe registered devices.
5721c6fd2807SJeff Garzik  *
5722c6fd2807SJeff Garzik  *	LOCKING:
5723f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5724c6fd2807SJeff Garzik  *
5725c6fd2807SJeff Garzik  *	RETURNS:
5726f3187195STejun Heo  *	0 on success, -errno otherwise.
5727c6fd2807SJeff Garzik  */
5728f3187195STejun Heo int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
5729c6fd2807SJeff Garzik {
5730f3187195STejun Heo 	int i, rc;
5731c6fd2807SJeff Garzik 
5732f3187195STejun Heo 	/* host must have been started */
5733f3187195STejun Heo 	if (!(host->flags & ATA_HOST_STARTED)) {
5734f3187195STejun Heo 		dev_printk(KERN_ERR, host->dev,
5735f3187195STejun Heo 			   "BUG: trying to register unstarted host\n");
5736f3187195STejun Heo 		WARN_ON(1);
5737f3187195STejun Heo 		return -EINVAL;
573802f076aaSAlan Cox 	}
5739f0d36efdSTejun Heo 
5740f3187195STejun Heo 	/* Blow away unused ports.  This happens when LLD can't
5741f3187195STejun Heo 	 * determine the exact number of ports to allocate at
5742f3187195STejun Heo 	 * allocation time.
5743f3187195STejun Heo 	 */
5744f3187195STejun Heo 	for (i = host->n_ports; host->ports[i]; i++)
5745f3187195STejun Heo 		kfree(host->ports[i]);
5746f0d36efdSTejun Heo 
5747f3187195STejun Heo 	/* give ports names and add SCSI hosts */
5748f3187195STejun Heo 	for (i = 0; i < host->n_ports; i++)
5749f3187195STejun Heo 		host->ports[i]->print_id = ata_print_id++;
5750c6fd2807SJeff Garzik 
5751f3187195STejun Heo 	rc = ata_scsi_add_hosts(host, sht);
5752ecef7253STejun Heo 	if (rc)
5753f3187195STejun Heo 		return rc;
5754ecef7253STejun Heo 
5755fafbae87STejun Heo 	/* associate with ACPI nodes */
5756fafbae87STejun Heo 	ata_acpi_associate(host);
5757fafbae87STejun Heo 
5758f3187195STejun Heo 	/* set cable, sata_spd_limit and report */
5759cca3974eSJeff Garzik 	for (i = 0; i < host->n_ports; i++) {
5760cca3974eSJeff Garzik 		struct ata_port *ap = host->ports[i];
5761f3187195STejun Heo 		unsigned long xfer_mask;
5762f3187195STejun Heo 
5763f3187195STejun Heo 		/* set SATA cable type if still unset */
5764f3187195STejun Heo 		if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
5765f3187195STejun Heo 			ap->cbl = ATA_CBL_SATA;
5766c6fd2807SJeff Garzik 
5767c6fd2807SJeff Garzik 		/* init sata_spd_limit to the current value */
57684fb37a25STejun Heo 		sata_link_init_spd(&ap->link);
5769b1c72916STejun Heo 		if (ap->slave_link)
5770b1c72916STejun Heo 			sata_link_init_spd(ap->slave_link);
5771c6fd2807SJeff Garzik 
5772cbcdd875STejun Heo 		/* print per-port info to dmesg */
5773f3187195STejun Heo 		xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
5774f3187195STejun Heo 					      ap->udma_mask);
5775f3187195STejun Heo 
5776abf6e8edSTejun Heo 		if (!ata_port_is_dummy(ap)) {
5777cbcdd875STejun Heo 			ata_port_printk(ap, KERN_INFO,
5778cbcdd875STejun Heo 					"%cATA max %s %s\n",
5779a16abc0bSTejun Heo 					(ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
5780f3187195STejun Heo 					ata_mode_string(xfer_mask),
5781cbcdd875STejun Heo 					ap->link.eh_info.desc);
5782abf6e8edSTejun Heo 			ata_ehi_clear_desc(&ap->link.eh_info);
5783abf6e8edSTejun Heo 		} else
5784f3187195STejun Heo 			ata_port_printk(ap, KERN_INFO, "DUMMY\n");
5785c6fd2807SJeff Garzik 	}
5786c6fd2807SJeff Garzik 
5787f3187195STejun Heo 	/* perform each probe synchronously */
5788f3187195STejun Heo 	DPRINTK("probe begin\n");
5789f3187195STejun Heo 	for (i = 0; i < host->n_ports; i++) {
5790f3187195STejun Heo 		struct ata_port *ap = host->ports[i];
5791f3187195STejun Heo 
5792f3187195STejun Heo 		/* probe */
5793c6fd2807SJeff Garzik 		if (ap->ops->error_handler) {
57949af5c9c9STejun Heo 			struct ata_eh_info *ehi = &ap->link.eh_info;
5795c6fd2807SJeff Garzik 			unsigned long flags;
5796c6fd2807SJeff Garzik 
5797c6fd2807SJeff Garzik 			ata_port_probe(ap);
5798c6fd2807SJeff Garzik 
5799c6fd2807SJeff Garzik 			/* kick EH for boot probing */
5800c6fd2807SJeff Garzik 			spin_lock_irqsave(ap->lock, flags);
5801c6fd2807SJeff Garzik 
5802b558edddSTejun Heo 			ehi->probe_mask |= ATA_ALL_DEVICES;
5803391191c1STejun Heo 			ehi->action |= ATA_EH_RESET | ATA_EH_LPM;
5804c6fd2807SJeff Garzik 			ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
5805c6fd2807SJeff Garzik 
5806f4d6d004STejun Heo 			ap->pflags &= ~ATA_PFLAG_INITIALIZING;
5807c6fd2807SJeff Garzik 			ap->pflags |= ATA_PFLAG_LOADING;
5808c6fd2807SJeff Garzik 			ata_port_schedule_eh(ap);
5809c6fd2807SJeff Garzik 
5810c6fd2807SJeff Garzik 			spin_unlock_irqrestore(ap->lock, flags);
5811c6fd2807SJeff Garzik 
5812c6fd2807SJeff Garzik 			/* wait for EH to finish */
5813c6fd2807SJeff Garzik 			ata_port_wait_eh(ap);
5814c6fd2807SJeff Garzik 		} else {
581544877b4eSTejun Heo 			DPRINTK("ata%u: bus probe begin\n", ap->print_id);
5816c6fd2807SJeff Garzik 			rc = ata_bus_probe(ap);
581744877b4eSTejun Heo 			DPRINTK("ata%u: bus probe end\n", ap->print_id);
5818c6fd2807SJeff Garzik 
5819c6fd2807SJeff Garzik 			if (rc) {
5820c6fd2807SJeff Garzik 				/* FIXME: do something useful here?
5821c6fd2807SJeff Garzik 				 * Current libata behavior will
5822c6fd2807SJeff Garzik 				 * tear down everything when
5823c6fd2807SJeff Garzik 				 * the module is removed
5824c6fd2807SJeff Garzik 				 * or the h/w is unplugged.
5825c6fd2807SJeff Garzik 				 */
5826c6fd2807SJeff Garzik 			}
5827c6fd2807SJeff Garzik 		}
5828c6fd2807SJeff Garzik 	}
5829c6fd2807SJeff Garzik 
5830c6fd2807SJeff Garzik 	/* probes are done, now scan each port's disk(s) */
5831c6fd2807SJeff Garzik 	DPRINTK("host probe begin\n");
5832cca3974eSJeff Garzik 	for (i = 0; i < host->n_ports; i++) {
5833cca3974eSJeff Garzik 		struct ata_port *ap = host->ports[i];
5834c6fd2807SJeff Garzik 
58351ae46317STejun Heo 		ata_scsi_scan_host(ap, 1);
5836c6fd2807SJeff Garzik 	}
5837c6fd2807SJeff Garzik 
5838f3187195STejun Heo 	return 0;
5839f3187195STejun Heo }
5840f3187195STejun Heo 
5841f3187195STejun Heo /**
5842f5cda257STejun Heo  *	ata_host_activate - start host, request IRQ and register it
5843f5cda257STejun Heo  *	@host: target ATA host
5844f5cda257STejun Heo  *	@irq: IRQ to request
5845f5cda257STejun Heo  *	@irq_handler: irq_handler used when requesting IRQ
5846f5cda257STejun Heo  *	@irq_flags: irq_flags used when requesting IRQ
5847f5cda257STejun Heo  *	@sht: scsi_host_template to use when registering the host
5848f5cda257STejun Heo  *
5849f5cda257STejun Heo  *	After allocating an ATA host and initializing it, most libata
5850f5cda257STejun Heo  *	LLDs perform three steps to activate the host - start host,
5851f5cda257STejun Heo  *	request IRQ and register it.  This helper takes necessasry
5852f5cda257STejun Heo  *	arguments and performs the three steps in one go.
5853f5cda257STejun Heo  *
58543d46b2e2SPaul Mundt  *	An invalid IRQ skips the IRQ registration and expects the host to
58553d46b2e2SPaul Mundt  *	have set polling mode on the port. In this case, @irq_handler
58563d46b2e2SPaul Mundt  *	should be NULL.
58573d46b2e2SPaul Mundt  *
5858f5cda257STejun Heo  *	LOCKING:
5859f5cda257STejun Heo  *	Inherited from calling layer (may sleep).
5860f5cda257STejun Heo  *
5861f5cda257STejun Heo  *	RETURNS:
5862f5cda257STejun Heo  *	0 on success, -errno otherwise.
5863f5cda257STejun Heo  */
5864f5cda257STejun Heo int ata_host_activate(struct ata_host *host, int irq,
5865f5cda257STejun Heo 		      irq_handler_t irq_handler, unsigned long irq_flags,
5866f5cda257STejun Heo 		      struct scsi_host_template *sht)
5867f5cda257STejun Heo {
5868cbcdd875STejun Heo 	int i, rc;
5869f5cda257STejun Heo 
5870f5cda257STejun Heo 	rc = ata_host_start(host);
5871f5cda257STejun Heo 	if (rc)
5872f5cda257STejun Heo 		return rc;
5873f5cda257STejun Heo 
58743d46b2e2SPaul Mundt 	/* Special case for polling mode */
58753d46b2e2SPaul Mundt 	if (!irq) {
58763d46b2e2SPaul Mundt 		WARN_ON(irq_handler);
58773d46b2e2SPaul Mundt 		return ata_host_register(host, sht);
58783d46b2e2SPaul Mundt 	}
58793d46b2e2SPaul Mundt 
5880f5cda257STejun Heo 	rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
5881f5cda257STejun Heo 			      dev_driver_string(host->dev), host);
5882f5cda257STejun Heo 	if (rc)
5883f5cda257STejun Heo 		return rc;
5884f5cda257STejun Heo 
5885cbcdd875STejun Heo 	for (i = 0; i < host->n_ports; i++)
5886cbcdd875STejun Heo 		ata_port_desc(host->ports[i], "irq %d", irq);
58874031826bSTejun Heo 
5888f5cda257STejun Heo 	rc = ata_host_register(host, sht);
5889f5cda257STejun Heo 	/* if failed, just free the IRQ and leave ports alone */
5890f5cda257STejun Heo 	if (rc)
5891f5cda257STejun Heo 		devm_free_irq(host->dev, irq, host);
5892f5cda257STejun Heo 
5893f5cda257STejun Heo 	return rc;
5894f5cda257STejun Heo }
5895f5cda257STejun Heo 
5896f5cda257STejun Heo /**
5897c6fd2807SJeff Garzik  *	ata_port_detach - Detach ATA port in prepration of device removal
5898c6fd2807SJeff Garzik  *	@ap: ATA port to be detached
5899c6fd2807SJeff Garzik  *
5900c6fd2807SJeff Garzik  *	Detach all ATA devices and the associated SCSI devices of @ap;
5901c6fd2807SJeff Garzik  *	then, remove the associated SCSI host.  @ap is guaranteed to
5902c6fd2807SJeff Garzik  *	be quiescent on return from this function.
5903c6fd2807SJeff Garzik  *
5904c6fd2807SJeff Garzik  *	LOCKING:
5905c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
5906c6fd2807SJeff Garzik  */
5907741b7763SAdrian Bunk static void ata_port_detach(struct ata_port *ap)
5908c6fd2807SJeff Garzik {
5909c6fd2807SJeff Garzik 	unsigned long flags;
591041bda9c9STejun Heo 	struct ata_link *link;
5911f58229f8STejun Heo 	struct ata_device *dev;
5912c6fd2807SJeff Garzik 
5913c6fd2807SJeff Garzik 	if (!ap->ops->error_handler)
5914c6fd2807SJeff Garzik 		goto skip_eh;
5915c6fd2807SJeff Garzik 
5916c6fd2807SJeff Garzik 	/* tell EH we're leaving & flush EH */
5917c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
5918c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_UNLOADING;
5919c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
5920c6fd2807SJeff Garzik 
5921c6fd2807SJeff Garzik 	ata_port_wait_eh(ap);
5922c6fd2807SJeff Garzik 
59237f9ad9b8STejun Heo 	/* EH is now guaranteed to see UNLOADING - EH context belongs
5924d127ea7bSTejun Heo 	 * to us.  Restore SControl and disable all existing devices.
5925c6fd2807SJeff Garzik 	 */
5926d127ea7bSTejun Heo 	__ata_port_for_each_link(link, ap) {
5927d127ea7bSTejun Heo 		sata_scr_write(link, SCR_CONTROL, link->saved_scontrol);
592841bda9c9STejun Heo 		ata_link_for_each_dev(dev, link)
5929f58229f8STejun Heo 			ata_dev_disable(dev);
593041bda9c9STejun Heo 	}
5931c6fd2807SJeff Garzik 
5932c6fd2807SJeff Garzik 	/* Final freeze & EH.  All in-flight commands are aborted.  EH
5933c6fd2807SJeff Garzik 	 * will be skipped and retrials will be terminated with bad
5934c6fd2807SJeff Garzik 	 * target.
5935c6fd2807SJeff Garzik 	 */
5936c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
5937c6fd2807SJeff Garzik 	ata_port_freeze(ap);	/* won't be thawed */
5938c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
5939c6fd2807SJeff Garzik 
5940c6fd2807SJeff Garzik 	ata_port_wait_eh(ap);
594145a66c1cSOleg Nesterov 	cancel_rearming_delayed_work(&ap->hotplug_task);
5942c6fd2807SJeff Garzik 
5943c6fd2807SJeff Garzik  skip_eh:
5944c6fd2807SJeff Garzik 	/* remove the associated SCSI host */
5945cca3974eSJeff Garzik 	scsi_remove_host(ap->scsi_host);
5946c6fd2807SJeff Garzik }
5947c6fd2807SJeff Garzik 
5948c6fd2807SJeff Garzik /**
59490529c159STejun Heo  *	ata_host_detach - Detach all ports of an ATA host
59500529c159STejun Heo  *	@host: Host to detach
59510529c159STejun Heo  *
59520529c159STejun Heo  *	Detach all ports of @host.
59530529c159STejun Heo  *
59540529c159STejun Heo  *	LOCKING:
59550529c159STejun Heo  *	Kernel thread context (may sleep).
59560529c159STejun Heo  */
59570529c159STejun Heo void ata_host_detach(struct ata_host *host)
59580529c159STejun Heo {
59590529c159STejun Heo 	int i;
59600529c159STejun Heo 
59610529c159STejun Heo 	for (i = 0; i < host->n_ports; i++)
59620529c159STejun Heo 		ata_port_detach(host->ports[i]);
5963562f0c2dSTejun Heo 
5964562f0c2dSTejun Heo 	/* the host is dead now, dissociate ACPI */
5965562f0c2dSTejun Heo 	ata_acpi_dissociate(host);
59660529c159STejun Heo }
59670529c159STejun Heo 
5968c6fd2807SJeff Garzik #ifdef CONFIG_PCI
5969c6fd2807SJeff Garzik 
5970c6fd2807SJeff Garzik /**
5971c6fd2807SJeff Garzik  *	ata_pci_remove_one - PCI layer callback for device removal
5972c6fd2807SJeff Garzik  *	@pdev: PCI device that was removed
5973c6fd2807SJeff Garzik  *
5974b878ca5dSTejun Heo  *	PCI layer indicates to libata via this hook that hot-unplug or
5975b878ca5dSTejun Heo  *	module unload event has occurred.  Detach all ports.  Resource
5976b878ca5dSTejun Heo  *	release is handled via devres.
5977c6fd2807SJeff Garzik  *
5978c6fd2807SJeff Garzik  *	LOCKING:
5979c6fd2807SJeff Garzik  *	Inherited from PCI layer (may sleep).
5980c6fd2807SJeff Garzik  */
5981c6fd2807SJeff Garzik void ata_pci_remove_one(struct pci_dev *pdev)
5982c6fd2807SJeff Garzik {
59832855568bSJeff Garzik 	struct device *dev = &pdev->dev;
5984cca3974eSJeff Garzik 	struct ata_host *host = dev_get_drvdata(dev);
5985c6fd2807SJeff Garzik 
5986f0d36efdSTejun Heo 	ata_host_detach(host);
5987c6fd2807SJeff Garzik }
5988c6fd2807SJeff Garzik 
5989c6fd2807SJeff Garzik /* move to PCI subsystem */
5990c6fd2807SJeff Garzik int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
5991c6fd2807SJeff Garzik {
5992c6fd2807SJeff Garzik 	unsigned long tmp = 0;
5993c6fd2807SJeff Garzik 
5994c6fd2807SJeff Garzik 	switch (bits->width) {
5995c6fd2807SJeff Garzik 	case 1: {
5996c6fd2807SJeff Garzik 		u8 tmp8 = 0;
5997c6fd2807SJeff Garzik 		pci_read_config_byte(pdev, bits->reg, &tmp8);
5998c6fd2807SJeff Garzik 		tmp = tmp8;
5999c6fd2807SJeff Garzik 		break;
6000c6fd2807SJeff Garzik 	}
6001c6fd2807SJeff Garzik 	case 2: {
6002c6fd2807SJeff Garzik 		u16 tmp16 = 0;
6003c6fd2807SJeff Garzik 		pci_read_config_word(pdev, bits->reg, &tmp16);
6004c6fd2807SJeff Garzik 		tmp = tmp16;
6005c6fd2807SJeff Garzik 		break;
6006c6fd2807SJeff Garzik 	}
6007c6fd2807SJeff Garzik 	case 4: {
6008c6fd2807SJeff Garzik 		u32 tmp32 = 0;
6009c6fd2807SJeff Garzik 		pci_read_config_dword(pdev, bits->reg, &tmp32);
6010c6fd2807SJeff Garzik 		tmp = tmp32;
6011c6fd2807SJeff Garzik 		break;
6012c6fd2807SJeff Garzik 	}
6013c6fd2807SJeff Garzik 
6014c6fd2807SJeff Garzik 	default:
6015c6fd2807SJeff Garzik 		return -EINVAL;
6016c6fd2807SJeff Garzik 	}
6017c6fd2807SJeff Garzik 
6018c6fd2807SJeff Garzik 	tmp &= bits->mask;
6019c6fd2807SJeff Garzik 
6020c6fd2807SJeff Garzik 	return (tmp == bits->val) ? 1 : 0;
6021c6fd2807SJeff Garzik }
6022c6fd2807SJeff Garzik 
60236ffa01d8STejun Heo #ifdef CONFIG_PM
6024c6fd2807SJeff Garzik void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
6025c6fd2807SJeff Garzik {
6026c6fd2807SJeff Garzik 	pci_save_state(pdev);
6027c6fd2807SJeff Garzik 	pci_disable_device(pdev);
60284c90d971STejun Heo 
60293a2d5b70SRafael J. Wysocki 	if (mesg.event & PM_EVENT_SLEEP)
6030c6fd2807SJeff Garzik 		pci_set_power_state(pdev, PCI_D3hot);
6031c6fd2807SJeff Garzik }
6032c6fd2807SJeff Garzik 
6033553c4aa6STejun Heo int ata_pci_device_do_resume(struct pci_dev *pdev)
6034c6fd2807SJeff Garzik {
6035553c4aa6STejun Heo 	int rc;
6036553c4aa6STejun Heo 
6037c6fd2807SJeff Garzik 	pci_set_power_state(pdev, PCI_D0);
6038c6fd2807SJeff Garzik 	pci_restore_state(pdev);
6039553c4aa6STejun Heo 
6040f0d36efdSTejun Heo 	rc = pcim_enable_device(pdev);
6041553c4aa6STejun Heo 	if (rc) {
6042553c4aa6STejun Heo 		dev_printk(KERN_ERR, &pdev->dev,
6043553c4aa6STejun Heo 			   "failed to enable device after resume (%d)\n", rc);
6044553c4aa6STejun Heo 		return rc;
6045553c4aa6STejun Heo 	}
6046553c4aa6STejun Heo 
6047c6fd2807SJeff Garzik 	pci_set_master(pdev);
6048553c4aa6STejun Heo 	return 0;
6049c6fd2807SJeff Garzik }
6050c6fd2807SJeff Garzik 
6051c6fd2807SJeff Garzik int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
6052c6fd2807SJeff Garzik {
6053cca3974eSJeff Garzik 	struct ata_host *host = dev_get_drvdata(&pdev->dev);
6054c6fd2807SJeff Garzik 	int rc = 0;
6055c6fd2807SJeff Garzik 
6056cca3974eSJeff Garzik 	rc = ata_host_suspend(host, mesg);
6057c6fd2807SJeff Garzik 	if (rc)
6058c6fd2807SJeff Garzik 		return rc;
6059c6fd2807SJeff Garzik 
6060c6fd2807SJeff Garzik 	ata_pci_device_do_suspend(pdev, mesg);
6061c6fd2807SJeff Garzik 
6062c6fd2807SJeff Garzik 	return 0;
6063c6fd2807SJeff Garzik }
6064c6fd2807SJeff Garzik 
6065c6fd2807SJeff Garzik int ata_pci_device_resume(struct pci_dev *pdev)
6066c6fd2807SJeff Garzik {
6067cca3974eSJeff Garzik 	struct ata_host *host = dev_get_drvdata(&pdev->dev);
6068553c4aa6STejun Heo 	int rc;
6069c6fd2807SJeff Garzik 
6070553c4aa6STejun Heo 	rc = ata_pci_device_do_resume(pdev);
6071553c4aa6STejun Heo 	if (rc == 0)
6072cca3974eSJeff Garzik 		ata_host_resume(host);
6073553c4aa6STejun Heo 	return rc;
6074c6fd2807SJeff Garzik }
60756ffa01d8STejun Heo #endif /* CONFIG_PM */
60766ffa01d8STejun Heo 
6077c6fd2807SJeff Garzik #endif /* CONFIG_PCI */
6078c6fd2807SJeff Garzik 
607933267325STejun Heo static int __init ata_parse_force_one(char **cur,
608033267325STejun Heo 				      struct ata_force_ent *force_ent,
608133267325STejun Heo 				      const char **reason)
608233267325STejun Heo {
608333267325STejun Heo 	/* FIXME: Currently, there's no way to tag init const data and
608433267325STejun Heo 	 * using __initdata causes build failure on some versions of
608533267325STejun Heo 	 * gcc.  Once __initdataconst is implemented, add const to the
608633267325STejun Heo 	 * following structure.
608733267325STejun Heo 	 */
608833267325STejun Heo 	static struct ata_force_param force_tbl[] __initdata = {
608933267325STejun Heo 		{ "40c",	.cbl		= ATA_CBL_PATA40 },
609033267325STejun Heo 		{ "80c",	.cbl		= ATA_CBL_PATA80 },
609133267325STejun Heo 		{ "short40c",	.cbl		= ATA_CBL_PATA40_SHORT },
609233267325STejun Heo 		{ "unk",	.cbl		= ATA_CBL_PATA_UNK },
609333267325STejun Heo 		{ "ign",	.cbl		= ATA_CBL_PATA_IGN },
609433267325STejun Heo 		{ "sata",	.cbl		= ATA_CBL_SATA },
609533267325STejun Heo 		{ "1.5Gbps",	.spd_limit	= 1 },
609633267325STejun Heo 		{ "3.0Gbps",	.spd_limit	= 2 },
609733267325STejun Heo 		{ "noncq",	.horkage_on	= ATA_HORKAGE_NONCQ },
609833267325STejun Heo 		{ "ncq",	.horkage_off	= ATA_HORKAGE_NONCQ },
609933267325STejun Heo 		{ "pio0",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 0) },
610033267325STejun Heo 		{ "pio1",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 1) },
610133267325STejun Heo 		{ "pio2",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 2) },
610233267325STejun Heo 		{ "pio3",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 3) },
610333267325STejun Heo 		{ "pio4",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 4) },
610433267325STejun Heo 		{ "pio5",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 5) },
610533267325STejun Heo 		{ "pio6",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 6) },
610633267325STejun Heo 		{ "mwdma0",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 0) },
610733267325STejun Heo 		{ "mwdma1",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 1) },
610833267325STejun Heo 		{ "mwdma2",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 2) },
610933267325STejun Heo 		{ "mwdma3",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 3) },
611033267325STejun Heo 		{ "mwdma4",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 4) },
611133267325STejun Heo 		{ "udma0",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
611233267325STejun Heo 		{ "udma16",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
611333267325STejun Heo 		{ "udma/16",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
611433267325STejun Heo 		{ "udma1",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
611533267325STejun Heo 		{ "udma25",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
611633267325STejun Heo 		{ "udma/25",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
611733267325STejun Heo 		{ "udma2",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
611833267325STejun Heo 		{ "udma33",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
611933267325STejun Heo 		{ "udma/33",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
612033267325STejun Heo 		{ "udma3",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
612133267325STejun Heo 		{ "udma44",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
612233267325STejun Heo 		{ "udma/44",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
612333267325STejun Heo 		{ "udma4",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
612433267325STejun Heo 		{ "udma66",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
612533267325STejun Heo 		{ "udma/66",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
612633267325STejun Heo 		{ "udma5",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
612733267325STejun Heo 		{ "udma100",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
612833267325STejun Heo 		{ "udma/100",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
612933267325STejun Heo 		{ "udma6",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
613033267325STejun Heo 		{ "udma133",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
613133267325STejun Heo 		{ "udma/133",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
613233267325STejun Heo 		{ "udma7",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 7) },
613305944bdfSTejun Heo 		{ "nohrst",	.lflags		= ATA_LFLAG_NO_HRST },
613405944bdfSTejun Heo 		{ "nosrst",	.lflags		= ATA_LFLAG_NO_SRST },
613505944bdfSTejun Heo 		{ "norst",	.lflags		= ATA_LFLAG_NO_HRST | ATA_LFLAG_NO_SRST },
613633267325STejun Heo 	};
613733267325STejun Heo 	char *start = *cur, *p = *cur;
613833267325STejun Heo 	char *id, *val, *endp;
613933267325STejun Heo 	const struct ata_force_param *match_fp = NULL;
614033267325STejun Heo 	int nr_matches = 0, i;
614133267325STejun Heo 
614233267325STejun Heo 	/* find where this param ends and update *cur */
614333267325STejun Heo 	while (*p != '\0' && *p != ',')
614433267325STejun Heo 		p++;
614533267325STejun Heo 
614633267325STejun Heo 	if (*p == '\0')
614733267325STejun Heo 		*cur = p;
614833267325STejun Heo 	else
614933267325STejun Heo 		*cur = p + 1;
615033267325STejun Heo 
615133267325STejun Heo 	*p = '\0';
615233267325STejun Heo 
615333267325STejun Heo 	/* parse */
615433267325STejun Heo 	p = strchr(start, ':');
615533267325STejun Heo 	if (!p) {
615633267325STejun Heo 		val = strstrip(start);
615733267325STejun Heo 		goto parse_val;
615833267325STejun Heo 	}
615933267325STejun Heo 	*p = '\0';
616033267325STejun Heo 
616133267325STejun Heo 	id = strstrip(start);
616233267325STejun Heo 	val = strstrip(p + 1);
616333267325STejun Heo 
616433267325STejun Heo 	/* parse id */
616533267325STejun Heo 	p = strchr(id, '.');
616633267325STejun Heo 	if (p) {
616733267325STejun Heo 		*p++ = '\0';
616833267325STejun Heo 		force_ent->device = simple_strtoul(p, &endp, 10);
616933267325STejun Heo 		if (p == endp || *endp != '\0') {
617033267325STejun Heo 			*reason = "invalid device";
617133267325STejun Heo 			return -EINVAL;
617233267325STejun Heo 		}
617333267325STejun Heo 	}
617433267325STejun Heo 
617533267325STejun Heo 	force_ent->port = simple_strtoul(id, &endp, 10);
617633267325STejun Heo 	if (p == endp || *endp != '\0') {
617733267325STejun Heo 		*reason = "invalid port/link";
617833267325STejun Heo 		return -EINVAL;
617933267325STejun Heo 	}
618033267325STejun Heo 
618133267325STejun Heo  parse_val:
618233267325STejun Heo 	/* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */
618333267325STejun Heo 	for (i = 0; i < ARRAY_SIZE(force_tbl); i++) {
618433267325STejun Heo 		const struct ata_force_param *fp = &force_tbl[i];
618533267325STejun Heo 
618633267325STejun Heo 		if (strncasecmp(val, fp->name, strlen(val)))
618733267325STejun Heo 			continue;
618833267325STejun Heo 
618933267325STejun Heo 		nr_matches++;
619033267325STejun Heo 		match_fp = fp;
619133267325STejun Heo 
619233267325STejun Heo 		if (strcasecmp(val, fp->name) == 0) {
619333267325STejun Heo 			nr_matches = 1;
619433267325STejun Heo 			break;
619533267325STejun Heo 		}
619633267325STejun Heo 	}
619733267325STejun Heo 
619833267325STejun Heo 	if (!nr_matches) {
619933267325STejun Heo 		*reason = "unknown value";
620033267325STejun Heo 		return -EINVAL;
620133267325STejun Heo 	}
620233267325STejun Heo 	if (nr_matches > 1) {
620333267325STejun Heo 		*reason = "ambigious value";
620433267325STejun Heo 		return -EINVAL;
620533267325STejun Heo 	}
620633267325STejun Heo 
620733267325STejun Heo 	force_ent->param = *match_fp;
620833267325STejun Heo 
620933267325STejun Heo 	return 0;
621033267325STejun Heo }
621133267325STejun Heo 
621233267325STejun Heo static void __init ata_parse_force_param(void)
621333267325STejun Heo {
621433267325STejun Heo 	int idx = 0, size = 1;
621533267325STejun Heo 	int last_port = -1, last_device = -1;
621633267325STejun Heo 	char *p, *cur, *next;
621733267325STejun Heo 
621833267325STejun Heo 	/* calculate maximum number of params and allocate force_tbl */
621933267325STejun Heo 	for (p = ata_force_param_buf; *p; p++)
622033267325STejun Heo 		if (*p == ',')
622133267325STejun Heo 			size++;
622233267325STejun Heo 
622333267325STejun Heo 	ata_force_tbl = kzalloc(sizeof(ata_force_tbl[0]) * size, GFP_KERNEL);
622433267325STejun Heo 	if (!ata_force_tbl) {
622533267325STejun Heo 		printk(KERN_WARNING "ata: failed to extend force table, "
622633267325STejun Heo 		       "libata.force ignored\n");
622733267325STejun Heo 		return;
622833267325STejun Heo 	}
622933267325STejun Heo 
623033267325STejun Heo 	/* parse and populate the table */
623133267325STejun Heo 	for (cur = ata_force_param_buf; *cur != '\0'; cur = next) {
623233267325STejun Heo 		const char *reason = "";
623333267325STejun Heo 		struct ata_force_ent te = { .port = -1, .device = -1 };
623433267325STejun Heo 
623533267325STejun Heo 		next = cur;
623633267325STejun Heo 		if (ata_parse_force_one(&next, &te, &reason)) {
623733267325STejun Heo 			printk(KERN_WARNING "ata: failed to parse force "
623833267325STejun Heo 			       "parameter \"%s\" (%s)\n",
623933267325STejun Heo 			       cur, reason);
624033267325STejun Heo 			continue;
624133267325STejun Heo 		}
624233267325STejun Heo 
624333267325STejun Heo 		if (te.port == -1) {
624433267325STejun Heo 			te.port = last_port;
624533267325STejun Heo 			te.device = last_device;
624633267325STejun Heo 		}
624733267325STejun Heo 
624833267325STejun Heo 		ata_force_tbl[idx++] = te;
624933267325STejun Heo 
625033267325STejun Heo 		last_port = te.port;
625133267325STejun Heo 		last_device = te.device;
625233267325STejun Heo 	}
625333267325STejun Heo 
625433267325STejun Heo 	ata_force_tbl_size = idx;
625533267325STejun Heo }
6256c6fd2807SJeff Garzik 
6257c6fd2807SJeff Garzik static int __init ata_init(void)
6258c6fd2807SJeff Garzik {
625933267325STejun Heo 	ata_parse_force_param();
626033267325STejun Heo 
6261c6fd2807SJeff Garzik 	ata_wq = create_workqueue("ata");
6262c6fd2807SJeff Garzik 	if (!ata_wq)
626349ea3b04SElias Oltmanns 		goto free_force_tbl;
6264c6fd2807SJeff Garzik 
6265c6fd2807SJeff Garzik 	ata_aux_wq = create_singlethread_workqueue("ata_aux");
626649ea3b04SElias Oltmanns 	if (!ata_aux_wq)
626749ea3b04SElias Oltmanns 		goto free_wq;
6268c6fd2807SJeff Garzik 
6269c6fd2807SJeff Garzik 	printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
6270c6fd2807SJeff Garzik 	return 0;
627149ea3b04SElias Oltmanns 
627249ea3b04SElias Oltmanns free_wq:
627349ea3b04SElias Oltmanns 	destroy_workqueue(ata_wq);
627449ea3b04SElias Oltmanns free_force_tbl:
627549ea3b04SElias Oltmanns 	kfree(ata_force_tbl);
627649ea3b04SElias Oltmanns 	return -ENOMEM;
6277c6fd2807SJeff Garzik }
6278c6fd2807SJeff Garzik 
6279c6fd2807SJeff Garzik static void __exit ata_exit(void)
6280c6fd2807SJeff Garzik {
628133267325STejun Heo 	kfree(ata_force_tbl);
6282c6fd2807SJeff Garzik 	destroy_workqueue(ata_wq);
6283c6fd2807SJeff Garzik 	destroy_workqueue(ata_aux_wq);
6284c6fd2807SJeff Garzik }
6285c6fd2807SJeff Garzik 
6286a4625085SBrian King subsys_initcall(ata_init);
6287c6fd2807SJeff Garzik module_exit(ata_exit);
6288c6fd2807SJeff Garzik 
6289c6fd2807SJeff Garzik static unsigned long ratelimit_time;
6290c6fd2807SJeff Garzik static DEFINE_SPINLOCK(ata_ratelimit_lock);
6291c6fd2807SJeff Garzik 
6292c6fd2807SJeff Garzik int ata_ratelimit(void)
6293c6fd2807SJeff Garzik {
6294c6fd2807SJeff Garzik 	int rc;
6295c6fd2807SJeff Garzik 	unsigned long flags;
6296c6fd2807SJeff Garzik 
6297c6fd2807SJeff Garzik 	spin_lock_irqsave(&ata_ratelimit_lock, flags);
6298c6fd2807SJeff Garzik 
6299c6fd2807SJeff Garzik 	if (time_after(jiffies, ratelimit_time)) {
6300c6fd2807SJeff Garzik 		rc = 1;
6301c6fd2807SJeff Garzik 		ratelimit_time = jiffies + (HZ/5);
6302c6fd2807SJeff Garzik 	} else
6303c6fd2807SJeff Garzik 		rc = 0;
6304c6fd2807SJeff Garzik 
6305c6fd2807SJeff Garzik 	spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
6306c6fd2807SJeff Garzik 
6307c6fd2807SJeff Garzik 	return rc;
6308c6fd2807SJeff Garzik }
6309c6fd2807SJeff Garzik 
6310c6fd2807SJeff Garzik /**
6311c6fd2807SJeff Garzik  *	ata_wait_register - wait until register value changes
6312c6fd2807SJeff Garzik  *	@reg: IO-mapped register
6313c6fd2807SJeff Garzik  *	@mask: Mask to apply to read register value
6314c6fd2807SJeff Garzik  *	@val: Wait condition
6315341c2c95STejun Heo  *	@interval: polling interval in milliseconds
6316341c2c95STejun Heo  *	@timeout: timeout in milliseconds
6317c6fd2807SJeff Garzik  *
6318c6fd2807SJeff Garzik  *	Waiting for some bits of register to change is a common
6319c6fd2807SJeff Garzik  *	operation for ATA controllers.  This function reads 32bit LE
6320c6fd2807SJeff Garzik  *	IO-mapped register @reg and tests for the following condition.
6321c6fd2807SJeff Garzik  *
6322c6fd2807SJeff Garzik  *	(*@reg & mask) != val
6323c6fd2807SJeff Garzik  *
6324c6fd2807SJeff Garzik  *	If the condition is met, it returns; otherwise, the process is
6325c6fd2807SJeff Garzik  *	repeated after @interval_msec until timeout.
6326c6fd2807SJeff Garzik  *
6327c6fd2807SJeff Garzik  *	LOCKING:
6328c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
6329c6fd2807SJeff Garzik  *
6330c6fd2807SJeff Garzik  *	RETURNS:
6331c6fd2807SJeff Garzik  *	The final register value.
6332c6fd2807SJeff Garzik  */
6333c6fd2807SJeff Garzik u32 ata_wait_register(void __iomem *reg, u32 mask, u32 val,
6334341c2c95STejun Heo 		      unsigned long interval, unsigned long timeout)
6335c6fd2807SJeff Garzik {
6336341c2c95STejun Heo 	unsigned long deadline;
6337c6fd2807SJeff Garzik 	u32 tmp;
6338c6fd2807SJeff Garzik 
6339c6fd2807SJeff Garzik 	tmp = ioread32(reg);
6340c6fd2807SJeff Garzik 
6341c6fd2807SJeff Garzik 	/* Calculate timeout _after_ the first read to make sure
6342c6fd2807SJeff Garzik 	 * preceding writes reach the controller before starting to
6343c6fd2807SJeff Garzik 	 * eat away the timeout.
6344c6fd2807SJeff Garzik 	 */
6345341c2c95STejun Heo 	deadline = ata_deadline(jiffies, timeout);
6346c6fd2807SJeff Garzik 
6347341c2c95STejun Heo 	while ((tmp & mask) == val && time_before(jiffies, deadline)) {
6348341c2c95STejun Heo 		msleep(interval);
6349c6fd2807SJeff Garzik 		tmp = ioread32(reg);
6350c6fd2807SJeff Garzik 	}
6351c6fd2807SJeff Garzik 
6352c6fd2807SJeff Garzik 	return tmp;
6353c6fd2807SJeff Garzik }
6354c6fd2807SJeff Garzik 
6355c6fd2807SJeff Garzik /*
6356c6fd2807SJeff Garzik  * Dummy port_ops
6357c6fd2807SJeff Garzik  */
6358c6fd2807SJeff Garzik static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
6359c6fd2807SJeff Garzik {
6360c6fd2807SJeff Garzik 	return AC_ERR_SYSTEM;
6361c6fd2807SJeff Garzik }
6362c6fd2807SJeff Garzik 
6363182d7bbaSTejun Heo static void ata_dummy_error_handler(struct ata_port *ap)
6364182d7bbaSTejun Heo {
6365182d7bbaSTejun Heo 	/* truly dummy */
6366182d7bbaSTejun Heo }
6367182d7bbaSTejun Heo 
6368029cfd6bSTejun Heo struct ata_port_operations ata_dummy_port_ops = {
6369c6fd2807SJeff Garzik 	.qc_prep		= ata_noop_qc_prep,
6370c6fd2807SJeff Garzik 	.qc_issue		= ata_dummy_qc_issue,
6371182d7bbaSTejun Heo 	.error_handler		= ata_dummy_error_handler,
6372c6fd2807SJeff Garzik };
6373c6fd2807SJeff Garzik 
637421b0ad4fSTejun Heo const struct ata_port_info ata_dummy_port_info = {
637521b0ad4fSTejun Heo 	.port_ops		= &ata_dummy_port_ops,
637621b0ad4fSTejun Heo };
637721b0ad4fSTejun Heo 
6378c6fd2807SJeff Garzik /*
6379c6fd2807SJeff Garzik  * libata is essentially a library of internal helper functions for
6380c6fd2807SJeff Garzik  * low-level ATA host controller drivers.  As such, the API/ABI is
6381c6fd2807SJeff Garzik  * likely to change as new drivers are added and updated.
6382c6fd2807SJeff Garzik  * Do not depend on ABI/API stability.
6383c6fd2807SJeff Garzik  */
6384c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
6385c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
6386c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_deb_timing_long);
6387029cfd6bSTejun Heo EXPORT_SYMBOL_GPL(ata_base_port_ops);
6388029cfd6bSTejun Heo EXPORT_SYMBOL_GPL(sata_port_ops);
6389c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
639021b0ad4fSTejun Heo EXPORT_SYMBOL_GPL(ata_dummy_port_info);
6391aadffb68STejun Heo EXPORT_SYMBOL_GPL(__ata_port_next_link);
6392c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_std_bios_param);
6393cca3974eSJeff Garzik EXPORT_SYMBOL_GPL(ata_host_init);
6394f3187195STejun Heo EXPORT_SYMBOL_GPL(ata_host_alloc);
6395f5cda257STejun Heo EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
6396b1c72916STejun Heo EXPORT_SYMBOL_GPL(ata_slave_link_init);
6397ecef7253STejun Heo EXPORT_SYMBOL_GPL(ata_host_start);
6398f3187195STejun Heo EXPORT_SYMBOL_GPL(ata_host_register);
6399f5cda257STejun Heo EXPORT_SYMBOL_GPL(ata_host_activate);
64000529c159STejun Heo EXPORT_SYMBOL_GPL(ata_host_detach);
6401c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_sg_init);
6402c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_qc_complete);
6403c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
6404436d34b3STejun Heo EXPORT_SYMBOL_GPL(atapi_cmd_type);
6405c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_tf_to_fis);
6406c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_tf_from_fis);
64076357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_pack_xfermask);
64086357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_unpack_xfermask);
64096357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
64106357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
64116357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
64126357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_mode_string);
64136357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_id_xfermask);
6414c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_start);
641504351821SAlan EXPORT_SYMBOL_GPL(ata_do_set_mode);
641631cc23b3STejun Heo EXPORT_SYMBOL_GPL(ata_std_qc_defer);
6417c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
6418c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_probe);
641910305f0fSAlan EXPORT_SYMBOL_GPL(ata_dev_disable);
6420c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_set_spd);
6421aa2731adSTejun Heo EXPORT_SYMBOL_GPL(ata_wait_after_reset);
6422936fd732STejun Heo EXPORT_SYMBOL_GPL(sata_link_debounce);
6423936fd732STejun Heo EXPORT_SYMBOL_GPL(sata_link_resume);
64240aa1113dSTejun Heo EXPORT_SYMBOL_GPL(ata_std_prereset);
6425cc0680a5STejun Heo EXPORT_SYMBOL_GPL(sata_link_hardreset);
642657c9efdfSTejun Heo EXPORT_SYMBOL_GPL(sata_std_hardreset);
6427203c75b8STejun Heo EXPORT_SYMBOL_GPL(ata_std_postreset);
6428c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_dev_classify);
6429c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_dev_pair);
6430c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_disable);
6431c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_ratelimit);
6432c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_wait_register);
6433c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_ioctl);
6434c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
6435c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
6436c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
6437c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
6438c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_valid);
6439c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_read);
6440c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_write);
6441c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_write_flush);
6442936fd732STejun Heo EXPORT_SYMBOL_GPL(ata_link_online);
6443936fd732STejun Heo EXPORT_SYMBOL_GPL(ata_link_offline);
64446ffa01d8STejun Heo #ifdef CONFIG_PM
6445cca3974eSJeff Garzik EXPORT_SYMBOL_GPL(ata_host_suspend);
6446cca3974eSJeff Garzik EXPORT_SYMBOL_GPL(ata_host_resume);
64476ffa01d8STejun Heo #endif /* CONFIG_PM */
6448c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_id_string);
6449c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_id_c_string);
6450963e4975SAlan Cox EXPORT_SYMBOL_GPL(ata_do_dev_read_id);
6451c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_simulate);
6452c6fd2807SJeff Garzik 
6453c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
64546357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_timing_find_mode);
6455c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_timing_compute);
6456c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_timing_merge);
6457a0f79b92STejun Heo EXPORT_SYMBOL_GPL(ata_timing_cycle2mode);
6458c6fd2807SJeff Garzik 
6459c6fd2807SJeff Garzik #ifdef CONFIG_PCI
6460c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(pci_test_config_bits);
6461c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_remove_one);
64626ffa01d8STejun Heo #ifdef CONFIG_PM
6463c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
6464c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
6465c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
6466c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_resume);
64676ffa01d8STejun Heo #endif /* CONFIG_PM */
6468c6fd2807SJeff Garzik #endif /* CONFIG_PCI */
6469c6fd2807SJeff Garzik 
6470b64bbc39STejun Heo EXPORT_SYMBOL_GPL(__ata_ehi_push_desc);
6471b64bbc39STejun Heo EXPORT_SYMBOL_GPL(ata_ehi_push_desc);
6472b64bbc39STejun Heo EXPORT_SYMBOL_GPL(ata_ehi_clear_desc);
6473cbcdd875STejun Heo EXPORT_SYMBOL_GPL(ata_port_desc);
6474cbcdd875STejun Heo #ifdef CONFIG_PCI
6475cbcdd875STejun Heo EXPORT_SYMBOL_GPL(ata_port_pbar_desc);
6476cbcdd875STejun Heo #endif /* CONFIG_PCI */
6477c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
6478dbd82616STejun Heo EXPORT_SYMBOL_GPL(ata_link_abort);
6479c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_abort);
6480c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_freeze);
64817d77b247STejun Heo EXPORT_SYMBOL_GPL(sata_async_notification);
6482c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
6483c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
6484c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
6485c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
648610acf3b0SMark Lord EXPORT_SYMBOL_GPL(ata_eh_analyze_ncq_error);
6487c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_do_eh);
6488a1efdabaSTejun Heo EXPORT_SYMBOL_GPL(ata_std_error_handler);
6489be0d18dfSAlan Cox 
6490be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_40wire);
6491be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_80wire);
6492be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_unknown);
6493c88f90c3STejun Heo EXPORT_SYMBOL_GPL(ata_cable_ignore);
6494be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_sata);
6495