xref: /openbmc/linux/drivers/ata/libata-core.c (revision 2f9e4d16)
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>
5979318057SArjan van de Ven #include <linux/async.h>
60e18086d6SMark Lord #include <linux/log2.h>
615a0e3ad6STejun Heo #include <linux/slab.h>
62c6fd2807SJeff Garzik #include <scsi/scsi.h>
63c6fd2807SJeff Garzik #include <scsi/scsi_cmnd.h>
64c6fd2807SJeff Garzik #include <scsi/scsi_host.h>
65c6fd2807SJeff Garzik #include <linux/libata.h>
66c6fd2807SJeff Garzik #include <asm/byteorder.h>
67140b5e59STejun Heo #include <linux/cdrom.h>
689990b6f3SAkinobu Mita #include <linux/ratelimit.h>
69c6fd2807SJeff Garzik 
70c6fd2807SJeff Garzik #include "libata.h"
71c6fd2807SJeff Garzik 
72fda0efc5SJeff Garzik 
73c6fd2807SJeff Garzik /* debounce timing parameters in msecs { interval, duration, timeout } */
74c6fd2807SJeff Garzik const unsigned long sata_deb_timing_normal[]		= {   5,  100, 2000 };
75c6fd2807SJeff Garzik const unsigned long sata_deb_timing_hotplug[]		= {  25,  500, 2000 };
76c6fd2807SJeff Garzik const unsigned long sata_deb_timing_long[]		= { 100, 2000, 5000 };
77c6fd2807SJeff Garzik 
78029cfd6bSTejun Heo const struct ata_port_operations ata_base_port_ops = {
790aa1113dSTejun Heo 	.prereset		= ata_std_prereset,
80203c75b8STejun Heo 	.postreset		= ata_std_postreset,
81a1efdabaSTejun Heo 	.error_handler		= ata_std_error_handler,
82029cfd6bSTejun Heo };
83029cfd6bSTejun Heo 
84029cfd6bSTejun Heo const struct ata_port_operations sata_port_ops = {
85029cfd6bSTejun Heo 	.inherits		= &ata_base_port_ops,
86029cfd6bSTejun Heo 
87029cfd6bSTejun Heo 	.qc_defer		= ata_std_qc_defer,
8857c9efdfSTejun Heo 	.hardreset		= sata_std_hardreset,
89029cfd6bSTejun Heo };
90029cfd6bSTejun Heo 
91c6fd2807SJeff Garzik static unsigned int ata_dev_init_params(struct ata_device *dev,
92c6fd2807SJeff Garzik 					u16 heads, u16 sectors);
93c6fd2807SJeff Garzik static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
94218f3d30SJeff Garzik static unsigned int ata_dev_set_feature(struct ata_device *dev,
95218f3d30SJeff Garzik 					u8 enable, u8 feature);
96c6fd2807SJeff Garzik static void ata_dev_xfermask(struct ata_device *dev);
9775683fe7STejun Heo static unsigned long ata_dev_blacklisted(const struct ata_device *dev);
98c6fd2807SJeff Garzik 
99f3187195STejun Heo unsigned int ata_print_id = 1;
100c6fd2807SJeff Garzik 
101c6fd2807SJeff Garzik struct workqueue_struct *ata_aux_wq;
102c6fd2807SJeff Garzik 
10333267325STejun Heo struct ata_force_param {
10433267325STejun Heo 	const char	*name;
10533267325STejun Heo 	unsigned int	cbl;
10633267325STejun Heo 	int		spd_limit;
10733267325STejun Heo 	unsigned long	xfer_mask;
10833267325STejun Heo 	unsigned int	horkage_on;
10933267325STejun Heo 	unsigned int	horkage_off;
11005944bdfSTejun Heo 	unsigned int	lflags;
11133267325STejun Heo };
11233267325STejun Heo 
11333267325STejun Heo struct ata_force_ent {
11433267325STejun Heo 	int			port;
11533267325STejun Heo 	int			device;
11633267325STejun Heo 	struct ata_force_param	param;
11733267325STejun Heo };
11833267325STejun Heo 
11933267325STejun Heo static struct ata_force_ent *ata_force_tbl;
12033267325STejun Heo static int ata_force_tbl_size;
12133267325STejun Heo 
12233267325STejun Heo static char ata_force_param_buf[PAGE_SIZE] __initdata;
1237afb4222STejun Heo /* param_buf is thrown away after initialization, disallow read */
1247afb4222STejun Heo module_param_string(force, ata_force_param_buf, sizeof(ata_force_param_buf), 0);
12533267325STejun Heo MODULE_PARM_DESC(force, "Force ATA configurations including cable type, link speed and transfer mode (see Documentation/kernel-parameters.txt for details)");
12633267325STejun Heo 
1272486fa56STejun Heo static int atapi_enabled = 1;
128c6fd2807SJeff Garzik module_param(atapi_enabled, int, 0444);
129ad5d8eacSEvgeni Golov MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on [default])");
130c6fd2807SJeff Garzik 
131c5c61bdaSAdrian Bunk static int atapi_dmadir = 0;
132c6fd2807SJeff Garzik module_param(atapi_dmadir, int, 0444);
133ad5d8eacSEvgeni Golov MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off [default], 1=on)");
134c6fd2807SJeff Garzik 
135baf4fdfaSMark Lord int atapi_passthru16 = 1;
136baf4fdfaSMark Lord module_param(atapi_passthru16, int, 0444);
137ad5d8eacSEvgeni Golov MODULE_PARM_DESC(atapi_passthru16, "Enable ATA_16 passthru for ATAPI devices (0=off, 1=on [default])");
138baf4fdfaSMark Lord 
139c6fd2807SJeff Garzik int libata_fua = 0;
140c6fd2807SJeff Garzik module_param_named(fua, libata_fua, int, 0444);
141ad5d8eacSEvgeni Golov MODULE_PARM_DESC(fua, "FUA support (0=off [default], 1=on)");
142c6fd2807SJeff Garzik 
1432dcb407eSJeff Garzik static int ata_ignore_hpa;
1441e999736SAlan Cox module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644);
1451e999736SAlan Cox MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)");
1461e999736SAlan Cox 
147b3a70601SAlan Cox static int libata_dma_mask = ATA_DMA_MASK_ATA|ATA_DMA_MASK_ATAPI|ATA_DMA_MASK_CFA;
148b3a70601SAlan Cox module_param_named(dma, libata_dma_mask, int, 0444);
149b3a70601SAlan Cox MODULE_PARM_DESC(dma, "DMA enable/disable (0x1==ATA, 0x2==ATAPI, 0x4==CF)");
150b3a70601SAlan Cox 
15187fbc5a0STejun Heo static int ata_probe_timeout;
152c6fd2807SJeff Garzik module_param(ata_probe_timeout, int, 0444);
153c6fd2807SJeff Garzik MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
154c6fd2807SJeff Garzik 
1556ebe9d86SJeff Garzik int libata_noacpi = 0;
156d7d0dad6SJeff Garzik module_param_named(noacpi, libata_noacpi, int, 0444);
157ad5d8eacSEvgeni Golov MODULE_PARM_DESC(noacpi, "Disable the use of ACPI in probe/suspend/resume (0=off [default], 1=on)");
15811ef697bSKristen Carlson Accardi 
159ae8d4ee7SAlan Cox int libata_allow_tpm = 0;
160ae8d4ee7SAlan Cox module_param_named(allow_tpm, libata_allow_tpm, int, 0444);
161ad5d8eacSEvgeni Golov MODULE_PARM_DESC(allow_tpm, "Permit the use of TPM commands (0=off [default], 1=on)");
162ae8d4ee7SAlan Cox 
163e7ecd435STejun Heo static int atapi_an;
164e7ecd435STejun Heo module_param(atapi_an, int, 0444);
165e7ecd435STejun Heo MODULE_PARM_DESC(atapi_an, "Enable ATAPI AN media presence notification (0=0ff [default], 1=on)");
166e7ecd435STejun Heo 
167c6fd2807SJeff Garzik MODULE_AUTHOR("Jeff Garzik");
168c6fd2807SJeff Garzik MODULE_DESCRIPTION("Library module for ATA devices");
169c6fd2807SJeff Garzik MODULE_LICENSE("GPL");
170c6fd2807SJeff Garzik MODULE_VERSION(DRV_VERSION);
171c6fd2807SJeff Garzik 
172c6fd2807SJeff Garzik 
1739913ff8aSTejun Heo static bool ata_sstatus_online(u32 sstatus)
1749913ff8aSTejun Heo {
1759913ff8aSTejun Heo 	return (sstatus & 0xf) == 0x3;
1769913ff8aSTejun Heo }
1779913ff8aSTejun Heo 
1781eca4365STejun Heo /**
1791eca4365STejun Heo  *	ata_link_next - link iteration helper
1801eca4365STejun Heo  *	@link: the previous link, NULL to start
1811eca4365STejun Heo  *	@ap: ATA port containing links to iterate
1821eca4365STejun Heo  *	@mode: iteration mode, one of ATA_LITER_*
183aadffb68STejun Heo  *
184aadffb68STejun Heo  *	LOCKING:
185aadffb68STejun Heo  *	Host lock or EH context.
1861eca4365STejun Heo  *
1871eca4365STejun Heo  *	RETURNS:
1881eca4365STejun Heo  *	Pointer to the next link.
189aadffb68STejun Heo  */
1901eca4365STejun Heo struct ata_link *ata_link_next(struct ata_link *link, struct ata_port *ap,
1911eca4365STejun Heo 			       enum ata_link_iter_mode mode)
192aadffb68STejun Heo {
1931eca4365STejun Heo 	BUG_ON(mode != ATA_LITER_EDGE &&
1941eca4365STejun Heo 	       mode != ATA_LITER_PMP_FIRST && mode != ATA_LITER_HOST_FIRST);
1951eca4365STejun Heo 
196aadffb68STejun Heo 	/* NULL link indicates start of iteration */
1971eca4365STejun Heo 	if (!link)
1981eca4365STejun Heo 		switch (mode) {
1991eca4365STejun Heo 		case ATA_LITER_EDGE:
2001eca4365STejun Heo 		case ATA_LITER_PMP_FIRST:
2011eca4365STejun Heo 			if (sata_pmp_attached(ap))
202aadffb68STejun Heo 				return ap->pmp_link;
2031eca4365STejun Heo 			/* fall through */
2041eca4365STejun Heo 		case ATA_LITER_HOST_FIRST:
205aadffb68STejun Heo 			return &ap->link;
206aadffb68STejun Heo 		}
207aadffb68STejun Heo 
2081eca4365STejun Heo 	/* we just iterated over the host link, what's next? */
2091eca4365STejun Heo 	if (link == &ap->link)
2101eca4365STejun Heo 		switch (mode) {
2111eca4365STejun Heo 		case ATA_LITER_HOST_FIRST:
2121eca4365STejun Heo 			if (sata_pmp_attached(ap))
213aadffb68STejun Heo 				return ap->pmp_link;
2141eca4365STejun Heo 			/* fall through */
2151eca4365STejun Heo 		case ATA_LITER_PMP_FIRST:
2161eca4365STejun Heo 			if (unlikely(ap->slave_link))
2171eca4365STejun Heo 				return ap->slave_link;
2181eca4365STejun Heo 			/* fall through */
2191eca4365STejun Heo 		case ATA_LITER_EDGE:
2201eca4365STejun Heo 			return NULL;
221aadffb68STejun Heo 		}
222aadffb68STejun Heo 
223b1c72916STejun Heo 	/* slave_link excludes PMP */
224b1c72916STejun Heo 	if (unlikely(link == ap->slave_link))
225b1c72916STejun Heo 		return NULL;
226b1c72916STejun Heo 
2271eca4365STejun Heo 	/* we were over a PMP link */
228aadffb68STejun Heo 	if (++link < ap->pmp_link + ap->nr_pmp_links)
229aadffb68STejun Heo 		return link;
2301eca4365STejun Heo 
2311eca4365STejun Heo 	if (mode == ATA_LITER_PMP_FIRST)
2321eca4365STejun Heo 		return &ap->link;
2331eca4365STejun Heo 
234aadffb68STejun Heo 	return NULL;
235aadffb68STejun Heo }
236aadffb68STejun Heo 
237c6fd2807SJeff Garzik /**
2381eca4365STejun Heo  *	ata_dev_next - device iteration helper
2391eca4365STejun Heo  *	@dev: the previous device, NULL to start
2401eca4365STejun Heo  *	@link: ATA link containing devices to iterate
2411eca4365STejun Heo  *	@mode: iteration mode, one of ATA_DITER_*
2421eca4365STejun Heo  *
2431eca4365STejun Heo  *	LOCKING:
2441eca4365STejun Heo  *	Host lock or EH context.
2451eca4365STejun Heo  *
2461eca4365STejun Heo  *	RETURNS:
2471eca4365STejun Heo  *	Pointer to the next device.
2481eca4365STejun Heo  */
2491eca4365STejun Heo struct ata_device *ata_dev_next(struct ata_device *dev, struct ata_link *link,
2501eca4365STejun Heo 				enum ata_dev_iter_mode mode)
2511eca4365STejun Heo {
2521eca4365STejun Heo 	BUG_ON(mode != ATA_DITER_ENABLED && mode != ATA_DITER_ENABLED_REVERSE &&
2531eca4365STejun Heo 	       mode != ATA_DITER_ALL && mode != ATA_DITER_ALL_REVERSE);
2541eca4365STejun Heo 
2551eca4365STejun Heo 	/* NULL dev indicates start of iteration */
2561eca4365STejun Heo 	if (!dev)
2571eca4365STejun Heo 		switch (mode) {
2581eca4365STejun Heo 		case ATA_DITER_ENABLED:
2591eca4365STejun Heo 		case ATA_DITER_ALL:
2601eca4365STejun Heo 			dev = link->device;
2611eca4365STejun Heo 			goto check;
2621eca4365STejun Heo 		case ATA_DITER_ENABLED_REVERSE:
2631eca4365STejun Heo 		case ATA_DITER_ALL_REVERSE:
2641eca4365STejun Heo 			dev = link->device + ata_link_max_devices(link) - 1;
2651eca4365STejun Heo 			goto check;
2661eca4365STejun Heo 		}
2671eca4365STejun Heo 
2681eca4365STejun Heo  next:
2691eca4365STejun Heo 	/* move to the next one */
2701eca4365STejun Heo 	switch (mode) {
2711eca4365STejun Heo 	case ATA_DITER_ENABLED:
2721eca4365STejun Heo 	case ATA_DITER_ALL:
2731eca4365STejun Heo 		if (++dev < link->device + ata_link_max_devices(link))
2741eca4365STejun Heo 			goto check;
2751eca4365STejun Heo 		return NULL;
2761eca4365STejun Heo 	case ATA_DITER_ENABLED_REVERSE:
2771eca4365STejun Heo 	case ATA_DITER_ALL_REVERSE:
2781eca4365STejun Heo 		if (--dev >= link->device)
2791eca4365STejun Heo 			goto check;
2801eca4365STejun Heo 		return NULL;
2811eca4365STejun Heo 	}
2821eca4365STejun Heo 
2831eca4365STejun Heo  check:
2841eca4365STejun Heo 	if ((mode == ATA_DITER_ENABLED || mode == ATA_DITER_ENABLED_REVERSE) &&
2851eca4365STejun Heo 	    !ata_dev_enabled(dev))
2861eca4365STejun Heo 		goto next;
2871eca4365STejun Heo 	return dev;
2881eca4365STejun Heo }
2891eca4365STejun Heo 
2901eca4365STejun Heo /**
291b1c72916STejun Heo  *	ata_dev_phys_link - find physical link for a device
292b1c72916STejun Heo  *	@dev: ATA device to look up physical link for
293b1c72916STejun Heo  *
294b1c72916STejun Heo  *	Look up physical link which @dev is attached to.  Note that
295b1c72916STejun Heo  *	this is different from @dev->link only when @dev is on slave
296b1c72916STejun Heo  *	link.  For all other cases, it's the same as @dev->link.
297b1c72916STejun Heo  *
298b1c72916STejun Heo  *	LOCKING:
299b1c72916STejun Heo  *	Don't care.
300b1c72916STejun Heo  *
301b1c72916STejun Heo  *	RETURNS:
302b1c72916STejun Heo  *	Pointer to the found physical link.
303b1c72916STejun Heo  */
304b1c72916STejun Heo struct ata_link *ata_dev_phys_link(struct ata_device *dev)
305b1c72916STejun Heo {
306b1c72916STejun Heo 	struct ata_port *ap = dev->link->ap;
307b1c72916STejun Heo 
308b1c72916STejun Heo 	if (!ap->slave_link)
309b1c72916STejun Heo 		return dev->link;
310b1c72916STejun Heo 	if (!dev->devno)
311b1c72916STejun Heo 		return &ap->link;
312b1c72916STejun Heo 	return ap->slave_link;
313b1c72916STejun Heo }
314b1c72916STejun Heo 
315b1c72916STejun Heo /**
31633267325STejun Heo  *	ata_force_cbl - force cable type according to libata.force
3174cdfa1b3SRandy Dunlap  *	@ap: ATA port of interest
31833267325STejun Heo  *
31933267325STejun Heo  *	Force cable type according to libata.force and whine about it.
32033267325STejun Heo  *	The last entry which has matching port number is used, so it
32133267325STejun Heo  *	can be specified as part of device force parameters.  For
32233267325STejun Heo  *	example, both "a:40c,1.00:udma4" and "1.00:40c,udma4" have the
32333267325STejun Heo  *	same effect.
32433267325STejun Heo  *
32533267325STejun Heo  *	LOCKING:
32633267325STejun Heo  *	EH context.
32733267325STejun Heo  */
32833267325STejun Heo void ata_force_cbl(struct ata_port *ap)
32933267325STejun Heo {
33033267325STejun Heo 	int i;
33133267325STejun Heo 
33233267325STejun Heo 	for (i = ata_force_tbl_size - 1; i >= 0; i--) {
33333267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
33433267325STejun Heo 
33533267325STejun Heo 		if (fe->port != -1 && fe->port != ap->print_id)
33633267325STejun Heo 			continue;
33733267325STejun Heo 
33833267325STejun Heo 		if (fe->param.cbl == ATA_CBL_NONE)
33933267325STejun Heo 			continue;
34033267325STejun Heo 
34133267325STejun Heo 		ap->cbl = fe->param.cbl;
34233267325STejun Heo 		ata_port_printk(ap, KERN_NOTICE,
34333267325STejun Heo 				"FORCE: cable set to %s\n", fe->param.name);
34433267325STejun Heo 		return;
34533267325STejun Heo 	}
34633267325STejun Heo }
34733267325STejun Heo 
34833267325STejun Heo /**
34905944bdfSTejun Heo  *	ata_force_link_limits - force link limits according to libata.force
35033267325STejun Heo  *	@link: ATA link of interest
35133267325STejun Heo  *
35205944bdfSTejun Heo  *	Force link flags and SATA spd limit according to libata.force
35305944bdfSTejun Heo  *	and whine about it.  When only the port part is specified
35405944bdfSTejun Heo  *	(e.g. 1:), the limit applies to all links connected to both
35505944bdfSTejun Heo  *	the host link and all fan-out ports connected via PMP.  If the
35605944bdfSTejun Heo  *	device part is specified as 0 (e.g. 1.00:), it specifies the
35705944bdfSTejun Heo  *	first fan-out link not the host link.  Device number 15 always
358b1c72916STejun Heo  *	points to the host link whether PMP is attached or not.  If the
359b1c72916STejun Heo  *	controller has slave link, device number 16 points to it.
36033267325STejun Heo  *
36133267325STejun Heo  *	LOCKING:
36233267325STejun Heo  *	EH context.
36333267325STejun Heo  */
36405944bdfSTejun Heo static void ata_force_link_limits(struct ata_link *link)
36533267325STejun Heo {
36605944bdfSTejun Heo 	bool did_spd = false;
367b1c72916STejun Heo 	int linkno = link->pmp;
368b1c72916STejun Heo 	int i;
36933267325STejun Heo 
37033267325STejun Heo 	if (ata_is_host_link(link))
371b1c72916STejun Heo 		linkno += 15;
37233267325STejun Heo 
37333267325STejun Heo 	for (i = ata_force_tbl_size - 1; i >= 0; i--) {
37433267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
37533267325STejun Heo 
37633267325STejun Heo 		if (fe->port != -1 && fe->port != link->ap->print_id)
37733267325STejun Heo 			continue;
37833267325STejun Heo 
37933267325STejun Heo 		if (fe->device != -1 && fe->device != linkno)
38033267325STejun Heo 			continue;
38133267325STejun Heo 
38205944bdfSTejun Heo 		/* only honor the first spd limit */
38305944bdfSTejun Heo 		if (!did_spd && fe->param.spd_limit) {
38433267325STejun Heo 			link->hw_sata_spd_limit = (1 << fe->param.spd_limit) - 1;
38533267325STejun Heo 			ata_link_printk(link, KERN_NOTICE,
38605944bdfSTejun Heo 					"FORCE: PHY spd limit set to %s\n",
38705944bdfSTejun Heo 					fe->param.name);
38805944bdfSTejun Heo 			did_spd = true;
38905944bdfSTejun Heo 		}
39005944bdfSTejun Heo 
39105944bdfSTejun Heo 		/* let lflags stack */
39205944bdfSTejun Heo 		if (fe->param.lflags) {
39305944bdfSTejun Heo 			link->flags |= fe->param.lflags;
39405944bdfSTejun Heo 			ata_link_printk(link, KERN_NOTICE,
39505944bdfSTejun Heo 					"FORCE: link flag 0x%x forced -> 0x%x\n",
39605944bdfSTejun Heo 					fe->param.lflags, link->flags);
39705944bdfSTejun Heo 		}
39833267325STejun Heo 	}
39933267325STejun Heo }
40033267325STejun Heo 
40133267325STejun Heo /**
40233267325STejun Heo  *	ata_force_xfermask - force xfermask according to libata.force
40333267325STejun Heo  *	@dev: ATA device of interest
40433267325STejun Heo  *
40533267325STejun Heo  *	Force xfer_mask according to libata.force and whine about it.
40633267325STejun Heo  *	For consistency with link selection, device number 15 selects
40733267325STejun Heo  *	the first device connected to the host link.
40833267325STejun Heo  *
40933267325STejun Heo  *	LOCKING:
41033267325STejun Heo  *	EH context.
41133267325STejun Heo  */
41233267325STejun Heo static void ata_force_xfermask(struct ata_device *dev)
41333267325STejun Heo {
41433267325STejun Heo 	int devno = dev->link->pmp + dev->devno;
41533267325STejun Heo 	int alt_devno = devno;
41633267325STejun Heo 	int i;
41733267325STejun Heo 
418b1c72916STejun Heo 	/* allow n.15/16 for devices attached to host port */
419b1c72916STejun Heo 	if (ata_is_host_link(dev->link))
420b1c72916STejun Heo 		alt_devno += 15;
42133267325STejun Heo 
42233267325STejun Heo 	for (i = ata_force_tbl_size - 1; i >= 0; i--) {
42333267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
42433267325STejun Heo 		unsigned long pio_mask, mwdma_mask, udma_mask;
42533267325STejun Heo 
42633267325STejun Heo 		if (fe->port != -1 && fe->port != dev->link->ap->print_id)
42733267325STejun Heo 			continue;
42833267325STejun Heo 
42933267325STejun Heo 		if (fe->device != -1 && fe->device != devno &&
43033267325STejun Heo 		    fe->device != alt_devno)
43133267325STejun Heo 			continue;
43233267325STejun Heo 
43333267325STejun Heo 		if (!fe->param.xfer_mask)
43433267325STejun Heo 			continue;
43533267325STejun Heo 
43633267325STejun Heo 		ata_unpack_xfermask(fe->param.xfer_mask,
43733267325STejun Heo 				    &pio_mask, &mwdma_mask, &udma_mask);
43833267325STejun Heo 		if (udma_mask)
43933267325STejun Heo 			dev->udma_mask = udma_mask;
44033267325STejun Heo 		else if (mwdma_mask) {
44133267325STejun Heo 			dev->udma_mask = 0;
44233267325STejun Heo 			dev->mwdma_mask = mwdma_mask;
44333267325STejun Heo 		} else {
44433267325STejun Heo 			dev->udma_mask = 0;
44533267325STejun Heo 			dev->mwdma_mask = 0;
44633267325STejun Heo 			dev->pio_mask = pio_mask;
44733267325STejun Heo 		}
44833267325STejun Heo 
44933267325STejun Heo 		ata_dev_printk(dev, KERN_NOTICE,
45033267325STejun Heo 			"FORCE: xfer_mask set to %s\n", fe->param.name);
45133267325STejun Heo 		return;
45233267325STejun Heo 	}
45333267325STejun Heo }
45433267325STejun Heo 
45533267325STejun Heo /**
45633267325STejun Heo  *	ata_force_horkage - force horkage according to libata.force
45733267325STejun Heo  *	@dev: ATA device of interest
45833267325STejun Heo  *
45933267325STejun Heo  *	Force horkage according to libata.force and whine about it.
46033267325STejun Heo  *	For consistency with link selection, device number 15 selects
46133267325STejun Heo  *	the first device connected to the host link.
46233267325STejun Heo  *
46333267325STejun Heo  *	LOCKING:
46433267325STejun Heo  *	EH context.
46533267325STejun Heo  */
46633267325STejun Heo static void ata_force_horkage(struct ata_device *dev)
46733267325STejun Heo {
46833267325STejun Heo 	int devno = dev->link->pmp + dev->devno;
46933267325STejun Heo 	int alt_devno = devno;
47033267325STejun Heo 	int i;
47133267325STejun Heo 
472b1c72916STejun Heo 	/* allow n.15/16 for devices attached to host port */
473b1c72916STejun Heo 	if (ata_is_host_link(dev->link))
474b1c72916STejun Heo 		alt_devno += 15;
47533267325STejun Heo 
47633267325STejun Heo 	for (i = 0; i < ata_force_tbl_size; i++) {
47733267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
47833267325STejun Heo 
47933267325STejun Heo 		if (fe->port != -1 && fe->port != dev->link->ap->print_id)
48033267325STejun Heo 			continue;
48133267325STejun Heo 
48233267325STejun Heo 		if (fe->device != -1 && fe->device != devno &&
48333267325STejun Heo 		    fe->device != alt_devno)
48433267325STejun Heo 			continue;
48533267325STejun Heo 
48633267325STejun Heo 		if (!(~dev->horkage & fe->param.horkage_on) &&
48733267325STejun Heo 		    !(dev->horkage & fe->param.horkage_off))
48833267325STejun Heo 			continue;
48933267325STejun Heo 
49033267325STejun Heo 		dev->horkage |= fe->param.horkage_on;
49133267325STejun Heo 		dev->horkage &= ~fe->param.horkage_off;
49233267325STejun Heo 
49333267325STejun Heo 		ata_dev_printk(dev, KERN_NOTICE,
49433267325STejun Heo 			"FORCE: horkage modified (%s)\n", fe->param.name);
49533267325STejun Heo 	}
49633267325STejun Heo }
49733267325STejun Heo 
49833267325STejun Heo /**
499436d34b3STejun Heo  *	atapi_cmd_type - Determine ATAPI command type from SCSI opcode
500436d34b3STejun Heo  *	@opcode: SCSI opcode
501436d34b3STejun Heo  *
502436d34b3STejun Heo  *	Determine ATAPI command type from @opcode.
503436d34b3STejun Heo  *
504436d34b3STejun Heo  *	LOCKING:
505436d34b3STejun Heo  *	None.
506436d34b3STejun Heo  *
507436d34b3STejun Heo  *	RETURNS:
508436d34b3STejun Heo  *	ATAPI_{READ|WRITE|READ_CD|PASS_THRU|MISC}
509436d34b3STejun Heo  */
510436d34b3STejun Heo int atapi_cmd_type(u8 opcode)
511436d34b3STejun Heo {
512436d34b3STejun Heo 	switch (opcode) {
513436d34b3STejun Heo 	case GPCMD_READ_10:
514436d34b3STejun Heo 	case GPCMD_READ_12:
515436d34b3STejun Heo 		return ATAPI_READ;
516436d34b3STejun Heo 
517436d34b3STejun Heo 	case GPCMD_WRITE_10:
518436d34b3STejun Heo 	case GPCMD_WRITE_12:
519436d34b3STejun Heo 	case GPCMD_WRITE_AND_VERIFY_10:
520436d34b3STejun Heo 		return ATAPI_WRITE;
521436d34b3STejun Heo 
522436d34b3STejun Heo 	case GPCMD_READ_CD:
523436d34b3STejun Heo 	case GPCMD_READ_CD_MSF:
524436d34b3STejun Heo 		return ATAPI_READ_CD;
525436d34b3STejun Heo 
526e52dcc48STejun Heo 	case ATA_16:
527e52dcc48STejun Heo 	case ATA_12:
528e52dcc48STejun Heo 		if (atapi_passthru16)
529e52dcc48STejun Heo 			return ATAPI_PASS_THRU;
530e52dcc48STejun Heo 		/* fall thru */
531436d34b3STejun Heo 	default:
532436d34b3STejun Heo 		return ATAPI_MISC;
533436d34b3STejun Heo 	}
534436d34b3STejun Heo }
535436d34b3STejun Heo 
536436d34b3STejun Heo /**
537c6fd2807SJeff Garzik  *	ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
538c6fd2807SJeff Garzik  *	@tf: Taskfile to convert
539c6fd2807SJeff Garzik  *	@pmp: Port multiplier port
5409977126cSTejun Heo  *	@is_cmd: This FIS is for command
5419977126cSTejun Heo  *	@fis: Buffer into which data will output
542c6fd2807SJeff Garzik  *
543c6fd2807SJeff Garzik  *	Converts a standard ATA taskfile to a Serial ATA
544c6fd2807SJeff Garzik  *	FIS structure (Register - Host to Device).
545c6fd2807SJeff Garzik  *
546c6fd2807SJeff Garzik  *	LOCKING:
547c6fd2807SJeff Garzik  *	Inherited from caller.
548c6fd2807SJeff Garzik  */
5499977126cSTejun Heo void ata_tf_to_fis(const struct ata_taskfile *tf, u8 pmp, int is_cmd, u8 *fis)
550c6fd2807SJeff Garzik {
551c6fd2807SJeff Garzik 	fis[0] = 0x27;			/* Register - Host to Device FIS */
5529977126cSTejun Heo 	fis[1] = pmp & 0xf;		/* Port multiplier number*/
5539977126cSTejun Heo 	if (is_cmd)
5549977126cSTejun Heo 		fis[1] |= (1 << 7);	/* bit 7 indicates Command FIS */
5559977126cSTejun Heo 
556c6fd2807SJeff Garzik 	fis[2] = tf->command;
557c6fd2807SJeff Garzik 	fis[3] = tf->feature;
558c6fd2807SJeff Garzik 
559c6fd2807SJeff Garzik 	fis[4] = tf->lbal;
560c6fd2807SJeff Garzik 	fis[5] = tf->lbam;
561c6fd2807SJeff Garzik 	fis[6] = tf->lbah;
562c6fd2807SJeff Garzik 	fis[7] = tf->device;
563c6fd2807SJeff Garzik 
564c6fd2807SJeff Garzik 	fis[8] = tf->hob_lbal;
565c6fd2807SJeff Garzik 	fis[9] = tf->hob_lbam;
566c6fd2807SJeff Garzik 	fis[10] = tf->hob_lbah;
567c6fd2807SJeff Garzik 	fis[11] = tf->hob_feature;
568c6fd2807SJeff Garzik 
569c6fd2807SJeff Garzik 	fis[12] = tf->nsect;
570c6fd2807SJeff Garzik 	fis[13] = tf->hob_nsect;
571c6fd2807SJeff Garzik 	fis[14] = 0;
572c6fd2807SJeff Garzik 	fis[15] = tf->ctl;
573c6fd2807SJeff Garzik 
574c6fd2807SJeff Garzik 	fis[16] = 0;
575c6fd2807SJeff Garzik 	fis[17] = 0;
576c6fd2807SJeff Garzik 	fis[18] = 0;
577c6fd2807SJeff Garzik 	fis[19] = 0;
578c6fd2807SJeff Garzik }
579c6fd2807SJeff Garzik 
580c6fd2807SJeff Garzik /**
581c6fd2807SJeff Garzik  *	ata_tf_from_fis - Convert SATA FIS to ATA taskfile
582c6fd2807SJeff Garzik  *	@fis: Buffer from which data will be input
583c6fd2807SJeff Garzik  *	@tf: Taskfile to output
584c6fd2807SJeff Garzik  *
585c6fd2807SJeff Garzik  *	Converts a serial ATA FIS structure to a standard ATA taskfile.
586c6fd2807SJeff Garzik  *
587c6fd2807SJeff Garzik  *	LOCKING:
588c6fd2807SJeff Garzik  *	Inherited from caller.
589c6fd2807SJeff Garzik  */
590c6fd2807SJeff Garzik 
591c6fd2807SJeff Garzik void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
592c6fd2807SJeff Garzik {
593c6fd2807SJeff Garzik 	tf->command	= fis[2];	/* status */
594c6fd2807SJeff Garzik 	tf->feature	= fis[3];	/* error */
595c6fd2807SJeff Garzik 
596c6fd2807SJeff Garzik 	tf->lbal	= fis[4];
597c6fd2807SJeff Garzik 	tf->lbam	= fis[5];
598c6fd2807SJeff Garzik 	tf->lbah	= fis[6];
599c6fd2807SJeff Garzik 	tf->device	= fis[7];
600c6fd2807SJeff Garzik 
601c6fd2807SJeff Garzik 	tf->hob_lbal	= fis[8];
602c6fd2807SJeff Garzik 	tf->hob_lbam	= fis[9];
603c6fd2807SJeff Garzik 	tf->hob_lbah	= fis[10];
604c6fd2807SJeff Garzik 
605c6fd2807SJeff Garzik 	tf->nsect	= fis[12];
606c6fd2807SJeff Garzik 	tf->hob_nsect	= fis[13];
607c6fd2807SJeff Garzik }
608c6fd2807SJeff Garzik 
609c6fd2807SJeff Garzik static const u8 ata_rw_cmds[] = {
610c6fd2807SJeff Garzik 	/* pio multi */
611c6fd2807SJeff Garzik 	ATA_CMD_READ_MULTI,
612c6fd2807SJeff Garzik 	ATA_CMD_WRITE_MULTI,
613c6fd2807SJeff Garzik 	ATA_CMD_READ_MULTI_EXT,
614c6fd2807SJeff Garzik 	ATA_CMD_WRITE_MULTI_EXT,
615c6fd2807SJeff Garzik 	0,
616c6fd2807SJeff Garzik 	0,
617c6fd2807SJeff Garzik 	0,
618c6fd2807SJeff Garzik 	ATA_CMD_WRITE_MULTI_FUA_EXT,
619c6fd2807SJeff Garzik 	/* pio */
620c6fd2807SJeff Garzik 	ATA_CMD_PIO_READ,
621c6fd2807SJeff Garzik 	ATA_CMD_PIO_WRITE,
622c6fd2807SJeff Garzik 	ATA_CMD_PIO_READ_EXT,
623c6fd2807SJeff Garzik 	ATA_CMD_PIO_WRITE_EXT,
624c6fd2807SJeff Garzik 	0,
625c6fd2807SJeff Garzik 	0,
626c6fd2807SJeff Garzik 	0,
627c6fd2807SJeff Garzik 	0,
628c6fd2807SJeff Garzik 	/* dma */
629c6fd2807SJeff Garzik 	ATA_CMD_READ,
630c6fd2807SJeff Garzik 	ATA_CMD_WRITE,
631c6fd2807SJeff Garzik 	ATA_CMD_READ_EXT,
632c6fd2807SJeff Garzik 	ATA_CMD_WRITE_EXT,
633c6fd2807SJeff Garzik 	0,
634c6fd2807SJeff Garzik 	0,
635c6fd2807SJeff Garzik 	0,
636c6fd2807SJeff Garzik 	ATA_CMD_WRITE_FUA_EXT
637c6fd2807SJeff Garzik };
638c6fd2807SJeff Garzik 
639c6fd2807SJeff Garzik /**
640c6fd2807SJeff Garzik  *	ata_rwcmd_protocol - set taskfile r/w commands and protocol
641bd056d7eSTejun Heo  *	@tf: command to examine and configure
642bd056d7eSTejun Heo  *	@dev: device tf belongs to
643c6fd2807SJeff Garzik  *
644c6fd2807SJeff Garzik  *	Examine the device configuration and tf->flags to calculate
645c6fd2807SJeff Garzik  *	the proper read/write commands and protocol to use.
646c6fd2807SJeff Garzik  *
647c6fd2807SJeff Garzik  *	LOCKING:
648c6fd2807SJeff Garzik  *	caller.
649c6fd2807SJeff Garzik  */
650bd056d7eSTejun Heo static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev)
651c6fd2807SJeff Garzik {
652c6fd2807SJeff Garzik 	u8 cmd;
653c6fd2807SJeff Garzik 
654c6fd2807SJeff Garzik 	int index, fua, lba48, write;
655c6fd2807SJeff Garzik 
656c6fd2807SJeff Garzik 	fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
657c6fd2807SJeff Garzik 	lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
658c6fd2807SJeff Garzik 	write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
659c6fd2807SJeff Garzik 
660c6fd2807SJeff Garzik 	if (dev->flags & ATA_DFLAG_PIO) {
661c6fd2807SJeff Garzik 		tf->protocol = ATA_PROT_PIO;
662c6fd2807SJeff Garzik 		index = dev->multi_count ? 0 : 8;
6639af5c9c9STejun Heo 	} else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) {
664c6fd2807SJeff Garzik 		/* Unable to use DMA due to host limitation */
665c6fd2807SJeff Garzik 		tf->protocol = ATA_PROT_PIO;
666c6fd2807SJeff Garzik 		index = dev->multi_count ? 0 : 8;
667c6fd2807SJeff Garzik 	} else {
668c6fd2807SJeff Garzik 		tf->protocol = ATA_PROT_DMA;
669c6fd2807SJeff Garzik 		index = 16;
670c6fd2807SJeff Garzik 	}
671c6fd2807SJeff Garzik 
672c6fd2807SJeff Garzik 	cmd = ata_rw_cmds[index + fua + lba48 + write];
673c6fd2807SJeff Garzik 	if (cmd) {
674c6fd2807SJeff Garzik 		tf->command = cmd;
675c6fd2807SJeff Garzik 		return 0;
676c6fd2807SJeff Garzik 	}
677c6fd2807SJeff Garzik 	return -1;
678c6fd2807SJeff Garzik }
679c6fd2807SJeff Garzik 
680c6fd2807SJeff Garzik /**
68135b649feSTejun Heo  *	ata_tf_read_block - Read block address from ATA taskfile
68235b649feSTejun Heo  *	@tf: ATA taskfile of interest
68335b649feSTejun Heo  *	@dev: ATA device @tf belongs to
68435b649feSTejun Heo  *
68535b649feSTejun Heo  *	LOCKING:
68635b649feSTejun Heo  *	None.
68735b649feSTejun Heo  *
68835b649feSTejun Heo  *	Read block address from @tf.  This function can handle all
68935b649feSTejun Heo  *	three address formats - LBA, LBA48 and CHS.  tf->protocol and
69035b649feSTejun Heo  *	flags select the address format to use.
69135b649feSTejun Heo  *
69235b649feSTejun Heo  *	RETURNS:
69335b649feSTejun Heo  *	Block address read from @tf.
69435b649feSTejun Heo  */
69535b649feSTejun Heo u64 ata_tf_read_block(struct ata_taskfile *tf, struct ata_device *dev)
69635b649feSTejun Heo {
69735b649feSTejun Heo 	u64 block = 0;
69835b649feSTejun Heo 
69935b649feSTejun Heo 	if (tf->flags & ATA_TFLAG_LBA) {
70035b649feSTejun Heo 		if (tf->flags & ATA_TFLAG_LBA48) {
70135b649feSTejun Heo 			block |= (u64)tf->hob_lbah << 40;
70235b649feSTejun Heo 			block |= (u64)tf->hob_lbam << 32;
70344901a96SRoland Dreier 			block |= (u64)tf->hob_lbal << 24;
70435b649feSTejun Heo 		} else
70535b649feSTejun Heo 			block |= (tf->device & 0xf) << 24;
70635b649feSTejun Heo 
70735b649feSTejun Heo 		block |= tf->lbah << 16;
70835b649feSTejun Heo 		block |= tf->lbam << 8;
70935b649feSTejun Heo 		block |= tf->lbal;
71035b649feSTejun Heo 	} else {
71135b649feSTejun Heo 		u32 cyl, head, sect;
71235b649feSTejun Heo 
71335b649feSTejun Heo 		cyl = tf->lbam | (tf->lbah << 8);
71435b649feSTejun Heo 		head = tf->device & 0xf;
71535b649feSTejun Heo 		sect = tf->lbal;
71635b649feSTejun Heo 
717ac8672eaSTejun Heo 		if (!sect) {
718ac8672eaSTejun Heo 			ata_dev_printk(dev, KERN_WARNING, "device reported "
719ac8672eaSTejun Heo 				       "invalid CHS sector 0\n");
720ac8672eaSTejun Heo 			sect = 1; /* oh well */
721ac8672eaSTejun Heo 		}
722ac8672eaSTejun Heo 
723ac8672eaSTejun Heo 		block = (cyl * dev->heads + head) * dev->sectors + sect - 1;
72435b649feSTejun Heo 	}
72535b649feSTejun Heo 
72635b649feSTejun Heo 	return block;
72735b649feSTejun Heo }
72835b649feSTejun Heo 
72935b649feSTejun Heo /**
730bd056d7eSTejun Heo  *	ata_build_rw_tf - Build ATA taskfile for given read/write request
731bd056d7eSTejun Heo  *	@tf: Target ATA taskfile
732bd056d7eSTejun Heo  *	@dev: ATA device @tf belongs to
733bd056d7eSTejun Heo  *	@block: Block address
734bd056d7eSTejun Heo  *	@n_block: Number of blocks
735bd056d7eSTejun Heo  *	@tf_flags: RW/FUA etc...
736bd056d7eSTejun Heo  *	@tag: tag
737bd056d7eSTejun Heo  *
738bd056d7eSTejun Heo  *	LOCKING:
739bd056d7eSTejun Heo  *	None.
740bd056d7eSTejun Heo  *
741bd056d7eSTejun Heo  *	Build ATA taskfile @tf for read/write request described by
742bd056d7eSTejun Heo  *	@block, @n_block, @tf_flags and @tag on @dev.
743bd056d7eSTejun Heo  *
744bd056d7eSTejun Heo  *	RETURNS:
745bd056d7eSTejun Heo  *
746bd056d7eSTejun Heo  *	0 on success, -ERANGE if the request is too large for @dev,
747bd056d7eSTejun Heo  *	-EINVAL if the request is invalid.
748bd056d7eSTejun Heo  */
749bd056d7eSTejun Heo int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev,
750bd056d7eSTejun Heo 		    u64 block, u32 n_block, unsigned int tf_flags,
751bd056d7eSTejun Heo 		    unsigned int tag)
752bd056d7eSTejun Heo {
753bd056d7eSTejun Heo 	tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
754bd056d7eSTejun Heo 	tf->flags |= tf_flags;
755bd056d7eSTejun Heo 
7566d1245bfSTejun Heo 	if (ata_ncq_enabled(dev) && likely(tag != ATA_TAG_INTERNAL)) {
757bd056d7eSTejun Heo 		/* yay, NCQ */
758bd056d7eSTejun Heo 		if (!lba_48_ok(block, n_block))
759bd056d7eSTejun Heo 			return -ERANGE;
760bd056d7eSTejun Heo 
761bd056d7eSTejun Heo 		tf->protocol = ATA_PROT_NCQ;
762bd056d7eSTejun Heo 		tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
763bd056d7eSTejun Heo 
764bd056d7eSTejun Heo 		if (tf->flags & ATA_TFLAG_WRITE)
765bd056d7eSTejun Heo 			tf->command = ATA_CMD_FPDMA_WRITE;
766bd056d7eSTejun Heo 		else
767bd056d7eSTejun Heo 			tf->command = ATA_CMD_FPDMA_READ;
768bd056d7eSTejun Heo 
769bd056d7eSTejun Heo 		tf->nsect = tag << 3;
770bd056d7eSTejun Heo 		tf->hob_feature = (n_block >> 8) & 0xff;
771bd056d7eSTejun Heo 		tf->feature = n_block & 0xff;
772bd056d7eSTejun Heo 
773bd056d7eSTejun Heo 		tf->hob_lbah = (block >> 40) & 0xff;
774bd056d7eSTejun Heo 		tf->hob_lbam = (block >> 32) & 0xff;
775bd056d7eSTejun Heo 		tf->hob_lbal = (block >> 24) & 0xff;
776bd056d7eSTejun Heo 		tf->lbah = (block >> 16) & 0xff;
777bd056d7eSTejun Heo 		tf->lbam = (block >> 8) & 0xff;
778bd056d7eSTejun Heo 		tf->lbal = block & 0xff;
779bd056d7eSTejun Heo 
780bd056d7eSTejun Heo 		tf->device = 1 << 6;
781bd056d7eSTejun Heo 		if (tf->flags & ATA_TFLAG_FUA)
782bd056d7eSTejun Heo 			tf->device |= 1 << 7;
783bd056d7eSTejun Heo 	} else if (dev->flags & ATA_DFLAG_LBA) {
784bd056d7eSTejun Heo 		tf->flags |= ATA_TFLAG_LBA;
785bd056d7eSTejun Heo 
786bd056d7eSTejun Heo 		if (lba_28_ok(block, n_block)) {
787bd056d7eSTejun Heo 			/* use LBA28 */
788bd056d7eSTejun Heo 			tf->device |= (block >> 24) & 0xf;
789bd056d7eSTejun Heo 		} else if (lba_48_ok(block, n_block)) {
790bd056d7eSTejun Heo 			if (!(dev->flags & ATA_DFLAG_LBA48))
791bd056d7eSTejun Heo 				return -ERANGE;
792bd056d7eSTejun Heo 
793bd056d7eSTejun Heo 			/* use LBA48 */
794bd056d7eSTejun Heo 			tf->flags |= ATA_TFLAG_LBA48;
795bd056d7eSTejun Heo 
796bd056d7eSTejun Heo 			tf->hob_nsect = (n_block >> 8) & 0xff;
797bd056d7eSTejun Heo 
798bd056d7eSTejun Heo 			tf->hob_lbah = (block >> 40) & 0xff;
799bd056d7eSTejun Heo 			tf->hob_lbam = (block >> 32) & 0xff;
800bd056d7eSTejun Heo 			tf->hob_lbal = (block >> 24) & 0xff;
801bd056d7eSTejun Heo 		} else
802bd056d7eSTejun Heo 			/* request too large even for LBA48 */
803bd056d7eSTejun Heo 			return -ERANGE;
804bd056d7eSTejun Heo 
805bd056d7eSTejun Heo 		if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
806bd056d7eSTejun Heo 			return -EINVAL;
807bd056d7eSTejun Heo 
808bd056d7eSTejun Heo 		tf->nsect = n_block & 0xff;
809bd056d7eSTejun Heo 
810bd056d7eSTejun Heo 		tf->lbah = (block >> 16) & 0xff;
811bd056d7eSTejun Heo 		tf->lbam = (block >> 8) & 0xff;
812bd056d7eSTejun Heo 		tf->lbal = block & 0xff;
813bd056d7eSTejun Heo 
814bd056d7eSTejun Heo 		tf->device |= ATA_LBA;
815bd056d7eSTejun Heo 	} else {
816bd056d7eSTejun Heo 		/* CHS */
817bd056d7eSTejun Heo 		u32 sect, head, cyl, track;
818bd056d7eSTejun Heo 
819bd056d7eSTejun Heo 		/* The request -may- be too large for CHS addressing. */
820bd056d7eSTejun Heo 		if (!lba_28_ok(block, n_block))
821bd056d7eSTejun Heo 			return -ERANGE;
822bd056d7eSTejun Heo 
823bd056d7eSTejun Heo 		if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
824bd056d7eSTejun Heo 			return -EINVAL;
825bd056d7eSTejun Heo 
826bd056d7eSTejun Heo 		/* Convert LBA to CHS */
827bd056d7eSTejun Heo 		track = (u32)block / dev->sectors;
828bd056d7eSTejun Heo 		cyl   = track / dev->heads;
829bd056d7eSTejun Heo 		head  = track % dev->heads;
830bd056d7eSTejun Heo 		sect  = (u32)block % dev->sectors + 1;
831bd056d7eSTejun Heo 
832bd056d7eSTejun Heo 		DPRINTK("block %u track %u cyl %u head %u sect %u\n",
833bd056d7eSTejun Heo 			(u32)block, track, cyl, head, sect);
834bd056d7eSTejun Heo 
835bd056d7eSTejun Heo 		/* Check whether the converted CHS can fit.
836bd056d7eSTejun Heo 		   Cylinder: 0-65535
837bd056d7eSTejun Heo 		   Head: 0-15
838bd056d7eSTejun Heo 		   Sector: 1-255*/
839bd056d7eSTejun Heo 		if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
840bd056d7eSTejun Heo 			return -ERANGE;
841bd056d7eSTejun Heo 
842bd056d7eSTejun Heo 		tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
843bd056d7eSTejun Heo 		tf->lbal = sect;
844bd056d7eSTejun Heo 		tf->lbam = cyl;
845bd056d7eSTejun Heo 		tf->lbah = cyl >> 8;
846bd056d7eSTejun Heo 		tf->device |= head;
847bd056d7eSTejun Heo 	}
848bd056d7eSTejun Heo 
849bd056d7eSTejun Heo 	return 0;
850bd056d7eSTejun Heo }
851bd056d7eSTejun Heo 
852bd056d7eSTejun Heo /**
853c6fd2807SJeff Garzik  *	ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
854c6fd2807SJeff Garzik  *	@pio_mask: pio_mask
855c6fd2807SJeff Garzik  *	@mwdma_mask: mwdma_mask
856c6fd2807SJeff Garzik  *	@udma_mask: udma_mask
857c6fd2807SJeff Garzik  *
858c6fd2807SJeff Garzik  *	Pack @pio_mask, @mwdma_mask and @udma_mask into a single
859c6fd2807SJeff Garzik  *	unsigned int xfer_mask.
860c6fd2807SJeff Garzik  *
861c6fd2807SJeff Garzik  *	LOCKING:
862c6fd2807SJeff Garzik  *	None.
863c6fd2807SJeff Garzik  *
864c6fd2807SJeff Garzik  *	RETURNS:
865c6fd2807SJeff Garzik  *	Packed xfer_mask.
866c6fd2807SJeff Garzik  */
8677dc951aeSTejun Heo unsigned long ata_pack_xfermask(unsigned long pio_mask,
8687dc951aeSTejun Heo 				unsigned long mwdma_mask,
8697dc951aeSTejun Heo 				unsigned long udma_mask)
870c6fd2807SJeff Garzik {
871c6fd2807SJeff Garzik 	return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
872c6fd2807SJeff Garzik 		((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
873c6fd2807SJeff Garzik 		((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
874c6fd2807SJeff Garzik }
875c6fd2807SJeff Garzik 
876c6fd2807SJeff Garzik /**
877c6fd2807SJeff Garzik  *	ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
878c6fd2807SJeff Garzik  *	@xfer_mask: xfer_mask to unpack
879c6fd2807SJeff Garzik  *	@pio_mask: resulting pio_mask
880c6fd2807SJeff Garzik  *	@mwdma_mask: resulting mwdma_mask
881c6fd2807SJeff Garzik  *	@udma_mask: resulting udma_mask
882c6fd2807SJeff Garzik  *
883c6fd2807SJeff Garzik  *	Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
884c6fd2807SJeff Garzik  *	Any NULL distination masks will be ignored.
885c6fd2807SJeff Garzik  */
8867dc951aeSTejun Heo void ata_unpack_xfermask(unsigned long xfer_mask, unsigned long *pio_mask,
8877dc951aeSTejun Heo 			 unsigned long *mwdma_mask, unsigned long *udma_mask)
888c6fd2807SJeff Garzik {
889c6fd2807SJeff Garzik 	if (pio_mask)
890c6fd2807SJeff Garzik 		*pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
891c6fd2807SJeff Garzik 	if (mwdma_mask)
892c6fd2807SJeff Garzik 		*mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
893c6fd2807SJeff Garzik 	if (udma_mask)
894c6fd2807SJeff Garzik 		*udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
895c6fd2807SJeff Garzik }
896c6fd2807SJeff Garzik 
897c6fd2807SJeff Garzik static const struct ata_xfer_ent {
898c6fd2807SJeff Garzik 	int shift, bits;
899c6fd2807SJeff Garzik 	u8 base;
900c6fd2807SJeff Garzik } ata_xfer_tbl[] = {
90170cd071eSTejun Heo 	{ ATA_SHIFT_PIO, ATA_NR_PIO_MODES, XFER_PIO_0 },
90270cd071eSTejun Heo 	{ ATA_SHIFT_MWDMA, ATA_NR_MWDMA_MODES, XFER_MW_DMA_0 },
90370cd071eSTejun Heo 	{ ATA_SHIFT_UDMA, ATA_NR_UDMA_MODES, XFER_UDMA_0 },
904c6fd2807SJeff Garzik 	{ -1, },
905c6fd2807SJeff Garzik };
906c6fd2807SJeff Garzik 
907c6fd2807SJeff Garzik /**
908c6fd2807SJeff Garzik  *	ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
909c6fd2807SJeff Garzik  *	@xfer_mask: xfer_mask of interest
910c6fd2807SJeff Garzik  *
911c6fd2807SJeff Garzik  *	Return matching XFER_* value for @xfer_mask.  Only the highest
912c6fd2807SJeff Garzik  *	bit of @xfer_mask is considered.
913c6fd2807SJeff Garzik  *
914c6fd2807SJeff Garzik  *	LOCKING:
915c6fd2807SJeff Garzik  *	None.
916c6fd2807SJeff Garzik  *
917c6fd2807SJeff Garzik  *	RETURNS:
91870cd071eSTejun Heo  *	Matching XFER_* value, 0xff if no match found.
919c6fd2807SJeff Garzik  */
9207dc951aeSTejun Heo u8 ata_xfer_mask2mode(unsigned long xfer_mask)
921c6fd2807SJeff Garzik {
922c6fd2807SJeff Garzik 	int highbit = fls(xfer_mask) - 1;
923c6fd2807SJeff Garzik 	const struct ata_xfer_ent *ent;
924c6fd2807SJeff Garzik 
925c6fd2807SJeff Garzik 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
926c6fd2807SJeff Garzik 		if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
927c6fd2807SJeff Garzik 			return ent->base + highbit - ent->shift;
92870cd071eSTejun Heo 	return 0xff;
929c6fd2807SJeff Garzik }
930c6fd2807SJeff Garzik 
931c6fd2807SJeff Garzik /**
932c6fd2807SJeff Garzik  *	ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
933c6fd2807SJeff Garzik  *	@xfer_mode: XFER_* of interest
934c6fd2807SJeff Garzik  *
935c6fd2807SJeff Garzik  *	Return matching xfer_mask for @xfer_mode.
936c6fd2807SJeff Garzik  *
937c6fd2807SJeff Garzik  *	LOCKING:
938c6fd2807SJeff Garzik  *	None.
939c6fd2807SJeff Garzik  *
940c6fd2807SJeff Garzik  *	RETURNS:
941c6fd2807SJeff Garzik  *	Matching xfer_mask, 0 if no match found.
942c6fd2807SJeff Garzik  */
9437dc951aeSTejun Heo unsigned long ata_xfer_mode2mask(u8 xfer_mode)
944c6fd2807SJeff Garzik {
945c6fd2807SJeff Garzik 	const struct ata_xfer_ent *ent;
946c6fd2807SJeff Garzik 
947c6fd2807SJeff Garzik 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
948c6fd2807SJeff Garzik 		if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
94970cd071eSTejun Heo 			return ((2 << (ent->shift + xfer_mode - ent->base)) - 1)
95070cd071eSTejun Heo 				& ~((1 << ent->shift) - 1);
951c6fd2807SJeff Garzik 	return 0;
952c6fd2807SJeff Garzik }
953c6fd2807SJeff Garzik 
954c6fd2807SJeff Garzik /**
955c6fd2807SJeff Garzik  *	ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
956c6fd2807SJeff Garzik  *	@xfer_mode: XFER_* of interest
957c6fd2807SJeff Garzik  *
958c6fd2807SJeff Garzik  *	Return matching xfer_shift for @xfer_mode.
959c6fd2807SJeff Garzik  *
960c6fd2807SJeff Garzik  *	LOCKING:
961c6fd2807SJeff Garzik  *	None.
962c6fd2807SJeff Garzik  *
963c6fd2807SJeff Garzik  *	RETURNS:
964c6fd2807SJeff Garzik  *	Matching xfer_shift, -1 if no match found.
965c6fd2807SJeff Garzik  */
9667dc951aeSTejun Heo int ata_xfer_mode2shift(unsigned long xfer_mode)
967c6fd2807SJeff Garzik {
968c6fd2807SJeff Garzik 	const struct ata_xfer_ent *ent;
969c6fd2807SJeff Garzik 
970c6fd2807SJeff Garzik 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
971c6fd2807SJeff Garzik 		if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
972c6fd2807SJeff Garzik 			return ent->shift;
973c6fd2807SJeff Garzik 	return -1;
974c6fd2807SJeff Garzik }
975c6fd2807SJeff Garzik 
976c6fd2807SJeff Garzik /**
977c6fd2807SJeff Garzik  *	ata_mode_string - convert xfer_mask to string
978c6fd2807SJeff Garzik  *	@xfer_mask: mask of bits supported; only highest bit counts.
979c6fd2807SJeff Garzik  *
980c6fd2807SJeff Garzik  *	Determine string which represents the highest speed
981c6fd2807SJeff Garzik  *	(highest bit in @modemask).
982c6fd2807SJeff Garzik  *
983c6fd2807SJeff Garzik  *	LOCKING:
984c6fd2807SJeff Garzik  *	None.
985c6fd2807SJeff Garzik  *
986c6fd2807SJeff Garzik  *	RETURNS:
987c6fd2807SJeff Garzik  *	Constant C string representing highest speed listed in
988c6fd2807SJeff Garzik  *	@mode_mask, or the constant C string "<n/a>".
989c6fd2807SJeff Garzik  */
9907dc951aeSTejun Heo const char *ata_mode_string(unsigned long xfer_mask)
991c6fd2807SJeff Garzik {
992c6fd2807SJeff Garzik 	static const char * const xfer_mode_str[] = {
993c6fd2807SJeff Garzik 		"PIO0",
994c6fd2807SJeff Garzik 		"PIO1",
995c6fd2807SJeff Garzik 		"PIO2",
996c6fd2807SJeff Garzik 		"PIO3",
997c6fd2807SJeff Garzik 		"PIO4",
998b352e57dSAlan Cox 		"PIO5",
999b352e57dSAlan Cox 		"PIO6",
1000c6fd2807SJeff Garzik 		"MWDMA0",
1001c6fd2807SJeff Garzik 		"MWDMA1",
1002c6fd2807SJeff Garzik 		"MWDMA2",
1003b352e57dSAlan Cox 		"MWDMA3",
1004b352e57dSAlan Cox 		"MWDMA4",
1005c6fd2807SJeff Garzik 		"UDMA/16",
1006c6fd2807SJeff Garzik 		"UDMA/25",
1007c6fd2807SJeff Garzik 		"UDMA/33",
1008c6fd2807SJeff Garzik 		"UDMA/44",
1009c6fd2807SJeff Garzik 		"UDMA/66",
1010c6fd2807SJeff Garzik 		"UDMA/100",
1011c6fd2807SJeff Garzik 		"UDMA/133",
1012c6fd2807SJeff Garzik 		"UDMA7",
1013c6fd2807SJeff Garzik 	};
1014c6fd2807SJeff Garzik 	int highbit;
1015c6fd2807SJeff Garzik 
1016c6fd2807SJeff Garzik 	highbit = fls(xfer_mask) - 1;
1017c6fd2807SJeff Garzik 	if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
1018c6fd2807SJeff Garzik 		return xfer_mode_str[highbit];
1019c6fd2807SJeff Garzik 	return "<n/a>";
1020c6fd2807SJeff Garzik }
1021c6fd2807SJeff Garzik 
1022c6fd2807SJeff Garzik static const char *sata_spd_string(unsigned int spd)
1023c6fd2807SJeff Garzik {
1024c6fd2807SJeff Garzik 	static const char * const spd_str[] = {
1025c6fd2807SJeff Garzik 		"1.5 Gbps",
1026c6fd2807SJeff Garzik 		"3.0 Gbps",
10278522ee25SShane Huang 		"6.0 Gbps",
1028c6fd2807SJeff Garzik 	};
1029c6fd2807SJeff Garzik 
1030c6fd2807SJeff Garzik 	if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
1031c6fd2807SJeff Garzik 		return "<unknown>";
1032c6fd2807SJeff Garzik 	return spd_str[spd - 1];
1033c6fd2807SJeff Garzik }
1034c6fd2807SJeff Garzik 
1035ca77329fSKristen Carlson Accardi static int ata_dev_set_dipm(struct ata_device *dev, enum link_pm policy)
1036ca77329fSKristen Carlson Accardi {
1037ca77329fSKristen Carlson Accardi 	struct ata_link *link = dev->link;
1038ca77329fSKristen Carlson Accardi 	struct ata_port *ap = link->ap;
1039ca77329fSKristen Carlson Accardi 	u32 scontrol;
1040ca77329fSKristen Carlson Accardi 	unsigned int err_mask;
1041ca77329fSKristen Carlson Accardi 	int rc;
1042ca77329fSKristen Carlson Accardi 
1043ca77329fSKristen Carlson Accardi 	/*
1044ca77329fSKristen Carlson Accardi 	 * disallow DIPM for drivers which haven't set
1045ca77329fSKristen Carlson Accardi 	 * ATA_FLAG_IPM.  This is because when DIPM is enabled,
1046ca77329fSKristen Carlson Accardi 	 * phy ready will be set in the interrupt status on
1047ca77329fSKristen Carlson Accardi 	 * state changes, which will cause some drivers to
1048ca77329fSKristen Carlson Accardi 	 * think there are errors - additionally drivers will
1049ca77329fSKristen Carlson Accardi 	 * need to disable hot plug.
1050ca77329fSKristen Carlson Accardi 	 */
1051ca77329fSKristen Carlson Accardi 	if (!(ap->flags & ATA_FLAG_IPM) || !ata_dev_enabled(dev)) {
1052ca77329fSKristen Carlson Accardi 		ap->pm_policy = NOT_AVAILABLE;
1053ca77329fSKristen Carlson Accardi 		return -EINVAL;
1054ca77329fSKristen Carlson Accardi 	}
1055ca77329fSKristen Carlson Accardi 
1056ca77329fSKristen Carlson Accardi 	/*
1057ca77329fSKristen Carlson Accardi 	 * For DIPM, we will only enable it for the
1058ca77329fSKristen Carlson Accardi 	 * min_power setting.
1059ca77329fSKristen Carlson Accardi 	 *
1060ca77329fSKristen Carlson Accardi 	 * Why?  Because Disks are too stupid to know that
1061ca77329fSKristen Carlson Accardi 	 * If the host rejects a request to go to SLUMBER
1062ca77329fSKristen Carlson Accardi 	 * they should retry at PARTIAL, and instead it
1063ca77329fSKristen Carlson Accardi 	 * just would give up.  So, for medium_power to
1064ca77329fSKristen Carlson Accardi 	 * work at all, we need to only allow HIPM.
1065ca77329fSKristen Carlson Accardi 	 */
1066ca77329fSKristen Carlson Accardi 	rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
1067ca77329fSKristen Carlson Accardi 	if (rc)
1068ca77329fSKristen Carlson Accardi 		return rc;
1069ca77329fSKristen Carlson Accardi 
1070ca77329fSKristen Carlson Accardi 	switch (policy) {
1071ca77329fSKristen Carlson Accardi 	case MIN_POWER:
1072ca77329fSKristen Carlson Accardi 		/* no restrictions on IPM transitions */
1073ca77329fSKristen Carlson Accardi 		scontrol &= ~(0x3 << 8);
1074ca77329fSKristen Carlson Accardi 		rc = sata_scr_write(link, SCR_CONTROL, scontrol);
1075ca77329fSKristen Carlson Accardi 		if (rc)
1076ca77329fSKristen Carlson Accardi 			return rc;
1077ca77329fSKristen Carlson Accardi 
1078ca77329fSKristen Carlson Accardi 		/* enable DIPM */
1079ca77329fSKristen Carlson Accardi 		if (dev->flags & ATA_DFLAG_DIPM)
1080ca77329fSKristen Carlson Accardi 			err_mask = ata_dev_set_feature(dev,
1081ca77329fSKristen Carlson Accardi 					SETFEATURES_SATA_ENABLE, SATA_DIPM);
1082ca77329fSKristen Carlson Accardi 		break;
1083ca77329fSKristen Carlson Accardi 	case MEDIUM_POWER:
1084ca77329fSKristen Carlson Accardi 		/* allow IPM to PARTIAL */
1085ca77329fSKristen Carlson Accardi 		scontrol &= ~(0x1 << 8);
1086ca77329fSKristen Carlson Accardi 		scontrol |= (0x2 << 8);
1087ca77329fSKristen Carlson Accardi 		rc = sata_scr_write(link, SCR_CONTROL, scontrol);
1088ca77329fSKristen Carlson Accardi 		if (rc)
1089ca77329fSKristen Carlson Accardi 			return rc;
1090ca77329fSKristen Carlson Accardi 
1091f5456b63SKristen Carlson Accardi 		/*
1092f5456b63SKristen Carlson Accardi 		 * we don't have to disable DIPM since IPM flags
1093f5456b63SKristen Carlson Accardi 		 * disallow transitions to SLUMBER, which effectively
1094f5456b63SKristen Carlson Accardi 		 * disable DIPM if it does not support PARTIAL
1095f5456b63SKristen Carlson Accardi 		 */
1096ca77329fSKristen Carlson Accardi 		break;
1097ca77329fSKristen Carlson Accardi 	case NOT_AVAILABLE:
1098ca77329fSKristen Carlson Accardi 	case MAX_PERFORMANCE:
1099ca77329fSKristen Carlson Accardi 		/* disable all IPM transitions */
1100ca77329fSKristen Carlson Accardi 		scontrol |= (0x3 << 8);
1101ca77329fSKristen Carlson Accardi 		rc = sata_scr_write(link, SCR_CONTROL, scontrol);
1102ca77329fSKristen Carlson Accardi 		if (rc)
1103ca77329fSKristen Carlson Accardi 			return rc;
1104ca77329fSKristen Carlson Accardi 
1105f5456b63SKristen Carlson Accardi 		/*
1106f5456b63SKristen Carlson Accardi 		 * we don't have to disable DIPM since IPM flags
1107f5456b63SKristen Carlson Accardi 		 * disallow all transitions which effectively
1108f5456b63SKristen Carlson Accardi 		 * disable DIPM anyway.
1109f5456b63SKristen Carlson Accardi 		 */
1110ca77329fSKristen Carlson Accardi 		break;
1111ca77329fSKristen Carlson Accardi 	}
1112ca77329fSKristen Carlson Accardi 
1113ca77329fSKristen Carlson Accardi 	/* FIXME: handle SET FEATURES failure */
1114ca77329fSKristen Carlson Accardi 	(void) err_mask;
1115ca77329fSKristen Carlson Accardi 
1116ca77329fSKristen Carlson Accardi 	return 0;
1117ca77329fSKristen Carlson Accardi }
1118ca77329fSKristen Carlson Accardi 
1119ca77329fSKristen Carlson Accardi /**
1120ca77329fSKristen Carlson Accardi  *	ata_dev_enable_pm - enable SATA interface power management
112148166fd9SStephen Hemminger  *	@dev:  device to enable power management
112248166fd9SStephen Hemminger  *	@policy: the link power management policy
1123ca77329fSKristen Carlson Accardi  *
1124ca77329fSKristen Carlson Accardi  *	Enable SATA Interface power management.  This will enable
1125ca77329fSKristen Carlson Accardi  *	Device Interface Power Management (DIPM) for min_power
1126ca77329fSKristen Carlson Accardi  * 	policy, and then call driver specific callbacks for
1127ca77329fSKristen Carlson Accardi  *	enabling Host Initiated Power management.
1128ca77329fSKristen Carlson Accardi  *
1129ca77329fSKristen Carlson Accardi  *	Locking: Caller.
1130ca77329fSKristen Carlson Accardi  *	Returns: -EINVAL if IPM is not supported, 0 otherwise.
1131ca77329fSKristen Carlson Accardi  */
1132ca77329fSKristen Carlson Accardi void ata_dev_enable_pm(struct ata_device *dev, enum link_pm policy)
1133ca77329fSKristen Carlson Accardi {
1134ca77329fSKristen Carlson Accardi 	int rc = 0;
1135ca77329fSKristen Carlson Accardi 	struct ata_port *ap = dev->link->ap;
1136ca77329fSKristen Carlson Accardi 
1137ca77329fSKristen Carlson Accardi 	/* set HIPM first, then DIPM */
1138ca77329fSKristen Carlson Accardi 	if (ap->ops->enable_pm)
1139ca77329fSKristen Carlson Accardi 		rc = ap->ops->enable_pm(ap, policy);
1140ca77329fSKristen Carlson Accardi 	if (rc)
1141ca77329fSKristen Carlson Accardi 		goto enable_pm_out;
1142ca77329fSKristen Carlson Accardi 	rc = ata_dev_set_dipm(dev, policy);
1143ca77329fSKristen Carlson Accardi 
1144ca77329fSKristen Carlson Accardi enable_pm_out:
1145ca77329fSKristen Carlson Accardi 	if (rc)
1146ca77329fSKristen Carlson Accardi 		ap->pm_policy = MAX_PERFORMANCE;
1147ca77329fSKristen Carlson Accardi 	else
1148ca77329fSKristen Carlson Accardi 		ap->pm_policy = policy;
1149ca77329fSKristen Carlson Accardi 	return /* rc */;	/* hopefully we can use 'rc' eventually */
1150ca77329fSKristen Carlson Accardi }
1151ca77329fSKristen Carlson Accardi 
11521992a5edSStephen Rothwell #ifdef CONFIG_PM
1153ca77329fSKristen Carlson Accardi /**
1154ca77329fSKristen Carlson Accardi  *	ata_dev_disable_pm - disable SATA interface power management
115548166fd9SStephen Hemminger  *	@dev: device to disable power management
1156ca77329fSKristen Carlson Accardi  *
1157ca77329fSKristen Carlson Accardi  *	Disable SATA Interface power management.  This will disable
1158ca77329fSKristen Carlson Accardi  *	Device Interface Power Management (DIPM) without changing
1159ca77329fSKristen Carlson Accardi  * 	policy,  call driver specific callbacks for disabling Host
1160ca77329fSKristen Carlson Accardi  * 	Initiated Power management.
1161ca77329fSKristen Carlson Accardi  *
1162ca77329fSKristen Carlson Accardi  *	Locking: Caller.
1163ca77329fSKristen Carlson Accardi  *	Returns: void
1164ca77329fSKristen Carlson Accardi  */
1165ca77329fSKristen Carlson Accardi static void ata_dev_disable_pm(struct ata_device *dev)
1166ca77329fSKristen Carlson Accardi {
1167ca77329fSKristen Carlson Accardi 	struct ata_port *ap = dev->link->ap;
1168ca77329fSKristen Carlson Accardi 
1169ca77329fSKristen Carlson Accardi 	ata_dev_set_dipm(dev, MAX_PERFORMANCE);
1170ca77329fSKristen Carlson Accardi 	if (ap->ops->disable_pm)
1171ca77329fSKristen Carlson Accardi 		ap->ops->disable_pm(ap);
1172ca77329fSKristen Carlson Accardi }
11731992a5edSStephen Rothwell #endif	/* CONFIG_PM */
1174ca77329fSKristen Carlson Accardi 
1175ca77329fSKristen Carlson Accardi void ata_lpm_schedule(struct ata_port *ap, enum link_pm policy)
1176ca77329fSKristen Carlson Accardi {
1177ca77329fSKristen Carlson Accardi 	ap->pm_policy = policy;
11783ec25ebdSTejun Heo 	ap->link.eh_info.action |= ATA_EH_LPM;
1179ca77329fSKristen Carlson Accardi 	ap->link.eh_info.flags |= ATA_EHI_NO_AUTOPSY;
1180ca77329fSKristen Carlson Accardi 	ata_port_schedule_eh(ap);
1181ca77329fSKristen Carlson Accardi }
1182ca77329fSKristen Carlson Accardi 
11831992a5edSStephen Rothwell #ifdef CONFIG_PM
1184ca77329fSKristen Carlson Accardi static void ata_lpm_enable(struct ata_host *host)
1185ca77329fSKristen Carlson Accardi {
1186ca77329fSKristen Carlson Accardi 	struct ata_link *link;
1187ca77329fSKristen Carlson Accardi 	struct ata_port *ap;
1188ca77329fSKristen Carlson Accardi 	struct ata_device *dev;
1189ca77329fSKristen Carlson Accardi 	int i;
1190ca77329fSKristen Carlson Accardi 
1191ca77329fSKristen Carlson Accardi 	for (i = 0; i < host->n_ports; i++) {
1192ca77329fSKristen Carlson Accardi 		ap = host->ports[i];
11931eca4365STejun Heo 		ata_for_each_link(link, ap, EDGE) {
11941eca4365STejun Heo 			ata_for_each_dev(dev, link, ALL)
1195ca77329fSKristen Carlson Accardi 				ata_dev_disable_pm(dev);
1196ca77329fSKristen Carlson Accardi 		}
1197ca77329fSKristen Carlson Accardi 	}
1198ca77329fSKristen Carlson Accardi }
1199ca77329fSKristen Carlson Accardi 
1200ca77329fSKristen Carlson Accardi static void ata_lpm_disable(struct ata_host *host)
1201ca77329fSKristen Carlson Accardi {
1202ca77329fSKristen Carlson Accardi 	int i;
1203ca77329fSKristen Carlson Accardi 
1204ca77329fSKristen Carlson Accardi 	for (i = 0; i < host->n_ports; i++) {
1205ca77329fSKristen Carlson Accardi 		struct ata_port *ap = host->ports[i];
1206ca77329fSKristen Carlson Accardi 		ata_lpm_schedule(ap, ap->pm_policy);
1207ca77329fSKristen Carlson Accardi 	}
1208ca77329fSKristen Carlson Accardi }
12091992a5edSStephen Rothwell #endif	/* CONFIG_PM */
1210ca77329fSKristen Carlson Accardi 
1211c6fd2807SJeff Garzik /**
1212c6fd2807SJeff Garzik  *	ata_dev_classify - determine device type based on ATA-spec signature
1213c6fd2807SJeff Garzik  *	@tf: ATA taskfile register set for device to be identified
1214c6fd2807SJeff Garzik  *
1215c6fd2807SJeff Garzik  *	Determine from taskfile register contents whether a device is
1216c6fd2807SJeff Garzik  *	ATA or ATAPI, as per "Signature and persistence" section
1217c6fd2807SJeff Garzik  *	of ATA/PI spec (volume 1, sect 5.14).
1218c6fd2807SJeff Garzik  *
1219c6fd2807SJeff Garzik  *	LOCKING:
1220c6fd2807SJeff Garzik  *	None.
1221c6fd2807SJeff Garzik  *
1222c6fd2807SJeff Garzik  *	RETURNS:
1223633273a3STejun Heo  *	Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP or
1224633273a3STejun Heo  *	%ATA_DEV_UNKNOWN the event of failure.
1225c6fd2807SJeff Garzik  */
1226c6fd2807SJeff Garzik unsigned int ata_dev_classify(const struct ata_taskfile *tf)
1227c6fd2807SJeff Garzik {
1228c6fd2807SJeff Garzik 	/* Apple's open source Darwin code hints that some devices only
1229c6fd2807SJeff Garzik 	 * put a proper signature into the LBA mid/high registers,
1230c6fd2807SJeff Garzik 	 * So, we only check those.  It's sufficient for uniqueness.
1231633273a3STejun Heo 	 *
1232633273a3STejun Heo 	 * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate
1233633273a3STejun Heo 	 * signatures for ATA and ATAPI devices attached on SerialATA,
1234633273a3STejun Heo 	 * 0x3c/0xc3 and 0x69/0x96 respectively.  However, SerialATA
1235633273a3STejun Heo 	 * spec has never mentioned about using different signatures
1236633273a3STejun Heo 	 * for ATA/ATAPI devices.  Then, Serial ATA II: Port
1237633273a3STejun Heo 	 * Multiplier specification began to use 0x69/0x96 to identify
1238633273a3STejun Heo 	 * port multpliers and 0x3c/0xc3 to identify SEMB device.
1239633273a3STejun Heo 	 * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and
1240633273a3STejun Heo 	 * 0x69/0x96 shortly and described them as reserved for
1241633273a3STejun Heo 	 * SerialATA.
1242633273a3STejun Heo 	 *
1243633273a3STejun Heo 	 * We follow the current spec and consider that 0x69/0x96
1244633273a3STejun Heo 	 * identifies a port multiplier and 0x3c/0xc3 a SEMB device.
124579b42babSTejun Heo 	 * Unfortunately, WDC WD1600JS-62MHB5 (a hard drive) reports
124679b42babSTejun Heo 	 * SEMB signature.  This is worked around in
124779b42babSTejun Heo 	 * ata_dev_read_id().
1248c6fd2807SJeff Garzik 	 */
1249633273a3STejun Heo 	if ((tf->lbam == 0) && (tf->lbah == 0)) {
1250c6fd2807SJeff Garzik 		DPRINTK("found ATA device by sig\n");
1251c6fd2807SJeff Garzik 		return ATA_DEV_ATA;
1252c6fd2807SJeff Garzik 	}
1253c6fd2807SJeff Garzik 
1254633273a3STejun Heo 	if ((tf->lbam == 0x14) && (tf->lbah == 0xeb)) {
1255c6fd2807SJeff Garzik 		DPRINTK("found ATAPI device by sig\n");
1256c6fd2807SJeff Garzik 		return ATA_DEV_ATAPI;
1257c6fd2807SJeff Garzik 	}
1258c6fd2807SJeff Garzik 
1259633273a3STejun Heo 	if ((tf->lbam == 0x69) && (tf->lbah == 0x96)) {
1260633273a3STejun Heo 		DPRINTK("found PMP device by sig\n");
1261633273a3STejun Heo 		return ATA_DEV_PMP;
1262633273a3STejun Heo 	}
1263633273a3STejun Heo 
1264633273a3STejun Heo 	if ((tf->lbam == 0x3c) && (tf->lbah == 0xc3)) {
126579b42babSTejun Heo 		DPRINTK("found SEMB device by sig (could be ATA device)\n");
126679b42babSTejun Heo 		return ATA_DEV_SEMB;
1267633273a3STejun Heo 	}
1268633273a3STejun Heo 
1269c6fd2807SJeff Garzik 	DPRINTK("unknown device\n");
1270c6fd2807SJeff Garzik 	return ATA_DEV_UNKNOWN;
1271c6fd2807SJeff Garzik }
1272c6fd2807SJeff Garzik 
1273c6fd2807SJeff Garzik /**
1274c6fd2807SJeff Garzik  *	ata_id_string - Convert IDENTIFY DEVICE page into string
1275c6fd2807SJeff Garzik  *	@id: IDENTIFY DEVICE results we will examine
1276c6fd2807SJeff Garzik  *	@s: string into which data is output
1277c6fd2807SJeff Garzik  *	@ofs: offset into identify device page
1278c6fd2807SJeff Garzik  *	@len: length of string to return. must be an even number.
1279c6fd2807SJeff Garzik  *
1280c6fd2807SJeff Garzik  *	The strings in the IDENTIFY DEVICE page are broken up into
1281c6fd2807SJeff Garzik  *	16-bit chunks.  Run through the string, and output each
1282c6fd2807SJeff Garzik  *	8-bit chunk linearly, regardless of platform.
1283c6fd2807SJeff Garzik  *
1284c6fd2807SJeff Garzik  *	LOCKING:
1285c6fd2807SJeff Garzik  *	caller.
1286c6fd2807SJeff Garzik  */
1287c6fd2807SJeff Garzik 
1288c6fd2807SJeff Garzik void ata_id_string(const u16 *id, unsigned char *s,
1289c6fd2807SJeff Garzik 		   unsigned int ofs, unsigned int len)
1290c6fd2807SJeff Garzik {
1291c6fd2807SJeff Garzik 	unsigned int c;
1292c6fd2807SJeff Garzik 
1293963e4975SAlan Cox 	BUG_ON(len & 1);
1294963e4975SAlan Cox 
1295c6fd2807SJeff Garzik 	while (len > 0) {
1296c6fd2807SJeff Garzik 		c = id[ofs] >> 8;
1297c6fd2807SJeff Garzik 		*s = c;
1298c6fd2807SJeff Garzik 		s++;
1299c6fd2807SJeff Garzik 
1300c6fd2807SJeff Garzik 		c = id[ofs] & 0xff;
1301c6fd2807SJeff Garzik 		*s = c;
1302c6fd2807SJeff Garzik 		s++;
1303c6fd2807SJeff Garzik 
1304c6fd2807SJeff Garzik 		ofs++;
1305c6fd2807SJeff Garzik 		len -= 2;
1306c6fd2807SJeff Garzik 	}
1307c6fd2807SJeff Garzik }
1308c6fd2807SJeff Garzik 
1309c6fd2807SJeff Garzik /**
1310c6fd2807SJeff Garzik  *	ata_id_c_string - Convert IDENTIFY DEVICE page into C string
1311c6fd2807SJeff Garzik  *	@id: IDENTIFY DEVICE results we will examine
1312c6fd2807SJeff Garzik  *	@s: string into which data is output
1313c6fd2807SJeff Garzik  *	@ofs: offset into identify device page
1314c6fd2807SJeff Garzik  *	@len: length of string to return. must be an odd number.
1315c6fd2807SJeff Garzik  *
1316c6fd2807SJeff Garzik  *	This function is identical to ata_id_string except that it
1317c6fd2807SJeff Garzik  *	trims trailing spaces and terminates the resulting string with
1318c6fd2807SJeff Garzik  *	null.  @len must be actual maximum length (even number) + 1.
1319c6fd2807SJeff Garzik  *
1320c6fd2807SJeff Garzik  *	LOCKING:
1321c6fd2807SJeff Garzik  *	caller.
1322c6fd2807SJeff Garzik  */
1323c6fd2807SJeff Garzik void ata_id_c_string(const u16 *id, unsigned char *s,
1324c6fd2807SJeff Garzik 		     unsigned int ofs, unsigned int len)
1325c6fd2807SJeff Garzik {
1326c6fd2807SJeff Garzik 	unsigned char *p;
1327c6fd2807SJeff Garzik 
1328c6fd2807SJeff Garzik 	ata_id_string(id, s, ofs, len - 1);
1329c6fd2807SJeff Garzik 
1330c6fd2807SJeff Garzik 	p = s + strnlen(s, len - 1);
1331c6fd2807SJeff Garzik 	while (p > s && p[-1] == ' ')
1332c6fd2807SJeff Garzik 		p--;
1333c6fd2807SJeff Garzik 	*p = '\0';
1334c6fd2807SJeff Garzik }
1335c6fd2807SJeff Garzik 
1336db6f8759STejun Heo static u64 ata_id_n_sectors(const u16 *id)
1337db6f8759STejun Heo {
1338db6f8759STejun Heo 	if (ata_id_has_lba(id)) {
1339db6f8759STejun Heo 		if (ata_id_has_lba48(id))
1340968e594aSRobert Hancock 			return ata_id_u64(id, ATA_ID_LBA_CAPACITY_2);
1341db6f8759STejun Heo 		else
1342968e594aSRobert Hancock 			return ata_id_u32(id, ATA_ID_LBA_CAPACITY);
1343db6f8759STejun Heo 	} else {
1344db6f8759STejun Heo 		if (ata_id_current_chs_valid(id))
1345968e594aSRobert Hancock 			return id[ATA_ID_CUR_CYLS] * id[ATA_ID_CUR_HEADS] *
1346968e594aSRobert Hancock 			       id[ATA_ID_CUR_SECTORS];
1347db6f8759STejun Heo 		else
1348968e594aSRobert Hancock 			return id[ATA_ID_CYLS] * id[ATA_ID_HEADS] *
1349968e594aSRobert Hancock 			       id[ATA_ID_SECTORS];
1350db6f8759STejun Heo 	}
1351db6f8759STejun Heo }
1352db6f8759STejun Heo 
1353a5987e0aSTejun Heo u64 ata_tf_to_lba48(const struct ata_taskfile *tf)
13541e999736SAlan Cox {
13551e999736SAlan Cox 	u64 sectors = 0;
13561e999736SAlan Cox 
13571e999736SAlan Cox 	sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40;
13581e999736SAlan Cox 	sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32;
1359ba14a9c2SRoland Dreier 	sectors |= ((u64)(tf->hob_lbal & 0xff)) << 24;
13601e999736SAlan Cox 	sectors |= (tf->lbah & 0xff) << 16;
13611e999736SAlan Cox 	sectors |= (tf->lbam & 0xff) << 8;
13621e999736SAlan Cox 	sectors |= (tf->lbal & 0xff);
13631e999736SAlan Cox 
1364a5987e0aSTejun Heo 	return sectors;
13651e999736SAlan Cox }
13661e999736SAlan Cox 
1367a5987e0aSTejun Heo u64 ata_tf_to_lba(const struct ata_taskfile *tf)
13681e999736SAlan Cox {
13691e999736SAlan Cox 	u64 sectors = 0;
13701e999736SAlan Cox 
13711e999736SAlan Cox 	sectors |= (tf->device & 0x0f) << 24;
13721e999736SAlan Cox 	sectors |= (tf->lbah & 0xff) << 16;
13731e999736SAlan Cox 	sectors |= (tf->lbam & 0xff) << 8;
13741e999736SAlan Cox 	sectors |= (tf->lbal & 0xff);
13751e999736SAlan Cox 
1376a5987e0aSTejun Heo 	return sectors;
13771e999736SAlan Cox }
13781e999736SAlan Cox 
13791e999736SAlan Cox /**
1380c728a914STejun Heo  *	ata_read_native_max_address - Read native max address
1381c728a914STejun Heo  *	@dev: target device
1382c728a914STejun Heo  *	@max_sectors: out parameter for the result native max address
13831e999736SAlan Cox  *
1384c728a914STejun Heo  *	Perform an LBA48 or LBA28 native size query upon the device in
1385c728a914STejun Heo  *	question.
1386c728a914STejun Heo  *
1387c728a914STejun Heo  *	RETURNS:
1388c728a914STejun Heo  *	0 on success, -EACCES if command is aborted by the drive.
1389c728a914STejun Heo  *	-EIO on other errors.
13901e999736SAlan Cox  */
1391c728a914STejun Heo static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
13921e999736SAlan Cox {
1393c728a914STejun Heo 	unsigned int err_mask;
13941e999736SAlan Cox 	struct ata_taskfile tf;
1395c728a914STejun Heo 	int lba48 = ata_id_has_lba48(dev->id);
13961e999736SAlan Cox 
13971e999736SAlan Cox 	ata_tf_init(dev, &tf);
13981e999736SAlan Cox 
1399c728a914STejun Heo 	/* always clear all address registers */
14001e999736SAlan Cox 	tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1401c728a914STejun Heo 
1402c728a914STejun Heo 	if (lba48) {
1403c728a914STejun Heo 		tf.command = ATA_CMD_READ_NATIVE_MAX_EXT;
1404c728a914STejun Heo 		tf.flags |= ATA_TFLAG_LBA48;
1405c728a914STejun Heo 	} else
1406c728a914STejun Heo 		tf.command = ATA_CMD_READ_NATIVE_MAX;
1407c728a914STejun Heo 
14081e999736SAlan Cox 	tf.protocol |= ATA_PROT_NODATA;
1409c728a914STejun Heo 	tf.device |= ATA_LBA;
14101e999736SAlan Cox 
14112b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1412c728a914STejun Heo 	if (err_mask) {
1413c728a914STejun Heo 		ata_dev_printk(dev, KERN_WARNING, "failed to read native "
1414c728a914STejun Heo 			       "max address (err_mask=0x%x)\n", err_mask);
1415c728a914STejun Heo 		if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
1416c728a914STejun Heo 			return -EACCES;
1417c728a914STejun Heo 		return -EIO;
1418c728a914STejun Heo 	}
1419c728a914STejun Heo 
1420c728a914STejun Heo 	if (lba48)
1421a5987e0aSTejun Heo 		*max_sectors = ata_tf_to_lba48(&tf) + 1;
1422c728a914STejun Heo 	else
1423a5987e0aSTejun Heo 		*max_sectors = ata_tf_to_lba(&tf) + 1;
142493328e11SAlan Cox 	if (dev->horkage & ATA_HORKAGE_HPA_SIZE)
142593328e11SAlan Cox 		(*max_sectors)--;
14261e999736SAlan Cox 	return 0;
14271e999736SAlan Cox }
14281e999736SAlan Cox 
14291e999736SAlan Cox /**
1430c728a914STejun Heo  *	ata_set_max_sectors - Set max sectors
1431c728a914STejun Heo  *	@dev: target device
14326b38d1d1SRandy Dunlap  *	@new_sectors: new max sectors value to set for the device
14331e999736SAlan Cox  *
1434c728a914STejun Heo  *	Set max sectors of @dev to @new_sectors.
1435c728a914STejun Heo  *
1436c728a914STejun Heo  *	RETURNS:
1437c728a914STejun Heo  *	0 on success, -EACCES if command is aborted or denied (due to
1438c728a914STejun Heo  *	previous non-volatile SET_MAX) by the drive.  -EIO on other
1439c728a914STejun Heo  *	errors.
14401e999736SAlan Cox  */
144105027adcSTejun Heo static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
14421e999736SAlan Cox {
1443c728a914STejun Heo 	unsigned int err_mask;
14441e999736SAlan Cox 	struct ata_taskfile tf;
1445c728a914STejun Heo 	int lba48 = ata_id_has_lba48(dev->id);
14461e999736SAlan Cox 
14471e999736SAlan Cox 	new_sectors--;
14481e999736SAlan Cox 
14491e999736SAlan Cox 	ata_tf_init(dev, &tf);
14501e999736SAlan Cox 
1451c728a914STejun Heo 	tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
14521e999736SAlan Cox 
1453c728a914STejun Heo 	if (lba48) {
1454c728a914STejun Heo 		tf.command = ATA_CMD_SET_MAX_EXT;
1455c728a914STejun Heo 		tf.flags |= ATA_TFLAG_LBA48;
14561e999736SAlan Cox 
14571e999736SAlan Cox 		tf.hob_lbal = (new_sectors >> 24) & 0xff;
14581e999736SAlan Cox 		tf.hob_lbam = (new_sectors >> 32) & 0xff;
14591e999736SAlan Cox 		tf.hob_lbah = (new_sectors >> 40) & 0xff;
14601e582ba4STejun Heo 	} else {
14611e999736SAlan Cox 		tf.command = ATA_CMD_SET_MAX;
1462c728a914STejun Heo 
14631e582ba4STejun Heo 		tf.device |= (new_sectors >> 24) & 0xf;
14641e582ba4STejun Heo 	}
14651e582ba4STejun Heo 
14661e999736SAlan Cox 	tf.protocol |= ATA_PROT_NODATA;
1467c728a914STejun Heo 	tf.device |= ATA_LBA;
14681e999736SAlan Cox 
14691e999736SAlan Cox 	tf.lbal = (new_sectors >> 0) & 0xff;
14701e999736SAlan Cox 	tf.lbam = (new_sectors >> 8) & 0xff;
14711e999736SAlan Cox 	tf.lbah = (new_sectors >> 16) & 0xff;
14721e999736SAlan Cox 
14732b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1474c728a914STejun Heo 	if (err_mask) {
1475c728a914STejun Heo 		ata_dev_printk(dev, KERN_WARNING, "failed to set "
1476c728a914STejun Heo 			       "max address (err_mask=0x%x)\n", err_mask);
1477c728a914STejun Heo 		if (err_mask == AC_ERR_DEV &&
1478c728a914STejun Heo 		    (tf.feature & (ATA_ABORTED | ATA_IDNF)))
1479c728a914STejun Heo 			return -EACCES;
1480c728a914STejun Heo 		return -EIO;
1481c728a914STejun Heo 	}
1482c728a914STejun Heo 
14831e999736SAlan Cox 	return 0;
14841e999736SAlan Cox }
14851e999736SAlan Cox 
14861e999736SAlan Cox /**
14871e999736SAlan Cox  *	ata_hpa_resize		-	Resize a device with an HPA set
14881e999736SAlan Cox  *	@dev: Device to resize
14891e999736SAlan Cox  *
14901e999736SAlan Cox  *	Read the size of an LBA28 or LBA48 disk with HPA features and resize
14911e999736SAlan Cox  *	it if required to the full size of the media. The caller must check
14921e999736SAlan Cox  *	the drive has the HPA feature set enabled.
149305027adcSTejun Heo  *
149405027adcSTejun Heo  *	RETURNS:
149505027adcSTejun Heo  *	0 on success, -errno on failure.
14961e999736SAlan Cox  */
149705027adcSTejun Heo static int ata_hpa_resize(struct ata_device *dev)
14981e999736SAlan Cox {
149905027adcSTejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
150005027adcSTejun Heo 	int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
1501445d211bSTejun Heo 	bool unlock_hpa = ata_ignore_hpa || dev->flags & ATA_DFLAG_UNLOCK_HPA;
150205027adcSTejun Heo 	u64 sectors = ata_id_n_sectors(dev->id);
150305027adcSTejun Heo 	u64 native_sectors;
1504c728a914STejun Heo 	int rc;
15051e999736SAlan Cox 
150605027adcSTejun Heo 	/* do we need to do it? */
150705027adcSTejun Heo 	if (dev->class != ATA_DEV_ATA ||
150805027adcSTejun Heo 	    !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) ||
150905027adcSTejun Heo 	    (dev->horkage & ATA_HORKAGE_BROKEN_HPA))
1510c728a914STejun Heo 		return 0;
15111e999736SAlan Cox 
151205027adcSTejun Heo 	/* read native max address */
151305027adcSTejun Heo 	rc = ata_read_native_max_address(dev, &native_sectors);
151405027adcSTejun Heo 	if (rc) {
1515dda7aba1STejun Heo 		/* If device aborted the command or HPA isn't going to
1516dda7aba1STejun Heo 		 * be unlocked, skip HPA resizing.
151705027adcSTejun Heo 		 */
1518445d211bSTejun Heo 		if (rc == -EACCES || !unlock_hpa) {
151905027adcSTejun Heo 			ata_dev_printk(dev, KERN_WARNING, "HPA support seems "
1520dda7aba1STejun Heo 				       "broken, skipping HPA handling\n");
152105027adcSTejun Heo 			dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
152205027adcSTejun Heo 
152305027adcSTejun Heo 			/* we can continue if device aborted the command */
152405027adcSTejun Heo 			if (rc == -EACCES)
152505027adcSTejun Heo 				rc = 0;
152605027adcSTejun Heo 		}
152705027adcSTejun Heo 
152805027adcSTejun Heo 		return rc;
152905027adcSTejun Heo 	}
15305920dadfSTejun Heo 	dev->n_native_sectors = native_sectors;
153105027adcSTejun Heo 
153205027adcSTejun Heo 	/* nothing to do? */
1533445d211bSTejun Heo 	if (native_sectors <= sectors || !unlock_hpa) {
153405027adcSTejun Heo 		if (!print_info || native_sectors == sectors)
153505027adcSTejun Heo 			return 0;
153605027adcSTejun Heo 
153705027adcSTejun Heo 		if (native_sectors > sectors)
15381e999736SAlan Cox 			ata_dev_printk(dev, KERN_INFO,
153905027adcSTejun Heo 				"HPA detected: current %llu, native %llu\n",
154005027adcSTejun Heo 				(unsigned long long)sectors,
154105027adcSTejun Heo 				(unsigned long long)native_sectors);
154205027adcSTejun Heo 		else if (native_sectors < sectors)
154305027adcSTejun Heo 			ata_dev_printk(dev, KERN_WARNING,
154405027adcSTejun Heo 				"native sectors (%llu) is smaller than "
154505027adcSTejun Heo 				"sectors (%llu)\n",
154605027adcSTejun Heo 				(unsigned long long)native_sectors,
154705027adcSTejun Heo 				(unsigned long long)sectors);
154805027adcSTejun Heo 		return 0;
15491e999736SAlan Cox 	}
155037301a55STejun Heo 
155105027adcSTejun Heo 	/* let's unlock HPA */
155205027adcSTejun Heo 	rc = ata_set_max_sectors(dev, native_sectors);
155305027adcSTejun Heo 	if (rc == -EACCES) {
155405027adcSTejun Heo 		/* if device aborted the command, skip HPA resizing */
155505027adcSTejun Heo 		ata_dev_printk(dev, KERN_WARNING, "device aborted resize "
155605027adcSTejun Heo 			       "(%llu -> %llu), skipping HPA handling\n",
155705027adcSTejun Heo 			       (unsigned long long)sectors,
155805027adcSTejun Heo 			       (unsigned long long)native_sectors);
155905027adcSTejun Heo 		dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
156005027adcSTejun Heo 		return 0;
156105027adcSTejun Heo 	} else if (rc)
156205027adcSTejun Heo 		return rc;
156305027adcSTejun Heo 
156405027adcSTejun Heo 	/* re-read IDENTIFY data */
156505027adcSTejun Heo 	rc = ata_dev_reread_id(dev, 0);
156605027adcSTejun Heo 	if (rc) {
156705027adcSTejun Heo 		ata_dev_printk(dev, KERN_ERR, "failed to re-read IDENTIFY "
156805027adcSTejun Heo 			       "data after HPA resizing\n");
156905027adcSTejun Heo 		return rc;
157005027adcSTejun Heo 	}
157105027adcSTejun Heo 
157205027adcSTejun Heo 	if (print_info) {
157305027adcSTejun Heo 		u64 new_sectors = ata_id_n_sectors(dev->id);
157405027adcSTejun Heo 		ata_dev_printk(dev, KERN_INFO,
157505027adcSTejun Heo 			"HPA unlocked: %llu -> %llu, native %llu\n",
157605027adcSTejun Heo 			(unsigned long long)sectors,
157705027adcSTejun Heo 			(unsigned long long)new_sectors,
157805027adcSTejun Heo 			(unsigned long long)native_sectors);
157905027adcSTejun Heo 	}
158005027adcSTejun Heo 
158105027adcSTejun Heo 	return 0;
15821e999736SAlan Cox }
15831e999736SAlan Cox 
1584c6fd2807SJeff Garzik /**
1585c6fd2807SJeff Garzik  *	ata_dump_id - IDENTIFY DEVICE info debugging output
1586c6fd2807SJeff Garzik  *	@id: IDENTIFY DEVICE page to dump
1587c6fd2807SJeff Garzik  *
1588c6fd2807SJeff Garzik  *	Dump selected 16-bit words from the given IDENTIFY DEVICE
1589c6fd2807SJeff Garzik  *	page.
1590c6fd2807SJeff Garzik  *
1591c6fd2807SJeff Garzik  *	LOCKING:
1592c6fd2807SJeff Garzik  *	caller.
1593c6fd2807SJeff Garzik  */
1594c6fd2807SJeff Garzik 
1595c6fd2807SJeff Garzik static inline void ata_dump_id(const u16 *id)
1596c6fd2807SJeff Garzik {
1597c6fd2807SJeff Garzik 	DPRINTK("49==0x%04x  "
1598c6fd2807SJeff Garzik 		"53==0x%04x  "
1599c6fd2807SJeff Garzik 		"63==0x%04x  "
1600c6fd2807SJeff Garzik 		"64==0x%04x  "
1601c6fd2807SJeff Garzik 		"75==0x%04x  \n",
1602c6fd2807SJeff Garzik 		id[49],
1603c6fd2807SJeff Garzik 		id[53],
1604c6fd2807SJeff Garzik 		id[63],
1605c6fd2807SJeff Garzik 		id[64],
1606c6fd2807SJeff Garzik 		id[75]);
1607c6fd2807SJeff Garzik 	DPRINTK("80==0x%04x  "
1608c6fd2807SJeff Garzik 		"81==0x%04x  "
1609c6fd2807SJeff Garzik 		"82==0x%04x  "
1610c6fd2807SJeff Garzik 		"83==0x%04x  "
1611c6fd2807SJeff Garzik 		"84==0x%04x  \n",
1612c6fd2807SJeff Garzik 		id[80],
1613c6fd2807SJeff Garzik 		id[81],
1614c6fd2807SJeff Garzik 		id[82],
1615c6fd2807SJeff Garzik 		id[83],
1616c6fd2807SJeff Garzik 		id[84]);
1617c6fd2807SJeff Garzik 	DPRINTK("88==0x%04x  "
1618c6fd2807SJeff Garzik 		"93==0x%04x\n",
1619c6fd2807SJeff Garzik 		id[88],
1620c6fd2807SJeff Garzik 		id[93]);
1621c6fd2807SJeff Garzik }
1622c6fd2807SJeff Garzik 
1623c6fd2807SJeff Garzik /**
1624c6fd2807SJeff Garzik  *	ata_id_xfermask - Compute xfermask from the given IDENTIFY data
1625c6fd2807SJeff Garzik  *	@id: IDENTIFY data to compute xfer mask from
1626c6fd2807SJeff Garzik  *
1627c6fd2807SJeff Garzik  *	Compute the xfermask for this device. This is not as trivial
1628c6fd2807SJeff Garzik  *	as it seems if we must consider early devices correctly.
1629c6fd2807SJeff Garzik  *
1630c6fd2807SJeff Garzik  *	FIXME: pre IDE drive timing (do we care ?).
1631c6fd2807SJeff Garzik  *
1632c6fd2807SJeff Garzik  *	LOCKING:
1633c6fd2807SJeff Garzik  *	None.
1634c6fd2807SJeff Garzik  *
1635c6fd2807SJeff Garzik  *	RETURNS:
1636c6fd2807SJeff Garzik  *	Computed xfermask
1637c6fd2807SJeff Garzik  */
16387dc951aeSTejun Heo unsigned long ata_id_xfermask(const u16 *id)
1639c6fd2807SJeff Garzik {
16407dc951aeSTejun Heo 	unsigned long pio_mask, mwdma_mask, udma_mask;
1641c6fd2807SJeff Garzik 
1642c6fd2807SJeff Garzik 	/* Usual case. Word 53 indicates word 64 is valid */
1643c6fd2807SJeff Garzik 	if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
1644c6fd2807SJeff Garzik 		pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
1645c6fd2807SJeff Garzik 		pio_mask <<= 3;
1646c6fd2807SJeff Garzik 		pio_mask |= 0x7;
1647c6fd2807SJeff Garzik 	} else {
1648c6fd2807SJeff Garzik 		/* If word 64 isn't valid then Word 51 high byte holds
1649c6fd2807SJeff Garzik 		 * the PIO timing number for the maximum. Turn it into
1650c6fd2807SJeff Garzik 		 * a mask.
1651c6fd2807SJeff Garzik 		 */
16527a0f1c8aSLennert Buytenhek 		u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
165346767aebSAlan Cox 		if (mode < 5)	/* Valid PIO range */
165446767aebSAlan Cox 			pio_mask = (2 << mode) - 1;
165546767aebSAlan Cox 		else
165646767aebSAlan Cox 			pio_mask = 1;
1657c6fd2807SJeff Garzik 
1658c6fd2807SJeff Garzik 		/* But wait.. there's more. Design your standards by
1659c6fd2807SJeff Garzik 		 * committee and you too can get a free iordy field to
1660c6fd2807SJeff Garzik 		 * process. However its the speeds not the modes that
1661c6fd2807SJeff Garzik 		 * are supported... Note drivers using the timing API
1662c6fd2807SJeff Garzik 		 * will get this right anyway
1663c6fd2807SJeff Garzik 		 */
1664c6fd2807SJeff Garzik 	}
1665c6fd2807SJeff Garzik 
1666c6fd2807SJeff Garzik 	mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
1667c6fd2807SJeff Garzik 
1668b352e57dSAlan Cox 	if (ata_id_is_cfa(id)) {
1669b352e57dSAlan Cox 		/*
1670b352e57dSAlan Cox 		 *	Process compact flash extended modes
1671b352e57dSAlan Cox 		 */
167262afe5d7SSergei Shtylyov 		int pio = (id[ATA_ID_CFA_MODES] >> 0) & 0x7;
167362afe5d7SSergei Shtylyov 		int dma = (id[ATA_ID_CFA_MODES] >> 3) & 0x7;
1674b352e57dSAlan Cox 
1675b352e57dSAlan Cox 		if (pio)
1676b352e57dSAlan Cox 			pio_mask |= (1 << 5);
1677b352e57dSAlan Cox 		if (pio > 1)
1678b352e57dSAlan Cox 			pio_mask |= (1 << 6);
1679b352e57dSAlan Cox 		if (dma)
1680b352e57dSAlan Cox 			mwdma_mask |= (1 << 3);
1681b352e57dSAlan Cox 		if (dma > 1)
1682b352e57dSAlan Cox 			mwdma_mask |= (1 << 4);
1683b352e57dSAlan Cox 	}
1684b352e57dSAlan Cox 
1685c6fd2807SJeff Garzik 	udma_mask = 0;
1686c6fd2807SJeff Garzik 	if (id[ATA_ID_FIELD_VALID] & (1 << 2))
1687c6fd2807SJeff Garzik 		udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
1688c6fd2807SJeff Garzik 
1689c6fd2807SJeff Garzik 	return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
1690c6fd2807SJeff Garzik }
1691c6fd2807SJeff Garzik 
16927102d230SAdrian Bunk static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
1693c6fd2807SJeff Garzik {
1694c6fd2807SJeff Garzik 	struct completion *waiting = qc->private_data;
1695c6fd2807SJeff Garzik 
1696c6fd2807SJeff Garzik 	complete(waiting);
1697c6fd2807SJeff Garzik }
1698c6fd2807SJeff Garzik 
1699c6fd2807SJeff Garzik /**
17002432697bSTejun Heo  *	ata_exec_internal_sg - execute libata internal command
1701c6fd2807SJeff Garzik  *	@dev: Device to which the command is sent
1702c6fd2807SJeff Garzik  *	@tf: Taskfile registers for the command and the result
1703c6fd2807SJeff Garzik  *	@cdb: CDB for packet command
1704c6fd2807SJeff Garzik  *	@dma_dir: Data tranfer direction of the command
17055c1ad8b3SRandy Dunlap  *	@sgl: sg list for the data buffer of the command
17062432697bSTejun Heo  *	@n_elem: Number of sg entries
17072b789108STejun Heo  *	@timeout: Timeout in msecs (0 for default)
1708c6fd2807SJeff Garzik  *
1709c6fd2807SJeff Garzik  *	Executes libata internal command with timeout.  @tf contains
1710c6fd2807SJeff Garzik  *	command on entry and result on return.  Timeout and error
1711c6fd2807SJeff Garzik  *	conditions are reported via return value.  No recovery action
1712c6fd2807SJeff Garzik  *	is taken after a command times out.  It's caller's duty to
1713c6fd2807SJeff Garzik  *	clean up after timeout.
1714c6fd2807SJeff Garzik  *
1715c6fd2807SJeff Garzik  *	LOCKING:
1716c6fd2807SJeff Garzik  *	None.  Should be called with kernel context, might sleep.
1717c6fd2807SJeff Garzik  *
1718c6fd2807SJeff Garzik  *	RETURNS:
1719c6fd2807SJeff Garzik  *	Zero on success, AC_ERR_* mask on failure
1720c6fd2807SJeff Garzik  */
17212432697bSTejun Heo unsigned ata_exec_internal_sg(struct ata_device *dev,
1722c6fd2807SJeff Garzik 			      struct ata_taskfile *tf, const u8 *cdb,
172387260216SJens Axboe 			      int dma_dir, struct scatterlist *sgl,
17242b789108STejun Heo 			      unsigned int n_elem, unsigned long timeout)
1725c6fd2807SJeff Garzik {
17269af5c9c9STejun Heo 	struct ata_link *link = dev->link;
17279af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
1728c6fd2807SJeff Garzik 	u8 command = tf->command;
172987fbc5a0STejun Heo 	int auto_timeout = 0;
1730c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
1731c6fd2807SJeff Garzik 	unsigned int tag, preempted_tag;
1732c6fd2807SJeff Garzik 	u32 preempted_sactive, preempted_qc_active;
1733da917d69STejun Heo 	int preempted_nr_active_links;
1734c6fd2807SJeff Garzik 	DECLARE_COMPLETION_ONSTACK(wait);
1735c6fd2807SJeff Garzik 	unsigned long flags;
1736c6fd2807SJeff Garzik 	unsigned int err_mask;
1737c6fd2807SJeff Garzik 	int rc;
1738c6fd2807SJeff Garzik 
1739c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1740c6fd2807SJeff Garzik 
1741c6fd2807SJeff Garzik 	/* no internal command while frozen */
1742c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_FROZEN) {
1743c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
1744c6fd2807SJeff Garzik 		return AC_ERR_SYSTEM;
1745c6fd2807SJeff Garzik 	}
1746c6fd2807SJeff Garzik 
1747c6fd2807SJeff Garzik 	/* initialize internal qc */
1748c6fd2807SJeff Garzik 
1749c6fd2807SJeff Garzik 	/* XXX: Tag 0 is used for drivers with legacy EH as some
1750c6fd2807SJeff Garzik 	 * drivers choke if any other tag is given.  This breaks
1751c6fd2807SJeff Garzik 	 * ata_tag_internal() test for those drivers.  Don't use new
1752c6fd2807SJeff Garzik 	 * EH stuff without converting to it.
1753c6fd2807SJeff Garzik 	 */
1754c6fd2807SJeff Garzik 	if (ap->ops->error_handler)
1755c6fd2807SJeff Garzik 		tag = ATA_TAG_INTERNAL;
1756c6fd2807SJeff Garzik 	else
1757c6fd2807SJeff Garzik 		tag = 0;
1758c6fd2807SJeff Garzik 
17598a8bc223STejun Heo 	if (test_and_set_bit(tag, &ap->qc_allocated))
17608a8bc223STejun Heo 		BUG();
1761c6fd2807SJeff Garzik 	qc = __ata_qc_from_tag(ap, tag);
1762c6fd2807SJeff Garzik 
1763c6fd2807SJeff Garzik 	qc->tag = tag;
1764c6fd2807SJeff Garzik 	qc->scsicmd = NULL;
1765c6fd2807SJeff Garzik 	qc->ap = ap;
1766c6fd2807SJeff Garzik 	qc->dev = dev;
1767c6fd2807SJeff Garzik 	ata_qc_reinit(qc);
1768c6fd2807SJeff Garzik 
17699af5c9c9STejun Heo 	preempted_tag = link->active_tag;
17709af5c9c9STejun Heo 	preempted_sactive = link->sactive;
1771c6fd2807SJeff Garzik 	preempted_qc_active = ap->qc_active;
1772da917d69STejun Heo 	preempted_nr_active_links = ap->nr_active_links;
17739af5c9c9STejun Heo 	link->active_tag = ATA_TAG_POISON;
17749af5c9c9STejun Heo 	link->sactive = 0;
1775c6fd2807SJeff Garzik 	ap->qc_active = 0;
1776da917d69STejun Heo 	ap->nr_active_links = 0;
1777c6fd2807SJeff Garzik 
1778c6fd2807SJeff Garzik 	/* prepare & issue qc */
1779c6fd2807SJeff Garzik 	qc->tf = *tf;
1780c6fd2807SJeff Garzik 	if (cdb)
1781c6fd2807SJeff Garzik 		memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
1782c6fd2807SJeff Garzik 	qc->flags |= ATA_QCFLAG_RESULT_TF;
1783c6fd2807SJeff Garzik 	qc->dma_dir = dma_dir;
1784c6fd2807SJeff Garzik 	if (dma_dir != DMA_NONE) {
17852432697bSTejun Heo 		unsigned int i, buflen = 0;
178687260216SJens Axboe 		struct scatterlist *sg;
17872432697bSTejun Heo 
178887260216SJens Axboe 		for_each_sg(sgl, sg, n_elem, i)
178987260216SJens Axboe 			buflen += sg->length;
17902432697bSTejun Heo 
179187260216SJens Axboe 		ata_sg_init(qc, sgl, n_elem);
179249c80429SBrian King 		qc->nbytes = buflen;
1793c6fd2807SJeff Garzik 	}
1794c6fd2807SJeff Garzik 
1795c6fd2807SJeff Garzik 	qc->private_data = &wait;
1796c6fd2807SJeff Garzik 	qc->complete_fn = ata_qc_complete_internal;
1797c6fd2807SJeff Garzik 
1798c6fd2807SJeff Garzik 	ata_qc_issue(qc);
1799c6fd2807SJeff Garzik 
1800c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1801c6fd2807SJeff Garzik 
180287fbc5a0STejun Heo 	if (!timeout) {
180387fbc5a0STejun Heo 		if (ata_probe_timeout)
1804341c2c95STejun Heo 			timeout = ata_probe_timeout * 1000;
180587fbc5a0STejun Heo 		else {
180687fbc5a0STejun Heo 			timeout = ata_internal_cmd_timeout(dev, command);
180787fbc5a0STejun Heo 			auto_timeout = 1;
180887fbc5a0STejun Heo 		}
180987fbc5a0STejun Heo 	}
18102b789108STejun Heo 
18112b789108STejun Heo 	rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
1812c6fd2807SJeff Garzik 
1813c429137aSTejun Heo 	ata_sff_flush_pio_task(ap);
1814c6fd2807SJeff Garzik 
1815c6fd2807SJeff Garzik 	if (!rc) {
1816c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
1817c6fd2807SJeff Garzik 
1818c6fd2807SJeff Garzik 		/* We're racing with irq here.  If we lose, the
1819c6fd2807SJeff Garzik 		 * following test prevents us from completing the qc
1820c6fd2807SJeff Garzik 		 * twice.  If we win, the port is frozen and will be
1821c6fd2807SJeff Garzik 		 * cleaned up by ->post_internal_cmd().
1822c6fd2807SJeff Garzik 		 */
1823c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_ACTIVE) {
1824c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_TIMEOUT;
1825c6fd2807SJeff Garzik 
1826c6fd2807SJeff Garzik 			if (ap->ops->error_handler)
1827c6fd2807SJeff Garzik 				ata_port_freeze(ap);
1828c6fd2807SJeff Garzik 			else
1829c6fd2807SJeff Garzik 				ata_qc_complete(qc);
1830c6fd2807SJeff Garzik 
1831c6fd2807SJeff Garzik 			if (ata_msg_warn(ap))
1832c6fd2807SJeff Garzik 				ata_dev_printk(dev, KERN_WARNING,
1833c6fd2807SJeff Garzik 					"qc timeout (cmd 0x%x)\n", command);
1834c6fd2807SJeff Garzik 		}
1835c6fd2807SJeff Garzik 
1836c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
1837c6fd2807SJeff Garzik 	}
1838c6fd2807SJeff Garzik 
1839c6fd2807SJeff Garzik 	/* do post_internal_cmd */
1840c6fd2807SJeff Garzik 	if (ap->ops->post_internal_cmd)
1841c6fd2807SJeff Garzik 		ap->ops->post_internal_cmd(qc);
1842c6fd2807SJeff Garzik 
1843a51d644aSTejun Heo 	/* perform minimal error analysis */
1844a51d644aSTejun Heo 	if (qc->flags & ATA_QCFLAG_FAILED) {
1845a51d644aSTejun Heo 		if (qc->result_tf.command & (ATA_ERR | ATA_DF))
1846a51d644aSTejun Heo 			qc->err_mask |= AC_ERR_DEV;
1847a51d644aSTejun Heo 
1848a51d644aSTejun Heo 		if (!qc->err_mask)
1849c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_OTHER;
1850a51d644aSTejun Heo 
1851a51d644aSTejun Heo 		if (qc->err_mask & ~AC_ERR_OTHER)
1852a51d644aSTejun Heo 			qc->err_mask &= ~AC_ERR_OTHER;
1853c6fd2807SJeff Garzik 	}
1854c6fd2807SJeff Garzik 
1855c6fd2807SJeff Garzik 	/* finish up */
1856c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1857c6fd2807SJeff Garzik 
1858c6fd2807SJeff Garzik 	*tf = qc->result_tf;
1859c6fd2807SJeff Garzik 	err_mask = qc->err_mask;
1860c6fd2807SJeff Garzik 
1861c6fd2807SJeff Garzik 	ata_qc_free(qc);
18629af5c9c9STejun Heo 	link->active_tag = preempted_tag;
18639af5c9c9STejun Heo 	link->sactive = preempted_sactive;
1864c6fd2807SJeff Garzik 	ap->qc_active = preempted_qc_active;
1865da917d69STejun Heo 	ap->nr_active_links = preempted_nr_active_links;
1866c6fd2807SJeff Garzik 
1867c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1868c6fd2807SJeff Garzik 
186987fbc5a0STejun Heo 	if ((err_mask & AC_ERR_TIMEOUT) && auto_timeout)
187087fbc5a0STejun Heo 		ata_internal_cmd_timed_out(dev, command);
187187fbc5a0STejun Heo 
1872c6fd2807SJeff Garzik 	return err_mask;
1873c6fd2807SJeff Garzik }
1874c6fd2807SJeff Garzik 
1875c6fd2807SJeff Garzik /**
187633480a0eSTejun Heo  *	ata_exec_internal - execute libata internal command
18772432697bSTejun Heo  *	@dev: Device to which the command is sent
18782432697bSTejun Heo  *	@tf: Taskfile registers for the command and the result
18792432697bSTejun Heo  *	@cdb: CDB for packet command
18802432697bSTejun Heo  *	@dma_dir: Data tranfer direction of the command
18812432697bSTejun Heo  *	@buf: Data buffer of the command
18822432697bSTejun Heo  *	@buflen: Length of data buffer
18832b789108STejun Heo  *	@timeout: Timeout in msecs (0 for default)
18842432697bSTejun Heo  *
18852432697bSTejun Heo  *	Wrapper around ata_exec_internal_sg() which takes simple
18862432697bSTejun Heo  *	buffer instead of sg list.
18872432697bSTejun Heo  *
18882432697bSTejun Heo  *	LOCKING:
18892432697bSTejun Heo  *	None.  Should be called with kernel context, might sleep.
18902432697bSTejun Heo  *
18912432697bSTejun Heo  *	RETURNS:
18922432697bSTejun Heo  *	Zero on success, AC_ERR_* mask on failure
18932432697bSTejun Heo  */
18942432697bSTejun Heo unsigned ata_exec_internal(struct ata_device *dev,
18952432697bSTejun Heo 			   struct ata_taskfile *tf, const u8 *cdb,
18962b789108STejun Heo 			   int dma_dir, void *buf, unsigned int buflen,
18972b789108STejun Heo 			   unsigned long timeout)
18982432697bSTejun Heo {
189933480a0eSTejun Heo 	struct scatterlist *psg = NULL, sg;
190033480a0eSTejun Heo 	unsigned int n_elem = 0;
19012432697bSTejun Heo 
190233480a0eSTejun Heo 	if (dma_dir != DMA_NONE) {
190333480a0eSTejun Heo 		WARN_ON(!buf);
19042432697bSTejun Heo 		sg_init_one(&sg, buf, buflen);
190533480a0eSTejun Heo 		psg = &sg;
190633480a0eSTejun Heo 		n_elem++;
190733480a0eSTejun Heo 	}
19082432697bSTejun Heo 
19092b789108STejun Heo 	return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem,
19102b789108STejun Heo 				    timeout);
19112432697bSTejun Heo }
19122432697bSTejun Heo 
19132432697bSTejun Heo /**
1914c6fd2807SJeff Garzik  *	ata_do_simple_cmd - execute simple internal command
1915c6fd2807SJeff Garzik  *	@dev: Device to which the command is sent
1916c6fd2807SJeff Garzik  *	@cmd: Opcode to execute
1917c6fd2807SJeff Garzik  *
1918c6fd2807SJeff Garzik  *	Execute a 'simple' command, that only consists of the opcode
1919c6fd2807SJeff Garzik  *	'cmd' itself, without filling any other registers
1920c6fd2807SJeff Garzik  *
1921c6fd2807SJeff Garzik  *	LOCKING:
1922c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1923c6fd2807SJeff Garzik  *
1924c6fd2807SJeff Garzik  *	RETURNS:
1925c6fd2807SJeff Garzik  *	Zero on success, AC_ERR_* mask on failure
1926c6fd2807SJeff Garzik  */
1927c6fd2807SJeff Garzik unsigned int ata_do_simple_cmd(struct ata_device *dev, u8 cmd)
1928c6fd2807SJeff Garzik {
1929c6fd2807SJeff Garzik 	struct ata_taskfile tf;
1930c6fd2807SJeff Garzik 
1931c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
1932c6fd2807SJeff Garzik 
1933c6fd2807SJeff Garzik 	tf.command = cmd;
1934c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_DEVICE;
1935c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
1936c6fd2807SJeff Garzik 
19372b789108STejun Heo 	return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1938c6fd2807SJeff Garzik }
1939c6fd2807SJeff Garzik 
1940c6fd2807SJeff Garzik /**
1941c6fd2807SJeff Garzik  *	ata_pio_need_iordy	-	check if iordy needed
1942c6fd2807SJeff Garzik  *	@adev: ATA device
1943c6fd2807SJeff Garzik  *
1944c6fd2807SJeff Garzik  *	Check if the current speed of the device requires IORDY. Used
1945c6fd2807SJeff Garzik  *	by various controllers for chip configuration.
1946c6fd2807SJeff Garzik  */
1947c6fd2807SJeff Garzik unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1948c6fd2807SJeff Garzik {
19490d9e6659STejun Heo 	/* Don't set IORDY if we're preparing for reset.  IORDY may
19500d9e6659STejun Heo 	 * lead to controller lock up on certain controllers if the
19510d9e6659STejun Heo 	 * port is not occupied.  See bko#11703 for details.
19520d9e6659STejun Heo 	 */
19530d9e6659STejun Heo 	if (adev->link->ap->pflags & ATA_PFLAG_RESETTING)
19540d9e6659STejun Heo 		return 0;
19550d9e6659STejun Heo 	/* Controller doesn't support IORDY.  Probably a pointless
19560d9e6659STejun Heo 	 * check as the caller should know this.
19570d9e6659STejun Heo 	 */
19589af5c9c9STejun Heo 	if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
1959c6fd2807SJeff Garzik 		return 0;
19605c18c4d2SDavid Daney 	/* CF spec. r4.1 Table 22 says no iordy on PIO5 and PIO6.  */
19615c18c4d2SDavid Daney 	if (ata_id_is_cfa(adev->id)
19625c18c4d2SDavid Daney 	    && (adev->pio_mode == XFER_PIO_5 || adev->pio_mode == XFER_PIO_6))
19635c18c4d2SDavid Daney 		return 0;
1964432729f0SAlan Cox 	/* PIO3 and higher it is mandatory */
1965432729f0SAlan Cox 	if (adev->pio_mode > XFER_PIO_2)
1966c6fd2807SJeff Garzik 		return 1;
1967432729f0SAlan Cox 	/* We turn it on when possible */
1968432729f0SAlan Cox 	if (ata_id_has_iordy(adev->id))
1969432729f0SAlan Cox 		return 1;
1970432729f0SAlan Cox 	return 0;
1971432729f0SAlan Cox }
1972c6fd2807SJeff Garzik 
1973432729f0SAlan Cox /**
1974432729f0SAlan Cox  *	ata_pio_mask_no_iordy	-	Return the non IORDY mask
1975432729f0SAlan Cox  *	@adev: ATA device
1976432729f0SAlan Cox  *
1977432729f0SAlan Cox  *	Compute the highest mode possible if we are not using iordy. Return
1978432729f0SAlan Cox  *	-1 if no iordy mode is available.
1979432729f0SAlan Cox  */
1980432729f0SAlan Cox static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
1981432729f0SAlan Cox {
1982c6fd2807SJeff Garzik 	/* If we have no drive specific rule, then PIO 2 is non IORDY */
1983c6fd2807SJeff Garzik 	if (adev->id[ATA_ID_FIELD_VALID] & 2) {	/* EIDE */
1984432729f0SAlan Cox 		u16 pio = adev->id[ATA_ID_EIDE_PIO];
1985c6fd2807SJeff Garzik 		/* Is the speed faster than the drive allows non IORDY ? */
1986c6fd2807SJeff Garzik 		if (pio) {
1987c6fd2807SJeff Garzik 			/* This is cycle times not frequency - watch the logic! */
1988c6fd2807SJeff Garzik 			if (pio > 240)	/* PIO2 is 240nS per cycle */
1989432729f0SAlan Cox 				return 3 << ATA_SHIFT_PIO;
1990432729f0SAlan Cox 			return 7 << ATA_SHIFT_PIO;
1991c6fd2807SJeff Garzik 		}
1992c6fd2807SJeff Garzik 	}
1993432729f0SAlan Cox 	return 3 << ATA_SHIFT_PIO;
1994c6fd2807SJeff Garzik }
1995c6fd2807SJeff Garzik 
1996c6fd2807SJeff Garzik /**
1997963e4975SAlan Cox  *	ata_do_dev_read_id		-	default ID read method
1998963e4975SAlan Cox  *	@dev: device
1999963e4975SAlan Cox  *	@tf: proposed taskfile
2000963e4975SAlan Cox  *	@id: data buffer
2001963e4975SAlan Cox  *
2002963e4975SAlan Cox  *	Issue the identify taskfile and hand back the buffer containing
2003963e4975SAlan Cox  *	identify data. For some RAID controllers and for pre ATA devices
2004963e4975SAlan Cox  *	this function is wrapped or replaced by the driver
2005963e4975SAlan Cox  */
2006963e4975SAlan Cox unsigned int ata_do_dev_read_id(struct ata_device *dev,
2007963e4975SAlan Cox 					struct ata_taskfile *tf, u16 *id)
2008963e4975SAlan Cox {
2009963e4975SAlan Cox 	return ata_exec_internal(dev, tf, NULL, DMA_FROM_DEVICE,
2010963e4975SAlan Cox 				     id, sizeof(id[0]) * ATA_ID_WORDS, 0);
2011963e4975SAlan Cox }
2012963e4975SAlan Cox 
2013963e4975SAlan Cox /**
2014c6fd2807SJeff Garzik  *	ata_dev_read_id - Read ID data from the specified device
2015c6fd2807SJeff Garzik  *	@dev: target device
2016c6fd2807SJeff Garzik  *	@p_class: pointer to class of the target device (may be changed)
2017bff04647STejun Heo  *	@flags: ATA_READID_* flags
2018c6fd2807SJeff Garzik  *	@id: buffer to read IDENTIFY data into
2019c6fd2807SJeff Garzik  *
2020c6fd2807SJeff Garzik  *	Read ID data from the specified device.  ATA_CMD_ID_ATA is
2021c6fd2807SJeff Garzik  *	performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
2022c6fd2807SJeff Garzik  *	devices.  This function also issues ATA_CMD_INIT_DEV_PARAMS
2023c6fd2807SJeff Garzik  *	for pre-ATA4 drives.
2024c6fd2807SJeff Garzik  *
202550a99018SAlan Cox  *	FIXME: ATA_CMD_ID_ATA is optional for early drives and right
202650a99018SAlan Cox  *	now we abort if we hit that case.
202750a99018SAlan Cox  *
2028c6fd2807SJeff Garzik  *	LOCKING:
2029c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
2030c6fd2807SJeff Garzik  *
2031c6fd2807SJeff Garzik  *	RETURNS:
2032c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
2033c6fd2807SJeff Garzik  */
2034c6fd2807SJeff Garzik int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
2035bff04647STejun Heo 		    unsigned int flags, u16 *id)
2036c6fd2807SJeff Garzik {
20379af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
2038c6fd2807SJeff Garzik 	unsigned int class = *p_class;
2039c6fd2807SJeff Garzik 	struct ata_taskfile tf;
2040c6fd2807SJeff Garzik 	unsigned int err_mask = 0;
2041c6fd2807SJeff Garzik 	const char *reason;
204279b42babSTejun Heo 	bool is_semb = class == ATA_DEV_SEMB;
204354936f8bSTejun Heo 	int may_fallback = 1, tried_spinup = 0;
2044c6fd2807SJeff Garzik 	int rc;
2045c6fd2807SJeff Garzik 
2046c6fd2807SJeff Garzik 	if (ata_msg_ctl(ap))
20477f5e4e8dSHarvey Harrison 		ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __func__);
2048c6fd2807SJeff Garzik 
2049c6fd2807SJeff Garzik retry:
2050c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
2051c6fd2807SJeff Garzik 
2052c6fd2807SJeff Garzik 	switch (class) {
205379b42babSTejun Heo 	case ATA_DEV_SEMB:
205479b42babSTejun Heo 		class = ATA_DEV_ATA;	/* some hard drives report SEMB sig */
2055c6fd2807SJeff Garzik 	case ATA_DEV_ATA:
2056c6fd2807SJeff Garzik 		tf.command = ATA_CMD_ID_ATA;
2057c6fd2807SJeff Garzik 		break;
2058c6fd2807SJeff Garzik 	case ATA_DEV_ATAPI:
2059c6fd2807SJeff Garzik 		tf.command = ATA_CMD_ID_ATAPI;
2060c6fd2807SJeff Garzik 		break;
2061c6fd2807SJeff Garzik 	default:
2062c6fd2807SJeff Garzik 		rc = -ENODEV;
2063c6fd2807SJeff Garzik 		reason = "unsupported class";
2064c6fd2807SJeff Garzik 		goto err_out;
2065c6fd2807SJeff Garzik 	}
2066c6fd2807SJeff Garzik 
2067c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_PIO;
206881afe893STejun Heo 
206981afe893STejun Heo 	/* Some devices choke if TF registers contain garbage.  Make
207081afe893STejun Heo 	 * sure those are properly initialized.
207181afe893STejun Heo 	 */
207281afe893STejun Heo 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
207381afe893STejun Heo 
207481afe893STejun Heo 	/* Device presence detection is unreliable on some
207581afe893STejun Heo 	 * controllers.  Always poll IDENTIFY if available.
207681afe893STejun Heo 	 */
207781afe893STejun Heo 	tf.flags |= ATA_TFLAG_POLLING;
2078c6fd2807SJeff Garzik 
2079963e4975SAlan Cox 	if (ap->ops->read_id)
2080963e4975SAlan Cox 		err_mask = ap->ops->read_id(dev, &tf, id);
2081963e4975SAlan Cox 	else
2082963e4975SAlan Cox 		err_mask = ata_do_dev_read_id(dev, &tf, id);
2083963e4975SAlan Cox 
2084c6fd2807SJeff Garzik 	if (err_mask) {
2085800b3996STejun Heo 		if (err_mask & AC_ERR_NODEV_HINT) {
20861ffc151fSTejun Heo 			ata_dev_printk(dev, KERN_DEBUG,
20871ffc151fSTejun Heo 				       "NODEV after polling detection\n");
208855a8e2c8STejun Heo 			return -ENOENT;
208955a8e2c8STejun Heo 		}
209055a8e2c8STejun Heo 
209179b42babSTejun Heo 		if (is_semb) {
209279b42babSTejun Heo 			ata_dev_printk(dev, KERN_INFO, "IDENTIFY failed on "
209379b42babSTejun Heo 				       "device w/ SEMB sig, disabled\n");
209479b42babSTejun Heo 			/* SEMB is not supported yet */
209579b42babSTejun Heo 			*p_class = ATA_DEV_SEMB_UNSUP;
209679b42babSTejun Heo 			return 0;
209779b42babSTejun Heo 		}
209879b42babSTejun Heo 
20991ffc151fSTejun Heo 		if ((err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
21001ffc151fSTejun Heo 			/* Device or controller might have reported
21011ffc151fSTejun Heo 			 * the wrong device class.  Give a shot at the
21021ffc151fSTejun Heo 			 * other IDENTIFY if the current one is
21031ffc151fSTejun Heo 			 * aborted by the device.
210454936f8bSTejun Heo 			 */
21051ffc151fSTejun Heo 			if (may_fallback) {
210654936f8bSTejun Heo 				may_fallback = 0;
210754936f8bSTejun Heo 
210854936f8bSTejun Heo 				if (class == ATA_DEV_ATA)
210954936f8bSTejun Heo 					class = ATA_DEV_ATAPI;
211054936f8bSTejun Heo 				else
211154936f8bSTejun Heo 					class = ATA_DEV_ATA;
211254936f8bSTejun Heo 				goto retry;
211354936f8bSTejun Heo 			}
211454936f8bSTejun Heo 
21151ffc151fSTejun Heo 			/* Control reaches here iff the device aborted
21161ffc151fSTejun Heo 			 * both flavors of IDENTIFYs which happens
21171ffc151fSTejun Heo 			 * sometimes with phantom devices.
21181ffc151fSTejun Heo 			 */
21191ffc151fSTejun Heo 			ata_dev_printk(dev, KERN_DEBUG,
21201ffc151fSTejun Heo 				       "both IDENTIFYs aborted, assuming NODEV\n");
21211ffc151fSTejun Heo 			return -ENOENT;
21221ffc151fSTejun Heo 		}
21231ffc151fSTejun Heo 
2124c6fd2807SJeff Garzik 		rc = -EIO;
2125c6fd2807SJeff Garzik 		reason = "I/O error";
2126c6fd2807SJeff Garzik 		goto err_out;
2127c6fd2807SJeff Garzik 	}
2128c6fd2807SJeff Garzik 
212943c9c591STejun Heo 	if (dev->horkage & ATA_HORKAGE_DUMP_ID) {
213043c9c591STejun Heo 		ata_dev_printk(dev, KERN_DEBUG, "dumping IDENTIFY data, "
213143c9c591STejun Heo 			       "class=%d may_fallback=%d tried_spinup=%d\n",
213243c9c591STejun Heo 			       class, may_fallback, tried_spinup);
213343c9c591STejun Heo 		print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET,
213443c9c591STejun Heo 			       16, 2, id, ATA_ID_WORDS * sizeof(*id), true);
213543c9c591STejun Heo 	}
213643c9c591STejun Heo 
213754936f8bSTejun Heo 	/* Falling back doesn't make sense if ID data was read
213854936f8bSTejun Heo 	 * successfully at least once.
213954936f8bSTejun Heo 	 */
214054936f8bSTejun Heo 	may_fallback = 0;
214154936f8bSTejun Heo 
2142c6fd2807SJeff Garzik 	swap_buf_le16(id, ATA_ID_WORDS);
2143c6fd2807SJeff Garzik 
2144c6fd2807SJeff Garzik 	/* sanity check */
2145c6fd2807SJeff Garzik 	rc = -EINVAL;
21466070068bSAlan Cox 	reason = "device reports invalid type";
21474a3381feSJeff Garzik 
21484a3381feSJeff Garzik 	if (class == ATA_DEV_ATA) {
21494a3381feSJeff Garzik 		if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
21504a3381feSJeff Garzik 			goto err_out;
21514a3381feSJeff Garzik 	} else {
21524a3381feSJeff Garzik 		if (ata_id_is_ata(id))
2153c6fd2807SJeff Garzik 			goto err_out;
2154c6fd2807SJeff Garzik 	}
2155c6fd2807SJeff Garzik 
2156169439c2SMark Lord 	if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
2157169439c2SMark Lord 		tried_spinup = 1;
2158169439c2SMark Lord 		/*
2159169439c2SMark Lord 		 * Drive powered-up in standby mode, and requires a specific
2160169439c2SMark Lord 		 * SET_FEATURES spin-up subcommand before it will accept
2161169439c2SMark Lord 		 * anything other than the original IDENTIFY command.
2162169439c2SMark Lord 		 */
2163218f3d30SJeff Garzik 		err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
2164fb0582f9SRyan Power 		if (err_mask && id[2] != 0x738c) {
2165169439c2SMark Lord 			rc = -EIO;
2166169439c2SMark Lord 			reason = "SPINUP failed";
2167169439c2SMark Lord 			goto err_out;
2168169439c2SMark Lord 		}
2169169439c2SMark Lord 		/*
2170169439c2SMark Lord 		 * If the drive initially returned incomplete IDENTIFY info,
2171169439c2SMark Lord 		 * we now must reissue the IDENTIFY command.
2172169439c2SMark Lord 		 */
2173169439c2SMark Lord 		if (id[2] == 0x37c8)
2174169439c2SMark Lord 			goto retry;
2175169439c2SMark Lord 	}
2176169439c2SMark Lord 
2177bff04647STejun Heo 	if ((flags & ATA_READID_POSTRESET) && class == ATA_DEV_ATA) {
2178c6fd2807SJeff Garzik 		/*
2179c6fd2807SJeff Garzik 		 * The exact sequence expected by certain pre-ATA4 drives is:
2180c6fd2807SJeff Garzik 		 * SRST RESET
218150a99018SAlan Cox 		 * IDENTIFY (optional in early ATA)
218250a99018SAlan Cox 		 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
2183c6fd2807SJeff Garzik 		 * anything else..
2184c6fd2807SJeff Garzik 		 * Some drives were very specific about that exact sequence.
218550a99018SAlan Cox 		 *
218650a99018SAlan Cox 		 * Note that ATA4 says lba is mandatory so the second check
2187c9404c9cSAdam Buchbinder 		 * should never trigger.
2188c6fd2807SJeff Garzik 		 */
2189c6fd2807SJeff Garzik 		if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
2190c6fd2807SJeff Garzik 			err_mask = ata_dev_init_params(dev, id[3], id[6]);
2191c6fd2807SJeff Garzik 			if (err_mask) {
2192c6fd2807SJeff Garzik 				rc = -EIO;
2193c6fd2807SJeff Garzik 				reason = "INIT_DEV_PARAMS failed";
2194c6fd2807SJeff Garzik 				goto err_out;
2195c6fd2807SJeff Garzik 			}
2196c6fd2807SJeff Garzik 
2197c6fd2807SJeff Garzik 			/* current CHS translation info (id[53-58]) might be
2198c6fd2807SJeff Garzik 			 * changed. reread the identify device info.
2199c6fd2807SJeff Garzik 			 */
2200bff04647STejun Heo 			flags &= ~ATA_READID_POSTRESET;
2201c6fd2807SJeff Garzik 			goto retry;
2202c6fd2807SJeff Garzik 		}
2203c6fd2807SJeff Garzik 	}
2204c6fd2807SJeff Garzik 
2205c6fd2807SJeff Garzik 	*p_class = class;
2206c6fd2807SJeff Garzik 
2207c6fd2807SJeff Garzik 	return 0;
2208c6fd2807SJeff Garzik 
2209c6fd2807SJeff Garzik  err_out:
2210c6fd2807SJeff Garzik 	if (ata_msg_warn(ap))
2211c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_WARNING, "failed to IDENTIFY "
2212c6fd2807SJeff Garzik 			       "(%s, err_mask=0x%x)\n", reason, err_mask);
2213c6fd2807SJeff Garzik 	return rc;
2214c6fd2807SJeff Garzik }
2215c6fd2807SJeff Garzik 
22169062712fSTejun Heo static int ata_do_link_spd_horkage(struct ata_device *dev)
22179062712fSTejun Heo {
22189062712fSTejun Heo 	struct ata_link *plink = ata_dev_phys_link(dev);
22199062712fSTejun Heo 	u32 target, target_limit;
22209062712fSTejun Heo 
22219062712fSTejun Heo 	if (!sata_scr_valid(plink))
22229062712fSTejun Heo 		return 0;
22239062712fSTejun Heo 
22249062712fSTejun Heo 	if (dev->horkage & ATA_HORKAGE_1_5_GBPS)
22259062712fSTejun Heo 		target = 1;
22269062712fSTejun Heo 	else
22279062712fSTejun Heo 		return 0;
22289062712fSTejun Heo 
22299062712fSTejun Heo 	target_limit = (1 << target) - 1;
22309062712fSTejun Heo 
22319062712fSTejun Heo 	/* if already on stricter limit, no need to push further */
22329062712fSTejun Heo 	if (plink->sata_spd_limit <= target_limit)
22339062712fSTejun Heo 		return 0;
22349062712fSTejun Heo 
22359062712fSTejun Heo 	plink->sata_spd_limit = target_limit;
22369062712fSTejun Heo 
22379062712fSTejun Heo 	/* Request another EH round by returning -EAGAIN if link is
22389062712fSTejun Heo 	 * going faster than the target speed.  Forward progress is
22399062712fSTejun Heo 	 * guaranteed by setting sata_spd_limit to target_limit above.
22409062712fSTejun Heo 	 */
22419062712fSTejun Heo 	if (plink->sata_spd > target) {
22429062712fSTejun Heo 		ata_dev_printk(dev, KERN_INFO,
22439062712fSTejun Heo 			       "applying link speed limit horkage to %s\n",
22449062712fSTejun Heo 			       sata_spd_string(target));
22459062712fSTejun Heo 		return -EAGAIN;
22469062712fSTejun Heo 	}
22479062712fSTejun Heo 	return 0;
22489062712fSTejun Heo }
22499062712fSTejun Heo 
2250c6fd2807SJeff Garzik static inline u8 ata_dev_knobble(struct ata_device *dev)
2251c6fd2807SJeff Garzik {
22529af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
22539ce8e307SJens Axboe 
22549ce8e307SJens Axboe 	if (ata_dev_blacklisted(dev) & ATA_HORKAGE_BRIDGE_OK)
22559ce8e307SJens Axboe 		return 0;
22569ce8e307SJens Axboe 
22579af5c9c9STejun Heo 	return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
2258c6fd2807SJeff Garzik }
2259c6fd2807SJeff Garzik 
2260388539f3SShaohua Li static int ata_dev_config_ncq(struct ata_device *dev,
2261c6fd2807SJeff Garzik 			       char *desc, size_t desc_sz)
2262c6fd2807SJeff Garzik {
22639af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
2264c6fd2807SJeff Garzik 	int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
2265388539f3SShaohua Li 	unsigned int err_mask;
2266388539f3SShaohua Li 	char *aa_desc = "";
2267c6fd2807SJeff Garzik 
2268c6fd2807SJeff Garzik 	if (!ata_id_has_ncq(dev->id)) {
2269c6fd2807SJeff Garzik 		desc[0] = '\0';
2270388539f3SShaohua Li 		return 0;
2271c6fd2807SJeff Garzik 	}
227275683fe7STejun Heo 	if (dev->horkage & ATA_HORKAGE_NONCQ) {
22736919a0a6SAlan Cox 		snprintf(desc, desc_sz, "NCQ (not used)");
2274388539f3SShaohua Li 		return 0;
22756919a0a6SAlan Cox 	}
2276c6fd2807SJeff Garzik 	if (ap->flags & ATA_FLAG_NCQ) {
2277cca3974eSJeff Garzik 		hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE - 1);
2278c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_NCQ;
2279c6fd2807SJeff Garzik 	}
2280c6fd2807SJeff Garzik 
2281388539f3SShaohua Li 	if (!(dev->horkage & ATA_HORKAGE_BROKEN_FPDMA_AA) &&
2282388539f3SShaohua Li 		(ap->flags & ATA_FLAG_FPDMA_AA) &&
2283388539f3SShaohua Li 		ata_id_has_fpdma_aa(dev->id)) {
2284388539f3SShaohua Li 		err_mask = ata_dev_set_feature(dev, SETFEATURES_SATA_ENABLE,
2285388539f3SShaohua Li 			SATA_FPDMA_AA);
2286388539f3SShaohua Li 		if (err_mask) {
2287388539f3SShaohua Li 			ata_dev_printk(dev, KERN_ERR, "failed to enable AA"
2288388539f3SShaohua Li 				"(error_mask=0x%x)\n", err_mask);
2289388539f3SShaohua Li 			if (err_mask != AC_ERR_DEV) {
2290388539f3SShaohua Li 				dev->horkage |= ATA_HORKAGE_BROKEN_FPDMA_AA;
2291388539f3SShaohua Li 				return -EIO;
2292388539f3SShaohua Li 			}
2293388539f3SShaohua Li 		} else
2294388539f3SShaohua Li 			aa_desc = ", AA";
2295388539f3SShaohua Li 	}
2296388539f3SShaohua Li 
2297c6fd2807SJeff Garzik 	if (hdepth >= ddepth)
2298388539f3SShaohua Li 		snprintf(desc, desc_sz, "NCQ (depth %d)%s", ddepth, aa_desc);
2299c6fd2807SJeff Garzik 	else
2300388539f3SShaohua Li 		snprintf(desc, desc_sz, "NCQ (depth %d/%d)%s", hdepth,
2301388539f3SShaohua Li 			ddepth, aa_desc);
2302388539f3SShaohua Li 	return 0;
2303c6fd2807SJeff Garzik }
2304c6fd2807SJeff Garzik 
2305c6fd2807SJeff Garzik /**
2306c6fd2807SJeff Garzik  *	ata_dev_configure - Configure the specified ATA/ATAPI device
2307c6fd2807SJeff Garzik  *	@dev: Target device to configure
2308c6fd2807SJeff Garzik  *
2309c6fd2807SJeff Garzik  *	Configure @dev according to @dev->id.  Generic and low-level
2310c6fd2807SJeff Garzik  *	driver specific fixups are also applied.
2311c6fd2807SJeff Garzik  *
2312c6fd2807SJeff Garzik  *	LOCKING:
2313c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
2314c6fd2807SJeff Garzik  *
2315c6fd2807SJeff Garzik  *	RETURNS:
2316c6fd2807SJeff Garzik  *	0 on success, -errno otherwise
2317c6fd2807SJeff Garzik  */
2318efdaedc4STejun Heo int ata_dev_configure(struct ata_device *dev)
2319c6fd2807SJeff Garzik {
23209af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
23219af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
23226746544cSTejun Heo 	int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
2323c6fd2807SJeff Garzik 	const u16 *id = dev->id;
23247dc951aeSTejun Heo 	unsigned long xfer_mask;
2325b352e57dSAlan Cox 	char revbuf[7];		/* XYZ-99\0 */
23263f64f565SEric D. Mudama 	char fwrevbuf[ATA_ID_FW_REV_LEN+1];
23273f64f565SEric D. Mudama 	char modelbuf[ATA_ID_PROD_LEN+1];
2328c6fd2807SJeff Garzik 	int rc;
2329c6fd2807SJeff Garzik 
2330c6fd2807SJeff Garzik 	if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
233144877b4eSTejun Heo 		ata_dev_printk(dev, KERN_INFO, "%s: ENTER/EXIT -- nodev\n",
23327f5e4e8dSHarvey Harrison 			       __func__);
2333c6fd2807SJeff Garzik 		return 0;
2334c6fd2807SJeff Garzik 	}
2335c6fd2807SJeff Garzik 
2336c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
23377f5e4e8dSHarvey Harrison 		ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __func__);
2338c6fd2807SJeff Garzik 
233975683fe7STejun Heo 	/* set horkage */
234075683fe7STejun Heo 	dev->horkage |= ata_dev_blacklisted(dev);
234133267325STejun Heo 	ata_force_horkage(dev);
234275683fe7STejun Heo 
234350af2fa1STejun Heo 	if (dev->horkage & ATA_HORKAGE_DISABLE) {
234450af2fa1STejun Heo 		ata_dev_printk(dev, KERN_INFO,
234550af2fa1STejun Heo 			       "unsupported device, disabling\n");
234650af2fa1STejun Heo 		ata_dev_disable(dev);
234750af2fa1STejun Heo 		return 0;
234850af2fa1STejun Heo 	}
234950af2fa1STejun Heo 
23502486fa56STejun Heo 	if ((!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) &&
23512486fa56STejun Heo 	    dev->class == ATA_DEV_ATAPI) {
23522486fa56STejun Heo 		ata_dev_printk(dev, KERN_WARNING,
23532486fa56STejun Heo 			"WARNING: ATAPI is %s, device ignored.\n",
23542486fa56STejun Heo 			atapi_enabled ? "not supported with this driver"
23552486fa56STejun Heo 				      : "disabled");
23562486fa56STejun Heo 		ata_dev_disable(dev);
23572486fa56STejun Heo 		return 0;
23582486fa56STejun Heo 	}
23592486fa56STejun Heo 
23609062712fSTejun Heo 	rc = ata_do_link_spd_horkage(dev);
23619062712fSTejun Heo 	if (rc)
23629062712fSTejun Heo 		return rc;
23639062712fSTejun Heo 
23646746544cSTejun Heo 	/* let ACPI work its magic */
23656746544cSTejun Heo 	rc = ata_acpi_on_devcfg(dev);
23666746544cSTejun Heo 	if (rc)
23676746544cSTejun Heo 		return rc;
236808573a86SKristen Carlson Accardi 
236905027adcSTejun Heo 	/* massage HPA, do it early as it might change IDENTIFY data */
237005027adcSTejun Heo 	rc = ata_hpa_resize(dev);
237105027adcSTejun Heo 	if (rc)
237205027adcSTejun Heo 		return rc;
237305027adcSTejun Heo 
2374c6fd2807SJeff Garzik 	/* print device capabilities */
2375c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2376c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_DEBUG,
2377c6fd2807SJeff Garzik 			       "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
2378c6fd2807SJeff Garzik 			       "85:%04x 86:%04x 87:%04x 88:%04x\n",
23797f5e4e8dSHarvey Harrison 			       __func__,
2380c6fd2807SJeff Garzik 			       id[49], id[82], id[83], id[84],
2381c6fd2807SJeff Garzik 			       id[85], id[86], id[87], id[88]);
2382c6fd2807SJeff Garzik 
2383c6fd2807SJeff Garzik 	/* initialize to-be-configured parameters */
2384c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_CFG_MASK;
2385c6fd2807SJeff Garzik 	dev->max_sectors = 0;
2386c6fd2807SJeff Garzik 	dev->cdb_len = 0;
2387c6fd2807SJeff Garzik 	dev->n_sectors = 0;
2388c6fd2807SJeff Garzik 	dev->cylinders = 0;
2389c6fd2807SJeff Garzik 	dev->heads = 0;
2390c6fd2807SJeff Garzik 	dev->sectors = 0;
2391e18086d6SMark Lord 	dev->multi_count = 0;
2392c6fd2807SJeff Garzik 
2393c6fd2807SJeff Garzik 	/*
2394c6fd2807SJeff Garzik 	 * common ATA, ATAPI feature tests
2395c6fd2807SJeff Garzik 	 */
2396c6fd2807SJeff Garzik 
2397c6fd2807SJeff Garzik 	/* find max transfer mode; for printk only */
2398c6fd2807SJeff Garzik 	xfer_mask = ata_id_xfermask(id);
2399c6fd2807SJeff Garzik 
2400c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2401c6fd2807SJeff Garzik 		ata_dump_id(id);
2402c6fd2807SJeff Garzik 
2403ef143d57SAlbert Lee 	/* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
2404ef143d57SAlbert Lee 	ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
2405ef143d57SAlbert Lee 			sizeof(fwrevbuf));
2406ef143d57SAlbert Lee 
2407ef143d57SAlbert Lee 	ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
2408ef143d57SAlbert Lee 			sizeof(modelbuf));
2409ef143d57SAlbert Lee 
2410c6fd2807SJeff Garzik 	/* ATA-specific feature tests */
2411c6fd2807SJeff Garzik 	if (dev->class == ATA_DEV_ATA) {
2412b352e57dSAlan Cox 		if (ata_id_is_cfa(id)) {
241362afe5d7SSergei Shtylyov 			/* CPRM may make this media unusable */
241462afe5d7SSergei Shtylyov 			if (id[ATA_ID_CFA_KEY_MGMT] & 1)
241544877b4eSTejun Heo 				ata_dev_printk(dev, KERN_WARNING,
241644877b4eSTejun Heo 					       "supports DRM functions and may "
241744877b4eSTejun Heo 					       "not be fully accessable.\n");
2418b352e57dSAlan Cox 			snprintf(revbuf, 7, "CFA");
2419ae8d4ee7SAlan Cox 		} else {
2420b352e57dSAlan Cox 			snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
2421ae8d4ee7SAlan Cox 			/* Warn the user if the device has TPM extensions */
2422ae8d4ee7SAlan Cox 			if (ata_id_has_tpm(id))
2423ae8d4ee7SAlan Cox 				ata_dev_printk(dev, KERN_WARNING,
2424ae8d4ee7SAlan Cox 					       "supports DRM functions and may "
2425ae8d4ee7SAlan Cox 					       "not be fully accessable.\n");
2426ae8d4ee7SAlan Cox 		}
2427b352e57dSAlan Cox 
2428c6fd2807SJeff Garzik 		dev->n_sectors = ata_id_n_sectors(id);
2429c6fd2807SJeff Garzik 
2430e18086d6SMark Lord 		/* get current R/W Multiple count setting */
2431e18086d6SMark Lord 		if ((dev->id[47] >> 8) == 0x80 && (dev->id[59] & 0x100)) {
2432e18086d6SMark Lord 			unsigned int max = dev->id[47] & 0xff;
2433e18086d6SMark Lord 			unsigned int cnt = dev->id[59] & 0xff;
2434e18086d6SMark Lord 			/* only recognize/allow powers of two here */
2435e18086d6SMark Lord 			if (is_power_of_2(max) && is_power_of_2(cnt))
2436e18086d6SMark Lord 				if (cnt <= max)
2437e18086d6SMark Lord 					dev->multi_count = cnt;
2438e18086d6SMark Lord 		}
24393f64f565SEric D. Mudama 
2440c6fd2807SJeff Garzik 		if (ata_id_has_lba(id)) {
2441c6fd2807SJeff Garzik 			const char *lba_desc;
2442388539f3SShaohua Li 			char ncq_desc[24];
2443c6fd2807SJeff Garzik 
2444c6fd2807SJeff Garzik 			lba_desc = "LBA";
2445c6fd2807SJeff Garzik 			dev->flags |= ATA_DFLAG_LBA;
2446c6fd2807SJeff Garzik 			if (ata_id_has_lba48(id)) {
2447c6fd2807SJeff Garzik 				dev->flags |= ATA_DFLAG_LBA48;
2448c6fd2807SJeff Garzik 				lba_desc = "LBA48";
24496fc49adbSTejun Heo 
24506fc49adbSTejun Heo 				if (dev->n_sectors >= (1UL << 28) &&
24516fc49adbSTejun Heo 				    ata_id_has_flush_ext(id))
24526fc49adbSTejun Heo 					dev->flags |= ATA_DFLAG_FLUSH_EXT;
2453c6fd2807SJeff Garzik 			}
2454c6fd2807SJeff Garzik 
2455c6fd2807SJeff Garzik 			/* config NCQ */
2456388539f3SShaohua Li 			rc = ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
2457388539f3SShaohua Li 			if (rc)
2458388539f3SShaohua Li 				return rc;
2459c6fd2807SJeff Garzik 
2460c6fd2807SJeff Garzik 			/* print device info to dmesg */
24613f64f565SEric D. Mudama 			if (ata_msg_drv(ap) && print_info) {
24623f64f565SEric D. Mudama 				ata_dev_printk(dev, KERN_INFO,
24633f64f565SEric D. Mudama 					"%s: %s, %s, max %s\n",
24643f64f565SEric D. Mudama 					revbuf, modelbuf, fwrevbuf,
24653f64f565SEric D. Mudama 					ata_mode_string(xfer_mask));
24663f64f565SEric D. Mudama 				ata_dev_printk(dev, KERN_INFO,
24673f64f565SEric D. Mudama 					"%Lu sectors, multi %u: %s %s\n",
2468c6fd2807SJeff Garzik 					(unsigned long long)dev->n_sectors,
24693f64f565SEric D. Mudama 					dev->multi_count, lba_desc, ncq_desc);
24703f64f565SEric D. Mudama 			}
2471c6fd2807SJeff Garzik 		} else {
2472c6fd2807SJeff Garzik 			/* CHS */
2473c6fd2807SJeff Garzik 
2474c6fd2807SJeff Garzik 			/* Default translation */
2475c6fd2807SJeff Garzik 			dev->cylinders	= id[1];
2476c6fd2807SJeff Garzik 			dev->heads	= id[3];
2477c6fd2807SJeff Garzik 			dev->sectors	= id[6];
2478c6fd2807SJeff Garzik 
2479c6fd2807SJeff Garzik 			if (ata_id_current_chs_valid(id)) {
2480c6fd2807SJeff Garzik 				/* Current CHS translation is valid. */
2481c6fd2807SJeff Garzik 				dev->cylinders = id[54];
2482c6fd2807SJeff Garzik 				dev->heads     = id[55];
2483c6fd2807SJeff Garzik 				dev->sectors   = id[56];
2484c6fd2807SJeff Garzik 			}
2485c6fd2807SJeff Garzik 
2486c6fd2807SJeff Garzik 			/* print device info to dmesg */
24873f64f565SEric D. Mudama 			if (ata_msg_drv(ap) && print_info) {
2488c6fd2807SJeff Garzik 				ata_dev_printk(dev, KERN_INFO,
24893f64f565SEric D. Mudama 					"%s: %s, %s, max %s\n",
24903f64f565SEric D. Mudama 					revbuf,	modelbuf, fwrevbuf,
24913f64f565SEric D. Mudama 					ata_mode_string(xfer_mask));
24923f64f565SEric D. Mudama 				ata_dev_printk(dev, KERN_INFO,
24933f64f565SEric D. Mudama 					"%Lu sectors, multi %u, CHS %u/%u/%u\n",
24943f64f565SEric D. Mudama 					(unsigned long long)dev->n_sectors,
24953f64f565SEric D. Mudama 					dev->multi_count, dev->cylinders,
24963f64f565SEric D. Mudama 					dev->heads, dev->sectors);
24973f64f565SEric D. Mudama 			}
2498c6fd2807SJeff Garzik 		}
2499c6fd2807SJeff Garzik 
2500c6fd2807SJeff Garzik 		dev->cdb_len = 16;
2501c6fd2807SJeff Garzik 	}
2502c6fd2807SJeff Garzik 
2503c6fd2807SJeff Garzik 	/* ATAPI-specific feature tests */
2504c6fd2807SJeff Garzik 	else if (dev->class == ATA_DEV_ATAPI) {
2505854c73a2STejun Heo 		const char *cdb_intr_string = "";
2506854c73a2STejun Heo 		const char *atapi_an_string = "";
250791163006STejun Heo 		const char *dma_dir_string = "";
25087d77b247STejun Heo 		u32 sntf;
2509c6fd2807SJeff Garzik 
2510c6fd2807SJeff Garzik 		rc = atapi_cdb_len(id);
2511c6fd2807SJeff Garzik 		if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
2512c6fd2807SJeff Garzik 			if (ata_msg_warn(ap))
2513c6fd2807SJeff Garzik 				ata_dev_printk(dev, KERN_WARNING,
2514c6fd2807SJeff Garzik 					       "unsupported CDB len\n");
2515c6fd2807SJeff Garzik 			rc = -EINVAL;
2516c6fd2807SJeff Garzik 			goto err_out_nosup;
2517c6fd2807SJeff Garzik 		}
2518c6fd2807SJeff Garzik 		dev->cdb_len = (unsigned int) rc;
2519c6fd2807SJeff Garzik 
25207d77b247STejun Heo 		/* Enable ATAPI AN if both the host and device have
25217d77b247STejun Heo 		 * the support.  If PMP is attached, SNTF is required
25227d77b247STejun Heo 		 * to enable ATAPI AN to discern between PHY status
25237d77b247STejun Heo 		 * changed notifications and ATAPI ANs.
25249f45cbd3SKristen Carlson Accardi 		 */
2525e7ecd435STejun Heo 		if (atapi_an &&
2526e7ecd435STejun Heo 		    (ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
2527071f44b1STejun Heo 		    (!sata_pmp_attached(ap) ||
25287d77b247STejun Heo 		     sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
2529854c73a2STejun Heo 			unsigned int err_mask;
2530854c73a2STejun Heo 
25319f45cbd3SKristen Carlson Accardi 			/* issue SET feature command to turn this on */
2532218f3d30SJeff Garzik 			err_mask = ata_dev_set_feature(dev,
2533218f3d30SJeff Garzik 					SETFEATURES_SATA_ENABLE, SATA_AN);
2534854c73a2STejun Heo 			if (err_mask)
25359f45cbd3SKristen Carlson Accardi 				ata_dev_printk(dev, KERN_ERR,
2536854c73a2STejun Heo 					"failed to enable ATAPI AN "
2537854c73a2STejun Heo 					"(err_mask=0x%x)\n", err_mask);
2538854c73a2STejun Heo 			else {
25399f45cbd3SKristen Carlson Accardi 				dev->flags |= ATA_DFLAG_AN;
2540854c73a2STejun Heo 				atapi_an_string = ", ATAPI AN";
2541854c73a2STejun Heo 			}
25429f45cbd3SKristen Carlson Accardi 		}
25439f45cbd3SKristen Carlson Accardi 
2544c6fd2807SJeff Garzik 		if (ata_id_cdb_intr(dev->id)) {
2545c6fd2807SJeff Garzik 			dev->flags |= ATA_DFLAG_CDB_INTR;
2546c6fd2807SJeff Garzik 			cdb_intr_string = ", CDB intr";
2547c6fd2807SJeff Garzik 		}
2548c6fd2807SJeff Garzik 
254991163006STejun Heo 		if (atapi_dmadir || atapi_id_dmadir(dev->id)) {
255091163006STejun Heo 			dev->flags |= ATA_DFLAG_DMADIR;
255191163006STejun Heo 			dma_dir_string = ", DMADIR";
255291163006STejun Heo 		}
255391163006STejun Heo 
2554c6fd2807SJeff Garzik 		/* print device info to dmesg */
2555c6fd2807SJeff Garzik 		if (ata_msg_drv(ap) && print_info)
2556ef143d57SAlbert Lee 			ata_dev_printk(dev, KERN_INFO,
255791163006STejun Heo 				       "ATAPI: %s, %s, max %s%s%s%s\n",
2558ef143d57SAlbert Lee 				       modelbuf, fwrevbuf,
2559c6fd2807SJeff Garzik 				       ata_mode_string(xfer_mask),
256091163006STejun Heo 				       cdb_intr_string, atapi_an_string,
256191163006STejun Heo 				       dma_dir_string);
2562c6fd2807SJeff Garzik 	}
2563c6fd2807SJeff Garzik 
2564914ed354STejun Heo 	/* determine max_sectors */
2565914ed354STejun Heo 	dev->max_sectors = ATA_MAX_SECTORS;
2566914ed354STejun Heo 	if (dev->flags & ATA_DFLAG_LBA48)
2567914ed354STejun Heo 		dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2568914ed354STejun Heo 
2569ca77329fSKristen Carlson Accardi 	if (!(dev->horkage & ATA_HORKAGE_IPM)) {
2570ca77329fSKristen Carlson Accardi 		if (ata_id_has_hipm(dev->id))
2571ca77329fSKristen Carlson Accardi 			dev->flags |= ATA_DFLAG_HIPM;
2572ca77329fSKristen Carlson Accardi 		if (ata_id_has_dipm(dev->id))
2573ca77329fSKristen Carlson Accardi 			dev->flags |= ATA_DFLAG_DIPM;
2574ca77329fSKristen Carlson Accardi 	}
2575ca77329fSKristen Carlson Accardi 
2576c5038fc0SAlan Cox 	/* Limit PATA drive on SATA cable bridge transfers to udma5,
2577c5038fc0SAlan Cox 	   200 sectors */
2578c6fd2807SJeff Garzik 	if (ata_dev_knobble(dev)) {
2579c6fd2807SJeff Garzik 		if (ata_msg_drv(ap) && print_info)
2580c6fd2807SJeff Garzik 			ata_dev_printk(dev, KERN_INFO,
2581c6fd2807SJeff Garzik 				       "applying bridge limits\n");
2582c6fd2807SJeff Garzik 		dev->udma_mask &= ATA_UDMA5;
2583c6fd2807SJeff Garzik 		dev->max_sectors = ATA_MAX_SECTORS;
2584c6fd2807SJeff Garzik 	}
2585c6fd2807SJeff Garzik 
2586f8d8e579STony Battersby 	if ((dev->class == ATA_DEV_ATAPI) &&
2587f442cd86SAlbert Lee 	    (atapi_command_packet_set(id) == TYPE_TAPE)) {
2588f8d8e579STony Battersby 		dev->max_sectors = ATA_MAX_SECTORS_TAPE;
2589f442cd86SAlbert Lee 		dev->horkage |= ATA_HORKAGE_STUCK_ERR;
2590f442cd86SAlbert Lee 	}
2591f8d8e579STony Battersby 
259275683fe7STejun Heo 	if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
259303ec52deSTejun Heo 		dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
259403ec52deSTejun Heo 					 dev->max_sectors);
259518d6e9d5SAlbert Lee 
2596ca77329fSKristen Carlson Accardi 	if (ata_dev_blacklisted(dev) & ATA_HORKAGE_IPM) {
2597ca77329fSKristen Carlson Accardi 		dev->horkage |= ATA_HORKAGE_IPM;
2598ca77329fSKristen Carlson Accardi 
2599ca77329fSKristen Carlson Accardi 		/* reset link pm_policy for this port to no pm */
2600ca77329fSKristen Carlson Accardi 		ap->pm_policy = MAX_PERFORMANCE;
2601ca77329fSKristen Carlson Accardi 	}
2602ca77329fSKristen Carlson Accardi 
2603c6fd2807SJeff Garzik 	if (ap->ops->dev_config)
2604cd0d3bbcSAlan 		ap->ops->dev_config(dev);
2605c6fd2807SJeff Garzik 
2606c5038fc0SAlan Cox 	if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2607c5038fc0SAlan Cox 		/* Let the user know. We don't want to disallow opens for
2608c5038fc0SAlan Cox 		   rescue purposes, or in case the vendor is just a blithering
2609c5038fc0SAlan Cox 		   idiot. Do this after the dev_config call as some controllers
2610c5038fc0SAlan Cox 		   with buggy firmware may want to avoid reporting false device
2611c5038fc0SAlan Cox 		   bugs */
2612c5038fc0SAlan Cox 
2613c5038fc0SAlan Cox 		if (print_info) {
2614c5038fc0SAlan Cox 			ata_dev_printk(dev, KERN_WARNING,
2615c5038fc0SAlan Cox "Drive reports diagnostics failure. This may indicate a drive\n");
2616c5038fc0SAlan Cox 			ata_dev_printk(dev, KERN_WARNING,
2617c5038fc0SAlan Cox "fault or invalid emulation. Contact drive vendor for information.\n");
2618c5038fc0SAlan Cox 		}
2619c5038fc0SAlan Cox 	}
2620c5038fc0SAlan Cox 
2621ac70a964STejun Heo 	if ((dev->horkage & ATA_HORKAGE_FIRMWARE_WARN) && print_info) {
2622ac70a964STejun Heo 		ata_dev_printk(dev, KERN_WARNING, "WARNING: device requires "
2623ac70a964STejun Heo 			       "firmware update to be fully functional.\n");
2624ac70a964STejun Heo 		ata_dev_printk(dev, KERN_WARNING, "         contact the vendor "
2625ac70a964STejun Heo 			       "or visit http://ata.wiki.kernel.org.\n");
2626ac70a964STejun Heo 	}
2627ac70a964STejun Heo 
2628c6fd2807SJeff Garzik 	return 0;
2629c6fd2807SJeff Garzik 
2630c6fd2807SJeff Garzik err_out_nosup:
2631c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2632c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_DEBUG,
26337f5e4e8dSHarvey Harrison 			       "%s: EXIT, err\n", __func__);
2634c6fd2807SJeff Garzik 	return rc;
2635c6fd2807SJeff Garzik }
2636c6fd2807SJeff Garzik 
2637c6fd2807SJeff Garzik /**
26382e41e8e6SAlan Cox  *	ata_cable_40wire	-	return 40 wire cable type
2639be0d18dfSAlan Cox  *	@ap: port
2640be0d18dfSAlan Cox  *
26412e41e8e6SAlan Cox  *	Helper method for drivers which want to hardwire 40 wire cable
2642be0d18dfSAlan Cox  *	detection.
2643be0d18dfSAlan Cox  */
2644be0d18dfSAlan Cox 
2645be0d18dfSAlan Cox int ata_cable_40wire(struct ata_port *ap)
2646be0d18dfSAlan Cox {
2647be0d18dfSAlan Cox 	return ATA_CBL_PATA40;
2648be0d18dfSAlan Cox }
2649be0d18dfSAlan Cox 
2650be0d18dfSAlan Cox /**
26512e41e8e6SAlan Cox  *	ata_cable_80wire	-	return 80 wire cable type
2652be0d18dfSAlan Cox  *	@ap: port
2653be0d18dfSAlan Cox  *
26542e41e8e6SAlan Cox  *	Helper method for drivers which want to hardwire 80 wire cable
2655be0d18dfSAlan Cox  *	detection.
2656be0d18dfSAlan Cox  */
2657be0d18dfSAlan Cox 
2658be0d18dfSAlan Cox int ata_cable_80wire(struct ata_port *ap)
2659be0d18dfSAlan Cox {
2660be0d18dfSAlan Cox 	return ATA_CBL_PATA80;
2661be0d18dfSAlan Cox }
2662be0d18dfSAlan Cox 
2663be0d18dfSAlan Cox /**
2664be0d18dfSAlan Cox  *	ata_cable_unknown	-	return unknown PATA cable.
2665be0d18dfSAlan Cox  *	@ap: port
2666be0d18dfSAlan Cox  *
2667be0d18dfSAlan Cox  *	Helper method for drivers which have no PATA cable detection.
2668be0d18dfSAlan Cox  */
2669be0d18dfSAlan Cox 
2670be0d18dfSAlan Cox int ata_cable_unknown(struct ata_port *ap)
2671be0d18dfSAlan Cox {
2672be0d18dfSAlan Cox 	return ATA_CBL_PATA_UNK;
2673be0d18dfSAlan Cox }
2674be0d18dfSAlan Cox 
2675be0d18dfSAlan Cox /**
2676c88f90c3STejun Heo  *	ata_cable_ignore	-	return ignored PATA cable.
2677c88f90c3STejun Heo  *	@ap: port
2678c88f90c3STejun Heo  *
2679c88f90c3STejun Heo  *	Helper method for drivers which don't use cable type to limit
2680c88f90c3STejun Heo  *	transfer mode.
2681c88f90c3STejun Heo  */
2682c88f90c3STejun Heo int ata_cable_ignore(struct ata_port *ap)
2683c88f90c3STejun Heo {
2684c88f90c3STejun Heo 	return ATA_CBL_PATA_IGN;
2685c88f90c3STejun Heo }
2686c88f90c3STejun Heo 
2687c88f90c3STejun Heo /**
2688be0d18dfSAlan Cox  *	ata_cable_sata	-	return SATA cable type
2689be0d18dfSAlan Cox  *	@ap: port
2690be0d18dfSAlan Cox  *
2691be0d18dfSAlan Cox  *	Helper method for drivers which have SATA cables
2692be0d18dfSAlan Cox  */
2693be0d18dfSAlan Cox 
2694be0d18dfSAlan Cox int ata_cable_sata(struct ata_port *ap)
2695be0d18dfSAlan Cox {
2696be0d18dfSAlan Cox 	return ATA_CBL_SATA;
2697be0d18dfSAlan Cox }
2698be0d18dfSAlan Cox 
2699be0d18dfSAlan Cox /**
2700c6fd2807SJeff Garzik  *	ata_bus_probe - Reset and probe ATA bus
2701c6fd2807SJeff Garzik  *	@ap: Bus to probe
2702c6fd2807SJeff Garzik  *
2703c6fd2807SJeff Garzik  *	Master ATA bus probing function.  Initiates a hardware-dependent
2704c6fd2807SJeff Garzik  *	bus reset, then attempts to identify any devices found on
2705c6fd2807SJeff Garzik  *	the bus.
2706c6fd2807SJeff Garzik  *
2707c6fd2807SJeff Garzik  *	LOCKING:
2708c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
2709c6fd2807SJeff Garzik  *
2710c6fd2807SJeff Garzik  *	RETURNS:
2711c6fd2807SJeff Garzik  *	Zero on success, negative errno otherwise.
2712c6fd2807SJeff Garzik  */
2713c6fd2807SJeff Garzik 
2714c6fd2807SJeff Garzik int ata_bus_probe(struct ata_port *ap)
2715c6fd2807SJeff Garzik {
2716c6fd2807SJeff Garzik 	unsigned int classes[ATA_MAX_DEVICES];
2717c6fd2807SJeff Garzik 	int tries[ATA_MAX_DEVICES];
2718f58229f8STejun Heo 	int rc;
2719c6fd2807SJeff Garzik 	struct ata_device *dev;
2720c6fd2807SJeff Garzik 
27211eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL)
2722f58229f8STejun Heo 		tries[dev->devno] = ATA_PROBE_MAX_TRIES;
2723c6fd2807SJeff Garzik 
2724c6fd2807SJeff Garzik  retry:
27251eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL) {
2726cdeab114STejun Heo 		/* If we issue an SRST then an ATA drive (not ATAPI)
2727cdeab114STejun Heo 		 * may change configuration and be in PIO0 timing. If
2728cdeab114STejun Heo 		 * we do a hard reset (or are coming from power on)
2729cdeab114STejun Heo 		 * this is true for ATA or ATAPI. Until we've set a
2730cdeab114STejun Heo 		 * suitable controller mode we should not touch the
2731cdeab114STejun Heo 		 * bus as we may be talking too fast.
2732cdeab114STejun Heo 		 */
2733cdeab114STejun Heo 		dev->pio_mode = XFER_PIO_0;
2734cdeab114STejun Heo 
2735cdeab114STejun Heo 		/* If the controller has a pio mode setup function
2736cdeab114STejun Heo 		 * then use it to set the chipset to rights. Don't
2737cdeab114STejun Heo 		 * touch the DMA setup as that will be dealt with when
2738cdeab114STejun Heo 		 * configuring devices.
2739cdeab114STejun Heo 		 */
2740cdeab114STejun Heo 		if (ap->ops->set_piomode)
2741cdeab114STejun Heo 			ap->ops->set_piomode(ap, dev);
2742cdeab114STejun Heo 	}
2743cdeab114STejun Heo 
2744c6fd2807SJeff Garzik 	/* reset and determine device classes */
2745c6fd2807SJeff Garzik 	ap->ops->phy_reset(ap);
2746c6fd2807SJeff Garzik 
27471eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL) {
27483e4ec344STejun Heo 		if (dev->class != ATA_DEV_UNKNOWN)
2749c6fd2807SJeff Garzik 			classes[dev->devno] = dev->class;
2750c6fd2807SJeff Garzik 		else
2751c6fd2807SJeff Garzik 			classes[dev->devno] = ATA_DEV_NONE;
2752c6fd2807SJeff Garzik 
2753c6fd2807SJeff Garzik 		dev->class = ATA_DEV_UNKNOWN;
2754c6fd2807SJeff Garzik 	}
2755c6fd2807SJeff Garzik 
2756f31f0cc2SJeff Garzik 	/* read IDENTIFY page and configure devices. We have to do the identify
2757f31f0cc2SJeff Garzik 	   specific sequence bass-ackwards so that PDIAG- is released by
2758f31f0cc2SJeff Garzik 	   the slave device */
2759f31f0cc2SJeff Garzik 
27601eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL_REVERSE) {
2761f58229f8STejun Heo 		if (tries[dev->devno])
2762f58229f8STejun Heo 			dev->class = classes[dev->devno];
2763c6fd2807SJeff Garzik 
2764c6fd2807SJeff Garzik 		if (!ata_dev_enabled(dev))
2765c6fd2807SJeff Garzik 			continue;
2766c6fd2807SJeff Garzik 
2767bff04647STejun Heo 		rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2768bff04647STejun Heo 				     dev->id);
2769c6fd2807SJeff Garzik 		if (rc)
2770c6fd2807SJeff Garzik 			goto fail;
2771f31f0cc2SJeff Garzik 	}
2772f31f0cc2SJeff Garzik 
2773be0d18dfSAlan Cox 	/* Now ask for the cable type as PDIAG- should have been released */
2774be0d18dfSAlan Cox 	if (ap->ops->cable_detect)
2775be0d18dfSAlan Cox 		ap->cbl = ap->ops->cable_detect(ap);
2776be0d18dfSAlan Cox 
27771eca4365STejun Heo 	/* We may have SATA bridge glue hiding here irrespective of
27781eca4365STejun Heo 	 * the reported cable types and sensed types.  When SATA
27791eca4365STejun Heo 	 * drives indicate we have a bridge, we don't know which end
27801eca4365STejun Heo 	 * of the link the bridge is which is a problem.
27811eca4365STejun Heo 	 */
27821eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED)
2783614fe29bSAlan Cox 		if (ata_id_is_sata(dev->id))
2784614fe29bSAlan Cox 			ap->cbl = ATA_CBL_SATA;
2785614fe29bSAlan Cox 
2786f31f0cc2SJeff Garzik 	/* After the identify sequence we can now set up the devices. We do
2787f31f0cc2SJeff Garzik 	   this in the normal order so that the user doesn't get confused */
2788f31f0cc2SJeff Garzik 
27891eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED) {
27909af5c9c9STejun Heo 		ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
2791efdaedc4STejun Heo 		rc = ata_dev_configure(dev);
27929af5c9c9STejun Heo 		ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
2793c6fd2807SJeff Garzik 		if (rc)
2794c6fd2807SJeff Garzik 			goto fail;
2795c6fd2807SJeff Garzik 	}
2796c6fd2807SJeff Garzik 
2797c6fd2807SJeff Garzik 	/* configure transfer mode */
27980260731fSTejun Heo 	rc = ata_set_mode(&ap->link, &dev);
27994ae72a1eSTejun Heo 	if (rc)
2800c6fd2807SJeff Garzik 		goto fail;
2801c6fd2807SJeff Garzik 
28021eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED)
2803c6fd2807SJeff Garzik 		return 0;
2804c6fd2807SJeff Garzik 
2805c6fd2807SJeff Garzik 	return -ENODEV;
2806c6fd2807SJeff Garzik 
2807c6fd2807SJeff Garzik  fail:
28084ae72a1eSTejun Heo 	tries[dev->devno]--;
28094ae72a1eSTejun Heo 
2810c6fd2807SJeff Garzik 	switch (rc) {
2811c6fd2807SJeff Garzik 	case -EINVAL:
28124ae72a1eSTejun Heo 		/* eeek, something went very wrong, give up */
2813c6fd2807SJeff Garzik 		tries[dev->devno] = 0;
2814c6fd2807SJeff Garzik 		break;
28154ae72a1eSTejun Heo 
28164ae72a1eSTejun Heo 	case -ENODEV:
28174ae72a1eSTejun Heo 		/* give it just one more chance */
28184ae72a1eSTejun Heo 		tries[dev->devno] = min(tries[dev->devno], 1);
2819c6fd2807SJeff Garzik 	case -EIO:
28204ae72a1eSTejun Heo 		if (tries[dev->devno] == 1) {
28214ae72a1eSTejun Heo 			/* This is the last chance, better to slow
28224ae72a1eSTejun Heo 			 * down than lose it.
28234ae72a1eSTejun Heo 			 */
2824a07d499bSTejun Heo 			sata_down_spd_limit(&ap->link, 0);
28254ae72a1eSTejun Heo 			ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
28264ae72a1eSTejun Heo 		}
2827c6fd2807SJeff Garzik 	}
2828c6fd2807SJeff Garzik 
28294ae72a1eSTejun Heo 	if (!tries[dev->devno])
2830c6fd2807SJeff Garzik 		ata_dev_disable(dev);
2831c6fd2807SJeff Garzik 
2832c6fd2807SJeff Garzik 	goto retry;
2833c6fd2807SJeff Garzik }
2834c6fd2807SJeff Garzik 
2835c6fd2807SJeff Garzik /**
2836c6fd2807SJeff Garzik  *	sata_print_link_status - Print SATA link status
2837936fd732STejun Heo  *	@link: SATA link to printk link status about
2838c6fd2807SJeff Garzik  *
2839c6fd2807SJeff Garzik  *	This function prints link speed and status of a SATA link.
2840c6fd2807SJeff Garzik  *
2841c6fd2807SJeff Garzik  *	LOCKING:
2842c6fd2807SJeff Garzik  *	None.
2843c6fd2807SJeff Garzik  */
28446bdb4fc9SAdrian Bunk static void sata_print_link_status(struct ata_link *link)
2845c6fd2807SJeff Garzik {
2846c6fd2807SJeff Garzik 	u32 sstatus, scontrol, tmp;
2847c6fd2807SJeff Garzik 
2848936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus))
2849c6fd2807SJeff Garzik 		return;
2850936fd732STejun Heo 	sata_scr_read(link, SCR_CONTROL, &scontrol);
2851c6fd2807SJeff Garzik 
2852b1c72916STejun Heo 	if (ata_phys_link_online(link)) {
2853c6fd2807SJeff Garzik 		tmp = (sstatus >> 4) & 0xf;
2854936fd732STejun Heo 		ata_link_printk(link, KERN_INFO,
2855c6fd2807SJeff Garzik 				"SATA link up %s (SStatus %X SControl %X)\n",
2856c6fd2807SJeff Garzik 				sata_spd_string(tmp), sstatus, scontrol);
2857c6fd2807SJeff Garzik 	} else {
2858936fd732STejun Heo 		ata_link_printk(link, KERN_INFO,
2859c6fd2807SJeff Garzik 				"SATA link down (SStatus %X SControl %X)\n",
2860c6fd2807SJeff Garzik 				sstatus, scontrol);
2861c6fd2807SJeff Garzik 	}
2862c6fd2807SJeff Garzik }
2863c6fd2807SJeff Garzik 
2864c6fd2807SJeff Garzik /**
2865c6fd2807SJeff Garzik  *	ata_dev_pair		-	return other device on cable
2866c6fd2807SJeff Garzik  *	@adev: device
2867c6fd2807SJeff Garzik  *
2868c6fd2807SJeff Garzik  *	Obtain the other device on the same cable, or if none is
2869c6fd2807SJeff Garzik  *	present NULL is returned
2870c6fd2807SJeff Garzik  */
2871c6fd2807SJeff Garzik 
2872c6fd2807SJeff Garzik struct ata_device *ata_dev_pair(struct ata_device *adev)
2873c6fd2807SJeff Garzik {
28749af5c9c9STejun Heo 	struct ata_link *link = adev->link;
28759af5c9c9STejun Heo 	struct ata_device *pair = &link->device[1 - adev->devno];
2876c6fd2807SJeff Garzik 	if (!ata_dev_enabled(pair))
2877c6fd2807SJeff Garzik 		return NULL;
2878c6fd2807SJeff Garzik 	return pair;
2879c6fd2807SJeff Garzik }
2880c6fd2807SJeff Garzik 
2881c6fd2807SJeff Garzik /**
2882c6fd2807SJeff Garzik  *	sata_down_spd_limit - adjust SATA spd limit downward
2883936fd732STejun Heo  *	@link: Link to adjust SATA spd limit for
2884a07d499bSTejun Heo  *	@spd_limit: Additional limit
2885c6fd2807SJeff Garzik  *
2886936fd732STejun Heo  *	Adjust SATA spd limit of @link downward.  Note that this
2887c6fd2807SJeff Garzik  *	function only adjusts the limit.  The change must be applied
2888c6fd2807SJeff Garzik  *	using sata_set_spd().
2889c6fd2807SJeff Garzik  *
2890a07d499bSTejun Heo  *	If @spd_limit is non-zero, the speed is limited to equal to or
2891a07d499bSTejun Heo  *	lower than @spd_limit if such speed is supported.  If
2892a07d499bSTejun Heo  *	@spd_limit is slower than any supported speed, only the lowest
2893a07d499bSTejun Heo  *	supported speed is allowed.
2894a07d499bSTejun Heo  *
2895c6fd2807SJeff Garzik  *	LOCKING:
2896c6fd2807SJeff Garzik  *	Inherited from caller.
2897c6fd2807SJeff Garzik  *
2898c6fd2807SJeff Garzik  *	RETURNS:
2899c6fd2807SJeff Garzik  *	0 on success, negative errno on failure
2900c6fd2807SJeff Garzik  */
2901a07d499bSTejun Heo int sata_down_spd_limit(struct ata_link *link, u32 spd_limit)
2902c6fd2807SJeff Garzik {
2903c6fd2807SJeff Garzik 	u32 sstatus, spd, mask;
2904a07d499bSTejun Heo 	int rc, bit;
2905c6fd2807SJeff Garzik 
2906936fd732STejun Heo 	if (!sata_scr_valid(link))
2907008a7896STejun Heo 		return -EOPNOTSUPP;
2908008a7896STejun Heo 
2909008a7896STejun Heo 	/* If SCR can be read, use it to determine the current SPD.
2910936fd732STejun Heo 	 * If not, use cached value in link->sata_spd.
2911008a7896STejun Heo 	 */
2912936fd732STejun Heo 	rc = sata_scr_read(link, SCR_STATUS, &sstatus);
29139913ff8aSTejun Heo 	if (rc == 0 && ata_sstatus_online(sstatus))
2914008a7896STejun Heo 		spd = (sstatus >> 4) & 0xf;
2915008a7896STejun Heo 	else
2916936fd732STejun Heo 		spd = link->sata_spd;
2917c6fd2807SJeff Garzik 
2918936fd732STejun Heo 	mask = link->sata_spd_limit;
2919c6fd2807SJeff Garzik 	if (mask <= 1)
2920c6fd2807SJeff Garzik 		return -EINVAL;
2921008a7896STejun Heo 
2922008a7896STejun Heo 	/* unconditionally mask off the highest bit */
2923a07d499bSTejun Heo 	bit = fls(mask) - 1;
2924a07d499bSTejun Heo 	mask &= ~(1 << bit);
2925c6fd2807SJeff Garzik 
2926008a7896STejun Heo 	/* Mask off all speeds higher than or equal to the current
2927008a7896STejun Heo 	 * one.  Force 1.5Gbps if current SPD is not available.
2928008a7896STejun Heo 	 */
2929008a7896STejun Heo 	if (spd > 1)
2930008a7896STejun Heo 		mask &= (1 << (spd - 1)) - 1;
2931008a7896STejun Heo 	else
2932008a7896STejun Heo 		mask &= 1;
2933008a7896STejun Heo 
2934008a7896STejun Heo 	/* were we already at the bottom? */
2935c6fd2807SJeff Garzik 	if (!mask)
2936c6fd2807SJeff Garzik 		return -EINVAL;
2937c6fd2807SJeff Garzik 
2938a07d499bSTejun Heo 	if (spd_limit) {
2939a07d499bSTejun Heo 		if (mask & ((1 << spd_limit) - 1))
2940a07d499bSTejun Heo 			mask &= (1 << spd_limit) - 1;
2941a07d499bSTejun Heo 		else {
2942a07d499bSTejun Heo 			bit = ffs(mask) - 1;
2943a07d499bSTejun Heo 			mask = 1 << bit;
2944a07d499bSTejun Heo 		}
2945a07d499bSTejun Heo 	}
2946a07d499bSTejun Heo 
2947936fd732STejun Heo 	link->sata_spd_limit = mask;
2948c6fd2807SJeff Garzik 
2949936fd732STejun Heo 	ata_link_printk(link, KERN_WARNING, "limiting SATA link speed to %s\n",
2950c6fd2807SJeff Garzik 			sata_spd_string(fls(mask)));
2951c6fd2807SJeff Garzik 
2952c6fd2807SJeff Garzik 	return 0;
2953c6fd2807SJeff Garzik }
2954c6fd2807SJeff Garzik 
2955936fd732STejun Heo static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
2956c6fd2807SJeff Garzik {
29575270222fSTejun Heo 	struct ata_link *host_link = &link->ap->link;
29585270222fSTejun Heo 	u32 limit, target, spd;
2959c6fd2807SJeff Garzik 
29605270222fSTejun Heo 	limit = link->sata_spd_limit;
29615270222fSTejun Heo 
29625270222fSTejun Heo 	/* Don't configure downstream link faster than upstream link.
29635270222fSTejun Heo 	 * It doesn't speed up anything and some PMPs choke on such
29645270222fSTejun Heo 	 * configuration.
29655270222fSTejun Heo 	 */
29665270222fSTejun Heo 	if (!ata_is_host_link(link) && host_link->sata_spd)
29675270222fSTejun Heo 		limit &= (1 << host_link->sata_spd) - 1;
29685270222fSTejun Heo 
29695270222fSTejun Heo 	if (limit == UINT_MAX)
29705270222fSTejun Heo 		target = 0;
2971c6fd2807SJeff Garzik 	else
29725270222fSTejun Heo 		target = fls(limit);
2973c6fd2807SJeff Garzik 
2974c6fd2807SJeff Garzik 	spd = (*scontrol >> 4) & 0xf;
29755270222fSTejun Heo 	*scontrol = (*scontrol & ~0xf0) | ((target & 0xf) << 4);
2976c6fd2807SJeff Garzik 
29775270222fSTejun Heo 	return spd != target;
2978c6fd2807SJeff Garzik }
2979c6fd2807SJeff Garzik 
2980c6fd2807SJeff Garzik /**
2981c6fd2807SJeff Garzik  *	sata_set_spd_needed - is SATA spd configuration needed
2982936fd732STejun Heo  *	@link: Link in question
2983c6fd2807SJeff Garzik  *
2984c6fd2807SJeff Garzik  *	Test whether the spd limit in SControl matches
2985936fd732STejun Heo  *	@link->sata_spd_limit.  This function is used to determine
2986c6fd2807SJeff Garzik  *	whether hardreset is necessary to apply SATA spd
2987c6fd2807SJeff Garzik  *	configuration.
2988c6fd2807SJeff Garzik  *
2989c6fd2807SJeff Garzik  *	LOCKING:
2990c6fd2807SJeff Garzik  *	Inherited from caller.
2991c6fd2807SJeff Garzik  *
2992c6fd2807SJeff Garzik  *	RETURNS:
2993c6fd2807SJeff Garzik  *	1 if SATA spd configuration is needed, 0 otherwise.
2994c6fd2807SJeff Garzik  */
29951dc55e87SAdrian Bunk static int sata_set_spd_needed(struct ata_link *link)
2996c6fd2807SJeff Garzik {
2997c6fd2807SJeff Garzik 	u32 scontrol;
2998c6fd2807SJeff Garzik 
2999936fd732STejun Heo 	if (sata_scr_read(link, SCR_CONTROL, &scontrol))
3000db64bcf3STejun Heo 		return 1;
3001c6fd2807SJeff Garzik 
3002936fd732STejun Heo 	return __sata_set_spd_needed(link, &scontrol);
3003c6fd2807SJeff Garzik }
3004c6fd2807SJeff Garzik 
3005c6fd2807SJeff Garzik /**
3006c6fd2807SJeff Garzik  *	sata_set_spd - set SATA spd according to spd limit
3007936fd732STejun Heo  *	@link: Link to set SATA spd for
3008c6fd2807SJeff Garzik  *
3009936fd732STejun Heo  *	Set SATA spd of @link according to sata_spd_limit.
3010c6fd2807SJeff Garzik  *
3011c6fd2807SJeff Garzik  *	LOCKING:
3012c6fd2807SJeff Garzik  *	Inherited from caller.
3013c6fd2807SJeff Garzik  *
3014c6fd2807SJeff Garzik  *	RETURNS:
3015c6fd2807SJeff Garzik  *	0 if spd doesn't need to be changed, 1 if spd has been
3016c6fd2807SJeff Garzik  *	changed.  Negative errno if SCR registers are inaccessible.
3017c6fd2807SJeff Garzik  */
3018936fd732STejun Heo int sata_set_spd(struct ata_link *link)
3019c6fd2807SJeff Garzik {
3020c6fd2807SJeff Garzik 	u32 scontrol;
3021c6fd2807SJeff Garzik 	int rc;
3022c6fd2807SJeff Garzik 
3023936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3024c6fd2807SJeff Garzik 		return rc;
3025c6fd2807SJeff Garzik 
3026936fd732STejun Heo 	if (!__sata_set_spd_needed(link, &scontrol))
3027c6fd2807SJeff Garzik 		return 0;
3028c6fd2807SJeff Garzik 
3029936fd732STejun Heo 	if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3030c6fd2807SJeff Garzik 		return rc;
3031c6fd2807SJeff Garzik 
3032c6fd2807SJeff Garzik 	return 1;
3033c6fd2807SJeff Garzik }
3034c6fd2807SJeff Garzik 
3035c6fd2807SJeff Garzik /*
3036c6fd2807SJeff Garzik  * This mode timing computation functionality is ported over from
3037c6fd2807SJeff Garzik  * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
3038c6fd2807SJeff Garzik  */
3039c6fd2807SJeff Garzik /*
3040b352e57dSAlan Cox  * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
3041c6fd2807SJeff Garzik  * These were taken from ATA/ATAPI-6 standard, rev 0a, except
3042b352e57dSAlan Cox  * for UDMA6, which is currently supported only by Maxtor drives.
3043b352e57dSAlan Cox  *
3044b352e57dSAlan Cox  * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0.
3045c6fd2807SJeff Garzik  */
3046c6fd2807SJeff Garzik 
3047c6fd2807SJeff Garzik static const struct ata_timing ata_timing[] = {
30483ada9c12SDavid Daney /*	{ XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 0,  960,   0 }, */
30493ada9c12SDavid Daney 	{ XFER_PIO_0,     70, 290, 240, 600, 165, 150, 0,  600,   0 },
30503ada9c12SDavid Daney 	{ XFER_PIO_1,     50, 290,  93, 383, 125, 100, 0,  383,   0 },
30513ada9c12SDavid Daney 	{ XFER_PIO_2,     30, 290,  40, 330, 100,  90, 0,  240,   0 },
30523ada9c12SDavid Daney 	{ XFER_PIO_3,     30,  80,  70, 180,  80,  70, 0,  180,   0 },
30533ada9c12SDavid Daney 	{ XFER_PIO_4,     25,  70,  25, 120,  70,  25, 0,  120,   0 },
30543ada9c12SDavid Daney 	{ XFER_PIO_5,     15,  65,  25, 100,  65,  25, 0,  100,   0 },
30553ada9c12SDavid Daney 	{ XFER_PIO_6,     10,  55,  20,  80,  55,  20, 0,   80,   0 },
3056c6fd2807SJeff Garzik 
30573ada9c12SDavid Daney 	{ XFER_SW_DMA_0, 120,   0,   0,   0, 480, 480, 50, 960,   0 },
30583ada9c12SDavid Daney 	{ XFER_SW_DMA_1,  90,   0,   0,   0, 240, 240, 30, 480,   0 },
30593ada9c12SDavid Daney 	{ XFER_SW_DMA_2,  60,   0,   0,   0, 120, 120, 20, 240,   0 },
3060c6fd2807SJeff Garzik 
30613ada9c12SDavid Daney 	{ XFER_MW_DMA_0,  60,   0,   0,   0, 215, 215, 20, 480,   0 },
30623ada9c12SDavid Daney 	{ XFER_MW_DMA_1,  45,   0,   0,   0,  80,  50, 5,  150,   0 },
30633ada9c12SDavid Daney 	{ XFER_MW_DMA_2,  25,   0,   0,   0,  70,  25, 5,  120,   0 },
30643ada9c12SDavid Daney 	{ XFER_MW_DMA_3,  25,   0,   0,   0,  65,  25, 5,  100,   0 },
30653ada9c12SDavid Daney 	{ XFER_MW_DMA_4,  25,   0,   0,   0,  55,  20, 5,   80,   0 },
3066c6fd2807SJeff Garzik 
30673ada9c12SDavid Daney /*	{ XFER_UDMA_SLOW,  0,   0,   0,   0,   0,   0, 0,    0, 150 }, */
30683ada9c12SDavid Daney 	{ XFER_UDMA_0,     0,   0,   0,   0,   0,   0, 0,    0, 120 },
30693ada9c12SDavid Daney 	{ XFER_UDMA_1,     0,   0,   0,   0,   0,   0, 0,    0,  80 },
30703ada9c12SDavid Daney 	{ XFER_UDMA_2,     0,   0,   0,   0,   0,   0, 0,    0,  60 },
30713ada9c12SDavid Daney 	{ XFER_UDMA_3,     0,   0,   0,   0,   0,   0, 0,    0,  45 },
30723ada9c12SDavid Daney 	{ XFER_UDMA_4,     0,   0,   0,   0,   0,   0, 0,    0,  30 },
30733ada9c12SDavid Daney 	{ XFER_UDMA_5,     0,   0,   0,   0,   0,   0, 0,    0,  20 },
30743ada9c12SDavid Daney 	{ XFER_UDMA_6,     0,   0,   0,   0,   0,   0, 0,    0,  15 },
3075c6fd2807SJeff Garzik 
3076c6fd2807SJeff Garzik 	{ 0xFF }
3077c6fd2807SJeff Garzik };
3078c6fd2807SJeff Garzik 
3079c6fd2807SJeff Garzik #define ENOUGH(v, unit)		(((v)-1)/(unit)+1)
3080c6fd2807SJeff Garzik #define EZ(v, unit)		((v)?ENOUGH(v, unit):0)
3081c6fd2807SJeff Garzik 
3082c6fd2807SJeff Garzik static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
3083c6fd2807SJeff Garzik {
3084c6fd2807SJeff Garzik 	q->setup	= EZ(t->setup      * 1000,  T);
3085c6fd2807SJeff Garzik 	q->act8b	= EZ(t->act8b      * 1000,  T);
3086c6fd2807SJeff Garzik 	q->rec8b	= EZ(t->rec8b      * 1000,  T);
3087c6fd2807SJeff Garzik 	q->cyc8b	= EZ(t->cyc8b      * 1000,  T);
3088c6fd2807SJeff Garzik 	q->active	= EZ(t->active     * 1000,  T);
3089c6fd2807SJeff Garzik 	q->recover	= EZ(t->recover    * 1000,  T);
30903ada9c12SDavid Daney 	q->dmack_hold	= EZ(t->dmack_hold * 1000,  T);
3091c6fd2807SJeff Garzik 	q->cycle	= EZ(t->cycle      * 1000,  T);
3092c6fd2807SJeff Garzik 	q->udma		= EZ(t->udma       * 1000, UT);
3093c6fd2807SJeff Garzik }
3094c6fd2807SJeff Garzik 
3095c6fd2807SJeff Garzik void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
3096c6fd2807SJeff Garzik 		      struct ata_timing *m, unsigned int what)
3097c6fd2807SJeff Garzik {
3098c6fd2807SJeff Garzik 	if (what & ATA_TIMING_SETUP  ) m->setup   = max(a->setup,   b->setup);
3099c6fd2807SJeff Garzik 	if (what & ATA_TIMING_ACT8B  ) m->act8b   = max(a->act8b,   b->act8b);
3100c6fd2807SJeff Garzik 	if (what & ATA_TIMING_REC8B  ) m->rec8b   = max(a->rec8b,   b->rec8b);
3101c6fd2807SJeff Garzik 	if (what & ATA_TIMING_CYC8B  ) m->cyc8b   = max(a->cyc8b,   b->cyc8b);
3102c6fd2807SJeff Garzik 	if (what & ATA_TIMING_ACTIVE ) m->active  = max(a->active,  b->active);
3103c6fd2807SJeff Garzik 	if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
31043ada9c12SDavid Daney 	if (what & ATA_TIMING_DMACK_HOLD) m->dmack_hold = max(a->dmack_hold, b->dmack_hold);
3105c6fd2807SJeff Garzik 	if (what & ATA_TIMING_CYCLE  ) m->cycle   = max(a->cycle,   b->cycle);
3106c6fd2807SJeff Garzik 	if (what & ATA_TIMING_UDMA   ) m->udma    = max(a->udma,    b->udma);
3107c6fd2807SJeff Garzik }
3108c6fd2807SJeff Garzik 
31096357357cSTejun Heo const struct ata_timing *ata_timing_find_mode(u8 xfer_mode)
3110c6fd2807SJeff Garzik {
311170cd071eSTejun Heo 	const struct ata_timing *t = ata_timing;
3112c6fd2807SJeff Garzik 
311370cd071eSTejun Heo 	while (xfer_mode > t->mode)
311470cd071eSTejun Heo 		t++;
311570cd071eSTejun Heo 
311670cd071eSTejun Heo 	if (xfer_mode == t->mode)
3117c6fd2807SJeff Garzik 		return t;
311870cd071eSTejun Heo 	return NULL;
3119c6fd2807SJeff Garzik }
3120c6fd2807SJeff Garzik 
3121c6fd2807SJeff Garzik int ata_timing_compute(struct ata_device *adev, unsigned short speed,
3122c6fd2807SJeff Garzik 		       struct ata_timing *t, int T, int UT)
3123c6fd2807SJeff Garzik {
31249e8808a9SBartlomiej Zolnierkiewicz 	const u16 *id = adev->id;
3125c6fd2807SJeff Garzik 	const struct ata_timing *s;
3126c6fd2807SJeff Garzik 	struct ata_timing p;
3127c6fd2807SJeff Garzik 
3128c6fd2807SJeff Garzik 	/*
3129c6fd2807SJeff Garzik 	 * Find the mode.
3130c6fd2807SJeff Garzik 	 */
3131c6fd2807SJeff Garzik 
3132c6fd2807SJeff Garzik 	if (!(s = ata_timing_find_mode(speed)))
3133c6fd2807SJeff Garzik 		return -EINVAL;
3134c6fd2807SJeff Garzik 
3135c6fd2807SJeff Garzik 	memcpy(t, s, sizeof(*s));
3136c6fd2807SJeff Garzik 
3137c6fd2807SJeff Garzik 	/*
3138c6fd2807SJeff Garzik 	 * If the drive is an EIDE drive, it can tell us it needs extended
3139c6fd2807SJeff Garzik 	 * PIO/MW_DMA cycle timing.
3140c6fd2807SJeff Garzik 	 */
3141c6fd2807SJeff Garzik 
31429e8808a9SBartlomiej Zolnierkiewicz 	if (id[ATA_ID_FIELD_VALID] & 2) {	/* EIDE drive */
3143c6fd2807SJeff Garzik 		memset(&p, 0, sizeof(p));
31449e8808a9SBartlomiej Zolnierkiewicz 
3145c6fd2807SJeff Garzik 		if (speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
31469e8808a9SBartlomiej Zolnierkiewicz 			if (speed <= XFER_PIO_2)
31479e8808a9SBartlomiej Zolnierkiewicz 				p.cycle = p.cyc8b = id[ATA_ID_EIDE_PIO];
31489e8808a9SBartlomiej Zolnierkiewicz 			else if ((speed <= XFER_PIO_4) ||
31499e8808a9SBartlomiej Zolnierkiewicz 				 (speed == XFER_PIO_5 && !ata_id_is_cfa(id)))
31509e8808a9SBartlomiej Zolnierkiewicz 				p.cycle = p.cyc8b = id[ATA_ID_EIDE_PIO_IORDY];
31519e8808a9SBartlomiej Zolnierkiewicz 		} else if (speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2)
31529e8808a9SBartlomiej Zolnierkiewicz 			p.cycle = id[ATA_ID_EIDE_DMA_MIN];
31539e8808a9SBartlomiej Zolnierkiewicz 
3154c6fd2807SJeff Garzik 		ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
3155c6fd2807SJeff Garzik 	}
3156c6fd2807SJeff Garzik 
3157c6fd2807SJeff Garzik 	/*
3158c6fd2807SJeff Garzik 	 * Convert the timing to bus clock counts.
3159c6fd2807SJeff Garzik 	 */
3160c6fd2807SJeff Garzik 
3161c6fd2807SJeff Garzik 	ata_timing_quantize(t, t, T, UT);
3162c6fd2807SJeff Garzik 
3163c6fd2807SJeff Garzik 	/*
3164c6fd2807SJeff Garzik 	 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
3165c6fd2807SJeff Garzik 	 * S.M.A.R.T * and some other commands. We have to ensure that the
3166c6fd2807SJeff Garzik 	 * DMA cycle timing is slower/equal than the fastest PIO timing.
3167c6fd2807SJeff Garzik 	 */
3168c6fd2807SJeff Garzik 
3169fd3367afSAlan 	if (speed > XFER_PIO_6) {
3170c6fd2807SJeff Garzik 		ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
3171c6fd2807SJeff Garzik 		ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
3172c6fd2807SJeff Garzik 	}
3173c6fd2807SJeff Garzik 
3174c6fd2807SJeff Garzik 	/*
3175c6fd2807SJeff Garzik 	 * Lengthen active & recovery time so that cycle time is correct.
3176c6fd2807SJeff Garzik 	 */
3177c6fd2807SJeff Garzik 
3178c6fd2807SJeff Garzik 	if (t->act8b + t->rec8b < t->cyc8b) {
3179c6fd2807SJeff Garzik 		t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
3180c6fd2807SJeff Garzik 		t->rec8b = t->cyc8b - t->act8b;
3181c6fd2807SJeff Garzik 	}
3182c6fd2807SJeff Garzik 
3183c6fd2807SJeff Garzik 	if (t->active + t->recover < t->cycle) {
3184c6fd2807SJeff Garzik 		t->active += (t->cycle - (t->active + t->recover)) / 2;
3185c6fd2807SJeff Garzik 		t->recover = t->cycle - t->active;
3186c6fd2807SJeff Garzik 	}
31874f701d1eSAlan Cox 
31884f701d1eSAlan Cox 	/* In a few cases quantisation may produce enough errors to
31894f701d1eSAlan Cox 	   leave t->cycle too low for the sum of active and recovery
31904f701d1eSAlan Cox 	   if so we must correct this */
31914f701d1eSAlan Cox 	if (t->active + t->recover > t->cycle)
31924f701d1eSAlan Cox 		t->cycle = t->active + t->recover;
3193c6fd2807SJeff Garzik 
3194c6fd2807SJeff Garzik 	return 0;
3195c6fd2807SJeff Garzik }
3196c6fd2807SJeff Garzik 
3197c6fd2807SJeff Garzik /**
3198a0f79b92STejun Heo  *	ata_timing_cycle2mode - find xfer mode for the specified cycle duration
3199a0f79b92STejun Heo  *	@xfer_shift: ATA_SHIFT_* value for transfer type to examine.
3200a0f79b92STejun Heo  *	@cycle: cycle duration in ns
3201a0f79b92STejun Heo  *
3202a0f79b92STejun Heo  *	Return matching xfer mode for @cycle.  The returned mode is of
3203a0f79b92STejun Heo  *	the transfer type specified by @xfer_shift.  If @cycle is too
3204a0f79b92STejun Heo  *	slow for @xfer_shift, 0xff is returned.  If @cycle is faster
3205a0f79b92STejun Heo  *	than the fastest known mode, the fasted mode is returned.
3206a0f79b92STejun Heo  *
3207a0f79b92STejun Heo  *	LOCKING:
3208a0f79b92STejun Heo  *	None.
3209a0f79b92STejun Heo  *
3210a0f79b92STejun Heo  *	RETURNS:
3211a0f79b92STejun Heo  *	Matching xfer_mode, 0xff if no match found.
3212a0f79b92STejun Heo  */
3213a0f79b92STejun Heo u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
3214a0f79b92STejun Heo {
3215a0f79b92STejun Heo 	u8 base_mode = 0xff, last_mode = 0xff;
3216a0f79b92STejun Heo 	const struct ata_xfer_ent *ent;
3217a0f79b92STejun Heo 	const struct ata_timing *t;
3218a0f79b92STejun Heo 
3219a0f79b92STejun Heo 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
3220a0f79b92STejun Heo 		if (ent->shift == xfer_shift)
3221a0f79b92STejun Heo 			base_mode = ent->base;
3222a0f79b92STejun Heo 
3223a0f79b92STejun Heo 	for (t = ata_timing_find_mode(base_mode);
3224a0f79b92STejun Heo 	     t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
3225a0f79b92STejun Heo 		unsigned short this_cycle;
3226a0f79b92STejun Heo 
3227a0f79b92STejun Heo 		switch (xfer_shift) {
3228a0f79b92STejun Heo 		case ATA_SHIFT_PIO:
3229a0f79b92STejun Heo 		case ATA_SHIFT_MWDMA:
3230a0f79b92STejun Heo 			this_cycle = t->cycle;
3231a0f79b92STejun Heo 			break;
3232a0f79b92STejun Heo 		case ATA_SHIFT_UDMA:
3233a0f79b92STejun Heo 			this_cycle = t->udma;
3234a0f79b92STejun Heo 			break;
3235a0f79b92STejun Heo 		default:
3236a0f79b92STejun Heo 			return 0xff;
3237a0f79b92STejun Heo 		}
3238a0f79b92STejun Heo 
3239a0f79b92STejun Heo 		if (cycle > this_cycle)
3240a0f79b92STejun Heo 			break;
3241a0f79b92STejun Heo 
3242a0f79b92STejun Heo 		last_mode = t->mode;
3243a0f79b92STejun Heo 	}
3244a0f79b92STejun Heo 
3245a0f79b92STejun Heo 	return last_mode;
3246a0f79b92STejun Heo }
3247a0f79b92STejun Heo 
3248a0f79b92STejun Heo /**
3249c6fd2807SJeff Garzik  *	ata_down_xfermask_limit - adjust dev xfer masks downward
3250c6fd2807SJeff Garzik  *	@dev: Device to adjust xfer masks
3251458337dbSTejun Heo  *	@sel: ATA_DNXFER_* selector
3252c6fd2807SJeff Garzik  *
3253c6fd2807SJeff Garzik  *	Adjust xfer masks of @dev downward.  Note that this function
3254c6fd2807SJeff Garzik  *	does not apply the change.  Invoking ata_set_mode() afterwards
3255c6fd2807SJeff Garzik  *	will apply the limit.
3256c6fd2807SJeff Garzik  *
3257c6fd2807SJeff Garzik  *	LOCKING:
3258c6fd2807SJeff Garzik  *	Inherited from caller.
3259c6fd2807SJeff Garzik  *
3260c6fd2807SJeff Garzik  *	RETURNS:
3261c6fd2807SJeff Garzik  *	0 on success, negative errno on failure
3262c6fd2807SJeff Garzik  */
3263458337dbSTejun Heo int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
3264c6fd2807SJeff Garzik {
3265458337dbSTejun Heo 	char buf[32];
32667dc951aeSTejun Heo 	unsigned long orig_mask, xfer_mask;
32677dc951aeSTejun Heo 	unsigned long pio_mask, mwdma_mask, udma_mask;
3268458337dbSTejun Heo 	int quiet, highbit;
3269c6fd2807SJeff Garzik 
3270458337dbSTejun Heo 	quiet = !!(sel & ATA_DNXFER_QUIET);
3271458337dbSTejun Heo 	sel &= ~ATA_DNXFER_QUIET;
3272458337dbSTejun Heo 
3273458337dbSTejun Heo 	xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
3274458337dbSTejun Heo 						  dev->mwdma_mask,
3275c6fd2807SJeff Garzik 						  dev->udma_mask);
3276458337dbSTejun Heo 	ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
3277c6fd2807SJeff Garzik 
3278458337dbSTejun Heo 	switch (sel) {
3279458337dbSTejun Heo 	case ATA_DNXFER_PIO:
3280458337dbSTejun Heo 		highbit = fls(pio_mask) - 1;
3281458337dbSTejun Heo 		pio_mask &= ~(1 << highbit);
3282458337dbSTejun Heo 		break;
3283458337dbSTejun Heo 
3284458337dbSTejun Heo 	case ATA_DNXFER_DMA:
3285458337dbSTejun Heo 		if (udma_mask) {
3286458337dbSTejun Heo 			highbit = fls(udma_mask) - 1;
3287458337dbSTejun Heo 			udma_mask &= ~(1 << highbit);
3288458337dbSTejun Heo 			if (!udma_mask)
3289458337dbSTejun Heo 				return -ENOENT;
3290458337dbSTejun Heo 		} else if (mwdma_mask) {
3291458337dbSTejun Heo 			highbit = fls(mwdma_mask) - 1;
3292458337dbSTejun Heo 			mwdma_mask &= ~(1 << highbit);
3293458337dbSTejun Heo 			if (!mwdma_mask)
3294458337dbSTejun Heo 				return -ENOENT;
3295458337dbSTejun Heo 		}
3296458337dbSTejun Heo 		break;
3297458337dbSTejun Heo 
3298458337dbSTejun Heo 	case ATA_DNXFER_40C:
3299458337dbSTejun Heo 		udma_mask &= ATA_UDMA_MASK_40C;
3300458337dbSTejun Heo 		break;
3301458337dbSTejun Heo 
3302458337dbSTejun Heo 	case ATA_DNXFER_FORCE_PIO0:
3303458337dbSTejun Heo 		pio_mask &= 1;
3304458337dbSTejun Heo 	case ATA_DNXFER_FORCE_PIO:
3305458337dbSTejun Heo 		mwdma_mask = 0;
3306458337dbSTejun Heo 		udma_mask = 0;
3307458337dbSTejun Heo 		break;
3308458337dbSTejun Heo 
3309458337dbSTejun Heo 	default:
3310458337dbSTejun Heo 		BUG();
3311458337dbSTejun Heo 	}
3312458337dbSTejun Heo 
3313458337dbSTejun Heo 	xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
3314458337dbSTejun Heo 
3315458337dbSTejun Heo 	if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
3316458337dbSTejun Heo 		return -ENOENT;
3317458337dbSTejun Heo 
3318458337dbSTejun Heo 	if (!quiet) {
3319458337dbSTejun Heo 		if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
3320458337dbSTejun Heo 			snprintf(buf, sizeof(buf), "%s:%s",
3321458337dbSTejun Heo 				 ata_mode_string(xfer_mask),
3322458337dbSTejun Heo 				 ata_mode_string(xfer_mask & ATA_MASK_PIO));
3323458337dbSTejun Heo 		else
3324458337dbSTejun Heo 			snprintf(buf, sizeof(buf), "%s",
3325458337dbSTejun Heo 				 ata_mode_string(xfer_mask));
3326458337dbSTejun Heo 
3327458337dbSTejun Heo 		ata_dev_printk(dev, KERN_WARNING,
3328458337dbSTejun Heo 			       "limiting speed to %s\n", buf);
3329458337dbSTejun Heo 	}
3330c6fd2807SJeff Garzik 
3331c6fd2807SJeff Garzik 	ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
3332c6fd2807SJeff Garzik 			    &dev->udma_mask);
3333c6fd2807SJeff Garzik 
3334c6fd2807SJeff Garzik 	return 0;
3335c6fd2807SJeff Garzik }
3336c6fd2807SJeff Garzik 
3337c6fd2807SJeff Garzik static int ata_dev_set_mode(struct ata_device *dev)
3338c6fd2807SJeff Garzik {
3339d0cb43b3STejun Heo 	struct ata_port *ap = dev->link->ap;
33409af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
3341d0cb43b3STejun Heo 	const bool nosetxfer = dev->horkage & ATA_HORKAGE_NOSETXFER;
33424055dee7STejun Heo 	const char *dev_err_whine = "";
33434055dee7STejun Heo 	int ign_dev_err = 0;
3344d0cb43b3STejun Heo 	unsigned int err_mask = 0;
3345c6fd2807SJeff Garzik 	int rc;
3346c6fd2807SJeff Garzik 
3347c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_PIO;
3348c6fd2807SJeff Garzik 	if (dev->xfer_shift == ATA_SHIFT_PIO)
3349c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_PIO;
3350c6fd2807SJeff Garzik 
3351d0cb43b3STejun Heo 	if (nosetxfer && ap->flags & ATA_FLAG_SATA && ata_id_is_sata(dev->id))
3352d0cb43b3STejun Heo 		dev_err_whine = " (SET_XFERMODE skipped)";
3353d0cb43b3STejun Heo 	else {
3354d0cb43b3STejun Heo 		if (nosetxfer)
3355d0cb43b3STejun Heo 			ata_dev_printk(dev, KERN_WARNING,
3356d0cb43b3STejun Heo 				       "NOSETXFER but PATA detected - can't "
3357d0cb43b3STejun Heo 				       "skip SETXFER, might malfunction\n");
3358c6fd2807SJeff Garzik 		err_mask = ata_dev_set_xfermode(dev);
3359d0cb43b3STejun Heo 	}
33602dcb407eSJeff Garzik 
33614055dee7STejun Heo 	if (err_mask & ~AC_ERR_DEV)
33624055dee7STejun Heo 		goto fail;
33632dcb407eSJeff Garzik 
33644055dee7STejun Heo 	/* revalidate */
3365baa1e78aSTejun Heo 	ehc->i.flags |= ATA_EHI_POST_SETMODE;
3366422c9daaSTejun Heo 	rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
3367baa1e78aSTejun Heo 	ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
3368c6fd2807SJeff Garzik 	if (rc)
3369c6fd2807SJeff Garzik 		return rc;
3370c6fd2807SJeff Garzik 
3371b93fda12SAlan Cox 	if (dev->xfer_shift == ATA_SHIFT_PIO) {
33724055dee7STejun Heo 		/* Old CFA may refuse this command, which is just fine */
3373b93fda12SAlan Cox 		if (ata_id_is_cfa(dev->id))
33744055dee7STejun Heo 			ign_dev_err = 1;
3375b93fda12SAlan Cox 		/* Catch several broken garbage emulations plus some pre
3376b93fda12SAlan Cox 		   ATA devices */
3377b93fda12SAlan Cox 		if (ata_id_major_version(dev->id) == 0 &&
33784055dee7STejun Heo 					dev->pio_mode <= XFER_PIO_2)
33794055dee7STejun Heo 			ign_dev_err = 1;
3380b93fda12SAlan Cox 		/* Some very old devices and some bad newer ones fail
3381b93fda12SAlan Cox 		   any kind of SET_XFERMODE request but support PIO0-2
3382b93fda12SAlan Cox 		   timings and no IORDY */
3383b93fda12SAlan Cox 		if (!ata_id_has_iordy(dev->id) && dev->pio_mode <= XFER_PIO_2)
3384b93fda12SAlan Cox 			ign_dev_err = 1;
3385b93fda12SAlan Cox 	}
33864055dee7STejun Heo 	/* Early MWDMA devices do DMA but don't allow DMA mode setting.
33874055dee7STejun Heo 	   Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
33884055dee7STejun Heo 	if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
33894055dee7STejun Heo 	    dev->dma_mode == XFER_MW_DMA_0 &&
33904055dee7STejun Heo 	    (dev->id[63] >> 8) & 1)
33914055dee7STejun Heo 		ign_dev_err = 1;
33924055dee7STejun Heo 
33934055dee7STejun Heo 	/* if the device is actually configured correctly, ignore dev err */
33944055dee7STejun Heo 	if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id)))
33954055dee7STejun Heo 		ign_dev_err = 1;
33964055dee7STejun Heo 
33974055dee7STejun Heo 	if (err_mask & AC_ERR_DEV) {
33984055dee7STejun Heo 		if (!ign_dev_err)
33994055dee7STejun Heo 			goto fail;
34004055dee7STejun Heo 		else
34014055dee7STejun Heo 			dev_err_whine = " (device error ignored)";
34024055dee7STejun Heo 	}
34034055dee7STejun Heo 
3404c6fd2807SJeff Garzik 	DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
3405c6fd2807SJeff Garzik 		dev->xfer_shift, (int)dev->xfer_mode);
3406c6fd2807SJeff Garzik 
34074055dee7STejun Heo 	ata_dev_printk(dev, KERN_INFO, "configured for %s%s\n",
34084055dee7STejun Heo 		       ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)),
34094055dee7STejun Heo 		       dev_err_whine);
34104055dee7STejun Heo 
3411c6fd2807SJeff Garzik 	return 0;
34124055dee7STejun Heo 
34134055dee7STejun Heo  fail:
34144055dee7STejun Heo 	ata_dev_printk(dev, KERN_ERR, "failed to set xfermode "
34154055dee7STejun Heo 		       "(err_mask=0x%x)\n", err_mask);
34164055dee7STejun Heo 	return -EIO;
3417c6fd2807SJeff Garzik }
3418c6fd2807SJeff Garzik 
3419c6fd2807SJeff Garzik /**
342004351821SAlan  *	ata_do_set_mode - Program timings and issue SET FEATURES - XFER
34210260731fSTejun Heo  *	@link: link on which timings will be programmed
34221967b7ffSJoe Perches  *	@r_failed_dev: out parameter for failed device
3423c6fd2807SJeff Garzik  *
342404351821SAlan  *	Standard implementation of the function used to tune and set
342504351821SAlan  *	ATA device disk transfer mode (PIO3, UDMA6, etc.).  If
342604351821SAlan  *	ata_dev_set_mode() fails, pointer to the failing device is
3427c6fd2807SJeff Garzik  *	returned in @r_failed_dev.
3428c6fd2807SJeff Garzik  *
3429c6fd2807SJeff Garzik  *	LOCKING:
3430c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
3431c6fd2807SJeff Garzik  *
3432c6fd2807SJeff Garzik  *	RETURNS:
3433c6fd2807SJeff Garzik  *	0 on success, negative errno otherwise
3434c6fd2807SJeff Garzik  */
343504351821SAlan 
34360260731fSTejun Heo int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
3437c6fd2807SJeff Garzik {
34380260731fSTejun Heo 	struct ata_port *ap = link->ap;
3439c6fd2807SJeff Garzik 	struct ata_device *dev;
3440f58229f8STejun Heo 	int rc = 0, used_dma = 0, found = 0;
3441c6fd2807SJeff Garzik 
3442c6fd2807SJeff Garzik 	/* step 1: calculate xfer_mask */
34431eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
34447dc951aeSTejun Heo 		unsigned long pio_mask, dma_mask;
3445b3a70601SAlan Cox 		unsigned int mode_mask;
3446c6fd2807SJeff Garzik 
3447b3a70601SAlan Cox 		mode_mask = ATA_DMA_MASK_ATA;
3448b3a70601SAlan Cox 		if (dev->class == ATA_DEV_ATAPI)
3449b3a70601SAlan Cox 			mode_mask = ATA_DMA_MASK_ATAPI;
3450b3a70601SAlan Cox 		else if (ata_id_is_cfa(dev->id))
3451b3a70601SAlan Cox 			mode_mask = ATA_DMA_MASK_CFA;
3452b3a70601SAlan Cox 
3453c6fd2807SJeff Garzik 		ata_dev_xfermask(dev);
345433267325STejun Heo 		ata_force_xfermask(dev);
3455c6fd2807SJeff Garzik 
3456c6fd2807SJeff Garzik 		pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
3457c6fd2807SJeff Garzik 		dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
3458b3a70601SAlan Cox 
3459b3a70601SAlan Cox 		if (libata_dma_mask & mode_mask)
3460b3a70601SAlan Cox 			dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
3461b3a70601SAlan Cox 		else
3462b3a70601SAlan Cox 			dma_mask = 0;
3463b3a70601SAlan Cox 
3464c6fd2807SJeff Garzik 		dev->pio_mode = ata_xfer_mask2mode(pio_mask);
3465c6fd2807SJeff Garzik 		dev->dma_mode = ata_xfer_mask2mode(dma_mask);
3466c6fd2807SJeff Garzik 
3467c6fd2807SJeff Garzik 		found = 1;
3468b15b3ebaSAlan Cox 		if (ata_dma_enabled(dev))
3469c6fd2807SJeff Garzik 			used_dma = 1;
3470c6fd2807SJeff Garzik 	}
3471c6fd2807SJeff Garzik 	if (!found)
3472c6fd2807SJeff Garzik 		goto out;
3473c6fd2807SJeff Garzik 
3474c6fd2807SJeff Garzik 	/* step 2: always set host PIO timings */
34751eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
347670cd071eSTejun Heo 		if (dev->pio_mode == 0xff) {
3477c6fd2807SJeff Garzik 			ata_dev_printk(dev, KERN_WARNING, "no PIO support\n");
3478c6fd2807SJeff Garzik 			rc = -EINVAL;
3479c6fd2807SJeff Garzik 			goto out;
3480c6fd2807SJeff Garzik 		}
3481c6fd2807SJeff Garzik 
3482c6fd2807SJeff Garzik 		dev->xfer_mode = dev->pio_mode;
3483c6fd2807SJeff Garzik 		dev->xfer_shift = ATA_SHIFT_PIO;
3484c6fd2807SJeff Garzik 		if (ap->ops->set_piomode)
3485c6fd2807SJeff Garzik 			ap->ops->set_piomode(ap, dev);
3486c6fd2807SJeff Garzik 	}
3487c6fd2807SJeff Garzik 
3488c6fd2807SJeff Garzik 	/* step 3: set host DMA timings */
34891eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
34901eca4365STejun Heo 		if (!ata_dma_enabled(dev))
3491c6fd2807SJeff Garzik 			continue;
3492c6fd2807SJeff Garzik 
3493c6fd2807SJeff Garzik 		dev->xfer_mode = dev->dma_mode;
3494c6fd2807SJeff Garzik 		dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
3495c6fd2807SJeff Garzik 		if (ap->ops->set_dmamode)
3496c6fd2807SJeff Garzik 			ap->ops->set_dmamode(ap, dev);
3497c6fd2807SJeff Garzik 	}
3498c6fd2807SJeff Garzik 
3499c6fd2807SJeff Garzik 	/* step 4: update devices' xfer mode */
35001eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
3501c6fd2807SJeff Garzik 		rc = ata_dev_set_mode(dev);
3502c6fd2807SJeff Garzik 		if (rc)
3503c6fd2807SJeff Garzik 			goto out;
3504c6fd2807SJeff Garzik 	}
3505c6fd2807SJeff Garzik 
3506c6fd2807SJeff Garzik 	/* Record simplex status. If we selected DMA then the other
3507c6fd2807SJeff Garzik 	 * host channels are not permitted to do so.
3508c6fd2807SJeff Garzik 	 */
3509cca3974eSJeff Garzik 	if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
3510032af1ceSAlan 		ap->host->simplex_claimed = ap;
3511c6fd2807SJeff Garzik 
3512c6fd2807SJeff Garzik  out:
3513c6fd2807SJeff Garzik 	if (rc)
3514c6fd2807SJeff Garzik 		*r_failed_dev = dev;
3515c6fd2807SJeff Garzik 	return rc;
3516c6fd2807SJeff Garzik }
3517c6fd2807SJeff Garzik 
3518c6fd2807SJeff Garzik /**
3519aa2731adSTejun Heo  *	ata_wait_ready - wait for link to become ready
3520aa2731adSTejun Heo  *	@link: link to be waited on
3521aa2731adSTejun Heo  *	@deadline: deadline jiffies for the operation
3522aa2731adSTejun Heo  *	@check_ready: callback to check link readiness
3523aa2731adSTejun Heo  *
3524aa2731adSTejun Heo  *	Wait for @link to become ready.  @check_ready should return
3525aa2731adSTejun Heo  *	positive number if @link is ready, 0 if it isn't, -ENODEV if
3526aa2731adSTejun Heo  *	link doesn't seem to be occupied, other errno for other error
3527aa2731adSTejun Heo  *	conditions.
3528aa2731adSTejun Heo  *
3529aa2731adSTejun Heo  *	Transient -ENODEV conditions are allowed for
3530aa2731adSTejun Heo  *	ATA_TMOUT_FF_WAIT.
3531aa2731adSTejun Heo  *
3532aa2731adSTejun Heo  *	LOCKING:
3533aa2731adSTejun Heo  *	EH context.
3534aa2731adSTejun Heo  *
3535aa2731adSTejun Heo  *	RETURNS:
3536aa2731adSTejun Heo  *	0 if @linke is ready before @deadline; otherwise, -errno.
3537aa2731adSTejun Heo  */
3538aa2731adSTejun Heo int ata_wait_ready(struct ata_link *link, unsigned long deadline,
3539aa2731adSTejun Heo 		   int (*check_ready)(struct ata_link *link))
3540aa2731adSTejun Heo {
3541aa2731adSTejun Heo 	unsigned long start = jiffies;
3542b48d58f5STejun Heo 	unsigned long nodev_deadline;
3543aa2731adSTejun Heo 	int warned = 0;
3544aa2731adSTejun Heo 
3545b48d58f5STejun Heo 	/* choose which 0xff timeout to use, read comment in libata.h */
3546b48d58f5STejun Heo 	if (link->ap->host->flags & ATA_HOST_PARALLEL_SCAN)
3547b48d58f5STejun Heo 		nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT_LONG);
3548b48d58f5STejun Heo 	else
3549b48d58f5STejun Heo 		nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT);
3550b48d58f5STejun Heo 
3551b1c72916STejun Heo 	/* Slave readiness can't be tested separately from master.  On
3552b1c72916STejun Heo 	 * M/S emulation configuration, this function should be called
3553b1c72916STejun Heo 	 * only on the master and it will handle both master and slave.
3554b1c72916STejun Heo 	 */
3555b1c72916STejun Heo 	WARN_ON(link == link->ap->slave_link);
3556b1c72916STejun Heo 
3557aa2731adSTejun Heo 	if (time_after(nodev_deadline, deadline))
3558aa2731adSTejun Heo 		nodev_deadline = deadline;
3559aa2731adSTejun Heo 
3560aa2731adSTejun Heo 	while (1) {
3561aa2731adSTejun Heo 		unsigned long now = jiffies;
3562aa2731adSTejun Heo 		int ready, tmp;
3563aa2731adSTejun Heo 
3564aa2731adSTejun Heo 		ready = tmp = check_ready(link);
3565aa2731adSTejun Heo 		if (ready > 0)
3566aa2731adSTejun Heo 			return 0;
3567aa2731adSTejun Heo 
3568b48d58f5STejun Heo 		/*
3569b48d58f5STejun Heo 		 * -ENODEV could be transient.  Ignore -ENODEV if link
3570aa2731adSTejun Heo 		 * is online.  Also, some SATA devices take a long
3571b48d58f5STejun Heo 		 * time to clear 0xff after reset.  Wait for
3572b48d58f5STejun Heo 		 * ATA_TMOUT_FF_WAIT[_LONG] on -ENODEV if link isn't
3573b48d58f5STejun Heo 		 * offline.
3574aa2731adSTejun Heo 		 *
3575aa2731adSTejun Heo 		 * Note that some PATA controllers (pata_ali) explode
3576aa2731adSTejun Heo 		 * if status register is read more than once when
3577aa2731adSTejun Heo 		 * there's no device attached.
3578aa2731adSTejun Heo 		 */
3579aa2731adSTejun Heo 		if (ready == -ENODEV) {
3580aa2731adSTejun Heo 			if (ata_link_online(link))
3581aa2731adSTejun Heo 				ready = 0;
3582aa2731adSTejun Heo 			else if ((link->ap->flags & ATA_FLAG_SATA) &&
3583aa2731adSTejun Heo 				 !ata_link_offline(link) &&
3584aa2731adSTejun Heo 				 time_before(now, nodev_deadline))
3585aa2731adSTejun Heo 				ready = 0;
3586aa2731adSTejun Heo 		}
3587aa2731adSTejun Heo 
3588aa2731adSTejun Heo 		if (ready)
3589aa2731adSTejun Heo 			return ready;
3590aa2731adSTejun Heo 		if (time_after(now, deadline))
3591aa2731adSTejun Heo 			return -EBUSY;
3592aa2731adSTejun Heo 
3593aa2731adSTejun Heo 		if (!warned && time_after(now, start + 5 * HZ) &&
3594aa2731adSTejun Heo 		    (deadline - now > 3 * HZ)) {
3595aa2731adSTejun Heo 			ata_link_printk(link, KERN_WARNING,
3596aa2731adSTejun Heo 				"link is slow to respond, please be patient "
3597aa2731adSTejun Heo 				"(ready=%d)\n", tmp);
3598aa2731adSTejun Heo 			warned = 1;
3599aa2731adSTejun Heo 		}
3600aa2731adSTejun Heo 
3601aa2731adSTejun Heo 		msleep(50);
3602aa2731adSTejun Heo 	}
3603aa2731adSTejun Heo }
3604aa2731adSTejun Heo 
3605aa2731adSTejun Heo /**
3606aa2731adSTejun Heo  *	ata_wait_after_reset - wait for link to become ready after reset
3607aa2731adSTejun Heo  *	@link: link to be waited on
3608aa2731adSTejun Heo  *	@deadline: deadline jiffies for the operation
3609aa2731adSTejun Heo  *	@check_ready: callback to check link readiness
3610aa2731adSTejun Heo  *
3611aa2731adSTejun Heo  *	Wait for @link to become ready after reset.
3612aa2731adSTejun Heo  *
3613aa2731adSTejun Heo  *	LOCKING:
3614aa2731adSTejun Heo  *	EH context.
3615aa2731adSTejun Heo  *
3616aa2731adSTejun Heo  *	RETURNS:
3617aa2731adSTejun Heo  *	0 if @linke is ready before @deadline; otherwise, -errno.
3618aa2731adSTejun Heo  */
36192b4221bbSHarvey Harrison int ata_wait_after_reset(struct ata_link *link, unsigned long deadline,
3620aa2731adSTejun Heo 				int (*check_ready)(struct ata_link *link))
3621aa2731adSTejun Heo {
3622341c2c95STejun Heo 	msleep(ATA_WAIT_AFTER_RESET);
3623aa2731adSTejun Heo 
3624aa2731adSTejun Heo 	return ata_wait_ready(link, deadline, check_ready);
3625aa2731adSTejun Heo }
3626aa2731adSTejun Heo 
3627aa2731adSTejun Heo /**
3628936fd732STejun Heo  *	sata_link_debounce - debounce SATA phy status
3629936fd732STejun Heo  *	@link: ATA link to debounce SATA phy status for
3630c6fd2807SJeff Garzik  *	@params: timing parameters { interval, duratinon, timeout } in msec
3631d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
3632c6fd2807SJeff Garzik  *
3633936fd732STejun Heo *	Make sure SStatus of @link reaches stable state, determined by
3634c6fd2807SJeff Garzik  *	holding the same value where DET is not 1 for @duration polled
3635c6fd2807SJeff Garzik  *	every @interval, before @timeout.  Timeout constraints the
3636d4b2bab4STejun Heo  *	beginning of the stable state.  Because DET gets stuck at 1 on
3637d4b2bab4STejun Heo  *	some controllers after hot unplugging, this functions waits
3638c6fd2807SJeff Garzik  *	until timeout then returns 0 if DET is stable at 1.
3639c6fd2807SJeff Garzik  *
3640d4b2bab4STejun Heo  *	@timeout is further limited by @deadline.  The sooner of the
3641d4b2bab4STejun Heo  *	two is used.
3642d4b2bab4STejun Heo  *
3643c6fd2807SJeff Garzik  *	LOCKING:
3644c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3645c6fd2807SJeff Garzik  *
3646c6fd2807SJeff Garzik  *	RETURNS:
3647c6fd2807SJeff Garzik  *	0 on success, -errno on failure.
3648c6fd2807SJeff Garzik  */
3649936fd732STejun Heo int sata_link_debounce(struct ata_link *link, const unsigned long *params,
3650d4b2bab4STejun Heo 		       unsigned long deadline)
3651c6fd2807SJeff Garzik {
3652341c2c95STejun Heo 	unsigned long interval = params[0];
3653341c2c95STejun Heo 	unsigned long duration = params[1];
3654d4b2bab4STejun Heo 	unsigned long last_jiffies, t;
3655c6fd2807SJeff Garzik 	u32 last, cur;
3656c6fd2807SJeff Garzik 	int rc;
3657c6fd2807SJeff Garzik 
3658341c2c95STejun Heo 	t = ata_deadline(jiffies, params[2]);
3659d4b2bab4STejun Heo 	if (time_before(t, deadline))
3660d4b2bab4STejun Heo 		deadline = t;
3661d4b2bab4STejun Heo 
3662936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3663c6fd2807SJeff Garzik 		return rc;
3664c6fd2807SJeff Garzik 	cur &= 0xf;
3665c6fd2807SJeff Garzik 
3666c6fd2807SJeff Garzik 	last = cur;
3667c6fd2807SJeff Garzik 	last_jiffies = jiffies;
3668c6fd2807SJeff Garzik 
3669c6fd2807SJeff Garzik 	while (1) {
3670341c2c95STejun Heo 		msleep(interval);
3671936fd732STejun Heo 		if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3672c6fd2807SJeff Garzik 			return rc;
3673c6fd2807SJeff Garzik 		cur &= 0xf;
3674c6fd2807SJeff Garzik 
3675c6fd2807SJeff Garzik 		/* DET stable? */
3676c6fd2807SJeff Garzik 		if (cur == last) {
3677d4b2bab4STejun Heo 			if (cur == 1 && time_before(jiffies, deadline))
3678c6fd2807SJeff Garzik 				continue;
3679341c2c95STejun Heo 			if (time_after(jiffies,
3680341c2c95STejun Heo 				       ata_deadline(last_jiffies, duration)))
3681c6fd2807SJeff Garzik 				return 0;
3682c6fd2807SJeff Garzik 			continue;
3683c6fd2807SJeff Garzik 		}
3684c6fd2807SJeff Garzik 
3685c6fd2807SJeff Garzik 		/* unstable, start over */
3686c6fd2807SJeff Garzik 		last = cur;
3687c6fd2807SJeff Garzik 		last_jiffies = jiffies;
3688c6fd2807SJeff Garzik 
3689f1545154STejun Heo 		/* Check deadline.  If debouncing failed, return
3690f1545154STejun Heo 		 * -EPIPE to tell upper layer to lower link speed.
3691f1545154STejun Heo 		 */
3692d4b2bab4STejun Heo 		if (time_after(jiffies, deadline))
3693f1545154STejun Heo 			return -EPIPE;
3694c6fd2807SJeff Garzik 	}
3695c6fd2807SJeff Garzik }
3696c6fd2807SJeff Garzik 
3697c6fd2807SJeff Garzik /**
3698936fd732STejun Heo  *	sata_link_resume - resume SATA link
3699936fd732STejun Heo  *	@link: ATA link to resume SATA
3700c6fd2807SJeff Garzik  *	@params: timing parameters { interval, duratinon, timeout } in msec
3701d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
3702c6fd2807SJeff Garzik  *
3703936fd732STejun Heo  *	Resume SATA phy @link and debounce it.
3704c6fd2807SJeff Garzik  *
3705c6fd2807SJeff Garzik  *	LOCKING:
3706c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3707c6fd2807SJeff Garzik  *
3708c6fd2807SJeff Garzik  *	RETURNS:
3709c6fd2807SJeff Garzik  *	0 on success, -errno on failure.
3710c6fd2807SJeff Garzik  */
3711936fd732STejun Heo int sata_link_resume(struct ata_link *link, const unsigned long *params,
3712d4b2bab4STejun Heo 		     unsigned long deadline)
3713c6fd2807SJeff Garzik {
37145040ab67STejun Heo 	int tries = ATA_LINK_RESUME_TRIES;
3715ac371987STejun Heo 	u32 scontrol, serror;
3716c6fd2807SJeff Garzik 	int rc;
3717c6fd2807SJeff Garzik 
3718936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3719c6fd2807SJeff Garzik 		return rc;
3720c6fd2807SJeff Garzik 
37215040ab67STejun Heo 	/*
37225040ab67STejun Heo 	 * Writes to SControl sometimes get ignored under certain
37235040ab67STejun Heo 	 * controllers (ata_piix SIDPR).  Make sure DET actually is
37245040ab67STejun Heo 	 * cleared.
37255040ab67STejun Heo 	 */
37265040ab67STejun Heo 	do {
3727c6fd2807SJeff Garzik 		scontrol = (scontrol & 0x0f0) | 0x300;
3728936fd732STejun Heo 		if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3729c6fd2807SJeff Garzik 			return rc;
37305040ab67STejun Heo 		/*
37315040ab67STejun Heo 		 * Some PHYs react badly if SStatus is pounded
37325040ab67STejun Heo 		 * immediately after resuming.  Delay 200ms before
37335040ab67STejun Heo 		 * debouncing.
3734c6fd2807SJeff Garzik 		 */
3735c6fd2807SJeff Garzik 		msleep(200);
3736c6fd2807SJeff Garzik 
37375040ab67STejun Heo 		/* is SControl restored correctly? */
37385040ab67STejun Heo 		if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
37395040ab67STejun Heo 			return rc;
37405040ab67STejun Heo 	} while ((scontrol & 0xf0f) != 0x300 && --tries);
37415040ab67STejun Heo 
37425040ab67STejun Heo 	if ((scontrol & 0xf0f) != 0x300) {
37435040ab67STejun Heo 		ata_link_printk(link, KERN_ERR,
37445040ab67STejun Heo 				"failed to resume link (SControl %X)\n",
37455040ab67STejun Heo 				scontrol);
37465040ab67STejun Heo 		return 0;
37475040ab67STejun Heo 	}
37485040ab67STejun Heo 
37495040ab67STejun Heo 	if (tries < ATA_LINK_RESUME_TRIES)
37505040ab67STejun Heo 		ata_link_printk(link, KERN_WARNING,
37515040ab67STejun Heo 				"link resume succeeded after %d retries\n",
37525040ab67STejun Heo 				ATA_LINK_RESUME_TRIES - tries);
37535040ab67STejun Heo 
3754ac371987STejun Heo 	if ((rc = sata_link_debounce(link, params, deadline)))
3755ac371987STejun Heo 		return rc;
3756ac371987STejun Heo 
3757f046519fSTejun Heo 	/* clear SError, some PHYs require this even for SRST to work */
3758ac371987STejun Heo 	if (!(rc = sata_scr_read(link, SCR_ERROR, &serror)))
3759ac371987STejun Heo 		rc = sata_scr_write(link, SCR_ERROR, serror);
3760ac371987STejun Heo 
3761f046519fSTejun Heo 	return rc != -EINVAL ? rc : 0;
3762c6fd2807SJeff Garzik }
3763c6fd2807SJeff Garzik 
3764c6fd2807SJeff Garzik /**
37650aa1113dSTejun Heo  *	ata_std_prereset - prepare for reset
3766cc0680a5STejun Heo  *	@link: ATA link to be reset
3767d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
3768c6fd2807SJeff Garzik  *
3769cc0680a5STejun Heo  *	@link is about to be reset.  Initialize it.  Failure from
3770b8cffc6aSTejun Heo  *	prereset makes libata abort whole reset sequence and give up
3771b8cffc6aSTejun Heo  *	that port, so prereset should be best-effort.  It does its
3772b8cffc6aSTejun Heo  *	best to prepare for reset sequence but if things go wrong, it
3773b8cffc6aSTejun Heo  *	should just whine, not fail.
3774c6fd2807SJeff Garzik  *
3775c6fd2807SJeff Garzik  *	LOCKING:
3776c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3777c6fd2807SJeff Garzik  *
3778c6fd2807SJeff Garzik  *	RETURNS:
3779c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
3780c6fd2807SJeff Garzik  */
37810aa1113dSTejun Heo int ata_std_prereset(struct ata_link *link, unsigned long deadline)
3782c6fd2807SJeff Garzik {
3783cc0680a5STejun Heo 	struct ata_port *ap = link->ap;
3784936fd732STejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
3785c6fd2807SJeff Garzik 	const unsigned long *timing = sata_ehc_deb_timing(ehc);
3786c6fd2807SJeff Garzik 	int rc;
3787c6fd2807SJeff Garzik 
3788c6fd2807SJeff Garzik 	/* if we're about to do hardreset, nothing more to do */
3789c6fd2807SJeff Garzik 	if (ehc->i.action & ATA_EH_HARDRESET)
3790c6fd2807SJeff Garzik 		return 0;
3791c6fd2807SJeff Garzik 
3792936fd732STejun Heo 	/* if SATA, resume link */
3793a16abc0bSTejun Heo 	if (ap->flags & ATA_FLAG_SATA) {
3794936fd732STejun Heo 		rc = sata_link_resume(link, timing, deadline);
3795b8cffc6aSTejun Heo 		/* whine about phy resume failure but proceed */
3796b8cffc6aSTejun Heo 		if (rc && rc != -EOPNOTSUPP)
3797cc0680a5STejun Heo 			ata_link_printk(link, KERN_WARNING, "failed to resume "
3798c6fd2807SJeff Garzik 					"link for reset (errno=%d)\n", rc);
3799c6fd2807SJeff Garzik 	}
3800c6fd2807SJeff Garzik 
380145db2f6cSTejun Heo 	/* no point in trying softreset on offline link */
3802b1c72916STejun Heo 	if (ata_phys_link_offline(link))
380345db2f6cSTejun Heo 		ehc->i.action &= ~ATA_EH_SOFTRESET;
380445db2f6cSTejun Heo 
3805c6fd2807SJeff Garzik 	return 0;
3806c6fd2807SJeff Garzik }
3807c6fd2807SJeff Garzik 
3808c6fd2807SJeff Garzik /**
3809cc0680a5STejun Heo  *	sata_link_hardreset - reset link via SATA phy reset
3810cc0680a5STejun Heo  *	@link: link to reset
3811b6103f6dSTejun Heo  *	@timing: timing parameters { interval, duratinon, timeout } in msec
3812d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
38139dadd45bSTejun Heo  *	@online: optional out parameter indicating link onlineness
38149dadd45bSTejun Heo  *	@check_ready: optional callback to check link readiness
3815c6fd2807SJeff Garzik  *
3816cc0680a5STejun Heo  *	SATA phy-reset @link using DET bits of SControl register.
38179dadd45bSTejun Heo  *	After hardreset, link readiness is waited upon using
38189dadd45bSTejun Heo  *	ata_wait_ready() if @check_ready is specified.  LLDs are
38199dadd45bSTejun Heo  *	allowed to not specify @check_ready and wait itself after this
38209dadd45bSTejun Heo  *	function returns.  Device classification is LLD's
38219dadd45bSTejun Heo  *	responsibility.
38229dadd45bSTejun Heo  *
38239dadd45bSTejun Heo  *	*@online is set to one iff reset succeeded and @link is online
38249dadd45bSTejun Heo  *	after reset.
3825c6fd2807SJeff Garzik  *
3826c6fd2807SJeff Garzik  *	LOCKING:
3827c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3828c6fd2807SJeff Garzik  *
3829c6fd2807SJeff Garzik  *	RETURNS:
3830c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
3831c6fd2807SJeff Garzik  */
3832cc0680a5STejun Heo int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
38339dadd45bSTejun Heo 			unsigned long deadline,
38349dadd45bSTejun Heo 			bool *online, int (*check_ready)(struct ata_link *))
3835c6fd2807SJeff Garzik {
3836c6fd2807SJeff Garzik 	u32 scontrol;
3837c6fd2807SJeff Garzik 	int rc;
3838c6fd2807SJeff Garzik 
3839c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
3840c6fd2807SJeff Garzik 
38419dadd45bSTejun Heo 	if (online)
38429dadd45bSTejun Heo 		*online = false;
38439dadd45bSTejun Heo 
3844936fd732STejun Heo 	if (sata_set_spd_needed(link)) {
3845c6fd2807SJeff Garzik 		/* SATA spec says nothing about how to reconfigure
3846c6fd2807SJeff Garzik 		 * spd.  To be on the safe side, turn off phy during
3847c6fd2807SJeff Garzik 		 * reconfiguration.  This works for at least ICH7 AHCI
3848c6fd2807SJeff Garzik 		 * and Sil3124.
3849c6fd2807SJeff Garzik 		 */
3850936fd732STejun Heo 		if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3851b6103f6dSTejun Heo 			goto out;
3852c6fd2807SJeff Garzik 
3853cea0d336SJeff Garzik 		scontrol = (scontrol & 0x0f0) | 0x304;
3854c6fd2807SJeff Garzik 
3855936fd732STejun Heo 		if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3856b6103f6dSTejun Heo 			goto out;
3857c6fd2807SJeff Garzik 
3858936fd732STejun Heo 		sata_set_spd(link);
3859c6fd2807SJeff Garzik 	}
3860c6fd2807SJeff Garzik 
3861c6fd2807SJeff Garzik 	/* issue phy wake/reset */
3862936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3863b6103f6dSTejun Heo 		goto out;
3864c6fd2807SJeff Garzik 
3865c6fd2807SJeff Garzik 	scontrol = (scontrol & 0x0f0) | 0x301;
3866c6fd2807SJeff Garzik 
3867936fd732STejun Heo 	if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
3868b6103f6dSTejun Heo 		goto out;
3869c6fd2807SJeff Garzik 
3870c6fd2807SJeff Garzik 	/* Couldn't find anything in SATA I/II specs, but AHCI-1.1
3871c6fd2807SJeff Garzik 	 * 10.4.2 says at least 1 ms.
3872c6fd2807SJeff Garzik 	 */
3873c6fd2807SJeff Garzik 	msleep(1);
3874c6fd2807SJeff Garzik 
3875936fd732STejun Heo 	/* bring link back */
3876936fd732STejun Heo 	rc = sata_link_resume(link, timing, deadline);
38779dadd45bSTejun Heo 	if (rc)
38789dadd45bSTejun Heo 		goto out;
38799dadd45bSTejun Heo 	/* if link is offline nothing more to do */
3880b1c72916STejun Heo 	if (ata_phys_link_offline(link))
38819dadd45bSTejun Heo 		goto out;
38829dadd45bSTejun Heo 
38839dadd45bSTejun Heo 	/* Link is online.  From this point, -ENODEV too is an error. */
38849dadd45bSTejun Heo 	if (online)
38859dadd45bSTejun Heo 		*online = true;
38869dadd45bSTejun Heo 
3887071f44b1STejun Heo 	if (sata_pmp_supported(link->ap) && ata_is_host_link(link)) {
38889dadd45bSTejun Heo 		/* If PMP is supported, we have to do follow-up SRST.
38899dadd45bSTejun Heo 		 * Some PMPs don't send D2H Reg FIS after hardreset if
38909dadd45bSTejun Heo 		 * the first port is empty.  Wait only for
38919dadd45bSTejun Heo 		 * ATA_TMOUT_PMP_SRST_WAIT.
38929dadd45bSTejun Heo 		 */
38939dadd45bSTejun Heo 		if (check_ready) {
38949dadd45bSTejun Heo 			unsigned long pmp_deadline;
38959dadd45bSTejun Heo 
3896341c2c95STejun Heo 			pmp_deadline = ata_deadline(jiffies,
3897341c2c95STejun Heo 						    ATA_TMOUT_PMP_SRST_WAIT);
38989dadd45bSTejun Heo 			if (time_after(pmp_deadline, deadline))
38999dadd45bSTejun Heo 				pmp_deadline = deadline;
39009dadd45bSTejun Heo 			ata_wait_ready(link, pmp_deadline, check_ready);
39019dadd45bSTejun Heo 		}
39029dadd45bSTejun Heo 		rc = -EAGAIN;
39039dadd45bSTejun Heo 		goto out;
39049dadd45bSTejun Heo 	}
39059dadd45bSTejun Heo 
39069dadd45bSTejun Heo 	rc = 0;
39079dadd45bSTejun Heo 	if (check_ready)
39089dadd45bSTejun Heo 		rc = ata_wait_ready(link, deadline, check_ready);
3909b6103f6dSTejun Heo  out:
39100cbf0711STejun Heo 	if (rc && rc != -EAGAIN) {
39110cbf0711STejun Heo 		/* online is set iff link is online && reset succeeded */
39120cbf0711STejun Heo 		if (online)
39130cbf0711STejun Heo 			*online = false;
39149dadd45bSTejun Heo 		ata_link_printk(link, KERN_ERR,
39159dadd45bSTejun Heo 				"COMRESET failed (errno=%d)\n", rc);
39160cbf0711STejun Heo 	}
3917b6103f6dSTejun Heo 	DPRINTK("EXIT, rc=%d\n", rc);
3918b6103f6dSTejun Heo 	return rc;
3919b6103f6dSTejun Heo }
3920b6103f6dSTejun Heo 
3921b6103f6dSTejun Heo /**
392257c9efdfSTejun Heo  *	sata_std_hardreset - COMRESET w/o waiting or classification
392357c9efdfSTejun Heo  *	@link: link to reset
392457c9efdfSTejun Heo  *	@class: resulting class of attached device
392557c9efdfSTejun Heo  *	@deadline: deadline jiffies for the operation
392657c9efdfSTejun Heo  *
392757c9efdfSTejun Heo  *	Standard SATA COMRESET w/o waiting or classification.
392857c9efdfSTejun Heo  *
392957c9efdfSTejun Heo  *	LOCKING:
393057c9efdfSTejun Heo  *	Kernel thread context (may sleep)
393157c9efdfSTejun Heo  *
393257c9efdfSTejun Heo  *	RETURNS:
393357c9efdfSTejun Heo  *	0 if link offline, -EAGAIN if link online, -errno on errors.
393457c9efdfSTejun Heo  */
393557c9efdfSTejun Heo int sata_std_hardreset(struct ata_link *link, unsigned int *class,
393657c9efdfSTejun Heo 		       unsigned long deadline)
393757c9efdfSTejun Heo {
393857c9efdfSTejun Heo 	const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
393957c9efdfSTejun Heo 	bool online;
394057c9efdfSTejun Heo 	int rc;
394157c9efdfSTejun Heo 
394257c9efdfSTejun Heo 	/* do hardreset */
394357c9efdfSTejun Heo 	rc = sata_link_hardreset(link, timing, deadline, &online, NULL);
394457c9efdfSTejun Heo 	return online ? -EAGAIN : rc;
394557c9efdfSTejun Heo }
394657c9efdfSTejun Heo 
394757c9efdfSTejun Heo /**
3948203c75b8STejun Heo  *	ata_std_postreset - standard postreset callback
3949cc0680a5STejun Heo  *	@link: the target ata_link
3950c6fd2807SJeff Garzik  *	@classes: classes of attached devices
3951c6fd2807SJeff Garzik  *
3952c6fd2807SJeff Garzik  *	This function is invoked after a successful reset.  Note that
3953c6fd2807SJeff Garzik  *	the device might have been reset more than once using
3954c6fd2807SJeff Garzik  *	different reset methods before postreset is invoked.
3955c6fd2807SJeff Garzik  *
3956c6fd2807SJeff Garzik  *	LOCKING:
3957c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3958c6fd2807SJeff Garzik  */
3959203c75b8STejun Heo void ata_std_postreset(struct ata_link *link, unsigned int *classes)
3960c6fd2807SJeff Garzik {
3961f046519fSTejun Heo 	u32 serror;
3962f046519fSTejun Heo 
3963c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
3964c6fd2807SJeff Garzik 
3965f046519fSTejun Heo 	/* reset complete, clear SError */
3966f046519fSTejun Heo 	if (!sata_scr_read(link, SCR_ERROR, &serror))
3967f046519fSTejun Heo 		sata_scr_write(link, SCR_ERROR, serror);
3968f046519fSTejun Heo 
3969c6fd2807SJeff Garzik 	/* print link status */
3970936fd732STejun Heo 	sata_print_link_status(link);
3971c6fd2807SJeff Garzik 
3972c6fd2807SJeff Garzik 	DPRINTK("EXIT\n");
3973c6fd2807SJeff Garzik }
3974c6fd2807SJeff Garzik 
3975c6fd2807SJeff Garzik /**
3976c6fd2807SJeff Garzik  *	ata_dev_same_device - Determine whether new ID matches configured device
3977c6fd2807SJeff Garzik  *	@dev: device to compare against
3978c6fd2807SJeff Garzik  *	@new_class: class of the new device
3979c6fd2807SJeff Garzik  *	@new_id: IDENTIFY page of the new device
3980c6fd2807SJeff Garzik  *
3981c6fd2807SJeff Garzik  *	Compare @new_class and @new_id against @dev and determine
3982c6fd2807SJeff Garzik  *	whether @dev is the device indicated by @new_class and
3983c6fd2807SJeff Garzik  *	@new_id.
3984c6fd2807SJeff Garzik  *
3985c6fd2807SJeff Garzik  *	LOCKING:
3986c6fd2807SJeff Garzik  *	None.
3987c6fd2807SJeff Garzik  *
3988c6fd2807SJeff Garzik  *	RETURNS:
3989c6fd2807SJeff Garzik  *	1 if @dev matches @new_class and @new_id, 0 otherwise.
3990c6fd2807SJeff Garzik  */
3991c6fd2807SJeff Garzik static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
3992c6fd2807SJeff Garzik 			       const u16 *new_id)
3993c6fd2807SJeff Garzik {
3994c6fd2807SJeff Garzik 	const u16 *old_id = dev->id;
3995a0cf733bSTejun Heo 	unsigned char model[2][ATA_ID_PROD_LEN + 1];
3996a0cf733bSTejun Heo 	unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
3997c6fd2807SJeff Garzik 
3998c6fd2807SJeff Garzik 	if (dev->class != new_class) {
3999c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_INFO, "class mismatch %d != %d\n",
4000c6fd2807SJeff Garzik 			       dev->class, new_class);
4001c6fd2807SJeff Garzik 		return 0;
4002c6fd2807SJeff Garzik 	}
4003c6fd2807SJeff Garzik 
4004a0cf733bSTejun Heo 	ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
4005a0cf733bSTejun Heo 	ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
4006a0cf733bSTejun Heo 	ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
4007a0cf733bSTejun Heo 	ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
4008c6fd2807SJeff Garzik 
4009c6fd2807SJeff Garzik 	if (strcmp(model[0], model[1])) {
4010c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_INFO, "model number mismatch "
4011c6fd2807SJeff Garzik 			       "'%s' != '%s'\n", model[0], model[1]);
4012c6fd2807SJeff Garzik 		return 0;
4013c6fd2807SJeff Garzik 	}
4014c6fd2807SJeff Garzik 
4015c6fd2807SJeff Garzik 	if (strcmp(serial[0], serial[1])) {
4016c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_INFO, "serial number mismatch "
4017c6fd2807SJeff Garzik 			       "'%s' != '%s'\n", serial[0], serial[1]);
4018c6fd2807SJeff Garzik 		return 0;
4019c6fd2807SJeff Garzik 	}
4020c6fd2807SJeff Garzik 
4021c6fd2807SJeff Garzik 	return 1;
4022c6fd2807SJeff Garzik }
4023c6fd2807SJeff Garzik 
4024c6fd2807SJeff Garzik /**
4025fe30911bSTejun Heo  *	ata_dev_reread_id - Re-read IDENTIFY data
40263fae450cSHenrik Kretzschmar  *	@dev: target ATA device
4027bff04647STejun Heo  *	@readid_flags: read ID flags
4028c6fd2807SJeff Garzik  *
4029c6fd2807SJeff Garzik  *	Re-read IDENTIFY page and make sure @dev is still attached to
4030c6fd2807SJeff Garzik  *	the port.
4031c6fd2807SJeff Garzik  *
4032c6fd2807SJeff Garzik  *	LOCKING:
4033c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
4034c6fd2807SJeff Garzik  *
4035c6fd2807SJeff Garzik  *	RETURNS:
4036c6fd2807SJeff Garzik  *	0 on success, negative errno otherwise
4037c6fd2807SJeff Garzik  */
4038fe30911bSTejun Heo int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
4039c6fd2807SJeff Garzik {
4040c6fd2807SJeff Garzik 	unsigned int class = dev->class;
40419af5c9c9STejun Heo 	u16 *id = (void *)dev->link->ap->sector_buf;
4042c6fd2807SJeff Garzik 	int rc;
4043c6fd2807SJeff Garzik 
4044c6fd2807SJeff Garzik 	/* read ID data */
4045bff04647STejun Heo 	rc = ata_dev_read_id(dev, &class, readid_flags, id);
4046c6fd2807SJeff Garzik 	if (rc)
4047fe30911bSTejun Heo 		return rc;
4048c6fd2807SJeff Garzik 
4049c6fd2807SJeff Garzik 	/* is the device still there? */
4050fe30911bSTejun Heo 	if (!ata_dev_same_device(dev, class, id))
4051fe30911bSTejun Heo 		return -ENODEV;
4052c6fd2807SJeff Garzik 
4053c6fd2807SJeff Garzik 	memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
4054fe30911bSTejun Heo 	return 0;
4055fe30911bSTejun Heo }
4056fe30911bSTejun Heo 
4057fe30911bSTejun Heo /**
4058fe30911bSTejun Heo  *	ata_dev_revalidate - Revalidate ATA device
4059fe30911bSTejun Heo  *	@dev: device to revalidate
4060422c9daaSTejun Heo  *	@new_class: new class code
4061fe30911bSTejun Heo  *	@readid_flags: read ID flags
4062fe30911bSTejun Heo  *
4063fe30911bSTejun Heo  *	Re-read IDENTIFY page, make sure @dev is still attached to the
4064fe30911bSTejun Heo  *	port and reconfigure it according to the new IDENTIFY page.
4065fe30911bSTejun Heo  *
4066fe30911bSTejun Heo  *	LOCKING:
4067fe30911bSTejun Heo  *	Kernel thread context (may sleep)
4068fe30911bSTejun Heo  *
4069fe30911bSTejun Heo  *	RETURNS:
4070fe30911bSTejun Heo  *	0 on success, negative errno otherwise
4071fe30911bSTejun Heo  */
4072422c9daaSTejun Heo int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
4073422c9daaSTejun Heo 		       unsigned int readid_flags)
4074fe30911bSTejun Heo {
40756ddcd3b0STejun Heo 	u64 n_sectors = dev->n_sectors;
40765920dadfSTejun Heo 	u64 n_native_sectors = dev->n_native_sectors;
4077fe30911bSTejun Heo 	int rc;
4078fe30911bSTejun Heo 
4079fe30911bSTejun Heo 	if (!ata_dev_enabled(dev))
4080fe30911bSTejun Heo 		return -ENODEV;
4081fe30911bSTejun Heo 
4082422c9daaSTejun Heo 	/* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
4083422c9daaSTejun Heo 	if (ata_class_enabled(new_class) &&
4084f0d0613dSBorislav Petkov 	    new_class != ATA_DEV_ATA &&
4085f0d0613dSBorislav Petkov 	    new_class != ATA_DEV_ATAPI &&
4086f0d0613dSBorislav Petkov 	    new_class != ATA_DEV_SEMB) {
4087422c9daaSTejun Heo 		ata_dev_printk(dev, KERN_INFO, "class mismatch %u != %u\n",
4088422c9daaSTejun Heo 			       dev->class, new_class);
4089422c9daaSTejun Heo 		rc = -ENODEV;
4090422c9daaSTejun Heo 		goto fail;
4091422c9daaSTejun Heo 	}
4092422c9daaSTejun Heo 
4093fe30911bSTejun Heo 	/* re-read ID */
4094fe30911bSTejun Heo 	rc = ata_dev_reread_id(dev, readid_flags);
4095fe30911bSTejun Heo 	if (rc)
4096fe30911bSTejun Heo 		goto fail;
4097c6fd2807SJeff Garzik 
4098c6fd2807SJeff Garzik 	/* configure device according to the new ID */
4099efdaedc4STejun Heo 	rc = ata_dev_configure(dev);
41006ddcd3b0STejun Heo 	if (rc)
41016ddcd3b0STejun Heo 		goto fail;
41026ddcd3b0STejun Heo 
41036ddcd3b0STejun Heo 	/* verify n_sectors hasn't changed */
4104445d211bSTejun Heo 	if (dev->class != ATA_DEV_ATA || !n_sectors ||
4105445d211bSTejun Heo 	    dev->n_sectors == n_sectors)
4106445d211bSTejun Heo 		return 0;
4107445d211bSTejun Heo 
4108445d211bSTejun Heo 	/* n_sectors has changed */
4109445d211bSTejun Heo 	ata_dev_printk(dev, KERN_WARNING, "n_sectors mismatch %llu != %llu\n",
41106ddcd3b0STejun Heo 		       (unsigned long long)n_sectors,
41116ddcd3b0STejun Heo 		       (unsigned long long)dev->n_sectors);
4112445d211bSTejun Heo 
41135920dadfSTejun Heo 	/*
41145920dadfSTejun Heo 	 * Something could have caused HPA to be unlocked
4115445d211bSTejun Heo 	 * involuntarily.  If n_native_sectors hasn't changed and the
4116445d211bSTejun Heo 	 * new size matches it, keep the device.
41175920dadfSTejun Heo 	 */
41185920dadfSTejun Heo 	if (dev->n_native_sectors == n_native_sectors &&
4119445d211bSTejun Heo 	    dev->n_sectors > n_sectors && dev->n_sectors == n_native_sectors) {
41205920dadfSTejun Heo 		ata_dev_printk(dev, KERN_WARNING,
41215920dadfSTejun Heo 			       "new n_sectors matches native, probably "
412268939ce5STejun Heo 			       "late HPA unlock, n_sectors updated\n");
412368939ce5STejun Heo 		/* use the larger n_sectors */
4124445d211bSTejun Heo 		return 0;
4125445d211bSTejun Heo 	}
4126445d211bSTejun Heo 
4127445d211bSTejun Heo 	/*
4128445d211bSTejun Heo 	 * Some BIOSes boot w/o HPA but resume w/ HPA locked.  Try
4129445d211bSTejun Heo 	 * unlocking HPA in those cases.
4130445d211bSTejun Heo 	 *
4131445d211bSTejun Heo 	 * https://bugzilla.kernel.org/show_bug.cgi?id=15396
4132445d211bSTejun Heo 	 */
4133445d211bSTejun Heo 	if (dev->n_native_sectors == n_native_sectors &&
4134445d211bSTejun Heo 	    dev->n_sectors < n_sectors && n_sectors == n_native_sectors &&
4135445d211bSTejun Heo 	    !(dev->horkage & ATA_HORKAGE_BROKEN_HPA)) {
4136445d211bSTejun Heo 		ata_dev_printk(dev, KERN_WARNING,
4137445d211bSTejun Heo 			       "old n_sectors matches native, probably "
4138445d211bSTejun Heo 			       "late HPA lock, will try to unlock HPA\n");
4139445d211bSTejun Heo 		/* try unlocking HPA */
4140445d211bSTejun Heo 		dev->flags |= ATA_DFLAG_UNLOCK_HPA;
4141445d211bSTejun Heo 		rc = -EIO;
4142445d211bSTejun Heo 	} else
4143445d211bSTejun Heo 		rc = -ENODEV;
4144445d211bSTejun Heo 
4145445d211bSTejun Heo 	/* restore original n_[native_]sectors and fail */
41465920dadfSTejun Heo 	dev->n_native_sectors = n_native_sectors;
41475920dadfSTejun Heo 	dev->n_sectors = n_sectors;
4148c6fd2807SJeff Garzik  fail:
4149c6fd2807SJeff Garzik 	ata_dev_printk(dev, KERN_ERR, "revalidation failed (errno=%d)\n", rc);
4150c6fd2807SJeff Garzik 	return rc;
4151c6fd2807SJeff Garzik }
4152c6fd2807SJeff Garzik 
41536919a0a6SAlan Cox struct ata_blacklist_entry {
41546919a0a6SAlan Cox 	const char *model_num;
41556919a0a6SAlan Cox 	const char *model_rev;
41566919a0a6SAlan Cox 	unsigned long horkage;
41576919a0a6SAlan Cox };
41586919a0a6SAlan Cox 
41596919a0a6SAlan Cox static const struct ata_blacklist_entry ata_device_blacklist [] = {
41606919a0a6SAlan Cox 	/* Devices with DMA related problems under Linux */
41616919a0a6SAlan Cox 	{ "WDC AC11000H",	NULL,		ATA_HORKAGE_NODMA },
41626919a0a6SAlan Cox 	{ "WDC AC22100H",	NULL,		ATA_HORKAGE_NODMA },
41636919a0a6SAlan Cox 	{ "WDC AC32500H",	NULL,		ATA_HORKAGE_NODMA },
41646919a0a6SAlan Cox 	{ "WDC AC33100H",	NULL,		ATA_HORKAGE_NODMA },
41656919a0a6SAlan Cox 	{ "WDC AC31600H",	NULL,		ATA_HORKAGE_NODMA },
41666919a0a6SAlan Cox 	{ "WDC AC32100H",	"24.09P07",	ATA_HORKAGE_NODMA },
41676919a0a6SAlan Cox 	{ "WDC AC23200L",	"21.10N21",	ATA_HORKAGE_NODMA },
41686919a0a6SAlan Cox 	{ "Compaq CRD-8241B", 	NULL,		ATA_HORKAGE_NODMA },
41696919a0a6SAlan Cox 	{ "CRD-8400B",		NULL, 		ATA_HORKAGE_NODMA },
41707da4c935SMark Lord 	{ "CRD-848[02]B",	NULL,		ATA_HORKAGE_NODMA },
41716919a0a6SAlan Cox 	{ "CRD-84",		NULL,		ATA_HORKAGE_NODMA },
41726919a0a6SAlan Cox 	{ "SanDisk SDP3B",	NULL,		ATA_HORKAGE_NODMA },
41736919a0a6SAlan Cox 	{ "SanDisk SDP3B-64",	NULL,		ATA_HORKAGE_NODMA },
41746919a0a6SAlan Cox 	{ "SANYO CD-ROM CRD",	NULL,		ATA_HORKAGE_NODMA },
41756919a0a6SAlan Cox 	{ "HITACHI CDR-8",	NULL,		ATA_HORKAGE_NODMA },
41767da4c935SMark Lord 	{ "HITACHI CDR-8[34]35",NULL,		ATA_HORKAGE_NODMA },
41776919a0a6SAlan Cox 	{ "Toshiba CD-ROM XM-6202B", NULL,	ATA_HORKAGE_NODMA },
41786919a0a6SAlan Cox 	{ "TOSHIBA CD-ROM XM-1702BC", NULL,	ATA_HORKAGE_NODMA },
41796919a0a6SAlan Cox 	{ "CD-532E-A", 		NULL,		ATA_HORKAGE_NODMA },
41806919a0a6SAlan Cox 	{ "E-IDE CD-ROM CR-840",NULL,		ATA_HORKAGE_NODMA },
41816919a0a6SAlan Cox 	{ "CD-ROM Drive/F5A",	NULL,		ATA_HORKAGE_NODMA },
41826919a0a6SAlan Cox 	{ "WPI CDD-820", 	NULL,		ATA_HORKAGE_NODMA },
41836919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SC-148C", NULL,	ATA_HORKAGE_NODMA },
41846919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SC",	NULL,		ATA_HORKAGE_NODMA },
41856919a0a6SAlan Cox 	{ "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
41866919a0a6SAlan Cox 	{ "_NEC DV5800A", 	NULL,		ATA_HORKAGE_NODMA },
41876919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SN-124", "N001",	ATA_HORKAGE_NODMA },
418839f19886SDave Jones 	{ "Seagate STT20000A", NULL,		ATA_HORKAGE_NODMA },
41893af9a77aSTejun Heo 	/* Odd clown on sil3726/4726 PMPs */
419050af2fa1STejun Heo 	{ "Config  Disk",	NULL,		ATA_HORKAGE_DISABLE },
41916919a0a6SAlan Cox 
419218d6e9d5SAlbert Lee 	/* Weird ATAPI devices */
419340a1d531STejun Heo 	{ "TORiSAN DVD-ROM DRD-N216", NULL,	ATA_HORKAGE_MAX_SEC_128 },
41946a87e42eSTejun Heo 	{ "QUANTUM DAT    DAT72-000", NULL,	ATA_HORKAGE_ATAPI_MOD16_DMA },
419518d6e9d5SAlbert Lee 
41966919a0a6SAlan Cox 	/* Devices we expect to fail diagnostics */
41976919a0a6SAlan Cox 
41986919a0a6SAlan Cox 	/* Devices where NCQ should be avoided */
41996919a0a6SAlan Cox 	/* NCQ is slow */
42006919a0a6SAlan Cox 	{ "WDC WD740ADFD-00",	NULL,		ATA_HORKAGE_NONCQ },
4201459ad688STejun Heo 	{ "WDC WD740ADFD-00NLR1", NULL,		ATA_HORKAGE_NONCQ, },
420209125ea6STejun Heo 	/* http://thread.gmane.org/gmane.linux.ide/14907 */
420309125ea6STejun Heo 	{ "FUJITSU MHT2060BH",	NULL,		ATA_HORKAGE_NONCQ },
42047acfaf30SPaul Rolland 	/* NCQ is broken */
4205539cc7c7SJeff Garzik 	{ "Maxtor *",		"BANC*",	ATA_HORKAGE_NONCQ },
42060e3dbc01SAlan Cox 	{ "Maxtor 7V300F0",	"VA111630",	ATA_HORKAGE_NONCQ },
4207da6f0ec2SPaolo Ornati 	{ "ST380817AS",		"3.42",		ATA_HORKAGE_NONCQ },
4208e41bd3e8STejun Heo 	{ "ST3160023AS",	"3.42",		ATA_HORKAGE_NONCQ },
42095ccfca97SLubomir Bulej 	{ "OCZ CORE_SSD",	"02.10104",	ATA_HORKAGE_NONCQ },
4210539cc7c7SJeff Garzik 
4211ac70a964STejun Heo 	/* Seagate NCQ + FLUSH CACHE firmware bug */
4212dbaf5083SMark Lord 	{ "ST31500341AS",	"SD1[56789]",	ATA_HORKAGE_NONCQ |
4213ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4214d10d491fSTejun Heo 
4215dbaf5083SMark Lord 	{ "ST31000333AS",	"SD1[56789]",	ATA_HORKAGE_NONCQ |
4216d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4217d10d491fSTejun Heo 
4218dbaf5083SMark Lord 	{ "ST3640623AS",	"SD1[56789]",	ATA_HORKAGE_NONCQ |
4219d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4220d10d491fSTejun Heo 
4221dbaf5083SMark Lord 	{ "ST3640323AS",	"SD1[56789]",	ATA_HORKAGE_NONCQ |
4222d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4223d10d491fSTejun Heo 
4224dbaf5083SMark Lord 	{ "ST3320813AS",	"SD1[56789]",	ATA_HORKAGE_NONCQ |
4225d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4226d10d491fSTejun Heo 
4227dbaf5083SMark Lord 	{ "ST3320613AS",	"SD1[56789]",	ATA_HORKAGE_NONCQ |
4228ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4229ac70a964STejun Heo 
423036e337d0SRobert Hancock 	/* Blacklist entries taken from Silicon Image 3124/3132
423136e337d0SRobert Hancock 	   Windows driver .inf file - also several Linux problem reports */
423236e337d0SRobert Hancock 	{ "HTS541060G9SA00",    "MB3OC60D",     ATA_HORKAGE_NONCQ, },
423336e337d0SRobert Hancock 	{ "HTS541080G9SA00",    "MB4OC60D",     ATA_HORKAGE_NONCQ, },
423436e337d0SRobert Hancock 	{ "HTS541010G9SA00",    "MBZOC60D",     ATA_HORKAGE_NONCQ, },
42356919a0a6SAlan Cox 
423668b0ddb2STejun Heo 	/* https://bugzilla.kernel.org/show_bug.cgi?id=15573 */
423768b0ddb2STejun Heo 	{ "C300-CTFDDAC128MAG",	"0001",		ATA_HORKAGE_NONCQ, },
423868b0ddb2STejun Heo 
423916c55b03STejun Heo 	/* devices which puke on READ_NATIVE_MAX */
424016c55b03STejun Heo 	{ "HDS724040KLSA80",	"KFAOA20N",	ATA_HORKAGE_BROKEN_HPA, },
424116c55b03STejun Heo 	{ "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
424216c55b03STejun Heo 	{ "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
424316c55b03STejun Heo 	{ "MAXTOR 6L080L4",	"A93.0500",	ATA_HORKAGE_BROKEN_HPA },
42446919a0a6SAlan Cox 
42457831387bSTejun Heo 	/* this one allows HPA unlocking but fails IOs on the area */
42467831387bSTejun Heo 	{ "OCZ-VERTEX",		    "1.30",	ATA_HORKAGE_BROKEN_HPA },
42477831387bSTejun Heo 
424893328e11SAlan Cox 	/* Devices which report 1 sector over size HPA */
424993328e11SAlan Cox 	{ "ST340823A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
425093328e11SAlan Cox 	{ "ST320413A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
4251b152fcd3SMikko Rapeli 	{ "ST310211A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
425293328e11SAlan Cox 
42536bbfd53dSAlan Cox 	/* Devices which get the IVB wrong */
42546bbfd53dSAlan Cox 	{ "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB, },
4255a79067e5SAlan Cox 	/* Maybe we should just blacklist TSSTcorp... */
42567da4c935SMark Lord 	{ "TSSTcorp CDDVDW SH-S202[HJN]", "SB0[01]",  ATA_HORKAGE_IVB, },
42576bbfd53dSAlan Cox 
42589ce8e307SJens Axboe 	/* Devices that do not need bridging limits applied */
42599ce8e307SJens Axboe 	{ "MTRON MSP-SATA*",		NULL,	ATA_HORKAGE_BRIDGE_OK, },
42609ce8e307SJens Axboe 
42619062712fSTejun Heo 	/* Devices which aren't very happy with higher link speeds */
42629062712fSTejun Heo 	{ "WD My Book",			NULL,	ATA_HORKAGE_1_5_GBPS, },
42639062712fSTejun Heo 
4264d0cb43b3STejun Heo 	/*
4265d0cb43b3STejun Heo 	 * Devices which choke on SETXFER.  Applies only if both the
4266d0cb43b3STejun Heo 	 * device and controller are SATA.
4267d0cb43b3STejun Heo 	 */
4268d0cb43b3STejun Heo 	{ "PIONEER DVD-RW  DVRTD08",	"1.00",	ATA_HORKAGE_NOSETXFER },
4269d0cb43b3STejun Heo 
42706919a0a6SAlan Cox 	/* End Marker */
42716919a0a6SAlan Cox 	{ }
4272c6fd2807SJeff Garzik };
4273c6fd2807SJeff Garzik 
4274bce036ceSMark Lord /**
4275bce036ceSMark Lord  *	glob_match - match a text string against a glob-style pattern
4276bce036ceSMark Lord  *	@text: the string to be examined
4277bce036ceSMark Lord  *	@pattern: the glob-style pattern to be matched against
4278bce036ceSMark Lord  *
4279bce036ceSMark Lord  *	Either/both of text and pattern can be empty strings.
4280bce036ceSMark Lord  *
4281bce036ceSMark Lord  *	Match text against a glob-style pattern, with wildcards and simple sets:
4282bce036ceSMark Lord  *
4283bce036ceSMark Lord  *		?	matches any single character.
4284bce036ceSMark Lord  *		*	matches any run of characters.
4285bce036ceSMark Lord  *		[xyz]	matches a single character from the set: x, y, or z.
42862f9e4d16SMark Lord  *		[a-d]	matches a single character from the range: a, b, c, or d.
42872f9e4d16SMark Lord  *		[a-d0-9] matches a single character from either range.
4288bce036ceSMark Lord  *
42892f9e4d16SMark Lord  *	The special characters ?, [, -, or *, can be matched using a set, eg. [*]
42902f9e4d16SMark Lord  *	Behaviour with malformed patterns is undefined, though generally reasonable.
4291bce036ceSMark Lord  *
42922f9e4d16SMark Lord  *	Example patterns:  "SD1?",  "SD1[0-5]",  "*R0",  SD*1?[012]*xx"
4293bce036ceSMark Lord  *
4294bce036ceSMark Lord  *	This function uses one level of recursion per '*' in pattern.
4295bce036ceSMark Lord  *	Since it calls _nothing_ else, and has _no_ explicit local variables,
4296bce036ceSMark Lord  *	this will not cause stack problems for any reasonable use here.
4297bce036ceSMark Lord  *
4298bce036ceSMark Lord  *	RETURNS:
4299bce036ceSMark Lord  *	0 on match, 1 otherwise.
4300539cc7c7SJeff Garzik  */
4301bce036ceSMark Lord static int glob_match (const char *text, const char *pattern)
4302bce036ceSMark Lord {
4303bce036ceSMark Lord 	do {
4304bce036ceSMark Lord 		/* Match single character or a '?' wildcard */
4305bce036ceSMark Lord 		if (*text == *pattern || *pattern == '?') {
4306bce036ceSMark Lord 			if (!*pattern++)
4307bce036ceSMark Lord 				return 0;  /* End of both strings: match */
4308bce036ceSMark Lord 		} else {
4309bce036ceSMark Lord 			/* Match single char against a '[' bracketed ']' pattern set */
4310bce036ceSMark Lord 			if (!*text || *pattern != '[')
4311bce036ceSMark Lord 				break;  /* Not a pattern set */
43122f9e4d16SMark Lord 			while (*++pattern && *pattern != ']' && *text != *pattern) {
43132f9e4d16SMark Lord 				if (*pattern == '-' && *(pattern - 1) != '[')
43142f9e4d16SMark Lord 					if (*text > *(pattern - 1) && *text < *(pattern + 1)) {
43152f9e4d16SMark Lord 						++pattern;
43162f9e4d16SMark Lord 						break;
43172f9e4d16SMark Lord 					}
43182f9e4d16SMark Lord 			}
4319bce036ceSMark Lord 			if (!*pattern || *pattern == ']')
4320bce036ceSMark Lord 				return 1;  /* No match */
4321bce036ceSMark Lord 			while (*pattern && *pattern++ != ']');
4322317b50b8SAndrew Paprocki 		}
4323bce036ceSMark Lord 	} while (*++text && *pattern);
4324539cc7c7SJeff Garzik 
4325bce036ceSMark Lord 	/* Match any run of chars against a '*' wildcard */
4326bce036ceSMark Lord 	if (*pattern == '*') {
4327bce036ceSMark Lord 		if (!*++pattern)
4328bce036ceSMark Lord 			return 0;  /* Match: avoid recursion at end of pattern */
4329bce036ceSMark Lord 		/* Loop to handle additional pattern chars after the wildcard */
4330bce036ceSMark Lord 		while (*text) {
4331bce036ceSMark Lord 			if (glob_match(text, pattern) == 0)
4332bce036ceSMark Lord 				return 0;  /* Remainder matched */
4333bce036ceSMark Lord 			++text;  /* Absorb (match) this char and try again */
4334bce036ceSMark Lord 		}
4335bce036ceSMark Lord 	}
4336bce036ceSMark Lord 	if (!*text && !*pattern)
4337bce036ceSMark Lord 		return 0;  /* End of both strings: match */
4338bce036ceSMark Lord 	return 1;  /* No match */
4339539cc7c7SJeff Garzik }
4340539cc7c7SJeff Garzik 
434175683fe7STejun Heo static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
4342c6fd2807SJeff Garzik {
43438bfa79fcSTejun Heo 	unsigned char model_num[ATA_ID_PROD_LEN + 1];
43448bfa79fcSTejun Heo 	unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
43456919a0a6SAlan Cox 	const struct ata_blacklist_entry *ad = ata_device_blacklist;
4346c6fd2807SJeff Garzik 
43478bfa79fcSTejun Heo 	ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
43488bfa79fcSTejun Heo 	ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
4349c6fd2807SJeff Garzik 
43506919a0a6SAlan Cox 	while (ad->model_num) {
4351bce036ceSMark Lord 		if (!glob_match(model_num, ad->model_num)) {
43526919a0a6SAlan Cox 			if (ad->model_rev == NULL)
43536919a0a6SAlan Cox 				return ad->horkage;
4354bce036ceSMark Lord 			if (!glob_match(model_rev, ad->model_rev))
43556919a0a6SAlan Cox 				return ad->horkage;
4356c6fd2807SJeff Garzik 		}
43576919a0a6SAlan Cox 		ad++;
4358c6fd2807SJeff Garzik 	}
4359c6fd2807SJeff Garzik 	return 0;
4360c6fd2807SJeff Garzik }
4361c6fd2807SJeff Garzik 
43626919a0a6SAlan Cox static int ata_dma_blacklisted(const struct ata_device *dev)
43636919a0a6SAlan Cox {
43646919a0a6SAlan Cox 	/* We don't support polling DMA.
43656919a0a6SAlan Cox 	 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
43666919a0a6SAlan Cox 	 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
43676919a0a6SAlan Cox 	 */
43689af5c9c9STejun Heo 	if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
43696919a0a6SAlan Cox 	    (dev->flags & ATA_DFLAG_CDB_INTR))
43706919a0a6SAlan Cox 		return 1;
437175683fe7STejun Heo 	return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
43726919a0a6SAlan Cox }
43736919a0a6SAlan Cox 
4374c6fd2807SJeff Garzik /**
43756bbfd53dSAlan Cox  *	ata_is_40wire		-	check drive side detection
43766bbfd53dSAlan Cox  *	@dev: device
43776bbfd53dSAlan Cox  *
43786bbfd53dSAlan Cox  *	Perform drive side detection decoding, allowing for device vendors
43796bbfd53dSAlan Cox  *	who can't follow the documentation.
43806bbfd53dSAlan Cox  */
43816bbfd53dSAlan Cox 
43826bbfd53dSAlan Cox static int ata_is_40wire(struct ata_device *dev)
43836bbfd53dSAlan Cox {
43846bbfd53dSAlan Cox 	if (dev->horkage & ATA_HORKAGE_IVB)
43856bbfd53dSAlan Cox 		return ata_drive_40wire_relaxed(dev->id);
43866bbfd53dSAlan Cox 	return ata_drive_40wire(dev->id);
43876bbfd53dSAlan Cox }
43886bbfd53dSAlan Cox 
43896bbfd53dSAlan Cox /**
439015a5551cSAlan Cox  *	cable_is_40wire		-	40/80/SATA decider
439115a5551cSAlan Cox  *	@ap: port to consider
439215a5551cSAlan Cox  *
439315a5551cSAlan Cox  *	This function encapsulates the policy for speed management
439415a5551cSAlan Cox  *	in one place. At the moment we don't cache the result but
439515a5551cSAlan Cox  *	there is a good case for setting ap->cbl to the result when
439615a5551cSAlan Cox  *	we are called with unknown cables (and figuring out if it
439715a5551cSAlan Cox  *	impacts hotplug at all).
439815a5551cSAlan Cox  *
439915a5551cSAlan Cox  *	Return 1 if the cable appears to be 40 wire.
440015a5551cSAlan Cox  */
440115a5551cSAlan Cox 
440215a5551cSAlan Cox static int cable_is_40wire(struct ata_port *ap)
440315a5551cSAlan Cox {
440415a5551cSAlan Cox 	struct ata_link *link;
440515a5551cSAlan Cox 	struct ata_device *dev;
440615a5551cSAlan Cox 
44074a9c7b33STejun Heo 	/* If the controller thinks we are 40 wire, we are. */
440815a5551cSAlan Cox 	if (ap->cbl == ATA_CBL_PATA40)
440915a5551cSAlan Cox 		return 1;
44104a9c7b33STejun Heo 
44114a9c7b33STejun Heo 	/* If the controller thinks we are 80 wire, we are. */
441215a5551cSAlan Cox 	if (ap->cbl == ATA_CBL_PATA80 || ap->cbl == ATA_CBL_SATA)
441315a5551cSAlan Cox 		return 0;
44144a9c7b33STejun Heo 
44154a9c7b33STejun Heo 	/* If the system is known to be 40 wire short cable (eg
44164a9c7b33STejun Heo 	 * laptop), then we allow 80 wire modes even if the drive
44174a9c7b33STejun Heo 	 * isn't sure.
44184a9c7b33STejun Heo 	 */
4419f792068eSAlan Cox 	if (ap->cbl == ATA_CBL_PATA40_SHORT)
4420f792068eSAlan Cox 		return 0;
442115a5551cSAlan Cox 
44224a9c7b33STejun Heo 	/* If the controller doesn't know, we scan.
44234a9c7b33STejun Heo 	 *
44244a9c7b33STejun Heo 	 * Note: We look for all 40 wire detects at this point.  Any
44254a9c7b33STejun Heo 	 *       80 wire detect is taken to be 80 wire cable because
44264a9c7b33STejun Heo 	 * - in many setups only the one drive (slave if present) will
44274a9c7b33STejun Heo 	 *   give a valid detect
44284a9c7b33STejun Heo 	 * - if you have a non detect capable drive you don't want it
44294a9c7b33STejun Heo 	 *   to colour the choice
443015a5551cSAlan Cox 	 */
44311eca4365STejun Heo 	ata_for_each_link(link, ap, EDGE) {
44321eca4365STejun Heo 		ata_for_each_dev(dev, link, ENABLED) {
44331eca4365STejun Heo 			if (!ata_is_40wire(dev))
443415a5551cSAlan Cox 				return 0;
443515a5551cSAlan Cox 		}
443615a5551cSAlan Cox 	}
443715a5551cSAlan Cox 	return 1;
443815a5551cSAlan Cox }
443915a5551cSAlan Cox 
444015a5551cSAlan Cox /**
4441c6fd2807SJeff Garzik  *	ata_dev_xfermask - Compute supported xfermask of the given device
4442c6fd2807SJeff Garzik  *	@dev: Device to compute xfermask for
4443c6fd2807SJeff Garzik  *
4444c6fd2807SJeff Garzik  *	Compute supported xfermask of @dev and store it in
4445c6fd2807SJeff Garzik  *	dev->*_mask.  This function is responsible for applying all
4446c6fd2807SJeff Garzik  *	known limits including host controller limits, device
4447c6fd2807SJeff Garzik  *	blacklist, etc...
4448c6fd2807SJeff Garzik  *
4449c6fd2807SJeff Garzik  *	LOCKING:
4450c6fd2807SJeff Garzik  *	None.
4451c6fd2807SJeff Garzik  */
4452c6fd2807SJeff Garzik static void ata_dev_xfermask(struct ata_device *dev)
4453c6fd2807SJeff Garzik {
44549af5c9c9STejun Heo 	struct ata_link *link = dev->link;
44559af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
4456cca3974eSJeff Garzik 	struct ata_host *host = ap->host;
4457c6fd2807SJeff Garzik 	unsigned long xfer_mask;
4458c6fd2807SJeff Garzik 
4459c6fd2807SJeff Garzik 	/* controller modes available */
4460c6fd2807SJeff Garzik 	xfer_mask = ata_pack_xfermask(ap->pio_mask,
4461c6fd2807SJeff Garzik 				      ap->mwdma_mask, ap->udma_mask);
4462c6fd2807SJeff Garzik 
44638343f889SRobert Hancock 	/* drive modes available */
4464c6fd2807SJeff Garzik 	xfer_mask &= ata_pack_xfermask(dev->pio_mask,
4465c6fd2807SJeff Garzik 				       dev->mwdma_mask, dev->udma_mask);
4466c6fd2807SJeff Garzik 	xfer_mask &= ata_id_xfermask(dev->id);
4467c6fd2807SJeff Garzik 
4468b352e57dSAlan Cox 	/*
4469b352e57dSAlan Cox 	 *	CFA Advanced TrueIDE timings are not allowed on a shared
4470b352e57dSAlan Cox 	 *	cable
4471b352e57dSAlan Cox 	 */
4472b352e57dSAlan Cox 	if (ata_dev_pair(dev)) {
4473b352e57dSAlan Cox 		/* No PIO5 or PIO6 */
4474b352e57dSAlan Cox 		xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
4475b352e57dSAlan Cox 		/* No MWDMA3 or MWDMA 4 */
4476b352e57dSAlan Cox 		xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
4477b352e57dSAlan Cox 	}
4478b352e57dSAlan Cox 
4479c6fd2807SJeff Garzik 	if (ata_dma_blacklisted(dev)) {
4480c6fd2807SJeff Garzik 		xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4481c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_WARNING,
4482c6fd2807SJeff Garzik 			       "device is on DMA blacklist, disabling DMA\n");
4483c6fd2807SJeff Garzik 	}
4484c6fd2807SJeff Garzik 
448514d66ab7SPetr Vandrovec 	if ((host->flags & ATA_HOST_SIMPLEX) &&
448614d66ab7SPetr Vandrovec 	    host->simplex_claimed && host->simplex_claimed != ap) {
4487c6fd2807SJeff Garzik 		xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4488c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_WARNING, "simplex DMA is claimed by "
4489c6fd2807SJeff Garzik 			       "other device, disabling DMA\n");
4490c6fd2807SJeff Garzik 	}
4491c6fd2807SJeff Garzik 
4492e424675fSJeff Garzik 	if (ap->flags & ATA_FLAG_NO_IORDY)
4493e424675fSJeff Garzik 		xfer_mask &= ata_pio_mask_no_iordy(dev);
4494e424675fSJeff Garzik 
4495c6fd2807SJeff Garzik 	if (ap->ops->mode_filter)
4496a76b62caSAlan Cox 		xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
4497c6fd2807SJeff Garzik 
44988343f889SRobert Hancock 	/* Apply cable rule here.  Don't apply it early because when
44998343f889SRobert Hancock 	 * we handle hot plug the cable type can itself change.
45008343f889SRobert Hancock 	 * Check this last so that we know if the transfer rate was
45018343f889SRobert Hancock 	 * solely limited by the cable.
45028343f889SRobert Hancock 	 * Unknown or 80 wire cables reported host side are checked
45038343f889SRobert Hancock 	 * drive side as well. Cases where we know a 40wire cable
45048343f889SRobert Hancock 	 * is used safely for 80 are not checked here.
45058343f889SRobert Hancock 	 */
45068343f889SRobert Hancock 	if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
45078343f889SRobert Hancock 		/* UDMA/44 or higher would be available */
450815a5551cSAlan Cox 		if (cable_is_40wire(ap)) {
45098343f889SRobert Hancock 			ata_dev_printk(dev, KERN_WARNING,
45108343f889SRobert Hancock 				 "limited to UDMA/33 due to 40-wire cable\n");
45118343f889SRobert Hancock 			xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
45128343f889SRobert Hancock 		}
45138343f889SRobert Hancock 
4514c6fd2807SJeff Garzik 	ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
4515c6fd2807SJeff Garzik 			    &dev->mwdma_mask, &dev->udma_mask);
4516c6fd2807SJeff Garzik }
4517c6fd2807SJeff Garzik 
4518c6fd2807SJeff Garzik /**
4519c6fd2807SJeff Garzik  *	ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
4520c6fd2807SJeff Garzik  *	@dev: Device to which command will be sent
4521c6fd2807SJeff Garzik  *
4522c6fd2807SJeff Garzik  *	Issue SET FEATURES - XFER MODE command to device @dev
4523c6fd2807SJeff Garzik  *	on port @ap.
4524c6fd2807SJeff Garzik  *
4525c6fd2807SJeff Garzik  *	LOCKING:
4526c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
4527c6fd2807SJeff Garzik  *
4528c6fd2807SJeff Garzik  *	RETURNS:
4529c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
4530c6fd2807SJeff Garzik  */
4531c6fd2807SJeff Garzik 
4532c6fd2807SJeff Garzik static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
4533c6fd2807SJeff Garzik {
4534c6fd2807SJeff Garzik 	struct ata_taskfile tf;
4535c6fd2807SJeff Garzik 	unsigned int err_mask;
4536c6fd2807SJeff Garzik 
4537c6fd2807SJeff Garzik 	/* set up set-features taskfile */
4538c6fd2807SJeff Garzik 	DPRINTK("set features - xfer mode\n");
4539c6fd2807SJeff Garzik 
4540464cf177STejun Heo 	/* Some controllers and ATAPI devices show flaky interrupt
4541464cf177STejun Heo 	 * behavior after setting xfer mode.  Use polling instead.
4542464cf177STejun Heo 	 */
4543c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
4544c6fd2807SJeff Garzik 	tf.command = ATA_CMD_SET_FEATURES;
4545c6fd2807SJeff Garzik 	tf.feature = SETFEATURES_XFER;
4546464cf177STejun Heo 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
4547c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
4548b9f8ab2dSAlan Cox 	/* If we are using IORDY we must send the mode setting command */
4549b9f8ab2dSAlan Cox 	if (ata_pio_need_iordy(dev))
4550c6fd2807SJeff Garzik 		tf.nsect = dev->xfer_mode;
4551b9f8ab2dSAlan Cox 	/* If the device has IORDY and the controller does not - turn it off */
4552b9f8ab2dSAlan Cox  	else if (ata_id_has_iordy(dev->id))
4553b9f8ab2dSAlan Cox 		tf.nsect = 0x01;
4554b9f8ab2dSAlan Cox 	else /* In the ancient relic department - skip all of this */
4555b9f8ab2dSAlan Cox 		return 0;
4556c6fd2807SJeff Garzik 
45572b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
4558c6fd2807SJeff Garzik 
4559c6fd2807SJeff Garzik 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
4560c6fd2807SJeff Garzik 	return err_mask;
4561c6fd2807SJeff Garzik }
4562c6fd2807SJeff Garzik /**
4563218f3d30SJeff Garzik  *	ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES
45649f45cbd3SKristen Carlson Accardi  *	@dev: Device to which command will be sent
45659f45cbd3SKristen Carlson Accardi  *	@enable: Whether to enable or disable the feature
4566218f3d30SJeff Garzik  *	@feature: The sector count represents the feature to set
45679f45cbd3SKristen Carlson Accardi  *
45689f45cbd3SKristen Carlson Accardi  *	Issue SET FEATURES - SATA FEATURES command to device @dev
4569218f3d30SJeff Garzik  *	on port @ap with sector count
45709f45cbd3SKristen Carlson Accardi  *
45719f45cbd3SKristen Carlson Accardi  *	LOCKING:
45729f45cbd3SKristen Carlson Accardi  *	PCI/etc. bus probe sem.
45739f45cbd3SKristen Carlson Accardi  *
45749f45cbd3SKristen Carlson Accardi  *	RETURNS:
45759f45cbd3SKristen Carlson Accardi  *	0 on success, AC_ERR_* mask otherwise.
45769f45cbd3SKristen Carlson Accardi  */
4577218f3d30SJeff Garzik static unsigned int ata_dev_set_feature(struct ata_device *dev, u8 enable,
4578218f3d30SJeff Garzik 					u8 feature)
45799f45cbd3SKristen Carlson Accardi {
45809f45cbd3SKristen Carlson Accardi 	struct ata_taskfile tf;
45819f45cbd3SKristen Carlson Accardi 	unsigned int err_mask;
45829f45cbd3SKristen Carlson Accardi 
45839f45cbd3SKristen Carlson Accardi 	/* set up set-features taskfile */
45849f45cbd3SKristen Carlson Accardi 	DPRINTK("set features - SATA features\n");
45859f45cbd3SKristen Carlson Accardi 
45869f45cbd3SKristen Carlson Accardi 	ata_tf_init(dev, &tf);
45879f45cbd3SKristen Carlson Accardi 	tf.command = ATA_CMD_SET_FEATURES;
45889f45cbd3SKristen Carlson Accardi 	tf.feature = enable;
45899f45cbd3SKristen Carlson Accardi 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
45909f45cbd3SKristen Carlson Accardi 	tf.protocol = ATA_PROT_NODATA;
4591218f3d30SJeff Garzik 	tf.nsect = feature;
45929f45cbd3SKristen Carlson Accardi 
45932b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
45949f45cbd3SKristen Carlson Accardi 
45959f45cbd3SKristen Carlson Accardi 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
45969f45cbd3SKristen Carlson Accardi 	return err_mask;
45979f45cbd3SKristen Carlson Accardi }
45989f45cbd3SKristen Carlson Accardi 
45999f45cbd3SKristen Carlson Accardi /**
4600c6fd2807SJeff Garzik  *	ata_dev_init_params - Issue INIT DEV PARAMS command
4601c6fd2807SJeff Garzik  *	@dev: Device to which command will be sent
4602c6fd2807SJeff Garzik  *	@heads: Number of heads (taskfile parameter)
4603c6fd2807SJeff Garzik  *	@sectors: Number of sectors (taskfile parameter)
4604c6fd2807SJeff Garzik  *
4605c6fd2807SJeff Garzik  *	LOCKING:
4606c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
4607c6fd2807SJeff Garzik  *
4608c6fd2807SJeff Garzik  *	RETURNS:
4609c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
4610c6fd2807SJeff Garzik  */
4611c6fd2807SJeff Garzik static unsigned int ata_dev_init_params(struct ata_device *dev,
4612c6fd2807SJeff Garzik 					u16 heads, u16 sectors)
4613c6fd2807SJeff Garzik {
4614c6fd2807SJeff Garzik 	struct ata_taskfile tf;
4615c6fd2807SJeff Garzik 	unsigned int err_mask;
4616c6fd2807SJeff Garzik 
4617c6fd2807SJeff Garzik 	/* Number of sectors per track 1-255. Number of heads 1-16 */
4618c6fd2807SJeff Garzik 	if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
4619c6fd2807SJeff Garzik 		return AC_ERR_INVALID;
4620c6fd2807SJeff Garzik 
4621c6fd2807SJeff Garzik 	/* set up init dev params taskfile */
4622c6fd2807SJeff Garzik 	DPRINTK("init dev params \n");
4623c6fd2807SJeff Garzik 
4624c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
4625c6fd2807SJeff Garzik 	tf.command = ATA_CMD_INIT_DEV_PARAMS;
4626c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4627c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
4628c6fd2807SJeff Garzik 	tf.nsect = sectors;
4629c6fd2807SJeff Garzik 	tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
4630c6fd2807SJeff Garzik 
46312b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
463218b2466cSAlan Cox 	/* A clean abort indicates an original or just out of spec drive
463318b2466cSAlan Cox 	   and we should continue as we issue the setup based on the
463418b2466cSAlan Cox 	   drive reported working geometry */
463518b2466cSAlan Cox 	if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
463618b2466cSAlan Cox 		err_mask = 0;
4637c6fd2807SJeff Garzik 
4638c6fd2807SJeff Garzik 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
4639c6fd2807SJeff Garzik 	return err_mask;
4640c6fd2807SJeff Garzik }
4641c6fd2807SJeff Garzik 
4642c6fd2807SJeff Garzik /**
4643c6fd2807SJeff Garzik  *	ata_sg_clean - Unmap DMA memory associated with command
4644c6fd2807SJeff Garzik  *	@qc: Command containing DMA memory to be released
4645c6fd2807SJeff Garzik  *
4646c6fd2807SJeff Garzik  *	Unmap all mapped DMA memory associated with this command.
4647c6fd2807SJeff Garzik  *
4648c6fd2807SJeff Garzik  *	LOCKING:
4649cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4650c6fd2807SJeff Garzik  */
465170e6ad0cSTejun Heo void ata_sg_clean(struct ata_queued_cmd *qc)
4652c6fd2807SJeff Garzik {
4653c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4654ff2aeb1eSTejun Heo 	struct scatterlist *sg = qc->sg;
4655c6fd2807SJeff Garzik 	int dir = qc->dma_dir;
4656c6fd2807SJeff Garzik 
4657efcb3cf7STejun Heo 	WARN_ON_ONCE(sg == NULL);
4658c6fd2807SJeff Garzik 
4659dde20207SJames Bottomley 	VPRINTK("unmapping %u sg elements\n", qc->n_elem);
4660c6fd2807SJeff Garzik 
4661dde20207SJames Bottomley 	if (qc->n_elem)
46625825627cSFUJITA Tomonori 		dma_unmap_sg(ap->dev, sg, qc->orig_n_elem, dir);
4663c6fd2807SJeff Garzik 
4664c6fd2807SJeff Garzik 	qc->flags &= ~ATA_QCFLAG_DMAMAP;
4665ff2aeb1eSTejun Heo 	qc->sg = NULL;
4666c6fd2807SJeff Garzik }
4667c6fd2807SJeff Garzik 
4668c6fd2807SJeff Garzik /**
46695895ef9aSTejun Heo  *	atapi_check_dma - Check whether ATAPI DMA can be supported
4670c6fd2807SJeff Garzik  *	@qc: Metadata associated with taskfile to check
4671c6fd2807SJeff Garzik  *
4672c6fd2807SJeff Garzik  *	Allow low-level driver to filter ATA PACKET commands, returning
4673c6fd2807SJeff Garzik  *	a status indicating whether or not it is OK to use DMA for the
4674c6fd2807SJeff Garzik  *	supplied PACKET command.
4675c6fd2807SJeff Garzik  *
4676c6fd2807SJeff Garzik  *	LOCKING:
4677cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4678c6fd2807SJeff Garzik  *
4679c6fd2807SJeff Garzik  *	RETURNS: 0 when ATAPI DMA can be used
4680c6fd2807SJeff Garzik  *               nonzero otherwise
4681c6fd2807SJeff Garzik  */
46825895ef9aSTejun Heo int atapi_check_dma(struct ata_queued_cmd *qc)
4683c6fd2807SJeff Garzik {
4684c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4685c6fd2807SJeff Garzik 
4686b9a4197eSTejun Heo 	/* Don't allow DMA if it isn't multiple of 16 bytes.  Quite a
4687b9a4197eSTejun Heo 	 * few ATAPI devices choke on such DMA requests.
4688b9a4197eSTejun Heo 	 */
46896a87e42eSTejun Heo 	if (!(qc->dev->horkage & ATA_HORKAGE_ATAPI_MOD16_DMA) &&
46906a87e42eSTejun Heo 	    unlikely(qc->nbytes & 15))
46916f23a31dSAlbert Lee 		return 1;
46926f23a31dSAlbert Lee 
4693c6fd2807SJeff Garzik 	if (ap->ops->check_atapi_dma)
4694b9a4197eSTejun Heo 		return ap->ops->check_atapi_dma(qc);
4695c6fd2807SJeff Garzik 
4696b9a4197eSTejun Heo 	return 0;
4697c6fd2807SJeff Garzik }
4698b9a4197eSTejun Heo 
4699c6fd2807SJeff Garzik /**
470031cc23b3STejun Heo  *	ata_std_qc_defer - Check whether a qc needs to be deferred
470131cc23b3STejun Heo  *	@qc: ATA command in question
470231cc23b3STejun Heo  *
470331cc23b3STejun Heo  *	Non-NCQ commands cannot run with any other command, NCQ or
470431cc23b3STejun Heo  *	not.  As upper layer only knows the queue depth, we are
470531cc23b3STejun Heo  *	responsible for maintaining exclusion.  This function checks
470631cc23b3STejun Heo  *	whether a new command @qc can be issued.
470731cc23b3STejun Heo  *
470831cc23b3STejun Heo  *	LOCKING:
470931cc23b3STejun Heo  *	spin_lock_irqsave(host lock)
471031cc23b3STejun Heo  *
471131cc23b3STejun Heo  *	RETURNS:
471231cc23b3STejun Heo  *	ATA_DEFER_* if deferring is needed, 0 otherwise.
471331cc23b3STejun Heo  */
471431cc23b3STejun Heo int ata_std_qc_defer(struct ata_queued_cmd *qc)
471531cc23b3STejun Heo {
471631cc23b3STejun Heo 	struct ata_link *link = qc->dev->link;
471731cc23b3STejun Heo 
471831cc23b3STejun Heo 	if (qc->tf.protocol == ATA_PROT_NCQ) {
471931cc23b3STejun Heo 		if (!ata_tag_valid(link->active_tag))
472031cc23b3STejun Heo 			return 0;
472131cc23b3STejun Heo 	} else {
472231cc23b3STejun Heo 		if (!ata_tag_valid(link->active_tag) && !link->sactive)
472331cc23b3STejun Heo 			return 0;
472431cc23b3STejun Heo 	}
472531cc23b3STejun Heo 
472631cc23b3STejun Heo 	return ATA_DEFER_LINK;
472731cc23b3STejun Heo }
472831cc23b3STejun Heo 
4729c6fd2807SJeff Garzik void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
4730c6fd2807SJeff Garzik 
4731c6fd2807SJeff Garzik /**
4732c6fd2807SJeff Garzik  *	ata_sg_init - Associate command with scatter-gather table.
4733c6fd2807SJeff Garzik  *	@qc: Command to be associated
4734c6fd2807SJeff Garzik  *	@sg: Scatter-gather table.
4735c6fd2807SJeff Garzik  *	@n_elem: Number of elements in s/g table.
4736c6fd2807SJeff Garzik  *
4737c6fd2807SJeff Garzik  *	Initialize the data-related elements of queued_cmd @qc
4738c6fd2807SJeff Garzik  *	to point to a scatter-gather table @sg, containing @n_elem
4739c6fd2807SJeff Garzik  *	elements.
4740c6fd2807SJeff Garzik  *
4741c6fd2807SJeff Garzik  *	LOCKING:
4742cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4743c6fd2807SJeff Garzik  */
4744c6fd2807SJeff Garzik void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
4745c6fd2807SJeff Garzik 		 unsigned int n_elem)
4746c6fd2807SJeff Garzik {
4747ff2aeb1eSTejun Heo 	qc->sg = sg;
4748c6fd2807SJeff Garzik 	qc->n_elem = n_elem;
4749ff2aeb1eSTejun Heo 	qc->cursg = qc->sg;
4750ff2aeb1eSTejun Heo }
4751ff2aeb1eSTejun Heo 
4752c6fd2807SJeff Garzik /**
4753c6fd2807SJeff Garzik  *	ata_sg_setup - DMA-map the scatter-gather table associated with a command.
4754c6fd2807SJeff Garzik  *	@qc: Command with scatter-gather table to be mapped.
4755c6fd2807SJeff Garzik  *
4756c6fd2807SJeff Garzik  *	DMA-map the scatter-gather table associated with queued_cmd @qc.
4757c6fd2807SJeff Garzik  *
4758c6fd2807SJeff Garzik  *	LOCKING:
4759cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4760c6fd2807SJeff Garzik  *
4761c6fd2807SJeff Garzik  *	RETURNS:
4762c6fd2807SJeff Garzik  *	Zero on success, negative on error.
4763c6fd2807SJeff Garzik  *
4764c6fd2807SJeff Garzik  */
4765c6fd2807SJeff Garzik static int ata_sg_setup(struct ata_queued_cmd *qc)
4766c6fd2807SJeff Garzik {
4767c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4768dde20207SJames Bottomley 	unsigned int n_elem;
4769c6fd2807SJeff Garzik 
477044877b4eSTejun Heo 	VPRINTK("ENTER, ata%u\n", ap->print_id);
4771c6fd2807SJeff Garzik 
4772dde20207SJames Bottomley 	n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir);
4773dde20207SJames Bottomley 	if (n_elem < 1)
4774c6fd2807SJeff Garzik 		return -1;
4775c6fd2807SJeff Garzik 
4776dde20207SJames Bottomley 	DPRINTK("%d sg elements mapped\n", n_elem);
47775825627cSFUJITA Tomonori 	qc->orig_n_elem = qc->n_elem;
4778dde20207SJames Bottomley 	qc->n_elem = n_elem;
4779f92a2636STejun Heo 	qc->flags |= ATA_QCFLAG_DMAMAP;
4780c6fd2807SJeff Garzik 
4781c6fd2807SJeff Garzik 	return 0;
4782c6fd2807SJeff Garzik }
4783c6fd2807SJeff Garzik 
4784c6fd2807SJeff Garzik /**
4785c6fd2807SJeff Garzik  *	swap_buf_le16 - swap halves of 16-bit words in place
4786c6fd2807SJeff Garzik  *	@buf:  Buffer to swap
4787c6fd2807SJeff Garzik  *	@buf_words:  Number of 16-bit words in buffer.
4788c6fd2807SJeff Garzik  *
4789c6fd2807SJeff Garzik  *	Swap halves of 16-bit words if needed to convert from
4790c6fd2807SJeff Garzik  *	little-endian byte order to native cpu byte order, or
4791c6fd2807SJeff Garzik  *	vice-versa.
4792c6fd2807SJeff Garzik  *
4793c6fd2807SJeff Garzik  *	LOCKING:
4794c6fd2807SJeff Garzik  *	Inherited from caller.
4795c6fd2807SJeff Garzik  */
4796c6fd2807SJeff Garzik void swap_buf_le16(u16 *buf, unsigned int buf_words)
4797c6fd2807SJeff Garzik {
4798c6fd2807SJeff Garzik #ifdef __BIG_ENDIAN
4799c6fd2807SJeff Garzik 	unsigned int i;
4800c6fd2807SJeff Garzik 
4801c6fd2807SJeff Garzik 	for (i = 0; i < buf_words; i++)
4802c6fd2807SJeff Garzik 		buf[i] = le16_to_cpu(buf[i]);
4803c6fd2807SJeff Garzik #endif /* __BIG_ENDIAN */
4804c6fd2807SJeff Garzik }
4805c6fd2807SJeff Garzik 
4806c6fd2807SJeff Garzik /**
48078a8bc223STejun Heo  *	ata_qc_new - Request an available ATA command, for queueing
48085eb66fe0SRandy Dunlap  *	@ap: target port
48098a8bc223STejun Heo  *
48108a8bc223STejun Heo  *	LOCKING:
48118a8bc223STejun Heo  *	None.
48128a8bc223STejun Heo  */
48138a8bc223STejun Heo 
48148a8bc223STejun Heo static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
48158a8bc223STejun Heo {
48168a8bc223STejun Heo 	struct ata_queued_cmd *qc = NULL;
48178a8bc223STejun Heo 	unsigned int i;
48188a8bc223STejun Heo 
48198a8bc223STejun Heo 	/* no command while frozen */
48208a8bc223STejun Heo 	if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
48218a8bc223STejun Heo 		return NULL;
48228a8bc223STejun Heo 
48238a8bc223STejun Heo 	/* the last tag is reserved for internal command. */
48248a8bc223STejun Heo 	for (i = 0; i < ATA_MAX_QUEUE - 1; i++)
48258a8bc223STejun Heo 		if (!test_and_set_bit(i, &ap->qc_allocated)) {
48268a8bc223STejun Heo 			qc = __ata_qc_from_tag(ap, i);
48278a8bc223STejun Heo 			break;
48288a8bc223STejun Heo 		}
48298a8bc223STejun Heo 
48308a8bc223STejun Heo 	if (qc)
48318a8bc223STejun Heo 		qc->tag = i;
48328a8bc223STejun Heo 
48338a8bc223STejun Heo 	return qc;
48348a8bc223STejun Heo }
48358a8bc223STejun Heo 
48368a8bc223STejun Heo /**
4837c6fd2807SJeff Garzik  *	ata_qc_new_init - Request an available ATA command, and initialize it
4838c6fd2807SJeff Garzik  *	@dev: Device from whom we request an available command structure
4839c6fd2807SJeff Garzik  *
4840c6fd2807SJeff Garzik  *	LOCKING:
4841c6fd2807SJeff Garzik  *	None.
4842c6fd2807SJeff Garzik  */
4843c6fd2807SJeff Garzik 
48448a8bc223STejun Heo struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev)
4845c6fd2807SJeff Garzik {
48469af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
4847c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
4848c6fd2807SJeff Garzik 
48498a8bc223STejun Heo 	qc = ata_qc_new(ap);
4850c6fd2807SJeff Garzik 	if (qc) {
4851c6fd2807SJeff Garzik 		qc->scsicmd = NULL;
4852c6fd2807SJeff Garzik 		qc->ap = ap;
4853c6fd2807SJeff Garzik 		qc->dev = dev;
4854c6fd2807SJeff Garzik 
4855c6fd2807SJeff Garzik 		ata_qc_reinit(qc);
4856c6fd2807SJeff Garzik 	}
4857c6fd2807SJeff Garzik 
4858c6fd2807SJeff Garzik 	return qc;
4859c6fd2807SJeff Garzik }
4860c6fd2807SJeff Garzik 
48618a8bc223STejun Heo /**
48628a8bc223STejun Heo  *	ata_qc_free - free unused ata_queued_cmd
48638a8bc223STejun Heo  *	@qc: Command to complete
48648a8bc223STejun Heo  *
48658a8bc223STejun Heo  *	Designed to free unused ata_queued_cmd object
48668a8bc223STejun Heo  *	in case something prevents using it.
48678a8bc223STejun Heo  *
48688a8bc223STejun Heo  *	LOCKING:
48698a8bc223STejun Heo  *	spin_lock_irqsave(host lock)
48708a8bc223STejun Heo  */
48718a8bc223STejun Heo void ata_qc_free(struct ata_queued_cmd *qc)
48728a8bc223STejun Heo {
4873a1104016SJulia Lawall 	struct ata_port *ap;
48748a8bc223STejun Heo 	unsigned int tag;
48758a8bc223STejun Heo 
4876efcb3cf7STejun Heo 	WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4877a1104016SJulia Lawall 	ap = qc->ap;
48788a8bc223STejun Heo 
48798a8bc223STejun Heo 	qc->flags = 0;
48808a8bc223STejun Heo 	tag = qc->tag;
48818a8bc223STejun Heo 	if (likely(ata_tag_valid(tag))) {
48828a8bc223STejun Heo 		qc->tag = ATA_TAG_POISON;
48838a8bc223STejun Heo 		clear_bit(tag, &ap->qc_allocated);
48848a8bc223STejun Heo 	}
48858a8bc223STejun Heo }
48868a8bc223STejun Heo 
4887c6fd2807SJeff Garzik void __ata_qc_complete(struct ata_queued_cmd *qc)
4888c6fd2807SJeff Garzik {
4889a1104016SJulia Lawall 	struct ata_port *ap;
4890a1104016SJulia Lawall 	struct ata_link *link;
4891c6fd2807SJeff Garzik 
4892efcb3cf7STejun Heo 	WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4893efcb3cf7STejun Heo 	WARN_ON_ONCE(!(qc->flags & ATA_QCFLAG_ACTIVE));
4894a1104016SJulia Lawall 	ap = qc->ap;
4895a1104016SJulia Lawall 	link = qc->dev->link;
4896c6fd2807SJeff Garzik 
4897c6fd2807SJeff Garzik 	if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
4898c6fd2807SJeff Garzik 		ata_sg_clean(qc);
4899c6fd2807SJeff Garzik 
4900c6fd2807SJeff Garzik 	/* command should be marked inactive atomically with qc completion */
4901da917d69STejun Heo 	if (qc->tf.protocol == ATA_PROT_NCQ) {
49029af5c9c9STejun Heo 		link->sactive &= ~(1 << qc->tag);
4903da917d69STejun Heo 		if (!link->sactive)
4904da917d69STejun Heo 			ap->nr_active_links--;
4905da917d69STejun Heo 	} else {
49069af5c9c9STejun Heo 		link->active_tag = ATA_TAG_POISON;
4907da917d69STejun Heo 		ap->nr_active_links--;
4908da917d69STejun Heo 	}
4909da917d69STejun Heo 
4910da917d69STejun Heo 	/* clear exclusive status */
4911da917d69STejun Heo 	if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
4912da917d69STejun Heo 		     ap->excl_link == link))
4913da917d69STejun Heo 		ap->excl_link = NULL;
4914c6fd2807SJeff Garzik 
4915c6fd2807SJeff Garzik 	/* atapi: mark qc as inactive to prevent the interrupt handler
4916c6fd2807SJeff Garzik 	 * from completing the command twice later, before the error handler
4917c6fd2807SJeff Garzik 	 * is called. (when rc != 0 and atapi request sense is needed)
4918c6fd2807SJeff Garzik 	 */
4919c6fd2807SJeff Garzik 	qc->flags &= ~ATA_QCFLAG_ACTIVE;
4920c6fd2807SJeff Garzik 	ap->qc_active &= ~(1 << qc->tag);
4921c6fd2807SJeff Garzik 
4922c6fd2807SJeff Garzik 	/* call completion callback */
4923c6fd2807SJeff Garzik 	qc->complete_fn(qc);
4924c6fd2807SJeff Garzik }
4925c6fd2807SJeff Garzik 
492639599a53STejun Heo static void fill_result_tf(struct ata_queued_cmd *qc)
492739599a53STejun Heo {
492839599a53STejun Heo 	struct ata_port *ap = qc->ap;
492939599a53STejun Heo 
493039599a53STejun Heo 	qc->result_tf.flags = qc->tf.flags;
493122183bf5STejun Heo 	ap->ops->qc_fill_rtf(qc);
493239599a53STejun Heo }
493339599a53STejun Heo 
493400115e0fSTejun Heo static void ata_verify_xfer(struct ata_queued_cmd *qc)
493500115e0fSTejun Heo {
493600115e0fSTejun Heo 	struct ata_device *dev = qc->dev;
493700115e0fSTejun Heo 
493800115e0fSTejun Heo 	if (ata_tag_internal(qc->tag))
493900115e0fSTejun Heo 		return;
494000115e0fSTejun Heo 
494100115e0fSTejun Heo 	if (ata_is_nodata(qc->tf.protocol))
494200115e0fSTejun Heo 		return;
494300115e0fSTejun Heo 
494400115e0fSTejun Heo 	if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
494500115e0fSTejun Heo 		return;
494600115e0fSTejun Heo 
494700115e0fSTejun Heo 	dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
494800115e0fSTejun Heo }
494900115e0fSTejun Heo 
4950c6fd2807SJeff Garzik /**
4951c6fd2807SJeff Garzik  *	ata_qc_complete - Complete an active ATA command
4952c6fd2807SJeff Garzik  *	@qc: Command to complete
4953c6fd2807SJeff Garzik  *
4954c6fd2807SJeff Garzik  *	Indicate to the mid and upper layers that an ATA
4955c6fd2807SJeff Garzik  *	command has completed, with either an ok or not-ok status.
4956c6fd2807SJeff Garzik  *
4957c6fd2807SJeff Garzik  *	LOCKING:
4958cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4959c6fd2807SJeff Garzik  */
4960c6fd2807SJeff Garzik void ata_qc_complete(struct ata_queued_cmd *qc)
4961c6fd2807SJeff Garzik {
4962c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4963c6fd2807SJeff Garzik 
4964c6fd2807SJeff Garzik 	/* XXX: New EH and old EH use different mechanisms to
4965c6fd2807SJeff Garzik 	 * synchronize EH with regular execution path.
4966c6fd2807SJeff Garzik 	 *
4967c6fd2807SJeff Garzik 	 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
4968c6fd2807SJeff Garzik 	 * Normal execution path is responsible for not accessing a
4969c6fd2807SJeff Garzik 	 * failed qc.  libata core enforces the rule by returning NULL
4970c6fd2807SJeff Garzik 	 * from ata_qc_from_tag() for failed qcs.
4971c6fd2807SJeff Garzik 	 *
4972c6fd2807SJeff Garzik 	 * Old EH depends on ata_qc_complete() nullifying completion
4973c6fd2807SJeff Garzik 	 * requests if ATA_QCFLAG_EH_SCHEDULED is set.  Old EH does
4974c6fd2807SJeff Garzik 	 * not synchronize with interrupt handler.  Only PIO task is
4975c6fd2807SJeff Garzik 	 * taken care of.
4976c6fd2807SJeff Garzik 	 */
4977c6fd2807SJeff Garzik 	if (ap->ops->error_handler) {
49784dbfa39bSTejun Heo 		struct ata_device *dev = qc->dev;
49794dbfa39bSTejun Heo 		struct ata_eh_info *ehi = &dev->link->eh_info;
49804dbfa39bSTejun Heo 
4981c6fd2807SJeff Garzik 		if (unlikely(qc->err_mask))
4982c6fd2807SJeff Garzik 			qc->flags |= ATA_QCFLAG_FAILED;
4983c6fd2807SJeff Garzik 
4984c6fd2807SJeff Garzik 		if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
4985c6fd2807SJeff Garzik 			/* always fill result TF for failed qc */
498639599a53STejun Heo 			fill_result_tf(qc);
4987f4b31db9STejun Heo 
4988f4b31db9STejun Heo 			if (!ata_tag_internal(qc->tag))
4989c6fd2807SJeff Garzik 				ata_qc_schedule_eh(qc);
4990f4b31db9STejun Heo 			else
4991f4b31db9STejun Heo 				__ata_qc_complete(qc);
4992c6fd2807SJeff Garzik 			return;
4993c6fd2807SJeff Garzik 		}
4994c6fd2807SJeff Garzik 
49954dc738edSTejun Heo 		WARN_ON_ONCE(ap->pflags & ATA_PFLAG_FROZEN);
49964dc738edSTejun Heo 
4997c6fd2807SJeff Garzik 		/* read result TF if requested */
4998c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_RESULT_TF)
499939599a53STejun Heo 			fill_result_tf(qc);
5000c6fd2807SJeff Garzik 
50014dbfa39bSTejun Heo 		/* Some commands need post-processing after successful
50024dbfa39bSTejun Heo 		 * completion.
50034dbfa39bSTejun Heo 		 */
50044dbfa39bSTejun Heo 		switch (qc->tf.command) {
50054dbfa39bSTejun Heo 		case ATA_CMD_SET_FEATURES:
50064dbfa39bSTejun Heo 			if (qc->tf.feature != SETFEATURES_WC_ON &&
50074dbfa39bSTejun Heo 			    qc->tf.feature != SETFEATURES_WC_OFF)
50084dbfa39bSTejun Heo 				break;
50094dbfa39bSTejun Heo 			/* fall through */
50104dbfa39bSTejun Heo 		case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
50114dbfa39bSTejun Heo 		case ATA_CMD_SET_MULTI: /* multi_count changed */
50124dbfa39bSTejun Heo 			/* revalidate device */
50134dbfa39bSTejun Heo 			ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE;
50144dbfa39bSTejun Heo 			ata_port_schedule_eh(ap);
50154dbfa39bSTejun Heo 			break;
5016054a5fbaSTejun Heo 
5017054a5fbaSTejun Heo 		case ATA_CMD_SLEEP:
5018054a5fbaSTejun Heo 			dev->flags |= ATA_DFLAG_SLEEPING;
5019054a5fbaSTejun Heo 			break;
50204dbfa39bSTejun Heo 		}
50214dbfa39bSTejun Heo 
502200115e0fSTejun Heo 		if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
502300115e0fSTejun Heo 			ata_verify_xfer(qc);
502400115e0fSTejun Heo 
5025c6fd2807SJeff Garzik 		__ata_qc_complete(qc);
5026c6fd2807SJeff Garzik 	} else {
5027c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
5028c6fd2807SJeff Garzik 			return;
5029c6fd2807SJeff Garzik 
5030c6fd2807SJeff Garzik 		/* read result TF if failed or requested */
5031c6fd2807SJeff Garzik 		if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
503239599a53STejun Heo 			fill_result_tf(qc);
5033c6fd2807SJeff Garzik 
5034c6fd2807SJeff Garzik 		__ata_qc_complete(qc);
5035c6fd2807SJeff Garzik 	}
5036c6fd2807SJeff Garzik }
5037c6fd2807SJeff Garzik 
5038c6fd2807SJeff Garzik /**
5039c6fd2807SJeff Garzik  *	ata_qc_complete_multiple - Complete multiple qcs successfully
5040c6fd2807SJeff Garzik  *	@ap: port in question
5041c6fd2807SJeff Garzik  *	@qc_active: new qc_active mask
5042c6fd2807SJeff Garzik  *
5043c6fd2807SJeff Garzik  *	Complete in-flight commands.  This functions is meant to be
5044c6fd2807SJeff Garzik  *	called from low-level driver's interrupt routine to complete
5045c6fd2807SJeff Garzik  *	requests normally.  ap->qc_active and @qc_active is compared
5046c6fd2807SJeff Garzik  *	and commands are completed accordingly.
5047c6fd2807SJeff Garzik  *
5048c6fd2807SJeff Garzik  *	LOCKING:
5049cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
5050c6fd2807SJeff Garzik  *
5051c6fd2807SJeff Garzik  *	RETURNS:
5052c6fd2807SJeff Garzik  *	Number of completed commands on success, -errno otherwise.
5053c6fd2807SJeff Garzik  */
505479f97dadSTejun Heo int ata_qc_complete_multiple(struct ata_port *ap, u32 qc_active)
5055c6fd2807SJeff Garzik {
5056c6fd2807SJeff Garzik 	int nr_done = 0;
5057c6fd2807SJeff Garzik 	u32 done_mask;
5058c6fd2807SJeff Garzik 
5059c6fd2807SJeff Garzik 	done_mask = ap->qc_active ^ qc_active;
5060c6fd2807SJeff Garzik 
5061c6fd2807SJeff Garzik 	if (unlikely(done_mask & qc_active)) {
5062c6fd2807SJeff Garzik 		ata_port_printk(ap, KERN_ERR, "illegal qc_active transition "
5063c6fd2807SJeff Garzik 				"(%08x->%08x)\n", ap->qc_active, qc_active);
5064c6fd2807SJeff Garzik 		return -EINVAL;
5065c6fd2807SJeff Garzik 	}
5066c6fd2807SJeff Garzik 
506743768180SJens Axboe 	while (done_mask) {
5068c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc;
506943768180SJens Axboe 		unsigned int tag = __ffs(done_mask);
5070c6fd2807SJeff Garzik 
507143768180SJens Axboe 		qc = ata_qc_from_tag(ap, tag);
507243768180SJens Axboe 		if (qc) {
5073c6fd2807SJeff Garzik 			ata_qc_complete(qc);
5074c6fd2807SJeff Garzik 			nr_done++;
5075c6fd2807SJeff Garzik 		}
507643768180SJens Axboe 		done_mask &= ~(1 << tag);
5077c6fd2807SJeff Garzik 	}
5078c6fd2807SJeff Garzik 
5079c6fd2807SJeff Garzik 	return nr_done;
5080c6fd2807SJeff Garzik }
5081c6fd2807SJeff Garzik 
5082c6fd2807SJeff Garzik /**
5083c6fd2807SJeff Garzik  *	ata_qc_issue - issue taskfile to device
5084c6fd2807SJeff Garzik  *	@qc: command to issue to device
5085c6fd2807SJeff Garzik  *
5086c6fd2807SJeff Garzik  *	Prepare an ATA command to submission to device.
5087c6fd2807SJeff Garzik  *	This includes mapping the data into a DMA-able
5088c6fd2807SJeff Garzik  *	area, filling in the S/G table, and finally
5089c6fd2807SJeff Garzik  *	writing the taskfile to hardware, starting the command.
5090c6fd2807SJeff Garzik  *
5091c6fd2807SJeff Garzik  *	LOCKING:
5092cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
5093c6fd2807SJeff Garzik  */
5094c6fd2807SJeff Garzik void ata_qc_issue(struct ata_queued_cmd *qc)
5095c6fd2807SJeff Garzik {
5096c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
50979af5c9c9STejun Heo 	struct ata_link *link = qc->dev->link;
5098405e66b3STejun Heo 	u8 prot = qc->tf.protocol;
5099c6fd2807SJeff Garzik 
5100c6fd2807SJeff Garzik 	/* Make sure only one non-NCQ command is outstanding.  The
5101c6fd2807SJeff Garzik 	 * check is skipped for old EH because it reuses active qc to
5102c6fd2807SJeff Garzik 	 * request ATAPI sense.
5103c6fd2807SJeff Garzik 	 */
5104efcb3cf7STejun Heo 	WARN_ON_ONCE(ap->ops->error_handler && ata_tag_valid(link->active_tag));
5105c6fd2807SJeff Garzik 
51061973a023STejun Heo 	if (ata_is_ncq(prot)) {
5107efcb3cf7STejun Heo 		WARN_ON_ONCE(link->sactive & (1 << qc->tag));
5108da917d69STejun Heo 
5109da917d69STejun Heo 		if (!link->sactive)
5110da917d69STejun Heo 			ap->nr_active_links++;
51119af5c9c9STejun Heo 		link->sactive |= 1 << qc->tag;
5112c6fd2807SJeff Garzik 	} else {
5113efcb3cf7STejun Heo 		WARN_ON_ONCE(link->sactive);
5114da917d69STejun Heo 
5115da917d69STejun Heo 		ap->nr_active_links++;
51169af5c9c9STejun Heo 		link->active_tag = qc->tag;
5117c6fd2807SJeff Garzik 	}
5118c6fd2807SJeff Garzik 
5119c6fd2807SJeff Garzik 	qc->flags |= ATA_QCFLAG_ACTIVE;
5120c6fd2807SJeff Garzik 	ap->qc_active |= 1 << qc->tag;
5121c6fd2807SJeff Garzik 
5122f92a2636STejun Heo 	/* We guarantee to LLDs that they will have at least one
5123f92a2636STejun Heo 	 * non-zero sg if the command is a data command.
5124f92a2636STejun Heo 	 */
5125ff2aeb1eSTejun Heo 	BUG_ON(ata_is_data(prot) && (!qc->sg || !qc->n_elem || !qc->nbytes));
5126f92a2636STejun Heo 
5127405e66b3STejun Heo 	if (ata_is_dma(prot) || (ata_is_pio(prot) &&
5128f92a2636STejun Heo 				 (ap->flags & ATA_FLAG_PIO_DMA)))
5129c6fd2807SJeff Garzik 		if (ata_sg_setup(qc))
5130c6fd2807SJeff Garzik 			goto sg_err;
5131c6fd2807SJeff Garzik 
5132cf480626STejun Heo 	/* if device is sleeping, schedule reset and abort the link */
5133054a5fbaSTejun Heo 	if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
5134cf480626STejun Heo 		link->eh_info.action |= ATA_EH_RESET;
5135054a5fbaSTejun Heo 		ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
5136054a5fbaSTejun Heo 		ata_link_abort(link);
5137054a5fbaSTejun Heo 		return;
5138054a5fbaSTejun Heo 	}
5139054a5fbaSTejun Heo 
5140c6fd2807SJeff Garzik 	ap->ops->qc_prep(qc);
5141c6fd2807SJeff Garzik 
5142c6fd2807SJeff Garzik 	qc->err_mask |= ap->ops->qc_issue(qc);
5143c6fd2807SJeff Garzik 	if (unlikely(qc->err_mask))
5144c6fd2807SJeff Garzik 		goto err;
5145c6fd2807SJeff Garzik 	return;
5146c6fd2807SJeff Garzik 
5147c6fd2807SJeff Garzik sg_err:
5148c6fd2807SJeff Garzik 	qc->err_mask |= AC_ERR_SYSTEM;
5149c6fd2807SJeff Garzik err:
5150c6fd2807SJeff Garzik 	ata_qc_complete(qc);
5151c6fd2807SJeff Garzik }
5152c6fd2807SJeff Garzik 
5153c6fd2807SJeff Garzik /**
5154c6fd2807SJeff Garzik  *	sata_scr_valid - test whether SCRs are accessible
5155936fd732STejun Heo  *	@link: ATA link to test SCR accessibility for
5156c6fd2807SJeff Garzik  *
5157936fd732STejun Heo  *	Test whether SCRs are accessible for @link.
5158c6fd2807SJeff Garzik  *
5159c6fd2807SJeff Garzik  *	LOCKING:
5160c6fd2807SJeff Garzik  *	None.
5161c6fd2807SJeff Garzik  *
5162c6fd2807SJeff Garzik  *	RETURNS:
5163c6fd2807SJeff Garzik  *	1 if SCRs are accessible, 0 otherwise.
5164c6fd2807SJeff Garzik  */
5165936fd732STejun Heo int sata_scr_valid(struct ata_link *link)
5166c6fd2807SJeff Garzik {
5167936fd732STejun Heo 	struct ata_port *ap = link->ap;
5168936fd732STejun Heo 
5169a16abc0bSTejun Heo 	return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
5170c6fd2807SJeff Garzik }
5171c6fd2807SJeff Garzik 
5172c6fd2807SJeff Garzik /**
5173c6fd2807SJeff Garzik  *	sata_scr_read - read SCR register of the specified port
5174936fd732STejun Heo  *	@link: ATA link to read SCR for
5175c6fd2807SJeff Garzik  *	@reg: SCR to read
5176c6fd2807SJeff Garzik  *	@val: Place to store read value
5177c6fd2807SJeff Garzik  *
5178936fd732STejun Heo  *	Read SCR register @reg of @link into *@val.  This function is
5179633273a3STejun Heo  *	guaranteed to succeed if @link is ap->link, the cable type of
5180633273a3STejun Heo  *	the port is SATA and the port implements ->scr_read.
5181c6fd2807SJeff Garzik  *
5182c6fd2807SJeff Garzik  *	LOCKING:
5183633273a3STejun Heo  *	None if @link is ap->link.  Kernel thread context otherwise.
5184c6fd2807SJeff Garzik  *
5185c6fd2807SJeff Garzik  *	RETURNS:
5186c6fd2807SJeff Garzik  *	0 on success, negative errno on failure.
5187c6fd2807SJeff Garzik  */
5188936fd732STejun Heo int sata_scr_read(struct ata_link *link, int reg, u32 *val)
5189c6fd2807SJeff Garzik {
5190633273a3STejun Heo 	if (ata_is_host_link(link)) {
5191936fd732STejun Heo 		if (sata_scr_valid(link))
519282ef04fbSTejun Heo 			return link->ap->ops->scr_read(link, reg, val);
5193c6fd2807SJeff Garzik 		return -EOPNOTSUPP;
5194c6fd2807SJeff Garzik 	}
5195c6fd2807SJeff Garzik 
5196633273a3STejun Heo 	return sata_pmp_scr_read(link, reg, val);
5197633273a3STejun Heo }
5198633273a3STejun Heo 
5199c6fd2807SJeff Garzik /**
5200c6fd2807SJeff Garzik  *	sata_scr_write - write SCR register of the specified port
5201936fd732STejun Heo  *	@link: ATA link to write SCR for
5202c6fd2807SJeff Garzik  *	@reg: SCR to write
5203c6fd2807SJeff Garzik  *	@val: value to write
5204c6fd2807SJeff Garzik  *
5205936fd732STejun Heo  *	Write @val to SCR register @reg of @link.  This function is
5206633273a3STejun Heo  *	guaranteed to succeed if @link is ap->link, the cable type of
5207633273a3STejun Heo  *	the port is SATA and the port implements ->scr_read.
5208c6fd2807SJeff Garzik  *
5209c6fd2807SJeff Garzik  *	LOCKING:
5210633273a3STejun Heo  *	None if @link is ap->link.  Kernel thread context otherwise.
5211c6fd2807SJeff Garzik  *
5212c6fd2807SJeff Garzik  *	RETURNS:
5213c6fd2807SJeff Garzik  *	0 on success, negative errno on failure.
5214c6fd2807SJeff Garzik  */
5215936fd732STejun Heo int sata_scr_write(struct ata_link *link, int reg, u32 val)
5216c6fd2807SJeff Garzik {
5217633273a3STejun Heo 	if (ata_is_host_link(link)) {
5218936fd732STejun Heo 		if (sata_scr_valid(link))
521982ef04fbSTejun Heo 			return link->ap->ops->scr_write(link, reg, val);
5220c6fd2807SJeff Garzik 		return -EOPNOTSUPP;
5221c6fd2807SJeff Garzik 	}
5222c6fd2807SJeff Garzik 
5223633273a3STejun Heo 	return sata_pmp_scr_write(link, reg, val);
5224633273a3STejun Heo }
5225633273a3STejun Heo 
5226c6fd2807SJeff Garzik /**
5227c6fd2807SJeff Garzik  *	sata_scr_write_flush - write SCR register of the specified port and flush
5228936fd732STejun Heo  *	@link: ATA link to write SCR for
5229c6fd2807SJeff Garzik  *	@reg: SCR to write
5230c6fd2807SJeff Garzik  *	@val: value to write
5231c6fd2807SJeff Garzik  *
5232c6fd2807SJeff Garzik  *	This function is identical to sata_scr_write() except that this
5233c6fd2807SJeff Garzik  *	function performs flush after writing to the register.
5234c6fd2807SJeff Garzik  *
5235c6fd2807SJeff Garzik  *	LOCKING:
5236633273a3STejun Heo  *	None if @link is ap->link.  Kernel thread context otherwise.
5237c6fd2807SJeff Garzik  *
5238c6fd2807SJeff Garzik  *	RETURNS:
5239c6fd2807SJeff Garzik  *	0 on success, negative errno on failure.
5240c6fd2807SJeff Garzik  */
5241936fd732STejun Heo int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
5242c6fd2807SJeff Garzik {
5243633273a3STejun Heo 	if (ata_is_host_link(link)) {
5244da3dbb17STejun Heo 		int rc;
5245da3dbb17STejun Heo 
5246936fd732STejun Heo 		if (sata_scr_valid(link)) {
524782ef04fbSTejun Heo 			rc = link->ap->ops->scr_write(link, reg, val);
5248da3dbb17STejun Heo 			if (rc == 0)
524982ef04fbSTejun Heo 				rc = link->ap->ops->scr_read(link, reg, &val);
5250da3dbb17STejun Heo 			return rc;
5251c6fd2807SJeff Garzik 		}
5252c6fd2807SJeff Garzik 		return -EOPNOTSUPP;
5253c6fd2807SJeff Garzik 	}
5254c6fd2807SJeff Garzik 
5255633273a3STejun Heo 	return sata_pmp_scr_write(link, reg, val);
5256633273a3STejun Heo }
5257633273a3STejun Heo 
5258c6fd2807SJeff Garzik /**
5259b1c72916STejun Heo  *	ata_phys_link_online - test whether the given link is online
5260936fd732STejun Heo  *	@link: ATA link to test
5261c6fd2807SJeff Garzik  *
5262936fd732STejun Heo  *	Test whether @link is online.  Note that this function returns
5263936fd732STejun Heo  *	0 if online status of @link cannot be obtained, so
5264936fd732STejun Heo  *	ata_link_online(link) != !ata_link_offline(link).
5265c6fd2807SJeff Garzik  *
5266c6fd2807SJeff Garzik  *	LOCKING:
5267c6fd2807SJeff Garzik  *	None.
5268c6fd2807SJeff Garzik  *
5269c6fd2807SJeff Garzik  *	RETURNS:
5270b5b3fa38STejun Heo  *	True if the port online status is available and online.
5271c6fd2807SJeff Garzik  */
5272b1c72916STejun Heo bool ata_phys_link_online(struct ata_link *link)
5273c6fd2807SJeff Garzik {
5274c6fd2807SJeff Garzik 	u32 sstatus;
5275c6fd2807SJeff Garzik 
5276936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
52779913ff8aSTejun Heo 	    ata_sstatus_online(sstatus))
5278b5b3fa38STejun Heo 		return true;
5279b5b3fa38STejun Heo 	return false;
5280c6fd2807SJeff Garzik }
5281c6fd2807SJeff Garzik 
5282c6fd2807SJeff Garzik /**
5283b1c72916STejun Heo  *	ata_phys_link_offline - test whether the given link is offline
5284936fd732STejun Heo  *	@link: ATA link to test
5285c6fd2807SJeff Garzik  *
5286936fd732STejun Heo  *	Test whether @link is offline.  Note that this function
5287936fd732STejun Heo  *	returns 0 if offline status of @link cannot be obtained, so
5288936fd732STejun Heo  *	ata_link_online(link) != !ata_link_offline(link).
5289c6fd2807SJeff Garzik  *
5290c6fd2807SJeff Garzik  *	LOCKING:
5291c6fd2807SJeff Garzik  *	None.
5292c6fd2807SJeff Garzik  *
5293c6fd2807SJeff Garzik  *	RETURNS:
5294b5b3fa38STejun Heo  *	True if the port offline status is available and offline.
5295c6fd2807SJeff Garzik  */
5296b1c72916STejun Heo bool ata_phys_link_offline(struct ata_link *link)
5297c6fd2807SJeff Garzik {
5298c6fd2807SJeff Garzik 	u32 sstatus;
5299c6fd2807SJeff Garzik 
5300936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
53019913ff8aSTejun Heo 	    !ata_sstatus_online(sstatus))
5302b5b3fa38STejun Heo 		return true;
5303b5b3fa38STejun Heo 	return false;
5304c6fd2807SJeff Garzik }
5305c6fd2807SJeff Garzik 
5306b1c72916STejun Heo /**
5307b1c72916STejun Heo  *	ata_link_online - test whether the given link is online
5308b1c72916STejun Heo  *	@link: ATA link to test
5309b1c72916STejun Heo  *
5310b1c72916STejun Heo  *	Test whether @link is online.  This is identical to
5311b1c72916STejun Heo  *	ata_phys_link_online() when there's no slave link.  When
5312b1c72916STejun Heo  *	there's a slave link, this function should only be called on
5313b1c72916STejun Heo  *	the master link and will return true if any of M/S links is
5314b1c72916STejun Heo  *	online.
5315b1c72916STejun Heo  *
5316b1c72916STejun Heo  *	LOCKING:
5317b1c72916STejun Heo  *	None.
5318b1c72916STejun Heo  *
5319b1c72916STejun Heo  *	RETURNS:
5320b1c72916STejun Heo  *	True if the port online status is available and online.
5321b1c72916STejun Heo  */
5322b1c72916STejun Heo bool ata_link_online(struct ata_link *link)
5323b1c72916STejun Heo {
5324b1c72916STejun Heo 	struct ata_link *slave = link->ap->slave_link;
5325b1c72916STejun Heo 
5326b1c72916STejun Heo 	WARN_ON(link == slave);	/* shouldn't be called on slave link */
5327b1c72916STejun Heo 
5328b1c72916STejun Heo 	return ata_phys_link_online(link) ||
5329b1c72916STejun Heo 		(slave && ata_phys_link_online(slave));
5330b1c72916STejun Heo }
5331b1c72916STejun Heo 
5332b1c72916STejun Heo /**
5333b1c72916STejun Heo  *	ata_link_offline - test whether the given link is offline
5334b1c72916STejun Heo  *	@link: ATA link to test
5335b1c72916STejun Heo  *
5336b1c72916STejun Heo  *	Test whether @link is offline.  This is identical to
5337b1c72916STejun Heo  *	ata_phys_link_offline() when there's no slave link.  When
5338b1c72916STejun Heo  *	there's a slave link, this function should only be called on
5339b1c72916STejun Heo  *	the master link and will return true if both M/S links are
5340b1c72916STejun Heo  *	offline.
5341b1c72916STejun Heo  *
5342b1c72916STejun Heo  *	LOCKING:
5343b1c72916STejun Heo  *	None.
5344b1c72916STejun Heo  *
5345b1c72916STejun Heo  *	RETURNS:
5346b1c72916STejun Heo  *	True if the port offline status is available and offline.
5347b1c72916STejun Heo  */
5348b1c72916STejun Heo bool ata_link_offline(struct ata_link *link)
5349b1c72916STejun Heo {
5350b1c72916STejun Heo 	struct ata_link *slave = link->ap->slave_link;
5351b1c72916STejun Heo 
5352b1c72916STejun Heo 	WARN_ON(link == slave);	/* shouldn't be called on slave link */
5353b1c72916STejun Heo 
5354b1c72916STejun Heo 	return ata_phys_link_offline(link) &&
5355b1c72916STejun Heo 		(!slave || ata_phys_link_offline(slave));
5356b1c72916STejun Heo }
5357b1c72916STejun Heo 
53586ffa01d8STejun Heo #ifdef CONFIG_PM
5359cca3974eSJeff Garzik static int ata_host_request_pm(struct ata_host *host, pm_message_t mesg,
5360cca3974eSJeff Garzik 			       unsigned int action, unsigned int ehi_flags,
5361cca3974eSJeff Garzik 			       int wait)
5362c6fd2807SJeff Garzik {
5363c6fd2807SJeff Garzik 	unsigned long flags;
5364c6fd2807SJeff Garzik 	int i, rc;
5365c6fd2807SJeff Garzik 
5366cca3974eSJeff Garzik 	for (i = 0; i < host->n_ports; i++) {
5367cca3974eSJeff Garzik 		struct ata_port *ap = host->ports[i];
5368e3667ebfSTejun Heo 		struct ata_link *link;
5369c6fd2807SJeff Garzik 
5370c6fd2807SJeff Garzik 		/* Previous resume operation might still be in
5371c6fd2807SJeff Garzik 		 * progress.  Wait for PM_PENDING to clear.
5372c6fd2807SJeff Garzik 		 */
5373c6fd2807SJeff Garzik 		if (ap->pflags & ATA_PFLAG_PM_PENDING) {
5374c6fd2807SJeff Garzik 			ata_port_wait_eh(ap);
5375c6fd2807SJeff Garzik 			WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5376c6fd2807SJeff Garzik 		}
5377c6fd2807SJeff Garzik 
5378c6fd2807SJeff Garzik 		/* request PM ops to EH */
5379c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
5380c6fd2807SJeff Garzik 
5381c6fd2807SJeff Garzik 		ap->pm_mesg = mesg;
5382c6fd2807SJeff Garzik 		if (wait) {
5383c6fd2807SJeff Garzik 			rc = 0;
5384c6fd2807SJeff Garzik 			ap->pm_result = &rc;
5385c6fd2807SJeff Garzik 		}
5386c6fd2807SJeff Garzik 
5387c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_PM_PENDING;
53881eca4365STejun Heo 		ata_for_each_link(link, ap, HOST_FIRST) {
5389e3667ebfSTejun Heo 			link->eh_info.action |= action;
5390e3667ebfSTejun Heo 			link->eh_info.flags |= ehi_flags;
5391e3667ebfSTejun Heo 		}
5392c6fd2807SJeff Garzik 
5393c6fd2807SJeff Garzik 		ata_port_schedule_eh(ap);
5394c6fd2807SJeff Garzik 
5395c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
5396c6fd2807SJeff Garzik 
5397c6fd2807SJeff Garzik 		/* wait and check result */
5398c6fd2807SJeff Garzik 		if (wait) {
5399c6fd2807SJeff Garzik 			ata_port_wait_eh(ap);
5400c6fd2807SJeff Garzik 			WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5401c6fd2807SJeff Garzik 			if (rc)
5402c6fd2807SJeff Garzik 				return rc;
5403c6fd2807SJeff Garzik 		}
5404c6fd2807SJeff Garzik 	}
5405c6fd2807SJeff Garzik 
5406c6fd2807SJeff Garzik 	return 0;
5407c6fd2807SJeff Garzik }
5408c6fd2807SJeff Garzik 
5409c6fd2807SJeff Garzik /**
5410cca3974eSJeff Garzik  *	ata_host_suspend - suspend host
5411cca3974eSJeff Garzik  *	@host: host to suspend
5412c6fd2807SJeff Garzik  *	@mesg: PM message
5413c6fd2807SJeff Garzik  *
5414cca3974eSJeff Garzik  *	Suspend @host.  Actual operation is performed by EH.  This
5415c6fd2807SJeff Garzik  *	function requests EH to perform PM operations and waits for EH
5416c6fd2807SJeff Garzik  *	to finish.
5417c6fd2807SJeff Garzik  *
5418c6fd2807SJeff Garzik  *	LOCKING:
5419c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
5420c6fd2807SJeff Garzik  *
5421c6fd2807SJeff Garzik  *	RETURNS:
5422c6fd2807SJeff Garzik  *	0 on success, -errno on failure.
5423c6fd2807SJeff Garzik  */
5424cca3974eSJeff Garzik int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
5425c6fd2807SJeff Garzik {
54269666f400STejun Heo 	int rc;
5427c6fd2807SJeff Garzik 
5428ca77329fSKristen Carlson Accardi 	/*
5429ca77329fSKristen Carlson Accardi 	 * disable link pm on all ports before requesting
5430ca77329fSKristen Carlson Accardi 	 * any pm activity
5431ca77329fSKristen Carlson Accardi 	 */
5432ca77329fSKristen Carlson Accardi 	ata_lpm_enable(host);
5433ca77329fSKristen Carlson Accardi 
5434cca3974eSJeff Garzik 	rc = ata_host_request_pm(host, mesg, 0, ATA_EHI_QUIET, 1);
543572ad6ec4SJeff Garzik 	if (rc == 0)
543672ad6ec4SJeff Garzik 		host->dev->power.power_state = mesg;
5437c6fd2807SJeff Garzik 	return rc;
5438c6fd2807SJeff Garzik }
5439c6fd2807SJeff Garzik 
5440c6fd2807SJeff Garzik /**
5441cca3974eSJeff Garzik  *	ata_host_resume - resume host
5442cca3974eSJeff Garzik  *	@host: host to resume
5443c6fd2807SJeff Garzik  *
5444cca3974eSJeff Garzik  *	Resume @host.  Actual operation is performed by EH.  This
5445c6fd2807SJeff Garzik  *	function requests EH to perform PM operations and returns.
5446c6fd2807SJeff Garzik  *	Note that all resume operations are performed parallely.
5447c6fd2807SJeff Garzik  *
5448c6fd2807SJeff Garzik  *	LOCKING:
5449c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
5450c6fd2807SJeff Garzik  */
5451cca3974eSJeff Garzik void ata_host_resume(struct ata_host *host)
5452c6fd2807SJeff Garzik {
5453cf480626STejun Heo 	ata_host_request_pm(host, PMSG_ON, ATA_EH_RESET,
5454c6fd2807SJeff Garzik 			    ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET, 0);
545572ad6ec4SJeff Garzik 	host->dev->power.power_state = PMSG_ON;
5456ca77329fSKristen Carlson Accardi 
5457ca77329fSKristen Carlson Accardi 	/* reenable link pm */
5458ca77329fSKristen Carlson Accardi 	ata_lpm_disable(host);
5459c6fd2807SJeff Garzik }
54606ffa01d8STejun Heo #endif
5461c6fd2807SJeff Garzik 
5462c6fd2807SJeff Garzik /**
5463c6fd2807SJeff Garzik  *	ata_dev_init - Initialize an ata_device structure
5464c6fd2807SJeff Garzik  *	@dev: Device structure to initialize
5465c6fd2807SJeff Garzik  *
5466c6fd2807SJeff Garzik  *	Initialize @dev in preparation for probing.
5467c6fd2807SJeff Garzik  *
5468c6fd2807SJeff Garzik  *	LOCKING:
5469c6fd2807SJeff Garzik  *	Inherited from caller.
5470c6fd2807SJeff Garzik  */
5471c6fd2807SJeff Garzik void ata_dev_init(struct ata_device *dev)
5472c6fd2807SJeff Garzik {
5473b1c72916STejun Heo 	struct ata_link *link = ata_dev_phys_link(dev);
54749af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
5475c6fd2807SJeff Garzik 	unsigned long flags;
5476c6fd2807SJeff Garzik 
5477b1c72916STejun Heo 	/* SATA spd limit is bound to the attached device, reset together */
54789af5c9c9STejun Heo 	link->sata_spd_limit = link->hw_sata_spd_limit;
54799af5c9c9STejun Heo 	link->sata_spd = 0;
5480c6fd2807SJeff Garzik 
5481c6fd2807SJeff Garzik 	/* High bits of dev->flags are used to record warm plug
5482c6fd2807SJeff Garzik 	 * requests which occur asynchronously.  Synchronize using
5483cca3974eSJeff Garzik 	 * host lock.
5484c6fd2807SJeff Garzik 	 */
5485c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
5486c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_INIT_MASK;
54873dcc323fSTejun Heo 	dev->horkage = 0;
5488c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
5489c6fd2807SJeff Garzik 
549099cf610aSTejun Heo 	memset((void *)dev + ATA_DEVICE_CLEAR_BEGIN, 0,
549199cf610aSTejun Heo 	       ATA_DEVICE_CLEAR_END - ATA_DEVICE_CLEAR_BEGIN);
5492c6fd2807SJeff Garzik 	dev->pio_mask = UINT_MAX;
5493c6fd2807SJeff Garzik 	dev->mwdma_mask = UINT_MAX;
5494c6fd2807SJeff Garzik 	dev->udma_mask = UINT_MAX;
5495c6fd2807SJeff Garzik }
5496c6fd2807SJeff Garzik 
5497c6fd2807SJeff Garzik /**
54984fb37a25STejun Heo  *	ata_link_init - Initialize an ata_link structure
54994fb37a25STejun Heo  *	@ap: ATA port link is attached to
55004fb37a25STejun Heo  *	@link: Link structure to initialize
55018989805dSTejun Heo  *	@pmp: Port multiplier port number
55024fb37a25STejun Heo  *
55034fb37a25STejun Heo  *	Initialize @link.
55044fb37a25STejun Heo  *
55054fb37a25STejun Heo  *	LOCKING:
55064fb37a25STejun Heo  *	Kernel thread context (may sleep)
55074fb37a25STejun Heo  */
5508fb7fd614STejun Heo void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
55094fb37a25STejun Heo {
55104fb37a25STejun Heo 	int i;
55114fb37a25STejun Heo 
55124fb37a25STejun Heo 	/* clear everything except for devices */
55134fb37a25STejun Heo 	memset(link, 0, offsetof(struct ata_link, device[0]));
55144fb37a25STejun Heo 
55154fb37a25STejun Heo 	link->ap = ap;
55168989805dSTejun Heo 	link->pmp = pmp;
55174fb37a25STejun Heo 	link->active_tag = ATA_TAG_POISON;
55184fb37a25STejun Heo 	link->hw_sata_spd_limit = UINT_MAX;
55194fb37a25STejun Heo 
55204fb37a25STejun Heo 	/* can't use iterator, ap isn't initialized yet */
55214fb37a25STejun Heo 	for (i = 0; i < ATA_MAX_DEVICES; i++) {
55224fb37a25STejun Heo 		struct ata_device *dev = &link->device[i];
55234fb37a25STejun Heo 
55244fb37a25STejun Heo 		dev->link = link;
55254fb37a25STejun Heo 		dev->devno = dev - link->device;
5526110f66d2STejun Heo #ifdef CONFIG_ATA_ACPI
5527110f66d2STejun Heo 		dev->gtf_filter = ata_acpi_gtf_filter;
5528110f66d2STejun Heo #endif
55294fb37a25STejun Heo 		ata_dev_init(dev);
55304fb37a25STejun Heo 	}
55314fb37a25STejun Heo }
55324fb37a25STejun Heo 
55334fb37a25STejun Heo /**
55344fb37a25STejun Heo  *	sata_link_init_spd - Initialize link->sata_spd_limit
55354fb37a25STejun Heo  *	@link: Link to configure sata_spd_limit for
55364fb37a25STejun Heo  *
55374fb37a25STejun Heo  *	Initialize @link->[hw_]sata_spd_limit to the currently
55384fb37a25STejun Heo  *	configured value.
55394fb37a25STejun Heo  *
55404fb37a25STejun Heo  *	LOCKING:
55414fb37a25STejun Heo  *	Kernel thread context (may sleep).
55424fb37a25STejun Heo  *
55434fb37a25STejun Heo  *	RETURNS:
55444fb37a25STejun Heo  *	0 on success, -errno on failure.
55454fb37a25STejun Heo  */
5546fb7fd614STejun Heo int sata_link_init_spd(struct ata_link *link)
55474fb37a25STejun Heo {
554833267325STejun Heo 	u8 spd;
55494fb37a25STejun Heo 	int rc;
55504fb37a25STejun Heo 
5551d127ea7bSTejun Heo 	rc = sata_scr_read(link, SCR_CONTROL, &link->saved_scontrol);
55524fb37a25STejun Heo 	if (rc)
55534fb37a25STejun Heo 		return rc;
55544fb37a25STejun Heo 
5555d127ea7bSTejun Heo 	spd = (link->saved_scontrol >> 4) & 0xf;
55564fb37a25STejun Heo 	if (spd)
55574fb37a25STejun Heo 		link->hw_sata_spd_limit &= (1 << spd) - 1;
55584fb37a25STejun Heo 
555905944bdfSTejun Heo 	ata_force_link_limits(link);
556033267325STejun Heo 
55614fb37a25STejun Heo 	link->sata_spd_limit = link->hw_sata_spd_limit;
55624fb37a25STejun Heo 
55634fb37a25STejun Heo 	return 0;
55644fb37a25STejun Heo }
55654fb37a25STejun Heo 
55664fb37a25STejun Heo /**
5567f3187195STejun Heo  *	ata_port_alloc - allocate and initialize basic ATA port resources
5568f3187195STejun Heo  *	@host: ATA host this allocated port belongs to
5569c6fd2807SJeff Garzik  *
5570f3187195STejun Heo  *	Allocate and initialize basic ATA port resources.
5571f3187195STejun Heo  *
5572f3187195STejun Heo  *	RETURNS:
5573f3187195STejun Heo  *	Allocate ATA port on success, NULL on failure.
5574c6fd2807SJeff Garzik  *
5575c6fd2807SJeff Garzik  *	LOCKING:
5576f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5577c6fd2807SJeff Garzik  */
5578f3187195STejun Heo struct ata_port *ata_port_alloc(struct ata_host *host)
5579c6fd2807SJeff Garzik {
5580f3187195STejun Heo 	struct ata_port *ap;
5581c6fd2807SJeff Garzik 
5582f3187195STejun Heo 	DPRINTK("ENTER\n");
5583f3187195STejun Heo 
5584f3187195STejun Heo 	ap = kzalloc(sizeof(*ap), GFP_KERNEL);
5585f3187195STejun Heo 	if (!ap)
5586f3187195STejun Heo 		return NULL;
5587f3187195STejun Heo 
5588f4d6d004STejun Heo 	ap->pflags |= ATA_PFLAG_INITIALIZING;
5589cca3974eSJeff Garzik 	ap->lock = &host->lock;
5590f3187195STejun Heo 	ap->print_id = -1;
5591cca3974eSJeff Garzik 	ap->host = host;
5592f3187195STejun Heo 	ap->dev = host->dev;
5593c6fd2807SJeff Garzik 
5594c6fd2807SJeff Garzik #if defined(ATA_VERBOSE_DEBUG)
5595c6fd2807SJeff Garzik 	/* turn on all debugging levels */
5596c6fd2807SJeff Garzik 	ap->msg_enable = 0x00FF;
5597c6fd2807SJeff Garzik #elif defined(ATA_DEBUG)
5598c6fd2807SJeff Garzik 	ap->msg_enable = ATA_MSG_DRV | ATA_MSG_INFO | ATA_MSG_CTL | ATA_MSG_WARN | ATA_MSG_ERR;
5599c6fd2807SJeff Garzik #else
5600c6fd2807SJeff Garzik 	ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
5601c6fd2807SJeff Garzik #endif
5602c6fd2807SJeff Garzik 
560365f27f38SDavid Howells 	INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
560465f27f38SDavid Howells 	INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
5605c6fd2807SJeff Garzik 	INIT_LIST_HEAD(&ap->eh_done_q);
5606c6fd2807SJeff Garzik 	init_waitqueue_head(&ap->eh_wait_q);
560745fabbb7SElias Oltmanns 	init_completion(&ap->park_req_pending);
56085ddf24c5STejun Heo 	init_timer_deferrable(&ap->fastdrain_timer);
56095ddf24c5STejun Heo 	ap->fastdrain_timer.function = ata_eh_fastdrain_timerfn;
56105ddf24c5STejun Heo 	ap->fastdrain_timer.data = (unsigned long)ap;
5611c6fd2807SJeff Garzik 
5612c6fd2807SJeff Garzik 	ap->cbl = ATA_CBL_NONE;
5613c6fd2807SJeff Garzik 
56148989805dSTejun Heo 	ata_link_init(ap, &ap->link, 0);
5615c6fd2807SJeff Garzik 
5616c6fd2807SJeff Garzik #ifdef ATA_IRQ_TRAP
5617c6fd2807SJeff Garzik 	ap->stats.unhandled_irq = 1;
5618c6fd2807SJeff Garzik 	ap->stats.idle_irq = 1;
5619c6fd2807SJeff Garzik #endif
5620270390e1STejun Heo 	ata_sff_port_init(ap);
5621270390e1STejun Heo 
5622c6fd2807SJeff Garzik 	return ap;
5623c6fd2807SJeff Garzik }
5624c6fd2807SJeff Garzik 
5625f0d36efdSTejun Heo static void ata_host_release(struct device *gendev, void *res)
5626f0d36efdSTejun Heo {
5627f0d36efdSTejun Heo 	struct ata_host *host = dev_get_drvdata(gendev);
5628f0d36efdSTejun Heo 	int i;
5629f0d36efdSTejun Heo 
5630f0d36efdSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
5631f0d36efdSTejun Heo 		struct ata_port *ap = host->ports[i];
5632f0d36efdSTejun Heo 
5633ecef7253STejun Heo 		if (!ap)
5634ecef7253STejun Heo 			continue;
5635ecef7253STejun Heo 
56364911487aSTejun Heo 		if (ap->scsi_host)
56371aa506e4STejun Heo 			scsi_host_put(ap->scsi_host);
56381aa506e4STejun Heo 
5639633273a3STejun Heo 		kfree(ap->pmp_link);
5640b1c72916STejun Heo 		kfree(ap->slave_link);
56414911487aSTejun Heo 		kfree(ap);
56421aa506e4STejun Heo 		host->ports[i] = NULL;
56431aa506e4STejun Heo 	}
56441aa506e4STejun Heo 
56451aa56ccaSTejun Heo 	dev_set_drvdata(gendev, NULL);
5646f0d36efdSTejun Heo }
5647f0d36efdSTejun Heo 
5648c6fd2807SJeff Garzik /**
5649f3187195STejun Heo  *	ata_host_alloc - allocate and init basic ATA host resources
5650f3187195STejun Heo  *	@dev: generic device this host is associated with
5651f3187195STejun Heo  *	@max_ports: maximum number of ATA ports associated with this host
5652f3187195STejun Heo  *
5653f3187195STejun Heo  *	Allocate and initialize basic ATA host resources.  LLD calls
5654f3187195STejun Heo  *	this function to allocate a host, initializes it fully and
5655f3187195STejun Heo  *	attaches it using ata_host_register().
5656f3187195STejun Heo  *
5657f3187195STejun Heo  *	@max_ports ports are allocated and host->n_ports is
5658f3187195STejun Heo  *	initialized to @max_ports.  The caller is allowed to decrease
5659f3187195STejun Heo  *	host->n_ports before calling ata_host_register().  The unused
5660f3187195STejun Heo  *	ports will be automatically freed on registration.
5661f3187195STejun Heo  *
5662f3187195STejun Heo  *	RETURNS:
5663f3187195STejun Heo  *	Allocate ATA host on success, NULL on failure.
5664f3187195STejun Heo  *
5665f3187195STejun Heo  *	LOCKING:
5666f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5667f3187195STejun Heo  */
5668f3187195STejun Heo struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
5669f3187195STejun Heo {
5670f3187195STejun Heo 	struct ata_host *host;
5671f3187195STejun Heo 	size_t sz;
5672f3187195STejun Heo 	int i;
5673f3187195STejun Heo 
5674f3187195STejun Heo 	DPRINTK("ENTER\n");
5675f3187195STejun Heo 
5676f3187195STejun Heo 	if (!devres_open_group(dev, NULL, GFP_KERNEL))
5677f3187195STejun Heo 		return NULL;
5678f3187195STejun Heo 
5679f3187195STejun Heo 	/* alloc a container for our list of ATA ports (buses) */
5680f3187195STejun Heo 	sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
5681f3187195STejun Heo 	/* alloc a container for our list of ATA ports (buses) */
5682f3187195STejun Heo 	host = devres_alloc(ata_host_release, sz, GFP_KERNEL);
5683f3187195STejun Heo 	if (!host)
5684f3187195STejun Heo 		goto err_out;
5685f3187195STejun Heo 
5686f3187195STejun Heo 	devres_add(dev, host);
5687f3187195STejun Heo 	dev_set_drvdata(dev, host);
5688f3187195STejun Heo 
5689f3187195STejun Heo 	spin_lock_init(&host->lock);
5690f3187195STejun Heo 	host->dev = dev;
5691f3187195STejun Heo 	host->n_ports = max_ports;
5692f3187195STejun Heo 
5693f3187195STejun Heo 	/* allocate ports bound to this host */
5694f3187195STejun Heo 	for (i = 0; i < max_ports; i++) {
5695f3187195STejun Heo 		struct ata_port *ap;
5696f3187195STejun Heo 
5697f3187195STejun Heo 		ap = ata_port_alloc(host);
5698f3187195STejun Heo 		if (!ap)
5699f3187195STejun Heo 			goto err_out;
5700f3187195STejun Heo 
5701f3187195STejun Heo 		ap->port_no = i;
5702f3187195STejun Heo 		host->ports[i] = ap;
5703f3187195STejun Heo 	}
5704f3187195STejun Heo 
5705f3187195STejun Heo 	devres_remove_group(dev, NULL);
5706f3187195STejun Heo 	return host;
5707f3187195STejun Heo 
5708f3187195STejun Heo  err_out:
5709f3187195STejun Heo 	devres_release_group(dev, NULL);
5710f3187195STejun Heo 	return NULL;
5711f3187195STejun Heo }
5712f3187195STejun Heo 
5713f3187195STejun Heo /**
5714f5cda257STejun Heo  *	ata_host_alloc_pinfo - alloc host and init with port_info array
5715f5cda257STejun Heo  *	@dev: generic device this host is associated with
5716f5cda257STejun Heo  *	@ppi: array of ATA port_info to initialize host with
5717f5cda257STejun Heo  *	@n_ports: number of ATA ports attached to this host
5718f5cda257STejun Heo  *
5719f5cda257STejun Heo  *	Allocate ATA host and initialize with info from @ppi.  If NULL
5720f5cda257STejun Heo  *	terminated, @ppi may contain fewer entries than @n_ports.  The
5721f5cda257STejun Heo  *	last entry will be used for the remaining ports.
5722f5cda257STejun Heo  *
5723f5cda257STejun Heo  *	RETURNS:
5724f5cda257STejun Heo  *	Allocate ATA host on success, NULL on failure.
5725f5cda257STejun Heo  *
5726f5cda257STejun Heo  *	LOCKING:
5727f5cda257STejun Heo  *	Inherited from calling layer (may sleep).
5728f5cda257STejun Heo  */
5729f5cda257STejun Heo struct ata_host *ata_host_alloc_pinfo(struct device *dev,
5730f5cda257STejun Heo 				      const struct ata_port_info * const * ppi,
5731f5cda257STejun Heo 				      int n_ports)
5732f5cda257STejun Heo {
5733f5cda257STejun Heo 	const struct ata_port_info *pi;
5734f5cda257STejun Heo 	struct ata_host *host;
5735f5cda257STejun Heo 	int i, j;
5736f5cda257STejun Heo 
5737f5cda257STejun Heo 	host = ata_host_alloc(dev, n_ports);
5738f5cda257STejun Heo 	if (!host)
5739f5cda257STejun Heo 		return NULL;
5740f5cda257STejun Heo 
5741f5cda257STejun Heo 	for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
5742f5cda257STejun Heo 		struct ata_port *ap = host->ports[i];
5743f5cda257STejun Heo 
5744f5cda257STejun Heo 		if (ppi[j])
5745f5cda257STejun Heo 			pi = ppi[j++];
5746f5cda257STejun Heo 
5747f5cda257STejun Heo 		ap->pio_mask = pi->pio_mask;
5748f5cda257STejun Heo 		ap->mwdma_mask = pi->mwdma_mask;
5749f5cda257STejun Heo 		ap->udma_mask = pi->udma_mask;
5750f5cda257STejun Heo 		ap->flags |= pi->flags;
57510c88758bSTejun Heo 		ap->link.flags |= pi->link_flags;
5752f5cda257STejun Heo 		ap->ops = pi->port_ops;
5753f5cda257STejun Heo 
5754f5cda257STejun Heo 		if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
5755f5cda257STejun Heo 			host->ops = pi->port_ops;
5756f5cda257STejun Heo 	}
5757f5cda257STejun Heo 
5758f5cda257STejun Heo 	return host;
5759f5cda257STejun Heo }
5760f5cda257STejun Heo 
5761b1c72916STejun Heo /**
5762b1c72916STejun Heo  *	ata_slave_link_init - initialize slave link
5763b1c72916STejun Heo  *	@ap: port to initialize slave link for
5764b1c72916STejun Heo  *
5765b1c72916STejun Heo  *	Create and initialize slave link for @ap.  This enables slave
5766b1c72916STejun Heo  *	link handling on the port.
5767b1c72916STejun Heo  *
5768b1c72916STejun Heo  *	In libata, a port contains links and a link contains devices.
5769b1c72916STejun Heo  *	There is single host link but if a PMP is attached to it,
5770b1c72916STejun Heo  *	there can be multiple fan-out links.  On SATA, there's usually
5771b1c72916STejun Heo  *	a single device connected to a link but PATA and SATA
5772b1c72916STejun Heo  *	controllers emulating TF based interface can have two - master
5773b1c72916STejun Heo  *	and slave.
5774b1c72916STejun Heo  *
5775b1c72916STejun Heo  *	However, there are a few controllers which don't fit into this
5776b1c72916STejun Heo  *	abstraction too well - SATA controllers which emulate TF
5777b1c72916STejun Heo  *	interface with both master and slave devices but also have
5778b1c72916STejun Heo  *	separate SCR register sets for each device.  These controllers
5779b1c72916STejun Heo  *	need separate links for physical link handling
5780b1c72916STejun Heo  *	(e.g. onlineness, link speed) but should be treated like a
5781b1c72916STejun Heo  *	traditional M/S controller for everything else (e.g. command
5782b1c72916STejun Heo  *	issue, softreset).
5783b1c72916STejun Heo  *
5784b1c72916STejun Heo  *	slave_link is libata's way of handling this class of
5785b1c72916STejun Heo  *	controllers without impacting core layer too much.  For
5786b1c72916STejun Heo  *	anything other than physical link handling, the default host
5787b1c72916STejun Heo  *	link is used for both master and slave.  For physical link
5788b1c72916STejun Heo  *	handling, separate @ap->slave_link is used.  All dirty details
5789b1c72916STejun Heo  *	are implemented inside libata core layer.  From LLD's POV, the
5790b1c72916STejun Heo  *	only difference is that prereset, hardreset and postreset are
5791b1c72916STejun Heo  *	called once more for the slave link, so the reset sequence
5792b1c72916STejun Heo  *	looks like the following.
5793b1c72916STejun Heo  *
5794b1c72916STejun Heo  *	prereset(M) -> prereset(S) -> hardreset(M) -> hardreset(S) ->
5795b1c72916STejun Heo  *	softreset(M) -> postreset(M) -> postreset(S)
5796b1c72916STejun Heo  *
5797b1c72916STejun Heo  *	Note that softreset is called only for the master.  Softreset
5798b1c72916STejun Heo  *	resets both M/S by definition, so SRST on master should handle
5799b1c72916STejun Heo  *	both (the standard method will work just fine).
5800b1c72916STejun Heo  *
5801b1c72916STejun Heo  *	LOCKING:
5802b1c72916STejun Heo  *	Should be called before host is registered.
5803b1c72916STejun Heo  *
5804b1c72916STejun Heo  *	RETURNS:
5805b1c72916STejun Heo  *	0 on success, -errno on failure.
5806b1c72916STejun Heo  */
5807b1c72916STejun Heo int ata_slave_link_init(struct ata_port *ap)
5808b1c72916STejun Heo {
5809b1c72916STejun Heo 	struct ata_link *link;
5810b1c72916STejun Heo 
5811b1c72916STejun Heo 	WARN_ON(ap->slave_link);
5812b1c72916STejun Heo 	WARN_ON(ap->flags & ATA_FLAG_PMP);
5813b1c72916STejun Heo 
5814b1c72916STejun Heo 	link = kzalloc(sizeof(*link), GFP_KERNEL);
5815b1c72916STejun Heo 	if (!link)
5816b1c72916STejun Heo 		return -ENOMEM;
5817b1c72916STejun Heo 
5818b1c72916STejun Heo 	ata_link_init(ap, link, 1);
5819b1c72916STejun Heo 	ap->slave_link = link;
5820b1c72916STejun Heo 	return 0;
5821b1c72916STejun Heo }
5822b1c72916STejun Heo 
582332ebbc0cSTejun Heo static void ata_host_stop(struct device *gendev, void *res)
582432ebbc0cSTejun Heo {
582532ebbc0cSTejun Heo 	struct ata_host *host = dev_get_drvdata(gendev);
582632ebbc0cSTejun Heo 	int i;
582732ebbc0cSTejun Heo 
582832ebbc0cSTejun Heo 	WARN_ON(!(host->flags & ATA_HOST_STARTED));
582932ebbc0cSTejun Heo 
583032ebbc0cSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
583132ebbc0cSTejun Heo 		struct ata_port *ap = host->ports[i];
583232ebbc0cSTejun Heo 
583332ebbc0cSTejun Heo 		if (ap->ops->port_stop)
583432ebbc0cSTejun Heo 			ap->ops->port_stop(ap);
583532ebbc0cSTejun Heo 	}
583632ebbc0cSTejun Heo 
583732ebbc0cSTejun Heo 	if (host->ops->host_stop)
583832ebbc0cSTejun Heo 		host->ops->host_stop(host);
583932ebbc0cSTejun Heo }
584032ebbc0cSTejun Heo 
5841f5cda257STejun Heo /**
5842029cfd6bSTejun Heo  *	ata_finalize_port_ops - finalize ata_port_operations
5843029cfd6bSTejun Heo  *	@ops: ata_port_operations to finalize
5844029cfd6bSTejun Heo  *
5845029cfd6bSTejun Heo  *	An ata_port_operations can inherit from another ops and that
5846029cfd6bSTejun Heo  *	ops can again inherit from another.  This can go on as many
5847029cfd6bSTejun Heo  *	times as necessary as long as there is no loop in the
5848029cfd6bSTejun Heo  *	inheritance chain.
5849029cfd6bSTejun Heo  *
5850029cfd6bSTejun Heo  *	Ops tables are finalized when the host is started.  NULL or
5851029cfd6bSTejun Heo  *	unspecified entries are inherited from the closet ancestor
5852029cfd6bSTejun Heo  *	which has the method and the entry is populated with it.
5853029cfd6bSTejun Heo  *	After finalization, the ops table directly points to all the
5854029cfd6bSTejun Heo  *	methods and ->inherits is no longer necessary and cleared.
5855029cfd6bSTejun Heo  *
5856029cfd6bSTejun Heo  *	Using ATA_OP_NULL, inheriting ops can force a method to NULL.
5857029cfd6bSTejun Heo  *
5858029cfd6bSTejun Heo  *	LOCKING:
5859029cfd6bSTejun Heo  *	None.
5860029cfd6bSTejun Heo  */
5861029cfd6bSTejun Heo static void ata_finalize_port_ops(struct ata_port_operations *ops)
5862029cfd6bSTejun Heo {
58632da67659SPradeep Singh Rautela 	static DEFINE_SPINLOCK(lock);
5864029cfd6bSTejun Heo 	const struct ata_port_operations *cur;
5865029cfd6bSTejun Heo 	void **begin = (void **)ops;
5866029cfd6bSTejun Heo 	void **end = (void **)&ops->inherits;
5867029cfd6bSTejun Heo 	void **pp;
5868029cfd6bSTejun Heo 
5869029cfd6bSTejun Heo 	if (!ops || !ops->inherits)
5870029cfd6bSTejun Heo 		return;
5871029cfd6bSTejun Heo 
5872029cfd6bSTejun Heo 	spin_lock(&lock);
5873029cfd6bSTejun Heo 
5874029cfd6bSTejun Heo 	for (cur = ops->inherits; cur; cur = cur->inherits) {
5875029cfd6bSTejun Heo 		void **inherit = (void **)cur;
5876029cfd6bSTejun Heo 
5877029cfd6bSTejun Heo 		for (pp = begin; pp < end; pp++, inherit++)
5878029cfd6bSTejun Heo 			if (!*pp)
5879029cfd6bSTejun Heo 				*pp = *inherit;
5880029cfd6bSTejun Heo 	}
5881029cfd6bSTejun Heo 
5882029cfd6bSTejun Heo 	for (pp = begin; pp < end; pp++)
5883029cfd6bSTejun Heo 		if (IS_ERR(*pp))
5884029cfd6bSTejun Heo 			*pp = NULL;
5885029cfd6bSTejun Heo 
5886029cfd6bSTejun Heo 	ops->inherits = NULL;
5887029cfd6bSTejun Heo 
5888029cfd6bSTejun Heo 	spin_unlock(&lock);
5889029cfd6bSTejun Heo }
5890029cfd6bSTejun Heo 
5891029cfd6bSTejun Heo /**
5892ecef7253STejun Heo  *	ata_host_start - start and freeze ports of an ATA host
5893ecef7253STejun Heo  *	@host: ATA host to start ports for
5894ecef7253STejun Heo  *
5895ecef7253STejun Heo  *	Start and then freeze ports of @host.  Started status is
5896ecef7253STejun Heo  *	recorded in host->flags, so this function can be called
5897ecef7253STejun Heo  *	multiple times.  Ports are guaranteed to get started only
5898f3187195STejun Heo  *	once.  If host->ops isn't initialized yet, its set to the
5899f3187195STejun Heo  *	first non-dummy port ops.
5900ecef7253STejun Heo  *
5901ecef7253STejun Heo  *	LOCKING:
5902ecef7253STejun Heo  *	Inherited from calling layer (may sleep).
5903ecef7253STejun Heo  *
5904ecef7253STejun Heo  *	RETURNS:
5905ecef7253STejun Heo  *	0 if all ports are started successfully, -errno otherwise.
5906ecef7253STejun Heo  */
5907ecef7253STejun Heo int ata_host_start(struct ata_host *host)
5908ecef7253STejun Heo {
590932ebbc0cSTejun Heo 	int have_stop = 0;
591032ebbc0cSTejun Heo 	void *start_dr = NULL;
5911ecef7253STejun Heo 	int i, rc;
5912ecef7253STejun Heo 
5913ecef7253STejun Heo 	if (host->flags & ATA_HOST_STARTED)
5914ecef7253STejun Heo 		return 0;
5915ecef7253STejun Heo 
5916029cfd6bSTejun Heo 	ata_finalize_port_ops(host->ops);
5917029cfd6bSTejun Heo 
5918ecef7253STejun Heo 	for (i = 0; i < host->n_ports; i++) {
5919ecef7253STejun Heo 		struct ata_port *ap = host->ports[i];
5920ecef7253STejun Heo 
5921029cfd6bSTejun Heo 		ata_finalize_port_ops(ap->ops);
5922029cfd6bSTejun Heo 
5923f3187195STejun Heo 		if (!host->ops && !ata_port_is_dummy(ap))
5924f3187195STejun Heo 			host->ops = ap->ops;
5925f3187195STejun Heo 
592632ebbc0cSTejun Heo 		if (ap->ops->port_stop)
592732ebbc0cSTejun Heo 			have_stop = 1;
592832ebbc0cSTejun Heo 	}
592932ebbc0cSTejun Heo 
593032ebbc0cSTejun Heo 	if (host->ops->host_stop)
593132ebbc0cSTejun Heo 		have_stop = 1;
593232ebbc0cSTejun Heo 
593332ebbc0cSTejun Heo 	if (have_stop) {
593432ebbc0cSTejun Heo 		start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
593532ebbc0cSTejun Heo 		if (!start_dr)
593632ebbc0cSTejun Heo 			return -ENOMEM;
593732ebbc0cSTejun Heo 	}
593832ebbc0cSTejun Heo 
593932ebbc0cSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
594032ebbc0cSTejun Heo 		struct ata_port *ap = host->ports[i];
594132ebbc0cSTejun Heo 
5942ecef7253STejun Heo 		if (ap->ops->port_start) {
5943ecef7253STejun Heo 			rc = ap->ops->port_start(ap);
5944ecef7253STejun Heo 			if (rc) {
59450f9fe9b7SAlan Cox 				if (rc != -ENODEV)
59460f757743SAndrew Morton 					dev_printk(KERN_ERR, host->dev,
59470f757743SAndrew Morton 						"failed to start port %d "
59480f757743SAndrew Morton 						"(errno=%d)\n", i, rc);
5949ecef7253STejun Heo 				goto err_out;
5950ecef7253STejun Heo 			}
5951ecef7253STejun Heo 		}
5952ecef7253STejun Heo 		ata_eh_freeze_port(ap);
5953ecef7253STejun Heo 	}
5954ecef7253STejun Heo 
595532ebbc0cSTejun Heo 	if (start_dr)
595632ebbc0cSTejun Heo 		devres_add(host->dev, start_dr);
5957ecef7253STejun Heo 	host->flags |= ATA_HOST_STARTED;
5958ecef7253STejun Heo 	return 0;
5959ecef7253STejun Heo 
5960ecef7253STejun Heo  err_out:
5961ecef7253STejun Heo 	while (--i >= 0) {
5962ecef7253STejun Heo 		struct ata_port *ap = host->ports[i];
5963ecef7253STejun Heo 
5964ecef7253STejun Heo 		if (ap->ops->port_stop)
5965ecef7253STejun Heo 			ap->ops->port_stop(ap);
5966ecef7253STejun Heo 	}
596732ebbc0cSTejun Heo 	devres_free(start_dr);
5968ecef7253STejun Heo 	return rc;
5969ecef7253STejun Heo }
5970ecef7253STejun Heo 
5971ecef7253STejun Heo /**
5972cca3974eSJeff Garzik  *	ata_sas_host_init - Initialize a host struct
5973cca3974eSJeff Garzik  *	@host:	host to initialize
5974cca3974eSJeff Garzik  *	@dev:	device host is attached to
5975cca3974eSJeff Garzik  *	@flags:	host flags
5976c6fd2807SJeff Garzik  *	@ops:	port_ops
5977c6fd2807SJeff Garzik  *
5978c6fd2807SJeff Garzik  *	LOCKING:
5979c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
5980c6fd2807SJeff Garzik  *
5981c6fd2807SJeff Garzik  */
5982f3187195STejun Heo /* KILLME - the only user left is ipr */
5983cca3974eSJeff Garzik void ata_host_init(struct ata_host *host, struct device *dev,
5984029cfd6bSTejun Heo 		   unsigned long flags, struct ata_port_operations *ops)
5985c6fd2807SJeff Garzik {
5986cca3974eSJeff Garzik 	spin_lock_init(&host->lock);
5987cca3974eSJeff Garzik 	host->dev = dev;
5988cca3974eSJeff Garzik 	host->flags = flags;
5989cca3974eSJeff Garzik 	host->ops = ops;
5990c6fd2807SJeff Garzik }
5991c6fd2807SJeff Garzik 
599279318057SArjan van de Ven 
599379318057SArjan van de Ven static void async_port_probe(void *data, async_cookie_t cookie)
599479318057SArjan van de Ven {
599579318057SArjan van de Ven 	int rc;
599679318057SArjan van de Ven 	struct ata_port *ap = data;
5997886ad09fSArjan van de Ven 
5998886ad09fSArjan van de Ven 	/*
5999886ad09fSArjan van de Ven 	 * If we're not allowed to scan this host in parallel,
6000886ad09fSArjan van de Ven 	 * we need to wait until all previous scans have completed
6001886ad09fSArjan van de Ven 	 * before going further.
6002fa853a48SArjan van de Ven 	 * Jeff Garzik says this is only within a controller, so we
6003fa853a48SArjan van de Ven 	 * don't need to wait for port 0, only for later ports.
6004886ad09fSArjan van de Ven 	 */
6005fa853a48SArjan van de Ven 	if (!(ap->host->flags & ATA_HOST_PARALLEL_SCAN) && ap->port_no != 0)
6006886ad09fSArjan van de Ven 		async_synchronize_cookie(cookie);
6007886ad09fSArjan van de Ven 
600879318057SArjan van de Ven 	/* probe */
600979318057SArjan van de Ven 	if (ap->ops->error_handler) {
601079318057SArjan van de Ven 		struct ata_eh_info *ehi = &ap->link.eh_info;
601179318057SArjan van de Ven 		unsigned long flags;
601279318057SArjan van de Ven 
601379318057SArjan van de Ven 		/* kick EH for boot probing */
601479318057SArjan van de Ven 		spin_lock_irqsave(ap->lock, flags);
601579318057SArjan van de Ven 
601679318057SArjan van de Ven 		ehi->probe_mask |= ATA_ALL_DEVICES;
601779318057SArjan van de Ven 		ehi->action |= ATA_EH_RESET | ATA_EH_LPM;
601879318057SArjan van de Ven 		ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
601979318057SArjan van de Ven 
602079318057SArjan van de Ven 		ap->pflags &= ~ATA_PFLAG_INITIALIZING;
602179318057SArjan van de Ven 		ap->pflags |= ATA_PFLAG_LOADING;
602279318057SArjan van de Ven 		ata_port_schedule_eh(ap);
602379318057SArjan van de Ven 
602479318057SArjan van de Ven 		spin_unlock_irqrestore(ap->lock, flags);
602579318057SArjan van de Ven 
602679318057SArjan van de Ven 		/* wait for EH to finish */
602779318057SArjan van de Ven 		ata_port_wait_eh(ap);
602879318057SArjan van de Ven 	} else {
602979318057SArjan van de Ven 		DPRINTK("ata%u: bus probe begin\n", ap->print_id);
603079318057SArjan van de Ven 		rc = ata_bus_probe(ap);
603179318057SArjan van de Ven 		DPRINTK("ata%u: bus probe end\n", ap->print_id);
603279318057SArjan van de Ven 
603379318057SArjan van de Ven 		if (rc) {
603479318057SArjan van de Ven 			/* FIXME: do something useful here?
603579318057SArjan van de Ven 			 * Current libata behavior will
603679318057SArjan van de Ven 			 * tear down everything when
603779318057SArjan van de Ven 			 * the module is removed
603879318057SArjan van de Ven 			 * or the h/w is unplugged.
603979318057SArjan van de Ven 			 */
604079318057SArjan van de Ven 		}
604179318057SArjan van de Ven 	}
6042f29d3b23SArjan van de Ven 
6043f29d3b23SArjan van de Ven 	/* in order to keep device order, we need to synchronize at this point */
6044f29d3b23SArjan van de Ven 	async_synchronize_cookie(cookie);
6045f29d3b23SArjan van de Ven 
6046f29d3b23SArjan van de Ven 	ata_scsi_scan_host(ap, 1);
6047f29d3b23SArjan van de Ven 
604879318057SArjan van de Ven }
6049c6fd2807SJeff Garzik /**
6050f3187195STejun Heo  *	ata_host_register - register initialized ATA host
6051f3187195STejun Heo  *	@host: ATA host to register
6052f3187195STejun Heo  *	@sht: template for SCSI host
6053c6fd2807SJeff Garzik  *
6054f3187195STejun Heo  *	Register initialized ATA host.  @host is allocated using
6055f3187195STejun Heo  *	ata_host_alloc() and fully initialized by LLD.  This function
6056f3187195STejun Heo  *	starts ports, registers @host with ATA and SCSI layers and
6057f3187195STejun Heo  *	probe registered devices.
6058c6fd2807SJeff Garzik  *
6059c6fd2807SJeff Garzik  *	LOCKING:
6060f3187195STejun Heo  *	Inherited from calling layer (may sleep).
6061c6fd2807SJeff Garzik  *
6062c6fd2807SJeff Garzik  *	RETURNS:
6063f3187195STejun Heo  *	0 on success, -errno otherwise.
6064c6fd2807SJeff Garzik  */
6065f3187195STejun Heo int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
6066c6fd2807SJeff Garzik {
6067f3187195STejun Heo 	int i, rc;
6068c6fd2807SJeff Garzik 
6069f3187195STejun Heo 	/* host must have been started */
6070f3187195STejun Heo 	if (!(host->flags & ATA_HOST_STARTED)) {
6071f3187195STejun Heo 		dev_printk(KERN_ERR, host->dev,
6072f3187195STejun Heo 			   "BUG: trying to register unstarted host\n");
6073f3187195STejun Heo 		WARN_ON(1);
6074f3187195STejun Heo 		return -EINVAL;
607502f076aaSAlan Cox 	}
6076f0d36efdSTejun Heo 
6077f3187195STejun Heo 	/* Blow away unused ports.  This happens when LLD can't
6078f3187195STejun Heo 	 * determine the exact number of ports to allocate at
6079f3187195STejun Heo 	 * allocation time.
6080f3187195STejun Heo 	 */
6081f3187195STejun Heo 	for (i = host->n_ports; host->ports[i]; i++)
6082f3187195STejun Heo 		kfree(host->ports[i]);
6083f0d36efdSTejun Heo 
6084f3187195STejun Heo 	/* give ports names and add SCSI hosts */
6085f3187195STejun Heo 	for (i = 0; i < host->n_ports; i++)
6086f3187195STejun Heo 		host->ports[i]->print_id = ata_print_id++;
6087c6fd2807SJeff Garzik 
6088f3187195STejun Heo 	rc = ata_scsi_add_hosts(host, sht);
6089ecef7253STejun Heo 	if (rc)
6090f3187195STejun Heo 		return rc;
6091ecef7253STejun Heo 
6092fafbae87STejun Heo 	/* associate with ACPI nodes */
6093fafbae87STejun Heo 	ata_acpi_associate(host);
6094fafbae87STejun Heo 
6095f3187195STejun Heo 	/* set cable, sata_spd_limit and report */
6096cca3974eSJeff Garzik 	for (i = 0; i < host->n_ports; i++) {
6097cca3974eSJeff Garzik 		struct ata_port *ap = host->ports[i];
6098f3187195STejun Heo 		unsigned long xfer_mask;
6099f3187195STejun Heo 
6100f3187195STejun Heo 		/* set SATA cable type if still unset */
6101f3187195STejun Heo 		if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
6102f3187195STejun Heo 			ap->cbl = ATA_CBL_SATA;
6103c6fd2807SJeff Garzik 
6104c6fd2807SJeff Garzik 		/* init sata_spd_limit to the current value */
61054fb37a25STejun Heo 		sata_link_init_spd(&ap->link);
6106b1c72916STejun Heo 		if (ap->slave_link)
6107b1c72916STejun Heo 			sata_link_init_spd(ap->slave_link);
6108c6fd2807SJeff Garzik 
6109cbcdd875STejun Heo 		/* print per-port info to dmesg */
6110f3187195STejun Heo 		xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
6111f3187195STejun Heo 					      ap->udma_mask);
6112f3187195STejun Heo 
6113abf6e8edSTejun Heo 		if (!ata_port_is_dummy(ap)) {
6114cbcdd875STejun Heo 			ata_port_printk(ap, KERN_INFO,
6115cbcdd875STejun Heo 					"%cATA max %s %s\n",
6116a16abc0bSTejun Heo 					(ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
6117f3187195STejun Heo 					ata_mode_string(xfer_mask),
6118cbcdd875STejun Heo 					ap->link.eh_info.desc);
6119abf6e8edSTejun Heo 			ata_ehi_clear_desc(&ap->link.eh_info);
6120abf6e8edSTejun Heo 		} else
6121f3187195STejun Heo 			ata_port_printk(ap, KERN_INFO, "DUMMY\n");
6122c6fd2807SJeff Garzik 	}
6123c6fd2807SJeff Garzik 
6124f6005354SVegard Nossum 	/* perform each probe asynchronously */
6125f3187195STejun Heo 	for (i = 0; i < host->n_ports; i++) {
6126f3187195STejun Heo 		struct ata_port *ap = host->ports[i];
612779318057SArjan van de Ven 		async_schedule(async_port_probe, ap);
6128c6fd2807SJeff Garzik 	}
6129c6fd2807SJeff Garzik 
6130f3187195STejun Heo 	return 0;
6131f3187195STejun Heo }
6132f3187195STejun Heo 
6133f3187195STejun Heo /**
6134f5cda257STejun Heo  *	ata_host_activate - start host, request IRQ and register it
6135f5cda257STejun Heo  *	@host: target ATA host
6136f5cda257STejun Heo  *	@irq: IRQ to request
6137f5cda257STejun Heo  *	@irq_handler: irq_handler used when requesting IRQ
6138f5cda257STejun Heo  *	@irq_flags: irq_flags used when requesting IRQ
6139f5cda257STejun Heo  *	@sht: scsi_host_template to use when registering the host
6140f5cda257STejun Heo  *
6141f5cda257STejun Heo  *	After allocating an ATA host and initializing it, most libata
6142f5cda257STejun Heo  *	LLDs perform three steps to activate the host - start host,
6143f5cda257STejun Heo  *	request IRQ and register it.  This helper takes necessasry
6144f5cda257STejun Heo  *	arguments and performs the three steps in one go.
6145f5cda257STejun Heo  *
61463d46b2e2SPaul Mundt  *	An invalid IRQ skips the IRQ registration and expects the host to
61473d46b2e2SPaul Mundt  *	have set polling mode on the port. In this case, @irq_handler
61483d46b2e2SPaul Mundt  *	should be NULL.
61493d46b2e2SPaul Mundt  *
6150f5cda257STejun Heo  *	LOCKING:
6151f5cda257STejun Heo  *	Inherited from calling layer (may sleep).
6152f5cda257STejun Heo  *
6153f5cda257STejun Heo  *	RETURNS:
6154f5cda257STejun Heo  *	0 on success, -errno otherwise.
6155f5cda257STejun Heo  */
6156f5cda257STejun Heo int ata_host_activate(struct ata_host *host, int irq,
6157f5cda257STejun Heo 		      irq_handler_t irq_handler, unsigned long irq_flags,
6158f5cda257STejun Heo 		      struct scsi_host_template *sht)
6159f5cda257STejun Heo {
6160cbcdd875STejun Heo 	int i, rc;
6161f5cda257STejun Heo 
6162f5cda257STejun Heo 	rc = ata_host_start(host);
6163f5cda257STejun Heo 	if (rc)
6164f5cda257STejun Heo 		return rc;
6165f5cda257STejun Heo 
61663d46b2e2SPaul Mundt 	/* Special case for polling mode */
61673d46b2e2SPaul Mundt 	if (!irq) {
61683d46b2e2SPaul Mundt 		WARN_ON(irq_handler);
61693d46b2e2SPaul Mundt 		return ata_host_register(host, sht);
61703d46b2e2SPaul Mundt 	}
61713d46b2e2SPaul Mundt 
6172f5cda257STejun Heo 	rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
6173f5cda257STejun Heo 			      dev_driver_string(host->dev), host);
6174f5cda257STejun Heo 	if (rc)
6175f5cda257STejun Heo 		return rc;
6176f5cda257STejun Heo 
6177cbcdd875STejun Heo 	for (i = 0; i < host->n_ports; i++)
6178cbcdd875STejun Heo 		ata_port_desc(host->ports[i], "irq %d", irq);
61794031826bSTejun Heo 
6180f5cda257STejun Heo 	rc = ata_host_register(host, sht);
6181f5cda257STejun Heo 	/* if failed, just free the IRQ and leave ports alone */
6182f5cda257STejun Heo 	if (rc)
6183f5cda257STejun Heo 		devm_free_irq(host->dev, irq, host);
6184f5cda257STejun Heo 
6185f5cda257STejun Heo 	return rc;
6186f5cda257STejun Heo }
6187f5cda257STejun Heo 
6188f5cda257STejun Heo /**
6189c6fd2807SJeff Garzik  *	ata_port_detach - Detach ATA port in prepration of device removal
6190c6fd2807SJeff Garzik  *	@ap: ATA port to be detached
6191c6fd2807SJeff Garzik  *
6192c6fd2807SJeff Garzik  *	Detach all ATA devices and the associated SCSI devices of @ap;
6193c6fd2807SJeff Garzik  *	then, remove the associated SCSI host.  @ap is guaranteed to
6194c6fd2807SJeff Garzik  *	be quiescent on return from this function.
6195c6fd2807SJeff Garzik  *
6196c6fd2807SJeff Garzik  *	LOCKING:
6197c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
6198c6fd2807SJeff Garzik  */
6199741b7763SAdrian Bunk static void ata_port_detach(struct ata_port *ap)
6200c6fd2807SJeff Garzik {
6201c6fd2807SJeff Garzik 	unsigned long flags;
6202c6fd2807SJeff Garzik 
6203c6fd2807SJeff Garzik 	if (!ap->ops->error_handler)
6204c6fd2807SJeff Garzik 		goto skip_eh;
6205c6fd2807SJeff Garzik 
6206c6fd2807SJeff Garzik 	/* tell EH we're leaving & flush EH */
6207c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
6208c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_UNLOADING;
6209ece180d1STejun Heo 	ata_port_schedule_eh(ap);
6210c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
6211c6fd2807SJeff Garzik 
6212ece180d1STejun Heo 	/* wait till EH commits suicide */
6213c6fd2807SJeff Garzik 	ata_port_wait_eh(ap);
6214c6fd2807SJeff Garzik 
6215ece180d1STejun Heo 	/* it better be dead now */
6216ece180d1STejun Heo 	WARN_ON(!(ap->pflags & ATA_PFLAG_UNLOADED));
6217c6fd2807SJeff Garzik 
621845a66c1cSOleg Nesterov 	cancel_rearming_delayed_work(&ap->hotplug_task);
6219c6fd2807SJeff Garzik 
6220c6fd2807SJeff Garzik  skip_eh:
6221c6fd2807SJeff Garzik 	/* remove the associated SCSI host */
6222cca3974eSJeff Garzik 	scsi_remove_host(ap->scsi_host);
6223c6fd2807SJeff Garzik }
6224c6fd2807SJeff Garzik 
6225c6fd2807SJeff Garzik /**
62260529c159STejun Heo  *	ata_host_detach - Detach all ports of an ATA host
62270529c159STejun Heo  *	@host: Host to detach
62280529c159STejun Heo  *
62290529c159STejun Heo  *	Detach all ports of @host.
62300529c159STejun Heo  *
62310529c159STejun Heo  *	LOCKING:
62320529c159STejun Heo  *	Kernel thread context (may sleep).
62330529c159STejun Heo  */
62340529c159STejun Heo void ata_host_detach(struct ata_host *host)
62350529c159STejun Heo {
62360529c159STejun Heo 	int i;
62370529c159STejun Heo 
62380529c159STejun Heo 	for (i = 0; i < host->n_ports; i++)
62390529c159STejun Heo 		ata_port_detach(host->ports[i]);
6240562f0c2dSTejun Heo 
6241562f0c2dSTejun Heo 	/* the host is dead now, dissociate ACPI */
6242562f0c2dSTejun Heo 	ata_acpi_dissociate(host);
62430529c159STejun Heo }
62440529c159STejun Heo 
6245c6fd2807SJeff Garzik #ifdef CONFIG_PCI
6246c6fd2807SJeff Garzik 
6247c6fd2807SJeff Garzik /**
6248c6fd2807SJeff Garzik  *	ata_pci_remove_one - PCI layer callback for device removal
6249c6fd2807SJeff Garzik  *	@pdev: PCI device that was removed
6250c6fd2807SJeff Garzik  *
6251b878ca5dSTejun Heo  *	PCI layer indicates to libata via this hook that hot-unplug or
6252b878ca5dSTejun Heo  *	module unload event has occurred.  Detach all ports.  Resource
6253b878ca5dSTejun Heo  *	release is handled via devres.
6254c6fd2807SJeff Garzik  *
6255c6fd2807SJeff Garzik  *	LOCKING:
6256c6fd2807SJeff Garzik  *	Inherited from PCI layer (may sleep).
6257c6fd2807SJeff Garzik  */
6258c6fd2807SJeff Garzik void ata_pci_remove_one(struct pci_dev *pdev)
6259c6fd2807SJeff Garzik {
62602855568bSJeff Garzik 	struct device *dev = &pdev->dev;
6261cca3974eSJeff Garzik 	struct ata_host *host = dev_get_drvdata(dev);
6262c6fd2807SJeff Garzik 
6263f0d36efdSTejun Heo 	ata_host_detach(host);
6264c6fd2807SJeff Garzik }
6265c6fd2807SJeff Garzik 
6266c6fd2807SJeff Garzik /* move to PCI subsystem */
6267c6fd2807SJeff Garzik int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
6268c6fd2807SJeff Garzik {
6269c6fd2807SJeff Garzik 	unsigned long tmp = 0;
6270c6fd2807SJeff Garzik 
6271c6fd2807SJeff Garzik 	switch (bits->width) {
6272c6fd2807SJeff Garzik 	case 1: {
6273c6fd2807SJeff Garzik 		u8 tmp8 = 0;
6274c6fd2807SJeff Garzik 		pci_read_config_byte(pdev, bits->reg, &tmp8);
6275c6fd2807SJeff Garzik 		tmp = tmp8;
6276c6fd2807SJeff Garzik 		break;
6277c6fd2807SJeff Garzik 	}
6278c6fd2807SJeff Garzik 	case 2: {
6279c6fd2807SJeff Garzik 		u16 tmp16 = 0;
6280c6fd2807SJeff Garzik 		pci_read_config_word(pdev, bits->reg, &tmp16);
6281c6fd2807SJeff Garzik 		tmp = tmp16;
6282c6fd2807SJeff Garzik 		break;
6283c6fd2807SJeff Garzik 	}
6284c6fd2807SJeff Garzik 	case 4: {
6285c6fd2807SJeff Garzik 		u32 tmp32 = 0;
6286c6fd2807SJeff Garzik 		pci_read_config_dword(pdev, bits->reg, &tmp32);
6287c6fd2807SJeff Garzik 		tmp = tmp32;
6288c6fd2807SJeff Garzik 		break;
6289c6fd2807SJeff Garzik 	}
6290c6fd2807SJeff Garzik 
6291c6fd2807SJeff Garzik 	default:
6292c6fd2807SJeff Garzik 		return -EINVAL;
6293c6fd2807SJeff Garzik 	}
6294c6fd2807SJeff Garzik 
6295c6fd2807SJeff Garzik 	tmp &= bits->mask;
6296c6fd2807SJeff Garzik 
6297c6fd2807SJeff Garzik 	return (tmp == bits->val) ? 1 : 0;
6298c6fd2807SJeff Garzik }
6299c6fd2807SJeff Garzik 
63006ffa01d8STejun Heo #ifdef CONFIG_PM
6301c6fd2807SJeff Garzik void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
6302c6fd2807SJeff Garzik {
6303c6fd2807SJeff Garzik 	pci_save_state(pdev);
6304c6fd2807SJeff Garzik 	pci_disable_device(pdev);
63054c90d971STejun Heo 
63063a2d5b70SRafael J. Wysocki 	if (mesg.event & PM_EVENT_SLEEP)
6307c6fd2807SJeff Garzik 		pci_set_power_state(pdev, PCI_D3hot);
6308c6fd2807SJeff Garzik }
6309c6fd2807SJeff Garzik 
6310553c4aa6STejun Heo int ata_pci_device_do_resume(struct pci_dev *pdev)
6311c6fd2807SJeff Garzik {
6312553c4aa6STejun Heo 	int rc;
6313553c4aa6STejun Heo 
6314c6fd2807SJeff Garzik 	pci_set_power_state(pdev, PCI_D0);
6315c6fd2807SJeff Garzik 	pci_restore_state(pdev);
6316553c4aa6STejun Heo 
6317f0d36efdSTejun Heo 	rc = pcim_enable_device(pdev);
6318553c4aa6STejun Heo 	if (rc) {
6319553c4aa6STejun Heo 		dev_printk(KERN_ERR, &pdev->dev,
6320553c4aa6STejun Heo 			   "failed to enable device after resume (%d)\n", rc);
6321553c4aa6STejun Heo 		return rc;
6322553c4aa6STejun Heo 	}
6323553c4aa6STejun Heo 
6324c6fd2807SJeff Garzik 	pci_set_master(pdev);
6325553c4aa6STejun Heo 	return 0;
6326c6fd2807SJeff Garzik }
6327c6fd2807SJeff Garzik 
6328c6fd2807SJeff Garzik int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
6329c6fd2807SJeff Garzik {
6330cca3974eSJeff Garzik 	struct ata_host *host = dev_get_drvdata(&pdev->dev);
6331c6fd2807SJeff Garzik 	int rc = 0;
6332c6fd2807SJeff Garzik 
6333cca3974eSJeff Garzik 	rc = ata_host_suspend(host, mesg);
6334c6fd2807SJeff Garzik 	if (rc)
6335c6fd2807SJeff Garzik 		return rc;
6336c6fd2807SJeff Garzik 
6337c6fd2807SJeff Garzik 	ata_pci_device_do_suspend(pdev, mesg);
6338c6fd2807SJeff Garzik 
6339c6fd2807SJeff Garzik 	return 0;
6340c6fd2807SJeff Garzik }
6341c6fd2807SJeff Garzik 
6342c6fd2807SJeff Garzik int ata_pci_device_resume(struct pci_dev *pdev)
6343c6fd2807SJeff Garzik {
6344cca3974eSJeff Garzik 	struct ata_host *host = dev_get_drvdata(&pdev->dev);
6345553c4aa6STejun Heo 	int rc;
6346c6fd2807SJeff Garzik 
6347553c4aa6STejun Heo 	rc = ata_pci_device_do_resume(pdev);
6348553c4aa6STejun Heo 	if (rc == 0)
6349cca3974eSJeff Garzik 		ata_host_resume(host);
6350553c4aa6STejun Heo 	return rc;
6351c6fd2807SJeff Garzik }
63526ffa01d8STejun Heo #endif /* CONFIG_PM */
63536ffa01d8STejun Heo 
6354c6fd2807SJeff Garzik #endif /* CONFIG_PCI */
6355c6fd2807SJeff Garzik 
635633267325STejun Heo static int __init ata_parse_force_one(char **cur,
635733267325STejun Heo 				      struct ata_force_ent *force_ent,
635833267325STejun Heo 				      const char **reason)
635933267325STejun Heo {
636033267325STejun Heo 	/* FIXME: Currently, there's no way to tag init const data and
636133267325STejun Heo 	 * using __initdata causes build failure on some versions of
636233267325STejun Heo 	 * gcc.  Once __initdataconst is implemented, add const to the
636333267325STejun Heo 	 * following structure.
636433267325STejun Heo 	 */
636533267325STejun Heo 	static struct ata_force_param force_tbl[] __initdata = {
636633267325STejun Heo 		{ "40c",	.cbl		= ATA_CBL_PATA40 },
636733267325STejun Heo 		{ "80c",	.cbl		= ATA_CBL_PATA80 },
636833267325STejun Heo 		{ "short40c",	.cbl		= ATA_CBL_PATA40_SHORT },
636933267325STejun Heo 		{ "unk",	.cbl		= ATA_CBL_PATA_UNK },
637033267325STejun Heo 		{ "ign",	.cbl		= ATA_CBL_PATA_IGN },
637133267325STejun Heo 		{ "sata",	.cbl		= ATA_CBL_SATA },
637233267325STejun Heo 		{ "1.5Gbps",	.spd_limit	= 1 },
637333267325STejun Heo 		{ "3.0Gbps",	.spd_limit	= 2 },
637433267325STejun Heo 		{ "noncq",	.horkage_on	= ATA_HORKAGE_NONCQ },
637533267325STejun Heo 		{ "ncq",	.horkage_off	= ATA_HORKAGE_NONCQ },
637643c9c591STejun Heo 		{ "dump_id",	.horkage_on	= ATA_HORKAGE_DUMP_ID },
637733267325STejun Heo 		{ "pio0",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 0) },
637833267325STejun Heo 		{ "pio1",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 1) },
637933267325STejun Heo 		{ "pio2",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 2) },
638033267325STejun Heo 		{ "pio3",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 3) },
638133267325STejun Heo 		{ "pio4",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 4) },
638233267325STejun Heo 		{ "pio5",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 5) },
638333267325STejun Heo 		{ "pio6",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 6) },
638433267325STejun Heo 		{ "mwdma0",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 0) },
638533267325STejun Heo 		{ "mwdma1",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 1) },
638633267325STejun Heo 		{ "mwdma2",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 2) },
638733267325STejun Heo 		{ "mwdma3",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 3) },
638833267325STejun Heo 		{ "mwdma4",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 4) },
638933267325STejun Heo 		{ "udma0",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
639033267325STejun Heo 		{ "udma16",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
639133267325STejun Heo 		{ "udma/16",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
639233267325STejun Heo 		{ "udma1",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
639333267325STejun Heo 		{ "udma25",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
639433267325STejun Heo 		{ "udma/25",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
639533267325STejun Heo 		{ "udma2",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
639633267325STejun Heo 		{ "udma33",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
639733267325STejun Heo 		{ "udma/33",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
639833267325STejun Heo 		{ "udma3",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
639933267325STejun Heo 		{ "udma44",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
640033267325STejun Heo 		{ "udma/44",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
640133267325STejun Heo 		{ "udma4",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
640233267325STejun Heo 		{ "udma66",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
640333267325STejun Heo 		{ "udma/66",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
640433267325STejun Heo 		{ "udma5",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
640533267325STejun Heo 		{ "udma100",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
640633267325STejun Heo 		{ "udma/100",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
640733267325STejun Heo 		{ "udma6",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
640833267325STejun Heo 		{ "udma133",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
640933267325STejun Heo 		{ "udma/133",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
641033267325STejun Heo 		{ "udma7",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 7) },
641105944bdfSTejun Heo 		{ "nohrst",	.lflags		= ATA_LFLAG_NO_HRST },
641205944bdfSTejun Heo 		{ "nosrst",	.lflags		= ATA_LFLAG_NO_SRST },
641305944bdfSTejun Heo 		{ "norst",	.lflags		= ATA_LFLAG_NO_HRST | ATA_LFLAG_NO_SRST },
641433267325STejun Heo 	};
641533267325STejun Heo 	char *start = *cur, *p = *cur;
641633267325STejun Heo 	char *id, *val, *endp;
641733267325STejun Heo 	const struct ata_force_param *match_fp = NULL;
641833267325STejun Heo 	int nr_matches = 0, i;
641933267325STejun Heo 
642033267325STejun Heo 	/* find where this param ends and update *cur */
642133267325STejun Heo 	while (*p != '\0' && *p != ',')
642233267325STejun Heo 		p++;
642333267325STejun Heo 
642433267325STejun Heo 	if (*p == '\0')
642533267325STejun Heo 		*cur = p;
642633267325STejun Heo 	else
642733267325STejun Heo 		*cur = p + 1;
642833267325STejun Heo 
642933267325STejun Heo 	*p = '\0';
643033267325STejun Heo 
643133267325STejun Heo 	/* parse */
643233267325STejun Heo 	p = strchr(start, ':');
643333267325STejun Heo 	if (!p) {
643433267325STejun Heo 		val = strstrip(start);
643533267325STejun Heo 		goto parse_val;
643633267325STejun Heo 	}
643733267325STejun Heo 	*p = '\0';
643833267325STejun Heo 
643933267325STejun Heo 	id = strstrip(start);
644033267325STejun Heo 	val = strstrip(p + 1);
644133267325STejun Heo 
644233267325STejun Heo 	/* parse id */
644333267325STejun Heo 	p = strchr(id, '.');
644433267325STejun Heo 	if (p) {
644533267325STejun Heo 		*p++ = '\0';
644633267325STejun Heo 		force_ent->device = simple_strtoul(p, &endp, 10);
644733267325STejun Heo 		if (p == endp || *endp != '\0') {
644833267325STejun Heo 			*reason = "invalid device";
644933267325STejun Heo 			return -EINVAL;
645033267325STejun Heo 		}
645133267325STejun Heo 	}
645233267325STejun Heo 
645333267325STejun Heo 	force_ent->port = simple_strtoul(id, &endp, 10);
645433267325STejun Heo 	if (p == endp || *endp != '\0') {
645533267325STejun Heo 		*reason = "invalid port/link";
645633267325STejun Heo 		return -EINVAL;
645733267325STejun Heo 	}
645833267325STejun Heo 
645933267325STejun Heo  parse_val:
646033267325STejun Heo 	/* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */
646133267325STejun Heo 	for (i = 0; i < ARRAY_SIZE(force_tbl); i++) {
646233267325STejun Heo 		const struct ata_force_param *fp = &force_tbl[i];
646333267325STejun Heo 
646433267325STejun Heo 		if (strncasecmp(val, fp->name, strlen(val)))
646533267325STejun Heo 			continue;
646633267325STejun Heo 
646733267325STejun Heo 		nr_matches++;
646833267325STejun Heo 		match_fp = fp;
646933267325STejun Heo 
647033267325STejun Heo 		if (strcasecmp(val, fp->name) == 0) {
647133267325STejun Heo 			nr_matches = 1;
647233267325STejun Heo 			break;
647333267325STejun Heo 		}
647433267325STejun Heo 	}
647533267325STejun Heo 
647633267325STejun Heo 	if (!nr_matches) {
647733267325STejun Heo 		*reason = "unknown value";
647833267325STejun Heo 		return -EINVAL;
647933267325STejun Heo 	}
648033267325STejun Heo 	if (nr_matches > 1) {
648133267325STejun Heo 		*reason = "ambigious value";
648233267325STejun Heo 		return -EINVAL;
648333267325STejun Heo 	}
648433267325STejun Heo 
648533267325STejun Heo 	force_ent->param = *match_fp;
648633267325STejun Heo 
648733267325STejun Heo 	return 0;
648833267325STejun Heo }
648933267325STejun Heo 
649033267325STejun Heo static void __init ata_parse_force_param(void)
649133267325STejun Heo {
649233267325STejun Heo 	int idx = 0, size = 1;
649333267325STejun Heo 	int last_port = -1, last_device = -1;
649433267325STejun Heo 	char *p, *cur, *next;
649533267325STejun Heo 
649633267325STejun Heo 	/* calculate maximum number of params and allocate force_tbl */
649733267325STejun Heo 	for (p = ata_force_param_buf; *p; p++)
649833267325STejun Heo 		if (*p == ',')
649933267325STejun Heo 			size++;
650033267325STejun Heo 
650133267325STejun Heo 	ata_force_tbl = kzalloc(sizeof(ata_force_tbl[0]) * size, GFP_KERNEL);
650233267325STejun Heo 	if (!ata_force_tbl) {
650333267325STejun Heo 		printk(KERN_WARNING "ata: failed to extend force table, "
650433267325STejun Heo 		       "libata.force ignored\n");
650533267325STejun Heo 		return;
650633267325STejun Heo 	}
650733267325STejun Heo 
650833267325STejun Heo 	/* parse and populate the table */
650933267325STejun Heo 	for (cur = ata_force_param_buf; *cur != '\0'; cur = next) {
651033267325STejun Heo 		const char *reason = "";
651133267325STejun Heo 		struct ata_force_ent te = { .port = -1, .device = -1 };
651233267325STejun Heo 
651333267325STejun Heo 		next = cur;
651433267325STejun Heo 		if (ata_parse_force_one(&next, &te, &reason)) {
651533267325STejun Heo 			printk(KERN_WARNING "ata: failed to parse force "
651633267325STejun Heo 			       "parameter \"%s\" (%s)\n",
651733267325STejun Heo 			       cur, reason);
651833267325STejun Heo 			continue;
651933267325STejun Heo 		}
652033267325STejun Heo 
652133267325STejun Heo 		if (te.port == -1) {
652233267325STejun Heo 			te.port = last_port;
652333267325STejun Heo 			te.device = last_device;
652433267325STejun Heo 		}
652533267325STejun Heo 
652633267325STejun Heo 		ata_force_tbl[idx++] = te;
652733267325STejun Heo 
652833267325STejun Heo 		last_port = te.port;
652933267325STejun Heo 		last_device = te.device;
653033267325STejun Heo 	}
653133267325STejun Heo 
653233267325STejun Heo 	ata_force_tbl_size = idx;
653333267325STejun Heo }
6534c6fd2807SJeff Garzik 
6535c6fd2807SJeff Garzik static int __init ata_init(void)
6536c6fd2807SJeff Garzik {
6537270390e1STejun Heo 	int rc = -ENOMEM;
6538270390e1STejun Heo 
653933267325STejun Heo 	ata_parse_force_param();
654033267325STejun Heo 
6541c6fd2807SJeff Garzik 	ata_aux_wq = create_singlethread_workqueue("ata_aux");
654249ea3b04SElias Oltmanns 	if (!ata_aux_wq)
6543270390e1STejun Heo 		goto fail;
6544270390e1STejun Heo 
6545270390e1STejun Heo 	rc = ata_sff_init();
6546270390e1STejun Heo 	if (rc)
6547270390e1STejun Heo 		goto fail;
6548c6fd2807SJeff Garzik 
6549c6fd2807SJeff Garzik 	printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
6550c6fd2807SJeff Garzik 	return 0;
655149ea3b04SElias Oltmanns 
6552270390e1STejun Heo fail:
655349ea3b04SElias Oltmanns 	kfree(ata_force_tbl);
6554270390e1STejun Heo 	if (ata_aux_wq)
6555270390e1STejun Heo 		destroy_workqueue(ata_aux_wq);
6556270390e1STejun Heo 	return rc;
6557c6fd2807SJeff Garzik }
6558c6fd2807SJeff Garzik 
6559c6fd2807SJeff Garzik static void __exit ata_exit(void)
6560c6fd2807SJeff Garzik {
6561270390e1STejun Heo 	ata_sff_exit();
656233267325STejun Heo 	kfree(ata_force_tbl);
6563c6fd2807SJeff Garzik 	destroy_workqueue(ata_aux_wq);
6564c6fd2807SJeff Garzik }
6565c6fd2807SJeff Garzik 
6566a4625085SBrian King subsys_initcall(ata_init);
6567c6fd2807SJeff Garzik module_exit(ata_exit);
6568c6fd2807SJeff Garzik 
65699990b6f3SAkinobu Mita static DEFINE_RATELIMIT_STATE(ratelimit, HZ / 5, 1);
6570c6fd2807SJeff Garzik 
6571c6fd2807SJeff Garzik int ata_ratelimit(void)
6572c6fd2807SJeff Garzik {
65739990b6f3SAkinobu Mita 	return __ratelimit(&ratelimit);
6574c6fd2807SJeff Garzik }
6575c6fd2807SJeff Garzik 
6576c6fd2807SJeff Garzik /**
6577c6fd2807SJeff Garzik  *	ata_wait_register - wait until register value changes
6578c6fd2807SJeff Garzik  *	@reg: IO-mapped register
6579c6fd2807SJeff Garzik  *	@mask: Mask to apply to read register value
6580c6fd2807SJeff Garzik  *	@val: Wait condition
6581341c2c95STejun Heo  *	@interval: polling interval in milliseconds
6582341c2c95STejun Heo  *	@timeout: timeout in milliseconds
6583c6fd2807SJeff Garzik  *
6584c6fd2807SJeff Garzik  *	Waiting for some bits of register to change is a common
6585c6fd2807SJeff Garzik  *	operation for ATA controllers.  This function reads 32bit LE
6586c6fd2807SJeff Garzik  *	IO-mapped register @reg and tests for the following condition.
6587c6fd2807SJeff Garzik  *
6588c6fd2807SJeff Garzik  *	(*@reg & mask) != val
6589c6fd2807SJeff Garzik  *
6590c6fd2807SJeff Garzik  *	If the condition is met, it returns; otherwise, the process is
6591c6fd2807SJeff Garzik  *	repeated after @interval_msec until timeout.
6592c6fd2807SJeff Garzik  *
6593c6fd2807SJeff Garzik  *	LOCKING:
6594c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
6595c6fd2807SJeff Garzik  *
6596c6fd2807SJeff Garzik  *	RETURNS:
6597c6fd2807SJeff Garzik  *	The final register value.
6598c6fd2807SJeff Garzik  */
6599c6fd2807SJeff Garzik u32 ata_wait_register(void __iomem *reg, u32 mask, u32 val,
6600341c2c95STejun Heo 		      unsigned long interval, unsigned long timeout)
6601c6fd2807SJeff Garzik {
6602341c2c95STejun Heo 	unsigned long deadline;
6603c6fd2807SJeff Garzik 	u32 tmp;
6604c6fd2807SJeff Garzik 
6605c6fd2807SJeff Garzik 	tmp = ioread32(reg);
6606c6fd2807SJeff Garzik 
6607c6fd2807SJeff Garzik 	/* Calculate timeout _after_ the first read to make sure
6608c6fd2807SJeff Garzik 	 * preceding writes reach the controller before starting to
6609c6fd2807SJeff Garzik 	 * eat away the timeout.
6610c6fd2807SJeff Garzik 	 */
6611341c2c95STejun Heo 	deadline = ata_deadline(jiffies, timeout);
6612c6fd2807SJeff Garzik 
6613341c2c95STejun Heo 	while ((tmp & mask) == val && time_before(jiffies, deadline)) {
6614341c2c95STejun Heo 		msleep(interval);
6615c6fd2807SJeff Garzik 		tmp = ioread32(reg);
6616c6fd2807SJeff Garzik 	}
6617c6fd2807SJeff Garzik 
6618c6fd2807SJeff Garzik 	return tmp;
6619c6fd2807SJeff Garzik }
6620c6fd2807SJeff Garzik 
6621c6fd2807SJeff Garzik /*
6622c6fd2807SJeff Garzik  * Dummy port_ops
6623c6fd2807SJeff Garzik  */
6624c6fd2807SJeff Garzik static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
6625c6fd2807SJeff Garzik {
6626c6fd2807SJeff Garzik 	return AC_ERR_SYSTEM;
6627c6fd2807SJeff Garzik }
6628c6fd2807SJeff Garzik 
6629182d7bbaSTejun Heo static void ata_dummy_error_handler(struct ata_port *ap)
6630182d7bbaSTejun Heo {
6631182d7bbaSTejun Heo 	/* truly dummy */
6632182d7bbaSTejun Heo }
6633182d7bbaSTejun Heo 
6634029cfd6bSTejun Heo struct ata_port_operations ata_dummy_port_ops = {
6635c6fd2807SJeff Garzik 	.qc_prep		= ata_noop_qc_prep,
6636c6fd2807SJeff Garzik 	.qc_issue		= ata_dummy_qc_issue,
6637182d7bbaSTejun Heo 	.error_handler		= ata_dummy_error_handler,
6638c6fd2807SJeff Garzik };
6639c6fd2807SJeff Garzik 
664021b0ad4fSTejun Heo const struct ata_port_info ata_dummy_port_info = {
664121b0ad4fSTejun Heo 	.port_ops		= &ata_dummy_port_ops,
664221b0ad4fSTejun Heo };
664321b0ad4fSTejun Heo 
6644c6fd2807SJeff Garzik /*
6645c6fd2807SJeff Garzik  * libata is essentially a library of internal helper functions for
6646c6fd2807SJeff Garzik  * low-level ATA host controller drivers.  As such, the API/ABI is
6647c6fd2807SJeff Garzik  * likely to change as new drivers are added and updated.
6648c6fd2807SJeff Garzik  * Do not depend on ABI/API stability.
6649c6fd2807SJeff Garzik  */
6650c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
6651c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
6652c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_deb_timing_long);
6653029cfd6bSTejun Heo EXPORT_SYMBOL_GPL(ata_base_port_ops);
6654029cfd6bSTejun Heo EXPORT_SYMBOL_GPL(sata_port_ops);
6655c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
665621b0ad4fSTejun Heo EXPORT_SYMBOL_GPL(ata_dummy_port_info);
66571eca4365STejun Heo EXPORT_SYMBOL_GPL(ata_link_next);
66581eca4365STejun Heo EXPORT_SYMBOL_GPL(ata_dev_next);
6659c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_std_bios_param);
6660d8d9129eSTejun Heo EXPORT_SYMBOL_GPL(ata_scsi_unlock_native_capacity);
6661cca3974eSJeff Garzik EXPORT_SYMBOL_GPL(ata_host_init);
6662f3187195STejun Heo EXPORT_SYMBOL_GPL(ata_host_alloc);
6663f5cda257STejun Heo EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
6664b1c72916STejun Heo EXPORT_SYMBOL_GPL(ata_slave_link_init);
6665ecef7253STejun Heo EXPORT_SYMBOL_GPL(ata_host_start);
6666f3187195STejun Heo EXPORT_SYMBOL_GPL(ata_host_register);
6667f5cda257STejun Heo EXPORT_SYMBOL_GPL(ata_host_activate);
66680529c159STejun Heo EXPORT_SYMBOL_GPL(ata_host_detach);
6669c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_sg_init);
6670c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_qc_complete);
6671c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
6672436d34b3STejun Heo EXPORT_SYMBOL_GPL(atapi_cmd_type);
6673c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_tf_to_fis);
6674c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_tf_from_fis);
66756357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_pack_xfermask);
66766357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_unpack_xfermask);
66776357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
66786357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
66796357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
66806357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_mode_string);
66816357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_id_xfermask);
668204351821SAlan EXPORT_SYMBOL_GPL(ata_do_set_mode);
668331cc23b3STejun Heo EXPORT_SYMBOL_GPL(ata_std_qc_defer);
6684c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
668510305f0fSAlan EXPORT_SYMBOL_GPL(ata_dev_disable);
6686c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_set_spd);
6687aa2731adSTejun Heo EXPORT_SYMBOL_GPL(ata_wait_after_reset);
6688936fd732STejun Heo EXPORT_SYMBOL_GPL(sata_link_debounce);
6689936fd732STejun Heo EXPORT_SYMBOL_GPL(sata_link_resume);
66900aa1113dSTejun Heo EXPORT_SYMBOL_GPL(ata_std_prereset);
6691cc0680a5STejun Heo EXPORT_SYMBOL_GPL(sata_link_hardreset);
669257c9efdfSTejun Heo EXPORT_SYMBOL_GPL(sata_std_hardreset);
6693203c75b8STejun Heo EXPORT_SYMBOL_GPL(ata_std_postreset);
6694c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_dev_classify);
6695c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_dev_pair);
6696c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_ratelimit);
6697c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_wait_register);
6698c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
6699c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
6700c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
6701c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
6702c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_valid);
6703c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_read);
6704c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_write);
6705c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_write_flush);
6706936fd732STejun Heo EXPORT_SYMBOL_GPL(ata_link_online);
6707936fd732STejun Heo EXPORT_SYMBOL_GPL(ata_link_offline);
67086ffa01d8STejun Heo #ifdef CONFIG_PM
6709cca3974eSJeff Garzik EXPORT_SYMBOL_GPL(ata_host_suspend);
6710cca3974eSJeff Garzik EXPORT_SYMBOL_GPL(ata_host_resume);
67116ffa01d8STejun Heo #endif /* CONFIG_PM */
6712c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_id_string);
6713c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_id_c_string);
6714963e4975SAlan Cox EXPORT_SYMBOL_GPL(ata_do_dev_read_id);
6715c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_simulate);
6716c6fd2807SJeff Garzik 
6717c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
67186357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_timing_find_mode);
6719c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_timing_compute);
6720c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_timing_merge);
6721a0f79b92STejun Heo EXPORT_SYMBOL_GPL(ata_timing_cycle2mode);
6722c6fd2807SJeff Garzik 
6723c6fd2807SJeff Garzik #ifdef CONFIG_PCI
6724c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(pci_test_config_bits);
6725c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_remove_one);
67266ffa01d8STejun Heo #ifdef CONFIG_PM
6727c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
6728c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
6729c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
6730c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_resume);
67316ffa01d8STejun Heo #endif /* CONFIG_PM */
6732c6fd2807SJeff Garzik #endif /* CONFIG_PCI */
6733c6fd2807SJeff Garzik 
6734b64bbc39STejun Heo EXPORT_SYMBOL_GPL(__ata_ehi_push_desc);
6735b64bbc39STejun Heo EXPORT_SYMBOL_GPL(ata_ehi_push_desc);
6736b64bbc39STejun Heo EXPORT_SYMBOL_GPL(ata_ehi_clear_desc);
6737cbcdd875STejun Heo EXPORT_SYMBOL_GPL(ata_port_desc);
6738cbcdd875STejun Heo #ifdef CONFIG_PCI
6739cbcdd875STejun Heo EXPORT_SYMBOL_GPL(ata_port_pbar_desc);
6740cbcdd875STejun Heo #endif /* CONFIG_PCI */
6741c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
6742dbd82616STejun Heo EXPORT_SYMBOL_GPL(ata_link_abort);
6743c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_abort);
6744c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_freeze);
67457d77b247STejun Heo EXPORT_SYMBOL_GPL(sata_async_notification);
6746c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
6747c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
6748c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
6749c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
675010acf3b0SMark Lord EXPORT_SYMBOL_GPL(ata_eh_analyze_ncq_error);
6751c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_do_eh);
6752a1efdabaSTejun Heo EXPORT_SYMBOL_GPL(ata_std_error_handler);
6753be0d18dfSAlan Cox 
6754be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_40wire);
6755be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_80wire);
6756be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_unknown);
6757c88f90c3STejun Heo EXPORT_SYMBOL_GPL(ata_cable_ignore);
6758be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_sata);
6759