xref: /openbmc/linux/drivers/ata/libata-core.c (revision 9e8808a9)
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>
61c6fd2807SJeff Garzik #include <scsi/scsi.h>
62c6fd2807SJeff Garzik #include <scsi/scsi_cmnd.h>
63c6fd2807SJeff Garzik #include <scsi/scsi_host.h>
64c6fd2807SJeff Garzik #include <linux/libata.h>
65c6fd2807SJeff Garzik #include <asm/byteorder.h>
66140b5e59STejun Heo #include <linux/cdrom.h>
67c6fd2807SJeff Garzik 
68c6fd2807SJeff Garzik #include "libata.h"
69c6fd2807SJeff Garzik 
70fda0efc5SJeff Garzik 
71c6fd2807SJeff Garzik /* debounce timing parameters in msecs { interval, duration, timeout } */
72c6fd2807SJeff Garzik const unsigned long sata_deb_timing_normal[]		= {   5,  100, 2000 };
73c6fd2807SJeff Garzik const unsigned long sata_deb_timing_hotplug[]		= {  25,  500, 2000 };
74c6fd2807SJeff Garzik const unsigned long sata_deb_timing_long[]		= { 100, 2000, 5000 };
75c6fd2807SJeff Garzik 
76029cfd6bSTejun Heo const struct ata_port_operations ata_base_port_ops = {
770aa1113dSTejun Heo 	.prereset		= ata_std_prereset,
78203c75b8STejun Heo 	.postreset		= ata_std_postreset,
79a1efdabaSTejun Heo 	.error_handler		= ata_std_error_handler,
80029cfd6bSTejun Heo };
81029cfd6bSTejun Heo 
82029cfd6bSTejun Heo const struct ata_port_operations sata_port_ops = {
83029cfd6bSTejun Heo 	.inherits		= &ata_base_port_ops,
84029cfd6bSTejun Heo 
85029cfd6bSTejun Heo 	.qc_defer		= ata_std_qc_defer,
8657c9efdfSTejun Heo 	.hardreset		= sata_std_hardreset,
87029cfd6bSTejun Heo };
88029cfd6bSTejun Heo 
89c6fd2807SJeff Garzik static unsigned int ata_dev_init_params(struct ata_device *dev,
90c6fd2807SJeff Garzik 					u16 heads, u16 sectors);
91c6fd2807SJeff Garzik static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
92218f3d30SJeff Garzik static unsigned int ata_dev_set_feature(struct ata_device *dev,
93218f3d30SJeff Garzik 					u8 enable, u8 feature);
94c6fd2807SJeff Garzik static void ata_dev_xfermask(struct ata_device *dev);
9575683fe7STejun Heo static unsigned long ata_dev_blacklisted(const struct ata_device *dev);
96c6fd2807SJeff Garzik 
97f3187195STejun Heo unsigned int ata_print_id = 1;
98c6fd2807SJeff Garzik static struct workqueue_struct *ata_wq;
99c6fd2807SJeff Garzik 
100c6fd2807SJeff Garzik struct workqueue_struct *ata_aux_wq;
101c6fd2807SJeff Garzik 
10233267325STejun Heo struct ata_force_param {
10333267325STejun Heo 	const char	*name;
10433267325STejun Heo 	unsigned int	cbl;
10533267325STejun Heo 	int		spd_limit;
10633267325STejun Heo 	unsigned long	xfer_mask;
10733267325STejun Heo 	unsigned int	horkage_on;
10833267325STejun Heo 	unsigned int	horkage_off;
10905944bdfSTejun Heo 	unsigned int	lflags;
11033267325STejun Heo };
11133267325STejun Heo 
11233267325STejun Heo struct ata_force_ent {
11333267325STejun Heo 	int			port;
11433267325STejun Heo 	int			device;
11533267325STejun Heo 	struct ata_force_param	param;
11633267325STejun Heo };
11733267325STejun Heo 
11833267325STejun Heo static struct ata_force_ent *ata_force_tbl;
11933267325STejun Heo static int ata_force_tbl_size;
12033267325STejun Heo 
12133267325STejun Heo static char ata_force_param_buf[PAGE_SIZE] __initdata;
1227afb4222STejun Heo /* param_buf is thrown away after initialization, disallow read */
1237afb4222STejun Heo module_param_string(force, ata_force_param_buf, sizeof(ata_force_param_buf), 0);
12433267325STejun Heo MODULE_PARM_DESC(force, "Force ATA configurations including cable type, link speed and transfer mode (see Documentation/kernel-parameters.txt for details)");
12533267325STejun Heo 
1262486fa56STejun Heo static int atapi_enabled = 1;
127c6fd2807SJeff Garzik module_param(atapi_enabled, int, 0444);
128ad5d8eacSEvgeni Golov MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on [default])");
129c6fd2807SJeff Garzik 
130c5c61bdaSAdrian Bunk static int atapi_dmadir = 0;
131c6fd2807SJeff Garzik module_param(atapi_dmadir, int, 0444);
132ad5d8eacSEvgeni Golov MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off [default], 1=on)");
133c6fd2807SJeff Garzik 
134baf4fdfaSMark Lord int atapi_passthru16 = 1;
135baf4fdfaSMark Lord module_param(atapi_passthru16, int, 0444);
136ad5d8eacSEvgeni Golov MODULE_PARM_DESC(atapi_passthru16, "Enable ATA_16 passthru for ATAPI devices (0=off, 1=on [default])");
137baf4fdfaSMark Lord 
138c6fd2807SJeff Garzik int libata_fua = 0;
139c6fd2807SJeff Garzik module_param_named(fua, libata_fua, int, 0444);
140ad5d8eacSEvgeni Golov MODULE_PARM_DESC(fua, "FUA support (0=off [default], 1=on)");
141c6fd2807SJeff Garzik 
1422dcb407eSJeff Garzik static int ata_ignore_hpa;
1431e999736SAlan Cox module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644);
1441e999736SAlan Cox MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)");
1451e999736SAlan Cox 
146b3a70601SAlan Cox static int libata_dma_mask = ATA_DMA_MASK_ATA|ATA_DMA_MASK_ATAPI|ATA_DMA_MASK_CFA;
147b3a70601SAlan Cox module_param_named(dma, libata_dma_mask, int, 0444);
148b3a70601SAlan Cox MODULE_PARM_DESC(dma, "DMA enable/disable (0x1==ATA, 0x2==ATAPI, 0x4==CF)");
149b3a70601SAlan Cox 
15087fbc5a0STejun Heo static int ata_probe_timeout;
151c6fd2807SJeff Garzik module_param(ata_probe_timeout, int, 0444);
152c6fd2807SJeff Garzik MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
153c6fd2807SJeff Garzik 
1546ebe9d86SJeff Garzik int libata_noacpi = 0;
155d7d0dad6SJeff Garzik module_param_named(noacpi, libata_noacpi, int, 0444);
156ad5d8eacSEvgeni Golov MODULE_PARM_DESC(noacpi, "Disable the use of ACPI in probe/suspend/resume (0=off [default], 1=on)");
15711ef697bSKristen Carlson Accardi 
158ae8d4ee7SAlan Cox int libata_allow_tpm = 0;
159ae8d4ee7SAlan Cox module_param_named(allow_tpm, libata_allow_tpm, int, 0444);
160ad5d8eacSEvgeni Golov MODULE_PARM_DESC(allow_tpm, "Permit the use of TPM commands (0=off [default], 1=on)");
161ae8d4ee7SAlan Cox 
162c6fd2807SJeff Garzik MODULE_AUTHOR("Jeff Garzik");
163c6fd2807SJeff Garzik MODULE_DESCRIPTION("Library module for ATA devices");
164c6fd2807SJeff Garzik MODULE_LICENSE("GPL");
165c6fd2807SJeff Garzik MODULE_VERSION(DRV_VERSION);
166c6fd2807SJeff Garzik 
167c6fd2807SJeff Garzik 
1689913ff8aSTejun Heo static bool ata_sstatus_online(u32 sstatus)
1699913ff8aSTejun Heo {
1709913ff8aSTejun Heo 	return (sstatus & 0xf) == 0x3;
1719913ff8aSTejun Heo }
1729913ff8aSTejun Heo 
1731eca4365STejun Heo /**
1741eca4365STejun Heo  *	ata_link_next - link iteration helper
1751eca4365STejun Heo  *	@link: the previous link, NULL to start
1761eca4365STejun Heo  *	@ap: ATA port containing links to iterate
1771eca4365STejun Heo  *	@mode: iteration mode, one of ATA_LITER_*
178aadffb68STejun Heo  *
179aadffb68STejun Heo  *	LOCKING:
180aadffb68STejun Heo  *	Host lock or EH context.
1811eca4365STejun Heo  *
1821eca4365STejun Heo  *	RETURNS:
1831eca4365STejun Heo  *	Pointer to the next link.
184aadffb68STejun Heo  */
1851eca4365STejun Heo struct ata_link *ata_link_next(struct ata_link *link, struct ata_port *ap,
1861eca4365STejun Heo 			       enum ata_link_iter_mode mode)
187aadffb68STejun Heo {
1881eca4365STejun Heo 	BUG_ON(mode != ATA_LITER_EDGE &&
1891eca4365STejun Heo 	       mode != ATA_LITER_PMP_FIRST && mode != ATA_LITER_HOST_FIRST);
1901eca4365STejun Heo 
191aadffb68STejun Heo 	/* NULL link indicates start of iteration */
1921eca4365STejun Heo 	if (!link)
1931eca4365STejun Heo 		switch (mode) {
1941eca4365STejun Heo 		case ATA_LITER_EDGE:
1951eca4365STejun Heo 		case ATA_LITER_PMP_FIRST:
1961eca4365STejun Heo 			if (sata_pmp_attached(ap))
197aadffb68STejun Heo 				return ap->pmp_link;
1981eca4365STejun Heo 			/* fall through */
1991eca4365STejun Heo 		case ATA_LITER_HOST_FIRST:
200aadffb68STejun Heo 			return &ap->link;
201aadffb68STejun Heo 		}
202aadffb68STejun Heo 
2031eca4365STejun Heo 	/* we just iterated over the host link, what's next? */
2041eca4365STejun Heo 	if (link == &ap->link)
2051eca4365STejun Heo 		switch (mode) {
2061eca4365STejun Heo 		case ATA_LITER_HOST_FIRST:
2071eca4365STejun Heo 			if (sata_pmp_attached(ap))
208aadffb68STejun Heo 				return ap->pmp_link;
2091eca4365STejun Heo 			/* fall through */
2101eca4365STejun Heo 		case ATA_LITER_PMP_FIRST:
2111eca4365STejun Heo 			if (unlikely(ap->slave_link))
2121eca4365STejun Heo 				return ap->slave_link;
2131eca4365STejun Heo 			/* fall through */
2141eca4365STejun Heo 		case ATA_LITER_EDGE:
2151eca4365STejun Heo 			return NULL;
216aadffb68STejun Heo 		}
217aadffb68STejun Heo 
218b1c72916STejun Heo 	/* slave_link excludes PMP */
219b1c72916STejun Heo 	if (unlikely(link == ap->slave_link))
220b1c72916STejun Heo 		return NULL;
221b1c72916STejun Heo 
2221eca4365STejun Heo 	/* we were over a PMP link */
223aadffb68STejun Heo 	if (++link < ap->pmp_link + ap->nr_pmp_links)
224aadffb68STejun Heo 		return link;
2251eca4365STejun Heo 
2261eca4365STejun Heo 	if (mode == ATA_LITER_PMP_FIRST)
2271eca4365STejun Heo 		return &ap->link;
2281eca4365STejun Heo 
229aadffb68STejun Heo 	return NULL;
230aadffb68STejun Heo }
231aadffb68STejun Heo 
232c6fd2807SJeff Garzik /**
2331eca4365STejun Heo  *	ata_dev_next - device iteration helper
2341eca4365STejun Heo  *	@dev: the previous device, NULL to start
2351eca4365STejun Heo  *	@link: ATA link containing devices to iterate
2361eca4365STejun Heo  *	@mode: iteration mode, one of ATA_DITER_*
2371eca4365STejun Heo  *
2381eca4365STejun Heo  *	LOCKING:
2391eca4365STejun Heo  *	Host lock or EH context.
2401eca4365STejun Heo  *
2411eca4365STejun Heo  *	RETURNS:
2421eca4365STejun Heo  *	Pointer to the next device.
2431eca4365STejun Heo  */
2441eca4365STejun Heo struct ata_device *ata_dev_next(struct ata_device *dev, struct ata_link *link,
2451eca4365STejun Heo 				enum ata_dev_iter_mode mode)
2461eca4365STejun Heo {
2471eca4365STejun Heo 	BUG_ON(mode != ATA_DITER_ENABLED && mode != ATA_DITER_ENABLED_REVERSE &&
2481eca4365STejun Heo 	       mode != ATA_DITER_ALL && mode != ATA_DITER_ALL_REVERSE);
2491eca4365STejun Heo 
2501eca4365STejun Heo 	/* NULL dev indicates start of iteration */
2511eca4365STejun Heo 	if (!dev)
2521eca4365STejun Heo 		switch (mode) {
2531eca4365STejun Heo 		case ATA_DITER_ENABLED:
2541eca4365STejun Heo 		case ATA_DITER_ALL:
2551eca4365STejun Heo 			dev = link->device;
2561eca4365STejun Heo 			goto check;
2571eca4365STejun Heo 		case ATA_DITER_ENABLED_REVERSE:
2581eca4365STejun Heo 		case ATA_DITER_ALL_REVERSE:
2591eca4365STejun Heo 			dev = link->device + ata_link_max_devices(link) - 1;
2601eca4365STejun Heo 			goto check;
2611eca4365STejun Heo 		}
2621eca4365STejun Heo 
2631eca4365STejun Heo  next:
2641eca4365STejun Heo 	/* move to the next one */
2651eca4365STejun Heo 	switch (mode) {
2661eca4365STejun Heo 	case ATA_DITER_ENABLED:
2671eca4365STejun Heo 	case ATA_DITER_ALL:
2681eca4365STejun Heo 		if (++dev < link->device + ata_link_max_devices(link))
2691eca4365STejun Heo 			goto check;
2701eca4365STejun Heo 		return NULL;
2711eca4365STejun Heo 	case ATA_DITER_ENABLED_REVERSE:
2721eca4365STejun Heo 	case ATA_DITER_ALL_REVERSE:
2731eca4365STejun Heo 		if (--dev >= link->device)
2741eca4365STejun Heo 			goto check;
2751eca4365STejun Heo 		return NULL;
2761eca4365STejun Heo 	}
2771eca4365STejun Heo 
2781eca4365STejun Heo  check:
2791eca4365STejun Heo 	if ((mode == ATA_DITER_ENABLED || mode == ATA_DITER_ENABLED_REVERSE) &&
2801eca4365STejun Heo 	    !ata_dev_enabled(dev))
2811eca4365STejun Heo 		goto next;
2821eca4365STejun Heo 	return dev;
2831eca4365STejun Heo }
2841eca4365STejun Heo 
2851eca4365STejun Heo /**
286b1c72916STejun Heo  *	ata_dev_phys_link - find physical link for a device
287b1c72916STejun Heo  *	@dev: ATA device to look up physical link for
288b1c72916STejun Heo  *
289b1c72916STejun Heo  *	Look up physical link which @dev is attached to.  Note that
290b1c72916STejun Heo  *	this is different from @dev->link only when @dev is on slave
291b1c72916STejun Heo  *	link.  For all other cases, it's the same as @dev->link.
292b1c72916STejun Heo  *
293b1c72916STejun Heo  *	LOCKING:
294b1c72916STejun Heo  *	Don't care.
295b1c72916STejun Heo  *
296b1c72916STejun Heo  *	RETURNS:
297b1c72916STejun Heo  *	Pointer to the found physical link.
298b1c72916STejun Heo  */
299b1c72916STejun Heo struct ata_link *ata_dev_phys_link(struct ata_device *dev)
300b1c72916STejun Heo {
301b1c72916STejun Heo 	struct ata_port *ap = dev->link->ap;
302b1c72916STejun Heo 
303b1c72916STejun Heo 	if (!ap->slave_link)
304b1c72916STejun Heo 		return dev->link;
305b1c72916STejun Heo 	if (!dev->devno)
306b1c72916STejun Heo 		return &ap->link;
307b1c72916STejun Heo 	return ap->slave_link;
308b1c72916STejun Heo }
309b1c72916STejun Heo 
310b1c72916STejun Heo /**
31133267325STejun Heo  *	ata_force_cbl - force cable type according to libata.force
3124cdfa1b3SRandy Dunlap  *	@ap: ATA port of interest
31333267325STejun Heo  *
31433267325STejun Heo  *	Force cable type according to libata.force and whine about it.
31533267325STejun Heo  *	The last entry which has matching port number is used, so it
31633267325STejun Heo  *	can be specified as part of device force parameters.  For
31733267325STejun Heo  *	example, both "a:40c,1.00:udma4" and "1.00:40c,udma4" have the
31833267325STejun Heo  *	same effect.
31933267325STejun Heo  *
32033267325STejun Heo  *	LOCKING:
32133267325STejun Heo  *	EH context.
32233267325STejun Heo  */
32333267325STejun Heo void ata_force_cbl(struct ata_port *ap)
32433267325STejun Heo {
32533267325STejun Heo 	int i;
32633267325STejun Heo 
32733267325STejun Heo 	for (i = ata_force_tbl_size - 1; i >= 0; i--) {
32833267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
32933267325STejun Heo 
33033267325STejun Heo 		if (fe->port != -1 && fe->port != ap->print_id)
33133267325STejun Heo 			continue;
33233267325STejun Heo 
33333267325STejun Heo 		if (fe->param.cbl == ATA_CBL_NONE)
33433267325STejun Heo 			continue;
33533267325STejun Heo 
33633267325STejun Heo 		ap->cbl = fe->param.cbl;
33733267325STejun Heo 		ata_port_printk(ap, KERN_NOTICE,
33833267325STejun Heo 				"FORCE: cable set to %s\n", fe->param.name);
33933267325STejun Heo 		return;
34033267325STejun Heo 	}
34133267325STejun Heo }
34233267325STejun Heo 
34333267325STejun Heo /**
34405944bdfSTejun Heo  *	ata_force_link_limits - force link limits according to libata.force
34533267325STejun Heo  *	@link: ATA link of interest
34633267325STejun Heo  *
34705944bdfSTejun Heo  *	Force link flags and SATA spd limit according to libata.force
34805944bdfSTejun Heo  *	and whine about it.  When only the port part is specified
34905944bdfSTejun Heo  *	(e.g. 1:), the limit applies to all links connected to both
35005944bdfSTejun Heo  *	the host link and all fan-out ports connected via PMP.  If the
35105944bdfSTejun Heo  *	device part is specified as 0 (e.g. 1.00:), it specifies the
35205944bdfSTejun Heo  *	first fan-out link not the host link.  Device number 15 always
353b1c72916STejun Heo  *	points to the host link whether PMP is attached or not.  If the
354b1c72916STejun Heo  *	controller has slave link, device number 16 points to it.
35533267325STejun Heo  *
35633267325STejun Heo  *	LOCKING:
35733267325STejun Heo  *	EH context.
35833267325STejun Heo  */
35905944bdfSTejun Heo static void ata_force_link_limits(struct ata_link *link)
36033267325STejun Heo {
36105944bdfSTejun Heo 	bool did_spd = false;
362b1c72916STejun Heo 	int linkno = link->pmp;
363b1c72916STejun Heo 	int i;
36433267325STejun Heo 
36533267325STejun Heo 	if (ata_is_host_link(link))
366b1c72916STejun Heo 		linkno += 15;
36733267325STejun Heo 
36833267325STejun Heo 	for (i = ata_force_tbl_size - 1; i >= 0; i--) {
36933267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
37033267325STejun Heo 
37133267325STejun Heo 		if (fe->port != -1 && fe->port != link->ap->print_id)
37233267325STejun Heo 			continue;
37333267325STejun Heo 
37433267325STejun Heo 		if (fe->device != -1 && fe->device != linkno)
37533267325STejun Heo 			continue;
37633267325STejun Heo 
37705944bdfSTejun Heo 		/* only honor the first spd limit */
37805944bdfSTejun Heo 		if (!did_spd && fe->param.spd_limit) {
37933267325STejun Heo 			link->hw_sata_spd_limit = (1 << fe->param.spd_limit) - 1;
38033267325STejun Heo 			ata_link_printk(link, KERN_NOTICE,
38105944bdfSTejun Heo 					"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;
38905944bdfSTejun Heo 			ata_link_printk(link, KERN_NOTICE,
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 
44433267325STejun Heo 		ata_dev_printk(dev, KERN_NOTICE,
44533267325STejun Heo 			"FORCE: xfer_mask set to %s\n", 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 
48833267325STejun Heo 		ata_dev_printk(dev, KERN_NOTICE,
48933267325STejun Heo 			"FORCE: horkage modified (%s)\n", 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) {
713ac8672eaSTejun Heo 			ata_dev_printk(dev, KERN_WARNING, "device reported "
714ac8672eaSTejun Heo 				       "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 
1017c6fd2807SJeff Garzik static 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 
1030ca77329fSKristen Carlson Accardi static int ata_dev_set_dipm(struct ata_device *dev, enum link_pm policy)
1031ca77329fSKristen Carlson Accardi {
1032ca77329fSKristen Carlson Accardi 	struct ata_link *link = dev->link;
1033ca77329fSKristen Carlson Accardi 	struct ata_port *ap = link->ap;
1034ca77329fSKristen Carlson Accardi 	u32 scontrol;
1035ca77329fSKristen Carlson Accardi 	unsigned int err_mask;
1036ca77329fSKristen Carlson Accardi 	int rc;
1037ca77329fSKristen Carlson Accardi 
1038ca77329fSKristen Carlson Accardi 	/*
1039ca77329fSKristen Carlson Accardi 	 * disallow DIPM for drivers which haven't set
1040ca77329fSKristen Carlson Accardi 	 * ATA_FLAG_IPM.  This is because when DIPM is enabled,
1041ca77329fSKristen Carlson Accardi 	 * phy ready will be set in the interrupt status on
1042ca77329fSKristen Carlson Accardi 	 * state changes, which will cause some drivers to
1043ca77329fSKristen Carlson Accardi 	 * think there are errors - additionally drivers will
1044ca77329fSKristen Carlson Accardi 	 * need to disable hot plug.
1045ca77329fSKristen Carlson Accardi 	 */
1046ca77329fSKristen Carlson Accardi 	if (!(ap->flags & ATA_FLAG_IPM) || !ata_dev_enabled(dev)) {
1047ca77329fSKristen Carlson Accardi 		ap->pm_policy = NOT_AVAILABLE;
1048ca77329fSKristen Carlson Accardi 		return -EINVAL;
1049ca77329fSKristen Carlson Accardi 	}
1050ca77329fSKristen Carlson Accardi 
1051ca77329fSKristen Carlson Accardi 	/*
1052ca77329fSKristen Carlson Accardi 	 * For DIPM, we will only enable it for the
1053ca77329fSKristen Carlson Accardi 	 * min_power setting.
1054ca77329fSKristen Carlson Accardi 	 *
1055ca77329fSKristen Carlson Accardi 	 * Why?  Because Disks are too stupid to know that
1056ca77329fSKristen Carlson Accardi 	 * If the host rejects a request to go to SLUMBER
1057ca77329fSKristen Carlson Accardi 	 * they should retry at PARTIAL, and instead it
1058ca77329fSKristen Carlson Accardi 	 * just would give up.  So, for medium_power to
1059ca77329fSKristen Carlson Accardi 	 * work at all, we need to only allow HIPM.
1060ca77329fSKristen Carlson Accardi 	 */
1061ca77329fSKristen Carlson Accardi 	rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
1062ca77329fSKristen Carlson Accardi 	if (rc)
1063ca77329fSKristen Carlson Accardi 		return rc;
1064ca77329fSKristen Carlson Accardi 
1065ca77329fSKristen Carlson Accardi 	switch (policy) {
1066ca77329fSKristen Carlson Accardi 	case MIN_POWER:
1067ca77329fSKristen Carlson Accardi 		/* no restrictions on IPM transitions */
1068ca77329fSKristen Carlson Accardi 		scontrol &= ~(0x3 << 8);
1069ca77329fSKristen Carlson Accardi 		rc = sata_scr_write(link, SCR_CONTROL, scontrol);
1070ca77329fSKristen Carlson Accardi 		if (rc)
1071ca77329fSKristen Carlson Accardi 			return rc;
1072ca77329fSKristen Carlson Accardi 
1073ca77329fSKristen Carlson Accardi 		/* enable DIPM */
1074ca77329fSKristen Carlson Accardi 		if (dev->flags & ATA_DFLAG_DIPM)
1075ca77329fSKristen Carlson Accardi 			err_mask = ata_dev_set_feature(dev,
1076ca77329fSKristen Carlson Accardi 					SETFEATURES_SATA_ENABLE, SATA_DIPM);
1077ca77329fSKristen Carlson Accardi 		break;
1078ca77329fSKristen Carlson Accardi 	case MEDIUM_POWER:
1079ca77329fSKristen Carlson Accardi 		/* allow IPM to PARTIAL */
1080ca77329fSKristen Carlson Accardi 		scontrol &= ~(0x1 << 8);
1081ca77329fSKristen Carlson Accardi 		scontrol |= (0x2 << 8);
1082ca77329fSKristen Carlson Accardi 		rc = sata_scr_write(link, SCR_CONTROL, scontrol);
1083ca77329fSKristen Carlson Accardi 		if (rc)
1084ca77329fSKristen Carlson Accardi 			return rc;
1085ca77329fSKristen Carlson Accardi 
1086f5456b63SKristen Carlson Accardi 		/*
1087f5456b63SKristen Carlson Accardi 		 * we don't have to disable DIPM since IPM flags
1088f5456b63SKristen Carlson Accardi 		 * disallow transitions to SLUMBER, which effectively
1089f5456b63SKristen Carlson Accardi 		 * disable DIPM if it does not support PARTIAL
1090f5456b63SKristen Carlson Accardi 		 */
1091ca77329fSKristen Carlson Accardi 		break;
1092ca77329fSKristen Carlson Accardi 	case NOT_AVAILABLE:
1093ca77329fSKristen Carlson Accardi 	case MAX_PERFORMANCE:
1094ca77329fSKristen Carlson Accardi 		/* disable all IPM transitions */
1095ca77329fSKristen Carlson Accardi 		scontrol |= (0x3 << 8);
1096ca77329fSKristen Carlson Accardi 		rc = sata_scr_write(link, SCR_CONTROL, scontrol);
1097ca77329fSKristen Carlson Accardi 		if (rc)
1098ca77329fSKristen Carlson Accardi 			return rc;
1099ca77329fSKristen Carlson Accardi 
1100f5456b63SKristen Carlson Accardi 		/*
1101f5456b63SKristen Carlson Accardi 		 * we don't have to disable DIPM since IPM flags
1102f5456b63SKristen Carlson Accardi 		 * disallow all transitions which effectively
1103f5456b63SKristen Carlson Accardi 		 * disable DIPM anyway.
1104f5456b63SKristen Carlson Accardi 		 */
1105ca77329fSKristen Carlson Accardi 		break;
1106ca77329fSKristen Carlson Accardi 	}
1107ca77329fSKristen Carlson Accardi 
1108ca77329fSKristen Carlson Accardi 	/* FIXME: handle SET FEATURES failure */
1109ca77329fSKristen Carlson Accardi 	(void) err_mask;
1110ca77329fSKristen Carlson Accardi 
1111ca77329fSKristen Carlson Accardi 	return 0;
1112ca77329fSKristen Carlson Accardi }
1113ca77329fSKristen Carlson Accardi 
1114ca77329fSKristen Carlson Accardi /**
1115ca77329fSKristen Carlson Accardi  *	ata_dev_enable_pm - enable SATA interface power management
111648166fd9SStephen Hemminger  *	@dev:  device to enable power management
111748166fd9SStephen Hemminger  *	@policy: the link power management policy
1118ca77329fSKristen Carlson Accardi  *
1119ca77329fSKristen Carlson Accardi  *	Enable SATA Interface power management.  This will enable
1120ca77329fSKristen Carlson Accardi  *	Device Interface Power Management (DIPM) for min_power
1121ca77329fSKristen Carlson Accardi  * 	policy, and then call driver specific callbacks for
1122ca77329fSKristen Carlson Accardi  *	enabling Host Initiated Power management.
1123ca77329fSKristen Carlson Accardi  *
1124ca77329fSKristen Carlson Accardi  *	Locking: Caller.
1125ca77329fSKristen Carlson Accardi  *	Returns: -EINVAL if IPM is not supported, 0 otherwise.
1126ca77329fSKristen Carlson Accardi  */
1127ca77329fSKristen Carlson Accardi void ata_dev_enable_pm(struct ata_device *dev, enum link_pm policy)
1128ca77329fSKristen Carlson Accardi {
1129ca77329fSKristen Carlson Accardi 	int rc = 0;
1130ca77329fSKristen Carlson Accardi 	struct ata_port *ap = dev->link->ap;
1131ca77329fSKristen Carlson Accardi 
1132ca77329fSKristen Carlson Accardi 	/* set HIPM first, then DIPM */
1133ca77329fSKristen Carlson Accardi 	if (ap->ops->enable_pm)
1134ca77329fSKristen Carlson Accardi 		rc = ap->ops->enable_pm(ap, policy);
1135ca77329fSKristen Carlson Accardi 	if (rc)
1136ca77329fSKristen Carlson Accardi 		goto enable_pm_out;
1137ca77329fSKristen Carlson Accardi 	rc = ata_dev_set_dipm(dev, policy);
1138ca77329fSKristen Carlson Accardi 
1139ca77329fSKristen Carlson Accardi enable_pm_out:
1140ca77329fSKristen Carlson Accardi 	if (rc)
1141ca77329fSKristen Carlson Accardi 		ap->pm_policy = MAX_PERFORMANCE;
1142ca77329fSKristen Carlson Accardi 	else
1143ca77329fSKristen Carlson Accardi 		ap->pm_policy = policy;
1144ca77329fSKristen Carlson Accardi 	return /* rc */;	/* hopefully we can use 'rc' eventually */
1145ca77329fSKristen Carlson Accardi }
1146ca77329fSKristen Carlson Accardi 
11471992a5edSStephen Rothwell #ifdef CONFIG_PM
1148ca77329fSKristen Carlson Accardi /**
1149ca77329fSKristen Carlson Accardi  *	ata_dev_disable_pm - disable SATA interface power management
115048166fd9SStephen Hemminger  *	@dev: device to disable power management
1151ca77329fSKristen Carlson Accardi  *
1152ca77329fSKristen Carlson Accardi  *	Disable SATA Interface power management.  This will disable
1153ca77329fSKristen Carlson Accardi  *	Device Interface Power Management (DIPM) without changing
1154ca77329fSKristen Carlson Accardi  * 	policy,  call driver specific callbacks for disabling Host
1155ca77329fSKristen Carlson Accardi  * 	Initiated Power management.
1156ca77329fSKristen Carlson Accardi  *
1157ca77329fSKristen Carlson Accardi  *	Locking: Caller.
1158ca77329fSKristen Carlson Accardi  *	Returns: void
1159ca77329fSKristen Carlson Accardi  */
1160ca77329fSKristen Carlson Accardi static void ata_dev_disable_pm(struct ata_device *dev)
1161ca77329fSKristen Carlson Accardi {
1162ca77329fSKristen Carlson Accardi 	struct ata_port *ap = dev->link->ap;
1163ca77329fSKristen Carlson Accardi 
1164ca77329fSKristen Carlson Accardi 	ata_dev_set_dipm(dev, MAX_PERFORMANCE);
1165ca77329fSKristen Carlson Accardi 	if (ap->ops->disable_pm)
1166ca77329fSKristen Carlson Accardi 		ap->ops->disable_pm(ap);
1167ca77329fSKristen Carlson Accardi }
11681992a5edSStephen Rothwell #endif	/* CONFIG_PM */
1169ca77329fSKristen Carlson Accardi 
1170ca77329fSKristen Carlson Accardi void ata_lpm_schedule(struct ata_port *ap, enum link_pm policy)
1171ca77329fSKristen Carlson Accardi {
1172ca77329fSKristen Carlson Accardi 	ap->pm_policy = policy;
11733ec25ebdSTejun Heo 	ap->link.eh_info.action |= ATA_EH_LPM;
1174ca77329fSKristen Carlson Accardi 	ap->link.eh_info.flags |= ATA_EHI_NO_AUTOPSY;
1175ca77329fSKristen Carlson Accardi 	ata_port_schedule_eh(ap);
1176ca77329fSKristen Carlson Accardi }
1177ca77329fSKristen Carlson Accardi 
11781992a5edSStephen Rothwell #ifdef CONFIG_PM
1179ca77329fSKristen Carlson Accardi static void ata_lpm_enable(struct ata_host *host)
1180ca77329fSKristen Carlson Accardi {
1181ca77329fSKristen Carlson Accardi 	struct ata_link *link;
1182ca77329fSKristen Carlson Accardi 	struct ata_port *ap;
1183ca77329fSKristen Carlson Accardi 	struct ata_device *dev;
1184ca77329fSKristen Carlson Accardi 	int i;
1185ca77329fSKristen Carlson Accardi 
1186ca77329fSKristen Carlson Accardi 	for (i = 0; i < host->n_ports; i++) {
1187ca77329fSKristen Carlson Accardi 		ap = host->ports[i];
11881eca4365STejun Heo 		ata_for_each_link(link, ap, EDGE) {
11891eca4365STejun Heo 			ata_for_each_dev(dev, link, ALL)
1190ca77329fSKristen Carlson Accardi 				ata_dev_disable_pm(dev);
1191ca77329fSKristen Carlson Accardi 		}
1192ca77329fSKristen Carlson Accardi 	}
1193ca77329fSKristen Carlson Accardi }
1194ca77329fSKristen Carlson Accardi 
1195ca77329fSKristen Carlson Accardi static void ata_lpm_disable(struct ata_host *host)
1196ca77329fSKristen Carlson Accardi {
1197ca77329fSKristen Carlson Accardi 	int i;
1198ca77329fSKristen Carlson Accardi 
1199ca77329fSKristen Carlson Accardi 	for (i = 0; i < host->n_ports; i++) {
1200ca77329fSKristen Carlson Accardi 		struct ata_port *ap = host->ports[i];
1201ca77329fSKristen Carlson Accardi 		ata_lpm_schedule(ap, ap->pm_policy);
1202ca77329fSKristen Carlson Accardi 	}
1203ca77329fSKristen Carlson Accardi }
12041992a5edSStephen Rothwell #endif	/* CONFIG_PM */
1205ca77329fSKristen Carlson Accardi 
1206c6fd2807SJeff Garzik /**
1207c6fd2807SJeff Garzik  *	ata_dev_classify - determine device type based on ATA-spec signature
1208c6fd2807SJeff Garzik  *	@tf: ATA taskfile register set for device to be identified
1209c6fd2807SJeff Garzik  *
1210c6fd2807SJeff Garzik  *	Determine from taskfile register contents whether a device is
1211c6fd2807SJeff Garzik  *	ATA or ATAPI, as per "Signature and persistence" section
1212c6fd2807SJeff Garzik  *	of ATA/PI spec (volume 1, sect 5.14).
1213c6fd2807SJeff Garzik  *
1214c6fd2807SJeff Garzik  *	LOCKING:
1215c6fd2807SJeff Garzik  *	None.
1216c6fd2807SJeff Garzik  *
1217c6fd2807SJeff Garzik  *	RETURNS:
1218633273a3STejun Heo  *	Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP or
1219633273a3STejun Heo  *	%ATA_DEV_UNKNOWN the event of failure.
1220c6fd2807SJeff Garzik  */
1221c6fd2807SJeff Garzik unsigned int ata_dev_classify(const struct ata_taskfile *tf)
1222c6fd2807SJeff Garzik {
1223c6fd2807SJeff Garzik 	/* Apple's open source Darwin code hints that some devices only
1224c6fd2807SJeff Garzik 	 * put a proper signature into the LBA mid/high registers,
1225c6fd2807SJeff Garzik 	 * So, we only check those.  It's sufficient for uniqueness.
1226633273a3STejun Heo 	 *
1227633273a3STejun Heo 	 * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate
1228633273a3STejun Heo 	 * signatures for ATA and ATAPI devices attached on SerialATA,
1229633273a3STejun Heo 	 * 0x3c/0xc3 and 0x69/0x96 respectively.  However, SerialATA
1230633273a3STejun Heo 	 * spec has never mentioned about using different signatures
1231633273a3STejun Heo 	 * for ATA/ATAPI devices.  Then, Serial ATA II: Port
1232633273a3STejun Heo 	 * Multiplier specification began to use 0x69/0x96 to identify
1233633273a3STejun Heo 	 * port multpliers and 0x3c/0xc3 to identify SEMB device.
1234633273a3STejun Heo 	 * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and
1235633273a3STejun Heo 	 * 0x69/0x96 shortly and described them as reserved for
1236633273a3STejun Heo 	 * SerialATA.
1237633273a3STejun Heo 	 *
1238633273a3STejun Heo 	 * We follow the current spec and consider that 0x69/0x96
1239633273a3STejun Heo 	 * identifies a port multiplier and 0x3c/0xc3 a SEMB device.
124079b42babSTejun Heo 	 * Unfortunately, WDC WD1600JS-62MHB5 (a hard drive) reports
124179b42babSTejun Heo 	 * SEMB signature.  This is worked around in
124279b42babSTejun Heo 	 * ata_dev_read_id().
1243c6fd2807SJeff Garzik 	 */
1244633273a3STejun Heo 	if ((tf->lbam == 0) && (tf->lbah == 0)) {
1245c6fd2807SJeff Garzik 		DPRINTK("found ATA device by sig\n");
1246c6fd2807SJeff Garzik 		return ATA_DEV_ATA;
1247c6fd2807SJeff Garzik 	}
1248c6fd2807SJeff Garzik 
1249633273a3STejun Heo 	if ((tf->lbam == 0x14) && (tf->lbah == 0xeb)) {
1250c6fd2807SJeff Garzik 		DPRINTK("found ATAPI device by sig\n");
1251c6fd2807SJeff Garzik 		return ATA_DEV_ATAPI;
1252c6fd2807SJeff Garzik 	}
1253c6fd2807SJeff Garzik 
1254633273a3STejun Heo 	if ((tf->lbam == 0x69) && (tf->lbah == 0x96)) {
1255633273a3STejun Heo 		DPRINTK("found PMP device by sig\n");
1256633273a3STejun Heo 		return ATA_DEV_PMP;
1257633273a3STejun Heo 	}
1258633273a3STejun Heo 
1259633273a3STejun Heo 	if ((tf->lbam == 0x3c) && (tf->lbah == 0xc3)) {
126079b42babSTejun Heo 		DPRINTK("found SEMB device by sig (could be ATA device)\n");
126179b42babSTejun Heo 		return ATA_DEV_SEMB;
1262633273a3STejun Heo 	}
1263633273a3STejun Heo 
1264c6fd2807SJeff Garzik 	DPRINTK("unknown device\n");
1265c6fd2807SJeff Garzik 	return ATA_DEV_UNKNOWN;
1266c6fd2807SJeff Garzik }
1267c6fd2807SJeff Garzik 
1268c6fd2807SJeff Garzik /**
1269c6fd2807SJeff Garzik  *	ata_id_string - Convert IDENTIFY DEVICE page into string
1270c6fd2807SJeff Garzik  *	@id: IDENTIFY DEVICE results we will examine
1271c6fd2807SJeff Garzik  *	@s: string into which data is output
1272c6fd2807SJeff Garzik  *	@ofs: offset into identify device page
1273c6fd2807SJeff Garzik  *	@len: length of string to return. must be an even number.
1274c6fd2807SJeff Garzik  *
1275c6fd2807SJeff Garzik  *	The strings in the IDENTIFY DEVICE page are broken up into
1276c6fd2807SJeff Garzik  *	16-bit chunks.  Run through the string, and output each
1277c6fd2807SJeff Garzik  *	8-bit chunk linearly, regardless of platform.
1278c6fd2807SJeff Garzik  *
1279c6fd2807SJeff Garzik  *	LOCKING:
1280c6fd2807SJeff Garzik  *	caller.
1281c6fd2807SJeff Garzik  */
1282c6fd2807SJeff Garzik 
1283c6fd2807SJeff Garzik void ata_id_string(const u16 *id, unsigned char *s,
1284c6fd2807SJeff Garzik 		   unsigned int ofs, unsigned int len)
1285c6fd2807SJeff Garzik {
1286c6fd2807SJeff Garzik 	unsigned int c;
1287c6fd2807SJeff Garzik 
1288963e4975SAlan Cox 	BUG_ON(len & 1);
1289963e4975SAlan Cox 
1290c6fd2807SJeff Garzik 	while (len > 0) {
1291c6fd2807SJeff Garzik 		c = id[ofs] >> 8;
1292c6fd2807SJeff Garzik 		*s = c;
1293c6fd2807SJeff Garzik 		s++;
1294c6fd2807SJeff Garzik 
1295c6fd2807SJeff Garzik 		c = id[ofs] & 0xff;
1296c6fd2807SJeff Garzik 		*s = c;
1297c6fd2807SJeff Garzik 		s++;
1298c6fd2807SJeff Garzik 
1299c6fd2807SJeff Garzik 		ofs++;
1300c6fd2807SJeff Garzik 		len -= 2;
1301c6fd2807SJeff Garzik 	}
1302c6fd2807SJeff Garzik }
1303c6fd2807SJeff Garzik 
1304c6fd2807SJeff Garzik /**
1305c6fd2807SJeff Garzik  *	ata_id_c_string - Convert IDENTIFY DEVICE page into C string
1306c6fd2807SJeff Garzik  *	@id: IDENTIFY DEVICE results we will examine
1307c6fd2807SJeff Garzik  *	@s: string into which data is output
1308c6fd2807SJeff Garzik  *	@ofs: offset into identify device page
1309c6fd2807SJeff Garzik  *	@len: length of string to return. must be an odd number.
1310c6fd2807SJeff Garzik  *
1311c6fd2807SJeff Garzik  *	This function is identical to ata_id_string except that it
1312c6fd2807SJeff Garzik  *	trims trailing spaces and terminates the resulting string with
1313c6fd2807SJeff Garzik  *	null.  @len must be actual maximum length (even number) + 1.
1314c6fd2807SJeff Garzik  *
1315c6fd2807SJeff Garzik  *	LOCKING:
1316c6fd2807SJeff Garzik  *	caller.
1317c6fd2807SJeff Garzik  */
1318c6fd2807SJeff Garzik void ata_id_c_string(const u16 *id, unsigned char *s,
1319c6fd2807SJeff Garzik 		     unsigned int ofs, unsigned int len)
1320c6fd2807SJeff Garzik {
1321c6fd2807SJeff Garzik 	unsigned char *p;
1322c6fd2807SJeff Garzik 
1323c6fd2807SJeff Garzik 	ata_id_string(id, s, ofs, len - 1);
1324c6fd2807SJeff Garzik 
1325c6fd2807SJeff Garzik 	p = s + strnlen(s, len - 1);
1326c6fd2807SJeff Garzik 	while (p > s && p[-1] == ' ')
1327c6fd2807SJeff Garzik 		p--;
1328c6fd2807SJeff Garzik 	*p = '\0';
1329c6fd2807SJeff Garzik }
1330c6fd2807SJeff Garzik 
1331db6f8759STejun Heo static u64 ata_id_n_sectors(const u16 *id)
1332db6f8759STejun Heo {
1333db6f8759STejun Heo 	if (ata_id_has_lba(id)) {
1334db6f8759STejun Heo 		if (ata_id_has_lba48(id))
1335968e594aSRobert Hancock 			return ata_id_u64(id, ATA_ID_LBA_CAPACITY_2);
1336db6f8759STejun Heo 		else
1337968e594aSRobert Hancock 			return ata_id_u32(id, ATA_ID_LBA_CAPACITY);
1338db6f8759STejun Heo 	} else {
1339db6f8759STejun Heo 		if (ata_id_current_chs_valid(id))
1340968e594aSRobert Hancock 			return id[ATA_ID_CUR_CYLS] * id[ATA_ID_CUR_HEADS] *
1341968e594aSRobert Hancock 			       id[ATA_ID_CUR_SECTORS];
1342db6f8759STejun Heo 		else
1343968e594aSRobert Hancock 			return id[ATA_ID_CYLS] * id[ATA_ID_HEADS] *
1344968e594aSRobert Hancock 			       id[ATA_ID_SECTORS];
1345db6f8759STejun Heo 	}
1346db6f8759STejun Heo }
1347db6f8759STejun Heo 
1348a5987e0aSTejun Heo u64 ata_tf_to_lba48(const struct ata_taskfile *tf)
13491e999736SAlan Cox {
13501e999736SAlan Cox 	u64 sectors = 0;
13511e999736SAlan Cox 
13521e999736SAlan Cox 	sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40;
13531e999736SAlan Cox 	sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32;
1354ba14a9c2SRoland Dreier 	sectors |= ((u64)(tf->hob_lbal & 0xff)) << 24;
13551e999736SAlan Cox 	sectors |= (tf->lbah & 0xff) << 16;
13561e999736SAlan Cox 	sectors |= (tf->lbam & 0xff) << 8;
13571e999736SAlan Cox 	sectors |= (tf->lbal & 0xff);
13581e999736SAlan Cox 
1359a5987e0aSTejun Heo 	return sectors;
13601e999736SAlan Cox }
13611e999736SAlan Cox 
1362a5987e0aSTejun Heo u64 ata_tf_to_lba(const struct ata_taskfile *tf)
13631e999736SAlan Cox {
13641e999736SAlan Cox 	u64 sectors = 0;
13651e999736SAlan Cox 
13661e999736SAlan Cox 	sectors |= (tf->device & 0x0f) << 24;
13671e999736SAlan Cox 	sectors |= (tf->lbah & 0xff) << 16;
13681e999736SAlan Cox 	sectors |= (tf->lbam & 0xff) << 8;
13691e999736SAlan Cox 	sectors |= (tf->lbal & 0xff);
13701e999736SAlan Cox 
1371a5987e0aSTejun Heo 	return sectors;
13721e999736SAlan Cox }
13731e999736SAlan Cox 
13741e999736SAlan Cox /**
1375c728a914STejun Heo  *	ata_read_native_max_address - Read native max address
1376c728a914STejun Heo  *	@dev: target device
1377c728a914STejun Heo  *	@max_sectors: out parameter for the result native max address
13781e999736SAlan Cox  *
1379c728a914STejun Heo  *	Perform an LBA48 or LBA28 native size query upon the device in
1380c728a914STejun Heo  *	question.
1381c728a914STejun Heo  *
1382c728a914STejun Heo  *	RETURNS:
1383c728a914STejun Heo  *	0 on success, -EACCES if command is aborted by the drive.
1384c728a914STejun Heo  *	-EIO on other errors.
13851e999736SAlan Cox  */
1386c728a914STejun Heo static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
13871e999736SAlan Cox {
1388c728a914STejun Heo 	unsigned int err_mask;
13891e999736SAlan Cox 	struct ata_taskfile tf;
1390c728a914STejun Heo 	int lba48 = ata_id_has_lba48(dev->id);
13911e999736SAlan Cox 
13921e999736SAlan Cox 	ata_tf_init(dev, &tf);
13931e999736SAlan Cox 
1394c728a914STejun Heo 	/* always clear all address registers */
13951e999736SAlan Cox 	tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1396c728a914STejun Heo 
1397c728a914STejun Heo 	if (lba48) {
1398c728a914STejun Heo 		tf.command = ATA_CMD_READ_NATIVE_MAX_EXT;
1399c728a914STejun Heo 		tf.flags |= ATA_TFLAG_LBA48;
1400c728a914STejun Heo 	} else
1401c728a914STejun Heo 		tf.command = ATA_CMD_READ_NATIVE_MAX;
1402c728a914STejun Heo 
14031e999736SAlan Cox 	tf.protocol |= ATA_PROT_NODATA;
1404c728a914STejun Heo 	tf.device |= ATA_LBA;
14051e999736SAlan Cox 
14062b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1407c728a914STejun Heo 	if (err_mask) {
1408c728a914STejun Heo 		ata_dev_printk(dev, KERN_WARNING, "failed to read native "
1409c728a914STejun Heo 			       "max address (err_mask=0x%x)\n", err_mask);
1410c728a914STejun Heo 		if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
1411c728a914STejun Heo 			return -EACCES;
1412c728a914STejun Heo 		return -EIO;
1413c728a914STejun Heo 	}
1414c728a914STejun Heo 
1415c728a914STejun Heo 	if (lba48)
1416a5987e0aSTejun Heo 		*max_sectors = ata_tf_to_lba48(&tf) + 1;
1417c728a914STejun Heo 	else
1418a5987e0aSTejun Heo 		*max_sectors = ata_tf_to_lba(&tf) + 1;
141993328e11SAlan Cox 	if (dev->horkage & ATA_HORKAGE_HPA_SIZE)
142093328e11SAlan Cox 		(*max_sectors)--;
14211e999736SAlan Cox 	return 0;
14221e999736SAlan Cox }
14231e999736SAlan Cox 
14241e999736SAlan Cox /**
1425c728a914STejun Heo  *	ata_set_max_sectors - Set max sectors
1426c728a914STejun Heo  *	@dev: target device
14276b38d1d1SRandy Dunlap  *	@new_sectors: new max sectors value to set for the device
14281e999736SAlan Cox  *
1429c728a914STejun Heo  *	Set max sectors of @dev to @new_sectors.
1430c728a914STejun Heo  *
1431c728a914STejun Heo  *	RETURNS:
1432c728a914STejun Heo  *	0 on success, -EACCES if command is aborted or denied (due to
1433c728a914STejun Heo  *	previous non-volatile SET_MAX) by the drive.  -EIO on other
1434c728a914STejun Heo  *	errors.
14351e999736SAlan Cox  */
143605027adcSTejun Heo static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
14371e999736SAlan Cox {
1438c728a914STejun Heo 	unsigned int err_mask;
14391e999736SAlan Cox 	struct ata_taskfile tf;
1440c728a914STejun Heo 	int lba48 = ata_id_has_lba48(dev->id);
14411e999736SAlan Cox 
14421e999736SAlan Cox 	new_sectors--;
14431e999736SAlan Cox 
14441e999736SAlan Cox 	ata_tf_init(dev, &tf);
14451e999736SAlan Cox 
1446c728a914STejun Heo 	tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
14471e999736SAlan Cox 
1448c728a914STejun Heo 	if (lba48) {
1449c728a914STejun Heo 		tf.command = ATA_CMD_SET_MAX_EXT;
1450c728a914STejun Heo 		tf.flags |= ATA_TFLAG_LBA48;
14511e999736SAlan Cox 
14521e999736SAlan Cox 		tf.hob_lbal = (new_sectors >> 24) & 0xff;
14531e999736SAlan Cox 		tf.hob_lbam = (new_sectors >> 32) & 0xff;
14541e999736SAlan Cox 		tf.hob_lbah = (new_sectors >> 40) & 0xff;
14551e582ba4STejun Heo 	} else {
14561e999736SAlan Cox 		tf.command = ATA_CMD_SET_MAX;
1457c728a914STejun Heo 
14581e582ba4STejun Heo 		tf.device |= (new_sectors >> 24) & 0xf;
14591e582ba4STejun Heo 	}
14601e582ba4STejun Heo 
14611e999736SAlan Cox 	tf.protocol |= ATA_PROT_NODATA;
1462c728a914STejun Heo 	tf.device |= ATA_LBA;
14631e999736SAlan Cox 
14641e999736SAlan Cox 	tf.lbal = (new_sectors >> 0) & 0xff;
14651e999736SAlan Cox 	tf.lbam = (new_sectors >> 8) & 0xff;
14661e999736SAlan Cox 	tf.lbah = (new_sectors >> 16) & 0xff;
14671e999736SAlan Cox 
14682b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1469c728a914STejun Heo 	if (err_mask) {
1470c728a914STejun Heo 		ata_dev_printk(dev, KERN_WARNING, "failed to set "
1471c728a914STejun Heo 			       "max address (err_mask=0x%x)\n", err_mask);
1472c728a914STejun Heo 		if (err_mask == AC_ERR_DEV &&
1473c728a914STejun Heo 		    (tf.feature & (ATA_ABORTED | ATA_IDNF)))
1474c728a914STejun Heo 			return -EACCES;
1475c728a914STejun Heo 		return -EIO;
1476c728a914STejun Heo 	}
1477c728a914STejun Heo 
14781e999736SAlan Cox 	return 0;
14791e999736SAlan Cox }
14801e999736SAlan Cox 
14811e999736SAlan Cox /**
14821e999736SAlan Cox  *	ata_hpa_resize		-	Resize a device with an HPA set
14831e999736SAlan Cox  *	@dev: Device to resize
14841e999736SAlan Cox  *
14851e999736SAlan Cox  *	Read the size of an LBA28 or LBA48 disk with HPA features and resize
14861e999736SAlan Cox  *	it if required to the full size of the media. The caller must check
14871e999736SAlan Cox  *	the drive has the HPA feature set enabled.
148805027adcSTejun Heo  *
148905027adcSTejun Heo  *	RETURNS:
149005027adcSTejun Heo  *	0 on success, -errno on failure.
14911e999736SAlan Cox  */
149205027adcSTejun Heo static int ata_hpa_resize(struct ata_device *dev)
14931e999736SAlan Cox {
149405027adcSTejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
149505027adcSTejun Heo 	int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
149605027adcSTejun Heo 	u64 sectors = ata_id_n_sectors(dev->id);
149705027adcSTejun Heo 	u64 native_sectors;
1498c728a914STejun Heo 	int rc;
14991e999736SAlan Cox 
150005027adcSTejun Heo 	/* do we need to do it? */
150105027adcSTejun Heo 	if (dev->class != ATA_DEV_ATA ||
150205027adcSTejun Heo 	    !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) ||
150305027adcSTejun Heo 	    (dev->horkage & ATA_HORKAGE_BROKEN_HPA))
1504c728a914STejun Heo 		return 0;
15051e999736SAlan Cox 
150605027adcSTejun Heo 	/* read native max address */
150705027adcSTejun Heo 	rc = ata_read_native_max_address(dev, &native_sectors);
150805027adcSTejun Heo 	if (rc) {
1509dda7aba1STejun Heo 		/* If device aborted the command or HPA isn't going to
1510dda7aba1STejun Heo 		 * be unlocked, skip HPA resizing.
151105027adcSTejun Heo 		 */
1512dda7aba1STejun Heo 		if (rc == -EACCES || !ata_ignore_hpa) {
151305027adcSTejun Heo 			ata_dev_printk(dev, KERN_WARNING, "HPA support seems "
1514dda7aba1STejun Heo 				       "broken, skipping HPA handling\n");
151505027adcSTejun Heo 			dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
151605027adcSTejun Heo 
151705027adcSTejun Heo 			/* we can continue if device aborted the command */
151805027adcSTejun Heo 			if (rc == -EACCES)
151905027adcSTejun Heo 				rc = 0;
152005027adcSTejun Heo 		}
152105027adcSTejun Heo 
152205027adcSTejun Heo 		return rc;
152305027adcSTejun Heo 	}
15245920dadfSTejun Heo 	dev->n_native_sectors = native_sectors;
152505027adcSTejun Heo 
152605027adcSTejun Heo 	/* nothing to do? */
152705027adcSTejun Heo 	if (native_sectors <= sectors || !ata_ignore_hpa) {
152805027adcSTejun Heo 		if (!print_info || native_sectors == sectors)
152905027adcSTejun Heo 			return 0;
153005027adcSTejun Heo 
153105027adcSTejun Heo 		if (native_sectors > sectors)
15321e999736SAlan Cox 			ata_dev_printk(dev, KERN_INFO,
153305027adcSTejun Heo 				"HPA detected: current %llu, native %llu\n",
153405027adcSTejun Heo 				(unsigned long long)sectors,
153505027adcSTejun Heo 				(unsigned long long)native_sectors);
153605027adcSTejun Heo 		else if (native_sectors < sectors)
153705027adcSTejun Heo 			ata_dev_printk(dev, KERN_WARNING,
153805027adcSTejun Heo 				"native sectors (%llu) is smaller than "
153905027adcSTejun Heo 				"sectors (%llu)\n",
154005027adcSTejun Heo 				(unsigned long long)native_sectors,
154105027adcSTejun Heo 				(unsigned long long)sectors);
154205027adcSTejun Heo 		return 0;
15431e999736SAlan Cox 	}
154437301a55STejun Heo 
154505027adcSTejun Heo 	/* let's unlock HPA */
154605027adcSTejun Heo 	rc = ata_set_max_sectors(dev, native_sectors);
154705027adcSTejun Heo 	if (rc == -EACCES) {
154805027adcSTejun Heo 		/* if device aborted the command, skip HPA resizing */
154905027adcSTejun Heo 		ata_dev_printk(dev, KERN_WARNING, "device aborted resize "
155005027adcSTejun Heo 			       "(%llu -> %llu), skipping HPA handling\n",
155105027adcSTejun Heo 			       (unsigned long long)sectors,
155205027adcSTejun Heo 			       (unsigned long long)native_sectors);
155305027adcSTejun Heo 		dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
155405027adcSTejun Heo 		return 0;
155505027adcSTejun Heo 	} else if (rc)
155605027adcSTejun Heo 		return rc;
155705027adcSTejun Heo 
155805027adcSTejun Heo 	/* re-read IDENTIFY data */
155905027adcSTejun Heo 	rc = ata_dev_reread_id(dev, 0);
156005027adcSTejun Heo 	if (rc) {
156105027adcSTejun Heo 		ata_dev_printk(dev, KERN_ERR, "failed to re-read IDENTIFY "
156205027adcSTejun Heo 			       "data after HPA resizing\n");
156305027adcSTejun Heo 		return rc;
156405027adcSTejun Heo 	}
156505027adcSTejun Heo 
156605027adcSTejun Heo 	if (print_info) {
156705027adcSTejun Heo 		u64 new_sectors = ata_id_n_sectors(dev->id);
156805027adcSTejun Heo 		ata_dev_printk(dev, KERN_INFO,
156905027adcSTejun Heo 			"HPA unlocked: %llu -> %llu, native %llu\n",
157005027adcSTejun Heo 			(unsigned long long)sectors,
157105027adcSTejun Heo 			(unsigned long long)new_sectors,
157205027adcSTejun Heo 			(unsigned long long)native_sectors);
157305027adcSTejun Heo 	}
157405027adcSTejun Heo 
157505027adcSTejun Heo 	return 0;
15761e999736SAlan Cox }
15771e999736SAlan Cox 
1578c6fd2807SJeff Garzik /**
1579c6fd2807SJeff Garzik  *	ata_dump_id - IDENTIFY DEVICE info debugging output
1580c6fd2807SJeff Garzik  *	@id: IDENTIFY DEVICE page to dump
1581c6fd2807SJeff Garzik  *
1582c6fd2807SJeff Garzik  *	Dump selected 16-bit words from the given IDENTIFY DEVICE
1583c6fd2807SJeff Garzik  *	page.
1584c6fd2807SJeff Garzik  *
1585c6fd2807SJeff Garzik  *	LOCKING:
1586c6fd2807SJeff Garzik  *	caller.
1587c6fd2807SJeff Garzik  */
1588c6fd2807SJeff Garzik 
1589c6fd2807SJeff Garzik static inline void ata_dump_id(const u16 *id)
1590c6fd2807SJeff Garzik {
1591c6fd2807SJeff Garzik 	DPRINTK("49==0x%04x  "
1592c6fd2807SJeff Garzik 		"53==0x%04x  "
1593c6fd2807SJeff Garzik 		"63==0x%04x  "
1594c6fd2807SJeff Garzik 		"64==0x%04x  "
1595c6fd2807SJeff Garzik 		"75==0x%04x  \n",
1596c6fd2807SJeff Garzik 		id[49],
1597c6fd2807SJeff Garzik 		id[53],
1598c6fd2807SJeff Garzik 		id[63],
1599c6fd2807SJeff Garzik 		id[64],
1600c6fd2807SJeff Garzik 		id[75]);
1601c6fd2807SJeff Garzik 	DPRINTK("80==0x%04x  "
1602c6fd2807SJeff Garzik 		"81==0x%04x  "
1603c6fd2807SJeff Garzik 		"82==0x%04x  "
1604c6fd2807SJeff Garzik 		"83==0x%04x  "
1605c6fd2807SJeff Garzik 		"84==0x%04x  \n",
1606c6fd2807SJeff Garzik 		id[80],
1607c6fd2807SJeff Garzik 		id[81],
1608c6fd2807SJeff Garzik 		id[82],
1609c6fd2807SJeff Garzik 		id[83],
1610c6fd2807SJeff Garzik 		id[84]);
1611c6fd2807SJeff Garzik 	DPRINTK("88==0x%04x  "
1612c6fd2807SJeff Garzik 		"93==0x%04x\n",
1613c6fd2807SJeff Garzik 		id[88],
1614c6fd2807SJeff Garzik 		id[93]);
1615c6fd2807SJeff Garzik }
1616c6fd2807SJeff Garzik 
1617c6fd2807SJeff Garzik /**
1618c6fd2807SJeff Garzik  *	ata_id_xfermask - Compute xfermask from the given IDENTIFY data
1619c6fd2807SJeff Garzik  *	@id: IDENTIFY data to compute xfer mask from
1620c6fd2807SJeff Garzik  *
1621c6fd2807SJeff Garzik  *	Compute the xfermask for this device. This is not as trivial
1622c6fd2807SJeff Garzik  *	as it seems if we must consider early devices correctly.
1623c6fd2807SJeff Garzik  *
1624c6fd2807SJeff Garzik  *	FIXME: pre IDE drive timing (do we care ?).
1625c6fd2807SJeff Garzik  *
1626c6fd2807SJeff Garzik  *	LOCKING:
1627c6fd2807SJeff Garzik  *	None.
1628c6fd2807SJeff Garzik  *
1629c6fd2807SJeff Garzik  *	RETURNS:
1630c6fd2807SJeff Garzik  *	Computed xfermask
1631c6fd2807SJeff Garzik  */
16327dc951aeSTejun Heo unsigned long ata_id_xfermask(const u16 *id)
1633c6fd2807SJeff Garzik {
16347dc951aeSTejun Heo 	unsigned long pio_mask, mwdma_mask, udma_mask;
1635c6fd2807SJeff Garzik 
1636c6fd2807SJeff Garzik 	/* Usual case. Word 53 indicates word 64 is valid */
1637c6fd2807SJeff Garzik 	if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
1638c6fd2807SJeff Garzik 		pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
1639c6fd2807SJeff Garzik 		pio_mask <<= 3;
1640c6fd2807SJeff Garzik 		pio_mask |= 0x7;
1641c6fd2807SJeff Garzik 	} else {
1642c6fd2807SJeff Garzik 		/* If word 64 isn't valid then Word 51 high byte holds
1643c6fd2807SJeff Garzik 		 * the PIO timing number for the maximum. Turn it into
1644c6fd2807SJeff Garzik 		 * a mask.
1645c6fd2807SJeff Garzik 		 */
16467a0f1c8aSLennert Buytenhek 		u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
164746767aebSAlan Cox 		if (mode < 5)	/* Valid PIO range */
164846767aebSAlan Cox 			pio_mask = (2 << mode) - 1;
164946767aebSAlan Cox 		else
165046767aebSAlan Cox 			pio_mask = 1;
1651c6fd2807SJeff Garzik 
1652c6fd2807SJeff Garzik 		/* But wait.. there's more. Design your standards by
1653c6fd2807SJeff Garzik 		 * committee and you too can get a free iordy field to
1654c6fd2807SJeff Garzik 		 * process. However its the speeds not the modes that
1655c6fd2807SJeff Garzik 		 * are supported... Note drivers using the timing API
1656c6fd2807SJeff Garzik 		 * will get this right anyway
1657c6fd2807SJeff Garzik 		 */
1658c6fd2807SJeff Garzik 	}
1659c6fd2807SJeff Garzik 
1660c6fd2807SJeff Garzik 	mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
1661c6fd2807SJeff Garzik 
1662b352e57dSAlan Cox 	if (ata_id_is_cfa(id)) {
1663b352e57dSAlan Cox 		/*
1664b352e57dSAlan Cox 		 *	Process compact flash extended modes
1665b352e57dSAlan Cox 		 */
166662afe5d7SSergei Shtylyov 		int pio = (id[ATA_ID_CFA_MODES] >> 0) & 0x7;
166762afe5d7SSergei Shtylyov 		int dma = (id[ATA_ID_CFA_MODES] >> 3) & 0x7;
1668b352e57dSAlan Cox 
1669b352e57dSAlan Cox 		if (pio)
1670b352e57dSAlan Cox 			pio_mask |= (1 << 5);
1671b352e57dSAlan Cox 		if (pio > 1)
1672b352e57dSAlan Cox 			pio_mask |= (1 << 6);
1673b352e57dSAlan Cox 		if (dma)
1674b352e57dSAlan Cox 			mwdma_mask |= (1 << 3);
1675b352e57dSAlan Cox 		if (dma > 1)
1676b352e57dSAlan Cox 			mwdma_mask |= (1 << 4);
1677b352e57dSAlan Cox 	}
1678b352e57dSAlan Cox 
1679c6fd2807SJeff Garzik 	udma_mask = 0;
1680c6fd2807SJeff Garzik 	if (id[ATA_ID_FIELD_VALID] & (1 << 2))
1681c6fd2807SJeff Garzik 		udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
1682c6fd2807SJeff Garzik 
1683c6fd2807SJeff Garzik 	return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
1684c6fd2807SJeff Garzik }
1685c6fd2807SJeff Garzik 
1686c6fd2807SJeff Garzik /**
1687442eacc3SJeff Garzik  *	ata_pio_queue_task - Queue port_task
1688c6fd2807SJeff Garzik  *	@ap: The ata_port to queue port_task for
168965f27f38SDavid Howells  *	@data: data for @fn to use
1690341c2c95STejun Heo  *	@delay: delay time in msecs for workqueue function
1691c6fd2807SJeff Garzik  *
1692c6fd2807SJeff Garzik  *	Schedule @fn(@data) for execution after @delay jiffies using
1693c6fd2807SJeff Garzik  *	port_task.  There is one port_task per port and it's the
1694c6fd2807SJeff Garzik  *	user(low level driver)'s responsibility to make sure that only
1695c6fd2807SJeff Garzik  *	one task is active at any given time.
1696c6fd2807SJeff Garzik  *
1697c6fd2807SJeff Garzik  *	libata core layer takes care of synchronization between
1698442eacc3SJeff Garzik  *	port_task and EH.  ata_pio_queue_task() may be ignored for EH
1699c6fd2807SJeff Garzik  *	synchronization.
1700c6fd2807SJeff Garzik  *
1701c6fd2807SJeff Garzik  *	LOCKING:
1702c6fd2807SJeff Garzik  *	Inherited from caller.
1703c6fd2807SJeff Garzik  */
1704624d5c51STejun Heo void ata_pio_queue_task(struct ata_port *ap, void *data, unsigned long delay)
1705c6fd2807SJeff Garzik {
170665f27f38SDavid Howells 	ap->port_task_data = data;
1707c6fd2807SJeff Garzik 
170845a66c1cSOleg Nesterov 	/* may fail if ata_port_flush_task() in progress */
1709341c2c95STejun Heo 	queue_delayed_work(ata_wq, &ap->port_task, msecs_to_jiffies(delay));
1710c6fd2807SJeff Garzik }
1711c6fd2807SJeff Garzik 
1712c6fd2807SJeff Garzik /**
1713c6fd2807SJeff Garzik  *	ata_port_flush_task - Flush port_task
1714c6fd2807SJeff Garzik  *	@ap: The ata_port to flush port_task for
1715c6fd2807SJeff Garzik  *
1716c6fd2807SJeff Garzik  *	After this function completes, port_task is guranteed not to
1717c6fd2807SJeff Garzik  *	be running or scheduled.
1718c6fd2807SJeff Garzik  *
1719c6fd2807SJeff Garzik  *	LOCKING:
1720c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
1721c6fd2807SJeff Garzik  */
1722c6fd2807SJeff Garzik void ata_port_flush_task(struct ata_port *ap)
1723c6fd2807SJeff Garzik {
1724c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
1725c6fd2807SJeff Garzik 
172645a66c1cSOleg Nesterov 	cancel_rearming_delayed_work(&ap->port_task);
1727c6fd2807SJeff Garzik 
1728c6fd2807SJeff Garzik 	if (ata_msg_ctl(ap))
17297f5e4e8dSHarvey Harrison 		ata_port_printk(ap, KERN_DEBUG, "%s: EXIT\n", __func__);
1730c6fd2807SJeff Garzik }
1731c6fd2807SJeff Garzik 
17327102d230SAdrian Bunk static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
1733c6fd2807SJeff Garzik {
1734c6fd2807SJeff Garzik 	struct completion *waiting = qc->private_data;
1735c6fd2807SJeff Garzik 
1736c6fd2807SJeff Garzik 	complete(waiting);
1737c6fd2807SJeff Garzik }
1738c6fd2807SJeff Garzik 
1739c6fd2807SJeff Garzik /**
17402432697bSTejun Heo  *	ata_exec_internal_sg - execute libata internal command
1741c6fd2807SJeff Garzik  *	@dev: Device to which the command is sent
1742c6fd2807SJeff Garzik  *	@tf: Taskfile registers for the command and the result
1743c6fd2807SJeff Garzik  *	@cdb: CDB for packet command
1744c6fd2807SJeff Garzik  *	@dma_dir: Data tranfer direction of the command
17455c1ad8b3SRandy Dunlap  *	@sgl: sg list for the data buffer of the command
17462432697bSTejun Heo  *	@n_elem: Number of sg entries
17472b789108STejun Heo  *	@timeout: Timeout in msecs (0 for default)
1748c6fd2807SJeff Garzik  *
1749c6fd2807SJeff Garzik  *	Executes libata internal command with timeout.  @tf contains
1750c6fd2807SJeff Garzik  *	command on entry and result on return.  Timeout and error
1751c6fd2807SJeff Garzik  *	conditions are reported via return value.  No recovery action
1752c6fd2807SJeff Garzik  *	is taken after a command times out.  It's caller's duty to
1753c6fd2807SJeff Garzik  *	clean up after timeout.
1754c6fd2807SJeff Garzik  *
1755c6fd2807SJeff Garzik  *	LOCKING:
1756c6fd2807SJeff Garzik  *	None.  Should be called with kernel context, might sleep.
1757c6fd2807SJeff Garzik  *
1758c6fd2807SJeff Garzik  *	RETURNS:
1759c6fd2807SJeff Garzik  *	Zero on success, AC_ERR_* mask on failure
1760c6fd2807SJeff Garzik  */
17612432697bSTejun Heo unsigned ata_exec_internal_sg(struct ata_device *dev,
1762c6fd2807SJeff Garzik 			      struct ata_taskfile *tf, const u8 *cdb,
176387260216SJens Axboe 			      int dma_dir, struct scatterlist *sgl,
17642b789108STejun Heo 			      unsigned int n_elem, unsigned long timeout)
1765c6fd2807SJeff Garzik {
17669af5c9c9STejun Heo 	struct ata_link *link = dev->link;
17679af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
1768c6fd2807SJeff Garzik 	u8 command = tf->command;
176987fbc5a0STejun Heo 	int auto_timeout = 0;
1770c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
1771c6fd2807SJeff Garzik 	unsigned int tag, preempted_tag;
1772c6fd2807SJeff Garzik 	u32 preempted_sactive, preempted_qc_active;
1773da917d69STejun Heo 	int preempted_nr_active_links;
1774c6fd2807SJeff Garzik 	DECLARE_COMPLETION_ONSTACK(wait);
1775c6fd2807SJeff Garzik 	unsigned long flags;
1776c6fd2807SJeff Garzik 	unsigned int err_mask;
1777c6fd2807SJeff Garzik 	int rc;
1778c6fd2807SJeff Garzik 
1779c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1780c6fd2807SJeff Garzik 
1781c6fd2807SJeff Garzik 	/* no internal command while frozen */
1782c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_FROZEN) {
1783c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
1784c6fd2807SJeff Garzik 		return AC_ERR_SYSTEM;
1785c6fd2807SJeff Garzik 	}
1786c6fd2807SJeff Garzik 
1787c6fd2807SJeff Garzik 	/* initialize internal qc */
1788c6fd2807SJeff Garzik 
1789c6fd2807SJeff Garzik 	/* XXX: Tag 0 is used for drivers with legacy EH as some
1790c6fd2807SJeff Garzik 	 * drivers choke if any other tag is given.  This breaks
1791c6fd2807SJeff Garzik 	 * ata_tag_internal() test for those drivers.  Don't use new
1792c6fd2807SJeff Garzik 	 * EH stuff without converting to it.
1793c6fd2807SJeff Garzik 	 */
1794c6fd2807SJeff Garzik 	if (ap->ops->error_handler)
1795c6fd2807SJeff Garzik 		tag = ATA_TAG_INTERNAL;
1796c6fd2807SJeff Garzik 	else
1797c6fd2807SJeff Garzik 		tag = 0;
1798c6fd2807SJeff Garzik 
17998a8bc223STejun Heo 	if (test_and_set_bit(tag, &ap->qc_allocated))
18008a8bc223STejun Heo 		BUG();
1801c6fd2807SJeff Garzik 	qc = __ata_qc_from_tag(ap, tag);
1802c6fd2807SJeff Garzik 
1803c6fd2807SJeff Garzik 	qc->tag = tag;
1804c6fd2807SJeff Garzik 	qc->scsicmd = NULL;
1805c6fd2807SJeff Garzik 	qc->ap = ap;
1806c6fd2807SJeff Garzik 	qc->dev = dev;
1807c6fd2807SJeff Garzik 	ata_qc_reinit(qc);
1808c6fd2807SJeff Garzik 
18099af5c9c9STejun Heo 	preempted_tag = link->active_tag;
18109af5c9c9STejun Heo 	preempted_sactive = link->sactive;
1811c6fd2807SJeff Garzik 	preempted_qc_active = ap->qc_active;
1812da917d69STejun Heo 	preempted_nr_active_links = ap->nr_active_links;
18139af5c9c9STejun Heo 	link->active_tag = ATA_TAG_POISON;
18149af5c9c9STejun Heo 	link->sactive = 0;
1815c6fd2807SJeff Garzik 	ap->qc_active = 0;
1816da917d69STejun Heo 	ap->nr_active_links = 0;
1817c6fd2807SJeff Garzik 
1818c6fd2807SJeff Garzik 	/* prepare & issue qc */
1819c6fd2807SJeff Garzik 	qc->tf = *tf;
1820c6fd2807SJeff Garzik 	if (cdb)
1821c6fd2807SJeff Garzik 		memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
1822c6fd2807SJeff Garzik 	qc->flags |= ATA_QCFLAG_RESULT_TF;
1823c6fd2807SJeff Garzik 	qc->dma_dir = dma_dir;
1824c6fd2807SJeff Garzik 	if (dma_dir != DMA_NONE) {
18252432697bSTejun Heo 		unsigned int i, buflen = 0;
182687260216SJens Axboe 		struct scatterlist *sg;
18272432697bSTejun Heo 
182887260216SJens Axboe 		for_each_sg(sgl, sg, n_elem, i)
182987260216SJens Axboe 			buflen += sg->length;
18302432697bSTejun Heo 
183187260216SJens Axboe 		ata_sg_init(qc, sgl, n_elem);
183249c80429SBrian King 		qc->nbytes = buflen;
1833c6fd2807SJeff Garzik 	}
1834c6fd2807SJeff Garzik 
1835c6fd2807SJeff Garzik 	qc->private_data = &wait;
1836c6fd2807SJeff Garzik 	qc->complete_fn = ata_qc_complete_internal;
1837c6fd2807SJeff Garzik 
1838c6fd2807SJeff Garzik 	ata_qc_issue(qc);
1839c6fd2807SJeff Garzik 
1840c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1841c6fd2807SJeff Garzik 
184287fbc5a0STejun Heo 	if (!timeout) {
184387fbc5a0STejun Heo 		if (ata_probe_timeout)
1844341c2c95STejun Heo 			timeout = ata_probe_timeout * 1000;
184587fbc5a0STejun Heo 		else {
184687fbc5a0STejun Heo 			timeout = ata_internal_cmd_timeout(dev, command);
184787fbc5a0STejun Heo 			auto_timeout = 1;
184887fbc5a0STejun Heo 		}
184987fbc5a0STejun Heo 	}
18502b789108STejun Heo 
18512b789108STejun Heo 	rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
1852c6fd2807SJeff Garzik 
1853c6fd2807SJeff Garzik 	ata_port_flush_task(ap);
1854c6fd2807SJeff Garzik 
1855c6fd2807SJeff Garzik 	if (!rc) {
1856c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
1857c6fd2807SJeff Garzik 
1858c6fd2807SJeff Garzik 		/* We're racing with irq here.  If we lose, the
1859c6fd2807SJeff Garzik 		 * following test prevents us from completing the qc
1860c6fd2807SJeff Garzik 		 * twice.  If we win, the port is frozen and will be
1861c6fd2807SJeff Garzik 		 * cleaned up by ->post_internal_cmd().
1862c6fd2807SJeff Garzik 		 */
1863c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_ACTIVE) {
1864c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_TIMEOUT;
1865c6fd2807SJeff Garzik 
1866c6fd2807SJeff Garzik 			if (ap->ops->error_handler)
1867c6fd2807SJeff Garzik 				ata_port_freeze(ap);
1868c6fd2807SJeff Garzik 			else
1869c6fd2807SJeff Garzik 				ata_qc_complete(qc);
1870c6fd2807SJeff Garzik 
1871c6fd2807SJeff Garzik 			if (ata_msg_warn(ap))
1872c6fd2807SJeff Garzik 				ata_dev_printk(dev, KERN_WARNING,
1873c6fd2807SJeff Garzik 					"qc timeout (cmd 0x%x)\n", command);
1874c6fd2807SJeff Garzik 		}
1875c6fd2807SJeff Garzik 
1876c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
1877c6fd2807SJeff Garzik 	}
1878c6fd2807SJeff Garzik 
1879c6fd2807SJeff Garzik 	/* do post_internal_cmd */
1880c6fd2807SJeff Garzik 	if (ap->ops->post_internal_cmd)
1881c6fd2807SJeff Garzik 		ap->ops->post_internal_cmd(qc);
1882c6fd2807SJeff Garzik 
1883a51d644aSTejun Heo 	/* perform minimal error analysis */
1884a51d644aSTejun Heo 	if (qc->flags & ATA_QCFLAG_FAILED) {
1885a51d644aSTejun Heo 		if (qc->result_tf.command & (ATA_ERR | ATA_DF))
1886a51d644aSTejun Heo 			qc->err_mask |= AC_ERR_DEV;
1887a51d644aSTejun Heo 
1888a51d644aSTejun Heo 		if (!qc->err_mask)
1889c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_OTHER;
1890a51d644aSTejun Heo 
1891a51d644aSTejun Heo 		if (qc->err_mask & ~AC_ERR_OTHER)
1892a51d644aSTejun Heo 			qc->err_mask &= ~AC_ERR_OTHER;
1893c6fd2807SJeff Garzik 	}
1894c6fd2807SJeff Garzik 
1895c6fd2807SJeff Garzik 	/* finish up */
1896c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1897c6fd2807SJeff Garzik 
1898c6fd2807SJeff Garzik 	*tf = qc->result_tf;
1899c6fd2807SJeff Garzik 	err_mask = qc->err_mask;
1900c6fd2807SJeff Garzik 
1901c6fd2807SJeff Garzik 	ata_qc_free(qc);
19029af5c9c9STejun Heo 	link->active_tag = preempted_tag;
19039af5c9c9STejun Heo 	link->sactive = preempted_sactive;
1904c6fd2807SJeff Garzik 	ap->qc_active = preempted_qc_active;
1905da917d69STejun Heo 	ap->nr_active_links = preempted_nr_active_links;
1906c6fd2807SJeff Garzik 
1907c6fd2807SJeff Garzik 	/* XXX - Some LLDDs (sata_mv) disable port on command failure.
1908c6fd2807SJeff Garzik 	 * Until those drivers are fixed, we detect the condition
1909c6fd2807SJeff Garzik 	 * here, fail the command with AC_ERR_SYSTEM and reenable the
1910c6fd2807SJeff Garzik 	 * port.
1911c6fd2807SJeff Garzik 	 *
1912c6fd2807SJeff Garzik 	 * Note that this doesn't change any behavior as internal
1913c6fd2807SJeff Garzik 	 * command failure results in disabling the device in the
1914c6fd2807SJeff Garzik 	 * higher layer for LLDDs without new reset/EH callbacks.
1915c6fd2807SJeff Garzik 	 *
1916c6fd2807SJeff Garzik 	 * Kill the following code as soon as those drivers are fixed.
1917c6fd2807SJeff Garzik 	 */
1918c6fd2807SJeff Garzik 	if (ap->flags & ATA_FLAG_DISABLED) {
1919c6fd2807SJeff Garzik 		err_mask |= AC_ERR_SYSTEM;
1920c6fd2807SJeff Garzik 		ata_port_probe(ap);
1921c6fd2807SJeff Garzik 	}
1922c6fd2807SJeff Garzik 
1923c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1924c6fd2807SJeff Garzik 
192587fbc5a0STejun Heo 	if ((err_mask & AC_ERR_TIMEOUT) && auto_timeout)
192687fbc5a0STejun Heo 		ata_internal_cmd_timed_out(dev, command);
192787fbc5a0STejun Heo 
1928c6fd2807SJeff Garzik 	return err_mask;
1929c6fd2807SJeff Garzik }
1930c6fd2807SJeff Garzik 
1931c6fd2807SJeff Garzik /**
193233480a0eSTejun Heo  *	ata_exec_internal - execute libata internal command
19332432697bSTejun Heo  *	@dev: Device to which the command is sent
19342432697bSTejun Heo  *	@tf: Taskfile registers for the command and the result
19352432697bSTejun Heo  *	@cdb: CDB for packet command
19362432697bSTejun Heo  *	@dma_dir: Data tranfer direction of the command
19372432697bSTejun Heo  *	@buf: Data buffer of the command
19382432697bSTejun Heo  *	@buflen: Length of data buffer
19392b789108STejun Heo  *	@timeout: Timeout in msecs (0 for default)
19402432697bSTejun Heo  *
19412432697bSTejun Heo  *	Wrapper around ata_exec_internal_sg() which takes simple
19422432697bSTejun Heo  *	buffer instead of sg list.
19432432697bSTejun Heo  *
19442432697bSTejun Heo  *	LOCKING:
19452432697bSTejun Heo  *	None.  Should be called with kernel context, might sleep.
19462432697bSTejun Heo  *
19472432697bSTejun Heo  *	RETURNS:
19482432697bSTejun Heo  *	Zero on success, AC_ERR_* mask on failure
19492432697bSTejun Heo  */
19502432697bSTejun Heo unsigned ata_exec_internal(struct ata_device *dev,
19512432697bSTejun Heo 			   struct ata_taskfile *tf, const u8 *cdb,
19522b789108STejun Heo 			   int dma_dir, void *buf, unsigned int buflen,
19532b789108STejun Heo 			   unsigned long timeout)
19542432697bSTejun Heo {
195533480a0eSTejun Heo 	struct scatterlist *psg = NULL, sg;
195633480a0eSTejun Heo 	unsigned int n_elem = 0;
19572432697bSTejun Heo 
195833480a0eSTejun Heo 	if (dma_dir != DMA_NONE) {
195933480a0eSTejun Heo 		WARN_ON(!buf);
19602432697bSTejun Heo 		sg_init_one(&sg, buf, buflen);
196133480a0eSTejun Heo 		psg = &sg;
196233480a0eSTejun Heo 		n_elem++;
196333480a0eSTejun Heo 	}
19642432697bSTejun Heo 
19652b789108STejun Heo 	return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem,
19662b789108STejun Heo 				    timeout);
19672432697bSTejun Heo }
19682432697bSTejun Heo 
19692432697bSTejun Heo /**
1970c6fd2807SJeff Garzik  *	ata_do_simple_cmd - execute simple internal command
1971c6fd2807SJeff Garzik  *	@dev: Device to which the command is sent
1972c6fd2807SJeff Garzik  *	@cmd: Opcode to execute
1973c6fd2807SJeff Garzik  *
1974c6fd2807SJeff Garzik  *	Execute a 'simple' command, that only consists of the opcode
1975c6fd2807SJeff Garzik  *	'cmd' itself, without filling any other registers
1976c6fd2807SJeff Garzik  *
1977c6fd2807SJeff Garzik  *	LOCKING:
1978c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
1979c6fd2807SJeff Garzik  *
1980c6fd2807SJeff Garzik  *	RETURNS:
1981c6fd2807SJeff Garzik  *	Zero on success, AC_ERR_* mask on failure
1982c6fd2807SJeff Garzik  */
1983c6fd2807SJeff Garzik unsigned int ata_do_simple_cmd(struct ata_device *dev, u8 cmd)
1984c6fd2807SJeff Garzik {
1985c6fd2807SJeff Garzik 	struct ata_taskfile tf;
1986c6fd2807SJeff Garzik 
1987c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
1988c6fd2807SJeff Garzik 
1989c6fd2807SJeff Garzik 	tf.command = cmd;
1990c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_DEVICE;
1991c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
1992c6fd2807SJeff Garzik 
19932b789108STejun Heo 	return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1994c6fd2807SJeff Garzik }
1995c6fd2807SJeff Garzik 
1996c6fd2807SJeff Garzik /**
1997c6fd2807SJeff Garzik  *	ata_pio_need_iordy	-	check if iordy needed
1998c6fd2807SJeff Garzik  *	@adev: ATA device
1999c6fd2807SJeff Garzik  *
2000c6fd2807SJeff Garzik  *	Check if the current speed of the device requires IORDY. Used
2001c6fd2807SJeff Garzik  *	by various controllers for chip configuration.
2002c6fd2807SJeff Garzik  */
2003c6fd2807SJeff Garzik unsigned int ata_pio_need_iordy(const struct ata_device *adev)
2004c6fd2807SJeff Garzik {
20050d9e6659STejun Heo 	/* Don't set IORDY if we're preparing for reset.  IORDY may
20060d9e6659STejun Heo 	 * lead to controller lock up on certain controllers if the
20070d9e6659STejun Heo 	 * port is not occupied.  See bko#11703 for details.
20080d9e6659STejun Heo 	 */
20090d9e6659STejun Heo 	if (adev->link->ap->pflags & ATA_PFLAG_RESETTING)
20100d9e6659STejun Heo 		return 0;
20110d9e6659STejun Heo 	/* Controller doesn't support IORDY.  Probably a pointless
20120d9e6659STejun Heo 	 * check as the caller should know this.
20130d9e6659STejun Heo 	 */
20149af5c9c9STejun Heo 	if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
2015c6fd2807SJeff Garzik 		return 0;
20165c18c4d2SDavid Daney 	/* CF spec. r4.1 Table 22 says no iordy on PIO5 and PIO6.  */
20175c18c4d2SDavid Daney 	if (ata_id_is_cfa(adev->id)
20185c18c4d2SDavid Daney 	    && (adev->pio_mode == XFER_PIO_5 || adev->pio_mode == XFER_PIO_6))
20195c18c4d2SDavid Daney 		return 0;
2020432729f0SAlan Cox 	/* PIO3 and higher it is mandatory */
2021432729f0SAlan Cox 	if (adev->pio_mode > XFER_PIO_2)
2022c6fd2807SJeff Garzik 		return 1;
2023432729f0SAlan Cox 	/* We turn it on when possible */
2024432729f0SAlan Cox 	if (ata_id_has_iordy(adev->id))
2025432729f0SAlan Cox 		return 1;
2026432729f0SAlan Cox 	return 0;
2027432729f0SAlan Cox }
2028c6fd2807SJeff Garzik 
2029432729f0SAlan Cox /**
2030432729f0SAlan Cox  *	ata_pio_mask_no_iordy	-	Return the non IORDY mask
2031432729f0SAlan Cox  *	@adev: ATA device
2032432729f0SAlan Cox  *
2033432729f0SAlan Cox  *	Compute the highest mode possible if we are not using iordy. Return
2034432729f0SAlan Cox  *	-1 if no iordy mode is available.
2035432729f0SAlan Cox  */
2036432729f0SAlan Cox static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
2037432729f0SAlan Cox {
2038c6fd2807SJeff Garzik 	/* If we have no drive specific rule, then PIO 2 is non IORDY */
2039c6fd2807SJeff Garzik 	if (adev->id[ATA_ID_FIELD_VALID] & 2) {	/* EIDE */
2040432729f0SAlan Cox 		u16 pio = adev->id[ATA_ID_EIDE_PIO];
2041c6fd2807SJeff Garzik 		/* Is the speed faster than the drive allows non IORDY ? */
2042c6fd2807SJeff Garzik 		if (pio) {
2043c6fd2807SJeff Garzik 			/* This is cycle times not frequency - watch the logic! */
2044c6fd2807SJeff Garzik 			if (pio > 240)	/* PIO2 is 240nS per cycle */
2045432729f0SAlan Cox 				return 3 << ATA_SHIFT_PIO;
2046432729f0SAlan Cox 			return 7 << ATA_SHIFT_PIO;
2047c6fd2807SJeff Garzik 		}
2048c6fd2807SJeff Garzik 	}
2049432729f0SAlan Cox 	return 3 << ATA_SHIFT_PIO;
2050c6fd2807SJeff Garzik }
2051c6fd2807SJeff Garzik 
2052c6fd2807SJeff Garzik /**
2053963e4975SAlan Cox  *	ata_do_dev_read_id		-	default ID read method
2054963e4975SAlan Cox  *	@dev: device
2055963e4975SAlan Cox  *	@tf: proposed taskfile
2056963e4975SAlan Cox  *	@id: data buffer
2057963e4975SAlan Cox  *
2058963e4975SAlan Cox  *	Issue the identify taskfile and hand back the buffer containing
2059963e4975SAlan Cox  *	identify data. For some RAID controllers and for pre ATA devices
2060963e4975SAlan Cox  *	this function is wrapped or replaced by the driver
2061963e4975SAlan Cox  */
2062963e4975SAlan Cox unsigned int ata_do_dev_read_id(struct ata_device *dev,
2063963e4975SAlan Cox 					struct ata_taskfile *tf, u16 *id)
2064963e4975SAlan Cox {
2065963e4975SAlan Cox 	return ata_exec_internal(dev, tf, NULL, DMA_FROM_DEVICE,
2066963e4975SAlan Cox 				     id, sizeof(id[0]) * ATA_ID_WORDS, 0);
2067963e4975SAlan Cox }
2068963e4975SAlan Cox 
2069963e4975SAlan Cox /**
2070c6fd2807SJeff Garzik  *	ata_dev_read_id - Read ID data from the specified device
2071c6fd2807SJeff Garzik  *	@dev: target device
2072c6fd2807SJeff Garzik  *	@p_class: pointer to class of the target device (may be changed)
2073bff04647STejun Heo  *	@flags: ATA_READID_* flags
2074c6fd2807SJeff Garzik  *	@id: buffer to read IDENTIFY data into
2075c6fd2807SJeff Garzik  *
2076c6fd2807SJeff Garzik  *	Read ID data from the specified device.  ATA_CMD_ID_ATA is
2077c6fd2807SJeff Garzik  *	performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
2078c6fd2807SJeff Garzik  *	devices.  This function also issues ATA_CMD_INIT_DEV_PARAMS
2079c6fd2807SJeff Garzik  *	for pre-ATA4 drives.
2080c6fd2807SJeff Garzik  *
208150a99018SAlan Cox  *	FIXME: ATA_CMD_ID_ATA is optional for early drives and right
208250a99018SAlan Cox  *	now we abort if we hit that case.
208350a99018SAlan Cox  *
2084c6fd2807SJeff Garzik  *	LOCKING:
2085c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
2086c6fd2807SJeff Garzik  *
2087c6fd2807SJeff Garzik  *	RETURNS:
2088c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
2089c6fd2807SJeff Garzik  */
2090c6fd2807SJeff Garzik int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
2091bff04647STejun Heo 		    unsigned int flags, u16 *id)
2092c6fd2807SJeff Garzik {
20939af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
2094c6fd2807SJeff Garzik 	unsigned int class = *p_class;
2095c6fd2807SJeff Garzik 	struct ata_taskfile tf;
2096c6fd2807SJeff Garzik 	unsigned int err_mask = 0;
2097c6fd2807SJeff Garzik 	const char *reason;
209879b42babSTejun Heo 	bool is_semb = class == ATA_DEV_SEMB;
209954936f8bSTejun Heo 	int may_fallback = 1, tried_spinup = 0;
2100c6fd2807SJeff Garzik 	int rc;
2101c6fd2807SJeff Garzik 
2102c6fd2807SJeff Garzik 	if (ata_msg_ctl(ap))
21037f5e4e8dSHarvey Harrison 		ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __func__);
2104c6fd2807SJeff Garzik 
2105c6fd2807SJeff Garzik retry:
2106c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
2107c6fd2807SJeff Garzik 
2108c6fd2807SJeff Garzik 	switch (class) {
210979b42babSTejun Heo 	case ATA_DEV_SEMB:
211079b42babSTejun Heo 		class = ATA_DEV_ATA;	/* some hard drives report SEMB sig */
2111c6fd2807SJeff Garzik 	case ATA_DEV_ATA:
2112c6fd2807SJeff Garzik 		tf.command = ATA_CMD_ID_ATA;
2113c6fd2807SJeff Garzik 		break;
2114c6fd2807SJeff Garzik 	case ATA_DEV_ATAPI:
2115c6fd2807SJeff Garzik 		tf.command = ATA_CMD_ID_ATAPI;
2116c6fd2807SJeff Garzik 		break;
2117c6fd2807SJeff Garzik 	default:
2118c6fd2807SJeff Garzik 		rc = -ENODEV;
2119c6fd2807SJeff Garzik 		reason = "unsupported class";
2120c6fd2807SJeff Garzik 		goto err_out;
2121c6fd2807SJeff Garzik 	}
2122c6fd2807SJeff Garzik 
2123c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_PIO;
212481afe893STejun Heo 
212581afe893STejun Heo 	/* Some devices choke if TF registers contain garbage.  Make
212681afe893STejun Heo 	 * sure those are properly initialized.
212781afe893STejun Heo 	 */
212881afe893STejun Heo 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
212981afe893STejun Heo 
213081afe893STejun Heo 	/* Device presence detection is unreliable on some
213181afe893STejun Heo 	 * controllers.  Always poll IDENTIFY if available.
213281afe893STejun Heo 	 */
213381afe893STejun Heo 	tf.flags |= ATA_TFLAG_POLLING;
2134c6fd2807SJeff Garzik 
2135963e4975SAlan Cox 	if (ap->ops->read_id)
2136963e4975SAlan Cox 		err_mask = ap->ops->read_id(dev, &tf, id);
2137963e4975SAlan Cox 	else
2138963e4975SAlan Cox 		err_mask = ata_do_dev_read_id(dev, &tf, id);
2139963e4975SAlan Cox 
2140c6fd2807SJeff Garzik 	if (err_mask) {
2141800b3996STejun Heo 		if (err_mask & AC_ERR_NODEV_HINT) {
21421ffc151fSTejun Heo 			ata_dev_printk(dev, KERN_DEBUG,
21431ffc151fSTejun Heo 				       "NODEV after polling detection\n");
214455a8e2c8STejun Heo 			return -ENOENT;
214555a8e2c8STejun Heo 		}
214655a8e2c8STejun Heo 
214779b42babSTejun Heo 		if (is_semb) {
214879b42babSTejun Heo 			ata_dev_printk(dev, KERN_INFO, "IDENTIFY failed on "
214979b42babSTejun Heo 				       "device w/ SEMB sig, disabled\n");
215079b42babSTejun Heo 			/* SEMB is not supported yet */
215179b42babSTejun Heo 			*p_class = ATA_DEV_SEMB_UNSUP;
215279b42babSTejun Heo 			return 0;
215379b42babSTejun Heo 		}
215479b42babSTejun Heo 
21551ffc151fSTejun Heo 		if ((err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
21561ffc151fSTejun Heo 			/* Device or controller might have reported
21571ffc151fSTejun Heo 			 * the wrong device class.  Give a shot at the
21581ffc151fSTejun Heo 			 * other IDENTIFY if the current one is
21591ffc151fSTejun Heo 			 * aborted by the device.
216054936f8bSTejun Heo 			 */
21611ffc151fSTejun Heo 			if (may_fallback) {
216254936f8bSTejun Heo 				may_fallback = 0;
216354936f8bSTejun Heo 
216454936f8bSTejun Heo 				if (class == ATA_DEV_ATA)
216554936f8bSTejun Heo 					class = ATA_DEV_ATAPI;
216654936f8bSTejun Heo 				else
216754936f8bSTejun Heo 					class = ATA_DEV_ATA;
216854936f8bSTejun Heo 				goto retry;
216954936f8bSTejun Heo 			}
217054936f8bSTejun Heo 
21711ffc151fSTejun Heo 			/* Control reaches here iff the device aborted
21721ffc151fSTejun Heo 			 * both flavors of IDENTIFYs which happens
21731ffc151fSTejun Heo 			 * sometimes with phantom devices.
21741ffc151fSTejun Heo 			 */
21751ffc151fSTejun Heo 			ata_dev_printk(dev, KERN_DEBUG,
21761ffc151fSTejun Heo 				       "both IDENTIFYs aborted, assuming NODEV\n");
21771ffc151fSTejun Heo 			return -ENOENT;
21781ffc151fSTejun Heo 		}
21791ffc151fSTejun Heo 
2180c6fd2807SJeff Garzik 		rc = -EIO;
2181c6fd2807SJeff Garzik 		reason = "I/O error";
2182c6fd2807SJeff Garzik 		goto err_out;
2183c6fd2807SJeff Garzik 	}
2184c6fd2807SJeff Garzik 
218554936f8bSTejun Heo 	/* Falling back doesn't make sense if ID data was read
218654936f8bSTejun Heo 	 * successfully at least once.
218754936f8bSTejun Heo 	 */
218854936f8bSTejun Heo 	may_fallback = 0;
218954936f8bSTejun Heo 
2190c6fd2807SJeff Garzik 	swap_buf_le16(id, ATA_ID_WORDS);
2191c6fd2807SJeff Garzik 
2192c6fd2807SJeff Garzik 	/* sanity check */
2193c6fd2807SJeff Garzik 	rc = -EINVAL;
21946070068bSAlan Cox 	reason = "device reports invalid type";
21954a3381feSJeff Garzik 
21964a3381feSJeff Garzik 	if (class == ATA_DEV_ATA) {
21974a3381feSJeff Garzik 		if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
21984a3381feSJeff Garzik 			goto err_out;
21994a3381feSJeff Garzik 	} else {
22004a3381feSJeff Garzik 		if (ata_id_is_ata(id))
2201c6fd2807SJeff Garzik 			goto err_out;
2202c6fd2807SJeff Garzik 	}
2203c6fd2807SJeff Garzik 
2204169439c2SMark Lord 	if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
2205169439c2SMark Lord 		tried_spinup = 1;
2206169439c2SMark Lord 		/*
2207169439c2SMark Lord 		 * Drive powered-up in standby mode, and requires a specific
2208169439c2SMark Lord 		 * SET_FEATURES spin-up subcommand before it will accept
2209169439c2SMark Lord 		 * anything other than the original IDENTIFY command.
2210169439c2SMark Lord 		 */
2211218f3d30SJeff Garzik 		err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
2212fb0582f9SRyan Power 		if (err_mask && id[2] != 0x738c) {
2213169439c2SMark Lord 			rc = -EIO;
2214169439c2SMark Lord 			reason = "SPINUP failed";
2215169439c2SMark Lord 			goto err_out;
2216169439c2SMark Lord 		}
2217169439c2SMark Lord 		/*
2218169439c2SMark Lord 		 * If the drive initially returned incomplete IDENTIFY info,
2219169439c2SMark Lord 		 * we now must reissue the IDENTIFY command.
2220169439c2SMark Lord 		 */
2221169439c2SMark Lord 		if (id[2] == 0x37c8)
2222169439c2SMark Lord 			goto retry;
2223169439c2SMark Lord 	}
2224169439c2SMark Lord 
2225bff04647STejun Heo 	if ((flags & ATA_READID_POSTRESET) && class == ATA_DEV_ATA) {
2226c6fd2807SJeff Garzik 		/*
2227c6fd2807SJeff Garzik 		 * The exact sequence expected by certain pre-ATA4 drives is:
2228c6fd2807SJeff Garzik 		 * SRST RESET
222950a99018SAlan Cox 		 * IDENTIFY (optional in early ATA)
223050a99018SAlan Cox 		 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
2231c6fd2807SJeff Garzik 		 * anything else..
2232c6fd2807SJeff Garzik 		 * Some drives were very specific about that exact sequence.
223350a99018SAlan Cox 		 *
223450a99018SAlan Cox 		 * Note that ATA4 says lba is mandatory so the second check
223550a99018SAlan Cox 		 * shoud never trigger.
2236c6fd2807SJeff Garzik 		 */
2237c6fd2807SJeff Garzik 		if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
2238c6fd2807SJeff Garzik 			err_mask = ata_dev_init_params(dev, id[3], id[6]);
2239c6fd2807SJeff Garzik 			if (err_mask) {
2240c6fd2807SJeff Garzik 				rc = -EIO;
2241c6fd2807SJeff Garzik 				reason = "INIT_DEV_PARAMS failed";
2242c6fd2807SJeff Garzik 				goto err_out;
2243c6fd2807SJeff Garzik 			}
2244c6fd2807SJeff Garzik 
2245c6fd2807SJeff Garzik 			/* current CHS translation info (id[53-58]) might be
2246c6fd2807SJeff Garzik 			 * changed. reread the identify device info.
2247c6fd2807SJeff Garzik 			 */
2248bff04647STejun Heo 			flags &= ~ATA_READID_POSTRESET;
2249c6fd2807SJeff Garzik 			goto retry;
2250c6fd2807SJeff Garzik 		}
2251c6fd2807SJeff Garzik 	}
2252c6fd2807SJeff Garzik 
2253c6fd2807SJeff Garzik 	*p_class = class;
2254c6fd2807SJeff Garzik 
2255c6fd2807SJeff Garzik 	return 0;
2256c6fd2807SJeff Garzik 
2257c6fd2807SJeff Garzik  err_out:
2258c6fd2807SJeff Garzik 	if (ata_msg_warn(ap))
2259c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_WARNING, "failed to IDENTIFY "
2260c6fd2807SJeff Garzik 			       "(%s, err_mask=0x%x)\n", reason, err_mask);
2261c6fd2807SJeff Garzik 	return rc;
2262c6fd2807SJeff Garzik }
2263c6fd2807SJeff Garzik 
22649062712fSTejun Heo static int ata_do_link_spd_horkage(struct ata_device *dev)
22659062712fSTejun Heo {
22669062712fSTejun Heo 	struct ata_link *plink = ata_dev_phys_link(dev);
22679062712fSTejun Heo 	u32 target, target_limit;
22689062712fSTejun Heo 
22699062712fSTejun Heo 	if (!sata_scr_valid(plink))
22709062712fSTejun Heo 		return 0;
22719062712fSTejun Heo 
22729062712fSTejun Heo 	if (dev->horkage & ATA_HORKAGE_1_5_GBPS)
22739062712fSTejun Heo 		target = 1;
22749062712fSTejun Heo 	else
22759062712fSTejun Heo 		return 0;
22769062712fSTejun Heo 
22779062712fSTejun Heo 	target_limit = (1 << target) - 1;
22789062712fSTejun Heo 
22799062712fSTejun Heo 	/* if already on stricter limit, no need to push further */
22809062712fSTejun Heo 	if (plink->sata_spd_limit <= target_limit)
22819062712fSTejun Heo 		return 0;
22829062712fSTejun Heo 
22839062712fSTejun Heo 	plink->sata_spd_limit = target_limit;
22849062712fSTejun Heo 
22859062712fSTejun Heo 	/* Request another EH round by returning -EAGAIN if link is
22869062712fSTejun Heo 	 * going faster than the target speed.  Forward progress is
22879062712fSTejun Heo 	 * guaranteed by setting sata_spd_limit to target_limit above.
22889062712fSTejun Heo 	 */
22899062712fSTejun Heo 	if (plink->sata_spd > target) {
22909062712fSTejun Heo 		ata_dev_printk(dev, KERN_INFO,
22919062712fSTejun Heo 			       "applying link speed limit horkage to %s\n",
22929062712fSTejun Heo 			       sata_spd_string(target));
22939062712fSTejun Heo 		return -EAGAIN;
22949062712fSTejun Heo 	}
22959062712fSTejun Heo 	return 0;
22969062712fSTejun Heo }
22979062712fSTejun Heo 
2298c6fd2807SJeff Garzik static inline u8 ata_dev_knobble(struct ata_device *dev)
2299c6fd2807SJeff Garzik {
23009af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
23019ce8e307SJens Axboe 
23029ce8e307SJens Axboe 	if (ata_dev_blacklisted(dev) & ATA_HORKAGE_BRIDGE_OK)
23039ce8e307SJens Axboe 		return 0;
23049ce8e307SJens Axboe 
23059af5c9c9STejun Heo 	return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
2306c6fd2807SJeff Garzik }
2307c6fd2807SJeff Garzik 
2308388539f3SShaohua Li static int ata_dev_config_ncq(struct ata_device *dev,
2309c6fd2807SJeff Garzik 			       char *desc, size_t desc_sz)
2310c6fd2807SJeff Garzik {
23119af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
2312c6fd2807SJeff Garzik 	int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
2313388539f3SShaohua Li 	unsigned int err_mask;
2314388539f3SShaohua Li 	char *aa_desc = "";
2315c6fd2807SJeff Garzik 
2316c6fd2807SJeff Garzik 	if (!ata_id_has_ncq(dev->id)) {
2317c6fd2807SJeff Garzik 		desc[0] = '\0';
2318388539f3SShaohua Li 		return 0;
2319c6fd2807SJeff Garzik 	}
232075683fe7STejun Heo 	if (dev->horkage & ATA_HORKAGE_NONCQ) {
23216919a0a6SAlan Cox 		snprintf(desc, desc_sz, "NCQ (not used)");
2322388539f3SShaohua Li 		return 0;
23236919a0a6SAlan Cox 	}
2324c6fd2807SJeff Garzik 	if (ap->flags & ATA_FLAG_NCQ) {
2325cca3974eSJeff Garzik 		hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE - 1);
2326c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_NCQ;
2327c6fd2807SJeff Garzik 	}
2328c6fd2807SJeff Garzik 
2329388539f3SShaohua Li 	if (!(dev->horkage & ATA_HORKAGE_BROKEN_FPDMA_AA) &&
2330388539f3SShaohua Li 		(ap->flags & ATA_FLAG_FPDMA_AA) &&
2331388539f3SShaohua Li 		ata_id_has_fpdma_aa(dev->id)) {
2332388539f3SShaohua Li 		err_mask = ata_dev_set_feature(dev, SETFEATURES_SATA_ENABLE,
2333388539f3SShaohua Li 			SATA_FPDMA_AA);
2334388539f3SShaohua Li 		if (err_mask) {
2335388539f3SShaohua Li 			ata_dev_printk(dev, KERN_ERR, "failed to enable AA"
2336388539f3SShaohua Li 				"(error_mask=0x%x)\n", err_mask);
2337388539f3SShaohua Li 			if (err_mask != AC_ERR_DEV) {
2338388539f3SShaohua Li 				dev->horkage |= ATA_HORKAGE_BROKEN_FPDMA_AA;
2339388539f3SShaohua Li 				return -EIO;
2340388539f3SShaohua Li 			}
2341388539f3SShaohua Li 		} else
2342388539f3SShaohua Li 			aa_desc = ", AA";
2343388539f3SShaohua Li 	}
2344388539f3SShaohua Li 
2345c6fd2807SJeff Garzik 	if (hdepth >= ddepth)
2346388539f3SShaohua Li 		snprintf(desc, desc_sz, "NCQ (depth %d)%s", ddepth, aa_desc);
2347c6fd2807SJeff Garzik 	else
2348388539f3SShaohua Li 		snprintf(desc, desc_sz, "NCQ (depth %d/%d)%s", hdepth,
2349388539f3SShaohua Li 			ddepth, aa_desc);
2350388539f3SShaohua Li 	return 0;
2351c6fd2807SJeff Garzik }
2352c6fd2807SJeff Garzik 
2353c6fd2807SJeff Garzik /**
2354c6fd2807SJeff Garzik  *	ata_dev_configure - Configure the specified ATA/ATAPI device
2355c6fd2807SJeff Garzik  *	@dev: Target device to configure
2356c6fd2807SJeff Garzik  *
2357c6fd2807SJeff Garzik  *	Configure @dev according to @dev->id.  Generic and low-level
2358c6fd2807SJeff Garzik  *	driver specific fixups are also applied.
2359c6fd2807SJeff Garzik  *
2360c6fd2807SJeff Garzik  *	LOCKING:
2361c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
2362c6fd2807SJeff Garzik  *
2363c6fd2807SJeff Garzik  *	RETURNS:
2364c6fd2807SJeff Garzik  *	0 on success, -errno otherwise
2365c6fd2807SJeff Garzik  */
2366efdaedc4STejun Heo int ata_dev_configure(struct ata_device *dev)
2367c6fd2807SJeff Garzik {
23689af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
23699af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
23706746544cSTejun Heo 	int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
2371c6fd2807SJeff Garzik 	const u16 *id = dev->id;
23727dc951aeSTejun Heo 	unsigned long xfer_mask;
2373b352e57dSAlan Cox 	char revbuf[7];		/* XYZ-99\0 */
23743f64f565SEric D. Mudama 	char fwrevbuf[ATA_ID_FW_REV_LEN+1];
23753f64f565SEric D. Mudama 	char modelbuf[ATA_ID_PROD_LEN+1];
2376c6fd2807SJeff Garzik 	int rc;
2377c6fd2807SJeff Garzik 
2378c6fd2807SJeff Garzik 	if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
237944877b4eSTejun Heo 		ata_dev_printk(dev, KERN_INFO, "%s: ENTER/EXIT -- nodev\n",
23807f5e4e8dSHarvey Harrison 			       __func__);
2381c6fd2807SJeff Garzik 		return 0;
2382c6fd2807SJeff Garzik 	}
2383c6fd2807SJeff Garzik 
2384c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
23857f5e4e8dSHarvey Harrison 		ata_dev_printk(dev, KERN_DEBUG, "%s: ENTER\n", __func__);
2386c6fd2807SJeff Garzik 
238775683fe7STejun Heo 	/* set horkage */
238875683fe7STejun Heo 	dev->horkage |= ata_dev_blacklisted(dev);
238933267325STejun Heo 	ata_force_horkage(dev);
239075683fe7STejun Heo 
239150af2fa1STejun Heo 	if (dev->horkage & ATA_HORKAGE_DISABLE) {
239250af2fa1STejun Heo 		ata_dev_printk(dev, KERN_INFO,
239350af2fa1STejun Heo 			       "unsupported device, disabling\n");
239450af2fa1STejun Heo 		ata_dev_disable(dev);
239550af2fa1STejun Heo 		return 0;
239650af2fa1STejun Heo 	}
239750af2fa1STejun Heo 
23982486fa56STejun Heo 	if ((!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) &&
23992486fa56STejun Heo 	    dev->class == ATA_DEV_ATAPI) {
24002486fa56STejun Heo 		ata_dev_printk(dev, KERN_WARNING,
24012486fa56STejun Heo 			"WARNING: ATAPI is %s, device ignored.\n",
24022486fa56STejun Heo 			atapi_enabled ? "not supported with this driver"
24032486fa56STejun Heo 				      : "disabled");
24042486fa56STejun Heo 		ata_dev_disable(dev);
24052486fa56STejun Heo 		return 0;
24062486fa56STejun Heo 	}
24072486fa56STejun Heo 
24089062712fSTejun Heo 	rc = ata_do_link_spd_horkage(dev);
24099062712fSTejun Heo 	if (rc)
24109062712fSTejun Heo 		return rc;
24119062712fSTejun Heo 
24126746544cSTejun Heo 	/* let ACPI work its magic */
24136746544cSTejun Heo 	rc = ata_acpi_on_devcfg(dev);
24146746544cSTejun Heo 	if (rc)
24156746544cSTejun Heo 		return rc;
241608573a86SKristen Carlson Accardi 
241705027adcSTejun Heo 	/* massage HPA, do it early as it might change IDENTIFY data */
241805027adcSTejun Heo 	rc = ata_hpa_resize(dev);
241905027adcSTejun Heo 	if (rc)
242005027adcSTejun Heo 		return rc;
242105027adcSTejun Heo 
2422c6fd2807SJeff Garzik 	/* print device capabilities */
2423c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2424c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_DEBUG,
2425c6fd2807SJeff Garzik 			       "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
2426c6fd2807SJeff Garzik 			       "85:%04x 86:%04x 87:%04x 88:%04x\n",
24277f5e4e8dSHarvey Harrison 			       __func__,
2428c6fd2807SJeff Garzik 			       id[49], id[82], id[83], id[84],
2429c6fd2807SJeff Garzik 			       id[85], id[86], id[87], id[88]);
2430c6fd2807SJeff Garzik 
2431c6fd2807SJeff Garzik 	/* initialize to-be-configured parameters */
2432c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_CFG_MASK;
2433c6fd2807SJeff Garzik 	dev->max_sectors = 0;
2434c6fd2807SJeff Garzik 	dev->cdb_len = 0;
2435c6fd2807SJeff Garzik 	dev->n_sectors = 0;
2436c6fd2807SJeff Garzik 	dev->cylinders = 0;
2437c6fd2807SJeff Garzik 	dev->heads = 0;
2438c6fd2807SJeff Garzik 	dev->sectors = 0;
2439e18086d6SMark Lord 	dev->multi_count = 0;
2440c6fd2807SJeff Garzik 
2441c6fd2807SJeff Garzik 	/*
2442c6fd2807SJeff Garzik 	 * common ATA, ATAPI feature tests
2443c6fd2807SJeff Garzik 	 */
2444c6fd2807SJeff Garzik 
2445c6fd2807SJeff Garzik 	/* find max transfer mode; for printk only */
2446c6fd2807SJeff Garzik 	xfer_mask = ata_id_xfermask(id);
2447c6fd2807SJeff Garzik 
2448c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2449c6fd2807SJeff Garzik 		ata_dump_id(id);
2450c6fd2807SJeff Garzik 
2451ef143d57SAlbert Lee 	/* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
2452ef143d57SAlbert Lee 	ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
2453ef143d57SAlbert Lee 			sizeof(fwrevbuf));
2454ef143d57SAlbert Lee 
2455ef143d57SAlbert Lee 	ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
2456ef143d57SAlbert Lee 			sizeof(modelbuf));
2457ef143d57SAlbert Lee 
2458c6fd2807SJeff Garzik 	/* ATA-specific feature tests */
2459c6fd2807SJeff Garzik 	if (dev->class == ATA_DEV_ATA) {
2460b352e57dSAlan Cox 		if (ata_id_is_cfa(id)) {
246162afe5d7SSergei Shtylyov 			/* CPRM may make this media unusable */
246262afe5d7SSergei Shtylyov 			if (id[ATA_ID_CFA_KEY_MGMT] & 1)
246344877b4eSTejun Heo 				ata_dev_printk(dev, KERN_WARNING,
246444877b4eSTejun Heo 					       "supports DRM functions and may "
246544877b4eSTejun Heo 					       "not be fully accessable.\n");
2466b352e57dSAlan Cox 			snprintf(revbuf, 7, "CFA");
2467ae8d4ee7SAlan Cox 		} else {
2468b352e57dSAlan Cox 			snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
2469ae8d4ee7SAlan Cox 			/* Warn the user if the device has TPM extensions */
2470ae8d4ee7SAlan Cox 			if (ata_id_has_tpm(id))
2471ae8d4ee7SAlan Cox 				ata_dev_printk(dev, KERN_WARNING,
2472ae8d4ee7SAlan Cox 					       "supports DRM functions and may "
2473ae8d4ee7SAlan Cox 					       "not be fully accessable.\n");
2474ae8d4ee7SAlan Cox 		}
2475b352e57dSAlan Cox 
2476c6fd2807SJeff Garzik 		dev->n_sectors = ata_id_n_sectors(id);
2477c6fd2807SJeff Garzik 
2478e18086d6SMark Lord 		/* get current R/W Multiple count setting */
2479e18086d6SMark Lord 		if ((dev->id[47] >> 8) == 0x80 && (dev->id[59] & 0x100)) {
2480e18086d6SMark Lord 			unsigned int max = dev->id[47] & 0xff;
2481e18086d6SMark Lord 			unsigned int cnt = dev->id[59] & 0xff;
2482e18086d6SMark Lord 			/* only recognize/allow powers of two here */
2483e18086d6SMark Lord 			if (is_power_of_2(max) && is_power_of_2(cnt))
2484e18086d6SMark Lord 				if (cnt <= max)
2485e18086d6SMark Lord 					dev->multi_count = cnt;
2486e18086d6SMark Lord 		}
24873f64f565SEric D. Mudama 
2488c6fd2807SJeff Garzik 		if (ata_id_has_lba(id)) {
2489c6fd2807SJeff Garzik 			const char *lba_desc;
2490388539f3SShaohua Li 			char ncq_desc[24];
2491c6fd2807SJeff Garzik 
2492c6fd2807SJeff Garzik 			lba_desc = "LBA";
2493c6fd2807SJeff Garzik 			dev->flags |= ATA_DFLAG_LBA;
2494c6fd2807SJeff Garzik 			if (ata_id_has_lba48(id)) {
2495c6fd2807SJeff Garzik 				dev->flags |= ATA_DFLAG_LBA48;
2496c6fd2807SJeff Garzik 				lba_desc = "LBA48";
24976fc49adbSTejun Heo 
24986fc49adbSTejun Heo 				if (dev->n_sectors >= (1UL << 28) &&
24996fc49adbSTejun Heo 				    ata_id_has_flush_ext(id))
25006fc49adbSTejun Heo 					dev->flags |= ATA_DFLAG_FLUSH_EXT;
2501c6fd2807SJeff Garzik 			}
2502c6fd2807SJeff Garzik 
2503c6fd2807SJeff Garzik 			/* config NCQ */
2504388539f3SShaohua Li 			rc = ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
2505388539f3SShaohua Li 			if (rc)
2506388539f3SShaohua Li 				return rc;
2507c6fd2807SJeff Garzik 
2508c6fd2807SJeff Garzik 			/* print device info to dmesg */
25093f64f565SEric D. Mudama 			if (ata_msg_drv(ap) && print_info) {
25103f64f565SEric D. Mudama 				ata_dev_printk(dev, KERN_INFO,
25113f64f565SEric D. Mudama 					"%s: %s, %s, max %s\n",
25123f64f565SEric D. Mudama 					revbuf, modelbuf, fwrevbuf,
25133f64f565SEric D. Mudama 					ata_mode_string(xfer_mask));
25143f64f565SEric D. Mudama 				ata_dev_printk(dev, KERN_INFO,
25153f64f565SEric D. Mudama 					"%Lu sectors, multi %u: %s %s\n",
2516c6fd2807SJeff Garzik 					(unsigned long long)dev->n_sectors,
25173f64f565SEric D. Mudama 					dev->multi_count, lba_desc, ncq_desc);
25183f64f565SEric D. Mudama 			}
2519c6fd2807SJeff Garzik 		} else {
2520c6fd2807SJeff Garzik 			/* CHS */
2521c6fd2807SJeff Garzik 
2522c6fd2807SJeff Garzik 			/* Default translation */
2523c6fd2807SJeff Garzik 			dev->cylinders	= id[1];
2524c6fd2807SJeff Garzik 			dev->heads	= id[3];
2525c6fd2807SJeff Garzik 			dev->sectors	= id[6];
2526c6fd2807SJeff Garzik 
2527c6fd2807SJeff Garzik 			if (ata_id_current_chs_valid(id)) {
2528c6fd2807SJeff Garzik 				/* Current CHS translation is valid. */
2529c6fd2807SJeff Garzik 				dev->cylinders = id[54];
2530c6fd2807SJeff Garzik 				dev->heads     = id[55];
2531c6fd2807SJeff Garzik 				dev->sectors   = id[56];
2532c6fd2807SJeff Garzik 			}
2533c6fd2807SJeff Garzik 
2534c6fd2807SJeff Garzik 			/* print device info to dmesg */
25353f64f565SEric D. Mudama 			if (ata_msg_drv(ap) && print_info) {
2536c6fd2807SJeff Garzik 				ata_dev_printk(dev, KERN_INFO,
25373f64f565SEric D. Mudama 					"%s: %s, %s, max %s\n",
25383f64f565SEric D. Mudama 					revbuf,	modelbuf, fwrevbuf,
25393f64f565SEric D. Mudama 					ata_mode_string(xfer_mask));
25403f64f565SEric D. Mudama 				ata_dev_printk(dev, KERN_INFO,
25413f64f565SEric D. Mudama 					"%Lu sectors, multi %u, CHS %u/%u/%u\n",
25423f64f565SEric D. Mudama 					(unsigned long long)dev->n_sectors,
25433f64f565SEric D. Mudama 					dev->multi_count, dev->cylinders,
25443f64f565SEric D. Mudama 					dev->heads, dev->sectors);
25453f64f565SEric D. Mudama 			}
2546c6fd2807SJeff Garzik 		}
2547c6fd2807SJeff Garzik 
2548c6fd2807SJeff Garzik 		dev->cdb_len = 16;
2549c6fd2807SJeff Garzik 	}
2550c6fd2807SJeff Garzik 
2551c6fd2807SJeff Garzik 	/* ATAPI-specific feature tests */
2552c6fd2807SJeff Garzik 	else if (dev->class == ATA_DEV_ATAPI) {
2553854c73a2STejun Heo 		const char *cdb_intr_string = "";
2554854c73a2STejun Heo 		const char *atapi_an_string = "";
255591163006STejun Heo 		const char *dma_dir_string = "";
25567d77b247STejun Heo 		u32 sntf;
2557c6fd2807SJeff Garzik 
2558c6fd2807SJeff Garzik 		rc = atapi_cdb_len(id);
2559c6fd2807SJeff Garzik 		if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
2560c6fd2807SJeff Garzik 			if (ata_msg_warn(ap))
2561c6fd2807SJeff Garzik 				ata_dev_printk(dev, KERN_WARNING,
2562c6fd2807SJeff Garzik 					       "unsupported CDB len\n");
2563c6fd2807SJeff Garzik 			rc = -EINVAL;
2564c6fd2807SJeff Garzik 			goto err_out_nosup;
2565c6fd2807SJeff Garzik 		}
2566c6fd2807SJeff Garzik 		dev->cdb_len = (unsigned int) rc;
2567c6fd2807SJeff Garzik 
25687d77b247STejun Heo 		/* Enable ATAPI AN if both the host and device have
25697d77b247STejun Heo 		 * the support.  If PMP is attached, SNTF is required
25707d77b247STejun Heo 		 * to enable ATAPI AN to discern between PHY status
25717d77b247STejun Heo 		 * changed notifications and ATAPI ANs.
25729f45cbd3SKristen Carlson Accardi 		 */
25737d77b247STejun Heo 		if ((ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
2574071f44b1STejun Heo 		    (!sata_pmp_attached(ap) ||
25757d77b247STejun Heo 		     sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
2576854c73a2STejun Heo 			unsigned int err_mask;
2577854c73a2STejun Heo 
25789f45cbd3SKristen Carlson Accardi 			/* issue SET feature command to turn this on */
2579218f3d30SJeff Garzik 			err_mask = ata_dev_set_feature(dev,
2580218f3d30SJeff Garzik 					SETFEATURES_SATA_ENABLE, SATA_AN);
2581854c73a2STejun Heo 			if (err_mask)
25829f45cbd3SKristen Carlson Accardi 				ata_dev_printk(dev, KERN_ERR,
2583854c73a2STejun Heo 					"failed to enable ATAPI AN "
2584854c73a2STejun Heo 					"(err_mask=0x%x)\n", err_mask);
2585854c73a2STejun Heo 			else {
25869f45cbd3SKristen Carlson Accardi 				dev->flags |= ATA_DFLAG_AN;
2587854c73a2STejun Heo 				atapi_an_string = ", ATAPI AN";
2588854c73a2STejun Heo 			}
25899f45cbd3SKristen Carlson Accardi 		}
25909f45cbd3SKristen Carlson Accardi 
2591c6fd2807SJeff Garzik 		if (ata_id_cdb_intr(dev->id)) {
2592c6fd2807SJeff Garzik 			dev->flags |= ATA_DFLAG_CDB_INTR;
2593c6fd2807SJeff Garzik 			cdb_intr_string = ", CDB intr";
2594c6fd2807SJeff Garzik 		}
2595c6fd2807SJeff Garzik 
259691163006STejun Heo 		if (atapi_dmadir || atapi_id_dmadir(dev->id)) {
259791163006STejun Heo 			dev->flags |= ATA_DFLAG_DMADIR;
259891163006STejun Heo 			dma_dir_string = ", DMADIR";
259991163006STejun Heo 		}
260091163006STejun Heo 
2601c6fd2807SJeff Garzik 		/* print device info to dmesg */
2602c6fd2807SJeff Garzik 		if (ata_msg_drv(ap) && print_info)
2603ef143d57SAlbert Lee 			ata_dev_printk(dev, KERN_INFO,
260491163006STejun Heo 				       "ATAPI: %s, %s, max %s%s%s%s\n",
2605ef143d57SAlbert Lee 				       modelbuf, fwrevbuf,
2606c6fd2807SJeff Garzik 				       ata_mode_string(xfer_mask),
260791163006STejun Heo 				       cdb_intr_string, atapi_an_string,
260891163006STejun Heo 				       dma_dir_string);
2609c6fd2807SJeff Garzik 	}
2610c6fd2807SJeff Garzik 
2611914ed354STejun Heo 	/* determine max_sectors */
2612914ed354STejun Heo 	dev->max_sectors = ATA_MAX_SECTORS;
2613914ed354STejun Heo 	if (dev->flags & ATA_DFLAG_LBA48)
2614914ed354STejun Heo 		dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2615914ed354STejun Heo 
2616ca77329fSKristen Carlson Accardi 	if (!(dev->horkage & ATA_HORKAGE_IPM)) {
2617ca77329fSKristen Carlson Accardi 		if (ata_id_has_hipm(dev->id))
2618ca77329fSKristen Carlson Accardi 			dev->flags |= ATA_DFLAG_HIPM;
2619ca77329fSKristen Carlson Accardi 		if (ata_id_has_dipm(dev->id))
2620ca77329fSKristen Carlson Accardi 			dev->flags |= ATA_DFLAG_DIPM;
2621ca77329fSKristen Carlson Accardi 	}
2622ca77329fSKristen Carlson Accardi 
2623c5038fc0SAlan Cox 	/* Limit PATA drive on SATA cable bridge transfers to udma5,
2624c5038fc0SAlan Cox 	   200 sectors */
2625c6fd2807SJeff Garzik 	if (ata_dev_knobble(dev)) {
2626c6fd2807SJeff Garzik 		if (ata_msg_drv(ap) && print_info)
2627c6fd2807SJeff Garzik 			ata_dev_printk(dev, KERN_INFO,
2628c6fd2807SJeff Garzik 				       "applying bridge limits\n");
2629c6fd2807SJeff Garzik 		dev->udma_mask &= ATA_UDMA5;
2630c6fd2807SJeff Garzik 		dev->max_sectors = ATA_MAX_SECTORS;
2631c6fd2807SJeff Garzik 	}
2632c6fd2807SJeff Garzik 
2633f8d8e579STony Battersby 	if ((dev->class == ATA_DEV_ATAPI) &&
2634f442cd86SAlbert Lee 	    (atapi_command_packet_set(id) == TYPE_TAPE)) {
2635f8d8e579STony Battersby 		dev->max_sectors = ATA_MAX_SECTORS_TAPE;
2636f442cd86SAlbert Lee 		dev->horkage |= ATA_HORKAGE_STUCK_ERR;
2637f442cd86SAlbert Lee 	}
2638f8d8e579STony Battersby 
263975683fe7STejun Heo 	if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
264003ec52deSTejun Heo 		dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
264103ec52deSTejun Heo 					 dev->max_sectors);
264218d6e9d5SAlbert Lee 
2643ca77329fSKristen Carlson Accardi 	if (ata_dev_blacklisted(dev) & ATA_HORKAGE_IPM) {
2644ca77329fSKristen Carlson Accardi 		dev->horkage |= ATA_HORKAGE_IPM;
2645ca77329fSKristen Carlson Accardi 
2646ca77329fSKristen Carlson Accardi 		/* reset link pm_policy for this port to no pm */
2647ca77329fSKristen Carlson Accardi 		ap->pm_policy = MAX_PERFORMANCE;
2648ca77329fSKristen Carlson Accardi 	}
2649ca77329fSKristen Carlson Accardi 
2650c6fd2807SJeff Garzik 	if (ap->ops->dev_config)
2651cd0d3bbcSAlan 		ap->ops->dev_config(dev);
2652c6fd2807SJeff Garzik 
2653c5038fc0SAlan Cox 	if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2654c5038fc0SAlan Cox 		/* Let the user know. We don't want to disallow opens for
2655c5038fc0SAlan Cox 		   rescue purposes, or in case the vendor is just a blithering
2656c5038fc0SAlan Cox 		   idiot. Do this after the dev_config call as some controllers
2657c5038fc0SAlan Cox 		   with buggy firmware may want to avoid reporting false device
2658c5038fc0SAlan Cox 		   bugs */
2659c5038fc0SAlan Cox 
2660c5038fc0SAlan Cox 		if (print_info) {
2661c5038fc0SAlan Cox 			ata_dev_printk(dev, KERN_WARNING,
2662c5038fc0SAlan Cox "Drive reports diagnostics failure. This may indicate a drive\n");
2663c5038fc0SAlan Cox 			ata_dev_printk(dev, KERN_WARNING,
2664c5038fc0SAlan Cox "fault or invalid emulation. Contact drive vendor for information.\n");
2665c5038fc0SAlan Cox 		}
2666c5038fc0SAlan Cox 	}
2667c5038fc0SAlan Cox 
2668ac70a964STejun Heo 	if ((dev->horkage & ATA_HORKAGE_FIRMWARE_WARN) && print_info) {
2669ac70a964STejun Heo 		ata_dev_printk(dev, KERN_WARNING, "WARNING: device requires "
2670ac70a964STejun Heo 			       "firmware update to be fully functional.\n");
2671ac70a964STejun Heo 		ata_dev_printk(dev, KERN_WARNING, "         contact the vendor "
2672ac70a964STejun Heo 			       "or visit http://ata.wiki.kernel.org.\n");
2673ac70a964STejun Heo 	}
2674ac70a964STejun Heo 
2675c6fd2807SJeff Garzik 	return 0;
2676c6fd2807SJeff Garzik 
2677c6fd2807SJeff Garzik err_out_nosup:
2678c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2679c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_DEBUG,
26807f5e4e8dSHarvey Harrison 			       "%s: EXIT, err\n", __func__);
2681c6fd2807SJeff Garzik 	return rc;
2682c6fd2807SJeff Garzik }
2683c6fd2807SJeff Garzik 
2684c6fd2807SJeff Garzik /**
26852e41e8e6SAlan Cox  *	ata_cable_40wire	-	return 40 wire cable type
2686be0d18dfSAlan Cox  *	@ap: port
2687be0d18dfSAlan Cox  *
26882e41e8e6SAlan Cox  *	Helper method for drivers which want to hardwire 40 wire cable
2689be0d18dfSAlan Cox  *	detection.
2690be0d18dfSAlan Cox  */
2691be0d18dfSAlan Cox 
2692be0d18dfSAlan Cox int ata_cable_40wire(struct ata_port *ap)
2693be0d18dfSAlan Cox {
2694be0d18dfSAlan Cox 	return ATA_CBL_PATA40;
2695be0d18dfSAlan Cox }
2696be0d18dfSAlan Cox 
2697be0d18dfSAlan Cox /**
26982e41e8e6SAlan Cox  *	ata_cable_80wire	-	return 80 wire cable type
2699be0d18dfSAlan Cox  *	@ap: port
2700be0d18dfSAlan Cox  *
27012e41e8e6SAlan Cox  *	Helper method for drivers which want to hardwire 80 wire cable
2702be0d18dfSAlan Cox  *	detection.
2703be0d18dfSAlan Cox  */
2704be0d18dfSAlan Cox 
2705be0d18dfSAlan Cox int ata_cable_80wire(struct ata_port *ap)
2706be0d18dfSAlan Cox {
2707be0d18dfSAlan Cox 	return ATA_CBL_PATA80;
2708be0d18dfSAlan Cox }
2709be0d18dfSAlan Cox 
2710be0d18dfSAlan Cox /**
2711be0d18dfSAlan Cox  *	ata_cable_unknown	-	return unknown PATA cable.
2712be0d18dfSAlan Cox  *	@ap: port
2713be0d18dfSAlan Cox  *
2714be0d18dfSAlan Cox  *	Helper method for drivers which have no PATA cable detection.
2715be0d18dfSAlan Cox  */
2716be0d18dfSAlan Cox 
2717be0d18dfSAlan Cox int ata_cable_unknown(struct ata_port *ap)
2718be0d18dfSAlan Cox {
2719be0d18dfSAlan Cox 	return ATA_CBL_PATA_UNK;
2720be0d18dfSAlan Cox }
2721be0d18dfSAlan Cox 
2722be0d18dfSAlan Cox /**
2723c88f90c3STejun Heo  *	ata_cable_ignore	-	return ignored PATA cable.
2724c88f90c3STejun Heo  *	@ap: port
2725c88f90c3STejun Heo  *
2726c88f90c3STejun Heo  *	Helper method for drivers which don't use cable type to limit
2727c88f90c3STejun Heo  *	transfer mode.
2728c88f90c3STejun Heo  */
2729c88f90c3STejun Heo int ata_cable_ignore(struct ata_port *ap)
2730c88f90c3STejun Heo {
2731c88f90c3STejun Heo 	return ATA_CBL_PATA_IGN;
2732c88f90c3STejun Heo }
2733c88f90c3STejun Heo 
2734c88f90c3STejun Heo /**
2735be0d18dfSAlan Cox  *	ata_cable_sata	-	return SATA cable type
2736be0d18dfSAlan Cox  *	@ap: port
2737be0d18dfSAlan Cox  *
2738be0d18dfSAlan Cox  *	Helper method for drivers which have SATA cables
2739be0d18dfSAlan Cox  */
2740be0d18dfSAlan Cox 
2741be0d18dfSAlan Cox int ata_cable_sata(struct ata_port *ap)
2742be0d18dfSAlan Cox {
2743be0d18dfSAlan Cox 	return ATA_CBL_SATA;
2744be0d18dfSAlan Cox }
2745be0d18dfSAlan Cox 
2746be0d18dfSAlan Cox /**
2747c6fd2807SJeff Garzik  *	ata_bus_probe - Reset and probe ATA bus
2748c6fd2807SJeff Garzik  *	@ap: Bus to probe
2749c6fd2807SJeff Garzik  *
2750c6fd2807SJeff Garzik  *	Master ATA bus probing function.  Initiates a hardware-dependent
2751c6fd2807SJeff Garzik  *	bus reset, then attempts to identify any devices found on
2752c6fd2807SJeff Garzik  *	the bus.
2753c6fd2807SJeff Garzik  *
2754c6fd2807SJeff Garzik  *	LOCKING:
2755c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
2756c6fd2807SJeff Garzik  *
2757c6fd2807SJeff Garzik  *	RETURNS:
2758c6fd2807SJeff Garzik  *	Zero on success, negative errno otherwise.
2759c6fd2807SJeff Garzik  */
2760c6fd2807SJeff Garzik 
2761c6fd2807SJeff Garzik int ata_bus_probe(struct ata_port *ap)
2762c6fd2807SJeff Garzik {
2763c6fd2807SJeff Garzik 	unsigned int classes[ATA_MAX_DEVICES];
2764c6fd2807SJeff Garzik 	int tries[ATA_MAX_DEVICES];
2765f58229f8STejun Heo 	int rc;
2766c6fd2807SJeff Garzik 	struct ata_device *dev;
2767c6fd2807SJeff Garzik 
2768c6fd2807SJeff Garzik 	ata_port_probe(ap);
2769c6fd2807SJeff Garzik 
27701eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL)
2771f58229f8STejun Heo 		tries[dev->devno] = ATA_PROBE_MAX_TRIES;
2772c6fd2807SJeff Garzik 
2773c6fd2807SJeff Garzik  retry:
27741eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL) {
2775cdeab114STejun Heo 		/* If we issue an SRST then an ATA drive (not ATAPI)
2776cdeab114STejun Heo 		 * may change configuration and be in PIO0 timing. If
2777cdeab114STejun Heo 		 * we do a hard reset (or are coming from power on)
2778cdeab114STejun Heo 		 * this is true for ATA or ATAPI. Until we've set a
2779cdeab114STejun Heo 		 * suitable controller mode we should not touch the
2780cdeab114STejun Heo 		 * bus as we may be talking too fast.
2781cdeab114STejun Heo 		 */
2782cdeab114STejun Heo 		dev->pio_mode = XFER_PIO_0;
2783cdeab114STejun Heo 
2784cdeab114STejun Heo 		/* If the controller has a pio mode setup function
2785cdeab114STejun Heo 		 * then use it to set the chipset to rights. Don't
2786cdeab114STejun Heo 		 * touch the DMA setup as that will be dealt with when
2787cdeab114STejun Heo 		 * configuring devices.
2788cdeab114STejun Heo 		 */
2789cdeab114STejun Heo 		if (ap->ops->set_piomode)
2790cdeab114STejun Heo 			ap->ops->set_piomode(ap, dev);
2791cdeab114STejun Heo 	}
2792cdeab114STejun Heo 
2793c6fd2807SJeff Garzik 	/* reset and determine device classes */
2794c6fd2807SJeff Garzik 	ap->ops->phy_reset(ap);
2795c6fd2807SJeff Garzik 
27961eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL) {
2797c6fd2807SJeff Garzik 		if (!(ap->flags & ATA_FLAG_DISABLED) &&
2798c6fd2807SJeff Garzik 		    dev->class != ATA_DEV_UNKNOWN)
2799c6fd2807SJeff Garzik 			classes[dev->devno] = dev->class;
2800c6fd2807SJeff Garzik 		else
2801c6fd2807SJeff Garzik 			classes[dev->devno] = ATA_DEV_NONE;
2802c6fd2807SJeff Garzik 
2803c6fd2807SJeff Garzik 		dev->class = ATA_DEV_UNKNOWN;
2804c6fd2807SJeff Garzik 	}
2805c6fd2807SJeff Garzik 
2806c6fd2807SJeff Garzik 	ata_port_probe(ap);
2807c6fd2807SJeff Garzik 
2808f31f0cc2SJeff Garzik 	/* read IDENTIFY page and configure devices. We have to do the identify
2809f31f0cc2SJeff Garzik 	   specific sequence bass-ackwards so that PDIAG- is released by
2810f31f0cc2SJeff Garzik 	   the slave device */
2811f31f0cc2SJeff Garzik 
28121eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL_REVERSE) {
2813f58229f8STejun Heo 		if (tries[dev->devno])
2814f58229f8STejun Heo 			dev->class = classes[dev->devno];
2815c6fd2807SJeff Garzik 
2816c6fd2807SJeff Garzik 		if (!ata_dev_enabled(dev))
2817c6fd2807SJeff Garzik 			continue;
2818c6fd2807SJeff Garzik 
2819bff04647STejun Heo 		rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2820bff04647STejun Heo 				     dev->id);
2821c6fd2807SJeff Garzik 		if (rc)
2822c6fd2807SJeff Garzik 			goto fail;
2823f31f0cc2SJeff Garzik 	}
2824f31f0cc2SJeff Garzik 
2825be0d18dfSAlan Cox 	/* Now ask for the cable type as PDIAG- should have been released */
2826be0d18dfSAlan Cox 	if (ap->ops->cable_detect)
2827be0d18dfSAlan Cox 		ap->cbl = ap->ops->cable_detect(ap);
2828be0d18dfSAlan Cox 
28291eca4365STejun Heo 	/* We may have SATA bridge glue hiding here irrespective of
28301eca4365STejun Heo 	 * the reported cable types and sensed types.  When SATA
28311eca4365STejun Heo 	 * drives indicate we have a bridge, we don't know which end
28321eca4365STejun Heo 	 * of the link the bridge is which is a problem.
28331eca4365STejun Heo 	 */
28341eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED)
2835614fe29bSAlan Cox 		if (ata_id_is_sata(dev->id))
2836614fe29bSAlan Cox 			ap->cbl = ATA_CBL_SATA;
2837614fe29bSAlan Cox 
2838f31f0cc2SJeff Garzik 	/* After the identify sequence we can now set up the devices. We do
2839f31f0cc2SJeff Garzik 	   this in the normal order so that the user doesn't get confused */
2840f31f0cc2SJeff Garzik 
28411eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED) {
28429af5c9c9STejun Heo 		ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
2843efdaedc4STejun Heo 		rc = ata_dev_configure(dev);
28449af5c9c9STejun Heo 		ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
2845c6fd2807SJeff Garzik 		if (rc)
2846c6fd2807SJeff Garzik 			goto fail;
2847c6fd2807SJeff Garzik 	}
2848c6fd2807SJeff Garzik 
2849c6fd2807SJeff Garzik 	/* configure transfer mode */
28500260731fSTejun Heo 	rc = ata_set_mode(&ap->link, &dev);
28514ae72a1eSTejun Heo 	if (rc)
2852c6fd2807SJeff Garzik 		goto fail;
2853c6fd2807SJeff Garzik 
28541eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED)
2855c6fd2807SJeff Garzik 		return 0;
2856c6fd2807SJeff Garzik 
2857c6fd2807SJeff Garzik 	/* no device present, disable port */
2858c6fd2807SJeff Garzik 	ata_port_disable(ap);
2859c6fd2807SJeff Garzik 	return -ENODEV;
2860c6fd2807SJeff Garzik 
2861c6fd2807SJeff Garzik  fail:
28624ae72a1eSTejun Heo 	tries[dev->devno]--;
28634ae72a1eSTejun Heo 
2864c6fd2807SJeff Garzik 	switch (rc) {
2865c6fd2807SJeff Garzik 	case -EINVAL:
28664ae72a1eSTejun Heo 		/* eeek, something went very wrong, give up */
2867c6fd2807SJeff Garzik 		tries[dev->devno] = 0;
2868c6fd2807SJeff Garzik 		break;
28694ae72a1eSTejun Heo 
28704ae72a1eSTejun Heo 	case -ENODEV:
28714ae72a1eSTejun Heo 		/* give it just one more chance */
28724ae72a1eSTejun Heo 		tries[dev->devno] = min(tries[dev->devno], 1);
2873c6fd2807SJeff Garzik 	case -EIO:
28744ae72a1eSTejun Heo 		if (tries[dev->devno] == 1) {
28754ae72a1eSTejun Heo 			/* This is the last chance, better to slow
28764ae72a1eSTejun Heo 			 * down than lose it.
28774ae72a1eSTejun Heo 			 */
2878a07d499bSTejun Heo 			sata_down_spd_limit(&ap->link, 0);
28794ae72a1eSTejun Heo 			ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
28804ae72a1eSTejun Heo 		}
2881c6fd2807SJeff Garzik 	}
2882c6fd2807SJeff Garzik 
28834ae72a1eSTejun Heo 	if (!tries[dev->devno])
2884c6fd2807SJeff Garzik 		ata_dev_disable(dev);
2885c6fd2807SJeff Garzik 
2886c6fd2807SJeff Garzik 	goto retry;
2887c6fd2807SJeff Garzik }
2888c6fd2807SJeff Garzik 
2889c6fd2807SJeff Garzik /**
2890c6fd2807SJeff Garzik  *	ata_port_probe - Mark port as enabled
2891c6fd2807SJeff Garzik  *	@ap: Port for which we indicate enablement
2892c6fd2807SJeff Garzik  *
2893c6fd2807SJeff Garzik  *	Modify @ap data structure such that the system
2894c6fd2807SJeff Garzik  *	thinks that the entire port is enabled.
2895c6fd2807SJeff Garzik  *
2896cca3974eSJeff Garzik  *	LOCKING: host lock, or some other form of
2897c6fd2807SJeff Garzik  *	serialization.
2898c6fd2807SJeff Garzik  */
2899c6fd2807SJeff Garzik 
2900c6fd2807SJeff Garzik void ata_port_probe(struct ata_port *ap)
2901c6fd2807SJeff Garzik {
2902c6fd2807SJeff Garzik 	ap->flags &= ~ATA_FLAG_DISABLED;
2903c6fd2807SJeff Garzik }
2904c6fd2807SJeff Garzik 
2905c6fd2807SJeff Garzik /**
2906c6fd2807SJeff Garzik  *	sata_print_link_status - Print SATA link status
2907936fd732STejun Heo  *	@link: SATA link to printk link status about
2908c6fd2807SJeff Garzik  *
2909c6fd2807SJeff Garzik  *	This function prints link speed and status of a SATA link.
2910c6fd2807SJeff Garzik  *
2911c6fd2807SJeff Garzik  *	LOCKING:
2912c6fd2807SJeff Garzik  *	None.
2913c6fd2807SJeff Garzik  */
29146bdb4fc9SAdrian Bunk static void sata_print_link_status(struct ata_link *link)
2915c6fd2807SJeff Garzik {
2916c6fd2807SJeff Garzik 	u32 sstatus, scontrol, tmp;
2917c6fd2807SJeff Garzik 
2918936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus))
2919c6fd2807SJeff Garzik 		return;
2920936fd732STejun Heo 	sata_scr_read(link, SCR_CONTROL, &scontrol);
2921c6fd2807SJeff Garzik 
2922b1c72916STejun Heo 	if (ata_phys_link_online(link)) {
2923c6fd2807SJeff Garzik 		tmp = (sstatus >> 4) & 0xf;
2924936fd732STejun Heo 		ata_link_printk(link, KERN_INFO,
2925c6fd2807SJeff Garzik 				"SATA link up %s (SStatus %X SControl %X)\n",
2926c6fd2807SJeff Garzik 				sata_spd_string(tmp), sstatus, scontrol);
2927c6fd2807SJeff Garzik 	} else {
2928936fd732STejun Heo 		ata_link_printk(link, KERN_INFO,
2929c6fd2807SJeff Garzik 				"SATA link down (SStatus %X SControl %X)\n",
2930c6fd2807SJeff Garzik 				sstatus, scontrol);
2931c6fd2807SJeff Garzik 	}
2932c6fd2807SJeff Garzik }
2933c6fd2807SJeff Garzik 
2934c6fd2807SJeff Garzik /**
2935c6fd2807SJeff Garzik  *	ata_dev_pair		-	return other device on cable
2936c6fd2807SJeff Garzik  *	@adev: device
2937c6fd2807SJeff Garzik  *
2938c6fd2807SJeff Garzik  *	Obtain the other device on the same cable, or if none is
2939c6fd2807SJeff Garzik  *	present NULL is returned
2940c6fd2807SJeff Garzik  */
2941c6fd2807SJeff Garzik 
2942c6fd2807SJeff Garzik struct ata_device *ata_dev_pair(struct ata_device *adev)
2943c6fd2807SJeff Garzik {
29449af5c9c9STejun Heo 	struct ata_link *link = adev->link;
29459af5c9c9STejun Heo 	struct ata_device *pair = &link->device[1 - adev->devno];
2946c6fd2807SJeff Garzik 	if (!ata_dev_enabled(pair))
2947c6fd2807SJeff Garzik 		return NULL;
2948c6fd2807SJeff Garzik 	return pair;
2949c6fd2807SJeff Garzik }
2950c6fd2807SJeff Garzik 
2951c6fd2807SJeff Garzik /**
2952c6fd2807SJeff Garzik  *	ata_port_disable - Disable port.
2953c6fd2807SJeff Garzik  *	@ap: Port to be disabled.
2954c6fd2807SJeff Garzik  *
2955c6fd2807SJeff Garzik  *	Modify @ap data structure such that the system
2956c6fd2807SJeff Garzik  *	thinks that the entire port is disabled, and should
2957c6fd2807SJeff Garzik  *	never attempt to probe or communicate with devices
2958c6fd2807SJeff Garzik  *	on this port.
2959c6fd2807SJeff Garzik  *
2960cca3974eSJeff Garzik  *	LOCKING: host lock, or some other form of
2961c6fd2807SJeff Garzik  *	serialization.
2962c6fd2807SJeff Garzik  */
2963c6fd2807SJeff Garzik 
2964c6fd2807SJeff Garzik void ata_port_disable(struct ata_port *ap)
2965c6fd2807SJeff Garzik {
29669af5c9c9STejun Heo 	ap->link.device[0].class = ATA_DEV_NONE;
29679af5c9c9STejun Heo 	ap->link.device[1].class = ATA_DEV_NONE;
2968c6fd2807SJeff Garzik 	ap->flags |= ATA_FLAG_DISABLED;
2969c6fd2807SJeff Garzik }
2970c6fd2807SJeff Garzik 
2971c6fd2807SJeff Garzik /**
2972c6fd2807SJeff Garzik  *	sata_down_spd_limit - adjust SATA spd limit downward
2973936fd732STejun Heo  *	@link: Link to adjust SATA spd limit for
2974a07d499bSTejun Heo  *	@spd_limit: Additional limit
2975c6fd2807SJeff Garzik  *
2976936fd732STejun Heo  *	Adjust SATA spd limit of @link downward.  Note that this
2977c6fd2807SJeff Garzik  *	function only adjusts the limit.  The change must be applied
2978c6fd2807SJeff Garzik  *	using sata_set_spd().
2979c6fd2807SJeff Garzik  *
2980a07d499bSTejun Heo  *	If @spd_limit is non-zero, the speed is limited to equal to or
2981a07d499bSTejun Heo  *	lower than @spd_limit if such speed is supported.  If
2982a07d499bSTejun Heo  *	@spd_limit is slower than any supported speed, only the lowest
2983a07d499bSTejun Heo  *	supported speed is allowed.
2984a07d499bSTejun Heo  *
2985c6fd2807SJeff Garzik  *	LOCKING:
2986c6fd2807SJeff Garzik  *	Inherited from caller.
2987c6fd2807SJeff Garzik  *
2988c6fd2807SJeff Garzik  *	RETURNS:
2989c6fd2807SJeff Garzik  *	0 on success, negative errno on failure
2990c6fd2807SJeff Garzik  */
2991a07d499bSTejun Heo int sata_down_spd_limit(struct ata_link *link, u32 spd_limit)
2992c6fd2807SJeff Garzik {
2993c6fd2807SJeff Garzik 	u32 sstatus, spd, mask;
2994a07d499bSTejun Heo 	int rc, bit;
2995c6fd2807SJeff Garzik 
2996936fd732STejun Heo 	if (!sata_scr_valid(link))
2997008a7896STejun Heo 		return -EOPNOTSUPP;
2998008a7896STejun Heo 
2999008a7896STejun Heo 	/* If SCR can be read, use it to determine the current SPD.
3000936fd732STejun Heo 	 * If not, use cached value in link->sata_spd.
3001008a7896STejun Heo 	 */
3002936fd732STejun Heo 	rc = sata_scr_read(link, SCR_STATUS, &sstatus);
30039913ff8aSTejun Heo 	if (rc == 0 && ata_sstatus_online(sstatus))
3004008a7896STejun Heo 		spd = (sstatus >> 4) & 0xf;
3005008a7896STejun Heo 	else
3006936fd732STejun Heo 		spd = link->sata_spd;
3007c6fd2807SJeff Garzik 
3008936fd732STejun Heo 	mask = link->sata_spd_limit;
3009c6fd2807SJeff Garzik 	if (mask <= 1)
3010c6fd2807SJeff Garzik 		return -EINVAL;
3011008a7896STejun Heo 
3012008a7896STejun Heo 	/* unconditionally mask off the highest bit */
3013a07d499bSTejun Heo 	bit = fls(mask) - 1;
3014a07d499bSTejun Heo 	mask &= ~(1 << bit);
3015c6fd2807SJeff Garzik 
3016008a7896STejun Heo 	/* Mask off all speeds higher than or equal to the current
3017008a7896STejun Heo 	 * one.  Force 1.5Gbps if current SPD is not available.
3018008a7896STejun Heo 	 */
3019008a7896STejun Heo 	if (spd > 1)
3020008a7896STejun Heo 		mask &= (1 << (spd - 1)) - 1;
3021008a7896STejun Heo 	else
3022008a7896STejun Heo 		mask &= 1;
3023008a7896STejun Heo 
3024008a7896STejun Heo 	/* were we already at the bottom? */
3025c6fd2807SJeff Garzik 	if (!mask)
3026c6fd2807SJeff Garzik 		return -EINVAL;
3027c6fd2807SJeff Garzik 
3028a07d499bSTejun Heo 	if (spd_limit) {
3029a07d499bSTejun Heo 		if (mask & ((1 << spd_limit) - 1))
3030a07d499bSTejun Heo 			mask &= (1 << spd_limit) - 1;
3031a07d499bSTejun Heo 		else {
3032a07d499bSTejun Heo 			bit = ffs(mask) - 1;
3033a07d499bSTejun Heo 			mask = 1 << bit;
3034a07d499bSTejun Heo 		}
3035a07d499bSTejun Heo 	}
3036a07d499bSTejun Heo 
3037936fd732STejun Heo 	link->sata_spd_limit = mask;
3038c6fd2807SJeff Garzik 
3039936fd732STejun Heo 	ata_link_printk(link, KERN_WARNING, "limiting SATA link speed to %s\n",
3040c6fd2807SJeff Garzik 			sata_spd_string(fls(mask)));
3041c6fd2807SJeff Garzik 
3042c6fd2807SJeff Garzik 	return 0;
3043c6fd2807SJeff Garzik }
3044c6fd2807SJeff Garzik 
3045936fd732STejun Heo static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
3046c6fd2807SJeff Garzik {
30475270222fSTejun Heo 	struct ata_link *host_link = &link->ap->link;
30485270222fSTejun Heo 	u32 limit, target, spd;
3049c6fd2807SJeff Garzik 
30505270222fSTejun Heo 	limit = link->sata_spd_limit;
30515270222fSTejun Heo 
30525270222fSTejun Heo 	/* Don't configure downstream link faster than upstream link.
30535270222fSTejun Heo 	 * It doesn't speed up anything and some PMPs choke on such
30545270222fSTejun Heo 	 * configuration.
30555270222fSTejun Heo 	 */
30565270222fSTejun Heo 	if (!ata_is_host_link(link) && host_link->sata_spd)
30575270222fSTejun Heo 		limit &= (1 << host_link->sata_spd) - 1;
30585270222fSTejun Heo 
30595270222fSTejun Heo 	if (limit == UINT_MAX)
30605270222fSTejun Heo 		target = 0;
3061c6fd2807SJeff Garzik 	else
30625270222fSTejun Heo 		target = fls(limit);
3063c6fd2807SJeff Garzik 
3064c6fd2807SJeff Garzik 	spd = (*scontrol >> 4) & 0xf;
30655270222fSTejun Heo 	*scontrol = (*scontrol & ~0xf0) | ((target & 0xf) << 4);
3066c6fd2807SJeff Garzik 
30675270222fSTejun Heo 	return spd != target;
3068c6fd2807SJeff Garzik }
3069c6fd2807SJeff Garzik 
3070c6fd2807SJeff Garzik /**
3071c6fd2807SJeff Garzik  *	sata_set_spd_needed - is SATA spd configuration needed
3072936fd732STejun Heo  *	@link: Link in question
3073c6fd2807SJeff Garzik  *
3074c6fd2807SJeff Garzik  *	Test whether the spd limit in SControl matches
3075936fd732STejun Heo  *	@link->sata_spd_limit.  This function is used to determine
3076c6fd2807SJeff Garzik  *	whether hardreset is necessary to apply SATA spd
3077c6fd2807SJeff Garzik  *	configuration.
3078c6fd2807SJeff Garzik  *
3079c6fd2807SJeff Garzik  *	LOCKING:
3080c6fd2807SJeff Garzik  *	Inherited from caller.
3081c6fd2807SJeff Garzik  *
3082c6fd2807SJeff Garzik  *	RETURNS:
3083c6fd2807SJeff Garzik  *	1 if SATA spd configuration is needed, 0 otherwise.
3084c6fd2807SJeff Garzik  */
30851dc55e87SAdrian Bunk static int sata_set_spd_needed(struct ata_link *link)
3086c6fd2807SJeff Garzik {
3087c6fd2807SJeff Garzik 	u32 scontrol;
3088c6fd2807SJeff Garzik 
3089936fd732STejun Heo 	if (sata_scr_read(link, SCR_CONTROL, &scontrol))
3090db64bcf3STejun Heo 		return 1;
3091c6fd2807SJeff Garzik 
3092936fd732STejun Heo 	return __sata_set_spd_needed(link, &scontrol);
3093c6fd2807SJeff Garzik }
3094c6fd2807SJeff Garzik 
3095c6fd2807SJeff Garzik /**
3096c6fd2807SJeff Garzik  *	sata_set_spd - set SATA spd according to spd limit
3097936fd732STejun Heo  *	@link: Link to set SATA spd for
3098c6fd2807SJeff Garzik  *
3099936fd732STejun Heo  *	Set SATA spd of @link according to sata_spd_limit.
3100c6fd2807SJeff Garzik  *
3101c6fd2807SJeff Garzik  *	LOCKING:
3102c6fd2807SJeff Garzik  *	Inherited from caller.
3103c6fd2807SJeff Garzik  *
3104c6fd2807SJeff Garzik  *	RETURNS:
3105c6fd2807SJeff Garzik  *	0 if spd doesn't need to be changed, 1 if spd has been
3106c6fd2807SJeff Garzik  *	changed.  Negative errno if SCR registers are inaccessible.
3107c6fd2807SJeff Garzik  */
3108936fd732STejun Heo int sata_set_spd(struct ata_link *link)
3109c6fd2807SJeff Garzik {
3110c6fd2807SJeff Garzik 	u32 scontrol;
3111c6fd2807SJeff Garzik 	int rc;
3112c6fd2807SJeff Garzik 
3113936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3114c6fd2807SJeff Garzik 		return rc;
3115c6fd2807SJeff Garzik 
3116936fd732STejun Heo 	if (!__sata_set_spd_needed(link, &scontrol))
3117c6fd2807SJeff Garzik 		return 0;
3118c6fd2807SJeff Garzik 
3119936fd732STejun Heo 	if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3120c6fd2807SJeff Garzik 		return rc;
3121c6fd2807SJeff Garzik 
3122c6fd2807SJeff Garzik 	return 1;
3123c6fd2807SJeff Garzik }
3124c6fd2807SJeff Garzik 
3125c6fd2807SJeff Garzik /*
3126c6fd2807SJeff Garzik  * This mode timing computation functionality is ported over from
3127c6fd2807SJeff Garzik  * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
3128c6fd2807SJeff Garzik  */
3129c6fd2807SJeff Garzik /*
3130b352e57dSAlan Cox  * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
3131c6fd2807SJeff Garzik  * These were taken from ATA/ATAPI-6 standard, rev 0a, except
3132b352e57dSAlan Cox  * for UDMA6, which is currently supported only by Maxtor drives.
3133b352e57dSAlan Cox  *
3134b352e57dSAlan Cox  * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0.
3135c6fd2807SJeff Garzik  */
3136c6fd2807SJeff Garzik 
3137c6fd2807SJeff Garzik static const struct ata_timing ata_timing[] = {
31383ada9c12SDavid Daney /*	{ XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 0,  960,   0 }, */
31393ada9c12SDavid Daney 	{ XFER_PIO_0,     70, 290, 240, 600, 165, 150, 0,  600,   0 },
31403ada9c12SDavid Daney 	{ XFER_PIO_1,     50, 290,  93, 383, 125, 100, 0,  383,   0 },
31413ada9c12SDavid Daney 	{ XFER_PIO_2,     30, 290,  40, 330, 100,  90, 0,  240,   0 },
31423ada9c12SDavid Daney 	{ XFER_PIO_3,     30,  80,  70, 180,  80,  70, 0,  180,   0 },
31433ada9c12SDavid Daney 	{ XFER_PIO_4,     25,  70,  25, 120,  70,  25, 0,  120,   0 },
31443ada9c12SDavid Daney 	{ XFER_PIO_5,     15,  65,  25, 100,  65,  25, 0,  100,   0 },
31453ada9c12SDavid Daney 	{ XFER_PIO_6,     10,  55,  20,  80,  55,  20, 0,   80,   0 },
3146c6fd2807SJeff Garzik 
31473ada9c12SDavid Daney 	{ XFER_SW_DMA_0, 120,   0,   0,   0, 480, 480, 50, 960,   0 },
31483ada9c12SDavid Daney 	{ XFER_SW_DMA_1,  90,   0,   0,   0, 240, 240, 30, 480,   0 },
31493ada9c12SDavid Daney 	{ XFER_SW_DMA_2,  60,   0,   0,   0, 120, 120, 20, 240,   0 },
3150c6fd2807SJeff Garzik 
31513ada9c12SDavid Daney 	{ XFER_MW_DMA_0,  60,   0,   0,   0, 215, 215, 20, 480,   0 },
31523ada9c12SDavid Daney 	{ XFER_MW_DMA_1,  45,   0,   0,   0,  80,  50, 5,  150,   0 },
31533ada9c12SDavid Daney 	{ XFER_MW_DMA_2,  25,   0,   0,   0,  70,  25, 5,  120,   0 },
31543ada9c12SDavid Daney 	{ XFER_MW_DMA_3,  25,   0,   0,   0,  65,  25, 5,  100,   0 },
31553ada9c12SDavid Daney 	{ XFER_MW_DMA_4,  25,   0,   0,   0,  55,  20, 5,   80,   0 },
3156c6fd2807SJeff Garzik 
31573ada9c12SDavid Daney /*	{ XFER_UDMA_SLOW,  0,   0,   0,   0,   0,   0, 0,    0, 150 }, */
31583ada9c12SDavid Daney 	{ XFER_UDMA_0,     0,   0,   0,   0,   0,   0, 0,    0, 120 },
31593ada9c12SDavid Daney 	{ XFER_UDMA_1,     0,   0,   0,   0,   0,   0, 0,    0,  80 },
31603ada9c12SDavid Daney 	{ XFER_UDMA_2,     0,   0,   0,   0,   0,   0, 0,    0,  60 },
31613ada9c12SDavid Daney 	{ XFER_UDMA_3,     0,   0,   0,   0,   0,   0, 0,    0,  45 },
31623ada9c12SDavid Daney 	{ XFER_UDMA_4,     0,   0,   0,   0,   0,   0, 0,    0,  30 },
31633ada9c12SDavid Daney 	{ XFER_UDMA_5,     0,   0,   0,   0,   0,   0, 0,    0,  20 },
31643ada9c12SDavid Daney 	{ XFER_UDMA_6,     0,   0,   0,   0,   0,   0, 0,    0,  15 },
3165c6fd2807SJeff Garzik 
3166c6fd2807SJeff Garzik 	{ 0xFF }
3167c6fd2807SJeff Garzik };
3168c6fd2807SJeff Garzik 
3169c6fd2807SJeff Garzik #define ENOUGH(v, unit)		(((v)-1)/(unit)+1)
3170c6fd2807SJeff Garzik #define EZ(v, unit)		((v)?ENOUGH(v, unit):0)
3171c6fd2807SJeff Garzik 
3172c6fd2807SJeff Garzik static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
3173c6fd2807SJeff Garzik {
3174c6fd2807SJeff Garzik 	q->setup	= EZ(t->setup      * 1000,  T);
3175c6fd2807SJeff Garzik 	q->act8b	= EZ(t->act8b      * 1000,  T);
3176c6fd2807SJeff Garzik 	q->rec8b	= EZ(t->rec8b      * 1000,  T);
3177c6fd2807SJeff Garzik 	q->cyc8b	= EZ(t->cyc8b      * 1000,  T);
3178c6fd2807SJeff Garzik 	q->active	= EZ(t->active     * 1000,  T);
3179c6fd2807SJeff Garzik 	q->recover	= EZ(t->recover    * 1000,  T);
31803ada9c12SDavid Daney 	q->dmack_hold	= EZ(t->dmack_hold * 1000,  T);
3181c6fd2807SJeff Garzik 	q->cycle	= EZ(t->cycle      * 1000,  T);
3182c6fd2807SJeff Garzik 	q->udma		= EZ(t->udma       * 1000, UT);
3183c6fd2807SJeff Garzik }
3184c6fd2807SJeff Garzik 
3185c6fd2807SJeff Garzik void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
3186c6fd2807SJeff Garzik 		      struct ata_timing *m, unsigned int what)
3187c6fd2807SJeff Garzik {
3188c6fd2807SJeff Garzik 	if (what & ATA_TIMING_SETUP  ) m->setup   = max(a->setup,   b->setup);
3189c6fd2807SJeff Garzik 	if (what & ATA_TIMING_ACT8B  ) m->act8b   = max(a->act8b,   b->act8b);
3190c6fd2807SJeff Garzik 	if (what & ATA_TIMING_REC8B  ) m->rec8b   = max(a->rec8b,   b->rec8b);
3191c6fd2807SJeff Garzik 	if (what & ATA_TIMING_CYC8B  ) m->cyc8b   = max(a->cyc8b,   b->cyc8b);
3192c6fd2807SJeff Garzik 	if (what & ATA_TIMING_ACTIVE ) m->active  = max(a->active,  b->active);
3193c6fd2807SJeff Garzik 	if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
31943ada9c12SDavid Daney 	if (what & ATA_TIMING_DMACK_HOLD) m->dmack_hold = max(a->dmack_hold, b->dmack_hold);
3195c6fd2807SJeff Garzik 	if (what & ATA_TIMING_CYCLE  ) m->cycle   = max(a->cycle,   b->cycle);
3196c6fd2807SJeff Garzik 	if (what & ATA_TIMING_UDMA   ) m->udma    = max(a->udma,    b->udma);
3197c6fd2807SJeff Garzik }
3198c6fd2807SJeff Garzik 
31996357357cSTejun Heo const struct ata_timing *ata_timing_find_mode(u8 xfer_mode)
3200c6fd2807SJeff Garzik {
320170cd071eSTejun Heo 	const struct ata_timing *t = ata_timing;
3202c6fd2807SJeff Garzik 
320370cd071eSTejun Heo 	while (xfer_mode > t->mode)
320470cd071eSTejun Heo 		t++;
320570cd071eSTejun Heo 
320670cd071eSTejun Heo 	if (xfer_mode == t->mode)
3207c6fd2807SJeff Garzik 		return t;
320870cd071eSTejun Heo 	return NULL;
3209c6fd2807SJeff Garzik }
3210c6fd2807SJeff Garzik 
3211c6fd2807SJeff Garzik int ata_timing_compute(struct ata_device *adev, unsigned short speed,
3212c6fd2807SJeff Garzik 		       struct ata_timing *t, int T, int UT)
3213c6fd2807SJeff Garzik {
32149e8808a9SBartlomiej Zolnierkiewicz 	const u16 *id = adev->id;
3215c6fd2807SJeff Garzik 	const struct ata_timing *s;
3216c6fd2807SJeff Garzik 	struct ata_timing p;
3217c6fd2807SJeff Garzik 
3218c6fd2807SJeff Garzik 	/*
3219c6fd2807SJeff Garzik 	 * Find the mode.
3220c6fd2807SJeff Garzik 	 */
3221c6fd2807SJeff Garzik 
3222c6fd2807SJeff Garzik 	if (!(s = ata_timing_find_mode(speed)))
3223c6fd2807SJeff Garzik 		return -EINVAL;
3224c6fd2807SJeff Garzik 
3225c6fd2807SJeff Garzik 	memcpy(t, s, sizeof(*s));
3226c6fd2807SJeff Garzik 
3227c6fd2807SJeff Garzik 	/*
3228c6fd2807SJeff Garzik 	 * If the drive is an EIDE drive, it can tell us it needs extended
3229c6fd2807SJeff Garzik 	 * PIO/MW_DMA cycle timing.
3230c6fd2807SJeff Garzik 	 */
3231c6fd2807SJeff Garzik 
32329e8808a9SBartlomiej Zolnierkiewicz 	if (id[ATA_ID_FIELD_VALID] & 2) {	/* EIDE drive */
3233c6fd2807SJeff Garzik 		memset(&p, 0, sizeof(p));
32349e8808a9SBartlomiej Zolnierkiewicz 
3235c6fd2807SJeff Garzik 		if (speed >= XFER_PIO_0 && speed <= XFER_SW_DMA_0) {
32369e8808a9SBartlomiej Zolnierkiewicz 			if (speed <= XFER_PIO_2)
32379e8808a9SBartlomiej Zolnierkiewicz 				p.cycle = p.cyc8b = id[ATA_ID_EIDE_PIO];
32389e8808a9SBartlomiej Zolnierkiewicz 			else if ((speed <= XFER_PIO_4) ||
32399e8808a9SBartlomiej Zolnierkiewicz 				 (speed == XFER_PIO_5 && !ata_id_is_cfa(id)))
32409e8808a9SBartlomiej Zolnierkiewicz 				p.cycle = p.cyc8b = id[ATA_ID_EIDE_PIO_IORDY];
32419e8808a9SBartlomiej Zolnierkiewicz 		} else if (speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2)
32429e8808a9SBartlomiej Zolnierkiewicz 			p.cycle = id[ATA_ID_EIDE_DMA_MIN];
32439e8808a9SBartlomiej Zolnierkiewicz 
3244c6fd2807SJeff Garzik 		ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
3245c6fd2807SJeff Garzik 	}
3246c6fd2807SJeff Garzik 
3247c6fd2807SJeff Garzik 	/*
3248c6fd2807SJeff Garzik 	 * Convert the timing to bus clock counts.
3249c6fd2807SJeff Garzik 	 */
3250c6fd2807SJeff Garzik 
3251c6fd2807SJeff Garzik 	ata_timing_quantize(t, t, T, UT);
3252c6fd2807SJeff Garzik 
3253c6fd2807SJeff Garzik 	/*
3254c6fd2807SJeff Garzik 	 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
3255c6fd2807SJeff Garzik 	 * S.M.A.R.T * and some other commands. We have to ensure that the
3256c6fd2807SJeff Garzik 	 * DMA cycle timing is slower/equal than the fastest PIO timing.
3257c6fd2807SJeff Garzik 	 */
3258c6fd2807SJeff Garzik 
3259fd3367afSAlan 	if (speed > XFER_PIO_6) {
3260c6fd2807SJeff Garzik 		ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
3261c6fd2807SJeff Garzik 		ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
3262c6fd2807SJeff Garzik 	}
3263c6fd2807SJeff Garzik 
3264c6fd2807SJeff Garzik 	/*
3265c6fd2807SJeff Garzik 	 * Lengthen active & recovery time so that cycle time is correct.
3266c6fd2807SJeff Garzik 	 */
3267c6fd2807SJeff Garzik 
3268c6fd2807SJeff Garzik 	if (t->act8b + t->rec8b < t->cyc8b) {
3269c6fd2807SJeff Garzik 		t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
3270c6fd2807SJeff Garzik 		t->rec8b = t->cyc8b - t->act8b;
3271c6fd2807SJeff Garzik 	}
3272c6fd2807SJeff Garzik 
3273c6fd2807SJeff Garzik 	if (t->active + t->recover < t->cycle) {
3274c6fd2807SJeff Garzik 		t->active += (t->cycle - (t->active + t->recover)) / 2;
3275c6fd2807SJeff Garzik 		t->recover = t->cycle - t->active;
3276c6fd2807SJeff Garzik 	}
32774f701d1eSAlan Cox 
32784f701d1eSAlan Cox 	/* In a few cases quantisation may produce enough errors to
32794f701d1eSAlan Cox 	   leave t->cycle too low for the sum of active and recovery
32804f701d1eSAlan Cox 	   if so we must correct this */
32814f701d1eSAlan Cox 	if (t->active + t->recover > t->cycle)
32824f701d1eSAlan Cox 		t->cycle = t->active + t->recover;
3283c6fd2807SJeff Garzik 
3284c6fd2807SJeff Garzik 	return 0;
3285c6fd2807SJeff Garzik }
3286c6fd2807SJeff Garzik 
3287c6fd2807SJeff Garzik /**
3288a0f79b92STejun Heo  *	ata_timing_cycle2mode - find xfer mode for the specified cycle duration
3289a0f79b92STejun Heo  *	@xfer_shift: ATA_SHIFT_* value for transfer type to examine.
3290a0f79b92STejun Heo  *	@cycle: cycle duration in ns
3291a0f79b92STejun Heo  *
3292a0f79b92STejun Heo  *	Return matching xfer mode for @cycle.  The returned mode is of
3293a0f79b92STejun Heo  *	the transfer type specified by @xfer_shift.  If @cycle is too
3294a0f79b92STejun Heo  *	slow for @xfer_shift, 0xff is returned.  If @cycle is faster
3295a0f79b92STejun Heo  *	than the fastest known mode, the fasted mode is returned.
3296a0f79b92STejun Heo  *
3297a0f79b92STejun Heo  *	LOCKING:
3298a0f79b92STejun Heo  *	None.
3299a0f79b92STejun Heo  *
3300a0f79b92STejun Heo  *	RETURNS:
3301a0f79b92STejun Heo  *	Matching xfer_mode, 0xff if no match found.
3302a0f79b92STejun Heo  */
3303a0f79b92STejun Heo u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
3304a0f79b92STejun Heo {
3305a0f79b92STejun Heo 	u8 base_mode = 0xff, last_mode = 0xff;
3306a0f79b92STejun Heo 	const struct ata_xfer_ent *ent;
3307a0f79b92STejun Heo 	const struct ata_timing *t;
3308a0f79b92STejun Heo 
3309a0f79b92STejun Heo 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
3310a0f79b92STejun Heo 		if (ent->shift == xfer_shift)
3311a0f79b92STejun Heo 			base_mode = ent->base;
3312a0f79b92STejun Heo 
3313a0f79b92STejun Heo 	for (t = ata_timing_find_mode(base_mode);
3314a0f79b92STejun Heo 	     t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
3315a0f79b92STejun Heo 		unsigned short this_cycle;
3316a0f79b92STejun Heo 
3317a0f79b92STejun Heo 		switch (xfer_shift) {
3318a0f79b92STejun Heo 		case ATA_SHIFT_PIO:
3319a0f79b92STejun Heo 		case ATA_SHIFT_MWDMA:
3320a0f79b92STejun Heo 			this_cycle = t->cycle;
3321a0f79b92STejun Heo 			break;
3322a0f79b92STejun Heo 		case ATA_SHIFT_UDMA:
3323a0f79b92STejun Heo 			this_cycle = t->udma;
3324a0f79b92STejun Heo 			break;
3325a0f79b92STejun Heo 		default:
3326a0f79b92STejun Heo 			return 0xff;
3327a0f79b92STejun Heo 		}
3328a0f79b92STejun Heo 
3329a0f79b92STejun Heo 		if (cycle > this_cycle)
3330a0f79b92STejun Heo 			break;
3331a0f79b92STejun Heo 
3332a0f79b92STejun Heo 		last_mode = t->mode;
3333a0f79b92STejun Heo 	}
3334a0f79b92STejun Heo 
3335a0f79b92STejun Heo 	return last_mode;
3336a0f79b92STejun Heo }
3337a0f79b92STejun Heo 
3338a0f79b92STejun Heo /**
3339c6fd2807SJeff Garzik  *	ata_down_xfermask_limit - adjust dev xfer masks downward
3340c6fd2807SJeff Garzik  *	@dev: Device to adjust xfer masks
3341458337dbSTejun Heo  *	@sel: ATA_DNXFER_* selector
3342c6fd2807SJeff Garzik  *
3343c6fd2807SJeff Garzik  *	Adjust xfer masks of @dev downward.  Note that this function
3344c6fd2807SJeff Garzik  *	does not apply the change.  Invoking ata_set_mode() afterwards
3345c6fd2807SJeff Garzik  *	will apply the limit.
3346c6fd2807SJeff Garzik  *
3347c6fd2807SJeff Garzik  *	LOCKING:
3348c6fd2807SJeff Garzik  *	Inherited from caller.
3349c6fd2807SJeff Garzik  *
3350c6fd2807SJeff Garzik  *	RETURNS:
3351c6fd2807SJeff Garzik  *	0 on success, negative errno on failure
3352c6fd2807SJeff Garzik  */
3353458337dbSTejun Heo int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
3354c6fd2807SJeff Garzik {
3355458337dbSTejun Heo 	char buf[32];
33567dc951aeSTejun Heo 	unsigned long orig_mask, xfer_mask;
33577dc951aeSTejun Heo 	unsigned long pio_mask, mwdma_mask, udma_mask;
3358458337dbSTejun Heo 	int quiet, highbit;
3359c6fd2807SJeff Garzik 
3360458337dbSTejun Heo 	quiet = !!(sel & ATA_DNXFER_QUIET);
3361458337dbSTejun Heo 	sel &= ~ATA_DNXFER_QUIET;
3362458337dbSTejun Heo 
3363458337dbSTejun Heo 	xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
3364458337dbSTejun Heo 						  dev->mwdma_mask,
3365c6fd2807SJeff Garzik 						  dev->udma_mask);
3366458337dbSTejun Heo 	ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
3367c6fd2807SJeff Garzik 
3368458337dbSTejun Heo 	switch (sel) {
3369458337dbSTejun Heo 	case ATA_DNXFER_PIO:
3370458337dbSTejun Heo 		highbit = fls(pio_mask) - 1;
3371458337dbSTejun Heo 		pio_mask &= ~(1 << highbit);
3372458337dbSTejun Heo 		break;
3373458337dbSTejun Heo 
3374458337dbSTejun Heo 	case ATA_DNXFER_DMA:
3375458337dbSTejun Heo 		if (udma_mask) {
3376458337dbSTejun Heo 			highbit = fls(udma_mask) - 1;
3377458337dbSTejun Heo 			udma_mask &= ~(1 << highbit);
3378458337dbSTejun Heo 			if (!udma_mask)
3379458337dbSTejun Heo 				return -ENOENT;
3380458337dbSTejun Heo 		} else if (mwdma_mask) {
3381458337dbSTejun Heo 			highbit = fls(mwdma_mask) - 1;
3382458337dbSTejun Heo 			mwdma_mask &= ~(1 << highbit);
3383458337dbSTejun Heo 			if (!mwdma_mask)
3384458337dbSTejun Heo 				return -ENOENT;
3385458337dbSTejun Heo 		}
3386458337dbSTejun Heo 		break;
3387458337dbSTejun Heo 
3388458337dbSTejun Heo 	case ATA_DNXFER_40C:
3389458337dbSTejun Heo 		udma_mask &= ATA_UDMA_MASK_40C;
3390458337dbSTejun Heo 		break;
3391458337dbSTejun Heo 
3392458337dbSTejun Heo 	case ATA_DNXFER_FORCE_PIO0:
3393458337dbSTejun Heo 		pio_mask &= 1;
3394458337dbSTejun Heo 	case ATA_DNXFER_FORCE_PIO:
3395458337dbSTejun Heo 		mwdma_mask = 0;
3396458337dbSTejun Heo 		udma_mask = 0;
3397458337dbSTejun Heo 		break;
3398458337dbSTejun Heo 
3399458337dbSTejun Heo 	default:
3400458337dbSTejun Heo 		BUG();
3401458337dbSTejun Heo 	}
3402458337dbSTejun Heo 
3403458337dbSTejun Heo 	xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
3404458337dbSTejun Heo 
3405458337dbSTejun Heo 	if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
3406458337dbSTejun Heo 		return -ENOENT;
3407458337dbSTejun Heo 
3408458337dbSTejun Heo 	if (!quiet) {
3409458337dbSTejun Heo 		if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
3410458337dbSTejun Heo 			snprintf(buf, sizeof(buf), "%s:%s",
3411458337dbSTejun Heo 				 ata_mode_string(xfer_mask),
3412458337dbSTejun Heo 				 ata_mode_string(xfer_mask & ATA_MASK_PIO));
3413458337dbSTejun Heo 		else
3414458337dbSTejun Heo 			snprintf(buf, sizeof(buf), "%s",
3415458337dbSTejun Heo 				 ata_mode_string(xfer_mask));
3416458337dbSTejun Heo 
3417458337dbSTejun Heo 		ata_dev_printk(dev, KERN_WARNING,
3418458337dbSTejun Heo 			       "limiting speed to %s\n", buf);
3419458337dbSTejun Heo 	}
3420c6fd2807SJeff Garzik 
3421c6fd2807SJeff Garzik 	ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
3422c6fd2807SJeff Garzik 			    &dev->udma_mask);
3423c6fd2807SJeff Garzik 
3424c6fd2807SJeff Garzik 	return 0;
3425c6fd2807SJeff Garzik }
3426c6fd2807SJeff Garzik 
3427c6fd2807SJeff Garzik static int ata_dev_set_mode(struct ata_device *dev)
3428c6fd2807SJeff Garzik {
3429d0cb43b3STejun Heo 	struct ata_port *ap = dev->link->ap;
34309af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
3431d0cb43b3STejun Heo 	const bool nosetxfer = dev->horkage & ATA_HORKAGE_NOSETXFER;
34324055dee7STejun Heo 	const char *dev_err_whine = "";
34334055dee7STejun Heo 	int ign_dev_err = 0;
3434d0cb43b3STejun Heo 	unsigned int err_mask = 0;
3435c6fd2807SJeff Garzik 	int rc;
3436c6fd2807SJeff Garzik 
3437c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_PIO;
3438c6fd2807SJeff Garzik 	if (dev->xfer_shift == ATA_SHIFT_PIO)
3439c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_PIO;
3440c6fd2807SJeff Garzik 
3441d0cb43b3STejun Heo 	if (nosetxfer && ap->flags & ATA_FLAG_SATA && ata_id_is_sata(dev->id))
3442d0cb43b3STejun Heo 		dev_err_whine = " (SET_XFERMODE skipped)";
3443d0cb43b3STejun Heo 	else {
3444d0cb43b3STejun Heo 		if (nosetxfer)
3445d0cb43b3STejun Heo 			ata_dev_printk(dev, KERN_WARNING,
3446d0cb43b3STejun Heo 				       "NOSETXFER but PATA detected - can't "
3447d0cb43b3STejun Heo 				       "skip SETXFER, might malfunction\n");
3448c6fd2807SJeff Garzik 		err_mask = ata_dev_set_xfermode(dev);
3449d0cb43b3STejun Heo 	}
34502dcb407eSJeff Garzik 
34514055dee7STejun Heo 	if (err_mask & ~AC_ERR_DEV)
34524055dee7STejun Heo 		goto fail;
34532dcb407eSJeff Garzik 
34544055dee7STejun Heo 	/* revalidate */
3455baa1e78aSTejun Heo 	ehc->i.flags |= ATA_EHI_POST_SETMODE;
3456422c9daaSTejun Heo 	rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
3457baa1e78aSTejun Heo 	ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
3458c6fd2807SJeff Garzik 	if (rc)
3459c6fd2807SJeff Garzik 		return rc;
3460c6fd2807SJeff Garzik 
3461b93fda12SAlan Cox 	if (dev->xfer_shift == ATA_SHIFT_PIO) {
34624055dee7STejun Heo 		/* Old CFA may refuse this command, which is just fine */
3463b93fda12SAlan Cox 		if (ata_id_is_cfa(dev->id))
34644055dee7STejun Heo 			ign_dev_err = 1;
3465b93fda12SAlan Cox 		/* Catch several broken garbage emulations plus some pre
3466b93fda12SAlan Cox 		   ATA devices */
3467b93fda12SAlan Cox 		if (ata_id_major_version(dev->id) == 0 &&
34684055dee7STejun Heo 					dev->pio_mode <= XFER_PIO_2)
34694055dee7STejun Heo 			ign_dev_err = 1;
3470b93fda12SAlan Cox 		/* Some very old devices and some bad newer ones fail
3471b93fda12SAlan Cox 		   any kind of SET_XFERMODE request but support PIO0-2
3472b93fda12SAlan Cox 		   timings and no IORDY */
3473b93fda12SAlan Cox 		if (!ata_id_has_iordy(dev->id) && dev->pio_mode <= XFER_PIO_2)
3474b93fda12SAlan Cox 			ign_dev_err = 1;
3475b93fda12SAlan Cox 	}
34764055dee7STejun Heo 	/* Early MWDMA devices do DMA but don't allow DMA mode setting.
34774055dee7STejun Heo 	   Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
34784055dee7STejun Heo 	if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
34794055dee7STejun Heo 	    dev->dma_mode == XFER_MW_DMA_0 &&
34804055dee7STejun Heo 	    (dev->id[63] >> 8) & 1)
34814055dee7STejun Heo 		ign_dev_err = 1;
34824055dee7STejun Heo 
34834055dee7STejun Heo 	/* if the device is actually configured correctly, ignore dev err */
34844055dee7STejun Heo 	if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id)))
34854055dee7STejun Heo 		ign_dev_err = 1;
34864055dee7STejun Heo 
34874055dee7STejun Heo 	if (err_mask & AC_ERR_DEV) {
34884055dee7STejun Heo 		if (!ign_dev_err)
34894055dee7STejun Heo 			goto fail;
34904055dee7STejun Heo 		else
34914055dee7STejun Heo 			dev_err_whine = " (device error ignored)";
34924055dee7STejun Heo 	}
34934055dee7STejun Heo 
3494c6fd2807SJeff Garzik 	DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
3495c6fd2807SJeff Garzik 		dev->xfer_shift, (int)dev->xfer_mode);
3496c6fd2807SJeff Garzik 
34974055dee7STejun Heo 	ata_dev_printk(dev, KERN_INFO, "configured for %s%s\n",
34984055dee7STejun Heo 		       ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)),
34994055dee7STejun Heo 		       dev_err_whine);
35004055dee7STejun Heo 
3501c6fd2807SJeff Garzik 	return 0;
35024055dee7STejun Heo 
35034055dee7STejun Heo  fail:
35044055dee7STejun Heo 	ata_dev_printk(dev, KERN_ERR, "failed to set xfermode "
35054055dee7STejun Heo 		       "(err_mask=0x%x)\n", err_mask);
35064055dee7STejun Heo 	return -EIO;
3507c6fd2807SJeff Garzik }
3508c6fd2807SJeff Garzik 
3509c6fd2807SJeff Garzik /**
351004351821SAlan  *	ata_do_set_mode - Program timings and issue SET FEATURES - XFER
35110260731fSTejun Heo  *	@link: link on which timings will be programmed
35121967b7ffSJoe Perches  *	@r_failed_dev: out parameter for failed device
3513c6fd2807SJeff Garzik  *
351404351821SAlan  *	Standard implementation of the function used to tune and set
351504351821SAlan  *	ATA device disk transfer mode (PIO3, UDMA6, etc.).  If
351604351821SAlan  *	ata_dev_set_mode() fails, pointer to the failing device is
3517c6fd2807SJeff Garzik  *	returned in @r_failed_dev.
3518c6fd2807SJeff Garzik  *
3519c6fd2807SJeff Garzik  *	LOCKING:
3520c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
3521c6fd2807SJeff Garzik  *
3522c6fd2807SJeff Garzik  *	RETURNS:
3523c6fd2807SJeff Garzik  *	0 on success, negative errno otherwise
3524c6fd2807SJeff Garzik  */
352504351821SAlan 
35260260731fSTejun Heo int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
3527c6fd2807SJeff Garzik {
35280260731fSTejun Heo 	struct ata_port *ap = link->ap;
3529c6fd2807SJeff Garzik 	struct ata_device *dev;
3530f58229f8STejun Heo 	int rc = 0, used_dma = 0, found = 0;
3531c6fd2807SJeff Garzik 
3532c6fd2807SJeff Garzik 	/* step 1: calculate xfer_mask */
35331eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
35347dc951aeSTejun Heo 		unsigned long pio_mask, dma_mask;
3535b3a70601SAlan Cox 		unsigned int mode_mask;
3536c6fd2807SJeff Garzik 
3537b3a70601SAlan Cox 		mode_mask = ATA_DMA_MASK_ATA;
3538b3a70601SAlan Cox 		if (dev->class == ATA_DEV_ATAPI)
3539b3a70601SAlan Cox 			mode_mask = ATA_DMA_MASK_ATAPI;
3540b3a70601SAlan Cox 		else if (ata_id_is_cfa(dev->id))
3541b3a70601SAlan Cox 			mode_mask = ATA_DMA_MASK_CFA;
3542b3a70601SAlan Cox 
3543c6fd2807SJeff Garzik 		ata_dev_xfermask(dev);
354433267325STejun Heo 		ata_force_xfermask(dev);
3545c6fd2807SJeff Garzik 
3546c6fd2807SJeff Garzik 		pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
3547c6fd2807SJeff Garzik 		dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
3548b3a70601SAlan Cox 
3549b3a70601SAlan Cox 		if (libata_dma_mask & mode_mask)
3550b3a70601SAlan Cox 			dma_mask = ata_pack_xfermask(0, dev->mwdma_mask, dev->udma_mask);
3551b3a70601SAlan Cox 		else
3552b3a70601SAlan Cox 			dma_mask = 0;
3553b3a70601SAlan Cox 
3554c6fd2807SJeff Garzik 		dev->pio_mode = ata_xfer_mask2mode(pio_mask);
3555c6fd2807SJeff Garzik 		dev->dma_mode = ata_xfer_mask2mode(dma_mask);
3556c6fd2807SJeff Garzik 
3557c6fd2807SJeff Garzik 		found = 1;
3558b15b3ebaSAlan Cox 		if (ata_dma_enabled(dev))
3559c6fd2807SJeff Garzik 			used_dma = 1;
3560c6fd2807SJeff Garzik 	}
3561c6fd2807SJeff Garzik 	if (!found)
3562c6fd2807SJeff Garzik 		goto out;
3563c6fd2807SJeff Garzik 
3564c6fd2807SJeff Garzik 	/* step 2: always set host PIO timings */
35651eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
356670cd071eSTejun Heo 		if (dev->pio_mode == 0xff) {
3567c6fd2807SJeff Garzik 			ata_dev_printk(dev, KERN_WARNING, "no PIO support\n");
3568c6fd2807SJeff Garzik 			rc = -EINVAL;
3569c6fd2807SJeff Garzik 			goto out;
3570c6fd2807SJeff Garzik 		}
3571c6fd2807SJeff Garzik 
3572c6fd2807SJeff Garzik 		dev->xfer_mode = dev->pio_mode;
3573c6fd2807SJeff Garzik 		dev->xfer_shift = ATA_SHIFT_PIO;
3574c6fd2807SJeff Garzik 		if (ap->ops->set_piomode)
3575c6fd2807SJeff Garzik 			ap->ops->set_piomode(ap, dev);
3576c6fd2807SJeff Garzik 	}
3577c6fd2807SJeff Garzik 
3578c6fd2807SJeff Garzik 	/* step 3: set host DMA timings */
35791eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
35801eca4365STejun Heo 		if (!ata_dma_enabled(dev))
3581c6fd2807SJeff Garzik 			continue;
3582c6fd2807SJeff Garzik 
3583c6fd2807SJeff Garzik 		dev->xfer_mode = dev->dma_mode;
3584c6fd2807SJeff Garzik 		dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
3585c6fd2807SJeff Garzik 		if (ap->ops->set_dmamode)
3586c6fd2807SJeff Garzik 			ap->ops->set_dmamode(ap, dev);
3587c6fd2807SJeff Garzik 	}
3588c6fd2807SJeff Garzik 
3589c6fd2807SJeff Garzik 	/* step 4: update devices' xfer mode */
35901eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
3591c6fd2807SJeff Garzik 		rc = ata_dev_set_mode(dev);
3592c6fd2807SJeff Garzik 		if (rc)
3593c6fd2807SJeff Garzik 			goto out;
3594c6fd2807SJeff Garzik 	}
3595c6fd2807SJeff Garzik 
3596c6fd2807SJeff Garzik 	/* Record simplex status. If we selected DMA then the other
3597c6fd2807SJeff Garzik 	 * host channels are not permitted to do so.
3598c6fd2807SJeff Garzik 	 */
3599cca3974eSJeff Garzik 	if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
3600032af1ceSAlan 		ap->host->simplex_claimed = ap;
3601c6fd2807SJeff Garzik 
3602c6fd2807SJeff Garzik  out:
3603c6fd2807SJeff Garzik 	if (rc)
3604c6fd2807SJeff Garzik 		*r_failed_dev = dev;
3605c6fd2807SJeff Garzik 	return rc;
3606c6fd2807SJeff Garzik }
3607c6fd2807SJeff Garzik 
3608c6fd2807SJeff Garzik /**
3609aa2731adSTejun Heo  *	ata_wait_ready - wait for link to become ready
3610aa2731adSTejun Heo  *	@link: link to be waited on
3611aa2731adSTejun Heo  *	@deadline: deadline jiffies for the operation
3612aa2731adSTejun Heo  *	@check_ready: callback to check link readiness
3613aa2731adSTejun Heo  *
3614aa2731adSTejun Heo  *	Wait for @link to become ready.  @check_ready should return
3615aa2731adSTejun Heo  *	positive number if @link is ready, 0 if it isn't, -ENODEV if
3616aa2731adSTejun Heo  *	link doesn't seem to be occupied, other errno for other error
3617aa2731adSTejun Heo  *	conditions.
3618aa2731adSTejun Heo  *
3619aa2731adSTejun Heo  *	Transient -ENODEV conditions are allowed for
3620aa2731adSTejun Heo  *	ATA_TMOUT_FF_WAIT.
3621aa2731adSTejun Heo  *
3622aa2731adSTejun Heo  *	LOCKING:
3623aa2731adSTejun Heo  *	EH context.
3624aa2731adSTejun Heo  *
3625aa2731adSTejun Heo  *	RETURNS:
3626aa2731adSTejun Heo  *	0 if @linke is ready before @deadline; otherwise, -errno.
3627aa2731adSTejun Heo  */
3628aa2731adSTejun Heo int ata_wait_ready(struct ata_link *link, unsigned long deadline,
3629aa2731adSTejun Heo 		   int (*check_ready)(struct ata_link *link))
3630aa2731adSTejun Heo {
3631aa2731adSTejun Heo 	unsigned long start = jiffies;
3632341c2c95STejun Heo 	unsigned long nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT);
3633aa2731adSTejun Heo 	int warned = 0;
3634aa2731adSTejun Heo 
3635b1c72916STejun Heo 	/* Slave readiness can't be tested separately from master.  On
3636b1c72916STejun Heo 	 * M/S emulation configuration, this function should be called
3637b1c72916STejun Heo 	 * only on the master and it will handle both master and slave.
3638b1c72916STejun Heo 	 */
3639b1c72916STejun Heo 	WARN_ON(link == link->ap->slave_link);
3640b1c72916STejun Heo 
3641aa2731adSTejun Heo 	if (time_after(nodev_deadline, deadline))
3642aa2731adSTejun Heo 		nodev_deadline = deadline;
3643aa2731adSTejun Heo 
3644aa2731adSTejun Heo 	while (1) {
3645aa2731adSTejun Heo 		unsigned long now = jiffies;
3646aa2731adSTejun Heo 		int ready, tmp;
3647aa2731adSTejun Heo 
3648aa2731adSTejun Heo 		ready = tmp = check_ready(link);
3649aa2731adSTejun Heo 		if (ready > 0)
3650aa2731adSTejun Heo 			return 0;
3651aa2731adSTejun Heo 
3652aa2731adSTejun Heo 		/* -ENODEV could be transient.  Ignore -ENODEV if link
3653aa2731adSTejun Heo 		 * is online.  Also, some SATA devices take a long
3654aa2731adSTejun Heo 		 * time to clear 0xff after reset.  For example,
3655aa2731adSTejun Heo 		 * HHD424020F7SV00 iVDR needs >= 800ms while Quantum
3656aa2731adSTejun Heo 		 * GoVault needs even more than that.  Wait for
3657aa2731adSTejun Heo 		 * ATA_TMOUT_FF_WAIT on -ENODEV if link isn't offline.
3658aa2731adSTejun Heo 		 *
3659aa2731adSTejun Heo 		 * Note that some PATA controllers (pata_ali) explode
3660aa2731adSTejun Heo 		 * if status register is read more than once when
3661aa2731adSTejun Heo 		 * there's no device attached.
3662aa2731adSTejun Heo 		 */
3663aa2731adSTejun Heo 		if (ready == -ENODEV) {
3664aa2731adSTejun Heo 			if (ata_link_online(link))
3665aa2731adSTejun Heo 				ready = 0;
3666aa2731adSTejun Heo 			else if ((link->ap->flags & ATA_FLAG_SATA) &&
3667aa2731adSTejun Heo 				 !ata_link_offline(link) &&
3668aa2731adSTejun Heo 				 time_before(now, nodev_deadline))
3669aa2731adSTejun Heo 				ready = 0;
3670aa2731adSTejun Heo 		}
3671aa2731adSTejun Heo 
3672aa2731adSTejun Heo 		if (ready)
3673aa2731adSTejun Heo 			return ready;
3674aa2731adSTejun Heo 		if (time_after(now, deadline))
3675aa2731adSTejun Heo 			return -EBUSY;
3676aa2731adSTejun Heo 
3677aa2731adSTejun Heo 		if (!warned && time_after(now, start + 5 * HZ) &&
3678aa2731adSTejun Heo 		    (deadline - now > 3 * HZ)) {
3679aa2731adSTejun Heo 			ata_link_printk(link, KERN_WARNING,
3680aa2731adSTejun Heo 				"link is slow to respond, please be patient "
3681aa2731adSTejun Heo 				"(ready=%d)\n", tmp);
3682aa2731adSTejun Heo 			warned = 1;
3683aa2731adSTejun Heo 		}
3684aa2731adSTejun Heo 
3685aa2731adSTejun Heo 		msleep(50);
3686aa2731adSTejun Heo 	}
3687aa2731adSTejun Heo }
3688aa2731adSTejun Heo 
3689aa2731adSTejun Heo /**
3690aa2731adSTejun Heo  *	ata_wait_after_reset - wait for link to become ready after reset
3691aa2731adSTejun Heo  *	@link: link to be waited on
3692aa2731adSTejun Heo  *	@deadline: deadline jiffies for the operation
3693aa2731adSTejun Heo  *	@check_ready: callback to check link readiness
3694aa2731adSTejun Heo  *
3695aa2731adSTejun Heo  *	Wait for @link to become ready after reset.
3696aa2731adSTejun Heo  *
3697aa2731adSTejun Heo  *	LOCKING:
3698aa2731adSTejun Heo  *	EH context.
3699aa2731adSTejun Heo  *
3700aa2731adSTejun Heo  *	RETURNS:
3701aa2731adSTejun Heo  *	0 if @linke is ready before @deadline; otherwise, -errno.
3702aa2731adSTejun Heo  */
37032b4221bbSHarvey Harrison int ata_wait_after_reset(struct ata_link *link, unsigned long deadline,
3704aa2731adSTejun Heo 				int (*check_ready)(struct ata_link *link))
3705aa2731adSTejun Heo {
3706341c2c95STejun Heo 	msleep(ATA_WAIT_AFTER_RESET);
3707aa2731adSTejun Heo 
3708aa2731adSTejun Heo 	return ata_wait_ready(link, deadline, check_ready);
3709aa2731adSTejun Heo }
3710aa2731adSTejun Heo 
3711aa2731adSTejun Heo /**
3712936fd732STejun Heo  *	sata_link_debounce - debounce SATA phy status
3713936fd732STejun Heo  *	@link: ATA link to debounce SATA phy status for
3714c6fd2807SJeff Garzik  *	@params: timing parameters { interval, duratinon, timeout } in msec
3715d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
3716c6fd2807SJeff Garzik  *
3717936fd732STejun Heo *	Make sure SStatus of @link reaches stable state, determined by
3718c6fd2807SJeff Garzik  *	holding the same value where DET is not 1 for @duration polled
3719c6fd2807SJeff Garzik  *	every @interval, before @timeout.  Timeout constraints the
3720d4b2bab4STejun Heo  *	beginning of the stable state.  Because DET gets stuck at 1 on
3721d4b2bab4STejun Heo  *	some controllers after hot unplugging, this functions waits
3722c6fd2807SJeff Garzik  *	until timeout then returns 0 if DET is stable at 1.
3723c6fd2807SJeff Garzik  *
3724d4b2bab4STejun Heo  *	@timeout is further limited by @deadline.  The sooner of the
3725d4b2bab4STejun Heo  *	two is used.
3726d4b2bab4STejun Heo  *
3727c6fd2807SJeff Garzik  *	LOCKING:
3728c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3729c6fd2807SJeff Garzik  *
3730c6fd2807SJeff Garzik  *	RETURNS:
3731c6fd2807SJeff Garzik  *	0 on success, -errno on failure.
3732c6fd2807SJeff Garzik  */
3733936fd732STejun Heo int sata_link_debounce(struct ata_link *link, const unsigned long *params,
3734d4b2bab4STejun Heo 		       unsigned long deadline)
3735c6fd2807SJeff Garzik {
3736341c2c95STejun Heo 	unsigned long interval = params[0];
3737341c2c95STejun Heo 	unsigned long duration = params[1];
3738d4b2bab4STejun Heo 	unsigned long last_jiffies, t;
3739c6fd2807SJeff Garzik 	u32 last, cur;
3740c6fd2807SJeff Garzik 	int rc;
3741c6fd2807SJeff Garzik 
3742341c2c95STejun Heo 	t = ata_deadline(jiffies, params[2]);
3743d4b2bab4STejun Heo 	if (time_before(t, deadline))
3744d4b2bab4STejun Heo 		deadline = t;
3745d4b2bab4STejun Heo 
3746936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3747c6fd2807SJeff Garzik 		return rc;
3748c6fd2807SJeff Garzik 	cur &= 0xf;
3749c6fd2807SJeff Garzik 
3750c6fd2807SJeff Garzik 	last = cur;
3751c6fd2807SJeff Garzik 	last_jiffies = jiffies;
3752c6fd2807SJeff Garzik 
3753c6fd2807SJeff Garzik 	while (1) {
3754341c2c95STejun Heo 		msleep(interval);
3755936fd732STejun Heo 		if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3756c6fd2807SJeff Garzik 			return rc;
3757c6fd2807SJeff Garzik 		cur &= 0xf;
3758c6fd2807SJeff Garzik 
3759c6fd2807SJeff Garzik 		/* DET stable? */
3760c6fd2807SJeff Garzik 		if (cur == last) {
3761d4b2bab4STejun Heo 			if (cur == 1 && time_before(jiffies, deadline))
3762c6fd2807SJeff Garzik 				continue;
3763341c2c95STejun Heo 			if (time_after(jiffies,
3764341c2c95STejun Heo 				       ata_deadline(last_jiffies, duration)))
3765c6fd2807SJeff Garzik 				return 0;
3766c6fd2807SJeff Garzik 			continue;
3767c6fd2807SJeff Garzik 		}
3768c6fd2807SJeff Garzik 
3769c6fd2807SJeff Garzik 		/* unstable, start over */
3770c6fd2807SJeff Garzik 		last = cur;
3771c6fd2807SJeff Garzik 		last_jiffies = jiffies;
3772c6fd2807SJeff Garzik 
3773f1545154STejun Heo 		/* Check deadline.  If debouncing failed, return
3774f1545154STejun Heo 		 * -EPIPE to tell upper layer to lower link speed.
3775f1545154STejun Heo 		 */
3776d4b2bab4STejun Heo 		if (time_after(jiffies, deadline))
3777f1545154STejun Heo 			return -EPIPE;
3778c6fd2807SJeff Garzik 	}
3779c6fd2807SJeff Garzik }
3780c6fd2807SJeff Garzik 
3781c6fd2807SJeff Garzik /**
3782936fd732STejun Heo  *	sata_link_resume - resume SATA link
3783936fd732STejun Heo  *	@link: ATA link to resume SATA
3784c6fd2807SJeff Garzik  *	@params: timing parameters { interval, duratinon, timeout } in msec
3785d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
3786c6fd2807SJeff Garzik  *
3787936fd732STejun Heo  *	Resume SATA phy @link and debounce it.
3788c6fd2807SJeff Garzik  *
3789c6fd2807SJeff Garzik  *	LOCKING:
3790c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3791c6fd2807SJeff Garzik  *
3792c6fd2807SJeff Garzik  *	RETURNS:
3793c6fd2807SJeff Garzik  *	0 on success, -errno on failure.
3794c6fd2807SJeff Garzik  */
3795936fd732STejun Heo int sata_link_resume(struct ata_link *link, const unsigned long *params,
3796d4b2bab4STejun Heo 		     unsigned long deadline)
3797c6fd2807SJeff Garzik {
37985040ab67STejun Heo 	int tries = ATA_LINK_RESUME_TRIES;
3799ac371987STejun Heo 	u32 scontrol, serror;
3800c6fd2807SJeff Garzik 	int rc;
3801c6fd2807SJeff Garzik 
3802936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3803c6fd2807SJeff Garzik 		return rc;
3804c6fd2807SJeff Garzik 
38055040ab67STejun Heo 	/*
38065040ab67STejun Heo 	 * Writes to SControl sometimes get ignored under certain
38075040ab67STejun Heo 	 * controllers (ata_piix SIDPR).  Make sure DET actually is
38085040ab67STejun Heo 	 * cleared.
38095040ab67STejun Heo 	 */
38105040ab67STejun Heo 	do {
3811c6fd2807SJeff Garzik 		scontrol = (scontrol & 0x0f0) | 0x300;
3812936fd732STejun Heo 		if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3813c6fd2807SJeff Garzik 			return rc;
38145040ab67STejun Heo 		/*
38155040ab67STejun Heo 		 * Some PHYs react badly if SStatus is pounded
38165040ab67STejun Heo 		 * immediately after resuming.  Delay 200ms before
38175040ab67STejun Heo 		 * debouncing.
3818c6fd2807SJeff Garzik 		 */
3819c6fd2807SJeff Garzik 		msleep(200);
3820c6fd2807SJeff Garzik 
38215040ab67STejun Heo 		/* is SControl restored correctly? */
38225040ab67STejun Heo 		if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
38235040ab67STejun Heo 			return rc;
38245040ab67STejun Heo 	} while ((scontrol & 0xf0f) != 0x300 && --tries);
38255040ab67STejun Heo 
38265040ab67STejun Heo 	if ((scontrol & 0xf0f) != 0x300) {
38275040ab67STejun Heo 		ata_link_printk(link, KERN_ERR,
38285040ab67STejun Heo 				"failed to resume link (SControl %X)\n",
38295040ab67STejun Heo 				scontrol);
38305040ab67STejun Heo 		return 0;
38315040ab67STejun Heo 	}
38325040ab67STejun Heo 
38335040ab67STejun Heo 	if (tries < ATA_LINK_RESUME_TRIES)
38345040ab67STejun Heo 		ata_link_printk(link, KERN_WARNING,
38355040ab67STejun Heo 				"link resume succeeded after %d retries\n",
38365040ab67STejun Heo 				ATA_LINK_RESUME_TRIES - tries);
38375040ab67STejun Heo 
3838ac371987STejun Heo 	if ((rc = sata_link_debounce(link, params, deadline)))
3839ac371987STejun Heo 		return rc;
3840ac371987STejun Heo 
3841f046519fSTejun Heo 	/* clear SError, some PHYs require this even for SRST to work */
3842ac371987STejun Heo 	if (!(rc = sata_scr_read(link, SCR_ERROR, &serror)))
3843ac371987STejun Heo 		rc = sata_scr_write(link, SCR_ERROR, serror);
3844ac371987STejun Heo 
3845f046519fSTejun Heo 	return rc != -EINVAL ? rc : 0;
3846c6fd2807SJeff Garzik }
3847c6fd2807SJeff Garzik 
3848c6fd2807SJeff Garzik /**
38490aa1113dSTejun Heo  *	ata_std_prereset - prepare for reset
3850cc0680a5STejun Heo  *	@link: ATA link to be reset
3851d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
3852c6fd2807SJeff Garzik  *
3853cc0680a5STejun Heo  *	@link is about to be reset.  Initialize it.  Failure from
3854b8cffc6aSTejun Heo  *	prereset makes libata abort whole reset sequence and give up
3855b8cffc6aSTejun Heo  *	that port, so prereset should be best-effort.  It does its
3856b8cffc6aSTejun Heo  *	best to prepare for reset sequence but if things go wrong, it
3857b8cffc6aSTejun Heo  *	should just whine, not fail.
3858c6fd2807SJeff Garzik  *
3859c6fd2807SJeff Garzik  *	LOCKING:
3860c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3861c6fd2807SJeff Garzik  *
3862c6fd2807SJeff Garzik  *	RETURNS:
3863c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
3864c6fd2807SJeff Garzik  */
38650aa1113dSTejun Heo int ata_std_prereset(struct ata_link *link, unsigned long deadline)
3866c6fd2807SJeff Garzik {
3867cc0680a5STejun Heo 	struct ata_port *ap = link->ap;
3868936fd732STejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
3869c6fd2807SJeff Garzik 	const unsigned long *timing = sata_ehc_deb_timing(ehc);
3870c6fd2807SJeff Garzik 	int rc;
3871c6fd2807SJeff Garzik 
3872c6fd2807SJeff Garzik 	/* if we're about to do hardreset, nothing more to do */
3873c6fd2807SJeff Garzik 	if (ehc->i.action & ATA_EH_HARDRESET)
3874c6fd2807SJeff Garzik 		return 0;
3875c6fd2807SJeff Garzik 
3876936fd732STejun Heo 	/* if SATA, resume link */
3877a16abc0bSTejun Heo 	if (ap->flags & ATA_FLAG_SATA) {
3878936fd732STejun Heo 		rc = sata_link_resume(link, timing, deadline);
3879b8cffc6aSTejun Heo 		/* whine about phy resume failure but proceed */
3880b8cffc6aSTejun Heo 		if (rc && rc != -EOPNOTSUPP)
3881cc0680a5STejun Heo 			ata_link_printk(link, KERN_WARNING, "failed to resume "
3882c6fd2807SJeff Garzik 					"link for reset (errno=%d)\n", rc);
3883c6fd2807SJeff Garzik 	}
3884c6fd2807SJeff Garzik 
388545db2f6cSTejun Heo 	/* no point in trying softreset on offline link */
3886b1c72916STejun Heo 	if (ata_phys_link_offline(link))
388745db2f6cSTejun Heo 		ehc->i.action &= ~ATA_EH_SOFTRESET;
388845db2f6cSTejun Heo 
3889c6fd2807SJeff Garzik 	return 0;
3890c6fd2807SJeff Garzik }
3891c6fd2807SJeff Garzik 
3892c6fd2807SJeff Garzik /**
3893cc0680a5STejun Heo  *	sata_link_hardreset - reset link via SATA phy reset
3894cc0680a5STejun Heo  *	@link: link to reset
3895b6103f6dSTejun Heo  *	@timing: timing parameters { interval, duratinon, timeout } in msec
3896d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
38979dadd45bSTejun Heo  *	@online: optional out parameter indicating link onlineness
38989dadd45bSTejun Heo  *	@check_ready: optional callback to check link readiness
3899c6fd2807SJeff Garzik  *
3900cc0680a5STejun Heo  *	SATA phy-reset @link using DET bits of SControl register.
39019dadd45bSTejun Heo  *	After hardreset, link readiness is waited upon using
39029dadd45bSTejun Heo  *	ata_wait_ready() if @check_ready is specified.  LLDs are
39039dadd45bSTejun Heo  *	allowed to not specify @check_ready and wait itself after this
39049dadd45bSTejun Heo  *	function returns.  Device classification is LLD's
39059dadd45bSTejun Heo  *	responsibility.
39069dadd45bSTejun Heo  *
39079dadd45bSTejun Heo  *	*@online is set to one iff reset succeeded and @link is online
39089dadd45bSTejun Heo  *	after reset.
3909c6fd2807SJeff Garzik  *
3910c6fd2807SJeff Garzik  *	LOCKING:
3911c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3912c6fd2807SJeff Garzik  *
3913c6fd2807SJeff Garzik  *	RETURNS:
3914c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
3915c6fd2807SJeff Garzik  */
3916cc0680a5STejun Heo int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
39179dadd45bSTejun Heo 			unsigned long deadline,
39189dadd45bSTejun Heo 			bool *online, int (*check_ready)(struct ata_link *))
3919c6fd2807SJeff Garzik {
3920c6fd2807SJeff Garzik 	u32 scontrol;
3921c6fd2807SJeff Garzik 	int rc;
3922c6fd2807SJeff Garzik 
3923c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
3924c6fd2807SJeff Garzik 
39259dadd45bSTejun Heo 	if (online)
39269dadd45bSTejun Heo 		*online = false;
39279dadd45bSTejun Heo 
3928936fd732STejun Heo 	if (sata_set_spd_needed(link)) {
3929c6fd2807SJeff Garzik 		/* SATA spec says nothing about how to reconfigure
3930c6fd2807SJeff Garzik 		 * spd.  To be on the safe side, turn off phy during
3931c6fd2807SJeff Garzik 		 * reconfiguration.  This works for at least ICH7 AHCI
3932c6fd2807SJeff Garzik 		 * and Sil3124.
3933c6fd2807SJeff Garzik 		 */
3934936fd732STejun Heo 		if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3935b6103f6dSTejun Heo 			goto out;
3936c6fd2807SJeff Garzik 
3937cea0d336SJeff Garzik 		scontrol = (scontrol & 0x0f0) | 0x304;
3938c6fd2807SJeff Garzik 
3939936fd732STejun Heo 		if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3940b6103f6dSTejun Heo 			goto out;
3941c6fd2807SJeff Garzik 
3942936fd732STejun Heo 		sata_set_spd(link);
3943c6fd2807SJeff Garzik 	}
3944c6fd2807SJeff Garzik 
3945c6fd2807SJeff Garzik 	/* issue phy wake/reset */
3946936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3947b6103f6dSTejun Heo 		goto out;
3948c6fd2807SJeff Garzik 
3949c6fd2807SJeff Garzik 	scontrol = (scontrol & 0x0f0) | 0x301;
3950c6fd2807SJeff Garzik 
3951936fd732STejun Heo 	if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
3952b6103f6dSTejun Heo 		goto out;
3953c6fd2807SJeff Garzik 
3954c6fd2807SJeff Garzik 	/* Couldn't find anything in SATA I/II specs, but AHCI-1.1
3955c6fd2807SJeff Garzik 	 * 10.4.2 says at least 1 ms.
3956c6fd2807SJeff Garzik 	 */
3957c6fd2807SJeff Garzik 	msleep(1);
3958c6fd2807SJeff Garzik 
3959936fd732STejun Heo 	/* bring link back */
3960936fd732STejun Heo 	rc = sata_link_resume(link, timing, deadline);
39619dadd45bSTejun Heo 	if (rc)
39629dadd45bSTejun Heo 		goto out;
39639dadd45bSTejun Heo 	/* if link is offline nothing more to do */
3964b1c72916STejun Heo 	if (ata_phys_link_offline(link))
39659dadd45bSTejun Heo 		goto out;
39669dadd45bSTejun Heo 
39679dadd45bSTejun Heo 	/* Link is online.  From this point, -ENODEV too is an error. */
39689dadd45bSTejun Heo 	if (online)
39699dadd45bSTejun Heo 		*online = true;
39709dadd45bSTejun Heo 
3971071f44b1STejun Heo 	if (sata_pmp_supported(link->ap) && ata_is_host_link(link)) {
39729dadd45bSTejun Heo 		/* If PMP is supported, we have to do follow-up SRST.
39739dadd45bSTejun Heo 		 * Some PMPs don't send D2H Reg FIS after hardreset if
39749dadd45bSTejun Heo 		 * the first port is empty.  Wait only for
39759dadd45bSTejun Heo 		 * ATA_TMOUT_PMP_SRST_WAIT.
39769dadd45bSTejun Heo 		 */
39779dadd45bSTejun Heo 		if (check_ready) {
39789dadd45bSTejun Heo 			unsigned long pmp_deadline;
39799dadd45bSTejun Heo 
3980341c2c95STejun Heo 			pmp_deadline = ata_deadline(jiffies,
3981341c2c95STejun Heo 						    ATA_TMOUT_PMP_SRST_WAIT);
39829dadd45bSTejun Heo 			if (time_after(pmp_deadline, deadline))
39839dadd45bSTejun Heo 				pmp_deadline = deadline;
39849dadd45bSTejun Heo 			ata_wait_ready(link, pmp_deadline, check_ready);
39859dadd45bSTejun Heo 		}
39869dadd45bSTejun Heo 		rc = -EAGAIN;
39879dadd45bSTejun Heo 		goto out;
39889dadd45bSTejun Heo 	}
39899dadd45bSTejun Heo 
39909dadd45bSTejun Heo 	rc = 0;
39919dadd45bSTejun Heo 	if (check_ready)
39929dadd45bSTejun Heo 		rc = ata_wait_ready(link, deadline, check_ready);
3993b6103f6dSTejun Heo  out:
39940cbf0711STejun Heo 	if (rc && rc != -EAGAIN) {
39950cbf0711STejun Heo 		/* online is set iff link is online && reset succeeded */
39960cbf0711STejun Heo 		if (online)
39970cbf0711STejun Heo 			*online = false;
39989dadd45bSTejun Heo 		ata_link_printk(link, KERN_ERR,
39999dadd45bSTejun Heo 				"COMRESET failed (errno=%d)\n", rc);
40000cbf0711STejun Heo 	}
4001b6103f6dSTejun Heo 	DPRINTK("EXIT, rc=%d\n", rc);
4002b6103f6dSTejun Heo 	return rc;
4003b6103f6dSTejun Heo }
4004b6103f6dSTejun Heo 
4005b6103f6dSTejun Heo /**
400657c9efdfSTejun Heo  *	sata_std_hardreset - COMRESET w/o waiting or classification
400757c9efdfSTejun Heo  *	@link: link to reset
400857c9efdfSTejun Heo  *	@class: resulting class of attached device
400957c9efdfSTejun Heo  *	@deadline: deadline jiffies for the operation
401057c9efdfSTejun Heo  *
401157c9efdfSTejun Heo  *	Standard SATA COMRESET w/o waiting or classification.
401257c9efdfSTejun Heo  *
401357c9efdfSTejun Heo  *	LOCKING:
401457c9efdfSTejun Heo  *	Kernel thread context (may sleep)
401557c9efdfSTejun Heo  *
401657c9efdfSTejun Heo  *	RETURNS:
401757c9efdfSTejun Heo  *	0 if link offline, -EAGAIN if link online, -errno on errors.
401857c9efdfSTejun Heo  */
401957c9efdfSTejun Heo int sata_std_hardreset(struct ata_link *link, unsigned int *class,
402057c9efdfSTejun Heo 		       unsigned long deadline)
402157c9efdfSTejun Heo {
402257c9efdfSTejun Heo 	const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
402357c9efdfSTejun Heo 	bool online;
402457c9efdfSTejun Heo 	int rc;
402557c9efdfSTejun Heo 
402657c9efdfSTejun Heo 	/* do hardreset */
402757c9efdfSTejun Heo 	rc = sata_link_hardreset(link, timing, deadline, &online, NULL);
402857c9efdfSTejun Heo 	return online ? -EAGAIN : rc;
402957c9efdfSTejun Heo }
403057c9efdfSTejun Heo 
403157c9efdfSTejun Heo /**
4032203c75b8STejun Heo  *	ata_std_postreset - standard postreset callback
4033cc0680a5STejun Heo  *	@link: the target ata_link
4034c6fd2807SJeff Garzik  *	@classes: classes of attached devices
4035c6fd2807SJeff Garzik  *
4036c6fd2807SJeff Garzik  *	This function is invoked after a successful reset.  Note that
4037c6fd2807SJeff Garzik  *	the device might have been reset more than once using
4038c6fd2807SJeff Garzik  *	different reset methods before postreset is invoked.
4039c6fd2807SJeff Garzik  *
4040c6fd2807SJeff Garzik  *	LOCKING:
4041c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
4042c6fd2807SJeff Garzik  */
4043203c75b8STejun Heo void ata_std_postreset(struct ata_link *link, unsigned int *classes)
4044c6fd2807SJeff Garzik {
4045f046519fSTejun Heo 	u32 serror;
4046f046519fSTejun Heo 
4047c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
4048c6fd2807SJeff Garzik 
4049f046519fSTejun Heo 	/* reset complete, clear SError */
4050f046519fSTejun Heo 	if (!sata_scr_read(link, SCR_ERROR, &serror))
4051f046519fSTejun Heo 		sata_scr_write(link, SCR_ERROR, serror);
4052f046519fSTejun Heo 
4053c6fd2807SJeff Garzik 	/* print link status */
4054936fd732STejun Heo 	sata_print_link_status(link);
4055c6fd2807SJeff Garzik 
4056c6fd2807SJeff Garzik 	DPRINTK("EXIT\n");
4057c6fd2807SJeff Garzik }
4058c6fd2807SJeff Garzik 
4059c6fd2807SJeff Garzik /**
4060c6fd2807SJeff Garzik  *	ata_dev_same_device - Determine whether new ID matches configured device
4061c6fd2807SJeff Garzik  *	@dev: device to compare against
4062c6fd2807SJeff Garzik  *	@new_class: class of the new device
4063c6fd2807SJeff Garzik  *	@new_id: IDENTIFY page of the new device
4064c6fd2807SJeff Garzik  *
4065c6fd2807SJeff Garzik  *	Compare @new_class and @new_id against @dev and determine
4066c6fd2807SJeff Garzik  *	whether @dev is the device indicated by @new_class and
4067c6fd2807SJeff Garzik  *	@new_id.
4068c6fd2807SJeff Garzik  *
4069c6fd2807SJeff Garzik  *	LOCKING:
4070c6fd2807SJeff Garzik  *	None.
4071c6fd2807SJeff Garzik  *
4072c6fd2807SJeff Garzik  *	RETURNS:
4073c6fd2807SJeff Garzik  *	1 if @dev matches @new_class and @new_id, 0 otherwise.
4074c6fd2807SJeff Garzik  */
4075c6fd2807SJeff Garzik static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
4076c6fd2807SJeff Garzik 			       const u16 *new_id)
4077c6fd2807SJeff Garzik {
4078c6fd2807SJeff Garzik 	const u16 *old_id = dev->id;
4079a0cf733bSTejun Heo 	unsigned char model[2][ATA_ID_PROD_LEN + 1];
4080a0cf733bSTejun Heo 	unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
4081c6fd2807SJeff Garzik 
4082c6fd2807SJeff Garzik 	if (dev->class != new_class) {
4083c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_INFO, "class mismatch %d != %d\n",
4084c6fd2807SJeff Garzik 			       dev->class, new_class);
4085c6fd2807SJeff Garzik 		return 0;
4086c6fd2807SJeff Garzik 	}
4087c6fd2807SJeff Garzik 
4088a0cf733bSTejun Heo 	ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
4089a0cf733bSTejun Heo 	ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
4090a0cf733bSTejun Heo 	ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
4091a0cf733bSTejun Heo 	ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
4092c6fd2807SJeff Garzik 
4093c6fd2807SJeff Garzik 	if (strcmp(model[0], model[1])) {
4094c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_INFO, "model number mismatch "
4095c6fd2807SJeff Garzik 			       "'%s' != '%s'\n", model[0], model[1]);
4096c6fd2807SJeff Garzik 		return 0;
4097c6fd2807SJeff Garzik 	}
4098c6fd2807SJeff Garzik 
4099c6fd2807SJeff Garzik 	if (strcmp(serial[0], serial[1])) {
4100c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_INFO, "serial number mismatch "
4101c6fd2807SJeff Garzik 			       "'%s' != '%s'\n", serial[0], serial[1]);
4102c6fd2807SJeff Garzik 		return 0;
4103c6fd2807SJeff Garzik 	}
4104c6fd2807SJeff Garzik 
4105c6fd2807SJeff Garzik 	return 1;
4106c6fd2807SJeff Garzik }
4107c6fd2807SJeff Garzik 
4108c6fd2807SJeff Garzik /**
4109fe30911bSTejun Heo  *	ata_dev_reread_id - Re-read IDENTIFY data
41103fae450cSHenrik Kretzschmar  *	@dev: target ATA device
4111bff04647STejun Heo  *	@readid_flags: read ID flags
4112c6fd2807SJeff Garzik  *
4113c6fd2807SJeff Garzik  *	Re-read IDENTIFY page and make sure @dev is still attached to
4114c6fd2807SJeff Garzik  *	the port.
4115c6fd2807SJeff Garzik  *
4116c6fd2807SJeff Garzik  *	LOCKING:
4117c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
4118c6fd2807SJeff Garzik  *
4119c6fd2807SJeff Garzik  *	RETURNS:
4120c6fd2807SJeff Garzik  *	0 on success, negative errno otherwise
4121c6fd2807SJeff Garzik  */
4122fe30911bSTejun Heo int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
4123c6fd2807SJeff Garzik {
4124c6fd2807SJeff Garzik 	unsigned int class = dev->class;
41259af5c9c9STejun Heo 	u16 *id = (void *)dev->link->ap->sector_buf;
4126c6fd2807SJeff Garzik 	int rc;
4127c6fd2807SJeff Garzik 
4128c6fd2807SJeff Garzik 	/* read ID data */
4129bff04647STejun Heo 	rc = ata_dev_read_id(dev, &class, readid_flags, id);
4130c6fd2807SJeff Garzik 	if (rc)
4131fe30911bSTejun Heo 		return rc;
4132c6fd2807SJeff Garzik 
4133c6fd2807SJeff Garzik 	/* is the device still there? */
4134fe30911bSTejun Heo 	if (!ata_dev_same_device(dev, class, id))
4135fe30911bSTejun Heo 		return -ENODEV;
4136c6fd2807SJeff Garzik 
4137c6fd2807SJeff Garzik 	memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
4138fe30911bSTejun Heo 	return 0;
4139fe30911bSTejun Heo }
4140fe30911bSTejun Heo 
4141fe30911bSTejun Heo /**
4142fe30911bSTejun Heo  *	ata_dev_revalidate - Revalidate ATA device
4143fe30911bSTejun Heo  *	@dev: device to revalidate
4144422c9daaSTejun Heo  *	@new_class: new class code
4145fe30911bSTejun Heo  *	@readid_flags: read ID flags
4146fe30911bSTejun Heo  *
4147fe30911bSTejun Heo  *	Re-read IDENTIFY page, make sure @dev is still attached to the
4148fe30911bSTejun Heo  *	port and reconfigure it according to the new IDENTIFY page.
4149fe30911bSTejun Heo  *
4150fe30911bSTejun Heo  *	LOCKING:
4151fe30911bSTejun Heo  *	Kernel thread context (may sleep)
4152fe30911bSTejun Heo  *
4153fe30911bSTejun Heo  *	RETURNS:
4154fe30911bSTejun Heo  *	0 on success, negative errno otherwise
4155fe30911bSTejun Heo  */
4156422c9daaSTejun Heo int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
4157422c9daaSTejun Heo 		       unsigned int readid_flags)
4158fe30911bSTejun Heo {
41596ddcd3b0STejun Heo 	u64 n_sectors = dev->n_sectors;
41605920dadfSTejun Heo 	u64 n_native_sectors = dev->n_native_sectors;
4161fe30911bSTejun Heo 	int rc;
4162fe30911bSTejun Heo 
4163fe30911bSTejun Heo 	if (!ata_dev_enabled(dev))
4164fe30911bSTejun Heo 		return -ENODEV;
4165fe30911bSTejun Heo 
4166422c9daaSTejun Heo 	/* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
4167422c9daaSTejun Heo 	if (ata_class_enabled(new_class) &&
4168f0d0613dSBorislav Petkov 	    new_class != ATA_DEV_ATA &&
4169f0d0613dSBorislav Petkov 	    new_class != ATA_DEV_ATAPI &&
4170f0d0613dSBorislav Petkov 	    new_class != ATA_DEV_SEMB) {
4171422c9daaSTejun Heo 		ata_dev_printk(dev, KERN_INFO, "class mismatch %u != %u\n",
4172422c9daaSTejun Heo 			       dev->class, new_class);
4173422c9daaSTejun Heo 		rc = -ENODEV;
4174422c9daaSTejun Heo 		goto fail;
4175422c9daaSTejun Heo 	}
4176422c9daaSTejun Heo 
4177fe30911bSTejun Heo 	/* re-read ID */
4178fe30911bSTejun Heo 	rc = ata_dev_reread_id(dev, readid_flags);
4179fe30911bSTejun Heo 	if (rc)
4180fe30911bSTejun Heo 		goto fail;
4181c6fd2807SJeff Garzik 
4182c6fd2807SJeff Garzik 	/* configure device according to the new ID */
4183efdaedc4STejun Heo 	rc = ata_dev_configure(dev);
41846ddcd3b0STejun Heo 	if (rc)
41856ddcd3b0STejun Heo 		goto fail;
41866ddcd3b0STejun Heo 
41876ddcd3b0STejun Heo 	/* verify n_sectors hasn't changed */
4188b54eebd6STejun Heo 	if (dev->class == ATA_DEV_ATA && n_sectors &&
4189b54eebd6STejun Heo 	    dev->n_sectors != n_sectors) {
41905920dadfSTejun Heo 		ata_dev_printk(dev, KERN_WARNING, "n_sectors mismatch "
41916ddcd3b0STejun Heo 			       "%llu != %llu\n",
41926ddcd3b0STejun Heo 			       (unsigned long long)n_sectors,
41936ddcd3b0STejun Heo 			       (unsigned long long)dev->n_sectors);
41945920dadfSTejun Heo 		/*
41955920dadfSTejun Heo 		 * Something could have caused HPA to be unlocked
41965920dadfSTejun Heo 		 * involuntarily.  If n_native_sectors hasn't changed
41975920dadfSTejun Heo 		 * and the new size matches it, keep the device.
41985920dadfSTejun Heo 		 */
41995920dadfSTejun Heo 		if (dev->n_native_sectors == n_native_sectors &&
42005920dadfSTejun Heo 		    dev->n_sectors > n_sectors &&
42015920dadfSTejun Heo 		    dev->n_sectors == n_native_sectors) {
42025920dadfSTejun Heo 			ata_dev_printk(dev, KERN_WARNING,
42035920dadfSTejun Heo 				       "new n_sectors matches native, probably "
42045920dadfSTejun Heo 				       "late HPA unlock, continuing\n");
42055920dadfSTejun Heo 			/* keep using the old n_sectors */
42068270bec4STejun Heo 			dev->n_sectors = n_sectors;
42075920dadfSTejun Heo 		} else {
42085920dadfSTejun Heo 			/* restore original n_[native]_sectors and fail */
42095920dadfSTejun Heo 			dev->n_native_sectors = n_native_sectors;
42105920dadfSTejun Heo 			dev->n_sectors = n_sectors;
42116ddcd3b0STejun Heo 			rc = -ENODEV;
42126ddcd3b0STejun Heo 			goto fail;
42136ddcd3b0STejun Heo 		}
42145920dadfSTejun Heo 	}
42156ddcd3b0STejun Heo 
4216c6fd2807SJeff Garzik 	return 0;
4217c6fd2807SJeff Garzik 
4218c6fd2807SJeff Garzik  fail:
4219c6fd2807SJeff Garzik 	ata_dev_printk(dev, KERN_ERR, "revalidation failed (errno=%d)\n", rc);
4220c6fd2807SJeff Garzik 	return rc;
4221c6fd2807SJeff Garzik }
4222c6fd2807SJeff Garzik 
42236919a0a6SAlan Cox struct ata_blacklist_entry {
42246919a0a6SAlan Cox 	const char *model_num;
42256919a0a6SAlan Cox 	const char *model_rev;
42266919a0a6SAlan Cox 	unsigned long horkage;
42276919a0a6SAlan Cox };
42286919a0a6SAlan Cox 
42296919a0a6SAlan Cox static const struct ata_blacklist_entry ata_device_blacklist [] = {
42306919a0a6SAlan Cox 	/* Devices with DMA related problems under Linux */
42316919a0a6SAlan Cox 	{ "WDC AC11000H",	NULL,		ATA_HORKAGE_NODMA },
42326919a0a6SAlan Cox 	{ "WDC AC22100H",	NULL,		ATA_HORKAGE_NODMA },
42336919a0a6SAlan Cox 	{ "WDC AC32500H",	NULL,		ATA_HORKAGE_NODMA },
42346919a0a6SAlan Cox 	{ "WDC AC33100H",	NULL,		ATA_HORKAGE_NODMA },
42356919a0a6SAlan Cox 	{ "WDC AC31600H",	NULL,		ATA_HORKAGE_NODMA },
42366919a0a6SAlan Cox 	{ "WDC AC32100H",	"24.09P07",	ATA_HORKAGE_NODMA },
42376919a0a6SAlan Cox 	{ "WDC AC23200L",	"21.10N21",	ATA_HORKAGE_NODMA },
42386919a0a6SAlan Cox 	{ "Compaq CRD-8241B", 	NULL,		ATA_HORKAGE_NODMA },
42396919a0a6SAlan Cox 	{ "CRD-8400B",		NULL, 		ATA_HORKAGE_NODMA },
42406919a0a6SAlan Cox 	{ "CRD-8480B",		NULL,		ATA_HORKAGE_NODMA },
42416919a0a6SAlan Cox 	{ "CRD-8482B",		NULL,		ATA_HORKAGE_NODMA },
42426919a0a6SAlan Cox 	{ "CRD-84",		NULL,		ATA_HORKAGE_NODMA },
42436919a0a6SAlan Cox 	{ "SanDisk SDP3B",	NULL,		ATA_HORKAGE_NODMA },
42446919a0a6SAlan Cox 	{ "SanDisk SDP3B-64",	NULL,		ATA_HORKAGE_NODMA },
42456919a0a6SAlan Cox 	{ "SANYO CD-ROM CRD",	NULL,		ATA_HORKAGE_NODMA },
42466919a0a6SAlan Cox 	{ "HITACHI CDR-8",	NULL,		ATA_HORKAGE_NODMA },
42476919a0a6SAlan Cox 	{ "HITACHI CDR-8335",	NULL,		ATA_HORKAGE_NODMA },
42486919a0a6SAlan Cox 	{ "HITACHI CDR-8435",	NULL,		ATA_HORKAGE_NODMA },
42496919a0a6SAlan Cox 	{ "Toshiba CD-ROM XM-6202B", NULL,	ATA_HORKAGE_NODMA },
42506919a0a6SAlan Cox 	{ "TOSHIBA CD-ROM XM-1702BC", NULL,	ATA_HORKAGE_NODMA },
42516919a0a6SAlan Cox 	{ "CD-532E-A", 		NULL,		ATA_HORKAGE_NODMA },
42526919a0a6SAlan Cox 	{ "E-IDE CD-ROM CR-840",NULL,		ATA_HORKAGE_NODMA },
42536919a0a6SAlan Cox 	{ "CD-ROM Drive/F5A",	NULL,		ATA_HORKAGE_NODMA },
42546919a0a6SAlan Cox 	{ "WPI CDD-820", 	NULL,		ATA_HORKAGE_NODMA },
42556919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SC-148C", NULL,	ATA_HORKAGE_NODMA },
42566919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SC",	NULL,		ATA_HORKAGE_NODMA },
42576919a0a6SAlan Cox 	{ "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
42586919a0a6SAlan Cox 	{ "_NEC DV5800A", 	NULL,		ATA_HORKAGE_NODMA },
42596919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SN-124", "N001",	ATA_HORKAGE_NODMA },
426039f19886SDave Jones 	{ "Seagate STT20000A", NULL,		ATA_HORKAGE_NODMA },
42613af9a77aSTejun Heo 	/* Odd clown on sil3726/4726 PMPs */
426250af2fa1STejun Heo 	{ "Config  Disk",	NULL,		ATA_HORKAGE_DISABLE },
42636919a0a6SAlan Cox 
426418d6e9d5SAlbert Lee 	/* Weird ATAPI devices */
426540a1d531STejun Heo 	{ "TORiSAN DVD-ROM DRD-N216", NULL,	ATA_HORKAGE_MAX_SEC_128 },
42666a87e42eSTejun Heo 	{ "QUANTUM DAT    DAT72-000", NULL,	ATA_HORKAGE_ATAPI_MOD16_DMA },
426718d6e9d5SAlbert Lee 
42686919a0a6SAlan Cox 	/* Devices we expect to fail diagnostics */
42696919a0a6SAlan Cox 
42706919a0a6SAlan Cox 	/* Devices where NCQ should be avoided */
42716919a0a6SAlan Cox 	/* NCQ is slow */
42726919a0a6SAlan Cox 	{ "WDC WD740ADFD-00",	NULL,		ATA_HORKAGE_NONCQ },
4273459ad688STejun Heo 	{ "WDC WD740ADFD-00NLR1", NULL,		ATA_HORKAGE_NONCQ, },
427409125ea6STejun Heo 	/* http://thread.gmane.org/gmane.linux.ide/14907 */
427509125ea6STejun Heo 	{ "FUJITSU MHT2060BH",	NULL,		ATA_HORKAGE_NONCQ },
42767acfaf30SPaul Rolland 	/* NCQ is broken */
4277539cc7c7SJeff Garzik 	{ "Maxtor *",		"BANC*",	ATA_HORKAGE_NONCQ },
42780e3dbc01SAlan Cox 	{ "Maxtor 7V300F0",	"VA111630",	ATA_HORKAGE_NONCQ },
4279da6f0ec2SPaolo Ornati 	{ "ST380817AS",		"3.42",		ATA_HORKAGE_NONCQ },
4280e41bd3e8STejun Heo 	{ "ST3160023AS",	"3.42",		ATA_HORKAGE_NONCQ },
42815ccfca97SLubomir Bulej 	{ "OCZ CORE_SSD",	"02.10104",	ATA_HORKAGE_NONCQ },
4282539cc7c7SJeff Garzik 
4283ac70a964STejun Heo 	/* Seagate NCQ + FLUSH CACHE firmware bug */
4284d10d491fSTejun Heo 	{ "ST31500341AS",	"SD15",		ATA_HORKAGE_NONCQ |
4285ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4286d10d491fSTejun Heo 	{ "ST31500341AS",	"SD16",		ATA_HORKAGE_NONCQ |
4287ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4288d10d491fSTejun Heo 	{ "ST31500341AS",	"SD17",		ATA_HORKAGE_NONCQ |
4289ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4290d10d491fSTejun Heo 	{ "ST31500341AS",	"SD18",		ATA_HORKAGE_NONCQ |
4291ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4292d10d491fSTejun Heo 	{ "ST31500341AS",	"SD19",		ATA_HORKAGE_NONCQ |
4293ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4294d10d491fSTejun Heo 
4295d10d491fSTejun Heo 	{ "ST31000333AS",	"SD15",		ATA_HORKAGE_NONCQ |
4296d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4297d10d491fSTejun Heo 	{ "ST31000333AS",	"SD16",		ATA_HORKAGE_NONCQ |
4298d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4299d10d491fSTejun Heo 	{ "ST31000333AS",	"SD17",		ATA_HORKAGE_NONCQ |
4300d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4301d10d491fSTejun Heo 	{ "ST31000333AS",	"SD18",		ATA_HORKAGE_NONCQ |
4302d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4303d10d491fSTejun Heo 	{ "ST31000333AS",	"SD19",		ATA_HORKAGE_NONCQ |
4304d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4305d10d491fSTejun Heo 
4306d10d491fSTejun Heo 	{ "ST3640623AS",	"SD15",		ATA_HORKAGE_NONCQ |
4307d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4308d10d491fSTejun Heo 	{ "ST3640623AS",	"SD16",		ATA_HORKAGE_NONCQ |
4309d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4310d10d491fSTejun Heo 	{ "ST3640623AS",	"SD17",		ATA_HORKAGE_NONCQ |
4311d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4312d10d491fSTejun Heo 	{ "ST3640623AS",	"SD18",		ATA_HORKAGE_NONCQ |
4313d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4314d10d491fSTejun Heo 	{ "ST3640623AS",	"SD19",		ATA_HORKAGE_NONCQ |
4315d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4316d10d491fSTejun Heo 
4317d10d491fSTejun Heo 	{ "ST3640323AS",	"SD15",		ATA_HORKAGE_NONCQ |
4318d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4319d10d491fSTejun Heo 	{ "ST3640323AS",	"SD16",		ATA_HORKAGE_NONCQ |
4320d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4321d10d491fSTejun Heo 	{ "ST3640323AS",	"SD17",		ATA_HORKAGE_NONCQ |
4322d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4323d10d491fSTejun Heo 	{ "ST3640323AS",	"SD18",		ATA_HORKAGE_NONCQ |
4324d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4325d10d491fSTejun Heo 	{ "ST3640323AS",	"SD19",		ATA_HORKAGE_NONCQ |
4326d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4327d10d491fSTejun Heo 
4328d10d491fSTejun Heo 	{ "ST3320813AS",	"SD15",		ATA_HORKAGE_NONCQ |
4329d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4330d10d491fSTejun Heo 	{ "ST3320813AS",	"SD16",		ATA_HORKAGE_NONCQ |
4331d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4332d10d491fSTejun Heo 	{ "ST3320813AS",	"SD17",		ATA_HORKAGE_NONCQ |
4333d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4334d10d491fSTejun Heo 	{ "ST3320813AS",	"SD18",		ATA_HORKAGE_NONCQ |
4335d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4336d10d491fSTejun Heo 	{ "ST3320813AS",	"SD19",		ATA_HORKAGE_NONCQ |
4337d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4338d10d491fSTejun Heo 
4339d10d491fSTejun Heo 	{ "ST3320613AS",	"SD15",		ATA_HORKAGE_NONCQ |
4340d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4341d10d491fSTejun Heo 	{ "ST3320613AS",	"SD16",		ATA_HORKAGE_NONCQ |
4342d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4343d10d491fSTejun Heo 	{ "ST3320613AS",	"SD17",		ATA_HORKAGE_NONCQ |
4344d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4345d10d491fSTejun Heo 	{ "ST3320613AS",	"SD18",		ATA_HORKAGE_NONCQ |
4346d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4347d10d491fSTejun Heo 	{ "ST3320613AS",	"SD19",		ATA_HORKAGE_NONCQ |
4348ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4349ac70a964STejun Heo 
435036e337d0SRobert Hancock 	/* Blacklist entries taken from Silicon Image 3124/3132
435136e337d0SRobert Hancock 	   Windows driver .inf file - also several Linux problem reports */
435236e337d0SRobert Hancock 	{ "HTS541060G9SA00",    "MB3OC60D",     ATA_HORKAGE_NONCQ, },
435336e337d0SRobert Hancock 	{ "HTS541080G9SA00",    "MB4OC60D",     ATA_HORKAGE_NONCQ, },
435436e337d0SRobert Hancock 	{ "HTS541010G9SA00",    "MBZOC60D",     ATA_HORKAGE_NONCQ, },
43556919a0a6SAlan Cox 
435616c55b03STejun Heo 	/* devices which puke on READ_NATIVE_MAX */
435716c55b03STejun Heo 	{ "HDS724040KLSA80",	"KFAOA20N",	ATA_HORKAGE_BROKEN_HPA, },
435816c55b03STejun Heo 	{ "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
435916c55b03STejun Heo 	{ "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
436016c55b03STejun Heo 	{ "MAXTOR 6L080L4",	"A93.0500",	ATA_HORKAGE_BROKEN_HPA },
43616919a0a6SAlan Cox 
43627831387bSTejun Heo 	/* this one allows HPA unlocking but fails IOs on the area */
43637831387bSTejun Heo 	{ "OCZ-VERTEX",		    "1.30",	ATA_HORKAGE_BROKEN_HPA },
43647831387bSTejun Heo 
436593328e11SAlan Cox 	/* Devices which report 1 sector over size HPA */
436693328e11SAlan Cox 	{ "ST340823A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
436793328e11SAlan Cox 	{ "ST320413A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
4368b152fcd3SMikko Rapeli 	{ "ST310211A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
436993328e11SAlan Cox 
43706bbfd53dSAlan Cox 	/* Devices which get the IVB wrong */
43716bbfd53dSAlan Cox 	{ "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB, },
4372a79067e5SAlan Cox 	/* Maybe we should just blacklist TSSTcorp... */
4373a79067e5SAlan Cox 	{ "TSSTcorp CDDVDW SH-S202H", "SB00",	  ATA_HORKAGE_IVB, },
4374a79067e5SAlan Cox 	{ "TSSTcorp CDDVDW SH-S202H", "SB01",	  ATA_HORKAGE_IVB, },
43756bbfd53dSAlan Cox 	{ "TSSTcorp CDDVDW SH-S202J", "SB00",	  ATA_HORKAGE_IVB, },
4376e9f33406SPeter Missel 	{ "TSSTcorp CDDVDW SH-S202J", "SB01",	  ATA_HORKAGE_IVB, },
4377e9f33406SPeter Missel 	{ "TSSTcorp CDDVDW SH-S202N", "SB00",	  ATA_HORKAGE_IVB, },
4378e9f33406SPeter Missel 	{ "TSSTcorp CDDVDW SH-S202N", "SB01",	  ATA_HORKAGE_IVB, },
43796bbfd53dSAlan Cox 
43809ce8e307SJens Axboe 	/* Devices that do not need bridging limits applied */
43819ce8e307SJens Axboe 	{ "MTRON MSP-SATA*",		NULL,	ATA_HORKAGE_BRIDGE_OK, },
43829ce8e307SJens Axboe 
43839062712fSTejun Heo 	/* Devices which aren't very happy with higher link speeds */
43849062712fSTejun Heo 	{ "WD My Book",			NULL,	ATA_HORKAGE_1_5_GBPS, },
43859062712fSTejun Heo 
4386d0cb43b3STejun Heo 	/*
4387d0cb43b3STejun Heo 	 * Devices which choke on SETXFER.  Applies only if both the
4388d0cb43b3STejun Heo 	 * device and controller are SATA.
4389d0cb43b3STejun Heo 	 */
4390d0cb43b3STejun Heo 	{ "PIONEER DVD-RW  DVRTD08",	"1.00",	ATA_HORKAGE_NOSETXFER },
4391d0cb43b3STejun Heo 
43926919a0a6SAlan Cox 	/* End Marker */
43936919a0a6SAlan Cox 	{ }
4394c6fd2807SJeff Garzik };
4395c6fd2807SJeff Garzik 
4396741b7763SAdrian Bunk static int strn_pattern_cmp(const char *patt, const char *name, int wildchar)
4397539cc7c7SJeff Garzik {
4398539cc7c7SJeff Garzik 	const char *p;
4399539cc7c7SJeff Garzik 	int len;
4400539cc7c7SJeff Garzik 
4401539cc7c7SJeff Garzik 	/*
4402539cc7c7SJeff Garzik 	 * check for trailing wildcard: *\0
4403539cc7c7SJeff Garzik 	 */
4404539cc7c7SJeff Garzik 	p = strchr(patt, wildchar);
4405539cc7c7SJeff Garzik 	if (p && ((*(p + 1)) == 0))
4406539cc7c7SJeff Garzik 		len = p - patt;
4407317b50b8SAndrew Paprocki 	else {
4408539cc7c7SJeff Garzik 		len = strlen(name);
4409317b50b8SAndrew Paprocki 		if (!len) {
4410317b50b8SAndrew Paprocki 			if (!*patt)
4411317b50b8SAndrew Paprocki 				return 0;
4412317b50b8SAndrew Paprocki 			return -1;
4413317b50b8SAndrew Paprocki 		}
4414317b50b8SAndrew Paprocki 	}
4415539cc7c7SJeff Garzik 
4416539cc7c7SJeff Garzik 	return strncmp(patt, name, len);
4417539cc7c7SJeff Garzik }
4418539cc7c7SJeff Garzik 
441975683fe7STejun Heo static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
4420c6fd2807SJeff Garzik {
44218bfa79fcSTejun Heo 	unsigned char model_num[ATA_ID_PROD_LEN + 1];
44228bfa79fcSTejun Heo 	unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
44236919a0a6SAlan Cox 	const struct ata_blacklist_entry *ad = ata_device_blacklist;
4424c6fd2807SJeff Garzik 
44258bfa79fcSTejun Heo 	ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
44268bfa79fcSTejun Heo 	ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
4427c6fd2807SJeff Garzik 
44286919a0a6SAlan Cox 	while (ad->model_num) {
4429539cc7c7SJeff Garzik 		if (!strn_pattern_cmp(ad->model_num, model_num, '*')) {
44306919a0a6SAlan Cox 			if (ad->model_rev == NULL)
44316919a0a6SAlan Cox 				return ad->horkage;
4432539cc7c7SJeff Garzik 			if (!strn_pattern_cmp(ad->model_rev, model_rev, '*'))
44336919a0a6SAlan Cox 				return ad->horkage;
4434c6fd2807SJeff Garzik 		}
44356919a0a6SAlan Cox 		ad++;
4436c6fd2807SJeff Garzik 	}
4437c6fd2807SJeff Garzik 	return 0;
4438c6fd2807SJeff Garzik }
4439c6fd2807SJeff Garzik 
44406919a0a6SAlan Cox static int ata_dma_blacklisted(const struct ata_device *dev)
44416919a0a6SAlan Cox {
44426919a0a6SAlan Cox 	/* We don't support polling DMA.
44436919a0a6SAlan Cox 	 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
44446919a0a6SAlan Cox 	 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
44456919a0a6SAlan Cox 	 */
44469af5c9c9STejun Heo 	if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
44476919a0a6SAlan Cox 	    (dev->flags & ATA_DFLAG_CDB_INTR))
44486919a0a6SAlan Cox 		return 1;
444975683fe7STejun Heo 	return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
44506919a0a6SAlan Cox }
44516919a0a6SAlan Cox 
4452c6fd2807SJeff Garzik /**
44536bbfd53dSAlan Cox  *	ata_is_40wire		-	check drive side detection
44546bbfd53dSAlan Cox  *	@dev: device
44556bbfd53dSAlan Cox  *
44566bbfd53dSAlan Cox  *	Perform drive side detection decoding, allowing for device vendors
44576bbfd53dSAlan Cox  *	who can't follow the documentation.
44586bbfd53dSAlan Cox  */
44596bbfd53dSAlan Cox 
44606bbfd53dSAlan Cox static int ata_is_40wire(struct ata_device *dev)
44616bbfd53dSAlan Cox {
44626bbfd53dSAlan Cox 	if (dev->horkage & ATA_HORKAGE_IVB)
44636bbfd53dSAlan Cox 		return ata_drive_40wire_relaxed(dev->id);
44646bbfd53dSAlan Cox 	return ata_drive_40wire(dev->id);
44656bbfd53dSAlan Cox }
44666bbfd53dSAlan Cox 
44676bbfd53dSAlan Cox /**
446815a5551cSAlan Cox  *	cable_is_40wire		-	40/80/SATA decider
446915a5551cSAlan Cox  *	@ap: port to consider
447015a5551cSAlan Cox  *
447115a5551cSAlan Cox  *	This function encapsulates the policy for speed management
447215a5551cSAlan Cox  *	in one place. At the moment we don't cache the result but
447315a5551cSAlan Cox  *	there is a good case for setting ap->cbl to the result when
447415a5551cSAlan Cox  *	we are called with unknown cables (and figuring out if it
447515a5551cSAlan Cox  *	impacts hotplug at all).
447615a5551cSAlan Cox  *
447715a5551cSAlan Cox  *	Return 1 if the cable appears to be 40 wire.
447815a5551cSAlan Cox  */
447915a5551cSAlan Cox 
448015a5551cSAlan Cox static int cable_is_40wire(struct ata_port *ap)
448115a5551cSAlan Cox {
448215a5551cSAlan Cox 	struct ata_link *link;
448315a5551cSAlan Cox 	struct ata_device *dev;
448415a5551cSAlan Cox 
44854a9c7b33STejun Heo 	/* If the controller thinks we are 40 wire, we are. */
448615a5551cSAlan Cox 	if (ap->cbl == ATA_CBL_PATA40)
448715a5551cSAlan Cox 		return 1;
44884a9c7b33STejun Heo 
44894a9c7b33STejun Heo 	/* If the controller thinks we are 80 wire, we are. */
449015a5551cSAlan Cox 	if (ap->cbl == ATA_CBL_PATA80 || ap->cbl == ATA_CBL_SATA)
449115a5551cSAlan Cox 		return 0;
44924a9c7b33STejun Heo 
44934a9c7b33STejun Heo 	/* If the system is known to be 40 wire short cable (eg
44944a9c7b33STejun Heo 	 * laptop), then we allow 80 wire modes even if the drive
44954a9c7b33STejun Heo 	 * isn't sure.
44964a9c7b33STejun Heo 	 */
4497f792068eSAlan Cox 	if (ap->cbl == ATA_CBL_PATA40_SHORT)
4498f792068eSAlan Cox 		return 0;
449915a5551cSAlan Cox 
45004a9c7b33STejun Heo 	/* If the controller doesn't know, we scan.
45014a9c7b33STejun Heo 	 *
45024a9c7b33STejun Heo 	 * Note: We look for all 40 wire detects at this point.  Any
45034a9c7b33STejun Heo 	 *       80 wire detect is taken to be 80 wire cable because
45044a9c7b33STejun Heo 	 * - in many setups only the one drive (slave if present) will
45054a9c7b33STejun Heo 	 *   give a valid detect
45064a9c7b33STejun Heo 	 * - if you have a non detect capable drive you don't want it
45074a9c7b33STejun Heo 	 *   to colour the choice
450815a5551cSAlan Cox 	 */
45091eca4365STejun Heo 	ata_for_each_link(link, ap, EDGE) {
45101eca4365STejun Heo 		ata_for_each_dev(dev, link, ENABLED) {
45111eca4365STejun Heo 			if (!ata_is_40wire(dev))
451215a5551cSAlan Cox 				return 0;
451315a5551cSAlan Cox 		}
451415a5551cSAlan Cox 	}
451515a5551cSAlan Cox 	return 1;
451615a5551cSAlan Cox }
451715a5551cSAlan Cox 
451815a5551cSAlan Cox /**
4519c6fd2807SJeff Garzik  *	ata_dev_xfermask - Compute supported xfermask of the given device
4520c6fd2807SJeff Garzik  *	@dev: Device to compute xfermask for
4521c6fd2807SJeff Garzik  *
4522c6fd2807SJeff Garzik  *	Compute supported xfermask of @dev and store it in
4523c6fd2807SJeff Garzik  *	dev->*_mask.  This function is responsible for applying all
4524c6fd2807SJeff Garzik  *	known limits including host controller limits, device
4525c6fd2807SJeff Garzik  *	blacklist, etc...
4526c6fd2807SJeff Garzik  *
4527c6fd2807SJeff Garzik  *	LOCKING:
4528c6fd2807SJeff Garzik  *	None.
4529c6fd2807SJeff Garzik  */
4530c6fd2807SJeff Garzik static void ata_dev_xfermask(struct ata_device *dev)
4531c6fd2807SJeff Garzik {
45329af5c9c9STejun Heo 	struct ata_link *link = dev->link;
45339af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
4534cca3974eSJeff Garzik 	struct ata_host *host = ap->host;
4535c6fd2807SJeff Garzik 	unsigned long xfer_mask;
4536c6fd2807SJeff Garzik 
4537c6fd2807SJeff Garzik 	/* controller modes available */
4538c6fd2807SJeff Garzik 	xfer_mask = ata_pack_xfermask(ap->pio_mask,
4539c6fd2807SJeff Garzik 				      ap->mwdma_mask, ap->udma_mask);
4540c6fd2807SJeff Garzik 
45418343f889SRobert Hancock 	/* drive modes available */
4542c6fd2807SJeff Garzik 	xfer_mask &= ata_pack_xfermask(dev->pio_mask,
4543c6fd2807SJeff Garzik 				       dev->mwdma_mask, dev->udma_mask);
4544c6fd2807SJeff Garzik 	xfer_mask &= ata_id_xfermask(dev->id);
4545c6fd2807SJeff Garzik 
4546b352e57dSAlan Cox 	/*
4547b352e57dSAlan Cox 	 *	CFA Advanced TrueIDE timings are not allowed on a shared
4548b352e57dSAlan Cox 	 *	cable
4549b352e57dSAlan Cox 	 */
4550b352e57dSAlan Cox 	if (ata_dev_pair(dev)) {
4551b352e57dSAlan Cox 		/* No PIO5 or PIO6 */
4552b352e57dSAlan Cox 		xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
4553b352e57dSAlan Cox 		/* No MWDMA3 or MWDMA 4 */
4554b352e57dSAlan Cox 		xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
4555b352e57dSAlan Cox 	}
4556b352e57dSAlan Cox 
4557c6fd2807SJeff Garzik 	if (ata_dma_blacklisted(dev)) {
4558c6fd2807SJeff Garzik 		xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4559c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_WARNING,
4560c6fd2807SJeff Garzik 			       "device is on DMA blacklist, disabling DMA\n");
4561c6fd2807SJeff Garzik 	}
4562c6fd2807SJeff Garzik 
456314d66ab7SPetr Vandrovec 	if ((host->flags & ATA_HOST_SIMPLEX) &&
456414d66ab7SPetr Vandrovec 	    host->simplex_claimed && host->simplex_claimed != ap) {
4565c6fd2807SJeff Garzik 		xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4566c6fd2807SJeff Garzik 		ata_dev_printk(dev, KERN_WARNING, "simplex DMA is claimed by "
4567c6fd2807SJeff Garzik 			       "other device, disabling DMA\n");
4568c6fd2807SJeff Garzik 	}
4569c6fd2807SJeff Garzik 
4570e424675fSJeff Garzik 	if (ap->flags & ATA_FLAG_NO_IORDY)
4571e424675fSJeff Garzik 		xfer_mask &= ata_pio_mask_no_iordy(dev);
4572e424675fSJeff Garzik 
4573c6fd2807SJeff Garzik 	if (ap->ops->mode_filter)
4574a76b62caSAlan Cox 		xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
4575c6fd2807SJeff Garzik 
45768343f889SRobert Hancock 	/* Apply cable rule here.  Don't apply it early because when
45778343f889SRobert Hancock 	 * we handle hot plug the cable type can itself change.
45788343f889SRobert Hancock 	 * Check this last so that we know if the transfer rate was
45798343f889SRobert Hancock 	 * solely limited by the cable.
45808343f889SRobert Hancock 	 * Unknown or 80 wire cables reported host side are checked
45818343f889SRobert Hancock 	 * drive side as well. Cases where we know a 40wire cable
45828343f889SRobert Hancock 	 * is used safely for 80 are not checked here.
45838343f889SRobert Hancock 	 */
45848343f889SRobert Hancock 	if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
45858343f889SRobert Hancock 		/* UDMA/44 or higher would be available */
458615a5551cSAlan Cox 		if (cable_is_40wire(ap)) {
45878343f889SRobert Hancock 			ata_dev_printk(dev, KERN_WARNING,
45888343f889SRobert Hancock 				 "limited to UDMA/33 due to 40-wire cable\n");
45898343f889SRobert Hancock 			xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
45908343f889SRobert Hancock 		}
45918343f889SRobert Hancock 
4592c6fd2807SJeff Garzik 	ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
4593c6fd2807SJeff Garzik 			    &dev->mwdma_mask, &dev->udma_mask);
4594c6fd2807SJeff Garzik }
4595c6fd2807SJeff Garzik 
4596c6fd2807SJeff Garzik /**
4597c6fd2807SJeff Garzik  *	ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
4598c6fd2807SJeff Garzik  *	@dev: Device to which command will be sent
4599c6fd2807SJeff Garzik  *
4600c6fd2807SJeff Garzik  *	Issue SET FEATURES - XFER MODE command to device @dev
4601c6fd2807SJeff Garzik  *	on port @ap.
4602c6fd2807SJeff Garzik  *
4603c6fd2807SJeff Garzik  *	LOCKING:
4604c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
4605c6fd2807SJeff Garzik  *
4606c6fd2807SJeff Garzik  *	RETURNS:
4607c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
4608c6fd2807SJeff Garzik  */
4609c6fd2807SJeff Garzik 
4610c6fd2807SJeff Garzik static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
4611c6fd2807SJeff Garzik {
4612c6fd2807SJeff Garzik 	struct ata_taskfile tf;
4613c6fd2807SJeff Garzik 	unsigned int err_mask;
4614c6fd2807SJeff Garzik 
4615c6fd2807SJeff Garzik 	/* set up set-features taskfile */
4616c6fd2807SJeff Garzik 	DPRINTK("set features - xfer mode\n");
4617c6fd2807SJeff Garzik 
4618464cf177STejun Heo 	/* Some controllers and ATAPI devices show flaky interrupt
4619464cf177STejun Heo 	 * behavior after setting xfer mode.  Use polling instead.
4620464cf177STejun Heo 	 */
4621c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
4622c6fd2807SJeff Garzik 	tf.command = ATA_CMD_SET_FEATURES;
4623c6fd2807SJeff Garzik 	tf.feature = SETFEATURES_XFER;
4624464cf177STejun Heo 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
4625c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
4626b9f8ab2dSAlan Cox 	/* If we are using IORDY we must send the mode setting command */
4627b9f8ab2dSAlan Cox 	if (ata_pio_need_iordy(dev))
4628c6fd2807SJeff Garzik 		tf.nsect = dev->xfer_mode;
4629b9f8ab2dSAlan Cox 	/* If the device has IORDY and the controller does not - turn it off */
4630b9f8ab2dSAlan Cox  	else if (ata_id_has_iordy(dev->id))
4631b9f8ab2dSAlan Cox 		tf.nsect = 0x01;
4632b9f8ab2dSAlan Cox 	else /* In the ancient relic department - skip all of this */
4633b9f8ab2dSAlan Cox 		return 0;
4634c6fd2807SJeff Garzik 
46352b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
4636c6fd2807SJeff Garzik 
4637c6fd2807SJeff Garzik 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
4638c6fd2807SJeff Garzik 	return err_mask;
4639c6fd2807SJeff Garzik }
4640c6fd2807SJeff Garzik /**
4641218f3d30SJeff Garzik  *	ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES
46429f45cbd3SKristen Carlson Accardi  *	@dev: Device to which command will be sent
46439f45cbd3SKristen Carlson Accardi  *	@enable: Whether to enable or disable the feature
4644218f3d30SJeff Garzik  *	@feature: The sector count represents the feature to set
46459f45cbd3SKristen Carlson Accardi  *
46469f45cbd3SKristen Carlson Accardi  *	Issue SET FEATURES - SATA FEATURES command to device @dev
4647218f3d30SJeff Garzik  *	on port @ap with sector count
46489f45cbd3SKristen Carlson Accardi  *
46499f45cbd3SKristen Carlson Accardi  *	LOCKING:
46509f45cbd3SKristen Carlson Accardi  *	PCI/etc. bus probe sem.
46519f45cbd3SKristen Carlson Accardi  *
46529f45cbd3SKristen Carlson Accardi  *	RETURNS:
46539f45cbd3SKristen Carlson Accardi  *	0 on success, AC_ERR_* mask otherwise.
46549f45cbd3SKristen Carlson Accardi  */
4655218f3d30SJeff Garzik static unsigned int ata_dev_set_feature(struct ata_device *dev, u8 enable,
4656218f3d30SJeff Garzik 					u8 feature)
46579f45cbd3SKristen Carlson Accardi {
46589f45cbd3SKristen Carlson Accardi 	struct ata_taskfile tf;
46599f45cbd3SKristen Carlson Accardi 	unsigned int err_mask;
46609f45cbd3SKristen Carlson Accardi 
46619f45cbd3SKristen Carlson Accardi 	/* set up set-features taskfile */
46629f45cbd3SKristen Carlson Accardi 	DPRINTK("set features - SATA features\n");
46639f45cbd3SKristen Carlson Accardi 
46649f45cbd3SKristen Carlson Accardi 	ata_tf_init(dev, &tf);
46659f45cbd3SKristen Carlson Accardi 	tf.command = ATA_CMD_SET_FEATURES;
46669f45cbd3SKristen Carlson Accardi 	tf.feature = enable;
46679f45cbd3SKristen Carlson Accardi 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
46689f45cbd3SKristen Carlson Accardi 	tf.protocol = ATA_PROT_NODATA;
4669218f3d30SJeff Garzik 	tf.nsect = feature;
46709f45cbd3SKristen Carlson Accardi 
46712b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
46729f45cbd3SKristen Carlson Accardi 
46739f45cbd3SKristen Carlson Accardi 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
46749f45cbd3SKristen Carlson Accardi 	return err_mask;
46759f45cbd3SKristen Carlson Accardi }
46769f45cbd3SKristen Carlson Accardi 
46779f45cbd3SKristen Carlson Accardi /**
4678c6fd2807SJeff Garzik  *	ata_dev_init_params - Issue INIT DEV PARAMS command
4679c6fd2807SJeff Garzik  *	@dev: Device to which command will be sent
4680c6fd2807SJeff Garzik  *	@heads: Number of heads (taskfile parameter)
4681c6fd2807SJeff Garzik  *	@sectors: Number of sectors (taskfile parameter)
4682c6fd2807SJeff Garzik  *
4683c6fd2807SJeff Garzik  *	LOCKING:
4684c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
4685c6fd2807SJeff Garzik  *
4686c6fd2807SJeff Garzik  *	RETURNS:
4687c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
4688c6fd2807SJeff Garzik  */
4689c6fd2807SJeff Garzik static unsigned int ata_dev_init_params(struct ata_device *dev,
4690c6fd2807SJeff Garzik 					u16 heads, u16 sectors)
4691c6fd2807SJeff Garzik {
4692c6fd2807SJeff Garzik 	struct ata_taskfile tf;
4693c6fd2807SJeff Garzik 	unsigned int err_mask;
4694c6fd2807SJeff Garzik 
4695c6fd2807SJeff Garzik 	/* Number of sectors per track 1-255. Number of heads 1-16 */
4696c6fd2807SJeff Garzik 	if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
4697c6fd2807SJeff Garzik 		return AC_ERR_INVALID;
4698c6fd2807SJeff Garzik 
4699c6fd2807SJeff Garzik 	/* set up init dev params taskfile */
4700c6fd2807SJeff Garzik 	DPRINTK("init dev params \n");
4701c6fd2807SJeff Garzik 
4702c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
4703c6fd2807SJeff Garzik 	tf.command = ATA_CMD_INIT_DEV_PARAMS;
4704c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4705c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
4706c6fd2807SJeff Garzik 	tf.nsect = sectors;
4707c6fd2807SJeff Garzik 	tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
4708c6fd2807SJeff Garzik 
47092b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
471018b2466cSAlan Cox 	/* A clean abort indicates an original or just out of spec drive
471118b2466cSAlan Cox 	   and we should continue as we issue the setup based on the
471218b2466cSAlan Cox 	   drive reported working geometry */
471318b2466cSAlan Cox 	if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
471418b2466cSAlan Cox 		err_mask = 0;
4715c6fd2807SJeff Garzik 
4716c6fd2807SJeff Garzik 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
4717c6fd2807SJeff Garzik 	return err_mask;
4718c6fd2807SJeff Garzik }
4719c6fd2807SJeff Garzik 
4720c6fd2807SJeff Garzik /**
4721c6fd2807SJeff Garzik  *	ata_sg_clean - Unmap DMA memory associated with command
4722c6fd2807SJeff Garzik  *	@qc: Command containing DMA memory to be released
4723c6fd2807SJeff Garzik  *
4724c6fd2807SJeff Garzik  *	Unmap all mapped DMA memory associated with this command.
4725c6fd2807SJeff Garzik  *
4726c6fd2807SJeff Garzik  *	LOCKING:
4727cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4728c6fd2807SJeff Garzik  */
472970e6ad0cSTejun Heo void ata_sg_clean(struct ata_queued_cmd *qc)
4730c6fd2807SJeff Garzik {
4731c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4732ff2aeb1eSTejun Heo 	struct scatterlist *sg = qc->sg;
4733c6fd2807SJeff Garzik 	int dir = qc->dma_dir;
4734c6fd2807SJeff Garzik 
4735efcb3cf7STejun Heo 	WARN_ON_ONCE(sg == NULL);
4736c6fd2807SJeff Garzik 
4737dde20207SJames Bottomley 	VPRINTK("unmapping %u sg elements\n", qc->n_elem);
4738c6fd2807SJeff Garzik 
4739dde20207SJames Bottomley 	if (qc->n_elem)
47405825627cSFUJITA Tomonori 		dma_unmap_sg(ap->dev, sg, qc->orig_n_elem, dir);
4741c6fd2807SJeff Garzik 
4742c6fd2807SJeff Garzik 	qc->flags &= ~ATA_QCFLAG_DMAMAP;
4743ff2aeb1eSTejun Heo 	qc->sg = NULL;
4744c6fd2807SJeff Garzik }
4745c6fd2807SJeff Garzik 
4746c6fd2807SJeff Garzik /**
47475895ef9aSTejun Heo  *	atapi_check_dma - Check whether ATAPI DMA can be supported
4748c6fd2807SJeff Garzik  *	@qc: Metadata associated with taskfile to check
4749c6fd2807SJeff Garzik  *
4750c6fd2807SJeff Garzik  *	Allow low-level driver to filter ATA PACKET commands, returning
4751c6fd2807SJeff Garzik  *	a status indicating whether or not it is OK to use DMA for the
4752c6fd2807SJeff Garzik  *	supplied PACKET command.
4753c6fd2807SJeff Garzik  *
4754c6fd2807SJeff Garzik  *	LOCKING:
4755cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4756c6fd2807SJeff Garzik  *
4757c6fd2807SJeff Garzik  *	RETURNS: 0 when ATAPI DMA can be used
4758c6fd2807SJeff Garzik  *               nonzero otherwise
4759c6fd2807SJeff Garzik  */
47605895ef9aSTejun Heo int atapi_check_dma(struct ata_queued_cmd *qc)
4761c6fd2807SJeff Garzik {
4762c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4763c6fd2807SJeff Garzik 
4764b9a4197eSTejun Heo 	/* Don't allow DMA if it isn't multiple of 16 bytes.  Quite a
4765b9a4197eSTejun Heo 	 * few ATAPI devices choke on such DMA requests.
4766b9a4197eSTejun Heo 	 */
47676a87e42eSTejun Heo 	if (!(qc->dev->horkage & ATA_HORKAGE_ATAPI_MOD16_DMA) &&
47686a87e42eSTejun Heo 	    unlikely(qc->nbytes & 15))
47696f23a31dSAlbert Lee 		return 1;
47706f23a31dSAlbert Lee 
4771c6fd2807SJeff Garzik 	if (ap->ops->check_atapi_dma)
4772b9a4197eSTejun Heo 		return ap->ops->check_atapi_dma(qc);
4773c6fd2807SJeff Garzik 
4774b9a4197eSTejun Heo 	return 0;
4775c6fd2807SJeff Garzik }
4776b9a4197eSTejun Heo 
4777c6fd2807SJeff Garzik /**
477831cc23b3STejun Heo  *	ata_std_qc_defer - Check whether a qc needs to be deferred
477931cc23b3STejun Heo  *	@qc: ATA command in question
478031cc23b3STejun Heo  *
478131cc23b3STejun Heo  *	Non-NCQ commands cannot run with any other command, NCQ or
478231cc23b3STejun Heo  *	not.  As upper layer only knows the queue depth, we are
478331cc23b3STejun Heo  *	responsible for maintaining exclusion.  This function checks
478431cc23b3STejun Heo  *	whether a new command @qc can be issued.
478531cc23b3STejun Heo  *
478631cc23b3STejun Heo  *	LOCKING:
478731cc23b3STejun Heo  *	spin_lock_irqsave(host lock)
478831cc23b3STejun Heo  *
478931cc23b3STejun Heo  *	RETURNS:
479031cc23b3STejun Heo  *	ATA_DEFER_* if deferring is needed, 0 otherwise.
479131cc23b3STejun Heo  */
479231cc23b3STejun Heo int ata_std_qc_defer(struct ata_queued_cmd *qc)
479331cc23b3STejun Heo {
479431cc23b3STejun Heo 	struct ata_link *link = qc->dev->link;
479531cc23b3STejun Heo 
479631cc23b3STejun Heo 	if (qc->tf.protocol == ATA_PROT_NCQ) {
479731cc23b3STejun Heo 		if (!ata_tag_valid(link->active_tag))
479831cc23b3STejun Heo 			return 0;
479931cc23b3STejun Heo 	} else {
480031cc23b3STejun Heo 		if (!ata_tag_valid(link->active_tag) && !link->sactive)
480131cc23b3STejun Heo 			return 0;
480231cc23b3STejun Heo 	}
480331cc23b3STejun Heo 
480431cc23b3STejun Heo 	return ATA_DEFER_LINK;
480531cc23b3STejun Heo }
480631cc23b3STejun Heo 
4807c6fd2807SJeff Garzik void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
4808c6fd2807SJeff Garzik 
4809c6fd2807SJeff Garzik /**
4810c6fd2807SJeff Garzik  *	ata_sg_init - Associate command with scatter-gather table.
4811c6fd2807SJeff Garzik  *	@qc: Command to be associated
4812c6fd2807SJeff Garzik  *	@sg: Scatter-gather table.
4813c6fd2807SJeff Garzik  *	@n_elem: Number of elements in s/g table.
4814c6fd2807SJeff Garzik  *
4815c6fd2807SJeff Garzik  *	Initialize the data-related elements of queued_cmd @qc
4816c6fd2807SJeff Garzik  *	to point to a scatter-gather table @sg, containing @n_elem
4817c6fd2807SJeff Garzik  *	elements.
4818c6fd2807SJeff Garzik  *
4819c6fd2807SJeff Garzik  *	LOCKING:
4820cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4821c6fd2807SJeff Garzik  */
4822c6fd2807SJeff Garzik void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
4823c6fd2807SJeff Garzik 		 unsigned int n_elem)
4824c6fd2807SJeff Garzik {
4825ff2aeb1eSTejun Heo 	qc->sg = sg;
4826c6fd2807SJeff Garzik 	qc->n_elem = n_elem;
4827ff2aeb1eSTejun Heo 	qc->cursg = qc->sg;
4828ff2aeb1eSTejun Heo }
4829ff2aeb1eSTejun Heo 
4830c6fd2807SJeff Garzik /**
4831c6fd2807SJeff Garzik  *	ata_sg_setup - DMA-map the scatter-gather table associated with a command.
4832c6fd2807SJeff Garzik  *	@qc: Command with scatter-gather table to be mapped.
4833c6fd2807SJeff Garzik  *
4834c6fd2807SJeff Garzik  *	DMA-map the scatter-gather table associated with queued_cmd @qc.
4835c6fd2807SJeff Garzik  *
4836c6fd2807SJeff Garzik  *	LOCKING:
4837cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4838c6fd2807SJeff Garzik  *
4839c6fd2807SJeff Garzik  *	RETURNS:
4840c6fd2807SJeff Garzik  *	Zero on success, negative on error.
4841c6fd2807SJeff Garzik  *
4842c6fd2807SJeff Garzik  */
4843c6fd2807SJeff Garzik static int ata_sg_setup(struct ata_queued_cmd *qc)
4844c6fd2807SJeff Garzik {
4845c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4846dde20207SJames Bottomley 	unsigned int n_elem;
4847c6fd2807SJeff Garzik 
484844877b4eSTejun Heo 	VPRINTK("ENTER, ata%u\n", ap->print_id);
4849c6fd2807SJeff Garzik 
4850dde20207SJames Bottomley 	n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir);
4851dde20207SJames Bottomley 	if (n_elem < 1)
4852c6fd2807SJeff Garzik 		return -1;
4853c6fd2807SJeff Garzik 
4854dde20207SJames Bottomley 	DPRINTK("%d sg elements mapped\n", n_elem);
48555825627cSFUJITA Tomonori 	qc->orig_n_elem = qc->n_elem;
4856dde20207SJames Bottomley 	qc->n_elem = n_elem;
4857f92a2636STejun Heo 	qc->flags |= ATA_QCFLAG_DMAMAP;
4858c6fd2807SJeff Garzik 
4859c6fd2807SJeff Garzik 	return 0;
4860c6fd2807SJeff Garzik }
4861c6fd2807SJeff Garzik 
4862c6fd2807SJeff Garzik /**
4863c6fd2807SJeff Garzik  *	swap_buf_le16 - swap halves of 16-bit words in place
4864c6fd2807SJeff Garzik  *	@buf:  Buffer to swap
4865c6fd2807SJeff Garzik  *	@buf_words:  Number of 16-bit words in buffer.
4866c6fd2807SJeff Garzik  *
4867c6fd2807SJeff Garzik  *	Swap halves of 16-bit words if needed to convert from
4868c6fd2807SJeff Garzik  *	little-endian byte order to native cpu byte order, or
4869c6fd2807SJeff Garzik  *	vice-versa.
4870c6fd2807SJeff Garzik  *
4871c6fd2807SJeff Garzik  *	LOCKING:
4872c6fd2807SJeff Garzik  *	Inherited from caller.
4873c6fd2807SJeff Garzik  */
4874c6fd2807SJeff Garzik void swap_buf_le16(u16 *buf, unsigned int buf_words)
4875c6fd2807SJeff Garzik {
4876c6fd2807SJeff Garzik #ifdef __BIG_ENDIAN
4877c6fd2807SJeff Garzik 	unsigned int i;
4878c6fd2807SJeff Garzik 
4879c6fd2807SJeff Garzik 	for (i = 0; i < buf_words; i++)
4880c6fd2807SJeff Garzik 		buf[i] = le16_to_cpu(buf[i]);
4881c6fd2807SJeff Garzik #endif /* __BIG_ENDIAN */
4882c6fd2807SJeff Garzik }
4883c6fd2807SJeff Garzik 
4884c6fd2807SJeff Garzik /**
48858a8bc223STejun Heo  *	ata_qc_new - Request an available ATA command, for queueing
48865eb66fe0SRandy Dunlap  *	@ap: target port
48878a8bc223STejun Heo  *
48888a8bc223STejun Heo  *	LOCKING:
48898a8bc223STejun Heo  *	None.
48908a8bc223STejun Heo  */
48918a8bc223STejun Heo 
48928a8bc223STejun Heo static struct ata_queued_cmd *ata_qc_new(struct ata_port *ap)
48938a8bc223STejun Heo {
48948a8bc223STejun Heo 	struct ata_queued_cmd *qc = NULL;
48958a8bc223STejun Heo 	unsigned int i;
48968a8bc223STejun Heo 
48978a8bc223STejun Heo 	/* no command while frozen */
48988a8bc223STejun Heo 	if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
48998a8bc223STejun Heo 		return NULL;
49008a8bc223STejun Heo 
49018a8bc223STejun Heo 	/* the last tag is reserved for internal command. */
49028a8bc223STejun Heo 	for (i = 0; i < ATA_MAX_QUEUE - 1; i++)
49038a8bc223STejun Heo 		if (!test_and_set_bit(i, &ap->qc_allocated)) {
49048a8bc223STejun Heo 			qc = __ata_qc_from_tag(ap, i);
49058a8bc223STejun Heo 			break;
49068a8bc223STejun Heo 		}
49078a8bc223STejun Heo 
49088a8bc223STejun Heo 	if (qc)
49098a8bc223STejun Heo 		qc->tag = i;
49108a8bc223STejun Heo 
49118a8bc223STejun Heo 	return qc;
49128a8bc223STejun Heo }
49138a8bc223STejun Heo 
49148a8bc223STejun Heo /**
4915c6fd2807SJeff Garzik  *	ata_qc_new_init - Request an available ATA command, and initialize it
4916c6fd2807SJeff Garzik  *	@dev: Device from whom we request an available command structure
4917c6fd2807SJeff Garzik  *
4918c6fd2807SJeff Garzik  *	LOCKING:
4919c6fd2807SJeff Garzik  *	None.
4920c6fd2807SJeff Garzik  */
4921c6fd2807SJeff Garzik 
49228a8bc223STejun Heo struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev)
4923c6fd2807SJeff Garzik {
49249af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
4925c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
4926c6fd2807SJeff Garzik 
49278a8bc223STejun Heo 	qc = ata_qc_new(ap);
4928c6fd2807SJeff Garzik 	if (qc) {
4929c6fd2807SJeff Garzik 		qc->scsicmd = NULL;
4930c6fd2807SJeff Garzik 		qc->ap = ap;
4931c6fd2807SJeff Garzik 		qc->dev = dev;
4932c6fd2807SJeff Garzik 
4933c6fd2807SJeff Garzik 		ata_qc_reinit(qc);
4934c6fd2807SJeff Garzik 	}
4935c6fd2807SJeff Garzik 
4936c6fd2807SJeff Garzik 	return qc;
4937c6fd2807SJeff Garzik }
4938c6fd2807SJeff Garzik 
49398a8bc223STejun Heo /**
49408a8bc223STejun Heo  *	ata_qc_free - free unused ata_queued_cmd
49418a8bc223STejun Heo  *	@qc: Command to complete
49428a8bc223STejun Heo  *
49438a8bc223STejun Heo  *	Designed to free unused ata_queued_cmd object
49448a8bc223STejun Heo  *	in case something prevents using it.
49458a8bc223STejun Heo  *
49468a8bc223STejun Heo  *	LOCKING:
49478a8bc223STejun Heo  *	spin_lock_irqsave(host lock)
49488a8bc223STejun Heo  */
49498a8bc223STejun Heo void ata_qc_free(struct ata_queued_cmd *qc)
49508a8bc223STejun Heo {
4951a1104016SJulia Lawall 	struct ata_port *ap;
49528a8bc223STejun Heo 	unsigned int tag;
49538a8bc223STejun Heo 
4954efcb3cf7STejun Heo 	WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4955a1104016SJulia Lawall 	ap = qc->ap;
49568a8bc223STejun Heo 
49578a8bc223STejun Heo 	qc->flags = 0;
49588a8bc223STejun Heo 	tag = qc->tag;
49598a8bc223STejun Heo 	if (likely(ata_tag_valid(tag))) {
49608a8bc223STejun Heo 		qc->tag = ATA_TAG_POISON;
49618a8bc223STejun Heo 		clear_bit(tag, &ap->qc_allocated);
49628a8bc223STejun Heo 	}
49638a8bc223STejun Heo }
49648a8bc223STejun Heo 
4965c6fd2807SJeff Garzik void __ata_qc_complete(struct ata_queued_cmd *qc)
4966c6fd2807SJeff Garzik {
4967a1104016SJulia Lawall 	struct ata_port *ap;
4968a1104016SJulia Lawall 	struct ata_link *link;
4969c6fd2807SJeff Garzik 
4970efcb3cf7STejun Heo 	WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4971efcb3cf7STejun Heo 	WARN_ON_ONCE(!(qc->flags & ATA_QCFLAG_ACTIVE));
4972a1104016SJulia Lawall 	ap = qc->ap;
4973a1104016SJulia Lawall 	link = qc->dev->link;
4974c6fd2807SJeff Garzik 
4975c6fd2807SJeff Garzik 	if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
4976c6fd2807SJeff Garzik 		ata_sg_clean(qc);
4977c6fd2807SJeff Garzik 
4978c6fd2807SJeff Garzik 	/* command should be marked inactive atomically with qc completion */
4979da917d69STejun Heo 	if (qc->tf.protocol == ATA_PROT_NCQ) {
49809af5c9c9STejun Heo 		link->sactive &= ~(1 << qc->tag);
4981da917d69STejun Heo 		if (!link->sactive)
4982da917d69STejun Heo 			ap->nr_active_links--;
4983da917d69STejun Heo 	} else {
49849af5c9c9STejun Heo 		link->active_tag = ATA_TAG_POISON;
4985da917d69STejun Heo 		ap->nr_active_links--;
4986da917d69STejun Heo 	}
4987da917d69STejun Heo 
4988da917d69STejun Heo 	/* clear exclusive status */
4989da917d69STejun Heo 	if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
4990da917d69STejun Heo 		     ap->excl_link == link))
4991da917d69STejun Heo 		ap->excl_link = NULL;
4992c6fd2807SJeff Garzik 
4993c6fd2807SJeff Garzik 	/* atapi: mark qc as inactive to prevent the interrupt handler
4994c6fd2807SJeff Garzik 	 * from completing the command twice later, before the error handler
4995c6fd2807SJeff Garzik 	 * is called. (when rc != 0 and atapi request sense is needed)
4996c6fd2807SJeff Garzik 	 */
4997c6fd2807SJeff Garzik 	qc->flags &= ~ATA_QCFLAG_ACTIVE;
4998c6fd2807SJeff Garzik 	ap->qc_active &= ~(1 << qc->tag);
4999c6fd2807SJeff Garzik 
5000c6fd2807SJeff Garzik 	/* call completion callback */
5001c6fd2807SJeff Garzik 	qc->complete_fn(qc);
5002c6fd2807SJeff Garzik }
5003c6fd2807SJeff Garzik 
500439599a53STejun Heo static void fill_result_tf(struct ata_queued_cmd *qc)
500539599a53STejun Heo {
500639599a53STejun Heo 	struct ata_port *ap = qc->ap;
500739599a53STejun Heo 
500839599a53STejun Heo 	qc->result_tf.flags = qc->tf.flags;
500922183bf5STejun Heo 	ap->ops->qc_fill_rtf(qc);
501039599a53STejun Heo }
501139599a53STejun Heo 
501200115e0fSTejun Heo static void ata_verify_xfer(struct ata_queued_cmd *qc)
501300115e0fSTejun Heo {
501400115e0fSTejun Heo 	struct ata_device *dev = qc->dev;
501500115e0fSTejun Heo 
501600115e0fSTejun Heo 	if (ata_tag_internal(qc->tag))
501700115e0fSTejun Heo 		return;
501800115e0fSTejun Heo 
501900115e0fSTejun Heo 	if (ata_is_nodata(qc->tf.protocol))
502000115e0fSTejun Heo 		return;
502100115e0fSTejun Heo 
502200115e0fSTejun Heo 	if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
502300115e0fSTejun Heo 		return;
502400115e0fSTejun Heo 
502500115e0fSTejun Heo 	dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
502600115e0fSTejun Heo }
502700115e0fSTejun Heo 
5028c6fd2807SJeff Garzik /**
5029c6fd2807SJeff Garzik  *	ata_qc_complete - Complete an active ATA command
5030c6fd2807SJeff Garzik  *	@qc: Command to complete
5031c6fd2807SJeff Garzik  *
5032c6fd2807SJeff Garzik  *	Indicate to the mid and upper layers that an ATA
5033c6fd2807SJeff Garzik  *	command has completed, with either an ok or not-ok status.
5034c6fd2807SJeff Garzik  *
5035c6fd2807SJeff Garzik  *	LOCKING:
5036cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
5037c6fd2807SJeff Garzik  */
5038c6fd2807SJeff Garzik void ata_qc_complete(struct ata_queued_cmd *qc)
5039c6fd2807SJeff Garzik {
5040c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
5041c6fd2807SJeff Garzik 
5042c6fd2807SJeff Garzik 	/* XXX: New EH and old EH use different mechanisms to
5043c6fd2807SJeff Garzik 	 * synchronize EH with regular execution path.
5044c6fd2807SJeff Garzik 	 *
5045c6fd2807SJeff Garzik 	 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
5046c6fd2807SJeff Garzik 	 * Normal execution path is responsible for not accessing a
5047c6fd2807SJeff Garzik 	 * failed qc.  libata core enforces the rule by returning NULL
5048c6fd2807SJeff Garzik 	 * from ata_qc_from_tag() for failed qcs.
5049c6fd2807SJeff Garzik 	 *
5050c6fd2807SJeff Garzik 	 * Old EH depends on ata_qc_complete() nullifying completion
5051c6fd2807SJeff Garzik 	 * requests if ATA_QCFLAG_EH_SCHEDULED is set.  Old EH does
5052c6fd2807SJeff Garzik 	 * not synchronize with interrupt handler.  Only PIO task is
5053c6fd2807SJeff Garzik 	 * taken care of.
5054c6fd2807SJeff Garzik 	 */
5055c6fd2807SJeff Garzik 	if (ap->ops->error_handler) {
50564dbfa39bSTejun Heo 		struct ata_device *dev = qc->dev;
50574dbfa39bSTejun Heo 		struct ata_eh_info *ehi = &dev->link->eh_info;
50584dbfa39bSTejun Heo 
5059c6fd2807SJeff Garzik 		if (unlikely(qc->err_mask))
5060c6fd2807SJeff Garzik 			qc->flags |= ATA_QCFLAG_FAILED;
5061c6fd2807SJeff Garzik 
5062c6fd2807SJeff Garzik 		if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
5063c6fd2807SJeff Garzik 			/* always fill result TF for failed qc */
506439599a53STejun Heo 			fill_result_tf(qc);
5065f4b31db9STejun Heo 
5066f4b31db9STejun Heo 			if (!ata_tag_internal(qc->tag))
5067c6fd2807SJeff Garzik 				ata_qc_schedule_eh(qc);
5068f4b31db9STejun Heo 			else
5069f4b31db9STejun Heo 				__ata_qc_complete(qc);
5070c6fd2807SJeff Garzik 			return;
5071c6fd2807SJeff Garzik 		}
5072c6fd2807SJeff Garzik 
50734dc738edSTejun Heo 		WARN_ON_ONCE(ap->pflags & ATA_PFLAG_FROZEN);
50744dc738edSTejun Heo 
5075c6fd2807SJeff Garzik 		/* read result TF if requested */
5076c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_RESULT_TF)
507739599a53STejun Heo 			fill_result_tf(qc);
5078c6fd2807SJeff Garzik 
50794dbfa39bSTejun Heo 		/* Some commands need post-processing after successful
50804dbfa39bSTejun Heo 		 * completion.
50814dbfa39bSTejun Heo 		 */
50824dbfa39bSTejun Heo 		switch (qc->tf.command) {
50834dbfa39bSTejun Heo 		case ATA_CMD_SET_FEATURES:
50844dbfa39bSTejun Heo 			if (qc->tf.feature != SETFEATURES_WC_ON &&
50854dbfa39bSTejun Heo 			    qc->tf.feature != SETFEATURES_WC_OFF)
50864dbfa39bSTejun Heo 				break;
50874dbfa39bSTejun Heo 			/* fall through */
50884dbfa39bSTejun Heo 		case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
50894dbfa39bSTejun Heo 		case ATA_CMD_SET_MULTI: /* multi_count changed */
50904dbfa39bSTejun Heo 			/* revalidate device */
50914dbfa39bSTejun Heo 			ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE;
50924dbfa39bSTejun Heo 			ata_port_schedule_eh(ap);
50934dbfa39bSTejun Heo 			break;
5094054a5fbaSTejun Heo 
5095054a5fbaSTejun Heo 		case ATA_CMD_SLEEP:
5096054a5fbaSTejun Heo 			dev->flags |= ATA_DFLAG_SLEEPING;
5097054a5fbaSTejun Heo 			break;
50984dbfa39bSTejun Heo 		}
50994dbfa39bSTejun Heo 
510000115e0fSTejun Heo 		if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
510100115e0fSTejun Heo 			ata_verify_xfer(qc);
510200115e0fSTejun Heo 
5103c6fd2807SJeff Garzik 		__ata_qc_complete(qc);
5104c6fd2807SJeff Garzik 	} else {
5105c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
5106c6fd2807SJeff Garzik 			return;
5107c6fd2807SJeff Garzik 
5108c6fd2807SJeff Garzik 		/* read result TF if failed or requested */
5109c6fd2807SJeff Garzik 		if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
511039599a53STejun Heo 			fill_result_tf(qc);
5111c6fd2807SJeff Garzik 
5112c6fd2807SJeff Garzik 		__ata_qc_complete(qc);
5113c6fd2807SJeff Garzik 	}
5114c6fd2807SJeff Garzik }
5115c6fd2807SJeff Garzik 
5116c6fd2807SJeff Garzik /**
5117c6fd2807SJeff Garzik  *	ata_qc_complete_multiple - Complete multiple qcs successfully
5118c6fd2807SJeff Garzik  *	@ap: port in question
5119c6fd2807SJeff Garzik  *	@qc_active: new qc_active mask
5120c6fd2807SJeff Garzik  *
5121c6fd2807SJeff Garzik  *	Complete in-flight commands.  This functions is meant to be
5122c6fd2807SJeff Garzik  *	called from low-level driver's interrupt routine to complete
5123c6fd2807SJeff Garzik  *	requests normally.  ap->qc_active and @qc_active is compared
5124c6fd2807SJeff Garzik  *	and commands are completed accordingly.
5125c6fd2807SJeff Garzik  *
5126c6fd2807SJeff Garzik  *	LOCKING:
5127cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
5128c6fd2807SJeff Garzik  *
5129c6fd2807SJeff Garzik  *	RETURNS:
5130c6fd2807SJeff Garzik  *	Number of completed commands on success, -errno otherwise.
5131c6fd2807SJeff Garzik  */
513279f97dadSTejun Heo int ata_qc_complete_multiple(struct ata_port *ap, u32 qc_active)
5133c6fd2807SJeff Garzik {
5134c6fd2807SJeff Garzik 	int nr_done = 0;
5135c6fd2807SJeff Garzik 	u32 done_mask;
5136c6fd2807SJeff Garzik 
5137c6fd2807SJeff Garzik 	done_mask = ap->qc_active ^ qc_active;
5138c6fd2807SJeff Garzik 
5139c6fd2807SJeff Garzik 	if (unlikely(done_mask & qc_active)) {
5140c6fd2807SJeff Garzik 		ata_port_printk(ap, KERN_ERR, "illegal qc_active transition "
5141c6fd2807SJeff Garzik 				"(%08x->%08x)\n", ap->qc_active, qc_active);
5142c6fd2807SJeff Garzik 		return -EINVAL;
5143c6fd2807SJeff Garzik 	}
5144c6fd2807SJeff Garzik 
514543768180SJens Axboe 	while (done_mask) {
5146c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc;
514743768180SJens Axboe 		unsigned int tag = __ffs(done_mask);
5148c6fd2807SJeff Garzik 
514943768180SJens Axboe 		qc = ata_qc_from_tag(ap, tag);
515043768180SJens Axboe 		if (qc) {
5151c6fd2807SJeff Garzik 			ata_qc_complete(qc);
5152c6fd2807SJeff Garzik 			nr_done++;
5153c6fd2807SJeff Garzik 		}
515443768180SJens Axboe 		done_mask &= ~(1 << tag);
5155c6fd2807SJeff Garzik 	}
5156c6fd2807SJeff Garzik 
5157c6fd2807SJeff Garzik 	return nr_done;
5158c6fd2807SJeff Garzik }
5159c6fd2807SJeff Garzik 
5160c6fd2807SJeff Garzik /**
5161c6fd2807SJeff Garzik  *	ata_qc_issue - issue taskfile to device
5162c6fd2807SJeff Garzik  *	@qc: command to issue to device
5163c6fd2807SJeff Garzik  *
5164c6fd2807SJeff Garzik  *	Prepare an ATA command to submission to device.
5165c6fd2807SJeff Garzik  *	This includes mapping the data into a DMA-able
5166c6fd2807SJeff Garzik  *	area, filling in the S/G table, and finally
5167c6fd2807SJeff Garzik  *	writing the taskfile to hardware, starting the command.
5168c6fd2807SJeff Garzik  *
5169c6fd2807SJeff Garzik  *	LOCKING:
5170cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
5171c6fd2807SJeff Garzik  */
5172c6fd2807SJeff Garzik void ata_qc_issue(struct ata_queued_cmd *qc)
5173c6fd2807SJeff Garzik {
5174c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
51759af5c9c9STejun Heo 	struct ata_link *link = qc->dev->link;
5176405e66b3STejun Heo 	u8 prot = qc->tf.protocol;
5177c6fd2807SJeff Garzik 
5178c6fd2807SJeff Garzik 	/* Make sure only one non-NCQ command is outstanding.  The
5179c6fd2807SJeff Garzik 	 * check is skipped for old EH because it reuses active qc to
5180c6fd2807SJeff Garzik 	 * request ATAPI sense.
5181c6fd2807SJeff Garzik 	 */
5182efcb3cf7STejun Heo 	WARN_ON_ONCE(ap->ops->error_handler && ata_tag_valid(link->active_tag));
5183c6fd2807SJeff Garzik 
51841973a023STejun Heo 	if (ata_is_ncq(prot)) {
5185efcb3cf7STejun Heo 		WARN_ON_ONCE(link->sactive & (1 << qc->tag));
5186da917d69STejun Heo 
5187da917d69STejun Heo 		if (!link->sactive)
5188da917d69STejun Heo 			ap->nr_active_links++;
51899af5c9c9STejun Heo 		link->sactive |= 1 << qc->tag;
5190c6fd2807SJeff Garzik 	} else {
5191efcb3cf7STejun Heo 		WARN_ON_ONCE(link->sactive);
5192da917d69STejun Heo 
5193da917d69STejun Heo 		ap->nr_active_links++;
51949af5c9c9STejun Heo 		link->active_tag = qc->tag;
5195c6fd2807SJeff Garzik 	}
5196c6fd2807SJeff Garzik 
5197c6fd2807SJeff Garzik 	qc->flags |= ATA_QCFLAG_ACTIVE;
5198c6fd2807SJeff Garzik 	ap->qc_active |= 1 << qc->tag;
5199c6fd2807SJeff Garzik 
5200f92a2636STejun Heo 	/* We guarantee to LLDs that they will have at least one
5201f92a2636STejun Heo 	 * non-zero sg if the command is a data command.
5202f92a2636STejun Heo 	 */
5203ff2aeb1eSTejun Heo 	BUG_ON(ata_is_data(prot) && (!qc->sg || !qc->n_elem || !qc->nbytes));
5204f92a2636STejun Heo 
5205405e66b3STejun Heo 	if (ata_is_dma(prot) || (ata_is_pio(prot) &&
5206f92a2636STejun Heo 				 (ap->flags & ATA_FLAG_PIO_DMA)))
5207c6fd2807SJeff Garzik 		if (ata_sg_setup(qc))
5208c6fd2807SJeff Garzik 			goto sg_err;
5209c6fd2807SJeff Garzik 
5210cf480626STejun Heo 	/* if device is sleeping, schedule reset and abort the link */
5211054a5fbaSTejun Heo 	if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
5212cf480626STejun Heo 		link->eh_info.action |= ATA_EH_RESET;
5213054a5fbaSTejun Heo 		ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
5214054a5fbaSTejun Heo 		ata_link_abort(link);
5215054a5fbaSTejun Heo 		return;
5216054a5fbaSTejun Heo 	}
5217054a5fbaSTejun Heo 
5218c6fd2807SJeff Garzik 	ap->ops->qc_prep(qc);
5219c6fd2807SJeff Garzik 
5220c6fd2807SJeff Garzik 	qc->err_mask |= ap->ops->qc_issue(qc);
5221c6fd2807SJeff Garzik 	if (unlikely(qc->err_mask))
5222c6fd2807SJeff Garzik 		goto err;
5223c6fd2807SJeff Garzik 	return;
5224c6fd2807SJeff Garzik 
5225c6fd2807SJeff Garzik sg_err:
5226c6fd2807SJeff Garzik 	qc->err_mask |= AC_ERR_SYSTEM;
5227c6fd2807SJeff Garzik err:
5228c6fd2807SJeff Garzik 	ata_qc_complete(qc);
5229c6fd2807SJeff Garzik }
5230c6fd2807SJeff Garzik 
5231c6fd2807SJeff Garzik /**
5232c6fd2807SJeff Garzik  *	sata_scr_valid - test whether SCRs are accessible
5233936fd732STejun Heo  *	@link: ATA link to test SCR accessibility for
5234c6fd2807SJeff Garzik  *
5235936fd732STejun Heo  *	Test whether SCRs are accessible for @link.
5236c6fd2807SJeff Garzik  *
5237c6fd2807SJeff Garzik  *	LOCKING:
5238c6fd2807SJeff Garzik  *	None.
5239c6fd2807SJeff Garzik  *
5240c6fd2807SJeff Garzik  *	RETURNS:
5241c6fd2807SJeff Garzik  *	1 if SCRs are accessible, 0 otherwise.
5242c6fd2807SJeff Garzik  */
5243936fd732STejun Heo int sata_scr_valid(struct ata_link *link)
5244c6fd2807SJeff Garzik {
5245936fd732STejun Heo 	struct ata_port *ap = link->ap;
5246936fd732STejun Heo 
5247a16abc0bSTejun Heo 	return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
5248c6fd2807SJeff Garzik }
5249c6fd2807SJeff Garzik 
5250c6fd2807SJeff Garzik /**
5251c6fd2807SJeff Garzik  *	sata_scr_read - read SCR register of the specified port
5252936fd732STejun Heo  *	@link: ATA link to read SCR for
5253c6fd2807SJeff Garzik  *	@reg: SCR to read
5254c6fd2807SJeff Garzik  *	@val: Place to store read value
5255c6fd2807SJeff Garzik  *
5256936fd732STejun Heo  *	Read SCR register @reg of @link into *@val.  This function is
5257633273a3STejun Heo  *	guaranteed to succeed if @link is ap->link, the cable type of
5258633273a3STejun Heo  *	the port is SATA and the port implements ->scr_read.
5259c6fd2807SJeff Garzik  *
5260c6fd2807SJeff Garzik  *	LOCKING:
5261633273a3STejun Heo  *	None if @link is ap->link.  Kernel thread context otherwise.
5262c6fd2807SJeff Garzik  *
5263c6fd2807SJeff Garzik  *	RETURNS:
5264c6fd2807SJeff Garzik  *	0 on success, negative errno on failure.
5265c6fd2807SJeff Garzik  */
5266936fd732STejun Heo int sata_scr_read(struct ata_link *link, int reg, u32 *val)
5267c6fd2807SJeff Garzik {
5268633273a3STejun Heo 	if (ata_is_host_link(link)) {
5269936fd732STejun Heo 		if (sata_scr_valid(link))
527082ef04fbSTejun Heo 			return link->ap->ops->scr_read(link, reg, val);
5271c6fd2807SJeff Garzik 		return -EOPNOTSUPP;
5272c6fd2807SJeff Garzik 	}
5273c6fd2807SJeff Garzik 
5274633273a3STejun Heo 	return sata_pmp_scr_read(link, reg, val);
5275633273a3STejun Heo }
5276633273a3STejun Heo 
5277c6fd2807SJeff Garzik /**
5278c6fd2807SJeff Garzik  *	sata_scr_write - write SCR register of the specified port
5279936fd732STejun Heo  *	@link: ATA link to write SCR for
5280c6fd2807SJeff Garzik  *	@reg: SCR to write
5281c6fd2807SJeff Garzik  *	@val: value to write
5282c6fd2807SJeff Garzik  *
5283936fd732STejun Heo  *	Write @val to SCR register @reg of @link.  This function is
5284633273a3STejun Heo  *	guaranteed to succeed if @link is ap->link, the cable type of
5285633273a3STejun Heo  *	the port is SATA and the port implements ->scr_read.
5286c6fd2807SJeff Garzik  *
5287c6fd2807SJeff Garzik  *	LOCKING:
5288633273a3STejun Heo  *	None if @link is ap->link.  Kernel thread context otherwise.
5289c6fd2807SJeff Garzik  *
5290c6fd2807SJeff Garzik  *	RETURNS:
5291c6fd2807SJeff Garzik  *	0 on success, negative errno on failure.
5292c6fd2807SJeff Garzik  */
5293936fd732STejun Heo int sata_scr_write(struct ata_link *link, int reg, u32 val)
5294c6fd2807SJeff Garzik {
5295633273a3STejun Heo 	if (ata_is_host_link(link)) {
5296936fd732STejun Heo 		if (sata_scr_valid(link))
529782ef04fbSTejun Heo 			return link->ap->ops->scr_write(link, reg, val);
5298c6fd2807SJeff Garzik 		return -EOPNOTSUPP;
5299c6fd2807SJeff Garzik 	}
5300c6fd2807SJeff Garzik 
5301633273a3STejun Heo 	return sata_pmp_scr_write(link, reg, val);
5302633273a3STejun Heo }
5303633273a3STejun Heo 
5304c6fd2807SJeff Garzik /**
5305c6fd2807SJeff Garzik  *	sata_scr_write_flush - write SCR register of the specified port and flush
5306936fd732STejun Heo  *	@link: ATA link to write SCR for
5307c6fd2807SJeff Garzik  *	@reg: SCR to write
5308c6fd2807SJeff Garzik  *	@val: value to write
5309c6fd2807SJeff Garzik  *
5310c6fd2807SJeff Garzik  *	This function is identical to sata_scr_write() except that this
5311c6fd2807SJeff Garzik  *	function performs flush after writing to the register.
5312c6fd2807SJeff Garzik  *
5313c6fd2807SJeff Garzik  *	LOCKING:
5314633273a3STejun Heo  *	None if @link is ap->link.  Kernel thread context otherwise.
5315c6fd2807SJeff Garzik  *
5316c6fd2807SJeff Garzik  *	RETURNS:
5317c6fd2807SJeff Garzik  *	0 on success, negative errno on failure.
5318c6fd2807SJeff Garzik  */
5319936fd732STejun Heo int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
5320c6fd2807SJeff Garzik {
5321633273a3STejun Heo 	if (ata_is_host_link(link)) {
5322da3dbb17STejun Heo 		int rc;
5323da3dbb17STejun Heo 
5324936fd732STejun Heo 		if (sata_scr_valid(link)) {
532582ef04fbSTejun Heo 			rc = link->ap->ops->scr_write(link, reg, val);
5326da3dbb17STejun Heo 			if (rc == 0)
532782ef04fbSTejun Heo 				rc = link->ap->ops->scr_read(link, reg, &val);
5328da3dbb17STejun Heo 			return rc;
5329c6fd2807SJeff Garzik 		}
5330c6fd2807SJeff Garzik 		return -EOPNOTSUPP;
5331c6fd2807SJeff Garzik 	}
5332c6fd2807SJeff Garzik 
5333633273a3STejun Heo 	return sata_pmp_scr_write(link, reg, val);
5334633273a3STejun Heo }
5335633273a3STejun Heo 
5336c6fd2807SJeff Garzik /**
5337b1c72916STejun Heo  *	ata_phys_link_online - test whether the given link is online
5338936fd732STejun Heo  *	@link: ATA link to test
5339c6fd2807SJeff Garzik  *
5340936fd732STejun Heo  *	Test whether @link is online.  Note that this function returns
5341936fd732STejun Heo  *	0 if online status of @link cannot be obtained, so
5342936fd732STejun Heo  *	ata_link_online(link) != !ata_link_offline(link).
5343c6fd2807SJeff Garzik  *
5344c6fd2807SJeff Garzik  *	LOCKING:
5345c6fd2807SJeff Garzik  *	None.
5346c6fd2807SJeff Garzik  *
5347c6fd2807SJeff Garzik  *	RETURNS:
5348b5b3fa38STejun Heo  *	True if the port online status is available and online.
5349c6fd2807SJeff Garzik  */
5350b1c72916STejun Heo bool ata_phys_link_online(struct ata_link *link)
5351c6fd2807SJeff Garzik {
5352c6fd2807SJeff Garzik 	u32 sstatus;
5353c6fd2807SJeff Garzik 
5354936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
53559913ff8aSTejun Heo 	    ata_sstatus_online(sstatus))
5356b5b3fa38STejun Heo 		return true;
5357b5b3fa38STejun Heo 	return false;
5358c6fd2807SJeff Garzik }
5359c6fd2807SJeff Garzik 
5360c6fd2807SJeff Garzik /**
5361b1c72916STejun Heo  *	ata_phys_link_offline - test whether the given link is offline
5362936fd732STejun Heo  *	@link: ATA link to test
5363c6fd2807SJeff Garzik  *
5364936fd732STejun Heo  *	Test whether @link is offline.  Note that this function
5365936fd732STejun Heo  *	returns 0 if offline status of @link cannot be obtained, so
5366936fd732STejun Heo  *	ata_link_online(link) != !ata_link_offline(link).
5367c6fd2807SJeff Garzik  *
5368c6fd2807SJeff Garzik  *	LOCKING:
5369c6fd2807SJeff Garzik  *	None.
5370c6fd2807SJeff Garzik  *
5371c6fd2807SJeff Garzik  *	RETURNS:
5372b5b3fa38STejun Heo  *	True if the port offline status is available and offline.
5373c6fd2807SJeff Garzik  */
5374b1c72916STejun Heo bool ata_phys_link_offline(struct ata_link *link)
5375c6fd2807SJeff Garzik {
5376c6fd2807SJeff Garzik 	u32 sstatus;
5377c6fd2807SJeff Garzik 
5378936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
53799913ff8aSTejun Heo 	    !ata_sstatus_online(sstatus))
5380b5b3fa38STejun Heo 		return true;
5381b5b3fa38STejun Heo 	return false;
5382c6fd2807SJeff Garzik }
5383c6fd2807SJeff Garzik 
5384b1c72916STejun Heo /**
5385b1c72916STejun Heo  *	ata_link_online - test whether the given link is online
5386b1c72916STejun Heo  *	@link: ATA link to test
5387b1c72916STejun Heo  *
5388b1c72916STejun Heo  *	Test whether @link is online.  This is identical to
5389b1c72916STejun Heo  *	ata_phys_link_online() when there's no slave link.  When
5390b1c72916STejun Heo  *	there's a slave link, this function should only be called on
5391b1c72916STejun Heo  *	the master link and will return true if any of M/S links is
5392b1c72916STejun Heo  *	online.
5393b1c72916STejun Heo  *
5394b1c72916STejun Heo  *	LOCKING:
5395b1c72916STejun Heo  *	None.
5396b1c72916STejun Heo  *
5397b1c72916STejun Heo  *	RETURNS:
5398b1c72916STejun Heo  *	True if the port online status is available and online.
5399b1c72916STejun Heo  */
5400b1c72916STejun Heo bool ata_link_online(struct ata_link *link)
5401b1c72916STejun Heo {
5402b1c72916STejun Heo 	struct ata_link *slave = link->ap->slave_link;
5403b1c72916STejun Heo 
5404b1c72916STejun Heo 	WARN_ON(link == slave);	/* shouldn't be called on slave link */
5405b1c72916STejun Heo 
5406b1c72916STejun Heo 	return ata_phys_link_online(link) ||
5407b1c72916STejun Heo 		(slave && ata_phys_link_online(slave));
5408b1c72916STejun Heo }
5409b1c72916STejun Heo 
5410b1c72916STejun Heo /**
5411b1c72916STejun Heo  *	ata_link_offline - test whether the given link is offline
5412b1c72916STejun Heo  *	@link: ATA link to test
5413b1c72916STejun Heo  *
5414b1c72916STejun Heo  *	Test whether @link is offline.  This is identical to
5415b1c72916STejun Heo  *	ata_phys_link_offline() when there's no slave link.  When
5416b1c72916STejun Heo  *	there's a slave link, this function should only be called on
5417b1c72916STejun Heo  *	the master link and will return true if both M/S links are
5418b1c72916STejun Heo  *	offline.
5419b1c72916STejun Heo  *
5420b1c72916STejun Heo  *	LOCKING:
5421b1c72916STejun Heo  *	None.
5422b1c72916STejun Heo  *
5423b1c72916STejun Heo  *	RETURNS:
5424b1c72916STejun Heo  *	True if the port offline status is available and offline.
5425b1c72916STejun Heo  */
5426b1c72916STejun Heo bool ata_link_offline(struct ata_link *link)
5427b1c72916STejun Heo {
5428b1c72916STejun Heo 	struct ata_link *slave = link->ap->slave_link;
5429b1c72916STejun Heo 
5430b1c72916STejun Heo 	WARN_ON(link == slave);	/* shouldn't be called on slave link */
5431b1c72916STejun Heo 
5432b1c72916STejun Heo 	return ata_phys_link_offline(link) &&
5433b1c72916STejun Heo 		(!slave || ata_phys_link_offline(slave));
5434b1c72916STejun Heo }
5435b1c72916STejun Heo 
54366ffa01d8STejun Heo #ifdef CONFIG_PM
5437cca3974eSJeff Garzik static int ata_host_request_pm(struct ata_host *host, pm_message_t mesg,
5438cca3974eSJeff Garzik 			       unsigned int action, unsigned int ehi_flags,
5439cca3974eSJeff Garzik 			       int wait)
5440c6fd2807SJeff Garzik {
5441c6fd2807SJeff Garzik 	unsigned long flags;
5442c6fd2807SJeff Garzik 	int i, rc;
5443c6fd2807SJeff Garzik 
5444cca3974eSJeff Garzik 	for (i = 0; i < host->n_ports; i++) {
5445cca3974eSJeff Garzik 		struct ata_port *ap = host->ports[i];
5446e3667ebfSTejun Heo 		struct ata_link *link;
5447c6fd2807SJeff Garzik 
5448c6fd2807SJeff Garzik 		/* Previous resume operation might still be in
5449c6fd2807SJeff Garzik 		 * progress.  Wait for PM_PENDING to clear.
5450c6fd2807SJeff Garzik 		 */
5451c6fd2807SJeff Garzik 		if (ap->pflags & ATA_PFLAG_PM_PENDING) {
5452c6fd2807SJeff Garzik 			ata_port_wait_eh(ap);
5453c6fd2807SJeff Garzik 			WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5454c6fd2807SJeff Garzik 		}
5455c6fd2807SJeff Garzik 
5456c6fd2807SJeff Garzik 		/* request PM ops to EH */
5457c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
5458c6fd2807SJeff Garzik 
5459c6fd2807SJeff Garzik 		ap->pm_mesg = mesg;
5460c6fd2807SJeff Garzik 		if (wait) {
5461c6fd2807SJeff Garzik 			rc = 0;
5462c6fd2807SJeff Garzik 			ap->pm_result = &rc;
5463c6fd2807SJeff Garzik 		}
5464c6fd2807SJeff Garzik 
5465c6fd2807SJeff Garzik 		ap->pflags |= ATA_PFLAG_PM_PENDING;
54661eca4365STejun Heo 		ata_for_each_link(link, ap, HOST_FIRST) {
5467e3667ebfSTejun Heo 			link->eh_info.action |= action;
5468e3667ebfSTejun Heo 			link->eh_info.flags |= ehi_flags;
5469e3667ebfSTejun Heo 		}
5470c6fd2807SJeff Garzik 
5471c6fd2807SJeff Garzik 		ata_port_schedule_eh(ap);
5472c6fd2807SJeff Garzik 
5473c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
5474c6fd2807SJeff Garzik 
5475c6fd2807SJeff Garzik 		/* wait and check result */
5476c6fd2807SJeff Garzik 		if (wait) {
5477c6fd2807SJeff Garzik 			ata_port_wait_eh(ap);
5478c6fd2807SJeff Garzik 			WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5479c6fd2807SJeff Garzik 			if (rc)
5480c6fd2807SJeff Garzik 				return rc;
5481c6fd2807SJeff Garzik 		}
5482c6fd2807SJeff Garzik 	}
5483c6fd2807SJeff Garzik 
5484c6fd2807SJeff Garzik 	return 0;
5485c6fd2807SJeff Garzik }
5486c6fd2807SJeff Garzik 
5487c6fd2807SJeff Garzik /**
5488cca3974eSJeff Garzik  *	ata_host_suspend - suspend host
5489cca3974eSJeff Garzik  *	@host: host to suspend
5490c6fd2807SJeff Garzik  *	@mesg: PM message
5491c6fd2807SJeff Garzik  *
5492cca3974eSJeff Garzik  *	Suspend @host.  Actual operation is performed by EH.  This
5493c6fd2807SJeff Garzik  *	function requests EH to perform PM operations and waits for EH
5494c6fd2807SJeff Garzik  *	to finish.
5495c6fd2807SJeff Garzik  *
5496c6fd2807SJeff Garzik  *	LOCKING:
5497c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
5498c6fd2807SJeff Garzik  *
5499c6fd2807SJeff Garzik  *	RETURNS:
5500c6fd2807SJeff Garzik  *	0 on success, -errno on failure.
5501c6fd2807SJeff Garzik  */
5502cca3974eSJeff Garzik int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
5503c6fd2807SJeff Garzik {
55049666f400STejun Heo 	int rc;
5505c6fd2807SJeff Garzik 
5506ca77329fSKristen Carlson Accardi 	/*
5507ca77329fSKristen Carlson Accardi 	 * disable link pm on all ports before requesting
5508ca77329fSKristen Carlson Accardi 	 * any pm activity
5509ca77329fSKristen Carlson Accardi 	 */
5510ca77329fSKristen Carlson Accardi 	ata_lpm_enable(host);
5511ca77329fSKristen Carlson Accardi 
5512cca3974eSJeff Garzik 	rc = ata_host_request_pm(host, mesg, 0, ATA_EHI_QUIET, 1);
551372ad6ec4SJeff Garzik 	if (rc == 0)
551472ad6ec4SJeff Garzik 		host->dev->power.power_state = mesg;
5515c6fd2807SJeff Garzik 	return rc;
5516c6fd2807SJeff Garzik }
5517c6fd2807SJeff Garzik 
5518c6fd2807SJeff Garzik /**
5519cca3974eSJeff Garzik  *	ata_host_resume - resume host
5520cca3974eSJeff Garzik  *	@host: host to resume
5521c6fd2807SJeff Garzik  *
5522cca3974eSJeff Garzik  *	Resume @host.  Actual operation is performed by EH.  This
5523c6fd2807SJeff Garzik  *	function requests EH to perform PM operations and returns.
5524c6fd2807SJeff Garzik  *	Note that all resume operations are performed parallely.
5525c6fd2807SJeff Garzik  *
5526c6fd2807SJeff Garzik  *	LOCKING:
5527c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
5528c6fd2807SJeff Garzik  */
5529cca3974eSJeff Garzik void ata_host_resume(struct ata_host *host)
5530c6fd2807SJeff Garzik {
5531cf480626STejun Heo 	ata_host_request_pm(host, PMSG_ON, ATA_EH_RESET,
5532c6fd2807SJeff Garzik 			    ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET, 0);
553372ad6ec4SJeff Garzik 	host->dev->power.power_state = PMSG_ON;
5534ca77329fSKristen Carlson Accardi 
5535ca77329fSKristen Carlson Accardi 	/* reenable link pm */
5536ca77329fSKristen Carlson Accardi 	ata_lpm_disable(host);
5537c6fd2807SJeff Garzik }
55386ffa01d8STejun Heo #endif
5539c6fd2807SJeff Garzik 
5540c6fd2807SJeff Garzik /**
5541c6fd2807SJeff Garzik  *	ata_port_start - Set port up for dma.
5542c6fd2807SJeff Garzik  *	@ap: Port to initialize
5543c6fd2807SJeff Garzik  *
5544c6fd2807SJeff Garzik  *	Called just after data structures for each port are
5545c6fd2807SJeff Garzik  *	initialized.  Allocates space for PRD table.
5546c6fd2807SJeff Garzik  *
5547c6fd2807SJeff Garzik  *	May be used as the port_start() entry in ata_port_operations.
5548c6fd2807SJeff Garzik  *
5549c6fd2807SJeff Garzik  *	LOCKING:
5550c6fd2807SJeff Garzik  *	Inherited from caller.
5551c6fd2807SJeff Garzik  */
5552c6fd2807SJeff Garzik int ata_port_start(struct ata_port *ap)
5553c6fd2807SJeff Garzik {
5554c6fd2807SJeff Garzik 	struct device *dev = ap->dev;
5555c6fd2807SJeff Garzik 
5556f0d36efdSTejun Heo 	ap->prd = dmam_alloc_coherent(dev, ATA_PRD_TBL_SZ, &ap->prd_dma,
5557f0d36efdSTejun Heo 				      GFP_KERNEL);
5558c6fd2807SJeff Garzik 	if (!ap->prd)
5559c6fd2807SJeff Garzik 		return -ENOMEM;
5560c6fd2807SJeff Garzik 
5561c6fd2807SJeff Garzik 	return 0;
5562c6fd2807SJeff Garzik }
5563c6fd2807SJeff Garzik 
5564c6fd2807SJeff Garzik /**
5565c6fd2807SJeff Garzik  *	ata_dev_init - Initialize an ata_device structure
5566c6fd2807SJeff Garzik  *	@dev: Device structure to initialize
5567c6fd2807SJeff Garzik  *
5568c6fd2807SJeff Garzik  *	Initialize @dev in preparation for probing.
5569c6fd2807SJeff Garzik  *
5570c6fd2807SJeff Garzik  *	LOCKING:
5571c6fd2807SJeff Garzik  *	Inherited from caller.
5572c6fd2807SJeff Garzik  */
5573c6fd2807SJeff Garzik void ata_dev_init(struct ata_device *dev)
5574c6fd2807SJeff Garzik {
5575b1c72916STejun Heo 	struct ata_link *link = ata_dev_phys_link(dev);
55769af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
5577c6fd2807SJeff Garzik 	unsigned long flags;
5578c6fd2807SJeff Garzik 
5579b1c72916STejun Heo 	/* SATA spd limit is bound to the attached device, reset together */
55809af5c9c9STejun Heo 	link->sata_spd_limit = link->hw_sata_spd_limit;
55819af5c9c9STejun Heo 	link->sata_spd = 0;
5582c6fd2807SJeff Garzik 
5583c6fd2807SJeff Garzik 	/* High bits of dev->flags are used to record warm plug
5584c6fd2807SJeff Garzik 	 * requests which occur asynchronously.  Synchronize using
5585cca3974eSJeff Garzik 	 * host lock.
5586c6fd2807SJeff Garzik 	 */
5587c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
5588c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_INIT_MASK;
55893dcc323fSTejun Heo 	dev->horkage = 0;
5590c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
5591c6fd2807SJeff Garzik 
559299cf610aSTejun Heo 	memset((void *)dev + ATA_DEVICE_CLEAR_BEGIN, 0,
559399cf610aSTejun Heo 	       ATA_DEVICE_CLEAR_END - ATA_DEVICE_CLEAR_BEGIN);
5594c6fd2807SJeff Garzik 	dev->pio_mask = UINT_MAX;
5595c6fd2807SJeff Garzik 	dev->mwdma_mask = UINT_MAX;
5596c6fd2807SJeff Garzik 	dev->udma_mask = UINT_MAX;
5597c6fd2807SJeff Garzik }
5598c6fd2807SJeff Garzik 
5599c6fd2807SJeff Garzik /**
56004fb37a25STejun Heo  *	ata_link_init - Initialize an ata_link structure
56014fb37a25STejun Heo  *	@ap: ATA port link is attached to
56024fb37a25STejun Heo  *	@link: Link structure to initialize
56038989805dSTejun Heo  *	@pmp: Port multiplier port number
56044fb37a25STejun Heo  *
56054fb37a25STejun Heo  *	Initialize @link.
56064fb37a25STejun Heo  *
56074fb37a25STejun Heo  *	LOCKING:
56084fb37a25STejun Heo  *	Kernel thread context (may sleep)
56094fb37a25STejun Heo  */
5610fb7fd614STejun Heo void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
56114fb37a25STejun Heo {
56124fb37a25STejun Heo 	int i;
56134fb37a25STejun Heo 
56144fb37a25STejun Heo 	/* clear everything except for devices */
56154fb37a25STejun Heo 	memset(link, 0, offsetof(struct ata_link, device[0]));
56164fb37a25STejun Heo 
56174fb37a25STejun Heo 	link->ap = ap;
56188989805dSTejun Heo 	link->pmp = pmp;
56194fb37a25STejun Heo 	link->active_tag = ATA_TAG_POISON;
56204fb37a25STejun Heo 	link->hw_sata_spd_limit = UINT_MAX;
56214fb37a25STejun Heo 
56224fb37a25STejun Heo 	/* can't use iterator, ap isn't initialized yet */
56234fb37a25STejun Heo 	for (i = 0; i < ATA_MAX_DEVICES; i++) {
56244fb37a25STejun Heo 		struct ata_device *dev = &link->device[i];
56254fb37a25STejun Heo 
56264fb37a25STejun Heo 		dev->link = link;
56274fb37a25STejun Heo 		dev->devno = dev - link->device;
5628110f66d2STejun Heo #ifdef CONFIG_ATA_ACPI
5629110f66d2STejun Heo 		dev->gtf_filter = ata_acpi_gtf_filter;
5630110f66d2STejun Heo #endif
56314fb37a25STejun Heo 		ata_dev_init(dev);
56324fb37a25STejun Heo 	}
56334fb37a25STejun Heo }
56344fb37a25STejun Heo 
56354fb37a25STejun Heo /**
56364fb37a25STejun Heo  *	sata_link_init_spd - Initialize link->sata_spd_limit
56374fb37a25STejun Heo  *	@link: Link to configure sata_spd_limit for
56384fb37a25STejun Heo  *
56394fb37a25STejun Heo  *	Initialize @link->[hw_]sata_spd_limit to the currently
56404fb37a25STejun Heo  *	configured value.
56414fb37a25STejun Heo  *
56424fb37a25STejun Heo  *	LOCKING:
56434fb37a25STejun Heo  *	Kernel thread context (may sleep).
56444fb37a25STejun Heo  *
56454fb37a25STejun Heo  *	RETURNS:
56464fb37a25STejun Heo  *	0 on success, -errno on failure.
56474fb37a25STejun Heo  */
5648fb7fd614STejun Heo int sata_link_init_spd(struct ata_link *link)
56494fb37a25STejun Heo {
565033267325STejun Heo 	u8 spd;
56514fb37a25STejun Heo 	int rc;
56524fb37a25STejun Heo 
5653d127ea7bSTejun Heo 	rc = sata_scr_read(link, SCR_CONTROL, &link->saved_scontrol);
56544fb37a25STejun Heo 	if (rc)
56554fb37a25STejun Heo 		return rc;
56564fb37a25STejun Heo 
5657d127ea7bSTejun Heo 	spd = (link->saved_scontrol >> 4) & 0xf;
56584fb37a25STejun Heo 	if (spd)
56594fb37a25STejun Heo 		link->hw_sata_spd_limit &= (1 << spd) - 1;
56604fb37a25STejun Heo 
566105944bdfSTejun Heo 	ata_force_link_limits(link);
566233267325STejun Heo 
56634fb37a25STejun Heo 	link->sata_spd_limit = link->hw_sata_spd_limit;
56644fb37a25STejun Heo 
56654fb37a25STejun Heo 	return 0;
56664fb37a25STejun Heo }
56674fb37a25STejun Heo 
56684fb37a25STejun Heo /**
5669f3187195STejun Heo  *	ata_port_alloc - allocate and initialize basic ATA port resources
5670f3187195STejun Heo  *	@host: ATA host this allocated port belongs to
5671c6fd2807SJeff Garzik  *
5672f3187195STejun Heo  *	Allocate and initialize basic ATA port resources.
5673f3187195STejun Heo  *
5674f3187195STejun Heo  *	RETURNS:
5675f3187195STejun Heo  *	Allocate ATA port on success, NULL on failure.
5676c6fd2807SJeff Garzik  *
5677c6fd2807SJeff Garzik  *	LOCKING:
5678f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5679c6fd2807SJeff Garzik  */
5680f3187195STejun Heo struct ata_port *ata_port_alloc(struct ata_host *host)
5681c6fd2807SJeff Garzik {
5682f3187195STejun Heo 	struct ata_port *ap;
5683c6fd2807SJeff Garzik 
5684f3187195STejun Heo 	DPRINTK("ENTER\n");
5685f3187195STejun Heo 
5686f3187195STejun Heo 	ap = kzalloc(sizeof(*ap), GFP_KERNEL);
5687f3187195STejun Heo 	if (!ap)
5688f3187195STejun Heo 		return NULL;
5689f3187195STejun Heo 
5690f4d6d004STejun Heo 	ap->pflags |= ATA_PFLAG_INITIALIZING;
5691cca3974eSJeff Garzik 	ap->lock = &host->lock;
5692c6fd2807SJeff Garzik 	ap->flags = ATA_FLAG_DISABLED;
5693f3187195STejun Heo 	ap->print_id = -1;
5694c6fd2807SJeff Garzik 	ap->ctl = ATA_DEVCTL_OBS;
5695cca3974eSJeff Garzik 	ap->host = host;
5696f3187195STejun Heo 	ap->dev = host->dev;
5697c6fd2807SJeff Garzik 	ap->last_ctl = 0xFF;
5698c6fd2807SJeff Garzik 
5699c6fd2807SJeff Garzik #if defined(ATA_VERBOSE_DEBUG)
5700c6fd2807SJeff Garzik 	/* turn on all debugging levels */
5701c6fd2807SJeff Garzik 	ap->msg_enable = 0x00FF;
5702c6fd2807SJeff Garzik #elif defined(ATA_DEBUG)
5703c6fd2807SJeff Garzik 	ap->msg_enable = ATA_MSG_DRV | ATA_MSG_INFO | ATA_MSG_CTL | ATA_MSG_WARN | ATA_MSG_ERR;
5704c6fd2807SJeff Garzik #else
5705c6fd2807SJeff Garzik 	ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
5706c6fd2807SJeff Garzik #endif
5707c6fd2807SJeff Garzik 
5708127102aeSTejun Heo #ifdef CONFIG_ATA_SFF
5709442eacc3SJeff Garzik 	INIT_DELAYED_WORK(&ap->port_task, ata_pio_task);
5710f667fdbbSTejun Heo #else
5711f667fdbbSTejun Heo 	INIT_DELAYED_WORK(&ap->port_task, NULL);
5712127102aeSTejun Heo #endif
571365f27f38SDavid Howells 	INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
571465f27f38SDavid Howells 	INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
5715c6fd2807SJeff Garzik 	INIT_LIST_HEAD(&ap->eh_done_q);
5716c6fd2807SJeff Garzik 	init_waitqueue_head(&ap->eh_wait_q);
571745fabbb7SElias Oltmanns 	init_completion(&ap->park_req_pending);
57185ddf24c5STejun Heo 	init_timer_deferrable(&ap->fastdrain_timer);
57195ddf24c5STejun Heo 	ap->fastdrain_timer.function = ata_eh_fastdrain_timerfn;
57205ddf24c5STejun Heo 	ap->fastdrain_timer.data = (unsigned long)ap;
5721c6fd2807SJeff Garzik 
5722c6fd2807SJeff Garzik 	ap->cbl = ATA_CBL_NONE;
5723c6fd2807SJeff Garzik 
57248989805dSTejun Heo 	ata_link_init(ap, &ap->link, 0);
5725c6fd2807SJeff Garzik 
5726c6fd2807SJeff Garzik #ifdef ATA_IRQ_TRAP
5727c6fd2807SJeff Garzik 	ap->stats.unhandled_irq = 1;
5728c6fd2807SJeff Garzik 	ap->stats.idle_irq = 1;
5729c6fd2807SJeff Garzik #endif
5730c6fd2807SJeff Garzik 	return ap;
5731c6fd2807SJeff Garzik }
5732c6fd2807SJeff Garzik 
5733f0d36efdSTejun Heo static void ata_host_release(struct device *gendev, void *res)
5734f0d36efdSTejun Heo {
5735f0d36efdSTejun Heo 	struct ata_host *host = dev_get_drvdata(gendev);
5736f0d36efdSTejun Heo 	int i;
5737f0d36efdSTejun Heo 
5738f0d36efdSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
5739f0d36efdSTejun Heo 		struct ata_port *ap = host->ports[i];
5740f0d36efdSTejun Heo 
5741ecef7253STejun Heo 		if (!ap)
5742ecef7253STejun Heo 			continue;
5743ecef7253STejun Heo 
57444911487aSTejun Heo 		if (ap->scsi_host)
57451aa506e4STejun Heo 			scsi_host_put(ap->scsi_host);
57461aa506e4STejun Heo 
5747633273a3STejun Heo 		kfree(ap->pmp_link);
5748b1c72916STejun Heo 		kfree(ap->slave_link);
57494911487aSTejun Heo 		kfree(ap);
57501aa506e4STejun Heo 		host->ports[i] = NULL;
57511aa506e4STejun Heo 	}
57521aa506e4STejun Heo 
57531aa56ccaSTejun Heo 	dev_set_drvdata(gendev, NULL);
5754f0d36efdSTejun Heo }
5755f0d36efdSTejun Heo 
5756c6fd2807SJeff Garzik /**
5757f3187195STejun Heo  *	ata_host_alloc - allocate and init basic ATA host resources
5758f3187195STejun Heo  *	@dev: generic device this host is associated with
5759f3187195STejun Heo  *	@max_ports: maximum number of ATA ports associated with this host
5760f3187195STejun Heo  *
5761f3187195STejun Heo  *	Allocate and initialize basic ATA host resources.  LLD calls
5762f3187195STejun Heo  *	this function to allocate a host, initializes it fully and
5763f3187195STejun Heo  *	attaches it using ata_host_register().
5764f3187195STejun Heo  *
5765f3187195STejun Heo  *	@max_ports ports are allocated and host->n_ports is
5766f3187195STejun Heo  *	initialized to @max_ports.  The caller is allowed to decrease
5767f3187195STejun Heo  *	host->n_ports before calling ata_host_register().  The unused
5768f3187195STejun Heo  *	ports will be automatically freed on registration.
5769f3187195STejun Heo  *
5770f3187195STejun Heo  *	RETURNS:
5771f3187195STejun Heo  *	Allocate ATA host on success, NULL on failure.
5772f3187195STejun Heo  *
5773f3187195STejun Heo  *	LOCKING:
5774f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5775f3187195STejun Heo  */
5776f3187195STejun Heo struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
5777f3187195STejun Heo {
5778f3187195STejun Heo 	struct ata_host *host;
5779f3187195STejun Heo 	size_t sz;
5780f3187195STejun Heo 	int i;
5781f3187195STejun Heo 
5782f3187195STejun Heo 	DPRINTK("ENTER\n");
5783f3187195STejun Heo 
5784f3187195STejun Heo 	if (!devres_open_group(dev, NULL, GFP_KERNEL))
5785f3187195STejun Heo 		return NULL;
5786f3187195STejun Heo 
5787f3187195STejun Heo 	/* alloc a container for our list of ATA ports (buses) */
5788f3187195STejun Heo 	sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
5789f3187195STejun Heo 	/* alloc a container for our list of ATA ports (buses) */
5790f3187195STejun Heo 	host = devres_alloc(ata_host_release, sz, GFP_KERNEL);
5791f3187195STejun Heo 	if (!host)
5792f3187195STejun Heo 		goto err_out;
5793f3187195STejun Heo 
5794f3187195STejun Heo 	devres_add(dev, host);
5795f3187195STejun Heo 	dev_set_drvdata(dev, host);
5796f3187195STejun Heo 
5797f3187195STejun Heo 	spin_lock_init(&host->lock);
5798f3187195STejun Heo 	host->dev = dev;
5799f3187195STejun Heo 	host->n_ports = max_ports;
5800f3187195STejun Heo 
5801f3187195STejun Heo 	/* allocate ports bound to this host */
5802f3187195STejun Heo 	for (i = 0; i < max_ports; i++) {
5803f3187195STejun Heo 		struct ata_port *ap;
5804f3187195STejun Heo 
5805f3187195STejun Heo 		ap = ata_port_alloc(host);
5806f3187195STejun Heo 		if (!ap)
5807f3187195STejun Heo 			goto err_out;
5808f3187195STejun Heo 
5809f3187195STejun Heo 		ap->port_no = i;
5810f3187195STejun Heo 		host->ports[i] = ap;
5811f3187195STejun Heo 	}
5812f3187195STejun Heo 
5813f3187195STejun Heo 	devres_remove_group(dev, NULL);
5814f3187195STejun Heo 	return host;
5815f3187195STejun Heo 
5816f3187195STejun Heo  err_out:
5817f3187195STejun Heo 	devres_release_group(dev, NULL);
5818f3187195STejun Heo 	return NULL;
5819f3187195STejun Heo }
5820f3187195STejun Heo 
5821f3187195STejun Heo /**
5822f5cda257STejun Heo  *	ata_host_alloc_pinfo - alloc host and init with port_info array
5823f5cda257STejun Heo  *	@dev: generic device this host is associated with
5824f5cda257STejun Heo  *	@ppi: array of ATA port_info to initialize host with
5825f5cda257STejun Heo  *	@n_ports: number of ATA ports attached to this host
5826f5cda257STejun Heo  *
5827f5cda257STejun Heo  *	Allocate ATA host and initialize with info from @ppi.  If NULL
5828f5cda257STejun Heo  *	terminated, @ppi may contain fewer entries than @n_ports.  The
5829f5cda257STejun Heo  *	last entry will be used for the remaining ports.
5830f5cda257STejun Heo  *
5831f5cda257STejun Heo  *	RETURNS:
5832f5cda257STejun Heo  *	Allocate ATA host on success, NULL on failure.
5833f5cda257STejun Heo  *
5834f5cda257STejun Heo  *	LOCKING:
5835f5cda257STejun Heo  *	Inherited from calling layer (may sleep).
5836f5cda257STejun Heo  */
5837f5cda257STejun Heo struct ata_host *ata_host_alloc_pinfo(struct device *dev,
5838f5cda257STejun Heo 				      const struct ata_port_info * const * ppi,
5839f5cda257STejun Heo 				      int n_ports)
5840f5cda257STejun Heo {
5841f5cda257STejun Heo 	const struct ata_port_info *pi;
5842f5cda257STejun Heo 	struct ata_host *host;
5843f5cda257STejun Heo 	int i, j;
5844f5cda257STejun Heo 
5845f5cda257STejun Heo 	host = ata_host_alloc(dev, n_ports);
5846f5cda257STejun Heo 	if (!host)
5847f5cda257STejun Heo 		return NULL;
5848f5cda257STejun Heo 
5849f5cda257STejun Heo 	for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
5850f5cda257STejun Heo 		struct ata_port *ap = host->ports[i];
5851f5cda257STejun Heo 
5852f5cda257STejun Heo 		if (ppi[j])
5853f5cda257STejun Heo 			pi = ppi[j++];
5854f5cda257STejun Heo 
5855f5cda257STejun Heo 		ap->pio_mask = pi->pio_mask;
5856f5cda257STejun Heo 		ap->mwdma_mask = pi->mwdma_mask;
5857f5cda257STejun Heo 		ap->udma_mask = pi->udma_mask;
5858f5cda257STejun Heo 		ap->flags |= pi->flags;
58590c88758bSTejun Heo 		ap->link.flags |= pi->link_flags;
5860f5cda257STejun Heo 		ap->ops = pi->port_ops;
5861f5cda257STejun Heo 
5862f5cda257STejun Heo 		if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
5863f5cda257STejun Heo 			host->ops = pi->port_ops;
5864f5cda257STejun Heo 	}
5865f5cda257STejun Heo 
5866f5cda257STejun Heo 	return host;
5867f5cda257STejun Heo }
5868f5cda257STejun Heo 
5869b1c72916STejun Heo /**
5870b1c72916STejun Heo  *	ata_slave_link_init - initialize slave link
5871b1c72916STejun Heo  *	@ap: port to initialize slave link for
5872b1c72916STejun Heo  *
5873b1c72916STejun Heo  *	Create and initialize slave link for @ap.  This enables slave
5874b1c72916STejun Heo  *	link handling on the port.
5875b1c72916STejun Heo  *
5876b1c72916STejun Heo  *	In libata, a port contains links and a link contains devices.
5877b1c72916STejun Heo  *	There is single host link but if a PMP is attached to it,
5878b1c72916STejun Heo  *	there can be multiple fan-out links.  On SATA, there's usually
5879b1c72916STejun Heo  *	a single device connected to a link but PATA and SATA
5880b1c72916STejun Heo  *	controllers emulating TF based interface can have two - master
5881b1c72916STejun Heo  *	and slave.
5882b1c72916STejun Heo  *
5883b1c72916STejun Heo  *	However, there are a few controllers which don't fit into this
5884b1c72916STejun Heo  *	abstraction too well - SATA controllers which emulate TF
5885b1c72916STejun Heo  *	interface with both master and slave devices but also have
5886b1c72916STejun Heo  *	separate SCR register sets for each device.  These controllers
5887b1c72916STejun Heo  *	need separate links for physical link handling
5888b1c72916STejun Heo  *	(e.g. onlineness, link speed) but should be treated like a
5889b1c72916STejun Heo  *	traditional M/S controller for everything else (e.g. command
5890b1c72916STejun Heo  *	issue, softreset).
5891b1c72916STejun Heo  *
5892b1c72916STejun Heo  *	slave_link is libata's way of handling this class of
5893b1c72916STejun Heo  *	controllers without impacting core layer too much.  For
5894b1c72916STejun Heo  *	anything other than physical link handling, the default host
5895b1c72916STejun Heo  *	link is used for both master and slave.  For physical link
5896b1c72916STejun Heo  *	handling, separate @ap->slave_link is used.  All dirty details
5897b1c72916STejun Heo  *	are implemented inside libata core layer.  From LLD's POV, the
5898b1c72916STejun Heo  *	only difference is that prereset, hardreset and postreset are
5899b1c72916STejun Heo  *	called once more for the slave link, so the reset sequence
5900b1c72916STejun Heo  *	looks like the following.
5901b1c72916STejun Heo  *
5902b1c72916STejun Heo  *	prereset(M) -> prereset(S) -> hardreset(M) -> hardreset(S) ->
5903b1c72916STejun Heo  *	softreset(M) -> postreset(M) -> postreset(S)
5904b1c72916STejun Heo  *
5905b1c72916STejun Heo  *	Note that softreset is called only for the master.  Softreset
5906b1c72916STejun Heo  *	resets both M/S by definition, so SRST on master should handle
5907b1c72916STejun Heo  *	both (the standard method will work just fine).
5908b1c72916STejun Heo  *
5909b1c72916STejun Heo  *	LOCKING:
5910b1c72916STejun Heo  *	Should be called before host is registered.
5911b1c72916STejun Heo  *
5912b1c72916STejun Heo  *	RETURNS:
5913b1c72916STejun Heo  *	0 on success, -errno on failure.
5914b1c72916STejun Heo  */
5915b1c72916STejun Heo int ata_slave_link_init(struct ata_port *ap)
5916b1c72916STejun Heo {
5917b1c72916STejun Heo 	struct ata_link *link;
5918b1c72916STejun Heo 
5919b1c72916STejun Heo 	WARN_ON(ap->slave_link);
5920b1c72916STejun Heo 	WARN_ON(ap->flags & ATA_FLAG_PMP);
5921b1c72916STejun Heo 
5922b1c72916STejun Heo 	link = kzalloc(sizeof(*link), GFP_KERNEL);
5923b1c72916STejun Heo 	if (!link)
5924b1c72916STejun Heo 		return -ENOMEM;
5925b1c72916STejun Heo 
5926b1c72916STejun Heo 	ata_link_init(ap, link, 1);
5927b1c72916STejun Heo 	ap->slave_link = link;
5928b1c72916STejun Heo 	return 0;
5929b1c72916STejun Heo }
5930b1c72916STejun Heo 
593132ebbc0cSTejun Heo static void ata_host_stop(struct device *gendev, void *res)
593232ebbc0cSTejun Heo {
593332ebbc0cSTejun Heo 	struct ata_host *host = dev_get_drvdata(gendev);
593432ebbc0cSTejun Heo 	int i;
593532ebbc0cSTejun Heo 
593632ebbc0cSTejun Heo 	WARN_ON(!(host->flags & ATA_HOST_STARTED));
593732ebbc0cSTejun Heo 
593832ebbc0cSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
593932ebbc0cSTejun Heo 		struct ata_port *ap = host->ports[i];
594032ebbc0cSTejun Heo 
594132ebbc0cSTejun Heo 		if (ap->ops->port_stop)
594232ebbc0cSTejun Heo 			ap->ops->port_stop(ap);
594332ebbc0cSTejun Heo 	}
594432ebbc0cSTejun Heo 
594532ebbc0cSTejun Heo 	if (host->ops->host_stop)
594632ebbc0cSTejun Heo 		host->ops->host_stop(host);
594732ebbc0cSTejun Heo }
594832ebbc0cSTejun Heo 
5949f5cda257STejun Heo /**
5950029cfd6bSTejun Heo  *	ata_finalize_port_ops - finalize ata_port_operations
5951029cfd6bSTejun Heo  *	@ops: ata_port_operations to finalize
5952029cfd6bSTejun Heo  *
5953029cfd6bSTejun Heo  *	An ata_port_operations can inherit from another ops and that
5954029cfd6bSTejun Heo  *	ops can again inherit from another.  This can go on as many
5955029cfd6bSTejun Heo  *	times as necessary as long as there is no loop in the
5956029cfd6bSTejun Heo  *	inheritance chain.
5957029cfd6bSTejun Heo  *
5958029cfd6bSTejun Heo  *	Ops tables are finalized when the host is started.  NULL or
5959029cfd6bSTejun Heo  *	unspecified entries are inherited from the closet ancestor
5960029cfd6bSTejun Heo  *	which has the method and the entry is populated with it.
5961029cfd6bSTejun Heo  *	After finalization, the ops table directly points to all the
5962029cfd6bSTejun Heo  *	methods and ->inherits is no longer necessary and cleared.
5963029cfd6bSTejun Heo  *
5964029cfd6bSTejun Heo  *	Using ATA_OP_NULL, inheriting ops can force a method to NULL.
5965029cfd6bSTejun Heo  *
5966029cfd6bSTejun Heo  *	LOCKING:
5967029cfd6bSTejun Heo  *	None.
5968029cfd6bSTejun Heo  */
5969029cfd6bSTejun Heo static void ata_finalize_port_ops(struct ata_port_operations *ops)
5970029cfd6bSTejun Heo {
59712da67659SPradeep Singh Rautela 	static DEFINE_SPINLOCK(lock);
5972029cfd6bSTejun Heo 	const struct ata_port_operations *cur;
5973029cfd6bSTejun Heo 	void **begin = (void **)ops;
5974029cfd6bSTejun Heo 	void **end = (void **)&ops->inherits;
5975029cfd6bSTejun Heo 	void **pp;
5976029cfd6bSTejun Heo 
5977029cfd6bSTejun Heo 	if (!ops || !ops->inherits)
5978029cfd6bSTejun Heo 		return;
5979029cfd6bSTejun Heo 
5980029cfd6bSTejun Heo 	spin_lock(&lock);
5981029cfd6bSTejun Heo 
5982029cfd6bSTejun Heo 	for (cur = ops->inherits; cur; cur = cur->inherits) {
5983029cfd6bSTejun Heo 		void **inherit = (void **)cur;
5984029cfd6bSTejun Heo 
5985029cfd6bSTejun Heo 		for (pp = begin; pp < end; pp++, inherit++)
5986029cfd6bSTejun Heo 			if (!*pp)
5987029cfd6bSTejun Heo 				*pp = *inherit;
5988029cfd6bSTejun Heo 	}
5989029cfd6bSTejun Heo 
5990029cfd6bSTejun Heo 	for (pp = begin; pp < end; pp++)
5991029cfd6bSTejun Heo 		if (IS_ERR(*pp))
5992029cfd6bSTejun Heo 			*pp = NULL;
5993029cfd6bSTejun Heo 
5994029cfd6bSTejun Heo 	ops->inherits = NULL;
5995029cfd6bSTejun Heo 
5996029cfd6bSTejun Heo 	spin_unlock(&lock);
5997029cfd6bSTejun Heo }
5998029cfd6bSTejun Heo 
5999029cfd6bSTejun Heo /**
6000ecef7253STejun Heo  *	ata_host_start - start and freeze ports of an ATA host
6001ecef7253STejun Heo  *	@host: ATA host to start ports for
6002ecef7253STejun Heo  *
6003ecef7253STejun Heo  *	Start and then freeze ports of @host.  Started status is
6004ecef7253STejun Heo  *	recorded in host->flags, so this function can be called
6005ecef7253STejun Heo  *	multiple times.  Ports are guaranteed to get started only
6006f3187195STejun Heo  *	once.  If host->ops isn't initialized yet, its set to the
6007f3187195STejun Heo  *	first non-dummy port ops.
6008ecef7253STejun Heo  *
6009ecef7253STejun Heo  *	LOCKING:
6010ecef7253STejun Heo  *	Inherited from calling layer (may sleep).
6011ecef7253STejun Heo  *
6012ecef7253STejun Heo  *	RETURNS:
6013ecef7253STejun Heo  *	0 if all ports are started successfully, -errno otherwise.
6014ecef7253STejun Heo  */
6015ecef7253STejun Heo int ata_host_start(struct ata_host *host)
6016ecef7253STejun Heo {
601732ebbc0cSTejun Heo 	int have_stop = 0;
601832ebbc0cSTejun Heo 	void *start_dr = NULL;
6019ecef7253STejun Heo 	int i, rc;
6020ecef7253STejun Heo 
6021ecef7253STejun Heo 	if (host->flags & ATA_HOST_STARTED)
6022ecef7253STejun Heo 		return 0;
6023ecef7253STejun Heo 
6024029cfd6bSTejun Heo 	ata_finalize_port_ops(host->ops);
6025029cfd6bSTejun Heo 
6026ecef7253STejun Heo 	for (i = 0; i < host->n_ports; i++) {
6027ecef7253STejun Heo 		struct ata_port *ap = host->ports[i];
6028ecef7253STejun Heo 
6029029cfd6bSTejun Heo 		ata_finalize_port_ops(ap->ops);
6030029cfd6bSTejun Heo 
6031f3187195STejun Heo 		if (!host->ops && !ata_port_is_dummy(ap))
6032f3187195STejun Heo 			host->ops = ap->ops;
6033f3187195STejun Heo 
603432ebbc0cSTejun Heo 		if (ap->ops->port_stop)
603532ebbc0cSTejun Heo 			have_stop = 1;
603632ebbc0cSTejun Heo 	}
603732ebbc0cSTejun Heo 
603832ebbc0cSTejun Heo 	if (host->ops->host_stop)
603932ebbc0cSTejun Heo 		have_stop = 1;
604032ebbc0cSTejun Heo 
604132ebbc0cSTejun Heo 	if (have_stop) {
604232ebbc0cSTejun Heo 		start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
604332ebbc0cSTejun Heo 		if (!start_dr)
604432ebbc0cSTejun Heo 			return -ENOMEM;
604532ebbc0cSTejun Heo 	}
604632ebbc0cSTejun Heo 
604732ebbc0cSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
604832ebbc0cSTejun Heo 		struct ata_port *ap = host->ports[i];
604932ebbc0cSTejun Heo 
6050ecef7253STejun Heo 		if (ap->ops->port_start) {
6051ecef7253STejun Heo 			rc = ap->ops->port_start(ap);
6052ecef7253STejun Heo 			if (rc) {
60530f9fe9b7SAlan Cox 				if (rc != -ENODEV)
60540f757743SAndrew Morton 					dev_printk(KERN_ERR, host->dev,
60550f757743SAndrew Morton 						"failed to start port %d "
60560f757743SAndrew Morton 						"(errno=%d)\n", i, rc);
6057ecef7253STejun Heo 				goto err_out;
6058ecef7253STejun Heo 			}
6059ecef7253STejun Heo 		}
6060ecef7253STejun Heo 		ata_eh_freeze_port(ap);
6061ecef7253STejun Heo 	}
6062ecef7253STejun Heo 
606332ebbc0cSTejun Heo 	if (start_dr)
606432ebbc0cSTejun Heo 		devres_add(host->dev, start_dr);
6065ecef7253STejun Heo 	host->flags |= ATA_HOST_STARTED;
6066ecef7253STejun Heo 	return 0;
6067ecef7253STejun Heo 
6068ecef7253STejun Heo  err_out:
6069ecef7253STejun Heo 	while (--i >= 0) {
6070ecef7253STejun Heo 		struct ata_port *ap = host->ports[i];
6071ecef7253STejun Heo 
6072ecef7253STejun Heo 		if (ap->ops->port_stop)
6073ecef7253STejun Heo 			ap->ops->port_stop(ap);
6074ecef7253STejun Heo 	}
607532ebbc0cSTejun Heo 	devres_free(start_dr);
6076ecef7253STejun Heo 	return rc;
6077ecef7253STejun Heo }
6078ecef7253STejun Heo 
6079ecef7253STejun Heo /**
6080cca3974eSJeff Garzik  *	ata_sas_host_init - Initialize a host struct
6081cca3974eSJeff Garzik  *	@host:	host to initialize
6082cca3974eSJeff Garzik  *	@dev:	device host is attached to
6083cca3974eSJeff Garzik  *	@flags:	host flags
6084c6fd2807SJeff Garzik  *	@ops:	port_ops
6085c6fd2807SJeff Garzik  *
6086c6fd2807SJeff Garzik  *	LOCKING:
6087c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
6088c6fd2807SJeff Garzik  *
6089c6fd2807SJeff Garzik  */
6090f3187195STejun Heo /* KILLME - the only user left is ipr */
6091cca3974eSJeff Garzik void ata_host_init(struct ata_host *host, struct device *dev,
6092029cfd6bSTejun Heo 		   unsigned long flags, struct ata_port_operations *ops)
6093c6fd2807SJeff Garzik {
6094cca3974eSJeff Garzik 	spin_lock_init(&host->lock);
6095cca3974eSJeff Garzik 	host->dev = dev;
6096cca3974eSJeff Garzik 	host->flags = flags;
6097cca3974eSJeff Garzik 	host->ops = ops;
6098c6fd2807SJeff Garzik }
6099c6fd2807SJeff Garzik 
610079318057SArjan van de Ven 
610179318057SArjan van de Ven static void async_port_probe(void *data, async_cookie_t cookie)
610279318057SArjan van de Ven {
610379318057SArjan van de Ven 	int rc;
610479318057SArjan van de Ven 	struct ata_port *ap = data;
6105886ad09fSArjan van de Ven 
6106886ad09fSArjan van de Ven 	/*
6107886ad09fSArjan van de Ven 	 * If we're not allowed to scan this host in parallel,
6108886ad09fSArjan van de Ven 	 * we need to wait until all previous scans have completed
6109886ad09fSArjan van de Ven 	 * before going further.
6110fa853a48SArjan van de Ven 	 * Jeff Garzik says this is only within a controller, so we
6111fa853a48SArjan van de Ven 	 * don't need to wait for port 0, only for later ports.
6112886ad09fSArjan van de Ven 	 */
6113fa853a48SArjan van de Ven 	if (!(ap->host->flags & ATA_HOST_PARALLEL_SCAN) && ap->port_no != 0)
6114886ad09fSArjan van de Ven 		async_synchronize_cookie(cookie);
6115886ad09fSArjan van de Ven 
611679318057SArjan van de Ven 	/* probe */
611779318057SArjan van de Ven 	if (ap->ops->error_handler) {
611879318057SArjan van de Ven 		struct ata_eh_info *ehi = &ap->link.eh_info;
611979318057SArjan van de Ven 		unsigned long flags;
612079318057SArjan van de Ven 
612179318057SArjan van de Ven 		ata_port_probe(ap);
612279318057SArjan van de Ven 
612379318057SArjan van de Ven 		/* kick EH for boot probing */
612479318057SArjan van de Ven 		spin_lock_irqsave(ap->lock, flags);
612579318057SArjan van de Ven 
612679318057SArjan van de Ven 		ehi->probe_mask |= ATA_ALL_DEVICES;
612779318057SArjan van de Ven 		ehi->action |= ATA_EH_RESET | ATA_EH_LPM;
612879318057SArjan van de Ven 		ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
612979318057SArjan van de Ven 
613079318057SArjan van de Ven 		ap->pflags &= ~ATA_PFLAG_INITIALIZING;
613179318057SArjan van de Ven 		ap->pflags |= ATA_PFLAG_LOADING;
613279318057SArjan van de Ven 		ata_port_schedule_eh(ap);
613379318057SArjan van de Ven 
613479318057SArjan van de Ven 		spin_unlock_irqrestore(ap->lock, flags);
613579318057SArjan van de Ven 
613679318057SArjan van de Ven 		/* wait for EH to finish */
613779318057SArjan van de Ven 		ata_port_wait_eh(ap);
613879318057SArjan van de Ven 	} else {
613979318057SArjan van de Ven 		DPRINTK("ata%u: bus probe begin\n", ap->print_id);
614079318057SArjan van de Ven 		rc = ata_bus_probe(ap);
614179318057SArjan van de Ven 		DPRINTK("ata%u: bus probe end\n", ap->print_id);
614279318057SArjan van de Ven 
614379318057SArjan van de Ven 		if (rc) {
614479318057SArjan van de Ven 			/* FIXME: do something useful here?
614579318057SArjan van de Ven 			 * Current libata behavior will
614679318057SArjan van de Ven 			 * tear down everything when
614779318057SArjan van de Ven 			 * the module is removed
614879318057SArjan van de Ven 			 * or the h/w is unplugged.
614979318057SArjan van de Ven 			 */
615079318057SArjan van de Ven 		}
615179318057SArjan van de Ven 	}
6152f29d3b23SArjan van de Ven 
6153f29d3b23SArjan van de Ven 	/* in order to keep device order, we need to synchronize at this point */
6154f29d3b23SArjan van de Ven 	async_synchronize_cookie(cookie);
6155f29d3b23SArjan van de Ven 
6156f29d3b23SArjan van de Ven 	ata_scsi_scan_host(ap, 1);
6157f29d3b23SArjan van de Ven 
615879318057SArjan van de Ven }
6159c6fd2807SJeff Garzik /**
6160f3187195STejun Heo  *	ata_host_register - register initialized ATA host
6161f3187195STejun Heo  *	@host: ATA host to register
6162f3187195STejun Heo  *	@sht: template for SCSI host
6163c6fd2807SJeff Garzik  *
6164f3187195STejun Heo  *	Register initialized ATA host.  @host is allocated using
6165f3187195STejun Heo  *	ata_host_alloc() and fully initialized by LLD.  This function
6166f3187195STejun Heo  *	starts ports, registers @host with ATA and SCSI layers and
6167f3187195STejun Heo  *	probe registered devices.
6168c6fd2807SJeff Garzik  *
6169c6fd2807SJeff Garzik  *	LOCKING:
6170f3187195STejun Heo  *	Inherited from calling layer (may sleep).
6171c6fd2807SJeff Garzik  *
6172c6fd2807SJeff Garzik  *	RETURNS:
6173f3187195STejun Heo  *	0 on success, -errno otherwise.
6174c6fd2807SJeff Garzik  */
6175f3187195STejun Heo int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
6176c6fd2807SJeff Garzik {
6177f3187195STejun Heo 	int i, rc;
6178c6fd2807SJeff Garzik 
6179f3187195STejun Heo 	/* host must have been started */
6180f3187195STejun Heo 	if (!(host->flags & ATA_HOST_STARTED)) {
6181f3187195STejun Heo 		dev_printk(KERN_ERR, host->dev,
6182f3187195STejun Heo 			   "BUG: trying to register unstarted host\n");
6183f3187195STejun Heo 		WARN_ON(1);
6184f3187195STejun Heo 		return -EINVAL;
618502f076aaSAlan Cox 	}
6186f0d36efdSTejun Heo 
6187f3187195STejun Heo 	/* Blow away unused ports.  This happens when LLD can't
6188f3187195STejun Heo 	 * determine the exact number of ports to allocate at
6189f3187195STejun Heo 	 * allocation time.
6190f3187195STejun Heo 	 */
6191f3187195STejun Heo 	for (i = host->n_ports; host->ports[i]; i++)
6192f3187195STejun Heo 		kfree(host->ports[i]);
6193f0d36efdSTejun Heo 
6194f3187195STejun Heo 	/* give ports names and add SCSI hosts */
6195f3187195STejun Heo 	for (i = 0; i < host->n_ports; i++)
6196f3187195STejun Heo 		host->ports[i]->print_id = ata_print_id++;
6197c6fd2807SJeff Garzik 
6198f3187195STejun Heo 	rc = ata_scsi_add_hosts(host, sht);
6199ecef7253STejun Heo 	if (rc)
6200f3187195STejun Heo 		return rc;
6201ecef7253STejun Heo 
6202fafbae87STejun Heo 	/* associate with ACPI nodes */
6203fafbae87STejun Heo 	ata_acpi_associate(host);
6204fafbae87STejun Heo 
6205f3187195STejun Heo 	/* set cable, sata_spd_limit and report */
6206cca3974eSJeff Garzik 	for (i = 0; i < host->n_ports; i++) {
6207cca3974eSJeff Garzik 		struct ata_port *ap = host->ports[i];
6208f3187195STejun Heo 		unsigned long xfer_mask;
6209f3187195STejun Heo 
6210f3187195STejun Heo 		/* set SATA cable type if still unset */
6211f3187195STejun Heo 		if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
6212f3187195STejun Heo 			ap->cbl = ATA_CBL_SATA;
6213c6fd2807SJeff Garzik 
6214c6fd2807SJeff Garzik 		/* init sata_spd_limit to the current value */
62154fb37a25STejun Heo 		sata_link_init_spd(&ap->link);
6216b1c72916STejun Heo 		if (ap->slave_link)
6217b1c72916STejun Heo 			sata_link_init_spd(ap->slave_link);
6218c6fd2807SJeff Garzik 
6219cbcdd875STejun Heo 		/* print per-port info to dmesg */
6220f3187195STejun Heo 		xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
6221f3187195STejun Heo 					      ap->udma_mask);
6222f3187195STejun Heo 
6223abf6e8edSTejun Heo 		if (!ata_port_is_dummy(ap)) {
6224cbcdd875STejun Heo 			ata_port_printk(ap, KERN_INFO,
6225cbcdd875STejun Heo 					"%cATA max %s %s\n",
6226a16abc0bSTejun Heo 					(ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
6227f3187195STejun Heo 					ata_mode_string(xfer_mask),
6228cbcdd875STejun Heo 					ap->link.eh_info.desc);
6229abf6e8edSTejun Heo 			ata_ehi_clear_desc(&ap->link.eh_info);
6230abf6e8edSTejun Heo 		} else
6231f3187195STejun Heo 			ata_port_printk(ap, KERN_INFO, "DUMMY\n");
6232c6fd2807SJeff Garzik 	}
6233c6fd2807SJeff Garzik 
6234f6005354SVegard Nossum 	/* perform each probe asynchronously */
6235f3187195STejun Heo 	for (i = 0; i < host->n_ports; i++) {
6236f3187195STejun Heo 		struct ata_port *ap = host->ports[i];
623779318057SArjan van de Ven 		async_schedule(async_port_probe, ap);
6238c6fd2807SJeff Garzik 	}
6239c6fd2807SJeff Garzik 
6240f3187195STejun Heo 	return 0;
6241f3187195STejun Heo }
6242f3187195STejun Heo 
6243f3187195STejun Heo /**
6244f5cda257STejun Heo  *	ata_host_activate - start host, request IRQ and register it
6245f5cda257STejun Heo  *	@host: target ATA host
6246f5cda257STejun Heo  *	@irq: IRQ to request
6247f5cda257STejun Heo  *	@irq_handler: irq_handler used when requesting IRQ
6248f5cda257STejun Heo  *	@irq_flags: irq_flags used when requesting IRQ
6249f5cda257STejun Heo  *	@sht: scsi_host_template to use when registering the host
6250f5cda257STejun Heo  *
6251f5cda257STejun Heo  *	After allocating an ATA host and initializing it, most libata
6252f5cda257STejun Heo  *	LLDs perform three steps to activate the host - start host,
6253f5cda257STejun Heo  *	request IRQ and register it.  This helper takes necessasry
6254f5cda257STejun Heo  *	arguments and performs the three steps in one go.
6255f5cda257STejun Heo  *
62563d46b2e2SPaul Mundt  *	An invalid IRQ skips the IRQ registration and expects the host to
62573d46b2e2SPaul Mundt  *	have set polling mode on the port. In this case, @irq_handler
62583d46b2e2SPaul Mundt  *	should be NULL.
62593d46b2e2SPaul Mundt  *
6260f5cda257STejun Heo  *	LOCKING:
6261f5cda257STejun Heo  *	Inherited from calling layer (may sleep).
6262f5cda257STejun Heo  *
6263f5cda257STejun Heo  *	RETURNS:
6264f5cda257STejun Heo  *	0 on success, -errno otherwise.
6265f5cda257STejun Heo  */
6266f5cda257STejun Heo int ata_host_activate(struct ata_host *host, int irq,
6267f5cda257STejun Heo 		      irq_handler_t irq_handler, unsigned long irq_flags,
6268f5cda257STejun Heo 		      struct scsi_host_template *sht)
6269f5cda257STejun Heo {
6270cbcdd875STejun Heo 	int i, rc;
6271f5cda257STejun Heo 
6272f5cda257STejun Heo 	rc = ata_host_start(host);
6273f5cda257STejun Heo 	if (rc)
6274f5cda257STejun Heo 		return rc;
6275f5cda257STejun Heo 
62763d46b2e2SPaul Mundt 	/* Special case for polling mode */
62773d46b2e2SPaul Mundt 	if (!irq) {
62783d46b2e2SPaul Mundt 		WARN_ON(irq_handler);
62793d46b2e2SPaul Mundt 		return ata_host_register(host, sht);
62803d46b2e2SPaul Mundt 	}
62813d46b2e2SPaul Mundt 
6282f5cda257STejun Heo 	rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
6283f5cda257STejun Heo 			      dev_driver_string(host->dev), host);
6284f5cda257STejun Heo 	if (rc)
6285f5cda257STejun Heo 		return rc;
6286f5cda257STejun Heo 
6287cbcdd875STejun Heo 	for (i = 0; i < host->n_ports; i++)
6288cbcdd875STejun Heo 		ata_port_desc(host->ports[i], "irq %d", irq);
62894031826bSTejun Heo 
6290f5cda257STejun Heo 	rc = ata_host_register(host, sht);
6291f5cda257STejun Heo 	/* if failed, just free the IRQ and leave ports alone */
6292f5cda257STejun Heo 	if (rc)
6293f5cda257STejun Heo 		devm_free_irq(host->dev, irq, host);
6294f5cda257STejun Heo 
6295f5cda257STejun Heo 	return rc;
6296f5cda257STejun Heo }
6297f5cda257STejun Heo 
6298f5cda257STejun Heo /**
6299c6fd2807SJeff Garzik  *	ata_port_detach - Detach ATA port in prepration of device removal
6300c6fd2807SJeff Garzik  *	@ap: ATA port to be detached
6301c6fd2807SJeff Garzik  *
6302c6fd2807SJeff Garzik  *	Detach all ATA devices and the associated SCSI devices of @ap;
6303c6fd2807SJeff Garzik  *	then, remove the associated SCSI host.  @ap is guaranteed to
6304c6fd2807SJeff Garzik  *	be quiescent on return from this function.
6305c6fd2807SJeff Garzik  *
6306c6fd2807SJeff Garzik  *	LOCKING:
6307c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
6308c6fd2807SJeff Garzik  */
6309741b7763SAdrian Bunk static void ata_port_detach(struct ata_port *ap)
6310c6fd2807SJeff Garzik {
6311c6fd2807SJeff Garzik 	unsigned long flags;
6312c6fd2807SJeff Garzik 
6313c6fd2807SJeff Garzik 	if (!ap->ops->error_handler)
6314c6fd2807SJeff Garzik 		goto skip_eh;
6315c6fd2807SJeff Garzik 
6316c6fd2807SJeff Garzik 	/* tell EH we're leaving & flush EH */
6317c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
6318c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_UNLOADING;
6319ece180d1STejun Heo 	ata_port_schedule_eh(ap);
6320c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
6321c6fd2807SJeff Garzik 
6322ece180d1STejun Heo 	/* wait till EH commits suicide */
6323c6fd2807SJeff Garzik 	ata_port_wait_eh(ap);
6324c6fd2807SJeff Garzik 
6325ece180d1STejun Heo 	/* it better be dead now */
6326ece180d1STejun Heo 	WARN_ON(!(ap->pflags & ATA_PFLAG_UNLOADED));
6327c6fd2807SJeff Garzik 
632845a66c1cSOleg Nesterov 	cancel_rearming_delayed_work(&ap->hotplug_task);
6329c6fd2807SJeff Garzik 
6330c6fd2807SJeff Garzik  skip_eh:
6331c6fd2807SJeff Garzik 	/* remove the associated SCSI host */
6332cca3974eSJeff Garzik 	scsi_remove_host(ap->scsi_host);
6333c6fd2807SJeff Garzik }
6334c6fd2807SJeff Garzik 
6335c6fd2807SJeff Garzik /**
63360529c159STejun Heo  *	ata_host_detach - Detach all ports of an ATA host
63370529c159STejun Heo  *	@host: Host to detach
63380529c159STejun Heo  *
63390529c159STejun Heo  *	Detach all ports of @host.
63400529c159STejun Heo  *
63410529c159STejun Heo  *	LOCKING:
63420529c159STejun Heo  *	Kernel thread context (may sleep).
63430529c159STejun Heo  */
63440529c159STejun Heo void ata_host_detach(struct ata_host *host)
63450529c159STejun Heo {
63460529c159STejun Heo 	int i;
63470529c159STejun Heo 
63480529c159STejun Heo 	for (i = 0; i < host->n_ports; i++)
63490529c159STejun Heo 		ata_port_detach(host->ports[i]);
6350562f0c2dSTejun Heo 
6351562f0c2dSTejun Heo 	/* the host is dead now, dissociate ACPI */
6352562f0c2dSTejun Heo 	ata_acpi_dissociate(host);
63530529c159STejun Heo }
63540529c159STejun Heo 
6355c6fd2807SJeff Garzik #ifdef CONFIG_PCI
6356c6fd2807SJeff Garzik 
6357c6fd2807SJeff Garzik /**
6358c6fd2807SJeff Garzik  *	ata_pci_remove_one - PCI layer callback for device removal
6359c6fd2807SJeff Garzik  *	@pdev: PCI device that was removed
6360c6fd2807SJeff Garzik  *
6361b878ca5dSTejun Heo  *	PCI layer indicates to libata via this hook that hot-unplug or
6362b878ca5dSTejun Heo  *	module unload event has occurred.  Detach all ports.  Resource
6363b878ca5dSTejun Heo  *	release is handled via devres.
6364c6fd2807SJeff Garzik  *
6365c6fd2807SJeff Garzik  *	LOCKING:
6366c6fd2807SJeff Garzik  *	Inherited from PCI layer (may sleep).
6367c6fd2807SJeff Garzik  */
6368c6fd2807SJeff Garzik void ata_pci_remove_one(struct pci_dev *pdev)
6369c6fd2807SJeff Garzik {
63702855568bSJeff Garzik 	struct device *dev = &pdev->dev;
6371cca3974eSJeff Garzik 	struct ata_host *host = dev_get_drvdata(dev);
6372c6fd2807SJeff Garzik 
6373f0d36efdSTejun Heo 	ata_host_detach(host);
6374c6fd2807SJeff Garzik }
6375c6fd2807SJeff Garzik 
6376c6fd2807SJeff Garzik /* move to PCI subsystem */
6377c6fd2807SJeff Garzik int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
6378c6fd2807SJeff Garzik {
6379c6fd2807SJeff Garzik 	unsigned long tmp = 0;
6380c6fd2807SJeff Garzik 
6381c6fd2807SJeff Garzik 	switch (bits->width) {
6382c6fd2807SJeff Garzik 	case 1: {
6383c6fd2807SJeff Garzik 		u8 tmp8 = 0;
6384c6fd2807SJeff Garzik 		pci_read_config_byte(pdev, bits->reg, &tmp8);
6385c6fd2807SJeff Garzik 		tmp = tmp8;
6386c6fd2807SJeff Garzik 		break;
6387c6fd2807SJeff Garzik 	}
6388c6fd2807SJeff Garzik 	case 2: {
6389c6fd2807SJeff Garzik 		u16 tmp16 = 0;
6390c6fd2807SJeff Garzik 		pci_read_config_word(pdev, bits->reg, &tmp16);
6391c6fd2807SJeff Garzik 		tmp = tmp16;
6392c6fd2807SJeff Garzik 		break;
6393c6fd2807SJeff Garzik 	}
6394c6fd2807SJeff Garzik 	case 4: {
6395c6fd2807SJeff Garzik 		u32 tmp32 = 0;
6396c6fd2807SJeff Garzik 		pci_read_config_dword(pdev, bits->reg, &tmp32);
6397c6fd2807SJeff Garzik 		tmp = tmp32;
6398c6fd2807SJeff Garzik 		break;
6399c6fd2807SJeff Garzik 	}
6400c6fd2807SJeff Garzik 
6401c6fd2807SJeff Garzik 	default:
6402c6fd2807SJeff Garzik 		return -EINVAL;
6403c6fd2807SJeff Garzik 	}
6404c6fd2807SJeff Garzik 
6405c6fd2807SJeff Garzik 	tmp &= bits->mask;
6406c6fd2807SJeff Garzik 
6407c6fd2807SJeff Garzik 	return (tmp == bits->val) ? 1 : 0;
6408c6fd2807SJeff Garzik }
6409c6fd2807SJeff Garzik 
64106ffa01d8STejun Heo #ifdef CONFIG_PM
6411c6fd2807SJeff Garzik void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
6412c6fd2807SJeff Garzik {
6413c6fd2807SJeff Garzik 	pci_save_state(pdev);
6414c6fd2807SJeff Garzik 	pci_disable_device(pdev);
64154c90d971STejun Heo 
64163a2d5b70SRafael J. Wysocki 	if (mesg.event & PM_EVENT_SLEEP)
6417c6fd2807SJeff Garzik 		pci_set_power_state(pdev, PCI_D3hot);
6418c6fd2807SJeff Garzik }
6419c6fd2807SJeff Garzik 
6420553c4aa6STejun Heo int ata_pci_device_do_resume(struct pci_dev *pdev)
6421c6fd2807SJeff Garzik {
6422553c4aa6STejun Heo 	int rc;
6423553c4aa6STejun Heo 
6424c6fd2807SJeff Garzik 	pci_set_power_state(pdev, PCI_D0);
6425c6fd2807SJeff Garzik 	pci_restore_state(pdev);
6426553c4aa6STejun Heo 
6427f0d36efdSTejun Heo 	rc = pcim_enable_device(pdev);
6428553c4aa6STejun Heo 	if (rc) {
6429553c4aa6STejun Heo 		dev_printk(KERN_ERR, &pdev->dev,
6430553c4aa6STejun Heo 			   "failed to enable device after resume (%d)\n", rc);
6431553c4aa6STejun Heo 		return rc;
6432553c4aa6STejun Heo 	}
6433553c4aa6STejun Heo 
6434c6fd2807SJeff Garzik 	pci_set_master(pdev);
6435553c4aa6STejun Heo 	return 0;
6436c6fd2807SJeff Garzik }
6437c6fd2807SJeff Garzik 
6438c6fd2807SJeff Garzik int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
6439c6fd2807SJeff Garzik {
6440cca3974eSJeff Garzik 	struct ata_host *host = dev_get_drvdata(&pdev->dev);
6441c6fd2807SJeff Garzik 	int rc = 0;
6442c6fd2807SJeff Garzik 
6443cca3974eSJeff Garzik 	rc = ata_host_suspend(host, mesg);
6444c6fd2807SJeff Garzik 	if (rc)
6445c6fd2807SJeff Garzik 		return rc;
6446c6fd2807SJeff Garzik 
6447c6fd2807SJeff Garzik 	ata_pci_device_do_suspend(pdev, mesg);
6448c6fd2807SJeff Garzik 
6449c6fd2807SJeff Garzik 	return 0;
6450c6fd2807SJeff Garzik }
6451c6fd2807SJeff Garzik 
6452c6fd2807SJeff Garzik int ata_pci_device_resume(struct pci_dev *pdev)
6453c6fd2807SJeff Garzik {
6454cca3974eSJeff Garzik 	struct ata_host *host = dev_get_drvdata(&pdev->dev);
6455553c4aa6STejun Heo 	int rc;
6456c6fd2807SJeff Garzik 
6457553c4aa6STejun Heo 	rc = ata_pci_device_do_resume(pdev);
6458553c4aa6STejun Heo 	if (rc == 0)
6459cca3974eSJeff Garzik 		ata_host_resume(host);
6460553c4aa6STejun Heo 	return rc;
6461c6fd2807SJeff Garzik }
64626ffa01d8STejun Heo #endif /* CONFIG_PM */
64636ffa01d8STejun Heo 
6464c6fd2807SJeff Garzik #endif /* CONFIG_PCI */
6465c6fd2807SJeff Garzik 
646633267325STejun Heo static int __init ata_parse_force_one(char **cur,
646733267325STejun Heo 				      struct ata_force_ent *force_ent,
646833267325STejun Heo 				      const char **reason)
646933267325STejun Heo {
647033267325STejun Heo 	/* FIXME: Currently, there's no way to tag init const data and
647133267325STejun Heo 	 * using __initdata causes build failure on some versions of
647233267325STejun Heo 	 * gcc.  Once __initdataconst is implemented, add const to the
647333267325STejun Heo 	 * following structure.
647433267325STejun Heo 	 */
647533267325STejun Heo 	static struct ata_force_param force_tbl[] __initdata = {
647633267325STejun Heo 		{ "40c",	.cbl		= ATA_CBL_PATA40 },
647733267325STejun Heo 		{ "80c",	.cbl		= ATA_CBL_PATA80 },
647833267325STejun Heo 		{ "short40c",	.cbl		= ATA_CBL_PATA40_SHORT },
647933267325STejun Heo 		{ "unk",	.cbl		= ATA_CBL_PATA_UNK },
648033267325STejun Heo 		{ "ign",	.cbl		= ATA_CBL_PATA_IGN },
648133267325STejun Heo 		{ "sata",	.cbl		= ATA_CBL_SATA },
648233267325STejun Heo 		{ "1.5Gbps",	.spd_limit	= 1 },
648333267325STejun Heo 		{ "3.0Gbps",	.spd_limit	= 2 },
648433267325STejun Heo 		{ "noncq",	.horkage_on	= ATA_HORKAGE_NONCQ },
648533267325STejun Heo 		{ "ncq",	.horkage_off	= ATA_HORKAGE_NONCQ },
648633267325STejun Heo 		{ "pio0",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 0) },
648733267325STejun Heo 		{ "pio1",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 1) },
648833267325STejun Heo 		{ "pio2",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 2) },
648933267325STejun Heo 		{ "pio3",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 3) },
649033267325STejun Heo 		{ "pio4",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 4) },
649133267325STejun Heo 		{ "pio5",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 5) },
649233267325STejun Heo 		{ "pio6",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 6) },
649333267325STejun Heo 		{ "mwdma0",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 0) },
649433267325STejun Heo 		{ "mwdma1",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 1) },
649533267325STejun Heo 		{ "mwdma2",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 2) },
649633267325STejun Heo 		{ "mwdma3",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 3) },
649733267325STejun Heo 		{ "mwdma4",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 4) },
649833267325STejun Heo 		{ "udma0",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
649933267325STejun Heo 		{ "udma16",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
650033267325STejun Heo 		{ "udma/16",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
650133267325STejun Heo 		{ "udma1",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
650233267325STejun Heo 		{ "udma25",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
650333267325STejun Heo 		{ "udma/25",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
650433267325STejun Heo 		{ "udma2",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
650533267325STejun Heo 		{ "udma33",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
650633267325STejun Heo 		{ "udma/33",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
650733267325STejun Heo 		{ "udma3",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
650833267325STejun Heo 		{ "udma44",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
650933267325STejun Heo 		{ "udma/44",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
651033267325STejun Heo 		{ "udma4",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
651133267325STejun Heo 		{ "udma66",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
651233267325STejun Heo 		{ "udma/66",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
651333267325STejun Heo 		{ "udma5",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
651433267325STejun Heo 		{ "udma100",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
651533267325STejun Heo 		{ "udma/100",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
651633267325STejun Heo 		{ "udma6",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
651733267325STejun Heo 		{ "udma133",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
651833267325STejun Heo 		{ "udma/133",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
651933267325STejun Heo 		{ "udma7",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 7) },
652005944bdfSTejun Heo 		{ "nohrst",	.lflags		= ATA_LFLAG_NO_HRST },
652105944bdfSTejun Heo 		{ "nosrst",	.lflags		= ATA_LFLAG_NO_SRST },
652205944bdfSTejun Heo 		{ "norst",	.lflags		= ATA_LFLAG_NO_HRST | ATA_LFLAG_NO_SRST },
652333267325STejun Heo 	};
652433267325STejun Heo 	char *start = *cur, *p = *cur;
652533267325STejun Heo 	char *id, *val, *endp;
652633267325STejun Heo 	const struct ata_force_param *match_fp = NULL;
652733267325STejun Heo 	int nr_matches = 0, i;
652833267325STejun Heo 
652933267325STejun Heo 	/* find where this param ends and update *cur */
653033267325STejun Heo 	while (*p != '\0' && *p != ',')
653133267325STejun Heo 		p++;
653233267325STejun Heo 
653333267325STejun Heo 	if (*p == '\0')
653433267325STejun Heo 		*cur = p;
653533267325STejun Heo 	else
653633267325STejun Heo 		*cur = p + 1;
653733267325STejun Heo 
653833267325STejun Heo 	*p = '\0';
653933267325STejun Heo 
654033267325STejun Heo 	/* parse */
654133267325STejun Heo 	p = strchr(start, ':');
654233267325STejun Heo 	if (!p) {
654333267325STejun Heo 		val = strstrip(start);
654433267325STejun Heo 		goto parse_val;
654533267325STejun Heo 	}
654633267325STejun Heo 	*p = '\0';
654733267325STejun Heo 
654833267325STejun Heo 	id = strstrip(start);
654933267325STejun Heo 	val = strstrip(p + 1);
655033267325STejun Heo 
655133267325STejun Heo 	/* parse id */
655233267325STejun Heo 	p = strchr(id, '.');
655333267325STejun Heo 	if (p) {
655433267325STejun Heo 		*p++ = '\0';
655533267325STejun Heo 		force_ent->device = simple_strtoul(p, &endp, 10);
655633267325STejun Heo 		if (p == endp || *endp != '\0') {
655733267325STejun Heo 			*reason = "invalid device";
655833267325STejun Heo 			return -EINVAL;
655933267325STejun Heo 		}
656033267325STejun Heo 	}
656133267325STejun Heo 
656233267325STejun Heo 	force_ent->port = simple_strtoul(id, &endp, 10);
656333267325STejun Heo 	if (p == endp || *endp != '\0') {
656433267325STejun Heo 		*reason = "invalid port/link";
656533267325STejun Heo 		return -EINVAL;
656633267325STejun Heo 	}
656733267325STejun Heo 
656833267325STejun Heo  parse_val:
656933267325STejun Heo 	/* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */
657033267325STejun Heo 	for (i = 0; i < ARRAY_SIZE(force_tbl); i++) {
657133267325STejun Heo 		const struct ata_force_param *fp = &force_tbl[i];
657233267325STejun Heo 
657333267325STejun Heo 		if (strncasecmp(val, fp->name, strlen(val)))
657433267325STejun Heo 			continue;
657533267325STejun Heo 
657633267325STejun Heo 		nr_matches++;
657733267325STejun Heo 		match_fp = fp;
657833267325STejun Heo 
657933267325STejun Heo 		if (strcasecmp(val, fp->name) == 0) {
658033267325STejun Heo 			nr_matches = 1;
658133267325STejun Heo 			break;
658233267325STejun Heo 		}
658333267325STejun Heo 	}
658433267325STejun Heo 
658533267325STejun Heo 	if (!nr_matches) {
658633267325STejun Heo 		*reason = "unknown value";
658733267325STejun Heo 		return -EINVAL;
658833267325STejun Heo 	}
658933267325STejun Heo 	if (nr_matches > 1) {
659033267325STejun Heo 		*reason = "ambigious value";
659133267325STejun Heo 		return -EINVAL;
659233267325STejun Heo 	}
659333267325STejun Heo 
659433267325STejun Heo 	force_ent->param = *match_fp;
659533267325STejun Heo 
659633267325STejun Heo 	return 0;
659733267325STejun Heo }
659833267325STejun Heo 
659933267325STejun Heo static void __init ata_parse_force_param(void)
660033267325STejun Heo {
660133267325STejun Heo 	int idx = 0, size = 1;
660233267325STejun Heo 	int last_port = -1, last_device = -1;
660333267325STejun Heo 	char *p, *cur, *next;
660433267325STejun Heo 
660533267325STejun Heo 	/* calculate maximum number of params and allocate force_tbl */
660633267325STejun Heo 	for (p = ata_force_param_buf; *p; p++)
660733267325STejun Heo 		if (*p == ',')
660833267325STejun Heo 			size++;
660933267325STejun Heo 
661033267325STejun Heo 	ata_force_tbl = kzalloc(sizeof(ata_force_tbl[0]) * size, GFP_KERNEL);
661133267325STejun Heo 	if (!ata_force_tbl) {
661233267325STejun Heo 		printk(KERN_WARNING "ata: failed to extend force table, "
661333267325STejun Heo 		       "libata.force ignored\n");
661433267325STejun Heo 		return;
661533267325STejun Heo 	}
661633267325STejun Heo 
661733267325STejun Heo 	/* parse and populate the table */
661833267325STejun Heo 	for (cur = ata_force_param_buf; *cur != '\0'; cur = next) {
661933267325STejun Heo 		const char *reason = "";
662033267325STejun Heo 		struct ata_force_ent te = { .port = -1, .device = -1 };
662133267325STejun Heo 
662233267325STejun Heo 		next = cur;
662333267325STejun Heo 		if (ata_parse_force_one(&next, &te, &reason)) {
662433267325STejun Heo 			printk(KERN_WARNING "ata: failed to parse force "
662533267325STejun Heo 			       "parameter \"%s\" (%s)\n",
662633267325STejun Heo 			       cur, reason);
662733267325STejun Heo 			continue;
662833267325STejun Heo 		}
662933267325STejun Heo 
663033267325STejun Heo 		if (te.port == -1) {
663133267325STejun Heo 			te.port = last_port;
663233267325STejun Heo 			te.device = last_device;
663333267325STejun Heo 		}
663433267325STejun Heo 
663533267325STejun Heo 		ata_force_tbl[idx++] = te;
663633267325STejun Heo 
663733267325STejun Heo 		last_port = te.port;
663833267325STejun Heo 		last_device = te.device;
663933267325STejun Heo 	}
664033267325STejun Heo 
664133267325STejun Heo 	ata_force_tbl_size = idx;
664233267325STejun Heo }
6643c6fd2807SJeff Garzik 
6644c6fd2807SJeff Garzik static int __init ata_init(void)
6645c6fd2807SJeff Garzik {
664633267325STejun Heo 	ata_parse_force_param();
664733267325STejun Heo 
66489cd13bdbSBartlomiej Zolnierkiewicz 	/*
66499cd13bdbSBartlomiej Zolnierkiewicz 	 * FIXME: In UP case, there is only one workqueue thread and if you
66509cd13bdbSBartlomiej Zolnierkiewicz 	 * have more than one PIO device, latency is bloody awful, with
66519cd13bdbSBartlomiej Zolnierkiewicz 	 * occasional multi-second "hiccups" as one PIO device waits for
66529cd13bdbSBartlomiej Zolnierkiewicz 	 * another.  It's an ugly wart that users DO occasionally complain
66539cd13bdbSBartlomiej Zolnierkiewicz 	 * about; luckily most users have at most one PIO polled device.
66549cd13bdbSBartlomiej Zolnierkiewicz 	 */
6655c6fd2807SJeff Garzik 	ata_wq = create_workqueue("ata");
6656c6fd2807SJeff Garzik 	if (!ata_wq)
665749ea3b04SElias Oltmanns 		goto free_force_tbl;
6658c6fd2807SJeff Garzik 
6659c6fd2807SJeff Garzik 	ata_aux_wq = create_singlethread_workqueue("ata_aux");
666049ea3b04SElias Oltmanns 	if (!ata_aux_wq)
666149ea3b04SElias Oltmanns 		goto free_wq;
6662c6fd2807SJeff Garzik 
6663c6fd2807SJeff Garzik 	printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
6664c6fd2807SJeff Garzik 	return 0;
666549ea3b04SElias Oltmanns 
666649ea3b04SElias Oltmanns free_wq:
666749ea3b04SElias Oltmanns 	destroy_workqueue(ata_wq);
666849ea3b04SElias Oltmanns free_force_tbl:
666949ea3b04SElias Oltmanns 	kfree(ata_force_tbl);
667049ea3b04SElias Oltmanns 	return -ENOMEM;
6671c6fd2807SJeff Garzik }
6672c6fd2807SJeff Garzik 
6673c6fd2807SJeff Garzik static void __exit ata_exit(void)
6674c6fd2807SJeff Garzik {
667533267325STejun Heo 	kfree(ata_force_tbl);
6676c6fd2807SJeff Garzik 	destroy_workqueue(ata_wq);
6677c6fd2807SJeff Garzik 	destroy_workqueue(ata_aux_wq);
6678c6fd2807SJeff Garzik }
6679c6fd2807SJeff Garzik 
6680a4625085SBrian King subsys_initcall(ata_init);
6681c6fd2807SJeff Garzik module_exit(ata_exit);
6682c6fd2807SJeff Garzik 
6683c6fd2807SJeff Garzik static unsigned long ratelimit_time;
6684c6fd2807SJeff Garzik static DEFINE_SPINLOCK(ata_ratelimit_lock);
6685c6fd2807SJeff Garzik 
6686c6fd2807SJeff Garzik int ata_ratelimit(void)
6687c6fd2807SJeff Garzik {
6688c6fd2807SJeff Garzik 	int rc;
6689c6fd2807SJeff Garzik 	unsigned long flags;
6690c6fd2807SJeff Garzik 
6691c6fd2807SJeff Garzik 	spin_lock_irqsave(&ata_ratelimit_lock, flags);
6692c6fd2807SJeff Garzik 
6693c6fd2807SJeff Garzik 	if (time_after(jiffies, ratelimit_time)) {
6694c6fd2807SJeff Garzik 		rc = 1;
6695c6fd2807SJeff Garzik 		ratelimit_time = jiffies + (HZ/5);
6696c6fd2807SJeff Garzik 	} else
6697c6fd2807SJeff Garzik 		rc = 0;
6698c6fd2807SJeff Garzik 
6699c6fd2807SJeff Garzik 	spin_unlock_irqrestore(&ata_ratelimit_lock, flags);
6700c6fd2807SJeff Garzik 
6701c6fd2807SJeff Garzik 	return rc;
6702c6fd2807SJeff Garzik }
6703c6fd2807SJeff Garzik 
6704c6fd2807SJeff Garzik /**
6705c6fd2807SJeff Garzik  *	ata_wait_register - wait until register value changes
6706c6fd2807SJeff Garzik  *	@reg: IO-mapped register
6707c6fd2807SJeff Garzik  *	@mask: Mask to apply to read register value
6708c6fd2807SJeff Garzik  *	@val: Wait condition
6709341c2c95STejun Heo  *	@interval: polling interval in milliseconds
6710341c2c95STejun Heo  *	@timeout: timeout in milliseconds
6711c6fd2807SJeff Garzik  *
6712c6fd2807SJeff Garzik  *	Waiting for some bits of register to change is a common
6713c6fd2807SJeff Garzik  *	operation for ATA controllers.  This function reads 32bit LE
6714c6fd2807SJeff Garzik  *	IO-mapped register @reg and tests for the following condition.
6715c6fd2807SJeff Garzik  *
6716c6fd2807SJeff Garzik  *	(*@reg & mask) != val
6717c6fd2807SJeff Garzik  *
6718c6fd2807SJeff Garzik  *	If the condition is met, it returns; otherwise, the process is
6719c6fd2807SJeff Garzik  *	repeated after @interval_msec until timeout.
6720c6fd2807SJeff Garzik  *
6721c6fd2807SJeff Garzik  *	LOCKING:
6722c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
6723c6fd2807SJeff Garzik  *
6724c6fd2807SJeff Garzik  *	RETURNS:
6725c6fd2807SJeff Garzik  *	The final register value.
6726c6fd2807SJeff Garzik  */
6727c6fd2807SJeff Garzik u32 ata_wait_register(void __iomem *reg, u32 mask, u32 val,
6728341c2c95STejun Heo 		      unsigned long interval, unsigned long timeout)
6729c6fd2807SJeff Garzik {
6730341c2c95STejun Heo 	unsigned long deadline;
6731c6fd2807SJeff Garzik 	u32 tmp;
6732c6fd2807SJeff Garzik 
6733c6fd2807SJeff Garzik 	tmp = ioread32(reg);
6734c6fd2807SJeff Garzik 
6735c6fd2807SJeff Garzik 	/* Calculate timeout _after_ the first read to make sure
6736c6fd2807SJeff Garzik 	 * preceding writes reach the controller before starting to
6737c6fd2807SJeff Garzik 	 * eat away the timeout.
6738c6fd2807SJeff Garzik 	 */
6739341c2c95STejun Heo 	deadline = ata_deadline(jiffies, timeout);
6740c6fd2807SJeff Garzik 
6741341c2c95STejun Heo 	while ((tmp & mask) == val && time_before(jiffies, deadline)) {
6742341c2c95STejun Heo 		msleep(interval);
6743c6fd2807SJeff Garzik 		tmp = ioread32(reg);
6744c6fd2807SJeff Garzik 	}
6745c6fd2807SJeff Garzik 
6746c6fd2807SJeff Garzik 	return tmp;
6747c6fd2807SJeff Garzik }
6748c6fd2807SJeff Garzik 
6749c6fd2807SJeff Garzik /*
6750c6fd2807SJeff Garzik  * Dummy port_ops
6751c6fd2807SJeff Garzik  */
6752c6fd2807SJeff Garzik static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
6753c6fd2807SJeff Garzik {
6754c6fd2807SJeff Garzik 	return AC_ERR_SYSTEM;
6755c6fd2807SJeff Garzik }
6756c6fd2807SJeff Garzik 
6757182d7bbaSTejun Heo static void ata_dummy_error_handler(struct ata_port *ap)
6758182d7bbaSTejun Heo {
6759182d7bbaSTejun Heo 	/* truly dummy */
6760182d7bbaSTejun Heo }
6761182d7bbaSTejun Heo 
6762029cfd6bSTejun Heo struct ata_port_operations ata_dummy_port_ops = {
6763c6fd2807SJeff Garzik 	.qc_prep		= ata_noop_qc_prep,
6764c6fd2807SJeff Garzik 	.qc_issue		= ata_dummy_qc_issue,
6765182d7bbaSTejun Heo 	.error_handler		= ata_dummy_error_handler,
6766c6fd2807SJeff Garzik };
6767c6fd2807SJeff Garzik 
676821b0ad4fSTejun Heo const struct ata_port_info ata_dummy_port_info = {
676921b0ad4fSTejun Heo 	.port_ops		= &ata_dummy_port_ops,
677021b0ad4fSTejun Heo };
677121b0ad4fSTejun Heo 
6772c6fd2807SJeff Garzik /*
6773c6fd2807SJeff Garzik  * libata is essentially a library of internal helper functions for
6774c6fd2807SJeff Garzik  * low-level ATA host controller drivers.  As such, the API/ABI is
6775c6fd2807SJeff Garzik  * likely to change as new drivers are added and updated.
6776c6fd2807SJeff Garzik  * Do not depend on ABI/API stability.
6777c6fd2807SJeff Garzik  */
6778c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
6779c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
6780c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_deb_timing_long);
6781029cfd6bSTejun Heo EXPORT_SYMBOL_GPL(ata_base_port_ops);
6782029cfd6bSTejun Heo EXPORT_SYMBOL_GPL(sata_port_ops);
6783c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
678421b0ad4fSTejun Heo EXPORT_SYMBOL_GPL(ata_dummy_port_info);
67851eca4365STejun Heo EXPORT_SYMBOL_GPL(ata_link_next);
67861eca4365STejun Heo EXPORT_SYMBOL_GPL(ata_dev_next);
6787c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_std_bios_param);
6788cca3974eSJeff Garzik EXPORT_SYMBOL_GPL(ata_host_init);
6789f3187195STejun Heo EXPORT_SYMBOL_GPL(ata_host_alloc);
6790f5cda257STejun Heo EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
6791b1c72916STejun Heo EXPORT_SYMBOL_GPL(ata_slave_link_init);
6792ecef7253STejun Heo EXPORT_SYMBOL_GPL(ata_host_start);
6793f3187195STejun Heo EXPORT_SYMBOL_GPL(ata_host_register);
6794f5cda257STejun Heo EXPORT_SYMBOL_GPL(ata_host_activate);
67950529c159STejun Heo EXPORT_SYMBOL_GPL(ata_host_detach);
6796c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_sg_init);
6797c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_qc_complete);
6798c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
6799436d34b3STejun Heo EXPORT_SYMBOL_GPL(atapi_cmd_type);
6800c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_tf_to_fis);
6801c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_tf_from_fis);
68026357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_pack_xfermask);
68036357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_unpack_xfermask);
68046357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
68056357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
68066357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
68076357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_mode_string);
68086357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_id_xfermask);
6809c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_start);
681004351821SAlan EXPORT_SYMBOL_GPL(ata_do_set_mode);
681131cc23b3STejun Heo EXPORT_SYMBOL_GPL(ata_std_qc_defer);
6812c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
6813c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_probe);
681410305f0fSAlan EXPORT_SYMBOL_GPL(ata_dev_disable);
6815c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_set_spd);
6816aa2731adSTejun Heo EXPORT_SYMBOL_GPL(ata_wait_after_reset);
6817936fd732STejun Heo EXPORT_SYMBOL_GPL(sata_link_debounce);
6818936fd732STejun Heo EXPORT_SYMBOL_GPL(sata_link_resume);
68190aa1113dSTejun Heo EXPORT_SYMBOL_GPL(ata_std_prereset);
6820cc0680a5STejun Heo EXPORT_SYMBOL_GPL(sata_link_hardreset);
682157c9efdfSTejun Heo EXPORT_SYMBOL_GPL(sata_std_hardreset);
6822203c75b8STejun Heo EXPORT_SYMBOL_GPL(ata_std_postreset);
6823c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_dev_classify);
6824c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_dev_pair);
6825c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_disable);
6826c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_ratelimit);
6827c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_wait_register);
6828c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
6829c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
6830c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
6831c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
6832c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_valid);
6833c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_read);
6834c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_write);
6835c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_write_flush);
6836936fd732STejun Heo EXPORT_SYMBOL_GPL(ata_link_online);
6837936fd732STejun Heo EXPORT_SYMBOL_GPL(ata_link_offline);
68386ffa01d8STejun Heo #ifdef CONFIG_PM
6839cca3974eSJeff Garzik EXPORT_SYMBOL_GPL(ata_host_suspend);
6840cca3974eSJeff Garzik EXPORT_SYMBOL_GPL(ata_host_resume);
68416ffa01d8STejun Heo #endif /* CONFIG_PM */
6842c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_id_string);
6843c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_id_c_string);
6844963e4975SAlan Cox EXPORT_SYMBOL_GPL(ata_do_dev_read_id);
6845c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_simulate);
6846c6fd2807SJeff Garzik 
68471a660164SMark Lord EXPORT_SYMBOL_GPL(ata_pio_queue_task);
6848c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
68496357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_timing_find_mode);
6850c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_timing_compute);
6851c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_timing_merge);
6852a0f79b92STejun Heo EXPORT_SYMBOL_GPL(ata_timing_cycle2mode);
6853c6fd2807SJeff Garzik 
6854c6fd2807SJeff Garzik #ifdef CONFIG_PCI
6855c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(pci_test_config_bits);
6856c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_remove_one);
68576ffa01d8STejun Heo #ifdef CONFIG_PM
6858c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
6859c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
6860c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
6861c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_resume);
68626ffa01d8STejun Heo #endif /* CONFIG_PM */
6863c6fd2807SJeff Garzik #endif /* CONFIG_PCI */
6864c6fd2807SJeff Garzik 
6865b64bbc39STejun Heo EXPORT_SYMBOL_GPL(__ata_ehi_push_desc);
6866b64bbc39STejun Heo EXPORT_SYMBOL_GPL(ata_ehi_push_desc);
6867b64bbc39STejun Heo EXPORT_SYMBOL_GPL(ata_ehi_clear_desc);
6868cbcdd875STejun Heo EXPORT_SYMBOL_GPL(ata_port_desc);
6869cbcdd875STejun Heo #ifdef CONFIG_PCI
6870cbcdd875STejun Heo EXPORT_SYMBOL_GPL(ata_port_pbar_desc);
6871cbcdd875STejun Heo #endif /* CONFIG_PCI */
6872c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
6873dbd82616STejun Heo EXPORT_SYMBOL_GPL(ata_link_abort);
6874c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_abort);
6875c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_freeze);
68767d77b247STejun Heo EXPORT_SYMBOL_GPL(sata_async_notification);
6877c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
6878c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
6879c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
6880c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
688110acf3b0SMark Lord EXPORT_SYMBOL_GPL(ata_eh_analyze_ncq_error);
6882c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_do_eh);
6883a1efdabaSTejun Heo EXPORT_SYMBOL_GPL(ata_std_error_handler);
6884be0d18dfSAlan Cox 
6885be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_40wire);
6886be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_80wire);
6887be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_unknown);
6888c88f90c3STejun Heo EXPORT_SYMBOL_GPL(ata_cable_ignore);
6889be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_sata);
6890