xref: /openbmc/linux/drivers/ata/libata-core.c (revision 410b5c7b)
1c6fd2807SJeff Garzik /*
2c6fd2807SJeff Garzik  *  libata-core.c - helper library for ATA
3c6fd2807SJeff Garzik  *
48c3d3d4bSTejun Heo  *  Maintained by:  Tejun Heo <tj@kernel.org>
5c6fd2807SJeff Garzik  *    		    Please ALWAYS copy linux-ide@vger.kernel.org
6c6fd2807SJeff Garzik  *		    on emails.
7c6fd2807SJeff Garzik  *
8c6fd2807SJeff Garzik  *  Copyright 2003-2004 Red Hat, Inc.  All rights reserved.
9c6fd2807SJeff Garzik  *  Copyright 2003-2004 Jeff Garzik
10c6fd2807SJeff Garzik  *
11c6fd2807SJeff Garzik  *
12c6fd2807SJeff Garzik  *  This program is free software; you can redistribute it and/or modify
13c6fd2807SJeff Garzik  *  it under the terms of the GNU General Public License as published by
14c6fd2807SJeff Garzik  *  the Free Software Foundation; either version 2, or (at your option)
15c6fd2807SJeff Garzik  *  any later version.
16c6fd2807SJeff Garzik  *
17c6fd2807SJeff Garzik  *  This program is distributed in the hope that it will be useful,
18c6fd2807SJeff Garzik  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19c6fd2807SJeff Garzik  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20c6fd2807SJeff Garzik  *  GNU General Public License for more details.
21c6fd2807SJeff Garzik  *
22c6fd2807SJeff Garzik  *  You should have received a copy of the GNU General Public License
23c6fd2807SJeff Garzik  *  along with this program; see the file COPYING.  If not, write to
24c6fd2807SJeff Garzik  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25c6fd2807SJeff Garzik  *
26c6fd2807SJeff Garzik  *
27c6fd2807SJeff Garzik  *  libata documentation is available via 'make {ps|pdf}docs',
2819285f3cSMauro Carvalho Chehab  *  as Documentation/driver-api/libata.rst
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>
53848c3920SAnil Veliyankara Madam #include <linux/time.h>
54c6fd2807SJeff Garzik #include <linux/interrupt.h>
55c6fd2807SJeff Garzik #include <linux/completion.h>
56c6fd2807SJeff Garzik #include <linux/suspend.h>
57c6fd2807SJeff Garzik #include <linux/workqueue.h>
58c6fd2807SJeff Garzik #include <linux/scatterlist.h>
592dcb407eSJeff Garzik #include <linux/io.h>
6079318057SArjan van de Ven #include <linux/async.h>
61e18086d6SMark Lord #include <linux/log2.h>
625a0e3ad6STejun Heo #include <linux/slab.h>
63428ac5fcSGeorge Spelvin #include <linux/glob.h>
64c6fd2807SJeff Garzik #include <scsi/scsi.h>
65c6fd2807SJeff Garzik #include <scsi/scsi_cmnd.h>
66c6fd2807SJeff Garzik #include <scsi/scsi_host.h>
67c6fd2807SJeff Garzik #include <linux/libata.h>
68c6fd2807SJeff Garzik #include <asm/byteorder.h>
69fe5af0ccSHannes Reinecke #include <asm/unaligned.h>
70140b5e59STejun Heo #include <linux/cdrom.h>
719990b6f3SAkinobu Mita #include <linux/ratelimit.h>
72eb25cb99SStephan Linz #include <linux/leds.h>
739ee4f393SLin Ming #include <linux/pm_runtime.h>
74b7db04d9SBrian Norris #include <linux/platform_device.h>
75c6fd2807SJeff Garzik 
76255c03d1SHannes Reinecke #define CREATE_TRACE_POINTS
77255c03d1SHannes Reinecke #include <trace/events/libata.h>
78255c03d1SHannes Reinecke 
79c6fd2807SJeff Garzik #include "libata.h"
80d9027470SGwendal Grignou #include "libata-transport.h"
81fda0efc5SJeff Garzik 
82c6fd2807SJeff Garzik /* debounce timing parameters in msecs { interval, duration, timeout } */
83c6fd2807SJeff Garzik const unsigned long sata_deb_timing_normal[]		= {   5,  100, 2000 };
84c6fd2807SJeff Garzik const unsigned long sata_deb_timing_hotplug[]		= {  25,  500, 2000 };
85c6fd2807SJeff Garzik const unsigned long sata_deb_timing_long[]		= { 100, 2000, 5000 };
86c6fd2807SJeff Garzik 
87029cfd6bSTejun Heo const struct ata_port_operations ata_base_port_ops = {
880aa1113dSTejun Heo 	.prereset		= ata_std_prereset,
89203c75b8STejun Heo 	.postreset		= ata_std_postreset,
90a1efdabaSTejun Heo 	.error_handler		= ata_std_error_handler,
91e4a9c373SDan Williams 	.sched_eh		= ata_std_sched_eh,
92e4a9c373SDan Williams 	.end_eh			= ata_std_end_eh,
93029cfd6bSTejun Heo };
94029cfd6bSTejun Heo 
95029cfd6bSTejun Heo const struct ata_port_operations sata_port_ops = {
96029cfd6bSTejun Heo 	.inherits		= &ata_base_port_ops,
97029cfd6bSTejun Heo 
98029cfd6bSTejun Heo 	.qc_defer		= ata_std_qc_defer,
9957c9efdfSTejun Heo 	.hardreset		= sata_std_hardreset,
100029cfd6bSTejun Heo };
101029cfd6bSTejun Heo 
102c6fd2807SJeff Garzik static unsigned int ata_dev_init_params(struct ata_device *dev,
103c6fd2807SJeff Garzik 					u16 heads, u16 sectors);
104c6fd2807SJeff Garzik static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
105c6fd2807SJeff Garzik static void ata_dev_xfermask(struct ata_device *dev);
10675683fe7STejun Heo static unsigned long ata_dev_blacklisted(const struct ata_device *dev);
107c6fd2807SJeff Garzik 
108a78f57afSTero Roponen atomic_t ata_print_id = ATOMIC_INIT(0);
109c6fd2807SJeff Garzik 
11033267325STejun Heo struct ata_force_param {
11133267325STejun Heo 	const char	*name;
11233267325STejun Heo 	unsigned int	cbl;
11333267325STejun Heo 	int		spd_limit;
11433267325STejun Heo 	unsigned long	xfer_mask;
11533267325STejun Heo 	unsigned int	horkage_on;
11633267325STejun Heo 	unsigned int	horkage_off;
11705944bdfSTejun Heo 	unsigned int	lflags;
11833267325STejun Heo };
11933267325STejun Heo 
12033267325STejun Heo struct ata_force_ent {
12133267325STejun Heo 	int			port;
12233267325STejun Heo 	int			device;
12333267325STejun Heo 	struct ata_force_param	param;
12433267325STejun Heo };
12533267325STejun Heo 
12633267325STejun Heo static struct ata_force_ent *ata_force_tbl;
12733267325STejun Heo static int ata_force_tbl_size;
12833267325STejun Heo 
12933267325STejun Heo static char ata_force_param_buf[PAGE_SIZE] __initdata;
1307afb4222STejun Heo /* param_buf is thrown away after initialization, disallow read */
1317afb4222STejun Heo module_param_string(force, ata_force_param_buf, sizeof(ata_force_param_buf), 0);
1328c27ceffSMauro Carvalho Chehab MODULE_PARM_DESC(force, "Force ATA configurations including cable type, link speed and transfer mode (see Documentation/admin-guide/kernel-parameters.rst for details)");
13333267325STejun Heo 
1342486fa56STejun Heo static int atapi_enabled = 1;
135c6fd2807SJeff Garzik module_param(atapi_enabled, int, 0444);
136ad5d8eacSEvgeni Golov MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on [default])");
137c6fd2807SJeff Garzik 
138c5c61bdaSAdrian Bunk static int atapi_dmadir = 0;
139c6fd2807SJeff Garzik module_param(atapi_dmadir, int, 0444);
140ad5d8eacSEvgeni Golov MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off [default], 1=on)");
141c6fd2807SJeff Garzik 
142baf4fdfaSMark Lord int atapi_passthru16 = 1;
143baf4fdfaSMark Lord module_param(atapi_passthru16, int, 0444);
144ad5d8eacSEvgeni Golov MODULE_PARM_DESC(atapi_passthru16, "Enable ATA_16 passthru for ATAPI devices (0=off, 1=on [default])");
145baf4fdfaSMark Lord 
146c6fd2807SJeff Garzik int libata_fua = 0;
147c6fd2807SJeff Garzik module_param_named(fua, libata_fua, int, 0444);
148ad5d8eacSEvgeni Golov MODULE_PARM_DESC(fua, "FUA support (0=off [default], 1=on)");
149c6fd2807SJeff Garzik 
1502dcb407eSJeff Garzik static int ata_ignore_hpa;
1511e999736SAlan Cox module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644);
1521e999736SAlan Cox MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)");
1531e999736SAlan Cox 
154b3a70601SAlan Cox static int libata_dma_mask = ATA_DMA_MASK_ATA|ATA_DMA_MASK_ATAPI|ATA_DMA_MASK_CFA;
155b3a70601SAlan Cox module_param_named(dma, libata_dma_mask, int, 0444);
156b3a70601SAlan Cox MODULE_PARM_DESC(dma, "DMA enable/disable (0x1==ATA, 0x2==ATAPI, 0x4==CF)");
157b3a70601SAlan Cox 
15887fbc5a0STejun Heo static int ata_probe_timeout;
159c6fd2807SJeff Garzik module_param(ata_probe_timeout, int, 0444);
160c6fd2807SJeff Garzik MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
161c6fd2807SJeff Garzik 
1626ebe9d86SJeff Garzik int libata_noacpi = 0;
163d7d0dad6SJeff Garzik module_param_named(noacpi, libata_noacpi, int, 0444);
164ad5d8eacSEvgeni Golov MODULE_PARM_DESC(noacpi, "Disable the use of ACPI in probe/suspend/resume (0=off [default], 1=on)");
16511ef697bSKristen Carlson Accardi 
166ae8d4ee7SAlan Cox int libata_allow_tpm = 0;
167ae8d4ee7SAlan Cox module_param_named(allow_tpm, libata_allow_tpm, int, 0444);
168ad5d8eacSEvgeni Golov MODULE_PARM_DESC(allow_tpm, "Permit the use of TPM commands (0=off [default], 1=on)");
169ae8d4ee7SAlan Cox 
170e7ecd435STejun Heo static int atapi_an;
171e7ecd435STejun Heo module_param(atapi_an, int, 0444);
172e7ecd435STejun Heo MODULE_PARM_DESC(atapi_an, "Enable ATAPI AN media presence notification (0=0ff [default], 1=on)");
173e7ecd435STejun Heo 
174c6fd2807SJeff Garzik MODULE_AUTHOR("Jeff Garzik");
175c6fd2807SJeff Garzik MODULE_DESCRIPTION("Library module for ATA devices");
176c6fd2807SJeff Garzik MODULE_LICENSE("GPL");
177c6fd2807SJeff Garzik MODULE_VERSION(DRV_VERSION);
178c6fd2807SJeff Garzik 
179c6fd2807SJeff Garzik 
1809913ff8aSTejun Heo static bool ata_sstatus_online(u32 sstatus)
1819913ff8aSTejun Heo {
1829913ff8aSTejun Heo 	return (sstatus & 0xf) == 0x3;
1839913ff8aSTejun Heo }
1849913ff8aSTejun Heo 
1851eca4365STejun Heo /**
1861eca4365STejun Heo  *	ata_link_next - link iteration helper
1871eca4365STejun Heo  *	@link: the previous link, NULL to start
1881eca4365STejun Heo  *	@ap: ATA port containing links to iterate
1891eca4365STejun Heo  *	@mode: iteration mode, one of ATA_LITER_*
190aadffb68STejun Heo  *
191aadffb68STejun Heo  *	LOCKING:
192aadffb68STejun Heo  *	Host lock or EH context.
1931eca4365STejun Heo  *
1941eca4365STejun Heo  *	RETURNS:
1951eca4365STejun Heo  *	Pointer to the next link.
196aadffb68STejun Heo  */
1971eca4365STejun Heo struct ata_link *ata_link_next(struct ata_link *link, struct ata_port *ap,
1981eca4365STejun Heo 			       enum ata_link_iter_mode mode)
199aadffb68STejun Heo {
2001eca4365STejun Heo 	BUG_ON(mode != ATA_LITER_EDGE &&
2011eca4365STejun Heo 	       mode != ATA_LITER_PMP_FIRST && mode != ATA_LITER_HOST_FIRST);
2021eca4365STejun Heo 
203aadffb68STejun Heo 	/* NULL link indicates start of iteration */
2041eca4365STejun Heo 	if (!link)
2051eca4365STejun Heo 		switch (mode) {
2061eca4365STejun Heo 		case ATA_LITER_EDGE:
2071eca4365STejun Heo 		case ATA_LITER_PMP_FIRST:
2081eca4365STejun Heo 			if (sata_pmp_attached(ap))
209aadffb68STejun Heo 				return ap->pmp_link;
2101eca4365STejun Heo 			/* fall through */
2111eca4365STejun Heo 		case ATA_LITER_HOST_FIRST:
212aadffb68STejun Heo 			return &ap->link;
213aadffb68STejun Heo 		}
214aadffb68STejun Heo 
2151eca4365STejun Heo 	/* we just iterated over the host link, what's next? */
2161eca4365STejun Heo 	if (link == &ap->link)
2171eca4365STejun Heo 		switch (mode) {
2181eca4365STejun Heo 		case ATA_LITER_HOST_FIRST:
2191eca4365STejun Heo 			if (sata_pmp_attached(ap))
220aadffb68STejun Heo 				return ap->pmp_link;
2211eca4365STejun Heo 			/* fall through */
2221eca4365STejun Heo 		case ATA_LITER_PMP_FIRST:
2231eca4365STejun Heo 			if (unlikely(ap->slave_link))
2241eca4365STejun Heo 				return ap->slave_link;
2251eca4365STejun Heo 			/* fall through */
2261eca4365STejun Heo 		case ATA_LITER_EDGE:
2271eca4365STejun Heo 			return NULL;
228aadffb68STejun Heo 		}
229aadffb68STejun Heo 
230b1c72916STejun Heo 	/* slave_link excludes PMP */
231b1c72916STejun Heo 	if (unlikely(link == ap->slave_link))
232b1c72916STejun Heo 		return NULL;
233b1c72916STejun Heo 
2341eca4365STejun Heo 	/* we were over a PMP link */
235aadffb68STejun Heo 	if (++link < ap->pmp_link + ap->nr_pmp_links)
236aadffb68STejun Heo 		return link;
2371eca4365STejun Heo 
2381eca4365STejun Heo 	if (mode == ATA_LITER_PMP_FIRST)
2391eca4365STejun Heo 		return &ap->link;
2401eca4365STejun Heo 
241aadffb68STejun Heo 	return NULL;
242aadffb68STejun Heo }
243aadffb68STejun Heo 
244c6fd2807SJeff Garzik /**
2451eca4365STejun Heo  *	ata_dev_next - device iteration helper
2461eca4365STejun Heo  *	@dev: the previous device, NULL to start
2471eca4365STejun Heo  *	@link: ATA link containing devices to iterate
2481eca4365STejun Heo  *	@mode: iteration mode, one of ATA_DITER_*
2491eca4365STejun Heo  *
2501eca4365STejun Heo  *	LOCKING:
2511eca4365STejun Heo  *	Host lock or EH context.
2521eca4365STejun Heo  *
2531eca4365STejun Heo  *	RETURNS:
2541eca4365STejun Heo  *	Pointer to the next device.
2551eca4365STejun Heo  */
2561eca4365STejun Heo struct ata_device *ata_dev_next(struct ata_device *dev, struct ata_link *link,
2571eca4365STejun Heo 				enum ata_dev_iter_mode mode)
2581eca4365STejun Heo {
2591eca4365STejun Heo 	BUG_ON(mode != ATA_DITER_ENABLED && mode != ATA_DITER_ENABLED_REVERSE &&
2601eca4365STejun Heo 	       mode != ATA_DITER_ALL && mode != ATA_DITER_ALL_REVERSE);
2611eca4365STejun Heo 
2621eca4365STejun Heo 	/* NULL dev indicates start of iteration */
2631eca4365STejun Heo 	if (!dev)
2641eca4365STejun Heo 		switch (mode) {
2651eca4365STejun Heo 		case ATA_DITER_ENABLED:
2661eca4365STejun Heo 		case ATA_DITER_ALL:
2671eca4365STejun Heo 			dev = link->device;
2681eca4365STejun Heo 			goto check;
2691eca4365STejun Heo 		case ATA_DITER_ENABLED_REVERSE:
2701eca4365STejun Heo 		case ATA_DITER_ALL_REVERSE:
2711eca4365STejun Heo 			dev = link->device + ata_link_max_devices(link) - 1;
2721eca4365STejun Heo 			goto check;
2731eca4365STejun Heo 		}
2741eca4365STejun Heo 
2751eca4365STejun Heo  next:
2761eca4365STejun Heo 	/* move to the next one */
2771eca4365STejun Heo 	switch (mode) {
2781eca4365STejun Heo 	case ATA_DITER_ENABLED:
2791eca4365STejun Heo 	case ATA_DITER_ALL:
2801eca4365STejun Heo 		if (++dev < link->device + ata_link_max_devices(link))
2811eca4365STejun Heo 			goto check;
2821eca4365STejun Heo 		return NULL;
2831eca4365STejun Heo 	case ATA_DITER_ENABLED_REVERSE:
2841eca4365STejun Heo 	case ATA_DITER_ALL_REVERSE:
2851eca4365STejun Heo 		if (--dev >= link->device)
2861eca4365STejun Heo 			goto check;
2871eca4365STejun Heo 		return NULL;
2881eca4365STejun Heo 	}
2891eca4365STejun Heo 
2901eca4365STejun Heo  check:
2911eca4365STejun Heo 	if ((mode == ATA_DITER_ENABLED || mode == ATA_DITER_ENABLED_REVERSE) &&
2921eca4365STejun Heo 	    !ata_dev_enabled(dev))
2931eca4365STejun Heo 		goto next;
2941eca4365STejun Heo 	return dev;
2951eca4365STejun Heo }
2961eca4365STejun Heo 
2971eca4365STejun Heo /**
298b1c72916STejun Heo  *	ata_dev_phys_link - find physical link for a device
299b1c72916STejun Heo  *	@dev: ATA device to look up physical link for
300b1c72916STejun Heo  *
301b1c72916STejun Heo  *	Look up physical link which @dev is attached to.  Note that
302b1c72916STejun Heo  *	this is different from @dev->link only when @dev is on slave
303b1c72916STejun Heo  *	link.  For all other cases, it's the same as @dev->link.
304b1c72916STejun Heo  *
305b1c72916STejun Heo  *	LOCKING:
306b1c72916STejun Heo  *	Don't care.
307b1c72916STejun Heo  *
308b1c72916STejun Heo  *	RETURNS:
309b1c72916STejun Heo  *	Pointer to the found physical link.
310b1c72916STejun Heo  */
311b1c72916STejun Heo struct ata_link *ata_dev_phys_link(struct ata_device *dev)
312b1c72916STejun Heo {
313b1c72916STejun Heo 	struct ata_port *ap = dev->link->ap;
314b1c72916STejun Heo 
315b1c72916STejun Heo 	if (!ap->slave_link)
316b1c72916STejun Heo 		return dev->link;
317b1c72916STejun Heo 	if (!dev->devno)
318b1c72916STejun Heo 		return &ap->link;
319b1c72916STejun Heo 	return ap->slave_link;
320b1c72916STejun Heo }
321b1c72916STejun Heo 
322b1c72916STejun Heo /**
32333267325STejun Heo  *	ata_force_cbl - force cable type according to libata.force
3244cdfa1b3SRandy Dunlap  *	@ap: ATA port of interest
32533267325STejun Heo  *
32633267325STejun Heo  *	Force cable type according to libata.force and whine about it.
32733267325STejun Heo  *	The last entry which has matching port number is used, so it
32833267325STejun Heo  *	can be specified as part of device force parameters.  For
32933267325STejun Heo  *	example, both "a:40c,1.00:udma4" and "1.00:40c,udma4" have the
33033267325STejun Heo  *	same effect.
33133267325STejun Heo  *
33233267325STejun Heo  *	LOCKING:
33333267325STejun Heo  *	EH context.
33433267325STejun Heo  */
33533267325STejun Heo void ata_force_cbl(struct ata_port *ap)
33633267325STejun Heo {
33733267325STejun Heo 	int i;
33833267325STejun Heo 
33933267325STejun Heo 	for (i = ata_force_tbl_size - 1; i >= 0; i--) {
34033267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
34133267325STejun Heo 
34233267325STejun Heo 		if (fe->port != -1 && fe->port != ap->print_id)
34333267325STejun Heo 			continue;
34433267325STejun Heo 
34533267325STejun Heo 		if (fe->param.cbl == ATA_CBL_NONE)
34633267325STejun Heo 			continue;
34733267325STejun Heo 
34833267325STejun Heo 		ap->cbl = fe->param.cbl;
349a9a79dfeSJoe Perches 		ata_port_notice(ap, "FORCE: cable set to %s\n", fe->param.name);
35033267325STejun Heo 		return;
35133267325STejun Heo 	}
35233267325STejun Heo }
35333267325STejun Heo 
35433267325STejun Heo /**
35505944bdfSTejun Heo  *	ata_force_link_limits - force link limits according to libata.force
35633267325STejun Heo  *	@link: ATA link of interest
35733267325STejun Heo  *
35805944bdfSTejun Heo  *	Force link flags and SATA spd limit according to libata.force
35905944bdfSTejun Heo  *	and whine about it.  When only the port part is specified
36005944bdfSTejun Heo  *	(e.g. 1:), the limit applies to all links connected to both
36105944bdfSTejun Heo  *	the host link and all fan-out ports connected via PMP.  If the
36205944bdfSTejun Heo  *	device part is specified as 0 (e.g. 1.00:), it specifies the
36305944bdfSTejun Heo  *	first fan-out link not the host link.  Device number 15 always
364b1c72916STejun Heo  *	points to the host link whether PMP is attached or not.  If the
365b1c72916STejun Heo  *	controller has slave link, device number 16 points to it.
36633267325STejun Heo  *
36733267325STejun Heo  *	LOCKING:
36833267325STejun Heo  *	EH context.
36933267325STejun Heo  */
37005944bdfSTejun Heo static void ata_force_link_limits(struct ata_link *link)
37133267325STejun Heo {
37205944bdfSTejun Heo 	bool did_spd = false;
373b1c72916STejun Heo 	int linkno = link->pmp;
374b1c72916STejun Heo 	int i;
37533267325STejun Heo 
37633267325STejun Heo 	if (ata_is_host_link(link))
377b1c72916STejun Heo 		linkno += 15;
37833267325STejun Heo 
37933267325STejun Heo 	for (i = ata_force_tbl_size - 1; i >= 0; i--) {
38033267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
38133267325STejun Heo 
38233267325STejun Heo 		if (fe->port != -1 && fe->port != link->ap->print_id)
38333267325STejun Heo 			continue;
38433267325STejun Heo 
38533267325STejun Heo 		if (fe->device != -1 && fe->device != linkno)
38633267325STejun Heo 			continue;
38733267325STejun Heo 
38805944bdfSTejun Heo 		/* only honor the first spd limit */
38905944bdfSTejun Heo 		if (!did_spd && fe->param.spd_limit) {
39033267325STejun Heo 			link->hw_sata_spd_limit = (1 << fe->param.spd_limit) - 1;
391a9a79dfeSJoe Perches 			ata_link_notice(link, "FORCE: PHY spd limit set to %s\n",
39205944bdfSTejun Heo 					fe->param.name);
39305944bdfSTejun Heo 			did_spd = true;
39405944bdfSTejun Heo 		}
39505944bdfSTejun Heo 
39605944bdfSTejun Heo 		/* let lflags stack */
39705944bdfSTejun Heo 		if (fe->param.lflags) {
39805944bdfSTejun Heo 			link->flags |= fe->param.lflags;
399a9a79dfeSJoe Perches 			ata_link_notice(link,
40005944bdfSTejun Heo 					"FORCE: link flag 0x%x forced -> 0x%x\n",
40105944bdfSTejun Heo 					fe->param.lflags, link->flags);
40205944bdfSTejun Heo 		}
40333267325STejun Heo 	}
40433267325STejun Heo }
40533267325STejun Heo 
40633267325STejun Heo /**
40733267325STejun Heo  *	ata_force_xfermask - force xfermask according to libata.force
40833267325STejun Heo  *	@dev: ATA device of interest
40933267325STejun Heo  *
41033267325STejun Heo  *	Force xfer_mask according to libata.force and whine about it.
41133267325STejun Heo  *	For consistency with link selection, device number 15 selects
41233267325STejun Heo  *	the first device connected to the host link.
41333267325STejun Heo  *
41433267325STejun Heo  *	LOCKING:
41533267325STejun Heo  *	EH context.
41633267325STejun Heo  */
41733267325STejun Heo static void ata_force_xfermask(struct ata_device *dev)
41833267325STejun Heo {
41933267325STejun Heo 	int devno = dev->link->pmp + dev->devno;
42033267325STejun Heo 	int alt_devno = devno;
42133267325STejun Heo 	int i;
42233267325STejun Heo 
423b1c72916STejun Heo 	/* allow n.15/16 for devices attached to host port */
424b1c72916STejun Heo 	if (ata_is_host_link(dev->link))
425b1c72916STejun Heo 		alt_devno += 15;
42633267325STejun Heo 
42733267325STejun Heo 	for (i = ata_force_tbl_size - 1; i >= 0; i--) {
42833267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
42933267325STejun Heo 		unsigned long pio_mask, mwdma_mask, udma_mask;
43033267325STejun Heo 
43133267325STejun Heo 		if (fe->port != -1 && fe->port != dev->link->ap->print_id)
43233267325STejun Heo 			continue;
43333267325STejun Heo 
43433267325STejun Heo 		if (fe->device != -1 && fe->device != devno &&
43533267325STejun Heo 		    fe->device != alt_devno)
43633267325STejun Heo 			continue;
43733267325STejun Heo 
43833267325STejun Heo 		if (!fe->param.xfer_mask)
43933267325STejun Heo 			continue;
44033267325STejun Heo 
44133267325STejun Heo 		ata_unpack_xfermask(fe->param.xfer_mask,
44233267325STejun Heo 				    &pio_mask, &mwdma_mask, &udma_mask);
44333267325STejun Heo 		if (udma_mask)
44433267325STejun Heo 			dev->udma_mask = udma_mask;
44533267325STejun Heo 		else if (mwdma_mask) {
44633267325STejun Heo 			dev->udma_mask = 0;
44733267325STejun Heo 			dev->mwdma_mask = mwdma_mask;
44833267325STejun Heo 		} else {
44933267325STejun Heo 			dev->udma_mask = 0;
45033267325STejun Heo 			dev->mwdma_mask = 0;
45133267325STejun Heo 			dev->pio_mask = pio_mask;
45233267325STejun Heo 		}
45333267325STejun Heo 
454a9a79dfeSJoe Perches 		ata_dev_notice(dev, "FORCE: xfer_mask set to %s\n",
455a9a79dfeSJoe Perches 			       fe->param.name);
45633267325STejun Heo 		return;
45733267325STejun Heo 	}
45833267325STejun Heo }
45933267325STejun Heo 
46033267325STejun Heo /**
46133267325STejun Heo  *	ata_force_horkage - force horkage according to libata.force
46233267325STejun Heo  *	@dev: ATA device of interest
46333267325STejun Heo  *
46433267325STejun Heo  *	Force horkage according to libata.force and whine about it.
46533267325STejun Heo  *	For consistency with link selection, device number 15 selects
46633267325STejun Heo  *	the first device connected to the host link.
46733267325STejun Heo  *
46833267325STejun Heo  *	LOCKING:
46933267325STejun Heo  *	EH context.
47033267325STejun Heo  */
47133267325STejun Heo static void ata_force_horkage(struct ata_device *dev)
47233267325STejun Heo {
47333267325STejun Heo 	int devno = dev->link->pmp + dev->devno;
47433267325STejun Heo 	int alt_devno = devno;
47533267325STejun Heo 	int i;
47633267325STejun Heo 
477b1c72916STejun Heo 	/* allow n.15/16 for devices attached to host port */
478b1c72916STejun Heo 	if (ata_is_host_link(dev->link))
479b1c72916STejun Heo 		alt_devno += 15;
48033267325STejun Heo 
48133267325STejun Heo 	for (i = 0; i < ata_force_tbl_size; i++) {
48233267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
48333267325STejun Heo 
48433267325STejun Heo 		if (fe->port != -1 && fe->port != dev->link->ap->print_id)
48533267325STejun Heo 			continue;
48633267325STejun Heo 
48733267325STejun Heo 		if (fe->device != -1 && fe->device != devno &&
48833267325STejun Heo 		    fe->device != alt_devno)
48933267325STejun Heo 			continue;
49033267325STejun Heo 
49133267325STejun Heo 		if (!(~dev->horkage & fe->param.horkage_on) &&
49233267325STejun Heo 		    !(dev->horkage & fe->param.horkage_off))
49333267325STejun Heo 			continue;
49433267325STejun Heo 
49533267325STejun Heo 		dev->horkage |= fe->param.horkage_on;
49633267325STejun Heo 		dev->horkage &= ~fe->param.horkage_off;
49733267325STejun Heo 
498a9a79dfeSJoe Perches 		ata_dev_notice(dev, "FORCE: horkage modified (%s)\n",
499a9a79dfeSJoe Perches 			       fe->param.name);
50033267325STejun Heo 	}
50133267325STejun Heo }
50233267325STejun Heo 
50333267325STejun Heo /**
504436d34b3STejun Heo  *	atapi_cmd_type - Determine ATAPI command type from SCSI opcode
505436d34b3STejun Heo  *	@opcode: SCSI opcode
506436d34b3STejun Heo  *
507436d34b3STejun Heo  *	Determine ATAPI command type from @opcode.
508436d34b3STejun Heo  *
509436d34b3STejun Heo  *	LOCKING:
510436d34b3STejun Heo  *	None.
511436d34b3STejun Heo  *
512436d34b3STejun Heo  *	RETURNS:
513436d34b3STejun Heo  *	ATAPI_{READ|WRITE|READ_CD|PASS_THRU|MISC}
514436d34b3STejun Heo  */
515436d34b3STejun Heo int atapi_cmd_type(u8 opcode)
516436d34b3STejun Heo {
517436d34b3STejun Heo 	switch (opcode) {
518436d34b3STejun Heo 	case GPCMD_READ_10:
519436d34b3STejun Heo 	case GPCMD_READ_12:
520436d34b3STejun Heo 		return ATAPI_READ;
521436d34b3STejun Heo 
522436d34b3STejun Heo 	case GPCMD_WRITE_10:
523436d34b3STejun Heo 	case GPCMD_WRITE_12:
524436d34b3STejun Heo 	case GPCMD_WRITE_AND_VERIFY_10:
525436d34b3STejun Heo 		return ATAPI_WRITE;
526436d34b3STejun Heo 
527436d34b3STejun Heo 	case GPCMD_READ_CD:
528436d34b3STejun Heo 	case GPCMD_READ_CD_MSF:
529436d34b3STejun Heo 		return ATAPI_READ_CD;
530436d34b3STejun Heo 
531e52dcc48STejun Heo 	case ATA_16:
532e52dcc48STejun Heo 	case ATA_12:
533e52dcc48STejun Heo 		if (atapi_passthru16)
534e52dcc48STejun Heo 			return ATAPI_PASS_THRU;
535e52dcc48STejun Heo 		/* fall thru */
536436d34b3STejun Heo 	default:
537436d34b3STejun Heo 		return ATAPI_MISC;
538436d34b3STejun Heo 	}
539436d34b3STejun Heo }
540436d34b3STejun Heo 
541436d34b3STejun Heo /**
542c6fd2807SJeff Garzik  *	ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
543c6fd2807SJeff Garzik  *	@tf: Taskfile to convert
544c6fd2807SJeff Garzik  *	@pmp: Port multiplier port
5459977126cSTejun Heo  *	@is_cmd: This FIS is for command
5469977126cSTejun Heo  *	@fis: Buffer into which data will output
547c6fd2807SJeff Garzik  *
548c6fd2807SJeff Garzik  *	Converts a standard ATA taskfile to a Serial ATA
549c6fd2807SJeff Garzik  *	FIS structure (Register - Host to Device).
550c6fd2807SJeff Garzik  *
551c6fd2807SJeff Garzik  *	LOCKING:
552c6fd2807SJeff Garzik  *	Inherited from caller.
553c6fd2807SJeff Garzik  */
5549977126cSTejun Heo void ata_tf_to_fis(const struct ata_taskfile *tf, u8 pmp, int is_cmd, u8 *fis)
555c6fd2807SJeff Garzik {
556c6fd2807SJeff Garzik 	fis[0] = 0x27;			/* Register - Host to Device FIS */
5579977126cSTejun Heo 	fis[1] = pmp & 0xf;		/* Port multiplier number*/
5589977126cSTejun Heo 	if (is_cmd)
5599977126cSTejun Heo 		fis[1] |= (1 << 7);	/* bit 7 indicates Command FIS */
5609977126cSTejun Heo 
561c6fd2807SJeff Garzik 	fis[2] = tf->command;
562c6fd2807SJeff Garzik 	fis[3] = tf->feature;
563c6fd2807SJeff Garzik 
564c6fd2807SJeff Garzik 	fis[4] = tf->lbal;
565c6fd2807SJeff Garzik 	fis[5] = tf->lbam;
566c6fd2807SJeff Garzik 	fis[6] = tf->lbah;
567c6fd2807SJeff Garzik 	fis[7] = tf->device;
568c6fd2807SJeff Garzik 
569c6fd2807SJeff Garzik 	fis[8] = tf->hob_lbal;
570c6fd2807SJeff Garzik 	fis[9] = tf->hob_lbam;
571c6fd2807SJeff Garzik 	fis[10] = tf->hob_lbah;
572c6fd2807SJeff Garzik 	fis[11] = tf->hob_feature;
573c6fd2807SJeff Garzik 
574c6fd2807SJeff Garzik 	fis[12] = tf->nsect;
575c6fd2807SJeff Garzik 	fis[13] = tf->hob_nsect;
576c6fd2807SJeff Garzik 	fis[14] = 0;
577c6fd2807SJeff Garzik 	fis[15] = tf->ctl;
578c6fd2807SJeff Garzik 
57986a565e6SMarc Carino 	fis[16] = tf->auxiliary & 0xff;
58086a565e6SMarc Carino 	fis[17] = (tf->auxiliary >> 8) & 0xff;
58186a565e6SMarc Carino 	fis[18] = (tf->auxiliary >> 16) & 0xff;
58286a565e6SMarc Carino 	fis[19] = (tf->auxiliary >> 24) & 0xff;
583c6fd2807SJeff Garzik }
584c6fd2807SJeff Garzik 
585c6fd2807SJeff Garzik /**
586c6fd2807SJeff Garzik  *	ata_tf_from_fis - Convert SATA FIS to ATA taskfile
587c6fd2807SJeff Garzik  *	@fis: Buffer from which data will be input
588c6fd2807SJeff Garzik  *	@tf: Taskfile to output
589c6fd2807SJeff Garzik  *
590c6fd2807SJeff Garzik  *	Converts a serial ATA FIS structure to a standard ATA taskfile.
591c6fd2807SJeff Garzik  *
592c6fd2807SJeff Garzik  *	LOCKING:
593c6fd2807SJeff Garzik  *	Inherited from caller.
594c6fd2807SJeff Garzik  */
595c6fd2807SJeff Garzik 
596c6fd2807SJeff Garzik void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
597c6fd2807SJeff Garzik {
598c6fd2807SJeff Garzik 	tf->command	= fis[2];	/* status */
599c6fd2807SJeff Garzik 	tf->feature	= fis[3];	/* error */
600c6fd2807SJeff Garzik 
601c6fd2807SJeff Garzik 	tf->lbal	= fis[4];
602c6fd2807SJeff Garzik 	tf->lbam	= fis[5];
603c6fd2807SJeff Garzik 	tf->lbah	= fis[6];
604c6fd2807SJeff Garzik 	tf->device	= fis[7];
605c6fd2807SJeff Garzik 
606c6fd2807SJeff Garzik 	tf->hob_lbal	= fis[8];
607c6fd2807SJeff Garzik 	tf->hob_lbam	= fis[9];
608c6fd2807SJeff Garzik 	tf->hob_lbah	= fis[10];
609c6fd2807SJeff Garzik 
610c6fd2807SJeff Garzik 	tf->nsect	= fis[12];
611c6fd2807SJeff Garzik 	tf->hob_nsect	= fis[13];
612c6fd2807SJeff Garzik }
613c6fd2807SJeff Garzik 
614c6fd2807SJeff Garzik static const u8 ata_rw_cmds[] = {
615c6fd2807SJeff Garzik 	/* pio multi */
616c6fd2807SJeff Garzik 	ATA_CMD_READ_MULTI,
617c6fd2807SJeff Garzik 	ATA_CMD_WRITE_MULTI,
618c6fd2807SJeff Garzik 	ATA_CMD_READ_MULTI_EXT,
619c6fd2807SJeff Garzik 	ATA_CMD_WRITE_MULTI_EXT,
620c6fd2807SJeff Garzik 	0,
621c6fd2807SJeff Garzik 	0,
622c6fd2807SJeff Garzik 	0,
623c6fd2807SJeff Garzik 	ATA_CMD_WRITE_MULTI_FUA_EXT,
624c6fd2807SJeff Garzik 	/* pio */
625c6fd2807SJeff Garzik 	ATA_CMD_PIO_READ,
626c6fd2807SJeff Garzik 	ATA_CMD_PIO_WRITE,
627c6fd2807SJeff Garzik 	ATA_CMD_PIO_READ_EXT,
628c6fd2807SJeff Garzik 	ATA_CMD_PIO_WRITE_EXT,
629c6fd2807SJeff Garzik 	0,
630c6fd2807SJeff Garzik 	0,
631c6fd2807SJeff Garzik 	0,
632c6fd2807SJeff Garzik 	0,
633c6fd2807SJeff Garzik 	/* dma */
634c6fd2807SJeff Garzik 	ATA_CMD_READ,
635c6fd2807SJeff Garzik 	ATA_CMD_WRITE,
636c6fd2807SJeff Garzik 	ATA_CMD_READ_EXT,
637c6fd2807SJeff Garzik 	ATA_CMD_WRITE_EXT,
638c6fd2807SJeff Garzik 	0,
639c6fd2807SJeff Garzik 	0,
640c6fd2807SJeff Garzik 	0,
641c6fd2807SJeff Garzik 	ATA_CMD_WRITE_FUA_EXT
642c6fd2807SJeff Garzik };
643c6fd2807SJeff Garzik 
644c6fd2807SJeff Garzik /**
645c6fd2807SJeff Garzik  *	ata_rwcmd_protocol - set taskfile r/w commands and protocol
646bd056d7eSTejun Heo  *	@tf: command to examine and configure
647bd056d7eSTejun Heo  *	@dev: device tf belongs to
648c6fd2807SJeff Garzik  *
649c6fd2807SJeff Garzik  *	Examine the device configuration and tf->flags to calculate
650c6fd2807SJeff Garzik  *	the proper read/write commands and protocol to use.
651c6fd2807SJeff Garzik  *
652c6fd2807SJeff Garzik  *	LOCKING:
653c6fd2807SJeff Garzik  *	caller.
654c6fd2807SJeff Garzik  */
655bd056d7eSTejun Heo static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev)
656c6fd2807SJeff Garzik {
657c6fd2807SJeff Garzik 	u8 cmd;
658c6fd2807SJeff Garzik 
659c6fd2807SJeff Garzik 	int index, fua, lba48, write;
660c6fd2807SJeff Garzik 
661c6fd2807SJeff Garzik 	fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
662c6fd2807SJeff Garzik 	lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
663c6fd2807SJeff Garzik 	write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
664c6fd2807SJeff Garzik 
665c6fd2807SJeff Garzik 	if (dev->flags & ATA_DFLAG_PIO) {
666c6fd2807SJeff Garzik 		tf->protocol = ATA_PROT_PIO;
667c6fd2807SJeff Garzik 		index = dev->multi_count ? 0 : 8;
6689af5c9c9STejun Heo 	} else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) {
669c6fd2807SJeff Garzik 		/* Unable to use DMA due to host limitation */
670c6fd2807SJeff Garzik 		tf->protocol = ATA_PROT_PIO;
671c6fd2807SJeff Garzik 		index = dev->multi_count ? 0 : 8;
672c6fd2807SJeff Garzik 	} else {
673c6fd2807SJeff Garzik 		tf->protocol = ATA_PROT_DMA;
674c6fd2807SJeff Garzik 		index = 16;
675c6fd2807SJeff Garzik 	}
676c6fd2807SJeff Garzik 
677c6fd2807SJeff Garzik 	cmd = ata_rw_cmds[index + fua + lba48 + write];
678c6fd2807SJeff Garzik 	if (cmd) {
679c6fd2807SJeff Garzik 		tf->command = cmd;
680c6fd2807SJeff Garzik 		return 0;
681c6fd2807SJeff Garzik 	}
682c6fd2807SJeff Garzik 	return -1;
683c6fd2807SJeff Garzik }
684c6fd2807SJeff Garzik 
685c6fd2807SJeff Garzik /**
68635b649feSTejun Heo  *	ata_tf_read_block - Read block address from ATA taskfile
68735b649feSTejun Heo  *	@tf: ATA taskfile of interest
68835b649feSTejun Heo  *	@dev: ATA device @tf belongs to
68935b649feSTejun Heo  *
69035b649feSTejun Heo  *	LOCKING:
69135b649feSTejun Heo  *	None.
69235b649feSTejun Heo  *
69335b649feSTejun Heo  *	Read block address from @tf.  This function can handle all
69435b649feSTejun Heo  *	three address formats - LBA, LBA48 and CHS.  tf->protocol and
69535b649feSTejun Heo  *	flags select the address format to use.
69635b649feSTejun Heo  *
69735b649feSTejun Heo  *	RETURNS:
69835b649feSTejun Heo  *	Block address read from @tf.
69935b649feSTejun Heo  */
700cffd1ee9SHannes Reinecke u64 ata_tf_read_block(const struct ata_taskfile *tf, struct ata_device *dev)
70135b649feSTejun Heo {
70235b649feSTejun Heo 	u64 block = 0;
70335b649feSTejun Heo 
704fe16d4f2STejun Heo 	if (tf->flags & ATA_TFLAG_LBA) {
70535b649feSTejun Heo 		if (tf->flags & ATA_TFLAG_LBA48) {
70635b649feSTejun Heo 			block |= (u64)tf->hob_lbah << 40;
70735b649feSTejun Heo 			block |= (u64)tf->hob_lbam << 32;
70844901a96SRoland Dreier 			block |= (u64)tf->hob_lbal << 24;
70935b649feSTejun Heo 		} else
71035b649feSTejun Heo 			block |= (tf->device & 0xf) << 24;
71135b649feSTejun Heo 
71235b649feSTejun Heo 		block |= tf->lbah << 16;
71335b649feSTejun Heo 		block |= tf->lbam << 8;
71435b649feSTejun Heo 		block |= tf->lbal;
71535b649feSTejun Heo 	} else {
71635b649feSTejun Heo 		u32 cyl, head, sect;
71735b649feSTejun Heo 
71835b649feSTejun Heo 		cyl = tf->lbam | (tf->lbah << 8);
71935b649feSTejun Heo 		head = tf->device & 0xf;
72035b649feSTejun Heo 		sect = tf->lbal;
72135b649feSTejun Heo 
722ac8672eaSTejun Heo 		if (!sect) {
723a9a79dfeSJoe Perches 			ata_dev_warn(dev,
724a9a79dfeSJoe Perches 				     "device reported invalid CHS sector 0\n");
725cffd1ee9SHannes Reinecke 			return U64_MAX;
726ac8672eaSTejun Heo 		}
727ac8672eaSTejun Heo 
728ac8672eaSTejun Heo 		block = (cyl * dev->heads + head) * dev->sectors + sect - 1;
72935b649feSTejun Heo 	}
73035b649feSTejun Heo 
73135b649feSTejun Heo 	return block;
73235b649feSTejun Heo }
73335b649feSTejun Heo 
73435b649feSTejun Heo /**
735bd056d7eSTejun Heo  *	ata_build_rw_tf - Build ATA taskfile for given read/write request
736bd056d7eSTejun Heo  *	@tf: Target ATA taskfile
737bd056d7eSTejun Heo  *	@dev: ATA device @tf belongs to
738bd056d7eSTejun Heo  *	@block: Block address
739bd056d7eSTejun Heo  *	@n_block: Number of blocks
740bd056d7eSTejun Heo  *	@tf_flags: RW/FUA etc...
741bd056d7eSTejun Heo  *	@tag: tag
7428e061784SAdam Manzanares  *	@class: IO priority class
743bd056d7eSTejun Heo  *
744bd056d7eSTejun Heo  *	LOCKING:
745bd056d7eSTejun Heo  *	None.
746bd056d7eSTejun Heo  *
747bd056d7eSTejun Heo  *	Build ATA taskfile @tf for read/write request described by
748bd056d7eSTejun Heo  *	@block, @n_block, @tf_flags and @tag on @dev.
749bd056d7eSTejun Heo  *
750bd056d7eSTejun Heo  *	RETURNS:
751bd056d7eSTejun Heo  *
752bd056d7eSTejun Heo  *	0 on success, -ERANGE if the request is too large for @dev,
753bd056d7eSTejun Heo  *	-EINVAL if the request is invalid.
754bd056d7eSTejun Heo  */
755bd056d7eSTejun Heo int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev,
756bd056d7eSTejun Heo 		    u64 block, u32 n_block, unsigned int tf_flags,
7578e061784SAdam Manzanares 		    unsigned int tag, int class)
758bd056d7eSTejun Heo {
759bd056d7eSTejun Heo 	tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
760bd056d7eSTejun Heo 	tf->flags |= tf_flags;
761bd056d7eSTejun Heo 
7622e2cc676SJens Axboe 	if (ata_ncq_enabled(dev) && !ata_tag_internal(tag)) {
763bd056d7eSTejun Heo 		/* yay, NCQ */
764bd056d7eSTejun Heo 		if (!lba_48_ok(block, n_block))
765bd056d7eSTejun Heo 			return -ERANGE;
766bd056d7eSTejun Heo 
767bd056d7eSTejun Heo 		tf->protocol = ATA_PROT_NCQ;
768bd056d7eSTejun Heo 		tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
769bd056d7eSTejun Heo 
770bd056d7eSTejun Heo 		if (tf->flags & ATA_TFLAG_WRITE)
771bd056d7eSTejun Heo 			tf->command = ATA_CMD_FPDMA_WRITE;
772bd056d7eSTejun Heo 		else
773bd056d7eSTejun Heo 			tf->command = ATA_CMD_FPDMA_READ;
774bd056d7eSTejun Heo 
775bd056d7eSTejun Heo 		tf->nsect = tag << 3;
776bd056d7eSTejun Heo 		tf->hob_feature = (n_block >> 8) & 0xff;
777bd056d7eSTejun Heo 		tf->feature = n_block & 0xff;
778bd056d7eSTejun Heo 
779bd056d7eSTejun Heo 		tf->hob_lbah = (block >> 40) & 0xff;
780bd056d7eSTejun Heo 		tf->hob_lbam = (block >> 32) & 0xff;
781bd056d7eSTejun Heo 		tf->hob_lbal = (block >> 24) & 0xff;
782bd056d7eSTejun Heo 		tf->lbah = (block >> 16) & 0xff;
783bd056d7eSTejun Heo 		tf->lbam = (block >> 8) & 0xff;
784bd056d7eSTejun Heo 		tf->lbal = block & 0xff;
785bd056d7eSTejun Heo 
7869ca7cfa4SSergei Shtylyov 		tf->device = ATA_LBA;
787bd056d7eSTejun Heo 		if (tf->flags & ATA_TFLAG_FUA)
788bd056d7eSTejun Heo 			tf->device |= 1 << 7;
7898e061784SAdam Manzanares 
7909f56eca3SAdam Manzanares 		if (dev->flags & ATA_DFLAG_NCQ_PRIO) {
7918e061784SAdam Manzanares 			if (class == IOPRIO_CLASS_RT)
7928e061784SAdam Manzanares 				tf->hob_nsect |= ATA_PRIO_HIGH <<
7938e061784SAdam Manzanares 						 ATA_SHIFT_PRIO;
7948e061784SAdam Manzanares 		}
795bd056d7eSTejun Heo 	} else if (dev->flags & ATA_DFLAG_LBA) {
796bd056d7eSTejun Heo 		tf->flags |= ATA_TFLAG_LBA;
797bd056d7eSTejun Heo 
798bd056d7eSTejun Heo 		if (lba_28_ok(block, n_block)) {
799bd056d7eSTejun Heo 			/* use LBA28 */
800bd056d7eSTejun Heo 			tf->device |= (block >> 24) & 0xf;
801bd056d7eSTejun Heo 		} else if (lba_48_ok(block, n_block)) {
802bd056d7eSTejun Heo 			if (!(dev->flags & ATA_DFLAG_LBA48))
803bd056d7eSTejun Heo 				return -ERANGE;
804bd056d7eSTejun Heo 
805bd056d7eSTejun Heo 			/* use LBA48 */
806bd056d7eSTejun Heo 			tf->flags |= ATA_TFLAG_LBA48;
807bd056d7eSTejun Heo 
808bd056d7eSTejun Heo 			tf->hob_nsect = (n_block >> 8) & 0xff;
809bd056d7eSTejun Heo 
810bd056d7eSTejun Heo 			tf->hob_lbah = (block >> 40) & 0xff;
811bd056d7eSTejun Heo 			tf->hob_lbam = (block >> 32) & 0xff;
812bd056d7eSTejun Heo 			tf->hob_lbal = (block >> 24) & 0xff;
813bd056d7eSTejun Heo 		} else
814bd056d7eSTejun Heo 			/* request too large even for LBA48 */
815bd056d7eSTejun Heo 			return -ERANGE;
816bd056d7eSTejun Heo 
817bd056d7eSTejun Heo 		if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
818bd056d7eSTejun Heo 			return -EINVAL;
819bd056d7eSTejun Heo 
820bd056d7eSTejun Heo 		tf->nsect = n_block & 0xff;
821bd056d7eSTejun Heo 
822bd056d7eSTejun Heo 		tf->lbah = (block >> 16) & 0xff;
823bd056d7eSTejun Heo 		tf->lbam = (block >> 8) & 0xff;
824bd056d7eSTejun Heo 		tf->lbal = block & 0xff;
825bd056d7eSTejun Heo 
826bd056d7eSTejun Heo 		tf->device |= ATA_LBA;
827bd056d7eSTejun Heo 	} else {
828bd056d7eSTejun Heo 		/* CHS */
829bd056d7eSTejun Heo 		u32 sect, head, cyl, track;
830bd056d7eSTejun Heo 
831bd056d7eSTejun Heo 		/* The request -may- be too large for CHS addressing. */
832bd056d7eSTejun Heo 		if (!lba_28_ok(block, n_block))
833bd056d7eSTejun Heo 			return -ERANGE;
834bd056d7eSTejun Heo 
835bd056d7eSTejun Heo 		if (unlikely(ata_rwcmd_protocol(tf, dev) < 0))
836bd056d7eSTejun Heo 			return -EINVAL;
837bd056d7eSTejun Heo 
838bd056d7eSTejun Heo 		/* Convert LBA to CHS */
839bd056d7eSTejun Heo 		track = (u32)block / dev->sectors;
840bd056d7eSTejun Heo 		cyl   = track / dev->heads;
841bd056d7eSTejun Heo 		head  = track % dev->heads;
842bd056d7eSTejun Heo 		sect  = (u32)block % dev->sectors + 1;
843bd056d7eSTejun Heo 
844bd056d7eSTejun Heo 		DPRINTK("block %u track %u cyl %u head %u sect %u\n",
845bd056d7eSTejun Heo 			(u32)block, track, cyl, head, sect);
846bd056d7eSTejun Heo 
847bd056d7eSTejun Heo 		/* Check whether the converted CHS can fit.
848bd056d7eSTejun Heo 		   Cylinder: 0-65535
849bd056d7eSTejun Heo 		   Head: 0-15
850bd056d7eSTejun Heo 		   Sector: 1-255*/
851bd056d7eSTejun Heo 		if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
852bd056d7eSTejun Heo 			return -ERANGE;
853bd056d7eSTejun Heo 
854bd056d7eSTejun Heo 		tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
855bd056d7eSTejun Heo 		tf->lbal = sect;
856bd056d7eSTejun Heo 		tf->lbam = cyl;
857bd056d7eSTejun Heo 		tf->lbah = cyl >> 8;
858bd056d7eSTejun Heo 		tf->device |= head;
859bd056d7eSTejun Heo 	}
860bd056d7eSTejun Heo 
861bd056d7eSTejun Heo 	return 0;
862bd056d7eSTejun Heo }
863bd056d7eSTejun Heo 
864bd056d7eSTejun Heo /**
865c6fd2807SJeff Garzik  *	ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
866c6fd2807SJeff Garzik  *	@pio_mask: pio_mask
867c6fd2807SJeff Garzik  *	@mwdma_mask: mwdma_mask
868c6fd2807SJeff Garzik  *	@udma_mask: udma_mask
869c6fd2807SJeff Garzik  *
870c6fd2807SJeff Garzik  *	Pack @pio_mask, @mwdma_mask and @udma_mask into a single
871c6fd2807SJeff Garzik  *	unsigned int xfer_mask.
872c6fd2807SJeff Garzik  *
873c6fd2807SJeff Garzik  *	LOCKING:
874c6fd2807SJeff Garzik  *	None.
875c6fd2807SJeff Garzik  *
876c6fd2807SJeff Garzik  *	RETURNS:
877c6fd2807SJeff Garzik  *	Packed xfer_mask.
878c6fd2807SJeff Garzik  */
8797dc951aeSTejun Heo unsigned long ata_pack_xfermask(unsigned long pio_mask,
8807dc951aeSTejun Heo 				unsigned long mwdma_mask,
8817dc951aeSTejun Heo 				unsigned long udma_mask)
882c6fd2807SJeff Garzik {
883c6fd2807SJeff Garzik 	return ((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
884c6fd2807SJeff Garzik 		((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
885c6fd2807SJeff Garzik 		((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
886c6fd2807SJeff Garzik }
887c6fd2807SJeff Garzik 
888c6fd2807SJeff Garzik /**
889c6fd2807SJeff Garzik  *	ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
890c6fd2807SJeff Garzik  *	@xfer_mask: xfer_mask to unpack
891c6fd2807SJeff Garzik  *	@pio_mask: resulting pio_mask
892c6fd2807SJeff Garzik  *	@mwdma_mask: resulting mwdma_mask
893c6fd2807SJeff Garzik  *	@udma_mask: resulting udma_mask
894c6fd2807SJeff Garzik  *
895c6fd2807SJeff Garzik  *	Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
896c9b5560aSMasanari Iida  *	Any NULL destination masks will be ignored.
897c6fd2807SJeff Garzik  */
8987dc951aeSTejun Heo void ata_unpack_xfermask(unsigned long xfer_mask, unsigned long *pio_mask,
8997dc951aeSTejun Heo 			 unsigned long *mwdma_mask, unsigned long *udma_mask)
900c6fd2807SJeff Garzik {
901c6fd2807SJeff Garzik 	if (pio_mask)
902c6fd2807SJeff Garzik 		*pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
903c6fd2807SJeff Garzik 	if (mwdma_mask)
904c6fd2807SJeff Garzik 		*mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
905c6fd2807SJeff Garzik 	if (udma_mask)
906c6fd2807SJeff Garzik 		*udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
907c6fd2807SJeff Garzik }
908c6fd2807SJeff Garzik 
909c6fd2807SJeff Garzik static const struct ata_xfer_ent {
910c6fd2807SJeff Garzik 	int shift, bits;
911c6fd2807SJeff Garzik 	u8 base;
912c6fd2807SJeff Garzik } ata_xfer_tbl[] = {
91370cd071eSTejun Heo 	{ ATA_SHIFT_PIO, ATA_NR_PIO_MODES, XFER_PIO_0 },
91470cd071eSTejun Heo 	{ ATA_SHIFT_MWDMA, ATA_NR_MWDMA_MODES, XFER_MW_DMA_0 },
91570cd071eSTejun Heo 	{ ATA_SHIFT_UDMA, ATA_NR_UDMA_MODES, XFER_UDMA_0 },
916c6fd2807SJeff Garzik 	{ -1, },
917c6fd2807SJeff Garzik };
918c6fd2807SJeff Garzik 
919c6fd2807SJeff Garzik /**
920c6fd2807SJeff Garzik  *	ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
921c6fd2807SJeff Garzik  *	@xfer_mask: xfer_mask of interest
922c6fd2807SJeff Garzik  *
923c6fd2807SJeff Garzik  *	Return matching XFER_* value for @xfer_mask.  Only the highest
924c6fd2807SJeff Garzik  *	bit of @xfer_mask is considered.
925c6fd2807SJeff Garzik  *
926c6fd2807SJeff Garzik  *	LOCKING:
927c6fd2807SJeff Garzik  *	None.
928c6fd2807SJeff Garzik  *
929c6fd2807SJeff Garzik  *	RETURNS:
93070cd071eSTejun Heo  *	Matching XFER_* value, 0xff if no match found.
931c6fd2807SJeff Garzik  */
9327dc951aeSTejun Heo u8 ata_xfer_mask2mode(unsigned long xfer_mask)
933c6fd2807SJeff Garzik {
934c6fd2807SJeff Garzik 	int highbit = fls(xfer_mask) - 1;
935c6fd2807SJeff Garzik 	const struct ata_xfer_ent *ent;
936c6fd2807SJeff Garzik 
937c6fd2807SJeff Garzik 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
938c6fd2807SJeff Garzik 		if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
939c6fd2807SJeff Garzik 			return ent->base + highbit - ent->shift;
94070cd071eSTejun Heo 	return 0xff;
941c6fd2807SJeff Garzik }
942c6fd2807SJeff Garzik 
943c6fd2807SJeff Garzik /**
944c6fd2807SJeff Garzik  *	ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
945c6fd2807SJeff Garzik  *	@xfer_mode: XFER_* of interest
946c6fd2807SJeff Garzik  *
947c6fd2807SJeff Garzik  *	Return matching xfer_mask for @xfer_mode.
948c6fd2807SJeff Garzik  *
949c6fd2807SJeff Garzik  *	LOCKING:
950c6fd2807SJeff Garzik  *	None.
951c6fd2807SJeff Garzik  *
952c6fd2807SJeff Garzik  *	RETURNS:
953c6fd2807SJeff Garzik  *	Matching xfer_mask, 0 if no match found.
954c6fd2807SJeff Garzik  */
9557dc951aeSTejun Heo unsigned long ata_xfer_mode2mask(u8 xfer_mode)
956c6fd2807SJeff Garzik {
957c6fd2807SJeff Garzik 	const struct ata_xfer_ent *ent;
958c6fd2807SJeff Garzik 
959c6fd2807SJeff Garzik 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
960c6fd2807SJeff Garzik 		if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
96170cd071eSTejun Heo 			return ((2 << (ent->shift + xfer_mode - ent->base)) - 1)
96270cd071eSTejun Heo 				& ~((1 << ent->shift) - 1);
963c6fd2807SJeff Garzik 	return 0;
964c6fd2807SJeff Garzik }
965c6fd2807SJeff Garzik 
966c6fd2807SJeff Garzik /**
967c6fd2807SJeff Garzik  *	ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
968c6fd2807SJeff Garzik  *	@xfer_mode: XFER_* of interest
969c6fd2807SJeff Garzik  *
970c6fd2807SJeff Garzik  *	Return matching xfer_shift for @xfer_mode.
971c6fd2807SJeff Garzik  *
972c6fd2807SJeff Garzik  *	LOCKING:
973c6fd2807SJeff Garzik  *	None.
974c6fd2807SJeff Garzik  *
975c6fd2807SJeff Garzik  *	RETURNS:
976c6fd2807SJeff Garzik  *	Matching xfer_shift, -1 if no match found.
977c6fd2807SJeff Garzik  */
9787dc951aeSTejun Heo int ata_xfer_mode2shift(unsigned long xfer_mode)
979c6fd2807SJeff Garzik {
980c6fd2807SJeff Garzik 	const struct ata_xfer_ent *ent;
981c6fd2807SJeff Garzik 
982c6fd2807SJeff Garzik 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
983c6fd2807SJeff Garzik 		if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
984c6fd2807SJeff Garzik 			return ent->shift;
985c6fd2807SJeff Garzik 	return -1;
986c6fd2807SJeff Garzik }
987c6fd2807SJeff Garzik 
988c6fd2807SJeff Garzik /**
989c6fd2807SJeff Garzik  *	ata_mode_string - convert xfer_mask to string
990c6fd2807SJeff Garzik  *	@xfer_mask: mask of bits supported; only highest bit counts.
991c6fd2807SJeff Garzik  *
992c6fd2807SJeff Garzik  *	Determine string which represents the highest speed
993c6fd2807SJeff Garzik  *	(highest bit in @modemask).
994c6fd2807SJeff Garzik  *
995c6fd2807SJeff Garzik  *	LOCKING:
996c6fd2807SJeff Garzik  *	None.
997c6fd2807SJeff Garzik  *
998c6fd2807SJeff Garzik  *	RETURNS:
999c6fd2807SJeff Garzik  *	Constant C string representing highest speed listed in
1000c6fd2807SJeff Garzik  *	@mode_mask, or the constant C string "<n/a>".
1001c6fd2807SJeff Garzik  */
10027dc951aeSTejun Heo const char *ata_mode_string(unsigned long xfer_mask)
1003c6fd2807SJeff Garzik {
1004c6fd2807SJeff Garzik 	static const char * const xfer_mode_str[] = {
1005c6fd2807SJeff Garzik 		"PIO0",
1006c6fd2807SJeff Garzik 		"PIO1",
1007c6fd2807SJeff Garzik 		"PIO2",
1008c6fd2807SJeff Garzik 		"PIO3",
1009c6fd2807SJeff Garzik 		"PIO4",
1010b352e57dSAlan Cox 		"PIO5",
1011b352e57dSAlan Cox 		"PIO6",
1012c6fd2807SJeff Garzik 		"MWDMA0",
1013c6fd2807SJeff Garzik 		"MWDMA1",
1014c6fd2807SJeff Garzik 		"MWDMA2",
1015b352e57dSAlan Cox 		"MWDMA3",
1016b352e57dSAlan Cox 		"MWDMA4",
1017c6fd2807SJeff Garzik 		"UDMA/16",
1018c6fd2807SJeff Garzik 		"UDMA/25",
1019c6fd2807SJeff Garzik 		"UDMA/33",
1020c6fd2807SJeff Garzik 		"UDMA/44",
1021c6fd2807SJeff Garzik 		"UDMA/66",
1022c6fd2807SJeff Garzik 		"UDMA/100",
1023c6fd2807SJeff Garzik 		"UDMA/133",
1024c6fd2807SJeff Garzik 		"UDMA7",
1025c6fd2807SJeff Garzik 	};
1026c6fd2807SJeff Garzik 	int highbit;
1027c6fd2807SJeff Garzik 
1028c6fd2807SJeff Garzik 	highbit = fls(xfer_mask) - 1;
1029c6fd2807SJeff Garzik 	if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
1030c6fd2807SJeff Garzik 		return xfer_mode_str[highbit];
1031c6fd2807SJeff Garzik 	return "<n/a>";
1032c6fd2807SJeff Garzik }
1033c6fd2807SJeff Garzik 
1034d9027470SGwendal Grignou const char *sata_spd_string(unsigned int spd)
1035c6fd2807SJeff Garzik {
1036c6fd2807SJeff Garzik 	static const char * const spd_str[] = {
1037c6fd2807SJeff Garzik 		"1.5 Gbps",
1038c6fd2807SJeff Garzik 		"3.0 Gbps",
10398522ee25SShane Huang 		"6.0 Gbps",
1040c6fd2807SJeff Garzik 	};
1041c6fd2807SJeff Garzik 
1042c6fd2807SJeff Garzik 	if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
1043c6fd2807SJeff Garzik 		return "<unknown>";
1044c6fd2807SJeff Garzik 	return spd_str[spd - 1];
1045c6fd2807SJeff Garzik }
1046c6fd2807SJeff Garzik 
1047c6fd2807SJeff Garzik /**
1048c6fd2807SJeff Garzik  *	ata_dev_classify - determine device type based on ATA-spec signature
1049c6fd2807SJeff Garzik  *	@tf: ATA taskfile register set for device to be identified
1050c6fd2807SJeff Garzik  *
1051c6fd2807SJeff Garzik  *	Determine from taskfile register contents whether a device is
1052c6fd2807SJeff Garzik  *	ATA or ATAPI, as per "Signature and persistence" section
1053c6fd2807SJeff Garzik  *	of ATA/PI spec (volume 1, sect 5.14).
1054c6fd2807SJeff Garzik  *
1055c6fd2807SJeff Garzik  *	LOCKING:
1056c6fd2807SJeff Garzik  *	None.
1057c6fd2807SJeff Garzik  *
1058c6fd2807SJeff Garzik  *	RETURNS:
10599162c657SHannes Reinecke  *	Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP,
10609162c657SHannes Reinecke  *	%ATA_DEV_ZAC, or %ATA_DEV_UNKNOWN the event of failure.
1061c6fd2807SJeff Garzik  */
1062c6fd2807SJeff Garzik unsigned int ata_dev_classify(const struct ata_taskfile *tf)
1063c6fd2807SJeff Garzik {
1064c6fd2807SJeff Garzik 	/* Apple's open source Darwin code hints that some devices only
1065c6fd2807SJeff Garzik 	 * put a proper signature into the LBA mid/high registers,
1066c6fd2807SJeff Garzik 	 * So, we only check those.  It's sufficient for uniqueness.
1067633273a3STejun Heo 	 *
1068633273a3STejun Heo 	 * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate
1069633273a3STejun Heo 	 * signatures for ATA and ATAPI devices attached on SerialATA,
1070633273a3STejun Heo 	 * 0x3c/0xc3 and 0x69/0x96 respectively.  However, SerialATA
1071633273a3STejun Heo 	 * spec has never mentioned about using different signatures
1072633273a3STejun Heo 	 * for ATA/ATAPI devices.  Then, Serial ATA II: Port
1073633273a3STejun Heo 	 * Multiplier specification began to use 0x69/0x96 to identify
1074633273a3STejun Heo 	 * port multpliers and 0x3c/0xc3 to identify SEMB device.
1075633273a3STejun Heo 	 * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and
1076633273a3STejun Heo 	 * 0x69/0x96 shortly and described them as reserved for
1077633273a3STejun Heo 	 * SerialATA.
1078633273a3STejun Heo 	 *
1079633273a3STejun Heo 	 * We follow the current spec and consider that 0x69/0x96
1080633273a3STejun Heo 	 * identifies a port multiplier and 0x3c/0xc3 a SEMB device.
108179b42babSTejun Heo 	 * Unfortunately, WDC WD1600JS-62MHB5 (a hard drive) reports
108279b42babSTejun Heo 	 * SEMB signature.  This is worked around in
108379b42babSTejun Heo 	 * ata_dev_read_id().
1084c6fd2807SJeff Garzik 	 */
1085633273a3STejun Heo 	if ((tf->lbam == 0) && (tf->lbah == 0)) {
1086c6fd2807SJeff Garzik 		DPRINTK("found ATA device by sig\n");
1087c6fd2807SJeff Garzik 		return ATA_DEV_ATA;
1088c6fd2807SJeff Garzik 	}
1089c6fd2807SJeff Garzik 
1090633273a3STejun Heo 	if ((tf->lbam == 0x14) && (tf->lbah == 0xeb)) {
1091c6fd2807SJeff Garzik 		DPRINTK("found ATAPI device by sig\n");
1092c6fd2807SJeff Garzik 		return ATA_DEV_ATAPI;
1093c6fd2807SJeff Garzik 	}
1094c6fd2807SJeff Garzik 
1095633273a3STejun Heo 	if ((tf->lbam == 0x69) && (tf->lbah == 0x96)) {
1096633273a3STejun Heo 		DPRINTK("found PMP device by sig\n");
1097633273a3STejun Heo 		return ATA_DEV_PMP;
1098633273a3STejun Heo 	}
1099633273a3STejun Heo 
1100633273a3STejun Heo 	if ((tf->lbam == 0x3c) && (tf->lbah == 0xc3)) {
110179b42babSTejun Heo 		DPRINTK("found SEMB device by sig (could be ATA device)\n");
110279b42babSTejun Heo 		return ATA_DEV_SEMB;
1103633273a3STejun Heo 	}
1104633273a3STejun Heo 
11059162c657SHannes Reinecke 	if ((tf->lbam == 0xcd) && (tf->lbah == 0xab)) {
11069162c657SHannes Reinecke 		DPRINTK("found ZAC device by sig\n");
11079162c657SHannes Reinecke 		return ATA_DEV_ZAC;
11089162c657SHannes Reinecke 	}
11099162c657SHannes Reinecke 
1110c6fd2807SJeff Garzik 	DPRINTK("unknown device\n");
1111c6fd2807SJeff Garzik 	return ATA_DEV_UNKNOWN;
1112c6fd2807SJeff Garzik }
1113c6fd2807SJeff Garzik 
1114c6fd2807SJeff Garzik /**
1115c6fd2807SJeff Garzik  *	ata_id_string - Convert IDENTIFY DEVICE page into string
1116c6fd2807SJeff Garzik  *	@id: IDENTIFY DEVICE results we will examine
1117c6fd2807SJeff Garzik  *	@s: string into which data is output
1118c6fd2807SJeff Garzik  *	@ofs: offset into identify device page
1119c6fd2807SJeff Garzik  *	@len: length of string to return. must be an even number.
1120c6fd2807SJeff Garzik  *
1121c6fd2807SJeff Garzik  *	The strings in the IDENTIFY DEVICE page are broken up into
1122c6fd2807SJeff Garzik  *	16-bit chunks.  Run through the string, and output each
1123c6fd2807SJeff Garzik  *	8-bit chunk linearly, regardless of platform.
1124c6fd2807SJeff Garzik  *
1125c6fd2807SJeff Garzik  *	LOCKING:
1126c6fd2807SJeff Garzik  *	caller.
1127c6fd2807SJeff Garzik  */
1128c6fd2807SJeff Garzik 
1129c6fd2807SJeff Garzik void ata_id_string(const u16 *id, unsigned char *s,
1130c6fd2807SJeff Garzik 		   unsigned int ofs, unsigned int len)
1131c6fd2807SJeff Garzik {
1132c6fd2807SJeff Garzik 	unsigned int c;
1133c6fd2807SJeff Garzik 
1134963e4975SAlan Cox 	BUG_ON(len & 1);
1135963e4975SAlan Cox 
1136c6fd2807SJeff Garzik 	while (len > 0) {
1137c6fd2807SJeff Garzik 		c = id[ofs] >> 8;
1138c6fd2807SJeff Garzik 		*s = c;
1139c6fd2807SJeff Garzik 		s++;
1140c6fd2807SJeff Garzik 
1141c6fd2807SJeff Garzik 		c = id[ofs] & 0xff;
1142c6fd2807SJeff Garzik 		*s = c;
1143c6fd2807SJeff Garzik 		s++;
1144c6fd2807SJeff Garzik 
1145c6fd2807SJeff Garzik 		ofs++;
1146c6fd2807SJeff Garzik 		len -= 2;
1147c6fd2807SJeff Garzik 	}
1148c6fd2807SJeff Garzik }
1149c6fd2807SJeff Garzik 
1150c6fd2807SJeff Garzik /**
1151c6fd2807SJeff Garzik  *	ata_id_c_string - Convert IDENTIFY DEVICE page into C string
1152c6fd2807SJeff Garzik  *	@id: IDENTIFY DEVICE results we will examine
1153c6fd2807SJeff Garzik  *	@s: string into which data is output
1154c6fd2807SJeff Garzik  *	@ofs: offset into identify device page
1155c6fd2807SJeff Garzik  *	@len: length of string to return. must be an odd number.
1156c6fd2807SJeff Garzik  *
1157c6fd2807SJeff Garzik  *	This function is identical to ata_id_string except that it
1158c6fd2807SJeff Garzik  *	trims trailing spaces and terminates the resulting string with
1159c6fd2807SJeff Garzik  *	null.  @len must be actual maximum length (even number) + 1.
1160c6fd2807SJeff Garzik  *
1161c6fd2807SJeff Garzik  *	LOCKING:
1162c6fd2807SJeff Garzik  *	caller.
1163c6fd2807SJeff Garzik  */
1164c6fd2807SJeff Garzik void ata_id_c_string(const u16 *id, unsigned char *s,
1165c6fd2807SJeff Garzik 		     unsigned int ofs, unsigned int len)
1166c6fd2807SJeff Garzik {
1167c6fd2807SJeff Garzik 	unsigned char *p;
1168c6fd2807SJeff Garzik 
1169c6fd2807SJeff Garzik 	ata_id_string(id, s, ofs, len - 1);
1170c6fd2807SJeff Garzik 
1171c6fd2807SJeff Garzik 	p = s + strnlen(s, len - 1);
1172c6fd2807SJeff Garzik 	while (p > s && p[-1] == ' ')
1173c6fd2807SJeff Garzik 		p--;
1174c6fd2807SJeff Garzik 	*p = '\0';
1175c6fd2807SJeff Garzik }
1176c6fd2807SJeff Garzik 
1177db6f8759STejun Heo static u64 ata_id_n_sectors(const u16 *id)
1178db6f8759STejun Heo {
1179db6f8759STejun Heo 	if (ata_id_has_lba(id)) {
1180db6f8759STejun Heo 		if (ata_id_has_lba48(id))
1181968e594aSRobert Hancock 			return ata_id_u64(id, ATA_ID_LBA_CAPACITY_2);
1182db6f8759STejun Heo 		else
1183968e594aSRobert Hancock 			return ata_id_u32(id, ATA_ID_LBA_CAPACITY);
1184db6f8759STejun Heo 	} else {
1185db6f8759STejun Heo 		if (ata_id_current_chs_valid(id))
1186968e594aSRobert Hancock 			return id[ATA_ID_CUR_CYLS] * id[ATA_ID_CUR_HEADS] *
1187968e594aSRobert Hancock 			       id[ATA_ID_CUR_SECTORS];
1188db6f8759STejun Heo 		else
1189968e594aSRobert Hancock 			return id[ATA_ID_CYLS] * id[ATA_ID_HEADS] *
1190968e594aSRobert Hancock 			       id[ATA_ID_SECTORS];
1191db6f8759STejun Heo 	}
1192db6f8759STejun Heo }
1193db6f8759STejun Heo 
1194a5987e0aSTejun Heo u64 ata_tf_to_lba48(const struct ata_taskfile *tf)
11951e999736SAlan Cox {
11961e999736SAlan Cox 	u64 sectors = 0;
11971e999736SAlan Cox 
11981e999736SAlan Cox 	sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40;
11991e999736SAlan Cox 	sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32;
1200ba14a9c2SRoland Dreier 	sectors |= ((u64)(tf->hob_lbal & 0xff)) << 24;
12011e999736SAlan Cox 	sectors |= (tf->lbah & 0xff) << 16;
12021e999736SAlan Cox 	sectors |= (tf->lbam & 0xff) << 8;
12031e999736SAlan Cox 	sectors |= (tf->lbal & 0xff);
12041e999736SAlan Cox 
1205a5987e0aSTejun Heo 	return sectors;
12061e999736SAlan Cox }
12071e999736SAlan Cox 
1208a5987e0aSTejun Heo u64 ata_tf_to_lba(const struct ata_taskfile *tf)
12091e999736SAlan Cox {
12101e999736SAlan Cox 	u64 sectors = 0;
12111e999736SAlan Cox 
12121e999736SAlan Cox 	sectors |= (tf->device & 0x0f) << 24;
12131e999736SAlan Cox 	sectors |= (tf->lbah & 0xff) << 16;
12141e999736SAlan Cox 	sectors |= (tf->lbam & 0xff) << 8;
12151e999736SAlan Cox 	sectors |= (tf->lbal & 0xff);
12161e999736SAlan Cox 
1217a5987e0aSTejun Heo 	return sectors;
12181e999736SAlan Cox }
12191e999736SAlan Cox 
12201e999736SAlan Cox /**
1221c728a914STejun Heo  *	ata_read_native_max_address - Read native max address
1222c728a914STejun Heo  *	@dev: target device
1223c728a914STejun Heo  *	@max_sectors: out parameter for the result native max address
12241e999736SAlan Cox  *
1225c728a914STejun Heo  *	Perform an LBA48 or LBA28 native size query upon the device in
1226c728a914STejun Heo  *	question.
1227c728a914STejun Heo  *
1228c728a914STejun Heo  *	RETURNS:
1229c728a914STejun Heo  *	0 on success, -EACCES if command is aborted by the drive.
1230c728a914STejun Heo  *	-EIO on other errors.
12311e999736SAlan Cox  */
1232c728a914STejun Heo static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
12331e999736SAlan Cox {
1234c728a914STejun Heo 	unsigned int err_mask;
12351e999736SAlan Cox 	struct ata_taskfile tf;
1236c728a914STejun Heo 	int lba48 = ata_id_has_lba48(dev->id);
12371e999736SAlan Cox 
12381e999736SAlan Cox 	ata_tf_init(dev, &tf);
12391e999736SAlan Cox 
1240c728a914STejun Heo 	/* always clear all address registers */
12411e999736SAlan Cox 	tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1242c728a914STejun Heo 
1243c728a914STejun Heo 	if (lba48) {
1244c728a914STejun Heo 		tf.command = ATA_CMD_READ_NATIVE_MAX_EXT;
1245c728a914STejun Heo 		tf.flags |= ATA_TFLAG_LBA48;
1246c728a914STejun Heo 	} else
1247c728a914STejun Heo 		tf.command = ATA_CMD_READ_NATIVE_MAX;
1248c728a914STejun Heo 
1249bd18bc04SHannes Reinecke 	tf.protocol = ATA_PROT_NODATA;
1250c728a914STejun Heo 	tf.device |= ATA_LBA;
12511e999736SAlan Cox 
12522b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1253c728a914STejun Heo 	if (err_mask) {
1254a9a79dfeSJoe Perches 		ata_dev_warn(dev,
1255a9a79dfeSJoe Perches 			     "failed to read native max address (err_mask=0x%x)\n",
1256a9a79dfeSJoe Perches 			     err_mask);
1257c728a914STejun Heo 		if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
1258c728a914STejun Heo 			return -EACCES;
1259c728a914STejun Heo 		return -EIO;
1260c728a914STejun Heo 	}
1261c728a914STejun Heo 
1262c728a914STejun Heo 	if (lba48)
1263a5987e0aSTejun Heo 		*max_sectors = ata_tf_to_lba48(&tf) + 1;
1264c728a914STejun Heo 	else
1265a5987e0aSTejun Heo 		*max_sectors = ata_tf_to_lba(&tf) + 1;
126693328e11SAlan Cox 	if (dev->horkage & ATA_HORKAGE_HPA_SIZE)
126793328e11SAlan Cox 		(*max_sectors)--;
12681e999736SAlan Cox 	return 0;
12691e999736SAlan Cox }
12701e999736SAlan Cox 
12711e999736SAlan Cox /**
1272c728a914STejun Heo  *	ata_set_max_sectors - Set max sectors
1273c728a914STejun Heo  *	@dev: target device
12746b38d1d1SRandy Dunlap  *	@new_sectors: new max sectors value to set for the device
12751e999736SAlan Cox  *
1276c728a914STejun Heo  *	Set max sectors of @dev to @new_sectors.
1277c728a914STejun Heo  *
1278c728a914STejun Heo  *	RETURNS:
1279c728a914STejun Heo  *	0 on success, -EACCES if command is aborted or denied (due to
1280c728a914STejun Heo  *	previous non-volatile SET_MAX) by the drive.  -EIO on other
1281c728a914STejun Heo  *	errors.
12821e999736SAlan Cox  */
128305027adcSTejun Heo static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
12841e999736SAlan Cox {
1285c728a914STejun Heo 	unsigned int err_mask;
12861e999736SAlan Cox 	struct ata_taskfile tf;
1287c728a914STejun Heo 	int lba48 = ata_id_has_lba48(dev->id);
12881e999736SAlan Cox 
12891e999736SAlan Cox 	new_sectors--;
12901e999736SAlan Cox 
12911e999736SAlan Cox 	ata_tf_init(dev, &tf);
12921e999736SAlan Cox 
1293c728a914STejun Heo 	tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
12941e999736SAlan Cox 
1295c728a914STejun Heo 	if (lba48) {
1296c728a914STejun Heo 		tf.command = ATA_CMD_SET_MAX_EXT;
1297c728a914STejun Heo 		tf.flags |= ATA_TFLAG_LBA48;
12981e999736SAlan Cox 
12991e999736SAlan Cox 		tf.hob_lbal = (new_sectors >> 24) & 0xff;
13001e999736SAlan Cox 		tf.hob_lbam = (new_sectors >> 32) & 0xff;
13011e999736SAlan Cox 		tf.hob_lbah = (new_sectors >> 40) & 0xff;
13021e582ba4STejun Heo 	} else {
13031e999736SAlan Cox 		tf.command = ATA_CMD_SET_MAX;
1304c728a914STejun Heo 
13051e582ba4STejun Heo 		tf.device |= (new_sectors >> 24) & 0xf;
13061e582ba4STejun Heo 	}
13071e582ba4STejun Heo 
1308bd18bc04SHannes Reinecke 	tf.protocol = ATA_PROT_NODATA;
1309c728a914STejun Heo 	tf.device |= ATA_LBA;
13101e999736SAlan Cox 
13111e999736SAlan Cox 	tf.lbal = (new_sectors >> 0) & 0xff;
13121e999736SAlan Cox 	tf.lbam = (new_sectors >> 8) & 0xff;
13131e999736SAlan Cox 	tf.lbah = (new_sectors >> 16) & 0xff;
13141e999736SAlan Cox 
13152b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1316c728a914STejun Heo 	if (err_mask) {
1317a9a79dfeSJoe Perches 		ata_dev_warn(dev,
1318a9a79dfeSJoe Perches 			     "failed to set max address (err_mask=0x%x)\n",
1319a9a79dfeSJoe Perches 			     err_mask);
1320c728a914STejun Heo 		if (err_mask == AC_ERR_DEV &&
1321c728a914STejun Heo 		    (tf.feature & (ATA_ABORTED | ATA_IDNF)))
1322c728a914STejun Heo 			return -EACCES;
1323c728a914STejun Heo 		return -EIO;
1324c728a914STejun Heo 	}
1325c728a914STejun Heo 
13261e999736SAlan Cox 	return 0;
13271e999736SAlan Cox }
13281e999736SAlan Cox 
13291e999736SAlan Cox /**
13301e999736SAlan Cox  *	ata_hpa_resize		-	Resize a device with an HPA set
13311e999736SAlan Cox  *	@dev: Device to resize
13321e999736SAlan Cox  *
13331e999736SAlan Cox  *	Read the size of an LBA28 or LBA48 disk with HPA features and resize
13341e999736SAlan Cox  *	it if required to the full size of the media. The caller must check
13351e999736SAlan Cox  *	the drive has the HPA feature set enabled.
133605027adcSTejun Heo  *
133705027adcSTejun Heo  *	RETURNS:
133805027adcSTejun Heo  *	0 on success, -errno on failure.
13391e999736SAlan Cox  */
134005027adcSTejun Heo static int ata_hpa_resize(struct ata_device *dev)
13411e999736SAlan Cox {
134205027adcSTejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
134305027adcSTejun Heo 	int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
1344445d211bSTejun Heo 	bool unlock_hpa = ata_ignore_hpa || dev->flags & ATA_DFLAG_UNLOCK_HPA;
134505027adcSTejun Heo 	u64 sectors = ata_id_n_sectors(dev->id);
134605027adcSTejun Heo 	u64 native_sectors;
1347c728a914STejun Heo 	int rc;
13481e999736SAlan Cox 
134905027adcSTejun Heo 	/* do we need to do it? */
13509162c657SHannes Reinecke 	if ((dev->class != ATA_DEV_ATA && dev->class != ATA_DEV_ZAC) ||
135105027adcSTejun Heo 	    !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) ||
135205027adcSTejun Heo 	    (dev->horkage & ATA_HORKAGE_BROKEN_HPA))
1353c728a914STejun Heo 		return 0;
13541e999736SAlan Cox 
135505027adcSTejun Heo 	/* read native max address */
135605027adcSTejun Heo 	rc = ata_read_native_max_address(dev, &native_sectors);
135705027adcSTejun Heo 	if (rc) {
1358dda7aba1STejun Heo 		/* If device aborted the command or HPA isn't going to
1359dda7aba1STejun Heo 		 * be unlocked, skip HPA resizing.
136005027adcSTejun Heo 		 */
1361445d211bSTejun Heo 		if (rc == -EACCES || !unlock_hpa) {
1362a9a79dfeSJoe Perches 			ata_dev_warn(dev,
1363a9a79dfeSJoe Perches 				     "HPA support seems broken, skipping HPA handling\n");
136405027adcSTejun Heo 			dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
136505027adcSTejun Heo 
136605027adcSTejun Heo 			/* we can continue if device aborted the command */
136705027adcSTejun Heo 			if (rc == -EACCES)
136805027adcSTejun Heo 				rc = 0;
136905027adcSTejun Heo 		}
137005027adcSTejun Heo 
137105027adcSTejun Heo 		return rc;
137205027adcSTejun Heo 	}
13735920dadfSTejun Heo 	dev->n_native_sectors = native_sectors;
137405027adcSTejun Heo 
137505027adcSTejun Heo 	/* nothing to do? */
1376445d211bSTejun Heo 	if (native_sectors <= sectors || !unlock_hpa) {
137705027adcSTejun Heo 		if (!print_info || native_sectors == sectors)
137805027adcSTejun Heo 			return 0;
137905027adcSTejun Heo 
138005027adcSTejun Heo 		if (native_sectors > sectors)
1381a9a79dfeSJoe Perches 			ata_dev_info(dev,
138205027adcSTejun Heo 				"HPA detected: current %llu, native %llu\n",
138305027adcSTejun Heo 				(unsigned long long)sectors,
138405027adcSTejun Heo 				(unsigned long long)native_sectors);
138505027adcSTejun Heo 		else if (native_sectors < sectors)
1386a9a79dfeSJoe Perches 			ata_dev_warn(dev,
1387a9a79dfeSJoe Perches 				"native sectors (%llu) is smaller than sectors (%llu)\n",
138805027adcSTejun Heo 				(unsigned long long)native_sectors,
138905027adcSTejun Heo 				(unsigned long long)sectors);
139005027adcSTejun Heo 		return 0;
13911e999736SAlan Cox 	}
139237301a55STejun Heo 
139305027adcSTejun Heo 	/* let's unlock HPA */
139405027adcSTejun Heo 	rc = ata_set_max_sectors(dev, native_sectors);
139505027adcSTejun Heo 	if (rc == -EACCES) {
139605027adcSTejun Heo 		/* if device aborted the command, skip HPA resizing */
1397a9a79dfeSJoe Perches 		ata_dev_warn(dev,
1398a9a79dfeSJoe Perches 			     "device aborted resize (%llu -> %llu), skipping HPA handling\n",
139905027adcSTejun Heo 			     (unsigned long long)sectors,
140005027adcSTejun Heo 			     (unsigned long long)native_sectors);
140105027adcSTejun Heo 		dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
140205027adcSTejun Heo 		return 0;
140305027adcSTejun Heo 	} else if (rc)
140405027adcSTejun Heo 		return rc;
140505027adcSTejun Heo 
140605027adcSTejun Heo 	/* re-read IDENTIFY data */
140705027adcSTejun Heo 	rc = ata_dev_reread_id(dev, 0);
140805027adcSTejun Heo 	if (rc) {
1409a9a79dfeSJoe Perches 		ata_dev_err(dev,
1410a9a79dfeSJoe Perches 			    "failed to re-read IDENTIFY data after HPA resizing\n");
141105027adcSTejun Heo 		return rc;
141205027adcSTejun Heo 	}
141305027adcSTejun Heo 
141405027adcSTejun Heo 	if (print_info) {
141505027adcSTejun Heo 		u64 new_sectors = ata_id_n_sectors(dev->id);
1416a9a79dfeSJoe Perches 		ata_dev_info(dev,
141705027adcSTejun Heo 			"HPA unlocked: %llu -> %llu, native %llu\n",
141805027adcSTejun Heo 			(unsigned long long)sectors,
141905027adcSTejun Heo 			(unsigned long long)new_sectors,
142005027adcSTejun Heo 			(unsigned long long)native_sectors);
142105027adcSTejun Heo 	}
142205027adcSTejun Heo 
142305027adcSTejun Heo 	return 0;
14241e999736SAlan Cox }
14251e999736SAlan Cox 
1426c6fd2807SJeff Garzik /**
1427c6fd2807SJeff Garzik  *	ata_dump_id - IDENTIFY DEVICE info debugging output
1428c6fd2807SJeff Garzik  *	@id: IDENTIFY DEVICE page to dump
1429c6fd2807SJeff Garzik  *
1430c6fd2807SJeff Garzik  *	Dump selected 16-bit words from the given IDENTIFY DEVICE
1431c6fd2807SJeff Garzik  *	page.
1432c6fd2807SJeff Garzik  *
1433c6fd2807SJeff Garzik  *	LOCKING:
1434c6fd2807SJeff Garzik  *	caller.
1435c6fd2807SJeff Garzik  */
1436c6fd2807SJeff Garzik 
1437c6fd2807SJeff Garzik static inline void ata_dump_id(const u16 *id)
1438c6fd2807SJeff Garzik {
1439c6fd2807SJeff Garzik 	DPRINTK("49==0x%04x  "
1440c6fd2807SJeff Garzik 		"53==0x%04x  "
1441c6fd2807SJeff Garzik 		"63==0x%04x  "
1442c6fd2807SJeff Garzik 		"64==0x%04x  "
1443c6fd2807SJeff Garzik 		"75==0x%04x  \n",
1444c6fd2807SJeff Garzik 		id[49],
1445c6fd2807SJeff Garzik 		id[53],
1446c6fd2807SJeff Garzik 		id[63],
1447c6fd2807SJeff Garzik 		id[64],
1448c6fd2807SJeff Garzik 		id[75]);
1449c6fd2807SJeff Garzik 	DPRINTK("80==0x%04x  "
1450c6fd2807SJeff Garzik 		"81==0x%04x  "
1451c6fd2807SJeff Garzik 		"82==0x%04x  "
1452c6fd2807SJeff Garzik 		"83==0x%04x  "
1453c6fd2807SJeff Garzik 		"84==0x%04x  \n",
1454c6fd2807SJeff Garzik 		id[80],
1455c6fd2807SJeff Garzik 		id[81],
1456c6fd2807SJeff Garzik 		id[82],
1457c6fd2807SJeff Garzik 		id[83],
1458c6fd2807SJeff Garzik 		id[84]);
1459c6fd2807SJeff Garzik 	DPRINTK("88==0x%04x  "
1460c6fd2807SJeff Garzik 		"93==0x%04x\n",
1461c6fd2807SJeff Garzik 		id[88],
1462c6fd2807SJeff Garzik 		id[93]);
1463c6fd2807SJeff Garzik }
1464c6fd2807SJeff Garzik 
1465c6fd2807SJeff Garzik /**
1466c6fd2807SJeff Garzik  *	ata_id_xfermask - Compute xfermask from the given IDENTIFY data
1467c6fd2807SJeff Garzik  *	@id: IDENTIFY data to compute xfer mask from
1468c6fd2807SJeff Garzik  *
1469c6fd2807SJeff Garzik  *	Compute the xfermask for this device. This is not as trivial
1470c6fd2807SJeff Garzik  *	as it seems if we must consider early devices correctly.
1471c6fd2807SJeff Garzik  *
1472c6fd2807SJeff Garzik  *	FIXME: pre IDE drive timing (do we care ?).
1473c6fd2807SJeff Garzik  *
1474c6fd2807SJeff Garzik  *	LOCKING:
1475c6fd2807SJeff Garzik  *	None.
1476c6fd2807SJeff Garzik  *
1477c6fd2807SJeff Garzik  *	RETURNS:
1478c6fd2807SJeff Garzik  *	Computed xfermask
1479c6fd2807SJeff Garzik  */
14807dc951aeSTejun Heo unsigned long ata_id_xfermask(const u16 *id)
1481c6fd2807SJeff Garzik {
14827dc951aeSTejun Heo 	unsigned long pio_mask, mwdma_mask, udma_mask;
1483c6fd2807SJeff Garzik 
1484c6fd2807SJeff Garzik 	/* Usual case. Word 53 indicates word 64 is valid */
1485c6fd2807SJeff Garzik 	if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
1486c6fd2807SJeff Garzik 		pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
1487c6fd2807SJeff Garzik 		pio_mask <<= 3;
1488c6fd2807SJeff Garzik 		pio_mask |= 0x7;
1489c6fd2807SJeff Garzik 	} else {
1490c6fd2807SJeff Garzik 		/* If word 64 isn't valid then Word 51 high byte holds
1491c6fd2807SJeff Garzik 		 * the PIO timing number for the maximum. Turn it into
1492c6fd2807SJeff Garzik 		 * a mask.
1493c6fd2807SJeff Garzik 		 */
14947a0f1c8aSLennert Buytenhek 		u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
149546767aebSAlan Cox 		if (mode < 5)	/* Valid PIO range */
149646767aebSAlan Cox 			pio_mask = (2 << mode) - 1;
149746767aebSAlan Cox 		else
149846767aebSAlan Cox 			pio_mask = 1;
1499c6fd2807SJeff Garzik 
1500c6fd2807SJeff Garzik 		/* But wait.. there's more. Design your standards by
1501c6fd2807SJeff Garzik 		 * committee and you too can get a free iordy field to
1502c6fd2807SJeff Garzik 		 * process. However its the speeds not the modes that
1503c6fd2807SJeff Garzik 		 * are supported... Note drivers using the timing API
1504c6fd2807SJeff Garzik 		 * will get this right anyway
1505c6fd2807SJeff Garzik 		 */
1506c6fd2807SJeff Garzik 	}
1507c6fd2807SJeff Garzik 
1508c6fd2807SJeff Garzik 	mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
1509c6fd2807SJeff Garzik 
1510b352e57dSAlan Cox 	if (ata_id_is_cfa(id)) {
1511b352e57dSAlan Cox 		/*
1512b352e57dSAlan Cox 		 *	Process compact flash extended modes
1513b352e57dSAlan Cox 		 */
151462afe5d7SSergei Shtylyov 		int pio = (id[ATA_ID_CFA_MODES] >> 0) & 0x7;
151562afe5d7SSergei Shtylyov 		int dma = (id[ATA_ID_CFA_MODES] >> 3) & 0x7;
1516b352e57dSAlan Cox 
1517b352e57dSAlan Cox 		if (pio)
1518b352e57dSAlan Cox 			pio_mask |= (1 << 5);
1519b352e57dSAlan Cox 		if (pio > 1)
1520b352e57dSAlan Cox 			pio_mask |= (1 << 6);
1521b352e57dSAlan Cox 		if (dma)
1522b352e57dSAlan Cox 			mwdma_mask |= (1 << 3);
1523b352e57dSAlan Cox 		if (dma > 1)
1524b352e57dSAlan Cox 			mwdma_mask |= (1 << 4);
1525b352e57dSAlan Cox 	}
1526b352e57dSAlan Cox 
1527c6fd2807SJeff Garzik 	udma_mask = 0;
1528c6fd2807SJeff Garzik 	if (id[ATA_ID_FIELD_VALID] & (1 << 2))
1529c6fd2807SJeff Garzik 		udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
1530c6fd2807SJeff Garzik 
1531c6fd2807SJeff Garzik 	return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
1532c6fd2807SJeff Garzik }
1533c6fd2807SJeff Garzik 
15347102d230SAdrian Bunk static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
1535c6fd2807SJeff Garzik {
1536c6fd2807SJeff Garzik 	struct completion *waiting = qc->private_data;
1537c6fd2807SJeff Garzik 
1538c6fd2807SJeff Garzik 	complete(waiting);
1539c6fd2807SJeff Garzik }
1540c6fd2807SJeff Garzik 
1541c6fd2807SJeff Garzik /**
15422432697bSTejun Heo  *	ata_exec_internal_sg - execute libata internal command
1543c6fd2807SJeff Garzik  *	@dev: Device to which the command is sent
1544c6fd2807SJeff Garzik  *	@tf: Taskfile registers for the command and the result
1545c6fd2807SJeff Garzik  *	@cdb: CDB for packet command
1546e227867fSMasanari Iida  *	@dma_dir: Data transfer direction of the command
15475c1ad8b3SRandy Dunlap  *	@sgl: sg list for the data buffer of the command
15482432697bSTejun Heo  *	@n_elem: Number of sg entries
15492b789108STejun Heo  *	@timeout: Timeout in msecs (0 for default)
1550c6fd2807SJeff Garzik  *
1551c6fd2807SJeff Garzik  *	Executes libata internal command with timeout.  @tf contains
1552c6fd2807SJeff Garzik  *	command on entry and result on return.  Timeout and error
1553c6fd2807SJeff Garzik  *	conditions are reported via return value.  No recovery action
1554c6fd2807SJeff Garzik  *	is taken after a command times out.  It's caller's duty to
1555c6fd2807SJeff Garzik  *	clean up after timeout.
1556c6fd2807SJeff Garzik  *
1557c6fd2807SJeff Garzik  *	LOCKING:
1558c6fd2807SJeff Garzik  *	None.  Should be called with kernel context, might sleep.
1559c6fd2807SJeff Garzik  *
1560c6fd2807SJeff Garzik  *	RETURNS:
1561c6fd2807SJeff Garzik  *	Zero on success, AC_ERR_* mask on failure
1562c6fd2807SJeff Garzik  */
15632432697bSTejun Heo unsigned ata_exec_internal_sg(struct ata_device *dev,
1564c6fd2807SJeff Garzik 			      struct ata_taskfile *tf, const u8 *cdb,
156587260216SJens Axboe 			      int dma_dir, struct scatterlist *sgl,
15662b789108STejun Heo 			      unsigned int n_elem, unsigned long timeout)
1567c6fd2807SJeff Garzik {
15689af5c9c9STejun Heo 	struct ata_link *link = dev->link;
15699af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
1570c6fd2807SJeff Garzik 	u8 command = tf->command;
157187fbc5a0STejun Heo 	int auto_timeout = 0;
1572c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
157328361c40SJens Axboe 	unsigned int preempted_tag;
1574e3ed8939SJens Axboe 	u32 preempted_sactive;
1575e3ed8939SJens Axboe 	u64 preempted_qc_active;
1576da917d69STejun Heo 	int preempted_nr_active_links;
1577c6fd2807SJeff Garzik 	DECLARE_COMPLETION_ONSTACK(wait);
1578c6fd2807SJeff Garzik 	unsigned long flags;
1579c6fd2807SJeff Garzik 	unsigned int err_mask;
1580c6fd2807SJeff Garzik 	int rc;
1581c6fd2807SJeff Garzik 
1582c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1583c6fd2807SJeff Garzik 
1584c6fd2807SJeff Garzik 	/* no internal command while frozen */
1585c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_FROZEN) {
1586c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
1587c6fd2807SJeff Garzik 		return AC_ERR_SYSTEM;
1588c6fd2807SJeff Garzik 	}
1589c6fd2807SJeff Garzik 
1590c6fd2807SJeff Garzik 	/* initialize internal qc */
159128361c40SJens Axboe 	qc = __ata_qc_from_tag(ap, ATA_TAG_INTERNAL);
1592c6fd2807SJeff Garzik 
159328361c40SJens Axboe 	qc->tag = ATA_TAG_INTERNAL;
159428361c40SJens Axboe 	qc->hw_tag = 0;
1595c6fd2807SJeff Garzik 	qc->scsicmd = NULL;
1596c6fd2807SJeff Garzik 	qc->ap = ap;
1597c6fd2807SJeff Garzik 	qc->dev = dev;
1598c6fd2807SJeff Garzik 	ata_qc_reinit(qc);
1599c6fd2807SJeff Garzik 
16009af5c9c9STejun Heo 	preempted_tag = link->active_tag;
16019af5c9c9STejun Heo 	preempted_sactive = link->sactive;
1602c6fd2807SJeff Garzik 	preempted_qc_active = ap->qc_active;
1603da917d69STejun Heo 	preempted_nr_active_links = ap->nr_active_links;
16049af5c9c9STejun Heo 	link->active_tag = ATA_TAG_POISON;
16059af5c9c9STejun Heo 	link->sactive = 0;
1606c6fd2807SJeff Garzik 	ap->qc_active = 0;
1607da917d69STejun Heo 	ap->nr_active_links = 0;
1608c6fd2807SJeff Garzik 
1609c6fd2807SJeff Garzik 	/* prepare & issue qc */
1610c6fd2807SJeff Garzik 	qc->tf = *tf;
1611c6fd2807SJeff Garzik 	if (cdb)
1612c6fd2807SJeff Garzik 		memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
1613e771451cSVincent Pelletier 
1614e771451cSVincent Pelletier 	/* some SATA bridges need us to indicate data xfer direction */
1615e771451cSVincent Pelletier 	if (tf->protocol == ATAPI_PROT_DMA && (dev->flags & ATA_DFLAG_DMADIR) &&
1616e771451cSVincent Pelletier 	    dma_dir == DMA_FROM_DEVICE)
1617e771451cSVincent Pelletier 		qc->tf.feature |= ATAPI_DMADIR;
1618e771451cSVincent Pelletier 
1619c6fd2807SJeff Garzik 	qc->flags |= ATA_QCFLAG_RESULT_TF;
1620c6fd2807SJeff Garzik 	qc->dma_dir = dma_dir;
1621c6fd2807SJeff Garzik 	if (dma_dir != DMA_NONE) {
16222432697bSTejun Heo 		unsigned int i, buflen = 0;
162387260216SJens Axboe 		struct scatterlist *sg;
16242432697bSTejun Heo 
162587260216SJens Axboe 		for_each_sg(sgl, sg, n_elem, i)
162687260216SJens Axboe 			buflen += sg->length;
16272432697bSTejun Heo 
162887260216SJens Axboe 		ata_sg_init(qc, sgl, n_elem);
162949c80429SBrian King 		qc->nbytes = buflen;
1630c6fd2807SJeff Garzik 	}
1631c6fd2807SJeff Garzik 
1632c6fd2807SJeff Garzik 	qc->private_data = &wait;
1633c6fd2807SJeff Garzik 	qc->complete_fn = ata_qc_complete_internal;
1634c6fd2807SJeff Garzik 
1635c6fd2807SJeff Garzik 	ata_qc_issue(qc);
1636c6fd2807SJeff Garzik 
1637c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1638c6fd2807SJeff Garzik 
163987fbc5a0STejun Heo 	if (!timeout) {
164087fbc5a0STejun Heo 		if (ata_probe_timeout)
1641341c2c95STejun Heo 			timeout = ata_probe_timeout * 1000;
164287fbc5a0STejun Heo 		else {
164387fbc5a0STejun Heo 			timeout = ata_internal_cmd_timeout(dev, command);
164487fbc5a0STejun Heo 			auto_timeout = 1;
164587fbc5a0STejun Heo 		}
164687fbc5a0STejun Heo 	}
16472b789108STejun Heo 
1648c0c362b6STejun Heo 	if (ap->ops->error_handler)
1649c0c362b6STejun Heo 		ata_eh_release(ap);
1650c0c362b6STejun Heo 
16512b789108STejun Heo 	rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
1652c6fd2807SJeff Garzik 
1653c0c362b6STejun Heo 	if (ap->ops->error_handler)
1654c0c362b6STejun Heo 		ata_eh_acquire(ap);
1655c0c362b6STejun Heo 
1656c429137aSTejun Heo 	ata_sff_flush_pio_task(ap);
1657c6fd2807SJeff Garzik 
1658c6fd2807SJeff Garzik 	if (!rc) {
1659c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
1660c6fd2807SJeff Garzik 
1661c6fd2807SJeff Garzik 		/* We're racing with irq here.  If we lose, the
1662c6fd2807SJeff Garzik 		 * following test prevents us from completing the qc
1663c6fd2807SJeff Garzik 		 * twice.  If we win, the port is frozen and will be
1664c6fd2807SJeff Garzik 		 * cleaned up by ->post_internal_cmd().
1665c6fd2807SJeff Garzik 		 */
1666c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_ACTIVE) {
1667c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_TIMEOUT;
1668c6fd2807SJeff Garzik 
1669c6fd2807SJeff Garzik 			if (ap->ops->error_handler)
1670c6fd2807SJeff Garzik 				ata_port_freeze(ap);
1671c6fd2807SJeff Garzik 			else
1672c6fd2807SJeff Garzik 				ata_qc_complete(qc);
1673c6fd2807SJeff Garzik 
1674c6fd2807SJeff Garzik 			if (ata_msg_warn(ap))
1675a9a79dfeSJoe Perches 				ata_dev_warn(dev, "qc timeout (cmd 0x%x)\n",
1676a9a79dfeSJoe Perches 					     command);
1677c6fd2807SJeff Garzik 		}
1678c6fd2807SJeff Garzik 
1679c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
1680c6fd2807SJeff Garzik 	}
1681c6fd2807SJeff Garzik 
1682c6fd2807SJeff Garzik 	/* do post_internal_cmd */
1683c6fd2807SJeff Garzik 	if (ap->ops->post_internal_cmd)
1684c6fd2807SJeff Garzik 		ap->ops->post_internal_cmd(qc);
1685c6fd2807SJeff Garzik 
1686a51d644aSTejun Heo 	/* perform minimal error analysis */
1687a51d644aSTejun Heo 	if (qc->flags & ATA_QCFLAG_FAILED) {
1688a51d644aSTejun Heo 		if (qc->result_tf.command & (ATA_ERR | ATA_DF))
1689a51d644aSTejun Heo 			qc->err_mask |= AC_ERR_DEV;
1690a51d644aSTejun Heo 
1691a51d644aSTejun Heo 		if (!qc->err_mask)
1692c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_OTHER;
1693a51d644aSTejun Heo 
1694a51d644aSTejun Heo 		if (qc->err_mask & ~AC_ERR_OTHER)
1695a51d644aSTejun Heo 			qc->err_mask &= ~AC_ERR_OTHER;
16962dae9955SDamien Le Moal 	} else if (qc->tf.command == ATA_CMD_REQ_SENSE_DATA) {
16972dae9955SDamien Le Moal 		qc->result_tf.command |= ATA_SENSE;
1698c6fd2807SJeff Garzik 	}
1699c6fd2807SJeff Garzik 
1700c6fd2807SJeff Garzik 	/* finish up */
1701c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1702c6fd2807SJeff Garzik 
1703c6fd2807SJeff Garzik 	*tf = qc->result_tf;
1704c6fd2807SJeff Garzik 	err_mask = qc->err_mask;
1705c6fd2807SJeff Garzik 
1706c6fd2807SJeff Garzik 	ata_qc_free(qc);
17079af5c9c9STejun Heo 	link->active_tag = preempted_tag;
17089af5c9c9STejun Heo 	link->sactive = preempted_sactive;
1709c6fd2807SJeff Garzik 	ap->qc_active = preempted_qc_active;
1710da917d69STejun Heo 	ap->nr_active_links = preempted_nr_active_links;
1711c6fd2807SJeff Garzik 
1712c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1713c6fd2807SJeff Garzik 
171487fbc5a0STejun Heo 	if ((err_mask & AC_ERR_TIMEOUT) && auto_timeout)
171587fbc5a0STejun Heo 		ata_internal_cmd_timed_out(dev, command);
171687fbc5a0STejun Heo 
1717c6fd2807SJeff Garzik 	return err_mask;
1718c6fd2807SJeff Garzik }
1719c6fd2807SJeff Garzik 
1720c6fd2807SJeff Garzik /**
172133480a0eSTejun Heo  *	ata_exec_internal - execute libata internal command
17222432697bSTejun Heo  *	@dev: Device to which the command is sent
17232432697bSTejun Heo  *	@tf: Taskfile registers for the command and the result
17242432697bSTejun Heo  *	@cdb: CDB for packet command
1725e227867fSMasanari Iida  *	@dma_dir: Data transfer direction of the command
17262432697bSTejun Heo  *	@buf: Data buffer of the command
17272432697bSTejun Heo  *	@buflen: Length of data buffer
17282b789108STejun Heo  *	@timeout: Timeout in msecs (0 for default)
17292432697bSTejun Heo  *
17302432697bSTejun Heo  *	Wrapper around ata_exec_internal_sg() which takes simple
17312432697bSTejun Heo  *	buffer instead of sg list.
17322432697bSTejun Heo  *
17332432697bSTejun Heo  *	LOCKING:
17342432697bSTejun Heo  *	None.  Should be called with kernel context, might sleep.
17352432697bSTejun Heo  *
17362432697bSTejun Heo  *	RETURNS:
17372432697bSTejun Heo  *	Zero on success, AC_ERR_* mask on failure
17382432697bSTejun Heo  */
17392432697bSTejun Heo unsigned ata_exec_internal(struct ata_device *dev,
17402432697bSTejun Heo 			   struct ata_taskfile *tf, const u8 *cdb,
17412b789108STejun Heo 			   int dma_dir, void *buf, unsigned int buflen,
17422b789108STejun Heo 			   unsigned long timeout)
17432432697bSTejun Heo {
174433480a0eSTejun Heo 	struct scatterlist *psg = NULL, sg;
174533480a0eSTejun Heo 	unsigned int n_elem = 0;
17462432697bSTejun Heo 
174733480a0eSTejun Heo 	if (dma_dir != DMA_NONE) {
174833480a0eSTejun Heo 		WARN_ON(!buf);
17492432697bSTejun Heo 		sg_init_one(&sg, buf, buflen);
175033480a0eSTejun Heo 		psg = &sg;
175133480a0eSTejun Heo 		n_elem++;
175233480a0eSTejun Heo 	}
17532432697bSTejun Heo 
17542b789108STejun Heo 	return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem,
17552b789108STejun Heo 				    timeout);
17562432697bSTejun Heo }
17572432697bSTejun Heo 
17582432697bSTejun Heo /**
1759c6fd2807SJeff Garzik  *	ata_pio_need_iordy	-	check if iordy needed
1760c6fd2807SJeff Garzik  *	@adev: ATA device
1761c6fd2807SJeff Garzik  *
1762c6fd2807SJeff Garzik  *	Check if the current speed of the device requires IORDY. Used
1763c6fd2807SJeff Garzik  *	by various controllers for chip configuration.
1764c6fd2807SJeff Garzik  */
1765c6fd2807SJeff Garzik unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1766c6fd2807SJeff Garzik {
17670d9e6659STejun Heo 	/* Don't set IORDY if we're preparing for reset.  IORDY may
17680d9e6659STejun Heo 	 * lead to controller lock up on certain controllers if the
17690d9e6659STejun Heo 	 * port is not occupied.  See bko#11703 for details.
17700d9e6659STejun Heo 	 */
17710d9e6659STejun Heo 	if (adev->link->ap->pflags & ATA_PFLAG_RESETTING)
17720d9e6659STejun Heo 		return 0;
17730d9e6659STejun Heo 	/* Controller doesn't support IORDY.  Probably a pointless
17740d9e6659STejun Heo 	 * check as the caller should know this.
17750d9e6659STejun Heo 	 */
17769af5c9c9STejun Heo 	if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
1777c6fd2807SJeff Garzik 		return 0;
17785c18c4d2SDavid Daney 	/* CF spec. r4.1 Table 22 says no iordy on PIO5 and PIO6.  */
17795c18c4d2SDavid Daney 	if (ata_id_is_cfa(adev->id)
17805c18c4d2SDavid Daney 	    && (adev->pio_mode == XFER_PIO_5 || adev->pio_mode == XFER_PIO_6))
17815c18c4d2SDavid Daney 		return 0;
1782432729f0SAlan Cox 	/* PIO3 and higher it is mandatory */
1783432729f0SAlan Cox 	if (adev->pio_mode > XFER_PIO_2)
1784c6fd2807SJeff Garzik 		return 1;
1785432729f0SAlan Cox 	/* We turn it on when possible */
1786432729f0SAlan Cox 	if (ata_id_has_iordy(adev->id))
1787432729f0SAlan Cox 		return 1;
1788432729f0SAlan Cox 	return 0;
1789432729f0SAlan Cox }
1790c6fd2807SJeff Garzik 
1791432729f0SAlan Cox /**
1792432729f0SAlan Cox  *	ata_pio_mask_no_iordy	-	Return the non IORDY mask
1793432729f0SAlan Cox  *	@adev: ATA device
1794432729f0SAlan Cox  *
1795432729f0SAlan Cox  *	Compute the highest mode possible if we are not using iordy. Return
1796432729f0SAlan Cox  *	-1 if no iordy mode is available.
1797432729f0SAlan Cox  */
1798432729f0SAlan Cox static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
1799432729f0SAlan Cox {
1800c6fd2807SJeff Garzik 	/* If we have no drive specific rule, then PIO 2 is non IORDY */
1801c6fd2807SJeff Garzik 	if (adev->id[ATA_ID_FIELD_VALID] & 2) {	/* EIDE */
1802432729f0SAlan Cox 		u16 pio = adev->id[ATA_ID_EIDE_PIO];
1803c6fd2807SJeff Garzik 		/* Is the speed faster than the drive allows non IORDY ? */
1804c6fd2807SJeff Garzik 		if (pio) {
1805c6fd2807SJeff Garzik 			/* This is cycle times not frequency - watch the logic! */
1806c6fd2807SJeff Garzik 			if (pio > 240)	/* PIO2 is 240nS per cycle */
1807432729f0SAlan Cox 				return 3 << ATA_SHIFT_PIO;
1808432729f0SAlan Cox 			return 7 << ATA_SHIFT_PIO;
1809c6fd2807SJeff Garzik 		}
1810c6fd2807SJeff Garzik 	}
1811432729f0SAlan Cox 	return 3 << ATA_SHIFT_PIO;
1812c6fd2807SJeff Garzik }
1813c6fd2807SJeff Garzik 
1814c6fd2807SJeff Garzik /**
1815963e4975SAlan Cox  *	ata_do_dev_read_id		-	default ID read method
1816963e4975SAlan Cox  *	@dev: device
1817963e4975SAlan Cox  *	@tf: proposed taskfile
1818963e4975SAlan Cox  *	@id: data buffer
1819963e4975SAlan Cox  *
1820963e4975SAlan Cox  *	Issue the identify taskfile and hand back the buffer containing
1821963e4975SAlan Cox  *	identify data. For some RAID controllers and for pre ATA devices
1822963e4975SAlan Cox  *	this function is wrapped or replaced by the driver
1823963e4975SAlan Cox  */
1824963e4975SAlan Cox unsigned int ata_do_dev_read_id(struct ata_device *dev,
1825963e4975SAlan Cox 					struct ata_taskfile *tf, u16 *id)
1826963e4975SAlan Cox {
1827963e4975SAlan Cox 	return ata_exec_internal(dev, tf, NULL, DMA_FROM_DEVICE,
1828963e4975SAlan Cox 				     id, sizeof(id[0]) * ATA_ID_WORDS, 0);
1829963e4975SAlan Cox }
1830963e4975SAlan Cox 
1831963e4975SAlan Cox /**
1832c6fd2807SJeff Garzik  *	ata_dev_read_id - Read ID data from the specified device
1833c6fd2807SJeff Garzik  *	@dev: target device
1834c6fd2807SJeff Garzik  *	@p_class: pointer to class of the target device (may be changed)
1835bff04647STejun Heo  *	@flags: ATA_READID_* flags
1836c6fd2807SJeff Garzik  *	@id: buffer to read IDENTIFY data into
1837c6fd2807SJeff Garzik  *
1838c6fd2807SJeff Garzik  *	Read ID data from the specified device.  ATA_CMD_ID_ATA is
1839c6fd2807SJeff Garzik  *	performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
1840c6fd2807SJeff Garzik  *	devices.  This function also issues ATA_CMD_INIT_DEV_PARAMS
1841c6fd2807SJeff Garzik  *	for pre-ATA4 drives.
1842c6fd2807SJeff Garzik  *
184350a99018SAlan Cox  *	FIXME: ATA_CMD_ID_ATA is optional for early drives and right
184450a99018SAlan Cox  *	now we abort if we hit that case.
184550a99018SAlan Cox  *
1846c6fd2807SJeff Garzik  *	LOCKING:
1847c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
1848c6fd2807SJeff Garzik  *
1849c6fd2807SJeff Garzik  *	RETURNS:
1850c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
1851c6fd2807SJeff Garzik  */
1852c6fd2807SJeff Garzik int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
1853bff04647STejun Heo 		    unsigned int flags, u16 *id)
1854c6fd2807SJeff Garzik {
18559af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
1856c6fd2807SJeff Garzik 	unsigned int class = *p_class;
1857c6fd2807SJeff Garzik 	struct ata_taskfile tf;
1858c6fd2807SJeff Garzik 	unsigned int err_mask = 0;
1859c6fd2807SJeff Garzik 	const char *reason;
186079b42babSTejun Heo 	bool is_semb = class == ATA_DEV_SEMB;
186154936f8bSTejun Heo 	int may_fallback = 1, tried_spinup = 0;
1862c6fd2807SJeff Garzik 	int rc;
1863c6fd2807SJeff Garzik 
1864c6fd2807SJeff Garzik 	if (ata_msg_ctl(ap))
1865a9a79dfeSJoe Perches 		ata_dev_dbg(dev, "%s: ENTER\n", __func__);
1866c6fd2807SJeff Garzik 
1867c6fd2807SJeff Garzik retry:
1868c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
1869c6fd2807SJeff Garzik 
1870c6fd2807SJeff Garzik 	switch (class) {
187179b42babSTejun Heo 	case ATA_DEV_SEMB:
187279b42babSTejun Heo 		class = ATA_DEV_ATA;	/* some hard drives report SEMB sig */
187305b83605SGustavo A. R. Silva 		/* fall through */
1874c6fd2807SJeff Garzik 	case ATA_DEV_ATA:
18759162c657SHannes Reinecke 	case ATA_DEV_ZAC:
1876c6fd2807SJeff Garzik 		tf.command = ATA_CMD_ID_ATA;
1877c6fd2807SJeff Garzik 		break;
1878c6fd2807SJeff Garzik 	case ATA_DEV_ATAPI:
1879c6fd2807SJeff Garzik 		tf.command = ATA_CMD_ID_ATAPI;
1880c6fd2807SJeff Garzik 		break;
1881c6fd2807SJeff Garzik 	default:
1882c6fd2807SJeff Garzik 		rc = -ENODEV;
1883c6fd2807SJeff Garzik 		reason = "unsupported class";
1884c6fd2807SJeff Garzik 		goto err_out;
1885c6fd2807SJeff Garzik 	}
1886c6fd2807SJeff Garzik 
1887c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_PIO;
188881afe893STejun Heo 
188981afe893STejun Heo 	/* Some devices choke if TF registers contain garbage.  Make
189081afe893STejun Heo 	 * sure those are properly initialized.
189181afe893STejun Heo 	 */
189281afe893STejun Heo 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
189381afe893STejun Heo 
189481afe893STejun Heo 	/* Device presence detection is unreliable on some
189581afe893STejun Heo 	 * controllers.  Always poll IDENTIFY if available.
189681afe893STejun Heo 	 */
189781afe893STejun Heo 	tf.flags |= ATA_TFLAG_POLLING;
1898c6fd2807SJeff Garzik 
1899963e4975SAlan Cox 	if (ap->ops->read_id)
1900963e4975SAlan Cox 		err_mask = ap->ops->read_id(dev, &tf, id);
1901963e4975SAlan Cox 	else
1902963e4975SAlan Cox 		err_mask = ata_do_dev_read_id(dev, &tf, id);
1903963e4975SAlan Cox 
1904c6fd2807SJeff Garzik 	if (err_mask) {
1905800b3996STejun Heo 		if (err_mask & AC_ERR_NODEV_HINT) {
1906a9a79dfeSJoe Perches 			ata_dev_dbg(dev, "NODEV after polling detection\n");
190755a8e2c8STejun Heo 			return -ENOENT;
190855a8e2c8STejun Heo 		}
190955a8e2c8STejun Heo 
191079b42babSTejun Heo 		if (is_semb) {
1911a9a79dfeSJoe Perches 			ata_dev_info(dev,
1912a9a79dfeSJoe Perches 		     "IDENTIFY failed on device w/ SEMB sig, disabled\n");
191379b42babSTejun Heo 			/* SEMB is not supported yet */
191479b42babSTejun Heo 			*p_class = ATA_DEV_SEMB_UNSUP;
191579b42babSTejun Heo 			return 0;
191679b42babSTejun Heo 		}
191779b42babSTejun Heo 
19181ffc151fSTejun Heo 		if ((err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
19191ffc151fSTejun Heo 			/* Device or controller might have reported
19201ffc151fSTejun Heo 			 * the wrong device class.  Give a shot at the
19211ffc151fSTejun Heo 			 * other IDENTIFY if the current one is
19221ffc151fSTejun Heo 			 * aborted by the device.
192354936f8bSTejun Heo 			 */
19241ffc151fSTejun Heo 			if (may_fallback) {
192554936f8bSTejun Heo 				may_fallback = 0;
192654936f8bSTejun Heo 
192754936f8bSTejun Heo 				if (class == ATA_DEV_ATA)
192854936f8bSTejun Heo 					class = ATA_DEV_ATAPI;
192954936f8bSTejun Heo 				else
193054936f8bSTejun Heo 					class = ATA_DEV_ATA;
193154936f8bSTejun Heo 				goto retry;
193254936f8bSTejun Heo 			}
193354936f8bSTejun Heo 
19341ffc151fSTejun Heo 			/* Control reaches here iff the device aborted
19351ffc151fSTejun Heo 			 * both flavors of IDENTIFYs which happens
19361ffc151fSTejun Heo 			 * sometimes with phantom devices.
19371ffc151fSTejun Heo 			 */
1938a9a79dfeSJoe Perches 			ata_dev_dbg(dev,
19391ffc151fSTejun Heo 				    "both IDENTIFYs aborted, assuming NODEV\n");
19401ffc151fSTejun Heo 			return -ENOENT;
19411ffc151fSTejun Heo 		}
19421ffc151fSTejun Heo 
1943c6fd2807SJeff Garzik 		rc = -EIO;
1944c6fd2807SJeff Garzik 		reason = "I/O error";
1945c6fd2807SJeff Garzik 		goto err_out;
1946c6fd2807SJeff Garzik 	}
1947c6fd2807SJeff Garzik 
194843c9c591STejun Heo 	if (dev->horkage & ATA_HORKAGE_DUMP_ID) {
1949a9a79dfeSJoe Perches 		ata_dev_dbg(dev, "dumping IDENTIFY data, "
195043c9c591STejun Heo 			    "class=%d may_fallback=%d tried_spinup=%d\n",
195143c9c591STejun Heo 			    class, may_fallback, tried_spinup);
195243c9c591STejun Heo 		print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET,
195343c9c591STejun Heo 			       16, 2, id, ATA_ID_WORDS * sizeof(*id), true);
195443c9c591STejun Heo 	}
195543c9c591STejun Heo 
195654936f8bSTejun Heo 	/* Falling back doesn't make sense if ID data was read
195754936f8bSTejun Heo 	 * successfully at least once.
195854936f8bSTejun Heo 	 */
195954936f8bSTejun Heo 	may_fallback = 0;
196054936f8bSTejun Heo 
1961c6fd2807SJeff Garzik 	swap_buf_le16(id, ATA_ID_WORDS);
1962c6fd2807SJeff Garzik 
1963c6fd2807SJeff Garzik 	/* sanity check */
1964c6fd2807SJeff Garzik 	rc = -EINVAL;
19656070068bSAlan Cox 	reason = "device reports invalid type";
19664a3381feSJeff Garzik 
19679162c657SHannes Reinecke 	if (class == ATA_DEV_ATA || class == ATA_DEV_ZAC) {
19684a3381feSJeff Garzik 		if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
19694a3381feSJeff Garzik 			goto err_out;
1970db63a4c8SAndy Whitcroft 		if (ap->host->flags & ATA_HOST_IGNORE_ATA &&
1971db63a4c8SAndy Whitcroft 							ata_id_is_ata(id)) {
1972db63a4c8SAndy Whitcroft 			ata_dev_dbg(dev,
1973db63a4c8SAndy Whitcroft 				"host indicates ignore ATA devices, ignored\n");
1974db63a4c8SAndy Whitcroft 			return -ENOENT;
1975db63a4c8SAndy Whitcroft 		}
19764a3381feSJeff Garzik 	} else {
19774a3381feSJeff Garzik 		if (ata_id_is_ata(id))
1978c6fd2807SJeff Garzik 			goto err_out;
1979c6fd2807SJeff Garzik 	}
1980c6fd2807SJeff Garzik 
1981169439c2SMark Lord 	if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
1982169439c2SMark Lord 		tried_spinup = 1;
1983169439c2SMark Lord 		/*
1984169439c2SMark Lord 		 * Drive powered-up in standby mode, and requires a specific
1985169439c2SMark Lord 		 * SET_FEATURES spin-up subcommand before it will accept
1986169439c2SMark Lord 		 * anything other than the original IDENTIFY command.
1987169439c2SMark Lord 		 */
1988218f3d30SJeff Garzik 		err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
1989fb0582f9SRyan Power 		if (err_mask && id[2] != 0x738c) {
1990169439c2SMark Lord 			rc = -EIO;
1991169439c2SMark Lord 			reason = "SPINUP failed";
1992169439c2SMark Lord 			goto err_out;
1993169439c2SMark Lord 		}
1994169439c2SMark Lord 		/*
1995169439c2SMark Lord 		 * If the drive initially returned incomplete IDENTIFY info,
1996169439c2SMark Lord 		 * we now must reissue the IDENTIFY command.
1997169439c2SMark Lord 		 */
1998169439c2SMark Lord 		if (id[2] == 0x37c8)
1999169439c2SMark Lord 			goto retry;
2000169439c2SMark Lord 	}
2001169439c2SMark Lord 
20029162c657SHannes Reinecke 	if ((flags & ATA_READID_POSTRESET) &&
20039162c657SHannes Reinecke 	    (class == ATA_DEV_ATA || class == ATA_DEV_ZAC)) {
2004c6fd2807SJeff Garzik 		/*
2005c6fd2807SJeff Garzik 		 * The exact sequence expected by certain pre-ATA4 drives is:
2006c6fd2807SJeff Garzik 		 * SRST RESET
200750a99018SAlan Cox 		 * IDENTIFY (optional in early ATA)
200850a99018SAlan Cox 		 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
2009c6fd2807SJeff Garzik 		 * anything else..
2010c6fd2807SJeff Garzik 		 * Some drives were very specific about that exact sequence.
201150a99018SAlan Cox 		 *
201250a99018SAlan Cox 		 * Note that ATA4 says lba is mandatory so the second check
2013c9404c9cSAdam Buchbinder 		 * should never trigger.
2014c6fd2807SJeff Garzik 		 */
2015c6fd2807SJeff Garzik 		if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
2016c6fd2807SJeff Garzik 			err_mask = ata_dev_init_params(dev, id[3], id[6]);
2017c6fd2807SJeff Garzik 			if (err_mask) {
2018c6fd2807SJeff Garzik 				rc = -EIO;
2019c6fd2807SJeff Garzik 				reason = "INIT_DEV_PARAMS failed";
2020c6fd2807SJeff Garzik 				goto err_out;
2021c6fd2807SJeff Garzik 			}
2022c6fd2807SJeff Garzik 
2023c6fd2807SJeff Garzik 			/* current CHS translation info (id[53-58]) might be
2024c6fd2807SJeff Garzik 			 * changed. reread the identify device info.
2025c6fd2807SJeff Garzik 			 */
2026bff04647STejun Heo 			flags &= ~ATA_READID_POSTRESET;
2027c6fd2807SJeff Garzik 			goto retry;
2028c6fd2807SJeff Garzik 		}
2029c6fd2807SJeff Garzik 	}
2030c6fd2807SJeff Garzik 
2031c6fd2807SJeff Garzik 	*p_class = class;
2032c6fd2807SJeff Garzik 
2033c6fd2807SJeff Garzik 	return 0;
2034c6fd2807SJeff Garzik 
2035c6fd2807SJeff Garzik  err_out:
2036c6fd2807SJeff Garzik 	if (ata_msg_warn(ap))
2037a9a79dfeSJoe Perches 		ata_dev_warn(dev, "failed to IDENTIFY (%s, err_mask=0x%x)\n",
2038a9a79dfeSJoe Perches 			     reason, err_mask);
2039c6fd2807SJeff Garzik 	return rc;
2040c6fd2807SJeff Garzik }
2041c6fd2807SJeff Garzik 
2042f01f62c2SChristoph Hellwig /**
2043f01f62c2SChristoph Hellwig  *	ata_read_log_page - read a specific log page
2044f01f62c2SChristoph Hellwig  *	@dev: target device
2045f01f62c2SChristoph Hellwig  *	@log: log to read
2046f01f62c2SChristoph Hellwig  *	@page: page to read
2047f01f62c2SChristoph Hellwig  *	@buf: buffer to store read page
2048f01f62c2SChristoph Hellwig  *	@sectors: number of sectors to read
2049f01f62c2SChristoph Hellwig  *
2050f01f62c2SChristoph Hellwig  *	Read log page using READ_LOG_EXT command.
2051f01f62c2SChristoph Hellwig  *
2052f01f62c2SChristoph Hellwig  *	LOCKING:
2053f01f62c2SChristoph Hellwig  *	Kernel thread context (may sleep).
2054f01f62c2SChristoph Hellwig  *
2055f01f62c2SChristoph Hellwig  *	RETURNS:
2056f01f62c2SChristoph Hellwig  *	0 on success, AC_ERR_* mask otherwise.
2057f01f62c2SChristoph Hellwig  */
2058f01f62c2SChristoph Hellwig unsigned int ata_read_log_page(struct ata_device *dev, u8 log,
2059f01f62c2SChristoph Hellwig 			       u8 page, void *buf, unsigned int sectors)
2060f01f62c2SChristoph Hellwig {
2061f01f62c2SChristoph Hellwig 	unsigned long ap_flags = dev->link->ap->flags;
2062f01f62c2SChristoph Hellwig 	struct ata_taskfile tf;
2063f01f62c2SChristoph Hellwig 	unsigned int err_mask;
2064f01f62c2SChristoph Hellwig 	bool dma = false;
2065f01f62c2SChristoph Hellwig 
2066f01f62c2SChristoph Hellwig 	DPRINTK("read log page - log 0x%x, page 0x%x\n", log, page);
2067f01f62c2SChristoph Hellwig 
2068f01f62c2SChristoph Hellwig 	/*
2069f01f62c2SChristoph Hellwig 	 * Return error without actually issuing the command on controllers
2070f01f62c2SChristoph Hellwig 	 * which e.g. lockup on a read log page.
2071f01f62c2SChristoph Hellwig 	 */
2072f01f62c2SChristoph Hellwig 	if (ap_flags & ATA_FLAG_NO_LOG_PAGE)
2073f01f62c2SChristoph Hellwig 		return AC_ERR_DEV;
2074f01f62c2SChristoph Hellwig 
2075f01f62c2SChristoph Hellwig retry:
2076f01f62c2SChristoph Hellwig 	ata_tf_init(dev, &tf);
2077f01f62c2SChristoph Hellwig 	if (dev->dma_mode && ata_id_has_read_log_dma_ext(dev->id) &&
20787cfdfdc8SDamien Le Moal 	    !(dev->horkage & ATA_HORKAGE_NO_DMA_LOG)) {
2079f01f62c2SChristoph Hellwig 		tf.command = ATA_CMD_READ_LOG_DMA_EXT;
2080f01f62c2SChristoph Hellwig 		tf.protocol = ATA_PROT_DMA;
2081f01f62c2SChristoph Hellwig 		dma = true;
2082f01f62c2SChristoph Hellwig 	} else {
2083f01f62c2SChristoph Hellwig 		tf.command = ATA_CMD_READ_LOG_EXT;
2084f01f62c2SChristoph Hellwig 		tf.protocol = ATA_PROT_PIO;
2085f01f62c2SChristoph Hellwig 		dma = false;
2086f01f62c2SChristoph Hellwig 	}
2087f01f62c2SChristoph Hellwig 	tf.lbal = log;
2088f01f62c2SChristoph Hellwig 	tf.lbam = page;
2089f01f62c2SChristoph Hellwig 	tf.nsect = sectors;
2090f01f62c2SChristoph Hellwig 	tf.hob_nsect = sectors >> 8;
2091f01f62c2SChristoph Hellwig 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
2092f01f62c2SChristoph Hellwig 
2093f01f62c2SChristoph Hellwig 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
2094f01f62c2SChristoph Hellwig 				     buf, sectors * ATA_SECT_SIZE, 0);
2095f01f62c2SChristoph Hellwig 
2096f01f62c2SChristoph Hellwig 	if (err_mask && dma) {
20977cfdfdc8SDamien Le Moal 		dev->horkage |= ATA_HORKAGE_NO_DMA_LOG;
20987cfdfdc8SDamien Le Moal 		ata_dev_warn(dev, "READ LOG DMA EXT failed, trying PIO\n");
2099f01f62c2SChristoph Hellwig 		goto retry;
2100f01f62c2SChristoph Hellwig 	}
2101f01f62c2SChristoph Hellwig 
2102f01f62c2SChristoph Hellwig 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
2103f01f62c2SChristoph Hellwig 	return err_mask;
2104f01f62c2SChristoph Hellwig }
2105f01f62c2SChristoph Hellwig 
2106efe205a3SChristoph Hellwig static bool ata_log_supported(struct ata_device *dev, u8 log)
2107efe205a3SChristoph Hellwig {
2108efe205a3SChristoph Hellwig 	struct ata_port *ap = dev->link->ap;
2109efe205a3SChristoph Hellwig 
2110efe205a3SChristoph Hellwig 	if (ata_read_log_page(dev, ATA_LOG_DIRECTORY, 0, ap->sector_buf, 1))
2111efe205a3SChristoph Hellwig 		return false;
2112efe205a3SChristoph Hellwig 	return get_unaligned_le16(&ap->sector_buf[log * 2]) ? true : false;
2113efe205a3SChristoph Hellwig }
2114efe205a3SChristoph Hellwig 
2115a0fd2454SChristoph Hellwig static bool ata_identify_page_supported(struct ata_device *dev, u8 page)
2116a0fd2454SChristoph Hellwig {
2117a0fd2454SChristoph Hellwig 	struct ata_port *ap = dev->link->ap;
2118a0fd2454SChristoph Hellwig 	unsigned int err, i;
2119a0fd2454SChristoph Hellwig 
2120a0fd2454SChristoph Hellwig 	if (!ata_log_supported(dev, ATA_LOG_IDENTIFY_DEVICE)) {
2121a0fd2454SChristoph Hellwig 		ata_dev_warn(dev, "ATA Identify Device Log not supported\n");
2122a0fd2454SChristoph Hellwig 		return false;
2123a0fd2454SChristoph Hellwig 	}
2124a0fd2454SChristoph Hellwig 
2125a0fd2454SChristoph Hellwig 	/*
2126a0fd2454SChristoph Hellwig 	 * Read IDENTIFY DEVICE data log, page 0, to figure out if the page is
2127a0fd2454SChristoph Hellwig 	 * supported.
2128a0fd2454SChristoph Hellwig 	 */
2129a0fd2454SChristoph Hellwig 	err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, 0, ap->sector_buf,
2130a0fd2454SChristoph Hellwig 				1);
2131a0fd2454SChristoph Hellwig 	if (err) {
2132a0fd2454SChristoph Hellwig 		ata_dev_info(dev,
2133a0fd2454SChristoph Hellwig 			     "failed to get Device Identify Log Emask 0x%x\n",
2134a0fd2454SChristoph Hellwig 			     err);
2135a0fd2454SChristoph Hellwig 		return false;
2136a0fd2454SChristoph Hellwig 	}
2137a0fd2454SChristoph Hellwig 
2138a0fd2454SChristoph Hellwig 	for (i = 0; i < ap->sector_buf[8]; i++) {
2139a0fd2454SChristoph Hellwig 		if (ap->sector_buf[9 + i] == page)
2140a0fd2454SChristoph Hellwig 			return true;
2141a0fd2454SChristoph Hellwig 	}
2142a0fd2454SChristoph Hellwig 
2143a0fd2454SChristoph Hellwig 	return false;
2144a0fd2454SChristoph Hellwig }
2145a0fd2454SChristoph Hellwig 
21469062712fSTejun Heo static int ata_do_link_spd_horkage(struct ata_device *dev)
21479062712fSTejun Heo {
21489062712fSTejun Heo 	struct ata_link *plink = ata_dev_phys_link(dev);
21499062712fSTejun Heo 	u32 target, target_limit;
21509062712fSTejun Heo 
21519062712fSTejun Heo 	if (!sata_scr_valid(plink))
21529062712fSTejun Heo 		return 0;
21539062712fSTejun Heo 
21549062712fSTejun Heo 	if (dev->horkage & ATA_HORKAGE_1_5_GBPS)
21559062712fSTejun Heo 		target = 1;
21569062712fSTejun Heo 	else
21579062712fSTejun Heo 		return 0;
21589062712fSTejun Heo 
21599062712fSTejun Heo 	target_limit = (1 << target) - 1;
21609062712fSTejun Heo 
21619062712fSTejun Heo 	/* if already on stricter limit, no need to push further */
21629062712fSTejun Heo 	if (plink->sata_spd_limit <= target_limit)
21639062712fSTejun Heo 		return 0;
21649062712fSTejun Heo 
21659062712fSTejun Heo 	plink->sata_spd_limit = target_limit;
21669062712fSTejun Heo 
21679062712fSTejun Heo 	/* Request another EH round by returning -EAGAIN if link is
21689062712fSTejun Heo 	 * going faster than the target speed.  Forward progress is
21699062712fSTejun Heo 	 * guaranteed by setting sata_spd_limit to target_limit above.
21709062712fSTejun Heo 	 */
21719062712fSTejun Heo 	if (plink->sata_spd > target) {
2172a9a79dfeSJoe Perches 		ata_dev_info(dev, "applying link speed limit horkage to %s\n",
21739062712fSTejun Heo 			     sata_spd_string(target));
21749062712fSTejun Heo 		return -EAGAIN;
21759062712fSTejun Heo 	}
21769062712fSTejun Heo 	return 0;
21779062712fSTejun Heo }
21789062712fSTejun Heo 
2179c6fd2807SJeff Garzik static inline u8 ata_dev_knobble(struct ata_device *dev)
2180c6fd2807SJeff Garzik {
21819af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
21829ce8e307SJens Axboe 
21839ce8e307SJens Axboe 	if (ata_dev_blacklisted(dev) & ATA_HORKAGE_BRIDGE_OK)
21849ce8e307SJens Axboe 		return 0;
21859ce8e307SJens Axboe 
21869af5c9c9STejun Heo 	return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
2187c6fd2807SJeff Garzik }
2188c6fd2807SJeff Garzik 
21895a233551SHannes Reinecke static void ata_dev_config_ncq_send_recv(struct ata_device *dev)
21905a233551SHannes Reinecke {
21915a233551SHannes Reinecke 	struct ata_port *ap = dev->link->ap;
21925a233551SHannes Reinecke 	unsigned int err_mask;
21935a233551SHannes Reinecke 
2194efe205a3SChristoph Hellwig 	if (!ata_log_supported(dev, ATA_LOG_NCQ_SEND_RECV)) {
2195efe205a3SChristoph Hellwig 		ata_dev_warn(dev, "NCQ Send/Recv Log not supported\n");
2196fe5af0ccSHannes Reinecke 		return;
2197fe5af0ccSHannes Reinecke 	}
21985a233551SHannes Reinecke 	err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_SEND_RECV,
21995a233551SHannes Reinecke 				     0, ap->sector_buf, 1);
22005a233551SHannes Reinecke 	if (err_mask) {
22015a233551SHannes Reinecke 		ata_dev_dbg(dev,
22025a233551SHannes Reinecke 			    "failed to get NCQ Send/Recv Log Emask 0x%x\n",
22035a233551SHannes Reinecke 			    err_mask);
22045a233551SHannes Reinecke 	} else {
22055a233551SHannes Reinecke 		u8 *cmds = dev->ncq_send_recv_cmds;
22065a233551SHannes Reinecke 
22075a233551SHannes Reinecke 		dev->flags |= ATA_DFLAG_NCQ_SEND_RECV;
22085a233551SHannes Reinecke 		memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_SEND_RECV_SIZE);
22095a233551SHannes Reinecke 
22105a233551SHannes Reinecke 		if (dev->horkage & ATA_HORKAGE_NO_NCQ_TRIM) {
22115a233551SHannes Reinecke 			ata_dev_dbg(dev, "disabling queued TRIM support\n");
22125a233551SHannes Reinecke 			cmds[ATA_LOG_NCQ_SEND_RECV_DSM_OFFSET] &=
22135a233551SHannes Reinecke 				~ATA_LOG_NCQ_SEND_RECV_DSM_TRIM;
22145a233551SHannes Reinecke 		}
22155a233551SHannes Reinecke 	}
22165a233551SHannes Reinecke }
22175a233551SHannes Reinecke 
2218284b3b77SHannes Reinecke static void ata_dev_config_ncq_non_data(struct ata_device *dev)
2219284b3b77SHannes Reinecke {
2220284b3b77SHannes Reinecke 	struct ata_port *ap = dev->link->ap;
2221284b3b77SHannes Reinecke 	unsigned int err_mask;
2222284b3b77SHannes Reinecke 
2223efe205a3SChristoph Hellwig 	if (!ata_log_supported(dev, ATA_LOG_NCQ_NON_DATA)) {
2224284b3b77SHannes Reinecke 		ata_dev_warn(dev,
2225284b3b77SHannes Reinecke 			     "NCQ Send/Recv Log not supported\n");
2226284b3b77SHannes Reinecke 		return;
2227284b3b77SHannes Reinecke 	}
2228284b3b77SHannes Reinecke 	err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_NON_DATA,
2229284b3b77SHannes Reinecke 				     0, ap->sector_buf, 1);
2230284b3b77SHannes Reinecke 	if (err_mask) {
2231284b3b77SHannes Reinecke 		ata_dev_dbg(dev,
2232284b3b77SHannes Reinecke 			    "failed to get NCQ Non-Data Log Emask 0x%x\n",
2233284b3b77SHannes Reinecke 			    err_mask);
2234284b3b77SHannes Reinecke 	} else {
2235284b3b77SHannes Reinecke 		u8 *cmds = dev->ncq_non_data_cmds;
2236284b3b77SHannes Reinecke 
2237284b3b77SHannes Reinecke 		memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_NON_DATA_SIZE);
2238284b3b77SHannes Reinecke 	}
2239284b3b77SHannes Reinecke }
2240284b3b77SHannes Reinecke 
22418e061784SAdam Manzanares static void ata_dev_config_ncq_prio(struct ata_device *dev)
22428e061784SAdam Manzanares {
22438e061784SAdam Manzanares 	struct ata_port *ap = dev->link->ap;
22448e061784SAdam Manzanares 	unsigned int err_mask;
22458e061784SAdam Manzanares 
22469f56eca3SAdam Manzanares 	if (!(dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLE)) {
22479f56eca3SAdam Manzanares 		dev->flags &= ~ATA_DFLAG_NCQ_PRIO;
22489f56eca3SAdam Manzanares 		return;
22499f56eca3SAdam Manzanares 	}
22509f56eca3SAdam Manzanares 
22518e061784SAdam Manzanares 	err_mask = ata_read_log_page(dev,
22521d51d5f3SChristoph Hellwig 				     ATA_LOG_IDENTIFY_DEVICE,
22538e061784SAdam Manzanares 				     ATA_LOG_SATA_SETTINGS,
22548e061784SAdam Manzanares 				     ap->sector_buf,
22558e061784SAdam Manzanares 				     1);
22568e061784SAdam Manzanares 	if (err_mask) {
22578e061784SAdam Manzanares 		ata_dev_dbg(dev,
22588e061784SAdam Manzanares 			    "failed to get Identify Device data, Emask 0x%x\n",
22598e061784SAdam Manzanares 			    err_mask);
22608e061784SAdam Manzanares 		return;
22618e061784SAdam Manzanares 	}
22628e061784SAdam Manzanares 
22639f56eca3SAdam Manzanares 	if (ap->sector_buf[ATA_LOG_NCQ_PRIO_OFFSET] & BIT(3)) {
22648e061784SAdam Manzanares 		dev->flags |= ATA_DFLAG_NCQ_PRIO;
22659f56eca3SAdam Manzanares 	} else {
22669f56eca3SAdam Manzanares 		dev->flags &= ~ATA_DFLAG_NCQ_PRIO;
22678e061784SAdam Manzanares 		ata_dev_dbg(dev, "SATA page does not support priority\n");
22689f56eca3SAdam Manzanares 	}
22698e061784SAdam Manzanares 
22708e061784SAdam Manzanares }
22718e061784SAdam Manzanares 
2272388539f3SShaohua Li static int ata_dev_config_ncq(struct ata_device *dev,
2273c6fd2807SJeff Garzik 			       char *desc, size_t desc_sz)
2274c6fd2807SJeff Garzik {
22759af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
2276c6fd2807SJeff Garzik 	int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
2277388539f3SShaohua Li 	unsigned int err_mask;
2278388539f3SShaohua Li 	char *aa_desc = "";
2279c6fd2807SJeff Garzik 
2280c6fd2807SJeff Garzik 	if (!ata_id_has_ncq(dev->id)) {
2281c6fd2807SJeff Garzik 		desc[0] = '\0';
2282388539f3SShaohua Li 		return 0;
2283c6fd2807SJeff Garzik 	}
228475683fe7STejun Heo 	if (dev->horkage & ATA_HORKAGE_NONCQ) {
22856919a0a6SAlan Cox 		snprintf(desc, desc_sz, "NCQ (not used)");
2286388539f3SShaohua Li 		return 0;
22876919a0a6SAlan Cox 	}
2288c6fd2807SJeff Garzik 	if (ap->flags & ATA_FLAG_NCQ) {
228969278f79SJens Axboe 		hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE);
2290c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_NCQ;
2291c6fd2807SJeff Garzik 	}
2292c6fd2807SJeff Garzik 
2293388539f3SShaohua Li 	if (!(dev->horkage & ATA_HORKAGE_BROKEN_FPDMA_AA) &&
2294388539f3SShaohua Li 		(ap->flags & ATA_FLAG_FPDMA_AA) &&
2295388539f3SShaohua Li 		ata_id_has_fpdma_aa(dev->id)) {
2296388539f3SShaohua Li 		err_mask = ata_dev_set_feature(dev, SETFEATURES_SATA_ENABLE,
2297388539f3SShaohua Li 			SATA_FPDMA_AA);
2298388539f3SShaohua Li 		if (err_mask) {
2299a9a79dfeSJoe Perches 			ata_dev_err(dev,
2300a9a79dfeSJoe Perches 				    "failed to enable AA (error_mask=0x%x)\n",
2301a9a79dfeSJoe Perches 				    err_mask);
2302388539f3SShaohua Li 			if (err_mask != AC_ERR_DEV) {
2303388539f3SShaohua Li 				dev->horkage |= ATA_HORKAGE_BROKEN_FPDMA_AA;
2304388539f3SShaohua Li 				return -EIO;
2305388539f3SShaohua Li 			}
2306388539f3SShaohua Li 		} else
2307388539f3SShaohua Li 			aa_desc = ", AA";
2308388539f3SShaohua Li 	}
2309388539f3SShaohua Li 
2310c6fd2807SJeff Garzik 	if (hdepth >= ddepth)
2311388539f3SShaohua Li 		snprintf(desc, desc_sz, "NCQ (depth %d)%s", ddepth, aa_desc);
2312c6fd2807SJeff Garzik 	else
2313388539f3SShaohua Li 		snprintf(desc, desc_sz, "NCQ (depth %d/%d)%s", hdepth,
2314388539f3SShaohua Li 			ddepth, aa_desc);
2315ed36911cSMarc Carino 
2316284b3b77SHannes Reinecke 	if ((ap->flags & ATA_FLAG_FPDMA_AUX)) {
2317284b3b77SHannes Reinecke 		if (ata_id_has_ncq_send_and_recv(dev->id))
23185a233551SHannes Reinecke 			ata_dev_config_ncq_send_recv(dev);
2319284b3b77SHannes Reinecke 		if (ata_id_has_ncq_non_data(dev->id))
2320284b3b77SHannes Reinecke 			ata_dev_config_ncq_non_data(dev);
23218e061784SAdam Manzanares 		if (ata_id_has_ncq_prio(dev->id))
23228e061784SAdam Manzanares 			ata_dev_config_ncq_prio(dev);
2323ed36911cSMarc Carino 	}
2324ed36911cSMarc Carino 
2325388539f3SShaohua Li 	return 0;
2326c6fd2807SJeff Garzik }
2327c6fd2807SJeff Garzik 
2328e87fd28cSHannes Reinecke static void ata_dev_config_sense_reporting(struct ata_device *dev)
2329e87fd28cSHannes Reinecke {
2330e87fd28cSHannes Reinecke 	unsigned int err_mask;
2331e87fd28cSHannes Reinecke 
2332e87fd28cSHannes Reinecke 	if (!ata_id_has_sense_reporting(dev->id))
2333e87fd28cSHannes Reinecke 		return;
2334e87fd28cSHannes Reinecke 
2335e87fd28cSHannes Reinecke 	if (ata_id_sense_reporting_enabled(dev->id))
2336e87fd28cSHannes Reinecke 		return;
2337e87fd28cSHannes Reinecke 
2338e87fd28cSHannes Reinecke 	err_mask = ata_dev_set_feature(dev, SETFEATURE_SENSE_DATA, 0x1);
2339e87fd28cSHannes Reinecke 	if (err_mask) {
2340e87fd28cSHannes Reinecke 		ata_dev_dbg(dev,
2341e87fd28cSHannes Reinecke 			    "failed to enable Sense Data Reporting, Emask 0x%x\n",
2342e87fd28cSHannes Reinecke 			    err_mask);
2343e87fd28cSHannes Reinecke 	}
2344e87fd28cSHannes Reinecke }
2345e87fd28cSHannes Reinecke 
23466d1003aeSHannes Reinecke static void ata_dev_config_zac(struct ata_device *dev)
23476d1003aeSHannes Reinecke {
23486d1003aeSHannes Reinecke 	struct ata_port *ap = dev->link->ap;
23496d1003aeSHannes Reinecke 	unsigned int err_mask;
23506d1003aeSHannes Reinecke 	u8 *identify_buf = ap->sector_buf;
23516d1003aeSHannes Reinecke 
23526d1003aeSHannes Reinecke 	dev->zac_zones_optimal_open = U32_MAX;
23536d1003aeSHannes Reinecke 	dev->zac_zones_optimal_nonseq = U32_MAX;
23546d1003aeSHannes Reinecke 	dev->zac_zones_max_open = U32_MAX;
23556d1003aeSHannes Reinecke 
23566d1003aeSHannes Reinecke 	/*
23576d1003aeSHannes Reinecke 	 * Always set the 'ZAC' flag for Host-managed devices.
23586d1003aeSHannes Reinecke 	 */
23596d1003aeSHannes Reinecke 	if (dev->class == ATA_DEV_ZAC)
23606d1003aeSHannes Reinecke 		dev->flags |= ATA_DFLAG_ZAC;
23616d1003aeSHannes Reinecke 	else if (ata_id_zoned_cap(dev->id) == 0x01)
23626d1003aeSHannes Reinecke 		/*
23636d1003aeSHannes Reinecke 		 * Check for host-aware devices.
23646d1003aeSHannes Reinecke 		 */
23656d1003aeSHannes Reinecke 		dev->flags |= ATA_DFLAG_ZAC;
23666d1003aeSHannes Reinecke 
23676d1003aeSHannes Reinecke 	if (!(dev->flags & ATA_DFLAG_ZAC))
23686d1003aeSHannes Reinecke 		return;
23696d1003aeSHannes Reinecke 
2370a0fd2454SChristoph Hellwig 	if (!ata_identify_page_supported(dev, ATA_LOG_ZONED_INFORMATION)) {
23716d1003aeSHannes Reinecke 		ata_dev_warn(dev,
23726d1003aeSHannes Reinecke 			     "ATA Zoned Information Log not supported\n");
23736d1003aeSHannes Reinecke 		return;
23746d1003aeSHannes Reinecke 	}
23756d1003aeSHannes Reinecke 
23766d1003aeSHannes Reinecke 	/*
23776d1003aeSHannes Reinecke 	 * Read IDENTIFY DEVICE data log, page 9 (Zoned-device information)
23786d1003aeSHannes Reinecke 	 */
23791d51d5f3SChristoph Hellwig 	err_mask = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE,
23806d1003aeSHannes Reinecke 				     ATA_LOG_ZONED_INFORMATION,
23816d1003aeSHannes Reinecke 				     identify_buf, 1);
23826d1003aeSHannes Reinecke 	if (!err_mask) {
23836d1003aeSHannes Reinecke 		u64 zoned_cap, opt_open, opt_nonseq, max_open;
23846d1003aeSHannes Reinecke 
23856d1003aeSHannes Reinecke 		zoned_cap = get_unaligned_le64(&identify_buf[8]);
23866d1003aeSHannes Reinecke 		if ((zoned_cap >> 63))
23876d1003aeSHannes Reinecke 			dev->zac_zoned_cap = (zoned_cap & 1);
23886d1003aeSHannes Reinecke 		opt_open = get_unaligned_le64(&identify_buf[24]);
23896d1003aeSHannes Reinecke 		if ((opt_open >> 63))
23906d1003aeSHannes Reinecke 			dev->zac_zones_optimal_open = (u32)opt_open;
23916d1003aeSHannes Reinecke 		opt_nonseq = get_unaligned_le64(&identify_buf[32]);
23926d1003aeSHannes Reinecke 		if ((opt_nonseq >> 63))
23936d1003aeSHannes Reinecke 			dev->zac_zones_optimal_nonseq = (u32)opt_nonseq;
23946d1003aeSHannes Reinecke 		max_open = get_unaligned_le64(&identify_buf[40]);
23956d1003aeSHannes Reinecke 		if ((max_open >> 63))
23966d1003aeSHannes Reinecke 			dev->zac_zones_max_open = (u32)max_open;
23976d1003aeSHannes Reinecke 	}
23986d1003aeSHannes Reinecke }
23996d1003aeSHannes Reinecke 
2400818831c8SChristoph Hellwig static void ata_dev_config_trusted(struct ata_device *dev)
2401818831c8SChristoph Hellwig {
2402818831c8SChristoph Hellwig 	struct ata_port *ap = dev->link->ap;
2403818831c8SChristoph Hellwig 	u64 trusted_cap;
2404818831c8SChristoph Hellwig 	unsigned int err;
2405818831c8SChristoph Hellwig 
2406e8f11db9SChristoph Hellwig 	if (!ata_id_has_trusted(dev->id))
2407e8f11db9SChristoph Hellwig 		return;
2408e8f11db9SChristoph Hellwig 
2409818831c8SChristoph Hellwig 	if (!ata_identify_page_supported(dev, ATA_LOG_SECURITY)) {
2410818831c8SChristoph Hellwig 		ata_dev_warn(dev,
2411818831c8SChristoph Hellwig 			     "Security Log not supported\n");
2412818831c8SChristoph Hellwig 		return;
2413818831c8SChristoph Hellwig 	}
2414818831c8SChristoph Hellwig 
2415818831c8SChristoph Hellwig 	err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, ATA_LOG_SECURITY,
2416818831c8SChristoph Hellwig 			ap->sector_buf, 1);
2417818831c8SChristoph Hellwig 	if (err) {
2418818831c8SChristoph Hellwig 		ata_dev_dbg(dev,
2419818831c8SChristoph Hellwig 			    "failed to read Security Log, Emask 0x%x\n", err);
2420818831c8SChristoph Hellwig 		return;
2421818831c8SChristoph Hellwig 	}
2422818831c8SChristoph Hellwig 
2423818831c8SChristoph Hellwig 	trusted_cap = get_unaligned_le64(&ap->sector_buf[40]);
2424818831c8SChristoph Hellwig 	if (!(trusted_cap & (1ULL << 63))) {
2425818831c8SChristoph Hellwig 		ata_dev_dbg(dev,
2426818831c8SChristoph Hellwig 			    "Trusted Computing capability qword not valid!\n");
2427818831c8SChristoph Hellwig 		return;
2428818831c8SChristoph Hellwig 	}
2429818831c8SChristoph Hellwig 
2430818831c8SChristoph Hellwig 	if (trusted_cap & (1 << 0))
2431818831c8SChristoph Hellwig 		dev->flags |= ATA_DFLAG_TRUSTED;
2432818831c8SChristoph Hellwig }
2433818831c8SChristoph Hellwig 
2434c6fd2807SJeff Garzik /**
2435c6fd2807SJeff Garzik  *	ata_dev_configure - Configure the specified ATA/ATAPI device
2436c6fd2807SJeff Garzik  *	@dev: Target device to configure
2437c6fd2807SJeff Garzik  *
2438c6fd2807SJeff Garzik  *	Configure @dev according to @dev->id.  Generic and low-level
2439c6fd2807SJeff Garzik  *	driver specific fixups are also applied.
2440c6fd2807SJeff Garzik  *
2441c6fd2807SJeff Garzik  *	LOCKING:
2442c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
2443c6fd2807SJeff Garzik  *
2444c6fd2807SJeff Garzik  *	RETURNS:
2445c6fd2807SJeff Garzik  *	0 on success, -errno otherwise
2446c6fd2807SJeff Garzik  */
2447efdaedc4STejun Heo int ata_dev_configure(struct ata_device *dev)
2448c6fd2807SJeff Garzik {
24499af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
24509af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
24516746544cSTejun Heo 	int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
2452c6fd2807SJeff Garzik 	const u16 *id = dev->id;
24537dc951aeSTejun Heo 	unsigned long xfer_mask;
245465fe1f0fSShane Huang 	unsigned int err_mask;
2455b352e57dSAlan Cox 	char revbuf[7];		/* XYZ-99\0 */
24563f64f565SEric D. Mudama 	char fwrevbuf[ATA_ID_FW_REV_LEN+1];
24573f64f565SEric D. Mudama 	char modelbuf[ATA_ID_PROD_LEN+1];
2458c6fd2807SJeff Garzik 	int rc;
2459c6fd2807SJeff Garzik 
2460c6fd2807SJeff Garzik 	if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
2461a9a79dfeSJoe Perches 		ata_dev_info(dev, "%s: ENTER/EXIT -- nodev\n", __func__);
2462c6fd2807SJeff Garzik 		return 0;
2463c6fd2807SJeff Garzik 	}
2464c6fd2807SJeff Garzik 
2465c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2466a9a79dfeSJoe Perches 		ata_dev_dbg(dev, "%s: ENTER\n", __func__);
2467c6fd2807SJeff Garzik 
246875683fe7STejun Heo 	/* set horkage */
246975683fe7STejun Heo 	dev->horkage |= ata_dev_blacklisted(dev);
247033267325STejun Heo 	ata_force_horkage(dev);
247175683fe7STejun Heo 
247250af2fa1STejun Heo 	if (dev->horkage & ATA_HORKAGE_DISABLE) {
2473a9a79dfeSJoe Perches 		ata_dev_info(dev, "unsupported device, disabling\n");
247450af2fa1STejun Heo 		ata_dev_disable(dev);
247550af2fa1STejun Heo 		return 0;
247650af2fa1STejun Heo 	}
247750af2fa1STejun Heo 
24782486fa56STejun Heo 	if ((!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) &&
24792486fa56STejun Heo 	    dev->class == ATA_DEV_ATAPI) {
2480a9a79dfeSJoe Perches 		ata_dev_warn(dev, "WARNING: ATAPI is %s, device ignored\n",
24812486fa56STejun Heo 			     atapi_enabled ? "not supported with this driver"
24822486fa56STejun Heo 			     : "disabled");
24832486fa56STejun Heo 		ata_dev_disable(dev);
24842486fa56STejun Heo 		return 0;
24852486fa56STejun Heo 	}
24862486fa56STejun Heo 
24879062712fSTejun Heo 	rc = ata_do_link_spd_horkage(dev);
24889062712fSTejun Heo 	if (rc)
24899062712fSTejun Heo 		return rc;
24909062712fSTejun Heo 
2491ecd75ad5STejun Heo 	/* some WD SATA-1 drives have issues with LPM, turn on NOLPM for them */
2492ecd75ad5STejun Heo 	if ((dev->horkage & ATA_HORKAGE_WD_BROKEN_LPM) &&
2493ecd75ad5STejun Heo 	    (id[ATA_ID_SATA_CAPABILITY] & 0xe) == 0x2)
2494ecd75ad5STejun Heo 		dev->horkage |= ATA_HORKAGE_NOLPM;
2495ecd75ad5STejun Heo 
2496240630e6SHans de Goede 	if (ap->flags & ATA_FLAG_NO_LPM)
2497240630e6SHans de Goede 		dev->horkage |= ATA_HORKAGE_NOLPM;
2498240630e6SHans de Goede 
2499ecd75ad5STejun Heo 	if (dev->horkage & ATA_HORKAGE_NOLPM) {
2500ecd75ad5STejun Heo 		ata_dev_warn(dev, "LPM support broken, forcing max_power\n");
2501ecd75ad5STejun Heo 		dev->link->ap->target_lpm_policy = ATA_LPM_MAX_POWER;
2502ecd75ad5STejun Heo 	}
2503ecd75ad5STejun Heo 
25046746544cSTejun Heo 	/* let ACPI work its magic */
25056746544cSTejun Heo 	rc = ata_acpi_on_devcfg(dev);
25066746544cSTejun Heo 	if (rc)
25076746544cSTejun Heo 		return rc;
250808573a86SKristen Carlson Accardi 
250905027adcSTejun Heo 	/* massage HPA, do it early as it might change IDENTIFY data */
251005027adcSTejun Heo 	rc = ata_hpa_resize(dev);
251105027adcSTejun Heo 	if (rc)
251205027adcSTejun Heo 		return rc;
251305027adcSTejun Heo 
2514c6fd2807SJeff Garzik 	/* print device capabilities */
2515c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2516a9a79dfeSJoe Perches 		ata_dev_dbg(dev,
2517c6fd2807SJeff Garzik 			    "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
2518c6fd2807SJeff Garzik 			    "85:%04x 86:%04x 87:%04x 88:%04x\n",
25197f5e4e8dSHarvey Harrison 			    __func__,
2520c6fd2807SJeff Garzik 			    id[49], id[82], id[83], id[84],
2521c6fd2807SJeff Garzik 			    id[85], id[86], id[87], id[88]);
2522c6fd2807SJeff Garzik 
2523c6fd2807SJeff Garzik 	/* initialize to-be-configured parameters */
2524c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_CFG_MASK;
2525c6fd2807SJeff Garzik 	dev->max_sectors = 0;
2526c6fd2807SJeff Garzik 	dev->cdb_len = 0;
2527c6fd2807SJeff Garzik 	dev->n_sectors = 0;
2528c6fd2807SJeff Garzik 	dev->cylinders = 0;
2529c6fd2807SJeff Garzik 	dev->heads = 0;
2530c6fd2807SJeff Garzik 	dev->sectors = 0;
2531e18086d6SMark Lord 	dev->multi_count = 0;
2532c6fd2807SJeff Garzik 
2533c6fd2807SJeff Garzik 	/*
2534c6fd2807SJeff Garzik 	 * common ATA, ATAPI feature tests
2535c6fd2807SJeff Garzik 	 */
2536c6fd2807SJeff Garzik 
2537c6fd2807SJeff Garzik 	/* find max transfer mode; for printk only */
2538c6fd2807SJeff Garzik 	xfer_mask = ata_id_xfermask(id);
2539c6fd2807SJeff Garzik 
2540c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2541c6fd2807SJeff Garzik 		ata_dump_id(id);
2542c6fd2807SJeff Garzik 
2543ef143d57SAlbert Lee 	/* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
2544ef143d57SAlbert Lee 	ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
2545ef143d57SAlbert Lee 			sizeof(fwrevbuf));
2546ef143d57SAlbert Lee 
2547ef143d57SAlbert Lee 	ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
2548ef143d57SAlbert Lee 			sizeof(modelbuf));
2549ef143d57SAlbert Lee 
2550c6fd2807SJeff Garzik 	/* ATA-specific feature tests */
25519162c657SHannes Reinecke 	if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) {
2552b352e57dSAlan Cox 		if (ata_id_is_cfa(id)) {
255362afe5d7SSergei Shtylyov 			/* CPRM may make this media unusable */
255462afe5d7SSergei Shtylyov 			if (id[ATA_ID_CFA_KEY_MGMT] & 1)
2555a9a79dfeSJoe Perches 				ata_dev_warn(dev,
2556a9a79dfeSJoe Perches 	"supports DRM functions and may not be fully accessible\n");
2557b352e57dSAlan Cox 			snprintf(revbuf, 7, "CFA");
2558ae8d4ee7SAlan Cox 		} else {
2559b352e57dSAlan Cox 			snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
2560ae8d4ee7SAlan Cox 			/* Warn the user if the device has TPM extensions */
2561ae8d4ee7SAlan Cox 			if (ata_id_has_tpm(id))
2562a9a79dfeSJoe Perches 				ata_dev_warn(dev,
2563a9a79dfeSJoe Perches 	"supports DRM functions and may not be fully accessible\n");
2564ae8d4ee7SAlan Cox 		}
2565b352e57dSAlan Cox 
2566c6fd2807SJeff Garzik 		dev->n_sectors = ata_id_n_sectors(id);
2567c6fd2807SJeff Garzik 
2568e18086d6SMark Lord 		/* get current R/W Multiple count setting */
2569e18086d6SMark Lord 		if ((dev->id[47] >> 8) == 0x80 && (dev->id[59] & 0x100)) {
2570e18086d6SMark Lord 			unsigned int max = dev->id[47] & 0xff;
2571e18086d6SMark Lord 			unsigned int cnt = dev->id[59] & 0xff;
2572e18086d6SMark Lord 			/* only recognize/allow powers of two here */
2573e18086d6SMark Lord 			if (is_power_of_2(max) && is_power_of_2(cnt))
2574e18086d6SMark Lord 				if (cnt <= max)
2575e18086d6SMark Lord 					dev->multi_count = cnt;
2576e18086d6SMark Lord 		}
25773f64f565SEric D. Mudama 
2578c6fd2807SJeff Garzik 		if (ata_id_has_lba(id)) {
2579c6fd2807SJeff Garzik 			const char *lba_desc;
2580388539f3SShaohua Li 			char ncq_desc[24];
2581c6fd2807SJeff Garzik 
2582c6fd2807SJeff Garzik 			lba_desc = "LBA";
2583c6fd2807SJeff Garzik 			dev->flags |= ATA_DFLAG_LBA;
2584c6fd2807SJeff Garzik 			if (ata_id_has_lba48(id)) {
2585c6fd2807SJeff Garzik 				dev->flags |= ATA_DFLAG_LBA48;
2586c6fd2807SJeff Garzik 				lba_desc = "LBA48";
25876fc49adbSTejun Heo 
25886fc49adbSTejun Heo 				if (dev->n_sectors >= (1UL << 28) &&
25896fc49adbSTejun Heo 				    ata_id_has_flush_ext(id))
25906fc49adbSTejun Heo 					dev->flags |= ATA_DFLAG_FLUSH_EXT;
2591c6fd2807SJeff Garzik 			}
2592c6fd2807SJeff Garzik 
2593c6fd2807SJeff Garzik 			/* config NCQ */
2594388539f3SShaohua Li 			rc = ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
2595388539f3SShaohua Li 			if (rc)
2596388539f3SShaohua Li 				return rc;
2597c6fd2807SJeff Garzik 
2598c6fd2807SJeff Garzik 			/* print device info to dmesg */
25993f64f565SEric D. Mudama 			if (ata_msg_drv(ap) && print_info) {
2600a9a79dfeSJoe Perches 				ata_dev_info(dev, "%s: %s, %s, max %s\n",
26013f64f565SEric D. Mudama 					     revbuf, modelbuf, fwrevbuf,
26023f64f565SEric D. Mudama 					     ata_mode_string(xfer_mask));
2603a9a79dfeSJoe Perches 				ata_dev_info(dev,
2604a9a79dfeSJoe Perches 					     "%llu sectors, multi %u: %s %s\n",
2605c6fd2807SJeff Garzik 					(unsigned long long)dev->n_sectors,
26063f64f565SEric D. Mudama 					dev->multi_count, lba_desc, ncq_desc);
26073f64f565SEric D. Mudama 			}
2608c6fd2807SJeff Garzik 		} else {
2609c6fd2807SJeff Garzik 			/* CHS */
2610c6fd2807SJeff Garzik 
2611c6fd2807SJeff Garzik 			/* Default translation */
2612c6fd2807SJeff Garzik 			dev->cylinders	= id[1];
2613c6fd2807SJeff Garzik 			dev->heads	= id[3];
2614c6fd2807SJeff Garzik 			dev->sectors	= id[6];
2615c6fd2807SJeff Garzik 
2616c6fd2807SJeff Garzik 			if (ata_id_current_chs_valid(id)) {
2617c6fd2807SJeff Garzik 				/* Current CHS translation is valid. */
2618c6fd2807SJeff Garzik 				dev->cylinders = id[54];
2619c6fd2807SJeff Garzik 				dev->heads     = id[55];
2620c6fd2807SJeff Garzik 				dev->sectors   = id[56];
2621c6fd2807SJeff Garzik 			}
2622c6fd2807SJeff Garzik 
2623c6fd2807SJeff Garzik 			/* print device info to dmesg */
26243f64f565SEric D. Mudama 			if (ata_msg_drv(ap) && print_info) {
2625a9a79dfeSJoe Perches 				ata_dev_info(dev, "%s: %s, %s, max %s\n",
26263f64f565SEric D. Mudama 					     revbuf,	modelbuf, fwrevbuf,
26273f64f565SEric D. Mudama 					     ata_mode_string(xfer_mask));
2628a9a79dfeSJoe Perches 				ata_dev_info(dev,
2629a9a79dfeSJoe Perches 					     "%llu sectors, multi %u, CHS %u/%u/%u\n",
26303f64f565SEric D. Mudama 					     (unsigned long long)dev->n_sectors,
26313f64f565SEric D. Mudama 					     dev->multi_count, dev->cylinders,
26323f64f565SEric D. Mudama 					     dev->heads, dev->sectors);
26333f64f565SEric D. Mudama 			}
2634c6fd2807SJeff Garzik 		}
2635c6fd2807SJeff Garzik 
2636803739d2SShane Huang 		/* Check and mark DevSlp capability. Get DevSlp timing variables
2637803739d2SShane Huang 		 * from SATA Settings page of Identify Device Data Log.
263865fe1f0fSShane Huang 		 */
2639803739d2SShane Huang 		if (ata_id_has_devslp(dev->id)) {
26408e725c7fSDavid Woodhouse 			u8 *sata_setting = ap->sector_buf;
2641803739d2SShane Huang 			int i, j;
2642803739d2SShane Huang 
2643803739d2SShane Huang 			dev->flags |= ATA_DFLAG_DEVSLP;
264465fe1f0fSShane Huang 			err_mask = ata_read_log_page(dev,
26451d51d5f3SChristoph Hellwig 						     ATA_LOG_IDENTIFY_DEVICE,
264665fe1f0fSShane Huang 						     ATA_LOG_SATA_SETTINGS,
2647803739d2SShane Huang 						     sata_setting,
264865fe1f0fSShane Huang 						     1);
264965fe1f0fSShane Huang 			if (err_mask)
265065fe1f0fSShane Huang 				ata_dev_dbg(dev,
265165fe1f0fSShane Huang 					    "failed to get Identify Device Data, Emask 0x%x\n",
265265fe1f0fSShane Huang 					    err_mask);
2653803739d2SShane Huang 			else
2654803739d2SShane Huang 				for (i = 0; i < ATA_LOG_DEVSLP_SIZE; i++) {
2655803739d2SShane Huang 					j = ATA_LOG_DEVSLP_OFFSET + i;
2656803739d2SShane Huang 					dev->devslp_timing[i] = sata_setting[j];
2657803739d2SShane Huang 				}
265865fe1f0fSShane Huang 		}
2659e87fd28cSHannes Reinecke 		ata_dev_config_sense_reporting(dev);
26606d1003aeSHannes Reinecke 		ata_dev_config_zac(dev);
2661818831c8SChristoph Hellwig 		ata_dev_config_trusted(dev);
2662b1ffbf85SMinwoo Im 		dev->cdb_len = 32;
2663c6fd2807SJeff Garzik 	}
2664c6fd2807SJeff Garzik 
2665c6fd2807SJeff Garzik 	/* ATAPI-specific feature tests */
2666c6fd2807SJeff Garzik 	else if (dev->class == ATA_DEV_ATAPI) {
2667854c73a2STejun Heo 		const char *cdb_intr_string = "";
2668854c73a2STejun Heo 		const char *atapi_an_string = "";
266991163006STejun Heo 		const char *dma_dir_string = "";
26707d77b247STejun Heo 		u32 sntf;
2671c6fd2807SJeff Garzik 
2672c6fd2807SJeff Garzik 		rc = atapi_cdb_len(id);
2673c6fd2807SJeff Garzik 		if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
2674c6fd2807SJeff Garzik 			if (ata_msg_warn(ap))
2675a9a79dfeSJoe Perches 				ata_dev_warn(dev, "unsupported CDB len\n");
2676c6fd2807SJeff Garzik 			rc = -EINVAL;
2677c6fd2807SJeff Garzik 			goto err_out_nosup;
2678c6fd2807SJeff Garzik 		}
2679c6fd2807SJeff Garzik 		dev->cdb_len = (unsigned int) rc;
2680c6fd2807SJeff Garzik 
26817d77b247STejun Heo 		/* Enable ATAPI AN if both the host and device have
26827d77b247STejun Heo 		 * the support.  If PMP is attached, SNTF is required
26837d77b247STejun Heo 		 * to enable ATAPI AN to discern between PHY status
26847d77b247STejun Heo 		 * changed notifications and ATAPI ANs.
26859f45cbd3SKristen Carlson Accardi 		 */
2686e7ecd435STejun Heo 		if (atapi_an &&
2687e7ecd435STejun Heo 		    (ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
2688071f44b1STejun Heo 		    (!sata_pmp_attached(ap) ||
26897d77b247STejun Heo 		     sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
26909f45cbd3SKristen Carlson Accardi 			/* issue SET feature command to turn this on */
2691218f3d30SJeff Garzik 			err_mask = ata_dev_set_feature(dev,
2692218f3d30SJeff Garzik 					SETFEATURES_SATA_ENABLE, SATA_AN);
2693854c73a2STejun Heo 			if (err_mask)
2694a9a79dfeSJoe Perches 				ata_dev_err(dev,
2695a9a79dfeSJoe Perches 					    "failed to enable ATAPI AN (err_mask=0x%x)\n",
2696a9a79dfeSJoe Perches 					    err_mask);
2697854c73a2STejun Heo 			else {
26989f45cbd3SKristen Carlson Accardi 				dev->flags |= ATA_DFLAG_AN;
2699854c73a2STejun Heo 				atapi_an_string = ", ATAPI AN";
2700854c73a2STejun Heo 			}
27019f45cbd3SKristen Carlson Accardi 		}
27029f45cbd3SKristen Carlson Accardi 
2703c6fd2807SJeff Garzik 		if (ata_id_cdb_intr(dev->id)) {
2704c6fd2807SJeff Garzik 			dev->flags |= ATA_DFLAG_CDB_INTR;
2705c6fd2807SJeff Garzik 			cdb_intr_string = ", CDB intr";
2706c6fd2807SJeff Garzik 		}
2707c6fd2807SJeff Garzik 
2708966fbe19SVincent Pelletier 		if (atapi_dmadir || (dev->horkage & ATA_HORKAGE_ATAPI_DMADIR) || atapi_id_dmadir(dev->id)) {
270991163006STejun Heo 			dev->flags |= ATA_DFLAG_DMADIR;
271091163006STejun Heo 			dma_dir_string = ", DMADIR";
271191163006STejun Heo 		}
271291163006STejun Heo 
2713afe75951SAaron Lu 		if (ata_id_has_da(dev->id)) {
2714b1354cbbSLin Ming 			dev->flags |= ATA_DFLAG_DA;
2715afe75951SAaron Lu 			zpodd_init(dev);
2716afe75951SAaron Lu 		}
2717b1354cbbSLin Ming 
2718c6fd2807SJeff Garzik 		/* print device info to dmesg */
2719c6fd2807SJeff Garzik 		if (ata_msg_drv(ap) && print_info)
2720a9a79dfeSJoe Perches 			ata_dev_info(dev,
272191163006STejun Heo 				     "ATAPI: %s, %s, max %s%s%s%s\n",
2722ef143d57SAlbert Lee 				     modelbuf, fwrevbuf,
2723c6fd2807SJeff Garzik 				     ata_mode_string(xfer_mask),
272491163006STejun Heo 				     cdb_intr_string, atapi_an_string,
272591163006STejun Heo 				     dma_dir_string);
2726c6fd2807SJeff Garzik 	}
2727c6fd2807SJeff Garzik 
2728914ed354STejun Heo 	/* determine max_sectors */
2729914ed354STejun Heo 	dev->max_sectors = ATA_MAX_SECTORS;
2730914ed354STejun Heo 	if (dev->flags & ATA_DFLAG_LBA48)
2731914ed354STejun Heo 		dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2732914ed354STejun Heo 
2733c5038fc0SAlan Cox 	/* Limit PATA drive on SATA cable bridge transfers to udma5,
2734c5038fc0SAlan Cox 	   200 sectors */
2735c6fd2807SJeff Garzik 	if (ata_dev_knobble(dev)) {
2736c6fd2807SJeff Garzik 		if (ata_msg_drv(ap) && print_info)
2737a9a79dfeSJoe Perches 			ata_dev_info(dev, "applying bridge limits\n");
2738c6fd2807SJeff Garzik 		dev->udma_mask &= ATA_UDMA5;
2739c6fd2807SJeff Garzik 		dev->max_sectors = ATA_MAX_SECTORS;
2740c6fd2807SJeff Garzik 	}
2741c6fd2807SJeff Garzik 
2742f8d8e579STony Battersby 	if ((dev->class == ATA_DEV_ATAPI) &&
2743f442cd86SAlbert Lee 	    (atapi_command_packet_set(id) == TYPE_TAPE)) {
2744f8d8e579STony Battersby 		dev->max_sectors = ATA_MAX_SECTORS_TAPE;
2745f442cd86SAlbert Lee 		dev->horkage |= ATA_HORKAGE_STUCK_ERR;
2746f442cd86SAlbert Lee 	}
2747f8d8e579STony Battersby 
274875683fe7STejun Heo 	if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
274903ec52deSTejun Heo 		dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
275003ec52deSTejun Heo 					 dev->max_sectors);
275118d6e9d5SAlbert Lee 
2752af34d637SDavid Milburn 	if (dev->horkage & ATA_HORKAGE_MAX_SEC_1024)
2753af34d637SDavid Milburn 		dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_1024,
2754af34d637SDavid Milburn 					 dev->max_sectors);
2755af34d637SDavid Milburn 
2756a32450e1SShan Hai 	if (dev->horkage & ATA_HORKAGE_MAX_SEC_LBA48)
2757a32450e1SShan Hai 		dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2758a32450e1SShan Hai 
2759c6fd2807SJeff Garzik 	if (ap->ops->dev_config)
2760cd0d3bbcSAlan 		ap->ops->dev_config(dev);
2761c6fd2807SJeff Garzik 
2762c5038fc0SAlan Cox 	if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2763c5038fc0SAlan Cox 		/* Let the user know. We don't want to disallow opens for
2764c5038fc0SAlan Cox 		   rescue purposes, or in case the vendor is just a blithering
2765c5038fc0SAlan Cox 		   idiot. Do this after the dev_config call as some controllers
2766c5038fc0SAlan Cox 		   with buggy firmware may want to avoid reporting false device
2767c5038fc0SAlan Cox 		   bugs */
2768c5038fc0SAlan Cox 
2769c5038fc0SAlan Cox 		if (print_info) {
2770a9a79dfeSJoe Perches 			ata_dev_warn(dev,
2771c5038fc0SAlan Cox "Drive reports diagnostics failure. This may indicate a drive\n");
2772a9a79dfeSJoe Perches 			ata_dev_warn(dev,
2773c5038fc0SAlan Cox "fault or invalid emulation. Contact drive vendor for information.\n");
2774c5038fc0SAlan Cox 		}
2775c5038fc0SAlan Cox 	}
2776c5038fc0SAlan Cox 
2777ac70a964STejun Heo 	if ((dev->horkage & ATA_HORKAGE_FIRMWARE_WARN) && print_info) {
2778a9a79dfeSJoe Perches 		ata_dev_warn(dev, "WARNING: device requires firmware update to be fully functional\n");
2779a9a79dfeSJoe Perches 		ata_dev_warn(dev, "         contact the vendor or visit http://ata.wiki.kernel.org\n");
2780ac70a964STejun Heo 	}
2781ac70a964STejun Heo 
2782c6fd2807SJeff Garzik 	return 0;
2783c6fd2807SJeff Garzik 
2784c6fd2807SJeff Garzik err_out_nosup:
2785c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2786a9a79dfeSJoe Perches 		ata_dev_dbg(dev, "%s: EXIT, err\n", __func__);
2787c6fd2807SJeff Garzik 	return rc;
2788c6fd2807SJeff Garzik }
2789c6fd2807SJeff Garzik 
2790c6fd2807SJeff Garzik /**
27912e41e8e6SAlan Cox  *	ata_cable_40wire	-	return 40 wire cable type
2792be0d18dfSAlan Cox  *	@ap: port
2793be0d18dfSAlan Cox  *
27942e41e8e6SAlan Cox  *	Helper method for drivers which want to hardwire 40 wire cable
2795be0d18dfSAlan Cox  *	detection.
2796be0d18dfSAlan Cox  */
2797be0d18dfSAlan Cox 
2798be0d18dfSAlan Cox int ata_cable_40wire(struct ata_port *ap)
2799be0d18dfSAlan Cox {
2800be0d18dfSAlan Cox 	return ATA_CBL_PATA40;
2801be0d18dfSAlan Cox }
2802be0d18dfSAlan Cox 
2803be0d18dfSAlan Cox /**
28042e41e8e6SAlan Cox  *	ata_cable_80wire	-	return 80 wire cable type
2805be0d18dfSAlan Cox  *	@ap: port
2806be0d18dfSAlan Cox  *
28072e41e8e6SAlan Cox  *	Helper method for drivers which want to hardwire 80 wire cable
2808be0d18dfSAlan Cox  *	detection.
2809be0d18dfSAlan Cox  */
2810be0d18dfSAlan Cox 
2811be0d18dfSAlan Cox int ata_cable_80wire(struct ata_port *ap)
2812be0d18dfSAlan Cox {
2813be0d18dfSAlan Cox 	return ATA_CBL_PATA80;
2814be0d18dfSAlan Cox }
2815be0d18dfSAlan Cox 
2816be0d18dfSAlan Cox /**
2817be0d18dfSAlan Cox  *	ata_cable_unknown	-	return unknown PATA cable.
2818be0d18dfSAlan Cox  *	@ap: port
2819be0d18dfSAlan Cox  *
2820be0d18dfSAlan Cox  *	Helper method for drivers which have no PATA cable detection.
2821be0d18dfSAlan Cox  */
2822be0d18dfSAlan Cox 
2823be0d18dfSAlan Cox int ata_cable_unknown(struct ata_port *ap)
2824be0d18dfSAlan Cox {
2825be0d18dfSAlan Cox 	return ATA_CBL_PATA_UNK;
2826be0d18dfSAlan Cox }
2827be0d18dfSAlan Cox 
2828be0d18dfSAlan Cox /**
2829c88f90c3STejun Heo  *	ata_cable_ignore	-	return ignored PATA cable.
2830c88f90c3STejun Heo  *	@ap: port
2831c88f90c3STejun Heo  *
2832c88f90c3STejun Heo  *	Helper method for drivers which don't use cable type to limit
2833c88f90c3STejun Heo  *	transfer mode.
2834c88f90c3STejun Heo  */
2835c88f90c3STejun Heo int ata_cable_ignore(struct ata_port *ap)
2836c88f90c3STejun Heo {
2837c88f90c3STejun Heo 	return ATA_CBL_PATA_IGN;
2838c88f90c3STejun Heo }
2839c88f90c3STejun Heo 
2840c88f90c3STejun Heo /**
2841be0d18dfSAlan Cox  *	ata_cable_sata	-	return SATA cable type
2842be0d18dfSAlan Cox  *	@ap: port
2843be0d18dfSAlan Cox  *
2844be0d18dfSAlan Cox  *	Helper method for drivers which have SATA cables
2845be0d18dfSAlan Cox  */
2846be0d18dfSAlan Cox 
2847be0d18dfSAlan Cox int ata_cable_sata(struct ata_port *ap)
2848be0d18dfSAlan Cox {
2849be0d18dfSAlan Cox 	return ATA_CBL_SATA;
2850be0d18dfSAlan Cox }
2851be0d18dfSAlan Cox 
2852be0d18dfSAlan Cox /**
2853c6fd2807SJeff Garzik  *	ata_bus_probe - Reset and probe ATA bus
2854c6fd2807SJeff Garzik  *	@ap: Bus to probe
2855c6fd2807SJeff Garzik  *
2856c6fd2807SJeff Garzik  *	Master ATA bus probing function.  Initiates a hardware-dependent
2857c6fd2807SJeff Garzik  *	bus reset, then attempts to identify any devices found on
2858c6fd2807SJeff Garzik  *	the bus.
2859c6fd2807SJeff Garzik  *
2860c6fd2807SJeff Garzik  *	LOCKING:
2861c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
2862c6fd2807SJeff Garzik  *
2863c6fd2807SJeff Garzik  *	RETURNS:
2864c6fd2807SJeff Garzik  *	Zero on success, negative errno otherwise.
2865c6fd2807SJeff Garzik  */
2866c6fd2807SJeff Garzik 
2867c6fd2807SJeff Garzik int ata_bus_probe(struct ata_port *ap)
2868c6fd2807SJeff Garzik {
2869c6fd2807SJeff Garzik 	unsigned int classes[ATA_MAX_DEVICES];
2870c6fd2807SJeff Garzik 	int tries[ATA_MAX_DEVICES];
2871f58229f8STejun Heo 	int rc;
2872c6fd2807SJeff Garzik 	struct ata_device *dev;
2873c6fd2807SJeff Garzik 
28741eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL)
2875f58229f8STejun Heo 		tries[dev->devno] = ATA_PROBE_MAX_TRIES;
2876c6fd2807SJeff Garzik 
2877c6fd2807SJeff Garzik  retry:
28781eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL) {
2879cdeab114STejun Heo 		/* If we issue an SRST then an ATA drive (not ATAPI)
2880cdeab114STejun Heo 		 * may change configuration and be in PIO0 timing. If
2881cdeab114STejun Heo 		 * we do a hard reset (or are coming from power on)
2882cdeab114STejun Heo 		 * this is true for ATA or ATAPI. Until we've set a
2883cdeab114STejun Heo 		 * suitable controller mode we should not touch the
2884cdeab114STejun Heo 		 * bus as we may be talking too fast.
2885cdeab114STejun Heo 		 */
2886cdeab114STejun Heo 		dev->pio_mode = XFER_PIO_0;
28875416912aSAaron Lu 		dev->dma_mode = 0xff;
2888cdeab114STejun Heo 
2889cdeab114STejun Heo 		/* If the controller has a pio mode setup function
2890cdeab114STejun Heo 		 * then use it to set the chipset to rights. Don't
2891cdeab114STejun Heo 		 * touch the DMA setup as that will be dealt with when
2892cdeab114STejun Heo 		 * configuring devices.
2893cdeab114STejun Heo 		 */
2894cdeab114STejun Heo 		if (ap->ops->set_piomode)
2895cdeab114STejun Heo 			ap->ops->set_piomode(ap, dev);
2896cdeab114STejun Heo 	}
2897cdeab114STejun Heo 
2898c6fd2807SJeff Garzik 	/* reset and determine device classes */
2899c6fd2807SJeff Garzik 	ap->ops->phy_reset(ap);
2900c6fd2807SJeff Garzik 
29011eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL) {
29023e4ec344STejun Heo 		if (dev->class != ATA_DEV_UNKNOWN)
2903c6fd2807SJeff Garzik 			classes[dev->devno] = dev->class;
2904c6fd2807SJeff Garzik 		else
2905c6fd2807SJeff Garzik 			classes[dev->devno] = ATA_DEV_NONE;
2906c6fd2807SJeff Garzik 
2907c6fd2807SJeff Garzik 		dev->class = ATA_DEV_UNKNOWN;
2908c6fd2807SJeff Garzik 	}
2909c6fd2807SJeff Garzik 
2910f31f0cc2SJeff Garzik 	/* read IDENTIFY page and configure devices. We have to do the identify
2911f31f0cc2SJeff Garzik 	   specific sequence bass-ackwards so that PDIAG- is released by
2912f31f0cc2SJeff Garzik 	   the slave device */
2913f31f0cc2SJeff Garzik 
29141eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL_REVERSE) {
2915f58229f8STejun Heo 		if (tries[dev->devno])
2916f58229f8STejun Heo 			dev->class = classes[dev->devno];
2917c6fd2807SJeff Garzik 
2918c6fd2807SJeff Garzik 		if (!ata_dev_enabled(dev))
2919c6fd2807SJeff Garzik 			continue;
2920c6fd2807SJeff Garzik 
2921bff04647STejun Heo 		rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2922bff04647STejun Heo 				     dev->id);
2923c6fd2807SJeff Garzik 		if (rc)
2924c6fd2807SJeff Garzik 			goto fail;
2925f31f0cc2SJeff Garzik 	}
2926f31f0cc2SJeff Garzik 
2927be0d18dfSAlan Cox 	/* Now ask for the cable type as PDIAG- should have been released */
2928be0d18dfSAlan Cox 	if (ap->ops->cable_detect)
2929be0d18dfSAlan Cox 		ap->cbl = ap->ops->cable_detect(ap);
2930be0d18dfSAlan Cox 
29311eca4365STejun Heo 	/* We may have SATA bridge glue hiding here irrespective of
29321eca4365STejun Heo 	 * the reported cable types and sensed types.  When SATA
29331eca4365STejun Heo 	 * drives indicate we have a bridge, we don't know which end
29341eca4365STejun Heo 	 * of the link the bridge is which is a problem.
29351eca4365STejun Heo 	 */
29361eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED)
2937614fe29bSAlan Cox 		if (ata_id_is_sata(dev->id))
2938614fe29bSAlan Cox 			ap->cbl = ATA_CBL_SATA;
2939614fe29bSAlan Cox 
2940f31f0cc2SJeff Garzik 	/* After the identify sequence we can now set up the devices. We do
2941f31f0cc2SJeff Garzik 	   this in the normal order so that the user doesn't get confused */
2942f31f0cc2SJeff Garzik 
29431eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED) {
29449af5c9c9STejun Heo 		ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
2945efdaedc4STejun Heo 		rc = ata_dev_configure(dev);
29469af5c9c9STejun Heo 		ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
2947c6fd2807SJeff Garzik 		if (rc)
2948c6fd2807SJeff Garzik 			goto fail;
2949c6fd2807SJeff Garzik 	}
2950c6fd2807SJeff Garzik 
2951c6fd2807SJeff Garzik 	/* configure transfer mode */
29520260731fSTejun Heo 	rc = ata_set_mode(&ap->link, &dev);
29534ae72a1eSTejun Heo 	if (rc)
2954c6fd2807SJeff Garzik 		goto fail;
2955c6fd2807SJeff Garzik 
29561eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED)
2957c6fd2807SJeff Garzik 		return 0;
2958c6fd2807SJeff Garzik 
2959c6fd2807SJeff Garzik 	return -ENODEV;
2960c6fd2807SJeff Garzik 
2961c6fd2807SJeff Garzik  fail:
29624ae72a1eSTejun Heo 	tries[dev->devno]--;
29634ae72a1eSTejun Heo 
2964c6fd2807SJeff Garzik 	switch (rc) {
2965c6fd2807SJeff Garzik 	case -EINVAL:
29664ae72a1eSTejun Heo 		/* eeek, something went very wrong, give up */
2967c6fd2807SJeff Garzik 		tries[dev->devno] = 0;
2968c6fd2807SJeff Garzik 		break;
29694ae72a1eSTejun Heo 
29704ae72a1eSTejun Heo 	case -ENODEV:
29714ae72a1eSTejun Heo 		/* give it just one more chance */
29724ae72a1eSTejun Heo 		tries[dev->devno] = min(tries[dev->devno], 1);
297305b83605SGustavo A. R. Silva 		/* fall through */
2974c6fd2807SJeff Garzik 	case -EIO:
29754ae72a1eSTejun Heo 		if (tries[dev->devno] == 1) {
29764ae72a1eSTejun Heo 			/* This is the last chance, better to slow
29774ae72a1eSTejun Heo 			 * down than lose it.
29784ae72a1eSTejun Heo 			 */
2979a07d499bSTejun Heo 			sata_down_spd_limit(&ap->link, 0);
29804ae72a1eSTejun Heo 			ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
29814ae72a1eSTejun Heo 		}
2982c6fd2807SJeff Garzik 	}
2983c6fd2807SJeff Garzik 
29844ae72a1eSTejun Heo 	if (!tries[dev->devno])
2985c6fd2807SJeff Garzik 		ata_dev_disable(dev);
2986c6fd2807SJeff Garzik 
2987c6fd2807SJeff Garzik 	goto retry;
2988c6fd2807SJeff Garzik }
2989c6fd2807SJeff Garzik 
2990c6fd2807SJeff Garzik /**
2991c6fd2807SJeff Garzik  *	sata_print_link_status - Print SATA link status
2992936fd732STejun Heo  *	@link: SATA link to printk link status about
2993c6fd2807SJeff Garzik  *
2994c6fd2807SJeff Garzik  *	This function prints link speed and status of a SATA link.
2995c6fd2807SJeff Garzik  *
2996c6fd2807SJeff Garzik  *	LOCKING:
2997c6fd2807SJeff Garzik  *	None.
2998c6fd2807SJeff Garzik  */
29996bdb4fc9SAdrian Bunk static void sata_print_link_status(struct ata_link *link)
3000c6fd2807SJeff Garzik {
3001c6fd2807SJeff Garzik 	u32 sstatus, scontrol, tmp;
3002c6fd2807SJeff Garzik 
3003936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus))
3004c6fd2807SJeff Garzik 		return;
3005936fd732STejun Heo 	sata_scr_read(link, SCR_CONTROL, &scontrol);
3006c6fd2807SJeff Garzik 
3007b1c72916STejun Heo 	if (ata_phys_link_online(link)) {
3008c6fd2807SJeff Garzik 		tmp = (sstatus >> 4) & 0xf;
3009a9a79dfeSJoe Perches 		ata_link_info(link, "SATA link up %s (SStatus %X SControl %X)\n",
3010c6fd2807SJeff Garzik 			      sata_spd_string(tmp), sstatus, scontrol);
3011c6fd2807SJeff Garzik 	} else {
3012a9a79dfeSJoe Perches 		ata_link_info(link, "SATA link down (SStatus %X SControl %X)\n",
3013c6fd2807SJeff Garzik 			      sstatus, scontrol);
3014c6fd2807SJeff Garzik 	}
3015c6fd2807SJeff Garzik }
3016c6fd2807SJeff Garzik 
3017c6fd2807SJeff Garzik /**
3018c6fd2807SJeff Garzik  *	ata_dev_pair		-	return other device on cable
3019c6fd2807SJeff Garzik  *	@adev: device
3020c6fd2807SJeff Garzik  *
3021c6fd2807SJeff Garzik  *	Obtain the other device on the same cable, or if none is
3022c6fd2807SJeff Garzik  *	present NULL is returned
3023c6fd2807SJeff Garzik  */
3024c6fd2807SJeff Garzik 
3025c6fd2807SJeff Garzik struct ata_device *ata_dev_pair(struct ata_device *adev)
3026c6fd2807SJeff Garzik {
30279af5c9c9STejun Heo 	struct ata_link *link = adev->link;
30289af5c9c9STejun Heo 	struct ata_device *pair = &link->device[1 - adev->devno];
3029c6fd2807SJeff Garzik 	if (!ata_dev_enabled(pair))
3030c6fd2807SJeff Garzik 		return NULL;
3031c6fd2807SJeff Garzik 	return pair;
3032c6fd2807SJeff Garzik }
3033c6fd2807SJeff Garzik 
3034c6fd2807SJeff Garzik /**
3035c6fd2807SJeff Garzik  *	sata_down_spd_limit - adjust SATA spd limit downward
3036936fd732STejun Heo  *	@link: Link to adjust SATA spd limit for
3037a07d499bSTejun Heo  *	@spd_limit: Additional limit
3038c6fd2807SJeff Garzik  *
3039936fd732STejun Heo  *	Adjust SATA spd limit of @link downward.  Note that this
3040c6fd2807SJeff Garzik  *	function only adjusts the limit.  The change must be applied
3041c6fd2807SJeff Garzik  *	using sata_set_spd().
3042c6fd2807SJeff Garzik  *
3043a07d499bSTejun Heo  *	If @spd_limit is non-zero, the speed is limited to equal to or
3044a07d499bSTejun Heo  *	lower than @spd_limit if such speed is supported.  If
3045a07d499bSTejun Heo  *	@spd_limit is slower than any supported speed, only the lowest
3046a07d499bSTejun Heo  *	supported speed is allowed.
3047a07d499bSTejun Heo  *
3048c6fd2807SJeff Garzik  *	LOCKING:
3049c6fd2807SJeff Garzik  *	Inherited from caller.
3050c6fd2807SJeff Garzik  *
3051c6fd2807SJeff Garzik  *	RETURNS:
3052c6fd2807SJeff Garzik  *	0 on success, negative errno on failure
3053c6fd2807SJeff Garzik  */
3054a07d499bSTejun Heo int sata_down_spd_limit(struct ata_link *link, u32 spd_limit)
3055c6fd2807SJeff Garzik {
3056c6fd2807SJeff Garzik 	u32 sstatus, spd, mask;
3057a07d499bSTejun Heo 	int rc, bit;
3058c6fd2807SJeff Garzik 
3059936fd732STejun Heo 	if (!sata_scr_valid(link))
3060008a7896STejun Heo 		return -EOPNOTSUPP;
3061008a7896STejun Heo 
3062008a7896STejun Heo 	/* If SCR can be read, use it to determine the current SPD.
3063936fd732STejun Heo 	 * If not, use cached value in link->sata_spd.
3064008a7896STejun Heo 	 */
3065936fd732STejun Heo 	rc = sata_scr_read(link, SCR_STATUS, &sstatus);
30669913ff8aSTejun Heo 	if (rc == 0 && ata_sstatus_online(sstatus))
3067008a7896STejun Heo 		spd = (sstatus >> 4) & 0xf;
3068008a7896STejun Heo 	else
3069936fd732STejun Heo 		spd = link->sata_spd;
3070c6fd2807SJeff Garzik 
3071936fd732STejun Heo 	mask = link->sata_spd_limit;
3072c6fd2807SJeff Garzik 	if (mask <= 1)
3073c6fd2807SJeff Garzik 		return -EINVAL;
3074008a7896STejun Heo 
3075008a7896STejun Heo 	/* unconditionally mask off the highest bit */
3076a07d499bSTejun Heo 	bit = fls(mask) - 1;
3077a07d499bSTejun Heo 	mask &= ~(1 << bit);
3078c6fd2807SJeff Garzik 
30792dc0b46bSDavid Milburn 	/*
30802dc0b46bSDavid Milburn 	 * Mask off all speeds higher than or equal to the current one.  At
30812dc0b46bSDavid Milburn 	 * this point, if current SPD is not available and we previously
30822dc0b46bSDavid Milburn 	 * recorded the link speed from SStatus, the driver has already
30832dc0b46bSDavid Milburn 	 * masked off the highest bit so mask should already be 1 or 0.
30842dc0b46bSDavid Milburn 	 * Otherwise, we should not force 1.5Gbps on a link where we have
30852dc0b46bSDavid Milburn 	 * not previously recorded speed from SStatus.  Just return in this
30862dc0b46bSDavid Milburn 	 * case.
3087008a7896STejun Heo 	 */
3088008a7896STejun Heo 	if (spd > 1)
3089008a7896STejun Heo 		mask &= (1 << (spd - 1)) - 1;
3090008a7896STejun Heo 	else
30912dc0b46bSDavid Milburn 		return -EINVAL;
3092008a7896STejun Heo 
3093008a7896STejun Heo 	/* were we already at the bottom? */
3094c6fd2807SJeff Garzik 	if (!mask)
3095c6fd2807SJeff Garzik 		return -EINVAL;
3096c6fd2807SJeff Garzik 
3097a07d499bSTejun Heo 	if (spd_limit) {
3098a07d499bSTejun Heo 		if (mask & ((1 << spd_limit) - 1))
3099a07d499bSTejun Heo 			mask &= (1 << spd_limit) - 1;
3100a07d499bSTejun Heo 		else {
3101a07d499bSTejun Heo 			bit = ffs(mask) - 1;
3102a07d499bSTejun Heo 			mask = 1 << bit;
3103a07d499bSTejun Heo 		}
3104a07d499bSTejun Heo 	}
3105a07d499bSTejun Heo 
3106936fd732STejun Heo 	link->sata_spd_limit = mask;
3107c6fd2807SJeff Garzik 
3108a9a79dfeSJoe Perches 	ata_link_warn(link, "limiting SATA link speed to %s\n",
3109c6fd2807SJeff Garzik 		      sata_spd_string(fls(mask)));
3110c6fd2807SJeff Garzik 
3111c6fd2807SJeff Garzik 	return 0;
3112c6fd2807SJeff Garzik }
3113c6fd2807SJeff Garzik 
3114936fd732STejun Heo static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
3115c6fd2807SJeff Garzik {
31165270222fSTejun Heo 	struct ata_link *host_link = &link->ap->link;
31175270222fSTejun Heo 	u32 limit, target, spd;
3118c6fd2807SJeff Garzik 
31195270222fSTejun Heo 	limit = link->sata_spd_limit;
31205270222fSTejun Heo 
31215270222fSTejun Heo 	/* Don't configure downstream link faster than upstream link.
31225270222fSTejun Heo 	 * It doesn't speed up anything and some PMPs choke on such
31235270222fSTejun Heo 	 * configuration.
31245270222fSTejun Heo 	 */
31255270222fSTejun Heo 	if (!ata_is_host_link(link) && host_link->sata_spd)
31265270222fSTejun Heo 		limit &= (1 << host_link->sata_spd) - 1;
31275270222fSTejun Heo 
31285270222fSTejun Heo 	if (limit == UINT_MAX)
31295270222fSTejun Heo 		target = 0;
3130c6fd2807SJeff Garzik 	else
31315270222fSTejun Heo 		target = fls(limit);
3132c6fd2807SJeff Garzik 
3133c6fd2807SJeff Garzik 	spd = (*scontrol >> 4) & 0xf;
31345270222fSTejun Heo 	*scontrol = (*scontrol & ~0xf0) | ((target & 0xf) << 4);
3135c6fd2807SJeff Garzik 
31365270222fSTejun Heo 	return spd != target;
3137c6fd2807SJeff Garzik }
3138c6fd2807SJeff Garzik 
3139c6fd2807SJeff Garzik /**
3140c6fd2807SJeff Garzik  *	sata_set_spd_needed - is SATA spd configuration needed
3141936fd732STejun Heo  *	@link: Link in question
3142c6fd2807SJeff Garzik  *
3143c6fd2807SJeff Garzik  *	Test whether the spd limit in SControl matches
3144936fd732STejun Heo  *	@link->sata_spd_limit.  This function is used to determine
3145c6fd2807SJeff Garzik  *	whether hardreset is necessary to apply SATA spd
3146c6fd2807SJeff Garzik  *	configuration.
3147c6fd2807SJeff Garzik  *
3148c6fd2807SJeff Garzik  *	LOCKING:
3149c6fd2807SJeff Garzik  *	Inherited from caller.
3150c6fd2807SJeff Garzik  *
3151c6fd2807SJeff Garzik  *	RETURNS:
3152c6fd2807SJeff Garzik  *	1 if SATA spd configuration is needed, 0 otherwise.
3153c6fd2807SJeff Garzik  */
31541dc55e87SAdrian Bunk static int sata_set_spd_needed(struct ata_link *link)
3155c6fd2807SJeff Garzik {
3156c6fd2807SJeff Garzik 	u32 scontrol;
3157c6fd2807SJeff Garzik 
3158936fd732STejun Heo 	if (sata_scr_read(link, SCR_CONTROL, &scontrol))
3159db64bcf3STejun Heo 		return 1;
3160c6fd2807SJeff Garzik 
3161936fd732STejun Heo 	return __sata_set_spd_needed(link, &scontrol);
3162c6fd2807SJeff Garzik }
3163c6fd2807SJeff Garzik 
3164c6fd2807SJeff Garzik /**
3165c6fd2807SJeff Garzik  *	sata_set_spd - set SATA spd according to spd limit
3166936fd732STejun Heo  *	@link: Link to set SATA spd for
3167c6fd2807SJeff Garzik  *
3168936fd732STejun Heo  *	Set SATA spd of @link according to sata_spd_limit.
3169c6fd2807SJeff Garzik  *
3170c6fd2807SJeff Garzik  *	LOCKING:
3171c6fd2807SJeff Garzik  *	Inherited from caller.
3172c6fd2807SJeff Garzik  *
3173c6fd2807SJeff Garzik  *	RETURNS:
3174c6fd2807SJeff Garzik  *	0 if spd doesn't need to be changed, 1 if spd has been
3175c6fd2807SJeff Garzik  *	changed.  Negative errno if SCR registers are inaccessible.
3176c6fd2807SJeff Garzik  */
3177936fd732STejun Heo int sata_set_spd(struct ata_link *link)
3178c6fd2807SJeff Garzik {
3179c6fd2807SJeff Garzik 	u32 scontrol;
3180c6fd2807SJeff Garzik 	int rc;
3181c6fd2807SJeff Garzik 
3182936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3183c6fd2807SJeff Garzik 		return rc;
3184c6fd2807SJeff Garzik 
3185936fd732STejun Heo 	if (!__sata_set_spd_needed(link, &scontrol))
3186c6fd2807SJeff Garzik 		return 0;
3187c6fd2807SJeff Garzik 
3188936fd732STejun Heo 	if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3189c6fd2807SJeff Garzik 		return rc;
3190c6fd2807SJeff Garzik 
3191c6fd2807SJeff Garzik 	return 1;
3192c6fd2807SJeff Garzik }
3193c6fd2807SJeff Garzik 
3194c6fd2807SJeff Garzik /*
3195c6fd2807SJeff Garzik  * This mode timing computation functionality is ported over from
3196c6fd2807SJeff Garzik  * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
3197c6fd2807SJeff Garzik  */
3198c6fd2807SJeff Garzik /*
3199b352e57dSAlan Cox  * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
3200c6fd2807SJeff Garzik  * These were taken from ATA/ATAPI-6 standard, rev 0a, except
3201b352e57dSAlan Cox  * for UDMA6, which is currently supported only by Maxtor drives.
3202b352e57dSAlan Cox  *
3203b352e57dSAlan Cox  * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0.
3204c6fd2807SJeff Garzik  */
3205c6fd2807SJeff Garzik 
3206c6fd2807SJeff Garzik static const struct ata_timing ata_timing[] = {
32073ada9c12SDavid Daney /*	{ XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 0,  960,   0 }, */
32083ada9c12SDavid Daney 	{ XFER_PIO_0,     70, 290, 240, 600, 165, 150, 0,  600,   0 },
32093ada9c12SDavid Daney 	{ XFER_PIO_1,     50, 290,  93, 383, 125, 100, 0,  383,   0 },
32103ada9c12SDavid Daney 	{ XFER_PIO_2,     30, 290,  40, 330, 100,  90, 0,  240,   0 },
32113ada9c12SDavid Daney 	{ XFER_PIO_3,     30,  80,  70, 180,  80,  70, 0,  180,   0 },
32123ada9c12SDavid Daney 	{ XFER_PIO_4,     25,  70,  25, 120,  70,  25, 0,  120,   0 },
32133ada9c12SDavid Daney 	{ XFER_PIO_5,     15,  65,  25, 100,  65,  25, 0,  100,   0 },
32143ada9c12SDavid Daney 	{ XFER_PIO_6,     10,  55,  20,  80,  55,  20, 0,   80,   0 },
3215c6fd2807SJeff Garzik 
32163ada9c12SDavid Daney 	{ XFER_SW_DMA_0, 120,   0,   0,   0, 480, 480, 50, 960,   0 },
32173ada9c12SDavid Daney 	{ XFER_SW_DMA_1,  90,   0,   0,   0, 240, 240, 30, 480,   0 },
32183ada9c12SDavid Daney 	{ XFER_SW_DMA_2,  60,   0,   0,   0, 120, 120, 20, 240,   0 },
3219c6fd2807SJeff Garzik 
32203ada9c12SDavid Daney 	{ XFER_MW_DMA_0,  60,   0,   0,   0, 215, 215, 20, 480,   0 },
32213ada9c12SDavid Daney 	{ XFER_MW_DMA_1,  45,   0,   0,   0,  80,  50, 5,  150,   0 },
32223ada9c12SDavid Daney 	{ XFER_MW_DMA_2,  25,   0,   0,   0,  70,  25, 5,  120,   0 },
32233ada9c12SDavid Daney 	{ XFER_MW_DMA_3,  25,   0,   0,   0,  65,  25, 5,  100,   0 },
32243ada9c12SDavid Daney 	{ XFER_MW_DMA_4,  25,   0,   0,   0,  55,  20, 5,   80,   0 },
3225c6fd2807SJeff Garzik 
32263ada9c12SDavid Daney /*	{ XFER_UDMA_SLOW,  0,   0,   0,   0,   0,   0, 0,    0, 150 }, */
32273ada9c12SDavid Daney 	{ XFER_UDMA_0,     0,   0,   0,   0,   0,   0, 0,    0, 120 },
32283ada9c12SDavid Daney 	{ XFER_UDMA_1,     0,   0,   0,   0,   0,   0, 0,    0,  80 },
32293ada9c12SDavid Daney 	{ XFER_UDMA_2,     0,   0,   0,   0,   0,   0, 0,    0,  60 },
32303ada9c12SDavid Daney 	{ XFER_UDMA_3,     0,   0,   0,   0,   0,   0, 0,    0,  45 },
32313ada9c12SDavid Daney 	{ XFER_UDMA_4,     0,   0,   0,   0,   0,   0, 0,    0,  30 },
32323ada9c12SDavid Daney 	{ XFER_UDMA_5,     0,   0,   0,   0,   0,   0, 0,    0,  20 },
32333ada9c12SDavid Daney 	{ XFER_UDMA_6,     0,   0,   0,   0,   0,   0, 0,    0,  15 },
3234c6fd2807SJeff Garzik 
3235c6fd2807SJeff Garzik 	{ 0xFF }
3236c6fd2807SJeff Garzik };
3237c6fd2807SJeff Garzik 
3238c6fd2807SJeff Garzik #define ENOUGH(v, unit)		(((v)-1)/(unit)+1)
323923e4c67aSArnd Bergmann #define EZ(v, unit)		((v)?ENOUGH(((v) * 1000), unit):0)
3240c6fd2807SJeff Garzik 
3241c6fd2807SJeff Garzik static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
3242c6fd2807SJeff Garzik {
324323e4c67aSArnd Bergmann 	q->setup	= EZ(t->setup,       T);
324423e4c67aSArnd Bergmann 	q->act8b	= EZ(t->act8b,       T);
324523e4c67aSArnd Bergmann 	q->rec8b	= EZ(t->rec8b,       T);
324623e4c67aSArnd Bergmann 	q->cyc8b	= EZ(t->cyc8b,       T);
324723e4c67aSArnd Bergmann 	q->active	= EZ(t->active,      T);
324823e4c67aSArnd Bergmann 	q->recover	= EZ(t->recover,     T);
324923e4c67aSArnd Bergmann 	q->dmack_hold	= EZ(t->dmack_hold,  T);
325023e4c67aSArnd Bergmann 	q->cycle	= EZ(t->cycle,       T);
325123e4c67aSArnd Bergmann 	q->udma		= EZ(t->udma,       UT);
3252c6fd2807SJeff Garzik }
3253c6fd2807SJeff Garzik 
3254c6fd2807SJeff Garzik void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
3255c6fd2807SJeff Garzik 		      struct ata_timing *m, unsigned int what)
3256c6fd2807SJeff Garzik {
3257c6fd2807SJeff Garzik 	if (what & ATA_TIMING_SETUP  ) m->setup   = max(a->setup,   b->setup);
3258c6fd2807SJeff Garzik 	if (what & ATA_TIMING_ACT8B  ) m->act8b   = max(a->act8b,   b->act8b);
3259c6fd2807SJeff Garzik 	if (what & ATA_TIMING_REC8B  ) m->rec8b   = max(a->rec8b,   b->rec8b);
3260c6fd2807SJeff Garzik 	if (what & ATA_TIMING_CYC8B  ) m->cyc8b   = max(a->cyc8b,   b->cyc8b);
3261c6fd2807SJeff Garzik 	if (what & ATA_TIMING_ACTIVE ) m->active  = max(a->active,  b->active);
3262c6fd2807SJeff Garzik 	if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
32633ada9c12SDavid Daney 	if (what & ATA_TIMING_DMACK_HOLD) m->dmack_hold = max(a->dmack_hold, b->dmack_hold);
3264c6fd2807SJeff Garzik 	if (what & ATA_TIMING_CYCLE  ) m->cycle   = max(a->cycle,   b->cycle);
3265c6fd2807SJeff Garzik 	if (what & ATA_TIMING_UDMA   ) m->udma    = max(a->udma,    b->udma);
3266c6fd2807SJeff Garzik }
3267c6fd2807SJeff Garzik 
32686357357cSTejun Heo const struct ata_timing *ata_timing_find_mode(u8 xfer_mode)
3269c6fd2807SJeff Garzik {
327070cd071eSTejun Heo 	const struct ata_timing *t = ata_timing;
3271c6fd2807SJeff Garzik 
327270cd071eSTejun Heo 	while (xfer_mode > t->mode)
327370cd071eSTejun Heo 		t++;
327470cd071eSTejun Heo 
327570cd071eSTejun Heo 	if (xfer_mode == t->mode)
3276c6fd2807SJeff Garzik 		return t;
3277cd705d5aSBorislav Petkov 
3278cd705d5aSBorislav Petkov 	WARN_ONCE(true, "%s: unable to find timing for xfer_mode 0x%x\n",
3279cd705d5aSBorislav Petkov 			__func__, xfer_mode);
3280cd705d5aSBorislav Petkov 
328170cd071eSTejun Heo 	return NULL;
3282c6fd2807SJeff Garzik }
3283c6fd2807SJeff Garzik 
3284c6fd2807SJeff Garzik int ata_timing_compute(struct ata_device *adev, unsigned short speed,
3285c6fd2807SJeff Garzik 		       struct ata_timing *t, int T, int UT)
3286c6fd2807SJeff Garzik {
32879e8808a9SBartlomiej Zolnierkiewicz 	const u16 *id = adev->id;
3288c6fd2807SJeff Garzik 	const struct ata_timing *s;
3289c6fd2807SJeff Garzik 	struct ata_timing p;
3290c6fd2807SJeff Garzik 
3291c6fd2807SJeff Garzik 	/*
3292c6fd2807SJeff Garzik 	 * Find the mode.
3293c6fd2807SJeff Garzik 	 */
3294c6fd2807SJeff Garzik 
3295c6fd2807SJeff Garzik 	if (!(s = ata_timing_find_mode(speed)))
3296c6fd2807SJeff Garzik 		return -EINVAL;
3297c6fd2807SJeff Garzik 
3298c6fd2807SJeff Garzik 	memcpy(t, s, sizeof(*s));
3299c6fd2807SJeff Garzik 
3300c6fd2807SJeff Garzik 	/*
3301c6fd2807SJeff Garzik 	 * If the drive is an EIDE drive, it can tell us it needs extended
3302c6fd2807SJeff Garzik 	 * PIO/MW_DMA cycle timing.
3303c6fd2807SJeff Garzik 	 */
3304c6fd2807SJeff Garzik 
33059e8808a9SBartlomiej Zolnierkiewicz 	if (id[ATA_ID_FIELD_VALID] & 2) {	/* EIDE drive */
3306c6fd2807SJeff Garzik 		memset(&p, 0, sizeof(p));
33079e8808a9SBartlomiej Zolnierkiewicz 
3308bff00256SBartlomiej Zolnierkiewicz 		if (speed >= XFER_PIO_0 && speed < XFER_SW_DMA_0) {
33099e8808a9SBartlomiej Zolnierkiewicz 			if (speed <= XFER_PIO_2)
33109e8808a9SBartlomiej Zolnierkiewicz 				p.cycle = p.cyc8b = id[ATA_ID_EIDE_PIO];
33119e8808a9SBartlomiej Zolnierkiewicz 			else if ((speed <= XFER_PIO_4) ||
33129e8808a9SBartlomiej Zolnierkiewicz 				 (speed == XFER_PIO_5 && !ata_id_is_cfa(id)))
33139e8808a9SBartlomiej Zolnierkiewicz 				p.cycle = p.cyc8b = id[ATA_ID_EIDE_PIO_IORDY];
33149e8808a9SBartlomiej Zolnierkiewicz 		} else if (speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2)
33159e8808a9SBartlomiej Zolnierkiewicz 			p.cycle = id[ATA_ID_EIDE_DMA_MIN];
33169e8808a9SBartlomiej Zolnierkiewicz 
3317c6fd2807SJeff Garzik 		ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
3318c6fd2807SJeff Garzik 	}
3319c6fd2807SJeff Garzik 
3320c6fd2807SJeff Garzik 	/*
3321c6fd2807SJeff Garzik 	 * Convert the timing to bus clock counts.
3322c6fd2807SJeff Garzik 	 */
3323c6fd2807SJeff Garzik 
3324c6fd2807SJeff Garzik 	ata_timing_quantize(t, t, T, UT);
3325c6fd2807SJeff Garzik 
3326c6fd2807SJeff Garzik 	/*
3327c6fd2807SJeff Garzik 	 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
3328c6fd2807SJeff Garzik 	 * S.M.A.R.T * and some other commands. We have to ensure that the
3329c6fd2807SJeff Garzik 	 * DMA cycle timing is slower/equal than the fastest PIO timing.
3330c6fd2807SJeff Garzik 	 */
3331c6fd2807SJeff Garzik 
3332fd3367afSAlan 	if (speed > XFER_PIO_6) {
3333c6fd2807SJeff Garzik 		ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
3334c6fd2807SJeff Garzik 		ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
3335c6fd2807SJeff Garzik 	}
3336c6fd2807SJeff Garzik 
3337c6fd2807SJeff Garzik 	/*
3338c6fd2807SJeff Garzik 	 * Lengthen active & recovery time so that cycle time is correct.
3339c6fd2807SJeff Garzik 	 */
3340c6fd2807SJeff Garzik 
3341c6fd2807SJeff Garzik 	if (t->act8b + t->rec8b < t->cyc8b) {
3342c6fd2807SJeff Garzik 		t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
3343c6fd2807SJeff Garzik 		t->rec8b = t->cyc8b - t->act8b;
3344c6fd2807SJeff Garzik 	}
3345c6fd2807SJeff Garzik 
3346c6fd2807SJeff Garzik 	if (t->active + t->recover < t->cycle) {
3347c6fd2807SJeff Garzik 		t->active += (t->cycle - (t->active + t->recover)) / 2;
3348c6fd2807SJeff Garzik 		t->recover = t->cycle - t->active;
3349c6fd2807SJeff Garzik 	}
33504f701d1eSAlan Cox 
33514f701d1eSAlan Cox 	/* In a few cases quantisation may produce enough errors to
33524f701d1eSAlan Cox 	   leave t->cycle too low for the sum of active and recovery
33534f701d1eSAlan Cox 	   if so we must correct this */
33544f701d1eSAlan Cox 	if (t->active + t->recover > t->cycle)
33554f701d1eSAlan Cox 		t->cycle = t->active + t->recover;
3356c6fd2807SJeff Garzik 
3357c6fd2807SJeff Garzik 	return 0;
3358c6fd2807SJeff Garzik }
3359c6fd2807SJeff Garzik 
3360c6fd2807SJeff Garzik /**
3361a0f79b92STejun Heo  *	ata_timing_cycle2mode - find xfer mode for the specified cycle duration
3362a0f79b92STejun Heo  *	@xfer_shift: ATA_SHIFT_* value for transfer type to examine.
3363a0f79b92STejun Heo  *	@cycle: cycle duration in ns
3364a0f79b92STejun Heo  *
3365a0f79b92STejun Heo  *	Return matching xfer mode for @cycle.  The returned mode is of
3366a0f79b92STejun Heo  *	the transfer type specified by @xfer_shift.  If @cycle is too
3367a0f79b92STejun Heo  *	slow for @xfer_shift, 0xff is returned.  If @cycle is faster
3368a0f79b92STejun Heo  *	than the fastest known mode, the fasted mode is returned.
3369a0f79b92STejun Heo  *
3370a0f79b92STejun Heo  *	LOCKING:
3371a0f79b92STejun Heo  *	None.
3372a0f79b92STejun Heo  *
3373a0f79b92STejun Heo  *	RETURNS:
3374a0f79b92STejun Heo  *	Matching xfer_mode, 0xff if no match found.
3375a0f79b92STejun Heo  */
3376a0f79b92STejun Heo u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
3377a0f79b92STejun Heo {
3378a0f79b92STejun Heo 	u8 base_mode = 0xff, last_mode = 0xff;
3379a0f79b92STejun Heo 	const struct ata_xfer_ent *ent;
3380a0f79b92STejun Heo 	const struct ata_timing *t;
3381a0f79b92STejun Heo 
3382a0f79b92STejun Heo 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
3383a0f79b92STejun Heo 		if (ent->shift == xfer_shift)
3384a0f79b92STejun Heo 			base_mode = ent->base;
3385a0f79b92STejun Heo 
3386a0f79b92STejun Heo 	for (t = ata_timing_find_mode(base_mode);
3387a0f79b92STejun Heo 	     t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
3388a0f79b92STejun Heo 		unsigned short this_cycle;
3389a0f79b92STejun Heo 
3390a0f79b92STejun Heo 		switch (xfer_shift) {
3391a0f79b92STejun Heo 		case ATA_SHIFT_PIO:
3392a0f79b92STejun Heo 		case ATA_SHIFT_MWDMA:
3393a0f79b92STejun Heo 			this_cycle = t->cycle;
3394a0f79b92STejun Heo 			break;
3395a0f79b92STejun Heo 		case ATA_SHIFT_UDMA:
3396a0f79b92STejun Heo 			this_cycle = t->udma;
3397a0f79b92STejun Heo 			break;
3398a0f79b92STejun Heo 		default:
3399a0f79b92STejun Heo 			return 0xff;
3400a0f79b92STejun Heo 		}
3401a0f79b92STejun Heo 
3402a0f79b92STejun Heo 		if (cycle > this_cycle)
3403a0f79b92STejun Heo 			break;
3404a0f79b92STejun Heo 
3405a0f79b92STejun Heo 		last_mode = t->mode;
3406a0f79b92STejun Heo 	}
3407a0f79b92STejun Heo 
3408a0f79b92STejun Heo 	return last_mode;
3409a0f79b92STejun Heo }
3410a0f79b92STejun Heo 
3411a0f79b92STejun Heo /**
3412c6fd2807SJeff Garzik  *	ata_down_xfermask_limit - adjust dev xfer masks downward
3413c6fd2807SJeff Garzik  *	@dev: Device to adjust xfer masks
3414458337dbSTejun Heo  *	@sel: ATA_DNXFER_* selector
3415c6fd2807SJeff Garzik  *
3416c6fd2807SJeff Garzik  *	Adjust xfer masks of @dev downward.  Note that this function
3417c6fd2807SJeff Garzik  *	does not apply the change.  Invoking ata_set_mode() afterwards
3418c6fd2807SJeff Garzik  *	will apply the limit.
3419c6fd2807SJeff Garzik  *
3420c6fd2807SJeff Garzik  *	LOCKING:
3421c6fd2807SJeff Garzik  *	Inherited from caller.
3422c6fd2807SJeff Garzik  *
3423c6fd2807SJeff Garzik  *	RETURNS:
3424c6fd2807SJeff Garzik  *	0 on success, negative errno on failure
3425c6fd2807SJeff Garzik  */
3426458337dbSTejun Heo int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
3427c6fd2807SJeff Garzik {
3428458337dbSTejun Heo 	char buf[32];
34297dc951aeSTejun Heo 	unsigned long orig_mask, xfer_mask;
34307dc951aeSTejun Heo 	unsigned long pio_mask, mwdma_mask, udma_mask;
3431458337dbSTejun Heo 	int quiet, highbit;
3432c6fd2807SJeff Garzik 
3433458337dbSTejun Heo 	quiet = !!(sel & ATA_DNXFER_QUIET);
3434458337dbSTejun Heo 	sel &= ~ATA_DNXFER_QUIET;
3435458337dbSTejun Heo 
3436458337dbSTejun Heo 	xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
3437458337dbSTejun Heo 						  dev->mwdma_mask,
3438c6fd2807SJeff Garzik 						  dev->udma_mask);
3439458337dbSTejun Heo 	ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
3440c6fd2807SJeff Garzik 
3441458337dbSTejun Heo 	switch (sel) {
3442458337dbSTejun Heo 	case ATA_DNXFER_PIO:
3443458337dbSTejun Heo 		highbit = fls(pio_mask) - 1;
3444458337dbSTejun Heo 		pio_mask &= ~(1 << highbit);
3445458337dbSTejun Heo 		break;
3446458337dbSTejun Heo 
3447458337dbSTejun Heo 	case ATA_DNXFER_DMA:
3448458337dbSTejun Heo 		if (udma_mask) {
3449458337dbSTejun Heo 			highbit = fls(udma_mask) - 1;
3450458337dbSTejun Heo 			udma_mask &= ~(1 << highbit);
3451458337dbSTejun Heo 			if (!udma_mask)
3452458337dbSTejun Heo 				return -ENOENT;
3453458337dbSTejun Heo 		} else if (mwdma_mask) {
3454458337dbSTejun Heo 			highbit = fls(mwdma_mask) - 1;
3455458337dbSTejun Heo 			mwdma_mask &= ~(1 << highbit);
3456458337dbSTejun Heo 			if (!mwdma_mask)
3457458337dbSTejun Heo 				return -ENOENT;
3458458337dbSTejun Heo 		}
3459458337dbSTejun Heo 		break;
3460458337dbSTejun Heo 
3461458337dbSTejun Heo 	case ATA_DNXFER_40C:
3462458337dbSTejun Heo 		udma_mask &= ATA_UDMA_MASK_40C;
3463458337dbSTejun Heo 		break;
3464458337dbSTejun Heo 
3465458337dbSTejun Heo 	case ATA_DNXFER_FORCE_PIO0:
3466458337dbSTejun Heo 		pio_mask &= 1;
346705b83605SGustavo A. R. Silva 		/* fall through */
3468458337dbSTejun Heo 	case ATA_DNXFER_FORCE_PIO:
3469458337dbSTejun Heo 		mwdma_mask = 0;
3470458337dbSTejun Heo 		udma_mask = 0;
3471458337dbSTejun Heo 		break;
3472458337dbSTejun Heo 
3473458337dbSTejun Heo 	default:
3474458337dbSTejun Heo 		BUG();
3475458337dbSTejun Heo 	}
3476458337dbSTejun Heo 
3477458337dbSTejun Heo 	xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
3478458337dbSTejun Heo 
3479458337dbSTejun Heo 	if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
3480458337dbSTejun Heo 		return -ENOENT;
3481458337dbSTejun Heo 
3482458337dbSTejun Heo 	if (!quiet) {
3483458337dbSTejun Heo 		if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
3484458337dbSTejun Heo 			snprintf(buf, sizeof(buf), "%s:%s",
3485458337dbSTejun Heo 				 ata_mode_string(xfer_mask),
3486458337dbSTejun Heo 				 ata_mode_string(xfer_mask & ATA_MASK_PIO));
3487458337dbSTejun Heo 		else
3488458337dbSTejun Heo 			snprintf(buf, sizeof(buf), "%s",
3489458337dbSTejun Heo 				 ata_mode_string(xfer_mask));
3490458337dbSTejun Heo 
3491a9a79dfeSJoe Perches 		ata_dev_warn(dev, "limiting speed to %s\n", buf);
3492458337dbSTejun Heo 	}
3493c6fd2807SJeff Garzik 
3494c6fd2807SJeff Garzik 	ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
3495c6fd2807SJeff Garzik 			    &dev->udma_mask);
3496c6fd2807SJeff Garzik 
3497c6fd2807SJeff Garzik 	return 0;
3498c6fd2807SJeff Garzik }
3499c6fd2807SJeff Garzik 
3500c6fd2807SJeff Garzik static int ata_dev_set_mode(struct ata_device *dev)
3501c6fd2807SJeff Garzik {
3502d0cb43b3STejun Heo 	struct ata_port *ap = dev->link->ap;
35039af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
3504d0cb43b3STejun Heo 	const bool nosetxfer = dev->horkage & ATA_HORKAGE_NOSETXFER;
35054055dee7STejun Heo 	const char *dev_err_whine = "";
35064055dee7STejun Heo 	int ign_dev_err = 0;
3507d0cb43b3STejun Heo 	unsigned int err_mask = 0;
3508c6fd2807SJeff Garzik 	int rc;
3509c6fd2807SJeff Garzik 
3510c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_PIO;
3511c6fd2807SJeff Garzik 	if (dev->xfer_shift == ATA_SHIFT_PIO)
3512c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_PIO;
3513c6fd2807SJeff Garzik 
3514d0cb43b3STejun Heo 	if (nosetxfer && ap->flags & ATA_FLAG_SATA && ata_id_is_sata(dev->id))
3515d0cb43b3STejun Heo 		dev_err_whine = " (SET_XFERMODE skipped)";
3516d0cb43b3STejun Heo 	else {
3517d0cb43b3STejun Heo 		if (nosetxfer)
3518a9a79dfeSJoe Perches 			ata_dev_warn(dev,
3519d0cb43b3STejun Heo 				     "NOSETXFER but PATA detected - can't "
3520d0cb43b3STejun Heo 				     "skip SETXFER, might malfunction\n");
3521c6fd2807SJeff Garzik 		err_mask = ata_dev_set_xfermode(dev);
3522d0cb43b3STejun Heo 	}
35232dcb407eSJeff Garzik 
35244055dee7STejun Heo 	if (err_mask & ~AC_ERR_DEV)
35254055dee7STejun Heo 		goto fail;
35262dcb407eSJeff Garzik 
35274055dee7STejun Heo 	/* revalidate */
3528baa1e78aSTejun Heo 	ehc->i.flags |= ATA_EHI_POST_SETMODE;
3529422c9daaSTejun Heo 	rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
3530baa1e78aSTejun Heo 	ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
3531c6fd2807SJeff Garzik 	if (rc)
3532c6fd2807SJeff Garzik 		return rc;
3533c6fd2807SJeff Garzik 
3534b93fda12SAlan Cox 	if (dev->xfer_shift == ATA_SHIFT_PIO) {
35354055dee7STejun Heo 		/* Old CFA may refuse this command, which is just fine */
3536b93fda12SAlan Cox 		if (ata_id_is_cfa(dev->id))
35374055dee7STejun Heo 			ign_dev_err = 1;
3538b93fda12SAlan Cox 		/* Catch several broken garbage emulations plus some pre
3539b93fda12SAlan Cox 		   ATA devices */
3540b93fda12SAlan Cox 		if (ata_id_major_version(dev->id) == 0 &&
35414055dee7STejun Heo 					dev->pio_mode <= XFER_PIO_2)
35424055dee7STejun Heo 			ign_dev_err = 1;
3543b93fda12SAlan Cox 		/* Some very old devices and some bad newer ones fail
3544b93fda12SAlan Cox 		   any kind of SET_XFERMODE request but support PIO0-2
3545b93fda12SAlan Cox 		   timings and no IORDY */
3546b93fda12SAlan Cox 		if (!ata_id_has_iordy(dev->id) && dev->pio_mode <= XFER_PIO_2)
3547b93fda12SAlan Cox 			ign_dev_err = 1;
3548b93fda12SAlan Cox 	}
35494055dee7STejun Heo 	/* Early MWDMA devices do DMA but don't allow DMA mode setting.
35504055dee7STejun Heo 	   Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
35514055dee7STejun Heo 	if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
35524055dee7STejun Heo 	    dev->dma_mode == XFER_MW_DMA_0 &&
35534055dee7STejun Heo 	    (dev->id[63] >> 8) & 1)
35544055dee7STejun Heo 		ign_dev_err = 1;
35554055dee7STejun Heo 
35564055dee7STejun Heo 	/* if the device is actually configured correctly, ignore dev err */
35574055dee7STejun Heo 	if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id)))
35584055dee7STejun Heo 		ign_dev_err = 1;
35594055dee7STejun Heo 
35604055dee7STejun Heo 	if (err_mask & AC_ERR_DEV) {
35614055dee7STejun Heo 		if (!ign_dev_err)
35624055dee7STejun Heo 			goto fail;
35634055dee7STejun Heo 		else
35644055dee7STejun Heo 			dev_err_whine = " (device error ignored)";
35654055dee7STejun Heo 	}
35664055dee7STejun Heo 
3567c6fd2807SJeff Garzik 	DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
3568c6fd2807SJeff Garzik 		dev->xfer_shift, (int)dev->xfer_mode);
3569c6fd2807SJeff Garzik 
357007b9b6d6SDamien Le Moal 	if (!(ehc->i.flags & ATA_EHI_QUIET) ||
357107b9b6d6SDamien Le Moal 	    ehc->i.flags & ATA_EHI_DID_HARDRESET)
3572a9a79dfeSJoe Perches 		ata_dev_info(dev, "configured for %s%s\n",
35734055dee7STejun Heo 			     ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)),
35744055dee7STejun Heo 			     dev_err_whine);
35754055dee7STejun Heo 
3576c6fd2807SJeff Garzik 	return 0;
35774055dee7STejun Heo 
35784055dee7STejun Heo  fail:
3579a9a79dfeSJoe Perches 	ata_dev_err(dev, "failed to set xfermode (err_mask=0x%x)\n", err_mask);
35804055dee7STejun Heo 	return -EIO;
3581c6fd2807SJeff Garzik }
3582c6fd2807SJeff Garzik 
3583c6fd2807SJeff Garzik /**
358404351821SAlan  *	ata_do_set_mode - Program timings and issue SET FEATURES - XFER
35850260731fSTejun Heo  *	@link: link on which timings will be programmed
35861967b7ffSJoe Perches  *	@r_failed_dev: out parameter for failed device
3587c6fd2807SJeff Garzik  *
358804351821SAlan  *	Standard implementation of the function used to tune and set
358904351821SAlan  *	ATA device disk transfer mode (PIO3, UDMA6, etc.).  If
359004351821SAlan  *	ata_dev_set_mode() fails, pointer to the failing device is
3591c6fd2807SJeff Garzik  *	returned in @r_failed_dev.
3592c6fd2807SJeff Garzik  *
3593c6fd2807SJeff Garzik  *	LOCKING:
3594c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
3595c6fd2807SJeff Garzik  *
3596c6fd2807SJeff Garzik  *	RETURNS:
3597c6fd2807SJeff Garzik  *	0 on success, negative errno otherwise
3598c6fd2807SJeff Garzik  */
359904351821SAlan 
36000260731fSTejun Heo int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
3601c6fd2807SJeff Garzik {
36020260731fSTejun Heo 	struct ata_port *ap = link->ap;
3603c6fd2807SJeff Garzik 	struct ata_device *dev;
3604f58229f8STejun Heo 	int rc = 0, used_dma = 0, found = 0;
3605c6fd2807SJeff Garzik 
3606c6fd2807SJeff Garzik 	/* step 1: calculate xfer_mask */
36071eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
36087dc951aeSTejun Heo 		unsigned long pio_mask, dma_mask;
3609b3a70601SAlan Cox 		unsigned int mode_mask;
3610c6fd2807SJeff Garzik 
3611b3a70601SAlan Cox 		mode_mask = ATA_DMA_MASK_ATA;
3612b3a70601SAlan Cox 		if (dev->class == ATA_DEV_ATAPI)
3613b3a70601SAlan Cox 			mode_mask = ATA_DMA_MASK_ATAPI;
3614b3a70601SAlan Cox 		else if (ata_id_is_cfa(dev->id))
3615b3a70601SAlan Cox 			mode_mask = ATA_DMA_MASK_CFA;
3616b3a70601SAlan Cox 
3617c6fd2807SJeff Garzik 		ata_dev_xfermask(dev);
361833267325STejun Heo 		ata_force_xfermask(dev);
3619c6fd2807SJeff Garzik 
3620c6fd2807SJeff Garzik 		pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
3621b3a70601SAlan Cox 
3622b3a70601SAlan Cox 		if (libata_dma_mask & mode_mask)
362380a9c430SSergei Shtylyov 			dma_mask = ata_pack_xfermask(0, dev->mwdma_mask,
362480a9c430SSergei Shtylyov 						     dev->udma_mask);
3625b3a70601SAlan Cox 		else
3626b3a70601SAlan Cox 			dma_mask = 0;
3627b3a70601SAlan Cox 
3628c6fd2807SJeff Garzik 		dev->pio_mode = ata_xfer_mask2mode(pio_mask);
3629c6fd2807SJeff Garzik 		dev->dma_mode = ata_xfer_mask2mode(dma_mask);
3630c6fd2807SJeff Garzik 
3631c6fd2807SJeff Garzik 		found = 1;
3632b15b3ebaSAlan Cox 		if (ata_dma_enabled(dev))
3633c6fd2807SJeff Garzik 			used_dma = 1;
3634c6fd2807SJeff Garzik 	}
3635c6fd2807SJeff Garzik 	if (!found)
3636c6fd2807SJeff Garzik 		goto out;
3637c6fd2807SJeff Garzik 
3638c6fd2807SJeff Garzik 	/* step 2: always set host PIO timings */
36391eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
364070cd071eSTejun Heo 		if (dev->pio_mode == 0xff) {
3641a9a79dfeSJoe Perches 			ata_dev_warn(dev, "no PIO support\n");
3642c6fd2807SJeff Garzik 			rc = -EINVAL;
3643c6fd2807SJeff Garzik 			goto out;
3644c6fd2807SJeff Garzik 		}
3645c6fd2807SJeff Garzik 
3646c6fd2807SJeff Garzik 		dev->xfer_mode = dev->pio_mode;
3647c6fd2807SJeff Garzik 		dev->xfer_shift = ATA_SHIFT_PIO;
3648c6fd2807SJeff Garzik 		if (ap->ops->set_piomode)
3649c6fd2807SJeff Garzik 			ap->ops->set_piomode(ap, dev);
3650c6fd2807SJeff Garzik 	}
3651c6fd2807SJeff Garzik 
3652c6fd2807SJeff Garzik 	/* step 3: set host DMA timings */
36531eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
36541eca4365STejun Heo 		if (!ata_dma_enabled(dev))
3655c6fd2807SJeff Garzik 			continue;
3656c6fd2807SJeff Garzik 
3657c6fd2807SJeff Garzik 		dev->xfer_mode = dev->dma_mode;
3658c6fd2807SJeff Garzik 		dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
3659c6fd2807SJeff Garzik 		if (ap->ops->set_dmamode)
3660c6fd2807SJeff Garzik 			ap->ops->set_dmamode(ap, dev);
3661c6fd2807SJeff Garzik 	}
3662c6fd2807SJeff Garzik 
3663c6fd2807SJeff Garzik 	/* step 4: update devices' xfer mode */
36641eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
3665c6fd2807SJeff Garzik 		rc = ata_dev_set_mode(dev);
3666c6fd2807SJeff Garzik 		if (rc)
3667c6fd2807SJeff Garzik 			goto out;
3668c6fd2807SJeff Garzik 	}
3669c6fd2807SJeff Garzik 
3670c6fd2807SJeff Garzik 	/* Record simplex status. If we selected DMA then the other
3671c6fd2807SJeff Garzik 	 * host channels are not permitted to do so.
3672c6fd2807SJeff Garzik 	 */
3673cca3974eSJeff Garzik 	if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
3674032af1ceSAlan 		ap->host->simplex_claimed = ap;
3675c6fd2807SJeff Garzik 
3676c6fd2807SJeff Garzik  out:
3677c6fd2807SJeff Garzik 	if (rc)
3678c6fd2807SJeff Garzik 		*r_failed_dev = dev;
3679c6fd2807SJeff Garzik 	return rc;
3680c6fd2807SJeff Garzik }
3681c6fd2807SJeff Garzik 
3682c6fd2807SJeff Garzik /**
3683aa2731adSTejun Heo  *	ata_wait_ready - wait for link to become ready
3684aa2731adSTejun Heo  *	@link: link to be waited on
3685aa2731adSTejun Heo  *	@deadline: deadline jiffies for the operation
3686aa2731adSTejun Heo  *	@check_ready: callback to check link readiness
3687aa2731adSTejun Heo  *
3688aa2731adSTejun Heo  *	Wait for @link to become ready.  @check_ready should return
3689aa2731adSTejun Heo  *	positive number if @link is ready, 0 if it isn't, -ENODEV if
3690aa2731adSTejun Heo  *	link doesn't seem to be occupied, other errno for other error
3691aa2731adSTejun Heo  *	conditions.
3692aa2731adSTejun Heo  *
3693aa2731adSTejun Heo  *	Transient -ENODEV conditions are allowed for
3694aa2731adSTejun Heo  *	ATA_TMOUT_FF_WAIT.
3695aa2731adSTejun Heo  *
3696aa2731adSTejun Heo  *	LOCKING:
3697aa2731adSTejun Heo  *	EH context.
3698aa2731adSTejun Heo  *
3699aa2731adSTejun Heo  *	RETURNS:
3700c9b5560aSMasanari Iida  *	0 if @link is ready before @deadline; otherwise, -errno.
3701aa2731adSTejun Heo  */
3702aa2731adSTejun Heo int ata_wait_ready(struct ata_link *link, unsigned long deadline,
3703aa2731adSTejun Heo 		   int (*check_ready)(struct ata_link *link))
3704aa2731adSTejun Heo {
3705aa2731adSTejun Heo 	unsigned long start = jiffies;
3706b48d58f5STejun Heo 	unsigned long nodev_deadline;
3707aa2731adSTejun Heo 	int warned = 0;
3708aa2731adSTejun Heo 
3709b48d58f5STejun Heo 	/* choose which 0xff timeout to use, read comment in libata.h */
3710b48d58f5STejun Heo 	if (link->ap->host->flags & ATA_HOST_PARALLEL_SCAN)
3711b48d58f5STejun Heo 		nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT_LONG);
3712b48d58f5STejun Heo 	else
3713b48d58f5STejun Heo 		nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT);
3714b48d58f5STejun Heo 
3715b1c72916STejun Heo 	/* Slave readiness can't be tested separately from master.  On
3716b1c72916STejun Heo 	 * M/S emulation configuration, this function should be called
3717b1c72916STejun Heo 	 * only on the master and it will handle both master and slave.
3718b1c72916STejun Heo 	 */
3719b1c72916STejun Heo 	WARN_ON(link == link->ap->slave_link);
3720b1c72916STejun Heo 
3721aa2731adSTejun Heo 	if (time_after(nodev_deadline, deadline))
3722aa2731adSTejun Heo 		nodev_deadline = deadline;
3723aa2731adSTejun Heo 
3724aa2731adSTejun Heo 	while (1) {
3725aa2731adSTejun Heo 		unsigned long now = jiffies;
3726aa2731adSTejun Heo 		int ready, tmp;
3727aa2731adSTejun Heo 
3728aa2731adSTejun Heo 		ready = tmp = check_ready(link);
3729aa2731adSTejun Heo 		if (ready > 0)
3730aa2731adSTejun Heo 			return 0;
3731aa2731adSTejun Heo 
3732b48d58f5STejun Heo 		/*
3733b48d58f5STejun Heo 		 * -ENODEV could be transient.  Ignore -ENODEV if link
3734aa2731adSTejun Heo 		 * is online.  Also, some SATA devices take a long
3735b48d58f5STejun Heo 		 * time to clear 0xff after reset.  Wait for
3736b48d58f5STejun Heo 		 * ATA_TMOUT_FF_WAIT[_LONG] on -ENODEV if link isn't
3737b48d58f5STejun Heo 		 * offline.
3738aa2731adSTejun Heo 		 *
3739aa2731adSTejun Heo 		 * Note that some PATA controllers (pata_ali) explode
3740aa2731adSTejun Heo 		 * if status register is read more than once when
3741aa2731adSTejun Heo 		 * there's no device attached.
3742aa2731adSTejun Heo 		 */
3743aa2731adSTejun Heo 		if (ready == -ENODEV) {
3744aa2731adSTejun Heo 			if (ata_link_online(link))
3745aa2731adSTejun Heo 				ready = 0;
3746aa2731adSTejun Heo 			else if ((link->ap->flags & ATA_FLAG_SATA) &&
3747aa2731adSTejun Heo 				 !ata_link_offline(link) &&
3748aa2731adSTejun Heo 				 time_before(now, nodev_deadline))
3749aa2731adSTejun Heo 				ready = 0;
3750aa2731adSTejun Heo 		}
3751aa2731adSTejun Heo 
3752aa2731adSTejun Heo 		if (ready)
3753aa2731adSTejun Heo 			return ready;
3754aa2731adSTejun Heo 		if (time_after(now, deadline))
3755aa2731adSTejun Heo 			return -EBUSY;
3756aa2731adSTejun Heo 
3757aa2731adSTejun Heo 		if (!warned && time_after(now, start + 5 * HZ) &&
3758aa2731adSTejun Heo 		    (deadline - now > 3 * HZ)) {
3759a9a79dfeSJoe Perches 			ata_link_warn(link,
3760aa2731adSTejun Heo 				"link is slow to respond, please be patient "
3761aa2731adSTejun Heo 				"(ready=%d)\n", tmp);
3762aa2731adSTejun Heo 			warned = 1;
3763aa2731adSTejun Heo 		}
3764aa2731adSTejun Heo 
376597750cebSTejun Heo 		ata_msleep(link->ap, 50);
3766aa2731adSTejun Heo 	}
3767aa2731adSTejun Heo }
3768aa2731adSTejun Heo 
3769aa2731adSTejun Heo /**
3770aa2731adSTejun Heo  *	ata_wait_after_reset - wait for link to become ready after reset
3771aa2731adSTejun Heo  *	@link: link to be waited on
3772aa2731adSTejun Heo  *	@deadline: deadline jiffies for the operation
3773aa2731adSTejun Heo  *	@check_ready: callback to check link readiness
3774aa2731adSTejun Heo  *
3775aa2731adSTejun Heo  *	Wait for @link to become ready after reset.
3776aa2731adSTejun Heo  *
3777aa2731adSTejun Heo  *	LOCKING:
3778aa2731adSTejun Heo  *	EH context.
3779aa2731adSTejun Heo  *
3780aa2731adSTejun Heo  *	RETURNS:
3781c9b5560aSMasanari Iida  *	0 if @link is ready before @deadline; otherwise, -errno.
3782aa2731adSTejun Heo  */
37832b4221bbSHarvey Harrison int ata_wait_after_reset(struct ata_link *link, unsigned long deadline,
3784aa2731adSTejun Heo 				int (*check_ready)(struct ata_link *link))
3785aa2731adSTejun Heo {
378697750cebSTejun Heo 	ata_msleep(link->ap, ATA_WAIT_AFTER_RESET);
3787aa2731adSTejun Heo 
3788aa2731adSTejun Heo 	return ata_wait_ready(link, deadline, check_ready);
3789aa2731adSTejun Heo }
3790aa2731adSTejun Heo 
3791aa2731adSTejun Heo /**
3792936fd732STejun Heo  *	sata_link_debounce - debounce SATA phy status
3793936fd732STejun Heo  *	@link: ATA link to debounce SATA phy status for
3794c9b5560aSMasanari Iida  *	@params: timing parameters { interval, duration, timeout } in msec
3795d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
3796c6fd2807SJeff Garzik  *
3797936fd732STejun Heo  *	Make sure SStatus of @link reaches stable state, determined by
3798c6fd2807SJeff Garzik  *	holding the same value where DET is not 1 for @duration polled
3799c6fd2807SJeff Garzik  *	every @interval, before @timeout.  Timeout constraints the
3800d4b2bab4STejun Heo  *	beginning of the stable state.  Because DET gets stuck at 1 on
3801d4b2bab4STejun Heo  *	some controllers after hot unplugging, this functions waits
3802c6fd2807SJeff Garzik  *	until timeout then returns 0 if DET is stable at 1.
3803c6fd2807SJeff Garzik  *
3804d4b2bab4STejun Heo  *	@timeout is further limited by @deadline.  The sooner of the
3805d4b2bab4STejun Heo  *	two is used.
3806d4b2bab4STejun Heo  *
3807c6fd2807SJeff Garzik  *	LOCKING:
3808c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3809c6fd2807SJeff Garzik  *
3810c6fd2807SJeff Garzik  *	RETURNS:
3811c6fd2807SJeff Garzik  *	0 on success, -errno on failure.
3812c6fd2807SJeff Garzik  */
3813936fd732STejun Heo int sata_link_debounce(struct ata_link *link, const unsigned long *params,
3814d4b2bab4STejun Heo 		       unsigned long deadline)
3815c6fd2807SJeff Garzik {
3816341c2c95STejun Heo 	unsigned long interval = params[0];
3817341c2c95STejun Heo 	unsigned long duration = params[1];
3818d4b2bab4STejun Heo 	unsigned long last_jiffies, t;
3819c6fd2807SJeff Garzik 	u32 last, cur;
3820c6fd2807SJeff Garzik 	int rc;
3821c6fd2807SJeff Garzik 
3822341c2c95STejun Heo 	t = ata_deadline(jiffies, params[2]);
3823d4b2bab4STejun Heo 	if (time_before(t, deadline))
3824d4b2bab4STejun Heo 		deadline = t;
3825d4b2bab4STejun Heo 
3826936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3827c6fd2807SJeff Garzik 		return rc;
3828c6fd2807SJeff Garzik 	cur &= 0xf;
3829c6fd2807SJeff Garzik 
3830c6fd2807SJeff Garzik 	last = cur;
3831c6fd2807SJeff Garzik 	last_jiffies = jiffies;
3832c6fd2807SJeff Garzik 
3833c6fd2807SJeff Garzik 	while (1) {
383497750cebSTejun Heo 		ata_msleep(link->ap, interval);
3835936fd732STejun Heo 		if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3836c6fd2807SJeff Garzik 			return rc;
3837c6fd2807SJeff Garzik 		cur &= 0xf;
3838c6fd2807SJeff Garzik 
3839c6fd2807SJeff Garzik 		/* DET stable? */
3840c6fd2807SJeff Garzik 		if (cur == last) {
3841d4b2bab4STejun Heo 			if (cur == 1 && time_before(jiffies, deadline))
3842c6fd2807SJeff Garzik 				continue;
3843341c2c95STejun Heo 			if (time_after(jiffies,
3844341c2c95STejun Heo 				       ata_deadline(last_jiffies, duration)))
3845c6fd2807SJeff Garzik 				return 0;
3846c6fd2807SJeff Garzik 			continue;
3847c6fd2807SJeff Garzik 		}
3848c6fd2807SJeff Garzik 
3849c6fd2807SJeff Garzik 		/* unstable, start over */
3850c6fd2807SJeff Garzik 		last = cur;
3851c6fd2807SJeff Garzik 		last_jiffies = jiffies;
3852c6fd2807SJeff Garzik 
3853f1545154STejun Heo 		/* Check deadline.  If debouncing failed, return
3854f1545154STejun Heo 		 * -EPIPE to tell upper layer to lower link speed.
3855f1545154STejun Heo 		 */
3856d4b2bab4STejun Heo 		if (time_after(jiffies, deadline))
3857f1545154STejun Heo 			return -EPIPE;
3858c6fd2807SJeff Garzik 	}
3859c6fd2807SJeff Garzik }
3860c6fd2807SJeff Garzik 
3861c6fd2807SJeff Garzik /**
3862936fd732STejun Heo  *	sata_link_resume - resume SATA link
3863936fd732STejun Heo  *	@link: ATA link to resume SATA
3864c9b5560aSMasanari Iida  *	@params: timing parameters { interval, duration, timeout } in msec
3865d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
3866c6fd2807SJeff Garzik  *
3867936fd732STejun Heo  *	Resume SATA phy @link and debounce it.
3868c6fd2807SJeff Garzik  *
3869c6fd2807SJeff Garzik  *	LOCKING:
3870c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3871c6fd2807SJeff Garzik  *
3872c6fd2807SJeff Garzik  *	RETURNS:
3873c6fd2807SJeff Garzik  *	0 on success, -errno on failure.
3874c6fd2807SJeff Garzik  */
3875936fd732STejun Heo int sata_link_resume(struct ata_link *link, const unsigned long *params,
3876d4b2bab4STejun Heo 		     unsigned long deadline)
3877c6fd2807SJeff Garzik {
38785040ab67STejun Heo 	int tries = ATA_LINK_RESUME_TRIES;
3879ac371987STejun Heo 	u32 scontrol, serror;
3880c6fd2807SJeff Garzik 	int rc;
3881c6fd2807SJeff Garzik 
3882936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3883c6fd2807SJeff Garzik 		return rc;
3884c6fd2807SJeff Garzik 
38855040ab67STejun Heo 	/*
38865040ab67STejun Heo 	 * Writes to SControl sometimes get ignored under certain
38875040ab67STejun Heo 	 * controllers (ata_piix SIDPR).  Make sure DET actually is
38885040ab67STejun Heo 	 * cleared.
38895040ab67STejun Heo 	 */
38905040ab67STejun Heo 	do {
3891c6fd2807SJeff Garzik 		scontrol = (scontrol & 0x0f0) | 0x300;
3892936fd732STejun Heo 		if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3893c6fd2807SJeff Garzik 			return rc;
38945040ab67STejun Heo 		/*
38955040ab67STejun Heo 		 * Some PHYs react badly if SStatus is pounded
38965040ab67STejun Heo 		 * immediately after resuming.  Delay 200ms before
38975040ab67STejun Heo 		 * debouncing.
3898c6fd2807SJeff Garzik 		 */
3899e39b2bb3SDanesh Petigara 		if (!(link->flags & ATA_LFLAG_NO_DB_DELAY))
390097750cebSTejun Heo 			ata_msleep(link->ap, 200);
3901c6fd2807SJeff Garzik 
39025040ab67STejun Heo 		/* is SControl restored correctly? */
39035040ab67STejun Heo 		if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
39045040ab67STejun Heo 			return rc;
39055040ab67STejun Heo 	} while ((scontrol & 0xf0f) != 0x300 && --tries);
39065040ab67STejun Heo 
39075040ab67STejun Heo 	if ((scontrol & 0xf0f) != 0x300) {
390838941c95STejun Heo 		ata_link_warn(link, "failed to resume link (SControl %X)\n",
39095040ab67STejun Heo 			     scontrol);
39105040ab67STejun Heo 		return 0;
39115040ab67STejun Heo 	}
39125040ab67STejun Heo 
39135040ab67STejun Heo 	if (tries < ATA_LINK_RESUME_TRIES)
3914a9a79dfeSJoe Perches 		ata_link_warn(link, "link resume succeeded after %d retries\n",
39155040ab67STejun Heo 			      ATA_LINK_RESUME_TRIES - tries);
39165040ab67STejun Heo 
3917ac371987STejun Heo 	if ((rc = sata_link_debounce(link, params, deadline)))
3918ac371987STejun Heo 		return rc;
3919ac371987STejun Heo 
3920f046519fSTejun Heo 	/* clear SError, some PHYs require this even for SRST to work */
3921ac371987STejun Heo 	if (!(rc = sata_scr_read(link, SCR_ERROR, &serror)))
3922ac371987STejun Heo 		rc = sata_scr_write(link, SCR_ERROR, serror);
3923ac371987STejun Heo 
3924f046519fSTejun Heo 	return rc != -EINVAL ? rc : 0;
3925c6fd2807SJeff Garzik }
3926c6fd2807SJeff Garzik 
3927c6fd2807SJeff Garzik /**
39281152b261STejun Heo  *	sata_link_scr_lpm - manipulate SControl IPM and SPM fields
39291152b261STejun Heo  *	@link: ATA link to manipulate SControl for
39301152b261STejun Heo  *	@policy: LPM policy to configure
39311152b261STejun Heo  *	@spm_wakeup: initiate LPM transition to active state
39321152b261STejun Heo  *
39331152b261STejun Heo  *	Manipulate the IPM field of the SControl register of @link
39341152b261STejun Heo  *	according to @policy.  If @policy is ATA_LPM_MAX_POWER and
39351152b261STejun Heo  *	@spm_wakeup is %true, the SPM field is manipulated to wake up
39361152b261STejun Heo  *	the link.  This function also clears PHYRDY_CHG before
39371152b261STejun Heo  *	returning.
39381152b261STejun Heo  *
39391152b261STejun Heo  *	LOCKING:
39401152b261STejun Heo  *	EH context.
39411152b261STejun Heo  *
39421152b261STejun Heo  *	RETURNS:
39438485187bSMasanari Iida  *	0 on success, -errno otherwise.
39441152b261STejun Heo  */
39451152b261STejun Heo int sata_link_scr_lpm(struct ata_link *link, enum ata_lpm_policy policy,
39461152b261STejun Heo 		      bool spm_wakeup)
39471152b261STejun Heo {
39481152b261STejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
39491152b261STejun Heo 	bool woken_up = false;
39501152b261STejun Heo 	u32 scontrol;
39511152b261STejun Heo 	int rc;
39521152b261STejun Heo 
39531152b261STejun Heo 	rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
39541152b261STejun Heo 	if (rc)
39551152b261STejun Heo 		return rc;
39561152b261STejun Heo 
39571152b261STejun Heo 	switch (policy) {
39581152b261STejun Heo 	case ATA_LPM_MAX_POWER:
39591152b261STejun Heo 		/* disable all LPM transitions */
396065fe1f0fSShane Huang 		scontrol |= (0x7 << 8);
39611152b261STejun Heo 		/* initiate transition to active state */
39621152b261STejun Heo 		if (spm_wakeup) {
39631152b261STejun Heo 			scontrol |= (0x4 << 12);
39641152b261STejun Heo 			woken_up = true;
39651152b261STejun Heo 		}
39661152b261STejun Heo 		break;
39671152b261STejun Heo 	case ATA_LPM_MED_POWER:
39681152b261STejun Heo 		/* allow LPM to PARTIAL */
39691152b261STejun Heo 		scontrol &= ~(0x1 << 8);
397065fe1f0fSShane Huang 		scontrol |= (0x6 << 8);
39711152b261STejun Heo 		break;
3972f4ac6476SHans de Goede 	case ATA_LPM_MED_POWER_WITH_DIPM:
3973a5ec5a7bSSrinivas Pandruvada 	case ATA_LPM_MIN_POWER_WITH_PARTIAL:
39741152b261STejun Heo 	case ATA_LPM_MIN_POWER:
39758a745f1fSKristen Carlson Accardi 		if (ata_link_nr_enabled(link) > 0)
39761152b261STejun Heo 			/* no restrictions on LPM transitions */
397765fe1f0fSShane Huang 			scontrol &= ~(0x7 << 8);
39788a745f1fSKristen Carlson Accardi 		else {
39798a745f1fSKristen Carlson Accardi 			/* empty port, power off */
39808a745f1fSKristen Carlson Accardi 			scontrol &= ~0xf;
39818a745f1fSKristen Carlson Accardi 			scontrol |= (0x1 << 2);
39828a745f1fSKristen Carlson Accardi 		}
39831152b261STejun Heo 		break;
39841152b261STejun Heo 	default:
39851152b261STejun Heo 		WARN_ON(1);
39861152b261STejun Heo 	}
39871152b261STejun Heo 
39881152b261STejun Heo 	rc = sata_scr_write(link, SCR_CONTROL, scontrol);
39891152b261STejun Heo 	if (rc)
39901152b261STejun Heo 		return rc;
39911152b261STejun Heo 
39921152b261STejun Heo 	/* give the link time to transit out of LPM state */
39931152b261STejun Heo 	if (woken_up)
39941152b261STejun Heo 		msleep(10);
39951152b261STejun Heo 
39961152b261STejun Heo 	/* clear PHYRDY_CHG from SError */
39971152b261STejun Heo 	ehc->i.serror &= ~SERR_PHYRDY_CHG;
39981152b261STejun Heo 	return sata_scr_write(link, SCR_ERROR, SERR_PHYRDY_CHG);
39991152b261STejun Heo }
40001152b261STejun Heo 
40011152b261STejun Heo /**
40020aa1113dSTejun Heo  *	ata_std_prereset - prepare for reset
4003cc0680a5STejun Heo  *	@link: ATA link to be reset
4004d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
4005c6fd2807SJeff Garzik  *
4006cc0680a5STejun Heo  *	@link is about to be reset.  Initialize it.  Failure from
4007b8cffc6aSTejun Heo  *	prereset makes libata abort whole reset sequence and give up
4008b8cffc6aSTejun Heo  *	that port, so prereset should be best-effort.  It does its
4009b8cffc6aSTejun Heo  *	best to prepare for reset sequence but if things go wrong, it
4010b8cffc6aSTejun Heo  *	should just whine, not fail.
4011c6fd2807SJeff Garzik  *
4012c6fd2807SJeff Garzik  *	LOCKING:
4013c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
4014c6fd2807SJeff Garzik  *
4015c6fd2807SJeff Garzik  *	RETURNS:
4016c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
4017c6fd2807SJeff Garzik  */
40180aa1113dSTejun Heo int ata_std_prereset(struct ata_link *link, unsigned long deadline)
4019c6fd2807SJeff Garzik {
4020cc0680a5STejun Heo 	struct ata_port *ap = link->ap;
4021936fd732STejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
4022c6fd2807SJeff Garzik 	const unsigned long *timing = sata_ehc_deb_timing(ehc);
4023c6fd2807SJeff Garzik 	int rc;
4024c6fd2807SJeff Garzik 
4025c6fd2807SJeff Garzik 	/* if we're about to do hardreset, nothing more to do */
4026c6fd2807SJeff Garzik 	if (ehc->i.action & ATA_EH_HARDRESET)
4027c6fd2807SJeff Garzik 		return 0;
4028c6fd2807SJeff Garzik 
4029936fd732STejun Heo 	/* if SATA, resume link */
4030a16abc0bSTejun Heo 	if (ap->flags & ATA_FLAG_SATA) {
4031936fd732STejun Heo 		rc = sata_link_resume(link, timing, deadline);
4032b8cffc6aSTejun Heo 		/* whine about phy resume failure but proceed */
4033b8cffc6aSTejun Heo 		if (rc && rc != -EOPNOTSUPP)
4034a9a79dfeSJoe Perches 			ata_link_warn(link,
4035a9a79dfeSJoe Perches 				      "failed to resume link for reset (errno=%d)\n",
4036a9a79dfeSJoe Perches 				      rc);
4037c6fd2807SJeff Garzik 	}
4038c6fd2807SJeff Garzik 
403945db2f6cSTejun Heo 	/* no point in trying softreset on offline link */
4040b1c72916STejun Heo 	if (ata_phys_link_offline(link))
404145db2f6cSTejun Heo 		ehc->i.action &= ~ATA_EH_SOFTRESET;
404245db2f6cSTejun Heo 
4043c6fd2807SJeff Garzik 	return 0;
4044c6fd2807SJeff Garzik }
4045c6fd2807SJeff Garzik 
4046c6fd2807SJeff Garzik /**
4047cc0680a5STejun Heo  *	sata_link_hardreset - reset link via SATA phy reset
4048cc0680a5STejun Heo  *	@link: link to reset
4049c9b5560aSMasanari Iida  *	@timing: timing parameters { interval, duration, timeout } in msec
4050d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
40519dadd45bSTejun Heo  *	@online: optional out parameter indicating link onlineness
40529dadd45bSTejun Heo  *	@check_ready: optional callback to check link readiness
4053c6fd2807SJeff Garzik  *
4054cc0680a5STejun Heo  *	SATA phy-reset @link using DET bits of SControl register.
40559dadd45bSTejun Heo  *	After hardreset, link readiness is waited upon using
40569dadd45bSTejun Heo  *	ata_wait_ready() if @check_ready is specified.  LLDs are
40579dadd45bSTejun Heo  *	allowed to not specify @check_ready and wait itself after this
40589dadd45bSTejun Heo  *	function returns.  Device classification is LLD's
40599dadd45bSTejun Heo  *	responsibility.
40609dadd45bSTejun Heo  *
40619dadd45bSTejun Heo  *	*@online is set to one iff reset succeeded and @link is online
40629dadd45bSTejun Heo  *	after reset.
4063c6fd2807SJeff Garzik  *
4064c6fd2807SJeff Garzik  *	LOCKING:
4065c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
4066c6fd2807SJeff Garzik  *
4067c6fd2807SJeff Garzik  *	RETURNS:
4068c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
4069c6fd2807SJeff Garzik  */
4070cc0680a5STejun Heo int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
40719dadd45bSTejun Heo 			unsigned long deadline,
40729dadd45bSTejun Heo 			bool *online, int (*check_ready)(struct ata_link *))
4073c6fd2807SJeff Garzik {
4074c6fd2807SJeff Garzik 	u32 scontrol;
4075c6fd2807SJeff Garzik 	int rc;
4076c6fd2807SJeff Garzik 
4077c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
4078c6fd2807SJeff Garzik 
40799dadd45bSTejun Heo 	if (online)
40809dadd45bSTejun Heo 		*online = false;
40819dadd45bSTejun Heo 
4082936fd732STejun Heo 	if (sata_set_spd_needed(link)) {
4083c6fd2807SJeff Garzik 		/* SATA spec says nothing about how to reconfigure
4084c6fd2807SJeff Garzik 		 * spd.  To be on the safe side, turn off phy during
4085c6fd2807SJeff Garzik 		 * reconfiguration.  This works for at least ICH7 AHCI
4086c6fd2807SJeff Garzik 		 * and Sil3124.
4087c6fd2807SJeff Garzik 		 */
4088936fd732STejun Heo 		if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
4089b6103f6dSTejun Heo 			goto out;
4090c6fd2807SJeff Garzik 
4091cea0d336SJeff Garzik 		scontrol = (scontrol & 0x0f0) | 0x304;
4092c6fd2807SJeff Garzik 
4093936fd732STejun Heo 		if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
4094b6103f6dSTejun Heo 			goto out;
4095c6fd2807SJeff Garzik 
4096936fd732STejun Heo 		sata_set_spd(link);
4097c6fd2807SJeff Garzik 	}
4098c6fd2807SJeff Garzik 
4099c6fd2807SJeff Garzik 	/* issue phy wake/reset */
4100936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
4101b6103f6dSTejun Heo 		goto out;
4102c6fd2807SJeff Garzik 
4103c6fd2807SJeff Garzik 	scontrol = (scontrol & 0x0f0) | 0x301;
4104c6fd2807SJeff Garzik 
4105936fd732STejun Heo 	if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
4106b6103f6dSTejun Heo 		goto out;
4107c6fd2807SJeff Garzik 
4108c6fd2807SJeff Garzik 	/* Couldn't find anything in SATA I/II specs, but AHCI-1.1
4109c6fd2807SJeff Garzik 	 * 10.4.2 says at least 1 ms.
4110c6fd2807SJeff Garzik 	 */
411197750cebSTejun Heo 	ata_msleep(link->ap, 1);
4112c6fd2807SJeff Garzik 
4113936fd732STejun Heo 	/* bring link back */
4114936fd732STejun Heo 	rc = sata_link_resume(link, timing, deadline);
41159dadd45bSTejun Heo 	if (rc)
41169dadd45bSTejun Heo 		goto out;
41179dadd45bSTejun Heo 	/* if link is offline nothing more to do */
4118b1c72916STejun Heo 	if (ata_phys_link_offline(link))
41199dadd45bSTejun Heo 		goto out;
41209dadd45bSTejun Heo 
41219dadd45bSTejun Heo 	/* Link is online.  From this point, -ENODEV too is an error. */
41229dadd45bSTejun Heo 	if (online)
41239dadd45bSTejun Heo 		*online = true;
41249dadd45bSTejun Heo 
4125071f44b1STejun Heo 	if (sata_pmp_supported(link->ap) && ata_is_host_link(link)) {
41269dadd45bSTejun Heo 		/* If PMP is supported, we have to do follow-up SRST.
41279dadd45bSTejun Heo 		 * Some PMPs don't send D2H Reg FIS after hardreset if
41289dadd45bSTejun Heo 		 * the first port is empty.  Wait only for
41299dadd45bSTejun Heo 		 * ATA_TMOUT_PMP_SRST_WAIT.
41309dadd45bSTejun Heo 		 */
41319dadd45bSTejun Heo 		if (check_ready) {
41329dadd45bSTejun Heo 			unsigned long pmp_deadline;
41339dadd45bSTejun Heo 
4134341c2c95STejun Heo 			pmp_deadline = ata_deadline(jiffies,
4135341c2c95STejun Heo 						    ATA_TMOUT_PMP_SRST_WAIT);
41369dadd45bSTejun Heo 			if (time_after(pmp_deadline, deadline))
41379dadd45bSTejun Heo 				pmp_deadline = deadline;
41389dadd45bSTejun Heo 			ata_wait_ready(link, pmp_deadline, check_ready);
41399dadd45bSTejun Heo 		}
41409dadd45bSTejun Heo 		rc = -EAGAIN;
41419dadd45bSTejun Heo 		goto out;
41429dadd45bSTejun Heo 	}
41439dadd45bSTejun Heo 
41449dadd45bSTejun Heo 	rc = 0;
41459dadd45bSTejun Heo 	if (check_ready)
41469dadd45bSTejun Heo 		rc = ata_wait_ready(link, deadline, check_ready);
4147b6103f6dSTejun Heo  out:
41480cbf0711STejun Heo 	if (rc && rc != -EAGAIN) {
41490cbf0711STejun Heo 		/* online is set iff link is online && reset succeeded */
41500cbf0711STejun Heo 		if (online)
41510cbf0711STejun Heo 			*online = false;
4152a9a79dfeSJoe Perches 		ata_link_err(link, "COMRESET failed (errno=%d)\n", rc);
41530cbf0711STejun Heo 	}
4154b6103f6dSTejun Heo 	DPRINTK("EXIT, rc=%d\n", rc);
4155b6103f6dSTejun Heo 	return rc;
4156b6103f6dSTejun Heo }
4157b6103f6dSTejun Heo 
4158b6103f6dSTejun Heo /**
415957c9efdfSTejun Heo  *	sata_std_hardreset - COMRESET w/o waiting or classification
416057c9efdfSTejun Heo  *	@link: link to reset
416157c9efdfSTejun Heo  *	@class: resulting class of attached device
416257c9efdfSTejun Heo  *	@deadline: deadline jiffies for the operation
416357c9efdfSTejun Heo  *
416457c9efdfSTejun Heo  *	Standard SATA COMRESET w/o waiting or classification.
416557c9efdfSTejun Heo  *
416657c9efdfSTejun Heo  *	LOCKING:
416757c9efdfSTejun Heo  *	Kernel thread context (may sleep)
416857c9efdfSTejun Heo  *
416957c9efdfSTejun Heo  *	RETURNS:
417057c9efdfSTejun Heo  *	0 if link offline, -EAGAIN if link online, -errno on errors.
417157c9efdfSTejun Heo  */
417257c9efdfSTejun Heo int sata_std_hardreset(struct ata_link *link, unsigned int *class,
417357c9efdfSTejun Heo 		       unsigned long deadline)
417457c9efdfSTejun Heo {
417557c9efdfSTejun Heo 	const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
417657c9efdfSTejun Heo 	bool online;
417757c9efdfSTejun Heo 	int rc;
417857c9efdfSTejun Heo 
417957c9efdfSTejun Heo 	/* do hardreset */
418057c9efdfSTejun Heo 	rc = sata_link_hardreset(link, timing, deadline, &online, NULL);
418157c9efdfSTejun Heo 	return online ? -EAGAIN : rc;
418257c9efdfSTejun Heo }
418357c9efdfSTejun Heo 
418457c9efdfSTejun Heo /**
4185203c75b8STejun Heo  *	ata_std_postreset - standard postreset callback
4186cc0680a5STejun Heo  *	@link: the target ata_link
4187c6fd2807SJeff Garzik  *	@classes: classes of attached devices
4188c6fd2807SJeff Garzik  *
4189c6fd2807SJeff Garzik  *	This function is invoked after a successful reset.  Note that
4190c6fd2807SJeff Garzik  *	the device might have been reset more than once using
4191c6fd2807SJeff Garzik  *	different reset methods before postreset is invoked.
4192c6fd2807SJeff Garzik  *
4193c6fd2807SJeff Garzik  *	LOCKING:
4194c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
4195c6fd2807SJeff Garzik  */
4196203c75b8STejun Heo void ata_std_postreset(struct ata_link *link, unsigned int *classes)
4197c6fd2807SJeff Garzik {
4198f046519fSTejun Heo 	u32 serror;
4199f046519fSTejun Heo 
4200c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
4201c6fd2807SJeff Garzik 
4202f046519fSTejun Heo 	/* reset complete, clear SError */
4203f046519fSTejun Heo 	if (!sata_scr_read(link, SCR_ERROR, &serror))
4204f046519fSTejun Heo 		sata_scr_write(link, SCR_ERROR, serror);
4205f046519fSTejun Heo 
4206c6fd2807SJeff Garzik 	/* print link status */
4207936fd732STejun Heo 	sata_print_link_status(link);
4208c6fd2807SJeff Garzik 
4209c6fd2807SJeff Garzik 	DPRINTK("EXIT\n");
4210c6fd2807SJeff Garzik }
4211c6fd2807SJeff Garzik 
4212c6fd2807SJeff Garzik /**
4213c6fd2807SJeff Garzik  *	ata_dev_same_device - Determine whether new ID matches configured device
4214c6fd2807SJeff Garzik  *	@dev: device to compare against
4215c6fd2807SJeff Garzik  *	@new_class: class of the new device
4216c6fd2807SJeff Garzik  *	@new_id: IDENTIFY page of the new device
4217c6fd2807SJeff Garzik  *
4218c6fd2807SJeff Garzik  *	Compare @new_class and @new_id against @dev and determine
4219c6fd2807SJeff Garzik  *	whether @dev is the device indicated by @new_class and
4220c6fd2807SJeff Garzik  *	@new_id.
4221c6fd2807SJeff Garzik  *
4222c6fd2807SJeff Garzik  *	LOCKING:
4223c6fd2807SJeff Garzik  *	None.
4224c6fd2807SJeff Garzik  *
4225c6fd2807SJeff Garzik  *	RETURNS:
4226c6fd2807SJeff Garzik  *	1 if @dev matches @new_class and @new_id, 0 otherwise.
4227c6fd2807SJeff Garzik  */
4228c6fd2807SJeff Garzik static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
4229c6fd2807SJeff Garzik 			       const u16 *new_id)
4230c6fd2807SJeff Garzik {
4231c6fd2807SJeff Garzik 	const u16 *old_id = dev->id;
4232a0cf733bSTejun Heo 	unsigned char model[2][ATA_ID_PROD_LEN + 1];
4233a0cf733bSTejun Heo 	unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
4234c6fd2807SJeff Garzik 
4235c6fd2807SJeff Garzik 	if (dev->class != new_class) {
4236a9a79dfeSJoe Perches 		ata_dev_info(dev, "class mismatch %d != %d\n",
4237c6fd2807SJeff Garzik 			     dev->class, new_class);
4238c6fd2807SJeff Garzik 		return 0;
4239c6fd2807SJeff Garzik 	}
4240c6fd2807SJeff Garzik 
4241a0cf733bSTejun Heo 	ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
4242a0cf733bSTejun Heo 	ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
4243a0cf733bSTejun Heo 	ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
4244a0cf733bSTejun Heo 	ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
4245c6fd2807SJeff Garzik 
4246c6fd2807SJeff Garzik 	if (strcmp(model[0], model[1])) {
4247a9a79dfeSJoe Perches 		ata_dev_info(dev, "model number mismatch '%s' != '%s'\n",
4248a9a79dfeSJoe Perches 			     model[0], model[1]);
4249c6fd2807SJeff Garzik 		return 0;
4250c6fd2807SJeff Garzik 	}
4251c6fd2807SJeff Garzik 
4252c6fd2807SJeff Garzik 	if (strcmp(serial[0], serial[1])) {
4253a9a79dfeSJoe Perches 		ata_dev_info(dev, "serial number mismatch '%s' != '%s'\n",
4254a9a79dfeSJoe Perches 			     serial[0], serial[1]);
4255c6fd2807SJeff Garzik 		return 0;
4256c6fd2807SJeff Garzik 	}
4257c6fd2807SJeff Garzik 
4258c6fd2807SJeff Garzik 	return 1;
4259c6fd2807SJeff Garzik }
4260c6fd2807SJeff Garzik 
4261c6fd2807SJeff Garzik /**
4262fe30911bSTejun Heo  *	ata_dev_reread_id - Re-read IDENTIFY data
42633fae450cSHenrik Kretzschmar  *	@dev: target ATA device
4264bff04647STejun Heo  *	@readid_flags: read ID flags
4265c6fd2807SJeff Garzik  *
4266c6fd2807SJeff Garzik  *	Re-read IDENTIFY page and make sure @dev is still attached to
4267c6fd2807SJeff Garzik  *	the port.
4268c6fd2807SJeff Garzik  *
4269c6fd2807SJeff Garzik  *	LOCKING:
4270c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
4271c6fd2807SJeff Garzik  *
4272c6fd2807SJeff Garzik  *	RETURNS:
4273c6fd2807SJeff Garzik  *	0 on success, negative errno otherwise
4274c6fd2807SJeff Garzik  */
4275fe30911bSTejun Heo int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
4276c6fd2807SJeff Garzik {
4277c6fd2807SJeff Garzik 	unsigned int class = dev->class;
42789af5c9c9STejun Heo 	u16 *id = (void *)dev->link->ap->sector_buf;
4279c6fd2807SJeff Garzik 	int rc;
4280c6fd2807SJeff Garzik 
4281c6fd2807SJeff Garzik 	/* read ID data */
4282bff04647STejun Heo 	rc = ata_dev_read_id(dev, &class, readid_flags, id);
4283c6fd2807SJeff Garzik 	if (rc)
4284fe30911bSTejun Heo 		return rc;
4285c6fd2807SJeff Garzik 
4286c6fd2807SJeff Garzik 	/* is the device still there? */
4287fe30911bSTejun Heo 	if (!ata_dev_same_device(dev, class, id))
4288fe30911bSTejun Heo 		return -ENODEV;
4289c6fd2807SJeff Garzik 
4290c6fd2807SJeff Garzik 	memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
4291fe30911bSTejun Heo 	return 0;
4292fe30911bSTejun Heo }
4293fe30911bSTejun Heo 
4294fe30911bSTejun Heo /**
4295fe30911bSTejun Heo  *	ata_dev_revalidate - Revalidate ATA device
4296fe30911bSTejun Heo  *	@dev: device to revalidate
4297422c9daaSTejun Heo  *	@new_class: new class code
4298fe30911bSTejun Heo  *	@readid_flags: read ID flags
4299fe30911bSTejun Heo  *
4300fe30911bSTejun Heo  *	Re-read IDENTIFY page, make sure @dev is still attached to the
4301fe30911bSTejun Heo  *	port and reconfigure it according to the new IDENTIFY page.
4302fe30911bSTejun Heo  *
4303fe30911bSTejun Heo  *	LOCKING:
4304fe30911bSTejun Heo  *	Kernel thread context (may sleep)
4305fe30911bSTejun Heo  *
4306fe30911bSTejun Heo  *	RETURNS:
4307fe30911bSTejun Heo  *	0 on success, negative errno otherwise
4308fe30911bSTejun Heo  */
4309422c9daaSTejun Heo int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
4310422c9daaSTejun Heo 		       unsigned int readid_flags)
4311fe30911bSTejun Heo {
43126ddcd3b0STejun Heo 	u64 n_sectors = dev->n_sectors;
43135920dadfSTejun Heo 	u64 n_native_sectors = dev->n_native_sectors;
4314fe30911bSTejun Heo 	int rc;
4315fe30911bSTejun Heo 
4316fe30911bSTejun Heo 	if (!ata_dev_enabled(dev))
4317fe30911bSTejun Heo 		return -ENODEV;
4318fe30911bSTejun Heo 
4319422c9daaSTejun Heo 	/* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
4320422c9daaSTejun Heo 	if (ata_class_enabled(new_class) &&
4321f0d0613dSBorislav Petkov 	    new_class != ATA_DEV_ATA &&
4322f0d0613dSBorislav Petkov 	    new_class != ATA_DEV_ATAPI &&
43239162c657SHannes Reinecke 	    new_class != ATA_DEV_ZAC &&
4324f0d0613dSBorislav Petkov 	    new_class != ATA_DEV_SEMB) {
4325a9a79dfeSJoe Perches 		ata_dev_info(dev, "class mismatch %u != %u\n",
4326422c9daaSTejun Heo 			     dev->class, new_class);
4327422c9daaSTejun Heo 		rc = -ENODEV;
4328422c9daaSTejun Heo 		goto fail;
4329422c9daaSTejun Heo 	}
4330422c9daaSTejun Heo 
4331fe30911bSTejun Heo 	/* re-read ID */
4332fe30911bSTejun Heo 	rc = ata_dev_reread_id(dev, readid_flags);
4333fe30911bSTejun Heo 	if (rc)
4334fe30911bSTejun Heo 		goto fail;
4335c6fd2807SJeff Garzik 
4336c6fd2807SJeff Garzik 	/* configure device according to the new ID */
4337efdaedc4STejun Heo 	rc = ata_dev_configure(dev);
43386ddcd3b0STejun Heo 	if (rc)
43396ddcd3b0STejun Heo 		goto fail;
43406ddcd3b0STejun Heo 
43416ddcd3b0STejun Heo 	/* verify n_sectors hasn't changed */
4342445d211bSTejun Heo 	if (dev->class != ATA_DEV_ATA || !n_sectors ||
4343445d211bSTejun Heo 	    dev->n_sectors == n_sectors)
4344445d211bSTejun Heo 		return 0;
4345445d211bSTejun Heo 
4346445d211bSTejun Heo 	/* n_sectors has changed */
4347a9a79dfeSJoe Perches 	ata_dev_warn(dev, "n_sectors mismatch %llu != %llu\n",
43486ddcd3b0STejun Heo 		     (unsigned long long)n_sectors,
43496ddcd3b0STejun Heo 		     (unsigned long long)dev->n_sectors);
4350445d211bSTejun Heo 
43515920dadfSTejun Heo 	/*
43525920dadfSTejun Heo 	 * Something could have caused HPA to be unlocked
4353445d211bSTejun Heo 	 * involuntarily.  If n_native_sectors hasn't changed and the
4354445d211bSTejun Heo 	 * new size matches it, keep the device.
43555920dadfSTejun Heo 	 */
43565920dadfSTejun Heo 	if (dev->n_native_sectors == n_native_sectors &&
4357445d211bSTejun Heo 	    dev->n_sectors > n_sectors && dev->n_sectors == n_native_sectors) {
4358a9a79dfeSJoe Perches 		ata_dev_warn(dev,
43595920dadfSTejun Heo 			     "new n_sectors matches native, probably "
436068939ce5STejun Heo 			     "late HPA unlock, n_sectors updated\n");
436168939ce5STejun Heo 		/* use the larger n_sectors */
4362445d211bSTejun Heo 		return 0;
4363445d211bSTejun Heo 	}
4364445d211bSTejun Heo 
4365445d211bSTejun Heo 	/*
4366445d211bSTejun Heo 	 * Some BIOSes boot w/o HPA but resume w/ HPA locked.  Try
4367445d211bSTejun Heo 	 * unlocking HPA in those cases.
4368445d211bSTejun Heo 	 *
4369445d211bSTejun Heo 	 * https://bugzilla.kernel.org/show_bug.cgi?id=15396
4370445d211bSTejun Heo 	 */
4371445d211bSTejun Heo 	if (dev->n_native_sectors == n_native_sectors &&
4372445d211bSTejun Heo 	    dev->n_sectors < n_sectors && n_sectors == n_native_sectors &&
4373445d211bSTejun Heo 	    !(dev->horkage & ATA_HORKAGE_BROKEN_HPA)) {
4374a9a79dfeSJoe Perches 		ata_dev_warn(dev,
4375445d211bSTejun Heo 			     "old n_sectors matches native, probably "
4376445d211bSTejun Heo 			     "late HPA lock, will try to unlock HPA\n");
4377445d211bSTejun Heo 		/* try unlocking HPA */
4378445d211bSTejun Heo 		dev->flags |= ATA_DFLAG_UNLOCK_HPA;
4379445d211bSTejun Heo 		rc = -EIO;
4380445d211bSTejun Heo 	} else
4381445d211bSTejun Heo 		rc = -ENODEV;
4382445d211bSTejun Heo 
4383445d211bSTejun Heo 	/* restore original n_[native_]sectors and fail */
43845920dadfSTejun Heo 	dev->n_native_sectors = n_native_sectors;
43855920dadfSTejun Heo 	dev->n_sectors = n_sectors;
4386c6fd2807SJeff Garzik  fail:
4387a9a79dfeSJoe Perches 	ata_dev_err(dev, "revalidation failed (errno=%d)\n", rc);
4388c6fd2807SJeff Garzik 	return rc;
4389c6fd2807SJeff Garzik }
4390c6fd2807SJeff Garzik 
43916919a0a6SAlan Cox struct ata_blacklist_entry {
43926919a0a6SAlan Cox 	const char *model_num;
43936919a0a6SAlan Cox 	const char *model_rev;
43946919a0a6SAlan Cox 	unsigned long horkage;
43956919a0a6SAlan Cox };
43966919a0a6SAlan Cox 
43976919a0a6SAlan Cox static const struct ata_blacklist_entry ata_device_blacklist [] = {
43986919a0a6SAlan Cox 	/* Devices with DMA related problems under Linux */
43996919a0a6SAlan Cox 	{ "WDC AC11000H",	NULL,		ATA_HORKAGE_NODMA },
44006919a0a6SAlan Cox 	{ "WDC AC22100H",	NULL,		ATA_HORKAGE_NODMA },
44016919a0a6SAlan Cox 	{ "WDC AC32500H",	NULL,		ATA_HORKAGE_NODMA },
44026919a0a6SAlan Cox 	{ "WDC AC33100H",	NULL,		ATA_HORKAGE_NODMA },
44036919a0a6SAlan Cox 	{ "WDC AC31600H",	NULL,		ATA_HORKAGE_NODMA },
44046919a0a6SAlan Cox 	{ "WDC AC32100H",	"24.09P07",	ATA_HORKAGE_NODMA },
44056919a0a6SAlan Cox 	{ "WDC AC23200L",	"21.10N21",	ATA_HORKAGE_NODMA },
44066919a0a6SAlan Cox 	{ "Compaq CRD-8241B", 	NULL,		ATA_HORKAGE_NODMA },
44076919a0a6SAlan Cox 	{ "CRD-8400B",		NULL, 		ATA_HORKAGE_NODMA },
44087da4c935SMark Lord 	{ "CRD-848[02]B",	NULL,		ATA_HORKAGE_NODMA },
44096919a0a6SAlan Cox 	{ "CRD-84",		NULL,		ATA_HORKAGE_NODMA },
44106919a0a6SAlan Cox 	{ "SanDisk SDP3B",	NULL,		ATA_HORKAGE_NODMA },
44116919a0a6SAlan Cox 	{ "SanDisk SDP3B-64",	NULL,		ATA_HORKAGE_NODMA },
44126919a0a6SAlan Cox 	{ "SANYO CD-ROM CRD",	NULL,		ATA_HORKAGE_NODMA },
44136919a0a6SAlan Cox 	{ "HITACHI CDR-8",	NULL,		ATA_HORKAGE_NODMA },
44147da4c935SMark Lord 	{ "HITACHI CDR-8[34]35",NULL,		ATA_HORKAGE_NODMA },
44156919a0a6SAlan Cox 	{ "Toshiba CD-ROM XM-6202B", NULL,	ATA_HORKAGE_NODMA },
44166919a0a6SAlan Cox 	{ "TOSHIBA CD-ROM XM-1702BC", NULL,	ATA_HORKAGE_NODMA },
44176919a0a6SAlan Cox 	{ "CD-532E-A", 		NULL,		ATA_HORKAGE_NODMA },
44186919a0a6SAlan Cox 	{ "E-IDE CD-ROM CR-840",NULL,		ATA_HORKAGE_NODMA },
44196919a0a6SAlan Cox 	{ "CD-ROM Drive/F5A",	NULL,		ATA_HORKAGE_NODMA },
44206919a0a6SAlan Cox 	{ "WPI CDD-820", 	NULL,		ATA_HORKAGE_NODMA },
44216919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SC-148C", NULL,	ATA_HORKAGE_NODMA },
44226919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SC",	NULL,		ATA_HORKAGE_NODMA },
44236919a0a6SAlan Cox 	{ "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
44246919a0a6SAlan Cox 	{ "_NEC DV5800A", 	NULL,		ATA_HORKAGE_NODMA },
44256919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SN-124", "N001",	ATA_HORKAGE_NODMA },
442639f19886SDave Jones 	{ "Seagate STT20000A", NULL,		ATA_HORKAGE_NODMA },
4427d70e551cSPrarit Bhargava 	{ " 2GB ATA Flash Disk", "ADMA428M",	ATA_HORKAGE_NODMA },
4428b00622fcSShikha Jain 	{ "VRFDFC22048UCHC-TE*", NULL,		ATA_HORKAGE_NODMA },
44293af9a77aSTejun Heo 	/* Odd clown on sil3726/4726 PMPs */
443050af2fa1STejun Heo 	{ "Config  Disk",	NULL,		ATA_HORKAGE_DISABLE },
44316919a0a6SAlan Cox 
443218d6e9d5SAlbert Lee 	/* Weird ATAPI devices */
443340a1d531STejun Heo 	{ "TORiSAN DVD-ROM DRD-N216", NULL,	ATA_HORKAGE_MAX_SEC_128 },
44346a87e42eSTejun Heo 	{ "QUANTUM DAT    DAT72-000", NULL,	ATA_HORKAGE_ATAPI_MOD16_DMA },
4435a32450e1SShan Hai 	{ "Slimtype DVD A  DS8A8SH", NULL,	ATA_HORKAGE_MAX_SEC_LBA48 },
44360523f037SShan Hai 	{ "Slimtype DVD A  DS8A9SH", NULL,	ATA_HORKAGE_MAX_SEC_LBA48 },
443718d6e9d5SAlbert Lee 
4438af34d637SDavid Milburn 	/*
4439af34d637SDavid Milburn 	 * Causes silent data corruption with higher max sects.
4440af34d637SDavid Milburn 	 * http://lkml.kernel.org/g/x49wpy40ysk.fsf@segfault.boston.devel.redhat.com
4441af34d637SDavid Milburn 	 */
4442af34d637SDavid Milburn 	{ "ST380013AS",		"3.20",		ATA_HORKAGE_MAX_SEC_1024 },
4443af34d637SDavid Milburn 
44441488a1e3STejun Heo 	/*
4445e0edc8c5STejun Heo 	 * These devices time out with higher max sects.
44461488a1e3STejun Heo 	 * https://bugzilla.kernel.org/show_bug.cgi?id=121671
44471488a1e3STejun Heo 	 */
4448e0edc8c5STejun Heo 	{ "LITEON CX1-JB*-HP",	NULL,		ATA_HORKAGE_MAX_SEC_1024 },
4449db5ff909SXinyu Lin 	{ "LITEON EP1-*",	NULL,		ATA_HORKAGE_MAX_SEC_1024 },
44501488a1e3STejun Heo 
44516919a0a6SAlan Cox 	/* Devices we expect to fail diagnostics */
44526919a0a6SAlan Cox 
44536919a0a6SAlan Cox 	/* Devices where NCQ should be avoided */
44546919a0a6SAlan Cox 	/* NCQ is slow */
44556919a0a6SAlan Cox 	{ "WDC WD740ADFD-00",	NULL,		ATA_HORKAGE_NONCQ },
4456459ad688STejun Heo 	{ "WDC WD740ADFD-00NLR1", NULL,		ATA_HORKAGE_NONCQ, },
445709125ea6STejun Heo 	/* http://thread.gmane.org/gmane.linux.ide/14907 */
445809125ea6STejun Heo 	{ "FUJITSU MHT2060BH",	NULL,		ATA_HORKAGE_NONCQ },
44597acfaf30SPaul Rolland 	/* NCQ is broken */
4460539cc7c7SJeff Garzik 	{ "Maxtor *",		"BANC*",	ATA_HORKAGE_NONCQ },
44610e3dbc01SAlan Cox 	{ "Maxtor 7V300F0",	"VA111630",	ATA_HORKAGE_NONCQ },
4462da6f0ec2SPaolo Ornati 	{ "ST380817AS",		"3.42",		ATA_HORKAGE_NONCQ },
4463e41bd3e8STejun Heo 	{ "ST3160023AS",	"3.42",		ATA_HORKAGE_NONCQ },
44645ccfca97SLubomir Bulej 	{ "OCZ CORE_SSD",	"02.10104",	ATA_HORKAGE_NONCQ },
4465539cc7c7SJeff Garzik 
4466ac70a964STejun Heo 	/* Seagate NCQ + FLUSH CACHE firmware bug */
44674d1f9082SMark Lord 	{ "ST31500341AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
4468ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4469d10d491fSTejun Heo 
44704d1f9082SMark Lord 	{ "ST31000333AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
4471d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4472d10d491fSTejun Heo 
44734d1f9082SMark Lord 	{ "ST3640[36]23AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
4474d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4475d10d491fSTejun Heo 
44764d1f9082SMark Lord 	{ "ST3320[68]13AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
4477ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4478ac70a964STejun Heo 
447908c85d2aSAleksei Mamlin 	/* drives which fail FPDMA_AA activation (some may freeze afterwards) */
448087809942SMichele Baldessari 	{ "ST1000LM024 HN-M101MBB", "2AR10001",	ATA_HORKAGE_BROKEN_FPDMA_AA },
4481b28a613eSMichele Baldessari 	{ "ST1000LM024 HN-M101MBB", "2BA30001",	ATA_HORKAGE_BROKEN_FPDMA_AA },
448208c85d2aSAleksei Mamlin 	{ "VB0250EAVER",	"HPG7",		ATA_HORKAGE_BROKEN_FPDMA_AA },
448387809942SMichele Baldessari 
448436e337d0SRobert Hancock 	/* Blacklist entries taken from Silicon Image 3124/3132
448536e337d0SRobert Hancock 	   Windows driver .inf file - also several Linux problem reports */
448636e337d0SRobert Hancock 	{ "HTS541060G9SA00",    "MB3OC60D",     ATA_HORKAGE_NONCQ, },
448736e337d0SRobert Hancock 	{ "HTS541080G9SA00",    "MB4OC60D",     ATA_HORKAGE_NONCQ, },
448836e337d0SRobert Hancock 	{ "HTS541010G9SA00",    "MBZOC60D",     ATA_HORKAGE_NONCQ, },
44896919a0a6SAlan Cox 
449068b0ddb2STejun Heo 	/* https://bugzilla.kernel.org/show_bug.cgi?id=15573 */
449168b0ddb2STejun Heo 	{ "C300-CTFDDAC128MAG",	"0001",		ATA_HORKAGE_NONCQ, },
449268b0ddb2STejun Heo 
4493322579dcSTejun Heo 	/* Some Sandisk SSDs lock up hard with NCQ enabled.  Reported on
4494322579dcSTejun Heo 	   SD7SN6S256G and SD8SN8U256G */
4495322579dcSTejun Heo 	{ "SanDisk SD[78]SN*G",	NULL,		ATA_HORKAGE_NONCQ, },
4496322579dcSTejun Heo 
449716c55b03STejun Heo 	/* devices which puke on READ_NATIVE_MAX */
449816c55b03STejun Heo 	{ "HDS724040KLSA80",	"KFAOA20N",	ATA_HORKAGE_BROKEN_HPA, },
449916c55b03STejun Heo 	{ "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
450016c55b03STejun Heo 	{ "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
450116c55b03STejun Heo 	{ "MAXTOR 6L080L4",	"A93.0500",	ATA_HORKAGE_BROKEN_HPA },
45026919a0a6SAlan Cox 
45037831387bSTejun Heo 	/* this one allows HPA unlocking but fails IOs on the area */
45047831387bSTejun Heo 	{ "OCZ-VERTEX",		    "1.30",	ATA_HORKAGE_BROKEN_HPA },
45057831387bSTejun Heo 
450693328e11SAlan Cox 	/* Devices which report 1 sector over size HPA */
450793328e11SAlan Cox 	{ "ST340823A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
450893328e11SAlan Cox 	{ "ST320413A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
4509b152fcd3SMikko Rapeli 	{ "ST310211A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
451093328e11SAlan Cox 
45116bbfd53dSAlan Cox 	/* Devices which get the IVB wrong */
45126bbfd53dSAlan Cox 	{ "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB, },
4513a79067e5SAlan Cox 	/* Maybe we should just blacklist TSSTcorp... */
45147da4c935SMark Lord 	{ "TSSTcorp CDDVDW SH-S202[HJN]", "SB0[01]",  ATA_HORKAGE_IVB, },
45156bbfd53dSAlan Cox 
45169ce8e307SJens Axboe 	/* Devices that do not need bridging limits applied */
45179ce8e307SJens Axboe 	{ "MTRON MSP-SATA*",		NULL,	ATA_HORKAGE_BRIDGE_OK, },
451804d0f1b8SJeff Garzik 	{ "BUFFALO HD-QSU2/R5",		NULL,	ATA_HORKAGE_BRIDGE_OK, },
45199ce8e307SJens Axboe 
45209062712fSTejun Heo 	/* Devices which aren't very happy with higher link speeds */
45219062712fSTejun Heo 	{ "WD My Book",			NULL,	ATA_HORKAGE_1_5_GBPS, },
4522c531077fSDaniel J Blueman 	{ "Seagate FreeAgent GoFlex",	NULL,	ATA_HORKAGE_1_5_GBPS, },
45239062712fSTejun Heo 
4524d0cb43b3STejun Heo 	/*
4525d0cb43b3STejun Heo 	 * Devices which choke on SETXFER.  Applies only if both the
4526d0cb43b3STejun Heo 	 * device and controller are SATA.
4527d0cb43b3STejun Heo 	 */
4528cd691876STejun Heo 	{ "PIONEER DVD-RW  DVRTD08",	NULL,	ATA_HORKAGE_NOSETXFER },
45293a25179eSVladimir LAVALLADE 	{ "PIONEER DVD-RW  DVRTD08A",	NULL,	ATA_HORKAGE_NOSETXFER },
45303a25179eSVladimir LAVALLADE 	{ "PIONEER DVD-RW  DVR-215",	NULL,	ATA_HORKAGE_NOSETXFER },
4531cd691876STejun Heo 	{ "PIONEER DVD-RW  DVR-212D",	NULL,	ATA_HORKAGE_NOSETXFER },
4532cd691876STejun Heo 	{ "PIONEER DVD-RW  DVR-216D",	NULL,	ATA_HORKAGE_NOSETXFER },
4533d0cb43b3STejun Heo 
4534b17e5729SKai-Heng Feng 	/* Crucial BX100 SSD 500GB has broken LPM support */
45353bf7b5d6SHans de Goede 	{ "CT500BX100SSD1",		NULL,	ATA_HORKAGE_NOLPM },
4536b17e5729SKai-Heng Feng 
4537d418ff56SHans de Goede 	/* 512GB MX100 with MU01 firmware has both queued TRIM and LPM issues */
4538d418ff56SHans de Goede 	{ "Crucial_CT512MX100*",	"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
45399c7be59fSHans de Goede 						ATA_HORKAGE_ZERO_AFTER_TRIM |
45409c7be59fSHans de Goede 						ATA_HORKAGE_NOLPM, },
4541d418ff56SHans de Goede 	/* 512GB MX100 with newer firmware has only LPM issues */
4542d418ff56SHans de Goede 	{ "Crucial_CT512MX100*",	NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM |
4543d418ff56SHans de Goede 						ATA_HORKAGE_NOLPM, },
45449c7be59fSHans de Goede 
454562ac3f73SHans de Goede 	/* 480GB+ M500 SSDs have both queued TRIM and LPM issues */
454662ac3f73SHans de Goede 	{ "Crucial_CT480M500*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
454762ac3f73SHans de Goede 						ATA_HORKAGE_ZERO_AFTER_TRIM |
454862ac3f73SHans de Goede 						ATA_HORKAGE_NOLPM, },
454962ac3f73SHans de Goede 	{ "Crucial_CT960M500*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
455062ac3f73SHans de Goede 						ATA_HORKAGE_ZERO_AFTER_TRIM |
455162ac3f73SHans de Goede 						ATA_HORKAGE_NOLPM, },
455262ac3f73SHans de Goede 
455376936e9aSFrançois Cami 	/* These specific Samsung models/firmware-revs do not handle LPM well */
4554b5b4d3a5SHans de Goede 	{ "SAMSUNG MZMPC128HBFU-000MV", "CXM14M1Q", ATA_HORKAGE_NOLPM, },
455576936e9aSFrançois Cami 	{ "SAMSUNG SSD PM830 mSATA *",  "CXM13D1Q", ATA_HORKAGE_NOLPM, },
4556410b5c7bSDiego Viola 	{ "SAMSUNG MZ7TD256HAFV-000L9", NULL,       ATA_HORKAGE_NOLPM, },
4557b5b4d3a5SHans de Goede 
4558f78dea06SMarc Carino 	/* devices that don't properly handle queued TRIM commands */
4559136d769eSSudip Mukherjee 	{ "Micron_M500IT_*",		"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
4560136d769eSSudip Mukherjee 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
4561243918beSMartin K. Petersen 	{ "Micron_M500_*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
4562e61f7d1cSMartin K. Petersen 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
4563ff7f53fbSMartin K. Petersen 	{ "Crucial_CT*M500*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
4564ff7f53fbSMartin K. Petersen 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
45659051bd39SMartin K. Petersen 	{ "Micron_M5[15]0_*",		"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
4566ff7f53fbSMartin K. Petersen 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
4567ff7f53fbSMartin K. Petersen 	{ "Crucial_CT*M550*",		"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
4568ff7f53fbSMartin K. Petersen 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
4569ff7f53fbSMartin K. Petersen 	{ "Crucial_CT*MX100*",		"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
4570ff7f53fbSMartin K. Petersen 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
4571ca6bfcb2SJu Hyung Park 	{ "Samsung SSD 840*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
4572ca6bfcb2SJu Hyung Park 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
4573ca6bfcb2SJu Hyung Park 	{ "Samsung SSD 850*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
45746fc4d97aSMartin K. Petersen 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
45757a7184b0SGuillermo A. Amaral 	{ "FCCT*M500*",			NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
45767a7184b0SGuillermo A. Amaral 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
4577e61f7d1cSMartin K. Petersen 
4578cda57b1bSArne Fitzenreiter 	/* devices that don't properly handle TRIM commands */
4579cda57b1bSArne Fitzenreiter 	{ "SuperSSpeed S238*",		NULL,	ATA_HORKAGE_NOTRIM, },
4580cda57b1bSArne Fitzenreiter 
4581e61f7d1cSMartin K. Petersen 	/*
4582e61f7d1cSMartin K. Petersen 	 * As defined, the DRAT (Deterministic Read After Trim) and RZAT
4583e61f7d1cSMartin K. Petersen 	 * (Return Zero After Trim) flags in the ATA Command Set are
4584e61f7d1cSMartin K. Petersen 	 * unreliable in the sense that they only define what happens if
4585e61f7d1cSMartin K. Petersen 	 * the device successfully executed the DSM TRIM command. TRIM
4586e61f7d1cSMartin K. Petersen 	 * is only advisory, however, and the device is free to silently
4587e61f7d1cSMartin K. Petersen 	 * ignore all or parts of the request.
4588e61f7d1cSMartin K. Petersen 	 *
4589e61f7d1cSMartin K. Petersen 	 * Whitelist drives that are known to reliably return zeroes
4590e61f7d1cSMartin K. Petersen 	 * after TRIM.
4591e61f7d1cSMartin K. Petersen 	 */
4592e61f7d1cSMartin K. Petersen 
4593e61f7d1cSMartin K. Petersen 	/*
4594e61f7d1cSMartin K. Petersen 	 * The intel 510 drive has buggy DRAT/RZAT. Explicitly exclude
4595e61f7d1cSMartin K. Petersen 	 * that model before whitelisting all other intel SSDs.
4596e61f7d1cSMartin K. Petersen 	 */
4597e61f7d1cSMartin K. Petersen 	{ "INTEL*SSDSC2MH*",		NULL,	0, },
4598e61f7d1cSMartin K. Petersen 
4599ff7f53fbSMartin K. Petersen 	{ "Micron*",			NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM, },
4600ff7f53fbSMartin K. Petersen 	{ "Crucial*",			NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM, },
4601e61f7d1cSMartin K. Petersen 	{ "INTEL*SSD*", 		NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM, },
4602e61f7d1cSMartin K. Petersen 	{ "SSD*INTEL*",			NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM, },
4603e61f7d1cSMartin K. Petersen 	{ "Samsung*SSD*",		NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM, },
4604e61f7d1cSMartin K. Petersen 	{ "SAMSUNG*SSD*",		NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM, },
4605e61f7d1cSMartin K. Petersen 	{ "ST[1248][0248]0[FH]*",	NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM, },
4606f78dea06SMarc Carino 
4607ecd75ad5STejun Heo 	/*
4608ecd75ad5STejun Heo 	 * Some WD SATA-I drives spin up and down erratically when the link
4609ecd75ad5STejun Heo 	 * is put into the slumber mode.  We don't have full list of the
4610ecd75ad5STejun Heo 	 * affected devices.  Disable LPM if the device matches one of the
4611ecd75ad5STejun Heo 	 * known prefixes and is SATA-1.  As a side effect LPM partial is
4612ecd75ad5STejun Heo 	 * lost too.
4613ecd75ad5STejun Heo 	 *
4614ecd75ad5STejun Heo 	 * https://bugzilla.kernel.org/show_bug.cgi?id=57211
4615ecd75ad5STejun Heo 	 */
4616ecd75ad5STejun Heo 	{ "WDC WD800JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4617ecd75ad5STejun Heo 	{ "WDC WD1200JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4618ecd75ad5STejun Heo 	{ "WDC WD1600JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4619ecd75ad5STejun Heo 	{ "WDC WD2000JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4620ecd75ad5STejun Heo 	{ "WDC WD2500JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4621ecd75ad5STejun Heo 	{ "WDC WD3000JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4622ecd75ad5STejun Heo 	{ "WDC WD3200JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4623ecd75ad5STejun Heo 
46246919a0a6SAlan Cox 	/* End Marker */
46256919a0a6SAlan Cox 	{ }
4626c6fd2807SJeff Garzik };
4627c6fd2807SJeff Garzik 
462875683fe7STejun Heo static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
4629c6fd2807SJeff Garzik {
46308bfa79fcSTejun Heo 	unsigned char model_num[ATA_ID_PROD_LEN + 1];
46318bfa79fcSTejun Heo 	unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
46326919a0a6SAlan Cox 	const struct ata_blacklist_entry *ad = ata_device_blacklist;
4633c6fd2807SJeff Garzik 
46348bfa79fcSTejun Heo 	ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
46358bfa79fcSTejun Heo 	ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
4636c6fd2807SJeff Garzik 
46376919a0a6SAlan Cox 	while (ad->model_num) {
46381c402799SGeorge Spelvin 		if (glob_match(ad->model_num, model_num)) {
46396919a0a6SAlan Cox 			if (ad->model_rev == NULL)
46406919a0a6SAlan Cox 				return ad->horkage;
46411c402799SGeorge Spelvin 			if (glob_match(ad->model_rev, model_rev))
46426919a0a6SAlan Cox 				return ad->horkage;
4643c6fd2807SJeff Garzik 		}
46446919a0a6SAlan Cox 		ad++;
4645c6fd2807SJeff Garzik 	}
4646c6fd2807SJeff Garzik 	return 0;
4647c6fd2807SJeff Garzik }
4648c6fd2807SJeff Garzik 
46496919a0a6SAlan Cox static int ata_dma_blacklisted(const struct ata_device *dev)
46506919a0a6SAlan Cox {
46516919a0a6SAlan Cox 	/* We don't support polling DMA.
46526919a0a6SAlan Cox 	 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
46536919a0a6SAlan Cox 	 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
46546919a0a6SAlan Cox 	 */
46559af5c9c9STejun Heo 	if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
46566919a0a6SAlan Cox 	    (dev->flags & ATA_DFLAG_CDB_INTR))
46576919a0a6SAlan Cox 		return 1;
465875683fe7STejun Heo 	return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
46596919a0a6SAlan Cox }
46606919a0a6SAlan Cox 
4661c6fd2807SJeff Garzik /**
46626bbfd53dSAlan Cox  *	ata_is_40wire		-	check drive side detection
46636bbfd53dSAlan Cox  *	@dev: device
46646bbfd53dSAlan Cox  *
46656bbfd53dSAlan Cox  *	Perform drive side detection decoding, allowing for device vendors
46666bbfd53dSAlan Cox  *	who can't follow the documentation.
46676bbfd53dSAlan Cox  */
46686bbfd53dSAlan Cox 
46696bbfd53dSAlan Cox static int ata_is_40wire(struct ata_device *dev)
46706bbfd53dSAlan Cox {
46716bbfd53dSAlan Cox 	if (dev->horkage & ATA_HORKAGE_IVB)
46726bbfd53dSAlan Cox 		return ata_drive_40wire_relaxed(dev->id);
46736bbfd53dSAlan Cox 	return ata_drive_40wire(dev->id);
46746bbfd53dSAlan Cox }
46756bbfd53dSAlan Cox 
46766bbfd53dSAlan Cox /**
467715a5551cSAlan Cox  *	cable_is_40wire		-	40/80/SATA decider
467815a5551cSAlan Cox  *	@ap: port to consider
467915a5551cSAlan Cox  *
468015a5551cSAlan Cox  *	This function encapsulates the policy for speed management
468115a5551cSAlan Cox  *	in one place. At the moment we don't cache the result but
468215a5551cSAlan Cox  *	there is a good case for setting ap->cbl to the result when
468315a5551cSAlan Cox  *	we are called with unknown cables (and figuring out if it
468415a5551cSAlan Cox  *	impacts hotplug at all).
468515a5551cSAlan Cox  *
468615a5551cSAlan Cox  *	Return 1 if the cable appears to be 40 wire.
468715a5551cSAlan Cox  */
468815a5551cSAlan Cox 
468915a5551cSAlan Cox static int cable_is_40wire(struct ata_port *ap)
469015a5551cSAlan Cox {
469115a5551cSAlan Cox 	struct ata_link *link;
469215a5551cSAlan Cox 	struct ata_device *dev;
469315a5551cSAlan Cox 
46944a9c7b33STejun Heo 	/* If the controller thinks we are 40 wire, we are. */
469515a5551cSAlan Cox 	if (ap->cbl == ATA_CBL_PATA40)
469615a5551cSAlan Cox 		return 1;
46974a9c7b33STejun Heo 
46984a9c7b33STejun Heo 	/* If the controller thinks we are 80 wire, we are. */
469915a5551cSAlan Cox 	if (ap->cbl == ATA_CBL_PATA80 || ap->cbl == ATA_CBL_SATA)
470015a5551cSAlan Cox 		return 0;
47014a9c7b33STejun Heo 
47024a9c7b33STejun Heo 	/* If the system is known to be 40 wire short cable (eg
47034a9c7b33STejun Heo 	 * laptop), then we allow 80 wire modes even if the drive
47044a9c7b33STejun Heo 	 * isn't sure.
47054a9c7b33STejun Heo 	 */
4706f792068eSAlan Cox 	if (ap->cbl == ATA_CBL_PATA40_SHORT)
4707f792068eSAlan Cox 		return 0;
470815a5551cSAlan Cox 
47094a9c7b33STejun Heo 	/* If the controller doesn't know, we scan.
47104a9c7b33STejun Heo 	 *
47114a9c7b33STejun Heo 	 * Note: We look for all 40 wire detects at this point.  Any
47124a9c7b33STejun Heo 	 *       80 wire detect is taken to be 80 wire cable because
47134a9c7b33STejun Heo 	 * - in many setups only the one drive (slave if present) will
47144a9c7b33STejun Heo 	 *   give a valid detect
47154a9c7b33STejun Heo 	 * - if you have a non detect capable drive you don't want it
47164a9c7b33STejun Heo 	 *   to colour the choice
471715a5551cSAlan Cox 	 */
47181eca4365STejun Heo 	ata_for_each_link(link, ap, EDGE) {
47191eca4365STejun Heo 		ata_for_each_dev(dev, link, ENABLED) {
47201eca4365STejun Heo 			if (!ata_is_40wire(dev))
472115a5551cSAlan Cox 				return 0;
472215a5551cSAlan Cox 		}
472315a5551cSAlan Cox 	}
472415a5551cSAlan Cox 	return 1;
472515a5551cSAlan Cox }
472615a5551cSAlan Cox 
472715a5551cSAlan Cox /**
4728c6fd2807SJeff Garzik  *	ata_dev_xfermask - Compute supported xfermask of the given device
4729c6fd2807SJeff Garzik  *	@dev: Device to compute xfermask for
4730c6fd2807SJeff Garzik  *
4731c6fd2807SJeff Garzik  *	Compute supported xfermask of @dev and store it in
4732c6fd2807SJeff Garzik  *	dev->*_mask.  This function is responsible for applying all
4733c6fd2807SJeff Garzik  *	known limits including host controller limits, device
4734c6fd2807SJeff Garzik  *	blacklist, etc...
4735c6fd2807SJeff Garzik  *
4736c6fd2807SJeff Garzik  *	LOCKING:
4737c6fd2807SJeff Garzik  *	None.
4738c6fd2807SJeff Garzik  */
4739c6fd2807SJeff Garzik static void ata_dev_xfermask(struct ata_device *dev)
4740c6fd2807SJeff Garzik {
47419af5c9c9STejun Heo 	struct ata_link *link = dev->link;
47429af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
4743cca3974eSJeff Garzik 	struct ata_host *host = ap->host;
4744c6fd2807SJeff Garzik 	unsigned long xfer_mask;
4745c6fd2807SJeff Garzik 
4746c6fd2807SJeff Garzik 	/* controller modes available */
4747c6fd2807SJeff Garzik 	xfer_mask = ata_pack_xfermask(ap->pio_mask,
4748c6fd2807SJeff Garzik 				      ap->mwdma_mask, ap->udma_mask);
4749c6fd2807SJeff Garzik 
47508343f889SRobert Hancock 	/* drive modes available */
4751c6fd2807SJeff Garzik 	xfer_mask &= ata_pack_xfermask(dev->pio_mask,
4752c6fd2807SJeff Garzik 				       dev->mwdma_mask, dev->udma_mask);
4753c6fd2807SJeff Garzik 	xfer_mask &= ata_id_xfermask(dev->id);
4754c6fd2807SJeff Garzik 
4755b352e57dSAlan Cox 	/*
4756b352e57dSAlan Cox 	 *	CFA Advanced TrueIDE timings are not allowed on a shared
4757b352e57dSAlan Cox 	 *	cable
4758b352e57dSAlan Cox 	 */
4759b352e57dSAlan Cox 	if (ata_dev_pair(dev)) {
4760b352e57dSAlan Cox 		/* No PIO5 or PIO6 */
4761b352e57dSAlan Cox 		xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
4762b352e57dSAlan Cox 		/* No MWDMA3 or MWDMA 4 */
4763b352e57dSAlan Cox 		xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
4764b352e57dSAlan Cox 	}
4765b352e57dSAlan Cox 
4766c6fd2807SJeff Garzik 	if (ata_dma_blacklisted(dev)) {
4767c6fd2807SJeff Garzik 		xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4768a9a79dfeSJoe Perches 		ata_dev_warn(dev,
4769c6fd2807SJeff Garzik 			     "device is on DMA blacklist, disabling DMA\n");
4770c6fd2807SJeff Garzik 	}
4771c6fd2807SJeff Garzik 
477214d66ab7SPetr Vandrovec 	if ((host->flags & ATA_HOST_SIMPLEX) &&
477314d66ab7SPetr Vandrovec 	    host->simplex_claimed && host->simplex_claimed != ap) {
4774c6fd2807SJeff Garzik 		xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4775a9a79dfeSJoe Perches 		ata_dev_warn(dev,
4776a9a79dfeSJoe Perches 			     "simplex DMA is claimed by other device, disabling DMA\n");
4777c6fd2807SJeff Garzik 	}
4778c6fd2807SJeff Garzik 
4779e424675fSJeff Garzik 	if (ap->flags & ATA_FLAG_NO_IORDY)
4780e424675fSJeff Garzik 		xfer_mask &= ata_pio_mask_no_iordy(dev);
4781e424675fSJeff Garzik 
4782c6fd2807SJeff Garzik 	if (ap->ops->mode_filter)
4783a76b62caSAlan Cox 		xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
4784c6fd2807SJeff Garzik 
47858343f889SRobert Hancock 	/* Apply cable rule here.  Don't apply it early because when
47868343f889SRobert Hancock 	 * we handle hot plug the cable type can itself change.
47878343f889SRobert Hancock 	 * Check this last so that we know if the transfer rate was
47888343f889SRobert Hancock 	 * solely limited by the cable.
47898343f889SRobert Hancock 	 * Unknown or 80 wire cables reported host side are checked
47908343f889SRobert Hancock 	 * drive side as well. Cases where we know a 40wire cable
47918343f889SRobert Hancock 	 * is used safely for 80 are not checked here.
47928343f889SRobert Hancock 	 */
47938343f889SRobert Hancock 	if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
47948343f889SRobert Hancock 		/* UDMA/44 or higher would be available */
479515a5551cSAlan Cox 		if (cable_is_40wire(ap)) {
4796a9a79dfeSJoe Perches 			ata_dev_warn(dev,
47978343f889SRobert Hancock 				     "limited to UDMA/33 due to 40-wire cable\n");
47988343f889SRobert Hancock 			xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
47998343f889SRobert Hancock 		}
48008343f889SRobert Hancock 
4801c6fd2807SJeff Garzik 	ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
4802c6fd2807SJeff Garzik 			    &dev->mwdma_mask, &dev->udma_mask);
4803c6fd2807SJeff Garzik }
4804c6fd2807SJeff Garzik 
4805c6fd2807SJeff Garzik /**
4806c6fd2807SJeff Garzik  *	ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
4807c6fd2807SJeff Garzik  *	@dev: Device to which command will be sent
4808c6fd2807SJeff Garzik  *
4809c6fd2807SJeff Garzik  *	Issue SET FEATURES - XFER MODE command to device @dev
4810c6fd2807SJeff Garzik  *	on port @ap.
4811c6fd2807SJeff Garzik  *
4812c6fd2807SJeff Garzik  *	LOCKING:
4813c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
4814c6fd2807SJeff Garzik  *
4815c6fd2807SJeff Garzik  *	RETURNS:
4816c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
4817c6fd2807SJeff Garzik  */
4818c6fd2807SJeff Garzik 
4819c6fd2807SJeff Garzik static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
4820c6fd2807SJeff Garzik {
4821c6fd2807SJeff Garzik 	struct ata_taskfile tf;
4822c6fd2807SJeff Garzik 	unsigned int err_mask;
4823c6fd2807SJeff Garzik 
4824c6fd2807SJeff Garzik 	/* set up set-features taskfile */
4825c6fd2807SJeff Garzik 	DPRINTK("set features - xfer mode\n");
4826c6fd2807SJeff Garzik 
4827464cf177STejun Heo 	/* Some controllers and ATAPI devices show flaky interrupt
4828464cf177STejun Heo 	 * behavior after setting xfer mode.  Use polling instead.
4829464cf177STejun Heo 	 */
4830c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
4831c6fd2807SJeff Garzik 	tf.command = ATA_CMD_SET_FEATURES;
4832c6fd2807SJeff Garzik 	tf.feature = SETFEATURES_XFER;
4833464cf177STejun Heo 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
4834c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
4835b9f8ab2dSAlan Cox 	/* If we are using IORDY we must send the mode setting command */
4836b9f8ab2dSAlan Cox 	if (ata_pio_need_iordy(dev))
4837c6fd2807SJeff Garzik 		tf.nsect = dev->xfer_mode;
4838b9f8ab2dSAlan Cox 	/* If the device has IORDY and the controller does not - turn it off */
4839b9f8ab2dSAlan Cox  	else if (ata_id_has_iordy(dev->id))
4840b9f8ab2dSAlan Cox 		tf.nsect = 0x01;
4841b9f8ab2dSAlan Cox 	else /* In the ancient relic department - skip all of this */
4842b9f8ab2dSAlan Cox 		return 0;
4843c6fd2807SJeff Garzik 
4844d531be2cSMikulas Patocka 	/* On some disks, this command causes spin-up, so we need longer timeout */
4845d531be2cSMikulas Patocka 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 15000);
4846c6fd2807SJeff Garzik 
4847c6fd2807SJeff Garzik 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
4848c6fd2807SJeff Garzik 	return err_mask;
4849c6fd2807SJeff Garzik }
48501152b261STejun Heo 
4851c6fd2807SJeff Garzik /**
4852218f3d30SJeff Garzik  *	ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES
48539f45cbd3SKristen Carlson Accardi  *	@dev: Device to which command will be sent
48549f45cbd3SKristen Carlson Accardi  *	@enable: Whether to enable or disable the feature
4855218f3d30SJeff Garzik  *	@feature: The sector count represents the feature to set
48569f45cbd3SKristen Carlson Accardi  *
48579f45cbd3SKristen Carlson Accardi  *	Issue SET FEATURES - SATA FEATURES command to device @dev
4858218f3d30SJeff Garzik  *	on port @ap with sector count
48599f45cbd3SKristen Carlson Accardi  *
48609f45cbd3SKristen Carlson Accardi  *	LOCKING:
48619f45cbd3SKristen Carlson Accardi  *	PCI/etc. bus probe sem.
48629f45cbd3SKristen Carlson Accardi  *
48639f45cbd3SKristen Carlson Accardi  *	RETURNS:
48649f45cbd3SKristen Carlson Accardi  *	0 on success, AC_ERR_* mask otherwise.
48659f45cbd3SKristen Carlson Accardi  */
48661152b261STejun Heo unsigned int ata_dev_set_feature(struct ata_device *dev, u8 enable, u8 feature)
48679f45cbd3SKristen Carlson Accardi {
48689f45cbd3SKristen Carlson Accardi 	struct ata_taskfile tf;
48699f45cbd3SKristen Carlson Accardi 	unsigned int err_mask;
4870974e0a45SDamien Le Moal 	unsigned long timeout = 0;
48719f45cbd3SKristen Carlson Accardi 
48729f45cbd3SKristen Carlson Accardi 	/* set up set-features taskfile */
48739f45cbd3SKristen Carlson Accardi 	DPRINTK("set features - SATA features\n");
48749f45cbd3SKristen Carlson Accardi 
48759f45cbd3SKristen Carlson Accardi 	ata_tf_init(dev, &tf);
48769f45cbd3SKristen Carlson Accardi 	tf.command = ATA_CMD_SET_FEATURES;
48779f45cbd3SKristen Carlson Accardi 	tf.feature = enable;
48789f45cbd3SKristen Carlson Accardi 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
48799f45cbd3SKristen Carlson Accardi 	tf.protocol = ATA_PROT_NODATA;
4880218f3d30SJeff Garzik 	tf.nsect = feature;
48819f45cbd3SKristen Carlson Accardi 
4882974e0a45SDamien Le Moal 	if (enable == SETFEATURES_SPINUP)
4883974e0a45SDamien Le Moal 		timeout = ata_probe_timeout ?
4884974e0a45SDamien Le Moal 			  ata_probe_timeout * 1000 : SETFEATURES_SPINUP_TIMEOUT;
4885974e0a45SDamien Le Moal 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, timeout);
48869f45cbd3SKristen Carlson Accardi 
48879f45cbd3SKristen Carlson Accardi 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
48889f45cbd3SKristen Carlson Accardi 	return err_mask;
48899f45cbd3SKristen Carlson Accardi }
4890633de4ccSJeff Garzik EXPORT_SYMBOL_GPL(ata_dev_set_feature);
48919f45cbd3SKristen Carlson Accardi 
48929f45cbd3SKristen Carlson Accardi /**
4893c6fd2807SJeff Garzik  *	ata_dev_init_params - Issue INIT DEV PARAMS command
4894c6fd2807SJeff Garzik  *	@dev: Device to which command will be sent
4895c6fd2807SJeff Garzik  *	@heads: Number of heads (taskfile parameter)
4896c6fd2807SJeff Garzik  *	@sectors: Number of sectors (taskfile parameter)
4897c6fd2807SJeff Garzik  *
4898c6fd2807SJeff Garzik  *	LOCKING:
4899c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
4900c6fd2807SJeff Garzik  *
4901c6fd2807SJeff Garzik  *	RETURNS:
4902c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
4903c6fd2807SJeff Garzik  */
4904c6fd2807SJeff Garzik static unsigned int ata_dev_init_params(struct ata_device *dev,
4905c6fd2807SJeff Garzik 					u16 heads, u16 sectors)
4906c6fd2807SJeff Garzik {
4907c6fd2807SJeff Garzik 	struct ata_taskfile tf;
4908c6fd2807SJeff Garzik 	unsigned int err_mask;
4909c6fd2807SJeff Garzik 
4910c6fd2807SJeff Garzik 	/* Number of sectors per track 1-255. Number of heads 1-16 */
4911c6fd2807SJeff Garzik 	if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
4912c6fd2807SJeff Garzik 		return AC_ERR_INVALID;
4913c6fd2807SJeff Garzik 
4914c6fd2807SJeff Garzik 	/* set up init dev params taskfile */
4915c6fd2807SJeff Garzik 	DPRINTK("init dev params \n");
4916c6fd2807SJeff Garzik 
4917c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
4918c6fd2807SJeff Garzik 	tf.command = ATA_CMD_INIT_DEV_PARAMS;
4919c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4920c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
4921c6fd2807SJeff Garzik 	tf.nsect = sectors;
4922c6fd2807SJeff Garzik 	tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
4923c6fd2807SJeff Garzik 
49242b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
492518b2466cSAlan Cox 	/* A clean abort indicates an original or just out of spec drive
492618b2466cSAlan Cox 	   and we should continue as we issue the setup based on the
492718b2466cSAlan Cox 	   drive reported working geometry */
492818b2466cSAlan Cox 	if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
492918b2466cSAlan Cox 		err_mask = 0;
4930c6fd2807SJeff Garzik 
4931c6fd2807SJeff Garzik 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
4932c6fd2807SJeff Garzik 	return err_mask;
4933c6fd2807SJeff Garzik }
4934c6fd2807SJeff Garzik 
4935c6fd2807SJeff Garzik /**
49365895ef9aSTejun Heo  *	atapi_check_dma - Check whether ATAPI DMA can be supported
4937c6fd2807SJeff Garzik  *	@qc: Metadata associated with taskfile to check
4938c6fd2807SJeff Garzik  *
4939c6fd2807SJeff Garzik  *	Allow low-level driver to filter ATA PACKET commands, returning
4940c6fd2807SJeff Garzik  *	a status indicating whether or not it is OK to use DMA for the
4941c6fd2807SJeff Garzik  *	supplied PACKET command.
4942c6fd2807SJeff Garzik  *
4943c6fd2807SJeff Garzik  *	LOCKING:
4944cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4945c6fd2807SJeff Garzik  *
4946c6fd2807SJeff Garzik  *	RETURNS: 0 when ATAPI DMA can be used
4947c6fd2807SJeff Garzik  *               nonzero otherwise
4948c6fd2807SJeff Garzik  */
49495895ef9aSTejun Heo int atapi_check_dma(struct ata_queued_cmd *qc)
4950c6fd2807SJeff Garzik {
4951c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4952c6fd2807SJeff Garzik 
4953b9a4197eSTejun Heo 	/* Don't allow DMA if it isn't multiple of 16 bytes.  Quite a
4954b9a4197eSTejun Heo 	 * few ATAPI devices choke on such DMA requests.
4955b9a4197eSTejun Heo 	 */
49566a87e42eSTejun Heo 	if (!(qc->dev->horkage & ATA_HORKAGE_ATAPI_MOD16_DMA) &&
49576a87e42eSTejun Heo 	    unlikely(qc->nbytes & 15))
49586f23a31dSAlbert Lee 		return 1;
49596f23a31dSAlbert Lee 
4960c6fd2807SJeff Garzik 	if (ap->ops->check_atapi_dma)
4961b9a4197eSTejun Heo 		return ap->ops->check_atapi_dma(qc);
4962c6fd2807SJeff Garzik 
4963b9a4197eSTejun Heo 	return 0;
4964c6fd2807SJeff Garzik }
4965b9a4197eSTejun Heo 
4966c6fd2807SJeff Garzik /**
496731cc23b3STejun Heo  *	ata_std_qc_defer - Check whether a qc needs to be deferred
496831cc23b3STejun Heo  *	@qc: ATA command in question
496931cc23b3STejun Heo  *
497031cc23b3STejun Heo  *	Non-NCQ commands cannot run with any other command, NCQ or
497131cc23b3STejun Heo  *	not.  As upper layer only knows the queue depth, we are
497231cc23b3STejun Heo  *	responsible for maintaining exclusion.  This function checks
497331cc23b3STejun Heo  *	whether a new command @qc can be issued.
497431cc23b3STejun Heo  *
497531cc23b3STejun Heo  *	LOCKING:
497631cc23b3STejun Heo  *	spin_lock_irqsave(host lock)
497731cc23b3STejun Heo  *
497831cc23b3STejun Heo  *	RETURNS:
497931cc23b3STejun Heo  *	ATA_DEFER_* if deferring is needed, 0 otherwise.
498031cc23b3STejun Heo  */
498131cc23b3STejun Heo int ata_std_qc_defer(struct ata_queued_cmd *qc)
498231cc23b3STejun Heo {
498331cc23b3STejun Heo 	struct ata_link *link = qc->dev->link;
498431cc23b3STejun Heo 
4985179b310aSHannes Reinecke 	if (ata_is_ncq(qc->tf.protocol)) {
498631cc23b3STejun Heo 		if (!ata_tag_valid(link->active_tag))
498731cc23b3STejun Heo 			return 0;
498831cc23b3STejun Heo 	} else {
498931cc23b3STejun Heo 		if (!ata_tag_valid(link->active_tag) && !link->sactive)
499031cc23b3STejun Heo 			return 0;
499131cc23b3STejun Heo 	}
499231cc23b3STejun Heo 
499331cc23b3STejun Heo 	return ATA_DEFER_LINK;
499431cc23b3STejun Heo }
499531cc23b3STejun Heo 
4996c6fd2807SJeff Garzik void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
4997c6fd2807SJeff Garzik 
4998c6fd2807SJeff Garzik /**
4999c6fd2807SJeff Garzik  *	ata_sg_init - Associate command with scatter-gather table.
5000c6fd2807SJeff Garzik  *	@qc: Command to be associated
5001c6fd2807SJeff Garzik  *	@sg: Scatter-gather table.
5002c6fd2807SJeff Garzik  *	@n_elem: Number of elements in s/g table.
5003c6fd2807SJeff Garzik  *
5004c6fd2807SJeff Garzik  *	Initialize the data-related elements of queued_cmd @qc
5005c6fd2807SJeff Garzik  *	to point to a scatter-gather table @sg, containing @n_elem
5006c6fd2807SJeff Garzik  *	elements.
5007c6fd2807SJeff Garzik  *
5008c6fd2807SJeff Garzik  *	LOCKING:
5009cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
5010c6fd2807SJeff Garzik  */
5011c6fd2807SJeff Garzik void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
5012c6fd2807SJeff Garzik 		 unsigned int n_elem)
5013c6fd2807SJeff Garzik {
5014ff2aeb1eSTejun Heo 	qc->sg = sg;
5015c6fd2807SJeff Garzik 	qc->n_elem = n_elem;
5016ff2aeb1eSTejun Heo 	qc->cursg = qc->sg;
5017ff2aeb1eSTejun Heo }
5018ff2aeb1eSTejun Heo 
50192874d5eeSGeert Uytterhoeven #ifdef CONFIG_HAS_DMA
50202874d5eeSGeert Uytterhoeven 
50212874d5eeSGeert Uytterhoeven /**
50222874d5eeSGeert Uytterhoeven  *	ata_sg_clean - Unmap DMA memory associated with command
50232874d5eeSGeert Uytterhoeven  *	@qc: Command containing DMA memory to be released
50242874d5eeSGeert Uytterhoeven  *
50252874d5eeSGeert Uytterhoeven  *	Unmap all mapped DMA memory associated with this command.
50262874d5eeSGeert Uytterhoeven  *
50272874d5eeSGeert Uytterhoeven  *	LOCKING:
50282874d5eeSGeert Uytterhoeven  *	spin_lock_irqsave(host lock)
50292874d5eeSGeert Uytterhoeven  */
5030af27e01cSJason Yan static void ata_sg_clean(struct ata_queued_cmd *qc)
50312874d5eeSGeert Uytterhoeven {
50322874d5eeSGeert Uytterhoeven 	struct ata_port *ap = qc->ap;
50332874d5eeSGeert Uytterhoeven 	struct scatterlist *sg = qc->sg;
50342874d5eeSGeert Uytterhoeven 	int dir = qc->dma_dir;
50352874d5eeSGeert Uytterhoeven 
50362874d5eeSGeert Uytterhoeven 	WARN_ON_ONCE(sg == NULL);
50372874d5eeSGeert Uytterhoeven 
50382874d5eeSGeert Uytterhoeven 	VPRINTK("unmapping %u sg elements\n", qc->n_elem);
50392874d5eeSGeert Uytterhoeven 
50402874d5eeSGeert Uytterhoeven 	if (qc->n_elem)
50412874d5eeSGeert Uytterhoeven 		dma_unmap_sg(ap->dev, sg, qc->orig_n_elem, dir);
50422874d5eeSGeert Uytterhoeven 
50432874d5eeSGeert Uytterhoeven 	qc->flags &= ~ATA_QCFLAG_DMAMAP;
50442874d5eeSGeert Uytterhoeven 	qc->sg = NULL;
50452874d5eeSGeert Uytterhoeven }
50462874d5eeSGeert Uytterhoeven 
5047c6fd2807SJeff Garzik /**
5048c6fd2807SJeff Garzik  *	ata_sg_setup - DMA-map the scatter-gather table associated with a command.
5049c6fd2807SJeff Garzik  *	@qc: Command with scatter-gather table to be mapped.
5050c6fd2807SJeff Garzik  *
5051c6fd2807SJeff Garzik  *	DMA-map the scatter-gather table associated with queued_cmd @qc.
5052c6fd2807SJeff Garzik  *
5053c6fd2807SJeff Garzik  *	LOCKING:
5054cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
5055c6fd2807SJeff Garzik  *
5056c6fd2807SJeff Garzik  *	RETURNS:
5057c6fd2807SJeff Garzik  *	Zero on success, negative on error.
5058c6fd2807SJeff Garzik  *
5059c6fd2807SJeff Garzik  */
5060c6fd2807SJeff Garzik static int ata_sg_setup(struct ata_queued_cmd *qc)
5061c6fd2807SJeff Garzik {
5062c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
5063dde20207SJames Bottomley 	unsigned int n_elem;
5064c6fd2807SJeff Garzik 
506544877b4eSTejun Heo 	VPRINTK("ENTER, ata%u\n", ap->print_id);
5066c6fd2807SJeff Garzik 
5067dde20207SJames Bottomley 	n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir);
5068dde20207SJames Bottomley 	if (n_elem < 1)
5069c6fd2807SJeff Garzik 		return -1;
5070c6fd2807SJeff Garzik 
5071bb376673SPaul Menzel 	VPRINTK("%d sg elements mapped\n", n_elem);
50725825627cSFUJITA Tomonori 	qc->orig_n_elem = qc->n_elem;
5073dde20207SJames Bottomley 	qc->n_elem = n_elem;
5074f92a2636STejun Heo 	qc->flags |= ATA_QCFLAG_DMAMAP;
5075c6fd2807SJeff Garzik 
5076c6fd2807SJeff Garzik 	return 0;
5077c6fd2807SJeff Garzik }
5078c6fd2807SJeff Garzik 
50792874d5eeSGeert Uytterhoeven #else /* !CONFIG_HAS_DMA */
50802874d5eeSGeert Uytterhoeven 
50812874d5eeSGeert Uytterhoeven static inline void ata_sg_clean(struct ata_queued_cmd *qc) {}
50822874d5eeSGeert Uytterhoeven static inline int ata_sg_setup(struct ata_queued_cmd *qc) { return -1; }
50832874d5eeSGeert Uytterhoeven 
50842874d5eeSGeert Uytterhoeven #endif /* !CONFIG_HAS_DMA */
50852874d5eeSGeert Uytterhoeven 
5086c6fd2807SJeff Garzik /**
5087c6fd2807SJeff Garzik  *	swap_buf_le16 - swap halves of 16-bit words in place
5088c6fd2807SJeff Garzik  *	@buf:  Buffer to swap
5089c6fd2807SJeff Garzik  *	@buf_words:  Number of 16-bit words in buffer.
5090c6fd2807SJeff Garzik  *
5091c6fd2807SJeff Garzik  *	Swap halves of 16-bit words if needed to convert from
5092c6fd2807SJeff Garzik  *	little-endian byte order to native cpu byte order, or
5093c6fd2807SJeff Garzik  *	vice-versa.
5094c6fd2807SJeff Garzik  *
5095c6fd2807SJeff Garzik  *	LOCKING:
5096c6fd2807SJeff Garzik  *	Inherited from caller.
5097c6fd2807SJeff Garzik  */
5098c6fd2807SJeff Garzik void swap_buf_le16(u16 *buf, unsigned int buf_words)
5099c6fd2807SJeff Garzik {
5100c6fd2807SJeff Garzik #ifdef __BIG_ENDIAN
5101c6fd2807SJeff Garzik 	unsigned int i;
5102c6fd2807SJeff Garzik 
5103c6fd2807SJeff Garzik 	for (i = 0; i < buf_words; i++)
5104c6fd2807SJeff Garzik 		buf[i] = le16_to_cpu(buf[i]);
5105c6fd2807SJeff Garzik #endif /* __BIG_ENDIAN */
5106c6fd2807SJeff Garzik }
5107c6fd2807SJeff Garzik 
5108c6fd2807SJeff Garzik /**
5109c6fd2807SJeff Garzik  *	ata_qc_new_init - Request an available ATA command, and initialize it
5110c6fd2807SJeff Garzik  *	@dev: Device from whom we request an available command structure
511138755e89SRandy Dunlap  *	@tag: tag
5112c6fd2807SJeff Garzik  *
5113c6fd2807SJeff Garzik  *	LOCKING:
5114c6fd2807SJeff Garzik  *	None.
5115c6fd2807SJeff Garzik  */
5116c6fd2807SJeff Garzik 
511798bd4be1SShaohua Li struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev, int tag)
5118c6fd2807SJeff Garzik {
51199af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
5120c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
5121c6fd2807SJeff Garzik 
512298bd4be1SShaohua Li 	/* no command while frozen */
512398bd4be1SShaohua Li 	if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
512498bd4be1SShaohua Li 		return NULL;
512598bd4be1SShaohua Li 
512698bd4be1SShaohua Li 	/* libsas case */
51275067c046SShaohua Li 	if (ap->flags & ATA_FLAG_SAS_HOST) {
512898bd4be1SShaohua Li 		tag = ata_sas_allocate_tag(ap);
512998bd4be1SShaohua Li 		if (tag < 0)
513098bd4be1SShaohua Li 			return NULL;
513198bd4be1SShaohua Li 	}
513298bd4be1SShaohua Li 
513398bd4be1SShaohua Li 	qc = __ata_qc_from_tag(ap, tag);
51345ac40790SJens Axboe 	qc->tag = qc->hw_tag = tag;
5135c6fd2807SJeff Garzik 	qc->scsicmd = NULL;
5136c6fd2807SJeff Garzik 	qc->ap = ap;
5137c6fd2807SJeff Garzik 	qc->dev = dev;
5138c6fd2807SJeff Garzik 
5139c6fd2807SJeff Garzik 	ata_qc_reinit(qc);
5140c6fd2807SJeff Garzik 
5141c6fd2807SJeff Garzik 	return qc;
5142c6fd2807SJeff Garzik }
5143c6fd2807SJeff Garzik 
51448a8bc223STejun Heo /**
51458a8bc223STejun Heo  *	ata_qc_free - free unused ata_queued_cmd
51468a8bc223STejun Heo  *	@qc: Command to complete
51478a8bc223STejun Heo  *
51488a8bc223STejun Heo  *	Designed to free unused ata_queued_cmd object
51498a8bc223STejun Heo  *	in case something prevents using it.
51508a8bc223STejun Heo  *
51518a8bc223STejun Heo  *	LOCKING:
51528a8bc223STejun Heo  *	spin_lock_irqsave(host lock)
51538a8bc223STejun Heo  */
51548a8bc223STejun Heo void ata_qc_free(struct ata_queued_cmd *qc)
51558a8bc223STejun Heo {
5156a1104016SJulia Lawall 	struct ata_port *ap;
51578a8bc223STejun Heo 	unsigned int tag;
51588a8bc223STejun Heo 
5159efcb3cf7STejun Heo 	WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
5160a1104016SJulia Lawall 	ap = qc->ap;
51618a8bc223STejun Heo 
51628a8bc223STejun Heo 	qc->flags = 0;
51638a8bc223STejun Heo 	tag = qc->tag;
516428361c40SJens Axboe 	if (ata_tag_valid(tag)) {
51658a8bc223STejun Heo 		qc->tag = ATA_TAG_POISON;
51665067c046SShaohua Li 		if (ap->flags & ATA_FLAG_SAS_HOST)
516798bd4be1SShaohua Li 			ata_sas_free_tag(tag, ap);
51688a8bc223STejun Heo 	}
51698a8bc223STejun Heo }
51708a8bc223STejun Heo 
5171c6fd2807SJeff Garzik void __ata_qc_complete(struct ata_queued_cmd *qc)
5172c6fd2807SJeff Garzik {
5173a1104016SJulia Lawall 	struct ata_port *ap;
5174a1104016SJulia Lawall 	struct ata_link *link;
5175c6fd2807SJeff Garzik 
5176efcb3cf7STejun Heo 	WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
5177efcb3cf7STejun Heo 	WARN_ON_ONCE(!(qc->flags & ATA_QCFLAG_ACTIVE));
5178a1104016SJulia Lawall 	ap = qc->ap;
5179a1104016SJulia Lawall 	link = qc->dev->link;
5180c6fd2807SJeff Garzik 
5181c6fd2807SJeff Garzik 	if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
5182c6fd2807SJeff Garzik 		ata_sg_clean(qc);
5183c6fd2807SJeff Garzik 
5184c6fd2807SJeff Garzik 	/* command should be marked inactive atomically with qc completion */
5185179b310aSHannes Reinecke 	if (ata_is_ncq(qc->tf.protocol)) {
51864e5b6260SJens Axboe 		link->sactive &= ~(1 << qc->hw_tag);
5187da917d69STejun Heo 		if (!link->sactive)
5188da917d69STejun Heo 			ap->nr_active_links--;
5189da917d69STejun Heo 	} else {
51909af5c9c9STejun Heo 		link->active_tag = ATA_TAG_POISON;
5191da917d69STejun Heo 		ap->nr_active_links--;
5192da917d69STejun Heo 	}
5193da917d69STejun Heo 
5194da917d69STejun Heo 	/* clear exclusive status */
5195da917d69STejun Heo 	if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
5196da917d69STejun Heo 		     ap->excl_link == link))
5197da917d69STejun Heo 		ap->excl_link = NULL;
5198c6fd2807SJeff Garzik 
5199c6fd2807SJeff Garzik 	/* atapi: mark qc as inactive to prevent the interrupt handler
5200c6fd2807SJeff Garzik 	 * from completing the command twice later, before the error handler
5201c6fd2807SJeff Garzik 	 * is called. (when rc != 0 and atapi request sense is needed)
5202c6fd2807SJeff Garzik 	 */
5203c6fd2807SJeff Garzik 	qc->flags &= ~ATA_QCFLAG_ACTIVE;
5204e3ed8939SJens Axboe 	ap->qc_active &= ~(1ULL << qc->tag);
5205c6fd2807SJeff Garzik 
5206c6fd2807SJeff Garzik 	/* call completion callback */
5207c6fd2807SJeff Garzik 	qc->complete_fn(qc);
5208c6fd2807SJeff Garzik }
5209c6fd2807SJeff Garzik 
521039599a53STejun Heo static void fill_result_tf(struct ata_queued_cmd *qc)
521139599a53STejun Heo {
521239599a53STejun Heo 	struct ata_port *ap = qc->ap;
521339599a53STejun Heo 
521439599a53STejun Heo 	qc->result_tf.flags = qc->tf.flags;
521522183bf5STejun Heo 	ap->ops->qc_fill_rtf(qc);
521639599a53STejun Heo }
521739599a53STejun Heo 
521800115e0fSTejun Heo static void ata_verify_xfer(struct ata_queued_cmd *qc)
521900115e0fSTejun Heo {
522000115e0fSTejun Heo 	struct ata_device *dev = qc->dev;
522100115e0fSTejun Heo 
5222eb0effdfSChristoph Hellwig 	if (!ata_is_data(qc->tf.protocol))
522300115e0fSTejun Heo 		return;
522400115e0fSTejun Heo 
522500115e0fSTejun Heo 	if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
522600115e0fSTejun Heo 		return;
522700115e0fSTejun Heo 
522800115e0fSTejun Heo 	dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
522900115e0fSTejun Heo }
523000115e0fSTejun Heo 
5231c6fd2807SJeff Garzik /**
5232c6fd2807SJeff Garzik  *	ata_qc_complete - Complete an active ATA command
5233c6fd2807SJeff Garzik  *	@qc: Command to complete
5234c6fd2807SJeff Garzik  *
52351aadf5c3STejun Heo  *	Indicate to the mid and upper layers that an ATA command has
52361aadf5c3STejun Heo  *	completed, with either an ok or not-ok status.
52371aadf5c3STejun Heo  *
52381aadf5c3STejun Heo  *	Refrain from calling this function multiple times when
52391aadf5c3STejun Heo  *	successfully completing multiple NCQ commands.
52401aadf5c3STejun Heo  *	ata_qc_complete_multiple() should be used instead, which will
52411aadf5c3STejun Heo  *	properly update IRQ expect state.
5242c6fd2807SJeff Garzik  *
5243c6fd2807SJeff Garzik  *	LOCKING:
5244cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
5245c6fd2807SJeff Garzik  */
5246c6fd2807SJeff Garzik void ata_qc_complete(struct ata_queued_cmd *qc)
5247c6fd2807SJeff Garzik {
5248c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
5249c6fd2807SJeff Garzik 
5250eb25cb99SStephan Linz 	/* Trigger the LED (if available) */
5251d1ed7c55SLinus Walleij 	ledtrig_disk_activity(!!(qc->tf.flags & ATA_TFLAG_WRITE));
5252eb25cb99SStephan Linz 
5253c6fd2807SJeff Garzik 	/* XXX: New EH and old EH use different mechanisms to
5254c6fd2807SJeff Garzik 	 * synchronize EH with regular execution path.
5255c6fd2807SJeff Garzik 	 *
5256c6fd2807SJeff Garzik 	 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
5257c6fd2807SJeff Garzik 	 * Normal execution path is responsible for not accessing a
5258c6fd2807SJeff Garzik 	 * failed qc.  libata core enforces the rule by returning NULL
5259c6fd2807SJeff Garzik 	 * from ata_qc_from_tag() for failed qcs.
5260c6fd2807SJeff Garzik 	 *
5261c6fd2807SJeff Garzik 	 * Old EH depends on ata_qc_complete() nullifying completion
5262c6fd2807SJeff Garzik 	 * requests if ATA_QCFLAG_EH_SCHEDULED is set.  Old EH does
5263c6fd2807SJeff Garzik 	 * not synchronize with interrupt handler.  Only PIO task is
5264c6fd2807SJeff Garzik 	 * taken care of.
5265c6fd2807SJeff Garzik 	 */
5266c6fd2807SJeff Garzik 	if (ap->ops->error_handler) {
52674dbfa39bSTejun Heo 		struct ata_device *dev = qc->dev;
52684dbfa39bSTejun Heo 		struct ata_eh_info *ehi = &dev->link->eh_info;
52694dbfa39bSTejun Heo 
5270c6fd2807SJeff Garzik 		if (unlikely(qc->err_mask))
5271c6fd2807SJeff Garzik 			qc->flags |= ATA_QCFLAG_FAILED;
5272c6fd2807SJeff Garzik 
5273f08dc1acSTejun Heo 		/*
5274f08dc1acSTejun Heo 		 * Finish internal commands without any further processing
5275f08dc1acSTejun Heo 		 * and always with the result TF filled.
5276f08dc1acSTejun Heo 		 */
5277f08dc1acSTejun Heo 		if (unlikely(ata_tag_internal(qc->tag))) {
527839599a53STejun Heo 			fill_result_tf(qc);
5279255c03d1SHannes Reinecke 			trace_ata_qc_complete_internal(qc);
5280f4b31db9STejun Heo 			__ata_qc_complete(qc);
5281c6fd2807SJeff Garzik 			return;
5282c6fd2807SJeff Garzik 		}
5283c6fd2807SJeff Garzik 
5284f08dc1acSTejun Heo 		/*
5285f08dc1acSTejun Heo 		 * Non-internal qc has failed.  Fill the result TF and
5286f08dc1acSTejun Heo 		 * summon EH.
5287f08dc1acSTejun Heo 		 */
5288f08dc1acSTejun Heo 		if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
5289f08dc1acSTejun Heo 			fill_result_tf(qc);
5290255c03d1SHannes Reinecke 			trace_ata_qc_complete_failed(qc);
5291f08dc1acSTejun Heo 			ata_qc_schedule_eh(qc);
5292f08dc1acSTejun Heo 			return;
5293f08dc1acSTejun Heo 		}
5294f08dc1acSTejun Heo 
52954dc738edSTejun Heo 		WARN_ON_ONCE(ap->pflags & ATA_PFLAG_FROZEN);
52964dc738edSTejun Heo 
5297c6fd2807SJeff Garzik 		/* read result TF if requested */
5298c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_RESULT_TF)
529939599a53STejun Heo 			fill_result_tf(qc);
5300c6fd2807SJeff Garzik 
5301255c03d1SHannes Reinecke 		trace_ata_qc_complete_done(qc);
53024dbfa39bSTejun Heo 		/* Some commands need post-processing after successful
53034dbfa39bSTejun Heo 		 * completion.
53044dbfa39bSTejun Heo 		 */
53054dbfa39bSTejun Heo 		switch (qc->tf.command) {
53064dbfa39bSTejun Heo 		case ATA_CMD_SET_FEATURES:
53074dbfa39bSTejun Heo 			if (qc->tf.feature != SETFEATURES_WC_ON &&
53080c12735eSTom Yan 			    qc->tf.feature != SETFEATURES_WC_OFF &&
53090c12735eSTom Yan 			    qc->tf.feature != SETFEATURES_RA_ON &&
53100c12735eSTom Yan 			    qc->tf.feature != SETFEATURES_RA_OFF)
53114dbfa39bSTejun Heo 				break;
53124dbfa39bSTejun Heo 			/* fall through */
53134dbfa39bSTejun Heo 		case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
53144dbfa39bSTejun Heo 		case ATA_CMD_SET_MULTI: /* multi_count changed */
53154dbfa39bSTejun Heo 			/* revalidate device */
53164dbfa39bSTejun Heo 			ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE;
53174dbfa39bSTejun Heo 			ata_port_schedule_eh(ap);
53184dbfa39bSTejun Heo 			break;
5319054a5fbaSTejun Heo 
5320054a5fbaSTejun Heo 		case ATA_CMD_SLEEP:
5321054a5fbaSTejun Heo 			dev->flags |= ATA_DFLAG_SLEEPING;
5322054a5fbaSTejun Heo 			break;
53234dbfa39bSTejun Heo 		}
53244dbfa39bSTejun Heo 
532500115e0fSTejun Heo 		if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
532600115e0fSTejun Heo 			ata_verify_xfer(qc);
532700115e0fSTejun Heo 
5328c6fd2807SJeff Garzik 		__ata_qc_complete(qc);
5329c6fd2807SJeff Garzik 	} else {
5330c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
5331c6fd2807SJeff Garzik 			return;
5332c6fd2807SJeff Garzik 
5333c6fd2807SJeff Garzik 		/* read result TF if failed or requested */
5334c6fd2807SJeff Garzik 		if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
533539599a53STejun Heo 			fill_result_tf(qc);
5336c6fd2807SJeff Garzik 
5337c6fd2807SJeff Garzik 		__ata_qc_complete(qc);
5338c6fd2807SJeff Garzik 	}
5339c6fd2807SJeff Garzik }
5340c6fd2807SJeff Garzik 
5341c6fd2807SJeff Garzik /**
5342c6fd2807SJeff Garzik  *	ata_qc_complete_multiple - Complete multiple qcs successfully
5343c6fd2807SJeff Garzik  *	@ap: port in question
5344c6fd2807SJeff Garzik  *	@qc_active: new qc_active mask
5345c6fd2807SJeff Garzik  *
5346c6fd2807SJeff Garzik  *	Complete in-flight commands.  This functions is meant to be
5347c6fd2807SJeff Garzik  *	called from low-level driver's interrupt routine to complete
5348c6fd2807SJeff Garzik  *	requests normally.  ap->qc_active and @qc_active is compared
5349c6fd2807SJeff Garzik  *	and commands are completed accordingly.
5350c6fd2807SJeff Garzik  *
53511aadf5c3STejun Heo  *	Always use this function when completing multiple NCQ commands
53521aadf5c3STejun Heo  *	from IRQ handlers instead of calling ata_qc_complete()
53531aadf5c3STejun Heo  *	multiple times to keep IRQ expect status properly in sync.
53541aadf5c3STejun Heo  *
5355c6fd2807SJeff Garzik  *	LOCKING:
5356cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
5357c6fd2807SJeff Garzik  *
5358c6fd2807SJeff Garzik  *	RETURNS:
5359c6fd2807SJeff Garzik  *	Number of completed commands on success, -errno otherwise.
5360c6fd2807SJeff Garzik  */
5361e3ed8939SJens Axboe int ata_qc_complete_multiple(struct ata_port *ap, u64 qc_active)
5362c6fd2807SJeff Garzik {
53637ce5c8cdSJens Axboe 	u64 done_mask, ap_qc_active = ap->qc_active;
5364c6fd2807SJeff Garzik 	int nr_done = 0;
5365c6fd2807SJeff Garzik 
53667ce5c8cdSJens Axboe 	/*
53677ce5c8cdSJens Axboe 	 * If the internal tag is set on ap->qc_active, then we care about
53687ce5c8cdSJens Axboe 	 * bit0 on the passed in qc_active mask. Move that bit up to match
53697ce5c8cdSJens Axboe 	 * the internal tag.
53707ce5c8cdSJens Axboe 	 */
53717ce5c8cdSJens Axboe 	if (ap_qc_active & (1ULL << ATA_TAG_INTERNAL)) {
53727ce5c8cdSJens Axboe 		qc_active |= (qc_active & 0x01) << ATA_TAG_INTERNAL;
53737ce5c8cdSJens Axboe 		qc_active ^= qc_active & 0x01;
53747ce5c8cdSJens Axboe 	}
53757ce5c8cdSJens Axboe 
53767ce5c8cdSJens Axboe 	done_mask = ap_qc_active ^ qc_active;
5377c6fd2807SJeff Garzik 
5378c6fd2807SJeff Garzik 	if (unlikely(done_mask & qc_active)) {
5379e3ed8939SJens Axboe 		ata_port_err(ap, "illegal qc_active transition (%08llx->%08llx)\n",
5380a9a79dfeSJoe Perches 			     ap->qc_active, qc_active);
5381c6fd2807SJeff Garzik 		return -EINVAL;
5382c6fd2807SJeff Garzik 	}
5383c6fd2807SJeff Garzik 
538443768180SJens Axboe 	while (done_mask) {
5385c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc;
5386e3ed8939SJens Axboe 		unsigned int tag = __ffs64(done_mask);
5387c6fd2807SJeff Garzik 
538843768180SJens Axboe 		qc = ata_qc_from_tag(ap, tag);
538943768180SJens Axboe 		if (qc) {
5390c6fd2807SJeff Garzik 			ata_qc_complete(qc);
5391c6fd2807SJeff Garzik 			nr_done++;
5392c6fd2807SJeff Garzik 		}
5393e3ed8939SJens Axboe 		done_mask &= ~(1ULL << tag);
5394c6fd2807SJeff Garzik 	}
5395c6fd2807SJeff Garzik 
5396c6fd2807SJeff Garzik 	return nr_done;
5397c6fd2807SJeff Garzik }
5398c6fd2807SJeff Garzik 
5399c6fd2807SJeff Garzik /**
5400c6fd2807SJeff Garzik  *	ata_qc_issue - issue taskfile to device
5401c6fd2807SJeff Garzik  *	@qc: command to issue to device
5402c6fd2807SJeff Garzik  *
5403c6fd2807SJeff Garzik  *	Prepare an ATA command to submission to device.
5404c6fd2807SJeff Garzik  *	This includes mapping the data into a DMA-able
5405c6fd2807SJeff Garzik  *	area, filling in the S/G table, and finally
5406c6fd2807SJeff Garzik  *	writing the taskfile to hardware, starting the command.
5407c6fd2807SJeff Garzik  *
5408c6fd2807SJeff Garzik  *	LOCKING:
5409cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
5410c6fd2807SJeff Garzik  */
5411c6fd2807SJeff Garzik void ata_qc_issue(struct ata_queued_cmd *qc)
5412c6fd2807SJeff Garzik {
5413c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
54149af5c9c9STejun Heo 	struct ata_link *link = qc->dev->link;
5415405e66b3STejun Heo 	u8 prot = qc->tf.protocol;
5416c6fd2807SJeff Garzik 
5417c6fd2807SJeff Garzik 	/* Make sure only one non-NCQ command is outstanding.  The
5418c6fd2807SJeff Garzik 	 * check is skipped for old EH because it reuses active qc to
5419c6fd2807SJeff Garzik 	 * request ATAPI sense.
5420c6fd2807SJeff Garzik 	 */
5421efcb3cf7STejun Heo 	WARN_ON_ONCE(ap->ops->error_handler && ata_tag_valid(link->active_tag));
5422c6fd2807SJeff Garzik 
54231973a023STejun Heo 	if (ata_is_ncq(prot)) {
54244e5b6260SJens Axboe 		WARN_ON_ONCE(link->sactive & (1 << qc->hw_tag));
5425da917d69STejun Heo 
5426da917d69STejun Heo 		if (!link->sactive)
5427da917d69STejun Heo 			ap->nr_active_links++;
54284e5b6260SJens Axboe 		link->sactive |= 1 << qc->hw_tag;
5429c6fd2807SJeff Garzik 	} else {
5430efcb3cf7STejun Heo 		WARN_ON_ONCE(link->sactive);
5431da917d69STejun Heo 
5432da917d69STejun Heo 		ap->nr_active_links++;
54339af5c9c9STejun Heo 		link->active_tag = qc->tag;
5434c6fd2807SJeff Garzik 	}
5435c6fd2807SJeff Garzik 
5436c6fd2807SJeff Garzik 	qc->flags |= ATA_QCFLAG_ACTIVE;
5437e3ed8939SJens Axboe 	ap->qc_active |= 1ULL << qc->tag;
5438c6fd2807SJeff Garzik 
543960f5d6efSTejun Heo 	/*
544060f5d6efSTejun Heo 	 * We guarantee to LLDs that they will have at least one
5441f92a2636STejun Heo 	 * non-zero sg if the command is a data command.
5442f92a2636STejun Heo 	 */
54439173e5e8SEric Biggers 	if (ata_is_data(prot) && (!qc->sg || !qc->n_elem || !qc->nbytes))
544460f5d6efSTejun Heo 		goto sys_err;
5445f92a2636STejun Heo 
5446405e66b3STejun Heo 	if (ata_is_dma(prot) || (ata_is_pio(prot) &&
5447f92a2636STejun Heo 				 (ap->flags & ATA_FLAG_PIO_DMA)))
5448c6fd2807SJeff Garzik 		if (ata_sg_setup(qc))
544960f5d6efSTejun Heo 			goto sys_err;
5450c6fd2807SJeff Garzik 
5451cf480626STejun Heo 	/* if device is sleeping, schedule reset and abort the link */
5452054a5fbaSTejun Heo 	if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
5453cf480626STejun Heo 		link->eh_info.action |= ATA_EH_RESET;
5454054a5fbaSTejun Heo 		ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
5455054a5fbaSTejun Heo 		ata_link_abort(link);
5456054a5fbaSTejun Heo 		return;
5457054a5fbaSTejun Heo 	}
5458054a5fbaSTejun Heo 
5459c6fd2807SJeff Garzik 	ap->ops->qc_prep(qc);
5460255c03d1SHannes Reinecke 	trace_ata_qc_issue(qc);
5461c6fd2807SJeff Garzik 	qc->err_mask |= ap->ops->qc_issue(qc);
5462c6fd2807SJeff Garzik 	if (unlikely(qc->err_mask))
5463c6fd2807SJeff Garzik 		goto err;
5464c6fd2807SJeff Garzik 	return;
5465c6fd2807SJeff Garzik 
546660f5d6efSTejun Heo sys_err:
5467c6fd2807SJeff Garzik 	qc->err_mask |= AC_ERR_SYSTEM;
5468c6fd2807SJeff Garzik err:
5469c6fd2807SJeff Garzik 	ata_qc_complete(qc);
5470c6fd2807SJeff Garzik }
5471c6fd2807SJeff Garzik 
5472c6fd2807SJeff Garzik /**
5473c6fd2807SJeff Garzik  *	sata_scr_valid - test whether SCRs are accessible
5474936fd732STejun Heo  *	@link: ATA link to test SCR accessibility for
5475c6fd2807SJeff Garzik  *
5476936fd732STejun Heo  *	Test whether SCRs are accessible for @link.
5477c6fd2807SJeff Garzik  *
5478c6fd2807SJeff Garzik  *	LOCKING:
5479c6fd2807SJeff Garzik  *	None.
5480c6fd2807SJeff Garzik  *
5481c6fd2807SJeff Garzik  *	RETURNS:
5482c6fd2807SJeff Garzik  *	1 if SCRs are accessible, 0 otherwise.
5483c6fd2807SJeff Garzik  */
5484936fd732STejun Heo int sata_scr_valid(struct ata_link *link)
5485c6fd2807SJeff Garzik {
5486936fd732STejun Heo 	struct ata_port *ap = link->ap;
5487936fd732STejun Heo 
5488a16abc0bSTejun Heo 	return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
5489c6fd2807SJeff Garzik }
5490c6fd2807SJeff Garzik 
5491c6fd2807SJeff Garzik /**
5492c6fd2807SJeff Garzik  *	sata_scr_read - read SCR register of the specified port
5493936fd732STejun Heo  *	@link: ATA link to read SCR for
5494c6fd2807SJeff Garzik  *	@reg: SCR to read
5495c6fd2807SJeff Garzik  *	@val: Place to store read value
5496c6fd2807SJeff Garzik  *
5497936fd732STejun Heo  *	Read SCR register @reg of @link into *@val.  This function is
5498633273a3STejun Heo  *	guaranteed to succeed if @link is ap->link, the cable type of
5499633273a3STejun Heo  *	the port is SATA and the port implements ->scr_read.
5500c6fd2807SJeff Garzik  *
5501c6fd2807SJeff Garzik  *	LOCKING:
5502633273a3STejun Heo  *	None if @link is ap->link.  Kernel thread context otherwise.
5503c6fd2807SJeff Garzik  *
5504c6fd2807SJeff Garzik  *	RETURNS:
5505c6fd2807SJeff Garzik  *	0 on success, negative errno on failure.
5506c6fd2807SJeff Garzik  */
5507936fd732STejun Heo int sata_scr_read(struct ata_link *link, int reg, u32 *val)
5508c6fd2807SJeff Garzik {
5509633273a3STejun Heo 	if (ata_is_host_link(link)) {
5510936fd732STejun Heo 		if (sata_scr_valid(link))
551182ef04fbSTejun Heo 			return link->ap->ops->scr_read(link, reg, val);
5512c6fd2807SJeff Garzik 		return -EOPNOTSUPP;
5513c6fd2807SJeff Garzik 	}
5514c6fd2807SJeff Garzik 
5515633273a3STejun Heo 	return sata_pmp_scr_read(link, reg, val);
5516633273a3STejun Heo }
5517633273a3STejun Heo 
5518c6fd2807SJeff Garzik /**
5519c6fd2807SJeff Garzik  *	sata_scr_write - write SCR register of the specified port
5520936fd732STejun Heo  *	@link: ATA link to write SCR for
5521c6fd2807SJeff Garzik  *	@reg: SCR to write
5522c6fd2807SJeff Garzik  *	@val: value to write
5523c6fd2807SJeff Garzik  *
5524936fd732STejun Heo  *	Write @val to SCR register @reg of @link.  This function is
5525633273a3STejun Heo  *	guaranteed to succeed if @link is ap->link, the cable type of
5526633273a3STejun Heo  *	the port is SATA and the port implements ->scr_read.
5527c6fd2807SJeff Garzik  *
5528c6fd2807SJeff Garzik  *	LOCKING:
5529633273a3STejun Heo  *	None if @link is ap->link.  Kernel thread context otherwise.
5530c6fd2807SJeff Garzik  *
5531c6fd2807SJeff Garzik  *	RETURNS:
5532c6fd2807SJeff Garzik  *	0 on success, negative errno on failure.
5533c6fd2807SJeff Garzik  */
5534936fd732STejun Heo int sata_scr_write(struct ata_link *link, int reg, u32 val)
5535c6fd2807SJeff Garzik {
5536633273a3STejun Heo 	if (ata_is_host_link(link)) {
5537936fd732STejun Heo 		if (sata_scr_valid(link))
553882ef04fbSTejun Heo 			return link->ap->ops->scr_write(link, reg, val);
5539c6fd2807SJeff Garzik 		return -EOPNOTSUPP;
5540c6fd2807SJeff Garzik 	}
5541c6fd2807SJeff Garzik 
5542633273a3STejun Heo 	return sata_pmp_scr_write(link, reg, val);
5543633273a3STejun Heo }
5544633273a3STejun Heo 
5545c6fd2807SJeff Garzik /**
5546c6fd2807SJeff Garzik  *	sata_scr_write_flush - write SCR register of the specified port and flush
5547936fd732STejun Heo  *	@link: ATA link to write SCR for
5548c6fd2807SJeff Garzik  *	@reg: SCR to write
5549c6fd2807SJeff Garzik  *	@val: value to write
5550c6fd2807SJeff Garzik  *
5551c6fd2807SJeff Garzik  *	This function is identical to sata_scr_write() except that this
5552c6fd2807SJeff Garzik  *	function performs flush after writing to the register.
5553c6fd2807SJeff Garzik  *
5554c6fd2807SJeff Garzik  *	LOCKING:
5555633273a3STejun Heo  *	None if @link is ap->link.  Kernel thread context otherwise.
5556c6fd2807SJeff Garzik  *
5557c6fd2807SJeff Garzik  *	RETURNS:
5558c6fd2807SJeff Garzik  *	0 on success, negative errno on failure.
5559c6fd2807SJeff Garzik  */
5560936fd732STejun Heo int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
5561c6fd2807SJeff Garzik {
5562633273a3STejun Heo 	if (ata_is_host_link(link)) {
5563da3dbb17STejun Heo 		int rc;
5564da3dbb17STejun Heo 
5565936fd732STejun Heo 		if (sata_scr_valid(link)) {
556682ef04fbSTejun Heo 			rc = link->ap->ops->scr_write(link, reg, val);
5567da3dbb17STejun Heo 			if (rc == 0)
556882ef04fbSTejun Heo 				rc = link->ap->ops->scr_read(link, reg, &val);
5569da3dbb17STejun Heo 			return rc;
5570c6fd2807SJeff Garzik 		}
5571c6fd2807SJeff Garzik 		return -EOPNOTSUPP;
5572c6fd2807SJeff Garzik 	}
5573c6fd2807SJeff Garzik 
5574633273a3STejun Heo 	return sata_pmp_scr_write(link, reg, val);
5575633273a3STejun Heo }
5576633273a3STejun Heo 
5577c6fd2807SJeff Garzik /**
5578b1c72916STejun Heo  *	ata_phys_link_online - test whether the given link is online
5579936fd732STejun Heo  *	@link: ATA link to test
5580c6fd2807SJeff Garzik  *
5581936fd732STejun Heo  *	Test whether @link is online.  Note that this function returns
5582936fd732STejun Heo  *	0 if online status of @link cannot be obtained, so
5583936fd732STejun Heo  *	ata_link_online(link) != !ata_link_offline(link).
5584c6fd2807SJeff Garzik  *
5585c6fd2807SJeff Garzik  *	LOCKING:
5586c6fd2807SJeff Garzik  *	None.
5587c6fd2807SJeff Garzik  *
5588c6fd2807SJeff Garzik  *	RETURNS:
5589b5b3fa38STejun Heo  *	True if the port online status is available and online.
5590c6fd2807SJeff Garzik  */
5591b1c72916STejun Heo bool ata_phys_link_online(struct ata_link *link)
5592c6fd2807SJeff Garzik {
5593c6fd2807SJeff Garzik 	u32 sstatus;
5594c6fd2807SJeff Garzik 
5595936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
55969913ff8aSTejun Heo 	    ata_sstatus_online(sstatus))
5597b5b3fa38STejun Heo 		return true;
5598b5b3fa38STejun Heo 	return false;
5599c6fd2807SJeff Garzik }
5600c6fd2807SJeff Garzik 
5601c6fd2807SJeff Garzik /**
5602b1c72916STejun Heo  *	ata_phys_link_offline - test whether the given link is offline
5603936fd732STejun Heo  *	@link: ATA link to test
5604c6fd2807SJeff Garzik  *
5605936fd732STejun Heo  *	Test whether @link is offline.  Note that this function
5606936fd732STejun Heo  *	returns 0 if offline status of @link cannot be obtained, so
5607936fd732STejun Heo  *	ata_link_online(link) != !ata_link_offline(link).
5608c6fd2807SJeff Garzik  *
5609c6fd2807SJeff Garzik  *	LOCKING:
5610c6fd2807SJeff Garzik  *	None.
5611c6fd2807SJeff Garzik  *
5612c6fd2807SJeff Garzik  *	RETURNS:
5613b5b3fa38STejun Heo  *	True if the port offline status is available and offline.
5614c6fd2807SJeff Garzik  */
5615b1c72916STejun Heo bool ata_phys_link_offline(struct ata_link *link)
5616c6fd2807SJeff Garzik {
5617c6fd2807SJeff Garzik 	u32 sstatus;
5618c6fd2807SJeff Garzik 
5619936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
56209913ff8aSTejun Heo 	    !ata_sstatus_online(sstatus))
5621b5b3fa38STejun Heo 		return true;
5622b5b3fa38STejun Heo 	return false;
5623c6fd2807SJeff Garzik }
5624c6fd2807SJeff Garzik 
5625b1c72916STejun Heo /**
5626b1c72916STejun Heo  *	ata_link_online - test whether the given link is online
5627b1c72916STejun Heo  *	@link: ATA link to test
5628b1c72916STejun Heo  *
5629b1c72916STejun Heo  *	Test whether @link is online.  This is identical to
5630b1c72916STejun Heo  *	ata_phys_link_online() when there's no slave link.  When
5631b1c72916STejun Heo  *	there's a slave link, this function should only be called on
5632b1c72916STejun Heo  *	the master link and will return true if any of M/S links is
5633b1c72916STejun Heo  *	online.
5634b1c72916STejun Heo  *
5635b1c72916STejun Heo  *	LOCKING:
5636b1c72916STejun Heo  *	None.
5637b1c72916STejun Heo  *
5638b1c72916STejun Heo  *	RETURNS:
5639b1c72916STejun Heo  *	True if the port online status is available and online.
5640b1c72916STejun Heo  */
5641b1c72916STejun Heo bool ata_link_online(struct ata_link *link)
5642b1c72916STejun Heo {
5643b1c72916STejun Heo 	struct ata_link *slave = link->ap->slave_link;
5644b1c72916STejun Heo 
5645b1c72916STejun Heo 	WARN_ON(link == slave);	/* shouldn't be called on slave link */
5646b1c72916STejun Heo 
5647b1c72916STejun Heo 	return ata_phys_link_online(link) ||
5648b1c72916STejun Heo 		(slave && ata_phys_link_online(slave));
5649b1c72916STejun Heo }
5650b1c72916STejun Heo 
5651b1c72916STejun Heo /**
5652b1c72916STejun Heo  *	ata_link_offline - test whether the given link is offline
5653b1c72916STejun Heo  *	@link: ATA link to test
5654b1c72916STejun Heo  *
5655b1c72916STejun Heo  *	Test whether @link is offline.  This is identical to
5656b1c72916STejun Heo  *	ata_phys_link_offline() when there's no slave link.  When
5657b1c72916STejun Heo  *	there's a slave link, this function should only be called on
5658b1c72916STejun Heo  *	the master link and will return true if both M/S links are
5659b1c72916STejun Heo  *	offline.
5660b1c72916STejun Heo  *
5661b1c72916STejun Heo  *	LOCKING:
5662b1c72916STejun Heo  *	None.
5663b1c72916STejun Heo  *
5664b1c72916STejun Heo  *	RETURNS:
5665b1c72916STejun Heo  *	True if the port offline status is available and offline.
5666b1c72916STejun Heo  */
5667b1c72916STejun Heo bool ata_link_offline(struct ata_link *link)
5668b1c72916STejun Heo {
5669b1c72916STejun Heo 	struct ata_link *slave = link->ap->slave_link;
5670b1c72916STejun Heo 
5671b1c72916STejun Heo 	WARN_ON(link == slave);	/* shouldn't be called on slave link */
5672b1c72916STejun Heo 
5673b1c72916STejun Heo 	return ata_phys_link_offline(link) &&
5674b1c72916STejun Heo 		(!slave || ata_phys_link_offline(slave));
5675b1c72916STejun Heo }
5676b1c72916STejun Heo 
56776ffa01d8STejun Heo #ifdef CONFIG_PM
5678bc6e7c4bSDan Williams static void ata_port_request_pm(struct ata_port *ap, pm_message_t mesg,
5679cca3974eSJeff Garzik 				unsigned int action, unsigned int ehi_flags,
5680bc6e7c4bSDan Williams 				bool async)
5681c6fd2807SJeff Garzik {
5682e3667ebfSTejun Heo 	struct ata_link *link;
56835ef41082SLin Ming 	unsigned long flags;
5684c6fd2807SJeff Garzik 
5685c6fd2807SJeff Garzik 	/* Previous resume operation might still be in
5686c6fd2807SJeff Garzik 	 * progress.  Wait for PM_PENDING to clear.
5687c6fd2807SJeff Garzik 	 */
5688c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_PM_PENDING) {
5689c6fd2807SJeff Garzik 		ata_port_wait_eh(ap);
5690c6fd2807SJeff Garzik 		WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5691c6fd2807SJeff Garzik 	}
5692c6fd2807SJeff Garzik 
5693c6fd2807SJeff Garzik 	/* request PM ops to EH */
5694c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
5695c6fd2807SJeff Garzik 
5696c6fd2807SJeff Garzik 	ap->pm_mesg = mesg;
5697c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_PM_PENDING;
56981eca4365STejun Heo 	ata_for_each_link(link, ap, HOST_FIRST) {
5699e3667ebfSTejun Heo 		link->eh_info.action |= action;
5700e3667ebfSTejun Heo 		link->eh_info.flags |= ehi_flags;
5701e3667ebfSTejun Heo 	}
5702c6fd2807SJeff Garzik 
5703c6fd2807SJeff Garzik 	ata_port_schedule_eh(ap);
5704c6fd2807SJeff Garzik 
5705c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
5706c6fd2807SJeff Garzik 
57072fcbdcb4SDan Williams 	if (!async) {
5708c6fd2807SJeff Garzik 		ata_port_wait_eh(ap);
5709c6fd2807SJeff Garzik 		WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5710c6fd2807SJeff Garzik 	}
5711c6fd2807SJeff Garzik }
5712c6fd2807SJeff Garzik 
571333574d68SLin Ming /*
5714bc6e7c4bSDan Williams  * On some hardware, device fails to respond after spun down for suspend.  As
5715bc6e7c4bSDan Williams  * the device won't be used before being resumed, we don't need to touch the
5716bc6e7c4bSDan Williams  * device.  Ask EH to skip the usual stuff and proceed directly to suspend.
571733574d68SLin Ming  *
571833574d68SLin Ming  * http://thread.gmane.org/gmane.linux.ide/46764
571933574d68SLin Ming  */
5720bc6e7c4bSDan Williams static const unsigned int ata_port_suspend_ehi = ATA_EHI_QUIET
5721bc6e7c4bSDan Williams 						 | ATA_EHI_NO_AUTOPSY
5722bc6e7c4bSDan Williams 						 | ATA_EHI_NO_RECOVERY;
5723bc6e7c4bSDan Williams 
5724bc6e7c4bSDan Williams static void ata_port_suspend(struct ata_port *ap, pm_message_t mesg)
5725bc6e7c4bSDan Williams {
5726bc6e7c4bSDan Williams 	ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, false);
57275ef41082SLin Ming }
57285ef41082SLin Ming 
5729bc6e7c4bSDan Williams static void ata_port_suspend_async(struct ata_port *ap, pm_message_t mesg)
5730bc6e7c4bSDan Williams {
5731bc6e7c4bSDan Williams 	ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, true);
5732bc6e7c4bSDan Williams }
5733bc6e7c4bSDan Williams 
5734bc6e7c4bSDan Williams static int ata_port_pm_suspend(struct device *dev)
57352fcbdcb4SDan Williams {
57362fcbdcb4SDan Williams 	struct ata_port *ap = to_ata_port(dev);
57372fcbdcb4SDan Williams 
57385ef41082SLin Ming 	if (pm_runtime_suspended(dev))
57395ef41082SLin Ming 		return 0;
57405ef41082SLin Ming 
5741bc6e7c4bSDan Williams 	ata_port_suspend(ap, PMSG_SUSPEND);
5742f5e6d0d0SAaron Lu 	return 0;
574333574d68SLin Ming }
574433574d68SLin Ming 
5745bc6e7c4bSDan Williams static int ata_port_pm_freeze(struct device *dev)
57462fcbdcb4SDan Williams {
57472fcbdcb4SDan Williams 	struct ata_port *ap = to_ata_port(dev);
57482fcbdcb4SDan Williams 
5749bc6e7c4bSDan Williams 	if (pm_runtime_suspended(dev))
5750bc6e7c4bSDan Williams 		return 0;
5751bc6e7c4bSDan Williams 
5752bc6e7c4bSDan Williams 	ata_port_suspend(ap, PMSG_FREEZE);
5753bc6e7c4bSDan Williams 	return 0;
57542fcbdcb4SDan Williams }
57552fcbdcb4SDan Williams 
5756bc6e7c4bSDan Williams static int ata_port_pm_poweroff(struct device *dev)
5757e90b1e5aSLin Ming {
5758bc6e7c4bSDan Williams 	ata_port_suspend(to_ata_port(dev), PMSG_HIBERNATE);
5759bc6e7c4bSDan Williams 	return 0;
5760bc6e7c4bSDan Williams }
5761e90b1e5aSLin Ming 
5762bc6e7c4bSDan Williams static const unsigned int ata_port_resume_ehi = ATA_EHI_NO_AUTOPSY
5763bc6e7c4bSDan Williams 						| ATA_EHI_QUIET;
5764bc6e7c4bSDan Williams 
5765bc6e7c4bSDan Williams static void ata_port_resume(struct ata_port *ap, pm_message_t mesg)
5766bc6e7c4bSDan Williams {
5767bc6e7c4bSDan Williams 	ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, false);
5768bc6e7c4bSDan Williams }
5769bc6e7c4bSDan Williams 
5770bc6e7c4bSDan Williams static void ata_port_resume_async(struct ata_port *ap, pm_message_t mesg)
5771bc6e7c4bSDan Williams {
5772bc6e7c4bSDan Williams 	ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, true);
5773bc6e7c4bSDan Williams }
5774bc6e7c4bSDan Williams 
5775bc6e7c4bSDan Williams static int ata_port_pm_resume(struct device *dev)
5776bc6e7c4bSDan Williams {
5777200421a8STodd Brandt 	ata_port_resume_async(to_ata_port(dev), PMSG_RESUME);
5778e90b1e5aSLin Ming 	pm_runtime_disable(dev);
5779e90b1e5aSLin Ming 	pm_runtime_set_active(dev);
5780e90b1e5aSLin Ming 	pm_runtime_enable(dev);
5781bc6e7c4bSDan Williams 	return 0;
5782e90b1e5aSLin Ming }
5783e90b1e5aSLin Ming 
57847e15e9beSAaron Lu /*
57857e15e9beSAaron Lu  * For ODDs, the upper layer will poll for media change every few seconds,
57867e15e9beSAaron Lu  * which will make it enter and leave suspend state every few seconds. And
57877e15e9beSAaron Lu  * as each suspend will cause a hard/soft reset, the gain of runtime suspend
57887e15e9beSAaron Lu  * is very little and the ODD may malfunction after constantly being reset.
57897e15e9beSAaron Lu  * So the idle callback here will not proceed to suspend if a non-ZPODD capable
57907e15e9beSAaron Lu  * ODD is attached to the port.
57917e15e9beSAaron Lu  */
57929ee4f393SLin Ming static int ata_port_runtime_idle(struct device *dev)
57939ee4f393SLin Ming {
57947e15e9beSAaron Lu 	struct ata_port *ap = to_ata_port(dev);
57957e15e9beSAaron Lu 	struct ata_link *link;
57967e15e9beSAaron Lu 	struct ata_device *adev;
57977e15e9beSAaron Lu 
57987e15e9beSAaron Lu 	ata_for_each_link(link, ap, HOST_FIRST) {
57997e15e9beSAaron Lu 		ata_for_each_dev(adev, link, ENABLED)
58007e15e9beSAaron Lu 			if (adev->class == ATA_DEV_ATAPI &&
58017e15e9beSAaron Lu 			    !zpodd_dev_enabled(adev))
58027e15e9beSAaron Lu 				return -EBUSY;
58037e15e9beSAaron Lu 	}
58047e15e9beSAaron Lu 
580545f0a85cSRafael J. Wysocki 	return 0;
58069ee4f393SLin Ming }
58079ee4f393SLin Ming 
5808a7ff60dbSAaron Lu static int ata_port_runtime_suspend(struct device *dev)
5809a7ff60dbSAaron Lu {
5810bc6e7c4bSDan Williams 	ata_port_suspend(to_ata_port(dev), PMSG_AUTO_SUSPEND);
5811bc6e7c4bSDan Williams 	return 0;
5812a7ff60dbSAaron Lu }
5813a7ff60dbSAaron Lu 
5814a7ff60dbSAaron Lu static int ata_port_runtime_resume(struct device *dev)
5815a7ff60dbSAaron Lu {
5816bc6e7c4bSDan Williams 	ata_port_resume(to_ata_port(dev), PMSG_AUTO_RESUME);
5817bc6e7c4bSDan Williams 	return 0;
5818a7ff60dbSAaron Lu }
5819a7ff60dbSAaron Lu 
58205ef41082SLin Ming static const struct dev_pm_ops ata_port_pm_ops = {
5821bc6e7c4bSDan Williams 	.suspend = ata_port_pm_suspend,
5822bc6e7c4bSDan Williams 	.resume = ata_port_pm_resume,
5823bc6e7c4bSDan Williams 	.freeze = ata_port_pm_freeze,
5824bc6e7c4bSDan Williams 	.thaw = ata_port_pm_resume,
5825bc6e7c4bSDan Williams 	.poweroff = ata_port_pm_poweroff,
5826bc6e7c4bSDan Williams 	.restore = ata_port_pm_resume,
58279ee4f393SLin Ming 
5828a7ff60dbSAaron Lu 	.runtime_suspend = ata_port_runtime_suspend,
5829a7ff60dbSAaron Lu 	.runtime_resume = ata_port_runtime_resume,
58309ee4f393SLin Ming 	.runtime_idle = ata_port_runtime_idle,
58315ef41082SLin Ming };
58325ef41082SLin Ming 
58332fcbdcb4SDan Williams /* sas ports don't participate in pm runtime management of ata_ports,
58342fcbdcb4SDan Williams  * and need to resume ata devices at the domain level, not the per-port
58352fcbdcb4SDan Williams  * level. sas suspend/resume is async to allow parallel port recovery
58362fcbdcb4SDan Williams  * since sas has multiple ata_port instances per Scsi_Host.
58372fcbdcb4SDan Williams  */
5838bc6e7c4bSDan Williams void ata_sas_port_suspend(struct ata_port *ap)
58392fcbdcb4SDan Williams {
5840bc6e7c4bSDan Williams 	ata_port_suspend_async(ap, PMSG_SUSPEND);
58412fcbdcb4SDan Williams }
5842bc6e7c4bSDan Williams EXPORT_SYMBOL_GPL(ata_sas_port_suspend);
58432fcbdcb4SDan Williams 
5844bc6e7c4bSDan Williams void ata_sas_port_resume(struct ata_port *ap)
58452fcbdcb4SDan Williams {
5846bc6e7c4bSDan Williams 	ata_port_resume_async(ap, PMSG_RESUME);
58472fcbdcb4SDan Williams }
5848bc6e7c4bSDan Williams EXPORT_SYMBOL_GPL(ata_sas_port_resume);
58492fcbdcb4SDan Williams 
5850c6fd2807SJeff Garzik /**
5851cca3974eSJeff Garzik  *	ata_host_suspend - suspend host
5852cca3974eSJeff Garzik  *	@host: host to suspend
5853c6fd2807SJeff Garzik  *	@mesg: PM message
5854c6fd2807SJeff Garzik  *
58555ef41082SLin Ming  *	Suspend @host.  Actual operation is performed by port suspend.
5856c6fd2807SJeff Garzik  */
5857cca3974eSJeff Garzik int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
5858c6fd2807SJeff Garzik {
585972ad6ec4SJeff Garzik 	host->dev->power.power_state = mesg;
58605ef41082SLin Ming 	return 0;
5861c6fd2807SJeff Garzik }
5862c6fd2807SJeff Garzik 
5863c6fd2807SJeff Garzik /**
5864cca3974eSJeff Garzik  *	ata_host_resume - resume host
5865cca3974eSJeff Garzik  *	@host: host to resume
5866c6fd2807SJeff Garzik  *
58675ef41082SLin Ming  *	Resume @host.  Actual operation is performed by port resume.
5868c6fd2807SJeff Garzik  */
5869cca3974eSJeff Garzik void ata_host_resume(struct ata_host *host)
5870c6fd2807SJeff Garzik {
587172ad6ec4SJeff Garzik 	host->dev->power.power_state = PMSG_ON;
5872c6fd2807SJeff Garzik }
58736ffa01d8STejun Heo #endif
5874c6fd2807SJeff Garzik 
58758df82c13SBhumika Goyal const struct device_type ata_port_type = {
58765ef41082SLin Ming 	.name = "ata_port",
58775ef41082SLin Ming #ifdef CONFIG_PM
58785ef41082SLin Ming 	.pm = &ata_port_pm_ops,
58795ef41082SLin Ming #endif
58805ef41082SLin Ming };
58815ef41082SLin Ming 
5882c6fd2807SJeff Garzik /**
5883c6fd2807SJeff Garzik  *	ata_dev_init - Initialize an ata_device structure
5884c6fd2807SJeff Garzik  *	@dev: Device structure to initialize
5885c6fd2807SJeff Garzik  *
5886c6fd2807SJeff Garzik  *	Initialize @dev in preparation for probing.
5887c6fd2807SJeff Garzik  *
5888c6fd2807SJeff Garzik  *	LOCKING:
5889c6fd2807SJeff Garzik  *	Inherited from caller.
5890c6fd2807SJeff Garzik  */
5891c6fd2807SJeff Garzik void ata_dev_init(struct ata_device *dev)
5892c6fd2807SJeff Garzik {
5893b1c72916STejun Heo 	struct ata_link *link = ata_dev_phys_link(dev);
58949af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
5895c6fd2807SJeff Garzik 	unsigned long flags;
5896c6fd2807SJeff Garzik 
5897b1c72916STejun Heo 	/* SATA spd limit is bound to the attached device, reset together */
58989af5c9c9STejun Heo 	link->sata_spd_limit = link->hw_sata_spd_limit;
58999af5c9c9STejun Heo 	link->sata_spd = 0;
5900c6fd2807SJeff Garzik 
5901c6fd2807SJeff Garzik 	/* High bits of dev->flags are used to record warm plug
5902c6fd2807SJeff Garzik 	 * requests which occur asynchronously.  Synchronize using
5903cca3974eSJeff Garzik 	 * host lock.
5904c6fd2807SJeff Garzik 	 */
5905c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
5906c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_INIT_MASK;
59073dcc323fSTejun Heo 	dev->horkage = 0;
5908c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
5909c6fd2807SJeff Garzik 
591099cf610aSTejun Heo 	memset((void *)dev + ATA_DEVICE_CLEAR_BEGIN, 0,
591199cf610aSTejun Heo 	       ATA_DEVICE_CLEAR_END - ATA_DEVICE_CLEAR_BEGIN);
5912c6fd2807SJeff Garzik 	dev->pio_mask = UINT_MAX;
5913c6fd2807SJeff Garzik 	dev->mwdma_mask = UINT_MAX;
5914c6fd2807SJeff Garzik 	dev->udma_mask = UINT_MAX;
5915c6fd2807SJeff Garzik }
5916c6fd2807SJeff Garzik 
5917c6fd2807SJeff Garzik /**
59184fb37a25STejun Heo  *	ata_link_init - Initialize an ata_link structure
59194fb37a25STejun Heo  *	@ap: ATA port link is attached to
59204fb37a25STejun Heo  *	@link: Link structure to initialize
59218989805dSTejun Heo  *	@pmp: Port multiplier port number
59224fb37a25STejun Heo  *
59234fb37a25STejun Heo  *	Initialize @link.
59244fb37a25STejun Heo  *
59254fb37a25STejun Heo  *	LOCKING:
59264fb37a25STejun Heo  *	Kernel thread context (may sleep)
59274fb37a25STejun Heo  */
5928fb7fd614STejun Heo void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
59294fb37a25STejun Heo {
59304fb37a25STejun Heo 	int i;
59314fb37a25STejun Heo 
59324fb37a25STejun Heo 	/* clear everything except for devices */
5933d9027470SGwendal Grignou 	memset((void *)link + ATA_LINK_CLEAR_BEGIN, 0,
5934d9027470SGwendal Grignou 	       ATA_LINK_CLEAR_END - ATA_LINK_CLEAR_BEGIN);
59354fb37a25STejun Heo 
59364fb37a25STejun Heo 	link->ap = ap;
59378989805dSTejun Heo 	link->pmp = pmp;
59384fb37a25STejun Heo 	link->active_tag = ATA_TAG_POISON;
59394fb37a25STejun Heo 	link->hw_sata_spd_limit = UINT_MAX;
59404fb37a25STejun Heo 
59414fb37a25STejun Heo 	/* can't use iterator, ap isn't initialized yet */
59424fb37a25STejun Heo 	for (i = 0; i < ATA_MAX_DEVICES; i++) {
59434fb37a25STejun Heo 		struct ata_device *dev = &link->device[i];
59444fb37a25STejun Heo 
59454fb37a25STejun Heo 		dev->link = link;
59464fb37a25STejun Heo 		dev->devno = dev - link->device;
5947110f66d2STejun Heo #ifdef CONFIG_ATA_ACPI
5948110f66d2STejun Heo 		dev->gtf_filter = ata_acpi_gtf_filter;
5949110f66d2STejun Heo #endif
59504fb37a25STejun Heo 		ata_dev_init(dev);
59514fb37a25STejun Heo 	}
59524fb37a25STejun Heo }
59534fb37a25STejun Heo 
59544fb37a25STejun Heo /**
59554fb37a25STejun Heo  *	sata_link_init_spd - Initialize link->sata_spd_limit
59564fb37a25STejun Heo  *	@link: Link to configure sata_spd_limit for
59574fb37a25STejun Heo  *
59584fb37a25STejun Heo  *	Initialize @link->[hw_]sata_spd_limit to the currently
59594fb37a25STejun Heo  *	configured value.
59604fb37a25STejun Heo  *
59614fb37a25STejun Heo  *	LOCKING:
59624fb37a25STejun Heo  *	Kernel thread context (may sleep).
59634fb37a25STejun Heo  *
59644fb37a25STejun Heo  *	RETURNS:
59654fb37a25STejun Heo  *	0 on success, -errno on failure.
59664fb37a25STejun Heo  */
5967fb7fd614STejun Heo int sata_link_init_spd(struct ata_link *link)
59684fb37a25STejun Heo {
596933267325STejun Heo 	u8 spd;
59704fb37a25STejun Heo 	int rc;
59714fb37a25STejun Heo 
5972d127ea7bSTejun Heo 	rc = sata_scr_read(link, SCR_CONTROL, &link->saved_scontrol);
59734fb37a25STejun Heo 	if (rc)
59744fb37a25STejun Heo 		return rc;
59754fb37a25STejun Heo 
5976d127ea7bSTejun Heo 	spd = (link->saved_scontrol >> 4) & 0xf;
59774fb37a25STejun Heo 	if (spd)
59784fb37a25STejun Heo 		link->hw_sata_spd_limit &= (1 << spd) - 1;
59794fb37a25STejun Heo 
598005944bdfSTejun Heo 	ata_force_link_limits(link);
598133267325STejun Heo 
59824fb37a25STejun Heo 	link->sata_spd_limit = link->hw_sata_spd_limit;
59834fb37a25STejun Heo 
59844fb37a25STejun Heo 	return 0;
59854fb37a25STejun Heo }
59864fb37a25STejun Heo 
59874fb37a25STejun Heo /**
5988f3187195STejun Heo  *	ata_port_alloc - allocate and initialize basic ATA port resources
5989f3187195STejun Heo  *	@host: ATA host this allocated port belongs to
5990c6fd2807SJeff Garzik  *
5991f3187195STejun Heo  *	Allocate and initialize basic ATA port resources.
5992f3187195STejun Heo  *
5993f3187195STejun Heo  *	RETURNS:
5994f3187195STejun Heo  *	Allocate ATA port on success, NULL on failure.
5995c6fd2807SJeff Garzik  *
5996c6fd2807SJeff Garzik  *	LOCKING:
5997f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5998c6fd2807SJeff Garzik  */
5999f3187195STejun Heo struct ata_port *ata_port_alloc(struct ata_host *host)
6000c6fd2807SJeff Garzik {
6001f3187195STejun Heo 	struct ata_port *ap;
6002c6fd2807SJeff Garzik 
6003f3187195STejun Heo 	DPRINTK("ENTER\n");
6004f3187195STejun Heo 
6005f3187195STejun Heo 	ap = kzalloc(sizeof(*ap), GFP_KERNEL);
6006f3187195STejun Heo 	if (!ap)
6007f3187195STejun Heo 		return NULL;
6008f3187195STejun Heo 
60097b3a24c5SMaxime Bizon 	ap->pflags |= ATA_PFLAG_INITIALIZING | ATA_PFLAG_FROZEN;
6010cca3974eSJeff Garzik 	ap->lock = &host->lock;
6011f3187195STejun Heo 	ap->print_id = -1;
6012e628dc99SDavid Milburn 	ap->local_port_no = -1;
6013cca3974eSJeff Garzik 	ap->host = host;
6014f3187195STejun Heo 	ap->dev = host->dev;
6015c6fd2807SJeff Garzik 
6016c6fd2807SJeff Garzik #if defined(ATA_VERBOSE_DEBUG)
6017c6fd2807SJeff Garzik 	/* turn on all debugging levels */
6018c6fd2807SJeff Garzik 	ap->msg_enable = 0x00FF;
6019c6fd2807SJeff Garzik #elif defined(ATA_DEBUG)
6020c6fd2807SJeff Garzik 	ap->msg_enable = ATA_MSG_DRV | ATA_MSG_INFO | ATA_MSG_CTL | ATA_MSG_WARN | ATA_MSG_ERR;
6021c6fd2807SJeff Garzik #else
6022c6fd2807SJeff Garzik 	ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
6023c6fd2807SJeff Garzik #endif
6024c6fd2807SJeff Garzik 
6025ad72cf98STejun Heo 	mutex_init(&ap->scsi_scan_mutex);
602665f27f38SDavid Howells 	INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
602765f27f38SDavid Howells 	INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
6028c6fd2807SJeff Garzik 	INIT_LIST_HEAD(&ap->eh_done_q);
6029c6fd2807SJeff Garzik 	init_waitqueue_head(&ap->eh_wait_q);
603045fabbb7SElias Oltmanns 	init_completion(&ap->park_req_pending);
6031b93ab338SKees Cook 	timer_setup(&ap->fastdrain_timer, ata_eh_fastdrain_timerfn,
6032b93ab338SKees Cook 		    TIMER_DEFERRABLE);
6033c6fd2807SJeff Garzik 
6034c6fd2807SJeff Garzik 	ap->cbl = ATA_CBL_NONE;
6035c6fd2807SJeff Garzik 
60368989805dSTejun Heo 	ata_link_init(ap, &ap->link, 0);
6037c6fd2807SJeff Garzik 
6038c6fd2807SJeff Garzik #ifdef ATA_IRQ_TRAP
6039c6fd2807SJeff Garzik 	ap->stats.unhandled_irq = 1;
6040c6fd2807SJeff Garzik 	ap->stats.idle_irq = 1;
6041c6fd2807SJeff Garzik #endif
6042270390e1STejun Heo 	ata_sff_port_init(ap);
6043270390e1STejun Heo 
6044c6fd2807SJeff Garzik 	return ap;
6045c6fd2807SJeff Garzik }
6046c6fd2807SJeff Garzik 
60472623c7a5STaras Kondratiuk static void ata_devres_release(struct device *gendev, void *res)
6048f0d36efdSTejun Heo {
6049f0d36efdSTejun Heo 	struct ata_host *host = dev_get_drvdata(gendev);
6050f0d36efdSTejun Heo 	int i;
6051f0d36efdSTejun Heo 
6052f0d36efdSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
6053f0d36efdSTejun Heo 		struct ata_port *ap = host->ports[i];
6054f0d36efdSTejun Heo 
6055ecef7253STejun Heo 		if (!ap)
6056ecef7253STejun Heo 			continue;
6057ecef7253STejun Heo 
60584911487aSTejun Heo 		if (ap->scsi_host)
60591aa506e4STejun Heo 			scsi_host_put(ap->scsi_host);
60601aa506e4STejun Heo 
60612623c7a5STaras Kondratiuk 	}
60622623c7a5STaras Kondratiuk 
60632623c7a5STaras Kondratiuk 	dev_set_drvdata(gendev, NULL);
60642623c7a5STaras Kondratiuk 	ata_host_put(host);
60652623c7a5STaras Kondratiuk }
60662623c7a5STaras Kondratiuk 
60672623c7a5STaras Kondratiuk static void ata_host_release(struct kref *kref)
60682623c7a5STaras Kondratiuk {
60692623c7a5STaras Kondratiuk 	struct ata_host *host = container_of(kref, struct ata_host, kref);
60702623c7a5STaras Kondratiuk 	int i;
60712623c7a5STaras Kondratiuk 
60722623c7a5STaras Kondratiuk 	for (i = 0; i < host->n_ports; i++) {
60732623c7a5STaras Kondratiuk 		struct ata_port *ap = host->ports[i];
60742623c7a5STaras Kondratiuk 
6075633273a3STejun Heo 		kfree(ap->pmp_link);
6076b1c72916STejun Heo 		kfree(ap->slave_link);
60774911487aSTejun Heo 		kfree(ap);
60781aa506e4STejun Heo 		host->ports[i] = NULL;
60791aa506e4STejun Heo 	}
60802623c7a5STaras Kondratiuk 	kfree(host);
60812623c7a5STaras Kondratiuk }
60821aa506e4STejun Heo 
60832623c7a5STaras Kondratiuk void ata_host_get(struct ata_host *host)
60842623c7a5STaras Kondratiuk {
60852623c7a5STaras Kondratiuk 	kref_get(&host->kref);
60862623c7a5STaras Kondratiuk }
60872623c7a5STaras Kondratiuk 
60882623c7a5STaras Kondratiuk void ata_host_put(struct ata_host *host)
60892623c7a5STaras Kondratiuk {
60902623c7a5STaras Kondratiuk 	kref_put(&host->kref, ata_host_release);
6091f0d36efdSTejun Heo }
6092f0d36efdSTejun Heo 
6093c6fd2807SJeff Garzik /**
6094f3187195STejun Heo  *	ata_host_alloc - allocate and init basic ATA host resources
6095f3187195STejun Heo  *	@dev: generic device this host is associated with
6096f3187195STejun Heo  *	@max_ports: maximum number of ATA ports associated with this host
6097f3187195STejun Heo  *
6098f3187195STejun Heo  *	Allocate and initialize basic ATA host resources.  LLD calls
6099f3187195STejun Heo  *	this function to allocate a host, initializes it fully and
6100f3187195STejun Heo  *	attaches it using ata_host_register().
6101f3187195STejun Heo  *
6102f3187195STejun Heo  *	@max_ports ports are allocated and host->n_ports is
6103f3187195STejun Heo  *	initialized to @max_ports.  The caller is allowed to decrease
6104f3187195STejun Heo  *	host->n_ports before calling ata_host_register().  The unused
6105f3187195STejun Heo  *	ports will be automatically freed on registration.
6106f3187195STejun Heo  *
6107f3187195STejun Heo  *	RETURNS:
6108f3187195STejun Heo  *	Allocate ATA host on success, NULL on failure.
6109f3187195STejun Heo  *
6110f3187195STejun Heo  *	LOCKING:
6111f3187195STejun Heo  *	Inherited from calling layer (may sleep).
6112f3187195STejun Heo  */
6113f3187195STejun Heo struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
6114f3187195STejun Heo {
6115f3187195STejun Heo 	struct ata_host *host;
6116f3187195STejun Heo 	size_t sz;
6117f3187195STejun Heo 	int i;
61182623c7a5STaras Kondratiuk 	void *dr;
6119f3187195STejun Heo 
6120f3187195STejun Heo 	DPRINTK("ENTER\n");
6121f3187195STejun Heo 
6122f3187195STejun Heo 	/* alloc a container for our list of ATA ports (buses) */
6123f3187195STejun Heo 	sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
61242623c7a5STaras Kondratiuk 	host = kzalloc(sz, GFP_KERNEL);
6125f3187195STejun Heo 	if (!host)
61262623c7a5STaras Kondratiuk 		return NULL;
61272623c7a5STaras Kondratiuk 
6128f3187195STejun Heo 	if (!devres_open_group(dev, NULL, GFP_KERNEL))
6129dafd6c49SColin Ian King 		goto err_free;
6130f3187195STejun Heo 
61312623c7a5STaras Kondratiuk 	dr = devres_alloc(ata_devres_release, 0, GFP_KERNEL);
61322623c7a5STaras Kondratiuk 	if (!dr)
6133f3187195STejun Heo 		goto err_out;
6134f3187195STejun Heo 
61352623c7a5STaras Kondratiuk 	devres_add(dev, dr);
6136f3187195STejun Heo 	dev_set_drvdata(dev, host);
6137f3187195STejun Heo 
6138f3187195STejun Heo 	spin_lock_init(&host->lock);
6139c0c362b6STejun Heo 	mutex_init(&host->eh_mutex);
6140f3187195STejun Heo 	host->dev = dev;
6141f3187195STejun Heo 	host->n_ports = max_ports;
61422623c7a5STaras Kondratiuk 	kref_init(&host->kref);
6143f3187195STejun Heo 
6144f3187195STejun Heo 	/* allocate ports bound to this host */
6145f3187195STejun Heo 	for (i = 0; i < max_ports; i++) {
6146f3187195STejun Heo 		struct ata_port *ap;
6147f3187195STejun Heo 
6148f3187195STejun Heo 		ap = ata_port_alloc(host);
6149f3187195STejun Heo 		if (!ap)
6150f3187195STejun Heo 			goto err_out;
6151f3187195STejun Heo 
6152f3187195STejun Heo 		ap->port_no = i;
6153f3187195STejun Heo 		host->ports[i] = ap;
6154f3187195STejun Heo 	}
6155f3187195STejun Heo 
6156f3187195STejun Heo 	devres_remove_group(dev, NULL);
6157f3187195STejun Heo 	return host;
6158f3187195STejun Heo 
6159f3187195STejun Heo  err_out:
6160f3187195STejun Heo 	devres_release_group(dev, NULL);
6161dafd6c49SColin Ian King  err_free:
6162dafd6c49SColin Ian King 	kfree(host);
6163f3187195STejun Heo 	return NULL;
6164f3187195STejun Heo }
6165f3187195STejun Heo 
6166f3187195STejun Heo /**
6167f5cda257STejun Heo  *	ata_host_alloc_pinfo - alloc host and init with port_info array
6168f5cda257STejun Heo  *	@dev: generic device this host is associated with
6169f5cda257STejun Heo  *	@ppi: array of ATA port_info to initialize host with
6170f5cda257STejun Heo  *	@n_ports: number of ATA ports attached to this host
6171f5cda257STejun Heo  *
6172f5cda257STejun Heo  *	Allocate ATA host and initialize with info from @ppi.  If NULL
6173f5cda257STejun Heo  *	terminated, @ppi may contain fewer entries than @n_ports.  The
6174f5cda257STejun Heo  *	last entry will be used for the remaining ports.
6175f5cda257STejun Heo  *
6176f5cda257STejun Heo  *	RETURNS:
6177f5cda257STejun Heo  *	Allocate ATA host on success, NULL on failure.
6178f5cda257STejun Heo  *
6179f5cda257STejun Heo  *	LOCKING:
6180f5cda257STejun Heo  *	Inherited from calling layer (may sleep).
6181f5cda257STejun Heo  */
6182f5cda257STejun Heo struct ata_host *ata_host_alloc_pinfo(struct device *dev,
6183f5cda257STejun Heo 				      const struct ata_port_info * const * ppi,
6184f5cda257STejun Heo 				      int n_ports)
6185f5cda257STejun Heo {
6186f5cda257STejun Heo 	const struct ata_port_info *pi;
6187f5cda257STejun Heo 	struct ata_host *host;
6188f5cda257STejun Heo 	int i, j;
6189f5cda257STejun Heo 
6190f5cda257STejun Heo 	host = ata_host_alloc(dev, n_ports);
6191f5cda257STejun Heo 	if (!host)
6192f5cda257STejun Heo 		return NULL;
6193f5cda257STejun Heo 
6194f5cda257STejun Heo 	for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
6195f5cda257STejun Heo 		struct ata_port *ap = host->ports[i];
6196f5cda257STejun Heo 
6197f5cda257STejun Heo 		if (ppi[j])
6198f5cda257STejun Heo 			pi = ppi[j++];
6199f5cda257STejun Heo 
6200f5cda257STejun Heo 		ap->pio_mask = pi->pio_mask;
6201f5cda257STejun Heo 		ap->mwdma_mask = pi->mwdma_mask;
6202f5cda257STejun Heo 		ap->udma_mask = pi->udma_mask;
6203f5cda257STejun Heo 		ap->flags |= pi->flags;
62040c88758bSTejun Heo 		ap->link.flags |= pi->link_flags;
6205f5cda257STejun Heo 		ap->ops = pi->port_ops;
6206f5cda257STejun Heo 
6207f5cda257STejun Heo 		if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
6208f5cda257STejun Heo 			host->ops = pi->port_ops;
6209f5cda257STejun Heo 	}
6210f5cda257STejun Heo 
6211f5cda257STejun Heo 	return host;
6212f5cda257STejun Heo }
6213f5cda257STejun Heo 
6214b1c72916STejun Heo /**
6215b1c72916STejun Heo  *	ata_slave_link_init - initialize slave link
6216b1c72916STejun Heo  *	@ap: port to initialize slave link for
6217b1c72916STejun Heo  *
6218b1c72916STejun Heo  *	Create and initialize slave link for @ap.  This enables slave
6219b1c72916STejun Heo  *	link handling on the port.
6220b1c72916STejun Heo  *
6221b1c72916STejun Heo  *	In libata, a port contains links and a link contains devices.
6222b1c72916STejun Heo  *	There is single host link but if a PMP is attached to it,
6223b1c72916STejun Heo  *	there can be multiple fan-out links.  On SATA, there's usually
6224b1c72916STejun Heo  *	a single device connected to a link but PATA and SATA
6225b1c72916STejun Heo  *	controllers emulating TF based interface can have two - master
6226b1c72916STejun Heo  *	and slave.
6227b1c72916STejun Heo  *
6228b1c72916STejun Heo  *	However, there are a few controllers which don't fit into this
6229b1c72916STejun Heo  *	abstraction too well - SATA controllers which emulate TF
6230b1c72916STejun Heo  *	interface with both master and slave devices but also have
6231b1c72916STejun Heo  *	separate SCR register sets for each device.  These controllers
6232b1c72916STejun Heo  *	need separate links for physical link handling
6233b1c72916STejun Heo  *	(e.g. onlineness, link speed) but should be treated like a
6234b1c72916STejun Heo  *	traditional M/S controller for everything else (e.g. command
6235b1c72916STejun Heo  *	issue, softreset).
6236b1c72916STejun Heo  *
6237b1c72916STejun Heo  *	slave_link is libata's way of handling this class of
6238b1c72916STejun Heo  *	controllers without impacting core layer too much.  For
6239b1c72916STejun Heo  *	anything other than physical link handling, the default host
6240b1c72916STejun Heo  *	link is used for both master and slave.  For physical link
6241b1c72916STejun Heo  *	handling, separate @ap->slave_link is used.  All dirty details
6242b1c72916STejun Heo  *	are implemented inside libata core layer.  From LLD's POV, the
6243b1c72916STejun Heo  *	only difference is that prereset, hardreset and postreset are
6244b1c72916STejun Heo  *	called once more for the slave link, so the reset sequence
6245b1c72916STejun Heo  *	looks like the following.
6246b1c72916STejun Heo  *
6247b1c72916STejun Heo  *	prereset(M) -> prereset(S) -> hardreset(M) -> hardreset(S) ->
6248b1c72916STejun Heo  *	softreset(M) -> postreset(M) -> postreset(S)
6249b1c72916STejun Heo  *
6250b1c72916STejun Heo  *	Note that softreset is called only for the master.  Softreset
6251b1c72916STejun Heo  *	resets both M/S by definition, so SRST on master should handle
6252b1c72916STejun Heo  *	both (the standard method will work just fine).
6253b1c72916STejun Heo  *
6254b1c72916STejun Heo  *	LOCKING:
6255b1c72916STejun Heo  *	Should be called before host is registered.
6256b1c72916STejun Heo  *
6257b1c72916STejun Heo  *	RETURNS:
6258b1c72916STejun Heo  *	0 on success, -errno on failure.
6259b1c72916STejun Heo  */
6260b1c72916STejun Heo int ata_slave_link_init(struct ata_port *ap)
6261b1c72916STejun Heo {
6262b1c72916STejun Heo 	struct ata_link *link;
6263b1c72916STejun Heo 
6264b1c72916STejun Heo 	WARN_ON(ap->slave_link);
6265b1c72916STejun Heo 	WARN_ON(ap->flags & ATA_FLAG_PMP);
6266b1c72916STejun Heo 
6267b1c72916STejun Heo 	link = kzalloc(sizeof(*link), GFP_KERNEL);
6268b1c72916STejun Heo 	if (!link)
6269b1c72916STejun Heo 		return -ENOMEM;
6270b1c72916STejun Heo 
6271b1c72916STejun Heo 	ata_link_init(ap, link, 1);
6272b1c72916STejun Heo 	ap->slave_link = link;
6273b1c72916STejun Heo 	return 0;
6274b1c72916STejun Heo }
6275b1c72916STejun Heo 
627632ebbc0cSTejun Heo static void ata_host_stop(struct device *gendev, void *res)
627732ebbc0cSTejun Heo {
627832ebbc0cSTejun Heo 	struct ata_host *host = dev_get_drvdata(gendev);
627932ebbc0cSTejun Heo 	int i;
628032ebbc0cSTejun Heo 
628132ebbc0cSTejun Heo 	WARN_ON(!(host->flags & ATA_HOST_STARTED));
628232ebbc0cSTejun Heo 
628332ebbc0cSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
628432ebbc0cSTejun Heo 		struct ata_port *ap = host->ports[i];
628532ebbc0cSTejun Heo 
628632ebbc0cSTejun Heo 		if (ap->ops->port_stop)
628732ebbc0cSTejun Heo 			ap->ops->port_stop(ap);
628832ebbc0cSTejun Heo 	}
628932ebbc0cSTejun Heo 
629032ebbc0cSTejun Heo 	if (host->ops->host_stop)
629132ebbc0cSTejun Heo 		host->ops->host_stop(host);
629232ebbc0cSTejun Heo }
629332ebbc0cSTejun Heo 
6294f5cda257STejun Heo /**
6295029cfd6bSTejun Heo  *	ata_finalize_port_ops - finalize ata_port_operations
6296029cfd6bSTejun Heo  *	@ops: ata_port_operations to finalize
6297029cfd6bSTejun Heo  *
6298029cfd6bSTejun Heo  *	An ata_port_operations can inherit from another ops and that
6299029cfd6bSTejun Heo  *	ops can again inherit from another.  This can go on as many
6300029cfd6bSTejun Heo  *	times as necessary as long as there is no loop in the
6301029cfd6bSTejun Heo  *	inheritance chain.
6302029cfd6bSTejun Heo  *
6303029cfd6bSTejun Heo  *	Ops tables are finalized when the host is started.  NULL or
6304029cfd6bSTejun Heo  *	unspecified entries are inherited from the closet ancestor
6305029cfd6bSTejun Heo  *	which has the method and the entry is populated with it.
6306029cfd6bSTejun Heo  *	After finalization, the ops table directly points to all the
6307029cfd6bSTejun Heo  *	methods and ->inherits is no longer necessary and cleared.
6308029cfd6bSTejun Heo  *
6309029cfd6bSTejun Heo  *	Using ATA_OP_NULL, inheriting ops can force a method to NULL.
6310029cfd6bSTejun Heo  *
6311029cfd6bSTejun Heo  *	LOCKING:
6312029cfd6bSTejun Heo  *	None.
6313029cfd6bSTejun Heo  */
6314029cfd6bSTejun Heo static void ata_finalize_port_ops(struct ata_port_operations *ops)
6315029cfd6bSTejun Heo {
63162da67659SPradeep Singh Rautela 	static DEFINE_SPINLOCK(lock);
6317029cfd6bSTejun Heo 	const struct ata_port_operations *cur;
6318029cfd6bSTejun Heo 	void **begin = (void **)ops;
6319029cfd6bSTejun Heo 	void **end = (void **)&ops->inherits;
6320029cfd6bSTejun Heo 	void **pp;
6321029cfd6bSTejun Heo 
6322029cfd6bSTejun Heo 	if (!ops || !ops->inherits)
6323029cfd6bSTejun Heo 		return;
6324029cfd6bSTejun Heo 
6325029cfd6bSTejun Heo 	spin_lock(&lock);
6326029cfd6bSTejun Heo 
6327029cfd6bSTejun Heo 	for (cur = ops->inherits; cur; cur = cur->inherits) {
6328029cfd6bSTejun Heo 		void **inherit = (void **)cur;
6329029cfd6bSTejun Heo 
6330029cfd6bSTejun Heo 		for (pp = begin; pp < end; pp++, inherit++)
6331029cfd6bSTejun Heo 			if (!*pp)
6332029cfd6bSTejun Heo 				*pp = *inherit;
6333029cfd6bSTejun Heo 	}
6334029cfd6bSTejun Heo 
6335029cfd6bSTejun Heo 	for (pp = begin; pp < end; pp++)
6336029cfd6bSTejun Heo 		if (IS_ERR(*pp))
6337029cfd6bSTejun Heo 			*pp = NULL;
6338029cfd6bSTejun Heo 
6339029cfd6bSTejun Heo 	ops->inherits = NULL;
6340029cfd6bSTejun Heo 
6341029cfd6bSTejun Heo 	spin_unlock(&lock);
6342029cfd6bSTejun Heo }
6343029cfd6bSTejun Heo 
6344029cfd6bSTejun Heo /**
6345ecef7253STejun Heo  *	ata_host_start - start and freeze ports of an ATA host
6346ecef7253STejun Heo  *	@host: ATA host to start ports for
6347ecef7253STejun Heo  *
6348ecef7253STejun Heo  *	Start and then freeze ports of @host.  Started status is
6349ecef7253STejun Heo  *	recorded in host->flags, so this function can be called
6350ecef7253STejun Heo  *	multiple times.  Ports are guaranteed to get started only
6351f3187195STejun Heo  *	once.  If host->ops isn't initialized yet, its set to the
6352f3187195STejun Heo  *	first non-dummy port ops.
6353ecef7253STejun Heo  *
6354ecef7253STejun Heo  *	LOCKING:
6355ecef7253STejun Heo  *	Inherited from calling layer (may sleep).
6356ecef7253STejun Heo  *
6357ecef7253STejun Heo  *	RETURNS:
6358ecef7253STejun Heo  *	0 if all ports are started successfully, -errno otherwise.
6359ecef7253STejun Heo  */
6360ecef7253STejun Heo int ata_host_start(struct ata_host *host)
6361ecef7253STejun Heo {
636232ebbc0cSTejun Heo 	int have_stop = 0;
636332ebbc0cSTejun Heo 	void *start_dr = NULL;
6364ecef7253STejun Heo 	int i, rc;
6365ecef7253STejun Heo 
6366ecef7253STejun Heo 	if (host->flags & ATA_HOST_STARTED)
6367ecef7253STejun Heo 		return 0;
6368ecef7253STejun Heo 
6369029cfd6bSTejun Heo 	ata_finalize_port_ops(host->ops);
6370029cfd6bSTejun Heo 
6371ecef7253STejun Heo 	for (i = 0; i < host->n_ports; i++) {
6372ecef7253STejun Heo 		struct ata_port *ap = host->ports[i];
6373ecef7253STejun Heo 
6374029cfd6bSTejun Heo 		ata_finalize_port_ops(ap->ops);
6375029cfd6bSTejun Heo 
6376f3187195STejun Heo 		if (!host->ops && !ata_port_is_dummy(ap))
6377f3187195STejun Heo 			host->ops = ap->ops;
6378f3187195STejun Heo 
637932ebbc0cSTejun Heo 		if (ap->ops->port_stop)
638032ebbc0cSTejun Heo 			have_stop = 1;
638132ebbc0cSTejun Heo 	}
638232ebbc0cSTejun Heo 
638332ebbc0cSTejun Heo 	if (host->ops->host_stop)
638432ebbc0cSTejun Heo 		have_stop = 1;
638532ebbc0cSTejun Heo 
638632ebbc0cSTejun Heo 	if (have_stop) {
638732ebbc0cSTejun Heo 		start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
638832ebbc0cSTejun Heo 		if (!start_dr)
638932ebbc0cSTejun Heo 			return -ENOMEM;
639032ebbc0cSTejun Heo 	}
639132ebbc0cSTejun Heo 
639232ebbc0cSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
639332ebbc0cSTejun Heo 		struct ata_port *ap = host->ports[i];
639432ebbc0cSTejun Heo 
6395ecef7253STejun Heo 		if (ap->ops->port_start) {
6396ecef7253STejun Heo 			rc = ap->ops->port_start(ap);
6397ecef7253STejun Heo 			if (rc) {
63980f9fe9b7SAlan Cox 				if (rc != -ENODEV)
6399a44fec1fSJoe Perches 					dev_err(host->dev,
6400a44fec1fSJoe Perches 						"failed to start port %d (errno=%d)\n",
6401a44fec1fSJoe Perches 						i, rc);
6402ecef7253STejun Heo 				goto err_out;
6403ecef7253STejun Heo 			}
6404ecef7253STejun Heo 		}
6405ecef7253STejun Heo 		ata_eh_freeze_port(ap);
6406ecef7253STejun Heo 	}
6407ecef7253STejun Heo 
640832ebbc0cSTejun Heo 	if (start_dr)
640932ebbc0cSTejun Heo 		devres_add(host->dev, start_dr);
6410ecef7253STejun Heo 	host->flags |= ATA_HOST_STARTED;
6411ecef7253STejun Heo 	return 0;
6412ecef7253STejun Heo 
6413ecef7253STejun Heo  err_out:
6414ecef7253STejun Heo 	while (--i >= 0) {
6415ecef7253STejun Heo 		struct ata_port *ap = host->ports[i];
6416ecef7253STejun Heo 
6417ecef7253STejun Heo 		if (ap->ops->port_stop)
6418ecef7253STejun Heo 			ap->ops->port_stop(ap);
6419ecef7253STejun Heo 	}
642032ebbc0cSTejun Heo 	devres_free(start_dr);
6421ecef7253STejun Heo 	return rc;
6422ecef7253STejun Heo }
6423ecef7253STejun Heo 
6424ecef7253STejun Heo /**
64258d8e7d13SDan Williams  *	ata_sas_host_init - Initialize a host struct for sas (ipr, libsas)
6426cca3974eSJeff Garzik  *	@host:	host to initialize
6427cca3974eSJeff Garzik  *	@dev:	device host is attached to
6428c6fd2807SJeff Garzik  *	@ops:	port_ops
6429c6fd2807SJeff Garzik  *
6430c6fd2807SJeff Garzik  */
6431cca3974eSJeff Garzik void ata_host_init(struct ata_host *host, struct device *dev,
64328d8e7d13SDan Williams 		   struct ata_port_operations *ops)
6433c6fd2807SJeff Garzik {
6434cca3974eSJeff Garzik 	spin_lock_init(&host->lock);
6435c0c362b6STejun Heo 	mutex_init(&host->eh_mutex);
643669278f79SJens Axboe 	host->n_tags = ATA_MAX_QUEUE;
6437cca3974eSJeff Garzik 	host->dev = dev;
6438cca3974eSJeff Garzik 	host->ops = ops;
64392fa4a326SJason Yan 	kref_init(&host->kref);
6440c6fd2807SJeff Garzik }
6441c6fd2807SJeff Garzik 
64429508a66fSDan Williams void __ata_port_probe(struct ata_port *ap)
644379318057SArjan van de Ven {
644479318057SArjan van de Ven 	struct ata_eh_info *ehi = &ap->link.eh_info;
644579318057SArjan van de Ven 	unsigned long flags;
644679318057SArjan van de Ven 
644779318057SArjan van de Ven 	/* kick EH for boot probing */
644879318057SArjan van de Ven 	spin_lock_irqsave(ap->lock, flags);
644979318057SArjan van de Ven 
645079318057SArjan van de Ven 	ehi->probe_mask |= ATA_ALL_DEVICES;
64516b7ae954STejun Heo 	ehi->action |= ATA_EH_RESET;
645279318057SArjan van de Ven 	ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
645379318057SArjan van de Ven 
645479318057SArjan van de Ven 	ap->pflags &= ~ATA_PFLAG_INITIALIZING;
645579318057SArjan van de Ven 	ap->pflags |= ATA_PFLAG_LOADING;
645679318057SArjan van de Ven 	ata_port_schedule_eh(ap);
645779318057SArjan van de Ven 
645879318057SArjan van de Ven 	spin_unlock_irqrestore(ap->lock, flags);
64599508a66fSDan Williams }
646079318057SArjan van de Ven 
64619508a66fSDan Williams int ata_port_probe(struct ata_port *ap)
64629508a66fSDan Williams {
64639508a66fSDan Williams 	int rc = 0;
64649508a66fSDan Williams 
64659508a66fSDan Williams 	if (ap->ops->error_handler) {
64669508a66fSDan Williams 		__ata_port_probe(ap);
646779318057SArjan van de Ven 		ata_port_wait_eh(ap);
646879318057SArjan van de Ven 	} else {
646979318057SArjan van de Ven 		DPRINTK("ata%u: bus probe begin\n", ap->print_id);
647079318057SArjan van de Ven 		rc = ata_bus_probe(ap);
647179318057SArjan van de Ven 		DPRINTK("ata%u: bus probe end\n", ap->print_id);
6472238c9cf9SJames Bottomley 	}
6473238c9cf9SJames Bottomley 	return rc;
6474238c9cf9SJames Bottomley }
647579318057SArjan van de Ven 
6476238c9cf9SJames Bottomley 
6477238c9cf9SJames Bottomley static void async_port_probe(void *data, async_cookie_t cookie)
6478238c9cf9SJames Bottomley {
6479238c9cf9SJames Bottomley 	struct ata_port *ap = data;
6480238c9cf9SJames Bottomley 
6481238c9cf9SJames Bottomley 	/*
6482238c9cf9SJames Bottomley 	 * If we're not allowed to scan this host in parallel,
6483238c9cf9SJames Bottomley 	 * we need to wait until all previous scans have completed
6484238c9cf9SJames Bottomley 	 * before going further.
6485238c9cf9SJames Bottomley 	 * Jeff Garzik says this is only within a controller, so we
6486238c9cf9SJames Bottomley 	 * don't need to wait for port 0, only for later ports.
648779318057SArjan van de Ven 	 */
6488238c9cf9SJames Bottomley 	if (!(ap->host->flags & ATA_HOST_PARALLEL_SCAN) && ap->port_no != 0)
6489238c9cf9SJames Bottomley 		async_synchronize_cookie(cookie);
6490238c9cf9SJames Bottomley 
6491238c9cf9SJames Bottomley 	(void)ata_port_probe(ap);
6492f29d3b23SArjan van de Ven 
6493f29d3b23SArjan van de Ven 	/* in order to keep device order, we need to synchronize at this point */
6494f29d3b23SArjan van de Ven 	async_synchronize_cookie(cookie);
6495f29d3b23SArjan van de Ven 
6496f29d3b23SArjan van de Ven 	ata_scsi_scan_host(ap, 1);
649779318057SArjan van de Ven }
6498238c9cf9SJames Bottomley 
6499c6fd2807SJeff Garzik /**
6500f3187195STejun Heo  *	ata_host_register - register initialized ATA host
6501f3187195STejun Heo  *	@host: ATA host to register
6502f3187195STejun Heo  *	@sht: template for SCSI host
6503c6fd2807SJeff Garzik  *
6504f3187195STejun Heo  *	Register initialized ATA host.  @host is allocated using
6505f3187195STejun Heo  *	ata_host_alloc() and fully initialized by LLD.  This function
6506f3187195STejun Heo  *	starts ports, registers @host with ATA and SCSI layers and
6507f3187195STejun Heo  *	probe registered devices.
6508c6fd2807SJeff Garzik  *
6509c6fd2807SJeff Garzik  *	LOCKING:
6510f3187195STejun Heo  *	Inherited from calling layer (may sleep).
6511c6fd2807SJeff Garzik  *
6512c6fd2807SJeff Garzik  *	RETURNS:
6513f3187195STejun Heo  *	0 on success, -errno otherwise.
6514c6fd2807SJeff Garzik  */
6515f3187195STejun Heo int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
6516c6fd2807SJeff Garzik {
6517f3187195STejun Heo 	int i, rc;
6518c6fd2807SJeff Garzik 
651969278f79SJens Axboe 	host->n_tags = clamp(sht->can_queue, 1, ATA_MAX_QUEUE);
65201871ee13SKevin Hao 
6521f3187195STejun Heo 	/* host must have been started */
6522f3187195STejun Heo 	if (!(host->flags & ATA_HOST_STARTED)) {
6523a44fec1fSJoe Perches 		dev_err(host->dev, "BUG: trying to register unstarted host\n");
6524f3187195STejun Heo 		WARN_ON(1);
6525f3187195STejun Heo 		return -EINVAL;
652602f076aaSAlan Cox 	}
6527f0d36efdSTejun Heo 
6528f3187195STejun Heo 	/* Blow away unused ports.  This happens when LLD can't
6529f3187195STejun Heo 	 * determine the exact number of ports to allocate at
6530f3187195STejun Heo 	 * allocation time.
6531f3187195STejun Heo 	 */
6532f3187195STejun Heo 	for (i = host->n_ports; host->ports[i]; i++)
6533f3187195STejun Heo 		kfree(host->ports[i]);
6534f0d36efdSTejun Heo 
6535f3187195STejun Heo 	/* give ports names and add SCSI hosts */
6536e628dc99SDavid Milburn 	for (i = 0; i < host->n_ports; i++) {
653785d6725bSDan Williams 		host->ports[i]->print_id = atomic_inc_return(&ata_print_id);
6538e628dc99SDavid Milburn 		host->ports[i]->local_port_no = i + 1;
6539e628dc99SDavid Milburn 	}
6540d9027470SGwendal Grignou 
6541d9027470SGwendal Grignou 	/* Create associated sysfs transport objects  */
6542d9027470SGwendal Grignou 	for (i = 0; i < host->n_ports; i++) {
6543d9027470SGwendal Grignou 		rc = ata_tport_add(host->dev,host->ports[i]);
6544d9027470SGwendal Grignou 		if (rc) {
6545d9027470SGwendal Grignou 			goto err_tadd;
6546d9027470SGwendal Grignou 		}
6547d9027470SGwendal Grignou 	}
6548d9027470SGwendal Grignou 
6549f3187195STejun Heo 	rc = ata_scsi_add_hosts(host, sht);
6550ecef7253STejun Heo 	if (rc)
6551d9027470SGwendal Grignou 		goto err_tadd;
6552ecef7253STejun Heo 
6553f3187195STejun Heo 	/* set cable, sata_spd_limit and report */
6554cca3974eSJeff Garzik 	for (i = 0; i < host->n_ports; i++) {
6555cca3974eSJeff Garzik 		struct ata_port *ap = host->ports[i];
6556f3187195STejun Heo 		unsigned long xfer_mask;
6557f3187195STejun Heo 
6558f3187195STejun Heo 		/* set SATA cable type if still unset */
6559f3187195STejun Heo 		if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
6560f3187195STejun Heo 			ap->cbl = ATA_CBL_SATA;
6561c6fd2807SJeff Garzik 
6562c6fd2807SJeff Garzik 		/* init sata_spd_limit to the current value */
65634fb37a25STejun Heo 		sata_link_init_spd(&ap->link);
6564b1c72916STejun Heo 		if (ap->slave_link)
6565b1c72916STejun Heo 			sata_link_init_spd(ap->slave_link);
6566c6fd2807SJeff Garzik 
6567cbcdd875STejun Heo 		/* print per-port info to dmesg */
6568f3187195STejun Heo 		xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
6569f3187195STejun Heo 					      ap->udma_mask);
6570f3187195STejun Heo 
6571abf6e8edSTejun Heo 		if (!ata_port_is_dummy(ap)) {
6572a9a79dfeSJoe Perches 			ata_port_info(ap, "%cATA max %s %s\n",
6573a16abc0bSTejun Heo 				      (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
6574f3187195STejun Heo 				      ata_mode_string(xfer_mask),
6575cbcdd875STejun Heo 				      ap->link.eh_info.desc);
6576abf6e8edSTejun Heo 			ata_ehi_clear_desc(&ap->link.eh_info);
6577abf6e8edSTejun Heo 		} else
6578a9a79dfeSJoe Perches 			ata_port_info(ap, "DUMMY\n");
6579c6fd2807SJeff Garzik 	}
6580c6fd2807SJeff Garzik 
6581f6005354SVegard Nossum 	/* perform each probe asynchronously */
6582f3187195STejun Heo 	for (i = 0; i < host->n_ports; i++) {
6583f3187195STejun Heo 		struct ata_port *ap = host->ports[i];
658479318057SArjan van de Ven 		async_schedule(async_port_probe, ap);
6585c6fd2807SJeff Garzik 	}
6586c6fd2807SJeff Garzik 
6587f3187195STejun Heo 	return 0;
6588d9027470SGwendal Grignou 
6589d9027470SGwendal Grignou  err_tadd:
6590d9027470SGwendal Grignou 	while (--i >= 0) {
6591d9027470SGwendal Grignou 		ata_tport_delete(host->ports[i]);
6592d9027470SGwendal Grignou 	}
6593d9027470SGwendal Grignou 	return rc;
6594d9027470SGwendal Grignou 
6595f3187195STejun Heo }
6596f3187195STejun Heo 
6597f3187195STejun Heo /**
6598f5cda257STejun Heo  *	ata_host_activate - start host, request IRQ and register it
6599f5cda257STejun Heo  *	@host: target ATA host
6600f5cda257STejun Heo  *	@irq: IRQ to request
6601f5cda257STejun Heo  *	@irq_handler: irq_handler used when requesting IRQ
6602f5cda257STejun Heo  *	@irq_flags: irq_flags used when requesting IRQ
6603f5cda257STejun Heo  *	@sht: scsi_host_template to use when registering the host
6604f5cda257STejun Heo  *
6605f5cda257STejun Heo  *	After allocating an ATA host and initializing it, most libata
6606f5cda257STejun Heo  *	LLDs perform three steps to activate the host - start host,
6607c9b5560aSMasanari Iida  *	request IRQ and register it.  This helper takes necessary
6608f5cda257STejun Heo  *	arguments and performs the three steps in one go.
6609f5cda257STejun Heo  *
66103d46b2e2SPaul Mundt  *	An invalid IRQ skips the IRQ registration and expects the host to
66113d46b2e2SPaul Mundt  *	have set polling mode on the port. In this case, @irq_handler
66123d46b2e2SPaul Mundt  *	should be NULL.
66133d46b2e2SPaul Mundt  *
6614f5cda257STejun Heo  *	LOCKING:
6615f5cda257STejun Heo  *	Inherited from calling layer (may sleep).
6616f5cda257STejun Heo  *
6617f5cda257STejun Heo  *	RETURNS:
6618f5cda257STejun Heo  *	0 on success, -errno otherwise.
6619f5cda257STejun Heo  */
6620f5cda257STejun Heo int ata_host_activate(struct ata_host *host, int irq,
6621f5cda257STejun Heo 		      irq_handler_t irq_handler, unsigned long irq_flags,
6622f5cda257STejun Heo 		      struct scsi_host_template *sht)
6623f5cda257STejun Heo {
6624cbcdd875STejun Heo 	int i, rc;
66257e22c002SHeiner Kallweit 	char *irq_desc;
6626f5cda257STejun Heo 
6627f5cda257STejun Heo 	rc = ata_host_start(host);
6628f5cda257STejun Heo 	if (rc)
6629f5cda257STejun Heo 		return rc;
6630f5cda257STejun Heo 
66313d46b2e2SPaul Mundt 	/* Special case for polling mode */
66323d46b2e2SPaul Mundt 	if (!irq) {
66333d46b2e2SPaul Mundt 		WARN_ON(irq_handler);
66343d46b2e2SPaul Mundt 		return ata_host_register(host, sht);
66353d46b2e2SPaul Mundt 	}
66363d46b2e2SPaul Mundt 
66377e22c002SHeiner Kallweit 	irq_desc = devm_kasprintf(host->dev, GFP_KERNEL, "%s[%s]",
66387e22c002SHeiner Kallweit 				  dev_driver_string(host->dev),
66397e22c002SHeiner Kallweit 				  dev_name(host->dev));
66407e22c002SHeiner Kallweit 	if (!irq_desc)
66417e22c002SHeiner Kallweit 		return -ENOMEM;
66427e22c002SHeiner Kallweit 
6643f5cda257STejun Heo 	rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
66447e22c002SHeiner Kallweit 			      irq_desc, host);
6645f5cda257STejun Heo 	if (rc)
6646f5cda257STejun Heo 		return rc;
6647f5cda257STejun Heo 
6648cbcdd875STejun Heo 	for (i = 0; i < host->n_ports; i++)
6649cbcdd875STejun Heo 		ata_port_desc(host->ports[i], "irq %d", irq);
66504031826bSTejun Heo 
6651f5cda257STejun Heo 	rc = ata_host_register(host, sht);
6652f5cda257STejun Heo 	/* if failed, just free the IRQ and leave ports alone */
6653f5cda257STejun Heo 	if (rc)
6654f5cda257STejun Heo 		devm_free_irq(host->dev, irq, host);
6655f5cda257STejun Heo 
6656f5cda257STejun Heo 	return rc;
6657f5cda257STejun Heo }
6658f5cda257STejun Heo 
6659f5cda257STejun Heo /**
6660c9b5560aSMasanari Iida  *	ata_port_detach - Detach ATA port in preparation of device removal
6661c6fd2807SJeff Garzik  *	@ap: ATA port to be detached
6662c6fd2807SJeff Garzik  *
6663c6fd2807SJeff Garzik  *	Detach all ATA devices and the associated SCSI devices of @ap;
6664c6fd2807SJeff Garzik  *	then, remove the associated SCSI host.  @ap is guaranteed to
6665c6fd2807SJeff Garzik  *	be quiescent on return from this function.
6666c6fd2807SJeff Garzik  *
6667c6fd2807SJeff Garzik  *	LOCKING:
6668c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
6669c6fd2807SJeff Garzik  */
6670741b7763SAdrian Bunk static void ata_port_detach(struct ata_port *ap)
6671c6fd2807SJeff Garzik {
6672c6fd2807SJeff Garzik 	unsigned long flags;
6673a6f9bf4dSLevente Kurusa 	struct ata_link *link;
6674a6f9bf4dSLevente Kurusa 	struct ata_device *dev;
6675c6fd2807SJeff Garzik 
6676c6fd2807SJeff Garzik 	if (!ap->ops->error_handler)
6677c6fd2807SJeff Garzik 		goto skip_eh;
6678c6fd2807SJeff Garzik 
6679c6fd2807SJeff Garzik 	/* tell EH we're leaving & flush EH */
6680c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
6681c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_UNLOADING;
6682ece180d1STejun Heo 	ata_port_schedule_eh(ap);
6683c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
6684c6fd2807SJeff Garzik 
6685ece180d1STejun Heo 	/* wait till EH commits suicide */
6686c6fd2807SJeff Garzik 	ata_port_wait_eh(ap);
6687c6fd2807SJeff Garzik 
6688ece180d1STejun Heo 	/* it better be dead now */
6689ece180d1STejun Heo 	WARN_ON(!(ap->pflags & ATA_PFLAG_UNLOADED));
6690c6fd2807SJeff Garzik 
6691afe2c511STejun Heo 	cancel_delayed_work_sync(&ap->hotplug_task);
6692c6fd2807SJeff Garzik 
6693c6fd2807SJeff Garzik  skip_eh:
6694a6f9bf4dSLevente Kurusa 	/* clean up zpodd on port removal */
6695a6f9bf4dSLevente Kurusa 	ata_for_each_link(link, ap, HOST_FIRST) {
6696a6f9bf4dSLevente Kurusa 		ata_for_each_dev(dev, link, ALL) {
6697a6f9bf4dSLevente Kurusa 			if (zpodd_dev_enabled(dev))
6698a6f9bf4dSLevente Kurusa 				zpodd_exit(dev);
6699a6f9bf4dSLevente Kurusa 		}
6700a6f9bf4dSLevente Kurusa 	}
6701d9027470SGwendal Grignou 	if (ap->pmp_link) {
6702d9027470SGwendal Grignou 		int i;
6703d9027470SGwendal Grignou 		for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
6704d9027470SGwendal Grignou 			ata_tlink_delete(&ap->pmp_link[i]);
6705d9027470SGwendal Grignou 	}
6706c6fd2807SJeff Garzik 	/* remove the associated SCSI host */
6707cca3974eSJeff Garzik 	scsi_remove_host(ap->scsi_host);
6708c5700766SRafael J. Wysocki 	ata_tport_delete(ap);
6709c6fd2807SJeff Garzik }
6710c6fd2807SJeff Garzik 
6711c6fd2807SJeff Garzik /**
67120529c159STejun Heo  *	ata_host_detach - Detach all ports of an ATA host
67130529c159STejun Heo  *	@host: Host to detach
67140529c159STejun Heo  *
67150529c159STejun Heo  *	Detach all ports of @host.
67160529c159STejun Heo  *
67170529c159STejun Heo  *	LOCKING:
67180529c159STejun Heo  *	Kernel thread context (may sleep).
67190529c159STejun Heo  */
67200529c159STejun Heo void ata_host_detach(struct ata_host *host)
67210529c159STejun Heo {
67220529c159STejun Heo 	int i;
67230529c159STejun Heo 
67240529c159STejun Heo 	for (i = 0; i < host->n_ports; i++)
67250529c159STejun Heo 		ata_port_detach(host->ports[i]);
6726562f0c2dSTejun Heo 
6727562f0c2dSTejun Heo 	/* the host is dead now, dissociate ACPI */
6728562f0c2dSTejun Heo 	ata_acpi_dissociate(host);
67290529c159STejun Heo }
67300529c159STejun Heo 
6731c6fd2807SJeff Garzik #ifdef CONFIG_PCI
6732c6fd2807SJeff Garzik 
6733c6fd2807SJeff Garzik /**
6734c6fd2807SJeff Garzik  *	ata_pci_remove_one - PCI layer callback for device removal
6735c6fd2807SJeff Garzik  *	@pdev: PCI device that was removed
6736c6fd2807SJeff Garzik  *
6737b878ca5dSTejun Heo  *	PCI layer indicates to libata via this hook that hot-unplug or
6738b878ca5dSTejun Heo  *	module unload event has occurred.  Detach all ports.  Resource
6739b878ca5dSTejun Heo  *	release is handled via devres.
6740c6fd2807SJeff Garzik  *
6741c6fd2807SJeff Garzik  *	LOCKING:
6742c6fd2807SJeff Garzik  *	Inherited from PCI layer (may sleep).
6743c6fd2807SJeff Garzik  */
6744c6fd2807SJeff Garzik void ata_pci_remove_one(struct pci_dev *pdev)
6745c6fd2807SJeff Garzik {
674604a3f5b7SBrian Norris 	struct ata_host *host = pci_get_drvdata(pdev);
6747c6fd2807SJeff Garzik 
6748f0d36efdSTejun Heo 	ata_host_detach(host);
6749c6fd2807SJeff Garzik }
6750c6fd2807SJeff Garzik 
6751c6fd2807SJeff Garzik /* move to PCI subsystem */
6752c6fd2807SJeff Garzik int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
6753c6fd2807SJeff Garzik {
6754c6fd2807SJeff Garzik 	unsigned long tmp = 0;
6755c6fd2807SJeff Garzik 
6756c6fd2807SJeff Garzik 	switch (bits->width) {
6757c6fd2807SJeff Garzik 	case 1: {
6758c6fd2807SJeff Garzik 		u8 tmp8 = 0;
6759c6fd2807SJeff Garzik 		pci_read_config_byte(pdev, bits->reg, &tmp8);
6760c6fd2807SJeff Garzik 		tmp = tmp8;
6761c6fd2807SJeff Garzik 		break;
6762c6fd2807SJeff Garzik 	}
6763c6fd2807SJeff Garzik 	case 2: {
6764c6fd2807SJeff Garzik 		u16 tmp16 = 0;
6765c6fd2807SJeff Garzik 		pci_read_config_word(pdev, bits->reg, &tmp16);
6766c6fd2807SJeff Garzik 		tmp = tmp16;
6767c6fd2807SJeff Garzik 		break;
6768c6fd2807SJeff Garzik 	}
6769c6fd2807SJeff Garzik 	case 4: {
6770c6fd2807SJeff Garzik 		u32 tmp32 = 0;
6771c6fd2807SJeff Garzik 		pci_read_config_dword(pdev, bits->reg, &tmp32);
6772c6fd2807SJeff Garzik 		tmp = tmp32;
6773c6fd2807SJeff Garzik 		break;
6774c6fd2807SJeff Garzik 	}
6775c6fd2807SJeff Garzik 
6776c6fd2807SJeff Garzik 	default:
6777c6fd2807SJeff Garzik 		return -EINVAL;
6778c6fd2807SJeff Garzik 	}
6779c6fd2807SJeff Garzik 
6780c6fd2807SJeff Garzik 	tmp &= bits->mask;
6781c6fd2807SJeff Garzik 
6782c6fd2807SJeff Garzik 	return (tmp == bits->val) ? 1 : 0;
6783c6fd2807SJeff Garzik }
6784c6fd2807SJeff Garzik 
67856ffa01d8STejun Heo #ifdef CONFIG_PM
6786c6fd2807SJeff Garzik void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
6787c6fd2807SJeff Garzik {
6788c6fd2807SJeff Garzik 	pci_save_state(pdev);
6789c6fd2807SJeff Garzik 	pci_disable_device(pdev);
67904c90d971STejun Heo 
67913a2d5b70SRafael J. Wysocki 	if (mesg.event & PM_EVENT_SLEEP)
6792c6fd2807SJeff Garzik 		pci_set_power_state(pdev, PCI_D3hot);
6793c6fd2807SJeff Garzik }
6794c6fd2807SJeff Garzik 
6795553c4aa6STejun Heo int ata_pci_device_do_resume(struct pci_dev *pdev)
6796c6fd2807SJeff Garzik {
6797553c4aa6STejun Heo 	int rc;
6798553c4aa6STejun Heo 
6799c6fd2807SJeff Garzik 	pci_set_power_state(pdev, PCI_D0);
6800c6fd2807SJeff Garzik 	pci_restore_state(pdev);
6801553c4aa6STejun Heo 
6802f0d36efdSTejun Heo 	rc = pcim_enable_device(pdev);
6803553c4aa6STejun Heo 	if (rc) {
6804a44fec1fSJoe Perches 		dev_err(&pdev->dev,
6805553c4aa6STejun Heo 			"failed to enable device after resume (%d)\n", rc);
6806553c4aa6STejun Heo 		return rc;
6807553c4aa6STejun Heo 	}
6808553c4aa6STejun Heo 
6809c6fd2807SJeff Garzik 	pci_set_master(pdev);
6810553c4aa6STejun Heo 	return 0;
6811c6fd2807SJeff Garzik }
6812c6fd2807SJeff Garzik 
6813c6fd2807SJeff Garzik int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
6814c6fd2807SJeff Garzik {
681504a3f5b7SBrian Norris 	struct ata_host *host = pci_get_drvdata(pdev);
6816c6fd2807SJeff Garzik 	int rc = 0;
6817c6fd2807SJeff Garzik 
6818cca3974eSJeff Garzik 	rc = ata_host_suspend(host, mesg);
6819c6fd2807SJeff Garzik 	if (rc)
6820c6fd2807SJeff Garzik 		return rc;
6821c6fd2807SJeff Garzik 
6822c6fd2807SJeff Garzik 	ata_pci_device_do_suspend(pdev, mesg);
6823c6fd2807SJeff Garzik 
6824c6fd2807SJeff Garzik 	return 0;
6825c6fd2807SJeff Garzik }
6826c6fd2807SJeff Garzik 
6827c6fd2807SJeff Garzik int ata_pci_device_resume(struct pci_dev *pdev)
6828c6fd2807SJeff Garzik {
682904a3f5b7SBrian Norris 	struct ata_host *host = pci_get_drvdata(pdev);
6830553c4aa6STejun Heo 	int rc;
6831c6fd2807SJeff Garzik 
6832553c4aa6STejun Heo 	rc = ata_pci_device_do_resume(pdev);
6833553c4aa6STejun Heo 	if (rc == 0)
6834cca3974eSJeff Garzik 		ata_host_resume(host);
6835553c4aa6STejun Heo 	return rc;
6836c6fd2807SJeff Garzik }
68376ffa01d8STejun Heo #endif /* CONFIG_PM */
68386ffa01d8STejun Heo 
6839c6fd2807SJeff Garzik #endif /* CONFIG_PCI */
6840c6fd2807SJeff Garzik 
6841b7db04d9SBrian Norris /**
6842b7db04d9SBrian Norris  *	ata_platform_remove_one - Platform layer callback for device removal
6843b7db04d9SBrian Norris  *	@pdev: Platform device that was removed
6844b7db04d9SBrian Norris  *
6845b7db04d9SBrian Norris  *	Platform layer indicates to libata via this hook that hot-unplug or
6846b7db04d9SBrian Norris  *	module unload event has occurred.  Detach all ports.  Resource
6847b7db04d9SBrian Norris  *	release is handled via devres.
6848b7db04d9SBrian Norris  *
6849b7db04d9SBrian Norris  *	LOCKING:
6850b7db04d9SBrian Norris  *	Inherited from platform layer (may sleep).
6851b7db04d9SBrian Norris  */
6852b7db04d9SBrian Norris int ata_platform_remove_one(struct platform_device *pdev)
6853b7db04d9SBrian Norris {
6854b7db04d9SBrian Norris 	struct ata_host *host = platform_get_drvdata(pdev);
6855b7db04d9SBrian Norris 
6856b7db04d9SBrian Norris 	ata_host_detach(host);
6857b7db04d9SBrian Norris 
6858b7db04d9SBrian Norris 	return 0;
6859b7db04d9SBrian Norris }
6860b7db04d9SBrian Norris 
686133267325STejun Heo static int __init ata_parse_force_one(char **cur,
686233267325STejun Heo 				      struct ata_force_ent *force_ent,
686333267325STejun Heo 				      const char **reason)
686433267325STejun Heo {
68650f5f264bSRasmus Villemoes 	static const struct ata_force_param force_tbl[] __initconst = {
686633267325STejun Heo 		{ "40c",	.cbl		= ATA_CBL_PATA40 },
686733267325STejun Heo 		{ "80c",	.cbl		= ATA_CBL_PATA80 },
686833267325STejun Heo 		{ "short40c",	.cbl		= ATA_CBL_PATA40_SHORT },
686933267325STejun Heo 		{ "unk",	.cbl		= ATA_CBL_PATA_UNK },
687033267325STejun Heo 		{ "ign",	.cbl		= ATA_CBL_PATA_IGN },
687133267325STejun Heo 		{ "sata",	.cbl		= ATA_CBL_SATA },
687233267325STejun Heo 		{ "1.5Gbps",	.spd_limit	= 1 },
687333267325STejun Heo 		{ "3.0Gbps",	.spd_limit	= 2 },
687433267325STejun Heo 		{ "noncq",	.horkage_on	= ATA_HORKAGE_NONCQ },
687533267325STejun Heo 		{ "ncq",	.horkage_off	= ATA_HORKAGE_NONCQ },
6876d7b16e4fSMartin K. Petersen 		{ "noncqtrim",	.horkage_on	= ATA_HORKAGE_NO_NCQ_TRIM },
6877d7b16e4fSMartin K. Petersen 		{ "ncqtrim",	.horkage_off	= ATA_HORKAGE_NO_NCQ_TRIM },
687843c9c591STejun Heo 		{ "dump_id",	.horkage_on	= ATA_HORKAGE_DUMP_ID },
687933267325STejun Heo 		{ "pio0",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 0) },
688033267325STejun Heo 		{ "pio1",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 1) },
688133267325STejun Heo 		{ "pio2",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 2) },
688233267325STejun Heo 		{ "pio3",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 3) },
688333267325STejun Heo 		{ "pio4",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 4) },
688433267325STejun Heo 		{ "pio5",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 5) },
688533267325STejun Heo 		{ "pio6",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 6) },
688633267325STejun Heo 		{ "mwdma0",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 0) },
688733267325STejun Heo 		{ "mwdma1",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 1) },
688833267325STejun Heo 		{ "mwdma2",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 2) },
688933267325STejun Heo 		{ "mwdma3",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 3) },
689033267325STejun Heo 		{ "mwdma4",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 4) },
689133267325STejun Heo 		{ "udma0",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
689233267325STejun Heo 		{ "udma16",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
689333267325STejun Heo 		{ "udma/16",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
689433267325STejun Heo 		{ "udma1",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
689533267325STejun Heo 		{ "udma25",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
689633267325STejun Heo 		{ "udma/25",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
689733267325STejun Heo 		{ "udma2",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
689833267325STejun Heo 		{ "udma33",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
689933267325STejun Heo 		{ "udma/33",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
690033267325STejun Heo 		{ "udma3",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
690133267325STejun Heo 		{ "udma44",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
690233267325STejun Heo 		{ "udma/44",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
690333267325STejun Heo 		{ "udma4",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
690433267325STejun Heo 		{ "udma66",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
690533267325STejun Heo 		{ "udma/66",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
690633267325STejun Heo 		{ "udma5",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
690733267325STejun Heo 		{ "udma100",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
690833267325STejun Heo 		{ "udma/100",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
690933267325STejun Heo 		{ "udma6",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
691033267325STejun Heo 		{ "udma133",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
691133267325STejun Heo 		{ "udma/133",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
691233267325STejun Heo 		{ "udma7",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 7) },
691305944bdfSTejun Heo 		{ "nohrst",	.lflags		= ATA_LFLAG_NO_HRST },
691405944bdfSTejun Heo 		{ "nosrst",	.lflags		= ATA_LFLAG_NO_SRST },
691505944bdfSTejun Heo 		{ "norst",	.lflags		= ATA_LFLAG_NO_HRST | ATA_LFLAG_NO_SRST },
6916ca6d43b0SDan Williams 		{ "rstonce",	.lflags		= ATA_LFLAG_RST_ONCE },
6917966fbe19SVincent Pelletier 		{ "atapi_dmadir", .horkage_on	= ATA_HORKAGE_ATAPI_DMADIR },
6918b8bd6dc3SRobin H. Johnson 		{ "disable",	.horkage_on	= ATA_HORKAGE_DISABLE },
691933267325STejun Heo 	};
692033267325STejun Heo 	char *start = *cur, *p = *cur;
692133267325STejun Heo 	char *id, *val, *endp;
692233267325STejun Heo 	const struct ata_force_param *match_fp = NULL;
692333267325STejun Heo 	int nr_matches = 0, i;
692433267325STejun Heo 
692533267325STejun Heo 	/* find where this param ends and update *cur */
692633267325STejun Heo 	while (*p != '\0' && *p != ',')
692733267325STejun Heo 		p++;
692833267325STejun Heo 
692933267325STejun Heo 	if (*p == '\0')
693033267325STejun Heo 		*cur = p;
693133267325STejun Heo 	else
693233267325STejun Heo 		*cur = p + 1;
693333267325STejun Heo 
693433267325STejun Heo 	*p = '\0';
693533267325STejun Heo 
693633267325STejun Heo 	/* parse */
693733267325STejun Heo 	p = strchr(start, ':');
693833267325STejun Heo 	if (!p) {
693933267325STejun Heo 		val = strstrip(start);
694033267325STejun Heo 		goto parse_val;
694133267325STejun Heo 	}
694233267325STejun Heo 	*p = '\0';
694333267325STejun Heo 
694433267325STejun Heo 	id = strstrip(start);
694533267325STejun Heo 	val = strstrip(p + 1);
694633267325STejun Heo 
694733267325STejun Heo 	/* parse id */
694833267325STejun Heo 	p = strchr(id, '.');
694933267325STejun Heo 	if (p) {
695033267325STejun Heo 		*p++ = '\0';
695133267325STejun Heo 		force_ent->device = simple_strtoul(p, &endp, 10);
695233267325STejun Heo 		if (p == endp || *endp != '\0') {
695333267325STejun Heo 			*reason = "invalid device";
695433267325STejun Heo 			return -EINVAL;
695533267325STejun Heo 		}
695633267325STejun Heo 	}
695733267325STejun Heo 
695833267325STejun Heo 	force_ent->port = simple_strtoul(id, &endp, 10);
6959f7cf69aeSTejun Heo 	if (id == endp || *endp != '\0') {
696033267325STejun Heo 		*reason = "invalid port/link";
696133267325STejun Heo 		return -EINVAL;
696233267325STejun Heo 	}
696333267325STejun Heo 
696433267325STejun Heo  parse_val:
696533267325STejun Heo 	/* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */
696633267325STejun Heo 	for (i = 0; i < ARRAY_SIZE(force_tbl); i++) {
696733267325STejun Heo 		const struct ata_force_param *fp = &force_tbl[i];
696833267325STejun Heo 
696933267325STejun Heo 		if (strncasecmp(val, fp->name, strlen(val)))
697033267325STejun Heo 			continue;
697133267325STejun Heo 
697233267325STejun Heo 		nr_matches++;
697333267325STejun Heo 		match_fp = fp;
697433267325STejun Heo 
697533267325STejun Heo 		if (strcasecmp(val, fp->name) == 0) {
697633267325STejun Heo 			nr_matches = 1;
697733267325STejun Heo 			break;
697833267325STejun Heo 		}
697933267325STejun Heo 	}
698033267325STejun Heo 
698133267325STejun Heo 	if (!nr_matches) {
698233267325STejun Heo 		*reason = "unknown value";
698333267325STejun Heo 		return -EINVAL;
698433267325STejun Heo 	}
698533267325STejun Heo 	if (nr_matches > 1) {
69869de55351SArvind Yadav 		*reason = "ambiguous value";
698733267325STejun Heo 		return -EINVAL;
698833267325STejun Heo 	}
698933267325STejun Heo 
699033267325STejun Heo 	force_ent->param = *match_fp;
699133267325STejun Heo 
699233267325STejun Heo 	return 0;
699333267325STejun Heo }
699433267325STejun Heo 
699533267325STejun Heo static void __init ata_parse_force_param(void)
699633267325STejun Heo {
699733267325STejun Heo 	int idx = 0, size = 1;
699833267325STejun Heo 	int last_port = -1, last_device = -1;
699933267325STejun Heo 	char *p, *cur, *next;
700033267325STejun Heo 
700133267325STejun Heo 	/* calculate maximum number of params and allocate force_tbl */
700233267325STejun Heo 	for (p = ata_force_param_buf; *p; p++)
700333267325STejun Heo 		if (*p == ',')
700433267325STejun Heo 			size++;
700533267325STejun Heo 
70066396bb22SKees Cook 	ata_force_tbl = kcalloc(size, sizeof(ata_force_tbl[0]), GFP_KERNEL);
700733267325STejun Heo 	if (!ata_force_tbl) {
700833267325STejun Heo 		printk(KERN_WARNING "ata: failed to extend force table, "
700933267325STejun Heo 		       "libata.force ignored\n");
701033267325STejun Heo 		return;
701133267325STejun Heo 	}
701233267325STejun Heo 
701333267325STejun Heo 	/* parse and populate the table */
701433267325STejun Heo 	for (cur = ata_force_param_buf; *cur != '\0'; cur = next) {
701533267325STejun Heo 		const char *reason = "";
701633267325STejun Heo 		struct ata_force_ent te = { .port = -1, .device = -1 };
701733267325STejun Heo 
701833267325STejun Heo 		next = cur;
701933267325STejun Heo 		if (ata_parse_force_one(&next, &te, &reason)) {
702033267325STejun Heo 			printk(KERN_WARNING "ata: failed to parse force "
702133267325STejun Heo 			       "parameter \"%s\" (%s)\n",
702233267325STejun Heo 			       cur, reason);
702333267325STejun Heo 			continue;
702433267325STejun Heo 		}
702533267325STejun Heo 
702633267325STejun Heo 		if (te.port == -1) {
702733267325STejun Heo 			te.port = last_port;
702833267325STejun Heo 			te.device = last_device;
702933267325STejun Heo 		}
703033267325STejun Heo 
703133267325STejun Heo 		ata_force_tbl[idx++] = te;
703233267325STejun Heo 
703333267325STejun Heo 		last_port = te.port;
703433267325STejun Heo 		last_device = te.device;
703533267325STejun Heo 	}
703633267325STejun Heo 
703733267325STejun Heo 	ata_force_tbl_size = idx;
703833267325STejun Heo }
7039c6fd2807SJeff Garzik 
7040c6fd2807SJeff Garzik static int __init ata_init(void)
7041c6fd2807SJeff Garzik {
7042d9027470SGwendal Grignou 	int rc;
7043270390e1STejun Heo 
704433267325STejun Heo 	ata_parse_force_param();
704533267325STejun Heo 
7046270390e1STejun Heo 	rc = ata_sff_init();
7047ad72cf98STejun Heo 	if (rc) {
7048ad72cf98STejun Heo 		kfree(ata_force_tbl);
7049ad72cf98STejun Heo 		return rc;
7050ad72cf98STejun Heo 	}
7051c6fd2807SJeff Garzik 
7052d9027470SGwendal Grignou 	libata_transport_init();
7053d9027470SGwendal Grignou 	ata_scsi_transport_template = ata_attach_transport();
7054d9027470SGwendal Grignou 	if (!ata_scsi_transport_template) {
7055d9027470SGwendal Grignou 		ata_sff_exit();
7056d9027470SGwendal Grignou 		rc = -ENOMEM;
7057d9027470SGwendal Grignou 		goto err_out;
7058d9027470SGwendal Grignou 	}
7059d9027470SGwendal Grignou 
7060c6fd2807SJeff Garzik 	printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
7061c6fd2807SJeff Garzik 	return 0;
7062d9027470SGwendal Grignou 
7063d9027470SGwendal Grignou err_out:
7064d9027470SGwendal Grignou 	return rc;
7065c6fd2807SJeff Garzik }
7066c6fd2807SJeff Garzik 
7067c6fd2807SJeff Garzik static void __exit ata_exit(void)
7068c6fd2807SJeff Garzik {
7069d9027470SGwendal Grignou 	ata_release_transport(ata_scsi_transport_template);
7070d9027470SGwendal Grignou 	libata_transport_exit();
7071270390e1STejun Heo 	ata_sff_exit();
707233267325STejun Heo 	kfree(ata_force_tbl);
7073c6fd2807SJeff Garzik }
7074c6fd2807SJeff Garzik 
7075a4625085SBrian King subsys_initcall(ata_init);
7076c6fd2807SJeff Garzik module_exit(ata_exit);
7077c6fd2807SJeff Garzik 
70789990b6f3SAkinobu Mita static DEFINE_RATELIMIT_STATE(ratelimit, HZ / 5, 1);
7079c6fd2807SJeff Garzik 
7080c6fd2807SJeff Garzik int ata_ratelimit(void)
7081c6fd2807SJeff Garzik {
70829990b6f3SAkinobu Mita 	return __ratelimit(&ratelimit);
7083c6fd2807SJeff Garzik }
7084c6fd2807SJeff Garzik 
7085c0c362b6STejun Heo /**
7086c0c362b6STejun Heo  *	ata_msleep - ATA EH owner aware msleep
7087c0c362b6STejun Heo  *	@ap: ATA port to attribute the sleep to
7088c0c362b6STejun Heo  *	@msecs: duration to sleep in milliseconds
7089c0c362b6STejun Heo  *
7090c0c362b6STejun Heo  *	Sleeps @msecs.  If the current task is owner of @ap's EH, the
7091c0c362b6STejun Heo  *	ownership is released before going to sleep and reacquired
7092c0c362b6STejun Heo  *	after the sleep is complete.  IOW, other ports sharing the
7093c0c362b6STejun Heo  *	@ap->host will be allowed to own the EH while this task is
7094c0c362b6STejun Heo  *	sleeping.
7095c0c362b6STejun Heo  *
7096c0c362b6STejun Heo  *	LOCKING:
7097c0c362b6STejun Heo  *	Might sleep.
7098c0c362b6STejun Heo  */
709997750cebSTejun Heo void ata_msleep(struct ata_port *ap, unsigned int msecs)
710097750cebSTejun Heo {
7101c0c362b6STejun Heo 	bool owns_eh = ap && ap->host->eh_owner == current;
7102c0c362b6STejun Heo 
7103c0c362b6STejun Heo 	if (owns_eh)
7104c0c362b6STejun Heo 		ata_eh_release(ap);
7105c0c362b6STejun Heo 
7106848c3920SAnil Veliyankara Madam 	if (msecs < 20) {
7107848c3920SAnil Veliyankara Madam 		unsigned long usecs = msecs * USEC_PER_MSEC;
7108848c3920SAnil Veliyankara Madam 		usleep_range(usecs, usecs + 50);
7109848c3920SAnil Veliyankara Madam 	} else {
711097750cebSTejun Heo 		msleep(msecs);
7111848c3920SAnil Veliyankara Madam 	}
7112c0c362b6STejun Heo 
7113c0c362b6STejun Heo 	if (owns_eh)
7114c0c362b6STejun Heo 		ata_eh_acquire(ap);
711597750cebSTejun Heo }
711697750cebSTejun Heo 
7117c6fd2807SJeff Garzik /**
7118c6fd2807SJeff Garzik  *	ata_wait_register - wait until register value changes
711997750cebSTejun Heo  *	@ap: ATA port to wait register for, can be NULL
7120c6fd2807SJeff Garzik  *	@reg: IO-mapped register
7121c6fd2807SJeff Garzik  *	@mask: Mask to apply to read register value
7122c6fd2807SJeff Garzik  *	@val: Wait condition
7123341c2c95STejun Heo  *	@interval: polling interval in milliseconds
7124341c2c95STejun Heo  *	@timeout: timeout in milliseconds
7125c6fd2807SJeff Garzik  *
7126c6fd2807SJeff Garzik  *	Waiting for some bits of register to change is a common
7127c6fd2807SJeff Garzik  *	operation for ATA controllers.  This function reads 32bit LE
7128c6fd2807SJeff Garzik  *	IO-mapped register @reg and tests for the following condition.
7129c6fd2807SJeff Garzik  *
7130c6fd2807SJeff Garzik  *	(*@reg & mask) != val
7131c6fd2807SJeff Garzik  *
7132c6fd2807SJeff Garzik  *	If the condition is met, it returns; otherwise, the process is
7133c6fd2807SJeff Garzik  *	repeated after @interval_msec until timeout.
7134c6fd2807SJeff Garzik  *
7135c6fd2807SJeff Garzik  *	LOCKING:
7136c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
7137c6fd2807SJeff Garzik  *
7138c6fd2807SJeff Garzik  *	RETURNS:
7139c6fd2807SJeff Garzik  *	The final register value.
7140c6fd2807SJeff Garzik  */
714197750cebSTejun Heo u32 ata_wait_register(struct ata_port *ap, void __iomem *reg, u32 mask, u32 val,
7142341c2c95STejun Heo 		      unsigned long interval, unsigned long timeout)
7143c6fd2807SJeff Garzik {
7144341c2c95STejun Heo 	unsigned long deadline;
7145c6fd2807SJeff Garzik 	u32 tmp;
7146c6fd2807SJeff Garzik 
7147c6fd2807SJeff Garzik 	tmp = ioread32(reg);
7148c6fd2807SJeff Garzik 
7149c6fd2807SJeff Garzik 	/* Calculate timeout _after_ the first read to make sure
7150c6fd2807SJeff Garzik 	 * preceding writes reach the controller before starting to
7151c6fd2807SJeff Garzik 	 * eat away the timeout.
7152c6fd2807SJeff Garzik 	 */
7153341c2c95STejun Heo 	deadline = ata_deadline(jiffies, timeout);
7154c6fd2807SJeff Garzik 
7155341c2c95STejun Heo 	while ((tmp & mask) == val && time_before(jiffies, deadline)) {
715697750cebSTejun Heo 		ata_msleep(ap, interval);
7157c6fd2807SJeff Garzik 		tmp = ioread32(reg);
7158c6fd2807SJeff Garzik 	}
7159c6fd2807SJeff Garzik 
7160c6fd2807SJeff Garzik 	return tmp;
7161c6fd2807SJeff Garzik }
7162c6fd2807SJeff Garzik 
71638393b811SGabriele Mazzotta /**
71648393b811SGabriele Mazzotta  *	sata_lpm_ignore_phy_events - test if PHY event should be ignored
71658393b811SGabriele Mazzotta  *	@link: Link receiving the event
71668393b811SGabriele Mazzotta  *
71678393b811SGabriele Mazzotta  *	Test whether the received PHY event has to be ignored or not.
71688393b811SGabriele Mazzotta  *
71698393b811SGabriele Mazzotta  *	LOCKING:
71708393b811SGabriele Mazzotta  *	None:
71718393b811SGabriele Mazzotta  *
71728393b811SGabriele Mazzotta  *	RETURNS:
71738393b811SGabriele Mazzotta  *	True if the event has to be ignored.
71748393b811SGabriele Mazzotta  */
71758393b811SGabriele Mazzotta bool sata_lpm_ignore_phy_events(struct ata_link *link)
71768393b811SGabriele Mazzotta {
717709c5b480SGabriele Mazzotta 	unsigned long lpm_timeout = link->last_lpm_change +
717809c5b480SGabriele Mazzotta 				    msecs_to_jiffies(ATA_TMOUT_SPURIOUS_PHY);
717909c5b480SGabriele Mazzotta 
71808393b811SGabriele Mazzotta 	/* if LPM is enabled, PHYRDY doesn't mean anything */
718109c5b480SGabriele Mazzotta 	if (link->lpm_policy > ATA_LPM_MAX_POWER)
718209c5b480SGabriele Mazzotta 		return true;
718309c5b480SGabriele Mazzotta 
718409c5b480SGabriele Mazzotta 	/* ignore the first PHY event after the LPM policy changed
718509c5b480SGabriele Mazzotta 	 * as it is might be spurious
718609c5b480SGabriele Mazzotta 	 */
718709c5b480SGabriele Mazzotta 	if ((link->flags & ATA_LFLAG_CHANGED) &&
718809c5b480SGabriele Mazzotta 	    time_before(jiffies, lpm_timeout))
718909c5b480SGabriele Mazzotta 		return true;
719009c5b480SGabriele Mazzotta 
719109c5b480SGabriele Mazzotta 	return false;
71928393b811SGabriele Mazzotta }
71938393b811SGabriele Mazzotta EXPORT_SYMBOL_GPL(sata_lpm_ignore_phy_events);
71948393b811SGabriele Mazzotta 
7195c6fd2807SJeff Garzik /*
7196c6fd2807SJeff Garzik  * Dummy port_ops
7197c6fd2807SJeff Garzik  */
7198c6fd2807SJeff Garzik static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
7199c6fd2807SJeff Garzik {
7200c6fd2807SJeff Garzik 	return AC_ERR_SYSTEM;
7201c6fd2807SJeff Garzik }
7202c6fd2807SJeff Garzik 
7203182d7bbaSTejun Heo static void ata_dummy_error_handler(struct ata_port *ap)
7204182d7bbaSTejun Heo {
7205182d7bbaSTejun Heo 	/* truly dummy */
7206182d7bbaSTejun Heo }
7207182d7bbaSTejun Heo 
7208029cfd6bSTejun Heo struct ata_port_operations ata_dummy_port_ops = {
7209c6fd2807SJeff Garzik 	.qc_prep		= ata_noop_qc_prep,
7210c6fd2807SJeff Garzik 	.qc_issue		= ata_dummy_qc_issue,
7211182d7bbaSTejun Heo 	.error_handler		= ata_dummy_error_handler,
7212e4a9c373SDan Williams 	.sched_eh		= ata_std_sched_eh,
7213e4a9c373SDan Williams 	.end_eh			= ata_std_end_eh,
7214c6fd2807SJeff Garzik };
7215c6fd2807SJeff Garzik 
721621b0ad4fSTejun Heo const struct ata_port_info ata_dummy_port_info = {
721721b0ad4fSTejun Heo 	.port_ops		= &ata_dummy_port_ops,
721821b0ad4fSTejun Heo };
721921b0ad4fSTejun Heo 
7220c6fd2807SJeff Garzik /*
7221a9a79dfeSJoe Perches  * Utility print functions
7222a9a79dfeSJoe Perches  */
7223d7bead1bSJoe Perches void ata_port_printk(const struct ata_port *ap, const char *level,
7224a9a79dfeSJoe Perches 		     const char *fmt, ...)
7225a9a79dfeSJoe Perches {
7226a9a79dfeSJoe Perches 	struct va_format vaf;
7227a9a79dfeSJoe Perches 	va_list args;
7228a9a79dfeSJoe Perches 
7229a9a79dfeSJoe Perches 	va_start(args, fmt);
7230a9a79dfeSJoe Perches 
7231a9a79dfeSJoe Perches 	vaf.fmt = fmt;
7232a9a79dfeSJoe Perches 	vaf.va = &args;
7233a9a79dfeSJoe Perches 
7234d7bead1bSJoe Perches 	printk("%sata%u: %pV", level, ap->print_id, &vaf);
7235a9a79dfeSJoe Perches 
7236a9a79dfeSJoe Perches 	va_end(args);
7237a9a79dfeSJoe Perches }
7238a9a79dfeSJoe Perches EXPORT_SYMBOL(ata_port_printk);
7239a9a79dfeSJoe Perches 
7240d7bead1bSJoe Perches void ata_link_printk(const struct ata_link *link, const char *level,
7241a9a79dfeSJoe Perches 		     const char *fmt, ...)
7242a9a79dfeSJoe Perches {
7243a9a79dfeSJoe Perches 	struct va_format vaf;
7244a9a79dfeSJoe Perches 	va_list args;
7245a9a79dfeSJoe Perches 
7246a9a79dfeSJoe Perches 	va_start(args, fmt);
7247a9a79dfeSJoe Perches 
7248a9a79dfeSJoe Perches 	vaf.fmt = fmt;
7249a9a79dfeSJoe Perches 	vaf.va = &args;
7250a9a79dfeSJoe Perches 
7251a9a79dfeSJoe Perches 	if (sata_pmp_attached(link->ap) || link->ap->slave_link)
7252d7bead1bSJoe Perches 		printk("%sata%u.%02u: %pV",
7253a9a79dfeSJoe Perches 		       level, link->ap->print_id, link->pmp, &vaf);
7254a9a79dfeSJoe Perches 	else
7255d7bead1bSJoe Perches 		printk("%sata%u: %pV",
7256a9a79dfeSJoe Perches 		       level, link->ap->print_id, &vaf);
7257a9a79dfeSJoe Perches 
7258a9a79dfeSJoe Perches 	va_end(args);
7259a9a79dfeSJoe Perches }
7260a9a79dfeSJoe Perches EXPORT_SYMBOL(ata_link_printk);
7261a9a79dfeSJoe Perches 
7262d7bead1bSJoe Perches void ata_dev_printk(const struct ata_device *dev, const char *level,
7263a9a79dfeSJoe Perches 		    const char *fmt, ...)
7264a9a79dfeSJoe Perches {
7265a9a79dfeSJoe Perches 	struct va_format vaf;
7266a9a79dfeSJoe Perches 	va_list args;
7267a9a79dfeSJoe Perches 
7268a9a79dfeSJoe Perches 	va_start(args, fmt);
7269a9a79dfeSJoe Perches 
7270a9a79dfeSJoe Perches 	vaf.fmt = fmt;
7271a9a79dfeSJoe Perches 	vaf.va = &args;
7272a9a79dfeSJoe Perches 
7273d7bead1bSJoe Perches 	printk("%sata%u.%02u: %pV",
7274a9a79dfeSJoe Perches 	       level, dev->link->ap->print_id, dev->link->pmp + dev->devno,
7275a9a79dfeSJoe Perches 	       &vaf);
7276a9a79dfeSJoe Perches 
7277a9a79dfeSJoe Perches 	va_end(args);
7278a9a79dfeSJoe Perches }
7279a9a79dfeSJoe Perches EXPORT_SYMBOL(ata_dev_printk);
7280a9a79dfeSJoe Perches 
728106296a1eSJoe Perches void ata_print_version(const struct device *dev, const char *version)
728206296a1eSJoe Perches {
728306296a1eSJoe Perches 	dev_printk(KERN_DEBUG, dev, "version %s\n", version);
728406296a1eSJoe Perches }
728506296a1eSJoe Perches EXPORT_SYMBOL(ata_print_version);
728606296a1eSJoe Perches 
7287a9a79dfeSJoe Perches /*
7288c6fd2807SJeff Garzik  * libata is essentially a library of internal helper functions for
7289c6fd2807SJeff Garzik  * low-level ATA host controller drivers.  As such, the API/ABI is
7290c6fd2807SJeff Garzik  * likely to change as new drivers are added and updated.
7291c6fd2807SJeff Garzik  * Do not depend on ABI/API stability.
7292c6fd2807SJeff Garzik  */
7293c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
7294c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
7295c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_deb_timing_long);
7296029cfd6bSTejun Heo EXPORT_SYMBOL_GPL(ata_base_port_ops);
7297029cfd6bSTejun Heo EXPORT_SYMBOL_GPL(sata_port_ops);
7298c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
729921b0ad4fSTejun Heo EXPORT_SYMBOL_GPL(ata_dummy_port_info);
73001eca4365STejun Heo EXPORT_SYMBOL_GPL(ata_link_next);
73011eca4365STejun Heo EXPORT_SYMBOL_GPL(ata_dev_next);
7302c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_std_bios_param);
7303d8d9129eSTejun Heo EXPORT_SYMBOL_GPL(ata_scsi_unlock_native_capacity);
7304cca3974eSJeff Garzik EXPORT_SYMBOL_GPL(ata_host_init);
7305f3187195STejun Heo EXPORT_SYMBOL_GPL(ata_host_alloc);
7306f5cda257STejun Heo EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
7307b1c72916STejun Heo EXPORT_SYMBOL_GPL(ata_slave_link_init);
7308ecef7253STejun Heo EXPORT_SYMBOL_GPL(ata_host_start);
7309f3187195STejun Heo EXPORT_SYMBOL_GPL(ata_host_register);
7310f5cda257STejun Heo EXPORT_SYMBOL_GPL(ata_host_activate);
73110529c159STejun Heo EXPORT_SYMBOL_GPL(ata_host_detach);
7312c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_sg_init);
7313c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_qc_complete);
7314c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
7315436d34b3STejun Heo EXPORT_SYMBOL_GPL(atapi_cmd_type);
7316c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_tf_to_fis);
7317c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_tf_from_fis);
73186357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_pack_xfermask);
73196357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_unpack_xfermask);
73206357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
73216357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
73226357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
73236357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_mode_string);
73246357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_id_xfermask);
732504351821SAlan EXPORT_SYMBOL_GPL(ata_do_set_mode);
732631cc23b3STejun Heo EXPORT_SYMBOL_GPL(ata_std_qc_defer);
7327c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
732810305f0fSAlan EXPORT_SYMBOL_GPL(ata_dev_disable);
7329c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_set_spd);
7330aa2731adSTejun Heo EXPORT_SYMBOL_GPL(ata_wait_after_reset);
7331936fd732STejun Heo EXPORT_SYMBOL_GPL(sata_link_debounce);
7332936fd732STejun Heo EXPORT_SYMBOL_GPL(sata_link_resume);
73331152b261STejun Heo EXPORT_SYMBOL_GPL(sata_link_scr_lpm);
73340aa1113dSTejun Heo EXPORT_SYMBOL_GPL(ata_std_prereset);
7335cc0680a5STejun Heo EXPORT_SYMBOL_GPL(sata_link_hardreset);
733657c9efdfSTejun Heo EXPORT_SYMBOL_GPL(sata_std_hardreset);
7337203c75b8STejun Heo EXPORT_SYMBOL_GPL(ata_std_postreset);
7338c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_dev_classify);
7339c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_dev_pair);
7340c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_ratelimit);
734197750cebSTejun Heo EXPORT_SYMBOL_GPL(ata_msleep);
7342c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_wait_register);
7343c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
7344c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
7345c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
7346c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
7347f6e67035SDan Williams EXPORT_SYMBOL_GPL(__ata_change_queue_depth);
7348c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_valid);
7349c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_read);
7350c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_write);
7351c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_write_flush);
7352936fd732STejun Heo EXPORT_SYMBOL_GPL(ata_link_online);
7353936fd732STejun Heo EXPORT_SYMBOL_GPL(ata_link_offline);
73546ffa01d8STejun Heo #ifdef CONFIG_PM
7355cca3974eSJeff Garzik EXPORT_SYMBOL_GPL(ata_host_suspend);
7356cca3974eSJeff Garzik EXPORT_SYMBOL_GPL(ata_host_resume);
73576ffa01d8STejun Heo #endif /* CONFIG_PM */
7358c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_id_string);
7359c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_id_c_string);
7360963e4975SAlan Cox EXPORT_SYMBOL_GPL(ata_do_dev_read_id);
7361c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_simulate);
7362c6fd2807SJeff Garzik 
7363c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
73646357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_timing_find_mode);
7365c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_timing_compute);
7366c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_timing_merge);
7367a0f79b92STejun Heo EXPORT_SYMBOL_GPL(ata_timing_cycle2mode);
7368c6fd2807SJeff Garzik 
7369c6fd2807SJeff Garzik #ifdef CONFIG_PCI
7370c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(pci_test_config_bits);
7371c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_remove_one);
73726ffa01d8STejun Heo #ifdef CONFIG_PM
7373c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
7374c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
7375c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
7376c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_resume);
73776ffa01d8STejun Heo #endif /* CONFIG_PM */
7378c6fd2807SJeff Garzik #endif /* CONFIG_PCI */
7379c6fd2807SJeff Garzik 
7380b7db04d9SBrian Norris EXPORT_SYMBOL_GPL(ata_platform_remove_one);
7381b7db04d9SBrian Norris 
7382b64bbc39STejun Heo EXPORT_SYMBOL_GPL(__ata_ehi_push_desc);
7383b64bbc39STejun Heo EXPORT_SYMBOL_GPL(ata_ehi_push_desc);
7384b64bbc39STejun Heo EXPORT_SYMBOL_GPL(ata_ehi_clear_desc);
7385cbcdd875STejun Heo EXPORT_SYMBOL_GPL(ata_port_desc);
7386cbcdd875STejun Heo #ifdef CONFIG_PCI
7387cbcdd875STejun Heo EXPORT_SYMBOL_GPL(ata_port_pbar_desc);
7388cbcdd875STejun Heo #endif /* CONFIG_PCI */
7389c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
7390dbd82616STejun Heo EXPORT_SYMBOL_GPL(ata_link_abort);
7391c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_abort);
7392c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_freeze);
73937d77b247STejun Heo EXPORT_SYMBOL_GPL(sata_async_notification);
7394c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
7395c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
7396c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
7397c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
739810acf3b0SMark Lord EXPORT_SYMBOL_GPL(ata_eh_analyze_ncq_error);
7399c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_do_eh);
7400a1efdabaSTejun Heo EXPORT_SYMBOL_GPL(ata_std_error_handler);
7401be0d18dfSAlan Cox 
7402be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_40wire);
7403be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_80wire);
7404be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_unknown);
7405c88f90c3STejun Heo EXPORT_SYMBOL_GPL(ata_cable_ignore);
7406be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_sata);
74072fa4a326SJason Yan EXPORT_SYMBOL_GPL(ata_host_get);
74082fa4a326SJason Yan EXPORT_SYMBOL_GPL(ata_host_put);
7409