xref: /openbmc/linux/drivers/ata/libata-core.c (revision 2fa4a326)
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 
2496ecd75ad5STejun Heo 	if (dev->horkage & ATA_HORKAGE_NOLPM) {
2497ecd75ad5STejun Heo 		ata_dev_warn(dev, "LPM support broken, forcing max_power\n");
2498ecd75ad5STejun Heo 		dev->link->ap->target_lpm_policy = ATA_LPM_MAX_POWER;
2499ecd75ad5STejun Heo 	}
2500ecd75ad5STejun Heo 
25016746544cSTejun Heo 	/* let ACPI work its magic */
25026746544cSTejun Heo 	rc = ata_acpi_on_devcfg(dev);
25036746544cSTejun Heo 	if (rc)
25046746544cSTejun Heo 		return rc;
250508573a86SKristen Carlson Accardi 
250605027adcSTejun Heo 	/* massage HPA, do it early as it might change IDENTIFY data */
250705027adcSTejun Heo 	rc = ata_hpa_resize(dev);
250805027adcSTejun Heo 	if (rc)
250905027adcSTejun Heo 		return rc;
251005027adcSTejun Heo 
2511c6fd2807SJeff Garzik 	/* print device capabilities */
2512c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2513a9a79dfeSJoe Perches 		ata_dev_dbg(dev,
2514c6fd2807SJeff Garzik 			    "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
2515c6fd2807SJeff Garzik 			    "85:%04x 86:%04x 87:%04x 88:%04x\n",
25167f5e4e8dSHarvey Harrison 			    __func__,
2517c6fd2807SJeff Garzik 			    id[49], id[82], id[83], id[84],
2518c6fd2807SJeff Garzik 			    id[85], id[86], id[87], id[88]);
2519c6fd2807SJeff Garzik 
2520c6fd2807SJeff Garzik 	/* initialize to-be-configured parameters */
2521c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_CFG_MASK;
2522c6fd2807SJeff Garzik 	dev->max_sectors = 0;
2523c6fd2807SJeff Garzik 	dev->cdb_len = 0;
2524c6fd2807SJeff Garzik 	dev->n_sectors = 0;
2525c6fd2807SJeff Garzik 	dev->cylinders = 0;
2526c6fd2807SJeff Garzik 	dev->heads = 0;
2527c6fd2807SJeff Garzik 	dev->sectors = 0;
2528e18086d6SMark Lord 	dev->multi_count = 0;
2529c6fd2807SJeff Garzik 
2530c6fd2807SJeff Garzik 	/*
2531c6fd2807SJeff Garzik 	 * common ATA, ATAPI feature tests
2532c6fd2807SJeff Garzik 	 */
2533c6fd2807SJeff Garzik 
2534c6fd2807SJeff Garzik 	/* find max transfer mode; for printk only */
2535c6fd2807SJeff Garzik 	xfer_mask = ata_id_xfermask(id);
2536c6fd2807SJeff Garzik 
2537c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2538c6fd2807SJeff Garzik 		ata_dump_id(id);
2539c6fd2807SJeff Garzik 
2540ef143d57SAlbert Lee 	/* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
2541ef143d57SAlbert Lee 	ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
2542ef143d57SAlbert Lee 			sizeof(fwrevbuf));
2543ef143d57SAlbert Lee 
2544ef143d57SAlbert Lee 	ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
2545ef143d57SAlbert Lee 			sizeof(modelbuf));
2546ef143d57SAlbert Lee 
2547c6fd2807SJeff Garzik 	/* ATA-specific feature tests */
25489162c657SHannes Reinecke 	if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) {
2549b352e57dSAlan Cox 		if (ata_id_is_cfa(id)) {
255062afe5d7SSergei Shtylyov 			/* CPRM may make this media unusable */
255162afe5d7SSergei Shtylyov 			if (id[ATA_ID_CFA_KEY_MGMT] & 1)
2552a9a79dfeSJoe Perches 				ata_dev_warn(dev,
2553a9a79dfeSJoe Perches 	"supports DRM functions and may not be fully accessible\n");
2554b352e57dSAlan Cox 			snprintf(revbuf, 7, "CFA");
2555ae8d4ee7SAlan Cox 		} else {
2556b352e57dSAlan Cox 			snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
2557ae8d4ee7SAlan Cox 			/* Warn the user if the device has TPM extensions */
2558ae8d4ee7SAlan Cox 			if (ata_id_has_tpm(id))
2559a9a79dfeSJoe Perches 				ata_dev_warn(dev,
2560a9a79dfeSJoe Perches 	"supports DRM functions and may not be fully accessible\n");
2561ae8d4ee7SAlan Cox 		}
2562b352e57dSAlan Cox 
2563c6fd2807SJeff Garzik 		dev->n_sectors = ata_id_n_sectors(id);
2564c6fd2807SJeff Garzik 
2565e18086d6SMark Lord 		/* get current R/W Multiple count setting */
2566e18086d6SMark Lord 		if ((dev->id[47] >> 8) == 0x80 && (dev->id[59] & 0x100)) {
2567e18086d6SMark Lord 			unsigned int max = dev->id[47] & 0xff;
2568e18086d6SMark Lord 			unsigned int cnt = dev->id[59] & 0xff;
2569e18086d6SMark Lord 			/* only recognize/allow powers of two here */
2570e18086d6SMark Lord 			if (is_power_of_2(max) && is_power_of_2(cnt))
2571e18086d6SMark Lord 				if (cnt <= max)
2572e18086d6SMark Lord 					dev->multi_count = cnt;
2573e18086d6SMark Lord 		}
25743f64f565SEric D. Mudama 
2575c6fd2807SJeff Garzik 		if (ata_id_has_lba(id)) {
2576c6fd2807SJeff Garzik 			const char *lba_desc;
2577388539f3SShaohua Li 			char ncq_desc[24];
2578c6fd2807SJeff Garzik 
2579c6fd2807SJeff Garzik 			lba_desc = "LBA";
2580c6fd2807SJeff Garzik 			dev->flags |= ATA_DFLAG_LBA;
2581c6fd2807SJeff Garzik 			if (ata_id_has_lba48(id)) {
2582c6fd2807SJeff Garzik 				dev->flags |= ATA_DFLAG_LBA48;
2583c6fd2807SJeff Garzik 				lba_desc = "LBA48";
25846fc49adbSTejun Heo 
25856fc49adbSTejun Heo 				if (dev->n_sectors >= (1UL << 28) &&
25866fc49adbSTejun Heo 				    ata_id_has_flush_ext(id))
25876fc49adbSTejun Heo 					dev->flags |= ATA_DFLAG_FLUSH_EXT;
2588c6fd2807SJeff Garzik 			}
2589c6fd2807SJeff Garzik 
2590c6fd2807SJeff Garzik 			/* config NCQ */
2591388539f3SShaohua Li 			rc = ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
2592388539f3SShaohua Li 			if (rc)
2593388539f3SShaohua Li 				return rc;
2594c6fd2807SJeff Garzik 
2595c6fd2807SJeff Garzik 			/* print device info to dmesg */
25963f64f565SEric D. Mudama 			if (ata_msg_drv(ap) && print_info) {
2597a9a79dfeSJoe Perches 				ata_dev_info(dev, "%s: %s, %s, max %s\n",
25983f64f565SEric D. Mudama 					     revbuf, modelbuf, fwrevbuf,
25993f64f565SEric D. Mudama 					     ata_mode_string(xfer_mask));
2600a9a79dfeSJoe Perches 				ata_dev_info(dev,
2601a9a79dfeSJoe Perches 					     "%llu sectors, multi %u: %s %s\n",
2602c6fd2807SJeff Garzik 					(unsigned long long)dev->n_sectors,
26033f64f565SEric D. Mudama 					dev->multi_count, lba_desc, ncq_desc);
26043f64f565SEric D. Mudama 			}
2605c6fd2807SJeff Garzik 		} else {
2606c6fd2807SJeff Garzik 			/* CHS */
2607c6fd2807SJeff Garzik 
2608c6fd2807SJeff Garzik 			/* Default translation */
2609c6fd2807SJeff Garzik 			dev->cylinders	= id[1];
2610c6fd2807SJeff Garzik 			dev->heads	= id[3];
2611c6fd2807SJeff Garzik 			dev->sectors	= id[6];
2612c6fd2807SJeff Garzik 
2613c6fd2807SJeff Garzik 			if (ata_id_current_chs_valid(id)) {
2614c6fd2807SJeff Garzik 				/* Current CHS translation is valid. */
2615c6fd2807SJeff Garzik 				dev->cylinders = id[54];
2616c6fd2807SJeff Garzik 				dev->heads     = id[55];
2617c6fd2807SJeff Garzik 				dev->sectors   = id[56];
2618c6fd2807SJeff Garzik 			}
2619c6fd2807SJeff Garzik 
2620c6fd2807SJeff Garzik 			/* print device info to dmesg */
26213f64f565SEric D. Mudama 			if (ata_msg_drv(ap) && print_info) {
2622a9a79dfeSJoe Perches 				ata_dev_info(dev, "%s: %s, %s, max %s\n",
26233f64f565SEric D. Mudama 					     revbuf,	modelbuf, fwrevbuf,
26243f64f565SEric D. Mudama 					     ata_mode_string(xfer_mask));
2625a9a79dfeSJoe Perches 				ata_dev_info(dev,
2626a9a79dfeSJoe Perches 					     "%llu sectors, multi %u, CHS %u/%u/%u\n",
26273f64f565SEric D. Mudama 					     (unsigned long long)dev->n_sectors,
26283f64f565SEric D. Mudama 					     dev->multi_count, dev->cylinders,
26293f64f565SEric D. Mudama 					     dev->heads, dev->sectors);
26303f64f565SEric D. Mudama 			}
2631c6fd2807SJeff Garzik 		}
2632c6fd2807SJeff Garzik 
2633803739d2SShane Huang 		/* Check and mark DevSlp capability. Get DevSlp timing variables
2634803739d2SShane Huang 		 * from SATA Settings page of Identify Device Data Log.
263565fe1f0fSShane Huang 		 */
2636803739d2SShane Huang 		if (ata_id_has_devslp(dev->id)) {
26378e725c7fSDavid Woodhouse 			u8 *sata_setting = ap->sector_buf;
2638803739d2SShane Huang 			int i, j;
2639803739d2SShane Huang 
2640803739d2SShane Huang 			dev->flags |= ATA_DFLAG_DEVSLP;
264165fe1f0fSShane Huang 			err_mask = ata_read_log_page(dev,
26421d51d5f3SChristoph Hellwig 						     ATA_LOG_IDENTIFY_DEVICE,
264365fe1f0fSShane Huang 						     ATA_LOG_SATA_SETTINGS,
2644803739d2SShane Huang 						     sata_setting,
264565fe1f0fSShane Huang 						     1);
264665fe1f0fSShane Huang 			if (err_mask)
264765fe1f0fSShane Huang 				ata_dev_dbg(dev,
264865fe1f0fSShane Huang 					    "failed to get Identify Device Data, Emask 0x%x\n",
264965fe1f0fSShane Huang 					    err_mask);
2650803739d2SShane Huang 			else
2651803739d2SShane Huang 				for (i = 0; i < ATA_LOG_DEVSLP_SIZE; i++) {
2652803739d2SShane Huang 					j = ATA_LOG_DEVSLP_OFFSET + i;
2653803739d2SShane Huang 					dev->devslp_timing[i] = sata_setting[j];
2654803739d2SShane Huang 				}
265565fe1f0fSShane Huang 		}
2656e87fd28cSHannes Reinecke 		ata_dev_config_sense_reporting(dev);
26576d1003aeSHannes Reinecke 		ata_dev_config_zac(dev);
2658818831c8SChristoph Hellwig 		ata_dev_config_trusted(dev);
2659b1ffbf85SMinwoo Im 		dev->cdb_len = 32;
2660c6fd2807SJeff Garzik 	}
2661c6fd2807SJeff Garzik 
2662c6fd2807SJeff Garzik 	/* ATAPI-specific feature tests */
2663c6fd2807SJeff Garzik 	else if (dev->class == ATA_DEV_ATAPI) {
2664854c73a2STejun Heo 		const char *cdb_intr_string = "";
2665854c73a2STejun Heo 		const char *atapi_an_string = "";
266691163006STejun Heo 		const char *dma_dir_string = "";
26677d77b247STejun Heo 		u32 sntf;
2668c6fd2807SJeff Garzik 
2669c6fd2807SJeff Garzik 		rc = atapi_cdb_len(id);
2670c6fd2807SJeff Garzik 		if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
2671c6fd2807SJeff Garzik 			if (ata_msg_warn(ap))
2672a9a79dfeSJoe Perches 				ata_dev_warn(dev, "unsupported CDB len\n");
2673c6fd2807SJeff Garzik 			rc = -EINVAL;
2674c6fd2807SJeff Garzik 			goto err_out_nosup;
2675c6fd2807SJeff Garzik 		}
2676c6fd2807SJeff Garzik 		dev->cdb_len = (unsigned int) rc;
2677c6fd2807SJeff Garzik 
26787d77b247STejun Heo 		/* Enable ATAPI AN if both the host and device have
26797d77b247STejun Heo 		 * the support.  If PMP is attached, SNTF is required
26807d77b247STejun Heo 		 * to enable ATAPI AN to discern between PHY status
26817d77b247STejun Heo 		 * changed notifications and ATAPI ANs.
26829f45cbd3SKristen Carlson Accardi 		 */
2683e7ecd435STejun Heo 		if (atapi_an &&
2684e7ecd435STejun Heo 		    (ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
2685071f44b1STejun Heo 		    (!sata_pmp_attached(ap) ||
26867d77b247STejun Heo 		     sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
26879f45cbd3SKristen Carlson Accardi 			/* issue SET feature command to turn this on */
2688218f3d30SJeff Garzik 			err_mask = ata_dev_set_feature(dev,
2689218f3d30SJeff Garzik 					SETFEATURES_SATA_ENABLE, SATA_AN);
2690854c73a2STejun Heo 			if (err_mask)
2691a9a79dfeSJoe Perches 				ata_dev_err(dev,
2692a9a79dfeSJoe Perches 					    "failed to enable ATAPI AN (err_mask=0x%x)\n",
2693a9a79dfeSJoe Perches 					    err_mask);
2694854c73a2STejun Heo 			else {
26959f45cbd3SKristen Carlson Accardi 				dev->flags |= ATA_DFLAG_AN;
2696854c73a2STejun Heo 				atapi_an_string = ", ATAPI AN";
2697854c73a2STejun Heo 			}
26989f45cbd3SKristen Carlson Accardi 		}
26999f45cbd3SKristen Carlson Accardi 
2700c6fd2807SJeff Garzik 		if (ata_id_cdb_intr(dev->id)) {
2701c6fd2807SJeff Garzik 			dev->flags |= ATA_DFLAG_CDB_INTR;
2702c6fd2807SJeff Garzik 			cdb_intr_string = ", CDB intr";
2703c6fd2807SJeff Garzik 		}
2704c6fd2807SJeff Garzik 
2705966fbe19SVincent Pelletier 		if (atapi_dmadir || (dev->horkage & ATA_HORKAGE_ATAPI_DMADIR) || atapi_id_dmadir(dev->id)) {
270691163006STejun Heo 			dev->flags |= ATA_DFLAG_DMADIR;
270791163006STejun Heo 			dma_dir_string = ", DMADIR";
270891163006STejun Heo 		}
270991163006STejun Heo 
2710afe75951SAaron Lu 		if (ata_id_has_da(dev->id)) {
2711b1354cbbSLin Ming 			dev->flags |= ATA_DFLAG_DA;
2712afe75951SAaron Lu 			zpodd_init(dev);
2713afe75951SAaron Lu 		}
2714b1354cbbSLin Ming 
2715c6fd2807SJeff Garzik 		/* print device info to dmesg */
2716c6fd2807SJeff Garzik 		if (ata_msg_drv(ap) && print_info)
2717a9a79dfeSJoe Perches 			ata_dev_info(dev,
271891163006STejun Heo 				     "ATAPI: %s, %s, max %s%s%s%s\n",
2719ef143d57SAlbert Lee 				     modelbuf, fwrevbuf,
2720c6fd2807SJeff Garzik 				     ata_mode_string(xfer_mask),
272191163006STejun Heo 				     cdb_intr_string, atapi_an_string,
272291163006STejun Heo 				     dma_dir_string);
2723c6fd2807SJeff Garzik 	}
2724c6fd2807SJeff Garzik 
2725914ed354STejun Heo 	/* determine max_sectors */
2726914ed354STejun Heo 	dev->max_sectors = ATA_MAX_SECTORS;
2727914ed354STejun Heo 	if (dev->flags & ATA_DFLAG_LBA48)
2728914ed354STejun Heo 		dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2729914ed354STejun Heo 
2730c5038fc0SAlan Cox 	/* Limit PATA drive on SATA cable bridge transfers to udma5,
2731c5038fc0SAlan Cox 	   200 sectors */
2732c6fd2807SJeff Garzik 	if (ata_dev_knobble(dev)) {
2733c6fd2807SJeff Garzik 		if (ata_msg_drv(ap) && print_info)
2734a9a79dfeSJoe Perches 			ata_dev_info(dev, "applying bridge limits\n");
2735c6fd2807SJeff Garzik 		dev->udma_mask &= ATA_UDMA5;
2736c6fd2807SJeff Garzik 		dev->max_sectors = ATA_MAX_SECTORS;
2737c6fd2807SJeff Garzik 	}
2738c6fd2807SJeff Garzik 
2739f8d8e579STony Battersby 	if ((dev->class == ATA_DEV_ATAPI) &&
2740f442cd86SAlbert Lee 	    (atapi_command_packet_set(id) == TYPE_TAPE)) {
2741f8d8e579STony Battersby 		dev->max_sectors = ATA_MAX_SECTORS_TAPE;
2742f442cd86SAlbert Lee 		dev->horkage |= ATA_HORKAGE_STUCK_ERR;
2743f442cd86SAlbert Lee 	}
2744f8d8e579STony Battersby 
274575683fe7STejun Heo 	if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
274603ec52deSTejun Heo 		dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
274703ec52deSTejun Heo 					 dev->max_sectors);
274818d6e9d5SAlbert Lee 
2749af34d637SDavid Milburn 	if (dev->horkage & ATA_HORKAGE_MAX_SEC_1024)
2750af34d637SDavid Milburn 		dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_1024,
2751af34d637SDavid Milburn 					 dev->max_sectors);
2752af34d637SDavid Milburn 
2753a32450e1SShan Hai 	if (dev->horkage & ATA_HORKAGE_MAX_SEC_LBA48)
2754a32450e1SShan Hai 		dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2755a32450e1SShan Hai 
2756c6fd2807SJeff Garzik 	if (ap->ops->dev_config)
2757cd0d3bbcSAlan 		ap->ops->dev_config(dev);
2758c6fd2807SJeff Garzik 
2759c5038fc0SAlan Cox 	if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2760c5038fc0SAlan Cox 		/* Let the user know. We don't want to disallow opens for
2761c5038fc0SAlan Cox 		   rescue purposes, or in case the vendor is just a blithering
2762c5038fc0SAlan Cox 		   idiot. Do this after the dev_config call as some controllers
2763c5038fc0SAlan Cox 		   with buggy firmware may want to avoid reporting false device
2764c5038fc0SAlan Cox 		   bugs */
2765c5038fc0SAlan Cox 
2766c5038fc0SAlan Cox 		if (print_info) {
2767a9a79dfeSJoe Perches 			ata_dev_warn(dev,
2768c5038fc0SAlan Cox "Drive reports diagnostics failure. This may indicate a drive\n");
2769a9a79dfeSJoe Perches 			ata_dev_warn(dev,
2770c5038fc0SAlan Cox "fault or invalid emulation. Contact drive vendor for information.\n");
2771c5038fc0SAlan Cox 		}
2772c5038fc0SAlan Cox 	}
2773c5038fc0SAlan Cox 
2774ac70a964STejun Heo 	if ((dev->horkage & ATA_HORKAGE_FIRMWARE_WARN) && print_info) {
2775a9a79dfeSJoe Perches 		ata_dev_warn(dev, "WARNING: device requires firmware update to be fully functional\n");
2776a9a79dfeSJoe Perches 		ata_dev_warn(dev, "         contact the vendor or visit http://ata.wiki.kernel.org\n");
2777ac70a964STejun Heo 	}
2778ac70a964STejun Heo 
2779c6fd2807SJeff Garzik 	return 0;
2780c6fd2807SJeff Garzik 
2781c6fd2807SJeff Garzik err_out_nosup:
2782c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2783a9a79dfeSJoe Perches 		ata_dev_dbg(dev, "%s: EXIT, err\n", __func__);
2784c6fd2807SJeff Garzik 	return rc;
2785c6fd2807SJeff Garzik }
2786c6fd2807SJeff Garzik 
2787c6fd2807SJeff Garzik /**
27882e41e8e6SAlan Cox  *	ata_cable_40wire	-	return 40 wire cable type
2789be0d18dfSAlan Cox  *	@ap: port
2790be0d18dfSAlan Cox  *
27912e41e8e6SAlan Cox  *	Helper method for drivers which want to hardwire 40 wire cable
2792be0d18dfSAlan Cox  *	detection.
2793be0d18dfSAlan Cox  */
2794be0d18dfSAlan Cox 
2795be0d18dfSAlan Cox int ata_cable_40wire(struct ata_port *ap)
2796be0d18dfSAlan Cox {
2797be0d18dfSAlan Cox 	return ATA_CBL_PATA40;
2798be0d18dfSAlan Cox }
2799be0d18dfSAlan Cox 
2800be0d18dfSAlan Cox /**
28012e41e8e6SAlan Cox  *	ata_cable_80wire	-	return 80 wire cable type
2802be0d18dfSAlan Cox  *	@ap: port
2803be0d18dfSAlan Cox  *
28042e41e8e6SAlan Cox  *	Helper method for drivers which want to hardwire 80 wire cable
2805be0d18dfSAlan Cox  *	detection.
2806be0d18dfSAlan Cox  */
2807be0d18dfSAlan Cox 
2808be0d18dfSAlan Cox int ata_cable_80wire(struct ata_port *ap)
2809be0d18dfSAlan Cox {
2810be0d18dfSAlan Cox 	return ATA_CBL_PATA80;
2811be0d18dfSAlan Cox }
2812be0d18dfSAlan Cox 
2813be0d18dfSAlan Cox /**
2814be0d18dfSAlan Cox  *	ata_cable_unknown	-	return unknown PATA cable.
2815be0d18dfSAlan Cox  *	@ap: port
2816be0d18dfSAlan Cox  *
2817be0d18dfSAlan Cox  *	Helper method for drivers which have no PATA cable detection.
2818be0d18dfSAlan Cox  */
2819be0d18dfSAlan Cox 
2820be0d18dfSAlan Cox int ata_cable_unknown(struct ata_port *ap)
2821be0d18dfSAlan Cox {
2822be0d18dfSAlan Cox 	return ATA_CBL_PATA_UNK;
2823be0d18dfSAlan Cox }
2824be0d18dfSAlan Cox 
2825be0d18dfSAlan Cox /**
2826c88f90c3STejun Heo  *	ata_cable_ignore	-	return ignored PATA cable.
2827c88f90c3STejun Heo  *	@ap: port
2828c88f90c3STejun Heo  *
2829c88f90c3STejun Heo  *	Helper method for drivers which don't use cable type to limit
2830c88f90c3STejun Heo  *	transfer mode.
2831c88f90c3STejun Heo  */
2832c88f90c3STejun Heo int ata_cable_ignore(struct ata_port *ap)
2833c88f90c3STejun Heo {
2834c88f90c3STejun Heo 	return ATA_CBL_PATA_IGN;
2835c88f90c3STejun Heo }
2836c88f90c3STejun Heo 
2837c88f90c3STejun Heo /**
2838be0d18dfSAlan Cox  *	ata_cable_sata	-	return SATA cable type
2839be0d18dfSAlan Cox  *	@ap: port
2840be0d18dfSAlan Cox  *
2841be0d18dfSAlan Cox  *	Helper method for drivers which have SATA cables
2842be0d18dfSAlan Cox  */
2843be0d18dfSAlan Cox 
2844be0d18dfSAlan Cox int ata_cable_sata(struct ata_port *ap)
2845be0d18dfSAlan Cox {
2846be0d18dfSAlan Cox 	return ATA_CBL_SATA;
2847be0d18dfSAlan Cox }
2848be0d18dfSAlan Cox 
2849be0d18dfSAlan Cox /**
2850c6fd2807SJeff Garzik  *	ata_bus_probe - Reset and probe ATA bus
2851c6fd2807SJeff Garzik  *	@ap: Bus to probe
2852c6fd2807SJeff Garzik  *
2853c6fd2807SJeff Garzik  *	Master ATA bus probing function.  Initiates a hardware-dependent
2854c6fd2807SJeff Garzik  *	bus reset, then attempts to identify any devices found on
2855c6fd2807SJeff Garzik  *	the bus.
2856c6fd2807SJeff Garzik  *
2857c6fd2807SJeff Garzik  *	LOCKING:
2858c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
2859c6fd2807SJeff Garzik  *
2860c6fd2807SJeff Garzik  *	RETURNS:
2861c6fd2807SJeff Garzik  *	Zero on success, negative errno otherwise.
2862c6fd2807SJeff Garzik  */
2863c6fd2807SJeff Garzik 
2864c6fd2807SJeff Garzik int ata_bus_probe(struct ata_port *ap)
2865c6fd2807SJeff Garzik {
2866c6fd2807SJeff Garzik 	unsigned int classes[ATA_MAX_DEVICES];
2867c6fd2807SJeff Garzik 	int tries[ATA_MAX_DEVICES];
2868f58229f8STejun Heo 	int rc;
2869c6fd2807SJeff Garzik 	struct ata_device *dev;
2870c6fd2807SJeff Garzik 
28711eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL)
2872f58229f8STejun Heo 		tries[dev->devno] = ATA_PROBE_MAX_TRIES;
2873c6fd2807SJeff Garzik 
2874c6fd2807SJeff Garzik  retry:
28751eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL) {
2876cdeab114STejun Heo 		/* If we issue an SRST then an ATA drive (not ATAPI)
2877cdeab114STejun Heo 		 * may change configuration and be in PIO0 timing. If
2878cdeab114STejun Heo 		 * we do a hard reset (or are coming from power on)
2879cdeab114STejun Heo 		 * this is true for ATA or ATAPI. Until we've set a
2880cdeab114STejun Heo 		 * suitable controller mode we should not touch the
2881cdeab114STejun Heo 		 * bus as we may be talking too fast.
2882cdeab114STejun Heo 		 */
2883cdeab114STejun Heo 		dev->pio_mode = XFER_PIO_0;
28845416912aSAaron Lu 		dev->dma_mode = 0xff;
2885cdeab114STejun Heo 
2886cdeab114STejun Heo 		/* If the controller has a pio mode setup function
2887cdeab114STejun Heo 		 * then use it to set the chipset to rights. Don't
2888cdeab114STejun Heo 		 * touch the DMA setup as that will be dealt with when
2889cdeab114STejun Heo 		 * configuring devices.
2890cdeab114STejun Heo 		 */
2891cdeab114STejun Heo 		if (ap->ops->set_piomode)
2892cdeab114STejun Heo 			ap->ops->set_piomode(ap, dev);
2893cdeab114STejun Heo 	}
2894cdeab114STejun Heo 
2895c6fd2807SJeff Garzik 	/* reset and determine device classes */
2896c6fd2807SJeff Garzik 	ap->ops->phy_reset(ap);
2897c6fd2807SJeff Garzik 
28981eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL) {
28993e4ec344STejun Heo 		if (dev->class != ATA_DEV_UNKNOWN)
2900c6fd2807SJeff Garzik 			classes[dev->devno] = dev->class;
2901c6fd2807SJeff Garzik 		else
2902c6fd2807SJeff Garzik 			classes[dev->devno] = ATA_DEV_NONE;
2903c6fd2807SJeff Garzik 
2904c6fd2807SJeff Garzik 		dev->class = ATA_DEV_UNKNOWN;
2905c6fd2807SJeff Garzik 	}
2906c6fd2807SJeff Garzik 
2907f31f0cc2SJeff Garzik 	/* read IDENTIFY page and configure devices. We have to do the identify
2908f31f0cc2SJeff Garzik 	   specific sequence bass-ackwards so that PDIAG- is released by
2909f31f0cc2SJeff Garzik 	   the slave device */
2910f31f0cc2SJeff Garzik 
29111eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL_REVERSE) {
2912f58229f8STejun Heo 		if (tries[dev->devno])
2913f58229f8STejun Heo 			dev->class = classes[dev->devno];
2914c6fd2807SJeff Garzik 
2915c6fd2807SJeff Garzik 		if (!ata_dev_enabled(dev))
2916c6fd2807SJeff Garzik 			continue;
2917c6fd2807SJeff Garzik 
2918bff04647STejun Heo 		rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2919bff04647STejun Heo 				     dev->id);
2920c6fd2807SJeff Garzik 		if (rc)
2921c6fd2807SJeff Garzik 			goto fail;
2922f31f0cc2SJeff Garzik 	}
2923f31f0cc2SJeff Garzik 
2924be0d18dfSAlan Cox 	/* Now ask for the cable type as PDIAG- should have been released */
2925be0d18dfSAlan Cox 	if (ap->ops->cable_detect)
2926be0d18dfSAlan Cox 		ap->cbl = ap->ops->cable_detect(ap);
2927be0d18dfSAlan Cox 
29281eca4365STejun Heo 	/* We may have SATA bridge glue hiding here irrespective of
29291eca4365STejun Heo 	 * the reported cable types and sensed types.  When SATA
29301eca4365STejun Heo 	 * drives indicate we have a bridge, we don't know which end
29311eca4365STejun Heo 	 * of the link the bridge is which is a problem.
29321eca4365STejun Heo 	 */
29331eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED)
2934614fe29bSAlan Cox 		if (ata_id_is_sata(dev->id))
2935614fe29bSAlan Cox 			ap->cbl = ATA_CBL_SATA;
2936614fe29bSAlan Cox 
2937f31f0cc2SJeff Garzik 	/* After the identify sequence we can now set up the devices. We do
2938f31f0cc2SJeff Garzik 	   this in the normal order so that the user doesn't get confused */
2939f31f0cc2SJeff Garzik 
29401eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED) {
29419af5c9c9STejun Heo 		ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
2942efdaedc4STejun Heo 		rc = ata_dev_configure(dev);
29439af5c9c9STejun Heo 		ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
2944c6fd2807SJeff Garzik 		if (rc)
2945c6fd2807SJeff Garzik 			goto fail;
2946c6fd2807SJeff Garzik 	}
2947c6fd2807SJeff Garzik 
2948c6fd2807SJeff Garzik 	/* configure transfer mode */
29490260731fSTejun Heo 	rc = ata_set_mode(&ap->link, &dev);
29504ae72a1eSTejun Heo 	if (rc)
2951c6fd2807SJeff Garzik 		goto fail;
2952c6fd2807SJeff Garzik 
29531eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED)
2954c6fd2807SJeff Garzik 		return 0;
2955c6fd2807SJeff Garzik 
2956c6fd2807SJeff Garzik 	return -ENODEV;
2957c6fd2807SJeff Garzik 
2958c6fd2807SJeff Garzik  fail:
29594ae72a1eSTejun Heo 	tries[dev->devno]--;
29604ae72a1eSTejun Heo 
2961c6fd2807SJeff Garzik 	switch (rc) {
2962c6fd2807SJeff Garzik 	case -EINVAL:
29634ae72a1eSTejun Heo 		/* eeek, something went very wrong, give up */
2964c6fd2807SJeff Garzik 		tries[dev->devno] = 0;
2965c6fd2807SJeff Garzik 		break;
29664ae72a1eSTejun Heo 
29674ae72a1eSTejun Heo 	case -ENODEV:
29684ae72a1eSTejun Heo 		/* give it just one more chance */
29694ae72a1eSTejun Heo 		tries[dev->devno] = min(tries[dev->devno], 1);
297005b83605SGustavo A. R. Silva 		/* fall through */
2971c6fd2807SJeff Garzik 	case -EIO:
29724ae72a1eSTejun Heo 		if (tries[dev->devno] == 1) {
29734ae72a1eSTejun Heo 			/* This is the last chance, better to slow
29744ae72a1eSTejun Heo 			 * down than lose it.
29754ae72a1eSTejun Heo 			 */
2976a07d499bSTejun Heo 			sata_down_spd_limit(&ap->link, 0);
29774ae72a1eSTejun Heo 			ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
29784ae72a1eSTejun Heo 		}
2979c6fd2807SJeff Garzik 	}
2980c6fd2807SJeff Garzik 
29814ae72a1eSTejun Heo 	if (!tries[dev->devno])
2982c6fd2807SJeff Garzik 		ata_dev_disable(dev);
2983c6fd2807SJeff Garzik 
2984c6fd2807SJeff Garzik 	goto retry;
2985c6fd2807SJeff Garzik }
2986c6fd2807SJeff Garzik 
2987c6fd2807SJeff Garzik /**
2988c6fd2807SJeff Garzik  *	sata_print_link_status - Print SATA link status
2989936fd732STejun Heo  *	@link: SATA link to printk link status about
2990c6fd2807SJeff Garzik  *
2991c6fd2807SJeff Garzik  *	This function prints link speed and status of a SATA link.
2992c6fd2807SJeff Garzik  *
2993c6fd2807SJeff Garzik  *	LOCKING:
2994c6fd2807SJeff Garzik  *	None.
2995c6fd2807SJeff Garzik  */
29966bdb4fc9SAdrian Bunk static void sata_print_link_status(struct ata_link *link)
2997c6fd2807SJeff Garzik {
2998c6fd2807SJeff Garzik 	u32 sstatus, scontrol, tmp;
2999c6fd2807SJeff Garzik 
3000936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus))
3001c6fd2807SJeff Garzik 		return;
3002936fd732STejun Heo 	sata_scr_read(link, SCR_CONTROL, &scontrol);
3003c6fd2807SJeff Garzik 
3004b1c72916STejun Heo 	if (ata_phys_link_online(link)) {
3005c6fd2807SJeff Garzik 		tmp = (sstatus >> 4) & 0xf;
3006a9a79dfeSJoe Perches 		ata_link_info(link, "SATA link up %s (SStatus %X SControl %X)\n",
3007c6fd2807SJeff Garzik 			      sata_spd_string(tmp), sstatus, scontrol);
3008c6fd2807SJeff Garzik 	} else {
3009a9a79dfeSJoe Perches 		ata_link_info(link, "SATA link down (SStatus %X SControl %X)\n",
3010c6fd2807SJeff Garzik 			      sstatus, scontrol);
3011c6fd2807SJeff Garzik 	}
3012c6fd2807SJeff Garzik }
3013c6fd2807SJeff Garzik 
3014c6fd2807SJeff Garzik /**
3015c6fd2807SJeff Garzik  *	ata_dev_pair		-	return other device on cable
3016c6fd2807SJeff Garzik  *	@adev: device
3017c6fd2807SJeff Garzik  *
3018c6fd2807SJeff Garzik  *	Obtain the other device on the same cable, or if none is
3019c6fd2807SJeff Garzik  *	present NULL is returned
3020c6fd2807SJeff Garzik  */
3021c6fd2807SJeff Garzik 
3022c6fd2807SJeff Garzik struct ata_device *ata_dev_pair(struct ata_device *adev)
3023c6fd2807SJeff Garzik {
30249af5c9c9STejun Heo 	struct ata_link *link = adev->link;
30259af5c9c9STejun Heo 	struct ata_device *pair = &link->device[1 - adev->devno];
3026c6fd2807SJeff Garzik 	if (!ata_dev_enabled(pair))
3027c6fd2807SJeff Garzik 		return NULL;
3028c6fd2807SJeff Garzik 	return pair;
3029c6fd2807SJeff Garzik }
3030c6fd2807SJeff Garzik 
3031c6fd2807SJeff Garzik /**
3032c6fd2807SJeff Garzik  *	sata_down_spd_limit - adjust SATA spd limit downward
3033936fd732STejun Heo  *	@link: Link to adjust SATA spd limit for
3034a07d499bSTejun Heo  *	@spd_limit: Additional limit
3035c6fd2807SJeff Garzik  *
3036936fd732STejun Heo  *	Adjust SATA spd limit of @link downward.  Note that this
3037c6fd2807SJeff Garzik  *	function only adjusts the limit.  The change must be applied
3038c6fd2807SJeff Garzik  *	using sata_set_spd().
3039c6fd2807SJeff Garzik  *
3040a07d499bSTejun Heo  *	If @spd_limit is non-zero, the speed is limited to equal to or
3041a07d499bSTejun Heo  *	lower than @spd_limit if such speed is supported.  If
3042a07d499bSTejun Heo  *	@spd_limit is slower than any supported speed, only the lowest
3043a07d499bSTejun Heo  *	supported speed is allowed.
3044a07d499bSTejun Heo  *
3045c6fd2807SJeff Garzik  *	LOCKING:
3046c6fd2807SJeff Garzik  *	Inherited from caller.
3047c6fd2807SJeff Garzik  *
3048c6fd2807SJeff Garzik  *	RETURNS:
3049c6fd2807SJeff Garzik  *	0 on success, negative errno on failure
3050c6fd2807SJeff Garzik  */
3051a07d499bSTejun Heo int sata_down_spd_limit(struct ata_link *link, u32 spd_limit)
3052c6fd2807SJeff Garzik {
3053c6fd2807SJeff Garzik 	u32 sstatus, spd, mask;
3054a07d499bSTejun Heo 	int rc, bit;
3055c6fd2807SJeff Garzik 
3056936fd732STejun Heo 	if (!sata_scr_valid(link))
3057008a7896STejun Heo 		return -EOPNOTSUPP;
3058008a7896STejun Heo 
3059008a7896STejun Heo 	/* If SCR can be read, use it to determine the current SPD.
3060936fd732STejun Heo 	 * If not, use cached value in link->sata_spd.
3061008a7896STejun Heo 	 */
3062936fd732STejun Heo 	rc = sata_scr_read(link, SCR_STATUS, &sstatus);
30639913ff8aSTejun Heo 	if (rc == 0 && ata_sstatus_online(sstatus))
3064008a7896STejun Heo 		spd = (sstatus >> 4) & 0xf;
3065008a7896STejun Heo 	else
3066936fd732STejun Heo 		spd = link->sata_spd;
3067c6fd2807SJeff Garzik 
3068936fd732STejun Heo 	mask = link->sata_spd_limit;
3069c6fd2807SJeff Garzik 	if (mask <= 1)
3070c6fd2807SJeff Garzik 		return -EINVAL;
3071008a7896STejun Heo 
3072008a7896STejun Heo 	/* unconditionally mask off the highest bit */
3073a07d499bSTejun Heo 	bit = fls(mask) - 1;
3074a07d499bSTejun Heo 	mask &= ~(1 << bit);
3075c6fd2807SJeff Garzik 
30762dc0b46bSDavid Milburn 	/*
30772dc0b46bSDavid Milburn 	 * Mask off all speeds higher than or equal to the current one.  At
30782dc0b46bSDavid Milburn 	 * this point, if current SPD is not available and we previously
30792dc0b46bSDavid Milburn 	 * recorded the link speed from SStatus, the driver has already
30802dc0b46bSDavid Milburn 	 * masked off the highest bit so mask should already be 1 or 0.
30812dc0b46bSDavid Milburn 	 * Otherwise, we should not force 1.5Gbps on a link where we have
30822dc0b46bSDavid Milburn 	 * not previously recorded speed from SStatus.  Just return in this
30832dc0b46bSDavid Milburn 	 * case.
3084008a7896STejun Heo 	 */
3085008a7896STejun Heo 	if (spd > 1)
3086008a7896STejun Heo 		mask &= (1 << (spd - 1)) - 1;
3087008a7896STejun Heo 	else
30882dc0b46bSDavid Milburn 		return -EINVAL;
3089008a7896STejun Heo 
3090008a7896STejun Heo 	/* were we already at the bottom? */
3091c6fd2807SJeff Garzik 	if (!mask)
3092c6fd2807SJeff Garzik 		return -EINVAL;
3093c6fd2807SJeff Garzik 
3094a07d499bSTejun Heo 	if (spd_limit) {
3095a07d499bSTejun Heo 		if (mask & ((1 << spd_limit) - 1))
3096a07d499bSTejun Heo 			mask &= (1 << spd_limit) - 1;
3097a07d499bSTejun Heo 		else {
3098a07d499bSTejun Heo 			bit = ffs(mask) - 1;
3099a07d499bSTejun Heo 			mask = 1 << bit;
3100a07d499bSTejun Heo 		}
3101a07d499bSTejun Heo 	}
3102a07d499bSTejun Heo 
3103936fd732STejun Heo 	link->sata_spd_limit = mask;
3104c6fd2807SJeff Garzik 
3105a9a79dfeSJoe Perches 	ata_link_warn(link, "limiting SATA link speed to %s\n",
3106c6fd2807SJeff Garzik 		      sata_spd_string(fls(mask)));
3107c6fd2807SJeff Garzik 
3108c6fd2807SJeff Garzik 	return 0;
3109c6fd2807SJeff Garzik }
3110c6fd2807SJeff Garzik 
3111936fd732STejun Heo static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
3112c6fd2807SJeff Garzik {
31135270222fSTejun Heo 	struct ata_link *host_link = &link->ap->link;
31145270222fSTejun Heo 	u32 limit, target, spd;
3115c6fd2807SJeff Garzik 
31165270222fSTejun Heo 	limit = link->sata_spd_limit;
31175270222fSTejun Heo 
31185270222fSTejun Heo 	/* Don't configure downstream link faster than upstream link.
31195270222fSTejun Heo 	 * It doesn't speed up anything and some PMPs choke on such
31205270222fSTejun Heo 	 * configuration.
31215270222fSTejun Heo 	 */
31225270222fSTejun Heo 	if (!ata_is_host_link(link) && host_link->sata_spd)
31235270222fSTejun Heo 		limit &= (1 << host_link->sata_spd) - 1;
31245270222fSTejun Heo 
31255270222fSTejun Heo 	if (limit == UINT_MAX)
31265270222fSTejun Heo 		target = 0;
3127c6fd2807SJeff Garzik 	else
31285270222fSTejun Heo 		target = fls(limit);
3129c6fd2807SJeff Garzik 
3130c6fd2807SJeff Garzik 	spd = (*scontrol >> 4) & 0xf;
31315270222fSTejun Heo 	*scontrol = (*scontrol & ~0xf0) | ((target & 0xf) << 4);
3132c6fd2807SJeff Garzik 
31335270222fSTejun Heo 	return spd != target;
3134c6fd2807SJeff Garzik }
3135c6fd2807SJeff Garzik 
3136c6fd2807SJeff Garzik /**
3137c6fd2807SJeff Garzik  *	sata_set_spd_needed - is SATA spd configuration needed
3138936fd732STejun Heo  *	@link: Link in question
3139c6fd2807SJeff Garzik  *
3140c6fd2807SJeff Garzik  *	Test whether the spd limit in SControl matches
3141936fd732STejun Heo  *	@link->sata_spd_limit.  This function is used to determine
3142c6fd2807SJeff Garzik  *	whether hardreset is necessary to apply SATA spd
3143c6fd2807SJeff Garzik  *	configuration.
3144c6fd2807SJeff Garzik  *
3145c6fd2807SJeff Garzik  *	LOCKING:
3146c6fd2807SJeff Garzik  *	Inherited from caller.
3147c6fd2807SJeff Garzik  *
3148c6fd2807SJeff Garzik  *	RETURNS:
3149c6fd2807SJeff Garzik  *	1 if SATA spd configuration is needed, 0 otherwise.
3150c6fd2807SJeff Garzik  */
31511dc55e87SAdrian Bunk static int sata_set_spd_needed(struct ata_link *link)
3152c6fd2807SJeff Garzik {
3153c6fd2807SJeff Garzik 	u32 scontrol;
3154c6fd2807SJeff Garzik 
3155936fd732STejun Heo 	if (sata_scr_read(link, SCR_CONTROL, &scontrol))
3156db64bcf3STejun Heo 		return 1;
3157c6fd2807SJeff Garzik 
3158936fd732STejun Heo 	return __sata_set_spd_needed(link, &scontrol);
3159c6fd2807SJeff Garzik }
3160c6fd2807SJeff Garzik 
3161c6fd2807SJeff Garzik /**
3162c6fd2807SJeff Garzik  *	sata_set_spd - set SATA spd according to spd limit
3163936fd732STejun Heo  *	@link: Link to set SATA spd for
3164c6fd2807SJeff Garzik  *
3165936fd732STejun Heo  *	Set SATA spd of @link according to sata_spd_limit.
3166c6fd2807SJeff Garzik  *
3167c6fd2807SJeff Garzik  *	LOCKING:
3168c6fd2807SJeff Garzik  *	Inherited from caller.
3169c6fd2807SJeff Garzik  *
3170c6fd2807SJeff Garzik  *	RETURNS:
3171c6fd2807SJeff Garzik  *	0 if spd doesn't need to be changed, 1 if spd has been
3172c6fd2807SJeff Garzik  *	changed.  Negative errno if SCR registers are inaccessible.
3173c6fd2807SJeff Garzik  */
3174936fd732STejun Heo int sata_set_spd(struct ata_link *link)
3175c6fd2807SJeff Garzik {
3176c6fd2807SJeff Garzik 	u32 scontrol;
3177c6fd2807SJeff Garzik 	int rc;
3178c6fd2807SJeff Garzik 
3179936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3180c6fd2807SJeff Garzik 		return rc;
3181c6fd2807SJeff Garzik 
3182936fd732STejun Heo 	if (!__sata_set_spd_needed(link, &scontrol))
3183c6fd2807SJeff Garzik 		return 0;
3184c6fd2807SJeff Garzik 
3185936fd732STejun Heo 	if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3186c6fd2807SJeff Garzik 		return rc;
3187c6fd2807SJeff Garzik 
3188c6fd2807SJeff Garzik 	return 1;
3189c6fd2807SJeff Garzik }
3190c6fd2807SJeff Garzik 
3191c6fd2807SJeff Garzik /*
3192c6fd2807SJeff Garzik  * This mode timing computation functionality is ported over from
3193c6fd2807SJeff Garzik  * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
3194c6fd2807SJeff Garzik  */
3195c6fd2807SJeff Garzik /*
3196b352e57dSAlan Cox  * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
3197c6fd2807SJeff Garzik  * These were taken from ATA/ATAPI-6 standard, rev 0a, except
3198b352e57dSAlan Cox  * for UDMA6, which is currently supported only by Maxtor drives.
3199b352e57dSAlan Cox  *
3200b352e57dSAlan Cox  * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0.
3201c6fd2807SJeff Garzik  */
3202c6fd2807SJeff Garzik 
3203c6fd2807SJeff Garzik static const struct ata_timing ata_timing[] = {
32043ada9c12SDavid Daney /*	{ XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 0,  960,   0 }, */
32053ada9c12SDavid Daney 	{ XFER_PIO_0,     70, 290, 240, 600, 165, 150, 0,  600,   0 },
32063ada9c12SDavid Daney 	{ XFER_PIO_1,     50, 290,  93, 383, 125, 100, 0,  383,   0 },
32073ada9c12SDavid Daney 	{ XFER_PIO_2,     30, 290,  40, 330, 100,  90, 0,  240,   0 },
32083ada9c12SDavid Daney 	{ XFER_PIO_3,     30,  80,  70, 180,  80,  70, 0,  180,   0 },
32093ada9c12SDavid Daney 	{ XFER_PIO_4,     25,  70,  25, 120,  70,  25, 0,  120,   0 },
32103ada9c12SDavid Daney 	{ XFER_PIO_5,     15,  65,  25, 100,  65,  25, 0,  100,   0 },
32113ada9c12SDavid Daney 	{ XFER_PIO_6,     10,  55,  20,  80,  55,  20, 0,   80,   0 },
3212c6fd2807SJeff Garzik 
32133ada9c12SDavid Daney 	{ XFER_SW_DMA_0, 120,   0,   0,   0, 480, 480, 50, 960,   0 },
32143ada9c12SDavid Daney 	{ XFER_SW_DMA_1,  90,   0,   0,   0, 240, 240, 30, 480,   0 },
32153ada9c12SDavid Daney 	{ XFER_SW_DMA_2,  60,   0,   0,   0, 120, 120, 20, 240,   0 },
3216c6fd2807SJeff Garzik 
32173ada9c12SDavid Daney 	{ XFER_MW_DMA_0,  60,   0,   0,   0, 215, 215, 20, 480,   0 },
32183ada9c12SDavid Daney 	{ XFER_MW_DMA_1,  45,   0,   0,   0,  80,  50, 5,  150,   0 },
32193ada9c12SDavid Daney 	{ XFER_MW_DMA_2,  25,   0,   0,   0,  70,  25, 5,  120,   0 },
32203ada9c12SDavid Daney 	{ XFER_MW_DMA_3,  25,   0,   0,   0,  65,  25, 5,  100,   0 },
32213ada9c12SDavid Daney 	{ XFER_MW_DMA_4,  25,   0,   0,   0,  55,  20, 5,   80,   0 },
3222c6fd2807SJeff Garzik 
32233ada9c12SDavid Daney /*	{ XFER_UDMA_SLOW,  0,   0,   0,   0,   0,   0, 0,    0, 150 }, */
32243ada9c12SDavid Daney 	{ XFER_UDMA_0,     0,   0,   0,   0,   0,   0, 0,    0, 120 },
32253ada9c12SDavid Daney 	{ XFER_UDMA_1,     0,   0,   0,   0,   0,   0, 0,    0,  80 },
32263ada9c12SDavid Daney 	{ XFER_UDMA_2,     0,   0,   0,   0,   0,   0, 0,    0,  60 },
32273ada9c12SDavid Daney 	{ XFER_UDMA_3,     0,   0,   0,   0,   0,   0, 0,    0,  45 },
32283ada9c12SDavid Daney 	{ XFER_UDMA_4,     0,   0,   0,   0,   0,   0, 0,    0,  30 },
32293ada9c12SDavid Daney 	{ XFER_UDMA_5,     0,   0,   0,   0,   0,   0, 0,    0,  20 },
32303ada9c12SDavid Daney 	{ XFER_UDMA_6,     0,   0,   0,   0,   0,   0, 0,    0,  15 },
3231c6fd2807SJeff Garzik 
3232c6fd2807SJeff Garzik 	{ 0xFF }
3233c6fd2807SJeff Garzik };
3234c6fd2807SJeff Garzik 
3235c6fd2807SJeff Garzik #define ENOUGH(v, unit)		(((v)-1)/(unit)+1)
323623e4c67aSArnd Bergmann #define EZ(v, unit)		((v)?ENOUGH(((v) * 1000), unit):0)
3237c6fd2807SJeff Garzik 
3238c6fd2807SJeff Garzik static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
3239c6fd2807SJeff Garzik {
324023e4c67aSArnd Bergmann 	q->setup	= EZ(t->setup,       T);
324123e4c67aSArnd Bergmann 	q->act8b	= EZ(t->act8b,       T);
324223e4c67aSArnd Bergmann 	q->rec8b	= EZ(t->rec8b,       T);
324323e4c67aSArnd Bergmann 	q->cyc8b	= EZ(t->cyc8b,       T);
324423e4c67aSArnd Bergmann 	q->active	= EZ(t->active,      T);
324523e4c67aSArnd Bergmann 	q->recover	= EZ(t->recover,     T);
324623e4c67aSArnd Bergmann 	q->dmack_hold	= EZ(t->dmack_hold,  T);
324723e4c67aSArnd Bergmann 	q->cycle	= EZ(t->cycle,       T);
324823e4c67aSArnd Bergmann 	q->udma		= EZ(t->udma,       UT);
3249c6fd2807SJeff Garzik }
3250c6fd2807SJeff Garzik 
3251c6fd2807SJeff Garzik void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
3252c6fd2807SJeff Garzik 		      struct ata_timing *m, unsigned int what)
3253c6fd2807SJeff Garzik {
3254c6fd2807SJeff Garzik 	if (what & ATA_TIMING_SETUP  ) m->setup   = max(a->setup,   b->setup);
3255c6fd2807SJeff Garzik 	if (what & ATA_TIMING_ACT8B  ) m->act8b   = max(a->act8b,   b->act8b);
3256c6fd2807SJeff Garzik 	if (what & ATA_TIMING_REC8B  ) m->rec8b   = max(a->rec8b,   b->rec8b);
3257c6fd2807SJeff Garzik 	if (what & ATA_TIMING_CYC8B  ) m->cyc8b   = max(a->cyc8b,   b->cyc8b);
3258c6fd2807SJeff Garzik 	if (what & ATA_TIMING_ACTIVE ) m->active  = max(a->active,  b->active);
3259c6fd2807SJeff Garzik 	if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
32603ada9c12SDavid Daney 	if (what & ATA_TIMING_DMACK_HOLD) m->dmack_hold = max(a->dmack_hold, b->dmack_hold);
3261c6fd2807SJeff Garzik 	if (what & ATA_TIMING_CYCLE  ) m->cycle   = max(a->cycle,   b->cycle);
3262c6fd2807SJeff Garzik 	if (what & ATA_TIMING_UDMA   ) m->udma    = max(a->udma,    b->udma);
3263c6fd2807SJeff Garzik }
3264c6fd2807SJeff Garzik 
32656357357cSTejun Heo const struct ata_timing *ata_timing_find_mode(u8 xfer_mode)
3266c6fd2807SJeff Garzik {
326770cd071eSTejun Heo 	const struct ata_timing *t = ata_timing;
3268c6fd2807SJeff Garzik 
326970cd071eSTejun Heo 	while (xfer_mode > t->mode)
327070cd071eSTejun Heo 		t++;
327170cd071eSTejun Heo 
327270cd071eSTejun Heo 	if (xfer_mode == t->mode)
3273c6fd2807SJeff Garzik 		return t;
3274cd705d5aSBorislav Petkov 
3275cd705d5aSBorislav Petkov 	WARN_ONCE(true, "%s: unable to find timing for xfer_mode 0x%x\n",
3276cd705d5aSBorislav Petkov 			__func__, xfer_mode);
3277cd705d5aSBorislav Petkov 
327870cd071eSTejun Heo 	return NULL;
3279c6fd2807SJeff Garzik }
3280c6fd2807SJeff Garzik 
3281c6fd2807SJeff Garzik int ata_timing_compute(struct ata_device *adev, unsigned short speed,
3282c6fd2807SJeff Garzik 		       struct ata_timing *t, int T, int UT)
3283c6fd2807SJeff Garzik {
32849e8808a9SBartlomiej Zolnierkiewicz 	const u16 *id = adev->id;
3285c6fd2807SJeff Garzik 	const struct ata_timing *s;
3286c6fd2807SJeff Garzik 	struct ata_timing p;
3287c6fd2807SJeff Garzik 
3288c6fd2807SJeff Garzik 	/*
3289c6fd2807SJeff Garzik 	 * Find the mode.
3290c6fd2807SJeff Garzik 	 */
3291c6fd2807SJeff Garzik 
3292c6fd2807SJeff Garzik 	if (!(s = ata_timing_find_mode(speed)))
3293c6fd2807SJeff Garzik 		return -EINVAL;
3294c6fd2807SJeff Garzik 
3295c6fd2807SJeff Garzik 	memcpy(t, s, sizeof(*s));
3296c6fd2807SJeff Garzik 
3297c6fd2807SJeff Garzik 	/*
3298c6fd2807SJeff Garzik 	 * If the drive is an EIDE drive, it can tell us it needs extended
3299c6fd2807SJeff Garzik 	 * PIO/MW_DMA cycle timing.
3300c6fd2807SJeff Garzik 	 */
3301c6fd2807SJeff Garzik 
33029e8808a9SBartlomiej Zolnierkiewicz 	if (id[ATA_ID_FIELD_VALID] & 2) {	/* EIDE drive */
3303c6fd2807SJeff Garzik 		memset(&p, 0, sizeof(p));
33049e8808a9SBartlomiej Zolnierkiewicz 
3305bff00256SBartlomiej Zolnierkiewicz 		if (speed >= XFER_PIO_0 && speed < XFER_SW_DMA_0) {
33069e8808a9SBartlomiej Zolnierkiewicz 			if (speed <= XFER_PIO_2)
33079e8808a9SBartlomiej Zolnierkiewicz 				p.cycle = p.cyc8b = id[ATA_ID_EIDE_PIO];
33089e8808a9SBartlomiej Zolnierkiewicz 			else if ((speed <= XFER_PIO_4) ||
33099e8808a9SBartlomiej Zolnierkiewicz 				 (speed == XFER_PIO_5 && !ata_id_is_cfa(id)))
33109e8808a9SBartlomiej Zolnierkiewicz 				p.cycle = p.cyc8b = id[ATA_ID_EIDE_PIO_IORDY];
33119e8808a9SBartlomiej Zolnierkiewicz 		} else if (speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2)
33129e8808a9SBartlomiej Zolnierkiewicz 			p.cycle = id[ATA_ID_EIDE_DMA_MIN];
33139e8808a9SBartlomiej Zolnierkiewicz 
3314c6fd2807SJeff Garzik 		ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
3315c6fd2807SJeff Garzik 	}
3316c6fd2807SJeff Garzik 
3317c6fd2807SJeff Garzik 	/*
3318c6fd2807SJeff Garzik 	 * Convert the timing to bus clock counts.
3319c6fd2807SJeff Garzik 	 */
3320c6fd2807SJeff Garzik 
3321c6fd2807SJeff Garzik 	ata_timing_quantize(t, t, T, UT);
3322c6fd2807SJeff Garzik 
3323c6fd2807SJeff Garzik 	/*
3324c6fd2807SJeff Garzik 	 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
3325c6fd2807SJeff Garzik 	 * S.M.A.R.T * and some other commands. We have to ensure that the
3326c6fd2807SJeff Garzik 	 * DMA cycle timing is slower/equal than the fastest PIO timing.
3327c6fd2807SJeff Garzik 	 */
3328c6fd2807SJeff Garzik 
3329fd3367afSAlan 	if (speed > XFER_PIO_6) {
3330c6fd2807SJeff Garzik 		ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
3331c6fd2807SJeff Garzik 		ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
3332c6fd2807SJeff Garzik 	}
3333c6fd2807SJeff Garzik 
3334c6fd2807SJeff Garzik 	/*
3335c6fd2807SJeff Garzik 	 * Lengthen active & recovery time so that cycle time is correct.
3336c6fd2807SJeff Garzik 	 */
3337c6fd2807SJeff Garzik 
3338c6fd2807SJeff Garzik 	if (t->act8b + t->rec8b < t->cyc8b) {
3339c6fd2807SJeff Garzik 		t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
3340c6fd2807SJeff Garzik 		t->rec8b = t->cyc8b - t->act8b;
3341c6fd2807SJeff Garzik 	}
3342c6fd2807SJeff Garzik 
3343c6fd2807SJeff Garzik 	if (t->active + t->recover < t->cycle) {
3344c6fd2807SJeff Garzik 		t->active += (t->cycle - (t->active + t->recover)) / 2;
3345c6fd2807SJeff Garzik 		t->recover = t->cycle - t->active;
3346c6fd2807SJeff Garzik 	}
33474f701d1eSAlan Cox 
33484f701d1eSAlan Cox 	/* In a few cases quantisation may produce enough errors to
33494f701d1eSAlan Cox 	   leave t->cycle too low for the sum of active and recovery
33504f701d1eSAlan Cox 	   if so we must correct this */
33514f701d1eSAlan Cox 	if (t->active + t->recover > t->cycle)
33524f701d1eSAlan Cox 		t->cycle = t->active + t->recover;
3353c6fd2807SJeff Garzik 
3354c6fd2807SJeff Garzik 	return 0;
3355c6fd2807SJeff Garzik }
3356c6fd2807SJeff Garzik 
3357c6fd2807SJeff Garzik /**
3358a0f79b92STejun Heo  *	ata_timing_cycle2mode - find xfer mode for the specified cycle duration
3359a0f79b92STejun Heo  *	@xfer_shift: ATA_SHIFT_* value for transfer type to examine.
3360a0f79b92STejun Heo  *	@cycle: cycle duration in ns
3361a0f79b92STejun Heo  *
3362a0f79b92STejun Heo  *	Return matching xfer mode for @cycle.  The returned mode is of
3363a0f79b92STejun Heo  *	the transfer type specified by @xfer_shift.  If @cycle is too
3364a0f79b92STejun Heo  *	slow for @xfer_shift, 0xff is returned.  If @cycle is faster
3365a0f79b92STejun Heo  *	than the fastest known mode, the fasted mode is returned.
3366a0f79b92STejun Heo  *
3367a0f79b92STejun Heo  *	LOCKING:
3368a0f79b92STejun Heo  *	None.
3369a0f79b92STejun Heo  *
3370a0f79b92STejun Heo  *	RETURNS:
3371a0f79b92STejun Heo  *	Matching xfer_mode, 0xff if no match found.
3372a0f79b92STejun Heo  */
3373a0f79b92STejun Heo u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
3374a0f79b92STejun Heo {
3375a0f79b92STejun Heo 	u8 base_mode = 0xff, last_mode = 0xff;
3376a0f79b92STejun Heo 	const struct ata_xfer_ent *ent;
3377a0f79b92STejun Heo 	const struct ata_timing *t;
3378a0f79b92STejun Heo 
3379a0f79b92STejun Heo 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
3380a0f79b92STejun Heo 		if (ent->shift == xfer_shift)
3381a0f79b92STejun Heo 			base_mode = ent->base;
3382a0f79b92STejun Heo 
3383a0f79b92STejun Heo 	for (t = ata_timing_find_mode(base_mode);
3384a0f79b92STejun Heo 	     t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
3385a0f79b92STejun Heo 		unsigned short this_cycle;
3386a0f79b92STejun Heo 
3387a0f79b92STejun Heo 		switch (xfer_shift) {
3388a0f79b92STejun Heo 		case ATA_SHIFT_PIO:
3389a0f79b92STejun Heo 		case ATA_SHIFT_MWDMA:
3390a0f79b92STejun Heo 			this_cycle = t->cycle;
3391a0f79b92STejun Heo 			break;
3392a0f79b92STejun Heo 		case ATA_SHIFT_UDMA:
3393a0f79b92STejun Heo 			this_cycle = t->udma;
3394a0f79b92STejun Heo 			break;
3395a0f79b92STejun Heo 		default:
3396a0f79b92STejun Heo 			return 0xff;
3397a0f79b92STejun Heo 		}
3398a0f79b92STejun Heo 
3399a0f79b92STejun Heo 		if (cycle > this_cycle)
3400a0f79b92STejun Heo 			break;
3401a0f79b92STejun Heo 
3402a0f79b92STejun Heo 		last_mode = t->mode;
3403a0f79b92STejun Heo 	}
3404a0f79b92STejun Heo 
3405a0f79b92STejun Heo 	return last_mode;
3406a0f79b92STejun Heo }
3407a0f79b92STejun Heo 
3408a0f79b92STejun Heo /**
3409c6fd2807SJeff Garzik  *	ata_down_xfermask_limit - adjust dev xfer masks downward
3410c6fd2807SJeff Garzik  *	@dev: Device to adjust xfer masks
3411458337dbSTejun Heo  *	@sel: ATA_DNXFER_* selector
3412c6fd2807SJeff Garzik  *
3413c6fd2807SJeff Garzik  *	Adjust xfer masks of @dev downward.  Note that this function
3414c6fd2807SJeff Garzik  *	does not apply the change.  Invoking ata_set_mode() afterwards
3415c6fd2807SJeff Garzik  *	will apply the limit.
3416c6fd2807SJeff Garzik  *
3417c6fd2807SJeff Garzik  *	LOCKING:
3418c6fd2807SJeff Garzik  *	Inherited from caller.
3419c6fd2807SJeff Garzik  *
3420c6fd2807SJeff Garzik  *	RETURNS:
3421c6fd2807SJeff Garzik  *	0 on success, negative errno on failure
3422c6fd2807SJeff Garzik  */
3423458337dbSTejun Heo int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
3424c6fd2807SJeff Garzik {
3425458337dbSTejun Heo 	char buf[32];
34267dc951aeSTejun Heo 	unsigned long orig_mask, xfer_mask;
34277dc951aeSTejun Heo 	unsigned long pio_mask, mwdma_mask, udma_mask;
3428458337dbSTejun Heo 	int quiet, highbit;
3429c6fd2807SJeff Garzik 
3430458337dbSTejun Heo 	quiet = !!(sel & ATA_DNXFER_QUIET);
3431458337dbSTejun Heo 	sel &= ~ATA_DNXFER_QUIET;
3432458337dbSTejun Heo 
3433458337dbSTejun Heo 	xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
3434458337dbSTejun Heo 						  dev->mwdma_mask,
3435c6fd2807SJeff Garzik 						  dev->udma_mask);
3436458337dbSTejun Heo 	ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
3437c6fd2807SJeff Garzik 
3438458337dbSTejun Heo 	switch (sel) {
3439458337dbSTejun Heo 	case ATA_DNXFER_PIO:
3440458337dbSTejun Heo 		highbit = fls(pio_mask) - 1;
3441458337dbSTejun Heo 		pio_mask &= ~(1 << highbit);
3442458337dbSTejun Heo 		break;
3443458337dbSTejun Heo 
3444458337dbSTejun Heo 	case ATA_DNXFER_DMA:
3445458337dbSTejun Heo 		if (udma_mask) {
3446458337dbSTejun Heo 			highbit = fls(udma_mask) - 1;
3447458337dbSTejun Heo 			udma_mask &= ~(1 << highbit);
3448458337dbSTejun Heo 			if (!udma_mask)
3449458337dbSTejun Heo 				return -ENOENT;
3450458337dbSTejun Heo 		} else if (mwdma_mask) {
3451458337dbSTejun Heo 			highbit = fls(mwdma_mask) - 1;
3452458337dbSTejun Heo 			mwdma_mask &= ~(1 << highbit);
3453458337dbSTejun Heo 			if (!mwdma_mask)
3454458337dbSTejun Heo 				return -ENOENT;
3455458337dbSTejun Heo 		}
3456458337dbSTejun Heo 		break;
3457458337dbSTejun Heo 
3458458337dbSTejun Heo 	case ATA_DNXFER_40C:
3459458337dbSTejun Heo 		udma_mask &= ATA_UDMA_MASK_40C;
3460458337dbSTejun Heo 		break;
3461458337dbSTejun Heo 
3462458337dbSTejun Heo 	case ATA_DNXFER_FORCE_PIO0:
3463458337dbSTejun Heo 		pio_mask &= 1;
346405b83605SGustavo A. R. Silva 		/* fall through */
3465458337dbSTejun Heo 	case ATA_DNXFER_FORCE_PIO:
3466458337dbSTejun Heo 		mwdma_mask = 0;
3467458337dbSTejun Heo 		udma_mask = 0;
3468458337dbSTejun Heo 		break;
3469458337dbSTejun Heo 
3470458337dbSTejun Heo 	default:
3471458337dbSTejun Heo 		BUG();
3472458337dbSTejun Heo 	}
3473458337dbSTejun Heo 
3474458337dbSTejun Heo 	xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
3475458337dbSTejun Heo 
3476458337dbSTejun Heo 	if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
3477458337dbSTejun Heo 		return -ENOENT;
3478458337dbSTejun Heo 
3479458337dbSTejun Heo 	if (!quiet) {
3480458337dbSTejun Heo 		if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
3481458337dbSTejun Heo 			snprintf(buf, sizeof(buf), "%s:%s",
3482458337dbSTejun Heo 				 ata_mode_string(xfer_mask),
3483458337dbSTejun Heo 				 ata_mode_string(xfer_mask & ATA_MASK_PIO));
3484458337dbSTejun Heo 		else
3485458337dbSTejun Heo 			snprintf(buf, sizeof(buf), "%s",
3486458337dbSTejun Heo 				 ata_mode_string(xfer_mask));
3487458337dbSTejun Heo 
3488a9a79dfeSJoe Perches 		ata_dev_warn(dev, "limiting speed to %s\n", buf);
3489458337dbSTejun Heo 	}
3490c6fd2807SJeff Garzik 
3491c6fd2807SJeff Garzik 	ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
3492c6fd2807SJeff Garzik 			    &dev->udma_mask);
3493c6fd2807SJeff Garzik 
3494c6fd2807SJeff Garzik 	return 0;
3495c6fd2807SJeff Garzik }
3496c6fd2807SJeff Garzik 
3497c6fd2807SJeff Garzik static int ata_dev_set_mode(struct ata_device *dev)
3498c6fd2807SJeff Garzik {
3499d0cb43b3STejun Heo 	struct ata_port *ap = dev->link->ap;
35009af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
3501d0cb43b3STejun Heo 	const bool nosetxfer = dev->horkage & ATA_HORKAGE_NOSETXFER;
35024055dee7STejun Heo 	const char *dev_err_whine = "";
35034055dee7STejun Heo 	int ign_dev_err = 0;
3504d0cb43b3STejun Heo 	unsigned int err_mask = 0;
3505c6fd2807SJeff Garzik 	int rc;
3506c6fd2807SJeff Garzik 
3507c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_PIO;
3508c6fd2807SJeff Garzik 	if (dev->xfer_shift == ATA_SHIFT_PIO)
3509c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_PIO;
3510c6fd2807SJeff Garzik 
3511d0cb43b3STejun Heo 	if (nosetxfer && ap->flags & ATA_FLAG_SATA && ata_id_is_sata(dev->id))
3512d0cb43b3STejun Heo 		dev_err_whine = " (SET_XFERMODE skipped)";
3513d0cb43b3STejun Heo 	else {
3514d0cb43b3STejun Heo 		if (nosetxfer)
3515a9a79dfeSJoe Perches 			ata_dev_warn(dev,
3516d0cb43b3STejun Heo 				     "NOSETXFER but PATA detected - can't "
3517d0cb43b3STejun Heo 				     "skip SETXFER, might malfunction\n");
3518c6fd2807SJeff Garzik 		err_mask = ata_dev_set_xfermode(dev);
3519d0cb43b3STejun Heo 	}
35202dcb407eSJeff Garzik 
35214055dee7STejun Heo 	if (err_mask & ~AC_ERR_DEV)
35224055dee7STejun Heo 		goto fail;
35232dcb407eSJeff Garzik 
35244055dee7STejun Heo 	/* revalidate */
3525baa1e78aSTejun Heo 	ehc->i.flags |= ATA_EHI_POST_SETMODE;
3526422c9daaSTejun Heo 	rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
3527baa1e78aSTejun Heo 	ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
3528c6fd2807SJeff Garzik 	if (rc)
3529c6fd2807SJeff Garzik 		return rc;
3530c6fd2807SJeff Garzik 
3531b93fda12SAlan Cox 	if (dev->xfer_shift == ATA_SHIFT_PIO) {
35324055dee7STejun Heo 		/* Old CFA may refuse this command, which is just fine */
3533b93fda12SAlan Cox 		if (ata_id_is_cfa(dev->id))
35344055dee7STejun Heo 			ign_dev_err = 1;
3535b93fda12SAlan Cox 		/* Catch several broken garbage emulations plus some pre
3536b93fda12SAlan Cox 		   ATA devices */
3537b93fda12SAlan Cox 		if (ata_id_major_version(dev->id) == 0 &&
35384055dee7STejun Heo 					dev->pio_mode <= XFER_PIO_2)
35394055dee7STejun Heo 			ign_dev_err = 1;
3540b93fda12SAlan Cox 		/* Some very old devices and some bad newer ones fail
3541b93fda12SAlan Cox 		   any kind of SET_XFERMODE request but support PIO0-2
3542b93fda12SAlan Cox 		   timings and no IORDY */
3543b93fda12SAlan Cox 		if (!ata_id_has_iordy(dev->id) && dev->pio_mode <= XFER_PIO_2)
3544b93fda12SAlan Cox 			ign_dev_err = 1;
3545b93fda12SAlan Cox 	}
35464055dee7STejun Heo 	/* Early MWDMA devices do DMA but don't allow DMA mode setting.
35474055dee7STejun Heo 	   Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
35484055dee7STejun Heo 	if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
35494055dee7STejun Heo 	    dev->dma_mode == XFER_MW_DMA_0 &&
35504055dee7STejun Heo 	    (dev->id[63] >> 8) & 1)
35514055dee7STejun Heo 		ign_dev_err = 1;
35524055dee7STejun Heo 
35534055dee7STejun Heo 	/* if the device is actually configured correctly, ignore dev err */
35544055dee7STejun Heo 	if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id)))
35554055dee7STejun Heo 		ign_dev_err = 1;
35564055dee7STejun Heo 
35574055dee7STejun Heo 	if (err_mask & AC_ERR_DEV) {
35584055dee7STejun Heo 		if (!ign_dev_err)
35594055dee7STejun Heo 			goto fail;
35604055dee7STejun Heo 		else
35614055dee7STejun Heo 			dev_err_whine = " (device error ignored)";
35624055dee7STejun Heo 	}
35634055dee7STejun Heo 
3564c6fd2807SJeff Garzik 	DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
3565c6fd2807SJeff Garzik 		dev->xfer_shift, (int)dev->xfer_mode);
3566c6fd2807SJeff Garzik 
356707b9b6d6SDamien Le Moal 	if (!(ehc->i.flags & ATA_EHI_QUIET) ||
356807b9b6d6SDamien Le Moal 	    ehc->i.flags & ATA_EHI_DID_HARDRESET)
3569a9a79dfeSJoe Perches 		ata_dev_info(dev, "configured for %s%s\n",
35704055dee7STejun Heo 			     ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)),
35714055dee7STejun Heo 			     dev_err_whine);
35724055dee7STejun Heo 
3573c6fd2807SJeff Garzik 	return 0;
35744055dee7STejun Heo 
35754055dee7STejun Heo  fail:
3576a9a79dfeSJoe Perches 	ata_dev_err(dev, "failed to set xfermode (err_mask=0x%x)\n", err_mask);
35774055dee7STejun Heo 	return -EIO;
3578c6fd2807SJeff Garzik }
3579c6fd2807SJeff Garzik 
3580c6fd2807SJeff Garzik /**
358104351821SAlan  *	ata_do_set_mode - Program timings and issue SET FEATURES - XFER
35820260731fSTejun Heo  *	@link: link on which timings will be programmed
35831967b7ffSJoe Perches  *	@r_failed_dev: out parameter for failed device
3584c6fd2807SJeff Garzik  *
358504351821SAlan  *	Standard implementation of the function used to tune and set
358604351821SAlan  *	ATA device disk transfer mode (PIO3, UDMA6, etc.).  If
358704351821SAlan  *	ata_dev_set_mode() fails, pointer to the failing device is
3588c6fd2807SJeff Garzik  *	returned in @r_failed_dev.
3589c6fd2807SJeff Garzik  *
3590c6fd2807SJeff Garzik  *	LOCKING:
3591c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
3592c6fd2807SJeff Garzik  *
3593c6fd2807SJeff Garzik  *	RETURNS:
3594c6fd2807SJeff Garzik  *	0 on success, negative errno otherwise
3595c6fd2807SJeff Garzik  */
359604351821SAlan 
35970260731fSTejun Heo int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
3598c6fd2807SJeff Garzik {
35990260731fSTejun Heo 	struct ata_port *ap = link->ap;
3600c6fd2807SJeff Garzik 	struct ata_device *dev;
3601f58229f8STejun Heo 	int rc = 0, used_dma = 0, found = 0;
3602c6fd2807SJeff Garzik 
3603c6fd2807SJeff Garzik 	/* step 1: calculate xfer_mask */
36041eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
36057dc951aeSTejun Heo 		unsigned long pio_mask, dma_mask;
3606b3a70601SAlan Cox 		unsigned int mode_mask;
3607c6fd2807SJeff Garzik 
3608b3a70601SAlan Cox 		mode_mask = ATA_DMA_MASK_ATA;
3609b3a70601SAlan Cox 		if (dev->class == ATA_DEV_ATAPI)
3610b3a70601SAlan Cox 			mode_mask = ATA_DMA_MASK_ATAPI;
3611b3a70601SAlan Cox 		else if (ata_id_is_cfa(dev->id))
3612b3a70601SAlan Cox 			mode_mask = ATA_DMA_MASK_CFA;
3613b3a70601SAlan Cox 
3614c6fd2807SJeff Garzik 		ata_dev_xfermask(dev);
361533267325STejun Heo 		ata_force_xfermask(dev);
3616c6fd2807SJeff Garzik 
3617c6fd2807SJeff Garzik 		pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
3618b3a70601SAlan Cox 
3619b3a70601SAlan Cox 		if (libata_dma_mask & mode_mask)
362080a9c430SSergei Shtylyov 			dma_mask = ata_pack_xfermask(0, dev->mwdma_mask,
362180a9c430SSergei Shtylyov 						     dev->udma_mask);
3622b3a70601SAlan Cox 		else
3623b3a70601SAlan Cox 			dma_mask = 0;
3624b3a70601SAlan Cox 
3625c6fd2807SJeff Garzik 		dev->pio_mode = ata_xfer_mask2mode(pio_mask);
3626c6fd2807SJeff Garzik 		dev->dma_mode = ata_xfer_mask2mode(dma_mask);
3627c6fd2807SJeff Garzik 
3628c6fd2807SJeff Garzik 		found = 1;
3629b15b3ebaSAlan Cox 		if (ata_dma_enabled(dev))
3630c6fd2807SJeff Garzik 			used_dma = 1;
3631c6fd2807SJeff Garzik 	}
3632c6fd2807SJeff Garzik 	if (!found)
3633c6fd2807SJeff Garzik 		goto out;
3634c6fd2807SJeff Garzik 
3635c6fd2807SJeff Garzik 	/* step 2: always set host PIO timings */
36361eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
363770cd071eSTejun Heo 		if (dev->pio_mode == 0xff) {
3638a9a79dfeSJoe Perches 			ata_dev_warn(dev, "no PIO support\n");
3639c6fd2807SJeff Garzik 			rc = -EINVAL;
3640c6fd2807SJeff Garzik 			goto out;
3641c6fd2807SJeff Garzik 		}
3642c6fd2807SJeff Garzik 
3643c6fd2807SJeff Garzik 		dev->xfer_mode = dev->pio_mode;
3644c6fd2807SJeff Garzik 		dev->xfer_shift = ATA_SHIFT_PIO;
3645c6fd2807SJeff Garzik 		if (ap->ops->set_piomode)
3646c6fd2807SJeff Garzik 			ap->ops->set_piomode(ap, dev);
3647c6fd2807SJeff Garzik 	}
3648c6fd2807SJeff Garzik 
3649c6fd2807SJeff Garzik 	/* step 3: set host DMA timings */
36501eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
36511eca4365STejun Heo 		if (!ata_dma_enabled(dev))
3652c6fd2807SJeff Garzik 			continue;
3653c6fd2807SJeff Garzik 
3654c6fd2807SJeff Garzik 		dev->xfer_mode = dev->dma_mode;
3655c6fd2807SJeff Garzik 		dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
3656c6fd2807SJeff Garzik 		if (ap->ops->set_dmamode)
3657c6fd2807SJeff Garzik 			ap->ops->set_dmamode(ap, dev);
3658c6fd2807SJeff Garzik 	}
3659c6fd2807SJeff Garzik 
3660c6fd2807SJeff Garzik 	/* step 4: update devices' xfer mode */
36611eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
3662c6fd2807SJeff Garzik 		rc = ata_dev_set_mode(dev);
3663c6fd2807SJeff Garzik 		if (rc)
3664c6fd2807SJeff Garzik 			goto out;
3665c6fd2807SJeff Garzik 	}
3666c6fd2807SJeff Garzik 
3667c6fd2807SJeff Garzik 	/* Record simplex status. If we selected DMA then the other
3668c6fd2807SJeff Garzik 	 * host channels are not permitted to do so.
3669c6fd2807SJeff Garzik 	 */
3670cca3974eSJeff Garzik 	if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
3671032af1ceSAlan 		ap->host->simplex_claimed = ap;
3672c6fd2807SJeff Garzik 
3673c6fd2807SJeff Garzik  out:
3674c6fd2807SJeff Garzik 	if (rc)
3675c6fd2807SJeff Garzik 		*r_failed_dev = dev;
3676c6fd2807SJeff Garzik 	return rc;
3677c6fd2807SJeff Garzik }
3678c6fd2807SJeff Garzik 
3679c6fd2807SJeff Garzik /**
3680aa2731adSTejun Heo  *	ata_wait_ready - wait for link to become ready
3681aa2731adSTejun Heo  *	@link: link to be waited on
3682aa2731adSTejun Heo  *	@deadline: deadline jiffies for the operation
3683aa2731adSTejun Heo  *	@check_ready: callback to check link readiness
3684aa2731adSTejun Heo  *
3685aa2731adSTejun Heo  *	Wait for @link to become ready.  @check_ready should return
3686aa2731adSTejun Heo  *	positive number if @link is ready, 0 if it isn't, -ENODEV if
3687aa2731adSTejun Heo  *	link doesn't seem to be occupied, other errno for other error
3688aa2731adSTejun Heo  *	conditions.
3689aa2731adSTejun Heo  *
3690aa2731adSTejun Heo  *	Transient -ENODEV conditions are allowed for
3691aa2731adSTejun Heo  *	ATA_TMOUT_FF_WAIT.
3692aa2731adSTejun Heo  *
3693aa2731adSTejun Heo  *	LOCKING:
3694aa2731adSTejun Heo  *	EH context.
3695aa2731adSTejun Heo  *
3696aa2731adSTejun Heo  *	RETURNS:
3697c9b5560aSMasanari Iida  *	0 if @link is ready before @deadline; otherwise, -errno.
3698aa2731adSTejun Heo  */
3699aa2731adSTejun Heo int ata_wait_ready(struct ata_link *link, unsigned long deadline,
3700aa2731adSTejun Heo 		   int (*check_ready)(struct ata_link *link))
3701aa2731adSTejun Heo {
3702aa2731adSTejun Heo 	unsigned long start = jiffies;
3703b48d58f5STejun Heo 	unsigned long nodev_deadline;
3704aa2731adSTejun Heo 	int warned = 0;
3705aa2731adSTejun Heo 
3706b48d58f5STejun Heo 	/* choose which 0xff timeout to use, read comment in libata.h */
3707b48d58f5STejun Heo 	if (link->ap->host->flags & ATA_HOST_PARALLEL_SCAN)
3708b48d58f5STejun Heo 		nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT_LONG);
3709b48d58f5STejun Heo 	else
3710b48d58f5STejun Heo 		nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT);
3711b48d58f5STejun Heo 
3712b1c72916STejun Heo 	/* Slave readiness can't be tested separately from master.  On
3713b1c72916STejun Heo 	 * M/S emulation configuration, this function should be called
3714b1c72916STejun Heo 	 * only on the master and it will handle both master and slave.
3715b1c72916STejun Heo 	 */
3716b1c72916STejun Heo 	WARN_ON(link == link->ap->slave_link);
3717b1c72916STejun Heo 
3718aa2731adSTejun Heo 	if (time_after(nodev_deadline, deadline))
3719aa2731adSTejun Heo 		nodev_deadline = deadline;
3720aa2731adSTejun Heo 
3721aa2731adSTejun Heo 	while (1) {
3722aa2731adSTejun Heo 		unsigned long now = jiffies;
3723aa2731adSTejun Heo 		int ready, tmp;
3724aa2731adSTejun Heo 
3725aa2731adSTejun Heo 		ready = tmp = check_ready(link);
3726aa2731adSTejun Heo 		if (ready > 0)
3727aa2731adSTejun Heo 			return 0;
3728aa2731adSTejun Heo 
3729b48d58f5STejun Heo 		/*
3730b48d58f5STejun Heo 		 * -ENODEV could be transient.  Ignore -ENODEV if link
3731aa2731adSTejun Heo 		 * is online.  Also, some SATA devices take a long
3732b48d58f5STejun Heo 		 * time to clear 0xff after reset.  Wait for
3733b48d58f5STejun Heo 		 * ATA_TMOUT_FF_WAIT[_LONG] on -ENODEV if link isn't
3734b48d58f5STejun Heo 		 * offline.
3735aa2731adSTejun Heo 		 *
3736aa2731adSTejun Heo 		 * Note that some PATA controllers (pata_ali) explode
3737aa2731adSTejun Heo 		 * if status register is read more than once when
3738aa2731adSTejun Heo 		 * there's no device attached.
3739aa2731adSTejun Heo 		 */
3740aa2731adSTejun Heo 		if (ready == -ENODEV) {
3741aa2731adSTejun Heo 			if (ata_link_online(link))
3742aa2731adSTejun Heo 				ready = 0;
3743aa2731adSTejun Heo 			else if ((link->ap->flags & ATA_FLAG_SATA) &&
3744aa2731adSTejun Heo 				 !ata_link_offline(link) &&
3745aa2731adSTejun Heo 				 time_before(now, nodev_deadline))
3746aa2731adSTejun Heo 				ready = 0;
3747aa2731adSTejun Heo 		}
3748aa2731adSTejun Heo 
3749aa2731adSTejun Heo 		if (ready)
3750aa2731adSTejun Heo 			return ready;
3751aa2731adSTejun Heo 		if (time_after(now, deadline))
3752aa2731adSTejun Heo 			return -EBUSY;
3753aa2731adSTejun Heo 
3754aa2731adSTejun Heo 		if (!warned && time_after(now, start + 5 * HZ) &&
3755aa2731adSTejun Heo 		    (deadline - now > 3 * HZ)) {
3756a9a79dfeSJoe Perches 			ata_link_warn(link,
3757aa2731adSTejun Heo 				"link is slow to respond, please be patient "
3758aa2731adSTejun Heo 				"(ready=%d)\n", tmp);
3759aa2731adSTejun Heo 			warned = 1;
3760aa2731adSTejun Heo 		}
3761aa2731adSTejun Heo 
376297750cebSTejun Heo 		ata_msleep(link->ap, 50);
3763aa2731adSTejun Heo 	}
3764aa2731adSTejun Heo }
3765aa2731adSTejun Heo 
3766aa2731adSTejun Heo /**
3767aa2731adSTejun Heo  *	ata_wait_after_reset - wait for link to become ready after reset
3768aa2731adSTejun Heo  *	@link: link to be waited on
3769aa2731adSTejun Heo  *	@deadline: deadline jiffies for the operation
3770aa2731adSTejun Heo  *	@check_ready: callback to check link readiness
3771aa2731adSTejun Heo  *
3772aa2731adSTejun Heo  *	Wait for @link to become ready after reset.
3773aa2731adSTejun Heo  *
3774aa2731adSTejun Heo  *	LOCKING:
3775aa2731adSTejun Heo  *	EH context.
3776aa2731adSTejun Heo  *
3777aa2731adSTejun Heo  *	RETURNS:
3778c9b5560aSMasanari Iida  *	0 if @link is ready before @deadline; otherwise, -errno.
3779aa2731adSTejun Heo  */
37802b4221bbSHarvey Harrison int ata_wait_after_reset(struct ata_link *link, unsigned long deadline,
3781aa2731adSTejun Heo 				int (*check_ready)(struct ata_link *link))
3782aa2731adSTejun Heo {
378397750cebSTejun Heo 	ata_msleep(link->ap, ATA_WAIT_AFTER_RESET);
3784aa2731adSTejun Heo 
3785aa2731adSTejun Heo 	return ata_wait_ready(link, deadline, check_ready);
3786aa2731adSTejun Heo }
3787aa2731adSTejun Heo 
3788aa2731adSTejun Heo /**
3789936fd732STejun Heo  *	sata_link_debounce - debounce SATA phy status
3790936fd732STejun Heo  *	@link: ATA link to debounce SATA phy status for
3791c9b5560aSMasanari Iida  *	@params: timing parameters { interval, duration, timeout } in msec
3792d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
3793c6fd2807SJeff Garzik  *
3794936fd732STejun Heo  *	Make sure SStatus of @link reaches stable state, determined by
3795c6fd2807SJeff Garzik  *	holding the same value where DET is not 1 for @duration polled
3796c6fd2807SJeff Garzik  *	every @interval, before @timeout.  Timeout constraints the
3797d4b2bab4STejun Heo  *	beginning of the stable state.  Because DET gets stuck at 1 on
3798d4b2bab4STejun Heo  *	some controllers after hot unplugging, this functions waits
3799c6fd2807SJeff Garzik  *	until timeout then returns 0 if DET is stable at 1.
3800c6fd2807SJeff Garzik  *
3801d4b2bab4STejun Heo  *	@timeout is further limited by @deadline.  The sooner of the
3802d4b2bab4STejun Heo  *	two is used.
3803d4b2bab4STejun Heo  *
3804c6fd2807SJeff Garzik  *	LOCKING:
3805c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3806c6fd2807SJeff Garzik  *
3807c6fd2807SJeff Garzik  *	RETURNS:
3808c6fd2807SJeff Garzik  *	0 on success, -errno on failure.
3809c6fd2807SJeff Garzik  */
3810936fd732STejun Heo int sata_link_debounce(struct ata_link *link, const unsigned long *params,
3811d4b2bab4STejun Heo 		       unsigned long deadline)
3812c6fd2807SJeff Garzik {
3813341c2c95STejun Heo 	unsigned long interval = params[0];
3814341c2c95STejun Heo 	unsigned long duration = params[1];
3815d4b2bab4STejun Heo 	unsigned long last_jiffies, t;
3816c6fd2807SJeff Garzik 	u32 last, cur;
3817c6fd2807SJeff Garzik 	int rc;
3818c6fd2807SJeff Garzik 
3819341c2c95STejun Heo 	t = ata_deadline(jiffies, params[2]);
3820d4b2bab4STejun Heo 	if (time_before(t, deadline))
3821d4b2bab4STejun Heo 		deadline = t;
3822d4b2bab4STejun Heo 
3823936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3824c6fd2807SJeff Garzik 		return rc;
3825c6fd2807SJeff Garzik 	cur &= 0xf;
3826c6fd2807SJeff Garzik 
3827c6fd2807SJeff Garzik 	last = cur;
3828c6fd2807SJeff Garzik 	last_jiffies = jiffies;
3829c6fd2807SJeff Garzik 
3830c6fd2807SJeff Garzik 	while (1) {
383197750cebSTejun Heo 		ata_msleep(link->ap, interval);
3832936fd732STejun Heo 		if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3833c6fd2807SJeff Garzik 			return rc;
3834c6fd2807SJeff Garzik 		cur &= 0xf;
3835c6fd2807SJeff Garzik 
3836c6fd2807SJeff Garzik 		/* DET stable? */
3837c6fd2807SJeff Garzik 		if (cur == last) {
3838d4b2bab4STejun Heo 			if (cur == 1 && time_before(jiffies, deadline))
3839c6fd2807SJeff Garzik 				continue;
3840341c2c95STejun Heo 			if (time_after(jiffies,
3841341c2c95STejun Heo 				       ata_deadline(last_jiffies, duration)))
3842c6fd2807SJeff Garzik 				return 0;
3843c6fd2807SJeff Garzik 			continue;
3844c6fd2807SJeff Garzik 		}
3845c6fd2807SJeff Garzik 
3846c6fd2807SJeff Garzik 		/* unstable, start over */
3847c6fd2807SJeff Garzik 		last = cur;
3848c6fd2807SJeff Garzik 		last_jiffies = jiffies;
3849c6fd2807SJeff Garzik 
3850f1545154STejun Heo 		/* Check deadline.  If debouncing failed, return
3851f1545154STejun Heo 		 * -EPIPE to tell upper layer to lower link speed.
3852f1545154STejun Heo 		 */
3853d4b2bab4STejun Heo 		if (time_after(jiffies, deadline))
3854f1545154STejun Heo 			return -EPIPE;
3855c6fd2807SJeff Garzik 	}
3856c6fd2807SJeff Garzik }
3857c6fd2807SJeff Garzik 
3858c6fd2807SJeff Garzik /**
3859936fd732STejun Heo  *	sata_link_resume - resume SATA link
3860936fd732STejun Heo  *	@link: ATA link to resume SATA
3861c9b5560aSMasanari Iida  *	@params: timing parameters { interval, duration, timeout } in msec
3862d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
3863c6fd2807SJeff Garzik  *
3864936fd732STejun Heo  *	Resume SATA phy @link and debounce it.
3865c6fd2807SJeff Garzik  *
3866c6fd2807SJeff Garzik  *	LOCKING:
3867c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3868c6fd2807SJeff Garzik  *
3869c6fd2807SJeff Garzik  *	RETURNS:
3870c6fd2807SJeff Garzik  *	0 on success, -errno on failure.
3871c6fd2807SJeff Garzik  */
3872936fd732STejun Heo int sata_link_resume(struct ata_link *link, const unsigned long *params,
3873d4b2bab4STejun Heo 		     unsigned long deadline)
3874c6fd2807SJeff Garzik {
38755040ab67STejun Heo 	int tries = ATA_LINK_RESUME_TRIES;
3876ac371987STejun Heo 	u32 scontrol, serror;
3877c6fd2807SJeff Garzik 	int rc;
3878c6fd2807SJeff Garzik 
3879936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3880c6fd2807SJeff Garzik 		return rc;
3881c6fd2807SJeff Garzik 
38825040ab67STejun Heo 	/*
38835040ab67STejun Heo 	 * Writes to SControl sometimes get ignored under certain
38845040ab67STejun Heo 	 * controllers (ata_piix SIDPR).  Make sure DET actually is
38855040ab67STejun Heo 	 * cleared.
38865040ab67STejun Heo 	 */
38875040ab67STejun Heo 	do {
3888c6fd2807SJeff Garzik 		scontrol = (scontrol & 0x0f0) | 0x300;
3889936fd732STejun Heo 		if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3890c6fd2807SJeff Garzik 			return rc;
38915040ab67STejun Heo 		/*
38925040ab67STejun Heo 		 * Some PHYs react badly if SStatus is pounded
38935040ab67STejun Heo 		 * immediately after resuming.  Delay 200ms before
38945040ab67STejun Heo 		 * debouncing.
3895c6fd2807SJeff Garzik 		 */
3896e39b2bb3SDanesh Petigara 		if (!(link->flags & ATA_LFLAG_NO_DB_DELAY))
389797750cebSTejun Heo 			ata_msleep(link->ap, 200);
3898c6fd2807SJeff Garzik 
38995040ab67STejun Heo 		/* is SControl restored correctly? */
39005040ab67STejun Heo 		if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
39015040ab67STejun Heo 			return rc;
39025040ab67STejun Heo 	} while ((scontrol & 0xf0f) != 0x300 && --tries);
39035040ab67STejun Heo 
39045040ab67STejun Heo 	if ((scontrol & 0xf0f) != 0x300) {
390538941c95STejun Heo 		ata_link_warn(link, "failed to resume link (SControl %X)\n",
39065040ab67STejun Heo 			     scontrol);
39075040ab67STejun Heo 		return 0;
39085040ab67STejun Heo 	}
39095040ab67STejun Heo 
39105040ab67STejun Heo 	if (tries < ATA_LINK_RESUME_TRIES)
3911a9a79dfeSJoe Perches 		ata_link_warn(link, "link resume succeeded after %d retries\n",
39125040ab67STejun Heo 			      ATA_LINK_RESUME_TRIES - tries);
39135040ab67STejun Heo 
3914ac371987STejun Heo 	if ((rc = sata_link_debounce(link, params, deadline)))
3915ac371987STejun Heo 		return rc;
3916ac371987STejun Heo 
3917f046519fSTejun Heo 	/* clear SError, some PHYs require this even for SRST to work */
3918ac371987STejun Heo 	if (!(rc = sata_scr_read(link, SCR_ERROR, &serror)))
3919ac371987STejun Heo 		rc = sata_scr_write(link, SCR_ERROR, serror);
3920ac371987STejun Heo 
3921f046519fSTejun Heo 	return rc != -EINVAL ? rc : 0;
3922c6fd2807SJeff Garzik }
3923c6fd2807SJeff Garzik 
3924c6fd2807SJeff Garzik /**
39251152b261STejun Heo  *	sata_link_scr_lpm - manipulate SControl IPM and SPM fields
39261152b261STejun Heo  *	@link: ATA link to manipulate SControl for
39271152b261STejun Heo  *	@policy: LPM policy to configure
39281152b261STejun Heo  *	@spm_wakeup: initiate LPM transition to active state
39291152b261STejun Heo  *
39301152b261STejun Heo  *	Manipulate the IPM field of the SControl register of @link
39311152b261STejun Heo  *	according to @policy.  If @policy is ATA_LPM_MAX_POWER and
39321152b261STejun Heo  *	@spm_wakeup is %true, the SPM field is manipulated to wake up
39331152b261STejun Heo  *	the link.  This function also clears PHYRDY_CHG before
39341152b261STejun Heo  *	returning.
39351152b261STejun Heo  *
39361152b261STejun Heo  *	LOCKING:
39371152b261STejun Heo  *	EH context.
39381152b261STejun Heo  *
39391152b261STejun Heo  *	RETURNS:
39408485187bSMasanari Iida  *	0 on success, -errno otherwise.
39411152b261STejun Heo  */
39421152b261STejun Heo int sata_link_scr_lpm(struct ata_link *link, enum ata_lpm_policy policy,
39431152b261STejun Heo 		      bool spm_wakeup)
39441152b261STejun Heo {
39451152b261STejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
39461152b261STejun Heo 	bool woken_up = false;
39471152b261STejun Heo 	u32 scontrol;
39481152b261STejun Heo 	int rc;
39491152b261STejun Heo 
39501152b261STejun Heo 	rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
39511152b261STejun Heo 	if (rc)
39521152b261STejun Heo 		return rc;
39531152b261STejun Heo 
39541152b261STejun Heo 	switch (policy) {
39551152b261STejun Heo 	case ATA_LPM_MAX_POWER:
39561152b261STejun Heo 		/* disable all LPM transitions */
395765fe1f0fSShane Huang 		scontrol |= (0x7 << 8);
39581152b261STejun Heo 		/* initiate transition to active state */
39591152b261STejun Heo 		if (spm_wakeup) {
39601152b261STejun Heo 			scontrol |= (0x4 << 12);
39611152b261STejun Heo 			woken_up = true;
39621152b261STejun Heo 		}
39631152b261STejun Heo 		break;
39641152b261STejun Heo 	case ATA_LPM_MED_POWER:
39651152b261STejun Heo 		/* allow LPM to PARTIAL */
39661152b261STejun Heo 		scontrol &= ~(0x1 << 8);
396765fe1f0fSShane Huang 		scontrol |= (0x6 << 8);
39681152b261STejun Heo 		break;
3969f4ac6476SHans de Goede 	case ATA_LPM_MED_POWER_WITH_DIPM:
39701152b261STejun Heo 	case ATA_LPM_MIN_POWER:
39718a745f1fSKristen Carlson Accardi 		if (ata_link_nr_enabled(link) > 0)
39721152b261STejun Heo 			/* no restrictions on LPM transitions */
397365fe1f0fSShane Huang 			scontrol &= ~(0x7 << 8);
39748a745f1fSKristen Carlson Accardi 		else {
39758a745f1fSKristen Carlson Accardi 			/* empty port, power off */
39768a745f1fSKristen Carlson Accardi 			scontrol &= ~0xf;
39778a745f1fSKristen Carlson Accardi 			scontrol |= (0x1 << 2);
39788a745f1fSKristen Carlson Accardi 		}
39791152b261STejun Heo 		break;
39801152b261STejun Heo 	default:
39811152b261STejun Heo 		WARN_ON(1);
39821152b261STejun Heo 	}
39831152b261STejun Heo 
39841152b261STejun Heo 	rc = sata_scr_write(link, SCR_CONTROL, scontrol);
39851152b261STejun Heo 	if (rc)
39861152b261STejun Heo 		return rc;
39871152b261STejun Heo 
39881152b261STejun Heo 	/* give the link time to transit out of LPM state */
39891152b261STejun Heo 	if (woken_up)
39901152b261STejun Heo 		msleep(10);
39911152b261STejun Heo 
39921152b261STejun Heo 	/* clear PHYRDY_CHG from SError */
39931152b261STejun Heo 	ehc->i.serror &= ~SERR_PHYRDY_CHG;
39941152b261STejun Heo 	return sata_scr_write(link, SCR_ERROR, SERR_PHYRDY_CHG);
39951152b261STejun Heo }
39961152b261STejun Heo 
39971152b261STejun Heo /**
39980aa1113dSTejun Heo  *	ata_std_prereset - prepare for reset
3999cc0680a5STejun Heo  *	@link: ATA link to be reset
4000d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
4001c6fd2807SJeff Garzik  *
4002cc0680a5STejun Heo  *	@link is about to be reset.  Initialize it.  Failure from
4003b8cffc6aSTejun Heo  *	prereset makes libata abort whole reset sequence and give up
4004b8cffc6aSTejun Heo  *	that port, so prereset should be best-effort.  It does its
4005b8cffc6aSTejun Heo  *	best to prepare for reset sequence but if things go wrong, it
4006b8cffc6aSTejun Heo  *	should just whine, not fail.
4007c6fd2807SJeff Garzik  *
4008c6fd2807SJeff Garzik  *	LOCKING:
4009c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
4010c6fd2807SJeff Garzik  *
4011c6fd2807SJeff Garzik  *	RETURNS:
4012c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
4013c6fd2807SJeff Garzik  */
40140aa1113dSTejun Heo int ata_std_prereset(struct ata_link *link, unsigned long deadline)
4015c6fd2807SJeff Garzik {
4016cc0680a5STejun Heo 	struct ata_port *ap = link->ap;
4017936fd732STejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
4018c6fd2807SJeff Garzik 	const unsigned long *timing = sata_ehc_deb_timing(ehc);
4019c6fd2807SJeff Garzik 	int rc;
4020c6fd2807SJeff Garzik 
4021c6fd2807SJeff Garzik 	/* if we're about to do hardreset, nothing more to do */
4022c6fd2807SJeff Garzik 	if (ehc->i.action & ATA_EH_HARDRESET)
4023c6fd2807SJeff Garzik 		return 0;
4024c6fd2807SJeff Garzik 
4025936fd732STejun Heo 	/* if SATA, resume link */
4026a16abc0bSTejun Heo 	if (ap->flags & ATA_FLAG_SATA) {
4027936fd732STejun Heo 		rc = sata_link_resume(link, timing, deadline);
4028b8cffc6aSTejun Heo 		/* whine about phy resume failure but proceed */
4029b8cffc6aSTejun Heo 		if (rc && rc != -EOPNOTSUPP)
4030a9a79dfeSJoe Perches 			ata_link_warn(link,
4031a9a79dfeSJoe Perches 				      "failed to resume link for reset (errno=%d)\n",
4032a9a79dfeSJoe Perches 				      rc);
4033c6fd2807SJeff Garzik 	}
4034c6fd2807SJeff Garzik 
403545db2f6cSTejun Heo 	/* no point in trying softreset on offline link */
4036b1c72916STejun Heo 	if (ata_phys_link_offline(link))
403745db2f6cSTejun Heo 		ehc->i.action &= ~ATA_EH_SOFTRESET;
403845db2f6cSTejun Heo 
4039c6fd2807SJeff Garzik 	return 0;
4040c6fd2807SJeff Garzik }
4041c6fd2807SJeff Garzik 
4042c6fd2807SJeff Garzik /**
4043cc0680a5STejun Heo  *	sata_link_hardreset - reset link via SATA phy reset
4044cc0680a5STejun Heo  *	@link: link to reset
4045c9b5560aSMasanari Iida  *	@timing: timing parameters { interval, duration, timeout } in msec
4046d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
40479dadd45bSTejun Heo  *	@online: optional out parameter indicating link onlineness
40489dadd45bSTejun Heo  *	@check_ready: optional callback to check link readiness
4049c6fd2807SJeff Garzik  *
4050cc0680a5STejun Heo  *	SATA phy-reset @link using DET bits of SControl register.
40519dadd45bSTejun Heo  *	After hardreset, link readiness is waited upon using
40529dadd45bSTejun Heo  *	ata_wait_ready() if @check_ready is specified.  LLDs are
40539dadd45bSTejun Heo  *	allowed to not specify @check_ready and wait itself after this
40549dadd45bSTejun Heo  *	function returns.  Device classification is LLD's
40559dadd45bSTejun Heo  *	responsibility.
40569dadd45bSTejun Heo  *
40579dadd45bSTejun Heo  *	*@online is set to one iff reset succeeded and @link is online
40589dadd45bSTejun Heo  *	after reset.
4059c6fd2807SJeff Garzik  *
4060c6fd2807SJeff Garzik  *	LOCKING:
4061c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
4062c6fd2807SJeff Garzik  *
4063c6fd2807SJeff Garzik  *	RETURNS:
4064c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
4065c6fd2807SJeff Garzik  */
4066cc0680a5STejun Heo int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
40679dadd45bSTejun Heo 			unsigned long deadline,
40689dadd45bSTejun Heo 			bool *online, int (*check_ready)(struct ata_link *))
4069c6fd2807SJeff Garzik {
4070c6fd2807SJeff Garzik 	u32 scontrol;
4071c6fd2807SJeff Garzik 	int rc;
4072c6fd2807SJeff Garzik 
4073c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
4074c6fd2807SJeff Garzik 
40759dadd45bSTejun Heo 	if (online)
40769dadd45bSTejun Heo 		*online = false;
40779dadd45bSTejun Heo 
4078936fd732STejun Heo 	if (sata_set_spd_needed(link)) {
4079c6fd2807SJeff Garzik 		/* SATA spec says nothing about how to reconfigure
4080c6fd2807SJeff Garzik 		 * spd.  To be on the safe side, turn off phy during
4081c6fd2807SJeff Garzik 		 * reconfiguration.  This works for at least ICH7 AHCI
4082c6fd2807SJeff Garzik 		 * and Sil3124.
4083c6fd2807SJeff Garzik 		 */
4084936fd732STejun Heo 		if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
4085b6103f6dSTejun Heo 			goto out;
4086c6fd2807SJeff Garzik 
4087cea0d336SJeff Garzik 		scontrol = (scontrol & 0x0f0) | 0x304;
4088c6fd2807SJeff Garzik 
4089936fd732STejun Heo 		if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
4090b6103f6dSTejun Heo 			goto out;
4091c6fd2807SJeff Garzik 
4092936fd732STejun Heo 		sata_set_spd(link);
4093c6fd2807SJeff Garzik 	}
4094c6fd2807SJeff Garzik 
4095c6fd2807SJeff Garzik 	/* issue phy wake/reset */
4096936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
4097b6103f6dSTejun Heo 		goto out;
4098c6fd2807SJeff Garzik 
4099c6fd2807SJeff Garzik 	scontrol = (scontrol & 0x0f0) | 0x301;
4100c6fd2807SJeff Garzik 
4101936fd732STejun Heo 	if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
4102b6103f6dSTejun Heo 		goto out;
4103c6fd2807SJeff Garzik 
4104c6fd2807SJeff Garzik 	/* Couldn't find anything in SATA I/II specs, but AHCI-1.1
4105c6fd2807SJeff Garzik 	 * 10.4.2 says at least 1 ms.
4106c6fd2807SJeff Garzik 	 */
410797750cebSTejun Heo 	ata_msleep(link->ap, 1);
4108c6fd2807SJeff Garzik 
4109936fd732STejun Heo 	/* bring link back */
4110936fd732STejun Heo 	rc = sata_link_resume(link, timing, deadline);
41119dadd45bSTejun Heo 	if (rc)
41129dadd45bSTejun Heo 		goto out;
41139dadd45bSTejun Heo 	/* if link is offline nothing more to do */
4114b1c72916STejun Heo 	if (ata_phys_link_offline(link))
41159dadd45bSTejun Heo 		goto out;
41169dadd45bSTejun Heo 
41179dadd45bSTejun Heo 	/* Link is online.  From this point, -ENODEV too is an error. */
41189dadd45bSTejun Heo 	if (online)
41199dadd45bSTejun Heo 		*online = true;
41209dadd45bSTejun Heo 
4121071f44b1STejun Heo 	if (sata_pmp_supported(link->ap) && ata_is_host_link(link)) {
41229dadd45bSTejun Heo 		/* If PMP is supported, we have to do follow-up SRST.
41239dadd45bSTejun Heo 		 * Some PMPs don't send D2H Reg FIS after hardreset if
41249dadd45bSTejun Heo 		 * the first port is empty.  Wait only for
41259dadd45bSTejun Heo 		 * ATA_TMOUT_PMP_SRST_WAIT.
41269dadd45bSTejun Heo 		 */
41279dadd45bSTejun Heo 		if (check_ready) {
41289dadd45bSTejun Heo 			unsigned long pmp_deadline;
41299dadd45bSTejun Heo 
4130341c2c95STejun Heo 			pmp_deadline = ata_deadline(jiffies,
4131341c2c95STejun Heo 						    ATA_TMOUT_PMP_SRST_WAIT);
41329dadd45bSTejun Heo 			if (time_after(pmp_deadline, deadline))
41339dadd45bSTejun Heo 				pmp_deadline = deadline;
41349dadd45bSTejun Heo 			ata_wait_ready(link, pmp_deadline, check_ready);
41359dadd45bSTejun Heo 		}
41369dadd45bSTejun Heo 		rc = -EAGAIN;
41379dadd45bSTejun Heo 		goto out;
41389dadd45bSTejun Heo 	}
41399dadd45bSTejun Heo 
41409dadd45bSTejun Heo 	rc = 0;
41419dadd45bSTejun Heo 	if (check_ready)
41429dadd45bSTejun Heo 		rc = ata_wait_ready(link, deadline, check_ready);
4143b6103f6dSTejun Heo  out:
41440cbf0711STejun Heo 	if (rc && rc != -EAGAIN) {
41450cbf0711STejun Heo 		/* online is set iff link is online && reset succeeded */
41460cbf0711STejun Heo 		if (online)
41470cbf0711STejun Heo 			*online = false;
4148a9a79dfeSJoe Perches 		ata_link_err(link, "COMRESET failed (errno=%d)\n", rc);
41490cbf0711STejun Heo 	}
4150b6103f6dSTejun Heo 	DPRINTK("EXIT, rc=%d\n", rc);
4151b6103f6dSTejun Heo 	return rc;
4152b6103f6dSTejun Heo }
4153b6103f6dSTejun Heo 
4154b6103f6dSTejun Heo /**
415557c9efdfSTejun Heo  *	sata_std_hardreset - COMRESET w/o waiting or classification
415657c9efdfSTejun Heo  *	@link: link to reset
415757c9efdfSTejun Heo  *	@class: resulting class of attached device
415857c9efdfSTejun Heo  *	@deadline: deadline jiffies for the operation
415957c9efdfSTejun Heo  *
416057c9efdfSTejun Heo  *	Standard SATA COMRESET w/o waiting or classification.
416157c9efdfSTejun Heo  *
416257c9efdfSTejun Heo  *	LOCKING:
416357c9efdfSTejun Heo  *	Kernel thread context (may sleep)
416457c9efdfSTejun Heo  *
416557c9efdfSTejun Heo  *	RETURNS:
416657c9efdfSTejun Heo  *	0 if link offline, -EAGAIN if link online, -errno on errors.
416757c9efdfSTejun Heo  */
416857c9efdfSTejun Heo int sata_std_hardreset(struct ata_link *link, unsigned int *class,
416957c9efdfSTejun Heo 		       unsigned long deadline)
417057c9efdfSTejun Heo {
417157c9efdfSTejun Heo 	const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
417257c9efdfSTejun Heo 	bool online;
417357c9efdfSTejun Heo 	int rc;
417457c9efdfSTejun Heo 
417557c9efdfSTejun Heo 	/* do hardreset */
417657c9efdfSTejun Heo 	rc = sata_link_hardreset(link, timing, deadline, &online, NULL);
417757c9efdfSTejun Heo 	return online ? -EAGAIN : rc;
417857c9efdfSTejun Heo }
417957c9efdfSTejun Heo 
418057c9efdfSTejun Heo /**
4181203c75b8STejun Heo  *	ata_std_postreset - standard postreset callback
4182cc0680a5STejun Heo  *	@link: the target ata_link
4183c6fd2807SJeff Garzik  *	@classes: classes of attached devices
4184c6fd2807SJeff Garzik  *
4185c6fd2807SJeff Garzik  *	This function is invoked after a successful reset.  Note that
4186c6fd2807SJeff Garzik  *	the device might have been reset more than once using
4187c6fd2807SJeff Garzik  *	different reset methods before postreset is invoked.
4188c6fd2807SJeff Garzik  *
4189c6fd2807SJeff Garzik  *	LOCKING:
4190c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
4191c6fd2807SJeff Garzik  */
4192203c75b8STejun Heo void ata_std_postreset(struct ata_link *link, unsigned int *classes)
4193c6fd2807SJeff Garzik {
4194f046519fSTejun Heo 	u32 serror;
4195f046519fSTejun Heo 
4196c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
4197c6fd2807SJeff Garzik 
4198f046519fSTejun Heo 	/* reset complete, clear SError */
4199f046519fSTejun Heo 	if (!sata_scr_read(link, SCR_ERROR, &serror))
4200f046519fSTejun Heo 		sata_scr_write(link, SCR_ERROR, serror);
4201f046519fSTejun Heo 
4202c6fd2807SJeff Garzik 	/* print link status */
4203936fd732STejun Heo 	sata_print_link_status(link);
4204c6fd2807SJeff Garzik 
4205c6fd2807SJeff Garzik 	DPRINTK("EXIT\n");
4206c6fd2807SJeff Garzik }
4207c6fd2807SJeff Garzik 
4208c6fd2807SJeff Garzik /**
4209c6fd2807SJeff Garzik  *	ata_dev_same_device - Determine whether new ID matches configured device
4210c6fd2807SJeff Garzik  *	@dev: device to compare against
4211c6fd2807SJeff Garzik  *	@new_class: class of the new device
4212c6fd2807SJeff Garzik  *	@new_id: IDENTIFY page of the new device
4213c6fd2807SJeff Garzik  *
4214c6fd2807SJeff Garzik  *	Compare @new_class and @new_id against @dev and determine
4215c6fd2807SJeff Garzik  *	whether @dev is the device indicated by @new_class and
4216c6fd2807SJeff Garzik  *	@new_id.
4217c6fd2807SJeff Garzik  *
4218c6fd2807SJeff Garzik  *	LOCKING:
4219c6fd2807SJeff Garzik  *	None.
4220c6fd2807SJeff Garzik  *
4221c6fd2807SJeff Garzik  *	RETURNS:
4222c6fd2807SJeff Garzik  *	1 if @dev matches @new_class and @new_id, 0 otherwise.
4223c6fd2807SJeff Garzik  */
4224c6fd2807SJeff Garzik static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
4225c6fd2807SJeff Garzik 			       const u16 *new_id)
4226c6fd2807SJeff Garzik {
4227c6fd2807SJeff Garzik 	const u16 *old_id = dev->id;
4228a0cf733bSTejun Heo 	unsigned char model[2][ATA_ID_PROD_LEN + 1];
4229a0cf733bSTejun Heo 	unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
4230c6fd2807SJeff Garzik 
4231c6fd2807SJeff Garzik 	if (dev->class != new_class) {
4232a9a79dfeSJoe Perches 		ata_dev_info(dev, "class mismatch %d != %d\n",
4233c6fd2807SJeff Garzik 			     dev->class, new_class);
4234c6fd2807SJeff Garzik 		return 0;
4235c6fd2807SJeff Garzik 	}
4236c6fd2807SJeff Garzik 
4237a0cf733bSTejun Heo 	ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
4238a0cf733bSTejun Heo 	ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
4239a0cf733bSTejun Heo 	ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
4240a0cf733bSTejun Heo 	ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
4241c6fd2807SJeff Garzik 
4242c6fd2807SJeff Garzik 	if (strcmp(model[0], model[1])) {
4243a9a79dfeSJoe Perches 		ata_dev_info(dev, "model number mismatch '%s' != '%s'\n",
4244a9a79dfeSJoe Perches 			     model[0], model[1]);
4245c6fd2807SJeff Garzik 		return 0;
4246c6fd2807SJeff Garzik 	}
4247c6fd2807SJeff Garzik 
4248c6fd2807SJeff Garzik 	if (strcmp(serial[0], serial[1])) {
4249a9a79dfeSJoe Perches 		ata_dev_info(dev, "serial number mismatch '%s' != '%s'\n",
4250a9a79dfeSJoe Perches 			     serial[0], serial[1]);
4251c6fd2807SJeff Garzik 		return 0;
4252c6fd2807SJeff Garzik 	}
4253c6fd2807SJeff Garzik 
4254c6fd2807SJeff Garzik 	return 1;
4255c6fd2807SJeff Garzik }
4256c6fd2807SJeff Garzik 
4257c6fd2807SJeff Garzik /**
4258fe30911bSTejun Heo  *	ata_dev_reread_id - Re-read IDENTIFY data
42593fae450cSHenrik Kretzschmar  *	@dev: target ATA device
4260bff04647STejun Heo  *	@readid_flags: read ID flags
4261c6fd2807SJeff Garzik  *
4262c6fd2807SJeff Garzik  *	Re-read IDENTIFY page and make sure @dev is still attached to
4263c6fd2807SJeff Garzik  *	the port.
4264c6fd2807SJeff Garzik  *
4265c6fd2807SJeff Garzik  *	LOCKING:
4266c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
4267c6fd2807SJeff Garzik  *
4268c6fd2807SJeff Garzik  *	RETURNS:
4269c6fd2807SJeff Garzik  *	0 on success, negative errno otherwise
4270c6fd2807SJeff Garzik  */
4271fe30911bSTejun Heo int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
4272c6fd2807SJeff Garzik {
4273c6fd2807SJeff Garzik 	unsigned int class = dev->class;
42749af5c9c9STejun Heo 	u16 *id = (void *)dev->link->ap->sector_buf;
4275c6fd2807SJeff Garzik 	int rc;
4276c6fd2807SJeff Garzik 
4277c6fd2807SJeff Garzik 	/* read ID data */
4278bff04647STejun Heo 	rc = ata_dev_read_id(dev, &class, readid_flags, id);
4279c6fd2807SJeff Garzik 	if (rc)
4280fe30911bSTejun Heo 		return rc;
4281c6fd2807SJeff Garzik 
4282c6fd2807SJeff Garzik 	/* is the device still there? */
4283fe30911bSTejun Heo 	if (!ata_dev_same_device(dev, class, id))
4284fe30911bSTejun Heo 		return -ENODEV;
4285c6fd2807SJeff Garzik 
4286c6fd2807SJeff Garzik 	memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
4287fe30911bSTejun Heo 	return 0;
4288fe30911bSTejun Heo }
4289fe30911bSTejun Heo 
4290fe30911bSTejun Heo /**
4291fe30911bSTejun Heo  *	ata_dev_revalidate - Revalidate ATA device
4292fe30911bSTejun Heo  *	@dev: device to revalidate
4293422c9daaSTejun Heo  *	@new_class: new class code
4294fe30911bSTejun Heo  *	@readid_flags: read ID flags
4295fe30911bSTejun Heo  *
4296fe30911bSTejun Heo  *	Re-read IDENTIFY page, make sure @dev is still attached to the
4297fe30911bSTejun Heo  *	port and reconfigure it according to the new IDENTIFY page.
4298fe30911bSTejun Heo  *
4299fe30911bSTejun Heo  *	LOCKING:
4300fe30911bSTejun Heo  *	Kernel thread context (may sleep)
4301fe30911bSTejun Heo  *
4302fe30911bSTejun Heo  *	RETURNS:
4303fe30911bSTejun Heo  *	0 on success, negative errno otherwise
4304fe30911bSTejun Heo  */
4305422c9daaSTejun Heo int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
4306422c9daaSTejun Heo 		       unsigned int readid_flags)
4307fe30911bSTejun Heo {
43086ddcd3b0STejun Heo 	u64 n_sectors = dev->n_sectors;
43095920dadfSTejun Heo 	u64 n_native_sectors = dev->n_native_sectors;
4310fe30911bSTejun Heo 	int rc;
4311fe30911bSTejun Heo 
4312fe30911bSTejun Heo 	if (!ata_dev_enabled(dev))
4313fe30911bSTejun Heo 		return -ENODEV;
4314fe30911bSTejun Heo 
4315422c9daaSTejun Heo 	/* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
4316422c9daaSTejun Heo 	if (ata_class_enabled(new_class) &&
4317f0d0613dSBorislav Petkov 	    new_class != ATA_DEV_ATA &&
4318f0d0613dSBorislav Petkov 	    new_class != ATA_DEV_ATAPI &&
43199162c657SHannes Reinecke 	    new_class != ATA_DEV_ZAC &&
4320f0d0613dSBorislav Petkov 	    new_class != ATA_DEV_SEMB) {
4321a9a79dfeSJoe Perches 		ata_dev_info(dev, "class mismatch %u != %u\n",
4322422c9daaSTejun Heo 			     dev->class, new_class);
4323422c9daaSTejun Heo 		rc = -ENODEV;
4324422c9daaSTejun Heo 		goto fail;
4325422c9daaSTejun Heo 	}
4326422c9daaSTejun Heo 
4327fe30911bSTejun Heo 	/* re-read ID */
4328fe30911bSTejun Heo 	rc = ata_dev_reread_id(dev, readid_flags);
4329fe30911bSTejun Heo 	if (rc)
4330fe30911bSTejun Heo 		goto fail;
4331c6fd2807SJeff Garzik 
4332c6fd2807SJeff Garzik 	/* configure device according to the new ID */
4333efdaedc4STejun Heo 	rc = ata_dev_configure(dev);
43346ddcd3b0STejun Heo 	if (rc)
43356ddcd3b0STejun Heo 		goto fail;
43366ddcd3b0STejun Heo 
43376ddcd3b0STejun Heo 	/* verify n_sectors hasn't changed */
4338445d211bSTejun Heo 	if (dev->class != ATA_DEV_ATA || !n_sectors ||
4339445d211bSTejun Heo 	    dev->n_sectors == n_sectors)
4340445d211bSTejun Heo 		return 0;
4341445d211bSTejun Heo 
4342445d211bSTejun Heo 	/* n_sectors has changed */
4343a9a79dfeSJoe Perches 	ata_dev_warn(dev, "n_sectors mismatch %llu != %llu\n",
43446ddcd3b0STejun Heo 		     (unsigned long long)n_sectors,
43456ddcd3b0STejun Heo 		     (unsigned long long)dev->n_sectors);
4346445d211bSTejun Heo 
43475920dadfSTejun Heo 	/*
43485920dadfSTejun Heo 	 * Something could have caused HPA to be unlocked
4349445d211bSTejun Heo 	 * involuntarily.  If n_native_sectors hasn't changed and the
4350445d211bSTejun Heo 	 * new size matches it, keep the device.
43515920dadfSTejun Heo 	 */
43525920dadfSTejun Heo 	if (dev->n_native_sectors == n_native_sectors &&
4353445d211bSTejun Heo 	    dev->n_sectors > n_sectors && dev->n_sectors == n_native_sectors) {
4354a9a79dfeSJoe Perches 		ata_dev_warn(dev,
43555920dadfSTejun Heo 			     "new n_sectors matches native, probably "
435668939ce5STejun Heo 			     "late HPA unlock, n_sectors updated\n");
435768939ce5STejun Heo 		/* use the larger n_sectors */
4358445d211bSTejun Heo 		return 0;
4359445d211bSTejun Heo 	}
4360445d211bSTejun Heo 
4361445d211bSTejun Heo 	/*
4362445d211bSTejun Heo 	 * Some BIOSes boot w/o HPA but resume w/ HPA locked.  Try
4363445d211bSTejun Heo 	 * unlocking HPA in those cases.
4364445d211bSTejun Heo 	 *
4365445d211bSTejun Heo 	 * https://bugzilla.kernel.org/show_bug.cgi?id=15396
4366445d211bSTejun Heo 	 */
4367445d211bSTejun Heo 	if (dev->n_native_sectors == n_native_sectors &&
4368445d211bSTejun Heo 	    dev->n_sectors < n_sectors && n_sectors == n_native_sectors &&
4369445d211bSTejun Heo 	    !(dev->horkage & ATA_HORKAGE_BROKEN_HPA)) {
4370a9a79dfeSJoe Perches 		ata_dev_warn(dev,
4371445d211bSTejun Heo 			     "old n_sectors matches native, probably "
4372445d211bSTejun Heo 			     "late HPA lock, will try to unlock HPA\n");
4373445d211bSTejun Heo 		/* try unlocking HPA */
4374445d211bSTejun Heo 		dev->flags |= ATA_DFLAG_UNLOCK_HPA;
4375445d211bSTejun Heo 		rc = -EIO;
4376445d211bSTejun Heo 	} else
4377445d211bSTejun Heo 		rc = -ENODEV;
4378445d211bSTejun Heo 
4379445d211bSTejun Heo 	/* restore original n_[native_]sectors and fail */
43805920dadfSTejun Heo 	dev->n_native_sectors = n_native_sectors;
43815920dadfSTejun Heo 	dev->n_sectors = n_sectors;
4382c6fd2807SJeff Garzik  fail:
4383a9a79dfeSJoe Perches 	ata_dev_err(dev, "revalidation failed (errno=%d)\n", rc);
4384c6fd2807SJeff Garzik 	return rc;
4385c6fd2807SJeff Garzik }
4386c6fd2807SJeff Garzik 
43876919a0a6SAlan Cox struct ata_blacklist_entry {
43886919a0a6SAlan Cox 	const char *model_num;
43896919a0a6SAlan Cox 	const char *model_rev;
43906919a0a6SAlan Cox 	unsigned long horkage;
43916919a0a6SAlan Cox };
43926919a0a6SAlan Cox 
43936919a0a6SAlan Cox static const struct ata_blacklist_entry ata_device_blacklist [] = {
43946919a0a6SAlan Cox 	/* Devices with DMA related problems under Linux */
43956919a0a6SAlan Cox 	{ "WDC AC11000H",	NULL,		ATA_HORKAGE_NODMA },
43966919a0a6SAlan Cox 	{ "WDC AC22100H",	NULL,		ATA_HORKAGE_NODMA },
43976919a0a6SAlan Cox 	{ "WDC AC32500H",	NULL,		ATA_HORKAGE_NODMA },
43986919a0a6SAlan Cox 	{ "WDC AC33100H",	NULL,		ATA_HORKAGE_NODMA },
43996919a0a6SAlan Cox 	{ "WDC AC31600H",	NULL,		ATA_HORKAGE_NODMA },
44006919a0a6SAlan Cox 	{ "WDC AC32100H",	"24.09P07",	ATA_HORKAGE_NODMA },
44016919a0a6SAlan Cox 	{ "WDC AC23200L",	"21.10N21",	ATA_HORKAGE_NODMA },
44026919a0a6SAlan Cox 	{ "Compaq CRD-8241B", 	NULL,		ATA_HORKAGE_NODMA },
44036919a0a6SAlan Cox 	{ "CRD-8400B",		NULL, 		ATA_HORKAGE_NODMA },
44047da4c935SMark Lord 	{ "CRD-848[02]B",	NULL,		ATA_HORKAGE_NODMA },
44056919a0a6SAlan Cox 	{ "CRD-84",		NULL,		ATA_HORKAGE_NODMA },
44066919a0a6SAlan Cox 	{ "SanDisk SDP3B",	NULL,		ATA_HORKAGE_NODMA },
44076919a0a6SAlan Cox 	{ "SanDisk SDP3B-64",	NULL,		ATA_HORKAGE_NODMA },
44086919a0a6SAlan Cox 	{ "SANYO CD-ROM CRD",	NULL,		ATA_HORKAGE_NODMA },
44096919a0a6SAlan Cox 	{ "HITACHI CDR-8",	NULL,		ATA_HORKAGE_NODMA },
44107da4c935SMark Lord 	{ "HITACHI CDR-8[34]35",NULL,		ATA_HORKAGE_NODMA },
44116919a0a6SAlan Cox 	{ "Toshiba CD-ROM XM-6202B", NULL,	ATA_HORKAGE_NODMA },
44126919a0a6SAlan Cox 	{ "TOSHIBA CD-ROM XM-1702BC", NULL,	ATA_HORKAGE_NODMA },
44136919a0a6SAlan Cox 	{ "CD-532E-A", 		NULL,		ATA_HORKAGE_NODMA },
44146919a0a6SAlan Cox 	{ "E-IDE CD-ROM CR-840",NULL,		ATA_HORKAGE_NODMA },
44156919a0a6SAlan Cox 	{ "CD-ROM Drive/F5A",	NULL,		ATA_HORKAGE_NODMA },
44166919a0a6SAlan Cox 	{ "WPI CDD-820", 	NULL,		ATA_HORKAGE_NODMA },
44176919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SC-148C", NULL,	ATA_HORKAGE_NODMA },
44186919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SC",	NULL,		ATA_HORKAGE_NODMA },
44196919a0a6SAlan Cox 	{ "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
44206919a0a6SAlan Cox 	{ "_NEC DV5800A", 	NULL,		ATA_HORKAGE_NODMA },
44216919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SN-124", "N001",	ATA_HORKAGE_NODMA },
442239f19886SDave Jones 	{ "Seagate STT20000A", NULL,		ATA_HORKAGE_NODMA },
4423d70e551cSPrarit Bhargava 	{ " 2GB ATA Flash Disk", "ADMA428M",	ATA_HORKAGE_NODMA },
4424b00622fcSShikha Jain 	{ "VRFDFC22048UCHC-TE*", NULL,		ATA_HORKAGE_NODMA },
44253af9a77aSTejun Heo 	/* Odd clown on sil3726/4726 PMPs */
442650af2fa1STejun Heo 	{ "Config  Disk",	NULL,		ATA_HORKAGE_DISABLE },
44276919a0a6SAlan Cox 
442818d6e9d5SAlbert Lee 	/* Weird ATAPI devices */
442940a1d531STejun Heo 	{ "TORiSAN DVD-ROM DRD-N216", NULL,	ATA_HORKAGE_MAX_SEC_128 },
44306a87e42eSTejun Heo 	{ "QUANTUM DAT    DAT72-000", NULL,	ATA_HORKAGE_ATAPI_MOD16_DMA },
4431a32450e1SShan Hai 	{ "Slimtype DVD A  DS8A8SH", NULL,	ATA_HORKAGE_MAX_SEC_LBA48 },
44320523f037SShan Hai 	{ "Slimtype DVD A  DS8A9SH", NULL,	ATA_HORKAGE_MAX_SEC_LBA48 },
443318d6e9d5SAlbert Lee 
4434af34d637SDavid Milburn 	/*
4435af34d637SDavid Milburn 	 * Causes silent data corruption with higher max sects.
4436af34d637SDavid Milburn 	 * http://lkml.kernel.org/g/x49wpy40ysk.fsf@segfault.boston.devel.redhat.com
4437af34d637SDavid Milburn 	 */
4438af34d637SDavid Milburn 	{ "ST380013AS",		"3.20",		ATA_HORKAGE_MAX_SEC_1024 },
4439af34d637SDavid Milburn 
44401488a1e3STejun Heo 	/*
4441e0edc8c5STejun Heo 	 * These devices time out with higher max sects.
44421488a1e3STejun Heo 	 * https://bugzilla.kernel.org/show_bug.cgi?id=121671
44431488a1e3STejun Heo 	 */
4444e0edc8c5STejun Heo 	{ "LITEON CX1-JB*-HP",	NULL,		ATA_HORKAGE_MAX_SEC_1024 },
4445db5ff909SXinyu Lin 	{ "LITEON EP1-*",	NULL,		ATA_HORKAGE_MAX_SEC_1024 },
44461488a1e3STejun Heo 
44476919a0a6SAlan Cox 	/* Devices we expect to fail diagnostics */
44486919a0a6SAlan Cox 
44496919a0a6SAlan Cox 	/* Devices where NCQ should be avoided */
44506919a0a6SAlan Cox 	/* NCQ is slow */
44516919a0a6SAlan Cox 	{ "WDC WD740ADFD-00",	NULL,		ATA_HORKAGE_NONCQ },
4452459ad688STejun Heo 	{ "WDC WD740ADFD-00NLR1", NULL,		ATA_HORKAGE_NONCQ, },
445309125ea6STejun Heo 	/* http://thread.gmane.org/gmane.linux.ide/14907 */
445409125ea6STejun Heo 	{ "FUJITSU MHT2060BH",	NULL,		ATA_HORKAGE_NONCQ },
44557acfaf30SPaul Rolland 	/* NCQ is broken */
4456539cc7c7SJeff Garzik 	{ "Maxtor *",		"BANC*",	ATA_HORKAGE_NONCQ },
44570e3dbc01SAlan Cox 	{ "Maxtor 7V300F0",	"VA111630",	ATA_HORKAGE_NONCQ },
4458da6f0ec2SPaolo Ornati 	{ "ST380817AS",		"3.42",		ATA_HORKAGE_NONCQ },
4459e41bd3e8STejun Heo 	{ "ST3160023AS",	"3.42",		ATA_HORKAGE_NONCQ },
44605ccfca97SLubomir Bulej 	{ "OCZ CORE_SSD",	"02.10104",	ATA_HORKAGE_NONCQ },
4461539cc7c7SJeff Garzik 
4462ac70a964STejun Heo 	/* Seagate NCQ + FLUSH CACHE firmware bug */
44634d1f9082SMark Lord 	{ "ST31500341AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
4464ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4465d10d491fSTejun Heo 
44664d1f9082SMark Lord 	{ "ST31000333AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
4467d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4468d10d491fSTejun Heo 
44694d1f9082SMark Lord 	{ "ST3640[36]23AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
4470d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4471d10d491fSTejun Heo 
44724d1f9082SMark Lord 	{ "ST3320[68]13AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
4473ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4474ac70a964STejun Heo 
447508c85d2aSAleksei Mamlin 	/* drives which fail FPDMA_AA activation (some may freeze afterwards) */
447687809942SMichele Baldessari 	{ "ST1000LM024 HN-M101MBB", "2AR10001",	ATA_HORKAGE_BROKEN_FPDMA_AA },
4477b28a613eSMichele Baldessari 	{ "ST1000LM024 HN-M101MBB", "2BA30001",	ATA_HORKAGE_BROKEN_FPDMA_AA },
447808c85d2aSAleksei Mamlin 	{ "VB0250EAVER",	"HPG7",		ATA_HORKAGE_BROKEN_FPDMA_AA },
447987809942SMichele Baldessari 
448036e337d0SRobert Hancock 	/* Blacklist entries taken from Silicon Image 3124/3132
448136e337d0SRobert Hancock 	   Windows driver .inf file - also several Linux problem reports */
448236e337d0SRobert Hancock 	{ "HTS541060G9SA00",    "MB3OC60D",     ATA_HORKAGE_NONCQ, },
448336e337d0SRobert Hancock 	{ "HTS541080G9SA00",    "MB4OC60D",     ATA_HORKAGE_NONCQ, },
448436e337d0SRobert Hancock 	{ "HTS541010G9SA00",    "MBZOC60D",     ATA_HORKAGE_NONCQ, },
44856919a0a6SAlan Cox 
448668b0ddb2STejun Heo 	/* https://bugzilla.kernel.org/show_bug.cgi?id=15573 */
448768b0ddb2STejun Heo 	{ "C300-CTFDDAC128MAG",	"0001",		ATA_HORKAGE_NONCQ, },
448868b0ddb2STejun Heo 
4489322579dcSTejun Heo 	/* Some Sandisk SSDs lock up hard with NCQ enabled.  Reported on
4490322579dcSTejun Heo 	   SD7SN6S256G and SD8SN8U256G */
4491322579dcSTejun Heo 	{ "SanDisk SD[78]SN*G",	NULL,		ATA_HORKAGE_NONCQ, },
4492322579dcSTejun Heo 
449316c55b03STejun Heo 	/* devices which puke on READ_NATIVE_MAX */
449416c55b03STejun Heo 	{ "HDS724040KLSA80",	"KFAOA20N",	ATA_HORKAGE_BROKEN_HPA, },
449516c55b03STejun Heo 	{ "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
449616c55b03STejun Heo 	{ "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
449716c55b03STejun Heo 	{ "MAXTOR 6L080L4",	"A93.0500",	ATA_HORKAGE_BROKEN_HPA },
44986919a0a6SAlan Cox 
44997831387bSTejun Heo 	/* this one allows HPA unlocking but fails IOs on the area */
45007831387bSTejun Heo 	{ "OCZ-VERTEX",		    "1.30",	ATA_HORKAGE_BROKEN_HPA },
45017831387bSTejun Heo 
450293328e11SAlan Cox 	/* Devices which report 1 sector over size HPA */
450393328e11SAlan Cox 	{ "ST340823A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
450493328e11SAlan Cox 	{ "ST320413A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
4505b152fcd3SMikko Rapeli 	{ "ST310211A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
450693328e11SAlan Cox 
45076bbfd53dSAlan Cox 	/* Devices which get the IVB wrong */
45086bbfd53dSAlan Cox 	{ "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB, },
4509a79067e5SAlan Cox 	/* Maybe we should just blacklist TSSTcorp... */
45107da4c935SMark Lord 	{ "TSSTcorp CDDVDW SH-S202[HJN]", "SB0[01]",  ATA_HORKAGE_IVB, },
45116bbfd53dSAlan Cox 
45129ce8e307SJens Axboe 	/* Devices that do not need bridging limits applied */
45139ce8e307SJens Axboe 	{ "MTRON MSP-SATA*",		NULL,	ATA_HORKAGE_BRIDGE_OK, },
451404d0f1b8SJeff Garzik 	{ "BUFFALO HD-QSU2/R5",		NULL,	ATA_HORKAGE_BRIDGE_OK, },
45159ce8e307SJens Axboe 
45169062712fSTejun Heo 	/* Devices which aren't very happy with higher link speeds */
45179062712fSTejun Heo 	{ "WD My Book",			NULL,	ATA_HORKAGE_1_5_GBPS, },
4518c531077fSDaniel J Blueman 	{ "Seagate FreeAgent GoFlex",	NULL,	ATA_HORKAGE_1_5_GBPS, },
45199062712fSTejun Heo 
4520d0cb43b3STejun Heo 	/*
4521d0cb43b3STejun Heo 	 * Devices which choke on SETXFER.  Applies only if both the
4522d0cb43b3STejun Heo 	 * device and controller are SATA.
4523d0cb43b3STejun Heo 	 */
4524cd691876STejun Heo 	{ "PIONEER DVD-RW  DVRTD08",	NULL,	ATA_HORKAGE_NOSETXFER },
45253a25179eSVladimir LAVALLADE 	{ "PIONEER DVD-RW  DVRTD08A",	NULL,	ATA_HORKAGE_NOSETXFER },
45263a25179eSVladimir LAVALLADE 	{ "PIONEER DVD-RW  DVR-215",	NULL,	ATA_HORKAGE_NOSETXFER },
4527cd691876STejun Heo 	{ "PIONEER DVD-RW  DVR-212D",	NULL,	ATA_HORKAGE_NOSETXFER },
4528cd691876STejun Heo 	{ "PIONEER DVD-RW  DVR-216D",	NULL,	ATA_HORKAGE_NOSETXFER },
4529d0cb43b3STejun Heo 
4530b17e5729SKai-Heng Feng 	/* Crucial BX100 SSD 500GB has broken LPM support */
45313bf7b5d6SHans de Goede 	{ "CT500BX100SSD1",		NULL,	ATA_HORKAGE_NOLPM },
4532b17e5729SKai-Heng Feng 
4533d418ff56SHans de Goede 	/* 512GB MX100 with MU01 firmware has both queued TRIM and LPM issues */
4534d418ff56SHans de Goede 	{ "Crucial_CT512MX100*",	"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
45359c7be59fSHans de Goede 						ATA_HORKAGE_ZERO_AFTER_TRIM |
45369c7be59fSHans de Goede 						ATA_HORKAGE_NOLPM, },
4537d418ff56SHans de Goede 	/* 512GB MX100 with newer firmware has only LPM issues */
4538d418ff56SHans de Goede 	{ "Crucial_CT512MX100*",	NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM |
4539d418ff56SHans de Goede 						ATA_HORKAGE_NOLPM, },
45409c7be59fSHans de Goede 
454162ac3f73SHans de Goede 	/* 480GB+ M500 SSDs have both queued TRIM and LPM issues */
454262ac3f73SHans de Goede 	{ "Crucial_CT480M500*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
454362ac3f73SHans de Goede 						ATA_HORKAGE_ZERO_AFTER_TRIM |
454462ac3f73SHans de Goede 						ATA_HORKAGE_NOLPM, },
454562ac3f73SHans de Goede 	{ "Crucial_CT960M500*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
454662ac3f73SHans de Goede 						ATA_HORKAGE_ZERO_AFTER_TRIM |
454762ac3f73SHans de Goede 						ATA_HORKAGE_NOLPM, },
454862ac3f73SHans de Goede 
454976936e9aSFrançois Cami 	/* These specific Samsung models/firmware-revs do not handle LPM well */
4550b5b4d3a5SHans de Goede 	{ "SAMSUNG MZMPC128HBFU-000MV", "CXM14M1Q", ATA_HORKAGE_NOLPM, },
455176936e9aSFrançois Cami 	{ "SAMSUNG SSD PM830 mSATA *",  "CXM13D1Q", ATA_HORKAGE_NOLPM, },
4552b5b4d3a5SHans de Goede 
4553f78dea06SMarc Carino 	/* devices that don't properly handle queued TRIM commands */
4554136d769eSSudip Mukherjee 	{ "Micron_M500IT_*",		"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
4555136d769eSSudip Mukherjee 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
4556243918beSMartin K. Petersen 	{ "Micron_M500_*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
4557e61f7d1cSMartin K. Petersen 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
4558ff7f53fbSMartin K. Petersen 	{ "Crucial_CT*M500*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
4559ff7f53fbSMartin K. Petersen 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
45609051bd39SMartin K. Petersen 	{ "Micron_M5[15]0_*",		"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
4561ff7f53fbSMartin K. Petersen 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
4562ff7f53fbSMartin K. Petersen 	{ "Crucial_CT*M550*",		"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
4563ff7f53fbSMartin K. Petersen 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
4564ff7f53fbSMartin K. Petersen 	{ "Crucial_CT*MX100*",		"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
4565ff7f53fbSMartin K. Petersen 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
4566ca6bfcb2SJu Hyung Park 	{ "Samsung SSD 840*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
4567ca6bfcb2SJu Hyung Park 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
4568ca6bfcb2SJu Hyung Park 	{ "Samsung SSD 850*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
45696fc4d97aSMartin K. Petersen 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
45707a7184b0SGuillermo A. Amaral 	{ "FCCT*M500*",			NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
45717a7184b0SGuillermo A. Amaral 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
4572e61f7d1cSMartin K. Petersen 
4573cda57b1bSArne Fitzenreiter 	/* devices that don't properly handle TRIM commands */
4574cda57b1bSArne Fitzenreiter 	{ "SuperSSpeed S238*",		NULL,	ATA_HORKAGE_NOTRIM, },
4575cda57b1bSArne Fitzenreiter 
4576e61f7d1cSMartin K. Petersen 	/*
4577e61f7d1cSMartin K. Petersen 	 * As defined, the DRAT (Deterministic Read After Trim) and RZAT
4578e61f7d1cSMartin K. Petersen 	 * (Return Zero After Trim) flags in the ATA Command Set are
4579e61f7d1cSMartin K. Petersen 	 * unreliable in the sense that they only define what happens if
4580e61f7d1cSMartin K. Petersen 	 * the device successfully executed the DSM TRIM command. TRIM
4581e61f7d1cSMartin K. Petersen 	 * is only advisory, however, and the device is free to silently
4582e61f7d1cSMartin K. Petersen 	 * ignore all or parts of the request.
4583e61f7d1cSMartin K. Petersen 	 *
4584e61f7d1cSMartin K. Petersen 	 * Whitelist drives that are known to reliably return zeroes
4585e61f7d1cSMartin K. Petersen 	 * after TRIM.
4586e61f7d1cSMartin K. Petersen 	 */
4587e61f7d1cSMartin K. Petersen 
4588e61f7d1cSMartin K. Petersen 	/*
4589e61f7d1cSMartin K. Petersen 	 * The intel 510 drive has buggy DRAT/RZAT. Explicitly exclude
4590e61f7d1cSMartin K. Petersen 	 * that model before whitelisting all other intel SSDs.
4591e61f7d1cSMartin K. Petersen 	 */
4592e61f7d1cSMartin K. Petersen 	{ "INTEL*SSDSC2MH*",		NULL,	0, },
4593e61f7d1cSMartin K. Petersen 
4594ff7f53fbSMartin K. Petersen 	{ "Micron*",			NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM, },
4595ff7f53fbSMartin K. Petersen 	{ "Crucial*",			NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM, },
4596e61f7d1cSMartin K. Petersen 	{ "INTEL*SSD*", 		NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM, },
4597e61f7d1cSMartin K. Petersen 	{ "SSD*INTEL*",			NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM, },
4598e61f7d1cSMartin K. Petersen 	{ "Samsung*SSD*",		NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM, },
4599e61f7d1cSMartin K. Petersen 	{ "SAMSUNG*SSD*",		NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM, },
4600e61f7d1cSMartin K. Petersen 	{ "ST[1248][0248]0[FH]*",	NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM, },
4601f78dea06SMarc Carino 
4602ecd75ad5STejun Heo 	/*
4603ecd75ad5STejun Heo 	 * Some WD SATA-I drives spin up and down erratically when the link
4604ecd75ad5STejun Heo 	 * is put into the slumber mode.  We don't have full list of the
4605ecd75ad5STejun Heo 	 * affected devices.  Disable LPM if the device matches one of the
4606ecd75ad5STejun Heo 	 * known prefixes and is SATA-1.  As a side effect LPM partial is
4607ecd75ad5STejun Heo 	 * lost too.
4608ecd75ad5STejun Heo 	 *
4609ecd75ad5STejun Heo 	 * https://bugzilla.kernel.org/show_bug.cgi?id=57211
4610ecd75ad5STejun Heo 	 */
4611ecd75ad5STejun Heo 	{ "WDC WD800JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4612ecd75ad5STejun Heo 	{ "WDC WD1200JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4613ecd75ad5STejun Heo 	{ "WDC WD1600JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4614ecd75ad5STejun Heo 	{ "WDC WD2000JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4615ecd75ad5STejun Heo 	{ "WDC WD2500JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4616ecd75ad5STejun Heo 	{ "WDC WD3000JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4617ecd75ad5STejun Heo 	{ "WDC WD3200JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4618ecd75ad5STejun Heo 
46196919a0a6SAlan Cox 	/* End Marker */
46206919a0a6SAlan Cox 	{ }
4621c6fd2807SJeff Garzik };
4622c6fd2807SJeff Garzik 
462375683fe7STejun Heo static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
4624c6fd2807SJeff Garzik {
46258bfa79fcSTejun Heo 	unsigned char model_num[ATA_ID_PROD_LEN + 1];
46268bfa79fcSTejun Heo 	unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
46276919a0a6SAlan Cox 	const struct ata_blacklist_entry *ad = ata_device_blacklist;
4628c6fd2807SJeff Garzik 
46298bfa79fcSTejun Heo 	ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
46308bfa79fcSTejun Heo 	ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
4631c6fd2807SJeff Garzik 
46326919a0a6SAlan Cox 	while (ad->model_num) {
46331c402799SGeorge Spelvin 		if (glob_match(ad->model_num, model_num)) {
46346919a0a6SAlan Cox 			if (ad->model_rev == NULL)
46356919a0a6SAlan Cox 				return ad->horkage;
46361c402799SGeorge Spelvin 			if (glob_match(ad->model_rev, model_rev))
46376919a0a6SAlan Cox 				return ad->horkage;
4638c6fd2807SJeff Garzik 		}
46396919a0a6SAlan Cox 		ad++;
4640c6fd2807SJeff Garzik 	}
4641c6fd2807SJeff Garzik 	return 0;
4642c6fd2807SJeff Garzik }
4643c6fd2807SJeff Garzik 
46446919a0a6SAlan Cox static int ata_dma_blacklisted(const struct ata_device *dev)
46456919a0a6SAlan Cox {
46466919a0a6SAlan Cox 	/* We don't support polling DMA.
46476919a0a6SAlan Cox 	 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
46486919a0a6SAlan Cox 	 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
46496919a0a6SAlan Cox 	 */
46509af5c9c9STejun Heo 	if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
46516919a0a6SAlan Cox 	    (dev->flags & ATA_DFLAG_CDB_INTR))
46526919a0a6SAlan Cox 		return 1;
465375683fe7STejun Heo 	return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
46546919a0a6SAlan Cox }
46556919a0a6SAlan Cox 
4656c6fd2807SJeff Garzik /**
46576bbfd53dSAlan Cox  *	ata_is_40wire		-	check drive side detection
46586bbfd53dSAlan Cox  *	@dev: device
46596bbfd53dSAlan Cox  *
46606bbfd53dSAlan Cox  *	Perform drive side detection decoding, allowing for device vendors
46616bbfd53dSAlan Cox  *	who can't follow the documentation.
46626bbfd53dSAlan Cox  */
46636bbfd53dSAlan Cox 
46646bbfd53dSAlan Cox static int ata_is_40wire(struct ata_device *dev)
46656bbfd53dSAlan Cox {
46666bbfd53dSAlan Cox 	if (dev->horkage & ATA_HORKAGE_IVB)
46676bbfd53dSAlan Cox 		return ata_drive_40wire_relaxed(dev->id);
46686bbfd53dSAlan Cox 	return ata_drive_40wire(dev->id);
46696bbfd53dSAlan Cox }
46706bbfd53dSAlan Cox 
46716bbfd53dSAlan Cox /**
467215a5551cSAlan Cox  *	cable_is_40wire		-	40/80/SATA decider
467315a5551cSAlan Cox  *	@ap: port to consider
467415a5551cSAlan Cox  *
467515a5551cSAlan Cox  *	This function encapsulates the policy for speed management
467615a5551cSAlan Cox  *	in one place. At the moment we don't cache the result but
467715a5551cSAlan Cox  *	there is a good case for setting ap->cbl to the result when
467815a5551cSAlan Cox  *	we are called with unknown cables (and figuring out if it
467915a5551cSAlan Cox  *	impacts hotplug at all).
468015a5551cSAlan Cox  *
468115a5551cSAlan Cox  *	Return 1 if the cable appears to be 40 wire.
468215a5551cSAlan Cox  */
468315a5551cSAlan Cox 
468415a5551cSAlan Cox static int cable_is_40wire(struct ata_port *ap)
468515a5551cSAlan Cox {
468615a5551cSAlan Cox 	struct ata_link *link;
468715a5551cSAlan Cox 	struct ata_device *dev;
468815a5551cSAlan Cox 
46894a9c7b33STejun Heo 	/* If the controller thinks we are 40 wire, we are. */
469015a5551cSAlan Cox 	if (ap->cbl == ATA_CBL_PATA40)
469115a5551cSAlan Cox 		return 1;
46924a9c7b33STejun Heo 
46934a9c7b33STejun Heo 	/* If the controller thinks we are 80 wire, we are. */
469415a5551cSAlan Cox 	if (ap->cbl == ATA_CBL_PATA80 || ap->cbl == ATA_CBL_SATA)
469515a5551cSAlan Cox 		return 0;
46964a9c7b33STejun Heo 
46974a9c7b33STejun Heo 	/* If the system is known to be 40 wire short cable (eg
46984a9c7b33STejun Heo 	 * laptop), then we allow 80 wire modes even if the drive
46994a9c7b33STejun Heo 	 * isn't sure.
47004a9c7b33STejun Heo 	 */
4701f792068eSAlan Cox 	if (ap->cbl == ATA_CBL_PATA40_SHORT)
4702f792068eSAlan Cox 		return 0;
470315a5551cSAlan Cox 
47044a9c7b33STejun Heo 	/* If the controller doesn't know, we scan.
47054a9c7b33STejun Heo 	 *
47064a9c7b33STejun Heo 	 * Note: We look for all 40 wire detects at this point.  Any
47074a9c7b33STejun Heo 	 *       80 wire detect is taken to be 80 wire cable because
47084a9c7b33STejun Heo 	 * - in many setups only the one drive (slave if present) will
47094a9c7b33STejun Heo 	 *   give a valid detect
47104a9c7b33STejun Heo 	 * - if you have a non detect capable drive you don't want it
47114a9c7b33STejun Heo 	 *   to colour the choice
471215a5551cSAlan Cox 	 */
47131eca4365STejun Heo 	ata_for_each_link(link, ap, EDGE) {
47141eca4365STejun Heo 		ata_for_each_dev(dev, link, ENABLED) {
47151eca4365STejun Heo 			if (!ata_is_40wire(dev))
471615a5551cSAlan Cox 				return 0;
471715a5551cSAlan Cox 		}
471815a5551cSAlan Cox 	}
471915a5551cSAlan Cox 	return 1;
472015a5551cSAlan Cox }
472115a5551cSAlan Cox 
472215a5551cSAlan Cox /**
4723c6fd2807SJeff Garzik  *	ata_dev_xfermask - Compute supported xfermask of the given device
4724c6fd2807SJeff Garzik  *	@dev: Device to compute xfermask for
4725c6fd2807SJeff Garzik  *
4726c6fd2807SJeff Garzik  *	Compute supported xfermask of @dev and store it in
4727c6fd2807SJeff Garzik  *	dev->*_mask.  This function is responsible for applying all
4728c6fd2807SJeff Garzik  *	known limits including host controller limits, device
4729c6fd2807SJeff Garzik  *	blacklist, etc...
4730c6fd2807SJeff Garzik  *
4731c6fd2807SJeff Garzik  *	LOCKING:
4732c6fd2807SJeff Garzik  *	None.
4733c6fd2807SJeff Garzik  */
4734c6fd2807SJeff Garzik static void ata_dev_xfermask(struct ata_device *dev)
4735c6fd2807SJeff Garzik {
47369af5c9c9STejun Heo 	struct ata_link *link = dev->link;
47379af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
4738cca3974eSJeff Garzik 	struct ata_host *host = ap->host;
4739c6fd2807SJeff Garzik 	unsigned long xfer_mask;
4740c6fd2807SJeff Garzik 
4741c6fd2807SJeff Garzik 	/* controller modes available */
4742c6fd2807SJeff Garzik 	xfer_mask = ata_pack_xfermask(ap->pio_mask,
4743c6fd2807SJeff Garzik 				      ap->mwdma_mask, ap->udma_mask);
4744c6fd2807SJeff Garzik 
47458343f889SRobert Hancock 	/* drive modes available */
4746c6fd2807SJeff Garzik 	xfer_mask &= ata_pack_xfermask(dev->pio_mask,
4747c6fd2807SJeff Garzik 				       dev->mwdma_mask, dev->udma_mask);
4748c6fd2807SJeff Garzik 	xfer_mask &= ata_id_xfermask(dev->id);
4749c6fd2807SJeff Garzik 
4750b352e57dSAlan Cox 	/*
4751b352e57dSAlan Cox 	 *	CFA Advanced TrueIDE timings are not allowed on a shared
4752b352e57dSAlan Cox 	 *	cable
4753b352e57dSAlan Cox 	 */
4754b352e57dSAlan Cox 	if (ata_dev_pair(dev)) {
4755b352e57dSAlan Cox 		/* No PIO5 or PIO6 */
4756b352e57dSAlan Cox 		xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
4757b352e57dSAlan Cox 		/* No MWDMA3 or MWDMA 4 */
4758b352e57dSAlan Cox 		xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
4759b352e57dSAlan Cox 	}
4760b352e57dSAlan Cox 
4761c6fd2807SJeff Garzik 	if (ata_dma_blacklisted(dev)) {
4762c6fd2807SJeff Garzik 		xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4763a9a79dfeSJoe Perches 		ata_dev_warn(dev,
4764c6fd2807SJeff Garzik 			     "device is on DMA blacklist, disabling DMA\n");
4765c6fd2807SJeff Garzik 	}
4766c6fd2807SJeff Garzik 
476714d66ab7SPetr Vandrovec 	if ((host->flags & ATA_HOST_SIMPLEX) &&
476814d66ab7SPetr Vandrovec 	    host->simplex_claimed && host->simplex_claimed != ap) {
4769c6fd2807SJeff Garzik 		xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4770a9a79dfeSJoe Perches 		ata_dev_warn(dev,
4771a9a79dfeSJoe Perches 			     "simplex DMA is claimed by other device, disabling DMA\n");
4772c6fd2807SJeff Garzik 	}
4773c6fd2807SJeff Garzik 
4774e424675fSJeff Garzik 	if (ap->flags & ATA_FLAG_NO_IORDY)
4775e424675fSJeff Garzik 		xfer_mask &= ata_pio_mask_no_iordy(dev);
4776e424675fSJeff Garzik 
4777c6fd2807SJeff Garzik 	if (ap->ops->mode_filter)
4778a76b62caSAlan Cox 		xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
4779c6fd2807SJeff Garzik 
47808343f889SRobert Hancock 	/* Apply cable rule here.  Don't apply it early because when
47818343f889SRobert Hancock 	 * we handle hot plug the cable type can itself change.
47828343f889SRobert Hancock 	 * Check this last so that we know if the transfer rate was
47838343f889SRobert Hancock 	 * solely limited by the cable.
47848343f889SRobert Hancock 	 * Unknown or 80 wire cables reported host side are checked
47858343f889SRobert Hancock 	 * drive side as well. Cases where we know a 40wire cable
47868343f889SRobert Hancock 	 * is used safely for 80 are not checked here.
47878343f889SRobert Hancock 	 */
47888343f889SRobert Hancock 	if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
47898343f889SRobert Hancock 		/* UDMA/44 or higher would be available */
479015a5551cSAlan Cox 		if (cable_is_40wire(ap)) {
4791a9a79dfeSJoe Perches 			ata_dev_warn(dev,
47928343f889SRobert Hancock 				     "limited to UDMA/33 due to 40-wire cable\n");
47938343f889SRobert Hancock 			xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
47948343f889SRobert Hancock 		}
47958343f889SRobert Hancock 
4796c6fd2807SJeff Garzik 	ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
4797c6fd2807SJeff Garzik 			    &dev->mwdma_mask, &dev->udma_mask);
4798c6fd2807SJeff Garzik }
4799c6fd2807SJeff Garzik 
4800c6fd2807SJeff Garzik /**
4801c6fd2807SJeff Garzik  *	ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
4802c6fd2807SJeff Garzik  *	@dev: Device to which command will be sent
4803c6fd2807SJeff Garzik  *
4804c6fd2807SJeff Garzik  *	Issue SET FEATURES - XFER MODE command to device @dev
4805c6fd2807SJeff Garzik  *	on port @ap.
4806c6fd2807SJeff Garzik  *
4807c6fd2807SJeff Garzik  *	LOCKING:
4808c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
4809c6fd2807SJeff Garzik  *
4810c6fd2807SJeff Garzik  *	RETURNS:
4811c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
4812c6fd2807SJeff Garzik  */
4813c6fd2807SJeff Garzik 
4814c6fd2807SJeff Garzik static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
4815c6fd2807SJeff Garzik {
4816c6fd2807SJeff Garzik 	struct ata_taskfile tf;
4817c6fd2807SJeff Garzik 	unsigned int err_mask;
4818c6fd2807SJeff Garzik 
4819c6fd2807SJeff Garzik 	/* set up set-features taskfile */
4820c6fd2807SJeff Garzik 	DPRINTK("set features - xfer mode\n");
4821c6fd2807SJeff Garzik 
4822464cf177STejun Heo 	/* Some controllers and ATAPI devices show flaky interrupt
4823464cf177STejun Heo 	 * behavior after setting xfer mode.  Use polling instead.
4824464cf177STejun Heo 	 */
4825c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
4826c6fd2807SJeff Garzik 	tf.command = ATA_CMD_SET_FEATURES;
4827c6fd2807SJeff Garzik 	tf.feature = SETFEATURES_XFER;
4828464cf177STejun Heo 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
4829c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
4830b9f8ab2dSAlan Cox 	/* If we are using IORDY we must send the mode setting command */
4831b9f8ab2dSAlan Cox 	if (ata_pio_need_iordy(dev))
4832c6fd2807SJeff Garzik 		tf.nsect = dev->xfer_mode;
4833b9f8ab2dSAlan Cox 	/* If the device has IORDY and the controller does not - turn it off */
4834b9f8ab2dSAlan Cox  	else if (ata_id_has_iordy(dev->id))
4835b9f8ab2dSAlan Cox 		tf.nsect = 0x01;
4836b9f8ab2dSAlan Cox 	else /* In the ancient relic department - skip all of this */
4837b9f8ab2dSAlan Cox 		return 0;
4838c6fd2807SJeff Garzik 
4839d531be2cSMikulas Patocka 	/* On some disks, this command causes spin-up, so we need longer timeout */
4840d531be2cSMikulas Patocka 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 15000);
4841c6fd2807SJeff Garzik 
4842c6fd2807SJeff Garzik 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
4843c6fd2807SJeff Garzik 	return err_mask;
4844c6fd2807SJeff Garzik }
48451152b261STejun Heo 
4846c6fd2807SJeff Garzik /**
4847218f3d30SJeff Garzik  *	ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES
48489f45cbd3SKristen Carlson Accardi  *	@dev: Device to which command will be sent
48499f45cbd3SKristen Carlson Accardi  *	@enable: Whether to enable or disable the feature
4850218f3d30SJeff Garzik  *	@feature: The sector count represents the feature to set
48519f45cbd3SKristen Carlson Accardi  *
48529f45cbd3SKristen Carlson Accardi  *	Issue SET FEATURES - SATA FEATURES command to device @dev
4853218f3d30SJeff Garzik  *	on port @ap with sector count
48549f45cbd3SKristen Carlson Accardi  *
48559f45cbd3SKristen Carlson Accardi  *	LOCKING:
48569f45cbd3SKristen Carlson Accardi  *	PCI/etc. bus probe sem.
48579f45cbd3SKristen Carlson Accardi  *
48589f45cbd3SKristen Carlson Accardi  *	RETURNS:
48599f45cbd3SKristen Carlson Accardi  *	0 on success, AC_ERR_* mask otherwise.
48609f45cbd3SKristen Carlson Accardi  */
48611152b261STejun Heo unsigned int ata_dev_set_feature(struct ata_device *dev, u8 enable, u8 feature)
48629f45cbd3SKristen Carlson Accardi {
48639f45cbd3SKristen Carlson Accardi 	struct ata_taskfile tf;
48649f45cbd3SKristen Carlson Accardi 	unsigned int err_mask;
4865974e0a45SDamien Le Moal 	unsigned long timeout = 0;
48669f45cbd3SKristen Carlson Accardi 
48679f45cbd3SKristen Carlson Accardi 	/* set up set-features taskfile */
48689f45cbd3SKristen Carlson Accardi 	DPRINTK("set features - SATA features\n");
48699f45cbd3SKristen Carlson Accardi 
48709f45cbd3SKristen Carlson Accardi 	ata_tf_init(dev, &tf);
48719f45cbd3SKristen Carlson Accardi 	tf.command = ATA_CMD_SET_FEATURES;
48729f45cbd3SKristen Carlson Accardi 	tf.feature = enable;
48739f45cbd3SKristen Carlson Accardi 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
48749f45cbd3SKristen Carlson Accardi 	tf.protocol = ATA_PROT_NODATA;
4875218f3d30SJeff Garzik 	tf.nsect = feature;
48769f45cbd3SKristen Carlson Accardi 
4877974e0a45SDamien Le Moal 	if (enable == SETFEATURES_SPINUP)
4878974e0a45SDamien Le Moal 		timeout = ata_probe_timeout ?
4879974e0a45SDamien Le Moal 			  ata_probe_timeout * 1000 : SETFEATURES_SPINUP_TIMEOUT;
4880974e0a45SDamien Le Moal 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, timeout);
48819f45cbd3SKristen Carlson Accardi 
48829f45cbd3SKristen Carlson Accardi 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
48839f45cbd3SKristen Carlson Accardi 	return err_mask;
48849f45cbd3SKristen Carlson Accardi }
4885633de4ccSJeff Garzik EXPORT_SYMBOL_GPL(ata_dev_set_feature);
48869f45cbd3SKristen Carlson Accardi 
48879f45cbd3SKristen Carlson Accardi /**
4888c6fd2807SJeff Garzik  *	ata_dev_init_params - Issue INIT DEV PARAMS command
4889c6fd2807SJeff Garzik  *	@dev: Device to which command will be sent
4890c6fd2807SJeff Garzik  *	@heads: Number of heads (taskfile parameter)
4891c6fd2807SJeff Garzik  *	@sectors: Number of sectors (taskfile parameter)
4892c6fd2807SJeff Garzik  *
4893c6fd2807SJeff Garzik  *	LOCKING:
4894c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
4895c6fd2807SJeff Garzik  *
4896c6fd2807SJeff Garzik  *	RETURNS:
4897c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
4898c6fd2807SJeff Garzik  */
4899c6fd2807SJeff Garzik static unsigned int ata_dev_init_params(struct ata_device *dev,
4900c6fd2807SJeff Garzik 					u16 heads, u16 sectors)
4901c6fd2807SJeff Garzik {
4902c6fd2807SJeff Garzik 	struct ata_taskfile tf;
4903c6fd2807SJeff Garzik 	unsigned int err_mask;
4904c6fd2807SJeff Garzik 
4905c6fd2807SJeff Garzik 	/* Number of sectors per track 1-255. Number of heads 1-16 */
4906c6fd2807SJeff Garzik 	if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
4907c6fd2807SJeff Garzik 		return AC_ERR_INVALID;
4908c6fd2807SJeff Garzik 
4909c6fd2807SJeff Garzik 	/* set up init dev params taskfile */
4910c6fd2807SJeff Garzik 	DPRINTK("init dev params \n");
4911c6fd2807SJeff Garzik 
4912c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
4913c6fd2807SJeff Garzik 	tf.command = ATA_CMD_INIT_DEV_PARAMS;
4914c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4915c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
4916c6fd2807SJeff Garzik 	tf.nsect = sectors;
4917c6fd2807SJeff Garzik 	tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
4918c6fd2807SJeff Garzik 
49192b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
492018b2466cSAlan Cox 	/* A clean abort indicates an original or just out of spec drive
492118b2466cSAlan Cox 	   and we should continue as we issue the setup based on the
492218b2466cSAlan Cox 	   drive reported working geometry */
492318b2466cSAlan Cox 	if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
492418b2466cSAlan Cox 		err_mask = 0;
4925c6fd2807SJeff Garzik 
4926c6fd2807SJeff Garzik 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
4927c6fd2807SJeff Garzik 	return err_mask;
4928c6fd2807SJeff Garzik }
4929c6fd2807SJeff Garzik 
4930c6fd2807SJeff Garzik /**
49315895ef9aSTejun Heo  *	atapi_check_dma - Check whether ATAPI DMA can be supported
4932c6fd2807SJeff Garzik  *	@qc: Metadata associated with taskfile to check
4933c6fd2807SJeff Garzik  *
4934c6fd2807SJeff Garzik  *	Allow low-level driver to filter ATA PACKET commands, returning
4935c6fd2807SJeff Garzik  *	a status indicating whether or not it is OK to use DMA for the
4936c6fd2807SJeff Garzik  *	supplied PACKET command.
4937c6fd2807SJeff Garzik  *
4938c6fd2807SJeff Garzik  *	LOCKING:
4939cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4940c6fd2807SJeff Garzik  *
4941c6fd2807SJeff Garzik  *	RETURNS: 0 when ATAPI DMA can be used
4942c6fd2807SJeff Garzik  *               nonzero otherwise
4943c6fd2807SJeff Garzik  */
49445895ef9aSTejun Heo int atapi_check_dma(struct ata_queued_cmd *qc)
4945c6fd2807SJeff Garzik {
4946c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4947c6fd2807SJeff Garzik 
4948b9a4197eSTejun Heo 	/* Don't allow DMA if it isn't multiple of 16 bytes.  Quite a
4949b9a4197eSTejun Heo 	 * few ATAPI devices choke on such DMA requests.
4950b9a4197eSTejun Heo 	 */
49516a87e42eSTejun Heo 	if (!(qc->dev->horkage & ATA_HORKAGE_ATAPI_MOD16_DMA) &&
49526a87e42eSTejun Heo 	    unlikely(qc->nbytes & 15))
49536f23a31dSAlbert Lee 		return 1;
49546f23a31dSAlbert Lee 
4955c6fd2807SJeff Garzik 	if (ap->ops->check_atapi_dma)
4956b9a4197eSTejun Heo 		return ap->ops->check_atapi_dma(qc);
4957c6fd2807SJeff Garzik 
4958b9a4197eSTejun Heo 	return 0;
4959c6fd2807SJeff Garzik }
4960b9a4197eSTejun Heo 
4961c6fd2807SJeff Garzik /**
496231cc23b3STejun Heo  *	ata_std_qc_defer - Check whether a qc needs to be deferred
496331cc23b3STejun Heo  *	@qc: ATA command in question
496431cc23b3STejun Heo  *
496531cc23b3STejun Heo  *	Non-NCQ commands cannot run with any other command, NCQ or
496631cc23b3STejun Heo  *	not.  As upper layer only knows the queue depth, we are
496731cc23b3STejun Heo  *	responsible for maintaining exclusion.  This function checks
496831cc23b3STejun Heo  *	whether a new command @qc can be issued.
496931cc23b3STejun Heo  *
497031cc23b3STejun Heo  *	LOCKING:
497131cc23b3STejun Heo  *	spin_lock_irqsave(host lock)
497231cc23b3STejun Heo  *
497331cc23b3STejun Heo  *	RETURNS:
497431cc23b3STejun Heo  *	ATA_DEFER_* if deferring is needed, 0 otherwise.
497531cc23b3STejun Heo  */
497631cc23b3STejun Heo int ata_std_qc_defer(struct ata_queued_cmd *qc)
497731cc23b3STejun Heo {
497831cc23b3STejun Heo 	struct ata_link *link = qc->dev->link;
497931cc23b3STejun Heo 
4980179b310aSHannes Reinecke 	if (ata_is_ncq(qc->tf.protocol)) {
498131cc23b3STejun Heo 		if (!ata_tag_valid(link->active_tag))
498231cc23b3STejun Heo 			return 0;
498331cc23b3STejun Heo 	} else {
498431cc23b3STejun Heo 		if (!ata_tag_valid(link->active_tag) && !link->sactive)
498531cc23b3STejun Heo 			return 0;
498631cc23b3STejun Heo 	}
498731cc23b3STejun Heo 
498831cc23b3STejun Heo 	return ATA_DEFER_LINK;
498931cc23b3STejun Heo }
499031cc23b3STejun Heo 
4991c6fd2807SJeff Garzik void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
4992c6fd2807SJeff Garzik 
4993c6fd2807SJeff Garzik /**
4994c6fd2807SJeff Garzik  *	ata_sg_init - Associate command with scatter-gather table.
4995c6fd2807SJeff Garzik  *	@qc: Command to be associated
4996c6fd2807SJeff Garzik  *	@sg: Scatter-gather table.
4997c6fd2807SJeff Garzik  *	@n_elem: Number of elements in s/g table.
4998c6fd2807SJeff Garzik  *
4999c6fd2807SJeff Garzik  *	Initialize the data-related elements of queued_cmd @qc
5000c6fd2807SJeff Garzik  *	to point to a scatter-gather table @sg, containing @n_elem
5001c6fd2807SJeff Garzik  *	elements.
5002c6fd2807SJeff Garzik  *
5003c6fd2807SJeff Garzik  *	LOCKING:
5004cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
5005c6fd2807SJeff Garzik  */
5006c6fd2807SJeff Garzik void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
5007c6fd2807SJeff Garzik 		 unsigned int n_elem)
5008c6fd2807SJeff Garzik {
5009ff2aeb1eSTejun Heo 	qc->sg = sg;
5010c6fd2807SJeff Garzik 	qc->n_elem = n_elem;
5011ff2aeb1eSTejun Heo 	qc->cursg = qc->sg;
5012ff2aeb1eSTejun Heo }
5013ff2aeb1eSTejun Heo 
50142874d5eeSGeert Uytterhoeven #ifdef CONFIG_HAS_DMA
50152874d5eeSGeert Uytterhoeven 
50162874d5eeSGeert Uytterhoeven /**
50172874d5eeSGeert Uytterhoeven  *	ata_sg_clean - Unmap DMA memory associated with command
50182874d5eeSGeert Uytterhoeven  *	@qc: Command containing DMA memory to be released
50192874d5eeSGeert Uytterhoeven  *
50202874d5eeSGeert Uytterhoeven  *	Unmap all mapped DMA memory associated with this command.
50212874d5eeSGeert Uytterhoeven  *
50222874d5eeSGeert Uytterhoeven  *	LOCKING:
50232874d5eeSGeert Uytterhoeven  *	spin_lock_irqsave(host lock)
50242874d5eeSGeert Uytterhoeven  */
5025af27e01cSJason Yan static void ata_sg_clean(struct ata_queued_cmd *qc)
50262874d5eeSGeert Uytterhoeven {
50272874d5eeSGeert Uytterhoeven 	struct ata_port *ap = qc->ap;
50282874d5eeSGeert Uytterhoeven 	struct scatterlist *sg = qc->sg;
50292874d5eeSGeert Uytterhoeven 	int dir = qc->dma_dir;
50302874d5eeSGeert Uytterhoeven 
50312874d5eeSGeert Uytterhoeven 	WARN_ON_ONCE(sg == NULL);
50322874d5eeSGeert Uytterhoeven 
50332874d5eeSGeert Uytterhoeven 	VPRINTK("unmapping %u sg elements\n", qc->n_elem);
50342874d5eeSGeert Uytterhoeven 
50352874d5eeSGeert Uytterhoeven 	if (qc->n_elem)
50362874d5eeSGeert Uytterhoeven 		dma_unmap_sg(ap->dev, sg, qc->orig_n_elem, dir);
50372874d5eeSGeert Uytterhoeven 
50382874d5eeSGeert Uytterhoeven 	qc->flags &= ~ATA_QCFLAG_DMAMAP;
50392874d5eeSGeert Uytterhoeven 	qc->sg = NULL;
50402874d5eeSGeert Uytterhoeven }
50412874d5eeSGeert Uytterhoeven 
5042c6fd2807SJeff Garzik /**
5043c6fd2807SJeff Garzik  *	ata_sg_setup - DMA-map the scatter-gather table associated with a command.
5044c6fd2807SJeff Garzik  *	@qc: Command with scatter-gather table to be mapped.
5045c6fd2807SJeff Garzik  *
5046c6fd2807SJeff Garzik  *	DMA-map the scatter-gather table associated with queued_cmd @qc.
5047c6fd2807SJeff Garzik  *
5048c6fd2807SJeff Garzik  *	LOCKING:
5049cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
5050c6fd2807SJeff Garzik  *
5051c6fd2807SJeff Garzik  *	RETURNS:
5052c6fd2807SJeff Garzik  *	Zero on success, negative on error.
5053c6fd2807SJeff Garzik  *
5054c6fd2807SJeff Garzik  */
5055c6fd2807SJeff Garzik static int ata_sg_setup(struct ata_queued_cmd *qc)
5056c6fd2807SJeff Garzik {
5057c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
5058dde20207SJames Bottomley 	unsigned int n_elem;
5059c6fd2807SJeff Garzik 
506044877b4eSTejun Heo 	VPRINTK("ENTER, ata%u\n", ap->print_id);
5061c6fd2807SJeff Garzik 
5062dde20207SJames Bottomley 	n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir);
5063dde20207SJames Bottomley 	if (n_elem < 1)
5064c6fd2807SJeff Garzik 		return -1;
5065c6fd2807SJeff Garzik 
5066dde20207SJames Bottomley 	DPRINTK("%d sg elements mapped\n", n_elem);
50675825627cSFUJITA Tomonori 	qc->orig_n_elem = qc->n_elem;
5068dde20207SJames Bottomley 	qc->n_elem = n_elem;
5069f92a2636STejun Heo 	qc->flags |= ATA_QCFLAG_DMAMAP;
5070c6fd2807SJeff Garzik 
5071c6fd2807SJeff Garzik 	return 0;
5072c6fd2807SJeff Garzik }
5073c6fd2807SJeff Garzik 
50742874d5eeSGeert Uytterhoeven #else /* !CONFIG_HAS_DMA */
50752874d5eeSGeert Uytterhoeven 
50762874d5eeSGeert Uytterhoeven static inline void ata_sg_clean(struct ata_queued_cmd *qc) {}
50772874d5eeSGeert Uytterhoeven static inline int ata_sg_setup(struct ata_queued_cmd *qc) { return -1; }
50782874d5eeSGeert Uytterhoeven 
50792874d5eeSGeert Uytterhoeven #endif /* !CONFIG_HAS_DMA */
50802874d5eeSGeert Uytterhoeven 
5081c6fd2807SJeff Garzik /**
5082c6fd2807SJeff Garzik  *	swap_buf_le16 - swap halves of 16-bit words in place
5083c6fd2807SJeff Garzik  *	@buf:  Buffer to swap
5084c6fd2807SJeff Garzik  *	@buf_words:  Number of 16-bit words in buffer.
5085c6fd2807SJeff Garzik  *
5086c6fd2807SJeff Garzik  *	Swap halves of 16-bit words if needed to convert from
5087c6fd2807SJeff Garzik  *	little-endian byte order to native cpu byte order, or
5088c6fd2807SJeff Garzik  *	vice-versa.
5089c6fd2807SJeff Garzik  *
5090c6fd2807SJeff Garzik  *	LOCKING:
5091c6fd2807SJeff Garzik  *	Inherited from caller.
5092c6fd2807SJeff Garzik  */
5093c6fd2807SJeff Garzik void swap_buf_le16(u16 *buf, unsigned int buf_words)
5094c6fd2807SJeff Garzik {
5095c6fd2807SJeff Garzik #ifdef __BIG_ENDIAN
5096c6fd2807SJeff Garzik 	unsigned int i;
5097c6fd2807SJeff Garzik 
5098c6fd2807SJeff Garzik 	for (i = 0; i < buf_words; i++)
5099c6fd2807SJeff Garzik 		buf[i] = le16_to_cpu(buf[i]);
5100c6fd2807SJeff Garzik #endif /* __BIG_ENDIAN */
5101c6fd2807SJeff Garzik }
5102c6fd2807SJeff Garzik 
5103c6fd2807SJeff Garzik /**
5104c6fd2807SJeff Garzik  *	ata_qc_new_init - Request an available ATA command, and initialize it
5105c6fd2807SJeff Garzik  *	@dev: Device from whom we request an available command structure
510638755e89SRandy Dunlap  *	@tag: tag
5107c6fd2807SJeff Garzik  *
5108c6fd2807SJeff Garzik  *	LOCKING:
5109c6fd2807SJeff Garzik  *	None.
5110c6fd2807SJeff Garzik  */
5111c6fd2807SJeff Garzik 
511298bd4be1SShaohua Li struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev, int tag)
5113c6fd2807SJeff Garzik {
51149af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
5115c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
5116c6fd2807SJeff Garzik 
511798bd4be1SShaohua Li 	/* no command while frozen */
511898bd4be1SShaohua Li 	if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
511998bd4be1SShaohua Li 		return NULL;
512098bd4be1SShaohua Li 
512198bd4be1SShaohua Li 	/* libsas case */
51225067c046SShaohua Li 	if (ap->flags & ATA_FLAG_SAS_HOST) {
512398bd4be1SShaohua Li 		tag = ata_sas_allocate_tag(ap);
512498bd4be1SShaohua Li 		if (tag < 0)
512598bd4be1SShaohua Li 			return NULL;
512698bd4be1SShaohua Li 	}
512798bd4be1SShaohua Li 
512898bd4be1SShaohua Li 	qc = __ata_qc_from_tag(ap, tag);
51295ac40790SJens Axboe 	qc->tag = qc->hw_tag = tag;
5130c6fd2807SJeff Garzik 	qc->scsicmd = NULL;
5131c6fd2807SJeff Garzik 	qc->ap = ap;
5132c6fd2807SJeff Garzik 	qc->dev = dev;
5133c6fd2807SJeff Garzik 
5134c6fd2807SJeff Garzik 	ata_qc_reinit(qc);
5135c6fd2807SJeff Garzik 
5136c6fd2807SJeff Garzik 	return qc;
5137c6fd2807SJeff Garzik }
5138c6fd2807SJeff Garzik 
51398a8bc223STejun Heo /**
51408a8bc223STejun Heo  *	ata_qc_free - free unused ata_queued_cmd
51418a8bc223STejun Heo  *	@qc: Command to complete
51428a8bc223STejun Heo  *
51438a8bc223STejun Heo  *	Designed to free unused ata_queued_cmd object
51448a8bc223STejun Heo  *	in case something prevents using it.
51458a8bc223STejun Heo  *
51468a8bc223STejun Heo  *	LOCKING:
51478a8bc223STejun Heo  *	spin_lock_irqsave(host lock)
51488a8bc223STejun Heo  */
51498a8bc223STejun Heo void ata_qc_free(struct ata_queued_cmd *qc)
51508a8bc223STejun Heo {
5151a1104016SJulia Lawall 	struct ata_port *ap;
51528a8bc223STejun Heo 	unsigned int tag;
51538a8bc223STejun Heo 
5154efcb3cf7STejun Heo 	WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
5155a1104016SJulia Lawall 	ap = qc->ap;
51568a8bc223STejun Heo 
51578a8bc223STejun Heo 	qc->flags = 0;
51588a8bc223STejun Heo 	tag = qc->tag;
515928361c40SJens Axboe 	if (ata_tag_valid(tag)) {
51608a8bc223STejun Heo 		qc->tag = ATA_TAG_POISON;
51615067c046SShaohua Li 		if (ap->flags & ATA_FLAG_SAS_HOST)
516298bd4be1SShaohua Li 			ata_sas_free_tag(tag, ap);
51638a8bc223STejun Heo 	}
51648a8bc223STejun Heo }
51658a8bc223STejun Heo 
5166c6fd2807SJeff Garzik void __ata_qc_complete(struct ata_queued_cmd *qc)
5167c6fd2807SJeff Garzik {
5168a1104016SJulia Lawall 	struct ata_port *ap;
5169a1104016SJulia Lawall 	struct ata_link *link;
5170c6fd2807SJeff Garzik 
5171efcb3cf7STejun Heo 	WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
5172efcb3cf7STejun Heo 	WARN_ON_ONCE(!(qc->flags & ATA_QCFLAG_ACTIVE));
5173a1104016SJulia Lawall 	ap = qc->ap;
5174a1104016SJulia Lawall 	link = qc->dev->link;
5175c6fd2807SJeff Garzik 
5176c6fd2807SJeff Garzik 	if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
5177c6fd2807SJeff Garzik 		ata_sg_clean(qc);
5178c6fd2807SJeff Garzik 
5179c6fd2807SJeff Garzik 	/* command should be marked inactive atomically with qc completion */
5180179b310aSHannes Reinecke 	if (ata_is_ncq(qc->tf.protocol)) {
51814e5b6260SJens Axboe 		link->sactive &= ~(1 << qc->hw_tag);
5182da917d69STejun Heo 		if (!link->sactive)
5183da917d69STejun Heo 			ap->nr_active_links--;
5184da917d69STejun Heo 	} else {
51859af5c9c9STejun Heo 		link->active_tag = ATA_TAG_POISON;
5186da917d69STejun Heo 		ap->nr_active_links--;
5187da917d69STejun Heo 	}
5188da917d69STejun Heo 
5189da917d69STejun Heo 	/* clear exclusive status */
5190da917d69STejun Heo 	if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
5191da917d69STejun Heo 		     ap->excl_link == link))
5192da917d69STejun Heo 		ap->excl_link = NULL;
5193c6fd2807SJeff Garzik 
5194c6fd2807SJeff Garzik 	/* atapi: mark qc as inactive to prevent the interrupt handler
5195c6fd2807SJeff Garzik 	 * from completing the command twice later, before the error handler
5196c6fd2807SJeff Garzik 	 * is called. (when rc != 0 and atapi request sense is needed)
5197c6fd2807SJeff Garzik 	 */
5198c6fd2807SJeff Garzik 	qc->flags &= ~ATA_QCFLAG_ACTIVE;
5199e3ed8939SJens Axboe 	ap->qc_active &= ~(1ULL << qc->tag);
5200c6fd2807SJeff Garzik 
5201c6fd2807SJeff Garzik 	/* call completion callback */
5202c6fd2807SJeff Garzik 	qc->complete_fn(qc);
5203c6fd2807SJeff Garzik }
5204c6fd2807SJeff Garzik 
520539599a53STejun Heo static void fill_result_tf(struct ata_queued_cmd *qc)
520639599a53STejun Heo {
520739599a53STejun Heo 	struct ata_port *ap = qc->ap;
520839599a53STejun Heo 
520939599a53STejun Heo 	qc->result_tf.flags = qc->tf.flags;
521022183bf5STejun Heo 	ap->ops->qc_fill_rtf(qc);
521139599a53STejun Heo }
521239599a53STejun Heo 
521300115e0fSTejun Heo static void ata_verify_xfer(struct ata_queued_cmd *qc)
521400115e0fSTejun Heo {
521500115e0fSTejun Heo 	struct ata_device *dev = qc->dev;
521600115e0fSTejun Heo 
5217eb0effdfSChristoph Hellwig 	if (!ata_is_data(qc->tf.protocol))
521800115e0fSTejun Heo 		return;
521900115e0fSTejun Heo 
522000115e0fSTejun Heo 	if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
522100115e0fSTejun Heo 		return;
522200115e0fSTejun Heo 
522300115e0fSTejun Heo 	dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
522400115e0fSTejun Heo }
522500115e0fSTejun Heo 
5226c6fd2807SJeff Garzik /**
5227c6fd2807SJeff Garzik  *	ata_qc_complete - Complete an active ATA command
5228c6fd2807SJeff Garzik  *	@qc: Command to complete
5229c6fd2807SJeff Garzik  *
52301aadf5c3STejun Heo  *	Indicate to the mid and upper layers that an ATA command has
52311aadf5c3STejun Heo  *	completed, with either an ok or not-ok status.
52321aadf5c3STejun Heo  *
52331aadf5c3STejun Heo  *	Refrain from calling this function multiple times when
52341aadf5c3STejun Heo  *	successfully completing multiple NCQ commands.
52351aadf5c3STejun Heo  *	ata_qc_complete_multiple() should be used instead, which will
52361aadf5c3STejun Heo  *	properly update IRQ expect state.
5237c6fd2807SJeff Garzik  *
5238c6fd2807SJeff Garzik  *	LOCKING:
5239cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
5240c6fd2807SJeff Garzik  */
5241c6fd2807SJeff Garzik void ata_qc_complete(struct ata_queued_cmd *qc)
5242c6fd2807SJeff Garzik {
5243c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
5244c6fd2807SJeff Garzik 
5245eb25cb99SStephan Linz 	/* Trigger the LED (if available) */
5246d1ed7c55SLinus Walleij 	ledtrig_disk_activity(!!(qc->tf.flags & ATA_TFLAG_WRITE));
5247eb25cb99SStephan Linz 
5248c6fd2807SJeff Garzik 	/* XXX: New EH and old EH use different mechanisms to
5249c6fd2807SJeff Garzik 	 * synchronize EH with regular execution path.
5250c6fd2807SJeff Garzik 	 *
5251c6fd2807SJeff Garzik 	 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
5252c6fd2807SJeff Garzik 	 * Normal execution path is responsible for not accessing a
5253c6fd2807SJeff Garzik 	 * failed qc.  libata core enforces the rule by returning NULL
5254c6fd2807SJeff Garzik 	 * from ata_qc_from_tag() for failed qcs.
5255c6fd2807SJeff Garzik 	 *
5256c6fd2807SJeff Garzik 	 * Old EH depends on ata_qc_complete() nullifying completion
5257c6fd2807SJeff Garzik 	 * requests if ATA_QCFLAG_EH_SCHEDULED is set.  Old EH does
5258c6fd2807SJeff Garzik 	 * not synchronize with interrupt handler.  Only PIO task is
5259c6fd2807SJeff Garzik 	 * taken care of.
5260c6fd2807SJeff Garzik 	 */
5261c6fd2807SJeff Garzik 	if (ap->ops->error_handler) {
52624dbfa39bSTejun Heo 		struct ata_device *dev = qc->dev;
52634dbfa39bSTejun Heo 		struct ata_eh_info *ehi = &dev->link->eh_info;
52644dbfa39bSTejun Heo 
5265c6fd2807SJeff Garzik 		if (unlikely(qc->err_mask))
5266c6fd2807SJeff Garzik 			qc->flags |= ATA_QCFLAG_FAILED;
5267c6fd2807SJeff Garzik 
5268f08dc1acSTejun Heo 		/*
5269f08dc1acSTejun Heo 		 * Finish internal commands without any further processing
5270f08dc1acSTejun Heo 		 * and always with the result TF filled.
5271f08dc1acSTejun Heo 		 */
5272f08dc1acSTejun Heo 		if (unlikely(ata_tag_internal(qc->tag))) {
527339599a53STejun Heo 			fill_result_tf(qc);
5274255c03d1SHannes Reinecke 			trace_ata_qc_complete_internal(qc);
5275f4b31db9STejun Heo 			__ata_qc_complete(qc);
5276c6fd2807SJeff Garzik 			return;
5277c6fd2807SJeff Garzik 		}
5278c6fd2807SJeff Garzik 
5279f08dc1acSTejun Heo 		/*
5280f08dc1acSTejun Heo 		 * Non-internal qc has failed.  Fill the result TF and
5281f08dc1acSTejun Heo 		 * summon EH.
5282f08dc1acSTejun Heo 		 */
5283f08dc1acSTejun Heo 		if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
5284f08dc1acSTejun Heo 			fill_result_tf(qc);
5285255c03d1SHannes Reinecke 			trace_ata_qc_complete_failed(qc);
5286f08dc1acSTejun Heo 			ata_qc_schedule_eh(qc);
5287f08dc1acSTejun Heo 			return;
5288f08dc1acSTejun Heo 		}
5289f08dc1acSTejun Heo 
52904dc738edSTejun Heo 		WARN_ON_ONCE(ap->pflags & ATA_PFLAG_FROZEN);
52914dc738edSTejun Heo 
5292c6fd2807SJeff Garzik 		/* read result TF if requested */
5293c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_RESULT_TF)
529439599a53STejun Heo 			fill_result_tf(qc);
5295c6fd2807SJeff Garzik 
5296255c03d1SHannes Reinecke 		trace_ata_qc_complete_done(qc);
52974dbfa39bSTejun Heo 		/* Some commands need post-processing after successful
52984dbfa39bSTejun Heo 		 * completion.
52994dbfa39bSTejun Heo 		 */
53004dbfa39bSTejun Heo 		switch (qc->tf.command) {
53014dbfa39bSTejun Heo 		case ATA_CMD_SET_FEATURES:
53024dbfa39bSTejun Heo 			if (qc->tf.feature != SETFEATURES_WC_ON &&
53030c12735eSTom Yan 			    qc->tf.feature != SETFEATURES_WC_OFF &&
53040c12735eSTom Yan 			    qc->tf.feature != SETFEATURES_RA_ON &&
53050c12735eSTom Yan 			    qc->tf.feature != SETFEATURES_RA_OFF)
53064dbfa39bSTejun Heo 				break;
53074dbfa39bSTejun Heo 			/* fall through */
53084dbfa39bSTejun Heo 		case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
53094dbfa39bSTejun Heo 		case ATA_CMD_SET_MULTI: /* multi_count changed */
53104dbfa39bSTejun Heo 			/* revalidate device */
53114dbfa39bSTejun Heo 			ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE;
53124dbfa39bSTejun Heo 			ata_port_schedule_eh(ap);
53134dbfa39bSTejun Heo 			break;
5314054a5fbaSTejun Heo 
5315054a5fbaSTejun Heo 		case ATA_CMD_SLEEP:
5316054a5fbaSTejun Heo 			dev->flags |= ATA_DFLAG_SLEEPING;
5317054a5fbaSTejun Heo 			break;
53184dbfa39bSTejun Heo 		}
53194dbfa39bSTejun Heo 
532000115e0fSTejun Heo 		if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
532100115e0fSTejun Heo 			ata_verify_xfer(qc);
532200115e0fSTejun Heo 
5323c6fd2807SJeff Garzik 		__ata_qc_complete(qc);
5324c6fd2807SJeff Garzik 	} else {
5325c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
5326c6fd2807SJeff Garzik 			return;
5327c6fd2807SJeff Garzik 
5328c6fd2807SJeff Garzik 		/* read result TF if failed or requested */
5329c6fd2807SJeff Garzik 		if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
533039599a53STejun Heo 			fill_result_tf(qc);
5331c6fd2807SJeff Garzik 
5332c6fd2807SJeff Garzik 		__ata_qc_complete(qc);
5333c6fd2807SJeff Garzik 	}
5334c6fd2807SJeff Garzik }
5335c6fd2807SJeff Garzik 
5336c6fd2807SJeff Garzik /**
5337c6fd2807SJeff Garzik  *	ata_qc_complete_multiple - Complete multiple qcs successfully
5338c6fd2807SJeff Garzik  *	@ap: port in question
5339c6fd2807SJeff Garzik  *	@qc_active: new qc_active mask
5340c6fd2807SJeff Garzik  *
5341c6fd2807SJeff Garzik  *	Complete in-flight commands.  This functions is meant to be
5342c6fd2807SJeff Garzik  *	called from low-level driver's interrupt routine to complete
5343c6fd2807SJeff Garzik  *	requests normally.  ap->qc_active and @qc_active is compared
5344c6fd2807SJeff Garzik  *	and commands are completed accordingly.
5345c6fd2807SJeff Garzik  *
53461aadf5c3STejun Heo  *	Always use this function when completing multiple NCQ commands
53471aadf5c3STejun Heo  *	from IRQ handlers instead of calling ata_qc_complete()
53481aadf5c3STejun Heo  *	multiple times to keep IRQ expect status properly in sync.
53491aadf5c3STejun Heo  *
5350c6fd2807SJeff Garzik  *	LOCKING:
5351cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
5352c6fd2807SJeff Garzik  *
5353c6fd2807SJeff Garzik  *	RETURNS:
5354c6fd2807SJeff Garzik  *	Number of completed commands on success, -errno otherwise.
5355c6fd2807SJeff Garzik  */
5356e3ed8939SJens Axboe int ata_qc_complete_multiple(struct ata_port *ap, u64 qc_active)
5357c6fd2807SJeff Garzik {
5358c6fd2807SJeff Garzik 	int nr_done = 0;
5359e3ed8939SJens Axboe 	u64 done_mask;
5360c6fd2807SJeff Garzik 
5361c6fd2807SJeff Garzik 	done_mask = ap->qc_active ^ qc_active;
5362c6fd2807SJeff Garzik 
5363c6fd2807SJeff Garzik 	if (unlikely(done_mask & qc_active)) {
5364e3ed8939SJens Axboe 		ata_port_err(ap, "illegal qc_active transition (%08llx->%08llx)\n",
5365a9a79dfeSJoe Perches 			     ap->qc_active, qc_active);
5366c6fd2807SJeff Garzik 		return -EINVAL;
5367c6fd2807SJeff Garzik 	}
5368c6fd2807SJeff Garzik 
536943768180SJens Axboe 	while (done_mask) {
5370c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc;
5371e3ed8939SJens Axboe 		unsigned int tag = __ffs64(done_mask);
5372c6fd2807SJeff Garzik 
537343768180SJens Axboe 		qc = ata_qc_from_tag(ap, tag);
537443768180SJens Axboe 		if (qc) {
5375c6fd2807SJeff Garzik 			ata_qc_complete(qc);
5376c6fd2807SJeff Garzik 			nr_done++;
5377c6fd2807SJeff Garzik 		}
5378e3ed8939SJens Axboe 		done_mask &= ~(1ULL << tag);
5379c6fd2807SJeff Garzik 	}
5380c6fd2807SJeff Garzik 
5381c6fd2807SJeff Garzik 	return nr_done;
5382c6fd2807SJeff Garzik }
5383c6fd2807SJeff Garzik 
5384c6fd2807SJeff Garzik /**
5385c6fd2807SJeff Garzik  *	ata_qc_issue - issue taskfile to device
5386c6fd2807SJeff Garzik  *	@qc: command to issue to device
5387c6fd2807SJeff Garzik  *
5388c6fd2807SJeff Garzik  *	Prepare an ATA command to submission to device.
5389c6fd2807SJeff Garzik  *	This includes mapping the data into a DMA-able
5390c6fd2807SJeff Garzik  *	area, filling in the S/G table, and finally
5391c6fd2807SJeff Garzik  *	writing the taskfile to hardware, starting the command.
5392c6fd2807SJeff Garzik  *
5393c6fd2807SJeff Garzik  *	LOCKING:
5394cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
5395c6fd2807SJeff Garzik  */
5396c6fd2807SJeff Garzik void ata_qc_issue(struct ata_queued_cmd *qc)
5397c6fd2807SJeff Garzik {
5398c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
53999af5c9c9STejun Heo 	struct ata_link *link = qc->dev->link;
5400405e66b3STejun Heo 	u8 prot = qc->tf.protocol;
5401c6fd2807SJeff Garzik 
5402c6fd2807SJeff Garzik 	/* Make sure only one non-NCQ command is outstanding.  The
5403c6fd2807SJeff Garzik 	 * check is skipped for old EH because it reuses active qc to
5404c6fd2807SJeff Garzik 	 * request ATAPI sense.
5405c6fd2807SJeff Garzik 	 */
5406efcb3cf7STejun Heo 	WARN_ON_ONCE(ap->ops->error_handler && ata_tag_valid(link->active_tag));
5407c6fd2807SJeff Garzik 
54081973a023STejun Heo 	if (ata_is_ncq(prot)) {
54094e5b6260SJens Axboe 		WARN_ON_ONCE(link->sactive & (1 << qc->hw_tag));
5410da917d69STejun Heo 
5411da917d69STejun Heo 		if (!link->sactive)
5412da917d69STejun Heo 			ap->nr_active_links++;
54134e5b6260SJens Axboe 		link->sactive |= 1 << qc->hw_tag;
5414c6fd2807SJeff Garzik 	} else {
5415efcb3cf7STejun Heo 		WARN_ON_ONCE(link->sactive);
5416da917d69STejun Heo 
5417da917d69STejun Heo 		ap->nr_active_links++;
54189af5c9c9STejun Heo 		link->active_tag = qc->tag;
5419c6fd2807SJeff Garzik 	}
5420c6fd2807SJeff Garzik 
5421c6fd2807SJeff Garzik 	qc->flags |= ATA_QCFLAG_ACTIVE;
5422e3ed8939SJens Axboe 	ap->qc_active |= 1ULL << qc->tag;
5423c6fd2807SJeff Garzik 
542460f5d6efSTejun Heo 	/*
542560f5d6efSTejun Heo 	 * We guarantee to LLDs that they will have at least one
5426f92a2636STejun Heo 	 * non-zero sg if the command is a data command.
5427f92a2636STejun Heo 	 */
54289173e5e8SEric Biggers 	if (ata_is_data(prot) && (!qc->sg || !qc->n_elem || !qc->nbytes))
542960f5d6efSTejun Heo 		goto sys_err;
5430f92a2636STejun Heo 
5431405e66b3STejun Heo 	if (ata_is_dma(prot) || (ata_is_pio(prot) &&
5432f92a2636STejun Heo 				 (ap->flags & ATA_FLAG_PIO_DMA)))
5433c6fd2807SJeff Garzik 		if (ata_sg_setup(qc))
543460f5d6efSTejun Heo 			goto sys_err;
5435c6fd2807SJeff Garzik 
5436cf480626STejun Heo 	/* if device is sleeping, schedule reset and abort the link */
5437054a5fbaSTejun Heo 	if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
5438cf480626STejun Heo 		link->eh_info.action |= ATA_EH_RESET;
5439054a5fbaSTejun Heo 		ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
5440054a5fbaSTejun Heo 		ata_link_abort(link);
5441054a5fbaSTejun Heo 		return;
5442054a5fbaSTejun Heo 	}
5443054a5fbaSTejun Heo 
5444c6fd2807SJeff Garzik 	ap->ops->qc_prep(qc);
5445255c03d1SHannes Reinecke 	trace_ata_qc_issue(qc);
5446c6fd2807SJeff Garzik 	qc->err_mask |= ap->ops->qc_issue(qc);
5447c6fd2807SJeff Garzik 	if (unlikely(qc->err_mask))
5448c6fd2807SJeff Garzik 		goto err;
5449c6fd2807SJeff Garzik 	return;
5450c6fd2807SJeff Garzik 
545160f5d6efSTejun Heo sys_err:
5452c6fd2807SJeff Garzik 	qc->err_mask |= AC_ERR_SYSTEM;
5453c6fd2807SJeff Garzik err:
5454c6fd2807SJeff Garzik 	ata_qc_complete(qc);
5455c6fd2807SJeff Garzik }
5456c6fd2807SJeff Garzik 
5457c6fd2807SJeff Garzik /**
5458c6fd2807SJeff Garzik  *	sata_scr_valid - test whether SCRs are accessible
5459936fd732STejun Heo  *	@link: ATA link to test SCR accessibility for
5460c6fd2807SJeff Garzik  *
5461936fd732STejun Heo  *	Test whether SCRs are accessible for @link.
5462c6fd2807SJeff Garzik  *
5463c6fd2807SJeff Garzik  *	LOCKING:
5464c6fd2807SJeff Garzik  *	None.
5465c6fd2807SJeff Garzik  *
5466c6fd2807SJeff Garzik  *	RETURNS:
5467c6fd2807SJeff Garzik  *	1 if SCRs are accessible, 0 otherwise.
5468c6fd2807SJeff Garzik  */
5469936fd732STejun Heo int sata_scr_valid(struct ata_link *link)
5470c6fd2807SJeff Garzik {
5471936fd732STejun Heo 	struct ata_port *ap = link->ap;
5472936fd732STejun Heo 
5473a16abc0bSTejun Heo 	return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
5474c6fd2807SJeff Garzik }
5475c6fd2807SJeff Garzik 
5476c6fd2807SJeff Garzik /**
5477c6fd2807SJeff Garzik  *	sata_scr_read - read SCR register of the specified port
5478936fd732STejun Heo  *	@link: ATA link to read SCR for
5479c6fd2807SJeff Garzik  *	@reg: SCR to read
5480c6fd2807SJeff Garzik  *	@val: Place to store read value
5481c6fd2807SJeff Garzik  *
5482936fd732STejun Heo  *	Read SCR register @reg of @link into *@val.  This function is
5483633273a3STejun Heo  *	guaranteed to succeed if @link is ap->link, the cable type of
5484633273a3STejun Heo  *	the port is SATA and the port implements ->scr_read.
5485c6fd2807SJeff Garzik  *
5486c6fd2807SJeff Garzik  *	LOCKING:
5487633273a3STejun Heo  *	None if @link is ap->link.  Kernel thread context otherwise.
5488c6fd2807SJeff Garzik  *
5489c6fd2807SJeff Garzik  *	RETURNS:
5490c6fd2807SJeff Garzik  *	0 on success, negative errno on failure.
5491c6fd2807SJeff Garzik  */
5492936fd732STejun Heo int sata_scr_read(struct ata_link *link, int reg, u32 *val)
5493c6fd2807SJeff Garzik {
5494633273a3STejun Heo 	if (ata_is_host_link(link)) {
5495936fd732STejun Heo 		if (sata_scr_valid(link))
549682ef04fbSTejun Heo 			return link->ap->ops->scr_read(link, reg, val);
5497c6fd2807SJeff Garzik 		return -EOPNOTSUPP;
5498c6fd2807SJeff Garzik 	}
5499c6fd2807SJeff Garzik 
5500633273a3STejun Heo 	return sata_pmp_scr_read(link, reg, val);
5501633273a3STejun Heo }
5502633273a3STejun Heo 
5503c6fd2807SJeff Garzik /**
5504c6fd2807SJeff Garzik  *	sata_scr_write - write SCR register of the specified port
5505936fd732STejun Heo  *	@link: ATA link to write SCR for
5506c6fd2807SJeff Garzik  *	@reg: SCR to write
5507c6fd2807SJeff Garzik  *	@val: value to write
5508c6fd2807SJeff Garzik  *
5509936fd732STejun Heo  *	Write @val to SCR register @reg of @link.  This function is
5510633273a3STejun Heo  *	guaranteed to succeed if @link is ap->link, the cable type of
5511633273a3STejun Heo  *	the port is SATA and the port implements ->scr_read.
5512c6fd2807SJeff Garzik  *
5513c6fd2807SJeff Garzik  *	LOCKING:
5514633273a3STejun Heo  *	None if @link is ap->link.  Kernel thread context otherwise.
5515c6fd2807SJeff Garzik  *
5516c6fd2807SJeff Garzik  *	RETURNS:
5517c6fd2807SJeff Garzik  *	0 on success, negative errno on failure.
5518c6fd2807SJeff Garzik  */
5519936fd732STejun Heo int sata_scr_write(struct ata_link *link, int reg, u32 val)
5520c6fd2807SJeff Garzik {
5521633273a3STejun Heo 	if (ata_is_host_link(link)) {
5522936fd732STejun Heo 		if (sata_scr_valid(link))
552382ef04fbSTejun Heo 			return link->ap->ops->scr_write(link, reg, val);
5524c6fd2807SJeff Garzik 		return -EOPNOTSUPP;
5525c6fd2807SJeff Garzik 	}
5526c6fd2807SJeff Garzik 
5527633273a3STejun Heo 	return sata_pmp_scr_write(link, reg, val);
5528633273a3STejun Heo }
5529633273a3STejun Heo 
5530c6fd2807SJeff Garzik /**
5531c6fd2807SJeff Garzik  *	sata_scr_write_flush - write SCR register of the specified port and flush
5532936fd732STejun Heo  *	@link: ATA link to write SCR for
5533c6fd2807SJeff Garzik  *	@reg: SCR to write
5534c6fd2807SJeff Garzik  *	@val: value to write
5535c6fd2807SJeff Garzik  *
5536c6fd2807SJeff Garzik  *	This function is identical to sata_scr_write() except that this
5537c6fd2807SJeff Garzik  *	function performs flush after writing to the register.
5538c6fd2807SJeff Garzik  *
5539c6fd2807SJeff Garzik  *	LOCKING:
5540633273a3STejun Heo  *	None if @link is ap->link.  Kernel thread context otherwise.
5541c6fd2807SJeff Garzik  *
5542c6fd2807SJeff Garzik  *	RETURNS:
5543c6fd2807SJeff Garzik  *	0 on success, negative errno on failure.
5544c6fd2807SJeff Garzik  */
5545936fd732STejun Heo int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
5546c6fd2807SJeff Garzik {
5547633273a3STejun Heo 	if (ata_is_host_link(link)) {
5548da3dbb17STejun Heo 		int rc;
5549da3dbb17STejun Heo 
5550936fd732STejun Heo 		if (sata_scr_valid(link)) {
555182ef04fbSTejun Heo 			rc = link->ap->ops->scr_write(link, reg, val);
5552da3dbb17STejun Heo 			if (rc == 0)
555382ef04fbSTejun Heo 				rc = link->ap->ops->scr_read(link, reg, &val);
5554da3dbb17STejun Heo 			return rc;
5555c6fd2807SJeff Garzik 		}
5556c6fd2807SJeff Garzik 		return -EOPNOTSUPP;
5557c6fd2807SJeff Garzik 	}
5558c6fd2807SJeff Garzik 
5559633273a3STejun Heo 	return sata_pmp_scr_write(link, reg, val);
5560633273a3STejun Heo }
5561633273a3STejun Heo 
5562c6fd2807SJeff Garzik /**
5563b1c72916STejun Heo  *	ata_phys_link_online - test whether the given link is online
5564936fd732STejun Heo  *	@link: ATA link to test
5565c6fd2807SJeff Garzik  *
5566936fd732STejun Heo  *	Test whether @link is online.  Note that this function returns
5567936fd732STejun Heo  *	0 if online status of @link cannot be obtained, so
5568936fd732STejun Heo  *	ata_link_online(link) != !ata_link_offline(link).
5569c6fd2807SJeff Garzik  *
5570c6fd2807SJeff Garzik  *	LOCKING:
5571c6fd2807SJeff Garzik  *	None.
5572c6fd2807SJeff Garzik  *
5573c6fd2807SJeff Garzik  *	RETURNS:
5574b5b3fa38STejun Heo  *	True if the port online status is available and online.
5575c6fd2807SJeff Garzik  */
5576b1c72916STejun Heo bool ata_phys_link_online(struct ata_link *link)
5577c6fd2807SJeff Garzik {
5578c6fd2807SJeff Garzik 	u32 sstatus;
5579c6fd2807SJeff Garzik 
5580936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
55819913ff8aSTejun Heo 	    ata_sstatus_online(sstatus))
5582b5b3fa38STejun Heo 		return true;
5583b5b3fa38STejun Heo 	return false;
5584c6fd2807SJeff Garzik }
5585c6fd2807SJeff Garzik 
5586c6fd2807SJeff Garzik /**
5587b1c72916STejun Heo  *	ata_phys_link_offline - test whether the given link is offline
5588936fd732STejun Heo  *	@link: ATA link to test
5589c6fd2807SJeff Garzik  *
5590936fd732STejun Heo  *	Test whether @link is offline.  Note that this function
5591936fd732STejun Heo  *	returns 0 if offline status of @link cannot be obtained, so
5592936fd732STejun Heo  *	ata_link_online(link) != !ata_link_offline(link).
5593c6fd2807SJeff Garzik  *
5594c6fd2807SJeff Garzik  *	LOCKING:
5595c6fd2807SJeff Garzik  *	None.
5596c6fd2807SJeff Garzik  *
5597c6fd2807SJeff Garzik  *	RETURNS:
5598b5b3fa38STejun Heo  *	True if the port offline status is available and offline.
5599c6fd2807SJeff Garzik  */
5600b1c72916STejun Heo bool ata_phys_link_offline(struct ata_link *link)
5601c6fd2807SJeff Garzik {
5602c6fd2807SJeff Garzik 	u32 sstatus;
5603c6fd2807SJeff Garzik 
5604936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
56059913ff8aSTejun Heo 	    !ata_sstatus_online(sstatus))
5606b5b3fa38STejun Heo 		return true;
5607b5b3fa38STejun Heo 	return false;
5608c6fd2807SJeff Garzik }
5609c6fd2807SJeff Garzik 
5610b1c72916STejun Heo /**
5611b1c72916STejun Heo  *	ata_link_online - test whether the given link is online
5612b1c72916STejun Heo  *	@link: ATA link to test
5613b1c72916STejun Heo  *
5614b1c72916STejun Heo  *	Test whether @link is online.  This is identical to
5615b1c72916STejun Heo  *	ata_phys_link_online() when there's no slave link.  When
5616b1c72916STejun Heo  *	there's a slave link, this function should only be called on
5617b1c72916STejun Heo  *	the master link and will return true if any of M/S links is
5618b1c72916STejun Heo  *	online.
5619b1c72916STejun Heo  *
5620b1c72916STejun Heo  *	LOCKING:
5621b1c72916STejun Heo  *	None.
5622b1c72916STejun Heo  *
5623b1c72916STejun Heo  *	RETURNS:
5624b1c72916STejun Heo  *	True if the port online status is available and online.
5625b1c72916STejun Heo  */
5626b1c72916STejun Heo bool ata_link_online(struct ata_link *link)
5627b1c72916STejun Heo {
5628b1c72916STejun Heo 	struct ata_link *slave = link->ap->slave_link;
5629b1c72916STejun Heo 
5630b1c72916STejun Heo 	WARN_ON(link == slave);	/* shouldn't be called on slave link */
5631b1c72916STejun Heo 
5632b1c72916STejun Heo 	return ata_phys_link_online(link) ||
5633b1c72916STejun Heo 		(slave && ata_phys_link_online(slave));
5634b1c72916STejun Heo }
5635b1c72916STejun Heo 
5636b1c72916STejun Heo /**
5637b1c72916STejun Heo  *	ata_link_offline - test whether the given link is offline
5638b1c72916STejun Heo  *	@link: ATA link to test
5639b1c72916STejun Heo  *
5640b1c72916STejun Heo  *	Test whether @link is offline.  This is identical to
5641b1c72916STejun Heo  *	ata_phys_link_offline() when there's no slave link.  When
5642b1c72916STejun Heo  *	there's a slave link, this function should only be called on
5643b1c72916STejun Heo  *	the master link and will return true if both M/S links are
5644b1c72916STejun Heo  *	offline.
5645b1c72916STejun Heo  *
5646b1c72916STejun Heo  *	LOCKING:
5647b1c72916STejun Heo  *	None.
5648b1c72916STejun Heo  *
5649b1c72916STejun Heo  *	RETURNS:
5650b1c72916STejun Heo  *	True if the port offline status is available and offline.
5651b1c72916STejun Heo  */
5652b1c72916STejun Heo bool ata_link_offline(struct ata_link *link)
5653b1c72916STejun Heo {
5654b1c72916STejun Heo 	struct ata_link *slave = link->ap->slave_link;
5655b1c72916STejun Heo 
5656b1c72916STejun Heo 	WARN_ON(link == slave);	/* shouldn't be called on slave link */
5657b1c72916STejun Heo 
5658b1c72916STejun Heo 	return ata_phys_link_offline(link) &&
5659b1c72916STejun Heo 		(!slave || ata_phys_link_offline(slave));
5660b1c72916STejun Heo }
5661b1c72916STejun Heo 
56626ffa01d8STejun Heo #ifdef CONFIG_PM
5663bc6e7c4bSDan Williams static void ata_port_request_pm(struct ata_port *ap, pm_message_t mesg,
5664cca3974eSJeff Garzik 				unsigned int action, unsigned int ehi_flags,
5665bc6e7c4bSDan Williams 				bool async)
5666c6fd2807SJeff Garzik {
5667e3667ebfSTejun Heo 	struct ata_link *link;
56685ef41082SLin Ming 	unsigned long flags;
5669c6fd2807SJeff Garzik 
5670c6fd2807SJeff Garzik 	/* Previous resume operation might still be in
5671c6fd2807SJeff Garzik 	 * progress.  Wait for PM_PENDING to clear.
5672c6fd2807SJeff Garzik 	 */
5673c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_PM_PENDING) {
5674c6fd2807SJeff Garzik 		ata_port_wait_eh(ap);
5675c6fd2807SJeff Garzik 		WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5676c6fd2807SJeff Garzik 	}
5677c6fd2807SJeff Garzik 
5678c6fd2807SJeff Garzik 	/* request PM ops to EH */
5679c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
5680c6fd2807SJeff Garzik 
5681c6fd2807SJeff Garzik 	ap->pm_mesg = mesg;
5682c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_PM_PENDING;
56831eca4365STejun Heo 	ata_for_each_link(link, ap, HOST_FIRST) {
5684e3667ebfSTejun Heo 		link->eh_info.action |= action;
5685e3667ebfSTejun Heo 		link->eh_info.flags |= ehi_flags;
5686e3667ebfSTejun Heo 	}
5687c6fd2807SJeff Garzik 
5688c6fd2807SJeff Garzik 	ata_port_schedule_eh(ap);
5689c6fd2807SJeff Garzik 
5690c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
5691c6fd2807SJeff Garzik 
56922fcbdcb4SDan Williams 	if (!async) {
5693c6fd2807SJeff Garzik 		ata_port_wait_eh(ap);
5694c6fd2807SJeff Garzik 		WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5695c6fd2807SJeff Garzik 	}
5696c6fd2807SJeff Garzik }
5697c6fd2807SJeff Garzik 
569833574d68SLin Ming /*
5699bc6e7c4bSDan Williams  * On some hardware, device fails to respond after spun down for suspend.  As
5700bc6e7c4bSDan Williams  * the device won't be used before being resumed, we don't need to touch the
5701bc6e7c4bSDan Williams  * device.  Ask EH to skip the usual stuff and proceed directly to suspend.
570233574d68SLin Ming  *
570333574d68SLin Ming  * http://thread.gmane.org/gmane.linux.ide/46764
570433574d68SLin Ming  */
5705bc6e7c4bSDan Williams static const unsigned int ata_port_suspend_ehi = ATA_EHI_QUIET
5706bc6e7c4bSDan Williams 						 | ATA_EHI_NO_AUTOPSY
5707bc6e7c4bSDan Williams 						 | ATA_EHI_NO_RECOVERY;
5708bc6e7c4bSDan Williams 
5709bc6e7c4bSDan Williams static void ata_port_suspend(struct ata_port *ap, pm_message_t mesg)
5710bc6e7c4bSDan Williams {
5711bc6e7c4bSDan Williams 	ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, false);
57125ef41082SLin Ming }
57135ef41082SLin Ming 
5714bc6e7c4bSDan Williams static void ata_port_suspend_async(struct ata_port *ap, pm_message_t mesg)
5715bc6e7c4bSDan Williams {
5716bc6e7c4bSDan Williams 	ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, true);
5717bc6e7c4bSDan Williams }
5718bc6e7c4bSDan Williams 
5719bc6e7c4bSDan Williams static int ata_port_pm_suspend(struct device *dev)
57202fcbdcb4SDan Williams {
57212fcbdcb4SDan Williams 	struct ata_port *ap = to_ata_port(dev);
57222fcbdcb4SDan Williams 
57235ef41082SLin Ming 	if (pm_runtime_suspended(dev))
57245ef41082SLin Ming 		return 0;
57255ef41082SLin Ming 
5726bc6e7c4bSDan Williams 	ata_port_suspend(ap, PMSG_SUSPEND);
5727f5e6d0d0SAaron Lu 	return 0;
572833574d68SLin Ming }
572933574d68SLin Ming 
5730bc6e7c4bSDan Williams static int ata_port_pm_freeze(struct device *dev)
57312fcbdcb4SDan Williams {
57322fcbdcb4SDan Williams 	struct ata_port *ap = to_ata_port(dev);
57332fcbdcb4SDan Williams 
5734bc6e7c4bSDan Williams 	if (pm_runtime_suspended(dev))
5735bc6e7c4bSDan Williams 		return 0;
5736bc6e7c4bSDan Williams 
5737bc6e7c4bSDan Williams 	ata_port_suspend(ap, PMSG_FREEZE);
5738bc6e7c4bSDan Williams 	return 0;
57392fcbdcb4SDan Williams }
57402fcbdcb4SDan Williams 
5741bc6e7c4bSDan Williams static int ata_port_pm_poweroff(struct device *dev)
5742e90b1e5aSLin Ming {
5743bc6e7c4bSDan Williams 	ata_port_suspend(to_ata_port(dev), PMSG_HIBERNATE);
5744bc6e7c4bSDan Williams 	return 0;
5745bc6e7c4bSDan Williams }
5746e90b1e5aSLin Ming 
5747bc6e7c4bSDan Williams static const unsigned int ata_port_resume_ehi = ATA_EHI_NO_AUTOPSY
5748bc6e7c4bSDan Williams 						| ATA_EHI_QUIET;
5749bc6e7c4bSDan Williams 
5750bc6e7c4bSDan Williams static void ata_port_resume(struct ata_port *ap, pm_message_t mesg)
5751bc6e7c4bSDan Williams {
5752bc6e7c4bSDan Williams 	ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, false);
5753bc6e7c4bSDan Williams }
5754bc6e7c4bSDan Williams 
5755bc6e7c4bSDan Williams static void ata_port_resume_async(struct ata_port *ap, pm_message_t mesg)
5756bc6e7c4bSDan Williams {
5757bc6e7c4bSDan Williams 	ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, true);
5758bc6e7c4bSDan Williams }
5759bc6e7c4bSDan Williams 
5760bc6e7c4bSDan Williams static int ata_port_pm_resume(struct device *dev)
5761bc6e7c4bSDan Williams {
5762200421a8STodd Brandt 	ata_port_resume_async(to_ata_port(dev), PMSG_RESUME);
5763e90b1e5aSLin Ming 	pm_runtime_disable(dev);
5764e90b1e5aSLin Ming 	pm_runtime_set_active(dev);
5765e90b1e5aSLin Ming 	pm_runtime_enable(dev);
5766bc6e7c4bSDan Williams 	return 0;
5767e90b1e5aSLin Ming }
5768e90b1e5aSLin Ming 
57697e15e9beSAaron Lu /*
57707e15e9beSAaron Lu  * For ODDs, the upper layer will poll for media change every few seconds,
57717e15e9beSAaron Lu  * which will make it enter and leave suspend state every few seconds. And
57727e15e9beSAaron Lu  * as each suspend will cause a hard/soft reset, the gain of runtime suspend
57737e15e9beSAaron Lu  * is very little and the ODD may malfunction after constantly being reset.
57747e15e9beSAaron Lu  * So the idle callback here will not proceed to suspend if a non-ZPODD capable
57757e15e9beSAaron Lu  * ODD is attached to the port.
57767e15e9beSAaron Lu  */
57779ee4f393SLin Ming static int ata_port_runtime_idle(struct device *dev)
57789ee4f393SLin Ming {
57797e15e9beSAaron Lu 	struct ata_port *ap = to_ata_port(dev);
57807e15e9beSAaron Lu 	struct ata_link *link;
57817e15e9beSAaron Lu 	struct ata_device *adev;
57827e15e9beSAaron Lu 
57837e15e9beSAaron Lu 	ata_for_each_link(link, ap, HOST_FIRST) {
57847e15e9beSAaron Lu 		ata_for_each_dev(adev, link, ENABLED)
57857e15e9beSAaron Lu 			if (adev->class == ATA_DEV_ATAPI &&
57867e15e9beSAaron Lu 			    !zpodd_dev_enabled(adev))
57877e15e9beSAaron Lu 				return -EBUSY;
57887e15e9beSAaron Lu 	}
57897e15e9beSAaron Lu 
579045f0a85cSRafael J. Wysocki 	return 0;
57919ee4f393SLin Ming }
57929ee4f393SLin Ming 
5793a7ff60dbSAaron Lu static int ata_port_runtime_suspend(struct device *dev)
5794a7ff60dbSAaron Lu {
5795bc6e7c4bSDan Williams 	ata_port_suspend(to_ata_port(dev), PMSG_AUTO_SUSPEND);
5796bc6e7c4bSDan Williams 	return 0;
5797a7ff60dbSAaron Lu }
5798a7ff60dbSAaron Lu 
5799a7ff60dbSAaron Lu static int ata_port_runtime_resume(struct device *dev)
5800a7ff60dbSAaron Lu {
5801bc6e7c4bSDan Williams 	ata_port_resume(to_ata_port(dev), PMSG_AUTO_RESUME);
5802bc6e7c4bSDan Williams 	return 0;
5803a7ff60dbSAaron Lu }
5804a7ff60dbSAaron Lu 
58055ef41082SLin Ming static const struct dev_pm_ops ata_port_pm_ops = {
5806bc6e7c4bSDan Williams 	.suspend = ata_port_pm_suspend,
5807bc6e7c4bSDan Williams 	.resume = ata_port_pm_resume,
5808bc6e7c4bSDan Williams 	.freeze = ata_port_pm_freeze,
5809bc6e7c4bSDan Williams 	.thaw = ata_port_pm_resume,
5810bc6e7c4bSDan Williams 	.poweroff = ata_port_pm_poweroff,
5811bc6e7c4bSDan Williams 	.restore = ata_port_pm_resume,
58129ee4f393SLin Ming 
5813a7ff60dbSAaron Lu 	.runtime_suspend = ata_port_runtime_suspend,
5814a7ff60dbSAaron Lu 	.runtime_resume = ata_port_runtime_resume,
58159ee4f393SLin Ming 	.runtime_idle = ata_port_runtime_idle,
58165ef41082SLin Ming };
58175ef41082SLin Ming 
58182fcbdcb4SDan Williams /* sas ports don't participate in pm runtime management of ata_ports,
58192fcbdcb4SDan Williams  * and need to resume ata devices at the domain level, not the per-port
58202fcbdcb4SDan Williams  * level. sas suspend/resume is async to allow parallel port recovery
58212fcbdcb4SDan Williams  * since sas has multiple ata_port instances per Scsi_Host.
58222fcbdcb4SDan Williams  */
5823bc6e7c4bSDan Williams void ata_sas_port_suspend(struct ata_port *ap)
58242fcbdcb4SDan Williams {
5825bc6e7c4bSDan Williams 	ata_port_suspend_async(ap, PMSG_SUSPEND);
58262fcbdcb4SDan Williams }
5827bc6e7c4bSDan Williams EXPORT_SYMBOL_GPL(ata_sas_port_suspend);
58282fcbdcb4SDan Williams 
5829bc6e7c4bSDan Williams void ata_sas_port_resume(struct ata_port *ap)
58302fcbdcb4SDan Williams {
5831bc6e7c4bSDan Williams 	ata_port_resume_async(ap, PMSG_RESUME);
58322fcbdcb4SDan Williams }
5833bc6e7c4bSDan Williams EXPORT_SYMBOL_GPL(ata_sas_port_resume);
58342fcbdcb4SDan Williams 
5835c6fd2807SJeff Garzik /**
5836cca3974eSJeff Garzik  *	ata_host_suspend - suspend host
5837cca3974eSJeff Garzik  *	@host: host to suspend
5838c6fd2807SJeff Garzik  *	@mesg: PM message
5839c6fd2807SJeff Garzik  *
58405ef41082SLin Ming  *	Suspend @host.  Actual operation is performed by port suspend.
5841c6fd2807SJeff Garzik  */
5842cca3974eSJeff Garzik int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
5843c6fd2807SJeff Garzik {
584472ad6ec4SJeff Garzik 	host->dev->power.power_state = mesg;
58455ef41082SLin Ming 	return 0;
5846c6fd2807SJeff Garzik }
5847c6fd2807SJeff Garzik 
5848c6fd2807SJeff Garzik /**
5849cca3974eSJeff Garzik  *	ata_host_resume - resume host
5850cca3974eSJeff Garzik  *	@host: host to resume
5851c6fd2807SJeff Garzik  *
58525ef41082SLin Ming  *	Resume @host.  Actual operation is performed by port resume.
5853c6fd2807SJeff Garzik  */
5854cca3974eSJeff Garzik void ata_host_resume(struct ata_host *host)
5855c6fd2807SJeff Garzik {
585672ad6ec4SJeff Garzik 	host->dev->power.power_state = PMSG_ON;
5857c6fd2807SJeff Garzik }
58586ffa01d8STejun Heo #endif
5859c6fd2807SJeff Garzik 
58608df82c13SBhumika Goyal const struct device_type ata_port_type = {
58615ef41082SLin Ming 	.name = "ata_port",
58625ef41082SLin Ming #ifdef CONFIG_PM
58635ef41082SLin Ming 	.pm = &ata_port_pm_ops,
58645ef41082SLin Ming #endif
58655ef41082SLin Ming };
58665ef41082SLin Ming 
5867c6fd2807SJeff Garzik /**
5868c6fd2807SJeff Garzik  *	ata_dev_init - Initialize an ata_device structure
5869c6fd2807SJeff Garzik  *	@dev: Device structure to initialize
5870c6fd2807SJeff Garzik  *
5871c6fd2807SJeff Garzik  *	Initialize @dev in preparation for probing.
5872c6fd2807SJeff Garzik  *
5873c6fd2807SJeff Garzik  *	LOCKING:
5874c6fd2807SJeff Garzik  *	Inherited from caller.
5875c6fd2807SJeff Garzik  */
5876c6fd2807SJeff Garzik void ata_dev_init(struct ata_device *dev)
5877c6fd2807SJeff Garzik {
5878b1c72916STejun Heo 	struct ata_link *link = ata_dev_phys_link(dev);
58799af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
5880c6fd2807SJeff Garzik 	unsigned long flags;
5881c6fd2807SJeff Garzik 
5882b1c72916STejun Heo 	/* SATA spd limit is bound to the attached device, reset together */
58839af5c9c9STejun Heo 	link->sata_spd_limit = link->hw_sata_spd_limit;
58849af5c9c9STejun Heo 	link->sata_spd = 0;
5885c6fd2807SJeff Garzik 
5886c6fd2807SJeff Garzik 	/* High bits of dev->flags are used to record warm plug
5887c6fd2807SJeff Garzik 	 * requests which occur asynchronously.  Synchronize using
5888cca3974eSJeff Garzik 	 * host lock.
5889c6fd2807SJeff Garzik 	 */
5890c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
5891c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_INIT_MASK;
58923dcc323fSTejun Heo 	dev->horkage = 0;
5893c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
5894c6fd2807SJeff Garzik 
589599cf610aSTejun Heo 	memset((void *)dev + ATA_DEVICE_CLEAR_BEGIN, 0,
589699cf610aSTejun Heo 	       ATA_DEVICE_CLEAR_END - ATA_DEVICE_CLEAR_BEGIN);
5897c6fd2807SJeff Garzik 	dev->pio_mask = UINT_MAX;
5898c6fd2807SJeff Garzik 	dev->mwdma_mask = UINT_MAX;
5899c6fd2807SJeff Garzik 	dev->udma_mask = UINT_MAX;
5900c6fd2807SJeff Garzik }
5901c6fd2807SJeff Garzik 
5902c6fd2807SJeff Garzik /**
59034fb37a25STejun Heo  *	ata_link_init - Initialize an ata_link structure
59044fb37a25STejun Heo  *	@ap: ATA port link is attached to
59054fb37a25STejun Heo  *	@link: Link structure to initialize
59068989805dSTejun Heo  *	@pmp: Port multiplier port number
59074fb37a25STejun Heo  *
59084fb37a25STejun Heo  *	Initialize @link.
59094fb37a25STejun Heo  *
59104fb37a25STejun Heo  *	LOCKING:
59114fb37a25STejun Heo  *	Kernel thread context (may sleep)
59124fb37a25STejun Heo  */
5913fb7fd614STejun Heo void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
59144fb37a25STejun Heo {
59154fb37a25STejun Heo 	int i;
59164fb37a25STejun Heo 
59174fb37a25STejun Heo 	/* clear everything except for devices */
5918d9027470SGwendal Grignou 	memset((void *)link + ATA_LINK_CLEAR_BEGIN, 0,
5919d9027470SGwendal Grignou 	       ATA_LINK_CLEAR_END - ATA_LINK_CLEAR_BEGIN);
59204fb37a25STejun Heo 
59214fb37a25STejun Heo 	link->ap = ap;
59228989805dSTejun Heo 	link->pmp = pmp;
59234fb37a25STejun Heo 	link->active_tag = ATA_TAG_POISON;
59244fb37a25STejun Heo 	link->hw_sata_spd_limit = UINT_MAX;
59254fb37a25STejun Heo 
59264fb37a25STejun Heo 	/* can't use iterator, ap isn't initialized yet */
59274fb37a25STejun Heo 	for (i = 0; i < ATA_MAX_DEVICES; i++) {
59284fb37a25STejun Heo 		struct ata_device *dev = &link->device[i];
59294fb37a25STejun Heo 
59304fb37a25STejun Heo 		dev->link = link;
59314fb37a25STejun Heo 		dev->devno = dev - link->device;
5932110f66d2STejun Heo #ifdef CONFIG_ATA_ACPI
5933110f66d2STejun Heo 		dev->gtf_filter = ata_acpi_gtf_filter;
5934110f66d2STejun Heo #endif
59354fb37a25STejun Heo 		ata_dev_init(dev);
59364fb37a25STejun Heo 	}
59374fb37a25STejun Heo }
59384fb37a25STejun Heo 
59394fb37a25STejun Heo /**
59404fb37a25STejun Heo  *	sata_link_init_spd - Initialize link->sata_spd_limit
59414fb37a25STejun Heo  *	@link: Link to configure sata_spd_limit for
59424fb37a25STejun Heo  *
59434fb37a25STejun Heo  *	Initialize @link->[hw_]sata_spd_limit to the currently
59444fb37a25STejun Heo  *	configured value.
59454fb37a25STejun Heo  *
59464fb37a25STejun Heo  *	LOCKING:
59474fb37a25STejun Heo  *	Kernel thread context (may sleep).
59484fb37a25STejun Heo  *
59494fb37a25STejun Heo  *	RETURNS:
59504fb37a25STejun Heo  *	0 on success, -errno on failure.
59514fb37a25STejun Heo  */
5952fb7fd614STejun Heo int sata_link_init_spd(struct ata_link *link)
59534fb37a25STejun Heo {
595433267325STejun Heo 	u8 spd;
59554fb37a25STejun Heo 	int rc;
59564fb37a25STejun Heo 
5957d127ea7bSTejun Heo 	rc = sata_scr_read(link, SCR_CONTROL, &link->saved_scontrol);
59584fb37a25STejun Heo 	if (rc)
59594fb37a25STejun Heo 		return rc;
59604fb37a25STejun Heo 
5961d127ea7bSTejun Heo 	spd = (link->saved_scontrol >> 4) & 0xf;
59624fb37a25STejun Heo 	if (spd)
59634fb37a25STejun Heo 		link->hw_sata_spd_limit &= (1 << spd) - 1;
59644fb37a25STejun Heo 
596505944bdfSTejun Heo 	ata_force_link_limits(link);
596633267325STejun Heo 
59674fb37a25STejun Heo 	link->sata_spd_limit = link->hw_sata_spd_limit;
59684fb37a25STejun Heo 
59694fb37a25STejun Heo 	return 0;
59704fb37a25STejun Heo }
59714fb37a25STejun Heo 
59724fb37a25STejun Heo /**
5973f3187195STejun Heo  *	ata_port_alloc - allocate and initialize basic ATA port resources
5974f3187195STejun Heo  *	@host: ATA host this allocated port belongs to
5975c6fd2807SJeff Garzik  *
5976f3187195STejun Heo  *	Allocate and initialize basic ATA port resources.
5977f3187195STejun Heo  *
5978f3187195STejun Heo  *	RETURNS:
5979f3187195STejun Heo  *	Allocate ATA port on success, NULL on failure.
5980c6fd2807SJeff Garzik  *
5981c6fd2807SJeff Garzik  *	LOCKING:
5982f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5983c6fd2807SJeff Garzik  */
5984f3187195STejun Heo struct ata_port *ata_port_alloc(struct ata_host *host)
5985c6fd2807SJeff Garzik {
5986f3187195STejun Heo 	struct ata_port *ap;
5987c6fd2807SJeff Garzik 
5988f3187195STejun Heo 	DPRINTK("ENTER\n");
5989f3187195STejun Heo 
5990f3187195STejun Heo 	ap = kzalloc(sizeof(*ap), GFP_KERNEL);
5991f3187195STejun Heo 	if (!ap)
5992f3187195STejun Heo 		return NULL;
5993f3187195STejun Heo 
59947b3a24c5SMaxime Bizon 	ap->pflags |= ATA_PFLAG_INITIALIZING | ATA_PFLAG_FROZEN;
5995cca3974eSJeff Garzik 	ap->lock = &host->lock;
5996f3187195STejun Heo 	ap->print_id = -1;
5997e628dc99SDavid Milburn 	ap->local_port_no = -1;
5998cca3974eSJeff Garzik 	ap->host = host;
5999f3187195STejun Heo 	ap->dev = host->dev;
6000c6fd2807SJeff Garzik 
6001c6fd2807SJeff Garzik #if defined(ATA_VERBOSE_DEBUG)
6002c6fd2807SJeff Garzik 	/* turn on all debugging levels */
6003c6fd2807SJeff Garzik 	ap->msg_enable = 0x00FF;
6004c6fd2807SJeff Garzik #elif defined(ATA_DEBUG)
6005c6fd2807SJeff Garzik 	ap->msg_enable = ATA_MSG_DRV | ATA_MSG_INFO | ATA_MSG_CTL | ATA_MSG_WARN | ATA_MSG_ERR;
6006c6fd2807SJeff Garzik #else
6007c6fd2807SJeff Garzik 	ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
6008c6fd2807SJeff Garzik #endif
6009c6fd2807SJeff Garzik 
6010ad72cf98STejun Heo 	mutex_init(&ap->scsi_scan_mutex);
601165f27f38SDavid Howells 	INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
601265f27f38SDavid Howells 	INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
6013c6fd2807SJeff Garzik 	INIT_LIST_HEAD(&ap->eh_done_q);
6014c6fd2807SJeff Garzik 	init_waitqueue_head(&ap->eh_wait_q);
601545fabbb7SElias Oltmanns 	init_completion(&ap->park_req_pending);
6016b93ab338SKees Cook 	timer_setup(&ap->fastdrain_timer, ata_eh_fastdrain_timerfn,
6017b93ab338SKees Cook 		    TIMER_DEFERRABLE);
6018c6fd2807SJeff Garzik 
6019c6fd2807SJeff Garzik 	ap->cbl = ATA_CBL_NONE;
6020c6fd2807SJeff Garzik 
60218989805dSTejun Heo 	ata_link_init(ap, &ap->link, 0);
6022c6fd2807SJeff Garzik 
6023c6fd2807SJeff Garzik #ifdef ATA_IRQ_TRAP
6024c6fd2807SJeff Garzik 	ap->stats.unhandled_irq = 1;
6025c6fd2807SJeff Garzik 	ap->stats.idle_irq = 1;
6026c6fd2807SJeff Garzik #endif
6027270390e1STejun Heo 	ata_sff_port_init(ap);
6028270390e1STejun Heo 
6029c6fd2807SJeff Garzik 	return ap;
6030c6fd2807SJeff Garzik }
6031c6fd2807SJeff Garzik 
60322623c7a5STaras Kondratiuk static void ata_devres_release(struct device *gendev, void *res)
6033f0d36efdSTejun Heo {
6034f0d36efdSTejun Heo 	struct ata_host *host = dev_get_drvdata(gendev);
6035f0d36efdSTejun Heo 	int i;
6036f0d36efdSTejun Heo 
6037f0d36efdSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
6038f0d36efdSTejun Heo 		struct ata_port *ap = host->ports[i];
6039f0d36efdSTejun Heo 
6040ecef7253STejun Heo 		if (!ap)
6041ecef7253STejun Heo 			continue;
6042ecef7253STejun Heo 
60434911487aSTejun Heo 		if (ap->scsi_host)
60441aa506e4STejun Heo 			scsi_host_put(ap->scsi_host);
60451aa506e4STejun Heo 
60462623c7a5STaras Kondratiuk 	}
60472623c7a5STaras Kondratiuk 
60482623c7a5STaras Kondratiuk 	dev_set_drvdata(gendev, NULL);
60492623c7a5STaras Kondratiuk 	ata_host_put(host);
60502623c7a5STaras Kondratiuk }
60512623c7a5STaras Kondratiuk 
60522623c7a5STaras Kondratiuk static void ata_host_release(struct kref *kref)
60532623c7a5STaras Kondratiuk {
60542623c7a5STaras Kondratiuk 	struct ata_host *host = container_of(kref, struct ata_host, kref);
60552623c7a5STaras Kondratiuk 	int i;
60562623c7a5STaras Kondratiuk 
60572623c7a5STaras Kondratiuk 	for (i = 0; i < host->n_ports; i++) {
60582623c7a5STaras Kondratiuk 		struct ata_port *ap = host->ports[i];
60592623c7a5STaras Kondratiuk 
6060633273a3STejun Heo 		kfree(ap->pmp_link);
6061b1c72916STejun Heo 		kfree(ap->slave_link);
60624911487aSTejun Heo 		kfree(ap);
60631aa506e4STejun Heo 		host->ports[i] = NULL;
60641aa506e4STejun Heo 	}
60652623c7a5STaras Kondratiuk 	kfree(host);
60662623c7a5STaras Kondratiuk }
60671aa506e4STejun Heo 
60682623c7a5STaras Kondratiuk void ata_host_get(struct ata_host *host)
60692623c7a5STaras Kondratiuk {
60702623c7a5STaras Kondratiuk 	kref_get(&host->kref);
60712623c7a5STaras Kondratiuk }
60722623c7a5STaras Kondratiuk 
60732623c7a5STaras Kondratiuk void ata_host_put(struct ata_host *host)
60742623c7a5STaras Kondratiuk {
60752623c7a5STaras Kondratiuk 	kref_put(&host->kref, ata_host_release);
6076f0d36efdSTejun Heo }
6077f0d36efdSTejun Heo 
6078c6fd2807SJeff Garzik /**
6079f3187195STejun Heo  *	ata_host_alloc - allocate and init basic ATA host resources
6080f3187195STejun Heo  *	@dev: generic device this host is associated with
6081f3187195STejun Heo  *	@max_ports: maximum number of ATA ports associated with this host
6082f3187195STejun Heo  *
6083f3187195STejun Heo  *	Allocate and initialize basic ATA host resources.  LLD calls
6084f3187195STejun Heo  *	this function to allocate a host, initializes it fully and
6085f3187195STejun Heo  *	attaches it using ata_host_register().
6086f3187195STejun Heo  *
6087f3187195STejun Heo  *	@max_ports ports are allocated and host->n_ports is
6088f3187195STejun Heo  *	initialized to @max_ports.  The caller is allowed to decrease
6089f3187195STejun Heo  *	host->n_ports before calling ata_host_register().  The unused
6090f3187195STejun Heo  *	ports will be automatically freed on registration.
6091f3187195STejun Heo  *
6092f3187195STejun Heo  *	RETURNS:
6093f3187195STejun Heo  *	Allocate ATA host on success, NULL on failure.
6094f3187195STejun Heo  *
6095f3187195STejun Heo  *	LOCKING:
6096f3187195STejun Heo  *	Inherited from calling layer (may sleep).
6097f3187195STejun Heo  */
6098f3187195STejun Heo struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
6099f3187195STejun Heo {
6100f3187195STejun Heo 	struct ata_host *host;
6101f3187195STejun Heo 	size_t sz;
6102f3187195STejun Heo 	int i;
61032623c7a5STaras Kondratiuk 	void *dr;
6104f3187195STejun Heo 
6105f3187195STejun Heo 	DPRINTK("ENTER\n");
6106f3187195STejun Heo 
6107f3187195STejun Heo 	/* alloc a container for our list of ATA ports (buses) */
6108f3187195STejun Heo 	sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
61092623c7a5STaras Kondratiuk 	host = kzalloc(sz, GFP_KERNEL);
6110f3187195STejun Heo 	if (!host)
61112623c7a5STaras Kondratiuk 		return NULL;
61122623c7a5STaras Kondratiuk 
6113f3187195STejun Heo 	if (!devres_open_group(dev, NULL, GFP_KERNEL))
6114dafd6c49SColin Ian King 		goto err_free;
6115f3187195STejun Heo 
61162623c7a5STaras Kondratiuk 	dr = devres_alloc(ata_devres_release, 0, GFP_KERNEL);
61172623c7a5STaras Kondratiuk 	if (!dr)
6118f3187195STejun Heo 		goto err_out;
6119f3187195STejun Heo 
61202623c7a5STaras Kondratiuk 	devres_add(dev, dr);
6121f3187195STejun Heo 	dev_set_drvdata(dev, host);
6122f3187195STejun Heo 
6123f3187195STejun Heo 	spin_lock_init(&host->lock);
6124c0c362b6STejun Heo 	mutex_init(&host->eh_mutex);
6125f3187195STejun Heo 	host->dev = dev;
6126f3187195STejun Heo 	host->n_ports = max_ports;
61272623c7a5STaras Kondratiuk 	kref_init(&host->kref);
6128f3187195STejun Heo 
6129f3187195STejun Heo 	/* allocate ports bound to this host */
6130f3187195STejun Heo 	for (i = 0; i < max_ports; i++) {
6131f3187195STejun Heo 		struct ata_port *ap;
6132f3187195STejun Heo 
6133f3187195STejun Heo 		ap = ata_port_alloc(host);
6134f3187195STejun Heo 		if (!ap)
6135f3187195STejun Heo 			goto err_out;
6136f3187195STejun Heo 
6137f3187195STejun Heo 		ap->port_no = i;
6138f3187195STejun Heo 		host->ports[i] = ap;
6139f3187195STejun Heo 	}
6140f3187195STejun Heo 
6141f3187195STejun Heo 	devres_remove_group(dev, NULL);
6142f3187195STejun Heo 	return host;
6143f3187195STejun Heo 
6144f3187195STejun Heo  err_out:
6145f3187195STejun Heo 	devres_release_group(dev, NULL);
6146dafd6c49SColin Ian King  err_free:
6147dafd6c49SColin Ian King 	kfree(host);
6148f3187195STejun Heo 	return NULL;
6149f3187195STejun Heo }
6150f3187195STejun Heo 
6151f3187195STejun Heo /**
6152f5cda257STejun Heo  *	ata_host_alloc_pinfo - alloc host and init with port_info array
6153f5cda257STejun Heo  *	@dev: generic device this host is associated with
6154f5cda257STejun Heo  *	@ppi: array of ATA port_info to initialize host with
6155f5cda257STejun Heo  *	@n_ports: number of ATA ports attached to this host
6156f5cda257STejun Heo  *
6157f5cda257STejun Heo  *	Allocate ATA host and initialize with info from @ppi.  If NULL
6158f5cda257STejun Heo  *	terminated, @ppi may contain fewer entries than @n_ports.  The
6159f5cda257STejun Heo  *	last entry will be used for the remaining ports.
6160f5cda257STejun Heo  *
6161f5cda257STejun Heo  *	RETURNS:
6162f5cda257STejun Heo  *	Allocate ATA host on success, NULL on failure.
6163f5cda257STejun Heo  *
6164f5cda257STejun Heo  *	LOCKING:
6165f5cda257STejun Heo  *	Inherited from calling layer (may sleep).
6166f5cda257STejun Heo  */
6167f5cda257STejun Heo struct ata_host *ata_host_alloc_pinfo(struct device *dev,
6168f5cda257STejun Heo 				      const struct ata_port_info * const * ppi,
6169f5cda257STejun Heo 				      int n_ports)
6170f5cda257STejun Heo {
6171f5cda257STejun Heo 	const struct ata_port_info *pi;
6172f5cda257STejun Heo 	struct ata_host *host;
6173f5cda257STejun Heo 	int i, j;
6174f5cda257STejun Heo 
6175f5cda257STejun Heo 	host = ata_host_alloc(dev, n_ports);
6176f5cda257STejun Heo 	if (!host)
6177f5cda257STejun Heo 		return NULL;
6178f5cda257STejun Heo 
6179f5cda257STejun Heo 	for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
6180f5cda257STejun Heo 		struct ata_port *ap = host->ports[i];
6181f5cda257STejun Heo 
6182f5cda257STejun Heo 		if (ppi[j])
6183f5cda257STejun Heo 			pi = ppi[j++];
6184f5cda257STejun Heo 
6185f5cda257STejun Heo 		ap->pio_mask = pi->pio_mask;
6186f5cda257STejun Heo 		ap->mwdma_mask = pi->mwdma_mask;
6187f5cda257STejun Heo 		ap->udma_mask = pi->udma_mask;
6188f5cda257STejun Heo 		ap->flags |= pi->flags;
61890c88758bSTejun Heo 		ap->link.flags |= pi->link_flags;
6190f5cda257STejun Heo 		ap->ops = pi->port_ops;
6191f5cda257STejun Heo 
6192f5cda257STejun Heo 		if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
6193f5cda257STejun Heo 			host->ops = pi->port_ops;
6194f5cda257STejun Heo 	}
6195f5cda257STejun Heo 
6196f5cda257STejun Heo 	return host;
6197f5cda257STejun Heo }
6198f5cda257STejun Heo 
6199b1c72916STejun Heo /**
6200b1c72916STejun Heo  *	ata_slave_link_init - initialize slave link
6201b1c72916STejun Heo  *	@ap: port to initialize slave link for
6202b1c72916STejun Heo  *
6203b1c72916STejun Heo  *	Create and initialize slave link for @ap.  This enables slave
6204b1c72916STejun Heo  *	link handling on the port.
6205b1c72916STejun Heo  *
6206b1c72916STejun Heo  *	In libata, a port contains links and a link contains devices.
6207b1c72916STejun Heo  *	There is single host link but if a PMP is attached to it,
6208b1c72916STejun Heo  *	there can be multiple fan-out links.  On SATA, there's usually
6209b1c72916STejun Heo  *	a single device connected to a link but PATA and SATA
6210b1c72916STejun Heo  *	controllers emulating TF based interface can have two - master
6211b1c72916STejun Heo  *	and slave.
6212b1c72916STejun Heo  *
6213b1c72916STejun Heo  *	However, there are a few controllers which don't fit into this
6214b1c72916STejun Heo  *	abstraction too well - SATA controllers which emulate TF
6215b1c72916STejun Heo  *	interface with both master and slave devices but also have
6216b1c72916STejun Heo  *	separate SCR register sets for each device.  These controllers
6217b1c72916STejun Heo  *	need separate links for physical link handling
6218b1c72916STejun Heo  *	(e.g. onlineness, link speed) but should be treated like a
6219b1c72916STejun Heo  *	traditional M/S controller for everything else (e.g. command
6220b1c72916STejun Heo  *	issue, softreset).
6221b1c72916STejun Heo  *
6222b1c72916STejun Heo  *	slave_link is libata's way of handling this class of
6223b1c72916STejun Heo  *	controllers without impacting core layer too much.  For
6224b1c72916STejun Heo  *	anything other than physical link handling, the default host
6225b1c72916STejun Heo  *	link is used for both master and slave.  For physical link
6226b1c72916STejun Heo  *	handling, separate @ap->slave_link is used.  All dirty details
6227b1c72916STejun Heo  *	are implemented inside libata core layer.  From LLD's POV, the
6228b1c72916STejun Heo  *	only difference is that prereset, hardreset and postreset are
6229b1c72916STejun Heo  *	called once more for the slave link, so the reset sequence
6230b1c72916STejun Heo  *	looks like the following.
6231b1c72916STejun Heo  *
6232b1c72916STejun Heo  *	prereset(M) -> prereset(S) -> hardreset(M) -> hardreset(S) ->
6233b1c72916STejun Heo  *	softreset(M) -> postreset(M) -> postreset(S)
6234b1c72916STejun Heo  *
6235b1c72916STejun Heo  *	Note that softreset is called only for the master.  Softreset
6236b1c72916STejun Heo  *	resets both M/S by definition, so SRST on master should handle
6237b1c72916STejun Heo  *	both (the standard method will work just fine).
6238b1c72916STejun Heo  *
6239b1c72916STejun Heo  *	LOCKING:
6240b1c72916STejun Heo  *	Should be called before host is registered.
6241b1c72916STejun Heo  *
6242b1c72916STejun Heo  *	RETURNS:
6243b1c72916STejun Heo  *	0 on success, -errno on failure.
6244b1c72916STejun Heo  */
6245b1c72916STejun Heo int ata_slave_link_init(struct ata_port *ap)
6246b1c72916STejun Heo {
6247b1c72916STejun Heo 	struct ata_link *link;
6248b1c72916STejun Heo 
6249b1c72916STejun Heo 	WARN_ON(ap->slave_link);
6250b1c72916STejun Heo 	WARN_ON(ap->flags & ATA_FLAG_PMP);
6251b1c72916STejun Heo 
6252b1c72916STejun Heo 	link = kzalloc(sizeof(*link), GFP_KERNEL);
6253b1c72916STejun Heo 	if (!link)
6254b1c72916STejun Heo 		return -ENOMEM;
6255b1c72916STejun Heo 
6256b1c72916STejun Heo 	ata_link_init(ap, link, 1);
6257b1c72916STejun Heo 	ap->slave_link = link;
6258b1c72916STejun Heo 	return 0;
6259b1c72916STejun Heo }
6260b1c72916STejun Heo 
626132ebbc0cSTejun Heo static void ata_host_stop(struct device *gendev, void *res)
626232ebbc0cSTejun Heo {
626332ebbc0cSTejun Heo 	struct ata_host *host = dev_get_drvdata(gendev);
626432ebbc0cSTejun Heo 	int i;
626532ebbc0cSTejun Heo 
626632ebbc0cSTejun Heo 	WARN_ON(!(host->flags & ATA_HOST_STARTED));
626732ebbc0cSTejun Heo 
626832ebbc0cSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
626932ebbc0cSTejun Heo 		struct ata_port *ap = host->ports[i];
627032ebbc0cSTejun Heo 
627132ebbc0cSTejun Heo 		if (ap->ops->port_stop)
627232ebbc0cSTejun Heo 			ap->ops->port_stop(ap);
627332ebbc0cSTejun Heo 	}
627432ebbc0cSTejun Heo 
627532ebbc0cSTejun Heo 	if (host->ops->host_stop)
627632ebbc0cSTejun Heo 		host->ops->host_stop(host);
627732ebbc0cSTejun Heo }
627832ebbc0cSTejun Heo 
6279f5cda257STejun Heo /**
6280029cfd6bSTejun Heo  *	ata_finalize_port_ops - finalize ata_port_operations
6281029cfd6bSTejun Heo  *	@ops: ata_port_operations to finalize
6282029cfd6bSTejun Heo  *
6283029cfd6bSTejun Heo  *	An ata_port_operations can inherit from another ops and that
6284029cfd6bSTejun Heo  *	ops can again inherit from another.  This can go on as many
6285029cfd6bSTejun Heo  *	times as necessary as long as there is no loop in the
6286029cfd6bSTejun Heo  *	inheritance chain.
6287029cfd6bSTejun Heo  *
6288029cfd6bSTejun Heo  *	Ops tables are finalized when the host is started.  NULL or
6289029cfd6bSTejun Heo  *	unspecified entries are inherited from the closet ancestor
6290029cfd6bSTejun Heo  *	which has the method and the entry is populated with it.
6291029cfd6bSTejun Heo  *	After finalization, the ops table directly points to all the
6292029cfd6bSTejun Heo  *	methods and ->inherits is no longer necessary and cleared.
6293029cfd6bSTejun Heo  *
6294029cfd6bSTejun Heo  *	Using ATA_OP_NULL, inheriting ops can force a method to NULL.
6295029cfd6bSTejun Heo  *
6296029cfd6bSTejun Heo  *	LOCKING:
6297029cfd6bSTejun Heo  *	None.
6298029cfd6bSTejun Heo  */
6299029cfd6bSTejun Heo static void ata_finalize_port_ops(struct ata_port_operations *ops)
6300029cfd6bSTejun Heo {
63012da67659SPradeep Singh Rautela 	static DEFINE_SPINLOCK(lock);
6302029cfd6bSTejun Heo 	const struct ata_port_operations *cur;
6303029cfd6bSTejun Heo 	void **begin = (void **)ops;
6304029cfd6bSTejun Heo 	void **end = (void **)&ops->inherits;
6305029cfd6bSTejun Heo 	void **pp;
6306029cfd6bSTejun Heo 
6307029cfd6bSTejun Heo 	if (!ops || !ops->inherits)
6308029cfd6bSTejun Heo 		return;
6309029cfd6bSTejun Heo 
6310029cfd6bSTejun Heo 	spin_lock(&lock);
6311029cfd6bSTejun Heo 
6312029cfd6bSTejun Heo 	for (cur = ops->inherits; cur; cur = cur->inherits) {
6313029cfd6bSTejun Heo 		void **inherit = (void **)cur;
6314029cfd6bSTejun Heo 
6315029cfd6bSTejun Heo 		for (pp = begin; pp < end; pp++, inherit++)
6316029cfd6bSTejun Heo 			if (!*pp)
6317029cfd6bSTejun Heo 				*pp = *inherit;
6318029cfd6bSTejun Heo 	}
6319029cfd6bSTejun Heo 
6320029cfd6bSTejun Heo 	for (pp = begin; pp < end; pp++)
6321029cfd6bSTejun Heo 		if (IS_ERR(*pp))
6322029cfd6bSTejun Heo 			*pp = NULL;
6323029cfd6bSTejun Heo 
6324029cfd6bSTejun Heo 	ops->inherits = NULL;
6325029cfd6bSTejun Heo 
6326029cfd6bSTejun Heo 	spin_unlock(&lock);
6327029cfd6bSTejun Heo }
6328029cfd6bSTejun Heo 
6329029cfd6bSTejun Heo /**
6330ecef7253STejun Heo  *	ata_host_start - start and freeze ports of an ATA host
6331ecef7253STejun Heo  *	@host: ATA host to start ports for
6332ecef7253STejun Heo  *
6333ecef7253STejun Heo  *	Start and then freeze ports of @host.  Started status is
6334ecef7253STejun Heo  *	recorded in host->flags, so this function can be called
6335ecef7253STejun Heo  *	multiple times.  Ports are guaranteed to get started only
6336f3187195STejun Heo  *	once.  If host->ops isn't initialized yet, its set to the
6337f3187195STejun Heo  *	first non-dummy port ops.
6338ecef7253STejun Heo  *
6339ecef7253STejun Heo  *	LOCKING:
6340ecef7253STejun Heo  *	Inherited from calling layer (may sleep).
6341ecef7253STejun Heo  *
6342ecef7253STejun Heo  *	RETURNS:
6343ecef7253STejun Heo  *	0 if all ports are started successfully, -errno otherwise.
6344ecef7253STejun Heo  */
6345ecef7253STejun Heo int ata_host_start(struct ata_host *host)
6346ecef7253STejun Heo {
634732ebbc0cSTejun Heo 	int have_stop = 0;
634832ebbc0cSTejun Heo 	void *start_dr = NULL;
6349ecef7253STejun Heo 	int i, rc;
6350ecef7253STejun Heo 
6351ecef7253STejun Heo 	if (host->flags & ATA_HOST_STARTED)
6352ecef7253STejun Heo 		return 0;
6353ecef7253STejun Heo 
6354029cfd6bSTejun Heo 	ata_finalize_port_ops(host->ops);
6355029cfd6bSTejun Heo 
6356ecef7253STejun Heo 	for (i = 0; i < host->n_ports; i++) {
6357ecef7253STejun Heo 		struct ata_port *ap = host->ports[i];
6358ecef7253STejun Heo 
6359029cfd6bSTejun Heo 		ata_finalize_port_ops(ap->ops);
6360029cfd6bSTejun Heo 
6361f3187195STejun Heo 		if (!host->ops && !ata_port_is_dummy(ap))
6362f3187195STejun Heo 			host->ops = ap->ops;
6363f3187195STejun Heo 
636432ebbc0cSTejun Heo 		if (ap->ops->port_stop)
636532ebbc0cSTejun Heo 			have_stop = 1;
636632ebbc0cSTejun Heo 	}
636732ebbc0cSTejun Heo 
636832ebbc0cSTejun Heo 	if (host->ops->host_stop)
636932ebbc0cSTejun Heo 		have_stop = 1;
637032ebbc0cSTejun Heo 
637132ebbc0cSTejun Heo 	if (have_stop) {
637232ebbc0cSTejun Heo 		start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
637332ebbc0cSTejun Heo 		if (!start_dr)
637432ebbc0cSTejun Heo 			return -ENOMEM;
637532ebbc0cSTejun Heo 	}
637632ebbc0cSTejun Heo 
637732ebbc0cSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
637832ebbc0cSTejun Heo 		struct ata_port *ap = host->ports[i];
637932ebbc0cSTejun Heo 
6380ecef7253STejun Heo 		if (ap->ops->port_start) {
6381ecef7253STejun Heo 			rc = ap->ops->port_start(ap);
6382ecef7253STejun Heo 			if (rc) {
63830f9fe9b7SAlan Cox 				if (rc != -ENODEV)
6384a44fec1fSJoe Perches 					dev_err(host->dev,
6385a44fec1fSJoe Perches 						"failed to start port %d (errno=%d)\n",
6386a44fec1fSJoe Perches 						i, rc);
6387ecef7253STejun Heo 				goto err_out;
6388ecef7253STejun Heo 			}
6389ecef7253STejun Heo 		}
6390ecef7253STejun Heo 		ata_eh_freeze_port(ap);
6391ecef7253STejun Heo 	}
6392ecef7253STejun Heo 
639332ebbc0cSTejun Heo 	if (start_dr)
639432ebbc0cSTejun Heo 		devres_add(host->dev, start_dr);
6395ecef7253STejun Heo 	host->flags |= ATA_HOST_STARTED;
6396ecef7253STejun Heo 	return 0;
6397ecef7253STejun Heo 
6398ecef7253STejun Heo  err_out:
6399ecef7253STejun Heo 	while (--i >= 0) {
6400ecef7253STejun Heo 		struct ata_port *ap = host->ports[i];
6401ecef7253STejun Heo 
6402ecef7253STejun Heo 		if (ap->ops->port_stop)
6403ecef7253STejun Heo 			ap->ops->port_stop(ap);
6404ecef7253STejun Heo 	}
640532ebbc0cSTejun Heo 	devres_free(start_dr);
6406ecef7253STejun Heo 	return rc;
6407ecef7253STejun Heo }
6408ecef7253STejun Heo 
6409ecef7253STejun Heo /**
64108d8e7d13SDan Williams  *	ata_sas_host_init - Initialize a host struct for sas (ipr, libsas)
6411cca3974eSJeff Garzik  *	@host:	host to initialize
6412cca3974eSJeff Garzik  *	@dev:	device host is attached to
6413c6fd2807SJeff Garzik  *	@ops:	port_ops
6414c6fd2807SJeff Garzik  *
6415c6fd2807SJeff Garzik  */
6416cca3974eSJeff Garzik void ata_host_init(struct ata_host *host, struct device *dev,
64178d8e7d13SDan Williams 		   struct ata_port_operations *ops)
6418c6fd2807SJeff Garzik {
6419cca3974eSJeff Garzik 	spin_lock_init(&host->lock);
6420c0c362b6STejun Heo 	mutex_init(&host->eh_mutex);
642169278f79SJens Axboe 	host->n_tags = ATA_MAX_QUEUE;
6422cca3974eSJeff Garzik 	host->dev = dev;
6423cca3974eSJeff Garzik 	host->ops = ops;
64242fa4a326SJason Yan 	kref_init(&host->kref);
6425c6fd2807SJeff Garzik }
6426c6fd2807SJeff Garzik 
64279508a66fSDan Williams void __ata_port_probe(struct ata_port *ap)
642879318057SArjan van de Ven {
642979318057SArjan van de Ven 	struct ata_eh_info *ehi = &ap->link.eh_info;
643079318057SArjan van de Ven 	unsigned long flags;
643179318057SArjan van de Ven 
643279318057SArjan van de Ven 	/* kick EH for boot probing */
643379318057SArjan van de Ven 	spin_lock_irqsave(ap->lock, flags);
643479318057SArjan van de Ven 
643579318057SArjan van de Ven 	ehi->probe_mask |= ATA_ALL_DEVICES;
64366b7ae954STejun Heo 	ehi->action |= ATA_EH_RESET;
643779318057SArjan van de Ven 	ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
643879318057SArjan van de Ven 
643979318057SArjan van de Ven 	ap->pflags &= ~ATA_PFLAG_INITIALIZING;
644079318057SArjan van de Ven 	ap->pflags |= ATA_PFLAG_LOADING;
644179318057SArjan van de Ven 	ata_port_schedule_eh(ap);
644279318057SArjan van de Ven 
644379318057SArjan van de Ven 	spin_unlock_irqrestore(ap->lock, flags);
64449508a66fSDan Williams }
644579318057SArjan van de Ven 
64469508a66fSDan Williams int ata_port_probe(struct ata_port *ap)
64479508a66fSDan Williams {
64489508a66fSDan Williams 	int rc = 0;
64499508a66fSDan Williams 
64509508a66fSDan Williams 	if (ap->ops->error_handler) {
64519508a66fSDan Williams 		__ata_port_probe(ap);
645279318057SArjan van de Ven 		ata_port_wait_eh(ap);
645379318057SArjan van de Ven 	} else {
645479318057SArjan van de Ven 		DPRINTK("ata%u: bus probe begin\n", ap->print_id);
645579318057SArjan van de Ven 		rc = ata_bus_probe(ap);
645679318057SArjan van de Ven 		DPRINTK("ata%u: bus probe end\n", ap->print_id);
6457238c9cf9SJames Bottomley 	}
6458238c9cf9SJames Bottomley 	return rc;
6459238c9cf9SJames Bottomley }
646079318057SArjan van de Ven 
6461238c9cf9SJames Bottomley 
6462238c9cf9SJames Bottomley static void async_port_probe(void *data, async_cookie_t cookie)
6463238c9cf9SJames Bottomley {
6464238c9cf9SJames Bottomley 	struct ata_port *ap = data;
6465238c9cf9SJames Bottomley 
6466238c9cf9SJames Bottomley 	/*
6467238c9cf9SJames Bottomley 	 * If we're not allowed to scan this host in parallel,
6468238c9cf9SJames Bottomley 	 * we need to wait until all previous scans have completed
6469238c9cf9SJames Bottomley 	 * before going further.
6470238c9cf9SJames Bottomley 	 * Jeff Garzik says this is only within a controller, so we
6471238c9cf9SJames Bottomley 	 * don't need to wait for port 0, only for later ports.
647279318057SArjan van de Ven 	 */
6473238c9cf9SJames Bottomley 	if (!(ap->host->flags & ATA_HOST_PARALLEL_SCAN) && ap->port_no != 0)
6474238c9cf9SJames Bottomley 		async_synchronize_cookie(cookie);
6475238c9cf9SJames Bottomley 
6476238c9cf9SJames Bottomley 	(void)ata_port_probe(ap);
6477f29d3b23SArjan van de Ven 
6478f29d3b23SArjan van de Ven 	/* in order to keep device order, we need to synchronize at this point */
6479f29d3b23SArjan van de Ven 	async_synchronize_cookie(cookie);
6480f29d3b23SArjan van de Ven 
6481f29d3b23SArjan van de Ven 	ata_scsi_scan_host(ap, 1);
648279318057SArjan van de Ven }
6483238c9cf9SJames Bottomley 
6484c6fd2807SJeff Garzik /**
6485f3187195STejun Heo  *	ata_host_register - register initialized ATA host
6486f3187195STejun Heo  *	@host: ATA host to register
6487f3187195STejun Heo  *	@sht: template for SCSI host
6488c6fd2807SJeff Garzik  *
6489f3187195STejun Heo  *	Register initialized ATA host.  @host is allocated using
6490f3187195STejun Heo  *	ata_host_alloc() and fully initialized by LLD.  This function
6491f3187195STejun Heo  *	starts ports, registers @host with ATA and SCSI layers and
6492f3187195STejun Heo  *	probe registered devices.
6493c6fd2807SJeff Garzik  *
6494c6fd2807SJeff Garzik  *	LOCKING:
6495f3187195STejun Heo  *	Inherited from calling layer (may sleep).
6496c6fd2807SJeff Garzik  *
6497c6fd2807SJeff Garzik  *	RETURNS:
6498f3187195STejun Heo  *	0 on success, -errno otherwise.
6499c6fd2807SJeff Garzik  */
6500f3187195STejun Heo int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
6501c6fd2807SJeff Garzik {
6502f3187195STejun Heo 	int i, rc;
6503c6fd2807SJeff Garzik 
650469278f79SJens Axboe 	host->n_tags = clamp(sht->can_queue, 1, ATA_MAX_QUEUE);
65051871ee13SKevin Hao 
6506f3187195STejun Heo 	/* host must have been started */
6507f3187195STejun Heo 	if (!(host->flags & ATA_HOST_STARTED)) {
6508a44fec1fSJoe Perches 		dev_err(host->dev, "BUG: trying to register unstarted host\n");
6509f3187195STejun Heo 		WARN_ON(1);
6510f3187195STejun Heo 		return -EINVAL;
651102f076aaSAlan Cox 	}
6512f0d36efdSTejun Heo 
6513f3187195STejun Heo 	/* Blow away unused ports.  This happens when LLD can't
6514f3187195STejun Heo 	 * determine the exact number of ports to allocate at
6515f3187195STejun Heo 	 * allocation time.
6516f3187195STejun Heo 	 */
6517f3187195STejun Heo 	for (i = host->n_ports; host->ports[i]; i++)
6518f3187195STejun Heo 		kfree(host->ports[i]);
6519f0d36efdSTejun Heo 
6520f3187195STejun Heo 	/* give ports names and add SCSI hosts */
6521e628dc99SDavid Milburn 	for (i = 0; i < host->n_ports; i++) {
652285d6725bSDan Williams 		host->ports[i]->print_id = atomic_inc_return(&ata_print_id);
6523e628dc99SDavid Milburn 		host->ports[i]->local_port_no = i + 1;
6524e628dc99SDavid Milburn 	}
6525d9027470SGwendal Grignou 
6526d9027470SGwendal Grignou 	/* Create associated sysfs transport objects  */
6527d9027470SGwendal Grignou 	for (i = 0; i < host->n_ports; i++) {
6528d9027470SGwendal Grignou 		rc = ata_tport_add(host->dev,host->ports[i]);
6529d9027470SGwendal Grignou 		if (rc) {
6530d9027470SGwendal Grignou 			goto err_tadd;
6531d9027470SGwendal Grignou 		}
6532d9027470SGwendal Grignou 	}
6533d9027470SGwendal Grignou 
6534f3187195STejun Heo 	rc = ata_scsi_add_hosts(host, sht);
6535ecef7253STejun Heo 	if (rc)
6536d9027470SGwendal Grignou 		goto err_tadd;
6537ecef7253STejun Heo 
6538f3187195STejun Heo 	/* set cable, sata_spd_limit and report */
6539cca3974eSJeff Garzik 	for (i = 0; i < host->n_ports; i++) {
6540cca3974eSJeff Garzik 		struct ata_port *ap = host->ports[i];
6541f3187195STejun Heo 		unsigned long xfer_mask;
6542f3187195STejun Heo 
6543f3187195STejun Heo 		/* set SATA cable type if still unset */
6544f3187195STejun Heo 		if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
6545f3187195STejun Heo 			ap->cbl = ATA_CBL_SATA;
6546c6fd2807SJeff Garzik 
6547c6fd2807SJeff Garzik 		/* init sata_spd_limit to the current value */
65484fb37a25STejun Heo 		sata_link_init_spd(&ap->link);
6549b1c72916STejun Heo 		if (ap->slave_link)
6550b1c72916STejun Heo 			sata_link_init_spd(ap->slave_link);
6551c6fd2807SJeff Garzik 
6552cbcdd875STejun Heo 		/* print per-port info to dmesg */
6553f3187195STejun Heo 		xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
6554f3187195STejun Heo 					      ap->udma_mask);
6555f3187195STejun Heo 
6556abf6e8edSTejun Heo 		if (!ata_port_is_dummy(ap)) {
6557a9a79dfeSJoe Perches 			ata_port_info(ap, "%cATA max %s %s\n",
6558a16abc0bSTejun Heo 				      (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
6559f3187195STejun Heo 				      ata_mode_string(xfer_mask),
6560cbcdd875STejun Heo 				      ap->link.eh_info.desc);
6561abf6e8edSTejun Heo 			ata_ehi_clear_desc(&ap->link.eh_info);
6562abf6e8edSTejun Heo 		} else
6563a9a79dfeSJoe Perches 			ata_port_info(ap, "DUMMY\n");
6564c6fd2807SJeff Garzik 	}
6565c6fd2807SJeff Garzik 
6566f6005354SVegard Nossum 	/* perform each probe asynchronously */
6567f3187195STejun Heo 	for (i = 0; i < host->n_ports; i++) {
6568f3187195STejun Heo 		struct ata_port *ap = host->ports[i];
656979318057SArjan van de Ven 		async_schedule(async_port_probe, ap);
6570c6fd2807SJeff Garzik 	}
6571c6fd2807SJeff Garzik 
6572f3187195STejun Heo 	return 0;
6573d9027470SGwendal Grignou 
6574d9027470SGwendal Grignou  err_tadd:
6575d9027470SGwendal Grignou 	while (--i >= 0) {
6576d9027470SGwendal Grignou 		ata_tport_delete(host->ports[i]);
6577d9027470SGwendal Grignou 	}
6578d9027470SGwendal Grignou 	return rc;
6579d9027470SGwendal Grignou 
6580f3187195STejun Heo }
6581f3187195STejun Heo 
6582f3187195STejun Heo /**
6583f5cda257STejun Heo  *	ata_host_activate - start host, request IRQ and register it
6584f5cda257STejun Heo  *	@host: target ATA host
6585f5cda257STejun Heo  *	@irq: IRQ to request
6586f5cda257STejun Heo  *	@irq_handler: irq_handler used when requesting IRQ
6587f5cda257STejun Heo  *	@irq_flags: irq_flags used when requesting IRQ
6588f5cda257STejun Heo  *	@sht: scsi_host_template to use when registering the host
6589f5cda257STejun Heo  *
6590f5cda257STejun Heo  *	After allocating an ATA host and initializing it, most libata
6591f5cda257STejun Heo  *	LLDs perform three steps to activate the host - start host,
6592c9b5560aSMasanari Iida  *	request IRQ and register it.  This helper takes necessary
6593f5cda257STejun Heo  *	arguments and performs the three steps in one go.
6594f5cda257STejun Heo  *
65953d46b2e2SPaul Mundt  *	An invalid IRQ skips the IRQ registration and expects the host to
65963d46b2e2SPaul Mundt  *	have set polling mode on the port. In this case, @irq_handler
65973d46b2e2SPaul Mundt  *	should be NULL.
65983d46b2e2SPaul Mundt  *
6599f5cda257STejun Heo  *	LOCKING:
6600f5cda257STejun Heo  *	Inherited from calling layer (may sleep).
6601f5cda257STejun Heo  *
6602f5cda257STejun Heo  *	RETURNS:
6603f5cda257STejun Heo  *	0 on success, -errno otherwise.
6604f5cda257STejun Heo  */
6605f5cda257STejun Heo int ata_host_activate(struct ata_host *host, int irq,
6606f5cda257STejun Heo 		      irq_handler_t irq_handler, unsigned long irq_flags,
6607f5cda257STejun Heo 		      struct scsi_host_template *sht)
6608f5cda257STejun Heo {
6609cbcdd875STejun Heo 	int i, rc;
66107e22c002SHeiner Kallweit 	char *irq_desc;
6611f5cda257STejun Heo 
6612f5cda257STejun Heo 	rc = ata_host_start(host);
6613f5cda257STejun Heo 	if (rc)
6614f5cda257STejun Heo 		return rc;
6615f5cda257STejun Heo 
66163d46b2e2SPaul Mundt 	/* Special case for polling mode */
66173d46b2e2SPaul Mundt 	if (!irq) {
66183d46b2e2SPaul Mundt 		WARN_ON(irq_handler);
66193d46b2e2SPaul Mundt 		return ata_host_register(host, sht);
66203d46b2e2SPaul Mundt 	}
66213d46b2e2SPaul Mundt 
66227e22c002SHeiner Kallweit 	irq_desc = devm_kasprintf(host->dev, GFP_KERNEL, "%s[%s]",
66237e22c002SHeiner Kallweit 				  dev_driver_string(host->dev),
66247e22c002SHeiner Kallweit 				  dev_name(host->dev));
66257e22c002SHeiner Kallweit 	if (!irq_desc)
66267e22c002SHeiner Kallweit 		return -ENOMEM;
66277e22c002SHeiner Kallweit 
6628f5cda257STejun Heo 	rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
66297e22c002SHeiner Kallweit 			      irq_desc, host);
6630f5cda257STejun Heo 	if (rc)
6631f5cda257STejun Heo 		return rc;
6632f5cda257STejun Heo 
6633cbcdd875STejun Heo 	for (i = 0; i < host->n_ports; i++)
6634cbcdd875STejun Heo 		ata_port_desc(host->ports[i], "irq %d", irq);
66354031826bSTejun Heo 
6636f5cda257STejun Heo 	rc = ata_host_register(host, sht);
6637f5cda257STejun Heo 	/* if failed, just free the IRQ and leave ports alone */
6638f5cda257STejun Heo 	if (rc)
6639f5cda257STejun Heo 		devm_free_irq(host->dev, irq, host);
6640f5cda257STejun Heo 
6641f5cda257STejun Heo 	return rc;
6642f5cda257STejun Heo }
6643f5cda257STejun Heo 
6644f5cda257STejun Heo /**
6645c9b5560aSMasanari Iida  *	ata_port_detach - Detach ATA port in preparation of device removal
6646c6fd2807SJeff Garzik  *	@ap: ATA port to be detached
6647c6fd2807SJeff Garzik  *
6648c6fd2807SJeff Garzik  *	Detach all ATA devices and the associated SCSI devices of @ap;
6649c6fd2807SJeff Garzik  *	then, remove the associated SCSI host.  @ap is guaranteed to
6650c6fd2807SJeff Garzik  *	be quiescent on return from this function.
6651c6fd2807SJeff Garzik  *
6652c6fd2807SJeff Garzik  *	LOCKING:
6653c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
6654c6fd2807SJeff Garzik  */
6655741b7763SAdrian Bunk static void ata_port_detach(struct ata_port *ap)
6656c6fd2807SJeff Garzik {
6657c6fd2807SJeff Garzik 	unsigned long flags;
6658a6f9bf4dSLevente Kurusa 	struct ata_link *link;
6659a6f9bf4dSLevente Kurusa 	struct ata_device *dev;
6660c6fd2807SJeff Garzik 
6661c6fd2807SJeff Garzik 	if (!ap->ops->error_handler)
6662c6fd2807SJeff Garzik 		goto skip_eh;
6663c6fd2807SJeff Garzik 
6664c6fd2807SJeff Garzik 	/* tell EH we're leaving & flush EH */
6665c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
6666c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_UNLOADING;
6667ece180d1STejun Heo 	ata_port_schedule_eh(ap);
6668c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
6669c6fd2807SJeff Garzik 
6670ece180d1STejun Heo 	/* wait till EH commits suicide */
6671c6fd2807SJeff Garzik 	ata_port_wait_eh(ap);
6672c6fd2807SJeff Garzik 
6673ece180d1STejun Heo 	/* it better be dead now */
6674ece180d1STejun Heo 	WARN_ON(!(ap->pflags & ATA_PFLAG_UNLOADED));
6675c6fd2807SJeff Garzik 
6676afe2c511STejun Heo 	cancel_delayed_work_sync(&ap->hotplug_task);
6677c6fd2807SJeff Garzik 
6678c6fd2807SJeff Garzik  skip_eh:
6679a6f9bf4dSLevente Kurusa 	/* clean up zpodd on port removal */
6680a6f9bf4dSLevente Kurusa 	ata_for_each_link(link, ap, HOST_FIRST) {
6681a6f9bf4dSLevente Kurusa 		ata_for_each_dev(dev, link, ALL) {
6682a6f9bf4dSLevente Kurusa 			if (zpodd_dev_enabled(dev))
6683a6f9bf4dSLevente Kurusa 				zpodd_exit(dev);
6684a6f9bf4dSLevente Kurusa 		}
6685a6f9bf4dSLevente Kurusa 	}
6686d9027470SGwendal Grignou 	if (ap->pmp_link) {
6687d9027470SGwendal Grignou 		int i;
6688d9027470SGwendal Grignou 		for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
6689d9027470SGwendal Grignou 			ata_tlink_delete(&ap->pmp_link[i]);
6690d9027470SGwendal Grignou 	}
6691c6fd2807SJeff Garzik 	/* remove the associated SCSI host */
6692cca3974eSJeff Garzik 	scsi_remove_host(ap->scsi_host);
6693c5700766SRafael J. Wysocki 	ata_tport_delete(ap);
6694c6fd2807SJeff Garzik }
6695c6fd2807SJeff Garzik 
6696c6fd2807SJeff Garzik /**
66970529c159STejun Heo  *	ata_host_detach - Detach all ports of an ATA host
66980529c159STejun Heo  *	@host: Host to detach
66990529c159STejun Heo  *
67000529c159STejun Heo  *	Detach all ports of @host.
67010529c159STejun Heo  *
67020529c159STejun Heo  *	LOCKING:
67030529c159STejun Heo  *	Kernel thread context (may sleep).
67040529c159STejun Heo  */
67050529c159STejun Heo void ata_host_detach(struct ata_host *host)
67060529c159STejun Heo {
67070529c159STejun Heo 	int i;
67080529c159STejun Heo 
67090529c159STejun Heo 	for (i = 0; i < host->n_ports; i++)
67100529c159STejun Heo 		ata_port_detach(host->ports[i]);
6711562f0c2dSTejun Heo 
6712562f0c2dSTejun Heo 	/* the host is dead now, dissociate ACPI */
6713562f0c2dSTejun Heo 	ata_acpi_dissociate(host);
67140529c159STejun Heo }
67150529c159STejun Heo 
6716c6fd2807SJeff Garzik #ifdef CONFIG_PCI
6717c6fd2807SJeff Garzik 
6718c6fd2807SJeff Garzik /**
6719c6fd2807SJeff Garzik  *	ata_pci_remove_one - PCI layer callback for device removal
6720c6fd2807SJeff Garzik  *	@pdev: PCI device that was removed
6721c6fd2807SJeff Garzik  *
6722b878ca5dSTejun Heo  *	PCI layer indicates to libata via this hook that hot-unplug or
6723b878ca5dSTejun Heo  *	module unload event has occurred.  Detach all ports.  Resource
6724b878ca5dSTejun Heo  *	release is handled via devres.
6725c6fd2807SJeff Garzik  *
6726c6fd2807SJeff Garzik  *	LOCKING:
6727c6fd2807SJeff Garzik  *	Inherited from PCI layer (may sleep).
6728c6fd2807SJeff Garzik  */
6729c6fd2807SJeff Garzik void ata_pci_remove_one(struct pci_dev *pdev)
6730c6fd2807SJeff Garzik {
673104a3f5b7SBrian Norris 	struct ata_host *host = pci_get_drvdata(pdev);
6732c6fd2807SJeff Garzik 
6733f0d36efdSTejun Heo 	ata_host_detach(host);
6734c6fd2807SJeff Garzik }
6735c6fd2807SJeff Garzik 
6736c6fd2807SJeff Garzik /* move to PCI subsystem */
6737c6fd2807SJeff Garzik int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
6738c6fd2807SJeff Garzik {
6739c6fd2807SJeff Garzik 	unsigned long tmp = 0;
6740c6fd2807SJeff Garzik 
6741c6fd2807SJeff Garzik 	switch (bits->width) {
6742c6fd2807SJeff Garzik 	case 1: {
6743c6fd2807SJeff Garzik 		u8 tmp8 = 0;
6744c6fd2807SJeff Garzik 		pci_read_config_byte(pdev, bits->reg, &tmp8);
6745c6fd2807SJeff Garzik 		tmp = tmp8;
6746c6fd2807SJeff Garzik 		break;
6747c6fd2807SJeff Garzik 	}
6748c6fd2807SJeff Garzik 	case 2: {
6749c6fd2807SJeff Garzik 		u16 tmp16 = 0;
6750c6fd2807SJeff Garzik 		pci_read_config_word(pdev, bits->reg, &tmp16);
6751c6fd2807SJeff Garzik 		tmp = tmp16;
6752c6fd2807SJeff Garzik 		break;
6753c6fd2807SJeff Garzik 	}
6754c6fd2807SJeff Garzik 	case 4: {
6755c6fd2807SJeff Garzik 		u32 tmp32 = 0;
6756c6fd2807SJeff Garzik 		pci_read_config_dword(pdev, bits->reg, &tmp32);
6757c6fd2807SJeff Garzik 		tmp = tmp32;
6758c6fd2807SJeff Garzik 		break;
6759c6fd2807SJeff Garzik 	}
6760c6fd2807SJeff Garzik 
6761c6fd2807SJeff Garzik 	default:
6762c6fd2807SJeff Garzik 		return -EINVAL;
6763c6fd2807SJeff Garzik 	}
6764c6fd2807SJeff Garzik 
6765c6fd2807SJeff Garzik 	tmp &= bits->mask;
6766c6fd2807SJeff Garzik 
6767c6fd2807SJeff Garzik 	return (tmp == bits->val) ? 1 : 0;
6768c6fd2807SJeff Garzik }
6769c6fd2807SJeff Garzik 
67706ffa01d8STejun Heo #ifdef CONFIG_PM
6771c6fd2807SJeff Garzik void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
6772c6fd2807SJeff Garzik {
6773c6fd2807SJeff Garzik 	pci_save_state(pdev);
6774c6fd2807SJeff Garzik 	pci_disable_device(pdev);
67754c90d971STejun Heo 
67763a2d5b70SRafael J. Wysocki 	if (mesg.event & PM_EVENT_SLEEP)
6777c6fd2807SJeff Garzik 		pci_set_power_state(pdev, PCI_D3hot);
6778c6fd2807SJeff Garzik }
6779c6fd2807SJeff Garzik 
6780553c4aa6STejun Heo int ata_pci_device_do_resume(struct pci_dev *pdev)
6781c6fd2807SJeff Garzik {
6782553c4aa6STejun Heo 	int rc;
6783553c4aa6STejun Heo 
6784c6fd2807SJeff Garzik 	pci_set_power_state(pdev, PCI_D0);
6785c6fd2807SJeff Garzik 	pci_restore_state(pdev);
6786553c4aa6STejun Heo 
6787f0d36efdSTejun Heo 	rc = pcim_enable_device(pdev);
6788553c4aa6STejun Heo 	if (rc) {
6789a44fec1fSJoe Perches 		dev_err(&pdev->dev,
6790553c4aa6STejun Heo 			"failed to enable device after resume (%d)\n", rc);
6791553c4aa6STejun Heo 		return rc;
6792553c4aa6STejun Heo 	}
6793553c4aa6STejun Heo 
6794c6fd2807SJeff Garzik 	pci_set_master(pdev);
6795553c4aa6STejun Heo 	return 0;
6796c6fd2807SJeff Garzik }
6797c6fd2807SJeff Garzik 
6798c6fd2807SJeff Garzik int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
6799c6fd2807SJeff Garzik {
680004a3f5b7SBrian Norris 	struct ata_host *host = pci_get_drvdata(pdev);
6801c6fd2807SJeff Garzik 	int rc = 0;
6802c6fd2807SJeff Garzik 
6803cca3974eSJeff Garzik 	rc = ata_host_suspend(host, mesg);
6804c6fd2807SJeff Garzik 	if (rc)
6805c6fd2807SJeff Garzik 		return rc;
6806c6fd2807SJeff Garzik 
6807c6fd2807SJeff Garzik 	ata_pci_device_do_suspend(pdev, mesg);
6808c6fd2807SJeff Garzik 
6809c6fd2807SJeff Garzik 	return 0;
6810c6fd2807SJeff Garzik }
6811c6fd2807SJeff Garzik 
6812c6fd2807SJeff Garzik int ata_pci_device_resume(struct pci_dev *pdev)
6813c6fd2807SJeff Garzik {
681404a3f5b7SBrian Norris 	struct ata_host *host = pci_get_drvdata(pdev);
6815553c4aa6STejun Heo 	int rc;
6816c6fd2807SJeff Garzik 
6817553c4aa6STejun Heo 	rc = ata_pci_device_do_resume(pdev);
6818553c4aa6STejun Heo 	if (rc == 0)
6819cca3974eSJeff Garzik 		ata_host_resume(host);
6820553c4aa6STejun Heo 	return rc;
6821c6fd2807SJeff Garzik }
68226ffa01d8STejun Heo #endif /* CONFIG_PM */
68236ffa01d8STejun Heo 
6824c6fd2807SJeff Garzik #endif /* CONFIG_PCI */
6825c6fd2807SJeff Garzik 
6826b7db04d9SBrian Norris /**
6827b7db04d9SBrian Norris  *	ata_platform_remove_one - Platform layer callback for device removal
6828b7db04d9SBrian Norris  *	@pdev: Platform device that was removed
6829b7db04d9SBrian Norris  *
6830b7db04d9SBrian Norris  *	Platform layer indicates to libata via this hook that hot-unplug or
6831b7db04d9SBrian Norris  *	module unload event has occurred.  Detach all ports.  Resource
6832b7db04d9SBrian Norris  *	release is handled via devres.
6833b7db04d9SBrian Norris  *
6834b7db04d9SBrian Norris  *	LOCKING:
6835b7db04d9SBrian Norris  *	Inherited from platform layer (may sleep).
6836b7db04d9SBrian Norris  */
6837b7db04d9SBrian Norris int ata_platform_remove_one(struct platform_device *pdev)
6838b7db04d9SBrian Norris {
6839b7db04d9SBrian Norris 	struct ata_host *host = platform_get_drvdata(pdev);
6840b7db04d9SBrian Norris 
6841b7db04d9SBrian Norris 	ata_host_detach(host);
6842b7db04d9SBrian Norris 
6843b7db04d9SBrian Norris 	return 0;
6844b7db04d9SBrian Norris }
6845b7db04d9SBrian Norris 
684633267325STejun Heo static int __init ata_parse_force_one(char **cur,
684733267325STejun Heo 				      struct ata_force_ent *force_ent,
684833267325STejun Heo 				      const char **reason)
684933267325STejun Heo {
68500f5f264bSRasmus Villemoes 	static const struct ata_force_param force_tbl[] __initconst = {
685133267325STejun Heo 		{ "40c",	.cbl		= ATA_CBL_PATA40 },
685233267325STejun Heo 		{ "80c",	.cbl		= ATA_CBL_PATA80 },
685333267325STejun Heo 		{ "short40c",	.cbl		= ATA_CBL_PATA40_SHORT },
685433267325STejun Heo 		{ "unk",	.cbl		= ATA_CBL_PATA_UNK },
685533267325STejun Heo 		{ "ign",	.cbl		= ATA_CBL_PATA_IGN },
685633267325STejun Heo 		{ "sata",	.cbl		= ATA_CBL_SATA },
685733267325STejun Heo 		{ "1.5Gbps",	.spd_limit	= 1 },
685833267325STejun Heo 		{ "3.0Gbps",	.spd_limit	= 2 },
685933267325STejun Heo 		{ "noncq",	.horkage_on	= ATA_HORKAGE_NONCQ },
686033267325STejun Heo 		{ "ncq",	.horkage_off	= ATA_HORKAGE_NONCQ },
6861d7b16e4fSMartin K. Petersen 		{ "noncqtrim",	.horkage_on	= ATA_HORKAGE_NO_NCQ_TRIM },
6862d7b16e4fSMartin K. Petersen 		{ "ncqtrim",	.horkage_off	= ATA_HORKAGE_NO_NCQ_TRIM },
686343c9c591STejun Heo 		{ "dump_id",	.horkage_on	= ATA_HORKAGE_DUMP_ID },
686433267325STejun Heo 		{ "pio0",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 0) },
686533267325STejun Heo 		{ "pio1",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 1) },
686633267325STejun Heo 		{ "pio2",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 2) },
686733267325STejun Heo 		{ "pio3",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 3) },
686833267325STejun Heo 		{ "pio4",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 4) },
686933267325STejun Heo 		{ "pio5",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 5) },
687033267325STejun Heo 		{ "pio6",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 6) },
687133267325STejun Heo 		{ "mwdma0",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 0) },
687233267325STejun Heo 		{ "mwdma1",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 1) },
687333267325STejun Heo 		{ "mwdma2",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 2) },
687433267325STejun Heo 		{ "mwdma3",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 3) },
687533267325STejun Heo 		{ "mwdma4",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 4) },
687633267325STejun Heo 		{ "udma0",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
687733267325STejun Heo 		{ "udma16",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
687833267325STejun Heo 		{ "udma/16",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
687933267325STejun Heo 		{ "udma1",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
688033267325STejun Heo 		{ "udma25",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
688133267325STejun Heo 		{ "udma/25",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
688233267325STejun Heo 		{ "udma2",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
688333267325STejun Heo 		{ "udma33",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
688433267325STejun Heo 		{ "udma/33",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
688533267325STejun Heo 		{ "udma3",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
688633267325STejun Heo 		{ "udma44",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
688733267325STejun Heo 		{ "udma/44",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
688833267325STejun Heo 		{ "udma4",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
688933267325STejun Heo 		{ "udma66",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
689033267325STejun Heo 		{ "udma/66",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
689133267325STejun Heo 		{ "udma5",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
689233267325STejun Heo 		{ "udma100",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
689333267325STejun Heo 		{ "udma/100",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
689433267325STejun Heo 		{ "udma6",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
689533267325STejun Heo 		{ "udma133",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
689633267325STejun Heo 		{ "udma/133",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
689733267325STejun Heo 		{ "udma7",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 7) },
689805944bdfSTejun Heo 		{ "nohrst",	.lflags		= ATA_LFLAG_NO_HRST },
689905944bdfSTejun Heo 		{ "nosrst",	.lflags		= ATA_LFLAG_NO_SRST },
690005944bdfSTejun Heo 		{ "norst",	.lflags		= ATA_LFLAG_NO_HRST | ATA_LFLAG_NO_SRST },
6901ca6d43b0SDan Williams 		{ "rstonce",	.lflags		= ATA_LFLAG_RST_ONCE },
6902966fbe19SVincent Pelletier 		{ "atapi_dmadir", .horkage_on	= ATA_HORKAGE_ATAPI_DMADIR },
6903b8bd6dc3SRobin H. Johnson 		{ "disable",	.horkage_on	= ATA_HORKAGE_DISABLE },
690433267325STejun Heo 	};
690533267325STejun Heo 	char *start = *cur, *p = *cur;
690633267325STejun Heo 	char *id, *val, *endp;
690733267325STejun Heo 	const struct ata_force_param *match_fp = NULL;
690833267325STejun Heo 	int nr_matches = 0, i;
690933267325STejun Heo 
691033267325STejun Heo 	/* find where this param ends and update *cur */
691133267325STejun Heo 	while (*p != '\0' && *p != ',')
691233267325STejun Heo 		p++;
691333267325STejun Heo 
691433267325STejun Heo 	if (*p == '\0')
691533267325STejun Heo 		*cur = p;
691633267325STejun Heo 	else
691733267325STejun Heo 		*cur = p + 1;
691833267325STejun Heo 
691933267325STejun Heo 	*p = '\0';
692033267325STejun Heo 
692133267325STejun Heo 	/* parse */
692233267325STejun Heo 	p = strchr(start, ':');
692333267325STejun Heo 	if (!p) {
692433267325STejun Heo 		val = strstrip(start);
692533267325STejun Heo 		goto parse_val;
692633267325STejun Heo 	}
692733267325STejun Heo 	*p = '\0';
692833267325STejun Heo 
692933267325STejun Heo 	id = strstrip(start);
693033267325STejun Heo 	val = strstrip(p + 1);
693133267325STejun Heo 
693233267325STejun Heo 	/* parse id */
693333267325STejun Heo 	p = strchr(id, '.');
693433267325STejun Heo 	if (p) {
693533267325STejun Heo 		*p++ = '\0';
693633267325STejun Heo 		force_ent->device = simple_strtoul(p, &endp, 10);
693733267325STejun Heo 		if (p == endp || *endp != '\0') {
693833267325STejun Heo 			*reason = "invalid device";
693933267325STejun Heo 			return -EINVAL;
694033267325STejun Heo 		}
694133267325STejun Heo 	}
694233267325STejun Heo 
694333267325STejun Heo 	force_ent->port = simple_strtoul(id, &endp, 10);
6944f7cf69aeSTejun Heo 	if (id == endp || *endp != '\0') {
694533267325STejun Heo 		*reason = "invalid port/link";
694633267325STejun Heo 		return -EINVAL;
694733267325STejun Heo 	}
694833267325STejun Heo 
694933267325STejun Heo  parse_val:
695033267325STejun Heo 	/* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */
695133267325STejun Heo 	for (i = 0; i < ARRAY_SIZE(force_tbl); i++) {
695233267325STejun Heo 		const struct ata_force_param *fp = &force_tbl[i];
695333267325STejun Heo 
695433267325STejun Heo 		if (strncasecmp(val, fp->name, strlen(val)))
695533267325STejun Heo 			continue;
695633267325STejun Heo 
695733267325STejun Heo 		nr_matches++;
695833267325STejun Heo 		match_fp = fp;
695933267325STejun Heo 
696033267325STejun Heo 		if (strcasecmp(val, fp->name) == 0) {
696133267325STejun Heo 			nr_matches = 1;
696233267325STejun Heo 			break;
696333267325STejun Heo 		}
696433267325STejun Heo 	}
696533267325STejun Heo 
696633267325STejun Heo 	if (!nr_matches) {
696733267325STejun Heo 		*reason = "unknown value";
696833267325STejun Heo 		return -EINVAL;
696933267325STejun Heo 	}
697033267325STejun Heo 	if (nr_matches > 1) {
69719de55351SArvind Yadav 		*reason = "ambiguous value";
697233267325STejun Heo 		return -EINVAL;
697333267325STejun Heo 	}
697433267325STejun Heo 
697533267325STejun Heo 	force_ent->param = *match_fp;
697633267325STejun Heo 
697733267325STejun Heo 	return 0;
697833267325STejun Heo }
697933267325STejun Heo 
698033267325STejun Heo static void __init ata_parse_force_param(void)
698133267325STejun Heo {
698233267325STejun Heo 	int idx = 0, size = 1;
698333267325STejun Heo 	int last_port = -1, last_device = -1;
698433267325STejun Heo 	char *p, *cur, *next;
698533267325STejun Heo 
698633267325STejun Heo 	/* calculate maximum number of params and allocate force_tbl */
698733267325STejun Heo 	for (p = ata_force_param_buf; *p; p++)
698833267325STejun Heo 		if (*p == ',')
698933267325STejun Heo 			size++;
699033267325STejun Heo 
69916396bb22SKees Cook 	ata_force_tbl = kcalloc(size, sizeof(ata_force_tbl[0]), GFP_KERNEL);
699233267325STejun Heo 	if (!ata_force_tbl) {
699333267325STejun Heo 		printk(KERN_WARNING "ata: failed to extend force table, "
699433267325STejun Heo 		       "libata.force ignored\n");
699533267325STejun Heo 		return;
699633267325STejun Heo 	}
699733267325STejun Heo 
699833267325STejun Heo 	/* parse and populate the table */
699933267325STejun Heo 	for (cur = ata_force_param_buf; *cur != '\0'; cur = next) {
700033267325STejun Heo 		const char *reason = "";
700133267325STejun Heo 		struct ata_force_ent te = { .port = -1, .device = -1 };
700233267325STejun Heo 
700333267325STejun Heo 		next = cur;
700433267325STejun Heo 		if (ata_parse_force_one(&next, &te, &reason)) {
700533267325STejun Heo 			printk(KERN_WARNING "ata: failed to parse force "
700633267325STejun Heo 			       "parameter \"%s\" (%s)\n",
700733267325STejun Heo 			       cur, reason);
700833267325STejun Heo 			continue;
700933267325STejun Heo 		}
701033267325STejun Heo 
701133267325STejun Heo 		if (te.port == -1) {
701233267325STejun Heo 			te.port = last_port;
701333267325STejun Heo 			te.device = last_device;
701433267325STejun Heo 		}
701533267325STejun Heo 
701633267325STejun Heo 		ata_force_tbl[idx++] = te;
701733267325STejun Heo 
701833267325STejun Heo 		last_port = te.port;
701933267325STejun Heo 		last_device = te.device;
702033267325STejun Heo 	}
702133267325STejun Heo 
702233267325STejun Heo 	ata_force_tbl_size = idx;
702333267325STejun Heo }
7024c6fd2807SJeff Garzik 
7025c6fd2807SJeff Garzik static int __init ata_init(void)
7026c6fd2807SJeff Garzik {
7027d9027470SGwendal Grignou 	int rc;
7028270390e1STejun Heo 
702933267325STejun Heo 	ata_parse_force_param();
703033267325STejun Heo 
7031270390e1STejun Heo 	rc = ata_sff_init();
7032ad72cf98STejun Heo 	if (rc) {
7033ad72cf98STejun Heo 		kfree(ata_force_tbl);
7034ad72cf98STejun Heo 		return rc;
7035ad72cf98STejun Heo 	}
7036c6fd2807SJeff Garzik 
7037d9027470SGwendal Grignou 	libata_transport_init();
7038d9027470SGwendal Grignou 	ata_scsi_transport_template = ata_attach_transport();
7039d9027470SGwendal Grignou 	if (!ata_scsi_transport_template) {
7040d9027470SGwendal Grignou 		ata_sff_exit();
7041d9027470SGwendal Grignou 		rc = -ENOMEM;
7042d9027470SGwendal Grignou 		goto err_out;
7043d9027470SGwendal Grignou 	}
7044d9027470SGwendal Grignou 
7045c6fd2807SJeff Garzik 	printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
7046c6fd2807SJeff Garzik 	return 0;
7047d9027470SGwendal Grignou 
7048d9027470SGwendal Grignou err_out:
7049d9027470SGwendal Grignou 	return rc;
7050c6fd2807SJeff Garzik }
7051c6fd2807SJeff Garzik 
7052c6fd2807SJeff Garzik static void __exit ata_exit(void)
7053c6fd2807SJeff Garzik {
7054d9027470SGwendal Grignou 	ata_release_transport(ata_scsi_transport_template);
7055d9027470SGwendal Grignou 	libata_transport_exit();
7056270390e1STejun Heo 	ata_sff_exit();
705733267325STejun Heo 	kfree(ata_force_tbl);
7058c6fd2807SJeff Garzik }
7059c6fd2807SJeff Garzik 
7060a4625085SBrian King subsys_initcall(ata_init);
7061c6fd2807SJeff Garzik module_exit(ata_exit);
7062c6fd2807SJeff Garzik 
70639990b6f3SAkinobu Mita static DEFINE_RATELIMIT_STATE(ratelimit, HZ / 5, 1);
7064c6fd2807SJeff Garzik 
7065c6fd2807SJeff Garzik int ata_ratelimit(void)
7066c6fd2807SJeff Garzik {
70679990b6f3SAkinobu Mita 	return __ratelimit(&ratelimit);
7068c6fd2807SJeff Garzik }
7069c6fd2807SJeff Garzik 
7070c0c362b6STejun Heo /**
7071c0c362b6STejun Heo  *	ata_msleep - ATA EH owner aware msleep
7072c0c362b6STejun Heo  *	@ap: ATA port to attribute the sleep to
7073c0c362b6STejun Heo  *	@msecs: duration to sleep in milliseconds
7074c0c362b6STejun Heo  *
7075c0c362b6STejun Heo  *	Sleeps @msecs.  If the current task is owner of @ap's EH, the
7076c0c362b6STejun Heo  *	ownership is released before going to sleep and reacquired
7077c0c362b6STejun Heo  *	after the sleep is complete.  IOW, other ports sharing the
7078c0c362b6STejun Heo  *	@ap->host will be allowed to own the EH while this task is
7079c0c362b6STejun Heo  *	sleeping.
7080c0c362b6STejun Heo  *
7081c0c362b6STejun Heo  *	LOCKING:
7082c0c362b6STejun Heo  *	Might sleep.
7083c0c362b6STejun Heo  */
708497750cebSTejun Heo void ata_msleep(struct ata_port *ap, unsigned int msecs)
708597750cebSTejun Heo {
7086c0c362b6STejun Heo 	bool owns_eh = ap && ap->host->eh_owner == current;
7087c0c362b6STejun Heo 
7088c0c362b6STejun Heo 	if (owns_eh)
7089c0c362b6STejun Heo 		ata_eh_release(ap);
7090c0c362b6STejun Heo 
7091848c3920SAnil Veliyankara Madam 	if (msecs < 20) {
7092848c3920SAnil Veliyankara Madam 		unsigned long usecs = msecs * USEC_PER_MSEC;
7093848c3920SAnil Veliyankara Madam 		usleep_range(usecs, usecs + 50);
7094848c3920SAnil Veliyankara Madam 	} else {
709597750cebSTejun Heo 		msleep(msecs);
7096848c3920SAnil Veliyankara Madam 	}
7097c0c362b6STejun Heo 
7098c0c362b6STejun Heo 	if (owns_eh)
7099c0c362b6STejun Heo 		ata_eh_acquire(ap);
710097750cebSTejun Heo }
710197750cebSTejun Heo 
7102c6fd2807SJeff Garzik /**
7103c6fd2807SJeff Garzik  *	ata_wait_register - wait until register value changes
710497750cebSTejun Heo  *	@ap: ATA port to wait register for, can be NULL
7105c6fd2807SJeff Garzik  *	@reg: IO-mapped register
7106c6fd2807SJeff Garzik  *	@mask: Mask to apply to read register value
7107c6fd2807SJeff Garzik  *	@val: Wait condition
7108341c2c95STejun Heo  *	@interval: polling interval in milliseconds
7109341c2c95STejun Heo  *	@timeout: timeout in milliseconds
7110c6fd2807SJeff Garzik  *
7111c6fd2807SJeff Garzik  *	Waiting for some bits of register to change is a common
7112c6fd2807SJeff Garzik  *	operation for ATA controllers.  This function reads 32bit LE
7113c6fd2807SJeff Garzik  *	IO-mapped register @reg and tests for the following condition.
7114c6fd2807SJeff Garzik  *
7115c6fd2807SJeff Garzik  *	(*@reg & mask) != val
7116c6fd2807SJeff Garzik  *
7117c6fd2807SJeff Garzik  *	If the condition is met, it returns; otherwise, the process is
7118c6fd2807SJeff Garzik  *	repeated after @interval_msec until timeout.
7119c6fd2807SJeff Garzik  *
7120c6fd2807SJeff Garzik  *	LOCKING:
7121c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
7122c6fd2807SJeff Garzik  *
7123c6fd2807SJeff Garzik  *	RETURNS:
7124c6fd2807SJeff Garzik  *	The final register value.
7125c6fd2807SJeff Garzik  */
712697750cebSTejun Heo u32 ata_wait_register(struct ata_port *ap, void __iomem *reg, u32 mask, u32 val,
7127341c2c95STejun Heo 		      unsigned long interval, unsigned long timeout)
7128c6fd2807SJeff Garzik {
7129341c2c95STejun Heo 	unsigned long deadline;
7130c6fd2807SJeff Garzik 	u32 tmp;
7131c6fd2807SJeff Garzik 
7132c6fd2807SJeff Garzik 	tmp = ioread32(reg);
7133c6fd2807SJeff Garzik 
7134c6fd2807SJeff Garzik 	/* Calculate timeout _after_ the first read to make sure
7135c6fd2807SJeff Garzik 	 * preceding writes reach the controller before starting to
7136c6fd2807SJeff Garzik 	 * eat away the timeout.
7137c6fd2807SJeff Garzik 	 */
7138341c2c95STejun Heo 	deadline = ata_deadline(jiffies, timeout);
7139c6fd2807SJeff Garzik 
7140341c2c95STejun Heo 	while ((tmp & mask) == val && time_before(jiffies, deadline)) {
714197750cebSTejun Heo 		ata_msleep(ap, interval);
7142c6fd2807SJeff Garzik 		tmp = ioread32(reg);
7143c6fd2807SJeff Garzik 	}
7144c6fd2807SJeff Garzik 
7145c6fd2807SJeff Garzik 	return tmp;
7146c6fd2807SJeff Garzik }
7147c6fd2807SJeff Garzik 
71488393b811SGabriele Mazzotta /**
71498393b811SGabriele Mazzotta  *	sata_lpm_ignore_phy_events - test if PHY event should be ignored
71508393b811SGabriele Mazzotta  *	@link: Link receiving the event
71518393b811SGabriele Mazzotta  *
71528393b811SGabriele Mazzotta  *	Test whether the received PHY event has to be ignored or not.
71538393b811SGabriele Mazzotta  *
71548393b811SGabriele Mazzotta  *	LOCKING:
71558393b811SGabriele Mazzotta  *	None:
71568393b811SGabriele Mazzotta  *
71578393b811SGabriele Mazzotta  *	RETURNS:
71588393b811SGabriele Mazzotta  *	True if the event has to be ignored.
71598393b811SGabriele Mazzotta  */
71608393b811SGabriele Mazzotta bool sata_lpm_ignore_phy_events(struct ata_link *link)
71618393b811SGabriele Mazzotta {
716209c5b480SGabriele Mazzotta 	unsigned long lpm_timeout = link->last_lpm_change +
716309c5b480SGabriele Mazzotta 				    msecs_to_jiffies(ATA_TMOUT_SPURIOUS_PHY);
716409c5b480SGabriele Mazzotta 
71658393b811SGabriele Mazzotta 	/* if LPM is enabled, PHYRDY doesn't mean anything */
716609c5b480SGabriele Mazzotta 	if (link->lpm_policy > ATA_LPM_MAX_POWER)
716709c5b480SGabriele Mazzotta 		return true;
716809c5b480SGabriele Mazzotta 
716909c5b480SGabriele Mazzotta 	/* ignore the first PHY event after the LPM policy changed
717009c5b480SGabriele Mazzotta 	 * as it is might be spurious
717109c5b480SGabriele Mazzotta 	 */
717209c5b480SGabriele Mazzotta 	if ((link->flags & ATA_LFLAG_CHANGED) &&
717309c5b480SGabriele Mazzotta 	    time_before(jiffies, lpm_timeout))
717409c5b480SGabriele Mazzotta 		return true;
717509c5b480SGabriele Mazzotta 
717609c5b480SGabriele Mazzotta 	return false;
71778393b811SGabriele Mazzotta }
71788393b811SGabriele Mazzotta EXPORT_SYMBOL_GPL(sata_lpm_ignore_phy_events);
71798393b811SGabriele Mazzotta 
7180c6fd2807SJeff Garzik /*
7181c6fd2807SJeff Garzik  * Dummy port_ops
7182c6fd2807SJeff Garzik  */
7183c6fd2807SJeff Garzik static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
7184c6fd2807SJeff Garzik {
7185c6fd2807SJeff Garzik 	return AC_ERR_SYSTEM;
7186c6fd2807SJeff Garzik }
7187c6fd2807SJeff Garzik 
7188182d7bbaSTejun Heo static void ata_dummy_error_handler(struct ata_port *ap)
7189182d7bbaSTejun Heo {
7190182d7bbaSTejun Heo 	/* truly dummy */
7191182d7bbaSTejun Heo }
7192182d7bbaSTejun Heo 
7193029cfd6bSTejun Heo struct ata_port_operations ata_dummy_port_ops = {
7194c6fd2807SJeff Garzik 	.qc_prep		= ata_noop_qc_prep,
7195c6fd2807SJeff Garzik 	.qc_issue		= ata_dummy_qc_issue,
7196182d7bbaSTejun Heo 	.error_handler		= ata_dummy_error_handler,
7197e4a9c373SDan Williams 	.sched_eh		= ata_std_sched_eh,
7198e4a9c373SDan Williams 	.end_eh			= ata_std_end_eh,
7199c6fd2807SJeff Garzik };
7200c6fd2807SJeff Garzik 
720121b0ad4fSTejun Heo const struct ata_port_info ata_dummy_port_info = {
720221b0ad4fSTejun Heo 	.port_ops		= &ata_dummy_port_ops,
720321b0ad4fSTejun Heo };
720421b0ad4fSTejun Heo 
7205c6fd2807SJeff Garzik /*
7206a9a79dfeSJoe Perches  * Utility print functions
7207a9a79dfeSJoe Perches  */
7208d7bead1bSJoe Perches void ata_port_printk(const struct ata_port *ap, const char *level,
7209a9a79dfeSJoe Perches 		     const char *fmt, ...)
7210a9a79dfeSJoe Perches {
7211a9a79dfeSJoe Perches 	struct va_format vaf;
7212a9a79dfeSJoe Perches 	va_list args;
7213a9a79dfeSJoe Perches 
7214a9a79dfeSJoe Perches 	va_start(args, fmt);
7215a9a79dfeSJoe Perches 
7216a9a79dfeSJoe Perches 	vaf.fmt = fmt;
7217a9a79dfeSJoe Perches 	vaf.va = &args;
7218a9a79dfeSJoe Perches 
7219d7bead1bSJoe Perches 	printk("%sata%u: %pV", level, ap->print_id, &vaf);
7220a9a79dfeSJoe Perches 
7221a9a79dfeSJoe Perches 	va_end(args);
7222a9a79dfeSJoe Perches }
7223a9a79dfeSJoe Perches EXPORT_SYMBOL(ata_port_printk);
7224a9a79dfeSJoe Perches 
7225d7bead1bSJoe Perches void ata_link_printk(const struct ata_link *link, const char *level,
7226a9a79dfeSJoe Perches 		     const char *fmt, ...)
7227a9a79dfeSJoe Perches {
7228a9a79dfeSJoe Perches 	struct va_format vaf;
7229a9a79dfeSJoe Perches 	va_list args;
7230a9a79dfeSJoe Perches 
7231a9a79dfeSJoe Perches 	va_start(args, fmt);
7232a9a79dfeSJoe Perches 
7233a9a79dfeSJoe Perches 	vaf.fmt = fmt;
7234a9a79dfeSJoe Perches 	vaf.va = &args;
7235a9a79dfeSJoe Perches 
7236a9a79dfeSJoe Perches 	if (sata_pmp_attached(link->ap) || link->ap->slave_link)
7237d7bead1bSJoe Perches 		printk("%sata%u.%02u: %pV",
7238a9a79dfeSJoe Perches 		       level, link->ap->print_id, link->pmp, &vaf);
7239a9a79dfeSJoe Perches 	else
7240d7bead1bSJoe Perches 		printk("%sata%u: %pV",
7241a9a79dfeSJoe Perches 		       level, link->ap->print_id, &vaf);
7242a9a79dfeSJoe Perches 
7243a9a79dfeSJoe Perches 	va_end(args);
7244a9a79dfeSJoe Perches }
7245a9a79dfeSJoe Perches EXPORT_SYMBOL(ata_link_printk);
7246a9a79dfeSJoe Perches 
7247d7bead1bSJoe Perches void ata_dev_printk(const struct ata_device *dev, const char *level,
7248a9a79dfeSJoe Perches 		    const char *fmt, ...)
7249a9a79dfeSJoe Perches {
7250a9a79dfeSJoe Perches 	struct va_format vaf;
7251a9a79dfeSJoe Perches 	va_list args;
7252a9a79dfeSJoe Perches 
7253a9a79dfeSJoe Perches 	va_start(args, fmt);
7254a9a79dfeSJoe Perches 
7255a9a79dfeSJoe Perches 	vaf.fmt = fmt;
7256a9a79dfeSJoe Perches 	vaf.va = &args;
7257a9a79dfeSJoe Perches 
7258d7bead1bSJoe Perches 	printk("%sata%u.%02u: %pV",
7259a9a79dfeSJoe Perches 	       level, dev->link->ap->print_id, dev->link->pmp + dev->devno,
7260a9a79dfeSJoe Perches 	       &vaf);
7261a9a79dfeSJoe Perches 
7262a9a79dfeSJoe Perches 	va_end(args);
7263a9a79dfeSJoe Perches }
7264a9a79dfeSJoe Perches EXPORT_SYMBOL(ata_dev_printk);
7265a9a79dfeSJoe Perches 
726606296a1eSJoe Perches void ata_print_version(const struct device *dev, const char *version)
726706296a1eSJoe Perches {
726806296a1eSJoe Perches 	dev_printk(KERN_DEBUG, dev, "version %s\n", version);
726906296a1eSJoe Perches }
727006296a1eSJoe Perches EXPORT_SYMBOL(ata_print_version);
727106296a1eSJoe Perches 
7272a9a79dfeSJoe Perches /*
7273c6fd2807SJeff Garzik  * libata is essentially a library of internal helper functions for
7274c6fd2807SJeff Garzik  * low-level ATA host controller drivers.  As such, the API/ABI is
7275c6fd2807SJeff Garzik  * likely to change as new drivers are added and updated.
7276c6fd2807SJeff Garzik  * Do not depend on ABI/API stability.
7277c6fd2807SJeff Garzik  */
7278c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
7279c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
7280c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_deb_timing_long);
7281029cfd6bSTejun Heo EXPORT_SYMBOL_GPL(ata_base_port_ops);
7282029cfd6bSTejun Heo EXPORT_SYMBOL_GPL(sata_port_ops);
7283c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
728421b0ad4fSTejun Heo EXPORT_SYMBOL_GPL(ata_dummy_port_info);
72851eca4365STejun Heo EXPORT_SYMBOL_GPL(ata_link_next);
72861eca4365STejun Heo EXPORT_SYMBOL_GPL(ata_dev_next);
7287c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_std_bios_param);
7288d8d9129eSTejun Heo EXPORT_SYMBOL_GPL(ata_scsi_unlock_native_capacity);
7289cca3974eSJeff Garzik EXPORT_SYMBOL_GPL(ata_host_init);
7290f3187195STejun Heo EXPORT_SYMBOL_GPL(ata_host_alloc);
7291f5cda257STejun Heo EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
7292b1c72916STejun Heo EXPORT_SYMBOL_GPL(ata_slave_link_init);
7293ecef7253STejun Heo EXPORT_SYMBOL_GPL(ata_host_start);
7294f3187195STejun Heo EXPORT_SYMBOL_GPL(ata_host_register);
7295f5cda257STejun Heo EXPORT_SYMBOL_GPL(ata_host_activate);
72960529c159STejun Heo EXPORT_SYMBOL_GPL(ata_host_detach);
7297c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_sg_init);
7298c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_qc_complete);
7299c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
7300436d34b3STejun Heo EXPORT_SYMBOL_GPL(atapi_cmd_type);
7301c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_tf_to_fis);
7302c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_tf_from_fis);
73036357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_pack_xfermask);
73046357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_unpack_xfermask);
73056357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
73066357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
73076357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
73086357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_mode_string);
73096357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_id_xfermask);
731004351821SAlan EXPORT_SYMBOL_GPL(ata_do_set_mode);
731131cc23b3STejun Heo EXPORT_SYMBOL_GPL(ata_std_qc_defer);
7312c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
731310305f0fSAlan EXPORT_SYMBOL_GPL(ata_dev_disable);
7314c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_set_spd);
7315aa2731adSTejun Heo EXPORT_SYMBOL_GPL(ata_wait_after_reset);
7316936fd732STejun Heo EXPORT_SYMBOL_GPL(sata_link_debounce);
7317936fd732STejun Heo EXPORT_SYMBOL_GPL(sata_link_resume);
73181152b261STejun Heo EXPORT_SYMBOL_GPL(sata_link_scr_lpm);
73190aa1113dSTejun Heo EXPORT_SYMBOL_GPL(ata_std_prereset);
7320cc0680a5STejun Heo EXPORT_SYMBOL_GPL(sata_link_hardreset);
732157c9efdfSTejun Heo EXPORT_SYMBOL_GPL(sata_std_hardreset);
7322203c75b8STejun Heo EXPORT_SYMBOL_GPL(ata_std_postreset);
7323c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_dev_classify);
7324c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_dev_pair);
7325c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_ratelimit);
732697750cebSTejun Heo EXPORT_SYMBOL_GPL(ata_msleep);
7327c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_wait_register);
7328c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
7329c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
7330c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
7331c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
7332f6e67035SDan Williams EXPORT_SYMBOL_GPL(__ata_change_queue_depth);
7333c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_valid);
7334c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_read);
7335c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_write);
7336c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_write_flush);
7337936fd732STejun Heo EXPORT_SYMBOL_GPL(ata_link_online);
7338936fd732STejun Heo EXPORT_SYMBOL_GPL(ata_link_offline);
73396ffa01d8STejun Heo #ifdef CONFIG_PM
7340cca3974eSJeff Garzik EXPORT_SYMBOL_GPL(ata_host_suspend);
7341cca3974eSJeff Garzik EXPORT_SYMBOL_GPL(ata_host_resume);
73426ffa01d8STejun Heo #endif /* CONFIG_PM */
7343c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_id_string);
7344c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_id_c_string);
7345963e4975SAlan Cox EXPORT_SYMBOL_GPL(ata_do_dev_read_id);
7346c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_simulate);
7347c6fd2807SJeff Garzik 
7348c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
73496357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_timing_find_mode);
7350c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_timing_compute);
7351c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_timing_merge);
7352a0f79b92STejun Heo EXPORT_SYMBOL_GPL(ata_timing_cycle2mode);
7353c6fd2807SJeff Garzik 
7354c6fd2807SJeff Garzik #ifdef CONFIG_PCI
7355c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(pci_test_config_bits);
7356c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_remove_one);
73576ffa01d8STejun Heo #ifdef CONFIG_PM
7358c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
7359c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
7360c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
7361c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_resume);
73626ffa01d8STejun Heo #endif /* CONFIG_PM */
7363c6fd2807SJeff Garzik #endif /* CONFIG_PCI */
7364c6fd2807SJeff Garzik 
7365b7db04d9SBrian Norris EXPORT_SYMBOL_GPL(ata_platform_remove_one);
7366b7db04d9SBrian Norris 
7367b64bbc39STejun Heo EXPORT_SYMBOL_GPL(__ata_ehi_push_desc);
7368b64bbc39STejun Heo EXPORT_SYMBOL_GPL(ata_ehi_push_desc);
7369b64bbc39STejun Heo EXPORT_SYMBOL_GPL(ata_ehi_clear_desc);
7370cbcdd875STejun Heo EXPORT_SYMBOL_GPL(ata_port_desc);
7371cbcdd875STejun Heo #ifdef CONFIG_PCI
7372cbcdd875STejun Heo EXPORT_SYMBOL_GPL(ata_port_pbar_desc);
7373cbcdd875STejun Heo #endif /* CONFIG_PCI */
7374c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
7375dbd82616STejun Heo EXPORT_SYMBOL_GPL(ata_link_abort);
7376c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_abort);
7377c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_freeze);
73787d77b247STejun Heo EXPORT_SYMBOL_GPL(sata_async_notification);
7379c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
7380c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
7381c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
7382c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
738310acf3b0SMark Lord EXPORT_SYMBOL_GPL(ata_eh_analyze_ncq_error);
7384c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_do_eh);
7385a1efdabaSTejun Heo EXPORT_SYMBOL_GPL(ata_std_error_handler);
7386be0d18dfSAlan Cox 
7387be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_40wire);
7388be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_80wire);
7389be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_unknown);
7390c88f90c3STejun Heo EXPORT_SYMBOL_GPL(ata_cable_ignore);
7391be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_sata);
73922fa4a326SJason Yan EXPORT_SYMBOL_GPL(ata_host_get);
73932fa4a326SJason Yan EXPORT_SYMBOL_GPL(ata_host_put);