xref: /openbmc/linux/drivers/ata/libata-core.c (revision 9ca7cfa4)
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 
1389973a1c3SJeff Garzik int libata_fua = 0;
139c6fd2807SJeff Garzik module_param_named(fua, libata_fua, int, 0444);
1409973a1c3SJeff Garzik 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;
294570cd071eSTejun Heo 	return NULL;
2946c6fd2807SJeff Garzik }
2947c6fd2807SJeff Garzik 
2948c6fd2807SJeff Garzik int ata_timing_compute(struct ata_device *adev, unsigned short speed,
2949c6fd2807SJeff Garzik 		       struct ata_timing *t, int T, int UT)
2950c6fd2807SJeff Garzik {
29519e8808a9SBartlomiej Zolnierkiewicz 	const u16 *id = adev->id;
2952c6fd2807SJeff Garzik 	const struct ata_timing *s;
2953c6fd2807SJeff Garzik 	struct ata_timing p;
2954c6fd2807SJeff Garzik 
2955c6fd2807SJeff Garzik 	/*
2956c6fd2807SJeff Garzik 	 * Find the mode.
2957c6fd2807SJeff Garzik 	 */
2958c6fd2807SJeff Garzik 
2959c6fd2807SJeff Garzik 	if (!(s = ata_timing_find_mode(speed)))
2960c6fd2807SJeff Garzik 		return -EINVAL;
2961c6fd2807SJeff Garzik 
2962c6fd2807SJeff Garzik 	memcpy(t, s, sizeof(*s));
2963c6fd2807SJeff Garzik 
2964c6fd2807SJeff Garzik 	/*
2965c6fd2807SJeff Garzik 	 * If the drive is an EIDE drive, it can tell us it needs extended
2966c6fd2807SJeff Garzik 	 * PIO/MW_DMA cycle timing.
2967c6fd2807SJeff Garzik 	 */
2968c6fd2807SJeff Garzik 
29699e8808a9SBartlomiej Zolnierkiewicz 	if (id[ATA_ID_FIELD_VALID] & 2) {	/* EIDE drive */
2970c6fd2807SJeff Garzik 		memset(&p, 0, sizeof(p));
29719e8808a9SBartlomiej Zolnierkiewicz 
2972bff00256SBartlomiej Zolnierkiewicz 		if (speed >= XFER_PIO_0 && speed < XFER_SW_DMA_0) {
29739e8808a9SBartlomiej Zolnierkiewicz 			if (speed <= XFER_PIO_2)
29749e8808a9SBartlomiej Zolnierkiewicz 				p.cycle = p.cyc8b = id[ATA_ID_EIDE_PIO];
29759e8808a9SBartlomiej Zolnierkiewicz 			else if ((speed <= XFER_PIO_4) ||
29769e8808a9SBartlomiej Zolnierkiewicz 				 (speed == XFER_PIO_5 && !ata_id_is_cfa(id)))
29779e8808a9SBartlomiej Zolnierkiewicz 				p.cycle = p.cyc8b = id[ATA_ID_EIDE_PIO_IORDY];
29789e8808a9SBartlomiej Zolnierkiewicz 		} else if (speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2)
29799e8808a9SBartlomiej Zolnierkiewicz 			p.cycle = id[ATA_ID_EIDE_DMA_MIN];
29809e8808a9SBartlomiej Zolnierkiewicz 
2981c6fd2807SJeff Garzik 		ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
2982c6fd2807SJeff Garzik 	}
2983c6fd2807SJeff Garzik 
2984c6fd2807SJeff Garzik 	/*
2985c6fd2807SJeff Garzik 	 * Convert the timing to bus clock counts.
2986c6fd2807SJeff Garzik 	 */
2987c6fd2807SJeff Garzik 
2988c6fd2807SJeff Garzik 	ata_timing_quantize(t, t, T, UT);
2989c6fd2807SJeff Garzik 
2990c6fd2807SJeff Garzik 	/*
2991c6fd2807SJeff Garzik 	 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
2992c6fd2807SJeff Garzik 	 * S.M.A.R.T * and some other commands. We have to ensure that the
2993c6fd2807SJeff Garzik 	 * DMA cycle timing is slower/equal than the fastest PIO timing.
2994c6fd2807SJeff Garzik 	 */
2995c6fd2807SJeff Garzik 
2996fd3367afSAlan 	if (speed > XFER_PIO_6) {
2997c6fd2807SJeff Garzik 		ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
2998c6fd2807SJeff Garzik 		ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
2999c6fd2807SJeff Garzik 	}
3000c6fd2807SJeff Garzik 
3001c6fd2807SJeff Garzik 	/*
3002c6fd2807SJeff Garzik 	 * Lengthen active & recovery time so that cycle time is correct.
3003c6fd2807SJeff Garzik 	 */
3004c6fd2807SJeff Garzik 
3005c6fd2807SJeff Garzik 	if (t->act8b + t->rec8b < t->cyc8b) {
3006c6fd2807SJeff Garzik 		t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
3007c6fd2807SJeff Garzik 		t->rec8b = t->cyc8b - t->act8b;
3008c6fd2807SJeff Garzik 	}
3009c6fd2807SJeff Garzik 
3010c6fd2807SJeff Garzik 	if (t->active + t->recover < t->cycle) {
3011c6fd2807SJeff Garzik 		t->active += (t->cycle - (t->active + t->recover)) / 2;
3012c6fd2807SJeff Garzik 		t->recover = t->cycle - t->active;
3013c6fd2807SJeff Garzik 	}
30144f701d1eSAlan Cox 
30154f701d1eSAlan Cox 	/* In a few cases quantisation may produce enough errors to
30164f701d1eSAlan Cox 	   leave t->cycle too low for the sum of active and recovery
30174f701d1eSAlan Cox 	   if so we must correct this */
30184f701d1eSAlan Cox 	if (t->active + t->recover > t->cycle)
30194f701d1eSAlan Cox 		t->cycle = t->active + t->recover;
3020c6fd2807SJeff Garzik 
3021c6fd2807SJeff Garzik 	return 0;
3022c6fd2807SJeff Garzik }
3023c6fd2807SJeff Garzik 
3024c6fd2807SJeff Garzik /**
3025a0f79b92STejun Heo  *	ata_timing_cycle2mode - find xfer mode for the specified cycle duration
3026a0f79b92STejun Heo  *	@xfer_shift: ATA_SHIFT_* value for transfer type to examine.
3027a0f79b92STejun Heo  *	@cycle: cycle duration in ns
3028a0f79b92STejun Heo  *
3029a0f79b92STejun Heo  *	Return matching xfer mode for @cycle.  The returned mode is of
3030a0f79b92STejun Heo  *	the transfer type specified by @xfer_shift.  If @cycle is too
3031a0f79b92STejun Heo  *	slow for @xfer_shift, 0xff is returned.  If @cycle is faster
3032a0f79b92STejun Heo  *	than the fastest known mode, the fasted mode is returned.
3033a0f79b92STejun Heo  *
3034a0f79b92STejun Heo  *	LOCKING:
3035a0f79b92STejun Heo  *	None.
3036a0f79b92STejun Heo  *
3037a0f79b92STejun Heo  *	RETURNS:
3038a0f79b92STejun Heo  *	Matching xfer_mode, 0xff if no match found.
3039a0f79b92STejun Heo  */
3040a0f79b92STejun Heo u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
3041a0f79b92STejun Heo {
3042a0f79b92STejun Heo 	u8 base_mode = 0xff, last_mode = 0xff;
3043a0f79b92STejun Heo 	const struct ata_xfer_ent *ent;
3044a0f79b92STejun Heo 	const struct ata_timing *t;
3045a0f79b92STejun Heo 
3046a0f79b92STejun Heo 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
3047a0f79b92STejun Heo 		if (ent->shift == xfer_shift)
3048a0f79b92STejun Heo 			base_mode = ent->base;
3049a0f79b92STejun Heo 
3050a0f79b92STejun Heo 	for (t = ata_timing_find_mode(base_mode);
3051a0f79b92STejun Heo 	     t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
3052a0f79b92STejun Heo 		unsigned short this_cycle;
3053a0f79b92STejun Heo 
3054a0f79b92STejun Heo 		switch (xfer_shift) {
3055a0f79b92STejun Heo 		case ATA_SHIFT_PIO:
3056a0f79b92STejun Heo 		case ATA_SHIFT_MWDMA:
3057a0f79b92STejun Heo 			this_cycle = t->cycle;
3058a0f79b92STejun Heo 			break;
3059a0f79b92STejun Heo 		case ATA_SHIFT_UDMA:
3060a0f79b92STejun Heo 			this_cycle = t->udma;
3061a0f79b92STejun Heo 			break;
3062a0f79b92STejun Heo 		default:
3063a0f79b92STejun Heo 			return 0xff;
3064a0f79b92STejun Heo 		}
3065a0f79b92STejun Heo 
3066a0f79b92STejun Heo 		if (cycle > this_cycle)
3067a0f79b92STejun Heo 			break;
3068a0f79b92STejun Heo 
3069a0f79b92STejun Heo 		last_mode = t->mode;
3070a0f79b92STejun Heo 	}
3071a0f79b92STejun Heo 
3072a0f79b92STejun Heo 	return last_mode;
3073a0f79b92STejun Heo }
3074a0f79b92STejun Heo 
3075a0f79b92STejun Heo /**
3076c6fd2807SJeff Garzik  *	ata_down_xfermask_limit - adjust dev xfer masks downward
3077c6fd2807SJeff Garzik  *	@dev: Device to adjust xfer masks
3078458337dbSTejun Heo  *	@sel: ATA_DNXFER_* selector
3079c6fd2807SJeff Garzik  *
3080c6fd2807SJeff Garzik  *	Adjust xfer masks of @dev downward.  Note that this function
3081c6fd2807SJeff Garzik  *	does not apply the change.  Invoking ata_set_mode() afterwards
3082c6fd2807SJeff Garzik  *	will apply the limit.
3083c6fd2807SJeff Garzik  *
3084c6fd2807SJeff Garzik  *	LOCKING:
3085c6fd2807SJeff Garzik  *	Inherited from caller.
3086c6fd2807SJeff Garzik  *
3087c6fd2807SJeff Garzik  *	RETURNS:
3088c6fd2807SJeff Garzik  *	0 on success, negative errno on failure
3089c6fd2807SJeff Garzik  */
3090458337dbSTejun Heo int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
3091c6fd2807SJeff Garzik {
3092458337dbSTejun Heo 	char buf[32];
30937dc951aeSTejun Heo 	unsigned long orig_mask, xfer_mask;
30947dc951aeSTejun Heo 	unsigned long pio_mask, mwdma_mask, udma_mask;
3095458337dbSTejun Heo 	int quiet, highbit;
3096c6fd2807SJeff Garzik 
3097458337dbSTejun Heo 	quiet = !!(sel & ATA_DNXFER_QUIET);
3098458337dbSTejun Heo 	sel &= ~ATA_DNXFER_QUIET;
3099458337dbSTejun Heo 
3100458337dbSTejun Heo 	xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
3101458337dbSTejun Heo 						  dev->mwdma_mask,
3102c6fd2807SJeff Garzik 						  dev->udma_mask);
3103458337dbSTejun Heo 	ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
3104c6fd2807SJeff Garzik 
3105458337dbSTejun Heo 	switch (sel) {
3106458337dbSTejun Heo 	case ATA_DNXFER_PIO:
3107458337dbSTejun Heo 		highbit = fls(pio_mask) - 1;
3108458337dbSTejun Heo 		pio_mask &= ~(1 << highbit);
3109458337dbSTejun Heo 		break;
3110458337dbSTejun Heo 
3111458337dbSTejun Heo 	case ATA_DNXFER_DMA:
3112458337dbSTejun Heo 		if (udma_mask) {
3113458337dbSTejun Heo 			highbit = fls(udma_mask) - 1;
3114458337dbSTejun Heo 			udma_mask &= ~(1 << highbit);
3115458337dbSTejun Heo 			if (!udma_mask)
3116458337dbSTejun Heo 				return -ENOENT;
3117458337dbSTejun Heo 		} else if (mwdma_mask) {
3118458337dbSTejun Heo 			highbit = fls(mwdma_mask) - 1;
3119458337dbSTejun Heo 			mwdma_mask &= ~(1 << highbit);
3120458337dbSTejun Heo 			if (!mwdma_mask)
3121458337dbSTejun Heo 				return -ENOENT;
3122458337dbSTejun Heo 		}
3123458337dbSTejun Heo 		break;
3124458337dbSTejun Heo 
3125458337dbSTejun Heo 	case ATA_DNXFER_40C:
3126458337dbSTejun Heo 		udma_mask &= ATA_UDMA_MASK_40C;
3127458337dbSTejun Heo 		break;
3128458337dbSTejun Heo 
3129458337dbSTejun Heo 	case ATA_DNXFER_FORCE_PIO0:
3130458337dbSTejun Heo 		pio_mask &= 1;
3131458337dbSTejun Heo 	case ATA_DNXFER_FORCE_PIO:
3132458337dbSTejun Heo 		mwdma_mask = 0;
3133458337dbSTejun Heo 		udma_mask = 0;
3134458337dbSTejun Heo 		break;
3135458337dbSTejun Heo 
3136458337dbSTejun Heo 	default:
3137458337dbSTejun Heo 		BUG();
3138458337dbSTejun Heo 	}
3139458337dbSTejun Heo 
3140458337dbSTejun Heo 	xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
3141458337dbSTejun Heo 
3142458337dbSTejun Heo 	if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
3143458337dbSTejun Heo 		return -ENOENT;
3144458337dbSTejun Heo 
3145458337dbSTejun Heo 	if (!quiet) {
3146458337dbSTejun Heo 		if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
3147458337dbSTejun Heo 			snprintf(buf, sizeof(buf), "%s:%s",
3148458337dbSTejun Heo 				 ata_mode_string(xfer_mask),
3149458337dbSTejun Heo 				 ata_mode_string(xfer_mask & ATA_MASK_PIO));
3150458337dbSTejun Heo 		else
3151458337dbSTejun Heo 			snprintf(buf, sizeof(buf), "%s",
3152458337dbSTejun Heo 				 ata_mode_string(xfer_mask));
3153458337dbSTejun Heo 
3154a9a79dfeSJoe Perches 		ata_dev_warn(dev, "limiting speed to %s\n", buf);
3155458337dbSTejun Heo 	}
3156c6fd2807SJeff Garzik 
3157c6fd2807SJeff Garzik 	ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
3158c6fd2807SJeff Garzik 			    &dev->udma_mask);
3159c6fd2807SJeff Garzik 
3160c6fd2807SJeff Garzik 	return 0;
3161c6fd2807SJeff Garzik }
3162c6fd2807SJeff Garzik 
3163c6fd2807SJeff Garzik static int ata_dev_set_mode(struct ata_device *dev)
3164c6fd2807SJeff Garzik {
3165d0cb43b3STejun Heo 	struct ata_port *ap = dev->link->ap;
31669af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
3167d0cb43b3STejun Heo 	const bool nosetxfer = dev->horkage & ATA_HORKAGE_NOSETXFER;
31684055dee7STejun Heo 	const char *dev_err_whine = "";
31694055dee7STejun Heo 	int ign_dev_err = 0;
3170d0cb43b3STejun Heo 	unsigned int err_mask = 0;
3171c6fd2807SJeff Garzik 	int rc;
3172c6fd2807SJeff Garzik 
3173c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_PIO;
3174c6fd2807SJeff Garzik 	if (dev->xfer_shift == ATA_SHIFT_PIO)
3175c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_PIO;
3176c6fd2807SJeff Garzik 
3177d0cb43b3STejun Heo 	if (nosetxfer && ap->flags & ATA_FLAG_SATA && ata_id_is_sata(dev->id))
3178d0cb43b3STejun Heo 		dev_err_whine = " (SET_XFERMODE skipped)";
3179d0cb43b3STejun Heo 	else {
3180d0cb43b3STejun Heo 		if (nosetxfer)
3181a9a79dfeSJoe Perches 			ata_dev_warn(dev,
3182d0cb43b3STejun Heo 				     "NOSETXFER but PATA detected - can't "
3183d0cb43b3STejun Heo 				     "skip SETXFER, might malfunction\n");
3184c6fd2807SJeff Garzik 		err_mask = ata_dev_set_xfermode(dev);
3185d0cb43b3STejun Heo 	}
31862dcb407eSJeff Garzik 
31874055dee7STejun Heo 	if (err_mask & ~AC_ERR_DEV)
31884055dee7STejun Heo 		goto fail;
31892dcb407eSJeff Garzik 
31904055dee7STejun Heo 	/* revalidate */
3191baa1e78aSTejun Heo 	ehc->i.flags |= ATA_EHI_POST_SETMODE;
3192422c9daaSTejun Heo 	rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
3193baa1e78aSTejun Heo 	ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
3194c6fd2807SJeff Garzik 	if (rc)
3195c6fd2807SJeff Garzik 		return rc;
3196c6fd2807SJeff Garzik 
3197b93fda12SAlan Cox 	if (dev->xfer_shift == ATA_SHIFT_PIO) {
31984055dee7STejun Heo 		/* Old CFA may refuse this command, which is just fine */
3199b93fda12SAlan Cox 		if (ata_id_is_cfa(dev->id))
32004055dee7STejun Heo 			ign_dev_err = 1;
3201b93fda12SAlan Cox 		/* Catch several broken garbage emulations plus some pre
3202b93fda12SAlan Cox 		   ATA devices */
3203b93fda12SAlan Cox 		if (ata_id_major_version(dev->id) == 0 &&
32044055dee7STejun Heo 					dev->pio_mode <= XFER_PIO_2)
32054055dee7STejun Heo 			ign_dev_err = 1;
3206b93fda12SAlan Cox 		/* Some very old devices and some bad newer ones fail
3207b93fda12SAlan Cox 		   any kind of SET_XFERMODE request but support PIO0-2
3208b93fda12SAlan Cox 		   timings and no IORDY */
3209b93fda12SAlan Cox 		if (!ata_id_has_iordy(dev->id) && dev->pio_mode <= XFER_PIO_2)
3210b93fda12SAlan Cox 			ign_dev_err = 1;
3211b93fda12SAlan Cox 	}
32124055dee7STejun Heo 	/* Early MWDMA devices do DMA but don't allow DMA mode setting.
32134055dee7STejun Heo 	   Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
32144055dee7STejun Heo 	if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
32154055dee7STejun Heo 	    dev->dma_mode == XFER_MW_DMA_0 &&
32164055dee7STejun Heo 	    (dev->id[63] >> 8) & 1)
32174055dee7STejun Heo 		ign_dev_err = 1;
32184055dee7STejun Heo 
32194055dee7STejun Heo 	/* if the device is actually configured correctly, ignore dev err */
32204055dee7STejun Heo 	if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id)))
32214055dee7STejun Heo 		ign_dev_err = 1;
32224055dee7STejun Heo 
32234055dee7STejun Heo 	if (err_mask & AC_ERR_DEV) {
32244055dee7STejun Heo 		if (!ign_dev_err)
32254055dee7STejun Heo 			goto fail;
32264055dee7STejun Heo 		else
32274055dee7STejun Heo 			dev_err_whine = " (device error ignored)";
32284055dee7STejun Heo 	}
32294055dee7STejun Heo 
3230c6fd2807SJeff Garzik 	DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
3231c6fd2807SJeff Garzik 		dev->xfer_shift, (int)dev->xfer_mode);
3232c6fd2807SJeff Garzik 
3233a9a79dfeSJoe Perches 	ata_dev_info(dev, "configured for %s%s\n",
32344055dee7STejun Heo 		     ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)),
32354055dee7STejun Heo 		     dev_err_whine);
32364055dee7STejun Heo 
3237c6fd2807SJeff Garzik 	return 0;
32384055dee7STejun Heo 
32394055dee7STejun Heo  fail:
3240a9a79dfeSJoe Perches 	ata_dev_err(dev, "failed to set xfermode (err_mask=0x%x)\n", err_mask);
32414055dee7STejun Heo 	return -EIO;
3242c6fd2807SJeff Garzik }
3243c6fd2807SJeff Garzik 
3244c6fd2807SJeff Garzik /**
324504351821SAlan  *	ata_do_set_mode - Program timings and issue SET FEATURES - XFER
32460260731fSTejun Heo  *	@link: link on which timings will be programmed
32471967b7ffSJoe Perches  *	@r_failed_dev: out parameter for failed device
3248c6fd2807SJeff Garzik  *
324904351821SAlan  *	Standard implementation of the function used to tune and set
325004351821SAlan  *	ATA device disk transfer mode (PIO3, UDMA6, etc.).  If
325104351821SAlan  *	ata_dev_set_mode() fails, pointer to the failing device is
3252c6fd2807SJeff Garzik  *	returned in @r_failed_dev.
3253c6fd2807SJeff Garzik  *
3254c6fd2807SJeff Garzik  *	LOCKING:
3255c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
3256c6fd2807SJeff Garzik  *
3257c6fd2807SJeff Garzik  *	RETURNS:
3258c6fd2807SJeff Garzik  *	0 on success, negative errno otherwise
3259c6fd2807SJeff Garzik  */
326004351821SAlan 
32610260731fSTejun Heo int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
3262c6fd2807SJeff Garzik {
32630260731fSTejun Heo 	struct ata_port *ap = link->ap;
3264c6fd2807SJeff Garzik 	struct ata_device *dev;
3265f58229f8STejun Heo 	int rc = 0, used_dma = 0, found = 0;
3266c6fd2807SJeff Garzik 
3267c6fd2807SJeff Garzik 	/* step 1: calculate xfer_mask */
32681eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
32697dc951aeSTejun Heo 		unsigned long pio_mask, dma_mask;
3270b3a70601SAlan Cox 		unsigned int mode_mask;
3271c6fd2807SJeff Garzik 
3272b3a70601SAlan Cox 		mode_mask = ATA_DMA_MASK_ATA;
3273b3a70601SAlan Cox 		if (dev->class == ATA_DEV_ATAPI)
3274b3a70601SAlan Cox 			mode_mask = ATA_DMA_MASK_ATAPI;
3275b3a70601SAlan Cox 		else if (ata_id_is_cfa(dev->id))
3276b3a70601SAlan Cox 			mode_mask = ATA_DMA_MASK_CFA;
3277b3a70601SAlan Cox 
3278c6fd2807SJeff Garzik 		ata_dev_xfermask(dev);
327933267325STejun Heo 		ata_force_xfermask(dev);
3280c6fd2807SJeff Garzik 
3281c6fd2807SJeff Garzik 		pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
3282b3a70601SAlan Cox 
3283b3a70601SAlan Cox 		if (libata_dma_mask & mode_mask)
328480a9c430SSergei Shtylyov 			dma_mask = ata_pack_xfermask(0, dev->mwdma_mask,
328580a9c430SSergei Shtylyov 						     dev->udma_mask);
3286b3a70601SAlan Cox 		else
3287b3a70601SAlan Cox 			dma_mask = 0;
3288b3a70601SAlan Cox 
3289c6fd2807SJeff Garzik 		dev->pio_mode = ata_xfer_mask2mode(pio_mask);
3290c6fd2807SJeff Garzik 		dev->dma_mode = ata_xfer_mask2mode(dma_mask);
3291c6fd2807SJeff Garzik 
3292c6fd2807SJeff Garzik 		found = 1;
3293b15b3ebaSAlan Cox 		if (ata_dma_enabled(dev))
3294c6fd2807SJeff Garzik 			used_dma = 1;
3295c6fd2807SJeff Garzik 	}
3296c6fd2807SJeff Garzik 	if (!found)
3297c6fd2807SJeff Garzik 		goto out;
3298c6fd2807SJeff Garzik 
3299c6fd2807SJeff Garzik 	/* step 2: always set host PIO timings */
33001eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
330170cd071eSTejun Heo 		if (dev->pio_mode == 0xff) {
3302a9a79dfeSJoe Perches 			ata_dev_warn(dev, "no PIO support\n");
3303c6fd2807SJeff Garzik 			rc = -EINVAL;
3304c6fd2807SJeff Garzik 			goto out;
3305c6fd2807SJeff Garzik 		}
3306c6fd2807SJeff Garzik 
3307c6fd2807SJeff Garzik 		dev->xfer_mode = dev->pio_mode;
3308c6fd2807SJeff Garzik 		dev->xfer_shift = ATA_SHIFT_PIO;
3309c6fd2807SJeff Garzik 		if (ap->ops->set_piomode)
3310c6fd2807SJeff Garzik 			ap->ops->set_piomode(ap, dev);
3311c6fd2807SJeff Garzik 	}
3312c6fd2807SJeff Garzik 
3313c6fd2807SJeff Garzik 	/* step 3: set host DMA timings */
33141eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
33151eca4365STejun Heo 		if (!ata_dma_enabled(dev))
3316c6fd2807SJeff Garzik 			continue;
3317c6fd2807SJeff Garzik 
3318c6fd2807SJeff Garzik 		dev->xfer_mode = dev->dma_mode;
3319c6fd2807SJeff Garzik 		dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
3320c6fd2807SJeff Garzik 		if (ap->ops->set_dmamode)
3321c6fd2807SJeff Garzik 			ap->ops->set_dmamode(ap, dev);
3322c6fd2807SJeff Garzik 	}
3323c6fd2807SJeff Garzik 
3324c6fd2807SJeff Garzik 	/* step 4: update devices' xfer mode */
33251eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
3326c6fd2807SJeff Garzik 		rc = ata_dev_set_mode(dev);
3327c6fd2807SJeff Garzik 		if (rc)
3328c6fd2807SJeff Garzik 			goto out;
3329c6fd2807SJeff Garzik 	}
3330c6fd2807SJeff Garzik 
3331c6fd2807SJeff Garzik 	/* Record simplex status. If we selected DMA then the other
3332c6fd2807SJeff Garzik 	 * host channels are not permitted to do so.
3333c6fd2807SJeff Garzik 	 */
3334cca3974eSJeff Garzik 	if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
3335032af1ceSAlan 		ap->host->simplex_claimed = ap;
3336c6fd2807SJeff Garzik 
3337c6fd2807SJeff Garzik  out:
3338c6fd2807SJeff Garzik 	if (rc)
3339c6fd2807SJeff Garzik 		*r_failed_dev = dev;
3340c6fd2807SJeff Garzik 	return rc;
3341c6fd2807SJeff Garzik }
3342c6fd2807SJeff Garzik 
3343c6fd2807SJeff Garzik /**
3344aa2731adSTejun Heo  *	ata_wait_ready - wait for link to become ready
3345aa2731adSTejun Heo  *	@link: link to be waited on
3346aa2731adSTejun Heo  *	@deadline: deadline jiffies for the operation
3347aa2731adSTejun Heo  *	@check_ready: callback to check link readiness
3348aa2731adSTejun Heo  *
3349aa2731adSTejun Heo  *	Wait for @link to become ready.  @check_ready should return
3350aa2731adSTejun Heo  *	positive number if @link is ready, 0 if it isn't, -ENODEV if
3351aa2731adSTejun Heo  *	link doesn't seem to be occupied, other errno for other error
3352aa2731adSTejun Heo  *	conditions.
3353aa2731adSTejun Heo  *
3354aa2731adSTejun Heo  *	Transient -ENODEV conditions are allowed for
3355aa2731adSTejun Heo  *	ATA_TMOUT_FF_WAIT.
3356aa2731adSTejun Heo  *
3357aa2731adSTejun Heo  *	LOCKING:
3358aa2731adSTejun Heo  *	EH context.
3359aa2731adSTejun Heo  *
3360aa2731adSTejun Heo  *	RETURNS:
3361aa2731adSTejun Heo  *	0 if @linke is ready before @deadline; otherwise, -errno.
3362aa2731adSTejun Heo  */
3363aa2731adSTejun Heo int ata_wait_ready(struct ata_link *link, unsigned long deadline,
3364aa2731adSTejun Heo 		   int (*check_ready)(struct ata_link *link))
3365aa2731adSTejun Heo {
3366aa2731adSTejun Heo 	unsigned long start = jiffies;
3367b48d58f5STejun Heo 	unsigned long nodev_deadline;
3368aa2731adSTejun Heo 	int warned = 0;
3369aa2731adSTejun Heo 
3370b48d58f5STejun Heo 	/* choose which 0xff timeout to use, read comment in libata.h */
3371b48d58f5STejun Heo 	if (link->ap->host->flags & ATA_HOST_PARALLEL_SCAN)
3372b48d58f5STejun Heo 		nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT_LONG);
3373b48d58f5STejun Heo 	else
3374b48d58f5STejun Heo 		nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT);
3375b48d58f5STejun Heo 
3376b1c72916STejun Heo 	/* Slave readiness can't be tested separately from master.  On
3377b1c72916STejun Heo 	 * M/S emulation configuration, this function should be called
3378b1c72916STejun Heo 	 * only on the master and it will handle both master and slave.
3379b1c72916STejun Heo 	 */
3380b1c72916STejun Heo 	WARN_ON(link == link->ap->slave_link);
3381b1c72916STejun Heo 
3382aa2731adSTejun Heo 	if (time_after(nodev_deadline, deadline))
3383aa2731adSTejun Heo 		nodev_deadline = deadline;
3384aa2731adSTejun Heo 
3385aa2731adSTejun Heo 	while (1) {
3386aa2731adSTejun Heo 		unsigned long now = jiffies;
3387aa2731adSTejun Heo 		int ready, tmp;
3388aa2731adSTejun Heo 
3389aa2731adSTejun Heo 		ready = tmp = check_ready(link);
3390aa2731adSTejun Heo 		if (ready > 0)
3391aa2731adSTejun Heo 			return 0;
3392aa2731adSTejun Heo 
3393b48d58f5STejun Heo 		/*
3394b48d58f5STejun Heo 		 * -ENODEV could be transient.  Ignore -ENODEV if link
3395aa2731adSTejun Heo 		 * is online.  Also, some SATA devices take a long
3396b48d58f5STejun Heo 		 * time to clear 0xff after reset.  Wait for
3397b48d58f5STejun Heo 		 * ATA_TMOUT_FF_WAIT[_LONG] on -ENODEV if link isn't
3398b48d58f5STejun Heo 		 * offline.
3399aa2731adSTejun Heo 		 *
3400aa2731adSTejun Heo 		 * Note that some PATA controllers (pata_ali) explode
3401aa2731adSTejun Heo 		 * if status register is read more than once when
3402aa2731adSTejun Heo 		 * there's no device attached.
3403aa2731adSTejun Heo 		 */
3404aa2731adSTejun Heo 		if (ready == -ENODEV) {
3405aa2731adSTejun Heo 			if (ata_link_online(link))
3406aa2731adSTejun Heo 				ready = 0;
3407aa2731adSTejun Heo 			else if ((link->ap->flags & ATA_FLAG_SATA) &&
3408aa2731adSTejun Heo 				 !ata_link_offline(link) &&
3409aa2731adSTejun Heo 				 time_before(now, nodev_deadline))
3410aa2731adSTejun Heo 				ready = 0;
3411aa2731adSTejun Heo 		}
3412aa2731adSTejun Heo 
3413aa2731adSTejun Heo 		if (ready)
3414aa2731adSTejun Heo 			return ready;
3415aa2731adSTejun Heo 		if (time_after(now, deadline))
3416aa2731adSTejun Heo 			return -EBUSY;
3417aa2731adSTejun Heo 
3418aa2731adSTejun Heo 		if (!warned && time_after(now, start + 5 * HZ) &&
3419aa2731adSTejun Heo 		    (deadline - now > 3 * HZ)) {
3420a9a79dfeSJoe Perches 			ata_link_warn(link,
3421aa2731adSTejun Heo 				"link is slow to respond, please be patient "
3422aa2731adSTejun Heo 				"(ready=%d)\n", tmp);
3423aa2731adSTejun Heo 			warned = 1;
3424aa2731adSTejun Heo 		}
3425aa2731adSTejun Heo 
342697750cebSTejun Heo 		ata_msleep(link->ap, 50);
3427aa2731adSTejun Heo 	}
3428aa2731adSTejun Heo }
3429aa2731adSTejun Heo 
3430aa2731adSTejun Heo /**
3431aa2731adSTejun Heo  *	ata_wait_after_reset - wait for link to become ready after reset
3432aa2731adSTejun Heo  *	@link: link to be waited on
3433aa2731adSTejun Heo  *	@deadline: deadline jiffies for the operation
3434aa2731adSTejun Heo  *	@check_ready: callback to check link readiness
3435aa2731adSTejun Heo  *
3436aa2731adSTejun Heo  *	Wait for @link to become ready after reset.
3437aa2731adSTejun Heo  *
3438aa2731adSTejun Heo  *	LOCKING:
3439aa2731adSTejun Heo  *	EH context.
3440aa2731adSTejun Heo  *
3441aa2731adSTejun Heo  *	RETURNS:
3442aa2731adSTejun Heo  *	0 if @linke is ready before @deadline; otherwise, -errno.
3443aa2731adSTejun Heo  */
34442b4221bbSHarvey Harrison int ata_wait_after_reset(struct ata_link *link, unsigned long deadline,
3445aa2731adSTejun Heo 				int (*check_ready)(struct ata_link *link))
3446aa2731adSTejun Heo {
344797750cebSTejun Heo 	ata_msleep(link->ap, ATA_WAIT_AFTER_RESET);
3448aa2731adSTejun Heo 
3449aa2731adSTejun Heo 	return ata_wait_ready(link, deadline, check_ready);
3450aa2731adSTejun Heo }
3451aa2731adSTejun Heo 
3452aa2731adSTejun Heo /**
3453936fd732STejun Heo  *	sata_link_debounce - debounce SATA phy status
3454936fd732STejun Heo  *	@link: ATA link to debounce SATA phy status for
3455c6fd2807SJeff Garzik  *	@params: timing parameters { interval, duratinon, timeout } in msec
3456d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
3457c6fd2807SJeff Garzik  *
3458936fd732STejun Heo  *	Make sure SStatus of @link reaches stable state, determined by
3459c6fd2807SJeff Garzik  *	holding the same value where DET is not 1 for @duration polled
3460c6fd2807SJeff Garzik  *	every @interval, before @timeout.  Timeout constraints the
3461d4b2bab4STejun Heo  *	beginning of the stable state.  Because DET gets stuck at 1 on
3462d4b2bab4STejun Heo  *	some controllers after hot unplugging, this functions waits
3463c6fd2807SJeff Garzik  *	until timeout then returns 0 if DET is stable at 1.
3464c6fd2807SJeff Garzik  *
3465d4b2bab4STejun Heo  *	@timeout is further limited by @deadline.  The sooner of the
3466d4b2bab4STejun Heo  *	two is used.
3467d4b2bab4STejun Heo  *
3468c6fd2807SJeff Garzik  *	LOCKING:
3469c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3470c6fd2807SJeff Garzik  *
3471c6fd2807SJeff Garzik  *	RETURNS:
3472c6fd2807SJeff Garzik  *	0 on success, -errno on failure.
3473c6fd2807SJeff Garzik  */
3474936fd732STejun Heo int sata_link_debounce(struct ata_link *link, const unsigned long *params,
3475d4b2bab4STejun Heo 		       unsigned long deadline)
3476c6fd2807SJeff Garzik {
3477341c2c95STejun Heo 	unsigned long interval = params[0];
3478341c2c95STejun Heo 	unsigned long duration = params[1];
3479d4b2bab4STejun Heo 	unsigned long last_jiffies, t;
3480c6fd2807SJeff Garzik 	u32 last, cur;
3481c6fd2807SJeff Garzik 	int rc;
3482c6fd2807SJeff Garzik 
3483341c2c95STejun Heo 	t = ata_deadline(jiffies, params[2]);
3484d4b2bab4STejun Heo 	if (time_before(t, deadline))
3485d4b2bab4STejun Heo 		deadline = t;
3486d4b2bab4STejun Heo 
3487936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3488c6fd2807SJeff Garzik 		return rc;
3489c6fd2807SJeff Garzik 	cur &= 0xf;
3490c6fd2807SJeff Garzik 
3491c6fd2807SJeff Garzik 	last = cur;
3492c6fd2807SJeff Garzik 	last_jiffies = jiffies;
3493c6fd2807SJeff Garzik 
3494c6fd2807SJeff Garzik 	while (1) {
349597750cebSTejun Heo 		ata_msleep(link->ap, interval);
3496936fd732STejun Heo 		if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3497c6fd2807SJeff Garzik 			return rc;
3498c6fd2807SJeff Garzik 		cur &= 0xf;
3499c6fd2807SJeff Garzik 
3500c6fd2807SJeff Garzik 		/* DET stable? */
3501c6fd2807SJeff Garzik 		if (cur == last) {
3502d4b2bab4STejun Heo 			if (cur == 1 && time_before(jiffies, deadline))
3503c6fd2807SJeff Garzik 				continue;
3504341c2c95STejun Heo 			if (time_after(jiffies,
3505341c2c95STejun Heo 				       ata_deadline(last_jiffies, duration)))
3506c6fd2807SJeff Garzik 				return 0;
3507c6fd2807SJeff Garzik 			continue;
3508c6fd2807SJeff Garzik 		}
3509c6fd2807SJeff Garzik 
3510c6fd2807SJeff Garzik 		/* unstable, start over */
3511c6fd2807SJeff Garzik 		last = cur;
3512c6fd2807SJeff Garzik 		last_jiffies = jiffies;
3513c6fd2807SJeff Garzik 
3514f1545154STejun Heo 		/* Check deadline.  If debouncing failed, return
3515f1545154STejun Heo 		 * -EPIPE to tell upper layer to lower link speed.
3516f1545154STejun Heo 		 */
3517d4b2bab4STejun Heo 		if (time_after(jiffies, deadline))
3518f1545154STejun Heo 			return -EPIPE;
3519c6fd2807SJeff Garzik 	}
3520c6fd2807SJeff Garzik }
3521c6fd2807SJeff Garzik 
3522c6fd2807SJeff Garzik /**
3523936fd732STejun Heo  *	sata_link_resume - resume SATA link
3524936fd732STejun Heo  *	@link: ATA link to resume SATA
3525c6fd2807SJeff Garzik  *	@params: timing parameters { interval, duratinon, timeout } in msec
3526d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
3527c6fd2807SJeff Garzik  *
3528936fd732STejun Heo  *	Resume SATA phy @link and debounce it.
3529c6fd2807SJeff Garzik  *
3530c6fd2807SJeff Garzik  *	LOCKING:
3531c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3532c6fd2807SJeff Garzik  *
3533c6fd2807SJeff Garzik  *	RETURNS:
3534c6fd2807SJeff Garzik  *	0 on success, -errno on failure.
3535c6fd2807SJeff Garzik  */
3536936fd732STejun Heo int sata_link_resume(struct ata_link *link, const unsigned long *params,
3537d4b2bab4STejun Heo 		     unsigned long deadline)
3538c6fd2807SJeff Garzik {
35395040ab67STejun Heo 	int tries = ATA_LINK_RESUME_TRIES;
3540ac371987STejun Heo 	u32 scontrol, serror;
3541c6fd2807SJeff Garzik 	int rc;
3542c6fd2807SJeff Garzik 
3543936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3544c6fd2807SJeff Garzik 		return rc;
3545c6fd2807SJeff Garzik 
35465040ab67STejun Heo 	/*
35475040ab67STejun Heo 	 * Writes to SControl sometimes get ignored under certain
35485040ab67STejun Heo 	 * controllers (ata_piix SIDPR).  Make sure DET actually is
35495040ab67STejun Heo 	 * cleared.
35505040ab67STejun Heo 	 */
35515040ab67STejun Heo 	do {
3552c6fd2807SJeff Garzik 		scontrol = (scontrol & 0x0f0) | 0x300;
3553936fd732STejun Heo 		if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3554c6fd2807SJeff Garzik 			return rc;
35555040ab67STejun Heo 		/*
35565040ab67STejun Heo 		 * Some PHYs react badly if SStatus is pounded
35575040ab67STejun Heo 		 * immediately after resuming.  Delay 200ms before
35585040ab67STejun Heo 		 * debouncing.
3559c6fd2807SJeff Garzik 		 */
356097750cebSTejun Heo 		ata_msleep(link->ap, 200);
3561c6fd2807SJeff Garzik 
35625040ab67STejun Heo 		/* is SControl restored correctly? */
35635040ab67STejun Heo 		if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
35645040ab67STejun Heo 			return rc;
35655040ab67STejun Heo 	} while ((scontrol & 0xf0f) != 0x300 && --tries);
35665040ab67STejun Heo 
35675040ab67STejun Heo 	if ((scontrol & 0xf0f) != 0x300) {
356838941c95STejun Heo 		ata_link_warn(link, "failed to resume link (SControl %X)\n",
35695040ab67STejun Heo 			     scontrol);
35705040ab67STejun Heo 		return 0;
35715040ab67STejun Heo 	}
35725040ab67STejun Heo 
35735040ab67STejun Heo 	if (tries < ATA_LINK_RESUME_TRIES)
3574a9a79dfeSJoe Perches 		ata_link_warn(link, "link resume succeeded after %d retries\n",
35755040ab67STejun Heo 			      ATA_LINK_RESUME_TRIES - tries);
35765040ab67STejun Heo 
3577ac371987STejun Heo 	if ((rc = sata_link_debounce(link, params, deadline)))
3578ac371987STejun Heo 		return rc;
3579ac371987STejun Heo 
3580f046519fSTejun Heo 	/* clear SError, some PHYs require this even for SRST to work */
3581ac371987STejun Heo 	if (!(rc = sata_scr_read(link, SCR_ERROR, &serror)))
3582ac371987STejun Heo 		rc = sata_scr_write(link, SCR_ERROR, serror);
3583ac371987STejun Heo 
3584f046519fSTejun Heo 	return rc != -EINVAL ? rc : 0;
3585c6fd2807SJeff Garzik }
3586c6fd2807SJeff Garzik 
3587c6fd2807SJeff Garzik /**
35881152b261STejun Heo  *	sata_link_scr_lpm - manipulate SControl IPM and SPM fields
35891152b261STejun Heo  *	@link: ATA link to manipulate SControl for
35901152b261STejun Heo  *	@policy: LPM policy to configure
35911152b261STejun Heo  *	@spm_wakeup: initiate LPM transition to active state
35921152b261STejun Heo  *
35931152b261STejun Heo  *	Manipulate the IPM field of the SControl register of @link
35941152b261STejun Heo  *	according to @policy.  If @policy is ATA_LPM_MAX_POWER and
35951152b261STejun Heo  *	@spm_wakeup is %true, the SPM field is manipulated to wake up
35961152b261STejun Heo  *	the link.  This function also clears PHYRDY_CHG before
35971152b261STejun Heo  *	returning.
35981152b261STejun Heo  *
35991152b261STejun Heo  *	LOCKING:
36001152b261STejun Heo  *	EH context.
36011152b261STejun Heo  *
36021152b261STejun Heo  *	RETURNS:
36031152b261STejun Heo  *	0 on succes, -errno otherwise.
36041152b261STejun Heo  */
36051152b261STejun Heo int sata_link_scr_lpm(struct ata_link *link, enum ata_lpm_policy policy,
36061152b261STejun Heo 		      bool spm_wakeup)
36071152b261STejun Heo {
36081152b261STejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
36091152b261STejun Heo 	bool woken_up = false;
36101152b261STejun Heo 	u32 scontrol;
36111152b261STejun Heo 	int rc;
36121152b261STejun Heo 
36131152b261STejun Heo 	rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
36141152b261STejun Heo 	if (rc)
36151152b261STejun Heo 		return rc;
36161152b261STejun Heo 
36171152b261STejun Heo 	switch (policy) {
36181152b261STejun Heo 	case ATA_LPM_MAX_POWER:
36191152b261STejun Heo 		/* disable all LPM transitions */
362065fe1f0fSShane Huang 		scontrol |= (0x7 << 8);
36211152b261STejun Heo 		/* initiate transition to active state */
36221152b261STejun Heo 		if (spm_wakeup) {
36231152b261STejun Heo 			scontrol |= (0x4 << 12);
36241152b261STejun Heo 			woken_up = true;
36251152b261STejun Heo 		}
36261152b261STejun Heo 		break;
36271152b261STejun Heo 	case ATA_LPM_MED_POWER:
36281152b261STejun Heo 		/* allow LPM to PARTIAL */
36291152b261STejun Heo 		scontrol &= ~(0x1 << 8);
363065fe1f0fSShane Huang 		scontrol |= (0x6 << 8);
36311152b261STejun Heo 		break;
36321152b261STejun Heo 	case ATA_LPM_MIN_POWER:
36338a745f1fSKristen Carlson Accardi 		if (ata_link_nr_enabled(link) > 0)
36341152b261STejun Heo 			/* no restrictions on LPM transitions */
363565fe1f0fSShane Huang 			scontrol &= ~(0x7 << 8);
36368a745f1fSKristen Carlson Accardi 		else {
36378a745f1fSKristen Carlson Accardi 			/* empty port, power off */
36388a745f1fSKristen Carlson Accardi 			scontrol &= ~0xf;
36398a745f1fSKristen Carlson Accardi 			scontrol |= (0x1 << 2);
36408a745f1fSKristen Carlson Accardi 		}
36411152b261STejun Heo 		break;
36421152b261STejun Heo 	default:
36431152b261STejun Heo 		WARN_ON(1);
36441152b261STejun Heo 	}
36451152b261STejun Heo 
36461152b261STejun Heo 	rc = sata_scr_write(link, SCR_CONTROL, scontrol);
36471152b261STejun Heo 	if (rc)
36481152b261STejun Heo 		return rc;
36491152b261STejun Heo 
36501152b261STejun Heo 	/* give the link time to transit out of LPM state */
36511152b261STejun Heo 	if (woken_up)
36521152b261STejun Heo 		msleep(10);
36531152b261STejun Heo 
36541152b261STejun Heo 	/* clear PHYRDY_CHG from SError */
36551152b261STejun Heo 	ehc->i.serror &= ~SERR_PHYRDY_CHG;
36561152b261STejun Heo 	return sata_scr_write(link, SCR_ERROR, SERR_PHYRDY_CHG);
36571152b261STejun Heo }
36581152b261STejun Heo 
36591152b261STejun Heo /**
36600aa1113dSTejun Heo  *	ata_std_prereset - prepare for reset
3661cc0680a5STejun Heo  *	@link: ATA link to be reset
3662d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
3663c6fd2807SJeff Garzik  *
3664cc0680a5STejun Heo  *	@link is about to be reset.  Initialize it.  Failure from
3665b8cffc6aSTejun Heo  *	prereset makes libata abort whole reset sequence and give up
3666b8cffc6aSTejun Heo  *	that port, so prereset should be best-effort.  It does its
3667b8cffc6aSTejun Heo  *	best to prepare for reset sequence but if things go wrong, it
3668b8cffc6aSTejun Heo  *	should just whine, not fail.
3669c6fd2807SJeff Garzik  *
3670c6fd2807SJeff Garzik  *	LOCKING:
3671c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3672c6fd2807SJeff Garzik  *
3673c6fd2807SJeff Garzik  *	RETURNS:
3674c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
3675c6fd2807SJeff Garzik  */
36760aa1113dSTejun Heo int ata_std_prereset(struct ata_link *link, unsigned long deadline)
3677c6fd2807SJeff Garzik {
3678cc0680a5STejun Heo 	struct ata_port *ap = link->ap;
3679936fd732STejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
3680c6fd2807SJeff Garzik 	const unsigned long *timing = sata_ehc_deb_timing(ehc);
3681c6fd2807SJeff Garzik 	int rc;
3682c6fd2807SJeff Garzik 
3683c6fd2807SJeff Garzik 	/* if we're about to do hardreset, nothing more to do */
3684c6fd2807SJeff Garzik 	if (ehc->i.action & ATA_EH_HARDRESET)
3685c6fd2807SJeff Garzik 		return 0;
3686c6fd2807SJeff Garzik 
3687936fd732STejun Heo 	/* if SATA, resume link */
3688a16abc0bSTejun Heo 	if (ap->flags & ATA_FLAG_SATA) {
3689936fd732STejun Heo 		rc = sata_link_resume(link, timing, deadline);
3690b8cffc6aSTejun Heo 		/* whine about phy resume failure but proceed */
3691b8cffc6aSTejun Heo 		if (rc && rc != -EOPNOTSUPP)
3692a9a79dfeSJoe Perches 			ata_link_warn(link,
3693a9a79dfeSJoe Perches 				      "failed to resume link for reset (errno=%d)\n",
3694a9a79dfeSJoe Perches 				      rc);
3695c6fd2807SJeff Garzik 	}
3696c6fd2807SJeff Garzik 
369745db2f6cSTejun Heo 	/* no point in trying softreset on offline link */
3698b1c72916STejun Heo 	if (ata_phys_link_offline(link))
369945db2f6cSTejun Heo 		ehc->i.action &= ~ATA_EH_SOFTRESET;
370045db2f6cSTejun Heo 
3701c6fd2807SJeff Garzik 	return 0;
3702c6fd2807SJeff Garzik }
3703c6fd2807SJeff Garzik 
3704c6fd2807SJeff Garzik /**
3705cc0680a5STejun Heo  *	sata_link_hardreset - reset link via SATA phy reset
3706cc0680a5STejun Heo  *	@link: link to reset
3707b6103f6dSTejun Heo  *	@timing: timing parameters { interval, duratinon, timeout } in msec
3708d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
37099dadd45bSTejun Heo  *	@online: optional out parameter indicating link onlineness
37109dadd45bSTejun Heo  *	@check_ready: optional callback to check link readiness
3711c6fd2807SJeff Garzik  *
3712cc0680a5STejun Heo  *	SATA phy-reset @link using DET bits of SControl register.
37139dadd45bSTejun Heo  *	After hardreset, link readiness is waited upon using
37149dadd45bSTejun Heo  *	ata_wait_ready() if @check_ready is specified.  LLDs are
37159dadd45bSTejun Heo  *	allowed to not specify @check_ready and wait itself after this
37169dadd45bSTejun Heo  *	function returns.  Device classification is LLD's
37179dadd45bSTejun Heo  *	responsibility.
37189dadd45bSTejun Heo  *
37199dadd45bSTejun Heo  *	*@online is set to one iff reset succeeded and @link is online
37209dadd45bSTejun Heo  *	after reset.
3721c6fd2807SJeff Garzik  *
3722c6fd2807SJeff Garzik  *	LOCKING:
3723c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3724c6fd2807SJeff Garzik  *
3725c6fd2807SJeff Garzik  *	RETURNS:
3726c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
3727c6fd2807SJeff Garzik  */
3728cc0680a5STejun Heo int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
37299dadd45bSTejun Heo 			unsigned long deadline,
37309dadd45bSTejun Heo 			bool *online, int (*check_ready)(struct ata_link *))
3731c6fd2807SJeff Garzik {
3732c6fd2807SJeff Garzik 	u32 scontrol;
3733c6fd2807SJeff Garzik 	int rc;
3734c6fd2807SJeff Garzik 
3735c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
3736c6fd2807SJeff Garzik 
37379dadd45bSTejun Heo 	if (online)
37389dadd45bSTejun Heo 		*online = false;
37399dadd45bSTejun Heo 
3740936fd732STejun Heo 	if (sata_set_spd_needed(link)) {
3741c6fd2807SJeff Garzik 		/* SATA spec says nothing about how to reconfigure
3742c6fd2807SJeff Garzik 		 * spd.  To be on the safe side, turn off phy during
3743c6fd2807SJeff Garzik 		 * reconfiguration.  This works for at least ICH7 AHCI
3744c6fd2807SJeff Garzik 		 * and Sil3124.
3745c6fd2807SJeff Garzik 		 */
3746936fd732STejun Heo 		if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3747b6103f6dSTejun Heo 			goto out;
3748c6fd2807SJeff Garzik 
3749cea0d336SJeff Garzik 		scontrol = (scontrol & 0x0f0) | 0x304;
3750c6fd2807SJeff Garzik 
3751936fd732STejun Heo 		if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3752b6103f6dSTejun Heo 			goto out;
3753c6fd2807SJeff Garzik 
3754936fd732STejun Heo 		sata_set_spd(link);
3755c6fd2807SJeff Garzik 	}
3756c6fd2807SJeff Garzik 
3757c6fd2807SJeff Garzik 	/* issue phy wake/reset */
3758936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3759b6103f6dSTejun Heo 		goto out;
3760c6fd2807SJeff Garzik 
3761c6fd2807SJeff Garzik 	scontrol = (scontrol & 0x0f0) | 0x301;
3762c6fd2807SJeff Garzik 
3763936fd732STejun Heo 	if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
3764b6103f6dSTejun Heo 		goto out;
3765c6fd2807SJeff Garzik 
3766c6fd2807SJeff Garzik 	/* Couldn't find anything in SATA I/II specs, but AHCI-1.1
3767c6fd2807SJeff Garzik 	 * 10.4.2 says at least 1 ms.
3768c6fd2807SJeff Garzik 	 */
376997750cebSTejun Heo 	ata_msleep(link->ap, 1);
3770c6fd2807SJeff Garzik 
3771936fd732STejun Heo 	/* bring link back */
3772936fd732STejun Heo 	rc = sata_link_resume(link, timing, deadline);
37739dadd45bSTejun Heo 	if (rc)
37749dadd45bSTejun Heo 		goto out;
37759dadd45bSTejun Heo 	/* if link is offline nothing more to do */
3776b1c72916STejun Heo 	if (ata_phys_link_offline(link))
37779dadd45bSTejun Heo 		goto out;
37789dadd45bSTejun Heo 
37799dadd45bSTejun Heo 	/* Link is online.  From this point, -ENODEV too is an error. */
37809dadd45bSTejun Heo 	if (online)
37819dadd45bSTejun Heo 		*online = true;
37829dadd45bSTejun Heo 
3783071f44b1STejun Heo 	if (sata_pmp_supported(link->ap) && ata_is_host_link(link)) {
37849dadd45bSTejun Heo 		/* If PMP is supported, we have to do follow-up SRST.
37859dadd45bSTejun Heo 		 * Some PMPs don't send D2H Reg FIS after hardreset if
37869dadd45bSTejun Heo 		 * the first port is empty.  Wait only for
37879dadd45bSTejun Heo 		 * ATA_TMOUT_PMP_SRST_WAIT.
37889dadd45bSTejun Heo 		 */
37899dadd45bSTejun Heo 		if (check_ready) {
37909dadd45bSTejun Heo 			unsigned long pmp_deadline;
37919dadd45bSTejun Heo 
3792341c2c95STejun Heo 			pmp_deadline = ata_deadline(jiffies,
3793341c2c95STejun Heo 						    ATA_TMOUT_PMP_SRST_WAIT);
37949dadd45bSTejun Heo 			if (time_after(pmp_deadline, deadline))
37959dadd45bSTejun Heo 				pmp_deadline = deadline;
37969dadd45bSTejun Heo 			ata_wait_ready(link, pmp_deadline, check_ready);
37979dadd45bSTejun Heo 		}
37989dadd45bSTejun Heo 		rc = -EAGAIN;
37999dadd45bSTejun Heo 		goto out;
38009dadd45bSTejun Heo 	}
38019dadd45bSTejun Heo 
38029dadd45bSTejun Heo 	rc = 0;
38039dadd45bSTejun Heo 	if (check_ready)
38049dadd45bSTejun Heo 		rc = ata_wait_ready(link, deadline, check_ready);
3805b6103f6dSTejun Heo  out:
38060cbf0711STejun Heo 	if (rc && rc != -EAGAIN) {
38070cbf0711STejun Heo 		/* online is set iff link is online && reset succeeded */
38080cbf0711STejun Heo 		if (online)
38090cbf0711STejun Heo 			*online = false;
3810a9a79dfeSJoe Perches 		ata_link_err(link, "COMRESET failed (errno=%d)\n", rc);
38110cbf0711STejun Heo 	}
3812b6103f6dSTejun Heo 	DPRINTK("EXIT, rc=%d\n", rc);
3813b6103f6dSTejun Heo 	return rc;
3814b6103f6dSTejun Heo }
3815b6103f6dSTejun Heo 
3816b6103f6dSTejun Heo /**
381757c9efdfSTejun Heo  *	sata_std_hardreset - COMRESET w/o waiting or classification
381857c9efdfSTejun Heo  *	@link: link to reset
381957c9efdfSTejun Heo  *	@class: resulting class of attached device
382057c9efdfSTejun Heo  *	@deadline: deadline jiffies for the operation
382157c9efdfSTejun Heo  *
382257c9efdfSTejun Heo  *	Standard SATA COMRESET w/o waiting or classification.
382357c9efdfSTejun Heo  *
382457c9efdfSTejun Heo  *	LOCKING:
382557c9efdfSTejun Heo  *	Kernel thread context (may sleep)
382657c9efdfSTejun Heo  *
382757c9efdfSTejun Heo  *	RETURNS:
382857c9efdfSTejun Heo  *	0 if link offline, -EAGAIN if link online, -errno on errors.
382957c9efdfSTejun Heo  */
383057c9efdfSTejun Heo int sata_std_hardreset(struct ata_link *link, unsigned int *class,
383157c9efdfSTejun Heo 		       unsigned long deadline)
383257c9efdfSTejun Heo {
383357c9efdfSTejun Heo 	const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
383457c9efdfSTejun Heo 	bool online;
383557c9efdfSTejun Heo 	int rc;
383657c9efdfSTejun Heo 
383757c9efdfSTejun Heo 	/* do hardreset */
383857c9efdfSTejun Heo 	rc = sata_link_hardreset(link, timing, deadline, &online, NULL);
383957c9efdfSTejun Heo 	return online ? -EAGAIN : rc;
384057c9efdfSTejun Heo }
384157c9efdfSTejun Heo 
384257c9efdfSTejun Heo /**
3843203c75b8STejun Heo  *	ata_std_postreset - standard postreset callback
3844cc0680a5STejun Heo  *	@link: the target ata_link
3845c6fd2807SJeff Garzik  *	@classes: classes of attached devices
3846c6fd2807SJeff Garzik  *
3847c6fd2807SJeff Garzik  *	This function is invoked after a successful reset.  Note that
3848c6fd2807SJeff Garzik  *	the device might have been reset more than once using
3849c6fd2807SJeff Garzik  *	different reset methods before postreset is invoked.
3850c6fd2807SJeff Garzik  *
3851c6fd2807SJeff Garzik  *	LOCKING:
3852c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3853c6fd2807SJeff Garzik  */
3854203c75b8STejun Heo void ata_std_postreset(struct ata_link *link, unsigned int *classes)
3855c6fd2807SJeff Garzik {
3856f046519fSTejun Heo 	u32 serror;
3857f046519fSTejun Heo 
3858c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
3859c6fd2807SJeff Garzik 
3860f046519fSTejun Heo 	/* reset complete, clear SError */
3861f046519fSTejun Heo 	if (!sata_scr_read(link, SCR_ERROR, &serror))
3862f046519fSTejun Heo 		sata_scr_write(link, SCR_ERROR, serror);
3863f046519fSTejun Heo 
3864c6fd2807SJeff Garzik 	/* print link status */
3865936fd732STejun Heo 	sata_print_link_status(link);
3866c6fd2807SJeff Garzik 
3867c6fd2807SJeff Garzik 	DPRINTK("EXIT\n");
3868c6fd2807SJeff Garzik }
3869c6fd2807SJeff Garzik 
3870c6fd2807SJeff Garzik /**
3871c6fd2807SJeff Garzik  *	ata_dev_same_device - Determine whether new ID matches configured device
3872c6fd2807SJeff Garzik  *	@dev: device to compare against
3873c6fd2807SJeff Garzik  *	@new_class: class of the new device
3874c6fd2807SJeff Garzik  *	@new_id: IDENTIFY page of the new device
3875c6fd2807SJeff Garzik  *
3876c6fd2807SJeff Garzik  *	Compare @new_class and @new_id against @dev and determine
3877c6fd2807SJeff Garzik  *	whether @dev is the device indicated by @new_class and
3878c6fd2807SJeff Garzik  *	@new_id.
3879c6fd2807SJeff Garzik  *
3880c6fd2807SJeff Garzik  *	LOCKING:
3881c6fd2807SJeff Garzik  *	None.
3882c6fd2807SJeff Garzik  *
3883c6fd2807SJeff Garzik  *	RETURNS:
3884c6fd2807SJeff Garzik  *	1 if @dev matches @new_class and @new_id, 0 otherwise.
3885c6fd2807SJeff Garzik  */
3886c6fd2807SJeff Garzik static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
3887c6fd2807SJeff Garzik 			       const u16 *new_id)
3888c6fd2807SJeff Garzik {
3889c6fd2807SJeff Garzik 	const u16 *old_id = dev->id;
3890a0cf733bSTejun Heo 	unsigned char model[2][ATA_ID_PROD_LEN + 1];
3891a0cf733bSTejun Heo 	unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
3892c6fd2807SJeff Garzik 
3893c6fd2807SJeff Garzik 	if (dev->class != new_class) {
3894a9a79dfeSJoe Perches 		ata_dev_info(dev, "class mismatch %d != %d\n",
3895c6fd2807SJeff Garzik 			     dev->class, new_class);
3896c6fd2807SJeff Garzik 		return 0;
3897c6fd2807SJeff Garzik 	}
3898c6fd2807SJeff Garzik 
3899a0cf733bSTejun Heo 	ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
3900a0cf733bSTejun Heo 	ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
3901a0cf733bSTejun Heo 	ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
3902a0cf733bSTejun Heo 	ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
3903c6fd2807SJeff Garzik 
3904c6fd2807SJeff Garzik 	if (strcmp(model[0], model[1])) {
3905a9a79dfeSJoe Perches 		ata_dev_info(dev, "model number mismatch '%s' != '%s'\n",
3906a9a79dfeSJoe Perches 			     model[0], model[1]);
3907c6fd2807SJeff Garzik 		return 0;
3908c6fd2807SJeff Garzik 	}
3909c6fd2807SJeff Garzik 
3910c6fd2807SJeff Garzik 	if (strcmp(serial[0], serial[1])) {
3911a9a79dfeSJoe Perches 		ata_dev_info(dev, "serial number mismatch '%s' != '%s'\n",
3912a9a79dfeSJoe Perches 			     serial[0], serial[1]);
3913c6fd2807SJeff Garzik 		return 0;
3914c6fd2807SJeff Garzik 	}
3915c6fd2807SJeff Garzik 
3916c6fd2807SJeff Garzik 	return 1;
3917c6fd2807SJeff Garzik }
3918c6fd2807SJeff Garzik 
3919c6fd2807SJeff Garzik /**
3920fe30911bSTejun Heo  *	ata_dev_reread_id - Re-read IDENTIFY data
39213fae450cSHenrik Kretzschmar  *	@dev: target ATA device
3922bff04647STejun Heo  *	@readid_flags: read ID flags
3923c6fd2807SJeff Garzik  *
3924c6fd2807SJeff Garzik  *	Re-read IDENTIFY page and make sure @dev is still attached to
3925c6fd2807SJeff Garzik  *	the port.
3926c6fd2807SJeff Garzik  *
3927c6fd2807SJeff Garzik  *	LOCKING:
3928c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3929c6fd2807SJeff Garzik  *
3930c6fd2807SJeff Garzik  *	RETURNS:
3931c6fd2807SJeff Garzik  *	0 on success, negative errno otherwise
3932c6fd2807SJeff Garzik  */
3933fe30911bSTejun Heo int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
3934c6fd2807SJeff Garzik {
3935c6fd2807SJeff Garzik 	unsigned int class = dev->class;
39369af5c9c9STejun Heo 	u16 *id = (void *)dev->link->ap->sector_buf;
3937c6fd2807SJeff Garzik 	int rc;
3938c6fd2807SJeff Garzik 
3939c6fd2807SJeff Garzik 	/* read ID data */
3940bff04647STejun Heo 	rc = ata_dev_read_id(dev, &class, readid_flags, id);
3941c6fd2807SJeff Garzik 	if (rc)
3942fe30911bSTejun Heo 		return rc;
3943c6fd2807SJeff Garzik 
3944c6fd2807SJeff Garzik 	/* is the device still there? */
3945fe30911bSTejun Heo 	if (!ata_dev_same_device(dev, class, id))
3946fe30911bSTejun Heo 		return -ENODEV;
3947c6fd2807SJeff Garzik 
3948c6fd2807SJeff Garzik 	memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
3949fe30911bSTejun Heo 	return 0;
3950fe30911bSTejun Heo }
3951fe30911bSTejun Heo 
3952fe30911bSTejun Heo /**
3953fe30911bSTejun Heo  *	ata_dev_revalidate - Revalidate ATA device
3954fe30911bSTejun Heo  *	@dev: device to revalidate
3955422c9daaSTejun Heo  *	@new_class: new class code
3956fe30911bSTejun Heo  *	@readid_flags: read ID flags
3957fe30911bSTejun Heo  *
3958fe30911bSTejun Heo  *	Re-read IDENTIFY page, make sure @dev is still attached to the
3959fe30911bSTejun Heo  *	port and reconfigure it according to the new IDENTIFY page.
3960fe30911bSTejun Heo  *
3961fe30911bSTejun Heo  *	LOCKING:
3962fe30911bSTejun Heo  *	Kernel thread context (may sleep)
3963fe30911bSTejun Heo  *
3964fe30911bSTejun Heo  *	RETURNS:
3965fe30911bSTejun Heo  *	0 on success, negative errno otherwise
3966fe30911bSTejun Heo  */
3967422c9daaSTejun Heo int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
3968422c9daaSTejun Heo 		       unsigned int readid_flags)
3969fe30911bSTejun Heo {
39706ddcd3b0STejun Heo 	u64 n_sectors = dev->n_sectors;
39715920dadfSTejun Heo 	u64 n_native_sectors = dev->n_native_sectors;
3972fe30911bSTejun Heo 	int rc;
3973fe30911bSTejun Heo 
3974fe30911bSTejun Heo 	if (!ata_dev_enabled(dev))
3975fe30911bSTejun Heo 		return -ENODEV;
3976fe30911bSTejun Heo 
3977422c9daaSTejun Heo 	/* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
3978422c9daaSTejun Heo 	if (ata_class_enabled(new_class) &&
3979f0d0613dSBorislav Petkov 	    new_class != ATA_DEV_ATA &&
3980f0d0613dSBorislav Petkov 	    new_class != ATA_DEV_ATAPI &&
3981f0d0613dSBorislav Petkov 	    new_class != ATA_DEV_SEMB) {
3982a9a79dfeSJoe Perches 		ata_dev_info(dev, "class mismatch %u != %u\n",
3983422c9daaSTejun Heo 			     dev->class, new_class);
3984422c9daaSTejun Heo 		rc = -ENODEV;
3985422c9daaSTejun Heo 		goto fail;
3986422c9daaSTejun Heo 	}
3987422c9daaSTejun Heo 
3988fe30911bSTejun Heo 	/* re-read ID */
3989fe30911bSTejun Heo 	rc = ata_dev_reread_id(dev, readid_flags);
3990fe30911bSTejun Heo 	if (rc)
3991fe30911bSTejun Heo 		goto fail;
3992c6fd2807SJeff Garzik 
3993c6fd2807SJeff Garzik 	/* configure device according to the new ID */
3994efdaedc4STejun Heo 	rc = ata_dev_configure(dev);
39956ddcd3b0STejun Heo 	if (rc)
39966ddcd3b0STejun Heo 		goto fail;
39976ddcd3b0STejun Heo 
39986ddcd3b0STejun Heo 	/* verify n_sectors hasn't changed */
3999445d211bSTejun Heo 	if (dev->class != ATA_DEV_ATA || !n_sectors ||
4000445d211bSTejun Heo 	    dev->n_sectors == n_sectors)
4001445d211bSTejun Heo 		return 0;
4002445d211bSTejun Heo 
4003445d211bSTejun Heo 	/* n_sectors has changed */
4004a9a79dfeSJoe Perches 	ata_dev_warn(dev, "n_sectors mismatch %llu != %llu\n",
40056ddcd3b0STejun Heo 		     (unsigned long long)n_sectors,
40066ddcd3b0STejun Heo 		     (unsigned long long)dev->n_sectors);
4007445d211bSTejun Heo 
40085920dadfSTejun Heo 	/*
40095920dadfSTejun Heo 	 * Something could have caused HPA to be unlocked
4010445d211bSTejun Heo 	 * involuntarily.  If n_native_sectors hasn't changed and the
4011445d211bSTejun Heo 	 * new size matches it, keep the device.
40125920dadfSTejun Heo 	 */
40135920dadfSTejun Heo 	if (dev->n_native_sectors == n_native_sectors &&
4014445d211bSTejun Heo 	    dev->n_sectors > n_sectors && dev->n_sectors == n_native_sectors) {
4015a9a79dfeSJoe Perches 		ata_dev_warn(dev,
40165920dadfSTejun Heo 			     "new n_sectors matches native, probably "
401768939ce5STejun Heo 			     "late HPA unlock, n_sectors updated\n");
401868939ce5STejun Heo 		/* use the larger n_sectors */
4019445d211bSTejun Heo 		return 0;
4020445d211bSTejun Heo 	}
4021445d211bSTejun Heo 
4022445d211bSTejun Heo 	/*
4023445d211bSTejun Heo 	 * Some BIOSes boot w/o HPA but resume w/ HPA locked.  Try
4024445d211bSTejun Heo 	 * unlocking HPA in those cases.
4025445d211bSTejun Heo 	 *
4026445d211bSTejun Heo 	 * https://bugzilla.kernel.org/show_bug.cgi?id=15396
4027445d211bSTejun Heo 	 */
4028445d211bSTejun Heo 	if (dev->n_native_sectors == n_native_sectors &&
4029445d211bSTejun Heo 	    dev->n_sectors < n_sectors && n_sectors == n_native_sectors &&
4030445d211bSTejun Heo 	    !(dev->horkage & ATA_HORKAGE_BROKEN_HPA)) {
4031a9a79dfeSJoe Perches 		ata_dev_warn(dev,
4032445d211bSTejun Heo 			     "old n_sectors matches native, probably "
4033445d211bSTejun Heo 			     "late HPA lock, will try to unlock HPA\n");
4034445d211bSTejun Heo 		/* try unlocking HPA */
4035445d211bSTejun Heo 		dev->flags |= ATA_DFLAG_UNLOCK_HPA;
4036445d211bSTejun Heo 		rc = -EIO;
4037445d211bSTejun Heo 	} else
4038445d211bSTejun Heo 		rc = -ENODEV;
4039445d211bSTejun Heo 
4040445d211bSTejun Heo 	/* restore original n_[native_]sectors and fail */
40415920dadfSTejun Heo 	dev->n_native_sectors = n_native_sectors;
40425920dadfSTejun Heo 	dev->n_sectors = n_sectors;
4043c6fd2807SJeff Garzik  fail:
4044a9a79dfeSJoe Perches 	ata_dev_err(dev, "revalidation failed (errno=%d)\n", rc);
4045c6fd2807SJeff Garzik 	return rc;
4046c6fd2807SJeff Garzik }
4047c6fd2807SJeff Garzik 
40486919a0a6SAlan Cox struct ata_blacklist_entry {
40496919a0a6SAlan Cox 	const char *model_num;
40506919a0a6SAlan Cox 	const char *model_rev;
40516919a0a6SAlan Cox 	unsigned long horkage;
40526919a0a6SAlan Cox };
40536919a0a6SAlan Cox 
40546919a0a6SAlan Cox static const struct ata_blacklist_entry ata_device_blacklist [] = {
40556919a0a6SAlan Cox 	/* Devices with DMA related problems under Linux */
40566919a0a6SAlan Cox 	{ "WDC AC11000H",	NULL,		ATA_HORKAGE_NODMA },
40576919a0a6SAlan Cox 	{ "WDC AC22100H",	NULL,		ATA_HORKAGE_NODMA },
40586919a0a6SAlan Cox 	{ "WDC AC32500H",	NULL,		ATA_HORKAGE_NODMA },
40596919a0a6SAlan Cox 	{ "WDC AC33100H",	NULL,		ATA_HORKAGE_NODMA },
40606919a0a6SAlan Cox 	{ "WDC AC31600H",	NULL,		ATA_HORKAGE_NODMA },
40616919a0a6SAlan Cox 	{ "WDC AC32100H",	"24.09P07",	ATA_HORKAGE_NODMA },
40626919a0a6SAlan Cox 	{ "WDC AC23200L",	"21.10N21",	ATA_HORKAGE_NODMA },
40636919a0a6SAlan Cox 	{ "Compaq CRD-8241B", 	NULL,		ATA_HORKAGE_NODMA },
40646919a0a6SAlan Cox 	{ "CRD-8400B",		NULL, 		ATA_HORKAGE_NODMA },
40657da4c935SMark Lord 	{ "CRD-848[02]B",	NULL,		ATA_HORKAGE_NODMA },
40666919a0a6SAlan Cox 	{ "CRD-84",		NULL,		ATA_HORKAGE_NODMA },
40676919a0a6SAlan Cox 	{ "SanDisk SDP3B",	NULL,		ATA_HORKAGE_NODMA },
40686919a0a6SAlan Cox 	{ "SanDisk SDP3B-64",	NULL,		ATA_HORKAGE_NODMA },
40696919a0a6SAlan Cox 	{ "SANYO CD-ROM CRD",	NULL,		ATA_HORKAGE_NODMA },
40706919a0a6SAlan Cox 	{ "HITACHI CDR-8",	NULL,		ATA_HORKAGE_NODMA },
40717da4c935SMark Lord 	{ "HITACHI CDR-8[34]35",NULL,		ATA_HORKAGE_NODMA },
40726919a0a6SAlan Cox 	{ "Toshiba CD-ROM XM-6202B", NULL,	ATA_HORKAGE_NODMA },
40736919a0a6SAlan Cox 	{ "TOSHIBA CD-ROM XM-1702BC", NULL,	ATA_HORKAGE_NODMA },
40746919a0a6SAlan Cox 	{ "CD-532E-A", 		NULL,		ATA_HORKAGE_NODMA },
40756919a0a6SAlan Cox 	{ "E-IDE CD-ROM CR-840",NULL,		ATA_HORKAGE_NODMA },
40766919a0a6SAlan Cox 	{ "CD-ROM Drive/F5A",	NULL,		ATA_HORKAGE_NODMA },
40776919a0a6SAlan Cox 	{ "WPI CDD-820", 	NULL,		ATA_HORKAGE_NODMA },
40786919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SC-148C", NULL,	ATA_HORKAGE_NODMA },
40796919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SC",	NULL,		ATA_HORKAGE_NODMA },
40806919a0a6SAlan Cox 	{ "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
40816919a0a6SAlan Cox 	{ "_NEC DV5800A", 	NULL,		ATA_HORKAGE_NODMA },
40826919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SN-124", "N001",	ATA_HORKAGE_NODMA },
408339f19886SDave Jones 	{ "Seagate STT20000A", NULL,		ATA_HORKAGE_NODMA },
4084d70e551cSPrarit Bhargava 	{ "2GB ATA Flash Disk", "ADMA428M",	ATA_HORKAGE_NODMA },
40853af9a77aSTejun Heo 	/* Odd clown on sil3726/4726 PMPs */
408650af2fa1STejun Heo 	{ "Config  Disk",	NULL,		ATA_HORKAGE_DISABLE },
40876919a0a6SAlan Cox 
408818d6e9d5SAlbert Lee 	/* Weird ATAPI devices */
408940a1d531STejun Heo 	{ "TORiSAN DVD-ROM DRD-N216", NULL,	ATA_HORKAGE_MAX_SEC_128 },
40906a87e42eSTejun Heo 	{ "QUANTUM DAT    DAT72-000", NULL,	ATA_HORKAGE_ATAPI_MOD16_DMA },
409118d6e9d5SAlbert Lee 
40926919a0a6SAlan Cox 	/* Devices we expect to fail diagnostics */
40936919a0a6SAlan Cox 
40946919a0a6SAlan Cox 	/* Devices where NCQ should be avoided */
40956919a0a6SAlan Cox 	/* NCQ is slow */
40966919a0a6SAlan Cox 	{ "WDC WD740ADFD-00",	NULL,		ATA_HORKAGE_NONCQ },
4097459ad688STejun Heo 	{ "WDC WD740ADFD-00NLR1", NULL,		ATA_HORKAGE_NONCQ, },
409809125ea6STejun Heo 	/* http://thread.gmane.org/gmane.linux.ide/14907 */
409909125ea6STejun Heo 	{ "FUJITSU MHT2060BH",	NULL,		ATA_HORKAGE_NONCQ },
41007acfaf30SPaul Rolland 	/* NCQ is broken */
4101539cc7c7SJeff Garzik 	{ "Maxtor *",		"BANC*",	ATA_HORKAGE_NONCQ },
41020e3dbc01SAlan Cox 	{ "Maxtor 7V300F0",	"VA111630",	ATA_HORKAGE_NONCQ },
4103da6f0ec2SPaolo Ornati 	{ "ST380817AS",		"3.42",		ATA_HORKAGE_NONCQ },
4104e41bd3e8STejun Heo 	{ "ST3160023AS",	"3.42",		ATA_HORKAGE_NONCQ },
41055ccfca97SLubomir Bulej 	{ "OCZ CORE_SSD",	"02.10104",	ATA_HORKAGE_NONCQ },
4106539cc7c7SJeff Garzik 
4107ac70a964STejun Heo 	/* Seagate NCQ + FLUSH CACHE firmware bug */
41084d1f9082SMark Lord 	{ "ST31500341AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
4109ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4110d10d491fSTejun Heo 
41114d1f9082SMark Lord 	{ "ST31000333AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
4112d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4113d10d491fSTejun Heo 
41144d1f9082SMark Lord 	{ "ST3640[36]23AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
4115d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4116d10d491fSTejun Heo 
41174d1f9082SMark Lord 	{ "ST3320[68]13AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
4118ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4119ac70a964STejun Heo 
412036e337d0SRobert Hancock 	/* Blacklist entries taken from Silicon Image 3124/3132
412136e337d0SRobert Hancock 	   Windows driver .inf file - also several Linux problem reports */
412236e337d0SRobert Hancock 	{ "HTS541060G9SA00",    "MB3OC60D",     ATA_HORKAGE_NONCQ, },
412336e337d0SRobert Hancock 	{ "HTS541080G9SA00",    "MB4OC60D",     ATA_HORKAGE_NONCQ, },
412436e337d0SRobert Hancock 	{ "HTS541010G9SA00",    "MBZOC60D",     ATA_HORKAGE_NONCQ, },
41256919a0a6SAlan Cox 
412668b0ddb2STejun Heo 	/* https://bugzilla.kernel.org/show_bug.cgi?id=15573 */
412768b0ddb2STejun Heo 	{ "C300-CTFDDAC128MAG",	"0001",		ATA_HORKAGE_NONCQ, },
412868b0ddb2STejun Heo 
412916c55b03STejun Heo 	/* devices which puke on READ_NATIVE_MAX */
413016c55b03STejun Heo 	{ "HDS724040KLSA80",	"KFAOA20N",	ATA_HORKAGE_BROKEN_HPA, },
413116c55b03STejun Heo 	{ "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
413216c55b03STejun Heo 	{ "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
413316c55b03STejun Heo 	{ "MAXTOR 6L080L4",	"A93.0500",	ATA_HORKAGE_BROKEN_HPA },
41346919a0a6SAlan Cox 
41357831387bSTejun Heo 	/* this one allows HPA unlocking but fails IOs on the area */
41367831387bSTejun Heo 	{ "OCZ-VERTEX",		    "1.30",	ATA_HORKAGE_BROKEN_HPA },
41377831387bSTejun Heo 
413893328e11SAlan Cox 	/* Devices which report 1 sector over size HPA */
413993328e11SAlan Cox 	{ "ST340823A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
414093328e11SAlan Cox 	{ "ST320413A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
4141b152fcd3SMikko Rapeli 	{ "ST310211A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
414293328e11SAlan Cox 
41436bbfd53dSAlan Cox 	/* Devices which get the IVB wrong */
41446bbfd53dSAlan Cox 	{ "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB, },
4145a79067e5SAlan Cox 	/* Maybe we should just blacklist TSSTcorp... */
41467da4c935SMark Lord 	{ "TSSTcorp CDDVDW SH-S202[HJN]", "SB0[01]",  ATA_HORKAGE_IVB, },
41476bbfd53dSAlan Cox 
41489ce8e307SJens Axboe 	/* Devices that do not need bridging limits applied */
41499ce8e307SJens Axboe 	{ "MTRON MSP-SATA*",		NULL,	ATA_HORKAGE_BRIDGE_OK, },
415004d0f1b8SJeff Garzik 	{ "BUFFALO HD-QSU2/R5",		NULL,	ATA_HORKAGE_BRIDGE_OK, },
41519ce8e307SJens Axboe 
41529062712fSTejun Heo 	/* Devices which aren't very happy with higher link speeds */
41539062712fSTejun Heo 	{ "WD My Book",			NULL,	ATA_HORKAGE_1_5_GBPS, },
4154c531077fSDaniel J Blueman 	{ "Seagate FreeAgent GoFlex",	NULL,	ATA_HORKAGE_1_5_GBPS, },
41559062712fSTejun Heo 
4156d0cb43b3STejun Heo 	/*
4157d0cb43b3STejun Heo 	 * Devices which choke on SETXFER.  Applies only if both the
4158d0cb43b3STejun Heo 	 * device and controller are SATA.
4159d0cb43b3STejun Heo 	 */
4160cd691876STejun Heo 	{ "PIONEER DVD-RW  DVRTD08",	NULL,	ATA_HORKAGE_NOSETXFER },
41613a25179eSVladimir LAVALLADE 	{ "PIONEER DVD-RW  DVRTD08A",	NULL,	ATA_HORKAGE_NOSETXFER },
41623a25179eSVladimir LAVALLADE 	{ "PIONEER DVD-RW  DVR-215",	NULL,	ATA_HORKAGE_NOSETXFER },
4163cd691876STejun Heo 	{ "PIONEER DVD-RW  DVR-212D",	NULL,	ATA_HORKAGE_NOSETXFER },
4164cd691876STejun Heo 	{ "PIONEER DVD-RW  DVR-216D",	NULL,	ATA_HORKAGE_NOSETXFER },
4165d0cb43b3STejun Heo 
41666919a0a6SAlan Cox 	/* End Marker */
41676919a0a6SAlan Cox 	{ }
4168c6fd2807SJeff Garzik };
4169c6fd2807SJeff Garzik 
4170bce036ceSMark Lord /**
4171bce036ceSMark Lord  *	glob_match - match a text string against a glob-style pattern
4172bce036ceSMark Lord  *	@text: the string to be examined
4173bce036ceSMark Lord  *	@pattern: the glob-style pattern to be matched against
4174bce036ceSMark Lord  *
4175bce036ceSMark Lord  *	Either/both of text and pattern can be empty strings.
4176bce036ceSMark Lord  *
4177bce036ceSMark Lord  *	Match text against a glob-style pattern, with wildcards and simple sets:
4178bce036ceSMark Lord  *
4179bce036ceSMark Lord  *		?	matches any single character.
4180bce036ceSMark Lord  *		*	matches any run of characters.
4181bce036ceSMark Lord  *		[xyz]	matches a single character from the set: x, y, or z.
41822f9e4d16SMark Lord  *		[a-d]	matches a single character from the range: a, b, c, or d.
41832f9e4d16SMark Lord  *		[a-d0-9] matches a single character from either range.
4184bce036ceSMark Lord  *
41852f9e4d16SMark Lord  *	The special characters ?, [, -, or *, can be matched using a set, eg. [*]
41862f9e4d16SMark Lord  *	Behaviour with malformed patterns is undefined, though generally reasonable.
4187bce036ceSMark Lord  *
41883d2be54bSRandy Dunlap  *	Sample patterns:  "SD1?",  "SD1[0-5]",  "*R0",  "SD*1?[012]*xx"
4189bce036ceSMark Lord  *
4190bce036ceSMark Lord  *	This function uses one level of recursion per '*' in pattern.
4191bce036ceSMark Lord  *	Since it calls _nothing_ else, and has _no_ explicit local variables,
4192bce036ceSMark Lord  *	this will not cause stack problems for any reasonable use here.
4193bce036ceSMark Lord  *
4194bce036ceSMark Lord  *	RETURNS:
4195bce036ceSMark Lord  *	0 on match, 1 otherwise.
4196539cc7c7SJeff Garzik  */
4197bce036ceSMark Lord static int glob_match (const char *text, const char *pattern)
4198bce036ceSMark Lord {
4199bce036ceSMark Lord 	do {
4200bce036ceSMark Lord 		/* Match single character or a '?' wildcard */
4201bce036ceSMark Lord 		if (*text == *pattern || *pattern == '?') {
4202bce036ceSMark Lord 			if (!*pattern++)
4203bce036ceSMark Lord 				return 0;  /* End of both strings: match */
4204bce036ceSMark Lord 		} else {
4205bce036ceSMark Lord 			/* Match single char against a '[' bracketed ']' pattern set */
4206bce036ceSMark Lord 			if (!*text || *pattern != '[')
4207bce036ceSMark Lord 				break;  /* Not a pattern set */
42082f9e4d16SMark Lord 			while (*++pattern && *pattern != ']' && *text != *pattern) {
42092f9e4d16SMark Lord 				if (*pattern == '-' && *(pattern - 1) != '[')
42102f9e4d16SMark Lord 					if (*text > *(pattern - 1) && *text < *(pattern + 1)) {
42112f9e4d16SMark Lord 						++pattern;
42122f9e4d16SMark Lord 						break;
42132f9e4d16SMark Lord 					}
42142f9e4d16SMark Lord 			}
4215bce036ceSMark Lord 			if (!*pattern || *pattern == ']')
4216bce036ceSMark Lord 				return 1;  /* No match */
4217bce036ceSMark Lord 			while (*pattern && *pattern++ != ']');
4218317b50b8SAndrew Paprocki 		}
4219bce036ceSMark Lord 	} while (*++text && *pattern);
4220539cc7c7SJeff Garzik 
4221bce036ceSMark Lord 	/* Match any run of chars against a '*' wildcard */
4222bce036ceSMark Lord 	if (*pattern == '*') {
4223bce036ceSMark Lord 		if (!*++pattern)
4224bce036ceSMark Lord 			return 0;  /* Match: avoid recursion at end of pattern */
4225bce036ceSMark Lord 		/* Loop to handle additional pattern chars after the wildcard */
4226bce036ceSMark Lord 		while (*text) {
4227bce036ceSMark Lord 			if (glob_match(text, pattern) == 0)
4228bce036ceSMark Lord 				return 0;  /* Remainder matched */
4229bce036ceSMark Lord 			++text;  /* Absorb (match) this char and try again */
4230bce036ceSMark Lord 		}
4231bce036ceSMark Lord 	}
4232bce036ceSMark Lord 	if (!*text && !*pattern)
4233bce036ceSMark Lord 		return 0;  /* End of both strings: match */
4234bce036ceSMark Lord 	return 1;  /* No match */
4235539cc7c7SJeff Garzik }
4236539cc7c7SJeff Garzik 
423775683fe7STejun Heo static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
4238c6fd2807SJeff Garzik {
42398bfa79fcSTejun Heo 	unsigned char model_num[ATA_ID_PROD_LEN + 1];
42408bfa79fcSTejun Heo 	unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
42416919a0a6SAlan Cox 	const struct ata_blacklist_entry *ad = ata_device_blacklist;
4242c6fd2807SJeff Garzik 
42438bfa79fcSTejun Heo 	ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
42448bfa79fcSTejun Heo 	ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
4245c6fd2807SJeff Garzik 
42466919a0a6SAlan Cox 	while (ad->model_num) {
4247bce036ceSMark Lord 		if (!glob_match(model_num, ad->model_num)) {
42486919a0a6SAlan Cox 			if (ad->model_rev == NULL)
42496919a0a6SAlan Cox 				return ad->horkage;
4250bce036ceSMark Lord 			if (!glob_match(model_rev, ad->model_rev))
42516919a0a6SAlan Cox 				return ad->horkage;
4252c6fd2807SJeff Garzik 		}
42536919a0a6SAlan Cox 		ad++;
4254c6fd2807SJeff Garzik 	}
4255c6fd2807SJeff Garzik 	return 0;
4256c6fd2807SJeff Garzik }
4257c6fd2807SJeff Garzik 
42586919a0a6SAlan Cox static int ata_dma_blacklisted(const struct ata_device *dev)
42596919a0a6SAlan Cox {
42606919a0a6SAlan Cox 	/* We don't support polling DMA.
42616919a0a6SAlan Cox 	 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
42626919a0a6SAlan Cox 	 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
42636919a0a6SAlan Cox 	 */
42649af5c9c9STejun Heo 	if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
42656919a0a6SAlan Cox 	    (dev->flags & ATA_DFLAG_CDB_INTR))
42666919a0a6SAlan Cox 		return 1;
426775683fe7STejun Heo 	return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
42686919a0a6SAlan Cox }
42696919a0a6SAlan Cox 
4270c6fd2807SJeff Garzik /**
42716bbfd53dSAlan Cox  *	ata_is_40wire		-	check drive side detection
42726bbfd53dSAlan Cox  *	@dev: device
42736bbfd53dSAlan Cox  *
42746bbfd53dSAlan Cox  *	Perform drive side detection decoding, allowing for device vendors
42756bbfd53dSAlan Cox  *	who can't follow the documentation.
42766bbfd53dSAlan Cox  */
42776bbfd53dSAlan Cox 
42786bbfd53dSAlan Cox static int ata_is_40wire(struct ata_device *dev)
42796bbfd53dSAlan Cox {
42806bbfd53dSAlan Cox 	if (dev->horkage & ATA_HORKAGE_IVB)
42816bbfd53dSAlan Cox 		return ata_drive_40wire_relaxed(dev->id);
42826bbfd53dSAlan Cox 	return ata_drive_40wire(dev->id);
42836bbfd53dSAlan Cox }
42846bbfd53dSAlan Cox 
42856bbfd53dSAlan Cox /**
428615a5551cSAlan Cox  *	cable_is_40wire		-	40/80/SATA decider
428715a5551cSAlan Cox  *	@ap: port to consider
428815a5551cSAlan Cox  *
428915a5551cSAlan Cox  *	This function encapsulates the policy for speed management
429015a5551cSAlan Cox  *	in one place. At the moment we don't cache the result but
429115a5551cSAlan Cox  *	there is a good case for setting ap->cbl to the result when
429215a5551cSAlan Cox  *	we are called with unknown cables (and figuring out if it
429315a5551cSAlan Cox  *	impacts hotplug at all).
429415a5551cSAlan Cox  *
429515a5551cSAlan Cox  *	Return 1 if the cable appears to be 40 wire.
429615a5551cSAlan Cox  */
429715a5551cSAlan Cox 
429815a5551cSAlan Cox static int cable_is_40wire(struct ata_port *ap)
429915a5551cSAlan Cox {
430015a5551cSAlan Cox 	struct ata_link *link;
430115a5551cSAlan Cox 	struct ata_device *dev;
430215a5551cSAlan Cox 
43034a9c7b33STejun Heo 	/* If the controller thinks we are 40 wire, we are. */
430415a5551cSAlan Cox 	if (ap->cbl == ATA_CBL_PATA40)
430515a5551cSAlan Cox 		return 1;
43064a9c7b33STejun Heo 
43074a9c7b33STejun Heo 	/* If the controller thinks we are 80 wire, we are. */
430815a5551cSAlan Cox 	if (ap->cbl == ATA_CBL_PATA80 || ap->cbl == ATA_CBL_SATA)
430915a5551cSAlan Cox 		return 0;
43104a9c7b33STejun Heo 
43114a9c7b33STejun Heo 	/* If the system is known to be 40 wire short cable (eg
43124a9c7b33STejun Heo 	 * laptop), then we allow 80 wire modes even if the drive
43134a9c7b33STejun Heo 	 * isn't sure.
43144a9c7b33STejun Heo 	 */
4315f792068eSAlan Cox 	if (ap->cbl == ATA_CBL_PATA40_SHORT)
4316f792068eSAlan Cox 		return 0;
431715a5551cSAlan Cox 
43184a9c7b33STejun Heo 	/* If the controller doesn't know, we scan.
43194a9c7b33STejun Heo 	 *
43204a9c7b33STejun Heo 	 * Note: We look for all 40 wire detects at this point.  Any
43214a9c7b33STejun Heo 	 *       80 wire detect is taken to be 80 wire cable because
43224a9c7b33STejun Heo 	 * - in many setups only the one drive (slave if present) will
43234a9c7b33STejun Heo 	 *   give a valid detect
43244a9c7b33STejun Heo 	 * - if you have a non detect capable drive you don't want it
43254a9c7b33STejun Heo 	 *   to colour the choice
432615a5551cSAlan Cox 	 */
43271eca4365STejun Heo 	ata_for_each_link(link, ap, EDGE) {
43281eca4365STejun Heo 		ata_for_each_dev(dev, link, ENABLED) {
43291eca4365STejun Heo 			if (!ata_is_40wire(dev))
433015a5551cSAlan Cox 				return 0;
433115a5551cSAlan Cox 		}
433215a5551cSAlan Cox 	}
433315a5551cSAlan Cox 	return 1;
433415a5551cSAlan Cox }
433515a5551cSAlan Cox 
433615a5551cSAlan Cox /**
4337c6fd2807SJeff Garzik  *	ata_dev_xfermask - Compute supported xfermask of the given device
4338c6fd2807SJeff Garzik  *	@dev: Device to compute xfermask for
4339c6fd2807SJeff Garzik  *
4340c6fd2807SJeff Garzik  *	Compute supported xfermask of @dev and store it in
4341c6fd2807SJeff Garzik  *	dev->*_mask.  This function is responsible for applying all
4342c6fd2807SJeff Garzik  *	known limits including host controller limits, device
4343c6fd2807SJeff Garzik  *	blacklist, etc...
4344c6fd2807SJeff Garzik  *
4345c6fd2807SJeff Garzik  *	LOCKING:
4346c6fd2807SJeff Garzik  *	None.
4347c6fd2807SJeff Garzik  */
4348c6fd2807SJeff Garzik static void ata_dev_xfermask(struct ata_device *dev)
4349c6fd2807SJeff Garzik {
43509af5c9c9STejun Heo 	struct ata_link *link = dev->link;
43519af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
4352cca3974eSJeff Garzik 	struct ata_host *host = ap->host;
4353c6fd2807SJeff Garzik 	unsigned long xfer_mask;
4354c6fd2807SJeff Garzik 
4355c6fd2807SJeff Garzik 	/* controller modes available */
4356c6fd2807SJeff Garzik 	xfer_mask = ata_pack_xfermask(ap->pio_mask,
4357c6fd2807SJeff Garzik 				      ap->mwdma_mask, ap->udma_mask);
4358c6fd2807SJeff Garzik 
43598343f889SRobert Hancock 	/* drive modes available */
4360c6fd2807SJeff Garzik 	xfer_mask &= ata_pack_xfermask(dev->pio_mask,
4361c6fd2807SJeff Garzik 				       dev->mwdma_mask, dev->udma_mask);
4362c6fd2807SJeff Garzik 	xfer_mask &= ata_id_xfermask(dev->id);
4363c6fd2807SJeff Garzik 
4364b352e57dSAlan Cox 	/*
4365b352e57dSAlan Cox 	 *	CFA Advanced TrueIDE timings are not allowed on a shared
4366b352e57dSAlan Cox 	 *	cable
4367b352e57dSAlan Cox 	 */
4368b352e57dSAlan Cox 	if (ata_dev_pair(dev)) {
4369b352e57dSAlan Cox 		/* No PIO5 or PIO6 */
4370b352e57dSAlan Cox 		xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
4371b352e57dSAlan Cox 		/* No MWDMA3 or MWDMA 4 */
4372b352e57dSAlan Cox 		xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
4373b352e57dSAlan Cox 	}
4374b352e57dSAlan Cox 
4375c6fd2807SJeff Garzik 	if (ata_dma_blacklisted(dev)) {
4376c6fd2807SJeff Garzik 		xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4377a9a79dfeSJoe Perches 		ata_dev_warn(dev,
4378c6fd2807SJeff Garzik 			     "device is on DMA blacklist, disabling DMA\n");
4379c6fd2807SJeff Garzik 	}
4380c6fd2807SJeff Garzik 
438114d66ab7SPetr Vandrovec 	if ((host->flags & ATA_HOST_SIMPLEX) &&
438214d66ab7SPetr Vandrovec 	    host->simplex_claimed && host->simplex_claimed != ap) {
4383c6fd2807SJeff Garzik 		xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4384a9a79dfeSJoe Perches 		ata_dev_warn(dev,
4385a9a79dfeSJoe Perches 			     "simplex DMA is claimed by other device, disabling DMA\n");
4386c6fd2807SJeff Garzik 	}
4387c6fd2807SJeff Garzik 
4388e424675fSJeff Garzik 	if (ap->flags & ATA_FLAG_NO_IORDY)
4389e424675fSJeff Garzik 		xfer_mask &= ata_pio_mask_no_iordy(dev);
4390e424675fSJeff Garzik 
4391c6fd2807SJeff Garzik 	if (ap->ops->mode_filter)
4392a76b62caSAlan Cox 		xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
4393c6fd2807SJeff Garzik 
43948343f889SRobert Hancock 	/* Apply cable rule here.  Don't apply it early because when
43958343f889SRobert Hancock 	 * we handle hot plug the cable type can itself change.
43968343f889SRobert Hancock 	 * Check this last so that we know if the transfer rate was
43978343f889SRobert Hancock 	 * solely limited by the cable.
43988343f889SRobert Hancock 	 * Unknown or 80 wire cables reported host side are checked
43998343f889SRobert Hancock 	 * drive side as well. Cases where we know a 40wire cable
44008343f889SRobert Hancock 	 * is used safely for 80 are not checked here.
44018343f889SRobert Hancock 	 */
44028343f889SRobert Hancock 	if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
44038343f889SRobert Hancock 		/* UDMA/44 or higher would be available */
440415a5551cSAlan Cox 		if (cable_is_40wire(ap)) {
4405a9a79dfeSJoe Perches 			ata_dev_warn(dev,
44068343f889SRobert Hancock 				     "limited to UDMA/33 due to 40-wire cable\n");
44078343f889SRobert Hancock 			xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
44088343f889SRobert Hancock 		}
44098343f889SRobert Hancock 
4410c6fd2807SJeff Garzik 	ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
4411c6fd2807SJeff Garzik 			    &dev->mwdma_mask, &dev->udma_mask);
4412c6fd2807SJeff Garzik }
4413c6fd2807SJeff Garzik 
4414c6fd2807SJeff Garzik /**
4415c6fd2807SJeff Garzik  *	ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
4416c6fd2807SJeff Garzik  *	@dev: Device to which command will be sent
4417c6fd2807SJeff Garzik  *
4418c6fd2807SJeff Garzik  *	Issue SET FEATURES - XFER MODE command to device @dev
4419c6fd2807SJeff Garzik  *	on port @ap.
4420c6fd2807SJeff Garzik  *
4421c6fd2807SJeff Garzik  *	LOCKING:
4422c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
4423c6fd2807SJeff Garzik  *
4424c6fd2807SJeff Garzik  *	RETURNS:
4425c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
4426c6fd2807SJeff Garzik  */
4427c6fd2807SJeff Garzik 
4428c6fd2807SJeff Garzik static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
4429c6fd2807SJeff Garzik {
4430c6fd2807SJeff Garzik 	struct ata_taskfile tf;
4431c6fd2807SJeff Garzik 	unsigned int err_mask;
4432c6fd2807SJeff Garzik 
4433c6fd2807SJeff Garzik 	/* set up set-features taskfile */
4434c6fd2807SJeff Garzik 	DPRINTK("set features - xfer mode\n");
4435c6fd2807SJeff Garzik 
4436464cf177STejun Heo 	/* Some controllers and ATAPI devices show flaky interrupt
4437464cf177STejun Heo 	 * behavior after setting xfer mode.  Use polling instead.
4438464cf177STejun Heo 	 */
4439c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
4440c6fd2807SJeff Garzik 	tf.command = ATA_CMD_SET_FEATURES;
4441c6fd2807SJeff Garzik 	tf.feature = SETFEATURES_XFER;
4442464cf177STejun Heo 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
4443c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
4444b9f8ab2dSAlan Cox 	/* If we are using IORDY we must send the mode setting command */
4445b9f8ab2dSAlan Cox 	if (ata_pio_need_iordy(dev))
4446c6fd2807SJeff Garzik 		tf.nsect = dev->xfer_mode;
4447b9f8ab2dSAlan Cox 	/* If the device has IORDY and the controller does not - turn it off */
4448b9f8ab2dSAlan Cox  	else if (ata_id_has_iordy(dev->id))
4449b9f8ab2dSAlan Cox 		tf.nsect = 0x01;
4450b9f8ab2dSAlan Cox 	else /* In the ancient relic department - skip all of this */
4451b9f8ab2dSAlan Cox 		return 0;
4452c6fd2807SJeff Garzik 
44532b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
4454c6fd2807SJeff Garzik 
4455c6fd2807SJeff Garzik 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
4456c6fd2807SJeff Garzik 	return err_mask;
4457c6fd2807SJeff Garzik }
44581152b261STejun Heo 
4459c6fd2807SJeff Garzik /**
4460218f3d30SJeff Garzik  *	ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES
44619f45cbd3SKristen Carlson Accardi  *	@dev: Device to which command will be sent
44629f45cbd3SKristen Carlson Accardi  *	@enable: Whether to enable or disable the feature
4463218f3d30SJeff Garzik  *	@feature: The sector count represents the feature to set
44649f45cbd3SKristen Carlson Accardi  *
44659f45cbd3SKristen Carlson Accardi  *	Issue SET FEATURES - SATA FEATURES command to device @dev
4466218f3d30SJeff Garzik  *	on port @ap with sector count
44679f45cbd3SKristen Carlson Accardi  *
44689f45cbd3SKristen Carlson Accardi  *	LOCKING:
44699f45cbd3SKristen Carlson Accardi  *	PCI/etc. bus probe sem.
44709f45cbd3SKristen Carlson Accardi  *
44719f45cbd3SKristen Carlson Accardi  *	RETURNS:
44729f45cbd3SKristen Carlson Accardi  *	0 on success, AC_ERR_* mask otherwise.
44739f45cbd3SKristen Carlson Accardi  */
44741152b261STejun Heo unsigned int ata_dev_set_feature(struct ata_device *dev, u8 enable, u8 feature)
44759f45cbd3SKristen Carlson Accardi {
44769f45cbd3SKristen Carlson Accardi 	struct ata_taskfile tf;
44779f45cbd3SKristen Carlson Accardi 	unsigned int err_mask;
44789f45cbd3SKristen Carlson Accardi 
44799f45cbd3SKristen Carlson Accardi 	/* set up set-features taskfile */
44809f45cbd3SKristen Carlson Accardi 	DPRINTK("set features - SATA features\n");
44819f45cbd3SKristen Carlson Accardi 
44829f45cbd3SKristen Carlson Accardi 	ata_tf_init(dev, &tf);
44839f45cbd3SKristen Carlson Accardi 	tf.command = ATA_CMD_SET_FEATURES;
44849f45cbd3SKristen Carlson Accardi 	tf.feature = enable;
44859f45cbd3SKristen Carlson Accardi 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
44869f45cbd3SKristen Carlson Accardi 	tf.protocol = ATA_PROT_NODATA;
4487218f3d30SJeff Garzik 	tf.nsect = feature;
44889f45cbd3SKristen Carlson Accardi 
44892b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
44909f45cbd3SKristen Carlson Accardi 
44919f45cbd3SKristen Carlson Accardi 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
44929f45cbd3SKristen Carlson Accardi 	return err_mask;
44939f45cbd3SKristen Carlson Accardi }
44949f45cbd3SKristen Carlson Accardi 
44959f45cbd3SKristen Carlson Accardi /**
4496c6fd2807SJeff Garzik  *	ata_dev_init_params - Issue INIT DEV PARAMS command
4497c6fd2807SJeff Garzik  *	@dev: Device to which command will be sent
4498c6fd2807SJeff Garzik  *	@heads: Number of heads (taskfile parameter)
4499c6fd2807SJeff Garzik  *	@sectors: Number of sectors (taskfile parameter)
4500c6fd2807SJeff Garzik  *
4501c6fd2807SJeff Garzik  *	LOCKING:
4502c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
4503c6fd2807SJeff Garzik  *
4504c6fd2807SJeff Garzik  *	RETURNS:
4505c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
4506c6fd2807SJeff Garzik  */
4507c6fd2807SJeff Garzik static unsigned int ata_dev_init_params(struct ata_device *dev,
4508c6fd2807SJeff Garzik 					u16 heads, u16 sectors)
4509c6fd2807SJeff Garzik {
4510c6fd2807SJeff Garzik 	struct ata_taskfile tf;
4511c6fd2807SJeff Garzik 	unsigned int err_mask;
4512c6fd2807SJeff Garzik 
4513c6fd2807SJeff Garzik 	/* Number of sectors per track 1-255. Number of heads 1-16 */
4514c6fd2807SJeff Garzik 	if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
4515c6fd2807SJeff Garzik 		return AC_ERR_INVALID;
4516c6fd2807SJeff Garzik 
4517c6fd2807SJeff Garzik 	/* set up init dev params taskfile */
4518c6fd2807SJeff Garzik 	DPRINTK("init dev params \n");
4519c6fd2807SJeff Garzik 
4520c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
4521c6fd2807SJeff Garzik 	tf.command = ATA_CMD_INIT_DEV_PARAMS;
4522c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4523c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
4524c6fd2807SJeff Garzik 	tf.nsect = sectors;
4525c6fd2807SJeff Garzik 	tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
4526c6fd2807SJeff Garzik 
45272b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
452818b2466cSAlan Cox 	/* A clean abort indicates an original or just out of spec drive
452918b2466cSAlan Cox 	   and we should continue as we issue the setup based on the
453018b2466cSAlan Cox 	   drive reported working geometry */
453118b2466cSAlan Cox 	if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
453218b2466cSAlan Cox 		err_mask = 0;
4533c6fd2807SJeff Garzik 
4534c6fd2807SJeff Garzik 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
4535c6fd2807SJeff Garzik 	return err_mask;
4536c6fd2807SJeff Garzik }
4537c6fd2807SJeff Garzik 
4538c6fd2807SJeff Garzik /**
4539c6fd2807SJeff Garzik  *	ata_sg_clean - Unmap DMA memory associated with command
4540c6fd2807SJeff Garzik  *	@qc: Command containing DMA memory to be released
4541c6fd2807SJeff Garzik  *
4542c6fd2807SJeff Garzik  *	Unmap all mapped DMA memory associated with this command.
4543c6fd2807SJeff Garzik  *
4544c6fd2807SJeff Garzik  *	LOCKING:
4545cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4546c6fd2807SJeff Garzik  */
454770e6ad0cSTejun Heo void ata_sg_clean(struct ata_queued_cmd *qc)
4548c6fd2807SJeff Garzik {
4549c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4550ff2aeb1eSTejun Heo 	struct scatterlist *sg = qc->sg;
4551c6fd2807SJeff Garzik 	int dir = qc->dma_dir;
4552c6fd2807SJeff Garzik 
4553efcb3cf7STejun Heo 	WARN_ON_ONCE(sg == NULL);
4554c6fd2807SJeff Garzik 
4555dde20207SJames Bottomley 	VPRINTK("unmapping %u sg elements\n", qc->n_elem);
4556c6fd2807SJeff Garzik 
4557dde20207SJames Bottomley 	if (qc->n_elem)
45585825627cSFUJITA Tomonori 		dma_unmap_sg(ap->dev, sg, qc->orig_n_elem, dir);
4559c6fd2807SJeff Garzik 
4560c6fd2807SJeff Garzik 	qc->flags &= ~ATA_QCFLAG_DMAMAP;
4561ff2aeb1eSTejun Heo 	qc->sg = NULL;
4562c6fd2807SJeff Garzik }
4563c6fd2807SJeff Garzik 
4564c6fd2807SJeff Garzik /**
45655895ef9aSTejun Heo  *	atapi_check_dma - Check whether ATAPI DMA can be supported
4566c6fd2807SJeff Garzik  *	@qc: Metadata associated with taskfile to check
4567c6fd2807SJeff Garzik  *
4568c6fd2807SJeff Garzik  *	Allow low-level driver to filter ATA PACKET commands, returning
4569c6fd2807SJeff Garzik  *	a status indicating whether or not it is OK to use DMA for the
4570c6fd2807SJeff Garzik  *	supplied PACKET command.
4571c6fd2807SJeff Garzik  *
4572c6fd2807SJeff Garzik  *	LOCKING:
4573cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4574c6fd2807SJeff Garzik  *
4575c6fd2807SJeff Garzik  *	RETURNS: 0 when ATAPI DMA can be used
4576c6fd2807SJeff Garzik  *               nonzero otherwise
4577c6fd2807SJeff Garzik  */
45785895ef9aSTejun Heo int atapi_check_dma(struct ata_queued_cmd *qc)
4579c6fd2807SJeff Garzik {
4580c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4581c6fd2807SJeff Garzik 
4582b9a4197eSTejun Heo 	/* Don't allow DMA if it isn't multiple of 16 bytes.  Quite a
4583b9a4197eSTejun Heo 	 * few ATAPI devices choke on such DMA requests.
4584b9a4197eSTejun Heo 	 */
45856a87e42eSTejun Heo 	if (!(qc->dev->horkage & ATA_HORKAGE_ATAPI_MOD16_DMA) &&
45866a87e42eSTejun Heo 	    unlikely(qc->nbytes & 15))
45876f23a31dSAlbert Lee 		return 1;
45886f23a31dSAlbert Lee 
4589c6fd2807SJeff Garzik 	if (ap->ops->check_atapi_dma)
4590b9a4197eSTejun Heo 		return ap->ops->check_atapi_dma(qc);
4591c6fd2807SJeff Garzik 
4592b9a4197eSTejun Heo 	return 0;
4593c6fd2807SJeff Garzik }
4594b9a4197eSTejun Heo 
4595c6fd2807SJeff Garzik /**
459631cc23b3STejun Heo  *	ata_std_qc_defer - Check whether a qc needs to be deferred
459731cc23b3STejun Heo  *	@qc: ATA command in question
459831cc23b3STejun Heo  *
459931cc23b3STejun Heo  *	Non-NCQ commands cannot run with any other command, NCQ or
460031cc23b3STejun Heo  *	not.  As upper layer only knows the queue depth, we are
460131cc23b3STejun Heo  *	responsible for maintaining exclusion.  This function checks
460231cc23b3STejun Heo  *	whether a new command @qc can be issued.
460331cc23b3STejun Heo  *
460431cc23b3STejun Heo  *	LOCKING:
460531cc23b3STejun Heo  *	spin_lock_irqsave(host lock)
460631cc23b3STejun Heo  *
460731cc23b3STejun Heo  *	RETURNS:
460831cc23b3STejun Heo  *	ATA_DEFER_* if deferring is needed, 0 otherwise.
460931cc23b3STejun Heo  */
461031cc23b3STejun Heo int ata_std_qc_defer(struct ata_queued_cmd *qc)
461131cc23b3STejun Heo {
461231cc23b3STejun Heo 	struct ata_link *link = qc->dev->link;
461331cc23b3STejun Heo 
461431cc23b3STejun Heo 	if (qc->tf.protocol == ATA_PROT_NCQ) {
461531cc23b3STejun Heo 		if (!ata_tag_valid(link->active_tag))
461631cc23b3STejun Heo 			return 0;
461731cc23b3STejun Heo 	} else {
461831cc23b3STejun Heo 		if (!ata_tag_valid(link->active_tag) && !link->sactive)
461931cc23b3STejun Heo 			return 0;
462031cc23b3STejun Heo 	}
462131cc23b3STejun Heo 
462231cc23b3STejun Heo 	return ATA_DEFER_LINK;
462331cc23b3STejun Heo }
462431cc23b3STejun Heo 
4625c6fd2807SJeff Garzik void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
4626c6fd2807SJeff Garzik 
4627c6fd2807SJeff Garzik /**
4628c6fd2807SJeff Garzik  *	ata_sg_init - Associate command with scatter-gather table.
4629c6fd2807SJeff Garzik  *	@qc: Command to be associated
4630c6fd2807SJeff Garzik  *	@sg: Scatter-gather table.
4631c6fd2807SJeff Garzik  *	@n_elem: Number of elements in s/g table.
4632c6fd2807SJeff Garzik  *
4633c6fd2807SJeff Garzik  *	Initialize the data-related elements of queued_cmd @qc
4634c6fd2807SJeff Garzik  *	to point to a scatter-gather table @sg, containing @n_elem
4635c6fd2807SJeff Garzik  *	elements.
4636c6fd2807SJeff Garzik  *
4637c6fd2807SJeff Garzik  *	LOCKING:
4638cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4639c6fd2807SJeff Garzik  */
4640c6fd2807SJeff Garzik void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
4641c6fd2807SJeff Garzik 		 unsigned int n_elem)
4642c6fd2807SJeff Garzik {
4643ff2aeb1eSTejun Heo 	qc->sg = sg;
4644c6fd2807SJeff Garzik 	qc->n_elem = n_elem;
4645ff2aeb1eSTejun Heo 	qc->cursg = qc->sg;
4646ff2aeb1eSTejun Heo }
4647ff2aeb1eSTejun Heo 
4648c6fd2807SJeff Garzik /**
4649c6fd2807SJeff Garzik  *	ata_sg_setup - DMA-map the scatter-gather table associated with a command.
4650c6fd2807SJeff Garzik  *	@qc: Command with scatter-gather table to be mapped.
4651c6fd2807SJeff Garzik  *
4652c6fd2807SJeff Garzik  *	DMA-map the scatter-gather table associated with queued_cmd @qc.
4653c6fd2807SJeff Garzik  *
4654c6fd2807SJeff Garzik  *	LOCKING:
4655cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4656c6fd2807SJeff Garzik  *
4657c6fd2807SJeff Garzik  *	RETURNS:
4658c6fd2807SJeff Garzik  *	Zero on success, negative on error.
4659c6fd2807SJeff Garzik  *
4660c6fd2807SJeff Garzik  */
4661c6fd2807SJeff Garzik static int ata_sg_setup(struct ata_queued_cmd *qc)
4662c6fd2807SJeff Garzik {
4663c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4664dde20207SJames Bottomley 	unsigned int n_elem;
4665c6fd2807SJeff Garzik 
466644877b4eSTejun Heo 	VPRINTK("ENTER, ata%u\n", ap->print_id);
4667c6fd2807SJeff Garzik 
4668dde20207SJames Bottomley 	n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir);
4669dde20207SJames Bottomley 	if (n_elem < 1)
4670c6fd2807SJeff Garzik 		return -1;
4671c6fd2807SJeff Garzik 
4672dde20207SJames Bottomley 	DPRINTK("%d sg elements mapped\n", n_elem);
46735825627cSFUJITA Tomonori 	qc->orig_n_elem = qc->n_elem;
4674dde20207SJames Bottomley 	qc->n_elem = n_elem;
4675f92a2636STejun Heo 	qc->flags |= ATA_QCFLAG_DMAMAP;
4676c6fd2807SJeff Garzik 
4677c6fd2807SJeff Garzik 	return 0;
4678c6fd2807SJeff Garzik }
4679c6fd2807SJeff Garzik 
4680c6fd2807SJeff Garzik /**
4681c6fd2807SJeff Garzik  *	swap_buf_le16 - swap halves of 16-bit words in place
4682c6fd2807SJeff Garzik  *	@buf:  Buffer to swap
4683c6fd2807SJeff Garzik  *	@buf_words:  Number of 16-bit words in buffer.
4684c6fd2807SJeff Garzik  *
4685c6fd2807SJeff Garzik  *	Swap halves of 16-bit words if needed to convert from
4686c6fd2807SJeff Garzik  *	little-endian byte order to native cpu byte order, or
4687c6fd2807SJeff Garzik  *	vice-versa.
4688c6fd2807SJeff Garzik  *
4689c6fd2807SJeff Garzik  *	LOCKING:
4690c6fd2807SJeff Garzik  *	Inherited from caller.
4691c6fd2807SJeff Garzik  */
4692c6fd2807SJeff Garzik void swap_buf_le16(u16 *buf, unsigned int buf_words)
4693c6fd2807SJeff Garzik {
4694c6fd2807SJeff Garzik #ifdef __BIG_ENDIAN
4695c6fd2807SJeff Garzik 	unsigned int i;
4696c6fd2807SJeff Garzik 
4697c6fd2807SJeff Garzik 	for (i = 0; i < buf_words; i++)
4698c6fd2807SJeff Garzik 		buf[i] = le16_to_cpu(buf[i]);
4699c6fd2807SJeff Garzik #endif /* __BIG_ENDIAN */
4700c6fd2807SJeff Garzik }
4701c6fd2807SJeff Garzik 
4702c6fd2807SJeff Garzik /**
47038a8bc223STejun Heo  *	ata_qc_new - Request an available ATA command, for queueing
47045eb66fe0SRandy Dunlap  *	@ap: target port
47058a8bc223STejun Heo  *
47068a8bc223STejun Heo  *	LOCKING:
47078a8bc223STejun Heo  *	None.
47088a8bc223STejun Heo  */
47098a8bc223STejun Heo 
47108a8bc223STejun Heo static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
47118a8bc223STejun Heo {
47128a8bc223STejun Heo 	struct ata_queued_cmd *qc = NULL;
47138a8bc223STejun Heo 	unsigned int i;
47148a8bc223STejun Heo 
47158a8bc223STejun Heo 	/* no command while frozen */
47168a8bc223STejun Heo 	if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
47178a8bc223STejun Heo 		return NULL;
47188a8bc223STejun Heo 
47198a8bc223STejun Heo 	/* the last tag is reserved for internal command. */
47208a8bc223STejun Heo 	for (i = 0; i < ATA_MAX_QUEUE - 1; i++)
47218a8bc223STejun Heo 		if (!test_and_set_bit(i, &ap->qc_allocated)) {
47228a8bc223STejun Heo 			qc = __ata_qc_from_tag(ap, i);
47238a8bc223STejun Heo 			break;
47248a8bc223STejun Heo 		}
47258a8bc223STejun Heo 
47268a8bc223STejun Heo 	if (qc)
47278a8bc223STejun Heo 		qc->tag = i;
47288a8bc223STejun Heo 
47298a8bc223STejun Heo 	return qc;
47308a8bc223STejun Heo }
47318a8bc223STejun Heo 
47328a8bc223STejun Heo /**
4733c6fd2807SJeff Garzik  *	ata_qc_new_init - Request an available ATA command, and initialize it
4734c6fd2807SJeff Garzik  *	@dev: Device from whom we request an available command structure
4735c6fd2807SJeff Garzik  *
4736c6fd2807SJeff Garzik  *	LOCKING:
4737c6fd2807SJeff Garzik  *	None.
4738c6fd2807SJeff Garzik  */
4739c6fd2807SJeff Garzik 
47408a8bc223STejun Heo struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev)
4741c6fd2807SJeff Garzik {
47429af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
4743c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
4744c6fd2807SJeff Garzik 
47458a8bc223STejun Heo 	qc = ata_qc_new(ap);
4746c6fd2807SJeff Garzik 	if (qc) {
4747c6fd2807SJeff Garzik 		qc->scsicmd = NULL;
4748c6fd2807SJeff Garzik 		qc->ap = ap;
4749c6fd2807SJeff Garzik 		qc->dev = dev;
4750c6fd2807SJeff Garzik 
4751c6fd2807SJeff Garzik 		ata_qc_reinit(qc);
4752c6fd2807SJeff Garzik 	}
4753c6fd2807SJeff Garzik 
4754c6fd2807SJeff Garzik 	return qc;
4755c6fd2807SJeff Garzik }
4756c6fd2807SJeff Garzik 
47578a8bc223STejun Heo /**
47588a8bc223STejun Heo  *	ata_qc_free - free unused ata_queued_cmd
47598a8bc223STejun Heo  *	@qc: Command to complete
47608a8bc223STejun Heo  *
47618a8bc223STejun Heo  *	Designed to free unused ata_queued_cmd object
47628a8bc223STejun Heo  *	in case something prevents using it.
47638a8bc223STejun Heo  *
47648a8bc223STejun Heo  *	LOCKING:
47658a8bc223STejun Heo  *	spin_lock_irqsave(host lock)
47668a8bc223STejun Heo  */
47678a8bc223STejun Heo void ata_qc_free(struct ata_queued_cmd *qc)
47688a8bc223STejun Heo {
4769a1104016SJulia Lawall 	struct ata_port *ap;
47708a8bc223STejun Heo 	unsigned int tag;
47718a8bc223STejun Heo 
4772efcb3cf7STejun Heo 	WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4773a1104016SJulia Lawall 	ap = qc->ap;
47748a8bc223STejun Heo 
47758a8bc223STejun Heo 	qc->flags = 0;
47768a8bc223STejun Heo 	tag = qc->tag;
47778a8bc223STejun Heo 	if (likely(ata_tag_valid(tag))) {
47788a8bc223STejun Heo 		qc->tag = ATA_TAG_POISON;
47798a8bc223STejun Heo 		clear_bit(tag, &ap->qc_allocated);
47808a8bc223STejun Heo 	}
47818a8bc223STejun Heo }
47828a8bc223STejun Heo 
4783c6fd2807SJeff Garzik void __ata_qc_complete(struct ata_queued_cmd *qc)
4784c6fd2807SJeff Garzik {
4785a1104016SJulia Lawall 	struct ata_port *ap;
4786a1104016SJulia Lawall 	struct ata_link *link;
4787c6fd2807SJeff Garzik 
4788efcb3cf7STejun Heo 	WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4789efcb3cf7STejun Heo 	WARN_ON_ONCE(!(qc->flags & ATA_QCFLAG_ACTIVE));
4790a1104016SJulia Lawall 	ap = qc->ap;
4791a1104016SJulia Lawall 	link = qc->dev->link;
4792c6fd2807SJeff Garzik 
4793c6fd2807SJeff Garzik 	if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
4794c6fd2807SJeff Garzik 		ata_sg_clean(qc);
4795c6fd2807SJeff Garzik 
4796c6fd2807SJeff Garzik 	/* command should be marked inactive atomically with qc completion */
4797da917d69STejun Heo 	if (qc->tf.protocol == ATA_PROT_NCQ) {
47989af5c9c9STejun Heo 		link->sactive &= ~(1 << qc->tag);
4799da917d69STejun Heo 		if (!link->sactive)
4800da917d69STejun Heo 			ap->nr_active_links--;
4801da917d69STejun Heo 	} else {
48029af5c9c9STejun Heo 		link->active_tag = ATA_TAG_POISON;
4803da917d69STejun Heo 		ap->nr_active_links--;
4804da917d69STejun Heo 	}
4805da917d69STejun Heo 
4806da917d69STejun Heo 	/* clear exclusive status */
4807da917d69STejun Heo 	if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
4808da917d69STejun Heo 		     ap->excl_link == link))
4809da917d69STejun Heo 		ap->excl_link = NULL;
4810c6fd2807SJeff Garzik 
4811c6fd2807SJeff Garzik 	/* atapi: mark qc as inactive to prevent the interrupt handler
4812c6fd2807SJeff Garzik 	 * from completing the command twice later, before the error handler
4813c6fd2807SJeff Garzik 	 * is called. (when rc != 0 and atapi request sense is needed)
4814c6fd2807SJeff Garzik 	 */
4815c6fd2807SJeff Garzik 	qc->flags &= ~ATA_QCFLAG_ACTIVE;
4816c6fd2807SJeff Garzik 	ap->qc_active &= ~(1 << qc->tag);
4817c6fd2807SJeff Garzik 
4818c6fd2807SJeff Garzik 	/* call completion callback */
4819c6fd2807SJeff Garzik 	qc->complete_fn(qc);
4820c6fd2807SJeff Garzik }
4821c6fd2807SJeff Garzik 
482239599a53STejun Heo static void fill_result_tf(struct ata_queued_cmd *qc)
482339599a53STejun Heo {
482439599a53STejun Heo 	struct ata_port *ap = qc->ap;
482539599a53STejun Heo 
482639599a53STejun Heo 	qc->result_tf.flags = qc->tf.flags;
482722183bf5STejun Heo 	ap->ops->qc_fill_rtf(qc);
482839599a53STejun Heo }
482939599a53STejun Heo 
483000115e0fSTejun Heo static void ata_verify_xfer(struct ata_queued_cmd *qc)
483100115e0fSTejun Heo {
483200115e0fSTejun Heo 	struct ata_device *dev = qc->dev;
483300115e0fSTejun Heo 
483400115e0fSTejun Heo 	if (ata_is_nodata(qc->tf.protocol))
483500115e0fSTejun Heo 		return;
483600115e0fSTejun Heo 
483700115e0fSTejun Heo 	if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
483800115e0fSTejun Heo 		return;
483900115e0fSTejun Heo 
484000115e0fSTejun Heo 	dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
484100115e0fSTejun Heo }
484200115e0fSTejun Heo 
4843c6fd2807SJeff Garzik /**
4844c6fd2807SJeff Garzik  *	ata_qc_complete - Complete an active ATA command
4845c6fd2807SJeff Garzik  *	@qc: Command to complete
4846c6fd2807SJeff Garzik  *
48471aadf5c3STejun Heo  *	Indicate to the mid and upper layers that an ATA command has
48481aadf5c3STejun Heo  *	completed, with either an ok or not-ok status.
48491aadf5c3STejun Heo  *
48501aadf5c3STejun Heo  *	Refrain from calling this function multiple times when
48511aadf5c3STejun Heo  *	successfully completing multiple NCQ commands.
48521aadf5c3STejun Heo  *	ata_qc_complete_multiple() should be used instead, which will
48531aadf5c3STejun Heo  *	properly update IRQ expect state.
4854c6fd2807SJeff Garzik  *
4855c6fd2807SJeff Garzik  *	LOCKING:
4856cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4857c6fd2807SJeff Garzik  */
4858c6fd2807SJeff Garzik void ata_qc_complete(struct ata_queued_cmd *qc)
4859c6fd2807SJeff Garzik {
4860c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4861c6fd2807SJeff Garzik 
4862c6fd2807SJeff Garzik 	/* XXX: New EH and old EH use different mechanisms to
4863c6fd2807SJeff Garzik 	 * synchronize EH with regular execution path.
4864c6fd2807SJeff Garzik 	 *
4865c6fd2807SJeff Garzik 	 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
4866c6fd2807SJeff Garzik 	 * Normal execution path is responsible for not accessing a
4867c6fd2807SJeff Garzik 	 * failed qc.  libata core enforces the rule by returning NULL
4868c6fd2807SJeff Garzik 	 * from ata_qc_from_tag() for failed qcs.
4869c6fd2807SJeff Garzik 	 *
4870c6fd2807SJeff Garzik 	 * Old EH depends on ata_qc_complete() nullifying completion
4871c6fd2807SJeff Garzik 	 * requests if ATA_QCFLAG_EH_SCHEDULED is set.  Old EH does
4872c6fd2807SJeff Garzik 	 * not synchronize with interrupt handler.  Only PIO task is
4873c6fd2807SJeff Garzik 	 * taken care of.
4874c6fd2807SJeff Garzik 	 */
4875c6fd2807SJeff Garzik 	if (ap->ops->error_handler) {
48764dbfa39bSTejun Heo 		struct ata_device *dev = qc->dev;
48774dbfa39bSTejun Heo 		struct ata_eh_info *ehi = &dev->link->eh_info;
48784dbfa39bSTejun Heo 
4879c6fd2807SJeff Garzik 		if (unlikely(qc->err_mask))
4880c6fd2807SJeff Garzik 			qc->flags |= ATA_QCFLAG_FAILED;
4881c6fd2807SJeff Garzik 
4882f08dc1acSTejun Heo 		/*
4883f08dc1acSTejun Heo 		 * Finish internal commands without any further processing
4884f08dc1acSTejun Heo 		 * and always with the result TF filled.
4885f08dc1acSTejun Heo 		 */
4886f08dc1acSTejun Heo 		if (unlikely(ata_tag_internal(qc->tag))) {
488739599a53STejun Heo 			fill_result_tf(qc);
4888f4b31db9STejun Heo 			__ata_qc_complete(qc);
4889c6fd2807SJeff Garzik 			return;
4890c6fd2807SJeff Garzik 		}
4891c6fd2807SJeff Garzik 
4892f08dc1acSTejun Heo 		/*
4893f08dc1acSTejun Heo 		 * Non-internal qc has failed.  Fill the result TF and
4894f08dc1acSTejun Heo 		 * summon EH.
4895f08dc1acSTejun Heo 		 */
4896f08dc1acSTejun Heo 		if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
4897f08dc1acSTejun Heo 			fill_result_tf(qc);
4898f08dc1acSTejun Heo 			ata_qc_schedule_eh(qc);
4899f08dc1acSTejun Heo 			return;
4900f08dc1acSTejun Heo 		}
4901f08dc1acSTejun Heo 
49024dc738edSTejun Heo 		WARN_ON_ONCE(ap->pflags & ATA_PFLAG_FROZEN);
49034dc738edSTejun Heo 
4904c6fd2807SJeff Garzik 		/* read result TF if requested */
4905c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_RESULT_TF)
490639599a53STejun Heo 			fill_result_tf(qc);
4907c6fd2807SJeff Garzik 
49084dbfa39bSTejun Heo 		/* Some commands need post-processing after successful
49094dbfa39bSTejun Heo 		 * completion.
49104dbfa39bSTejun Heo 		 */
49114dbfa39bSTejun Heo 		switch (qc->tf.command) {
49124dbfa39bSTejun Heo 		case ATA_CMD_SET_FEATURES:
49134dbfa39bSTejun Heo 			if (qc->tf.feature != SETFEATURES_WC_ON &&
49144dbfa39bSTejun Heo 			    qc->tf.feature != SETFEATURES_WC_OFF)
49154dbfa39bSTejun Heo 				break;
49164dbfa39bSTejun Heo 			/* fall through */
49174dbfa39bSTejun Heo 		case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
49184dbfa39bSTejun Heo 		case ATA_CMD_SET_MULTI: /* multi_count changed */
49194dbfa39bSTejun Heo 			/* revalidate device */
49204dbfa39bSTejun Heo 			ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE;
49214dbfa39bSTejun Heo 			ata_port_schedule_eh(ap);
49224dbfa39bSTejun Heo 			break;
4923054a5fbaSTejun Heo 
4924054a5fbaSTejun Heo 		case ATA_CMD_SLEEP:
4925054a5fbaSTejun Heo 			dev->flags |= ATA_DFLAG_SLEEPING;
4926054a5fbaSTejun Heo 			break;
49274dbfa39bSTejun Heo 		}
49284dbfa39bSTejun Heo 
492900115e0fSTejun Heo 		if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
493000115e0fSTejun Heo 			ata_verify_xfer(qc);
493100115e0fSTejun Heo 
4932c6fd2807SJeff Garzik 		__ata_qc_complete(qc);
4933c6fd2807SJeff Garzik 	} else {
4934c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
4935c6fd2807SJeff Garzik 			return;
4936c6fd2807SJeff Garzik 
4937c6fd2807SJeff Garzik 		/* read result TF if failed or requested */
4938c6fd2807SJeff Garzik 		if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
493939599a53STejun Heo 			fill_result_tf(qc);
4940c6fd2807SJeff Garzik 
4941c6fd2807SJeff Garzik 		__ata_qc_complete(qc);
4942c6fd2807SJeff Garzik 	}
4943c6fd2807SJeff Garzik }
4944c6fd2807SJeff Garzik 
4945c6fd2807SJeff Garzik /**
4946c6fd2807SJeff Garzik  *	ata_qc_complete_multiple - Complete multiple qcs successfully
4947c6fd2807SJeff Garzik  *	@ap: port in question
4948c6fd2807SJeff Garzik  *	@qc_active: new qc_active mask
4949c6fd2807SJeff Garzik  *
4950c6fd2807SJeff Garzik  *	Complete in-flight commands.  This functions is meant to be
4951c6fd2807SJeff Garzik  *	called from low-level driver's interrupt routine to complete
4952c6fd2807SJeff Garzik  *	requests normally.  ap->qc_active and @qc_active is compared
4953c6fd2807SJeff Garzik  *	and commands are completed accordingly.
4954c6fd2807SJeff Garzik  *
49551aadf5c3STejun Heo  *	Always use this function when completing multiple NCQ commands
49561aadf5c3STejun Heo  *	from IRQ handlers instead of calling ata_qc_complete()
49571aadf5c3STejun Heo  *	multiple times to keep IRQ expect status properly in sync.
49581aadf5c3STejun Heo  *
4959c6fd2807SJeff Garzik  *	LOCKING:
4960cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4961c6fd2807SJeff Garzik  *
4962c6fd2807SJeff Garzik  *	RETURNS:
4963c6fd2807SJeff Garzik  *	Number of completed commands on success, -errno otherwise.
4964c6fd2807SJeff Garzik  */
496579f97dadSTejun Heo int ata_qc_complete_multiple(struct ata_port *ap, u32 qc_active)
4966c6fd2807SJeff Garzik {
4967c6fd2807SJeff Garzik 	int nr_done = 0;
4968c6fd2807SJeff Garzik 	u32 done_mask;
4969c6fd2807SJeff Garzik 
4970c6fd2807SJeff Garzik 	done_mask = ap->qc_active ^ qc_active;
4971c6fd2807SJeff Garzik 
4972c6fd2807SJeff Garzik 	if (unlikely(done_mask & qc_active)) {
4973a9a79dfeSJoe Perches 		ata_port_err(ap, "illegal qc_active transition (%08x->%08x)\n",
4974a9a79dfeSJoe Perches 			     ap->qc_active, qc_active);
4975c6fd2807SJeff Garzik 		return -EINVAL;
4976c6fd2807SJeff Garzik 	}
4977c6fd2807SJeff Garzik 
497843768180SJens Axboe 	while (done_mask) {
4979c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc;
498043768180SJens Axboe 		unsigned int tag = __ffs(done_mask);
4981c6fd2807SJeff Garzik 
498243768180SJens Axboe 		qc = ata_qc_from_tag(ap, tag);
498343768180SJens Axboe 		if (qc) {
4984c6fd2807SJeff Garzik 			ata_qc_complete(qc);
4985c6fd2807SJeff Garzik 			nr_done++;
4986c6fd2807SJeff Garzik 		}
498743768180SJens Axboe 		done_mask &= ~(1 << tag);
4988c6fd2807SJeff Garzik 	}
4989c6fd2807SJeff Garzik 
4990c6fd2807SJeff Garzik 	return nr_done;
4991c6fd2807SJeff Garzik }
4992c6fd2807SJeff Garzik 
4993c6fd2807SJeff Garzik /**
4994c6fd2807SJeff Garzik  *	ata_qc_issue - issue taskfile to device
4995c6fd2807SJeff Garzik  *	@qc: command to issue to device
4996c6fd2807SJeff Garzik  *
4997c6fd2807SJeff Garzik  *	Prepare an ATA command to submission to device.
4998c6fd2807SJeff Garzik  *	This includes mapping the data into a DMA-able
4999c6fd2807SJeff Garzik  *	area, filling in the S/G table, and finally
5000c6fd2807SJeff Garzik  *	writing the taskfile to hardware, starting the command.
5001c6fd2807SJeff Garzik  *
5002c6fd2807SJeff Garzik  *	LOCKING:
5003cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
5004c6fd2807SJeff Garzik  */
5005c6fd2807SJeff Garzik void ata_qc_issue(struct ata_queued_cmd *qc)
5006c6fd2807SJeff Garzik {
5007c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
50089af5c9c9STejun Heo 	struct ata_link *link = qc->dev->link;
5009405e66b3STejun Heo 	u8 prot = qc->tf.protocol;
5010c6fd2807SJeff Garzik 
5011c6fd2807SJeff Garzik 	/* Make sure only one non-NCQ command is outstanding.  The
5012c6fd2807SJeff Garzik 	 * check is skipped for old EH because it reuses active qc to
5013c6fd2807SJeff Garzik 	 * request ATAPI sense.
5014c6fd2807SJeff Garzik 	 */
5015efcb3cf7STejun Heo 	WARN_ON_ONCE(ap->ops->error_handler && ata_tag_valid(link->active_tag));
5016c6fd2807SJeff Garzik 
50171973a023STejun Heo 	if (ata_is_ncq(prot)) {
5018efcb3cf7STejun Heo 		WARN_ON_ONCE(link->sactive & (1 << qc->tag));
5019da917d69STejun Heo 
5020da917d69STejun Heo 		if (!link->sactive)
5021da917d69STejun Heo 			ap->nr_active_links++;
50229af5c9c9STejun Heo 		link->sactive |= 1 << qc->tag;
5023c6fd2807SJeff Garzik 	} else {
5024efcb3cf7STejun Heo 		WARN_ON_ONCE(link->sactive);
5025da917d69STejun Heo 
5026da917d69STejun Heo 		ap->nr_active_links++;
50279af5c9c9STejun Heo 		link->active_tag = qc->tag;
5028c6fd2807SJeff Garzik 	}
5029c6fd2807SJeff Garzik 
5030c6fd2807SJeff Garzik 	qc->flags |= ATA_QCFLAG_ACTIVE;
5031c6fd2807SJeff Garzik 	ap->qc_active |= 1 << qc->tag;
5032c6fd2807SJeff Garzik 
503360f5d6efSTejun Heo 	/*
503460f5d6efSTejun Heo 	 * We guarantee to LLDs that they will have at least one
5035f92a2636STejun Heo 	 * non-zero sg if the command is a data command.
5036f92a2636STejun Heo 	 */
503760f5d6efSTejun Heo 	if (WARN_ON_ONCE(ata_is_data(prot) &&
503860f5d6efSTejun Heo 			 (!qc->sg || !qc->n_elem || !qc->nbytes)))
503960f5d6efSTejun Heo 		goto sys_err;
5040f92a2636STejun Heo 
5041405e66b3STejun Heo 	if (ata_is_dma(prot) || (ata_is_pio(prot) &&
5042f92a2636STejun Heo 				 (ap->flags & ATA_FLAG_PIO_DMA)))
5043c6fd2807SJeff Garzik 		if (ata_sg_setup(qc))
504460f5d6efSTejun Heo 			goto sys_err;
5045c6fd2807SJeff Garzik 
5046cf480626STejun Heo 	/* if device is sleeping, schedule reset and abort the link */
5047054a5fbaSTejun Heo 	if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
5048cf480626STejun Heo 		link->eh_info.action |= ATA_EH_RESET;
5049054a5fbaSTejun Heo 		ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
5050054a5fbaSTejun Heo 		ata_link_abort(link);
5051054a5fbaSTejun Heo 		return;
5052054a5fbaSTejun Heo 	}
5053054a5fbaSTejun Heo 
5054c6fd2807SJeff Garzik 	ap->ops->qc_prep(qc);
5055c6fd2807SJeff Garzik 
5056c6fd2807SJeff Garzik 	qc->err_mask |= ap->ops->qc_issue(qc);
5057c6fd2807SJeff Garzik 	if (unlikely(qc->err_mask))
5058c6fd2807SJeff Garzik 		goto err;
5059c6fd2807SJeff Garzik 	return;
5060c6fd2807SJeff Garzik 
506160f5d6efSTejun Heo sys_err:
5062c6fd2807SJeff Garzik 	qc->err_mask |= AC_ERR_SYSTEM;
5063c6fd2807SJeff Garzik err:
5064c6fd2807SJeff Garzik 	ata_qc_complete(qc);
5065c6fd2807SJeff Garzik }
5066c6fd2807SJeff Garzik 
5067c6fd2807SJeff Garzik /**
5068c6fd2807SJeff Garzik  *	sata_scr_valid - test whether SCRs are accessible
5069936fd732STejun Heo  *	@link: ATA link to test SCR accessibility for
5070c6fd2807SJeff Garzik  *
5071936fd732STejun Heo  *	Test whether SCRs are accessible for @link.
5072c6fd2807SJeff Garzik  *
5073c6fd2807SJeff Garzik  *	LOCKING:
5074c6fd2807SJeff Garzik  *	None.
5075c6fd2807SJeff Garzik  *
5076c6fd2807SJeff Garzik  *	RETURNS:
5077c6fd2807SJeff Garzik  *	1 if SCRs are accessible, 0 otherwise.
5078c6fd2807SJeff Garzik  */
5079936fd732STejun Heo int sata_scr_valid(struct ata_link *link)
5080c6fd2807SJeff Garzik {
5081936fd732STejun Heo 	struct ata_port *ap = link->ap;
5082936fd732STejun Heo 
5083a16abc0bSTejun Heo 	return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
5084c6fd2807SJeff Garzik }
5085c6fd2807SJeff Garzik 
5086c6fd2807SJeff Garzik /**
5087c6fd2807SJeff Garzik  *	sata_scr_read - read SCR register of the specified port
5088936fd732STejun Heo  *	@link: ATA link to read SCR for
5089c6fd2807SJeff Garzik  *	@reg: SCR to read
5090c6fd2807SJeff Garzik  *	@val: Place to store read value
5091c6fd2807SJeff Garzik  *
5092936fd732STejun Heo  *	Read SCR register @reg of @link into *@val.  This function is
5093633273a3STejun Heo  *	guaranteed to succeed if @link is ap->link, the cable type of
5094633273a3STejun Heo  *	the port is SATA and the port implements ->scr_read.
5095c6fd2807SJeff Garzik  *
5096c6fd2807SJeff Garzik  *	LOCKING:
5097633273a3STejun Heo  *	None if @link is ap->link.  Kernel thread context otherwise.
5098c6fd2807SJeff Garzik  *
5099c6fd2807SJeff Garzik  *	RETURNS:
5100c6fd2807SJeff Garzik  *	0 on success, negative errno on failure.
5101c6fd2807SJeff Garzik  */
5102936fd732STejun Heo int sata_scr_read(struct ata_link *link, int reg, u32 *val)
5103c6fd2807SJeff Garzik {
5104633273a3STejun Heo 	if (ata_is_host_link(link)) {
5105936fd732STejun Heo 		if (sata_scr_valid(link))
510682ef04fbSTejun Heo 			return link->ap->ops->scr_read(link, reg, val);
5107c6fd2807SJeff Garzik 		return -EOPNOTSUPP;
5108c6fd2807SJeff Garzik 	}
5109c6fd2807SJeff Garzik 
5110633273a3STejun Heo 	return sata_pmp_scr_read(link, reg, val);
5111633273a3STejun Heo }
5112633273a3STejun Heo 
5113c6fd2807SJeff Garzik /**
5114c6fd2807SJeff Garzik  *	sata_scr_write - write SCR register of the specified port
5115936fd732STejun Heo  *	@link: ATA link to write SCR for
5116c6fd2807SJeff Garzik  *	@reg: SCR to write
5117c6fd2807SJeff Garzik  *	@val: value to write
5118c6fd2807SJeff Garzik  *
5119936fd732STejun Heo  *	Write @val to SCR register @reg of @link.  This function is
5120633273a3STejun Heo  *	guaranteed to succeed if @link is ap->link, the cable type of
5121633273a3STejun Heo  *	the port is SATA and the port implements ->scr_read.
5122c6fd2807SJeff Garzik  *
5123c6fd2807SJeff Garzik  *	LOCKING:
5124633273a3STejun Heo  *	None if @link is ap->link.  Kernel thread context otherwise.
5125c6fd2807SJeff Garzik  *
5126c6fd2807SJeff Garzik  *	RETURNS:
5127c6fd2807SJeff Garzik  *	0 on success, negative errno on failure.
5128c6fd2807SJeff Garzik  */
5129936fd732STejun Heo int sata_scr_write(struct ata_link *link, int reg, u32 val)
5130c6fd2807SJeff Garzik {
5131633273a3STejun Heo 	if (ata_is_host_link(link)) {
5132936fd732STejun Heo 		if (sata_scr_valid(link))
513382ef04fbSTejun Heo 			return link->ap->ops->scr_write(link, reg, val);
5134c6fd2807SJeff Garzik 		return -EOPNOTSUPP;
5135c6fd2807SJeff Garzik 	}
5136c6fd2807SJeff Garzik 
5137633273a3STejun Heo 	return sata_pmp_scr_write(link, reg, val);
5138633273a3STejun Heo }
5139633273a3STejun Heo 
5140c6fd2807SJeff Garzik /**
5141c6fd2807SJeff Garzik  *	sata_scr_write_flush - write SCR register of the specified port and flush
5142936fd732STejun Heo  *	@link: ATA link to write SCR for
5143c6fd2807SJeff Garzik  *	@reg: SCR to write
5144c6fd2807SJeff Garzik  *	@val: value to write
5145c6fd2807SJeff Garzik  *
5146c6fd2807SJeff Garzik  *	This function is identical to sata_scr_write() except that this
5147c6fd2807SJeff Garzik  *	function performs flush after writing to the register.
5148c6fd2807SJeff Garzik  *
5149c6fd2807SJeff Garzik  *	LOCKING:
5150633273a3STejun Heo  *	None if @link is ap->link.  Kernel thread context otherwise.
5151c6fd2807SJeff Garzik  *
5152c6fd2807SJeff Garzik  *	RETURNS:
5153c6fd2807SJeff Garzik  *	0 on success, negative errno on failure.
5154c6fd2807SJeff Garzik  */
5155936fd732STejun Heo int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
5156c6fd2807SJeff Garzik {
5157633273a3STejun Heo 	if (ata_is_host_link(link)) {
5158da3dbb17STejun Heo 		int rc;
5159da3dbb17STejun Heo 
5160936fd732STejun Heo 		if (sata_scr_valid(link)) {
516182ef04fbSTejun Heo 			rc = link->ap->ops->scr_write(link, reg, val);
5162da3dbb17STejun Heo 			if (rc == 0)
516382ef04fbSTejun Heo 				rc = link->ap->ops->scr_read(link, reg, &val);
5164da3dbb17STejun Heo 			return rc;
5165c6fd2807SJeff Garzik 		}
5166c6fd2807SJeff Garzik 		return -EOPNOTSUPP;
5167c6fd2807SJeff Garzik 	}
5168c6fd2807SJeff Garzik 
5169633273a3STejun Heo 	return sata_pmp_scr_write(link, reg, val);
5170633273a3STejun Heo }
5171633273a3STejun Heo 
5172c6fd2807SJeff Garzik /**
5173b1c72916STejun Heo  *	ata_phys_link_online - test whether the given link is online
5174936fd732STejun Heo  *	@link: ATA link to test
5175c6fd2807SJeff Garzik  *
5176936fd732STejun Heo  *	Test whether @link is online.  Note that this function returns
5177936fd732STejun Heo  *	0 if online status of @link cannot be obtained, so
5178936fd732STejun Heo  *	ata_link_online(link) != !ata_link_offline(link).
5179c6fd2807SJeff Garzik  *
5180c6fd2807SJeff Garzik  *	LOCKING:
5181c6fd2807SJeff Garzik  *	None.
5182c6fd2807SJeff Garzik  *
5183c6fd2807SJeff Garzik  *	RETURNS:
5184b5b3fa38STejun Heo  *	True if the port online status is available and online.
5185c6fd2807SJeff Garzik  */
5186b1c72916STejun Heo bool ata_phys_link_online(struct ata_link *link)
5187c6fd2807SJeff Garzik {
5188c6fd2807SJeff Garzik 	u32 sstatus;
5189c6fd2807SJeff Garzik 
5190936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
51919913ff8aSTejun Heo 	    ata_sstatus_online(sstatus))
5192b5b3fa38STejun Heo 		return true;
5193b5b3fa38STejun Heo 	return false;
5194c6fd2807SJeff Garzik }
5195c6fd2807SJeff Garzik 
5196c6fd2807SJeff Garzik /**
5197b1c72916STejun Heo  *	ata_phys_link_offline - test whether the given link is offline
5198936fd732STejun Heo  *	@link: ATA link to test
5199c6fd2807SJeff Garzik  *
5200936fd732STejun Heo  *	Test whether @link is offline.  Note that this function
5201936fd732STejun Heo  *	returns 0 if offline status of @link cannot be obtained, so
5202936fd732STejun Heo  *	ata_link_online(link) != !ata_link_offline(link).
5203c6fd2807SJeff Garzik  *
5204c6fd2807SJeff Garzik  *	LOCKING:
5205c6fd2807SJeff Garzik  *	None.
5206c6fd2807SJeff Garzik  *
5207c6fd2807SJeff Garzik  *	RETURNS:
5208b5b3fa38STejun Heo  *	True if the port offline status is available and offline.
5209c6fd2807SJeff Garzik  */
5210b1c72916STejun Heo bool ata_phys_link_offline(struct ata_link *link)
5211c6fd2807SJeff Garzik {
5212c6fd2807SJeff Garzik 	u32 sstatus;
5213c6fd2807SJeff Garzik 
5214936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
52159913ff8aSTejun Heo 	    !ata_sstatus_online(sstatus))
5216b5b3fa38STejun Heo 		return true;
5217b5b3fa38STejun Heo 	return false;
5218c6fd2807SJeff Garzik }
5219c6fd2807SJeff Garzik 
5220b1c72916STejun Heo /**
5221b1c72916STejun Heo  *	ata_link_online - test whether the given link is online
5222b1c72916STejun Heo  *	@link: ATA link to test
5223b1c72916STejun Heo  *
5224b1c72916STejun Heo  *	Test whether @link is online.  This is identical to
5225b1c72916STejun Heo  *	ata_phys_link_online() when there's no slave link.  When
5226b1c72916STejun Heo  *	there's a slave link, this function should only be called on
5227b1c72916STejun Heo  *	the master link and will return true if any of M/S links is
5228b1c72916STejun Heo  *	online.
5229b1c72916STejun Heo  *
5230b1c72916STejun Heo  *	LOCKING:
5231b1c72916STejun Heo  *	None.
5232b1c72916STejun Heo  *
5233b1c72916STejun Heo  *	RETURNS:
5234b1c72916STejun Heo  *	True if the port online status is available and online.
5235b1c72916STejun Heo  */
5236b1c72916STejun Heo bool ata_link_online(struct ata_link *link)
5237b1c72916STejun Heo {
5238b1c72916STejun Heo 	struct ata_link *slave = link->ap->slave_link;
5239b1c72916STejun Heo 
5240b1c72916STejun Heo 	WARN_ON(link == slave);	/* shouldn't be called on slave link */
5241b1c72916STejun Heo 
5242b1c72916STejun Heo 	return ata_phys_link_online(link) ||
5243b1c72916STejun Heo 		(slave && ata_phys_link_online(slave));
5244b1c72916STejun Heo }
5245b1c72916STejun Heo 
5246b1c72916STejun Heo /**
5247b1c72916STejun Heo  *	ata_link_offline - test whether the given link is offline
5248b1c72916STejun Heo  *	@link: ATA link to test
5249b1c72916STejun Heo  *
5250b1c72916STejun Heo  *	Test whether @link is offline.  This is identical to
5251b1c72916STejun Heo  *	ata_phys_link_offline() when there's no slave link.  When
5252b1c72916STejun Heo  *	there's a slave link, this function should only be called on
5253b1c72916STejun Heo  *	the master link and will return true if both M/S links are
5254b1c72916STejun Heo  *	offline.
5255b1c72916STejun Heo  *
5256b1c72916STejun Heo  *	LOCKING:
5257b1c72916STejun Heo  *	None.
5258b1c72916STejun Heo  *
5259b1c72916STejun Heo  *	RETURNS:
5260b1c72916STejun Heo  *	True if the port offline status is available and offline.
5261b1c72916STejun Heo  */
5262b1c72916STejun Heo bool ata_link_offline(struct ata_link *link)
5263b1c72916STejun Heo {
5264b1c72916STejun Heo 	struct ata_link *slave = link->ap->slave_link;
5265b1c72916STejun Heo 
5266b1c72916STejun Heo 	WARN_ON(link == slave);	/* shouldn't be called on slave link */
5267b1c72916STejun Heo 
5268b1c72916STejun Heo 	return ata_phys_link_offline(link) &&
5269b1c72916STejun Heo 		(!slave || ata_phys_link_offline(slave));
5270b1c72916STejun Heo }
5271b1c72916STejun Heo 
52726ffa01d8STejun Heo #ifdef CONFIG_PM
52735ef41082SLin Ming static int ata_port_request_pm(struct ata_port *ap, pm_message_t mesg,
5274cca3974eSJeff Garzik 			       unsigned int action, unsigned int ehi_flags,
5275cca3974eSJeff Garzik 			       int wait)
5276c6fd2807SJeff Garzik {
5277e3667ebfSTejun Heo 	struct ata_link *link;
52785ef41082SLin Ming 	unsigned long flags;
52795ef41082SLin Ming 	int rc;
5280c6fd2807SJeff Garzik 
5281c6fd2807SJeff Garzik 	/* Previous resume operation might still be in
5282c6fd2807SJeff Garzik 	 * progress.  Wait for PM_PENDING to clear.
5283c6fd2807SJeff Garzik 	 */
5284c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_PM_PENDING) {
5285c6fd2807SJeff Garzik 		ata_port_wait_eh(ap);
5286c6fd2807SJeff Garzik 		WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5287c6fd2807SJeff Garzik 	}
5288c6fd2807SJeff Garzik 
5289c6fd2807SJeff Garzik 	/* request PM ops to EH */
5290c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
5291c6fd2807SJeff Garzik 
5292c6fd2807SJeff Garzik 	ap->pm_mesg = mesg;
5293c6fd2807SJeff Garzik 	if (wait) {
5294c6fd2807SJeff Garzik 		rc = 0;
5295c6fd2807SJeff Garzik 		ap->pm_result = &rc;
5296c6fd2807SJeff Garzik 	}
5297c6fd2807SJeff Garzik 
5298c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_PM_PENDING;
52991eca4365STejun Heo 	ata_for_each_link(link, ap, HOST_FIRST) {
5300e3667ebfSTejun Heo 		link->eh_info.action |= action;
5301e3667ebfSTejun Heo 		link->eh_info.flags |= ehi_flags;
5302e3667ebfSTejun Heo 	}
5303c6fd2807SJeff Garzik 
5304c6fd2807SJeff Garzik 	ata_port_schedule_eh(ap);
5305c6fd2807SJeff Garzik 
5306c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
5307c6fd2807SJeff Garzik 
5308c6fd2807SJeff Garzik 	/* wait and check result */
5309c6fd2807SJeff Garzik 	if (wait) {
5310c6fd2807SJeff Garzik 		ata_port_wait_eh(ap);
5311c6fd2807SJeff Garzik 		WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5312c6fd2807SJeff Garzik 	}
5313c6fd2807SJeff Garzik 
53145ef41082SLin Ming 	return rc;
5315c6fd2807SJeff Garzik }
5316c6fd2807SJeff Garzik 
531733574d68SLin Ming static int ata_port_suspend_common(struct device *dev, pm_message_t mesg)
53185ef41082SLin Ming {
53195ef41082SLin Ming 	struct ata_port *ap = to_ata_port(dev);
532033574d68SLin Ming 	unsigned int ehi_flags = ATA_EHI_QUIET;
53215ef41082SLin Ming 	int rc;
53225ef41082SLin Ming 
532333574d68SLin Ming 	/*
532433574d68SLin Ming 	 * On some hardware, device fails to respond after spun down
532533574d68SLin Ming 	 * for suspend.  As the device won't be used before being
532633574d68SLin Ming 	 * resumed, we don't need to touch the device.  Ask EH to skip
532733574d68SLin Ming 	 * the usual stuff and proceed directly to suspend.
532833574d68SLin Ming 	 *
532933574d68SLin Ming 	 * http://thread.gmane.org/gmane.linux.ide/46764
533033574d68SLin Ming 	 */
533133574d68SLin Ming 	if (mesg.event == PM_EVENT_SUSPEND)
533233574d68SLin Ming 		ehi_flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_NO_RECOVERY;
533333574d68SLin Ming 
533433574d68SLin Ming 	rc = ata_port_request_pm(ap, mesg, 0, ehi_flags, 1);
53355ef41082SLin Ming 	return rc;
53365ef41082SLin Ming }
53375ef41082SLin Ming 
53385ef41082SLin Ming static int ata_port_suspend(struct device *dev)
53395ef41082SLin Ming {
53405ef41082SLin Ming 	if (pm_runtime_suspended(dev))
53415ef41082SLin Ming 		return 0;
53425ef41082SLin Ming 
534333574d68SLin Ming 	return ata_port_suspend_common(dev, PMSG_SUSPEND);
534433574d68SLin Ming }
534533574d68SLin Ming 
534633574d68SLin Ming static int ata_port_do_freeze(struct device *dev)
534733574d68SLin Ming {
534833574d68SLin Ming 	if (pm_runtime_suspended(dev))
534933574d68SLin Ming 		pm_runtime_resume(dev);
535033574d68SLin Ming 
535133574d68SLin Ming 	return ata_port_suspend_common(dev, PMSG_FREEZE);
535233574d68SLin Ming }
535333574d68SLin Ming 
535433574d68SLin Ming static int ata_port_poweroff(struct device *dev)
535533574d68SLin Ming {
535633574d68SLin Ming 	if (pm_runtime_suspended(dev))
535733574d68SLin Ming 		return 0;
535833574d68SLin Ming 
535933574d68SLin Ming 	return ata_port_suspend_common(dev, PMSG_HIBERNATE);
53605ef41082SLin Ming }
53615ef41082SLin Ming 
5362e90b1e5aSLin Ming static int ata_port_resume_common(struct device *dev)
53635ef41082SLin Ming {
53645ef41082SLin Ming 	struct ata_port *ap = to_ata_port(dev);
53655ef41082SLin Ming 	int rc;
53665ef41082SLin Ming 
53675ef41082SLin Ming 	rc = ata_port_request_pm(ap, PMSG_ON, ATA_EH_RESET,
53685ef41082SLin Ming 		ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET, 1);
53695ef41082SLin Ming 	return rc;
53705ef41082SLin Ming }
53715ef41082SLin Ming 
5372e90b1e5aSLin Ming static int ata_port_resume(struct device *dev)
5373e90b1e5aSLin Ming {
5374e90b1e5aSLin Ming 	int rc;
5375e90b1e5aSLin Ming 
5376e90b1e5aSLin Ming 	rc = ata_port_resume_common(dev);
5377e90b1e5aSLin Ming 	if (!rc) {
5378e90b1e5aSLin Ming 		pm_runtime_disable(dev);
5379e90b1e5aSLin Ming 		pm_runtime_set_active(dev);
5380e90b1e5aSLin Ming 		pm_runtime_enable(dev);
5381e90b1e5aSLin Ming 	}
5382e90b1e5aSLin Ming 
5383e90b1e5aSLin Ming 	return rc;
5384e90b1e5aSLin Ming }
5385e90b1e5aSLin Ming 
53869ee4f393SLin Ming static int ata_port_runtime_idle(struct device *dev)
53879ee4f393SLin Ming {
53889ee4f393SLin Ming 	return pm_runtime_suspend(dev);
53899ee4f393SLin Ming }
53909ee4f393SLin Ming 
53915ef41082SLin Ming static const struct dev_pm_ops ata_port_pm_ops = {
53925ef41082SLin Ming 	.suspend = ata_port_suspend,
53935ef41082SLin Ming 	.resume = ata_port_resume,
539433574d68SLin Ming 	.freeze = ata_port_do_freeze,
539533574d68SLin Ming 	.thaw = ata_port_resume,
539633574d68SLin Ming 	.poweroff = ata_port_poweroff,
539733574d68SLin Ming 	.restore = ata_port_resume,
53989ee4f393SLin Ming 
539933574d68SLin Ming 	.runtime_suspend = ata_port_suspend,
5400e90b1e5aSLin Ming 	.runtime_resume = ata_port_resume_common,
54019ee4f393SLin Ming 	.runtime_idle = ata_port_runtime_idle,
54025ef41082SLin Ming };
54035ef41082SLin Ming 
5404c6fd2807SJeff Garzik /**
5405cca3974eSJeff Garzik  *	ata_host_suspend - suspend host
5406cca3974eSJeff Garzik  *	@host: host to suspend
5407c6fd2807SJeff Garzik  *	@mesg: PM message
5408c6fd2807SJeff Garzik  *
54095ef41082SLin Ming  *	Suspend @host.  Actual operation is performed by port suspend.
5410c6fd2807SJeff Garzik  */
5411cca3974eSJeff Garzik int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
5412c6fd2807SJeff Garzik {
541372ad6ec4SJeff Garzik 	host->dev->power.power_state = mesg;
54145ef41082SLin Ming 	return 0;
5415c6fd2807SJeff Garzik }
5416c6fd2807SJeff Garzik 
5417c6fd2807SJeff Garzik /**
5418cca3974eSJeff Garzik  *	ata_host_resume - resume host
5419cca3974eSJeff Garzik  *	@host: host to resume
5420c6fd2807SJeff Garzik  *
54215ef41082SLin Ming  *	Resume @host.  Actual operation is performed by port resume.
5422c6fd2807SJeff Garzik  */
5423cca3974eSJeff Garzik void ata_host_resume(struct ata_host *host)
5424c6fd2807SJeff Garzik {
542572ad6ec4SJeff Garzik 	host->dev->power.power_state = PMSG_ON;
5426c6fd2807SJeff Garzik }
54276ffa01d8STejun Heo #endif
5428c6fd2807SJeff Garzik 
54295ef41082SLin Ming struct device_type ata_port_type = {
54305ef41082SLin Ming 	.name = "ata_port",
54315ef41082SLin Ming #ifdef CONFIG_PM
54325ef41082SLin Ming 	.pm = &ata_port_pm_ops,
54335ef41082SLin Ming #endif
54345ef41082SLin Ming };
54355ef41082SLin Ming 
5436c6fd2807SJeff Garzik /**
5437c6fd2807SJeff Garzik  *	ata_dev_init - Initialize an ata_device structure
5438c6fd2807SJeff Garzik  *	@dev: Device structure to initialize
5439c6fd2807SJeff Garzik  *
5440c6fd2807SJeff Garzik  *	Initialize @dev in preparation for probing.
5441c6fd2807SJeff Garzik  *
5442c6fd2807SJeff Garzik  *	LOCKING:
5443c6fd2807SJeff Garzik  *	Inherited from caller.
5444c6fd2807SJeff Garzik  */
5445c6fd2807SJeff Garzik void ata_dev_init(struct ata_device *dev)
5446c6fd2807SJeff Garzik {
5447b1c72916STejun Heo 	struct ata_link *link = ata_dev_phys_link(dev);
54489af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
5449c6fd2807SJeff Garzik 	unsigned long flags;
5450c6fd2807SJeff Garzik 
5451b1c72916STejun Heo 	/* SATA spd limit is bound to the attached device, reset together */
54529af5c9c9STejun Heo 	link->sata_spd_limit = link->hw_sata_spd_limit;
54539af5c9c9STejun Heo 	link->sata_spd = 0;
5454c6fd2807SJeff Garzik 
5455c6fd2807SJeff Garzik 	/* High bits of dev->flags are used to record warm plug
5456c6fd2807SJeff Garzik 	 * requests which occur asynchronously.  Synchronize using
5457cca3974eSJeff Garzik 	 * host lock.
5458c6fd2807SJeff Garzik 	 */
5459c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
5460c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_INIT_MASK;
54613dcc323fSTejun Heo 	dev->horkage = 0;
5462c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
5463c6fd2807SJeff Garzik 
546499cf610aSTejun Heo 	memset((void *)dev + ATA_DEVICE_CLEAR_BEGIN, 0,
546599cf610aSTejun Heo 	       ATA_DEVICE_CLEAR_END - ATA_DEVICE_CLEAR_BEGIN);
5466c6fd2807SJeff Garzik 	dev->pio_mask = UINT_MAX;
5467c6fd2807SJeff Garzik 	dev->mwdma_mask = UINT_MAX;
5468c6fd2807SJeff Garzik 	dev->udma_mask = UINT_MAX;
5469c6fd2807SJeff Garzik }
5470c6fd2807SJeff Garzik 
5471c6fd2807SJeff Garzik /**
54724fb37a25STejun Heo  *	ata_link_init - Initialize an ata_link structure
54734fb37a25STejun Heo  *	@ap: ATA port link is attached to
54744fb37a25STejun Heo  *	@link: Link structure to initialize
54758989805dSTejun Heo  *	@pmp: Port multiplier port number
54764fb37a25STejun Heo  *
54774fb37a25STejun Heo  *	Initialize @link.
54784fb37a25STejun Heo  *
54794fb37a25STejun Heo  *	LOCKING:
54804fb37a25STejun Heo  *	Kernel thread context (may sleep)
54814fb37a25STejun Heo  */
5482fb7fd614STejun Heo void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
54834fb37a25STejun Heo {
54844fb37a25STejun Heo 	int i;
54854fb37a25STejun Heo 
54864fb37a25STejun Heo 	/* clear everything except for devices */
5487d9027470SGwendal Grignou 	memset((void *)link + ATA_LINK_CLEAR_BEGIN, 0,
5488d9027470SGwendal Grignou 	       ATA_LINK_CLEAR_END - ATA_LINK_CLEAR_BEGIN);
54894fb37a25STejun Heo 
54904fb37a25STejun Heo 	link->ap = ap;
54918989805dSTejun Heo 	link->pmp = pmp;
54924fb37a25STejun Heo 	link->active_tag = ATA_TAG_POISON;
54934fb37a25STejun Heo 	link->hw_sata_spd_limit = UINT_MAX;
54944fb37a25STejun Heo 
54954fb37a25STejun Heo 	/* can't use iterator, ap isn't initialized yet */
54964fb37a25STejun Heo 	for (i = 0; i < ATA_MAX_DEVICES; i++) {
54974fb37a25STejun Heo 		struct ata_device *dev = &link->device[i];
54984fb37a25STejun Heo 
54994fb37a25STejun Heo 		dev->link = link;
55004fb37a25STejun Heo 		dev->devno = dev - link->device;
5501110f66d2STejun Heo #ifdef CONFIG_ATA_ACPI
5502110f66d2STejun Heo 		dev->gtf_filter = ata_acpi_gtf_filter;
5503110f66d2STejun Heo #endif
55044fb37a25STejun Heo 		ata_dev_init(dev);
55054fb37a25STejun Heo 	}
55064fb37a25STejun Heo }
55074fb37a25STejun Heo 
55084fb37a25STejun Heo /**
55094fb37a25STejun Heo  *	sata_link_init_spd - Initialize link->sata_spd_limit
55104fb37a25STejun Heo  *	@link: Link to configure sata_spd_limit for
55114fb37a25STejun Heo  *
55124fb37a25STejun Heo  *	Initialize @link->[hw_]sata_spd_limit to the currently
55134fb37a25STejun Heo  *	configured value.
55144fb37a25STejun Heo  *
55154fb37a25STejun Heo  *	LOCKING:
55164fb37a25STejun Heo  *	Kernel thread context (may sleep).
55174fb37a25STejun Heo  *
55184fb37a25STejun Heo  *	RETURNS:
55194fb37a25STejun Heo  *	0 on success, -errno on failure.
55204fb37a25STejun Heo  */
5521fb7fd614STejun Heo int sata_link_init_spd(struct ata_link *link)
55224fb37a25STejun Heo {
552333267325STejun Heo 	u8 spd;
55244fb37a25STejun Heo 	int rc;
55254fb37a25STejun Heo 
5526d127ea7bSTejun Heo 	rc = sata_scr_read(link, SCR_CONTROL, &link->saved_scontrol);
55274fb37a25STejun Heo 	if (rc)
55284fb37a25STejun Heo 		return rc;
55294fb37a25STejun Heo 
5530d127ea7bSTejun Heo 	spd = (link->saved_scontrol >> 4) & 0xf;
55314fb37a25STejun Heo 	if (spd)
55324fb37a25STejun Heo 		link->hw_sata_spd_limit &= (1 << spd) - 1;
55334fb37a25STejun Heo 
553405944bdfSTejun Heo 	ata_force_link_limits(link);
553533267325STejun Heo 
55364fb37a25STejun Heo 	link->sata_spd_limit = link->hw_sata_spd_limit;
55374fb37a25STejun Heo 
55384fb37a25STejun Heo 	return 0;
55394fb37a25STejun Heo }
55404fb37a25STejun Heo 
55414fb37a25STejun Heo /**
5542f3187195STejun Heo  *	ata_port_alloc - allocate and initialize basic ATA port resources
5543f3187195STejun Heo  *	@host: ATA host this allocated port belongs to
5544c6fd2807SJeff Garzik  *
5545f3187195STejun Heo  *	Allocate and initialize basic ATA port resources.
5546f3187195STejun Heo  *
5547f3187195STejun Heo  *	RETURNS:
5548f3187195STejun Heo  *	Allocate ATA port on success, NULL on failure.
5549c6fd2807SJeff Garzik  *
5550c6fd2807SJeff Garzik  *	LOCKING:
5551f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5552c6fd2807SJeff Garzik  */
5553f3187195STejun Heo struct ata_port *ata_port_alloc(struct ata_host *host)
5554c6fd2807SJeff Garzik {
5555f3187195STejun Heo 	struct ata_port *ap;
5556c6fd2807SJeff Garzik 
5557f3187195STejun Heo 	DPRINTK("ENTER\n");
5558f3187195STejun Heo 
5559f3187195STejun Heo 	ap = kzalloc(sizeof(*ap), GFP_KERNEL);
5560f3187195STejun Heo 	if (!ap)
5561f3187195STejun Heo 		return NULL;
5562f3187195STejun Heo 
55637b3a24c5SMaxime Bizon 	ap->pflags |= ATA_PFLAG_INITIALIZING | ATA_PFLAG_FROZEN;
5564cca3974eSJeff Garzik 	ap->lock = &host->lock;
5565f3187195STejun Heo 	ap->print_id = -1;
5566cca3974eSJeff Garzik 	ap->host = host;
5567f3187195STejun Heo 	ap->dev = host->dev;
5568c6fd2807SJeff Garzik 
5569c6fd2807SJeff Garzik #if defined(ATA_VERBOSE_DEBUG)
5570c6fd2807SJeff Garzik 	/* turn on all debugging levels */
5571c6fd2807SJeff Garzik 	ap->msg_enable = 0x00FF;
5572c6fd2807SJeff Garzik #elif defined(ATA_DEBUG)
5573c6fd2807SJeff Garzik 	ap->msg_enable = ATA_MSG_DRV | ATA_MSG_INFO | ATA_MSG_CTL | ATA_MSG_WARN | ATA_MSG_ERR;
5574c6fd2807SJeff Garzik #else
5575c6fd2807SJeff Garzik 	ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
5576c6fd2807SJeff Garzik #endif
5577c6fd2807SJeff Garzik 
5578ad72cf98STejun Heo 	mutex_init(&ap->scsi_scan_mutex);
557965f27f38SDavid Howells 	INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
558065f27f38SDavid Howells 	INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
5581c6fd2807SJeff Garzik 	INIT_LIST_HEAD(&ap->eh_done_q);
5582c6fd2807SJeff Garzik 	init_waitqueue_head(&ap->eh_wait_q);
558345fabbb7SElias Oltmanns 	init_completion(&ap->park_req_pending);
55845ddf24c5STejun Heo 	init_timer_deferrable(&ap->fastdrain_timer);
55855ddf24c5STejun Heo 	ap->fastdrain_timer.function = ata_eh_fastdrain_timerfn;
55865ddf24c5STejun Heo 	ap->fastdrain_timer.data = (unsigned long)ap;
5587c6fd2807SJeff Garzik 
5588c6fd2807SJeff Garzik 	ap->cbl = ATA_CBL_NONE;
5589c6fd2807SJeff Garzik 
55908989805dSTejun Heo 	ata_link_init(ap, &ap->link, 0);
5591c6fd2807SJeff Garzik 
5592c6fd2807SJeff Garzik #ifdef ATA_IRQ_TRAP
5593c6fd2807SJeff Garzik 	ap->stats.unhandled_irq = 1;
5594c6fd2807SJeff Garzik 	ap->stats.idle_irq = 1;
5595c6fd2807SJeff Garzik #endif
5596270390e1STejun Heo 	ata_sff_port_init(ap);
5597270390e1STejun Heo 
5598c6fd2807SJeff Garzik 	return ap;
5599c6fd2807SJeff Garzik }
5600c6fd2807SJeff Garzik 
5601f0d36efdSTejun Heo static void ata_host_release(struct device *gendev, void *res)
5602f0d36efdSTejun Heo {
5603f0d36efdSTejun Heo 	struct ata_host *host = dev_get_drvdata(gendev);
5604f0d36efdSTejun Heo 	int i;
5605f0d36efdSTejun Heo 
5606f0d36efdSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
5607f0d36efdSTejun Heo 		struct ata_port *ap = host->ports[i];
5608f0d36efdSTejun Heo 
5609ecef7253STejun Heo 		if (!ap)
5610ecef7253STejun Heo 			continue;
5611ecef7253STejun Heo 
56124911487aSTejun Heo 		if (ap->scsi_host)
56131aa506e4STejun Heo 			scsi_host_put(ap->scsi_host);
56141aa506e4STejun Heo 
5615633273a3STejun Heo 		kfree(ap->pmp_link);
5616b1c72916STejun Heo 		kfree(ap->slave_link);
56174911487aSTejun Heo 		kfree(ap);
56181aa506e4STejun Heo 		host->ports[i] = NULL;
56191aa506e4STejun Heo 	}
56201aa506e4STejun Heo 
56211aa56ccaSTejun Heo 	dev_set_drvdata(gendev, NULL);
5622f0d36efdSTejun Heo }
5623f0d36efdSTejun Heo 
5624c6fd2807SJeff Garzik /**
5625f3187195STejun Heo  *	ata_host_alloc - allocate and init basic ATA host resources
5626f3187195STejun Heo  *	@dev: generic device this host is associated with
5627f3187195STejun Heo  *	@max_ports: maximum number of ATA ports associated with this host
5628f3187195STejun Heo  *
5629f3187195STejun Heo  *	Allocate and initialize basic ATA host resources.  LLD calls
5630f3187195STejun Heo  *	this function to allocate a host, initializes it fully and
5631f3187195STejun Heo  *	attaches it using ata_host_register().
5632f3187195STejun Heo  *
5633f3187195STejun Heo  *	@max_ports ports are allocated and host->n_ports is
5634f3187195STejun Heo  *	initialized to @max_ports.  The caller is allowed to decrease
5635f3187195STejun Heo  *	host->n_ports before calling ata_host_register().  The unused
5636f3187195STejun Heo  *	ports will be automatically freed on registration.
5637f3187195STejun Heo  *
5638f3187195STejun Heo  *	RETURNS:
5639f3187195STejun Heo  *	Allocate ATA host on success, NULL on failure.
5640f3187195STejun Heo  *
5641f3187195STejun Heo  *	LOCKING:
5642f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5643f3187195STejun Heo  */
5644f3187195STejun Heo struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
5645f3187195STejun Heo {
5646f3187195STejun Heo 	struct ata_host *host;
5647f3187195STejun Heo 	size_t sz;
5648f3187195STejun Heo 	int i;
5649f3187195STejun Heo 
5650f3187195STejun Heo 	DPRINTK("ENTER\n");
5651f3187195STejun Heo 
5652f3187195STejun Heo 	if (!devres_open_group(dev, NULL, GFP_KERNEL))
5653f3187195STejun Heo 		return NULL;
5654f3187195STejun Heo 
5655f3187195STejun Heo 	/* alloc a container for our list of ATA ports (buses) */
5656f3187195STejun Heo 	sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
5657f3187195STejun Heo 	/* alloc a container for our list of ATA ports (buses) */
5658f3187195STejun Heo 	host = devres_alloc(ata_host_release, sz, GFP_KERNEL);
5659f3187195STejun Heo 	if (!host)
5660f3187195STejun Heo 		goto err_out;
5661f3187195STejun Heo 
5662f3187195STejun Heo 	devres_add(dev, host);
5663f3187195STejun Heo 	dev_set_drvdata(dev, host);
5664f3187195STejun Heo 
5665f3187195STejun Heo 	spin_lock_init(&host->lock);
5666c0c362b6STejun Heo 	mutex_init(&host->eh_mutex);
5667f3187195STejun Heo 	host->dev = dev;
5668f3187195STejun Heo 	host->n_ports = max_ports;
5669f3187195STejun Heo 
5670f3187195STejun Heo 	/* allocate ports bound to this host */
5671f3187195STejun Heo 	for (i = 0; i < max_ports; i++) {
5672f3187195STejun Heo 		struct ata_port *ap;
5673f3187195STejun Heo 
5674f3187195STejun Heo 		ap = ata_port_alloc(host);
5675f3187195STejun Heo 		if (!ap)
5676f3187195STejun Heo 			goto err_out;
5677f3187195STejun Heo 
5678f3187195STejun Heo 		ap->port_no = i;
5679f3187195STejun Heo 		host->ports[i] = ap;
5680f3187195STejun Heo 	}
5681f3187195STejun Heo 
5682f3187195STejun Heo 	devres_remove_group(dev, NULL);
5683f3187195STejun Heo 	return host;
5684f3187195STejun Heo 
5685f3187195STejun Heo  err_out:
5686f3187195STejun Heo 	devres_release_group(dev, NULL);
5687f3187195STejun Heo 	return NULL;
5688f3187195STejun Heo }
5689f3187195STejun Heo 
5690f3187195STejun Heo /**
5691f5cda257STejun Heo  *	ata_host_alloc_pinfo - alloc host and init with port_info array
5692f5cda257STejun Heo  *	@dev: generic device this host is associated with
5693f5cda257STejun Heo  *	@ppi: array of ATA port_info to initialize host with
5694f5cda257STejun Heo  *	@n_ports: number of ATA ports attached to this host
5695f5cda257STejun Heo  *
5696f5cda257STejun Heo  *	Allocate ATA host and initialize with info from @ppi.  If NULL
5697f5cda257STejun Heo  *	terminated, @ppi may contain fewer entries than @n_ports.  The
5698f5cda257STejun Heo  *	last entry will be used for the remaining ports.
5699f5cda257STejun Heo  *
5700f5cda257STejun Heo  *	RETURNS:
5701f5cda257STejun Heo  *	Allocate ATA host on success, NULL on failure.
5702f5cda257STejun Heo  *
5703f5cda257STejun Heo  *	LOCKING:
5704f5cda257STejun Heo  *	Inherited from calling layer (may sleep).
5705f5cda257STejun Heo  */
5706f5cda257STejun Heo struct ata_host *ata_host_alloc_pinfo(struct device *dev,
5707f5cda257STejun Heo 				      const struct ata_port_info * const * ppi,
5708f5cda257STejun Heo 				      int n_ports)
5709f5cda257STejun Heo {
5710f5cda257STejun Heo 	const struct ata_port_info *pi;
5711f5cda257STejun Heo 	struct ata_host *host;
5712f5cda257STejun Heo 	int i, j;
5713f5cda257STejun Heo 
5714f5cda257STejun Heo 	host = ata_host_alloc(dev, n_ports);
5715f5cda257STejun Heo 	if (!host)
5716f5cda257STejun Heo 		return NULL;
5717f5cda257STejun Heo 
5718f5cda257STejun Heo 	for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
5719f5cda257STejun Heo 		struct ata_port *ap = host->ports[i];
5720f5cda257STejun Heo 
5721f5cda257STejun Heo 		if (ppi[j])
5722f5cda257STejun Heo 			pi = ppi[j++];
5723f5cda257STejun Heo 
5724f5cda257STejun Heo 		ap->pio_mask = pi->pio_mask;
5725f5cda257STejun Heo 		ap->mwdma_mask = pi->mwdma_mask;
5726f5cda257STejun Heo 		ap->udma_mask = pi->udma_mask;
5727f5cda257STejun Heo 		ap->flags |= pi->flags;
57280c88758bSTejun Heo 		ap->link.flags |= pi->link_flags;
5729f5cda257STejun Heo 		ap->ops = pi->port_ops;
5730f5cda257STejun Heo 
5731f5cda257STejun Heo 		if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
5732f5cda257STejun Heo 			host->ops = pi->port_ops;
5733f5cda257STejun Heo 	}
5734f5cda257STejun Heo 
5735f5cda257STejun Heo 	return host;
5736f5cda257STejun Heo }
5737f5cda257STejun Heo 
5738b1c72916STejun Heo /**
5739b1c72916STejun Heo  *	ata_slave_link_init - initialize slave link
5740b1c72916STejun Heo  *	@ap: port to initialize slave link for
5741b1c72916STejun Heo  *
5742b1c72916STejun Heo  *	Create and initialize slave link for @ap.  This enables slave
5743b1c72916STejun Heo  *	link handling on the port.
5744b1c72916STejun Heo  *
5745b1c72916STejun Heo  *	In libata, a port contains links and a link contains devices.
5746b1c72916STejun Heo  *	There is single host link but if a PMP is attached to it,
5747b1c72916STejun Heo  *	there can be multiple fan-out links.  On SATA, there's usually
5748b1c72916STejun Heo  *	a single device connected to a link but PATA and SATA
5749b1c72916STejun Heo  *	controllers emulating TF based interface can have two - master
5750b1c72916STejun Heo  *	and slave.
5751b1c72916STejun Heo  *
5752b1c72916STejun Heo  *	However, there are a few controllers which don't fit into this
5753b1c72916STejun Heo  *	abstraction too well - SATA controllers which emulate TF
5754b1c72916STejun Heo  *	interface with both master and slave devices but also have
5755b1c72916STejun Heo  *	separate SCR register sets for each device.  These controllers
5756b1c72916STejun Heo  *	need separate links for physical link handling
5757b1c72916STejun Heo  *	(e.g. onlineness, link speed) but should be treated like a
5758b1c72916STejun Heo  *	traditional M/S controller for everything else (e.g. command
5759b1c72916STejun Heo  *	issue, softreset).
5760b1c72916STejun Heo  *
5761b1c72916STejun Heo  *	slave_link is libata's way of handling this class of
5762b1c72916STejun Heo  *	controllers without impacting core layer too much.  For
5763b1c72916STejun Heo  *	anything other than physical link handling, the default host
5764b1c72916STejun Heo  *	link is used for both master and slave.  For physical link
5765b1c72916STejun Heo  *	handling, separate @ap->slave_link is used.  All dirty details
5766b1c72916STejun Heo  *	are implemented inside libata core layer.  From LLD's POV, the
5767b1c72916STejun Heo  *	only difference is that prereset, hardreset and postreset are
5768b1c72916STejun Heo  *	called once more for the slave link, so the reset sequence
5769b1c72916STejun Heo  *	looks like the following.
5770b1c72916STejun Heo  *
5771b1c72916STejun Heo  *	prereset(M) -> prereset(S) -> hardreset(M) -> hardreset(S) ->
5772b1c72916STejun Heo  *	softreset(M) -> postreset(M) -> postreset(S)
5773b1c72916STejun Heo  *
5774b1c72916STejun Heo  *	Note that softreset is called only for the master.  Softreset
5775b1c72916STejun Heo  *	resets both M/S by definition, so SRST on master should handle
5776b1c72916STejun Heo  *	both (the standard method will work just fine).
5777b1c72916STejun Heo  *
5778b1c72916STejun Heo  *	LOCKING:
5779b1c72916STejun Heo  *	Should be called before host is registered.
5780b1c72916STejun Heo  *
5781b1c72916STejun Heo  *	RETURNS:
5782b1c72916STejun Heo  *	0 on success, -errno on failure.
5783b1c72916STejun Heo  */
5784b1c72916STejun Heo int ata_slave_link_init(struct ata_port *ap)
5785b1c72916STejun Heo {
5786b1c72916STejun Heo 	struct ata_link *link;
5787b1c72916STejun Heo 
5788b1c72916STejun Heo 	WARN_ON(ap->slave_link);
5789b1c72916STejun Heo 	WARN_ON(ap->flags & ATA_FLAG_PMP);
5790b1c72916STejun Heo 
5791b1c72916STejun Heo 	link = kzalloc(sizeof(*link), GFP_KERNEL);
5792b1c72916STejun Heo 	if (!link)
5793b1c72916STejun Heo 		return -ENOMEM;
5794b1c72916STejun Heo 
5795b1c72916STejun Heo 	ata_link_init(ap, link, 1);
5796b1c72916STejun Heo 	ap->slave_link = link;
5797b1c72916STejun Heo 	return 0;
5798b1c72916STejun Heo }
5799b1c72916STejun Heo 
580032ebbc0cSTejun Heo static void ata_host_stop(struct device *gendev, void *res)
580132ebbc0cSTejun Heo {
580232ebbc0cSTejun Heo 	struct ata_host *host = dev_get_drvdata(gendev);
580332ebbc0cSTejun Heo 	int i;
580432ebbc0cSTejun Heo 
580532ebbc0cSTejun Heo 	WARN_ON(!(host->flags & ATA_HOST_STARTED));
580632ebbc0cSTejun Heo 
580732ebbc0cSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
580832ebbc0cSTejun Heo 		struct ata_port *ap = host->ports[i];
580932ebbc0cSTejun Heo 
581032ebbc0cSTejun Heo 		if (ap->ops->port_stop)
581132ebbc0cSTejun Heo 			ap->ops->port_stop(ap);
581232ebbc0cSTejun Heo 	}
581332ebbc0cSTejun Heo 
581432ebbc0cSTejun Heo 	if (host->ops->host_stop)
581532ebbc0cSTejun Heo 		host->ops->host_stop(host);
581632ebbc0cSTejun Heo }
581732ebbc0cSTejun Heo 
5818f5cda257STejun Heo /**
5819029cfd6bSTejun Heo  *	ata_finalize_port_ops - finalize ata_port_operations
5820029cfd6bSTejun Heo  *	@ops: ata_port_operations to finalize
5821029cfd6bSTejun Heo  *
5822029cfd6bSTejun Heo  *	An ata_port_operations can inherit from another ops and that
5823029cfd6bSTejun Heo  *	ops can again inherit from another.  This can go on as many
5824029cfd6bSTejun Heo  *	times as necessary as long as there is no loop in the
5825029cfd6bSTejun Heo  *	inheritance chain.
5826029cfd6bSTejun Heo  *
5827029cfd6bSTejun Heo  *	Ops tables are finalized when the host is started.  NULL or
5828029cfd6bSTejun Heo  *	unspecified entries are inherited from the closet ancestor
5829029cfd6bSTejun Heo  *	which has the method and the entry is populated with it.
5830029cfd6bSTejun Heo  *	After finalization, the ops table directly points to all the
5831029cfd6bSTejun Heo  *	methods and ->inherits is no longer necessary and cleared.
5832029cfd6bSTejun Heo  *
5833029cfd6bSTejun Heo  *	Using ATA_OP_NULL, inheriting ops can force a method to NULL.
5834029cfd6bSTejun Heo  *
5835029cfd6bSTejun Heo  *	LOCKING:
5836029cfd6bSTejun Heo  *	None.
5837029cfd6bSTejun Heo  */
5838029cfd6bSTejun Heo static void ata_finalize_port_ops(struct ata_port_operations *ops)
5839029cfd6bSTejun Heo {
58402da67659SPradeep Singh Rautela 	static DEFINE_SPINLOCK(lock);
5841029cfd6bSTejun Heo 	const struct ata_port_operations *cur;
5842029cfd6bSTejun Heo 	void **begin = (void **)ops;
5843029cfd6bSTejun Heo 	void **end = (void **)&ops->inherits;
5844029cfd6bSTejun Heo 	void **pp;
5845029cfd6bSTejun Heo 
5846029cfd6bSTejun Heo 	if (!ops || !ops->inherits)
5847029cfd6bSTejun Heo 		return;
5848029cfd6bSTejun Heo 
5849029cfd6bSTejun Heo 	spin_lock(&lock);
5850029cfd6bSTejun Heo 
5851029cfd6bSTejun Heo 	for (cur = ops->inherits; cur; cur = cur->inherits) {
5852029cfd6bSTejun Heo 		void **inherit = (void **)cur;
5853029cfd6bSTejun Heo 
5854029cfd6bSTejun Heo 		for (pp = begin; pp < end; pp++, inherit++)
5855029cfd6bSTejun Heo 			if (!*pp)
5856029cfd6bSTejun Heo 				*pp = *inherit;
5857029cfd6bSTejun Heo 	}
5858029cfd6bSTejun Heo 
5859029cfd6bSTejun Heo 	for (pp = begin; pp < end; pp++)
5860029cfd6bSTejun Heo 		if (IS_ERR(*pp))
5861029cfd6bSTejun Heo 			*pp = NULL;
5862029cfd6bSTejun Heo 
5863029cfd6bSTejun Heo 	ops->inherits = NULL;
5864029cfd6bSTejun Heo 
5865029cfd6bSTejun Heo 	spin_unlock(&lock);
5866029cfd6bSTejun Heo }
5867029cfd6bSTejun Heo 
5868029cfd6bSTejun Heo /**
5869ecef7253STejun Heo  *	ata_host_start - start and freeze ports of an ATA host
5870ecef7253STejun Heo  *	@host: ATA host to start ports for
5871ecef7253STejun Heo  *
5872ecef7253STejun Heo  *	Start and then freeze ports of @host.  Started status is
5873ecef7253STejun Heo  *	recorded in host->flags, so this function can be called
5874ecef7253STejun Heo  *	multiple times.  Ports are guaranteed to get started only
5875f3187195STejun Heo  *	once.  If host->ops isn't initialized yet, its set to the
5876f3187195STejun Heo  *	first non-dummy port ops.
5877ecef7253STejun Heo  *
5878ecef7253STejun Heo  *	LOCKING:
5879ecef7253STejun Heo  *	Inherited from calling layer (may sleep).
5880ecef7253STejun Heo  *
5881ecef7253STejun Heo  *	RETURNS:
5882ecef7253STejun Heo  *	0 if all ports are started successfully, -errno otherwise.
5883ecef7253STejun Heo  */
5884ecef7253STejun Heo int ata_host_start(struct ata_host *host)
5885ecef7253STejun Heo {
588632ebbc0cSTejun Heo 	int have_stop = 0;
588732ebbc0cSTejun Heo 	void *start_dr = NULL;
5888ecef7253STejun Heo 	int i, rc;
5889ecef7253STejun Heo 
5890ecef7253STejun Heo 	if (host->flags & ATA_HOST_STARTED)
5891ecef7253STejun Heo 		return 0;
5892ecef7253STejun Heo 
5893029cfd6bSTejun Heo 	ata_finalize_port_ops(host->ops);
5894029cfd6bSTejun Heo 
5895ecef7253STejun Heo 	for (i = 0; i < host->n_ports; i++) {
5896ecef7253STejun Heo 		struct ata_port *ap = host->ports[i];
5897ecef7253STejun Heo 
5898029cfd6bSTejun Heo 		ata_finalize_port_ops(ap->ops);
5899029cfd6bSTejun Heo 
5900f3187195STejun Heo 		if (!host->ops && !ata_port_is_dummy(ap))
5901f3187195STejun Heo 			host->ops = ap->ops;
5902f3187195STejun Heo 
590332ebbc0cSTejun Heo 		if (ap->ops->port_stop)
590432ebbc0cSTejun Heo 			have_stop = 1;
590532ebbc0cSTejun Heo 	}
590632ebbc0cSTejun Heo 
590732ebbc0cSTejun Heo 	if (host->ops->host_stop)
590832ebbc0cSTejun Heo 		have_stop = 1;
590932ebbc0cSTejun Heo 
591032ebbc0cSTejun Heo 	if (have_stop) {
591132ebbc0cSTejun Heo 		start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
591232ebbc0cSTejun Heo 		if (!start_dr)
591332ebbc0cSTejun Heo 			return -ENOMEM;
591432ebbc0cSTejun Heo 	}
591532ebbc0cSTejun Heo 
591632ebbc0cSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
591732ebbc0cSTejun Heo 		struct ata_port *ap = host->ports[i];
591832ebbc0cSTejun Heo 
5919ecef7253STejun Heo 		if (ap->ops->port_start) {
5920ecef7253STejun Heo 			rc = ap->ops->port_start(ap);
5921ecef7253STejun Heo 			if (rc) {
59220f9fe9b7SAlan Cox 				if (rc != -ENODEV)
5923a44fec1fSJoe Perches 					dev_err(host->dev,
5924a44fec1fSJoe Perches 						"failed to start port %d (errno=%d)\n",
5925a44fec1fSJoe Perches 						i, rc);
5926ecef7253STejun Heo 				goto err_out;
5927ecef7253STejun Heo 			}
5928ecef7253STejun Heo 		}
5929ecef7253STejun Heo 		ata_eh_freeze_port(ap);
5930ecef7253STejun Heo 	}
5931ecef7253STejun Heo 
593232ebbc0cSTejun Heo 	if (start_dr)
593332ebbc0cSTejun Heo 		devres_add(host->dev, start_dr);
5934ecef7253STejun Heo 	host->flags |= ATA_HOST_STARTED;
5935ecef7253STejun Heo 	return 0;
5936ecef7253STejun Heo 
5937ecef7253STejun Heo  err_out:
5938ecef7253STejun Heo 	while (--i >= 0) {
5939ecef7253STejun Heo 		struct ata_port *ap = host->ports[i];
5940ecef7253STejun Heo 
5941ecef7253STejun Heo 		if (ap->ops->port_stop)
5942ecef7253STejun Heo 			ap->ops->port_stop(ap);
5943ecef7253STejun Heo 	}
594432ebbc0cSTejun Heo 	devres_free(start_dr);
5945ecef7253STejun Heo 	return rc;
5946ecef7253STejun Heo }
5947ecef7253STejun Heo 
5948ecef7253STejun Heo /**
5949cca3974eSJeff Garzik  *	ata_sas_host_init - Initialize a host struct
5950cca3974eSJeff Garzik  *	@host:	host to initialize
5951cca3974eSJeff Garzik  *	@dev:	device host is attached to
5952cca3974eSJeff Garzik  *	@flags:	host flags
5953c6fd2807SJeff Garzik  *	@ops:	port_ops
5954c6fd2807SJeff Garzik  *
5955c6fd2807SJeff Garzik  *	LOCKING:
5956c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
5957c6fd2807SJeff Garzik  *
5958c6fd2807SJeff Garzik  */
5959f3187195STejun Heo /* KILLME - the only user left is ipr */
5960cca3974eSJeff Garzik void ata_host_init(struct ata_host *host, struct device *dev,
5961029cfd6bSTejun Heo 		   unsigned long flags, struct ata_port_operations *ops)
5962c6fd2807SJeff Garzik {
5963cca3974eSJeff Garzik 	spin_lock_init(&host->lock);
5964c0c362b6STejun Heo 	mutex_init(&host->eh_mutex);
5965cca3974eSJeff Garzik 	host->dev = dev;
5966cca3974eSJeff Garzik 	host->flags = flags;
5967cca3974eSJeff Garzik 	host->ops = ops;
5968c6fd2807SJeff Garzik }
5969c6fd2807SJeff Garzik 
59709508a66fSDan Williams void __ata_port_probe(struct ata_port *ap)
597179318057SArjan van de Ven {
597279318057SArjan van de Ven 	struct ata_eh_info *ehi = &ap->link.eh_info;
597379318057SArjan van de Ven 	unsigned long flags;
597479318057SArjan van de Ven 
597579318057SArjan van de Ven 	/* kick EH for boot probing */
597679318057SArjan van de Ven 	spin_lock_irqsave(ap->lock, flags);
597779318057SArjan van de Ven 
597879318057SArjan van de Ven 	ehi->probe_mask |= ATA_ALL_DEVICES;
59796b7ae954STejun Heo 	ehi->action |= ATA_EH_RESET;
598079318057SArjan van de Ven 	ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
598179318057SArjan van de Ven 
598279318057SArjan van de Ven 	ap->pflags &= ~ATA_PFLAG_INITIALIZING;
598379318057SArjan van de Ven 	ap->pflags |= ATA_PFLAG_LOADING;
598479318057SArjan van de Ven 	ata_port_schedule_eh(ap);
598579318057SArjan van de Ven 
598679318057SArjan van de Ven 	spin_unlock_irqrestore(ap->lock, flags);
59879508a66fSDan Williams }
598879318057SArjan van de Ven 
59899508a66fSDan Williams int ata_port_probe(struct ata_port *ap)
59909508a66fSDan Williams {
59919508a66fSDan Williams 	int rc = 0;
59929508a66fSDan Williams 
59939508a66fSDan Williams 	if (ap->ops->error_handler) {
59949508a66fSDan Williams 		__ata_port_probe(ap);
599579318057SArjan van de Ven 		ata_port_wait_eh(ap);
599679318057SArjan van de Ven 	} else {
599779318057SArjan van de Ven 		DPRINTK("ata%u: bus probe begin\n", ap->print_id);
599879318057SArjan van de Ven 		rc = ata_bus_probe(ap);
599979318057SArjan van de Ven 		DPRINTK("ata%u: bus probe end\n", ap->print_id);
6000238c9cf9SJames Bottomley 	}
6001238c9cf9SJames Bottomley 	return rc;
6002238c9cf9SJames Bottomley }
600379318057SArjan van de Ven 
6004238c9cf9SJames Bottomley 
6005238c9cf9SJames Bottomley static void async_port_probe(void *data, async_cookie_t cookie)
6006238c9cf9SJames Bottomley {
6007238c9cf9SJames Bottomley 	struct ata_port *ap = data;
6008238c9cf9SJames Bottomley 
6009238c9cf9SJames Bottomley 	/*
6010238c9cf9SJames Bottomley 	 * If we're not allowed to scan this host in parallel,
6011238c9cf9SJames Bottomley 	 * we need to wait until all previous scans have completed
6012238c9cf9SJames Bottomley 	 * before going further.
6013238c9cf9SJames Bottomley 	 * Jeff Garzik says this is only within a controller, so we
6014238c9cf9SJames Bottomley 	 * don't need to wait for port 0, only for later ports.
601579318057SArjan van de Ven 	 */
6016238c9cf9SJames Bottomley 	if (!(ap->host->flags & ATA_HOST_PARALLEL_SCAN) && ap->port_no != 0)
6017238c9cf9SJames Bottomley 		async_synchronize_cookie(cookie);
6018238c9cf9SJames Bottomley 
6019238c9cf9SJames Bottomley 	(void)ata_port_probe(ap);
6020f29d3b23SArjan van de Ven 
6021f29d3b23SArjan van de Ven 	/* in order to keep device order, we need to synchronize at this point */
6022f29d3b23SArjan van de Ven 	async_synchronize_cookie(cookie);
6023f29d3b23SArjan van de Ven 
6024f29d3b23SArjan van de Ven 	ata_scsi_scan_host(ap, 1);
602579318057SArjan van de Ven }
6026238c9cf9SJames Bottomley 
6027c6fd2807SJeff Garzik /**
6028f3187195STejun Heo  *	ata_host_register - register initialized ATA host
6029f3187195STejun Heo  *	@host: ATA host to register
6030f3187195STejun Heo  *	@sht: template for SCSI host
6031c6fd2807SJeff Garzik  *
6032f3187195STejun Heo  *	Register initialized ATA host.  @host is allocated using
6033f3187195STejun Heo  *	ata_host_alloc() and fully initialized by LLD.  This function
6034f3187195STejun Heo  *	starts ports, registers @host with ATA and SCSI layers and
6035f3187195STejun Heo  *	probe registered devices.
6036c6fd2807SJeff Garzik  *
6037c6fd2807SJeff Garzik  *	LOCKING:
6038f3187195STejun Heo  *	Inherited from calling layer (may sleep).
6039c6fd2807SJeff Garzik  *
6040c6fd2807SJeff Garzik  *	RETURNS:
6041f3187195STejun Heo  *	0 on success, -errno otherwise.
6042c6fd2807SJeff Garzik  */
6043f3187195STejun Heo int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
6044c6fd2807SJeff Garzik {
6045f3187195STejun Heo 	int i, rc;
6046c6fd2807SJeff Garzik 
6047f3187195STejun Heo 	/* host must have been started */
6048f3187195STejun Heo 	if (!(host->flags & ATA_HOST_STARTED)) {
6049a44fec1fSJoe Perches 		dev_err(host->dev, "BUG: trying to register unstarted host\n");
6050f3187195STejun Heo 		WARN_ON(1);
6051f3187195STejun Heo 		return -EINVAL;
605202f076aaSAlan Cox 	}
6053f0d36efdSTejun Heo 
6054f3187195STejun Heo 	/* Blow away unused ports.  This happens when LLD can't
6055f3187195STejun Heo 	 * determine the exact number of ports to allocate at
6056f3187195STejun Heo 	 * allocation time.
6057f3187195STejun Heo 	 */
6058f3187195STejun Heo 	for (i = host->n_ports; host->ports[i]; i++)
6059f3187195STejun Heo 		kfree(host->ports[i]);
6060f0d36efdSTejun Heo 
6061f3187195STejun Heo 	/* give ports names and add SCSI hosts */
6062f3187195STejun Heo 	for (i = 0; i < host->n_ports; i++)
606385d6725bSDan Williams 		host->ports[i]->print_id = atomic_inc_return(&ata_print_id);
6064c6fd2807SJeff Garzik 
6065d9027470SGwendal Grignou 
6066d9027470SGwendal Grignou 	/* Create associated sysfs transport objects  */
6067d9027470SGwendal Grignou 	for (i = 0; i < host->n_ports; i++) {
6068d9027470SGwendal Grignou 		rc = ata_tport_add(host->dev,host->ports[i]);
6069d9027470SGwendal Grignou 		if (rc) {
6070d9027470SGwendal Grignou 			goto err_tadd;
6071d9027470SGwendal Grignou 		}
6072d9027470SGwendal Grignou 	}
6073d9027470SGwendal Grignou 
6074f3187195STejun Heo 	rc = ata_scsi_add_hosts(host, sht);
6075ecef7253STejun Heo 	if (rc)
6076d9027470SGwendal Grignou 		goto err_tadd;
6077ecef7253STejun Heo 
6078f3187195STejun Heo 	/* set cable, sata_spd_limit and report */
6079cca3974eSJeff Garzik 	for (i = 0; i < host->n_ports; i++) {
6080cca3974eSJeff Garzik 		struct ata_port *ap = host->ports[i];
6081f3187195STejun Heo 		unsigned long xfer_mask;
6082f3187195STejun Heo 
6083f3187195STejun Heo 		/* set SATA cable type if still unset */
6084f3187195STejun Heo 		if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
6085f3187195STejun Heo 			ap->cbl = ATA_CBL_SATA;
6086c6fd2807SJeff Garzik 
6087c6fd2807SJeff Garzik 		/* init sata_spd_limit to the current value */
60884fb37a25STejun Heo 		sata_link_init_spd(&ap->link);
6089b1c72916STejun Heo 		if (ap->slave_link)
6090b1c72916STejun Heo 			sata_link_init_spd(ap->slave_link);
6091c6fd2807SJeff Garzik 
6092cbcdd875STejun Heo 		/* print per-port info to dmesg */
6093f3187195STejun Heo 		xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
6094f3187195STejun Heo 					      ap->udma_mask);
6095f3187195STejun Heo 
6096abf6e8edSTejun Heo 		if (!ata_port_is_dummy(ap)) {
6097a9a79dfeSJoe Perches 			ata_port_info(ap, "%cATA max %s %s\n",
6098a16abc0bSTejun Heo 				      (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
6099f3187195STejun Heo 				      ata_mode_string(xfer_mask),
6100cbcdd875STejun Heo 				      ap->link.eh_info.desc);
6101abf6e8edSTejun Heo 			ata_ehi_clear_desc(&ap->link.eh_info);
6102abf6e8edSTejun Heo 		} else
6103a9a79dfeSJoe Perches 			ata_port_info(ap, "DUMMY\n");
6104c6fd2807SJeff Garzik 	}
6105c6fd2807SJeff Garzik 
6106f6005354SVegard Nossum 	/* perform each probe asynchronously */
6107f3187195STejun Heo 	for (i = 0; i < host->n_ports; i++) {
6108f3187195STejun Heo 		struct ata_port *ap = host->ports[i];
610979318057SArjan van de Ven 		async_schedule(async_port_probe, ap);
6110c6fd2807SJeff Garzik 	}
6111c6fd2807SJeff Garzik 
6112f3187195STejun Heo 	return 0;
6113d9027470SGwendal Grignou 
6114d9027470SGwendal Grignou  err_tadd:
6115d9027470SGwendal Grignou 	while (--i >= 0) {
6116d9027470SGwendal Grignou 		ata_tport_delete(host->ports[i]);
6117d9027470SGwendal Grignou 	}
6118d9027470SGwendal Grignou 	return rc;
6119d9027470SGwendal Grignou 
6120f3187195STejun Heo }
6121f3187195STejun Heo 
6122f3187195STejun Heo /**
6123f5cda257STejun Heo  *	ata_host_activate - start host, request IRQ and register it
6124f5cda257STejun Heo  *	@host: target ATA host
6125f5cda257STejun Heo  *	@irq: IRQ to request
6126f5cda257STejun Heo  *	@irq_handler: irq_handler used when requesting IRQ
6127f5cda257STejun Heo  *	@irq_flags: irq_flags used when requesting IRQ
6128f5cda257STejun Heo  *	@sht: scsi_host_template to use when registering the host
6129f5cda257STejun Heo  *
6130f5cda257STejun Heo  *	After allocating an ATA host and initializing it, most libata
6131f5cda257STejun Heo  *	LLDs perform three steps to activate the host - start host,
6132f5cda257STejun Heo  *	request IRQ and register it.  This helper takes necessasry
6133f5cda257STejun Heo  *	arguments and performs the three steps in one go.
6134f5cda257STejun Heo  *
61353d46b2e2SPaul Mundt  *	An invalid IRQ skips the IRQ registration and expects the host to
61363d46b2e2SPaul Mundt  *	have set polling mode on the port. In this case, @irq_handler
61373d46b2e2SPaul Mundt  *	should be NULL.
61383d46b2e2SPaul Mundt  *
6139f5cda257STejun Heo  *	LOCKING:
6140f5cda257STejun Heo  *	Inherited from calling layer (may sleep).
6141f5cda257STejun Heo  *
6142f5cda257STejun Heo  *	RETURNS:
6143f5cda257STejun Heo  *	0 on success, -errno otherwise.
6144f5cda257STejun Heo  */
6145f5cda257STejun Heo int ata_host_activate(struct ata_host *host, int irq,
6146f5cda257STejun Heo 		      irq_handler_t irq_handler, unsigned long irq_flags,
6147f5cda257STejun Heo 		      struct scsi_host_template *sht)
6148f5cda257STejun Heo {
6149cbcdd875STejun Heo 	int i, rc;
6150f5cda257STejun Heo 
6151f5cda257STejun Heo 	rc = ata_host_start(host);
6152f5cda257STejun Heo 	if (rc)
6153f5cda257STejun Heo 		return rc;
6154f5cda257STejun Heo 
61553d46b2e2SPaul Mundt 	/* Special case for polling mode */
61563d46b2e2SPaul Mundt 	if (!irq) {
61573d46b2e2SPaul Mundt 		WARN_ON(irq_handler);
61583d46b2e2SPaul Mundt 		return ata_host_register(host, sht);
61593d46b2e2SPaul Mundt 	}
61603d46b2e2SPaul Mundt 
6161f5cda257STejun Heo 	rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
6162f5cda257STejun Heo 			      dev_driver_string(host->dev), host);
6163f5cda257STejun Heo 	if (rc)
6164f5cda257STejun Heo 		return rc;
6165f5cda257STejun Heo 
6166cbcdd875STejun Heo 	for (i = 0; i < host->n_ports; i++)
6167cbcdd875STejun Heo 		ata_port_desc(host->ports[i], "irq %d", irq);
61684031826bSTejun Heo 
6169f5cda257STejun Heo 	rc = ata_host_register(host, sht);
6170f5cda257STejun Heo 	/* if failed, just free the IRQ and leave ports alone */
6171f5cda257STejun Heo 	if (rc)
6172f5cda257STejun Heo 		devm_free_irq(host->dev, irq, host);
6173f5cda257STejun Heo 
6174f5cda257STejun Heo 	return rc;
6175f5cda257STejun Heo }
6176f5cda257STejun Heo 
6177f5cda257STejun Heo /**
6178c6fd2807SJeff Garzik  *	ata_port_detach - Detach ATA port in prepration of device removal
6179c6fd2807SJeff Garzik  *	@ap: ATA port to be detached
6180c6fd2807SJeff Garzik  *
6181c6fd2807SJeff Garzik  *	Detach all ATA devices and the associated SCSI devices of @ap;
6182c6fd2807SJeff Garzik  *	then, remove the associated SCSI host.  @ap is guaranteed to
6183c6fd2807SJeff Garzik  *	be quiescent on return from this function.
6184c6fd2807SJeff Garzik  *
6185c6fd2807SJeff Garzik  *	LOCKING:
6186c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
6187c6fd2807SJeff Garzik  */
6188741b7763SAdrian Bunk static void ata_port_detach(struct ata_port *ap)
6189c6fd2807SJeff Garzik {
6190c6fd2807SJeff Garzik 	unsigned long flags;
6191c6fd2807SJeff Garzik 
6192c6fd2807SJeff Garzik 	if (!ap->ops->error_handler)
6193c6fd2807SJeff Garzik 		goto skip_eh;
6194c6fd2807SJeff Garzik 
6195c6fd2807SJeff Garzik 	/* tell EH we're leaving & flush EH */
6196c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
6197c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_UNLOADING;
6198ece180d1STejun Heo 	ata_port_schedule_eh(ap);
6199c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
6200c6fd2807SJeff Garzik 
6201ece180d1STejun Heo 	/* wait till EH commits suicide */
6202c6fd2807SJeff Garzik 	ata_port_wait_eh(ap);
6203c6fd2807SJeff Garzik 
6204ece180d1STejun Heo 	/* it better be dead now */
6205ece180d1STejun Heo 	WARN_ON(!(ap->pflags & ATA_PFLAG_UNLOADED));
6206c6fd2807SJeff Garzik 
6207afe2c511STejun Heo 	cancel_delayed_work_sync(&ap->hotplug_task);
6208c6fd2807SJeff Garzik 
6209c6fd2807SJeff Garzik  skip_eh:
6210d9027470SGwendal Grignou 	if (ap->pmp_link) {
6211d9027470SGwendal Grignou 		int i;
6212d9027470SGwendal Grignou 		for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
6213d9027470SGwendal Grignou 			ata_tlink_delete(&ap->pmp_link[i]);
6214d9027470SGwendal Grignou 	}
6215d9027470SGwendal Grignou 	ata_tport_delete(ap);
6216d9027470SGwendal Grignou 
6217c6fd2807SJeff Garzik 	/* remove the associated SCSI host */
6218cca3974eSJeff Garzik 	scsi_remove_host(ap->scsi_host);
6219c6fd2807SJeff Garzik }
6220c6fd2807SJeff Garzik 
6221c6fd2807SJeff Garzik /**
62220529c159STejun Heo  *	ata_host_detach - Detach all ports of an ATA host
62230529c159STejun Heo  *	@host: Host to detach
62240529c159STejun Heo  *
62250529c159STejun Heo  *	Detach all ports of @host.
62260529c159STejun Heo  *
62270529c159STejun Heo  *	LOCKING:
62280529c159STejun Heo  *	Kernel thread context (may sleep).
62290529c159STejun Heo  */
62300529c159STejun Heo void ata_host_detach(struct ata_host *host)
62310529c159STejun Heo {
62320529c159STejun Heo 	int i;
62330529c159STejun Heo 
62340529c159STejun Heo 	for (i = 0; i < host->n_ports; i++)
62350529c159STejun Heo 		ata_port_detach(host->ports[i]);
6236562f0c2dSTejun Heo 
6237562f0c2dSTejun Heo 	/* the host is dead now, dissociate ACPI */
6238562f0c2dSTejun Heo 	ata_acpi_dissociate(host);
62390529c159STejun Heo }
62400529c159STejun Heo 
6241c6fd2807SJeff Garzik #ifdef CONFIG_PCI
6242c6fd2807SJeff Garzik 
6243c6fd2807SJeff Garzik /**
6244c6fd2807SJeff Garzik  *	ata_pci_remove_one - PCI layer callback for device removal
6245c6fd2807SJeff Garzik  *	@pdev: PCI device that was removed
6246c6fd2807SJeff Garzik  *
6247b878ca5dSTejun Heo  *	PCI layer indicates to libata via this hook that hot-unplug or
6248b878ca5dSTejun Heo  *	module unload event has occurred.  Detach all ports.  Resource
6249b878ca5dSTejun Heo  *	release is handled via devres.
6250c6fd2807SJeff Garzik  *
6251c6fd2807SJeff Garzik  *	LOCKING:
6252c6fd2807SJeff Garzik  *	Inherited from PCI layer (may sleep).
6253c6fd2807SJeff Garzik  */
6254c6fd2807SJeff Garzik void ata_pci_remove_one(struct pci_dev *pdev)
6255c6fd2807SJeff Garzik {
62562855568bSJeff Garzik 	struct device *dev = &pdev->dev;
6257cca3974eSJeff Garzik 	struct ata_host *host = dev_get_drvdata(dev);
6258c6fd2807SJeff Garzik 
6259f0d36efdSTejun Heo 	ata_host_detach(host);
6260c6fd2807SJeff Garzik }
6261c6fd2807SJeff Garzik 
6262c6fd2807SJeff Garzik /* move to PCI subsystem */
6263c6fd2807SJeff Garzik int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
6264c6fd2807SJeff Garzik {
6265c6fd2807SJeff Garzik 	unsigned long tmp = 0;
6266c6fd2807SJeff Garzik 
6267c6fd2807SJeff Garzik 	switch (bits->width) {
6268c6fd2807SJeff Garzik 	case 1: {
6269c6fd2807SJeff Garzik 		u8 tmp8 = 0;
6270c6fd2807SJeff Garzik 		pci_read_config_byte(pdev, bits->reg, &tmp8);
6271c6fd2807SJeff Garzik 		tmp = tmp8;
6272c6fd2807SJeff Garzik 		break;
6273c6fd2807SJeff Garzik 	}
6274c6fd2807SJeff Garzik 	case 2: {
6275c6fd2807SJeff Garzik 		u16 tmp16 = 0;
6276c6fd2807SJeff Garzik 		pci_read_config_word(pdev, bits->reg, &tmp16);
6277c6fd2807SJeff Garzik 		tmp = tmp16;
6278c6fd2807SJeff Garzik 		break;
6279c6fd2807SJeff Garzik 	}
6280c6fd2807SJeff Garzik 	case 4: {
6281c6fd2807SJeff Garzik 		u32 tmp32 = 0;
6282c6fd2807SJeff Garzik 		pci_read_config_dword(pdev, bits->reg, &tmp32);
6283c6fd2807SJeff Garzik 		tmp = tmp32;
6284c6fd2807SJeff Garzik 		break;
6285c6fd2807SJeff Garzik 	}
6286c6fd2807SJeff Garzik 
6287c6fd2807SJeff Garzik 	default:
6288c6fd2807SJeff Garzik 		return -EINVAL;
6289c6fd2807SJeff Garzik 	}
6290c6fd2807SJeff Garzik 
6291c6fd2807SJeff Garzik 	tmp &= bits->mask;
6292c6fd2807SJeff Garzik 
6293c6fd2807SJeff Garzik 	return (tmp == bits->val) ? 1 : 0;
6294c6fd2807SJeff Garzik }
6295c6fd2807SJeff Garzik 
62966ffa01d8STejun Heo #ifdef CONFIG_PM
6297c6fd2807SJeff Garzik void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
6298c6fd2807SJeff Garzik {
6299c6fd2807SJeff Garzik 	pci_save_state(pdev);
6300c6fd2807SJeff Garzik 	pci_disable_device(pdev);
63014c90d971STejun Heo 
63023a2d5b70SRafael J. Wysocki 	if (mesg.event & PM_EVENT_SLEEP)
6303c6fd2807SJeff Garzik 		pci_set_power_state(pdev, PCI_D3hot);
6304c6fd2807SJeff Garzik }
6305c6fd2807SJeff Garzik 
6306553c4aa6STejun Heo int ata_pci_device_do_resume(struct pci_dev *pdev)
6307c6fd2807SJeff Garzik {
6308553c4aa6STejun Heo 	int rc;
6309553c4aa6STejun Heo 
6310c6fd2807SJeff Garzik 	pci_set_power_state(pdev, PCI_D0);
6311c6fd2807SJeff Garzik 	pci_restore_state(pdev);
6312553c4aa6STejun Heo 
6313f0d36efdSTejun Heo 	rc = pcim_enable_device(pdev);
6314553c4aa6STejun Heo 	if (rc) {
6315a44fec1fSJoe Perches 		dev_err(&pdev->dev,
6316553c4aa6STejun Heo 			"failed to enable device after resume (%d)\n", rc);
6317553c4aa6STejun Heo 		return rc;
6318553c4aa6STejun Heo 	}
6319553c4aa6STejun Heo 
6320c6fd2807SJeff Garzik 	pci_set_master(pdev);
6321553c4aa6STejun Heo 	return 0;
6322c6fd2807SJeff Garzik }
6323c6fd2807SJeff Garzik 
6324c6fd2807SJeff Garzik int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
6325c6fd2807SJeff Garzik {
6326cca3974eSJeff Garzik 	struct ata_host *host = dev_get_drvdata(&pdev->dev);
6327c6fd2807SJeff Garzik 	int rc = 0;
6328c6fd2807SJeff Garzik 
6329cca3974eSJeff Garzik 	rc = ata_host_suspend(host, mesg);
6330c6fd2807SJeff Garzik 	if (rc)
6331c6fd2807SJeff Garzik 		return rc;
6332c6fd2807SJeff Garzik 
6333c6fd2807SJeff Garzik 	ata_pci_device_do_suspend(pdev, mesg);
6334c6fd2807SJeff Garzik 
6335c6fd2807SJeff Garzik 	return 0;
6336c6fd2807SJeff Garzik }
6337c6fd2807SJeff Garzik 
6338c6fd2807SJeff Garzik int ata_pci_device_resume(struct pci_dev *pdev)
6339c6fd2807SJeff Garzik {
6340cca3974eSJeff Garzik 	struct ata_host *host = dev_get_drvdata(&pdev->dev);
6341553c4aa6STejun Heo 	int rc;
6342c6fd2807SJeff Garzik 
6343553c4aa6STejun Heo 	rc = ata_pci_device_do_resume(pdev);
6344553c4aa6STejun Heo 	if (rc == 0)
6345cca3974eSJeff Garzik 		ata_host_resume(host);
6346553c4aa6STejun Heo 	return rc;
6347c6fd2807SJeff Garzik }
63486ffa01d8STejun Heo #endif /* CONFIG_PM */
63496ffa01d8STejun Heo 
6350c6fd2807SJeff Garzik #endif /* CONFIG_PCI */
6351c6fd2807SJeff Garzik 
635233267325STejun Heo static int __init ata_parse_force_one(char **cur,
635333267325STejun Heo 				      struct ata_force_ent *force_ent,
635433267325STejun Heo 				      const char **reason)
635533267325STejun Heo {
635633267325STejun Heo 	/* FIXME: Currently, there's no way to tag init const data and
635733267325STejun Heo 	 * using __initdata causes build failure on some versions of
635833267325STejun Heo 	 * gcc.  Once __initdataconst is implemented, add const to the
635933267325STejun Heo 	 * following structure.
636033267325STejun Heo 	 */
636133267325STejun Heo 	static struct ata_force_param force_tbl[] __initdata = {
636233267325STejun Heo 		{ "40c",	.cbl		= ATA_CBL_PATA40 },
636333267325STejun Heo 		{ "80c",	.cbl		= ATA_CBL_PATA80 },
636433267325STejun Heo 		{ "short40c",	.cbl		= ATA_CBL_PATA40_SHORT },
636533267325STejun Heo 		{ "unk",	.cbl		= ATA_CBL_PATA_UNK },
636633267325STejun Heo 		{ "ign",	.cbl		= ATA_CBL_PATA_IGN },
636733267325STejun Heo 		{ "sata",	.cbl		= ATA_CBL_SATA },
636833267325STejun Heo 		{ "1.5Gbps",	.spd_limit	= 1 },
636933267325STejun Heo 		{ "3.0Gbps",	.spd_limit	= 2 },
637033267325STejun Heo 		{ "noncq",	.horkage_on	= ATA_HORKAGE_NONCQ },
637133267325STejun Heo 		{ "ncq",	.horkage_off	= ATA_HORKAGE_NONCQ },
637243c9c591STejun Heo 		{ "dump_id",	.horkage_on	= ATA_HORKAGE_DUMP_ID },
637333267325STejun Heo 		{ "pio0",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 0) },
637433267325STejun Heo 		{ "pio1",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 1) },
637533267325STejun Heo 		{ "pio2",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 2) },
637633267325STejun Heo 		{ "pio3",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 3) },
637733267325STejun Heo 		{ "pio4",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 4) },
637833267325STejun Heo 		{ "pio5",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 5) },
637933267325STejun Heo 		{ "pio6",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 6) },
638033267325STejun Heo 		{ "mwdma0",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 0) },
638133267325STejun Heo 		{ "mwdma1",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 1) },
638233267325STejun Heo 		{ "mwdma2",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 2) },
638333267325STejun Heo 		{ "mwdma3",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 3) },
638433267325STejun Heo 		{ "mwdma4",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 4) },
638533267325STejun Heo 		{ "udma0",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
638633267325STejun Heo 		{ "udma16",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
638733267325STejun Heo 		{ "udma/16",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
638833267325STejun Heo 		{ "udma1",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
638933267325STejun Heo 		{ "udma25",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
639033267325STejun Heo 		{ "udma/25",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
639133267325STejun Heo 		{ "udma2",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
639233267325STejun Heo 		{ "udma33",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
639333267325STejun Heo 		{ "udma/33",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
639433267325STejun Heo 		{ "udma3",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
639533267325STejun Heo 		{ "udma44",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
639633267325STejun Heo 		{ "udma/44",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
639733267325STejun Heo 		{ "udma4",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
639833267325STejun Heo 		{ "udma66",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
639933267325STejun Heo 		{ "udma/66",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
640033267325STejun Heo 		{ "udma5",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
640133267325STejun Heo 		{ "udma100",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
640233267325STejun Heo 		{ "udma/100",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
640333267325STejun Heo 		{ "udma6",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
640433267325STejun Heo 		{ "udma133",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
640533267325STejun Heo 		{ "udma/133",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
640633267325STejun Heo 		{ "udma7",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 7) },
640705944bdfSTejun Heo 		{ "nohrst",	.lflags		= ATA_LFLAG_NO_HRST },
640805944bdfSTejun Heo 		{ "nosrst",	.lflags		= ATA_LFLAG_NO_SRST },
640905944bdfSTejun Heo 		{ "norst",	.lflags		= ATA_LFLAG_NO_HRST | ATA_LFLAG_NO_SRST },
641033267325STejun Heo 	};
641133267325STejun Heo 	char *start = *cur, *p = *cur;
641233267325STejun Heo 	char *id, *val, *endp;
641333267325STejun Heo 	const struct ata_force_param *match_fp = NULL;
641433267325STejun Heo 	int nr_matches = 0, i;
641533267325STejun Heo 
641633267325STejun Heo 	/* find where this param ends and update *cur */
641733267325STejun Heo 	while (*p != '\0' && *p != ',')
641833267325STejun Heo 		p++;
641933267325STejun Heo 
642033267325STejun Heo 	if (*p == '\0')
642133267325STejun Heo 		*cur = p;
642233267325STejun Heo 	else
642333267325STejun Heo 		*cur = p + 1;
642433267325STejun Heo 
642533267325STejun Heo 	*p = '\0';
642633267325STejun Heo 
642733267325STejun Heo 	/* parse */
642833267325STejun Heo 	p = strchr(start, ':');
642933267325STejun Heo 	if (!p) {
643033267325STejun Heo 		val = strstrip(start);
643133267325STejun Heo 		goto parse_val;
643233267325STejun Heo 	}
643333267325STejun Heo 	*p = '\0';
643433267325STejun Heo 
643533267325STejun Heo 	id = strstrip(start);
643633267325STejun Heo 	val = strstrip(p + 1);
643733267325STejun Heo 
643833267325STejun Heo 	/* parse id */
643933267325STejun Heo 	p = strchr(id, '.');
644033267325STejun Heo 	if (p) {
644133267325STejun Heo 		*p++ = '\0';
644233267325STejun Heo 		force_ent->device = simple_strtoul(p, &endp, 10);
644333267325STejun Heo 		if (p == endp || *endp != '\0') {
644433267325STejun Heo 			*reason = "invalid device";
644533267325STejun Heo 			return -EINVAL;
644633267325STejun Heo 		}
644733267325STejun Heo 	}
644833267325STejun Heo 
644933267325STejun Heo 	force_ent->port = simple_strtoul(id, &endp, 10);
645033267325STejun Heo 	if (p == endp || *endp != '\0') {
645133267325STejun Heo 		*reason = "invalid port/link";
645233267325STejun Heo 		return -EINVAL;
645333267325STejun Heo 	}
645433267325STejun Heo 
645533267325STejun Heo  parse_val:
645633267325STejun Heo 	/* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */
645733267325STejun Heo 	for (i = 0; i < ARRAY_SIZE(force_tbl); i++) {
645833267325STejun Heo 		const struct ata_force_param *fp = &force_tbl[i];
645933267325STejun Heo 
646033267325STejun Heo 		if (strncasecmp(val, fp->name, strlen(val)))
646133267325STejun Heo 			continue;
646233267325STejun Heo 
646333267325STejun Heo 		nr_matches++;
646433267325STejun Heo 		match_fp = fp;
646533267325STejun Heo 
646633267325STejun Heo 		if (strcasecmp(val, fp->name) == 0) {
646733267325STejun Heo 			nr_matches = 1;
646833267325STejun Heo 			break;
646933267325STejun Heo 		}
647033267325STejun Heo 	}
647133267325STejun Heo 
647233267325STejun Heo 	if (!nr_matches) {
647333267325STejun Heo 		*reason = "unknown value";
647433267325STejun Heo 		return -EINVAL;
647533267325STejun Heo 	}
647633267325STejun Heo 	if (nr_matches > 1) {
647733267325STejun Heo 		*reason = "ambigious value";
647833267325STejun Heo 		return -EINVAL;
647933267325STejun Heo 	}
648033267325STejun Heo 
648133267325STejun Heo 	force_ent->param = *match_fp;
648233267325STejun Heo 
648333267325STejun Heo 	return 0;
648433267325STejun Heo }
648533267325STejun Heo 
648633267325STejun Heo static void __init ata_parse_force_param(void)
648733267325STejun Heo {
648833267325STejun Heo 	int idx = 0, size = 1;
648933267325STejun Heo 	int last_port = -1, last_device = -1;
649033267325STejun Heo 	char *p, *cur, *next;
649133267325STejun Heo 
649233267325STejun Heo 	/* calculate maximum number of params and allocate force_tbl */
649333267325STejun Heo 	for (p = ata_force_param_buf; *p; p++)
649433267325STejun Heo 		if (*p == ',')
649533267325STejun Heo 			size++;
649633267325STejun Heo 
649733267325STejun Heo 	ata_force_tbl = kzalloc(sizeof(ata_force_tbl[0]) * size, GFP_KERNEL);
649833267325STejun Heo 	if (!ata_force_tbl) {
649933267325STejun Heo 		printk(KERN_WARNING "ata: failed to extend force table, "
650033267325STejun Heo 		       "libata.force ignored\n");
650133267325STejun Heo 		return;
650233267325STejun Heo 	}
650333267325STejun Heo 
650433267325STejun Heo 	/* parse and populate the table */
650533267325STejun Heo 	for (cur = ata_force_param_buf; *cur != '\0'; cur = next) {
650633267325STejun Heo 		const char *reason = "";
650733267325STejun Heo 		struct ata_force_ent te = { .port = -1, .device = -1 };
650833267325STejun Heo 
650933267325STejun Heo 		next = cur;
651033267325STejun Heo 		if (ata_parse_force_one(&next, &te, &reason)) {
651133267325STejun Heo 			printk(KERN_WARNING "ata: failed to parse force "
651233267325STejun Heo 			       "parameter \"%s\" (%s)\n",
651333267325STejun Heo 			       cur, reason);
651433267325STejun Heo 			continue;
651533267325STejun Heo 		}
651633267325STejun Heo 
651733267325STejun Heo 		if (te.port == -1) {
651833267325STejun Heo 			te.port = last_port;
651933267325STejun Heo 			te.device = last_device;
652033267325STejun Heo 		}
652133267325STejun Heo 
652233267325STejun Heo 		ata_force_tbl[idx++] = te;
652333267325STejun Heo 
652433267325STejun Heo 		last_port = te.port;
652533267325STejun Heo 		last_device = te.device;
652633267325STejun Heo 	}
652733267325STejun Heo 
652833267325STejun Heo 	ata_force_tbl_size = idx;
652933267325STejun Heo }
6530c6fd2807SJeff Garzik 
6531c6fd2807SJeff Garzik static int __init ata_init(void)
6532c6fd2807SJeff Garzik {
6533d9027470SGwendal Grignou 	int rc;
6534270390e1STejun Heo 
653533267325STejun Heo 	ata_parse_force_param();
653633267325STejun Heo 
65376b66d958SMatthew Garrett 	ata_acpi_register();
65386b66d958SMatthew Garrett 
6539270390e1STejun Heo 	rc = ata_sff_init();
6540ad72cf98STejun Heo 	if (rc) {
6541ad72cf98STejun Heo 		kfree(ata_force_tbl);
6542ad72cf98STejun Heo 		return rc;
6543ad72cf98STejun Heo 	}
6544c6fd2807SJeff Garzik 
6545d9027470SGwendal Grignou 	libata_transport_init();
6546d9027470SGwendal Grignou 	ata_scsi_transport_template = ata_attach_transport();
6547d9027470SGwendal Grignou 	if (!ata_scsi_transport_template) {
6548d9027470SGwendal Grignou 		ata_sff_exit();
6549d9027470SGwendal Grignou 		rc = -ENOMEM;
6550d9027470SGwendal Grignou 		goto err_out;
6551d9027470SGwendal Grignou 	}
6552d9027470SGwendal Grignou 
6553c6fd2807SJeff Garzik 	printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
6554c6fd2807SJeff Garzik 	return 0;
6555d9027470SGwendal Grignou 
6556d9027470SGwendal Grignou err_out:
6557d9027470SGwendal Grignou 	return rc;
6558c6fd2807SJeff Garzik }
6559c6fd2807SJeff Garzik 
6560c6fd2807SJeff Garzik static void __exit ata_exit(void)
6561c6fd2807SJeff Garzik {
6562d9027470SGwendal Grignou 	ata_release_transport(ata_scsi_transport_template);
6563d9027470SGwendal Grignou 	libata_transport_exit();
6564270390e1STejun Heo 	ata_sff_exit();
65656b66d958SMatthew Garrett 	ata_acpi_unregister();
656633267325STejun Heo 	kfree(ata_force_tbl);
6567c6fd2807SJeff Garzik }
6568c6fd2807SJeff Garzik 
6569a4625085SBrian King subsys_initcall(ata_init);
6570c6fd2807SJeff Garzik module_exit(ata_exit);
6571c6fd2807SJeff Garzik 
65729990b6f3SAkinobu Mita static DEFINE_RATELIMIT_STATE(ratelimit, HZ / 5, 1);
6573c6fd2807SJeff Garzik 
6574c6fd2807SJeff Garzik int ata_ratelimit(void)
6575c6fd2807SJeff Garzik {
65769990b6f3SAkinobu Mita 	return __ratelimit(&ratelimit);
6577c6fd2807SJeff Garzik }
6578c6fd2807SJeff Garzik 
6579c0c362b6STejun Heo /**
6580c0c362b6STejun Heo  *	ata_msleep - ATA EH owner aware msleep
6581c0c362b6STejun Heo  *	@ap: ATA port to attribute the sleep to
6582c0c362b6STejun Heo  *	@msecs: duration to sleep in milliseconds
6583c0c362b6STejun Heo  *
6584c0c362b6STejun Heo  *	Sleeps @msecs.  If the current task is owner of @ap's EH, the
6585c0c362b6STejun Heo  *	ownership is released before going to sleep and reacquired
6586c0c362b6STejun Heo  *	after the sleep is complete.  IOW, other ports sharing the
6587c0c362b6STejun Heo  *	@ap->host will be allowed to own the EH while this task is
6588c0c362b6STejun Heo  *	sleeping.
6589c0c362b6STejun Heo  *
6590c0c362b6STejun Heo  *	LOCKING:
6591c0c362b6STejun Heo  *	Might sleep.
6592c0c362b6STejun Heo  */
659397750cebSTejun Heo void ata_msleep(struct ata_port *ap, unsigned int msecs)
659497750cebSTejun Heo {
6595c0c362b6STejun Heo 	bool owns_eh = ap && ap->host->eh_owner == current;
6596c0c362b6STejun Heo 
6597c0c362b6STejun Heo 	if (owns_eh)
6598c0c362b6STejun Heo 		ata_eh_release(ap);
6599c0c362b6STejun Heo 
660097750cebSTejun Heo 	msleep(msecs);
6601c0c362b6STejun Heo 
6602c0c362b6STejun Heo 	if (owns_eh)
6603c0c362b6STejun Heo 		ata_eh_acquire(ap);
660497750cebSTejun Heo }
660597750cebSTejun Heo 
6606c6fd2807SJeff Garzik /**
6607c6fd2807SJeff Garzik  *	ata_wait_register - wait until register value changes
660897750cebSTejun Heo  *	@ap: ATA port to wait register for, can be NULL
6609c6fd2807SJeff Garzik  *	@reg: IO-mapped register
6610c6fd2807SJeff Garzik  *	@mask: Mask to apply to read register value
6611c6fd2807SJeff Garzik  *	@val: Wait condition
6612341c2c95STejun Heo  *	@interval: polling interval in milliseconds
6613341c2c95STejun Heo  *	@timeout: timeout in milliseconds
6614c6fd2807SJeff Garzik  *
6615c6fd2807SJeff Garzik  *	Waiting for some bits of register to change is a common
6616c6fd2807SJeff Garzik  *	operation for ATA controllers.  This function reads 32bit LE
6617c6fd2807SJeff Garzik  *	IO-mapped register @reg and tests for the following condition.
6618c6fd2807SJeff Garzik  *
6619c6fd2807SJeff Garzik  *	(*@reg & mask) != val
6620c6fd2807SJeff Garzik  *
6621c6fd2807SJeff Garzik  *	If the condition is met, it returns; otherwise, the process is
6622c6fd2807SJeff Garzik  *	repeated after @interval_msec until timeout.
6623c6fd2807SJeff Garzik  *
6624c6fd2807SJeff Garzik  *	LOCKING:
6625c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
6626c6fd2807SJeff Garzik  *
6627c6fd2807SJeff Garzik  *	RETURNS:
6628c6fd2807SJeff Garzik  *	The final register value.
6629c6fd2807SJeff Garzik  */
663097750cebSTejun Heo u32 ata_wait_register(struct ata_port *ap, void __iomem *reg, u32 mask, u32 val,
6631341c2c95STejun Heo 		      unsigned long interval, unsigned long timeout)
6632c6fd2807SJeff Garzik {
6633341c2c95STejun Heo 	unsigned long deadline;
6634c6fd2807SJeff Garzik 	u32 tmp;
6635c6fd2807SJeff Garzik 
6636c6fd2807SJeff Garzik 	tmp = ioread32(reg);
6637c6fd2807SJeff Garzik 
6638c6fd2807SJeff Garzik 	/* Calculate timeout _after_ the first read to make sure
6639c6fd2807SJeff Garzik 	 * preceding writes reach the controller before starting to
6640c6fd2807SJeff Garzik 	 * eat away the timeout.
6641c6fd2807SJeff Garzik 	 */
6642341c2c95STejun Heo 	deadline = ata_deadline(jiffies, timeout);
6643c6fd2807SJeff Garzik 
6644341c2c95STejun Heo 	while ((tmp & mask) == val && time_before(jiffies, deadline)) {
664597750cebSTejun Heo 		ata_msleep(ap, interval);
6646c6fd2807SJeff Garzik 		tmp = ioread32(reg);
6647c6fd2807SJeff Garzik 	}
6648c6fd2807SJeff Garzik 
6649c6fd2807SJeff Garzik 	return tmp;
6650c6fd2807SJeff Garzik }
6651c6fd2807SJeff Garzik 
6652c6fd2807SJeff Garzik /*
6653c6fd2807SJeff Garzik  * Dummy port_ops
6654c6fd2807SJeff Garzik  */
6655c6fd2807SJeff Garzik static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
6656c6fd2807SJeff Garzik {
6657c6fd2807SJeff Garzik 	return AC_ERR_SYSTEM;
6658c6fd2807SJeff Garzik }
6659c6fd2807SJeff Garzik 
6660182d7bbaSTejun Heo static void ata_dummy_error_handler(struct ata_port *ap)
6661182d7bbaSTejun Heo {
6662182d7bbaSTejun Heo 	/* truly dummy */
6663182d7bbaSTejun Heo }
6664182d7bbaSTejun Heo 
6665029cfd6bSTejun Heo struct ata_port_operations ata_dummy_port_ops = {
6666c6fd2807SJeff Garzik 	.qc_prep		= ata_noop_qc_prep,
6667c6fd2807SJeff Garzik 	.qc_issue		= ata_dummy_qc_issue,
6668182d7bbaSTejun Heo 	.error_handler		= ata_dummy_error_handler,
6669e4a9c373SDan Williams 	.sched_eh		= ata_std_sched_eh,
6670e4a9c373SDan Williams 	.end_eh			= ata_std_end_eh,
6671c6fd2807SJeff Garzik };
6672c6fd2807SJeff Garzik 
667321b0ad4fSTejun Heo const struct ata_port_info ata_dummy_port_info = {
667421b0ad4fSTejun Heo 	.port_ops		= &ata_dummy_port_ops,
667521b0ad4fSTejun Heo };
667621b0ad4fSTejun Heo 
6677c6fd2807SJeff Garzik /*
6678a9a79dfeSJoe Perches  * Utility print functions
6679a9a79dfeSJoe Perches  */
6680a9a79dfeSJoe Perches int ata_port_printk(const struct ata_port *ap, const char *level,
6681a9a79dfeSJoe Perches 		    const char *fmt, ...)
6682a9a79dfeSJoe Perches {
6683a9a79dfeSJoe Perches 	struct va_format vaf;
6684a9a79dfeSJoe Perches 	va_list args;
6685a9a79dfeSJoe Perches 	int r;
6686a9a79dfeSJoe Perches 
6687a9a79dfeSJoe Perches 	va_start(args, fmt);
6688a9a79dfeSJoe Perches 
6689a9a79dfeSJoe Perches 	vaf.fmt = fmt;
6690a9a79dfeSJoe Perches 	vaf.va = &args;
6691a9a79dfeSJoe Perches 
6692a9a79dfeSJoe Perches 	r = printk("%sata%u: %pV", level, ap->print_id, &vaf);
6693a9a79dfeSJoe Perches 
6694a9a79dfeSJoe Perches 	va_end(args);
6695a9a79dfeSJoe Perches 
6696a9a79dfeSJoe Perches 	return r;
6697a9a79dfeSJoe Perches }
6698a9a79dfeSJoe Perches EXPORT_SYMBOL(ata_port_printk);
6699a9a79dfeSJoe Perches 
6700a9a79dfeSJoe Perches int ata_link_printk(const struct ata_link *link, const char *level,
6701a9a79dfeSJoe Perches 		    const char *fmt, ...)
6702a9a79dfeSJoe Perches {
6703a9a79dfeSJoe Perches 	struct va_format vaf;
6704a9a79dfeSJoe Perches 	va_list args;
6705a9a79dfeSJoe Perches 	int r;
6706a9a79dfeSJoe Perches 
6707a9a79dfeSJoe Perches 	va_start(args, fmt);
6708a9a79dfeSJoe Perches 
6709a9a79dfeSJoe Perches 	vaf.fmt = fmt;
6710a9a79dfeSJoe Perches 	vaf.va = &args;
6711a9a79dfeSJoe Perches 
6712a9a79dfeSJoe Perches 	if (sata_pmp_attached(link->ap) || link->ap->slave_link)
6713a9a79dfeSJoe Perches 		r = printk("%sata%u.%02u: %pV",
6714a9a79dfeSJoe Perches 			   level, link->ap->print_id, link->pmp, &vaf);
6715a9a79dfeSJoe Perches 	else
6716a9a79dfeSJoe Perches 		r = printk("%sata%u: %pV",
6717a9a79dfeSJoe Perches 			   level, link->ap->print_id, &vaf);
6718a9a79dfeSJoe Perches 
6719a9a79dfeSJoe Perches 	va_end(args);
6720a9a79dfeSJoe Perches 
6721a9a79dfeSJoe Perches 	return r;
6722a9a79dfeSJoe Perches }
6723a9a79dfeSJoe Perches EXPORT_SYMBOL(ata_link_printk);
6724a9a79dfeSJoe Perches 
6725a9a79dfeSJoe Perches int ata_dev_printk(const struct ata_device *dev, const char *level,
6726a9a79dfeSJoe Perches 		    const char *fmt, ...)
6727a9a79dfeSJoe Perches {
6728a9a79dfeSJoe Perches 	struct va_format vaf;
6729a9a79dfeSJoe Perches 	va_list args;
6730a9a79dfeSJoe Perches 	int r;
6731a9a79dfeSJoe Perches 
6732a9a79dfeSJoe Perches 	va_start(args, fmt);
6733a9a79dfeSJoe Perches 
6734a9a79dfeSJoe Perches 	vaf.fmt = fmt;
6735a9a79dfeSJoe Perches 	vaf.va = &args;
6736a9a79dfeSJoe Perches 
6737a9a79dfeSJoe Perches 	r = printk("%sata%u.%02u: %pV",
6738a9a79dfeSJoe Perches 		   level, dev->link->ap->print_id, dev->link->pmp + dev->devno,
6739a9a79dfeSJoe Perches 		   &vaf);
6740a9a79dfeSJoe Perches 
6741a9a79dfeSJoe Perches 	va_end(args);
6742a9a79dfeSJoe Perches 
6743a9a79dfeSJoe Perches 	return r;
6744a9a79dfeSJoe Perches }
6745a9a79dfeSJoe Perches EXPORT_SYMBOL(ata_dev_printk);
6746a9a79dfeSJoe Perches 
674706296a1eSJoe Perches void ata_print_version(const struct device *dev, const char *version)
674806296a1eSJoe Perches {
674906296a1eSJoe Perches 	dev_printk(KERN_DEBUG, dev, "version %s\n", version);
675006296a1eSJoe Perches }
675106296a1eSJoe Perches EXPORT_SYMBOL(ata_print_version);
675206296a1eSJoe Perches 
6753a9a79dfeSJoe Perches /*
6754c6fd2807SJeff Garzik  * libata is essentially a library of internal helper functions for
6755c6fd2807SJeff Garzik  * low-level ATA host controller drivers.  As such, the API/ABI is
6756c6fd2807SJeff Garzik  * likely to change as new drivers are added and updated.
6757c6fd2807SJeff Garzik  * Do not depend on ABI/API stability.
6758c6fd2807SJeff Garzik  */
6759c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
6760c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
6761c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_deb_timing_long);
6762029cfd6bSTejun Heo EXPORT_SYMBOL_GPL(ata_base_port_ops);
6763029cfd6bSTejun Heo EXPORT_SYMBOL_GPL(sata_port_ops);
6764c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
676521b0ad4fSTejun Heo EXPORT_SYMBOL_GPL(ata_dummy_port_info);
67661eca4365STejun Heo EXPORT_SYMBOL_GPL(ata_link_next);
67671eca4365STejun Heo EXPORT_SYMBOL_GPL(ata_dev_next);
6768c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_std_bios_param);
6769d8d9129eSTejun Heo EXPORT_SYMBOL_GPL(ata_scsi_unlock_native_capacity);
6770cca3974eSJeff Garzik EXPORT_SYMBOL_GPL(ata_host_init);
6771f3187195STejun Heo EXPORT_SYMBOL_GPL(ata_host_alloc);
6772f5cda257STejun Heo EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
6773b1c72916STejun Heo EXPORT_SYMBOL_GPL(ata_slave_link_init);
6774ecef7253STejun Heo EXPORT_SYMBOL_GPL(ata_host_start);
6775f3187195STejun Heo EXPORT_SYMBOL_GPL(ata_host_register);
6776f5cda257STejun Heo EXPORT_SYMBOL_GPL(ata_host_activate);
67770529c159STejun Heo EXPORT_SYMBOL_GPL(ata_host_detach);
6778c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_sg_init);
6779c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_qc_complete);
6780c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
6781436d34b3STejun Heo EXPORT_SYMBOL_GPL(atapi_cmd_type);
6782c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_tf_to_fis);
6783c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_tf_from_fis);
67846357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_pack_xfermask);
67856357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_unpack_xfermask);
67866357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
67876357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
67886357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
67896357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_mode_string);
67906357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_id_xfermask);
679104351821SAlan EXPORT_SYMBOL_GPL(ata_do_set_mode);
679231cc23b3STejun Heo EXPORT_SYMBOL_GPL(ata_std_qc_defer);
6793c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
679410305f0fSAlan EXPORT_SYMBOL_GPL(ata_dev_disable);
6795c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_set_spd);
6796aa2731adSTejun Heo EXPORT_SYMBOL_GPL(ata_wait_after_reset);
6797936fd732STejun Heo EXPORT_SYMBOL_GPL(sata_link_debounce);
6798936fd732STejun Heo EXPORT_SYMBOL_GPL(sata_link_resume);
67991152b261STejun Heo EXPORT_SYMBOL_GPL(sata_link_scr_lpm);
68000aa1113dSTejun Heo EXPORT_SYMBOL_GPL(ata_std_prereset);
6801cc0680a5STejun Heo EXPORT_SYMBOL_GPL(sata_link_hardreset);
680257c9efdfSTejun Heo EXPORT_SYMBOL_GPL(sata_std_hardreset);
6803203c75b8STejun Heo EXPORT_SYMBOL_GPL(ata_std_postreset);
6804c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_dev_classify);
6805c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_dev_pair);
6806c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_ratelimit);
680797750cebSTejun Heo EXPORT_SYMBOL_GPL(ata_msleep);
6808c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_wait_register);
6809c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
6810c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
6811c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
6812c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
6813f6e67035SDan Williams EXPORT_SYMBOL_GPL(__ata_change_queue_depth);
6814c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_valid);
6815c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_read);
6816c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_write);
6817c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_write_flush);
6818936fd732STejun Heo EXPORT_SYMBOL_GPL(ata_link_online);
6819936fd732STejun Heo EXPORT_SYMBOL_GPL(ata_link_offline);
68206ffa01d8STejun Heo #ifdef CONFIG_PM
6821cca3974eSJeff Garzik EXPORT_SYMBOL_GPL(ata_host_suspend);
6822cca3974eSJeff Garzik EXPORT_SYMBOL_GPL(ata_host_resume);
68236ffa01d8STejun Heo #endif /* CONFIG_PM */
6824c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_id_string);
6825c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_id_c_string);
6826963e4975SAlan Cox EXPORT_SYMBOL_GPL(ata_do_dev_read_id);
6827c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_simulate);
6828c6fd2807SJeff Garzik 
6829c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
68306357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_timing_find_mode);
6831c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_timing_compute);
6832c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_timing_merge);
6833a0f79b92STejun Heo EXPORT_SYMBOL_GPL(ata_timing_cycle2mode);
6834c6fd2807SJeff Garzik 
6835c6fd2807SJeff Garzik #ifdef CONFIG_PCI
6836c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(pci_test_config_bits);
6837c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_remove_one);
68386ffa01d8STejun Heo #ifdef CONFIG_PM
6839c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
6840c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
6841c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
6842c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_resume);
68436ffa01d8STejun Heo #endif /* CONFIG_PM */
6844c6fd2807SJeff Garzik #endif /* CONFIG_PCI */
6845c6fd2807SJeff Garzik 
6846b64bbc39STejun Heo EXPORT_SYMBOL_GPL(__ata_ehi_push_desc);
6847b64bbc39STejun Heo EXPORT_SYMBOL_GPL(ata_ehi_push_desc);
6848b64bbc39STejun Heo EXPORT_SYMBOL_GPL(ata_ehi_clear_desc);
6849cbcdd875STejun Heo EXPORT_SYMBOL_GPL(ata_port_desc);
6850cbcdd875STejun Heo #ifdef CONFIG_PCI
6851cbcdd875STejun Heo EXPORT_SYMBOL_GPL(ata_port_pbar_desc);
6852cbcdd875STejun Heo #endif /* CONFIG_PCI */
6853c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
6854dbd82616STejun Heo EXPORT_SYMBOL_GPL(ata_link_abort);
6855c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_abort);
6856c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_freeze);
68577d77b247STejun Heo EXPORT_SYMBOL_GPL(sata_async_notification);
6858c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
6859c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
6860c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
6861c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
686210acf3b0SMark Lord EXPORT_SYMBOL_GPL(ata_eh_analyze_ncq_error);
6863c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_do_eh);
6864a1efdabaSTejun Heo EXPORT_SYMBOL_GPL(ata_std_error_handler);
6865be0d18dfSAlan Cox 
6866be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_40wire);
6867be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_80wire);
6868be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_unknown);
6869c88f90c3STejun Heo EXPORT_SYMBOL_GPL(ata_cable_ignore);
6870be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_sata);
6871