xref: /openbmc/linux/drivers/ata/libata-core.c (revision cd705d5a)
1c6fd2807SJeff Garzik /*
2c6fd2807SJeff Garzik  *  libata-core.c - helper library for ATA
3c6fd2807SJeff Garzik  *
4c6fd2807SJeff Garzik  *  Maintained by:  Jeff Garzik <jgarzik@pobox.com>
5c6fd2807SJeff Garzik  *    		    Please ALWAYS copy linux-ide@vger.kernel.org
6c6fd2807SJeff Garzik  *		    on emails.
7c6fd2807SJeff Garzik  *
8c6fd2807SJeff Garzik  *  Copyright 2003-2004 Red Hat, Inc.  All rights reserved.
9c6fd2807SJeff Garzik  *  Copyright 2003-2004 Jeff Garzik
10c6fd2807SJeff Garzik  *
11c6fd2807SJeff Garzik  *
12c6fd2807SJeff Garzik  *  This program is free software; you can redistribute it and/or modify
13c6fd2807SJeff Garzik  *  it under the terms of the GNU General Public License as published by
14c6fd2807SJeff Garzik  *  the Free Software Foundation; either version 2, or (at your option)
15c6fd2807SJeff Garzik  *  any later version.
16c6fd2807SJeff Garzik  *
17c6fd2807SJeff Garzik  *  This program is distributed in the hope that it will be useful,
18c6fd2807SJeff Garzik  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19c6fd2807SJeff Garzik  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20c6fd2807SJeff Garzik  *  GNU General Public License for more details.
21c6fd2807SJeff Garzik  *
22c6fd2807SJeff Garzik  *  You should have received a copy of the GNU General Public License
23c6fd2807SJeff Garzik  *  along with this program; see the file COPYING.  If not, write to
24c6fd2807SJeff Garzik  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25c6fd2807SJeff Garzik  *
26c6fd2807SJeff Garzik  *
27c6fd2807SJeff Garzik  *  libata documentation is available via 'make {ps|pdf}docs',
28c6fd2807SJeff Garzik  *  as Documentation/DocBook/libata.*
29c6fd2807SJeff Garzik  *
30c6fd2807SJeff Garzik  *  Hardware documentation available from http://www.t13.org/ and
31c6fd2807SJeff Garzik  *  http://www.sata-io.org/
32c6fd2807SJeff Garzik  *
3392c52c52SAlan Cox  *  Standards documents from:
3492c52c52SAlan Cox  *	http://www.t13.org (ATA standards, PCI DMA IDE spec)
3592c52c52SAlan Cox  *	http://www.t10.org (SCSI MMC - for ATAPI MMC)
3692c52c52SAlan Cox  *	http://www.sata-io.org (SATA)
3792c52c52SAlan Cox  *	http://www.compactflash.org (CF)
3892c52c52SAlan Cox  *	http://www.qic.org (QIC157 - Tape and DSC)
3992c52c52SAlan Cox  *	http://www.ce-ata.org (CE-ATA: not supported)
4092c52c52SAlan Cox  *
41c6fd2807SJeff Garzik  */
42c6fd2807SJeff Garzik 
43c6fd2807SJeff Garzik #include <linux/kernel.h>
44c6fd2807SJeff Garzik #include <linux/module.h>
45c6fd2807SJeff Garzik #include <linux/pci.h>
46c6fd2807SJeff Garzik #include <linux/init.h>
47c6fd2807SJeff Garzik #include <linux/list.h>
48c6fd2807SJeff Garzik #include <linux/mm.h>
49c6fd2807SJeff Garzik #include <linux/spinlock.h>
50c6fd2807SJeff Garzik #include <linux/blkdev.h>
51c6fd2807SJeff Garzik #include <linux/delay.h>
52c6fd2807SJeff Garzik #include <linux/timer.h>
53c6fd2807SJeff Garzik #include <linux/interrupt.h>
54c6fd2807SJeff Garzik #include <linux/completion.h>
55c6fd2807SJeff Garzik #include <linux/suspend.h>
56c6fd2807SJeff Garzik #include <linux/workqueue.h>
57c6fd2807SJeff Garzik #include <linux/scatterlist.h>
582dcb407eSJeff Garzik #include <linux/io.h>
5979318057SArjan van de Ven #include <linux/async.h>
60e18086d6SMark Lord #include <linux/log2.h>
615a0e3ad6STejun Heo #include <linux/slab.h>
62c6fd2807SJeff Garzik #include <scsi/scsi.h>
63c6fd2807SJeff Garzik #include <scsi/scsi_cmnd.h>
64c6fd2807SJeff Garzik #include <scsi/scsi_host.h>
65c6fd2807SJeff Garzik #include <linux/libata.h>
66c6fd2807SJeff Garzik #include <asm/byteorder.h>
67140b5e59STejun Heo #include <linux/cdrom.h>
689990b6f3SAkinobu Mita #include <linux/ratelimit.h>
699ee4f393SLin Ming #include <linux/pm_runtime.h>
70c6fd2807SJeff Garzik 
71c6fd2807SJeff Garzik #include "libata.h"
72d9027470SGwendal Grignou #include "libata-transport.h"
73fda0efc5SJeff Garzik 
74c6fd2807SJeff Garzik /* debounce timing parameters in msecs { interval, duration, timeout } */
75c6fd2807SJeff Garzik const unsigned long sata_deb_timing_normal[]		= {   5,  100, 2000 };
76c6fd2807SJeff Garzik const unsigned long sata_deb_timing_hotplug[]		= {  25,  500, 2000 };
77c6fd2807SJeff Garzik const unsigned long sata_deb_timing_long[]		= { 100, 2000, 5000 };
78c6fd2807SJeff Garzik 
79029cfd6bSTejun Heo const struct ata_port_operations ata_base_port_ops = {
800aa1113dSTejun Heo 	.prereset		= ata_std_prereset,
81203c75b8STejun Heo 	.postreset		= ata_std_postreset,
82a1efdabaSTejun Heo 	.error_handler		= ata_std_error_handler,
83e4a9c373SDan Williams 	.sched_eh		= ata_std_sched_eh,
84e4a9c373SDan Williams 	.end_eh			= ata_std_end_eh,
85029cfd6bSTejun Heo };
86029cfd6bSTejun Heo 
87029cfd6bSTejun Heo const struct ata_port_operations sata_port_ops = {
88029cfd6bSTejun Heo 	.inherits		= &ata_base_port_ops,
89029cfd6bSTejun Heo 
90029cfd6bSTejun Heo 	.qc_defer		= ata_std_qc_defer,
9157c9efdfSTejun Heo 	.hardreset		= sata_std_hardreset,
92029cfd6bSTejun Heo };
93029cfd6bSTejun Heo 
94c6fd2807SJeff Garzik static unsigned int ata_dev_init_params(struct ata_device *dev,
95c6fd2807SJeff Garzik 					u16 heads, u16 sectors);
96c6fd2807SJeff Garzik static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
97c6fd2807SJeff Garzik static void ata_dev_xfermask(struct ata_device *dev);
9875683fe7STejun Heo static unsigned long ata_dev_blacklisted(const struct ata_device *dev);
99c6fd2807SJeff Garzik 
100a78f57afSTero Roponen atomic_t ata_print_id = ATOMIC_INIT(0);
101c6fd2807SJeff Garzik 
10233267325STejun Heo struct ata_force_param {
10333267325STejun Heo 	const char	*name;
10433267325STejun Heo 	unsigned int	cbl;
10533267325STejun Heo 	int		spd_limit;
10633267325STejun Heo 	unsigned long	xfer_mask;
10733267325STejun Heo 	unsigned int	horkage_on;
10833267325STejun Heo 	unsigned int	horkage_off;
10905944bdfSTejun Heo 	unsigned int	lflags;
11033267325STejun Heo };
11133267325STejun Heo 
11233267325STejun Heo struct ata_force_ent {
11333267325STejun Heo 	int			port;
11433267325STejun Heo 	int			device;
11533267325STejun Heo 	struct ata_force_param	param;
11633267325STejun Heo };
11733267325STejun Heo 
11833267325STejun Heo static struct ata_force_ent *ata_force_tbl;
11933267325STejun Heo static int ata_force_tbl_size;
12033267325STejun Heo 
12133267325STejun Heo static char ata_force_param_buf[PAGE_SIZE] __initdata;
1227afb4222STejun Heo /* param_buf is thrown away after initialization, disallow read */
1237afb4222STejun Heo module_param_string(force, ata_force_param_buf, sizeof(ata_force_param_buf), 0);
12433267325STejun Heo MODULE_PARM_DESC(force, "Force ATA configurations including cable type, link speed and transfer mode (see Documentation/kernel-parameters.txt for details)");
12533267325STejun Heo 
1262486fa56STejun Heo static int atapi_enabled = 1;
127c6fd2807SJeff Garzik module_param(atapi_enabled, int, 0444);
128ad5d8eacSEvgeni Golov MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on [default])");
129c6fd2807SJeff Garzik 
130c5c61bdaSAdrian Bunk static int atapi_dmadir = 0;
131c6fd2807SJeff Garzik module_param(atapi_dmadir, int, 0444);
132ad5d8eacSEvgeni Golov MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off [default], 1=on)");
133c6fd2807SJeff Garzik 
134baf4fdfaSMark Lord int atapi_passthru16 = 1;
135baf4fdfaSMark Lord module_param(atapi_passthru16, int, 0444);
136ad5d8eacSEvgeni Golov MODULE_PARM_DESC(atapi_passthru16, "Enable ATA_16 passthru for ATAPI devices (0=off, 1=on [default])");
137baf4fdfaSMark Lord 
138c6fd2807SJeff Garzik int libata_fua = 0;
139c6fd2807SJeff Garzik module_param_named(fua, libata_fua, int, 0444);
140ad5d8eacSEvgeni Golov MODULE_PARM_DESC(fua, "FUA support (0=off [default], 1=on)");
141c6fd2807SJeff Garzik 
1422dcb407eSJeff Garzik static int ata_ignore_hpa;
1431e999736SAlan Cox module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644);
1441e999736SAlan Cox MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)");
1451e999736SAlan Cox 
146b3a70601SAlan Cox static int libata_dma_mask = ATA_DMA_MASK_ATA|ATA_DMA_MASK_ATAPI|ATA_DMA_MASK_CFA;
147b3a70601SAlan Cox module_param_named(dma, libata_dma_mask, int, 0444);
148b3a70601SAlan Cox MODULE_PARM_DESC(dma, "DMA enable/disable (0x1==ATA, 0x2==ATAPI, 0x4==CF)");
149b3a70601SAlan Cox 
15087fbc5a0STejun Heo static int ata_probe_timeout;
151c6fd2807SJeff Garzik module_param(ata_probe_timeout, int, 0444);
152c6fd2807SJeff Garzik MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
153c6fd2807SJeff Garzik 
1546ebe9d86SJeff Garzik int libata_noacpi = 0;
155d7d0dad6SJeff Garzik module_param_named(noacpi, libata_noacpi, int, 0444);
156ad5d8eacSEvgeni Golov MODULE_PARM_DESC(noacpi, "Disable the use of ACPI in probe/suspend/resume (0=off [default], 1=on)");
15711ef697bSKristen Carlson Accardi 
158ae8d4ee7SAlan Cox int libata_allow_tpm = 0;
159ae8d4ee7SAlan Cox module_param_named(allow_tpm, libata_allow_tpm, int, 0444);
160ad5d8eacSEvgeni Golov MODULE_PARM_DESC(allow_tpm, "Permit the use of TPM commands (0=off [default], 1=on)");
161ae8d4ee7SAlan Cox 
162e7ecd435STejun Heo static int atapi_an;
163e7ecd435STejun Heo module_param(atapi_an, int, 0444);
164e7ecd435STejun Heo MODULE_PARM_DESC(atapi_an, "Enable ATAPI AN media presence notification (0=0ff [default], 1=on)");
165e7ecd435STejun Heo 
166c6fd2807SJeff Garzik MODULE_AUTHOR("Jeff Garzik");
167c6fd2807SJeff Garzik MODULE_DESCRIPTION("Library module for ATA devices");
168c6fd2807SJeff Garzik MODULE_LICENSE("GPL");
169c6fd2807SJeff Garzik MODULE_VERSION(DRV_VERSION);
170c6fd2807SJeff Garzik 
171c6fd2807SJeff Garzik 
1729913ff8aSTejun Heo static bool ata_sstatus_online(u32 sstatus)
1739913ff8aSTejun Heo {
1749913ff8aSTejun Heo 	return (sstatus & 0xf) == 0x3;
1759913ff8aSTejun Heo }
1769913ff8aSTejun Heo 
1771eca4365STejun Heo /**
1781eca4365STejun Heo  *	ata_link_next - link iteration helper
1791eca4365STejun Heo  *	@link: the previous link, NULL to start
1801eca4365STejun Heo  *	@ap: ATA port containing links to iterate
1811eca4365STejun Heo  *	@mode: iteration mode, one of ATA_LITER_*
182aadffb68STejun Heo  *
183aadffb68STejun Heo  *	LOCKING:
184aadffb68STejun Heo  *	Host lock or EH context.
1851eca4365STejun Heo  *
1861eca4365STejun Heo  *	RETURNS:
1871eca4365STejun Heo  *	Pointer to the next link.
188aadffb68STejun Heo  */
1891eca4365STejun Heo struct ata_link *ata_link_next(struct ata_link *link, struct ata_port *ap,
1901eca4365STejun Heo 			       enum ata_link_iter_mode mode)
191aadffb68STejun Heo {
1921eca4365STejun Heo 	BUG_ON(mode != ATA_LITER_EDGE &&
1931eca4365STejun Heo 	       mode != ATA_LITER_PMP_FIRST && mode != ATA_LITER_HOST_FIRST);
1941eca4365STejun Heo 
195aadffb68STejun Heo 	/* NULL link indicates start of iteration */
1961eca4365STejun Heo 	if (!link)
1971eca4365STejun Heo 		switch (mode) {
1981eca4365STejun Heo 		case ATA_LITER_EDGE:
1991eca4365STejun Heo 		case ATA_LITER_PMP_FIRST:
2001eca4365STejun Heo 			if (sata_pmp_attached(ap))
201aadffb68STejun Heo 				return ap->pmp_link;
2021eca4365STejun Heo 			/* fall through */
2031eca4365STejun Heo 		case ATA_LITER_HOST_FIRST:
204aadffb68STejun Heo 			return &ap->link;
205aadffb68STejun Heo 		}
206aadffb68STejun Heo 
2071eca4365STejun Heo 	/* we just iterated over the host link, what's next? */
2081eca4365STejun Heo 	if (link == &ap->link)
2091eca4365STejun Heo 		switch (mode) {
2101eca4365STejun Heo 		case ATA_LITER_HOST_FIRST:
2111eca4365STejun Heo 			if (sata_pmp_attached(ap))
212aadffb68STejun Heo 				return ap->pmp_link;
2131eca4365STejun Heo 			/* fall through */
2141eca4365STejun Heo 		case ATA_LITER_PMP_FIRST:
2151eca4365STejun Heo 			if (unlikely(ap->slave_link))
2161eca4365STejun Heo 				return ap->slave_link;
2171eca4365STejun Heo 			/* fall through */
2181eca4365STejun Heo 		case ATA_LITER_EDGE:
2191eca4365STejun Heo 			return NULL;
220aadffb68STejun Heo 		}
221aadffb68STejun Heo 
222b1c72916STejun Heo 	/* slave_link excludes PMP */
223b1c72916STejun Heo 	if (unlikely(link == ap->slave_link))
224b1c72916STejun Heo 		return NULL;
225b1c72916STejun Heo 
2261eca4365STejun Heo 	/* we were over a PMP link */
227aadffb68STejun Heo 	if (++link < ap->pmp_link + ap->nr_pmp_links)
228aadffb68STejun Heo 		return link;
2291eca4365STejun Heo 
2301eca4365STejun Heo 	if (mode == ATA_LITER_PMP_FIRST)
2311eca4365STejun Heo 		return &ap->link;
2321eca4365STejun Heo 
233aadffb68STejun Heo 	return NULL;
234aadffb68STejun Heo }
235aadffb68STejun Heo 
236c6fd2807SJeff Garzik /**
2371eca4365STejun Heo  *	ata_dev_next - device iteration helper
2381eca4365STejun Heo  *	@dev: the previous device, NULL to start
2391eca4365STejun Heo  *	@link: ATA link containing devices to iterate
2401eca4365STejun Heo  *	@mode: iteration mode, one of ATA_DITER_*
2411eca4365STejun Heo  *
2421eca4365STejun Heo  *	LOCKING:
2431eca4365STejun Heo  *	Host lock or EH context.
2441eca4365STejun Heo  *
2451eca4365STejun Heo  *	RETURNS:
2461eca4365STejun Heo  *	Pointer to the next device.
2471eca4365STejun Heo  */
2481eca4365STejun Heo struct ata_device *ata_dev_next(struct ata_device *dev, struct ata_link *link,
2491eca4365STejun Heo 				enum ata_dev_iter_mode mode)
2501eca4365STejun Heo {
2511eca4365STejun Heo 	BUG_ON(mode != ATA_DITER_ENABLED && mode != ATA_DITER_ENABLED_REVERSE &&
2521eca4365STejun Heo 	       mode != ATA_DITER_ALL && mode != ATA_DITER_ALL_REVERSE);
2531eca4365STejun Heo 
2541eca4365STejun Heo 	/* NULL dev indicates start of iteration */
2551eca4365STejun Heo 	if (!dev)
2561eca4365STejun Heo 		switch (mode) {
2571eca4365STejun Heo 		case ATA_DITER_ENABLED:
2581eca4365STejun Heo 		case ATA_DITER_ALL:
2591eca4365STejun Heo 			dev = link->device;
2601eca4365STejun Heo 			goto check;
2611eca4365STejun Heo 		case ATA_DITER_ENABLED_REVERSE:
2621eca4365STejun Heo 		case ATA_DITER_ALL_REVERSE:
2631eca4365STejun Heo 			dev = link->device + ata_link_max_devices(link) - 1;
2641eca4365STejun Heo 			goto check;
2651eca4365STejun Heo 		}
2661eca4365STejun Heo 
2671eca4365STejun Heo  next:
2681eca4365STejun Heo 	/* move to the next one */
2691eca4365STejun Heo 	switch (mode) {
2701eca4365STejun Heo 	case ATA_DITER_ENABLED:
2711eca4365STejun Heo 	case ATA_DITER_ALL:
2721eca4365STejun Heo 		if (++dev < link->device + ata_link_max_devices(link))
2731eca4365STejun Heo 			goto check;
2741eca4365STejun Heo 		return NULL;
2751eca4365STejun Heo 	case ATA_DITER_ENABLED_REVERSE:
2761eca4365STejun Heo 	case ATA_DITER_ALL_REVERSE:
2771eca4365STejun Heo 		if (--dev >= link->device)
2781eca4365STejun Heo 			goto check;
2791eca4365STejun Heo 		return NULL;
2801eca4365STejun Heo 	}
2811eca4365STejun Heo 
2821eca4365STejun Heo  check:
2831eca4365STejun Heo 	if ((mode == ATA_DITER_ENABLED || mode == ATA_DITER_ENABLED_REVERSE) &&
2841eca4365STejun Heo 	    !ata_dev_enabled(dev))
2851eca4365STejun Heo 		goto next;
2861eca4365STejun Heo 	return dev;
2871eca4365STejun Heo }
2881eca4365STejun Heo 
2891eca4365STejun Heo /**
290b1c72916STejun Heo  *	ata_dev_phys_link - find physical link for a device
291b1c72916STejun Heo  *	@dev: ATA device to look up physical link for
292b1c72916STejun Heo  *
293b1c72916STejun Heo  *	Look up physical link which @dev is attached to.  Note that
294b1c72916STejun Heo  *	this is different from @dev->link only when @dev is on slave
295b1c72916STejun Heo  *	link.  For all other cases, it's the same as @dev->link.
296b1c72916STejun Heo  *
297b1c72916STejun Heo  *	LOCKING:
298b1c72916STejun Heo  *	Don't care.
299b1c72916STejun Heo  *
300b1c72916STejun Heo  *	RETURNS:
301b1c72916STejun Heo  *	Pointer to the found physical link.
302b1c72916STejun Heo  */
303b1c72916STejun Heo struct ata_link *ata_dev_phys_link(struct ata_device *dev)
304b1c72916STejun Heo {
305b1c72916STejun Heo 	struct ata_port *ap = dev->link->ap;
306b1c72916STejun Heo 
307b1c72916STejun Heo 	if (!ap->slave_link)
308b1c72916STejun Heo 		return dev->link;
309b1c72916STejun Heo 	if (!dev->devno)
310b1c72916STejun Heo 		return &ap->link;
311b1c72916STejun Heo 	return ap->slave_link;
312b1c72916STejun Heo }
313b1c72916STejun Heo 
314b1c72916STejun Heo /**
31533267325STejun Heo  *	ata_force_cbl - force cable type according to libata.force
3164cdfa1b3SRandy Dunlap  *	@ap: ATA port of interest
31733267325STejun Heo  *
31833267325STejun Heo  *	Force cable type according to libata.force and whine about it.
31933267325STejun Heo  *	The last entry which has matching port number is used, so it
32033267325STejun Heo  *	can be specified as part of device force parameters.  For
32133267325STejun Heo  *	example, both "a:40c,1.00:udma4" and "1.00:40c,udma4" have the
32233267325STejun Heo  *	same effect.
32333267325STejun Heo  *
32433267325STejun Heo  *	LOCKING:
32533267325STejun Heo  *	EH context.
32633267325STejun Heo  */
32733267325STejun Heo void ata_force_cbl(struct ata_port *ap)
32833267325STejun Heo {
32933267325STejun Heo 	int i;
33033267325STejun Heo 
33133267325STejun Heo 	for (i = ata_force_tbl_size - 1; i >= 0; i--) {
33233267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
33333267325STejun Heo 
33433267325STejun Heo 		if (fe->port != -1 && fe->port != ap->print_id)
33533267325STejun Heo 			continue;
33633267325STejun Heo 
33733267325STejun Heo 		if (fe->param.cbl == ATA_CBL_NONE)
33833267325STejun Heo 			continue;
33933267325STejun Heo 
34033267325STejun Heo 		ap->cbl = fe->param.cbl;
341a9a79dfeSJoe Perches 		ata_port_notice(ap, "FORCE: cable set to %s\n", fe->param.name);
34233267325STejun Heo 		return;
34333267325STejun Heo 	}
34433267325STejun Heo }
34533267325STejun Heo 
34633267325STejun Heo /**
34705944bdfSTejun Heo  *	ata_force_link_limits - force link limits according to libata.force
34833267325STejun Heo  *	@link: ATA link of interest
34933267325STejun Heo  *
35005944bdfSTejun Heo  *	Force link flags and SATA spd limit according to libata.force
35105944bdfSTejun Heo  *	and whine about it.  When only the port part is specified
35205944bdfSTejun Heo  *	(e.g. 1:), the limit applies to all links connected to both
35305944bdfSTejun Heo  *	the host link and all fan-out ports connected via PMP.  If the
35405944bdfSTejun Heo  *	device part is specified as 0 (e.g. 1.00:), it specifies the
35505944bdfSTejun Heo  *	first fan-out link not the host link.  Device number 15 always
356b1c72916STejun Heo  *	points to the host link whether PMP is attached or not.  If the
357b1c72916STejun Heo  *	controller has slave link, device number 16 points to it.
35833267325STejun Heo  *
35933267325STejun Heo  *	LOCKING:
36033267325STejun Heo  *	EH context.
36133267325STejun Heo  */
36205944bdfSTejun Heo static void ata_force_link_limits(struct ata_link *link)
36333267325STejun Heo {
36405944bdfSTejun Heo 	bool did_spd = false;
365b1c72916STejun Heo 	int linkno = link->pmp;
366b1c72916STejun Heo 	int i;
36733267325STejun Heo 
36833267325STejun Heo 	if (ata_is_host_link(link))
369b1c72916STejun Heo 		linkno += 15;
37033267325STejun Heo 
37133267325STejun Heo 	for (i = ata_force_tbl_size - 1; i >= 0; i--) {
37233267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
37333267325STejun Heo 
37433267325STejun Heo 		if (fe->port != -1 && fe->port != link->ap->print_id)
37533267325STejun Heo 			continue;
37633267325STejun Heo 
37733267325STejun Heo 		if (fe->device != -1 && fe->device != linkno)
37833267325STejun Heo 			continue;
37933267325STejun Heo 
38005944bdfSTejun Heo 		/* only honor the first spd limit */
38105944bdfSTejun Heo 		if (!did_spd && fe->param.spd_limit) {
38233267325STejun Heo 			link->hw_sata_spd_limit = (1 << fe->param.spd_limit) - 1;
383a9a79dfeSJoe Perches 			ata_link_notice(link, "FORCE: PHY spd limit set to %s\n",
38405944bdfSTejun Heo 					fe->param.name);
38505944bdfSTejun Heo 			did_spd = true;
38605944bdfSTejun Heo 		}
38705944bdfSTejun Heo 
38805944bdfSTejun Heo 		/* let lflags stack */
38905944bdfSTejun Heo 		if (fe->param.lflags) {
39005944bdfSTejun Heo 			link->flags |= fe->param.lflags;
391a9a79dfeSJoe Perches 			ata_link_notice(link,
39205944bdfSTejun Heo 					"FORCE: link flag 0x%x forced -> 0x%x\n",
39305944bdfSTejun Heo 					fe->param.lflags, link->flags);
39405944bdfSTejun Heo 		}
39533267325STejun Heo 	}
39633267325STejun Heo }
39733267325STejun Heo 
39833267325STejun Heo /**
39933267325STejun Heo  *	ata_force_xfermask - force xfermask according to libata.force
40033267325STejun Heo  *	@dev: ATA device of interest
40133267325STejun Heo  *
40233267325STejun Heo  *	Force xfer_mask according to libata.force and whine about it.
40333267325STejun Heo  *	For consistency with link selection, device number 15 selects
40433267325STejun Heo  *	the first device connected to the host link.
40533267325STejun Heo  *
40633267325STejun Heo  *	LOCKING:
40733267325STejun Heo  *	EH context.
40833267325STejun Heo  */
40933267325STejun Heo static void ata_force_xfermask(struct ata_device *dev)
41033267325STejun Heo {
41133267325STejun Heo 	int devno = dev->link->pmp + dev->devno;
41233267325STejun Heo 	int alt_devno = devno;
41333267325STejun Heo 	int i;
41433267325STejun Heo 
415b1c72916STejun Heo 	/* allow n.15/16 for devices attached to host port */
416b1c72916STejun Heo 	if (ata_is_host_link(dev->link))
417b1c72916STejun Heo 		alt_devno += 15;
41833267325STejun Heo 
41933267325STejun Heo 	for (i = ata_force_tbl_size - 1; i >= 0; i--) {
42033267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
42133267325STejun Heo 		unsigned long pio_mask, mwdma_mask, udma_mask;
42233267325STejun Heo 
42333267325STejun Heo 		if (fe->port != -1 && fe->port != dev->link->ap->print_id)
42433267325STejun Heo 			continue;
42533267325STejun Heo 
42633267325STejun Heo 		if (fe->device != -1 && fe->device != devno &&
42733267325STejun Heo 		    fe->device != alt_devno)
42833267325STejun Heo 			continue;
42933267325STejun Heo 
43033267325STejun Heo 		if (!fe->param.xfer_mask)
43133267325STejun Heo 			continue;
43233267325STejun Heo 
43333267325STejun Heo 		ata_unpack_xfermask(fe->param.xfer_mask,
43433267325STejun Heo 				    &pio_mask, &mwdma_mask, &udma_mask);
43533267325STejun Heo 		if (udma_mask)
43633267325STejun Heo 			dev->udma_mask = udma_mask;
43733267325STejun Heo 		else if (mwdma_mask) {
43833267325STejun Heo 			dev->udma_mask = 0;
43933267325STejun Heo 			dev->mwdma_mask = mwdma_mask;
44033267325STejun Heo 		} else {
44133267325STejun Heo 			dev->udma_mask = 0;
44233267325STejun Heo 			dev->mwdma_mask = 0;
44333267325STejun Heo 			dev->pio_mask = pio_mask;
44433267325STejun Heo 		}
44533267325STejun Heo 
446a9a79dfeSJoe Perches 		ata_dev_notice(dev, "FORCE: xfer_mask set to %s\n",
447a9a79dfeSJoe Perches 			       fe->param.name);
44833267325STejun Heo 		return;
44933267325STejun Heo 	}
45033267325STejun Heo }
45133267325STejun Heo 
45233267325STejun Heo /**
45333267325STejun Heo  *	ata_force_horkage - force horkage according to libata.force
45433267325STejun Heo  *	@dev: ATA device of interest
45533267325STejun Heo  *
45633267325STejun Heo  *	Force horkage according to libata.force and whine about it.
45733267325STejun Heo  *	For consistency with link selection, device number 15 selects
45833267325STejun Heo  *	the first device connected to the host link.
45933267325STejun Heo  *
46033267325STejun Heo  *	LOCKING:
46133267325STejun Heo  *	EH context.
46233267325STejun Heo  */
46333267325STejun Heo static void ata_force_horkage(struct ata_device *dev)
46433267325STejun Heo {
46533267325STejun Heo 	int devno = dev->link->pmp + dev->devno;
46633267325STejun Heo 	int alt_devno = devno;
46733267325STejun Heo 	int i;
46833267325STejun Heo 
469b1c72916STejun Heo 	/* allow n.15/16 for devices attached to host port */
470b1c72916STejun Heo 	if (ata_is_host_link(dev->link))
471b1c72916STejun Heo 		alt_devno += 15;
47233267325STejun Heo 
47333267325STejun Heo 	for (i = 0; i < ata_force_tbl_size; i++) {
47433267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
47533267325STejun Heo 
47633267325STejun Heo 		if (fe->port != -1 && fe->port != dev->link->ap->print_id)
47733267325STejun Heo 			continue;
47833267325STejun Heo 
47933267325STejun Heo 		if (fe->device != -1 && fe->device != devno &&
48033267325STejun Heo 		    fe->device != alt_devno)
48133267325STejun Heo 			continue;
48233267325STejun Heo 
48333267325STejun Heo 		if (!(~dev->horkage & fe->param.horkage_on) &&
48433267325STejun Heo 		    !(dev->horkage & fe->param.horkage_off))
48533267325STejun Heo 			continue;
48633267325STejun Heo 
48733267325STejun Heo 		dev->horkage |= fe->param.horkage_on;
48833267325STejun Heo 		dev->horkage &= ~fe->param.horkage_off;
48933267325STejun Heo 
490a9a79dfeSJoe Perches 		ata_dev_notice(dev, "FORCE: horkage modified (%s)\n",
491a9a79dfeSJoe Perches 			       fe->param.name);
49233267325STejun Heo 	}
49333267325STejun Heo }
49433267325STejun Heo 
49533267325STejun Heo /**
496436d34b3STejun Heo  *	atapi_cmd_type - Determine ATAPI command type from SCSI opcode
497436d34b3STejun Heo  *	@opcode: SCSI opcode
498436d34b3STejun Heo  *
499436d34b3STejun Heo  *	Determine ATAPI command type from @opcode.
500436d34b3STejun Heo  *
501436d34b3STejun Heo  *	LOCKING:
502436d34b3STejun Heo  *	None.
503436d34b3STejun Heo  *
504436d34b3STejun Heo  *	RETURNS:
505436d34b3STejun Heo  *	ATAPI_{READ|WRITE|READ_CD|PASS_THRU|MISC}
506436d34b3STejun Heo  */
507436d34b3STejun Heo int atapi_cmd_type(u8 opcode)
508436d34b3STejun Heo {
509436d34b3STejun Heo 	switch (opcode) {
510436d34b3STejun Heo 	case GPCMD_READ_10:
511436d34b3STejun Heo 	case GPCMD_READ_12:
512436d34b3STejun Heo 		return ATAPI_READ;
513436d34b3STejun Heo 
514436d34b3STejun Heo 	case GPCMD_WRITE_10:
515436d34b3STejun Heo 	case GPCMD_WRITE_12:
516436d34b3STejun Heo 	case GPCMD_WRITE_AND_VERIFY_10:
517436d34b3STejun Heo 		return ATAPI_WRITE;
518436d34b3STejun Heo 
519436d34b3STejun Heo 	case GPCMD_READ_CD:
520436d34b3STejun Heo 	case GPCMD_READ_CD_MSF:
521436d34b3STejun Heo 		return ATAPI_READ_CD;
522436d34b3STejun Heo 
523e52dcc48STejun Heo 	case ATA_16:
524e52dcc48STejun Heo 	case ATA_12:
525e52dcc48STejun Heo 		if (atapi_passthru16)
526e52dcc48STejun Heo 			return ATAPI_PASS_THRU;
527e52dcc48STejun Heo 		/* fall thru */
528436d34b3STejun Heo 	default:
529436d34b3STejun Heo 		return ATAPI_MISC;
530436d34b3STejun Heo 	}
531436d34b3STejun Heo }
532436d34b3STejun Heo 
533436d34b3STejun Heo /**
534c6fd2807SJeff Garzik  *	ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
535c6fd2807SJeff Garzik  *	@tf: Taskfile to convert
536c6fd2807SJeff Garzik  *	@pmp: Port multiplier port
5379977126cSTejun Heo  *	@is_cmd: This FIS is for command
5389977126cSTejun Heo  *	@fis: Buffer into which data will output
539c6fd2807SJeff Garzik  *
540c6fd2807SJeff Garzik  *	Converts a standard ATA taskfile to a Serial ATA
541c6fd2807SJeff Garzik  *	FIS structure (Register - Host to Device).
542c6fd2807SJeff Garzik  *
543c6fd2807SJeff Garzik  *	LOCKING:
544c6fd2807SJeff Garzik  *	Inherited from caller.
545c6fd2807SJeff Garzik  */
5469977126cSTejun Heo void ata_tf_to_fis(const struct ata_taskfile *tf, u8 pmp, int is_cmd, u8 *fis)
547c6fd2807SJeff Garzik {
548c6fd2807SJeff Garzik 	fis[0] = 0x27;			/* Register - Host to Device FIS */
5499977126cSTejun Heo 	fis[1] = pmp & 0xf;		/* Port multiplier number*/
5509977126cSTejun Heo 	if (is_cmd)
5519977126cSTejun Heo 		fis[1] |= (1 << 7);	/* bit 7 indicates Command FIS */
5529977126cSTejun Heo 
553c6fd2807SJeff Garzik 	fis[2] = tf->command;
554c6fd2807SJeff Garzik 	fis[3] = tf->feature;
555c6fd2807SJeff Garzik 
556c6fd2807SJeff Garzik 	fis[4] = tf->lbal;
557c6fd2807SJeff Garzik 	fis[5] = tf->lbam;
558c6fd2807SJeff Garzik 	fis[6] = tf->lbah;
559c6fd2807SJeff Garzik 	fis[7] = tf->device;
560c6fd2807SJeff Garzik 
561c6fd2807SJeff Garzik 	fis[8] = tf->hob_lbal;
562c6fd2807SJeff Garzik 	fis[9] = tf->hob_lbam;
563c6fd2807SJeff Garzik 	fis[10] = tf->hob_lbah;
564c6fd2807SJeff Garzik 	fis[11] = tf->hob_feature;
565c6fd2807SJeff Garzik 
566c6fd2807SJeff Garzik 	fis[12] = tf->nsect;
567c6fd2807SJeff Garzik 	fis[13] = tf->hob_nsect;
568c6fd2807SJeff Garzik 	fis[14] = 0;
569c6fd2807SJeff Garzik 	fis[15] = tf->ctl;
570c6fd2807SJeff Garzik 
571c6fd2807SJeff Garzik 	fis[16] = 0;
572c6fd2807SJeff Garzik 	fis[17] = 0;
573c6fd2807SJeff Garzik 	fis[18] = 0;
574c6fd2807SJeff Garzik 	fis[19] = 0;
575c6fd2807SJeff Garzik }
576c6fd2807SJeff Garzik 
577c6fd2807SJeff Garzik /**
578c6fd2807SJeff Garzik  *	ata_tf_from_fis - Convert SATA FIS to ATA taskfile
579c6fd2807SJeff Garzik  *	@fis: Buffer from which data will be input
580c6fd2807SJeff Garzik  *	@tf: Taskfile to output
581c6fd2807SJeff Garzik  *
582c6fd2807SJeff Garzik  *	Converts a serial ATA FIS structure to a standard ATA taskfile.
583c6fd2807SJeff Garzik  *
584c6fd2807SJeff Garzik  *	LOCKING:
585c6fd2807SJeff Garzik  *	Inherited from caller.
586c6fd2807SJeff Garzik  */
587c6fd2807SJeff Garzik 
588c6fd2807SJeff Garzik void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
589c6fd2807SJeff Garzik {
590c6fd2807SJeff Garzik 	tf->command	= fis[2];	/* status */
591c6fd2807SJeff Garzik 	tf->feature	= fis[3];	/* error */
592c6fd2807SJeff Garzik 
593c6fd2807SJeff Garzik 	tf->lbal	= fis[4];
594c6fd2807SJeff Garzik 	tf->lbam	= fis[5];
595c6fd2807SJeff Garzik 	tf->lbah	= fis[6];
596c6fd2807SJeff Garzik 	tf->device	= fis[7];
597c6fd2807SJeff Garzik 
598c6fd2807SJeff Garzik 	tf->hob_lbal	= fis[8];
599c6fd2807SJeff Garzik 	tf->hob_lbam	= fis[9];
600c6fd2807SJeff Garzik 	tf->hob_lbah	= fis[10];
601c6fd2807SJeff Garzik 
602c6fd2807SJeff Garzik 	tf->nsect	= fis[12];
603c6fd2807SJeff Garzik 	tf->hob_nsect	= fis[13];
604c6fd2807SJeff Garzik }
605c6fd2807SJeff Garzik 
606c6fd2807SJeff Garzik static const u8 ata_rw_cmds[] = {
607c6fd2807SJeff Garzik 	/* pio multi */
608c6fd2807SJeff Garzik 	ATA_CMD_READ_MULTI,
609c6fd2807SJeff Garzik 	ATA_CMD_WRITE_MULTI,
610c6fd2807SJeff Garzik 	ATA_CMD_READ_MULTI_EXT,
611c6fd2807SJeff Garzik 	ATA_CMD_WRITE_MULTI_EXT,
612c6fd2807SJeff Garzik 	0,
613c6fd2807SJeff Garzik 	0,
614c6fd2807SJeff Garzik 	0,
615c6fd2807SJeff Garzik 	ATA_CMD_WRITE_MULTI_FUA_EXT,
616c6fd2807SJeff Garzik 	/* pio */
617c6fd2807SJeff Garzik 	ATA_CMD_PIO_READ,
618c6fd2807SJeff Garzik 	ATA_CMD_PIO_WRITE,
619c6fd2807SJeff Garzik 	ATA_CMD_PIO_READ_EXT,
620c6fd2807SJeff Garzik 	ATA_CMD_PIO_WRITE_EXT,
621c6fd2807SJeff Garzik 	0,
622c6fd2807SJeff Garzik 	0,
623c6fd2807SJeff Garzik 	0,
624c6fd2807SJeff Garzik 	0,
625c6fd2807SJeff Garzik 	/* dma */
626c6fd2807SJeff Garzik 	ATA_CMD_READ,
627c6fd2807SJeff Garzik 	ATA_CMD_WRITE,
628c6fd2807SJeff Garzik 	ATA_CMD_READ_EXT,
629c6fd2807SJeff Garzik 	ATA_CMD_WRITE_EXT,
630c6fd2807SJeff Garzik 	0,
631c6fd2807SJeff Garzik 	0,
632c6fd2807SJeff Garzik 	0,
633c6fd2807SJeff Garzik 	ATA_CMD_WRITE_FUA_EXT
634c6fd2807SJeff Garzik };
635c6fd2807SJeff Garzik 
636c6fd2807SJeff Garzik /**
637c6fd2807SJeff Garzik  *	ata_rwcmd_protocol - set taskfile r/w commands and protocol
638bd056d7eSTejun Heo  *	@tf: command to examine and configure
639bd056d7eSTejun Heo  *	@dev: device tf belongs to
640c6fd2807SJeff Garzik  *
641c6fd2807SJeff Garzik  *	Examine the device configuration and tf->flags to calculate
642c6fd2807SJeff Garzik  *	the proper read/write commands and protocol to use.
643c6fd2807SJeff Garzik  *
644c6fd2807SJeff Garzik  *	LOCKING:
645c6fd2807SJeff Garzik  *	caller.
646c6fd2807SJeff Garzik  */
647bd056d7eSTejun Heo static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev)
648c6fd2807SJeff Garzik {
649c6fd2807SJeff Garzik 	u8 cmd;
650c6fd2807SJeff Garzik 
651c6fd2807SJeff Garzik 	int index, fua, lba48, write;
652c6fd2807SJeff Garzik 
653c6fd2807SJeff Garzik 	fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
654c6fd2807SJeff Garzik 	lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
655c6fd2807SJeff Garzik 	write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
656c6fd2807SJeff Garzik 
657c6fd2807SJeff Garzik 	if (dev->flags & ATA_DFLAG_PIO) {
658c6fd2807SJeff Garzik 		tf->protocol = ATA_PROT_PIO;
659c6fd2807SJeff Garzik 		index = dev->multi_count ? 0 : 8;
6609af5c9c9STejun Heo 	} else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) {
661c6fd2807SJeff Garzik 		/* Unable to use DMA due to host limitation */
662c6fd2807SJeff Garzik 		tf->protocol = ATA_PROT_PIO;
663c6fd2807SJeff Garzik 		index = dev->multi_count ? 0 : 8;
664c6fd2807SJeff Garzik 	} else {
665c6fd2807SJeff Garzik 		tf->protocol = ATA_PROT_DMA;
666c6fd2807SJeff Garzik 		index = 16;
667c6fd2807SJeff Garzik 	}
668c6fd2807SJeff Garzik 
669c6fd2807SJeff Garzik 	cmd = ata_rw_cmds[index + fua + lba48 + write];
670c6fd2807SJeff Garzik 	if (cmd) {
671c6fd2807SJeff Garzik 		tf->command = cmd;
672c6fd2807SJeff Garzik 		return 0;
673c6fd2807SJeff Garzik 	}
674c6fd2807SJeff Garzik 	return -1;
675c6fd2807SJeff Garzik }
676c6fd2807SJeff Garzik 
677c6fd2807SJeff Garzik /**
67835b649feSTejun Heo  *	ata_tf_read_block - Read block address from ATA taskfile
67935b649feSTejun Heo  *	@tf: ATA taskfile of interest
68035b649feSTejun Heo  *	@dev: ATA device @tf belongs to
68135b649feSTejun Heo  *
68235b649feSTejun Heo  *	LOCKING:
68335b649feSTejun Heo  *	None.
68435b649feSTejun Heo  *
68535b649feSTejun Heo  *	Read block address from @tf.  This function can handle all
68635b649feSTejun Heo  *	three address formats - LBA, LBA48 and CHS.  tf->protocol and
68735b649feSTejun Heo  *	flags select the address format to use.
68835b649feSTejun Heo  *
68935b649feSTejun Heo  *	RETURNS:
69035b649feSTejun Heo  *	Block address read from @tf.
69135b649feSTejun Heo  */
69235b649feSTejun Heo u64 ata_tf_read_block(struct ata_taskfile *tf, struct ata_device *dev)
69335b649feSTejun Heo {
69435b649feSTejun Heo 	u64 block = 0;
69535b649feSTejun Heo 
69635b649feSTejun Heo 	if (tf->flags & ATA_TFLAG_LBA) {
69735b649feSTejun Heo 		if (tf->flags & ATA_TFLAG_LBA48) {
69835b649feSTejun Heo 			block |= (u64)tf->hob_lbah << 40;
69935b649feSTejun Heo 			block |= (u64)tf->hob_lbam << 32;
70044901a96SRoland Dreier 			block |= (u64)tf->hob_lbal << 24;
70135b649feSTejun Heo 		} else
70235b649feSTejun Heo 			block |= (tf->device & 0xf) << 24;
70335b649feSTejun Heo 
70435b649feSTejun Heo 		block |= tf->lbah << 16;
70535b649feSTejun Heo 		block |= tf->lbam << 8;
70635b649feSTejun Heo 		block |= tf->lbal;
70735b649feSTejun Heo 	} else {
70835b649feSTejun Heo 		u32 cyl, head, sect;
70935b649feSTejun Heo 
71035b649feSTejun Heo 		cyl = tf->lbam | (tf->lbah << 8);
71135b649feSTejun Heo 		head = tf->device & 0xf;
71235b649feSTejun Heo 		sect = tf->lbal;
71335b649feSTejun Heo 
714ac8672eaSTejun Heo 		if (!sect) {
715a9a79dfeSJoe Perches 			ata_dev_warn(dev,
716a9a79dfeSJoe Perches 				     "device reported invalid CHS sector 0\n");
717ac8672eaSTejun Heo 			sect = 1; /* oh well */
718ac8672eaSTejun Heo 		}
719ac8672eaSTejun Heo 
720ac8672eaSTejun Heo 		block = (cyl * dev->heads + head) * dev->sectors + sect - 1;
72135b649feSTejun Heo 	}
72235b649feSTejun Heo 
72335b649feSTejun Heo 	return block;
72435b649feSTejun Heo }
72535b649feSTejun Heo 
72635b649feSTejun Heo /**
727bd056d7eSTejun Heo  *	ata_build_rw_tf - Build ATA taskfile for given read/write request
728bd056d7eSTejun Heo  *	@tf: Target ATA taskfile
729bd056d7eSTejun Heo  *	@dev: ATA device @tf belongs to
730bd056d7eSTejun Heo  *	@block: Block address
731bd056d7eSTejun Heo  *	@n_block: Number of blocks
732bd056d7eSTejun Heo  *	@tf_flags: RW/FUA etc...
733bd056d7eSTejun Heo  *	@tag: tag
734bd056d7eSTejun Heo  *
735bd056d7eSTejun Heo  *	LOCKING:
736bd056d7eSTejun Heo  *	None.
737bd056d7eSTejun Heo  *
738bd056d7eSTejun Heo  *	Build ATA taskfile @tf for read/write request described by
739bd056d7eSTejun Heo  *	@block, @n_block, @tf_flags and @tag on @dev.
740bd056d7eSTejun Heo  *
741bd056d7eSTejun Heo  *	RETURNS:
742bd056d7eSTejun Heo  *
743bd056d7eSTejun Heo  *	0 on success, -ERANGE if the request is too large for @dev,
744bd056d7eSTejun Heo  *	-EINVAL if the request is invalid.
745bd056d7eSTejun Heo  */
746bd056d7eSTejun Heo int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev,
747bd056d7eSTejun Heo 		    u64 block, u32 n_block, unsigned int tf_flags,
748bd056d7eSTejun Heo 		    unsigned int tag)
749bd056d7eSTejun Heo {
750bd056d7eSTejun Heo 	tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
751bd056d7eSTejun Heo 	tf->flags |= tf_flags;
752bd056d7eSTejun Heo 
7536d1245bfSTejun Heo 	if (ata_ncq_enabled(dev) && likely(tag != ATA_TAG_INTERNAL)) {
754bd056d7eSTejun Heo 		/* yay, NCQ */
755bd056d7eSTejun Heo 		if (!lba_48_ok(block, n_block))
756bd056d7eSTejun Heo 			return -ERANGE;
757bd056d7eSTejun Heo 
758bd056d7eSTejun Heo 		tf->protocol = ATA_PROT_NCQ;
759bd056d7eSTejun Heo 		tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
760bd056d7eSTejun Heo 
761bd056d7eSTejun Heo 		if (tf->flags & ATA_TFLAG_WRITE)
762bd056d7eSTejun Heo 			tf->command = ATA_CMD_FPDMA_WRITE;
763bd056d7eSTejun Heo 		else
764bd056d7eSTejun Heo 			tf->command = ATA_CMD_FPDMA_READ;
765bd056d7eSTejun Heo 
766bd056d7eSTejun Heo 		tf->nsect = tag << 3;
767bd056d7eSTejun Heo 		tf->hob_feature = (n_block >> 8) & 0xff;
768bd056d7eSTejun Heo 		tf->feature = n_block & 0xff;
769bd056d7eSTejun Heo 
770bd056d7eSTejun Heo 		tf->hob_lbah = (block >> 40) & 0xff;
771bd056d7eSTejun Heo 		tf->hob_lbam = (block >> 32) & 0xff;
772bd056d7eSTejun Heo 		tf->hob_lbal = (block >> 24) & 0xff;
773bd056d7eSTejun Heo 		tf->lbah = (block >> 16) & 0xff;
774bd056d7eSTejun Heo 		tf->lbam = (block >> 8) & 0xff;
775bd056d7eSTejun Heo 		tf->lbal = block & 0xff;
776bd056d7eSTejun Heo 
7779ca7cfa4SSergei Shtylyov 		tf->device = ATA_LBA;
778bd056d7eSTejun Heo 		if (tf->flags & ATA_TFLAG_FUA)
779bd056d7eSTejun Heo 			tf->device |= 1 << 7;
780bd056d7eSTejun Heo 	} else if (dev->flags & ATA_DFLAG_LBA) {
781bd056d7eSTejun Heo 		tf->flags |= ATA_TFLAG_LBA;
782bd056d7eSTejun Heo 
783bd056d7eSTejun Heo 		if (lba_28_ok(block, n_block)) {
784bd056d7eSTejun Heo 			/* use LBA28 */
785bd056d7eSTejun Heo 			tf->device |= (block >> 24) & 0xf;
786bd056d7eSTejun Heo 		} else if (lba_48_ok(block, n_block)) {
787bd056d7eSTejun Heo 			if (!(dev->flags & ATA_DFLAG_LBA48))
788bd056d7eSTejun Heo 				return -ERANGE;
789bd056d7eSTejun Heo 
790bd056d7eSTejun Heo 			/* use LBA48 */
791bd056d7eSTejun Heo 			tf->flags |= ATA_TFLAG_LBA48;
792bd056d7eSTejun Heo 
793bd056d7eSTejun Heo 			tf->hob_nsect = (n_block >> 8) & 0xff;
794bd056d7eSTejun Heo 
795bd056d7eSTejun Heo 			tf->hob_lbah = (block >> 40) & 0xff;
796bd056d7eSTejun Heo 			tf->hob_lbam = (block >> 32) & 0xff;
797bd056d7eSTejun Heo 			tf->hob_lbal = (block >> 24) & 0xff;
798bd056d7eSTejun Heo 		} else
799bd056d7eSTejun Heo 			/* request too large even for LBA48 */
800bd056d7eSTejun Heo 			return -ERANGE;
801bd056d7eSTejun Heo 
802bd056d7eSTejun Heo 		if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
803bd056d7eSTejun Heo 			return -EINVAL;
804bd056d7eSTejun Heo 
805bd056d7eSTejun Heo 		tf->nsect = n_block & 0xff;
806bd056d7eSTejun Heo 
807bd056d7eSTejun Heo 		tf->lbah = (block >> 16) & 0xff;
808bd056d7eSTejun Heo 		tf->lbam = (block >> 8) & 0xff;
809bd056d7eSTejun Heo 		tf->lbal = block & 0xff;
810bd056d7eSTejun Heo 
811bd056d7eSTejun Heo 		tf->device |= ATA_LBA;
812bd056d7eSTejun Heo 	} else {
813bd056d7eSTejun Heo 		/* CHS */
814bd056d7eSTejun Heo 		u32 sect, head, cyl, track;
815bd056d7eSTejun Heo 
816bd056d7eSTejun Heo 		/* The request -may- be too large for CHS addressing. */
817bd056d7eSTejun Heo 		if (!lba_28_ok(block, n_block))
818bd056d7eSTejun Heo 			return -ERANGE;
819bd056d7eSTejun Heo 
820bd056d7eSTejun Heo 		if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
821bd056d7eSTejun Heo 			return -EINVAL;
822bd056d7eSTejun Heo 
823bd056d7eSTejun Heo 		/* Convert LBA to CHS */
824bd056d7eSTejun Heo 		track = (u32)block / dev->sectors;
825bd056d7eSTejun Heo 		cyl   = track / dev->heads;
826bd056d7eSTejun Heo 		head  = track % dev->heads;
827bd056d7eSTejun Heo 		sect  = (u32)block % dev->sectors + 1;
828bd056d7eSTejun Heo 
829bd056d7eSTejun Heo 		DPRINTK("block %u track %u cyl %u head %u sect %u\n",
830bd056d7eSTejun Heo 			(u32)block, track, cyl, head, sect);
831bd056d7eSTejun Heo 
832bd056d7eSTejun Heo 		/* Check whether the converted CHS can fit.
833bd056d7eSTejun Heo 		   Cylinder: 0-65535
834bd056d7eSTejun Heo 		   Head: 0-15
835bd056d7eSTejun Heo 		   Sector: 1-255*/
836bd056d7eSTejun Heo 		if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
837bd056d7eSTejun Heo 			return -ERANGE;
838bd056d7eSTejun Heo 
839bd056d7eSTejun Heo 		tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
840bd056d7eSTejun Heo 		tf->lbal = sect;
841bd056d7eSTejun Heo 		tf->lbam = cyl;
842bd056d7eSTejun Heo 		tf->lbah = cyl >> 8;
843bd056d7eSTejun Heo 		tf->device |= head;
844bd056d7eSTejun Heo 	}
845bd056d7eSTejun Heo 
846bd056d7eSTejun Heo 	return 0;
847bd056d7eSTejun Heo }
848bd056d7eSTejun Heo 
849bd056d7eSTejun Heo /**
850c6fd2807SJeff Garzik  *	ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
851c6fd2807SJeff Garzik  *	@pio_mask: pio_mask
852c6fd2807SJeff Garzik  *	@mwdma_mask: mwdma_mask
853c6fd2807SJeff Garzik  *	@udma_mask: udma_mask
854c6fd2807SJeff Garzik  *
855c6fd2807SJeff Garzik  *	Pack @pio_mask, @mwdma_mask and @udma_mask into a single
856c6fd2807SJeff Garzik  *	unsigned int xfer_mask.
857c6fd2807SJeff Garzik  *
858c6fd2807SJeff Garzik  *	LOCKING:
859c6fd2807SJeff Garzik  *	None.
860c6fd2807SJeff Garzik  *
861c6fd2807SJeff Garzik  *	RETURNS:
862c6fd2807SJeff Garzik  *	Packed xfer_mask.
863c6fd2807SJeff Garzik  */
8647dc951aeSTejun Heo unsigned long ata_pack_xfermask(unsigned long pio_mask,
8657dc951aeSTejun Heo 				unsigned long mwdma_mask,
8667dc951aeSTejun Heo 				unsigned long udma_mask)
867c6fd2807SJeff Garzik {
868c6fd2807SJeff Garzik 	return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
869c6fd2807SJeff Garzik 		((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
870c6fd2807SJeff Garzik 		((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
871c6fd2807SJeff Garzik }
872c6fd2807SJeff Garzik 
873c6fd2807SJeff Garzik /**
874c6fd2807SJeff Garzik  *	ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
875c6fd2807SJeff Garzik  *	@xfer_mask: xfer_mask to unpack
876c6fd2807SJeff Garzik  *	@pio_mask: resulting pio_mask
877c6fd2807SJeff Garzik  *	@mwdma_mask: resulting mwdma_mask
878c6fd2807SJeff Garzik  *	@udma_mask: resulting udma_mask
879c6fd2807SJeff Garzik  *
880c6fd2807SJeff Garzik  *	Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
881c6fd2807SJeff Garzik  *	Any NULL distination masks will be ignored.
882c6fd2807SJeff Garzik  */
8837dc951aeSTejun Heo void ata_unpack_xfermask(unsigned long xfer_mask, unsigned long *pio_mask,
8847dc951aeSTejun Heo 			 unsigned long *mwdma_mask, unsigned long *udma_mask)
885c6fd2807SJeff Garzik {
886c6fd2807SJeff Garzik 	if (pio_mask)
887c6fd2807SJeff Garzik 		*pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
888c6fd2807SJeff Garzik 	if (mwdma_mask)
889c6fd2807SJeff Garzik 		*mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
890c6fd2807SJeff Garzik 	if (udma_mask)
891c6fd2807SJeff Garzik 		*udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
892c6fd2807SJeff Garzik }
893c6fd2807SJeff Garzik 
894c6fd2807SJeff Garzik static const struct ata_xfer_ent {
895c6fd2807SJeff Garzik 	int shift, bits;
896c6fd2807SJeff Garzik 	u8 base;
897c6fd2807SJeff Garzik } ata_xfer_tbl[] = {
89870cd071eSTejun Heo 	{ ATA_SHIFT_PIO, ATA_NR_PIO_MODES, XFER_PIO_0 },
89970cd071eSTejun Heo 	{ ATA_SHIFT_MWDMA, ATA_NR_MWDMA_MODES, XFER_MW_DMA_0 },
90070cd071eSTejun Heo 	{ ATA_SHIFT_UDMA, ATA_NR_UDMA_MODES, XFER_UDMA_0 },
901c6fd2807SJeff Garzik 	{ -1, },
902c6fd2807SJeff Garzik };
903c6fd2807SJeff Garzik 
904c6fd2807SJeff Garzik /**
905c6fd2807SJeff Garzik  *	ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
906c6fd2807SJeff Garzik  *	@xfer_mask: xfer_mask of interest
907c6fd2807SJeff Garzik  *
908c6fd2807SJeff Garzik  *	Return matching XFER_* value for @xfer_mask.  Only the highest
909c6fd2807SJeff Garzik  *	bit of @xfer_mask is considered.
910c6fd2807SJeff Garzik  *
911c6fd2807SJeff Garzik  *	LOCKING:
912c6fd2807SJeff Garzik  *	None.
913c6fd2807SJeff Garzik  *
914c6fd2807SJeff Garzik  *	RETURNS:
91570cd071eSTejun Heo  *	Matching XFER_* value, 0xff if no match found.
916c6fd2807SJeff Garzik  */
9177dc951aeSTejun Heo u8 ata_xfer_mask2mode(unsigned long xfer_mask)
918c6fd2807SJeff Garzik {
919c6fd2807SJeff Garzik 	int highbit = fls(xfer_mask) - 1;
920c6fd2807SJeff Garzik 	const struct ata_xfer_ent *ent;
921c6fd2807SJeff Garzik 
922c6fd2807SJeff Garzik 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
923c6fd2807SJeff Garzik 		if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
924c6fd2807SJeff Garzik 			return ent->base + highbit - ent->shift;
92570cd071eSTejun Heo 	return 0xff;
926c6fd2807SJeff Garzik }
927c6fd2807SJeff Garzik 
928c6fd2807SJeff Garzik /**
929c6fd2807SJeff Garzik  *	ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
930c6fd2807SJeff Garzik  *	@xfer_mode: XFER_* of interest
931c6fd2807SJeff Garzik  *
932c6fd2807SJeff Garzik  *	Return matching xfer_mask for @xfer_mode.
933c6fd2807SJeff Garzik  *
934c6fd2807SJeff Garzik  *	LOCKING:
935c6fd2807SJeff Garzik  *	None.
936c6fd2807SJeff Garzik  *
937c6fd2807SJeff Garzik  *	RETURNS:
938c6fd2807SJeff Garzik  *	Matching xfer_mask, 0 if no match found.
939c6fd2807SJeff Garzik  */
9407dc951aeSTejun Heo unsigned long ata_xfer_mode2mask(u8 xfer_mode)
941c6fd2807SJeff Garzik {
942c6fd2807SJeff Garzik 	const struct ata_xfer_ent *ent;
943c6fd2807SJeff Garzik 
944c6fd2807SJeff Garzik 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
945c6fd2807SJeff Garzik 		if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
94670cd071eSTejun Heo 			return ((2 << (ent->shift + xfer_mode - ent->base)) - 1)
94770cd071eSTejun Heo 				& ~((1 << ent->shift) - 1);
948c6fd2807SJeff Garzik 	return 0;
949c6fd2807SJeff Garzik }
950c6fd2807SJeff Garzik 
951c6fd2807SJeff Garzik /**
952c6fd2807SJeff Garzik  *	ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
953c6fd2807SJeff Garzik  *	@xfer_mode: XFER_* of interest
954c6fd2807SJeff Garzik  *
955c6fd2807SJeff Garzik  *	Return matching xfer_shift for @xfer_mode.
956c6fd2807SJeff Garzik  *
957c6fd2807SJeff Garzik  *	LOCKING:
958c6fd2807SJeff Garzik  *	None.
959c6fd2807SJeff Garzik  *
960c6fd2807SJeff Garzik  *	RETURNS:
961c6fd2807SJeff Garzik  *	Matching xfer_shift, -1 if no match found.
962c6fd2807SJeff Garzik  */
9637dc951aeSTejun Heo int ata_xfer_mode2shift(unsigned long xfer_mode)
964c6fd2807SJeff Garzik {
965c6fd2807SJeff Garzik 	const struct ata_xfer_ent *ent;
966c6fd2807SJeff Garzik 
967c6fd2807SJeff Garzik 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
968c6fd2807SJeff Garzik 		if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
969c6fd2807SJeff Garzik 			return ent->shift;
970c6fd2807SJeff Garzik 	return -1;
971c6fd2807SJeff Garzik }
972c6fd2807SJeff Garzik 
973c6fd2807SJeff Garzik /**
974c6fd2807SJeff Garzik  *	ata_mode_string - convert xfer_mask to string
975c6fd2807SJeff Garzik  *	@xfer_mask: mask of bits supported; only highest bit counts.
976c6fd2807SJeff Garzik  *
977c6fd2807SJeff Garzik  *	Determine string which represents the highest speed
978c6fd2807SJeff Garzik  *	(highest bit in @modemask).
979c6fd2807SJeff Garzik  *
980c6fd2807SJeff Garzik  *	LOCKING:
981c6fd2807SJeff Garzik  *	None.
982c6fd2807SJeff Garzik  *
983c6fd2807SJeff Garzik  *	RETURNS:
984c6fd2807SJeff Garzik  *	Constant C string representing highest speed listed in
985c6fd2807SJeff Garzik  *	@mode_mask, or the constant C string "<n/a>".
986c6fd2807SJeff Garzik  */
9877dc951aeSTejun Heo const char *ata_mode_string(unsigned long xfer_mask)
988c6fd2807SJeff Garzik {
989c6fd2807SJeff Garzik 	static const char * const xfer_mode_str[] = {
990c6fd2807SJeff Garzik 		"PIO0",
991c6fd2807SJeff Garzik 		"PIO1",
992c6fd2807SJeff Garzik 		"PIO2",
993c6fd2807SJeff Garzik 		"PIO3",
994c6fd2807SJeff Garzik 		"PIO4",
995b352e57dSAlan Cox 		"PIO5",
996b352e57dSAlan Cox 		"PIO6",
997c6fd2807SJeff Garzik 		"MWDMA0",
998c6fd2807SJeff Garzik 		"MWDMA1",
999c6fd2807SJeff Garzik 		"MWDMA2",
1000b352e57dSAlan Cox 		"MWDMA3",
1001b352e57dSAlan Cox 		"MWDMA4",
1002c6fd2807SJeff Garzik 		"UDMA/16",
1003c6fd2807SJeff Garzik 		"UDMA/25",
1004c6fd2807SJeff Garzik 		"UDMA/33",
1005c6fd2807SJeff Garzik 		"UDMA/44",
1006c6fd2807SJeff Garzik 		"UDMA/66",
1007c6fd2807SJeff Garzik 		"UDMA/100",
1008c6fd2807SJeff Garzik 		"UDMA/133",
1009c6fd2807SJeff Garzik 		"UDMA7",
1010c6fd2807SJeff Garzik 	};
1011c6fd2807SJeff Garzik 	int highbit;
1012c6fd2807SJeff Garzik 
1013c6fd2807SJeff Garzik 	highbit = fls(xfer_mask) - 1;
1014c6fd2807SJeff Garzik 	if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
1015c6fd2807SJeff Garzik 		return xfer_mode_str[highbit];
1016c6fd2807SJeff Garzik 	return "<n/a>";
1017c6fd2807SJeff Garzik }
1018c6fd2807SJeff Garzik 
1019d9027470SGwendal Grignou const char *sata_spd_string(unsigned int spd)
1020c6fd2807SJeff Garzik {
1021c6fd2807SJeff Garzik 	static const char * const spd_str[] = {
1022c6fd2807SJeff Garzik 		"1.5 Gbps",
1023c6fd2807SJeff Garzik 		"3.0 Gbps",
10248522ee25SShane Huang 		"6.0 Gbps",
1025c6fd2807SJeff Garzik 	};
1026c6fd2807SJeff Garzik 
1027c6fd2807SJeff Garzik 	if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
1028c6fd2807SJeff Garzik 		return "<unknown>";
1029c6fd2807SJeff Garzik 	return spd_str[spd - 1];
1030c6fd2807SJeff Garzik }
1031c6fd2807SJeff Garzik 
1032c6fd2807SJeff Garzik /**
1033c6fd2807SJeff Garzik  *	ata_dev_classify - determine device type based on ATA-spec signature
1034c6fd2807SJeff Garzik  *	@tf: ATA taskfile register set for device to be identified
1035c6fd2807SJeff Garzik  *
1036c6fd2807SJeff Garzik  *	Determine from taskfile register contents whether a device is
1037c6fd2807SJeff Garzik  *	ATA or ATAPI, as per "Signature and persistence" section
1038c6fd2807SJeff Garzik  *	of ATA/PI spec (volume 1, sect 5.14).
1039c6fd2807SJeff Garzik  *
1040c6fd2807SJeff Garzik  *	LOCKING:
1041c6fd2807SJeff Garzik  *	None.
1042c6fd2807SJeff Garzik  *
1043c6fd2807SJeff Garzik  *	RETURNS:
1044633273a3STejun Heo  *	Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP or
1045633273a3STejun Heo  *	%ATA_DEV_UNKNOWN the event of failure.
1046c6fd2807SJeff Garzik  */
1047c6fd2807SJeff Garzik unsigned int ata_dev_classify(const struct ata_taskfile *tf)
1048c6fd2807SJeff Garzik {
1049c6fd2807SJeff Garzik 	/* Apple's open source Darwin code hints that some devices only
1050c6fd2807SJeff Garzik 	 * put a proper signature into the LBA mid/high registers,
1051c6fd2807SJeff Garzik 	 * So, we only check those.  It's sufficient for uniqueness.
1052633273a3STejun Heo 	 *
1053633273a3STejun Heo 	 * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate
1054633273a3STejun Heo 	 * signatures for ATA and ATAPI devices attached on SerialATA,
1055633273a3STejun Heo 	 * 0x3c/0xc3 and 0x69/0x96 respectively.  However, SerialATA
1056633273a3STejun Heo 	 * spec has never mentioned about using different signatures
1057633273a3STejun Heo 	 * for ATA/ATAPI devices.  Then, Serial ATA II: Port
1058633273a3STejun Heo 	 * Multiplier specification began to use 0x69/0x96 to identify
1059633273a3STejun Heo 	 * port multpliers and 0x3c/0xc3 to identify SEMB device.
1060633273a3STejun Heo 	 * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and
1061633273a3STejun Heo 	 * 0x69/0x96 shortly and described them as reserved for
1062633273a3STejun Heo 	 * SerialATA.
1063633273a3STejun Heo 	 *
1064633273a3STejun Heo 	 * We follow the current spec and consider that 0x69/0x96
1065633273a3STejun Heo 	 * identifies a port multiplier and 0x3c/0xc3 a SEMB device.
106679b42babSTejun Heo 	 * Unfortunately, WDC WD1600JS-62MHB5 (a hard drive) reports
106779b42babSTejun Heo 	 * SEMB signature.  This is worked around in
106879b42babSTejun Heo 	 * ata_dev_read_id().
1069c6fd2807SJeff Garzik 	 */
1070633273a3STejun Heo 	if ((tf->lbam == 0) && (tf->lbah == 0)) {
1071c6fd2807SJeff Garzik 		DPRINTK("found ATA device by sig\n");
1072c6fd2807SJeff Garzik 		return ATA_DEV_ATA;
1073c6fd2807SJeff Garzik 	}
1074c6fd2807SJeff Garzik 
1075633273a3STejun Heo 	if ((tf->lbam == 0x14) && (tf->lbah == 0xeb)) {
1076c6fd2807SJeff Garzik 		DPRINTK("found ATAPI device by sig\n");
1077c6fd2807SJeff Garzik 		return ATA_DEV_ATAPI;
1078c6fd2807SJeff Garzik 	}
1079c6fd2807SJeff Garzik 
1080633273a3STejun Heo 	if ((tf->lbam == 0x69) && (tf->lbah == 0x96)) {
1081633273a3STejun Heo 		DPRINTK("found PMP device by sig\n");
1082633273a3STejun Heo 		return ATA_DEV_PMP;
1083633273a3STejun Heo 	}
1084633273a3STejun Heo 
1085633273a3STejun Heo 	if ((tf->lbam == 0x3c) && (tf->lbah == 0xc3)) {
108679b42babSTejun Heo 		DPRINTK("found SEMB device by sig (could be ATA device)\n");
108779b42babSTejun Heo 		return ATA_DEV_SEMB;
1088633273a3STejun Heo 	}
1089633273a3STejun Heo 
1090c6fd2807SJeff Garzik 	DPRINTK("unknown device\n");
1091c6fd2807SJeff Garzik 	return ATA_DEV_UNKNOWN;
1092c6fd2807SJeff Garzik }
1093c6fd2807SJeff Garzik 
1094c6fd2807SJeff Garzik /**
1095c6fd2807SJeff Garzik  *	ata_id_string - Convert IDENTIFY DEVICE page into string
1096c6fd2807SJeff Garzik  *	@id: IDENTIFY DEVICE results we will examine
1097c6fd2807SJeff Garzik  *	@s: string into which data is output
1098c6fd2807SJeff Garzik  *	@ofs: offset into identify device page
1099c6fd2807SJeff Garzik  *	@len: length of string to return. must be an even number.
1100c6fd2807SJeff Garzik  *
1101c6fd2807SJeff Garzik  *	The strings in the IDENTIFY DEVICE page are broken up into
1102c6fd2807SJeff Garzik  *	16-bit chunks.  Run through the string, and output each
1103c6fd2807SJeff Garzik  *	8-bit chunk linearly, regardless of platform.
1104c6fd2807SJeff Garzik  *
1105c6fd2807SJeff Garzik  *	LOCKING:
1106c6fd2807SJeff Garzik  *	caller.
1107c6fd2807SJeff Garzik  */
1108c6fd2807SJeff Garzik 
1109c6fd2807SJeff Garzik void ata_id_string(const u16 *id, unsigned char *s,
1110c6fd2807SJeff Garzik 		   unsigned int ofs, unsigned int len)
1111c6fd2807SJeff Garzik {
1112c6fd2807SJeff Garzik 	unsigned int c;
1113c6fd2807SJeff Garzik 
1114963e4975SAlan Cox 	BUG_ON(len & 1);
1115963e4975SAlan Cox 
1116c6fd2807SJeff Garzik 	while (len > 0) {
1117c6fd2807SJeff Garzik 		c = id[ofs] >> 8;
1118c6fd2807SJeff Garzik 		*s = c;
1119c6fd2807SJeff Garzik 		s++;
1120c6fd2807SJeff Garzik 
1121c6fd2807SJeff Garzik 		c = id[ofs] & 0xff;
1122c6fd2807SJeff Garzik 		*s = c;
1123c6fd2807SJeff Garzik 		s++;
1124c6fd2807SJeff Garzik 
1125c6fd2807SJeff Garzik 		ofs++;
1126c6fd2807SJeff Garzik 		len -= 2;
1127c6fd2807SJeff Garzik 	}
1128c6fd2807SJeff Garzik }
1129c6fd2807SJeff Garzik 
1130c6fd2807SJeff Garzik /**
1131c6fd2807SJeff Garzik  *	ata_id_c_string - Convert IDENTIFY DEVICE page into C string
1132c6fd2807SJeff Garzik  *	@id: IDENTIFY DEVICE results we will examine
1133c6fd2807SJeff Garzik  *	@s: string into which data is output
1134c6fd2807SJeff Garzik  *	@ofs: offset into identify device page
1135c6fd2807SJeff Garzik  *	@len: length of string to return. must be an odd number.
1136c6fd2807SJeff Garzik  *
1137c6fd2807SJeff Garzik  *	This function is identical to ata_id_string except that it
1138c6fd2807SJeff Garzik  *	trims trailing spaces and terminates the resulting string with
1139c6fd2807SJeff Garzik  *	null.  @len must be actual maximum length (even number) + 1.
1140c6fd2807SJeff Garzik  *
1141c6fd2807SJeff Garzik  *	LOCKING:
1142c6fd2807SJeff Garzik  *	caller.
1143c6fd2807SJeff Garzik  */
1144c6fd2807SJeff Garzik void ata_id_c_string(const u16 *id, unsigned char *s,
1145c6fd2807SJeff Garzik 		     unsigned int ofs, unsigned int len)
1146c6fd2807SJeff Garzik {
1147c6fd2807SJeff Garzik 	unsigned char *p;
1148c6fd2807SJeff Garzik 
1149c6fd2807SJeff Garzik 	ata_id_string(id, s, ofs, len - 1);
1150c6fd2807SJeff Garzik 
1151c6fd2807SJeff Garzik 	p = s + strnlen(s, len - 1);
1152c6fd2807SJeff Garzik 	while (p > s && p[-1] == ' ')
1153c6fd2807SJeff Garzik 		p--;
1154c6fd2807SJeff Garzik 	*p = '\0';
1155c6fd2807SJeff Garzik }
1156c6fd2807SJeff Garzik 
1157db6f8759STejun Heo static u64 ata_id_n_sectors(const u16 *id)
1158db6f8759STejun Heo {
1159db6f8759STejun Heo 	if (ata_id_has_lba(id)) {
1160db6f8759STejun Heo 		if (ata_id_has_lba48(id))
1161968e594aSRobert Hancock 			return ata_id_u64(id, ATA_ID_LBA_CAPACITY_2);
1162db6f8759STejun Heo 		else
1163968e594aSRobert Hancock 			return ata_id_u32(id, ATA_ID_LBA_CAPACITY);
1164db6f8759STejun Heo 	} else {
1165db6f8759STejun Heo 		if (ata_id_current_chs_valid(id))
1166968e594aSRobert Hancock 			return id[ATA_ID_CUR_CYLS] * id[ATA_ID_CUR_HEADS] *
1167968e594aSRobert Hancock 			       id[ATA_ID_CUR_SECTORS];
1168db6f8759STejun Heo 		else
1169968e594aSRobert Hancock 			return id[ATA_ID_CYLS] * id[ATA_ID_HEADS] *
1170968e594aSRobert Hancock 			       id[ATA_ID_SECTORS];
1171db6f8759STejun Heo 	}
1172db6f8759STejun Heo }
1173db6f8759STejun Heo 
1174a5987e0aSTejun Heo u64 ata_tf_to_lba48(const struct ata_taskfile *tf)
11751e999736SAlan Cox {
11761e999736SAlan Cox 	u64 sectors = 0;
11771e999736SAlan Cox 
11781e999736SAlan Cox 	sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40;
11791e999736SAlan Cox 	sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32;
1180ba14a9c2SRoland Dreier 	sectors |= ((u64)(tf->hob_lbal & 0xff)) << 24;
11811e999736SAlan Cox 	sectors |= (tf->lbah & 0xff) << 16;
11821e999736SAlan Cox 	sectors |= (tf->lbam & 0xff) << 8;
11831e999736SAlan Cox 	sectors |= (tf->lbal & 0xff);
11841e999736SAlan Cox 
1185a5987e0aSTejun Heo 	return sectors;
11861e999736SAlan Cox }
11871e999736SAlan Cox 
1188a5987e0aSTejun Heo u64 ata_tf_to_lba(const struct ata_taskfile *tf)
11891e999736SAlan Cox {
11901e999736SAlan Cox 	u64 sectors = 0;
11911e999736SAlan Cox 
11921e999736SAlan Cox 	sectors |= (tf->device & 0x0f) << 24;
11931e999736SAlan Cox 	sectors |= (tf->lbah & 0xff) << 16;
11941e999736SAlan Cox 	sectors |= (tf->lbam & 0xff) << 8;
11951e999736SAlan Cox 	sectors |= (tf->lbal & 0xff);
11961e999736SAlan Cox 
1197a5987e0aSTejun Heo 	return sectors;
11981e999736SAlan Cox }
11991e999736SAlan Cox 
12001e999736SAlan Cox /**
1201c728a914STejun Heo  *	ata_read_native_max_address - Read native max address
1202c728a914STejun Heo  *	@dev: target device
1203c728a914STejun Heo  *	@max_sectors: out parameter for the result native max address
12041e999736SAlan Cox  *
1205c728a914STejun Heo  *	Perform an LBA48 or LBA28 native size query upon the device in
1206c728a914STejun Heo  *	question.
1207c728a914STejun Heo  *
1208c728a914STejun Heo  *	RETURNS:
1209c728a914STejun Heo  *	0 on success, -EACCES if command is aborted by the drive.
1210c728a914STejun Heo  *	-EIO on other errors.
12111e999736SAlan Cox  */
1212c728a914STejun Heo static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
12131e999736SAlan Cox {
1214c728a914STejun Heo 	unsigned int err_mask;
12151e999736SAlan Cox 	struct ata_taskfile tf;
1216c728a914STejun Heo 	int lba48 = ata_id_has_lba48(dev->id);
12171e999736SAlan Cox 
12181e999736SAlan Cox 	ata_tf_init(dev, &tf);
12191e999736SAlan Cox 
1220c728a914STejun Heo 	/* always clear all address registers */
12211e999736SAlan Cox 	tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1222c728a914STejun Heo 
1223c728a914STejun Heo 	if (lba48) {
1224c728a914STejun Heo 		tf.command = ATA_CMD_READ_NATIVE_MAX_EXT;
1225c728a914STejun Heo 		tf.flags |= ATA_TFLAG_LBA48;
1226c728a914STejun Heo 	} else
1227c728a914STejun Heo 		tf.command = ATA_CMD_READ_NATIVE_MAX;
1228c728a914STejun Heo 
12291e999736SAlan Cox 	tf.protocol |= ATA_PROT_NODATA;
1230c728a914STejun Heo 	tf.device |= ATA_LBA;
12311e999736SAlan Cox 
12322b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1233c728a914STejun Heo 	if (err_mask) {
1234a9a79dfeSJoe Perches 		ata_dev_warn(dev,
1235a9a79dfeSJoe Perches 			     "failed to read native max address (err_mask=0x%x)\n",
1236a9a79dfeSJoe Perches 			     err_mask);
1237c728a914STejun Heo 		if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
1238c728a914STejun Heo 			return -EACCES;
1239c728a914STejun Heo 		return -EIO;
1240c728a914STejun Heo 	}
1241c728a914STejun Heo 
1242c728a914STejun Heo 	if (lba48)
1243a5987e0aSTejun Heo 		*max_sectors = ata_tf_to_lba48(&tf) + 1;
1244c728a914STejun Heo 	else
1245a5987e0aSTejun Heo 		*max_sectors = ata_tf_to_lba(&tf) + 1;
124693328e11SAlan Cox 	if (dev->horkage & ATA_HORKAGE_HPA_SIZE)
124793328e11SAlan Cox 		(*max_sectors)--;
12481e999736SAlan Cox 	return 0;
12491e999736SAlan Cox }
12501e999736SAlan Cox 
12511e999736SAlan Cox /**
1252c728a914STejun Heo  *	ata_set_max_sectors - Set max sectors
1253c728a914STejun Heo  *	@dev: target device
12546b38d1d1SRandy Dunlap  *	@new_sectors: new max sectors value to set for the device
12551e999736SAlan Cox  *
1256c728a914STejun Heo  *	Set max sectors of @dev to @new_sectors.
1257c728a914STejun Heo  *
1258c728a914STejun Heo  *	RETURNS:
1259c728a914STejun Heo  *	0 on success, -EACCES if command is aborted or denied (due to
1260c728a914STejun Heo  *	previous non-volatile SET_MAX) by the drive.  -EIO on other
1261c728a914STejun Heo  *	errors.
12621e999736SAlan Cox  */
126305027adcSTejun Heo static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
12641e999736SAlan Cox {
1265c728a914STejun Heo 	unsigned int err_mask;
12661e999736SAlan Cox 	struct ata_taskfile tf;
1267c728a914STejun Heo 	int lba48 = ata_id_has_lba48(dev->id);
12681e999736SAlan Cox 
12691e999736SAlan Cox 	new_sectors--;
12701e999736SAlan Cox 
12711e999736SAlan Cox 	ata_tf_init(dev, &tf);
12721e999736SAlan Cox 
1273c728a914STejun Heo 	tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
12741e999736SAlan Cox 
1275c728a914STejun Heo 	if (lba48) {
1276c728a914STejun Heo 		tf.command = ATA_CMD_SET_MAX_EXT;
1277c728a914STejun Heo 		tf.flags |= ATA_TFLAG_LBA48;
12781e999736SAlan Cox 
12791e999736SAlan Cox 		tf.hob_lbal = (new_sectors >> 24) & 0xff;
12801e999736SAlan Cox 		tf.hob_lbam = (new_sectors >> 32) & 0xff;
12811e999736SAlan Cox 		tf.hob_lbah = (new_sectors >> 40) & 0xff;
12821e582ba4STejun Heo 	} else {
12831e999736SAlan Cox 		tf.command = ATA_CMD_SET_MAX;
1284c728a914STejun Heo 
12851e582ba4STejun Heo 		tf.device |= (new_sectors >> 24) & 0xf;
12861e582ba4STejun Heo 	}
12871e582ba4STejun Heo 
12881e999736SAlan Cox 	tf.protocol |= ATA_PROT_NODATA;
1289c728a914STejun Heo 	tf.device |= ATA_LBA;
12901e999736SAlan Cox 
12911e999736SAlan Cox 	tf.lbal = (new_sectors >> 0) & 0xff;
12921e999736SAlan Cox 	tf.lbam = (new_sectors >> 8) & 0xff;
12931e999736SAlan Cox 	tf.lbah = (new_sectors >> 16) & 0xff;
12941e999736SAlan Cox 
12952b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1296c728a914STejun Heo 	if (err_mask) {
1297a9a79dfeSJoe Perches 		ata_dev_warn(dev,
1298a9a79dfeSJoe Perches 			     "failed to set max address (err_mask=0x%x)\n",
1299a9a79dfeSJoe Perches 			     err_mask);
1300c728a914STejun Heo 		if (err_mask == AC_ERR_DEV &&
1301c728a914STejun Heo 		    (tf.feature & (ATA_ABORTED | ATA_IDNF)))
1302c728a914STejun Heo 			return -EACCES;
1303c728a914STejun Heo 		return -EIO;
1304c728a914STejun Heo 	}
1305c728a914STejun Heo 
13061e999736SAlan Cox 	return 0;
13071e999736SAlan Cox }
13081e999736SAlan Cox 
13091e999736SAlan Cox /**
13101e999736SAlan Cox  *	ata_hpa_resize		-	Resize a device with an HPA set
13111e999736SAlan Cox  *	@dev: Device to resize
13121e999736SAlan Cox  *
13131e999736SAlan Cox  *	Read the size of an LBA28 or LBA48 disk with HPA features and resize
13141e999736SAlan Cox  *	it if required to the full size of the media. The caller must check
13151e999736SAlan Cox  *	the drive has the HPA feature set enabled.
131605027adcSTejun Heo  *
131705027adcSTejun Heo  *	RETURNS:
131805027adcSTejun Heo  *	0 on success, -errno on failure.
13191e999736SAlan Cox  */
132005027adcSTejun Heo static int ata_hpa_resize(struct ata_device *dev)
13211e999736SAlan Cox {
132205027adcSTejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
132305027adcSTejun Heo 	int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
1324445d211bSTejun Heo 	bool unlock_hpa = ata_ignore_hpa || dev->flags & ATA_DFLAG_UNLOCK_HPA;
132505027adcSTejun Heo 	u64 sectors = ata_id_n_sectors(dev->id);
132605027adcSTejun Heo 	u64 native_sectors;
1327c728a914STejun Heo 	int rc;
13281e999736SAlan Cox 
132905027adcSTejun Heo 	/* do we need to do it? */
133005027adcSTejun Heo 	if (dev->class != ATA_DEV_ATA ||
133105027adcSTejun Heo 	    !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) ||
133205027adcSTejun Heo 	    (dev->horkage & ATA_HORKAGE_BROKEN_HPA))
1333c728a914STejun Heo 		return 0;
13341e999736SAlan Cox 
133505027adcSTejun Heo 	/* read native max address */
133605027adcSTejun Heo 	rc = ata_read_native_max_address(dev, &native_sectors);
133705027adcSTejun Heo 	if (rc) {
1338dda7aba1STejun Heo 		/* If device aborted the command or HPA isn't going to
1339dda7aba1STejun Heo 		 * be unlocked, skip HPA resizing.
134005027adcSTejun Heo 		 */
1341445d211bSTejun Heo 		if (rc == -EACCES || !unlock_hpa) {
1342a9a79dfeSJoe Perches 			ata_dev_warn(dev,
1343a9a79dfeSJoe Perches 				     "HPA support seems broken, skipping HPA handling\n");
134405027adcSTejun Heo 			dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
134505027adcSTejun Heo 
134605027adcSTejun Heo 			/* we can continue if device aborted the command */
134705027adcSTejun Heo 			if (rc == -EACCES)
134805027adcSTejun Heo 				rc = 0;
134905027adcSTejun Heo 		}
135005027adcSTejun Heo 
135105027adcSTejun Heo 		return rc;
135205027adcSTejun Heo 	}
13535920dadfSTejun Heo 	dev->n_native_sectors = native_sectors;
135405027adcSTejun Heo 
135505027adcSTejun Heo 	/* nothing to do? */
1356445d211bSTejun Heo 	if (native_sectors <= sectors || !unlock_hpa) {
135705027adcSTejun Heo 		if (!print_info || native_sectors == sectors)
135805027adcSTejun Heo 			return 0;
135905027adcSTejun Heo 
136005027adcSTejun Heo 		if (native_sectors > sectors)
1361a9a79dfeSJoe Perches 			ata_dev_info(dev,
136205027adcSTejun Heo 				"HPA detected: current %llu, native %llu\n",
136305027adcSTejun Heo 				(unsigned long long)sectors,
136405027adcSTejun Heo 				(unsigned long long)native_sectors);
136505027adcSTejun Heo 		else if (native_sectors < sectors)
1366a9a79dfeSJoe Perches 			ata_dev_warn(dev,
1367a9a79dfeSJoe Perches 				"native sectors (%llu) is smaller than sectors (%llu)\n",
136805027adcSTejun Heo 				(unsigned long long)native_sectors,
136905027adcSTejun Heo 				(unsigned long long)sectors);
137005027adcSTejun Heo 		return 0;
13711e999736SAlan Cox 	}
137237301a55STejun Heo 
137305027adcSTejun Heo 	/* let's unlock HPA */
137405027adcSTejun Heo 	rc = ata_set_max_sectors(dev, native_sectors);
137505027adcSTejun Heo 	if (rc == -EACCES) {
137605027adcSTejun Heo 		/* if device aborted the command, skip HPA resizing */
1377a9a79dfeSJoe Perches 		ata_dev_warn(dev,
1378a9a79dfeSJoe Perches 			     "device aborted resize (%llu -> %llu), skipping HPA handling\n",
137905027adcSTejun Heo 			     (unsigned long long)sectors,
138005027adcSTejun Heo 			     (unsigned long long)native_sectors);
138105027adcSTejun Heo 		dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
138205027adcSTejun Heo 		return 0;
138305027adcSTejun Heo 	} else if (rc)
138405027adcSTejun Heo 		return rc;
138505027adcSTejun Heo 
138605027adcSTejun Heo 	/* re-read IDENTIFY data */
138705027adcSTejun Heo 	rc = ata_dev_reread_id(dev, 0);
138805027adcSTejun Heo 	if (rc) {
1389a9a79dfeSJoe Perches 		ata_dev_err(dev,
1390a9a79dfeSJoe Perches 			    "failed to re-read IDENTIFY data after HPA resizing\n");
139105027adcSTejun Heo 		return rc;
139205027adcSTejun Heo 	}
139305027adcSTejun Heo 
139405027adcSTejun Heo 	if (print_info) {
139505027adcSTejun Heo 		u64 new_sectors = ata_id_n_sectors(dev->id);
1396a9a79dfeSJoe Perches 		ata_dev_info(dev,
139705027adcSTejun Heo 			"HPA unlocked: %llu -> %llu, native %llu\n",
139805027adcSTejun Heo 			(unsigned long long)sectors,
139905027adcSTejun Heo 			(unsigned long long)new_sectors,
140005027adcSTejun Heo 			(unsigned long long)native_sectors);
140105027adcSTejun Heo 	}
140205027adcSTejun Heo 
140305027adcSTejun Heo 	return 0;
14041e999736SAlan Cox }
14051e999736SAlan Cox 
1406c6fd2807SJeff Garzik /**
1407c6fd2807SJeff Garzik  *	ata_dump_id - IDENTIFY DEVICE info debugging output
1408c6fd2807SJeff Garzik  *	@id: IDENTIFY DEVICE page to dump
1409c6fd2807SJeff Garzik  *
1410c6fd2807SJeff Garzik  *	Dump selected 16-bit words from the given IDENTIFY DEVICE
1411c6fd2807SJeff Garzik  *	page.
1412c6fd2807SJeff Garzik  *
1413c6fd2807SJeff Garzik  *	LOCKING:
1414c6fd2807SJeff Garzik  *	caller.
1415c6fd2807SJeff Garzik  */
1416c6fd2807SJeff Garzik 
1417c6fd2807SJeff Garzik static inline void ata_dump_id(const u16 *id)
1418c6fd2807SJeff Garzik {
1419c6fd2807SJeff Garzik 	DPRINTK("49==0x%04x  "
1420c6fd2807SJeff Garzik 		"53==0x%04x  "
1421c6fd2807SJeff Garzik 		"63==0x%04x  "
1422c6fd2807SJeff Garzik 		"64==0x%04x  "
1423c6fd2807SJeff Garzik 		"75==0x%04x  \n",
1424c6fd2807SJeff Garzik 		id[49],
1425c6fd2807SJeff Garzik 		id[53],
1426c6fd2807SJeff Garzik 		id[63],
1427c6fd2807SJeff Garzik 		id[64],
1428c6fd2807SJeff Garzik 		id[75]);
1429c6fd2807SJeff Garzik 	DPRINTK("80==0x%04x  "
1430c6fd2807SJeff Garzik 		"81==0x%04x  "
1431c6fd2807SJeff Garzik 		"82==0x%04x  "
1432c6fd2807SJeff Garzik 		"83==0x%04x  "
1433c6fd2807SJeff Garzik 		"84==0x%04x  \n",
1434c6fd2807SJeff Garzik 		id[80],
1435c6fd2807SJeff Garzik 		id[81],
1436c6fd2807SJeff Garzik 		id[82],
1437c6fd2807SJeff Garzik 		id[83],
1438c6fd2807SJeff Garzik 		id[84]);
1439c6fd2807SJeff Garzik 	DPRINTK("88==0x%04x  "
1440c6fd2807SJeff Garzik 		"93==0x%04x\n",
1441c6fd2807SJeff Garzik 		id[88],
1442c6fd2807SJeff Garzik 		id[93]);
1443c6fd2807SJeff Garzik }
1444c6fd2807SJeff Garzik 
1445c6fd2807SJeff Garzik /**
1446c6fd2807SJeff Garzik  *	ata_id_xfermask - Compute xfermask from the given IDENTIFY data
1447c6fd2807SJeff Garzik  *	@id: IDENTIFY data to compute xfer mask from
1448c6fd2807SJeff Garzik  *
1449c6fd2807SJeff Garzik  *	Compute the xfermask for this device. This is not as trivial
1450c6fd2807SJeff Garzik  *	as it seems if we must consider early devices correctly.
1451c6fd2807SJeff Garzik  *
1452c6fd2807SJeff Garzik  *	FIXME: pre IDE drive timing (do we care ?).
1453c6fd2807SJeff Garzik  *
1454c6fd2807SJeff Garzik  *	LOCKING:
1455c6fd2807SJeff Garzik  *	None.
1456c6fd2807SJeff Garzik  *
1457c6fd2807SJeff Garzik  *	RETURNS:
1458c6fd2807SJeff Garzik  *	Computed xfermask
1459c6fd2807SJeff Garzik  */
14607dc951aeSTejun Heo unsigned long ata_id_xfermask(const u16 *id)
1461c6fd2807SJeff Garzik {
14627dc951aeSTejun Heo 	unsigned long pio_mask, mwdma_mask, udma_mask;
1463c6fd2807SJeff Garzik 
1464c6fd2807SJeff Garzik 	/* Usual case. Word 53 indicates word 64 is valid */
1465c6fd2807SJeff Garzik 	if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
1466c6fd2807SJeff Garzik 		pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
1467c6fd2807SJeff Garzik 		pio_mask <<= 3;
1468c6fd2807SJeff Garzik 		pio_mask |= 0x7;
1469c6fd2807SJeff Garzik 	} else {
1470c6fd2807SJeff Garzik 		/* If word 64 isn't valid then Word 51 high byte holds
1471c6fd2807SJeff Garzik 		 * the PIO timing number for the maximum. Turn it into
1472c6fd2807SJeff Garzik 		 * a mask.
1473c6fd2807SJeff Garzik 		 */
14747a0f1c8aSLennert Buytenhek 		u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
147546767aebSAlan Cox 		if (mode < 5)	/* Valid PIO range */
147646767aebSAlan Cox 			pio_mask = (2 << mode) - 1;
147746767aebSAlan Cox 		else
147846767aebSAlan Cox 			pio_mask = 1;
1479c6fd2807SJeff Garzik 
1480c6fd2807SJeff Garzik 		/* But wait.. there's more. Design your standards by
1481c6fd2807SJeff Garzik 		 * committee and you too can get a free iordy field to
1482c6fd2807SJeff Garzik 		 * process. However its the speeds not the modes that
1483c6fd2807SJeff Garzik 		 * are supported... Note drivers using the timing API
1484c6fd2807SJeff Garzik 		 * will get this right anyway
1485c6fd2807SJeff Garzik 		 */
1486c6fd2807SJeff Garzik 	}
1487c6fd2807SJeff Garzik 
1488c6fd2807SJeff Garzik 	mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
1489c6fd2807SJeff Garzik 
1490b352e57dSAlan Cox 	if (ata_id_is_cfa(id)) {
1491b352e57dSAlan Cox 		/*
1492b352e57dSAlan Cox 		 *	Process compact flash extended modes
1493b352e57dSAlan Cox 		 */
149462afe5d7SSergei Shtylyov 		int pio = (id[ATA_ID_CFA_MODES] >> 0) & 0x7;
149562afe5d7SSergei Shtylyov 		int dma = (id[ATA_ID_CFA_MODES] >> 3) & 0x7;
1496b352e57dSAlan Cox 
1497b352e57dSAlan Cox 		if (pio)
1498b352e57dSAlan Cox 			pio_mask |= (1 << 5);
1499b352e57dSAlan Cox 		if (pio > 1)
1500b352e57dSAlan Cox 			pio_mask |= (1 << 6);
1501b352e57dSAlan Cox 		if (dma)
1502b352e57dSAlan Cox 			mwdma_mask |= (1 << 3);
1503b352e57dSAlan Cox 		if (dma > 1)
1504b352e57dSAlan Cox 			mwdma_mask |= (1 << 4);
1505b352e57dSAlan Cox 	}
1506b352e57dSAlan Cox 
1507c6fd2807SJeff Garzik 	udma_mask = 0;
1508c6fd2807SJeff Garzik 	if (id[ATA_ID_FIELD_VALID] & (1 << 2))
1509c6fd2807SJeff Garzik 		udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
1510c6fd2807SJeff Garzik 
1511c6fd2807SJeff Garzik 	return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
1512c6fd2807SJeff Garzik }
1513c6fd2807SJeff Garzik 
15147102d230SAdrian Bunk static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
1515c6fd2807SJeff Garzik {
1516c6fd2807SJeff Garzik 	struct completion *waiting = qc->private_data;
1517c6fd2807SJeff Garzik 
1518c6fd2807SJeff Garzik 	complete(waiting);
1519c6fd2807SJeff Garzik }
1520c6fd2807SJeff Garzik 
1521c6fd2807SJeff Garzik /**
15222432697bSTejun Heo  *	ata_exec_internal_sg - execute libata internal command
1523c6fd2807SJeff Garzik  *	@dev: Device to which the command is sent
1524c6fd2807SJeff Garzik  *	@tf: Taskfile registers for the command and the result
1525c6fd2807SJeff Garzik  *	@cdb: CDB for packet command
1526c6fd2807SJeff Garzik  *	@dma_dir: Data tranfer direction of the command
15275c1ad8b3SRandy Dunlap  *	@sgl: sg list for the data buffer of the command
15282432697bSTejun Heo  *	@n_elem: Number of sg entries
15292b789108STejun Heo  *	@timeout: Timeout in msecs (0 for default)
1530c6fd2807SJeff Garzik  *
1531c6fd2807SJeff Garzik  *	Executes libata internal command with timeout.  @tf contains
1532c6fd2807SJeff Garzik  *	command on entry and result on return.  Timeout and error
1533c6fd2807SJeff Garzik  *	conditions are reported via return value.  No recovery action
1534c6fd2807SJeff Garzik  *	is taken after a command times out.  It's caller's duty to
1535c6fd2807SJeff Garzik  *	clean up after timeout.
1536c6fd2807SJeff Garzik  *
1537c6fd2807SJeff Garzik  *	LOCKING:
1538c6fd2807SJeff Garzik  *	None.  Should be called with kernel context, might sleep.
1539c6fd2807SJeff Garzik  *
1540c6fd2807SJeff Garzik  *	RETURNS:
1541c6fd2807SJeff Garzik  *	Zero on success, AC_ERR_* mask on failure
1542c6fd2807SJeff Garzik  */
15432432697bSTejun Heo unsigned ata_exec_internal_sg(struct ata_device *dev,
1544c6fd2807SJeff Garzik 			      struct ata_taskfile *tf, const u8 *cdb,
154587260216SJens Axboe 			      int dma_dir, struct scatterlist *sgl,
15462b789108STejun Heo 			      unsigned int n_elem, unsigned long timeout)
1547c6fd2807SJeff Garzik {
15489af5c9c9STejun Heo 	struct ata_link *link = dev->link;
15499af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
1550c6fd2807SJeff Garzik 	u8 command = tf->command;
155187fbc5a0STejun Heo 	int auto_timeout = 0;
1552c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
1553c6fd2807SJeff Garzik 	unsigned int tag, preempted_tag;
1554c6fd2807SJeff Garzik 	u32 preempted_sactive, preempted_qc_active;
1555da917d69STejun Heo 	int preempted_nr_active_links;
1556c6fd2807SJeff Garzik 	DECLARE_COMPLETION_ONSTACK(wait);
1557c6fd2807SJeff Garzik 	unsigned long flags;
1558c6fd2807SJeff Garzik 	unsigned int err_mask;
1559c6fd2807SJeff Garzik 	int rc;
1560c6fd2807SJeff Garzik 
1561c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1562c6fd2807SJeff Garzik 
1563c6fd2807SJeff Garzik 	/* no internal command while frozen */
1564c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_FROZEN) {
1565c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
1566c6fd2807SJeff Garzik 		return AC_ERR_SYSTEM;
1567c6fd2807SJeff Garzik 	}
1568c6fd2807SJeff Garzik 
1569c6fd2807SJeff Garzik 	/* initialize internal qc */
1570c6fd2807SJeff Garzik 
1571c6fd2807SJeff Garzik 	/* XXX: Tag 0 is used for drivers with legacy EH as some
1572c6fd2807SJeff Garzik 	 * drivers choke if any other tag is given.  This breaks
1573c6fd2807SJeff Garzik 	 * ata_tag_internal() test for those drivers.  Don't use new
1574c6fd2807SJeff Garzik 	 * EH stuff without converting to it.
1575c6fd2807SJeff Garzik 	 */
1576c6fd2807SJeff Garzik 	if (ap->ops->error_handler)
1577c6fd2807SJeff Garzik 		tag = ATA_TAG_INTERNAL;
1578c6fd2807SJeff Garzik 	else
1579c6fd2807SJeff Garzik 		tag = 0;
1580c6fd2807SJeff Garzik 
15818a8bc223STejun Heo 	if (test_and_set_bit(tag, &ap->qc_allocated))
15828a8bc223STejun Heo 		BUG();
1583c6fd2807SJeff Garzik 	qc = __ata_qc_from_tag(ap, tag);
1584c6fd2807SJeff Garzik 
1585c6fd2807SJeff Garzik 	qc->tag = tag;
1586c6fd2807SJeff Garzik 	qc->scsicmd = NULL;
1587c6fd2807SJeff Garzik 	qc->ap = ap;
1588c6fd2807SJeff Garzik 	qc->dev = dev;
1589c6fd2807SJeff Garzik 	ata_qc_reinit(qc);
1590c6fd2807SJeff Garzik 
15919af5c9c9STejun Heo 	preempted_tag = link->active_tag;
15929af5c9c9STejun Heo 	preempted_sactive = link->sactive;
1593c6fd2807SJeff Garzik 	preempted_qc_active = ap->qc_active;
1594da917d69STejun Heo 	preempted_nr_active_links = ap->nr_active_links;
15959af5c9c9STejun Heo 	link->active_tag = ATA_TAG_POISON;
15969af5c9c9STejun Heo 	link->sactive = 0;
1597c6fd2807SJeff Garzik 	ap->qc_active = 0;
1598da917d69STejun Heo 	ap->nr_active_links = 0;
1599c6fd2807SJeff Garzik 
1600c6fd2807SJeff Garzik 	/* prepare & issue qc */
1601c6fd2807SJeff Garzik 	qc->tf = *tf;
1602c6fd2807SJeff Garzik 	if (cdb)
1603c6fd2807SJeff Garzik 		memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
1604c6fd2807SJeff Garzik 	qc->flags |= ATA_QCFLAG_RESULT_TF;
1605c6fd2807SJeff Garzik 	qc->dma_dir = dma_dir;
1606c6fd2807SJeff Garzik 	if (dma_dir != DMA_NONE) {
16072432697bSTejun Heo 		unsigned int i, buflen = 0;
160887260216SJens Axboe 		struct scatterlist *sg;
16092432697bSTejun Heo 
161087260216SJens Axboe 		for_each_sg(sgl, sg, n_elem, i)
161187260216SJens Axboe 			buflen += sg->length;
16122432697bSTejun Heo 
161387260216SJens Axboe 		ata_sg_init(qc, sgl, n_elem);
161449c80429SBrian King 		qc->nbytes = buflen;
1615c6fd2807SJeff Garzik 	}
1616c6fd2807SJeff Garzik 
1617c6fd2807SJeff Garzik 	qc->private_data = &wait;
1618c6fd2807SJeff Garzik 	qc->complete_fn = ata_qc_complete_internal;
1619c6fd2807SJeff Garzik 
1620c6fd2807SJeff Garzik 	ata_qc_issue(qc);
1621c6fd2807SJeff Garzik 
1622c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1623c6fd2807SJeff Garzik 
162487fbc5a0STejun Heo 	if (!timeout) {
162587fbc5a0STejun Heo 		if (ata_probe_timeout)
1626341c2c95STejun Heo 			timeout = ata_probe_timeout * 1000;
162787fbc5a0STejun Heo 		else {
162887fbc5a0STejun Heo 			timeout = ata_internal_cmd_timeout(dev, command);
162987fbc5a0STejun Heo 			auto_timeout = 1;
163087fbc5a0STejun Heo 		}
163187fbc5a0STejun Heo 	}
16322b789108STejun Heo 
1633c0c362b6STejun Heo 	if (ap->ops->error_handler)
1634c0c362b6STejun Heo 		ata_eh_release(ap);
1635c0c362b6STejun Heo 
16362b789108STejun Heo 	rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
1637c6fd2807SJeff Garzik 
1638c0c362b6STejun Heo 	if (ap->ops->error_handler)
1639c0c362b6STejun Heo 		ata_eh_acquire(ap);
1640c0c362b6STejun Heo 
1641c429137aSTejun Heo 	ata_sff_flush_pio_task(ap);
1642c6fd2807SJeff Garzik 
1643c6fd2807SJeff Garzik 	if (!rc) {
1644c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
1645c6fd2807SJeff Garzik 
1646c6fd2807SJeff Garzik 		/* We're racing with irq here.  If we lose, the
1647c6fd2807SJeff Garzik 		 * following test prevents us from completing the qc
1648c6fd2807SJeff Garzik 		 * twice.  If we win, the port is frozen and will be
1649c6fd2807SJeff Garzik 		 * cleaned up by ->post_internal_cmd().
1650c6fd2807SJeff Garzik 		 */
1651c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_ACTIVE) {
1652c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_TIMEOUT;
1653c6fd2807SJeff Garzik 
1654c6fd2807SJeff Garzik 			if (ap->ops->error_handler)
1655c6fd2807SJeff Garzik 				ata_port_freeze(ap);
1656c6fd2807SJeff Garzik 			else
1657c6fd2807SJeff Garzik 				ata_qc_complete(qc);
1658c6fd2807SJeff Garzik 
1659c6fd2807SJeff Garzik 			if (ata_msg_warn(ap))
1660a9a79dfeSJoe Perches 				ata_dev_warn(dev, "qc timeout (cmd 0x%x)\n",
1661a9a79dfeSJoe Perches 					     command);
1662c6fd2807SJeff Garzik 		}
1663c6fd2807SJeff Garzik 
1664c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
1665c6fd2807SJeff Garzik 	}
1666c6fd2807SJeff Garzik 
1667c6fd2807SJeff Garzik 	/* do post_internal_cmd */
1668c6fd2807SJeff Garzik 	if (ap->ops->post_internal_cmd)
1669c6fd2807SJeff Garzik 		ap->ops->post_internal_cmd(qc);
1670c6fd2807SJeff Garzik 
1671a51d644aSTejun Heo 	/* perform minimal error analysis */
1672a51d644aSTejun Heo 	if (qc->flags & ATA_QCFLAG_FAILED) {
1673a51d644aSTejun Heo 		if (qc->result_tf.command & (ATA_ERR | ATA_DF))
1674a51d644aSTejun Heo 			qc->err_mask |= AC_ERR_DEV;
1675a51d644aSTejun Heo 
1676a51d644aSTejun Heo 		if (!qc->err_mask)
1677c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_OTHER;
1678a51d644aSTejun Heo 
1679a51d644aSTejun Heo 		if (qc->err_mask & ~AC_ERR_OTHER)
1680a51d644aSTejun Heo 			qc->err_mask &= ~AC_ERR_OTHER;
1681c6fd2807SJeff Garzik 	}
1682c6fd2807SJeff Garzik 
1683c6fd2807SJeff Garzik 	/* finish up */
1684c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1685c6fd2807SJeff Garzik 
1686c6fd2807SJeff Garzik 	*tf = qc->result_tf;
1687c6fd2807SJeff Garzik 	err_mask = qc->err_mask;
1688c6fd2807SJeff Garzik 
1689c6fd2807SJeff Garzik 	ata_qc_free(qc);
16909af5c9c9STejun Heo 	link->active_tag = preempted_tag;
16919af5c9c9STejun Heo 	link->sactive = preempted_sactive;
1692c6fd2807SJeff Garzik 	ap->qc_active = preempted_qc_active;
1693da917d69STejun Heo 	ap->nr_active_links = preempted_nr_active_links;
1694c6fd2807SJeff Garzik 
1695c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1696c6fd2807SJeff Garzik 
169787fbc5a0STejun Heo 	if ((err_mask & AC_ERR_TIMEOUT) && auto_timeout)
169887fbc5a0STejun Heo 		ata_internal_cmd_timed_out(dev, command);
169987fbc5a0STejun Heo 
1700c6fd2807SJeff Garzik 	return err_mask;
1701c6fd2807SJeff Garzik }
1702c6fd2807SJeff Garzik 
1703c6fd2807SJeff Garzik /**
170433480a0eSTejun Heo  *	ata_exec_internal - execute libata internal command
17052432697bSTejun Heo  *	@dev: Device to which the command is sent
17062432697bSTejun Heo  *	@tf: Taskfile registers for the command and the result
17072432697bSTejun Heo  *	@cdb: CDB for packet command
17082432697bSTejun Heo  *	@dma_dir: Data tranfer direction of the command
17092432697bSTejun Heo  *	@buf: Data buffer of the command
17102432697bSTejun Heo  *	@buflen: Length of data buffer
17112b789108STejun Heo  *	@timeout: Timeout in msecs (0 for default)
17122432697bSTejun Heo  *
17132432697bSTejun Heo  *	Wrapper around ata_exec_internal_sg() which takes simple
17142432697bSTejun Heo  *	buffer instead of sg list.
17152432697bSTejun Heo  *
17162432697bSTejun Heo  *	LOCKING:
17172432697bSTejun Heo  *	None.  Should be called with kernel context, might sleep.
17182432697bSTejun Heo  *
17192432697bSTejun Heo  *	RETURNS:
17202432697bSTejun Heo  *	Zero on success, AC_ERR_* mask on failure
17212432697bSTejun Heo  */
17222432697bSTejun Heo unsigned ata_exec_internal(struct ata_device *dev,
17232432697bSTejun Heo 			   struct ata_taskfile *tf, const u8 *cdb,
17242b789108STejun Heo 			   int dma_dir, void *buf, unsigned int buflen,
17252b789108STejun Heo 			   unsigned long timeout)
17262432697bSTejun Heo {
172733480a0eSTejun Heo 	struct scatterlist *psg = NULL, sg;
172833480a0eSTejun Heo 	unsigned int n_elem = 0;
17292432697bSTejun Heo 
173033480a0eSTejun Heo 	if (dma_dir != DMA_NONE) {
173133480a0eSTejun Heo 		WARN_ON(!buf);
17322432697bSTejun Heo 		sg_init_one(&sg, buf, buflen);
173333480a0eSTejun Heo 		psg = &sg;
173433480a0eSTejun Heo 		n_elem++;
173533480a0eSTejun Heo 	}
17362432697bSTejun Heo 
17372b789108STejun Heo 	return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem,
17382b789108STejun Heo 				    timeout);
17392432697bSTejun Heo }
17402432697bSTejun Heo 
17412432697bSTejun Heo /**
1742c6fd2807SJeff Garzik  *	ata_do_simple_cmd - execute simple internal command
1743c6fd2807SJeff Garzik  *	@dev: Device to which the command is sent
1744c6fd2807SJeff Garzik  *	@cmd: Opcode to execute
1745c6fd2807SJeff Garzik  *
1746c6fd2807SJeff Garzik  *	Execute a 'simple' command, that only consists of the opcode
1747c6fd2807SJeff Garzik  *	'cmd' itself, without filling any other registers
1748c6fd2807SJeff Garzik  *
1749c6fd2807SJeff Garzik  *	LOCKING:
1750c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1751c6fd2807SJeff Garzik  *
1752c6fd2807SJeff Garzik  *	RETURNS:
1753c6fd2807SJeff Garzik  *	Zero on success, AC_ERR_* mask on failure
1754c6fd2807SJeff Garzik  */
1755c6fd2807SJeff Garzik unsigned int ata_do_simple_cmd(struct ata_device *dev, u8 cmd)
1756c6fd2807SJeff Garzik {
1757c6fd2807SJeff Garzik 	struct ata_taskfile tf;
1758c6fd2807SJeff Garzik 
1759c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
1760c6fd2807SJeff Garzik 
1761c6fd2807SJeff Garzik 	tf.command = cmd;
1762c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_DEVICE;
1763c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
1764c6fd2807SJeff Garzik 
17652b789108STejun Heo 	return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1766c6fd2807SJeff Garzik }
1767c6fd2807SJeff Garzik 
1768c6fd2807SJeff Garzik /**
1769c6fd2807SJeff Garzik  *	ata_pio_need_iordy	-	check if iordy needed
1770c6fd2807SJeff Garzik  *	@adev: ATA device
1771c6fd2807SJeff Garzik  *
1772c6fd2807SJeff Garzik  *	Check if the current speed of the device requires IORDY. Used
1773c6fd2807SJeff Garzik  *	by various controllers for chip configuration.
1774c6fd2807SJeff Garzik  */
1775c6fd2807SJeff Garzik unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1776c6fd2807SJeff Garzik {
17770d9e6659STejun Heo 	/* Don't set IORDY if we're preparing for reset.  IORDY may
17780d9e6659STejun Heo 	 * lead to controller lock up on certain controllers if the
17790d9e6659STejun Heo 	 * port is not occupied.  See bko#11703 for details.
17800d9e6659STejun Heo 	 */
17810d9e6659STejun Heo 	if (adev->link->ap->pflags & ATA_PFLAG_RESETTING)
17820d9e6659STejun Heo 		return 0;
17830d9e6659STejun Heo 	/* Controller doesn't support IORDY.  Probably a pointless
17840d9e6659STejun Heo 	 * check as the caller should know this.
17850d9e6659STejun Heo 	 */
17869af5c9c9STejun Heo 	if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
1787c6fd2807SJeff Garzik 		return 0;
17885c18c4d2SDavid Daney 	/* CF spec. r4.1 Table 22 says no iordy on PIO5 and PIO6.  */
17895c18c4d2SDavid Daney 	if (ata_id_is_cfa(adev->id)
17905c18c4d2SDavid Daney 	    && (adev->pio_mode == XFER_PIO_5 || adev->pio_mode == XFER_PIO_6))
17915c18c4d2SDavid Daney 		return 0;
1792432729f0SAlan Cox 	/* PIO3 and higher it is mandatory */
1793432729f0SAlan Cox 	if (adev->pio_mode > XFER_PIO_2)
1794c6fd2807SJeff Garzik 		return 1;
1795432729f0SAlan Cox 	/* We turn it on when possible */
1796432729f0SAlan Cox 	if (ata_id_has_iordy(adev->id))
1797432729f0SAlan Cox 		return 1;
1798432729f0SAlan Cox 	return 0;
1799432729f0SAlan Cox }
1800c6fd2807SJeff Garzik 
1801432729f0SAlan Cox /**
1802432729f0SAlan Cox  *	ata_pio_mask_no_iordy	-	Return the non IORDY mask
1803432729f0SAlan Cox  *	@adev: ATA device
1804432729f0SAlan Cox  *
1805432729f0SAlan Cox  *	Compute the highest mode possible if we are not using iordy. Return
1806432729f0SAlan Cox  *	-1 if no iordy mode is available.
1807432729f0SAlan Cox  */
1808432729f0SAlan Cox static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
1809432729f0SAlan Cox {
1810c6fd2807SJeff Garzik 	/* If we have no drive specific rule, then PIO 2 is non IORDY */
1811c6fd2807SJeff Garzik 	if (adev->id[ATA_ID_FIELD_VALID] & 2) {	/* EIDE */
1812432729f0SAlan Cox 		u16 pio = adev->id[ATA_ID_EIDE_PIO];
1813c6fd2807SJeff Garzik 		/* Is the speed faster than the drive allows non IORDY ? */
1814c6fd2807SJeff Garzik 		if (pio) {
1815c6fd2807SJeff Garzik 			/* This is cycle times not frequency - watch the logic! */
1816c6fd2807SJeff Garzik 			if (pio > 240)	/* PIO2 is 240nS per cycle */
1817432729f0SAlan Cox 				return 3 << ATA_SHIFT_PIO;
1818432729f0SAlan Cox 			return 7 << ATA_SHIFT_PIO;
1819c6fd2807SJeff Garzik 		}
1820c6fd2807SJeff Garzik 	}
1821432729f0SAlan Cox 	return 3 << ATA_SHIFT_PIO;
1822c6fd2807SJeff Garzik }
1823c6fd2807SJeff Garzik 
1824c6fd2807SJeff Garzik /**
1825963e4975SAlan Cox  *	ata_do_dev_read_id		-	default ID read method
1826963e4975SAlan Cox  *	@dev: device
1827963e4975SAlan Cox  *	@tf: proposed taskfile
1828963e4975SAlan Cox  *	@id: data buffer
1829963e4975SAlan Cox  *
1830963e4975SAlan Cox  *	Issue the identify taskfile and hand back the buffer containing
1831963e4975SAlan Cox  *	identify data. For some RAID controllers and for pre ATA devices
1832963e4975SAlan Cox  *	this function is wrapped or replaced by the driver
1833963e4975SAlan Cox  */
1834963e4975SAlan Cox unsigned int ata_do_dev_read_id(struct ata_device *dev,
1835963e4975SAlan Cox 					struct ata_taskfile *tf, u16 *id)
1836963e4975SAlan Cox {
1837963e4975SAlan Cox 	return ata_exec_internal(dev, tf, NULL, DMA_FROM_DEVICE,
1838963e4975SAlan Cox 				     id, sizeof(id[0]) * ATA_ID_WORDS, 0);
1839963e4975SAlan Cox }
1840963e4975SAlan Cox 
1841963e4975SAlan Cox /**
1842c6fd2807SJeff Garzik  *	ata_dev_read_id - Read ID data from the specified device
1843c6fd2807SJeff Garzik  *	@dev: target device
1844c6fd2807SJeff Garzik  *	@p_class: pointer to class of the target device (may be changed)
1845bff04647STejun Heo  *	@flags: ATA_READID_* flags
1846c6fd2807SJeff Garzik  *	@id: buffer to read IDENTIFY data into
1847c6fd2807SJeff Garzik  *
1848c6fd2807SJeff Garzik  *	Read ID data from the specified device.  ATA_CMD_ID_ATA is
1849c6fd2807SJeff Garzik  *	performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
1850c6fd2807SJeff Garzik  *	devices.  This function also issues ATA_CMD_INIT_DEV_PARAMS
1851c6fd2807SJeff Garzik  *	for pre-ATA4 drives.
1852c6fd2807SJeff Garzik  *
185350a99018SAlan Cox  *	FIXME: ATA_CMD_ID_ATA is optional for early drives and right
185450a99018SAlan Cox  *	now we abort if we hit that case.
185550a99018SAlan Cox  *
1856c6fd2807SJeff Garzik  *	LOCKING:
1857c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
1858c6fd2807SJeff Garzik  *
1859c6fd2807SJeff Garzik  *	RETURNS:
1860c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
1861c6fd2807SJeff Garzik  */
1862c6fd2807SJeff Garzik int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
1863bff04647STejun Heo 		    unsigned int flags, u16 *id)
1864c6fd2807SJeff Garzik {
18659af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
1866c6fd2807SJeff Garzik 	unsigned int class = *p_class;
1867c6fd2807SJeff Garzik 	struct ata_taskfile tf;
1868c6fd2807SJeff Garzik 	unsigned int err_mask = 0;
1869c6fd2807SJeff Garzik 	const char *reason;
187079b42babSTejun Heo 	bool is_semb = class == ATA_DEV_SEMB;
187154936f8bSTejun Heo 	int may_fallback = 1, tried_spinup = 0;
1872c6fd2807SJeff Garzik 	int rc;
1873c6fd2807SJeff Garzik 
1874c6fd2807SJeff Garzik 	if (ata_msg_ctl(ap))
1875a9a79dfeSJoe Perches 		ata_dev_dbg(dev, "%s: ENTER\n", __func__);
1876c6fd2807SJeff Garzik 
1877c6fd2807SJeff Garzik retry:
1878c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
1879c6fd2807SJeff Garzik 
1880c6fd2807SJeff Garzik 	switch (class) {
188179b42babSTejun Heo 	case ATA_DEV_SEMB:
188279b42babSTejun Heo 		class = ATA_DEV_ATA;	/* some hard drives report SEMB sig */
1883c6fd2807SJeff Garzik 	case ATA_DEV_ATA:
1884c6fd2807SJeff Garzik 		tf.command = ATA_CMD_ID_ATA;
1885c6fd2807SJeff Garzik 		break;
1886c6fd2807SJeff Garzik 	case ATA_DEV_ATAPI:
1887c6fd2807SJeff Garzik 		tf.command = ATA_CMD_ID_ATAPI;
1888c6fd2807SJeff Garzik 		break;
1889c6fd2807SJeff Garzik 	default:
1890c6fd2807SJeff Garzik 		rc = -ENODEV;
1891c6fd2807SJeff Garzik 		reason = "unsupported class";
1892c6fd2807SJeff Garzik 		goto err_out;
1893c6fd2807SJeff Garzik 	}
1894c6fd2807SJeff Garzik 
1895c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_PIO;
189681afe893STejun Heo 
189781afe893STejun Heo 	/* Some devices choke if TF registers contain garbage.  Make
189881afe893STejun Heo 	 * sure those are properly initialized.
189981afe893STejun Heo 	 */
190081afe893STejun Heo 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
190181afe893STejun Heo 
190281afe893STejun Heo 	/* Device presence detection is unreliable on some
190381afe893STejun Heo 	 * controllers.  Always poll IDENTIFY if available.
190481afe893STejun Heo 	 */
190581afe893STejun Heo 	tf.flags |= ATA_TFLAG_POLLING;
1906c6fd2807SJeff Garzik 
1907963e4975SAlan Cox 	if (ap->ops->read_id)
1908963e4975SAlan Cox 		err_mask = ap->ops->read_id(dev, &tf, id);
1909963e4975SAlan Cox 	else
1910963e4975SAlan Cox 		err_mask = ata_do_dev_read_id(dev, &tf, id);
1911963e4975SAlan Cox 
1912c6fd2807SJeff Garzik 	if (err_mask) {
1913800b3996STejun Heo 		if (err_mask & AC_ERR_NODEV_HINT) {
1914a9a79dfeSJoe Perches 			ata_dev_dbg(dev, "NODEV after polling detection\n");
191555a8e2c8STejun Heo 			return -ENOENT;
191655a8e2c8STejun Heo 		}
191755a8e2c8STejun Heo 
191879b42babSTejun Heo 		if (is_semb) {
1919a9a79dfeSJoe Perches 			ata_dev_info(dev,
1920a9a79dfeSJoe Perches 		     "IDENTIFY failed on device w/ SEMB sig, disabled\n");
192179b42babSTejun Heo 			/* SEMB is not supported yet */
192279b42babSTejun Heo 			*p_class = ATA_DEV_SEMB_UNSUP;
192379b42babSTejun Heo 			return 0;
192479b42babSTejun Heo 		}
192579b42babSTejun Heo 
19261ffc151fSTejun Heo 		if ((err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
19271ffc151fSTejun Heo 			/* Device or controller might have reported
19281ffc151fSTejun Heo 			 * the wrong device class.  Give a shot at the
19291ffc151fSTejun Heo 			 * other IDENTIFY if the current one is
19301ffc151fSTejun Heo 			 * aborted by the device.
193154936f8bSTejun Heo 			 */
19321ffc151fSTejun Heo 			if (may_fallback) {
193354936f8bSTejun Heo 				may_fallback = 0;
193454936f8bSTejun Heo 
193554936f8bSTejun Heo 				if (class == ATA_DEV_ATA)
193654936f8bSTejun Heo 					class = ATA_DEV_ATAPI;
193754936f8bSTejun Heo 				else
193854936f8bSTejun Heo 					class = ATA_DEV_ATA;
193954936f8bSTejun Heo 				goto retry;
194054936f8bSTejun Heo 			}
194154936f8bSTejun Heo 
19421ffc151fSTejun Heo 			/* Control reaches here iff the device aborted
19431ffc151fSTejun Heo 			 * both flavors of IDENTIFYs which happens
19441ffc151fSTejun Heo 			 * sometimes with phantom devices.
19451ffc151fSTejun Heo 			 */
1946a9a79dfeSJoe Perches 			ata_dev_dbg(dev,
19471ffc151fSTejun Heo 				    "both IDENTIFYs aborted, assuming NODEV\n");
19481ffc151fSTejun Heo 			return -ENOENT;
19491ffc151fSTejun Heo 		}
19501ffc151fSTejun Heo 
1951c6fd2807SJeff Garzik 		rc = -EIO;
1952c6fd2807SJeff Garzik 		reason = "I/O error";
1953c6fd2807SJeff Garzik 		goto err_out;
1954c6fd2807SJeff Garzik 	}
1955c6fd2807SJeff Garzik 
195643c9c591STejun Heo 	if (dev->horkage & ATA_HORKAGE_DUMP_ID) {
1957a9a79dfeSJoe Perches 		ata_dev_dbg(dev, "dumping IDENTIFY data, "
195843c9c591STejun Heo 			    "class=%d may_fallback=%d tried_spinup=%d\n",
195943c9c591STejun Heo 			    class, may_fallback, tried_spinup);
196043c9c591STejun Heo 		print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET,
196143c9c591STejun Heo 			       16, 2, id, ATA_ID_WORDS * sizeof(*id), true);
196243c9c591STejun Heo 	}
196343c9c591STejun Heo 
196454936f8bSTejun Heo 	/* Falling back doesn't make sense if ID data was read
196554936f8bSTejun Heo 	 * successfully at least once.
196654936f8bSTejun Heo 	 */
196754936f8bSTejun Heo 	may_fallback = 0;
196854936f8bSTejun Heo 
1969c6fd2807SJeff Garzik 	swap_buf_le16(id, ATA_ID_WORDS);
1970c6fd2807SJeff Garzik 
1971c6fd2807SJeff Garzik 	/* sanity check */
1972c6fd2807SJeff Garzik 	rc = -EINVAL;
19736070068bSAlan Cox 	reason = "device reports invalid type";
19744a3381feSJeff Garzik 
19754a3381feSJeff Garzik 	if (class == ATA_DEV_ATA) {
19764a3381feSJeff Garzik 		if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
19774a3381feSJeff Garzik 			goto err_out;
1978db63a4c8SAndy Whitcroft 		if (ap->host->flags & ATA_HOST_IGNORE_ATA &&
1979db63a4c8SAndy Whitcroft 							ata_id_is_ata(id)) {
1980db63a4c8SAndy Whitcroft 			ata_dev_dbg(dev,
1981db63a4c8SAndy Whitcroft 				"host indicates ignore ATA devices, ignored\n");
1982db63a4c8SAndy Whitcroft 			return -ENOENT;
1983db63a4c8SAndy Whitcroft 		}
19844a3381feSJeff Garzik 	} else {
19854a3381feSJeff Garzik 		if (ata_id_is_ata(id))
1986c6fd2807SJeff Garzik 			goto err_out;
1987c6fd2807SJeff Garzik 	}
1988c6fd2807SJeff Garzik 
1989169439c2SMark Lord 	if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
1990169439c2SMark Lord 		tried_spinup = 1;
1991169439c2SMark Lord 		/*
1992169439c2SMark Lord 		 * Drive powered-up in standby mode, and requires a specific
1993169439c2SMark Lord 		 * SET_FEATURES spin-up subcommand before it will accept
1994169439c2SMark Lord 		 * anything other than the original IDENTIFY command.
1995169439c2SMark Lord 		 */
1996218f3d30SJeff Garzik 		err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
1997fb0582f9SRyan Power 		if (err_mask && id[2] != 0x738c) {
1998169439c2SMark Lord 			rc = -EIO;
1999169439c2SMark Lord 			reason = "SPINUP failed";
2000169439c2SMark Lord 			goto err_out;
2001169439c2SMark Lord 		}
2002169439c2SMark Lord 		/*
2003169439c2SMark Lord 		 * If the drive initially returned incomplete IDENTIFY info,
2004169439c2SMark Lord 		 * we now must reissue the IDENTIFY command.
2005169439c2SMark Lord 		 */
2006169439c2SMark Lord 		if (id[2] == 0x37c8)
2007169439c2SMark Lord 			goto retry;
2008169439c2SMark Lord 	}
2009169439c2SMark Lord 
2010bff04647STejun Heo 	if ((flags & ATA_READID_POSTRESET) && class == ATA_DEV_ATA) {
2011c6fd2807SJeff Garzik 		/*
2012c6fd2807SJeff Garzik 		 * The exact sequence expected by certain pre-ATA4 drives is:
2013c6fd2807SJeff Garzik 		 * SRST RESET
201450a99018SAlan Cox 		 * IDENTIFY (optional in early ATA)
201550a99018SAlan Cox 		 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
2016c6fd2807SJeff Garzik 		 * anything else..
2017c6fd2807SJeff Garzik 		 * Some drives were very specific about that exact sequence.
201850a99018SAlan Cox 		 *
201950a99018SAlan Cox 		 * Note that ATA4 says lba is mandatory so the second check
2020c9404c9cSAdam Buchbinder 		 * should never trigger.
2021c6fd2807SJeff Garzik 		 */
2022c6fd2807SJeff Garzik 		if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
2023c6fd2807SJeff Garzik 			err_mask = ata_dev_init_params(dev, id[3], id[6]);
2024c6fd2807SJeff Garzik 			if (err_mask) {
2025c6fd2807SJeff Garzik 				rc = -EIO;
2026c6fd2807SJeff Garzik 				reason = "INIT_DEV_PARAMS failed";
2027c6fd2807SJeff Garzik 				goto err_out;
2028c6fd2807SJeff Garzik 			}
2029c6fd2807SJeff Garzik 
2030c6fd2807SJeff Garzik 			/* current CHS translation info (id[53-58]) might be
2031c6fd2807SJeff Garzik 			 * changed. reread the identify device info.
2032c6fd2807SJeff Garzik 			 */
2033bff04647STejun Heo 			flags &= ~ATA_READID_POSTRESET;
2034c6fd2807SJeff Garzik 			goto retry;
2035c6fd2807SJeff Garzik 		}
2036c6fd2807SJeff Garzik 	}
2037c6fd2807SJeff Garzik 
2038c6fd2807SJeff Garzik 	*p_class = class;
2039c6fd2807SJeff Garzik 
2040c6fd2807SJeff Garzik 	return 0;
2041c6fd2807SJeff Garzik 
2042c6fd2807SJeff Garzik  err_out:
2043c6fd2807SJeff Garzik 	if (ata_msg_warn(ap))
2044a9a79dfeSJoe Perches 		ata_dev_warn(dev, "failed to IDENTIFY (%s, err_mask=0x%x)\n",
2045a9a79dfeSJoe Perches 			     reason, err_mask);
2046c6fd2807SJeff Garzik 	return rc;
2047c6fd2807SJeff Garzik }
2048c6fd2807SJeff Garzik 
20499062712fSTejun Heo static int ata_do_link_spd_horkage(struct ata_device *dev)
20509062712fSTejun Heo {
20519062712fSTejun Heo 	struct ata_link *plink = ata_dev_phys_link(dev);
20529062712fSTejun Heo 	u32 target, target_limit;
20539062712fSTejun Heo 
20549062712fSTejun Heo 	if (!sata_scr_valid(plink))
20559062712fSTejun Heo 		return 0;
20569062712fSTejun Heo 
20579062712fSTejun Heo 	if (dev->horkage & ATA_HORKAGE_1_5_GBPS)
20589062712fSTejun Heo 		target = 1;
20599062712fSTejun Heo 	else
20609062712fSTejun Heo 		return 0;
20619062712fSTejun Heo 
20629062712fSTejun Heo 	target_limit = (1 << target) - 1;
20639062712fSTejun Heo 
20649062712fSTejun Heo 	/* if already on stricter limit, no need to push further */
20659062712fSTejun Heo 	if (plink->sata_spd_limit <= target_limit)
20669062712fSTejun Heo 		return 0;
20679062712fSTejun Heo 
20689062712fSTejun Heo 	plink->sata_spd_limit = target_limit;
20699062712fSTejun Heo 
20709062712fSTejun Heo 	/* Request another EH round by returning -EAGAIN if link is
20719062712fSTejun Heo 	 * going faster than the target speed.  Forward progress is
20729062712fSTejun Heo 	 * guaranteed by setting sata_spd_limit to target_limit above.
20739062712fSTejun Heo 	 */
20749062712fSTejun Heo 	if (plink->sata_spd > target) {
2075a9a79dfeSJoe Perches 		ata_dev_info(dev, "applying link speed limit horkage to %s\n",
20769062712fSTejun Heo 			     sata_spd_string(target));
20779062712fSTejun Heo 		return -EAGAIN;
20789062712fSTejun Heo 	}
20799062712fSTejun Heo 	return 0;
20809062712fSTejun Heo }
20819062712fSTejun Heo 
2082c6fd2807SJeff Garzik static inline u8 ata_dev_knobble(struct ata_device *dev)
2083c6fd2807SJeff Garzik {
20849af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
20859ce8e307SJens Axboe 
20869ce8e307SJens Axboe 	if (ata_dev_blacklisted(dev) & ATA_HORKAGE_BRIDGE_OK)
20879ce8e307SJens Axboe 		return 0;
20889ce8e307SJens Axboe 
20899af5c9c9STejun Heo 	return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
2090c6fd2807SJeff Garzik }
2091c6fd2807SJeff Garzik 
2092388539f3SShaohua Li static int ata_dev_config_ncq(struct ata_device *dev,
2093c6fd2807SJeff Garzik 			       char *desc, size_t desc_sz)
2094c6fd2807SJeff Garzik {
20959af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
2096c6fd2807SJeff Garzik 	int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
2097388539f3SShaohua Li 	unsigned int err_mask;
2098388539f3SShaohua Li 	char *aa_desc = "";
2099c6fd2807SJeff Garzik 
2100c6fd2807SJeff Garzik 	if (!ata_id_has_ncq(dev->id)) {
2101c6fd2807SJeff Garzik 		desc[0] = '\0';
2102388539f3SShaohua Li 		return 0;
2103c6fd2807SJeff Garzik 	}
210475683fe7STejun Heo 	if (dev->horkage & ATA_HORKAGE_NONCQ) {
21056919a0a6SAlan Cox 		snprintf(desc, desc_sz, "NCQ (not used)");
2106388539f3SShaohua Li 		return 0;
21076919a0a6SAlan Cox 	}
2108c6fd2807SJeff Garzik 	if (ap->flags & ATA_FLAG_NCQ) {
2109cca3974eSJeff Garzik 		hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE - 1);
2110c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_NCQ;
2111c6fd2807SJeff Garzik 	}
2112c6fd2807SJeff Garzik 
2113388539f3SShaohua Li 	if (!(dev->horkage & ATA_HORKAGE_BROKEN_FPDMA_AA) &&
2114388539f3SShaohua Li 		(ap->flags & ATA_FLAG_FPDMA_AA) &&
2115388539f3SShaohua Li 		ata_id_has_fpdma_aa(dev->id)) {
2116388539f3SShaohua Li 		err_mask = ata_dev_set_feature(dev, SETFEATURES_SATA_ENABLE,
2117388539f3SShaohua Li 			SATA_FPDMA_AA);
2118388539f3SShaohua Li 		if (err_mask) {
2119a9a79dfeSJoe Perches 			ata_dev_err(dev,
2120a9a79dfeSJoe Perches 				    "failed to enable AA (error_mask=0x%x)\n",
2121a9a79dfeSJoe Perches 				    err_mask);
2122388539f3SShaohua Li 			if (err_mask != AC_ERR_DEV) {
2123388539f3SShaohua Li 				dev->horkage |= ATA_HORKAGE_BROKEN_FPDMA_AA;
2124388539f3SShaohua Li 				return -EIO;
2125388539f3SShaohua Li 			}
2126388539f3SShaohua Li 		} else
2127388539f3SShaohua Li 			aa_desc = ", AA";
2128388539f3SShaohua Li 	}
2129388539f3SShaohua Li 
2130c6fd2807SJeff Garzik 	if (hdepth >= ddepth)
2131388539f3SShaohua Li 		snprintf(desc, desc_sz, "NCQ (depth %d)%s", ddepth, aa_desc);
2132c6fd2807SJeff Garzik 	else
2133388539f3SShaohua Li 		snprintf(desc, desc_sz, "NCQ (depth %d/%d)%s", hdepth,
2134388539f3SShaohua Li 			ddepth, aa_desc);
2135388539f3SShaohua Li 	return 0;
2136c6fd2807SJeff Garzik }
2137c6fd2807SJeff Garzik 
2138c6fd2807SJeff Garzik /**
2139c6fd2807SJeff Garzik  *	ata_dev_configure - Configure the specified ATA/ATAPI device
2140c6fd2807SJeff Garzik  *	@dev: Target device to configure
2141c6fd2807SJeff Garzik  *
2142c6fd2807SJeff Garzik  *	Configure @dev according to @dev->id.  Generic and low-level
2143c6fd2807SJeff Garzik  *	driver specific fixups are also applied.
2144c6fd2807SJeff Garzik  *
2145c6fd2807SJeff Garzik  *	LOCKING:
2146c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
2147c6fd2807SJeff Garzik  *
2148c6fd2807SJeff Garzik  *	RETURNS:
2149c6fd2807SJeff Garzik  *	0 on success, -errno otherwise
2150c6fd2807SJeff Garzik  */
2151efdaedc4STejun Heo int ata_dev_configure(struct ata_device *dev)
2152c6fd2807SJeff Garzik {
21539af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
21549af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
21556746544cSTejun Heo 	int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
2156c6fd2807SJeff Garzik 	const u16 *id = dev->id;
21577dc951aeSTejun Heo 	unsigned long xfer_mask;
215865fe1f0fSShane Huang 	unsigned int err_mask;
2159b352e57dSAlan Cox 	char revbuf[7];		/* XYZ-99\0 */
21603f64f565SEric D. Mudama 	char fwrevbuf[ATA_ID_FW_REV_LEN+1];
21613f64f565SEric D. Mudama 	char modelbuf[ATA_ID_PROD_LEN+1];
2162c6fd2807SJeff Garzik 	int rc;
2163c6fd2807SJeff Garzik 
2164c6fd2807SJeff Garzik 	if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
2165a9a79dfeSJoe Perches 		ata_dev_info(dev, "%s: ENTER/EXIT -- nodev\n", __func__);
2166c6fd2807SJeff Garzik 		return 0;
2167c6fd2807SJeff Garzik 	}
2168c6fd2807SJeff Garzik 
2169c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2170a9a79dfeSJoe Perches 		ata_dev_dbg(dev, "%s: ENTER\n", __func__);
2171c6fd2807SJeff Garzik 
217275683fe7STejun Heo 	/* set horkage */
217375683fe7STejun Heo 	dev->horkage |= ata_dev_blacklisted(dev);
217433267325STejun Heo 	ata_force_horkage(dev);
217575683fe7STejun Heo 
217650af2fa1STejun Heo 	if (dev->horkage & ATA_HORKAGE_DISABLE) {
2177a9a79dfeSJoe Perches 		ata_dev_info(dev, "unsupported device, disabling\n");
217850af2fa1STejun Heo 		ata_dev_disable(dev);
217950af2fa1STejun Heo 		return 0;
218050af2fa1STejun Heo 	}
218150af2fa1STejun Heo 
21822486fa56STejun Heo 	if ((!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) &&
21832486fa56STejun Heo 	    dev->class == ATA_DEV_ATAPI) {
2184a9a79dfeSJoe Perches 		ata_dev_warn(dev, "WARNING: ATAPI is %s, device ignored\n",
21852486fa56STejun Heo 			     atapi_enabled ? "not supported with this driver"
21862486fa56STejun Heo 			     : "disabled");
21872486fa56STejun Heo 		ata_dev_disable(dev);
21882486fa56STejun Heo 		return 0;
21892486fa56STejun Heo 	}
21902486fa56STejun Heo 
21919062712fSTejun Heo 	rc = ata_do_link_spd_horkage(dev);
21929062712fSTejun Heo 	if (rc)
21939062712fSTejun Heo 		return rc;
21949062712fSTejun Heo 
21956746544cSTejun Heo 	/* let ACPI work its magic */
21966746544cSTejun Heo 	rc = ata_acpi_on_devcfg(dev);
21976746544cSTejun Heo 	if (rc)
21986746544cSTejun Heo 		return rc;
219908573a86SKristen Carlson Accardi 
220005027adcSTejun Heo 	/* massage HPA, do it early as it might change IDENTIFY data */
220105027adcSTejun Heo 	rc = ata_hpa_resize(dev);
220205027adcSTejun Heo 	if (rc)
220305027adcSTejun Heo 		return rc;
220405027adcSTejun Heo 
2205c6fd2807SJeff Garzik 	/* print device capabilities */
2206c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2207a9a79dfeSJoe Perches 		ata_dev_dbg(dev,
2208c6fd2807SJeff Garzik 			    "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
2209c6fd2807SJeff Garzik 			    "85:%04x 86:%04x 87:%04x 88:%04x\n",
22107f5e4e8dSHarvey Harrison 			    __func__,
2211c6fd2807SJeff Garzik 			    id[49], id[82], id[83], id[84],
2212c6fd2807SJeff Garzik 			    id[85], id[86], id[87], id[88]);
2213c6fd2807SJeff Garzik 
2214c6fd2807SJeff Garzik 	/* initialize to-be-configured parameters */
2215c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_CFG_MASK;
2216c6fd2807SJeff Garzik 	dev->max_sectors = 0;
2217c6fd2807SJeff Garzik 	dev->cdb_len = 0;
2218c6fd2807SJeff Garzik 	dev->n_sectors = 0;
2219c6fd2807SJeff Garzik 	dev->cylinders = 0;
2220c6fd2807SJeff Garzik 	dev->heads = 0;
2221c6fd2807SJeff Garzik 	dev->sectors = 0;
2222e18086d6SMark Lord 	dev->multi_count = 0;
2223c6fd2807SJeff Garzik 
2224c6fd2807SJeff Garzik 	/*
2225c6fd2807SJeff Garzik 	 * common ATA, ATAPI feature tests
2226c6fd2807SJeff Garzik 	 */
2227c6fd2807SJeff Garzik 
2228c6fd2807SJeff Garzik 	/* find max transfer mode; for printk only */
2229c6fd2807SJeff Garzik 	xfer_mask = ata_id_xfermask(id);
2230c6fd2807SJeff Garzik 
2231c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2232c6fd2807SJeff Garzik 		ata_dump_id(id);
2233c6fd2807SJeff Garzik 
2234ef143d57SAlbert Lee 	/* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
2235ef143d57SAlbert Lee 	ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
2236ef143d57SAlbert Lee 			sizeof(fwrevbuf));
2237ef143d57SAlbert Lee 
2238ef143d57SAlbert Lee 	ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
2239ef143d57SAlbert Lee 			sizeof(modelbuf));
2240ef143d57SAlbert Lee 
2241c6fd2807SJeff Garzik 	/* ATA-specific feature tests */
2242c6fd2807SJeff Garzik 	if (dev->class == ATA_DEV_ATA) {
2243b352e57dSAlan Cox 		if (ata_id_is_cfa(id)) {
224462afe5d7SSergei Shtylyov 			/* CPRM may make this media unusable */
224562afe5d7SSergei Shtylyov 			if (id[ATA_ID_CFA_KEY_MGMT] & 1)
2246a9a79dfeSJoe Perches 				ata_dev_warn(dev,
2247a9a79dfeSJoe Perches 	"supports DRM functions and may not be fully accessible\n");
2248b352e57dSAlan Cox 			snprintf(revbuf, 7, "CFA");
2249ae8d4ee7SAlan Cox 		} else {
2250b352e57dSAlan Cox 			snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
2251ae8d4ee7SAlan Cox 			/* Warn the user if the device has TPM extensions */
2252ae8d4ee7SAlan Cox 			if (ata_id_has_tpm(id))
2253a9a79dfeSJoe Perches 				ata_dev_warn(dev,
2254a9a79dfeSJoe Perches 	"supports DRM functions and may not be fully accessible\n");
2255ae8d4ee7SAlan Cox 		}
2256b352e57dSAlan Cox 
2257c6fd2807SJeff Garzik 		dev->n_sectors = ata_id_n_sectors(id);
2258c6fd2807SJeff Garzik 
2259e18086d6SMark Lord 		/* get current R/W Multiple count setting */
2260e18086d6SMark Lord 		if ((dev->id[47] >> 8) == 0x80 && (dev->id[59] & 0x100)) {
2261e18086d6SMark Lord 			unsigned int max = dev->id[47] & 0xff;
2262e18086d6SMark Lord 			unsigned int cnt = dev->id[59] & 0xff;
2263e18086d6SMark Lord 			/* only recognize/allow powers of two here */
2264e18086d6SMark Lord 			if (is_power_of_2(max) && is_power_of_2(cnt))
2265e18086d6SMark Lord 				if (cnt <= max)
2266e18086d6SMark Lord 					dev->multi_count = cnt;
2267e18086d6SMark Lord 		}
22683f64f565SEric D. Mudama 
2269c6fd2807SJeff Garzik 		if (ata_id_has_lba(id)) {
2270c6fd2807SJeff Garzik 			const char *lba_desc;
2271388539f3SShaohua Li 			char ncq_desc[24];
2272c6fd2807SJeff Garzik 
2273c6fd2807SJeff Garzik 			lba_desc = "LBA";
2274c6fd2807SJeff Garzik 			dev->flags |= ATA_DFLAG_LBA;
2275c6fd2807SJeff Garzik 			if (ata_id_has_lba48(id)) {
2276c6fd2807SJeff Garzik 				dev->flags |= ATA_DFLAG_LBA48;
2277c6fd2807SJeff Garzik 				lba_desc = "LBA48";
22786fc49adbSTejun Heo 
22796fc49adbSTejun Heo 				if (dev->n_sectors >= (1UL << 28) &&
22806fc49adbSTejun Heo 				    ata_id_has_flush_ext(id))
22816fc49adbSTejun Heo 					dev->flags |= ATA_DFLAG_FLUSH_EXT;
2282c6fd2807SJeff Garzik 			}
2283c6fd2807SJeff Garzik 
2284c6fd2807SJeff Garzik 			/* config NCQ */
2285388539f3SShaohua Li 			rc = ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
2286388539f3SShaohua Li 			if (rc)
2287388539f3SShaohua Li 				return rc;
2288c6fd2807SJeff Garzik 
2289c6fd2807SJeff Garzik 			/* print device info to dmesg */
22903f64f565SEric D. Mudama 			if (ata_msg_drv(ap) && print_info) {
2291a9a79dfeSJoe Perches 				ata_dev_info(dev, "%s: %s, %s, max %s\n",
22923f64f565SEric D. Mudama 					     revbuf, modelbuf, fwrevbuf,
22933f64f565SEric D. Mudama 					     ata_mode_string(xfer_mask));
2294a9a79dfeSJoe Perches 				ata_dev_info(dev,
2295a9a79dfeSJoe Perches 					     "%llu sectors, multi %u: %s %s\n",
2296c6fd2807SJeff Garzik 					(unsigned long long)dev->n_sectors,
22973f64f565SEric D. Mudama 					dev->multi_count, lba_desc, ncq_desc);
22983f64f565SEric D. Mudama 			}
2299c6fd2807SJeff Garzik 		} else {
2300c6fd2807SJeff Garzik 			/* CHS */
2301c6fd2807SJeff Garzik 
2302c6fd2807SJeff Garzik 			/* Default translation */
2303c6fd2807SJeff Garzik 			dev->cylinders	= id[1];
2304c6fd2807SJeff Garzik 			dev->heads	= id[3];
2305c6fd2807SJeff Garzik 			dev->sectors	= id[6];
2306c6fd2807SJeff Garzik 
2307c6fd2807SJeff Garzik 			if (ata_id_current_chs_valid(id)) {
2308c6fd2807SJeff Garzik 				/* Current CHS translation is valid. */
2309c6fd2807SJeff Garzik 				dev->cylinders = id[54];
2310c6fd2807SJeff Garzik 				dev->heads     = id[55];
2311c6fd2807SJeff Garzik 				dev->sectors   = id[56];
2312c6fd2807SJeff Garzik 			}
2313c6fd2807SJeff Garzik 
2314c6fd2807SJeff Garzik 			/* print device info to dmesg */
23153f64f565SEric D. Mudama 			if (ata_msg_drv(ap) && print_info) {
2316a9a79dfeSJoe Perches 				ata_dev_info(dev, "%s: %s, %s, max %s\n",
23173f64f565SEric D. Mudama 					     revbuf,	modelbuf, fwrevbuf,
23183f64f565SEric D. Mudama 					     ata_mode_string(xfer_mask));
2319a9a79dfeSJoe Perches 				ata_dev_info(dev,
2320a9a79dfeSJoe Perches 					     "%llu sectors, multi %u, CHS %u/%u/%u\n",
23213f64f565SEric D. Mudama 					     (unsigned long long)dev->n_sectors,
23223f64f565SEric D. Mudama 					     dev->multi_count, dev->cylinders,
23233f64f565SEric D. Mudama 					     dev->heads, dev->sectors);
23243f64f565SEric D. Mudama 			}
2325c6fd2807SJeff Garzik 		}
2326c6fd2807SJeff Garzik 
232765fe1f0fSShane Huang 		/* check and mark DevSlp capability */
232865fe1f0fSShane Huang 		if (ata_id_has_devslp(dev->id))
232965fe1f0fSShane Huang 			dev->flags |= ATA_DFLAG_DEVSLP;
233065fe1f0fSShane Huang 
233165fe1f0fSShane Huang 		/* Obtain SATA Settings page from Identify Device Data Log,
233265fe1f0fSShane Huang 		 * which contains DevSlp timing variables etc.
233365fe1f0fSShane Huang 		 * Exclude old devices with ata_id_has_ncq()
233465fe1f0fSShane Huang 		 */
233565fe1f0fSShane Huang 		if (ata_id_has_ncq(dev->id)) {
233665fe1f0fSShane Huang 			err_mask = ata_read_log_page(dev,
233765fe1f0fSShane Huang 						     ATA_LOG_SATA_ID_DEV_DATA,
233865fe1f0fSShane Huang 						     ATA_LOG_SATA_SETTINGS,
233965fe1f0fSShane Huang 						     dev->sata_settings,
234065fe1f0fSShane Huang 						     1);
234165fe1f0fSShane Huang 			if (err_mask)
234265fe1f0fSShane Huang 				ata_dev_dbg(dev,
234365fe1f0fSShane Huang 					    "failed to get Identify Device Data, Emask 0x%x\n",
234465fe1f0fSShane Huang 					    err_mask);
234565fe1f0fSShane Huang 		}
234665fe1f0fSShane Huang 
2347c6fd2807SJeff Garzik 		dev->cdb_len = 16;
2348c6fd2807SJeff Garzik 	}
2349c6fd2807SJeff Garzik 
2350c6fd2807SJeff Garzik 	/* ATAPI-specific feature tests */
2351c6fd2807SJeff Garzik 	else if (dev->class == ATA_DEV_ATAPI) {
2352854c73a2STejun Heo 		const char *cdb_intr_string = "";
2353854c73a2STejun Heo 		const char *atapi_an_string = "";
235491163006STejun Heo 		const char *dma_dir_string = "";
23557d77b247STejun Heo 		u32 sntf;
2356c6fd2807SJeff Garzik 
2357c6fd2807SJeff Garzik 		rc = atapi_cdb_len(id);
2358c6fd2807SJeff Garzik 		if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
2359c6fd2807SJeff Garzik 			if (ata_msg_warn(ap))
2360a9a79dfeSJoe Perches 				ata_dev_warn(dev, "unsupported CDB len\n");
2361c6fd2807SJeff Garzik 			rc = -EINVAL;
2362c6fd2807SJeff Garzik 			goto err_out_nosup;
2363c6fd2807SJeff Garzik 		}
2364c6fd2807SJeff Garzik 		dev->cdb_len = (unsigned int) rc;
2365c6fd2807SJeff Garzik 
23667d77b247STejun Heo 		/* Enable ATAPI AN if both the host and device have
23677d77b247STejun Heo 		 * the support.  If PMP is attached, SNTF is required
23687d77b247STejun Heo 		 * to enable ATAPI AN to discern between PHY status
23697d77b247STejun Heo 		 * changed notifications and ATAPI ANs.
23709f45cbd3SKristen Carlson Accardi 		 */
2371e7ecd435STejun Heo 		if (atapi_an &&
2372e7ecd435STejun Heo 		    (ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
2373071f44b1STejun Heo 		    (!sata_pmp_attached(ap) ||
23747d77b247STejun Heo 		     sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
23759f45cbd3SKristen Carlson Accardi 			/* issue SET feature command to turn this on */
2376218f3d30SJeff Garzik 			err_mask = ata_dev_set_feature(dev,
2377218f3d30SJeff Garzik 					SETFEATURES_SATA_ENABLE, SATA_AN);
2378854c73a2STejun Heo 			if (err_mask)
2379a9a79dfeSJoe Perches 				ata_dev_err(dev,
2380a9a79dfeSJoe Perches 					    "failed to enable ATAPI AN (err_mask=0x%x)\n",
2381a9a79dfeSJoe Perches 					    err_mask);
2382854c73a2STejun Heo 			else {
23839f45cbd3SKristen Carlson Accardi 				dev->flags |= ATA_DFLAG_AN;
2384854c73a2STejun Heo 				atapi_an_string = ", ATAPI AN";
2385854c73a2STejun Heo 			}
23869f45cbd3SKristen Carlson Accardi 		}
23879f45cbd3SKristen Carlson Accardi 
2388c6fd2807SJeff Garzik 		if (ata_id_cdb_intr(dev->id)) {
2389c6fd2807SJeff Garzik 			dev->flags |= ATA_DFLAG_CDB_INTR;
2390c6fd2807SJeff Garzik 			cdb_intr_string = ", CDB intr";
2391c6fd2807SJeff Garzik 		}
2392c6fd2807SJeff Garzik 
239391163006STejun Heo 		if (atapi_dmadir || atapi_id_dmadir(dev->id)) {
239491163006STejun Heo 			dev->flags |= ATA_DFLAG_DMADIR;
239591163006STejun Heo 			dma_dir_string = ", DMADIR";
239691163006STejun Heo 		}
239791163006STejun Heo 
2398b1354cbbSLin Ming 		if (ata_id_has_da(dev->id))
2399b1354cbbSLin Ming 			dev->flags |= ATA_DFLAG_DA;
2400b1354cbbSLin Ming 
2401c6fd2807SJeff Garzik 		/* print device info to dmesg */
2402c6fd2807SJeff Garzik 		if (ata_msg_drv(ap) && print_info)
2403a9a79dfeSJoe Perches 			ata_dev_info(dev,
240491163006STejun Heo 				     "ATAPI: %s, %s, max %s%s%s%s\n",
2405ef143d57SAlbert Lee 				     modelbuf, fwrevbuf,
2406c6fd2807SJeff Garzik 				     ata_mode_string(xfer_mask),
240791163006STejun Heo 				     cdb_intr_string, atapi_an_string,
240891163006STejun Heo 				     dma_dir_string);
2409c6fd2807SJeff Garzik 	}
2410c6fd2807SJeff Garzik 
2411914ed354STejun Heo 	/* determine max_sectors */
2412914ed354STejun Heo 	dev->max_sectors = ATA_MAX_SECTORS;
2413914ed354STejun Heo 	if (dev->flags & ATA_DFLAG_LBA48)
2414914ed354STejun Heo 		dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2415914ed354STejun Heo 
2416c5038fc0SAlan Cox 	/* Limit PATA drive on SATA cable bridge transfers to udma5,
2417c5038fc0SAlan Cox 	   200 sectors */
2418c6fd2807SJeff Garzik 	if (ata_dev_knobble(dev)) {
2419c6fd2807SJeff Garzik 		if (ata_msg_drv(ap) && print_info)
2420a9a79dfeSJoe Perches 			ata_dev_info(dev, "applying bridge limits\n");
2421c6fd2807SJeff Garzik 		dev->udma_mask &= ATA_UDMA5;
2422c6fd2807SJeff Garzik 		dev->max_sectors = ATA_MAX_SECTORS;
2423c6fd2807SJeff Garzik 	}
2424c6fd2807SJeff Garzik 
2425f8d8e579STony Battersby 	if ((dev->class == ATA_DEV_ATAPI) &&
2426f442cd86SAlbert Lee 	    (atapi_command_packet_set(id) == TYPE_TAPE)) {
2427f8d8e579STony Battersby 		dev->max_sectors = ATA_MAX_SECTORS_TAPE;
2428f442cd86SAlbert Lee 		dev->horkage |= ATA_HORKAGE_STUCK_ERR;
2429f442cd86SAlbert Lee 	}
2430f8d8e579STony Battersby 
243175683fe7STejun Heo 	if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
243203ec52deSTejun Heo 		dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
243303ec52deSTejun Heo 					 dev->max_sectors);
243418d6e9d5SAlbert Lee 
2435c6fd2807SJeff Garzik 	if (ap->ops->dev_config)
2436cd0d3bbcSAlan 		ap->ops->dev_config(dev);
2437c6fd2807SJeff Garzik 
2438c5038fc0SAlan Cox 	if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2439c5038fc0SAlan Cox 		/* Let the user know. We don't want to disallow opens for
2440c5038fc0SAlan Cox 		   rescue purposes, or in case the vendor is just a blithering
2441c5038fc0SAlan Cox 		   idiot. Do this after the dev_config call as some controllers
2442c5038fc0SAlan Cox 		   with buggy firmware may want to avoid reporting false device
2443c5038fc0SAlan Cox 		   bugs */
2444c5038fc0SAlan Cox 
2445c5038fc0SAlan Cox 		if (print_info) {
2446a9a79dfeSJoe Perches 			ata_dev_warn(dev,
2447c5038fc0SAlan Cox "Drive reports diagnostics failure. This may indicate a drive\n");
2448a9a79dfeSJoe Perches 			ata_dev_warn(dev,
2449c5038fc0SAlan Cox "fault or invalid emulation. Contact drive vendor for information.\n");
2450c5038fc0SAlan Cox 		}
2451c5038fc0SAlan Cox 	}
2452c5038fc0SAlan Cox 
2453ac70a964STejun Heo 	if ((dev->horkage & ATA_HORKAGE_FIRMWARE_WARN) && print_info) {
2454a9a79dfeSJoe Perches 		ata_dev_warn(dev, "WARNING: device requires firmware update to be fully functional\n");
2455a9a79dfeSJoe Perches 		ata_dev_warn(dev, "         contact the vendor or visit http://ata.wiki.kernel.org\n");
2456ac70a964STejun Heo 	}
2457ac70a964STejun Heo 
2458c6fd2807SJeff Garzik 	return 0;
2459c6fd2807SJeff Garzik 
2460c6fd2807SJeff Garzik err_out_nosup:
2461c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2462a9a79dfeSJoe Perches 		ata_dev_dbg(dev, "%s: EXIT, err\n", __func__);
2463c6fd2807SJeff Garzik 	return rc;
2464c6fd2807SJeff Garzik }
2465c6fd2807SJeff Garzik 
2466c6fd2807SJeff Garzik /**
24672e41e8e6SAlan Cox  *	ata_cable_40wire	-	return 40 wire cable type
2468be0d18dfSAlan Cox  *	@ap: port
2469be0d18dfSAlan Cox  *
24702e41e8e6SAlan Cox  *	Helper method for drivers which want to hardwire 40 wire cable
2471be0d18dfSAlan Cox  *	detection.
2472be0d18dfSAlan Cox  */
2473be0d18dfSAlan Cox 
2474be0d18dfSAlan Cox int ata_cable_40wire(struct ata_port *ap)
2475be0d18dfSAlan Cox {
2476be0d18dfSAlan Cox 	return ATA_CBL_PATA40;
2477be0d18dfSAlan Cox }
2478be0d18dfSAlan Cox 
2479be0d18dfSAlan Cox /**
24802e41e8e6SAlan Cox  *	ata_cable_80wire	-	return 80 wire cable type
2481be0d18dfSAlan Cox  *	@ap: port
2482be0d18dfSAlan Cox  *
24832e41e8e6SAlan Cox  *	Helper method for drivers which want to hardwire 80 wire cable
2484be0d18dfSAlan Cox  *	detection.
2485be0d18dfSAlan Cox  */
2486be0d18dfSAlan Cox 
2487be0d18dfSAlan Cox int ata_cable_80wire(struct ata_port *ap)
2488be0d18dfSAlan Cox {
2489be0d18dfSAlan Cox 	return ATA_CBL_PATA80;
2490be0d18dfSAlan Cox }
2491be0d18dfSAlan Cox 
2492be0d18dfSAlan Cox /**
2493be0d18dfSAlan Cox  *	ata_cable_unknown	-	return unknown PATA cable.
2494be0d18dfSAlan Cox  *	@ap: port
2495be0d18dfSAlan Cox  *
2496be0d18dfSAlan Cox  *	Helper method for drivers which have no PATA cable detection.
2497be0d18dfSAlan Cox  */
2498be0d18dfSAlan Cox 
2499be0d18dfSAlan Cox int ata_cable_unknown(struct ata_port *ap)
2500be0d18dfSAlan Cox {
2501be0d18dfSAlan Cox 	return ATA_CBL_PATA_UNK;
2502be0d18dfSAlan Cox }
2503be0d18dfSAlan Cox 
2504be0d18dfSAlan Cox /**
2505c88f90c3STejun Heo  *	ata_cable_ignore	-	return ignored PATA cable.
2506c88f90c3STejun Heo  *	@ap: port
2507c88f90c3STejun Heo  *
2508c88f90c3STejun Heo  *	Helper method for drivers which don't use cable type to limit
2509c88f90c3STejun Heo  *	transfer mode.
2510c88f90c3STejun Heo  */
2511c88f90c3STejun Heo int ata_cable_ignore(struct ata_port *ap)
2512c88f90c3STejun Heo {
2513c88f90c3STejun Heo 	return ATA_CBL_PATA_IGN;
2514c88f90c3STejun Heo }
2515c88f90c3STejun Heo 
2516c88f90c3STejun Heo /**
2517be0d18dfSAlan Cox  *	ata_cable_sata	-	return SATA cable type
2518be0d18dfSAlan Cox  *	@ap: port
2519be0d18dfSAlan Cox  *
2520be0d18dfSAlan Cox  *	Helper method for drivers which have SATA cables
2521be0d18dfSAlan Cox  */
2522be0d18dfSAlan Cox 
2523be0d18dfSAlan Cox int ata_cable_sata(struct ata_port *ap)
2524be0d18dfSAlan Cox {
2525be0d18dfSAlan Cox 	return ATA_CBL_SATA;
2526be0d18dfSAlan Cox }
2527be0d18dfSAlan Cox 
2528be0d18dfSAlan Cox /**
2529c6fd2807SJeff Garzik  *	ata_bus_probe - Reset and probe ATA bus
2530c6fd2807SJeff Garzik  *	@ap: Bus to probe
2531c6fd2807SJeff Garzik  *
2532c6fd2807SJeff Garzik  *	Master ATA bus probing function.  Initiates a hardware-dependent
2533c6fd2807SJeff Garzik  *	bus reset, then attempts to identify any devices found on
2534c6fd2807SJeff Garzik  *	the bus.
2535c6fd2807SJeff Garzik  *
2536c6fd2807SJeff Garzik  *	LOCKING:
2537c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
2538c6fd2807SJeff Garzik  *
2539c6fd2807SJeff Garzik  *	RETURNS:
2540c6fd2807SJeff Garzik  *	Zero on success, negative errno otherwise.
2541c6fd2807SJeff Garzik  */
2542c6fd2807SJeff Garzik 
2543c6fd2807SJeff Garzik int ata_bus_probe(struct ata_port *ap)
2544c6fd2807SJeff Garzik {
2545c6fd2807SJeff Garzik 	unsigned int classes[ATA_MAX_DEVICES];
2546c6fd2807SJeff Garzik 	int tries[ATA_MAX_DEVICES];
2547f58229f8STejun Heo 	int rc;
2548c6fd2807SJeff Garzik 	struct ata_device *dev;
2549c6fd2807SJeff Garzik 
25501eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL)
2551f58229f8STejun Heo 		tries[dev->devno] = ATA_PROBE_MAX_TRIES;
2552c6fd2807SJeff Garzik 
2553c6fd2807SJeff Garzik  retry:
25541eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL) {
2555cdeab114STejun Heo 		/* If we issue an SRST then an ATA drive (not ATAPI)
2556cdeab114STejun Heo 		 * may change configuration and be in PIO0 timing. If
2557cdeab114STejun Heo 		 * we do a hard reset (or are coming from power on)
2558cdeab114STejun Heo 		 * this is true for ATA or ATAPI. Until we've set a
2559cdeab114STejun Heo 		 * suitable controller mode we should not touch the
2560cdeab114STejun Heo 		 * bus as we may be talking too fast.
2561cdeab114STejun Heo 		 */
2562cdeab114STejun Heo 		dev->pio_mode = XFER_PIO_0;
2563cdeab114STejun Heo 
2564cdeab114STejun Heo 		/* If the controller has a pio mode setup function
2565cdeab114STejun Heo 		 * then use it to set the chipset to rights. Don't
2566cdeab114STejun Heo 		 * touch the DMA setup as that will be dealt with when
2567cdeab114STejun Heo 		 * configuring devices.
2568cdeab114STejun Heo 		 */
2569cdeab114STejun Heo 		if (ap->ops->set_piomode)
2570cdeab114STejun Heo 			ap->ops->set_piomode(ap, dev);
2571cdeab114STejun Heo 	}
2572cdeab114STejun Heo 
2573c6fd2807SJeff Garzik 	/* reset and determine device classes */
2574c6fd2807SJeff Garzik 	ap->ops->phy_reset(ap);
2575c6fd2807SJeff Garzik 
25761eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL) {
25773e4ec344STejun Heo 		if (dev->class != ATA_DEV_UNKNOWN)
2578c6fd2807SJeff Garzik 			classes[dev->devno] = dev->class;
2579c6fd2807SJeff Garzik 		else
2580c6fd2807SJeff Garzik 			classes[dev->devno] = ATA_DEV_NONE;
2581c6fd2807SJeff Garzik 
2582c6fd2807SJeff Garzik 		dev->class = ATA_DEV_UNKNOWN;
2583c6fd2807SJeff Garzik 	}
2584c6fd2807SJeff Garzik 
2585f31f0cc2SJeff Garzik 	/* read IDENTIFY page and configure devices. We have to do the identify
2586f31f0cc2SJeff Garzik 	   specific sequence bass-ackwards so that PDIAG- is released by
2587f31f0cc2SJeff Garzik 	   the slave device */
2588f31f0cc2SJeff Garzik 
25891eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL_REVERSE) {
2590f58229f8STejun Heo 		if (tries[dev->devno])
2591f58229f8STejun Heo 			dev->class = classes[dev->devno];
2592c6fd2807SJeff Garzik 
2593c6fd2807SJeff Garzik 		if (!ata_dev_enabled(dev))
2594c6fd2807SJeff Garzik 			continue;
2595c6fd2807SJeff Garzik 
2596bff04647STejun Heo 		rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2597bff04647STejun Heo 				     dev->id);
2598c6fd2807SJeff Garzik 		if (rc)
2599c6fd2807SJeff Garzik 			goto fail;
2600f31f0cc2SJeff Garzik 	}
2601f31f0cc2SJeff Garzik 
2602be0d18dfSAlan Cox 	/* Now ask for the cable type as PDIAG- should have been released */
2603be0d18dfSAlan Cox 	if (ap->ops->cable_detect)
2604be0d18dfSAlan Cox 		ap->cbl = ap->ops->cable_detect(ap);
2605be0d18dfSAlan Cox 
26061eca4365STejun Heo 	/* We may have SATA bridge glue hiding here irrespective of
26071eca4365STejun Heo 	 * the reported cable types and sensed types.  When SATA
26081eca4365STejun Heo 	 * drives indicate we have a bridge, we don't know which end
26091eca4365STejun Heo 	 * of the link the bridge is which is a problem.
26101eca4365STejun Heo 	 */
26111eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED)
2612614fe29bSAlan Cox 		if (ata_id_is_sata(dev->id))
2613614fe29bSAlan Cox 			ap->cbl = ATA_CBL_SATA;
2614614fe29bSAlan Cox 
2615f31f0cc2SJeff Garzik 	/* After the identify sequence we can now set up the devices. We do
2616f31f0cc2SJeff Garzik 	   this in the normal order so that the user doesn't get confused */
2617f31f0cc2SJeff Garzik 
26181eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED) {
26199af5c9c9STejun Heo 		ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
2620efdaedc4STejun Heo 		rc = ata_dev_configure(dev);
26219af5c9c9STejun Heo 		ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
2622c6fd2807SJeff Garzik 		if (rc)
2623c6fd2807SJeff Garzik 			goto fail;
2624c6fd2807SJeff Garzik 	}
2625c6fd2807SJeff Garzik 
2626c6fd2807SJeff Garzik 	/* configure transfer mode */
26270260731fSTejun Heo 	rc = ata_set_mode(&ap->link, &dev);
26284ae72a1eSTejun Heo 	if (rc)
2629c6fd2807SJeff Garzik 		goto fail;
2630c6fd2807SJeff Garzik 
26311eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED)
2632c6fd2807SJeff Garzik 		return 0;
2633c6fd2807SJeff Garzik 
2634c6fd2807SJeff Garzik 	return -ENODEV;
2635c6fd2807SJeff Garzik 
2636c6fd2807SJeff Garzik  fail:
26374ae72a1eSTejun Heo 	tries[dev->devno]--;
26384ae72a1eSTejun Heo 
2639c6fd2807SJeff Garzik 	switch (rc) {
2640c6fd2807SJeff Garzik 	case -EINVAL:
26414ae72a1eSTejun Heo 		/* eeek, something went very wrong, give up */
2642c6fd2807SJeff Garzik 		tries[dev->devno] = 0;
2643c6fd2807SJeff Garzik 		break;
26444ae72a1eSTejun Heo 
26454ae72a1eSTejun Heo 	case -ENODEV:
26464ae72a1eSTejun Heo 		/* give it just one more chance */
26474ae72a1eSTejun Heo 		tries[dev->devno] = min(tries[dev->devno], 1);
2648c6fd2807SJeff Garzik 	case -EIO:
26494ae72a1eSTejun Heo 		if (tries[dev->devno] == 1) {
26504ae72a1eSTejun Heo 			/* This is the last chance, better to slow
26514ae72a1eSTejun Heo 			 * down than lose it.
26524ae72a1eSTejun Heo 			 */
2653a07d499bSTejun Heo 			sata_down_spd_limit(&ap->link, 0);
26544ae72a1eSTejun Heo 			ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
26554ae72a1eSTejun Heo 		}
2656c6fd2807SJeff Garzik 	}
2657c6fd2807SJeff Garzik 
26584ae72a1eSTejun Heo 	if (!tries[dev->devno])
2659c6fd2807SJeff Garzik 		ata_dev_disable(dev);
2660c6fd2807SJeff Garzik 
2661c6fd2807SJeff Garzik 	goto retry;
2662c6fd2807SJeff Garzik }
2663c6fd2807SJeff Garzik 
2664c6fd2807SJeff Garzik /**
2665c6fd2807SJeff Garzik  *	sata_print_link_status - Print SATA link status
2666936fd732STejun Heo  *	@link: SATA link to printk link status about
2667c6fd2807SJeff Garzik  *
2668c6fd2807SJeff Garzik  *	This function prints link speed and status of a SATA link.
2669c6fd2807SJeff Garzik  *
2670c6fd2807SJeff Garzik  *	LOCKING:
2671c6fd2807SJeff Garzik  *	None.
2672c6fd2807SJeff Garzik  */
26736bdb4fc9SAdrian Bunk static void sata_print_link_status(struct ata_link *link)
2674c6fd2807SJeff Garzik {
2675c6fd2807SJeff Garzik 	u32 sstatus, scontrol, tmp;
2676c6fd2807SJeff Garzik 
2677936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus))
2678c6fd2807SJeff Garzik 		return;
2679936fd732STejun Heo 	sata_scr_read(link, SCR_CONTROL, &scontrol);
2680c6fd2807SJeff Garzik 
2681b1c72916STejun Heo 	if (ata_phys_link_online(link)) {
2682c6fd2807SJeff Garzik 		tmp = (sstatus >> 4) & 0xf;
2683a9a79dfeSJoe Perches 		ata_link_info(link, "SATA link up %s (SStatus %X SControl %X)\n",
2684c6fd2807SJeff Garzik 			      sata_spd_string(tmp), sstatus, scontrol);
2685c6fd2807SJeff Garzik 	} else {
2686a9a79dfeSJoe Perches 		ata_link_info(link, "SATA link down (SStatus %X SControl %X)\n",
2687c6fd2807SJeff Garzik 			      sstatus, scontrol);
2688c6fd2807SJeff Garzik 	}
2689c6fd2807SJeff Garzik }
2690c6fd2807SJeff Garzik 
2691c6fd2807SJeff Garzik /**
2692c6fd2807SJeff Garzik  *	ata_dev_pair		-	return other device on cable
2693c6fd2807SJeff Garzik  *	@adev: device
2694c6fd2807SJeff Garzik  *
2695c6fd2807SJeff Garzik  *	Obtain the other device on the same cable, or if none is
2696c6fd2807SJeff Garzik  *	present NULL is returned
2697c6fd2807SJeff Garzik  */
2698c6fd2807SJeff Garzik 
2699c6fd2807SJeff Garzik struct ata_device *ata_dev_pair(struct ata_device *adev)
2700c6fd2807SJeff Garzik {
27019af5c9c9STejun Heo 	struct ata_link *link = adev->link;
27029af5c9c9STejun Heo 	struct ata_device *pair = &link->device[1 - adev->devno];
2703c6fd2807SJeff Garzik 	if (!ata_dev_enabled(pair))
2704c6fd2807SJeff Garzik 		return NULL;
2705c6fd2807SJeff Garzik 	return pair;
2706c6fd2807SJeff Garzik }
2707c6fd2807SJeff Garzik 
2708c6fd2807SJeff Garzik /**
2709c6fd2807SJeff Garzik  *	sata_down_spd_limit - adjust SATA spd limit downward
2710936fd732STejun Heo  *	@link: Link to adjust SATA spd limit for
2711a07d499bSTejun Heo  *	@spd_limit: Additional limit
2712c6fd2807SJeff Garzik  *
2713936fd732STejun Heo  *	Adjust SATA spd limit of @link downward.  Note that this
2714c6fd2807SJeff Garzik  *	function only adjusts the limit.  The change must be applied
2715c6fd2807SJeff Garzik  *	using sata_set_spd().
2716c6fd2807SJeff Garzik  *
2717a07d499bSTejun Heo  *	If @spd_limit is non-zero, the speed is limited to equal to or
2718a07d499bSTejun Heo  *	lower than @spd_limit if such speed is supported.  If
2719a07d499bSTejun Heo  *	@spd_limit is slower than any supported speed, only the lowest
2720a07d499bSTejun Heo  *	supported speed is allowed.
2721a07d499bSTejun Heo  *
2722c6fd2807SJeff Garzik  *	LOCKING:
2723c6fd2807SJeff Garzik  *	Inherited from caller.
2724c6fd2807SJeff Garzik  *
2725c6fd2807SJeff Garzik  *	RETURNS:
2726c6fd2807SJeff Garzik  *	0 on success, negative errno on failure
2727c6fd2807SJeff Garzik  */
2728a07d499bSTejun Heo int sata_down_spd_limit(struct ata_link *link, u32 spd_limit)
2729c6fd2807SJeff Garzik {
2730c6fd2807SJeff Garzik 	u32 sstatus, spd, mask;
2731a07d499bSTejun Heo 	int rc, bit;
2732c6fd2807SJeff Garzik 
2733936fd732STejun Heo 	if (!sata_scr_valid(link))
2734008a7896STejun Heo 		return -EOPNOTSUPP;
2735008a7896STejun Heo 
2736008a7896STejun Heo 	/* If SCR can be read, use it to determine the current SPD.
2737936fd732STejun Heo 	 * If not, use cached value in link->sata_spd.
2738008a7896STejun Heo 	 */
2739936fd732STejun Heo 	rc = sata_scr_read(link, SCR_STATUS, &sstatus);
27409913ff8aSTejun Heo 	if (rc == 0 && ata_sstatus_online(sstatus))
2741008a7896STejun Heo 		spd = (sstatus >> 4) & 0xf;
2742008a7896STejun Heo 	else
2743936fd732STejun Heo 		spd = link->sata_spd;
2744c6fd2807SJeff Garzik 
2745936fd732STejun Heo 	mask = link->sata_spd_limit;
2746c6fd2807SJeff Garzik 	if (mask <= 1)
2747c6fd2807SJeff Garzik 		return -EINVAL;
2748008a7896STejun Heo 
2749008a7896STejun Heo 	/* unconditionally mask off the highest bit */
2750a07d499bSTejun Heo 	bit = fls(mask) - 1;
2751a07d499bSTejun Heo 	mask &= ~(1 << bit);
2752c6fd2807SJeff Garzik 
2753008a7896STejun Heo 	/* Mask off all speeds higher than or equal to the current
2754008a7896STejun Heo 	 * one.  Force 1.5Gbps if current SPD is not available.
2755008a7896STejun Heo 	 */
2756008a7896STejun Heo 	if (spd > 1)
2757008a7896STejun Heo 		mask &= (1 << (spd - 1)) - 1;
2758008a7896STejun Heo 	else
2759008a7896STejun Heo 		mask &= 1;
2760008a7896STejun Heo 
2761008a7896STejun Heo 	/* were we already at the bottom? */
2762c6fd2807SJeff Garzik 	if (!mask)
2763c6fd2807SJeff Garzik 		return -EINVAL;
2764c6fd2807SJeff Garzik 
2765a07d499bSTejun Heo 	if (spd_limit) {
2766a07d499bSTejun Heo 		if (mask & ((1 << spd_limit) - 1))
2767a07d499bSTejun Heo 			mask &= (1 << spd_limit) - 1;
2768a07d499bSTejun Heo 		else {
2769a07d499bSTejun Heo 			bit = ffs(mask) - 1;
2770a07d499bSTejun Heo 			mask = 1 << bit;
2771a07d499bSTejun Heo 		}
2772a07d499bSTejun Heo 	}
2773a07d499bSTejun Heo 
2774936fd732STejun Heo 	link->sata_spd_limit = mask;
2775c6fd2807SJeff Garzik 
2776a9a79dfeSJoe Perches 	ata_link_warn(link, "limiting SATA link speed to %s\n",
2777c6fd2807SJeff Garzik 		      sata_spd_string(fls(mask)));
2778c6fd2807SJeff Garzik 
2779c6fd2807SJeff Garzik 	return 0;
2780c6fd2807SJeff Garzik }
2781c6fd2807SJeff Garzik 
2782936fd732STejun Heo static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
2783c6fd2807SJeff Garzik {
27845270222fSTejun Heo 	struct ata_link *host_link = &link->ap->link;
27855270222fSTejun Heo 	u32 limit, target, spd;
2786c6fd2807SJeff Garzik 
27875270222fSTejun Heo 	limit = link->sata_spd_limit;
27885270222fSTejun Heo 
27895270222fSTejun Heo 	/* Don't configure downstream link faster than upstream link.
27905270222fSTejun Heo 	 * It doesn't speed up anything and some PMPs choke on such
27915270222fSTejun Heo 	 * configuration.
27925270222fSTejun Heo 	 */
27935270222fSTejun Heo 	if (!ata_is_host_link(link) && host_link->sata_spd)
27945270222fSTejun Heo 		limit &= (1 << host_link->sata_spd) - 1;
27955270222fSTejun Heo 
27965270222fSTejun Heo 	if (limit == UINT_MAX)
27975270222fSTejun Heo 		target = 0;
2798c6fd2807SJeff Garzik 	else
27995270222fSTejun Heo 		target = fls(limit);
2800c6fd2807SJeff Garzik 
2801c6fd2807SJeff Garzik 	spd = (*scontrol >> 4) & 0xf;
28025270222fSTejun Heo 	*scontrol = (*scontrol & ~0xf0) | ((target & 0xf) << 4);
2803c6fd2807SJeff Garzik 
28045270222fSTejun Heo 	return spd != target;
2805c6fd2807SJeff Garzik }
2806c6fd2807SJeff Garzik 
2807c6fd2807SJeff Garzik /**
2808c6fd2807SJeff Garzik  *	sata_set_spd_needed - is SATA spd configuration needed
2809936fd732STejun Heo  *	@link: Link in question
2810c6fd2807SJeff Garzik  *
2811c6fd2807SJeff Garzik  *	Test whether the spd limit in SControl matches
2812936fd732STejun Heo  *	@link->sata_spd_limit.  This function is used to determine
2813c6fd2807SJeff Garzik  *	whether hardreset is necessary to apply SATA spd
2814c6fd2807SJeff Garzik  *	configuration.
2815c6fd2807SJeff Garzik  *
2816c6fd2807SJeff Garzik  *	LOCKING:
2817c6fd2807SJeff Garzik  *	Inherited from caller.
2818c6fd2807SJeff Garzik  *
2819c6fd2807SJeff Garzik  *	RETURNS:
2820c6fd2807SJeff Garzik  *	1 if SATA spd configuration is needed, 0 otherwise.
2821c6fd2807SJeff Garzik  */
28221dc55e87SAdrian Bunk static int sata_set_spd_needed(struct ata_link *link)
2823c6fd2807SJeff Garzik {
2824c6fd2807SJeff Garzik 	u32 scontrol;
2825c6fd2807SJeff Garzik 
2826936fd732STejun Heo 	if (sata_scr_read(link, SCR_CONTROL, &scontrol))
2827db64bcf3STejun Heo 		return 1;
2828c6fd2807SJeff Garzik 
2829936fd732STejun Heo 	return __sata_set_spd_needed(link, &scontrol);
2830c6fd2807SJeff Garzik }
2831c6fd2807SJeff Garzik 
2832c6fd2807SJeff Garzik /**
2833c6fd2807SJeff Garzik  *	sata_set_spd - set SATA spd according to spd limit
2834936fd732STejun Heo  *	@link: Link to set SATA spd for
2835c6fd2807SJeff Garzik  *
2836936fd732STejun Heo  *	Set SATA spd of @link according to sata_spd_limit.
2837c6fd2807SJeff Garzik  *
2838c6fd2807SJeff Garzik  *	LOCKING:
2839c6fd2807SJeff Garzik  *	Inherited from caller.
2840c6fd2807SJeff Garzik  *
2841c6fd2807SJeff Garzik  *	RETURNS:
2842c6fd2807SJeff Garzik  *	0 if spd doesn't need to be changed, 1 if spd has been
2843c6fd2807SJeff Garzik  *	changed.  Negative errno if SCR registers are inaccessible.
2844c6fd2807SJeff Garzik  */
2845936fd732STejun Heo int sata_set_spd(struct ata_link *link)
2846c6fd2807SJeff Garzik {
2847c6fd2807SJeff Garzik 	u32 scontrol;
2848c6fd2807SJeff Garzik 	int rc;
2849c6fd2807SJeff Garzik 
2850936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
2851c6fd2807SJeff Garzik 		return rc;
2852c6fd2807SJeff Garzik 
2853936fd732STejun Heo 	if (!__sata_set_spd_needed(link, &scontrol))
2854c6fd2807SJeff Garzik 		return 0;
2855c6fd2807SJeff Garzik 
2856936fd732STejun Heo 	if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
2857c6fd2807SJeff Garzik 		return rc;
2858c6fd2807SJeff Garzik 
2859c6fd2807SJeff Garzik 	return 1;
2860c6fd2807SJeff Garzik }
2861c6fd2807SJeff Garzik 
2862c6fd2807SJeff Garzik /*
2863c6fd2807SJeff Garzik  * This mode timing computation functionality is ported over from
2864c6fd2807SJeff Garzik  * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
2865c6fd2807SJeff Garzik  */
2866c6fd2807SJeff Garzik /*
2867b352e57dSAlan Cox  * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
2868c6fd2807SJeff Garzik  * These were taken from ATA/ATAPI-6 standard, rev 0a, except
2869b352e57dSAlan Cox  * for UDMA6, which is currently supported only by Maxtor drives.
2870b352e57dSAlan Cox  *
2871b352e57dSAlan Cox  * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0.
2872c6fd2807SJeff Garzik  */
2873c6fd2807SJeff Garzik 
2874c6fd2807SJeff Garzik static const struct ata_timing ata_timing[] = {
28753ada9c12SDavid Daney /*	{ XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 0,  960,   0 }, */
28763ada9c12SDavid Daney 	{ XFER_PIO_0,     70, 290, 240, 600, 165, 150, 0,  600,   0 },
28773ada9c12SDavid Daney 	{ XFER_PIO_1,     50, 290,  93, 383, 125, 100, 0,  383,   0 },
28783ada9c12SDavid Daney 	{ XFER_PIO_2,     30, 290,  40, 330, 100,  90, 0,  240,   0 },
28793ada9c12SDavid Daney 	{ XFER_PIO_3,     30,  80,  70, 180,  80,  70, 0,  180,   0 },
28803ada9c12SDavid Daney 	{ XFER_PIO_4,     25,  70,  25, 120,  70,  25, 0,  120,   0 },
28813ada9c12SDavid Daney 	{ XFER_PIO_5,     15,  65,  25, 100,  65,  25, 0,  100,   0 },
28823ada9c12SDavid Daney 	{ XFER_PIO_6,     10,  55,  20,  80,  55,  20, 0,   80,   0 },
2883c6fd2807SJeff Garzik 
28843ada9c12SDavid Daney 	{ XFER_SW_DMA_0, 120,   0,   0,   0, 480, 480, 50, 960,   0 },
28853ada9c12SDavid Daney 	{ XFER_SW_DMA_1,  90,   0,   0,   0, 240, 240, 30, 480,   0 },
28863ada9c12SDavid Daney 	{ XFER_SW_DMA_2,  60,   0,   0,   0, 120, 120, 20, 240,   0 },
2887c6fd2807SJeff Garzik 
28883ada9c12SDavid Daney 	{ XFER_MW_DMA_0,  60,   0,   0,   0, 215, 215, 20, 480,   0 },
28893ada9c12SDavid Daney 	{ XFER_MW_DMA_1,  45,   0,   0,   0,  80,  50, 5,  150,   0 },
28903ada9c12SDavid Daney 	{ XFER_MW_DMA_2,  25,   0,   0,   0,  70,  25, 5,  120,   0 },
28913ada9c12SDavid Daney 	{ XFER_MW_DMA_3,  25,   0,   0,   0,  65,  25, 5,  100,   0 },
28923ada9c12SDavid Daney 	{ XFER_MW_DMA_4,  25,   0,   0,   0,  55,  20, 5,   80,   0 },
2893c6fd2807SJeff Garzik 
28943ada9c12SDavid Daney /*	{ XFER_UDMA_SLOW,  0,   0,   0,   0,   0,   0, 0,    0, 150 }, */
28953ada9c12SDavid Daney 	{ XFER_UDMA_0,     0,   0,   0,   0,   0,   0, 0,    0, 120 },
28963ada9c12SDavid Daney 	{ XFER_UDMA_1,     0,   0,   0,   0,   0,   0, 0,    0,  80 },
28973ada9c12SDavid Daney 	{ XFER_UDMA_2,     0,   0,   0,   0,   0,   0, 0,    0,  60 },
28983ada9c12SDavid Daney 	{ XFER_UDMA_3,     0,   0,   0,   0,   0,   0, 0,    0,  45 },
28993ada9c12SDavid Daney 	{ XFER_UDMA_4,     0,   0,   0,   0,   0,   0, 0,    0,  30 },
29003ada9c12SDavid Daney 	{ XFER_UDMA_5,     0,   0,   0,   0,   0,   0, 0,    0,  20 },
29013ada9c12SDavid Daney 	{ XFER_UDMA_6,     0,   0,   0,   0,   0,   0, 0,    0,  15 },
2902c6fd2807SJeff Garzik 
2903c6fd2807SJeff Garzik 	{ 0xFF }
2904c6fd2807SJeff Garzik };
2905c6fd2807SJeff Garzik 
2906c6fd2807SJeff Garzik #define ENOUGH(v, unit)		(((v)-1)/(unit)+1)
2907c6fd2807SJeff Garzik #define EZ(v, unit)		((v)?ENOUGH(v, unit):0)
2908c6fd2807SJeff Garzik 
2909c6fd2807SJeff Garzik static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
2910c6fd2807SJeff Garzik {
2911c6fd2807SJeff Garzik 	q->setup	= EZ(t->setup      * 1000,  T);
2912c6fd2807SJeff Garzik 	q->act8b	= EZ(t->act8b      * 1000,  T);
2913c6fd2807SJeff Garzik 	q->rec8b	= EZ(t->rec8b      * 1000,  T);
2914c6fd2807SJeff Garzik 	q->cyc8b	= EZ(t->cyc8b      * 1000,  T);
2915c6fd2807SJeff Garzik 	q->active	= EZ(t->active     * 1000,  T);
2916c6fd2807SJeff Garzik 	q->recover	= EZ(t->recover    * 1000,  T);
29173ada9c12SDavid Daney 	q->dmack_hold	= EZ(t->dmack_hold * 1000,  T);
2918c6fd2807SJeff Garzik 	q->cycle	= EZ(t->cycle      * 1000,  T);
2919c6fd2807SJeff Garzik 	q->udma		= EZ(t->udma       * 1000, UT);
2920c6fd2807SJeff Garzik }
2921c6fd2807SJeff Garzik 
2922c6fd2807SJeff Garzik void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
2923c6fd2807SJeff Garzik 		      struct ata_timing *m, unsigned int what)
2924c6fd2807SJeff Garzik {
2925c6fd2807SJeff Garzik 	if (what & ATA_TIMING_SETUP  ) m->setup   = max(a->setup,   b->setup);
2926c6fd2807SJeff Garzik 	if (what & ATA_TIMING_ACT8B  ) m->act8b   = max(a->act8b,   b->act8b);
2927c6fd2807SJeff Garzik 	if (what & ATA_TIMING_REC8B  ) m->rec8b   = max(a->rec8b,   b->rec8b);
2928c6fd2807SJeff Garzik 	if (what & ATA_TIMING_CYC8B  ) m->cyc8b   = max(a->cyc8b,   b->cyc8b);
2929c6fd2807SJeff Garzik 	if (what & ATA_TIMING_ACTIVE ) m->active  = max(a->active,  b->active);
2930c6fd2807SJeff Garzik 	if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
29313ada9c12SDavid Daney 	if (what & ATA_TIMING_DMACK_HOLD) m->dmack_hold = max(a->dmack_hold, b->dmack_hold);
2932c6fd2807SJeff Garzik 	if (what & ATA_TIMING_CYCLE  ) m->cycle   = max(a->cycle,   b->cycle);
2933c6fd2807SJeff Garzik 	if (what & ATA_TIMING_UDMA   ) m->udma    = max(a->udma,    b->udma);
2934c6fd2807SJeff Garzik }
2935c6fd2807SJeff Garzik 
29366357357cSTejun Heo const struct ata_timing *ata_timing_find_mode(u8 xfer_mode)
2937c6fd2807SJeff Garzik {
293870cd071eSTejun Heo 	const struct ata_timing *t = ata_timing;
2939c6fd2807SJeff Garzik 
294070cd071eSTejun Heo 	while (xfer_mode > t->mode)
294170cd071eSTejun Heo 		t++;
294270cd071eSTejun Heo 
294370cd071eSTejun Heo 	if (xfer_mode == t->mode)
2944c6fd2807SJeff Garzik 		return t;
2945cd705d5aSBorislav Petkov 
2946cd705d5aSBorislav Petkov 	WARN_ONCE(true, "%s: unable to find timing for xfer_mode 0x%x\n",
2947cd705d5aSBorislav Petkov 			__func__, xfer_mode);
2948cd705d5aSBorislav Petkov 
294970cd071eSTejun Heo 	return NULL;
2950c6fd2807SJeff Garzik }
2951c6fd2807SJeff Garzik 
2952c6fd2807SJeff Garzik int ata_timing_compute(struct ata_device *adev, unsigned short speed,
2953c6fd2807SJeff Garzik 		       struct ata_timing *t, int T, int UT)
2954c6fd2807SJeff Garzik {
29559e8808a9SBartlomiej Zolnierkiewicz 	const u16 *id = adev->id;
2956c6fd2807SJeff Garzik 	const struct ata_timing *s;
2957c6fd2807SJeff Garzik 	struct ata_timing p;
2958c6fd2807SJeff Garzik 
2959c6fd2807SJeff Garzik 	/*
2960c6fd2807SJeff Garzik 	 * Find the mode.
2961c6fd2807SJeff Garzik 	 */
2962c6fd2807SJeff Garzik 
2963c6fd2807SJeff Garzik 	if (!(s = ata_timing_find_mode(speed)))
2964c6fd2807SJeff Garzik 		return -EINVAL;
2965c6fd2807SJeff Garzik 
2966c6fd2807SJeff Garzik 	memcpy(t, s, sizeof(*s));
2967c6fd2807SJeff Garzik 
2968c6fd2807SJeff Garzik 	/*
2969c6fd2807SJeff Garzik 	 * If the drive is an EIDE drive, it can tell us it needs extended
2970c6fd2807SJeff Garzik 	 * PIO/MW_DMA cycle timing.
2971c6fd2807SJeff Garzik 	 */
2972c6fd2807SJeff Garzik 
29739e8808a9SBartlomiej Zolnierkiewicz 	if (id[ATA_ID_FIELD_VALID] & 2) {	/* EIDE drive */
2974c6fd2807SJeff Garzik 		memset(&p, 0, sizeof(p));
29759e8808a9SBartlomiej Zolnierkiewicz 
2976bff00256SBartlomiej Zolnierkiewicz 		if (speed >= XFER_PIO_0 && speed < XFER_SW_DMA_0) {
29779e8808a9SBartlomiej Zolnierkiewicz 			if (speed <= XFER_PIO_2)
29789e8808a9SBartlomiej Zolnierkiewicz 				p.cycle = p.cyc8b = id[ATA_ID_EIDE_PIO];
29799e8808a9SBartlomiej Zolnierkiewicz 			else if ((speed <= XFER_PIO_4) ||
29809e8808a9SBartlomiej Zolnierkiewicz 				 (speed == XFER_PIO_5 && !ata_id_is_cfa(id)))
29819e8808a9SBartlomiej Zolnierkiewicz 				p.cycle = p.cyc8b = id[ATA_ID_EIDE_PIO_IORDY];
29829e8808a9SBartlomiej Zolnierkiewicz 		} else if (speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2)
29839e8808a9SBartlomiej Zolnierkiewicz 			p.cycle = id[ATA_ID_EIDE_DMA_MIN];
29849e8808a9SBartlomiej Zolnierkiewicz 
2985c6fd2807SJeff Garzik 		ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
2986c6fd2807SJeff Garzik 	}
2987c6fd2807SJeff Garzik 
2988c6fd2807SJeff Garzik 	/*
2989c6fd2807SJeff Garzik 	 * Convert the timing to bus clock counts.
2990c6fd2807SJeff Garzik 	 */
2991c6fd2807SJeff Garzik 
2992c6fd2807SJeff Garzik 	ata_timing_quantize(t, t, T, UT);
2993c6fd2807SJeff Garzik 
2994c6fd2807SJeff Garzik 	/*
2995c6fd2807SJeff Garzik 	 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
2996c6fd2807SJeff Garzik 	 * S.M.A.R.T * and some other commands. We have to ensure that the
2997c6fd2807SJeff Garzik 	 * DMA cycle timing is slower/equal than the fastest PIO timing.
2998c6fd2807SJeff Garzik 	 */
2999c6fd2807SJeff Garzik 
3000fd3367afSAlan 	if (speed > XFER_PIO_6) {
3001c6fd2807SJeff Garzik 		ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
3002c6fd2807SJeff Garzik 		ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
3003c6fd2807SJeff Garzik 	}
3004c6fd2807SJeff Garzik 
3005c6fd2807SJeff Garzik 	/*
3006c6fd2807SJeff Garzik 	 * Lengthen active & recovery time so that cycle time is correct.
3007c6fd2807SJeff Garzik 	 */
3008c6fd2807SJeff Garzik 
3009c6fd2807SJeff Garzik 	if (t->act8b + t->rec8b < t->cyc8b) {
3010c6fd2807SJeff Garzik 		t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
3011c6fd2807SJeff Garzik 		t->rec8b = t->cyc8b - t->act8b;
3012c6fd2807SJeff Garzik 	}
3013c6fd2807SJeff Garzik 
3014c6fd2807SJeff Garzik 	if (t->active + t->recover < t->cycle) {
3015c6fd2807SJeff Garzik 		t->active += (t->cycle - (t->active + t->recover)) / 2;
3016c6fd2807SJeff Garzik 		t->recover = t->cycle - t->active;
3017c6fd2807SJeff Garzik 	}
30184f701d1eSAlan Cox 
30194f701d1eSAlan Cox 	/* In a few cases quantisation may produce enough errors to
30204f701d1eSAlan Cox 	   leave t->cycle too low for the sum of active and recovery
30214f701d1eSAlan Cox 	   if so we must correct this */
30224f701d1eSAlan Cox 	if (t->active + t->recover > t->cycle)
30234f701d1eSAlan Cox 		t->cycle = t->active + t->recover;
3024c6fd2807SJeff Garzik 
3025c6fd2807SJeff Garzik 	return 0;
3026c6fd2807SJeff Garzik }
3027c6fd2807SJeff Garzik 
3028c6fd2807SJeff Garzik /**
3029a0f79b92STejun Heo  *	ata_timing_cycle2mode - find xfer mode for the specified cycle duration
3030a0f79b92STejun Heo  *	@xfer_shift: ATA_SHIFT_* value for transfer type to examine.
3031a0f79b92STejun Heo  *	@cycle: cycle duration in ns
3032a0f79b92STejun Heo  *
3033a0f79b92STejun Heo  *	Return matching xfer mode for @cycle.  The returned mode is of
3034a0f79b92STejun Heo  *	the transfer type specified by @xfer_shift.  If @cycle is too
3035a0f79b92STejun Heo  *	slow for @xfer_shift, 0xff is returned.  If @cycle is faster
3036a0f79b92STejun Heo  *	than the fastest known mode, the fasted mode is returned.
3037a0f79b92STejun Heo  *
3038a0f79b92STejun Heo  *	LOCKING:
3039a0f79b92STejun Heo  *	None.
3040a0f79b92STejun Heo  *
3041a0f79b92STejun Heo  *	RETURNS:
3042a0f79b92STejun Heo  *	Matching xfer_mode, 0xff if no match found.
3043a0f79b92STejun Heo  */
3044a0f79b92STejun Heo u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
3045a0f79b92STejun Heo {
3046a0f79b92STejun Heo 	u8 base_mode = 0xff, last_mode = 0xff;
3047a0f79b92STejun Heo 	const struct ata_xfer_ent *ent;
3048a0f79b92STejun Heo 	const struct ata_timing *t;
3049a0f79b92STejun Heo 
3050a0f79b92STejun Heo 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
3051a0f79b92STejun Heo 		if (ent->shift == xfer_shift)
3052a0f79b92STejun Heo 			base_mode = ent->base;
3053a0f79b92STejun Heo 
3054a0f79b92STejun Heo 	for (t = ata_timing_find_mode(base_mode);
3055a0f79b92STejun Heo 	     t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
3056a0f79b92STejun Heo 		unsigned short this_cycle;
3057a0f79b92STejun Heo 
3058a0f79b92STejun Heo 		switch (xfer_shift) {
3059a0f79b92STejun Heo 		case ATA_SHIFT_PIO:
3060a0f79b92STejun Heo 		case ATA_SHIFT_MWDMA:
3061a0f79b92STejun Heo 			this_cycle = t->cycle;
3062a0f79b92STejun Heo 			break;
3063a0f79b92STejun Heo 		case ATA_SHIFT_UDMA:
3064a0f79b92STejun Heo 			this_cycle = t->udma;
3065a0f79b92STejun Heo 			break;
3066a0f79b92STejun Heo 		default:
3067a0f79b92STejun Heo 			return 0xff;
3068a0f79b92STejun Heo 		}
3069a0f79b92STejun Heo 
3070a0f79b92STejun Heo 		if (cycle > this_cycle)
3071a0f79b92STejun Heo 			break;
3072a0f79b92STejun Heo 
3073a0f79b92STejun Heo 		last_mode = t->mode;
3074a0f79b92STejun Heo 	}
3075a0f79b92STejun Heo 
3076a0f79b92STejun Heo 	return last_mode;
3077a0f79b92STejun Heo }
3078a0f79b92STejun Heo 
3079a0f79b92STejun Heo /**
3080c6fd2807SJeff Garzik  *	ata_down_xfermask_limit - adjust dev xfer masks downward
3081c6fd2807SJeff Garzik  *	@dev: Device to adjust xfer masks
3082458337dbSTejun Heo  *	@sel: ATA_DNXFER_* selector
3083c6fd2807SJeff Garzik  *
3084c6fd2807SJeff Garzik  *	Adjust xfer masks of @dev downward.  Note that this function
3085c6fd2807SJeff Garzik  *	does not apply the change.  Invoking ata_set_mode() afterwards
3086c6fd2807SJeff Garzik  *	will apply the limit.
3087c6fd2807SJeff Garzik  *
3088c6fd2807SJeff Garzik  *	LOCKING:
3089c6fd2807SJeff Garzik  *	Inherited from caller.
3090c6fd2807SJeff Garzik  *
3091c6fd2807SJeff Garzik  *	RETURNS:
3092c6fd2807SJeff Garzik  *	0 on success, negative errno on failure
3093c6fd2807SJeff Garzik  */
3094458337dbSTejun Heo int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
3095c6fd2807SJeff Garzik {
3096458337dbSTejun Heo 	char buf[32];
30977dc951aeSTejun Heo 	unsigned long orig_mask, xfer_mask;
30987dc951aeSTejun Heo 	unsigned long pio_mask, mwdma_mask, udma_mask;
3099458337dbSTejun Heo 	int quiet, highbit;
3100c6fd2807SJeff Garzik 
3101458337dbSTejun Heo 	quiet = !!(sel & ATA_DNXFER_QUIET);
3102458337dbSTejun Heo 	sel &= ~ATA_DNXFER_QUIET;
3103458337dbSTejun Heo 
3104458337dbSTejun Heo 	xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
3105458337dbSTejun Heo 						  dev->mwdma_mask,
3106c6fd2807SJeff Garzik 						  dev->udma_mask);
3107458337dbSTejun Heo 	ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
3108c6fd2807SJeff Garzik 
3109458337dbSTejun Heo 	switch (sel) {
3110458337dbSTejun Heo 	case ATA_DNXFER_PIO:
3111458337dbSTejun Heo 		highbit = fls(pio_mask) - 1;
3112458337dbSTejun Heo 		pio_mask &= ~(1 << highbit);
3113458337dbSTejun Heo 		break;
3114458337dbSTejun Heo 
3115458337dbSTejun Heo 	case ATA_DNXFER_DMA:
3116458337dbSTejun Heo 		if (udma_mask) {
3117458337dbSTejun Heo 			highbit = fls(udma_mask) - 1;
3118458337dbSTejun Heo 			udma_mask &= ~(1 << highbit);
3119458337dbSTejun Heo 			if (!udma_mask)
3120458337dbSTejun Heo 				return -ENOENT;
3121458337dbSTejun Heo 		} else if (mwdma_mask) {
3122458337dbSTejun Heo 			highbit = fls(mwdma_mask) - 1;
3123458337dbSTejun Heo 			mwdma_mask &= ~(1 << highbit);
3124458337dbSTejun Heo 			if (!mwdma_mask)
3125458337dbSTejun Heo 				return -ENOENT;
3126458337dbSTejun Heo 		}
3127458337dbSTejun Heo 		break;
3128458337dbSTejun Heo 
3129458337dbSTejun Heo 	case ATA_DNXFER_40C:
3130458337dbSTejun Heo 		udma_mask &= ATA_UDMA_MASK_40C;
3131458337dbSTejun Heo 		break;
3132458337dbSTejun Heo 
3133458337dbSTejun Heo 	case ATA_DNXFER_FORCE_PIO0:
3134458337dbSTejun Heo 		pio_mask &= 1;
3135458337dbSTejun Heo 	case ATA_DNXFER_FORCE_PIO:
3136458337dbSTejun Heo 		mwdma_mask = 0;
3137458337dbSTejun Heo 		udma_mask = 0;
3138458337dbSTejun Heo 		break;
3139458337dbSTejun Heo 
3140458337dbSTejun Heo 	default:
3141458337dbSTejun Heo 		BUG();
3142458337dbSTejun Heo 	}
3143458337dbSTejun Heo 
3144458337dbSTejun Heo 	xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
3145458337dbSTejun Heo 
3146458337dbSTejun Heo 	if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
3147458337dbSTejun Heo 		return -ENOENT;
3148458337dbSTejun Heo 
3149458337dbSTejun Heo 	if (!quiet) {
3150458337dbSTejun Heo 		if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
3151458337dbSTejun Heo 			snprintf(buf, sizeof(buf), "%s:%s",
3152458337dbSTejun Heo 				 ata_mode_string(xfer_mask),
3153458337dbSTejun Heo 				 ata_mode_string(xfer_mask & ATA_MASK_PIO));
3154458337dbSTejun Heo 		else
3155458337dbSTejun Heo 			snprintf(buf, sizeof(buf), "%s",
3156458337dbSTejun Heo 				 ata_mode_string(xfer_mask));
3157458337dbSTejun Heo 
3158a9a79dfeSJoe Perches 		ata_dev_warn(dev, "limiting speed to %s\n", buf);
3159458337dbSTejun Heo 	}
3160c6fd2807SJeff Garzik 
3161c6fd2807SJeff Garzik 	ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
3162c6fd2807SJeff Garzik 			    &dev->udma_mask);
3163c6fd2807SJeff Garzik 
3164c6fd2807SJeff Garzik 	return 0;
3165c6fd2807SJeff Garzik }
3166c6fd2807SJeff Garzik 
3167c6fd2807SJeff Garzik static int ata_dev_set_mode(struct ata_device *dev)
3168c6fd2807SJeff Garzik {
3169d0cb43b3STejun Heo 	struct ata_port *ap = dev->link->ap;
31709af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
3171d0cb43b3STejun Heo 	const bool nosetxfer = dev->horkage & ATA_HORKAGE_NOSETXFER;
31724055dee7STejun Heo 	const char *dev_err_whine = "";
31734055dee7STejun Heo 	int ign_dev_err = 0;
3174d0cb43b3STejun Heo 	unsigned int err_mask = 0;
3175c6fd2807SJeff Garzik 	int rc;
3176c6fd2807SJeff Garzik 
3177c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_PIO;
3178c6fd2807SJeff Garzik 	if (dev->xfer_shift == ATA_SHIFT_PIO)
3179c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_PIO;
3180c6fd2807SJeff Garzik 
3181d0cb43b3STejun Heo 	if (nosetxfer && ap->flags & ATA_FLAG_SATA && ata_id_is_sata(dev->id))
3182d0cb43b3STejun Heo 		dev_err_whine = " (SET_XFERMODE skipped)";
3183d0cb43b3STejun Heo 	else {
3184d0cb43b3STejun Heo 		if (nosetxfer)
3185a9a79dfeSJoe Perches 			ata_dev_warn(dev,
3186d0cb43b3STejun Heo 				     "NOSETXFER but PATA detected - can't "
3187d0cb43b3STejun Heo 				     "skip SETXFER, might malfunction\n");
3188c6fd2807SJeff Garzik 		err_mask = ata_dev_set_xfermode(dev);
3189d0cb43b3STejun Heo 	}
31902dcb407eSJeff Garzik 
31914055dee7STejun Heo 	if (err_mask & ~AC_ERR_DEV)
31924055dee7STejun Heo 		goto fail;
31932dcb407eSJeff Garzik 
31944055dee7STejun Heo 	/* revalidate */
3195baa1e78aSTejun Heo 	ehc->i.flags |= ATA_EHI_POST_SETMODE;
3196422c9daaSTejun Heo 	rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
3197baa1e78aSTejun Heo 	ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
3198c6fd2807SJeff Garzik 	if (rc)
3199c6fd2807SJeff Garzik 		return rc;
3200c6fd2807SJeff Garzik 
3201b93fda12SAlan Cox 	if (dev->xfer_shift == ATA_SHIFT_PIO) {
32024055dee7STejun Heo 		/* Old CFA may refuse this command, which is just fine */
3203b93fda12SAlan Cox 		if (ata_id_is_cfa(dev->id))
32044055dee7STejun Heo 			ign_dev_err = 1;
3205b93fda12SAlan Cox 		/* Catch several broken garbage emulations plus some pre
3206b93fda12SAlan Cox 		   ATA devices */
3207b93fda12SAlan Cox 		if (ata_id_major_version(dev->id) == 0 &&
32084055dee7STejun Heo 					dev->pio_mode <= XFER_PIO_2)
32094055dee7STejun Heo 			ign_dev_err = 1;
3210b93fda12SAlan Cox 		/* Some very old devices and some bad newer ones fail
3211b93fda12SAlan Cox 		   any kind of SET_XFERMODE request but support PIO0-2
3212b93fda12SAlan Cox 		   timings and no IORDY */
3213b93fda12SAlan Cox 		if (!ata_id_has_iordy(dev->id) && dev->pio_mode <= XFER_PIO_2)
3214b93fda12SAlan Cox 			ign_dev_err = 1;
3215b93fda12SAlan Cox 	}
32164055dee7STejun Heo 	/* Early MWDMA devices do DMA but don't allow DMA mode setting.
32174055dee7STejun Heo 	   Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
32184055dee7STejun Heo 	if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
32194055dee7STejun Heo 	    dev->dma_mode == XFER_MW_DMA_0 &&
32204055dee7STejun Heo 	    (dev->id[63] >> 8) & 1)
32214055dee7STejun Heo 		ign_dev_err = 1;
32224055dee7STejun Heo 
32234055dee7STejun Heo 	/* if the device is actually configured correctly, ignore dev err */
32244055dee7STejun Heo 	if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id)))
32254055dee7STejun Heo 		ign_dev_err = 1;
32264055dee7STejun Heo 
32274055dee7STejun Heo 	if (err_mask & AC_ERR_DEV) {
32284055dee7STejun Heo 		if (!ign_dev_err)
32294055dee7STejun Heo 			goto fail;
32304055dee7STejun Heo 		else
32314055dee7STejun Heo 			dev_err_whine = " (device error ignored)";
32324055dee7STejun Heo 	}
32334055dee7STejun Heo 
3234c6fd2807SJeff Garzik 	DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
3235c6fd2807SJeff Garzik 		dev->xfer_shift, (int)dev->xfer_mode);
3236c6fd2807SJeff Garzik 
3237a9a79dfeSJoe Perches 	ata_dev_info(dev, "configured for %s%s\n",
32384055dee7STejun Heo 		     ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)),
32394055dee7STejun Heo 		     dev_err_whine);
32404055dee7STejun Heo 
3241c6fd2807SJeff Garzik 	return 0;
32424055dee7STejun Heo 
32434055dee7STejun Heo  fail:
3244a9a79dfeSJoe Perches 	ata_dev_err(dev, "failed to set xfermode (err_mask=0x%x)\n", err_mask);
32454055dee7STejun Heo 	return -EIO;
3246c6fd2807SJeff Garzik }
3247c6fd2807SJeff Garzik 
3248c6fd2807SJeff Garzik /**
324904351821SAlan  *	ata_do_set_mode - Program timings and issue SET FEATURES - XFER
32500260731fSTejun Heo  *	@link: link on which timings will be programmed
32511967b7ffSJoe Perches  *	@r_failed_dev: out parameter for failed device
3252c6fd2807SJeff Garzik  *
325304351821SAlan  *	Standard implementation of the function used to tune and set
325404351821SAlan  *	ATA device disk transfer mode (PIO3, UDMA6, etc.).  If
325504351821SAlan  *	ata_dev_set_mode() fails, pointer to the failing device is
3256c6fd2807SJeff Garzik  *	returned in @r_failed_dev.
3257c6fd2807SJeff Garzik  *
3258c6fd2807SJeff Garzik  *	LOCKING:
3259c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
3260c6fd2807SJeff Garzik  *
3261c6fd2807SJeff Garzik  *	RETURNS:
3262c6fd2807SJeff Garzik  *	0 on success, negative errno otherwise
3263c6fd2807SJeff Garzik  */
326404351821SAlan 
32650260731fSTejun Heo int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
3266c6fd2807SJeff Garzik {
32670260731fSTejun Heo 	struct ata_port *ap = link->ap;
3268c6fd2807SJeff Garzik 	struct ata_device *dev;
3269f58229f8STejun Heo 	int rc = 0, used_dma = 0, found = 0;
3270c6fd2807SJeff Garzik 
3271c6fd2807SJeff Garzik 	/* step 1: calculate xfer_mask */
32721eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
32737dc951aeSTejun Heo 		unsigned long pio_mask, dma_mask;
3274b3a70601SAlan Cox 		unsigned int mode_mask;
3275c6fd2807SJeff Garzik 
3276b3a70601SAlan Cox 		mode_mask = ATA_DMA_MASK_ATA;
3277b3a70601SAlan Cox 		if (dev->class == ATA_DEV_ATAPI)
3278b3a70601SAlan Cox 			mode_mask = ATA_DMA_MASK_ATAPI;
3279b3a70601SAlan Cox 		else if (ata_id_is_cfa(dev->id))
3280b3a70601SAlan Cox 			mode_mask = ATA_DMA_MASK_CFA;
3281b3a70601SAlan Cox 
3282c6fd2807SJeff Garzik 		ata_dev_xfermask(dev);
328333267325STejun Heo 		ata_force_xfermask(dev);
3284c6fd2807SJeff Garzik 
3285c6fd2807SJeff Garzik 		pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
3286b3a70601SAlan Cox 
3287b3a70601SAlan Cox 		if (libata_dma_mask & mode_mask)
328880a9c430SSergei Shtylyov 			dma_mask = ata_pack_xfermask(0, dev->mwdma_mask,
328980a9c430SSergei Shtylyov 						     dev->udma_mask);
3290b3a70601SAlan Cox 		else
3291b3a70601SAlan Cox 			dma_mask = 0;
3292b3a70601SAlan Cox 
3293c6fd2807SJeff Garzik 		dev->pio_mode = ata_xfer_mask2mode(pio_mask);
3294c6fd2807SJeff Garzik 		dev->dma_mode = ata_xfer_mask2mode(dma_mask);
3295c6fd2807SJeff Garzik 
3296c6fd2807SJeff Garzik 		found = 1;
3297b15b3ebaSAlan Cox 		if (ata_dma_enabled(dev))
3298c6fd2807SJeff Garzik 			used_dma = 1;
3299c6fd2807SJeff Garzik 	}
3300c6fd2807SJeff Garzik 	if (!found)
3301c6fd2807SJeff Garzik 		goto out;
3302c6fd2807SJeff Garzik 
3303c6fd2807SJeff Garzik 	/* step 2: always set host PIO timings */
33041eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
330570cd071eSTejun Heo 		if (dev->pio_mode == 0xff) {
3306a9a79dfeSJoe Perches 			ata_dev_warn(dev, "no PIO support\n");
3307c6fd2807SJeff Garzik 			rc = -EINVAL;
3308c6fd2807SJeff Garzik 			goto out;
3309c6fd2807SJeff Garzik 		}
3310c6fd2807SJeff Garzik 
3311c6fd2807SJeff Garzik 		dev->xfer_mode = dev->pio_mode;
3312c6fd2807SJeff Garzik 		dev->xfer_shift = ATA_SHIFT_PIO;
3313c6fd2807SJeff Garzik 		if (ap->ops->set_piomode)
3314c6fd2807SJeff Garzik 			ap->ops->set_piomode(ap, dev);
3315c6fd2807SJeff Garzik 	}
3316c6fd2807SJeff Garzik 
3317c6fd2807SJeff Garzik 	/* step 3: set host DMA timings */
33181eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
33191eca4365STejun Heo 		if (!ata_dma_enabled(dev))
3320c6fd2807SJeff Garzik 			continue;
3321c6fd2807SJeff Garzik 
3322c6fd2807SJeff Garzik 		dev->xfer_mode = dev->dma_mode;
3323c6fd2807SJeff Garzik 		dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
3324c6fd2807SJeff Garzik 		if (ap->ops->set_dmamode)
3325c6fd2807SJeff Garzik 			ap->ops->set_dmamode(ap, dev);
3326c6fd2807SJeff Garzik 	}
3327c6fd2807SJeff Garzik 
3328c6fd2807SJeff Garzik 	/* step 4: update devices' xfer mode */
33291eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
3330c6fd2807SJeff Garzik 		rc = ata_dev_set_mode(dev);
3331c6fd2807SJeff Garzik 		if (rc)
3332c6fd2807SJeff Garzik 			goto out;
3333c6fd2807SJeff Garzik 	}
3334c6fd2807SJeff Garzik 
3335c6fd2807SJeff Garzik 	/* Record simplex status. If we selected DMA then the other
3336c6fd2807SJeff Garzik 	 * host channels are not permitted to do so.
3337c6fd2807SJeff Garzik 	 */
3338cca3974eSJeff Garzik 	if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
3339032af1ceSAlan 		ap->host->simplex_claimed = ap;
3340c6fd2807SJeff Garzik 
3341c6fd2807SJeff Garzik  out:
3342c6fd2807SJeff Garzik 	if (rc)
3343c6fd2807SJeff Garzik 		*r_failed_dev = dev;
3344c6fd2807SJeff Garzik 	return rc;
3345c6fd2807SJeff Garzik }
3346c6fd2807SJeff Garzik 
3347c6fd2807SJeff Garzik /**
3348aa2731adSTejun Heo  *	ata_wait_ready - wait for link to become ready
3349aa2731adSTejun Heo  *	@link: link to be waited on
3350aa2731adSTejun Heo  *	@deadline: deadline jiffies for the operation
3351aa2731adSTejun Heo  *	@check_ready: callback to check link readiness
3352aa2731adSTejun Heo  *
3353aa2731adSTejun Heo  *	Wait for @link to become ready.  @check_ready should return
3354aa2731adSTejun Heo  *	positive number if @link is ready, 0 if it isn't, -ENODEV if
3355aa2731adSTejun Heo  *	link doesn't seem to be occupied, other errno for other error
3356aa2731adSTejun Heo  *	conditions.
3357aa2731adSTejun Heo  *
3358aa2731adSTejun Heo  *	Transient -ENODEV conditions are allowed for
3359aa2731adSTejun Heo  *	ATA_TMOUT_FF_WAIT.
3360aa2731adSTejun Heo  *
3361aa2731adSTejun Heo  *	LOCKING:
3362aa2731adSTejun Heo  *	EH context.
3363aa2731adSTejun Heo  *
3364aa2731adSTejun Heo  *	RETURNS:
3365aa2731adSTejun Heo  *	0 if @linke is ready before @deadline; otherwise, -errno.
3366aa2731adSTejun Heo  */
3367aa2731adSTejun Heo int ata_wait_ready(struct ata_link *link, unsigned long deadline,
3368aa2731adSTejun Heo 		   int (*check_ready)(struct ata_link *link))
3369aa2731adSTejun Heo {
3370aa2731adSTejun Heo 	unsigned long start = jiffies;
3371b48d58f5STejun Heo 	unsigned long nodev_deadline;
3372aa2731adSTejun Heo 	int warned = 0;
3373aa2731adSTejun Heo 
3374b48d58f5STejun Heo 	/* choose which 0xff timeout to use, read comment in libata.h */
3375b48d58f5STejun Heo 	if (link->ap->host->flags & ATA_HOST_PARALLEL_SCAN)
3376b48d58f5STejun Heo 		nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT_LONG);
3377b48d58f5STejun Heo 	else
3378b48d58f5STejun Heo 		nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT);
3379b48d58f5STejun Heo 
3380b1c72916STejun Heo 	/* Slave readiness can't be tested separately from master.  On
3381b1c72916STejun Heo 	 * M/S emulation configuration, this function should be called
3382b1c72916STejun Heo 	 * only on the master and it will handle both master and slave.
3383b1c72916STejun Heo 	 */
3384b1c72916STejun Heo 	WARN_ON(link == link->ap->slave_link);
3385b1c72916STejun Heo 
3386aa2731adSTejun Heo 	if (time_after(nodev_deadline, deadline))
3387aa2731adSTejun Heo 		nodev_deadline = deadline;
3388aa2731adSTejun Heo 
3389aa2731adSTejun Heo 	while (1) {
3390aa2731adSTejun Heo 		unsigned long now = jiffies;
3391aa2731adSTejun Heo 		int ready, tmp;
3392aa2731adSTejun Heo 
3393aa2731adSTejun Heo 		ready = tmp = check_ready(link);
3394aa2731adSTejun Heo 		if (ready > 0)
3395aa2731adSTejun Heo 			return 0;
3396aa2731adSTejun Heo 
3397b48d58f5STejun Heo 		/*
3398b48d58f5STejun Heo 		 * -ENODEV could be transient.  Ignore -ENODEV if link
3399aa2731adSTejun Heo 		 * is online.  Also, some SATA devices take a long
3400b48d58f5STejun Heo 		 * time to clear 0xff after reset.  Wait for
3401b48d58f5STejun Heo 		 * ATA_TMOUT_FF_WAIT[_LONG] on -ENODEV if link isn't
3402b48d58f5STejun Heo 		 * offline.
3403aa2731adSTejun Heo 		 *
3404aa2731adSTejun Heo 		 * Note that some PATA controllers (pata_ali) explode
3405aa2731adSTejun Heo 		 * if status register is read more than once when
3406aa2731adSTejun Heo 		 * there's no device attached.
3407aa2731adSTejun Heo 		 */
3408aa2731adSTejun Heo 		if (ready == -ENODEV) {
3409aa2731adSTejun Heo 			if (ata_link_online(link))
3410aa2731adSTejun Heo 				ready = 0;
3411aa2731adSTejun Heo 			else if ((link->ap->flags & ATA_FLAG_SATA) &&
3412aa2731adSTejun Heo 				 !ata_link_offline(link) &&
3413aa2731adSTejun Heo 				 time_before(now, nodev_deadline))
3414aa2731adSTejun Heo 				ready = 0;
3415aa2731adSTejun Heo 		}
3416aa2731adSTejun Heo 
3417aa2731adSTejun Heo 		if (ready)
3418aa2731adSTejun Heo 			return ready;
3419aa2731adSTejun Heo 		if (time_after(now, deadline))
3420aa2731adSTejun Heo 			return -EBUSY;
3421aa2731adSTejun Heo 
3422aa2731adSTejun Heo 		if (!warned && time_after(now, start + 5 * HZ) &&
3423aa2731adSTejun Heo 		    (deadline - now > 3 * HZ)) {
3424a9a79dfeSJoe Perches 			ata_link_warn(link,
3425aa2731adSTejun Heo 				"link is slow to respond, please be patient "
3426aa2731adSTejun Heo 				"(ready=%d)\n", tmp);
3427aa2731adSTejun Heo 			warned = 1;
3428aa2731adSTejun Heo 		}
3429aa2731adSTejun Heo 
343097750cebSTejun Heo 		ata_msleep(link->ap, 50);
3431aa2731adSTejun Heo 	}
3432aa2731adSTejun Heo }
3433aa2731adSTejun Heo 
3434aa2731adSTejun Heo /**
3435aa2731adSTejun Heo  *	ata_wait_after_reset - wait for link to become ready after reset
3436aa2731adSTejun Heo  *	@link: link to be waited on
3437aa2731adSTejun Heo  *	@deadline: deadline jiffies for the operation
3438aa2731adSTejun Heo  *	@check_ready: callback to check link readiness
3439aa2731adSTejun Heo  *
3440aa2731adSTejun Heo  *	Wait for @link to become ready after reset.
3441aa2731adSTejun Heo  *
3442aa2731adSTejun Heo  *	LOCKING:
3443aa2731adSTejun Heo  *	EH context.
3444aa2731adSTejun Heo  *
3445aa2731adSTejun Heo  *	RETURNS:
3446aa2731adSTejun Heo  *	0 if @linke is ready before @deadline; otherwise, -errno.
3447aa2731adSTejun Heo  */
34482b4221bbSHarvey Harrison int ata_wait_after_reset(struct ata_link *link, unsigned long deadline,
3449aa2731adSTejun Heo 				int (*check_ready)(struct ata_link *link))
3450aa2731adSTejun Heo {
345197750cebSTejun Heo 	ata_msleep(link->ap, ATA_WAIT_AFTER_RESET);
3452aa2731adSTejun Heo 
3453aa2731adSTejun Heo 	return ata_wait_ready(link, deadline, check_ready);
3454aa2731adSTejun Heo }
3455aa2731adSTejun Heo 
3456aa2731adSTejun Heo /**
3457936fd732STejun Heo  *	sata_link_debounce - debounce SATA phy status
3458936fd732STejun Heo  *	@link: ATA link to debounce SATA phy status for
3459c6fd2807SJeff Garzik  *	@params: timing parameters { interval, duratinon, timeout } in msec
3460d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
3461c6fd2807SJeff Garzik  *
3462936fd732STejun Heo  *	Make sure SStatus of @link reaches stable state, determined by
3463c6fd2807SJeff Garzik  *	holding the same value where DET is not 1 for @duration polled
3464c6fd2807SJeff Garzik  *	every @interval, before @timeout.  Timeout constraints the
3465d4b2bab4STejun Heo  *	beginning of the stable state.  Because DET gets stuck at 1 on
3466d4b2bab4STejun Heo  *	some controllers after hot unplugging, this functions waits
3467c6fd2807SJeff Garzik  *	until timeout then returns 0 if DET is stable at 1.
3468c6fd2807SJeff Garzik  *
3469d4b2bab4STejun Heo  *	@timeout is further limited by @deadline.  The sooner of the
3470d4b2bab4STejun Heo  *	two is used.
3471d4b2bab4STejun Heo  *
3472c6fd2807SJeff Garzik  *	LOCKING:
3473c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3474c6fd2807SJeff Garzik  *
3475c6fd2807SJeff Garzik  *	RETURNS:
3476c6fd2807SJeff Garzik  *	0 on success, -errno on failure.
3477c6fd2807SJeff Garzik  */
3478936fd732STejun Heo int sata_link_debounce(struct ata_link *link, const unsigned long *params,
3479d4b2bab4STejun Heo 		       unsigned long deadline)
3480c6fd2807SJeff Garzik {
3481341c2c95STejun Heo 	unsigned long interval = params[0];
3482341c2c95STejun Heo 	unsigned long duration = params[1];
3483d4b2bab4STejun Heo 	unsigned long last_jiffies, t;
3484c6fd2807SJeff Garzik 	u32 last, cur;
3485c6fd2807SJeff Garzik 	int rc;
3486c6fd2807SJeff Garzik 
3487341c2c95STejun Heo 	t = ata_deadline(jiffies, params[2]);
3488d4b2bab4STejun Heo 	if (time_before(t, deadline))
3489d4b2bab4STejun Heo 		deadline = t;
3490d4b2bab4STejun Heo 
3491936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3492c6fd2807SJeff Garzik 		return rc;
3493c6fd2807SJeff Garzik 	cur &= 0xf;
3494c6fd2807SJeff Garzik 
3495c6fd2807SJeff Garzik 	last = cur;
3496c6fd2807SJeff Garzik 	last_jiffies = jiffies;
3497c6fd2807SJeff Garzik 
3498c6fd2807SJeff Garzik 	while (1) {
349997750cebSTejun Heo 		ata_msleep(link->ap, interval);
3500936fd732STejun Heo 		if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3501c6fd2807SJeff Garzik 			return rc;
3502c6fd2807SJeff Garzik 		cur &= 0xf;
3503c6fd2807SJeff Garzik 
3504c6fd2807SJeff Garzik 		/* DET stable? */
3505c6fd2807SJeff Garzik 		if (cur == last) {
3506d4b2bab4STejun Heo 			if (cur == 1 && time_before(jiffies, deadline))
3507c6fd2807SJeff Garzik 				continue;
3508341c2c95STejun Heo 			if (time_after(jiffies,
3509341c2c95STejun Heo 				       ata_deadline(last_jiffies, duration)))
3510c6fd2807SJeff Garzik 				return 0;
3511c6fd2807SJeff Garzik 			continue;
3512c6fd2807SJeff Garzik 		}
3513c6fd2807SJeff Garzik 
3514c6fd2807SJeff Garzik 		/* unstable, start over */
3515c6fd2807SJeff Garzik 		last = cur;
3516c6fd2807SJeff Garzik 		last_jiffies = jiffies;
3517c6fd2807SJeff Garzik 
3518f1545154STejun Heo 		/* Check deadline.  If debouncing failed, return
3519f1545154STejun Heo 		 * -EPIPE to tell upper layer to lower link speed.
3520f1545154STejun Heo 		 */
3521d4b2bab4STejun Heo 		if (time_after(jiffies, deadline))
3522f1545154STejun Heo 			return -EPIPE;
3523c6fd2807SJeff Garzik 	}
3524c6fd2807SJeff Garzik }
3525c6fd2807SJeff Garzik 
3526c6fd2807SJeff Garzik /**
3527936fd732STejun Heo  *	sata_link_resume - resume SATA link
3528936fd732STejun Heo  *	@link: ATA link to resume SATA
3529c6fd2807SJeff Garzik  *	@params: timing parameters { interval, duratinon, timeout } in msec
3530d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
3531c6fd2807SJeff Garzik  *
3532936fd732STejun Heo  *	Resume SATA phy @link and debounce it.
3533c6fd2807SJeff Garzik  *
3534c6fd2807SJeff Garzik  *	LOCKING:
3535c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3536c6fd2807SJeff Garzik  *
3537c6fd2807SJeff Garzik  *	RETURNS:
3538c6fd2807SJeff Garzik  *	0 on success, -errno on failure.
3539c6fd2807SJeff Garzik  */
3540936fd732STejun Heo int sata_link_resume(struct ata_link *link, const unsigned long *params,
3541d4b2bab4STejun Heo 		     unsigned long deadline)
3542c6fd2807SJeff Garzik {
35435040ab67STejun Heo 	int tries = ATA_LINK_RESUME_TRIES;
3544ac371987STejun Heo 	u32 scontrol, serror;
3545c6fd2807SJeff Garzik 	int rc;
3546c6fd2807SJeff Garzik 
3547936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3548c6fd2807SJeff Garzik 		return rc;
3549c6fd2807SJeff Garzik 
35505040ab67STejun Heo 	/*
35515040ab67STejun Heo 	 * Writes to SControl sometimes get ignored under certain
35525040ab67STejun Heo 	 * controllers (ata_piix SIDPR).  Make sure DET actually is
35535040ab67STejun Heo 	 * cleared.
35545040ab67STejun Heo 	 */
35555040ab67STejun Heo 	do {
3556c6fd2807SJeff Garzik 		scontrol = (scontrol & 0x0f0) | 0x300;
3557936fd732STejun Heo 		if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3558c6fd2807SJeff Garzik 			return rc;
35595040ab67STejun Heo 		/*
35605040ab67STejun Heo 		 * Some PHYs react badly if SStatus is pounded
35615040ab67STejun Heo 		 * immediately after resuming.  Delay 200ms before
35625040ab67STejun Heo 		 * debouncing.
3563c6fd2807SJeff Garzik 		 */
356497750cebSTejun Heo 		ata_msleep(link->ap, 200);
3565c6fd2807SJeff Garzik 
35665040ab67STejun Heo 		/* is SControl restored correctly? */
35675040ab67STejun Heo 		if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
35685040ab67STejun Heo 			return rc;
35695040ab67STejun Heo 	} while ((scontrol & 0xf0f) != 0x300 && --tries);
35705040ab67STejun Heo 
35715040ab67STejun Heo 	if ((scontrol & 0xf0f) != 0x300) {
357238941c95STejun Heo 		ata_link_warn(link, "failed to resume link (SControl %X)\n",
35735040ab67STejun Heo 			     scontrol);
35745040ab67STejun Heo 		return 0;
35755040ab67STejun Heo 	}
35765040ab67STejun Heo 
35775040ab67STejun Heo 	if (tries < ATA_LINK_RESUME_TRIES)
3578a9a79dfeSJoe Perches 		ata_link_warn(link, "link resume succeeded after %d retries\n",
35795040ab67STejun Heo 			      ATA_LINK_RESUME_TRIES - tries);
35805040ab67STejun Heo 
3581ac371987STejun Heo 	if ((rc = sata_link_debounce(link, params, deadline)))
3582ac371987STejun Heo 		return rc;
3583ac371987STejun Heo 
3584f046519fSTejun Heo 	/* clear SError, some PHYs require this even for SRST to work */
3585ac371987STejun Heo 	if (!(rc = sata_scr_read(link, SCR_ERROR, &serror)))
3586ac371987STejun Heo 		rc = sata_scr_write(link, SCR_ERROR, serror);
3587ac371987STejun Heo 
3588f046519fSTejun Heo 	return rc != -EINVAL ? rc : 0;
3589c6fd2807SJeff Garzik }
3590c6fd2807SJeff Garzik 
3591c6fd2807SJeff Garzik /**
35921152b261STejun Heo  *	sata_link_scr_lpm - manipulate SControl IPM and SPM fields
35931152b261STejun Heo  *	@link: ATA link to manipulate SControl for
35941152b261STejun Heo  *	@policy: LPM policy to configure
35951152b261STejun Heo  *	@spm_wakeup: initiate LPM transition to active state
35961152b261STejun Heo  *
35971152b261STejun Heo  *	Manipulate the IPM field of the SControl register of @link
35981152b261STejun Heo  *	according to @policy.  If @policy is ATA_LPM_MAX_POWER and
35991152b261STejun Heo  *	@spm_wakeup is %true, the SPM field is manipulated to wake up
36001152b261STejun Heo  *	the link.  This function also clears PHYRDY_CHG before
36011152b261STejun Heo  *	returning.
36021152b261STejun Heo  *
36031152b261STejun Heo  *	LOCKING:
36041152b261STejun Heo  *	EH context.
36051152b261STejun Heo  *
36061152b261STejun Heo  *	RETURNS:
36071152b261STejun Heo  *	0 on succes, -errno otherwise.
36081152b261STejun Heo  */
36091152b261STejun Heo int sata_link_scr_lpm(struct ata_link *link, enum ata_lpm_policy policy,
36101152b261STejun Heo 		      bool spm_wakeup)
36111152b261STejun Heo {
36121152b261STejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
36131152b261STejun Heo 	bool woken_up = false;
36141152b261STejun Heo 	u32 scontrol;
36151152b261STejun Heo 	int rc;
36161152b261STejun Heo 
36171152b261STejun Heo 	rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
36181152b261STejun Heo 	if (rc)
36191152b261STejun Heo 		return rc;
36201152b261STejun Heo 
36211152b261STejun Heo 	switch (policy) {
36221152b261STejun Heo 	case ATA_LPM_MAX_POWER:
36231152b261STejun Heo 		/* disable all LPM transitions */
362465fe1f0fSShane Huang 		scontrol |= (0x7 << 8);
36251152b261STejun Heo 		/* initiate transition to active state */
36261152b261STejun Heo 		if (spm_wakeup) {
36271152b261STejun Heo 			scontrol |= (0x4 << 12);
36281152b261STejun Heo 			woken_up = true;
36291152b261STejun Heo 		}
36301152b261STejun Heo 		break;
36311152b261STejun Heo 	case ATA_LPM_MED_POWER:
36321152b261STejun Heo 		/* allow LPM to PARTIAL */
36331152b261STejun Heo 		scontrol &= ~(0x1 << 8);
363465fe1f0fSShane Huang 		scontrol |= (0x6 << 8);
36351152b261STejun Heo 		break;
36361152b261STejun Heo 	case ATA_LPM_MIN_POWER:
36378a745f1fSKristen Carlson Accardi 		if (ata_link_nr_enabled(link) > 0)
36381152b261STejun Heo 			/* no restrictions on LPM transitions */
363965fe1f0fSShane Huang 			scontrol &= ~(0x7 << 8);
36408a745f1fSKristen Carlson Accardi 		else {
36418a745f1fSKristen Carlson Accardi 			/* empty port, power off */
36428a745f1fSKristen Carlson Accardi 			scontrol &= ~0xf;
36438a745f1fSKristen Carlson Accardi 			scontrol |= (0x1 << 2);
36448a745f1fSKristen Carlson Accardi 		}
36451152b261STejun Heo 		break;
36461152b261STejun Heo 	default:
36471152b261STejun Heo 		WARN_ON(1);
36481152b261STejun Heo 	}
36491152b261STejun Heo 
36501152b261STejun Heo 	rc = sata_scr_write(link, SCR_CONTROL, scontrol);
36511152b261STejun Heo 	if (rc)
36521152b261STejun Heo 		return rc;
36531152b261STejun Heo 
36541152b261STejun Heo 	/* give the link time to transit out of LPM state */
36551152b261STejun Heo 	if (woken_up)
36561152b261STejun Heo 		msleep(10);
36571152b261STejun Heo 
36581152b261STejun Heo 	/* clear PHYRDY_CHG from SError */
36591152b261STejun Heo 	ehc->i.serror &= ~SERR_PHYRDY_CHG;
36601152b261STejun Heo 	return sata_scr_write(link, SCR_ERROR, SERR_PHYRDY_CHG);
36611152b261STejun Heo }
36621152b261STejun Heo 
36631152b261STejun Heo /**
36640aa1113dSTejun Heo  *	ata_std_prereset - prepare for reset
3665cc0680a5STejun Heo  *	@link: ATA link to be reset
3666d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
3667c6fd2807SJeff Garzik  *
3668cc0680a5STejun Heo  *	@link is about to be reset.  Initialize it.  Failure from
3669b8cffc6aSTejun Heo  *	prereset makes libata abort whole reset sequence and give up
3670b8cffc6aSTejun Heo  *	that port, so prereset should be best-effort.  It does its
3671b8cffc6aSTejun Heo  *	best to prepare for reset sequence but if things go wrong, it
3672b8cffc6aSTejun Heo  *	should just whine, not fail.
3673c6fd2807SJeff Garzik  *
3674c6fd2807SJeff Garzik  *	LOCKING:
3675c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3676c6fd2807SJeff Garzik  *
3677c6fd2807SJeff Garzik  *	RETURNS:
3678c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
3679c6fd2807SJeff Garzik  */
36800aa1113dSTejun Heo int ata_std_prereset(struct ata_link *link, unsigned long deadline)
3681c6fd2807SJeff Garzik {
3682cc0680a5STejun Heo 	struct ata_port *ap = link->ap;
3683936fd732STejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
3684c6fd2807SJeff Garzik 	const unsigned long *timing = sata_ehc_deb_timing(ehc);
3685c6fd2807SJeff Garzik 	int rc;
3686c6fd2807SJeff Garzik 
3687c6fd2807SJeff Garzik 	/* if we're about to do hardreset, nothing more to do */
3688c6fd2807SJeff Garzik 	if (ehc->i.action & ATA_EH_HARDRESET)
3689c6fd2807SJeff Garzik 		return 0;
3690c6fd2807SJeff Garzik 
3691936fd732STejun Heo 	/* if SATA, resume link */
3692a16abc0bSTejun Heo 	if (ap->flags & ATA_FLAG_SATA) {
3693936fd732STejun Heo 		rc = sata_link_resume(link, timing, deadline);
3694b8cffc6aSTejun Heo 		/* whine about phy resume failure but proceed */
3695b8cffc6aSTejun Heo 		if (rc && rc != -EOPNOTSUPP)
3696a9a79dfeSJoe Perches 			ata_link_warn(link,
3697a9a79dfeSJoe Perches 				      "failed to resume link for reset (errno=%d)\n",
3698a9a79dfeSJoe Perches 				      rc);
3699c6fd2807SJeff Garzik 	}
3700c6fd2807SJeff Garzik 
370145db2f6cSTejun Heo 	/* no point in trying softreset on offline link */
3702b1c72916STejun Heo 	if (ata_phys_link_offline(link))
370345db2f6cSTejun Heo 		ehc->i.action &= ~ATA_EH_SOFTRESET;
370445db2f6cSTejun Heo 
3705c6fd2807SJeff Garzik 	return 0;
3706c6fd2807SJeff Garzik }
3707c6fd2807SJeff Garzik 
3708c6fd2807SJeff Garzik /**
3709cc0680a5STejun Heo  *	sata_link_hardreset - reset link via SATA phy reset
3710cc0680a5STejun Heo  *	@link: link to reset
3711b6103f6dSTejun Heo  *	@timing: timing parameters { interval, duratinon, timeout } in msec
3712d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
37139dadd45bSTejun Heo  *	@online: optional out parameter indicating link onlineness
37149dadd45bSTejun Heo  *	@check_ready: optional callback to check link readiness
3715c6fd2807SJeff Garzik  *
3716cc0680a5STejun Heo  *	SATA phy-reset @link using DET bits of SControl register.
37179dadd45bSTejun Heo  *	After hardreset, link readiness is waited upon using
37189dadd45bSTejun Heo  *	ata_wait_ready() if @check_ready is specified.  LLDs are
37199dadd45bSTejun Heo  *	allowed to not specify @check_ready and wait itself after this
37209dadd45bSTejun Heo  *	function returns.  Device classification is LLD's
37219dadd45bSTejun Heo  *	responsibility.
37229dadd45bSTejun Heo  *
37239dadd45bSTejun Heo  *	*@online is set to one iff reset succeeded and @link is online
37249dadd45bSTejun Heo  *	after reset.
3725c6fd2807SJeff Garzik  *
3726c6fd2807SJeff Garzik  *	LOCKING:
3727c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3728c6fd2807SJeff Garzik  *
3729c6fd2807SJeff Garzik  *	RETURNS:
3730c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
3731c6fd2807SJeff Garzik  */
3732cc0680a5STejun Heo int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
37339dadd45bSTejun Heo 			unsigned long deadline,
37349dadd45bSTejun Heo 			bool *online, int (*check_ready)(struct ata_link *))
3735c6fd2807SJeff Garzik {
3736c6fd2807SJeff Garzik 	u32 scontrol;
3737c6fd2807SJeff Garzik 	int rc;
3738c6fd2807SJeff Garzik 
3739c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
3740c6fd2807SJeff Garzik 
37419dadd45bSTejun Heo 	if (online)
37429dadd45bSTejun Heo 		*online = false;
37439dadd45bSTejun Heo 
3744936fd732STejun Heo 	if (sata_set_spd_needed(link)) {
3745c6fd2807SJeff Garzik 		/* SATA spec says nothing about how to reconfigure
3746c6fd2807SJeff Garzik 		 * spd.  To be on the safe side, turn off phy during
3747c6fd2807SJeff Garzik 		 * reconfiguration.  This works for at least ICH7 AHCI
3748c6fd2807SJeff Garzik 		 * and Sil3124.
3749c6fd2807SJeff Garzik 		 */
3750936fd732STejun Heo 		if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3751b6103f6dSTejun Heo 			goto out;
3752c6fd2807SJeff Garzik 
3753cea0d336SJeff Garzik 		scontrol = (scontrol & 0x0f0) | 0x304;
3754c6fd2807SJeff Garzik 
3755936fd732STejun Heo 		if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3756b6103f6dSTejun Heo 			goto out;
3757c6fd2807SJeff Garzik 
3758936fd732STejun Heo 		sata_set_spd(link);
3759c6fd2807SJeff Garzik 	}
3760c6fd2807SJeff Garzik 
3761c6fd2807SJeff Garzik 	/* issue phy wake/reset */
3762936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3763b6103f6dSTejun Heo 		goto out;
3764c6fd2807SJeff Garzik 
3765c6fd2807SJeff Garzik 	scontrol = (scontrol & 0x0f0) | 0x301;
3766c6fd2807SJeff Garzik 
3767936fd732STejun Heo 	if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
3768b6103f6dSTejun Heo 		goto out;
3769c6fd2807SJeff Garzik 
3770c6fd2807SJeff Garzik 	/* Couldn't find anything in SATA I/II specs, but AHCI-1.1
3771c6fd2807SJeff Garzik 	 * 10.4.2 says at least 1 ms.
3772c6fd2807SJeff Garzik 	 */
377397750cebSTejun Heo 	ata_msleep(link->ap, 1);
3774c6fd2807SJeff Garzik 
3775936fd732STejun Heo 	/* bring link back */
3776936fd732STejun Heo 	rc = sata_link_resume(link, timing, deadline);
37779dadd45bSTejun Heo 	if (rc)
37789dadd45bSTejun Heo 		goto out;
37799dadd45bSTejun Heo 	/* if link is offline nothing more to do */
3780b1c72916STejun Heo 	if (ata_phys_link_offline(link))
37819dadd45bSTejun Heo 		goto out;
37829dadd45bSTejun Heo 
37839dadd45bSTejun Heo 	/* Link is online.  From this point, -ENODEV too is an error. */
37849dadd45bSTejun Heo 	if (online)
37859dadd45bSTejun Heo 		*online = true;
37869dadd45bSTejun Heo 
3787071f44b1STejun Heo 	if (sata_pmp_supported(link->ap) && ata_is_host_link(link)) {
37889dadd45bSTejun Heo 		/* If PMP is supported, we have to do follow-up SRST.
37899dadd45bSTejun Heo 		 * Some PMPs don't send D2H Reg FIS after hardreset if
37909dadd45bSTejun Heo 		 * the first port is empty.  Wait only for
37919dadd45bSTejun Heo 		 * ATA_TMOUT_PMP_SRST_WAIT.
37929dadd45bSTejun Heo 		 */
37939dadd45bSTejun Heo 		if (check_ready) {
37949dadd45bSTejun Heo 			unsigned long pmp_deadline;
37959dadd45bSTejun Heo 
3796341c2c95STejun Heo 			pmp_deadline = ata_deadline(jiffies,
3797341c2c95STejun Heo 						    ATA_TMOUT_PMP_SRST_WAIT);
37989dadd45bSTejun Heo 			if (time_after(pmp_deadline, deadline))
37999dadd45bSTejun Heo 				pmp_deadline = deadline;
38009dadd45bSTejun Heo 			ata_wait_ready(link, pmp_deadline, check_ready);
38019dadd45bSTejun Heo 		}
38029dadd45bSTejun Heo 		rc = -EAGAIN;
38039dadd45bSTejun Heo 		goto out;
38049dadd45bSTejun Heo 	}
38059dadd45bSTejun Heo 
38069dadd45bSTejun Heo 	rc = 0;
38079dadd45bSTejun Heo 	if (check_ready)
38089dadd45bSTejun Heo 		rc = ata_wait_ready(link, deadline, check_ready);
3809b6103f6dSTejun Heo  out:
38100cbf0711STejun Heo 	if (rc && rc != -EAGAIN) {
38110cbf0711STejun Heo 		/* online is set iff link is online && reset succeeded */
38120cbf0711STejun Heo 		if (online)
38130cbf0711STejun Heo 			*online = false;
3814a9a79dfeSJoe Perches 		ata_link_err(link, "COMRESET failed (errno=%d)\n", rc);
38150cbf0711STejun Heo 	}
3816b6103f6dSTejun Heo 	DPRINTK("EXIT, rc=%d\n", rc);
3817b6103f6dSTejun Heo 	return rc;
3818b6103f6dSTejun Heo }
3819b6103f6dSTejun Heo 
3820b6103f6dSTejun Heo /**
382157c9efdfSTejun Heo  *	sata_std_hardreset - COMRESET w/o waiting or classification
382257c9efdfSTejun Heo  *	@link: link to reset
382357c9efdfSTejun Heo  *	@class: resulting class of attached device
382457c9efdfSTejun Heo  *	@deadline: deadline jiffies for the operation
382557c9efdfSTejun Heo  *
382657c9efdfSTejun Heo  *	Standard SATA COMRESET w/o waiting or classification.
382757c9efdfSTejun Heo  *
382857c9efdfSTejun Heo  *	LOCKING:
382957c9efdfSTejun Heo  *	Kernel thread context (may sleep)
383057c9efdfSTejun Heo  *
383157c9efdfSTejun Heo  *	RETURNS:
383257c9efdfSTejun Heo  *	0 if link offline, -EAGAIN if link online, -errno on errors.
383357c9efdfSTejun Heo  */
383457c9efdfSTejun Heo int sata_std_hardreset(struct ata_link *link, unsigned int *class,
383557c9efdfSTejun Heo 		       unsigned long deadline)
383657c9efdfSTejun Heo {
383757c9efdfSTejun Heo 	const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
383857c9efdfSTejun Heo 	bool online;
383957c9efdfSTejun Heo 	int rc;
384057c9efdfSTejun Heo 
384157c9efdfSTejun Heo 	/* do hardreset */
384257c9efdfSTejun Heo 	rc = sata_link_hardreset(link, timing, deadline, &online, NULL);
384357c9efdfSTejun Heo 	return online ? -EAGAIN : rc;
384457c9efdfSTejun Heo }
384557c9efdfSTejun Heo 
384657c9efdfSTejun Heo /**
3847203c75b8STejun Heo  *	ata_std_postreset - standard postreset callback
3848cc0680a5STejun Heo  *	@link: the target ata_link
3849c6fd2807SJeff Garzik  *	@classes: classes of attached devices
3850c6fd2807SJeff Garzik  *
3851c6fd2807SJeff Garzik  *	This function is invoked after a successful reset.  Note that
3852c6fd2807SJeff Garzik  *	the device might have been reset more than once using
3853c6fd2807SJeff Garzik  *	different reset methods before postreset is invoked.
3854c6fd2807SJeff Garzik  *
3855c6fd2807SJeff Garzik  *	LOCKING:
3856c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3857c6fd2807SJeff Garzik  */
3858203c75b8STejun Heo void ata_std_postreset(struct ata_link *link, unsigned int *classes)
3859c6fd2807SJeff Garzik {
3860f046519fSTejun Heo 	u32 serror;
3861f046519fSTejun Heo 
3862c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
3863c6fd2807SJeff Garzik 
3864f046519fSTejun Heo 	/* reset complete, clear SError */
3865f046519fSTejun Heo 	if (!sata_scr_read(link, SCR_ERROR, &serror))
3866f046519fSTejun Heo 		sata_scr_write(link, SCR_ERROR, serror);
3867f046519fSTejun Heo 
3868c6fd2807SJeff Garzik 	/* print link status */
3869936fd732STejun Heo 	sata_print_link_status(link);
3870c6fd2807SJeff Garzik 
3871c6fd2807SJeff Garzik 	DPRINTK("EXIT\n");
3872c6fd2807SJeff Garzik }
3873c6fd2807SJeff Garzik 
3874c6fd2807SJeff Garzik /**
3875c6fd2807SJeff Garzik  *	ata_dev_same_device - Determine whether new ID matches configured device
3876c6fd2807SJeff Garzik  *	@dev: device to compare against
3877c6fd2807SJeff Garzik  *	@new_class: class of the new device
3878c6fd2807SJeff Garzik  *	@new_id: IDENTIFY page of the new device
3879c6fd2807SJeff Garzik  *
3880c6fd2807SJeff Garzik  *	Compare @new_class and @new_id against @dev and determine
3881c6fd2807SJeff Garzik  *	whether @dev is the device indicated by @new_class and
3882c6fd2807SJeff Garzik  *	@new_id.
3883c6fd2807SJeff Garzik  *
3884c6fd2807SJeff Garzik  *	LOCKING:
3885c6fd2807SJeff Garzik  *	None.
3886c6fd2807SJeff Garzik  *
3887c6fd2807SJeff Garzik  *	RETURNS:
3888c6fd2807SJeff Garzik  *	1 if @dev matches @new_class and @new_id, 0 otherwise.
3889c6fd2807SJeff Garzik  */
3890c6fd2807SJeff Garzik static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
3891c6fd2807SJeff Garzik 			       const u16 *new_id)
3892c6fd2807SJeff Garzik {
3893c6fd2807SJeff Garzik 	const u16 *old_id = dev->id;
3894a0cf733bSTejun Heo 	unsigned char model[2][ATA_ID_PROD_LEN + 1];
3895a0cf733bSTejun Heo 	unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
3896c6fd2807SJeff Garzik 
3897c6fd2807SJeff Garzik 	if (dev->class != new_class) {
3898a9a79dfeSJoe Perches 		ata_dev_info(dev, "class mismatch %d != %d\n",
3899c6fd2807SJeff Garzik 			     dev->class, new_class);
3900c6fd2807SJeff Garzik 		return 0;
3901c6fd2807SJeff Garzik 	}
3902c6fd2807SJeff Garzik 
3903a0cf733bSTejun Heo 	ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
3904a0cf733bSTejun Heo 	ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
3905a0cf733bSTejun Heo 	ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
3906a0cf733bSTejun Heo 	ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
3907c6fd2807SJeff Garzik 
3908c6fd2807SJeff Garzik 	if (strcmp(model[0], model[1])) {
3909a9a79dfeSJoe Perches 		ata_dev_info(dev, "model number mismatch '%s' != '%s'\n",
3910a9a79dfeSJoe Perches 			     model[0], model[1]);
3911c6fd2807SJeff Garzik 		return 0;
3912c6fd2807SJeff Garzik 	}
3913c6fd2807SJeff Garzik 
3914c6fd2807SJeff Garzik 	if (strcmp(serial[0], serial[1])) {
3915a9a79dfeSJoe Perches 		ata_dev_info(dev, "serial number mismatch '%s' != '%s'\n",
3916a9a79dfeSJoe Perches 			     serial[0], serial[1]);
3917c6fd2807SJeff Garzik 		return 0;
3918c6fd2807SJeff Garzik 	}
3919c6fd2807SJeff Garzik 
3920c6fd2807SJeff Garzik 	return 1;
3921c6fd2807SJeff Garzik }
3922c6fd2807SJeff Garzik 
3923c6fd2807SJeff Garzik /**
3924fe30911bSTejun Heo  *	ata_dev_reread_id - Re-read IDENTIFY data
39253fae450cSHenrik Kretzschmar  *	@dev: target ATA device
3926bff04647STejun Heo  *	@readid_flags: read ID flags
3927c6fd2807SJeff Garzik  *
3928c6fd2807SJeff Garzik  *	Re-read IDENTIFY page and make sure @dev is still attached to
3929c6fd2807SJeff Garzik  *	the port.
3930c6fd2807SJeff Garzik  *
3931c6fd2807SJeff Garzik  *	LOCKING:
3932c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3933c6fd2807SJeff Garzik  *
3934c6fd2807SJeff Garzik  *	RETURNS:
3935c6fd2807SJeff Garzik  *	0 on success, negative errno otherwise
3936c6fd2807SJeff Garzik  */
3937fe30911bSTejun Heo int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
3938c6fd2807SJeff Garzik {
3939c6fd2807SJeff Garzik 	unsigned int class = dev->class;
39409af5c9c9STejun Heo 	u16 *id = (void *)dev->link->ap->sector_buf;
3941c6fd2807SJeff Garzik 	int rc;
3942c6fd2807SJeff Garzik 
3943c6fd2807SJeff Garzik 	/* read ID data */
3944bff04647STejun Heo 	rc = ata_dev_read_id(dev, &class, readid_flags, id);
3945c6fd2807SJeff Garzik 	if (rc)
3946fe30911bSTejun Heo 		return rc;
3947c6fd2807SJeff Garzik 
3948c6fd2807SJeff Garzik 	/* is the device still there? */
3949fe30911bSTejun Heo 	if (!ata_dev_same_device(dev, class, id))
3950fe30911bSTejun Heo 		return -ENODEV;
3951c6fd2807SJeff Garzik 
3952c6fd2807SJeff Garzik 	memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
3953fe30911bSTejun Heo 	return 0;
3954fe30911bSTejun Heo }
3955fe30911bSTejun Heo 
3956fe30911bSTejun Heo /**
3957fe30911bSTejun Heo  *	ata_dev_revalidate - Revalidate ATA device
3958fe30911bSTejun Heo  *	@dev: device to revalidate
3959422c9daaSTejun Heo  *	@new_class: new class code
3960fe30911bSTejun Heo  *	@readid_flags: read ID flags
3961fe30911bSTejun Heo  *
3962fe30911bSTejun Heo  *	Re-read IDENTIFY page, make sure @dev is still attached to the
3963fe30911bSTejun Heo  *	port and reconfigure it according to the new IDENTIFY page.
3964fe30911bSTejun Heo  *
3965fe30911bSTejun Heo  *	LOCKING:
3966fe30911bSTejun Heo  *	Kernel thread context (may sleep)
3967fe30911bSTejun Heo  *
3968fe30911bSTejun Heo  *	RETURNS:
3969fe30911bSTejun Heo  *	0 on success, negative errno otherwise
3970fe30911bSTejun Heo  */
3971422c9daaSTejun Heo int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
3972422c9daaSTejun Heo 		       unsigned int readid_flags)
3973fe30911bSTejun Heo {
39746ddcd3b0STejun Heo 	u64 n_sectors = dev->n_sectors;
39755920dadfSTejun Heo 	u64 n_native_sectors = dev->n_native_sectors;
3976fe30911bSTejun Heo 	int rc;
3977fe30911bSTejun Heo 
3978fe30911bSTejun Heo 	if (!ata_dev_enabled(dev))
3979fe30911bSTejun Heo 		return -ENODEV;
3980fe30911bSTejun Heo 
3981422c9daaSTejun Heo 	/* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
3982422c9daaSTejun Heo 	if (ata_class_enabled(new_class) &&
3983f0d0613dSBorislav Petkov 	    new_class != ATA_DEV_ATA &&
3984f0d0613dSBorislav Petkov 	    new_class != ATA_DEV_ATAPI &&
3985f0d0613dSBorislav Petkov 	    new_class != ATA_DEV_SEMB) {
3986a9a79dfeSJoe Perches 		ata_dev_info(dev, "class mismatch %u != %u\n",
3987422c9daaSTejun Heo 			     dev->class, new_class);
3988422c9daaSTejun Heo 		rc = -ENODEV;
3989422c9daaSTejun Heo 		goto fail;
3990422c9daaSTejun Heo 	}
3991422c9daaSTejun Heo 
3992fe30911bSTejun Heo 	/* re-read ID */
3993fe30911bSTejun Heo 	rc = ata_dev_reread_id(dev, readid_flags);
3994fe30911bSTejun Heo 	if (rc)
3995fe30911bSTejun Heo 		goto fail;
3996c6fd2807SJeff Garzik 
3997c6fd2807SJeff Garzik 	/* configure device according to the new ID */
3998efdaedc4STejun Heo 	rc = ata_dev_configure(dev);
39996ddcd3b0STejun Heo 	if (rc)
40006ddcd3b0STejun Heo 		goto fail;
40016ddcd3b0STejun Heo 
40026ddcd3b0STejun Heo 	/* verify n_sectors hasn't changed */
4003445d211bSTejun Heo 	if (dev->class != ATA_DEV_ATA || !n_sectors ||
4004445d211bSTejun Heo 	    dev->n_sectors == n_sectors)
4005445d211bSTejun Heo 		return 0;
4006445d211bSTejun Heo 
4007445d211bSTejun Heo 	/* n_sectors has changed */
4008a9a79dfeSJoe Perches 	ata_dev_warn(dev, "n_sectors mismatch %llu != %llu\n",
40096ddcd3b0STejun Heo 		     (unsigned long long)n_sectors,
40106ddcd3b0STejun Heo 		     (unsigned long long)dev->n_sectors);
4011445d211bSTejun Heo 
40125920dadfSTejun Heo 	/*
40135920dadfSTejun Heo 	 * Something could have caused HPA to be unlocked
4014445d211bSTejun Heo 	 * involuntarily.  If n_native_sectors hasn't changed and the
4015445d211bSTejun Heo 	 * new size matches it, keep the device.
40165920dadfSTejun Heo 	 */
40175920dadfSTejun Heo 	if (dev->n_native_sectors == n_native_sectors &&
4018445d211bSTejun Heo 	    dev->n_sectors > n_sectors && dev->n_sectors == n_native_sectors) {
4019a9a79dfeSJoe Perches 		ata_dev_warn(dev,
40205920dadfSTejun Heo 			     "new n_sectors matches native, probably "
402168939ce5STejun Heo 			     "late HPA unlock, n_sectors updated\n");
402268939ce5STejun Heo 		/* use the larger n_sectors */
4023445d211bSTejun Heo 		return 0;
4024445d211bSTejun Heo 	}
4025445d211bSTejun Heo 
4026445d211bSTejun Heo 	/*
4027445d211bSTejun Heo 	 * Some BIOSes boot w/o HPA but resume w/ HPA locked.  Try
4028445d211bSTejun Heo 	 * unlocking HPA in those cases.
4029445d211bSTejun Heo 	 *
4030445d211bSTejun Heo 	 * https://bugzilla.kernel.org/show_bug.cgi?id=15396
4031445d211bSTejun Heo 	 */
4032445d211bSTejun Heo 	if (dev->n_native_sectors == n_native_sectors &&
4033445d211bSTejun Heo 	    dev->n_sectors < n_sectors && n_sectors == n_native_sectors &&
4034445d211bSTejun Heo 	    !(dev->horkage & ATA_HORKAGE_BROKEN_HPA)) {
4035a9a79dfeSJoe Perches 		ata_dev_warn(dev,
4036445d211bSTejun Heo 			     "old n_sectors matches native, probably "
4037445d211bSTejun Heo 			     "late HPA lock, will try to unlock HPA\n");
4038445d211bSTejun Heo 		/* try unlocking HPA */
4039445d211bSTejun Heo 		dev->flags |= ATA_DFLAG_UNLOCK_HPA;
4040445d211bSTejun Heo 		rc = -EIO;
4041445d211bSTejun Heo 	} else
4042445d211bSTejun Heo 		rc = -ENODEV;
4043445d211bSTejun Heo 
4044445d211bSTejun Heo 	/* restore original n_[native_]sectors and fail */
40455920dadfSTejun Heo 	dev->n_native_sectors = n_native_sectors;
40465920dadfSTejun Heo 	dev->n_sectors = n_sectors;
4047c6fd2807SJeff Garzik  fail:
4048a9a79dfeSJoe Perches 	ata_dev_err(dev, "revalidation failed (errno=%d)\n", rc);
4049c6fd2807SJeff Garzik 	return rc;
4050c6fd2807SJeff Garzik }
4051c6fd2807SJeff Garzik 
40526919a0a6SAlan Cox struct ata_blacklist_entry {
40536919a0a6SAlan Cox 	const char *model_num;
40546919a0a6SAlan Cox 	const char *model_rev;
40556919a0a6SAlan Cox 	unsigned long horkage;
40566919a0a6SAlan Cox };
40576919a0a6SAlan Cox 
40586919a0a6SAlan Cox static const struct ata_blacklist_entry ata_device_blacklist [] = {
40596919a0a6SAlan Cox 	/* Devices with DMA related problems under Linux */
40606919a0a6SAlan Cox 	{ "WDC AC11000H",	NULL,		ATA_HORKAGE_NODMA },
40616919a0a6SAlan Cox 	{ "WDC AC22100H",	NULL,		ATA_HORKAGE_NODMA },
40626919a0a6SAlan Cox 	{ "WDC AC32500H",	NULL,		ATA_HORKAGE_NODMA },
40636919a0a6SAlan Cox 	{ "WDC AC33100H",	NULL,		ATA_HORKAGE_NODMA },
40646919a0a6SAlan Cox 	{ "WDC AC31600H",	NULL,		ATA_HORKAGE_NODMA },
40656919a0a6SAlan Cox 	{ "WDC AC32100H",	"24.09P07",	ATA_HORKAGE_NODMA },
40666919a0a6SAlan Cox 	{ "WDC AC23200L",	"21.10N21",	ATA_HORKAGE_NODMA },
40676919a0a6SAlan Cox 	{ "Compaq CRD-8241B", 	NULL,		ATA_HORKAGE_NODMA },
40686919a0a6SAlan Cox 	{ "CRD-8400B",		NULL, 		ATA_HORKAGE_NODMA },
40697da4c935SMark Lord 	{ "CRD-848[02]B",	NULL,		ATA_HORKAGE_NODMA },
40706919a0a6SAlan Cox 	{ "CRD-84",		NULL,		ATA_HORKAGE_NODMA },
40716919a0a6SAlan Cox 	{ "SanDisk SDP3B",	NULL,		ATA_HORKAGE_NODMA },
40726919a0a6SAlan Cox 	{ "SanDisk SDP3B-64",	NULL,		ATA_HORKAGE_NODMA },
40736919a0a6SAlan Cox 	{ "SANYO CD-ROM CRD",	NULL,		ATA_HORKAGE_NODMA },
40746919a0a6SAlan Cox 	{ "HITACHI CDR-8",	NULL,		ATA_HORKAGE_NODMA },
40757da4c935SMark Lord 	{ "HITACHI CDR-8[34]35",NULL,		ATA_HORKAGE_NODMA },
40766919a0a6SAlan Cox 	{ "Toshiba CD-ROM XM-6202B", NULL,	ATA_HORKAGE_NODMA },
40776919a0a6SAlan Cox 	{ "TOSHIBA CD-ROM XM-1702BC", NULL,	ATA_HORKAGE_NODMA },
40786919a0a6SAlan Cox 	{ "CD-532E-A", 		NULL,		ATA_HORKAGE_NODMA },
40796919a0a6SAlan Cox 	{ "E-IDE CD-ROM CR-840",NULL,		ATA_HORKAGE_NODMA },
40806919a0a6SAlan Cox 	{ "CD-ROM Drive/F5A",	NULL,		ATA_HORKAGE_NODMA },
40816919a0a6SAlan Cox 	{ "WPI CDD-820", 	NULL,		ATA_HORKAGE_NODMA },
40826919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SC-148C", NULL,	ATA_HORKAGE_NODMA },
40836919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SC",	NULL,		ATA_HORKAGE_NODMA },
40846919a0a6SAlan Cox 	{ "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
40856919a0a6SAlan Cox 	{ "_NEC DV5800A", 	NULL,		ATA_HORKAGE_NODMA },
40866919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SN-124", "N001",	ATA_HORKAGE_NODMA },
408739f19886SDave Jones 	{ "Seagate STT20000A", NULL,		ATA_HORKAGE_NODMA },
4088d70e551cSPrarit Bhargava 	{ " 2GB ATA Flash Disk", "ADMA428M",	ATA_HORKAGE_NODMA },
40893af9a77aSTejun Heo 	/* Odd clown on sil3726/4726 PMPs */
409050af2fa1STejun Heo 	{ "Config  Disk",	NULL,		ATA_HORKAGE_DISABLE },
40916919a0a6SAlan Cox 
409218d6e9d5SAlbert Lee 	/* Weird ATAPI devices */
409340a1d531STejun Heo 	{ "TORiSAN DVD-ROM DRD-N216", NULL,	ATA_HORKAGE_MAX_SEC_128 },
40946a87e42eSTejun Heo 	{ "QUANTUM DAT    DAT72-000", NULL,	ATA_HORKAGE_ATAPI_MOD16_DMA },
409518d6e9d5SAlbert Lee 
40966919a0a6SAlan Cox 	/* Devices we expect to fail diagnostics */
40976919a0a6SAlan Cox 
40986919a0a6SAlan Cox 	/* Devices where NCQ should be avoided */
40996919a0a6SAlan Cox 	/* NCQ is slow */
41006919a0a6SAlan Cox 	{ "WDC WD740ADFD-00",	NULL,		ATA_HORKAGE_NONCQ },
4101459ad688STejun Heo 	{ "WDC WD740ADFD-00NLR1", NULL,		ATA_HORKAGE_NONCQ, },
410209125ea6STejun Heo 	/* http://thread.gmane.org/gmane.linux.ide/14907 */
410309125ea6STejun Heo 	{ "FUJITSU MHT2060BH",	NULL,		ATA_HORKAGE_NONCQ },
41047acfaf30SPaul Rolland 	/* NCQ is broken */
4105539cc7c7SJeff Garzik 	{ "Maxtor *",		"BANC*",	ATA_HORKAGE_NONCQ },
41060e3dbc01SAlan Cox 	{ "Maxtor 7V300F0",	"VA111630",	ATA_HORKAGE_NONCQ },
4107da6f0ec2SPaolo Ornati 	{ "ST380817AS",		"3.42",		ATA_HORKAGE_NONCQ },
4108e41bd3e8STejun Heo 	{ "ST3160023AS",	"3.42",		ATA_HORKAGE_NONCQ },
41095ccfca97SLubomir Bulej 	{ "OCZ CORE_SSD",	"02.10104",	ATA_HORKAGE_NONCQ },
4110539cc7c7SJeff Garzik 
4111ac70a964STejun Heo 	/* Seagate NCQ + FLUSH CACHE firmware bug */
41124d1f9082SMark Lord 	{ "ST31500341AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
4113ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4114d10d491fSTejun Heo 
41154d1f9082SMark Lord 	{ "ST31000333AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
4116d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4117d10d491fSTejun Heo 
41184d1f9082SMark Lord 	{ "ST3640[36]23AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
4119d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4120d10d491fSTejun Heo 
41214d1f9082SMark Lord 	{ "ST3320[68]13AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
4122ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4123ac70a964STejun Heo 
412436e337d0SRobert Hancock 	/* Blacklist entries taken from Silicon Image 3124/3132
412536e337d0SRobert Hancock 	   Windows driver .inf file - also several Linux problem reports */
412636e337d0SRobert Hancock 	{ "HTS541060G9SA00",    "MB3OC60D",     ATA_HORKAGE_NONCQ, },
412736e337d0SRobert Hancock 	{ "HTS541080G9SA00",    "MB4OC60D",     ATA_HORKAGE_NONCQ, },
412836e337d0SRobert Hancock 	{ "HTS541010G9SA00",    "MBZOC60D",     ATA_HORKAGE_NONCQ, },
41296919a0a6SAlan Cox 
413068b0ddb2STejun Heo 	/* https://bugzilla.kernel.org/show_bug.cgi?id=15573 */
413168b0ddb2STejun Heo 	{ "C300-CTFDDAC128MAG",	"0001",		ATA_HORKAGE_NONCQ, },
413268b0ddb2STejun Heo 
413316c55b03STejun Heo 	/* devices which puke on READ_NATIVE_MAX */
413416c55b03STejun Heo 	{ "HDS724040KLSA80",	"KFAOA20N",	ATA_HORKAGE_BROKEN_HPA, },
413516c55b03STejun Heo 	{ "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
413616c55b03STejun Heo 	{ "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
413716c55b03STejun Heo 	{ "MAXTOR 6L080L4",	"A93.0500",	ATA_HORKAGE_BROKEN_HPA },
41386919a0a6SAlan Cox 
41397831387bSTejun Heo 	/* this one allows HPA unlocking but fails IOs on the area */
41407831387bSTejun Heo 	{ "OCZ-VERTEX",		    "1.30",	ATA_HORKAGE_BROKEN_HPA },
41417831387bSTejun Heo 
414293328e11SAlan Cox 	/* Devices which report 1 sector over size HPA */
414393328e11SAlan Cox 	{ "ST340823A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
414493328e11SAlan Cox 	{ "ST320413A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
4145b152fcd3SMikko Rapeli 	{ "ST310211A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
414693328e11SAlan Cox 
41476bbfd53dSAlan Cox 	/* Devices which get the IVB wrong */
41486bbfd53dSAlan Cox 	{ "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB, },
4149a79067e5SAlan Cox 	/* Maybe we should just blacklist TSSTcorp... */
41507da4c935SMark Lord 	{ "TSSTcorp CDDVDW SH-S202[HJN]", "SB0[01]",  ATA_HORKAGE_IVB, },
41516bbfd53dSAlan Cox 
41529ce8e307SJens Axboe 	/* Devices that do not need bridging limits applied */
41539ce8e307SJens Axboe 	{ "MTRON MSP-SATA*",		NULL,	ATA_HORKAGE_BRIDGE_OK, },
415404d0f1b8SJeff Garzik 	{ "BUFFALO HD-QSU2/R5",		NULL,	ATA_HORKAGE_BRIDGE_OK, },
41559ce8e307SJens Axboe 
41569062712fSTejun Heo 	/* Devices which aren't very happy with higher link speeds */
41579062712fSTejun Heo 	{ "WD My Book",			NULL,	ATA_HORKAGE_1_5_GBPS, },
4158c531077fSDaniel J Blueman 	{ "Seagate FreeAgent GoFlex",	NULL,	ATA_HORKAGE_1_5_GBPS, },
41599062712fSTejun Heo 
4160d0cb43b3STejun Heo 	/*
4161d0cb43b3STejun Heo 	 * Devices which choke on SETXFER.  Applies only if both the
4162d0cb43b3STejun Heo 	 * device and controller are SATA.
4163d0cb43b3STejun Heo 	 */
4164cd691876STejun Heo 	{ "PIONEER DVD-RW  DVRTD08",	NULL,	ATA_HORKAGE_NOSETXFER },
41653a25179eSVladimir LAVALLADE 	{ "PIONEER DVD-RW  DVRTD08A",	NULL,	ATA_HORKAGE_NOSETXFER },
41663a25179eSVladimir LAVALLADE 	{ "PIONEER DVD-RW  DVR-215",	NULL,	ATA_HORKAGE_NOSETXFER },
4167cd691876STejun Heo 	{ "PIONEER DVD-RW  DVR-212D",	NULL,	ATA_HORKAGE_NOSETXFER },
4168cd691876STejun Heo 	{ "PIONEER DVD-RW  DVR-216D",	NULL,	ATA_HORKAGE_NOSETXFER },
4169d0cb43b3STejun Heo 
41706919a0a6SAlan Cox 	/* End Marker */
41716919a0a6SAlan Cox 	{ }
4172c6fd2807SJeff Garzik };
4173c6fd2807SJeff Garzik 
4174bce036ceSMark Lord /**
4175bce036ceSMark Lord  *	glob_match - match a text string against a glob-style pattern
4176bce036ceSMark Lord  *	@text: the string to be examined
4177bce036ceSMark Lord  *	@pattern: the glob-style pattern to be matched against
4178bce036ceSMark Lord  *
4179bce036ceSMark Lord  *	Either/both of text and pattern can be empty strings.
4180bce036ceSMark Lord  *
4181bce036ceSMark Lord  *	Match text against a glob-style pattern, with wildcards and simple sets:
4182bce036ceSMark Lord  *
4183bce036ceSMark Lord  *		?	matches any single character.
4184bce036ceSMark Lord  *		*	matches any run of characters.
4185bce036ceSMark Lord  *		[xyz]	matches a single character from the set: x, y, or z.
41862f9e4d16SMark Lord  *		[a-d]	matches a single character from the range: a, b, c, or d.
41872f9e4d16SMark Lord  *		[a-d0-9] matches a single character from either range.
4188bce036ceSMark Lord  *
41892f9e4d16SMark Lord  *	The special characters ?, [, -, or *, can be matched using a set, eg. [*]
41902f9e4d16SMark Lord  *	Behaviour with malformed patterns is undefined, though generally reasonable.
4191bce036ceSMark Lord  *
41923d2be54bSRandy Dunlap  *	Sample patterns:  "SD1?",  "SD1[0-5]",  "*R0",  "SD*1?[012]*xx"
4193bce036ceSMark Lord  *
4194bce036ceSMark Lord  *	This function uses one level of recursion per '*' in pattern.
4195bce036ceSMark Lord  *	Since it calls _nothing_ else, and has _no_ explicit local variables,
4196bce036ceSMark Lord  *	this will not cause stack problems for any reasonable use here.
4197bce036ceSMark Lord  *
4198bce036ceSMark Lord  *	RETURNS:
4199bce036ceSMark Lord  *	0 on match, 1 otherwise.
4200539cc7c7SJeff Garzik  */
4201bce036ceSMark Lord static int glob_match (const char *text, const char *pattern)
4202bce036ceSMark Lord {
4203bce036ceSMark Lord 	do {
4204bce036ceSMark Lord 		/* Match single character or a '?' wildcard */
4205bce036ceSMark Lord 		if (*text == *pattern || *pattern == '?') {
4206bce036ceSMark Lord 			if (!*pattern++)
4207bce036ceSMark Lord 				return 0;  /* End of both strings: match */
4208bce036ceSMark Lord 		} else {
4209bce036ceSMark Lord 			/* Match single char against a '[' bracketed ']' pattern set */
4210bce036ceSMark Lord 			if (!*text || *pattern != '[')
4211bce036ceSMark Lord 				break;  /* Not a pattern set */
42122f9e4d16SMark Lord 			while (*++pattern && *pattern != ']' && *text != *pattern) {
42132f9e4d16SMark Lord 				if (*pattern == '-' && *(pattern - 1) != '[')
42142f9e4d16SMark Lord 					if (*text > *(pattern - 1) && *text < *(pattern + 1)) {
42152f9e4d16SMark Lord 						++pattern;
42162f9e4d16SMark Lord 						break;
42172f9e4d16SMark Lord 					}
42182f9e4d16SMark Lord 			}
4219bce036ceSMark Lord 			if (!*pattern || *pattern == ']')
4220bce036ceSMark Lord 				return 1;  /* No match */
4221bce036ceSMark Lord 			while (*pattern && *pattern++ != ']');
4222317b50b8SAndrew Paprocki 		}
4223bce036ceSMark Lord 	} while (*++text && *pattern);
4224539cc7c7SJeff Garzik 
4225bce036ceSMark Lord 	/* Match any run of chars against a '*' wildcard */
4226bce036ceSMark Lord 	if (*pattern == '*') {
4227bce036ceSMark Lord 		if (!*++pattern)
4228bce036ceSMark Lord 			return 0;  /* Match: avoid recursion at end of pattern */
4229bce036ceSMark Lord 		/* Loop to handle additional pattern chars after the wildcard */
4230bce036ceSMark Lord 		while (*text) {
4231bce036ceSMark Lord 			if (glob_match(text, pattern) == 0)
4232bce036ceSMark Lord 				return 0;  /* Remainder matched */
4233bce036ceSMark Lord 			++text;  /* Absorb (match) this char and try again */
4234bce036ceSMark Lord 		}
4235bce036ceSMark Lord 	}
4236bce036ceSMark Lord 	if (!*text && !*pattern)
4237bce036ceSMark Lord 		return 0;  /* End of both strings: match */
4238bce036ceSMark Lord 	return 1;  /* No match */
4239539cc7c7SJeff Garzik }
4240539cc7c7SJeff Garzik 
424175683fe7STejun Heo static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
4242c6fd2807SJeff Garzik {
42438bfa79fcSTejun Heo 	unsigned char model_num[ATA_ID_PROD_LEN + 1];
42448bfa79fcSTejun Heo 	unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
42456919a0a6SAlan Cox 	const struct ata_blacklist_entry *ad = ata_device_blacklist;
4246c6fd2807SJeff Garzik 
42478bfa79fcSTejun Heo 	ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
42488bfa79fcSTejun Heo 	ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
4249c6fd2807SJeff Garzik 
42506919a0a6SAlan Cox 	while (ad->model_num) {
4251bce036ceSMark Lord 		if (!glob_match(model_num, ad->model_num)) {
42526919a0a6SAlan Cox 			if (ad->model_rev == NULL)
42536919a0a6SAlan Cox 				return ad->horkage;
4254bce036ceSMark Lord 			if (!glob_match(model_rev, ad->model_rev))
42556919a0a6SAlan Cox 				return ad->horkage;
4256c6fd2807SJeff Garzik 		}
42576919a0a6SAlan Cox 		ad++;
4258c6fd2807SJeff Garzik 	}
4259c6fd2807SJeff Garzik 	return 0;
4260c6fd2807SJeff Garzik }
4261c6fd2807SJeff Garzik 
42626919a0a6SAlan Cox static int ata_dma_blacklisted(const struct ata_device *dev)
42636919a0a6SAlan Cox {
42646919a0a6SAlan Cox 	/* We don't support polling DMA.
42656919a0a6SAlan Cox 	 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
42666919a0a6SAlan Cox 	 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
42676919a0a6SAlan Cox 	 */
42689af5c9c9STejun Heo 	if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
42696919a0a6SAlan Cox 	    (dev->flags & ATA_DFLAG_CDB_INTR))
42706919a0a6SAlan Cox 		return 1;
427175683fe7STejun Heo 	return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
42726919a0a6SAlan Cox }
42736919a0a6SAlan Cox 
4274c6fd2807SJeff Garzik /**
42756bbfd53dSAlan Cox  *	ata_is_40wire		-	check drive side detection
42766bbfd53dSAlan Cox  *	@dev: device
42776bbfd53dSAlan Cox  *
42786bbfd53dSAlan Cox  *	Perform drive side detection decoding, allowing for device vendors
42796bbfd53dSAlan Cox  *	who can't follow the documentation.
42806bbfd53dSAlan Cox  */
42816bbfd53dSAlan Cox 
42826bbfd53dSAlan Cox static int ata_is_40wire(struct ata_device *dev)
42836bbfd53dSAlan Cox {
42846bbfd53dSAlan Cox 	if (dev->horkage & ATA_HORKAGE_IVB)
42856bbfd53dSAlan Cox 		return ata_drive_40wire_relaxed(dev->id);
42866bbfd53dSAlan Cox 	return ata_drive_40wire(dev->id);
42876bbfd53dSAlan Cox }
42886bbfd53dSAlan Cox 
42896bbfd53dSAlan Cox /**
429015a5551cSAlan Cox  *	cable_is_40wire		-	40/80/SATA decider
429115a5551cSAlan Cox  *	@ap: port to consider
429215a5551cSAlan Cox  *
429315a5551cSAlan Cox  *	This function encapsulates the policy for speed management
429415a5551cSAlan Cox  *	in one place. At the moment we don't cache the result but
429515a5551cSAlan Cox  *	there is a good case for setting ap->cbl to the result when
429615a5551cSAlan Cox  *	we are called with unknown cables (and figuring out if it
429715a5551cSAlan Cox  *	impacts hotplug at all).
429815a5551cSAlan Cox  *
429915a5551cSAlan Cox  *	Return 1 if the cable appears to be 40 wire.
430015a5551cSAlan Cox  */
430115a5551cSAlan Cox 
430215a5551cSAlan Cox static int cable_is_40wire(struct ata_port *ap)
430315a5551cSAlan Cox {
430415a5551cSAlan Cox 	struct ata_link *link;
430515a5551cSAlan Cox 	struct ata_device *dev;
430615a5551cSAlan Cox 
43074a9c7b33STejun Heo 	/* If the controller thinks we are 40 wire, we are. */
430815a5551cSAlan Cox 	if (ap->cbl == ATA_CBL_PATA40)
430915a5551cSAlan Cox 		return 1;
43104a9c7b33STejun Heo 
43114a9c7b33STejun Heo 	/* If the controller thinks we are 80 wire, we are. */
431215a5551cSAlan Cox 	if (ap->cbl == ATA_CBL_PATA80 || ap->cbl == ATA_CBL_SATA)
431315a5551cSAlan Cox 		return 0;
43144a9c7b33STejun Heo 
43154a9c7b33STejun Heo 	/* If the system is known to be 40 wire short cable (eg
43164a9c7b33STejun Heo 	 * laptop), then we allow 80 wire modes even if the drive
43174a9c7b33STejun Heo 	 * isn't sure.
43184a9c7b33STejun Heo 	 */
4319f792068eSAlan Cox 	if (ap->cbl == ATA_CBL_PATA40_SHORT)
4320f792068eSAlan Cox 		return 0;
432115a5551cSAlan Cox 
43224a9c7b33STejun Heo 	/* If the controller doesn't know, we scan.
43234a9c7b33STejun Heo 	 *
43244a9c7b33STejun Heo 	 * Note: We look for all 40 wire detects at this point.  Any
43254a9c7b33STejun Heo 	 *       80 wire detect is taken to be 80 wire cable because
43264a9c7b33STejun Heo 	 * - in many setups only the one drive (slave if present) will
43274a9c7b33STejun Heo 	 *   give a valid detect
43284a9c7b33STejun Heo 	 * - if you have a non detect capable drive you don't want it
43294a9c7b33STejun Heo 	 *   to colour the choice
433015a5551cSAlan Cox 	 */
43311eca4365STejun Heo 	ata_for_each_link(link, ap, EDGE) {
43321eca4365STejun Heo 		ata_for_each_dev(dev, link, ENABLED) {
43331eca4365STejun Heo 			if (!ata_is_40wire(dev))
433415a5551cSAlan Cox 				return 0;
433515a5551cSAlan Cox 		}
433615a5551cSAlan Cox 	}
433715a5551cSAlan Cox 	return 1;
433815a5551cSAlan Cox }
433915a5551cSAlan Cox 
434015a5551cSAlan Cox /**
4341c6fd2807SJeff Garzik  *	ata_dev_xfermask - Compute supported xfermask of the given device
4342c6fd2807SJeff Garzik  *	@dev: Device to compute xfermask for
4343c6fd2807SJeff Garzik  *
4344c6fd2807SJeff Garzik  *	Compute supported xfermask of @dev and store it in
4345c6fd2807SJeff Garzik  *	dev->*_mask.  This function is responsible for applying all
4346c6fd2807SJeff Garzik  *	known limits including host controller limits, device
4347c6fd2807SJeff Garzik  *	blacklist, etc...
4348c6fd2807SJeff Garzik  *
4349c6fd2807SJeff Garzik  *	LOCKING:
4350c6fd2807SJeff Garzik  *	None.
4351c6fd2807SJeff Garzik  */
4352c6fd2807SJeff Garzik static void ata_dev_xfermask(struct ata_device *dev)
4353c6fd2807SJeff Garzik {
43549af5c9c9STejun Heo 	struct ata_link *link = dev->link;
43559af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
4356cca3974eSJeff Garzik 	struct ata_host *host = ap->host;
4357c6fd2807SJeff Garzik 	unsigned long xfer_mask;
4358c6fd2807SJeff Garzik 
4359c6fd2807SJeff Garzik 	/* controller modes available */
4360c6fd2807SJeff Garzik 	xfer_mask = ata_pack_xfermask(ap->pio_mask,
4361c6fd2807SJeff Garzik 				      ap->mwdma_mask, ap->udma_mask);
4362c6fd2807SJeff Garzik 
43638343f889SRobert Hancock 	/* drive modes available */
4364c6fd2807SJeff Garzik 	xfer_mask &= ata_pack_xfermask(dev->pio_mask,
4365c6fd2807SJeff Garzik 				       dev->mwdma_mask, dev->udma_mask);
4366c6fd2807SJeff Garzik 	xfer_mask &= ata_id_xfermask(dev->id);
4367c6fd2807SJeff Garzik 
4368b352e57dSAlan Cox 	/*
4369b352e57dSAlan Cox 	 *	CFA Advanced TrueIDE timings are not allowed on a shared
4370b352e57dSAlan Cox 	 *	cable
4371b352e57dSAlan Cox 	 */
4372b352e57dSAlan Cox 	if (ata_dev_pair(dev)) {
4373b352e57dSAlan Cox 		/* No PIO5 or PIO6 */
4374b352e57dSAlan Cox 		xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
4375b352e57dSAlan Cox 		/* No MWDMA3 or MWDMA 4 */
4376b352e57dSAlan Cox 		xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
4377b352e57dSAlan Cox 	}
4378b352e57dSAlan Cox 
4379c6fd2807SJeff Garzik 	if (ata_dma_blacklisted(dev)) {
4380c6fd2807SJeff Garzik 		xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4381a9a79dfeSJoe Perches 		ata_dev_warn(dev,
4382c6fd2807SJeff Garzik 			     "device is on DMA blacklist, disabling DMA\n");
4383c6fd2807SJeff Garzik 	}
4384c6fd2807SJeff Garzik 
438514d66ab7SPetr Vandrovec 	if ((host->flags & ATA_HOST_SIMPLEX) &&
438614d66ab7SPetr Vandrovec 	    host->simplex_claimed && host->simplex_claimed != ap) {
4387c6fd2807SJeff Garzik 		xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4388a9a79dfeSJoe Perches 		ata_dev_warn(dev,
4389a9a79dfeSJoe Perches 			     "simplex DMA is claimed by other device, disabling DMA\n");
4390c6fd2807SJeff Garzik 	}
4391c6fd2807SJeff Garzik 
4392e424675fSJeff Garzik 	if (ap->flags & ATA_FLAG_NO_IORDY)
4393e424675fSJeff Garzik 		xfer_mask &= ata_pio_mask_no_iordy(dev);
4394e424675fSJeff Garzik 
4395c6fd2807SJeff Garzik 	if (ap->ops->mode_filter)
4396a76b62caSAlan Cox 		xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
4397c6fd2807SJeff Garzik 
43988343f889SRobert Hancock 	/* Apply cable rule here.  Don't apply it early because when
43998343f889SRobert Hancock 	 * we handle hot plug the cable type can itself change.
44008343f889SRobert Hancock 	 * Check this last so that we know if the transfer rate was
44018343f889SRobert Hancock 	 * solely limited by the cable.
44028343f889SRobert Hancock 	 * Unknown or 80 wire cables reported host side are checked
44038343f889SRobert Hancock 	 * drive side as well. Cases where we know a 40wire cable
44048343f889SRobert Hancock 	 * is used safely for 80 are not checked here.
44058343f889SRobert Hancock 	 */
44068343f889SRobert Hancock 	if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
44078343f889SRobert Hancock 		/* UDMA/44 or higher would be available */
440815a5551cSAlan Cox 		if (cable_is_40wire(ap)) {
4409a9a79dfeSJoe Perches 			ata_dev_warn(dev,
44108343f889SRobert Hancock 				     "limited to UDMA/33 due to 40-wire cable\n");
44118343f889SRobert Hancock 			xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
44128343f889SRobert Hancock 		}
44138343f889SRobert Hancock 
4414c6fd2807SJeff Garzik 	ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
4415c6fd2807SJeff Garzik 			    &dev->mwdma_mask, &dev->udma_mask);
4416c6fd2807SJeff Garzik }
4417c6fd2807SJeff Garzik 
4418c6fd2807SJeff Garzik /**
4419c6fd2807SJeff Garzik  *	ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
4420c6fd2807SJeff Garzik  *	@dev: Device to which command will be sent
4421c6fd2807SJeff Garzik  *
4422c6fd2807SJeff Garzik  *	Issue SET FEATURES - XFER MODE command to device @dev
4423c6fd2807SJeff Garzik  *	on port @ap.
4424c6fd2807SJeff Garzik  *
4425c6fd2807SJeff Garzik  *	LOCKING:
4426c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
4427c6fd2807SJeff Garzik  *
4428c6fd2807SJeff Garzik  *	RETURNS:
4429c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
4430c6fd2807SJeff Garzik  */
4431c6fd2807SJeff Garzik 
4432c6fd2807SJeff Garzik static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
4433c6fd2807SJeff Garzik {
4434c6fd2807SJeff Garzik 	struct ata_taskfile tf;
4435c6fd2807SJeff Garzik 	unsigned int err_mask;
4436c6fd2807SJeff Garzik 
4437c6fd2807SJeff Garzik 	/* set up set-features taskfile */
4438c6fd2807SJeff Garzik 	DPRINTK("set features - xfer mode\n");
4439c6fd2807SJeff Garzik 
4440464cf177STejun Heo 	/* Some controllers and ATAPI devices show flaky interrupt
4441464cf177STejun Heo 	 * behavior after setting xfer mode.  Use polling instead.
4442464cf177STejun Heo 	 */
4443c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
4444c6fd2807SJeff Garzik 	tf.command = ATA_CMD_SET_FEATURES;
4445c6fd2807SJeff Garzik 	tf.feature = SETFEATURES_XFER;
4446464cf177STejun Heo 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
4447c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
4448b9f8ab2dSAlan Cox 	/* If we are using IORDY we must send the mode setting command */
4449b9f8ab2dSAlan Cox 	if (ata_pio_need_iordy(dev))
4450c6fd2807SJeff Garzik 		tf.nsect = dev->xfer_mode;
4451b9f8ab2dSAlan Cox 	/* If the device has IORDY and the controller does not - turn it off */
4452b9f8ab2dSAlan Cox  	else if (ata_id_has_iordy(dev->id))
4453b9f8ab2dSAlan Cox 		tf.nsect = 0x01;
4454b9f8ab2dSAlan Cox 	else /* In the ancient relic department - skip all of this */
4455b9f8ab2dSAlan Cox 		return 0;
4456c6fd2807SJeff Garzik 
44572b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
4458c6fd2807SJeff Garzik 
4459c6fd2807SJeff Garzik 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
4460c6fd2807SJeff Garzik 	return err_mask;
4461c6fd2807SJeff Garzik }
44621152b261STejun Heo 
4463c6fd2807SJeff Garzik /**
4464218f3d30SJeff Garzik  *	ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES
44659f45cbd3SKristen Carlson Accardi  *	@dev: Device to which command will be sent
44669f45cbd3SKristen Carlson Accardi  *	@enable: Whether to enable or disable the feature
4467218f3d30SJeff Garzik  *	@feature: The sector count represents the feature to set
44689f45cbd3SKristen Carlson Accardi  *
44699f45cbd3SKristen Carlson Accardi  *	Issue SET FEATURES - SATA FEATURES command to device @dev
4470218f3d30SJeff Garzik  *	on port @ap with sector count
44719f45cbd3SKristen Carlson Accardi  *
44729f45cbd3SKristen Carlson Accardi  *	LOCKING:
44739f45cbd3SKristen Carlson Accardi  *	PCI/etc. bus probe sem.
44749f45cbd3SKristen Carlson Accardi  *
44759f45cbd3SKristen Carlson Accardi  *	RETURNS:
44769f45cbd3SKristen Carlson Accardi  *	0 on success, AC_ERR_* mask otherwise.
44779f45cbd3SKristen Carlson Accardi  */
44781152b261STejun Heo unsigned int ata_dev_set_feature(struct ata_device *dev, u8 enable, u8 feature)
44799f45cbd3SKristen Carlson Accardi {
44809f45cbd3SKristen Carlson Accardi 	struct ata_taskfile tf;
44819f45cbd3SKristen Carlson Accardi 	unsigned int err_mask;
44829f45cbd3SKristen Carlson Accardi 
44839f45cbd3SKristen Carlson Accardi 	/* set up set-features taskfile */
44849f45cbd3SKristen Carlson Accardi 	DPRINTK("set features - SATA features\n");
44859f45cbd3SKristen Carlson Accardi 
44869f45cbd3SKristen Carlson Accardi 	ata_tf_init(dev, &tf);
44879f45cbd3SKristen Carlson Accardi 	tf.command = ATA_CMD_SET_FEATURES;
44889f45cbd3SKristen Carlson Accardi 	tf.feature = enable;
44899f45cbd3SKristen Carlson Accardi 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
44909f45cbd3SKristen Carlson Accardi 	tf.protocol = ATA_PROT_NODATA;
4491218f3d30SJeff Garzik 	tf.nsect = feature;
44929f45cbd3SKristen Carlson Accardi 
44932b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
44949f45cbd3SKristen Carlson Accardi 
44959f45cbd3SKristen Carlson Accardi 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
44969f45cbd3SKristen Carlson Accardi 	return err_mask;
44979f45cbd3SKristen Carlson Accardi }
4498633de4ccSJeff Garzik EXPORT_SYMBOL_GPL(ata_dev_set_feature);
44999f45cbd3SKristen Carlson Accardi 
45009f45cbd3SKristen Carlson Accardi /**
4501c6fd2807SJeff Garzik  *	ata_dev_init_params - Issue INIT DEV PARAMS command
4502c6fd2807SJeff Garzik  *	@dev: Device to which command will be sent
4503c6fd2807SJeff Garzik  *	@heads: Number of heads (taskfile parameter)
4504c6fd2807SJeff Garzik  *	@sectors: Number of sectors (taskfile parameter)
4505c6fd2807SJeff Garzik  *
4506c6fd2807SJeff Garzik  *	LOCKING:
4507c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
4508c6fd2807SJeff Garzik  *
4509c6fd2807SJeff Garzik  *	RETURNS:
4510c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
4511c6fd2807SJeff Garzik  */
4512c6fd2807SJeff Garzik static unsigned int ata_dev_init_params(struct ata_device *dev,
4513c6fd2807SJeff Garzik 					u16 heads, u16 sectors)
4514c6fd2807SJeff Garzik {
4515c6fd2807SJeff Garzik 	struct ata_taskfile tf;
4516c6fd2807SJeff Garzik 	unsigned int err_mask;
4517c6fd2807SJeff Garzik 
4518c6fd2807SJeff Garzik 	/* Number of sectors per track 1-255. Number of heads 1-16 */
4519c6fd2807SJeff Garzik 	if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
4520c6fd2807SJeff Garzik 		return AC_ERR_INVALID;
4521c6fd2807SJeff Garzik 
4522c6fd2807SJeff Garzik 	/* set up init dev params taskfile */
4523c6fd2807SJeff Garzik 	DPRINTK("init dev params \n");
4524c6fd2807SJeff Garzik 
4525c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
4526c6fd2807SJeff Garzik 	tf.command = ATA_CMD_INIT_DEV_PARAMS;
4527c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4528c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
4529c6fd2807SJeff Garzik 	tf.nsect = sectors;
4530c6fd2807SJeff Garzik 	tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
4531c6fd2807SJeff Garzik 
45322b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
453318b2466cSAlan Cox 	/* A clean abort indicates an original or just out of spec drive
453418b2466cSAlan Cox 	   and we should continue as we issue the setup based on the
453518b2466cSAlan Cox 	   drive reported working geometry */
453618b2466cSAlan Cox 	if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
453718b2466cSAlan Cox 		err_mask = 0;
4538c6fd2807SJeff Garzik 
4539c6fd2807SJeff Garzik 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
4540c6fd2807SJeff Garzik 	return err_mask;
4541c6fd2807SJeff Garzik }
4542c6fd2807SJeff Garzik 
4543c6fd2807SJeff Garzik /**
4544c6fd2807SJeff Garzik  *	ata_sg_clean - Unmap DMA memory associated with command
4545c6fd2807SJeff Garzik  *	@qc: Command containing DMA memory to be released
4546c6fd2807SJeff Garzik  *
4547c6fd2807SJeff Garzik  *	Unmap all mapped DMA memory associated with this command.
4548c6fd2807SJeff Garzik  *
4549c6fd2807SJeff Garzik  *	LOCKING:
4550cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4551c6fd2807SJeff Garzik  */
455270e6ad0cSTejun Heo void ata_sg_clean(struct ata_queued_cmd *qc)
4553c6fd2807SJeff Garzik {
4554c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4555ff2aeb1eSTejun Heo 	struct scatterlist *sg = qc->sg;
4556c6fd2807SJeff Garzik 	int dir = qc->dma_dir;
4557c6fd2807SJeff Garzik 
4558efcb3cf7STejun Heo 	WARN_ON_ONCE(sg == NULL);
4559c6fd2807SJeff Garzik 
4560dde20207SJames Bottomley 	VPRINTK("unmapping %u sg elements\n", qc->n_elem);
4561c6fd2807SJeff Garzik 
4562dde20207SJames Bottomley 	if (qc->n_elem)
45635825627cSFUJITA Tomonori 		dma_unmap_sg(ap->dev, sg, qc->orig_n_elem, dir);
4564c6fd2807SJeff Garzik 
4565c6fd2807SJeff Garzik 	qc->flags &= ~ATA_QCFLAG_DMAMAP;
4566ff2aeb1eSTejun Heo 	qc->sg = NULL;
4567c6fd2807SJeff Garzik }
4568c6fd2807SJeff Garzik 
4569c6fd2807SJeff Garzik /**
45705895ef9aSTejun Heo  *	atapi_check_dma - Check whether ATAPI DMA can be supported
4571c6fd2807SJeff Garzik  *	@qc: Metadata associated with taskfile to check
4572c6fd2807SJeff Garzik  *
4573c6fd2807SJeff Garzik  *	Allow low-level driver to filter ATA PACKET commands, returning
4574c6fd2807SJeff Garzik  *	a status indicating whether or not it is OK to use DMA for the
4575c6fd2807SJeff Garzik  *	supplied PACKET command.
4576c6fd2807SJeff Garzik  *
4577c6fd2807SJeff Garzik  *	LOCKING:
4578cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4579c6fd2807SJeff Garzik  *
4580c6fd2807SJeff Garzik  *	RETURNS: 0 when ATAPI DMA can be used
4581c6fd2807SJeff Garzik  *               nonzero otherwise
4582c6fd2807SJeff Garzik  */
45835895ef9aSTejun Heo int atapi_check_dma(struct ata_queued_cmd *qc)
4584c6fd2807SJeff Garzik {
4585c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4586c6fd2807SJeff Garzik 
4587b9a4197eSTejun Heo 	/* Don't allow DMA if it isn't multiple of 16 bytes.  Quite a
4588b9a4197eSTejun Heo 	 * few ATAPI devices choke on such DMA requests.
4589b9a4197eSTejun Heo 	 */
45906a87e42eSTejun Heo 	if (!(qc->dev->horkage & ATA_HORKAGE_ATAPI_MOD16_DMA) &&
45916a87e42eSTejun Heo 	    unlikely(qc->nbytes & 15))
45926f23a31dSAlbert Lee 		return 1;
45936f23a31dSAlbert Lee 
4594c6fd2807SJeff Garzik 	if (ap->ops->check_atapi_dma)
4595b9a4197eSTejun Heo 		return ap->ops->check_atapi_dma(qc);
4596c6fd2807SJeff Garzik 
4597b9a4197eSTejun Heo 	return 0;
4598c6fd2807SJeff Garzik }
4599b9a4197eSTejun Heo 
4600c6fd2807SJeff Garzik /**
460131cc23b3STejun Heo  *	ata_std_qc_defer - Check whether a qc needs to be deferred
460231cc23b3STejun Heo  *	@qc: ATA command in question
460331cc23b3STejun Heo  *
460431cc23b3STejun Heo  *	Non-NCQ commands cannot run with any other command, NCQ or
460531cc23b3STejun Heo  *	not.  As upper layer only knows the queue depth, we are
460631cc23b3STejun Heo  *	responsible for maintaining exclusion.  This function checks
460731cc23b3STejun Heo  *	whether a new command @qc can be issued.
460831cc23b3STejun Heo  *
460931cc23b3STejun Heo  *	LOCKING:
461031cc23b3STejun Heo  *	spin_lock_irqsave(host lock)
461131cc23b3STejun Heo  *
461231cc23b3STejun Heo  *	RETURNS:
461331cc23b3STejun Heo  *	ATA_DEFER_* if deferring is needed, 0 otherwise.
461431cc23b3STejun Heo  */
461531cc23b3STejun Heo int ata_std_qc_defer(struct ata_queued_cmd *qc)
461631cc23b3STejun Heo {
461731cc23b3STejun Heo 	struct ata_link *link = qc->dev->link;
461831cc23b3STejun Heo 
461931cc23b3STejun Heo 	if (qc->tf.protocol == ATA_PROT_NCQ) {
462031cc23b3STejun Heo 		if (!ata_tag_valid(link->active_tag))
462131cc23b3STejun Heo 			return 0;
462231cc23b3STejun Heo 	} else {
462331cc23b3STejun Heo 		if (!ata_tag_valid(link->active_tag) && !link->sactive)
462431cc23b3STejun Heo 			return 0;
462531cc23b3STejun Heo 	}
462631cc23b3STejun Heo 
462731cc23b3STejun Heo 	return ATA_DEFER_LINK;
462831cc23b3STejun Heo }
462931cc23b3STejun Heo 
4630c6fd2807SJeff Garzik void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
4631c6fd2807SJeff Garzik 
4632c6fd2807SJeff Garzik /**
4633c6fd2807SJeff Garzik  *	ata_sg_init - Associate command with scatter-gather table.
4634c6fd2807SJeff Garzik  *	@qc: Command to be associated
4635c6fd2807SJeff Garzik  *	@sg: Scatter-gather table.
4636c6fd2807SJeff Garzik  *	@n_elem: Number of elements in s/g table.
4637c6fd2807SJeff Garzik  *
4638c6fd2807SJeff Garzik  *	Initialize the data-related elements of queued_cmd @qc
4639c6fd2807SJeff Garzik  *	to point to a scatter-gather table @sg, containing @n_elem
4640c6fd2807SJeff Garzik  *	elements.
4641c6fd2807SJeff Garzik  *
4642c6fd2807SJeff Garzik  *	LOCKING:
4643cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4644c6fd2807SJeff Garzik  */
4645c6fd2807SJeff Garzik void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
4646c6fd2807SJeff Garzik 		 unsigned int n_elem)
4647c6fd2807SJeff Garzik {
4648ff2aeb1eSTejun Heo 	qc->sg = sg;
4649c6fd2807SJeff Garzik 	qc->n_elem = n_elem;
4650ff2aeb1eSTejun Heo 	qc->cursg = qc->sg;
4651ff2aeb1eSTejun Heo }
4652ff2aeb1eSTejun Heo 
4653c6fd2807SJeff Garzik /**
4654c6fd2807SJeff Garzik  *	ata_sg_setup - DMA-map the scatter-gather table associated with a command.
4655c6fd2807SJeff Garzik  *	@qc: Command with scatter-gather table to be mapped.
4656c6fd2807SJeff Garzik  *
4657c6fd2807SJeff Garzik  *	DMA-map the scatter-gather table associated with queued_cmd @qc.
4658c6fd2807SJeff Garzik  *
4659c6fd2807SJeff Garzik  *	LOCKING:
4660cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4661c6fd2807SJeff Garzik  *
4662c6fd2807SJeff Garzik  *	RETURNS:
4663c6fd2807SJeff Garzik  *	Zero on success, negative on error.
4664c6fd2807SJeff Garzik  *
4665c6fd2807SJeff Garzik  */
4666c6fd2807SJeff Garzik static int ata_sg_setup(struct ata_queued_cmd *qc)
4667c6fd2807SJeff Garzik {
4668c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4669dde20207SJames Bottomley 	unsigned int n_elem;
4670c6fd2807SJeff Garzik 
467144877b4eSTejun Heo 	VPRINTK("ENTER, ata%u\n", ap->print_id);
4672c6fd2807SJeff Garzik 
4673dde20207SJames Bottomley 	n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir);
4674dde20207SJames Bottomley 	if (n_elem < 1)
4675c6fd2807SJeff Garzik 		return -1;
4676c6fd2807SJeff Garzik 
4677dde20207SJames Bottomley 	DPRINTK("%d sg elements mapped\n", n_elem);
46785825627cSFUJITA Tomonori 	qc->orig_n_elem = qc->n_elem;
4679dde20207SJames Bottomley 	qc->n_elem = n_elem;
4680f92a2636STejun Heo 	qc->flags |= ATA_QCFLAG_DMAMAP;
4681c6fd2807SJeff Garzik 
4682c6fd2807SJeff Garzik 	return 0;
4683c6fd2807SJeff Garzik }
4684c6fd2807SJeff Garzik 
4685c6fd2807SJeff Garzik /**
4686c6fd2807SJeff Garzik  *	swap_buf_le16 - swap halves of 16-bit words in place
4687c6fd2807SJeff Garzik  *	@buf:  Buffer to swap
4688c6fd2807SJeff Garzik  *	@buf_words:  Number of 16-bit words in buffer.
4689c6fd2807SJeff Garzik  *
4690c6fd2807SJeff Garzik  *	Swap halves of 16-bit words if needed to convert from
4691c6fd2807SJeff Garzik  *	little-endian byte order to native cpu byte order, or
4692c6fd2807SJeff Garzik  *	vice-versa.
4693c6fd2807SJeff Garzik  *
4694c6fd2807SJeff Garzik  *	LOCKING:
4695c6fd2807SJeff Garzik  *	Inherited from caller.
4696c6fd2807SJeff Garzik  */
4697c6fd2807SJeff Garzik void swap_buf_le16(u16 *buf, unsigned int buf_words)
4698c6fd2807SJeff Garzik {
4699c6fd2807SJeff Garzik #ifdef __BIG_ENDIAN
4700c6fd2807SJeff Garzik 	unsigned int i;
4701c6fd2807SJeff Garzik 
4702c6fd2807SJeff Garzik 	for (i = 0; i < buf_words; i++)
4703c6fd2807SJeff Garzik 		buf[i] = le16_to_cpu(buf[i]);
4704c6fd2807SJeff Garzik #endif /* __BIG_ENDIAN */
4705c6fd2807SJeff Garzik }
4706c6fd2807SJeff Garzik 
4707c6fd2807SJeff Garzik /**
47088a8bc223STejun Heo  *	ata_qc_new - Request an available ATA command, for queueing
47095eb66fe0SRandy Dunlap  *	@ap: target port
47108a8bc223STejun Heo  *
47118a8bc223STejun Heo  *	LOCKING:
47128a8bc223STejun Heo  *	None.
47138a8bc223STejun Heo  */
47148a8bc223STejun Heo 
47158a8bc223STejun Heo static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
47168a8bc223STejun Heo {
47178a8bc223STejun Heo 	struct ata_queued_cmd *qc = NULL;
47188a8bc223STejun Heo 	unsigned int i;
47198a8bc223STejun Heo 
47208a8bc223STejun Heo 	/* no command while frozen */
47218a8bc223STejun Heo 	if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
47228a8bc223STejun Heo 		return NULL;
47238a8bc223STejun Heo 
47248a8bc223STejun Heo 	/* the last tag is reserved for internal command. */
47258a8bc223STejun Heo 	for (i = 0; i < ATA_MAX_QUEUE - 1; i++)
47268a8bc223STejun Heo 		if (!test_and_set_bit(i, &ap->qc_allocated)) {
47278a8bc223STejun Heo 			qc = __ata_qc_from_tag(ap, i);
47288a8bc223STejun Heo 			break;
47298a8bc223STejun Heo 		}
47308a8bc223STejun Heo 
47318a8bc223STejun Heo 	if (qc)
47328a8bc223STejun Heo 		qc->tag = i;
47338a8bc223STejun Heo 
47348a8bc223STejun Heo 	return qc;
47358a8bc223STejun Heo }
47368a8bc223STejun Heo 
47378a8bc223STejun Heo /**
4738c6fd2807SJeff Garzik  *	ata_qc_new_init - Request an available ATA command, and initialize it
4739c6fd2807SJeff Garzik  *	@dev: Device from whom we request an available command structure
4740c6fd2807SJeff Garzik  *
4741c6fd2807SJeff Garzik  *	LOCKING:
4742c6fd2807SJeff Garzik  *	None.
4743c6fd2807SJeff Garzik  */
4744c6fd2807SJeff Garzik 
47458a8bc223STejun Heo struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev)
4746c6fd2807SJeff Garzik {
47479af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
4748c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
4749c6fd2807SJeff Garzik 
47508a8bc223STejun Heo 	qc = ata_qc_new(ap);
4751c6fd2807SJeff Garzik 	if (qc) {
4752c6fd2807SJeff Garzik 		qc->scsicmd = NULL;
4753c6fd2807SJeff Garzik 		qc->ap = ap;
4754c6fd2807SJeff Garzik 		qc->dev = dev;
4755c6fd2807SJeff Garzik 
4756c6fd2807SJeff Garzik 		ata_qc_reinit(qc);
4757c6fd2807SJeff Garzik 	}
4758c6fd2807SJeff Garzik 
4759c6fd2807SJeff Garzik 	return qc;
4760c6fd2807SJeff Garzik }
4761c6fd2807SJeff Garzik 
47628a8bc223STejun Heo /**
47638a8bc223STejun Heo  *	ata_qc_free - free unused ata_queued_cmd
47648a8bc223STejun Heo  *	@qc: Command to complete
47658a8bc223STejun Heo  *
47668a8bc223STejun Heo  *	Designed to free unused ata_queued_cmd object
47678a8bc223STejun Heo  *	in case something prevents using it.
47688a8bc223STejun Heo  *
47698a8bc223STejun Heo  *	LOCKING:
47708a8bc223STejun Heo  *	spin_lock_irqsave(host lock)
47718a8bc223STejun Heo  */
47728a8bc223STejun Heo void ata_qc_free(struct ata_queued_cmd *qc)
47738a8bc223STejun Heo {
4774a1104016SJulia Lawall 	struct ata_port *ap;
47758a8bc223STejun Heo 	unsigned int tag;
47768a8bc223STejun Heo 
4777efcb3cf7STejun Heo 	WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4778a1104016SJulia Lawall 	ap = qc->ap;
47798a8bc223STejun Heo 
47808a8bc223STejun Heo 	qc->flags = 0;
47818a8bc223STejun Heo 	tag = qc->tag;
47828a8bc223STejun Heo 	if (likely(ata_tag_valid(tag))) {
47838a8bc223STejun Heo 		qc->tag = ATA_TAG_POISON;
47848a8bc223STejun Heo 		clear_bit(tag, &ap->qc_allocated);
47858a8bc223STejun Heo 	}
47868a8bc223STejun Heo }
47878a8bc223STejun Heo 
4788c6fd2807SJeff Garzik void __ata_qc_complete(struct ata_queued_cmd *qc)
4789c6fd2807SJeff Garzik {
4790a1104016SJulia Lawall 	struct ata_port *ap;
4791a1104016SJulia Lawall 	struct ata_link *link;
4792c6fd2807SJeff Garzik 
4793efcb3cf7STejun Heo 	WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4794efcb3cf7STejun Heo 	WARN_ON_ONCE(!(qc->flags & ATA_QCFLAG_ACTIVE));
4795a1104016SJulia Lawall 	ap = qc->ap;
4796a1104016SJulia Lawall 	link = qc->dev->link;
4797c6fd2807SJeff Garzik 
4798c6fd2807SJeff Garzik 	if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
4799c6fd2807SJeff Garzik 		ata_sg_clean(qc);
4800c6fd2807SJeff Garzik 
4801c6fd2807SJeff Garzik 	/* command should be marked inactive atomically with qc completion */
4802da917d69STejun Heo 	if (qc->tf.protocol == ATA_PROT_NCQ) {
48039af5c9c9STejun Heo 		link->sactive &= ~(1 << qc->tag);
4804da917d69STejun Heo 		if (!link->sactive)
4805da917d69STejun Heo 			ap->nr_active_links--;
4806da917d69STejun Heo 	} else {
48079af5c9c9STejun Heo 		link->active_tag = ATA_TAG_POISON;
4808da917d69STejun Heo 		ap->nr_active_links--;
4809da917d69STejun Heo 	}
4810da917d69STejun Heo 
4811da917d69STejun Heo 	/* clear exclusive status */
4812da917d69STejun Heo 	if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
4813da917d69STejun Heo 		     ap->excl_link == link))
4814da917d69STejun Heo 		ap->excl_link = NULL;
4815c6fd2807SJeff Garzik 
4816c6fd2807SJeff Garzik 	/* atapi: mark qc as inactive to prevent the interrupt handler
4817c6fd2807SJeff Garzik 	 * from completing the command twice later, before the error handler
4818c6fd2807SJeff Garzik 	 * is called. (when rc != 0 and atapi request sense is needed)
4819c6fd2807SJeff Garzik 	 */
4820c6fd2807SJeff Garzik 	qc->flags &= ~ATA_QCFLAG_ACTIVE;
4821c6fd2807SJeff Garzik 	ap->qc_active &= ~(1 << qc->tag);
4822c6fd2807SJeff Garzik 
4823c6fd2807SJeff Garzik 	/* call completion callback */
4824c6fd2807SJeff Garzik 	qc->complete_fn(qc);
4825c6fd2807SJeff Garzik }
4826c6fd2807SJeff Garzik 
482739599a53STejun Heo static void fill_result_tf(struct ata_queued_cmd *qc)
482839599a53STejun Heo {
482939599a53STejun Heo 	struct ata_port *ap = qc->ap;
483039599a53STejun Heo 
483139599a53STejun Heo 	qc->result_tf.flags = qc->tf.flags;
483222183bf5STejun Heo 	ap->ops->qc_fill_rtf(qc);
483339599a53STejun Heo }
483439599a53STejun Heo 
483500115e0fSTejun Heo static void ata_verify_xfer(struct ata_queued_cmd *qc)
483600115e0fSTejun Heo {
483700115e0fSTejun Heo 	struct ata_device *dev = qc->dev;
483800115e0fSTejun Heo 
483900115e0fSTejun Heo 	if (ata_is_nodata(qc->tf.protocol))
484000115e0fSTejun Heo 		return;
484100115e0fSTejun Heo 
484200115e0fSTejun Heo 	if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
484300115e0fSTejun Heo 		return;
484400115e0fSTejun Heo 
484500115e0fSTejun Heo 	dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
484600115e0fSTejun Heo }
484700115e0fSTejun Heo 
4848c6fd2807SJeff Garzik /**
4849c6fd2807SJeff Garzik  *	ata_qc_complete - Complete an active ATA command
4850c6fd2807SJeff Garzik  *	@qc: Command to complete
4851c6fd2807SJeff Garzik  *
48521aadf5c3STejun Heo  *	Indicate to the mid and upper layers that an ATA command has
48531aadf5c3STejun Heo  *	completed, with either an ok or not-ok status.
48541aadf5c3STejun Heo  *
48551aadf5c3STejun Heo  *	Refrain from calling this function multiple times when
48561aadf5c3STejun Heo  *	successfully completing multiple NCQ commands.
48571aadf5c3STejun Heo  *	ata_qc_complete_multiple() should be used instead, which will
48581aadf5c3STejun Heo  *	properly update IRQ expect state.
4859c6fd2807SJeff Garzik  *
4860c6fd2807SJeff Garzik  *	LOCKING:
4861cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4862c6fd2807SJeff Garzik  */
4863c6fd2807SJeff Garzik void ata_qc_complete(struct ata_queued_cmd *qc)
4864c6fd2807SJeff Garzik {
4865c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4866c6fd2807SJeff Garzik 
4867c6fd2807SJeff Garzik 	/* XXX: New EH and old EH use different mechanisms to
4868c6fd2807SJeff Garzik 	 * synchronize EH with regular execution path.
4869c6fd2807SJeff Garzik 	 *
4870c6fd2807SJeff Garzik 	 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
4871c6fd2807SJeff Garzik 	 * Normal execution path is responsible for not accessing a
4872c6fd2807SJeff Garzik 	 * failed qc.  libata core enforces the rule by returning NULL
4873c6fd2807SJeff Garzik 	 * from ata_qc_from_tag() for failed qcs.
4874c6fd2807SJeff Garzik 	 *
4875c6fd2807SJeff Garzik 	 * Old EH depends on ata_qc_complete() nullifying completion
4876c6fd2807SJeff Garzik 	 * requests if ATA_QCFLAG_EH_SCHEDULED is set.  Old EH does
4877c6fd2807SJeff Garzik 	 * not synchronize with interrupt handler.  Only PIO task is
4878c6fd2807SJeff Garzik 	 * taken care of.
4879c6fd2807SJeff Garzik 	 */
4880c6fd2807SJeff Garzik 	if (ap->ops->error_handler) {
48814dbfa39bSTejun Heo 		struct ata_device *dev = qc->dev;
48824dbfa39bSTejun Heo 		struct ata_eh_info *ehi = &dev->link->eh_info;
48834dbfa39bSTejun Heo 
4884c6fd2807SJeff Garzik 		if (unlikely(qc->err_mask))
4885c6fd2807SJeff Garzik 			qc->flags |= ATA_QCFLAG_FAILED;
4886c6fd2807SJeff Garzik 
4887f08dc1acSTejun Heo 		/*
4888f08dc1acSTejun Heo 		 * Finish internal commands without any further processing
4889f08dc1acSTejun Heo 		 * and always with the result TF filled.
4890f08dc1acSTejun Heo 		 */
4891f08dc1acSTejun Heo 		if (unlikely(ata_tag_internal(qc->tag))) {
489239599a53STejun Heo 			fill_result_tf(qc);
4893f4b31db9STejun Heo 			__ata_qc_complete(qc);
4894c6fd2807SJeff Garzik 			return;
4895c6fd2807SJeff Garzik 		}
4896c6fd2807SJeff Garzik 
4897f08dc1acSTejun Heo 		/*
4898f08dc1acSTejun Heo 		 * Non-internal qc has failed.  Fill the result TF and
4899f08dc1acSTejun Heo 		 * summon EH.
4900f08dc1acSTejun Heo 		 */
4901f08dc1acSTejun Heo 		if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
4902f08dc1acSTejun Heo 			fill_result_tf(qc);
4903f08dc1acSTejun Heo 			ata_qc_schedule_eh(qc);
4904f08dc1acSTejun Heo 			return;
4905f08dc1acSTejun Heo 		}
4906f08dc1acSTejun Heo 
49074dc738edSTejun Heo 		WARN_ON_ONCE(ap->pflags & ATA_PFLAG_FROZEN);
49084dc738edSTejun Heo 
4909c6fd2807SJeff Garzik 		/* read result TF if requested */
4910c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_RESULT_TF)
491139599a53STejun Heo 			fill_result_tf(qc);
4912c6fd2807SJeff Garzik 
49134dbfa39bSTejun Heo 		/* Some commands need post-processing after successful
49144dbfa39bSTejun Heo 		 * completion.
49154dbfa39bSTejun Heo 		 */
49164dbfa39bSTejun Heo 		switch (qc->tf.command) {
49174dbfa39bSTejun Heo 		case ATA_CMD_SET_FEATURES:
49184dbfa39bSTejun Heo 			if (qc->tf.feature != SETFEATURES_WC_ON &&
49194dbfa39bSTejun Heo 			    qc->tf.feature != SETFEATURES_WC_OFF)
49204dbfa39bSTejun Heo 				break;
49214dbfa39bSTejun Heo 			/* fall through */
49224dbfa39bSTejun Heo 		case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
49234dbfa39bSTejun Heo 		case ATA_CMD_SET_MULTI: /* multi_count changed */
49244dbfa39bSTejun Heo 			/* revalidate device */
49254dbfa39bSTejun Heo 			ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE;
49264dbfa39bSTejun Heo 			ata_port_schedule_eh(ap);
49274dbfa39bSTejun Heo 			break;
4928054a5fbaSTejun Heo 
4929054a5fbaSTejun Heo 		case ATA_CMD_SLEEP:
4930054a5fbaSTejun Heo 			dev->flags |= ATA_DFLAG_SLEEPING;
4931054a5fbaSTejun Heo 			break;
49324dbfa39bSTejun Heo 		}
49334dbfa39bSTejun Heo 
493400115e0fSTejun Heo 		if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
493500115e0fSTejun Heo 			ata_verify_xfer(qc);
493600115e0fSTejun Heo 
4937c6fd2807SJeff Garzik 		__ata_qc_complete(qc);
4938c6fd2807SJeff Garzik 	} else {
4939c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
4940c6fd2807SJeff Garzik 			return;
4941c6fd2807SJeff Garzik 
4942c6fd2807SJeff Garzik 		/* read result TF if failed or requested */
4943c6fd2807SJeff Garzik 		if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
494439599a53STejun Heo 			fill_result_tf(qc);
4945c6fd2807SJeff Garzik 
4946c6fd2807SJeff Garzik 		__ata_qc_complete(qc);
4947c6fd2807SJeff Garzik 	}
4948c6fd2807SJeff Garzik }
4949c6fd2807SJeff Garzik 
4950c6fd2807SJeff Garzik /**
4951c6fd2807SJeff Garzik  *	ata_qc_complete_multiple - Complete multiple qcs successfully
4952c6fd2807SJeff Garzik  *	@ap: port in question
4953c6fd2807SJeff Garzik  *	@qc_active: new qc_active mask
4954c6fd2807SJeff Garzik  *
4955c6fd2807SJeff Garzik  *	Complete in-flight commands.  This functions is meant to be
4956c6fd2807SJeff Garzik  *	called from low-level driver's interrupt routine to complete
4957c6fd2807SJeff Garzik  *	requests normally.  ap->qc_active and @qc_active is compared
4958c6fd2807SJeff Garzik  *	and commands are completed accordingly.
4959c6fd2807SJeff Garzik  *
49601aadf5c3STejun Heo  *	Always use this function when completing multiple NCQ commands
49611aadf5c3STejun Heo  *	from IRQ handlers instead of calling ata_qc_complete()
49621aadf5c3STejun Heo  *	multiple times to keep IRQ expect status properly in sync.
49631aadf5c3STejun Heo  *
4964c6fd2807SJeff Garzik  *	LOCKING:
4965cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4966c6fd2807SJeff Garzik  *
4967c6fd2807SJeff Garzik  *	RETURNS:
4968c6fd2807SJeff Garzik  *	Number of completed commands on success, -errno otherwise.
4969c6fd2807SJeff Garzik  */
497079f97dadSTejun Heo int ata_qc_complete_multiple(struct ata_port *ap, u32 qc_active)
4971c6fd2807SJeff Garzik {
4972c6fd2807SJeff Garzik 	int nr_done = 0;
4973c6fd2807SJeff Garzik 	u32 done_mask;
4974c6fd2807SJeff Garzik 
4975c6fd2807SJeff Garzik 	done_mask = ap->qc_active ^ qc_active;
4976c6fd2807SJeff Garzik 
4977c6fd2807SJeff Garzik 	if (unlikely(done_mask & qc_active)) {
4978a9a79dfeSJoe Perches 		ata_port_err(ap, "illegal qc_active transition (%08x->%08x)\n",
4979a9a79dfeSJoe Perches 			     ap->qc_active, qc_active);
4980c6fd2807SJeff Garzik 		return -EINVAL;
4981c6fd2807SJeff Garzik 	}
4982c6fd2807SJeff Garzik 
498343768180SJens Axboe 	while (done_mask) {
4984c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc;
498543768180SJens Axboe 		unsigned int tag = __ffs(done_mask);
4986c6fd2807SJeff Garzik 
498743768180SJens Axboe 		qc = ata_qc_from_tag(ap, tag);
498843768180SJens Axboe 		if (qc) {
4989c6fd2807SJeff Garzik 			ata_qc_complete(qc);
4990c6fd2807SJeff Garzik 			nr_done++;
4991c6fd2807SJeff Garzik 		}
499243768180SJens Axboe 		done_mask &= ~(1 << tag);
4993c6fd2807SJeff Garzik 	}
4994c6fd2807SJeff Garzik 
4995c6fd2807SJeff Garzik 	return nr_done;
4996c6fd2807SJeff Garzik }
4997c6fd2807SJeff Garzik 
4998c6fd2807SJeff Garzik /**
4999c6fd2807SJeff Garzik  *	ata_qc_issue - issue taskfile to device
5000c6fd2807SJeff Garzik  *	@qc: command to issue to device
5001c6fd2807SJeff Garzik  *
5002c6fd2807SJeff Garzik  *	Prepare an ATA command to submission to device.
5003c6fd2807SJeff Garzik  *	This includes mapping the data into a DMA-able
5004c6fd2807SJeff Garzik  *	area, filling in the S/G table, and finally
5005c6fd2807SJeff Garzik  *	writing the taskfile to hardware, starting the command.
5006c6fd2807SJeff Garzik  *
5007c6fd2807SJeff Garzik  *	LOCKING:
5008cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
5009c6fd2807SJeff Garzik  */
5010c6fd2807SJeff Garzik void ata_qc_issue(struct ata_queued_cmd *qc)
5011c6fd2807SJeff Garzik {
5012c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
50139af5c9c9STejun Heo 	struct ata_link *link = qc->dev->link;
5014405e66b3STejun Heo 	u8 prot = qc->tf.protocol;
5015c6fd2807SJeff Garzik 
5016c6fd2807SJeff Garzik 	/* Make sure only one non-NCQ command is outstanding.  The
5017c6fd2807SJeff Garzik 	 * check is skipped for old EH because it reuses active qc to
5018c6fd2807SJeff Garzik 	 * request ATAPI sense.
5019c6fd2807SJeff Garzik 	 */
5020efcb3cf7STejun Heo 	WARN_ON_ONCE(ap->ops->error_handler && ata_tag_valid(link->active_tag));
5021c6fd2807SJeff Garzik 
50221973a023STejun Heo 	if (ata_is_ncq(prot)) {
5023efcb3cf7STejun Heo 		WARN_ON_ONCE(link->sactive & (1 << qc->tag));
5024da917d69STejun Heo 
5025da917d69STejun Heo 		if (!link->sactive)
5026da917d69STejun Heo 			ap->nr_active_links++;
50279af5c9c9STejun Heo 		link->sactive |= 1 << qc->tag;
5028c6fd2807SJeff Garzik 	} else {
5029efcb3cf7STejun Heo 		WARN_ON_ONCE(link->sactive);
5030da917d69STejun Heo 
5031da917d69STejun Heo 		ap->nr_active_links++;
50329af5c9c9STejun Heo 		link->active_tag = qc->tag;
5033c6fd2807SJeff Garzik 	}
5034c6fd2807SJeff Garzik 
5035c6fd2807SJeff Garzik 	qc->flags |= ATA_QCFLAG_ACTIVE;
5036c6fd2807SJeff Garzik 	ap->qc_active |= 1 << qc->tag;
5037c6fd2807SJeff Garzik 
503860f5d6efSTejun Heo 	/*
503960f5d6efSTejun Heo 	 * We guarantee to LLDs that they will have at least one
5040f92a2636STejun Heo 	 * non-zero sg if the command is a data command.
5041f92a2636STejun Heo 	 */
504260f5d6efSTejun Heo 	if (WARN_ON_ONCE(ata_is_data(prot) &&
504360f5d6efSTejun Heo 			 (!qc->sg || !qc->n_elem || !qc->nbytes)))
504460f5d6efSTejun Heo 		goto sys_err;
5045f92a2636STejun Heo 
5046405e66b3STejun Heo 	if (ata_is_dma(prot) || (ata_is_pio(prot) &&
5047f92a2636STejun Heo 				 (ap->flags & ATA_FLAG_PIO_DMA)))
5048c6fd2807SJeff Garzik 		if (ata_sg_setup(qc))
504960f5d6efSTejun Heo 			goto sys_err;
5050c6fd2807SJeff Garzik 
5051cf480626STejun Heo 	/* if device is sleeping, schedule reset and abort the link */
5052054a5fbaSTejun Heo 	if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
5053cf480626STejun Heo 		link->eh_info.action |= ATA_EH_RESET;
5054054a5fbaSTejun Heo 		ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
5055054a5fbaSTejun Heo 		ata_link_abort(link);
5056054a5fbaSTejun Heo 		return;
5057054a5fbaSTejun Heo 	}
5058054a5fbaSTejun Heo 
5059c6fd2807SJeff Garzik 	ap->ops->qc_prep(qc);
5060c6fd2807SJeff Garzik 
5061c6fd2807SJeff Garzik 	qc->err_mask |= ap->ops->qc_issue(qc);
5062c6fd2807SJeff Garzik 	if (unlikely(qc->err_mask))
5063c6fd2807SJeff Garzik 		goto err;
5064c6fd2807SJeff Garzik 	return;
5065c6fd2807SJeff Garzik 
506660f5d6efSTejun Heo sys_err:
5067c6fd2807SJeff Garzik 	qc->err_mask |= AC_ERR_SYSTEM;
5068c6fd2807SJeff Garzik err:
5069c6fd2807SJeff Garzik 	ata_qc_complete(qc);
5070c6fd2807SJeff Garzik }
5071c6fd2807SJeff Garzik 
5072c6fd2807SJeff Garzik /**
5073c6fd2807SJeff Garzik  *	sata_scr_valid - test whether SCRs are accessible
5074936fd732STejun Heo  *	@link: ATA link to test SCR accessibility for
5075c6fd2807SJeff Garzik  *
5076936fd732STejun Heo  *	Test whether SCRs are accessible for @link.
5077c6fd2807SJeff Garzik  *
5078c6fd2807SJeff Garzik  *	LOCKING:
5079c6fd2807SJeff Garzik  *	None.
5080c6fd2807SJeff Garzik  *
5081c6fd2807SJeff Garzik  *	RETURNS:
5082c6fd2807SJeff Garzik  *	1 if SCRs are accessible, 0 otherwise.
5083c6fd2807SJeff Garzik  */
5084936fd732STejun Heo int sata_scr_valid(struct ata_link *link)
5085c6fd2807SJeff Garzik {
5086936fd732STejun Heo 	struct ata_port *ap = link->ap;
5087936fd732STejun Heo 
5088a16abc0bSTejun Heo 	return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
5089c6fd2807SJeff Garzik }
5090c6fd2807SJeff Garzik 
5091c6fd2807SJeff Garzik /**
5092c6fd2807SJeff Garzik  *	sata_scr_read - read SCR register of the specified port
5093936fd732STejun Heo  *	@link: ATA link to read SCR for
5094c6fd2807SJeff Garzik  *	@reg: SCR to read
5095c6fd2807SJeff Garzik  *	@val: Place to store read value
5096c6fd2807SJeff Garzik  *
5097936fd732STejun Heo  *	Read SCR register @reg of @link into *@val.  This function is
5098633273a3STejun Heo  *	guaranteed to succeed if @link is ap->link, the cable type of
5099633273a3STejun Heo  *	the port is SATA and the port implements ->scr_read.
5100c6fd2807SJeff Garzik  *
5101c6fd2807SJeff Garzik  *	LOCKING:
5102633273a3STejun Heo  *	None if @link is ap->link.  Kernel thread context otherwise.
5103c6fd2807SJeff Garzik  *
5104c6fd2807SJeff Garzik  *	RETURNS:
5105c6fd2807SJeff Garzik  *	0 on success, negative errno on failure.
5106c6fd2807SJeff Garzik  */
5107936fd732STejun Heo int sata_scr_read(struct ata_link *link, int reg, u32 *val)
5108c6fd2807SJeff Garzik {
5109633273a3STejun Heo 	if (ata_is_host_link(link)) {
5110936fd732STejun Heo 		if (sata_scr_valid(link))
511182ef04fbSTejun Heo 			return link->ap->ops->scr_read(link, reg, val);
5112c6fd2807SJeff Garzik 		return -EOPNOTSUPP;
5113c6fd2807SJeff Garzik 	}
5114c6fd2807SJeff Garzik 
5115633273a3STejun Heo 	return sata_pmp_scr_read(link, reg, val);
5116633273a3STejun Heo }
5117633273a3STejun Heo 
5118c6fd2807SJeff Garzik /**
5119c6fd2807SJeff Garzik  *	sata_scr_write - write SCR register of the specified port
5120936fd732STejun Heo  *	@link: ATA link to write SCR for
5121c6fd2807SJeff Garzik  *	@reg: SCR to write
5122c6fd2807SJeff Garzik  *	@val: value to write
5123c6fd2807SJeff Garzik  *
5124936fd732STejun Heo  *	Write @val to SCR register @reg of @link.  This function is
5125633273a3STejun Heo  *	guaranteed to succeed if @link is ap->link, the cable type of
5126633273a3STejun Heo  *	the port is SATA and the port implements ->scr_read.
5127c6fd2807SJeff Garzik  *
5128c6fd2807SJeff Garzik  *	LOCKING:
5129633273a3STejun Heo  *	None if @link is ap->link.  Kernel thread context otherwise.
5130c6fd2807SJeff Garzik  *
5131c6fd2807SJeff Garzik  *	RETURNS:
5132c6fd2807SJeff Garzik  *	0 on success, negative errno on failure.
5133c6fd2807SJeff Garzik  */
5134936fd732STejun Heo int sata_scr_write(struct ata_link *link, int reg, u32 val)
5135c6fd2807SJeff Garzik {
5136633273a3STejun Heo 	if (ata_is_host_link(link)) {
5137936fd732STejun Heo 		if (sata_scr_valid(link))
513882ef04fbSTejun Heo 			return link->ap->ops->scr_write(link, reg, val);
5139c6fd2807SJeff Garzik 		return -EOPNOTSUPP;
5140c6fd2807SJeff Garzik 	}
5141c6fd2807SJeff Garzik 
5142633273a3STejun Heo 	return sata_pmp_scr_write(link, reg, val);
5143633273a3STejun Heo }
5144633273a3STejun Heo 
5145c6fd2807SJeff Garzik /**
5146c6fd2807SJeff Garzik  *	sata_scr_write_flush - write SCR register of the specified port and flush
5147936fd732STejun Heo  *	@link: ATA link to write SCR for
5148c6fd2807SJeff Garzik  *	@reg: SCR to write
5149c6fd2807SJeff Garzik  *	@val: value to write
5150c6fd2807SJeff Garzik  *
5151c6fd2807SJeff Garzik  *	This function is identical to sata_scr_write() except that this
5152c6fd2807SJeff Garzik  *	function performs flush after writing to the register.
5153c6fd2807SJeff Garzik  *
5154c6fd2807SJeff Garzik  *	LOCKING:
5155633273a3STejun Heo  *	None if @link is ap->link.  Kernel thread context otherwise.
5156c6fd2807SJeff Garzik  *
5157c6fd2807SJeff Garzik  *	RETURNS:
5158c6fd2807SJeff Garzik  *	0 on success, negative errno on failure.
5159c6fd2807SJeff Garzik  */
5160936fd732STejun Heo int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
5161c6fd2807SJeff Garzik {
5162633273a3STejun Heo 	if (ata_is_host_link(link)) {
5163da3dbb17STejun Heo 		int rc;
5164da3dbb17STejun Heo 
5165936fd732STejun Heo 		if (sata_scr_valid(link)) {
516682ef04fbSTejun Heo 			rc = link->ap->ops->scr_write(link, reg, val);
5167da3dbb17STejun Heo 			if (rc == 0)
516882ef04fbSTejun Heo 				rc = link->ap->ops->scr_read(link, reg, &val);
5169da3dbb17STejun Heo 			return rc;
5170c6fd2807SJeff Garzik 		}
5171c6fd2807SJeff Garzik 		return -EOPNOTSUPP;
5172c6fd2807SJeff Garzik 	}
5173c6fd2807SJeff Garzik 
5174633273a3STejun Heo 	return sata_pmp_scr_write(link, reg, val);
5175633273a3STejun Heo }
5176633273a3STejun Heo 
5177c6fd2807SJeff Garzik /**
5178b1c72916STejun Heo  *	ata_phys_link_online - test whether the given link is online
5179936fd732STejun Heo  *	@link: ATA link to test
5180c6fd2807SJeff Garzik  *
5181936fd732STejun Heo  *	Test whether @link is online.  Note that this function returns
5182936fd732STejun Heo  *	0 if online status of @link cannot be obtained, so
5183936fd732STejun Heo  *	ata_link_online(link) != !ata_link_offline(link).
5184c6fd2807SJeff Garzik  *
5185c6fd2807SJeff Garzik  *	LOCKING:
5186c6fd2807SJeff Garzik  *	None.
5187c6fd2807SJeff Garzik  *
5188c6fd2807SJeff Garzik  *	RETURNS:
5189b5b3fa38STejun Heo  *	True if the port online status is available and online.
5190c6fd2807SJeff Garzik  */
5191b1c72916STejun Heo bool ata_phys_link_online(struct ata_link *link)
5192c6fd2807SJeff Garzik {
5193c6fd2807SJeff Garzik 	u32 sstatus;
5194c6fd2807SJeff Garzik 
5195936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
51969913ff8aSTejun Heo 	    ata_sstatus_online(sstatus))
5197b5b3fa38STejun Heo 		return true;
5198b5b3fa38STejun Heo 	return false;
5199c6fd2807SJeff Garzik }
5200c6fd2807SJeff Garzik 
5201c6fd2807SJeff Garzik /**
5202b1c72916STejun Heo  *	ata_phys_link_offline - test whether the given link is offline
5203936fd732STejun Heo  *	@link: ATA link to test
5204c6fd2807SJeff Garzik  *
5205936fd732STejun Heo  *	Test whether @link is offline.  Note that this function
5206936fd732STejun Heo  *	returns 0 if offline status of @link cannot be obtained, so
5207936fd732STejun Heo  *	ata_link_online(link) != !ata_link_offline(link).
5208c6fd2807SJeff Garzik  *
5209c6fd2807SJeff Garzik  *	LOCKING:
5210c6fd2807SJeff Garzik  *	None.
5211c6fd2807SJeff Garzik  *
5212c6fd2807SJeff Garzik  *	RETURNS:
5213b5b3fa38STejun Heo  *	True if the port offline status is available and offline.
5214c6fd2807SJeff Garzik  */
5215b1c72916STejun Heo bool ata_phys_link_offline(struct ata_link *link)
5216c6fd2807SJeff Garzik {
5217c6fd2807SJeff Garzik 	u32 sstatus;
5218c6fd2807SJeff Garzik 
5219936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
52209913ff8aSTejun Heo 	    !ata_sstatus_online(sstatus))
5221b5b3fa38STejun Heo 		return true;
5222b5b3fa38STejun Heo 	return false;
5223c6fd2807SJeff Garzik }
5224c6fd2807SJeff Garzik 
5225b1c72916STejun Heo /**
5226b1c72916STejun Heo  *	ata_link_online - test whether the given link is online
5227b1c72916STejun Heo  *	@link: ATA link to test
5228b1c72916STejun Heo  *
5229b1c72916STejun Heo  *	Test whether @link is online.  This is identical to
5230b1c72916STejun Heo  *	ata_phys_link_online() when there's no slave link.  When
5231b1c72916STejun Heo  *	there's a slave link, this function should only be called on
5232b1c72916STejun Heo  *	the master link and will return true if any of M/S links is
5233b1c72916STejun Heo  *	online.
5234b1c72916STejun Heo  *
5235b1c72916STejun Heo  *	LOCKING:
5236b1c72916STejun Heo  *	None.
5237b1c72916STejun Heo  *
5238b1c72916STejun Heo  *	RETURNS:
5239b1c72916STejun Heo  *	True if the port online status is available and online.
5240b1c72916STejun Heo  */
5241b1c72916STejun Heo bool ata_link_online(struct ata_link *link)
5242b1c72916STejun Heo {
5243b1c72916STejun Heo 	struct ata_link *slave = link->ap->slave_link;
5244b1c72916STejun Heo 
5245b1c72916STejun Heo 	WARN_ON(link == slave);	/* shouldn't be called on slave link */
5246b1c72916STejun Heo 
5247b1c72916STejun Heo 	return ata_phys_link_online(link) ||
5248b1c72916STejun Heo 		(slave && ata_phys_link_online(slave));
5249b1c72916STejun Heo }
5250b1c72916STejun Heo 
5251b1c72916STejun Heo /**
5252b1c72916STejun Heo  *	ata_link_offline - test whether the given link is offline
5253b1c72916STejun Heo  *	@link: ATA link to test
5254b1c72916STejun Heo  *
5255b1c72916STejun Heo  *	Test whether @link is offline.  This is identical to
5256b1c72916STejun Heo  *	ata_phys_link_offline() when there's no slave link.  When
5257b1c72916STejun Heo  *	there's a slave link, this function should only be called on
5258b1c72916STejun Heo  *	the master link and will return true if both M/S links are
5259b1c72916STejun Heo  *	offline.
5260b1c72916STejun Heo  *
5261b1c72916STejun Heo  *	LOCKING:
5262b1c72916STejun Heo  *	None.
5263b1c72916STejun Heo  *
5264b1c72916STejun Heo  *	RETURNS:
5265b1c72916STejun Heo  *	True if the port offline status is available and offline.
5266b1c72916STejun Heo  */
5267b1c72916STejun Heo bool ata_link_offline(struct ata_link *link)
5268b1c72916STejun Heo {
5269b1c72916STejun Heo 	struct ata_link *slave = link->ap->slave_link;
5270b1c72916STejun Heo 
5271b1c72916STejun Heo 	WARN_ON(link == slave);	/* shouldn't be called on slave link */
5272b1c72916STejun Heo 
5273b1c72916STejun Heo 	return ata_phys_link_offline(link) &&
5274b1c72916STejun Heo 		(!slave || ata_phys_link_offline(slave));
5275b1c72916STejun Heo }
5276b1c72916STejun Heo 
52776ffa01d8STejun Heo #ifdef CONFIG_PM
52785ef41082SLin Ming static int ata_port_request_pm(struct ata_port *ap, pm_message_t mesg,
5279cca3974eSJeff Garzik 			       unsigned int action, unsigned int ehi_flags,
52802fcbdcb4SDan Williams 			       int *async)
5281c6fd2807SJeff Garzik {
5282e3667ebfSTejun Heo 	struct ata_link *link;
52835ef41082SLin Ming 	unsigned long flags;
52842fcbdcb4SDan Williams 	int rc = 0;
5285c6fd2807SJeff Garzik 
5286c6fd2807SJeff Garzik 	/* Previous resume operation might still be in
5287c6fd2807SJeff Garzik 	 * progress.  Wait for PM_PENDING to clear.
5288c6fd2807SJeff Garzik 	 */
5289c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_PM_PENDING) {
52902fcbdcb4SDan Williams 		if (async) {
52912fcbdcb4SDan Williams 			*async = -EAGAIN;
52922fcbdcb4SDan Williams 			return 0;
52932fcbdcb4SDan Williams 		}
5294c6fd2807SJeff Garzik 		ata_port_wait_eh(ap);
5295c6fd2807SJeff Garzik 		WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5296c6fd2807SJeff Garzik 	}
5297c6fd2807SJeff Garzik 
5298c6fd2807SJeff Garzik 	/* request PM ops to EH */
5299c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
5300c6fd2807SJeff Garzik 
5301c6fd2807SJeff Garzik 	ap->pm_mesg = mesg;
53022fcbdcb4SDan Williams 	if (async)
53032fcbdcb4SDan Williams 		ap->pm_result = async;
53042fcbdcb4SDan Williams 	else
5305c6fd2807SJeff Garzik 		ap->pm_result = &rc;
5306c6fd2807SJeff Garzik 
5307c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_PM_PENDING;
53081eca4365STejun Heo 	ata_for_each_link(link, ap, HOST_FIRST) {
5309e3667ebfSTejun Heo 		link->eh_info.action |= action;
5310e3667ebfSTejun Heo 		link->eh_info.flags |= ehi_flags;
5311e3667ebfSTejun Heo 	}
5312c6fd2807SJeff Garzik 
5313c6fd2807SJeff Garzik 	ata_port_schedule_eh(ap);
5314c6fd2807SJeff Garzik 
5315c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
5316c6fd2807SJeff Garzik 
5317c6fd2807SJeff Garzik 	/* wait and check result */
53182fcbdcb4SDan Williams 	if (!async) {
5319c6fd2807SJeff Garzik 		ata_port_wait_eh(ap);
5320c6fd2807SJeff Garzik 		WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5321c6fd2807SJeff Garzik 	}
5322c6fd2807SJeff Garzik 
53235ef41082SLin Ming 	return rc;
5324c6fd2807SJeff Garzik }
5325c6fd2807SJeff Garzik 
53262fcbdcb4SDan Williams static int __ata_port_suspend_common(struct ata_port *ap, pm_message_t mesg, int *async)
53275ef41082SLin Ming {
532833574d68SLin Ming 	unsigned int ehi_flags = ATA_EHI_QUIET;
53295ef41082SLin Ming 	int rc;
53305ef41082SLin Ming 
533133574d68SLin Ming 	/*
533233574d68SLin Ming 	 * On some hardware, device fails to respond after spun down
533333574d68SLin Ming 	 * for suspend.  As the device won't be used before being
533433574d68SLin Ming 	 * resumed, we don't need to touch the device.  Ask EH to skip
533533574d68SLin Ming 	 * the usual stuff and proceed directly to suspend.
533633574d68SLin Ming 	 *
533733574d68SLin Ming 	 * http://thread.gmane.org/gmane.linux.ide/46764
533833574d68SLin Ming 	 */
533933574d68SLin Ming 	if (mesg.event == PM_EVENT_SUSPEND)
534033574d68SLin Ming 		ehi_flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_NO_RECOVERY;
534133574d68SLin Ming 
53422fcbdcb4SDan Williams 	rc = ata_port_request_pm(ap, mesg, 0, ehi_flags, async);
53435ef41082SLin Ming 	return rc;
53445ef41082SLin Ming }
53455ef41082SLin Ming 
53462fcbdcb4SDan Williams static int ata_port_suspend_common(struct device *dev, pm_message_t mesg)
53472fcbdcb4SDan Williams {
53482fcbdcb4SDan Williams 	struct ata_port *ap = to_ata_port(dev);
53492fcbdcb4SDan Williams 
53502fcbdcb4SDan Williams 	return __ata_port_suspend_common(ap, mesg, NULL);
53512fcbdcb4SDan Williams }
53522fcbdcb4SDan Williams 
53535ef41082SLin Ming static int ata_port_suspend(struct device *dev)
53545ef41082SLin Ming {
53555ef41082SLin Ming 	if (pm_runtime_suspended(dev))
53565ef41082SLin Ming 		return 0;
53575ef41082SLin Ming 
535833574d68SLin Ming 	return ata_port_suspend_common(dev, PMSG_SUSPEND);
535933574d68SLin Ming }
536033574d68SLin Ming 
536133574d68SLin Ming static int ata_port_do_freeze(struct device *dev)
536233574d68SLin Ming {
536333574d68SLin Ming 	if (pm_runtime_suspended(dev))
536433574d68SLin Ming 		pm_runtime_resume(dev);
536533574d68SLin Ming 
536633574d68SLin Ming 	return ata_port_suspend_common(dev, PMSG_FREEZE);
536733574d68SLin Ming }
536833574d68SLin Ming 
536933574d68SLin Ming static int ata_port_poweroff(struct device *dev)
537033574d68SLin Ming {
537133574d68SLin Ming 	if (pm_runtime_suspended(dev))
537233574d68SLin Ming 		return 0;
537333574d68SLin Ming 
537433574d68SLin Ming 	return ata_port_suspend_common(dev, PMSG_HIBERNATE);
53755ef41082SLin Ming }
53765ef41082SLin Ming 
53772fcbdcb4SDan Williams static int __ata_port_resume_common(struct ata_port *ap, int *async)
53785ef41082SLin Ming {
53795ef41082SLin Ming 	int rc;
53805ef41082SLin Ming 
53815ef41082SLin Ming 	rc = ata_port_request_pm(ap, PMSG_ON, ATA_EH_RESET,
53822fcbdcb4SDan Williams 		ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET, async);
53835ef41082SLin Ming 	return rc;
53845ef41082SLin Ming }
53855ef41082SLin Ming 
53862fcbdcb4SDan Williams static int ata_port_resume_common(struct device *dev)
53872fcbdcb4SDan Williams {
53882fcbdcb4SDan Williams 	struct ata_port *ap = to_ata_port(dev);
53892fcbdcb4SDan Williams 
53902fcbdcb4SDan Williams 	return __ata_port_resume_common(ap, NULL);
53912fcbdcb4SDan Williams }
53922fcbdcb4SDan Williams 
5393e90b1e5aSLin Ming static int ata_port_resume(struct device *dev)
5394e90b1e5aSLin Ming {
5395e90b1e5aSLin Ming 	int rc;
5396e90b1e5aSLin Ming 
5397e90b1e5aSLin Ming 	rc = ata_port_resume_common(dev);
5398e90b1e5aSLin Ming 	if (!rc) {
5399e90b1e5aSLin Ming 		pm_runtime_disable(dev);
5400e90b1e5aSLin Ming 		pm_runtime_set_active(dev);
5401e90b1e5aSLin Ming 		pm_runtime_enable(dev);
5402e90b1e5aSLin Ming 	}
5403e90b1e5aSLin Ming 
5404e90b1e5aSLin Ming 	return rc;
5405e90b1e5aSLin Ming }
5406e90b1e5aSLin Ming 
54079ee4f393SLin Ming static int ata_port_runtime_idle(struct device *dev)
54089ee4f393SLin Ming {
54099ee4f393SLin Ming 	return pm_runtime_suspend(dev);
54109ee4f393SLin Ming }
54119ee4f393SLin Ming 
54125ef41082SLin Ming static const struct dev_pm_ops ata_port_pm_ops = {
54135ef41082SLin Ming 	.suspend = ata_port_suspend,
54145ef41082SLin Ming 	.resume = ata_port_resume,
541533574d68SLin Ming 	.freeze = ata_port_do_freeze,
541633574d68SLin Ming 	.thaw = ata_port_resume,
541733574d68SLin Ming 	.poweroff = ata_port_poweroff,
541833574d68SLin Ming 	.restore = ata_port_resume,
54199ee4f393SLin Ming 
542033574d68SLin Ming 	.runtime_suspend = ata_port_suspend,
5421e90b1e5aSLin Ming 	.runtime_resume = ata_port_resume_common,
54229ee4f393SLin Ming 	.runtime_idle = ata_port_runtime_idle,
54235ef41082SLin Ming };
54245ef41082SLin Ming 
54252fcbdcb4SDan Williams /* sas ports don't participate in pm runtime management of ata_ports,
54262fcbdcb4SDan Williams  * and need to resume ata devices at the domain level, not the per-port
54272fcbdcb4SDan Williams  * level. sas suspend/resume is async to allow parallel port recovery
54282fcbdcb4SDan Williams  * since sas has multiple ata_port instances per Scsi_Host.
54292fcbdcb4SDan Williams  */
54302fcbdcb4SDan Williams int ata_sas_port_async_suspend(struct ata_port *ap, int *async)
54312fcbdcb4SDan Williams {
54322fcbdcb4SDan Williams 	return __ata_port_suspend_common(ap, PMSG_SUSPEND, async);
54332fcbdcb4SDan Williams }
54342fcbdcb4SDan Williams EXPORT_SYMBOL_GPL(ata_sas_port_async_suspend);
54352fcbdcb4SDan Williams 
54362fcbdcb4SDan Williams int ata_sas_port_async_resume(struct ata_port *ap, int *async)
54372fcbdcb4SDan Williams {
54382fcbdcb4SDan Williams 	return __ata_port_resume_common(ap, async);
54392fcbdcb4SDan Williams }
54402fcbdcb4SDan Williams EXPORT_SYMBOL_GPL(ata_sas_port_async_resume);
54412fcbdcb4SDan Williams 
54422fcbdcb4SDan Williams 
5443c6fd2807SJeff Garzik /**
5444cca3974eSJeff Garzik  *	ata_host_suspend - suspend host
5445cca3974eSJeff Garzik  *	@host: host to suspend
5446c6fd2807SJeff Garzik  *	@mesg: PM message
5447c6fd2807SJeff Garzik  *
54485ef41082SLin Ming  *	Suspend @host.  Actual operation is performed by port suspend.
5449c6fd2807SJeff Garzik  */
5450cca3974eSJeff Garzik int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
5451c6fd2807SJeff Garzik {
545272ad6ec4SJeff Garzik 	host->dev->power.power_state = mesg;
54535ef41082SLin Ming 	return 0;
5454c6fd2807SJeff Garzik }
5455c6fd2807SJeff Garzik 
5456c6fd2807SJeff Garzik /**
5457cca3974eSJeff Garzik  *	ata_host_resume - resume host
5458cca3974eSJeff Garzik  *	@host: host to resume
5459c6fd2807SJeff Garzik  *
54605ef41082SLin Ming  *	Resume @host.  Actual operation is performed by port resume.
5461c6fd2807SJeff Garzik  */
5462cca3974eSJeff Garzik void ata_host_resume(struct ata_host *host)
5463c6fd2807SJeff Garzik {
546472ad6ec4SJeff Garzik 	host->dev->power.power_state = PMSG_ON;
5465c6fd2807SJeff Garzik }
54666ffa01d8STejun Heo #endif
5467c6fd2807SJeff Garzik 
54685ef41082SLin Ming struct device_type ata_port_type = {
54695ef41082SLin Ming 	.name = "ata_port",
54705ef41082SLin Ming #ifdef CONFIG_PM
54715ef41082SLin Ming 	.pm = &ata_port_pm_ops,
54725ef41082SLin Ming #endif
54735ef41082SLin Ming };
54745ef41082SLin Ming 
5475c6fd2807SJeff Garzik /**
5476c6fd2807SJeff Garzik  *	ata_dev_init - Initialize an ata_device structure
5477c6fd2807SJeff Garzik  *	@dev: Device structure to initialize
5478c6fd2807SJeff Garzik  *
5479c6fd2807SJeff Garzik  *	Initialize @dev in preparation for probing.
5480c6fd2807SJeff Garzik  *
5481c6fd2807SJeff Garzik  *	LOCKING:
5482c6fd2807SJeff Garzik  *	Inherited from caller.
5483c6fd2807SJeff Garzik  */
5484c6fd2807SJeff Garzik void ata_dev_init(struct ata_device *dev)
5485c6fd2807SJeff Garzik {
5486b1c72916STejun Heo 	struct ata_link *link = ata_dev_phys_link(dev);
54879af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
5488c6fd2807SJeff Garzik 	unsigned long flags;
5489c6fd2807SJeff Garzik 
5490b1c72916STejun Heo 	/* SATA spd limit is bound to the attached device, reset together */
54919af5c9c9STejun Heo 	link->sata_spd_limit = link->hw_sata_spd_limit;
54929af5c9c9STejun Heo 	link->sata_spd = 0;
5493c6fd2807SJeff Garzik 
5494c6fd2807SJeff Garzik 	/* High bits of dev->flags are used to record warm plug
5495c6fd2807SJeff Garzik 	 * requests which occur asynchronously.  Synchronize using
5496cca3974eSJeff Garzik 	 * host lock.
5497c6fd2807SJeff Garzik 	 */
5498c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
5499c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_INIT_MASK;
55003dcc323fSTejun Heo 	dev->horkage = 0;
5501c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
5502c6fd2807SJeff Garzik 
550399cf610aSTejun Heo 	memset((void *)dev + ATA_DEVICE_CLEAR_BEGIN, 0,
550499cf610aSTejun Heo 	       ATA_DEVICE_CLEAR_END - ATA_DEVICE_CLEAR_BEGIN);
5505c6fd2807SJeff Garzik 	dev->pio_mask = UINT_MAX;
5506c6fd2807SJeff Garzik 	dev->mwdma_mask = UINT_MAX;
5507c6fd2807SJeff Garzik 	dev->udma_mask = UINT_MAX;
5508c6fd2807SJeff Garzik }
5509c6fd2807SJeff Garzik 
5510c6fd2807SJeff Garzik /**
55114fb37a25STejun Heo  *	ata_link_init - Initialize an ata_link structure
55124fb37a25STejun Heo  *	@ap: ATA port link is attached to
55134fb37a25STejun Heo  *	@link: Link structure to initialize
55148989805dSTejun Heo  *	@pmp: Port multiplier port number
55154fb37a25STejun Heo  *
55164fb37a25STejun Heo  *	Initialize @link.
55174fb37a25STejun Heo  *
55184fb37a25STejun Heo  *	LOCKING:
55194fb37a25STejun Heo  *	Kernel thread context (may sleep)
55204fb37a25STejun Heo  */
5521fb7fd614STejun Heo void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
55224fb37a25STejun Heo {
55234fb37a25STejun Heo 	int i;
55244fb37a25STejun Heo 
55254fb37a25STejun Heo 	/* clear everything except for devices */
5526d9027470SGwendal Grignou 	memset((void *)link + ATA_LINK_CLEAR_BEGIN, 0,
5527d9027470SGwendal Grignou 	       ATA_LINK_CLEAR_END - ATA_LINK_CLEAR_BEGIN);
55284fb37a25STejun Heo 
55294fb37a25STejun Heo 	link->ap = ap;
55308989805dSTejun Heo 	link->pmp = pmp;
55314fb37a25STejun Heo 	link->active_tag = ATA_TAG_POISON;
55324fb37a25STejun Heo 	link->hw_sata_spd_limit = UINT_MAX;
55334fb37a25STejun Heo 
55344fb37a25STejun Heo 	/* can't use iterator, ap isn't initialized yet */
55354fb37a25STejun Heo 	for (i = 0; i < ATA_MAX_DEVICES; i++) {
55364fb37a25STejun Heo 		struct ata_device *dev = &link->device[i];
55374fb37a25STejun Heo 
55384fb37a25STejun Heo 		dev->link = link;
55394fb37a25STejun Heo 		dev->devno = dev - link->device;
5540110f66d2STejun Heo #ifdef CONFIG_ATA_ACPI
5541110f66d2STejun Heo 		dev->gtf_filter = ata_acpi_gtf_filter;
5542110f66d2STejun Heo #endif
55434fb37a25STejun Heo 		ata_dev_init(dev);
55444fb37a25STejun Heo 	}
55454fb37a25STejun Heo }
55464fb37a25STejun Heo 
55474fb37a25STejun Heo /**
55484fb37a25STejun Heo  *	sata_link_init_spd - Initialize link->sata_spd_limit
55494fb37a25STejun Heo  *	@link: Link to configure sata_spd_limit for
55504fb37a25STejun Heo  *
55514fb37a25STejun Heo  *	Initialize @link->[hw_]sata_spd_limit to the currently
55524fb37a25STejun Heo  *	configured value.
55534fb37a25STejun Heo  *
55544fb37a25STejun Heo  *	LOCKING:
55554fb37a25STejun Heo  *	Kernel thread context (may sleep).
55564fb37a25STejun Heo  *
55574fb37a25STejun Heo  *	RETURNS:
55584fb37a25STejun Heo  *	0 on success, -errno on failure.
55594fb37a25STejun Heo  */
5560fb7fd614STejun Heo int sata_link_init_spd(struct ata_link *link)
55614fb37a25STejun Heo {
556233267325STejun Heo 	u8 spd;
55634fb37a25STejun Heo 	int rc;
55644fb37a25STejun Heo 
5565d127ea7bSTejun Heo 	rc = sata_scr_read(link, SCR_CONTROL, &link->saved_scontrol);
55664fb37a25STejun Heo 	if (rc)
55674fb37a25STejun Heo 		return rc;
55684fb37a25STejun Heo 
5569d127ea7bSTejun Heo 	spd = (link->saved_scontrol >> 4) & 0xf;
55704fb37a25STejun Heo 	if (spd)
55714fb37a25STejun Heo 		link->hw_sata_spd_limit &= (1 << spd) - 1;
55724fb37a25STejun Heo 
557305944bdfSTejun Heo 	ata_force_link_limits(link);
557433267325STejun Heo 
55754fb37a25STejun Heo 	link->sata_spd_limit = link->hw_sata_spd_limit;
55764fb37a25STejun Heo 
55774fb37a25STejun Heo 	return 0;
55784fb37a25STejun Heo }
55794fb37a25STejun Heo 
55804fb37a25STejun Heo /**
5581f3187195STejun Heo  *	ata_port_alloc - allocate and initialize basic ATA port resources
5582f3187195STejun Heo  *	@host: ATA host this allocated port belongs to
5583c6fd2807SJeff Garzik  *
5584f3187195STejun Heo  *	Allocate and initialize basic ATA port resources.
5585f3187195STejun Heo  *
5586f3187195STejun Heo  *	RETURNS:
5587f3187195STejun Heo  *	Allocate ATA port on success, NULL on failure.
5588c6fd2807SJeff Garzik  *
5589c6fd2807SJeff Garzik  *	LOCKING:
5590f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5591c6fd2807SJeff Garzik  */
5592f3187195STejun Heo struct ata_port *ata_port_alloc(struct ata_host *host)
5593c6fd2807SJeff Garzik {
5594f3187195STejun Heo 	struct ata_port *ap;
5595c6fd2807SJeff Garzik 
5596f3187195STejun Heo 	DPRINTK("ENTER\n");
5597f3187195STejun Heo 
5598f3187195STejun Heo 	ap = kzalloc(sizeof(*ap), GFP_KERNEL);
5599f3187195STejun Heo 	if (!ap)
5600f3187195STejun Heo 		return NULL;
5601f3187195STejun Heo 
56027b3a24c5SMaxime Bizon 	ap->pflags |= ATA_PFLAG_INITIALIZING | ATA_PFLAG_FROZEN;
5603cca3974eSJeff Garzik 	ap->lock = &host->lock;
5604f3187195STejun Heo 	ap->print_id = -1;
5605cca3974eSJeff Garzik 	ap->host = host;
5606f3187195STejun Heo 	ap->dev = host->dev;
5607c6fd2807SJeff Garzik 
5608c6fd2807SJeff Garzik #if defined(ATA_VERBOSE_DEBUG)
5609c6fd2807SJeff Garzik 	/* turn on all debugging levels */
5610c6fd2807SJeff Garzik 	ap->msg_enable = 0x00FF;
5611c6fd2807SJeff Garzik #elif defined(ATA_DEBUG)
5612c6fd2807SJeff Garzik 	ap->msg_enable = ATA_MSG_DRV | ATA_MSG_INFO | ATA_MSG_CTL | ATA_MSG_WARN | ATA_MSG_ERR;
5613c6fd2807SJeff Garzik #else
5614c6fd2807SJeff Garzik 	ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
5615c6fd2807SJeff Garzik #endif
5616c6fd2807SJeff Garzik 
5617ad72cf98STejun Heo 	mutex_init(&ap->scsi_scan_mutex);
561865f27f38SDavid Howells 	INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
561965f27f38SDavid Howells 	INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
5620c6fd2807SJeff Garzik 	INIT_LIST_HEAD(&ap->eh_done_q);
5621c6fd2807SJeff Garzik 	init_waitqueue_head(&ap->eh_wait_q);
562245fabbb7SElias Oltmanns 	init_completion(&ap->park_req_pending);
56235ddf24c5STejun Heo 	init_timer_deferrable(&ap->fastdrain_timer);
56245ddf24c5STejun Heo 	ap->fastdrain_timer.function = ata_eh_fastdrain_timerfn;
56255ddf24c5STejun Heo 	ap->fastdrain_timer.data = (unsigned long)ap;
5626c6fd2807SJeff Garzik 
5627c6fd2807SJeff Garzik 	ap->cbl = ATA_CBL_NONE;
5628c6fd2807SJeff Garzik 
56298989805dSTejun Heo 	ata_link_init(ap, &ap->link, 0);
5630c6fd2807SJeff Garzik 
5631c6fd2807SJeff Garzik #ifdef ATA_IRQ_TRAP
5632c6fd2807SJeff Garzik 	ap->stats.unhandled_irq = 1;
5633c6fd2807SJeff Garzik 	ap->stats.idle_irq = 1;
5634c6fd2807SJeff Garzik #endif
5635270390e1STejun Heo 	ata_sff_port_init(ap);
5636270390e1STejun Heo 
5637c6fd2807SJeff Garzik 	return ap;
5638c6fd2807SJeff Garzik }
5639c6fd2807SJeff Garzik 
5640f0d36efdSTejun Heo static void ata_host_release(struct device *gendev, void *res)
5641f0d36efdSTejun Heo {
5642f0d36efdSTejun Heo 	struct ata_host *host = dev_get_drvdata(gendev);
5643f0d36efdSTejun Heo 	int i;
5644f0d36efdSTejun Heo 
5645f0d36efdSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
5646f0d36efdSTejun Heo 		struct ata_port *ap = host->ports[i];
5647f0d36efdSTejun Heo 
5648ecef7253STejun Heo 		if (!ap)
5649ecef7253STejun Heo 			continue;
5650ecef7253STejun Heo 
56514911487aSTejun Heo 		if (ap->scsi_host)
56521aa506e4STejun Heo 			scsi_host_put(ap->scsi_host);
56531aa506e4STejun Heo 
5654633273a3STejun Heo 		kfree(ap->pmp_link);
5655b1c72916STejun Heo 		kfree(ap->slave_link);
56564911487aSTejun Heo 		kfree(ap);
56571aa506e4STejun Heo 		host->ports[i] = NULL;
56581aa506e4STejun Heo 	}
56591aa506e4STejun Heo 
56601aa56ccaSTejun Heo 	dev_set_drvdata(gendev, NULL);
5661f0d36efdSTejun Heo }
5662f0d36efdSTejun Heo 
5663c6fd2807SJeff Garzik /**
5664f3187195STejun Heo  *	ata_host_alloc - allocate and init basic ATA host resources
5665f3187195STejun Heo  *	@dev: generic device this host is associated with
5666f3187195STejun Heo  *	@max_ports: maximum number of ATA ports associated with this host
5667f3187195STejun Heo  *
5668f3187195STejun Heo  *	Allocate and initialize basic ATA host resources.  LLD calls
5669f3187195STejun Heo  *	this function to allocate a host, initializes it fully and
5670f3187195STejun Heo  *	attaches it using ata_host_register().
5671f3187195STejun Heo  *
5672f3187195STejun Heo  *	@max_ports ports are allocated and host->n_ports is
5673f3187195STejun Heo  *	initialized to @max_ports.  The caller is allowed to decrease
5674f3187195STejun Heo  *	host->n_ports before calling ata_host_register().  The unused
5675f3187195STejun Heo  *	ports will be automatically freed on registration.
5676f3187195STejun Heo  *
5677f3187195STejun Heo  *	RETURNS:
5678f3187195STejun Heo  *	Allocate ATA host on success, NULL on failure.
5679f3187195STejun Heo  *
5680f3187195STejun Heo  *	LOCKING:
5681f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5682f3187195STejun Heo  */
5683f3187195STejun Heo struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
5684f3187195STejun Heo {
5685f3187195STejun Heo 	struct ata_host *host;
5686f3187195STejun Heo 	size_t sz;
5687f3187195STejun Heo 	int i;
5688f3187195STejun Heo 
5689f3187195STejun Heo 	DPRINTK("ENTER\n");
5690f3187195STejun Heo 
5691f3187195STejun Heo 	if (!devres_open_group(dev, NULL, GFP_KERNEL))
5692f3187195STejun Heo 		return NULL;
5693f3187195STejun Heo 
5694f3187195STejun Heo 	/* alloc a container for our list of ATA ports (buses) */
5695f3187195STejun Heo 	sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
5696f3187195STejun Heo 	/* alloc a container for our list of ATA ports (buses) */
5697f3187195STejun Heo 	host = devres_alloc(ata_host_release, sz, GFP_KERNEL);
5698f3187195STejun Heo 	if (!host)
5699f3187195STejun Heo 		goto err_out;
5700f3187195STejun Heo 
5701f3187195STejun Heo 	devres_add(dev, host);
5702f3187195STejun Heo 	dev_set_drvdata(dev, host);
5703f3187195STejun Heo 
5704f3187195STejun Heo 	spin_lock_init(&host->lock);
5705c0c362b6STejun Heo 	mutex_init(&host->eh_mutex);
5706f3187195STejun Heo 	host->dev = dev;
5707f3187195STejun Heo 	host->n_ports = max_ports;
5708f3187195STejun Heo 
5709f3187195STejun Heo 	/* allocate ports bound to this host */
5710f3187195STejun Heo 	for (i = 0; i < max_ports; i++) {
5711f3187195STejun Heo 		struct ata_port *ap;
5712f3187195STejun Heo 
5713f3187195STejun Heo 		ap = ata_port_alloc(host);
5714f3187195STejun Heo 		if (!ap)
5715f3187195STejun Heo 			goto err_out;
5716f3187195STejun Heo 
5717f3187195STejun Heo 		ap->port_no = i;
5718f3187195STejun Heo 		host->ports[i] = ap;
5719f3187195STejun Heo 	}
5720f3187195STejun Heo 
5721f3187195STejun Heo 	devres_remove_group(dev, NULL);
5722f3187195STejun Heo 	return host;
5723f3187195STejun Heo 
5724f3187195STejun Heo  err_out:
5725f3187195STejun Heo 	devres_release_group(dev, NULL);
5726f3187195STejun Heo 	return NULL;
5727f3187195STejun Heo }
5728f3187195STejun Heo 
5729f3187195STejun Heo /**
5730f5cda257STejun Heo  *	ata_host_alloc_pinfo - alloc host and init with port_info array
5731f5cda257STejun Heo  *	@dev: generic device this host is associated with
5732f5cda257STejun Heo  *	@ppi: array of ATA port_info to initialize host with
5733f5cda257STejun Heo  *	@n_ports: number of ATA ports attached to this host
5734f5cda257STejun Heo  *
5735f5cda257STejun Heo  *	Allocate ATA host and initialize with info from @ppi.  If NULL
5736f5cda257STejun Heo  *	terminated, @ppi may contain fewer entries than @n_ports.  The
5737f5cda257STejun Heo  *	last entry will be used for the remaining ports.
5738f5cda257STejun Heo  *
5739f5cda257STejun Heo  *	RETURNS:
5740f5cda257STejun Heo  *	Allocate ATA host on success, NULL on failure.
5741f5cda257STejun Heo  *
5742f5cda257STejun Heo  *	LOCKING:
5743f5cda257STejun Heo  *	Inherited from calling layer (may sleep).
5744f5cda257STejun Heo  */
5745f5cda257STejun Heo struct ata_host *ata_host_alloc_pinfo(struct device *dev,
5746f5cda257STejun Heo 				      const struct ata_port_info * const * ppi,
5747f5cda257STejun Heo 				      int n_ports)
5748f5cda257STejun Heo {
5749f5cda257STejun Heo 	const struct ata_port_info *pi;
5750f5cda257STejun Heo 	struct ata_host *host;
5751f5cda257STejun Heo 	int i, j;
5752f5cda257STejun Heo 
5753f5cda257STejun Heo 	host = ata_host_alloc(dev, n_ports);
5754f5cda257STejun Heo 	if (!host)
5755f5cda257STejun Heo 		return NULL;
5756f5cda257STejun Heo 
5757f5cda257STejun Heo 	for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
5758f5cda257STejun Heo 		struct ata_port *ap = host->ports[i];
5759f5cda257STejun Heo 
5760f5cda257STejun Heo 		if (ppi[j])
5761f5cda257STejun Heo 			pi = ppi[j++];
5762f5cda257STejun Heo 
5763f5cda257STejun Heo 		ap->pio_mask = pi->pio_mask;
5764f5cda257STejun Heo 		ap->mwdma_mask = pi->mwdma_mask;
5765f5cda257STejun Heo 		ap->udma_mask = pi->udma_mask;
5766f5cda257STejun Heo 		ap->flags |= pi->flags;
57670c88758bSTejun Heo 		ap->link.flags |= pi->link_flags;
5768f5cda257STejun Heo 		ap->ops = pi->port_ops;
5769f5cda257STejun Heo 
5770f5cda257STejun Heo 		if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
5771f5cda257STejun Heo 			host->ops = pi->port_ops;
5772f5cda257STejun Heo 	}
5773f5cda257STejun Heo 
5774f5cda257STejun Heo 	return host;
5775f5cda257STejun Heo }
5776f5cda257STejun Heo 
5777b1c72916STejun Heo /**
5778b1c72916STejun Heo  *	ata_slave_link_init - initialize slave link
5779b1c72916STejun Heo  *	@ap: port to initialize slave link for
5780b1c72916STejun Heo  *
5781b1c72916STejun Heo  *	Create and initialize slave link for @ap.  This enables slave
5782b1c72916STejun Heo  *	link handling on the port.
5783b1c72916STejun Heo  *
5784b1c72916STejun Heo  *	In libata, a port contains links and a link contains devices.
5785b1c72916STejun Heo  *	There is single host link but if a PMP is attached to it,
5786b1c72916STejun Heo  *	there can be multiple fan-out links.  On SATA, there's usually
5787b1c72916STejun Heo  *	a single device connected to a link but PATA and SATA
5788b1c72916STejun Heo  *	controllers emulating TF based interface can have two - master
5789b1c72916STejun Heo  *	and slave.
5790b1c72916STejun Heo  *
5791b1c72916STejun Heo  *	However, there are a few controllers which don't fit into this
5792b1c72916STejun Heo  *	abstraction too well - SATA controllers which emulate TF
5793b1c72916STejun Heo  *	interface with both master and slave devices but also have
5794b1c72916STejun Heo  *	separate SCR register sets for each device.  These controllers
5795b1c72916STejun Heo  *	need separate links for physical link handling
5796b1c72916STejun Heo  *	(e.g. onlineness, link speed) but should be treated like a
5797b1c72916STejun Heo  *	traditional M/S controller for everything else (e.g. command
5798b1c72916STejun Heo  *	issue, softreset).
5799b1c72916STejun Heo  *
5800b1c72916STejun Heo  *	slave_link is libata's way of handling this class of
5801b1c72916STejun Heo  *	controllers without impacting core layer too much.  For
5802b1c72916STejun Heo  *	anything other than physical link handling, the default host
5803b1c72916STejun Heo  *	link is used for both master and slave.  For physical link
5804b1c72916STejun Heo  *	handling, separate @ap->slave_link is used.  All dirty details
5805b1c72916STejun Heo  *	are implemented inside libata core layer.  From LLD's POV, the
5806b1c72916STejun Heo  *	only difference is that prereset, hardreset and postreset are
5807b1c72916STejun Heo  *	called once more for the slave link, so the reset sequence
5808b1c72916STejun Heo  *	looks like the following.
5809b1c72916STejun Heo  *
5810b1c72916STejun Heo  *	prereset(M) -> prereset(S) -> hardreset(M) -> hardreset(S) ->
5811b1c72916STejun Heo  *	softreset(M) -> postreset(M) -> postreset(S)
5812b1c72916STejun Heo  *
5813b1c72916STejun Heo  *	Note that softreset is called only for the master.  Softreset
5814b1c72916STejun Heo  *	resets both M/S by definition, so SRST on master should handle
5815b1c72916STejun Heo  *	both (the standard method will work just fine).
5816b1c72916STejun Heo  *
5817b1c72916STejun Heo  *	LOCKING:
5818b1c72916STejun Heo  *	Should be called before host is registered.
5819b1c72916STejun Heo  *
5820b1c72916STejun Heo  *	RETURNS:
5821b1c72916STejun Heo  *	0 on success, -errno on failure.
5822b1c72916STejun Heo  */
5823b1c72916STejun Heo int ata_slave_link_init(struct ata_port *ap)
5824b1c72916STejun Heo {
5825b1c72916STejun Heo 	struct ata_link *link;
5826b1c72916STejun Heo 
5827b1c72916STejun Heo 	WARN_ON(ap->slave_link);
5828b1c72916STejun Heo 	WARN_ON(ap->flags & ATA_FLAG_PMP);
5829b1c72916STejun Heo 
5830b1c72916STejun Heo 	link = kzalloc(sizeof(*link), GFP_KERNEL);
5831b1c72916STejun Heo 	if (!link)
5832b1c72916STejun Heo 		return -ENOMEM;
5833b1c72916STejun Heo 
5834b1c72916STejun Heo 	ata_link_init(ap, link, 1);
5835b1c72916STejun Heo 	ap->slave_link = link;
5836b1c72916STejun Heo 	return 0;
5837b1c72916STejun Heo }
5838b1c72916STejun Heo 
583932ebbc0cSTejun Heo static void ata_host_stop(struct device *gendev, void *res)
584032ebbc0cSTejun Heo {
584132ebbc0cSTejun Heo 	struct ata_host *host = dev_get_drvdata(gendev);
584232ebbc0cSTejun Heo 	int i;
584332ebbc0cSTejun Heo 
584432ebbc0cSTejun Heo 	WARN_ON(!(host->flags & ATA_HOST_STARTED));
584532ebbc0cSTejun Heo 
584632ebbc0cSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
584732ebbc0cSTejun Heo 		struct ata_port *ap = host->ports[i];
584832ebbc0cSTejun Heo 
584932ebbc0cSTejun Heo 		if (ap->ops->port_stop)
585032ebbc0cSTejun Heo 			ap->ops->port_stop(ap);
585132ebbc0cSTejun Heo 	}
585232ebbc0cSTejun Heo 
585332ebbc0cSTejun Heo 	if (host->ops->host_stop)
585432ebbc0cSTejun Heo 		host->ops->host_stop(host);
585532ebbc0cSTejun Heo }
585632ebbc0cSTejun Heo 
5857f5cda257STejun Heo /**
5858029cfd6bSTejun Heo  *	ata_finalize_port_ops - finalize ata_port_operations
5859029cfd6bSTejun Heo  *	@ops: ata_port_operations to finalize
5860029cfd6bSTejun Heo  *
5861029cfd6bSTejun Heo  *	An ata_port_operations can inherit from another ops and that
5862029cfd6bSTejun Heo  *	ops can again inherit from another.  This can go on as many
5863029cfd6bSTejun Heo  *	times as necessary as long as there is no loop in the
5864029cfd6bSTejun Heo  *	inheritance chain.
5865029cfd6bSTejun Heo  *
5866029cfd6bSTejun Heo  *	Ops tables are finalized when the host is started.  NULL or
5867029cfd6bSTejun Heo  *	unspecified entries are inherited from the closet ancestor
5868029cfd6bSTejun Heo  *	which has the method and the entry is populated with it.
5869029cfd6bSTejun Heo  *	After finalization, the ops table directly points to all the
5870029cfd6bSTejun Heo  *	methods and ->inherits is no longer necessary and cleared.
5871029cfd6bSTejun Heo  *
5872029cfd6bSTejun Heo  *	Using ATA_OP_NULL, inheriting ops can force a method to NULL.
5873029cfd6bSTejun Heo  *
5874029cfd6bSTejun Heo  *	LOCKING:
5875029cfd6bSTejun Heo  *	None.
5876029cfd6bSTejun Heo  */
5877029cfd6bSTejun Heo static void ata_finalize_port_ops(struct ata_port_operations *ops)
5878029cfd6bSTejun Heo {
58792da67659SPradeep Singh Rautela 	static DEFINE_SPINLOCK(lock);
5880029cfd6bSTejun Heo 	const struct ata_port_operations *cur;
5881029cfd6bSTejun Heo 	void **begin = (void **)ops;
5882029cfd6bSTejun Heo 	void **end = (void **)&ops->inherits;
5883029cfd6bSTejun Heo 	void **pp;
5884029cfd6bSTejun Heo 
5885029cfd6bSTejun Heo 	if (!ops || !ops->inherits)
5886029cfd6bSTejun Heo 		return;
5887029cfd6bSTejun Heo 
5888029cfd6bSTejun Heo 	spin_lock(&lock);
5889029cfd6bSTejun Heo 
5890029cfd6bSTejun Heo 	for (cur = ops->inherits; cur; cur = cur->inherits) {
5891029cfd6bSTejun Heo 		void **inherit = (void **)cur;
5892029cfd6bSTejun Heo 
5893029cfd6bSTejun Heo 		for (pp = begin; pp < end; pp++, inherit++)
5894029cfd6bSTejun Heo 			if (!*pp)
5895029cfd6bSTejun Heo 				*pp = *inherit;
5896029cfd6bSTejun Heo 	}
5897029cfd6bSTejun Heo 
5898029cfd6bSTejun Heo 	for (pp = begin; pp < end; pp++)
5899029cfd6bSTejun Heo 		if (IS_ERR(*pp))
5900029cfd6bSTejun Heo 			*pp = NULL;
5901029cfd6bSTejun Heo 
5902029cfd6bSTejun Heo 	ops->inherits = NULL;
5903029cfd6bSTejun Heo 
5904029cfd6bSTejun Heo 	spin_unlock(&lock);
5905029cfd6bSTejun Heo }
5906029cfd6bSTejun Heo 
5907029cfd6bSTejun Heo /**
5908ecef7253STejun Heo  *	ata_host_start - start and freeze ports of an ATA host
5909ecef7253STejun Heo  *	@host: ATA host to start ports for
5910ecef7253STejun Heo  *
5911ecef7253STejun Heo  *	Start and then freeze ports of @host.  Started status is
5912ecef7253STejun Heo  *	recorded in host->flags, so this function can be called
5913ecef7253STejun Heo  *	multiple times.  Ports are guaranteed to get started only
5914f3187195STejun Heo  *	once.  If host->ops isn't initialized yet, its set to the
5915f3187195STejun Heo  *	first non-dummy port ops.
5916ecef7253STejun Heo  *
5917ecef7253STejun Heo  *	LOCKING:
5918ecef7253STejun Heo  *	Inherited from calling layer (may sleep).
5919ecef7253STejun Heo  *
5920ecef7253STejun Heo  *	RETURNS:
5921ecef7253STejun Heo  *	0 if all ports are started successfully, -errno otherwise.
5922ecef7253STejun Heo  */
5923ecef7253STejun Heo int ata_host_start(struct ata_host *host)
5924ecef7253STejun Heo {
592532ebbc0cSTejun Heo 	int have_stop = 0;
592632ebbc0cSTejun Heo 	void *start_dr = NULL;
5927ecef7253STejun Heo 	int i, rc;
5928ecef7253STejun Heo 
5929ecef7253STejun Heo 	if (host->flags & ATA_HOST_STARTED)
5930ecef7253STejun Heo 		return 0;
5931ecef7253STejun Heo 
5932029cfd6bSTejun Heo 	ata_finalize_port_ops(host->ops);
5933029cfd6bSTejun Heo 
5934ecef7253STejun Heo 	for (i = 0; i < host->n_ports; i++) {
5935ecef7253STejun Heo 		struct ata_port *ap = host->ports[i];
5936ecef7253STejun Heo 
5937029cfd6bSTejun Heo 		ata_finalize_port_ops(ap->ops);
5938029cfd6bSTejun Heo 
5939f3187195STejun Heo 		if (!host->ops && !ata_port_is_dummy(ap))
5940f3187195STejun Heo 			host->ops = ap->ops;
5941f3187195STejun Heo 
594232ebbc0cSTejun Heo 		if (ap->ops->port_stop)
594332ebbc0cSTejun Heo 			have_stop = 1;
594432ebbc0cSTejun Heo 	}
594532ebbc0cSTejun Heo 
594632ebbc0cSTejun Heo 	if (host->ops->host_stop)
594732ebbc0cSTejun Heo 		have_stop = 1;
594832ebbc0cSTejun Heo 
594932ebbc0cSTejun Heo 	if (have_stop) {
595032ebbc0cSTejun Heo 		start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
595132ebbc0cSTejun Heo 		if (!start_dr)
595232ebbc0cSTejun Heo 			return -ENOMEM;
595332ebbc0cSTejun Heo 	}
595432ebbc0cSTejun Heo 
595532ebbc0cSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
595632ebbc0cSTejun Heo 		struct ata_port *ap = host->ports[i];
595732ebbc0cSTejun Heo 
5958ecef7253STejun Heo 		if (ap->ops->port_start) {
5959ecef7253STejun Heo 			rc = ap->ops->port_start(ap);
5960ecef7253STejun Heo 			if (rc) {
59610f9fe9b7SAlan Cox 				if (rc != -ENODEV)
5962a44fec1fSJoe Perches 					dev_err(host->dev,
5963a44fec1fSJoe Perches 						"failed to start port %d (errno=%d)\n",
5964a44fec1fSJoe Perches 						i, rc);
5965ecef7253STejun Heo 				goto err_out;
5966ecef7253STejun Heo 			}
5967ecef7253STejun Heo 		}
5968ecef7253STejun Heo 		ata_eh_freeze_port(ap);
5969ecef7253STejun Heo 	}
5970ecef7253STejun Heo 
597132ebbc0cSTejun Heo 	if (start_dr)
597232ebbc0cSTejun Heo 		devres_add(host->dev, start_dr);
5973ecef7253STejun Heo 	host->flags |= ATA_HOST_STARTED;
5974ecef7253STejun Heo 	return 0;
5975ecef7253STejun Heo 
5976ecef7253STejun Heo  err_out:
5977ecef7253STejun Heo 	while (--i >= 0) {
5978ecef7253STejun Heo 		struct ata_port *ap = host->ports[i];
5979ecef7253STejun Heo 
5980ecef7253STejun Heo 		if (ap->ops->port_stop)
5981ecef7253STejun Heo 			ap->ops->port_stop(ap);
5982ecef7253STejun Heo 	}
598332ebbc0cSTejun Heo 	devres_free(start_dr);
5984ecef7253STejun Heo 	return rc;
5985ecef7253STejun Heo }
5986ecef7253STejun Heo 
5987ecef7253STejun Heo /**
59888d8e7d13SDan Williams  *	ata_sas_host_init - Initialize a host struct for sas (ipr, libsas)
5989cca3974eSJeff Garzik  *	@host:	host to initialize
5990cca3974eSJeff Garzik  *	@dev:	device host is attached to
5991c6fd2807SJeff Garzik  *	@ops:	port_ops
5992c6fd2807SJeff Garzik  *
5993c6fd2807SJeff Garzik  */
5994cca3974eSJeff Garzik void ata_host_init(struct ata_host *host, struct device *dev,
59958d8e7d13SDan Williams 		   struct ata_port_operations *ops)
5996c6fd2807SJeff Garzik {
5997cca3974eSJeff Garzik 	spin_lock_init(&host->lock);
5998c0c362b6STejun Heo 	mutex_init(&host->eh_mutex);
5999cca3974eSJeff Garzik 	host->dev = dev;
6000cca3974eSJeff Garzik 	host->ops = ops;
6001c6fd2807SJeff Garzik }
6002c6fd2807SJeff Garzik 
60039508a66fSDan Williams void __ata_port_probe(struct ata_port *ap)
600479318057SArjan van de Ven {
600579318057SArjan van de Ven 	struct ata_eh_info *ehi = &ap->link.eh_info;
600679318057SArjan van de Ven 	unsigned long flags;
600779318057SArjan van de Ven 
600879318057SArjan van de Ven 	/* kick EH for boot probing */
600979318057SArjan van de Ven 	spin_lock_irqsave(ap->lock, flags);
601079318057SArjan van de Ven 
601179318057SArjan van de Ven 	ehi->probe_mask |= ATA_ALL_DEVICES;
60126b7ae954STejun Heo 	ehi->action |= ATA_EH_RESET;
601379318057SArjan van de Ven 	ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
601479318057SArjan van de Ven 
601579318057SArjan van de Ven 	ap->pflags &= ~ATA_PFLAG_INITIALIZING;
601679318057SArjan van de Ven 	ap->pflags |= ATA_PFLAG_LOADING;
601779318057SArjan van de Ven 	ata_port_schedule_eh(ap);
601879318057SArjan van de Ven 
601979318057SArjan van de Ven 	spin_unlock_irqrestore(ap->lock, flags);
60209508a66fSDan Williams }
602179318057SArjan van de Ven 
60229508a66fSDan Williams int ata_port_probe(struct ata_port *ap)
60239508a66fSDan Williams {
60249508a66fSDan Williams 	int rc = 0;
60259508a66fSDan Williams 
60269508a66fSDan Williams 	if (ap->ops->error_handler) {
60279508a66fSDan Williams 		__ata_port_probe(ap);
602879318057SArjan van de Ven 		ata_port_wait_eh(ap);
602979318057SArjan van de Ven 	} else {
603079318057SArjan van de Ven 		DPRINTK("ata%u: bus probe begin\n", ap->print_id);
603179318057SArjan van de Ven 		rc = ata_bus_probe(ap);
603279318057SArjan van de Ven 		DPRINTK("ata%u: bus probe end\n", ap->print_id);
6033238c9cf9SJames Bottomley 	}
6034238c9cf9SJames Bottomley 	return rc;
6035238c9cf9SJames Bottomley }
603679318057SArjan van de Ven 
6037238c9cf9SJames Bottomley 
6038238c9cf9SJames Bottomley static void async_port_probe(void *data, async_cookie_t cookie)
6039238c9cf9SJames Bottomley {
6040238c9cf9SJames Bottomley 	struct ata_port *ap = data;
6041238c9cf9SJames Bottomley 
6042238c9cf9SJames Bottomley 	/*
6043238c9cf9SJames Bottomley 	 * If we're not allowed to scan this host in parallel,
6044238c9cf9SJames Bottomley 	 * we need to wait until all previous scans have completed
6045238c9cf9SJames Bottomley 	 * before going further.
6046238c9cf9SJames Bottomley 	 * Jeff Garzik says this is only within a controller, so we
6047238c9cf9SJames Bottomley 	 * don't need to wait for port 0, only for later ports.
604879318057SArjan van de Ven 	 */
6049238c9cf9SJames Bottomley 	if (!(ap->host->flags & ATA_HOST_PARALLEL_SCAN) && ap->port_no != 0)
6050238c9cf9SJames Bottomley 		async_synchronize_cookie(cookie);
6051238c9cf9SJames Bottomley 
6052238c9cf9SJames Bottomley 	(void)ata_port_probe(ap);
6053f29d3b23SArjan van de Ven 
6054f29d3b23SArjan van de Ven 	/* in order to keep device order, we need to synchronize at this point */
6055f29d3b23SArjan van de Ven 	async_synchronize_cookie(cookie);
6056f29d3b23SArjan van de Ven 
6057f29d3b23SArjan van de Ven 	ata_scsi_scan_host(ap, 1);
605879318057SArjan van de Ven }
6059238c9cf9SJames Bottomley 
6060c6fd2807SJeff Garzik /**
6061f3187195STejun Heo  *	ata_host_register - register initialized ATA host
6062f3187195STejun Heo  *	@host: ATA host to register
6063f3187195STejun Heo  *	@sht: template for SCSI host
6064c6fd2807SJeff Garzik  *
6065f3187195STejun Heo  *	Register initialized ATA host.  @host is allocated using
6066f3187195STejun Heo  *	ata_host_alloc() and fully initialized by LLD.  This function
6067f3187195STejun Heo  *	starts ports, registers @host with ATA and SCSI layers and
6068f3187195STejun Heo  *	probe registered devices.
6069c6fd2807SJeff Garzik  *
6070c6fd2807SJeff Garzik  *	LOCKING:
6071f3187195STejun Heo  *	Inherited from calling layer (may sleep).
6072c6fd2807SJeff Garzik  *
6073c6fd2807SJeff Garzik  *	RETURNS:
6074f3187195STejun Heo  *	0 on success, -errno otherwise.
6075c6fd2807SJeff Garzik  */
6076f3187195STejun Heo int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
6077c6fd2807SJeff Garzik {
6078f3187195STejun Heo 	int i, rc;
6079c6fd2807SJeff Garzik 
6080f3187195STejun Heo 	/* host must have been started */
6081f3187195STejun Heo 	if (!(host->flags & ATA_HOST_STARTED)) {
6082a44fec1fSJoe Perches 		dev_err(host->dev, "BUG: trying to register unstarted host\n");
6083f3187195STejun Heo 		WARN_ON(1);
6084f3187195STejun Heo 		return -EINVAL;
608502f076aaSAlan Cox 	}
6086f0d36efdSTejun Heo 
6087f3187195STejun Heo 	/* Blow away unused ports.  This happens when LLD can't
6088f3187195STejun Heo 	 * determine the exact number of ports to allocate at
6089f3187195STejun Heo 	 * allocation time.
6090f3187195STejun Heo 	 */
6091f3187195STejun Heo 	for (i = host->n_ports; host->ports[i]; i++)
6092f3187195STejun Heo 		kfree(host->ports[i]);
6093f0d36efdSTejun Heo 
6094f3187195STejun Heo 	/* give ports names and add SCSI hosts */
6095f3187195STejun Heo 	for (i = 0; i < host->n_ports; i++)
609685d6725bSDan Williams 		host->ports[i]->print_id = atomic_inc_return(&ata_print_id);
6097c6fd2807SJeff Garzik 
6098d9027470SGwendal Grignou 
6099d9027470SGwendal Grignou 	/* Create associated sysfs transport objects  */
6100d9027470SGwendal Grignou 	for (i = 0; i < host->n_ports; i++) {
6101d9027470SGwendal Grignou 		rc = ata_tport_add(host->dev,host->ports[i]);
6102d9027470SGwendal Grignou 		if (rc) {
6103d9027470SGwendal Grignou 			goto err_tadd;
6104d9027470SGwendal Grignou 		}
6105d9027470SGwendal Grignou 	}
6106d9027470SGwendal Grignou 
6107f3187195STejun Heo 	rc = ata_scsi_add_hosts(host, sht);
6108ecef7253STejun Heo 	if (rc)
6109d9027470SGwendal Grignou 		goto err_tadd;
6110ecef7253STejun Heo 
6111f3187195STejun Heo 	/* set cable, sata_spd_limit and report */
6112cca3974eSJeff Garzik 	for (i = 0; i < host->n_ports; i++) {
6113cca3974eSJeff Garzik 		struct ata_port *ap = host->ports[i];
6114f3187195STejun Heo 		unsigned long xfer_mask;
6115f3187195STejun Heo 
6116f3187195STejun Heo 		/* set SATA cable type if still unset */
6117f3187195STejun Heo 		if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
6118f3187195STejun Heo 			ap->cbl = ATA_CBL_SATA;
6119c6fd2807SJeff Garzik 
6120c6fd2807SJeff Garzik 		/* init sata_spd_limit to the current value */
61214fb37a25STejun Heo 		sata_link_init_spd(&ap->link);
6122b1c72916STejun Heo 		if (ap->slave_link)
6123b1c72916STejun Heo 			sata_link_init_spd(ap->slave_link);
6124c6fd2807SJeff Garzik 
6125cbcdd875STejun Heo 		/* print per-port info to dmesg */
6126f3187195STejun Heo 		xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
6127f3187195STejun Heo 					      ap->udma_mask);
6128f3187195STejun Heo 
6129abf6e8edSTejun Heo 		if (!ata_port_is_dummy(ap)) {
6130a9a79dfeSJoe Perches 			ata_port_info(ap, "%cATA max %s %s\n",
6131a16abc0bSTejun Heo 				      (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
6132f3187195STejun Heo 				      ata_mode_string(xfer_mask),
6133cbcdd875STejun Heo 				      ap->link.eh_info.desc);
6134abf6e8edSTejun Heo 			ata_ehi_clear_desc(&ap->link.eh_info);
6135abf6e8edSTejun Heo 		} else
6136a9a79dfeSJoe Perches 			ata_port_info(ap, "DUMMY\n");
6137c6fd2807SJeff Garzik 	}
6138c6fd2807SJeff Garzik 
6139f6005354SVegard Nossum 	/* perform each probe asynchronously */
6140f3187195STejun Heo 	for (i = 0; i < host->n_ports; i++) {
6141f3187195STejun Heo 		struct ata_port *ap = host->ports[i];
614279318057SArjan van de Ven 		async_schedule(async_port_probe, ap);
6143c6fd2807SJeff Garzik 	}
6144c6fd2807SJeff Garzik 
6145f3187195STejun Heo 	return 0;
6146d9027470SGwendal Grignou 
6147d9027470SGwendal Grignou  err_tadd:
6148d9027470SGwendal Grignou 	while (--i >= 0) {
6149d9027470SGwendal Grignou 		ata_tport_delete(host->ports[i]);
6150d9027470SGwendal Grignou 	}
6151d9027470SGwendal Grignou 	return rc;
6152d9027470SGwendal Grignou 
6153f3187195STejun Heo }
6154f3187195STejun Heo 
6155f3187195STejun Heo /**
6156f5cda257STejun Heo  *	ata_host_activate - start host, request IRQ and register it
6157f5cda257STejun Heo  *	@host: target ATA host
6158f5cda257STejun Heo  *	@irq: IRQ to request
6159f5cda257STejun Heo  *	@irq_handler: irq_handler used when requesting IRQ
6160f5cda257STejun Heo  *	@irq_flags: irq_flags used when requesting IRQ
6161f5cda257STejun Heo  *	@sht: scsi_host_template to use when registering the host
6162f5cda257STejun Heo  *
6163f5cda257STejun Heo  *	After allocating an ATA host and initializing it, most libata
6164f5cda257STejun Heo  *	LLDs perform three steps to activate the host - start host,
6165f5cda257STejun Heo  *	request IRQ and register it.  This helper takes necessasry
6166f5cda257STejun Heo  *	arguments and performs the three steps in one go.
6167f5cda257STejun Heo  *
61683d46b2e2SPaul Mundt  *	An invalid IRQ skips the IRQ registration and expects the host to
61693d46b2e2SPaul Mundt  *	have set polling mode on the port. In this case, @irq_handler
61703d46b2e2SPaul Mundt  *	should be NULL.
61713d46b2e2SPaul Mundt  *
6172f5cda257STejun Heo  *	LOCKING:
6173f5cda257STejun Heo  *	Inherited from calling layer (may sleep).
6174f5cda257STejun Heo  *
6175f5cda257STejun Heo  *	RETURNS:
6176f5cda257STejun Heo  *	0 on success, -errno otherwise.
6177f5cda257STejun Heo  */
6178f5cda257STejun Heo int ata_host_activate(struct ata_host *host, int irq,
6179f5cda257STejun Heo 		      irq_handler_t irq_handler, unsigned long irq_flags,
6180f5cda257STejun Heo 		      struct scsi_host_template *sht)
6181f5cda257STejun Heo {
6182cbcdd875STejun Heo 	int i, rc;
6183f5cda257STejun Heo 
6184f5cda257STejun Heo 	rc = ata_host_start(host);
6185f5cda257STejun Heo 	if (rc)
6186f5cda257STejun Heo 		return rc;
6187f5cda257STejun Heo 
61883d46b2e2SPaul Mundt 	/* Special case for polling mode */
61893d46b2e2SPaul Mundt 	if (!irq) {
61903d46b2e2SPaul Mundt 		WARN_ON(irq_handler);
61913d46b2e2SPaul Mundt 		return ata_host_register(host, sht);
61923d46b2e2SPaul Mundt 	}
61933d46b2e2SPaul Mundt 
6194f5cda257STejun Heo 	rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
6195f5cda257STejun Heo 			      dev_driver_string(host->dev), host);
6196f5cda257STejun Heo 	if (rc)
6197f5cda257STejun Heo 		return rc;
6198f5cda257STejun Heo 
6199cbcdd875STejun Heo 	for (i = 0; i < host->n_ports; i++)
6200cbcdd875STejun Heo 		ata_port_desc(host->ports[i], "irq %d", irq);
62014031826bSTejun Heo 
6202f5cda257STejun Heo 	rc = ata_host_register(host, sht);
6203f5cda257STejun Heo 	/* if failed, just free the IRQ and leave ports alone */
6204f5cda257STejun Heo 	if (rc)
6205f5cda257STejun Heo 		devm_free_irq(host->dev, irq, host);
6206f5cda257STejun Heo 
6207f5cda257STejun Heo 	return rc;
6208f5cda257STejun Heo }
6209f5cda257STejun Heo 
6210f5cda257STejun Heo /**
6211c6fd2807SJeff Garzik  *	ata_port_detach - Detach ATA port in prepration of device removal
6212c6fd2807SJeff Garzik  *	@ap: ATA port to be detached
6213c6fd2807SJeff Garzik  *
6214c6fd2807SJeff Garzik  *	Detach all ATA devices and the associated SCSI devices of @ap;
6215c6fd2807SJeff Garzik  *	then, remove the associated SCSI host.  @ap is guaranteed to
6216c6fd2807SJeff Garzik  *	be quiescent on return from this function.
6217c6fd2807SJeff Garzik  *
6218c6fd2807SJeff Garzik  *	LOCKING:
6219c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
6220c6fd2807SJeff Garzik  */
6221741b7763SAdrian Bunk static void ata_port_detach(struct ata_port *ap)
6222c6fd2807SJeff Garzik {
6223c6fd2807SJeff Garzik 	unsigned long flags;
6224c6fd2807SJeff Garzik 
6225c6fd2807SJeff Garzik 	if (!ap->ops->error_handler)
6226c6fd2807SJeff Garzik 		goto skip_eh;
6227c6fd2807SJeff Garzik 
6228c6fd2807SJeff Garzik 	/* tell EH we're leaving & flush EH */
6229c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
6230c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_UNLOADING;
6231ece180d1STejun Heo 	ata_port_schedule_eh(ap);
6232c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
6233c6fd2807SJeff Garzik 
6234ece180d1STejun Heo 	/* wait till EH commits suicide */
6235c6fd2807SJeff Garzik 	ata_port_wait_eh(ap);
6236c6fd2807SJeff Garzik 
6237ece180d1STejun Heo 	/* it better be dead now */
6238ece180d1STejun Heo 	WARN_ON(!(ap->pflags & ATA_PFLAG_UNLOADED));
6239c6fd2807SJeff Garzik 
6240afe2c511STejun Heo 	cancel_delayed_work_sync(&ap->hotplug_task);
6241c6fd2807SJeff Garzik 
6242c6fd2807SJeff Garzik  skip_eh:
6243d9027470SGwendal Grignou 	if (ap->pmp_link) {
6244d9027470SGwendal Grignou 		int i;
6245d9027470SGwendal Grignou 		for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
6246d9027470SGwendal Grignou 			ata_tlink_delete(&ap->pmp_link[i]);
6247d9027470SGwendal Grignou 	}
6248d9027470SGwendal Grignou 	ata_tport_delete(ap);
6249d9027470SGwendal Grignou 
6250c6fd2807SJeff Garzik 	/* remove the associated SCSI host */
6251cca3974eSJeff Garzik 	scsi_remove_host(ap->scsi_host);
6252c6fd2807SJeff Garzik }
6253c6fd2807SJeff Garzik 
6254c6fd2807SJeff Garzik /**
62550529c159STejun Heo  *	ata_host_detach - Detach all ports of an ATA host
62560529c159STejun Heo  *	@host: Host to detach
62570529c159STejun Heo  *
62580529c159STejun Heo  *	Detach all ports of @host.
62590529c159STejun Heo  *
62600529c159STejun Heo  *	LOCKING:
62610529c159STejun Heo  *	Kernel thread context (may sleep).
62620529c159STejun Heo  */
62630529c159STejun Heo void ata_host_detach(struct ata_host *host)
62640529c159STejun Heo {
62650529c159STejun Heo 	int i;
62660529c159STejun Heo 
62670529c159STejun Heo 	for (i = 0; i < host->n_ports; i++)
62680529c159STejun Heo 		ata_port_detach(host->ports[i]);
6269562f0c2dSTejun Heo 
6270562f0c2dSTejun Heo 	/* the host is dead now, dissociate ACPI */
6271562f0c2dSTejun Heo 	ata_acpi_dissociate(host);
62720529c159STejun Heo }
62730529c159STejun Heo 
6274c6fd2807SJeff Garzik #ifdef CONFIG_PCI
6275c6fd2807SJeff Garzik 
6276c6fd2807SJeff Garzik /**
6277c6fd2807SJeff Garzik  *	ata_pci_remove_one - PCI layer callback for device removal
6278c6fd2807SJeff Garzik  *	@pdev: PCI device that was removed
6279c6fd2807SJeff Garzik  *
6280b878ca5dSTejun Heo  *	PCI layer indicates to libata via this hook that hot-unplug or
6281b878ca5dSTejun Heo  *	module unload event has occurred.  Detach all ports.  Resource
6282b878ca5dSTejun Heo  *	release is handled via devres.
6283c6fd2807SJeff Garzik  *
6284c6fd2807SJeff Garzik  *	LOCKING:
6285c6fd2807SJeff Garzik  *	Inherited from PCI layer (may sleep).
6286c6fd2807SJeff Garzik  */
6287c6fd2807SJeff Garzik void ata_pci_remove_one(struct pci_dev *pdev)
6288c6fd2807SJeff Garzik {
62892855568bSJeff Garzik 	struct device *dev = &pdev->dev;
6290cca3974eSJeff Garzik 	struct ata_host *host = dev_get_drvdata(dev);
6291c6fd2807SJeff Garzik 
6292f0d36efdSTejun Heo 	ata_host_detach(host);
6293c6fd2807SJeff Garzik }
6294c6fd2807SJeff Garzik 
6295c6fd2807SJeff Garzik /* move to PCI subsystem */
6296c6fd2807SJeff Garzik int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
6297c6fd2807SJeff Garzik {
6298c6fd2807SJeff Garzik 	unsigned long tmp = 0;
6299c6fd2807SJeff Garzik 
6300c6fd2807SJeff Garzik 	switch (bits->width) {
6301c6fd2807SJeff Garzik 	case 1: {
6302c6fd2807SJeff Garzik 		u8 tmp8 = 0;
6303c6fd2807SJeff Garzik 		pci_read_config_byte(pdev, bits->reg, &tmp8);
6304c6fd2807SJeff Garzik 		tmp = tmp8;
6305c6fd2807SJeff Garzik 		break;
6306c6fd2807SJeff Garzik 	}
6307c6fd2807SJeff Garzik 	case 2: {
6308c6fd2807SJeff Garzik 		u16 tmp16 = 0;
6309c6fd2807SJeff Garzik 		pci_read_config_word(pdev, bits->reg, &tmp16);
6310c6fd2807SJeff Garzik 		tmp = tmp16;
6311c6fd2807SJeff Garzik 		break;
6312c6fd2807SJeff Garzik 	}
6313c6fd2807SJeff Garzik 	case 4: {
6314c6fd2807SJeff Garzik 		u32 tmp32 = 0;
6315c6fd2807SJeff Garzik 		pci_read_config_dword(pdev, bits->reg, &tmp32);
6316c6fd2807SJeff Garzik 		tmp = tmp32;
6317c6fd2807SJeff Garzik 		break;
6318c6fd2807SJeff Garzik 	}
6319c6fd2807SJeff Garzik 
6320c6fd2807SJeff Garzik 	default:
6321c6fd2807SJeff Garzik 		return -EINVAL;
6322c6fd2807SJeff Garzik 	}
6323c6fd2807SJeff Garzik 
6324c6fd2807SJeff Garzik 	tmp &= bits->mask;
6325c6fd2807SJeff Garzik 
6326c6fd2807SJeff Garzik 	return (tmp == bits->val) ? 1 : 0;
6327c6fd2807SJeff Garzik }
6328c6fd2807SJeff Garzik 
63296ffa01d8STejun Heo #ifdef CONFIG_PM
6330c6fd2807SJeff Garzik void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
6331c6fd2807SJeff Garzik {
6332c6fd2807SJeff Garzik 	pci_save_state(pdev);
6333c6fd2807SJeff Garzik 	pci_disable_device(pdev);
63344c90d971STejun Heo 
63353a2d5b70SRafael J. Wysocki 	if (mesg.event & PM_EVENT_SLEEP)
6336c6fd2807SJeff Garzik 		pci_set_power_state(pdev, PCI_D3hot);
6337c6fd2807SJeff Garzik }
6338c6fd2807SJeff Garzik 
6339553c4aa6STejun Heo int ata_pci_device_do_resume(struct pci_dev *pdev)
6340c6fd2807SJeff Garzik {
6341553c4aa6STejun Heo 	int rc;
6342553c4aa6STejun Heo 
6343c6fd2807SJeff Garzik 	pci_set_power_state(pdev, PCI_D0);
6344c6fd2807SJeff Garzik 	pci_restore_state(pdev);
6345553c4aa6STejun Heo 
6346f0d36efdSTejun Heo 	rc = pcim_enable_device(pdev);
6347553c4aa6STejun Heo 	if (rc) {
6348a44fec1fSJoe Perches 		dev_err(&pdev->dev,
6349553c4aa6STejun Heo 			"failed to enable device after resume (%d)\n", rc);
6350553c4aa6STejun Heo 		return rc;
6351553c4aa6STejun Heo 	}
6352553c4aa6STejun Heo 
6353c6fd2807SJeff Garzik 	pci_set_master(pdev);
6354553c4aa6STejun Heo 	return 0;
6355c6fd2807SJeff Garzik }
6356c6fd2807SJeff Garzik 
6357c6fd2807SJeff Garzik int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
6358c6fd2807SJeff Garzik {
6359cca3974eSJeff Garzik 	struct ata_host *host = dev_get_drvdata(&pdev->dev);
6360c6fd2807SJeff Garzik 	int rc = 0;
6361c6fd2807SJeff Garzik 
6362cca3974eSJeff Garzik 	rc = ata_host_suspend(host, mesg);
6363c6fd2807SJeff Garzik 	if (rc)
6364c6fd2807SJeff Garzik 		return rc;
6365c6fd2807SJeff Garzik 
6366c6fd2807SJeff Garzik 	ata_pci_device_do_suspend(pdev, mesg);
6367c6fd2807SJeff Garzik 
6368c6fd2807SJeff Garzik 	return 0;
6369c6fd2807SJeff Garzik }
6370c6fd2807SJeff Garzik 
6371c6fd2807SJeff Garzik int ata_pci_device_resume(struct pci_dev *pdev)
6372c6fd2807SJeff Garzik {
6373cca3974eSJeff Garzik 	struct ata_host *host = dev_get_drvdata(&pdev->dev);
6374553c4aa6STejun Heo 	int rc;
6375c6fd2807SJeff Garzik 
6376553c4aa6STejun Heo 	rc = ata_pci_device_do_resume(pdev);
6377553c4aa6STejun Heo 	if (rc == 0)
6378cca3974eSJeff Garzik 		ata_host_resume(host);
6379553c4aa6STejun Heo 	return rc;
6380c6fd2807SJeff Garzik }
63816ffa01d8STejun Heo #endif /* CONFIG_PM */
63826ffa01d8STejun Heo 
6383c6fd2807SJeff Garzik #endif /* CONFIG_PCI */
6384c6fd2807SJeff Garzik 
638533267325STejun Heo static int __init ata_parse_force_one(char **cur,
638633267325STejun Heo 				      struct ata_force_ent *force_ent,
638733267325STejun Heo 				      const char **reason)
638833267325STejun Heo {
638933267325STejun Heo 	/* FIXME: Currently, there's no way to tag init const data and
639033267325STejun Heo 	 * using __initdata causes build failure on some versions of
639133267325STejun Heo 	 * gcc.  Once __initdataconst is implemented, add const to the
639233267325STejun Heo 	 * following structure.
639333267325STejun Heo 	 */
639433267325STejun Heo 	static struct ata_force_param force_tbl[] __initdata = {
639533267325STejun Heo 		{ "40c",	.cbl		= ATA_CBL_PATA40 },
639633267325STejun Heo 		{ "80c",	.cbl		= ATA_CBL_PATA80 },
639733267325STejun Heo 		{ "short40c",	.cbl		= ATA_CBL_PATA40_SHORT },
639833267325STejun Heo 		{ "unk",	.cbl		= ATA_CBL_PATA_UNK },
639933267325STejun Heo 		{ "ign",	.cbl		= ATA_CBL_PATA_IGN },
640033267325STejun Heo 		{ "sata",	.cbl		= ATA_CBL_SATA },
640133267325STejun Heo 		{ "1.5Gbps",	.spd_limit	= 1 },
640233267325STejun Heo 		{ "3.0Gbps",	.spd_limit	= 2 },
640333267325STejun Heo 		{ "noncq",	.horkage_on	= ATA_HORKAGE_NONCQ },
640433267325STejun Heo 		{ "ncq",	.horkage_off	= ATA_HORKAGE_NONCQ },
640543c9c591STejun Heo 		{ "dump_id",	.horkage_on	= ATA_HORKAGE_DUMP_ID },
640633267325STejun Heo 		{ "pio0",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 0) },
640733267325STejun Heo 		{ "pio1",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 1) },
640833267325STejun Heo 		{ "pio2",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 2) },
640933267325STejun Heo 		{ "pio3",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 3) },
641033267325STejun Heo 		{ "pio4",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 4) },
641133267325STejun Heo 		{ "pio5",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 5) },
641233267325STejun Heo 		{ "pio6",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 6) },
641333267325STejun Heo 		{ "mwdma0",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 0) },
641433267325STejun Heo 		{ "mwdma1",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 1) },
641533267325STejun Heo 		{ "mwdma2",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 2) },
641633267325STejun Heo 		{ "mwdma3",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 3) },
641733267325STejun Heo 		{ "mwdma4",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 4) },
641833267325STejun Heo 		{ "udma0",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
641933267325STejun Heo 		{ "udma16",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
642033267325STejun Heo 		{ "udma/16",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
642133267325STejun Heo 		{ "udma1",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
642233267325STejun Heo 		{ "udma25",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
642333267325STejun Heo 		{ "udma/25",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
642433267325STejun Heo 		{ "udma2",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
642533267325STejun Heo 		{ "udma33",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
642633267325STejun Heo 		{ "udma/33",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
642733267325STejun Heo 		{ "udma3",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
642833267325STejun Heo 		{ "udma44",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
642933267325STejun Heo 		{ "udma/44",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
643033267325STejun Heo 		{ "udma4",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
643133267325STejun Heo 		{ "udma66",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
643233267325STejun Heo 		{ "udma/66",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
643333267325STejun Heo 		{ "udma5",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
643433267325STejun Heo 		{ "udma100",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
643533267325STejun Heo 		{ "udma/100",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
643633267325STejun Heo 		{ "udma6",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
643733267325STejun Heo 		{ "udma133",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
643833267325STejun Heo 		{ "udma/133",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
643933267325STejun Heo 		{ "udma7",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 7) },
644005944bdfSTejun Heo 		{ "nohrst",	.lflags		= ATA_LFLAG_NO_HRST },
644105944bdfSTejun Heo 		{ "nosrst",	.lflags		= ATA_LFLAG_NO_SRST },
644205944bdfSTejun Heo 		{ "norst",	.lflags		= ATA_LFLAG_NO_HRST | ATA_LFLAG_NO_SRST },
6443ca6d43b0SDan Williams 		{ "rstonce",	.lflags		= ATA_LFLAG_RST_ONCE },
644433267325STejun Heo 	};
644533267325STejun Heo 	char *start = *cur, *p = *cur;
644633267325STejun Heo 	char *id, *val, *endp;
644733267325STejun Heo 	const struct ata_force_param *match_fp = NULL;
644833267325STejun Heo 	int nr_matches = 0, i;
644933267325STejun Heo 
645033267325STejun Heo 	/* find where this param ends and update *cur */
645133267325STejun Heo 	while (*p != '\0' && *p != ',')
645233267325STejun Heo 		p++;
645333267325STejun Heo 
645433267325STejun Heo 	if (*p == '\0')
645533267325STejun Heo 		*cur = p;
645633267325STejun Heo 	else
645733267325STejun Heo 		*cur = p + 1;
645833267325STejun Heo 
645933267325STejun Heo 	*p = '\0';
646033267325STejun Heo 
646133267325STejun Heo 	/* parse */
646233267325STejun Heo 	p = strchr(start, ':');
646333267325STejun Heo 	if (!p) {
646433267325STejun Heo 		val = strstrip(start);
646533267325STejun Heo 		goto parse_val;
646633267325STejun Heo 	}
646733267325STejun Heo 	*p = '\0';
646833267325STejun Heo 
646933267325STejun Heo 	id = strstrip(start);
647033267325STejun Heo 	val = strstrip(p + 1);
647133267325STejun Heo 
647233267325STejun Heo 	/* parse id */
647333267325STejun Heo 	p = strchr(id, '.');
647433267325STejun Heo 	if (p) {
647533267325STejun Heo 		*p++ = '\0';
647633267325STejun Heo 		force_ent->device = simple_strtoul(p, &endp, 10);
647733267325STejun Heo 		if (p == endp || *endp != '\0') {
647833267325STejun Heo 			*reason = "invalid device";
647933267325STejun Heo 			return -EINVAL;
648033267325STejun Heo 		}
648133267325STejun Heo 	}
648233267325STejun Heo 
648333267325STejun Heo 	force_ent->port = simple_strtoul(id, &endp, 10);
648433267325STejun Heo 	if (p == endp || *endp != '\0') {
648533267325STejun Heo 		*reason = "invalid port/link";
648633267325STejun Heo 		return -EINVAL;
648733267325STejun Heo 	}
648833267325STejun Heo 
648933267325STejun Heo  parse_val:
649033267325STejun Heo 	/* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */
649133267325STejun Heo 	for (i = 0; i < ARRAY_SIZE(force_tbl); i++) {
649233267325STejun Heo 		const struct ata_force_param *fp = &force_tbl[i];
649333267325STejun Heo 
649433267325STejun Heo 		if (strncasecmp(val, fp->name, strlen(val)))
649533267325STejun Heo 			continue;
649633267325STejun Heo 
649733267325STejun Heo 		nr_matches++;
649833267325STejun Heo 		match_fp = fp;
649933267325STejun Heo 
650033267325STejun Heo 		if (strcasecmp(val, fp->name) == 0) {
650133267325STejun Heo 			nr_matches = 1;
650233267325STejun Heo 			break;
650333267325STejun Heo 		}
650433267325STejun Heo 	}
650533267325STejun Heo 
650633267325STejun Heo 	if (!nr_matches) {
650733267325STejun Heo 		*reason = "unknown value";
650833267325STejun Heo 		return -EINVAL;
650933267325STejun Heo 	}
651033267325STejun Heo 	if (nr_matches > 1) {
651133267325STejun Heo 		*reason = "ambigious value";
651233267325STejun Heo 		return -EINVAL;
651333267325STejun Heo 	}
651433267325STejun Heo 
651533267325STejun Heo 	force_ent->param = *match_fp;
651633267325STejun Heo 
651733267325STejun Heo 	return 0;
651833267325STejun Heo }
651933267325STejun Heo 
652033267325STejun Heo static void __init ata_parse_force_param(void)
652133267325STejun Heo {
652233267325STejun Heo 	int idx = 0, size = 1;
652333267325STejun Heo 	int last_port = -1, last_device = -1;
652433267325STejun Heo 	char *p, *cur, *next;
652533267325STejun Heo 
652633267325STejun Heo 	/* calculate maximum number of params and allocate force_tbl */
652733267325STejun Heo 	for (p = ata_force_param_buf; *p; p++)
652833267325STejun Heo 		if (*p == ',')
652933267325STejun Heo 			size++;
653033267325STejun Heo 
653133267325STejun Heo 	ata_force_tbl = kzalloc(sizeof(ata_force_tbl[0]) * size, GFP_KERNEL);
653233267325STejun Heo 	if (!ata_force_tbl) {
653333267325STejun Heo 		printk(KERN_WARNING "ata: failed to extend force table, "
653433267325STejun Heo 		       "libata.force ignored\n");
653533267325STejun Heo 		return;
653633267325STejun Heo 	}
653733267325STejun Heo 
653833267325STejun Heo 	/* parse and populate the table */
653933267325STejun Heo 	for (cur = ata_force_param_buf; *cur != '\0'; cur = next) {
654033267325STejun Heo 		const char *reason = "";
654133267325STejun Heo 		struct ata_force_ent te = { .port = -1, .device = -1 };
654233267325STejun Heo 
654333267325STejun Heo 		next = cur;
654433267325STejun Heo 		if (ata_parse_force_one(&next, &te, &reason)) {
654533267325STejun Heo 			printk(KERN_WARNING "ata: failed to parse force "
654633267325STejun Heo 			       "parameter \"%s\" (%s)\n",
654733267325STejun Heo 			       cur, reason);
654833267325STejun Heo 			continue;
654933267325STejun Heo 		}
655033267325STejun Heo 
655133267325STejun Heo 		if (te.port == -1) {
655233267325STejun Heo 			te.port = last_port;
655333267325STejun Heo 			te.device = last_device;
655433267325STejun Heo 		}
655533267325STejun Heo 
655633267325STejun Heo 		ata_force_tbl[idx++] = te;
655733267325STejun Heo 
655833267325STejun Heo 		last_port = te.port;
655933267325STejun Heo 		last_device = te.device;
656033267325STejun Heo 	}
656133267325STejun Heo 
656233267325STejun Heo 	ata_force_tbl_size = idx;
656333267325STejun Heo }
6564c6fd2807SJeff Garzik 
6565c6fd2807SJeff Garzik static int __init ata_init(void)
6566c6fd2807SJeff Garzik {
6567d9027470SGwendal Grignou 	int rc;
6568270390e1STejun Heo 
656933267325STejun Heo 	ata_parse_force_param();
657033267325STejun Heo 
65716b66d958SMatthew Garrett 	ata_acpi_register();
65726b66d958SMatthew Garrett 
6573270390e1STejun Heo 	rc = ata_sff_init();
6574ad72cf98STejun Heo 	if (rc) {
6575ad72cf98STejun Heo 		kfree(ata_force_tbl);
6576ad72cf98STejun Heo 		return rc;
6577ad72cf98STejun Heo 	}
6578c6fd2807SJeff Garzik 
6579d9027470SGwendal Grignou 	libata_transport_init();
6580d9027470SGwendal Grignou 	ata_scsi_transport_template = ata_attach_transport();
6581d9027470SGwendal Grignou 	if (!ata_scsi_transport_template) {
6582d9027470SGwendal Grignou 		ata_sff_exit();
6583d9027470SGwendal Grignou 		rc = -ENOMEM;
6584d9027470SGwendal Grignou 		goto err_out;
6585d9027470SGwendal Grignou 	}
6586d9027470SGwendal Grignou 
6587c6fd2807SJeff Garzik 	printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
6588c6fd2807SJeff Garzik 	return 0;
6589d9027470SGwendal Grignou 
6590d9027470SGwendal Grignou err_out:
6591d9027470SGwendal Grignou 	return rc;
6592c6fd2807SJeff Garzik }
6593c6fd2807SJeff Garzik 
6594c6fd2807SJeff Garzik static void __exit ata_exit(void)
6595c6fd2807SJeff Garzik {
6596d9027470SGwendal Grignou 	ata_release_transport(ata_scsi_transport_template);
6597d9027470SGwendal Grignou 	libata_transport_exit();
6598270390e1STejun Heo 	ata_sff_exit();
65996b66d958SMatthew Garrett 	ata_acpi_unregister();
660033267325STejun Heo 	kfree(ata_force_tbl);
6601c6fd2807SJeff Garzik }
6602c6fd2807SJeff Garzik 
6603a4625085SBrian King subsys_initcall(ata_init);
6604c6fd2807SJeff Garzik module_exit(ata_exit);
6605c6fd2807SJeff Garzik 
66069990b6f3SAkinobu Mita static DEFINE_RATELIMIT_STATE(ratelimit, HZ / 5, 1);
6607c6fd2807SJeff Garzik 
6608c6fd2807SJeff Garzik int ata_ratelimit(void)
6609c6fd2807SJeff Garzik {
66109990b6f3SAkinobu Mita 	return __ratelimit(&ratelimit);
6611c6fd2807SJeff Garzik }
6612c6fd2807SJeff Garzik 
6613c0c362b6STejun Heo /**
6614c0c362b6STejun Heo  *	ata_msleep - ATA EH owner aware msleep
6615c0c362b6STejun Heo  *	@ap: ATA port to attribute the sleep to
6616c0c362b6STejun Heo  *	@msecs: duration to sleep in milliseconds
6617c0c362b6STejun Heo  *
6618c0c362b6STejun Heo  *	Sleeps @msecs.  If the current task is owner of @ap's EH, the
6619c0c362b6STejun Heo  *	ownership is released before going to sleep and reacquired
6620c0c362b6STejun Heo  *	after the sleep is complete.  IOW, other ports sharing the
6621c0c362b6STejun Heo  *	@ap->host will be allowed to own the EH while this task is
6622c0c362b6STejun Heo  *	sleeping.
6623c0c362b6STejun Heo  *
6624c0c362b6STejun Heo  *	LOCKING:
6625c0c362b6STejun Heo  *	Might sleep.
6626c0c362b6STejun Heo  */
662797750cebSTejun Heo void ata_msleep(struct ata_port *ap, unsigned int msecs)
662897750cebSTejun Heo {
6629c0c362b6STejun Heo 	bool owns_eh = ap && ap->host->eh_owner == current;
6630c0c362b6STejun Heo 
6631c0c362b6STejun Heo 	if (owns_eh)
6632c0c362b6STejun Heo 		ata_eh_release(ap);
6633c0c362b6STejun Heo 
663497750cebSTejun Heo 	msleep(msecs);
6635c0c362b6STejun Heo 
6636c0c362b6STejun Heo 	if (owns_eh)
6637c0c362b6STejun Heo 		ata_eh_acquire(ap);
663897750cebSTejun Heo }
663997750cebSTejun Heo 
6640c6fd2807SJeff Garzik /**
6641c6fd2807SJeff Garzik  *	ata_wait_register - wait until register value changes
664297750cebSTejun Heo  *	@ap: ATA port to wait register for, can be NULL
6643c6fd2807SJeff Garzik  *	@reg: IO-mapped register
6644c6fd2807SJeff Garzik  *	@mask: Mask to apply to read register value
6645c6fd2807SJeff Garzik  *	@val: Wait condition
6646341c2c95STejun Heo  *	@interval: polling interval in milliseconds
6647341c2c95STejun Heo  *	@timeout: timeout in milliseconds
6648c6fd2807SJeff Garzik  *
6649c6fd2807SJeff Garzik  *	Waiting for some bits of register to change is a common
6650c6fd2807SJeff Garzik  *	operation for ATA controllers.  This function reads 32bit LE
6651c6fd2807SJeff Garzik  *	IO-mapped register @reg and tests for the following condition.
6652c6fd2807SJeff Garzik  *
6653c6fd2807SJeff Garzik  *	(*@reg & mask) != val
6654c6fd2807SJeff Garzik  *
6655c6fd2807SJeff Garzik  *	If the condition is met, it returns; otherwise, the process is
6656c6fd2807SJeff Garzik  *	repeated after @interval_msec until timeout.
6657c6fd2807SJeff Garzik  *
6658c6fd2807SJeff Garzik  *	LOCKING:
6659c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
6660c6fd2807SJeff Garzik  *
6661c6fd2807SJeff Garzik  *	RETURNS:
6662c6fd2807SJeff Garzik  *	The final register value.
6663c6fd2807SJeff Garzik  */
666497750cebSTejun Heo u32 ata_wait_register(struct ata_port *ap, void __iomem *reg, u32 mask, u32 val,
6665341c2c95STejun Heo 		      unsigned long interval, unsigned long timeout)
6666c6fd2807SJeff Garzik {
6667341c2c95STejun Heo 	unsigned long deadline;
6668c6fd2807SJeff Garzik 	u32 tmp;
6669c6fd2807SJeff Garzik 
6670c6fd2807SJeff Garzik 	tmp = ioread32(reg);
6671c6fd2807SJeff Garzik 
6672c6fd2807SJeff Garzik 	/* Calculate timeout _after_ the first read to make sure
6673c6fd2807SJeff Garzik 	 * preceding writes reach the controller before starting to
6674c6fd2807SJeff Garzik 	 * eat away the timeout.
6675c6fd2807SJeff Garzik 	 */
6676341c2c95STejun Heo 	deadline = ata_deadline(jiffies, timeout);
6677c6fd2807SJeff Garzik 
6678341c2c95STejun Heo 	while ((tmp & mask) == val && time_before(jiffies, deadline)) {
667997750cebSTejun Heo 		ata_msleep(ap, interval);
6680c6fd2807SJeff Garzik 		tmp = ioread32(reg);
6681c6fd2807SJeff Garzik 	}
6682c6fd2807SJeff Garzik 
6683c6fd2807SJeff Garzik 	return tmp;
6684c6fd2807SJeff Garzik }
6685c6fd2807SJeff Garzik 
6686c6fd2807SJeff Garzik /*
6687c6fd2807SJeff Garzik  * Dummy port_ops
6688c6fd2807SJeff Garzik  */
6689c6fd2807SJeff Garzik static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
6690c6fd2807SJeff Garzik {
6691c6fd2807SJeff Garzik 	return AC_ERR_SYSTEM;
6692c6fd2807SJeff Garzik }
6693c6fd2807SJeff Garzik 
6694182d7bbaSTejun Heo static void ata_dummy_error_handler(struct ata_port *ap)
6695182d7bbaSTejun Heo {
6696182d7bbaSTejun Heo 	/* truly dummy */
6697182d7bbaSTejun Heo }
6698182d7bbaSTejun Heo 
6699029cfd6bSTejun Heo struct ata_port_operations ata_dummy_port_ops = {
6700c6fd2807SJeff Garzik 	.qc_prep		= ata_noop_qc_prep,
6701c6fd2807SJeff Garzik 	.qc_issue		= ata_dummy_qc_issue,
6702182d7bbaSTejun Heo 	.error_handler		= ata_dummy_error_handler,
6703e4a9c373SDan Williams 	.sched_eh		= ata_std_sched_eh,
6704e4a9c373SDan Williams 	.end_eh			= ata_std_end_eh,
6705c6fd2807SJeff Garzik };
6706c6fd2807SJeff Garzik 
670721b0ad4fSTejun Heo const struct ata_port_info ata_dummy_port_info = {
670821b0ad4fSTejun Heo 	.port_ops		= &ata_dummy_port_ops,
670921b0ad4fSTejun Heo };
671021b0ad4fSTejun Heo 
6711c6fd2807SJeff Garzik /*
6712a9a79dfeSJoe Perches  * Utility print functions
6713a9a79dfeSJoe Perches  */
6714a9a79dfeSJoe Perches int ata_port_printk(const struct ata_port *ap, const char *level,
6715a9a79dfeSJoe Perches 		    const char *fmt, ...)
6716a9a79dfeSJoe Perches {
6717a9a79dfeSJoe Perches 	struct va_format vaf;
6718a9a79dfeSJoe Perches 	va_list args;
6719a9a79dfeSJoe Perches 	int r;
6720a9a79dfeSJoe Perches 
6721a9a79dfeSJoe Perches 	va_start(args, fmt);
6722a9a79dfeSJoe Perches 
6723a9a79dfeSJoe Perches 	vaf.fmt = fmt;
6724a9a79dfeSJoe Perches 	vaf.va = &args;
6725a9a79dfeSJoe Perches 
6726a9a79dfeSJoe Perches 	r = printk("%sata%u: %pV", level, ap->print_id, &vaf);
6727a9a79dfeSJoe Perches 
6728a9a79dfeSJoe Perches 	va_end(args);
6729a9a79dfeSJoe Perches 
6730a9a79dfeSJoe Perches 	return r;
6731a9a79dfeSJoe Perches }
6732a9a79dfeSJoe Perches EXPORT_SYMBOL(ata_port_printk);
6733a9a79dfeSJoe Perches 
6734a9a79dfeSJoe Perches int ata_link_printk(const struct ata_link *link, const char *level,
6735a9a79dfeSJoe Perches 		    const char *fmt, ...)
6736a9a79dfeSJoe Perches {
6737a9a79dfeSJoe Perches 	struct va_format vaf;
6738a9a79dfeSJoe Perches 	va_list args;
6739a9a79dfeSJoe Perches 	int r;
6740a9a79dfeSJoe Perches 
6741a9a79dfeSJoe Perches 	va_start(args, fmt);
6742a9a79dfeSJoe Perches 
6743a9a79dfeSJoe Perches 	vaf.fmt = fmt;
6744a9a79dfeSJoe Perches 	vaf.va = &args;
6745a9a79dfeSJoe Perches 
6746a9a79dfeSJoe Perches 	if (sata_pmp_attached(link->ap) || link->ap->slave_link)
6747a9a79dfeSJoe Perches 		r = printk("%sata%u.%02u: %pV",
6748a9a79dfeSJoe Perches 			   level, link->ap->print_id, link->pmp, &vaf);
6749a9a79dfeSJoe Perches 	else
6750a9a79dfeSJoe Perches 		r = printk("%sata%u: %pV",
6751a9a79dfeSJoe Perches 			   level, link->ap->print_id, &vaf);
6752a9a79dfeSJoe Perches 
6753a9a79dfeSJoe Perches 	va_end(args);
6754a9a79dfeSJoe Perches 
6755a9a79dfeSJoe Perches 	return r;
6756a9a79dfeSJoe Perches }
6757a9a79dfeSJoe Perches EXPORT_SYMBOL(ata_link_printk);
6758a9a79dfeSJoe Perches 
6759a9a79dfeSJoe Perches int ata_dev_printk(const struct ata_device *dev, const char *level,
6760a9a79dfeSJoe Perches 		    const char *fmt, ...)
6761a9a79dfeSJoe Perches {
6762a9a79dfeSJoe Perches 	struct va_format vaf;
6763a9a79dfeSJoe Perches 	va_list args;
6764a9a79dfeSJoe Perches 	int r;
6765a9a79dfeSJoe Perches 
6766a9a79dfeSJoe Perches 	va_start(args, fmt);
6767a9a79dfeSJoe Perches 
6768a9a79dfeSJoe Perches 	vaf.fmt = fmt;
6769a9a79dfeSJoe Perches 	vaf.va = &args;
6770a9a79dfeSJoe Perches 
6771a9a79dfeSJoe Perches 	r = printk("%sata%u.%02u: %pV",
6772a9a79dfeSJoe Perches 		   level, dev->link->ap->print_id, dev->link->pmp + dev->devno,
6773a9a79dfeSJoe Perches 		   &vaf);
6774a9a79dfeSJoe Perches 
6775a9a79dfeSJoe Perches 	va_end(args);
6776a9a79dfeSJoe Perches 
6777a9a79dfeSJoe Perches 	return r;
6778a9a79dfeSJoe Perches }
6779a9a79dfeSJoe Perches EXPORT_SYMBOL(ata_dev_printk);
6780a9a79dfeSJoe Perches 
678106296a1eSJoe Perches void ata_print_version(const struct device *dev, const char *version)
678206296a1eSJoe Perches {
678306296a1eSJoe Perches 	dev_printk(KERN_DEBUG, dev, "version %s\n", version);
678406296a1eSJoe Perches }
678506296a1eSJoe Perches EXPORT_SYMBOL(ata_print_version);
678606296a1eSJoe Perches 
6787a9a79dfeSJoe Perches /*
6788c6fd2807SJeff Garzik  * libata is essentially a library of internal helper functions for
6789c6fd2807SJeff Garzik  * low-level ATA host controller drivers.  As such, the API/ABI is
6790c6fd2807SJeff Garzik  * likely to change as new drivers are added and updated.
6791c6fd2807SJeff Garzik  * Do not depend on ABI/API stability.
6792c6fd2807SJeff Garzik  */
6793c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
6794c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
6795c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_deb_timing_long);
6796029cfd6bSTejun Heo EXPORT_SYMBOL_GPL(ata_base_port_ops);
6797029cfd6bSTejun Heo EXPORT_SYMBOL_GPL(sata_port_ops);
6798c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
679921b0ad4fSTejun Heo EXPORT_SYMBOL_GPL(ata_dummy_port_info);
68001eca4365STejun Heo EXPORT_SYMBOL_GPL(ata_link_next);
68011eca4365STejun Heo EXPORT_SYMBOL_GPL(ata_dev_next);
6802c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_std_bios_param);
6803d8d9129eSTejun Heo EXPORT_SYMBOL_GPL(ata_scsi_unlock_native_capacity);
6804cca3974eSJeff Garzik EXPORT_SYMBOL_GPL(ata_host_init);
6805f3187195STejun Heo EXPORT_SYMBOL_GPL(ata_host_alloc);
6806f5cda257STejun Heo EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
6807b1c72916STejun Heo EXPORT_SYMBOL_GPL(ata_slave_link_init);
6808ecef7253STejun Heo EXPORT_SYMBOL_GPL(ata_host_start);
6809f3187195STejun Heo EXPORT_SYMBOL_GPL(ata_host_register);
6810f5cda257STejun Heo EXPORT_SYMBOL_GPL(ata_host_activate);
68110529c159STejun Heo EXPORT_SYMBOL_GPL(ata_host_detach);
6812c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_sg_init);
6813c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_qc_complete);
6814c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
6815436d34b3STejun Heo EXPORT_SYMBOL_GPL(atapi_cmd_type);
6816c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_tf_to_fis);
6817c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_tf_from_fis);
68186357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_pack_xfermask);
68196357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_unpack_xfermask);
68206357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
68216357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
68226357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
68236357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_mode_string);
68246357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_id_xfermask);
682504351821SAlan EXPORT_SYMBOL_GPL(ata_do_set_mode);
682631cc23b3STejun Heo EXPORT_SYMBOL_GPL(ata_std_qc_defer);
6827c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
682810305f0fSAlan EXPORT_SYMBOL_GPL(ata_dev_disable);
6829c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_set_spd);
6830aa2731adSTejun Heo EXPORT_SYMBOL_GPL(ata_wait_after_reset);
6831936fd732STejun Heo EXPORT_SYMBOL_GPL(sata_link_debounce);
6832936fd732STejun Heo EXPORT_SYMBOL_GPL(sata_link_resume);
68331152b261STejun Heo EXPORT_SYMBOL_GPL(sata_link_scr_lpm);
68340aa1113dSTejun Heo EXPORT_SYMBOL_GPL(ata_std_prereset);
6835cc0680a5STejun Heo EXPORT_SYMBOL_GPL(sata_link_hardreset);
683657c9efdfSTejun Heo EXPORT_SYMBOL_GPL(sata_std_hardreset);
6837203c75b8STejun Heo EXPORT_SYMBOL_GPL(ata_std_postreset);
6838c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_dev_classify);
6839c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_dev_pair);
6840c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_ratelimit);
684197750cebSTejun Heo EXPORT_SYMBOL_GPL(ata_msleep);
6842c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_wait_register);
6843c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
6844c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
6845c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
6846c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
6847f6e67035SDan Williams EXPORT_SYMBOL_GPL(__ata_change_queue_depth);
6848c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_valid);
6849c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_read);
6850c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_write);
6851c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_write_flush);
6852936fd732STejun Heo EXPORT_SYMBOL_GPL(ata_link_online);
6853936fd732STejun Heo EXPORT_SYMBOL_GPL(ata_link_offline);
68546ffa01d8STejun Heo #ifdef CONFIG_PM
6855cca3974eSJeff Garzik EXPORT_SYMBOL_GPL(ata_host_suspend);
6856cca3974eSJeff Garzik EXPORT_SYMBOL_GPL(ata_host_resume);
68576ffa01d8STejun Heo #endif /* CONFIG_PM */
6858c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_id_string);
6859c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_id_c_string);
6860963e4975SAlan Cox EXPORT_SYMBOL_GPL(ata_do_dev_read_id);
6861c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_simulate);
6862c6fd2807SJeff Garzik 
6863c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
68646357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_timing_find_mode);
6865c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_timing_compute);
6866c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_timing_merge);
6867a0f79b92STejun Heo EXPORT_SYMBOL_GPL(ata_timing_cycle2mode);
6868c6fd2807SJeff Garzik 
6869c6fd2807SJeff Garzik #ifdef CONFIG_PCI
6870c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(pci_test_config_bits);
6871c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_remove_one);
68726ffa01d8STejun Heo #ifdef CONFIG_PM
6873c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
6874c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
6875c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
6876c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_resume);
68776ffa01d8STejun Heo #endif /* CONFIG_PM */
6878c6fd2807SJeff Garzik #endif /* CONFIG_PCI */
6879c6fd2807SJeff Garzik 
6880b64bbc39STejun Heo EXPORT_SYMBOL_GPL(__ata_ehi_push_desc);
6881b64bbc39STejun Heo EXPORT_SYMBOL_GPL(ata_ehi_push_desc);
6882b64bbc39STejun Heo EXPORT_SYMBOL_GPL(ata_ehi_clear_desc);
6883cbcdd875STejun Heo EXPORT_SYMBOL_GPL(ata_port_desc);
6884cbcdd875STejun Heo #ifdef CONFIG_PCI
6885cbcdd875STejun Heo EXPORT_SYMBOL_GPL(ata_port_pbar_desc);
6886cbcdd875STejun Heo #endif /* CONFIG_PCI */
6887c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
6888dbd82616STejun Heo EXPORT_SYMBOL_GPL(ata_link_abort);
6889c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_abort);
6890c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_freeze);
68917d77b247STejun Heo EXPORT_SYMBOL_GPL(sata_async_notification);
6892c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
6893c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
6894c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
6895c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
689610acf3b0SMark Lord EXPORT_SYMBOL_GPL(ata_eh_analyze_ncq_error);
6897c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_do_eh);
6898a1efdabaSTejun Heo EXPORT_SYMBOL_GPL(ata_std_error_handler);
6899be0d18dfSAlan Cox 
6900be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_40wire);
6901be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_80wire);
6902be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_unknown);
6903c88f90c3STejun Heo EXPORT_SYMBOL_GPL(ata_cable_ignore);
6904be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_sata);
6905