xref: /openbmc/linux/drivers/ata/libata-core.c (revision 85d6725b)
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,
83029cfd6bSTejun Heo };
84029cfd6bSTejun Heo 
85029cfd6bSTejun Heo const struct ata_port_operations sata_port_ops = {
86029cfd6bSTejun Heo 	.inherits		= &ata_base_port_ops,
87029cfd6bSTejun Heo 
88029cfd6bSTejun Heo 	.qc_defer		= ata_std_qc_defer,
8957c9efdfSTejun Heo 	.hardreset		= sata_std_hardreset,
90029cfd6bSTejun Heo };
91029cfd6bSTejun Heo 
92c6fd2807SJeff Garzik static unsigned int ata_dev_init_params(struct ata_device *dev,
93c6fd2807SJeff Garzik 					u16 heads, u16 sectors);
94c6fd2807SJeff Garzik static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
95c6fd2807SJeff Garzik static void ata_dev_xfermask(struct ata_device *dev);
9675683fe7STejun Heo static unsigned long ata_dev_blacklisted(const struct ata_device *dev);
97c6fd2807SJeff Garzik 
9885d6725bSDan Williams atomic_t ata_print_id = ATOMIC_INIT(1);
99c6fd2807SJeff Garzik 
10033267325STejun Heo struct ata_force_param {
10133267325STejun Heo 	const char	*name;
10233267325STejun Heo 	unsigned int	cbl;
10333267325STejun Heo 	int		spd_limit;
10433267325STejun Heo 	unsigned long	xfer_mask;
10533267325STejun Heo 	unsigned int	horkage_on;
10633267325STejun Heo 	unsigned int	horkage_off;
10705944bdfSTejun Heo 	unsigned int	lflags;
10833267325STejun Heo };
10933267325STejun Heo 
11033267325STejun Heo struct ata_force_ent {
11133267325STejun Heo 	int			port;
11233267325STejun Heo 	int			device;
11333267325STejun Heo 	struct ata_force_param	param;
11433267325STejun Heo };
11533267325STejun Heo 
11633267325STejun Heo static struct ata_force_ent *ata_force_tbl;
11733267325STejun Heo static int ata_force_tbl_size;
11833267325STejun Heo 
11933267325STejun Heo static char ata_force_param_buf[PAGE_SIZE] __initdata;
1207afb4222STejun Heo /* param_buf is thrown away after initialization, disallow read */
1217afb4222STejun Heo module_param_string(force, ata_force_param_buf, sizeof(ata_force_param_buf), 0);
12233267325STejun Heo MODULE_PARM_DESC(force, "Force ATA configurations including cable type, link speed and transfer mode (see Documentation/kernel-parameters.txt for details)");
12333267325STejun Heo 
1242486fa56STejun Heo static int atapi_enabled = 1;
125c6fd2807SJeff Garzik module_param(atapi_enabled, int, 0444);
126ad5d8eacSEvgeni Golov MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on [default])");
127c6fd2807SJeff Garzik 
128c5c61bdaSAdrian Bunk static int atapi_dmadir = 0;
129c6fd2807SJeff Garzik module_param(atapi_dmadir, int, 0444);
130ad5d8eacSEvgeni Golov MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off [default], 1=on)");
131c6fd2807SJeff Garzik 
132baf4fdfaSMark Lord int atapi_passthru16 = 1;
133baf4fdfaSMark Lord module_param(atapi_passthru16, int, 0444);
134ad5d8eacSEvgeni Golov MODULE_PARM_DESC(atapi_passthru16, "Enable ATA_16 passthru for ATAPI devices (0=off, 1=on [default])");
135baf4fdfaSMark Lord 
136c6fd2807SJeff Garzik int libata_fua = 0;
137c6fd2807SJeff Garzik module_param_named(fua, libata_fua, int, 0444);
138ad5d8eacSEvgeni Golov MODULE_PARM_DESC(fua, "FUA support (0=off [default], 1=on)");
139c6fd2807SJeff Garzik 
1402dcb407eSJeff Garzik static int ata_ignore_hpa;
1411e999736SAlan Cox module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644);
1421e999736SAlan Cox MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)");
1431e999736SAlan Cox 
144b3a70601SAlan Cox static int libata_dma_mask = ATA_DMA_MASK_ATA|ATA_DMA_MASK_ATAPI|ATA_DMA_MASK_CFA;
145b3a70601SAlan Cox module_param_named(dma, libata_dma_mask, int, 0444);
146b3a70601SAlan Cox MODULE_PARM_DESC(dma, "DMA enable/disable (0x1==ATA, 0x2==ATAPI, 0x4==CF)");
147b3a70601SAlan Cox 
14887fbc5a0STejun Heo static int ata_probe_timeout;
149c6fd2807SJeff Garzik module_param(ata_probe_timeout, int, 0444);
150c6fd2807SJeff Garzik MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
151c6fd2807SJeff Garzik 
1526ebe9d86SJeff Garzik int libata_noacpi = 0;
153d7d0dad6SJeff Garzik module_param_named(noacpi, libata_noacpi, int, 0444);
154ad5d8eacSEvgeni Golov MODULE_PARM_DESC(noacpi, "Disable the use of ACPI in probe/suspend/resume (0=off [default], 1=on)");
15511ef697bSKristen Carlson Accardi 
156ae8d4ee7SAlan Cox int libata_allow_tpm = 0;
157ae8d4ee7SAlan Cox module_param_named(allow_tpm, libata_allow_tpm, int, 0444);
158ad5d8eacSEvgeni Golov MODULE_PARM_DESC(allow_tpm, "Permit the use of TPM commands (0=off [default], 1=on)");
159ae8d4ee7SAlan Cox 
160e7ecd435STejun Heo static int atapi_an;
161e7ecd435STejun Heo module_param(atapi_an, int, 0444);
162e7ecd435STejun Heo MODULE_PARM_DESC(atapi_an, "Enable ATAPI AN media presence notification (0=0ff [default], 1=on)");
163e7ecd435STejun Heo 
164c6fd2807SJeff Garzik MODULE_AUTHOR("Jeff Garzik");
165c6fd2807SJeff Garzik MODULE_DESCRIPTION("Library module for ATA devices");
166c6fd2807SJeff Garzik MODULE_LICENSE("GPL");
167c6fd2807SJeff Garzik MODULE_VERSION(DRV_VERSION);
168c6fd2807SJeff Garzik 
169c6fd2807SJeff Garzik 
1709913ff8aSTejun Heo static bool ata_sstatus_online(u32 sstatus)
1719913ff8aSTejun Heo {
1729913ff8aSTejun Heo 	return (sstatus & 0xf) == 0x3;
1739913ff8aSTejun Heo }
1749913ff8aSTejun Heo 
1751eca4365STejun Heo /**
1761eca4365STejun Heo  *	ata_link_next - link iteration helper
1771eca4365STejun Heo  *	@link: the previous link, NULL to start
1781eca4365STejun Heo  *	@ap: ATA port containing links to iterate
1791eca4365STejun Heo  *	@mode: iteration mode, one of ATA_LITER_*
180aadffb68STejun Heo  *
181aadffb68STejun Heo  *	LOCKING:
182aadffb68STejun Heo  *	Host lock or EH context.
1831eca4365STejun Heo  *
1841eca4365STejun Heo  *	RETURNS:
1851eca4365STejun Heo  *	Pointer to the next link.
186aadffb68STejun Heo  */
1871eca4365STejun Heo struct ata_link *ata_link_next(struct ata_link *link, struct ata_port *ap,
1881eca4365STejun Heo 			       enum ata_link_iter_mode mode)
189aadffb68STejun Heo {
1901eca4365STejun Heo 	BUG_ON(mode != ATA_LITER_EDGE &&
1911eca4365STejun Heo 	       mode != ATA_LITER_PMP_FIRST && mode != ATA_LITER_HOST_FIRST);
1921eca4365STejun Heo 
193aadffb68STejun Heo 	/* NULL link indicates start of iteration */
1941eca4365STejun Heo 	if (!link)
1951eca4365STejun Heo 		switch (mode) {
1961eca4365STejun Heo 		case ATA_LITER_EDGE:
1971eca4365STejun Heo 		case ATA_LITER_PMP_FIRST:
1981eca4365STejun Heo 			if (sata_pmp_attached(ap))
199aadffb68STejun Heo 				return ap->pmp_link;
2001eca4365STejun Heo 			/* fall through */
2011eca4365STejun Heo 		case ATA_LITER_HOST_FIRST:
202aadffb68STejun Heo 			return &ap->link;
203aadffb68STejun Heo 		}
204aadffb68STejun Heo 
2051eca4365STejun Heo 	/* we just iterated over the host link, what's next? */
2061eca4365STejun Heo 	if (link == &ap->link)
2071eca4365STejun Heo 		switch (mode) {
2081eca4365STejun Heo 		case ATA_LITER_HOST_FIRST:
2091eca4365STejun Heo 			if (sata_pmp_attached(ap))
210aadffb68STejun Heo 				return ap->pmp_link;
2111eca4365STejun Heo 			/* fall through */
2121eca4365STejun Heo 		case ATA_LITER_PMP_FIRST:
2131eca4365STejun Heo 			if (unlikely(ap->slave_link))
2141eca4365STejun Heo 				return ap->slave_link;
2151eca4365STejun Heo 			/* fall through */
2161eca4365STejun Heo 		case ATA_LITER_EDGE:
2171eca4365STejun Heo 			return NULL;
218aadffb68STejun Heo 		}
219aadffb68STejun Heo 
220b1c72916STejun Heo 	/* slave_link excludes PMP */
221b1c72916STejun Heo 	if (unlikely(link == ap->slave_link))
222b1c72916STejun Heo 		return NULL;
223b1c72916STejun Heo 
2241eca4365STejun Heo 	/* we were over a PMP link */
225aadffb68STejun Heo 	if (++link < ap->pmp_link + ap->nr_pmp_links)
226aadffb68STejun Heo 		return link;
2271eca4365STejun Heo 
2281eca4365STejun Heo 	if (mode == ATA_LITER_PMP_FIRST)
2291eca4365STejun Heo 		return &ap->link;
2301eca4365STejun Heo 
231aadffb68STejun Heo 	return NULL;
232aadffb68STejun Heo }
233aadffb68STejun Heo 
234c6fd2807SJeff Garzik /**
2351eca4365STejun Heo  *	ata_dev_next - device iteration helper
2361eca4365STejun Heo  *	@dev: the previous device, NULL to start
2371eca4365STejun Heo  *	@link: ATA link containing devices to iterate
2381eca4365STejun Heo  *	@mode: iteration mode, one of ATA_DITER_*
2391eca4365STejun Heo  *
2401eca4365STejun Heo  *	LOCKING:
2411eca4365STejun Heo  *	Host lock or EH context.
2421eca4365STejun Heo  *
2431eca4365STejun Heo  *	RETURNS:
2441eca4365STejun Heo  *	Pointer to the next device.
2451eca4365STejun Heo  */
2461eca4365STejun Heo struct ata_device *ata_dev_next(struct ata_device *dev, struct ata_link *link,
2471eca4365STejun Heo 				enum ata_dev_iter_mode mode)
2481eca4365STejun Heo {
2491eca4365STejun Heo 	BUG_ON(mode != ATA_DITER_ENABLED && mode != ATA_DITER_ENABLED_REVERSE &&
2501eca4365STejun Heo 	       mode != ATA_DITER_ALL && mode != ATA_DITER_ALL_REVERSE);
2511eca4365STejun Heo 
2521eca4365STejun Heo 	/* NULL dev indicates start of iteration */
2531eca4365STejun Heo 	if (!dev)
2541eca4365STejun Heo 		switch (mode) {
2551eca4365STejun Heo 		case ATA_DITER_ENABLED:
2561eca4365STejun Heo 		case ATA_DITER_ALL:
2571eca4365STejun Heo 			dev = link->device;
2581eca4365STejun Heo 			goto check;
2591eca4365STejun Heo 		case ATA_DITER_ENABLED_REVERSE:
2601eca4365STejun Heo 		case ATA_DITER_ALL_REVERSE:
2611eca4365STejun Heo 			dev = link->device + ata_link_max_devices(link) - 1;
2621eca4365STejun Heo 			goto check;
2631eca4365STejun Heo 		}
2641eca4365STejun Heo 
2651eca4365STejun Heo  next:
2661eca4365STejun Heo 	/* move to the next one */
2671eca4365STejun Heo 	switch (mode) {
2681eca4365STejun Heo 	case ATA_DITER_ENABLED:
2691eca4365STejun Heo 	case ATA_DITER_ALL:
2701eca4365STejun Heo 		if (++dev < link->device + ata_link_max_devices(link))
2711eca4365STejun Heo 			goto check;
2721eca4365STejun Heo 		return NULL;
2731eca4365STejun Heo 	case ATA_DITER_ENABLED_REVERSE:
2741eca4365STejun Heo 	case ATA_DITER_ALL_REVERSE:
2751eca4365STejun Heo 		if (--dev >= link->device)
2761eca4365STejun Heo 			goto check;
2771eca4365STejun Heo 		return NULL;
2781eca4365STejun Heo 	}
2791eca4365STejun Heo 
2801eca4365STejun Heo  check:
2811eca4365STejun Heo 	if ((mode == ATA_DITER_ENABLED || mode == ATA_DITER_ENABLED_REVERSE) &&
2821eca4365STejun Heo 	    !ata_dev_enabled(dev))
2831eca4365STejun Heo 		goto next;
2841eca4365STejun Heo 	return dev;
2851eca4365STejun Heo }
2861eca4365STejun Heo 
2871eca4365STejun Heo /**
288b1c72916STejun Heo  *	ata_dev_phys_link - find physical link for a device
289b1c72916STejun Heo  *	@dev: ATA device to look up physical link for
290b1c72916STejun Heo  *
291b1c72916STejun Heo  *	Look up physical link which @dev is attached to.  Note that
292b1c72916STejun Heo  *	this is different from @dev->link only when @dev is on slave
293b1c72916STejun Heo  *	link.  For all other cases, it's the same as @dev->link.
294b1c72916STejun Heo  *
295b1c72916STejun Heo  *	LOCKING:
296b1c72916STejun Heo  *	Don't care.
297b1c72916STejun Heo  *
298b1c72916STejun Heo  *	RETURNS:
299b1c72916STejun Heo  *	Pointer to the found physical link.
300b1c72916STejun Heo  */
301b1c72916STejun Heo struct ata_link *ata_dev_phys_link(struct ata_device *dev)
302b1c72916STejun Heo {
303b1c72916STejun Heo 	struct ata_port *ap = dev->link->ap;
304b1c72916STejun Heo 
305b1c72916STejun Heo 	if (!ap->slave_link)
306b1c72916STejun Heo 		return dev->link;
307b1c72916STejun Heo 	if (!dev->devno)
308b1c72916STejun Heo 		return &ap->link;
309b1c72916STejun Heo 	return ap->slave_link;
310b1c72916STejun Heo }
311b1c72916STejun Heo 
312b1c72916STejun Heo /**
31333267325STejun Heo  *	ata_force_cbl - force cable type according to libata.force
3144cdfa1b3SRandy Dunlap  *	@ap: ATA port of interest
31533267325STejun Heo  *
31633267325STejun Heo  *	Force cable type according to libata.force and whine about it.
31733267325STejun Heo  *	The last entry which has matching port number is used, so it
31833267325STejun Heo  *	can be specified as part of device force parameters.  For
31933267325STejun Heo  *	example, both "a:40c,1.00:udma4" and "1.00:40c,udma4" have the
32033267325STejun Heo  *	same effect.
32133267325STejun Heo  *
32233267325STejun Heo  *	LOCKING:
32333267325STejun Heo  *	EH context.
32433267325STejun Heo  */
32533267325STejun Heo void ata_force_cbl(struct ata_port *ap)
32633267325STejun Heo {
32733267325STejun Heo 	int i;
32833267325STejun Heo 
32933267325STejun Heo 	for (i = ata_force_tbl_size - 1; i >= 0; i--) {
33033267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
33133267325STejun Heo 
33233267325STejun Heo 		if (fe->port != -1 && fe->port != ap->print_id)
33333267325STejun Heo 			continue;
33433267325STejun Heo 
33533267325STejun Heo 		if (fe->param.cbl == ATA_CBL_NONE)
33633267325STejun Heo 			continue;
33733267325STejun Heo 
33833267325STejun Heo 		ap->cbl = fe->param.cbl;
339a9a79dfeSJoe Perches 		ata_port_notice(ap, "FORCE: cable set to %s\n", fe->param.name);
34033267325STejun Heo 		return;
34133267325STejun Heo 	}
34233267325STejun Heo }
34333267325STejun Heo 
34433267325STejun Heo /**
34505944bdfSTejun Heo  *	ata_force_link_limits - force link limits according to libata.force
34633267325STejun Heo  *	@link: ATA link of interest
34733267325STejun Heo  *
34805944bdfSTejun Heo  *	Force link flags and SATA spd limit according to libata.force
34905944bdfSTejun Heo  *	and whine about it.  When only the port part is specified
35005944bdfSTejun Heo  *	(e.g. 1:), the limit applies to all links connected to both
35105944bdfSTejun Heo  *	the host link and all fan-out ports connected via PMP.  If the
35205944bdfSTejun Heo  *	device part is specified as 0 (e.g. 1.00:), it specifies the
35305944bdfSTejun Heo  *	first fan-out link not the host link.  Device number 15 always
354b1c72916STejun Heo  *	points to the host link whether PMP is attached or not.  If the
355b1c72916STejun Heo  *	controller has slave link, device number 16 points to it.
35633267325STejun Heo  *
35733267325STejun Heo  *	LOCKING:
35833267325STejun Heo  *	EH context.
35933267325STejun Heo  */
36005944bdfSTejun Heo static void ata_force_link_limits(struct ata_link *link)
36133267325STejun Heo {
36205944bdfSTejun Heo 	bool did_spd = false;
363b1c72916STejun Heo 	int linkno = link->pmp;
364b1c72916STejun Heo 	int i;
36533267325STejun Heo 
36633267325STejun Heo 	if (ata_is_host_link(link))
367b1c72916STejun Heo 		linkno += 15;
36833267325STejun Heo 
36933267325STejun Heo 	for (i = ata_force_tbl_size - 1; i >= 0; i--) {
37033267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
37133267325STejun Heo 
37233267325STejun Heo 		if (fe->port != -1 && fe->port != link->ap->print_id)
37333267325STejun Heo 			continue;
37433267325STejun Heo 
37533267325STejun Heo 		if (fe->device != -1 && fe->device != linkno)
37633267325STejun Heo 			continue;
37733267325STejun Heo 
37805944bdfSTejun Heo 		/* only honor the first spd limit */
37905944bdfSTejun Heo 		if (!did_spd && fe->param.spd_limit) {
38033267325STejun Heo 			link->hw_sata_spd_limit = (1 << fe->param.spd_limit) - 1;
381a9a79dfeSJoe Perches 			ata_link_notice(link, "FORCE: PHY spd limit set to %s\n",
38205944bdfSTejun Heo 					fe->param.name);
38305944bdfSTejun Heo 			did_spd = true;
38405944bdfSTejun Heo 		}
38505944bdfSTejun Heo 
38605944bdfSTejun Heo 		/* let lflags stack */
38705944bdfSTejun Heo 		if (fe->param.lflags) {
38805944bdfSTejun Heo 			link->flags |= fe->param.lflags;
389a9a79dfeSJoe Perches 			ata_link_notice(link,
39005944bdfSTejun Heo 					"FORCE: link flag 0x%x forced -> 0x%x\n",
39105944bdfSTejun Heo 					fe->param.lflags, link->flags);
39205944bdfSTejun Heo 		}
39333267325STejun Heo 	}
39433267325STejun Heo }
39533267325STejun Heo 
39633267325STejun Heo /**
39733267325STejun Heo  *	ata_force_xfermask - force xfermask according to libata.force
39833267325STejun Heo  *	@dev: ATA device of interest
39933267325STejun Heo  *
40033267325STejun Heo  *	Force xfer_mask according to libata.force and whine about it.
40133267325STejun Heo  *	For consistency with link selection, device number 15 selects
40233267325STejun Heo  *	the first device connected to the host link.
40333267325STejun Heo  *
40433267325STejun Heo  *	LOCKING:
40533267325STejun Heo  *	EH context.
40633267325STejun Heo  */
40733267325STejun Heo static void ata_force_xfermask(struct ata_device *dev)
40833267325STejun Heo {
40933267325STejun Heo 	int devno = dev->link->pmp + dev->devno;
41033267325STejun Heo 	int alt_devno = devno;
41133267325STejun Heo 	int i;
41233267325STejun Heo 
413b1c72916STejun Heo 	/* allow n.15/16 for devices attached to host port */
414b1c72916STejun Heo 	if (ata_is_host_link(dev->link))
415b1c72916STejun Heo 		alt_devno += 15;
41633267325STejun Heo 
41733267325STejun Heo 	for (i = ata_force_tbl_size - 1; i >= 0; i--) {
41833267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
41933267325STejun Heo 		unsigned long pio_mask, mwdma_mask, udma_mask;
42033267325STejun Heo 
42133267325STejun Heo 		if (fe->port != -1 && fe->port != dev->link->ap->print_id)
42233267325STejun Heo 			continue;
42333267325STejun Heo 
42433267325STejun Heo 		if (fe->device != -1 && fe->device != devno &&
42533267325STejun Heo 		    fe->device != alt_devno)
42633267325STejun Heo 			continue;
42733267325STejun Heo 
42833267325STejun Heo 		if (!fe->param.xfer_mask)
42933267325STejun Heo 			continue;
43033267325STejun Heo 
43133267325STejun Heo 		ata_unpack_xfermask(fe->param.xfer_mask,
43233267325STejun Heo 				    &pio_mask, &mwdma_mask, &udma_mask);
43333267325STejun Heo 		if (udma_mask)
43433267325STejun Heo 			dev->udma_mask = udma_mask;
43533267325STejun Heo 		else if (mwdma_mask) {
43633267325STejun Heo 			dev->udma_mask = 0;
43733267325STejun Heo 			dev->mwdma_mask = mwdma_mask;
43833267325STejun Heo 		} else {
43933267325STejun Heo 			dev->udma_mask = 0;
44033267325STejun Heo 			dev->mwdma_mask = 0;
44133267325STejun Heo 			dev->pio_mask = pio_mask;
44233267325STejun Heo 		}
44333267325STejun Heo 
444a9a79dfeSJoe Perches 		ata_dev_notice(dev, "FORCE: xfer_mask set to %s\n",
445a9a79dfeSJoe Perches 			       fe->param.name);
44633267325STejun Heo 		return;
44733267325STejun Heo 	}
44833267325STejun Heo }
44933267325STejun Heo 
45033267325STejun Heo /**
45133267325STejun Heo  *	ata_force_horkage - force horkage according to libata.force
45233267325STejun Heo  *	@dev: ATA device of interest
45333267325STejun Heo  *
45433267325STejun Heo  *	Force horkage according to libata.force and whine about it.
45533267325STejun Heo  *	For consistency with link selection, device number 15 selects
45633267325STejun Heo  *	the first device connected to the host link.
45733267325STejun Heo  *
45833267325STejun Heo  *	LOCKING:
45933267325STejun Heo  *	EH context.
46033267325STejun Heo  */
46133267325STejun Heo static void ata_force_horkage(struct ata_device *dev)
46233267325STejun Heo {
46333267325STejun Heo 	int devno = dev->link->pmp + dev->devno;
46433267325STejun Heo 	int alt_devno = devno;
46533267325STejun Heo 	int i;
46633267325STejun Heo 
467b1c72916STejun Heo 	/* allow n.15/16 for devices attached to host port */
468b1c72916STejun Heo 	if (ata_is_host_link(dev->link))
469b1c72916STejun Heo 		alt_devno += 15;
47033267325STejun Heo 
47133267325STejun Heo 	for (i = 0; i < ata_force_tbl_size; i++) {
47233267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
47333267325STejun Heo 
47433267325STejun Heo 		if (fe->port != -1 && fe->port != dev->link->ap->print_id)
47533267325STejun Heo 			continue;
47633267325STejun Heo 
47733267325STejun Heo 		if (fe->device != -1 && fe->device != devno &&
47833267325STejun Heo 		    fe->device != alt_devno)
47933267325STejun Heo 			continue;
48033267325STejun Heo 
48133267325STejun Heo 		if (!(~dev->horkage & fe->param.horkage_on) &&
48233267325STejun Heo 		    !(dev->horkage & fe->param.horkage_off))
48333267325STejun Heo 			continue;
48433267325STejun Heo 
48533267325STejun Heo 		dev->horkage |= fe->param.horkage_on;
48633267325STejun Heo 		dev->horkage &= ~fe->param.horkage_off;
48733267325STejun Heo 
488a9a79dfeSJoe Perches 		ata_dev_notice(dev, "FORCE: horkage modified (%s)\n",
489a9a79dfeSJoe Perches 			       fe->param.name);
49033267325STejun Heo 	}
49133267325STejun Heo }
49233267325STejun Heo 
49333267325STejun Heo /**
494436d34b3STejun Heo  *	atapi_cmd_type - Determine ATAPI command type from SCSI opcode
495436d34b3STejun Heo  *	@opcode: SCSI opcode
496436d34b3STejun Heo  *
497436d34b3STejun Heo  *	Determine ATAPI command type from @opcode.
498436d34b3STejun Heo  *
499436d34b3STejun Heo  *	LOCKING:
500436d34b3STejun Heo  *	None.
501436d34b3STejun Heo  *
502436d34b3STejun Heo  *	RETURNS:
503436d34b3STejun Heo  *	ATAPI_{READ|WRITE|READ_CD|PASS_THRU|MISC}
504436d34b3STejun Heo  */
505436d34b3STejun Heo int atapi_cmd_type(u8 opcode)
506436d34b3STejun Heo {
507436d34b3STejun Heo 	switch (opcode) {
508436d34b3STejun Heo 	case GPCMD_READ_10:
509436d34b3STejun Heo 	case GPCMD_READ_12:
510436d34b3STejun Heo 		return ATAPI_READ;
511436d34b3STejun Heo 
512436d34b3STejun Heo 	case GPCMD_WRITE_10:
513436d34b3STejun Heo 	case GPCMD_WRITE_12:
514436d34b3STejun Heo 	case GPCMD_WRITE_AND_VERIFY_10:
515436d34b3STejun Heo 		return ATAPI_WRITE;
516436d34b3STejun Heo 
517436d34b3STejun Heo 	case GPCMD_READ_CD:
518436d34b3STejun Heo 	case GPCMD_READ_CD_MSF:
519436d34b3STejun Heo 		return ATAPI_READ_CD;
520436d34b3STejun Heo 
521e52dcc48STejun Heo 	case ATA_16:
522e52dcc48STejun Heo 	case ATA_12:
523e52dcc48STejun Heo 		if (atapi_passthru16)
524e52dcc48STejun Heo 			return ATAPI_PASS_THRU;
525e52dcc48STejun Heo 		/* fall thru */
526436d34b3STejun Heo 	default:
527436d34b3STejun Heo 		return ATAPI_MISC;
528436d34b3STejun Heo 	}
529436d34b3STejun Heo }
530436d34b3STejun Heo 
531436d34b3STejun Heo /**
532c6fd2807SJeff Garzik  *	ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
533c6fd2807SJeff Garzik  *	@tf: Taskfile to convert
534c6fd2807SJeff Garzik  *	@pmp: Port multiplier port
5359977126cSTejun Heo  *	@is_cmd: This FIS is for command
5369977126cSTejun Heo  *	@fis: Buffer into which data will output
537c6fd2807SJeff Garzik  *
538c6fd2807SJeff Garzik  *	Converts a standard ATA taskfile to a Serial ATA
539c6fd2807SJeff Garzik  *	FIS structure (Register - Host to Device).
540c6fd2807SJeff Garzik  *
541c6fd2807SJeff Garzik  *	LOCKING:
542c6fd2807SJeff Garzik  *	Inherited from caller.
543c6fd2807SJeff Garzik  */
5449977126cSTejun Heo void ata_tf_to_fis(const struct ata_taskfile *tf, u8 pmp, int is_cmd, u8 *fis)
545c6fd2807SJeff Garzik {
546c6fd2807SJeff Garzik 	fis[0] = 0x27;			/* Register - Host to Device FIS */
5479977126cSTejun Heo 	fis[1] = pmp & 0xf;		/* Port multiplier number*/
5489977126cSTejun Heo 	if (is_cmd)
5499977126cSTejun Heo 		fis[1] |= (1 << 7);	/* bit 7 indicates Command FIS */
5509977126cSTejun Heo 
551c6fd2807SJeff Garzik 	fis[2] = tf->command;
552c6fd2807SJeff Garzik 	fis[3] = tf->feature;
553c6fd2807SJeff Garzik 
554c6fd2807SJeff Garzik 	fis[4] = tf->lbal;
555c6fd2807SJeff Garzik 	fis[5] = tf->lbam;
556c6fd2807SJeff Garzik 	fis[6] = tf->lbah;
557c6fd2807SJeff Garzik 	fis[7] = tf->device;
558c6fd2807SJeff Garzik 
559c6fd2807SJeff Garzik 	fis[8] = tf->hob_lbal;
560c6fd2807SJeff Garzik 	fis[9] = tf->hob_lbam;
561c6fd2807SJeff Garzik 	fis[10] = tf->hob_lbah;
562c6fd2807SJeff Garzik 	fis[11] = tf->hob_feature;
563c6fd2807SJeff Garzik 
564c6fd2807SJeff Garzik 	fis[12] = tf->nsect;
565c6fd2807SJeff Garzik 	fis[13] = tf->hob_nsect;
566c6fd2807SJeff Garzik 	fis[14] = 0;
567c6fd2807SJeff Garzik 	fis[15] = tf->ctl;
568c6fd2807SJeff Garzik 
569c6fd2807SJeff Garzik 	fis[16] = 0;
570c6fd2807SJeff Garzik 	fis[17] = 0;
571c6fd2807SJeff Garzik 	fis[18] = 0;
572c6fd2807SJeff Garzik 	fis[19] = 0;
573c6fd2807SJeff Garzik }
574c6fd2807SJeff Garzik 
575c6fd2807SJeff Garzik /**
576c6fd2807SJeff Garzik  *	ata_tf_from_fis - Convert SATA FIS to ATA taskfile
577c6fd2807SJeff Garzik  *	@fis: Buffer from which data will be input
578c6fd2807SJeff Garzik  *	@tf: Taskfile to output
579c6fd2807SJeff Garzik  *
580c6fd2807SJeff Garzik  *	Converts a serial ATA FIS structure to a standard ATA taskfile.
581c6fd2807SJeff Garzik  *
582c6fd2807SJeff Garzik  *	LOCKING:
583c6fd2807SJeff Garzik  *	Inherited from caller.
584c6fd2807SJeff Garzik  */
585c6fd2807SJeff Garzik 
586c6fd2807SJeff Garzik void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
587c6fd2807SJeff Garzik {
588c6fd2807SJeff Garzik 	tf->command	= fis[2];	/* status */
589c6fd2807SJeff Garzik 	tf->feature	= fis[3];	/* error */
590c6fd2807SJeff Garzik 
591c6fd2807SJeff Garzik 	tf->lbal	= fis[4];
592c6fd2807SJeff Garzik 	tf->lbam	= fis[5];
593c6fd2807SJeff Garzik 	tf->lbah	= fis[6];
594c6fd2807SJeff Garzik 	tf->device	= fis[7];
595c6fd2807SJeff Garzik 
596c6fd2807SJeff Garzik 	tf->hob_lbal	= fis[8];
597c6fd2807SJeff Garzik 	tf->hob_lbam	= fis[9];
598c6fd2807SJeff Garzik 	tf->hob_lbah	= fis[10];
599c6fd2807SJeff Garzik 
600c6fd2807SJeff Garzik 	tf->nsect	= fis[12];
601c6fd2807SJeff Garzik 	tf->hob_nsect	= fis[13];
602c6fd2807SJeff Garzik }
603c6fd2807SJeff Garzik 
604c6fd2807SJeff Garzik static const u8 ata_rw_cmds[] = {
605c6fd2807SJeff Garzik 	/* pio multi */
606c6fd2807SJeff Garzik 	ATA_CMD_READ_MULTI,
607c6fd2807SJeff Garzik 	ATA_CMD_WRITE_MULTI,
608c6fd2807SJeff Garzik 	ATA_CMD_READ_MULTI_EXT,
609c6fd2807SJeff Garzik 	ATA_CMD_WRITE_MULTI_EXT,
610c6fd2807SJeff Garzik 	0,
611c6fd2807SJeff Garzik 	0,
612c6fd2807SJeff Garzik 	0,
613c6fd2807SJeff Garzik 	ATA_CMD_WRITE_MULTI_FUA_EXT,
614c6fd2807SJeff Garzik 	/* pio */
615c6fd2807SJeff Garzik 	ATA_CMD_PIO_READ,
616c6fd2807SJeff Garzik 	ATA_CMD_PIO_WRITE,
617c6fd2807SJeff Garzik 	ATA_CMD_PIO_READ_EXT,
618c6fd2807SJeff Garzik 	ATA_CMD_PIO_WRITE_EXT,
619c6fd2807SJeff Garzik 	0,
620c6fd2807SJeff Garzik 	0,
621c6fd2807SJeff Garzik 	0,
622c6fd2807SJeff Garzik 	0,
623c6fd2807SJeff Garzik 	/* dma */
624c6fd2807SJeff Garzik 	ATA_CMD_READ,
625c6fd2807SJeff Garzik 	ATA_CMD_WRITE,
626c6fd2807SJeff Garzik 	ATA_CMD_READ_EXT,
627c6fd2807SJeff Garzik 	ATA_CMD_WRITE_EXT,
628c6fd2807SJeff Garzik 	0,
629c6fd2807SJeff Garzik 	0,
630c6fd2807SJeff Garzik 	0,
631c6fd2807SJeff Garzik 	ATA_CMD_WRITE_FUA_EXT
632c6fd2807SJeff Garzik };
633c6fd2807SJeff Garzik 
634c6fd2807SJeff Garzik /**
635c6fd2807SJeff Garzik  *	ata_rwcmd_protocol - set taskfile r/w commands and protocol
636bd056d7eSTejun Heo  *	@tf: command to examine and configure
637bd056d7eSTejun Heo  *	@dev: device tf belongs to
638c6fd2807SJeff Garzik  *
639c6fd2807SJeff Garzik  *	Examine the device configuration and tf->flags to calculate
640c6fd2807SJeff Garzik  *	the proper read/write commands and protocol to use.
641c6fd2807SJeff Garzik  *
642c6fd2807SJeff Garzik  *	LOCKING:
643c6fd2807SJeff Garzik  *	caller.
644c6fd2807SJeff Garzik  */
645bd056d7eSTejun Heo static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev)
646c6fd2807SJeff Garzik {
647c6fd2807SJeff Garzik 	u8 cmd;
648c6fd2807SJeff Garzik 
649c6fd2807SJeff Garzik 	int index, fua, lba48, write;
650c6fd2807SJeff Garzik 
651c6fd2807SJeff Garzik 	fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
652c6fd2807SJeff Garzik 	lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
653c6fd2807SJeff Garzik 	write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
654c6fd2807SJeff Garzik 
655c6fd2807SJeff Garzik 	if (dev->flags & ATA_DFLAG_PIO) {
656c6fd2807SJeff Garzik 		tf->protocol = ATA_PROT_PIO;
657c6fd2807SJeff Garzik 		index = dev->multi_count ? 0 : 8;
6589af5c9c9STejun Heo 	} else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) {
659c6fd2807SJeff Garzik 		/* Unable to use DMA due to host limitation */
660c6fd2807SJeff Garzik 		tf->protocol = ATA_PROT_PIO;
661c6fd2807SJeff Garzik 		index = dev->multi_count ? 0 : 8;
662c6fd2807SJeff Garzik 	} else {
663c6fd2807SJeff Garzik 		tf->protocol = ATA_PROT_DMA;
664c6fd2807SJeff Garzik 		index = 16;
665c6fd2807SJeff Garzik 	}
666c6fd2807SJeff Garzik 
667c6fd2807SJeff Garzik 	cmd = ata_rw_cmds[index + fua + lba48 + write];
668c6fd2807SJeff Garzik 	if (cmd) {
669c6fd2807SJeff Garzik 		tf->command = cmd;
670c6fd2807SJeff Garzik 		return 0;
671c6fd2807SJeff Garzik 	}
672c6fd2807SJeff Garzik 	return -1;
673c6fd2807SJeff Garzik }
674c6fd2807SJeff Garzik 
675c6fd2807SJeff Garzik /**
67635b649feSTejun Heo  *	ata_tf_read_block - Read block address from ATA taskfile
67735b649feSTejun Heo  *	@tf: ATA taskfile of interest
67835b649feSTejun Heo  *	@dev: ATA device @tf belongs to
67935b649feSTejun Heo  *
68035b649feSTejun Heo  *	LOCKING:
68135b649feSTejun Heo  *	None.
68235b649feSTejun Heo  *
68335b649feSTejun Heo  *	Read block address from @tf.  This function can handle all
68435b649feSTejun Heo  *	three address formats - LBA, LBA48 and CHS.  tf->protocol and
68535b649feSTejun Heo  *	flags select the address format to use.
68635b649feSTejun Heo  *
68735b649feSTejun Heo  *	RETURNS:
68835b649feSTejun Heo  *	Block address read from @tf.
68935b649feSTejun Heo  */
69035b649feSTejun Heo u64 ata_tf_read_block(struct ata_taskfile *tf, struct ata_device *dev)
69135b649feSTejun Heo {
69235b649feSTejun Heo 	u64 block = 0;
69335b649feSTejun Heo 
69435b649feSTejun Heo 	if (tf->flags & ATA_TFLAG_LBA) {
69535b649feSTejun Heo 		if (tf->flags & ATA_TFLAG_LBA48) {
69635b649feSTejun Heo 			block |= (u64)tf->hob_lbah << 40;
69735b649feSTejun Heo 			block |= (u64)tf->hob_lbam << 32;
69844901a96SRoland Dreier 			block |= (u64)tf->hob_lbal << 24;
69935b649feSTejun Heo 		} else
70035b649feSTejun Heo 			block |= (tf->device & 0xf) << 24;
70135b649feSTejun Heo 
70235b649feSTejun Heo 		block |= tf->lbah << 16;
70335b649feSTejun Heo 		block |= tf->lbam << 8;
70435b649feSTejun Heo 		block |= tf->lbal;
70535b649feSTejun Heo 	} else {
70635b649feSTejun Heo 		u32 cyl, head, sect;
70735b649feSTejun Heo 
70835b649feSTejun Heo 		cyl = tf->lbam | (tf->lbah << 8);
70935b649feSTejun Heo 		head = tf->device & 0xf;
71035b649feSTejun Heo 		sect = tf->lbal;
71135b649feSTejun Heo 
712ac8672eaSTejun Heo 		if (!sect) {
713a9a79dfeSJoe Perches 			ata_dev_warn(dev,
714a9a79dfeSJoe Perches 				     "device reported invalid CHS sector 0\n");
715ac8672eaSTejun Heo 			sect = 1; /* oh well */
716ac8672eaSTejun Heo 		}
717ac8672eaSTejun Heo 
718ac8672eaSTejun Heo 		block = (cyl * dev->heads + head) * dev->sectors + sect - 1;
71935b649feSTejun Heo 	}
72035b649feSTejun Heo 
72135b649feSTejun Heo 	return block;
72235b649feSTejun Heo }
72335b649feSTejun Heo 
72435b649feSTejun Heo /**
725bd056d7eSTejun Heo  *	ata_build_rw_tf - Build ATA taskfile for given read/write request
726bd056d7eSTejun Heo  *	@tf: Target ATA taskfile
727bd056d7eSTejun Heo  *	@dev: ATA device @tf belongs to
728bd056d7eSTejun Heo  *	@block: Block address
729bd056d7eSTejun Heo  *	@n_block: Number of blocks
730bd056d7eSTejun Heo  *	@tf_flags: RW/FUA etc...
731bd056d7eSTejun Heo  *	@tag: tag
732bd056d7eSTejun Heo  *
733bd056d7eSTejun Heo  *	LOCKING:
734bd056d7eSTejun Heo  *	None.
735bd056d7eSTejun Heo  *
736bd056d7eSTejun Heo  *	Build ATA taskfile @tf for read/write request described by
737bd056d7eSTejun Heo  *	@block, @n_block, @tf_flags and @tag on @dev.
738bd056d7eSTejun Heo  *
739bd056d7eSTejun Heo  *	RETURNS:
740bd056d7eSTejun Heo  *
741bd056d7eSTejun Heo  *	0 on success, -ERANGE if the request is too large for @dev,
742bd056d7eSTejun Heo  *	-EINVAL if the request is invalid.
743bd056d7eSTejun Heo  */
744bd056d7eSTejun Heo int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev,
745bd056d7eSTejun Heo 		    u64 block, u32 n_block, unsigned int tf_flags,
746bd056d7eSTejun Heo 		    unsigned int tag)
747bd056d7eSTejun Heo {
748bd056d7eSTejun Heo 	tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
749bd056d7eSTejun Heo 	tf->flags |= tf_flags;
750bd056d7eSTejun Heo 
7516d1245bfSTejun Heo 	if (ata_ncq_enabled(dev) && likely(tag != ATA_TAG_INTERNAL)) {
752bd056d7eSTejun Heo 		/* yay, NCQ */
753bd056d7eSTejun Heo 		if (!lba_48_ok(block, n_block))
754bd056d7eSTejun Heo 			return -ERANGE;
755bd056d7eSTejun Heo 
756bd056d7eSTejun Heo 		tf->protocol = ATA_PROT_NCQ;
757bd056d7eSTejun Heo 		tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
758bd056d7eSTejun Heo 
759bd056d7eSTejun Heo 		if (tf->flags & ATA_TFLAG_WRITE)
760bd056d7eSTejun Heo 			tf->command = ATA_CMD_FPDMA_WRITE;
761bd056d7eSTejun Heo 		else
762bd056d7eSTejun Heo 			tf->command = ATA_CMD_FPDMA_READ;
763bd056d7eSTejun Heo 
764bd056d7eSTejun Heo 		tf->nsect = tag << 3;
765bd056d7eSTejun Heo 		tf->hob_feature = (n_block >> 8) & 0xff;
766bd056d7eSTejun Heo 		tf->feature = n_block & 0xff;
767bd056d7eSTejun Heo 
768bd056d7eSTejun Heo 		tf->hob_lbah = (block >> 40) & 0xff;
769bd056d7eSTejun Heo 		tf->hob_lbam = (block >> 32) & 0xff;
770bd056d7eSTejun Heo 		tf->hob_lbal = (block >> 24) & 0xff;
771bd056d7eSTejun Heo 		tf->lbah = (block >> 16) & 0xff;
772bd056d7eSTejun Heo 		tf->lbam = (block >> 8) & 0xff;
773bd056d7eSTejun Heo 		tf->lbal = block & 0xff;
774bd056d7eSTejun Heo 
775bd056d7eSTejun Heo 		tf->device = 1 << 6;
776bd056d7eSTejun Heo 		if (tf->flags & ATA_TFLAG_FUA)
777bd056d7eSTejun Heo 			tf->device |= 1 << 7;
778bd056d7eSTejun Heo 	} else if (dev->flags & ATA_DFLAG_LBA) {
779bd056d7eSTejun Heo 		tf->flags |= ATA_TFLAG_LBA;
780bd056d7eSTejun Heo 
781bd056d7eSTejun Heo 		if (lba_28_ok(block, n_block)) {
782bd056d7eSTejun Heo 			/* use LBA28 */
783bd056d7eSTejun Heo 			tf->device |= (block >> 24) & 0xf;
784bd056d7eSTejun Heo 		} else if (lba_48_ok(block, n_block)) {
785bd056d7eSTejun Heo 			if (!(dev->flags & ATA_DFLAG_LBA48))
786bd056d7eSTejun Heo 				return -ERANGE;
787bd056d7eSTejun Heo 
788bd056d7eSTejun Heo 			/* use LBA48 */
789bd056d7eSTejun Heo 			tf->flags |= ATA_TFLAG_LBA48;
790bd056d7eSTejun Heo 
791bd056d7eSTejun Heo 			tf->hob_nsect = (n_block >> 8) & 0xff;
792bd056d7eSTejun Heo 
793bd056d7eSTejun Heo 			tf->hob_lbah = (block >> 40) & 0xff;
794bd056d7eSTejun Heo 			tf->hob_lbam = (block >> 32) & 0xff;
795bd056d7eSTejun Heo 			tf->hob_lbal = (block >> 24) & 0xff;
796bd056d7eSTejun Heo 		} else
797bd056d7eSTejun Heo 			/* request too large even for LBA48 */
798bd056d7eSTejun Heo 			return -ERANGE;
799bd056d7eSTejun Heo 
800bd056d7eSTejun Heo 		if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
801bd056d7eSTejun Heo 			return -EINVAL;
802bd056d7eSTejun Heo 
803bd056d7eSTejun Heo 		tf->nsect = n_block & 0xff;
804bd056d7eSTejun Heo 
805bd056d7eSTejun Heo 		tf->lbah = (block >> 16) & 0xff;
806bd056d7eSTejun Heo 		tf->lbam = (block >> 8) & 0xff;
807bd056d7eSTejun Heo 		tf->lbal = block & 0xff;
808bd056d7eSTejun Heo 
809bd056d7eSTejun Heo 		tf->device |= ATA_LBA;
810bd056d7eSTejun Heo 	} else {
811bd056d7eSTejun Heo 		/* CHS */
812bd056d7eSTejun Heo 		u32 sect, head, cyl, track;
813bd056d7eSTejun Heo 
814bd056d7eSTejun Heo 		/* The request -may- be too large for CHS addressing. */
815bd056d7eSTejun Heo 		if (!lba_28_ok(block, n_block))
816bd056d7eSTejun Heo 			return -ERANGE;
817bd056d7eSTejun Heo 
818bd056d7eSTejun Heo 		if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
819bd056d7eSTejun Heo 			return -EINVAL;
820bd056d7eSTejun Heo 
821bd056d7eSTejun Heo 		/* Convert LBA to CHS */
822bd056d7eSTejun Heo 		track = (u32)block / dev->sectors;
823bd056d7eSTejun Heo 		cyl   = track / dev->heads;
824bd056d7eSTejun Heo 		head  = track % dev->heads;
825bd056d7eSTejun Heo 		sect  = (u32)block % dev->sectors + 1;
826bd056d7eSTejun Heo 
827bd056d7eSTejun Heo 		DPRINTK("block %u track %u cyl %u head %u sect %u\n",
828bd056d7eSTejun Heo 			(u32)block, track, cyl, head, sect);
829bd056d7eSTejun Heo 
830bd056d7eSTejun Heo 		/* Check whether the converted CHS can fit.
831bd056d7eSTejun Heo 		   Cylinder: 0-65535
832bd056d7eSTejun Heo 		   Head: 0-15
833bd056d7eSTejun Heo 		   Sector: 1-255*/
834bd056d7eSTejun Heo 		if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
835bd056d7eSTejun Heo 			return -ERANGE;
836bd056d7eSTejun Heo 
837bd056d7eSTejun Heo 		tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
838bd056d7eSTejun Heo 		tf->lbal = sect;
839bd056d7eSTejun Heo 		tf->lbam = cyl;
840bd056d7eSTejun Heo 		tf->lbah = cyl >> 8;
841bd056d7eSTejun Heo 		tf->device |= head;
842bd056d7eSTejun Heo 	}
843bd056d7eSTejun Heo 
844bd056d7eSTejun Heo 	return 0;
845bd056d7eSTejun Heo }
846bd056d7eSTejun Heo 
847bd056d7eSTejun Heo /**
848c6fd2807SJeff Garzik  *	ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
849c6fd2807SJeff Garzik  *	@pio_mask: pio_mask
850c6fd2807SJeff Garzik  *	@mwdma_mask: mwdma_mask
851c6fd2807SJeff Garzik  *	@udma_mask: udma_mask
852c6fd2807SJeff Garzik  *
853c6fd2807SJeff Garzik  *	Pack @pio_mask, @mwdma_mask and @udma_mask into a single
854c6fd2807SJeff Garzik  *	unsigned int xfer_mask.
855c6fd2807SJeff Garzik  *
856c6fd2807SJeff Garzik  *	LOCKING:
857c6fd2807SJeff Garzik  *	None.
858c6fd2807SJeff Garzik  *
859c6fd2807SJeff Garzik  *	RETURNS:
860c6fd2807SJeff Garzik  *	Packed xfer_mask.
861c6fd2807SJeff Garzik  */
8627dc951aeSTejun Heo unsigned long ata_pack_xfermask(unsigned long pio_mask,
8637dc951aeSTejun Heo 				unsigned long mwdma_mask,
8647dc951aeSTejun Heo 				unsigned long udma_mask)
865c6fd2807SJeff Garzik {
866c6fd2807SJeff Garzik 	return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
867c6fd2807SJeff Garzik 		((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
868c6fd2807SJeff Garzik 		((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
869c6fd2807SJeff Garzik }
870c6fd2807SJeff Garzik 
871c6fd2807SJeff Garzik /**
872c6fd2807SJeff Garzik  *	ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
873c6fd2807SJeff Garzik  *	@xfer_mask: xfer_mask to unpack
874c6fd2807SJeff Garzik  *	@pio_mask: resulting pio_mask
875c6fd2807SJeff Garzik  *	@mwdma_mask: resulting mwdma_mask
876c6fd2807SJeff Garzik  *	@udma_mask: resulting udma_mask
877c6fd2807SJeff Garzik  *
878c6fd2807SJeff Garzik  *	Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
879c6fd2807SJeff Garzik  *	Any NULL distination masks will be ignored.
880c6fd2807SJeff Garzik  */
8817dc951aeSTejun Heo void ata_unpack_xfermask(unsigned long xfer_mask, unsigned long *pio_mask,
8827dc951aeSTejun Heo 			 unsigned long *mwdma_mask, unsigned long *udma_mask)
883c6fd2807SJeff Garzik {
884c6fd2807SJeff Garzik 	if (pio_mask)
885c6fd2807SJeff Garzik 		*pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
886c6fd2807SJeff Garzik 	if (mwdma_mask)
887c6fd2807SJeff Garzik 		*mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
888c6fd2807SJeff Garzik 	if (udma_mask)
889c6fd2807SJeff Garzik 		*udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
890c6fd2807SJeff Garzik }
891c6fd2807SJeff Garzik 
892c6fd2807SJeff Garzik static const struct ata_xfer_ent {
893c6fd2807SJeff Garzik 	int shift, bits;
894c6fd2807SJeff Garzik 	u8 base;
895c6fd2807SJeff Garzik } ata_xfer_tbl[] = {
89670cd071eSTejun Heo 	{ ATA_SHIFT_PIO, ATA_NR_PIO_MODES, XFER_PIO_0 },
89770cd071eSTejun Heo 	{ ATA_SHIFT_MWDMA, ATA_NR_MWDMA_MODES, XFER_MW_DMA_0 },
89870cd071eSTejun Heo 	{ ATA_SHIFT_UDMA, ATA_NR_UDMA_MODES, XFER_UDMA_0 },
899c6fd2807SJeff Garzik 	{ -1, },
900c6fd2807SJeff Garzik };
901c6fd2807SJeff Garzik 
902c6fd2807SJeff Garzik /**
903c6fd2807SJeff Garzik  *	ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
904c6fd2807SJeff Garzik  *	@xfer_mask: xfer_mask of interest
905c6fd2807SJeff Garzik  *
906c6fd2807SJeff Garzik  *	Return matching XFER_* value for @xfer_mask.  Only the highest
907c6fd2807SJeff Garzik  *	bit of @xfer_mask is considered.
908c6fd2807SJeff Garzik  *
909c6fd2807SJeff Garzik  *	LOCKING:
910c6fd2807SJeff Garzik  *	None.
911c6fd2807SJeff Garzik  *
912c6fd2807SJeff Garzik  *	RETURNS:
91370cd071eSTejun Heo  *	Matching XFER_* value, 0xff if no match found.
914c6fd2807SJeff Garzik  */
9157dc951aeSTejun Heo u8 ata_xfer_mask2mode(unsigned long xfer_mask)
916c6fd2807SJeff Garzik {
917c6fd2807SJeff Garzik 	int highbit = fls(xfer_mask) - 1;
918c6fd2807SJeff Garzik 	const struct ata_xfer_ent *ent;
919c6fd2807SJeff Garzik 
920c6fd2807SJeff Garzik 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
921c6fd2807SJeff Garzik 		if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
922c6fd2807SJeff Garzik 			return ent->base + highbit - ent->shift;
92370cd071eSTejun Heo 	return 0xff;
924c6fd2807SJeff Garzik }
925c6fd2807SJeff Garzik 
926c6fd2807SJeff Garzik /**
927c6fd2807SJeff Garzik  *	ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
928c6fd2807SJeff Garzik  *	@xfer_mode: XFER_* of interest
929c6fd2807SJeff Garzik  *
930c6fd2807SJeff Garzik  *	Return matching xfer_mask for @xfer_mode.
931c6fd2807SJeff Garzik  *
932c6fd2807SJeff Garzik  *	LOCKING:
933c6fd2807SJeff Garzik  *	None.
934c6fd2807SJeff Garzik  *
935c6fd2807SJeff Garzik  *	RETURNS:
936c6fd2807SJeff Garzik  *	Matching xfer_mask, 0 if no match found.
937c6fd2807SJeff Garzik  */
9387dc951aeSTejun Heo unsigned long ata_xfer_mode2mask(u8 xfer_mode)
939c6fd2807SJeff Garzik {
940c6fd2807SJeff Garzik 	const struct ata_xfer_ent *ent;
941c6fd2807SJeff Garzik 
942c6fd2807SJeff Garzik 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
943c6fd2807SJeff Garzik 		if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
94470cd071eSTejun Heo 			return ((2 << (ent->shift + xfer_mode - ent->base)) - 1)
94570cd071eSTejun Heo 				& ~((1 << ent->shift) - 1);
946c6fd2807SJeff Garzik 	return 0;
947c6fd2807SJeff Garzik }
948c6fd2807SJeff Garzik 
949c6fd2807SJeff Garzik /**
950c6fd2807SJeff Garzik  *	ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
951c6fd2807SJeff Garzik  *	@xfer_mode: XFER_* of interest
952c6fd2807SJeff Garzik  *
953c6fd2807SJeff Garzik  *	Return matching xfer_shift for @xfer_mode.
954c6fd2807SJeff Garzik  *
955c6fd2807SJeff Garzik  *	LOCKING:
956c6fd2807SJeff Garzik  *	None.
957c6fd2807SJeff Garzik  *
958c6fd2807SJeff Garzik  *	RETURNS:
959c6fd2807SJeff Garzik  *	Matching xfer_shift, -1 if no match found.
960c6fd2807SJeff Garzik  */
9617dc951aeSTejun Heo int ata_xfer_mode2shift(unsigned long xfer_mode)
962c6fd2807SJeff Garzik {
963c6fd2807SJeff Garzik 	const struct ata_xfer_ent *ent;
964c6fd2807SJeff Garzik 
965c6fd2807SJeff Garzik 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
966c6fd2807SJeff Garzik 		if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
967c6fd2807SJeff Garzik 			return ent->shift;
968c6fd2807SJeff Garzik 	return -1;
969c6fd2807SJeff Garzik }
970c6fd2807SJeff Garzik 
971c6fd2807SJeff Garzik /**
972c6fd2807SJeff Garzik  *	ata_mode_string - convert xfer_mask to string
973c6fd2807SJeff Garzik  *	@xfer_mask: mask of bits supported; only highest bit counts.
974c6fd2807SJeff Garzik  *
975c6fd2807SJeff Garzik  *	Determine string which represents the highest speed
976c6fd2807SJeff Garzik  *	(highest bit in @modemask).
977c6fd2807SJeff Garzik  *
978c6fd2807SJeff Garzik  *	LOCKING:
979c6fd2807SJeff Garzik  *	None.
980c6fd2807SJeff Garzik  *
981c6fd2807SJeff Garzik  *	RETURNS:
982c6fd2807SJeff Garzik  *	Constant C string representing highest speed listed in
983c6fd2807SJeff Garzik  *	@mode_mask, or the constant C string "<n/a>".
984c6fd2807SJeff Garzik  */
9857dc951aeSTejun Heo const char *ata_mode_string(unsigned long xfer_mask)
986c6fd2807SJeff Garzik {
987c6fd2807SJeff Garzik 	static const char * const xfer_mode_str[] = {
988c6fd2807SJeff Garzik 		"PIO0",
989c6fd2807SJeff Garzik 		"PIO1",
990c6fd2807SJeff Garzik 		"PIO2",
991c6fd2807SJeff Garzik 		"PIO3",
992c6fd2807SJeff Garzik 		"PIO4",
993b352e57dSAlan Cox 		"PIO5",
994b352e57dSAlan Cox 		"PIO6",
995c6fd2807SJeff Garzik 		"MWDMA0",
996c6fd2807SJeff Garzik 		"MWDMA1",
997c6fd2807SJeff Garzik 		"MWDMA2",
998b352e57dSAlan Cox 		"MWDMA3",
999b352e57dSAlan Cox 		"MWDMA4",
1000c6fd2807SJeff Garzik 		"UDMA/16",
1001c6fd2807SJeff Garzik 		"UDMA/25",
1002c6fd2807SJeff Garzik 		"UDMA/33",
1003c6fd2807SJeff Garzik 		"UDMA/44",
1004c6fd2807SJeff Garzik 		"UDMA/66",
1005c6fd2807SJeff Garzik 		"UDMA/100",
1006c6fd2807SJeff Garzik 		"UDMA/133",
1007c6fd2807SJeff Garzik 		"UDMA7",
1008c6fd2807SJeff Garzik 	};
1009c6fd2807SJeff Garzik 	int highbit;
1010c6fd2807SJeff Garzik 
1011c6fd2807SJeff Garzik 	highbit = fls(xfer_mask) - 1;
1012c6fd2807SJeff Garzik 	if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
1013c6fd2807SJeff Garzik 		return xfer_mode_str[highbit];
1014c6fd2807SJeff Garzik 	return "<n/a>";
1015c6fd2807SJeff Garzik }
1016c6fd2807SJeff Garzik 
1017d9027470SGwendal Grignou const char *sata_spd_string(unsigned int spd)
1018c6fd2807SJeff Garzik {
1019c6fd2807SJeff Garzik 	static const char * const spd_str[] = {
1020c6fd2807SJeff Garzik 		"1.5 Gbps",
1021c6fd2807SJeff Garzik 		"3.0 Gbps",
10228522ee25SShane Huang 		"6.0 Gbps",
1023c6fd2807SJeff Garzik 	};
1024c6fd2807SJeff Garzik 
1025c6fd2807SJeff Garzik 	if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
1026c6fd2807SJeff Garzik 		return "<unknown>";
1027c6fd2807SJeff Garzik 	return spd_str[spd - 1];
1028c6fd2807SJeff Garzik }
1029c6fd2807SJeff Garzik 
1030c6fd2807SJeff Garzik /**
1031c6fd2807SJeff Garzik  *	ata_dev_classify - determine device type based on ATA-spec signature
1032c6fd2807SJeff Garzik  *	@tf: ATA taskfile register set for device to be identified
1033c6fd2807SJeff Garzik  *
1034c6fd2807SJeff Garzik  *	Determine from taskfile register contents whether a device is
1035c6fd2807SJeff Garzik  *	ATA or ATAPI, as per "Signature and persistence" section
1036c6fd2807SJeff Garzik  *	of ATA/PI spec (volume 1, sect 5.14).
1037c6fd2807SJeff Garzik  *
1038c6fd2807SJeff Garzik  *	LOCKING:
1039c6fd2807SJeff Garzik  *	None.
1040c6fd2807SJeff Garzik  *
1041c6fd2807SJeff Garzik  *	RETURNS:
1042633273a3STejun Heo  *	Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP or
1043633273a3STejun Heo  *	%ATA_DEV_UNKNOWN the event of failure.
1044c6fd2807SJeff Garzik  */
1045c6fd2807SJeff Garzik unsigned int ata_dev_classify(const struct ata_taskfile *tf)
1046c6fd2807SJeff Garzik {
1047c6fd2807SJeff Garzik 	/* Apple's open source Darwin code hints that some devices only
1048c6fd2807SJeff Garzik 	 * put a proper signature into the LBA mid/high registers,
1049c6fd2807SJeff Garzik 	 * So, we only check those.  It's sufficient for uniqueness.
1050633273a3STejun Heo 	 *
1051633273a3STejun Heo 	 * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate
1052633273a3STejun Heo 	 * signatures for ATA and ATAPI devices attached on SerialATA,
1053633273a3STejun Heo 	 * 0x3c/0xc3 and 0x69/0x96 respectively.  However, SerialATA
1054633273a3STejun Heo 	 * spec has never mentioned about using different signatures
1055633273a3STejun Heo 	 * for ATA/ATAPI devices.  Then, Serial ATA II: Port
1056633273a3STejun Heo 	 * Multiplier specification began to use 0x69/0x96 to identify
1057633273a3STejun Heo 	 * port multpliers and 0x3c/0xc3 to identify SEMB device.
1058633273a3STejun Heo 	 * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and
1059633273a3STejun Heo 	 * 0x69/0x96 shortly and described them as reserved for
1060633273a3STejun Heo 	 * SerialATA.
1061633273a3STejun Heo 	 *
1062633273a3STejun Heo 	 * We follow the current spec and consider that 0x69/0x96
1063633273a3STejun Heo 	 * identifies a port multiplier and 0x3c/0xc3 a SEMB device.
106479b42babSTejun Heo 	 * Unfortunately, WDC WD1600JS-62MHB5 (a hard drive) reports
106579b42babSTejun Heo 	 * SEMB signature.  This is worked around in
106679b42babSTejun Heo 	 * ata_dev_read_id().
1067c6fd2807SJeff Garzik 	 */
1068633273a3STejun Heo 	if ((tf->lbam == 0) && (tf->lbah == 0)) {
1069c6fd2807SJeff Garzik 		DPRINTK("found ATA device by sig\n");
1070c6fd2807SJeff Garzik 		return ATA_DEV_ATA;
1071c6fd2807SJeff Garzik 	}
1072c6fd2807SJeff Garzik 
1073633273a3STejun Heo 	if ((tf->lbam == 0x14) && (tf->lbah == 0xeb)) {
1074c6fd2807SJeff Garzik 		DPRINTK("found ATAPI device by sig\n");
1075c6fd2807SJeff Garzik 		return ATA_DEV_ATAPI;
1076c6fd2807SJeff Garzik 	}
1077c6fd2807SJeff Garzik 
1078633273a3STejun Heo 	if ((tf->lbam == 0x69) && (tf->lbah == 0x96)) {
1079633273a3STejun Heo 		DPRINTK("found PMP device by sig\n");
1080633273a3STejun Heo 		return ATA_DEV_PMP;
1081633273a3STejun Heo 	}
1082633273a3STejun Heo 
1083633273a3STejun Heo 	if ((tf->lbam == 0x3c) && (tf->lbah == 0xc3)) {
108479b42babSTejun Heo 		DPRINTK("found SEMB device by sig (could be ATA device)\n");
108579b42babSTejun Heo 		return ATA_DEV_SEMB;
1086633273a3STejun Heo 	}
1087633273a3STejun Heo 
1088c6fd2807SJeff Garzik 	DPRINTK("unknown device\n");
1089c6fd2807SJeff Garzik 	return ATA_DEV_UNKNOWN;
1090c6fd2807SJeff Garzik }
1091c6fd2807SJeff Garzik 
1092c6fd2807SJeff Garzik /**
1093c6fd2807SJeff Garzik  *	ata_id_string - Convert IDENTIFY DEVICE page into string
1094c6fd2807SJeff Garzik  *	@id: IDENTIFY DEVICE results we will examine
1095c6fd2807SJeff Garzik  *	@s: string into which data is output
1096c6fd2807SJeff Garzik  *	@ofs: offset into identify device page
1097c6fd2807SJeff Garzik  *	@len: length of string to return. must be an even number.
1098c6fd2807SJeff Garzik  *
1099c6fd2807SJeff Garzik  *	The strings in the IDENTIFY DEVICE page are broken up into
1100c6fd2807SJeff Garzik  *	16-bit chunks.  Run through the string, and output each
1101c6fd2807SJeff Garzik  *	8-bit chunk linearly, regardless of platform.
1102c6fd2807SJeff Garzik  *
1103c6fd2807SJeff Garzik  *	LOCKING:
1104c6fd2807SJeff Garzik  *	caller.
1105c6fd2807SJeff Garzik  */
1106c6fd2807SJeff Garzik 
1107c6fd2807SJeff Garzik void ata_id_string(const u16 *id, unsigned char *s,
1108c6fd2807SJeff Garzik 		   unsigned int ofs, unsigned int len)
1109c6fd2807SJeff Garzik {
1110c6fd2807SJeff Garzik 	unsigned int c;
1111c6fd2807SJeff Garzik 
1112963e4975SAlan Cox 	BUG_ON(len & 1);
1113963e4975SAlan Cox 
1114c6fd2807SJeff Garzik 	while (len > 0) {
1115c6fd2807SJeff Garzik 		c = id[ofs] >> 8;
1116c6fd2807SJeff Garzik 		*s = c;
1117c6fd2807SJeff Garzik 		s++;
1118c6fd2807SJeff Garzik 
1119c6fd2807SJeff Garzik 		c = id[ofs] & 0xff;
1120c6fd2807SJeff Garzik 		*s = c;
1121c6fd2807SJeff Garzik 		s++;
1122c6fd2807SJeff Garzik 
1123c6fd2807SJeff Garzik 		ofs++;
1124c6fd2807SJeff Garzik 		len -= 2;
1125c6fd2807SJeff Garzik 	}
1126c6fd2807SJeff Garzik }
1127c6fd2807SJeff Garzik 
1128c6fd2807SJeff Garzik /**
1129c6fd2807SJeff Garzik  *	ata_id_c_string - Convert IDENTIFY DEVICE page into C string
1130c6fd2807SJeff Garzik  *	@id: IDENTIFY DEVICE results we will examine
1131c6fd2807SJeff Garzik  *	@s: string into which data is output
1132c6fd2807SJeff Garzik  *	@ofs: offset into identify device page
1133c6fd2807SJeff Garzik  *	@len: length of string to return. must be an odd number.
1134c6fd2807SJeff Garzik  *
1135c6fd2807SJeff Garzik  *	This function is identical to ata_id_string except that it
1136c6fd2807SJeff Garzik  *	trims trailing spaces and terminates the resulting string with
1137c6fd2807SJeff Garzik  *	null.  @len must be actual maximum length (even number) + 1.
1138c6fd2807SJeff Garzik  *
1139c6fd2807SJeff Garzik  *	LOCKING:
1140c6fd2807SJeff Garzik  *	caller.
1141c6fd2807SJeff Garzik  */
1142c6fd2807SJeff Garzik void ata_id_c_string(const u16 *id, unsigned char *s,
1143c6fd2807SJeff Garzik 		     unsigned int ofs, unsigned int len)
1144c6fd2807SJeff Garzik {
1145c6fd2807SJeff Garzik 	unsigned char *p;
1146c6fd2807SJeff Garzik 
1147c6fd2807SJeff Garzik 	ata_id_string(id, s, ofs, len - 1);
1148c6fd2807SJeff Garzik 
1149c6fd2807SJeff Garzik 	p = s + strnlen(s, len - 1);
1150c6fd2807SJeff Garzik 	while (p > s && p[-1] == ' ')
1151c6fd2807SJeff Garzik 		p--;
1152c6fd2807SJeff Garzik 	*p = '\0';
1153c6fd2807SJeff Garzik }
1154c6fd2807SJeff Garzik 
1155db6f8759STejun Heo static u64 ata_id_n_sectors(const u16 *id)
1156db6f8759STejun Heo {
1157db6f8759STejun Heo 	if (ata_id_has_lba(id)) {
1158db6f8759STejun Heo 		if (ata_id_has_lba48(id))
1159968e594aSRobert Hancock 			return ata_id_u64(id, ATA_ID_LBA_CAPACITY_2);
1160db6f8759STejun Heo 		else
1161968e594aSRobert Hancock 			return ata_id_u32(id, ATA_ID_LBA_CAPACITY);
1162db6f8759STejun Heo 	} else {
1163db6f8759STejun Heo 		if (ata_id_current_chs_valid(id))
1164968e594aSRobert Hancock 			return id[ATA_ID_CUR_CYLS] * id[ATA_ID_CUR_HEADS] *
1165968e594aSRobert Hancock 			       id[ATA_ID_CUR_SECTORS];
1166db6f8759STejun Heo 		else
1167968e594aSRobert Hancock 			return id[ATA_ID_CYLS] * id[ATA_ID_HEADS] *
1168968e594aSRobert Hancock 			       id[ATA_ID_SECTORS];
1169db6f8759STejun Heo 	}
1170db6f8759STejun Heo }
1171db6f8759STejun Heo 
1172a5987e0aSTejun Heo u64 ata_tf_to_lba48(const struct ata_taskfile *tf)
11731e999736SAlan Cox {
11741e999736SAlan Cox 	u64 sectors = 0;
11751e999736SAlan Cox 
11761e999736SAlan Cox 	sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40;
11771e999736SAlan Cox 	sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32;
1178ba14a9c2SRoland Dreier 	sectors |= ((u64)(tf->hob_lbal & 0xff)) << 24;
11791e999736SAlan Cox 	sectors |= (tf->lbah & 0xff) << 16;
11801e999736SAlan Cox 	sectors |= (tf->lbam & 0xff) << 8;
11811e999736SAlan Cox 	sectors |= (tf->lbal & 0xff);
11821e999736SAlan Cox 
1183a5987e0aSTejun Heo 	return sectors;
11841e999736SAlan Cox }
11851e999736SAlan Cox 
1186a5987e0aSTejun Heo u64 ata_tf_to_lba(const struct ata_taskfile *tf)
11871e999736SAlan Cox {
11881e999736SAlan Cox 	u64 sectors = 0;
11891e999736SAlan Cox 
11901e999736SAlan Cox 	sectors |= (tf->device & 0x0f) << 24;
11911e999736SAlan Cox 	sectors |= (tf->lbah & 0xff) << 16;
11921e999736SAlan Cox 	sectors |= (tf->lbam & 0xff) << 8;
11931e999736SAlan Cox 	sectors |= (tf->lbal & 0xff);
11941e999736SAlan Cox 
1195a5987e0aSTejun Heo 	return sectors;
11961e999736SAlan Cox }
11971e999736SAlan Cox 
11981e999736SAlan Cox /**
1199c728a914STejun Heo  *	ata_read_native_max_address - Read native max address
1200c728a914STejun Heo  *	@dev: target device
1201c728a914STejun Heo  *	@max_sectors: out parameter for the result native max address
12021e999736SAlan Cox  *
1203c728a914STejun Heo  *	Perform an LBA48 or LBA28 native size query upon the device in
1204c728a914STejun Heo  *	question.
1205c728a914STejun Heo  *
1206c728a914STejun Heo  *	RETURNS:
1207c728a914STejun Heo  *	0 on success, -EACCES if command is aborted by the drive.
1208c728a914STejun Heo  *	-EIO on other errors.
12091e999736SAlan Cox  */
1210c728a914STejun Heo static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
12111e999736SAlan Cox {
1212c728a914STejun Heo 	unsigned int err_mask;
12131e999736SAlan Cox 	struct ata_taskfile tf;
1214c728a914STejun Heo 	int lba48 = ata_id_has_lba48(dev->id);
12151e999736SAlan Cox 
12161e999736SAlan Cox 	ata_tf_init(dev, &tf);
12171e999736SAlan Cox 
1218c728a914STejun Heo 	/* always clear all address registers */
12191e999736SAlan Cox 	tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1220c728a914STejun Heo 
1221c728a914STejun Heo 	if (lba48) {
1222c728a914STejun Heo 		tf.command = ATA_CMD_READ_NATIVE_MAX_EXT;
1223c728a914STejun Heo 		tf.flags |= ATA_TFLAG_LBA48;
1224c728a914STejun Heo 	} else
1225c728a914STejun Heo 		tf.command = ATA_CMD_READ_NATIVE_MAX;
1226c728a914STejun Heo 
12271e999736SAlan Cox 	tf.protocol |= ATA_PROT_NODATA;
1228c728a914STejun Heo 	tf.device |= ATA_LBA;
12291e999736SAlan Cox 
12302b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1231c728a914STejun Heo 	if (err_mask) {
1232a9a79dfeSJoe Perches 		ata_dev_warn(dev,
1233a9a79dfeSJoe Perches 			     "failed to read native max address (err_mask=0x%x)\n",
1234a9a79dfeSJoe Perches 			     err_mask);
1235c728a914STejun Heo 		if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
1236c728a914STejun Heo 			return -EACCES;
1237c728a914STejun Heo 		return -EIO;
1238c728a914STejun Heo 	}
1239c728a914STejun Heo 
1240c728a914STejun Heo 	if (lba48)
1241a5987e0aSTejun Heo 		*max_sectors = ata_tf_to_lba48(&tf) + 1;
1242c728a914STejun Heo 	else
1243a5987e0aSTejun Heo 		*max_sectors = ata_tf_to_lba(&tf) + 1;
124493328e11SAlan Cox 	if (dev->horkage & ATA_HORKAGE_HPA_SIZE)
124593328e11SAlan Cox 		(*max_sectors)--;
12461e999736SAlan Cox 	return 0;
12471e999736SAlan Cox }
12481e999736SAlan Cox 
12491e999736SAlan Cox /**
1250c728a914STejun Heo  *	ata_set_max_sectors - Set max sectors
1251c728a914STejun Heo  *	@dev: target device
12526b38d1d1SRandy Dunlap  *	@new_sectors: new max sectors value to set for the device
12531e999736SAlan Cox  *
1254c728a914STejun Heo  *	Set max sectors of @dev to @new_sectors.
1255c728a914STejun Heo  *
1256c728a914STejun Heo  *	RETURNS:
1257c728a914STejun Heo  *	0 on success, -EACCES if command is aborted or denied (due to
1258c728a914STejun Heo  *	previous non-volatile SET_MAX) by the drive.  -EIO on other
1259c728a914STejun Heo  *	errors.
12601e999736SAlan Cox  */
126105027adcSTejun Heo static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
12621e999736SAlan Cox {
1263c728a914STejun Heo 	unsigned int err_mask;
12641e999736SAlan Cox 	struct ata_taskfile tf;
1265c728a914STejun Heo 	int lba48 = ata_id_has_lba48(dev->id);
12661e999736SAlan Cox 
12671e999736SAlan Cox 	new_sectors--;
12681e999736SAlan Cox 
12691e999736SAlan Cox 	ata_tf_init(dev, &tf);
12701e999736SAlan Cox 
1271c728a914STejun Heo 	tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
12721e999736SAlan Cox 
1273c728a914STejun Heo 	if (lba48) {
1274c728a914STejun Heo 		tf.command = ATA_CMD_SET_MAX_EXT;
1275c728a914STejun Heo 		tf.flags |= ATA_TFLAG_LBA48;
12761e999736SAlan Cox 
12771e999736SAlan Cox 		tf.hob_lbal = (new_sectors >> 24) & 0xff;
12781e999736SAlan Cox 		tf.hob_lbam = (new_sectors >> 32) & 0xff;
12791e999736SAlan Cox 		tf.hob_lbah = (new_sectors >> 40) & 0xff;
12801e582ba4STejun Heo 	} else {
12811e999736SAlan Cox 		tf.command = ATA_CMD_SET_MAX;
1282c728a914STejun Heo 
12831e582ba4STejun Heo 		tf.device |= (new_sectors >> 24) & 0xf;
12841e582ba4STejun Heo 	}
12851e582ba4STejun Heo 
12861e999736SAlan Cox 	tf.protocol |= ATA_PROT_NODATA;
1287c728a914STejun Heo 	tf.device |= ATA_LBA;
12881e999736SAlan Cox 
12891e999736SAlan Cox 	tf.lbal = (new_sectors >> 0) & 0xff;
12901e999736SAlan Cox 	tf.lbam = (new_sectors >> 8) & 0xff;
12911e999736SAlan Cox 	tf.lbah = (new_sectors >> 16) & 0xff;
12921e999736SAlan Cox 
12932b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1294c728a914STejun Heo 	if (err_mask) {
1295a9a79dfeSJoe Perches 		ata_dev_warn(dev,
1296a9a79dfeSJoe Perches 			     "failed to set max address (err_mask=0x%x)\n",
1297a9a79dfeSJoe Perches 			     err_mask);
1298c728a914STejun Heo 		if (err_mask == AC_ERR_DEV &&
1299c728a914STejun Heo 		    (tf.feature & (ATA_ABORTED | ATA_IDNF)))
1300c728a914STejun Heo 			return -EACCES;
1301c728a914STejun Heo 		return -EIO;
1302c728a914STejun Heo 	}
1303c728a914STejun Heo 
13041e999736SAlan Cox 	return 0;
13051e999736SAlan Cox }
13061e999736SAlan Cox 
13071e999736SAlan Cox /**
13081e999736SAlan Cox  *	ata_hpa_resize		-	Resize a device with an HPA set
13091e999736SAlan Cox  *	@dev: Device to resize
13101e999736SAlan Cox  *
13111e999736SAlan Cox  *	Read the size of an LBA28 or LBA48 disk with HPA features and resize
13121e999736SAlan Cox  *	it if required to the full size of the media. The caller must check
13131e999736SAlan Cox  *	the drive has the HPA feature set enabled.
131405027adcSTejun Heo  *
131505027adcSTejun Heo  *	RETURNS:
131605027adcSTejun Heo  *	0 on success, -errno on failure.
13171e999736SAlan Cox  */
131805027adcSTejun Heo static int ata_hpa_resize(struct ata_device *dev)
13191e999736SAlan Cox {
132005027adcSTejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
132105027adcSTejun Heo 	int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
1322445d211bSTejun Heo 	bool unlock_hpa = ata_ignore_hpa || dev->flags & ATA_DFLAG_UNLOCK_HPA;
132305027adcSTejun Heo 	u64 sectors = ata_id_n_sectors(dev->id);
132405027adcSTejun Heo 	u64 native_sectors;
1325c728a914STejun Heo 	int rc;
13261e999736SAlan Cox 
132705027adcSTejun Heo 	/* do we need to do it? */
132805027adcSTejun Heo 	if (dev->class != ATA_DEV_ATA ||
132905027adcSTejun Heo 	    !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) ||
133005027adcSTejun Heo 	    (dev->horkage & ATA_HORKAGE_BROKEN_HPA))
1331c728a914STejun Heo 		return 0;
13321e999736SAlan Cox 
133305027adcSTejun Heo 	/* read native max address */
133405027adcSTejun Heo 	rc = ata_read_native_max_address(dev, &native_sectors);
133505027adcSTejun Heo 	if (rc) {
1336dda7aba1STejun Heo 		/* If device aborted the command or HPA isn't going to
1337dda7aba1STejun Heo 		 * be unlocked, skip HPA resizing.
133805027adcSTejun Heo 		 */
1339445d211bSTejun Heo 		if (rc == -EACCES || !unlock_hpa) {
1340a9a79dfeSJoe Perches 			ata_dev_warn(dev,
1341a9a79dfeSJoe Perches 				     "HPA support seems broken, skipping HPA handling\n");
134205027adcSTejun Heo 			dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
134305027adcSTejun Heo 
134405027adcSTejun Heo 			/* we can continue if device aborted the command */
134505027adcSTejun Heo 			if (rc == -EACCES)
134605027adcSTejun Heo 				rc = 0;
134705027adcSTejun Heo 		}
134805027adcSTejun Heo 
134905027adcSTejun Heo 		return rc;
135005027adcSTejun Heo 	}
13515920dadfSTejun Heo 	dev->n_native_sectors = native_sectors;
135205027adcSTejun Heo 
135305027adcSTejun Heo 	/* nothing to do? */
1354445d211bSTejun Heo 	if (native_sectors <= sectors || !unlock_hpa) {
135505027adcSTejun Heo 		if (!print_info || native_sectors == sectors)
135605027adcSTejun Heo 			return 0;
135705027adcSTejun Heo 
135805027adcSTejun Heo 		if (native_sectors > sectors)
1359a9a79dfeSJoe Perches 			ata_dev_info(dev,
136005027adcSTejun Heo 				"HPA detected: current %llu, native %llu\n",
136105027adcSTejun Heo 				(unsigned long long)sectors,
136205027adcSTejun Heo 				(unsigned long long)native_sectors);
136305027adcSTejun Heo 		else if (native_sectors < sectors)
1364a9a79dfeSJoe Perches 			ata_dev_warn(dev,
1365a9a79dfeSJoe Perches 				"native sectors (%llu) is smaller than sectors (%llu)\n",
136605027adcSTejun Heo 				(unsigned long long)native_sectors,
136705027adcSTejun Heo 				(unsigned long long)sectors);
136805027adcSTejun Heo 		return 0;
13691e999736SAlan Cox 	}
137037301a55STejun Heo 
137105027adcSTejun Heo 	/* let's unlock HPA */
137205027adcSTejun Heo 	rc = ata_set_max_sectors(dev, native_sectors);
137305027adcSTejun Heo 	if (rc == -EACCES) {
137405027adcSTejun Heo 		/* if device aborted the command, skip HPA resizing */
1375a9a79dfeSJoe Perches 		ata_dev_warn(dev,
1376a9a79dfeSJoe Perches 			     "device aborted resize (%llu -> %llu), skipping HPA handling\n",
137705027adcSTejun Heo 			     (unsigned long long)sectors,
137805027adcSTejun Heo 			     (unsigned long long)native_sectors);
137905027adcSTejun Heo 		dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
138005027adcSTejun Heo 		return 0;
138105027adcSTejun Heo 	} else if (rc)
138205027adcSTejun Heo 		return rc;
138305027adcSTejun Heo 
138405027adcSTejun Heo 	/* re-read IDENTIFY data */
138505027adcSTejun Heo 	rc = ata_dev_reread_id(dev, 0);
138605027adcSTejun Heo 	if (rc) {
1387a9a79dfeSJoe Perches 		ata_dev_err(dev,
1388a9a79dfeSJoe Perches 			    "failed to re-read IDENTIFY data after HPA resizing\n");
138905027adcSTejun Heo 		return rc;
139005027adcSTejun Heo 	}
139105027adcSTejun Heo 
139205027adcSTejun Heo 	if (print_info) {
139305027adcSTejun Heo 		u64 new_sectors = ata_id_n_sectors(dev->id);
1394a9a79dfeSJoe Perches 		ata_dev_info(dev,
139505027adcSTejun Heo 			"HPA unlocked: %llu -> %llu, native %llu\n",
139605027adcSTejun Heo 			(unsigned long long)sectors,
139705027adcSTejun Heo 			(unsigned long long)new_sectors,
139805027adcSTejun Heo 			(unsigned long long)native_sectors);
139905027adcSTejun Heo 	}
140005027adcSTejun Heo 
140105027adcSTejun Heo 	return 0;
14021e999736SAlan Cox }
14031e999736SAlan Cox 
1404c6fd2807SJeff Garzik /**
1405c6fd2807SJeff Garzik  *	ata_dump_id - IDENTIFY DEVICE info debugging output
1406c6fd2807SJeff Garzik  *	@id: IDENTIFY DEVICE page to dump
1407c6fd2807SJeff Garzik  *
1408c6fd2807SJeff Garzik  *	Dump selected 16-bit words from the given IDENTIFY DEVICE
1409c6fd2807SJeff Garzik  *	page.
1410c6fd2807SJeff Garzik  *
1411c6fd2807SJeff Garzik  *	LOCKING:
1412c6fd2807SJeff Garzik  *	caller.
1413c6fd2807SJeff Garzik  */
1414c6fd2807SJeff Garzik 
1415c6fd2807SJeff Garzik static inline void ata_dump_id(const u16 *id)
1416c6fd2807SJeff Garzik {
1417c6fd2807SJeff Garzik 	DPRINTK("49==0x%04x  "
1418c6fd2807SJeff Garzik 		"53==0x%04x  "
1419c6fd2807SJeff Garzik 		"63==0x%04x  "
1420c6fd2807SJeff Garzik 		"64==0x%04x  "
1421c6fd2807SJeff Garzik 		"75==0x%04x  \n",
1422c6fd2807SJeff Garzik 		id[49],
1423c6fd2807SJeff Garzik 		id[53],
1424c6fd2807SJeff Garzik 		id[63],
1425c6fd2807SJeff Garzik 		id[64],
1426c6fd2807SJeff Garzik 		id[75]);
1427c6fd2807SJeff Garzik 	DPRINTK("80==0x%04x  "
1428c6fd2807SJeff Garzik 		"81==0x%04x  "
1429c6fd2807SJeff Garzik 		"82==0x%04x  "
1430c6fd2807SJeff Garzik 		"83==0x%04x  "
1431c6fd2807SJeff Garzik 		"84==0x%04x  \n",
1432c6fd2807SJeff Garzik 		id[80],
1433c6fd2807SJeff Garzik 		id[81],
1434c6fd2807SJeff Garzik 		id[82],
1435c6fd2807SJeff Garzik 		id[83],
1436c6fd2807SJeff Garzik 		id[84]);
1437c6fd2807SJeff Garzik 	DPRINTK("88==0x%04x  "
1438c6fd2807SJeff Garzik 		"93==0x%04x\n",
1439c6fd2807SJeff Garzik 		id[88],
1440c6fd2807SJeff Garzik 		id[93]);
1441c6fd2807SJeff Garzik }
1442c6fd2807SJeff Garzik 
1443c6fd2807SJeff Garzik /**
1444c6fd2807SJeff Garzik  *	ata_id_xfermask - Compute xfermask from the given IDENTIFY data
1445c6fd2807SJeff Garzik  *	@id: IDENTIFY data to compute xfer mask from
1446c6fd2807SJeff Garzik  *
1447c6fd2807SJeff Garzik  *	Compute the xfermask for this device. This is not as trivial
1448c6fd2807SJeff Garzik  *	as it seems if we must consider early devices correctly.
1449c6fd2807SJeff Garzik  *
1450c6fd2807SJeff Garzik  *	FIXME: pre IDE drive timing (do we care ?).
1451c6fd2807SJeff Garzik  *
1452c6fd2807SJeff Garzik  *	LOCKING:
1453c6fd2807SJeff Garzik  *	None.
1454c6fd2807SJeff Garzik  *
1455c6fd2807SJeff Garzik  *	RETURNS:
1456c6fd2807SJeff Garzik  *	Computed xfermask
1457c6fd2807SJeff Garzik  */
14587dc951aeSTejun Heo unsigned long ata_id_xfermask(const u16 *id)
1459c6fd2807SJeff Garzik {
14607dc951aeSTejun Heo 	unsigned long pio_mask, mwdma_mask, udma_mask;
1461c6fd2807SJeff Garzik 
1462c6fd2807SJeff Garzik 	/* Usual case. Word 53 indicates word 64 is valid */
1463c6fd2807SJeff Garzik 	if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
1464c6fd2807SJeff Garzik 		pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
1465c6fd2807SJeff Garzik 		pio_mask <<= 3;
1466c6fd2807SJeff Garzik 		pio_mask |= 0x7;
1467c6fd2807SJeff Garzik 	} else {
1468c6fd2807SJeff Garzik 		/* If word 64 isn't valid then Word 51 high byte holds
1469c6fd2807SJeff Garzik 		 * the PIO timing number for the maximum. Turn it into
1470c6fd2807SJeff Garzik 		 * a mask.
1471c6fd2807SJeff Garzik 		 */
14727a0f1c8aSLennert Buytenhek 		u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
147346767aebSAlan Cox 		if (mode < 5)	/* Valid PIO range */
147446767aebSAlan Cox 			pio_mask = (2 << mode) - 1;
147546767aebSAlan Cox 		else
147646767aebSAlan Cox 			pio_mask = 1;
1477c6fd2807SJeff Garzik 
1478c6fd2807SJeff Garzik 		/* But wait.. there's more. Design your standards by
1479c6fd2807SJeff Garzik 		 * committee and you too can get a free iordy field to
1480c6fd2807SJeff Garzik 		 * process. However its the speeds not the modes that
1481c6fd2807SJeff Garzik 		 * are supported... Note drivers using the timing API
1482c6fd2807SJeff Garzik 		 * will get this right anyway
1483c6fd2807SJeff Garzik 		 */
1484c6fd2807SJeff Garzik 	}
1485c6fd2807SJeff Garzik 
1486c6fd2807SJeff Garzik 	mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
1487c6fd2807SJeff Garzik 
1488b352e57dSAlan Cox 	if (ata_id_is_cfa(id)) {
1489b352e57dSAlan Cox 		/*
1490b352e57dSAlan Cox 		 *	Process compact flash extended modes
1491b352e57dSAlan Cox 		 */
149262afe5d7SSergei Shtylyov 		int pio = (id[ATA_ID_CFA_MODES] >> 0) & 0x7;
149362afe5d7SSergei Shtylyov 		int dma = (id[ATA_ID_CFA_MODES] >> 3) & 0x7;
1494b352e57dSAlan Cox 
1495b352e57dSAlan Cox 		if (pio)
1496b352e57dSAlan Cox 			pio_mask |= (1 << 5);
1497b352e57dSAlan Cox 		if (pio > 1)
1498b352e57dSAlan Cox 			pio_mask |= (1 << 6);
1499b352e57dSAlan Cox 		if (dma)
1500b352e57dSAlan Cox 			mwdma_mask |= (1 << 3);
1501b352e57dSAlan Cox 		if (dma > 1)
1502b352e57dSAlan Cox 			mwdma_mask |= (1 << 4);
1503b352e57dSAlan Cox 	}
1504b352e57dSAlan Cox 
1505c6fd2807SJeff Garzik 	udma_mask = 0;
1506c6fd2807SJeff Garzik 	if (id[ATA_ID_FIELD_VALID] & (1 << 2))
1507c6fd2807SJeff Garzik 		udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
1508c6fd2807SJeff Garzik 
1509c6fd2807SJeff Garzik 	return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
1510c6fd2807SJeff Garzik }
1511c6fd2807SJeff Garzik 
15127102d230SAdrian Bunk static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
1513c6fd2807SJeff Garzik {
1514c6fd2807SJeff Garzik 	struct completion *waiting = qc->private_data;
1515c6fd2807SJeff Garzik 
1516c6fd2807SJeff Garzik 	complete(waiting);
1517c6fd2807SJeff Garzik }
1518c6fd2807SJeff Garzik 
1519c6fd2807SJeff Garzik /**
15202432697bSTejun Heo  *	ata_exec_internal_sg - execute libata internal command
1521c6fd2807SJeff Garzik  *	@dev: Device to which the command is sent
1522c6fd2807SJeff Garzik  *	@tf: Taskfile registers for the command and the result
1523c6fd2807SJeff Garzik  *	@cdb: CDB for packet command
1524c6fd2807SJeff Garzik  *	@dma_dir: Data tranfer direction of the command
15255c1ad8b3SRandy Dunlap  *	@sgl: sg list for the data buffer of the command
15262432697bSTejun Heo  *	@n_elem: Number of sg entries
15272b789108STejun Heo  *	@timeout: Timeout in msecs (0 for default)
1528c6fd2807SJeff Garzik  *
1529c6fd2807SJeff Garzik  *	Executes libata internal command with timeout.  @tf contains
1530c6fd2807SJeff Garzik  *	command on entry and result on return.  Timeout and error
1531c6fd2807SJeff Garzik  *	conditions are reported via return value.  No recovery action
1532c6fd2807SJeff Garzik  *	is taken after a command times out.  It's caller's duty to
1533c6fd2807SJeff Garzik  *	clean up after timeout.
1534c6fd2807SJeff Garzik  *
1535c6fd2807SJeff Garzik  *	LOCKING:
1536c6fd2807SJeff Garzik  *	None.  Should be called with kernel context, might sleep.
1537c6fd2807SJeff Garzik  *
1538c6fd2807SJeff Garzik  *	RETURNS:
1539c6fd2807SJeff Garzik  *	Zero on success, AC_ERR_* mask on failure
1540c6fd2807SJeff Garzik  */
15412432697bSTejun Heo unsigned ata_exec_internal_sg(struct ata_device *dev,
1542c6fd2807SJeff Garzik 			      struct ata_taskfile *tf, const u8 *cdb,
154387260216SJens Axboe 			      int dma_dir, struct scatterlist *sgl,
15442b789108STejun Heo 			      unsigned int n_elem, unsigned long timeout)
1545c6fd2807SJeff Garzik {
15469af5c9c9STejun Heo 	struct ata_link *link = dev->link;
15479af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
1548c6fd2807SJeff Garzik 	u8 command = tf->command;
154987fbc5a0STejun Heo 	int auto_timeout = 0;
1550c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
1551c6fd2807SJeff Garzik 	unsigned int tag, preempted_tag;
1552c6fd2807SJeff Garzik 	u32 preempted_sactive, preempted_qc_active;
1553da917d69STejun Heo 	int preempted_nr_active_links;
1554c6fd2807SJeff Garzik 	DECLARE_COMPLETION_ONSTACK(wait);
1555c6fd2807SJeff Garzik 	unsigned long flags;
1556c6fd2807SJeff Garzik 	unsigned int err_mask;
1557c6fd2807SJeff Garzik 	int rc;
1558c6fd2807SJeff Garzik 
1559c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1560c6fd2807SJeff Garzik 
1561c6fd2807SJeff Garzik 	/* no internal command while frozen */
1562c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_FROZEN) {
1563c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
1564c6fd2807SJeff Garzik 		return AC_ERR_SYSTEM;
1565c6fd2807SJeff Garzik 	}
1566c6fd2807SJeff Garzik 
1567c6fd2807SJeff Garzik 	/* initialize internal qc */
1568c6fd2807SJeff Garzik 
1569c6fd2807SJeff Garzik 	/* XXX: Tag 0 is used for drivers with legacy EH as some
1570c6fd2807SJeff Garzik 	 * drivers choke if any other tag is given.  This breaks
1571c6fd2807SJeff Garzik 	 * ata_tag_internal() test for those drivers.  Don't use new
1572c6fd2807SJeff Garzik 	 * EH stuff without converting to it.
1573c6fd2807SJeff Garzik 	 */
1574c6fd2807SJeff Garzik 	if (ap->ops->error_handler)
1575c6fd2807SJeff Garzik 		tag = ATA_TAG_INTERNAL;
1576c6fd2807SJeff Garzik 	else
1577c6fd2807SJeff Garzik 		tag = 0;
1578c6fd2807SJeff Garzik 
15798a8bc223STejun Heo 	if (test_and_set_bit(tag, &ap->qc_allocated))
15808a8bc223STejun Heo 		BUG();
1581c6fd2807SJeff Garzik 	qc = __ata_qc_from_tag(ap, tag);
1582c6fd2807SJeff Garzik 
1583c6fd2807SJeff Garzik 	qc->tag = tag;
1584c6fd2807SJeff Garzik 	qc->scsicmd = NULL;
1585c6fd2807SJeff Garzik 	qc->ap = ap;
1586c6fd2807SJeff Garzik 	qc->dev = dev;
1587c6fd2807SJeff Garzik 	ata_qc_reinit(qc);
1588c6fd2807SJeff Garzik 
15899af5c9c9STejun Heo 	preempted_tag = link->active_tag;
15909af5c9c9STejun Heo 	preempted_sactive = link->sactive;
1591c6fd2807SJeff Garzik 	preempted_qc_active = ap->qc_active;
1592da917d69STejun Heo 	preempted_nr_active_links = ap->nr_active_links;
15939af5c9c9STejun Heo 	link->active_tag = ATA_TAG_POISON;
15949af5c9c9STejun Heo 	link->sactive = 0;
1595c6fd2807SJeff Garzik 	ap->qc_active = 0;
1596da917d69STejun Heo 	ap->nr_active_links = 0;
1597c6fd2807SJeff Garzik 
1598c6fd2807SJeff Garzik 	/* prepare & issue qc */
1599c6fd2807SJeff Garzik 	qc->tf = *tf;
1600c6fd2807SJeff Garzik 	if (cdb)
1601c6fd2807SJeff Garzik 		memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
1602c6fd2807SJeff Garzik 	qc->flags |= ATA_QCFLAG_RESULT_TF;
1603c6fd2807SJeff Garzik 	qc->dma_dir = dma_dir;
1604c6fd2807SJeff Garzik 	if (dma_dir != DMA_NONE) {
16052432697bSTejun Heo 		unsigned int i, buflen = 0;
160687260216SJens Axboe 		struct scatterlist *sg;
16072432697bSTejun Heo 
160887260216SJens Axboe 		for_each_sg(sgl, sg, n_elem, i)
160987260216SJens Axboe 			buflen += sg->length;
16102432697bSTejun Heo 
161187260216SJens Axboe 		ata_sg_init(qc, sgl, n_elem);
161249c80429SBrian King 		qc->nbytes = buflen;
1613c6fd2807SJeff Garzik 	}
1614c6fd2807SJeff Garzik 
1615c6fd2807SJeff Garzik 	qc->private_data = &wait;
1616c6fd2807SJeff Garzik 	qc->complete_fn = ata_qc_complete_internal;
1617c6fd2807SJeff Garzik 
1618c6fd2807SJeff Garzik 	ata_qc_issue(qc);
1619c6fd2807SJeff Garzik 
1620c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1621c6fd2807SJeff Garzik 
162287fbc5a0STejun Heo 	if (!timeout) {
162387fbc5a0STejun Heo 		if (ata_probe_timeout)
1624341c2c95STejun Heo 			timeout = ata_probe_timeout * 1000;
162587fbc5a0STejun Heo 		else {
162687fbc5a0STejun Heo 			timeout = ata_internal_cmd_timeout(dev, command);
162787fbc5a0STejun Heo 			auto_timeout = 1;
162887fbc5a0STejun Heo 		}
162987fbc5a0STejun Heo 	}
16302b789108STejun Heo 
1631c0c362b6STejun Heo 	if (ap->ops->error_handler)
1632c0c362b6STejun Heo 		ata_eh_release(ap);
1633c0c362b6STejun Heo 
16342b789108STejun Heo 	rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
1635c6fd2807SJeff Garzik 
1636c0c362b6STejun Heo 	if (ap->ops->error_handler)
1637c0c362b6STejun Heo 		ata_eh_acquire(ap);
1638c0c362b6STejun Heo 
1639c429137aSTejun Heo 	ata_sff_flush_pio_task(ap);
1640c6fd2807SJeff Garzik 
1641c6fd2807SJeff Garzik 	if (!rc) {
1642c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
1643c6fd2807SJeff Garzik 
1644c6fd2807SJeff Garzik 		/* We're racing with irq here.  If we lose, the
1645c6fd2807SJeff Garzik 		 * following test prevents us from completing the qc
1646c6fd2807SJeff Garzik 		 * twice.  If we win, the port is frozen and will be
1647c6fd2807SJeff Garzik 		 * cleaned up by ->post_internal_cmd().
1648c6fd2807SJeff Garzik 		 */
1649c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_ACTIVE) {
1650c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_TIMEOUT;
1651c6fd2807SJeff Garzik 
1652c6fd2807SJeff Garzik 			if (ap->ops->error_handler)
1653c6fd2807SJeff Garzik 				ata_port_freeze(ap);
1654c6fd2807SJeff Garzik 			else
1655c6fd2807SJeff Garzik 				ata_qc_complete(qc);
1656c6fd2807SJeff Garzik 
1657c6fd2807SJeff Garzik 			if (ata_msg_warn(ap))
1658a9a79dfeSJoe Perches 				ata_dev_warn(dev, "qc timeout (cmd 0x%x)\n",
1659a9a79dfeSJoe Perches 					     command);
1660c6fd2807SJeff Garzik 		}
1661c6fd2807SJeff Garzik 
1662c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
1663c6fd2807SJeff Garzik 	}
1664c6fd2807SJeff Garzik 
1665c6fd2807SJeff Garzik 	/* do post_internal_cmd */
1666c6fd2807SJeff Garzik 	if (ap->ops->post_internal_cmd)
1667c6fd2807SJeff Garzik 		ap->ops->post_internal_cmd(qc);
1668c6fd2807SJeff Garzik 
1669a51d644aSTejun Heo 	/* perform minimal error analysis */
1670a51d644aSTejun Heo 	if (qc->flags & ATA_QCFLAG_FAILED) {
1671a51d644aSTejun Heo 		if (qc->result_tf.command & (ATA_ERR | ATA_DF))
1672a51d644aSTejun Heo 			qc->err_mask |= AC_ERR_DEV;
1673a51d644aSTejun Heo 
1674a51d644aSTejun Heo 		if (!qc->err_mask)
1675c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_OTHER;
1676a51d644aSTejun Heo 
1677a51d644aSTejun Heo 		if (qc->err_mask & ~AC_ERR_OTHER)
1678a51d644aSTejun Heo 			qc->err_mask &= ~AC_ERR_OTHER;
1679c6fd2807SJeff Garzik 	}
1680c6fd2807SJeff Garzik 
1681c6fd2807SJeff Garzik 	/* finish up */
1682c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1683c6fd2807SJeff Garzik 
1684c6fd2807SJeff Garzik 	*tf = qc->result_tf;
1685c6fd2807SJeff Garzik 	err_mask = qc->err_mask;
1686c6fd2807SJeff Garzik 
1687c6fd2807SJeff Garzik 	ata_qc_free(qc);
16889af5c9c9STejun Heo 	link->active_tag = preempted_tag;
16899af5c9c9STejun Heo 	link->sactive = preempted_sactive;
1690c6fd2807SJeff Garzik 	ap->qc_active = preempted_qc_active;
1691da917d69STejun Heo 	ap->nr_active_links = preempted_nr_active_links;
1692c6fd2807SJeff Garzik 
1693c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1694c6fd2807SJeff Garzik 
169587fbc5a0STejun Heo 	if ((err_mask & AC_ERR_TIMEOUT) && auto_timeout)
169687fbc5a0STejun Heo 		ata_internal_cmd_timed_out(dev, command);
169787fbc5a0STejun Heo 
1698c6fd2807SJeff Garzik 	return err_mask;
1699c6fd2807SJeff Garzik }
1700c6fd2807SJeff Garzik 
1701c6fd2807SJeff Garzik /**
170233480a0eSTejun Heo  *	ata_exec_internal - execute libata internal command
17032432697bSTejun Heo  *	@dev: Device to which the command is sent
17042432697bSTejun Heo  *	@tf: Taskfile registers for the command and the result
17052432697bSTejun Heo  *	@cdb: CDB for packet command
17062432697bSTejun Heo  *	@dma_dir: Data tranfer direction of the command
17072432697bSTejun Heo  *	@buf: Data buffer of the command
17082432697bSTejun Heo  *	@buflen: Length of data buffer
17092b789108STejun Heo  *	@timeout: Timeout in msecs (0 for default)
17102432697bSTejun Heo  *
17112432697bSTejun Heo  *	Wrapper around ata_exec_internal_sg() which takes simple
17122432697bSTejun Heo  *	buffer instead of sg list.
17132432697bSTejun Heo  *
17142432697bSTejun Heo  *	LOCKING:
17152432697bSTejun Heo  *	None.  Should be called with kernel context, might sleep.
17162432697bSTejun Heo  *
17172432697bSTejun Heo  *	RETURNS:
17182432697bSTejun Heo  *	Zero on success, AC_ERR_* mask on failure
17192432697bSTejun Heo  */
17202432697bSTejun Heo unsigned ata_exec_internal(struct ata_device *dev,
17212432697bSTejun Heo 			   struct ata_taskfile *tf, const u8 *cdb,
17222b789108STejun Heo 			   int dma_dir, void *buf, unsigned int buflen,
17232b789108STejun Heo 			   unsigned long timeout)
17242432697bSTejun Heo {
172533480a0eSTejun Heo 	struct scatterlist *psg = NULL, sg;
172633480a0eSTejun Heo 	unsigned int n_elem = 0;
17272432697bSTejun Heo 
172833480a0eSTejun Heo 	if (dma_dir != DMA_NONE) {
172933480a0eSTejun Heo 		WARN_ON(!buf);
17302432697bSTejun Heo 		sg_init_one(&sg, buf, buflen);
173133480a0eSTejun Heo 		psg = &sg;
173233480a0eSTejun Heo 		n_elem++;
173333480a0eSTejun Heo 	}
17342432697bSTejun Heo 
17352b789108STejun Heo 	return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem,
17362b789108STejun Heo 				    timeout);
17372432697bSTejun Heo }
17382432697bSTejun Heo 
17392432697bSTejun Heo /**
1740c6fd2807SJeff Garzik  *	ata_do_simple_cmd - execute simple internal command
1741c6fd2807SJeff Garzik  *	@dev: Device to which the command is sent
1742c6fd2807SJeff Garzik  *	@cmd: Opcode to execute
1743c6fd2807SJeff Garzik  *
1744c6fd2807SJeff Garzik  *	Execute a 'simple' command, that only consists of the opcode
1745c6fd2807SJeff Garzik  *	'cmd' itself, without filling any other registers
1746c6fd2807SJeff Garzik  *
1747c6fd2807SJeff Garzik  *	LOCKING:
1748c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1749c6fd2807SJeff Garzik  *
1750c6fd2807SJeff Garzik  *	RETURNS:
1751c6fd2807SJeff Garzik  *	Zero on success, AC_ERR_* mask on failure
1752c6fd2807SJeff Garzik  */
1753c6fd2807SJeff Garzik unsigned int ata_do_simple_cmd(struct ata_device *dev, u8 cmd)
1754c6fd2807SJeff Garzik {
1755c6fd2807SJeff Garzik 	struct ata_taskfile tf;
1756c6fd2807SJeff Garzik 
1757c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
1758c6fd2807SJeff Garzik 
1759c6fd2807SJeff Garzik 	tf.command = cmd;
1760c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_DEVICE;
1761c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
1762c6fd2807SJeff Garzik 
17632b789108STejun Heo 	return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1764c6fd2807SJeff Garzik }
1765c6fd2807SJeff Garzik 
1766c6fd2807SJeff Garzik /**
1767c6fd2807SJeff Garzik  *	ata_pio_need_iordy	-	check if iordy needed
1768c6fd2807SJeff Garzik  *	@adev: ATA device
1769c6fd2807SJeff Garzik  *
1770c6fd2807SJeff Garzik  *	Check if the current speed of the device requires IORDY. Used
1771c6fd2807SJeff Garzik  *	by various controllers for chip configuration.
1772c6fd2807SJeff Garzik  */
1773c6fd2807SJeff Garzik unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1774c6fd2807SJeff Garzik {
17750d9e6659STejun Heo 	/* Don't set IORDY if we're preparing for reset.  IORDY may
17760d9e6659STejun Heo 	 * lead to controller lock up on certain controllers if the
17770d9e6659STejun Heo 	 * port is not occupied.  See bko#11703 for details.
17780d9e6659STejun Heo 	 */
17790d9e6659STejun Heo 	if (adev->link->ap->pflags & ATA_PFLAG_RESETTING)
17800d9e6659STejun Heo 		return 0;
17810d9e6659STejun Heo 	/* Controller doesn't support IORDY.  Probably a pointless
17820d9e6659STejun Heo 	 * check as the caller should know this.
17830d9e6659STejun Heo 	 */
17849af5c9c9STejun Heo 	if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
1785c6fd2807SJeff Garzik 		return 0;
17865c18c4d2SDavid Daney 	/* CF spec. r4.1 Table 22 says no iordy on PIO5 and PIO6.  */
17875c18c4d2SDavid Daney 	if (ata_id_is_cfa(adev->id)
17885c18c4d2SDavid Daney 	    && (adev->pio_mode == XFER_PIO_5 || adev->pio_mode == XFER_PIO_6))
17895c18c4d2SDavid Daney 		return 0;
1790432729f0SAlan Cox 	/* PIO3 and higher it is mandatory */
1791432729f0SAlan Cox 	if (adev->pio_mode > XFER_PIO_2)
1792c6fd2807SJeff Garzik 		return 1;
1793432729f0SAlan Cox 	/* We turn it on when possible */
1794432729f0SAlan Cox 	if (ata_id_has_iordy(adev->id))
1795432729f0SAlan Cox 		return 1;
1796432729f0SAlan Cox 	return 0;
1797432729f0SAlan Cox }
1798c6fd2807SJeff Garzik 
1799432729f0SAlan Cox /**
1800432729f0SAlan Cox  *	ata_pio_mask_no_iordy	-	Return the non IORDY mask
1801432729f0SAlan Cox  *	@adev: ATA device
1802432729f0SAlan Cox  *
1803432729f0SAlan Cox  *	Compute the highest mode possible if we are not using iordy. Return
1804432729f0SAlan Cox  *	-1 if no iordy mode is available.
1805432729f0SAlan Cox  */
1806432729f0SAlan Cox static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
1807432729f0SAlan Cox {
1808c6fd2807SJeff Garzik 	/* If we have no drive specific rule, then PIO 2 is non IORDY */
1809c6fd2807SJeff Garzik 	if (adev->id[ATA_ID_FIELD_VALID] & 2) {	/* EIDE */
1810432729f0SAlan Cox 		u16 pio = adev->id[ATA_ID_EIDE_PIO];
1811c6fd2807SJeff Garzik 		/* Is the speed faster than the drive allows non IORDY ? */
1812c6fd2807SJeff Garzik 		if (pio) {
1813c6fd2807SJeff Garzik 			/* This is cycle times not frequency - watch the logic! */
1814c6fd2807SJeff Garzik 			if (pio > 240)	/* PIO2 is 240nS per cycle */
1815432729f0SAlan Cox 				return 3 << ATA_SHIFT_PIO;
1816432729f0SAlan Cox 			return 7 << ATA_SHIFT_PIO;
1817c6fd2807SJeff Garzik 		}
1818c6fd2807SJeff Garzik 	}
1819432729f0SAlan Cox 	return 3 << ATA_SHIFT_PIO;
1820c6fd2807SJeff Garzik }
1821c6fd2807SJeff Garzik 
1822c6fd2807SJeff Garzik /**
1823963e4975SAlan Cox  *	ata_do_dev_read_id		-	default ID read method
1824963e4975SAlan Cox  *	@dev: device
1825963e4975SAlan Cox  *	@tf: proposed taskfile
1826963e4975SAlan Cox  *	@id: data buffer
1827963e4975SAlan Cox  *
1828963e4975SAlan Cox  *	Issue the identify taskfile and hand back the buffer containing
1829963e4975SAlan Cox  *	identify data. For some RAID controllers and for pre ATA devices
1830963e4975SAlan Cox  *	this function is wrapped or replaced by the driver
1831963e4975SAlan Cox  */
1832963e4975SAlan Cox unsigned int ata_do_dev_read_id(struct ata_device *dev,
1833963e4975SAlan Cox 					struct ata_taskfile *tf, u16 *id)
1834963e4975SAlan Cox {
1835963e4975SAlan Cox 	return ata_exec_internal(dev, tf, NULL, DMA_FROM_DEVICE,
1836963e4975SAlan Cox 				     id, sizeof(id[0]) * ATA_ID_WORDS, 0);
1837963e4975SAlan Cox }
1838963e4975SAlan Cox 
1839963e4975SAlan Cox /**
1840c6fd2807SJeff Garzik  *	ata_dev_read_id - Read ID data from the specified device
1841c6fd2807SJeff Garzik  *	@dev: target device
1842c6fd2807SJeff Garzik  *	@p_class: pointer to class of the target device (may be changed)
1843bff04647STejun Heo  *	@flags: ATA_READID_* flags
1844c6fd2807SJeff Garzik  *	@id: buffer to read IDENTIFY data into
1845c6fd2807SJeff Garzik  *
1846c6fd2807SJeff Garzik  *	Read ID data from the specified device.  ATA_CMD_ID_ATA is
1847c6fd2807SJeff Garzik  *	performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
1848c6fd2807SJeff Garzik  *	devices.  This function also issues ATA_CMD_INIT_DEV_PARAMS
1849c6fd2807SJeff Garzik  *	for pre-ATA4 drives.
1850c6fd2807SJeff Garzik  *
185150a99018SAlan Cox  *	FIXME: ATA_CMD_ID_ATA is optional for early drives and right
185250a99018SAlan Cox  *	now we abort if we hit that case.
185350a99018SAlan Cox  *
1854c6fd2807SJeff Garzik  *	LOCKING:
1855c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
1856c6fd2807SJeff Garzik  *
1857c6fd2807SJeff Garzik  *	RETURNS:
1858c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
1859c6fd2807SJeff Garzik  */
1860c6fd2807SJeff Garzik int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
1861bff04647STejun Heo 		    unsigned int flags, u16 *id)
1862c6fd2807SJeff Garzik {
18639af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
1864c6fd2807SJeff Garzik 	unsigned int class = *p_class;
1865c6fd2807SJeff Garzik 	struct ata_taskfile tf;
1866c6fd2807SJeff Garzik 	unsigned int err_mask = 0;
1867c6fd2807SJeff Garzik 	const char *reason;
186879b42babSTejun Heo 	bool is_semb = class == ATA_DEV_SEMB;
186954936f8bSTejun Heo 	int may_fallback = 1, tried_spinup = 0;
1870c6fd2807SJeff Garzik 	int rc;
1871c6fd2807SJeff Garzik 
1872c6fd2807SJeff Garzik 	if (ata_msg_ctl(ap))
1873a9a79dfeSJoe Perches 		ata_dev_dbg(dev, "%s: ENTER\n", __func__);
1874c6fd2807SJeff Garzik 
1875c6fd2807SJeff Garzik retry:
1876c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
1877c6fd2807SJeff Garzik 
1878c6fd2807SJeff Garzik 	switch (class) {
187979b42babSTejun Heo 	case ATA_DEV_SEMB:
188079b42babSTejun Heo 		class = ATA_DEV_ATA;	/* some hard drives report SEMB sig */
1881c6fd2807SJeff Garzik 	case ATA_DEV_ATA:
1882c6fd2807SJeff Garzik 		tf.command = ATA_CMD_ID_ATA;
1883c6fd2807SJeff Garzik 		break;
1884c6fd2807SJeff Garzik 	case ATA_DEV_ATAPI:
1885c6fd2807SJeff Garzik 		tf.command = ATA_CMD_ID_ATAPI;
1886c6fd2807SJeff Garzik 		break;
1887c6fd2807SJeff Garzik 	default:
1888c6fd2807SJeff Garzik 		rc = -ENODEV;
1889c6fd2807SJeff Garzik 		reason = "unsupported class";
1890c6fd2807SJeff Garzik 		goto err_out;
1891c6fd2807SJeff Garzik 	}
1892c6fd2807SJeff Garzik 
1893c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_PIO;
189481afe893STejun Heo 
189581afe893STejun Heo 	/* Some devices choke if TF registers contain garbage.  Make
189681afe893STejun Heo 	 * sure those are properly initialized.
189781afe893STejun Heo 	 */
189881afe893STejun Heo 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
189981afe893STejun Heo 
190081afe893STejun Heo 	/* Device presence detection is unreliable on some
190181afe893STejun Heo 	 * controllers.  Always poll IDENTIFY if available.
190281afe893STejun Heo 	 */
190381afe893STejun Heo 	tf.flags |= ATA_TFLAG_POLLING;
1904c6fd2807SJeff Garzik 
1905963e4975SAlan Cox 	if (ap->ops->read_id)
1906963e4975SAlan Cox 		err_mask = ap->ops->read_id(dev, &tf, id);
1907963e4975SAlan Cox 	else
1908963e4975SAlan Cox 		err_mask = ata_do_dev_read_id(dev, &tf, id);
1909963e4975SAlan Cox 
1910c6fd2807SJeff Garzik 	if (err_mask) {
1911800b3996STejun Heo 		if (err_mask & AC_ERR_NODEV_HINT) {
1912a9a79dfeSJoe Perches 			ata_dev_dbg(dev, "NODEV after polling detection\n");
191355a8e2c8STejun Heo 			return -ENOENT;
191455a8e2c8STejun Heo 		}
191555a8e2c8STejun Heo 
191679b42babSTejun Heo 		if (is_semb) {
1917a9a79dfeSJoe Perches 			ata_dev_info(dev,
1918a9a79dfeSJoe Perches 		     "IDENTIFY failed on device w/ SEMB sig, disabled\n");
191979b42babSTejun Heo 			/* SEMB is not supported yet */
192079b42babSTejun Heo 			*p_class = ATA_DEV_SEMB_UNSUP;
192179b42babSTejun Heo 			return 0;
192279b42babSTejun Heo 		}
192379b42babSTejun Heo 
19241ffc151fSTejun Heo 		if ((err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
19251ffc151fSTejun Heo 			/* Device or controller might have reported
19261ffc151fSTejun Heo 			 * the wrong device class.  Give a shot at the
19271ffc151fSTejun Heo 			 * other IDENTIFY if the current one is
19281ffc151fSTejun Heo 			 * aborted by the device.
192954936f8bSTejun Heo 			 */
19301ffc151fSTejun Heo 			if (may_fallback) {
193154936f8bSTejun Heo 				may_fallback = 0;
193254936f8bSTejun Heo 
193354936f8bSTejun Heo 				if (class == ATA_DEV_ATA)
193454936f8bSTejun Heo 					class = ATA_DEV_ATAPI;
193554936f8bSTejun Heo 				else
193654936f8bSTejun Heo 					class = ATA_DEV_ATA;
193754936f8bSTejun Heo 				goto retry;
193854936f8bSTejun Heo 			}
193954936f8bSTejun Heo 
19401ffc151fSTejun Heo 			/* Control reaches here iff the device aborted
19411ffc151fSTejun Heo 			 * both flavors of IDENTIFYs which happens
19421ffc151fSTejun Heo 			 * sometimes with phantom devices.
19431ffc151fSTejun Heo 			 */
1944a9a79dfeSJoe Perches 			ata_dev_dbg(dev,
19451ffc151fSTejun Heo 				    "both IDENTIFYs aborted, assuming NODEV\n");
19461ffc151fSTejun Heo 			return -ENOENT;
19471ffc151fSTejun Heo 		}
19481ffc151fSTejun Heo 
1949c6fd2807SJeff Garzik 		rc = -EIO;
1950c6fd2807SJeff Garzik 		reason = "I/O error";
1951c6fd2807SJeff Garzik 		goto err_out;
1952c6fd2807SJeff Garzik 	}
1953c6fd2807SJeff Garzik 
195443c9c591STejun Heo 	if (dev->horkage & ATA_HORKAGE_DUMP_ID) {
1955a9a79dfeSJoe Perches 		ata_dev_dbg(dev, "dumping IDENTIFY data, "
195643c9c591STejun Heo 			    "class=%d may_fallback=%d tried_spinup=%d\n",
195743c9c591STejun Heo 			    class, may_fallback, tried_spinup);
195843c9c591STejun Heo 		print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET,
195943c9c591STejun Heo 			       16, 2, id, ATA_ID_WORDS * sizeof(*id), true);
196043c9c591STejun Heo 	}
196143c9c591STejun Heo 
196254936f8bSTejun Heo 	/* Falling back doesn't make sense if ID data was read
196354936f8bSTejun Heo 	 * successfully at least once.
196454936f8bSTejun Heo 	 */
196554936f8bSTejun Heo 	may_fallback = 0;
196654936f8bSTejun Heo 
1967c6fd2807SJeff Garzik 	swap_buf_le16(id, ATA_ID_WORDS);
1968c6fd2807SJeff Garzik 
1969c6fd2807SJeff Garzik 	/* sanity check */
1970c6fd2807SJeff Garzik 	rc = -EINVAL;
19716070068bSAlan Cox 	reason = "device reports invalid type";
19724a3381feSJeff Garzik 
19734a3381feSJeff Garzik 	if (class == ATA_DEV_ATA) {
19744a3381feSJeff Garzik 		if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
19754a3381feSJeff Garzik 			goto err_out;
19764a3381feSJeff Garzik 	} else {
19774a3381feSJeff Garzik 		if (ata_id_is_ata(id))
1978c6fd2807SJeff Garzik 			goto err_out;
1979c6fd2807SJeff Garzik 	}
1980c6fd2807SJeff Garzik 
1981169439c2SMark Lord 	if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
1982169439c2SMark Lord 		tried_spinup = 1;
1983169439c2SMark Lord 		/*
1984169439c2SMark Lord 		 * Drive powered-up in standby mode, and requires a specific
1985169439c2SMark Lord 		 * SET_FEATURES spin-up subcommand before it will accept
1986169439c2SMark Lord 		 * anything other than the original IDENTIFY command.
1987169439c2SMark Lord 		 */
1988218f3d30SJeff Garzik 		err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
1989fb0582f9SRyan Power 		if (err_mask && id[2] != 0x738c) {
1990169439c2SMark Lord 			rc = -EIO;
1991169439c2SMark Lord 			reason = "SPINUP failed";
1992169439c2SMark Lord 			goto err_out;
1993169439c2SMark Lord 		}
1994169439c2SMark Lord 		/*
1995169439c2SMark Lord 		 * If the drive initially returned incomplete IDENTIFY info,
1996169439c2SMark Lord 		 * we now must reissue the IDENTIFY command.
1997169439c2SMark Lord 		 */
1998169439c2SMark Lord 		if (id[2] == 0x37c8)
1999169439c2SMark Lord 			goto retry;
2000169439c2SMark Lord 	}
2001169439c2SMark Lord 
2002bff04647STejun Heo 	if ((flags & ATA_READID_POSTRESET) && class == ATA_DEV_ATA) {
2003c6fd2807SJeff Garzik 		/*
2004c6fd2807SJeff Garzik 		 * The exact sequence expected by certain pre-ATA4 drives is:
2005c6fd2807SJeff Garzik 		 * SRST RESET
200650a99018SAlan Cox 		 * IDENTIFY (optional in early ATA)
200750a99018SAlan Cox 		 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
2008c6fd2807SJeff Garzik 		 * anything else..
2009c6fd2807SJeff Garzik 		 * Some drives were very specific about that exact sequence.
201050a99018SAlan Cox 		 *
201150a99018SAlan Cox 		 * Note that ATA4 says lba is mandatory so the second check
2012c9404c9cSAdam Buchbinder 		 * should never trigger.
2013c6fd2807SJeff Garzik 		 */
2014c6fd2807SJeff Garzik 		if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
2015c6fd2807SJeff Garzik 			err_mask = ata_dev_init_params(dev, id[3], id[6]);
2016c6fd2807SJeff Garzik 			if (err_mask) {
2017c6fd2807SJeff Garzik 				rc = -EIO;
2018c6fd2807SJeff Garzik 				reason = "INIT_DEV_PARAMS failed";
2019c6fd2807SJeff Garzik 				goto err_out;
2020c6fd2807SJeff Garzik 			}
2021c6fd2807SJeff Garzik 
2022c6fd2807SJeff Garzik 			/* current CHS translation info (id[53-58]) might be
2023c6fd2807SJeff Garzik 			 * changed. reread the identify device info.
2024c6fd2807SJeff Garzik 			 */
2025bff04647STejun Heo 			flags &= ~ATA_READID_POSTRESET;
2026c6fd2807SJeff Garzik 			goto retry;
2027c6fd2807SJeff Garzik 		}
2028c6fd2807SJeff Garzik 	}
2029c6fd2807SJeff Garzik 
2030c6fd2807SJeff Garzik 	*p_class = class;
2031c6fd2807SJeff Garzik 
2032c6fd2807SJeff Garzik 	return 0;
2033c6fd2807SJeff Garzik 
2034c6fd2807SJeff Garzik  err_out:
2035c6fd2807SJeff Garzik 	if (ata_msg_warn(ap))
2036a9a79dfeSJoe Perches 		ata_dev_warn(dev, "failed to IDENTIFY (%s, err_mask=0x%x)\n",
2037a9a79dfeSJoe Perches 			     reason, err_mask);
2038c6fd2807SJeff Garzik 	return rc;
2039c6fd2807SJeff Garzik }
2040c6fd2807SJeff Garzik 
20419062712fSTejun Heo static int ata_do_link_spd_horkage(struct ata_device *dev)
20429062712fSTejun Heo {
20439062712fSTejun Heo 	struct ata_link *plink = ata_dev_phys_link(dev);
20449062712fSTejun Heo 	u32 target, target_limit;
20459062712fSTejun Heo 
20469062712fSTejun Heo 	if (!sata_scr_valid(plink))
20479062712fSTejun Heo 		return 0;
20489062712fSTejun Heo 
20499062712fSTejun Heo 	if (dev->horkage & ATA_HORKAGE_1_5_GBPS)
20509062712fSTejun Heo 		target = 1;
20519062712fSTejun Heo 	else
20529062712fSTejun Heo 		return 0;
20539062712fSTejun Heo 
20549062712fSTejun Heo 	target_limit = (1 << target) - 1;
20559062712fSTejun Heo 
20569062712fSTejun Heo 	/* if already on stricter limit, no need to push further */
20579062712fSTejun Heo 	if (plink->sata_spd_limit <= target_limit)
20589062712fSTejun Heo 		return 0;
20599062712fSTejun Heo 
20609062712fSTejun Heo 	plink->sata_spd_limit = target_limit;
20619062712fSTejun Heo 
20629062712fSTejun Heo 	/* Request another EH round by returning -EAGAIN if link is
20639062712fSTejun Heo 	 * going faster than the target speed.  Forward progress is
20649062712fSTejun Heo 	 * guaranteed by setting sata_spd_limit to target_limit above.
20659062712fSTejun Heo 	 */
20669062712fSTejun Heo 	if (plink->sata_spd > target) {
2067a9a79dfeSJoe Perches 		ata_dev_info(dev, "applying link speed limit horkage to %s\n",
20689062712fSTejun Heo 			     sata_spd_string(target));
20699062712fSTejun Heo 		return -EAGAIN;
20709062712fSTejun Heo 	}
20719062712fSTejun Heo 	return 0;
20729062712fSTejun Heo }
20739062712fSTejun Heo 
2074c6fd2807SJeff Garzik static inline u8 ata_dev_knobble(struct ata_device *dev)
2075c6fd2807SJeff Garzik {
20769af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
20779ce8e307SJens Axboe 
20789ce8e307SJens Axboe 	if (ata_dev_blacklisted(dev) & ATA_HORKAGE_BRIDGE_OK)
20799ce8e307SJens Axboe 		return 0;
20809ce8e307SJens Axboe 
20819af5c9c9STejun Heo 	return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
2082c6fd2807SJeff Garzik }
2083c6fd2807SJeff Garzik 
2084388539f3SShaohua Li static int ata_dev_config_ncq(struct ata_device *dev,
2085c6fd2807SJeff Garzik 			       char *desc, size_t desc_sz)
2086c6fd2807SJeff Garzik {
20879af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
2088c6fd2807SJeff Garzik 	int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
2089388539f3SShaohua Li 	unsigned int err_mask;
2090388539f3SShaohua Li 	char *aa_desc = "";
2091c6fd2807SJeff Garzik 
2092c6fd2807SJeff Garzik 	if (!ata_id_has_ncq(dev->id)) {
2093c6fd2807SJeff Garzik 		desc[0] = '\0';
2094388539f3SShaohua Li 		return 0;
2095c6fd2807SJeff Garzik 	}
209675683fe7STejun Heo 	if (dev->horkage & ATA_HORKAGE_NONCQ) {
20976919a0a6SAlan Cox 		snprintf(desc, desc_sz, "NCQ (not used)");
2098388539f3SShaohua Li 		return 0;
20996919a0a6SAlan Cox 	}
2100c6fd2807SJeff Garzik 	if (ap->flags & ATA_FLAG_NCQ) {
2101cca3974eSJeff Garzik 		hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE - 1);
2102c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_NCQ;
2103c6fd2807SJeff Garzik 	}
2104c6fd2807SJeff Garzik 
2105388539f3SShaohua Li 	if (!(dev->horkage & ATA_HORKAGE_BROKEN_FPDMA_AA) &&
2106388539f3SShaohua Li 		(ap->flags & ATA_FLAG_FPDMA_AA) &&
2107388539f3SShaohua Li 		ata_id_has_fpdma_aa(dev->id)) {
2108388539f3SShaohua Li 		err_mask = ata_dev_set_feature(dev, SETFEATURES_SATA_ENABLE,
2109388539f3SShaohua Li 			SATA_FPDMA_AA);
2110388539f3SShaohua Li 		if (err_mask) {
2111a9a79dfeSJoe Perches 			ata_dev_err(dev,
2112a9a79dfeSJoe Perches 				    "failed to enable AA (error_mask=0x%x)\n",
2113a9a79dfeSJoe Perches 				    err_mask);
2114388539f3SShaohua Li 			if (err_mask != AC_ERR_DEV) {
2115388539f3SShaohua Li 				dev->horkage |= ATA_HORKAGE_BROKEN_FPDMA_AA;
2116388539f3SShaohua Li 				return -EIO;
2117388539f3SShaohua Li 			}
2118388539f3SShaohua Li 		} else
2119388539f3SShaohua Li 			aa_desc = ", AA";
2120388539f3SShaohua Li 	}
2121388539f3SShaohua Li 
2122c6fd2807SJeff Garzik 	if (hdepth >= ddepth)
2123388539f3SShaohua Li 		snprintf(desc, desc_sz, "NCQ (depth %d)%s", ddepth, aa_desc);
2124c6fd2807SJeff Garzik 	else
2125388539f3SShaohua Li 		snprintf(desc, desc_sz, "NCQ (depth %d/%d)%s", hdepth,
2126388539f3SShaohua Li 			ddepth, aa_desc);
2127388539f3SShaohua Li 	return 0;
2128c6fd2807SJeff Garzik }
2129c6fd2807SJeff Garzik 
2130c6fd2807SJeff Garzik /**
2131c6fd2807SJeff Garzik  *	ata_dev_configure - Configure the specified ATA/ATAPI device
2132c6fd2807SJeff Garzik  *	@dev: Target device to configure
2133c6fd2807SJeff Garzik  *
2134c6fd2807SJeff Garzik  *	Configure @dev according to @dev->id.  Generic and low-level
2135c6fd2807SJeff Garzik  *	driver specific fixups are also applied.
2136c6fd2807SJeff Garzik  *
2137c6fd2807SJeff Garzik  *	LOCKING:
2138c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
2139c6fd2807SJeff Garzik  *
2140c6fd2807SJeff Garzik  *	RETURNS:
2141c6fd2807SJeff Garzik  *	0 on success, -errno otherwise
2142c6fd2807SJeff Garzik  */
2143efdaedc4STejun Heo int ata_dev_configure(struct ata_device *dev)
2144c6fd2807SJeff Garzik {
21459af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
21469af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
21476746544cSTejun Heo 	int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
2148c6fd2807SJeff Garzik 	const u16 *id = dev->id;
21497dc951aeSTejun Heo 	unsigned long xfer_mask;
2150b352e57dSAlan Cox 	char revbuf[7];		/* XYZ-99\0 */
21513f64f565SEric D. Mudama 	char fwrevbuf[ATA_ID_FW_REV_LEN+1];
21523f64f565SEric D. Mudama 	char modelbuf[ATA_ID_PROD_LEN+1];
2153c6fd2807SJeff Garzik 	int rc;
2154c6fd2807SJeff Garzik 
2155c6fd2807SJeff Garzik 	if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
2156a9a79dfeSJoe Perches 		ata_dev_info(dev, "%s: ENTER/EXIT -- nodev\n", __func__);
2157c6fd2807SJeff Garzik 		return 0;
2158c6fd2807SJeff Garzik 	}
2159c6fd2807SJeff Garzik 
2160c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2161a9a79dfeSJoe Perches 		ata_dev_dbg(dev, "%s: ENTER\n", __func__);
2162c6fd2807SJeff Garzik 
216375683fe7STejun Heo 	/* set horkage */
216475683fe7STejun Heo 	dev->horkage |= ata_dev_blacklisted(dev);
216533267325STejun Heo 	ata_force_horkage(dev);
216675683fe7STejun Heo 
216750af2fa1STejun Heo 	if (dev->horkage & ATA_HORKAGE_DISABLE) {
2168a9a79dfeSJoe Perches 		ata_dev_info(dev, "unsupported device, disabling\n");
216950af2fa1STejun Heo 		ata_dev_disable(dev);
217050af2fa1STejun Heo 		return 0;
217150af2fa1STejun Heo 	}
217250af2fa1STejun Heo 
21732486fa56STejun Heo 	if ((!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) &&
21742486fa56STejun Heo 	    dev->class == ATA_DEV_ATAPI) {
2175a9a79dfeSJoe Perches 		ata_dev_warn(dev, "WARNING: ATAPI is %s, device ignored\n",
21762486fa56STejun Heo 			     atapi_enabled ? "not supported with this driver"
21772486fa56STejun Heo 			     : "disabled");
21782486fa56STejun Heo 		ata_dev_disable(dev);
21792486fa56STejun Heo 		return 0;
21802486fa56STejun Heo 	}
21812486fa56STejun Heo 
21829062712fSTejun Heo 	rc = ata_do_link_spd_horkage(dev);
21839062712fSTejun Heo 	if (rc)
21849062712fSTejun Heo 		return rc;
21859062712fSTejun Heo 
21866746544cSTejun Heo 	/* let ACPI work its magic */
21876746544cSTejun Heo 	rc = ata_acpi_on_devcfg(dev);
21886746544cSTejun Heo 	if (rc)
21896746544cSTejun Heo 		return rc;
219008573a86SKristen Carlson Accardi 
219105027adcSTejun Heo 	/* massage HPA, do it early as it might change IDENTIFY data */
219205027adcSTejun Heo 	rc = ata_hpa_resize(dev);
219305027adcSTejun Heo 	if (rc)
219405027adcSTejun Heo 		return rc;
219505027adcSTejun Heo 
2196c6fd2807SJeff Garzik 	/* print device capabilities */
2197c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2198a9a79dfeSJoe Perches 		ata_dev_dbg(dev,
2199c6fd2807SJeff Garzik 			    "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
2200c6fd2807SJeff Garzik 			    "85:%04x 86:%04x 87:%04x 88:%04x\n",
22017f5e4e8dSHarvey Harrison 			    __func__,
2202c6fd2807SJeff Garzik 			    id[49], id[82], id[83], id[84],
2203c6fd2807SJeff Garzik 			    id[85], id[86], id[87], id[88]);
2204c6fd2807SJeff Garzik 
2205c6fd2807SJeff Garzik 	/* initialize to-be-configured parameters */
2206c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_CFG_MASK;
2207c6fd2807SJeff Garzik 	dev->max_sectors = 0;
2208c6fd2807SJeff Garzik 	dev->cdb_len = 0;
2209c6fd2807SJeff Garzik 	dev->n_sectors = 0;
2210c6fd2807SJeff Garzik 	dev->cylinders = 0;
2211c6fd2807SJeff Garzik 	dev->heads = 0;
2212c6fd2807SJeff Garzik 	dev->sectors = 0;
2213e18086d6SMark Lord 	dev->multi_count = 0;
2214c6fd2807SJeff Garzik 
2215c6fd2807SJeff Garzik 	/*
2216c6fd2807SJeff Garzik 	 * common ATA, ATAPI feature tests
2217c6fd2807SJeff Garzik 	 */
2218c6fd2807SJeff Garzik 
2219c6fd2807SJeff Garzik 	/* find max transfer mode; for printk only */
2220c6fd2807SJeff Garzik 	xfer_mask = ata_id_xfermask(id);
2221c6fd2807SJeff Garzik 
2222c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2223c6fd2807SJeff Garzik 		ata_dump_id(id);
2224c6fd2807SJeff Garzik 
2225ef143d57SAlbert Lee 	/* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
2226ef143d57SAlbert Lee 	ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
2227ef143d57SAlbert Lee 			sizeof(fwrevbuf));
2228ef143d57SAlbert Lee 
2229ef143d57SAlbert Lee 	ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
2230ef143d57SAlbert Lee 			sizeof(modelbuf));
2231ef143d57SAlbert Lee 
2232c6fd2807SJeff Garzik 	/* ATA-specific feature tests */
2233c6fd2807SJeff Garzik 	if (dev->class == ATA_DEV_ATA) {
2234b352e57dSAlan Cox 		if (ata_id_is_cfa(id)) {
223562afe5d7SSergei Shtylyov 			/* CPRM may make this media unusable */
223662afe5d7SSergei Shtylyov 			if (id[ATA_ID_CFA_KEY_MGMT] & 1)
2237a9a79dfeSJoe Perches 				ata_dev_warn(dev,
2238a9a79dfeSJoe Perches 	"supports DRM functions and may not be fully accessible\n");
2239b352e57dSAlan Cox 			snprintf(revbuf, 7, "CFA");
2240ae8d4ee7SAlan Cox 		} else {
2241b352e57dSAlan Cox 			snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
2242ae8d4ee7SAlan Cox 			/* Warn the user if the device has TPM extensions */
2243ae8d4ee7SAlan Cox 			if (ata_id_has_tpm(id))
2244a9a79dfeSJoe Perches 				ata_dev_warn(dev,
2245a9a79dfeSJoe Perches 	"supports DRM functions and may not be fully accessible\n");
2246ae8d4ee7SAlan Cox 		}
2247b352e57dSAlan Cox 
2248c6fd2807SJeff Garzik 		dev->n_sectors = ata_id_n_sectors(id);
2249c6fd2807SJeff Garzik 
2250e18086d6SMark Lord 		/* get current R/W Multiple count setting */
2251e18086d6SMark Lord 		if ((dev->id[47] >> 8) == 0x80 && (dev->id[59] & 0x100)) {
2252e18086d6SMark Lord 			unsigned int max = dev->id[47] & 0xff;
2253e18086d6SMark Lord 			unsigned int cnt = dev->id[59] & 0xff;
2254e18086d6SMark Lord 			/* only recognize/allow powers of two here */
2255e18086d6SMark Lord 			if (is_power_of_2(max) && is_power_of_2(cnt))
2256e18086d6SMark Lord 				if (cnt <= max)
2257e18086d6SMark Lord 					dev->multi_count = cnt;
2258e18086d6SMark Lord 		}
22593f64f565SEric D. Mudama 
2260c6fd2807SJeff Garzik 		if (ata_id_has_lba(id)) {
2261c6fd2807SJeff Garzik 			const char *lba_desc;
2262388539f3SShaohua Li 			char ncq_desc[24];
2263c6fd2807SJeff Garzik 
2264c6fd2807SJeff Garzik 			lba_desc = "LBA";
2265c6fd2807SJeff Garzik 			dev->flags |= ATA_DFLAG_LBA;
2266c6fd2807SJeff Garzik 			if (ata_id_has_lba48(id)) {
2267c6fd2807SJeff Garzik 				dev->flags |= ATA_DFLAG_LBA48;
2268c6fd2807SJeff Garzik 				lba_desc = "LBA48";
22696fc49adbSTejun Heo 
22706fc49adbSTejun Heo 				if (dev->n_sectors >= (1UL << 28) &&
22716fc49adbSTejun Heo 				    ata_id_has_flush_ext(id))
22726fc49adbSTejun Heo 					dev->flags |= ATA_DFLAG_FLUSH_EXT;
2273c6fd2807SJeff Garzik 			}
2274c6fd2807SJeff Garzik 
2275c6fd2807SJeff Garzik 			/* config NCQ */
2276388539f3SShaohua Li 			rc = ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
2277388539f3SShaohua Li 			if (rc)
2278388539f3SShaohua Li 				return rc;
2279c6fd2807SJeff Garzik 
2280c6fd2807SJeff Garzik 			/* print device info to dmesg */
22813f64f565SEric D. Mudama 			if (ata_msg_drv(ap) && print_info) {
2282a9a79dfeSJoe Perches 				ata_dev_info(dev, "%s: %s, %s, max %s\n",
22833f64f565SEric D. Mudama 					     revbuf, modelbuf, fwrevbuf,
22843f64f565SEric D. Mudama 					     ata_mode_string(xfer_mask));
2285a9a79dfeSJoe Perches 				ata_dev_info(dev,
2286a9a79dfeSJoe Perches 					     "%llu sectors, multi %u: %s %s\n",
2287c6fd2807SJeff Garzik 					(unsigned long long)dev->n_sectors,
22883f64f565SEric D. Mudama 					dev->multi_count, lba_desc, ncq_desc);
22893f64f565SEric D. Mudama 			}
2290c6fd2807SJeff Garzik 		} else {
2291c6fd2807SJeff Garzik 			/* CHS */
2292c6fd2807SJeff Garzik 
2293c6fd2807SJeff Garzik 			/* Default translation */
2294c6fd2807SJeff Garzik 			dev->cylinders	= id[1];
2295c6fd2807SJeff Garzik 			dev->heads	= id[3];
2296c6fd2807SJeff Garzik 			dev->sectors	= id[6];
2297c6fd2807SJeff Garzik 
2298c6fd2807SJeff Garzik 			if (ata_id_current_chs_valid(id)) {
2299c6fd2807SJeff Garzik 				/* Current CHS translation is valid. */
2300c6fd2807SJeff Garzik 				dev->cylinders = id[54];
2301c6fd2807SJeff Garzik 				dev->heads     = id[55];
2302c6fd2807SJeff Garzik 				dev->sectors   = id[56];
2303c6fd2807SJeff Garzik 			}
2304c6fd2807SJeff Garzik 
2305c6fd2807SJeff Garzik 			/* print device info to dmesg */
23063f64f565SEric D. Mudama 			if (ata_msg_drv(ap) && print_info) {
2307a9a79dfeSJoe Perches 				ata_dev_info(dev, "%s: %s, %s, max %s\n",
23083f64f565SEric D. Mudama 					     revbuf,	modelbuf, fwrevbuf,
23093f64f565SEric D. Mudama 					     ata_mode_string(xfer_mask));
2310a9a79dfeSJoe Perches 				ata_dev_info(dev,
2311a9a79dfeSJoe Perches 					     "%llu sectors, multi %u, CHS %u/%u/%u\n",
23123f64f565SEric D. Mudama 					     (unsigned long long)dev->n_sectors,
23133f64f565SEric D. Mudama 					     dev->multi_count, dev->cylinders,
23143f64f565SEric D. Mudama 					     dev->heads, dev->sectors);
23153f64f565SEric D. Mudama 			}
2316c6fd2807SJeff Garzik 		}
2317c6fd2807SJeff Garzik 
2318c6fd2807SJeff Garzik 		dev->cdb_len = 16;
2319c6fd2807SJeff Garzik 	}
2320c6fd2807SJeff Garzik 
2321c6fd2807SJeff Garzik 	/* ATAPI-specific feature tests */
2322c6fd2807SJeff Garzik 	else if (dev->class == ATA_DEV_ATAPI) {
2323854c73a2STejun Heo 		const char *cdb_intr_string = "";
2324854c73a2STejun Heo 		const char *atapi_an_string = "";
232591163006STejun Heo 		const char *dma_dir_string = "";
23267d77b247STejun Heo 		u32 sntf;
2327c6fd2807SJeff Garzik 
2328c6fd2807SJeff Garzik 		rc = atapi_cdb_len(id);
2329c6fd2807SJeff Garzik 		if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
2330c6fd2807SJeff Garzik 			if (ata_msg_warn(ap))
2331a9a79dfeSJoe Perches 				ata_dev_warn(dev, "unsupported CDB len\n");
2332c6fd2807SJeff Garzik 			rc = -EINVAL;
2333c6fd2807SJeff Garzik 			goto err_out_nosup;
2334c6fd2807SJeff Garzik 		}
2335c6fd2807SJeff Garzik 		dev->cdb_len = (unsigned int) rc;
2336c6fd2807SJeff Garzik 
23377d77b247STejun Heo 		/* Enable ATAPI AN if both the host and device have
23387d77b247STejun Heo 		 * the support.  If PMP is attached, SNTF is required
23397d77b247STejun Heo 		 * to enable ATAPI AN to discern between PHY status
23407d77b247STejun Heo 		 * changed notifications and ATAPI ANs.
23419f45cbd3SKristen Carlson Accardi 		 */
2342e7ecd435STejun Heo 		if (atapi_an &&
2343e7ecd435STejun Heo 		    (ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
2344071f44b1STejun Heo 		    (!sata_pmp_attached(ap) ||
23457d77b247STejun Heo 		     sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
2346854c73a2STejun Heo 			unsigned int err_mask;
2347854c73a2STejun Heo 
23489f45cbd3SKristen Carlson Accardi 			/* issue SET feature command to turn this on */
2349218f3d30SJeff Garzik 			err_mask = ata_dev_set_feature(dev,
2350218f3d30SJeff Garzik 					SETFEATURES_SATA_ENABLE, SATA_AN);
2351854c73a2STejun Heo 			if (err_mask)
2352a9a79dfeSJoe Perches 				ata_dev_err(dev,
2353a9a79dfeSJoe Perches 					    "failed to enable ATAPI AN (err_mask=0x%x)\n",
2354a9a79dfeSJoe Perches 					    err_mask);
2355854c73a2STejun Heo 			else {
23569f45cbd3SKristen Carlson Accardi 				dev->flags |= ATA_DFLAG_AN;
2357854c73a2STejun Heo 				atapi_an_string = ", ATAPI AN";
2358854c73a2STejun Heo 			}
23599f45cbd3SKristen Carlson Accardi 		}
23609f45cbd3SKristen Carlson Accardi 
2361c6fd2807SJeff Garzik 		if (ata_id_cdb_intr(dev->id)) {
2362c6fd2807SJeff Garzik 			dev->flags |= ATA_DFLAG_CDB_INTR;
2363c6fd2807SJeff Garzik 			cdb_intr_string = ", CDB intr";
2364c6fd2807SJeff Garzik 		}
2365c6fd2807SJeff Garzik 
236691163006STejun Heo 		if (atapi_dmadir || atapi_id_dmadir(dev->id)) {
236791163006STejun Heo 			dev->flags |= ATA_DFLAG_DMADIR;
236891163006STejun Heo 			dma_dir_string = ", DMADIR";
236991163006STejun Heo 		}
237091163006STejun Heo 
2371c6fd2807SJeff Garzik 		/* print device info to dmesg */
2372c6fd2807SJeff Garzik 		if (ata_msg_drv(ap) && print_info)
2373a9a79dfeSJoe Perches 			ata_dev_info(dev,
237491163006STejun Heo 				     "ATAPI: %s, %s, max %s%s%s%s\n",
2375ef143d57SAlbert Lee 				     modelbuf, fwrevbuf,
2376c6fd2807SJeff Garzik 				     ata_mode_string(xfer_mask),
237791163006STejun Heo 				     cdb_intr_string, atapi_an_string,
237891163006STejun Heo 				     dma_dir_string);
2379c6fd2807SJeff Garzik 	}
2380c6fd2807SJeff Garzik 
2381914ed354STejun Heo 	/* determine max_sectors */
2382914ed354STejun Heo 	dev->max_sectors = ATA_MAX_SECTORS;
2383914ed354STejun Heo 	if (dev->flags & ATA_DFLAG_LBA48)
2384914ed354STejun Heo 		dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2385914ed354STejun Heo 
2386c5038fc0SAlan Cox 	/* Limit PATA drive on SATA cable bridge transfers to udma5,
2387c5038fc0SAlan Cox 	   200 sectors */
2388c6fd2807SJeff Garzik 	if (ata_dev_knobble(dev)) {
2389c6fd2807SJeff Garzik 		if (ata_msg_drv(ap) && print_info)
2390a9a79dfeSJoe Perches 			ata_dev_info(dev, "applying bridge limits\n");
2391c6fd2807SJeff Garzik 		dev->udma_mask &= ATA_UDMA5;
2392c6fd2807SJeff Garzik 		dev->max_sectors = ATA_MAX_SECTORS;
2393c6fd2807SJeff Garzik 	}
2394c6fd2807SJeff Garzik 
2395f8d8e579STony Battersby 	if ((dev->class == ATA_DEV_ATAPI) &&
2396f442cd86SAlbert Lee 	    (atapi_command_packet_set(id) == TYPE_TAPE)) {
2397f8d8e579STony Battersby 		dev->max_sectors = ATA_MAX_SECTORS_TAPE;
2398f442cd86SAlbert Lee 		dev->horkage |= ATA_HORKAGE_STUCK_ERR;
2399f442cd86SAlbert Lee 	}
2400f8d8e579STony Battersby 
240175683fe7STejun Heo 	if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
240203ec52deSTejun Heo 		dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
240303ec52deSTejun Heo 					 dev->max_sectors);
240418d6e9d5SAlbert Lee 
2405c6fd2807SJeff Garzik 	if (ap->ops->dev_config)
2406cd0d3bbcSAlan 		ap->ops->dev_config(dev);
2407c6fd2807SJeff Garzik 
2408c5038fc0SAlan Cox 	if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2409c5038fc0SAlan Cox 		/* Let the user know. We don't want to disallow opens for
2410c5038fc0SAlan Cox 		   rescue purposes, or in case the vendor is just a blithering
2411c5038fc0SAlan Cox 		   idiot. Do this after the dev_config call as some controllers
2412c5038fc0SAlan Cox 		   with buggy firmware may want to avoid reporting false device
2413c5038fc0SAlan Cox 		   bugs */
2414c5038fc0SAlan Cox 
2415c5038fc0SAlan Cox 		if (print_info) {
2416a9a79dfeSJoe Perches 			ata_dev_warn(dev,
2417c5038fc0SAlan Cox "Drive reports diagnostics failure. This may indicate a drive\n");
2418a9a79dfeSJoe Perches 			ata_dev_warn(dev,
2419c5038fc0SAlan Cox "fault or invalid emulation. Contact drive vendor for information.\n");
2420c5038fc0SAlan Cox 		}
2421c5038fc0SAlan Cox 	}
2422c5038fc0SAlan Cox 
2423ac70a964STejun Heo 	if ((dev->horkage & ATA_HORKAGE_FIRMWARE_WARN) && print_info) {
2424a9a79dfeSJoe Perches 		ata_dev_warn(dev, "WARNING: device requires firmware update to be fully functional\n");
2425a9a79dfeSJoe Perches 		ata_dev_warn(dev, "         contact the vendor or visit http://ata.wiki.kernel.org\n");
2426ac70a964STejun Heo 	}
2427ac70a964STejun Heo 
2428c6fd2807SJeff Garzik 	return 0;
2429c6fd2807SJeff Garzik 
2430c6fd2807SJeff Garzik err_out_nosup:
2431c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2432a9a79dfeSJoe Perches 		ata_dev_dbg(dev, "%s: EXIT, err\n", __func__);
2433c6fd2807SJeff Garzik 	return rc;
2434c6fd2807SJeff Garzik }
2435c6fd2807SJeff Garzik 
2436c6fd2807SJeff Garzik /**
24372e41e8e6SAlan Cox  *	ata_cable_40wire	-	return 40 wire cable type
2438be0d18dfSAlan Cox  *	@ap: port
2439be0d18dfSAlan Cox  *
24402e41e8e6SAlan Cox  *	Helper method for drivers which want to hardwire 40 wire cable
2441be0d18dfSAlan Cox  *	detection.
2442be0d18dfSAlan Cox  */
2443be0d18dfSAlan Cox 
2444be0d18dfSAlan Cox int ata_cable_40wire(struct ata_port *ap)
2445be0d18dfSAlan Cox {
2446be0d18dfSAlan Cox 	return ATA_CBL_PATA40;
2447be0d18dfSAlan Cox }
2448be0d18dfSAlan Cox 
2449be0d18dfSAlan Cox /**
24502e41e8e6SAlan Cox  *	ata_cable_80wire	-	return 80 wire cable type
2451be0d18dfSAlan Cox  *	@ap: port
2452be0d18dfSAlan Cox  *
24532e41e8e6SAlan Cox  *	Helper method for drivers which want to hardwire 80 wire cable
2454be0d18dfSAlan Cox  *	detection.
2455be0d18dfSAlan Cox  */
2456be0d18dfSAlan Cox 
2457be0d18dfSAlan Cox int ata_cable_80wire(struct ata_port *ap)
2458be0d18dfSAlan Cox {
2459be0d18dfSAlan Cox 	return ATA_CBL_PATA80;
2460be0d18dfSAlan Cox }
2461be0d18dfSAlan Cox 
2462be0d18dfSAlan Cox /**
2463be0d18dfSAlan Cox  *	ata_cable_unknown	-	return unknown PATA cable.
2464be0d18dfSAlan Cox  *	@ap: port
2465be0d18dfSAlan Cox  *
2466be0d18dfSAlan Cox  *	Helper method for drivers which have no PATA cable detection.
2467be0d18dfSAlan Cox  */
2468be0d18dfSAlan Cox 
2469be0d18dfSAlan Cox int ata_cable_unknown(struct ata_port *ap)
2470be0d18dfSAlan Cox {
2471be0d18dfSAlan Cox 	return ATA_CBL_PATA_UNK;
2472be0d18dfSAlan Cox }
2473be0d18dfSAlan Cox 
2474be0d18dfSAlan Cox /**
2475c88f90c3STejun Heo  *	ata_cable_ignore	-	return ignored PATA cable.
2476c88f90c3STejun Heo  *	@ap: port
2477c88f90c3STejun Heo  *
2478c88f90c3STejun Heo  *	Helper method for drivers which don't use cable type to limit
2479c88f90c3STejun Heo  *	transfer mode.
2480c88f90c3STejun Heo  */
2481c88f90c3STejun Heo int ata_cable_ignore(struct ata_port *ap)
2482c88f90c3STejun Heo {
2483c88f90c3STejun Heo 	return ATA_CBL_PATA_IGN;
2484c88f90c3STejun Heo }
2485c88f90c3STejun Heo 
2486c88f90c3STejun Heo /**
2487be0d18dfSAlan Cox  *	ata_cable_sata	-	return SATA cable type
2488be0d18dfSAlan Cox  *	@ap: port
2489be0d18dfSAlan Cox  *
2490be0d18dfSAlan Cox  *	Helper method for drivers which have SATA cables
2491be0d18dfSAlan Cox  */
2492be0d18dfSAlan Cox 
2493be0d18dfSAlan Cox int ata_cable_sata(struct ata_port *ap)
2494be0d18dfSAlan Cox {
2495be0d18dfSAlan Cox 	return ATA_CBL_SATA;
2496be0d18dfSAlan Cox }
2497be0d18dfSAlan Cox 
2498be0d18dfSAlan Cox /**
2499c6fd2807SJeff Garzik  *	ata_bus_probe - Reset and probe ATA bus
2500c6fd2807SJeff Garzik  *	@ap: Bus to probe
2501c6fd2807SJeff Garzik  *
2502c6fd2807SJeff Garzik  *	Master ATA bus probing function.  Initiates a hardware-dependent
2503c6fd2807SJeff Garzik  *	bus reset, then attempts to identify any devices found on
2504c6fd2807SJeff Garzik  *	the bus.
2505c6fd2807SJeff Garzik  *
2506c6fd2807SJeff Garzik  *	LOCKING:
2507c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
2508c6fd2807SJeff Garzik  *
2509c6fd2807SJeff Garzik  *	RETURNS:
2510c6fd2807SJeff Garzik  *	Zero on success, negative errno otherwise.
2511c6fd2807SJeff Garzik  */
2512c6fd2807SJeff Garzik 
2513c6fd2807SJeff Garzik int ata_bus_probe(struct ata_port *ap)
2514c6fd2807SJeff Garzik {
2515c6fd2807SJeff Garzik 	unsigned int classes[ATA_MAX_DEVICES];
2516c6fd2807SJeff Garzik 	int tries[ATA_MAX_DEVICES];
2517f58229f8STejun Heo 	int rc;
2518c6fd2807SJeff Garzik 	struct ata_device *dev;
2519c6fd2807SJeff Garzik 
25201eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL)
2521f58229f8STejun Heo 		tries[dev->devno] = ATA_PROBE_MAX_TRIES;
2522c6fd2807SJeff Garzik 
2523c6fd2807SJeff Garzik  retry:
25241eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL) {
2525cdeab114STejun Heo 		/* If we issue an SRST then an ATA drive (not ATAPI)
2526cdeab114STejun Heo 		 * may change configuration and be in PIO0 timing. If
2527cdeab114STejun Heo 		 * we do a hard reset (or are coming from power on)
2528cdeab114STejun Heo 		 * this is true for ATA or ATAPI. Until we've set a
2529cdeab114STejun Heo 		 * suitable controller mode we should not touch the
2530cdeab114STejun Heo 		 * bus as we may be talking too fast.
2531cdeab114STejun Heo 		 */
2532cdeab114STejun Heo 		dev->pio_mode = XFER_PIO_0;
2533cdeab114STejun Heo 
2534cdeab114STejun Heo 		/* If the controller has a pio mode setup function
2535cdeab114STejun Heo 		 * then use it to set the chipset to rights. Don't
2536cdeab114STejun Heo 		 * touch the DMA setup as that will be dealt with when
2537cdeab114STejun Heo 		 * configuring devices.
2538cdeab114STejun Heo 		 */
2539cdeab114STejun Heo 		if (ap->ops->set_piomode)
2540cdeab114STejun Heo 			ap->ops->set_piomode(ap, dev);
2541cdeab114STejun Heo 	}
2542cdeab114STejun Heo 
2543c6fd2807SJeff Garzik 	/* reset and determine device classes */
2544c6fd2807SJeff Garzik 	ap->ops->phy_reset(ap);
2545c6fd2807SJeff Garzik 
25461eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL) {
25473e4ec344STejun Heo 		if (dev->class != ATA_DEV_UNKNOWN)
2548c6fd2807SJeff Garzik 			classes[dev->devno] = dev->class;
2549c6fd2807SJeff Garzik 		else
2550c6fd2807SJeff Garzik 			classes[dev->devno] = ATA_DEV_NONE;
2551c6fd2807SJeff Garzik 
2552c6fd2807SJeff Garzik 		dev->class = ATA_DEV_UNKNOWN;
2553c6fd2807SJeff Garzik 	}
2554c6fd2807SJeff Garzik 
2555f31f0cc2SJeff Garzik 	/* read IDENTIFY page and configure devices. We have to do the identify
2556f31f0cc2SJeff Garzik 	   specific sequence bass-ackwards so that PDIAG- is released by
2557f31f0cc2SJeff Garzik 	   the slave device */
2558f31f0cc2SJeff Garzik 
25591eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL_REVERSE) {
2560f58229f8STejun Heo 		if (tries[dev->devno])
2561f58229f8STejun Heo 			dev->class = classes[dev->devno];
2562c6fd2807SJeff Garzik 
2563c6fd2807SJeff Garzik 		if (!ata_dev_enabled(dev))
2564c6fd2807SJeff Garzik 			continue;
2565c6fd2807SJeff Garzik 
2566bff04647STejun Heo 		rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2567bff04647STejun Heo 				     dev->id);
2568c6fd2807SJeff Garzik 		if (rc)
2569c6fd2807SJeff Garzik 			goto fail;
2570f31f0cc2SJeff Garzik 	}
2571f31f0cc2SJeff Garzik 
2572be0d18dfSAlan Cox 	/* Now ask for the cable type as PDIAG- should have been released */
2573be0d18dfSAlan Cox 	if (ap->ops->cable_detect)
2574be0d18dfSAlan Cox 		ap->cbl = ap->ops->cable_detect(ap);
2575be0d18dfSAlan Cox 
25761eca4365STejun Heo 	/* We may have SATA bridge glue hiding here irrespective of
25771eca4365STejun Heo 	 * the reported cable types and sensed types.  When SATA
25781eca4365STejun Heo 	 * drives indicate we have a bridge, we don't know which end
25791eca4365STejun Heo 	 * of the link the bridge is which is a problem.
25801eca4365STejun Heo 	 */
25811eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED)
2582614fe29bSAlan Cox 		if (ata_id_is_sata(dev->id))
2583614fe29bSAlan Cox 			ap->cbl = ATA_CBL_SATA;
2584614fe29bSAlan Cox 
2585f31f0cc2SJeff Garzik 	/* After the identify sequence we can now set up the devices. We do
2586f31f0cc2SJeff Garzik 	   this in the normal order so that the user doesn't get confused */
2587f31f0cc2SJeff Garzik 
25881eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED) {
25899af5c9c9STejun Heo 		ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
2590efdaedc4STejun Heo 		rc = ata_dev_configure(dev);
25919af5c9c9STejun Heo 		ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
2592c6fd2807SJeff Garzik 		if (rc)
2593c6fd2807SJeff Garzik 			goto fail;
2594c6fd2807SJeff Garzik 	}
2595c6fd2807SJeff Garzik 
2596c6fd2807SJeff Garzik 	/* configure transfer mode */
25970260731fSTejun Heo 	rc = ata_set_mode(&ap->link, &dev);
25984ae72a1eSTejun Heo 	if (rc)
2599c6fd2807SJeff Garzik 		goto fail;
2600c6fd2807SJeff Garzik 
26011eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED)
2602c6fd2807SJeff Garzik 		return 0;
2603c6fd2807SJeff Garzik 
2604c6fd2807SJeff Garzik 	return -ENODEV;
2605c6fd2807SJeff Garzik 
2606c6fd2807SJeff Garzik  fail:
26074ae72a1eSTejun Heo 	tries[dev->devno]--;
26084ae72a1eSTejun Heo 
2609c6fd2807SJeff Garzik 	switch (rc) {
2610c6fd2807SJeff Garzik 	case -EINVAL:
26114ae72a1eSTejun Heo 		/* eeek, something went very wrong, give up */
2612c6fd2807SJeff Garzik 		tries[dev->devno] = 0;
2613c6fd2807SJeff Garzik 		break;
26144ae72a1eSTejun Heo 
26154ae72a1eSTejun Heo 	case -ENODEV:
26164ae72a1eSTejun Heo 		/* give it just one more chance */
26174ae72a1eSTejun Heo 		tries[dev->devno] = min(tries[dev->devno], 1);
2618c6fd2807SJeff Garzik 	case -EIO:
26194ae72a1eSTejun Heo 		if (tries[dev->devno] == 1) {
26204ae72a1eSTejun Heo 			/* This is the last chance, better to slow
26214ae72a1eSTejun Heo 			 * down than lose it.
26224ae72a1eSTejun Heo 			 */
2623a07d499bSTejun Heo 			sata_down_spd_limit(&ap->link, 0);
26244ae72a1eSTejun Heo 			ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
26254ae72a1eSTejun Heo 		}
2626c6fd2807SJeff Garzik 	}
2627c6fd2807SJeff Garzik 
26284ae72a1eSTejun Heo 	if (!tries[dev->devno])
2629c6fd2807SJeff Garzik 		ata_dev_disable(dev);
2630c6fd2807SJeff Garzik 
2631c6fd2807SJeff Garzik 	goto retry;
2632c6fd2807SJeff Garzik }
2633c6fd2807SJeff Garzik 
2634c6fd2807SJeff Garzik /**
2635c6fd2807SJeff Garzik  *	sata_print_link_status - Print SATA link status
2636936fd732STejun Heo  *	@link: SATA link to printk link status about
2637c6fd2807SJeff Garzik  *
2638c6fd2807SJeff Garzik  *	This function prints link speed and status of a SATA link.
2639c6fd2807SJeff Garzik  *
2640c6fd2807SJeff Garzik  *	LOCKING:
2641c6fd2807SJeff Garzik  *	None.
2642c6fd2807SJeff Garzik  */
26436bdb4fc9SAdrian Bunk static void sata_print_link_status(struct ata_link *link)
2644c6fd2807SJeff Garzik {
2645c6fd2807SJeff Garzik 	u32 sstatus, scontrol, tmp;
2646c6fd2807SJeff Garzik 
2647936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus))
2648c6fd2807SJeff Garzik 		return;
2649936fd732STejun Heo 	sata_scr_read(link, SCR_CONTROL, &scontrol);
2650c6fd2807SJeff Garzik 
2651b1c72916STejun Heo 	if (ata_phys_link_online(link)) {
2652c6fd2807SJeff Garzik 		tmp = (sstatus >> 4) & 0xf;
2653a9a79dfeSJoe Perches 		ata_link_info(link, "SATA link up %s (SStatus %X SControl %X)\n",
2654c6fd2807SJeff Garzik 			      sata_spd_string(tmp), sstatus, scontrol);
2655c6fd2807SJeff Garzik 	} else {
2656a9a79dfeSJoe Perches 		ata_link_info(link, "SATA link down (SStatus %X SControl %X)\n",
2657c6fd2807SJeff Garzik 			      sstatus, scontrol);
2658c6fd2807SJeff Garzik 	}
2659c6fd2807SJeff Garzik }
2660c6fd2807SJeff Garzik 
2661c6fd2807SJeff Garzik /**
2662c6fd2807SJeff Garzik  *	ata_dev_pair		-	return other device on cable
2663c6fd2807SJeff Garzik  *	@adev: device
2664c6fd2807SJeff Garzik  *
2665c6fd2807SJeff Garzik  *	Obtain the other device on the same cable, or if none is
2666c6fd2807SJeff Garzik  *	present NULL is returned
2667c6fd2807SJeff Garzik  */
2668c6fd2807SJeff Garzik 
2669c6fd2807SJeff Garzik struct ata_device *ata_dev_pair(struct ata_device *adev)
2670c6fd2807SJeff Garzik {
26719af5c9c9STejun Heo 	struct ata_link *link = adev->link;
26729af5c9c9STejun Heo 	struct ata_device *pair = &link->device[1 - adev->devno];
2673c6fd2807SJeff Garzik 	if (!ata_dev_enabled(pair))
2674c6fd2807SJeff Garzik 		return NULL;
2675c6fd2807SJeff Garzik 	return pair;
2676c6fd2807SJeff Garzik }
2677c6fd2807SJeff Garzik 
2678c6fd2807SJeff Garzik /**
2679c6fd2807SJeff Garzik  *	sata_down_spd_limit - adjust SATA spd limit downward
2680936fd732STejun Heo  *	@link: Link to adjust SATA spd limit for
2681a07d499bSTejun Heo  *	@spd_limit: Additional limit
2682c6fd2807SJeff Garzik  *
2683936fd732STejun Heo  *	Adjust SATA spd limit of @link downward.  Note that this
2684c6fd2807SJeff Garzik  *	function only adjusts the limit.  The change must be applied
2685c6fd2807SJeff Garzik  *	using sata_set_spd().
2686c6fd2807SJeff Garzik  *
2687a07d499bSTejun Heo  *	If @spd_limit is non-zero, the speed is limited to equal to or
2688a07d499bSTejun Heo  *	lower than @spd_limit if such speed is supported.  If
2689a07d499bSTejun Heo  *	@spd_limit is slower than any supported speed, only the lowest
2690a07d499bSTejun Heo  *	supported speed is allowed.
2691a07d499bSTejun Heo  *
2692c6fd2807SJeff Garzik  *	LOCKING:
2693c6fd2807SJeff Garzik  *	Inherited from caller.
2694c6fd2807SJeff Garzik  *
2695c6fd2807SJeff Garzik  *	RETURNS:
2696c6fd2807SJeff Garzik  *	0 on success, negative errno on failure
2697c6fd2807SJeff Garzik  */
2698a07d499bSTejun Heo int sata_down_spd_limit(struct ata_link *link, u32 spd_limit)
2699c6fd2807SJeff Garzik {
2700c6fd2807SJeff Garzik 	u32 sstatus, spd, mask;
2701a07d499bSTejun Heo 	int rc, bit;
2702c6fd2807SJeff Garzik 
2703936fd732STejun Heo 	if (!sata_scr_valid(link))
2704008a7896STejun Heo 		return -EOPNOTSUPP;
2705008a7896STejun Heo 
2706008a7896STejun Heo 	/* If SCR can be read, use it to determine the current SPD.
2707936fd732STejun Heo 	 * If not, use cached value in link->sata_spd.
2708008a7896STejun Heo 	 */
2709936fd732STejun Heo 	rc = sata_scr_read(link, SCR_STATUS, &sstatus);
27109913ff8aSTejun Heo 	if (rc == 0 && ata_sstatus_online(sstatus))
2711008a7896STejun Heo 		spd = (sstatus >> 4) & 0xf;
2712008a7896STejun Heo 	else
2713936fd732STejun Heo 		spd = link->sata_spd;
2714c6fd2807SJeff Garzik 
2715936fd732STejun Heo 	mask = link->sata_spd_limit;
2716c6fd2807SJeff Garzik 	if (mask <= 1)
2717c6fd2807SJeff Garzik 		return -EINVAL;
2718008a7896STejun Heo 
2719008a7896STejun Heo 	/* unconditionally mask off the highest bit */
2720a07d499bSTejun Heo 	bit = fls(mask) - 1;
2721a07d499bSTejun Heo 	mask &= ~(1 << bit);
2722c6fd2807SJeff Garzik 
2723008a7896STejun Heo 	/* Mask off all speeds higher than or equal to the current
2724008a7896STejun Heo 	 * one.  Force 1.5Gbps if current SPD is not available.
2725008a7896STejun Heo 	 */
2726008a7896STejun Heo 	if (spd > 1)
2727008a7896STejun Heo 		mask &= (1 << (spd - 1)) - 1;
2728008a7896STejun Heo 	else
2729008a7896STejun Heo 		mask &= 1;
2730008a7896STejun Heo 
2731008a7896STejun Heo 	/* were we already at the bottom? */
2732c6fd2807SJeff Garzik 	if (!mask)
2733c6fd2807SJeff Garzik 		return -EINVAL;
2734c6fd2807SJeff Garzik 
2735a07d499bSTejun Heo 	if (spd_limit) {
2736a07d499bSTejun Heo 		if (mask & ((1 << spd_limit) - 1))
2737a07d499bSTejun Heo 			mask &= (1 << spd_limit) - 1;
2738a07d499bSTejun Heo 		else {
2739a07d499bSTejun Heo 			bit = ffs(mask) - 1;
2740a07d499bSTejun Heo 			mask = 1 << bit;
2741a07d499bSTejun Heo 		}
2742a07d499bSTejun Heo 	}
2743a07d499bSTejun Heo 
2744936fd732STejun Heo 	link->sata_spd_limit = mask;
2745c6fd2807SJeff Garzik 
2746a9a79dfeSJoe Perches 	ata_link_warn(link, "limiting SATA link speed to %s\n",
2747c6fd2807SJeff Garzik 		      sata_spd_string(fls(mask)));
2748c6fd2807SJeff Garzik 
2749c6fd2807SJeff Garzik 	return 0;
2750c6fd2807SJeff Garzik }
2751c6fd2807SJeff Garzik 
2752936fd732STejun Heo static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
2753c6fd2807SJeff Garzik {
27545270222fSTejun Heo 	struct ata_link *host_link = &link->ap->link;
27555270222fSTejun Heo 	u32 limit, target, spd;
2756c6fd2807SJeff Garzik 
27575270222fSTejun Heo 	limit = link->sata_spd_limit;
27585270222fSTejun Heo 
27595270222fSTejun Heo 	/* Don't configure downstream link faster than upstream link.
27605270222fSTejun Heo 	 * It doesn't speed up anything and some PMPs choke on such
27615270222fSTejun Heo 	 * configuration.
27625270222fSTejun Heo 	 */
27635270222fSTejun Heo 	if (!ata_is_host_link(link) && host_link->sata_spd)
27645270222fSTejun Heo 		limit &= (1 << host_link->sata_spd) - 1;
27655270222fSTejun Heo 
27665270222fSTejun Heo 	if (limit == UINT_MAX)
27675270222fSTejun Heo 		target = 0;
2768c6fd2807SJeff Garzik 	else
27695270222fSTejun Heo 		target = fls(limit);
2770c6fd2807SJeff Garzik 
2771c6fd2807SJeff Garzik 	spd = (*scontrol >> 4) & 0xf;
27725270222fSTejun Heo 	*scontrol = (*scontrol & ~0xf0) | ((target & 0xf) << 4);
2773c6fd2807SJeff Garzik 
27745270222fSTejun Heo 	return spd != target;
2775c6fd2807SJeff Garzik }
2776c6fd2807SJeff Garzik 
2777c6fd2807SJeff Garzik /**
2778c6fd2807SJeff Garzik  *	sata_set_spd_needed - is SATA spd configuration needed
2779936fd732STejun Heo  *	@link: Link in question
2780c6fd2807SJeff Garzik  *
2781c6fd2807SJeff Garzik  *	Test whether the spd limit in SControl matches
2782936fd732STejun Heo  *	@link->sata_spd_limit.  This function is used to determine
2783c6fd2807SJeff Garzik  *	whether hardreset is necessary to apply SATA spd
2784c6fd2807SJeff Garzik  *	configuration.
2785c6fd2807SJeff Garzik  *
2786c6fd2807SJeff Garzik  *	LOCKING:
2787c6fd2807SJeff Garzik  *	Inherited from caller.
2788c6fd2807SJeff Garzik  *
2789c6fd2807SJeff Garzik  *	RETURNS:
2790c6fd2807SJeff Garzik  *	1 if SATA spd configuration is needed, 0 otherwise.
2791c6fd2807SJeff Garzik  */
27921dc55e87SAdrian Bunk static int sata_set_spd_needed(struct ata_link *link)
2793c6fd2807SJeff Garzik {
2794c6fd2807SJeff Garzik 	u32 scontrol;
2795c6fd2807SJeff Garzik 
2796936fd732STejun Heo 	if (sata_scr_read(link, SCR_CONTROL, &scontrol))
2797db64bcf3STejun Heo 		return 1;
2798c6fd2807SJeff Garzik 
2799936fd732STejun Heo 	return __sata_set_spd_needed(link, &scontrol);
2800c6fd2807SJeff Garzik }
2801c6fd2807SJeff Garzik 
2802c6fd2807SJeff Garzik /**
2803c6fd2807SJeff Garzik  *	sata_set_spd - set SATA spd according to spd limit
2804936fd732STejun Heo  *	@link: Link to set SATA spd for
2805c6fd2807SJeff Garzik  *
2806936fd732STejun Heo  *	Set SATA spd of @link according to sata_spd_limit.
2807c6fd2807SJeff Garzik  *
2808c6fd2807SJeff Garzik  *	LOCKING:
2809c6fd2807SJeff Garzik  *	Inherited from caller.
2810c6fd2807SJeff Garzik  *
2811c6fd2807SJeff Garzik  *	RETURNS:
2812c6fd2807SJeff Garzik  *	0 if spd doesn't need to be changed, 1 if spd has been
2813c6fd2807SJeff Garzik  *	changed.  Negative errno if SCR registers are inaccessible.
2814c6fd2807SJeff Garzik  */
2815936fd732STejun Heo int sata_set_spd(struct ata_link *link)
2816c6fd2807SJeff Garzik {
2817c6fd2807SJeff Garzik 	u32 scontrol;
2818c6fd2807SJeff Garzik 	int rc;
2819c6fd2807SJeff Garzik 
2820936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
2821c6fd2807SJeff Garzik 		return rc;
2822c6fd2807SJeff Garzik 
2823936fd732STejun Heo 	if (!__sata_set_spd_needed(link, &scontrol))
2824c6fd2807SJeff Garzik 		return 0;
2825c6fd2807SJeff Garzik 
2826936fd732STejun Heo 	if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
2827c6fd2807SJeff Garzik 		return rc;
2828c6fd2807SJeff Garzik 
2829c6fd2807SJeff Garzik 	return 1;
2830c6fd2807SJeff Garzik }
2831c6fd2807SJeff Garzik 
2832c6fd2807SJeff Garzik /*
2833c6fd2807SJeff Garzik  * This mode timing computation functionality is ported over from
2834c6fd2807SJeff Garzik  * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
2835c6fd2807SJeff Garzik  */
2836c6fd2807SJeff Garzik /*
2837b352e57dSAlan Cox  * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
2838c6fd2807SJeff Garzik  * These were taken from ATA/ATAPI-6 standard, rev 0a, except
2839b352e57dSAlan Cox  * for UDMA6, which is currently supported only by Maxtor drives.
2840b352e57dSAlan Cox  *
2841b352e57dSAlan Cox  * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0.
2842c6fd2807SJeff Garzik  */
2843c6fd2807SJeff Garzik 
2844c6fd2807SJeff Garzik static const struct ata_timing ata_timing[] = {
28453ada9c12SDavid Daney /*	{ XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 0,  960,   0 }, */
28463ada9c12SDavid Daney 	{ XFER_PIO_0,     70, 290, 240, 600, 165, 150, 0,  600,   0 },
28473ada9c12SDavid Daney 	{ XFER_PIO_1,     50, 290,  93, 383, 125, 100, 0,  383,   0 },
28483ada9c12SDavid Daney 	{ XFER_PIO_2,     30, 290,  40, 330, 100,  90, 0,  240,   0 },
28493ada9c12SDavid Daney 	{ XFER_PIO_3,     30,  80,  70, 180,  80,  70, 0,  180,   0 },
28503ada9c12SDavid Daney 	{ XFER_PIO_4,     25,  70,  25, 120,  70,  25, 0,  120,   0 },
28513ada9c12SDavid Daney 	{ XFER_PIO_5,     15,  65,  25, 100,  65,  25, 0,  100,   0 },
28523ada9c12SDavid Daney 	{ XFER_PIO_6,     10,  55,  20,  80,  55,  20, 0,   80,   0 },
2853c6fd2807SJeff Garzik 
28543ada9c12SDavid Daney 	{ XFER_SW_DMA_0, 120,   0,   0,   0, 480, 480, 50, 960,   0 },
28553ada9c12SDavid Daney 	{ XFER_SW_DMA_1,  90,   0,   0,   0, 240, 240, 30, 480,   0 },
28563ada9c12SDavid Daney 	{ XFER_SW_DMA_2,  60,   0,   0,   0, 120, 120, 20, 240,   0 },
2857c6fd2807SJeff Garzik 
28583ada9c12SDavid Daney 	{ XFER_MW_DMA_0,  60,   0,   0,   0, 215, 215, 20, 480,   0 },
28593ada9c12SDavid Daney 	{ XFER_MW_DMA_1,  45,   0,   0,   0,  80,  50, 5,  150,   0 },
28603ada9c12SDavid Daney 	{ XFER_MW_DMA_2,  25,   0,   0,   0,  70,  25, 5,  120,   0 },
28613ada9c12SDavid Daney 	{ XFER_MW_DMA_3,  25,   0,   0,   0,  65,  25, 5,  100,   0 },
28623ada9c12SDavid Daney 	{ XFER_MW_DMA_4,  25,   0,   0,   0,  55,  20, 5,   80,   0 },
2863c6fd2807SJeff Garzik 
28643ada9c12SDavid Daney /*	{ XFER_UDMA_SLOW,  0,   0,   0,   0,   0,   0, 0,    0, 150 }, */
28653ada9c12SDavid Daney 	{ XFER_UDMA_0,     0,   0,   0,   0,   0,   0, 0,    0, 120 },
28663ada9c12SDavid Daney 	{ XFER_UDMA_1,     0,   0,   0,   0,   0,   0, 0,    0,  80 },
28673ada9c12SDavid Daney 	{ XFER_UDMA_2,     0,   0,   0,   0,   0,   0, 0,    0,  60 },
28683ada9c12SDavid Daney 	{ XFER_UDMA_3,     0,   0,   0,   0,   0,   0, 0,    0,  45 },
28693ada9c12SDavid Daney 	{ XFER_UDMA_4,     0,   0,   0,   0,   0,   0, 0,    0,  30 },
28703ada9c12SDavid Daney 	{ XFER_UDMA_5,     0,   0,   0,   0,   0,   0, 0,    0,  20 },
28713ada9c12SDavid Daney 	{ XFER_UDMA_6,     0,   0,   0,   0,   0,   0, 0,    0,  15 },
2872c6fd2807SJeff Garzik 
2873c6fd2807SJeff Garzik 	{ 0xFF }
2874c6fd2807SJeff Garzik };
2875c6fd2807SJeff Garzik 
2876c6fd2807SJeff Garzik #define ENOUGH(v, unit)		(((v)-1)/(unit)+1)
2877c6fd2807SJeff Garzik #define EZ(v, unit)		((v)?ENOUGH(v, unit):0)
2878c6fd2807SJeff Garzik 
2879c6fd2807SJeff Garzik static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
2880c6fd2807SJeff Garzik {
2881c6fd2807SJeff Garzik 	q->setup	= EZ(t->setup      * 1000,  T);
2882c6fd2807SJeff Garzik 	q->act8b	= EZ(t->act8b      * 1000,  T);
2883c6fd2807SJeff Garzik 	q->rec8b	= EZ(t->rec8b      * 1000,  T);
2884c6fd2807SJeff Garzik 	q->cyc8b	= EZ(t->cyc8b      * 1000,  T);
2885c6fd2807SJeff Garzik 	q->active	= EZ(t->active     * 1000,  T);
2886c6fd2807SJeff Garzik 	q->recover	= EZ(t->recover    * 1000,  T);
28873ada9c12SDavid Daney 	q->dmack_hold	= EZ(t->dmack_hold * 1000,  T);
2888c6fd2807SJeff Garzik 	q->cycle	= EZ(t->cycle      * 1000,  T);
2889c6fd2807SJeff Garzik 	q->udma		= EZ(t->udma       * 1000, UT);
2890c6fd2807SJeff Garzik }
2891c6fd2807SJeff Garzik 
2892c6fd2807SJeff Garzik void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
2893c6fd2807SJeff Garzik 		      struct ata_timing *m, unsigned int what)
2894c6fd2807SJeff Garzik {
2895c6fd2807SJeff Garzik 	if (what & ATA_TIMING_SETUP  ) m->setup   = max(a->setup,   b->setup);
2896c6fd2807SJeff Garzik 	if (what & ATA_TIMING_ACT8B  ) m->act8b   = max(a->act8b,   b->act8b);
2897c6fd2807SJeff Garzik 	if (what & ATA_TIMING_REC8B  ) m->rec8b   = max(a->rec8b,   b->rec8b);
2898c6fd2807SJeff Garzik 	if (what & ATA_TIMING_CYC8B  ) m->cyc8b   = max(a->cyc8b,   b->cyc8b);
2899c6fd2807SJeff Garzik 	if (what & ATA_TIMING_ACTIVE ) m->active  = max(a->active,  b->active);
2900c6fd2807SJeff Garzik 	if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
29013ada9c12SDavid Daney 	if (what & ATA_TIMING_DMACK_HOLD) m->dmack_hold = max(a->dmack_hold, b->dmack_hold);
2902c6fd2807SJeff Garzik 	if (what & ATA_TIMING_CYCLE  ) m->cycle   = max(a->cycle,   b->cycle);
2903c6fd2807SJeff Garzik 	if (what & ATA_TIMING_UDMA   ) m->udma    = max(a->udma,    b->udma);
2904c6fd2807SJeff Garzik }
2905c6fd2807SJeff Garzik 
29066357357cSTejun Heo const struct ata_timing *ata_timing_find_mode(u8 xfer_mode)
2907c6fd2807SJeff Garzik {
290870cd071eSTejun Heo 	const struct ata_timing *t = ata_timing;
2909c6fd2807SJeff Garzik 
291070cd071eSTejun Heo 	while (xfer_mode > t->mode)
291170cd071eSTejun Heo 		t++;
291270cd071eSTejun Heo 
291370cd071eSTejun Heo 	if (xfer_mode == t->mode)
2914c6fd2807SJeff Garzik 		return t;
291570cd071eSTejun Heo 	return NULL;
2916c6fd2807SJeff Garzik }
2917c6fd2807SJeff Garzik 
2918c6fd2807SJeff Garzik int ata_timing_compute(struct ata_device *adev, unsigned short speed,
2919c6fd2807SJeff Garzik 		       struct ata_timing *t, int T, int UT)
2920c6fd2807SJeff Garzik {
29219e8808a9SBartlomiej Zolnierkiewicz 	const u16 *id = adev->id;
2922c6fd2807SJeff Garzik 	const struct ata_timing *s;
2923c6fd2807SJeff Garzik 	struct ata_timing p;
2924c6fd2807SJeff Garzik 
2925c6fd2807SJeff Garzik 	/*
2926c6fd2807SJeff Garzik 	 * Find the mode.
2927c6fd2807SJeff Garzik 	 */
2928c6fd2807SJeff Garzik 
2929c6fd2807SJeff Garzik 	if (!(s = ata_timing_find_mode(speed)))
2930c6fd2807SJeff Garzik 		return -EINVAL;
2931c6fd2807SJeff Garzik 
2932c6fd2807SJeff Garzik 	memcpy(t, s, sizeof(*s));
2933c6fd2807SJeff Garzik 
2934c6fd2807SJeff Garzik 	/*
2935c6fd2807SJeff Garzik 	 * If the drive is an EIDE drive, it can tell us it needs extended
2936c6fd2807SJeff Garzik 	 * PIO/MW_DMA cycle timing.
2937c6fd2807SJeff Garzik 	 */
2938c6fd2807SJeff Garzik 
29399e8808a9SBartlomiej Zolnierkiewicz 	if (id[ATA_ID_FIELD_VALID] & 2) {	/* EIDE drive */
2940c6fd2807SJeff Garzik 		memset(&p, 0, sizeof(p));
29419e8808a9SBartlomiej Zolnierkiewicz 
2942bff00256SBartlomiej Zolnierkiewicz 		if (speed >= XFER_PIO_0 && speed < XFER_SW_DMA_0) {
29439e8808a9SBartlomiej Zolnierkiewicz 			if (speed <= XFER_PIO_2)
29449e8808a9SBartlomiej Zolnierkiewicz 				p.cycle = p.cyc8b = id[ATA_ID_EIDE_PIO];
29459e8808a9SBartlomiej Zolnierkiewicz 			else if ((speed <= XFER_PIO_4) ||
29469e8808a9SBartlomiej Zolnierkiewicz 				 (speed == XFER_PIO_5 && !ata_id_is_cfa(id)))
29479e8808a9SBartlomiej Zolnierkiewicz 				p.cycle = p.cyc8b = id[ATA_ID_EIDE_PIO_IORDY];
29489e8808a9SBartlomiej Zolnierkiewicz 		} else if (speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2)
29499e8808a9SBartlomiej Zolnierkiewicz 			p.cycle = id[ATA_ID_EIDE_DMA_MIN];
29509e8808a9SBartlomiej Zolnierkiewicz 
2951c6fd2807SJeff Garzik 		ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
2952c6fd2807SJeff Garzik 	}
2953c6fd2807SJeff Garzik 
2954c6fd2807SJeff Garzik 	/*
2955c6fd2807SJeff Garzik 	 * Convert the timing to bus clock counts.
2956c6fd2807SJeff Garzik 	 */
2957c6fd2807SJeff Garzik 
2958c6fd2807SJeff Garzik 	ata_timing_quantize(t, t, T, UT);
2959c6fd2807SJeff Garzik 
2960c6fd2807SJeff Garzik 	/*
2961c6fd2807SJeff Garzik 	 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
2962c6fd2807SJeff Garzik 	 * S.M.A.R.T * and some other commands. We have to ensure that the
2963c6fd2807SJeff Garzik 	 * DMA cycle timing is slower/equal than the fastest PIO timing.
2964c6fd2807SJeff Garzik 	 */
2965c6fd2807SJeff Garzik 
2966fd3367afSAlan 	if (speed > XFER_PIO_6) {
2967c6fd2807SJeff Garzik 		ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
2968c6fd2807SJeff Garzik 		ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
2969c6fd2807SJeff Garzik 	}
2970c6fd2807SJeff Garzik 
2971c6fd2807SJeff Garzik 	/*
2972c6fd2807SJeff Garzik 	 * Lengthen active & recovery time so that cycle time is correct.
2973c6fd2807SJeff Garzik 	 */
2974c6fd2807SJeff Garzik 
2975c6fd2807SJeff Garzik 	if (t->act8b + t->rec8b < t->cyc8b) {
2976c6fd2807SJeff Garzik 		t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
2977c6fd2807SJeff Garzik 		t->rec8b = t->cyc8b - t->act8b;
2978c6fd2807SJeff Garzik 	}
2979c6fd2807SJeff Garzik 
2980c6fd2807SJeff Garzik 	if (t->active + t->recover < t->cycle) {
2981c6fd2807SJeff Garzik 		t->active += (t->cycle - (t->active + t->recover)) / 2;
2982c6fd2807SJeff Garzik 		t->recover = t->cycle - t->active;
2983c6fd2807SJeff Garzik 	}
29844f701d1eSAlan Cox 
29854f701d1eSAlan Cox 	/* In a few cases quantisation may produce enough errors to
29864f701d1eSAlan Cox 	   leave t->cycle too low for the sum of active and recovery
29874f701d1eSAlan Cox 	   if so we must correct this */
29884f701d1eSAlan Cox 	if (t->active + t->recover > t->cycle)
29894f701d1eSAlan Cox 		t->cycle = t->active + t->recover;
2990c6fd2807SJeff Garzik 
2991c6fd2807SJeff Garzik 	return 0;
2992c6fd2807SJeff Garzik }
2993c6fd2807SJeff Garzik 
2994c6fd2807SJeff Garzik /**
2995a0f79b92STejun Heo  *	ata_timing_cycle2mode - find xfer mode for the specified cycle duration
2996a0f79b92STejun Heo  *	@xfer_shift: ATA_SHIFT_* value for transfer type to examine.
2997a0f79b92STejun Heo  *	@cycle: cycle duration in ns
2998a0f79b92STejun Heo  *
2999a0f79b92STejun Heo  *	Return matching xfer mode for @cycle.  The returned mode is of
3000a0f79b92STejun Heo  *	the transfer type specified by @xfer_shift.  If @cycle is too
3001a0f79b92STejun Heo  *	slow for @xfer_shift, 0xff is returned.  If @cycle is faster
3002a0f79b92STejun Heo  *	than the fastest known mode, the fasted mode is returned.
3003a0f79b92STejun Heo  *
3004a0f79b92STejun Heo  *	LOCKING:
3005a0f79b92STejun Heo  *	None.
3006a0f79b92STejun Heo  *
3007a0f79b92STejun Heo  *	RETURNS:
3008a0f79b92STejun Heo  *	Matching xfer_mode, 0xff if no match found.
3009a0f79b92STejun Heo  */
3010a0f79b92STejun Heo u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
3011a0f79b92STejun Heo {
3012a0f79b92STejun Heo 	u8 base_mode = 0xff, last_mode = 0xff;
3013a0f79b92STejun Heo 	const struct ata_xfer_ent *ent;
3014a0f79b92STejun Heo 	const struct ata_timing *t;
3015a0f79b92STejun Heo 
3016a0f79b92STejun Heo 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
3017a0f79b92STejun Heo 		if (ent->shift == xfer_shift)
3018a0f79b92STejun Heo 			base_mode = ent->base;
3019a0f79b92STejun Heo 
3020a0f79b92STejun Heo 	for (t = ata_timing_find_mode(base_mode);
3021a0f79b92STejun Heo 	     t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
3022a0f79b92STejun Heo 		unsigned short this_cycle;
3023a0f79b92STejun Heo 
3024a0f79b92STejun Heo 		switch (xfer_shift) {
3025a0f79b92STejun Heo 		case ATA_SHIFT_PIO:
3026a0f79b92STejun Heo 		case ATA_SHIFT_MWDMA:
3027a0f79b92STejun Heo 			this_cycle = t->cycle;
3028a0f79b92STejun Heo 			break;
3029a0f79b92STejun Heo 		case ATA_SHIFT_UDMA:
3030a0f79b92STejun Heo 			this_cycle = t->udma;
3031a0f79b92STejun Heo 			break;
3032a0f79b92STejun Heo 		default:
3033a0f79b92STejun Heo 			return 0xff;
3034a0f79b92STejun Heo 		}
3035a0f79b92STejun Heo 
3036a0f79b92STejun Heo 		if (cycle > this_cycle)
3037a0f79b92STejun Heo 			break;
3038a0f79b92STejun Heo 
3039a0f79b92STejun Heo 		last_mode = t->mode;
3040a0f79b92STejun Heo 	}
3041a0f79b92STejun Heo 
3042a0f79b92STejun Heo 	return last_mode;
3043a0f79b92STejun Heo }
3044a0f79b92STejun Heo 
3045a0f79b92STejun Heo /**
3046c6fd2807SJeff Garzik  *	ata_down_xfermask_limit - adjust dev xfer masks downward
3047c6fd2807SJeff Garzik  *	@dev: Device to adjust xfer masks
3048458337dbSTejun Heo  *	@sel: ATA_DNXFER_* selector
3049c6fd2807SJeff Garzik  *
3050c6fd2807SJeff Garzik  *	Adjust xfer masks of @dev downward.  Note that this function
3051c6fd2807SJeff Garzik  *	does not apply the change.  Invoking ata_set_mode() afterwards
3052c6fd2807SJeff Garzik  *	will apply the limit.
3053c6fd2807SJeff Garzik  *
3054c6fd2807SJeff Garzik  *	LOCKING:
3055c6fd2807SJeff Garzik  *	Inherited from caller.
3056c6fd2807SJeff Garzik  *
3057c6fd2807SJeff Garzik  *	RETURNS:
3058c6fd2807SJeff Garzik  *	0 on success, negative errno on failure
3059c6fd2807SJeff Garzik  */
3060458337dbSTejun Heo int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
3061c6fd2807SJeff Garzik {
3062458337dbSTejun Heo 	char buf[32];
30637dc951aeSTejun Heo 	unsigned long orig_mask, xfer_mask;
30647dc951aeSTejun Heo 	unsigned long pio_mask, mwdma_mask, udma_mask;
3065458337dbSTejun Heo 	int quiet, highbit;
3066c6fd2807SJeff Garzik 
3067458337dbSTejun Heo 	quiet = !!(sel & ATA_DNXFER_QUIET);
3068458337dbSTejun Heo 	sel &= ~ATA_DNXFER_QUIET;
3069458337dbSTejun Heo 
3070458337dbSTejun Heo 	xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
3071458337dbSTejun Heo 						  dev->mwdma_mask,
3072c6fd2807SJeff Garzik 						  dev->udma_mask);
3073458337dbSTejun Heo 	ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
3074c6fd2807SJeff Garzik 
3075458337dbSTejun Heo 	switch (sel) {
3076458337dbSTejun Heo 	case ATA_DNXFER_PIO:
3077458337dbSTejun Heo 		highbit = fls(pio_mask) - 1;
3078458337dbSTejun Heo 		pio_mask &= ~(1 << highbit);
3079458337dbSTejun Heo 		break;
3080458337dbSTejun Heo 
3081458337dbSTejun Heo 	case ATA_DNXFER_DMA:
3082458337dbSTejun Heo 		if (udma_mask) {
3083458337dbSTejun Heo 			highbit = fls(udma_mask) - 1;
3084458337dbSTejun Heo 			udma_mask &= ~(1 << highbit);
3085458337dbSTejun Heo 			if (!udma_mask)
3086458337dbSTejun Heo 				return -ENOENT;
3087458337dbSTejun Heo 		} else if (mwdma_mask) {
3088458337dbSTejun Heo 			highbit = fls(mwdma_mask) - 1;
3089458337dbSTejun Heo 			mwdma_mask &= ~(1 << highbit);
3090458337dbSTejun Heo 			if (!mwdma_mask)
3091458337dbSTejun Heo 				return -ENOENT;
3092458337dbSTejun Heo 		}
3093458337dbSTejun Heo 		break;
3094458337dbSTejun Heo 
3095458337dbSTejun Heo 	case ATA_DNXFER_40C:
3096458337dbSTejun Heo 		udma_mask &= ATA_UDMA_MASK_40C;
3097458337dbSTejun Heo 		break;
3098458337dbSTejun Heo 
3099458337dbSTejun Heo 	case ATA_DNXFER_FORCE_PIO0:
3100458337dbSTejun Heo 		pio_mask &= 1;
3101458337dbSTejun Heo 	case ATA_DNXFER_FORCE_PIO:
3102458337dbSTejun Heo 		mwdma_mask = 0;
3103458337dbSTejun Heo 		udma_mask = 0;
3104458337dbSTejun Heo 		break;
3105458337dbSTejun Heo 
3106458337dbSTejun Heo 	default:
3107458337dbSTejun Heo 		BUG();
3108458337dbSTejun Heo 	}
3109458337dbSTejun Heo 
3110458337dbSTejun Heo 	xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
3111458337dbSTejun Heo 
3112458337dbSTejun Heo 	if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
3113458337dbSTejun Heo 		return -ENOENT;
3114458337dbSTejun Heo 
3115458337dbSTejun Heo 	if (!quiet) {
3116458337dbSTejun Heo 		if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
3117458337dbSTejun Heo 			snprintf(buf, sizeof(buf), "%s:%s",
3118458337dbSTejun Heo 				 ata_mode_string(xfer_mask),
3119458337dbSTejun Heo 				 ata_mode_string(xfer_mask & ATA_MASK_PIO));
3120458337dbSTejun Heo 		else
3121458337dbSTejun Heo 			snprintf(buf, sizeof(buf), "%s",
3122458337dbSTejun Heo 				 ata_mode_string(xfer_mask));
3123458337dbSTejun Heo 
3124a9a79dfeSJoe Perches 		ata_dev_warn(dev, "limiting speed to %s\n", buf);
3125458337dbSTejun Heo 	}
3126c6fd2807SJeff Garzik 
3127c6fd2807SJeff Garzik 	ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
3128c6fd2807SJeff Garzik 			    &dev->udma_mask);
3129c6fd2807SJeff Garzik 
3130c6fd2807SJeff Garzik 	return 0;
3131c6fd2807SJeff Garzik }
3132c6fd2807SJeff Garzik 
3133c6fd2807SJeff Garzik static int ata_dev_set_mode(struct ata_device *dev)
3134c6fd2807SJeff Garzik {
3135d0cb43b3STejun Heo 	struct ata_port *ap = dev->link->ap;
31369af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
3137d0cb43b3STejun Heo 	const bool nosetxfer = dev->horkage & ATA_HORKAGE_NOSETXFER;
31384055dee7STejun Heo 	const char *dev_err_whine = "";
31394055dee7STejun Heo 	int ign_dev_err = 0;
3140d0cb43b3STejun Heo 	unsigned int err_mask = 0;
3141c6fd2807SJeff Garzik 	int rc;
3142c6fd2807SJeff Garzik 
3143c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_PIO;
3144c6fd2807SJeff Garzik 	if (dev->xfer_shift == ATA_SHIFT_PIO)
3145c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_PIO;
3146c6fd2807SJeff Garzik 
3147d0cb43b3STejun Heo 	if (nosetxfer && ap->flags & ATA_FLAG_SATA && ata_id_is_sata(dev->id))
3148d0cb43b3STejun Heo 		dev_err_whine = " (SET_XFERMODE skipped)";
3149d0cb43b3STejun Heo 	else {
3150d0cb43b3STejun Heo 		if (nosetxfer)
3151a9a79dfeSJoe Perches 			ata_dev_warn(dev,
3152d0cb43b3STejun Heo 				     "NOSETXFER but PATA detected - can't "
3153d0cb43b3STejun Heo 				     "skip SETXFER, might malfunction\n");
3154c6fd2807SJeff Garzik 		err_mask = ata_dev_set_xfermode(dev);
3155d0cb43b3STejun Heo 	}
31562dcb407eSJeff Garzik 
31574055dee7STejun Heo 	if (err_mask & ~AC_ERR_DEV)
31584055dee7STejun Heo 		goto fail;
31592dcb407eSJeff Garzik 
31604055dee7STejun Heo 	/* revalidate */
3161baa1e78aSTejun Heo 	ehc->i.flags |= ATA_EHI_POST_SETMODE;
3162422c9daaSTejun Heo 	rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
3163baa1e78aSTejun Heo 	ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
3164c6fd2807SJeff Garzik 	if (rc)
3165c6fd2807SJeff Garzik 		return rc;
3166c6fd2807SJeff Garzik 
3167b93fda12SAlan Cox 	if (dev->xfer_shift == ATA_SHIFT_PIO) {
31684055dee7STejun Heo 		/* Old CFA may refuse this command, which is just fine */
3169b93fda12SAlan Cox 		if (ata_id_is_cfa(dev->id))
31704055dee7STejun Heo 			ign_dev_err = 1;
3171b93fda12SAlan Cox 		/* Catch several broken garbage emulations plus some pre
3172b93fda12SAlan Cox 		   ATA devices */
3173b93fda12SAlan Cox 		if (ata_id_major_version(dev->id) == 0 &&
31744055dee7STejun Heo 					dev->pio_mode <= XFER_PIO_2)
31754055dee7STejun Heo 			ign_dev_err = 1;
3176b93fda12SAlan Cox 		/* Some very old devices and some bad newer ones fail
3177b93fda12SAlan Cox 		   any kind of SET_XFERMODE request but support PIO0-2
3178b93fda12SAlan Cox 		   timings and no IORDY */
3179b93fda12SAlan Cox 		if (!ata_id_has_iordy(dev->id) && dev->pio_mode <= XFER_PIO_2)
3180b93fda12SAlan Cox 			ign_dev_err = 1;
3181b93fda12SAlan Cox 	}
31824055dee7STejun Heo 	/* Early MWDMA devices do DMA but don't allow DMA mode setting.
31834055dee7STejun Heo 	   Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
31844055dee7STejun Heo 	if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
31854055dee7STejun Heo 	    dev->dma_mode == XFER_MW_DMA_0 &&
31864055dee7STejun Heo 	    (dev->id[63] >> 8) & 1)
31874055dee7STejun Heo 		ign_dev_err = 1;
31884055dee7STejun Heo 
31894055dee7STejun Heo 	/* if the device is actually configured correctly, ignore dev err */
31904055dee7STejun Heo 	if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id)))
31914055dee7STejun Heo 		ign_dev_err = 1;
31924055dee7STejun Heo 
31934055dee7STejun Heo 	if (err_mask & AC_ERR_DEV) {
31944055dee7STejun Heo 		if (!ign_dev_err)
31954055dee7STejun Heo 			goto fail;
31964055dee7STejun Heo 		else
31974055dee7STejun Heo 			dev_err_whine = " (device error ignored)";
31984055dee7STejun Heo 	}
31994055dee7STejun Heo 
3200c6fd2807SJeff Garzik 	DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
3201c6fd2807SJeff Garzik 		dev->xfer_shift, (int)dev->xfer_mode);
3202c6fd2807SJeff Garzik 
3203a9a79dfeSJoe Perches 	ata_dev_info(dev, "configured for %s%s\n",
32044055dee7STejun Heo 		     ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)),
32054055dee7STejun Heo 		     dev_err_whine);
32064055dee7STejun Heo 
3207c6fd2807SJeff Garzik 	return 0;
32084055dee7STejun Heo 
32094055dee7STejun Heo  fail:
3210a9a79dfeSJoe Perches 	ata_dev_err(dev, "failed to set xfermode (err_mask=0x%x)\n", err_mask);
32114055dee7STejun Heo 	return -EIO;
3212c6fd2807SJeff Garzik }
3213c6fd2807SJeff Garzik 
3214c6fd2807SJeff Garzik /**
321504351821SAlan  *	ata_do_set_mode - Program timings and issue SET FEATURES - XFER
32160260731fSTejun Heo  *	@link: link on which timings will be programmed
32171967b7ffSJoe Perches  *	@r_failed_dev: out parameter for failed device
3218c6fd2807SJeff Garzik  *
321904351821SAlan  *	Standard implementation of the function used to tune and set
322004351821SAlan  *	ATA device disk transfer mode (PIO3, UDMA6, etc.).  If
322104351821SAlan  *	ata_dev_set_mode() fails, pointer to the failing device is
3222c6fd2807SJeff Garzik  *	returned in @r_failed_dev.
3223c6fd2807SJeff Garzik  *
3224c6fd2807SJeff Garzik  *	LOCKING:
3225c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
3226c6fd2807SJeff Garzik  *
3227c6fd2807SJeff Garzik  *	RETURNS:
3228c6fd2807SJeff Garzik  *	0 on success, negative errno otherwise
3229c6fd2807SJeff Garzik  */
323004351821SAlan 
32310260731fSTejun Heo int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
3232c6fd2807SJeff Garzik {
32330260731fSTejun Heo 	struct ata_port *ap = link->ap;
3234c6fd2807SJeff Garzik 	struct ata_device *dev;
3235f58229f8STejun Heo 	int rc = 0, used_dma = 0, found = 0;
3236c6fd2807SJeff Garzik 
3237c6fd2807SJeff Garzik 	/* step 1: calculate xfer_mask */
32381eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
32397dc951aeSTejun Heo 		unsigned long pio_mask, dma_mask;
3240b3a70601SAlan Cox 		unsigned int mode_mask;
3241c6fd2807SJeff Garzik 
3242b3a70601SAlan Cox 		mode_mask = ATA_DMA_MASK_ATA;
3243b3a70601SAlan Cox 		if (dev->class == ATA_DEV_ATAPI)
3244b3a70601SAlan Cox 			mode_mask = ATA_DMA_MASK_ATAPI;
3245b3a70601SAlan Cox 		else if (ata_id_is_cfa(dev->id))
3246b3a70601SAlan Cox 			mode_mask = ATA_DMA_MASK_CFA;
3247b3a70601SAlan Cox 
3248c6fd2807SJeff Garzik 		ata_dev_xfermask(dev);
324933267325STejun Heo 		ata_force_xfermask(dev);
3250c6fd2807SJeff Garzik 
3251c6fd2807SJeff Garzik 		pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
3252b3a70601SAlan Cox 
3253b3a70601SAlan Cox 		if (libata_dma_mask & mode_mask)
325480a9c430SSergei Shtylyov 			dma_mask = ata_pack_xfermask(0, dev->mwdma_mask,
325580a9c430SSergei Shtylyov 						     dev->udma_mask);
3256b3a70601SAlan Cox 		else
3257b3a70601SAlan Cox 			dma_mask = 0;
3258b3a70601SAlan Cox 
3259c6fd2807SJeff Garzik 		dev->pio_mode = ata_xfer_mask2mode(pio_mask);
3260c6fd2807SJeff Garzik 		dev->dma_mode = ata_xfer_mask2mode(dma_mask);
3261c6fd2807SJeff Garzik 
3262c6fd2807SJeff Garzik 		found = 1;
3263b15b3ebaSAlan Cox 		if (ata_dma_enabled(dev))
3264c6fd2807SJeff Garzik 			used_dma = 1;
3265c6fd2807SJeff Garzik 	}
3266c6fd2807SJeff Garzik 	if (!found)
3267c6fd2807SJeff Garzik 		goto out;
3268c6fd2807SJeff Garzik 
3269c6fd2807SJeff Garzik 	/* step 2: always set host PIO timings */
32701eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
327170cd071eSTejun Heo 		if (dev->pio_mode == 0xff) {
3272a9a79dfeSJoe Perches 			ata_dev_warn(dev, "no PIO support\n");
3273c6fd2807SJeff Garzik 			rc = -EINVAL;
3274c6fd2807SJeff Garzik 			goto out;
3275c6fd2807SJeff Garzik 		}
3276c6fd2807SJeff Garzik 
3277c6fd2807SJeff Garzik 		dev->xfer_mode = dev->pio_mode;
3278c6fd2807SJeff Garzik 		dev->xfer_shift = ATA_SHIFT_PIO;
3279c6fd2807SJeff Garzik 		if (ap->ops->set_piomode)
3280c6fd2807SJeff Garzik 			ap->ops->set_piomode(ap, dev);
3281c6fd2807SJeff Garzik 	}
3282c6fd2807SJeff Garzik 
3283c6fd2807SJeff Garzik 	/* step 3: set host DMA timings */
32841eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
32851eca4365STejun Heo 		if (!ata_dma_enabled(dev))
3286c6fd2807SJeff Garzik 			continue;
3287c6fd2807SJeff Garzik 
3288c6fd2807SJeff Garzik 		dev->xfer_mode = dev->dma_mode;
3289c6fd2807SJeff Garzik 		dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
3290c6fd2807SJeff Garzik 		if (ap->ops->set_dmamode)
3291c6fd2807SJeff Garzik 			ap->ops->set_dmamode(ap, dev);
3292c6fd2807SJeff Garzik 	}
3293c6fd2807SJeff Garzik 
3294c6fd2807SJeff Garzik 	/* step 4: update devices' xfer mode */
32951eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
3296c6fd2807SJeff Garzik 		rc = ata_dev_set_mode(dev);
3297c6fd2807SJeff Garzik 		if (rc)
3298c6fd2807SJeff Garzik 			goto out;
3299c6fd2807SJeff Garzik 	}
3300c6fd2807SJeff Garzik 
3301c6fd2807SJeff Garzik 	/* Record simplex status. If we selected DMA then the other
3302c6fd2807SJeff Garzik 	 * host channels are not permitted to do so.
3303c6fd2807SJeff Garzik 	 */
3304cca3974eSJeff Garzik 	if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
3305032af1ceSAlan 		ap->host->simplex_claimed = ap;
3306c6fd2807SJeff Garzik 
3307c6fd2807SJeff Garzik  out:
3308c6fd2807SJeff Garzik 	if (rc)
3309c6fd2807SJeff Garzik 		*r_failed_dev = dev;
3310c6fd2807SJeff Garzik 	return rc;
3311c6fd2807SJeff Garzik }
3312c6fd2807SJeff Garzik 
3313c6fd2807SJeff Garzik /**
3314aa2731adSTejun Heo  *	ata_wait_ready - wait for link to become ready
3315aa2731adSTejun Heo  *	@link: link to be waited on
3316aa2731adSTejun Heo  *	@deadline: deadline jiffies for the operation
3317aa2731adSTejun Heo  *	@check_ready: callback to check link readiness
3318aa2731adSTejun Heo  *
3319aa2731adSTejun Heo  *	Wait for @link to become ready.  @check_ready should return
3320aa2731adSTejun Heo  *	positive number if @link is ready, 0 if it isn't, -ENODEV if
3321aa2731adSTejun Heo  *	link doesn't seem to be occupied, other errno for other error
3322aa2731adSTejun Heo  *	conditions.
3323aa2731adSTejun Heo  *
3324aa2731adSTejun Heo  *	Transient -ENODEV conditions are allowed for
3325aa2731adSTejun Heo  *	ATA_TMOUT_FF_WAIT.
3326aa2731adSTejun Heo  *
3327aa2731adSTejun Heo  *	LOCKING:
3328aa2731adSTejun Heo  *	EH context.
3329aa2731adSTejun Heo  *
3330aa2731adSTejun Heo  *	RETURNS:
3331aa2731adSTejun Heo  *	0 if @linke is ready before @deadline; otherwise, -errno.
3332aa2731adSTejun Heo  */
3333aa2731adSTejun Heo int ata_wait_ready(struct ata_link *link, unsigned long deadline,
3334aa2731adSTejun Heo 		   int (*check_ready)(struct ata_link *link))
3335aa2731adSTejun Heo {
3336aa2731adSTejun Heo 	unsigned long start = jiffies;
3337b48d58f5STejun Heo 	unsigned long nodev_deadline;
3338aa2731adSTejun Heo 	int warned = 0;
3339aa2731adSTejun Heo 
3340b48d58f5STejun Heo 	/* choose which 0xff timeout to use, read comment in libata.h */
3341b48d58f5STejun Heo 	if (link->ap->host->flags & ATA_HOST_PARALLEL_SCAN)
3342b48d58f5STejun Heo 		nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT_LONG);
3343b48d58f5STejun Heo 	else
3344b48d58f5STejun Heo 		nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT);
3345b48d58f5STejun Heo 
3346b1c72916STejun Heo 	/* Slave readiness can't be tested separately from master.  On
3347b1c72916STejun Heo 	 * M/S emulation configuration, this function should be called
3348b1c72916STejun Heo 	 * only on the master and it will handle both master and slave.
3349b1c72916STejun Heo 	 */
3350b1c72916STejun Heo 	WARN_ON(link == link->ap->slave_link);
3351b1c72916STejun Heo 
3352aa2731adSTejun Heo 	if (time_after(nodev_deadline, deadline))
3353aa2731adSTejun Heo 		nodev_deadline = deadline;
3354aa2731adSTejun Heo 
3355aa2731adSTejun Heo 	while (1) {
3356aa2731adSTejun Heo 		unsigned long now = jiffies;
3357aa2731adSTejun Heo 		int ready, tmp;
3358aa2731adSTejun Heo 
3359aa2731adSTejun Heo 		ready = tmp = check_ready(link);
3360aa2731adSTejun Heo 		if (ready > 0)
3361aa2731adSTejun Heo 			return 0;
3362aa2731adSTejun Heo 
3363b48d58f5STejun Heo 		/*
3364b48d58f5STejun Heo 		 * -ENODEV could be transient.  Ignore -ENODEV if link
3365aa2731adSTejun Heo 		 * is online.  Also, some SATA devices take a long
3366b48d58f5STejun Heo 		 * time to clear 0xff after reset.  Wait for
3367b48d58f5STejun Heo 		 * ATA_TMOUT_FF_WAIT[_LONG] on -ENODEV if link isn't
3368b48d58f5STejun Heo 		 * offline.
3369aa2731adSTejun Heo 		 *
3370aa2731adSTejun Heo 		 * Note that some PATA controllers (pata_ali) explode
3371aa2731adSTejun Heo 		 * if status register is read more than once when
3372aa2731adSTejun Heo 		 * there's no device attached.
3373aa2731adSTejun Heo 		 */
3374aa2731adSTejun Heo 		if (ready == -ENODEV) {
3375aa2731adSTejun Heo 			if (ata_link_online(link))
3376aa2731adSTejun Heo 				ready = 0;
3377aa2731adSTejun Heo 			else if ((link->ap->flags & ATA_FLAG_SATA) &&
3378aa2731adSTejun Heo 				 !ata_link_offline(link) &&
3379aa2731adSTejun Heo 				 time_before(now, nodev_deadline))
3380aa2731adSTejun Heo 				ready = 0;
3381aa2731adSTejun Heo 		}
3382aa2731adSTejun Heo 
3383aa2731adSTejun Heo 		if (ready)
3384aa2731adSTejun Heo 			return ready;
3385aa2731adSTejun Heo 		if (time_after(now, deadline))
3386aa2731adSTejun Heo 			return -EBUSY;
3387aa2731adSTejun Heo 
3388aa2731adSTejun Heo 		if (!warned && time_after(now, start + 5 * HZ) &&
3389aa2731adSTejun Heo 		    (deadline - now > 3 * HZ)) {
3390a9a79dfeSJoe Perches 			ata_link_warn(link,
3391aa2731adSTejun Heo 				"link is slow to respond, please be patient "
3392aa2731adSTejun Heo 				"(ready=%d)\n", tmp);
3393aa2731adSTejun Heo 			warned = 1;
3394aa2731adSTejun Heo 		}
3395aa2731adSTejun Heo 
339697750cebSTejun Heo 		ata_msleep(link->ap, 50);
3397aa2731adSTejun Heo 	}
3398aa2731adSTejun Heo }
3399aa2731adSTejun Heo 
3400aa2731adSTejun Heo /**
3401aa2731adSTejun Heo  *	ata_wait_after_reset - wait for link to become ready after reset
3402aa2731adSTejun Heo  *	@link: link to be waited on
3403aa2731adSTejun Heo  *	@deadline: deadline jiffies for the operation
3404aa2731adSTejun Heo  *	@check_ready: callback to check link readiness
3405aa2731adSTejun Heo  *
3406aa2731adSTejun Heo  *	Wait for @link to become ready after reset.
3407aa2731adSTejun Heo  *
3408aa2731adSTejun Heo  *	LOCKING:
3409aa2731adSTejun Heo  *	EH context.
3410aa2731adSTejun Heo  *
3411aa2731adSTejun Heo  *	RETURNS:
3412aa2731adSTejun Heo  *	0 if @linke is ready before @deadline; otherwise, -errno.
3413aa2731adSTejun Heo  */
34142b4221bbSHarvey Harrison int ata_wait_after_reset(struct ata_link *link, unsigned long deadline,
3415aa2731adSTejun Heo 				int (*check_ready)(struct ata_link *link))
3416aa2731adSTejun Heo {
341797750cebSTejun Heo 	ata_msleep(link->ap, ATA_WAIT_AFTER_RESET);
3418aa2731adSTejun Heo 
3419aa2731adSTejun Heo 	return ata_wait_ready(link, deadline, check_ready);
3420aa2731adSTejun Heo }
3421aa2731adSTejun Heo 
3422aa2731adSTejun Heo /**
3423936fd732STejun Heo  *	sata_link_debounce - debounce SATA phy status
3424936fd732STejun Heo  *	@link: ATA link to debounce SATA phy status for
3425c6fd2807SJeff Garzik  *	@params: timing parameters { interval, duratinon, timeout } in msec
3426d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
3427c6fd2807SJeff Garzik  *
3428936fd732STejun Heo  *	Make sure SStatus of @link reaches stable state, determined by
3429c6fd2807SJeff Garzik  *	holding the same value where DET is not 1 for @duration polled
3430c6fd2807SJeff Garzik  *	every @interval, before @timeout.  Timeout constraints the
3431d4b2bab4STejun Heo  *	beginning of the stable state.  Because DET gets stuck at 1 on
3432d4b2bab4STejun Heo  *	some controllers after hot unplugging, this functions waits
3433c6fd2807SJeff Garzik  *	until timeout then returns 0 if DET is stable at 1.
3434c6fd2807SJeff Garzik  *
3435d4b2bab4STejun Heo  *	@timeout is further limited by @deadline.  The sooner of the
3436d4b2bab4STejun Heo  *	two is used.
3437d4b2bab4STejun Heo  *
3438c6fd2807SJeff Garzik  *	LOCKING:
3439c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3440c6fd2807SJeff Garzik  *
3441c6fd2807SJeff Garzik  *	RETURNS:
3442c6fd2807SJeff Garzik  *	0 on success, -errno on failure.
3443c6fd2807SJeff Garzik  */
3444936fd732STejun Heo int sata_link_debounce(struct ata_link *link, const unsigned long *params,
3445d4b2bab4STejun Heo 		       unsigned long deadline)
3446c6fd2807SJeff Garzik {
3447341c2c95STejun Heo 	unsigned long interval = params[0];
3448341c2c95STejun Heo 	unsigned long duration = params[1];
3449d4b2bab4STejun Heo 	unsigned long last_jiffies, t;
3450c6fd2807SJeff Garzik 	u32 last, cur;
3451c6fd2807SJeff Garzik 	int rc;
3452c6fd2807SJeff Garzik 
3453341c2c95STejun Heo 	t = ata_deadline(jiffies, params[2]);
3454d4b2bab4STejun Heo 	if (time_before(t, deadline))
3455d4b2bab4STejun Heo 		deadline = t;
3456d4b2bab4STejun Heo 
3457936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3458c6fd2807SJeff Garzik 		return rc;
3459c6fd2807SJeff Garzik 	cur &= 0xf;
3460c6fd2807SJeff Garzik 
3461c6fd2807SJeff Garzik 	last = cur;
3462c6fd2807SJeff Garzik 	last_jiffies = jiffies;
3463c6fd2807SJeff Garzik 
3464c6fd2807SJeff Garzik 	while (1) {
346597750cebSTejun Heo 		ata_msleep(link->ap, interval);
3466936fd732STejun Heo 		if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3467c6fd2807SJeff Garzik 			return rc;
3468c6fd2807SJeff Garzik 		cur &= 0xf;
3469c6fd2807SJeff Garzik 
3470c6fd2807SJeff Garzik 		/* DET stable? */
3471c6fd2807SJeff Garzik 		if (cur == last) {
3472d4b2bab4STejun Heo 			if (cur == 1 && time_before(jiffies, deadline))
3473c6fd2807SJeff Garzik 				continue;
3474341c2c95STejun Heo 			if (time_after(jiffies,
3475341c2c95STejun Heo 				       ata_deadline(last_jiffies, duration)))
3476c6fd2807SJeff Garzik 				return 0;
3477c6fd2807SJeff Garzik 			continue;
3478c6fd2807SJeff Garzik 		}
3479c6fd2807SJeff Garzik 
3480c6fd2807SJeff Garzik 		/* unstable, start over */
3481c6fd2807SJeff Garzik 		last = cur;
3482c6fd2807SJeff Garzik 		last_jiffies = jiffies;
3483c6fd2807SJeff Garzik 
3484f1545154STejun Heo 		/* Check deadline.  If debouncing failed, return
3485f1545154STejun Heo 		 * -EPIPE to tell upper layer to lower link speed.
3486f1545154STejun Heo 		 */
3487d4b2bab4STejun Heo 		if (time_after(jiffies, deadline))
3488f1545154STejun Heo 			return -EPIPE;
3489c6fd2807SJeff Garzik 	}
3490c6fd2807SJeff Garzik }
3491c6fd2807SJeff Garzik 
3492c6fd2807SJeff Garzik /**
3493936fd732STejun Heo  *	sata_link_resume - resume SATA link
3494936fd732STejun Heo  *	@link: ATA link to resume SATA
3495c6fd2807SJeff Garzik  *	@params: timing parameters { interval, duratinon, timeout } in msec
3496d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
3497c6fd2807SJeff Garzik  *
3498936fd732STejun Heo  *	Resume SATA phy @link and debounce it.
3499c6fd2807SJeff Garzik  *
3500c6fd2807SJeff Garzik  *	LOCKING:
3501c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3502c6fd2807SJeff Garzik  *
3503c6fd2807SJeff Garzik  *	RETURNS:
3504c6fd2807SJeff Garzik  *	0 on success, -errno on failure.
3505c6fd2807SJeff Garzik  */
3506936fd732STejun Heo int sata_link_resume(struct ata_link *link, const unsigned long *params,
3507d4b2bab4STejun Heo 		     unsigned long deadline)
3508c6fd2807SJeff Garzik {
35095040ab67STejun Heo 	int tries = ATA_LINK_RESUME_TRIES;
3510ac371987STejun Heo 	u32 scontrol, serror;
3511c6fd2807SJeff Garzik 	int rc;
3512c6fd2807SJeff Garzik 
3513936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3514c6fd2807SJeff Garzik 		return rc;
3515c6fd2807SJeff Garzik 
35165040ab67STejun Heo 	/*
35175040ab67STejun Heo 	 * Writes to SControl sometimes get ignored under certain
35185040ab67STejun Heo 	 * controllers (ata_piix SIDPR).  Make sure DET actually is
35195040ab67STejun Heo 	 * cleared.
35205040ab67STejun Heo 	 */
35215040ab67STejun Heo 	do {
3522c6fd2807SJeff Garzik 		scontrol = (scontrol & 0x0f0) | 0x300;
3523936fd732STejun Heo 		if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3524c6fd2807SJeff Garzik 			return rc;
35255040ab67STejun Heo 		/*
35265040ab67STejun Heo 		 * Some PHYs react badly if SStatus is pounded
35275040ab67STejun Heo 		 * immediately after resuming.  Delay 200ms before
35285040ab67STejun Heo 		 * debouncing.
3529c6fd2807SJeff Garzik 		 */
353097750cebSTejun Heo 		ata_msleep(link->ap, 200);
3531c6fd2807SJeff Garzik 
35325040ab67STejun Heo 		/* is SControl restored correctly? */
35335040ab67STejun Heo 		if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
35345040ab67STejun Heo 			return rc;
35355040ab67STejun Heo 	} while ((scontrol & 0xf0f) != 0x300 && --tries);
35365040ab67STejun Heo 
35375040ab67STejun Heo 	if ((scontrol & 0xf0f) != 0x300) {
353838941c95STejun Heo 		ata_link_warn(link, "failed to resume link (SControl %X)\n",
35395040ab67STejun Heo 			     scontrol);
35405040ab67STejun Heo 		return 0;
35415040ab67STejun Heo 	}
35425040ab67STejun Heo 
35435040ab67STejun Heo 	if (tries < ATA_LINK_RESUME_TRIES)
3544a9a79dfeSJoe Perches 		ata_link_warn(link, "link resume succeeded after %d retries\n",
35455040ab67STejun Heo 			      ATA_LINK_RESUME_TRIES - tries);
35465040ab67STejun Heo 
3547ac371987STejun Heo 	if ((rc = sata_link_debounce(link, params, deadline)))
3548ac371987STejun Heo 		return rc;
3549ac371987STejun Heo 
3550f046519fSTejun Heo 	/* clear SError, some PHYs require this even for SRST to work */
3551ac371987STejun Heo 	if (!(rc = sata_scr_read(link, SCR_ERROR, &serror)))
3552ac371987STejun Heo 		rc = sata_scr_write(link, SCR_ERROR, serror);
3553ac371987STejun Heo 
3554f046519fSTejun Heo 	return rc != -EINVAL ? rc : 0;
3555c6fd2807SJeff Garzik }
3556c6fd2807SJeff Garzik 
3557c6fd2807SJeff Garzik /**
35581152b261STejun Heo  *	sata_link_scr_lpm - manipulate SControl IPM and SPM fields
35591152b261STejun Heo  *	@link: ATA link to manipulate SControl for
35601152b261STejun Heo  *	@policy: LPM policy to configure
35611152b261STejun Heo  *	@spm_wakeup: initiate LPM transition to active state
35621152b261STejun Heo  *
35631152b261STejun Heo  *	Manipulate the IPM field of the SControl register of @link
35641152b261STejun Heo  *	according to @policy.  If @policy is ATA_LPM_MAX_POWER and
35651152b261STejun Heo  *	@spm_wakeup is %true, the SPM field is manipulated to wake up
35661152b261STejun Heo  *	the link.  This function also clears PHYRDY_CHG before
35671152b261STejun Heo  *	returning.
35681152b261STejun Heo  *
35691152b261STejun Heo  *	LOCKING:
35701152b261STejun Heo  *	EH context.
35711152b261STejun Heo  *
35721152b261STejun Heo  *	RETURNS:
35731152b261STejun Heo  *	0 on succes, -errno otherwise.
35741152b261STejun Heo  */
35751152b261STejun Heo int sata_link_scr_lpm(struct ata_link *link, enum ata_lpm_policy policy,
35761152b261STejun Heo 		      bool spm_wakeup)
35771152b261STejun Heo {
35781152b261STejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
35791152b261STejun Heo 	bool woken_up = false;
35801152b261STejun Heo 	u32 scontrol;
35811152b261STejun Heo 	int rc;
35821152b261STejun Heo 
35831152b261STejun Heo 	rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
35841152b261STejun Heo 	if (rc)
35851152b261STejun Heo 		return rc;
35861152b261STejun Heo 
35871152b261STejun Heo 	switch (policy) {
35881152b261STejun Heo 	case ATA_LPM_MAX_POWER:
35891152b261STejun Heo 		/* disable all LPM transitions */
35901152b261STejun Heo 		scontrol |= (0x3 << 8);
35911152b261STejun Heo 		/* initiate transition to active state */
35921152b261STejun Heo 		if (spm_wakeup) {
35931152b261STejun Heo 			scontrol |= (0x4 << 12);
35941152b261STejun Heo 			woken_up = true;
35951152b261STejun Heo 		}
35961152b261STejun Heo 		break;
35971152b261STejun Heo 	case ATA_LPM_MED_POWER:
35981152b261STejun Heo 		/* allow LPM to PARTIAL */
35991152b261STejun Heo 		scontrol &= ~(0x1 << 8);
36001152b261STejun Heo 		scontrol |= (0x2 << 8);
36011152b261STejun Heo 		break;
36021152b261STejun Heo 	case ATA_LPM_MIN_POWER:
36038a745f1fSKristen Carlson Accardi 		if (ata_link_nr_enabled(link) > 0)
36041152b261STejun Heo 			/* no restrictions on LPM transitions */
36051152b261STejun Heo 			scontrol &= ~(0x3 << 8);
36068a745f1fSKristen Carlson Accardi 		else {
36078a745f1fSKristen Carlson Accardi 			/* empty port, power off */
36088a745f1fSKristen Carlson Accardi 			scontrol &= ~0xf;
36098a745f1fSKristen Carlson Accardi 			scontrol |= (0x1 << 2);
36108a745f1fSKristen Carlson Accardi 		}
36111152b261STejun Heo 		break;
36121152b261STejun Heo 	default:
36131152b261STejun Heo 		WARN_ON(1);
36141152b261STejun Heo 	}
36151152b261STejun Heo 
36161152b261STejun Heo 	rc = sata_scr_write(link, SCR_CONTROL, scontrol);
36171152b261STejun Heo 	if (rc)
36181152b261STejun Heo 		return rc;
36191152b261STejun Heo 
36201152b261STejun Heo 	/* give the link time to transit out of LPM state */
36211152b261STejun Heo 	if (woken_up)
36221152b261STejun Heo 		msleep(10);
36231152b261STejun Heo 
36241152b261STejun Heo 	/* clear PHYRDY_CHG from SError */
36251152b261STejun Heo 	ehc->i.serror &= ~SERR_PHYRDY_CHG;
36261152b261STejun Heo 	return sata_scr_write(link, SCR_ERROR, SERR_PHYRDY_CHG);
36271152b261STejun Heo }
36281152b261STejun Heo 
36291152b261STejun Heo /**
36300aa1113dSTejun Heo  *	ata_std_prereset - prepare for reset
3631cc0680a5STejun Heo  *	@link: ATA link to be reset
3632d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
3633c6fd2807SJeff Garzik  *
3634cc0680a5STejun Heo  *	@link is about to be reset.  Initialize it.  Failure from
3635b8cffc6aSTejun Heo  *	prereset makes libata abort whole reset sequence and give up
3636b8cffc6aSTejun Heo  *	that port, so prereset should be best-effort.  It does its
3637b8cffc6aSTejun Heo  *	best to prepare for reset sequence but if things go wrong, it
3638b8cffc6aSTejun Heo  *	should just whine, not fail.
3639c6fd2807SJeff Garzik  *
3640c6fd2807SJeff Garzik  *	LOCKING:
3641c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3642c6fd2807SJeff Garzik  *
3643c6fd2807SJeff Garzik  *	RETURNS:
3644c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
3645c6fd2807SJeff Garzik  */
36460aa1113dSTejun Heo int ata_std_prereset(struct ata_link *link, unsigned long deadline)
3647c6fd2807SJeff Garzik {
3648cc0680a5STejun Heo 	struct ata_port *ap = link->ap;
3649936fd732STejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
3650c6fd2807SJeff Garzik 	const unsigned long *timing = sata_ehc_deb_timing(ehc);
3651c6fd2807SJeff Garzik 	int rc;
3652c6fd2807SJeff Garzik 
3653c6fd2807SJeff Garzik 	/* if we're about to do hardreset, nothing more to do */
3654c6fd2807SJeff Garzik 	if (ehc->i.action & ATA_EH_HARDRESET)
3655c6fd2807SJeff Garzik 		return 0;
3656c6fd2807SJeff Garzik 
3657936fd732STejun Heo 	/* if SATA, resume link */
3658a16abc0bSTejun Heo 	if (ap->flags & ATA_FLAG_SATA) {
3659936fd732STejun Heo 		rc = sata_link_resume(link, timing, deadline);
3660b8cffc6aSTejun Heo 		/* whine about phy resume failure but proceed */
3661b8cffc6aSTejun Heo 		if (rc && rc != -EOPNOTSUPP)
3662a9a79dfeSJoe Perches 			ata_link_warn(link,
3663a9a79dfeSJoe Perches 				      "failed to resume link for reset (errno=%d)\n",
3664a9a79dfeSJoe Perches 				      rc);
3665c6fd2807SJeff Garzik 	}
3666c6fd2807SJeff Garzik 
366745db2f6cSTejun Heo 	/* no point in trying softreset on offline link */
3668b1c72916STejun Heo 	if (ata_phys_link_offline(link))
366945db2f6cSTejun Heo 		ehc->i.action &= ~ATA_EH_SOFTRESET;
367045db2f6cSTejun Heo 
3671c6fd2807SJeff Garzik 	return 0;
3672c6fd2807SJeff Garzik }
3673c6fd2807SJeff Garzik 
3674c6fd2807SJeff Garzik /**
3675cc0680a5STejun Heo  *	sata_link_hardreset - reset link via SATA phy reset
3676cc0680a5STejun Heo  *	@link: link to reset
3677b6103f6dSTejun Heo  *	@timing: timing parameters { interval, duratinon, timeout } in msec
3678d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
36799dadd45bSTejun Heo  *	@online: optional out parameter indicating link onlineness
36809dadd45bSTejun Heo  *	@check_ready: optional callback to check link readiness
3681c6fd2807SJeff Garzik  *
3682cc0680a5STejun Heo  *	SATA phy-reset @link using DET bits of SControl register.
36839dadd45bSTejun Heo  *	After hardreset, link readiness is waited upon using
36849dadd45bSTejun Heo  *	ata_wait_ready() if @check_ready is specified.  LLDs are
36859dadd45bSTejun Heo  *	allowed to not specify @check_ready and wait itself after this
36869dadd45bSTejun Heo  *	function returns.  Device classification is LLD's
36879dadd45bSTejun Heo  *	responsibility.
36889dadd45bSTejun Heo  *
36899dadd45bSTejun Heo  *	*@online is set to one iff reset succeeded and @link is online
36909dadd45bSTejun Heo  *	after reset.
3691c6fd2807SJeff Garzik  *
3692c6fd2807SJeff Garzik  *	LOCKING:
3693c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3694c6fd2807SJeff Garzik  *
3695c6fd2807SJeff Garzik  *	RETURNS:
3696c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
3697c6fd2807SJeff Garzik  */
3698cc0680a5STejun Heo int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
36999dadd45bSTejun Heo 			unsigned long deadline,
37009dadd45bSTejun Heo 			bool *online, int (*check_ready)(struct ata_link *))
3701c6fd2807SJeff Garzik {
3702c6fd2807SJeff Garzik 	u32 scontrol;
3703c6fd2807SJeff Garzik 	int rc;
3704c6fd2807SJeff Garzik 
3705c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
3706c6fd2807SJeff Garzik 
37079dadd45bSTejun Heo 	if (online)
37089dadd45bSTejun Heo 		*online = false;
37099dadd45bSTejun Heo 
3710936fd732STejun Heo 	if (sata_set_spd_needed(link)) {
3711c6fd2807SJeff Garzik 		/* SATA spec says nothing about how to reconfigure
3712c6fd2807SJeff Garzik 		 * spd.  To be on the safe side, turn off phy during
3713c6fd2807SJeff Garzik 		 * reconfiguration.  This works for at least ICH7 AHCI
3714c6fd2807SJeff Garzik 		 * and Sil3124.
3715c6fd2807SJeff Garzik 		 */
3716936fd732STejun Heo 		if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3717b6103f6dSTejun Heo 			goto out;
3718c6fd2807SJeff Garzik 
3719cea0d336SJeff Garzik 		scontrol = (scontrol & 0x0f0) | 0x304;
3720c6fd2807SJeff Garzik 
3721936fd732STejun Heo 		if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3722b6103f6dSTejun Heo 			goto out;
3723c6fd2807SJeff Garzik 
3724936fd732STejun Heo 		sata_set_spd(link);
3725c6fd2807SJeff Garzik 	}
3726c6fd2807SJeff Garzik 
3727c6fd2807SJeff Garzik 	/* issue phy wake/reset */
3728936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3729b6103f6dSTejun Heo 		goto out;
3730c6fd2807SJeff Garzik 
3731c6fd2807SJeff Garzik 	scontrol = (scontrol & 0x0f0) | 0x301;
3732c6fd2807SJeff Garzik 
3733936fd732STejun Heo 	if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
3734b6103f6dSTejun Heo 		goto out;
3735c6fd2807SJeff Garzik 
3736c6fd2807SJeff Garzik 	/* Couldn't find anything in SATA I/II specs, but AHCI-1.1
3737c6fd2807SJeff Garzik 	 * 10.4.2 says at least 1 ms.
3738c6fd2807SJeff Garzik 	 */
373997750cebSTejun Heo 	ata_msleep(link->ap, 1);
3740c6fd2807SJeff Garzik 
3741936fd732STejun Heo 	/* bring link back */
3742936fd732STejun Heo 	rc = sata_link_resume(link, timing, deadline);
37439dadd45bSTejun Heo 	if (rc)
37449dadd45bSTejun Heo 		goto out;
37459dadd45bSTejun Heo 	/* if link is offline nothing more to do */
3746b1c72916STejun Heo 	if (ata_phys_link_offline(link))
37479dadd45bSTejun Heo 		goto out;
37489dadd45bSTejun Heo 
37499dadd45bSTejun Heo 	/* Link is online.  From this point, -ENODEV too is an error. */
37509dadd45bSTejun Heo 	if (online)
37519dadd45bSTejun Heo 		*online = true;
37529dadd45bSTejun Heo 
3753071f44b1STejun Heo 	if (sata_pmp_supported(link->ap) && ata_is_host_link(link)) {
37549dadd45bSTejun Heo 		/* If PMP is supported, we have to do follow-up SRST.
37559dadd45bSTejun Heo 		 * Some PMPs don't send D2H Reg FIS after hardreset if
37569dadd45bSTejun Heo 		 * the first port is empty.  Wait only for
37579dadd45bSTejun Heo 		 * ATA_TMOUT_PMP_SRST_WAIT.
37589dadd45bSTejun Heo 		 */
37599dadd45bSTejun Heo 		if (check_ready) {
37609dadd45bSTejun Heo 			unsigned long pmp_deadline;
37619dadd45bSTejun Heo 
3762341c2c95STejun Heo 			pmp_deadline = ata_deadline(jiffies,
3763341c2c95STejun Heo 						    ATA_TMOUT_PMP_SRST_WAIT);
37649dadd45bSTejun Heo 			if (time_after(pmp_deadline, deadline))
37659dadd45bSTejun Heo 				pmp_deadline = deadline;
37669dadd45bSTejun Heo 			ata_wait_ready(link, pmp_deadline, check_ready);
37679dadd45bSTejun Heo 		}
37689dadd45bSTejun Heo 		rc = -EAGAIN;
37699dadd45bSTejun Heo 		goto out;
37709dadd45bSTejun Heo 	}
37719dadd45bSTejun Heo 
37729dadd45bSTejun Heo 	rc = 0;
37739dadd45bSTejun Heo 	if (check_ready)
37749dadd45bSTejun Heo 		rc = ata_wait_ready(link, deadline, check_ready);
3775b6103f6dSTejun Heo  out:
37760cbf0711STejun Heo 	if (rc && rc != -EAGAIN) {
37770cbf0711STejun Heo 		/* online is set iff link is online && reset succeeded */
37780cbf0711STejun Heo 		if (online)
37790cbf0711STejun Heo 			*online = false;
3780a9a79dfeSJoe Perches 		ata_link_err(link, "COMRESET failed (errno=%d)\n", rc);
37810cbf0711STejun Heo 	}
3782b6103f6dSTejun Heo 	DPRINTK("EXIT, rc=%d\n", rc);
3783b6103f6dSTejun Heo 	return rc;
3784b6103f6dSTejun Heo }
3785b6103f6dSTejun Heo 
3786b6103f6dSTejun Heo /**
378757c9efdfSTejun Heo  *	sata_std_hardreset - COMRESET w/o waiting or classification
378857c9efdfSTejun Heo  *	@link: link to reset
378957c9efdfSTejun Heo  *	@class: resulting class of attached device
379057c9efdfSTejun Heo  *	@deadline: deadline jiffies for the operation
379157c9efdfSTejun Heo  *
379257c9efdfSTejun Heo  *	Standard SATA COMRESET w/o waiting or classification.
379357c9efdfSTejun Heo  *
379457c9efdfSTejun Heo  *	LOCKING:
379557c9efdfSTejun Heo  *	Kernel thread context (may sleep)
379657c9efdfSTejun Heo  *
379757c9efdfSTejun Heo  *	RETURNS:
379857c9efdfSTejun Heo  *	0 if link offline, -EAGAIN if link online, -errno on errors.
379957c9efdfSTejun Heo  */
380057c9efdfSTejun Heo int sata_std_hardreset(struct ata_link *link, unsigned int *class,
380157c9efdfSTejun Heo 		       unsigned long deadline)
380257c9efdfSTejun Heo {
380357c9efdfSTejun Heo 	const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
380457c9efdfSTejun Heo 	bool online;
380557c9efdfSTejun Heo 	int rc;
380657c9efdfSTejun Heo 
380757c9efdfSTejun Heo 	/* do hardreset */
380857c9efdfSTejun Heo 	rc = sata_link_hardreset(link, timing, deadline, &online, NULL);
380957c9efdfSTejun Heo 	return online ? -EAGAIN : rc;
381057c9efdfSTejun Heo }
381157c9efdfSTejun Heo 
381257c9efdfSTejun Heo /**
3813203c75b8STejun Heo  *	ata_std_postreset - standard postreset callback
3814cc0680a5STejun Heo  *	@link: the target ata_link
3815c6fd2807SJeff Garzik  *	@classes: classes of attached devices
3816c6fd2807SJeff Garzik  *
3817c6fd2807SJeff Garzik  *	This function is invoked after a successful reset.  Note that
3818c6fd2807SJeff Garzik  *	the device might have been reset more than once using
3819c6fd2807SJeff Garzik  *	different reset methods before postreset is invoked.
3820c6fd2807SJeff Garzik  *
3821c6fd2807SJeff Garzik  *	LOCKING:
3822c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3823c6fd2807SJeff Garzik  */
3824203c75b8STejun Heo void ata_std_postreset(struct ata_link *link, unsigned int *classes)
3825c6fd2807SJeff Garzik {
3826f046519fSTejun Heo 	u32 serror;
3827f046519fSTejun Heo 
3828c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
3829c6fd2807SJeff Garzik 
3830f046519fSTejun Heo 	/* reset complete, clear SError */
3831f046519fSTejun Heo 	if (!sata_scr_read(link, SCR_ERROR, &serror))
3832f046519fSTejun Heo 		sata_scr_write(link, SCR_ERROR, serror);
3833f046519fSTejun Heo 
3834c6fd2807SJeff Garzik 	/* print link status */
3835936fd732STejun Heo 	sata_print_link_status(link);
3836c6fd2807SJeff Garzik 
3837c6fd2807SJeff Garzik 	DPRINTK("EXIT\n");
3838c6fd2807SJeff Garzik }
3839c6fd2807SJeff Garzik 
3840c6fd2807SJeff Garzik /**
3841c6fd2807SJeff Garzik  *	ata_dev_same_device - Determine whether new ID matches configured device
3842c6fd2807SJeff Garzik  *	@dev: device to compare against
3843c6fd2807SJeff Garzik  *	@new_class: class of the new device
3844c6fd2807SJeff Garzik  *	@new_id: IDENTIFY page of the new device
3845c6fd2807SJeff Garzik  *
3846c6fd2807SJeff Garzik  *	Compare @new_class and @new_id against @dev and determine
3847c6fd2807SJeff Garzik  *	whether @dev is the device indicated by @new_class and
3848c6fd2807SJeff Garzik  *	@new_id.
3849c6fd2807SJeff Garzik  *
3850c6fd2807SJeff Garzik  *	LOCKING:
3851c6fd2807SJeff Garzik  *	None.
3852c6fd2807SJeff Garzik  *
3853c6fd2807SJeff Garzik  *	RETURNS:
3854c6fd2807SJeff Garzik  *	1 if @dev matches @new_class and @new_id, 0 otherwise.
3855c6fd2807SJeff Garzik  */
3856c6fd2807SJeff Garzik static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
3857c6fd2807SJeff Garzik 			       const u16 *new_id)
3858c6fd2807SJeff Garzik {
3859c6fd2807SJeff Garzik 	const u16 *old_id = dev->id;
3860a0cf733bSTejun Heo 	unsigned char model[2][ATA_ID_PROD_LEN + 1];
3861a0cf733bSTejun Heo 	unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
3862c6fd2807SJeff Garzik 
3863c6fd2807SJeff Garzik 	if (dev->class != new_class) {
3864a9a79dfeSJoe Perches 		ata_dev_info(dev, "class mismatch %d != %d\n",
3865c6fd2807SJeff Garzik 			     dev->class, new_class);
3866c6fd2807SJeff Garzik 		return 0;
3867c6fd2807SJeff Garzik 	}
3868c6fd2807SJeff Garzik 
3869a0cf733bSTejun Heo 	ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
3870a0cf733bSTejun Heo 	ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
3871a0cf733bSTejun Heo 	ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
3872a0cf733bSTejun Heo 	ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
3873c6fd2807SJeff Garzik 
3874c6fd2807SJeff Garzik 	if (strcmp(model[0], model[1])) {
3875a9a79dfeSJoe Perches 		ata_dev_info(dev, "model number mismatch '%s' != '%s'\n",
3876a9a79dfeSJoe Perches 			     model[0], model[1]);
3877c6fd2807SJeff Garzik 		return 0;
3878c6fd2807SJeff Garzik 	}
3879c6fd2807SJeff Garzik 
3880c6fd2807SJeff Garzik 	if (strcmp(serial[0], serial[1])) {
3881a9a79dfeSJoe Perches 		ata_dev_info(dev, "serial number mismatch '%s' != '%s'\n",
3882a9a79dfeSJoe Perches 			     serial[0], serial[1]);
3883c6fd2807SJeff Garzik 		return 0;
3884c6fd2807SJeff Garzik 	}
3885c6fd2807SJeff Garzik 
3886c6fd2807SJeff Garzik 	return 1;
3887c6fd2807SJeff Garzik }
3888c6fd2807SJeff Garzik 
3889c6fd2807SJeff Garzik /**
3890fe30911bSTejun Heo  *	ata_dev_reread_id - Re-read IDENTIFY data
38913fae450cSHenrik Kretzschmar  *	@dev: target ATA device
3892bff04647STejun Heo  *	@readid_flags: read ID flags
3893c6fd2807SJeff Garzik  *
3894c6fd2807SJeff Garzik  *	Re-read IDENTIFY page and make sure @dev is still attached to
3895c6fd2807SJeff Garzik  *	the port.
3896c6fd2807SJeff Garzik  *
3897c6fd2807SJeff Garzik  *	LOCKING:
3898c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3899c6fd2807SJeff Garzik  *
3900c6fd2807SJeff Garzik  *	RETURNS:
3901c6fd2807SJeff Garzik  *	0 on success, negative errno otherwise
3902c6fd2807SJeff Garzik  */
3903fe30911bSTejun Heo int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
3904c6fd2807SJeff Garzik {
3905c6fd2807SJeff Garzik 	unsigned int class = dev->class;
39069af5c9c9STejun Heo 	u16 *id = (void *)dev->link->ap->sector_buf;
3907c6fd2807SJeff Garzik 	int rc;
3908c6fd2807SJeff Garzik 
3909c6fd2807SJeff Garzik 	/* read ID data */
3910bff04647STejun Heo 	rc = ata_dev_read_id(dev, &class, readid_flags, id);
3911c6fd2807SJeff Garzik 	if (rc)
3912fe30911bSTejun Heo 		return rc;
3913c6fd2807SJeff Garzik 
3914c6fd2807SJeff Garzik 	/* is the device still there? */
3915fe30911bSTejun Heo 	if (!ata_dev_same_device(dev, class, id))
3916fe30911bSTejun Heo 		return -ENODEV;
3917c6fd2807SJeff Garzik 
3918c6fd2807SJeff Garzik 	memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
3919fe30911bSTejun Heo 	return 0;
3920fe30911bSTejun Heo }
3921fe30911bSTejun Heo 
3922fe30911bSTejun Heo /**
3923fe30911bSTejun Heo  *	ata_dev_revalidate - Revalidate ATA device
3924fe30911bSTejun Heo  *	@dev: device to revalidate
3925422c9daaSTejun Heo  *	@new_class: new class code
3926fe30911bSTejun Heo  *	@readid_flags: read ID flags
3927fe30911bSTejun Heo  *
3928fe30911bSTejun Heo  *	Re-read IDENTIFY page, make sure @dev is still attached to the
3929fe30911bSTejun Heo  *	port and reconfigure it according to the new IDENTIFY page.
3930fe30911bSTejun Heo  *
3931fe30911bSTejun Heo  *	LOCKING:
3932fe30911bSTejun Heo  *	Kernel thread context (may sleep)
3933fe30911bSTejun Heo  *
3934fe30911bSTejun Heo  *	RETURNS:
3935fe30911bSTejun Heo  *	0 on success, negative errno otherwise
3936fe30911bSTejun Heo  */
3937422c9daaSTejun Heo int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
3938422c9daaSTejun Heo 		       unsigned int readid_flags)
3939fe30911bSTejun Heo {
39406ddcd3b0STejun Heo 	u64 n_sectors = dev->n_sectors;
39415920dadfSTejun Heo 	u64 n_native_sectors = dev->n_native_sectors;
3942fe30911bSTejun Heo 	int rc;
3943fe30911bSTejun Heo 
3944fe30911bSTejun Heo 	if (!ata_dev_enabled(dev))
3945fe30911bSTejun Heo 		return -ENODEV;
3946fe30911bSTejun Heo 
3947422c9daaSTejun Heo 	/* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
3948422c9daaSTejun Heo 	if (ata_class_enabled(new_class) &&
3949f0d0613dSBorislav Petkov 	    new_class != ATA_DEV_ATA &&
3950f0d0613dSBorislav Petkov 	    new_class != ATA_DEV_ATAPI &&
3951f0d0613dSBorislav Petkov 	    new_class != ATA_DEV_SEMB) {
3952a9a79dfeSJoe Perches 		ata_dev_info(dev, "class mismatch %u != %u\n",
3953422c9daaSTejun Heo 			     dev->class, new_class);
3954422c9daaSTejun Heo 		rc = -ENODEV;
3955422c9daaSTejun Heo 		goto fail;
3956422c9daaSTejun Heo 	}
3957422c9daaSTejun Heo 
3958fe30911bSTejun Heo 	/* re-read ID */
3959fe30911bSTejun Heo 	rc = ata_dev_reread_id(dev, readid_flags);
3960fe30911bSTejun Heo 	if (rc)
3961fe30911bSTejun Heo 		goto fail;
3962c6fd2807SJeff Garzik 
3963c6fd2807SJeff Garzik 	/* configure device according to the new ID */
3964efdaedc4STejun Heo 	rc = ata_dev_configure(dev);
39656ddcd3b0STejun Heo 	if (rc)
39666ddcd3b0STejun Heo 		goto fail;
39676ddcd3b0STejun Heo 
39686ddcd3b0STejun Heo 	/* verify n_sectors hasn't changed */
3969445d211bSTejun Heo 	if (dev->class != ATA_DEV_ATA || !n_sectors ||
3970445d211bSTejun Heo 	    dev->n_sectors == n_sectors)
3971445d211bSTejun Heo 		return 0;
3972445d211bSTejun Heo 
3973445d211bSTejun Heo 	/* n_sectors has changed */
3974a9a79dfeSJoe Perches 	ata_dev_warn(dev, "n_sectors mismatch %llu != %llu\n",
39756ddcd3b0STejun Heo 		     (unsigned long long)n_sectors,
39766ddcd3b0STejun Heo 		     (unsigned long long)dev->n_sectors);
3977445d211bSTejun Heo 
39785920dadfSTejun Heo 	/*
39795920dadfSTejun Heo 	 * Something could have caused HPA to be unlocked
3980445d211bSTejun Heo 	 * involuntarily.  If n_native_sectors hasn't changed and the
3981445d211bSTejun Heo 	 * new size matches it, keep the device.
39825920dadfSTejun Heo 	 */
39835920dadfSTejun Heo 	if (dev->n_native_sectors == n_native_sectors &&
3984445d211bSTejun Heo 	    dev->n_sectors > n_sectors && dev->n_sectors == n_native_sectors) {
3985a9a79dfeSJoe Perches 		ata_dev_warn(dev,
39865920dadfSTejun Heo 			     "new n_sectors matches native, probably "
398768939ce5STejun Heo 			     "late HPA unlock, n_sectors updated\n");
398868939ce5STejun Heo 		/* use the larger n_sectors */
3989445d211bSTejun Heo 		return 0;
3990445d211bSTejun Heo 	}
3991445d211bSTejun Heo 
3992445d211bSTejun Heo 	/*
3993445d211bSTejun Heo 	 * Some BIOSes boot w/o HPA but resume w/ HPA locked.  Try
3994445d211bSTejun Heo 	 * unlocking HPA in those cases.
3995445d211bSTejun Heo 	 *
3996445d211bSTejun Heo 	 * https://bugzilla.kernel.org/show_bug.cgi?id=15396
3997445d211bSTejun Heo 	 */
3998445d211bSTejun Heo 	if (dev->n_native_sectors == n_native_sectors &&
3999445d211bSTejun Heo 	    dev->n_sectors < n_sectors && n_sectors == n_native_sectors &&
4000445d211bSTejun Heo 	    !(dev->horkage & ATA_HORKAGE_BROKEN_HPA)) {
4001a9a79dfeSJoe Perches 		ata_dev_warn(dev,
4002445d211bSTejun Heo 			     "old n_sectors matches native, probably "
4003445d211bSTejun Heo 			     "late HPA lock, will try to unlock HPA\n");
4004445d211bSTejun Heo 		/* try unlocking HPA */
4005445d211bSTejun Heo 		dev->flags |= ATA_DFLAG_UNLOCK_HPA;
4006445d211bSTejun Heo 		rc = -EIO;
4007445d211bSTejun Heo 	} else
4008445d211bSTejun Heo 		rc = -ENODEV;
4009445d211bSTejun Heo 
4010445d211bSTejun Heo 	/* restore original n_[native_]sectors and fail */
40115920dadfSTejun Heo 	dev->n_native_sectors = n_native_sectors;
40125920dadfSTejun Heo 	dev->n_sectors = n_sectors;
4013c6fd2807SJeff Garzik  fail:
4014a9a79dfeSJoe Perches 	ata_dev_err(dev, "revalidation failed (errno=%d)\n", rc);
4015c6fd2807SJeff Garzik 	return rc;
4016c6fd2807SJeff Garzik }
4017c6fd2807SJeff Garzik 
40186919a0a6SAlan Cox struct ata_blacklist_entry {
40196919a0a6SAlan Cox 	const char *model_num;
40206919a0a6SAlan Cox 	const char *model_rev;
40216919a0a6SAlan Cox 	unsigned long horkage;
40226919a0a6SAlan Cox };
40236919a0a6SAlan Cox 
40246919a0a6SAlan Cox static const struct ata_blacklist_entry ata_device_blacklist [] = {
40256919a0a6SAlan Cox 	/* Devices with DMA related problems under Linux */
40266919a0a6SAlan Cox 	{ "WDC AC11000H",	NULL,		ATA_HORKAGE_NODMA },
40276919a0a6SAlan Cox 	{ "WDC AC22100H",	NULL,		ATA_HORKAGE_NODMA },
40286919a0a6SAlan Cox 	{ "WDC AC32500H",	NULL,		ATA_HORKAGE_NODMA },
40296919a0a6SAlan Cox 	{ "WDC AC33100H",	NULL,		ATA_HORKAGE_NODMA },
40306919a0a6SAlan Cox 	{ "WDC AC31600H",	NULL,		ATA_HORKAGE_NODMA },
40316919a0a6SAlan Cox 	{ "WDC AC32100H",	"24.09P07",	ATA_HORKAGE_NODMA },
40326919a0a6SAlan Cox 	{ "WDC AC23200L",	"21.10N21",	ATA_HORKAGE_NODMA },
40336919a0a6SAlan Cox 	{ "Compaq CRD-8241B", 	NULL,		ATA_HORKAGE_NODMA },
40346919a0a6SAlan Cox 	{ "CRD-8400B",		NULL, 		ATA_HORKAGE_NODMA },
40357da4c935SMark Lord 	{ "CRD-848[02]B",	NULL,		ATA_HORKAGE_NODMA },
40366919a0a6SAlan Cox 	{ "CRD-84",		NULL,		ATA_HORKAGE_NODMA },
40376919a0a6SAlan Cox 	{ "SanDisk SDP3B",	NULL,		ATA_HORKAGE_NODMA },
40386919a0a6SAlan Cox 	{ "SanDisk SDP3B-64",	NULL,		ATA_HORKAGE_NODMA },
40396919a0a6SAlan Cox 	{ "SANYO CD-ROM CRD",	NULL,		ATA_HORKAGE_NODMA },
40406919a0a6SAlan Cox 	{ "HITACHI CDR-8",	NULL,		ATA_HORKAGE_NODMA },
40417da4c935SMark Lord 	{ "HITACHI CDR-8[34]35",NULL,		ATA_HORKAGE_NODMA },
40426919a0a6SAlan Cox 	{ "Toshiba CD-ROM XM-6202B", NULL,	ATA_HORKAGE_NODMA },
40436919a0a6SAlan Cox 	{ "TOSHIBA CD-ROM XM-1702BC", NULL,	ATA_HORKAGE_NODMA },
40446919a0a6SAlan Cox 	{ "CD-532E-A", 		NULL,		ATA_HORKAGE_NODMA },
40456919a0a6SAlan Cox 	{ "E-IDE CD-ROM CR-840",NULL,		ATA_HORKAGE_NODMA },
40466919a0a6SAlan Cox 	{ "CD-ROM Drive/F5A",	NULL,		ATA_HORKAGE_NODMA },
40476919a0a6SAlan Cox 	{ "WPI CDD-820", 	NULL,		ATA_HORKAGE_NODMA },
40486919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SC-148C", NULL,	ATA_HORKAGE_NODMA },
40496919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SC",	NULL,		ATA_HORKAGE_NODMA },
40506919a0a6SAlan Cox 	{ "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
40516919a0a6SAlan Cox 	{ "_NEC DV5800A", 	NULL,		ATA_HORKAGE_NODMA },
40526919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SN-124", "N001",	ATA_HORKAGE_NODMA },
405339f19886SDave Jones 	{ "Seagate STT20000A", NULL,		ATA_HORKAGE_NODMA },
40543af9a77aSTejun Heo 	/* Odd clown on sil3726/4726 PMPs */
405550af2fa1STejun Heo 	{ "Config  Disk",	NULL,		ATA_HORKAGE_DISABLE },
40566919a0a6SAlan Cox 
405718d6e9d5SAlbert Lee 	/* Weird ATAPI devices */
405840a1d531STejun Heo 	{ "TORiSAN DVD-ROM DRD-N216", NULL,	ATA_HORKAGE_MAX_SEC_128 },
40596a87e42eSTejun Heo 	{ "QUANTUM DAT    DAT72-000", NULL,	ATA_HORKAGE_ATAPI_MOD16_DMA },
406018d6e9d5SAlbert Lee 
40616919a0a6SAlan Cox 	/* Devices we expect to fail diagnostics */
40626919a0a6SAlan Cox 
40636919a0a6SAlan Cox 	/* Devices where NCQ should be avoided */
40646919a0a6SAlan Cox 	/* NCQ is slow */
40656919a0a6SAlan Cox 	{ "WDC WD740ADFD-00",	NULL,		ATA_HORKAGE_NONCQ },
4066459ad688STejun Heo 	{ "WDC WD740ADFD-00NLR1", NULL,		ATA_HORKAGE_NONCQ, },
406709125ea6STejun Heo 	/* http://thread.gmane.org/gmane.linux.ide/14907 */
406809125ea6STejun Heo 	{ "FUJITSU MHT2060BH",	NULL,		ATA_HORKAGE_NONCQ },
40697acfaf30SPaul Rolland 	/* NCQ is broken */
4070539cc7c7SJeff Garzik 	{ "Maxtor *",		"BANC*",	ATA_HORKAGE_NONCQ },
40710e3dbc01SAlan Cox 	{ "Maxtor 7V300F0",	"VA111630",	ATA_HORKAGE_NONCQ },
4072da6f0ec2SPaolo Ornati 	{ "ST380817AS",		"3.42",		ATA_HORKAGE_NONCQ },
4073e41bd3e8STejun Heo 	{ "ST3160023AS",	"3.42",		ATA_HORKAGE_NONCQ },
40745ccfca97SLubomir Bulej 	{ "OCZ CORE_SSD",	"02.10104",	ATA_HORKAGE_NONCQ },
4075539cc7c7SJeff Garzik 
4076ac70a964STejun Heo 	/* Seagate NCQ + FLUSH CACHE firmware bug */
40774d1f9082SMark Lord 	{ "ST31500341AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
4078ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4079d10d491fSTejun Heo 
40804d1f9082SMark Lord 	{ "ST31000333AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
4081d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4082d10d491fSTejun Heo 
40834d1f9082SMark Lord 	{ "ST3640[36]23AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
4084d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4085d10d491fSTejun Heo 
40864d1f9082SMark Lord 	{ "ST3320[68]13AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
4087ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4088ac70a964STejun Heo 
408936e337d0SRobert Hancock 	/* Blacklist entries taken from Silicon Image 3124/3132
409036e337d0SRobert Hancock 	   Windows driver .inf file - also several Linux problem reports */
409136e337d0SRobert Hancock 	{ "HTS541060G9SA00",    "MB3OC60D",     ATA_HORKAGE_NONCQ, },
409236e337d0SRobert Hancock 	{ "HTS541080G9SA00",    "MB4OC60D",     ATA_HORKAGE_NONCQ, },
409336e337d0SRobert Hancock 	{ "HTS541010G9SA00",    "MBZOC60D",     ATA_HORKAGE_NONCQ, },
40946919a0a6SAlan Cox 
409568b0ddb2STejun Heo 	/* https://bugzilla.kernel.org/show_bug.cgi?id=15573 */
409668b0ddb2STejun Heo 	{ "C300-CTFDDAC128MAG",	"0001",		ATA_HORKAGE_NONCQ, },
409768b0ddb2STejun Heo 
409816c55b03STejun Heo 	/* devices which puke on READ_NATIVE_MAX */
409916c55b03STejun Heo 	{ "HDS724040KLSA80",	"KFAOA20N",	ATA_HORKAGE_BROKEN_HPA, },
410016c55b03STejun Heo 	{ "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
410116c55b03STejun Heo 	{ "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
410216c55b03STejun Heo 	{ "MAXTOR 6L080L4",	"A93.0500",	ATA_HORKAGE_BROKEN_HPA },
41036919a0a6SAlan Cox 
41047831387bSTejun Heo 	/* this one allows HPA unlocking but fails IOs on the area */
41057831387bSTejun Heo 	{ "OCZ-VERTEX",		    "1.30",	ATA_HORKAGE_BROKEN_HPA },
41067831387bSTejun Heo 
410793328e11SAlan Cox 	/* Devices which report 1 sector over size HPA */
410893328e11SAlan Cox 	{ "ST340823A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
410993328e11SAlan Cox 	{ "ST320413A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
4110b152fcd3SMikko Rapeli 	{ "ST310211A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
411193328e11SAlan Cox 
41126bbfd53dSAlan Cox 	/* Devices which get the IVB wrong */
41136bbfd53dSAlan Cox 	{ "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB, },
4114a79067e5SAlan Cox 	/* Maybe we should just blacklist TSSTcorp... */
41157da4c935SMark Lord 	{ "TSSTcorp CDDVDW SH-S202[HJN]", "SB0[01]",  ATA_HORKAGE_IVB, },
41166bbfd53dSAlan Cox 
41179ce8e307SJens Axboe 	/* Devices that do not need bridging limits applied */
41189ce8e307SJens Axboe 	{ "MTRON MSP-SATA*",		NULL,	ATA_HORKAGE_BRIDGE_OK, },
41199ce8e307SJens Axboe 
41209062712fSTejun Heo 	/* Devices which aren't very happy with higher link speeds */
41219062712fSTejun Heo 	{ "WD My Book",			NULL,	ATA_HORKAGE_1_5_GBPS, },
41229062712fSTejun Heo 
4123d0cb43b3STejun Heo 	/*
4124d0cb43b3STejun Heo 	 * Devices which choke on SETXFER.  Applies only if both the
4125d0cb43b3STejun Heo 	 * device and controller are SATA.
4126d0cb43b3STejun Heo 	 */
4127cd691876STejun Heo 	{ "PIONEER DVD-RW  DVRTD08",	NULL,	ATA_HORKAGE_NOSETXFER },
41283a25179eSVladimir LAVALLADE 	{ "PIONEER DVD-RW  DVRTD08A",	NULL,	ATA_HORKAGE_NOSETXFER },
41293a25179eSVladimir LAVALLADE 	{ "PIONEER DVD-RW  DVR-215",	NULL,	ATA_HORKAGE_NOSETXFER },
4130cd691876STejun Heo 	{ "PIONEER DVD-RW  DVR-212D",	NULL,	ATA_HORKAGE_NOSETXFER },
4131cd691876STejun Heo 	{ "PIONEER DVD-RW  DVR-216D",	NULL,	ATA_HORKAGE_NOSETXFER },
4132d0cb43b3STejun Heo 
41336919a0a6SAlan Cox 	/* End Marker */
41346919a0a6SAlan Cox 	{ }
4135c6fd2807SJeff Garzik };
4136c6fd2807SJeff Garzik 
4137bce036ceSMark Lord /**
4138bce036ceSMark Lord  *	glob_match - match a text string against a glob-style pattern
4139bce036ceSMark Lord  *	@text: the string to be examined
4140bce036ceSMark Lord  *	@pattern: the glob-style pattern to be matched against
4141bce036ceSMark Lord  *
4142bce036ceSMark Lord  *	Either/both of text and pattern can be empty strings.
4143bce036ceSMark Lord  *
4144bce036ceSMark Lord  *	Match text against a glob-style pattern, with wildcards and simple sets:
4145bce036ceSMark Lord  *
4146bce036ceSMark Lord  *		?	matches any single character.
4147bce036ceSMark Lord  *		*	matches any run of characters.
4148bce036ceSMark Lord  *		[xyz]	matches a single character from the set: x, y, or z.
41492f9e4d16SMark Lord  *		[a-d]	matches a single character from the range: a, b, c, or d.
41502f9e4d16SMark Lord  *		[a-d0-9] matches a single character from either range.
4151bce036ceSMark Lord  *
41522f9e4d16SMark Lord  *	The special characters ?, [, -, or *, can be matched using a set, eg. [*]
41532f9e4d16SMark Lord  *	Behaviour with malformed patterns is undefined, though generally reasonable.
4154bce036ceSMark Lord  *
41553d2be54bSRandy Dunlap  *	Sample patterns:  "SD1?",  "SD1[0-5]",  "*R0",  "SD*1?[012]*xx"
4156bce036ceSMark Lord  *
4157bce036ceSMark Lord  *	This function uses one level of recursion per '*' in pattern.
4158bce036ceSMark Lord  *	Since it calls _nothing_ else, and has _no_ explicit local variables,
4159bce036ceSMark Lord  *	this will not cause stack problems for any reasonable use here.
4160bce036ceSMark Lord  *
4161bce036ceSMark Lord  *	RETURNS:
4162bce036ceSMark Lord  *	0 on match, 1 otherwise.
4163539cc7c7SJeff Garzik  */
4164bce036ceSMark Lord static int glob_match (const char *text, const char *pattern)
4165bce036ceSMark Lord {
4166bce036ceSMark Lord 	do {
4167bce036ceSMark Lord 		/* Match single character or a '?' wildcard */
4168bce036ceSMark Lord 		if (*text == *pattern || *pattern == '?') {
4169bce036ceSMark Lord 			if (!*pattern++)
4170bce036ceSMark Lord 				return 0;  /* End of both strings: match */
4171bce036ceSMark Lord 		} else {
4172bce036ceSMark Lord 			/* Match single char against a '[' bracketed ']' pattern set */
4173bce036ceSMark Lord 			if (!*text || *pattern != '[')
4174bce036ceSMark Lord 				break;  /* Not a pattern set */
41752f9e4d16SMark Lord 			while (*++pattern && *pattern != ']' && *text != *pattern) {
41762f9e4d16SMark Lord 				if (*pattern == '-' && *(pattern - 1) != '[')
41772f9e4d16SMark Lord 					if (*text > *(pattern - 1) && *text < *(pattern + 1)) {
41782f9e4d16SMark Lord 						++pattern;
41792f9e4d16SMark Lord 						break;
41802f9e4d16SMark Lord 					}
41812f9e4d16SMark Lord 			}
4182bce036ceSMark Lord 			if (!*pattern || *pattern == ']')
4183bce036ceSMark Lord 				return 1;  /* No match */
4184bce036ceSMark Lord 			while (*pattern && *pattern++ != ']');
4185317b50b8SAndrew Paprocki 		}
4186bce036ceSMark Lord 	} while (*++text && *pattern);
4187539cc7c7SJeff Garzik 
4188bce036ceSMark Lord 	/* Match any run of chars against a '*' wildcard */
4189bce036ceSMark Lord 	if (*pattern == '*') {
4190bce036ceSMark Lord 		if (!*++pattern)
4191bce036ceSMark Lord 			return 0;  /* Match: avoid recursion at end of pattern */
4192bce036ceSMark Lord 		/* Loop to handle additional pattern chars after the wildcard */
4193bce036ceSMark Lord 		while (*text) {
4194bce036ceSMark Lord 			if (glob_match(text, pattern) == 0)
4195bce036ceSMark Lord 				return 0;  /* Remainder matched */
4196bce036ceSMark Lord 			++text;  /* Absorb (match) this char and try again */
4197bce036ceSMark Lord 		}
4198bce036ceSMark Lord 	}
4199bce036ceSMark Lord 	if (!*text && !*pattern)
4200bce036ceSMark Lord 		return 0;  /* End of both strings: match */
4201bce036ceSMark Lord 	return 1;  /* No match */
4202539cc7c7SJeff Garzik }
4203539cc7c7SJeff Garzik 
420475683fe7STejun Heo static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
4205c6fd2807SJeff Garzik {
42068bfa79fcSTejun Heo 	unsigned char model_num[ATA_ID_PROD_LEN + 1];
42078bfa79fcSTejun Heo 	unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
42086919a0a6SAlan Cox 	const struct ata_blacklist_entry *ad = ata_device_blacklist;
4209c6fd2807SJeff Garzik 
42108bfa79fcSTejun Heo 	ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
42118bfa79fcSTejun Heo 	ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
4212c6fd2807SJeff Garzik 
42136919a0a6SAlan Cox 	while (ad->model_num) {
4214bce036ceSMark Lord 		if (!glob_match(model_num, ad->model_num)) {
42156919a0a6SAlan Cox 			if (ad->model_rev == NULL)
42166919a0a6SAlan Cox 				return ad->horkage;
4217bce036ceSMark Lord 			if (!glob_match(model_rev, ad->model_rev))
42186919a0a6SAlan Cox 				return ad->horkage;
4219c6fd2807SJeff Garzik 		}
42206919a0a6SAlan Cox 		ad++;
4221c6fd2807SJeff Garzik 	}
4222c6fd2807SJeff Garzik 	return 0;
4223c6fd2807SJeff Garzik }
4224c6fd2807SJeff Garzik 
42256919a0a6SAlan Cox static int ata_dma_blacklisted(const struct ata_device *dev)
42266919a0a6SAlan Cox {
42276919a0a6SAlan Cox 	/* We don't support polling DMA.
42286919a0a6SAlan Cox 	 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
42296919a0a6SAlan Cox 	 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
42306919a0a6SAlan Cox 	 */
42319af5c9c9STejun Heo 	if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
42326919a0a6SAlan Cox 	    (dev->flags & ATA_DFLAG_CDB_INTR))
42336919a0a6SAlan Cox 		return 1;
423475683fe7STejun Heo 	return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
42356919a0a6SAlan Cox }
42366919a0a6SAlan Cox 
4237c6fd2807SJeff Garzik /**
42386bbfd53dSAlan Cox  *	ata_is_40wire		-	check drive side detection
42396bbfd53dSAlan Cox  *	@dev: device
42406bbfd53dSAlan Cox  *
42416bbfd53dSAlan Cox  *	Perform drive side detection decoding, allowing for device vendors
42426bbfd53dSAlan Cox  *	who can't follow the documentation.
42436bbfd53dSAlan Cox  */
42446bbfd53dSAlan Cox 
42456bbfd53dSAlan Cox static int ata_is_40wire(struct ata_device *dev)
42466bbfd53dSAlan Cox {
42476bbfd53dSAlan Cox 	if (dev->horkage & ATA_HORKAGE_IVB)
42486bbfd53dSAlan Cox 		return ata_drive_40wire_relaxed(dev->id);
42496bbfd53dSAlan Cox 	return ata_drive_40wire(dev->id);
42506bbfd53dSAlan Cox }
42516bbfd53dSAlan Cox 
42526bbfd53dSAlan Cox /**
425315a5551cSAlan Cox  *	cable_is_40wire		-	40/80/SATA decider
425415a5551cSAlan Cox  *	@ap: port to consider
425515a5551cSAlan Cox  *
425615a5551cSAlan Cox  *	This function encapsulates the policy for speed management
425715a5551cSAlan Cox  *	in one place. At the moment we don't cache the result but
425815a5551cSAlan Cox  *	there is a good case for setting ap->cbl to the result when
425915a5551cSAlan Cox  *	we are called with unknown cables (and figuring out if it
426015a5551cSAlan Cox  *	impacts hotplug at all).
426115a5551cSAlan Cox  *
426215a5551cSAlan Cox  *	Return 1 if the cable appears to be 40 wire.
426315a5551cSAlan Cox  */
426415a5551cSAlan Cox 
426515a5551cSAlan Cox static int cable_is_40wire(struct ata_port *ap)
426615a5551cSAlan Cox {
426715a5551cSAlan Cox 	struct ata_link *link;
426815a5551cSAlan Cox 	struct ata_device *dev;
426915a5551cSAlan Cox 
42704a9c7b33STejun Heo 	/* If the controller thinks we are 40 wire, we are. */
427115a5551cSAlan Cox 	if (ap->cbl == ATA_CBL_PATA40)
427215a5551cSAlan Cox 		return 1;
42734a9c7b33STejun Heo 
42744a9c7b33STejun Heo 	/* If the controller thinks we are 80 wire, we are. */
427515a5551cSAlan Cox 	if (ap->cbl == ATA_CBL_PATA80 || ap->cbl == ATA_CBL_SATA)
427615a5551cSAlan Cox 		return 0;
42774a9c7b33STejun Heo 
42784a9c7b33STejun Heo 	/* If the system is known to be 40 wire short cable (eg
42794a9c7b33STejun Heo 	 * laptop), then we allow 80 wire modes even if the drive
42804a9c7b33STejun Heo 	 * isn't sure.
42814a9c7b33STejun Heo 	 */
4282f792068eSAlan Cox 	if (ap->cbl == ATA_CBL_PATA40_SHORT)
4283f792068eSAlan Cox 		return 0;
428415a5551cSAlan Cox 
42854a9c7b33STejun Heo 	/* If the controller doesn't know, we scan.
42864a9c7b33STejun Heo 	 *
42874a9c7b33STejun Heo 	 * Note: We look for all 40 wire detects at this point.  Any
42884a9c7b33STejun Heo 	 *       80 wire detect is taken to be 80 wire cable because
42894a9c7b33STejun Heo 	 * - in many setups only the one drive (slave if present) will
42904a9c7b33STejun Heo 	 *   give a valid detect
42914a9c7b33STejun Heo 	 * - if you have a non detect capable drive you don't want it
42924a9c7b33STejun Heo 	 *   to colour the choice
429315a5551cSAlan Cox 	 */
42941eca4365STejun Heo 	ata_for_each_link(link, ap, EDGE) {
42951eca4365STejun Heo 		ata_for_each_dev(dev, link, ENABLED) {
42961eca4365STejun Heo 			if (!ata_is_40wire(dev))
429715a5551cSAlan Cox 				return 0;
429815a5551cSAlan Cox 		}
429915a5551cSAlan Cox 	}
430015a5551cSAlan Cox 	return 1;
430115a5551cSAlan Cox }
430215a5551cSAlan Cox 
430315a5551cSAlan Cox /**
4304c6fd2807SJeff Garzik  *	ata_dev_xfermask - Compute supported xfermask of the given device
4305c6fd2807SJeff Garzik  *	@dev: Device to compute xfermask for
4306c6fd2807SJeff Garzik  *
4307c6fd2807SJeff Garzik  *	Compute supported xfermask of @dev and store it in
4308c6fd2807SJeff Garzik  *	dev->*_mask.  This function is responsible for applying all
4309c6fd2807SJeff Garzik  *	known limits including host controller limits, device
4310c6fd2807SJeff Garzik  *	blacklist, etc...
4311c6fd2807SJeff Garzik  *
4312c6fd2807SJeff Garzik  *	LOCKING:
4313c6fd2807SJeff Garzik  *	None.
4314c6fd2807SJeff Garzik  */
4315c6fd2807SJeff Garzik static void ata_dev_xfermask(struct ata_device *dev)
4316c6fd2807SJeff Garzik {
43179af5c9c9STejun Heo 	struct ata_link *link = dev->link;
43189af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
4319cca3974eSJeff Garzik 	struct ata_host *host = ap->host;
4320c6fd2807SJeff Garzik 	unsigned long xfer_mask;
4321c6fd2807SJeff Garzik 
4322c6fd2807SJeff Garzik 	/* controller modes available */
4323c6fd2807SJeff Garzik 	xfer_mask = ata_pack_xfermask(ap->pio_mask,
4324c6fd2807SJeff Garzik 				      ap->mwdma_mask, ap->udma_mask);
4325c6fd2807SJeff Garzik 
43268343f889SRobert Hancock 	/* drive modes available */
4327c6fd2807SJeff Garzik 	xfer_mask &= ata_pack_xfermask(dev->pio_mask,
4328c6fd2807SJeff Garzik 				       dev->mwdma_mask, dev->udma_mask);
4329c6fd2807SJeff Garzik 	xfer_mask &= ata_id_xfermask(dev->id);
4330c6fd2807SJeff Garzik 
4331b352e57dSAlan Cox 	/*
4332b352e57dSAlan Cox 	 *	CFA Advanced TrueIDE timings are not allowed on a shared
4333b352e57dSAlan Cox 	 *	cable
4334b352e57dSAlan Cox 	 */
4335b352e57dSAlan Cox 	if (ata_dev_pair(dev)) {
4336b352e57dSAlan Cox 		/* No PIO5 or PIO6 */
4337b352e57dSAlan Cox 		xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
4338b352e57dSAlan Cox 		/* No MWDMA3 or MWDMA 4 */
4339b352e57dSAlan Cox 		xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
4340b352e57dSAlan Cox 	}
4341b352e57dSAlan Cox 
4342c6fd2807SJeff Garzik 	if (ata_dma_blacklisted(dev)) {
4343c6fd2807SJeff Garzik 		xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4344a9a79dfeSJoe Perches 		ata_dev_warn(dev,
4345c6fd2807SJeff Garzik 			     "device is on DMA blacklist, disabling DMA\n");
4346c6fd2807SJeff Garzik 	}
4347c6fd2807SJeff Garzik 
434814d66ab7SPetr Vandrovec 	if ((host->flags & ATA_HOST_SIMPLEX) &&
434914d66ab7SPetr Vandrovec 	    host->simplex_claimed && host->simplex_claimed != ap) {
4350c6fd2807SJeff Garzik 		xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4351a9a79dfeSJoe Perches 		ata_dev_warn(dev,
4352a9a79dfeSJoe Perches 			     "simplex DMA is claimed by other device, disabling DMA\n");
4353c6fd2807SJeff Garzik 	}
4354c6fd2807SJeff Garzik 
4355e424675fSJeff Garzik 	if (ap->flags & ATA_FLAG_NO_IORDY)
4356e424675fSJeff Garzik 		xfer_mask &= ata_pio_mask_no_iordy(dev);
4357e424675fSJeff Garzik 
4358c6fd2807SJeff Garzik 	if (ap->ops->mode_filter)
4359a76b62caSAlan Cox 		xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
4360c6fd2807SJeff Garzik 
43618343f889SRobert Hancock 	/* Apply cable rule here.  Don't apply it early because when
43628343f889SRobert Hancock 	 * we handle hot plug the cable type can itself change.
43638343f889SRobert Hancock 	 * Check this last so that we know if the transfer rate was
43648343f889SRobert Hancock 	 * solely limited by the cable.
43658343f889SRobert Hancock 	 * Unknown or 80 wire cables reported host side are checked
43668343f889SRobert Hancock 	 * drive side as well. Cases where we know a 40wire cable
43678343f889SRobert Hancock 	 * is used safely for 80 are not checked here.
43688343f889SRobert Hancock 	 */
43698343f889SRobert Hancock 	if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
43708343f889SRobert Hancock 		/* UDMA/44 or higher would be available */
437115a5551cSAlan Cox 		if (cable_is_40wire(ap)) {
4372a9a79dfeSJoe Perches 			ata_dev_warn(dev,
43738343f889SRobert Hancock 				     "limited to UDMA/33 due to 40-wire cable\n");
43748343f889SRobert Hancock 			xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
43758343f889SRobert Hancock 		}
43768343f889SRobert Hancock 
4377c6fd2807SJeff Garzik 	ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
4378c6fd2807SJeff Garzik 			    &dev->mwdma_mask, &dev->udma_mask);
4379c6fd2807SJeff Garzik }
4380c6fd2807SJeff Garzik 
4381c6fd2807SJeff Garzik /**
4382c6fd2807SJeff Garzik  *	ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
4383c6fd2807SJeff Garzik  *	@dev: Device to which command will be sent
4384c6fd2807SJeff Garzik  *
4385c6fd2807SJeff Garzik  *	Issue SET FEATURES - XFER MODE command to device @dev
4386c6fd2807SJeff Garzik  *	on port @ap.
4387c6fd2807SJeff Garzik  *
4388c6fd2807SJeff Garzik  *	LOCKING:
4389c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
4390c6fd2807SJeff Garzik  *
4391c6fd2807SJeff Garzik  *	RETURNS:
4392c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
4393c6fd2807SJeff Garzik  */
4394c6fd2807SJeff Garzik 
4395c6fd2807SJeff Garzik static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
4396c6fd2807SJeff Garzik {
4397c6fd2807SJeff Garzik 	struct ata_taskfile tf;
4398c6fd2807SJeff Garzik 	unsigned int err_mask;
4399c6fd2807SJeff Garzik 
4400c6fd2807SJeff Garzik 	/* set up set-features taskfile */
4401c6fd2807SJeff Garzik 	DPRINTK("set features - xfer mode\n");
4402c6fd2807SJeff Garzik 
4403464cf177STejun Heo 	/* Some controllers and ATAPI devices show flaky interrupt
4404464cf177STejun Heo 	 * behavior after setting xfer mode.  Use polling instead.
4405464cf177STejun Heo 	 */
4406c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
4407c6fd2807SJeff Garzik 	tf.command = ATA_CMD_SET_FEATURES;
4408c6fd2807SJeff Garzik 	tf.feature = SETFEATURES_XFER;
4409464cf177STejun Heo 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
4410c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
4411b9f8ab2dSAlan Cox 	/* If we are using IORDY we must send the mode setting command */
4412b9f8ab2dSAlan Cox 	if (ata_pio_need_iordy(dev))
4413c6fd2807SJeff Garzik 		tf.nsect = dev->xfer_mode;
4414b9f8ab2dSAlan Cox 	/* If the device has IORDY and the controller does not - turn it off */
4415b9f8ab2dSAlan Cox  	else if (ata_id_has_iordy(dev->id))
4416b9f8ab2dSAlan Cox 		tf.nsect = 0x01;
4417b9f8ab2dSAlan Cox 	else /* In the ancient relic department - skip all of this */
4418b9f8ab2dSAlan Cox 		return 0;
4419c6fd2807SJeff Garzik 
44202b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
4421c6fd2807SJeff Garzik 
4422c6fd2807SJeff Garzik 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
4423c6fd2807SJeff Garzik 	return err_mask;
4424c6fd2807SJeff Garzik }
44251152b261STejun Heo 
4426c6fd2807SJeff Garzik /**
4427218f3d30SJeff Garzik  *	ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES
44289f45cbd3SKristen Carlson Accardi  *	@dev: Device to which command will be sent
44299f45cbd3SKristen Carlson Accardi  *	@enable: Whether to enable or disable the feature
4430218f3d30SJeff Garzik  *	@feature: The sector count represents the feature to set
44319f45cbd3SKristen Carlson Accardi  *
44329f45cbd3SKristen Carlson Accardi  *	Issue SET FEATURES - SATA FEATURES command to device @dev
4433218f3d30SJeff Garzik  *	on port @ap with sector count
44349f45cbd3SKristen Carlson Accardi  *
44359f45cbd3SKristen Carlson Accardi  *	LOCKING:
44369f45cbd3SKristen Carlson Accardi  *	PCI/etc. bus probe sem.
44379f45cbd3SKristen Carlson Accardi  *
44389f45cbd3SKristen Carlson Accardi  *	RETURNS:
44399f45cbd3SKristen Carlson Accardi  *	0 on success, AC_ERR_* mask otherwise.
44409f45cbd3SKristen Carlson Accardi  */
44411152b261STejun Heo unsigned int ata_dev_set_feature(struct ata_device *dev, u8 enable, u8 feature)
44429f45cbd3SKristen Carlson Accardi {
44439f45cbd3SKristen Carlson Accardi 	struct ata_taskfile tf;
44449f45cbd3SKristen Carlson Accardi 	unsigned int err_mask;
44459f45cbd3SKristen Carlson Accardi 
44469f45cbd3SKristen Carlson Accardi 	/* set up set-features taskfile */
44479f45cbd3SKristen Carlson Accardi 	DPRINTK("set features - SATA features\n");
44489f45cbd3SKristen Carlson Accardi 
44499f45cbd3SKristen Carlson Accardi 	ata_tf_init(dev, &tf);
44509f45cbd3SKristen Carlson Accardi 	tf.command = ATA_CMD_SET_FEATURES;
44519f45cbd3SKristen Carlson Accardi 	tf.feature = enable;
44529f45cbd3SKristen Carlson Accardi 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
44539f45cbd3SKristen Carlson Accardi 	tf.protocol = ATA_PROT_NODATA;
4454218f3d30SJeff Garzik 	tf.nsect = feature;
44559f45cbd3SKristen Carlson Accardi 
44562b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
44579f45cbd3SKristen Carlson Accardi 
44589f45cbd3SKristen Carlson Accardi 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
44599f45cbd3SKristen Carlson Accardi 	return err_mask;
44609f45cbd3SKristen Carlson Accardi }
44619f45cbd3SKristen Carlson Accardi 
44629f45cbd3SKristen Carlson Accardi /**
4463c6fd2807SJeff Garzik  *	ata_dev_init_params - Issue INIT DEV PARAMS command
4464c6fd2807SJeff Garzik  *	@dev: Device to which command will be sent
4465c6fd2807SJeff Garzik  *	@heads: Number of heads (taskfile parameter)
4466c6fd2807SJeff Garzik  *	@sectors: Number of sectors (taskfile parameter)
4467c6fd2807SJeff Garzik  *
4468c6fd2807SJeff Garzik  *	LOCKING:
4469c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
4470c6fd2807SJeff Garzik  *
4471c6fd2807SJeff Garzik  *	RETURNS:
4472c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
4473c6fd2807SJeff Garzik  */
4474c6fd2807SJeff Garzik static unsigned int ata_dev_init_params(struct ata_device *dev,
4475c6fd2807SJeff Garzik 					u16 heads, u16 sectors)
4476c6fd2807SJeff Garzik {
4477c6fd2807SJeff Garzik 	struct ata_taskfile tf;
4478c6fd2807SJeff Garzik 	unsigned int err_mask;
4479c6fd2807SJeff Garzik 
4480c6fd2807SJeff Garzik 	/* Number of sectors per track 1-255. Number of heads 1-16 */
4481c6fd2807SJeff Garzik 	if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
4482c6fd2807SJeff Garzik 		return AC_ERR_INVALID;
4483c6fd2807SJeff Garzik 
4484c6fd2807SJeff Garzik 	/* set up init dev params taskfile */
4485c6fd2807SJeff Garzik 	DPRINTK("init dev params \n");
4486c6fd2807SJeff Garzik 
4487c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
4488c6fd2807SJeff Garzik 	tf.command = ATA_CMD_INIT_DEV_PARAMS;
4489c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4490c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
4491c6fd2807SJeff Garzik 	tf.nsect = sectors;
4492c6fd2807SJeff Garzik 	tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
4493c6fd2807SJeff Garzik 
44942b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
449518b2466cSAlan Cox 	/* A clean abort indicates an original or just out of spec drive
449618b2466cSAlan Cox 	   and we should continue as we issue the setup based on the
449718b2466cSAlan Cox 	   drive reported working geometry */
449818b2466cSAlan Cox 	if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
449918b2466cSAlan Cox 		err_mask = 0;
4500c6fd2807SJeff Garzik 
4501c6fd2807SJeff Garzik 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
4502c6fd2807SJeff Garzik 	return err_mask;
4503c6fd2807SJeff Garzik }
4504c6fd2807SJeff Garzik 
4505c6fd2807SJeff Garzik /**
4506c6fd2807SJeff Garzik  *	ata_sg_clean - Unmap DMA memory associated with command
4507c6fd2807SJeff Garzik  *	@qc: Command containing DMA memory to be released
4508c6fd2807SJeff Garzik  *
4509c6fd2807SJeff Garzik  *	Unmap all mapped DMA memory associated with this command.
4510c6fd2807SJeff Garzik  *
4511c6fd2807SJeff Garzik  *	LOCKING:
4512cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4513c6fd2807SJeff Garzik  */
451470e6ad0cSTejun Heo void ata_sg_clean(struct ata_queued_cmd *qc)
4515c6fd2807SJeff Garzik {
4516c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4517ff2aeb1eSTejun Heo 	struct scatterlist *sg = qc->sg;
4518c6fd2807SJeff Garzik 	int dir = qc->dma_dir;
4519c6fd2807SJeff Garzik 
4520efcb3cf7STejun Heo 	WARN_ON_ONCE(sg == NULL);
4521c6fd2807SJeff Garzik 
4522dde20207SJames Bottomley 	VPRINTK("unmapping %u sg elements\n", qc->n_elem);
4523c6fd2807SJeff Garzik 
4524dde20207SJames Bottomley 	if (qc->n_elem)
45255825627cSFUJITA Tomonori 		dma_unmap_sg(ap->dev, sg, qc->orig_n_elem, dir);
4526c6fd2807SJeff Garzik 
4527c6fd2807SJeff Garzik 	qc->flags &= ~ATA_QCFLAG_DMAMAP;
4528ff2aeb1eSTejun Heo 	qc->sg = NULL;
4529c6fd2807SJeff Garzik }
4530c6fd2807SJeff Garzik 
4531c6fd2807SJeff Garzik /**
45325895ef9aSTejun Heo  *	atapi_check_dma - Check whether ATAPI DMA can be supported
4533c6fd2807SJeff Garzik  *	@qc: Metadata associated with taskfile to check
4534c6fd2807SJeff Garzik  *
4535c6fd2807SJeff Garzik  *	Allow low-level driver to filter ATA PACKET commands, returning
4536c6fd2807SJeff Garzik  *	a status indicating whether or not it is OK to use DMA for the
4537c6fd2807SJeff Garzik  *	supplied PACKET command.
4538c6fd2807SJeff Garzik  *
4539c6fd2807SJeff Garzik  *	LOCKING:
4540cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4541c6fd2807SJeff Garzik  *
4542c6fd2807SJeff Garzik  *	RETURNS: 0 when ATAPI DMA can be used
4543c6fd2807SJeff Garzik  *               nonzero otherwise
4544c6fd2807SJeff Garzik  */
45455895ef9aSTejun Heo int atapi_check_dma(struct ata_queued_cmd *qc)
4546c6fd2807SJeff Garzik {
4547c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4548c6fd2807SJeff Garzik 
4549b9a4197eSTejun Heo 	/* Don't allow DMA if it isn't multiple of 16 bytes.  Quite a
4550b9a4197eSTejun Heo 	 * few ATAPI devices choke on such DMA requests.
4551b9a4197eSTejun Heo 	 */
45526a87e42eSTejun Heo 	if (!(qc->dev->horkage & ATA_HORKAGE_ATAPI_MOD16_DMA) &&
45536a87e42eSTejun Heo 	    unlikely(qc->nbytes & 15))
45546f23a31dSAlbert Lee 		return 1;
45556f23a31dSAlbert Lee 
4556c6fd2807SJeff Garzik 	if (ap->ops->check_atapi_dma)
4557b9a4197eSTejun Heo 		return ap->ops->check_atapi_dma(qc);
4558c6fd2807SJeff Garzik 
4559b9a4197eSTejun Heo 	return 0;
4560c6fd2807SJeff Garzik }
4561b9a4197eSTejun Heo 
4562c6fd2807SJeff Garzik /**
456331cc23b3STejun Heo  *	ata_std_qc_defer - Check whether a qc needs to be deferred
456431cc23b3STejun Heo  *	@qc: ATA command in question
456531cc23b3STejun Heo  *
456631cc23b3STejun Heo  *	Non-NCQ commands cannot run with any other command, NCQ or
456731cc23b3STejun Heo  *	not.  As upper layer only knows the queue depth, we are
456831cc23b3STejun Heo  *	responsible for maintaining exclusion.  This function checks
456931cc23b3STejun Heo  *	whether a new command @qc can be issued.
457031cc23b3STejun Heo  *
457131cc23b3STejun Heo  *	LOCKING:
457231cc23b3STejun Heo  *	spin_lock_irqsave(host lock)
457331cc23b3STejun Heo  *
457431cc23b3STejun Heo  *	RETURNS:
457531cc23b3STejun Heo  *	ATA_DEFER_* if deferring is needed, 0 otherwise.
457631cc23b3STejun Heo  */
457731cc23b3STejun Heo int ata_std_qc_defer(struct ata_queued_cmd *qc)
457831cc23b3STejun Heo {
457931cc23b3STejun Heo 	struct ata_link *link = qc->dev->link;
458031cc23b3STejun Heo 
458131cc23b3STejun Heo 	if (qc->tf.protocol == ATA_PROT_NCQ) {
458231cc23b3STejun Heo 		if (!ata_tag_valid(link->active_tag))
458331cc23b3STejun Heo 			return 0;
458431cc23b3STejun Heo 	} else {
458531cc23b3STejun Heo 		if (!ata_tag_valid(link->active_tag) && !link->sactive)
458631cc23b3STejun Heo 			return 0;
458731cc23b3STejun Heo 	}
458831cc23b3STejun Heo 
458931cc23b3STejun Heo 	return ATA_DEFER_LINK;
459031cc23b3STejun Heo }
459131cc23b3STejun Heo 
4592c6fd2807SJeff Garzik void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
4593c6fd2807SJeff Garzik 
4594c6fd2807SJeff Garzik /**
4595c6fd2807SJeff Garzik  *	ata_sg_init - Associate command with scatter-gather table.
4596c6fd2807SJeff Garzik  *	@qc: Command to be associated
4597c6fd2807SJeff Garzik  *	@sg: Scatter-gather table.
4598c6fd2807SJeff Garzik  *	@n_elem: Number of elements in s/g table.
4599c6fd2807SJeff Garzik  *
4600c6fd2807SJeff Garzik  *	Initialize the data-related elements of queued_cmd @qc
4601c6fd2807SJeff Garzik  *	to point to a scatter-gather table @sg, containing @n_elem
4602c6fd2807SJeff Garzik  *	elements.
4603c6fd2807SJeff Garzik  *
4604c6fd2807SJeff Garzik  *	LOCKING:
4605cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4606c6fd2807SJeff Garzik  */
4607c6fd2807SJeff Garzik void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
4608c6fd2807SJeff Garzik 		 unsigned int n_elem)
4609c6fd2807SJeff Garzik {
4610ff2aeb1eSTejun Heo 	qc->sg = sg;
4611c6fd2807SJeff Garzik 	qc->n_elem = n_elem;
4612ff2aeb1eSTejun Heo 	qc->cursg = qc->sg;
4613ff2aeb1eSTejun Heo }
4614ff2aeb1eSTejun Heo 
4615c6fd2807SJeff Garzik /**
4616c6fd2807SJeff Garzik  *	ata_sg_setup - DMA-map the scatter-gather table associated with a command.
4617c6fd2807SJeff Garzik  *	@qc: Command with scatter-gather table to be mapped.
4618c6fd2807SJeff Garzik  *
4619c6fd2807SJeff Garzik  *	DMA-map the scatter-gather table associated with queued_cmd @qc.
4620c6fd2807SJeff Garzik  *
4621c6fd2807SJeff Garzik  *	LOCKING:
4622cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4623c6fd2807SJeff Garzik  *
4624c6fd2807SJeff Garzik  *	RETURNS:
4625c6fd2807SJeff Garzik  *	Zero on success, negative on error.
4626c6fd2807SJeff Garzik  *
4627c6fd2807SJeff Garzik  */
4628c6fd2807SJeff Garzik static int ata_sg_setup(struct ata_queued_cmd *qc)
4629c6fd2807SJeff Garzik {
4630c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4631dde20207SJames Bottomley 	unsigned int n_elem;
4632c6fd2807SJeff Garzik 
463344877b4eSTejun Heo 	VPRINTK("ENTER, ata%u\n", ap->print_id);
4634c6fd2807SJeff Garzik 
4635dde20207SJames Bottomley 	n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir);
4636dde20207SJames Bottomley 	if (n_elem < 1)
4637c6fd2807SJeff Garzik 		return -1;
4638c6fd2807SJeff Garzik 
4639dde20207SJames Bottomley 	DPRINTK("%d sg elements mapped\n", n_elem);
46405825627cSFUJITA Tomonori 	qc->orig_n_elem = qc->n_elem;
4641dde20207SJames Bottomley 	qc->n_elem = n_elem;
4642f92a2636STejun Heo 	qc->flags |= ATA_QCFLAG_DMAMAP;
4643c6fd2807SJeff Garzik 
4644c6fd2807SJeff Garzik 	return 0;
4645c6fd2807SJeff Garzik }
4646c6fd2807SJeff Garzik 
4647c6fd2807SJeff Garzik /**
4648c6fd2807SJeff Garzik  *	swap_buf_le16 - swap halves of 16-bit words in place
4649c6fd2807SJeff Garzik  *	@buf:  Buffer to swap
4650c6fd2807SJeff Garzik  *	@buf_words:  Number of 16-bit words in buffer.
4651c6fd2807SJeff Garzik  *
4652c6fd2807SJeff Garzik  *	Swap halves of 16-bit words if needed to convert from
4653c6fd2807SJeff Garzik  *	little-endian byte order to native cpu byte order, or
4654c6fd2807SJeff Garzik  *	vice-versa.
4655c6fd2807SJeff Garzik  *
4656c6fd2807SJeff Garzik  *	LOCKING:
4657c6fd2807SJeff Garzik  *	Inherited from caller.
4658c6fd2807SJeff Garzik  */
4659c6fd2807SJeff Garzik void swap_buf_le16(u16 *buf, unsigned int buf_words)
4660c6fd2807SJeff Garzik {
4661c6fd2807SJeff Garzik #ifdef __BIG_ENDIAN
4662c6fd2807SJeff Garzik 	unsigned int i;
4663c6fd2807SJeff Garzik 
4664c6fd2807SJeff Garzik 	for (i = 0; i < buf_words; i++)
4665c6fd2807SJeff Garzik 		buf[i] = le16_to_cpu(buf[i]);
4666c6fd2807SJeff Garzik #endif /* __BIG_ENDIAN */
4667c6fd2807SJeff Garzik }
4668c6fd2807SJeff Garzik 
4669c6fd2807SJeff Garzik /**
46708a8bc223STejun Heo  *	ata_qc_new - Request an available ATA command, for queueing
46715eb66fe0SRandy Dunlap  *	@ap: target port
46728a8bc223STejun Heo  *
46738a8bc223STejun Heo  *	LOCKING:
46748a8bc223STejun Heo  *	None.
46758a8bc223STejun Heo  */
46768a8bc223STejun Heo 
46778a8bc223STejun Heo static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
46788a8bc223STejun Heo {
46798a8bc223STejun Heo 	struct ata_queued_cmd *qc = NULL;
46808a8bc223STejun Heo 	unsigned int i;
46818a8bc223STejun Heo 
46828a8bc223STejun Heo 	/* no command while frozen */
46838a8bc223STejun Heo 	if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
46848a8bc223STejun Heo 		return NULL;
46858a8bc223STejun Heo 
46868a8bc223STejun Heo 	/* the last tag is reserved for internal command. */
46878a8bc223STejun Heo 	for (i = 0; i < ATA_MAX_QUEUE - 1; i++)
46888a8bc223STejun Heo 		if (!test_and_set_bit(i, &ap->qc_allocated)) {
46898a8bc223STejun Heo 			qc = __ata_qc_from_tag(ap, i);
46908a8bc223STejun Heo 			break;
46918a8bc223STejun Heo 		}
46928a8bc223STejun Heo 
46938a8bc223STejun Heo 	if (qc)
46948a8bc223STejun Heo 		qc->tag = i;
46958a8bc223STejun Heo 
46968a8bc223STejun Heo 	return qc;
46978a8bc223STejun Heo }
46988a8bc223STejun Heo 
46998a8bc223STejun Heo /**
4700c6fd2807SJeff Garzik  *	ata_qc_new_init - Request an available ATA command, and initialize it
4701c6fd2807SJeff Garzik  *	@dev: Device from whom we request an available command structure
4702c6fd2807SJeff Garzik  *
4703c6fd2807SJeff Garzik  *	LOCKING:
4704c6fd2807SJeff Garzik  *	None.
4705c6fd2807SJeff Garzik  */
4706c6fd2807SJeff Garzik 
47078a8bc223STejun Heo struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev)
4708c6fd2807SJeff Garzik {
47099af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
4710c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
4711c6fd2807SJeff Garzik 
47128a8bc223STejun Heo 	qc = ata_qc_new(ap);
4713c6fd2807SJeff Garzik 	if (qc) {
4714c6fd2807SJeff Garzik 		qc->scsicmd = NULL;
4715c6fd2807SJeff Garzik 		qc->ap = ap;
4716c6fd2807SJeff Garzik 		qc->dev = dev;
4717c6fd2807SJeff Garzik 
4718c6fd2807SJeff Garzik 		ata_qc_reinit(qc);
4719c6fd2807SJeff Garzik 	}
4720c6fd2807SJeff Garzik 
4721c6fd2807SJeff Garzik 	return qc;
4722c6fd2807SJeff Garzik }
4723c6fd2807SJeff Garzik 
47248a8bc223STejun Heo /**
47258a8bc223STejun Heo  *	ata_qc_free - free unused ata_queued_cmd
47268a8bc223STejun Heo  *	@qc: Command to complete
47278a8bc223STejun Heo  *
47288a8bc223STejun Heo  *	Designed to free unused ata_queued_cmd object
47298a8bc223STejun Heo  *	in case something prevents using it.
47308a8bc223STejun Heo  *
47318a8bc223STejun Heo  *	LOCKING:
47328a8bc223STejun Heo  *	spin_lock_irqsave(host lock)
47338a8bc223STejun Heo  */
47348a8bc223STejun Heo void ata_qc_free(struct ata_queued_cmd *qc)
47358a8bc223STejun Heo {
4736a1104016SJulia Lawall 	struct ata_port *ap;
47378a8bc223STejun Heo 	unsigned int tag;
47388a8bc223STejun Heo 
4739efcb3cf7STejun Heo 	WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4740a1104016SJulia Lawall 	ap = qc->ap;
47418a8bc223STejun Heo 
47428a8bc223STejun Heo 	qc->flags = 0;
47438a8bc223STejun Heo 	tag = qc->tag;
47448a8bc223STejun Heo 	if (likely(ata_tag_valid(tag))) {
47458a8bc223STejun Heo 		qc->tag = ATA_TAG_POISON;
47468a8bc223STejun Heo 		clear_bit(tag, &ap->qc_allocated);
47478a8bc223STejun Heo 	}
47488a8bc223STejun Heo }
47498a8bc223STejun Heo 
4750c6fd2807SJeff Garzik void __ata_qc_complete(struct ata_queued_cmd *qc)
4751c6fd2807SJeff Garzik {
4752a1104016SJulia Lawall 	struct ata_port *ap;
4753a1104016SJulia Lawall 	struct ata_link *link;
4754c6fd2807SJeff Garzik 
4755efcb3cf7STejun Heo 	WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4756efcb3cf7STejun Heo 	WARN_ON_ONCE(!(qc->flags & ATA_QCFLAG_ACTIVE));
4757a1104016SJulia Lawall 	ap = qc->ap;
4758a1104016SJulia Lawall 	link = qc->dev->link;
4759c6fd2807SJeff Garzik 
4760c6fd2807SJeff Garzik 	if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
4761c6fd2807SJeff Garzik 		ata_sg_clean(qc);
4762c6fd2807SJeff Garzik 
4763c6fd2807SJeff Garzik 	/* command should be marked inactive atomically with qc completion */
4764da917d69STejun Heo 	if (qc->tf.protocol == ATA_PROT_NCQ) {
47659af5c9c9STejun Heo 		link->sactive &= ~(1 << qc->tag);
4766da917d69STejun Heo 		if (!link->sactive)
4767da917d69STejun Heo 			ap->nr_active_links--;
4768da917d69STejun Heo 	} else {
47699af5c9c9STejun Heo 		link->active_tag = ATA_TAG_POISON;
4770da917d69STejun Heo 		ap->nr_active_links--;
4771da917d69STejun Heo 	}
4772da917d69STejun Heo 
4773da917d69STejun Heo 	/* clear exclusive status */
4774da917d69STejun Heo 	if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
4775da917d69STejun Heo 		     ap->excl_link == link))
4776da917d69STejun Heo 		ap->excl_link = NULL;
4777c6fd2807SJeff Garzik 
4778c6fd2807SJeff Garzik 	/* atapi: mark qc as inactive to prevent the interrupt handler
4779c6fd2807SJeff Garzik 	 * from completing the command twice later, before the error handler
4780c6fd2807SJeff Garzik 	 * is called. (when rc != 0 and atapi request sense is needed)
4781c6fd2807SJeff Garzik 	 */
4782c6fd2807SJeff Garzik 	qc->flags &= ~ATA_QCFLAG_ACTIVE;
4783c6fd2807SJeff Garzik 	ap->qc_active &= ~(1 << qc->tag);
4784c6fd2807SJeff Garzik 
4785c6fd2807SJeff Garzik 	/* call completion callback */
4786c6fd2807SJeff Garzik 	qc->complete_fn(qc);
4787c6fd2807SJeff Garzik }
4788c6fd2807SJeff Garzik 
478939599a53STejun Heo static void fill_result_tf(struct ata_queued_cmd *qc)
479039599a53STejun Heo {
479139599a53STejun Heo 	struct ata_port *ap = qc->ap;
479239599a53STejun Heo 
479339599a53STejun Heo 	qc->result_tf.flags = qc->tf.flags;
479422183bf5STejun Heo 	ap->ops->qc_fill_rtf(qc);
479539599a53STejun Heo }
479639599a53STejun Heo 
479700115e0fSTejun Heo static void ata_verify_xfer(struct ata_queued_cmd *qc)
479800115e0fSTejun Heo {
479900115e0fSTejun Heo 	struct ata_device *dev = qc->dev;
480000115e0fSTejun Heo 
480100115e0fSTejun Heo 	if (ata_is_nodata(qc->tf.protocol))
480200115e0fSTejun Heo 		return;
480300115e0fSTejun Heo 
480400115e0fSTejun Heo 	if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
480500115e0fSTejun Heo 		return;
480600115e0fSTejun Heo 
480700115e0fSTejun Heo 	dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
480800115e0fSTejun Heo }
480900115e0fSTejun Heo 
4810c6fd2807SJeff Garzik /**
4811c6fd2807SJeff Garzik  *	ata_qc_complete - Complete an active ATA command
4812c6fd2807SJeff Garzik  *	@qc: Command to complete
4813c6fd2807SJeff Garzik  *
48141aadf5c3STejun Heo  *	Indicate to the mid and upper layers that an ATA command has
48151aadf5c3STejun Heo  *	completed, with either an ok or not-ok status.
48161aadf5c3STejun Heo  *
48171aadf5c3STejun Heo  *	Refrain from calling this function multiple times when
48181aadf5c3STejun Heo  *	successfully completing multiple NCQ commands.
48191aadf5c3STejun Heo  *	ata_qc_complete_multiple() should be used instead, which will
48201aadf5c3STejun Heo  *	properly update IRQ expect state.
4821c6fd2807SJeff Garzik  *
4822c6fd2807SJeff Garzik  *	LOCKING:
4823cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4824c6fd2807SJeff Garzik  */
4825c6fd2807SJeff Garzik void ata_qc_complete(struct ata_queued_cmd *qc)
4826c6fd2807SJeff Garzik {
4827c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4828c6fd2807SJeff Garzik 
4829c6fd2807SJeff Garzik 	/* XXX: New EH and old EH use different mechanisms to
4830c6fd2807SJeff Garzik 	 * synchronize EH with regular execution path.
4831c6fd2807SJeff Garzik 	 *
4832c6fd2807SJeff Garzik 	 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
4833c6fd2807SJeff Garzik 	 * Normal execution path is responsible for not accessing a
4834c6fd2807SJeff Garzik 	 * failed qc.  libata core enforces the rule by returning NULL
4835c6fd2807SJeff Garzik 	 * from ata_qc_from_tag() for failed qcs.
4836c6fd2807SJeff Garzik 	 *
4837c6fd2807SJeff Garzik 	 * Old EH depends on ata_qc_complete() nullifying completion
4838c6fd2807SJeff Garzik 	 * requests if ATA_QCFLAG_EH_SCHEDULED is set.  Old EH does
4839c6fd2807SJeff Garzik 	 * not synchronize with interrupt handler.  Only PIO task is
4840c6fd2807SJeff Garzik 	 * taken care of.
4841c6fd2807SJeff Garzik 	 */
4842c6fd2807SJeff Garzik 	if (ap->ops->error_handler) {
48434dbfa39bSTejun Heo 		struct ata_device *dev = qc->dev;
48444dbfa39bSTejun Heo 		struct ata_eh_info *ehi = &dev->link->eh_info;
48454dbfa39bSTejun Heo 
4846c6fd2807SJeff Garzik 		if (unlikely(qc->err_mask))
4847c6fd2807SJeff Garzik 			qc->flags |= ATA_QCFLAG_FAILED;
4848c6fd2807SJeff Garzik 
4849f08dc1acSTejun Heo 		/*
4850f08dc1acSTejun Heo 		 * Finish internal commands without any further processing
4851f08dc1acSTejun Heo 		 * and always with the result TF filled.
4852f08dc1acSTejun Heo 		 */
4853f08dc1acSTejun Heo 		if (unlikely(ata_tag_internal(qc->tag))) {
485439599a53STejun Heo 			fill_result_tf(qc);
4855f4b31db9STejun Heo 			__ata_qc_complete(qc);
4856c6fd2807SJeff Garzik 			return;
4857c6fd2807SJeff Garzik 		}
4858c6fd2807SJeff Garzik 
4859f08dc1acSTejun Heo 		/*
4860f08dc1acSTejun Heo 		 * Non-internal qc has failed.  Fill the result TF and
4861f08dc1acSTejun Heo 		 * summon EH.
4862f08dc1acSTejun Heo 		 */
4863f08dc1acSTejun Heo 		if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
4864f08dc1acSTejun Heo 			fill_result_tf(qc);
4865f08dc1acSTejun Heo 			ata_qc_schedule_eh(qc);
4866f08dc1acSTejun Heo 			return;
4867f08dc1acSTejun Heo 		}
4868f08dc1acSTejun Heo 
48694dc738edSTejun Heo 		WARN_ON_ONCE(ap->pflags & ATA_PFLAG_FROZEN);
48704dc738edSTejun Heo 
4871c6fd2807SJeff Garzik 		/* read result TF if requested */
4872c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_RESULT_TF)
487339599a53STejun Heo 			fill_result_tf(qc);
4874c6fd2807SJeff Garzik 
48754dbfa39bSTejun Heo 		/* Some commands need post-processing after successful
48764dbfa39bSTejun Heo 		 * completion.
48774dbfa39bSTejun Heo 		 */
48784dbfa39bSTejun Heo 		switch (qc->tf.command) {
48794dbfa39bSTejun Heo 		case ATA_CMD_SET_FEATURES:
48804dbfa39bSTejun Heo 			if (qc->tf.feature != SETFEATURES_WC_ON &&
48814dbfa39bSTejun Heo 			    qc->tf.feature != SETFEATURES_WC_OFF)
48824dbfa39bSTejun Heo 				break;
48834dbfa39bSTejun Heo 			/* fall through */
48844dbfa39bSTejun Heo 		case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
48854dbfa39bSTejun Heo 		case ATA_CMD_SET_MULTI: /* multi_count changed */
48864dbfa39bSTejun Heo 			/* revalidate device */
48874dbfa39bSTejun Heo 			ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE;
48884dbfa39bSTejun Heo 			ata_port_schedule_eh(ap);
48894dbfa39bSTejun Heo 			break;
4890054a5fbaSTejun Heo 
4891054a5fbaSTejun Heo 		case ATA_CMD_SLEEP:
4892054a5fbaSTejun Heo 			dev->flags |= ATA_DFLAG_SLEEPING;
4893054a5fbaSTejun Heo 			break;
48944dbfa39bSTejun Heo 		}
48954dbfa39bSTejun Heo 
489600115e0fSTejun Heo 		if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
489700115e0fSTejun Heo 			ata_verify_xfer(qc);
489800115e0fSTejun Heo 
4899c6fd2807SJeff Garzik 		__ata_qc_complete(qc);
4900c6fd2807SJeff Garzik 	} else {
4901c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
4902c6fd2807SJeff Garzik 			return;
4903c6fd2807SJeff Garzik 
4904c6fd2807SJeff Garzik 		/* read result TF if failed or requested */
4905c6fd2807SJeff Garzik 		if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
490639599a53STejun Heo 			fill_result_tf(qc);
4907c6fd2807SJeff Garzik 
4908c6fd2807SJeff Garzik 		__ata_qc_complete(qc);
4909c6fd2807SJeff Garzik 	}
4910c6fd2807SJeff Garzik }
4911c6fd2807SJeff Garzik 
4912c6fd2807SJeff Garzik /**
4913c6fd2807SJeff Garzik  *	ata_qc_complete_multiple - Complete multiple qcs successfully
4914c6fd2807SJeff Garzik  *	@ap: port in question
4915c6fd2807SJeff Garzik  *	@qc_active: new qc_active mask
4916c6fd2807SJeff Garzik  *
4917c6fd2807SJeff Garzik  *	Complete in-flight commands.  This functions is meant to be
4918c6fd2807SJeff Garzik  *	called from low-level driver's interrupt routine to complete
4919c6fd2807SJeff Garzik  *	requests normally.  ap->qc_active and @qc_active is compared
4920c6fd2807SJeff Garzik  *	and commands are completed accordingly.
4921c6fd2807SJeff Garzik  *
49221aadf5c3STejun Heo  *	Always use this function when completing multiple NCQ commands
49231aadf5c3STejun Heo  *	from IRQ handlers instead of calling ata_qc_complete()
49241aadf5c3STejun Heo  *	multiple times to keep IRQ expect status properly in sync.
49251aadf5c3STejun Heo  *
4926c6fd2807SJeff Garzik  *	LOCKING:
4927cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4928c6fd2807SJeff Garzik  *
4929c6fd2807SJeff Garzik  *	RETURNS:
4930c6fd2807SJeff Garzik  *	Number of completed commands on success, -errno otherwise.
4931c6fd2807SJeff Garzik  */
493279f97dadSTejun Heo int ata_qc_complete_multiple(struct ata_port *ap, u32 qc_active)
4933c6fd2807SJeff Garzik {
4934c6fd2807SJeff Garzik 	int nr_done = 0;
4935c6fd2807SJeff Garzik 	u32 done_mask;
4936c6fd2807SJeff Garzik 
4937c6fd2807SJeff Garzik 	done_mask = ap->qc_active ^ qc_active;
4938c6fd2807SJeff Garzik 
4939c6fd2807SJeff Garzik 	if (unlikely(done_mask & qc_active)) {
4940a9a79dfeSJoe Perches 		ata_port_err(ap, "illegal qc_active transition (%08x->%08x)\n",
4941a9a79dfeSJoe Perches 			     ap->qc_active, qc_active);
4942c6fd2807SJeff Garzik 		return -EINVAL;
4943c6fd2807SJeff Garzik 	}
4944c6fd2807SJeff Garzik 
494543768180SJens Axboe 	while (done_mask) {
4946c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc;
494743768180SJens Axboe 		unsigned int tag = __ffs(done_mask);
4948c6fd2807SJeff Garzik 
494943768180SJens Axboe 		qc = ata_qc_from_tag(ap, tag);
495043768180SJens Axboe 		if (qc) {
4951c6fd2807SJeff Garzik 			ata_qc_complete(qc);
4952c6fd2807SJeff Garzik 			nr_done++;
4953c6fd2807SJeff Garzik 		}
495443768180SJens Axboe 		done_mask &= ~(1 << tag);
4955c6fd2807SJeff Garzik 	}
4956c6fd2807SJeff Garzik 
4957c6fd2807SJeff Garzik 	return nr_done;
4958c6fd2807SJeff Garzik }
4959c6fd2807SJeff Garzik 
4960c6fd2807SJeff Garzik /**
4961c6fd2807SJeff Garzik  *	ata_qc_issue - issue taskfile to device
4962c6fd2807SJeff Garzik  *	@qc: command to issue to device
4963c6fd2807SJeff Garzik  *
4964c6fd2807SJeff Garzik  *	Prepare an ATA command to submission to device.
4965c6fd2807SJeff Garzik  *	This includes mapping the data into a DMA-able
4966c6fd2807SJeff Garzik  *	area, filling in the S/G table, and finally
4967c6fd2807SJeff Garzik  *	writing the taskfile to hardware, starting the command.
4968c6fd2807SJeff Garzik  *
4969c6fd2807SJeff Garzik  *	LOCKING:
4970cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4971c6fd2807SJeff Garzik  */
4972c6fd2807SJeff Garzik void ata_qc_issue(struct ata_queued_cmd *qc)
4973c6fd2807SJeff Garzik {
4974c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
49759af5c9c9STejun Heo 	struct ata_link *link = qc->dev->link;
4976405e66b3STejun Heo 	u8 prot = qc->tf.protocol;
4977c6fd2807SJeff Garzik 
4978c6fd2807SJeff Garzik 	/* Make sure only one non-NCQ command is outstanding.  The
4979c6fd2807SJeff Garzik 	 * check is skipped for old EH because it reuses active qc to
4980c6fd2807SJeff Garzik 	 * request ATAPI sense.
4981c6fd2807SJeff Garzik 	 */
4982efcb3cf7STejun Heo 	WARN_ON_ONCE(ap->ops->error_handler && ata_tag_valid(link->active_tag));
4983c6fd2807SJeff Garzik 
49841973a023STejun Heo 	if (ata_is_ncq(prot)) {
4985efcb3cf7STejun Heo 		WARN_ON_ONCE(link->sactive & (1 << qc->tag));
4986da917d69STejun Heo 
4987da917d69STejun Heo 		if (!link->sactive)
4988da917d69STejun Heo 			ap->nr_active_links++;
49899af5c9c9STejun Heo 		link->sactive |= 1 << qc->tag;
4990c6fd2807SJeff Garzik 	} else {
4991efcb3cf7STejun Heo 		WARN_ON_ONCE(link->sactive);
4992da917d69STejun Heo 
4993da917d69STejun Heo 		ap->nr_active_links++;
49949af5c9c9STejun Heo 		link->active_tag = qc->tag;
4995c6fd2807SJeff Garzik 	}
4996c6fd2807SJeff Garzik 
4997c6fd2807SJeff Garzik 	qc->flags |= ATA_QCFLAG_ACTIVE;
4998c6fd2807SJeff Garzik 	ap->qc_active |= 1 << qc->tag;
4999c6fd2807SJeff Garzik 
500060f5d6efSTejun Heo 	/*
500160f5d6efSTejun Heo 	 * We guarantee to LLDs that they will have at least one
5002f92a2636STejun Heo 	 * non-zero sg if the command is a data command.
5003f92a2636STejun Heo 	 */
500460f5d6efSTejun Heo 	if (WARN_ON_ONCE(ata_is_data(prot) &&
500560f5d6efSTejun Heo 			 (!qc->sg || !qc->n_elem || !qc->nbytes)))
500660f5d6efSTejun Heo 		goto sys_err;
5007f92a2636STejun Heo 
5008405e66b3STejun Heo 	if (ata_is_dma(prot) || (ata_is_pio(prot) &&
5009f92a2636STejun Heo 				 (ap->flags & ATA_FLAG_PIO_DMA)))
5010c6fd2807SJeff Garzik 		if (ata_sg_setup(qc))
501160f5d6efSTejun Heo 			goto sys_err;
5012c6fd2807SJeff Garzik 
5013cf480626STejun Heo 	/* if device is sleeping, schedule reset and abort the link */
5014054a5fbaSTejun Heo 	if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
5015cf480626STejun Heo 		link->eh_info.action |= ATA_EH_RESET;
5016054a5fbaSTejun Heo 		ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
5017054a5fbaSTejun Heo 		ata_link_abort(link);
5018054a5fbaSTejun Heo 		return;
5019054a5fbaSTejun Heo 	}
5020054a5fbaSTejun Heo 
5021c6fd2807SJeff Garzik 	ap->ops->qc_prep(qc);
5022c6fd2807SJeff Garzik 
5023c6fd2807SJeff Garzik 	qc->err_mask |= ap->ops->qc_issue(qc);
5024c6fd2807SJeff Garzik 	if (unlikely(qc->err_mask))
5025c6fd2807SJeff Garzik 		goto err;
5026c6fd2807SJeff Garzik 	return;
5027c6fd2807SJeff Garzik 
502860f5d6efSTejun Heo sys_err:
5029c6fd2807SJeff Garzik 	qc->err_mask |= AC_ERR_SYSTEM;
5030c6fd2807SJeff Garzik err:
5031c6fd2807SJeff Garzik 	ata_qc_complete(qc);
5032c6fd2807SJeff Garzik }
5033c6fd2807SJeff Garzik 
5034c6fd2807SJeff Garzik /**
5035c6fd2807SJeff Garzik  *	sata_scr_valid - test whether SCRs are accessible
5036936fd732STejun Heo  *	@link: ATA link to test SCR accessibility for
5037c6fd2807SJeff Garzik  *
5038936fd732STejun Heo  *	Test whether SCRs are accessible for @link.
5039c6fd2807SJeff Garzik  *
5040c6fd2807SJeff Garzik  *	LOCKING:
5041c6fd2807SJeff Garzik  *	None.
5042c6fd2807SJeff Garzik  *
5043c6fd2807SJeff Garzik  *	RETURNS:
5044c6fd2807SJeff Garzik  *	1 if SCRs are accessible, 0 otherwise.
5045c6fd2807SJeff Garzik  */
5046936fd732STejun Heo int sata_scr_valid(struct ata_link *link)
5047c6fd2807SJeff Garzik {
5048936fd732STejun Heo 	struct ata_port *ap = link->ap;
5049936fd732STejun Heo 
5050a16abc0bSTejun Heo 	return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
5051c6fd2807SJeff Garzik }
5052c6fd2807SJeff Garzik 
5053c6fd2807SJeff Garzik /**
5054c6fd2807SJeff Garzik  *	sata_scr_read - read SCR register of the specified port
5055936fd732STejun Heo  *	@link: ATA link to read SCR for
5056c6fd2807SJeff Garzik  *	@reg: SCR to read
5057c6fd2807SJeff Garzik  *	@val: Place to store read value
5058c6fd2807SJeff Garzik  *
5059936fd732STejun Heo  *	Read SCR register @reg of @link into *@val.  This function is
5060633273a3STejun Heo  *	guaranteed to succeed if @link is ap->link, the cable type of
5061633273a3STejun Heo  *	the port is SATA and the port implements ->scr_read.
5062c6fd2807SJeff Garzik  *
5063c6fd2807SJeff Garzik  *	LOCKING:
5064633273a3STejun Heo  *	None if @link is ap->link.  Kernel thread context otherwise.
5065c6fd2807SJeff Garzik  *
5066c6fd2807SJeff Garzik  *	RETURNS:
5067c6fd2807SJeff Garzik  *	0 on success, negative errno on failure.
5068c6fd2807SJeff Garzik  */
5069936fd732STejun Heo int sata_scr_read(struct ata_link *link, int reg, u32 *val)
5070c6fd2807SJeff Garzik {
5071633273a3STejun Heo 	if (ata_is_host_link(link)) {
5072936fd732STejun Heo 		if (sata_scr_valid(link))
507382ef04fbSTejun Heo 			return link->ap->ops->scr_read(link, reg, val);
5074c6fd2807SJeff Garzik 		return -EOPNOTSUPP;
5075c6fd2807SJeff Garzik 	}
5076c6fd2807SJeff Garzik 
5077633273a3STejun Heo 	return sata_pmp_scr_read(link, reg, val);
5078633273a3STejun Heo }
5079633273a3STejun Heo 
5080c6fd2807SJeff Garzik /**
5081c6fd2807SJeff Garzik  *	sata_scr_write - write SCR register of the specified port
5082936fd732STejun Heo  *	@link: ATA link to write SCR for
5083c6fd2807SJeff Garzik  *	@reg: SCR to write
5084c6fd2807SJeff Garzik  *	@val: value to write
5085c6fd2807SJeff Garzik  *
5086936fd732STejun Heo  *	Write @val to SCR register @reg of @link.  This function is
5087633273a3STejun Heo  *	guaranteed to succeed if @link is ap->link, the cable type of
5088633273a3STejun Heo  *	the port is SATA and the port implements ->scr_read.
5089c6fd2807SJeff Garzik  *
5090c6fd2807SJeff Garzik  *	LOCKING:
5091633273a3STejun Heo  *	None if @link is ap->link.  Kernel thread context otherwise.
5092c6fd2807SJeff Garzik  *
5093c6fd2807SJeff Garzik  *	RETURNS:
5094c6fd2807SJeff Garzik  *	0 on success, negative errno on failure.
5095c6fd2807SJeff Garzik  */
5096936fd732STejun Heo int sata_scr_write(struct ata_link *link, int reg, u32 val)
5097c6fd2807SJeff Garzik {
5098633273a3STejun Heo 	if (ata_is_host_link(link)) {
5099936fd732STejun Heo 		if (sata_scr_valid(link))
510082ef04fbSTejun Heo 			return link->ap->ops->scr_write(link, reg, val);
5101c6fd2807SJeff Garzik 		return -EOPNOTSUPP;
5102c6fd2807SJeff Garzik 	}
5103c6fd2807SJeff Garzik 
5104633273a3STejun Heo 	return sata_pmp_scr_write(link, reg, val);
5105633273a3STejun Heo }
5106633273a3STejun Heo 
5107c6fd2807SJeff Garzik /**
5108c6fd2807SJeff Garzik  *	sata_scr_write_flush - write SCR register of the specified port and flush
5109936fd732STejun Heo  *	@link: ATA link to write SCR for
5110c6fd2807SJeff Garzik  *	@reg: SCR to write
5111c6fd2807SJeff Garzik  *	@val: value to write
5112c6fd2807SJeff Garzik  *
5113c6fd2807SJeff Garzik  *	This function is identical to sata_scr_write() except that this
5114c6fd2807SJeff Garzik  *	function performs flush after writing to the register.
5115c6fd2807SJeff Garzik  *
5116c6fd2807SJeff Garzik  *	LOCKING:
5117633273a3STejun Heo  *	None if @link is ap->link.  Kernel thread context otherwise.
5118c6fd2807SJeff Garzik  *
5119c6fd2807SJeff Garzik  *	RETURNS:
5120c6fd2807SJeff Garzik  *	0 on success, negative errno on failure.
5121c6fd2807SJeff Garzik  */
5122936fd732STejun Heo int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
5123c6fd2807SJeff Garzik {
5124633273a3STejun Heo 	if (ata_is_host_link(link)) {
5125da3dbb17STejun Heo 		int rc;
5126da3dbb17STejun Heo 
5127936fd732STejun Heo 		if (sata_scr_valid(link)) {
512882ef04fbSTejun Heo 			rc = link->ap->ops->scr_write(link, reg, val);
5129da3dbb17STejun Heo 			if (rc == 0)
513082ef04fbSTejun Heo 				rc = link->ap->ops->scr_read(link, reg, &val);
5131da3dbb17STejun Heo 			return rc;
5132c6fd2807SJeff Garzik 		}
5133c6fd2807SJeff Garzik 		return -EOPNOTSUPP;
5134c6fd2807SJeff Garzik 	}
5135c6fd2807SJeff Garzik 
5136633273a3STejun Heo 	return sata_pmp_scr_write(link, reg, val);
5137633273a3STejun Heo }
5138633273a3STejun Heo 
5139c6fd2807SJeff Garzik /**
5140b1c72916STejun Heo  *	ata_phys_link_online - test whether the given link is online
5141936fd732STejun Heo  *	@link: ATA link to test
5142c6fd2807SJeff Garzik  *
5143936fd732STejun Heo  *	Test whether @link is online.  Note that this function returns
5144936fd732STejun Heo  *	0 if online status of @link cannot be obtained, so
5145936fd732STejun Heo  *	ata_link_online(link) != !ata_link_offline(link).
5146c6fd2807SJeff Garzik  *
5147c6fd2807SJeff Garzik  *	LOCKING:
5148c6fd2807SJeff Garzik  *	None.
5149c6fd2807SJeff Garzik  *
5150c6fd2807SJeff Garzik  *	RETURNS:
5151b5b3fa38STejun Heo  *	True if the port online status is available and online.
5152c6fd2807SJeff Garzik  */
5153b1c72916STejun Heo bool ata_phys_link_online(struct ata_link *link)
5154c6fd2807SJeff Garzik {
5155c6fd2807SJeff Garzik 	u32 sstatus;
5156c6fd2807SJeff Garzik 
5157936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
51589913ff8aSTejun Heo 	    ata_sstatus_online(sstatus))
5159b5b3fa38STejun Heo 		return true;
5160b5b3fa38STejun Heo 	return false;
5161c6fd2807SJeff Garzik }
5162c6fd2807SJeff Garzik 
5163c6fd2807SJeff Garzik /**
5164b1c72916STejun Heo  *	ata_phys_link_offline - test whether the given link is offline
5165936fd732STejun Heo  *	@link: ATA link to test
5166c6fd2807SJeff Garzik  *
5167936fd732STejun Heo  *	Test whether @link is offline.  Note that this function
5168936fd732STejun Heo  *	returns 0 if offline status of @link cannot be obtained, so
5169936fd732STejun Heo  *	ata_link_online(link) != !ata_link_offline(link).
5170c6fd2807SJeff Garzik  *
5171c6fd2807SJeff Garzik  *	LOCKING:
5172c6fd2807SJeff Garzik  *	None.
5173c6fd2807SJeff Garzik  *
5174c6fd2807SJeff Garzik  *	RETURNS:
5175b5b3fa38STejun Heo  *	True if the port offline status is available and offline.
5176c6fd2807SJeff Garzik  */
5177b1c72916STejun Heo bool ata_phys_link_offline(struct ata_link *link)
5178c6fd2807SJeff Garzik {
5179c6fd2807SJeff Garzik 	u32 sstatus;
5180c6fd2807SJeff Garzik 
5181936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
51829913ff8aSTejun Heo 	    !ata_sstatus_online(sstatus))
5183b5b3fa38STejun Heo 		return true;
5184b5b3fa38STejun Heo 	return false;
5185c6fd2807SJeff Garzik }
5186c6fd2807SJeff Garzik 
5187b1c72916STejun Heo /**
5188b1c72916STejun Heo  *	ata_link_online - test whether the given link is online
5189b1c72916STejun Heo  *	@link: ATA link to test
5190b1c72916STejun Heo  *
5191b1c72916STejun Heo  *	Test whether @link is online.  This is identical to
5192b1c72916STejun Heo  *	ata_phys_link_online() when there's no slave link.  When
5193b1c72916STejun Heo  *	there's a slave link, this function should only be called on
5194b1c72916STejun Heo  *	the master link and will return true if any of M/S links is
5195b1c72916STejun Heo  *	online.
5196b1c72916STejun Heo  *
5197b1c72916STejun Heo  *	LOCKING:
5198b1c72916STejun Heo  *	None.
5199b1c72916STejun Heo  *
5200b1c72916STejun Heo  *	RETURNS:
5201b1c72916STejun Heo  *	True if the port online status is available and online.
5202b1c72916STejun Heo  */
5203b1c72916STejun Heo bool ata_link_online(struct ata_link *link)
5204b1c72916STejun Heo {
5205b1c72916STejun Heo 	struct ata_link *slave = link->ap->slave_link;
5206b1c72916STejun Heo 
5207b1c72916STejun Heo 	WARN_ON(link == slave);	/* shouldn't be called on slave link */
5208b1c72916STejun Heo 
5209b1c72916STejun Heo 	return ata_phys_link_online(link) ||
5210b1c72916STejun Heo 		(slave && ata_phys_link_online(slave));
5211b1c72916STejun Heo }
5212b1c72916STejun Heo 
5213b1c72916STejun Heo /**
5214b1c72916STejun Heo  *	ata_link_offline - test whether the given link is offline
5215b1c72916STejun Heo  *	@link: ATA link to test
5216b1c72916STejun Heo  *
5217b1c72916STejun Heo  *	Test whether @link is offline.  This is identical to
5218b1c72916STejun Heo  *	ata_phys_link_offline() when there's no slave link.  When
5219b1c72916STejun Heo  *	there's a slave link, this function should only be called on
5220b1c72916STejun Heo  *	the master link and will return true if both M/S links are
5221b1c72916STejun Heo  *	offline.
5222b1c72916STejun Heo  *
5223b1c72916STejun Heo  *	LOCKING:
5224b1c72916STejun Heo  *	None.
5225b1c72916STejun Heo  *
5226b1c72916STejun Heo  *	RETURNS:
5227b1c72916STejun Heo  *	True if the port offline status is available and offline.
5228b1c72916STejun Heo  */
5229b1c72916STejun Heo bool ata_link_offline(struct ata_link *link)
5230b1c72916STejun Heo {
5231b1c72916STejun Heo 	struct ata_link *slave = link->ap->slave_link;
5232b1c72916STejun Heo 
5233b1c72916STejun Heo 	WARN_ON(link == slave);	/* shouldn't be called on slave link */
5234b1c72916STejun Heo 
5235b1c72916STejun Heo 	return ata_phys_link_offline(link) &&
5236b1c72916STejun Heo 		(!slave || ata_phys_link_offline(slave));
5237b1c72916STejun Heo }
5238b1c72916STejun Heo 
52396ffa01d8STejun Heo #ifdef CONFIG_PM
52405ef41082SLin Ming static int ata_port_request_pm(struct ata_port *ap, pm_message_t mesg,
5241cca3974eSJeff Garzik 			       unsigned int action, unsigned int ehi_flags,
5242cca3974eSJeff Garzik 			       int wait)
5243c6fd2807SJeff Garzik {
5244e3667ebfSTejun Heo 	struct ata_link *link;
52455ef41082SLin Ming 	unsigned long flags;
52465ef41082SLin Ming 	int rc;
5247c6fd2807SJeff Garzik 
5248c6fd2807SJeff Garzik 	/* Previous resume operation might still be in
5249c6fd2807SJeff Garzik 	 * progress.  Wait for PM_PENDING to clear.
5250c6fd2807SJeff Garzik 	 */
5251c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_PM_PENDING) {
5252c6fd2807SJeff Garzik 		ata_port_wait_eh(ap);
5253c6fd2807SJeff Garzik 		WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5254c6fd2807SJeff Garzik 	}
5255c6fd2807SJeff Garzik 
5256c6fd2807SJeff Garzik 	/* request PM ops to EH */
5257c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
5258c6fd2807SJeff Garzik 
5259c6fd2807SJeff Garzik 	ap->pm_mesg = mesg;
5260c6fd2807SJeff Garzik 	if (wait) {
5261c6fd2807SJeff Garzik 		rc = 0;
5262c6fd2807SJeff Garzik 		ap->pm_result = &rc;
5263c6fd2807SJeff Garzik 	}
5264c6fd2807SJeff Garzik 
5265c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_PM_PENDING;
52661eca4365STejun Heo 	ata_for_each_link(link, ap, HOST_FIRST) {
5267e3667ebfSTejun Heo 		link->eh_info.action |= action;
5268e3667ebfSTejun Heo 		link->eh_info.flags |= ehi_flags;
5269e3667ebfSTejun Heo 	}
5270c6fd2807SJeff Garzik 
5271c6fd2807SJeff Garzik 	ata_port_schedule_eh(ap);
5272c6fd2807SJeff Garzik 
5273c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
5274c6fd2807SJeff Garzik 
5275c6fd2807SJeff Garzik 	/* wait and check result */
5276c6fd2807SJeff Garzik 	if (wait) {
5277c6fd2807SJeff Garzik 		ata_port_wait_eh(ap);
5278c6fd2807SJeff Garzik 		WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5279c6fd2807SJeff Garzik 	}
5280c6fd2807SJeff Garzik 
52815ef41082SLin Ming 	return rc;
5282c6fd2807SJeff Garzik }
5283c6fd2807SJeff Garzik 
52845ef41082SLin Ming #define to_ata_port(d) container_of(d, struct ata_port, tdev)
52855ef41082SLin Ming 
528633574d68SLin Ming static int ata_port_suspend_common(struct device *dev, pm_message_t mesg)
52875ef41082SLin Ming {
52885ef41082SLin Ming 	struct ata_port *ap = to_ata_port(dev);
528933574d68SLin Ming 	unsigned int ehi_flags = ATA_EHI_QUIET;
52905ef41082SLin Ming 	int rc;
52915ef41082SLin Ming 
529233574d68SLin Ming 	/*
529333574d68SLin Ming 	 * On some hardware, device fails to respond after spun down
529433574d68SLin Ming 	 * for suspend.  As the device won't be used before being
529533574d68SLin Ming 	 * resumed, we don't need to touch the device.  Ask EH to skip
529633574d68SLin Ming 	 * the usual stuff and proceed directly to suspend.
529733574d68SLin Ming 	 *
529833574d68SLin Ming 	 * http://thread.gmane.org/gmane.linux.ide/46764
529933574d68SLin Ming 	 */
530033574d68SLin Ming 	if (mesg.event == PM_EVENT_SUSPEND)
530133574d68SLin Ming 		ehi_flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_NO_RECOVERY;
530233574d68SLin Ming 
530333574d68SLin Ming 	rc = ata_port_request_pm(ap, mesg, 0, ehi_flags, 1);
53045ef41082SLin Ming 	return rc;
53055ef41082SLin Ming }
53065ef41082SLin Ming 
53075ef41082SLin Ming static int ata_port_suspend(struct device *dev)
53085ef41082SLin Ming {
53095ef41082SLin Ming 	if (pm_runtime_suspended(dev))
53105ef41082SLin Ming 		return 0;
53115ef41082SLin Ming 
531233574d68SLin Ming 	return ata_port_suspend_common(dev, PMSG_SUSPEND);
531333574d68SLin Ming }
531433574d68SLin Ming 
531533574d68SLin Ming static int ata_port_do_freeze(struct device *dev)
531633574d68SLin Ming {
531733574d68SLin Ming 	if (pm_runtime_suspended(dev))
531833574d68SLin Ming 		pm_runtime_resume(dev);
531933574d68SLin Ming 
532033574d68SLin Ming 	return ata_port_suspend_common(dev, PMSG_FREEZE);
532133574d68SLin Ming }
532233574d68SLin Ming 
532333574d68SLin Ming static int ata_port_poweroff(struct device *dev)
532433574d68SLin Ming {
532533574d68SLin Ming 	if (pm_runtime_suspended(dev))
532633574d68SLin Ming 		return 0;
532733574d68SLin Ming 
532833574d68SLin Ming 	return ata_port_suspend_common(dev, PMSG_HIBERNATE);
53295ef41082SLin Ming }
53305ef41082SLin Ming 
5331e90b1e5aSLin Ming static int ata_port_resume_common(struct device *dev)
53325ef41082SLin Ming {
53335ef41082SLin Ming 	struct ata_port *ap = to_ata_port(dev);
53345ef41082SLin Ming 	int rc;
53355ef41082SLin Ming 
53365ef41082SLin Ming 	rc = ata_port_request_pm(ap, PMSG_ON, ATA_EH_RESET,
53375ef41082SLin Ming 		ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET, 1);
53385ef41082SLin Ming 	return rc;
53395ef41082SLin Ming }
53405ef41082SLin Ming 
5341e90b1e5aSLin Ming static int ata_port_resume(struct device *dev)
5342e90b1e5aSLin Ming {
5343e90b1e5aSLin Ming 	int rc;
5344e90b1e5aSLin Ming 
5345e90b1e5aSLin Ming 	rc = ata_port_resume_common(dev);
5346e90b1e5aSLin Ming 	if (!rc) {
5347e90b1e5aSLin Ming 		pm_runtime_disable(dev);
5348e90b1e5aSLin Ming 		pm_runtime_set_active(dev);
5349e90b1e5aSLin Ming 		pm_runtime_enable(dev);
5350e90b1e5aSLin Ming 	}
5351e90b1e5aSLin Ming 
5352e90b1e5aSLin Ming 	return rc;
5353e90b1e5aSLin Ming }
5354e90b1e5aSLin Ming 
53559ee4f393SLin Ming static int ata_port_runtime_idle(struct device *dev)
53569ee4f393SLin Ming {
53579ee4f393SLin Ming 	return pm_runtime_suspend(dev);
53589ee4f393SLin Ming }
53599ee4f393SLin Ming 
53605ef41082SLin Ming static const struct dev_pm_ops ata_port_pm_ops = {
53615ef41082SLin Ming 	.suspend = ata_port_suspend,
53625ef41082SLin Ming 	.resume = ata_port_resume,
536333574d68SLin Ming 	.freeze = ata_port_do_freeze,
536433574d68SLin Ming 	.thaw = ata_port_resume,
536533574d68SLin Ming 	.poweroff = ata_port_poweroff,
536633574d68SLin Ming 	.restore = ata_port_resume,
53679ee4f393SLin Ming 
536833574d68SLin Ming 	.runtime_suspend = ata_port_suspend,
5369e90b1e5aSLin Ming 	.runtime_resume = ata_port_resume_common,
53709ee4f393SLin Ming 	.runtime_idle = ata_port_runtime_idle,
53715ef41082SLin Ming };
53725ef41082SLin Ming 
5373c6fd2807SJeff Garzik /**
5374cca3974eSJeff Garzik  *	ata_host_suspend - suspend host
5375cca3974eSJeff Garzik  *	@host: host to suspend
5376c6fd2807SJeff Garzik  *	@mesg: PM message
5377c6fd2807SJeff Garzik  *
53785ef41082SLin Ming  *	Suspend @host.  Actual operation is performed by port suspend.
5379c6fd2807SJeff Garzik  */
5380cca3974eSJeff Garzik int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
5381c6fd2807SJeff Garzik {
538272ad6ec4SJeff Garzik 	host->dev->power.power_state = mesg;
53835ef41082SLin Ming 	return 0;
5384c6fd2807SJeff Garzik }
5385c6fd2807SJeff Garzik 
5386c6fd2807SJeff Garzik /**
5387cca3974eSJeff Garzik  *	ata_host_resume - resume host
5388cca3974eSJeff Garzik  *	@host: host to resume
5389c6fd2807SJeff Garzik  *
53905ef41082SLin Ming  *	Resume @host.  Actual operation is performed by port resume.
5391c6fd2807SJeff Garzik  */
5392cca3974eSJeff Garzik void ata_host_resume(struct ata_host *host)
5393c6fd2807SJeff Garzik {
539472ad6ec4SJeff Garzik 	host->dev->power.power_state = PMSG_ON;
5395c6fd2807SJeff Garzik }
53966ffa01d8STejun Heo #endif
5397c6fd2807SJeff Garzik 
53985ef41082SLin Ming struct device_type ata_port_type = {
53995ef41082SLin Ming 	.name = "ata_port",
54005ef41082SLin Ming #ifdef CONFIG_PM
54015ef41082SLin Ming 	.pm = &ata_port_pm_ops,
54025ef41082SLin Ming #endif
54035ef41082SLin Ming };
54045ef41082SLin Ming 
5405c6fd2807SJeff Garzik /**
5406c6fd2807SJeff Garzik  *	ata_dev_init - Initialize an ata_device structure
5407c6fd2807SJeff Garzik  *	@dev: Device structure to initialize
5408c6fd2807SJeff Garzik  *
5409c6fd2807SJeff Garzik  *	Initialize @dev in preparation for probing.
5410c6fd2807SJeff Garzik  *
5411c6fd2807SJeff Garzik  *	LOCKING:
5412c6fd2807SJeff Garzik  *	Inherited from caller.
5413c6fd2807SJeff Garzik  */
5414c6fd2807SJeff Garzik void ata_dev_init(struct ata_device *dev)
5415c6fd2807SJeff Garzik {
5416b1c72916STejun Heo 	struct ata_link *link = ata_dev_phys_link(dev);
54179af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
5418c6fd2807SJeff Garzik 	unsigned long flags;
5419c6fd2807SJeff Garzik 
5420b1c72916STejun Heo 	/* SATA spd limit is bound to the attached device, reset together */
54219af5c9c9STejun Heo 	link->sata_spd_limit = link->hw_sata_spd_limit;
54229af5c9c9STejun Heo 	link->sata_spd = 0;
5423c6fd2807SJeff Garzik 
5424c6fd2807SJeff Garzik 	/* High bits of dev->flags are used to record warm plug
5425c6fd2807SJeff Garzik 	 * requests which occur asynchronously.  Synchronize using
5426cca3974eSJeff Garzik 	 * host lock.
5427c6fd2807SJeff Garzik 	 */
5428c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
5429c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_INIT_MASK;
54303dcc323fSTejun Heo 	dev->horkage = 0;
5431c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
5432c6fd2807SJeff Garzik 
543399cf610aSTejun Heo 	memset((void *)dev + ATA_DEVICE_CLEAR_BEGIN, 0,
543499cf610aSTejun Heo 	       ATA_DEVICE_CLEAR_END - ATA_DEVICE_CLEAR_BEGIN);
5435c6fd2807SJeff Garzik 	dev->pio_mask = UINT_MAX;
5436c6fd2807SJeff Garzik 	dev->mwdma_mask = UINT_MAX;
5437c6fd2807SJeff Garzik 	dev->udma_mask = UINT_MAX;
5438c6fd2807SJeff Garzik }
5439c6fd2807SJeff Garzik 
5440c6fd2807SJeff Garzik /**
54414fb37a25STejun Heo  *	ata_link_init - Initialize an ata_link structure
54424fb37a25STejun Heo  *	@ap: ATA port link is attached to
54434fb37a25STejun Heo  *	@link: Link structure to initialize
54448989805dSTejun Heo  *	@pmp: Port multiplier port number
54454fb37a25STejun Heo  *
54464fb37a25STejun Heo  *	Initialize @link.
54474fb37a25STejun Heo  *
54484fb37a25STejun Heo  *	LOCKING:
54494fb37a25STejun Heo  *	Kernel thread context (may sleep)
54504fb37a25STejun Heo  */
5451fb7fd614STejun Heo void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
54524fb37a25STejun Heo {
54534fb37a25STejun Heo 	int i;
54544fb37a25STejun Heo 
54554fb37a25STejun Heo 	/* clear everything except for devices */
5456d9027470SGwendal Grignou 	memset((void *)link + ATA_LINK_CLEAR_BEGIN, 0,
5457d9027470SGwendal Grignou 	       ATA_LINK_CLEAR_END - ATA_LINK_CLEAR_BEGIN);
54584fb37a25STejun Heo 
54594fb37a25STejun Heo 	link->ap = ap;
54608989805dSTejun Heo 	link->pmp = pmp;
54614fb37a25STejun Heo 	link->active_tag = ATA_TAG_POISON;
54624fb37a25STejun Heo 	link->hw_sata_spd_limit = UINT_MAX;
54634fb37a25STejun Heo 
54644fb37a25STejun Heo 	/* can't use iterator, ap isn't initialized yet */
54654fb37a25STejun Heo 	for (i = 0; i < ATA_MAX_DEVICES; i++) {
54664fb37a25STejun Heo 		struct ata_device *dev = &link->device[i];
54674fb37a25STejun Heo 
54684fb37a25STejun Heo 		dev->link = link;
54694fb37a25STejun Heo 		dev->devno = dev - link->device;
5470110f66d2STejun Heo #ifdef CONFIG_ATA_ACPI
5471110f66d2STejun Heo 		dev->gtf_filter = ata_acpi_gtf_filter;
5472110f66d2STejun Heo #endif
54734fb37a25STejun Heo 		ata_dev_init(dev);
54744fb37a25STejun Heo 	}
54754fb37a25STejun Heo }
54764fb37a25STejun Heo 
54774fb37a25STejun Heo /**
54784fb37a25STejun Heo  *	sata_link_init_spd - Initialize link->sata_spd_limit
54794fb37a25STejun Heo  *	@link: Link to configure sata_spd_limit for
54804fb37a25STejun Heo  *
54814fb37a25STejun Heo  *	Initialize @link->[hw_]sata_spd_limit to the currently
54824fb37a25STejun Heo  *	configured value.
54834fb37a25STejun Heo  *
54844fb37a25STejun Heo  *	LOCKING:
54854fb37a25STejun Heo  *	Kernel thread context (may sleep).
54864fb37a25STejun Heo  *
54874fb37a25STejun Heo  *	RETURNS:
54884fb37a25STejun Heo  *	0 on success, -errno on failure.
54894fb37a25STejun Heo  */
5490fb7fd614STejun Heo int sata_link_init_spd(struct ata_link *link)
54914fb37a25STejun Heo {
549233267325STejun Heo 	u8 spd;
54934fb37a25STejun Heo 	int rc;
54944fb37a25STejun Heo 
5495d127ea7bSTejun Heo 	rc = sata_scr_read(link, SCR_CONTROL, &link->saved_scontrol);
54964fb37a25STejun Heo 	if (rc)
54974fb37a25STejun Heo 		return rc;
54984fb37a25STejun Heo 
5499d127ea7bSTejun Heo 	spd = (link->saved_scontrol >> 4) & 0xf;
55004fb37a25STejun Heo 	if (spd)
55014fb37a25STejun Heo 		link->hw_sata_spd_limit &= (1 << spd) - 1;
55024fb37a25STejun Heo 
550305944bdfSTejun Heo 	ata_force_link_limits(link);
550433267325STejun Heo 
55054fb37a25STejun Heo 	link->sata_spd_limit = link->hw_sata_spd_limit;
55064fb37a25STejun Heo 
55074fb37a25STejun Heo 	return 0;
55084fb37a25STejun Heo }
55094fb37a25STejun Heo 
55104fb37a25STejun Heo /**
5511f3187195STejun Heo  *	ata_port_alloc - allocate and initialize basic ATA port resources
5512f3187195STejun Heo  *	@host: ATA host this allocated port belongs to
5513c6fd2807SJeff Garzik  *
5514f3187195STejun Heo  *	Allocate and initialize basic ATA port resources.
5515f3187195STejun Heo  *
5516f3187195STejun Heo  *	RETURNS:
5517f3187195STejun Heo  *	Allocate ATA port on success, NULL on failure.
5518c6fd2807SJeff Garzik  *
5519c6fd2807SJeff Garzik  *	LOCKING:
5520f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5521c6fd2807SJeff Garzik  */
5522f3187195STejun Heo struct ata_port *ata_port_alloc(struct ata_host *host)
5523c6fd2807SJeff Garzik {
5524f3187195STejun Heo 	struct ata_port *ap;
5525c6fd2807SJeff Garzik 
5526f3187195STejun Heo 	DPRINTK("ENTER\n");
5527f3187195STejun Heo 
5528f3187195STejun Heo 	ap = kzalloc(sizeof(*ap), GFP_KERNEL);
5529f3187195STejun Heo 	if (!ap)
5530f3187195STejun Heo 		return NULL;
5531f3187195STejun Heo 
55327b3a24c5SMaxime Bizon 	ap->pflags |= ATA_PFLAG_INITIALIZING | ATA_PFLAG_FROZEN;
5533cca3974eSJeff Garzik 	ap->lock = &host->lock;
5534f3187195STejun Heo 	ap->print_id = -1;
5535cca3974eSJeff Garzik 	ap->host = host;
5536f3187195STejun Heo 	ap->dev = host->dev;
5537c6fd2807SJeff Garzik 
5538c6fd2807SJeff Garzik #if defined(ATA_VERBOSE_DEBUG)
5539c6fd2807SJeff Garzik 	/* turn on all debugging levels */
5540c6fd2807SJeff Garzik 	ap->msg_enable = 0x00FF;
5541c6fd2807SJeff Garzik #elif defined(ATA_DEBUG)
5542c6fd2807SJeff Garzik 	ap->msg_enable = ATA_MSG_DRV | ATA_MSG_INFO | ATA_MSG_CTL | ATA_MSG_WARN | ATA_MSG_ERR;
5543c6fd2807SJeff Garzik #else
5544c6fd2807SJeff Garzik 	ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
5545c6fd2807SJeff Garzik #endif
5546c6fd2807SJeff Garzik 
5547ad72cf98STejun Heo 	mutex_init(&ap->scsi_scan_mutex);
554865f27f38SDavid Howells 	INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
554965f27f38SDavid Howells 	INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
5550c6fd2807SJeff Garzik 	INIT_LIST_HEAD(&ap->eh_done_q);
5551c6fd2807SJeff Garzik 	init_waitqueue_head(&ap->eh_wait_q);
555245fabbb7SElias Oltmanns 	init_completion(&ap->park_req_pending);
55535ddf24c5STejun Heo 	init_timer_deferrable(&ap->fastdrain_timer);
55545ddf24c5STejun Heo 	ap->fastdrain_timer.function = ata_eh_fastdrain_timerfn;
55555ddf24c5STejun Heo 	ap->fastdrain_timer.data = (unsigned long)ap;
5556c6fd2807SJeff Garzik 
5557c6fd2807SJeff Garzik 	ap->cbl = ATA_CBL_NONE;
5558c6fd2807SJeff Garzik 
55598989805dSTejun Heo 	ata_link_init(ap, &ap->link, 0);
5560c6fd2807SJeff Garzik 
5561c6fd2807SJeff Garzik #ifdef ATA_IRQ_TRAP
5562c6fd2807SJeff Garzik 	ap->stats.unhandled_irq = 1;
5563c6fd2807SJeff Garzik 	ap->stats.idle_irq = 1;
5564c6fd2807SJeff Garzik #endif
5565270390e1STejun Heo 	ata_sff_port_init(ap);
5566270390e1STejun Heo 
5567c6fd2807SJeff Garzik 	return ap;
5568c6fd2807SJeff Garzik }
5569c6fd2807SJeff Garzik 
5570f0d36efdSTejun Heo static void ata_host_release(struct device *gendev, void *res)
5571f0d36efdSTejun Heo {
5572f0d36efdSTejun Heo 	struct ata_host *host = dev_get_drvdata(gendev);
5573f0d36efdSTejun Heo 	int i;
5574f0d36efdSTejun Heo 
5575f0d36efdSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
5576f0d36efdSTejun Heo 		struct ata_port *ap = host->ports[i];
5577f0d36efdSTejun Heo 
5578ecef7253STejun Heo 		if (!ap)
5579ecef7253STejun Heo 			continue;
5580ecef7253STejun Heo 
55814911487aSTejun Heo 		if (ap->scsi_host)
55821aa506e4STejun Heo 			scsi_host_put(ap->scsi_host);
55831aa506e4STejun Heo 
5584633273a3STejun Heo 		kfree(ap->pmp_link);
5585b1c72916STejun Heo 		kfree(ap->slave_link);
55864911487aSTejun Heo 		kfree(ap);
55871aa506e4STejun Heo 		host->ports[i] = NULL;
55881aa506e4STejun Heo 	}
55891aa506e4STejun Heo 
55901aa56ccaSTejun Heo 	dev_set_drvdata(gendev, NULL);
5591f0d36efdSTejun Heo }
5592f0d36efdSTejun Heo 
5593c6fd2807SJeff Garzik /**
5594f3187195STejun Heo  *	ata_host_alloc - allocate and init basic ATA host resources
5595f3187195STejun Heo  *	@dev: generic device this host is associated with
5596f3187195STejun Heo  *	@max_ports: maximum number of ATA ports associated with this host
5597f3187195STejun Heo  *
5598f3187195STejun Heo  *	Allocate and initialize basic ATA host resources.  LLD calls
5599f3187195STejun Heo  *	this function to allocate a host, initializes it fully and
5600f3187195STejun Heo  *	attaches it using ata_host_register().
5601f3187195STejun Heo  *
5602f3187195STejun Heo  *	@max_ports ports are allocated and host->n_ports is
5603f3187195STejun Heo  *	initialized to @max_ports.  The caller is allowed to decrease
5604f3187195STejun Heo  *	host->n_ports before calling ata_host_register().  The unused
5605f3187195STejun Heo  *	ports will be automatically freed on registration.
5606f3187195STejun Heo  *
5607f3187195STejun Heo  *	RETURNS:
5608f3187195STejun Heo  *	Allocate ATA host on success, NULL on failure.
5609f3187195STejun Heo  *
5610f3187195STejun Heo  *	LOCKING:
5611f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5612f3187195STejun Heo  */
5613f3187195STejun Heo struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
5614f3187195STejun Heo {
5615f3187195STejun Heo 	struct ata_host *host;
5616f3187195STejun Heo 	size_t sz;
5617f3187195STejun Heo 	int i;
5618f3187195STejun Heo 
5619f3187195STejun Heo 	DPRINTK("ENTER\n");
5620f3187195STejun Heo 
5621f3187195STejun Heo 	if (!devres_open_group(dev, NULL, GFP_KERNEL))
5622f3187195STejun Heo 		return NULL;
5623f3187195STejun Heo 
5624f3187195STejun Heo 	/* alloc a container for our list of ATA ports (buses) */
5625f3187195STejun Heo 	sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
5626f3187195STejun Heo 	/* alloc a container for our list of ATA ports (buses) */
5627f3187195STejun Heo 	host = devres_alloc(ata_host_release, sz, GFP_KERNEL);
5628f3187195STejun Heo 	if (!host)
5629f3187195STejun Heo 		goto err_out;
5630f3187195STejun Heo 
5631f3187195STejun Heo 	devres_add(dev, host);
5632f3187195STejun Heo 	dev_set_drvdata(dev, host);
5633f3187195STejun Heo 
5634f3187195STejun Heo 	spin_lock_init(&host->lock);
5635c0c362b6STejun Heo 	mutex_init(&host->eh_mutex);
5636f3187195STejun Heo 	host->dev = dev;
5637f3187195STejun Heo 	host->n_ports = max_ports;
5638f3187195STejun Heo 
5639f3187195STejun Heo 	/* allocate ports bound to this host */
5640f3187195STejun Heo 	for (i = 0; i < max_ports; i++) {
5641f3187195STejun Heo 		struct ata_port *ap;
5642f3187195STejun Heo 
5643f3187195STejun Heo 		ap = ata_port_alloc(host);
5644f3187195STejun Heo 		if (!ap)
5645f3187195STejun Heo 			goto err_out;
5646f3187195STejun Heo 
5647f3187195STejun Heo 		ap->port_no = i;
5648f3187195STejun Heo 		host->ports[i] = ap;
5649f3187195STejun Heo 	}
5650f3187195STejun Heo 
5651f3187195STejun Heo 	devres_remove_group(dev, NULL);
5652f3187195STejun Heo 	return host;
5653f3187195STejun Heo 
5654f3187195STejun Heo  err_out:
5655f3187195STejun Heo 	devres_release_group(dev, NULL);
5656f3187195STejun Heo 	return NULL;
5657f3187195STejun Heo }
5658f3187195STejun Heo 
5659f3187195STejun Heo /**
5660f5cda257STejun Heo  *	ata_host_alloc_pinfo - alloc host and init with port_info array
5661f5cda257STejun Heo  *	@dev: generic device this host is associated with
5662f5cda257STejun Heo  *	@ppi: array of ATA port_info to initialize host with
5663f5cda257STejun Heo  *	@n_ports: number of ATA ports attached to this host
5664f5cda257STejun Heo  *
5665f5cda257STejun Heo  *	Allocate ATA host and initialize with info from @ppi.  If NULL
5666f5cda257STejun Heo  *	terminated, @ppi may contain fewer entries than @n_ports.  The
5667f5cda257STejun Heo  *	last entry will be used for the remaining ports.
5668f5cda257STejun Heo  *
5669f5cda257STejun Heo  *	RETURNS:
5670f5cda257STejun Heo  *	Allocate ATA host on success, NULL on failure.
5671f5cda257STejun Heo  *
5672f5cda257STejun Heo  *	LOCKING:
5673f5cda257STejun Heo  *	Inherited from calling layer (may sleep).
5674f5cda257STejun Heo  */
5675f5cda257STejun Heo struct ata_host *ata_host_alloc_pinfo(struct device *dev,
5676f5cda257STejun Heo 				      const struct ata_port_info * const * ppi,
5677f5cda257STejun Heo 				      int n_ports)
5678f5cda257STejun Heo {
5679f5cda257STejun Heo 	const struct ata_port_info *pi;
5680f5cda257STejun Heo 	struct ata_host *host;
5681f5cda257STejun Heo 	int i, j;
5682f5cda257STejun Heo 
5683f5cda257STejun Heo 	host = ata_host_alloc(dev, n_ports);
5684f5cda257STejun Heo 	if (!host)
5685f5cda257STejun Heo 		return NULL;
5686f5cda257STejun Heo 
5687f5cda257STejun Heo 	for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
5688f5cda257STejun Heo 		struct ata_port *ap = host->ports[i];
5689f5cda257STejun Heo 
5690f5cda257STejun Heo 		if (ppi[j])
5691f5cda257STejun Heo 			pi = ppi[j++];
5692f5cda257STejun Heo 
5693f5cda257STejun Heo 		ap->pio_mask = pi->pio_mask;
5694f5cda257STejun Heo 		ap->mwdma_mask = pi->mwdma_mask;
5695f5cda257STejun Heo 		ap->udma_mask = pi->udma_mask;
5696f5cda257STejun Heo 		ap->flags |= pi->flags;
56970c88758bSTejun Heo 		ap->link.flags |= pi->link_flags;
5698f5cda257STejun Heo 		ap->ops = pi->port_ops;
5699f5cda257STejun Heo 
5700f5cda257STejun Heo 		if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
5701f5cda257STejun Heo 			host->ops = pi->port_ops;
5702f5cda257STejun Heo 	}
5703f5cda257STejun Heo 
5704f5cda257STejun Heo 	return host;
5705f5cda257STejun Heo }
5706f5cda257STejun Heo 
5707b1c72916STejun Heo /**
5708b1c72916STejun Heo  *	ata_slave_link_init - initialize slave link
5709b1c72916STejun Heo  *	@ap: port to initialize slave link for
5710b1c72916STejun Heo  *
5711b1c72916STejun Heo  *	Create and initialize slave link for @ap.  This enables slave
5712b1c72916STejun Heo  *	link handling on the port.
5713b1c72916STejun Heo  *
5714b1c72916STejun Heo  *	In libata, a port contains links and a link contains devices.
5715b1c72916STejun Heo  *	There is single host link but if a PMP is attached to it,
5716b1c72916STejun Heo  *	there can be multiple fan-out links.  On SATA, there's usually
5717b1c72916STejun Heo  *	a single device connected to a link but PATA and SATA
5718b1c72916STejun Heo  *	controllers emulating TF based interface can have two - master
5719b1c72916STejun Heo  *	and slave.
5720b1c72916STejun Heo  *
5721b1c72916STejun Heo  *	However, there are a few controllers which don't fit into this
5722b1c72916STejun Heo  *	abstraction too well - SATA controllers which emulate TF
5723b1c72916STejun Heo  *	interface with both master and slave devices but also have
5724b1c72916STejun Heo  *	separate SCR register sets for each device.  These controllers
5725b1c72916STejun Heo  *	need separate links for physical link handling
5726b1c72916STejun Heo  *	(e.g. onlineness, link speed) but should be treated like a
5727b1c72916STejun Heo  *	traditional M/S controller for everything else (e.g. command
5728b1c72916STejun Heo  *	issue, softreset).
5729b1c72916STejun Heo  *
5730b1c72916STejun Heo  *	slave_link is libata's way of handling this class of
5731b1c72916STejun Heo  *	controllers without impacting core layer too much.  For
5732b1c72916STejun Heo  *	anything other than physical link handling, the default host
5733b1c72916STejun Heo  *	link is used for both master and slave.  For physical link
5734b1c72916STejun Heo  *	handling, separate @ap->slave_link is used.  All dirty details
5735b1c72916STejun Heo  *	are implemented inside libata core layer.  From LLD's POV, the
5736b1c72916STejun Heo  *	only difference is that prereset, hardreset and postreset are
5737b1c72916STejun Heo  *	called once more for the slave link, so the reset sequence
5738b1c72916STejun Heo  *	looks like the following.
5739b1c72916STejun Heo  *
5740b1c72916STejun Heo  *	prereset(M) -> prereset(S) -> hardreset(M) -> hardreset(S) ->
5741b1c72916STejun Heo  *	softreset(M) -> postreset(M) -> postreset(S)
5742b1c72916STejun Heo  *
5743b1c72916STejun Heo  *	Note that softreset is called only for the master.  Softreset
5744b1c72916STejun Heo  *	resets both M/S by definition, so SRST on master should handle
5745b1c72916STejun Heo  *	both (the standard method will work just fine).
5746b1c72916STejun Heo  *
5747b1c72916STejun Heo  *	LOCKING:
5748b1c72916STejun Heo  *	Should be called before host is registered.
5749b1c72916STejun Heo  *
5750b1c72916STejun Heo  *	RETURNS:
5751b1c72916STejun Heo  *	0 on success, -errno on failure.
5752b1c72916STejun Heo  */
5753b1c72916STejun Heo int ata_slave_link_init(struct ata_port *ap)
5754b1c72916STejun Heo {
5755b1c72916STejun Heo 	struct ata_link *link;
5756b1c72916STejun Heo 
5757b1c72916STejun Heo 	WARN_ON(ap->slave_link);
5758b1c72916STejun Heo 	WARN_ON(ap->flags & ATA_FLAG_PMP);
5759b1c72916STejun Heo 
5760b1c72916STejun Heo 	link = kzalloc(sizeof(*link), GFP_KERNEL);
5761b1c72916STejun Heo 	if (!link)
5762b1c72916STejun Heo 		return -ENOMEM;
5763b1c72916STejun Heo 
5764b1c72916STejun Heo 	ata_link_init(ap, link, 1);
5765b1c72916STejun Heo 	ap->slave_link = link;
5766b1c72916STejun Heo 	return 0;
5767b1c72916STejun Heo }
5768b1c72916STejun Heo 
576932ebbc0cSTejun Heo static void ata_host_stop(struct device *gendev, void *res)
577032ebbc0cSTejun Heo {
577132ebbc0cSTejun Heo 	struct ata_host *host = dev_get_drvdata(gendev);
577232ebbc0cSTejun Heo 	int i;
577332ebbc0cSTejun Heo 
577432ebbc0cSTejun Heo 	WARN_ON(!(host->flags & ATA_HOST_STARTED));
577532ebbc0cSTejun Heo 
577632ebbc0cSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
577732ebbc0cSTejun Heo 		struct ata_port *ap = host->ports[i];
577832ebbc0cSTejun Heo 
577932ebbc0cSTejun Heo 		if (ap->ops->port_stop)
578032ebbc0cSTejun Heo 			ap->ops->port_stop(ap);
578132ebbc0cSTejun Heo 	}
578232ebbc0cSTejun Heo 
578332ebbc0cSTejun Heo 	if (host->ops->host_stop)
578432ebbc0cSTejun Heo 		host->ops->host_stop(host);
578532ebbc0cSTejun Heo }
578632ebbc0cSTejun Heo 
5787f5cda257STejun Heo /**
5788029cfd6bSTejun Heo  *	ata_finalize_port_ops - finalize ata_port_operations
5789029cfd6bSTejun Heo  *	@ops: ata_port_operations to finalize
5790029cfd6bSTejun Heo  *
5791029cfd6bSTejun Heo  *	An ata_port_operations can inherit from another ops and that
5792029cfd6bSTejun Heo  *	ops can again inherit from another.  This can go on as many
5793029cfd6bSTejun Heo  *	times as necessary as long as there is no loop in the
5794029cfd6bSTejun Heo  *	inheritance chain.
5795029cfd6bSTejun Heo  *
5796029cfd6bSTejun Heo  *	Ops tables are finalized when the host is started.  NULL or
5797029cfd6bSTejun Heo  *	unspecified entries are inherited from the closet ancestor
5798029cfd6bSTejun Heo  *	which has the method and the entry is populated with it.
5799029cfd6bSTejun Heo  *	After finalization, the ops table directly points to all the
5800029cfd6bSTejun Heo  *	methods and ->inherits is no longer necessary and cleared.
5801029cfd6bSTejun Heo  *
5802029cfd6bSTejun Heo  *	Using ATA_OP_NULL, inheriting ops can force a method to NULL.
5803029cfd6bSTejun Heo  *
5804029cfd6bSTejun Heo  *	LOCKING:
5805029cfd6bSTejun Heo  *	None.
5806029cfd6bSTejun Heo  */
5807029cfd6bSTejun Heo static void ata_finalize_port_ops(struct ata_port_operations *ops)
5808029cfd6bSTejun Heo {
58092da67659SPradeep Singh Rautela 	static DEFINE_SPINLOCK(lock);
5810029cfd6bSTejun Heo 	const struct ata_port_operations *cur;
5811029cfd6bSTejun Heo 	void **begin = (void **)ops;
5812029cfd6bSTejun Heo 	void **end = (void **)&ops->inherits;
5813029cfd6bSTejun Heo 	void **pp;
5814029cfd6bSTejun Heo 
5815029cfd6bSTejun Heo 	if (!ops || !ops->inherits)
5816029cfd6bSTejun Heo 		return;
5817029cfd6bSTejun Heo 
5818029cfd6bSTejun Heo 	spin_lock(&lock);
5819029cfd6bSTejun Heo 
5820029cfd6bSTejun Heo 	for (cur = ops->inherits; cur; cur = cur->inherits) {
5821029cfd6bSTejun Heo 		void **inherit = (void **)cur;
5822029cfd6bSTejun Heo 
5823029cfd6bSTejun Heo 		for (pp = begin; pp < end; pp++, inherit++)
5824029cfd6bSTejun Heo 			if (!*pp)
5825029cfd6bSTejun Heo 				*pp = *inherit;
5826029cfd6bSTejun Heo 	}
5827029cfd6bSTejun Heo 
5828029cfd6bSTejun Heo 	for (pp = begin; pp < end; pp++)
5829029cfd6bSTejun Heo 		if (IS_ERR(*pp))
5830029cfd6bSTejun Heo 			*pp = NULL;
5831029cfd6bSTejun Heo 
5832029cfd6bSTejun Heo 	ops->inherits = NULL;
5833029cfd6bSTejun Heo 
5834029cfd6bSTejun Heo 	spin_unlock(&lock);
5835029cfd6bSTejun Heo }
5836029cfd6bSTejun Heo 
5837029cfd6bSTejun Heo /**
5838ecef7253STejun Heo  *	ata_host_start - start and freeze ports of an ATA host
5839ecef7253STejun Heo  *	@host: ATA host to start ports for
5840ecef7253STejun Heo  *
5841ecef7253STejun Heo  *	Start and then freeze ports of @host.  Started status is
5842ecef7253STejun Heo  *	recorded in host->flags, so this function can be called
5843ecef7253STejun Heo  *	multiple times.  Ports are guaranteed to get started only
5844f3187195STejun Heo  *	once.  If host->ops isn't initialized yet, its set to the
5845f3187195STejun Heo  *	first non-dummy port ops.
5846ecef7253STejun Heo  *
5847ecef7253STejun Heo  *	LOCKING:
5848ecef7253STejun Heo  *	Inherited from calling layer (may sleep).
5849ecef7253STejun Heo  *
5850ecef7253STejun Heo  *	RETURNS:
5851ecef7253STejun Heo  *	0 if all ports are started successfully, -errno otherwise.
5852ecef7253STejun Heo  */
5853ecef7253STejun Heo int ata_host_start(struct ata_host *host)
5854ecef7253STejun Heo {
585532ebbc0cSTejun Heo 	int have_stop = 0;
585632ebbc0cSTejun Heo 	void *start_dr = NULL;
5857ecef7253STejun Heo 	int i, rc;
5858ecef7253STejun Heo 
5859ecef7253STejun Heo 	if (host->flags & ATA_HOST_STARTED)
5860ecef7253STejun Heo 		return 0;
5861ecef7253STejun Heo 
5862029cfd6bSTejun Heo 	ata_finalize_port_ops(host->ops);
5863029cfd6bSTejun Heo 
5864ecef7253STejun Heo 	for (i = 0; i < host->n_ports; i++) {
5865ecef7253STejun Heo 		struct ata_port *ap = host->ports[i];
5866ecef7253STejun Heo 
5867029cfd6bSTejun Heo 		ata_finalize_port_ops(ap->ops);
5868029cfd6bSTejun Heo 
5869f3187195STejun Heo 		if (!host->ops && !ata_port_is_dummy(ap))
5870f3187195STejun Heo 			host->ops = ap->ops;
5871f3187195STejun Heo 
587232ebbc0cSTejun Heo 		if (ap->ops->port_stop)
587332ebbc0cSTejun Heo 			have_stop = 1;
587432ebbc0cSTejun Heo 	}
587532ebbc0cSTejun Heo 
587632ebbc0cSTejun Heo 	if (host->ops->host_stop)
587732ebbc0cSTejun Heo 		have_stop = 1;
587832ebbc0cSTejun Heo 
587932ebbc0cSTejun Heo 	if (have_stop) {
588032ebbc0cSTejun Heo 		start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
588132ebbc0cSTejun Heo 		if (!start_dr)
588232ebbc0cSTejun Heo 			return -ENOMEM;
588332ebbc0cSTejun Heo 	}
588432ebbc0cSTejun Heo 
588532ebbc0cSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
588632ebbc0cSTejun Heo 		struct ata_port *ap = host->ports[i];
588732ebbc0cSTejun Heo 
5888ecef7253STejun Heo 		if (ap->ops->port_start) {
5889ecef7253STejun Heo 			rc = ap->ops->port_start(ap);
5890ecef7253STejun Heo 			if (rc) {
58910f9fe9b7SAlan Cox 				if (rc != -ENODEV)
5892a44fec1fSJoe Perches 					dev_err(host->dev,
5893a44fec1fSJoe Perches 						"failed to start port %d (errno=%d)\n",
5894a44fec1fSJoe Perches 						i, rc);
5895ecef7253STejun Heo 				goto err_out;
5896ecef7253STejun Heo 			}
5897ecef7253STejun Heo 		}
5898ecef7253STejun Heo 		ata_eh_freeze_port(ap);
5899ecef7253STejun Heo 	}
5900ecef7253STejun Heo 
590132ebbc0cSTejun Heo 	if (start_dr)
590232ebbc0cSTejun Heo 		devres_add(host->dev, start_dr);
5903ecef7253STejun Heo 	host->flags |= ATA_HOST_STARTED;
5904ecef7253STejun Heo 	return 0;
5905ecef7253STejun Heo 
5906ecef7253STejun Heo  err_out:
5907ecef7253STejun Heo 	while (--i >= 0) {
5908ecef7253STejun Heo 		struct ata_port *ap = host->ports[i];
5909ecef7253STejun Heo 
5910ecef7253STejun Heo 		if (ap->ops->port_stop)
5911ecef7253STejun Heo 			ap->ops->port_stop(ap);
5912ecef7253STejun Heo 	}
591332ebbc0cSTejun Heo 	devres_free(start_dr);
5914ecef7253STejun Heo 	return rc;
5915ecef7253STejun Heo }
5916ecef7253STejun Heo 
5917ecef7253STejun Heo /**
5918cca3974eSJeff Garzik  *	ata_sas_host_init - Initialize a host struct
5919cca3974eSJeff Garzik  *	@host:	host to initialize
5920cca3974eSJeff Garzik  *	@dev:	device host is attached to
5921cca3974eSJeff Garzik  *	@flags:	host flags
5922c6fd2807SJeff Garzik  *	@ops:	port_ops
5923c6fd2807SJeff Garzik  *
5924c6fd2807SJeff Garzik  *	LOCKING:
5925c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
5926c6fd2807SJeff Garzik  *
5927c6fd2807SJeff Garzik  */
5928f3187195STejun Heo /* KILLME - the only user left is ipr */
5929cca3974eSJeff Garzik void ata_host_init(struct ata_host *host, struct device *dev,
5930029cfd6bSTejun Heo 		   unsigned long flags, struct ata_port_operations *ops)
5931c6fd2807SJeff Garzik {
5932cca3974eSJeff Garzik 	spin_lock_init(&host->lock);
5933c0c362b6STejun Heo 	mutex_init(&host->eh_mutex);
5934cca3974eSJeff Garzik 	host->dev = dev;
5935cca3974eSJeff Garzik 	host->flags = flags;
5936cca3974eSJeff Garzik 	host->ops = ops;
5937c6fd2807SJeff Garzik }
5938c6fd2807SJeff Garzik 
59399508a66fSDan Williams void __ata_port_probe(struct ata_port *ap)
594079318057SArjan van de Ven {
594179318057SArjan van de Ven 	struct ata_eh_info *ehi = &ap->link.eh_info;
594279318057SArjan van de Ven 	unsigned long flags;
594379318057SArjan van de Ven 
594479318057SArjan van de Ven 	/* kick EH for boot probing */
594579318057SArjan van de Ven 	spin_lock_irqsave(ap->lock, flags);
594679318057SArjan van de Ven 
594779318057SArjan van de Ven 	ehi->probe_mask |= ATA_ALL_DEVICES;
59486b7ae954STejun Heo 	ehi->action |= ATA_EH_RESET;
594979318057SArjan van de Ven 	ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
595079318057SArjan van de Ven 
595179318057SArjan van de Ven 	ap->pflags &= ~ATA_PFLAG_INITIALIZING;
595279318057SArjan van de Ven 	ap->pflags |= ATA_PFLAG_LOADING;
595379318057SArjan van de Ven 	ata_port_schedule_eh(ap);
595479318057SArjan van de Ven 
595579318057SArjan van de Ven 	spin_unlock_irqrestore(ap->lock, flags);
59569508a66fSDan Williams }
595779318057SArjan van de Ven 
59589508a66fSDan Williams int ata_port_probe(struct ata_port *ap)
59599508a66fSDan Williams {
59609508a66fSDan Williams 	int rc = 0;
59619508a66fSDan Williams 
59629508a66fSDan Williams 	if (ap->ops->error_handler) {
59639508a66fSDan Williams 		__ata_port_probe(ap);
596479318057SArjan van de Ven 		ata_port_wait_eh(ap);
596579318057SArjan van de Ven 	} else {
596679318057SArjan van de Ven 		DPRINTK("ata%u: bus probe begin\n", ap->print_id);
596779318057SArjan van de Ven 		rc = ata_bus_probe(ap);
596879318057SArjan van de Ven 		DPRINTK("ata%u: bus probe end\n", ap->print_id);
5969238c9cf9SJames Bottomley 	}
5970238c9cf9SJames Bottomley 	return rc;
5971238c9cf9SJames Bottomley }
597279318057SArjan van de Ven 
5973238c9cf9SJames Bottomley 
5974238c9cf9SJames Bottomley static void async_port_probe(void *data, async_cookie_t cookie)
5975238c9cf9SJames Bottomley {
5976238c9cf9SJames Bottomley 	struct ata_port *ap = data;
5977238c9cf9SJames Bottomley 
5978238c9cf9SJames Bottomley 	/*
5979238c9cf9SJames Bottomley 	 * If we're not allowed to scan this host in parallel,
5980238c9cf9SJames Bottomley 	 * we need to wait until all previous scans have completed
5981238c9cf9SJames Bottomley 	 * before going further.
5982238c9cf9SJames Bottomley 	 * Jeff Garzik says this is only within a controller, so we
5983238c9cf9SJames Bottomley 	 * don't need to wait for port 0, only for later ports.
598479318057SArjan van de Ven 	 */
5985238c9cf9SJames Bottomley 	if (!(ap->host->flags & ATA_HOST_PARALLEL_SCAN) && ap->port_no != 0)
5986238c9cf9SJames Bottomley 		async_synchronize_cookie(cookie);
5987238c9cf9SJames Bottomley 
5988238c9cf9SJames Bottomley 	(void)ata_port_probe(ap);
5989f29d3b23SArjan van de Ven 
5990f29d3b23SArjan van de Ven 	/* in order to keep device order, we need to synchronize at this point */
5991f29d3b23SArjan van de Ven 	async_synchronize_cookie(cookie);
5992f29d3b23SArjan van de Ven 
5993f29d3b23SArjan van de Ven 	ata_scsi_scan_host(ap, 1);
599479318057SArjan van de Ven }
5995238c9cf9SJames Bottomley 
5996c6fd2807SJeff Garzik /**
5997f3187195STejun Heo  *	ata_host_register - register initialized ATA host
5998f3187195STejun Heo  *	@host: ATA host to register
5999f3187195STejun Heo  *	@sht: template for SCSI host
6000c6fd2807SJeff Garzik  *
6001f3187195STejun Heo  *	Register initialized ATA host.  @host is allocated using
6002f3187195STejun Heo  *	ata_host_alloc() and fully initialized by LLD.  This function
6003f3187195STejun Heo  *	starts ports, registers @host with ATA and SCSI layers and
6004f3187195STejun Heo  *	probe registered devices.
6005c6fd2807SJeff Garzik  *
6006c6fd2807SJeff Garzik  *	LOCKING:
6007f3187195STejun Heo  *	Inherited from calling layer (may sleep).
6008c6fd2807SJeff Garzik  *
6009c6fd2807SJeff Garzik  *	RETURNS:
6010f3187195STejun Heo  *	0 on success, -errno otherwise.
6011c6fd2807SJeff Garzik  */
6012f3187195STejun Heo int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
6013c6fd2807SJeff Garzik {
6014f3187195STejun Heo 	int i, rc;
6015c6fd2807SJeff Garzik 
6016f3187195STejun Heo 	/* host must have been started */
6017f3187195STejun Heo 	if (!(host->flags & ATA_HOST_STARTED)) {
6018a44fec1fSJoe Perches 		dev_err(host->dev, "BUG: trying to register unstarted host\n");
6019f3187195STejun Heo 		WARN_ON(1);
6020f3187195STejun Heo 		return -EINVAL;
602102f076aaSAlan Cox 	}
6022f0d36efdSTejun Heo 
6023f3187195STejun Heo 	/* Blow away unused ports.  This happens when LLD can't
6024f3187195STejun Heo 	 * determine the exact number of ports to allocate at
6025f3187195STejun Heo 	 * allocation time.
6026f3187195STejun Heo 	 */
6027f3187195STejun Heo 	for (i = host->n_ports; host->ports[i]; i++)
6028f3187195STejun Heo 		kfree(host->ports[i]);
6029f0d36efdSTejun Heo 
6030f3187195STejun Heo 	/* give ports names and add SCSI hosts */
6031f3187195STejun Heo 	for (i = 0; i < host->n_ports; i++)
603285d6725bSDan Williams 		host->ports[i]->print_id = atomic_inc_return(&ata_print_id);
6033c6fd2807SJeff Garzik 
6034d9027470SGwendal Grignou 
6035d9027470SGwendal Grignou 	/* Create associated sysfs transport objects  */
6036d9027470SGwendal Grignou 	for (i = 0; i < host->n_ports; i++) {
6037d9027470SGwendal Grignou 		rc = ata_tport_add(host->dev,host->ports[i]);
6038d9027470SGwendal Grignou 		if (rc) {
6039d9027470SGwendal Grignou 			goto err_tadd;
6040d9027470SGwendal Grignou 		}
6041d9027470SGwendal Grignou 	}
6042d9027470SGwendal Grignou 
6043f3187195STejun Heo 	rc = ata_scsi_add_hosts(host, sht);
6044ecef7253STejun Heo 	if (rc)
6045d9027470SGwendal Grignou 		goto err_tadd;
6046ecef7253STejun Heo 
6047fafbae87STejun Heo 	/* associate with ACPI nodes */
6048fafbae87STejun Heo 	ata_acpi_associate(host);
6049fafbae87STejun Heo 
6050f3187195STejun Heo 	/* set cable, sata_spd_limit and report */
6051cca3974eSJeff Garzik 	for (i = 0; i < host->n_ports; i++) {
6052cca3974eSJeff Garzik 		struct ata_port *ap = host->ports[i];
6053f3187195STejun Heo 		unsigned long xfer_mask;
6054f3187195STejun Heo 
6055f3187195STejun Heo 		/* set SATA cable type if still unset */
6056f3187195STejun Heo 		if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
6057f3187195STejun Heo 			ap->cbl = ATA_CBL_SATA;
6058c6fd2807SJeff Garzik 
6059c6fd2807SJeff Garzik 		/* init sata_spd_limit to the current value */
60604fb37a25STejun Heo 		sata_link_init_spd(&ap->link);
6061b1c72916STejun Heo 		if (ap->slave_link)
6062b1c72916STejun Heo 			sata_link_init_spd(ap->slave_link);
6063c6fd2807SJeff Garzik 
6064cbcdd875STejun Heo 		/* print per-port info to dmesg */
6065f3187195STejun Heo 		xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
6066f3187195STejun Heo 					      ap->udma_mask);
6067f3187195STejun Heo 
6068abf6e8edSTejun Heo 		if (!ata_port_is_dummy(ap)) {
6069a9a79dfeSJoe Perches 			ata_port_info(ap, "%cATA max %s %s\n",
6070a16abc0bSTejun Heo 				      (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
6071f3187195STejun Heo 				      ata_mode_string(xfer_mask),
6072cbcdd875STejun Heo 				      ap->link.eh_info.desc);
6073abf6e8edSTejun Heo 			ata_ehi_clear_desc(&ap->link.eh_info);
6074abf6e8edSTejun Heo 		} else
6075a9a79dfeSJoe Perches 			ata_port_info(ap, "DUMMY\n");
6076c6fd2807SJeff Garzik 	}
6077c6fd2807SJeff Garzik 
6078f6005354SVegard Nossum 	/* perform each probe asynchronously */
6079f3187195STejun Heo 	for (i = 0; i < host->n_ports; i++) {
6080f3187195STejun Heo 		struct ata_port *ap = host->ports[i];
608179318057SArjan van de Ven 		async_schedule(async_port_probe, ap);
6082c6fd2807SJeff Garzik 	}
6083c6fd2807SJeff Garzik 
6084f3187195STejun Heo 	return 0;
6085d9027470SGwendal Grignou 
6086d9027470SGwendal Grignou  err_tadd:
6087d9027470SGwendal Grignou 	while (--i >= 0) {
6088d9027470SGwendal Grignou 		ata_tport_delete(host->ports[i]);
6089d9027470SGwendal Grignou 	}
6090d9027470SGwendal Grignou 	return rc;
6091d9027470SGwendal Grignou 
6092f3187195STejun Heo }
6093f3187195STejun Heo 
6094f3187195STejun Heo /**
6095f5cda257STejun Heo  *	ata_host_activate - start host, request IRQ and register it
6096f5cda257STejun Heo  *	@host: target ATA host
6097f5cda257STejun Heo  *	@irq: IRQ to request
6098f5cda257STejun Heo  *	@irq_handler: irq_handler used when requesting IRQ
6099f5cda257STejun Heo  *	@irq_flags: irq_flags used when requesting IRQ
6100f5cda257STejun Heo  *	@sht: scsi_host_template to use when registering the host
6101f5cda257STejun Heo  *
6102f5cda257STejun Heo  *	After allocating an ATA host and initializing it, most libata
6103f5cda257STejun Heo  *	LLDs perform three steps to activate the host - start host,
6104f5cda257STejun Heo  *	request IRQ and register it.  This helper takes necessasry
6105f5cda257STejun Heo  *	arguments and performs the three steps in one go.
6106f5cda257STejun Heo  *
61073d46b2e2SPaul Mundt  *	An invalid IRQ skips the IRQ registration and expects the host to
61083d46b2e2SPaul Mundt  *	have set polling mode on the port. In this case, @irq_handler
61093d46b2e2SPaul Mundt  *	should be NULL.
61103d46b2e2SPaul Mundt  *
6111f5cda257STejun Heo  *	LOCKING:
6112f5cda257STejun Heo  *	Inherited from calling layer (may sleep).
6113f5cda257STejun Heo  *
6114f5cda257STejun Heo  *	RETURNS:
6115f5cda257STejun Heo  *	0 on success, -errno otherwise.
6116f5cda257STejun Heo  */
6117f5cda257STejun Heo int ata_host_activate(struct ata_host *host, int irq,
6118f5cda257STejun Heo 		      irq_handler_t irq_handler, unsigned long irq_flags,
6119f5cda257STejun Heo 		      struct scsi_host_template *sht)
6120f5cda257STejun Heo {
6121cbcdd875STejun Heo 	int i, rc;
6122f5cda257STejun Heo 
6123f5cda257STejun Heo 	rc = ata_host_start(host);
6124f5cda257STejun Heo 	if (rc)
6125f5cda257STejun Heo 		return rc;
6126f5cda257STejun Heo 
61273d46b2e2SPaul Mundt 	/* Special case for polling mode */
61283d46b2e2SPaul Mundt 	if (!irq) {
61293d46b2e2SPaul Mundt 		WARN_ON(irq_handler);
61303d46b2e2SPaul Mundt 		return ata_host_register(host, sht);
61313d46b2e2SPaul Mundt 	}
61323d46b2e2SPaul Mundt 
6133f5cda257STejun Heo 	rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
6134f5cda257STejun Heo 			      dev_driver_string(host->dev), host);
6135f5cda257STejun Heo 	if (rc)
6136f5cda257STejun Heo 		return rc;
6137f5cda257STejun Heo 
6138cbcdd875STejun Heo 	for (i = 0; i < host->n_ports; i++)
6139cbcdd875STejun Heo 		ata_port_desc(host->ports[i], "irq %d", irq);
61404031826bSTejun Heo 
6141f5cda257STejun Heo 	rc = ata_host_register(host, sht);
6142f5cda257STejun Heo 	/* if failed, just free the IRQ and leave ports alone */
6143f5cda257STejun Heo 	if (rc)
6144f5cda257STejun Heo 		devm_free_irq(host->dev, irq, host);
6145f5cda257STejun Heo 
6146f5cda257STejun Heo 	return rc;
6147f5cda257STejun Heo }
6148f5cda257STejun Heo 
6149f5cda257STejun Heo /**
6150c6fd2807SJeff Garzik  *	ata_port_detach - Detach ATA port in prepration of device removal
6151c6fd2807SJeff Garzik  *	@ap: ATA port to be detached
6152c6fd2807SJeff Garzik  *
6153c6fd2807SJeff Garzik  *	Detach all ATA devices and the associated SCSI devices of @ap;
6154c6fd2807SJeff Garzik  *	then, remove the associated SCSI host.  @ap is guaranteed to
6155c6fd2807SJeff Garzik  *	be quiescent on return from this function.
6156c6fd2807SJeff Garzik  *
6157c6fd2807SJeff Garzik  *	LOCKING:
6158c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
6159c6fd2807SJeff Garzik  */
6160741b7763SAdrian Bunk static void ata_port_detach(struct ata_port *ap)
6161c6fd2807SJeff Garzik {
6162c6fd2807SJeff Garzik 	unsigned long flags;
6163c6fd2807SJeff Garzik 
6164c6fd2807SJeff Garzik 	if (!ap->ops->error_handler)
6165c6fd2807SJeff Garzik 		goto skip_eh;
6166c6fd2807SJeff Garzik 
6167c6fd2807SJeff Garzik 	/* tell EH we're leaving & flush EH */
6168c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
6169c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_UNLOADING;
6170ece180d1STejun Heo 	ata_port_schedule_eh(ap);
6171c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
6172c6fd2807SJeff Garzik 
6173ece180d1STejun Heo 	/* wait till EH commits suicide */
6174c6fd2807SJeff Garzik 	ata_port_wait_eh(ap);
6175c6fd2807SJeff Garzik 
6176ece180d1STejun Heo 	/* it better be dead now */
6177ece180d1STejun Heo 	WARN_ON(!(ap->pflags & ATA_PFLAG_UNLOADED));
6178c6fd2807SJeff Garzik 
6179afe2c511STejun Heo 	cancel_delayed_work_sync(&ap->hotplug_task);
6180c6fd2807SJeff Garzik 
6181c6fd2807SJeff Garzik  skip_eh:
6182d9027470SGwendal Grignou 	if (ap->pmp_link) {
6183d9027470SGwendal Grignou 		int i;
6184d9027470SGwendal Grignou 		for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
6185d9027470SGwendal Grignou 			ata_tlink_delete(&ap->pmp_link[i]);
6186d9027470SGwendal Grignou 	}
6187d9027470SGwendal Grignou 	ata_tport_delete(ap);
6188d9027470SGwendal Grignou 
6189c6fd2807SJeff Garzik 	/* remove the associated SCSI host */
6190cca3974eSJeff Garzik 	scsi_remove_host(ap->scsi_host);
6191c6fd2807SJeff Garzik }
6192c6fd2807SJeff Garzik 
6193c6fd2807SJeff Garzik /**
61940529c159STejun Heo  *	ata_host_detach - Detach all ports of an ATA host
61950529c159STejun Heo  *	@host: Host to detach
61960529c159STejun Heo  *
61970529c159STejun Heo  *	Detach all ports of @host.
61980529c159STejun Heo  *
61990529c159STejun Heo  *	LOCKING:
62000529c159STejun Heo  *	Kernel thread context (may sleep).
62010529c159STejun Heo  */
62020529c159STejun Heo void ata_host_detach(struct ata_host *host)
62030529c159STejun Heo {
62040529c159STejun Heo 	int i;
62050529c159STejun Heo 
62060529c159STejun Heo 	for (i = 0; i < host->n_ports; i++)
62070529c159STejun Heo 		ata_port_detach(host->ports[i]);
6208562f0c2dSTejun Heo 
6209562f0c2dSTejun Heo 	/* the host is dead now, dissociate ACPI */
6210562f0c2dSTejun Heo 	ata_acpi_dissociate(host);
62110529c159STejun Heo }
62120529c159STejun Heo 
6213c6fd2807SJeff Garzik #ifdef CONFIG_PCI
6214c6fd2807SJeff Garzik 
6215c6fd2807SJeff Garzik /**
6216c6fd2807SJeff Garzik  *	ata_pci_remove_one - PCI layer callback for device removal
6217c6fd2807SJeff Garzik  *	@pdev: PCI device that was removed
6218c6fd2807SJeff Garzik  *
6219b878ca5dSTejun Heo  *	PCI layer indicates to libata via this hook that hot-unplug or
6220b878ca5dSTejun Heo  *	module unload event has occurred.  Detach all ports.  Resource
6221b878ca5dSTejun Heo  *	release is handled via devres.
6222c6fd2807SJeff Garzik  *
6223c6fd2807SJeff Garzik  *	LOCKING:
6224c6fd2807SJeff Garzik  *	Inherited from PCI layer (may sleep).
6225c6fd2807SJeff Garzik  */
6226c6fd2807SJeff Garzik void ata_pci_remove_one(struct pci_dev *pdev)
6227c6fd2807SJeff Garzik {
62282855568bSJeff Garzik 	struct device *dev = &pdev->dev;
6229cca3974eSJeff Garzik 	struct ata_host *host = dev_get_drvdata(dev);
6230c6fd2807SJeff Garzik 
6231f0d36efdSTejun Heo 	ata_host_detach(host);
6232c6fd2807SJeff Garzik }
6233c6fd2807SJeff Garzik 
6234c6fd2807SJeff Garzik /* move to PCI subsystem */
6235c6fd2807SJeff Garzik int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
6236c6fd2807SJeff Garzik {
6237c6fd2807SJeff Garzik 	unsigned long tmp = 0;
6238c6fd2807SJeff Garzik 
6239c6fd2807SJeff Garzik 	switch (bits->width) {
6240c6fd2807SJeff Garzik 	case 1: {
6241c6fd2807SJeff Garzik 		u8 tmp8 = 0;
6242c6fd2807SJeff Garzik 		pci_read_config_byte(pdev, bits->reg, &tmp8);
6243c6fd2807SJeff Garzik 		tmp = tmp8;
6244c6fd2807SJeff Garzik 		break;
6245c6fd2807SJeff Garzik 	}
6246c6fd2807SJeff Garzik 	case 2: {
6247c6fd2807SJeff Garzik 		u16 tmp16 = 0;
6248c6fd2807SJeff Garzik 		pci_read_config_word(pdev, bits->reg, &tmp16);
6249c6fd2807SJeff Garzik 		tmp = tmp16;
6250c6fd2807SJeff Garzik 		break;
6251c6fd2807SJeff Garzik 	}
6252c6fd2807SJeff Garzik 	case 4: {
6253c6fd2807SJeff Garzik 		u32 tmp32 = 0;
6254c6fd2807SJeff Garzik 		pci_read_config_dword(pdev, bits->reg, &tmp32);
6255c6fd2807SJeff Garzik 		tmp = tmp32;
6256c6fd2807SJeff Garzik 		break;
6257c6fd2807SJeff Garzik 	}
6258c6fd2807SJeff Garzik 
6259c6fd2807SJeff Garzik 	default:
6260c6fd2807SJeff Garzik 		return -EINVAL;
6261c6fd2807SJeff Garzik 	}
6262c6fd2807SJeff Garzik 
6263c6fd2807SJeff Garzik 	tmp &= bits->mask;
6264c6fd2807SJeff Garzik 
6265c6fd2807SJeff Garzik 	return (tmp == bits->val) ? 1 : 0;
6266c6fd2807SJeff Garzik }
6267c6fd2807SJeff Garzik 
62686ffa01d8STejun Heo #ifdef CONFIG_PM
6269c6fd2807SJeff Garzik void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
6270c6fd2807SJeff Garzik {
6271c6fd2807SJeff Garzik 	pci_save_state(pdev);
6272c6fd2807SJeff Garzik 	pci_disable_device(pdev);
62734c90d971STejun Heo 
62743a2d5b70SRafael J. Wysocki 	if (mesg.event & PM_EVENT_SLEEP)
6275c6fd2807SJeff Garzik 		pci_set_power_state(pdev, PCI_D3hot);
6276c6fd2807SJeff Garzik }
6277c6fd2807SJeff Garzik 
6278553c4aa6STejun Heo int ata_pci_device_do_resume(struct pci_dev *pdev)
6279c6fd2807SJeff Garzik {
6280553c4aa6STejun Heo 	int rc;
6281553c4aa6STejun Heo 
6282c6fd2807SJeff Garzik 	pci_set_power_state(pdev, PCI_D0);
6283c6fd2807SJeff Garzik 	pci_restore_state(pdev);
6284553c4aa6STejun Heo 
6285f0d36efdSTejun Heo 	rc = pcim_enable_device(pdev);
6286553c4aa6STejun Heo 	if (rc) {
6287a44fec1fSJoe Perches 		dev_err(&pdev->dev,
6288553c4aa6STejun Heo 			"failed to enable device after resume (%d)\n", rc);
6289553c4aa6STejun Heo 		return rc;
6290553c4aa6STejun Heo 	}
6291553c4aa6STejun Heo 
6292c6fd2807SJeff Garzik 	pci_set_master(pdev);
6293553c4aa6STejun Heo 	return 0;
6294c6fd2807SJeff Garzik }
6295c6fd2807SJeff Garzik 
6296c6fd2807SJeff Garzik int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
6297c6fd2807SJeff Garzik {
6298cca3974eSJeff Garzik 	struct ata_host *host = dev_get_drvdata(&pdev->dev);
6299c6fd2807SJeff Garzik 	int rc = 0;
6300c6fd2807SJeff Garzik 
6301cca3974eSJeff Garzik 	rc = ata_host_suspend(host, mesg);
6302c6fd2807SJeff Garzik 	if (rc)
6303c6fd2807SJeff Garzik 		return rc;
6304c6fd2807SJeff Garzik 
6305c6fd2807SJeff Garzik 	ata_pci_device_do_suspend(pdev, mesg);
6306c6fd2807SJeff Garzik 
6307c6fd2807SJeff Garzik 	return 0;
6308c6fd2807SJeff Garzik }
6309c6fd2807SJeff Garzik 
6310c6fd2807SJeff Garzik int ata_pci_device_resume(struct pci_dev *pdev)
6311c6fd2807SJeff Garzik {
6312cca3974eSJeff Garzik 	struct ata_host *host = dev_get_drvdata(&pdev->dev);
6313553c4aa6STejun Heo 	int rc;
6314c6fd2807SJeff Garzik 
6315553c4aa6STejun Heo 	rc = ata_pci_device_do_resume(pdev);
6316553c4aa6STejun Heo 	if (rc == 0)
6317cca3974eSJeff Garzik 		ata_host_resume(host);
6318553c4aa6STejun Heo 	return rc;
6319c6fd2807SJeff Garzik }
63206ffa01d8STejun Heo #endif /* CONFIG_PM */
63216ffa01d8STejun Heo 
6322c6fd2807SJeff Garzik #endif /* CONFIG_PCI */
6323c6fd2807SJeff Garzik 
632433267325STejun Heo static int __init ata_parse_force_one(char **cur,
632533267325STejun Heo 				      struct ata_force_ent *force_ent,
632633267325STejun Heo 				      const char **reason)
632733267325STejun Heo {
632833267325STejun Heo 	/* FIXME: Currently, there's no way to tag init const data and
632933267325STejun Heo 	 * using __initdata causes build failure on some versions of
633033267325STejun Heo 	 * gcc.  Once __initdataconst is implemented, add const to the
633133267325STejun Heo 	 * following structure.
633233267325STejun Heo 	 */
633333267325STejun Heo 	static struct ata_force_param force_tbl[] __initdata = {
633433267325STejun Heo 		{ "40c",	.cbl		= ATA_CBL_PATA40 },
633533267325STejun Heo 		{ "80c",	.cbl		= ATA_CBL_PATA80 },
633633267325STejun Heo 		{ "short40c",	.cbl		= ATA_CBL_PATA40_SHORT },
633733267325STejun Heo 		{ "unk",	.cbl		= ATA_CBL_PATA_UNK },
633833267325STejun Heo 		{ "ign",	.cbl		= ATA_CBL_PATA_IGN },
633933267325STejun Heo 		{ "sata",	.cbl		= ATA_CBL_SATA },
634033267325STejun Heo 		{ "1.5Gbps",	.spd_limit	= 1 },
634133267325STejun Heo 		{ "3.0Gbps",	.spd_limit	= 2 },
634233267325STejun Heo 		{ "noncq",	.horkage_on	= ATA_HORKAGE_NONCQ },
634333267325STejun Heo 		{ "ncq",	.horkage_off	= ATA_HORKAGE_NONCQ },
634443c9c591STejun Heo 		{ "dump_id",	.horkage_on	= ATA_HORKAGE_DUMP_ID },
634533267325STejun Heo 		{ "pio0",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 0) },
634633267325STejun Heo 		{ "pio1",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 1) },
634733267325STejun Heo 		{ "pio2",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 2) },
634833267325STejun Heo 		{ "pio3",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 3) },
634933267325STejun Heo 		{ "pio4",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 4) },
635033267325STejun Heo 		{ "pio5",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 5) },
635133267325STejun Heo 		{ "pio6",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 6) },
635233267325STejun Heo 		{ "mwdma0",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 0) },
635333267325STejun Heo 		{ "mwdma1",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 1) },
635433267325STejun Heo 		{ "mwdma2",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 2) },
635533267325STejun Heo 		{ "mwdma3",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 3) },
635633267325STejun Heo 		{ "mwdma4",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 4) },
635733267325STejun Heo 		{ "udma0",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
635833267325STejun Heo 		{ "udma16",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
635933267325STejun Heo 		{ "udma/16",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
636033267325STejun Heo 		{ "udma1",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
636133267325STejun Heo 		{ "udma25",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
636233267325STejun Heo 		{ "udma/25",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
636333267325STejun Heo 		{ "udma2",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
636433267325STejun Heo 		{ "udma33",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
636533267325STejun Heo 		{ "udma/33",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
636633267325STejun Heo 		{ "udma3",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
636733267325STejun Heo 		{ "udma44",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
636833267325STejun Heo 		{ "udma/44",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
636933267325STejun Heo 		{ "udma4",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
637033267325STejun Heo 		{ "udma66",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
637133267325STejun Heo 		{ "udma/66",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
637233267325STejun Heo 		{ "udma5",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
637333267325STejun Heo 		{ "udma100",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
637433267325STejun Heo 		{ "udma/100",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
637533267325STejun Heo 		{ "udma6",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
637633267325STejun Heo 		{ "udma133",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
637733267325STejun Heo 		{ "udma/133",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
637833267325STejun Heo 		{ "udma7",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 7) },
637905944bdfSTejun Heo 		{ "nohrst",	.lflags		= ATA_LFLAG_NO_HRST },
638005944bdfSTejun Heo 		{ "nosrst",	.lflags		= ATA_LFLAG_NO_SRST },
638105944bdfSTejun Heo 		{ "norst",	.lflags		= ATA_LFLAG_NO_HRST | ATA_LFLAG_NO_SRST },
638233267325STejun Heo 	};
638333267325STejun Heo 	char *start = *cur, *p = *cur;
638433267325STejun Heo 	char *id, *val, *endp;
638533267325STejun Heo 	const struct ata_force_param *match_fp = NULL;
638633267325STejun Heo 	int nr_matches = 0, i;
638733267325STejun Heo 
638833267325STejun Heo 	/* find where this param ends and update *cur */
638933267325STejun Heo 	while (*p != '\0' && *p != ',')
639033267325STejun Heo 		p++;
639133267325STejun Heo 
639233267325STejun Heo 	if (*p == '\0')
639333267325STejun Heo 		*cur = p;
639433267325STejun Heo 	else
639533267325STejun Heo 		*cur = p + 1;
639633267325STejun Heo 
639733267325STejun Heo 	*p = '\0';
639833267325STejun Heo 
639933267325STejun Heo 	/* parse */
640033267325STejun Heo 	p = strchr(start, ':');
640133267325STejun Heo 	if (!p) {
640233267325STejun Heo 		val = strstrip(start);
640333267325STejun Heo 		goto parse_val;
640433267325STejun Heo 	}
640533267325STejun Heo 	*p = '\0';
640633267325STejun Heo 
640733267325STejun Heo 	id = strstrip(start);
640833267325STejun Heo 	val = strstrip(p + 1);
640933267325STejun Heo 
641033267325STejun Heo 	/* parse id */
641133267325STejun Heo 	p = strchr(id, '.');
641233267325STejun Heo 	if (p) {
641333267325STejun Heo 		*p++ = '\0';
641433267325STejun Heo 		force_ent->device = simple_strtoul(p, &endp, 10);
641533267325STejun Heo 		if (p == endp || *endp != '\0') {
641633267325STejun Heo 			*reason = "invalid device";
641733267325STejun Heo 			return -EINVAL;
641833267325STejun Heo 		}
641933267325STejun Heo 	}
642033267325STejun Heo 
642133267325STejun Heo 	force_ent->port = simple_strtoul(id, &endp, 10);
642233267325STejun Heo 	if (p == endp || *endp != '\0') {
642333267325STejun Heo 		*reason = "invalid port/link";
642433267325STejun Heo 		return -EINVAL;
642533267325STejun Heo 	}
642633267325STejun Heo 
642733267325STejun Heo  parse_val:
642833267325STejun Heo 	/* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */
642933267325STejun Heo 	for (i = 0; i < ARRAY_SIZE(force_tbl); i++) {
643033267325STejun Heo 		const struct ata_force_param *fp = &force_tbl[i];
643133267325STejun Heo 
643233267325STejun Heo 		if (strncasecmp(val, fp->name, strlen(val)))
643333267325STejun Heo 			continue;
643433267325STejun Heo 
643533267325STejun Heo 		nr_matches++;
643633267325STejun Heo 		match_fp = fp;
643733267325STejun Heo 
643833267325STejun Heo 		if (strcasecmp(val, fp->name) == 0) {
643933267325STejun Heo 			nr_matches = 1;
644033267325STejun Heo 			break;
644133267325STejun Heo 		}
644233267325STejun Heo 	}
644333267325STejun Heo 
644433267325STejun Heo 	if (!nr_matches) {
644533267325STejun Heo 		*reason = "unknown value";
644633267325STejun Heo 		return -EINVAL;
644733267325STejun Heo 	}
644833267325STejun Heo 	if (nr_matches > 1) {
644933267325STejun Heo 		*reason = "ambigious value";
645033267325STejun Heo 		return -EINVAL;
645133267325STejun Heo 	}
645233267325STejun Heo 
645333267325STejun Heo 	force_ent->param = *match_fp;
645433267325STejun Heo 
645533267325STejun Heo 	return 0;
645633267325STejun Heo }
645733267325STejun Heo 
645833267325STejun Heo static void __init ata_parse_force_param(void)
645933267325STejun Heo {
646033267325STejun Heo 	int idx = 0, size = 1;
646133267325STejun Heo 	int last_port = -1, last_device = -1;
646233267325STejun Heo 	char *p, *cur, *next;
646333267325STejun Heo 
646433267325STejun Heo 	/* calculate maximum number of params and allocate force_tbl */
646533267325STejun Heo 	for (p = ata_force_param_buf; *p; p++)
646633267325STejun Heo 		if (*p == ',')
646733267325STejun Heo 			size++;
646833267325STejun Heo 
646933267325STejun Heo 	ata_force_tbl = kzalloc(sizeof(ata_force_tbl[0]) * size, GFP_KERNEL);
647033267325STejun Heo 	if (!ata_force_tbl) {
647133267325STejun Heo 		printk(KERN_WARNING "ata: failed to extend force table, "
647233267325STejun Heo 		       "libata.force ignored\n");
647333267325STejun Heo 		return;
647433267325STejun Heo 	}
647533267325STejun Heo 
647633267325STejun Heo 	/* parse and populate the table */
647733267325STejun Heo 	for (cur = ata_force_param_buf; *cur != '\0'; cur = next) {
647833267325STejun Heo 		const char *reason = "";
647933267325STejun Heo 		struct ata_force_ent te = { .port = -1, .device = -1 };
648033267325STejun Heo 
648133267325STejun Heo 		next = cur;
648233267325STejun Heo 		if (ata_parse_force_one(&next, &te, &reason)) {
648333267325STejun Heo 			printk(KERN_WARNING "ata: failed to parse force "
648433267325STejun Heo 			       "parameter \"%s\" (%s)\n",
648533267325STejun Heo 			       cur, reason);
648633267325STejun Heo 			continue;
648733267325STejun Heo 		}
648833267325STejun Heo 
648933267325STejun Heo 		if (te.port == -1) {
649033267325STejun Heo 			te.port = last_port;
649133267325STejun Heo 			te.device = last_device;
649233267325STejun Heo 		}
649333267325STejun Heo 
649433267325STejun Heo 		ata_force_tbl[idx++] = te;
649533267325STejun Heo 
649633267325STejun Heo 		last_port = te.port;
649733267325STejun Heo 		last_device = te.device;
649833267325STejun Heo 	}
649933267325STejun Heo 
650033267325STejun Heo 	ata_force_tbl_size = idx;
650133267325STejun Heo }
6502c6fd2807SJeff Garzik 
6503c6fd2807SJeff Garzik static int __init ata_init(void)
6504c6fd2807SJeff Garzik {
6505d9027470SGwendal Grignou 	int rc;
6506270390e1STejun Heo 
650733267325STejun Heo 	ata_parse_force_param();
650833267325STejun Heo 
6509270390e1STejun Heo 	rc = ata_sff_init();
6510ad72cf98STejun Heo 	if (rc) {
6511ad72cf98STejun Heo 		kfree(ata_force_tbl);
6512ad72cf98STejun Heo 		return rc;
6513ad72cf98STejun Heo 	}
6514c6fd2807SJeff Garzik 
6515d9027470SGwendal Grignou 	libata_transport_init();
6516d9027470SGwendal Grignou 	ata_scsi_transport_template = ata_attach_transport();
6517d9027470SGwendal Grignou 	if (!ata_scsi_transport_template) {
6518d9027470SGwendal Grignou 		ata_sff_exit();
6519d9027470SGwendal Grignou 		rc = -ENOMEM;
6520d9027470SGwendal Grignou 		goto err_out;
6521d9027470SGwendal Grignou 	}
6522d9027470SGwendal Grignou 
6523c6fd2807SJeff Garzik 	printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
6524c6fd2807SJeff Garzik 	return 0;
6525d9027470SGwendal Grignou 
6526d9027470SGwendal Grignou err_out:
6527d9027470SGwendal Grignou 	return rc;
6528c6fd2807SJeff Garzik }
6529c6fd2807SJeff Garzik 
6530c6fd2807SJeff Garzik static void __exit ata_exit(void)
6531c6fd2807SJeff Garzik {
6532d9027470SGwendal Grignou 	ata_release_transport(ata_scsi_transport_template);
6533d9027470SGwendal Grignou 	libata_transport_exit();
6534270390e1STejun Heo 	ata_sff_exit();
653533267325STejun Heo 	kfree(ata_force_tbl);
6536c6fd2807SJeff Garzik }
6537c6fd2807SJeff Garzik 
6538a4625085SBrian King subsys_initcall(ata_init);
6539c6fd2807SJeff Garzik module_exit(ata_exit);
6540c6fd2807SJeff Garzik 
65419990b6f3SAkinobu Mita static DEFINE_RATELIMIT_STATE(ratelimit, HZ / 5, 1);
6542c6fd2807SJeff Garzik 
6543c6fd2807SJeff Garzik int ata_ratelimit(void)
6544c6fd2807SJeff Garzik {
65459990b6f3SAkinobu Mita 	return __ratelimit(&ratelimit);
6546c6fd2807SJeff Garzik }
6547c6fd2807SJeff Garzik 
6548c0c362b6STejun Heo /**
6549c0c362b6STejun Heo  *	ata_msleep - ATA EH owner aware msleep
6550c0c362b6STejun Heo  *	@ap: ATA port to attribute the sleep to
6551c0c362b6STejun Heo  *	@msecs: duration to sleep in milliseconds
6552c0c362b6STejun Heo  *
6553c0c362b6STejun Heo  *	Sleeps @msecs.  If the current task is owner of @ap's EH, the
6554c0c362b6STejun Heo  *	ownership is released before going to sleep and reacquired
6555c0c362b6STejun Heo  *	after the sleep is complete.  IOW, other ports sharing the
6556c0c362b6STejun Heo  *	@ap->host will be allowed to own the EH while this task is
6557c0c362b6STejun Heo  *	sleeping.
6558c0c362b6STejun Heo  *
6559c0c362b6STejun Heo  *	LOCKING:
6560c0c362b6STejun Heo  *	Might sleep.
6561c0c362b6STejun Heo  */
656297750cebSTejun Heo void ata_msleep(struct ata_port *ap, unsigned int msecs)
656397750cebSTejun Heo {
6564c0c362b6STejun Heo 	bool owns_eh = ap && ap->host->eh_owner == current;
6565c0c362b6STejun Heo 
6566c0c362b6STejun Heo 	if (owns_eh)
6567c0c362b6STejun Heo 		ata_eh_release(ap);
6568c0c362b6STejun Heo 
656997750cebSTejun Heo 	msleep(msecs);
6570c0c362b6STejun Heo 
6571c0c362b6STejun Heo 	if (owns_eh)
6572c0c362b6STejun Heo 		ata_eh_acquire(ap);
657397750cebSTejun Heo }
657497750cebSTejun Heo 
6575c6fd2807SJeff Garzik /**
6576c6fd2807SJeff Garzik  *	ata_wait_register - wait until register value changes
657797750cebSTejun Heo  *	@ap: ATA port to wait register for, can be NULL
6578c6fd2807SJeff Garzik  *	@reg: IO-mapped register
6579c6fd2807SJeff Garzik  *	@mask: Mask to apply to read register value
6580c6fd2807SJeff Garzik  *	@val: Wait condition
6581341c2c95STejun Heo  *	@interval: polling interval in milliseconds
6582341c2c95STejun Heo  *	@timeout: timeout in milliseconds
6583c6fd2807SJeff Garzik  *
6584c6fd2807SJeff Garzik  *	Waiting for some bits of register to change is a common
6585c6fd2807SJeff Garzik  *	operation for ATA controllers.  This function reads 32bit LE
6586c6fd2807SJeff Garzik  *	IO-mapped register @reg and tests for the following condition.
6587c6fd2807SJeff Garzik  *
6588c6fd2807SJeff Garzik  *	(*@reg & mask) != val
6589c6fd2807SJeff Garzik  *
6590c6fd2807SJeff Garzik  *	If the condition is met, it returns; otherwise, the process is
6591c6fd2807SJeff Garzik  *	repeated after @interval_msec until timeout.
6592c6fd2807SJeff Garzik  *
6593c6fd2807SJeff Garzik  *	LOCKING:
6594c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
6595c6fd2807SJeff Garzik  *
6596c6fd2807SJeff Garzik  *	RETURNS:
6597c6fd2807SJeff Garzik  *	The final register value.
6598c6fd2807SJeff Garzik  */
659997750cebSTejun Heo u32 ata_wait_register(struct ata_port *ap, void __iomem *reg, u32 mask, u32 val,
6600341c2c95STejun Heo 		      unsigned long interval, unsigned long timeout)
6601c6fd2807SJeff Garzik {
6602341c2c95STejun Heo 	unsigned long deadline;
6603c6fd2807SJeff Garzik 	u32 tmp;
6604c6fd2807SJeff Garzik 
6605c6fd2807SJeff Garzik 	tmp = ioread32(reg);
6606c6fd2807SJeff Garzik 
6607c6fd2807SJeff Garzik 	/* Calculate timeout _after_ the first read to make sure
6608c6fd2807SJeff Garzik 	 * preceding writes reach the controller before starting to
6609c6fd2807SJeff Garzik 	 * eat away the timeout.
6610c6fd2807SJeff Garzik 	 */
6611341c2c95STejun Heo 	deadline = ata_deadline(jiffies, timeout);
6612c6fd2807SJeff Garzik 
6613341c2c95STejun Heo 	while ((tmp & mask) == val && time_before(jiffies, deadline)) {
661497750cebSTejun Heo 		ata_msleep(ap, interval);
6615c6fd2807SJeff Garzik 		tmp = ioread32(reg);
6616c6fd2807SJeff Garzik 	}
6617c6fd2807SJeff Garzik 
6618c6fd2807SJeff Garzik 	return tmp;
6619c6fd2807SJeff Garzik }
6620c6fd2807SJeff Garzik 
6621c6fd2807SJeff Garzik /*
6622c6fd2807SJeff Garzik  * Dummy port_ops
6623c6fd2807SJeff Garzik  */
6624c6fd2807SJeff Garzik static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
6625c6fd2807SJeff Garzik {
6626c6fd2807SJeff Garzik 	return AC_ERR_SYSTEM;
6627c6fd2807SJeff Garzik }
6628c6fd2807SJeff Garzik 
6629182d7bbaSTejun Heo static void ata_dummy_error_handler(struct ata_port *ap)
6630182d7bbaSTejun Heo {
6631182d7bbaSTejun Heo 	/* truly dummy */
6632182d7bbaSTejun Heo }
6633182d7bbaSTejun Heo 
6634029cfd6bSTejun Heo struct ata_port_operations ata_dummy_port_ops = {
6635c6fd2807SJeff Garzik 	.qc_prep		= ata_noop_qc_prep,
6636c6fd2807SJeff Garzik 	.qc_issue		= ata_dummy_qc_issue,
6637182d7bbaSTejun Heo 	.error_handler		= ata_dummy_error_handler,
6638c6fd2807SJeff Garzik };
6639c6fd2807SJeff Garzik 
664021b0ad4fSTejun Heo const struct ata_port_info ata_dummy_port_info = {
664121b0ad4fSTejun Heo 	.port_ops		= &ata_dummy_port_ops,
664221b0ad4fSTejun Heo };
664321b0ad4fSTejun Heo 
6644c6fd2807SJeff Garzik /*
6645a9a79dfeSJoe Perches  * Utility print functions
6646a9a79dfeSJoe Perches  */
6647a9a79dfeSJoe Perches int ata_port_printk(const struct ata_port *ap, const char *level,
6648a9a79dfeSJoe Perches 		    const char *fmt, ...)
6649a9a79dfeSJoe Perches {
6650a9a79dfeSJoe Perches 	struct va_format vaf;
6651a9a79dfeSJoe Perches 	va_list args;
6652a9a79dfeSJoe Perches 	int r;
6653a9a79dfeSJoe Perches 
6654a9a79dfeSJoe Perches 	va_start(args, fmt);
6655a9a79dfeSJoe Perches 
6656a9a79dfeSJoe Perches 	vaf.fmt = fmt;
6657a9a79dfeSJoe Perches 	vaf.va = &args;
6658a9a79dfeSJoe Perches 
6659a9a79dfeSJoe Perches 	r = printk("%sata%u: %pV", level, ap->print_id, &vaf);
6660a9a79dfeSJoe Perches 
6661a9a79dfeSJoe Perches 	va_end(args);
6662a9a79dfeSJoe Perches 
6663a9a79dfeSJoe Perches 	return r;
6664a9a79dfeSJoe Perches }
6665a9a79dfeSJoe Perches EXPORT_SYMBOL(ata_port_printk);
6666a9a79dfeSJoe Perches 
6667a9a79dfeSJoe Perches int ata_link_printk(const struct ata_link *link, const char *level,
6668a9a79dfeSJoe Perches 		    const char *fmt, ...)
6669a9a79dfeSJoe Perches {
6670a9a79dfeSJoe Perches 	struct va_format vaf;
6671a9a79dfeSJoe Perches 	va_list args;
6672a9a79dfeSJoe Perches 	int r;
6673a9a79dfeSJoe Perches 
6674a9a79dfeSJoe Perches 	va_start(args, fmt);
6675a9a79dfeSJoe Perches 
6676a9a79dfeSJoe Perches 	vaf.fmt = fmt;
6677a9a79dfeSJoe Perches 	vaf.va = &args;
6678a9a79dfeSJoe Perches 
6679a9a79dfeSJoe Perches 	if (sata_pmp_attached(link->ap) || link->ap->slave_link)
6680a9a79dfeSJoe Perches 		r = printk("%sata%u.%02u: %pV",
6681a9a79dfeSJoe Perches 			   level, link->ap->print_id, link->pmp, &vaf);
6682a9a79dfeSJoe Perches 	else
6683a9a79dfeSJoe Perches 		r = printk("%sata%u: %pV",
6684a9a79dfeSJoe Perches 			   level, link->ap->print_id, &vaf);
6685a9a79dfeSJoe Perches 
6686a9a79dfeSJoe Perches 	va_end(args);
6687a9a79dfeSJoe Perches 
6688a9a79dfeSJoe Perches 	return r;
6689a9a79dfeSJoe Perches }
6690a9a79dfeSJoe Perches EXPORT_SYMBOL(ata_link_printk);
6691a9a79dfeSJoe Perches 
6692a9a79dfeSJoe Perches int ata_dev_printk(const struct ata_device *dev, const char *level,
6693a9a79dfeSJoe Perches 		    const char *fmt, ...)
6694a9a79dfeSJoe Perches {
6695a9a79dfeSJoe Perches 	struct va_format vaf;
6696a9a79dfeSJoe Perches 	va_list args;
6697a9a79dfeSJoe Perches 	int r;
6698a9a79dfeSJoe Perches 
6699a9a79dfeSJoe Perches 	va_start(args, fmt);
6700a9a79dfeSJoe Perches 
6701a9a79dfeSJoe Perches 	vaf.fmt = fmt;
6702a9a79dfeSJoe Perches 	vaf.va = &args;
6703a9a79dfeSJoe Perches 
6704a9a79dfeSJoe Perches 	r = printk("%sata%u.%02u: %pV",
6705a9a79dfeSJoe Perches 		   level, dev->link->ap->print_id, dev->link->pmp + dev->devno,
6706a9a79dfeSJoe Perches 		   &vaf);
6707a9a79dfeSJoe Perches 
6708a9a79dfeSJoe Perches 	va_end(args);
6709a9a79dfeSJoe Perches 
6710a9a79dfeSJoe Perches 	return r;
6711a9a79dfeSJoe Perches }
6712a9a79dfeSJoe Perches EXPORT_SYMBOL(ata_dev_printk);
6713a9a79dfeSJoe Perches 
671406296a1eSJoe Perches void ata_print_version(const struct device *dev, const char *version)
671506296a1eSJoe Perches {
671606296a1eSJoe Perches 	dev_printk(KERN_DEBUG, dev, "version %s\n", version);
671706296a1eSJoe Perches }
671806296a1eSJoe Perches EXPORT_SYMBOL(ata_print_version);
671906296a1eSJoe Perches 
6720a9a79dfeSJoe Perches /*
6721c6fd2807SJeff Garzik  * libata is essentially a library of internal helper functions for
6722c6fd2807SJeff Garzik  * low-level ATA host controller drivers.  As such, the API/ABI is
6723c6fd2807SJeff Garzik  * likely to change as new drivers are added and updated.
6724c6fd2807SJeff Garzik  * Do not depend on ABI/API stability.
6725c6fd2807SJeff Garzik  */
6726c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
6727c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
6728c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_deb_timing_long);
6729029cfd6bSTejun Heo EXPORT_SYMBOL_GPL(ata_base_port_ops);
6730029cfd6bSTejun Heo EXPORT_SYMBOL_GPL(sata_port_ops);
6731c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
673221b0ad4fSTejun Heo EXPORT_SYMBOL_GPL(ata_dummy_port_info);
67331eca4365STejun Heo EXPORT_SYMBOL_GPL(ata_link_next);
67341eca4365STejun Heo EXPORT_SYMBOL_GPL(ata_dev_next);
6735c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_std_bios_param);
6736d8d9129eSTejun Heo EXPORT_SYMBOL_GPL(ata_scsi_unlock_native_capacity);
6737cca3974eSJeff Garzik EXPORT_SYMBOL_GPL(ata_host_init);
6738f3187195STejun Heo EXPORT_SYMBOL_GPL(ata_host_alloc);
6739f5cda257STejun Heo EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
6740b1c72916STejun Heo EXPORT_SYMBOL_GPL(ata_slave_link_init);
6741ecef7253STejun Heo EXPORT_SYMBOL_GPL(ata_host_start);
6742f3187195STejun Heo EXPORT_SYMBOL_GPL(ata_host_register);
6743f5cda257STejun Heo EXPORT_SYMBOL_GPL(ata_host_activate);
67440529c159STejun Heo EXPORT_SYMBOL_GPL(ata_host_detach);
6745c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_sg_init);
6746c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_qc_complete);
6747c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
6748436d34b3STejun Heo EXPORT_SYMBOL_GPL(atapi_cmd_type);
6749c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_tf_to_fis);
6750c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_tf_from_fis);
67516357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_pack_xfermask);
67526357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_unpack_xfermask);
67536357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
67546357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
67556357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
67566357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_mode_string);
67576357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_id_xfermask);
675804351821SAlan EXPORT_SYMBOL_GPL(ata_do_set_mode);
675931cc23b3STejun Heo EXPORT_SYMBOL_GPL(ata_std_qc_defer);
6760c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
676110305f0fSAlan EXPORT_SYMBOL_GPL(ata_dev_disable);
6762c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_set_spd);
6763aa2731adSTejun Heo EXPORT_SYMBOL_GPL(ata_wait_after_reset);
6764936fd732STejun Heo EXPORT_SYMBOL_GPL(sata_link_debounce);
6765936fd732STejun Heo EXPORT_SYMBOL_GPL(sata_link_resume);
67661152b261STejun Heo EXPORT_SYMBOL_GPL(sata_link_scr_lpm);
67670aa1113dSTejun Heo EXPORT_SYMBOL_GPL(ata_std_prereset);
6768cc0680a5STejun Heo EXPORT_SYMBOL_GPL(sata_link_hardreset);
676957c9efdfSTejun Heo EXPORT_SYMBOL_GPL(sata_std_hardreset);
6770203c75b8STejun Heo EXPORT_SYMBOL_GPL(ata_std_postreset);
6771c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_dev_classify);
6772c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_dev_pair);
6773c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_ratelimit);
677497750cebSTejun Heo EXPORT_SYMBOL_GPL(ata_msleep);
6775c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_wait_register);
6776c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
6777c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
6778c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
6779c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
6780f6e67035SDan Williams EXPORT_SYMBOL_GPL(__ata_change_queue_depth);
6781c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_valid);
6782c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_read);
6783c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_write);
6784c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_write_flush);
6785936fd732STejun Heo EXPORT_SYMBOL_GPL(ata_link_online);
6786936fd732STejun Heo EXPORT_SYMBOL_GPL(ata_link_offline);
67876ffa01d8STejun Heo #ifdef CONFIG_PM
6788cca3974eSJeff Garzik EXPORT_SYMBOL_GPL(ata_host_suspend);
6789cca3974eSJeff Garzik EXPORT_SYMBOL_GPL(ata_host_resume);
67906ffa01d8STejun Heo #endif /* CONFIG_PM */
6791c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_id_string);
6792c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_id_c_string);
6793963e4975SAlan Cox EXPORT_SYMBOL_GPL(ata_do_dev_read_id);
6794c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_simulate);
6795c6fd2807SJeff Garzik 
6796c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
67976357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_timing_find_mode);
6798c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_timing_compute);
6799c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_timing_merge);
6800a0f79b92STejun Heo EXPORT_SYMBOL_GPL(ata_timing_cycle2mode);
6801c6fd2807SJeff Garzik 
6802c6fd2807SJeff Garzik #ifdef CONFIG_PCI
6803c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(pci_test_config_bits);
6804c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_remove_one);
68056ffa01d8STejun Heo #ifdef CONFIG_PM
6806c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
6807c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
6808c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
6809c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_resume);
68106ffa01d8STejun Heo #endif /* CONFIG_PM */
6811c6fd2807SJeff Garzik #endif /* CONFIG_PCI */
6812c6fd2807SJeff Garzik 
6813b64bbc39STejun Heo EXPORT_SYMBOL_GPL(__ata_ehi_push_desc);
6814b64bbc39STejun Heo EXPORT_SYMBOL_GPL(ata_ehi_push_desc);
6815b64bbc39STejun Heo EXPORT_SYMBOL_GPL(ata_ehi_clear_desc);
6816cbcdd875STejun Heo EXPORT_SYMBOL_GPL(ata_port_desc);
6817cbcdd875STejun Heo #ifdef CONFIG_PCI
6818cbcdd875STejun Heo EXPORT_SYMBOL_GPL(ata_port_pbar_desc);
6819cbcdd875STejun Heo #endif /* CONFIG_PCI */
6820c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
6821dbd82616STejun Heo EXPORT_SYMBOL_GPL(ata_link_abort);
6822c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_abort);
6823c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_freeze);
68247d77b247STejun Heo EXPORT_SYMBOL_GPL(sata_async_notification);
6825c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
6826c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
6827c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
6828c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
682910acf3b0SMark Lord EXPORT_SYMBOL_GPL(ata_eh_analyze_ncq_error);
6830c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_do_eh);
6831a1efdabaSTejun Heo EXPORT_SYMBOL_GPL(ata_std_error_handler);
6832be0d18dfSAlan Cox 
6833be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_40wire);
6834be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_80wire);
6835be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_unknown);
6836c88f90c3STejun Heo EXPORT_SYMBOL_GPL(ata_cable_ignore);
6837be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_sata);
6838