xref: /openbmc/linux/drivers/ata/libata-core.c (revision a6f9bf4d)
1c6fd2807SJeff Garzik /*
2c6fd2807SJeff Garzik  *  libata-core.c - helper library for ATA
3c6fd2807SJeff Garzik  *
48c3d3d4bSTejun Heo  *  Maintained by:  Tejun Heo <tj@kernel.org>
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>
699ee4f393SLin Ming #include <linux/pm_runtime.h>
70b7db04d9SBrian Norris #include <linux/platform_device.h>
71c6fd2807SJeff Garzik 
72c6fd2807SJeff Garzik #include "libata.h"
73d9027470SGwendal Grignou #include "libata-transport.h"
74fda0efc5SJeff Garzik 
75c6fd2807SJeff Garzik /* debounce timing parameters in msecs { interval, duration, timeout } */
76c6fd2807SJeff Garzik const unsigned long sata_deb_timing_normal[]		= {   5,  100, 2000 };
77c6fd2807SJeff Garzik const unsigned long sata_deb_timing_hotplug[]		= {  25,  500, 2000 };
78c6fd2807SJeff Garzik const unsigned long sata_deb_timing_long[]		= { 100, 2000, 5000 };
79c6fd2807SJeff Garzik 
80029cfd6bSTejun Heo const struct ata_port_operations ata_base_port_ops = {
810aa1113dSTejun Heo 	.prereset		= ata_std_prereset,
82203c75b8STejun Heo 	.postreset		= ata_std_postreset,
83a1efdabaSTejun Heo 	.error_handler		= ata_std_error_handler,
84e4a9c373SDan Williams 	.sched_eh		= ata_std_sched_eh,
85e4a9c373SDan Williams 	.end_eh			= ata_std_end_eh,
86029cfd6bSTejun Heo };
87029cfd6bSTejun Heo 
88029cfd6bSTejun Heo const struct ata_port_operations sata_port_ops = {
89029cfd6bSTejun Heo 	.inherits		= &ata_base_port_ops,
90029cfd6bSTejun Heo 
91029cfd6bSTejun Heo 	.qc_defer		= ata_std_qc_defer,
9257c9efdfSTejun Heo 	.hardreset		= sata_std_hardreset,
93029cfd6bSTejun Heo };
94029cfd6bSTejun Heo 
95c6fd2807SJeff Garzik static unsigned int ata_dev_init_params(struct ata_device *dev,
96c6fd2807SJeff Garzik 					u16 heads, u16 sectors);
97c6fd2807SJeff Garzik static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
98c6fd2807SJeff Garzik static void ata_dev_xfermask(struct ata_device *dev);
9975683fe7STejun Heo static unsigned long ata_dev_blacklisted(const struct ata_device *dev);
100c6fd2807SJeff Garzik 
101a78f57afSTero Roponen atomic_t ata_print_id = ATOMIC_INIT(0);
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;
342a9a79dfeSJoe Perches 		ata_port_notice(ap, "FORCE: cable set to %s\n", fe->param.name);
34333267325STejun Heo 		return;
34433267325STejun Heo 	}
34533267325STejun Heo }
34633267325STejun Heo 
34733267325STejun Heo /**
34805944bdfSTejun Heo  *	ata_force_link_limits - force link limits according to libata.force
34933267325STejun Heo  *	@link: ATA link of interest
35033267325STejun Heo  *
35105944bdfSTejun Heo  *	Force link flags and SATA spd limit according to libata.force
35205944bdfSTejun Heo  *	and whine about it.  When only the port part is specified
35305944bdfSTejun Heo  *	(e.g. 1:), the limit applies to all links connected to both
35405944bdfSTejun Heo  *	the host link and all fan-out ports connected via PMP.  If the
35505944bdfSTejun Heo  *	device part is specified as 0 (e.g. 1.00:), it specifies the
35605944bdfSTejun Heo  *	first fan-out link not the host link.  Device number 15 always
357b1c72916STejun Heo  *	points to the host link whether PMP is attached or not.  If the
358b1c72916STejun Heo  *	controller has slave link, device number 16 points to it.
35933267325STejun Heo  *
36033267325STejun Heo  *	LOCKING:
36133267325STejun Heo  *	EH context.
36233267325STejun Heo  */
36305944bdfSTejun Heo static void ata_force_link_limits(struct ata_link *link)
36433267325STejun Heo {
36505944bdfSTejun Heo 	bool did_spd = false;
366b1c72916STejun Heo 	int linkno = link->pmp;
367b1c72916STejun Heo 	int i;
36833267325STejun Heo 
36933267325STejun Heo 	if (ata_is_host_link(link))
370b1c72916STejun Heo 		linkno += 15;
37133267325STejun Heo 
37233267325STejun Heo 	for (i = ata_force_tbl_size - 1; i >= 0; i--) {
37333267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
37433267325STejun Heo 
37533267325STejun Heo 		if (fe->port != -1 && fe->port != link->ap->print_id)
37633267325STejun Heo 			continue;
37733267325STejun Heo 
37833267325STejun Heo 		if (fe->device != -1 && fe->device != linkno)
37933267325STejun Heo 			continue;
38033267325STejun Heo 
38105944bdfSTejun Heo 		/* only honor the first spd limit */
38205944bdfSTejun Heo 		if (!did_spd && fe->param.spd_limit) {
38333267325STejun Heo 			link->hw_sata_spd_limit = (1 << fe->param.spd_limit) - 1;
384a9a79dfeSJoe Perches 			ata_link_notice(link, "FORCE: PHY spd limit set to %s\n",
38505944bdfSTejun Heo 					fe->param.name);
38605944bdfSTejun Heo 			did_spd = true;
38705944bdfSTejun Heo 		}
38805944bdfSTejun Heo 
38905944bdfSTejun Heo 		/* let lflags stack */
39005944bdfSTejun Heo 		if (fe->param.lflags) {
39105944bdfSTejun Heo 			link->flags |= fe->param.lflags;
392a9a79dfeSJoe Perches 			ata_link_notice(link,
39305944bdfSTejun Heo 					"FORCE: link flag 0x%x forced -> 0x%x\n",
39405944bdfSTejun Heo 					fe->param.lflags, link->flags);
39505944bdfSTejun Heo 		}
39633267325STejun Heo 	}
39733267325STejun Heo }
39833267325STejun Heo 
39933267325STejun Heo /**
40033267325STejun Heo  *	ata_force_xfermask - force xfermask according to libata.force
40133267325STejun Heo  *	@dev: ATA device of interest
40233267325STejun Heo  *
40333267325STejun Heo  *	Force xfer_mask according to libata.force and whine about it.
40433267325STejun Heo  *	For consistency with link selection, device number 15 selects
40533267325STejun Heo  *	the first device connected to the host link.
40633267325STejun Heo  *
40733267325STejun Heo  *	LOCKING:
40833267325STejun Heo  *	EH context.
40933267325STejun Heo  */
41033267325STejun Heo static void ata_force_xfermask(struct ata_device *dev)
41133267325STejun Heo {
41233267325STejun Heo 	int devno = dev->link->pmp + dev->devno;
41333267325STejun Heo 	int alt_devno = devno;
41433267325STejun Heo 	int i;
41533267325STejun Heo 
416b1c72916STejun Heo 	/* allow n.15/16 for devices attached to host port */
417b1c72916STejun Heo 	if (ata_is_host_link(dev->link))
418b1c72916STejun Heo 		alt_devno += 15;
41933267325STejun Heo 
42033267325STejun Heo 	for (i = ata_force_tbl_size - 1; i >= 0; i--) {
42133267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
42233267325STejun Heo 		unsigned long pio_mask, mwdma_mask, udma_mask;
42333267325STejun Heo 
42433267325STejun Heo 		if (fe->port != -1 && fe->port != dev->link->ap->print_id)
42533267325STejun Heo 			continue;
42633267325STejun Heo 
42733267325STejun Heo 		if (fe->device != -1 && fe->device != devno &&
42833267325STejun Heo 		    fe->device != alt_devno)
42933267325STejun Heo 			continue;
43033267325STejun Heo 
43133267325STejun Heo 		if (!fe->param.xfer_mask)
43233267325STejun Heo 			continue;
43333267325STejun Heo 
43433267325STejun Heo 		ata_unpack_xfermask(fe->param.xfer_mask,
43533267325STejun Heo 				    &pio_mask, &mwdma_mask, &udma_mask);
43633267325STejun Heo 		if (udma_mask)
43733267325STejun Heo 			dev->udma_mask = udma_mask;
43833267325STejun Heo 		else if (mwdma_mask) {
43933267325STejun Heo 			dev->udma_mask = 0;
44033267325STejun Heo 			dev->mwdma_mask = mwdma_mask;
44133267325STejun Heo 		} else {
44233267325STejun Heo 			dev->udma_mask = 0;
44333267325STejun Heo 			dev->mwdma_mask = 0;
44433267325STejun Heo 			dev->pio_mask = pio_mask;
44533267325STejun Heo 		}
44633267325STejun Heo 
447a9a79dfeSJoe Perches 		ata_dev_notice(dev, "FORCE: xfer_mask set to %s\n",
448a9a79dfeSJoe Perches 			       fe->param.name);
44933267325STejun Heo 		return;
45033267325STejun Heo 	}
45133267325STejun Heo }
45233267325STejun Heo 
45333267325STejun Heo /**
45433267325STejun Heo  *	ata_force_horkage - force horkage according to libata.force
45533267325STejun Heo  *	@dev: ATA device of interest
45633267325STejun Heo  *
45733267325STejun Heo  *	Force horkage according to libata.force and whine about it.
45833267325STejun Heo  *	For consistency with link selection, device number 15 selects
45933267325STejun Heo  *	the first device connected to the host link.
46033267325STejun Heo  *
46133267325STejun Heo  *	LOCKING:
46233267325STejun Heo  *	EH context.
46333267325STejun Heo  */
46433267325STejun Heo static void ata_force_horkage(struct ata_device *dev)
46533267325STejun Heo {
46633267325STejun Heo 	int devno = dev->link->pmp + dev->devno;
46733267325STejun Heo 	int alt_devno = devno;
46833267325STejun Heo 	int i;
46933267325STejun Heo 
470b1c72916STejun Heo 	/* allow n.15/16 for devices attached to host port */
471b1c72916STejun Heo 	if (ata_is_host_link(dev->link))
472b1c72916STejun Heo 		alt_devno += 15;
47333267325STejun Heo 
47433267325STejun Heo 	for (i = 0; i < ata_force_tbl_size; i++) {
47533267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
47633267325STejun Heo 
47733267325STejun Heo 		if (fe->port != -1 && fe->port != dev->link->ap->print_id)
47833267325STejun Heo 			continue;
47933267325STejun Heo 
48033267325STejun Heo 		if (fe->device != -1 && fe->device != devno &&
48133267325STejun Heo 		    fe->device != alt_devno)
48233267325STejun Heo 			continue;
48333267325STejun Heo 
48433267325STejun Heo 		if (!(~dev->horkage & fe->param.horkage_on) &&
48533267325STejun Heo 		    !(dev->horkage & fe->param.horkage_off))
48633267325STejun Heo 			continue;
48733267325STejun Heo 
48833267325STejun Heo 		dev->horkage |= fe->param.horkage_on;
48933267325STejun Heo 		dev->horkage &= ~fe->param.horkage_off;
49033267325STejun Heo 
491a9a79dfeSJoe Perches 		ata_dev_notice(dev, "FORCE: horkage modified (%s)\n",
492a9a79dfeSJoe Perches 			       fe->param.name);
49333267325STejun Heo 	}
49433267325STejun Heo }
49533267325STejun Heo 
49633267325STejun Heo /**
497436d34b3STejun Heo  *	atapi_cmd_type - Determine ATAPI command type from SCSI opcode
498436d34b3STejun Heo  *	@opcode: SCSI opcode
499436d34b3STejun Heo  *
500436d34b3STejun Heo  *	Determine ATAPI command type from @opcode.
501436d34b3STejun Heo  *
502436d34b3STejun Heo  *	LOCKING:
503436d34b3STejun Heo  *	None.
504436d34b3STejun Heo  *
505436d34b3STejun Heo  *	RETURNS:
506436d34b3STejun Heo  *	ATAPI_{READ|WRITE|READ_CD|PASS_THRU|MISC}
507436d34b3STejun Heo  */
508436d34b3STejun Heo int atapi_cmd_type(u8 opcode)
509436d34b3STejun Heo {
510436d34b3STejun Heo 	switch (opcode) {
511436d34b3STejun Heo 	case GPCMD_READ_10:
512436d34b3STejun Heo 	case GPCMD_READ_12:
513436d34b3STejun Heo 		return ATAPI_READ;
514436d34b3STejun Heo 
515436d34b3STejun Heo 	case GPCMD_WRITE_10:
516436d34b3STejun Heo 	case GPCMD_WRITE_12:
517436d34b3STejun Heo 	case GPCMD_WRITE_AND_VERIFY_10:
518436d34b3STejun Heo 		return ATAPI_WRITE;
519436d34b3STejun Heo 
520436d34b3STejun Heo 	case GPCMD_READ_CD:
521436d34b3STejun Heo 	case GPCMD_READ_CD_MSF:
522436d34b3STejun Heo 		return ATAPI_READ_CD;
523436d34b3STejun Heo 
524e52dcc48STejun Heo 	case ATA_16:
525e52dcc48STejun Heo 	case ATA_12:
526e52dcc48STejun Heo 		if (atapi_passthru16)
527e52dcc48STejun Heo 			return ATAPI_PASS_THRU;
528e52dcc48STejun Heo 		/* fall thru */
529436d34b3STejun Heo 	default:
530436d34b3STejun Heo 		return ATAPI_MISC;
531436d34b3STejun Heo 	}
532436d34b3STejun Heo }
533436d34b3STejun Heo 
534436d34b3STejun Heo /**
535c6fd2807SJeff Garzik  *	ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
536c6fd2807SJeff Garzik  *	@tf: Taskfile to convert
537c6fd2807SJeff Garzik  *	@pmp: Port multiplier port
5389977126cSTejun Heo  *	@is_cmd: This FIS is for command
5399977126cSTejun Heo  *	@fis: Buffer into which data will output
540c6fd2807SJeff Garzik  *
541c6fd2807SJeff Garzik  *	Converts a standard ATA taskfile to a Serial ATA
542c6fd2807SJeff Garzik  *	FIS structure (Register - Host to Device).
543c6fd2807SJeff Garzik  *
544c6fd2807SJeff Garzik  *	LOCKING:
545c6fd2807SJeff Garzik  *	Inherited from caller.
546c6fd2807SJeff Garzik  */
5479977126cSTejun Heo void ata_tf_to_fis(const struct ata_taskfile *tf, u8 pmp, int is_cmd, u8 *fis)
548c6fd2807SJeff Garzik {
549c6fd2807SJeff Garzik 	fis[0] = 0x27;			/* Register - Host to Device FIS */
5509977126cSTejun Heo 	fis[1] = pmp & 0xf;		/* Port multiplier number*/
5519977126cSTejun Heo 	if (is_cmd)
5529977126cSTejun Heo 		fis[1] |= (1 << 7);	/* bit 7 indicates Command FIS */
5539977126cSTejun Heo 
554c6fd2807SJeff Garzik 	fis[2] = tf->command;
555c6fd2807SJeff Garzik 	fis[3] = tf->feature;
556c6fd2807SJeff Garzik 
557c6fd2807SJeff Garzik 	fis[4] = tf->lbal;
558c6fd2807SJeff Garzik 	fis[5] = tf->lbam;
559c6fd2807SJeff Garzik 	fis[6] = tf->lbah;
560c6fd2807SJeff Garzik 	fis[7] = tf->device;
561c6fd2807SJeff Garzik 
562c6fd2807SJeff Garzik 	fis[8] = tf->hob_lbal;
563c6fd2807SJeff Garzik 	fis[9] = tf->hob_lbam;
564c6fd2807SJeff Garzik 	fis[10] = tf->hob_lbah;
565c6fd2807SJeff Garzik 	fis[11] = tf->hob_feature;
566c6fd2807SJeff Garzik 
567c6fd2807SJeff Garzik 	fis[12] = tf->nsect;
568c6fd2807SJeff Garzik 	fis[13] = tf->hob_nsect;
569c6fd2807SJeff Garzik 	fis[14] = 0;
570c6fd2807SJeff Garzik 	fis[15] = tf->ctl;
571c6fd2807SJeff Garzik 
57286a565e6SMarc Carino 	fis[16] = tf->auxiliary & 0xff;
57386a565e6SMarc Carino 	fis[17] = (tf->auxiliary >> 8) & 0xff;
57486a565e6SMarc Carino 	fis[18] = (tf->auxiliary >> 16) & 0xff;
57586a565e6SMarc Carino 	fis[19] = (tf->auxiliary >> 24) & 0xff;
576c6fd2807SJeff Garzik }
577c6fd2807SJeff Garzik 
578c6fd2807SJeff Garzik /**
579c6fd2807SJeff Garzik  *	ata_tf_from_fis - Convert SATA FIS to ATA taskfile
580c6fd2807SJeff Garzik  *	@fis: Buffer from which data will be input
581c6fd2807SJeff Garzik  *	@tf: Taskfile to output
582c6fd2807SJeff Garzik  *
583c6fd2807SJeff Garzik  *	Converts a serial ATA FIS structure to a standard ATA taskfile.
584c6fd2807SJeff Garzik  *
585c6fd2807SJeff Garzik  *	LOCKING:
586c6fd2807SJeff Garzik  *	Inherited from caller.
587c6fd2807SJeff Garzik  */
588c6fd2807SJeff Garzik 
589c6fd2807SJeff Garzik void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
590c6fd2807SJeff Garzik {
591c6fd2807SJeff Garzik 	tf->command	= fis[2];	/* status */
592c6fd2807SJeff Garzik 	tf->feature	= fis[3];	/* error */
593c6fd2807SJeff Garzik 
594c6fd2807SJeff Garzik 	tf->lbal	= fis[4];
595c6fd2807SJeff Garzik 	tf->lbam	= fis[5];
596c6fd2807SJeff Garzik 	tf->lbah	= fis[6];
597c6fd2807SJeff Garzik 	tf->device	= fis[7];
598c6fd2807SJeff Garzik 
599c6fd2807SJeff Garzik 	tf->hob_lbal	= fis[8];
600c6fd2807SJeff Garzik 	tf->hob_lbam	= fis[9];
601c6fd2807SJeff Garzik 	tf->hob_lbah	= fis[10];
602c6fd2807SJeff Garzik 
603c6fd2807SJeff Garzik 	tf->nsect	= fis[12];
604c6fd2807SJeff Garzik 	tf->hob_nsect	= fis[13];
605c6fd2807SJeff Garzik }
606c6fd2807SJeff Garzik 
607c6fd2807SJeff Garzik static const u8 ata_rw_cmds[] = {
608c6fd2807SJeff Garzik 	/* pio multi */
609c6fd2807SJeff Garzik 	ATA_CMD_READ_MULTI,
610c6fd2807SJeff Garzik 	ATA_CMD_WRITE_MULTI,
611c6fd2807SJeff Garzik 	ATA_CMD_READ_MULTI_EXT,
612c6fd2807SJeff Garzik 	ATA_CMD_WRITE_MULTI_EXT,
613c6fd2807SJeff Garzik 	0,
614c6fd2807SJeff Garzik 	0,
615c6fd2807SJeff Garzik 	0,
616c6fd2807SJeff Garzik 	ATA_CMD_WRITE_MULTI_FUA_EXT,
617c6fd2807SJeff Garzik 	/* pio */
618c6fd2807SJeff Garzik 	ATA_CMD_PIO_READ,
619c6fd2807SJeff Garzik 	ATA_CMD_PIO_WRITE,
620c6fd2807SJeff Garzik 	ATA_CMD_PIO_READ_EXT,
621c6fd2807SJeff Garzik 	ATA_CMD_PIO_WRITE_EXT,
622c6fd2807SJeff Garzik 	0,
623c6fd2807SJeff Garzik 	0,
624c6fd2807SJeff Garzik 	0,
625c6fd2807SJeff Garzik 	0,
626c6fd2807SJeff Garzik 	/* dma */
627c6fd2807SJeff Garzik 	ATA_CMD_READ,
628c6fd2807SJeff Garzik 	ATA_CMD_WRITE,
629c6fd2807SJeff Garzik 	ATA_CMD_READ_EXT,
630c6fd2807SJeff Garzik 	ATA_CMD_WRITE_EXT,
631c6fd2807SJeff Garzik 	0,
632c6fd2807SJeff Garzik 	0,
633c6fd2807SJeff Garzik 	0,
634c6fd2807SJeff Garzik 	ATA_CMD_WRITE_FUA_EXT
635c6fd2807SJeff Garzik };
636c6fd2807SJeff Garzik 
637c6fd2807SJeff Garzik /**
638c6fd2807SJeff Garzik  *	ata_rwcmd_protocol - set taskfile r/w commands and protocol
639bd056d7eSTejun Heo  *	@tf: command to examine and configure
640bd056d7eSTejun Heo  *	@dev: device tf belongs to
641c6fd2807SJeff Garzik  *
642c6fd2807SJeff Garzik  *	Examine the device configuration and tf->flags to calculate
643c6fd2807SJeff Garzik  *	the proper read/write commands and protocol to use.
644c6fd2807SJeff Garzik  *
645c6fd2807SJeff Garzik  *	LOCKING:
646c6fd2807SJeff Garzik  *	caller.
647c6fd2807SJeff Garzik  */
648bd056d7eSTejun Heo static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev)
649c6fd2807SJeff Garzik {
650c6fd2807SJeff Garzik 	u8 cmd;
651c6fd2807SJeff Garzik 
652c6fd2807SJeff Garzik 	int index, fua, lba48, write;
653c6fd2807SJeff Garzik 
654c6fd2807SJeff Garzik 	fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
655c6fd2807SJeff Garzik 	lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
656c6fd2807SJeff Garzik 	write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
657c6fd2807SJeff Garzik 
658c6fd2807SJeff Garzik 	if (dev->flags & ATA_DFLAG_PIO) {
659c6fd2807SJeff Garzik 		tf->protocol = ATA_PROT_PIO;
660c6fd2807SJeff Garzik 		index = dev->multi_count ? 0 : 8;
6619af5c9c9STejun Heo 	} else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) {
662c6fd2807SJeff Garzik 		/* Unable to use DMA due to host limitation */
663c6fd2807SJeff Garzik 		tf->protocol = ATA_PROT_PIO;
664c6fd2807SJeff Garzik 		index = dev->multi_count ? 0 : 8;
665c6fd2807SJeff Garzik 	} else {
666c6fd2807SJeff Garzik 		tf->protocol = ATA_PROT_DMA;
667c6fd2807SJeff Garzik 		index = 16;
668c6fd2807SJeff Garzik 	}
669c6fd2807SJeff Garzik 
670c6fd2807SJeff Garzik 	cmd = ata_rw_cmds[index + fua + lba48 + write];
671c6fd2807SJeff Garzik 	if (cmd) {
672c6fd2807SJeff Garzik 		tf->command = cmd;
673c6fd2807SJeff Garzik 		return 0;
674c6fd2807SJeff Garzik 	}
675c6fd2807SJeff Garzik 	return -1;
676c6fd2807SJeff Garzik }
677c6fd2807SJeff Garzik 
678c6fd2807SJeff Garzik /**
67935b649feSTejun Heo  *	ata_tf_read_block - Read block address from ATA taskfile
68035b649feSTejun Heo  *	@tf: ATA taskfile of interest
68135b649feSTejun Heo  *	@dev: ATA device @tf belongs to
68235b649feSTejun Heo  *
68335b649feSTejun Heo  *	LOCKING:
68435b649feSTejun Heo  *	None.
68535b649feSTejun Heo  *
68635b649feSTejun Heo  *	Read block address from @tf.  This function can handle all
68735b649feSTejun Heo  *	three address formats - LBA, LBA48 and CHS.  tf->protocol and
68835b649feSTejun Heo  *	flags select the address format to use.
68935b649feSTejun Heo  *
69035b649feSTejun Heo  *	RETURNS:
69135b649feSTejun Heo  *	Block address read from @tf.
69235b649feSTejun Heo  */
69335b649feSTejun Heo u64 ata_tf_read_block(struct ata_taskfile *tf, struct ata_device *dev)
69435b649feSTejun Heo {
69535b649feSTejun Heo 	u64 block = 0;
69635b649feSTejun Heo 
69735b649feSTejun Heo 	if (tf->flags & ATA_TFLAG_LBA) {
69835b649feSTejun Heo 		if (tf->flags & ATA_TFLAG_LBA48) {
69935b649feSTejun Heo 			block |= (u64)tf->hob_lbah << 40;
70035b649feSTejun Heo 			block |= (u64)tf->hob_lbam << 32;
70144901a96SRoland Dreier 			block |= (u64)tf->hob_lbal << 24;
70235b649feSTejun Heo 		} else
70335b649feSTejun Heo 			block |= (tf->device & 0xf) << 24;
70435b649feSTejun Heo 
70535b649feSTejun Heo 		block |= tf->lbah << 16;
70635b649feSTejun Heo 		block |= tf->lbam << 8;
70735b649feSTejun Heo 		block |= tf->lbal;
70835b649feSTejun Heo 	} else {
70935b649feSTejun Heo 		u32 cyl, head, sect;
71035b649feSTejun Heo 
71135b649feSTejun Heo 		cyl = tf->lbam | (tf->lbah << 8);
71235b649feSTejun Heo 		head = tf->device & 0xf;
71335b649feSTejun Heo 		sect = tf->lbal;
71435b649feSTejun Heo 
715ac8672eaSTejun Heo 		if (!sect) {
716a9a79dfeSJoe Perches 			ata_dev_warn(dev,
717a9a79dfeSJoe Perches 				     "device reported invalid CHS sector 0\n");
718ac8672eaSTejun Heo 			sect = 1; /* oh well */
719ac8672eaSTejun Heo 		}
720ac8672eaSTejun Heo 
721ac8672eaSTejun Heo 		block = (cyl * dev->heads + head) * dev->sectors + sect - 1;
72235b649feSTejun Heo 	}
72335b649feSTejun Heo 
72435b649feSTejun Heo 	return block;
72535b649feSTejun Heo }
72635b649feSTejun Heo 
72735b649feSTejun Heo /**
728bd056d7eSTejun Heo  *	ata_build_rw_tf - Build ATA taskfile for given read/write request
729bd056d7eSTejun Heo  *	@tf: Target ATA taskfile
730bd056d7eSTejun Heo  *	@dev: ATA device @tf belongs to
731bd056d7eSTejun Heo  *	@block: Block address
732bd056d7eSTejun Heo  *	@n_block: Number of blocks
733bd056d7eSTejun Heo  *	@tf_flags: RW/FUA etc...
734bd056d7eSTejun Heo  *	@tag: tag
735bd056d7eSTejun Heo  *
736bd056d7eSTejun Heo  *	LOCKING:
737bd056d7eSTejun Heo  *	None.
738bd056d7eSTejun Heo  *
739bd056d7eSTejun Heo  *	Build ATA taskfile @tf for read/write request described by
740bd056d7eSTejun Heo  *	@block, @n_block, @tf_flags and @tag on @dev.
741bd056d7eSTejun Heo  *
742bd056d7eSTejun Heo  *	RETURNS:
743bd056d7eSTejun Heo  *
744bd056d7eSTejun Heo  *	0 on success, -ERANGE if the request is too large for @dev,
745bd056d7eSTejun Heo  *	-EINVAL if the request is invalid.
746bd056d7eSTejun Heo  */
747bd056d7eSTejun Heo int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev,
748bd056d7eSTejun Heo 		    u64 block, u32 n_block, unsigned int tf_flags,
749bd056d7eSTejun Heo 		    unsigned int tag)
750bd056d7eSTejun Heo {
751bd056d7eSTejun Heo 	tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
752bd056d7eSTejun Heo 	tf->flags |= tf_flags;
753bd056d7eSTejun Heo 
7546d1245bfSTejun Heo 	if (ata_ncq_enabled(dev) && likely(tag != ATA_TAG_INTERNAL)) {
755bd056d7eSTejun Heo 		/* yay, NCQ */
756bd056d7eSTejun Heo 		if (!lba_48_ok(block, n_block))
757bd056d7eSTejun Heo 			return -ERANGE;
758bd056d7eSTejun Heo 
759bd056d7eSTejun Heo 		tf->protocol = ATA_PROT_NCQ;
760bd056d7eSTejun Heo 		tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
761bd056d7eSTejun Heo 
762bd056d7eSTejun Heo 		if (tf->flags & ATA_TFLAG_WRITE)
763bd056d7eSTejun Heo 			tf->command = ATA_CMD_FPDMA_WRITE;
764bd056d7eSTejun Heo 		else
765bd056d7eSTejun Heo 			tf->command = ATA_CMD_FPDMA_READ;
766bd056d7eSTejun Heo 
767bd056d7eSTejun Heo 		tf->nsect = tag << 3;
768bd056d7eSTejun Heo 		tf->hob_feature = (n_block >> 8) & 0xff;
769bd056d7eSTejun Heo 		tf->feature = n_block & 0xff;
770bd056d7eSTejun Heo 
771bd056d7eSTejun Heo 		tf->hob_lbah = (block >> 40) & 0xff;
772bd056d7eSTejun Heo 		tf->hob_lbam = (block >> 32) & 0xff;
773bd056d7eSTejun Heo 		tf->hob_lbal = (block >> 24) & 0xff;
774bd056d7eSTejun Heo 		tf->lbah = (block >> 16) & 0xff;
775bd056d7eSTejun Heo 		tf->lbam = (block >> 8) & 0xff;
776bd056d7eSTejun Heo 		tf->lbal = block & 0xff;
777bd056d7eSTejun Heo 
7789ca7cfa4SSergei Shtylyov 		tf->device = ATA_LBA;
779bd056d7eSTejun Heo 		if (tf->flags & ATA_TFLAG_FUA)
780bd056d7eSTejun Heo 			tf->device |= 1 << 7;
781bd056d7eSTejun Heo 	} else if (dev->flags & ATA_DFLAG_LBA) {
782bd056d7eSTejun Heo 		tf->flags |= ATA_TFLAG_LBA;
783bd056d7eSTejun Heo 
784bd056d7eSTejun Heo 		if (lba_28_ok(block, n_block)) {
785bd056d7eSTejun Heo 			/* use LBA28 */
786bd056d7eSTejun Heo 			tf->device |= (block >> 24) & 0xf;
787bd056d7eSTejun Heo 		} else if (lba_48_ok(block, n_block)) {
788bd056d7eSTejun Heo 			if (!(dev->flags & ATA_DFLAG_LBA48))
789bd056d7eSTejun Heo 				return -ERANGE;
790bd056d7eSTejun Heo 
791bd056d7eSTejun Heo 			/* use LBA48 */
792bd056d7eSTejun Heo 			tf->flags |= ATA_TFLAG_LBA48;
793bd056d7eSTejun Heo 
794bd056d7eSTejun Heo 			tf->hob_nsect = (n_block >> 8) & 0xff;
795bd056d7eSTejun Heo 
796bd056d7eSTejun Heo 			tf->hob_lbah = (block >> 40) & 0xff;
797bd056d7eSTejun Heo 			tf->hob_lbam = (block >> 32) & 0xff;
798bd056d7eSTejun Heo 			tf->hob_lbal = (block >> 24) & 0xff;
799bd056d7eSTejun Heo 		} else
800bd056d7eSTejun Heo 			/* request too large even for LBA48 */
801bd056d7eSTejun Heo 			return -ERANGE;
802bd056d7eSTejun Heo 
803bd056d7eSTejun Heo 		if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
804bd056d7eSTejun Heo 			return -EINVAL;
805bd056d7eSTejun Heo 
806bd056d7eSTejun Heo 		tf->nsect = n_block & 0xff;
807bd056d7eSTejun Heo 
808bd056d7eSTejun Heo 		tf->lbah = (block >> 16) & 0xff;
809bd056d7eSTejun Heo 		tf->lbam = (block >> 8) & 0xff;
810bd056d7eSTejun Heo 		tf->lbal = block & 0xff;
811bd056d7eSTejun Heo 
812bd056d7eSTejun Heo 		tf->device |= ATA_LBA;
813bd056d7eSTejun Heo 	} else {
814bd056d7eSTejun Heo 		/* CHS */
815bd056d7eSTejun Heo 		u32 sect, head, cyl, track;
816bd056d7eSTejun Heo 
817bd056d7eSTejun Heo 		/* The request -may- be too large for CHS addressing. */
818bd056d7eSTejun Heo 		if (!lba_28_ok(block, n_block))
819bd056d7eSTejun Heo 			return -ERANGE;
820bd056d7eSTejun Heo 
821bd056d7eSTejun Heo 		if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
822bd056d7eSTejun Heo 			return -EINVAL;
823bd056d7eSTejun Heo 
824bd056d7eSTejun Heo 		/* Convert LBA to CHS */
825bd056d7eSTejun Heo 		track = (u32)block / dev->sectors;
826bd056d7eSTejun Heo 		cyl   = track / dev->heads;
827bd056d7eSTejun Heo 		head  = track % dev->heads;
828bd056d7eSTejun Heo 		sect  = (u32)block % dev->sectors + 1;
829bd056d7eSTejun Heo 
830bd056d7eSTejun Heo 		DPRINTK("block %u track %u cyl %u head %u sect %u\n",
831bd056d7eSTejun Heo 			(u32)block, track, cyl, head, sect);
832bd056d7eSTejun Heo 
833bd056d7eSTejun Heo 		/* Check whether the converted CHS can fit.
834bd056d7eSTejun Heo 		   Cylinder: 0-65535
835bd056d7eSTejun Heo 		   Head: 0-15
836bd056d7eSTejun Heo 		   Sector: 1-255*/
837bd056d7eSTejun Heo 		if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
838bd056d7eSTejun Heo 			return -ERANGE;
839bd056d7eSTejun Heo 
840bd056d7eSTejun Heo 		tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
841bd056d7eSTejun Heo 		tf->lbal = sect;
842bd056d7eSTejun Heo 		tf->lbam = cyl;
843bd056d7eSTejun Heo 		tf->lbah = cyl >> 8;
844bd056d7eSTejun Heo 		tf->device |= head;
845bd056d7eSTejun Heo 	}
846bd056d7eSTejun Heo 
847bd056d7eSTejun Heo 	return 0;
848bd056d7eSTejun Heo }
849bd056d7eSTejun Heo 
850bd056d7eSTejun Heo /**
851c6fd2807SJeff Garzik  *	ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
852c6fd2807SJeff Garzik  *	@pio_mask: pio_mask
853c6fd2807SJeff Garzik  *	@mwdma_mask: mwdma_mask
854c6fd2807SJeff Garzik  *	@udma_mask: udma_mask
855c6fd2807SJeff Garzik  *
856c6fd2807SJeff Garzik  *	Pack @pio_mask, @mwdma_mask and @udma_mask into a single
857c6fd2807SJeff Garzik  *	unsigned int xfer_mask.
858c6fd2807SJeff Garzik  *
859c6fd2807SJeff Garzik  *	LOCKING:
860c6fd2807SJeff Garzik  *	None.
861c6fd2807SJeff Garzik  *
862c6fd2807SJeff Garzik  *	RETURNS:
863c6fd2807SJeff Garzik  *	Packed xfer_mask.
864c6fd2807SJeff Garzik  */
8657dc951aeSTejun Heo unsigned long ata_pack_xfermask(unsigned long pio_mask,
8667dc951aeSTejun Heo 				unsigned long mwdma_mask,
8677dc951aeSTejun Heo 				unsigned long udma_mask)
868c6fd2807SJeff Garzik {
869c6fd2807SJeff Garzik 	return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
870c6fd2807SJeff Garzik 		((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
871c6fd2807SJeff Garzik 		((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
872c6fd2807SJeff Garzik }
873c6fd2807SJeff Garzik 
874c6fd2807SJeff Garzik /**
875c6fd2807SJeff Garzik  *	ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
876c6fd2807SJeff Garzik  *	@xfer_mask: xfer_mask to unpack
877c6fd2807SJeff Garzik  *	@pio_mask: resulting pio_mask
878c6fd2807SJeff Garzik  *	@mwdma_mask: resulting mwdma_mask
879c6fd2807SJeff Garzik  *	@udma_mask: resulting udma_mask
880c6fd2807SJeff Garzik  *
881c6fd2807SJeff Garzik  *	Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
882c6fd2807SJeff Garzik  *	Any NULL distination masks will be ignored.
883c6fd2807SJeff Garzik  */
8847dc951aeSTejun Heo void ata_unpack_xfermask(unsigned long xfer_mask, unsigned long *pio_mask,
8857dc951aeSTejun Heo 			 unsigned long *mwdma_mask, unsigned long *udma_mask)
886c6fd2807SJeff Garzik {
887c6fd2807SJeff Garzik 	if (pio_mask)
888c6fd2807SJeff Garzik 		*pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
889c6fd2807SJeff Garzik 	if (mwdma_mask)
890c6fd2807SJeff Garzik 		*mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
891c6fd2807SJeff Garzik 	if (udma_mask)
892c6fd2807SJeff Garzik 		*udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
893c6fd2807SJeff Garzik }
894c6fd2807SJeff Garzik 
895c6fd2807SJeff Garzik static const struct ata_xfer_ent {
896c6fd2807SJeff Garzik 	int shift, bits;
897c6fd2807SJeff Garzik 	u8 base;
898c6fd2807SJeff Garzik } ata_xfer_tbl[] = {
89970cd071eSTejun Heo 	{ ATA_SHIFT_PIO, ATA_NR_PIO_MODES, XFER_PIO_0 },
90070cd071eSTejun Heo 	{ ATA_SHIFT_MWDMA, ATA_NR_MWDMA_MODES, XFER_MW_DMA_0 },
90170cd071eSTejun Heo 	{ ATA_SHIFT_UDMA, ATA_NR_UDMA_MODES, XFER_UDMA_0 },
902c6fd2807SJeff Garzik 	{ -1, },
903c6fd2807SJeff Garzik };
904c6fd2807SJeff Garzik 
905c6fd2807SJeff Garzik /**
906c6fd2807SJeff Garzik  *	ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
907c6fd2807SJeff Garzik  *	@xfer_mask: xfer_mask of interest
908c6fd2807SJeff Garzik  *
909c6fd2807SJeff Garzik  *	Return matching XFER_* value for @xfer_mask.  Only the highest
910c6fd2807SJeff Garzik  *	bit of @xfer_mask is considered.
911c6fd2807SJeff Garzik  *
912c6fd2807SJeff Garzik  *	LOCKING:
913c6fd2807SJeff Garzik  *	None.
914c6fd2807SJeff Garzik  *
915c6fd2807SJeff Garzik  *	RETURNS:
91670cd071eSTejun Heo  *	Matching XFER_* value, 0xff if no match found.
917c6fd2807SJeff Garzik  */
9187dc951aeSTejun Heo u8 ata_xfer_mask2mode(unsigned long xfer_mask)
919c6fd2807SJeff Garzik {
920c6fd2807SJeff Garzik 	int highbit = fls(xfer_mask) - 1;
921c6fd2807SJeff Garzik 	const struct ata_xfer_ent *ent;
922c6fd2807SJeff Garzik 
923c6fd2807SJeff Garzik 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
924c6fd2807SJeff Garzik 		if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
925c6fd2807SJeff Garzik 			return ent->base + highbit - ent->shift;
92670cd071eSTejun Heo 	return 0xff;
927c6fd2807SJeff Garzik }
928c6fd2807SJeff Garzik 
929c6fd2807SJeff Garzik /**
930c6fd2807SJeff Garzik  *	ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
931c6fd2807SJeff Garzik  *	@xfer_mode: XFER_* of interest
932c6fd2807SJeff Garzik  *
933c6fd2807SJeff Garzik  *	Return matching xfer_mask for @xfer_mode.
934c6fd2807SJeff Garzik  *
935c6fd2807SJeff Garzik  *	LOCKING:
936c6fd2807SJeff Garzik  *	None.
937c6fd2807SJeff Garzik  *
938c6fd2807SJeff Garzik  *	RETURNS:
939c6fd2807SJeff Garzik  *	Matching xfer_mask, 0 if no match found.
940c6fd2807SJeff Garzik  */
9417dc951aeSTejun Heo unsigned long ata_xfer_mode2mask(u8 xfer_mode)
942c6fd2807SJeff Garzik {
943c6fd2807SJeff Garzik 	const struct ata_xfer_ent *ent;
944c6fd2807SJeff Garzik 
945c6fd2807SJeff Garzik 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
946c6fd2807SJeff Garzik 		if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
94770cd071eSTejun Heo 			return ((2 << (ent->shift + xfer_mode - ent->base)) - 1)
94870cd071eSTejun Heo 				& ~((1 << ent->shift) - 1);
949c6fd2807SJeff Garzik 	return 0;
950c6fd2807SJeff Garzik }
951c6fd2807SJeff Garzik 
952c6fd2807SJeff Garzik /**
953c6fd2807SJeff Garzik  *	ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
954c6fd2807SJeff Garzik  *	@xfer_mode: XFER_* of interest
955c6fd2807SJeff Garzik  *
956c6fd2807SJeff Garzik  *	Return matching xfer_shift for @xfer_mode.
957c6fd2807SJeff Garzik  *
958c6fd2807SJeff Garzik  *	LOCKING:
959c6fd2807SJeff Garzik  *	None.
960c6fd2807SJeff Garzik  *
961c6fd2807SJeff Garzik  *	RETURNS:
962c6fd2807SJeff Garzik  *	Matching xfer_shift, -1 if no match found.
963c6fd2807SJeff Garzik  */
9647dc951aeSTejun Heo int ata_xfer_mode2shift(unsigned long xfer_mode)
965c6fd2807SJeff Garzik {
966c6fd2807SJeff Garzik 	const struct ata_xfer_ent *ent;
967c6fd2807SJeff Garzik 
968c6fd2807SJeff Garzik 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
969c6fd2807SJeff Garzik 		if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
970c6fd2807SJeff Garzik 			return ent->shift;
971c6fd2807SJeff Garzik 	return -1;
972c6fd2807SJeff Garzik }
973c6fd2807SJeff Garzik 
974c6fd2807SJeff Garzik /**
975c6fd2807SJeff Garzik  *	ata_mode_string - convert xfer_mask to string
976c6fd2807SJeff Garzik  *	@xfer_mask: mask of bits supported; only highest bit counts.
977c6fd2807SJeff Garzik  *
978c6fd2807SJeff Garzik  *	Determine string which represents the highest speed
979c6fd2807SJeff Garzik  *	(highest bit in @modemask).
980c6fd2807SJeff Garzik  *
981c6fd2807SJeff Garzik  *	LOCKING:
982c6fd2807SJeff Garzik  *	None.
983c6fd2807SJeff Garzik  *
984c6fd2807SJeff Garzik  *	RETURNS:
985c6fd2807SJeff Garzik  *	Constant C string representing highest speed listed in
986c6fd2807SJeff Garzik  *	@mode_mask, or the constant C string "<n/a>".
987c6fd2807SJeff Garzik  */
9887dc951aeSTejun Heo const char *ata_mode_string(unsigned long xfer_mask)
989c6fd2807SJeff Garzik {
990c6fd2807SJeff Garzik 	static const char * const xfer_mode_str[] = {
991c6fd2807SJeff Garzik 		"PIO0",
992c6fd2807SJeff Garzik 		"PIO1",
993c6fd2807SJeff Garzik 		"PIO2",
994c6fd2807SJeff Garzik 		"PIO3",
995c6fd2807SJeff Garzik 		"PIO4",
996b352e57dSAlan Cox 		"PIO5",
997b352e57dSAlan Cox 		"PIO6",
998c6fd2807SJeff Garzik 		"MWDMA0",
999c6fd2807SJeff Garzik 		"MWDMA1",
1000c6fd2807SJeff Garzik 		"MWDMA2",
1001b352e57dSAlan Cox 		"MWDMA3",
1002b352e57dSAlan Cox 		"MWDMA4",
1003c6fd2807SJeff Garzik 		"UDMA/16",
1004c6fd2807SJeff Garzik 		"UDMA/25",
1005c6fd2807SJeff Garzik 		"UDMA/33",
1006c6fd2807SJeff Garzik 		"UDMA/44",
1007c6fd2807SJeff Garzik 		"UDMA/66",
1008c6fd2807SJeff Garzik 		"UDMA/100",
1009c6fd2807SJeff Garzik 		"UDMA/133",
1010c6fd2807SJeff Garzik 		"UDMA7",
1011c6fd2807SJeff Garzik 	};
1012c6fd2807SJeff Garzik 	int highbit;
1013c6fd2807SJeff Garzik 
1014c6fd2807SJeff Garzik 	highbit = fls(xfer_mask) - 1;
1015c6fd2807SJeff Garzik 	if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
1016c6fd2807SJeff Garzik 		return xfer_mode_str[highbit];
1017c6fd2807SJeff Garzik 	return "<n/a>";
1018c6fd2807SJeff Garzik }
1019c6fd2807SJeff Garzik 
1020d9027470SGwendal Grignou const char *sata_spd_string(unsigned int spd)
1021c6fd2807SJeff Garzik {
1022c6fd2807SJeff Garzik 	static const char * const spd_str[] = {
1023c6fd2807SJeff Garzik 		"1.5 Gbps",
1024c6fd2807SJeff Garzik 		"3.0 Gbps",
10258522ee25SShane Huang 		"6.0 Gbps",
1026c6fd2807SJeff Garzik 	};
1027c6fd2807SJeff Garzik 
1028c6fd2807SJeff Garzik 	if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
1029c6fd2807SJeff Garzik 		return "<unknown>";
1030c6fd2807SJeff Garzik 	return spd_str[spd - 1];
1031c6fd2807SJeff Garzik }
1032c6fd2807SJeff Garzik 
1033c6fd2807SJeff Garzik /**
1034c6fd2807SJeff Garzik  *	ata_dev_classify - determine device type based on ATA-spec signature
1035c6fd2807SJeff Garzik  *	@tf: ATA taskfile register set for device to be identified
1036c6fd2807SJeff Garzik  *
1037c6fd2807SJeff Garzik  *	Determine from taskfile register contents whether a device is
1038c6fd2807SJeff Garzik  *	ATA or ATAPI, as per "Signature and persistence" section
1039c6fd2807SJeff Garzik  *	of ATA/PI spec (volume 1, sect 5.14).
1040c6fd2807SJeff Garzik  *
1041c6fd2807SJeff Garzik  *	LOCKING:
1042c6fd2807SJeff Garzik  *	None.
1043c6fd2807SJeff Garzik  *
1044c6fd2807SJeff Garzik  *	RETURNS:
1045633273a3STejun Heo  *	Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP or
1046633273a3STejun Heo  *	%ATA_DEV_UNKNOWN the event of failure.
1047c6fd2807SJeff Garzik  */
1048c6fd2807SJeff Garzik unsigned int ata_dev_classify(const struct ata_taskfile *tf)
1049c6fd2807SJeff Garzik {
1050c6fd2807SJeff Garzik 	/* Apple's open source Darwin code hints that some devices only
1051c6fd2807SJeff Garzik 	 * put a proper signature into the LBA mid/high registers,
1052c6fd2807SJeff Garzik 	 * So, we only check those.  It's sufficient for uniqueness.
1053633273a3STejun Heo 	 *
1054633273a3STejun Heo 	 * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate
1055633273a3STejun Heo 	 * signatures for ATA and ATAPI devices attached on SerialATA,
1056633273a3STejun Heo 	 * 0x3c/0xc3 and 0x69/0x96 respectively.  However, SerialATA
1057633273a3STejun Heo 	 * spec has never mentioned about using different signatures
1058633273a3STejun Heo 	 * for ATA/ATAPI devices.  Then, Serial ATA II: Port
1059633273a3STejun Heo 	 * Multiplier specification began to use 0x69/0x96 to identify
1060633273a3STejun Heo 	 * port multpliers and 0x3c/0xc3 to identify SEMB device.
1061633273a3STejun Heo 	 * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and
1062633273a3STejun Heo 	 * 0x69/0x96 shortly and described them as reserved for
1063633273a3STejun Heo 	 * SerialATA.
1064633273a3STejun Heo 	 *
1065633273a3STejun Heo 	 * We follow the current spec and consider that 0x69/0x96
1066633273a3STejun Heo 	 * identifies a port multiplier and 0x3c/0xc3 a SEMB device.
106779b42babSTejun Heo 	 * Unfortunately, WDC WD1600JS-62MHB5 (a hard drive) reports
106879b42babSTejun Heo 	 * SEMB signature.  This is worked around in
106979b42babSTejun Heo 	 * ata_dev_read_id().
1070c6fd2807SJeff Garzik 	 */
1071633273a3STejun Heo 	if ((tf->lbam == 0) && (tf->lbah == 0)) {
1072c6fd2807SJeff Garzik 		DPRINTK("found ATA device by sig\n");
1073c6fd2807SJeff Garzik 		return ATA_DEV_ATA;
1074c6fd2807SJeff Garzik 	}
1075c6fd2807SJeff Garzik 
1076633273a3STejun Heo 	if ((tf->lbam == 0x14) && (tf->lbah == 0xeb)) {
1077c6fd2807SJeff Garzik 		DPRINTK("found ATAPI device by sig\n");
1078c6fd2807SJeff Garzik 		return ATA_DEV_ATAPI;
1079c6fd2807SJeff Garzik 	}
1080c6fd2807SJeff Garzik 
1081633273a3STejun Heo 	if ((tf->lbam == 0x69) && (tf->lbah == 0x96)) {
1082633273a3STejun Heo 		DPRINTK("found PMP device by sig\n");
1083633273a3STejun Heo 		return ATA_DEV_PMP;
1084633273a3STejun Heo 	}
1085633273a3STejun Heo 
1086633273a3STejun Heo 	if ((tf->lbam == 0x3c) && (tf->lbah == 0xc3)) {
108779b42babSTejun Heo 		DPRINTK("found SEMB device by sig (could be ATA device)\n");
108879b42babSTejun Heo 		return ATA_DEV_SEMB;
1089633273a3STejun Heo 	}
1090633273a3STejun Heo 
1091c6fd2807SJeff Garzik 	DPRINTK("unknown device\n");
1092c6fd2807SJeff Garzik 	return ATA_DEV_UNKNOWN;
1093c6fd2807SJeff Garzik }
1094c6fd2807SJeff Garzik 
1095c6fd2807SJeff Garzik /**
1096c6fd2807SJeff Garzik  *	ata_id_string - Convert IDENTIFY DEVICE page into string
1097c6fd2807SJeff Garzik  *	@id: IDENTIFY DEVICE results we will examine
1098c6fd2807SJeff Garzik  *	@s: string into which data is output
1099c6fd2807SJeff Garzik  *	@ofs: offset into identify device page
1100c6fd2807SJeff Garzik  *	@len: length of string to return. must be an even number.
1101c6fd2807SJeff Garzik  *
1102c6fd2807SJeff Garzik  *	The strings in the IDENTIFY DEVICE page are broken up into
1103c6fd2807SJeff Garzik  *	16-bit chunks.  Run through the string, and output each
1104c6fd2807SJeff Garzik  *	8-bit chunk linearly, regardless of platform.
1105c6fd2807SJeff Garzik  *
1106c6fd2807SJeff Garzik  *	LOCKING:
1107c6fd2807SJeff Garzik  *	caller.
1108c6fd2807SJeff Garzik  */
1109c6fd2807SJeff Garzik 
1110c6fd2807SJeff Garzik void ata_id_string(const u16 *id, unsigned char *s,
1111c6fd2807SJeff Garzik 		   unsigned int ofs, unsigned int len)
1112c6fd2807SJeff Garzik {
1113c6fd2807SJeff Garzik 	unsigned int c;
1114c6fd2807SJeff Garzik 
1115963e4975SAlan Cox 	BUG_ON(len & 1);
1116963e4975SAlan Cox 
1117c6fd2807SJeff Garzik 	while (len > 0) {
1118c6fd2807SJeff Garzik 		c = id[ofs] >> 8;
1119c6fd2807SJeff Garzik 		*s = c;
1120c6fd2807SJeff Garzik 		s++;
1121c6fd2807SJeff Garzik 
1122c6fd2807SJeff Garzik 		c = id[ofs] & 0xff;
1123c6fd2807SJeff Garzik 		*s = c;
1124c6fd2807SJeff Garzik 		s++;
1125c6fd2807SJeff Garzik 
1126c6fd2807SJeff Garzik 		ofs++;
1127c6fd2807SJeff Garzik 		len -= 2;
1128c6fd2807SJeff Garzik 	}
1129c6fd2807SJeff Garzik }
1130c6fd2807SJeff Garzik 
1131c6fd2807SJeff Garzik /**
1132c6fd2807SJeff Garzik  *	ata_id_c_string - Convert IDENTIFY DEVICE page into C string
1133c6fd2807SJeff Garzik  *	@id: IDENTIFY DEVICE results we will examine
1134c6fd2807SJeff Garzik  *	@s: string into which data is output
1135c6fd2807SJeff Garzik  *	@ofs: offset into identify device page
1136c6fd2807SJeff Garzik  *	@len: length of string to return. must be an odd number.
1137c6fd2807SJeff Garzik  *
1138c6fd2807SJeff Garzik  *	This function is identical to ata_id_string except that it
1139c6fd2807SJeff Garzik  *	trims trailing spaces and terminates the resulting string with
1140c6fd2807SJeff Garzik  *	null.  @len must be actual maximum length (even number) + 1.
1141c6fd2807SJeff Garzik  *
1142c6fd2807SJeff Garzik  *	LOCKING:
1143c6fd2807SJeff Garzik  *	caller.
1144c6fd2807SJeff Garzik  */
1145c6fd2807SJeff Garzik void ata_id_c_string(const u16 *id, unsigned char *s,
1146c6fd2807SJeff Garzik 		     unsigned int ofs, unsigned int len)
1147c6fd2807SJeff Garzik {
1148c6fd2807SJeff Garzik 	unsigned char *p;
1149c6fd2807SJeff Garzik 
1150c6fd2807SJeff Garzik 	ata_id_string(id, s, ofs, len - 1);
1151c6fd2807SJeff Garzik 
1152c6fd2807SJeff Garzik 	p = s + strnlen(s, len - 1);
1153c6fd2807SJeff Garzik 	while (p > s && p[-1] == ' ')
1154c6fd2807SJeff Garzik 		p--;
1155c6fd2807SJeff Garzik 	*p = '\0';
1156c6fd2807SJeff Garzik }
1157c6fd2807SJeff Garzik 
1158db6f8759STejun Heo static u64 ata_id_n_sectors(const u16 *id)
1159db6f8759STejun Heo {
1160db6f8759STejun Heo 	if (ata_id_has_lba(id)) {
1161db6f8759STejun Heo 		if (ata_id_has_lba48(id))
1162968e594aSRobert Hancock 			return ata_id_u64(id, ATA_ID_LBA_CAPACITY_2);
1163db6f8759STejun Heo 		else
1164968e594aSRobert Hancock 			return ata_id_u32(id, ATA_ID_LBA_CAPACITY);
1165db6f8759STejun Heo 	} else {
1166db6f8759STejun Heo 		if (ata_id_current_chs_valid(id))
1167968e594aSRobert Hancock 			return id[ATA_ID_CUR_CYLS] * id[ATA_ID_CUR_HEADS] *
1168968e594aSRobert Hancock 			       id[ATA_ID_CUR_SECTORS];
1169db6f8759STejun Heo 		else
1170968e594aSRobert Hancock 			return id[ATA_ID_CYLS] * id[ATA_ID_HEADS] *
1171968e594aSRobert Hancock 			       id[ATA_ID_SECTORS];
1172db6f8759STejun Heo 	}
1173db6f8759STejun Heo }
1174db6f8759STejun Heo 
1175a5987e0aSTejun Heo u64 ata_tf_to_lba48(const struct ata_taskfile *tf)
11761e999736SAlan Cox {
11771e999736SAlan Cox 	u64 sectors = 0;
11781e999736SAlan Cox 
11791e999736SAlan Cox 	sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40;
11801e999736SAlan Cox 	sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32;
1181ba14a9c2SRoland Dreier 	sectors |= ((u64)(tf->hob_lbal & 0xff)) << 24;
11821e999736SAlan Cox 	sectors |= (tf->lbah & 0xff) << 16;
11831e999736SAlan Cox 	sectors |= (tf->lbam & 0xff) << 8;
11841e999736SAlan Cox 	sectors |= (tf->lbal & 0xff);
11851e999736SAlan Cox 
1186a5987e0aSTejun Heo 	return sectors;
11871e999736SAlan Cox }
11881e999736SAlan Cox 
1189a5987e0aSTejun Heo u64 ata_tf_to_lba(const struct ata_taskfile *tf)
11901e999736SAlan Cox {
11911e999736SAlan Cox 	u64 sectors = 0;
11921e999736SAlan Cox 
11931e999736SAlan Cox 	sectors |= (tf->device & 0x0f) << 24;
11941e999736SAlan Cox 	sectors |= (tf->lbah & 0xff) << 16;
11951e999736SAlan Cox 	sectors |= (tf->lbam & 0xff) << 8;
11961e999736SAlan Cox 	sectors |= (tf->lbal & 0xff);
11971e999736SAlan Cox 
1198a5987e0aSTejun Heo 	return sectors;
11991e999736SAlan Cox }
12001e999736SAlan Cox 
12011e999736SAlan Cox /**
1202c728a914STejun Heo  *	ata_read_native_max_address - Read native max address
1203c728a914STejun Heo  *	@dev: target device
1204c728a914STejun Heo  *	@max_sectors: out parameter for the result native max address
12051e999736SAlan Cox  *
1206c728a914STejun Heo  *	Perform an LBA48 or LBA28 native size query upon the device in
1207c728a914STejun Heo  *	question.
1208c728a914STejun Heo  *
1209c728a914STejun Heo  *	RETURNS:
1210c728a914STejun Heo  *	0 on success, -EACCES if command is aborted by the drive.
1211c728a914STejun Heo  *	-EIO on other errors.
12121e999736SAlan Cox  */
1213c728a914STejun Heo static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
12141e999736SAlan Cox {
1215c728a914STejun Heo 	unsigned int err_mask;
12161e999736SAlan Cox 	struct ata_taskfile tf;
1217c728a914STejun Heo 	int lba48 = ata_id_has_lba48(dev->id);
12181e999736SAlan Cox 
12191e999736SAlan Cox 	ata_tf_init(dev, &tf);
12201e999736SAlan Cox 
1221c728a914STejun Heo 	/* always clear all address registers */
12221e999736SAlan Cox 	tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1223c728a914STejun Heo 
1224c728a914STejun Heo 	if (lba48) {
1225c728a914STejun Heo 		tf.command = ATA_CMD_READ_NATIVE_MAX_EXT;
1226c728a914STejun Heo 		tf.flags |= ATA_TFLAG_LBA48;
1227c728a914STejun Heo 	} else
1228c728a914STejun Heo 		tf.command = ATA_CMD_READ_NATIVE_MAX;
1229c728a914STejun Heo 
12301e999736SAlan Cox 	tf.protocol |= ATA_PROT_NODATA;
1231c728a914STejun Heo 	tf.device |= ATA_LBA;
12321e999736SAlan Cox 
12332b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1234c728a914STejun Heo 	if (err_mask) {
1235a9a79dfeSJoe Perches 		ata_dev_warn(dev,
1236a9a79dfeSJoe Perches 			     "failed to read native max address (err_mask=0x%x)\n",
1237a9a79dfeSJoe Perches 			     err_mask);
1238c728a914STejun Heo 		if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
1239c728a914STejun Heo 			return -EACCES;
1240c728a914STejun Heo 		return -EIO;
1241c728a914STejun Heo 	}
1242c728a914STejun Heo 
1243c728a914STejun Heo 	if (lba48)
1244a5987e0aSTejun Heo 		*max_sectors = ata_tf_to_lba48(&tf) + 1;
1245c728a914STejun Heo 	else
1246a5987e0aSTejun Heo 		*max_sectors = ata_tf_to_lba(&tf) + 1;
124793328e11SAlan Cox 	if (dev->horkage & ATA_HORKAGE_HPA_SIZE)
124893328e11SAlan Cox 		(*max_sectors)--;
12491e999736SAlan Cox 	return 0;
12501e999736SAlan Cox }
12511e999736SAlan Cox 
12521e999736SAlan Cox /**
1253c728a914STejun Heo  *	ata_set_max_sectors - Set max sectors
1254c728a914STejun Heo  *	@dev: target device
12556b38d1d1SRandy Dunlap  *	@new_sectors: new max sectors value to set for the device
12561e999736SAlan Cox  *
1257c728a914STejun Heo  *	Set max sectors of @dev to @new_sectors.
1258c728a914STejun Heo  *
1259c728a914STejun Heo  *	RETURNS:
1260c728a914STejun Heo  *	0 on success, -EACCES if command is aborted or denied (due to
1261c728a914STejun Heo  *	previous non-volatile SET_MAX) by the drive.  -EIO on other
1262c728a914STejun Heo  *	errors.
12631e999736SAlan Cox  */
126405027adcSTejun Heo static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
12651e999736SAlan Cox {
1266c728a914STejun Heo 	unsigned int err_mask;
12671e999736SAlan Cox 	struct ata_taskfile tf;
1268c728a914STejun Heo 	int lba48 = ata_id_has_lba48(dev->id);
12691e999736SAlan Cox 
12701e999736SAlan Cox 	new_sectors--;
12711e999736SAlan Cox 
12721e999736SAlan Cox 	ata_tf_init(dev, &tf);
12731e999736SAlan Cox 
1274c728a914STejun Heo 	tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
12751e999736SAlan Cox 
1276c728a914STejun Heo 	if (lba48) {
1277c728a914STejun Heo 		tf.command = ATA_CMD_SET_MAX_EXT;
1278c728a914STejun Heo 		tf.flags |= ATA_TFLAG_LBA48;
12791e999736SAlan Cox 
12801e999736SAlan Cox 		tf.hob_lbal = (new_sectors >> 24) & 0xff;
12811e999736SAlan Cox 		tf.hob_lbam = (new_sectors >> 32) & 0xff;
12821e999736SAlan Cox 		tf.hob_lbah = (new_sectors >> 40) & 0xff;
12831e582ba4STejun Heo 	} else {
12841e999736SAlan Cox 		tf.command = ATA_CMD_SET_MAX;
1285c728a914STejun Heo 
12861e582ba4STejun Heo 		tf.device |= (new_sectors >> 24) & 0xf;
12871e582ba4STejun Heo 	}
12881e582ba4STejun Heo 
12891e999736SAlan Cox 	tf.protocol |= ATA_PROT_NODATA;
1290c728a914STejun Heo 	tf.device |= ATA_LBA;
12911e999736SAlan Cox 
12921e999736SAlan Cox 	tf.lbal = (new_sectors >> 0) & 0xff;
12931e999736SAlan Cox 	tf.lbam = (new_sectors >> 8) & 0xff;
12941e999736SAlan Cox 	tf.lbah = (new_sectors >> 16) & 0xff;
12951e999736SAlan Cox 
12962b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1297c728a914STejun Heo 	if (err_mask) {
1298a9a79dfeSJoe Perches 		ata_dev_warn(dev,
1299a9a79dfeSJoe Perches 			     "failed to set max address (err_mask=0x%x)\n",
1300a9a79dfeSJoe Perches 			     err_mask);
1301c728a914STejun Heo 		if (err_mask == AC_ERR_DEV &&
1302c728a914STejun Heo 		    (tf.feature & (ATA_ABORTED | ATA_IDNF)))
1303c728a914STejun Heo 			return -EACCES;
1304c728a914STejun Heo 		return -EIO;
1305c728a914STejun Heo 	}
1306c728a914STejun Heo 
13071e999736SAlan Cox 	return 0;
13081e999736SAlan Cox }
13091e999736SAlan Cox 
13101e999736SAlan Cox /**
13111e999736SAlan Cox  *	ata_hpa_resize		-	Resize a device with an HPA set
13121e999736SAlan Cox  *	@dev: Device to resize
13131e999736SAlan Cox  *
13141e999736SAlan Cox  *	Read the size of an LBA28 or LBA48 disk with HPA features and resize
13151e999736SAlan Cox  *	it if required to the full size of the media. The caller must check
13161e999736SAlan Cox  *	the drive has the HPA feature set enabled.
131705027adcSTejun Heo  *
131805027adcSTejun Heo  *	RETURNS:
131905027adcSTejun Heo  *	0 on success, -errno on failure.
13201e999736SAlan Cox  */
132105027adcSTejun Heo static int ata_hpa_resize(struct ata_device *dev)
13221e999736SAlan Cox {
132305027adcSTejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
132405027adcSTejun Heo 	int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
1325445d211bSTejun Heo 	bool unlock_hpa = ata_ignore_hpa || dev->flags & ATA_DFLAG_UNLOCK_HPA;
132605027adcSTejun Heo 	u64 sectors = ata_id_n_sectors(dev->id);
132705027adcSTejun Heo 	u64 native_sectors;
1328c728a914STejun Heo 	int rc;
13291e999736SAlan Cox 
133005027adcSTejun Heo 	/* do we need to do it? */
133105027adcSTejun Heo 	if (dev->class != ATA_DEV_ATA ||
133205027adcSTejun Heo 	    !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) ||
133305027adcSTejun Heo 	    (dev->horkage & ATA_HORKAGE_BROKEN_HPA))
1334c728a914STejun Heo 		return 0;
13351e999736SAlan Cox 
133605027adcSTejun Heo 	/* read native max address */
133705027adcSTejun Heo 	rc = ata_read_native_max_address(dev, &native_sectors);
133805027adcSTejun Heo 	if (rc) {
1339dda7aba1STejun Heo 		/* If device aborted the command or HPA isn't going to
1340dda7aba1STejun Heo 		 * be unlocked, skip HPA resizing.
134105027adcSTejun Heo 		 */
1342445d211bSTejun Heo 		if (rc == -EACCES || !unlock_hpa) {
1343a9a79dfeSJoe Perches 			ata_dev_warn(dev,
1344a9a79dfeSJoe Perches 				     "HPA support seems broken, skipping HPA handling\n");
134505027adcSTejun Heo 			dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
134605027adcSTejun Heo 
134705027adcSTejun Heo 			/* we can continue if device aborted the command */
134805027adcSTejun Heo 			if (rc == -EACCES)
134905027adcSTejun Heo 				rc = 0;
135005027adcSTejun Heo 		}
135105027adcSTejun Heo 
135205027adcSTejun Heo 		return rc;
135305027adcSTejun Heo 	}
13545920dadfSTejun Heo 	dev->n_native_sectors = native_sectors;
135505027adcSTejun Heo 
135605027adcSTejun Heo 	/* nothing to do? */
1357445d211bSTejun Heo 	if (native_sectors <= sectors || !unlock_hpa) {
135805027adcSTejun Heo 		if (!print_info || native_sectors == sectors)
135905027adcSTejun Heo 			return 0;
136005027adcSTejun Heo 
136105027adcSTejun Heo 		if (native_sectors > sectors)
1362a9a79dfeSJoe Perches 			ata_dev_info(dev,
136305027adcSTejun Heo 				"HPA detected: current %llu, native %llu\n",
136405027adcSTejun Heo 				(unsigned long long)sectors,
136505027adcSTejun Heo 				(unsigned long long)native_sectors);
136605027adcSTejun Heo 		else if (native_sectors < sectors)
1367a9a79dfeSJoe Perches 			ata_dev_warn(dev,
1368a9a79dfeSJoe Perches 				"native sectors (%llu) is smaller than sectors (%llu)\n",
136905027adcSTejun Heo 				(unsigned long long)native_sectors,
137005027adcSTejun Heo 				(unsigned long long)sectors);
137105027adcSTejun Heo 		return 0;
13721e999736SAlan Cox 	}
137337301a55STejun Heo 
137405027adcSTejun Heo 	/* let's unlock HPA */
137505027adcSTejun Heo 	rc = ata_set_max_sectors(dev, native_sectors);
137605027adcSTejun Heo 	if (rc == -EACCES) {
137705027adcSTejun Heo 		/* if device aborted the command, skip HPA resizing */
1378a9a79dfeSJoe Perches 		ata_dev_warn(dev,
1379a9a79dfeSJoe Perches 			     "device aborted resize (%llu -> %llu), skipping HPA handling\n",
138005027adcSTejun Heo 			     (unsigned long long)sectors,
138105027adcSTejun Heo 			     (unsigned long long)native_sectors);
138205027adcSTejun Heo 		dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
138305027adcSTejun Heo 		return 0;
138405027adcSTejun Heo 	} else if (rc)
138505027adcSTejun Heo 		return rc;
138605027adcSTejun Heo 
138705027adcSTejun Heo 	/* re-read IDENTIFY data */
138805027adcSTejun Heo 	rc = ata_dev_reread_id(dev, 0);
138905027adcSTejun Heo 	if (rc) {
1390a9a79dfeSJoe Perches 		ata_dev_err(dev,
1391a9a79dfeSJoe Perches 			    "failed to re-read IDENTIFY data after HPA resizing\n");
139205027adcSTejun Heo 		return rc;
139305027adcSTejun Heo 	}
139405027adcSTejun Heo 
139505027adcSTejun Heo 	if (print_info) {
139605027adcSTejun Heo 		u64 new_sectors = ata_id_n_sectors(dev->id);
1397a9a79dfeSJoe Perches 		ata_dev_info(dev,
139805027adcSTejun Heo 			"HPA unlocked: %llu -> %llu, native %llu\n",
139905027adcSTejun Heo 			(unsigned long long)sectors,
140005027adcSTejun Heo 			(unsigned long long)new_sectors,
140105027adcSTejun Heo 			(unsigned long long)native_sectors);
140205027adcSTejun Heo 	}
140305027adcSTejun Heo 
140405027adcSTejun Heo 	return 0;
14051e999736SAlan Cox }
14061e999736SAlan Cox 
1407c6fd2807SJeff Garzik /**
1408c6fd2807SJeff Garzik  *	ata_dump_id - IDENTIFY DEVICE info debugging output
1409c6fd2807SJeff Garzik  *	@id: IDENTIFY DEVICE page to dump
1410c6fd2807SJeff Garzik  *
1411c6fd2807SJeff Garzik  *	Dump selected 16-bit words from the given IDENTIFY DEVICE
1412c6fd2807SJeff Garzik  *	page.
1413c6fd2807SJeff Garzik  *
1414c6fd2807SJeff Garzik  *	LOCKING:
1415c6fd2807SJeff Garzik  *	caller.
1416c6fd2807SJeff Garzik  */
1417c6fd2807SJeff Garzik 
1418c6fd2807SJeff Garzik static inline void ata_dump_id(const u16 *id)
1419c6fd2807SJeff Garzik {
1420c6fd2807SJeff Garzik 	DPRINTK("49==0x%04x  "
1421c6fd2807SJeff Garzik 		"53==0x%04x  "
1422c6fd2807SJeff Garzik 		"63==0x%04x  "
1423c6fd2807SJeff Garzik 		"64==0x%04x  "
1424c6fd2807SJeff Garzik 		"75==0x%04x  \n",
1425c6fd2807SJeff Garzik 		id[49],
1426c6fd2807SJeff Garzik 		id[53],
1427c6fd2807SJeff Garzik 		id[63],
1428c6fd2807SJeff Garzik 		id[64],
1429c6fd2807SJeff Garzik 		id[75]);
1430c6fd2807SJeff Garzik 	DPRINTK("80==0x%04x  "
1431c6fd2807SJeff Garzik 		"81==0x%04x  "
1432c6fd2807SJeff Garzik 		"82==0x%04x  "
1433c6fd2807SJeff Garzik 		"83==0x%04x  "
1434c6fd2807SJeff Garzik 		"84==0x%04x  \n",
1435c6fd2807SJeff Garzik 		id[80],
1436c6fd2807SJeff Garzik 		id[81],
1437c6fd2807SJeff Garzik 		id[82],
1438c6fd2807SJeff Garzik 		id[83],
1439c6fd2807SJeff Garzik 		id[84]);
1440c6fd2807SJeff Garzik 	DPRINTK("88==0x%04x  "
1441c6fd2807SJeff Garzik 		"93==0x%04x\n",
1442c6fd2807SJeff Garzik 		id[88],
1443c6fd2807SJeff Garzik 		id[93]);
1444c6fd2807SJeff Garzik }
1445c6fd2807SJeff Garzik 
1446c6fd2807SJeff Garzik /**
1447c6fd2807SJeff Garzik  *	ata_id_xfermask - Compute xfermask from the given IDENTIFY data
1448c6fd2807SJeff Garzik  *	@id: IDENTIFY data to compute xfer mask from
1449c6fd2807SJeff Garzik  *
1450c6fd2807SJeff Garzik  *	Compute the xfermask for this device. This is not as trivial
1451c6fd2807SJeff Garzik  *	as it seems if we must consider early devices correctly.
1452c6fd2807SJeff Garzik  *
1453c6fd2807SJeff Garzik  *	FIXME: pre IDE drive timing (do we care ?).
1454c6fd2807SJeff Garzik  *
1455c6fd2807SJeff Garzik  *	LOCKING:
1456c6fd2807SJeff Garzik  *	None.
1457c6fd2807SJeff Garzik  *
1458c6fd2807SJeff Garzik  *	RETURNS:
1459c6fd2807SJeff Garzik  *	Computed xfermask
1460c6fd2807SJeff Garzik  */
14617dc951aeSTejun Heo unsigned long ata_id_xfermask(const u16 *id)
1462c6fd2807SJeff Garzik {
14637dc951aeSTejun Heo 	unsigned long pio_mask, mwdma_mask, udma_mask;
1464c6fd2807SJeff Garzik 
1465c6fd2807SJeff Garzik 	/* Usual case. Word 53 indicates word 64 is valid */
1466c6fd2807SJeff Garzik 	if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
1467c6fd2807SJeff Garzik 		pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
1468c6fd2807SJeff Garzik 		pio_mask <<= 3;
1469c6fd2807SJeff Garzik 		pio_mask |= 0x7;
1470c6fd2807SJeff Garzik 	} else {
1471c6fd2807SJeff Garzik 		/* If word 64 isn't valid then Word 51 high byte holds
1472c6fd2807SJeff Garzik 		 * the PIO timing number for the maximum. Turn it into
1473c6fd2807SJeff Garzik 		 * a mask.
1474c6fd2807SJeff Garzik 		 */
14757a0f1c8aSLennert Buytenhek 		u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
147646767aebSAlan Cox 		if (mode < 5)	/* Valid PIO range */
147746767aebSAlan Cox 			pio_mask = (2 << mode) - 1;
147846767aebSAlan Cox 		else
147946767aebSAlan Cox 			pio_mask = 1;
1480c6fd2807SJeff Garzik 
1481c6fd2807SJeff Garzik 		/* But wait.. there's more. Design your standards by
1482c6fd2807SJeff Garzik 		 * committee and you too can get a free iordy field to
1483c6fd2807SJeff Garzik 		 * process. However its the speeds not the modes that
1484c6fd2807SJeff Garzik 		 * are supported... Note drivers using the timing API
1485c6fd2807SJeff Garzik 		 * will get this right anyway
1486c6fd2807SJeff Garzik 		 */
1487c6fd2807SJeff Garzik 	}
1488c6fd2807SJeff Garzik 
1489c6fd2807SJeff Garzik 	mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
1490c6fd2807SJeff Garzik 
1491b352e57dSAlan Cox 	if (ata_id_is_cfa(id)) {
1492b352e57dSAlan Cox 		/*
1493b352e57dSAlan Cox 		 *	Process compact flash extended modes
1494b352e57dSAlan Cox 		 */
149562afe5d7SSergei Shtylyov 		int pio = (id[ATA_ID_CFA_MODES] >> 0) & 0x7;
149662afe5d7SSergei Shtylyov 		int dma = (id[ATA_ID_CFA_MODES] >> 3) & 0x7;
1497b352e57dSAlan Cox 
1498b352e57dSAlan Cox 		if (pio)
1499b352e57dSAlan Cox 			pio_mask |= (1 << 5);
1500b352e57dSAlan Cox 		if (pio > 1)
1501b352e57dSAlan Cox 			pio_mask |= (1 << 6);
1502b352e57dSAlan Cox 		if (dma)
1503b352e57dSAlan Cox 			mwdma_mask |= (1 << 3);
1504b352e57dSAlan Cox 		if (dma > 1)
1505b352e57dSAlan Cox 			mwdma_mask |= (1 << 4);
1506b352e57dSAlan Cox 	}
1507b352e57dSAlan Cox 
1508c6fd2807SJeff Garzik 	udma_mask = 0;
1509c6fd2807SJeff Garzik 	if (id[ATA_ID_FIELD_VALID] & (1 << 2))
1510c6fd2807SJeff Garzik 		udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
1511c6fd2807SJeff Garzik 
1512c6fd2807SJeff Garzik 	return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
1513c6fd2807SJeff Garzik }
1514c6fd2807SJeff Garzik 
15157102d230SAdrian Bunk static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
1516c6fd2807SJeff Garzik {
1517c6fd2807SJeff Garzik 	struct completion *waiting = qc->private_data;
1518c6fd2807SJeff Garzik 
1519c6fd2807SJeff Garzik 	complete(waiting);
1520c6fd2807SJeff Garzik }
1521c6fd2807SJeff Garzik 
1522c6fd2807SJeff Garzik /**
15232432697bSTejun Heo  *	ata_exec_internal_sg - execute libata internal command
1524c6fd2807SJeff Garzik  *	@dev: Device to which the command is sent
1525c6fd2807SJeff Garzik  *	@tf: Taskfile registers for the command and the result
1526c6fd2807SJeff Garzik  *	@cdb: CDB for packet command
1527c6fd2807SJeff Garzik  *	@dma_dir: Data tranfer direction of the command
15285c1ad8b3SRandy Dunlap  *	@sgl: sg list for the data buffer of the command
15292432697bSTejun Heo  *	@n_elem: Number of sg entries
15302b789108STejun Heo  *	@timeout: Timeout in msecs (0 for default)
1531c6fd2807SJeff Garzik  *
1532c6fd2807SJeff Garzik  *	Executes libata internal command with timeout.  @tf contains
1533c6fd2807SJeff Garzik  *	command on entry and result on return.  Timeout and error
1534c6fd2807SJeff Garzik  *	conditions are reported via return value.  No recovery action
1535c6fd2807SJeff Garzik  *	is taken after a command times out.  It's caller's duty to
1536c6fd2807SJeff Garzik  *	clean up after timeout.
1537c6fd2807SJeff Garzik  *
1538c6fd2807SJeff Garzik  *	LOCKING:
1539c6fd2807SJeff Garzik  *	None.  Should be called with kernel context, might sleep.
1540c6fd2807SJeff Garzik  *
1541c6fd2807SJeff Garzik  *	RETURNS:
1542c6fd2807SJeff Garzik  *	Zero on success, AC_ERR_* mask on failure
1543c6fd2807SJeff Garzik  */
15442432697bSTejun Heo unsigned ata_exec_internal_sg(struct ata_device *dev,
1545c6fd2807SJeff Garzik 			      struct ata_taskfile *tf, const u8 *cdb,
154687260216SJens Axboe 			      int dma_dir, struct scatterlist *sgl,
15472b789108STejun Heo 			      unsigned int n_elem, unsigned long timeout)
1548c6fd2807SJeff Garzik {
15499af5c9c9STejun Heo 	struct ata_link *link = dev->link;
15509af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
1551c6fd2807SJeff Garzik 	u8 command = tf->command;
155287fbc5a0STejun Heo 	int auto_timeout = 0;
1553c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
1554c6fd2807SJeff Garzik 	unsigned int tag, preempted_tag;
1555c6fd2807SJeff Garzik 	u32 preempted_sactive, preempted_qc_active;
1556da917d69STejun Heo 	int preempted_nr_active_links;
1557c6fd2807SJeff Garzik 	DECLARE_COMPLETION_ONSTACK(wait);
1558c6fd2807SJeff Garzik 	unsigned long flags;
1559c6fd2807SJeff Garzik 	unsigned int err_mask;
1560c6fd2807SJeff Garzik 	int rc;
1561c6fd2807SJeff Garzik 
1562c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1563c6fd2807SJeff Garzik 
1564c6fd2807SJeff Garzik 	/* no internal command while frozen */
1565c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_FROZEN) {
1566c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
1567c6fd2807SJeff Garzik 		return AC_ERR_SYSTEM;
1568c6fd2807SJeff Garzik 	}
1569c6fd2807SJeff Garzik 
1570c6fd2807SJeff Garzik 	/* initialize internal qc */
1571c6fd2807SJeff Garzik 
1572c6fd2807SJeff Garzik 	/* XXX: Tag 0 is used for drivers with legacy EH as some
1573c6fd2807SJeff Garzik 	 * drivers choke if any other tag is given.  This breaks
1574c6fd2807SJeff Garzik 	 * ata_tag_internal() test for those drivers.  Don't use new
1575c6fd2807SJeff Garzik 	 * EH stuff without converting to it.
1576c6fd2807SJeff Garzik 	 */
1577c6fd2807SJeff Garzik 	if (ap->ops->error_handler)
1578c6fd2807SJeff Garzik 		tag = ATA_TAG_INTERNAL;
1579c6fd2807SJeff Garzik 	else
1580c6fd2807SJeff Garzik 		tag = 0;
1581c6fd2807SJeff Garzik 
15828a8bc223STejun Heo 	if (test_and_set_bit(tag, &ap->qc_allocated))
15838a8bc223STejun Heo 		BUG();
1584c6fd2807SJeff Garzik 	qc = __ata_qc_from_tag(ap, tag);
1585c6fd2807SJeff Garzik 
1586c6fd2807SJeff Garzik 	qc->tag = tag;
1587c6fd2807SJeff Garzik 	qc->scsicmd = NULL;
1588c6fd2807SJeff Garzik 	qc->ap = ap;
1589c6fd2807SJeff Garzik 	qc->dev = dev;
1590c6fd2807SJeff Garzik 	ata_qc_reinit(qc);
1591c6fd2807SJeff Garzik 
15929af5c9c9STejun Heo 	preempted_tag = link->active_tag;
15939af5c9c9STejun Heo 	preempted_sactive = link->sactive;
1594c6fd2807SJeff Garzik 	preempted_qc_active = ap->qc_active;
1595da917d69STejun Heo 	preempted_nr_active_links = ap->nr_active_links;
15969af5c9c9STejun Heo 	link->active_tag = ATA_TAG_POISON;
15979af5c9c9STejun Heo 	link->sactive = 0;
1598c6fd2807SJeff Garzik 	ap->qc_active = 0;
1599da917d69STejun Heo 	ap->nr_active_links = 0;
1600c6fd2807SJeff Garzik 
1601c6fd2807SJeff Garzik 	/* prepare & issue qc */
1602c6fd2807SJeff Garzik 	qc->tf = *tf;
1603c6fd2807SJeff Garzik 	if (cdb)
1604c6fd2807SJeff Garzik 		memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
1605e771451cSVincent Pelletier 
1606e771451cSVincent Pelletier 	/* some SATA bridges need us to indicate data xfer direction */
1607e771451cSVincent Pelletier 	if (tf->protocol == ATAPI_PROT_DMA && (dev->flags & ATA_DFLAG_DMADIR) &&
1608e771451cSVincent Pelletier 	    dma_dir == DMA_FROM_DEVICE)
1609e771451cSVincent Pelletier 		qc->tf.feature |= ATAPI_DMADIR;
1610e771451cSVincent Pelletier 
1611c6fd2807SJeff Garzik 	qc->flags |= ATA_QCFLAG_RESULT_TF;
1612c6fd2807SJeff Garzik 	qc->dma_dir = dma_dir;
1613c6fd2807SJeff Garzik 	if (dma_dir != DMA_NONE) {
16142432697bSTejun Heo 		unsigned int i, buflen = 0;
161587260216SJens Axboe 		struct scatterlist *sg;
16162432697bSTejun Heo 
161787260216SJens Axboe 		for_each_sg(sgl, sg, n_elem, i)
161887260216SJens Axboe 			buflen += sg->length;
16192432697bSTejun Heo 
162087260216SJens Axboe 		ata_sg_init(qc, sgl, n_elem);
162149c80429SBrian King 		qc->nbytes = buflen;
1622c6fd2807SJeff Garzik 	}
1623c6fd2807SJeff Garzik 
1624c6fd2807SJeff Garzik 	qc->private_data = &wait;
1625c6fd2807SJeff Garzik 	qc->complete_fn = ata_qc_complete_internal;
1626c6fd2807SJeff Garzik 
1627c6fd2807SJeff Garzik 	ata_qc_issue(qc);
1628c6fd2807SJeff Garzik 
1629c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1630c6fd2807SJeff Garzik 
163187fbc5a0STejun Heo 	if (!timeout) {
163287fbc5a0STejun Heo 		if (ata_probe_timeout)
1633341c2c95STejun Heo 			timeout = ata_probe_timeout * 1000;
163487fbc5a0STejun Heo 		else {
163587fbc5a0STejun Heo 			timeout = ata_internal_cmd_timeout(dev, command);
163687fbc5a0STejun Heo 			auto_timeout = 1;
163787fbc5a0STejun Heo 		}
163887fbc5a0STejun Heo 	}
16392b789108STejun Heo 
1640c0c362b6STejun Heo 	if (ap->ops->error_handler)
1641c0c362b6STejun Heo 		ata_eh_release(ap);
1642c0c362b6STejun Heo 
16432b789108STejun Heo 	rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
1644c6fd2807SJeff Garzik 
1645c0c362b6STejun Heo 	if (ap->ops->error_handler)
1646c0c362b6STejun Heo 		ata_eh_acquire(ap);
1647c0c362b6STejun Heo 
1648c429137aSTejun Heo 	ata_sff_flush_pio_task(ap);
1649c6fd2807SJeff Garzik 
1650c6fd2807SJeff Garzik 	if (!rc) {
1651c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
1652c6fd2807SJeff Garzik 
1653c6fd2807SJeff Garzik 		/* We're racing with irq here.  If we lose, the
1654c6fd2807SJeff Garzik 		 * following test prevents us from completing the qc
1655c6fd2807SJeff Garzik 		 * twice.  If we win, the port is frozen and will be
1656c6fd2807SJeff Garzik 		 * cleaned up by ->post_internal_cmd().
1657c6fd2807SJeff Garzik 		 */
1658c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_ACTIVE) {
1659c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_TIMEOUT;
1660c6fd2807SJeff Garzik 
1661c6fd2807SJeff Garzik 			if (ap->ops->error_handler)
1662c6fd2807SJeff Garzik 				ata_port_freeze(ap);
1663c6fd2807SJeff Garzik 			else
1664c6fd2807SJeff Garzik 				ata_qc_complete(qc);
1665c6fd2807SJeff Garzik 
1666c6fd2807SJeff Garzik 			if (ata_msg_warn(ap))
1667a9a79dfeSJoe Perches 				ata_dev_warn(dev, "qc timeout (cmd 0x%x)\n",
1668a9a79dfeSJoe Perches 					     command);
1669c6fd2807SJeff Garzik 		}
1670c6fd2807SJeff Garzik 
1671c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
1672c6fd2807SJeff Garzik 	}
1673c6fd2807SJeff Garzik 
1674c6fd2807SJeff Garzik 	/* do post_internal_cmd */
1675c6fd2807SJeff Garzik 	if (ap->ops->post_internal_cmd)
1676c6fd2807SJeff Garzik 		ap->ops->post_internal_cmd(qc);
1677c6fd2807SJeff Garzik 
1678a51d644aSTejun Heo 	/* perform minimal error analysis */
1679a51d644aSTejun Heo 	if (qc->flags & ATA_QCFLAG_FAILED) {
1680a51d644aSTejun Heo 		if (qc->result_tf.command & (ATA_ERR | ATA_DF))
1681a51d644aSTejun Heo 			qc->err_mask |= AC_ERR_DEV;
1682a51d644aSTejun Heo 
1683a51d644aSTejun Heo 		if (!qc->err_mask)
1684c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_OTHER;
1685a51d644aSTejun Heo 
1686a51d644aSTejun Heo 		if (qc->err_mask & ~AC_ERR_OTHER)
1687a51d644aSTejun Heo 			qc->err_mask &= ~AC_ERR_OTHER;
1688c6fd2807SJeff Garzik 	}
1689c6fd2807SJeff Garzik 
1690c6fd2807SJeff Garzik 	/* finish up */
1691c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1692c6fd2807SJeff Garzik 
1693c6fd2807SJeff Garzik 	*tf = qc->result_tf;
1694c6fd2807SJeff Garzik 	err_mask = qc->err_mask;
1695c6fd2807SJeff Garzik 
1696c6fd2807SJeff Garzik 	ata_qc_free(qc);
16979af5c9c9STejun Heo 	link->active_tag = preempted_tag;
16989af5c9c9STejun Heo 	link->sactive = preempted_sactive;
1699c6fd2807SJeff Garzik 	ap->qc_active = preempted_qc_active;
1700da917d69STejun Heo 	ap->nr_active_links = preempted_nr_active_links;
1701c6fd2807SJeff Garzik 
1702c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1703c6fd2807SJeff Garzik 
170487fbc5a0STejun Heo 	if ((err_mask & AC_ERR_TIMEOUT) && auto_timeout)
170587fbc5a0STejun Heo 		ata_internal_cmd_timed_out(dev, command);
170687fbc5a0STejun Heo 
1707c6fd2807SJeff Garzik 	return err_mask;
1708c6fd2807SJeff Garzik }
1709c6fd2807SJeff Garzik 
1710c6fd2807SJeff Garzik /**
171133480a0eSTejun Heo  *	ata_exec_internal - execute libata internal command
17122432697bSTejun Heo  *	@dev: Device to which the command is sent
17132432697bSTejun Heo  *	@tf: Taskfile registers for the command and the result
17142432697bSTejun Heo  *	@cdb: CDB for packet command
17152432697bSTejun Heo  *	@dma_dir: Data tranfer direction of the command
17162432697bSTejun Heo  *	@buf: Data buffer of the command
17172432697bSTejun Heo  *	@buflen: Length of data buffer
17182b789108STejun Heo  *	@timeout: Timeout in msecs (0 for default)
17192432697bSTejun Heo  *
17202432697bSTejun Heo  *	Wrapper around ata_exec_internal_sg() which takes simple
17212432697bSTejun Heo  *	buffer instead of sg list.
17222432697bSTejun Heo  *
17232432697bSTejun Heo  *	LOCKING:
17242432697bSTejun Heo  *	None.  Should be called with kernel context, might sleep.
17252432697bSTejun Heo  *
17262432697bSTejun Heo  *	RETURNS:
17272432697bSTejun Heo  *	Zero on success, AC_ERR_* mask on failure
17282432697bSTejun Heo  */
17292432697bSTejun Heo unsigned ata_exec_internal(struct ata_device *dev,
17302432697bSTejun Heo 			   struct ata_taskfile *tf, const u8 *cdb,
17312b789108STejun Heo 			   int dma_dir, void *buf, unsigned int buflen,
17322b789108STejun Heo 			   unsigned long timeout)
17332432697bSTejun Heo {
173433480a0eSTejun Heo 	struct scatterlist *psg = NULL, sg;
173533480a0eSTejun Heo 	unsigned int n_elem = 0;
17362432697bSTejun Heo 
173733480a0eSTejun Heo 	if (dma_dir != DMA_NONE) {
173833480a0eSTejun Heo 		WARN_ON(!buf);
17392432697bSTejun Heo 		sg_init_one(&sg, buf, buflen);
174033480a0eSTejun Heo 		psg = &sg;
174133480a0eSTejun Heo 		n_elem++;
174233480a0eSTejun Heo 	}
17432432697bSTejun Heo 
17442b789108STejun Heo 	return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem,
17452b789108STejun Heo 				    timeout);
17462432697bSTejun Heo }
17472432697bSTejun Heo 
17482432697bSTejun Heo /**
1749c6fd2807SJeff Garzik  *	ata_do_simple_cmd - execute simple internal command
1750c6fd2807SJeff Garzik  *	@dev: Device to which the command is sent
1751c6fd2807SJeff Garzik  *	@cmd: Opcode to execute
1752c6fd2807SJeff Garzik  *
1753c6fd2807SJeff Garzik  *	Execute a 'simple' command, that only consists of the opcode
1754c6fd2807SJeff Garzik  *	'cmd' itself, without filling any other registers
1755c6fd2807SJeff Garzik  *
1756c6fd2807SJeff Garzik  *	LOCKING:
1757c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1758c6fd2807SJeff Garzik  *
1759c6fd2807SJeff Garzik  *	RETURNS:
1760c6fd2807SJeff Garzik  *	Zero on success, AC_ERR_* mask on failure
1761c6fd2807SJeff Garzik  */
1762c6fd2807SJeff Garzik unsigned int ata_do_simple_cmd(struct ata_device *dev, u8 cmd)
1763c6fd2807SJeff Garzik {
1764c6fd2807SJeff Garzik 	struct ata_taskfile tf;
1765c6fd2807SJeff Garzik 
1766c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
1767c6fd2807SJeff Garzik 
1768c6fd2807SJeff Garzik 	tf.command = cmd;
1769c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_DEVICE;
1770c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
1771c6fd2807SJeff Garzik 
17722b789108STejun Heo 	return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1773c6fd2807SJeff Garzik }
1774c6fd2807SJeff Garzik 
1775c6fd2807SJeff Garzik /**
1776c6fd2807SJeff Garzik  *	ata_pio_need_iordy	-	check if iordy needed
1777c6fd2807SJeff Garzik  *	@adev: ATA device
1778c6fd2807SJeff Garzik  *
1779c6fd2807SJeff Garzik  *	Check if the current speed of the device requires IORDY. Used
1780c6fd2807SJeff Garzik  *	by various controllers for chip configuration.
1781c6fd2807SJeff Garzik  */
1782c6fd2807SJeff Garzik unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1783c6fd2807SJeff Garzik {
17840d9e6659STejun Heo 	/* Don't set IORDY if we're preparing for reset.  IORDY may
17850d9e6659STejun Heo 	 * lead to controller lock up on certain controllers if the
17860d9e6659STejun Heo 	 * port is not occupied.  See bko#11703 for details.
17870d9e6659STejun Heo 	 */
17880d9e6659STejun Heo 	if (adev->link->ap->pflags & ATA_PFLAG_RESETTING)
17890d9e6659STejun Heo 		return 0;
17900d9e6659STejun Heo 	/* Controller doesn't support IORDY.  Probably a pointless
17910d9e6659STejun Heo 	 * check as the caller should know this.
17920d9e6659STejun Heo 	 */
17939af5c9c9STejun Heo 	if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
1794c6fd2807SJeff Garzik 		return 0;
17955c18c4d2SDavid Daney 	/* CF spec. r4.1 Table 22 says no iordy on PIO5 and PIO6.  */
17965c18c4d2SDavid Daney 	if (ata_id_is_cfa(adev->id)
17975c18c4d2SDavid Daney 	    && (adev->pio_mode == XFER_PIO_5 || adev->pio_mode == XFER_PIO_6))
17985c18c4d2SDavid Daney 		return 0;
1799432729f0SAlan Cox 	/* PIO3 and higher it is mandatory */
1800432729f0SAlan Cox 	if (adev->pio_mode > XFER_PIO_2)
1801c6fd2807SJeff Garzik 		return 1;
1802432729f0SAlan Cox 	/* We turn it on when possible */
1803432729f0SAlan Cox 	if (ata_id_has_iordy(adev->id))
1804432729f0SAlan Cox 		return 1;
1805432729f0SAlan Cox 	return 0;
1806432729f0SAlan Cox }
1807c6fd2807SJeff Garzik 
1808432729f0SAlan Cox /**
1809432729f0SAlan Cox  *	ata_pio_mask_no_iordy	-	Return the non IORDY mask
1810432729f0SAlan Cox  *	@adev: ATA device
1811432729f0SAlan Cox  *
1812432729f0SAlan Cox  *	Compute the highest mode possible if we are not using iordy. Return
1813432729f0SAlan Cox  *	-1 if no iordy mode is available.
1814432729f0SAlan Cox  */
1815432729f0SAlan Cox static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
1816432729f0SAlan Cox {
1817c6fd2807SJeff Garzik 	/* If we have no drive specific rule, then PIO 2 is non IORDY */
1818c6fd2807SJeff Garzik 	if (adev->id[ATA_ID_FIELD_VALID] & 2) {	/* EIDE */
1819432729f0SAlan Cox 		u16 pio = adev->id[ATA_ID_EIDE_PIO];
1820c6fd2807SJeff Garzik 		/* Is the speed faster than the drive allows non IORDY ? */
1821c6fd2807SJeff Garzik 		if (pio) {
1822c6fd2807SJeff Garzik 			/* This is cycle times not frequency - watch the logic! */
1823c6fd2807SJeff Garzik 			if (pio > 240)	/* PIO2 is 240nS per cycle */
1824432729f0SAlan Cox 				return 3 << ATA_SHIFT_PIO;
1825432729f0SAlan Cox 			return 7 << ATA_SHIFT_PIO;
1826c6fd2807SJeff Garzik 		}
1827c6fd2807SJeff Garzik 	}
1828432729f0SAlan Cox 	return 3 << ATA_SHIFT_PIO;
1829c6fd2807SJeff Garzik }
1830c6fd2807SJeff Garzik 
1831c6fd2807SJeff Garzik /**
1832963e4975SAlan Cox  *	ata_do_dev_read_id		-	default ID read method
1833963e4975SAlan Cox  *	@dev: device
1834963e4975SAlan Cox  *	@tf: proposed taskfile
1835963e4975SAlan Cox  *	@id: data buffer
1836963e4975SAlan Cox  *
1837963e4975SAlan Cox  *	Issue the identify taskfile and hand back the buffer containing
1838963e4975SAlan Cox  *	identify data. For some RAID controllers and for pre ATA devices
1839963e4975SAlan Cox  *	this function is wrapped or replaced by the driver
1840963e4975SAlan Cox  */
1841963e4975SAlan Cox unsigned int ata_do_dev_read_id(struct ata_device *dev,
1842963e4975SAlan Cox 					struct ata_taskfile *tf, u16 *id)
1843963e4975SAlan Cox {
1844963e4975SAlan Cox 	return ata_exec_internal(dev, tf, NULL, DMA_FROM_DEVICE,
1845963e4975SAlan Cox 				     id, sizeof(id[0]) * ATA_ID_WORDS, 0);
1846963e4975SAlan Cox }
1847963e4975SAlan Cox 
1848963e4975SAlan Cox /**
1849c6fd2807SJeff Garzik  *	ata_dev_read_id - Read ID data from the specified device
1850c6fd2807SJeff Garzik  *	@dev: target device
1851c6fd2807SJeff Garzik  *	@p_class: pointer to class of the target device (may be changed)
1852bff04647STejun Heo  *	@flags: ATA_READID_* flags
1853c6fd2807SJeff Garzik  *	@id: buffer to read IDENTIFY data into
1854c6fd2807SJeff Garzik  *
1855c6fd2807SJeff Garzik  *	Read ID data from the specified device.  ATA_CMD_ID_ATA is
1856c6fd2807SJeff Garzik  *	performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
1857c6fd2807SJeff Garzik  *	devices.  This function also issues ATA_CMD_INIT_DEV_PARAMS
1858c6fd2807SJeff Garzik  *	for pre-ATA4 drives.
1859c6fd2807SJeff Garzik  *
186050a99018SAlan Cox  *	FIXME: ATA_CMD_ID_ATA is optional for early drives and right
186150a99018SAlan Cox  *	now we abort if we hit that case.
186250a99018SAlan Cox  *
1863c6fd2807SJeff Garzik  *	LOCKING:
1864c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
1865c6fd2807SJeff Garzik  *
1866c6fd2807SJeff Garzik  *	RETURNS:
1867c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
1868c6fd2807SJeff Garzik  */
1869c6fd2807SJeff Garzik int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
1870bff04647STejun Heo 		    unsigned int flags, u16 *id)
1871c6fd2807SJeff Garzik {
18729af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
1873c6fd2807SJeff Garzik 	unsigned int class = *p_class;
1874c6fd2807SJeff Garzik 	struct ata_taskfile tf;
1875c6fd2807SJeff Garzik 	unsigned int err_mask = 0;
1876c6fd2807SJeff Garzik 	const char *reason;
187779b42babSTejun Heo 	bool is_semb = class == ATA_DEV_SEMB;
187854936f8bSTejun Heo 	int may_fallback = 1, tried_spinup = 0;
1879c6fd2807SJeff Garzik 	int rc;
1880c6fd2807SJeff Garzik 
1881c6fd2807SJeff Garzik 	if (ata_msg_ctl(ap))
1882a9a79dfeSJoe Perches 		ata_dev_dbg(dev, "%s: ENTER\n", __func__);
1883c6fd2807SJeff Garzik 
1884c6fd2807SJeff Garzik retry:
1885c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
1886c6fd2807SJeff Garzik 
1887c6fd2807SJeff Garzik 	switch (class) {
188879b42babSTejun Heo 	case ATA_DEV_SEMB:
188979b42babSTejun Heo 		class = ATA_DEV_ATA;	/* some hard drives report SEMB sig */
1890c6fd2807SJeff Garzik 	case ATA_DEV_ATA:
1891c6fd2807SJeff Garzik 		tf.command = ATA_CMD_ID_ATA;
1892c6fd2807SJeff Garzik 		break;
1893c6fd2807SJeff Garzik 	case ATA_DEV_ATAPI:
1894c6fd2807SJeff Garzik 		tf.command = ATA_CMD_ID_ATAPI;
1895c6fd2807SJeff Garzik 		break;
1896c6fd2807SJeff Garzik 	default:
1897c6fd2807SJeff Garzik 		rc = -ENODEV;
1898c6fd2807SJeff Garzik 		reason = "unsupported class";
1899c6fd2807SJeff Garzik 		goto err_out;
1900c6fd2807SJeff Garzik 	}
1901c6fd2807SJeff Garzik 
1902c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_PIO;
190381afe893STejun Heo 
190481afe893STejun Heo 	/* Some devices choke if TF registers contain garbage.  Make
190581afe893STejun Heo 	 * sure those are properly initialized.
190681afe893STejun Heo 	 */
190781afe893STejun Heo 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
190881afe893STejun Heo 
190981afe893STejun Heo 	/* Device presence detection is unreliable on some
191081afe893STejun Heo 	 * controllers.  Always poll IDENTIFY if available.
191181afe893STejun Heo 	 */
191281afe893STejun Heo 	tf.flags |= ATA_TFLAG_POLLING;
1913c6fd2807SJeff Garzik 
1914963e4975SAlan Cox 	if (ap->ops->read_id)
1915963e4975SAlan Cox 		err_mask = ap->ops->read_id(dev, &tf, id);
1916963e4975SAlan Cox 	else
1917963e4975SAlan Cox 		err_mask = ata_do_dev_read_id(dev, &tf, id);
1918963e4975SAlan Cox 
1919c6fd2807SJeff Garzik 	if (err_mask) {
1920800b3996STejun Heo 		if (err_mask & AC_ERR_NODEV_HINT) {
1921a9a79dfeSJoe Perches 			ata_dev_dbg(dev, "NODEV after polling detection\n");
192255a8e2c8STejun Heo 			return -ENOENT;
192355a8e2c8STejun Heo 		}
192455a8e2c8STejun Heo 
192579b42babSTejun Heo 		if (is_semb) {
1926a9a79dfeSJoe Perches 			ata_dev_info(dev,
1927a9a79dfeSJoe Perches 		     "IDENTIFY failed on device w/ SEMB sig, disabled\n");
192879b42babSTejun Heo 			/* SEMB is not supported yet */
192979b42babSTejun Heo 			*p_class = ATA_DEV_SEMB_UNSUP;
193079b42babSTejun Heo 			return 0;
193179b42babSTejun Heo 		}
193279b42babSTejun Heo 
19331ffc151fSTejun Heo 		if ((err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
19341ffc151fSTejun Heo 			/* Device or controller might have reported
19351ffc151fSTejun Heo 			 * the wrong device class.  Give a shot at the
19361ffc151fSTejun Heo 			 * other IDENTIFY if the current one is
19371ffc151fSTejun Heo 			 * aborted by the device.
193854936f8bSTejun Heo 			 */
19391ffc151fSTejun Heo 			if (may_fallback) {
194054936f8bSTejun Heo 				may_fallback = 0;
194154936f8bSTejun Heo 
194254936f8bSTejun Heo 				if (class == ATA_DEV_ATA)
194354936f8bSTejun Heo 					class = ATA_DEV_ATAPI;
194454936f8bSTejun Heo 				else
194554936f8bSTejun Heo 					class = ATA_DEV_ATA;
194654936f8bSTejun Heo 				goto retry;
194754936f8bSTejun Heo 			}
194854936f8bSTejun Heo 
19491ffc151fSTejun Heo 			/* Control reaches here iff the device aborted
19501ffc151fSTejun Heo 			 * both flavors of IDENTIFYs which happens
19511ffc151fSTejun Heo 			 * sometimes with phantom devices.
19521ffc151fSTejun Heo 			 */
1953a9a79dfeSJoe Perches 			ata_dev_dbg(dev,
19541ffc151fSTejun Heo 				    "both IDENTIFYs aborted, assuming NODEV\n");
19551ffc151fSTejun Heo 			return -ENOENT;
19561ffc151fSTejun Heo 		}
19571ffc151fSTejun Heo 
1958c6fd2807SJeff Garzik 		rc = -EIO;
1959c6fd2807SJeff Garzik 		reason = "I/O error";
1960c6fd2807SJeff Garzik 		goto err_out;
1961c6fd2807SJeff Garzik 	}
1962c6fd2807SJeff Garzik 
196343c9c591STejun Heo 	if (dev->horkage & ATA_HORKAGE_DUMP_ID) {
1964a9a79dfeSJoe Perches 		ata_dev_dbg(dev, "dumping IDENTIFY data, "
196543c9c591STejun Heo 			    "class=%d may_fallback=%d tried_spinup=%d\n",
196643c9c591STejun Heo 			    class, may_fallback, tried_spinup);
196743c9c591STejun Heo 		print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET,
196843c9c591STejun Heo 			       16, 2, id, ATA_ID_WORDS * sizeof(*id), true);
196943c9c591STejun Heo 	}
197043c9c591STejun Heo 
197154936f8bSTejun Heo 	/* Falling back doesn't make sense if ID data was read
197254936f8bSTejun Heo 	 * successfully at least once.
197354936f8bSTejun Heo 	 */
197454936f8bSTejun Heo 	may_fallback = 0;
197554936f8bSTejun Heo 
1976c6fd2807SJeff Garzik 	swap_buf_le16(id, ATA_ID_WORDS);
1977c6fd2807SJeff Garzik 
1978c6fd2807SJeff Garzik 	/* sanity check */
1979c6fd2807SJeff Garzik 	rc = -EINVAL;
19806070068bSAlan Cox 	reason = "device reports invalid type";
19814a3381feSJeff Garzik 
19824a3381feSJeff Garzik 	if (class == ATA_DEV_ATA) {
19834a3381feSJeff Garzik 		if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
19844a3381feSJeff Garzik 			goto err_out;
1985db63a4c8SAndy Whitcroft 		if (ap->host->flags & ATA_HOST_IGNORE_ATA &&
1986db63a4c8SAndy Whitcroft 							ata_id_is_ata(id)) {
1987db63a4c8SAndy Whitcroft 			ata_dev_dbg(dev,
1988db63a4c8SAndy Whitcroft 				"host indicates ignore ATA devices, ignored\n");
1989db63a4c8SAndy Whitcroft 			return -ENOENT;
1990db63a4c8SAndy Whitcroft 		}
19914a3381feSJeff Garzik 	} else {
19924a3381feSJeff Garzik 		if (ata_id_is_ata(id))
1993c6fd2807SJeff Garzik 			goto err_out;
1994c6fd2807SJeff Garzik 	}
1995c6fd2807SJeff Garzik 
1996169439c2SMark Lord 	if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
1997169439c2SMark Lord 		tried_spinup = 1;
1998169439c2SMark Lord 		/*
1999169439c2SMark Lord 		 * Drive powered-up in standby mode, and requires a specific
2000169439c2SMark Lord 		 * SET_FEATURES spin-up subcommand before it will accept
2001169439c2SMark Lord 		 * anything other than the original IDENTIFY command.
2002169439c2SMark Lord 		 */
2003218f3d30SJeff Garzik 		err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
2004fb0582f9SRyan Power 		if (err_mask && id[2] != 0x738c) {
2005169439c2SMark Lord 			rc = -EIO;
2006169439c2SMark Lord 			reason = "SPINUP failed";
2007169439c2SMark Lord 			goto err_out;
2008169439c2SMark Lord 		}
2009169439c2SMark Lord 		/*
2010169439c2SMark Lord 		 * If the drive initially returned incomplete IDENTIFY info,
2011169439c2SMark Lord 		 * we now must reissue the IDENTIFY command.
2012169439c2SMark Lord 		 */
2013169439c2SMark Lord 		if (id[2] == 0x37c8)
2014169439c2SMark Lord 			goto retry;
2015169439c2SMark Lord 	}
2016169439c2SMark Lord 
2017bff04647STejun Heo 	if ((flags & ATA_READID_POSTRESET) && class == ATA_DEV_ATA) {
2018c6fd2807SJeff Garzik 		/*
2019c6fd2807SJeff Garzik 		 * The exact sequence expected by certain pre-ATA4 drives is:
2020c6fd2807SJeff Garzik 		 * SRST RESET
202150a99018SAlan Cox 		 * IDENTIFY (optional in early ATA)
202250a99018SAlan Cox 		 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
2023c6fd2807SJeff Garzik 		 * anything else..
2024c6fd2807SJeff Garzik 		 * Some drives were very specific about that exact sequence.
202550a99018SAlan Cox 		 *
202650a99018SAlan Cox 		 * Note that ATA4 says lba is mandatory so the second check
2027c9404c9cSAdam Buchbinder 		 * should never trigger.
2028c6fd2807SJeff Garzik 		 */
2029c6fd2807SJeff Garzik 		if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
2030c6fd2807SJeff Garzik 			err_mask = ata_dev_init_params(dev, id[3], id[6]);
2031c6fd2807SJeff Garzik 			if (err_mask) {
2032c6fd2807SJeff Garzik 				rc = -EIO;
2033c6fd2807SJeff Garzik 				reason = "INIT_DEV_PARAMS failed";
2034c6fd2807SJeff Garzik 				goto err_out;
2035c6fd2807SJeff Garzik 			}
2036c6fd2807SJeff Garzik 
2037c6fd2807SJeff Garzik 			/* current CHS translation info (id[53-58]) might be
2038c6fd2807SJeff Garzik 			 * changed. reread the identify device info.
2039c6fd2807SJeff Garzik 			 */
2040bff04647STejun Heo 			flags &= ~ATA_READID_POSTRESET;
2041c6fd2807SJeff Garzik 			goto retry;
2042c6fd2807SJeff Garzik 		}
2043c6fd2807SJeff Garzik 	}
2044c6fd2807SJeff Garzik 
2045c6fd2807SJeff Garzik 	*p_class = class;
2046c6fd2807SJeff Garzik 
2047c6fd2807SJeff Garzik 	return 0;
2048c6fd2807SJeff Garzik 
2049c6fd2807SJeff Garzik  err_out:
2050c6fd2807SJeff Garzik 	if (ata_msg_warn(ap))
2051a9a79dfeSJoe Perches 		ata_dev_warn(dev, "failed to IDENTIFY (%s, err_mask=0x%x)\n",
2052a9a79dfeSJoe Perches 			     reason, err_mask);
2053c6fd2807SJeff Garzik 	return rc;
2054c6fd2807SJeff Garzik }
2055c6fd2807SJeff Garzik 
20569062712fSTejun Heo static int ata_do_link_spd_horkage(struct ata_device *dev)
20579062712fSTejun Heo {
20589062712fSTejun Heo 	struct ata_link *plink = ata_dev_phys_link(dev);
20599062712fSTejun Heo 	u32 target, target_limit;
20609062712fSTejun Heo 
20619062712fSTejun Heo 	if (!sata_scr_valid(plink))
20629062712fSTejun Heo 		return 0;
20639062712fSTejun Heo 
20649062712fSTejun Heo 	if (dev->horkage & ATA_HORKAGE_1_5_GBPS)
20659062712fSTejun Heo 		target = 1;
20669062712fSTejun Heo 	else
20679062712fSTejun Heo 		return 0;
20689062712fSTejun Heo 
20699062712fSTejun Heo 	target_limit = (1 << target) - 1;
20709062712fSTejun Heo 
20719062712fSTejun Heo 	/* if already on stricter limit, no need to push further */
20729062712fSTejun Heo 	if (plink->sata_spd_limit <= target_limit)
20739062712fSTejun Heo 		return 0;
20749062712fSTejun Heo 
20759062712fSTejun Heo 	plink->sata_spd_limit = target_limit;
20769062712fSTejun Heo 
20779062712fSTejun Heo 	/* Request another EH round by returning -EAGAIN if link is
20789062712fSTejun Heo 	 * going faster than the target speed.  Forward progress is
20799062712fSTejun Heo 	 * guaranteed by setting sata_spd_limit to target_limit above.
20809062712fSTejun Heo 	 */
20819062712fSTejun Heo 	if (plink->sata_spd > target) {
2082a9a79dfeSJoe Perches 		ata_dev_info(dev, "applying link speed limit horkage to %s\n",
20839062712fSTejun Heo 			     sata_spd_string(target));
20849062712fSTejun Heo 		return -EAGAIN;
20859062712fSTejun Heo 	}
20869062712fSTejun Heo 	return 0;
20879062712fSTejun Heo }
20889062712fSTejun Heo 
2089c6fd2807SJeff Garzik static inline u8 ata_dev_knobble(struct ata_device *dev)
2090c6fd2807SJeff Garzik {
20919af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
20929ce8e307SJens Axboe 
20939ce8e307SJens Axboe 	if (ata_dev_blacklisted(dev) & ATA_HORKAGE_BRIDGE_OK)
20949ce8e307SJens Axboe 		return 0;
20959ce8e307SJens Axboe 
20969af5c9c9STejun Heo 	return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
2097c6fd2807SJeff Garzik }
2098c6fd2807SJeff Garzik 
2099388539f3SShaohua Li static int ata_dev_config_ncq(struct ata_device *dev,
2100c6fd2807SJeff Garzik 			       char *desc, size_t desc_sz)
2101c6fd2807SJeff Garzik {
21029af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
2103c6fd2807SJeff Garzik 	int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
2104388539f3SShaohua Li 	unsigned int err_mask;
2105388539f3SShaohua Li 	char *aa_desc = "";
2106c6fd2807SJeff Garzik 
2107c6fd2807SJeff Garzik 	if (!ata_id_has_ncq(dev->id)) {
2108c6fd2807SJeff Garzik 		desc[0] = '\0';
2109388539f3SShaohua Li 		return 0;
2110c6fd2807SJeff Garzik 	}
211175683fe7STejun Heo 	if (dev->horkage & ATA_HORKAGE_NONCQ) {
21126919a0a6SAlan Cox 		snprintf(desc, desc_sz, "NCQ (not used)");
2113388539f3SShaohua Li 		return 0;
21146919a0a6SAlan Cox 	}
2115c6fd2807SJeff Garzik 	if (ap->flags & ATA_FLAG_NCQ) {
2116cca3974eSJeff Garzik 		hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE - 1);
2117c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_NCQ;
2118c6fd2807SJeff Garzik 	}
2119c6fd2807SJeff Garzik 
2120388539f3SShaohua Li 	if (!(dev->horkage & ATA_HORKAGE_BROKEN_FPDMA_AA) &&
2121388539f3SShaohua Li 		(ap->flags & ATA_FLAG_FPDMA_AA) &&
2122388539f3SShaohua Li 		ata_id_has_fpdma_aa(dev->id)) {
2123388539f3SShaohua Li 		err_mask = ata_dev_set_feature(dev, SETFEATURES_SATA_ENABLE,
2124388539f3SShaohua Li 			SATA_FPDMA_AA);
2125388539f3SShaohua Li 		if (err_mask) {
2126a9a79dfeSJoe Perches 			ata_dev_err(dev,
2127a9a79dfeSJoe Perches 				    "failed to enable AA (error_mask=0x%x)\n",
2128a9a79dfeSJoe Perches 				    err_mask);
2129388539f3SShaohua Li 			if (err_mask != AC_ERR_DEV) {
2130388539f3SShaohua Li 				dev->horkage |= ATA_HORKAGE_BROKEN_FPDMA_AA;
2131388539f3SShaohua Li 				return -EIO;
2132388539f3SShaohua Li 			}
2133388539f3SShaohua Li 		} else
2134388539f3SShaohua Li 			aa_desc = ", AA";
2135388539f3SShaohua Li 	}
2136388539f3SShaohua Li 
2137c6fd2807SJeff Garzik 	if (hdepth >= ddepth)
2138388539f3SShaohua Li 		snprintf(desc, desc_sz, "NCQ (depth %d)%s", ddepth, aa_desc);
2139c6fd2807SJeff Garzik 	else
2140388539f3SShaohua Li 		snprintf(desc, desc_sz, "NCQ (depth %d/%d)%s", hdepth,
2141388539f3SShaohua Li 			ddepth, aa_desc);
2142ed36911cSMarc Carino 
2143ed36911cSMarc Carino 	if ((ap->flags & ATA_FLAG_FPDMA_AUX) &&
2144ed36911cSMarc Carino 	    ata_id_has_ncq_send_and_recv(dev->id)) {
2145ed36911cSMarc Carino 		err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_SEND_RECV,
2146ed36911cSMarc Carino 					     0, ap->sector_buf, 1);
2147ed36911cSMarc Carino 		if (err_mask) {
2148ed36911cSMarc Carino 			ata_dev_dbg(dev,
2149ed36911cSMarc Carino 				    "failed to get NCQ Send/Recv Log Emask 0x%x\n",
2150ed36911cSMarc Carino 				    err_mask);
2151ed36911cSMarc Carino 		} else {
2152f78dea06SMarc Carino 			u8 *cmds = dev->ncq_send_recv_cmds;
2153f78dea06SMarc Carino 
2154ed36911cSMarc Carino 			dev->flags |= ATA_DFLAG_NCQ_SEND_RECV;
2155f78dea06SMarc Carino 			memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_SEND_RECV_SIZE);
2156f78dea06SMarc Carino 
2157f78dea06SMarc Carino 			if (dev->horkage & ATA_HORKAGE_NO_NCQ_TRIM) {
2158f78dea06SMarc Carino 				ata_dev_dbg(dev, "disabling queued TRIM support\n");
2159f78dea06SMarc Carino 				cmds[ATA_LOG_NCQ_SEND_RECV_DSM_OFFSET] &=
2160f78dea06SMarc Carino 					~ATA_LOG_NCQ_SEND_RECV_DSM_TRIM;
2161f78dea06SMarc Carino 			}
2162ed36911cSMarc Carino 		}
2163ed36911cSMarc Carino 	}
2164ed36911cSMarc Carino 
2165388539f3SShaohua Li 	return 0;
2166c6fd2807SJeff Garzik }
2167c6fd2807SJeff Garzik 
2168c6fd2807SJeff Garzik /**
2169c6fd2807SJeff Garzik  *	ata_dev_configure - Configure the specified ATA/ATAPI device
2170c6fd2807SJeff Garzik  *	@dev: Target device to configure
2171c6fd2807SJeff Garzik  *
2172c6fd2807SJeff Garzik  *	Configure @dev according to @dev->id.  Generic and low-level
2173c6fd2807SJeff Garzik  *	driver specific fixups are also applied.
2174c6fd2807SJeff Garzik  *
2175c6fd2807SJeff Garzik  *	LOCKING:
2176c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
2177c6fd2807SJeff Garzik  *
2178c6fd2807SJeff Garzik  *	RETURNS:
2179c6fd2807SJeff Garzik  *	0 on success, -errno otherwise
2180c6fd2807SJeff Garzik  */
2181efdaedc4STejun Heo int ata_dev_configure(struct ata_device *dev)
2182c6fd2807SJeff Garzik {
21839af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
21849af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
21856746544cSTejun Heo 	int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
2186c6fd2807SJeff Garzik 	const u16 *id = dev->id;
21877dc951aeSTejun Heo 	unsigned long xfer_mask;
218865fe1f0fSShane Huang 	unsigned int err_mask;
2189b352e57dSAlan Cox 	char revbuf[7];		/* XYZ-99\0 */
21903f64f565SEric D. Mudama 	char fwrevbuf[ATA_ID_FW_REV_LEN+1];
21913f64f565SEric D. Mudama 	char modelbuf[ATA_ID_PROD_LEN+1];
2192c6fd2807SJeff Garzik 	int rc;
2193c6fd2807SJeff Garzik 
2194c6fd2807SJeff Garzik 	if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
2195a9a79dfeSJoe Perches 		ata_dev_info(dev, "%s: ENTER/EXIT -- nodev\n", __func__);
2196c6fd2807SJeff Garzik 		return 0;
2197c6fd2807SJeff Garzik 	}
2198c6fd2807SJeff Garzik 
2199c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2200a9a79dfeSJoe Perches 		ata_dev_dbg(dev, "%s: ENTER\n", __func__);
2201c6fd2807SJeff Garzik 
220275683fe7STejun Heo 	/* set horkage */
220375683fe7STejun Heo 	dev->horkage |= ata_dev_blacklisted(dev);
220433267325STejun Heo 	ata_force_horkage(dev);
220575683fe7STejun Heo 
220650af2fa1STejun Heo 	if (dev->horkage & ATA_HORKAGE_DISABLE) {
2207a9a79dfeSJoe Perches 		ata_dev_info(dev, "unsupported device, disabling\n");
220850af2fa1STejun Heo 		ata_dev_disable(dev);
220950af2fa1STejun Heo 		return 0;
221050af2fa1STejun Heo 	}
221150af2fa1STejun Heo 
22122486fa56STejun Heo 	if ((!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) &&
22132486fa56STejun Heo 	    dev->class == ATA_DEV_ATAPI) {
2214a9a79dfeSJoe Perches 		ata_dev_warn(dev, "WARNING: ATAPI is %s, device ignored\n",
22152486fa56STejun Heo 			     atapi_enabled ? "not supported with this driver"
22162486fa56STejun Heo 			     : "disabled");
22172486fa56STejun Heo 		ata_dev_disable(dev);
22182486fa56STejun Heo 		return 0;
22192486fa56STejun Heo 	}
22202486fa56STejun Heo 
22219062712fSTejun Heo 	rc = ata_do_link_spd_horkage(dev);
22229062712fSTejun Heo 	if (rc)
22239062712fSTejun Heo 		return rc;
22249062712fSTejun Heo 
2225ecd75ad5STejun Heo 	/* some WD SATA-1 drives have issues with LPM, turn on NOLPM for them */
2226ecd75ad5STejun Heo 	if ((dev->horkage & ATA_HORKAGE_WD_BROKEN_LPM) &&
2227ecd75ad5STejun Heo 	    (id[ATA_ID_SATA_CAPABILITY] & 0xe) == 0x2)
2228ecd75ad5STejun Heo 		dev->horkage |= ATA_HORKAGE_NOLPM;
2229ecd75ad5STejun Heo 
2230ecd75ad5STejun Heo 	if (dev->horkage & ATA_HORKAGE_NOLPM) {
2231ecd75ad5STejun Heo 		ata_dev_warn(dev, "LPM support broken, forcing max_power\n");
2232ecd75ad5STejun Heo 		dev->link->ap->target_lpm_policy = ATA_LPM_MAX_POWER;
2233ecd75ad5STejun Heo 	}
2234ecd75ad5STejun Heo 
22356746544cSTejun Heo 	/* let ACPI work its magic */
22366746544cSTejun Heo 	rc = ata_acpi_on_devcfg(dev);
22376746544cSTejun Heo 	if (rc)
22386746544cSTejun Heo 		return rc;
223908573a86SKristen Carlson Accardi 
224005027adcSTejun Heo 	/* massage HPA, do it early as it might change IDENTIFY data */
224105027adcSTejun Heo 	rc = ata_hpa_resize(dev);
224205027adcSTejun Heo 	if (rc)
224305027adcSTejun Heo 		return rc;
224405027adcSTejun Heo 
2245c6fd2807SJeff Garzik 	/* print device capabilities */
2246c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2247a9a79dfeSJoe Perches 		ata_dev_dbg(dev,
2248c6fd2807SJeff Garzik 			    "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
2249c6fd2807SJeff Garzik 			    "85:%04x 86:%04x 87:%04x 88:%04x\n",
22507f5e4e8dSHarvey Harrison 			    __func__,
2251c6fd2807SJeff Garzik 			    id[49], id[82], id[83], id[84],
2252c6fd2807SJeff Garzik 			    id[85], id[86], id[87], id[88]);
2253c6fd2807SJeff Garzik 
2254c6fd2807SJeff Garzik 	/* initialize to-be-configured parameters */
2255c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_CFG_MASK;
2256c6fd2807SJeff Garzik 	dev->max_sectors = 0;
2257c6fd2807SJeff Garzik 	dev->cdb_len = 0;
2258c6fd2807SJeff Garzik 	dev->n_sectors = 0;
2259c6fd2807SJeff Garzik 	dev->cylinders = 0;
2260c6fd2807SJeff Garzik 	dev->heads = 0;
2261c6fd2807SJeff Garzik 	dev->sectors = 0;
2262e18086d6SMark Lord 	dev->multi_count = 0;
2263c6fd2807SJeff Garzik 
2264c6fd2807SJeff Garzik 	/*
2265c6fd2807SJeff Garzik 	 * common ATA, ATAPI feature tests
2266c6fd2807SJeff Garzik 	 */
2267c6fd2807SJeff Garzik 
2268c6fd2807SJeff Garzik 	/* find max transfer mode; for printk only */
2269c6fd2807SJeff Garzik 	xfer_mask = ata_id_xfermask(id);
2270c6fd2807SJeff Garzik 
2271c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2272c6fd2807SJeff Garzik 		ata_dump_id(id);
2273c6fd2807SJeff Garzik 
2274ef143d57SAlbert Lee 	/* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
2275ef143d57SAlbert Lee 	ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
2276ef143d57SAlbert Lee 			sizeof(fwrevbuf));
2277ef143d57SAlbert Lee 
2278ef143d57SAlbert Lee 	ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
2279ef143d57SAlbert Lee 			sizeof(modelbuf));
2280ef143d57SAlbert Lee 
2281c6fd2807SJeff Garzik 	/* ATA-specific feature tests */
2282c6fd2807SJeff Garzik 	if (dev->class == ATA_DEV_ATA) {
2283b352e57dSAlan Cox 		if (ata_id_is_cfa(id)) {
228462afe5d7SSergei Shtylyov 			/* CPRM may make this media unusable */
228562afe5d7SSergei Shtylyov 			if (id[ATA_ID_CFA_KEY_MGMT] & 1)
2286a9a79dfeSJoe Perches 				ata_dev_warn(dev,
2287a9a79dfeSJoe Perches 	"supports DRM functions and may not be fully accessible\n");
2288b352e57dSAlan Cox 			snprintf(revbuf, 7, "CFA");
2289ae8d4ee7SAlan Cox 		} else {
2290b352e57dSAlan Cox 			snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
2291ae8d4ee7SAlan Cox 			/* Warn the user if the device has TPM extensions */
2292ae8d4ee7SAlan Cox 			if (ata_id_has_tpm(id))
2293a9a79dfeSJoe Perches 				ata_dev_warn(dev,
2294a9a79dfeSJoe Perches 	"supports DRM functions and may not be fully accessible\n");
2295ae8d4ee7SAlan Cox 		}
2296b352e57dSAlan Cox 
2297c6fd2807SJeff Garzik 		dev->n_sectors = ata_id_n_sectors(id);
2298c6fd2807SJeff Garzik 
2299e18086d6SMark Lord 		/* get current R/W Multiple count setting */
2300e18086d6SMark Lord 		if ((dev->id[47] >> 8) == 0x80 && (dev->id[59] & 0x100)) {
2301e18086d6SMark Lord 			unsigned int max = dev->id[47] & 0xff;
2302e18086d6SMark Lord 			unsigned int cnt = dev->id[59] & 0xff;
2303e18086d6SMark Lord 			/* only recognize/allow powers of two here */
2304e18086d6SMark Lord 			if (is_power_of_2(max) && is_power_of_2(cnt))
2305e18086d6SMark Lord 				if (cnt <= max)
2306e18086d6SMark Lord 					dev->multi_count = cnt;
2307e18086d6SMark Lord 		}
23083f64f565SEric D. Mudama 
2309c6fd2807SJeff Garzik 		if (ata_id_has_lba(id)) {
2310c6fd2807SJeff Garzik 			const char *lba_desc;
2311388539f3SShaohua Li 			char ncq_desc[24];
2312c6fd2807SJeff Garzik 
2313c6fd2807SJeff Garzik 			lba_desc = "LBA";
2314c6fd2807SJeff Garzik 			dev->flags |= ATA_DFLAG_LBA;
2315c6fd2807SJeff Garzik 			if (ata_id_has_lba48(id)) {
2316c6fd2807SJeff Garzik 				dev->flags |= ATA_DFLAG_LBA48;
2317c6fd2807SJeff Garzik 				lba_desc = "LBA48";
23186fc49adbSTejun Heo 
23196fc49adbSTejun Heo 				if (dev->n_sectors >= (1UL << 28) &&
23206fc49adbSTejun Heo 				    ata_id_has_flush_ext(id))
23216fc49adbSTejun Heo 					dev->flags |= ATA_DFLAG_FLUSH_EXT;
2322c6fd2807SJeff Garzik 			}
2323c6fd2807SJeff Garzik 
2324c6fd2807SJeff Garzik 			/* config NCQ */
2325388539f3SShaohua Li 			rc = ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
2326388539f3SShaohua Li 			if (rc)
2327388539f3SShaohua Li 				return rc;
2328c6fd2807SJeff Garzik 
2329c6fd2807SJeff Garzik 			/* print device info to dmesg */
23303f64f565SEric D. Mudama 			if (ata_msg_drv(ap) && print_info) {
2331a9a79dfeSJoe Perches 				ata_dev_info(dev, "%s: %s, %s, max %s\n",
23323f64f565SEric D. Mudama 					     revbuf, modelbuf, fwrevbuf,
23333f64f565SEric D. Mudama 					     ata_mode_string(xfer_mask));
2334a9a79dfeSJoe Perches 				ata_dev_info(dev,
2335a9a79dfeSJoe Perches 					     "%llu sectors, multi %u: %s %s\n",
2336c6fd2807SJeff Garzik 					(unsigned long long)dev->n_sectors,
23373f64f565SEric D. Mudama 					dev->multi_count, lba_desc, ncq_desc);
23383f64f565SEric D. Mudama 			}
2339c6fd2807SJeff Garzik 		} else {
2340c6fd2807SJeff Garzik 			/* CHS */
2341c6fd2807SJeff Garzik 
2342c6fd2807SJeff Garzik 			/* Default translation */
2343c6fd2807SJeff Garzik 			dev->cylinders	= id[1];
2344c6fd2807SJeff Garzik 			dev->heads	= id[3];
2345c6fd2807SJeff Garzik 			dev->sectors	= id[6];
2346c6fd2807SJeff Garzik 
2347c6fd2807SJeff Garzik 			if (ata_id_current_chs_valid(id)) {
2348c6fd2807SJeff Garzik 				/* Current CHS translation is valid. */
2349c6fd2807SJeff Garzik 				dev->cylinders = id[54];
2350c6fd2807SJeff Garzik 				dev->heads     = id[55];
2351c6fd2807SJeff Garzik 				dev->sectors   = id[56];
2352c6fd2807SJeff Garzik 			}
2353c6fd2807SJeff Garzik 
2354c6fd2807SJeff Garzik 			/* print device info to dmesg */
23553f64f565SEric D. Mudama 			if (ata_msg_drv(ap) && print_info) {
2356a9a79dfeSJoe Perches 				ata_dev_info(dev, "%s: %s, %s, max %s\n",
23573f64f565SEric D. Mudama 					     revbuf,	modelbuf, fwrevbuf,
23583f64f565SEric D. Mudama 					     ata_mode_string(xfer_mask));
2359a9a79dfeSJoe Perches 				ata_dev_info(dev,
2360a9a79dfeSJoe Perches 					     "%llu sectors, multi %u, CHS %u/%u/%u\n",
23613f64f565SEric D. Mudama 					     (unsigned long long)dev->n_sectors,
23623f64f565SEric D. Mudama 					     dev->multi_count, dev->cylinders,
23633f64f565SEric D. Mudama 					     dev->heads, dev->sectors);
23643f64f565SEric D. Mudama 			}
2365c6fd2807SJeff Garzik 		}
2366c6fd2807SJeff Garzik 
2367803739d2SShane Huang 		/* Check and mark DevSlp capability. Get DevSlp timing variables
2368803739d2SShane Huang 		 * from SATA Settings page of Identify Device Data Log.
236965fe1f0fSShane Huang 		 */
2370803739d2SShane Huang 		if (ata_id_has_devslp(dev->id)) {
23718e725c7fSDavid Woodhouse 			u8 *sata_setting = ap->sector_buf;
2372803739d2SShane Huang 			int i, j;
2373803739d2SShane Huang 
2374803739d2SShane Huang 			dev->flags |= ATA_DFLAG_DEVSLP;
237565fe1f0fSShane Huang 			err_mask = ata_read_log_page(dev,
237665fe1f0fSShane Huang 						     ATA_LOG_SATA_ID_DEV_DATA,
237765fe1f0fSShane Huang 						     ATA_LOG_SATA_SETTINGS,
2378803739d2SShane Huang 						     sata_setting,
237965fe1f0fSShane Huang 						     1);
238065fe1f0fSShane Huang 			if (err_mask)
238165fe1f0fSShane Huang 				ata_dev_dbg(dev,
238265fe1f0fSShane Huang 					    "failed to get Identify Device Data, Emask 0x%x\n",
238365fe1f0fSShane Huang 					    err_mask);
2384803739d2SShane Huang 			else
2385803739d2SShane Huang 				for (i = 0; i < ATA_LOG_DEVSLP_SIZE; i++) {
2386803739d2SShane Huang 					j = ATA_LOG_DEVSLP_OFFSET + i;
2387803739d2SShane Huang 					dev->devslp_timing[i] = sata_setting[j];
2388803739d2SShane Huang 				}
238965fe1f0fSShane Huang 		}
239065fe1f0fSShane Huang 
2391c6fd2807SJeff Garzik 		dev->cdb_len = 16;
2392c6fd2807SJeff Garzik 	}
2393c6fd2807SJeff Garzik 
2394c6fd2807SJeff Garzik 	/* ATAPI-specific feature tests */
2395c6fd2807SJeff Garzik 	else if (dev->class == ATA_DEV_ATAPI) {
2396854c73a2STejun Heo 		const char *cdb_intr_string = "";
2397854c73a2STejun Heo 		const char *atapi_an_string = "";
239891163006STejun Heo 		const char *dma_dir_string = "";
23997d77b247STejun Heo 		u32 sntf;
2400c6fd2807SJeff Garzik 
2401c6fd2807SJeff Garzik 		rc = atapi_cdb_len(id);
2402c6fd2807SJeff Garzik 		if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
2403c6fd2807SJeff Garzik 			if (ata_msg_warn(ap))
2404a9a79dfeSJoe Perches 				ata_dev_warn(dev, "unsupported CDB len\n");
2405c6fd2807SJeff Garzik 			rc = -EINVAL;
2406c6fd2807SJeff Garzik 			goto err_out_nosup;
2407c6fd2807SJeff Garzik 		}
2408c6fd2807SJeff Garzik 		dev->cdb_len = (unsigned int) rc;
2409c6fd2807SJeff Garzik 
24107d77b247STejun Heo 		/* Enable ATAPI AN if both the host and device have
24117d77b247STejun Heo 		 * the support.  If PMP is attached, SNTF is required
24127d77b247STejun Heo 		 * to enable ATAPI AN to discern between PHY status
24137d77b247STejun Heo 		 * changed notifications and ATAPI ANs.
24149f45cbd3SKristen Carlson Accardi 		 */
2415e7ecd435STejun Heo 		if (atapi_an &&
2416e7ecd435STejun Heo 		    (ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
2417071f44b1STejun Heo 		    (!sata_pmp_attached(ap) ||
24187d77b247STejun Heo 		     sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
24199f45cbd3SKristen Carlson Accardi 			/* issue SET feature command to turn this on */
2420218f3d30SJeff Garzik 			err_mask = ata_dev_set_feature(dev,
2421218f3d30SJeff Garzik 					SETFEATURES_SATA_ENABLE, SATA_AN);
2422854c73a2STejun Heo 			if (err_mask)
2423a9a79dfeSJoe Perches 				ata_dev_err(dev,
2424a9a79dfeSJoe Perches 					    "failed to enable ATAPI AN (err_mask=0x%x)\n",
2425a9a79dfeSJoe Perches 					    err_mask);
2426854c73a2STejun Heo 			else {
24279f45cbd3SKristen Carlson Accardi 				dev->flags |= ATA_DFLAG_AN;
2428854c73a2STejun Heo 				atapi_an_string = ", ATAPI AN";
2429854c73a2STejun Heo 			}
24309f45cbd3SKristen Carlson Accardi 		}
24319f45cbd3SKristen Carlson Accardi 
2432c6fd2807SJeff Garzik 		if (ata_id_cdb_intr(dev->id)) {
2433c6fd2807SJeff Garzik 			dev->flags |= ATA_DFLAG_CDB_INTR;
2434c6fd2807SJeff Garzik 			cdb_intr_string = ", CDB intr";
2435c6fd2807SJeff Garzik 		}
2436c6fd2807SJeff Garzik 
2437966fbe19SVincent Pelletier 		if (atapi_dmadir || (dev->horkage & ATA_HORKAGE_ATAPI_DMADIR) || atapi_id_dmadir(dev->id)) {
243891163006STejun Heo 			dev->flags |= ATA_DFLAG_DMADIR;
243991163006STejun Heo 			dma_dir_string = ", DMADIR";
244091163006STejun Heo 		}
244191163006STejun Heo 
2442afe75951SAaron Lu 		if (ata_id_has_da(dev->id)) {
2443b1354cbbSLin Ming 			dev->flags |= ATA_DFLAG_DA;
2444afe75951SAaron Lu 			zpodd_init(dev);
2445afe75951SAaron Lu 		}
2446b1354cbbSLin Ming 
2447c6fd2807SJeff Garzik 		/* print device info to dmesg */
2448c6fd2807SJeff Garzik 		if (ata_msg_drv(ap) && print_info)
2449a9a79dfeSJoe Perches 			ata_dev_info(dev,
245091163006STejun Heo 				     "ATAPI: %s, %s, max %s%s%s%s\n",
2451ef143d57SAlbert Lee 				     modelbuf, fwrevbuf,
2452c6fd2807SJeff Garzik 				     ata_mode_string(xfer_mask),
245391163006STejun Heo 				     cdb_intr_string, atapi_an_string,
245491163006STejun Heo 				     dma_dir_string);
2455c6fd2807SJeff Garzik 	}
2456c6fd2807SJeff Garzik 
2457914ed354STejun Heo 	/* determine max_sectors */
2458914ed354STejun Heo 	dev->max_sectors = ATA_MAX_SECTORS;
2459914ed354STejun Heo 	if (dev->flags & ATA_DFLAG_LBA48)
2460914ed354STejun Heo 		dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2461914ed354STejun Heo 
2462c5038fc0SAlan Cox 	/* Limit PATA drive on SATA cable bridge transfers to udma5,
2463c5038fc0SAlan Cox 	   200 sectors */
2464c6fd2807SJeff Garzik 	if (ata_dev_knobble(dev)) {
2465c6fd2807SJeff Garzik 		if (ata_msg_drv(ap) && print_info)
2466a9a79dfeSJoe Perches 			ata_dev_info(dev, "applying bridge limits\n");
2467c6fd2807SJeff Garzik 		dev->udma_mask &= ATA_UDMA5;
2468c6fd2807SJeff Garzik 		dev->max_sectors = ATA_MAX_SECTORS;
2469c6fd2807SJeff Garzik 	}
2470c6fd2807SJeff Garzik 
2471f8d8e579STony Battersby 	if ((dev->class == ATA_DEV_ATAPI) &&
2472f442cd86SAlbert Lee 	    (atapi_command_packet_set(id) == TYPE_TAPE)) {
2473f8d8e579STony Battersby 		dev->max_sectors = ATA_MAX_SECTORS_TAPE;
2474f442cd86SAlbert Lee 		dev->horkage |= ATA_HORKAGE_STUCK_ERR;
2475f442cd86SAlbert Lee 	}
2476f8d8e579STony Battersby 
247775683fe7STejun Heo 	if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
247803ec52deSTejun Heo 		dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
247903ec52deSTejun Heo 					 dev->max_sectors);
248018d6e9d5SAlbert Lee 
2481a32450e1SShan Hai 	if (dev->horkage & ATA_HORKAGE_MAX_SEC_LBA48)
2482a32450e1SShan Hai 		dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2483a32450e1SShan Hai 
2484c6fd2807SJeff Garzik 	if (ap->ops->dev_config)
2485cd0d3bbcSAlan 		ap->ops->dev_config(dev);
2486c6fd2807SJeff Garzik 
2487c5038fc0SAlan Cox 	if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2488c5038fc0SAlan Cox 		/* Let the user know. We don't want to disallow opens for
2489c5038fc0SAlan Cox 		   rescue purposes, or in case the vendor is just a blithering
2490c5038fc0SAlan Cox 		   idiot. Do this after the dev_config call as some controllers
2491c5038fc0SAlan Cox 		   with buggy firmware may want to avoid reporting false device
2492c5038fc0SAlan Cox 		   bugs */
2493c5038fc0SAlan Cox 
2494c5038fc0SAlan Cox 		if (print_info) {
2495a9a79dfeSJoe Perches 			ata_dev_warn(dev,
2496c5038fc0SAlan Cox "Drive reports diagnostics failure. This may indicate a drive\n");
2497a9a79dfeSJoe Perches 			ata_dev_warn(dev,
2498c5038fc0SAlan Cox "fault or invalid emulation. Contact drive vendor for information.\n");
2499c5038fc0SAlan Cox 		}
2500c5038fc0SAlan Cox 	}
2501c5038fc0SAlan Cox 
2502ac70a964STejun Heo 	if ((dev->horkage & ATA_HORKAGE_FIRMWARE_WARN) && print_info) {
2503a9a79dfeSJoe Perches 		ata_dev_warn(dev, "WARNING: device requires firmware update to be fully functional\n");
2504a9a79dfeSJoe Perches 		ata_dev_warn(dev, "         contact the vendor or visit http://ata.wiki.kernel.org\n");
2505ac70a964STejun Heo 	}
2506ac70a964STejun Heo 
2507c6fd2807SJeff Garzik 	return 0;
2508c6fd2807SJeff Garzik 
2509c6fd2807SJeff Garzik err_out_nosup:
2510c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2511a9a79dfeSJoe Perches 		ata_dev_dbg(dev, "%s: EXIT, err\n", __func__);
2512c6fd2807SJeff Garzik 	return rc;
2513c6fd2807SJeff Garzik }
2514c6fd2807SJeff Garzik 
2515c6fd2807SJeff Garzik /**
25162e41e8e6SAlan Cox  *	ata_cable_40wire	-	return 40 wire cable type
2517be0d18dfSAlan Cox  *	@ap: port
2518be0d18dfSAlan Cox  *
25192e41e8e6SAlan Cox  *	Helper method for drivers which want to hardwire 40 wire cable
2520be0d18dfSAlan Cox  *	detection.
2521be0d18dfSAlan Cox  */
2522be0d18dfSAlan Cox 
2523be0d18dfSAlan Cox int ata_cable_40wire(struct ata_port *ap)
2524be0d18dfSAlan Cox {
2525be0d18dfSAlan Cox 	return ATA_CBL_PATA40;
2526be0d18dfSAlan Cox }
2527be0d18dfSAlan Cox 
2528be0d18dfSAlan Cox /**
25292e41e8e6SAlan Cox  *	ata_cable_80wire	-	return 80 wire cable type
2530be0d18dfSAlan Cox  *	@ap: port
2531be0d18dfSAlan Cox  *
25322e41e8e6SAlan Cox  *	Helper method for drivers which want to hardwire 80 wire cable
2533be0d18dfSAlan Cox  *	detection.
2534be0d18dfSAlan Cox  */
2535be0d18dfSAlan Cox 
2536be0d18dfSAlan Cox int ata_cable_80wire(struct ata_port *ap)
2537be0d18dfSAlan Cox {
2538be0d18dfSAlan Cox 	return ATA_CBL_PATA80;
2539be0d18dfSAlan Cox }
2540be0d18dfSAlan Cox 
2541be0d18dfSAlan Cox /**
2542be0d18dfSAlan Cox  *	ata_cable_unknown	-	return unknown PATA cable.
2543be0d18dfSAlan Cox  *	@ap: port
2544be0d18dfSAlan Cox  *
2545be0d18dfSAlan Cox  *	Helper method for drivers which have no PATA cable detection.
2546be0d18dfSAlan Cox  */
2547be0d18dfSAlan Cox 
2548be0d18dfSAlan Cox int ata_cable_unknown(struct ata_port *ap)
2549be0d18dfSAlan Cox {
2550be0d18dfSAlan Cox 	return ATA_CBL_PATA_UNK;
2551be0d18dfSAlan Cox }
2552be0d18dfSAlan Cox 
2553be0d18dfSAlan Cox /**
2554c88f90c3STejun Heo  *	ata_cable_ignore	-	return ignored PATA cable.
2555c88f90c3STejun Heo  *	@ap: port
2556c88f90c3STejun Heo  *
2557c88f90c3STejun Heo  *	Helper method for drivers which don't use cable type to limit
2558c88f90c3STejun Heo  *	transfer mode.
2559c88f90c3STejun Heo  */
2560c88f90c3STejun Heo int ata_cable_ignore(struct ata_port *ap)
2561c88f90c3STejun Heo {
2562c88f90c3STejun Heo 	return ATA_CBL_PATA_IGN;
2563c88f90c3STejun Heo }
2564c88f90c3STejun Heo 
2565c88f90c3STejun Heo /**
2566be0d18dfSAlan Cox  *	ata_cable_sata	-	return SATA cable type
2567be0d18dfSAlan Cox  *	@ap: port
2568be0d18dfSAlan Cox  *
2569be0d18dfSAlan Cox  *	Helper method for drivers which have SATA cables
2570be0d18dfSAlan Cox  */
2571be0d18dfSAlan Cox 
2572be0d18dfSAlan Cox int ata_cable_sata(struct ata_port *ap)
2573be0d18dfSAlan Cox {
2574be0d18dfSAlan Cox 	return ATA_CBL_SATA;
2575be0d18dfSAlan Cox }
2576be0d18dfSAlan Cox 
2577be0d18dfSAlan Cox /**
2578c6fd2807SJeff Garzik  *	ata_bus_probe - Reset and probe ATA bus
2579c6fd2807SJeff Garzik  *	@ap: Bus to probe
2580c6fd2807SJeff Garzik  *
2581c6fd2807SJeff Garzik  *	Master ATA bus probing function.  Initiates a hardware-dependent
2582c6fd2807SJeff Garzik  *	bus reset, then attempts to identify any devices found on
2583c6fd2807SJeff Garzik  *	the bus.
2584c6fd2807SJeff Garzik  *
2585c6fd2807SJeff Garzik  *	LOCKING:
2586c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
2587c6fd2807SJeff Garzik  *
2588c6fd2807SJeff Garzik  *	RETURNS:
2589c6fd2807SJeff Garzik  *	Zero on success, negative errno otherwise.
2590c6fd2807SJeff Garzik  */
2591c6fd2807SJeff Garzik 
2592c6fd2807SJeff Garzik int ata_bus_probe(struct ata_port *ap)
2593c6fd2807SJeff Garzik {
2594c6fd2807SJeff Garzik 	unsigned int classes[ATA_MAX_DEVICES];
2595c6fd2807SJeff Garzik 	int tries[ATA_MAX_DEVICES];
2596f58229f8STejun Heo 	int rc;
2597c6fd2807SJeff Garzik 	struct ata_device *dev;
2598c6fd2807SJeff Garzik 
25991eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL)
2600f58229f8STejun Heo 		tries[dev->devno] = ATA_PROBE_MAX_TRIES;
2601c6fd2807SJeff Garzik 
2602c6fd2807SJeff Garzik  retry:
26031eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL) {
2604cdeab114STejun Heo 		/* If we issue an SRST then an ATA drive (not ATAPI)
2605cdeab114STejun Heo 		 * may change configuration and be in PIO0 timing. If
2606cdeab114STejun Heo 		 * we do a hard reset (or are coming from power on)
2607cdeab114STejun Heo 		 * this is true for ATA or ATAPI. Until we've set a
2608cdeab114STejun Heo 		 * suitable controller mode we should not touch the
2609cdeab114STejun Heo 		 * bus as we may be talking too fast.
2610cdeab114STejun Heo 		 */
2611cdeab114STejun Heo 		dev->pio_mode = XFER_PIO_0;
26125416912aSAaron Lu 		dev->dma_mode = 0xff;
2613cdeab114STejun Heo 
2614cdeab114STejun Heo 		/* If the controller has a pio mode setup function
2615cdeab114STejun Heo 		 * then use it to set the chipset to rights. Don't
2616cdeab114STejun Heo 		 * touch the DMA setup as that will be dealt with when
2617cdeab114STejun Heo 		 * configuring devices.
2618cdeab114STejun Heo 		 */
2619cdeab114STejun Heo 		if (ap->ops->set_piomode)
2620cdeab114STejun Heo 			ap->ops->set_piomode(ap, dev);
2621cdeab114STejun Heo 	}
2622cdeab114STejun Heo 
2623c6fd2807SJeff Garzik 	/* reset and determine device classes */
2624c6fd2807SJeff Garzik 	ap->ops->phy_reset(ap);
2625c6fd2807SJeff Garzik 
26261eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL) {
26273e4ec344STejun Heo 		if (dev->class != ATA_DEV_UNKNOWN)
2628c6fd2807SJeff Garzik 			classes[dev->devno] = dev->class;
2629c6fd2807SJeff Garzik 		else
2630c6fd2807SJeff Garzik 			classes[dev->devno] = ATA_DEV_NONE;
2631c6fd2807SJeff Garzik 
2632c6fd2807SJeff Garzik 		dev->class = ATA_DEV_UNKNOWN;
2633c6fd2807SJeff Garzik 	}
2634c6fd2807SJeff Garzik 
2635f31f0cc2SJeff Garzik 	/* read IDENTIFY page and configure devices. We have to do the identify
2636f31f0cc2SJeff Garzik 	   specific sequence bass-ackwards so that PDIAG- is released by
2637f31f0cc2SJeff Garzik 	   the slave device */
2638f31f0cc2SJeff Garzik 
26391eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL_REVERSE) {
2640f58229f8STejun Heo 		if (tries[dev->devno])
2641f58229f8STejun Heo 			dev->class = classes[dev->devno];
2642c6fd2807SJeff Garzik 
2643c6fd2807SJeff Garzik 		if (!ata_dev_enabled(dev))
2644c6fd2807SJeff Garzik 			continue;
2645c6fd2807SJeff Garzik 
2646bff04647STejun Heo 		rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2647bff04647STejun Heo 				     dev->id);
2648c6fd2807SJeff Garzik 		if (rc)
2649c6fd2807SJeff Garzik 			goto fail;
2650f31f0cc2SJeff Garzik 	}
2651f31f0cc2SJeff Garzik 
2652be0d18dfSAlan Cox 	/* Now ask for the cable type as PDIAG- should have been released */
2653be0d18dfSAlan Cox 	if (ap->ops->cable_detect)
2654be0d18dfSAlan Cox 		ap->cbl = ap->ops->cable_detect(ap);
2655be0d18dfSAlan Cox 
26561eca4365STejun Heo 	/* We may have SATA bridge glue hiding here irrespective of
26571eca4365STejun Heo 	 * the reported cable types and sensed types.  When SATA
26581eca4365STejun Heo 	 * drives indicate we have a bridge, we don't know which end
26591eca4365STejun Heo 	 * of the link the bridge is which is a problem.
26601eca4365STejun Heo 	 */
26611eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED)
2662614fe29bSAlan Cox 		if (ata_id_is_sata(dev->id))
2663614fe29bSAlan Cox 			ap->cbl = ATA_CBL_SATA;
2664614fe29bSAlan Cox 
2665f31f0cc2SJeff Garzik 	/* After the identify sequence we can now set up the devices. We do
2666f31f0cc2SJeff Garzik 	   this in the normal order so that the user doesn't get confused */
2667f31f0cc2SJeff Garzik 
26681eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED) {
26699af5c9c9STejun Heo 		ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
2670efdaedc4STejun Heo 		rc = ata_dev_configure(dev);
26719af5c9c9STejun Heo 		ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
2672c6fd2807SJeff Garzik 		if (rc)
2673c6fd2807SJeff Garzik 			goto fail;
2674c6fd2807SJeff Garzik 	}
2675c6fd2807SJeff Garzik 
2676c6fd2807SJeff Garzik 	/* configure transfer mode */
26770260731fSTejun Heo 	rc = ata_set_mode(&ap->link, &dev);
26784ae72a1eSTejun Heo 	if (rc)
2679c6fd2807SJeff Garzik 		goto fail;
2680c6fd2807SJeff Garzik 
26811eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED)
2682c6fd2807SJeff Garzik 		return 0;
2683c6fd2807SJeff Garzik 
2684c6fd2807SJeff Garzik 	return -ENODEV;
2685c6fd2807SJeff Garzik 
2686c6fd2807SJeff Garzik  fail:
26874ae72a1eSTejun Heo 	tries[dev->devno]--;
26884ae72a1eSTejun Heo 
2689c6fd2807SJeff Garzik 	switch (rc) {
2690c6fd2807SJeff Garzik 	case -EINVAL:
26914ae72a1eSTejun Heo 		/* eeek, something went very wrong, give up */
2692c6fd2807SJeff Garzik 		tries[dev->devno] = 0;
2693c6fd2807SJeff Garzik 		break;
26944ae72a1eSTejun Heo 
26954ae72a1eSTejun Heo 	case -ENODEV:
26964ae72a1eSTejun Heo 		/* give it just one more chance */
26974ae72a1eSTejun Heo 		tries[dev->devno] = min(tries[dev->devno], 1);
2698c6fd2807SJeff Garzik 	case -EIO:
26994ae72a1eSTejun Heo 		if (tries[dev->devno] == 1) {
27004ae72a1eSTejun Heo 			/* This is the last chance, better to slow
27014ae72a1eSTejun Heo 			 * down than lose it.
27024ae72a1eSTejun Heo 			 */
2703a07d499bSTejun Heo 			sata_down_spd_limit(&ap->link, 0);
27044ae72a1eSTejun Heo 			ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
27054ae72a1eSTejun Heo 		}
2706c6fd2807SJeff Garzik 	}
2707c6fd2807SJeff Garzik 
27084ae72a1eSTejun Heo 	if (!tries[dev->devno])
2709c6fd2807SJeff Garzik 		ata_dev_disable(dev);
2710c6fd2807SJeff Garzik 
2711c6fd2807SJeff Garzik 	goto retry;
2712c6fd2807SJeff Garzik }
2713c6fd2807SJeff Garzik 
2714c6fd2807SJeff Garzik /**
2715c6fd2807SJeff Garzik  *	sata_print_link_status - Print SATA link status
2716936fd732STejun Heo  *	@link: SATA link to printk link status about
2717c6fd2807SJeff Garzik  *
2718c6fd2807SJeff Garzik  *	This function prints link speed and status of a SATA link.
2719c6fd2807SJeff Garzik  *
2720c6fd2807SJeff Garzik  *	LOCKING:
2721c6fd2807SJeff Garzik  *	None.
2722c6fd2807SJeff Garzik  */
27236bdb4fc9SAdrian Bunk static void sata_print_link_status(struct ata_link *link)
2724c6fd2807SJeff Garzik {
2725c6fd2807SJeff Garzik 	u32 sstatus, scontrol, tmp;
2726c6fd2807SJeff Garzik 
2727936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus))
2728c6fd2807SJeff Garzik 		return;
2729936fd732STejun Heo 	sata_scr_read(link, SCR_CONTROL, &scontrol);
2730c6fd2807SJeff Garzik 
2731b1c72916STejun Heo 	if (ata_phys_link_online(link)) {
2732c6fd2807SJeff Garzik 		tmp = (sstatus >> 4) & 0xf;
2733a9a79dfeSJoe Perches 		ata_link_info(link, "SATA link up %s (SStatus %X SControl %X)\n",
2734c6fd2807SJeff Garzik 			      sata_spd_string(tmp), sstatus, scontrol);
2735c6fd2807SJeff Garzik 	} else {
2736a9a79dfeSJoe Perches 		ata_link_info(link, "SATA link down (SStatus %X SControl %X)\n",
2737c6fd2807SJeff Garzik 			      sstatus, scontrol);
2738c6fd2807SJeff Garzik 	}
2739c6fd2807SJeff Garzik }
2740c6fd2807SJeff Garzik 
2741c6fd2807SJeff Garzik /**
2742c6fd2807SJeff Garzik  *	ata_dev_pair		-	return other device on cable
2743c6fd2807SJeff Garzik  *	@adev: device
2744c6fd2807SJeff Garzik  *
2745c6fd2807SJeff Garzik  *	Obtain the other device on the same cable, or if none is
2746c6fd2807SJeff Garzik  *	present NULL is returned
2747c6fd2807SJeff Garzik  */
2748c6fd2807SJeff Garzik 
2749c6fd2807SJeff Garzik struct ata_device *ata_dev_pair(struct ata_device *adev)
2750c6fd2807SJeff Garzik {
27519af5c9c9STejun Heo 	struct ata_link *link = adev->link;
27529af5c9c9STejun Heo 	struct ata_device *pair = &link->device[1 - adev->devno];
2753c6fd2807SJeff Garzik 	if (!ata_dev_enabled(pair))
2754c6fd2807SJeff Garzik 		return NULL;
2755c6fd2807SJeff Garzik 	return pair;
2756c6fd2807SJeff Garzik }
2757c6fd2807SJeff Garzik 
2758c6fd2807SJeff Garzik /**
2759c6fd2807SJeff Garzik  *	sata_down_spd_limit - adjust SATA spd limit downward
2760936fd732STejun Heo  *	@link: Link to adjust SATA spd limit for
2761a07d499bSTejun Heo  *	@spd_limit: Additional limit
2762c6fd2807SJeff Garzik  *
2763936fd732STejun Heo  *	Adjust SATA spd limit of @link downward.  Note that this
2764c6fd2807SJeff Garzik  *	function only adjusts the limit.  The change must be applied
2765c6fd2807SJeff Garzik  *	using sata_set_spd().
2766c6fd2807SJeff Garzik  *
2767a07d499bSTejun Heo  *	If @spd_limit is non-zero, the speed is limited to equal to or
2768a07d499bSTejun Heo  *	lower than @spd_limit if such speed is supported.  If
2769a07d499bSTejun Heo  *	@spd_limit is slower than any supported speed, only the lowest
2770a07d499bSTejun Heo  *	supported speed is allowed.
2771a07d499bSTejun Heo  *
2772c6fd2807SJeff Garzik  *	LOCKING:
2773c6fd2807SJeff Garzik  *	Inherited from caller.
2774c6fd2807SJeff Garzik  *
2775c6fd2807SJeff Garzik  *	RETURNS:
2776c6fd2807SJeff Garzik  *	0 on success, negative errno on failure
2777c6fd2807SJeff Garzik  */
2778a07d499bSTejun Heo int sata_down_spd_limit(struct ata_link *link, u32 spd_limit)
2779c6fd2807SJeff Garzik {
2780c6fd2807SJeff Garzik 	u32 sstatus, spd, mask;
2781a07d499bSTejun Heo 	int rc, bit;
2782c6fd2807SJeff Garzik 
2783936fd732STejun Heo 	if (!sata_scr_valid(link))
2784008a7896STejun Heo 		return -EOPNOTSUPP;
2785008a7896STejun Heo 
2786008a7896STejun Heo 	/* If SCR can be read, use it to determine the current SPD.
2787936fd732STejun Heo 	 * If not, use cached value in link->sata_spd.
2788008a7896STejun Heo 	 */
2789936fd732STejun Heo 	rc = sata_scr_read(link, SCR_STATUS, &sstatus);
27909913ff8aSTejun Heo 	if (rc == 0 && ata_sstatus_online(sstatus))
2791008a7896STejun Heo 		spd = (sstatus >> 4) & 0xf;
2792008a7896STejun Heo 	else
2793936fd732STejun Heo 		spd = link->sata_spd;
2794c6fd2807SJeff Garzik 
2795936fd732STejun Heo 	mask = link->sata_spd_limit;
2796c6fd2807SJeff Garzik 	if (mask <= 1)
2797c6fd2807SJeff Garzik 		return -EINVAL;
2798008a7896STejun Heo 
2799008a7896STejun Heo 	/* unconditionally mask off the highest bit */
2800a07d499bSTejun Heo 	bit = fls(mask) - 1;
2801a07d499bSTejun Heo 	mask &= ~(1 << bit);
2802c6fd2807SJeff Garzik 
2803008a7896STejun Heo 	/* Mask off all speeds higher than or equal to the current
2804008a7896STejun Heo 	 * one.  Force 1.5Gbps if current SPD is not available.
2805008a7896STejun Heo 	 */
2806008a7896STejun Heo 	if (spd > 1)
2807008a7896STejun Heo 		mask &= (1 << (spd - 1)) - 1;
2808008a7896STejun Heo 	else
2809008a7896STejun Heo 		mask &= 1;
2810008a7896STejun Heo 
2811008a7896STejun Heo 	/* were we already at the bottom? */
2812c6fd2807SJeff Garzik 	if (!mask)
2813c6fd2807SJeff Garzik 		return -EINVAL;
2814c6fd2807SJeff Garzik 
2815a07d499bSTejun Heo 	if (spd_limit) {
2816a07d499bSTejun Heo 		if (mask & ((1 << spd_limit) - 1))
2817a07d499bSTejun Heo 			mask &= (1 << spd_limit) - 1;
2818a07d499bSTejun Heo 		else {
2819a07d499bSTejun Heo 			bit = ffs(mask) - 1;
2820a07d499bSTejun Heo 			mask = 1 << bit;
2821a07d499bSTejun Heo 		}
2822a07d499bSTejun Heo 	}
2823a07d499bSTejun Heo 
2824936fd732STejun Heo 	link->sata_spd_limit = mask;
2825c6fd2807SJeff Garzik 
2826a9a79dfeSJoe Perches 	ata_link_warn(link, "limiting SATA link speed to %s\n",
2827c6fd2807SJeff Garzik 		      sata_spd_string(fls(mask)));
2828c6fd2807SJeff Garzik 
2829c6fd2807SJeff Garzik 	return 0;
2830c6fd2807SJeff Garzik }
2831c6fd2807SJeff Garzik 
2832936fd732STejun Heo static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
2833c6fd2807SJeff Garzik {
28345270222fSTejun Heo 	struct ata_link *host_link = &link->ap->link;
28355270222fSTejun Heo 	u32 limit, target, spd;
2836c6fd2807SJeff Garzik 
28375270222fSTejun Heo 	limit = link->sata_spd_limit;
28385270222fSTejun Heo 
28395270222fSTejun Heo 	/* Don't configure downstream link faster than upstream link.
28405270222fSTejun Heo 	 * It doesn't speed up anything and some PMPs choke on such
28415270222fSTejun Heo 	 * configuration.
28425270222fSTejun Heo 	 */
28435270222fSTejun Heo 	if (!ata_is_host_link(link) && host_link->sata_spd)
28445270222fSTejun Heo 		limit &= (1 << host_link->sata_spd) - 1;
28455270222fSTejun Heo 
28465270222fSTejun Heo 	if (limit == UINT_MAX)
28475270222fSTejun Heo 		target = 0;
2848c6fd2807SJeff Garzik 	else
28495270222fSTejun Heo 		target = fls(limit);
2850c6fd2807SJeff Garzik 
2851c6fd2807SJeff Garzik 	spd = (*scontrol >> 4) & 0xf;
28525270222fSTejun Heo 	*scontrol = (*scontrol & ~0xf0) | ((target & 0xf) << 4);
2853c6fd2807SJeff Garzik 
28545270222fSTejun Heo 	return spd != target;
2855c6fd2807SJeff Garzik }
2856c6fd2807SJeff Garzik 
2857c6fd2807SJeff Garzik /**
2858c6fd2807SJeff Garzik  *	sata_set_spd_needed - is SATA spd configuration needed
2859936fd732STejun Heo  *	@link: Link in question
2860c6fd2807SJeff Garzik  *
2861c6fd2807SJeff Garzik  *	Test whether the spd limit in SControl matches
2862936fd732STejun Heo  *	@link->sata_spd_limit.  This function is used to determine
2863c6fd2807SJeff Garzik  *	whether hardreset is necessary to apply SATA spd
2864c6fd2807SJeff Garzik  *	configuration.
2865c6fd2807SJeff Garzik  *
2866c6fd2807SJeff Garzik  *	LOCKING:
2867c6fd2807SJeff Garzik  *	Inherited from caller.
2868c6fd2807SJeff Garzik  *
2869c6fd2807SJeff Garzik  *	RETURNS:
2870c6fd2807SJeff Garzik  *	1 if SATA spd configuration is needed, 0 otherwise.
2871c6fd2807SJeff Garzik  */
28721dc55e87SAdrian Bunk static int sata_set_spd_needed(struct ata_link *link)
2873c6fd2807SJeff Garzik {
2874c6fd2807SJeff Garzik 	u32 scontrol;
2875c6fd2807SJeff Garzik 
2876936fd732STejun Heo 	if (sata_scr_read(link, SCR_CONTROL, &scontrol))
2877db64bcf3STejun Heo 		return 1;
2878c6fd2807SJeff Garzik 
2879936fd732STejun Heo 	return __sata_set_spd_needed(link, &scontrol);
2880c6fd2807SJeff Garzik }
2881c6fd2807SJeff Garzik 
2882c6fd2807SJeff Garzik /**
2883c6fd2807SJeff Garzik  *	sata_set_spd - set SATA spd according to spd limit
2884936fd732STejun Heo  *	@link: Link to set SATA spd for
2885c6fd2807SJeff Garzik  *
2886936fd732STejun Heo  *	Set SATA spd of @link according to sata_spd_limit.
2887c6fd2807SJeff Garzik  *
2888c6fd2807SJeff Garzik  *	LOCKING:
2889c6fd2807SJeff Garzik  *	Inherited from caller.
2890c6fd2807SJeff Garzik  *
2891c6fd2807SJeff Garzik  *	RETURNS:
2892c6fd2807SJeff Garzik  *	0 if spd doesn't need to be changed, 1 if spd has been
2893c6fd2807SJeff Garzik  *	changed.  Negative errno if SCR registers are inaccessible.
2894c6fd2807SJeff Garzik  */
2895936fd732STejun Heo int sata_set_spd(struct ata_link *link)
2896c6fd2807SJeff Garzik {
2897c6fd2807SJeff Garzik 	u32 scontrol;
2898c6fd2807SJeff Garzik 	int rc;
2899c6fd2807SJeff Garzik 
2900936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
2901c6fd2807SJeff Garzik 		return rc;
2902c6fd2807SJeff Garzik 
2903936fd732STejun Heo 	if (!__sata_set_spd_needed(link, &scontrol))
2904c6fd2807SJeff Garzik 		return 0;
2905c6fd2807SJeff Garzik 
2906936fd732STejun Heo 	if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
2907c6fd2807SJeff Garzik 		return rc;
2908c6fd2807SJeff Garzik 
2909c6fd2807SJeff Garzik 	return 1;
2910c6fd2807SJeff Garzik }
2911c6fd2807SJeff Garzik 
2912c6fd2807SJeff Garzik /*
2913c6fd2807SJeff Garzik  * This mode timing computation functionality is ported over from
2914c6fd2807SJeff Garzik  * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
2915c6fd2807SJeff Garzik  */
2916c6fd2807SJeff Garzik /*
2917b352e57dSAlan Cox  * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
2918c6fd2807SJeff Garzik  * These were taken from ATA/ATAPI-6 standard, rev 0a, except
2919b352e57dSAlan Cox  * for UDMA6, which is currently supported only by Maxtor drives.
2920b352e57dSAlan Cox  *
2921b352e57dSAlan Cox  * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0.
2922c6fd2807SJeff Garzik  */
2923c6fd2807SJeff Garzik 
2924c6fd2807SJeff Garzik static const struct ata_timing ata_timing[] = {
29253ada9c12SDavid Daney /*	{ XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 0,  960,   0 }, */
29263ada9c12SDavid Daney 	{ XFER_PIO_0,     70, 290, 240, 600, 165, 150, 0,  600,   0 },
29273ada9c12SDavid Daney 	{ XFER_PIO_1,     50, 290,  93, 383, 125, 100, 0,  383,   0 },
29283ada9c12SDavid Daney 	{ XFER_PIO_2,     30, 290,  40, 330, 100,  90, 0,  240,   0 },
29293ada9c12SDavid Daney 	{ XFER_PIO_3,     30,  80,  70, 180,  80,  70, 0,  180,   0 },
29303ada9c12SDavid Daney 	{ XFER_PIO_4,     25,  70,  25, 120,  70,  25, 0,  120,   0 },
29313ada9c12SDavid Daney 	{ XFER_PIO_5,     15,  65,  25, 100,  65,  25, 0,  100,   0 },
29323ada9c12SDavid Daney 	{ XFER_PIO_6,     10,  55,  20,  80,  55,  20, 0,   80,   0 },
2933c6fd2807SJeff Garzik 
29343ada9c12SDavid Daney 	{ XFER_SW_DMA_0, 120,   0,   0,   0, 480, 480, 50, 960,   0 },
29353ada9c12SDavid Daney 	{ XFER_SW_DMA_1,  90,   0,   0,   0, 240, 240, 30, 480,   0 },
29363ada9c12SDavid Daney 	{ XFER_SW_DMA_2,  60,   0,   0,   0, 120, 120, 20, 240,   0 },
2937c6fd2807SJeff Garzik 
29383ada9c12SDavid Daney 	{ XFER_MW_DMA_0,  60,   0,   0,   0, 215, 215, 20, 480,   0 },
29393ada9c12SDavid Daney 	{ XFER_MW_DMA_1,  45,   0,   0,   0,  80,  50, 5,  150,   0 },
29403ada9c12SDavid Daney 	{ XFER_MW_DMA_2,  25,   0,   0,   0,  70,  25, 5,  120,   0 },
29413ada9c12SDavid Daney 	{ XFER_MW_DMA_3,  25,   0,   0,   0,  65,  25, 5,  100,   0 },
29423ada9c12SDavid Daney 	{ XFER_MW_DMA_4,  25,   0,   0,   0,  55,  20, 5,   80,   0 },
2943c6fd2807SJeff Garzik 
29443ada9c12SDavid Daney /*	{ XFER_UDMA_SLOW,  0,   0,   0,   0,   0,   0, 0,    0, 150 }, */
29453ada9c12SDavid Daney 	{ XFER_UDMA_0,     0,   0,   0,   0,   0,   0, 0,    0, 120 },
29463ada9c12SDavid Daney 	{ XFER_UDMA_1,     0,   0,   0,   0,   0,   0, 0,    0,  80 },
29473ada9c12SDavid Daney 	{ XFER_UDMA_2,     0,   0,   0,   0,   0,   0, 0,    0,  60 },
29483ada9c12SDavid Daney 	{ XFER_UDMA_3,     0,   0,   0,   0,   0,   0, 0,    0,  45 },
29493ada9c12SDavid Daney 	{ XFER_UDMA_4,     0,   0,   0,   0,   0,   0, 0,    0,  30 },
29503ada9c12SDavid Daney 	{ XFER_UDMA_5,     0,   0,   0,   0,   0,   0, 0,    0,  20 },
29513ada9c12SDavid Daney 	{ XFER_UDMA_6,     0,   0,   0,   0,   0,   0, 0,    0,  15 },
2952c6fd2807SJeff Garzik 
2953c6fd2807SJeff Garzik 	{ 0xFF }
2954c6fd2807SJeff Garzik };
2955c6fd2807SJeff Garzik 
2956c6fd2807SJeff Garzik #define ENOUGH(v, unit)		(((v)-1)/(unit)+1)
2957c6fd2807SJeff Garzik #define EZ(v, unit)		((v)?ENOUGH(v, unit):0)
2958c6fd2807SJeff Garzik 
2959c6fd2807SJeff Garzik static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
2960c6fd2807SJeff Garzik {
2961c6fd2807SJeff Garzik 	q->setup	= EZ(t->setup      * 1000,  T);
2962c6fd2807SJeff Garzik 	q->act8b	= EZ(t->act8b      * 1000,  T);
2963c6fd2807SJeff Garzik 	q->rec8b	= EZ(t->rec8b      * 1000,  T);
2964c6fd2807SJeff Garzik 	q->cyc8b	= EZ(t->cyc8b      * 1000,  T);
2965c6fd2807SJeff Garzik 	q->active	= EZ(t->active     * 1000,  T);
2966c6fd2807SJeff Garzik 	q->recover	= EZ(t->recover    * 1000,  T);
29673ada9c12SDavid Daney 	q->dmack_hold	= EZ(t->dmack_hold * 1000,  T);
2968c6fd2807SJeff Garzik 	q->cycle	= EZ(t->cycle      * 1000,  T);
2969c6fd2807SJeff Garzik 	q->udma		= EZ(t->udma       * 1000, UT);
2970c6fd2807SJeff Garzik }
2971c6fd2807SJeff Garzik 
2972c6fd2807SJeff Garzik void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
2973c6fd2807SJeff Garzik 		      struct ata_timing *m, unsigned int what)
2974c6fd2807SJeff Garzik {
2975c6fd2807SJeff Garzik 	if (what & ATA_TIMING_SETUP  ) m->setup   = max(a->setup,   b->setup);
2976c6fd2807SJeff Garzik 	if (what & ATA_TIMING_ACT8B  ) m->act8b   = max(a->act8b,   b->act8b);
2977c6fd2807SJeff Garzik 	if (what & ATA_TIMING_REC8B  ) m->rec8b   = max(a->rec8b,   b->rec8b);
2978c6fd2807SJeff Garzik 	if (what & ATA_TIMING_CYC8B  ) m->cyc8b   = max(a->cyc8b,   b->cyc8b);
2979c6fd2807SJeff Garzik 	if (what & ATA_TIMING_ACTIVE ) m->active  = max(a->active,  b->active);
2980c6fd2807SJeff Garzik 	if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
29813ada9c12SDavid Daney 	if (what & ATA_TIMING_DMACK_HOLD) m->dmack_hold = max(a->dmack_hold, b->dmack_hold);
2982c6fd2807SJeff Garzik 	if (what & ATA_TIMING_CYCLE  ) m->cycle   = max(a->cycle,   b->cycle);
2983c6fd2807SJeff Garzik 	if (what & ATA_TIMING_UDMA   ) m->udma    = max(a->udma,    b->udma);
2984c6fd2807SJeff Garzik }
2985c6fd2807SJeff Garzik 
29866357357cSTejun Heo const struct ata_timing *ata_timing_find_mode(u8 xfer_mode)
2987c6fd2807SJeff Garzik {
298870cd071eSTejun Heo 	const struct ata_timing *t = ata_timing;
2989c6fd2807SJeff Garzik 
299070cd071eSTejun Heo 	while (xfer_mode > t->mode)
299170cd071eSTejun Heo 		t++;
299270cd071eSTejun Heo 
299370cd071eSTejun Heo 	if (xfer_mode == t->mode)
2994c6fd2807SJeff Garzik 		return t;
2995cd705d5aSBorislav Petkov 
2996cd705d5aSBorislav Petkov 	WARN_ONCE(true, "%s: unable to find timing for xfer_mode 0x%x\n",
2997cd705d5aSBorislav Petkov 			__func__, xfer_mode);
2998cd705d5aSBorislav Petkov 
299970cd071eSTejun Heo 	return NULL;
3000c6fd2807SJeff Garzik }
3001c6fd2807SJeff Garzik 
3002c6fd2807SJeff Garzik int ata_timing_compute(struct ata_device *adev, unsigned short speed,
3003c6fd2807SJeff Garzik 		       struct ata_timing *t, int T, int UT)
3004c6fd2807SJeff Garzik {
30059e8808a9SBartlomiej Zolnierkiewicz 	const u16 *id = adev->id;
3006c6fd2807SJeff Garzik 	const struct ata_timing *s;
3007c6fd2807SJeff Garzik 	struct ata_timing p;
3008c6fd2807SJeff Garzik 
3009c6fd2807SJeff Garzik 	/*
3010c6fd2807SJeff Garzik 	 * Find the mode.
3011c6fd2807SJeff Garzik 	 */
3012c6fd2807SJeff Garzik 
3013c6fd2807SJeff Garzik 	if (!(s = ata_timing_find_mode(speed)))
3014c6fd2807SJeff Garzik 		return -EINVAL;
3015c6fd2807SJeff Garzik 
3016c6fd2807SJeff Garzik 	memcpy(t, s, sizeof(*s));
3017c6fd2807SJeff Garzik 
3018c6fd2807SJeff Garzik 	/*
3019c6fd2807SJeff Garzik 	 * If the drive is an EIDE drive, it can tell us it needs extended
3020c6fd2807SJeff Garzik 	 * PIO/MW_DMA cycle timing.
3021c6fd2807SJeff Garzik 	 */
3022c6fd2807SJeff Garzik 
30239e8808a9SBartlomiej Zolnierkiewicz 	if (id[ATA_ID_FIELD_VALID] & 2) {	/* EIDE drive */
3024c6fd2807SJeff Garzik 		memset(&p, 0, sizeof(p));
30259e8808a9SBartlomiej Zolnierkiewicz 
3026bff00256SBartlomiej Zolnierkiewicz 		if (speed >= XFER_PIO_0 && speed < XFER_SW_DMA_0) {
30279e8808a9SBartlomiej Zolnierkiewicz 			if (speed <= XFER_PIO_2)
30289e8808a9SBartlomiej Zolnierkiewicz 				p.cycle = p.cyc8b = id[ATA_ID_EIDE_PIO];
30299e8808a9SBartlomiej Zolnierkiewicz 			else if ((speed <= XFER_PIO_4) ||
30309e8808a9SBartlomiej Zolnierkiewicz 				 (speed == XFER_PIO_5 && !ata_id_is_cfa(id)))
30319e8808a9SBartlomiej Zolnierkiewicz 				p.cycle = p.cyc8b = id[ATA_ID_EIDE_PIO_IORDY];
30329e8808a9SBartlomiej Zolnierkiewicz 		} else if (speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2)
30339e8808a9SBartlomiej Zolnierkiewicz 			p.cycle = id[ATA_ID_EIDE_DMA_MIN];
30349e8808a9SBartlomiej Zolnierkiewicz 
3035c6fd2807SJeff Garzik 		ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
3036c6fd2807SJeff Garzik 	}
3037c6fd2807SJeff Garzik 
3038c6fd2807SJeff Garzik 	/*
3039c6fd2807SJeff Garzik 	 * Convert the timing to bus clock counts.
3040c6fd2807SJeff Garzik 	 */
3041c6fd2807SJeff Garzik 
3042c6fd2807SJeff Garzik 	ata_timing_quantize(t, t, T, UT);
3043c6fd2807SJeff Garzik 
3044c6fd2807SJeff Garzik 	/*
3045c6fd2807SJeff Garzik 	 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
3046c6fd2807SJeff Garzik 	 * S.M.A.R.T * and some other commands. We have to ensure that the
3047c6fd2807SJeff Garzik 	 * DMA cycle timing is slower/equal than the fastest PIO timing.
3048c6fd2807SJeff Garzik 	 */
3049c6fd2807SJeff Garzik 
3050fd3367afSAlan 	if (speed > XFER_PIO_6) {
3051c6fd2807SJeff Garzik 		ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
3052c6fd2807SJeff Garzik 		ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
3053c6fd2807SJeff Garzik 	}
3054c6fd2807SJeff Garzik 
3055c6fd2807SJeff Garzik 	/*
3056c6fd2807SJeff Garzik 	 * Lengthen active & recovery time so that cycle time is correct.
3057c6fd2807SJeff Garzik 	 */
3058c6fd2807SJeff Garzik 
3059c6fd2807SJeff Garzik 	if (t->act8b + t->rec8b < t->cyc8b) {
3060c6fd2807SJeff Garzik 		t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
3061c6fd2807SJeff Garzik 		t->rec8b = t->cyc8b - t->act8b;
3062c6fd2807SJeff Garzik 	}
3063c6fd2807SJeff Garzik 
3064c6fd2807SJeff Garzik 	if (t->active + t->recover < t->cycle) {
3065c6fd2807SJeff Garzik 		t->active += (t->cycle - (t->active + t->recover)) / 2;
3066c6fd2807SJeff Garzik 		t->recover = t->cycle - t->active;
3067c6fd2807SJeff Garzik 	}
30684f701d1eSAlan Cox 
30694f701d1eSAlan Cox 	/* In a few cases quantisation may produce enough errors to
30704f701d1eSAlan Cox 	   leave t->cycle too low for the sum of active and recovery
30714f701d1eSAlan Cox 	   if so we must correct this */
30724f701d1eSAlan Cox 	if (t->active + t->recover > t->cycle)
30734f701d1eSAlan Cox 		t->cycle = t->active + t->recover;
3074c6fd2807SJeff Garzik 
3075c6fd2807SJeff Garzik 	return 0;
3076c6fd2807SJeff Garzik }
3077c6fd2807SJeff Garzik 
3078c6fd2807SJeff Garzik /**
3079a0f79b92STejun Heo  *	ata_timing_cycle2mode - find xfer mode for the specified cycle duration
3080a0f79b92STejun Heo  *	@xfer_shift: ATA_SHIFT_* value for transfer type to examine.
3081a0f79b92STejun Heo  *	@cycle: cycle duration in ns
3082a0f79b92STejun Heo  *
3083a0f79b92STejun Heo  *	Return matching xfer mode for @cycle.  The returned mode is of
3084a0f79b92STejun Heo  *	the transfer type specified by @xfer_shift.  If @cycle is too
3085a0f79b92STejun Heo  *	slow for @xfer_shift, 0xff is returned.  If @cycle is faster
3086a0f79b92STejun Heo  *	than the fastest known mode, the fasted mode is returned.
3087a0f79b92STejun Heo  *
3088a0f79b92STejun Heo  *	LOCKING:
3089a0f79b92STejun Heo  *	None.
3090a0f79b92STejun Heo  *
3091a0f79b92STejun Heo  *	RETURNS:
3092a0f79b92STejun Heo  *	Matching xfer_mode, 0xff if no match found.
3093a0f79b92STejun Heo  */
3094a0f79b92STejun Heo u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
3095a0f79b92STejun Heo {
3096a0f79b92STejun Heo 	u8 base_mode = 0xff, last_mode = 0xff;
3097a0f79b92STejun Heo 	const struct ata_xfer_ent *ent;
3098a0f79b92STejun Heo 	const struct ata_timing *t;
3099a0f79b92STejun Heo 
3100a0f79b92STejun Heo 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
3101a0f79b92STejun Heo 		if (ent->shift == xfer_shift)
3102a0f79b92STejun Heo 			base_mode = ent->base;
3103a0f79b92STejun Heo 
3104a0f79b92STejun Heo 	for (t = ata_timing_find_mode(base_mode);
3105a0f79b92STejun Heo 	     t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
3106a0f79b92STejun Heo 		unsigned short this_cycle;
3107a0f79b92STejun Heo 
3108a0f79b92STejun Heo 		switch (xfer_shift) {
3109a0f79b92STejun Heo 		case ATA_SHIFT_PIO:
3110a0f79b92STejun Heo 		case ATA_SHIFT_MWDMA:
3111a0f79b92STejun Heo 			this_cycle = t->cycle;
3112a0f79b92STejun Heo 			break;
3113a0f79b92STejun Heo 		case ATA_SHIFT_UDMA:
3114a0f79b92STejun Heo 			this_cycle = t->udma;
3115a0f79b92STejun Heo 			break;
3116a0f79b92STejun Heo 		default:
3117a0f79b92STejun Heo 			return 0xff;
3118a0f79b92STejun Heo 		}
3119a0f79b92STejun Heo 
3120a0f79b92STejun Heo 		if (cycle > this_cycle)
3121a0f79b92STejun Heo 			break;
3122a0f79b92STejun Heo 
3123a0f79b92STejun Heo 		last_mode = t->mode;
3124a0f79b92STejun Heo 	}
3125a0f79b92STejun Heo 
3126a0f79b92STejun Heo 	return last_mode;
3127a0f79b92STejun Heo }
3128a0f79b92STejun Heo 
3129a0f79b92STejun Heo /**
3130c6fd2807SJeff Garzik  *	ata_down_xfermask_limit - adjust dev xfer masks downward
3131c6fd2807SJeff Garzik  *	@dev: Device to adjust xfer masks
3132458337dbSTejun Heo  *	@sel: ATA_DNXFER_* selector
3133c6fd2807SJeff Garzik  *
3134c6fd2807SJeff Garzik  *	Adjust xfer masks of @dev downward.  Note that this function
3135c6fd2807SJeff Garzik  *	does not apply the change.  Invoking ata_set_mode() afterwards
3136c6fd2807SJeff Garzik  *	will apply the limit.
3137c6fd2807SJeff Garzik  *
3138c6fd2807SJeff Garzik  *	LOCKING:
3139c6fd2807SJeff Garzik  *	Inherited from caller.
3140c6fd2807SJeff Garzik  *
3141c6fd2807SJeff Garzik  *	RETURNS:
3142c6fd2807SJeff Garzik  *	0 on success, negative errno on failure
3143c6fd2807SJeff Garzik  */
3144458337dbSTejun Heo int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
3145c6fd2807SJeff Garzik {
3146458337dbSTejun Heo 	char buf[32];
31477dc951aeSTejun Heo 	unsigned long orig_mask, xfer_mask;
31487dc951aeSTejun Heo 	unsigned long pio_mask, mwdma_mask, udma_mask;
3149458337dbSTejun Heo 	int quiet, highbit;
3150c6fd2807SJeff Garzik 
3151458337dbSTejun Heo 	quiet = !!(sel & ATA_DNXFER_QUIET);
3152458337dbSTejun Heo 	sel &= ~ATA_DNXFER_QUIET;
3153458337dbSTejun Heo 
3154458337dbSTejun Heo 	xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
3155458337dbSTejun Heo 						  dev->mwdma_mask,
3156c6fd2807SJeff Garzik 						  dev->udma_mask);
3157458337dbSTejun Heo 	ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
3158c6fd2807SJeff Garzik 
3159458337dbSTejun Heo 	switch (sel) {
3160458337dbSTejun Heo 	case ATA_DNXFER_PIO:
3161458337dbSTejun Heo 		highbit = fls(pio_mask) - 1;
3162458337dbSTejun Heo 		pio_mask &= ~(1 << highbit);
3163458337dbSTejun Heo 		break;
3164458337dbSTejun Heo 
3165458337dbSTejun Heo 	case ATA_DNXFER_DMA:
3166458337dbSTejun Heo 		if (udma_mask) {
3167458337dbSTejun Heo 			highbit = fls(udma_mask) - 1;
3168458337dbSTejun Heo 			udma_mask &= ~(1 << highbit);
3169458337dbSTejun Heo 			if (!udma_mask)
3170458337dbSTejun Heo 				return -ENOENT;
3171458337dbSTejun Heo 		} else if (mwdma_mask) {
3172458337dbSTejun Heo 			highbit = fls(mwdma_mask) - 1;
3173458337dbSTejun Heo 			mwdma_mask &= ~(1 << highbit);
3174458337dbSTejun Heo 			if (!mwdma_mask)
3175458337dbSTejun Heo 				return -ENOENT;
3176458337dbSTejun Heo 		}
3177458337dbSTejun Heo 		break;
3178458337dbSTejun Heo 
3179458337dbSTejun Heo 	case ATA_DNXFER_40C:
3180458337dbSTejun Heo 		udma_mask &= ATA_UDMA_MASK_40C;
3181458337dbSTejun Heo 		break;
3182458337dbSTejun Heo 
3183458337dbSTejun Heo 	case ATA_DNXFER_FORCE_PIO0:
3184458337dbSTejun Heo 		pio_mask &= 1;
3185458337dbSTejun Heo 	case ATA_DNXFER_FORCE_PIO:
3186458337dbSTejun Heo 		mwdma_mask = 0;
3187458337dbSTejun Heo 		udma_mask = 0;
3188458337dbSTejun Heo 		break;
3189458337dbSTejun Heo 
3190458337dbSTejun Heo 	default:
3191458337dbSTejun Heo 		BUG();
3192458337dbSTejun Heo 	}
3193458337dbSTejun Heo 
3194458337dbSTejun Heo 	xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
3195458337dbSTejun Heo 
3196458337dbSTejun Heo 	if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
3197458337dbSTejun Heo 		return -ENOENT;
3198458337dbSTejun Heo 
3199458337dbSTejun Heo 	if (!quiet) {
3200458337dbSTejun Heo 		if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
3201458337dbSTejun Heo 			snprintf(buf, sizeof(buf), "%s:%s",
3202458337dbSTejun Heo 				 ata_mode_string(xfer_mask),
3203458337dbSTejun Heo 				 ata_mode_string(xfer_mask & ATA_MASK_PIO));
3204458337dbSTejun Heo 		else
3205458337dbSTejun Heo 			snprintf(buf, sizeof(buf), "%s",
3206458337dbSTejun Heo 				 ata_mode_string(xfer_mask));
3207458337dbSTejun Heo 
3208a9a79dfeSJoe Perches 		ata_dev_warn(dev, "limiting speed to %s\n", buf);
3209458337dbSTejun Heo 	}
3210c6fd2807SJeff Garzik 
3211c6fd2807SJeff Garzik 	ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
3212c6fd2807SJeff Garzik 			    &dev->udma_mask);
3213c6fd2807SJeff Garzik 
3214c6fd2807SJeff Garzik 	return 0;
3215c6fd2807SJeff Garzik }
3216c6fd2807SJeff Garzik 
3217c6fd2807SJeff Garzik static int ata_dev_set_mode(struct ata_device *dev)
3218c6fd2807SJeff Garzik {
3219d0cb43b3STejun Heo 	struct ata_port *ap = dev->link->ap;
32209af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
3221d0cb43b3STejun Heo 	const bool nosetxfer = dev->horkage & ATA_HORKAGE_NOSETXFER;
32224055dee7STejun Heo 	const char *dev_err_whine = "";
32234055dee7STejun Heo 	int ign_dev_err = 0;
3224d0cb43b3STejun Heo 	unsigned int err_mask = 0;
3225c6fd2807SJeff Garzik 	int rc;
3226c6fd2807SJeff Garzik 
3227c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_PIO;
3228c6fd2807SJeff Garzik 	if (dev->xfer_shift == ATA_SHIFT_PIO)
3229c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_PIO;
3230c6fd2807SJeff Garzik 
3231d0cb43b3STejun Heo 	if (nosetxfer && ap->flags & ATA_FLAG_SATA && ata_id_is_sata(dev->id))
3232d0cb43b3STejun Heo 		dev_err_whine = " (SET_XFERMODE skipped)";
3233d0cb43b3STejun Heo 	else {
3234d0cb43b3STejun Heo 		if (nosetxfer)
3235a9a79dfeSJoe Perches 			ata_dev_warn(dev,
3236d0cb43b3STejun Heo 				     "NOSETXFER but PATA detected - can't "
3237d0cb43b3STejun Heo 				     "skip SETXFER, might malfunction\n");
3238c6fd2807SJeff Garzik 		err_mask = ata_dev_set_xfermode(dev);
3239d0cb43b3STejun Heo 	}
32402dcb407eSJeff Garzik 
32414055dee7STejun Heo 	if (err_mask & ~AC_ERR_DEV)
32424055dee7STejun Heo 		goto fail;
32432dcb407eSJeff Garzik 
32444055dee7STejun Heo 	/* revalidate */
3245baa1e78aSTejun Heo 	ehc->i.flags |= ATA_EHI_POST_SETMODE;
3246422c9daaSTejun Heo 	rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
3247baa1e78aSTejun Heo 	ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
3248c6fd2807SJeff Garzik 	if (rc)
3249c6fd2807SJeff Garzik 		return rc;
3250c6fd2807SJeff Garzik 
3251b93fda12SAlan Cox 	if (dev->xfer_shift == ATA_SHIFT_PIO) {
32524055dee7STejun Heo 		/* Old CFA may refuse this command, which is just fine */
3253b93fda12SAlan Cox 		if (ata_id_is_cfa(dev->id))
32544055dee7STejun Heo 			ign_dev_err = 1;
3255b93fda12SAlan Cox 		/* Catch several broken garbage emulations plus some pre
3256b93fda12SAlan Cox 		   ATA devices */
3257b93fda12SAlan Cox 		if (ata_id_major_version(dev->id) == 0 &&
32584055dee7STejun Heo 					dev->pio_mode <= XFER_PIO_2)
32594055dee7STejun Heo 			ign_dev_err = 1;
3260b93fda12SAlan Cox 		/* Some very old devices and some bad newer ones fail
3261b93fda12SAlan Cox 		   any kind of SET_XFERMODE request but support PIO0-2
3262b93fda12SAlan Cox 		   timings and no IORDY */
3263b93fda12SAlan Cox 		if (!ata_id_has_iordy(dev->id) && dev->pio_mode <= XFER_PIO_2)
3264b93fda12SAlan Cox 			ign_dev_err = 1;
3265b93fda12SAlan Cox 	}
32664055dee7STejun Heo 	/* Early MWDMA devices do DMA but don't allow DMA mode setting.
32674055dee7STejun Heo 	   Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
32684055dee7STejun Heo 	if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
32694055dee7STejun Heo 	    dev->dma_mode == XFER_MW_DMA_0 &&
32704055dee7STejun Heo 	    (dev->id[63] >> 8) & 1)
32714055dee7STejun Heo 		ign_dev_err = 1;
32724055dee7STejun Heo 
32734055dee7STejun Heo 	/* if the device is actually configured correctly, ignore dev err */
32744055dee7STejun Heo 	if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id)))
32754055dee7STejun Heo 		ign_dev_err = 1;
32764055dee7STejun Heo 
32774055dee7STejun Heo 	if (err_mask & AC_ERR_DEV) {
32784055dee7STejun Heo 		if (!ign_dev_err)
32794055dee7STejun Heo 			goto fail;
32804055dee7STejun Heo 		else
32814055dee7STejun Heo 			dev_err_whine = " (device error ignored)";
32824055dee7STejun Heo 	}
32834055dee7STejun Heo 
3284c6fd2807SJeff Garzik 	DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
3285c6fd2807SJeff Garzik 		dev->xfer_shift, (int)dev->xfer_mode);
3286c6fd2807SJeff Garzik 
3287a9a79dfeSJoe Perches 	ata_dev_info(dev, "configured for %s%s\n",
32884055dee7STejun Heo 		     ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)),
32894055dee7STejun Heo 		     dev_err_whine);
32904055dee7STejun Heo 
3291c6fd2807SJeff Garzik 	return 0;
32924055dee7STejun Heo 
32934055dee7STejun Heo  fail:
3294a9a79dfeSJoe Perches 	ata_dev_err(dev, "failed to set xfermode (err_mask=0x%x)\n", err_mask);
32954055dee7STejun Heo 	return -EIO;
3296c6fd2807SJeff Garzik }
3297c6fd2807SJeff Garzik 
3298c6fd2807SJeff Garzik /**
329904351821SAlan  *	ata_do_set_mode - Program timings and issue SET FEATURES - XFER
33000260731fSTejun Heo  *	@link: link on which timings will be programmed
33011967b7ffSJoe Perches  *	@r_failed_dev: out parameter for failed device
3302c6fd2807SJeff Garzik  *
330304351821SAlan  *	Standard implementation of the function used to tune and set
330404351821SAlan  *	ATA device disk transfer mode (PIO3, UDMA6, etc.).  If
330504351821SAlan  *	ata_dev_set_mode() fails, pointer to the failing device is
3306c6fd2807SJeff Garzik  *	returned in @r_failed_dev.
3307c6fd2807SJeff Garzik  *
3308c6fd2807SJeff Garzik  *	LOCKING:
3309c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
3310c6fd2807SJeff Garzik  *
3311c6fd2807SJeff Garzik  *	RETURNS:
3312c6fd2807SJeff Garzik  *	0 on success, negative errno otherwise
3313c6fd2807SJeff Garzik  */
331404351821SAlan 
33150260731fSTejun Heo int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
3316c6fd2807SJeff Garzik {
33170260731fSTejun Heo 	struct ata_port *ap = link->ap;
3318c6fd2807SJeff Garzik 	struct ata_device *dev;
3319f58229f8STejun Heo 	int rc = 0, used_dma = 0, found = 0;
3320c6fd2807SJeff Garzik 
3321c6fd2807SJeff Garzik 	/* step 1: calculate xfer_mask */
33221eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
33237dc951aeSTejun Heo 		unsigned long pio_mask, dma_mask;
3324b3a70601SAlan Cox 		unsigned int mode_mask;
3325c6fd2807SJeff Garzik 
3326b3a70601SAlan Cox 		mode_mask = ATA_DMA_MASK_ATA;
3327b3a70601SAlan Cox 		if (dev->class == ATA_DEV_ATAPI)
3328b3a70601SAlan Cox 			mode_mask = ATA_DMA_MASK_ATAPI;
3329b3a70601SAlan Cox 		else if (ata_id_is_cfa(dev->id))
3330b3a70601SAlan Cox 			mode_mask = ATA_DMA_MASK_CFA;
3331b3a70601SAlan Cox 
3332c6fd2807SJeff Garzik 		ata_dev_xfermask(dev);
333333267325STejun Heo 		ata_force_xfermask(dev);
3334c6fd2807SJeff Garzik 
3335c6fd2807SJeff Garzik 		pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
3336b3a70601SAlan Cox 
3337b3a70601SAlan Cox 		if (libata_dma_mask & mode_mask)
333880a9c430SSergei Shtylyov 			dma_mask = ata_pack_xfermask(0, dev->mwdma_mask,
333980a9c430SSergei Shtylyov 						     dev->udma_mask);
3340b3a70601SAlan Cox 		else
3341b3a70601SAlan Cox 			dma_mask = 0;
3342b3a70601SAlan Cox 
3343c6fd2807SJeff Garzik 		dev->pio_mode = ata_xfer_mask2mode(pio_mask);
3344c6fd2807SJeff Garzik 		dev->dma_mode = ata_xfer_mask2mode(dma_mask);
3345c6fd2807SJeff Garzik 
3346c6fd2807SJeff Garzik 		found = 1;
3347b15b3ebaSAlan Cox 		if (ata_dma_enabled(dev))
3348c6fd2807SJeff Garzik 			used_dma = 1;
3349c6fd2807SJeff Garzik 	}
3350c6fd2807SJeff Garzik 	if (!found)
3351c6fd2807SJeff Garzik 		goto out;
3352c6fd2807SJeff Garzik 
3353c6fd2807SJeff Garzik 	/* step 2: always set host PIO timings */
33541eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
335570cd071eSTejun Heo 		if (dev->pio_mode == 0xff) {
3356a9a79dfeSJoe Perches 			ata_dev_warn(dev, "no PIO support\n");
3357c6fd2807SJeff Garzik 			rc = -EINVAL;
3358c6fd2807SJeff Garzik 			goto out;
3359c6fd2807SJeff Garzik 		}
3360c6fd2807SJeff Garzik 
3361c6fd2807SJeff Garzik 		dev->xfer_mode = dev->pio_mode;
3362c6fd2807SJeff Garzik 		dev->xfer_shift = ATA_SHIFT_PIO;
3363c6fd2807SJeff Garzik 		if (ap->ops->set_piomode)
3364c6fd2807SJeff Garzik 			ap->ops->set_piomode(ap, dev);
3365c6fd2807SJeff Garzik 	}
3366c6fd2807SJeff Garzik 
3367c6fd2807SJeff Garzik 	/* step 3: set host DMA timings */
33681eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
33691eca4365STejun Heo 		if (!ata_dma_enabled(dev))
3370c6fd2807SJeff Garzik 			continue;
3371c6fd2807SJeff Garzik 
3372c6fd2807SJeff Garzik 		dev->xfer_mode = dev->dma_mode;
3373c6fd2807SJeff Garzik 		dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
3374c6fd2807SJeff Garzik 		if (ap->ops->set_dmamode)
3375c6fd2807SJeff Garzik 			ap->ops->set_dmamode(ap, dev);
3376c6fd2807SJeff Garzik 	}
3377c6fd2807SJeff Garzik 
3378c6fd2807SJeff Garzik 	/* step 4: update devices' xfer mode */
33791eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
3380c6fd2807SJeff Garzik 		rc = ata_dev_set_mode(dev);
3381c6fd2807SJeff Garzik 		if (rc)
3382c6fd2807SJeff Garzik 			goto out;
3383c6fd2807SJeff Garzik 	}
3384c6fd2807SJeff Garzik 
3385c6fd2807SJeff Garzik 	/* Record simplex status. If we selected DMA then the other
3386c6fd2807SJeff Garzik 	 * host channels are not permitted to do so.
3387c6fd2807SJeff Garzik 	 */
3388cca3974eSJeff Garzik 	if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
3389032af1ceSAlan 		ap->host->simplex_claimed = ap;
3390c6fd2807SJeff Garzik 
3391c6fd2807SJeff Garzik  out:
3392c6fd2807SJeff Garzik 	if (rc)
3393c6fd2807SJeff Garzik 		*r_failed_dev = dev;
3394c6fd2807SJeff Garzik 	return rc;
3395c6fd2807SJeff Garzik }
3396c6fd2807SJeff Garzik 
3397c6fd2807SJeff Garzik /**
3398aa2731adSTejun Heo  *	ata_wait_ready - wait for link to become ready
3399aa2731adSTejun Heo  *	@link: link to be waited on
3400aa2731adSTejun Heo  *	@deadline: deadline jiffies for the operation
3401aa2731adSTejun Heo  *	@check_ready: callback to check link readiness
3402aa2731adSTejun Heo  *
3403aa2731adSTejun Heo  *	Wait for @link to become ready.  @check_ready should return
3404aa2731adSTejun Heo  *	positive number if @link is ready, 0 if it isn't, -ENODEV if
3405aa2731adSTejun Heo  *	link doesn't seem to be occupied, other errno for other error
3406aa2731adSTejun Heo  *	conditions.
3407aa2731adSTejun Heo  *
3408aa2731adSTejun Heo  *	Transient -ENODEV conditions are allowed for
3409aa2731adSTejun Heo  *	ATA_TMOUT_FF_WAIT.
3410aa2731adSTejun Heo  *
3411aa2731adSTejun Heo  *	LOCKING:
3412aa2731adSTejun Heo  *	EH context.
3413aa2731adSTejun Heo  *
3414aa2731adSTejun Heo  *	RETURNS:
3415aa2731adSTejun Heo  *	0 if @linke is ready before @deadline; otherwise, -errno.
3416aa2731adSTejun Heo  */
3417aa2731adSTejun Heo int ata_wait_ready(struct ata_link *link, unsigned long deadline,
3418aa2731adSTejun Heo 		   int (*check_ready)(struct ata_link *link))
3419aa2731adSTejun Heo {
3420aa2731adSTejun Heo 	unsigned long start = jiffies;
3421b48d58f5STejun Heo 	unsigned long nodev_deadline;
3422aa2731adSTejun Heo 	int warned = 0;
3423aa2731adSTejun Heo 
3424b48d58f5STejun Heo 	/* choose which 0xff timeout to use, read comment in libata.h */
3425b48d58f5STejun Heo 	if (link->ap->host->flags & ATA_HOST_PARALLEL_SCAN)
3426b48d58f5STejun Heo 		nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT_LONG);
3427b48d58f5STejun Heo 	else
3428b48d58f5STejun Heo 		nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT);
3429b48d58f5STejun Heo 
3430b1c72916STejun Heo 	/* Slave readiness can't be tested separately from master.  On
3431b1c72916STejun Heo 	 * M/S emulation configuration, this function should be called
3432b1c72916STejun Heo 	 * only on the master and it will handle both master and slave.
3433b1c72916STejun Heo 	 */
3434b1c72916STejun Heo 	WARN_ON(link == link->ap->slave_link);
3435b1c72916STejun Heo 
3436aa2731adSTejun Heo 	if (time_after(nodev_deadline, deadline))
3437aa2731adSTejun Heo 		nodev_deadline = deadline;
3438aa2731adSTejun Heo 
3439aa2731adSTejun Heo 	while (1) {
3440aa2731adSTejun Heo 		unsigned long now = jiffies;
3441aa2731adSTejun Heo 		int ready, tmp;
3442aa2731adSTejun Heo 
3443aa2731adSTejun Heo 		ready = tmp = check_ready(link);
3444aa2731adSTejun Heo 		if (ready > 0)
3445aa2731adSTejun Heo 			return 0;
3446aa2731adSTejun Heo 
3447b48d58f5STejun Heo 		/*
3448b48d58f5STejun Heo 		 * -ENODEV could be transient.  Ignore -ENODEV if link
3449aa2731adSTejun Heo 		 * is online.  Also, some SATA devices take a long
3450b48d58f5STejun Heo 		 * time to clear 0xff after reset.  Wait for
3451b48d58f5STejun Heo 		 * ATA_TMOUT_FF_WAIT[_LONG] on -ENODEV if link isn't
3452b48d58f5STejun Heo 		 * offline.
3453aa2731adSTejun Heo 		 *
3454aa2731adSTejun Heo 		 * Note that some PATA controllers (pata_ali) explode
3455aa2731adSTejun Heo 		 * if status register is read more than once when
3456aa2731adSTejun Heo 		 * there's no device attached.
3457aa2731adSTejun Heo 		 */
3458aa2731adSTejun Heo 		if (ready == -ENODEV) {
3459aa2731adSTejun Heo 			if (ata_link_online(link))
3460aa2731adSTejun Heo 				ready = 0;
3461aa2731adSTejun Heo 			else if ((link->ap->flags & ATA_FLAG_SATA) &&
3462aa2731adSTejun Heo 				 !ata_link_offline(link) &&
3463aa2731adSTejun Heo 				 time_before(now, nodev_deadline))
3464aa2731adSTejun Heo 				ready = 0;
3465aa2731adSTejun Heo 		}
3466aa2731adSTejun Heo 
3467aa2731adSTejun Heo 		if (ready)
3468aa2731adSTejun Heo 			return ready;
3469aa2731adSTejun Heo 		if (time_after(now, deadline))
3470aa2731adSTejun Heo 			return -EBUSY;
3471aa2731adSTejun Heo 
3472aa2731adSTejun Heo 		if (!warned && time_after(now, start + 5 * HZ) &&
3473aa2731adSTejun Heo 		    (deadline - now > 3 * HZ)) {
3474a9a79dfeSJoe Perches 			ata_link_warn(link,
3475aa2731adSTejun Heo 				"link is slow to respond, please be patient "
3476aa2731adSTejun Heo 				"(ready=%d)\n", tmp);
3477aa2731adSTejun Heo 			warned = 1;
3478aa2731adSTejun Heo 		}
3479aa2731adSTejun Heo 
348097750cebSTejun Heo 		ata_msleep(link->ap, 50);
3481aa2731adSTejun Heo 	}
3482aa2731adSTejun Heo }
3483aa2731adSTejun Heo 
3484aa2731adSTejun Heo /**
3485aa2731adSTejun Heo  *	ata_wait_after_reset - wait for link to become ready after reset
3486aa2731adSTejun Heo  *	@link: link to be waited on
3487aa2731adSTejun Heo  *	@deadline: deadline jiffies for the operation
3488aa2731adSTejun Heo  *	@check_ready: callback to check link readiness
3489aa2731adSTejun Heo  *
3490aa2731adSTejun Heo  *	Wait for @link to become ready after reset.
3491aa2731adSTejun Heo  *
3492aa2731adSTejun Heo  *	LOCKING:
3493aa2731adSTejun Heo  *	EH context.
3494aa2731adSTejun Heo  *
3495aa2731adSTejun Heo  *	RETURNS:
3496aa2731adSTejun Heo  *	0 if @linke is ready before @deadline; otherwise, -errno.
3497aa2731adSTejun Heo  */
34982b4221bbSHarvey Harrison int ata_wait_after_reset(struct ata_link *link, unsigned long deadline,
3499aa2731adSTejun Heo 				int (*check_ready)(struct ata_link *link))
3500aa2731adSTejun Heo {
350197750cebSTejun Heo 	ata_msleep(link->ap, ATA_WAIT_AFTER_RESET);
3502aa2731adSTejun Heo 
3503aa2731adSTejun Heo 	return ata_wait_ready(link, deadline, check_ready);
3504aa2731adSTejun Heo }
3505aa2731adSTejun Heo 
3506aa2731adSTejun Heo /**
3507936fd732STejun Heo  *	sata_link_debounce - debounce SATA phy status
3508936fd732STejun Heo  *	@link: ATA link to debounce SATA phy status for
3509c6fd2807SJeff Garzik  *	@params: timing parameters { interval, duratinon, timeout } in msec
3510d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
3511c6fd2807SJeff Garzik  *
3512936fd732STejun Heo  *	Make sure SStatus of @link reaches stable state, determined by
3513c6fd2807SJeff Garzik  *	holding the same value where DET is not 1 for @duration polled
3514c6fd2807SJeff Garzik  *	every @interval, before @timeout.  Timeout constraints the
3515d4b2bab4STejun Heo  *	beginning of the stable state.  Because DET gets stuck at 1 on
3516d4b2bab4STejun Heo  *	some controllers after hot unplugging, this functions waits
3517c6fd2807SJeff Garzik  *	until timeout then returns 0 if DET is stable at 1.
3518c6fd2807SJeff Garzik  *
3519d4b2bab4STejun Heo  *	@timeout is further limited by @deadline.  The sooner of the
3520d4b2bab4STejun Heo  *	two is used.
3521d4b2bab4STejun Heo  *
3522c6fd2807SJeff Garzik  *	LOCKING:
3523c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3524c6fd2807SJeff Garzik  *
3525c6fd2807SJeff Garzik  *	RETURNS:
3526c6fd2807SJeff Garzik  *	0 on success, -errno on failure.
3527c6fd2807SJeff Garzik  */
3528936fd732STejun Heo int sata_link_debounce(struct ata_link *link, const unsigned long *params,
3529d4b2bab4STejun Heo 		       unsigned long deadline)
3530c6fd2807SJeff Garzik {
3531341c2c95STejun Heo 	unsigned long interval = params[0];
3532341c2c95STejun Heo 	unsigned long duration = params[1];
3533d4b2bab4STejun Heo 	unsigned long last_jiffies, t;
3534c6fd2807SJeff Garzik 	u32 last, cur;
3535c6fd2807SJeff Garzik 	int rc;
3536c6fd2807SJeff Garzik 
3537341c2c95STejun Heo 	t = ata_deadline(jiffies, params[2]);
3538d4b2bab4STejun Heo 	if (time_before(t, deadline))
3539d4b2bab4STejun Heo 		deadline = t;
3540d4b2bab4STejun Heo 
3541936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3542c6fd2807SJeff Garzik 		return rc;
3543c6fd2807SJeff Garzik 	cur &= 0xf;
3544c6fd2807SJeff Garzik 
3545c6fd2807SJeff Garzik 	last = cur;
3546c6fd2807SJeff Garzik 	last_jiffies = jiffies;
3547c6fd2807SJeff Garzik 
3548c6fd2807SJeff Garzik 	while (1) {
354997750cebSTejun Heo 		ata_msleep(link->ap, interval);
3550936fd732STejun Heo 		if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3551c6fd2807SJeff Garzik 			return rc;
3552c6fd2807SJeff Garzik 		cur &= 0xf;
3553c6fd2807SJeff Garzik 
3554c6fd2807SJeff Garzik 		/* DET stable? */
3555c6fd2807SJeff Garzik 		if (cur == last) {
3556d4b2bab4STejun Heo 			if (cur == 1 && time_before(jiffies, deadline))
3557c6fd2807SJeff Garzik 				continue;
3558341c2c95STejun Heo 			if (time_after(jiffies,
3559341c2c95STejun Heo 				       ata_deadline(last_jiffies, duration)))
3560c6fd2807SJeff Garzik 				return 0;
3561c6fd2807SJeff Garzik 			continue;
3562c6fd2807SJeff Garzik 		}
3563c6fd2807SJeff Garzik 
3564c6fd2807SJeff Garzik 		/* unstable, start over */
3565c6fd2807SJeff Garzik 		last = cur;
3566c6fd2807SJeff Garzik 		last_jiffies = jiffies;
3567c6fd2807SJeff Garzik 
3568f1545154STejun Heo 		/* Check deadline.  If debouncing failed, return
3569f1545154STejun Heo 		 * -EPIPE to tell upper layer to lower link speed.
3570f1545154STejun Heo 		 */
3571d4b2bab4STejun Heo 		if (time_after(jiffies, deadline))
3572f1545154STejun Heo 			return -EPIPE;
3573c6fd2807SJeff Garzik 	}
3574c6fd2807SJeff Garzik }
3575c6fd2807SJeff Garzik 
3576c6fd2807SJeff Garzik /**
3577936fd732STejun Heo  *	sata_link_resume - resume SATA link
3578936fd732STejun Heo  *	@link: ATA link to resume SATA
3579c6fd2807SJeff Garzik  *	@params: timing parameters { interval, duratinon, timeout } in msec
3580d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
3581c6fd2807SJeff Garzik  *
3582936fd732STejun Heo  *	Resume SATA phy @link and debounce it.
3583c6fd2807SJeff Garzik  *
3584c6fd2807SJeff Garzik  *	LOCKING:
3585c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3586c6fd2807SJeff Garzik  *
3587c6fd2807SJeff Garzik  *	RETURNS:
3588c6fd2807SJeff Garzik  *	0 on success, -errno on failure.
3589c6fd2807SJeff Garzik  */
3590936fd732STejun Heo int sata_link_resume(struct ata_link *link, const unsigned long *params,
3591d4b2bab4STejun Heo 		     unsigned long deadline)
3592c6fd2807SJeff Garzik {
35935040ab67STejun Heo 	int tries = ATA_LINK_RESUME_TRIES;
3594ac371987STejun Heo 	u32 scontrol, serror;
3595c6fd2807SJeff Garzik 	int rc;
3596c6fd2807SJeff Garzik 
3597936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3598c6fd2807SJeff Garzik 		return rc;
3599c6fd2807SJeff Garzik 
36005040ab67STejun Heo 	/*
36015040ab67STejun Heo 	 * Writes to SControl sometimes get ignored under certain
36025040ab67STejun Heo 	 * controllers (ata_piix SIDPR).  Make sure DET actually is
36035040ab67STejun Heo 	 * cleared.
36045040ab67STejun Heo 	 */
36055040ab67STejun Heo 	do {
3606c6fd2807SJeff Garzik 		scontrol = (scontrol & 0x0f0) | 0x300;
3607936fd732STejun Heo 		if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3608c6fd2807SJeff Garzik 			return rc;
36095040ab67STejun Heo 		/*
36105040ab67STejun Heo 		 * Some PHYs react badly if SStatus is pounded
36115040ab67STejun Heo 		 * immediately after resuming.  Delay 200ms before
36125040ab67STejun Heo 		 * debouncing.
3613c6fd2807SJeff Garzik 		 */
361497750cebSTejun Heo 		ata_msleep(link->ap, 200);
3615c6fd2807SJeff Garzik 
36165040ab67STejun Heo 		/* is SControl restored correctly? */
36175040ab67STejun Heo 		if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
36185040ab67STejun Heo 			return rc;
36195040ab67STejun Heo 	} while ((scontrol & 0xf0f) != 0x300 && --tries);
36205040ab67STejun Heo 
36215040ab67STejun Heo 	if ((scontrol & 0xf0f) != 0x300) {
362238941c95STejun Heo 		ata_link_warn(link, "failed to resume link (SControl %X)\n",
36235040ab67STejun Heo 			     scontrol);
36245040ab67STejun Heo 		return 0;
36255040ab67STejun Heo 	}
36265040ab67STejun Heo 
36275040ab67STejun Heo 	if (tries < ATA_LINK_RESUME_TRIES)
3628a9a79dfeSJoe Perches 		ata_link_warn(link, "link resume succeeded after %d retries\n",
36295040ab67STejun Heo 			      ATA_LINK_RESUME_TRIES - tries);
36305040ab67STejun Heo 
3631ac371987STejun Heo 	if ((rc = sata_link_debounce(link, params, deadline)))
3632ac371987STejun Heo 		return rc;
3633ac371987STejun Heo 
3634f046519fSTejun Heo 	/* clear SError, some PHYs require this even for SRST to work */
3635ac371987STejun Heo 	if (!(rc = sata_scr_read(link, SCR_ERROR, &serror)))
3636ac371987STejun Heo 		rc = sata_scr_write(link, SCR_ERROR, serror);
3637ac371987STejun Heo 
3638f046519fSTejun Heo 	return rc != -EINVAL ? rc : 0;
3639c6fd2807SJeff Garzik }
3640c6fd2807SJeff Garzik 
3641c6fd2807SJeff Garzik /**
36421152b261STejun Heo  *	sata_link_scr_lpm - manipulate SControl IPM and SPM fields
36431152b261STejun Heo  *	@link: ATA link to manipulate SControl for
36441152b261STejun Heo  *	@policy: LPM policy to configure
36451152b261STejun Heo  *	@spm_wakeup: initiate LPM transition to active state
36461152b261STejun Heo  *
36471152b261STejun Heo  *	Manipulate the IPM field of the SControl register of @link
36481152b261STejun Heo  *	according to @policy.  If @policy is ATA_LPM_MAX_POWER and
36491152b261STejun Heo  *	@spm_wakeup is %true, the SPM field is manipulated to wake up
36501152b261STejun Heo  *	the link.  This function also clears PHYRDY_CHG before
36511152b261STejun Heo  *	returning.
36521152b261STejun Heo  *
36531152b261STejun Heo  *	LOCKING:
36541152b261STejun Heo  *	EH context.
36551152b261STejun Heo  *
36561152b261STejun Heo  *	RETURNS:
36571152b261STejun Heo  *	0 on succes, -errno otherwise.
36581152b261STejun Heo  */
36591152b261STejun Heo int sata_link_scr_lpm(struct ata_link *link, enum ata_lpm_policy policy,
36601152b261STejun Heo 		      bool spm_wakeup)
36611152b261STejun Heo {
36621152b261STejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
36631152b261STejun Heo 	bool woken_up = false;
36641152b261STejun Heo 	u32 scontrol;
36651152b261STejun Heo 	int rc;
36661152b261STejun Heo 
36671152b261STejun Heo 	rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
36681152b261STejun Heo 	if (rc)
36691152b261STejun Heo 		return rc;
36701152b261STejun Heo 
36711152b261STejun Heo 	switch (policy) {
36721152b261STejun Heo 	case ATA_LPM_MAX_POWER:
36731152b261STejun Heo 		/* disable all LPM transitions */
367465fe1f0fSShane Huang 		scontrol |= (0x7 << 8);
36751152b261STejun Heo 		/* initiate transition to active state */
36761152b261STejun Heo 		if (spm_wakeup) {
36771152b261STejun Heo 			scontrol |= (0x4 << 12);
36781152b261STejun Heo 			woken_up = true;
36791152b261STejun Heo 		}
36801152b261STejun Heo 		break;
36811152b261STejun Heo 	case ATA_LPM_MED_POWER:
36821152b261STejun Heo 		/* allow LPM to PARTIAL */
36831152b261STejun Heo 		scontrol &= ~(0x1 << 8);
368465fe1f0fSShane Huang 		scontrol |= (0x6 << 8);
36851152b261STejun Heo 		break;
36861152b261STejun Heo 	case ATA_LPM_MIN_POWER:
36878a745f1fSKristen Carlson Accardi 		if (ata_link_nr_enabled(link) > 0)
36881152b261STejun Heo 			/* no restrictions on LPM transitions */
368965fe1f0fSShane Huang 			scontrol &= ~(0x7 << 8);
36908a745f1fSKristen Carlson Accardi 		else {
36918a745f1fSKristen Carlson Accardi 			/* empty port, power off */
36928a745f1fSKristen Carlson Accardi 			scontrol &= ~0xf;
36938a745f1fSKristen Carlson Accardi 			scontrol |= (0x1 << 2);
36948a745f1fSKristen Carlson Accardi 		}
36951152b261STejun Heo 		break;
36961152b261STejun Heo 	default:
36971152b261STejun Heo 		WARN_ON(1);
36981152b261STejun Heo 	}
36991152b261STejun Heo 
37001152b261STejun Heo 	rc = sata_scr_write(link, SCR_CONTROL, scontrol);
37011152b261STejun Heo 	if (rc)
37021152b261STejun Heo 		return rc;
37031152b261STejun Heo 
37041152b261STejun Heo 	/* give the link time to transit out of LPM state */
37051152b261STejun Heo 	if (woken_up)
37061152b261STejun Heo 		msleep(10);
37071152b261STejun Heo 
37081152b261STejun Heo 	/* clear PHYRDY_CHG from SError */
37091152b261STejun Heo 	ehc->i.serror &= ~SERR_PHYRDY_CHG;
37101152b261STejun Heo 	return sata_scr_write(link, SCR_ERROR, SERR_PHYRDY_CHG);
37111152b261STejun Heo }
37121152b261STejun Heo 
37131152b261STejun Heo /**
37140aa1113dSTejun Heo  *	ata_std_prereset - prepare for reset
3715cc0680a5STejun Heo  *	@link: ATA link to be reset
3716d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
3717c6fd2807SJeff Garzik  *
3718cc0680a5STejun Heo  *	@link is about to be reset.  Initialize it.  Failure from
3719b8cffc6aSTejun Heo  *	prereset makes libata abort whole reset sequence and give up
3720b8cffc6aSTejun Heo  *	that port, so prereset should be best-effort.  It does its
3721b8cffc6aSTejun Heo  *	best to prepare for reset sequence but if things go wrong, it
3722b8cffc6aSTejun Heo  *	should just whine, not fail.
3723c6fd2807SJeff Garzik  *
3724c6fd2807SJeff Garzik  *	LOCKING:
3725c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3726c6fd2807SJeff Garzik  *
3727c6fd2807SJeff Garzik  *	RETURNS:
3728c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
3729c6fd2807SJeff Garzik  */
37300aa1113dSTejun Heo int ata_std_prereset(struct ata_link *link, unsigned long deadline)
3731c6fd2807SJeff Garzik {
3732cc0680a5STejun Heo 	struct ata_port *ap = link->ap;
3733936fd732STejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
3734c6fd2807SJeff Garzik 	const unsigned long *timing = sata_ehc_deb_timing(ehc);
3735c6fd2807SJeff Garzik 	int rc;
3736c6fd2807SJeff Garzik 
3737c6fd2807SJeff Garzik 	/* if we're about to do hardreset, nothing more to do */
3738c6fd2807SJeff Garzik 	if (ehc->i.action & ATA_EH_HARDRESET)
3739c6fd2807SJeff Garzik 		return 0;
3740c6fd2807SJeff Garzik 
3741936fd732STejun Heo 	/* if SATA, resume link */
3742a16abc0bSTejun Heo 	if (ap->flags & ATA_FLAG_SATA) {
3743936fd732STejun Heo 		rc = sata_link_resume(link, timing, deadline);
3744b8cffc6aSTejun Heo 		/* whine about phy resume failure but proceed */
3745b8cffc6aSTejun Heo 		if (rc && rc != -EOPNOTSUPP)
3746a9a79dfeSJoe Perches 			ata_link_warn(link,
3747a9a79dfeSJoe Perches 				      "failed to resume link for reset (errno=%d)\n",
3748a9a79dfeSJoe Perches 				      rc);
3749c6fd2807SJeff Garzik 	}
3750c6fd2807SJeff Garzik 
375145db2f6cSTejun Heo 	/* no point in trying softreset on offline link */
3752b1c72916STejun Heo 	if (ata_phys_link_offline(link))
375345db2f6cSTejun Heo 		ehc->i.action &= ~ATA_EH_SOFTRESET;
375445db2f6cSTejun Heo 
3755c6fd2807SJeff Garzik 	return 0;
3756c6fd2807SJeff Garzik }
3757c6fd2807SJeff Garzik 
3758c6fd2807SJeff Garzik /**
3759cc0680a5STejun Heo  *	sata_link_hardreset - reset link via SATA phy reset
3760cc0680a5STejun Heo  *	@link: link to reset
3761b6103f6dSTejun Heo  *	@timing: timing parameters { interval, duratinon, timeout } in msec
3762d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
37639dadd45bSTejun Heo  *	@online: optional out parameter indicating link onlineness
37649dadd45bSTejun Heo  *	@check_ready: optional callback to check link readiness
3765c6fd2807SJeff Garzik  *
3766cc0680a5STejun Heo  *	SATA phy-reset @link using DET bits of SControl register.
37679dadd45bSTejun Heo  *	After hardreset, link readiness is waited upon using
37689dadd45bSTejun Heo  *	ata_wait_ready() if @check_ready is specified.  LLDs are
37699dadd45bSTejun Heo  *	allowed to not specify @check_ready and wait itself after this
37709dadd45bSTejun Heo  *	function returns.  Device classification is LLD's
37719dadd45bSTejun Heo  *	responsibility.
37729dadd45bSTejun Heo  *
37739dadd45bSTejun Heo  *	*@online is set to one iff reset succeeded and @link is online
37749dadd45bSTejun Heo  *	after reset.
3775c6fd2807SJeff Garzik  *
3776c6fd2807SJeff Garzik  *	LOCKING:
3777c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3778c6fd2807SJeff Garzik  *
3779c6fd2807SJeff Garzik  *	RETURNS:
3780c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
3781c6fd2807SJeff Garzik  */
3782cc0680a5STejun Heo int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
37839dadd45bSTejun Heo 			unsigned long deadline,
37849dadd45bSTejun Heo 			bool *online, int (*check_ready)(struct ata_link *))
3785c6fd2807SJeff Garzik {
3786c6fd2807SJeff Garzik 	u32 scontrol;
3787c6fd2807SJeff Garzik 	int rc;
3788c6fd2807SJeff Garzik 
3789c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
3790c6fd2807SJeff Garzik 
37919dadd45bSTejun Heo 	if (online)
37929dadd45bSTejun Heo 		*online = false;
37939dadd45bSTejun Heo 
3794936fd732STejun Heo 	if (sata_set_spd_needed(link)) {
3795c6fd2807SJeff Garzik 		/* SATA spec says nothing about how to reconfigure
3796c6fd2807SJeff Garzik 		 * spd.  To be on the safe side, turn off phy during
3797c6fd2807SJeff Garzik 		 * reconfiguration.  This works for at least ICH7 AHCI
3798c6fd2807SJeff Garzik 		 * and Sil3124.
3799c6fd2807SJeff Garzik 		 */
3800936fd732STejun Heo 		if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3801b6103f6dSTejun Heo 			goto out;
3802c6fd2807SJeff Garzik 
3803cea0d336SJeff Garzik 		scontrol = (scontrol & 0x0f0) | 0x304;
3804c6fd2807SJeff Garzik 
3805936fd732STejun Heo 		if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3806b6103f6dSTejun Heo 			goto out;
3807c6fd2807SJeff Garzik 
3808936fd732STejun Heo 		sata_set_spd(link);
3809c6fd2807SJeff Garzik 	}
3810c6fd2807SJeff Garzik 
3811c6fd2807SJeff Garzik 	/* issue phy wake/reset */
3812936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3813b6103f6dSTejun Heo 		goto out;
3814c6fd2807SJeff Garzik 
3815c6fd2807SJeff Garzik 	scontrol = (scontrol & 0x0f0) | 0x301;
3816c6fd2807SJeff Garzik 
3817936fd732STejun Heo 	if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
3818b6103f6dSTejun Heo 		goto out;
3819c6fd2807SJeff Garzik 
3820c6fd2807SJeff Garzik 	/* Couldn't find anything in SATA I/II specs, but AHCI-1.1
3821c6fd2807SJeff Garzik 	 * 10.4.2 says at least 1 ms.
3822c6fd2807SJeff Garzik 	 */
382397750cebSTejun Heo 	ata_msleep(link->ap, 1);
3824c6fd2807SJeff Garzik 
3825936fd732STejun Heo 	/* bring link back */
3826936fd732STejun Heo 	rc = sata_link_resume(link, timing, deadline);
38279dadd45bSTejun Heo 	if (rc)
38289dadd45bSTejun Heo 		goto out;
38299dadd45bSTejun Heo 	/* if link is offline nothing more to do */
3830b1c72916STejun Heo 	if (ata_phys_link_offline(link))
38319dadd45bSTejun Heo 		goto out;
38329dadd45bSTejun Heo 
38339dadd45bSTejun Heo 	/* Link is online.  From this point, -ENODEV too is an error. */
38349dadd45bSTejun Heo 	if (online)
38359dadd45bSTejun Heo 		*online = true;
38369dadd45bSTejun Heo 
3837071f44b1STejun Heo 	if (sata_pmp_supported(link->ap) && ata_is_host_link(link)) {
38389dadd45bSTejun Heo 		/* If PMP is supported, we have to do follow-up SRST.
38399dadd45bSTejun Heo 		 * Some PMPs don't send D2H Reg FIS after hardreset if
38409dadd45bSTejun Heo 		 * the first port is empty.  Wait only for
38419dadd45bSTejun Heo 		 * ATA_TMOUT_PMP_SRST_WAIT.
38429dadd45bSTejun Heo 		 */
38439dadd45bSTejun Heo 		if (check_ready) {
38449dadd45bSTejun Heo 			unsigned long pmp_deadline;
38459dadd45bSTejun Heo 
3846341c2c95STejun Heo 			pmp_deadline = ata_deadline(jiffies,
3847341c2c95STejun Heo 						    ATA_TMOUT_PMP_SRST_WAIT);
38489dadd45bSTejun Heo 			if (time_after(pmp_deadline, deadline))
38499dadd45bSTejun Heo 				pmp_deadline = deadline;
38509dadd45bSTejun Heo 			ata_wait_ready(link, pmp_deadline, check_ready);
38519dadd45bSTejun Heo 		}
38529dadd45bSTejun Heo 		rc = -EAGAIN;
38539dadd45bSTejun Heo 		goto out;
38549dadd45bSTejun Heo 	}
38559dadd45bSTejun Heo 
38569dadd45bSTejun Heo 	rc = 0;
38579dadd45bSTejun Heo 	if (check_ready)
38589dadd45bSTejun Heo 		rc = ata_wait_ready(link, deadline, check_ready);
3859b6103f6dSTejun Heo  out:
38600cbf0711STejun Heo 	if (rc && rc != -EAGAIN) {
38610cbf0711STejun Heo 		/* online is set iff link is online && reset succeeded */
38620cbf0711STejun Heo 		if (online)
38630cbf0711STejun Heo 			*online = false;
3864a9a79dfeSJoe Perches 		ata_link_err(link, "COMRESET failed (errno=%d)\n", rc);
38650cbf0711STejun Heo 	}
3866b6103f6dSTejun Heo 	DPRINTK("EXIT, rc=%d\n", rc);
3867b6103f6dSTejun Heo 	return rc;
3868b6103f6dSTejun Heo }
3869b6103f6dSTejun Heo 
3870b6103f6dSTejun Heo /**
387157c9efdfSTejun Heo  *	sata_std_hardreset - COMRESET w/o waiting or classification
387257c9efdfSTejun Heo  *	@link: link to reset
387357c9efdfSTejun Heo  *	@class: resulting class of attached device
387457c9efdfSTejun Heo  *	@deadline: deadline jiffies for the operation
387557c9efdfSTejun Heo  *
387657c9efdfSTejun Heo  *	Standard SATA COMRESET w/o waiting or classification.
387757c9efdfSTejun Heo  *
387857c9efdfSTejun Heo  *	LOCKING:
387957c9efdfSTejun Heo  *	Kernel thread context (may sleep)
388057c9efdfSTejun Heo  *
388157c9efdfSTejun Heo  *	RETURNS:
388257c9efdfSTejun Heo  *	0 if link offline, -EAGAIN if link online, -errno on errors.
388357c9efdfSTejun Heo  */
388457c9efdfSTejun Heo int sata_std_hardreset(struct ata_link *link, unsigned int *class,
388557c9efdfSTejun Heo 		       unsigned long deadline)
388657c9efdfSTejun Heo {
388757c9efdfSTejun Heo 	const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
388857c9efdfSTejun Heo 	bool online;
388957c9efdfSTejun Heo 	int rc;
389057c9efdfSTejun Heo 
389157c9efdfSTejun Heo 	/* do hardreset */
389257c9efdfSTejun Heo 	rc = sata_link_hardreset(link, timing, deadline, &online, NULL);
389357c9efdfSTejun Heo 	return online ? -EAGAIN : rc;
389457c9efdfSTejun Heo }
389557c9efdfSTejun Heo 
389657c9efdfSTejun Heo /**
3897203c75b8STejun Heo  *	ata_std_postreset - standard postreset callback
3898cc0680a5STejun Heo  *	@link: the target ata_link
3899c6fd2807SJeff Garzik  *	@classes: classes of attached devices
3900c6fd2807SJeff Garzik  *
3901c6fd2807SJeff Garzik  *	This function is invoked after a successful reset.  Note that
3902c6fd2807SJeff Garzik  *	the device might have been reset more than once using
3903c6fd2807SJeff Garzik  *	different reset methods before postreset is invoked.
3904c6fd2807SJeff Garzik  *
3905c6fd2807SJeff Garzik  *	LOCKING:
3906c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3907c6fd2807SJeff Garzik  */
3908203c75b8STejun Heo void ata_std_postreset(struct ata_link *link, unsigned int *classes)
3909c6fd2807SJeff Garzik {
3910f046519fSTejun Heo 	u32 serror;
3911f046519fSTejun Heo 
3912c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
3913c6fd2807SJeff Garzik 
3914f046519fSTejun Heo 	/* reset complete, clear SError */
3915f046519fSTejun Heo 	if (!sata_scr_read(link, SCR_ERROR, &serror))
3916f046519fSTejun Heo 		sata_scr_write(link, SCR_ERROR, serror);
3917f046519fSTejun Heo 
3918c6fd2807SJeff Garzik 	/* print link status */
3919936fd732STejun Heo 	sata_print_link_status(link);
3920c6fd2807SJeff Garzik 
3921c6fd2807SJeff Garzik 	DPRINTK("EXIT\n");
3922c6fd2807SJeff Garzik }
3923c6fd2807SJeff Garzik 
3924c6fd2807SJeff Garzik /**
3925c6fd2807SJeff Garzik  *	ata_dev_same_device - Determine whether new ID matches configured device
3926c6fd2807SJeff Garzik  *	@dev: device to compare against
3927c6fd2807SJeff Garzik  *	@new_class: class of the new device
3928c6fd2807SJeff Garzik  *	@new_id: IDENTIFY page of the new device
3929c6fd2807SJeff Garzik  *
3930c6fd2807SJeff Garzik  *	Compare @new_class and @new_id against @dev and determine
3931c6fd2807SJeff Garzik  *	whether @dev is the device indicated by @new_class and
3932c6fd2807SJeff Garzik  *	@new_id.
3933c6fd2807SJeff Garzik  *
3934c6fd2807SJeff Garzik  *	LOCKING:
3935c6fd2807SJeff Garzik  *	None.
3936c6fd2807SJeff Garzik  *
3937c6fd2807SJeff Garzik  *	RETURNS:
3938c6fd2807SJeff Garzik  *	1 if @dev matches @new_class and @new_id, 0 otherwise.
3939c6fd2807SJeff Garzik  */
3940c6fd2807SJeff Garzik static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
3941c6fd2807SJeff Garzik 			       const u16 *new_id)
3942c6fd2807SJeff Garzik {
3943c6fd2807SJeff Garzik 	const u16 *old_id = dev->id;
3944a0cf733bSTejun Heo 	unsigned char model[2][ATA_ID_PROD_LEN + 1];
3945a0cf733bSTejun Heo 	unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
3946c6fd2807SJeff Garzik 
3947c6fd2807SJeff Garzik 	if (dev->class != new_class) {
3948a9a79dfeSJoe Perches 		ata_dev_info(dev, "class mismatch %d != %d\n",
3949c6fd2807SJeff Garzik 			     dev->class, new_class);
3950c6fd2807SJeff Garzik 		return 0;
3951c6fd2807SJeff Garzik 	}
3952c6fd2807SJeff Garzik 
3953a0cf733bSTejun Heo 	ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
3954a0cf733bSTejun Heo 	ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
3955a0cf733bSTejun Heo 	ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
3956a0cf733bSTejun Heo 	ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
3957c6fd2807SJeff Garzik 
3958c6fd2807SJeff Garzik 	if (strcmp(model[0], model[1])) {
3959a9a79dfeSJoe Perches 		ata_dev_info(dev, "model number mismatch '%s' != '%s'\n",
3960a9a79dfeSJoe Perches 			     model[0], model[1]);
3961c6fd2807SJeff Garzik 		return 0;
3962c6fd2807SJeff Garzik 	}
3963c6fd2807SJeff Garzik 
3964c6fd2807SJeff Garzik 	if (strcmp(serial[0], serial[1])) {
3965a9a79dfeSJoe Perches 		ata_dev_info(dev, "serial number mismatch '%s' != '%s'\n",
3966a9a79dfeSJoe Perches 			     serial[0], serial[1]);
3967c6fd2807SJeff Garzik 		return 0;
3968c6fd2807SJeff Garzik 	}
3969c6fd2807SJeff Garzik 
3970c6fd2807SJeff Garzik 	return 1;
3971c6fd2807SJeff Garzik }
3972c6fd2807SJeff Garzik 
3973c6fd2807SJeff Garzik /**
3974fe30911bSTejun Heo  *	ata_dev_reread_id - Re-read IDENTIFY data
39753fae450cSHenrik Kretzschmar  *	@dev: target ATA device
3976bff04647STejun Heo  *	@readid_flags: read ID flags
3977c6fd2807SJeff Garzik  *
3978c6fd2807SJeff Garzik  *	Re-read IDENTIFY page and make sure @dev is still attached to
3979c6fd2807SJeff Garzik  *	the port.
3980c6fd2807SJeff Garzik  *
3981c6fd2807SJeff Garzik  *	LOCKING:
3982c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3983c6fd2807SJeff Garzik  *
3984c6fd2807SJeff Garzik  *	RETURNS:
3985c6fd2807SJeff Garzik  *	0 on success, negative errno otherwise
3986c6fd2807SJeff Garzik  */
3987fe30911bSTejun Heo int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
3988c6fd2807SJeff Garzik {
3989c6fd2807SJeff Garzik 	unsigned int class = dev->class;
39909af5c9c9STejun Heo 	u16 *id = (void *)dev->link->ap->sector_buf;
3991c6fd2807SJeff Garzik 	int rc;
3992c6fd2807SJeff Garzik 
3993c6fd2807SJeff Garzik 	/* read ID data */
3994bff04647STejun Heo 	rc = ata_dev_read_id(dev, &class, readid_flags, id);
3995c6fd2807SJeff Garzik 	if (rc)
3996fe30911bSTejun Heo 		return rc;
3997c6fd2807SJeff Garzik 
3998c6fd2807SJeff Garzik 	/* is the device still there? */
3999fe30911bSTejun Heo 	if (!ata_dev_same_device(dev, class, id))
4000fe30911bSTejun Heo 		return -ENODEV;
4001c6fd2807SJeff Garzik 
4002c6fd2807SJeff Garzik 	memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
4003fe30911bSTejun Heo 	return 0;
4004fe30911bSTejun Heo }
4005fe30911bSTejun Heo 
4006fe30911bSTejun Heo /**
4007fe30911bSTejun Heo  *	ata_dev_revalidate - Revalidate ATA device
4008fe30911bSTejun Heo  *	@dev: device to revalidate
4009422c9daaSTejun Heo  *	@new_class: new class code
4010fe30911bSTejun Heo  *	@readid_flags: read ID flags
4011fe30911bSTejun Heo  *
4012fe30911bSTejun Heo  *	Re-read IDENTIFY page, make sure @dev is still attached to the
4013fe30911bSTejun Heo  *	port and reconfigure it according to the new IDENTIFY page.
4014fe30911bSTejun Heo  *
4015fe30911bSTejun Heo  *	LOCKING:
4016fe30911bSTejun Heo  *	Kernel thread context (may sleep)
4017fe30911bSTejun Heo  *
4018fe30911bSTejun Heo  *	RETURNS:
4019fe30911bSTejun Heo  *	0 on success, negative errno otherwise
4020fe30911bSTejun Heo  */
4021422c9daaSTejun Heo int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
4022422c9daaSTejun Heo 		       unsigned int readid_flags)
4023fe30911bSTejun Heo {
40246ddcd3b0STejun Heo 	u64 n_sectors = dev->n_sectors;
40255920dadfSTejun Heo 	u64 n_native_sectors = dev->n_native_sectors;
4026fe30911bSTejun Heo 	int rc;
4027fe30911bSTejun Heo 
4028fe30911bSTejun Heo 	if (!ata_dev_enabled(dev))
4029fe30911bSTejun Heo 		return -ENODEV;
4030fe30911bSTejun Heo 
4031422c9daaSTejun Heo 	/* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
4032422c9daaSTejun Heo 	if (ata_class_enabled(new_class) &&
4033f0d0613dSBorislav Petkov 	    new_class != ATA_DEV_ATA &&
4034f0d0613dSBorislav Petkov 	    new_class != ATA_DEV_ATAPI &&
4035f0d0613dSBorislav Petkov 	    new_class != ATA_DEV_SEMB) {
4036a9a79dfeSJoe Perches 		ata_dev_info(dev, "class mismatch %u != %u\n",
4037422c9daaSTejun Heo 			     dev->class, new_class);
4038422c9daaSTejun Heo 		rc = -ENODEV;
4039422c9daaSTejun Heo 		goto fail;
4040422c9daaSTejun Heo 	}
4041422c9daaSTejun Heo 
4042fe30911bSTejun Heo 	/* re-read ID */
4043fe30911bSTejun Heo 	rc = ata_dev_reread_id(dev, readid_flags);
4044fe30911bSTejun Heo 	if (rc)
4045fe30911bSTejun Heo 		goto fail;
4046c6fd2807SJeff Garzik 
4047c6fd2807SJeff Garzik 	/* configure device according to the new ID */
4048efdaedc4STejun Heo 	rc = ata_dev_configure(dev);
40496ddcd3b0STejun Heo 	if (rc)
40506ddcd3b0STejun Heo 		goto fail;
40516ddcd3b0STejun Heo 
40526ddcd3b0STejun Heo 	/* verify n_sectors hasn't changed */
4053445d211bSTejun Heo 	if (dev->class != ATA_DEV_ATA || !n_sectors ||
4054445d211bSTejun Heo 	    dev->n_sectors == n_sectors)
4055445d211bSTejun Heo 		return 0;
4056445d211bSTejun Heo 
4057445d211bSTejun Heo 	/* n_sectors has changed */
4058a9a79dfeSJoe Perches 	ata_dev_warn(dev, "n_sectors mismatch %llu != %llu\n",
40596ddcd3b0STejun Heo 		     (unsigned long long)n_sectors,
40606ddcd3b0STejun Heo 		     (unsigned long long)dev->n_sectors);
4061445d211bSTejun Heo 
40625920dadfSTejun Heo 	/*
40635920dadfSTejun Heo 	 * Something could have caused HPA to be unlocked
4064445d211bSTejun Heo 	 * involuntarily.  If n_native_sectors hasn't changed and the
4065445d211bSTejun Heo 	 * new size matches it, keep the device.
40665920dadfSTejun Heo 	 */
40675920dadfSTejun Heo 	if (dev->n_native_sectors == n_native_sectors &&
4068445d211bSTejun Heo 	    dev->n_sectors > n_sectors && dev->n_sectors == n_native_sectors) {
4069a9a79dfeSJoe Perches 		ata_dev_warn(dev,
40705920dadfSTejun Heo 			     "new n_sectors matches native, probably "
407168939ce5STejun Heo 			     "late HPA unlock, n_sectors updated\n");
407268939ce5STejun Heo 		/* use the larger n_sectors */
4073445d211bSTejun Heo 		return 0;
4074445d211bSTejun Heo 	}
4075445d211bSTejun Heo 
4076445d211bSTejun Heo 	/*
4077445d211bSTejun Heo 	 * Some BIOSes boot w/o HPA but resume w/ HPA locked.  Try
4078445d211bSTejun Heo 	 * unlocking HPA in those cases.
4079445d211bSTejun Heo 	 *
4080445d211bSTejun Heo 	 * https://bugzilla.kernel.org/show_bug.cgi?id=15396
4081445d211bSTejun Heo 	 */
4082445d211bSTejun Heo 	if (dev->n_native_sectors == n_native_sectors &&
4083445d211bSTejun Heo 	    dev->n_sectors < n_sectors && n_sectors == n_native_sectors &&
4084445d211bSTejun Heo 	    !(dev->horkage & ATA_HORKAGE_BROKEN_HPA)) {
4085a9a79dfeSJoe Perches 		ata_dev_warn(dev,
4086445d211bSTejun Heo 			     "old n_sectors matches native, probably "
4087445d211bSTejun Heo 			     "late HPA lock, will try to unlock HPA\n");
4088445d211bSTejun Heo 		/* try unlocking HPA */
4089445d211bSTejun Heo 		dev->flags |= ATA_DFLAG_UNLOCK_HPA;
4090445d211bSTejun Heo 		rc = -EIO;
4091445d211bSTejun Heo 	} else
4092445d211bSTejun Heo 		rc = -ENODEV;
4093445d211bSTejun Heo 
4094445d211bSTejun Heo 	/* restore original n_[native_]sectors and fail */
40955920dadfSTejun Heo 	dev->n_native_sectors = n_native_sectors;
40965920dadfSTejun Heo 	dev->n_sectors = n_sectors;
4097c6fd2807SJeff Garzik  fail:
4098a9a79dfeSJoe Perches 	ata_dev_err(dev, "revalidation failed (errno=%d)\n", rc);
4099c6fd2807SJeff Garzik 	return rc;
4100c6fd2807SJeff Garzik }
4101c6fd2807SJeff Garzik 
41026919a0a6SAlan Cox struct ata_blacklist_entry {
41036919a0a6SAlan Cox 	const char *model_num;
41046919a0a6SAlan Cox 	const char *model_rev;
41056919a0a6SAlan Cox 	unsigned long horkage;
41066919a0a6SAlan Cox };
41076919a0a6SAlan Cox 
41086919a0a6SAlan Cox static const struct ata_blacklist_entry ata_device_blacklist [] = {
41096919a0a6SAlan Cox 	/* Devices with DMA related problems under Linux */
41106919a0a6SAlan Cox 	{ "WDC AC11000H",	NULL,		ATA_HORKAGE_NODMA },
41116919a0a6SAlan Cox 	{ "WDC AC22100H",	NULL,		ATA_HORKAGE_NODMA },
41126919a0a6SAlan Cox 	{ "WDC AC32500H",	NULL,		ATA_HORKAGE_NODMA },
41136919a0a6SAlan Cox 	{ "WDC AC33100H",	NULL,		ATA_HORKAGE_NODMA },
41146919a0a6SAlan Cox 	{ "WDC AC31600H",	NULL,		ATA_HORKAGE_NODMA },
41156919a0a6SAlan Cox 	{ "WDC AC32100H",	"24.09P07",	ATA_HORKAGE_NODMA },
41166919a0a6SAlan Cox 	{ "WDC AC23200L",	"21.10N21",	ATA_HORKAGE_NODMA },
41176919a0a6SAlan Cox 	{ "Compaq CRD-8241B", 	NULL,		ATA_HORKAGE_NODMA },
41186919a0a6SAlan Cox 	{ "CRD-8400B",		NULL, 		ATA_HORKAGE_NODMA },
41197da4c935SMark Lord 	{ "CRD-848[02]B",	NULL,		ATA_HORKAGE_NODMA },
41206919a0a6SAlan Cox 	{ "CRD-84",		NULL,		ATA_HORKAGE_NODMA },
41216919a0a6SAlan Cox 	{ "SanDisk SDP3B",	NULL,		ATA_HORKAGE_NODMA },
41226919a0a6SAlan Cox 	{ "SanDisk SDP3B-64",	NULL,		ATA_HORKAGE_NODMA },
41236919a0a6SAlan Cox 	{ "SANYO CD-ROM CRD",	NULL,		ATA_HORKAGE_NODMA },
41246919a0a6SAlan Cox 	{ "HITACHI CDR-8",	NULL,		ATA_HORKAGE_NODMA },
41257da4c935SMark Lord 	{ "HITACHI CDR-8[34]35",NULL,		ATA_HORKAGE_NODMA },
41266919a0a6SAlan Cox 	{ "Toshiba CD-ROM XM-6202B", NULL,	ATA_HORKAGE_NODMA },
41276919a0a6SAlan Cox 	{ "TOSHIBA CD-ROM XM-1702BC", NULL,	ATA_HORKAGE_NODMA },
41286919a0a6SAlan Cox 	{ "CD-532E-A", 		NULL,		ATA_HORKAGE_NODMA },
41296919a0a6SAlan Cox 	{ "E-IDE CD-ROM CR-840",NULL,		ATA_HORKAGE_NODMA },
41306919a0a6SAlan Cox 	{ "CD-ROM Drive/F5A",	NULL,		ATA_HORKAGE_NODMA },
41316919a0a6SAlan Cox 	{ "WPI CDD-820", 	NULL,		ATA_HORKAGE_NODMA },
41326919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SC-148C", NULL,	ATA_HORKAGE_NODMA },
41336919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SC",	NULL,		ATA_HORKAGE_NODMA },
41346919a0a6SAlan Cox 	{ "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
41356919a0a6SAlan Cox 	{ "_NEC DV5800A", 	NULL,		ATA_HORKAGE_NODMA },
41366919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SN-124", "N001",	ATA_HORKAGE_NODMA },
413739f19886SDave Jones 	{ "Seagate STT20000A", NULL,		ATA_HORKAGE_NODMA },
4138d70e551cSPrarit Bhargava 	{ " 2GB ATA Flash Disk", "ADMA428M",	ATA_HORKAGE_NODMA },
41393af9a77aSTejun Heo 	/* Odd clown on sil3726/4726 PMPs */
414050af2fa1STejun Heo 	{ "Config  Disk",	NULL,		ATA_HORKAGE_DISABLE },
41416919a0a6SAlan Cox 
414218d6e9d5SAlbert Lee 	/* Weird ATAPI devices */
414340a1d531STejun Heo 	{ "TORiSAN DVD-ROM DRD-N216", NULL,	ATA_HORKAGE_MAX_SEC_128 },
41446a87e42eSTejun Heo 	{ "QUANTUM DAT    DAT72-000", NULL,	ATA_HORKAGE_ATAPI_MOD16_DMA },
4145a32450e1SShan Hai 	{ "Slimtype DVD A  DS8A8SH", NULL,	ATA_HORKAGE_MAX_SEC_LBA48 },
41460523f037SShan Hai 	{ "Slimtype DVD A  DS8A9SH", NULL,	ATA_HORKAGE_MAX_SEC_LBA48 },
414718d6e9d5SAlbert Lee 
41486919a0a6SAlan Cox 	/* Devices we expect to fail diagnostics */
41496919a0a6SAlan Cox 
41506919a0a6SAlan Cox 	/* Devices where NCQ should be avoided */
41516919a0a6SAlan Cox 	/* NCQ is slow */
41526919a0a6SAlan Cox 	{ "WDC WD740ADFD-00",	NULL,		ATA_HORKAGE_NONCQ },
4153459ad688STejun Heo 	{ "WDC WD740ADFD-00NLR1", NULL,		ATA_HORKAGE_NONCQ, },
415409125ea6STejun Heo 	/* http://thread.gmane.org/gmane.linux.ide/14907 */
415509125ea6STejun Heo 	{ "FUJITSU MHT2060BH",	NULL,		ATA_HORKAGE_NONCQ },
41567acfaf30SPaul Rolland 	/* NCQ is broken */
4157539cc7c7SJeff Garzik 	{ "Maxtor *",		"BANC*",	ATA_HORKAGE_NONCQ },
41580e3dbc01SAlan Cox 	{ "Maxtor 7V300F0",	"VA111630",	ATA_HORKAGE_NONCQ },
4159da6f0ec2SPaolo Ornati 	{ "ST380817AS",		"3.42",		ATA_HORKAGE_NONCQ },
4160e41bd3e8STejun Heo 	{ "ST3160023AS",	"3.42",		ATA_HORKAGE_NONCQ },
41615ccfca97SLubomir Bulej 	{ "OCZ CORE_SSD",	"02.10104",	ATA_HORKAGE_NONCQ },
4162539cc7c7SJeff Garzik 
4163ac70a964STejun Heo 	/* Seagate NCQ + FLUSH CACHE firmware bug */
41644d1f9082SMark Lord 	{ "ST31500341AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
4165ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4166d10d491fSTejun Heo 
41674d1f9082SMark Lord 	{ "ST31000333AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
4168d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4169d10d491fSTejun Heo 
41704d1f9082SMark Lord 	{ "ST3640[36]23AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
4171d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4172d10d491fSTejun Heo 
41734d1f9082SMark Lord 	{ "ST3320[68]13AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
4174ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4175ac70a964STejun Heo 
417687809942SMichele Baldessari 	/* Seagate Momentus SpinPoint M8 seem to have FPMDA_AA issues */
417787809942SMichele Baldessari 	{ "ST1000LM024 HN-M101MBB", "2AR10001",	ATA_HORKAGE_BROKEN_FPDMA_AA },
4178b28a613eSMichele Baldessari 	{ "ST1000LM024 HN-M101MBB", "2BA30001",	ATA_HORKAGE_BROKEN_FPDMA_AA },
417987809942SMichele Baldessari 
418036e337d0SRobert Hancock 	/* Blacklist entries taken from Silicon Image 3124/3132
418136e337d0SRobert Hancock 	   Windows driver .inf file - also several Linux problem reports */
418236e337d0SRobert Hancock 	{ "HTS541060G9SA00",    "MB3OC60D",     ATA_HORKAGE_NONCQ, },
418336e337d0SRobert Hancock 	{ "HTS541080G9SA00",    "MB4OC60D",     ATA_HORKAGE_NONCQ, },
418436e337d0SRobert Hancock 	{ "HTS541010G9SA00",    "MBZOC60D",     ATA_HORKAGE_NONCQ, },
41856919a0a6SAlan Cox 
418668b0ddb2STejun Heo 	/* https://bugzilla.kernel.org/show_bug.cgi?id=15573 */
418768b0ddb2STejun Heo 	{ "C300-CTFDDAC128MAG",	"0001",		ATA_HORKAGE_NONCQ, },
418868b0ddb2STejun Heo 
418916c55b03STejun Heo 	/* devices which puke on READ_NATIVE_MAX */
419016c55b03STejun Heo 	{ "HDS724040KLSA80",	"KFAOA20N",	ATA_HORKAGE_BROKEN_HPA, },
419116c55b03STejun Heo 	{ "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
419216c55b03STejun Heo 	{ "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
419316c55b03STejun Heo 	{ "MAXTOR 6L080L4",	"A93.0500",	ATA_HORKAGE_BROKEN_HPA },
41946919a0a6SAlan Cox 
41957831387bSTejun Heo 	/* this one allows HPA unlocking but fails IOs on the area */
41967831387bSTejun Heo 	{ "OCZ-VERTEX",		    "1.30",	ATA_HORKAGE_BROKEN_HPA },
41977831387bSTejun Heo 
419893328e11SAlan Cox 	/* Devices which report 1 sector over size HPA */
419993328e11SAlan Cox 	{ "ST340823A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
420093328e11SAlan Cox 	{ "ST320413A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
4201b152fcd3SMikko Rapeli 	{ "ST310211A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
420293328e11SAlan Cox 
42036bbfd53dSAlan Cox 	/* Devices which get the IVB wrong */
42046bbfd53dSAlan Cox 	{ "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB, },
4205a79067e5SAlan Cox 	/* Maybe we should just blacklist TSSTcorp... */
42067da4c935SMark Lord 	{ "TSSTcorp CDDVDW SH-S202[HJN]", "SB0[01]",  ATA_HORKAGE_IVB, },
42076bbfd53dSAlan Cox 
42089ce8e307SJens Axboe 	/* Devices that do not need bridging limits applied */
42099ce8e307SJens Axboe 	{ "MTRON MSP-SATA*",		NULL,	ATA_HORKAGE_BRIDGE_OK, },
421004d0f1b8SJeff Garzik 	{ "BUFFALO HD-QSU2/R5",		NULL,	ATA_HORKAGE_BRIDGE_OK, },
42119ce8e307SJens Axboe 
42129062712fSTejun Heo 	/* Devices which aren't very happy with higher link speeds */
42139062712fSTejun Heo 	{ "WD My Book",			NULL,	ATA_HORKAGE_1_5_GBPS, },
4214c531077fSDaniel J Blueman 	{ "Seagate FreeAgent GoFlex",	NULL,	ATA_HORKAGE_1_5_GBPS, },
42159062712fSTejun Heo 
4216d0cb43b3STejun Heo 	/*
4217d0cb43b3STejun Heo 	 * Devices which choke on SETXFER.  Applies only if both the
4218d0cb43b3STejun Heo 	 * device and controller are SATA.
4219d0cb43b3STejun Heo 	 */
4220cd691876STejun Heo 	{ "PIONEER DVD-RW  DVRTD08",	NULL,	ATA_HORKAGE_NOSETXFER },
42213a25179eSVladimir LAVALLADE 	{ "PIONEER DVD-RW  DVRTD08A",	NULL,	ATA_HORKAGE_NOSETXFER },
42223a25179eSVladimir LAVALLADE 	{ "PIONEER DVD-RW  DVR-215",	NULL,	ATA_HORKAGE_NOSETXFER },
4223cd691876STejun Heo 	{ "PIONEER DVD-RW  DVR-212D",	NULL,	ATA_HORKAGE_NOSETXFER },
4224cd691876STejun Heo 	{ "PIONEER DVD-RW  DVR-216D",	NULL,	ATA_HORKAGE_NOSETXFER },
4225d0cb43b3STejun Heo 
4226f78dea06SMarc Carino 	/* devices that don't properly handle queued TRIM commands */
4227d121f7d0SMartin K. Petersen 	{ "Micron_M500*",		"MU0[1-4]*",	ATA_HORKAGE_NO_NCQ_TRIM, },
4228d121f7d0SMartin K. Petersen 	{ "Crucial_CT???M500SSD*",	"MU0[1-4]*",	ATA_HORKAGE_NO_NCQ_TRIM, },
4229d121f7d0SMartin K. Petersen 	{ "Micron_M550*",		NULL,		ATA_HORKAGE_NO_NCQ_TRIM, },
4230d121f7d0SMartin K. Petersen 	{ "Crucial_CT???M550SSD*",	NULL,		ATA_HORKAGE_NO_NCQ_TRIM, },
4231f78dea06SMarc Carino 
4232ecd75ad5STejun Heo 	/*
4233ecd75ad5STejun Heo 	 * Some WD SATA-I drives spin up and down erratically when the link
4234ecd75ad5STejun Heo 	 * is put into the slumber mode.  We don't have full list of the
4235ecd75ad5STejun Heo 	 * affected devices.  Disable LPM if the device matches one of the
4236ecd75ad5STejun Heo 	 * known prefixes and is SATA-1.  As a side effect LPM partial is
4237ecd75ad5STejun Heo 	 * lost too.
4238ecd75ad5STejun Heo 	 *
4239ecd75ad5STejun Heo 	 * https://bugzilla.kernel.org/show_bug.cgi?id=57211
4240ecd75ad5STejun Heo 	 */
4241ecd75ad5STejun Heo 	{ "WDC WD800JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4242ecd75ad5STejun Heo 	{ "WDC WD1200JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4243ecd75ad5STejun Heo 	{ "WDC WD1600JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4244ecd75ad5STejun Heo 	{ "WDC WD2000JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4245ecd75ad5STejun Heo 	{ "WDC WD2500JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4246ecd75ad5STejun Heo 	{ "WDC WD3000JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4247ecd75ad5STejun Heo 	{ "WDC WD3200JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4248ecd75ad5STejun Heo 
42496919a0a6SAlan Cox 	/* End Marker */
42506919a0a6SAlan Cox 	{ }
4251c6fd2807SJeff Garzik };
4252c6fd2807SJeff Garzik 
4253bce036ceSMark Lord /**
4254bce036ceSMark Lord  *	glob_match - match a text string against a glob-style pattern
4255bce036ceSMark Lord  *	@text: the string to be examined
4256bce036ceSMark Lord  *	@pattern: the glob-style pattern to be matched against
4257bce036ceSMark Lord  *
4258bce036ceSMark Lord  *	Either/both of text and pattern can be empty strings.
4259bce036ceSMark Lord  *
4260bce036ceSMark Lord  *	Match text against a glob-style pattern, with wildcards and simple sets:
4261bce036ceSMark Lord  *
4262bce036ceSMark Lord  *		?	matches any single character.
4263bce036ceSMark Lord  *		*	matches any run of characters.
4264bce036ceSMark Lord  *		[xyz]	matches a single character from the set: x, y, or z.
42652f9e4d16SMark Lord  *		[a-d]	matches a single character from the range: a, b, c, or d.
42662f9e4d16SMark Lord  *		[a-d0-9] matches a single character from either range.
4267bce036ceSMark Lord  *
42682f9e4d16SMark Lord  *	The special characters ?, [, -, or *, can be matched using a set, eg. [*]
42692f9e4d16SMark Lord  *	Behaviour with malformed patterns is undefined, though generally reasonable.
4270bce036ceSMark Lord  *
42713d2be54bSRandy Dunlap  *	Sample patterns:  "SD1?",  "SD1[0-5]",  "*R0",  "SD*1?[012]*xx"
4272bce036ceSMark Lord  *
4273bce036ceSMark Lord  *	This function uses one level of recursion per '*' in pattern.
4274bce036ceSMark Lord  *	Since it calls _nothing_ else, and has _no_ explicit local variables,
4275bce036ceSMark Lord  *	this will not cause stack problems for any reasonable use here.
4276bce036ceSMark Lord  *
4277bce036ceSMark Lord  *	RETURNS:
4278bce036ceSMark Lord  *	0 on match, 1 otherwise.
4279539cc7c7SJeff Garzik  */
4280bce036ceSMark Lord static int glob_match (const char *text, const char *pattern)
4281bce036ceSMark Lord {
4282bce036ceSMark Lord 	do {
4283bce036ceSMark Lord 		/* Match single character or a '?' wildcard */
4284bce036ceSMark Lord 		if (*text == *pattern || *pattern == '?') {
4285bce036ceSMark Lord 			if (!*pattern++)
4286bce036ceSMark Lord 				return 0;  /* End of both strings: match */
4287bce036ceSMark Lord 		} else {
4288bce036ceSMark Lord 			/* Match single char against a '[' bracketed ']' pattern set */
4289bce036ceSMark Lord 			if (!*text || *pattern != '[')
4290bce036ceSMark Lord 				break;  /* Not a pattern set */
42912f9e4d16SMark Lord 			while (*++pattern && *pattern != ']' && *text != *pattern) {
42922f9e4d16SMark Lord 				if (*pattern == '-' && *(pattern - 1) != '[')
42932f9e4d16SMark Lord 					if (*text > *(pattern - 1) && *text < *(pattern + 1)) {
42942f9e4d16SMark Lord 						++pattern;
42952f9e4d16SMark Lord 						break;
42962f9e4d16SMark Lord 					}
42972f9e4d16SMark Lord 			}
4298bce036ceSMark Lord 			if (!*pattern || *pattern == ']')
4299bce036ceSMark Lord 				return 1;  /* No match */
4300bce036ceSMark Lord 			while (*pattern && *pattern++ != ']');
4301317b50b8SAndrew Paprocki 		}
4302bce036ceSMark Lord 	} while (*++text && *pattern);
4303539cc7c7SJeff Garzik 
4304bce036ceSMark Lord 	/* Match any run of chars against a '*' wildcard */
4305bce036ceSMark Lord 	if (*pattern == '*') {
4306bce036ceSMark Lord 		if (!*++pattern)
4307bce036ceSMark Lord 			return 0;  /* Match: avoid recursion at end of pattern */
4308bce036ceSMark Lord 		/* Loop to handle additional pattern chars after the wildcard */
4309bce036ceSMark Lord 		while (*text) {
4310bce036ceSMark Lord 			if (glob_match(text, pattern) == 0)
4311bce036ceSMark Lord 				return 0;  /* Remainder matched */
4312bce036ceSMark Lord 			++text;  /* Absorb (match) this char and try again */
4313bce036ceSMark Lord 		}
4314bce036ceSMark Lord 	}
4315bce036ceSMark Lord 	if (!*text && !*pattern)
4316bce036ceSMark Lord 		return 0;  /* End of both strings: match */
4317bce036ceSMark Lord 	return 1;  /* No match */
4318539cc7c7SJeff Garzik }
4319539cc7c7SJeff Garzik 
432075683fe7STejun Heo static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
4321c6fd2807SJeff Garzik {
43228bfa79fcSTejun Heo 	unsigned char model_num[ATA_ID_PROD_LEN + 1];
43238bfa79fcSTejun Heo 	unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
43246919a0a6SAlan Cox 	const struct ata_blacklist_entry *ad = ata_device_blacklist;
4325c6fd2807SJeff Garzik 
43268bfa79fcSTejun Heo 	ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
43278bfa79fcSTejun Heo 	ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
4328c6fd2807SJeff Garzik 
43296919a0a6SAlan Cox 	while (ad->model_num) {
4330bce036ceSMark Lord 		if (!glob_match(model_num, ad->model_num)) {
43316919a0a6SAlan Cox 			if (ad->model_rev == NULL)
43326919a0a6SAlan Cox 				return ad->horkage;
4333bce036ceSMark Lord 			if (!glob_match(model_rev, ad->model_rev))
43346919a0a6SAlan Cox 				return ad->horkage;
4335c6fd2807SJeff Garzik 		}
43366919a0a6SAlan Cox 		ad++;
4337c6fd2807SJeff Garzik 	}
4338c6fd2807SJeff Garzik 	return 0;
4339c6fd2807SJeff Garzik }
4340c6fd2807SJeff Garzik 
43416919a0a6SAlan Cox static int ata_dma_blacklisted(const struct ata_device *dev)
43426919a0a6SAlan Cox {
43436919a0a6SAlan Cox 	/* We don't support polling DMA.
43446919a0a6SAlan Cox 	 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
43456919a0a6SAlan Cox 	 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
43466919a0a6SAlan Cox 	 */
43479af5c9c9STejun Heo 	if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
43486919a0a6SAlan Cox 	    (dev->flags & ATA_DFLAG_CDB_INTR))
43496919a0a6SAlan Cox 		return 1;
435075683fe7STejun Heo 	return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
43516919a0a6SAlan Cox }
43526919a0a6SAlan Cox 
4353c6fd2807SJeff Garzik /**
43546bbfd53dSAlan Cox  *	ata_is_40wire		-	check drive side detection
43556bbfd53dSAlan Cox  *	@dev: device
43566bbfd53dSAlan Cox  *
43576bbfd53dSAlan Cox  *	Perform drive side detection decoding, allowing for device vendors
43586bbfd53dSAlan Cox  *	who can't follow the documentation.
43596bbfd53dSAlan Cox  */
43606bbfd53dSAlan Cox 
43616bbfd53dSAlan Cox static int ata_is_40wire(struct ata_device *dev)
43626bbfd53dSAlan Cox {
43636bbfd53dSAlan Cox 	if (dev->horkage & ATA_HORKAGE_IVB)
43646bbfd53dSAlan Cox 		return ata_drive_40wire_relaxed(dev->id);
43656bbfd53dSAlan Cox 	return ata_drive_40wire(dev->id);
43666bbfd53dSAlan Cox }
43676bbfd53dSAlan Cox 
43686bbfd53dSAlan Cox /**
436915a5551cSAlan Cox  *	cable_is_40wire		-	40/80/SATA decider
437015a5551cSAlan Cox  *	@ap: port to consider
437115a5551cSAlan Cox  *
437215a5551cSAlan Cox  *	This function encapsulates the policy for speed management
437315a5551cSAlan Cox  *	in one place. At the moment we don't cache the result but
437415a5551cSAlan Cox  *	there is a good case for setting ap->cbl to the result when
437515a5551cSAlan Cox  *	we are called with unknown cables (and figuring out if it
437615a5551cSAlan Cox  *	impacts hotplug at all).
437715a5551cSAlan Cox  *
437815a5551cSAlan Cox  *	Return 1 if the cable appears to be 40 wire.
437915a5551cSAlan Cox  */
438015a5551cSAlan Cox 
438115a5551cSAlan Cox static int cable_is_40wire(struct ata_port *ap)
438215a5551cSAlan Cox {
438315a5551cSAlan Cox 	struct ata_link *link;
438415a5551cSAlan Cox 	struct ata_device *dev;
438515a5551cSAlan Cox 
43864a9c7b33STejun Heo 	/* If the controller thinks we are 40 wire, we are. */
438715a5551cSAlan Cox 	if (ap->cbl == ATA_CBL_PATA40)
438815a5551cSAlan Cox 		return 1;
43894a9c7b33STejun Heo 
43904a9c7b33STejun Heo 	/* If the controller thinks we are 80 wire, we are. */
439115a5551cSAlan Cox 	if (ap->cbl == ATA_CBL_PATA80 || ap->cbl == ATA_CBL_SATA)
439215a5551cSAlan Cox 		return 0;
43934a9c7b33STejun Heo 
43944a9c7b33STejun Heo 	/* If the system is known to be 40 wire short cable (eg
43954a9c7b33STejun Heo 	 * laptop), then we allow 80 wire modes even if the drive
43964a9c7b33STejun Heo 	 * isn't sure.
43974a9c7b33STejun Heo 	 */
4398f792068eSAlan Cox 	if (ap->cbl == ATA_CBL_PATA40_SHORT)
4399f792068eSAlan Cox 		return 0;
440015a5551cSAlan Cox 
44014a9c7b33STejun Heo 	/* If the controller doesn't know, we scan.
44024a9c7b33STejun Heo 	 *
44034a9c7b33STejun Heo 	 * Note: We look for all 40 wire detects at this point.  Any
44044a9c7b33STejun Heo 	 *       80 wire detect is taken to be 80 wire cable because
44054a9c7b33STejun Heo 	 * - in many setups only the one drive (slave if present) will
44064a9c7b33STejun Heo 	 *   give a valid detect
44074a9c7b33STejun Heo 	 * - if you have a non detect capable drive you don't want it
44084a9c7b33STejun Heo 	 *   to colour the choice
440915a5551cSAlan Cox 	 */
44101eca4365STejun Heo 	ata_for_each_link(link, ap, EDGE) {
44111eca4365STejun Heo 		ata_for_each_dev(dev, link, ENABLED) {
44121eca4365STejun Heo 			if (!ata_is_40wire(dev))
441315a5551cSAlan Cox 				return 0;
441415a5551cSAlan Cox 		}
441515a5551cSAlan Cox 	}
441615a5551cSAlan Cox 	return 1;
441715a5551cSAlan Cox }
441815a5551cSAlan Cox 
441915a5551cSAlan Cox /**
4420c6fd2807SJeff Garzik  *	ata_dev_xfermask - Compute supported xfermask of the given device
4421c6fd2807SJeff Garzik  *	@dev: Device to compute xfermask for
4422c6fd2807SJeff Garzik  *
4423c6fd2807SJeff Garzik  *	Compute supported xfermask of @dev and store it in
4424c6fd2807SJeff Garzik  *	dev->*_mask.  This function is responsible for applying all
4425c6fd2807SJeff Garzik  *	known limits including host controller limits, device
4426c6fd2807SJeff Garzik  *	blacklist, etc...
4427c6fd2807SJeff Garzik  *
4428c6fd2807SJeff Garzik  *	LOCKING:
4429c6fd2807SJeff Garzik  *	None.
4430c6fd2807SJeff Garzik  */
4431c6fd2807SJeff Garzik static void ata_dev_xfermask(struct ata_device *dev)
4432c6fd2807SJeff Garzik {
44339af5c9c9STejun Heo 	struct ata_link *link = dev->link;
44349af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
4435cca3974eSJeff Garzik 	struct ata_host *host = ap->host;
4436c6fd2807SJeff Garzik 	unsigned long xfer_mask;
4437c6fd2807SJeff Garzik 
4438c6fd2807SJeff Garzik 	/* controller modes available */
4439c6fd2807SJeff Garzik 	xfer_mask = ata_pack_xfermask(ap->pio_mask,
4440c6fd2807SJeff Garzik 				      ap->mwdma_mask, ap->udma_mask);
4441c6fd2807SJeff Garzik 
44428343f889SRobert Hancock 	/* drive modes available */
4443c6fd2807SJeff Garzik 	xfer_mask &= ata_pack_xfermask(dev->pio_mask,
4444c6fd2807SJeff Garzik 				       dev->mwdma_mask, dev->udma_mask);
4445c6fd2807SJeff Garzik 	xfer_mask &= ata_id_xfermask(dev->id);
4446c6fd2807SJeff Garzik 
4447b352e57dSAlan Cox 	/*
4448b352e57dSAlan Cox 	 *	CFA Advanced TrueIDE timings are not allowed on a shared
4449b352e57dSAlan Cox 	 *	cable
4450b352e57dSAlan Cox 	 */
4451b352e57dSAlan Cox 	if (ata_dev_pair(dev)) {
4452b352e57dSAlan Cox 		/* No PIO5 or PIO6 */
4453b352e57dSAlan Cox 		xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
4454b352e57dSAlan Cox 		/* No MWDMA3 or MWDMA 4 */
4455b352e57dSAlan Cox 		xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
4456b352e57dSAlan Cox 	}
4457b352e57dSAlan Cox 
4458c6fd2807SJeff Garzik 	if (ata_dma_blacklisted(dev)) {
4459c6fd2807SJeff Garzik 		xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4460a9a79dfeSJoe Perches 		ata_dev_warn(dev,
4461c6fd2807SJeff Garzik 			     "device is on DMA blacklist, disabling DMA\n");
4462c6fd2807SJeff Garzik 	}
4463c6fd2807SJeff Garzik 
446414d66ab7SPetr Vandrovec 	if ((host->flags & ATA_HOST_SIMPLEX) &&
446514d66ab7SPetr Vandrovec 	    host->simplex_claimed && host->simplex_claimed != ap) {
4466c6fd2807SJeff Garzik 		xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4467a9a79dfeSJoe Perches 		ata_dev_warn(dev,
4468a9a79dfeSJoe Perches 			     "simplex DMA is claimed by other device, disabling DMA\n");
4469c6fd2807SJeff Garzik 	}
4470c6fd2807SJeff Garzik 
4471e424675fSJeff Garzik 	if (ap->flags & ATA_FLAG_NO_IORDY)
4472e424675fSJeff Garzik 		xfer_mask &= ata_pio_mask_no_iordy(dev);
4473e424675fSJeff Garzik 
4474c6fd2807SJeff Garzik 	if (ap->ops->mode_filter)
4475a76b62caSAlan Cox 		xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
4476c6fd2807SJeff Garzik 
44778343f889SRobert Hancock 	/* Apply cable rule here.  Don't apply it early because when
44788343f889SRobert Hancock 	 * we handle hot plug the cable type can itself change.
44798343f889SRobert Hancock 	 * Check this last so that we know if the transfer rate was
44808343f889SRobert Hancock 	 * solely limited by the cable.
44818343f889SRobert Hancock 	 * Unknown or 80 wire cables reported host side are checked
44828343f889SRobert Hancock 	 * drive side as well. Cases where we know a 40wire cable
44838343f889SRobert Hancock 	 * is used safely for 80 are not checked here.
44848343f889SRobert Hancock 	 */
44858343f889SRobert Hancock 	if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
44868343f889SRobert Hancock 		/* UDMA/44 or higher would be available */
448715a5551cSAlan Cox 		if (cable_is_40wire(ap)) {
4488a9a79dfeSJoe Perches 			ata_dev_warn(dev,
44898343f889SRobert Hancock 				     "limited to UDMA/33 due to 40-wire cable\n");
44908343f889SRobert Hancock 			xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
44918343f889SRobert Hancock 		}
44928343f889SRobert Hancock 
4493c6fd2807SJeff Garzik 	ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
4494c6fd2807SJeff Garzik 			    &dev->mwdma_mask, &dev->udma_mask);
4495c6fd2807SJeff Garzik }
4496c6fd2807SJeff Garzik 
4497c6fd2807SJeff Garzik /**
4498c6fd2807SJeff Garzik  *	ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
4499c6fd2807SJeff Garzik  *	@dev: Device to which command will be sent
4500c6fd2807SJeff Garzik  *
4501c6fd2807SJeff Garzik  *	Issue SET FEATURES - XFER MODE command to device @dev
4502c6fd2807SJeff Garzik  *	on port @ap.
4503c6fd2807SJeff Garzik  *
4504c6fd2807SJeff Garzik  *	LOCKING:
4505c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
4506c6fd2807SJeff Garzik  *
4507c6fd2807SJeff Garzik  *	RETURNS:
4508c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
4509c6fd2807SJeff Garzik  */
4510c6fd2807SJeff Garzik 
4511c6fd2807SJeff Garzik static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
4512c6fd2807SJeff Garzik {
4513c6fd2807SJeff Garzik 	struct ata_taskfile tf;
4514c6fd2807SJeff Garzik 	unsigned int err_mask;
4515c6fd2807SJeff Garzik 
4516c6fd2807SJeff Garzik 	/* set up set-features taskfile */
4517c6fd2807SJeff Garzik 	DPRINTK("set features - xfer mode\n");
4518c6fd2807SJeff Garzik 
4519464cf177STejun Heo 	/* Some controllers and ATAPI devices show flaky interrupt
4520464cf177STejun Heo 	 * behavior after setting xfer mode.  Use polling instead.
4521464cf177STejun Heo 	 */
4522c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
4523c6fd2807SJeff Garzik 	tf.command = ATA_CMD_SET_FEATURES;
4524c6fd2807SJeff Garzik 	tf.feature = SETFEATURES_XFER;
4525464cf177STejun Heo 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
4526c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
4527b9f8ab2dSAlan Cox 	/* If we are using IORDY we must send the mode setting command */
4528b9f8ab2dSAlan Cox 	if (ata_pio_need_iordy(dev))
4529c6fd2807SJeff Garzik 		tf.nsect = dev->xfer_mode;
4530b9f8ab2dSAlan Cox 	/* If the device has IORDY and the controller does not - turn it off */
4531b9f8ab2dSAlan Cox  	else if (ata_id_has_iordy(dev->id))
4532b9f8ab2dSAlan Cox 		tf.nsect = 0x01;
4533b9f8ab2dSAlan Cox 	else /* In the ancient relic department - skip all of this */
4534b9f8ab2dSAlan Cox 		return 0;
4535c6fd2807SJeff Garzik 
45362b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
4537c6fd2807SJeff Garzik 
4538c6fd2807SJeff Garzik 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
4539c6fd2807SJeff Garzik 	return err_mask;
4540c6fd2807SJeff Garzik }
45411152b261STejun Heo 
4542c6fd2807SJeff Garzik /**
4543218f3d30SJeff Garzik  *	ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES
45449f45cbd3SKristen Carlson Accardi  *	@dev: Device to which command will be sent
45459f45cbd3SKristen Carlson Accardi  *	@enable: Whether to enable or disable the feature
4546218f3d30SJeff Garzik  *	@feature: The sector count represents the feature to set
45479f45cbd3SKristen Carlson Accardi  *
45489f45cbd3SKristen Carlson Accardi  *	Issue SET FEATURES - SATA FEATURES command to device @dev
4549218f3d30SJeff Garzik  *	on port @ap with sector count
45509f45cbd3SKristen Carlson Accardi  *
45519f45cbd3SKristen Carlson Accardi  *	LOCKING:
45529f45cbd3SKristen Carlson Accardi  *	PCI/etc. bus probe sem.
45539f45cbd3SKristen Carlson Accardi  *
45549f45cbd3SKristen Carlson Accardi  *	RETURNS:
45559f45cbd3SKristen Carlson Accardi  *	0 on success, AC_ERR_* mask otherwise.
45569f45cbd3SKristen Carlson Accardi  */
45571152b261STejun Heo unsigned int ata_dev_set_feature(struct ata_device *dev, u8 enable, u8 feature)
45589f45cbd3SKristen Carlson Accardi {
45599f45cbd3SKristen Carlson Accardi 	struct ata_taskfile tf;
45609f45cbd3SKristen Carlson Accardi 	unsigned int err_mask;
45619f45cbd3SKristen Carlson Accardi 
45629f45cbd3SKristen Carlson Accardi 	/* set up set-features taskfile */
45639f45cbd3SKristen Carlson Accardi 	DPRINTK("set features - SATA features\n");
45649f45cbd3SKristen Carlson Accardi 
45659f45cbd3SKristen Carlson Accardi 	ata_tf_init(dev, &tf);
45669f45cbd3SKristen Carlson Accardi 	tf.command = ATA_CMD_SET_FEATURES;
45679f45cbd3SKristen Carlson Accardi 	tf.feature = enable;
45689f45cbd3SKristen Carlson Accardi 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
45699f45cbd3SKristen Carlson Accardi 	tf.protocol = ATA_PROT_NODATA;
4570218f3d30SJeff Garzik 	tf.nsect = feature;
45719f45cbd3SKristen Carlson Accardi 
45722b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
45739f45cbd3SKristen Carlson Accardi 
45749f45cbd3SKristen Carlson Accardi 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
45759f45cbd3SKristen Carlson Accardi 	return err_mask;
45769f45cbd3SKristen Carlson Accardi }
4577633de4ccSJeff Garzik EXPORT_SYMBOL_GPL(ata_dev_set_feature);
45789f45cbd3SKristen Carlson Accardi 
45799f45cbd3SKristen Carlson Accardi /**
4580c6fd2807SJeff Garzik  *	ata_dev_init_params - Issue INIT DEV PARAMS command
4581c6fd2807SJeff Garzik  *	@dev: Device to which command will be sent
4582c6fd2807SJeff Garzik  *	@heads: Number of heads (taskfile parameter)
4583c6fd2807SJeff Garzik  *	@sectors: Number of sectors (taskfile parameter)
4584c6fd2807SJeff Garzik  *
4585c6fd2807SJeff Garzik  *	LOCKING:
4586c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
4587c6fd2807SJeff Garzik  *
4588c6fd2807SJeff Garzik  *	RETURNS:
4589c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
4590c6fd2807SJeff Garzik  */
4591c6fd2807SJeff Garzik static unsigned int ata_dev_init_params(struct ata_device *dev,
4592c6fd2807SJeff Garzik 					u16 heads, u16 sectors)
4593c6fd2807SJeff Garzik {
4594c6fd2807SJeff Garzik 	struct ata_taskfile tf;
4595c6fd2807SJeff Garzik 	unsigned int err_mask;
4596c6fd2807SJeff Garzik 
4597c6fd2807SJeff Garzik 	/* Number of sectors per track 1-255. Number of heads 1-16 */
4598c6fd2807SJeff Garzik 	if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
4599c6fd2807SJeff Garzik 		return AC_ERR_INVALID;
4600c6fd2807SJeff Garzik 
4601c6fd2807SJeff Garzik 	/* set up init dev params taskfile */
4602c6fd2807SJeff Garzik 	DPRINTK("init dev params \n");
4603c6fd2807SJeff Garzik 
4604c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
4605c6fd2807SJeff Garzik 	tf.command = ATA_CMD_INIT_DEV_PARAMS;
4606c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4607c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
4608c6fd2807SJeff Garzik 	tf.nsect = sectors;
4609c6fd2807SJeff Garzik 	tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
4610c6fd2807SJeff Garzik 
46112b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
461218b2466cSAlan Cox 	/* A clean abort indicates an original or just out of spec drive
461318b2466cSAlan Cox 	   and we should continue as we issue the setup based on the
461418b2466cSAlan Cox 	   drive reported working geometry */
461518b2466cSAlan Cox 	if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
461618b2466cSAlan Cox 		err_mask = 0;
4617c6fd2807SJeff Garzik 
4618c6fd2807SJeff Garzik 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
4619c6fd2807SJeff Garzik 	return err_mask;
4620c6fd2807SJeff Garzik }
4621c6fd2807SJeff Garzik 
4622c6fd2807SJeff Garzik /**
4623c6fd2807SJeff Garzik  *	ata_sg_clean - Unmap DMA memory associated with command
4624c6fd2807SJeff Garzik  *	@qc: Command containing DMA memory to be released
4625c6fd2807SJeff Garzik  *
4626c6fd2807SJeff Garzik  *	Unmap all mapped DMA memory associated with this command.
4627c6fd2807SJeff Garzik  *
4628c6fd2807SJeff Garzik  *	LOCKING:
4629cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4630c6fd2807SJeff Garzik  */
463170e6ad0cSTejun Heo void ata_sg_clean(struct ata_queued_cmd *qc)
4632c6fd2807SJeff Garzik {
4633c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4634ff2aeb1eSTejun Heo 	struct scatterlist *sg = qc->sg;
4635c6fd2807SJeff Garzik 	int dir = qc->dma_dir;
4636c6fd2807SJeff Garzik 
4637efcb3cf7STejun Heo 	WARN_ON_ONCE(sg == NULL);
4638c6fd2807SJeff Garzik 
4639dde20207SJames Bottomley 	VPRINTK("unmapping %u sg elements\n", qc->n_elem);
4640c6fd2807SJeff Garzik 
4641dde20207SJames Bottomley 	if (qc->n_elem)
46425825627cSFUJITA Tomonori 		dma_unmap_sg(ap->dev, sg, qc->orig_n_elem, dir);
4643c6fd2807SJeff Garzik 
4644c6fd2807SJeff Garzik 	qc->flags &= ~ATA_QCFLAG_DMAMAP;
4645ff2aeb1eSTejun Heo 	qc->sg = NULL;
4646c6fd2807SJeff Garzik }
4647c6fd2807SJeff Garzik 
4648c6fd2807SJeff Garzik /**
46495895ef9aSTejun Heo  *	atapi_check_dma - Check whether ATAPI DMA can be supported
4650c6fd2807SJeff Garzik  *	@qc: Metadata associated with taskfile to check
4651c6fd2807SJeff Garzik  *
4652c6fd2807SJeff Garzik  *	Allow low-level driver to filter ATA PACKET commands, returning
4653c6fd2807SJeff Garzik  *	a status indicating whether or not it is OK to use DMA for the
4654c6fd2807SJeff Garzik  *	supplied PACKET command.
4655c6fd2807SJeff Garzik  *
4656c6fd2807SJeff Garzik  *	LOCKING:
4657cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4658c6fd2807SJeff Garzik  *
4659c6fd2807SJeff Garzik  *	RETURNS: 0 when ATAPI DMA can be used
4660c6fd2807SJeff Garzik  *               nonzero otherwise
4661c6fd2807SJeff Garzik  */
46625895ef9aSTejun Heo int atapi_check_dma(struct ata_queued_cmd *qc)
4663c6fd2807SJeff Garzik {
4664c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4665c6fd2807SJeff Garzik 
4666b9a4197eSTejun Heo 	/* Don't allow DMA if it isn't multiple of 16 bytes.  Quite a
4667b9a4197eSTejun Heo 	 * few ATAPI devices choke on such DMA requests.
4668b9a4197eSTejun Heo 	 */
46696a87e42eSTejun Heo 	if (!(qc->dev->horkage & ATA_HORKAGE_ATAPI_MOD16_DMA) &&
46706a87e42eSTejun Heo 	    unlikely(qc->nbytes & 15))
46716f23a31dSAlbert Lee 		return 1;
46726f23a31dSAlbert Lee 
4673c6fd2807SJeff Garzik 	if (ap->ops->check_atapi_dma)
4674b9a4197eSTejun Heo 		return ap->ops->check_atapi_dma(qc);
4675c6fd2807SJeff Garzik 
4676b9a4197eSTejun Heo 	return 0;
4677c6fd2807SJeff Garzik }
4678b9a4197eSTejun Heo 
4679c6fd2807SJeff Garzik /**
468031cc23b3STejun Heo  *	ata_std_qc_defer - Check whether a qc needs to be deferred
468131cc23b3STejun Heo  *	@qc: ATA command in question
468231cc23b3STejun Heo  *
468331cc23b3STejun Heo  *	Non-NCQ commands cannot run with any other command, NCQ or
468431cc23b3STejun Heo  *	not.  As upper layer only knows the queue depth, we are
468531cc23b3STejun Heo  *	responsible for maintaining exclusion.  This function checks
468631cc23b3STejun Heo  *	whether a new command @qc can be issued.
468731cc23b3STejun Heo  *
468831cc23b3STejun Heo  *	LOCKING:
468931cc23b3STejun Heo  *	spin_lock_irqsave(host lock)
469031cc23b3STejun Heo  *
469131cc23b3STejun Heo  *	RETURNS:
469231cc23b3STejun Heo  *	ATA_DEFER_* if deferring is needed, 0 otherwise.
469331cc23b3STejun Heo  */
469431cc23b3STejun Heo int ata_std_qc_defer(struct ata_queued_cmd *qc)
469531cc23b3STejun Heo {
469631cc23b3STejun Heo 	struct ata_link *link = qc->dev->link;
469731cc23b3STejun Heo 
469831cc23b3STejun Heo 	if (qc->tf.protocol == ATA_PROT_NCQ) {
469931cc23b3STejun Heo 		if (!ata_tag_valid(link->active_tag))
470031cc23b3STejun Heo 			return 0;
470131cc23b3STejun Heo 	} else {
470231cc23b3STejun Heo 		if (!ata_tag_valid(link->active_tag) && !link->sactive)
470331cc23b3STejun Heo 			return 0;
470431cc23b3STejun Heo 	}
470531cc23b3STejun Heo 
470631cc23b3STejun Heo 	return ATA_DEFER_LINK;
470731cc23b3STejun Heo }
470831cc23b3STejun Heo 
4709c6fd2807SJeff Garzik void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
4710c6fd2807SJeff Garzik 
4711c6fd2807SJeff Garzik /**
4712c6fd2807SJeff Garzik  *	ata_sg_init - Associate command with scatter-gather table.
4713c6fd2807SJeff Garzik  *	@qc: Command to be associated
4714c6fd2807SJeff Garzik  *	@sg: Scatter-gather table.
4715c6fd2807SJeff Garzik  *	@n_elem: Number of elements in s/g table.
4716c6fd2807SJeff Garzik  *
4717c6fd2807SJeff Garzik  *	Initialize the data-related elements of queued_cmd @qc
4718c6fd2807SJeff Garzik  *	to point to a scatter-gather table @sg, containing @n_elem
4719c6fd2807SJeff Garzik  *	elements.
4720c6fd2807SJeff Garzik  *
4721c6fd2807SJeff Garzik  *	LOCKING:
4722cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4723c6fd2807SJeff Garzik  */
4724c6fd2807SJeff Garzik void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
4725c6fd2807SJeff Garzik 		 unsigned int n_elem)
4726c6fd2807SJeff Garzik {
4727ff2aeb1eSTejun Heo 	qc->sg = sg;
4728c6fd2807SJeff Garzik 	qc->n_elem = n_elem;
4729ff2aeb1eSTejun Heo 	qc->cursg = qc->sg;
4730ff2aeb1eSTejun Heo }
4731ff2aeb1eSTejun Heo 
4732c6fd2807SJeff Garzik /**
4733c6fd2807SJeff Garzik  *	ata_sg_setup - DMA-map the scatter-gather table associated with a command.
4734c6fd2807SJeff Garzik  *	@qc: Command with scatter-gather table to be mapped.
4735c6fd2807SJeff Garzik  *
4736c6fd2807SJeff Garzik  *	DMA-map the scatter-gather table associated with queued_cmd @qc.
4737c6fd2807SJeff Garzik  *
4738c6fd2807SJeff Garzik  *	LOCKING:
4739cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4740c6fd2807SJeff Garzik  *
4741c6fd2807SJeff Garzik  *	RETURNS:
4742c6fd2807SJeff Garzik  *	Zero on success, negative on error.
4743c6fd2807SJeff Garzik  *
4744c6fd2807SJeff Garzik  */
4745c6fd2807SJeff Garzik static int ata_sg_setup(struct ata_queued_cmd *qc)
4746c6fd2807SJeff Garzik {
4747c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4748dde20207SJames Bottomley 	unsigned int n_elem;
4749c6fd2807SJeff Garzik 
475044877b4eSTejun Heo 	VPRINTK("ENTER, ata%u\n", ap->print_id);
4751c6fd2807SJeff Garzik 
4752dde20207SJames Bottomley 	n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir);
4753dde20207SJames Bottomley 	if (n_elem < 1)
4754c6fd2807SJeff Garzik 		return -1;
4755c6fd2807SJeff Garzik 
4756dde20207SJames Bottomley 	DPRINTK("%d sg elements mapped\n", n_elem);
47575825627cSFUJITA Tomonori 	qc->orig_n_elem = qc->n_elem;
4758dde20207SJames Bottomley 	qc->n_elem = n_elem;
4759f92a2636STejun Heo 	qc->flags |= ATA_QCFLAG_DMAMAP;
4760c6fd2807SJeff Garzik 
4761c6fd2807SJeff Garzik 	return 0;
4762c6fd2807SJeff Garzik }
4763c6fd2807SJeff Garzik 
4764c6fd2807SJeff Garzik /**
4765c6fd2807SJeff Garzik  *	swap_buf_le16 - swap halves of 16-bit words in place
4766c6fd2807SJeff Garzik  *	@buf:  Buffer to swap
4767c6fd2807SJeff Garzik  *	@buf_words:  Number of 16-bit words in buffer.
4768c6fd2807SJeff Garzik  *
4769c6fd2807SJeff Garzik  *	Swap halves of 16-bit words if needed to convert from
4770c6fd2807SJeff Garzik  *	little-endian byte order to native cpu byte order, or
4771c6fd2807SJeff Garzik  *	vice-versa.
4772c6fd2807SJeff Garzik  *
4773c6fd2807SJeff Garzik  *	LOCKING:
4774c6fd2807SJeff Garzik  *	Inherited from caller.
4775c6fd2807SJeff Garzik  */
4776c6fd2807SJeff Garzik void swap_buf_le16(u16 *buf, unsigned int buf_words)
4777c6fd2807SJeff Garzik {
4778c6fd2807SJeff Garzik #ifdef __BIG_ENDIAN
4779c6fd2807SJeff Garzik 	unsigned int i;
4780c6fd2807SJeff Garzik 
4781c6fd2807SJeff Garzik 	for (i = 0; i < buf_words; i++)
4782c6fd2807SJeff Garzik 		buf[i] = le16_to_cpu(buf[i]);
4783c6fd2807SJeff Garzik #endif /* __BIG_ENDIAN */
4784c6fd2807SJeff Garzik }
4785c6fd2807SJeff Garzik 
4786c6fd2807SJeff Garzik /**
47878a8bc223STejun Heo  *	ata_qc_new - Request an available ATA command, for queueing
47885eb66fe0SRandy Dunlap  *	@ap: target port
47898a8bc223STejun Heo  *
47908a8bc223STejun Heo  *	LOCKING:
47918a8bc223STejun Heo  *	None.
47928a8bc223STejun Heo  */
47938a8bc223STejun Heo 
47948a8bc223STejun Heo static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
47958a8bc223STejun Heo {
47968a8bc223STejun Heo 	struct ata_queued_cmd *qc = NULL;
47978a4aeec8SDan Williams 	unsigned int i, tag;
47988a8bc223STejun Heo 
47998a8bc223STejun Heo 	/* no command while frozen */
48008a8bc223STejun Heo 	if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
48018a8bc223STejun Heo 		return NULL;
48028a8bc223STejun Heo 
48038a4aeec8SDan Williams 	for (i = 0; i < ATA_MAX_QUEUE; i++) {
48048a4aeec8SDan Williams 		tag = (i + ap->last_tag + 1) % ATA_MAX_QUEUE;
48058a4aeec8SDan Williams 
48068a8bc223STejun Heo 		/* the last tag is reserved for internal command. */
48078a4aeec8SDan Williams 		if (tag == ATA_TAG_INTERNAL)
48088a4aeec8SDan Williams 			continue;
48098a4aeec8SDan Williams 
48108a4aeec8SDan Williams 		if (!test_and_set_bit(tag, &ap->qc_allocated)) {
48118a4aeec8SDan Williams 			qc = __ata_qc_from_tag(ap, tag);
48128a4aeec8SDan Williams 			qc->tag = tag;
48138a4aeec8SDan Williams 			ap->last_tag = tag;
48148a8bc223STejun Heo 			break;
48158a8bc223STejun Heo 		}
48168a4aeec8SDan Williams 	}
48178a8bc223STejun Heo 
48188a8bc223STejun Heo 	return qc;
48198a8bc223STejun Heo }
48208a8bc223STejun Heo 
48218a8bc223STejun Heo /**
4822c6fd2807SJeff Garzik  *	ata_qc_new_init - Request an available ATA command, and initialize it
4823c6fd2807SJeff Garzik  *	@dev: Device from whom we request an available command structure
4824c6fd2807SJeff Garzik  *
4825c6fd2807SJeff Garzik  *	LOCKING:
4826c6fd2807SJeff Garzik  *	None.
4827c6fd2807SJeff Garzik  */
4828c6fd2807SJeff Garzik 
48298a8bc223STejun Heo struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev)
4830c6fd2807SJeff Garzik {
48319af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
4832c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
4833c6fd2807SJeff Garzik 
48348a8bc223STejun Heo 	qc = ata_qc_new(ap);
4835c6fd2807SJeff Garzik 	if (qc) {
4836c6fd2807SJeff Garzik 		qc->scsicmd = NULL;
4837c6fd2807SJeff Garzik 		qc->ap = ap;
4838c6fd2807SJeff Garzik 		qc->dev = dev;
4839c6fd2807SJeff Garzik 
4840c6fd2807SJeff Garzik 		ata_qc_reinit(qc);
4841c6fd2807SJeff Garzik 	}
4842c6fd2807SJeff Garzik 
4843c6fd2807SJeff Garzik 	return qc;
4844c6fd2807SJeff Garzik }
4845c6fd2807SJeff Garzik 
48468a8bc223STejun Heo /**
48478a8bc223STejun Heo  *	ata_qc_free - free unused ata_queued_cmd
48488a8bc223STejun Heo  *	@qc: Command to complete
48498a8bc223STejun Heo  *
48508a8bc223STejun Heo  *	Designed to free unused ata_queued_cmd object
48518a8bc223STejun Heo  *	in case something prevents using it.
48528a8bc223STejun Heo  *
48538a8bc223STejun Heo  *	LOCKING:
48548a8bc223STejun Heo  *	spin_lock_irqsave(host lock)
48558a8bc223STejun Heo  */
48568a8bc223STejun Heo void ata_qc_free(struct ata_queued_cmd *qc)
48578a8bc223STejun Heo {
4858a1104016SJulia Lawall 	struct ata_port *ap;
48598a8bc223STejun Heo 	unsigned int tag;
48608a8bc223STejun Heo 
4861efcb3cf7STejun Heo 	WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4862a1104016SJulia Lawall 	ap = qc->ap;
48638a8bc223STejun Heo 
48648a8bc223STejun Heo 	qc->flags = 0;
48658a8bc223STejun Heo 	tag = qc->tag;
48668a8bc223STejun Heo 	if (likely(ata_tag_valid(tag))) {
48678a8bc223STejun Heo 		qc->tag = ATA_TAG_POISON;
48688a8bc223STejun Heo 		clear_bit(tag, &ap->qc_allocated);
48698a8bc223STejun Heo 	}
48708a8bc223STejun Heo }
48718a8bc223STejun Heo 
4872c6fd2807SJeff Garzik void __ata_qc_complete(struct ata_queued_cmd *qc)
4873c6fd2807SJeff Garzik {
4874a1104016SJulia Lawall 	struct ata_port *ap;
4875a1104016SJulia Lawall 	struct ata_link *link;
4876c6fd2807SJeff Garzik 
4877efcb3cf7STejun Heo 	WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4878efcb3cf7STejun Heo 	WARN_ON_ONCE(!(qc->flags & ATA_QCFLAG_ACTIVE));
4879a1104016SJulia Lawall 	ap = qc->ap;
4880a1104016SJulia Lawall 	link = qc->dev->link;
4881c6fd2807SJeff Garzik 
4882c6fd2807SJeff Garzik 	if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
4883c6fd2807SJeff Garzik 		ata_sg_clean(qc);
4884c6fd2807SJeff Garzik 
4885c6fd2807SJeff Garzik 	/* command should be marked inactive atomically with qc completion */
4886da917d69STejun Heo 	if (qc->tf.protocol == ATA_PROT_NCQ) {
48879af5c9c9STejun Heo 		link->sactive &= ~(1 << qc->tag);
4888da917d69STejun Heo 		if (!link->sactive)
4889da917d69STejun Heo 			ap->nr_active_links--;
4890da917d69STejun Heo 	} else {
48919af5c9c9STejun Heo 		link->active_tag = ATA_TAG_POISON;
4892da917d69STejun Heo 		ap->nr_active_links--;
4893da917d69STejun Heo 	}
4894da917d69STejun Heo 
4895da917d69STejun Heo 	/* clear exclusive status */
4896da917d69STejun Heo 	if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
4897da917d69STejun Heo 		     ap->excl_link == link))
4898da917d69STejun Heo 		ap->excl_link = NULL;
4899c6fd2807SJeff Garzik 
4900c6fd2807SJeff Garzik 	/* atapi: mark qc as inactive to prevent the interrupt handler
4901c6fd2807SJeff Garzik 	 * from completing the command twice later, before the error handler
4902c6fd2807SJeff Garzik 	 * is called. (when rc != 0 and atapi request sense is needed)
4903c6fd2807SJeff Garzik 	 */
4904c6fd2807SJeff Garzik 	qc->flags &= ~ATA_QCFLAG_ACTIVE;
4905c6fd2807SJeff Garzik 	ap->qc_active &= ~(1 << qc->tag);
4906c6fd2807SJeff Garzik 
4907c6fd2807SJeff Garzik 	/* call completion callback */
4908c6fd2807SJeff Garzik 	qc->complete_fn(qc);
4909c6fd2807SJeff Garzik }
4910c6fd2807SJeff Garzik 
491139599a53STejun Heo static void fill_result_tf(struct ata_queued_cmd *qc)
491239599a53STejun Heo {
491339599a53STejun Heo 	struct ata_port *ap = qc->ap;
491439599a53STejun Heo 
491539599a53STejun Heo 	qc->result_tf.flags = qc->tf.flags;
491622183bf5STejun Heo 	ap->ops->qc_fill_rtf(qc);
491739599a53STejun Heo }
491839599a53STejun Heo 
491900115e0fSTejun Heo static void ata_verify_xfer(struct ata_queued_cmd *qc)
492000115e0fSTejun Heo {
492100115e0fSTejun Heo 	struct ata_device *dev = qc->dev;
492200115e0fSTejun Heo 
492300115e0fSTejun Heo 	if (ata_is_nodata(qc->tf.protocol))
492400115e0fSTejun Heo 		return;
492500115e0fSTejun Heo 
492600115e0fSTejun Heo 	if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
492700115e0fSTejun Heo 		return;
492800115e0fSTejun Heo 
492900115e0fSTejun Heo 	dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
493000115e0fSTejun Heo }
493100115e0fSTejun Heo 
4932c6fd2807SJeff Garzik /**
4933c6fd2807SJeff Garzik  *	ata_qc_complete - Complete an active ATA command
4934c6fd2807SJeff Garzik  *	@qc: Command to complete
4935c6fd2807SJeff Garzik  *
49361aadf5c3STejun Heo  *	Indicate to the mid and upper layers that an ATA command has
49371aadf5c3STejun Heo  *	completed, with either an ok or not-ok status.
49381aadf5c3STejun Heo  *
49391aadf5c3STejun Heo  *	Refrain from calling this function multiple times when
49401aadf5c3STejun Heo  *	successfully completing multiple NCQ commands.
49411aadf5c3STejun Heo  *	ata_qc_complete_multiple() should be used instead, which will
49421aadf5c3STejun Heo  *	properly update IRQ expect state.
4943c6fd2807SJeff Garzik  *
4944c6fd2807SJeff Garzik  *	LOCKING:
4945cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4946c6fd2807SJeff Garzik  */
4947c6fd2807SJeff Garzik void ata_qc_complete(struct ata_queued_cmd *qc)
4948c6fd2807SJeff Garzik {
4949c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4950c6fd2807SJeff Garzik 
4951c6fd2807SJeff Garzik 	/* XXX: New EH and old EH use different mechanisms to
4952c6fd2807SJeff Garzik 	 * synchronize EH with regular execution path.
4953c6fd2807SJeff Garzik 	 *
4954c6fd2807SJeff Garzik 	 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
4955c6fd2807SJeff Garzik 	 * Normal execution path is responsible for not accessing a
4956c6fd2807SJeff Garzik 	 * failed qc.  libata core enforces the rule by returning NULL
4957c6fd2807SJeff Garzik 	 * from ata_qc_from_tag() for failed qcs.
4958c6fd2807SJeff Garzik 	 *
4959c6fd2807SJeff Garzik 	 * Old EH depends on ata_qc_complete() nullifying completion
4960c6fd2807SJeff Garzik 	 * requests if ATA_QCFLAG_EH_SCHEDULED is set.  Old EH does
4961c6fd2807SJeff Garzik 	 * not synchronize with interrupt handler.  Only PIO task is
4962c6fd2807SJeff Garzik 	 * taken care of.
4963c6fd2807SJeff Garzik 	 */
4964c6fd2807SJeff Garzik 	if (ap->ops->error_handler) {
49654dbfa39bSTejun Heo 		struct ata_device *dev = qc->dev;
49664dbfa39bSTejun Heo 		struct ata_eh_info *ehi = &dev->link->eh_info;
49674dbfa39bSTejun Heo 
4968c6fd2807SJeff Garzik 		if (unlikely(qc->err_mask))
4969c6fd2807SJeff Garzik 			qc->flags |= ATA_QCFLAG_FAILED;
4970c6fd2807SJeff Garzik 
4971f08dc1acSTejun Heo 		/*
4972f08dc1acSTejun Heo 		 * Finish internal commands without any further processing
4973f08dc1acSTejun Heo 		 * and always with the result TF filled.
4974f08dc1acSTejun Heo 		 */
4975f08dc1acSTejun Heo 		if (unlikely(ata_tag_internal(qc->tag))) {
497639599a53STejun Heo 			fill_result_tf(qc);
4977f4b31db9STejun Heo 			__ata_qc_complete(qc);
4978c6fd2807SJeff Garzik 			return;
4979c6fd2807SJeff Garzik 		}
4980c6fd2807SJeff Garzik 
4981f08dc1acSTejun Heo 		/*
4982f08dc1acSTejun Heo 		 * Non-internal qc has failed.  Fill the result TF and
4983f08dc1acSTejun Heo 		 * summon EH.
4984f08dc1acSTejun Heo 		 */
4985f08dc1acSTejun Heo 		if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
4986f08dc1acSTejun Heo 			fill_result_tf(qc);
4987f08dc1acSTejun Heo 			ata_qc_schedule_eh(qc);
4988f08dc1acSTejun Heo 			return;
4989f08dc1acSTejun Heo 		}
4990f08dc1acSTejun Heo 
49914dc738edSTejun Heo 		WARN_ON_ONCE(ap->pflags & ATA_PFLAG_FROZEN);
49924dc738edSTejun Heo 
4993c6fd2807SJeff Garzik 		/* read result TF if requested */
4994c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_RESULT_TF)
499539599a53STejun Heo 			fill_result_tf(qc);
4996c6fd2807SJeff Garzik 
49974dbfa39bSTejun Heo 		/* Some commands need post-processing after successful
49984dbfa39bSTejun Heo 		 * completion.
49994dbfa39bSTejun Heo 		 */
50004dbfa39bSTejun Heo 		switch (qc->tf.command) {
50014dbfa39bSTejun Heo 		case ATA_CMD_SET_FEATURES:
50024dbfa39bSTejun Heo 			if (qc->tf.feature != SETFEATURES_WC_ON &&
50034dbfa39bSTejun Heo 			    qc->tf.feature != SETFEATURES_WC_OFF)
50044dbfa39bSTejun Heo 				break;
50054dbfa39bSTejun Heo 			/* fall through */
50064dbfa39bSTejun Heo 		case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
50074dbfa39bSTejun Heo 		case ATA_CMD_SET_MULTI: /* multi_count changed */
50084dbfa39bSTejun Heo 			/* revalidate device */
50094dbfa39bSTejun Heo 			ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE;
50104dbfa39bSTejun Heo 			ata_port_schedule_eh(ap);
50114dbfa39bSTejun Heo 			break;
5012054a5fbaSTejun Heo 
5013054a5fbaSTejun Heo 		case ATA_CMD_SLEEP:
5014054a5fbaSTejun Heo 			dev->flags |= ATA_DFLAG_SLEEPING;
5015054a5fbaSTejun Heo 			break;
50164dbfa39bSTejun Heo 		}
50174dbfa39bSTejun Heo 
501800115e0fSTejun Heo 		if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
501900115e0fSTejun Heo 			ata_verify_xfer(qc);
502000115e0fSTejun Heo 
5021c6fd2807SJeff Garzik 		__ata_qc_complete(qc);
5022c6fd2807SJeff Garzik 	} else {
5023c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
5024c6fd2807SJeff Garzik 			return;
5025c6fd2807SJeff Garzik 
5026c6fd2807SJeff Garzik 		/* read result TF if failed or requested */
5027c6fd2807SJeff Garzik 		if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
502839599a53STejun Heo 			fill_result_tf(qc);
5029c6fd2807SJeff Garzik 
5030c6fd2807SJeff Garzik 		__ata_qc_complete(qc);
5031c6fd2807SJeff Garzik 	}
5032c6fd2807SJeff Garzik }
5033c6fd2807SJeff Garzik 
5034c6fd2807SJeff Garzik /**
5035c6fd2807SJeff Garzik  *	ata_qc_complete_multiple - Complete multiple qcs successfully
5036c6fd2807SJeff Garzik  *	@ap: port in question
5037c6fd2807SJeff Garzik  *	@qc_active: new qc_active mask
5038c6fd2807SJeff Garzik  *
5039c6fd2807SJeff Garzik  *	Complete in-flight commands.  This functions is meant to be
5040c6fd2807SJeff Garzik  *	called from low-level driver's interrupt routine to complete
5041c6fd2807SJeff Garzik  *	requests normally.  ap->qc_active and @qc_active is compared
5042c6fd2807SJeff Garzik  *	and commands are completed accordingly.
5043c6fd2807SJeff Garzik  *
50441aadf5c3STejun Heo  *	Always use this function when completing multiple NCQ commands
50451aadf5c3STejun Heo  *	from IRQ handlers instead of calling ata_qc_complete()
50461aadf5c3STejun Heo  *	multiple times to keep IRQ expect status properly in sync.
50471aadf5c3STejun Heo  *
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)) {
5062a9a79dfeSJoe Perches 		ata_port_err(ap, "illegal qc_active transition (%08x->%08x)\n",
5063a9a79dfeSJoe Perches 			     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 
512260f5d6efSTejun Heo 	/*
512360f5d6efSTejun Heo 	 * We guarantee to LLDs that they will have at least one
5124f92a2636STejun Heo 	 * non-zero sg if the command is a data command.
5125f92a2636STejun Heo 	 */
512660f5d6efSTejun Heo 	if (WARN_ON_ONCE(ata_is_data(prot) &&
512760f5d6efSTejun Heo 			 (!qc->sg || !qc->n_elem || !qc->nbytes)))
512860f5d6efSTejun Heo 		goto sys_err;
5129f92a2636STejun Heo 
5130405e66b3STejun Heo 	if (ata_is_dma(prot) || (ata_is_pio(prot) &&
5131f92a2636STejun Heo 				 (ap->flags & ATA_FLAG_PIO_DMA)))
5132c6fd2807SJeff Garzik 		if (ata_sg_setup(qc))
513360f5d6efSTejun Heo 			goto sys_err;
5134c6fd2807SJeff Garzik 
5135cf480626STejun Heo 	/* if device is sleeping, schedule reset and abort the link */
5136054a5fbaSTejun Heo 	if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
5137cf480626STejun Heo 		link->eh_info.action |= ATA_EH_RESET;
5138054a5fbaSTejun Heo 		ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
5139054a5fbaSTejun Heo 		ata_link_abort(link);
5140054a5fbaSTejun Heo 		return;
5141054a5fbaSTejun Heo 	}
5142054a5fbaSTejun Heo 
5143c6fd2807SJeff Garzik 	ap->ops->qc_prep(qc);
5144c6fd2807SJeff Garzik 
5145c6fd2807SJeff Garzik 	qc->err_mask |= ap->ops->qc_issue(qc);
5146c6fd2807SJeff Garzik 	if (unlikely(qc->err_mask))
5147c6fd2807SJeff Garzik 		goto err;
5148c6fd2807SJeff Garzik 	return;
5149c6fd2807SJeff Garzik 
515060f5d6efSTejun Heo sys_err:
5151c6fd2807SJeff Garzik 	qc->err_mask |= AC_ERR_SYSTEM;
5152c6fd2807SJeff Garzik err:
5153c6fd2807SJeff Garzik 	ata_qc_complete(qc);
5154c6fd2807SJeff Garzik }
5155c6fd2807SJeff Garzik 
5156c6fd2807SJeff Garzik /**
5157c6fd2807SJeff Garzik  *	sata_scr_valid - test whether SCRs are accessible
5158936fd732STejun Heo  *	@link: ATA link to test SCR accessibility for
5159c6fd2807SJeff Garzik  *
5160936fd732STejun Heo  *	Test whether SCRs are accessible for @link.
5161c6fd2807SJeff Garzik  *
5162c6fd2807SJeff Garzik  *	LOCKING:
5163c6fd2807SJeff Garzik  *	None.
5164c6fd2807SJeff Garzik  *
5165c6fd2807SJeff Garzik  *	RETURNS:
5166c6fd2807SJeff Garzik  *	1 if SCRs are accessible, 0 otherwise.
5167c6fd2807SJeff Garzik  */
5168936fd732STejun Heo int sata_scr_valid(struct ata_link *link)
5169c6fd2807SJeff Garzik {
5170936fd732STejun Heo 	struct ata_port *ap = link->ap;
5171936fd732STejun Heo 
5172a16abc0bSTejun Heo 	return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
5173c6fd2807SJeff Garzik }
5174c6fd2807SJeff Garzik 
5175c6fd2807SJeff Garzik /**
5176c6fd2807SJeff Garzik  *	sata_scr_read - read SCR register of the specified port
5177936fd732STejun Heo  *	@link: ATA link to read SCR for
5178c6fd2807SJeff Garzik  *	@reg: SCR to read
5179c6fd2807SJeff Garzik  *	@val: Place to store read value
5180c6fd2807SJeff Garzik  *
5181936fd732STejun Heo  *	Read SCR register @reg of @link into *@val.  This function is
5182633273a3STejun Heo  *	guaranteed to succeed if @link is ap->link, the cable type of
5183633273a3STejun Heo  *	the port is SATA and the port implements ->scr_read.
5184c6fd2807SJeff Garzik  *
5185c6fd2807SJeff Garzik  *	LOCKING:
5186633273a3STejun Heo  *	None if @link is ap->link.  Kernel thread context otherwise.
5187c6fd2807SJeff Garzik  *
5188c6fd2807SJeff Garzik  *	RETURNS:
5189c6fd2807SJeff Garzik  *	0 on success, negative errno on failure.
5190c6fd2807SJeff Garzik  */
5191936fd732STejun Heo int sata_scr_read(struct ata_link *link, int reg, u32 *val)
5192c6fd2807SJeff Garzik {
5193633273a3STejun Heo 	if (ata_is_host_link(link)) {
5194936fd732STejun Heo 		if (sata_scr_valid(link))
519582ef04fbSTejun Heo 			return link->ap->ops->scr_read(link, reg, val);
5196c6fd2807SJeff Garzik 		return -EOPNOTSUPP;
5197c6fd2807SJeff Garzik 	}
5198c6fd2807SJeff Garzik 
5199633273a3STejun Heo 	return sata_pmp_scr_read(link, reg, val);
5200633273a3STejun Heo }
5201633273a3STejun Heo 
5202c6fd2807SJeff Garzik /**
5203c6fd2807SJeff Garzik  *	sata_scr_write - write SCR register of the specified port
5204936fd732STejun Heo  *	@link: ATA link to write SCR for
5205c6fd2807SJeff Garzik  *	@reg: SCR to write
5206c6fd2807SJeff Garzik  *	@val: value to write
5207c6fd2807SJeff Garzik  *
5208936fd732STejun Heo  *	Write @val to SCR register @reg of @link.  This function is
5209633273a3STejun Heo  *	guaranteed to succeed if @link is ap->link, the cable type of
5210633273a3STejun Heo  *	the port is SATA and the port implements ->scr_read.
5211c6fd2807SJeff Garzik  *
5212c6fd2807SJeff Garzik  *	LOCKING:
5213633273a3STejun Heo  *	None if @link is ap->link.  Kernel thread context otherwise.
5214c6fd2807SJeff Garzik  *
5215c6fd2807SJeff Garzik  *	RETURNS:
5216c6fd2807SJeff Garzik  *	0 on success, negative errno on failure.
5217c6fd2807SJeff Garzik  */
5218936fd732STejun Heo int sata_scr_write(struct ata_link *link, int reg, u32 val)
5219c6fd2807SJeff Garzik {
5220633273a3STejun Heo 	if (ata_is_host_link(link)) {
5221936fd732STejun Heo 		if (sata_scr_valid(link))
522282ef04fbSTejun Heo 			return link->ap->ops->scr_write(link, reg, val);
5223c6fd2807SJeff Garzik 		return -EOPNOTSUPP;
5224c6fd2807SJeff Garzik 	}
5225c6fd2807SJeff Garzik 
5226633273a3STejun Heo 	return sata_pmp_scr_write(link, reg, val);
5227633273a3STejun Heo }
5228633273a3STejun Heo 
5229c6fd2807SJeff Garzik /**
5230c6fd2807SJeff Garzik  *	sata_scr_write_flush - write SCR register of the specified port and flush
5231936fd732STejun Heo  *	@link: ATA link to write SCR for
5232c6fd2807SJeff Garzik  *	@reg: SCR to write
5233c6fd2807SJeff Garzik  *	@val: value to write
5234c6fd2807SJeff Garzik  *
5235c6fd2807SJeff Garzik  *	This function is identical to sata_scr_write() except that this
5236c6fd2807SJeff Garzik  *	function performs flush after writing to the register.
5237c6fd2807SJeff Garzik  *
5238c6fd2807SJeff Garzik  *	LOCKING:
5239633273a3STejun Heo  *	None if @link is ap->link.  Kernel thread context otherwise.
5240c6fd2807SJeff Garzik  *
5241c6fd2807SJeff Garzik  *	RETURNS:
5242c6fd2807SJeff Garzik  *	0 on success, negative errno on failure.
5243c6fd2807SJeff Garzik  */
5244936fd732STejun Heo int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
5245c6fd2807SJeff Garzik {
5246633273a3STejun Heo 	if (ata_is_host_link(link)) {
5247da3dbb17STejun Heo 		int rc;
5248da3dbb17STejun Heo 
5249936fd732STejun Heo 		if (sata_scr_valid(link)) {
525082ef04fbSTejun Heo 			rc = link->ap->ops->scr_write(link, reg, val);
5251da3dbb17STejun Heo 			if (rc == 0)
525282ef04fbSTejun Heo 				rc = link->ap->ops->scr_read(link, reg, &val);
5253da3dbb17STejun Heo 			return rc;
5254c6fd2807SJeff Garzik 		}
5255c6fd2807SJeff Garzik 		return -EOPNOTSUPP;
5256c6fd2807SJeff Garzik 	}
5257c6fd2807SJeff Garzik 
5258633273a3STejun Heo 	return sata_pmp_scr_write(link, reg, val);
5259633273a3STejun Heo }
5260633273a3STejun Heo 
5261c6fd2807SJeff Garzik /**
5262b1c72916STejun Heo  *	ata_phys_link_online - test whether the given link is online
5263936fd732STejun Heo  *	@link: ATA link to test
5264c6fd2807SJeff Garzik  *
5265936fd732STejun Heo  *	Test whether @link is online.  Note that this function returns
5266936fd732STejun Heo  *	0 if online status of @link cannot be obtained, so
5267936fd732STejun Heo  *	ata_link_online(link) != !ata_link_offline(link).
5268c6fd2807SJeff Garzik  *
5269c6fd2807SJeff Garzik  *	LOCKING:
5270c6fd2807SJeff Garzik  *	None.
5271c6fd2807SJeff Garzik  *
5272c6fd2807SJeff Garzik  *	RETURNS:
5273b5b3fa38STejun Heo  *	True if the port online status is available and online.
5274c6fd2807SJeff Garzik  */
5275b1c72916STejun Heo bool ata_phys_link_online(struct ata_link *link)
5276c6fd2807SJeff Garzik {
5277c6fd2807SJeff Garzik 	u32 sstatus;
5278c6fd2807SJeff Garzik 
5279936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
52809913ff8aSTejun Heo 	    ata_sstatus_online(sstatus))
5281b5b3fa38STejun Heo 		return true;
5282b5b3fa38STejun Heo 	return false;
5283c6fd2807SJeff Garzik }
5284c6fd2807SJeff Garzik 
5285c6fd2807SJeff Garzik /**
5286b1c72916STejun Heo  *	ata_phys_link_offline - test whether the given link is offline
5287936fd732STejun Heo  *	@link: ATA link to test
5288c6fd2807SJeff Garzik  *
5289936fd732STejun Heo  *	Test whether @link is offline.  Note that this function
5290936fd732STejun Heo  *	returns 0 if offline status of @link cannot be obtained, so
5291936fd732STejun Heo  *	ata_link_online(link) != !ata_link_offline(link).
5292c6fd2807SJeff Garzik  *
5293c6fd2807SJeff Garzik  *	LOCKING:
5294c6fd2807SJeff Garzik  *	None.
5295c6fd2807SJeff Garzik  *
5296c6fd2807SJeff Garzik  *	RETURNS:
5297b5b3fa38STejun Heo  *	True if the port offline status is available and offline.
5298c6fd2807SJeff Garzik  */
5299b1c72916STejun Heo bool ata_phys_link_offline(struct ata_link *link)
5300c6fd2807SJeff Garzik {
5301c6fd2807SJeff Garzik 	u32 sstatus;
5302c6fd2807SJeff Garzik 
5303936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
53049913ff8aSTejun Heo 	    !ata_sstatus_online(sstatus))
5305b5b3fa38STejun Heo 		return true;
5306b5b3fa38STejun Heo 	return false;
5307c6fd2807SJeff Garzik }
5308c6fd2807SJeff Garzik 
5309b1c72916STejun Heo /**
5310b1c72916STejun Heo  *	ata_link_online - test whether the given link is online
5311b1c72916STejun Heo  *	@link: ATA link to test
5312b1c72916STejun Heo  *
5313b1c72916STejun Heo  *	Test whether @link is online.  This is identical to
5314b1c72916STejun Heo  *	ata_phys_link_online() when there's no slave link.  When
5315b1c72916STejun Heo  *	there's a slave link, this function should only be called on
5316b1c72916STejun Heo  *	the master link and will return true if any of M/S links is
5317b1c72916STejun Heo  *	online.
5318b1c72916STejun Heo  *
5319b1c72916STejun Heo  *	LOCKING:
5320b1c72916STejun Heo  *	None.
5321b1c72916STejun Heo  *
5322b1c72916STejun Heo  *	RETURNS:
5323b1c72916STejun Heo  *	True if the port online status is available and online.
5324b1c72916STejun Heo  */
5325b1c72916STejun Heo bool ata_link_online(struct ata_link *link)
5326b1c72916STejun Heo {
5327b1c72916STejun Heo 	struct ata_link *slave = link->ap->slave_link;
5328b1c72916STejun Heo 
5329b1c72916STejun Heo 	WARN_ON(link == slave);	/* shouldn't be called on slave link */
5330b1c72916STejun Heo 
5331b1c72916STejun Heo 	return ata_phys_link_online(link) ||
5332b1c72916STejun Heo 		(slave && ata_phys_link_online(slave));
5333b1c72916STejun Heo }
5334b1c72916STejun Heo 
5335b1c72916STejun Heo /**
5336b1c72916STejun Heo  *	ata_link_offline - test whether the given link is offline
5337b1c72916STejun Heo  *	@link: ATA link to test
5338b1c72916STejun Heo  *
5339b1c72916STejun Heo  *	Test whether @link is offline.  This is identical to
5340b1c72916STejun Heo  *	ata_phys_link_offline() when there's no slave link.  When
5341b1c72916STejun Heo  *	there's a slave link, this function should only be called on
5342b1c72916STejun Heo  *	the master link and will return true if both M/S links are
5343b1c72916STejun Heo  *	offline.
5344b1c72916STejun Heo  *
5345b1c72916STejun Heo  *	LOCKING:
5346b1c72916STejun Heo  *	None.
5347b1c72916STejun Heo  *
5348b1c72916STejun Heo  *	RETURNS:
5349b1c72916STejun Heo  *	True if the port offline status is available and offline.
5350b1c72916STejun Heo  */
5351b1c72916STejun Heo bool ata_link_offline(struct ata_link *link)
5352b1c72916STejun Heo {
5353b1c72916STejun Heo 	struct ata_link *slave = link->ap->slave_link;
5354b1c72916STejun Heo 
5355b1c72916STejun Heo 	WARN_ON(link == slave);	/* shouldn't be called on slave link */
5356b1c72916STejun Heo 
5357b1c72916STejun Heo 	return ata_phys_link_offline(link) &&
5358b1c72916STejun Heo 		(!slave || ata_phys_link_offline(slave));
5359b1c72916STejun Heo }
5360b1c72916STejun Heo 
53616ffa01d8STejun Heo #ifdef CONFIG_PM
5362bc6e7c4bSDan Williams static void ata_port_request_pm(struct ata_port *ap, pm_message_t mesg,
5363cca3974eSJeff Garzik 				unsigned int action, unsigned int ehi_flags,
5364bc6e7c4bSDan Williams 				bool async)
5365c6fd2807SJeff Garzik {
5366e3667ebfSTejun Heo 	struct ata_link *link;
53675ef41082SLin Ming 	unsigned long flags;
5368c6fd2807SJeff Garzik 
5369c6fd2807SJeff Garzik 	/* Previous resume operation might still be in
5370c6fd2807SJeff Garzik 	 * progress.  Wait for PM_PENDING to clear.
5371c6fd2807SJeff Garzik 	 */
5372c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_PM_PENDING) {
5373c6fd2807SJeff Garzik 		ata_port_wait_eh(ap);
5374c6fd2807SJeff Garzik 		WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5375c6fd2807SJeff Garzik 	}
5376c6fd2807SJeff Garzik 
5377c6fd2807SJeff Garzik 	/* request PM ops to EH */
5378c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
5379c6fd2807SJeff Garzik 
5380c6fd2807SJeff Garzik 	ap->pm_mesg = mesg;
5381c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_PM_PENDING;
53821eca4365STejun Heo 	ata_for_each_link(link, ap, HOST_FIRST) {
5383e3667ebfSTejun Heo 		link->eh_info.action |= action;
5384e3667ebfSTejun Heo 		link->eh_info.flags |= ehi_flags;
5385e3667ebfSTejun Heo 	}
5386c6fd2807SJeff Garzik 
5387c6fd2807SJeff Garzik 	ata_port_schedule_eh(ap);
5388c6fd2807SJeff Garzik 
5389c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
5390c6fd2807SJeff Garzik 
53912fcbdcb4SDan Williams 	if (!async) {
5392c6fd2807SJeff Garzik 		ata_port_wait_eh(ap);
5393c6fd2807SJeff Garzik 		WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5394c6fd2807SJeff Garzik 	}
5395c6fd2807SJeff Garzik }
5396c6fd2807SJeff Garzik 
539733574d68SLin Ming /*
5398bc6e7c4bSDan Williams  * On some hardware, device fails to respond after spun down for suspend.  As
5399bc6e7c4bSDan Williams  * the device won't be used before being resumed, we don't need to touch the
5400bc6e7c4bSDan Williams  * device.  Ask EH to skip the usual stuff and proceed directly to suspend.
540133574d68SLin Ming  *
540233574d68SLin Ming  * http://thread.gmane.org/gmane.linux.ide/46764
540333574d68SLin Ming  */
5404bc6e7c4bSDan Williams static const unsigned int ata_port_suspend_ehi = ATA_EHI_QUIET
5405bc6e7c4bSDan Williams 						 | ATA_EHI_NO_AUTOPSY
5406bc6e7c4bSDan Williams 						 | ATA_EHI_NO_RECOVERY;
5407bc6e7c4bSDan Williams 
5408bc6e7c4bSDan Williams static void ata_port_suspend(struct ata_port *ap, pm_message_t mesg)
5409bc6e7c4bSDan Williams {
5410bc6e7c4bSDan Williams 	ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, false);
54115ef41082SLin Ming }
54125ef41082SLin Ming 
5413bc6e7c4bSDan Williams static void ata_port_suspend_async(struct ata_port *ap, pm_message_t mesg)
5414bc6e7c4bSDan Williams {
5415bc6e7c4bSDan Williams 	ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, true);
5416bc6e7c4bSDan Williams }
5417bc6e7c4bSDan Williams 
5418bc6e7c4bSDan Williams static int ata_port_pm_suspend(struct device *dev)
54192fcbdcb4SDan Williams {
54202fcbdcb4SDan Williams 	struct ata_port *ap = to_ata_port(dev);
54212fcbdcb4SDan Williams 
54225ef41082SLin Ming 	if (pm_runtime_suspended(dev))
54235ef41082SLin Ming 		return 0;
54245ef41082SLin Ming 
5425bc6e7c4bSDan Williams 	ata_port_suspend(ap, PMSG_SUSPEND);
5426f5e6d0d0SAaron Lu 	return 0;
542733574d68SLin Ming }
542833574d68SLin Ming 
5429bc6e7c4bSDan Williams static int ata_port_pm_freeze(struct device *dev)
54302fcbdcb4SDan Williams {
54312fcbdcb4SDan Williams 	struct ata_port *ap = to_ata_port(dev);
54322fcbdcb4SDan Williams 
5433bc6e7c4bSDan Williams 	if (pm_runtime_suspended(dev))
5434bc6e7c4bSDan Williams 		return 0;
5435bc6e7c4bSDan Williams 
5436bc6e7c4bSDan Williams 	ata_port_suspend(ap, PMSG_FREEZE);
5437bc6e7c4bSDan Williams 	return 0;
54382fcbdcb4SDan Williams }
54392fcbdcb4SDan Williams 
5440bc6e7c4bSDan Williams static int ata_port_pm_poweroff(struct device *dev)
5441e90b1e5aSLin Ming {
5442bc6e7c4bSDan Williams 	ata_port_suspend(to_ata_port(dev), PMSG_HIBERNATE);
5443bc6e7c4bSDan Williams 	return 0;
5444bc6e7c4bSDan Williams }
5445e90b1e5aSLin Ming 
5446bc6e7c4bSDan Williams static const unsigned int ata_port_resume_ehi = ATA_EHI_NO_AUTOPSY
5447bc6e7c4bSDan Williams 						| ATA_EHI_QUIET;
5448bc6e7c4bSDan Williams 
5449bc6e7c4bSDan Williams static void ata_port_resume(struct ata_port *ap, pm_message_t mesg)
5450bc6e7c4bSDan Williams {
5451bc6e7c4bSDan Williams 	ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, false);
5452bc6e7c4bSDan Williams }
5453bc6e7c4bSDan Williams 
5454bc6e7c4bSDan Williams static void ata_port_resume_async(struct ata_port *ap, pm_message_t mesg)
5455bc6e7c4bSDan Williams {
5456bc6e7c4bSDan Williams 	ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, true);
5457bc6e7c4bSDan Williams }
5458bc6e7c4bSDan Williams 
5459bc6e7c4bSDan Williams static int ata_port_pm_resume(struct device *dev)
5460bc6e7c4bSDan Williams {
5461200421a8STodd Brandt 	ata_port_resume_async(to_ata_port(dev), PMSG_RESUME);
5462e90b1e5aSLin Ming 	pm_runtime_disable(dev);
5463e90b1e5aSLin Ming 	pm_runtime_set_active(dev);
5464e90b1e5aSLin Ming 	pm_runtime_enable(dev);
5465bc6e7c4bSDan Williams 	return 0;
5466e90b1e5aSLin Ming }
5467e90b1e5aSLin Ming 
54687e15e9beSAaron Lu /*
54697e15e9beSAaron Lu  * For ODDs, the upper layer will poll for media change every few seconds,
54707e15e9beSAaron Lu  * which will make it enter and leave suspend state every few seconds. And
54717e15e9beSAaron Lu  * as each suspend will cause a hard/soft reset, the gain of runtime suspend
54727e15e9beSAaron Lu  * is very little and the ODD may malfunction after constantly being reset.
54737e15e9beSAaron Lu  * So the idle callback here will not proceed to suspend if a non-ZPODD capable
54747e15e9beSAaron Lu  * ODD is attached to the port.
54757e15e9beSAaron Lu  */
54769ee4f393SLin Ming static int ata_port_runtime_idle(struct device *dev)
54779ee4f393SLin Ming {
54787e15e9beSAaron Lu 	struct ata_port *ap = to_ata_port(dev);
54797e15e9beSAaron Lu 	struct ata_link *link;
54807e15e9beSAaron Lu 	struct ata_device *adev;
54817e15e9beSAaron Lu 
54827e15e9beSAaron Lu 	ata_for_each_link(link, ap, HOST_FIRST) {
54837e15e9beSAaron Lu 		ata_for_each_dev(adev, link, ENABLED)
54847e15e9beSAaron Lu 			if (adev->class == ATA_DEV_ATAPI &&
54857e15e9beSAaron Lu 			    !zpodd_dev_enabled(adev))
54867e15e9beSAaron Lu 				return -EBUSY;
54877e15e9beSAaron Lu 	}
54887e15e9beSAaron Lu 
548945f0a85cSRafael J. Wysocki 	return 0;
54909ee4f393SLin Ming }
54919ee4f393SLin Ming 
5492a7ff60dbSAaron Lu static int ata_port_runtime_suspend(struct device *dev)
5493a7ff60dbSAaron Lu {
5494bc6e7c4bSDan Williams 	ata_port_suspend(to_ata_port(dev), PMSG_AUTO_SUSPEND);
5495bc6e7c4bSDan Williams 	return 0;
5496a7ff60dbSAaron Lu }
5497a7ff60dbSAaron Lu 
5498a7ff60dbSAaron Lu static int ata_port_runtime_resume(struct device *dev)
5499a7ff60dbSAaron Lu {
5500bc6e7c4bSDan Williams 	ata_port_resume(to_ata_port(dev), PMSG_AUTO_RESUME);
5501bc6e7c4bSDan Williams 	return 0;
5502a7ff60dbSAaron Lu }
5503a7ff60dbSAaron Lu 
55045ef41082SLin Ming static const struct dev_pm_ops ata_port_pm_ops = {
5505bc6e7c4bSDan Williams 	.suspend = ata_port_pm_suspend,
5506bc6e7c4bSDan Williams 	.resume = ata_port_pm_resume,
5507bc6e7c4bSDan Williams 	.freeze = ata_port_pm_freeze,
5508bc6e7c4bSDan Williams 	.thaw = ata_port_pm_resume,
5509bc6e7c4bSDan Williams 	.poweroff = ata_port_pm_poweroff,
5510bc6e7c4bSDan Williams 	.restore = ata_port_pm_resume,
55119ee4f393SLin Ming 
5512a7ff60dbSAaron Lu 	.runtime_suspend = ata_port_runtime_suspend,
5513a7ff60dbSAaron Lu 	.runtime_resume = ata_port_runtime_resume,
55149ee4f393SLin Ming 	.runtime_idle = ata_port_runtime_idle,
55155ef41082SLin Ming };
55165ef41082SLin Ming 
55172fcbdcb4SDan Williams /* sas ports don't participate in pm runtime management of ata_ports,
55182fcbdcb4SDan Williams  * and need to resume ata devices at the domain level, not the per-port
55192fcbdcb4SDan Williams  * level. sas suspend/resume is async to allow parallel port recovery
55202fcbdcb4SDan Williams  * since sas has multiple ata_port instances per Scsi_Host.
55212fcbdcb4SDan Williams  */
5522bc6e7c4bSDan Williams void ata_sas_port_suspend(struct ata_port *ap)
55232fcbdcb4SDan Williams {
5524bc6e7c4bSDan Williams 	ata_port_suspend_async(ap, PMSG_SUSPEND);
55252fcbdcb4SDan Williams }
5526bc6e7c4bSDan Williams EXPORT_SYMBOL_GPL(ata_sas_port_suspend);
55272fcbdcb4SDan Williams 
5528bc6e7c4bSDan Williams void ata_sas_port_resume(struct ata_port *ap)
55292fcbdcb4SDan Williams {
5530bc6e7c4bSDan Williams 	ata_port_resume_async(ap, PMSG_RESUME);
55312fcbdcb4SDan Williams }
5532bc6e7c4bSDan Williams EXPORT_SYMBOL_GPL(ata_sas_port_resume);
55332fcbdcb4SDan Williams 
5534c6fd2807SJeff Garzik /**
5535cca3974eSJeff Garzik  *	ata_host_suspend - suspend host
5536cca3974eSJeff Garzik  *	@host: host to suspend
5537c6fd2807SJeff Garzik  *	@mesg: PM message
5538c6fd2807SJeff Garzik  *
55395ef41082SLin Ming  *	Suspend @host.  Actual operation is performed by port suspend.
5540c6fd2807SJeff Garzik  */
5541cca3974eSJeff Garzik int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
5542c6fd2807SJeff Garzik {
554372ad6ec4SJeff Garzik 	host->dev->power.power_state = mesg;
55445ef41082SLin Ming 	return 0;
5545c6fd2807SJeff Garzik }
5546c6fd2807SJeff Garzik 
5547c6fd2807SJeff Garzik /**
5548cca3974eSJeff Garzik  *	ata_host_resume - resume host
5549cca3974eSJeff Garzik  *	@host: host to resume
5550c6fd2807SJeff Garzik  *
55515ef41082SLin Ming  *	Resume @host.  Actual operation is performed by port resume.
5552c6fd2807SJeff Garzik  */
5553cca3974eSJeff Garzik void ata_host_resume(struct ata_host *host)
5554c6fd2807SJeff Garzik {
555572ad6ec4SJeff Garzik 	host->dev->power.power_state = PMSG_ON;
5556c6fd2807SJeff Garzik }
55576ffa01d8STejun Heo #endif
5558c6fd2807SJeff Garzik 
55595ef41082SLin Ming struct device_type ata_port_type = {
55605ef41082SLin Ming 	.name = "ata_port",
55615ef41082SLin Ming #ifdef CONFIG_PM
55625ef41082SLin Ming 	.pm = &ata_port_pm_ops,
55635ef41082SLin Ming #endif
55645ef41082SLin Ming };
55655ef41082SLin Ming 
5566c6fd2807SJeff Garzik /**
5567c6fd2807SJeff Garzik  *	ata_dev_init - Initialize an ata_device structure
5568c6fd2807SJeff Garzik  *	@dev: Device structure to initialize
5569c6fd2807SJeff Garzik  *
5570c6fd2807SJeff Garzik  *	Initialize @dev in preparation for probing.
5571c6fd2807SJeff Garzik  *
5572c6fd2807SJeff Garzik  *	LOCKING:
5573c6fd2807SJeff Garzik  *	Inherited from caller.
5574c6fd2807SJeff Garzik  */
5575c6fd2807SJeff Garzik void ata_dev_init(struct ata_device *dev)
5576c6fd2807SJeff Garzik {
5577b1c72916STejun Heo 	struct ata_link *link = ata_dev_phys_link(dev);
55789af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
5579c6fd2807SJeff Garzik 	unsigned long flags;
5580c6fd2807SJeff Garzik 
5581b1c72916STejun Heo 	/* SATA spd limit is bound to the attached device, reset together */
55829af5c9c9STejun Heo 	link->sata_spd_limit = link->hw_sata_spd_limit;
55839af5c9c9STejun Heo 	link->sata_spd = 0;
5584c6fd2807SJeff Garzik 
5585c6fd2807SJeff Garzik 	/* High bits of dev->flags are used to record warm plug
5586c6fd2807SJeff Garzik 	 * requests which occur asynchronously.  Synchronize using
5587cca3974eSJeff Garzik 	 * host lock.
5588c6fd2807SJeff Garzik 	 */
5589c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
5590c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_INIT_MASK;
55913dcc323fSTejun Heo 	dev->horkage = 0;
5592c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
5593c6fd2807SJeff Garzik 
559499cf610aSTejun Heo 	memset((void *)dev + ATA_DEVICE_CLEAR_BEGIN, 0,
559599cf610aSTejun Heo 	       ATA_DEVICE_CLEAR_END - ATA_DEVICE_CLEAR_BEGIN);
5596c6fd2807SJeff Garzik 	dev->pio_mask = UINT_MAX;
5597c6fd2807SJeff Garzik 	dev->mwdma_mask = UINT_MAX;
5598c6fd2807SJeff Garzik 	dev->udma_mask = UINT_MAX;
5599c6fd2807SJeff Garzik }
5600c6fd2807SJeff Garzik 
5601c6fd2807SJeff Garzik /**
56024fb37a25STejun Heo  *	ata_link_init - Initialize an ata_link structure
56034fb37a25STejun Heo  *	@ap: ATA port link is attached to
56044fb37a25STejun Heo  *	@link: Link structure to initialize
56058989805dSTejun Heo  *	@pmp: Port multiplier port number
56064fb37a25STejun Heo  *
56074fb37a25STejun Heo  *	Initialize @link.
56084fb37a25STejun Heo  *
56094fb37a25STejun Heo  *	LOCKING:
56104fb37a25STejun Heo  *	Kernel thread context (may sleep)
56114fb37a25STejun Heo  */
5612fb7fd614STejun Heo void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
56134fb37a25STejun Heo {
56144fb37a25STejun Heo 	int i;
56154fb37a25STejun Heo 
56164fb37a25STejun Heo 	/* clear everything except for devices */
5617d9027470SGwendal Grignou 	memset((void *)link + ATA_LINK_CLEAR_BEGIN, 0,
5618d9027470SGwendal Grignou 	       ATA_LINK_CLEAR_END - ATA_LINK_CLEAR_BEGIN);
56194fb37a25STejun Heo 
56204fb37a25STejun Heo 	link->ap = ap;
56218989805dSTejun Heo 	link->pmp = pmp;
56224fb37a25STejun Heo 	link->active_tag = ATA_TAG_POISON;
56234fb37a25STejun Heo 	link->hw_sata_spd_limit = UINT_MAX;
56244fb37a25STejun Heo 
56254fb37a25STejun Heo 	/* can't use iterator, ap isn't initialized yet */
56264fb37a25STejun Heo 	for (i = 0; i < ATA_MAX_DEVICES; i++) {
56274fb37a25STejun Heo 		struct ata_device *dev = &link->device[i];
56284fb37a25STejun Heo 
56294fb37a25STejun Heo 		dev->link = link;
56304fb37a25STejun Heo 		dev->devno = dev - link->device;
5631110f66d2STejun Heo #ifdef CONFIG_ATA_ACPI
5632110f66d2STejun Heo 		dev->gtf_filter = ata_acpi_gtf_filter;
5633110f66d2STejun Heo #endif
56344fb37a25STejun Heo 		ata_dev_init(dev);
56354fb37a25STejun Heo 	}
56364fb37a25STejun Heo }
56374fb37a25STejun Heo 
56384fb37a25STejun Heo /**
56394fb37a25STejun Heo  *	sata_link_init_spd - Initialize link->sata_spd_limit
56404fb37a25STejun Heo  *	@link: Link to configure sata_spd_limit for
56414fb37a25STejun Heo  *
56424fb37a25STejun Heo  *	Initialize @link->[hw_]sata_spd_limit to the currently
56434fb37a25STejun Heo  *	configured value.
56444fb37a25STejun Heo  *
56454fb37a25STejun Heo  *	LOCKING:
56464fb37a25STejun Heo  *	Kernel thread context (may sleep).
56474fb37a25STejun Heo  *
56484fb37a25STejun Heo  *	RETURNS:
56494fb37a25STejun Heo  *	0 on success, -errno on failure.
56504fb37a25STejun Heo  */
5651fb7fd614STejun Heo int sata_link_init_spd(struct ata_link *link)
56524fb37a25STejun Heo {
565333267325STejun Heo 	u8 spd;
56544fb37a25STejun Heo 	int rc;
56554fb37a25STejun Heo 
5656d127ea7bSTejun Heo 	rc = sata_scr_read(link, SCR_CONTROL, &link->saved_scontrol);
56574fb37a25STejun Heo 	if (rc)
56584fb37a25STejun Heo 		return rc;
56594fb37a25STejun Heo 
5660d127ea7bSTejun Heo 	spd = (link->saved_scontrol >> 4) & 0xf;
56614fb37a25STejun Heo 	if (spd)
56624fb37a25STejun Heo 		link->hw_sata_spd_limit &= (1 << spd) - 1;
56634fb37a25STejun Heo 
566405944bdfSTejun Heo 	ata_force_link_limits(link);
566533267325STejun Heo 
56664fb37a25STejun Heo 	link->sata_spd_limit = link->hw_sata_spd_limit;
56674fb37a25STejun Heo 
56684fb37a25STejun Heo 	return 0;
56694fb37a25STejun Heo }
56704fb37a25STejun Heo 
56714fb37a25STejun Heo /**
5672f3187195STejun Heo  *	ata_port_alloc - allocate and initialize basic ATA port resources
5673f3187195STejun Heo  *	@host: ATA host this allocated port belongs to
5674c6fd2807SJeff Garzik  *
5675f3187195STejun Heo  *	Allocate and initialize basic ATA port resources.
5676f3187195STejun Heo  *
5677f3187195STejun Heo  *	RETURNS:
5678f3187195STejun Heo  *	Allocate ATA port on success, NULL on failure.
5679c6fd2807SJeff Garzik  *
5680c6fd2807SJeff Garzik  *	LOCKING:
5681f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5682c6fd2807SJeff Garzik  */
5683f3187195STejun Heo struct ata_port *ata_port_alloc(struct ata_host *host)
5684c6fd2807SJeff Garzik {
5685f3187195STejun Heo 	struct ata_port *ap;
5686c6fd2807SJeff Garzik 
5687f3187195STejun Heo 	DPRINTK("ENTER\n");
5688f3187195STejun Heo 
5689f3187195STejun Heo 	ap = kzalloc(sizeof(*ap), GFP_KERNEL);
5690f3187195STejun Heo 	if (!ap)
5691f3187195STejun Heo 		return NULL;
5692f3187195STejun Heo 
56937b3a24c5SMaxime Bizon 	ap->pflags |= ATA_PFLAG_INITIALIZING | ATA_PFLAG_FROZEN;
5694cca3974eSJeff Garzik 	ap->lock = &host->lock;
5695f3187195STejun Heo 	ap->print_id = -1;
5696e628dc99SDavid Milburn 	ap->local_port_no = -1;
5697cca3974eSJeff Garzik 	ap->host = host;
5698f3187195STejun Heo 	ap->dev = host->dev;
5699c6fd2807SJeff Garzik 
5700c6fd2807SJeff Garzik #if defined(ATA_VERBOSE_DEBUG)
5701c6fd2807SJeff Garzik 	/* turn on all debugging levels */
5702c6fd2807SJeff Garzik 	ap->msg_enable = 0x00FF;
5703c6fd2807SJeff Garzik #elif defined(ATA_DEBUG)
5704c6fd2807SJeff Garzik 	ap->msg_enable = ATA_MSG_DRV | ATA_MSG_INFO | ATA_MSG_CTL | ATA_MSG_WARN | ATA_MSG_ERR;
5705c6fd2807SJeff Garzik #else
5706c6fd2807SJeff Garzik 	ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
5707c6fd2807SJeff Garzik #endif
5708c6fd2807SJeff Garzik 
5709ad72cf98STejun Heo 	mutex_init(&ap->scsi_scan_mutex);
571065f27f38SDavid Howells 	INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
571165f27f38SDavid Howells 	INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
5712c6fd2807SJeff Garzik 	INIT_LIST_HEAD(&ap->eh_done_q);
5713c6fd2807SJeff Garzik 	init_waitqueue_head(&ap->eh_wait_q);
571445fabbb7SElias Oltmanns 	init_completion(&ap->park_req_pending);
57155ddf24c5STejun Heo 	init_timer_deferrable(&ap->fastdrain_timer);
57165ddf24c5STejun Heo 	ap->fastdrain_timer.function = ata_eh_fastdrain_timerfn;
57175ddf24c5STejun Heo 	ap->fastdrain_timer.data = (unsigned long)ap;
5718c6fd2807SJeff Garzik 
5719c6fd2807SJeff Garzik 	ap->cbl = ATA_CBL_NONE;
5720c6fd2807SJeff Garzik 
57218989805dSTejun Heo 	ata_link_init(ap, &ap->link, 0);
5722c6fd2807SJeff Garzik 
5723c6fd2807SJeff Garzik #ifdef ATA_IRQ_TRAP
5724c6fd2807SJeff Garzik 	ap->stats.unhandled_irq = 1;
5725c6fd2807SJeff Garzik 	ap->stats.idle_irq = 1;
5726c6fd2807SJeff Garzik #endif
5727270390e1STejun Heo 	ata_sff_port_init(ap);
5728270390e1STejun Heo 
5729c6fd2807SJeff Garzik 	return ap;
5730c6fd2807SJeff Garzik }
5731c6fd2807SJeff Garzik 
5732f0d36efdSTejun Heo static void ata_host_release(struct device *gendev, void *res)
5733f0d36efdSTejun Heo {
5734f0d36efdSTejun Heo 	struct ata_host *host = dev_get_drvdata(gendev);
5735f0d36efdSTejun Heo 	int i;
5736f0d36efdSTejun Heo 
5737f0d36efdSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
5738f0d36efdSTejun Heo 		struct ata_port *ap = host->ports[i];
5739f0d36efdSTejun Heo 
5740ecef7253STejun Heo 		if (!ap)
5741ecef7253STejun Heo 			continue;
5742ecef7253STejun Heo 
57434911487aSTejun Heo 		if (ap->scsi_host)
57441aa506e4STejun Heo 			scsi_host_put(ap->scsi_host);
57451aa506e4STejun Heo 
5746633273a3STejun Heo 		kfree(ap->pmp_link);
5747b1c72916STejun Heo 		kfree(ap->slave_link);
57484911487aSTejun Heo 		kfree(ap);
57491aa506e4STejun Heo 		host->ports[i] = NULL;
57501aa506e4STejun Heo 	}
57511aa506e4STejun Heo 
57521aa56ccaSTejun Heo 	dev_set_drvdata(gendev, NULL);
5753f0d36efdSTejun Heo }
5754f0d36efdSTejun Heo 
5755c6fd2807SJeff Garzik /**
5756f3187195STejun Heo  *	ata_host_alloc - allocate and init basic ATA host resources
5757f3187195STejun Heo  *	@dev: generic device this host is associated with
5758f3187195STejun Heo  *	@max_ports: maximum number of ATA ports associated with this host
5759f3187195STejun Heo  *
5760f3187195STejun Heo  *	Allocate and initialize basic ATA host resources.  LLD calls
5761f3187195STejun Heo  *	this function to allocate a host, initializes it fully and
5762f3187195STejun Heo  *	attaches it using ata_host_register().
5763f3187195STejun Heo  *
5764f3187195STejun Heo  *	@max_ports ports are allocated and host->n_ports is
5765f3187195STejun Heo  *	initialized to @max_ports.  The caller is allowed to decrease
5766f3187195STejun Heo  *	host->n_ports before calling ata_host_register().  The unused
5767f3187195STejun Heo  *	ports will be automatically freed on registration.
5768f3187195STejun Heo  *
5769f3187195STejun Heo  *	RETURNS:
5770f3187195STejun Heo  *	Allocate ATA host on success, NULL on failure.
5771f3187195STejun Heo  *
5772f3187195STejun Heo  *	LOCKING:
5773f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5774f3187195STejun Heo  */
5775f3187195STejun Heo struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
5776f3187195STejun Heo {
5777f3187195STejun Heo 	struct ata_host *host;
5778f3187195STejun Heo 	size_t sz;
5779f3187195STejun Heo 	int i;
5780f3187195STejun Heo 
5781f3187195STejun Heo 	DPRINTK("ENTER\n");
5782f3187195STejun Heo 
5783f3187195STejun Heo 	if (!devres_open_group(dev, NULL, GFP_KERNEL))
5784f3187195STejun Heo 		return NULL;
5785f3187195STejun Heo 
5786f3187195STejun Heo 	/* alloc a container for our list of ATA ports (buses) */
5787f3187195STejun Heo 	sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
5788f3187195STejun Heo 	/* alloc a container for our list of ATA ports (buses) */
5789f3187195STejun Heo 	host = devres_alloc(ata_host_release, sz, GFP_KERNEL);
5790f3187195STejun Heo 	if (!host)
5791f3187195STejun Heo 		goto err_out;
5792f3187195STejun Heo 
5793f3187195STejun Heo 	devres_add(dev, host);
5794f3187195STejun Heo 	dev_set_drvdata(dev, host);
5795f3187195STejun Heo 
5796f3187195STejun Heo 	spin_lock_init(&host->lock);
5797c0c362b6STejun Heo 	mutex_init(&host->eh_mutex);
5798f3187195STejun Heo 	host->dev = dev;
5799f3187195STejun Heo 	host->n_ports = max_ports;
5800f3187195STejun Heo 
5801f3187195STejun Heo 	/* allocate ports bound to this host */
5802f3187195STejun Heo 	for (i = 0; i < max_ports; i++) {
5803f3187195STejun Heo 		struct ata_port *ap;
5804f3187195STejun Heo 
5805f3187195STejun Heo 		ap = ata_port_alloc(host);
5806f3187195STejun Heo 		if (!ap)
5807f3187195STejun Heo 			goto err_out;
5808f3187195STejun Heo 
5809f3187195STejun Heo 		ap->port_no = i;
5810f3187195STejun Heo 		host->ports[i] = ap;
5811f3187195STejun Heo 	}
5812f3187195STejun Heo 
5813f3187195STejun Heo 	devres_remove_group(dev, NULL);
5814f3187195STejun Heo 	return host;
5815f3187195STejun Heo 
5816f3187195STejun Heo  err_out:
5817f3187195STejun Heo 	devres_release_group(dev, NULL);
5818f3187195STejun Heo 	return NULL;
5819f3187195STejun Heo }
5820f3187195STejun Heo 
5821f3187195STejun Heo /**
5822f5cda257STejun Heo  *	ata_host_alloc_pinfo - alloc host and init with port_info array
5823f5cda257STejun Heo  *	@dev: generic device this host is associated with
5824f5cda257STejun Heo  *	@ppi: array of ATA port_info to initialize host with
5825f5cda257STejun Heo  *	@n_ports: number of ATA ports attached to this host
5826f5cda257STejun Heo  *
5827f5cda257STejun Heo  *	Allocate ATA host and initialize with info from @ppi.  If NULL
5828f5cda257STejun Heo  *	terminated, @ppi may contain fewer entries than @n_ports.  The
5829f5cda257STejun Heo  *	last entry will be used for the remaining ports.
5830f5cda257STejun Heo  *
5831f5cda257STejun Heo  *	RETURNS:
5832f5cda257STejun Heo  *	Allocate ATA host on success, NULL on failure.
5833f5cda257STejun Heo  *
5834f5cda257STejun Heo  *	LOCKING:
5835f5cda257STejun Heo  *	Inherited from calling layer (may sleep).
5836f5cda257STejun Heo  */
5837f5cda257STejun Heo struct ata_host *ata_host_alloc_pinfo(struct device *dev,
5838f5cda257STejun Heo 				      const struct ata_port_info * const * ppi,
5839f5cda257STejun Heo 				      int n_ports)
5840f5cda257STejun Heo {
5841f5cda257STejun Heo 	const struct ata_port_info *pi;
5842f5cda257STejun Heo 	struct ata_host *host;
5843f5cda257STejun Heo 	int i, j;
5844f5cda257STejun Heo 
5845f5cda257STejun Heo 	host = ata_host_alloc(dev, n_ports);
5846f5cda257STejun Heo 	if (!host)
5847f5cda257STejun Heo 		return NULL;
5848f5cda257STejun Heo 
5849f5cda257STejun Heo 	for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
5850f5cda257STejun Heo 		struct ata_port *ap = host->ports[i];
5851f5cda257STejun Heo 
5852f5cda257STejun Heo 		if (ppi[j])
5853f5cda257STejun Heo 			pi = ppi[j++];
5854f5cda257STejun Heo 
5855f5cda257STejun Heo 		ap->pio_mask = pi->pio_mask;
5856f5cda257STejun Heo 		ap->mwdma_mask = pi->mwdma_mask;
5857f5cda257STejun Heo 		ap->udma_mask = pi->udma_mask;
5858f5cda257STejun Heo 		ap->flags |= pi->flags;
58590c88758bSTejun Heo 		ap->link.flags |= pi->link_flags;
5860f5cda257STejun Heo 		ap->ops = pi->port_ops;
5861f5cda257STejun Heo 
5862f5cda257STejun Heo 		if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
5863f5cda257STejun Heo 			host->ops = pi->port_ops;
5864f5cda257STejun Heo 	}
5865f5cda257STejun Heo 
5866f5cda257STejun Heo 	return host;
5867f5cda257STejun Heo }
5868f5cda257STejun Heo 
5869b1c72916STejun Heo /**
5870b1c72916STejun Heo  *	ata_slave_link_init - initialize slave link
5871b1c72916STejun Heo  *	@ap: port to initialize slave link for
5872b1c72916STejun Heo  *
5873b1c72916STejun Heo  *	Create and initialize slave link for @ap.  This enables slave
5874b1c72916STejun Heo  *	link handling on the port.
5875b1c72916STejun Heo  *
5876b1c72916STejun Heo  *	In libata, a port contains links and a link contains devices.
5877b1c72916STejun Heo  *	There is single host link but if a PMP is attached to it,
5878b1c72916STejun Heo  *	there can be multiple fan-out links.  On SATA, there's usually
5879b1c72916STejun Heo  *	a single device connected to a link but PATA and SATA
5880b1c72916STejun Heo  *	controllers emulating TF based interface can have two - master
5881b1c72916STejun Heo  *	and slave.
5882b1c72916STejun Heo  *
5883b1c72916STejun Heo  *	However, there are a few controllers which don't fit into this
5884b1c72916STejun Heo  *	abstraction too well - SATA controllers which emulate TF
5885b1c72916STejun Heo  *	interface with both master and slave devices but also have
5886b1c72916STejun Heo  *	separate SCR register sets for each device.  These controllers
5887b1c72916STejun Heo  *	need separate links for physical link handling
5888b1c72916STejun Heo  *	(e.g. onlineness, link speed) but should be treated like a
5889b1c72916STejun Heo  *	traditional M/S controller for everything else (e.g. command
5890b1c72916STejun Heo  *	issue, softreset).
5891b1c72916STejun Heo  *
5892b1c72916STejun Heo  *	slave_link is libata's way of handling this class of
5893b1c72916STejun Heo  *	controllers without impacting core layer too much.  For
5894b1c72916STejun Heo  *	anything other than physical link handling, the default host
5895b1c72916STejun Heo  *	link is used for both master and slave.  For physical link
5896b1c72916STejun Heo  *	handling, separate @ap->slave_link is used.  All dirty details
5897b1c72916STejun Heo  *	are implemented inside libata core layer.  From LLD's POV, the
5898b1c72916STejun Heo  *	only difference is that prereset, hardreset and postreset are
5899b1c72916STejun Heo  *	called once more for the slave link, so the reset sequence
5900b1c72916STejun Heo  *	looks like the following.
5901b1c72916STejun Heo  *
5902b1c72916STejun Heo  *	prereset(M) -> prereset(S) -> hardreset(M) -> hardreset(S) ->
5903b1c72916STejun Heo  *	softreset(M) -> postreset(M) -> postreset(S)
5904b1c72916STejun Heo  *
5905b1c72916STejun Heo  *	Note that softreset is called only for the master.  Softreset
5906b1c72916STejun Heo  *	resets both M/S by definition, so SRST on master should handle
5907b1c72916STejun Heo  *	both (the standard method will work just fine).
5908b1c72916STejun Heo  *
5909b1c72916STejun Heo  *	LOCKING:
5910b1c72916STejun Heo  *	Should be called before host is registered.
5911b1c72916STejun Heo  *
5912b1c72916STejun Heo  *	RETURNS:
5913b1c72916STejun Heo  *	0 on success, -errno on failure.
5914b1c72916STejun Heo  */
5915b1c72916STejun Heo int ata_slave_link_init(struct ata_port *ap)
5916b1c72916STejun Heo {
5917b1c72916STejun Heo 	struct ata_link *link;
5918b1c72916STejun Heo 
5919b1c72916STejun Heo 	WARN_ON(ap->slave_link);
5920b1c72916STejun Heo 	WARN_ON(ap->flags & ATA_FLAG_PMP);
5921b1c72916STejun Heo 
5922b1c72916STejun Heo 	link = kzalloc(sizeof(*link), GFP_KERNEL);
5923b1c72916STejun Heo 	if (!link)
5924b1c72916STejun Heo 		return -ENOMEM;
5925b1c72916STejun Heo 
5926b1c72916STejun Heo 	ata_link_init(ap, link, 1);
5927b1c72916STejun Heo 	ap->slave_link = link;
5928b1c72916STejun Heo 	return 0;
5929b1c72916STejun Heo }
5930b1c72916STejun Heo 
593132ebbc0cSTejun Heo static void ata_host_stop(struct device *gendev, void *res)
593232ebbc0cSTejun Heo {
593332ebbc0cSTejun Heo 	struct ata_host *host = dev_get_drvdata(gendev);
593432ebbc0cSTejun Heo 	int i;
593532ebbc0cSTejun Heo 
593632ebbc0cSTejun Heo 	WARN_ON(!(host->flags & ATA_HOST_STARTED));
593732ebbc0cSTejun Heo 
593832ebbc0cSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
593932ebbc0cSTejun Heo 		struct ata_port *ap = host->ports[i];
594032ebbc0cSTejun Heo 
594132ebbc0cSTejun Heo 		if (ap->ops->port_stop)
594232ebbc0cSTejun Heo 			ap->ops->port_stop(ap);
594332ebbc0cSTejun Heo 	}
594432ebbc0cSTejun Heo 
594532ebbc0cSTejun Heo 	if (host->ops->host_stop)
594632ebbc0cSTejun Heo 		host->ops->host_stop(host);
594732ebbc0cSTejun Heo }
594832ebbc0cSTejun Heo 
5949f5cda257STejun Heo /**
5950029cfd6bSTejun Heo  *	ata_finalize_port_ops - finalize ata_port_operations
5951029cfd6bSTejun Heo  *	@ops: ata_port_operations to finalize
5952029cfd6bSTejun Heo  *
5953029cfd6bSTejun Heo  *	An ata_port_operations can inherit from another ops and that
5954029cfd6bSTejun Heo  *	ops can again inherit from another.  This can go on as many
5955029cfd6bSTejun Heo  *	times as necessary as long as there is no loop in the
5956029cfd6bSTejun Heo  *	inheritance chain.
5957029cfd6bSTejun Heo  *
5958029cfd6bSTejun Heo  *	Ops tables are finalized when the host is started.  NULL or
5959029cfd6bSTejun Heo  *	unspecified entries are inherited from the closet ancestor
5960029cfd6bSTejun Heo  *	which has the method and the entry is populated with it.
5961029cfd6bSTejun Heo  *	After finalization, the ops table directly points to all the
5962029cfd6bSTejun Heo  *	methods and ->inherits is no longer necessary and cleared.
5963029cfd6bSTejun Heo  *
5964029cfd6bSTejun Heo  *	Using ATA_OP_NULL, inheriting ops can force a method to NULL.
5965029cfd6bSTejun Heo  *
5966029cfd6bSTejun Heo  *	LOCKING:
5967029cfd6bSTejun Heo  *	None.
5968029cfd6bSTejun Heo  */
5969029cfd6bSTejun Heo static void ata_finalize_port_ops(struct ata_port_operations *ops)
5970029cfd6bSTejun Heo {
59712da67659SPradeep Singh Rautela 	static DEFINE_SPINLOCK(lock);
5972029cfd6bSTejun Heo 	const struct ata_port_operations *cur;
5973029cfd6bSTejun Heo 	void **begin = (void **)ops;
5974029cfd6bSTejun Heo 	void **end = (void **)&ops->inherits;
5975029cfd6bSTejun Heo 	void **pp;
5976029cfd6bSTejun Heo 
5977029cfd6bSTejun Heo 	if (!ops || !ops->inherits)
5978029cfd6bSTejun Heo 		return;
5979029cfd6bSTejun Heo 
5980029cfd6bSTejun Heo 	spin_lock(&lock);
5981029cfd6bSTejun Heo 
5982029cfd6bSTejun Heo 	for (cur = ops->inherits; cur; cur = cur->inherits) {
5983029cfd6bSTejun Heo 		void **inherit = (void **)cur;
5984029cfd6bSTejun Heo 
5985029cfd6bSTejun Heo 		for (pp = begin; pp < end; pp++, inherit++)
5986029cfd6bSTejun Heo 			if (!*pp)
5987029cfd6bSTejun Heo 				*pp = *inherit;
5988029cfd6bSTejun Heo 	}
5989029cfd6bSTejun Heo 
5990029cfd6bSTejun Heo 	for (pp = begin; pp < end; pp++)
5991029cfd6bSTejun Heo 		if (IS_ERR(*pp))
5992029cfd6bSTejun Heo 			*pp = NULL;
5993029cfd6bSTejun Heo 
5994029cfd6bSTejun Heo 	ops->inherits = NULL;
5995029cfd6bSTejun Heo 
5996029cfd6bSTejun Heo 	spin_unlock(&lock);
5997029cfd6bSTejun Heo }
5998029cfd6bSTejun Heo 
5999029cfd6bSTejun Heo /**
6000ecef7253STejun Heo  *	ata_host_start - start and freeze ports of an ATA host
6001ecef7253STejun Heo  *	@host: ATA host to start ports for
6002ecef7253STejun Heo  *
6003ecef7253STejun Heo  *	Start and then freeze ports of @host.  Started status is
6004ecef7253STejun Heo  *	recorded in host->flags, so this function can be called
6005ecef7253STejun Heo  *	multiple times.  Ports are guaranteed to get started only
6006f3187195STejun Heo  *	once.  If host->ops isn't initialized yet, its set to the
6007f3187195STejun Heo  *	first non-dummy port ops.
6008ecef7253STejun Heo  *
6009ecef7253STejun Heo  *	LOCKING:
6010ecef7253STejun Heo  *	Inherited from calling layer (may sleep).
6011ecef7253STejun Heo  *
6012ecef7253STejun Heo  *	RETURNS:
6013ecef7253STejun Heo  *	0 if all ports are started successfully, -errno otherwise.
6014ecef7253STejun Heo  */
6015ecef7253STejun Heo int ata_host_start(struct ata_host *host)
6016ecef7253STejun Heo {
601732ebbc0cSTejun Heo 	int have_stop = 0;
601832ebbc0cSTejun Heo 	void *start_dr = NULL;
6019ecef7253STejun Heo 	int i, rc;
6020ecef7253STejun Heo 
6021ecef7253STejun Heo 	if (host->flags & ATA_HOST_STARTED)
6022ecef7253STejun Heo 		return 0;
6023ecef7253STejun Heo 
6024029cfd6bSTejun Heo 	ata_finalize_port_ops(host->ops);
6025029cfd6bSTejun Heo 
6026ecef7253STejun Heo 	for (i = 0; i < host->n_ports; i++) {
6027ecef7253STejun Heo 		struct ata_port *ap = host->ports[i];
6028ecef7253STejun Heo 
6029029cfd6bSTejun Heo 		ata_finalize_port_ops(ap->ops);
6030029cfd6bSTejun Heo 
6031f3187195STejun Heo 		if (!host->ops && !ata_port_is_dummy(ap))
6032f3187195STejun Heo 			host->ops = ap->ops;
6033f3187195STejun Heo 
603432ebbc0cSTejun Heo 		if (ap->ops->port_stop)
603532ebbc0cSTejun Heo 			have_stop = 1;
603632ebbc0cSTejun Heo 	}
603732ebbc0cSTejun Heo 
603832ebbc0cSTejun Heo 	if (host->ops->host_stop)
603932ebbc0cSTejun Heo 		have_stop = 1;
604032ebbc0cSTejun Heo 
604132ebbc0cSTejun Heo 	if (have_stop) {
604232ebbc0cSTejun Heo 		start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
604332ebbc0cSTejun Heo 		if (!start_dr)
604432ebbc0cSTejun Heo 			return -ENOMEM;
604532ebbc0cSTejun Heo 	}
604632ebbc0cSTejun Heo 
604732ebbc0cSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
604832ebbc0cSTejun Heo 		struct ata_port *ap = host->ports[i];
604932ebbc0cSTejun Heo 
6050ecef7253STejun Heo 		if (ap->ops->port_start) {
6051ecef7253STejun Heo 			rc = ap->ops->port_start(ap);
6052ecef7253STejun Heo 			if (rc) {
60530f9fe9b7SAlan Cox 				if (rc != -ENODEV)
6054a44fec1fSJoe Perches 					dev_err(host->dev,
6055a44fec1fSJoe Perches 						"failed to start port %d (errno=%d)\n",
6056a44fec1fSJoe Perches 						i, rc);
6057ecef7253STejun Heo 				goto err_out;
6058ecef7253STejun Heo 			}
6059ecef7253STejun Heo 		}
6060ecef7253STejun Heo 		ata_eh_freeze_port(ap);
6061ecef7253STejun Heo 	}
6062ecef7253STejun Heo 
606332ebbc0cSTejun Heo 	if (start_dr)
606432ebbc0cSTejun Heo 		devres_add(host->dev, start_dr);
6065ecef7253STejun Heo 	host->flags |= ATA_HOST_STARTED;
6066ecef7253STejun Heo 	return 0;
6067ecef7253STejun Heo 
6068ecef7253STejun Heo  err_out:
6069ecef7253STejun Heo 	while (--i >= 0) {
6070ecef7253STejun Heo 		struct ata_port *ap = host->ports[i];
6071ecef7253STejun Heo 
6072ecef7253STejun Heo 		if (ap->ops->port_stop)
6073ecef7253STejun Heo 			ap->ops->port_stop(ap);
6074ecef7253STejun Heo 	}
607532ebbc0cSTejun Heo 	devres_free(start_dr);
6076ecef7253STejun Heo 	return rc;
6077ecef7253STejun Heo }
6078ecef7253STejun Heo 
6079ecef7253STejun Heo /**
60808d8e7d13SDan Williams  *	ata_sas_host_init - Initialize a host struct for sas (ipr, libsas)
6081cca3974eSJeff Garzik  *	@host:	host to initialize
6082cca3974eSJeff Garzik  *	@dev:	device host is attached to
6083c6fd2807SJeff Garzik  *	@ops:	port_ops
6084c6fd2807SJeff Garzik  *
6085c6fd2807SJeff Garzik  */
6086cca3974eSJeff Garzik void ata_host_init(struct ata_host *host, struct device *dev,
60878d8e7d13SDan Williams 		   struct ata_port_operations *ops)
6088c6fd2807SJeff Garzik {
6089cca3974eSJeff Garzik 	spin_lock_init(&host->lock);
6090c0c362b6STejun Heo 	mutex_init(&host->eh_mutex);
6091cca3974eSJeff Garzik 	host->dev = dev;
6092cca3974eSJeff Garzik 	host->ops = ops;
6093c6fd2807SJeff Garzik }
6094c6fd2807SJeff Garzik 
60959508a66fSDan Williams void __ata_port_probe(struct ata_port *ap)
609679318057SArjan van de Ven {
609779318057SArjan van de Ven 	struct ata_eh_info *ehi = &ap->link.eh_info;
609879318057SArjan van de Ven 	unsigned long flags;
609979318057SArjan van de Ven 
610079318057SArjan van de Ven 	/* kick EH for boot probing */
610179318057SArjan van de Ven 	spin_lock_irqsave(ap->lock, flags);
610279318057SArjan van de Ven 
610379318057SArjan van de Ven 	ehi->probe_mask |= ATA_ALL_DEVICES;
61046b7ae954STejun Heo 	ehi->action |= ATA_EH_RESET;
610579318057SArjan van de Ven 	ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
610679318057SArjan van de Ven 
610779318057SArjan van de Ven 	ap->pflags &= ~ATA_PFLAG_INITIALIZING;
610879318057SArjan van de Ven 	ap->pflags |= ATA_PFLAG_LOADING;
610979318057SArjan van de Ven 	ata_port_schedule_eh(ap);
611079318057SArjan van de Ven 
611179318057SArjan van de Ven 	spin_unlock_irqrestore(ap->lock, flags);
61129508a66fSDan Williams }
611379318057SArjan van de Ven 
61149508a66fSDan Williams int ata_port_probe(struct ata_port *ap)
61159508a66fSDan Williams {
61169508a66fSDan Williams 	int rc = 0;
61179508a66fSDan Williams 
61189508a66fSDan Williams 	if (ap->ops->error_handler) {
61199508a66fSDan Williams 		__ata_port_probe(ap);
612079318057SArjan van de Ven 		ata_port_wait_eh(ap);
612179318057SArjan van de Ven 	} else {
612279318057SArjan van de Ven 		DPRINTK("ata%u: bus probe begin\n", ap->print_id);
612379318057SArjan van de Ven 		rc = ata_bus_probe(ap);
612479318057SArjan van de Ven 		DPRINTK("ata%u: bus probe end\n", ap->print_id);
6125238c9cf9SJames Bottomley 	}
6126238c9cf9SJames Bottomley 	return rc;
6127238c9cf9SJames Bottomley }
612879318057SArjan van de Ven 
6129238c9cf9SJames Bottomley 
6130238c9cf9SJames Bottomley static void async_port_probe(void *data, async_cookie_t cookie)
6131238c9cf9SJames Bottomley {
6132238c9cf9SJames Bottomley 	struct ata_port *ap = data;
6133238c9cf9SJames Bottomley 
6134238c9cf9SJames Bottomley 	/*
6135238c9cf9SJames Bottomley 	 * If we're not allowed to scan this host in parallel,
6136238c9cf9SJames Bottomley 	 * we need to wait until all previous scans have completed
6137238c9cf9SJames Bottomley 	 * before going further.
6138238c9cf9SJames Bottomley 	 * Jeff Garzik says this is only within a controller, so we
6139238c9cf9SJames Bottomley 	 * don't need to wait for port 0, only for later ports.
614079318057SArjan van de Ven 	 */
6141238c9cf9SJames Bottomley 	if (!(ap->host->flags & ATA_HOST_PARALLEL_SCAN) && ap->port_no != 0)
6142238c9cf9SJames Bottomley 		async_synchronize_cookie(cookie);
6143238c9cf9SJames Bottomley 
6144238c9cf9SJames Bottomley 	(void)ata_port_probe(ap);
6145f29d3b23SArjan van de Ven 
6146f29d3b23SArjan van de Ven 	/* in order to keep device order, we need to synchronize at this point */
6147f29d3b23SArjan van de Ven 	async_synchronize_cookie(cookie);
6148f29d3b23SArjan van de Ven 
6149f29d3b23SArjan van de Ven 	ata_scsi_scan_host(ap, 1);
615079318057SArjan van de Ven }
6151238c9cf9SJames Bottomley 
6152c6fd2807SJeff Garzik /**
6153f3187195STejun Heo  *	ata_host_register - register initialized ATA host
6154f3187195STejun Heo  *	@host: ATA host to register
6155f3187195STejun Heo  *	@sht: template for SCSI host
6156c6fd2807SJeff Garzik  *
6157f3187195STejun Heo  *	Register initialized ATA host.  @host is allocated using
6158f3187195STejun Heo  *	ata_host_alloc() and fully initialized by LLD.  This function
6159f3187195STejun Heo  *	starts ports, registers @host with ATA and SCSI layers and
6160f3187195STejun Heo  *	probe registered devices.
6161c6fd2807SJeff Garzik  *
6162c6fd2807SJeff Garzik  *	LOCKING:
6163f3187195STejun Heo  *	Inherited from calling layer (may sleep).
6164c6fd2807SJeff Garzik  *
6165c6fd2807SJeff Garzik  *	RETURNS:
6166f3187195STejun Heo  *	0 on success, -errno otherwise.
6167c6fd2807SJeff Garzik  */
6168f3187195STejun Heo int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
6169c6fd2807SJeff Garzik {
6170f3187195STejun Heo 	int i, rc;
6171c6fd2807SJeff Garzik 
6172f3187195STejun Heo 	/* host must have been started */
6173f3187195STejun Heo 	if (!(host->flags & ATA_HOST_STARTED)) {
6174a44fec1fSJoe Perches 		dev_err(host->dev, "BUG: trying to register unstarted host\n");
6175f3187195STejun Heo 		WARN_ON(1);
6176f3187195STejun Heo 		return -EINVAL;
617702f076aaSAlan Cox 	}
6178f0d36efdSTejun Heo 
6179f3187195STejun Heo 	/* Blow away unused ports.  This happens when LLD can't
6180f3187195STejun Heo 	 * determine the exact number of ports to allocate at
6181f3187195STejun Heo 	 * allocation time.
6182f3187195STejun Heo 	 */
6183f3187195STejun Heo 	for (i = host->n_ports; host->ports[i]; i++)
6184f3187195STejun Heo 		kfree(host->ports[i]);
6185f0d36efdSTejun Heo 
6186f3187195STejun Heo 	/* give ports names and add SCSI hosts */
6187e628dc99SDavid Milburn 	for (i = 0; i < host->n_ports; i++) {
618885d6725bSDan Williams 		host->ports[i]->print_id = atomic_inc_return(&ata_print_id);
6189e628dc99SDavid Milburn 		host->ports[i]->local_port_no = i + 1;
6190e628dc99SDavid Milburn 	}
6191d9027470SGwendal Grignou 
6192d9027470SGwendal Grignou 	/* Create associated sysfs transport objects  */
6193d9027470SGwendal Grignou 	for (i = 0; i < host->n_ports; i++) {
6194d9027470SGwendal Grignou 		rc = ata_tport_add(host->dev,host->ports[i]);
6195d9027470SGwendal Grignou 		if (rc) {
6196d9027470SGwendal Grignou 			goto err_tadd;
6197d9027470SGwendal Grignou 		}
6198d9027470SGwendal Grignou 	}
6199d9027470SGwendal Grignou 
6200f3187195STejun Heo 	rc = ata_scsi_add_hosts(host, sht);
6201ecef7253STejun Heo 	if (rc)
6202d9027470SGwendal Grignou 		goto err_tadd;
6203ecef7253STejun Heo 
6204f3187195STejun Heo 	/* set cable, sata_spd_limit and report */
6205cca3974eSJeff Garzik 	for (i = 0; i < host->n_ports; i++) {
6206cca3974eSJeff Garzik 		struct ata_port *ap = host->ports[i];
6207f3187195STejun Heo 		unsigned long xfer_mask;
6208f3187195STejun Heo 
6209f3187195STejun Heo 		/* set SATA cable type if still unset */
6210f3187195STejun Heo 		if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
6211f3187195STejun Heo 			ap->cbl = ATA_CBL_SATA;
6212c6fd2807SJeff Garzik 
6213c6fd2807SJeff Garzik 		/* init sata_spd_limit to the current value */
62144fb37a25STejun Heo 		sata_link_init_spd(&ap->link);
6215b1c72916STejun Heo 		if (ap->slave_link)
6216b1c72916STejun Heo 			sata_link_init_spd(ap->slave_link);
6217c6fd2807SJeff Garzik 
6218cbcdd875STejun Heo 		/* print per-port info to dmesg */
6219f3187195STejun Heo 		xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
6220f3187195STejun Heo 					      ap->udma_mask);
6221f3187195STejun Heo 
6222abf6e8edSTejun Heo 		if (!ata_port_is_dummy(ap)) {
6223a9a79dfeSJoe Perches 			ata_port_info(ap, "%cATA max %s %s\n",
6224a16abc0bSTejun Heo 				      (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
6225f3187195STejun Heo 				      ata_mode_string(xfer_mask),
6226cbcdd875STejun Heo 				      ap->link.eh_info.desc);
6227abf6e8edSTejun Heo 			ata_ehi_clear_desc(&ap->link.eh_info);
6228abf6e8edSTejun Heo 		} else
6229a9a79dfeSJoe Perches 			ata_port_info(ap, "DUMMY\n");
6230c6fd2807SJeff Garzik 	}
6231c6fd2807SJeff Garzik 
6232f6005354SVegard Nossum 	/* perform each probe asynchronously */
6233f3187195STejun Heo 	for (i = 0; i < host->n_ports; i++) {
6234f3187195STejun Heo 		struct ata_port *ap = host->ports[i];
623579318057SArjan van de Ven 		async_schedule(async_port_probe, ap);
6236c6fd2807SJeff Garzik 	}
6237c6fd2807SJeff Garzik 
6238f3187195STejun Heo 	return 0;
6239d9027470SGwendal Grignou 
6240d9027470SGwendal Grignou  err_tadd:
6241d9027470SGwendal Grignou 	while (--i >= 0) {
6242d9027470SGwendal Grignou 		ata_tport_delete(host->ports[i]);
6243d9027470SGwendal Grignou 	}
6244d9027470SGwendal Grignou 	return rc;
6245d9027470SGwendal Grignou 
6246f3187195STejun Heo }
6247f3187195STejun Heo 
6248f3187195STejun Heo /**
6249f5cda257STejun Heo  *	ata_host_activate - start host, request IRQ and register it
6250f5cda257STejun Heo  *	@host: target ATA host
6251f5cda257STejun Heo  *	@irq: IRQ to request
6252f5cda257STejun Heo  *	@irq_handler: irq_handler used when requesting IRQ
6253f5cda257STejun Heo  *	@irq_flags: irq_flags used when requesting IRQ
6254f5cda257STejun Heo  *	@sht: scsi_host_template to use when registering the host
6255f5cda257STejun Heo  *
6256f5cda257STejun Heo  *	After allocating an ATA host and initializing it, most libata
6257f5cda257STejun Heo  *	LLDs perform three steps to activate the host - start host,
6258f5cda257STejun Heo  *	request IRQ and register it.  This helper takes necessasry
6259f5cda257STejun Heo  *	arguments and performs the three steps in one go.
6260f5cda257STejun Heo  *
62613d46b2e2SPaul Mundt  *	An invalid IRQ skips the IRQ registration and expects the host to
62623d46b2e2SPaul Mundt  *	have set polling mode on the port. In this case, @irq_handler
62633d46b2e2SPaul Mundt  *	should be NULL.
62643d46b2e2SPaul Mundt  *
6265f5cda257STejun Heo  *	LOCKING:
6266f5cda257STejun Heo  *	Inherited from calling layer (may sleep).
6267f5cda257STejun Heo  *
6268f5cda257STejun Heo  *	RETURNS:
6269f5cda257STejun Heo  *	0 on success, -errno otherwise.
6270f5cda257STejun Heo  */
6271f5cda257STejun Heo int ata_host_activate(struct ata_host *host, int irq,
6272f5cda257STejun Heo 		      irq_handler_t irq_handler, unsigned long irq_flags,
6273f5cda257STejun Heo 		      struct scsi_host_template *sht)
6274f5cda257STejun Heo {
6275cbcdd875STejun Heo 	int i, rc;
6276f5cda257STejun Heo 
6277f5cda257STejun Heo 	rc = ata_host_start(host);
6278f5cda257STejun Heo 	if (rc)
6279f5cda257STejun Heo 		return rc;
6280f5cda257STejun Heo 
62813d46b2e2SPaul Mundt 	/* Special case for polling mode */
62823d46b2e2SPaul Mundt 	if (!irq) {
62833d46b2e2SPaul Mundt 		WARN_ON(irq_handler);
62843d46b2e2SPaul Mundt 		return ata_host_register(host, sht);
62853d46b2e2SPaul Mundt 	}
62863d46b2e2SPaul Mundt 
6287f5cda257STejun Heo 	rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
6288f5cda257STejun Heo 			      dev_driver_string(host->dev), host);
6289f5cda257STejun Heo 	if (rc)
6290f5cda257STejun Heo 		return rc;
6291f5cda257STejun Heo 
6292cbcdd875STejun Heo 	for (i = 0; i < host->n_ports; i++)
6293cbcdd875STejun Heo 		ata_port_desc(host->ports[i], "irq %d", irq);
62944031826bSTejun Heo 
6295f5cda257STejun Heo 	rc = ata_host_register(host, sht);
6296f5cda257STejun Heo 	/* if failed, just free the IRQ and leave ports alone */
6297f5cda257STejun Heo 	if (rc)
6298f5cda257STejun Heo 		devm_free_irq(host->dev, irq, host);
6299f5cda257STejun Heo 
6300f5cda257STejun Heo 	return rc;
6301f5cda257STejun Heo }
6302f5cda257STejun Heo 
6303f5cda257STejun Heo /**
6304c6fd2807SJeff Garzik  *	ata_port_detach - Detach ATA port in prepration of device removal
6305c6fd2807SJeff Garzik  *	@ap: ATA port to be detached
6306c6fd2807SJeff Garzik  *
6307c6fd2807SJeff Garzik  *	Detach all ATA devices and the associated SCSI devices of @ap;
6308c6fd2807SJeff Garzik  *	then, remove the associated SCSI host.  @ap is guaranteed to
6309c6fd2807SJeff Garzik  *	be quiescent on return from this function.
6310c6fd2807SJeff Garzik  *
6311c6fd2807SJeff Garzik  *	LOCKING:
6312c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
6313c6fd2807SJeff Garzik  */
6314741b7763SAdrian Bunk static void ata_port_detach(struct ata_port *ap)
6315c6fd2807SJeff Garzik {
6316c6fd2807SJeff Garzik 	unsigned long flags;
6317a6f9bf4dSLevente Kurusa 	struct ata_link *link;
6318a6f9bf4dSLevente Kurusa 	struct ata_device *dev;
6319c6fd2807SJeff Garzik 
6320c6fd2807SJeff Garzik 	if (!ap->ops->error_handler)
6321c6fd2807SJeff Garzik 		goto skip_eh;
6322c6fd2807SJeff Garzik 
6323c6fd2807SJeff Garzik 	/* tell EH we're leaving & flush EH */
6324c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
6325c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_UNLOADING;
6326ece180d1STejun Heo 	ata_port_schedule_eh(ap);
6327c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
6328c6fd2807SJeff Garzik 
6329ece180d1STejun Heo 	/* wait till EH commits suicide */
6330c6fd2807SJeff Garzik 	ata_port_wait_eh(ap);
6331c6fd2807SJeff Garzik 
6332ece180d1STejun Heo 	/* it better be dead now */
6333ece180d1STejun Heo 	WARN_ON(!(ap->pflags & ATA_PFLAG_UNLOADED));
6334c6fd2807SJeff Garzik 
6335afe2c511STejun Heo 	cancel_delayed_work_sync(&ap->hotplug_task);
6336c6fd2807SJeff Garzik 
6337c6fd2807SJeff Garzik  skip_eh:
6338a6f9bf4dSLevente Kurusa 	/* clean up zpodd on port removal */
6339a6f9bf4dSLevente Kurusa 	ata_for_each_link(link, ap, HOST_FIRST) {
6340a6f9bf4dSLevente Kurusa 		ata_for_each_dev(dev, link, ALL) {
6341a6f9bf4dSLevente Kurusa 			if (zpodd_dev_enabled(dev))
6342a6f9bf4dSLevente Kurusa 				zpodd_exit(dev);
6343a6f9bf4dSLevente Kurusa 		}
6344a6f9bf4dSLevente Kurusa 	}
6345d9027470SGwendal Grignou 	if (ap->pmp_link) {
6346d9027470SGwendal Grignou 		int i;
6347d9027470SGwendal Grignou 		for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
6348d9027470SGwendal Grignou 			ata_tlink_delete(&ap->pmp_link[i]);
6349d9027470SGwendal Grignou 	}
6350c6fd2807SJeff Garzik 	/* remove the associated SCSI host */
6351cca3974eSJeff Garzik 	scsi_remove_host(ap->scsi_host);
6352c5700766SRafael J. Wysocki 	ata_tport_delete(ap);
6353c6fd2807SJeff Garzik }
6354c6fd2807SJeff Garzik 
6355c6fd2807SJeff Garzik /**
63560529c159STejun Heo  *	ata_host_detach - Detach all ports of an ATA host
63570529c159STejun Heo  *	@host: Host to detach
63580529c159STejun Heo  *
63590529c159STejun Heo  *	Detach all ports of @host.
63600529c159STejun Heo  *
63610529c159STejun Heo  *	LOCKING:
63620529c159STejun Heo  *	Kernel thread context (may sleep).
63630529c159STejun Heo  */
63640529c159STejun Heo void ata_host_detach(struct ata_host *host)
63650529c159STejun Heo {
63660529c159STejun Heo 	int i;
63670529c159STejun Heo 
63680529c159STejun Heo 	for (i = 0; i < host->n_ports; i++)
63690529c159STejun Heo 		ata_port_detach(host->ports[i]);
6370562f0c2dSTejun Heo 
6371562f0c2dSTejun Heo 	/* the host is dead now, dissociate ACPI */
6372562f0c2dSTejun Heo 	ata_acpi_dissociate(host);
63730529c159STejun Heo }
63740529c159STejun Heo 
6375c6fd2807SJeff Garzik #ifdef CONFIG_PCI
6376c6fd2807SJeff Garzik 
6377c6fd2807SJeff Garzik /**
6378c6fd2807SJeff Garzik  *	ata_pci_remove_one - PCI layer callback for device removal
6379c6fd2807SJeff Garzik  *	@pdev: PCI device that was removed
6380c6fd2807SJeff Garzik  *
6381b878ca5dSTejun Heo  *	PCI layer indicates to libata via this hook that hot-unplug or
6382b878ca5dSTejun Heo  *	module unload event has occurred.  Detach all ports.  Resource
6383b878ca5dSTejun Heo  *	release is handled via devres.
6384c6fd2807SJeff Garzik  *
6385c6fd2807SJeff Garzik  *	LOCKING:
6386c6fd2807SJeff Garzik  *	Inherited from PCI layer (may sleep).
6387c6fd2807SJeff Garzik  */
6388c6fd2807SJeff Garzik void ata_pci_remove_one(struct pci_dev *pdev)
6389c6fd2807SJeff Garzik {
639004a3f5b7SBrian Norris 	struct ata_host *host = pci_get_drvdata(pdev);
6391c6fd2807SJeff Garzik 
6392f0d36efdSTejun Heo 	ata_host_detach(host);
6393c6fd2807SJeff Garzik }
6394c6fd2807SJeff Garzik 
6395c6fd2807SJeff Garzik /* move to PCI subsystem */
6396c6fd2807SJeff Garzik int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
6397c6fd2807SJeff Garzik {
6398c6fd2807SJeff Garzik 	unsigned long tmp = 0;
6399c6fd2807SJeff Garzik 
6400c6fd2807SJeff Garzik 	switch (bits->width) {
6401c6fd2807SJeff Garzik 	case 1: {
6402c6fd2807SJeff Garzik 		u8 tmp8 = 0;
6403c6fd2807SJeff Garzik 		pci_read_config_byte(pdev, bits->reg, &tmp8);
6404c6fd2807SJeff Garzik 		tmp = tmp8;
6405c6fd2807SJeff Garzik 		break;
6406c6fd2807SJeff Garzik 	}
6407c6fd2807SJeff Garzik 	case 2: {
6408c6fd2807SJeff Garzik 		u16 tmp16 = 0;
6409c6fd2807SJeff Garzik 		pci_read_config_word(pdev, bits->reg, &tmp16);
6410c6fd2807SJeff Garzik 		tmp = tmp16;
6411c6fd2807SJeff Garzik 		break;
6412c6fd2807SJeff Garzik 	}
6413c6fd2807SJeff Garzik 	case 4: {
6414c6fd2807SJeff Garzik 		u32 tmp32 = 0;
6415c6fd2807SJeff Garzik 		pci_read_config_dword(pdev, bits->reg, &tmp32);
6416c6fd2807SJeff Garzik 		tmp = tmp32;
6417c6fd2807SJeff Garzik 		break;
6418c6fd2807SJeff Garzik 	}
6419c6fd2807SJeff Garzik 
6420c6fd2807SJeff Garzik 	default:
6421c6fd2807SJeff Garzik 		return -EINVAL;
6422c6fd2807SJeff Garzik 	}
6423c6fd2807SJeff Garzik 
6424c6fd2807SJeff Garzik 	tmp &= bits->mask;
6425c6fd2807SJeff Garzik 
6426c6fd2807SJeff Garzik 	return (tmp == bits->val) ? 1 : 0;
6427c6fd2807SJeff Garzik }
6428c6fd2807SJeff Garzik 
64296ffa01d8STejun Heo #ifdef CONFIG_PM
6430c6fd2807SJeff Garzik void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
6431c6fd2807SJeff Garzik {
6432c6fd2807SJeff Garzik 	pci_save_state(pdev);
6433c6fd2807SJeff Garzik 	pci_disable_device(pdev);
64344c90d971STejun Heo 
64353a2d5b70SRafael J. Wysocki 	if (mesg.event & PM_EVENT_SLEEP)
6436c6fd2807SJeff Garzik 		pci_set_power_state(pdev, PCI_D3hot);
6437c6fd2807SJeff Garzik }
6438c6fd2807SJeff Garzik 
6439553c4aa6STejun Heo int ata_pci_device_do_resume(struct pci_dev *pdev)
6440c6fd2807SJeff Garzik {
6441553c4aa6STejun Heo 	int rc;
6442553c4aa6STejun Heo 
6443c6fd2807SJeff Garzik 	pci_set_power_state(pdev, PCI_D0);
6444c6fd2807SJeff Garzik 	pci_restore_state(pdev);
6445553c4aa6STejun Heo 
6446f0d36efdSTejun Heo 	rc = pcim_enable_device(pdev);
6447553c4aa6STejun Heo 	if (rc) {
6448a44fec1fSJoe Perches 		dev_err(&pdev->dev,
6449553c4aa6STejun Heo 			"failed to enable device after resume (%d)\n", rc);
6450553c4aa6STejun Heo 		return rc;
6451553c4aa6STejun Heo 	}
6452553c4aa6STejun Heo 
6453c6fd2807SJeff Garzik 	pci_set_master(pdev);
6454553c4aa6STejun Heo 	return 0;
6455c6fd2807SJeff Garzik }
6456c6fd2807SJeff Garzik 
6457c6fd2807SJeff Garzik int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
6458c6fd2807SJeff Garzik {
645904a3f5b7SBrian Norris 	struct ata_host *host = pci_get_drvdata(pdev);
6460c6fd2807SJeff Garzik 	int rc = 0;
6461c6fd2807SJeff Garzik 
6462cca3974eSJeff Garzik 	rc = ata_host_suspend(host, mesg);
6463c6fd2807SJeff Garzik 	if (rc)
6464c6fd2807SJeff Garzik 		return rc;
6465c6fd2807SJeff Garzik 
6466c6fd2807SJeff Garzik 	ata_pci_device_do_suspend(pdev, mesg);
6467c6fd2807SJeff Garzik 
6468c6fd2807SJeff Garzik 	return 0;
6469c6fd2807SJeff Garzik }
6470c6fd2807SJeff Garzik 
6471c6fd2807SJeff Garzik int ata_pci_device_resume(struct pci_dev *pdev)
6472c6fd2807SJeff Garzik {
647304a3f5b7SBrian Norris 	struct ata_host *host = pci_get_drvdata(pdev);
6474553c4aa6STejun Heo 	int rc;
6475c6fd2807SJeff Garzik 
6476553c4aa6STejun Heo 	rc = ata_pci_device_do_resume(pdev);
6477553c4aa6STejun Heo 	if (rc == 0)
6478cca3974eSJeff Garzik 		ata_host_resume(host);
6479553c4aa6STejun Heo 	return rc;
6480c6fd2807SJeff Garzik }
64816ffa01d8STejun Heo #endif /* CONFIG_PM */
64826ffa01d8STejun Heo 
6483c6fd2807SJeff Garzik #endif /* CONFIG_PCI */
6484c6fd2807SJeff Garzik 
6485b7db04d9SBrian Norris /**
6486b7db04d9SBrian Norris  *	ata_platform_remove_one - Platform layer callback for device removal
6487b7db04d9SBrian Norris  *	@pdev: Platform device that was removed
6488b7db04d9SBrian Norris  *
6489b7db04d9SBrian Norris  *	Platform layer indicates to libata via this hook that hot-unplug or
6490b7db04d9SBrian Norris  *	module unload event has occurred.  Detach all ports.  Resource
6491b7db04d9SBrian Norris  *	release is handled via devres.
6492b7db04d9SBrian Norris  *
6493b7db04d9SBrian Norris  *	LOCKING:
6494b7db04d9SBrian Norris  *	Inherited from platform layer (may sleep).
6495b7db04d9SBrian Norris  */
6496b7db04d9SBrian Norris int ata_platform_remove_one(struct platform_device *pdev)
6497b7db04d9SBrian Norris {
6498b7db04d9SBrian Norris 	struct ata_host *host = platform_get_drvdata(pdev);
6499b7db04d9SBrian Norris 
6500b7db04d9SBrian Norris 	ata_host_detach(host);
6501b7db04d9SBrian Norris 
6502b7db04d9SBrian Norris 	return 0;
6503b7db04d9SBrian Norris }
6504b7db04d9SBrian Norris 
650533267325STejun Heo static int __init ata_parse_force_one(char **cur,
650633267325STejun Heo 				      struct ata_force_ent *force_ent,
650733267325STejun Heo 				      const char **reason)
650833267325STejun Heo {
650933267325STejun Heo 	/* FIXME: Currently, there's no way to tag init const data and
651033267325STejun Heo 	 * using __initdata causes build failure on some versions of
651133267325STejun Heo 	 * gcc.  Once __initdataconst is implemented, add const to the
651233267325STejun Heo 	 * following structure.
651333267325STejun Heo 	 */
651433267325STejun Heo 	static struct ata_force_param force_tbl[] __initdata = {
651533267325STejun Heo 		{ "40c",	.cbl		= ATA_CBL_PATA40 },
651633267325STejun Heo 		{ "80c",	.cbl		= ATA_CBL_PATA80 },
651733267325STejun Heo 		{ "short40c",	.cbl		= ATA_CBL_PATA40_SHORT },
651833267325STejun Heo 		{ "unk",	.cbl		= ATA_CBL_PATA_UNK },
651933267325STejun Heo 		{ "ign",	.cbl		= ATA_CBL_PATA_IGN },
652033267325STejun Heo 		{ "sata",	.cbl		= ATA_CBL_SATA },
652133267325STejun Heo 		{ "1.5Gbps",	.spd_limit	= 1 },
652233267325STejun Heo 		{ "3.0Gbps",	.spd_limit	= 2 },
652333267325STejun Heo 		{ "noncq",	.horkage_on	= ATA_HORKAGE_NONCQ },
652433267325STejun Heo 		{ "ncq",	.horkage_off	= ATA_HORKAGE_NONCQ },
652543c9c591STejun Heo 		{ "dump_id",	.horkage_on	= ATA_HORKAGE_DUMP_ID },
652633267325STejun Heo 		{ "pio0",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 0) },
652733267325STejun Heo 		{ "pio1",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 1) },
652833267325STejun Heo 		{ "pio2",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 2) },
652933267325STejun Heo 		{ "pio3",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 3) },
653033267325STejun Heo 		{ "pio4",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 4) },
653133267325STejun Heo 		{ "pio5",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 5) },
653233267325STejun Heo 		{ "pio6",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 6) },
653333267325STejun Heo 		{ "mwdma0",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 0) },
653433267325STejun Heo 		{ "mwdma1",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 1) },
653533267325STejun Heo 		{ "mwdma2",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 2) },
653633267325STejun Heo 		{ "mwdma3",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 3) },
653733267325STejun Heo 		{ "mwdma4",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 4) },
653833267325STejun Heo 		{ "udma0",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
653933267325STejun Heo 		{ "udma16",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
654033267325STejun Heo 		{ "udma/16",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
654133267325STejun Heo 		{ "udma1",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
654233267325STejun Heo 		{ "udma25",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
654333267325STejun Heo 		{ "udma/25",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
654433267325STejun Heo 		{ "udma2",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
654533267325STejun Heo 		{ "udma33",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
654633267325STejun Heo 		{ "udma/33",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
654733267325STejun Heo 		{ "udma3",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
654833267325STejun Heo 		{ "udma44",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
654933267325STejun Heo 		{ "udma/44",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
655033267325STejun Heo 		{ "udma4",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
655133267325STejun Heo 		{ "udma66",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
655233267325STejun Heo 		{ "udma/66",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
655333267325STejun Heo 		{ "udma5",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
655433267325STejun Heo 		{ "udma100",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
655533267325STejun Heo 		{ "udma/100",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
655633267325STejun Heo 		{ "udma6",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
655733267325STejun Heo 		{ "udma133",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
655833267325STejun Heo 		{ "udma/133",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
655933267325STejun Heo 		{ "udma7",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 7) },
656005944bdfSTejun Heo 		{ "nohrst",	.lflags		= ATA_LFLAG_NO_HRST },
656105944bdfSTejun Heo 		{ "nosrst",	.lflags		= ATA_LFLAG_NO_SRST },
656205944bdfSTejun Heo 		{ "norst",	.lflags		= ATA_LFLAG_NO_HRST | ATA_LFLAG_NO_SRST },
6563ca6d43b0SDan Williams 		{ "rstonce",	.lflags		= ATA_LFLAG_RST_ONCE },
6564966fbe19SVincent Pelletier 		{ "atapi_dmadir", .horkage_on	= ATA_HORKAGE_ATAPI_DMADIR },
6565b8bd6dc3SRobin H. Johnson 		{ "disable",	.horkage_on	= ATA_HORKAGE_DISABLE },
656633267325STejun Heo 	};
656733267325STejun Heo 	char *start = *cur, *p = *cur;
656833267325STejun Heo 	char *id, *val, *endp;
656933267325STejun Heo 	const struct ata_force_param *match_fp = NULL;
657033267325STejun Heo 	int nr_matches = 0, i;
657133267325STejun Heo 
657233267325STejun Heo 	/* find where this param ends and update *cur */
657333267325STejun Heo 	while (*p != '\0' && *p != ',')
657433267325STejun Heo 		p++;
657533267325STejun Heo 
657633267325STejun Heo 	if (*p == '\0')
657733267325STejun Heo 		*cur = p;
657833267325STejun Heo 	else
657933267325STejun Heo 		*cur = p + 1;
658033267325STejun Heo 
658133267325STejun Heo 	*p = '\0';
658233267325STejun Heo 
658333267325STejun Heo 	/* parse */
658433267325STejun Heo 	p = strchr(start, ':');
658533267325STejun Heo 	if (!p) {
658633267325STejun Heo 		val = strstrip(start);
658733267325STejun Heo 		goto parse_val;
658833267325STejun Heo 	}
658933267325STejun Heo 	*p = '\0';
659033267325STejun Heo 
659133267325STejun Heo 	id = strstrip(start);
659233267325STejun Heo 	val = strstrip(p + 1);
659333267325STejun Heo 
659433267325STejun Heo 	/* parse id */
659533267325STejun Heo 	p = strchr(id, '.');
659633267325STejun Heo 	if (p) {
659733267325STejun Heo 		*p++ = '\0';
659833267325STejun Heo 		force_ent->device = simple_strtoul(p, &endp, 10);
659933267325STejun Heo 		if (p == endp || *endp != '\0') {
660033267325STejun Heo 			*reason = "invalid device";
660133267325STejun Heo 			return -EINVAL;
660233267325STejun Heo 		}
660333267325STejun Heo 	}
660433267325STejun Heo 
660533267325STejun Heo 	force_ent->port = simple_strtoul(id, &endp, 10);
660633267325STejun Heo 	if (p == endp || *endp != '\0') {
660733267325STejun Heo 		*reason = "invalid port/link";
660833267325STejun Heo 		return -EINVAL;
660933267325STejun Heo 	}
661033267325STejun Heo 
661133267325STejun Heo  parse_val:
661233267325STejun Heo 	/* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */
661333267325STejun Heo 	for (i = 0; i < ARRAY_SIZE(force_tbl); i++) {
661433267325STejun Heo 		const struct ata_force_param *fp = &force_tbl[i];
661533267325STejun Heo 
661633267325STejun Heo 		if (strncasecmp(val, fp->name, strlen(val)))
661733267325STejun Heo 			continue;
661833267325STejun Heo 
661933267325STejun Heo 		nr_matches++;
662033267325STejun Heo 		match_fp = fp;
662133267325STejun Heo 
662233267325STejun Heo 		if (strcasecmp(val, fp->name) == 0) {
662333267325STejun Heo 			nr_matches = 1;
662433267325STejun Heo 			break;
662533267325STejun Heo 		}
662633267325STejun Heo 	}
662733267325STejun Heo 
662833267325STejun Heo 	if (!nr_matches) {
662933267325STejun Heo 		*reason = "unknown value";
663033267325STejun Heo 		return -EINVAL;
663133267325STejun Heo 	}
663233267325STejun Heo 	if (nr_matches > 1) {
663333267325STejun Heo 		*reason = "ambigious value";
663433267325STejun Heo 		return -EINVAL;
663533267325STejun Heo 	}
663633267325STejun Heo 
663733267325STejun Heo 	force_ent->param = *match_fp;
663833267325STejun Heo 
663933267325STejun Heo 	return 0;
664033267325STejun Heo }
664133267325STejun Heo 
664233267325STejun Heo static void __init ata_parse_force_param(void)
664333267325STejun Heo {
664433267325STejun Heo 	int idx = 0, size = 1;
664533267325STejun Heo 	int last_port = -1, last_device = -1;
664633267325STejun Heo 	char *p, *cur, *next;
664733267325STejun Heo 
664833267325STejun Heo 	/* calculate maximum number of params and allocate force_tbl */
664933267325STejun Heo 	for (p = ata_force_param_buf; *p; p++)
665033267325STejun Heo 		if (*p == ',')
665133267325STejun Heo 			size++;
665233267325STejun Heo 
665333267325STejun Heo 	ata_force_tbl = kzalloc(sizeof(ata_force_tbl[0]) * size, GFP_KERNEL);
665433267325STejun Heo 	if (!ata_force_tbl) {
665533267325STejun Heo 		printk(KERN_WARNING "ata: failed to extend force table, "
665633267325STejun Heo 		       "libata.force ignored\n");
665733267325STejun Heo 		return;
665833267325STejun Heo 	}
665933267325STejun Heo 
666033267325STejun Heo 	/* parse and populate the table */
666133267325STejun Heo 	for (cur = ata_force_param_buf; *cur != '\0'; cur = next) {
666233267325STejun Heo 		const char *reason = "";
666333267325STejun Heo 		struct ata_force_ent te = { .port = -1, .device = -1 };
666433267325STejun Heo 
666533267325STejun Heo 		next = cur;
666633267325STejun Heo 		if (ata_parse_force_one(&next, &te, &reason)) {
666733267325STejun Heo 			printk(KERN_WARNING "ata: failed to parse force "
666833267325STejun Heo 			       "parameter \"%s\" (%s)\n",
666933267325STejun Heo 			       cur, reason);
667033267325STejun Heo 			continue;
667133267325STejun Heo 		}
667233267325STejun Heo 
667333267325STejun Heo 		if (te.port == -1) {
667433267325STejun Heo 			te.port = last_port;
667533267325STejun Heo 			te.device = last_device;
667633267325STejun Heo 		}
667733267325STejun Heo 
667833267325STejun Heo 		ata_force_tbl[idx++] = te;
667933267325STejun Heo 
668033267325STejun Heo 		last_port = te.port;
668133267325STejun Heo 		last_device = te.device;
668233267325STejun Heo 	}
668333267325STejun Heo 
668433267325STejun Heo 	ata_force_tbl_size = idx;
668533267325STejun Heo }
6686c6fd2807SJeff Garzik 
6687c6fd2807SJeff Garzik static int __init ata_init(void)
6688c6fd2807SJeff Garzik {
6689d9027470SGwendal Grignou 	int rc;
6690270390e1STejun Heo 
669133267325STejun Heo 	ata_parse_force_param();
669233267325STejun Heo 
6693270390e1STejun Heo 	rc = ata_sff_init();
6694ad72cf98STejun Heo 	if (rc) {
6695ad72cf98STejun Heo 		kfree(ata_force_tbl);
6696ad72cf98STejun Heo 		return rc;
6697ad72cf98STejun Heo 	}
6698c6fd2807SJeff Garzik 
6699d9027470SGwendal Grignou 	libata_transport_init();
6700d9027470SGwendal Grignou 	ata_scsi_transport_template = ata_attach_transport();
6701d9027470SGwendal Grignou 	if (!ata_scsi_transport_template) {
6702d9027470SGwendal Grignou 		ata_sff_exit();
6703d9027470SGwendal Grignou 		rc = -ENOMEM;
6704d9027470SGwendal Grignou 		goto err_out;
6705d9027470SGwendal Grignou 	}
6706d9027470SGwendal Grignou 
6707c6fd2807SJeff Garzik 	printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
6708c6fd2807SJeff Garzik 	return 0;
6709d9027470SGwendal Grignou 
6710d9027470SGwendal Grignou err_out:
6711d9027470SGwendal Grignou 	return rc;
6712c6fd2807SJeff Garzik }
6713c6fd2807SJeff Garzik 
6714c6fd2807SJeff Garzik static void __exit ata_exit(void)
6715c6fd2807SJeff Garzik {
6716d9027470SGwendal Grignou 	ata_release_transport(ata_scsi_transport_template);
6717d9027470SGwendal Grignou 	libata_transport_exit();
6718270390e1STejun Heo 	ata_sff_exit();
671933267325STejun Heo 	kfree(ata_force_tbl);
6720c6fd2807SJeff Garzik }
6721c6fd2807SJeff Garzik 
6722a4625085SBrian King subsys_initcall(ata_init);
6723c6fd2807SJeff Garzik module_exit(ata_exit);
6724c6fd2807SJeff Garzik 
67259990b6f3SAkinobu Mita static DEFINE_RATELIMIT_STATE(ratelimit, HZ / 5, 1);
6726c6fd2807SJeff Garzik 
6727c6fd2807SJeff Garzik int ata_ratelimit(void)
6728c6fd2807SJeff Garzik {
67299990b6f3SAkinobu Mita 	return __ratelimit(&ratelimit);
6730c6fd2807SJeff Garzik }
6731c6fd2807SJeff Garzik 
6732c0c362b6STejun Heo /**
6733c0c362b6STejun Heo  *	ata_msleep - ATA EH owner aware msleep
6734c0c362b6STejun Heo  *	@ap: ATA port to attribute the sleep to
6735c0c362b6STejun Heo  *	@msecs: duration to sleep in milliseconds
6736c0c362b6STejun Heo  *
6737c0c362b6STejun Heo  *	Sleeps @msecs.  If the current task is owner of @ap's EH, the
6738c0c362b6STejun Heo  *	ownership is released before going to sleep and reacquired
6739c0c362b6STejun Heo  *	after the sleep is complete.  IOW, other ports sharing the
6740c0c362b6STejun Heo  *	@ap->host will be allowed to own the EH while this task is
6741c0c362b6STejun Heo  *	sleeping.
6742c0c362b6STejun Heo  *
6743c0c362b6STejun Heo  *	LOCKING:
6744c0c362b6STejun Heo  *	Might sleep.
6745c0c362b6STejun Heo  */
674697750cebSTejun Heo void ata_msleep(struct ata_port *ap, unsigned int msecs)
674797750cebSTejun Heo {
6748c0c362b6STejun Heo 	bool owns_eh = ap && ap->host->eh_owner == current;
6749c0c362b6STejun Heo 
6750c0c362b6STejun Heo 	if (owns_eh)
6751c0c362b6STejun Heo 		ata_eh_release(ap);
6752c0c362b6STejun Heo 
675397750cebSTejun Heo 	msleep(msecs);
6754c0c362b6STejun Heo 
6755c0c362b6STejun Heo 	if (owns_eh)
6756c0c362b6STejun Heo 		ata_eh_acquire(ap);
675797750cebSTejun Heo }
675897750cebSTejun Heo 
6759c6fd2807SJeff Garzik /**
6760c6fd2807SJeff Garzik  *	ata_wait_register - wait until register value changes
676197750cebSTejun Heo  *	@ap: ATA port to wait register for, can be NULL
6762c6fd2807SJeff Garzik  *	@reg: IO-mapped register
6763c6fd2807SJeff Garzik  *	@mask: Mask to apply to read register value
6764c6fd2807SJeff Garzik  *	@val: Wait condition
6765341c2c95STejun Heo  *	@interval: polling interval in milliseconds
6766341c2c95STejun Heo  *	@timeout: timeout in milliseconds
6767c6fd2807SJeff Garzik  *
6768c6fd2807SJeff Garzik  *	Waiting for some bits of register to change is a common
6769c6fd2807SJeff Garzik  *	operation for ATA controllers.  This function reads 32bit LE
6770c6fd2807SJeff Garzik  *	IO-mapped register @reg and tests for the following condition.
6771c6fd2807SJeff Garzik  *
6772c6fd2807SJeff Garzik  *	(*@reg & mask) != val
6773c6fd2807SJeff Garzik  *
6774c6fd2807SJeff Garzik  *	If the condition is met, it returns; otherwise, the process is
6775c6fd2807SJeff Garzik  *	repeated after @interval_msec until timeout.
6776c6fd2807SJeff Garzik  *
6777c6fd2807SJeff Garzik  *	LOCKING:
6778c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
6779c6fd2807SJeff Garzik  *
6780c6fd2807SJeff Garzik  *	RETURNS:
6781c6fd2807SJeff Garzik  *	The final register value.
6782c6fd2807SJeff Garzik  */
678397750cebSTejun Heo u32 ata_wait_register(struct ata_port *ap, void __iomem *reg, u32 mask, u32 val,
6784341c2c95STejun Heo 		      unsigned long interval, unsigned long timeout)
6785c6fd2807SJeff Garzik {
6786341c2c95STejun Heo 	unsigned long deadline;
6787c6fd2807SJeff Garzik 	u32 tmp;
6788c6fd2807SJeff Garzik 
6789c6fd2807SJeff Garzik 	tmp = ioread32(reg);
6790c6fd2807SJeff Garzik 
6791c6fd2807SJeff Garzik 	/* Calculate timeout _after_ the first read to make sure
6792c6fd2807SJeff Garzik 	 * preceding writes reach the controller before starting to
6793c6fd2807SJeff Garzik 	 * eat away the timeout.
6794c6fd2807SJeff Garzik 	 */
6795341c2c95STejun Heo 	deadline = ata_deadline(jiffies, timeout);
6796c6fd2807SJeff Garzik 
6797341c2c95STejun Heo 	while ((tmp & mask) == val && time_before(jiffies, deadline)) {
679897750cebSTejun Heo 		ata_msleep(ap, interval);
6799c6fd2807SJeff Garzik 		tmp = ioread32(reg);
6800c6fd2807SJeff Garzik 	}
6801c6fd2807SJeff Garzik 
6802c6fd2807SJeff Garzik 	return tmp;
6803c6fd2807SJeff Garzik }
6804c6fd2807SJeff Garzik 
6805c6fd2807SJeff Garzik /*
6806c6fd2807SJeff Garzik  * Dummy port_ops
6807c6fd2807SJeff Garzik  */
6808c6fd2807SJeff Garzik static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
6809c6fd2807SJeff Garzik {
6810c6fd2807SJeff Garzik 	return AC_ERR_SYSTEM;
6811c6fd2807SJeff Garzik }
6812c6fd2807SJeff Garzik 
6813182d7bbaSTejun Heo static void ata_dummy_error_handler(struct ata_port *ap)
6814182d7bbaSTejun Heo {
6815182d7bbaSTejun Heo 	/* truly dummy */
6816182d7bbaSTejun Heo }
6817182d7bbaSTejun Heo 
6818029cfd6bSTejun Heo struct ata_port_operations ata_dummy_port_ops = {
6819c6fd2807SJeff Garzik 	.qc_prep		= ata_noop_qc_prep,
6820c6fd2807SJeff Garzik 	.qc_issue		= ata_dummy_qc_issue,
6821182d7bbaSTejun Heo 	.error_handler		= ata_dummy_error_handler,
6822e4a9c373SDan Williams 	.sched_eh		= ata_std_sched_eh,
6823e4a9c373SDan Williams 	.end_eh			= ata_std_end_eh,
6824c6fd2807SJeff Garzik };
6825c6fd2807SJeff Garzik 
682621b0ad4fSTejun Heo const struct ata_port_info ata_dummy_port_info = {
682721b0ad4fSTejun Heo 	.port_ops		= &ata_dummy_port_ops,
682821b0ad4fSTejun Heo };
682921b0ad4fSTejun Heo 
6830c6fd2807SJeff Garzik /*
6831a9a79dfeSJoe Perches  * Utility print functions
6832a9a79dfeSJoe Perches  */
6833a9a79dfeSJoe Perches int ata_port_printk(const struct ata_port *ap, const char *level,
6834a9a79dfeSJoe Perches 		    const char *fmt, ...)
6835a9a79dfeSJoe Perches {
6836a9a79dfeSJoe Perches 	struct va_format vaf;
6837a9a79dfeSJoe Perches 	va_list args;
6838a9a79dfeSJoe Perches 	int r;
6839a9a79dfeSJoe Perches 
6840a9a79dfeSJoe Perches 	va_start(args, fmt);
6841a9a79dfeSJoe Perches 
6842a9a79dfeSJoe Perches 	vaf.fmt = fmt;
6843a9a79dfeSJoe Perches 	vaf.va = &args;
6844a9a79dfeSJoe Perches 
6845a9a79dfeSJoe Perches 	r = printk("%sata%u: %pV", level, ap->print_id, &vaf);
6846a9a79dfeSJoe Perches 
6847a9a79dfeSJoe Perches 	va_end(args);
6848a9a79dfeSJoe Perches 
6849a9a79dfeSJoe Perches 	return r;
6850a9a79dfeSJoe Perches }
6851a9a79dfeSJoe Perches EXPORT_SYMBOL(ata_port_printk);
6852a9a79dfeSJoe Perches 
6853a9a79dfeSJoe Perches int ata_link_printk(const struct ata_link *link, const char *level,
6854a9a79dfeSJoe Perches 		    const char *fmt, ...)
6855a9a79dfeSJoe Perches {
6856a9a79dfeSJoe Perches 	struct va_format vaf;
6857a9a79dfeSJoe Perches 	va_list args;
6858a9a79dfeSJoe Perches 	int r;
6859a9a79dfeSJoe Perches 
6860a9a79dfeSJoe Perches 	va_start(args, fmt);
6861a9a79dfeSJoe Perches 
6862a9a79dfeSJoe Perches 	vaf.fmt = fmt;
6863a9a79dfeSJoe Perches 	vaf.va = &args;
6864a9a79dfeSJoe Perches 
6865a9a79dfeSJoe Perches 	if (sata_pmp_attached(link->ap) || link->ap->slave_link)
6866a9a79dfeSJoe Perches 		r = printk("%sata%u.%02u: %pV",
6867a9a79dfeSJoe Perches 			   level, link->ap->print_id, link->pmp, &vaf);
6868a9a79dfeSJoe Perches 	else
6869a9a79dfeSJoe Perches 		r = printk("%sata%u: %pV",
6870a9a79dfeSJoe Perches 			   level, link->ap->print_id, &vaf);
6871a9a79dfeSJoe Perches 
6872a9a79dfeSJoe Perches 	va_end(args);
6873a9a79dfeSJoe Perches 
6874a9a79dfeSJoe Perches 	return r;
6875a9a79dfeSJoe Perches }
6876a9a79dfeSJoe Perches EXPORT_SYMBOL(ata_link_printk);
6877a9a79dfeSJoe Perches 
6878a9a79dfeSJoe Perches int ata_dev_printk(const struct ata_device *dev, const char *level,
6879a9a79dfeSJoe Perches 		    const char *fmt, ...)
6880a9a79dfeSJoe Perches {
6881a9a79dfeSJoe Perches 	struct va_format vaf;
6882a9a79dfeSJoe Perches 	va_list args;
6883a9a79dfeSJoe Perches 	int r;
6884a9a79dfeSJoe Perches 
6885a9a79dfeSJoe Perches 	va_start(args, fmt);
6886a9a79dfeSJoe Perches 
6887a9a79dfeSJoe Perches 	vaf.fmt = fmt;
6888a9a79dfeSJoe Perches 	vaf.va = &args;
6889a9a79dfeSJoe Perches 
6890a9a79dfeSJoe Perches 	r = printk("%sata%u.%02u: %pV",
6891a9a79dfeSJoe Perches 		   level, dev->link->ap->print_id, dev->link->pmp + dev->devno,
6892a9a79dfeSJoe Perches 		   &vaf);
6893a9a79dfeSJoe Perches 
6894a9a79dfeSJoe Perches 	va_end(args);
6895a9a79dfeSJoe Perches 
6896a9a79dfeSJoe Perches 	return r;
6897a9a79dfeSJoe Perches }
6898a9a79dfeSJoe Perches EXPORT_SYMBOL(ata_dev_printk);
6899a9a79dfeSJoe Perches 
690006296a1eSJoe Perches void ata_print_version(const struct device *dev, const char *version)
690106296a1eSJoe Perches {
690206296a1eSJoe Perches 	dev_printk(KERN_DEBUG, dev, "version %s\n", version);
690306296a1eSJoe Perches }
690406296a1eSJoe Perches EXPORT_SYMBOL(ata_print_version);
690506296a1eSJoe Perches 
6906a9a79dfeSJoe Perches /*
6907c6fd2807SJeff Garzik  * libata is essentially a library of internal helper functions for
6908c6fd2807SJeff Garzik  * low-level ATA host controller drivers.  As such, the API/ABI is
6909c6fd2807SJeff Garzik  * likely to change as new drivers are added and updated.
6910c6fd2807SJeff Garzik  * Do not depend on ABI/API stability.
6911c6fd2807SJeff Garzik  */
6912c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
6913c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
6914c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_deb_timing_long);
6915029cfd6bSTejun Heo EXPORT_SYMBOL_GPL(ata_base_port_ops);
6916029cfd6bSTejun Heo EXPORT_SYMBOL_GPL(sata_port_ops);
6917c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
691821b0ad4fSTejun Heo EXPORT_SYMBOL_GPL(ata_dummy_port_info);
69191eca4365STejun Heo EXPORT_SYMBOL_GPL(ata_link_next);
69201eca4365STejun Heo EXPORT_SYMBOL_GPL(ata_dev_next);
6921c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_std_bios_param);
6922d8d9129eSTejun Heo EXPORT_SYMBOL_GPL(ata_scsi_unlock_native_capacity);
6923cca3974eSJeff Garzik EXPORT_SYMBOL_GPL(ata_host_init);
6924f3187195STejun Heo EXPORT_SYMBOL_GPL(ata_host_alloc);
6925f5cda257STejun Heo EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
6926b1c72916STejun Heo EXPORT_SYMBOL_GPL(ata_slave_link_init);
6927ecef7253STejun Heo EXPORT_SYMBOL_GPL(ata_host_start);
6928f3187195STejun Heo EXPORT_SYMBOL_GPL(ata_host_register);
6929f5cda257STejun Heo EXPORT_SYMBOL_GPL(ata_host_activate);
69300529c159STejun Heo EXPORT_SYMBOL_GPL(ata_host_detach);
6931c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_sg_init);
6932c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_qc_complete);
6933c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
6934436d34b3STejun Heo EXPORT_SYMBOL_GPL(atapi_cmd_type);
6935c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_tf_to_fis);
6936c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_tf_from_fis);
69376357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_pack_xfermask);
69386357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_unpack_xfermask);
69396357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
69406357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
69416357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
69426357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_mode_string);
69436357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_id_xfermask);
694404351821SAlan EXPORT_SYMBOL_GPL(ata_do_set_mode);
694531cc23b3STejun Heo EXPORT_SYMBOL_GPL(ata_std_qc_defer);
6946c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
694710305f0fSAlan EXPORT_SYMBOL_GPL(ata_dev_disable);
6948c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_set_spd);
6949aa2731adSTejun Heo EXPORT_SYMBOL_GPL(ata_wait_after_reset);
6950936fd732STejun Heo EXPORT_SYMBOL_GPL(sata_link_debounce);
6951936fd732STejun Heo EXPORT_SYMBOL_GPL(sata_link_resume);
69521152b261STejun Heo EXPORT_SYMBOL_GPL(sata_link_scr_lpm);
69530aa1113dSTejun Heo EXPORT_SYMBOL_GPL(ata_std_prereset);
6954cc0680a5STejun Heo EXPORT_SYMBOL_GPL(sata_link_hardreset);
695557c9efdfSTejun Heo EXPORT_SYMBOL_GPL(sata_std_hardreset);
6956203c75b8STejun Heo EXPORT_SYMBOL_GPL(ata_std_postreset);
6957c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_dev_classify);
6958c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_dev_pair);
6959c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_ratelimit);
696097750cebSTejun Heo EXPORT_SYMBOL_GPL(ata_msleep);
6961c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_wait_register);
6962c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
6963c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
6964c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
6965c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
6966f6e67035SDan Williams EXPORT_SYMBOL_GPL(__ata_change_queue_depth);
6967c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_valid);
6968c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_read);
6969c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_write);
6970c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_write_flush);
6971936fd732STejun Heo EXPORT_SYMBOL_GPL(ata_link_online);
6972936fd732STejun Heo EXPORT_SYMBOL_GPL(ata_link_offline);
69736ffa01d8STejun Heo #ifdef CONFIG_PM
6974cca3974eSJeff Garzik EXPORT_SYMBOL_GPL(ata_host_suspend);
6975cca3974eSJeff Garzik EXPORT_SYMBOL_GPL(ata_host_resume);
69766ffa01d8STejun Heo #endif /* CONFIG_PM */
6977c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_id_string);
6978c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_id_c_string);
6979963e4975SAlan Cox EXPORT_SYMBOL_GPL(ata_do_dev_read_id);
6980c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_simulate);
6981c6fd2807SJeff Garzik 
6982c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
69836357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_timing_find_mode);
6984c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_timing_compute);
6985c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_timing_merge);
6986a0f79b92STejun Heo EXPORT_SYMBOL_GPL(ata_timing_cycle2mode);
6987c6fd2807SJeff Garzik 
6988c6fd2807SJeff Garzik #ifdef CONFIG_PCI
6989c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(pci_test_config_bits);
6990c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_remove_one);
69916ffa01d8STejun Heo #ifdef CONFIG_PM
6992c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
6993c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
6994c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
6995c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_resume);
69966ffa01d8STejun Heo #endif /* CONFIG_PM */
6997c6fd2807SJeff Garzik #endif /* CONFIG_PCI */
6998c6fd2807SJeff Garzik 
6999b7db04d9SBrian Norris EXPORT_SYMBOL_GPL(ata_platform_remove_one);
7000b7db04d9SBrian Norris 
7001b64bbc39STejun Heo EXPORT_SYMBOL_GPL(__ata_ehi_push_desc);
7002b64bbc39STejun Heo EXPORT_SYMBOL_GPL(ata_ehi_push_desc);
7003b64bbc39STejun Heo EXPORT_SYMBOL_GPL(ata_ehi_clear_desc);
7004cbcdd875STejun Heo EXPORT_SYMBOL_GPL(ata_port_desc);
7005cbcdd875STejun Heo #ifdef CONFIG_PCI
7006cbcdd875STejun Heo EXPORT_SYMBOL_GPL(ata_port_pbar_desc);
7007cbcdd875STejun Heo #endif /* CONFIG_PCI */
7008c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
7009dbd82616STejun Heo EXPORT_SYMBOL_GPL(ata_link_abort);
7010c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_abort);
7011c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_freeze);
70127d77b247STejun Heo EXPORT_SYMBOL_GPL(sata_async_notification);
7013c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
7014c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
7015c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
7016c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
701710acf3b0SMark Lord EXPORT_SYMBOL_GPL(ata_eh_analyze_ncq_error);
7018c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_do_eh);
7019a1efdabaSTejun Heo EXPORT_SYMBOL_GPL(ata_std_error_handler);
7020be0d18dfSAlan Cox 
7021be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_40wire);
7022be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_80wire);
7023be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_unknown);
7024c88f90c3STejun Heo EXPORT_SYMBOL_GPL(ata_cable_ignore);
7025be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_sata);
7026