xref: /openbmc/linux/drivers/ata/libata-core.c (revision af27e01c)
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',
28c6fd2807SJeff Garzik  *  as Documentation/DocBook/libata.*
29c6fd2807SJeff Garzik  *
30c6fd2807SJeff Garzik  *  Hardware documentation available from http://www.t13.org/ and
31c6fd2807SJeff Garzik  *  http://www.sata-io.org/
32c6fd2807SJeff Garzik  *
3392c52c52SAlan Cox  *  Standards documents from:
3492c52c52SAlan Cox  *	http://www.t13.org (ATA standards, PCI DMA IDE spec)
3592c52c52SAlan Cox  *	http://www.t10.org (SCSI MMC - for ATAPI MMC)
3692c52c52SAlan Cox  *	http://www.sata-io.org (SATA)
3792c52c52SAlan Cox  *	http://www.compactflash.org (CF)
3892c52c52SAlan Cox  *	http://www.qic.org (QIC157 - Tape and DSC)
3992c52c52SAlan Cox  *	http://www.ce-ata.org (CE-ATA: not supported)
4092c52c52SAlan Cox  *
41c6fd2807SJeff Garzik  */
42c6fd2807SJeff Garzik 
43c6fd2807SJeff Garzik #include <linux/kernel.h>
44c6fd2807SJeff Garzik #include <linux/module.h>
45c6fd2807SJeff Garzik #include <linux/pci.h>
46c6fd2807SJeff Garzik #include <linux/init.h>
47c6fd2807SJeff Garzik #include <linux/list.h>
48c6fd2807SJeff Garzik #include <linux/mm.h>
49c6fd2807SJeff Garzik #include <linux/spinlock.h>
50c6fd2807SJeff Garzik #include <linux/blkdev.h>
51c6fd2807SJeff Garzik #include <linux/delay.h>
52c6fd2807SJeff Garzik #include <linux/timer.h>
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 
7626d1245bfSTejun Heo 	if (ata_ncq_enabled(dev) && likely(tag != ATA_TAG_INTERNAL)) {
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;
1573c6fd2807SJeff Garzik 	unsigned int tag, preempted_tag;
1574c6fd2807SJeff Garzik 	u32 preempted_sactive, preempted_qc_active;
1575da917d69STejun Heo 	int preempted_nr_active_links;
1576c6fd2807SJeff Garzik 	DECLARE_COMPLETION_ONSTACK(wait);
1577c6fd2807SJeff Garzik 	unsigned long flags;
1578c6fd2807SJeff Garzik 	unsigned int err_mask;
1579c6fd2807SJeff Garzik 	int rc;
1580c6fd2807SJeff Garzik 
1581c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1582c6fd2807SJeff Garzik 
1583c6fd2807SJeff Garzik 	/* no internal command while frozen */
1584c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_FROZEN) {
1585c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
1586c6fd2807SJeff Garzik 		return AC_ERR_SYSTEM;
1587c6fd2807SJeff Garzik 	}
1588c6fd2807SJeff Garzik 
1589c6fd2807SJeff Garzik 	/* initialize internal qc */
1590c6fd2807SJeff Garzik 
1591c6fd2807SJeff Garzik 	/* XXX: Tag 0 is used for drivers with legacy EH as some
1592c6fd2807SJeff Garzik 	 * drivers choke if any other tag is given.  This breaks
1593c6fd2807SJeff Garzik 	 * ata_tag_internal() test for those drivers.  Don't use new
1594c6fd2807SJeff Garzik 	 * EH stuff without converting to it.
1595c6fd2807SJeff Garzik 	 */
1596c6fd2807SJeff Garzik 	if (ap->ops->error_handler)
1597c6fd2807SJeff Garzik 		tag = ATA_TAG_INTERNAL;
1598c6fd2807SJeff Garzik 	else
1599c6fd2807SJeff Garzik 		tag = 0;
1600c6fd2807SJeff Garzik 
1601c6fd2807SJeff Garzik 	qc = __ata_qc_from_tag(ap, tag);
1602c6fd2807SJeff Garzik 
1603c6fd2807SJeff Garzik 	qc->tag = tag;
1604c6fd2807SJeff Garzik 	qc->scsicmd = NULL;
1605c6fd2807SJeff Garzik 	qc->ap = ap;
1606c6fd2807SJeff Garzik 	qc->dev = dev;
1607c6fd2807SJeff Garzik 	ata_qc_reinit(qc);
1608c6fd2807SJeff Garzik 
16099af5c9c9STejun Heo 	preempted_tag = link->active_tag;
16109af5c9c9STejun Heo 	preempted_sactive = link->sactive;
1611c6fd2807SJeff Garzik 	preempted_qc_active = ap->qc_active;
1612da917d69STejun Heo 	preempted_nr_active_links = ap->nr_active_links;
16139af5c9c9STejun Heo 	link->active_tag = ATA_TAG_POISON;
16149af5c9c9STejun Heo 	link->sactive = 0;
1615c6fd2807SJeff Garzik 	ap->qc_active = 0;
1616da917d69STejun Heo 	ap->nr_active_links = 0;
1617c6fd2807SJeff Garzik 
1618c6fd2807SJeff Garzik 	/* prepare & issue qc */
1619c6fd2807SJeff Garzik 	qc->tf = *tf;
1620c6fd2807SJeff Garzik 	if (cdb)
1621c6fd2807SJeff Garzik 		memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
1622e771451cSVincent Pelletier 
1623e771451cSVincent Pelletier 	/* some SATA bridges need us to indicate data xfer direction */
1624e771451cSVincent Pelletier 	if (tf->protocol == ATAPI_PROT_DMA && (dev->flags & ATA_DFLAG_DMADIR) &&
1625e771451cSVincent Pelletier 	    dma_dir == DMA_FROM_DEVICE)
1626e771451cSVincent Pelletier 		qc->tf.feature |= ATAPI_DMADIR;
1627e771451cSVincent Pelletier 
1628c6fd2807SJeff Garzik 	qc->flags |= ATA_QCFLAG_RESULT_TF;
1629c6fd2807SJeff Garzik 	qc->dma_dir = dma_dir;
1630c6fd2807SJeff Garzik 	if (dma_dir != DMA_NONE) {
16312432697bSTejun Heo 		unsigned int i, buflen = 0;
163287260216SJens Axboe 		struct scatterlist *sg;
16332432697bSTejun Heo 
163487260216SJens Axboe 		for_each_sg(sgl, sg, n_elem, i)
163587260216SJens Axboe 			buflen += sg->length;
16362432697bSTejun Heo 
163787260216SJens Axboe 		ata_sg_init(qc, sgl, n_elem);
163849c80429SBrian King 		qc->nbytes = buflen;
1639c6fd2807SJeff Garzik 	}
1640c6fd2807SJeff Garzik 
1641c6fd2807SJeff Garzik 	qc->private_data = &wait;
1642c6fd2807SJeff Garzik 	qc->complete_fn = ata_qc_complete_internal;
1643c6fd2807SJeff Garzik 
1644c6fd2807SJeff Garzik 	ata_qc_issue(qc);
1645c6fd2807SJeff Garzik 
1646c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1647c6fd2807SJeff Garzik 
164887fbc5a0STejun Heo 	if (!timeout) {
164987fbc5a0STejun Heo 		if (ata_probe_timeout)
1650341c2c95STejun Heo 			timeout = ata_probe_timeout * 1000;
165187fbc5a0STejun Heo 		else {
165287fbc5a0STejun Heo 			timeout = ata_internal_cmd_timeout(dev, command);
165387fbc5a0STejun Heo 			auto_timeout = 1;
165487fbc5a0STejun Heo 		}
165587fbc5a0STejun Heo 	}
16562b789108STejun Heo 
1657c0c362b6STejun Heo 	if (ap->ops->error_handler)
1658c0c362b6STejun Heo 		ata_eh_release(ap);
1659c0c362b6STejun Heo 
16602b789108STejun Heo 	rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
1661c6fd2807SJeff Garzik 
1662c0c362b6STejun Heo 	if (ap->ops->error_handler)
1663c0c362b6STejun Heo 		ata_eh_acquire(ap);
1664c0c362b6STejun Heo 
1665c429137aSTejun Heo 	ata_sff_flush_pio_task(ap);
1666c6fd2807SJeff Garzik 
1667c6fd2807SJeff Garzik 	if (!rc) {
1668c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
1669c6fd2807SJeff Garzik 
1670c6fd2807SJeff Garzik 		/* We're racing with irq here.  If we lose, the
1671c6fd2807SJeff Garzik 		 * following test prevents us from completing the qc
1672c6fd2807SJeff Garzik 		 * twice.  If we win, the port is frozen and will be
1673c6fd2807SJeff Garzik 		 * cleaned up by ->post_internal_cmd().
1674c6fd2807SJeff Garzik 		 */
1675c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_ACTIVE) {
1676c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_TIMEOUT;
1677c6fd2807SJeff Garzik 
1678c6fd2807SJeff Garzik 			if (ap->ops->error_handler)
1679c6fd2807SJeff Garzik 				ata_port_freeze(ap);
1680c6fd2807SJeff Garzik 			else
1681c6fd2807SJeff Garzik 				ata_qc_complete(qc);
1682c6fd2807SJeff Garzik 
1683c6fd2807SJeff Garzik 			if (ata_msg_warn(ap))
1684a9a79dfeSJoe Perches 				ata_dev_warn(dev, "qc timeout (cmd 0x%x)\n",
1685a9a79dfeSJoe Perches 					     command);
1686c6fd2807SJeff Garzik 		}
1687c6fd2807SJeff Garzik 
1688c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
1689c6fd2807SJeff Garzik 	}
1690c6fd2807SJeff Garzik 
1691c6fd2807SJeff Garzik 	/* do post_internal_cmd */
1692c6fd2807SJeff Garzik 	if (ap->ops->post_internal_cmd)
1693c6fd2807SJeff Garzik 		ap->ops->post_internal_cmd(qc);
1694c6fd2807SJeff Garzik 
1695a51d644aSTejun Heo 	/* perform minimal error analysis */
1696a51d644aSTejun Heo 	if (qc->flags & ATA_QCFLAG_FAILED) {
1697a51d644aSTejun Heo 		if (qc->result_tf.command & (ATA_ERR | ATA_DF))
1698a51d644aSTejun Heo 			qc->err_mask |= AC_ERR_DEV;
1699a51d644aSTejun Heo 
1700a51d644aSTejun Heo 		if (!qc->err_mask)
1701c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_OTHER;
1702a51d644aSTejun Heo 
1703a51d644aSTejun Heo 		if (qc->err_mask & ~AC_ERR_OTHER)
1704a51d644aSTejun Heo 			qc->err_mask &= ~AC_ERR_OTHER;
17052dae9955SDamien Le Moal 	} else if (qc->tf.command == ATA_CMD_REQ_SENSE_DATA) {
17062dae9955SDamien Le Moal 		qc->result_tf.command |= ATA_SENSE;
1707c6fd2807SJeff Garzik 	}
1708c6fd2807SJeff Garzik 
1709c6fd2807SJeff Garzik 	/* finish up */
1710c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1711c6fd2807SJeff Garzik 
1712c6fd2807SJeff Garzik 	*tf = qc->result_tf;
1713c6fd2807SJeff Garzik 	err_mask = qc->err_mask;
1714c6fd2807SJeff Garzik 
1715c6fd2807SJeff Garzik 	ata_qc_free(qc);
17169af5c9c9STejun Heo 	link->active_tag = preempted_tag;
17179af5c9c9STejun Heo 	link->sactive = preempted_sactive;
1718c6fd2807SJeff Garzik 	ap->qc_active = preempted_qc_active;
1719da917d69STejun Heo 	ap->nr_active_links = preempted_nr_active_links;
1720c6fd2807SJeff Garzik 
1721c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1722c6fd2807SJeff Garzik 
172387fbc5a0STejun Heo 	if ((err_mask & AC_ERR_TIMEOUT) && auto_timeout)
172487fbc5a0STejun Heo 		ata_internal_cmd_timed_out(dev, command);
172587fbc5a0STejun Heo 
1726c6fd2807SJeff Garzik 	return err_mask;
1727c6fd2807SJeff Garzik }
1728c6fd2807SJeff Garzik 
1729c6fd2807SJeff Garzik /**
173033480a0eSTejun Heo  *	ata_exec_internal - execute libata internal command
17312432697bSTejun Heo  *	@dev: Device to which the command is sent
17322432697bSTejun Heo  *	@tf: Taskfile registers for the command and the result
17332432697bSTejun Heo  *	@cdb: CDB for packet command
1734e227867fSMasanari Iida  *	@dma_dir: Data transfer direction of the command
17352432697bSTejun Heo  *	@buf: Data buffer of the command
17362432697bSTejun Heo  *	@buflen: Length of data buffer
17372b789108STejun Heo  *	@timeout: Timeout in msecs (0 for default)
17382432697bSTejun Heo  *
17392432697bSTejun Heo  *	Wrapper around ata_exec_internal_sg() which takes simple
17402432697bSTejun Heo  *	buffer instead of sg list.
17412432697bSTejun Heo  *
17422432697bSTejun Heo  *	LOCKING:
17432432697bSTejun Heo  *	None.  Should be called with kernel context, might sleep.
17442432697bSTejun Heo  *
17452432697bSTejun Heo  *	RETURNS:
17462432697bSTejun Heo  *	Zero on success, AC_ERR_* mask on failure
17472432697bSTejun Heo  */
17482432697bSTejun Heo unsigned ata_exec_internal(struct ata_device *dev,
17492432697bSTejun Heo 			   struct ata_taskfile *tf, const u8 *cdb,
17502b789108STejun Heo 			   int dma_dir, void *buf, unsigned int buflen,
17512b789108STejun Heo 			   unsigned long timeout)
17522432697bSTejun Heo {
175333480a0eSTejun Heo 	struct scatterlist *psg = NULL, sg;
175433480a0eSTejun Heo 	unsigned int n_elem = 0;
17552432697bSTejun Heo 
175633480a0eSTejun Heo 	if (dma_dir != DMA_NONE) {
175733480a0eSTejun Heo 		WARN_ON(!buf);
17582432697bSTejun Heo 		sg_init_one(&sg, buf, buflen);
175933480a0eSTejun Heo 		psg = &sg;
176033480a0eSTejun Heo 		n_elem++;
176133480a0eSTejun Heo 	}
17622432697bSTejun Heo 
17632b789108STejun Heo 	return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem,
17642b789108STejun Heo 				    timeout);
17652432697bSTejun Heo }
17662432697bSTejun Heo 
17672432697bSTejun Heo /**
1768c6fd2807SJeff Garzik  *	ata_pio_need_iordy	-	check if iordy needed
1769c6fd2807SJeff Garzik  *	@adev: ATA device
1770c6fd2807SJeff Garzik  *
1771c6fd2807SJeff Garzik  *	Check if the current speed of the device requires IORDY. Used
1772c6fd2807SJeff Garzik  *	by various controllers for chip configuration.
1773c6fd2807SJeff Garzik  */
1774c6fd2807SJeff Garzik unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1775c6fd2807SJeff Garzik {
17760d9e6659STejun Heo 	/* Don't set IORDY if we're preparing for reset.  IORDY may
17770d9e6659STejun Heo 	 * lead to controller lock up on certain controllers if the
17780d9e6659STejun Heo 	 * port is not occupied.  See bko#11703 for details.
17790d9e6659STejun Heo 	 */
17800d9e6659STejun Heo 	if (adev->link->ap->pflags & ATA_PFLAG_RESETTING)
17810d9e6659STejun Heo 		return 0;
17820d9e6659STejun Heo 	/* Controller doesn't support IORDY.  Probably a pointless
17830d9e6659STejun Heo 	 * check as the caller should know this.
17840d9e6659STejun Heo 	 */
17859af5c9c9STejun Heo 	if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
1786c6fd2807SJeff Garzik 		return 0;
17875c18c4d2SDavid Daney 	/* CF spec. r4.1 Table 22 says no iordy on PIO5 and PIO6.  */
17885c18c4d2SDavid Daney 	if (ata_id_is_cfa(adev->id)
17895c18c4d2SDavid Daney 	    && (adev->pio_mode == XFER_PIO_5 || adev->pio_mode == XFER_PIO_6))
17905c18c4d2SDavid Daney 		return 0;
1791432729f0SAlan Cox 	/* PIO3 and higher it is mandatory */
1792432729f0SAlan Cox 	if (adev->pio_mode > XFER_PIO_2)
1793c6fd2807SJeff Garzik 		return 1;
1794432729f0SAlan Cox 	/* We turn it on when possible */
1795432729f0SAlan Cox 	if (ata_id_has_iordy(adev->id))
1796432729f0SAlan Cox 		return 1;
1797432729f0SAlan Cox 	return 0;
1798432729f0SAlan Cox }
1799c6fd2807SJeff Garzik 
1800432729f0SAlan Cox /**
1801432729f0SAlan Cox  *	ata_pio_mask_no_iordy	-	Return the non IORDY mask
1802432729f0SAlan Cox  *	@adev: ATA device
1803432729f0SAlan Cox  *
1804432729f0SAlan Cox  *	Compute the highest mode possible if we are not using iordy. Return
1805432729f0SAlan Cox  *	-1 if no iordy mode is available.
1806432729f0SAlan Cox  */
1807432729f0SAlan Cox static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
1808432729f0SAlan Cox {
1809c6fd2807SJeff Garzik 	/* If we have no drive specific rule, then PIO 2 is non IORDY */
1810c6fd2807SJeff Garzik 	if (adev->id[ATA_ID_FIELD_VALID] & 2) {	/* EIDE */
1811432729f0SAlan Cox 		u16 pio = adev->id[ATA_ID_EIDE_PIO];
1812c6fd2807SJeff Garzik 		/* Is the speed faster than the drive allows non IORDY ? */
1813c6fd2807SJeff Garzik 		if (pio) {
1814c6fd2807SJeff Garzik 			/* This is cycle times not frequency - watch the logic! */
1815c6fd2807SJeff Garzik 			if (pio > 240)	/* PIO2 is 240nS per cycle */
1816432729f0SAlan Cox 				return 3 << ATA_SHIFT_PIO;
1817432729f0SAlan Cox 			return 7 << ATA_SHIFT_PIO;
1818c6fd2807SJeff Garzik 		}
1819c6fd2807SJeff Garzik 	}
1820432729f0SAlan Cox 	return 3 << ATA_SHIFT_PIO;
1821c6fd2807SJeff Garzik }
1822c6fd2807SJeff Garzik 
1823c6fd2807SJeff Garzik /**
1824963e4975SAlan Cox  *	ata_do_dev_read_id		-	default ID read method
1825963e4975SAlan Cox  *	@dev: device
1826963e4975SAlan Cox  *	@tf: proposed taskfile
1827963e4975SAlan Cox  *	@id: data buffer
1828963e4975SAlan Cox  *
1829963e4975SAlan Cox  *	Issue the identify taskfile and hand back the buffer containing
1830963e4975SAlan Cox  *	identify data. For some RAID controllers and for pre ATA devices
1831963e4975SAlan Cox  *	this function is wrapped or replaced by the driver
1832963e4975SAlan Cox  */
1833963e4975SAlan Cox unsigned int ata_do_dev_read_id(struct ata_device *dev,
1834963e4975SAlan Cox 					struct ata_taskfile *tf, u16 *id)
1835963e4975SAlan Cox {
1836963e4975SAlan Cox 	return ata_exec_internal(dev, tf, NULL, DMA_FROM_DEVICE,
1837963e4975SAlan Cox 				     id, sizeof(id[0]) * ATA_ID_WORDS, 0);
1838963e4975SAlan Cox }
1839963e4975SAlan Cox 
1840963e4975SAlan Cox /**
1841c6fd2807SJeff Garzik  *	ata_dev_read_id - Read ID data from the specified device
1842c6fd2807SJeff Garzik  *	@dev: target device
1843c6fd2807SJeff Garzik  *	@p_class: pointer to class of the target device (may be changed)
1844bff04647STejun Heo  *	@flags: ATA_READID_* flags
1845c6fd2807SJeff Garzik  *	@id: buffer to read IDENTIFY data into
1846c6fd2807SJeff Garzik  *
1847c6fd2807SJeff Garzik  *	Read ID data from the specified device.  ATA_CMD_ID_ATA is
1848c6fd2807SJeff Garzik  *	performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
1849c6fd2807SJeff Garzik  *	devices.  This function also issues ATA_CMD_INIT_DEV_PARAMS
1850c6fd2807SJeff Garzik  *	for pre-ATA4 drives.
1851c6fd2807SJeff Garzik  *
185250a99018SAlan Cox  *	FIXME: ATA_CMD_ID_ATA is optional for early drives and right
185350a99018SAlan Cox  *	now we abort if we hit that case.
185450a99018SAlan Cox  *
1855c6fd2807SJeff Garzik  *	LOCKING:
1856c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
1857c6fd2807SJeff Garzik  *
1858c6fd2807SJeff Garzik  *	RETURNS:
1859c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
1860c6fd2807SJeff Garzik  */
1861c6fd2807SJeff Garzik int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
1862bff04647STejun Heo 		    unsigned int flags, u16 *id)
1863c6fd2807SJeff Garzik {
18649af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
1865c6fd2807SJeff Garzik 	unsigned int class = *p_class;
1866c6fd2807SJeff Garzik 	struct ata_taskfile tf;
1867c6fd2807SJeff Garzik 	unsigned int err_mask = 0;
1868c6fd2807SJeff Garzik 	const char *reason;
186979b42babSTejun Heo 	bool is_semb = class == ATA_DEV_SEMB;
187054936f8bSTejun Heo 	int may_fallback = 1, tried_spinup = 0;
1871c6fd2807SJeff Garzik 	int rc;
1872c6fd2807SJeff Garzik 
1873c6fd2807SJeff Garzik 	if (ata_msg_ctl(ap))
1874a9a79dfeSJoe Perches 		ata_dev_dbg(dev, "%s: ENTER\n", __func__);
1875c6fd2807SJeff Garzik 
1876c6fd2807SJeff Garzik retry:
1877c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
1878c6fd2807SJeff Garzik 
1879c6fd2807SJeff Garzik 	switch (class) {
188079b42babSTejun Heo 	case ATA_DEV_SEMB:
188179b42babSTejun Heo 		class = ATA_DEV_ATA;	/* some hard drives report SEMB sig */
1882c6fd2807SJeff Garzik 	case ATA_DEV_ATA:
18839162c657SHannes Reinecke 	case ATA_DEV_ZAC:
1884c6fd2807SJeff Garzik 		tf.command = ATA_CMD_ID_ATA;
1885c6fd2807SJeff Garzik 		break;
1886c6fd2807SJeff Garzik 	case ATA_DEV_ATAPI:
1887c6fd2807SJeff Garzik 		tf.command = ATA_CMD_ID_ATAPI;
1888c6fd2807SJeff Garzik 		break;
1889c6fd2807SJeff Garzik 	default:
1890c6fd2807SJeff Garzik 		rc = -ENODEV;
1891c6fd2807SJeff Garzik 		reason = "unsupported class";
1892c6fd2807SJeff Garzik 		goto err_out;
1893c6fd2807SJeff Garzik 	}
1894c6fd2807SJeff Garzik 
1895c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_PIO;
189681afe893STejun Heo 
189781afe893STejun Heo 	/* Some devices choke if TF registers contain garbage.  Make
189881afe893STejun Heo 	 * sure those are properly initialized.
189981afe893STejun Heo 	 */
190081afe893STejun Heo 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
190181afe893STejun Heo 
190281afe893STejun Heo 	/* Device presence detection is unreliable on some
190381afe893STejun Heo 	 * controllers.  Always poll IDENTIFY if available.
190481afe893STejun Heo 	 */
190581afe893STejun Heo 	tf.flags |= ATA_TFLAG_POLLING;
1906c6fd2807SJeff Garzik 
1907963e4975SAlan Cox 	if (ap->ops->read_id)
1908963e4975SAlan Cox 		err_mask = ap->ops->read_id(dev, &tf, id);
1909963e4975SAlan Cox 	else
1910963e4975SAlan Cox 		err_mask = ata_do_dev_read_id(dev, &tf, id);
1911963e4975SAlan Cox 
1912c6fd2807SJeff Garzik 	if (err_mask) {
1913800b3996STejun Heo 		if (err_mask & AC_ERR_NODEV_HINT) {
1914a9a79dfeSJoe Perches 			ata_dev_dbg(dev, "NODEV after polling detection\n");
191555a8e2c8STejun Heo 			return -ENOENT;
191655a8e2c8STejun Heo 		}
191755a8e2c8STejun Heo 
191879b42babSTejun Heo 		if (is_semb) {
1919a9a79dfeSJoe Perches 			ata_dev_info(dev,
1920a9a79dfeSJoe Perches 		     "IDENTIFY failed on device w/ SEMB sig, disabled\n");
192179b42babSTejun Heo 			/* SEMB is not supported yet */
192279b42babSTejun Heo 			*p_class = ATA_DEV_SEMB_UNSUP;
192379b42babSTejun Heo 			return 0;
192479b42babSTejun Heo 		}
192579b42babSTejun Heo 
19261ffc151fSTejun Heo 		if ((err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
19271ffc151fSTejun Heo 			/* Device or controller might have reported
19281ffc151fSTejun Heo 			 * the wrong device class.  Give a shot at the
19291ffc151fSTejun Heo 			 * other IDENTIFY if the current one is
19301ffc151fSTejun Heo 			 * aborted by the device.
193154936f8bSTejun Heo 			 */
19321ffc151fSTejun Heo 			if (may_fallback) {
193354936f8bSTejun Heo 				may_fallback = 0;
193454936f8bSTejun Heo 
193554936f8bSTejun Heo 				if (class == ATA_DEV_ATA)
193654936f8bSTejun Heo 					class = ATA_DEV_ATAPI;
193754936f8bSTejun Heo 				else
193854936f8bSTejun Heo 					class = ATA_DEV_ATA;
193954936f8bSTejun Heo 				goto retry;
194054936f8bSTejun Heo 			}
194154936f8bSTejun Heo 
19421ffc151fSTejun Heo 			/* Control reaches here iff the device aborted
19431ffc151fSTejun Heo 			 * both flavors of IDENTIFYs which happens
19441ffc151fSTejun Heo 			 * sometimes with phantom devices.
19451ffc151fSTejun Heo 			 */
1946a9a79dfeSJoe Perches 			ata_dev_dbg(dev,
19471ffc151fSTejun Heo 				    "both IDENTIFYs aborted, assuming NODEV\n");
19481ffc151fSTejun Heo 			return -ENOENT;
19491ffc151fSTejun Heo 		}
19501ffc151fSTejun Heo 
1951c6fd2807SJeff Garzik 		rc = -EIO;
1952c6fd2807SJeff Garzik 		reason = "I/O error";
1953c6fd2807SJeff Garzik 		goto err_out;
1954c6fd2807SJeff Garzik 	}
1955c6fd2807SJeff Garzik 
195643c9c591STejun Heo 	if (dev->horkage & ATA_HORKAGE_DUMP_ID) {
1957a9a79dfeSJoe Perches 		ata_dev_dbg(dev, "dumping IDENTIFY data, "
195843c9c591STejun Heo 			    "class=%d may_fallback=%d tried_spinup=%d\n",
195943c9c591STejun Heo 			    class, may_fallback, tried_spinup);
196043c9c591STejun Heo 		print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET,
196143c9c591STejun Heo 			       16, 2, id, ATA_ID_WORDS * sizeof(*id), true);
196243c9c591STejun Heo 	}
196343c9c591STejun Heo 
196454936f8bSTejun Heo 	/* Falling back doesn't make sense if ID data was read
196554936f8bSTejun Heo 	 * successfully at least once.
196654936f8bSTejun Heo 	 */
196754936f8bSTejun Heo 	may_fallback = 0;
196854936f8bSTejun Heo 
1969c6fd2807SJeff Garzik 	swap_buf_le16(id, ATA_ID_WORDS);
1970c6fd2807SJeff Garzik 
1971c6fd2807SJeff Garzik 	/* sanity check */
1972c6fd2807SJeff Garzik 	rc = -EINVAL;
19736070068bSAlan Cox 	reason = "device reports invalid type";
19744a3381feSJeff Garzik 
19759162c657SHannes Reinecke 	if (class == ATA_DEV_ATA || class == ATA_DEV_ZAC) {
19764a3381feSJeff Garzik 		if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
19774a3381feSJeff Garzik 			goto err_out;
1978db63a4c8SAndy Whitcroft 		if (ap->host->flags & ATA_HOST_IGNORE_ATA &&
1979db63a4c8SAndy Whitcroft 							ata_id_is_ata(id)) {
1980db63a4c8SAndy Whitcroft 			ata_dev_dbg(dev,
1981db63a4c8SAndy Whitcroft 				"host indicates ignore ATA devices, ignored\n");
1982db63a4c8SAndy Whitcroft 			return -ENOENT;
1983db63a4c8SAndy Whitcroft 		}
19844a3381feSJeff Garzik 	} else {
19854a3381feSJeff Garzik 		if (ata_id_is_ata(id))
1986c6fd2807SJeff Garzik 			goto err_out;
1987c6fd2807SJeff Garzik 	}
1988c6fd2807SJeff Garzik 
1989169439c2SMark Lord 	if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
1990169439c2SMark Lord 		tried_spinup = 1;
1991169439c2SMark Lord 		/*
1992169439c2SMark Lord 		 * Drive powered-up in standby mode, and requires a specific
1993169439c2SMark Lord 		 * SET_FEATURES spin-up subcommand before it will accept
1994169439c2SMark Lord 		 * anything other than the original IDENTIFY command.
1995169439c2SMark Lord 		 */
1996218f3d30SJeff Garzik 		err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
1997fb0582f9SRyan Power 		if (err_mask && id[2] != 0x738c) {
1998169439c2SMark Lord 			rc = -EIO;
1999169439c2SMark Lord 			reason = "SPINUP failed";
2000169439c2SMark Lord 			goto err_out;
2001169439c2SMark Lord 		}
2002169439c2SMark Lord 		/*
2003169439c2SMark Lord 		 * If the drive initially returned incomplete IDENTIFY info,
2004169439c2SMark Lord 		 * we now must reissue the IDENTIFY command.
2005169439c2SMark Lord 		 */
2006169439c2SMark Lord 		if (id[2] == 0x37c8)
2007169439c2SMark Lord 			goto retry;
2008169439c2SMark Lord 	}
2009169439c2SMark Lord 
20109162c657SHannes Reinecke 	if ((flags & ATA_READID_POSTRESET) &&
20119162c657SHannes Reinecke 	    (class == ATA_DEV_ATA || class == ATA_DEV_ZAC)) {
2012c6fd2807SJeff Garzik 		/*
2013c6fd2807SJeff Garzik 		 * The exact sequence expected by certain pre-ATA4 drives is:
2014c6fd2807SJeff Garzik 		 * SRST RESET
201550a99018SAlan Cox 		 * IDENTIFY (optional in early ATA)
201650a99018SAlan Cox 		 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
2017c6fd2807SJeff Garzik 		 * anything else..
2018c6fd2807SJeff Garzik 		 * Some drives were very specific about that exact sequence.
201950a99018SAlan Cox 		 *
202050a99018SAlan Cox 		 * Note that ATA4 says lba is mandatory so the second check
2021c9404c9cSAdam Buchbinder 		 * should never trigger.
2022c6fd2807SJeff Garzik 		 */
2023c6fd2807SJeff Garzik 		if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
2024c6fd2807SJeff Garzik 			err_mask = ata_dev_init_params(dev, id[3], id[6]);
2025c6fd2807SJeff Garzik 			if (err_mask) {
2026c6fd2807SJeff Garzik 				rc = -EIO;
2027c6fd2807SJeff Garzik 				reason = "INIT_DEV_PARAMS failed";
2028c6fd2807SJeff Garzik 				goto err_out;
2029c6fd2807SJeff Garzik 			}
2030c6fd2807SJeff Garzik 
2031c6fd2807SJeff Garzik 			/* current CHS translation info (id[53-58]) might be
2032c6fd2807SJeff Garzik 			 * changed. reread the identify device info.
2033c6fd2807SJeff Garzik 			 */
2034bff04647STejun Heo 			flags &= ~ATA_READID_POSTRESET;
2035c6fd2807SJeff Garzik 			goto retry;
2036c6fd2807SJeff Garzik 		}
2037c6fd2807SJeff Garzik 	}
2038c6fd2807SJeff Garzik 
2039c6fd2807SJeff Garzik 	*p_class = class;
2040c6fd2807SJeff Garzik 
2041c6fd2807SJeff Garzik 	return 0;
2042c6fd2807SJeff Garzik 
2043c6fd2807SJeff Garzik  err_out:
2044c6fd2807SJeff Garzik 	if (ata_msg_warn(ap))
2045a9a79dfeSJoe Perches 		ata_dev_warn(dev, "failed to IDENTIFY (%s, err_mask=0x%x)\n",
2046a9a79dfeSJoe Perches 			     reason, err_mask);
2047c6fd2807SJeff Garzik 	return rc;
2048c6fd2807SJeff Garzik }
2049c6fd2807SJeff Garzik 
20509062712fSTejun Heo static int ata_do_link_spd_horkage(struct ata_device *dev)
20519062712fSTejun Heo {
20529062712fSTejun Heo 	struct ata_link *plink = ata_dev_phys_link(dev);
20539062712fSTejun Heo 	u32 target, target_limit;
20549062712fSTejun Heo 
20559062712fSTejun Heo 	if (!sata_scr_valid(plink))
20569062712fSTejun Heo 		return 0;
20579062712fSTejun Heo 
20589062712fSTejun Heo 	if (dev->horkage & ATA_HORKAGE_1_5_GBPS)
20599062712fSTejun Heo 		target = 1;
20609062712fSTejun Heo 	else
20619062712fSTejun Heo 		return 0;
20629062712fSTejun Heo 
20639062712fSTejun Heo 	target_limit = (1 << target) - 1;
20649062712fSTejun Heo 
20659062712fSTejun Heo 	/* if already on stricter limit, no need to push further */
20669062712fSTejun Heo 	if (plink->sata_spd_limit <= target_limit)
20679062712fSTejun Heo 		return 0;
20689062712fSTejun Heo 
20699062712fSTejun Heo 	plink->sata_spd_limit = target_limit;
20709062712fSTejun Heo 
20719062712fSTejun Heo 	/* Request another EH round by returning -EAGAIN if link is
20729062712fSTejun Heo 	 * going faster than the target speed.  Forward progress is
20739062712fSTejun Heo 	 * guaranteed by setting sata_spd_limit to target_limit above.
20749062712fSTejun Heo 	 */
20759062712fSTejun Heo 	if (plink->sata_spd > target) {
2076a9a79dfeSJoe Perches 		ata_dev_info(dev, "applying link speed limit horkage to %s\n",
20779062712fSTejun Heo 			     sata_spd_string(target));
20789062712fSTejun Heo 		return -EAGAIN;
20799062712fSTejun Heo 	}
20809062712fSTejun Heo 	return 0;
20819062712fSTejun Heo }
20829062712fSTejun Heo 
2083c6fd2807SJeff Garzik static inline u8 ata_dev_knobble(struct ata_device *dev)
2084c6fd2807SJeff Garzik {
20859af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
20869ce8e307SJens Axboe 
20879ce8e307SJens Axboe 	if (ata_dev_blacklisted(dev) & ATA_HORKAGE_BRIDGE_OK)
20889ce8e307SJens Axboe 		return 0;
20899ce8e307SJens Axboe 
20909af5c9c9STejun Heo 	return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
2091c6fd2807SJeff Garzik }
2092c6fd2807SJeff Garzik 
20935a233551SHannes Reinecke static void ata_dev_config_ncq_send_recv(struct ata_device *dev)
20945a233551SHannes Reinecke {
20955a233551SHannes Reinecke 	struct ata_port *ap = dev->link->ap;
20965a233551SHannes Reinecke 	unsigned int err_mask;
2097fe5af0ccSHannes Reinecke 	int log_index = ATA_LOG_NCQ_SEND_RECV * 2;
2098fe5af0ccSHannes Reinecke 	u16 log_pages;
20995a233551SHannes Reinecke 
2100fe5af0ccSHannes Reinecke 	err_mask = ata_read_log_page(dev, ATA_LOG_DIRECTORY,
2101fe5af0ccSHannes Reinecke 				     0, ap->sector_buf, 1);
2102fe5af0ccSHannes Reinecke 	if (err_mask) {
2103fe5af0ccSHannes Reinecke 		ata_dev_dbg(dev,
2104fe5af0ccSHannes Reinecke 			    "failed to get Log Directory Emask 0x%x\n",
2105fe5af0ccSHannes Reinecke 			    err_mask);
2106fe5af0ccSHannes Reinecke 		return;
2107fe5af0ccSHannes Reinecke 	}
2108fe5af0ccSHannes Reinecke 	log_pages = get_unaligned_le16(&ap->sector_buf[log_index]);
2109fe5af0ccSHannes Reinecke 	if (!log_pages) {
2110fe5af0ccSHannes Reinecke 		ata_dev_warn(dev,
2111fe5af0ccSHannes Reinecke 			     "NCQ Send/Recv Log not supported\n");
2112fe5af0ccSHannes Reinecke 		return;
2113fe5af0ccSHannes Reinecke 	}
21145a233551SHannes Reinecke 	err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_SEND_RECV,
21155a233551SHannes Reinecke 				     0, ap->sector_buf, 1);
21165a233551SHannes Reinecke 	if (err_mask) {
21175a233551SHannes Reinecke 		ata_dev_dbg(dev,
21185a233551SHannes Reinecke 			    "failed to get NCQ Send/Recv Log Emask 0x%x\n",
21195a233551SHannes Reinecke 			    err_mask);
21205a233551SHannes Reinecke 	} else {
21215a233551SHannes Reinecke 		u8 *cmds = dev->ncq_send_recv_cmds;
21225a233551SHannes Reinecke 
21235a233551SHannes Reinecke 		dev->flags |= ATA_DFLAG_NCQ_SEND_RECV;
21245a233551SHannes Reinecke 		memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_SEND_RECV_SIZE);
21255a233551SHannes Reinecke 
21265a233551SHannes Reinecke 		if (dev->horkage & ATA_HORKAGE_NO_NCQ_TRIM) {
21275a233551SHannes Reinecke 			ata_dev_dbg(dev, "disabling queued TRIM support\n");
21285a233551SHannes Reinecke 			cmds[ATA_LOG_NCQ_SEND_RECV_DSM_OFFSET] &=
21295a233551SHannes Reinecke 				~ATA_LOG_NCQ_SEND_RECV_DSM_TRIM;
21305a233551SHannes Reinecke 		}
21315a233551SHannes Reinecke 	}
21325a233551SHannes Reinecke }
21335a233551SHannes Reinecke 
2134284b3b77SHannes Reinecke static void ata_dev_config_ncq_non_data(struct ata_device *dev)
2135284b3b77SHannes Reinecke {
2136284b3b77SHannes Reinecke 	struct ata_port *ap = dev->link->ap;
2137284b3b77SHannes Reinecke 	unsigned int err_mask;
2138284b3b77SHannes Reinecke 	int log_index = ATA_LOG_NCQ_NON_DATA * 2;
2139284b3b77SHannes Reinecke 	u16 log_pages;
2140284b3b77SHannes Reinecke 
2141284b3b77SHannes Reinecke 	err_mask = ata_read_log_page(dev, ATA_LOG_DIRECTORY,
2142284b3b77SHannes Reinecke 				     0, ap->sector_buf, 1);
2143284b3b77SHannes Reinecke 	if (err_mask) {
2144284b3b77SHannes Reinecke 		ata_dev_dbg(dev,
2145284b3b77SHannes Reinecke 			    "failed to get Log Directory Emask 0x%x\n",
2146284b3b77SHannes Reinecke 			    err_mask);
2147284b3b77SHannes Reinecke 		return;
2148284b3b77SHannes Reinecke 	}
2149284b3b77SHannes Reinecke 	log_pages = get_unaligned_le16(&ap->sector_buf[log_index]);
2150284b3b77SHannes Reinecke 	if (!log_pages) {
2151284b3b77SHannes Reinecke 		ata_dev_warn(dev,
2152284b3b77SHannes Reinecke 			     "NCQ Send/Recv Log not supported\n");
2153284b3b77SHannes Reinecke 		return;
2154284b3b77SHannes Reinecke 	}
2155284b3b77SHannes Reinecke 	err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_NON_DATA,
2156284b3b77SHannes Reinecke 				     0, ap->sector_buf, 1);
2157284b3b77SHannes Reinecke 	if (err_mask) {
2158284b3b77SHannes Reinecke 		ata_dev_dbg(dev,
2159284b3b77SHannes Reinecke 			    "failed to get NCQ Non-Data Log Emask 0x%x\n",
2160284b3b77SHannes Reinecke 			    err_mask);
2161284b3b77SHannes Reinecke 	} else {
2162284b3b77SHannes Reinecke 		u8 *cmds = dev->ncq_non_data_cmds;
2163284b3b77SHannes Reinecke 
2164284b3b77SHannes Reinecke 		memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_NON_DATA_SIZE);
2165284b3b77SHannes Reinecke 	}
2166284b3b77SHannes Reinecke }
2167284b3b77SHannes Reinecke 
21688e061784SAdam Manzanares static void ata_dev_config_ncq_prio(struct ata_device *dev)
21698e061784SAdam Manzanares {
21708e061784SAdam Manzanares 	struct ata_port *ap = dev->link->ap;
21718e061784SAdam Manzanares 	unsigned int err_mask;
21728e061784SAdam Manzanares 
21739f56eca3SAdam Manzanares 	if (!(dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLE)) {
21749f56eca3SAdam Manzanares 		dev->flags &= ~ATA_DFLAG_NCQ_PRIO;
21759f56eca3SAdam Manzanares 		return;
21769f56eca3SAdam Manzanares 	}
21779f56eca3SAdam Manzanares 
21788e061784SAdam Manzanares 	err_mask = ata_read_log_page(dev,
21798e061784SAdam Manzanares 				     ATA_LOG_SATA_ID_DEV_DATA,
21808e061784SAdam Manzanares 				     ATA_LOG_SATA_SETTINGS,
21818e061784SAdam Manzanares 				     ap->sector_buf,
21828e061784SAdam Manzanares 				     1);
21838e061784SAdam Manzanares 	if (err_mask) {
21848e061784SAdam Manzanares 		ata_dev_dbg(dev,
21858e061784SAdam Manzanares 			    "failed to get Identify Device data, Emask 0x%x\n",
21868e061784SAdam Manzanares 			    err_mask);
21878e061784SAdam Manzanares 		return;
21888e061784SAdam Manzanares 	}
21898e061784SAdam Manzanares 
21909f56eca3SAdam Manzanares 	if (ap->sector_buf[ATA_LOG_NCQ_PRIO_OFFSET] & BIT(3)) {
21918e061784SAdam Manzanares 		dev->flags |= ATA_DFLAG_NCQ_PRIO;
21929f56eca3SAdam Manzanares 	} else {
21939f56eca3SAdam Manzanares 		dev->flags &= ~ATA_DFLAG_NCQ_PRIO;
21948e061784SAdam Manzanares 		ata_dev_dbg(dev, "SATA page does not support priority\n");
21959f56eca3SAdam Manzanares 	}
21968e061784SAdam Manzanares 
21978e061784SAdam Manzanares }
21988e061784SAdam Manzanares 
2199388539f3SShaohua Li static int ata_dev_config_ncq(struct ata_device *dev,
2200c6fd2807SJeff Garzik 			       char *desc, size_t desc_sz)
2201c6fd2807SJeff Garzik {
22029af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
2203c6fd2807SJeff Garzik 	int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
2204388539f3SShaohua Li 	unsigned int err_mask;
2205388539f3SShaohua Li 	char *aa_desc = "";
2206c6fd2807SJeff Garzik 
2207c6fd2807SJeff Garzik 	if (!ata_id_has_ncq(dev->id)) {
2208c6fd2807SJeff Garzik 		desc[0] = '\0';
2209388539f3SShaohua Li 		return 0;
2210c6fd2807SJeff Garzik 	}
221175683fe7STejun Heo 	if (dev->horkage & ATA_HORKAGE_NONCQ) {
22126919a0a6SAlan Cox 		snprintf(desc, desc_sz, "NCQ (not used)");
2213388539f3SShaohua Li 		return 0;
22146919a0a6SAlan Cox 	}
2215c6fd2807SJeff Garzik 	if (ap->flags & ATA_FLAG_NCQ) {
2216cca3974eSJeff Garzik 		hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE - 1);
2217c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_NCQ;
2218c6fd2807SJeff Garzik 	}
2219c6fd2807SJeff Garzik 
2220388539f3SShaohua Li 	if (!(dev->horkage & ATA_HORKAGE_BROKEN_FPDMA_AA) &&
2221388539f3SShaohua Li 		(ap->flags & ATA_FLAG_FPDMA_AA) &&
2222388539f3SShaohua Li 		ata_id_has_fpdma_aa(dev->id)) {
2223388539f3SShaohua Li 		err_mask = ata_dev_set_feature(dev, SETFEATURES_SATA_ENABLE,
2224388539f3SShaohua Li 			SATA_FPDMA_AA);
2225388539f3SShaohua Li 		if (err_mask) {
2226a9a79dfeSJoe Perches 			ata_dev_err(dev,
2227a9a79dfeSJoe Perches 				    "failed to enable AA (error_mask=0x%x)\n",
2228a9a79dfeSJoe Perches 				    err_mask);
2229388539f3SShaohua Li 			if (err_mask != AC_ERR_DEV) {
2230388539f3SShaohua Li 				dev->horkage |= ATA_HORKAGE_BROKEN_FPDMA_AA;
2231388539f3SShaohua Li 				return -EIO;
2232388539f3SShaohua Li 			}
2233388539f3SShaohua Li 		} else
2234388539f3SShaohua Li 			aa_desc = ", AA";
2235388539f3SShaohua Li 	}
2236388539f3SShaohua Li 
2237c6fd2807SJeff Garzik 	if (hdepth >= ddepth)
2238388539f3SShaohua Li 		snprintf(desc, desc_sz, "NCQ (depth %d)%s", ddepth, aa_desc);
2239c6fd2807SJeff Garzik 	else
2240388539f3SShaohua Li 		snprintf(desc, desc_sz, "NCQ (depth %d/%d)%s", hdepth,
2241388539f3SShaohua Li 			ddepth, aa_desc);
2242ed36911cSMarc Carino 
2243284b3b77SHannes Reinecke 	if ((ap->flags & ATA_FLAG_FPDMA_AUX)) {
2244284b3b77SHannes Reinecke 		if (ata_id_has_ncq_send_and_recv(dev->id))
22455a233551SHannes Reinecke 			ata_dev_config_ncq_send_recv(dev);
2246284b3b77SHannes Reinecke 		if (ata_id_has_ncq_non_data(dev->id))
2247284b3b77SHannes Reinecke 			ata_dev_config_ncq_non_data(dev);
22488e061784SAdam Manzanares 		if (ata_id_has_ncq_prio(dev->id))
22498e061784SAdam Manzanares 			ata_dev_config_ncq_prio(dev);
2250ed36911cSMarc Carino 	}
2251ed36911cSMarc Carino 
2252388539f3SShaohua Li 	return 0;
2253c6fd2807SJeff Garzik }
2254c6fd2807SJeff Garzik 
2255e87fd28cSHannes Reinecke static void ata_dev_config_sense_reporting(struct ata_device *dev)
2256e87fd28cSHannes Reinecke {
2257e87fd28cSHannes Reinecke 	unsigned int err_mask;
2258e87fd28cSHannes Reinecke 
2259e87fd28cSHannes Reinecke 	if (!ata_id_has_sense_reporting(dev->id))
2260e87fd28cSHannes Reinecke 		return;
2261e87fd28cSHannes Reinecke 
2262e87fd28cSHannes Reinecke 	if (ata_id_sense_reporting_enabled(dev->id))
2263e87fd28cSHannes Reinecke 		return;
2264e87fd28cSHannes Reinecke 
2265e87fd28cSHannes Reinecke 	err_mask = ata_dev_set_feature(dev, SETFEATURE_SENSE_DATA, 0x1);
2266e87fd28cSHannes Reinecke 	if (err_mask) {
2267e87fd28cSHannes Reinecke 		ata_dev_dbg(dev,
2268e87fd28cSHannes Reinecke 			    "failed to enable Sense Data Reporting, Emask 0x%x\n",
2269e87fd28cSHannes Reinecke 			    err_mask);
2270e87fd28cSHannes Reinecke 	}
2271e87fd28cSHannes Reinecke }
2272e87fd28cSHannes Reinecke 
22736d1003aeSHannes Reinecke static void ata_dev_config_zac(struct ata_device *dev)
22746d1003aeSHannes Reinecke {
22756d1003aeSHannes Reinecke 	struct ata_port *ap = dev->link->ap;
22766d1003aeSHannes Reinecke 	unsigned int err_mask;
22776d1003aeSHannes Reinecke 	u8 *identify_buf = ap->sector_buf;
22786d1003aeSHannes Reinecke 	int log_index = ATA_LOG_SATA_ID_DEV_DATA * 2, i, found = 0;
22796d1003aeSHannes Reinecke 	u16 log_pages;
22806d1003aeSHannes Reinecke 
22816d1003aeSHannes Reinecke 	dev->zac_zones_optimal_open = U32_MAX;
22826d1003aeSHannes Reinecke 	dev->zac_zones_optimal_nonseq = U32_MAX;
22836d1003aeSHannes Reinecke 	dev->zac_zones_max_open = U32_MAX;
22846d1003aeSHannes Reinecke 
22856d1003aeSHannes Reinecke 	/*
22866d1003aeSHannes Reinecke 	 * Always set the 'ZAC' flag for Host-managed devices.
22876d1003aeSHannes Reinecke 	 */
22886d1003aeSHannes Reinecke 	if (dev->class == ATA_DEV_ZAC)
22896d1003aeSHannes Reinecke 		dev->flags |= ATA_DFLAG_ZAC;
22906d1003aeSHannes Reinecke 	else if (ata_id_zoned_cap(dev->id) == 0x01)
22916d1003aeSHannes Reinecke 		/*
22926d1003aeSHannes Reinecke 		 * Check for host-aware devices.
22936d1003aeSHannes Reinecke 		 */
22946d1003aeSHannes Reinecke 		dev->flags |= ATA_DFLAG_ZAC;
22956d1003aeSHannes Reinecke 
22966d1003aeSHannes Reinecke 	if (!(dev->flags & ATA_DFLAG_ZAC))
22976d1003aeSHannes Reinecke 		return;
22986d1003aeSHannes Reinecke 
22996d1003aeSHannes Reinecke 	/*
23006d1003aeSHannes Reinecke 	 * Read Log Directory to figure out if IDENTIFY DEVICE log
23016d1003aeSHannes Reinecke 	 * is supported.
23026d1003aeSHannes Reinecke 	 */
23036d1003aeSHannes Reinecke 	err_mask = ata_read_log_page(dev, ATA_LOG_DIRECTORY,
23046d1003aeSHannes Reinecke 				     0, ap->sector_buf, 1);
23056d1003aeSHannes Reinecke 	if (err_mask) {
23066d1003aeSHannes Reinecke 		ata_dev_info(dev,
23076d1003aeSHannes Reinecke 			     "failed to get Log Directory Emask 0x%x\n",
23086d1003aeSHannes Reinecke 			     err_mask);
23096d1003aeSHannes Reinecke 		return;
23106d1003aeSHannes Reinecke 	}
23116d1003aeSHannes Reinecke 	log_pages = get_unaligned_le16(&ap->sector_buf[log_index]);
23126d1003aeSHannes Reinecke 	if (log_pages == 0) {
23136d1003aeSHannes Reinecke 		ata_dev_warn(dev,
23146d1003aeSHannes Reinecke 			     "ATA Identify Device Log not supported\n");
23156d1003aeSHannes Reinecke 		return;
23166d1003aeSHannes Reinecke 	}
23176d1003aeSHannes Reinecke 	/*
23186d1003aeSHannes Reinecke 	 * Read IDENTIFY DEVICE data log, page 0, to figure out
23196d1003aeSHannes Reinecke 	 * if page 9 is supported.
23206d1003aeSHannes Reinecke 	 */
23216d1003aeSHannes Reinecke 	err_mask = ata_read_log_page(dev, ATA_LOG_SATA_ID_DEV_DATA, 0,
23226d1003aeSHannes Reinecke 				     identify_buf, 1);
23236d1003aeSHannes Reinecke 	if (err_mask) {
23246d1003aeSHannes Reinecke 		ata_dev_info(dev,
23256d1003aeSHannes Reinecke 			     "failed to get Device Identify Log Emask 0x%x\n",
23266d1003aeSHannes Reinecke 			     err_mask);
23276d1003aeSHannes Reinecke 		return;
23286d1003aeSHannes Reinecke 	}
23296d1003aeSHannes Reinecke 	log_pages = identify_buf[8];
23306d1003aeSHannes Reinecke 	for (i = 0; i < log_pages; i++) {
23316d1003aeSHannes Reinecke 		if (identify_buf[9 + i] == ATA_LOG_ZONED_INFORMATION) {
23326d1003aeSHannes Reinecke 			found++;
23336d1003aeSHannes Reinecke 			break;
23346d1003aeSHannes Reinecke 		}
23356d1003aeSHannes Reinecke 	}
23366d1003aeSHannes Reinecke 	if (!found) {
23376d1003aeSHannes Reinecke 		ata_dev_warn(dev,
23386d1003aeSHannes Reinecke 			     "ATA Zoned Information Log not supported\n");
23396d1003aeSHannes Reinecke 		return;
23406d1003aeSHannes Reinecke 	}
23416d1003aeSHannes Reinecke 
23426d1003aeSHannes Reinecke 	/*
23436d1003aeSHannes Reinecke 	 * Read IDENTIFY DEVICE data log, page 9 (Zoned-device information)
23446d1003aeSHannes Reinecke 	 */
23456d1003aeSHannes Reinecke 	err_mask = ata_read_log_page(dev, ATA_LOG_SATA_ID_DEV_DATA,
23466d1003aeSHannes Reinecke 				     ATA_LOG_ZONED_INFORMATION,
23476d1003aeSHannes Reinecke 				     identify_buf, 1);
23486d1003aeSHannes Reinecke 	if (!err_mask) {
23496d1003aeSHannes Reinecke 		u64 zoned_cap, opt_open, opt_nonseq, max_open;
23506d1003aeSHannes Reinecke 
23516d1003aeSHannes Reinecke 		zoned_cap = get_unaligned_le64(&identify_buf[8]);
23526d1003aeSHannes Reinecke 		if ((zoned_cap >> 63))
23536d1003aeSHannes Reinecke 			dev->zac_zoned_cap = (zoned_cap & 1);
23546d1003aeSHannes Reinecke 		opt_open = get_unaligned_le64(&identify_buf[24]);
23556d1003aeSHannes Reinecke 		if ((opt_open >> 63))
23566d1003aeSHannes Reinecke 			dev->zac_zones_optimal_open = (u32)opt_open;
23576d1003aeSHannes Reinecke 		opt_nonseq = get_unaligned_le64(&identify_buf[32]);
23586d1003aeSHannes Reinecke 		if ((opt_nonseq >> 63))
23596d1003aeSHannes Reinecke 			dev->zac_zones_optimal_nonseq = (u32)opt_nonseq;
23606d1003aeSHannes Reinecke 		max_open = get_unaligned_le64(&identify_buf[40]);
23616d1003aeSHannes Reinecke 		if ((max_open >> 63))
23626d1003aeSHannes Reinecke 			dev->zac_zones_max_open = (u32)max_open;
23636d1003aeSHannes Reinecke 	}
23646d1003aeSHannes Reinecke }
23656d1003aeSHannes Reinecke 
2366c6fd2807SJeff Garzik /**
2367c6fd2807SJeff Garzik  *	ata_dev_configure - Configure the specified ATA/ATAPI device
2368c6fd2807SJeff Garzik  *	@dev: Target device to configure
2369c6fd2807SJeff Garzik  *
2370c6fd2807SJeff Garzik  *	Configure @dev according to @dev->id.  Generic and low-level
2371c6fd2807SJeff Garzik  *	driver specific fixups are also applied.
2372c6fd2807SJeff Garzik  *
2373c6fd2807SJeff Garzik  *	LOCKING:
2374c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
2375c6fd2807SJeff Garzik  *
2376c6fd2807SJeff Garzik  *	RETURNS:
2377c6fd2807SJeff Garzik  *	0 on success, -errno otherwise
2378c6fd2807SJeff Garzik  */
2379efdaedc4STejun Heo int ata_dev_configure(struct ata_device *dev)
2380c6fd2807SJeff Garzik {
23819af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
23829af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
23836746544cSTejun Heo 	int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
2384c6fd2807SJeff Garzik 	const u16 *id = dev->id;
23857dc951aeSTejun Heo 	unsigned long xfer_mask;
238665fe1f0fSShane Huang 	unsigned int err_mask;
2387b352e57dSAlan Cox 	char revbuf[7];		/* XYZ-99\0 */
23883f64f565SEric D. Mudama 	char fwrevbuf[ATA_ID_FW_REV_LEN+1];
23893f64f565SEric D. Mudama 	char modelbuf[ATA_ID_PROD_LEN+1];
2390c6fd2807SJeff Garzik 	int rc;
2391c6fd2807SJeff Garzik 
2392c6fd2807SJeff Garzik 	if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
2393a9a79dfeSJoe Perches 		ata_dev_info(dev, "%s: ENTER/EXIT -- nodev\n", __func__);
2394c6fd2807SJeff Garzik 		return 0;
2395c6fd2807SJeff Garzik 	}
2396c6fd2807SJeff Garzik 
2397c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2398a9a79dfeSJoe Perches 		ata_dev_dbg(dev, "%s: ENTER\n", __func__);
2399c6fd2807SJeff Garzik 
240075683fe7STejun Heo 	/* set horkage */
240175683fe7STejun Heo 	dev->horkage |= ata_dev_blacklisted(dev);
240233267325STejun Heo 	ata_force_horkage(dev);
240375683fe7STejun Heo 
240450af2fa1STejun Heo 	if (dev->horkage & ATA_HORKAGE_DISABLE) {
2405a9a79dfeSJoe Perches 		ata_dev_info(dev, "unsupported device, disabling\n");
240650af2fa1STejun Heo 		ata_dev_disable(dev);
240750af2fa1STejun Heo 		return 0;
240850af2fa1STejun Heo 	}
240950af2fa1STejun Heo 
24102486fa56STejun Heo 	if ((!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) &&
24112486fa56STejun Heo 	    dev->class == ATA_DEV_ATAPI) {
2412a9a79dfeSJoe Perches 		ata_dev_warn(dev, "WARNING: ATAPI is %s, device ignored\n",
24132486fa56STejun Heo 			     atapi_enabled ? "not supported with this driver"
24142486fa56STejun Heo 			     : "disabled");
24152486fa56STejun Heo 		ata_dev_disable(dev);
24162486fa56STejun Heo 		return 0;
24172486fa56STejun Heo 	}
24182486fa56STejun Heo 
24199062712fSTejun Heo 	rc = ata_do_link_spd_horkage(dev);
24209062712fSTejun Heo 	if (rc)
24219062712fSTejun Heo 		return rc;
24229062712fSTejun Heo 
2423ecd75ad5STejun Heo 	/* some WD SATA-1 drives have issues with LPM, turn on NOLPM for them */
2424ecd75ad5STejun Heo 	if ((dev->horkage & ATA_HORKAGE_WD_BROKEN_LPM) &&
2425ecd75ad5STejun Heo 	    (id[ATA_ID_SATA_CAPABILITY] & 0xe) == 0x2)
2426ecd75ad5STejun Heo 		dev->horkage |= ATA_HORKAGE_NOLPM;
2427ecd75ad5STejun Heo 
2428ecd75ad5STejun Heo 	if (dev->horkage & ATA_HORKAGE_NOLPM) {
2429ecd75ad5STejun Heo 		ata_dev_warn(dev, "LPM support broken, forcing max_power\n");
2430ecd75ad5STejun Heo 		dev->link->ap->target_lpm_policy = ATA_LPM_MAX_POWER;
2431ecd75ad5STejun Heo 	}
2432ecd75ad5STejun Heo 
24336746544cSTejun Heo 	/* let ACPI work its magic */
24346746544cSTejun Heo 	rc = ata_acpi_on_devcfg(dev);
24356746544cSTejun Heo 	if (rc)
24366746544cSTejun Heo 		return rc;
243708573a86SKristen Carlson Accardi 
243805027adcSTejun Heo 	/* massage HPA, do it early as it might change IDENTIFY data */
243905027adcSTejun Heo 	rc = ata_hpa_resize(dev);
244005027adcSTejun Heo 	if (rc)
244105027adcSTejun Heo 		return rc;
244205027adcSTejun Heo 
2443c6fd2807SJeff Garzik 	/* print device capabilities */
2444c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2445a9a79dfeSJoe Perches 		ata_dev_dbg(dev,
2446c6fd2807SJeff Garzik 			    "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
2447c6fd2807SJeff Garzik 			    "85:%04x 86:%04x 87:%04x 88:%04x\n",
24487f5e4e8dSHarvey Harrison 			    __func__,
2449c6fd2807SJeff Garzik 			    id[49], id[82], id[83], id[84],
2450c6fd2807SJeff Garzik 			    id[85], id[86], id[87], id[88]);
2451c6fd2807SJeff Garzik 
2452c6fd2807SJeff Garzik 	/* initialize to-be-configured parameters */
2453c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_CFG_MASK;
2454c6fd2807SJeff Garzik 	dev->max_sectors = 0;
2455c6fd2807SJeff Garzik 	dev->cdb_len = 0;
2456c6fd2807SJeff Garzik 	dev->n_sectors = 0;
2457c6fd2807SJeff Garzik 	dev->cylinders = 0;
2458c6fd2807SJeff Garzik 	dev->heads = 0;
2459c6fd2807SJeff Garzik 	dev->sectors = 0;
2460e18086d6SMark Lord 	dev->multi_count = 0;
2461c6fd2807SJeff Garzik 
2462c6fd2807SJeff Garzik 	/*
2463c6fd2807SJeff Garzik 	 * common ATA, ATAPI feature tests
2464c6fd2807SJeff Garzik 	 */
2465c6fd2807SJeff Garzik 
2466c6fd2807SJeff Garzik 	/* find max transfer mode; for printk only */
2467c6fd2807SJeff Garzik 	xfer_mask = ata_id_xfermask(id);
2468c6fd2807SJeff Garzik 
2469c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2470c6fd2807SJeff Garzik 		ata_dump_id(id);
2471c6fd2807SJeff Garzik 
2472ef143d57SAlbert Lee 	/* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
2473ef143d57SAlbert Lee 	ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
2474ef143d57SAlbert Lee 			sizeof(fwrevbuf));
2475ef143d57SAlbert Lee 
2476ef143d57SAlbert Lee 	ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
2477ef143d57SAlbert Lee 			sizeof(modelbuf));
2478ef143d57SAlbert Lee 
2479c6fd2807SJeff Garzik 	/* ATA-specific feature tests */
24809162c657SHannes Reinecke 	if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) {
2481b352e57dSAlan Cox 		if (ata_id_is_cfa(id)) {
248262afe5d7SSergei Shtylyov 			/* CPRM may make this media unusable */
248362afe5d7SSergei Shtylyov 			if (id[ATA_ID_CFA_KEY_MGMT] & 1)
2484a9a79dfeSJoe Perches 				ata_dev_warn(dev,
2485a9a79dfeSJoe Perches 	"supports DRM functions and may not be fully accessible\n");
2486b352e57dSAlan Cox 			snprintf(revbuf, 7, "CFA");
2487ae8d4ee7SAlan Cox 		} else {
2488b352e57dSAlan Cox 			snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
2489ae8d4ee7SAlan Cox 			/* Warn the user if the device has TPM extensions */
2490ae8d4ee7SAlan Cox 			if (ata_id_has_tpm(id))
2491a9a79dfeSJoe Perches 				ata_dev_warn(dev,
2492a9a79dfeSJoe Perches 	"supports DRM functions and may not be fully accessible\n");
2493ae8d4ee7SAlan Cox 		}
2494b352e57dSAlan Cox 
2495c6fd2807SJeff Garzik 		dev->n_sectors = ata_id_n_sectors(id);
2496c6fd2807SJeff Garzik 
2497e18086d6SMark Lord 		/* get current R/W Multiple count setting */
2498e18086d6SMark Lord 		if ((dev->id[47] >> 8) == 0x80 && (dev->id[59] & 0x100)) {
2499e18086d6SMark Lord 			unsigned int max = dev->id[47] & 0xff;
2500e18086d6SMark Lord 			unsigned int cnt = dev->id[59] & 0xff;
2501e18086d6SMark Lord 			/* only recognize/allow powers of two here */
2502e18086d6SMark Lord 			if (is_power_of_2(max) && is_power_of_2(cnt))
2503e18086d6SMark Lord 				if (cnt <= max)
2504e18086d6SMark Lord 					dev->multi_count = cnt;
2505e18086d6SMark Lord 		}
25063f64f565SEric D. Mudama 
2507c6fd2807SJeff Garzik 		if (ata_id_has_lba(id)) {
2508c6fd2807SJeff Garzik 			const char *lba_desc;
2509388539f3SShaohua Li 			char ncq_desc[24];
2510c6fd2807SJeff Garzik 
2511c6fd2807SJeff Garzik 			lba_desc = "LBA";
2512c6fd2807SJeff Garzik 			dev->flags |= ATA_DFLAG_LBA;
2513c6fd2807SJeff Garzik 			if (ata_id_has_lba48(id)) {
2514c6fd2807SJeff Garzik 				dev->flags |= ATA_DFLAG_LBA48;
2515c6fd2807SJeff Garzik 				lba_desc = "LBA48";
25166fc49adbSTejun Heo 
25176fc49adbSTejun Heo 				if (dev->n_sectors >= (1UL << 28) &&
25186fc49adbSTejun Heo 				    ata_id_has_flush_ext(id))
25196fc49adbSTejun Heo 					dev->flags |= ATA_DFLAG_FLUSH_EXT;
2520c6fd2807SJeff Garzik 			}
2521c6fd2807SJeff Garzik 
2522c6fd2807SJeff Garzik 			/* config NCQ */
2523388539f3SShaohua Li 			rc = ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
2524388539f3SShaohua Li 			if (rc)
2525388539f3SShaohua Li 				return rc;
2526c6fd2807SJeff Garzik 
2527c6fd2807SJeff Garzik 			/* print device info to dmesg */
25283f64f565SEric D. Mudama 			if (ata_msg_drv(ap) && print_info) {
2529a9a79dfeSJoe Perches 				ata_dev_info(dev, "%s: %s, %s, max %s\n",
25303f64f565SEric D. Mudama 					     revbuf, modelbuf, fwrevbuf,
25313f64f565SEric D. Mudama 					     ata_mode_string(xfer_mask));
2532a9a79dfeSJoe Perches 				ata_dev_info(dev,
2533a9a79dfeSJoe Perches 					     "%llu sectors, multi %u: %s %s\n",
2534c6fd2807SJeff Garzik 					(unsigned long long)dev->n_sectors,
25353f64f565SEric D. Mudama 					dev->multi_count, lba_desc, ncq_desc);
25363f64f565SEric D. Mudama 			}
2537c6fd2807SJeff Garzik 		} else {
2538c6fd2807SJeff Garzik 			/* CHS */
2539c6fd2807SJeff Garzik 
2540c6fd2807SJeff Garzik 			/* Default translation */
2541c6fd2807SJeff Garzik 			dev->cylinders	= id[1];
2542c6fd2807SJeff Garzik 			dev->heads	= id[3];
2543c6fd2807SJeff Garzik 			dev->sectors	= id[6];
2544c6fd2807SJeff Garzik 
2545c6fd2807SJeff Garzik 			if (ata_id_current_chs_valid(id)) {
2546c6fd2807SJeff Garzik 				/* Current CHS translation is valid. */
2547c6fd2807SJeff Garzik 				dev->cylinders = id[54];
2548c6fd2807SJeff Garzik 				dev->heads     = id[55];
2549c6fd2807SJeff Garzik 				dev->sectors   = id[56];
2550c6fd2807SJeff Garzik 			}
2551c6fd2807SJeff Garzik 
2552c6fd2807SJeff Garzik 			/* print device info to dmesg */
25533f64f565SEric D. Mudama 			if (ata_msg_drv(ap) && print_info) {
2554a9a79dfeSJoe Perches 				ata_dev_info(dev, "%s: %s, %s, max %s\n",
25553f64f565SEric D. Mudama 					     revbuf,	modelbuf, fwrevbuf,
25563f64f565SEric D. Mudama 					     ata_mode_string(xfer_mask));
2557a9a79dfeSJoe Perches 				ata_dev_info(dev,
2558a9a79dfeSJoe Perches 					     "%llu sectors, multi %u, CHS %u/%u/%u\n",
25593f64f565SEric D. Mudama 					     (unsigned long long)dev->n_sectors,
25603f64f565SEric D. Mudama 					     dev->multi_count, dev->cylinders,
25613f64f565SEric D. Mudama 					     dev->heads, dev->sectors);
25623f64f565SEric D. Mudama 			}
2563c6fd2807SJeff Garzik 		}
2564c6fd2807SJeff Garzik 
2565803739d2SShane Huang 		/* Check and mark DevSlp capability. Get DevSlp timing variables
2566803739d2SShane Huang 		 * from SATA Settings page of Identify Device Data Log.
256765fe1f0fSShane Huang 		 */
2568803739d2SShane Huang 		if (ata_id_has_devslp(dev->id)) {
25698e725c7fSDavid Woodhouse 			u8 *sata_setting = ap->sector_buf;
2570803739d2SShane Huang 			int i, j;
2571803739d2SShane Huang 
2572803739d2SShane Huang 			dev->flags |= ATA_DFLAG_DEVSLP;
257365fe1f0fSShane Huang 			err_mask = ata_read_log_page(dev,
257465fe1f0fSShane Huang 						     ATA_LOG_SATA_ID_DEV_DATA,
257565fe1f0fSShane Huang 						     ATA_LOG_SATA_SETTINGS,
2576803739d2SShane Huang 						     sata_setting,
257765fe1f0fSShane Huang 						     1);
257865fe1f0fSShane Huang 			if (err_mask)
257965fe1f0fSShane Huang 				ata_dev_dbg(dev,
258065fe1f0fSShane Huang 					    "failed to get Identify Device Data, Emask 0x%x\n",
258165fe1f0fSShane Huang 					    err_mask);
2582803739d2SShane Huang 			else
2583803739d2SShane Huang 				for (i = 0; i < ATA_LOG_DEVSLP_SIZE; i++) {
2584803739d2SShane Huang 					j = ATA_LOG_DEVSLP_OFFSET + i;
2585803739d2SShane Huang 					dev->devslp_timing[i] = sata_setting[j];
2586803739d2SShane Huang 				}
258765fe1f0fSShane Huang 		}
2588e87fd28cSHannes Reinecke 		ata_dev_config_sense_reporting(dev);
25896d1003aeSHannes Reinecke 		ata_dev_config_zac(dev);
2590c6fd2807SJeff Garzik 		dev->cdb_len = 16;
2591c6fd2807SJeff Garzik 	}
2592c6fd2807SJeff Garzik 
2593c6fd2807SJeff Garzik 	/* ATAPI-specific feature tests */
2594c6fd2807SJeff Garzik 	else if (dev->class == ATA_DEV_ATAPI) {
2595854c73a2STejun Heo 		const char *cdb_intr_string = "";
2596854c73a2STejun Heo 		const char *atapi_an_string = "";
259791163006STejun Heo 		const char *dma_dir_string = "";
25987d77b247STejun Heo 		u32 sntf;
2599c6fd2807SJeff Garzik 
2600c6fd2807SJeff Garzik 		rc = atapi_cdb_len(id);
2601c6fd2807SJeff Garzik 		if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
2602c6fd2807SJeff Garzik 			if (ata_msg_warn(ap))
2603a9a79dfeSJoe Perches 				ata_dev_warn(dev, "unsupported CDB len\n");
2604c6fd2807SJeff Garzik 			rc = -EINVAL;
2605c6fd2807SJeff Garzik 			goto err_out_nosup;
2606c6fd2807SJeff Garzik 		}
2607c6fd2807SJeff Garzik 		dev->cdb_len = (unsigned int) rc;
2608c6fd2807SJeff Garzik 
26097d77b247STejun Heo 		/* Enable ATAPI AN if both the host and device have
26107d77b247STejun Heo 		 * the support.  If PMP is attached, SNTF is required
26117d77b247STejun Heo 		 * to enable ATAPI AN to discern between PHY status
26127d77b247STejun Heo 		 * changed notifications and ATAPI ANs.
26139f45cbd3SKristen Carlson Accardi 		 */
2614e7ecd435STejun Heo 		if (atapi_an &&
2615e7ecd435STejun Heo 		    (ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
2616071f44b1STejun Heo 		    (!sata_pmp_attached(ap) ||
26177d77b247STejun Heo 		     sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
26189f45cbd3SKristen Carlson Accardi 			/* issue SET feature command to turn this on */
2619218f3d30SJeff Garzik 			err_mask = ata_dev_set_feature(dev,
2620218f3d30SJeff Garzik 					SETFEATURES_SATA_ENABLE, SATA_AN);
2621854c73a2STejun Heo 			if (err_mask)
2622a9a79dfeSJoe Perches 				ata_dev_err(dev,
2623a9a79dfeSJoe Perches 					    "failed to enable ATAPI AN (err_mask=0x%x)\n",
2624a9a79dfeSJoe Perches 					    err_mask);
2625854c73a2STejun Heo 			else {
26269f45cbd3SKristen Carlson Accardi 				dev->flags |= ATA_DFLAG_AN;
2627854c73a2STejun Heo 				atapi_an_string = ", ATAPI AN";
2628854c73a2STejun Heo 			}
26299f45cbd3SKristen Carlson Accardi 		}
26309f45cbd3SKristen Carlson Accardi 
2631c6fd2807SJeff Garzik 		if (ata_id_cdb_intr(dev->id)) {
2632c6fd2807SJeff Garzik 			dev->flags |= ATA_DFLAG_CDB_INTR;
2633c6fd2807SJeff Garzik 			cdb_intr_string = ", CDB intr";
2634c6fd2807SJeff Garzik 		}
2635c6fd2807SJeff Garzik 
2636966fbe19SVincent Pelletier 		if (atapi_dmadir || (dev->horkage & ATA_HORKAGE_ATAPI_DMADIR) || atapi_id_dmadir(dev->id)) {
263791163006STejun Heo 			dev->flags |= ATA_DFLAG_DMADIR;
263891163006STejun Heo 			dma_dir_string = ", DMADIR";
263991163006STejun Heo 		}
264091163006STejun Heo 
2641afe75951SAaron Lu 		if (ata_id_has_da(dev->id)) {
2642b1354cbbSLin Ming 			dev->flags |= ATA_DFLAG_DA;
2643afe75951SAaron Lu 			zpodd_init(dev);
2644afe75951SAaron Lu 		}
2645b1354cbbSLin Ming 
2646c6fd2807SJeff Garzik 		/* print device info to dmesg */
2647c6fd2807SJeff Garzik 		if (ata_msg_drv(ap) && print_info)
2648a9a79dfeSJoe Perches 			ata_dev_info(dev,
264991163006STejun Heo 				     "ATAPI: %s, %s, max %s%s%s%s\n",
2650ef143d57SAlbert Lee 				     modelbuf, fwrevbuf,
2651c6fd2807SJeff Garzik 				     ata_mode_string(xfer_mask),
265291163006STejun Heo 				     cdb_intr_string, atapi_an_string,
265391163006STejun Heo 				     dma_dir_string);
2654c6fd2807SJeff Garzik 	}
2655c6fd2807SJeff Garzik 
2656914ed354STejun Heo 	/* determine max_sectors */
2657914ed354STejun Heo 	dev->max_sectors = ATA_MAX_SECTORS;
2658914ed354STejun Heo 	if (dev->flags & ATA_DFLAG_LBA48)
2659914ed354STejun Heo 		dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2660914ed354STejun Heo 
2661c5038fc0SAlan Cox 	/* Limit PATA drive on SATA cable bridge transfers to udma5,
2662c5038fc0SAlan Cox 	   200 sectors */
2663c6fd2807SJeff Garzik 	if (ata_dev_knobble(dev)) {
2664c6fd2807SJeff Garzik 		if (ata_msg_drv(ap) && print_info)
2665a9a79dfeSJoe Perches 			ata_dev_info(dev, "applying bridge limits\n");
2666c6fd2807SJeff Garzik 		dev->udma_mask &= ATA_UDMA5;
2667c6fd2807SJeff Garzik 		dev->max_sectors = ATA_MAX_SECTORS;
2668c6fd2807SJeff Garzik 	}
2669c6fd2807SJeff Garzik 
2670f8d8e579STony Battersby 	if ((dev->class == ATA_DEV_ATAPI) &&
2671f442cd86SAlbert Lee 	    (atapi_command_packet_set(id) == TYPE_TAPE)) {
2672f8d8e579STony Battersby 		dev->max_sectors = ATA_MAX_SECTORS_TAPE;
2673f442cd86SAlbert Lee 		dev->horkage |= ATA_HORKAGE_STUCK_ERR;
2674f442cd86SAlbert Lee 	}
2675f8d8e579STony Battersby 
267675683fe7STejun Heo 	if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
267703ec52deSTejun Heo 		dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
267803ec52deSTejun Heo 					 dev->max_sectors);
267918d6e9d5SAlbert Lee 
2680af34d637SDavid Milburn 	if (dev->horkage & ATA_HORKAGE_MAX_SEC_1024)
2681af34d637SDavid Milburn 		dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_1024,
2682af34d637SDavid Milburn 					 dev->max_sectors);
2683af34d637SDavid Milburn 
2684a32450e1SShan Hai 	if (dev->horkage & ATA_HORKAGE_MAX_SEC_LBA48)
2685a32450e1SShan Hai 		dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2686a32450e1SShan Hai 
2687c6fd2807SJeff Garzik 	if (ap->ops->dev_config)
2688cd0d3bbcSAlan 		ap->ops->dev_config(dev);
2689c6fd2807SJeff Garzik 
2690c5038fc0SAlan Cox 	if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2691c5038fc0SAlan Cox 		/* Let the user know. We don't want to disallow opens for
2692c5038fc0SAlan Cox 		   rescue purposes, or in case the vendor is just a blithering
2693c5038fc0SAlan Cox 		   idiot. Do this after the dev_config call as some controllers
2694c5038fc0SAlan Cox 		   with buggy firmware may want to avoid reporting false device
2695c5038fc0SAlan Cox 		   bugs */
2696c5038fc0SAlan Cox 
2697c5038fc0SAlan Cox 		if (print_info) {
2698a9a79dfeSJoe Perches 			ata_dev_warn(dev,
2699c5038fc0SAlan Cox "Drive reports diagnostics failure. This may indicate a drive\n");
2700a9a79dfeSJoe Perches 			ata_dev_warn(dev,
2701c5038fc0SAlan Cox "fault or invalid emulation. Contact drive vendor for information.\n");
2702c5038fc0SAlan Cox 		}
2703c5038fc0SAlan Cox 	}
2704c5038fc0SAlan Cox 
2705ac70a964STejun Heo 	if ((dev->horkage & ATA_HORKAGE_FIRMWARE_WARN) && print_info) {
2706a9a79dfeSJoe Perches 		ata_dev_warn(dev, "WARNING: device requires firmware update to be fully functional\n");
2707a9a79dfeSJoe Perches 		ata_dev_warn(dev, "         contact the vendor or visit http://ata.wiki.kernel.org\n");
2708ac70a964STejun Heo 	}
2709ac70a964STejun Heo 
2710c6fd2807SJeff Garzik 	return 0;
2711c6fd2807SJeff Garzik 
2712c6fd2807SJeff Garzik err_out_nosup:
2713c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2714a9a79dfeSJoe Perches 		ata_dev_dbg(dev, "%s: EXIT, err\n", __func__);
2715c6fd2807SJeff Garzik 	return rc;
2716c6fd2807SJeff Garzik }
2717c6fd2807SJeff Garzik 
2718c6fd2807SJeff Garzik /**
27192e41e8e6SAlan Cox  *	ata_cable_40wire	-	return 40 wire cable type
2720be0d18dfSAlan Cox  *	@ap: port
2721be0d18dfSAlan Cox  *
27222e41e8e6SAlan Cox  *	Helper method for drivers which want to hardwire 40 wire cable
2723be0d18dfSAlan Cox  *	detection.
2724be0d18dfSAlan Cox  */
2725be0d18dfSAlan Cox 
2726be0d18dfSAlan Cox int ata_cable_40wire(struct ata_port *ap)
2727be0d18dfSAlan Cox {
2728be0d18dfSAlan Cox 	return ATA_CBL_PATA40;
2729be0d18dfSAlan Cox }
2730be0d18dfSAlan Cox 
2731be0d18dfSAlan Cox /**
27322e41e8e6SAlan Cox  *	ata_cable_80wire	-	return 80 wire cable type
2733be0d18dfSAlan Cox  *	@ap: port
2734be0d18dfSAlan Cox  *
27352e41e8e6SAlan Cox  *	Helper method for drivers which want to hardwire 80 wire cable
2736be0d18dfSAlan Cox  *	detection.
2737be0d18dfSAlan Cox  */
2738be0d18dfSAlan Cox 
2739be0d18dfSAlan Cox int ata_cable_80wire(struct ata_port *ap)
2740be0d18dfSAlan Cox {
2741be0d18dfSAlan Cox 	return ATA_CBL_PATA80;
2742be0d18dfSAlan Cox }
2743be0d18dfSAlan Cox 
2744be0d18dfSAlan Cox /**
2745be0d18dfSAlan Cox  *	ata_cable_unknown	-	return unknown PATA cable.
2746be0d18dfSAlan Cox  *	@ap: port
2747be0d18dfSAlan Cox  *
2748be0d18dfSAlan Cox  *	Helper method for drivers which have no PATA cable detection.
2749be0d18dfSAlan Cox  */
2750be0d18dfSAlan Cox 
2751be0d18dfSAlan Cox int ata_cable_unknown(struct ata_port *ap)
2752be0d18dfSAlan Cox {
2753be0d18dfSAlan Cox 	return ATA_CBL_PATA_UNK;
2754be0d18dfSAlan Cox }
2755be0d18dfSAlan Cox 
2756be0d18dfSAlan Cox /**
2757c88f90c3STejun Heo  *	ata_cable_ignore	-	return ignored PATA cable.
2758c88f90c3STejun Heo  *	@ap: port
2759c88f90c3STejun Heo  *
2760c88f90c3STejun Heo  *	Helper method for drivers which don't use cable type to limit
2761c88f90c3STejun Heo  *	transfer mode.
2762c88f90c3STejun Heo  */
2763c88f90c3STejun Heo int ata_cable_ignore(struct ata_port *ap)
2764c88f90c3STejun Heo {
2765c88f90c3STejun Heo 	return ATA_CBL_PATA_IGN;
2766c88f90c3STejun Heo }
2767c88f90c3STejun Heo 
2768c88f90c3STejun Heo /**
2769be0d18dfSAlan Cox  *	ata_cable_sata	-	return SATA cable type
2770be0d18dfSAlan Cox  *	@ap: port
2771be0d18dfSAlan Cox  *
2772be0d18dfSAlan Cox  *	Helper method for drivers which have SATA cables
2773be0d18dfSAlan Cox  */
2774be0d18dfSAlan Cox 
2775be0d18dfSAlan Cox int ata_cable_sata(struct ata_port *ap)
2776be0d18dfSAlan Cox {
2777be0d18dfSAlan Cox 	return ATA_CBL_SATA;
2778be0d18dfSAlan Cox }
2779be0d18dfSAlan Cox 
2780be0d18dfSAlan Cox /**
2781c6fd2807SJeff Garzik  *	ata_bus_probe - Reset and probe ATA bus
2782c6fd2807SJeff Garzik  *	@ap: Bus to probe
2783c6fd2807SJeff Garzik  *
2784c6fd2807SJeff Garzik  *	Master ATA bus probing function.  Initiates a hardware-dependent
2785c6fd2807SJeff Garzik  *	bus reset, then attempts to identify any devices found on
2786c6fd2807SJeff Garzik  *	the bus.
2787c6fd2807SJeff Garzik  *
2788c6fd2807SJeff Garzik  *	LOCKING:
2789c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
2790c6fd2807SJeff Garzik  *
2791c6fd2807SJeff Garzik  *	RETURNS:
2792c6fd2807SJeff Garzik  *	Zero on success, negative errno otherwise.
2793c6fd2807SJeff Garzik  */
2794c6fd2807SJeff Garzik 
2795c6fd2807SJeff Garzik int ata_bus_probe(struct ata_port *ap)
2796c6fd2807SJeff Garzik {
2797c6fd2807SJeff Garzik 	unsigned int classes[ATA_MAX_DEVICES];
2798c6fd2807SJeff Garzik 	int tries[ATA_MAX_DEVICES];
2799f58229f8STejun Heo 	int rc;
2800c6fd2807SJeff Garzik 	struct ata_device *dev;
2801c6fd2807SJeff Garzik 
28021eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL)
2803f58229f8STejun Heo 		tries[dev->devno] = ATA_PROBE_MAX_TRIES;
2804c6fd2807SJeff Garzik 
2805c6fd2807SJeff Garzik  retry:
28061eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL) {
2807cdeab114STejun Heo 		/* If we issue an SRST then an ATA drive (not ATAPI)
2808cdeab114STejun Heo 		 * may change configuration and be in PIO0 timing. If
2809cdeab114STejun Heo 		 * we do a hard reset (or are coming from power on)
2810cdeab114STejun Heo 		 * this is true for ATA or ATAPI. Until we've set a
2811cdeab114STejun Heo 		 * suitable controller mode we should not touch the
2812cdeab114STejun Heo 		 * bus as we may be talking too fast.
2813cdeab114STejun Heo 		 */
2814cdeab114STejun Heo 		dev->pio_mode = XFER_PIO_0;
28155416912aSAaron Lu 		dev->dma_mode = 0xff;
2816cdeab114STejun Heo 
2817cdeab114STejun Heo 		/* If the controller has a pio mode setup function
2818cdeab114STejun Heo 		 * then use it to set the chipset to rights. Don't
2819cdeab114STejun Heo 		 * touch the DMA setup as that will be dealt with when
2820cdeab114STejun Heo 		 * configuring devices.
2821cdeab114STejun Heo 		 */
2822cdeab114STejun Heo 		if (ap->ops->set_piomode)
2823cdeab114STejun Heo 			ap->ops->set_piomode(ap, dev);
2824cdeab114STejun Heo 	}
2825cdeab114STejun Heo 
2826c6fd2807SJeff Garzik 	/* reset and determine device classes */
2827c6fd2807SJeff Garzik 	ap->ops->phy_reset(ap);
2828c6fd2807SJeff Garzik 
28291eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL) {
28303e4ec344STejun Heo 		if (dev->class != ATA_DEV_UNKNOWN)
2831c6fd2807SJeff Garzik 			classes[dev->devno] = dev->class;
2832c6fd2807SJeff Garzik 		else
2833c6fd2807SJeff Garzik 			classes[dev->devno] = ATA_DEV_NONE;
2834c6fd2807SJeff Garzik 
2835c6fd2807SJeff Garzik 		dev->class = ATA_DEV_UNKNOWN;
2836c6fd2807SJeff Garzik 	}
2837c6fd2807SJeff Garzik 
2838f31f0cc2SJeff Garzik 	/* read IDENTIFY page and configure devices. We have to do the identify
2839f31f0cc2SJeff Garzik 	   specific sequence bass-ackwards so that PDIAG- is released by
2840f31f0cc2SJeff Garzik 	   the slave device */
2841f31f0cc2SJeff Garzik 
28421eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL_REVERSE) {
2843f58229f8STejun Heo 		if (tries[dev->devno])
2844f58229f8STejun Heo 			dev->class = classes[dev->devno];
2845c6fd2807SJeff Garzik 
2846c6fd2807SJeff Garzik 		if (!ata_dev_enabled(dev))
2847c6fd2807SJeff Garzik 			continue;
2848c6fd2807SJeff Garzik 
2849bff04647STejun Heo 		rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2850bff04647STejun Heo 				     dev->id);
2851c6fd2807SJeff Garzik 		if (rc)
2852c6fd2807SJeff Garzik 			goto fail;
2853f31f0cc2SJeff Garzik 	}
2854f31f0cc2SJeff Garzik 
2855be0d18dfSAlan Cox 	/* Now ask for the cable type as PDIAG- should have been released */
2856be0d18dfSAlan Cox 	if (ap->ops->cable_detect)
2857be0d18dfSAlan Cox 		ap->cbl = ap->ops->cable_detect(ap);
2858be0d18dfSAlan Cox 
28591eca4365STejun Heo 	/* We may have SATA bridge glue hiding here irrespective of
28601eca4365STejun Heo 	 * the reported cable types and sensed types.  When SATA
28611eca4365STejun Heo 	 * drives indicate we have a bridge, we don't know which end
28621eca4365STejun Heo 	 * of the link the bridge is which is a problem.
28631eca4365STejun Heo 	 */
28641eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED)
2865614fe29bSAlan Cox 		if (ata_id_is_sata(dev->id))
2866614fe29bSAlan Cox 			ap->cbl = ATA_CBL_SATA;
2867614fe29bSAlan Cox 
2868f31f0cc2SJeff Garzik 	/* After the identify sequence we can now set up the devices. We do
2869f31f0cc2SJeff Garzik 	   this in the normal order so that the user doesn't get confused */
2870f31f0cc2SJeff Garzik 
28711eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED) {
28729af5c9c9STejun Heo 		ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
2873efdaedc4STejun Heo 		rc = ata_dev_configure(dev);
28749af5c9c9STejun Heo 		ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
2875c6fd2807SJeff Garzik 		if (rc)
2876c6fd2807SJeff Garzik 			goto fail;
2877c6fd2807SJeff Garzik 	}
2878c6fd2807SJeff Garzik 
2879c6fd2807SJeff Garzik 	/* configure transfer mode */
28800260731fSTejun Heo 	rc = ata_set_mode(&ap->link, &dev);
28814ae72a1eSTejun Heo 	if (rc)
2882c6fd2807SJeff Garzik 		goto fail;
2883c6fd2807SJeff Garzik 
28841eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED)
2885c6fd2807SJeff Garzik 		return 0;
2886c6fd2807SJeff Garzik 
2887c6fd2807SJeff Garzik 	return -ENODEV;
2888c6fd2807SJeff Garzik 
2889c6fd2807SJeff Garzik  fail:
28904ae72a1eSTejun Heo 	tries[dev->devno]--;
28914ae72a1eSTejun Heo 
2892c6fd2807SJeff Garzik 	switch (rc) {
2893c6fd2807SJeff Garzik 	case -EINVAL:
28944ae72a1eSTejun Heo 		/* eeek, something went very wrong, give up */
2895c6fd2807SJeff Garzik 		tries[dev->devno] = 0;
2896c6fd2807SJeff Garzik 		break;
28974ae72a1eSTejun Heo 
28984ae72a1eSTejun Heo 	case -ENODEV:
28994ae72a1eSTejun Heo 		/* give it just one more chance */
29004ae72a1eSTejun Heo 		tries[dev->devno] = min(tries[dev->devno], 1);
2901c6fd2807SJeff Garzik 	case -EIO:
29024ae72a1eSTejun Heo 		if (tries[dev->devno] == 1) {
29034ae72a1eSTejun Heo 			/* This is the last chance, better to slow
29044ae72a1eSTejun Heo 			 * down than lose it.
29054ae72a1eSTejun Heo 			 */
2906a07d499bSTejun Heo 			sata_down_spd_limit(&ap->link, 0);
29074ae72a1eSTejun Heo 			ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
29084ae72a1eSTejun Heo 		}
2909c6fd2807SJeff Garzik 	}
2910c6fd2807SJeff Garzik 
29114ae72a1eSTejun Heo 	if (!tries[dev->devno])
2912c6fd2807SJeff Garzik 		ata_dev_disable(dev);
2913c6fd2807SJeff Garzik 
2914c6fd2807SJeff Garzik 	goto retry;
2915c6fd2807SJeff Garzik }
2916c6fd2807SJeff Garzik 
2917c6fd2807SJeff Garzik /**
2918c6fd2807SJeff Garzik  *	sata_print_link_status - Print SATA link status
2919936fd732STejun Heo  *	@link: SATA link to printk link status about
2920c6fd2807SJeff Garzik  *
2921c6fd2807SJeff Garzik  *	This function prints link speed and status of a SATA link.
2922c6fd2807SJeff Garzik  *
2923c6fd2807SJeff Garzik  *	LOCKING:
2924c6fd2807SJeff Garzik  *	None.
2925c6fd2807SJeff Garzik  */
29266bdb4fc9SAdrian Bunk static void sata_print_link_status(struct ata_link *link)
2927c6fd2807SJeff Garzik {
2928c6fd2807SJeff Garzik 	u32 sstatus, scontrol, tmp;
2929c6fd2807SJeff Garzik 
2930936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus))
2931c6fd2807SJeff Garzik 		return;
2932936fd732STejun Heo 	sata_scr_read(link, SCR_CONTROL, &scontrol);
2933c6fd2807SJeff Garzik 
2934b1c72916STejun Heo 	if (ata_phys_link_online(link)) {
2935c6fd2807SJeff Garzik 		tmp = (sstatus >> 4) & 0xf;
2936a9a79dfeSJoe Perches 		ata_link_info(link, "SATA link up %s (SStatus %X SControl %X)\n",
2937c6fd2807SJeff Garzik 			      sata_spd_string(tmp), sstatus, scontrol);
2938c6fd2807SJeff Garzik 	} else {
2939a9a79dfeSJoe Perches 		ata_link_info(link, "SATA link down (SStatus %X SControl %X)\n",
2940c6fd2807SJeff Garzik 			      sstatus, scontrol);
2941c6fd2807SJeff Garzik 	}
2942c6fd2807SJeff Garzik }
2943c6fd2807SJeff Garzik 
2944c6fd2807SJeff Garzik /**
2945c6fd2807SJeff Garzik  *	ata_dev_pair		-	return other device on cable
2946c6fd2807SJeff Garzik  *	@adev: device
2947c6fd2807SJeff Garzik  *
2948c6fd2807SJeff Garzik  *	Obtain the other device on the same cable, or if none is
2949c6fd2807SJeff Garzik  *	present NULL is returned
2950c6fd2807SJeff Garzik  */
2951c6fd2807SJeff Garzik 
2952c6fd2807SJeff Garzik struct ata_device *ata_dev_pair(struct ata_device *adev)
2953c6fd2807SJeff Garzik {
29549af5c9c9STejun Heo 	struct ata_link *link = adev->link;
29559af5c9c9STejun Heo 	struct ata_device *pair = &link->device[1 - adev->devno];
2956c6fd2807SJeff Garzik 	if (!ata_dev_enabled(pair))
2957c6fd2807SJeff Garzik 		return NULL;
2958c6fd2807SJeff Garzik 	return pair;
2959c6fd2807SJeff Garzik }
2960c6fd2807SJeff Garzik 
2961c6fd2807SJeff Garzik /**
2962c6fd2807SJeff Garzik  *	sata_down_spd_limit - adjust SATA spd limit downward
2963936fd732STejun Heo  *	@link: Link to adjust SATA spd limit for
2964a07d499bSTejun Heo  *	@spd_limit: Additional limit
2965c6fd2807SJeff Garzik  *
2966936fd732STejun Heo  *	Adjust SATA spd limit of @link downward.  Note that this
2967c6fd2807SJeff Garzik  *	function only adjusts the limit.  The change must be applied
2968c6fd2807SJeff Garzik  *	using sata_set_spd().
2969c6fd2807SJeff Garzik  *
2970a07d499bSTejun Heo  *	If @spd_limit is non-zero, the speed is limited to equal to or
2971a07d499bSTejun Heo  *	lower than @spd_limit if such speed is supported.  If
2972a07d499bSTejun Heo  *	@spd_limit is slower than any supported speed, only the lowest
2973a07d499bSTejun Heo  *	supported speed is allowed.
2974a07d499bSTejun Heo  *
2975c6fd2807SJeff Garzik  *	LOCKING:
2976c6fd2807SJeff Garzik  *	Inherited from caller.
2977c6fd2807SJeff Garzik  *
2978c6fd2807SJeff Garzik  *	RETURNS:
2979c6fd2807SJeff Garzik  *	0 on success, negative errno on failure
2980c6fd2807SJeff Garzik  */
2981a07d499bSTejun Heo int sata_down_spd_limit(struct ata_link *link, u32 spd_limit)
2982c6fd2807SJeff Garzik {
2983c6fd2807SJeff Garzik 	u32 sstatus, spd, mask;
2984a07d499bSTejun Heo 	int rc, bit;
2985c6fd2807SJeff Garzik 
2986936fd732STejun Heo 	if (!sata_scr_valid(link))
2987008a7896STejun Heo 		return -EOPNOTSUPP;
2988008a7896STejun Heo 
2989008a7896STejun Heo 	/* If SCR can be read, use it to determine the current SPD.
2990936fd732STejun Heo 	 * If not, use cached value in link->sata_spd.
2991008a7896STejun Heo 	 */
2992936fd732STejun Heo 	rc = sata_scr_read(link, SCR_STATUS, &sstatus);
29939913ff8aSTejun Heo 	if (rc == 0 && ata_sstatus_online(sstatus))
2994008a7896STejun Heo 		spd = (sstatus >> 4) & 0xf;
2995008a7896STejun Heo 	else
2996936fd732STejun Heo 		spd = link->sata_spd;
2997c6fd2807SJeff Garzik 
2998936fd732STejun Heo 	mask = link->sata_spd_limit;
2999c6fd2807SJeff Garzik 	if (mask <= 1)
3000c6fd2807SJeff Garzik 		return -EINVAL;
3001008a7896STejun Heo 
3002008a7896STejun Heo 	/* unconditionally mask off the highest bit */
3003a07d499bSTejun Heo 	bit = fls(mask) - 1;
3004a07d499bSTejun Heo 	mask &= ~(1 << bit);
3005c6fd2807SJeff Garzik 
3006008a7896STejun Heo 	/* Mask off all speeds higher than or equal to the current
3007008a7896STejun Heo 	 * one.  Force 1.5Gbps if current SPD is not available.
3008008a7896STejun Heo 	 */
3009008a7896STejun Heo 	if (spd > 1)
3010008a7896STejun Heo 		mask &= (1 << (spd - 1)) - 1;
3011008a7896STejun Heo 	else
3012008a7896STejun Heo 		mask &= 1;
3013008a7896STejun Heo 
3014008a7896STejun Heo 	/* were we already at the bottom? */
3015c6fd2807SJeff Garzik 	if (!mask)
3016c6fd2807SJeff Garzik 		return -EINVAL;
3017c6fd2807SJeff Garzik 
3018a07d499bSTejun Heo 	if (spd_limit) {
3019a07d499bSTejun Heo 		if (mask & ((1 << spd_limit) - 1))
3020a07d499bSTejun Heo 			mask &= (1 << spd_limit) - 1;
3021a07d499bSTejun Heo 		else {
3022a07d499bSTejun Heo 			bit = ffs(mask) - 1;
3023a07d499bSTejun Heo 			mask = 1 << bit;
3024a07d499bSTejun Heo 		}
3025a07d499bSTejun Heo 	}
3026a07d499bSTejun Heo 
3027936fd732STejun Heo 	link->sata_spd_limit = mask;
3028c6fd2807SJeff Garzik 
3029a9a79dfeSJoe Perches 	ata_link_warn(link, "limiting SATA link speed to %s\n",
3030c6fd2807SJeff Garzik 		      sata_spd_string(fls(mask)));
3031c6fd2807SJeff Garzik 
3032c6fd2807SJeff Garzik 	return 0;
3033c6fd2807SJeff Garzik }
3034c6fd2807SJeff Garzik 
3035936fd732STejun Heo static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
3036c6fd2807SJeff Garzik {
30375270222fSTejun Heo 	struct ata_link *host_link = &link->ap->link;
30385270222fSTejun Heo 	u32 limit, target, spd;
3039c6fd2807SJeff Garzik 
30405270222fSTejun Heo 	limit = link->sata_spd_limit;
30415270222fSTejun Heo 
30425270222fSTejun Heo 	/* Don't configure downstream link faster than upstream link.
30435270222fSTejun Heo 	 * It doesn't speed up anything and some PMPs choke on such
30445270222fSTejun Heo 	 * configuration.
30455270222fSTejun Heo 	 */
30465270222fSTejun Heo 	if (!ata_is_host_link(link) && host_link->sata_spd)
30475270222fSTejun Heo 		limit &= (1 << host_link->sata_spd) - 1;
30485270222fSTejun Heo 
30495270222fSTejun Heo 	if (limit == UINT_MAX)
30505270222fSTejun Heo 		target = 0;
3051c6fd2807SJeff Garzik 	else
30525270222fSTejun Heo 		target = fls(limit);
3053c6fd2807SJeff Garzik 
3054c6fd2807SJeff Garzik 	spd = (*scontrol >> 4) & 0xf;
30555270222fSTejun Heo 	*scontrol = (*scontrol & ~0xf0) | ((target & 0xf) << 4);
3056c6fd2807SJeff Garzik 
30575270222fSTejun Heo 	return spd != target;
3058c6fd2807SJeff Garzik }
3059c6fd2807SJeff Garzik 
3060c6fd2807SJeff Garzik /**
3061c6fd2807SJeff Garzik  *	sata_set_spd_needed - is SATA spd configuration needed
3062936fd732STejun Heo  *	@link: Link in question
3063c6fd2807SJeff Garzik  *
3064c6fd2807SJeff Garzik  *	Test whether the spd limit in SControl matches
3065936fd732STejun Heo  *	@link->sata_spd_limit.  This function is used to determine
3066c6fd2807SJeff Garzik  *	whether hardreset is necessary to apply SATA spd
3067c6fd2807SJeff Garzik  *	configuration.
3068c6fd2807SJeff Garzik  *
3069c6fd2807SJeff Garzik  *	LOCKING:
3070c6fd2807SJeff Garzik  *	Inherited from caller.
3071c6fd2807SJeff Garzik  *
3072c6fd2807SJeff Garzik  *	RETURNS:
3073c6fd2807SJeff Garzik  *	1 if SATA spd configuration is needed, 0 otherwise.
3074c6fd2807SJeff Garzik  */
30751dc55e87SAdrian Bunk static int sata_set_spd_needed(struct ata_link *link)
3076c6fd2807SJeff Garzik {
3077c6fd2807SJeff Garzik 	u32 scontrol;
3078c6fd2807SJeff Garzik 
3079936fd732STejun Heo 	if (sata_scr_read(link, SCR_CONTROL, &scontrol))
3080db64bcf3STejun Heo 		return 1;
3081c6fd2807SJeff Garzik 
3082936fd732STejun Heo 	return __sata_set_spd_needed(link, &scontrol);
3083c6fd2807SJeff Garzik }
3084c6fd2807SJeff Garzik 
3085c6fd2807SJeff Garzik /**
3086c6fd2807SJeff Garzik  *	sata_set_spd - set SATA spd according to spd limit
3087936fd732STejun Heo  *	@link: Link to set SATA spd for
3088c6fd2807SJeff Garzik  *
3089936fd732STejun Heo  *	Set SATA spd of @link according to sata_spd_limit.
3090c6fd2807SJeff Garzik  *
3091c6fd2807SJeff Garzik  *	LOCKING:
3092c6fd2807SJeff Garzik  *	Inherited from caller.
3093c6fd2807SJeff Garzik  *
3094c6fd2807SJeff Garzik  *	RETURNS:
3095c6fd2807SJeff Garzik  *	0 if spd doesn't need to be changed, 1 if spd has been
3096c6fd2807SJeff Garzik  *	changed.  Negative errno if SCR registers are inaccessible.
3097c6fd2807SJeff Garzik  */
3098936fd732STejun Heo int sata_set_spd(struct ata_link *link)
3099c6fd2807SJeff Garzik {
3100c6fd2807SJeff Garzik 	u32 scontrol;
3101c6fd2807SJeff Garzik 	int rc;
3102c6fd2807SJeff Garzik 
3103936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3104c6fd2807SJeff Garzik 		return rc;
3105c6fd2807SJeff Garzik 
3106936fd732STejun Heo 	if (!__sata_set_spd_needed(link, &scontrol))
3107c6fd2807SJeff Garzik 		return 0;
3108c6fd2807SJeff Garzik 
3109936fd732STejun Heo 	if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3110c6fd2807SJeff Garzik 		return rc;
3111c6fd2807SJeff Garzik 
3112c6fd2807SJeff Garzik 	return 1;
3113c6fd2807SJeff Garzik }
3114c6fd2807SJeff Garzik 
3115c6fd2807SJeff Garzik /*
3116c6fd2807SJeff Garzik  * This mode timing computation functionality is ported over from
3117c6fd2807SJeff Garzik  * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
3118c6fd2807SJeff Garzik  */
3119c6fd2807SJeff Garzik /*
3120b352e57dSAlan Cox  * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
3121c6fd2807SJeff Garzik  * These were taken from ATA/ATAPI-6 standard, rev 0a, except
3122b352e57dSAlan Cox  * for UDMA6, which is currently supported only by Maxtor drives.
3123b352e57dSAlan Cox  *
3124b352e57dSAlan Cox  * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0.
3125c6fd2807SJeff Garzik  */
3126c6fd2807SJeff Garzik 
3127c6fd2807SJeff Garzik static const struct ata_timing ata_timing[] = {
31283ada9c12SDavid Daney /*	{ XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 0,  960,   0 }, */
31293ada9c12SDavid Daney 	{ XFER_PIO_0,     70, 290, 240, 600, 165, 150, 0,  600,   0 },
31303ada9c12SDavid Daney 	{ XFER_PIO_1,     50, 290,  93, 383, 125, 100, 0,  383,   0 },
31313ada9c12SDavid Daney 	{ XFER_PIO_2,     30, 290,  40, 330, 100,  90, 0,  240,   0 },
31323ada9c12SDavid Daney 	{ XFER_PIO_3,     30,  80,  70, 180,  80,  70, 0,  180,   0 },
31333ada9c12SDavid Daney 	{ XFER_PIO_4,     25,  70,  25, 120,  70,  25, 0,  120,   0 },
31343ada9c12SDavid Daney 	{ XFER_PIO_5,     15,  65,  25, 100,  65,  25, 0,  100,   0 },
31353ada9c12SDavid Daney 	{ XFER_PIO_6,     10,  55,  20,  80,  55,  20, 0,   80,   0 },
3136c6fd2807SJeff Garzik 
31373ada9c12SDavid Daney 	{ XFER_SW_DMA_0, 120,   0,   0,   0, 480, 480, 50, 960,   0 },
31383ada9c12SDavid Daney 	{ XFER_SW_DMA_1,  90,   0,   0,   0, 240, 240, 30, 480,   0 },
31393ada9c12SDavid Daney 	{ XFER_SW_DMA_2,  60,   0,   0,   0, 120, 120, 20, 240,   0 },
3140c6fd2807SJeff Garzik 
31413ada9c12SDavid Daney 	{ XFER_MW_DMA_0,  60,   0,   0,   0, 215, 215, 20, 480,   0 },
31423ada9c12SDavid Daney 	{ XFER_MW_DMA_1,  45,   0,   0,   0,  80,  50, 5,  150,   0 },
31433ada9c12SDavid Daney 	{ XFER_MW_DMA_2,  25,   0,   0,   0,  70,  25, 5,  120,   0 },
31443ada9c12SDavid Daney 	{ XFER_MW_DMA_3,  25,   0,   0,   0,  65,  25, 5,  100,   0 },
31453ada9c12SDavid Daney 	{ XFER_MW_DMA_4,  25,   0,   0,   0,  55,  20, 5,   80,   0 },
3146c6fd2807SJeff Garzik 
31473ada9c12SDavid Daney /*	{ XFER_UDMA_SLOW,  0,   0,   0,   0,   0,   0, 0,    0, 150 }, */
31483ada9c12SDavid Daney 	{ XFER_UDMA_0,     0,   0,   0,   0,   0,   0, 0,    0, 120 },
31493ada9c12SDavid Daney 	{ XFER_UDMA_1,     0,   0,   0,   0,   0,   0, 0,    0,  80 },
31503ada9c12SDavid Daney 	{ XFER_UDMA_2,     0,   0,   0,   0,   0,   0, 0,    0,  60 },
31513ada9c12SDavid Daney 	{ XFER_UDMA_3,     0,   0,   0,   0,   0,   0, 0,    0,  45 },
31523ada9c12SDavid Daney 	{ XFER_UDMA_4,     0,   0,   0,   0,   0,   0, 0,    0,  30 },
31533ada9c12SDavid Daney 	{ XFER_UDMA_5,     0,   0,   0,   0,   0,   0, 0,    0,  20 },
31543ada9c12SDavid Daney 	{ XFER_UDMA_6,     0,   0,   0,   0,   0,   0, 0,    0,  15 },
3155c6fd2807SJeff Garzik 
3156c6fd2807SJeff Garzik 	{ 0xFF }
3157c6fd2807SJeff Garzik };
3158c6fd2807SJeff Garzik 
3159c6fd2807SJeff Garzik #define ENOUGH(v, unit)		(((v)-1)/(unit)+1)
3160c6fd2807SJeff Garzik #define EZ(v, unit)		((v)?ENOUGH(v, unit):0)
3161c6fd2807SJeff Garzik 
3162c6fd2807SJeff Garzik static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
3163c6fd2807SJeff Garzik {
3164c6fd2807SJeff Garzik 	q->setup	= EZ(t->setup      * 1000,  T);
3165c6fd2807SJeff Garzik 	q->act8b	= EZ(t->act8b      * 1000,  T);
3166c6fd2807SJeff Garzik 	q->rec8b	= EZ(t->rec8b      * 1000,  T);
3167c6fd2807SJeff Garzik 	q->cyc8b	= EZ(t->cyc8b      * 1000,  T);
3168c6fd2807SJeff Garzik 	q->active	= EZ(t->active     * 1000,  T);
3169c6fd2807SJeff Garzik 	q->recover	= EZ(t->recover    * 1000,  T);
31703ada9c12SDavid Daney 	q->dmack_hold	= EZ(t->dmack_hold * 1000,  T);
3171c6fd2807SJeff Garzik 	q->cycle	= EZ(t->cycle      * 1000,  T);
3172c6fd2807SJeff Garzik 	q->udma		= EZ(t->udma       * 1000, UT);
3173c6fd2807SJeff Garzik }
3174c6fd2807SJeff Garzik 
3175c6fd2807SJeff Garzik void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
3176c6fd2807SJeff Garzik 		      struct ata_timing *m, unsigned int what)
3177c6fd2807SJeff Garzik {
3178c6fd2807SJeff Garzik 	if (what & ATA_TIMING_SETUP  ) m->setup   = max(a->setup,   b->setup);
3179c6fd2807SJeff Garzik 	if (what & ATA_TIMING_ACT8B  ) m->act8b   = max(a->act8b,   b->act8b);
3180c6fd2807SJeff Garzik 	if (what & ATA_TIMING_REC8B  ) m->rec8b   = max(a->rec8b,   b->rec8b);
3181c6fd2807SJeff Garzik 	if (what & ATA_TIMING_CYC8B  ) m->cyc8b   = max(a->cyc8b,   b->cyc8b);
3182c6fd2807SJeff Garzik 	if (what & ATA_TIMING_ACTIVE ) m->active  = max(a->active,  b->active);
3183c6fd2807SJeff Garzik 	if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
31843ada9c12SDavid Daney 	if (what & ATA_TIMING_DMACK_HOLD) m->dmack_hold = max(a->dmack_hold, b->dmack_hold);
3185c6fd2807SJeff Garzik 	if (what & ATA_TIMING_CYCLE  ) m->cycle   = max(a->cycle,   b->cycle);
3186c6fd2807SJeff Garzik 	if (what & ATA_TIMING_UDMA   ) m->udma    = max(a->udma,    b->udma);
3187c6fd2807SJeff Garzik }
3188c6fd2807SJeff Garzik 
31896357357cSTejun Heo const struct ata_timing *ata_timing_find_mode(u8 xfer_mode)
3190c6fd2807SJeff Garzik {
319170cd071eSTejun Heo 	const struct ata_timing *t = ata_timing;
3192c6fd2807SJeff Garzik 
319370cd071eSTejun Heo 	while (xfer_mode > t->mode)
319470cd071eSTejun Heo 		t++;
319570cd071eSTejun Heo 
319670cd071eSTejun Heo 	if (xfer_mode == t->mode)
3197c6fd2807SJeff Garzik 		return t;
3198cd705d5aSBorislav Petkov 
3199cd705d5aSBorislav Petkov 	WARN_ONCE(true, "%s: unable to find timing for xfer_mode 0x%x\n",
3200cd705d5aSBorislav Petkov 			__func__, xfer_mode);
3201cd705d5aSBorislav Petkov 
320270cd071eSTejun Heo 	return NULL;
3203c6fd2807SJeff Garzik }
3204c6fd2807SJeff Garzik 
3205c6fd2807SJeff Garzik int ata_timing_compute(struct ata_device *adev, unsigned short speed,
3206c6fd2807SJeff Garzik 		       struct ata_timing *t, int T, int UT)
3207c6fd2807SJeff Garzik {
32089e8808a9SBartlomiej Zolnierkiewicz 	const u16 *id = adev->id;
3209c6fd2807SJeff Garzik 	const struct ata_timing *s;
3210c6fd2807SJeff Garzik 	struct ata_timing p;
3211c6fd2807SJeff Garzik 
3212c6fd2807SJeff Garzik 	/*
3213c6fd2807SJeff Garzik 	 * Find the mode.
3214c6fd2807SJeff Garzik 	 */
3215c6fd2807SJeff Garzik 
3216c6fd2807SJeff Garzik 	if (!(s = ata_timing_find_mode(speed)))
3217c6fd2807SJeff Garzik 		return -EINVAL;
3218c6fd2807SJeff Garzik 
3219c6fd2807SJeff Garzik 	memcpy(t, s, sizeof(*s));
3220c6fd2807SJeff Garzik 
3221c6fd2807SJeff Garzik 	/*
3222c6fd2807SJeff Garzik 	 * If the drive is an EIDE drive, it can tell us it needs extended
3223c6fd2807SJeff Garzik 	 * PIO/MW_DMA cycle timing.
3224c6fd2807SJeff Garzik 	 */
3225c6fd2807SJeff Garzik 
32269e8808a9SBartlomiej Zolnierkiewicz 	if (id[ATA_ID_FIELD_VALID] & 2) {	/* EIDE drive */
3227c6fd2807SJeff Garzik 		memset(&p, 0, sizeof(p));
32289e8808a9SBartlomiej Zolnierkiewicz 
3229bff00256SBartlomiej Zolnierkiewicz 		if (speed >= XFER_PIO_0 && speed < XFER_SW_DMA_0) {
32309e8808a9SBartlomiej Zolnierkiewicz 			if (speed <= XFER_PIO_2)
32319e8808a9SBartlomiej Zolnierkiewicz 				p.cycle = p.cyc8b = id[ATA_ID_EIDE_PIO];
32329e8808a9SBartlomiej Zolnierkiewicz 			else if ((speed <= XFER_PIO_4) ||
32339e8808a9SBartlomiej Zolnierkiewicz 				 (speed == XFER_PIO_5 && !ata_id_is_cfa(id)))
32349e8808a9SBartlomiej Zolnierkiewicz 				p.cycle = p.cyc8b = id[ATA_ID_EIDE_PIO_IORDY];
32359e8808a9SBartlomiej Zolnierkiewicz 		} else if (speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2)
32369e8808a9SBartlomiej Zolnierkiewicz 			p.cycle = id[ATA_ID_EIDE_DMA_MIN];
32379e8808a9SBartlomiej Zolnierkiewicz 
3238c6fd2807SJeff Garzik 		ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
3239c6fd2807SJeff Garzik 	}
3240c6fd2807SJeff Garzik 
3241c6fd2807SJeff Garzik 	/*
3242c6fd2807SJeff Garzik 	 * Convert the timing to bus clock counts.
3243c6fd2807SJeff Garzik 	 */
3244c6fd2807SJeff Garzik 
3245c6fd2807SJeff Garzik 	ata_timing_quantize(t, t, T, UT);
3246c6fd2807SJeff Garzik 
3247c6fd2807SJeff Garzik 	/*
3248c6fd2807SJeff Garzik 	 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
3249c6fd2807SJeff Garzik 	 * S.M.A.R.T * and some other commands. We have to ensure that the
3250c6fd2807SJeff Garzik 	 * DMA cycle timing is slower/equal than the fastest PIO timing.
3251c6fd2807SJeff Garzik 	 */
3252c6fd2807SJeff Garzik 
3253fd3367afSAlan 	if (speed > XFER_PIO_6) {
3254c6fd2807SJeff Garzik 		ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
3255c6fd2807SJeff Garzik 		ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
3256c6fd2807SJeff Garzik 	}
3257c6fd2807SJeff Garzik 
3258c6fd2807SJeff Garzik 	/*
3259c6fd2807SJeff Garzik 	 * Lengthen active & recovery time so that cycle time is correct.
3260c6fd2807SJeff Garzik 	 */
3261c6fd2807SJeff Garzik 
3262c6fd2807SJeff Garzik 	if (t->act8b + t->rec8b < t->cyc8b) {
3263c6fd2807SJeff Garzik 		t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
3264c6fd2807SJeff Garzik 		t->rec8b = t->cyc8b - t->act8b;
3265c6fd2807SJeff Garzik 	}
3266c6fd2807SJeff Garzik 
3267c6fd2807SJeff Garzik 	if (t->active + t->recover < t->cycle) {
3268c6fd2807SJeff Garzik 		t->active += (t->cycle - (t->active + t->recover)) / 2;
3269c6fd2807SJeff Garzik 		t->recover = t->cycle - t->active;
3270c6fd2807SJeff Garzik 	}
32714f701d1eSAlan Cox 
32724f701d1eSAlan Cox 	/* In a few cases quantisation may produce enough errors to
32734f701d1eSAlan Cox 	   leave t->cycle too low for the sum of active and recovery
32744f701d1eSAlan Cox 	   if so we must correct this */
32754f701d1eSAlan Cox 	if (t->active + t->recover > t->cycle)
32764f701d1eSAlan Cox 		t->cycle = t->active + t->recover;
3277c6fd2807SJeff Garzik 
3278c6fd2807SJeff Garzik 	return 0;
3279c6fd2807SJeff Garzik }
3280c6fd2807SJeff Garzik 
3281c6fd2807SJeff Garzik /**
3282a0f79b92STejun Heo  *	ata_timing_cycle2mode - find xfer mode for the specified cycle duration
3283a0f79b92STejun Heo  *	@xfer_shift: ATA_SHIFT_* value for transfer type to examine.
3284a0f79b92STejun Heo  *	@cycle: cycle duration in ns
3285a0f79b92STejun Heo  *
3286a0f79b92STejun Heo  *	Return matching xfer mode for @cycle.  The returned mode is of
3287a0f79b92STejun Heo  *	the transfer type specified by @xfer_shift.  If @cycle is too
3288a0f79b92STejun Heo  *	slow for @xfer_shift, 0xff is returned.  If @cycle is faster
3289a0f79b92STejun Heo  *	than the fastest known mode, the fasted mode is returned.
3290a0f79b92STejun Heo  *
3291a0f79b92STejun Heo  *	LOCKING:
3292a0f79b92STejun Heo  *	None.
3293a0f79b92STejun Heo  *
3294a0f79b92STejun Heo  *	RETURNS:
3295a0f79b92STejun Heo  *	Matching xfer_mode, 0xff if no match found.
3296a0f79b92STejun Heo  */
3297a0f79b92STejun Heo u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
3298a0f79b92STejun Heo {
3299a0f79b92STejun Heo 	u8 base_mode = 0xff, last_mode = 0xff;
3300a0f79b92STejun Heo 	const struct ata_xfer_ent *ent;
3301a0f79b92STejun Heo 	const struct ata_timing *t;
3302a0f79b92STejun Heo 
3303a0f79b92STejun Heo 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
3304a0f79b92STejun Heo 		if (ent->shift == xfer_shift)
3305a0f79b92STejun Heo 			base_mode = ent->base;
3306a0f79b92STejun Heo 
3307a0f79b92STejun Heo 	for (t = ata_timing_find_mode(base_mode);
3308a0f79b92STejun Heo 	     t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
3309a0f79b92STejun Heo 		unsigned short this_cycle;
3310a0f79b92STejun Heo 
3311a0f79b92STejun Heo 		switch (xfer_shift) {
3312a0f79b92STejun Heo 		case ATA_SHIFT_PIO:
3313a0f79b92STejun Heo 		case ATA_SHIFT_MWDMA:
3314a0f79b92STejun Heo 			this_cycle = t->cycle;
3315a0f79b92STejun Heo 			break;
3316a0f79b92STejun Heo 		case ATA_SHIFT_UDMA:
3317a0f79b92STejun Heo 			this_cycle = t->udma;
3318a0f79b92STejun Heo 			break;
3319a0f79b92STejun Heo 		default:
3320a0f79b92STejun Heo 			return 0xff;
3321a0f79b92STejun Heo 		}
3322a0f79b92STejun Heo 
3323a0f79b92STejun Heo 		if (cycle > this_cycle)
3324a0f79b92STejun Heo 			break;
3325a0f79b92STejun Heo 
3326a0f79b92STejun Heo 		last_mode = t->mode;
3327a0f79b92STejun Heo 	}
3328a0f79b92STejun Heo 
3329a0f79b92STejun Heo 	return last_mode;
3330a0f79b92STejun Heo }
3331a0f79b92STejun Heo 
3332a0f79b92STejun Heo /**
3333c6fd2807SJeff Garzik  *	ata_down_xfermask_limit - adjust dev xfer masks downward
3334c6fd2807SJeff Garzik  *	@dev: Device to adjust xfer masks
3335458337dbSTejun Heo  *	@sel: ATA_DNXFER_* selector
3336c6fd2807SJeff Garzik  *
3337c6fd2807SJeff Garzik  *	Adjust xfer masks of @dev downward.  Note that this function
3338c6fd2807SJeff Garzik  *	does not apply the change.  Invoking ata_set_mode() afterwards
3339c6fd2807SJeff Garzik  *	will apply the limit.
3340c6fd2807SJeff Garzik  *
3341c6fd2807SJeff Garzik  *	LOCKING:
3342c6fd2807SJeff Garzik  *	Inherited from caller.
3343c6fd2807SJeff Garzik  *
3344c6fd2807SJeff Garzik  *	RETURNS:
3345c6fd2807SJeff Garzik  *	0 on success, negative errno on failure
3346c6fd2807SJeff Garzik  */
3347458337dbSTejun Heo int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
3348c6fd2807SJeff Garzik {
3349458337dbSTejun Heo 	char buf[32];
33507dc951aeSTejun Heo 	unsigned long orig_mask, xfer_mask;
33517dc951aeSTejun Heo 	unsigned long pio_mask, mwdma_mask, udma_mask;
3352458337dbSTejun Heo 	int quiet, highbit;
3353c6fd2807SJeff Garzik 
3354458337dbSTejun Heo 	quiet = !!(sel & ATA_DNXFER_QUIET);
3355458337dbSTejun Heo 	sel &= ~ATA_DNXFER_QUIET;
3356458337dbSTejun Heo 
3357458337dbSTejun Heo 	xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
3358458337dbSTejun Heo 						  dev->mwdma_mask,
3359c6fd2807SJeff Garzik 						  dev->udma_mask);
3360458337dbSTejun Heo 	ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
3361c6fd2807SJeff Garzik 
3362458337dbSTejun Heo 	switch (sel) {
3363458337dbSTejun Heo 	case ATA_DNXFER_PIO:
3364458337dbSTejun Heo 		highbit = fls(pio_mask) - 1;
3365458337dbSTejun Heo 		pio_mask &= ~(1 << highbit);
3366458337dbSTejun Heo 		break;
3367458337dbSTejun Heo 
3368458337dbSTejun Heo 	case ATA_DNXFER_DMA:
3369458337dbSTejun Heo 		if (udma_mask) {
3370458337dbSTejun Heo 			highbit = fls(udma_mask) - 1;
3371458337dbSTejun Heo 			udma_mask &= ~(1 << highbit);
3372458337dbSTejun Heo 			if (!udma_mask)
3373458337dbSTejun Heo 				return -ENOENT;
3374458337dbSTejun Heo 		} else if (mwdma_mask) {
3375458337dbSTejun Heo 			highbit = fls(mwdma_mask) - 1;
3376458337dbSTejun Heo 			mwdma_mask &= ~(1 << highbit);
3377458337dbSTejun Heo 			if (!mwdma_mask)
3378458337dbSTejun Heo 				return -ENOENT;
3379458337dbSTejun Heo 		}
3380458337dbSTejun Heo 		break;
3381458337dbSTejun Heo 
3382458337dbSTejun Heo 	case ATA_DNXFER_40C:
3383458337dbSTejun Heo 		udma_mask &= ATA_UDMA_MASK_40C;
3384458337dbSTejun Heo 		break;
3385458337dbSTejun Heo 
3386458337dbSTejun Heo 	case ATA_DNXFER_FORCE_PIO0:
3387458337dbSTejun Heo 		pio_mask &= 1;
3388458337dbSTejun Heo 	case ATA_DNXFER_FORCE_PIO:
3389458337dbSTejun Heo 		mwdma_mask = 0;
3390458337dbSTejun Heo 		udma_mask = 0;
3391458337dbSTejun Heo 		break;
3392458337dbSTejun Heo 
3393458337dbSTejun Heo 	default:
3394458337dbSTejun Heo 		BUG();
3395458337dbSTejun Heo 	}
3396458337dbSTejun Heo 
3397458337dbSTejun Heo 	xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
3398458337dbSTejun Heo 
3399458337dbSTejun Heo 	if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
3400458337dbSTejun Heo 		return -ENOENT;
3401458337dbSTejun Heo 
3402458337dbSTejun Heo 	if (!quiet) {
3403458337dbSTejun Heo 		if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
3404458337dbSTejun Heo 			snprintf(buf, sizeof(buf), "%s:%s",
3405458337dbSTejun Heo 				 ata_mode_string(xfer_mask),
3406458337dbSTejun Heo 				 ata_mode_string(xfer_mask & ATA_MASK_PIO));
3407458337dbSTejun Heo 		else
3408458337dbSTejun Heo 			snprintf(buf, sizeof(buf), "%s",
3409458337dbSTejun Heo 				 ata_mode_string(xfer_mask));
3410458337dbSTejun Heo 
3411a9a79dfeSJoe Perches 		ata_dev_warn(dev, "limiting speed to %s\n", buf);
3412458337dbSTejun Heo 	}
3413c6fd2807SJeff Garzik 
3414c6fd2807SJeff Garzik 	ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
3415c6fd2807SJeff Garzik 			    &dev->udma_mask);
3416c6fd2807SJeff Garzik 
3417c6fd2807SJeff Garzik 	return 0;
3418c6fd2807SJeff Garzik }
3419c6fd2807SJeff Garzik 
3420c6fd2807SJeff Garzik static int ata_dev_set_mode(struct ata_device *dev)
3421c6fd2807SJeff Garzik {
3422d0cb43b3STejun Heo 	struct ata_port *ap = dev->link->ap;
34239af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
3424d0cb43b3STejun Heo 	const bool nosetxfer = dev->horkage & ATA_HORKAGE_NOSETXFER;
34254055dee7STejun Heo 	const char *dev_err_whine = "";
34264055dee7STejun Heo 	int ign_dev_err = 0;
3427d0cb43b3STejun Heo 	unsigned int err_mask = 0;
3428c6fd2807SJeff Garzik 	int rc;
3429c6fd2807SJeff Garzik 
3430c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_PIO;
3431c6fd2807SJeff Garzik 	if (dev->xfer_shift == ATA_SHIFT_PIO)
3432c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_PIO;
3433c6fd2807SJeff Garzik 
3434d0cb43b3STejun Heo 	if (nosetxfer && ap->flags & ATA_FLAG_SATA && ata_id_is_sata(dev->id))
3435d0cb43b3STejun Heo 		dev_err_whine = " (SET_XFERMODE skipped)";
3436d0cb43b3STejun Heo 	else {
3437d0cb43b3STejun Heo 		if (nosetxfer)
3438a9a79dfeSJoe Perches 			ata_dev_warn(dev,
3439d0cb43b3STejun Heo 				     "NOSETXFER but PATA detected - can't "
3440d0cb43b3STejun Heo 				     "skip SETXFER, might malfunction\n");
3441c6fd2807SJeff Garzik 		err_mask = ata_dev_set_xfermode(dev);
3442d0cb43b3STejun Heo 	}
34432dcb407eSJeff Garzik 
34444055dee7STejun Heo 	if (err_mask & ~AC_ERR_DEV)
34454055dee7STejun Heo 		goto fail;
34462dcb407eSJeff Garzik 
34474055dee7STejun Heo 	/* revalidate */
3448baa1e78aSTejun Heo 	ehc->i.flags |= ATA_EHI_POST_SETMODE;
3449422c9daaSTejun Heo 	rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
3450baa1e78aSTejun Heo 	ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
3451c6fd2807SJeff Garzik 	if (rc)
3452c6fd2807SJeff Garzik 		return rc;
3453c6fd2807SJeff Garzik 
3454b93fda12SAlan Cox 	if (dev->xfer_shift == ATA_SHIFT_PIO) {
34554055dee7STejun Heo 		/* Old CFA may refuse this command, which is just fine */
3456b93fda12SAlan Cox 		if (ata_id_is_cfa(dev->id))
34574055dee7STejun Heo 			ign_dev_err = 1;
3458b93fda12SAlan Cox 		/* Catch several broken garbage emulations plus some pre
3459b93fda12SAlan Cox 		   ATA devices */
3460b93fda12SAlan Cox 		if (ata_id_major_version(dev->id) == 0 &&
34614055dee7STejun Heo 					dev->pio_mode <= XFER_PIO_2)
34624055dee7STejun Heo 			ign_dev_err = 1;
3463b93fda12SAlan Cox 		/* Some very old devices and some bad newer ones fail
3464b93fda12SAlan Cox 		   any kind of SET_XFERMODE request but support PIO0-2
3465b93fda12SAlan Cox 		   timings and no IORDY */
3466b93fda12SAlan Cox 		if (!ata_id_has_iordy(dev->id) && dev->pio_mode <= XFER_PIO_2)
3467b93fda12SAlan Cox 			ign_dev_err = 1;
3468b93fda12SAlan Cox 	}
34694055dee7STejun Heo 	/* Early MWDMA devices do DMA but don't allow DMA mode setting.
34704055dee7STejun Heo 	   Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
34714055dee7STejun Heo 	if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
34724055dee7STejun Heo 	    dev->dma_mode == XFER_MW_DMA_0 &&
34734055dee7STejun Heo 	    (dev->id[63] >> 8) & 1)
34744055dee7STejun Heo 		ign_dev_err = 1;
34754055dee7STejun Heo 
34764055dee7STejun Heo 	/* if the device is actually configured correctly, ignore dev err */
34774055dee7STejun Heo 	if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id)))
34784055dee7STejun Heo 		ign_dev_err = 1;
34794055dee7STejun Heo 
34804055dee7STejun Heo 	if (err_mask & AC_ERR_DEV) {
34814055dee7STejun Heo 		if (!ign_dev_err)
34824055dee7STejun Heo 			goto fail;
34834055dee7STejun Heo 		else
34844055dee7STejun Heo 			dev_err_whine = " (device error ignored)";
34854055dee7STejun Heo 	}
34864055dee7STejun Heo 
3487c6fd2807SJeff Garzik 	DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
3488c6fd2807SJeff Garzik 		dev->xfer_shift, (int)dev->xfer_mode);
3489c6fd2807SJeff Garzik 
3490a9a79dfeSJoe Perches 	ata_dev_info(dev, "configured for %s%s\n",
34914055dee7STejun Heo 		     ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)),
34924055dee7STejun Heo 		     dev_err_whine);
34934055dee7STejun Heo 
3494c6fd2807SJeff Garzik 	return 0;
34954055dee7STejun Heo 
34964055dee7STejun Heo  fail:
3497a9a79dfeSJoe Perches 	ata_dev_err(dev, "failed to set xfermode (err_mask=0x%x)\n", err_mask);
34984055dee7STejun Heo 	return -EIO;
3499c6fd2807SJeff Garzik }
3500c6fd2807SJeff Garzik 
3501c6fd2807SJeff Garzik /**
350204351821SAlan  *	ata_do_set_mode - Program timings and issue SET FEATURES - XFER
35030260731fSTejun Heo  *	@link: link on which timings will be programmed
35041967b7ffSJoe Perches  *	@r_failed_dev: out parameter for failed device
3505c6fd2807SJeff Garzik  *
350604351821SAlan  *	Standard implementation of the function used to tune and set
350704351821SAlan  *	ATA device disk transfer mode (PIO3, UDMA6, etc.).  If
350804351821SAlan  *	ata_dev_set_mode() fails, pointer to the failing device is
3509c6fd2807SJeff Garzik  *	returned in @r_failed_dev.
3510c6fd2807SJeff Garzik  *
3511c6fd2807SJeff Garzik  *	LOCKING:
3512c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
3513c6fd2807SJeff Garzik  *
3514c6fd2807SJeff Garzik  *	RETURNS:
3515c6fd2807SJeff Garzik  *	0 on success, negative errno otherwise
3516c6fd2807SJeff Garzik  */
351704351821SAlan 
35180260731fSTejun Heo int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
3519c6fd2807SJeff Garzik {
35200260731fSTejun Heo 	struct ata_port *ap = link->ap;
3521c6fd2807SJeff Garzik 	struct ata_device *dev;
3522f58229f8STejun Heo 	int rc = 0, used_dma = 0, found = 0;
3523c6fd2807SJeff Garzik 
3524c6fd2807SJeff Garzik 	/* step 1: calculate xfer_mask */
35251eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
35267dc951aeSTejun Heo 		unsigned long pio_mask, dma_mask;
3527b3a70601SAlan Cox 		unsigned int mode_mask;
3528c6fd2807SJeff Garzik 
3529b3a70601SAlan Cox 		mode_mask = ATA_DMA_MASK_ATA;
3530b3a70601SAlan Cox 		if (dev->class == ATA_DEV_ATAPI)
3531b3a70601SAlan Cox 			mode_mask = ATA_DMA_MASK_ATAPI;
3532b3a70601SAlan Cox 		else if (ata_id_is_cfa(dev->id))
3533b3a70601SAlan Cox 			mode_mask = ATA_DMA_MASK_CFA;
3534b3a70601SAlan Cox 
3535c6fd2807SJeff Garzik 		ata_dev_xfermask(dev);
353633267325STejun Heo 		ata_force_xfermask(dev);
3537c6fd2807SJeff Garzik 
3538c6fd2807SJeff Garzik 		pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
3539b3a70601SAlan Cox 
3540b3a70601SAlan Cox 		if (libata_dma_mask & mode_mask)
354180a9c430SSergei Shtylyov 			dma_mask = ata_pack_xfermask(0, dev->mwdma_mask,
354280a9c430SSergei Shtylyov 						     dev->udma_mask);
3543b3a70601SAlan Cox 		else
3544b3a70601SAlan Cox 			dma_mask = 0;
3545b3a70601SAlan Cox 
3546c6fd2807SJeff Garzik 		dev->pio_mode = ata_xfer_mask2mode(pio_mask);
3547c6fd2807SJeff Garzik 		dev->dma_mode = ata_xfer_mask2mode(dma_mask);
3548c6fd2807SJeff Garzik 
3549c6fd2807SJeff Garzik 		found = 1;
3550b15b3ebaSAlan Cox 		if (ata_dma_enabled(dev))
3551c6fd2807SJeff Garzik 			used_dma = 1;
3552c6fd2807SJeff Garzik 	}
3553c6fd2807SJeff Garzik 	if (!found)
3554c6fd2807SJeff Garzik 		goto out;
3555c6fd2807SJeff Garzik 
3556c6fd2807SJeff Garzik 	/* step 2: always set host PIO timings */
35571eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
355870cd071eSTejun Heo 		if (dev->pio_mode == 0xff) {
3559a9a79dfeSJoe Perches 			ata_dev_warn(dev, "no PIO support\n");
3560c6fd2807SJeff Garzik 			rc = -EINVAL;
3561c6fd2807SJeff Garzik 			goto out;
3562c6fd2807SJeff Garzik 		}
3563c6fd2807SJeff Garzik 
3564c6fd2807SJeff Garzik 		dev->xfer_mode = dev->pio_mode;
3565c6fd2807SJeff Garzik 		dev->xfer_shift = ATA_SHIFT_PIO;
3566c6fd2807SJeff Garzik 		if (ap->ops->set_piomode)
3567c6fd2807SJeff Garzik 			ap->ops->set_piomode(ap, dev);
3568c6fd2807SJeff Garzik 	}
3569c6fd2807SJeff Garzik 
3570c6fd2807SJeff Garzik 	/* step 3: set host DMA timings */
35711eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
35721eca4365STejun Heo 		if (!ata_dma_enabled(dev))
3573c6fd2807SJeff Garzik 			continue;
3574c6fd2807SJeff Garzik 
3575c6fd2807SJeff Garzik 		dev->xfer_mode = dev->dma_mode;
3576c6fd2807SJeff Garzik 		dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
3577c6fd2807SJeff Garzik 		if (ap->ops->set_dmamode)
3578c6fd2807SJeff Garzik 			ap->ops->set_dmamode(ap, dev);
3579c6fd2807SJeff Garzik 	}
3580c6fd2807SJeff Garzik 
3581c6fd2807SJeff Garzik 	/* step 4: update devices' xfer mode */
35821eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
3583c6fd2807SJeff Garzik 		rc = ata_dev_set_mode(dev);
3584c6fd2807SJeff Garzik 		if (rc)
3585c6fd2807SJeff Garzik 			goto out;
3586c6fd2807SJeff Garzik 	}
3587c6fd2807SJeff Garzik 
3588c6fd2807SJeff Garzik 	/* Record simplex status. If we selected DMA then the other
3589c6fd2807SJeff Garzik 	 * host channels are not permitted to do so.
3590c6fd2807SJeff Garzik 	 */
3591cca3974eSJeff Garzik 	if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
3592032af1ceSAlan 		ap->host->simplex_claimed = ap;
3593c6fd2807SJeff Garzik 
3594c6fd2807SJeff Garzik  out:
3595c6fd2807SJeff Garzik 	if (rc)
3596c6fd2807SJeff Garzik 		*r_failed_dev = dev;
3597c6fd2807SJeff Garzik 	return rc;
3598c6fd2807SJeff Garzik }
3599c6fd2807SJeff Garzik 
3600c6fd2807SJeff Garzik /**
3601aa2731adSTejun Heo  *	ata_wait_ready - wait for link to become ready
3602aa2731adSTejun Heo  *	@link: link to be waited on
3603aa2731adSTejun Heo  *	@deadline: deadline jiffies for the operation
3604aa2731adSTejun Heo  *	@check_ready: callback to check link readiness
3605aa2731adSTejun Heo  *
3606aa2731adSTejun Heo  *	Wait for @link to become ready.  @check_ready should return
3607aa2731adSTejun Heo  *	positive number if @link is ready, 0 if it isn't, -ENODEV if
3608aa2731adSTejun Heo  *	link doesn't seem to be occupied, other errno for other error
3609aa2731adSTejun Heo  *	conditions.
3610aa2731adSTejun Heo  *
3611aa2731adSTejun Heo  *	Transient -ENODEV conditions are allowed for
3612aa2731adSTejun Heo  *	ATA_TMOUT_FF_WAIT.
3613aa2731adSTejun Heo  *
3614aa2731adSTejun Heo  *	LOCKING:
3615aa2731adSTejun Heo  *	EH context.
3616aa2731adSTejun Heo  *
3617aa2731adSTejun Heo  *	RETURNS:
3618c9b5560aSMasanari Iida  *	0 if @link is ready before @deadline; otherwise, -errno.
3619aa2731adSTejun Heo  */
3620aa2731adSTejun Heo int ata_wait_ready(struct ata_link *link, unsigned long deadline,
3621aa2731adSTejun Heo 		   int (*check_ready)(struct ata_link *link))
3622aa2731adSTejun Heo {
3623aa2731adSTejun Heo 	unsigned long start = jiffies;
3624b48d58f5STejun Heo 	unsigned long nodev_deadline;
3625aa2731adSTejun Heo 	int warned = 0;
3626aa2731adSTejun Heo 
3627b48d58f5STejun Heo 	/* choose which 0xff timeout to use, read comment in libata.h */
3628b48d58f5STejun Heo 	if (link->ap->host->flags & ATA_HOST_PARALLEL_SCAN)
3629b48d58f5STejun Heo 		nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT_LONG);
3630b48d58f5STejun Heo 	else
3631b48d58f5STejun Heo 		nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT);
3632b48d58f5STejun Heo 
3633b1c72916STejun Heo 	/* Slave readiness can't be tested separately from master.  On
3634b1c72916STejun Heo 	 * M/S emulation configuration, this function should be called
3635b1c72916STejun Heo 	 * only on the master and it will handle both master and slave.
3636b1c72916STejun Heo 	 */
3637b1c72916STejun Heo 	WARN_ON(link == link->ap->slave_link);
3638b1c72916STejun Heo 
3639aa2731adSTejun Heo 	if (time_after(nodev_deadline, deadline))
3640aa2731adSTejun Heo 		nodev_deadline = deadline;
3641aa2731adSTejun Heo 
3642aa2731adSTejun Heo 	while (1) {
3643aa2731adSTejun Heo 		unsigned long now = jiffies;
3644aa2731adSTejun Heo 		int ready, tmp;
3645aa2731adSTejun Heo 
3646aa2731adSTejun Heo 		ready = tmp = check_ready(link);
3647aa2731adSTejun Heo 		if (ready > 0)
3648aa2731adSTejun Heo 			return 0;
3649aa2731adSTejun Heo 
3650b48d58f5STejun Heo 		/*
3651b48d58f5STejun Heo 		 * -ENODEV could be transient.  Ignore -ENODEV if link
3652aa2731adSTejun Heo 		 * is online.  Also, some SATA devices take a long
3653b48d58f5STejun Heo 		 * time to clear 0xff after reset.  Wait for
3654b48d58f5STejun Heo 		 * ATA_TMOUT_FF_WAIT[_LONG] on -ENODEV if link isn't
3655b48d58f5STejun Heo 		 * offline.
3656aa2731adSTejun Heo 		 *
3657aa2731adSTejun Heo 		 * Note that some PATA controllers (pata_ali) explode
3658aa2731adSTejun Heo 		 * if status register is read more than once when
3659aa2731adSTejun Heo 		 * there's no device attached.
3660aa2731adSTejun Heo 		 */
3661aa2731adSTejun Heo 		if (ready == -ENODEV) {
3662aa2731adSTejun Heo 			if (ata_link_online(link))
3663aa2731adSTejun Heo 				ready = 0;
3664aa2731adSTejun Heo 			else if ((link->ap->flags & ATA_FLAG_SATA) &&
3665aa2731adSTejun Heo 				 !ata_link_offline(link) &&
3666aa2731adSTejun Heo 				 time_before(now, nodev_deadline))
3667aa2731adSTejun Heo 				ready = 0;
3668aa2731adSTejun Heo 		}
3669aa2731adSTejun Heo 
3670aa2731adSTejun Heo 		if (ready)
3671aa2731adSTejun Heo 			return ready;
3672aa2731adSTejun Heo 		if (time_after(now, deadline))
3673aa2731adSTejun Heo 			return -EBUSY;
3674aa2731adSTejun Heo 
3675aa2731adSTejun Heo 		if (!warned && time_after(now, start + 5 * HZ) &&
3676aa2731adSTejun Heo 		    (deadline - now > 3 * HZ)) {
3677a9a79dfeSJoe Perches 			ata_link_warn(link,
3678aa2731adSTejun Heo 				"link is slow to respond, please be patient "
3679aa2731adSTejun Heo 				"(ready=%d)\n", tmp);
3680aa2731adSTejun Heo 			warned = 1;
3681aa2731adSTejun Heo 		}
3682aa2731adSTejun Heo 
368397750cebSTejun Heo 		ata_msleep(link->ap, 50);
3684aa2731adSTejun Heo 	}
3685aa2731adSTejun Heo }
3686aa2731adSTejun Heo 
3687aa2731adSTejun Heo /**
3688aa2731adSTejun Heo  *	ata_wait_after_reset - wait for link to become ready after reset
3689aa2731adSTejun Heo  *	@link: link to be waited on
3690aa2731adSTejun Heo  *	@deadline: deadline jiffies for the operation
3691aa2731adSTejun Heo  *	@check_ready: callback to check link readiness
3692aa2731adSTejun Heo  *
3693aa2731adSTejun Heo  *	Wait for @link to become ready after reset.
3694aa2731adSTejun Heo  *
3695aa2731adSTejun Heo  *	LOCKING:
3696aa2731adSTejun Heo  *	EH context.
3697aa2731adSTejun Heo  *
3698aa2731adSTejun Heo  *	RETURNS:
3699c9b5560aSMasanari Iida  *	0 if @link is ready before @deadline; otherwise, -errno.
3700aa2731adSTejun Heo  */
37012b4221bbSHarvey Harrison int ata_wait_after_reset(struct ata_link *link, unsigned long deadline,
3702aa2731adSTejun Heo 				int (*check_ready)(struct ata_link *link))
3703aa2731adSTejun Heo {
370497750cebSTejun Heo 	ata_msleep(link->ap, ATA_WAIT_AFTER_RESET);
3705aa2731adSTejun Heo 
3706aa2731adSTejun Heo 	return ata_wait_ready(link, deadline, check_ready);
3707aa2731adSTejun Heo }
3708aa2731adSTejun Heo 
3709aa2731adSTejun Heo /**
3710936fd732STejun Heo  *	sata_link_debounce - debounce SATA phy status
3711936fd732STejun Heo  *	@link: ATA link to debounce SATA phy status for
3712c9b5560aSMasanari Iida  *	@params: timing parameters { interval, duration, timeout } in msec
3713d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
3714c6fd2807SJeff Garzik  *
3715936fd732STejun Heo  *	Make sure SStatus of @link reaches stable state, determined by
3716c6fd2807SJeff Garzik  *	holding the same value where DET is not 1 for @duration polled
3717c6fd2807SJeff Garzik  *	every @interval, before @timeout.  Timeout constraints the
3718d4b2bab4STejun Heo  *	beginning of the stable state.  Because DET gets stuck at 1 on
3719d4b2bab4STejun Heo  *	some controllers after hot unplugging, this functions waits
3720c6fd2807SJeff Garzik  *	until timeout then returns 0 if DET is stable at 1.
3721c6fd2807SJeff Garzik  *
3722d4b2bab4STejun Heo  *	@timeout is further limited by @deadline.  The sooner of the
3723d4b2bab4STejun Heo  *	two is used.
3724d4b2bab4STejun Heo  *
3725c6fd2807SJeff Garzik  *	LOCKING:
3726c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3727c6fd2807SJeff Garzik  *
3728c6fd2807SJeff Garzik  *	RETURNS:
3729c6fd2807SJeff Garzik  *	0 on success, -errno on failure.
3730c6fd2807SJeff Garzik  */
3731936fd732STejun Heo int sata_link_debounce(struct ata_link *link, const unsigned long *params,
3732d4b2bab4STejun Heo 		       unsigned long deadline)
3733c6fd2807SJeff Garzik {
3734341c2c95STejun Heo 	unsigned long interval = params[0];
3735341c2c95STejun Heo 	unsigned long duration = params[1];
3736d4b2bab4STejun Heo 	unsigned long last_jiffies, t;
3737c6fd2807SJeff Garzik 	u32 last, cur;
3738c6fd2807SJeff Garzik 	int rc;
3739c6fd2807SJeff Garzik 
3740341c2c95STejun Heo 	t = ata_deadline(jiffies, params[2]);
3741d4b2bab4STejun Heo 	if (time_before(t, deadline))
3742d4b2bab4STejun Heo 		deadline = t;
3743d4b2bab4STejun Heo 
3744936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3745c6fd2807SJeff Garzik 		return rc;
3746c6fd2807SJeff Garzik 	cur &= 0xf;
3747c6fd2807SJeff Garzik 
3748c6fd2807SJeff Garzik 	last = cur;
3749c6fd2807SJeff Garzik 	last_jiffies = jiffies;
3750c6fd2807SJeff Garzik 
3751c6fd2807SJeff Garzik 	while (1) {
375297750cebSTejun Heo 		ata_msleep(link->ap, interval);
3753936fd732STejun Heo 		if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3754c6fd2807SJeff Garzik 			return rc;
3755c6fd2807SJeff Garzik 		cur &= 0xf;
3756c6fd2807SJeff Garzik 
3757c6fd2807SJeff Garzik 		/* DET stable? */
3758c6fd2807SJeff Garzik 		if (cur == last) {
3759d4b2bab4STejun Heo 			if (cur == 1 && time_before(jiffies, deadline))
3760c6fd2807SJeff Garzik 				continue;
3761341c2c95STejun Heo 			if (time_after(jiffies,
3762341c2c95STejun Heo 				       ata_deadline(last_jiffies, duration)))
3763c6fd2807SJeff Garzik 				return 0;
3764c6fd2807SJeff Garzik 			continue;
3765c6fd2807SJeff Garzik 		}
3766c6fd2807SJeff Garzik 
3767c6fd2807SJeff Garzik 		/* unstable, start over */
3768c6fd2807SJeff Garzik 		last = cur;
3769c6fd2807SJeff Garzik 		last_jiffies = jiffies;
3770c6fd2807SJeff Garzik 
3771f1545154STejun Heo 		/* Check deadline.  If debouncing failed, return
3772f1545154STejun Heo 		 * -EPIPE to tell upper layer to lower link speed.
3773f1545154STejun Heo 		 */
3774d4b2bab4STejun Heo 		if (time_after(jiffies, deadline))
3775f1545154STejun Heo 			return -EPIPE;
3776c6fd2807SJeff Garzik 	}
3777c6fd2807SJeff Garzik }
3778c6fd2807SJeff Garzik 
3779c6fd2807SJeff Garzik /**
3780936fd732STejun Heo  *	sata_link_resume - resume SATA link
3781936fd732STejun Heo  *	@link: ATA link to resume SATA
3782c9b5560aSMasanari Iida  *	@params: timing parameters { interval, duration, timeout } in msec
3783d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
3784c6fd2807SJeff Garzik  *
3785936fd732STejun Heo  *	Resume SATA phy @link and debounce it.
3786c6fd2807SJeff Garzik  *
3787c6fd2807SJeff Garzik  *	LOCKING:
3788c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3789c6fd2807SJeff Garzik  *
3790c6fd2807SJeff Garzik  *	RETURNS:
3791c6fd2807SJeff Garzik  *	0 on success, -errno on failure.
3792c6fd2807SJeff Garzik  */
3793936fd732STejun Heo int sata_link_resume(struct ata_link *link, const unsigned long *params,
3794d4b2bab4STejun Heo 		     unsigned long deadline)
3795c6fd2807SJeff Garzik {
37965040ab67STejun Heo 	int tries = ATA_LINK_RESUME_TRIES;
3797ac371987STejun Heo 	u32 scontrol, serror;
3798c6fd2807SJeff Garzik 	int rc;
3799c6fd2807SJeff Garzik 
3800936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3801c6fd2807SJeff Garzik 		return rc;
3802c6fd2807SJeff Garzik 
38035040ab67STejun Heo 	/*
38045040ab67STejun Heo 	 * Writes to SControl sometimes get ignored under certain
38055040ab67STejun Heo 	 * controllers (ata_piix SIDPR).  Make sure DET actually is
38065040ab67STejun Heo 	 * cleared.
38075040ab67STejun Heo 	 */
38085040ab67STejun Heo 	do {
3809c6fd2807SJeff Garzik 		scontrol = (scontrol & 0x0f0) | 0x300;
3810936fd732STejun Heo 		if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3811c6fd2807SJeff Garzik 			return rc;
38125040ab67STejun Heo 		/*
38135040ab67STejun Heo 		 * Some PHYs react badly if SStatus is pounded
38145040ab67STejun Heo 		 * immediately after resuming.  Delay 200ms before
38155040ab67STejun Heo 		 * debouncing.
3816c6fd2807SJeff Garzik 		 */
3817e39b2bb3SDanesh Petigara 		if (!(link->flags & ATA_LFLAG_NO_DB_DELAY))
381897750cebSTejun Heo 			ata_msleep(link->ap, 200);
3819c6fd2807SJeff Garzik 
38205040ab67STejun Heo 		/* is SControl restored correctly? */
38215040ab67STejun Heo 		if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
38225040ab67STejun Heo 			return rc;
38235040ab67STejun Heo 	} while ((scontrol & 0xf0f) != 0x300 && --tries);
38245040ab67STejun Heo 
38255040ab67STejun Heo 	if ((scontrol & 0xf0f) != 0x300) {
382638941c95STejun Heo 		ata_link_warn(link, "failed to resume link (SControl %X)\n",
38275040ab67STejun Heo 			     scontrol);
38285040ab67STejun Heo 		return 0;
38295040ab67STejun Heo 	}
38305040ab67STejun Heo 
38315040ab67STejun Heo 	if (tries < ATA_LINK_RESUME_TRIES)
3832a9a79dfeSJoe Perches 		ata_link_warn(link, "link resume succeeded after %d retries\n",
38335040ab67STejun Heo 			      ATA_LINK_RESUME_TRIES - tries);
38345040ab67STejun Heo 
3835ac371987STejun Heo 	if ((rc = sata_link_debounce(link, params, deadline)))
3836ac371987STejun Heo 		return rc;
3837ac371987STejun Heo 
3838f046519fSTejun Heo 	/* clear SError, some PHYs require this even for SRST to work */
3839ac371987STejun Heo 	if (!(rc = sata_scr_read(link, SCR_ERROR, &serror)))
3840ac371987STejun Heo 		rc = sata_scr_write(link, SCR_ERROR, serror);
3841ac371987STejun Heo 
3842f046519fSTejun Heo 	return rc != -EINVAL ? rc : 0;
3843c6fd2807SJeff Garzik }
3844c6fd2807SJeff Garzik 
3845c6fd2807SJeff Garzik /**
38461152b261STejun Heo  *	sata_link_scr_lpm - manipulate SControl IPM and SPM fields
38471152b261STejun Heo  *	@link: ATA link to manipulate SControl for
38481152b261STejun Heo  *	@policy: LPM policy to configure
38491152b261STejun Heo  *	@spm_wakeup: initiate LPM transition to active state
38501152b261STejun Heo  *
38511152b261STejun Heo  *	Manipulate the IPM field of the SControl register of @link
38521152b261STejun Heo  *	according to @policy.  If @policy is ATA_LPM_MAX_POWER and
38531152b261STejun Heo  *	@spm_wakeup is %true, the SPM field is manipulated to wake up
38541152b261STejun Heo  *	the link.  This function also clears PHYRDY_CHG before
38551152b261STejun Heo  *	returning.
38561152b261STejun Heo  *
38571152b261STejun Heo  *	LOCKING:
38581152b261STejun Heo  *	EH context.
38591152b261STejun Heo  *
38601152b261STejun Heo  *	RETURNS:
38618485187bSMasanari Iida  *	0 on success, -errno otherwise.
38621152b261STejun Heo  */
38631152b261STejun Heo int sata_link_scr_lpm(struct ata_link *link, enum ata_lpm_policy policy,
38641152b261STejun Heo 		      bool spm_wakeup)
38651152b261STejun Heo {
38661152b261STejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
38671152b261STejun Heo 	bool woken_up = false;
38681152b261STejun Heo 	u32 scontrol;
38691152b261STejun Heo 	int rc;
38701152b261STejun Heo 
38711152b261STejun Heo 	rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
38721152b261STejun Heo 	if (rc)
38731152b261STejun Heo 		return rc;
38741152b261STejun Heo 
38751152b261STejun Heo 	switch (policy) {
38761152b261STejun Heo 	case ATA_LPM_MAX_POWER:
38771152b261STejun Heo 		/* disable all LPM transitions */
387865fe1f0fSShane Huang 		scontrol |= (0x7 << 8);
38791152b261STejun Heo 		/* initiate transition to active state */
38801152b261STejun Heo 		if (spm_wakeup) {
38811152b261STejun Heo 			scontrol |= (0x4 << 12);
38821152b261STejun Heo 			woken_up = true;
38831152b261STejun Heo 		}
38841152b261STejun Heo 		break;
38851152b261STejun Heo 	case ATA_LPM_MED_POWER:
38861152b261STejun Heo 		/* allow LPM to PARTIAL */
38871152b261STejun Heo 		scontrol &= ~(0x1 << 8);
388865fe1f0fSShane Huang 		scontrol |= (0x6 << 8);
38891152b261STejun Heo 		break;
38901152b261STejun Heo 	case ATA_LPM_MIN_POWER:
38918a745f1fSKristen Carlson Accardi 		if (ata_link_nr_enabled(link) > 0)
38921152b261STejun Heo 			/* no restrictions on LPM transitions */
389365fe1f0fSShane Huang 			scontrol &= ~(0x7 << 8);
38948a745f1fSKristen Carlson Accardi 		else {
38958a745f1fSKristen Carlson Accardi 			/* empty port, power off */
38968a745f1fSKristen Carlson Accardi 			scontrol &= ~0xf;
38978a745f1fSKristen Carlson Accardi 			scontrol |= (0x1 << 2);
38988a745f1fSKristen Carlson Accardi 		}
38991152b261STejun Heo 		break;
39001152b261STejun Heo 	default:
39011152b261STejun Heo 		WARN_ON(1);
39021152b261STejun Heo 	}
39031152b261STejun Heo 
39041152b261STejun Heo 	rc = sata_scr_write(link, SCR_CONTROL, scontrol);
39051152b261STejun Heo 	if (rc)
39061152b261STejun Heo 		return rc;
39071152b261STejun Heo 
39081152b261STejun Heo 	/* give the link time to transit out of LPM state */
39091152b261STejun Heo 	if (woken_up)
39101152b261STejun Heo 		msleep(10);
39111152b261STejun Heo 
39121152b261STejun Heo 	/* clear PHYRDY_CHG from SError */
39131152b261STejun Heo 	ehc->i.serror &= ~SERR_PHYRDY_CHG;
39141152b261STejun Heo 	return sata_scr_write(link, SCR_ERROR, SERR_PHYRDY_CHG);
39151152b261STejun Heo }
39161152b261STejun Heo 
39171152b261STejun Heo /**
39180aa1113dSTejun Heo  *	ata_std_prereset - prepare for reset
3919cc0680a5STejun Heo  *	@link: ATA link to be reset
3920d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
3921c6fd2807SJeff Garzik  *
3922cc0680a5STejun Heo  *	@link is about to be reset.  Initialize it.  Failure from
3923b8cffc6aSTejun Heo  *	prereset makes libata abort whole reset sequence and give up
3924b8cffc6aSTejun Heo  *	that port, so prereset should be best-effort.  It does its
3925b8cffc6aSTejun Heo  *	best to prepare for reset sequence but if things go wrong, it
3926b8cffc6aSTejun Heo  *	should just whine, not fail.
3927c6fd2807SJeff Garzik  *
3928c6fd2807SJeff Garzik  *	LOCKING:
3929c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3930c6fd2807SJeff Garzik  *
3931c6fd2807SJeff Garzik  *	RETURNS:
3932c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
3933c6fd2807SJeff Garzik  */
39340aa1113dSTejun Heo int ata_std_prereset(struct ata_link *link, unsigned long deadline)
3935c6fd2807SJeff Garzik {
3936cc0680a5STejun Heo 	struct ata_port *ap = link->ap;
3937936fd732STejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
3938c6fd2807SJeff Garzik 	const unsigned long *timing = sata_ehc_deb_timing(ehc);
3939c6fd2807SJeff Garzik 	int rc;
3940c6fd2807SJeff Garzik 
3941c6fd2807SJeff Garzik 	/* if we're about to do hardreset, nothing more to do */
3942c6fd2807SJeff Garzik 	if (ehc->i.action & ATA_EH_HARDRESET)
3943c6fd2807SJeff Garzik 		return 0;
3944c6fd2807SJeff Garzik 
3945936fd732STejun Heo 	/* if SATA, resume link */
3946a16abc0bSTejun Heo 	if (ap->flags & ATA_FLAG_SATA) {
3947936fd732STejun Heo 		rc = sata_link_resume(link, timing, deadline);
3948b8cffc6aSTejun Heo 		/* whine about phy resume failure but proceed */
3949b8cffc6aSTejun Heo 		if (rc && rc != -EOPNOTSUPP)
3950a9a79dfeSJoe Perches 			ata_link_warn(link,
3951a9a79dfeSJoe Perches 				      "failed to resume link for reset (errno=%d)\n",
3952a9a79dfeSJoe Perches 				      rc);
3953c6fd2807SJeff Garzik 	}
3954c6fd2807SJeff Garzik 
395545db2f6cSTejun Heo 	/* no point in trying softreset on offline link */
3956b1c72916STejun Heo 	if (ata_phys_link_offline(link))
395745db2f6cSTejun Heo 		ehc->i.action &= ~ATA_EH_SOFTRESET;
395845db2f6cSTejun Heo 
3959c6fd2807SJeff Garzik 	return 0;
3960c6fd2807SJeff Garzik }
3961c6fd2807SJeff Garzik 
3962c6fd2807SJeff Garzik /**
3963cc0680a5STejun Heo  *	sata_link_hardreset - reset link via SATA phy reset
3964cc0680a5STejun Heo  *	@link: link to reset
3965c9b5560aSMasanari Iida  *	@timing: timing parameters { interval, duration, timeout } in msec
3966d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
39679dadd45bSTejun Heo  *	@online: optional out parameter indicating link onlineness
39689dadd45bSTejun Heo  *	@check_ready: optional callback to check link readiness
3969c6fd2807SJeff Garzik  *
3970cc0680a5STejun Heo  *	SATA phy-reset @link using DET bits of SControl register.
39719dadd45bSTejun Heo  *	After hardreset, link readiness is waited upon using
39729dadd45bSTejun Heo  *	ata_wait_ready() if @check_ready is specified.  LLDs are
39739dadd45bSTejun Heo  *	allowed to not specify @check_ready and wait itself after this
39749dadd45bSTejun Heo  *	function returns.  Device classification is LLD's
39759dadd45bSTejun Heo  *	responsibility.
39769dadd45bSTejun Heo  *
39779dadd45bSTejun Heo  *	*@online is set to one iff reset succeeded and @link is online
39789dadd45bSTejun Heo  *	after reset.
3979c6fd2807SJeff Garzik  *
3980c6fd2807SJeff Garzik  *	LOCKING:
3981c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3982c6fd2807SJeff Garzik  *
3983c6fd2807SJeff Garzik  *	RETURNS:
3984c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
3985c6fd2807SJeff Garzik  */
3986cc0680a5STejun Heo int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
39879dadd45bSTejun Heo 			unsigned long deadline,
39889dadd45bSTejun Heo 			bool *online, int (*check_ready)(struct ata_link *))
3989c6fd2807SJeff Garzik {
3990c6fd2807SJeff Garzik 	u32 scontrol;
3991c6fd2807SJeff Garzik 	int rc;
3992c6fd2807SJeff Garzik 
3993c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
3994c6fd2807SJeff Garzik 
39959dadd45bSTejun Heo 	if (online)
39969dadd45bSTejun Heo 		*online = false;
39979dadd45bSTejun Heo 
3998936fd732STejun Heo 	if (sata_set_spd_needed(link)) {
3999c6fd2807SJeff Garzik 		/* SATA spec says nothing about how to reconfigure
4000c6fd2807SJeff Garzik 		 * spd.  To be on the safe side, turn off phy during
4001c6fd2807SJeff Garzik 		 * reconfiguration.  This works for at least ICH7 AHCI
4002c6fd2807SJeff Garzik 		 * and Sil3124.
4003c6fd2807SJeff Garzik 		 */
4004936fd732STejun Heo 		if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
4005b6103f6dSTejun Heo 			goto out;
4006c6fd2807SJeff Garzik 
4007cea0d336SJeff Garzik 		scontrol = (scontrol & 0x0f0) | 0x304;
4008c6fd2807SJeff Garzik 
4009936fd732STejun Heo 		if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
4010b6103f6dSTejun Heo 			goto out;
4011c6fd2807SJeff Garzik 
4012936fd732STejun Heo 		sata_set_spd(link);
4013c6fd2807SJeff Garzik 	}
4014c6fd2807SJeff Garzik 
4015c6fd2807SJeff Garzik 	/* issue phy wake/reset */
4016936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
4017b6103f6dSTejun Heo 		goto out;
4018c6fd2807SJeff Garzik 
4019c6fd2807SJeff Garzik 	scontrol = (scontrol & 0x0f0) | 0x301;
4020c6fd2807SJeff Garzik 
4021936fd732STejun Heo 	if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
4022b6103f6dSTejun Heo 		goto out;
4023c6fd2807SJeff Garzik 
4024c6fd2807SJeff Garzik 	/* Couldn't find anything in SATA I/II specs, but AHCI-1.1
4025c6fd2807SJeff Garzik 	 * 10.4.2 says at least 1 ms.
4026c6fd2807SJeff Garzik 	 */
402797750cebSTejun Heo 	ata_msleep(link->ap, 1);
4028c6fd2807SJeff Garzik 
4029936fd732STejun Heo 	/* bring link back */
4030936fd732STejun Heo 	rc = sata_link_resume(link, timing, deadline);
40319dadd45bSTejun Heo 	if (rc)
40329dadd45bSTejun Heo 		goto out;
40339dadd45bSTejun Heo 	/* if link is offline nothing more to do */
4034b1c72916STejun Heo 	if (ata_phys_link_offline(link))
40359dadd45bSTejun Heo 		goto out;
40369dadd45bSTejun Heo 
40379dadd45bSTejun Heo 	/* Link is online.  From this point, -ENODEV too is an error. */
40389dadd45bSTejun Heo 	if (online)
40399dadd45bSTejun Heo 		*online = true;
40409dadd45bSTejun Heo 
4041071f44b1STejun Heo 	if (sata_pmp_supported(link->ap) && ata_is_host_link(link)) {
40429dadd45bSTejun Heo 		/* If PMP is supported, we have to do follow-up SRST.
40439dadd45bSTejun Heo 		 * Some PMPs don't send D2H Reg FIS after hardreset if
40449dadd45bSTejun Heo 		 * the first port is empty.  Wait only for
40459dadd45bSTejun Heo 		 * ATA_TMOUT_PMP_SRST_WAIT.
40469dadd45bSTejun Heo 		 */
40479dadd45bSTejun Heo 		if (check_ready) {
40489dadd45bSTejun Heo 			unsigned long pmp_deadline;
40499dadd45bSTejun Heo 
4050341c2c95STejun Heo 			pmp_deadline = ata_deadline(jiffies,
4051341c2c95STejun Heo 						    ATA_TMOUT_PMP_SRST_WAIT);
40529dadd45bSTejun Heo 			if (time_after(pmp_deadline, deadline))
40539dadd45bSTejun Heo 				pmp_deadline = deadline;
40549dadd45bSTejun Heo 			ata_wait_ready(link, pmp_deadline, check_ready);
40559dadd45bSTejun Heo 		}
40569dadd45bSTejun Heo 		rc = -EAGAIN;
40579dadd45bSTejun Heo 		goto out;
40589dadd45bSTejun Heo 	}
40599dadd45bSTejun Heo 
40609dadd45bSTejun Heo 	rc = 0;
40619dadd45bSTejun Heo 	if (check_ready)
40629dadd45bSTejun Heo 		rc = ata_wait_ready(link, deadline, check_ready);
4063b6103f6dSTejun Heo  out:
40640cbf0711STejun Heo 	if (rc && rc != -EAGAIN) {
40650cbf0711STejun Heo 		/* online is set iff link is online && reset succeeded */
40660cbf0711STejun Heo 		if (online)
40670cbf0711STejun Heo 			*online = false;
4068a9a79dfeSJoe Perches 		ata_link_err(link, "COMRESET failed (errno=%d)\n", rc);
40690cbf0711STejun Heo 	}
4070b6103f6dSTejun Heo 	DPRINTK("EXIT, rc=%d\n", rc);
4071b6103f6dSTejun Heo 	return rc;
4072b6103f6dSTejun Heo }
4073b6103f6dSTejun Heo 
4074b6103f6dSTejun Heo /**
407557c9efdfSTejun Heo  *	sata_std_hardreset - COMRESET w/o waiting or classification
407657c9efdfSTejun Heo  *	@link: link to reset
407757c9efdfSTejun Heo  *	@class: resulting class of attached device
407857c9efdfSTejun Heo  *	@deadline: deadline jiffies for the operation
407957c9efdfSTejun Heo  *
408057c9efdfSTejun Heo  *	Standard SATA COMRESET w/o waiting or classification.
408157c9efdfSTejun Heo  *
408257c9efdfSTejun Heo  *	LOCKING:
408357c9efdfSTejun Heo  *	Kernel thread context (may sleep)
408457c9efdfSTejun Heo  *
408557c9efdfSTejun Heo  *	RETURNS:
408657c9efdfSTejun Heo  *	0 if link offline, -EAGAIN if link online, -errno on errors.
408757c9efdfSTejun Heo  */
408857c9efdfSTejun Heo int sata_std_hardreset(struct ata_link *link, unsigned int *class,
408957c9efdfSTejun Heo 		       unsigned long deadline)
409057c9efdfSTejun Heo {
409157c9efdfSTejun Heo 	const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
409257c9efdfSTejun Heo 	bool online;
409357c9efdfSTejun Heo 	int rc;
409457c9efdfSTejun Heo 
409557c9efdfSTejun Heo 	/* do hardreset */
409657c9efdfSTejun Heo 	rc = sata_link_hardreset(link, timing, deadline, &online, NULL);
409757c9efdfSTejun Heo 	return online ? -EAGAIN : rc;
409857c9efdfSTejun Heo }
409957c9efdfSTejun Heo 
410057c9efdfSTejun Heo /**
4101203c75b8STejun Heo  *	ata_std_postreset - standard postreset callback
4102cc0680a5STejun Heo  *	@link: the target ata_link
4103c6fd2807SJeff Garzik  *	@classes: classes of attached devices
4104c6fd2807SJeff Garzik  *
4105c6fd2807SJeff Garzik  *	This function is invoked after a successful reset.  Note that
4106c6fd2807SJeff Garzik  *	the device might have been reset more than once using
4107c6fd2807SJeff Garzik  *	different reset methods before postreset is invoked.
4108c6fd2807SJeff Garzik  *
4109c6fd2807SJeff Garzik  *	LOCKING:
4110c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
4111c6fd2807SJeff Garzik  */
4112203c75b8STejun Heo void ata_std_postreset(struct ata_link *link, unsigned int *classes)
4113c6fd2807SJeff Garzik {
4114f046519fSTejun Heo 	u32 serror;
4115f046519fSTejun Heo 
4116c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
4117c6fd2807SJeff Garzik 
4118f046519fSTejun Heo 	/* reset complete, clear SError */
4119f046519fSTejun Heo 	if (!sata_scr_read(link, SCR_ERROR, &serror))
4120f046519fSTejun Heo 		sata_scr_write(link, SCR_ERROR, serror);
4121f046519fSTejun Heo 
4122c6fd2807SJeff Garzik 	/* print link status */
4123936fd732STejun Heo 	sata_print_link_status(link);
4124c6fd2807SJeff Garzik 
4125c6fd2807SJeff Garzik 	DPRINTK("EXIT\n");
4126c6fd2807SJeff Garzik }
4127c6fd2807SJeff Garzik 
4128c6fd2807SJeff Garzik /**
4129c6fd2807SJeff Garzik  *	ata_dev_same_device - Determine whether new ID matches configured device
4130c6fd2807SJeff Garzik  *	@dev: device to compare against
4131c6fd2807SJeff Garzik  *	@new_class: class of the new device
4132c6fd2807SJeff Garzik  *	@new_id: IDENTIFY page of the new device
4133c6fd2807SJeff Garzik  *
4134c6fd2807SJeff Garzik  *	Compare @new_class and @new_id against @dev and determine
4135c6fd2807SJeff Garzik  *	whether @dev is the device indicated by @new_class and
4136c6fd2807SJeff Garzik  *	@new_id.
4137c6fd2807SJeff Garzik  *
4138c6fd2807SJeff Garzik  *	LOCKING:
4139c6fd2807SJeff Garzik  *	None.
4140c6fd2807SJeff Garzik  *
4141c6fd2807SJeff Garzik  *	RETURNS:
4142c6fd2807SJeff Garzik  *	1 if @dev matches @new_class and @new_id, 0 otherwise.
4143c6fd2807SJeff Garzik  */
4144c6fd2807SJeff Garzik static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
4145c6fd2807SJeff Garzik 			       const u16 *new_id)
4146c6fd2807SJeff Garzik {
4147c6fd2807SJeff Garzik 	const u16 *old_id = dev->id;
4148a0cf733bSTejun Heo 	unsigned char model[2][ATA_ID_PROD_LEN + 1];
4149a0cf733bSTejun Heo 	unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
4150c6fd2807SJeff Garzik 
4151c6fd2807SJeff Garzik 	if (dev->class != new_class) {
4152a9a79dfeSJoe Perches 		ata_dev_info(dev, "class mismatch %d != %d\n",
4153c6fd2807SJeff Garzik 			     dev->class, new_class);
4154c6fd2807SJeff Garzik 		return 0;
4155c6fd2807SJeff Garzik 	}
4156c6fd2807SJeff Garzik 
4157a0cf733bSTejun Heo 	ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
4158a0cf733bSTejun Heo 	ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
4159a0cf733bSTejun Heo 	ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
4160a0cf733bSTejun Heo 	ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
4161c6fd2807SJeff Garzik 
4162c6fd2807SJeff Garzik 	if (strcmp(model[0], model[1])) {
4163a9a79dfeSJoe Perches 		ata_dev_info(dev, "model number mismatch '%s' != '%s'\n",
4164a9a79dfeSJoe Perches 			     model[0], model[1]);
4165c6fd2807SJeff Garzik 		return 0;
4166c6fd2807SJeff Garzik 	}
4167c6fd2807SJeff Garzik 
4168c6fd2807SJeff Garzik 	if (strcmp(serial[0], serial[1])) {
4169a9a79dfeSJoe Perches 		ata_dev_info(dev, "serial number mismatch '%s' != '%s'\n",
4170a9a79dfeSJoe Perches 			     serial[0], serial[1]);
4171c6fd2807SJeff Garzik 		return 0;
4172c6fd2807SJeff Garzik 	}
4173c6fd2807SJeff Garzik 
4174c6fd2807SJeff Garzik 	return 1;
4175c6fd2807SJeff Garzik }
4176c6fd2807SJeff Garzik 
4177c6fd2807SJeff Garzik /**
4178fe30911bSTejun Heo  *	ata_dev_reread_id - Re-read IDENTIFY data
41793fae450cSHenrik Kretzschmar  *	@dev: target ATA device
4180bff04647STejun Heo  *	@readid_flags: read ID flags
4181c6fd2807SJeff Garzik  *
4182c6fd2807SJeff Garzik  *	Re-read IDENTIFY page and make sure @dev is still attached to
4183c6fd2807SJeff Garzik  *	the port.
4184c6fd2807SJeff Garzik  *
4185c6fd2807SJeff Garzik  *	LOCKING:
4186c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
4187c6fd2807SJeff Garzik  *
4188c6fd2807SJeff Garzik  *	RETURNS:
4189c6fd2807SJeff Garzik  *	0 on success, negative errno otherwise
4190c6fd2807SJeff Garzik  */
4191fe30911bSTejun Heo int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
4192c6fd2807SJeff Garzik {
4193c6fd2807SJeff Garzik 	unsigned int class = dev->class;
41949af5c9c9STejun Heo 	u16 *id = (void *)dev->link->ap->sector_buf;
4195c6fd2807SJeff Garzik 	int rc;
4196c6fd2807SJeff Garzik 
4197c6fd2807SJeff Garzik 	/* read ID data */
4198bff04647STejun Heo 	rc = ata_dev_read_id(dev, &class, readid_flags, id);
4199c6fd2807SJeff Garzik 	if (rc)
4200fe30911bSTejun Heo 		return rc;
4201c6fd2807SJeff Garzik 
4202c6fd2807SJeff Garzik 	/* is the device still there? */
4203fe30911bSTejun Heo 	if (!ata_dev_same_device(dev, class, id))
4204fe30911bSTejun Heo 		return -ENODEV;
4205c6fd2807SJeff Garzik 
4206c6fd2807SJeff Garzik 	memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
4207fe30911bSTejun Heo 	return 0;
4208fe30911bSTejun Heo }
4209fe30911bSTejun Heo 
4210fe30911bSTejun Heo /**
4211fe30911bSTejun Heo  *	ata_dev_revalidate - Revalidate ATA device
4212fe30911bSTejun Heo  *	@dev: device to revalidate
4213422c9daaSTejun Heo  *	@new_class: new class code
4214fe30911bSTejun Heo  *	@readid_flags: read ID flags
4215fe30911bSTejun Heo  *
4216fe30911bSTejun Heo  *	Re-read IDENTIFY page, make sure @dev is still attached to the
4217fe30911bSTejun Heo  *	port and reconfigure it according to the new IDENTIFY page.
4218fe30911bSTejun Heo  *
4219fe30911bSTejun Heo  *	LOCKING:
4220fe30911bSTejun Heo  *	Kernel thread context (may sleep)
4221fe30911bSTejun Heo  *
4222fe30911bSTejun Heo  *	RETURNS:
4223fe30911bSTejun Heo  *	0 on success, negative errno otherwise
4224fe30911bSTejun Heo  */
4225422c9daaSTejun Heo int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
4226422c9daaSTejun Heo 		       unsigned int readid_flags)
4227fe30911bSTejun Heo {
42286ddcd3b0STejun Heo 	u64 n_sectors = dev->n_sectors;
42295920dadfSTejun Heo 	u64 n_native_sectors = dev->n_native_sectors;
4230fe30911bSTejun Heo 	int rc;
4231fe30911bSTejun Heo 
4232fe30911bSTejun Heo 	if (!ata_dev_enabled(dev))
4233fe30911bSTejun Heo 		return -ENODEV;
4234fe30911bSTejun Heo 
4235422c9daaSTejun Heo 	/* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
4236422c9daaSTejun Heo 	if (ata_class_enabled(new_class) &&
4237f0d0613dSBorislav Petkov 	    new_class != ATA_DEV_ATA &&
4238f0d0613dSBorislav Petkov 	    new_class != ATA_DEV_ATAPI &&
42399162c657SHannes Reinecke 	    new_class != ATA_DEV_ZAC &&
4240f0d0613dSBorislav Petkov 	    new_class != ATA_DEV_SEMB) {
4241a9a79dfeSJoe Perches 		ata_dev_info(dev, "class mismatch %u != %u\n",
4242422c9daaSTejun Heo 			     dev->class, new_class);
4243422c9daaSTejun Heo 		rc = -ENODEV;
4244422c9daaSTejun Heo 		goto fail;
4245422c9daaSTejun Heo 	}
4246422c9daaSTejun Heo 
4247fe30911bSTejun Heo 	/* re-read ID */
4248fe30911bSTejun Heo 	rc = ata_dev_reread_id(dev, readid_flags);
4249fe30911bSTejun Heo 	if (rc)
4250fe30911bSTejun Heo 		goto fail;
4251c6fd2807SJeff Garzik 
4252c6fd2807SJeff Garzik 	/* configure device according to the new ID */
4253efdaedc4STejun Heo 	rc = ata_dev_configure(dev);
42546ddcd3b0STejun Heo 	if (rc)
42556ddcd3b0STejun Heo 		goto fail;
42566ddcd3b0STejun Heo 
42576ddcd3b0STejun Heo 	/* verify n_sectors hasn't changed */
4258445d211bSTejun Heo 	if (dev->class != ATA_DEV_ATA || !n_sectors ||
4259445d211bSTejun Heo 	    dev->n_sectors == n_sectors)
4260445d211bSTejun Heo 		return 0;
4261445d211bSTejun Heo 
4262445d211bSTejun Heo 	/* n_sectors has changed */
4263a9a79dfeSJoe Perches 	ata_dev_warn(dev, "n_sectors mismatch %llu != %llu\n",
42646ddcd3b0STejun Heo 		     (unsigned long long)n_sectors,
42656ddcd3b0STejun Heo 		     (unsigned long long)dev->n_sectors);
4266445d211bSTejun Heo 
42675920dadfSTejun Heo 	/*
42685920dadfSTejun Heo 	 * Something could have caused HPA to be unlocked
4269445d211bSTejun Heo 	 * involuntarily.  If n_native_sectors hasn't changed and the
4270445d211bSTejun Heo 	 * new size matches it, keep the device.
42715920dadfSTejun Heo 	 */
42725920dadfSTejun Heo 	if (dev->n_native_sectors == n_native_sectors &&
4273445d211bSTejun Heo 	    dev->n_sectors > n_sectors && dev->n_sectors == n_native_sectors) {
4274a9a79dfeSJoe Perches 		ata_dev_warn(dev,
42755920dadfSTejun Heo 			     "new n_sectors matches native, probably "
427668939ce5STejun Heo 			     "late HPA unlock, n_sectors updated\n");
427768939ce5STejun Heo 		/* use the larger n_sectors */
4278445d211bSTejun Heo 		return 0;
4279445d211bSTejun Heo 	}
4280445d211bSTejun Heo 
4281445d211bSTejun Heo 	/*
4282445d211bSTejun Heo 	 * Some BIOSes boot w/o HPA but resume w/ HPA locked.  Try
4283445d211bSTejun Heo 	 * unlocking HPA in those cases.
4284445d211bSTejun Heo 	 *
4285445d211bSTejun Heo 	 * https://bugzilla.kernel.org/show_bug.cgi?id=15396
4286445d211bSTejun Heo 	 */
4287445d211bSTejun Heo 	if (dev->n_native_sectors == n_native_sectors &&
4288445d211bSTejun Heo 	    dev->n_sectors < n_sectors && n_sectors == n_native_sectors &&
4289445d211bSTejun Heo 	    !(dev->horkage & ATA_HORKAGE_BROKEN_HPA)) {
4290a9a79dfeSJoe Perches 		ata_dev_warn(dev,
4291445d211bSTejun Heo 			     "old n_sectors matches native, probably "
4292445d211bSTejun Heo 			     "late HPA lock, will try to unlock HPA\n");
4293445d211bSTejun Heo 		/* try unlocking HPA */
4294445d211bSTejun Heo 		dev->flags |= ATA_DFLAG_UNLOCK_HPA;
4295445d211bSTejun Heo 		rc = -EIO;
4296445d211bSTejun Heo 	} else
4297445d211bSTejun Heo 		rc = -ENODEV;
4298445d211bSTejun Heo 
4299445d211bSTejun Heo 	/* restore original n_[native_]sectors and fail */
43005920dadfSTejun Heo 	dev->n_native_sectors = n_native_sectors;
43015920dadfSTejun Heo 	dev->n_sectors = n_sectors;
4302c6fd2807SJeff Garzik  fail:
4303a9a79dfeSJoe Perches 	ata_dev_err(dev, "revalidation failed (errno=%d)\n", rc);
4304c6fd2807SJeff Garzik 	return rc;
4305c6fd2807SJeff Garzik }
4306c6fd2807SJeff Garzik 
43076919a0a6SAlan Cox struct ata_blacklist_entry {
43086919a0a6SAlan Cox 	const char *model_num;
43096919a0a6SAlan Cox 	const char *model_rev;
43106919a0a6SAlan Cox 	unsigned long horkage;
43116919a0a6SAlan Cox };
43126919a0a6SAlan Cox 
43136919a0a6SAlan Cox static const struct ata_blacklist_entry ata_device_blacklist [] = {
43146919a0a6SAlan Cox 	/* Devices with DMA related problems under Linux */
43156919a0a6SAlan Cox 	{ "WDC AC11000H",	NULL,		ATA_HORKAGE_NODMA },
43166919a0a6SAlan Cox 	{ "WDC AC22100H",	NULL,		ATA_HORKAGE_NODMA },
43176919a0a6SAlan Cox 	{ "WDC AC32500H",	NULL,		ATA_HORKAGE_NODMA },
43186919a0a6SAlan Cox 	{ "WDC AC33100H",	NULL,		ATA_HORKAGE_NODMA },
43196919a0a6SAlan Cox 	{ "WDC AC31600H",	NULL,		ATA_HORKAGE_NODMA },
43206919a0a6SAlan Cox 	{ "WDC AC32100H",	"24.09P07",	ATA_HORKAGE_NODMA },
43216919a0a6SAlan Cox 	{ "WDC AC23200L",	"21.10N21",	ATA_HORKAGE_NODMA },
43226919a0a6SAlan Cox 	{ "Compaq CRD-8241B", 	NULL,		ATA_HORKAGE_NODMA },
43236919a0a6SAlan Cox 	{ "CRD-8400B",		NULL, 		ATA_HORKAGE_NODMA },
43247da4c935SMark Lord 	{ "CRD-848[02]B",	NULL,		ATA_HORKAGE_NODMA },
43256919a0a6SAlan Cox 	{ "CRD-84",		NULL,		ATA_HORKAGE_NODMA },
43266919a0a6SAlan Cox 	{ "SanDisk SDP3B",	NULL,		ATA_HORKAGE_NODMA },
43276919a0a6SAlan Cox 	{ "SanDisk SDP3B-64",	NULL,		ATA_HORKAGE_NODMA },
43286919a0a6SAlan Cox 	{ "SANYO CD-ROM CRD",	NULL,		ATA_HORKAGE_NODMA },
43296919a0a6SAlan Cox 	{ "HITACHI CDR-8",	NULL,		ATA_HORKAGE_NODMA },
43307da4c935SMark Lord 	{ "HITACHI CDR-8[34]35",NULL,		ATA_HORKAGE_NODMA },
43316919a0a6SAlan Cox 	{ "Toshiba CD-ROM XM-6202B", NULL,	ATA_HORKAGE_NODMA },
43326919a0a6SAlan Cox 	{ "TOSHIBA CD-ROM XM-1702BC", NULL,	ATA_HORKAGE_NODMA },
43336919a0a6SAlan Cox 	{ "CD-532E-A", 		NULL,		ATA_HORKAGE_NODMA },
43346919a0a6SAlan Cox 	{ "E-IDE CD-ROM CR-840",NULL,		ATA_HORKAGE_NODMA },
43356919a0a6SAlan Cox 	{ "CD-ROM Drive/F5A",	NULL,		ATA_HORKAGE_NODMA },
43366919a0a6SAlan Cox 	{ "WPI CDD-820", 	NULL,		ATA_HORKAGE_NODMA },
43376919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SC-148C", NULL,	ATA_HORKAGE_NODMA },
43386919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SC",	NULL,		ATA_HORKAGE_NODMA },
43396919a0a6SAlan Cox 	{ "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
43406919a0a6SAlan Cox 	{ "_NEC DV5800A", 	NULL,		ATA_HORKAGE_NODMA },
43416919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SN-124", "N001",	ATA_HORKAGE_NODMA },
434239f19886SDave Jones 	{ "Seagate STT20000A", NULL,		ATA_HORKAGE_NODMA },
4343d70e551cSPrarit Bhargava 	{ " 2GB ATA Flash Disk", "ADMA428M",	ATA_HORKAGE_NODMA },
4344b00622fcSShikha Jain 	{ "VRFDFC22048UCHC-TE*", NULL,		ATA_HORKAGE_NODMA },
43453af9a77aSTejun Heo 	/* Odd clown on sil3726/4726 PMPs */
434650af2fa1STejun Heo 	{ "Config  Disk",	NULL,		ATA_HORKAGE_DISABLE },
43476919a0a6SAlan Cox 
434818d6e9d5SAlbert Lee 	/* Weird ATAPI devices */
434940a1d531STejun Heo 	{ "TORiSAN DVD-ROM DRD-N216", NULL,	ATA_HORKAGE_MAX_SEC_128 },
43506a87e42eSTejun Heo 	{ "QUANTUM DAT    DAT72-000", NULL,	ATA_HORKAGE_ATAPI_MOD16_DMA },
4351a32450e1SShan Hai 	{ "Slimtype DVD A  DS8A8SH", NULL,	ATA_HORKAGE_MAX_SEC_LBA48 },
43520523f037SShan Hai 	{ "Slimtype DVD A  DS8A9SH", NULL,	ATA_HORKAGE_MAX_SEC_LBA48 },
435318d6e9d5SAlbert Lee 
4354af34d637SDavid Milburn 	/*
4355af34d637SDavid Milburn 	 * Causes silent data corruption with higher max sects.
4356af34d637SDavid Milburn 	 * http://lkml.kernel.org/g/x49wpy40ysk.fsf@segfault.boston.devel.redhat.com
4357af34d637SDavid Milburn 	 */
4358af34d637SDavid Milburn 	{ "ST380013AS",		"3.20",		ATA_HORKAGE_MAX_SEC_1024 },
4359af34d637SDavid Milburn 
43601488a1e3STejun Heo 	/*
4361e0edc8c5STejun Heo 	 * These devices time out with higher max sects.
43621488a1e3STejun Heo 	 * https://bugzilla.kernel.org/show_bug.cgi?id=121671
43631488a1e3STejun Heo 	 */
4364e0edc8c5STejun Heo 	{ "LITEON CX1-JB*-HP",	NULL,		ATA_HORKAGE_MAX_SEC_1024 },
43651488a1e3STejun Heo 
43666919a0a6SAlan Cox 	/* Devices we expect to fail diagnostics */
43676919a0a6SAlan Cox 
43686919a0a6SAlan Cox 	/* Devices where NCQ should be avoided */
43696919a0a6SAlan Cox 	/* NCQ is slow */
43706919a0a6SAlan Cox 	{ "WDC WD740ADFD-00",	NULL,		ATA_HORKAGE_NONCQ },
4371459ad688STejun Heo 	{ "WDC WD740ADFD-00NLR1", NULL,		ATA_HORKAGE_NONCQ, },
437209125ea6STejun Heo 	/* http://thread.gmane.org/gmane.linux.ide/14907 */
437309125ea6STejun Heo 	{ "FUJITSU MHT2060BH",	NULL,		ATA_HORKAGE_NONCQ },
43747acfaf30SPaul Rolland 	/* NCQ is broken */
4375539cc7c7SJeff Garzik 	{ "Maxtor *",		"BANC*",	ATA_HORKAGE_NONCQ },
43760e3dbc01SAlan Cox 	{ "Maxtor 7V300F0",	"VA111630",	ATA_HORKAGE_NONCQ },
4377da6f0ec2SPaolo Ornati 	{ "ST380817AS",		"3.42",		ATA_HORKAGE_NONCQ },
4378e41bd3e8STejun Heo 	{ "ST3160023AS",	"3.42",		ATA_HORKAGE_NONCQ },
43795ccfca97SLubomir Bulej 	{ "OCZ CORE_SSD",	"02.10104",	ATA_HORKAGE_NONCQ },
4380539cc7c7SJeff Garzik 
4381ac70a964STejun Heo 	/* Seagate NCQ + FLUSH CACHE firmware bug */
43824d1f9082SMark Lord 	{ "ST31500341AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
4383ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4384d10d491fSTejun Heo 
43854d1f9082SMark Lord 	{ "ST31000333AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
4386d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4387d10d491fSTejun Heo 
43884d1f9082SMark Lord 	{ "ST3640[36]23AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
4389d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4390d10d491fSTejun Heo 
43914d1f9082SMark Lord 	{ "ST3320[68]13AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
4392ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4393ac70a964STejun Heo 
439408c85d2aSAleksei Mamlin 	/* drives which fail FPDMA_AA activation (some may freeze afterwards) */
439587809942SMichele Baldessari 	{ "ST1000LM024 HN-M101MBB", "2AR10001",	ATA_HORKAGE_BROKEN_FPDMA_AA },
4396b28a613eSMichele Baldessari 	{ "ST1000LM024 HN-M101MBB", "2BA30001",	ATA_HORKAGE_BROKEN_FPDMA_AA },
439708c85d2aSAleksei Mamlin 	{ "VB0250EAVER",	"HPG7",		ATA_HORKAGE_BROKEN_FPDMA_AA },
439887809942SMichele Baldessari 
439936e337d0SRobert Hancock 	/* Blacklist entries taken from Silicon Image 3124/3132
440036e337d0SRobert Hancock 	   Windows driver .inf file - also several Linux problem reports */
440136e337d0SRobert Hancock 	{ "HTS541060G9SA00",    "MB3OC60D",     ATA_HORKAGE_NONCQ, },
440236e337d0SRobert Hancock 	{ "HTS541080G9SA00",    "MB4OC60D",     ATA_HORKAGE_NONCQ, },
440336e337d0SRobert Hancock 	{ "HTS541010G9SA00",    "MBZOC60D",     ATA_HORKAGE_NONCQ, },
44046919a0a6SAlan Cox 
440568b0ddb2STejun Heo 	/* https://bugzilla.kernel.org/show_bug.cgi?id=15573 */
440668b0ddb2STejun Heo 	{ "C300-CTFDDAC128MAG",	"0001",		ATA_HORKAGE_NONCQ, },
440768b0ddb2STejun Heo 
440816c55b03STejun Heo 	/* devices which puke on READ_NATIVE_MAX */
440916c55b03STejun Heo 	{ "HDS724040KLSA80",	"KFAOA20N",	ATA_HORKAGE_BROKEN_HPA, },
441016c55b03STejun Heo 	{ "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
441116c55b03STejun Heo 	{ "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
441216c55b03STejun Heo 	{ "MAXTOR 6L080L4",	"A93.0500",	ATA_HORKAGE_BROKEN_HPA },
44136919a0a6SAlan Cox 
44147831387bSTejun Heo 	/* this one allows HPA unlocking but fails IOs on the area */
44157831387bSTejun Heo 	{ "OCZ-VERTEX",		    "1.30",	ATA_HORKAGE_BROKEN_HPA },
44167831387bSTejun Heo 
441793328e11SAlan Cox 	/* Devices which report 1 sector over size HPA */
441893328e11SAlan Cox 	{ "ST340823A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
441993328e11SAlan Cox 	{ "ST320413A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
4420b152fcd3SMikko Rapeli 	{ "ST310211A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
442193328e11SAlan Cox 
44226bbfd53dSAlan Cox 	/* Devices which get the IVB wrong */
44236bbfd53dSAlan Cox 	{ "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB, },
4424a79067e5SAlan Cox 	/* Maybe we should just blacklist TSSTcorp... */
44257da4c935SMark Lord 	{ "TSSTcorp CDDVDW SH-S202[HJN]", "SB0[01]",  ATA_HORKAGE_IVB, },
44266bbfd53dSAlan Cox 
44279ce8e307SJens Axboe 	/* Devices that do not need bridging limits applied */
44289ce8e307SJens Axboe 	{ "MTRON MSP-SATA*",		NULL,	ATA_HORKAGE_BRIDGE_OK, },
442904d0f1b8SJeff Garzik 	{ "BUFFALO HD-QSU2/R5",		NULL,	ATA_HORKAGE_BRIDGE_OK, },
44309ce8e307SJens Axboe 
44319062712fSTejun Heo 	/* Devices which aren't very happy with higher link speeds */
44329062712fSTejun Heo 	{ "WD My Book",			NULL,	ATA_HORKAGE_1_5_GBPS, },
4433c531077fSDaniel J Blueman 	{ "Seagate FreeAgent GoFlex",	NULL,	ATA_HORKAGE_1_5_GBPS, },
44349062712fSTejun Heo 
4435d0cb43b3STejun Heo 	/*
4436d0cb43b3STejun Heo 	 * Devices which choke on SETXFER.  Applies only if both the
4437d0cb43b3STejun Heo 	 * device and controller are SATA.
4438d0cb43b3STejun Heo 	 */
4439cd691876STejun Heo 	{ "PIONEER DVD-RW  DVRTD08",	NULL,	ATA_HORKAGE_NOSETXFER },
44403a25179eSVladimir LAVALLADE 	{ "PIONEER DVD-RW  DVRTD08A",	NULL,	ATA_HORKAGE_NOSETXFER },
44413a25179eSVladimir LAVALLADE 	{ "PIONEER DVD-RW  DVR-215",	NULL,	ATA_HORKAGE_NOSETXFER },
4442cd691876STejun Heo 	{ "PIONEER DVD-RW  DVR-212D",	NULL,	ATA_HORKAGE_NOSETXFER },
4443cd691876STejun Heo 	{ "PIONEER DVD-RW  DVR-216D",	NULL,	ATA_HORKAGE_NOSETXFER },
4444d0cb43b3STejun Heo 
4445f78dea06SMarc Carino 	/* devices that don't properly handle queued TRIM commands */
4446243918beSMartin K. Petersen 	{ "Micron_M500_*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
4447e61f7d1cSMartin K. Petersen 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
4448ff7f53fbSMartin K. Petersen 	{ "Crucial_CT*M500*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
4449ff7f53fbSMartin K. Petersen 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
44509051bd39SMartin K. Petersen 	{ "Micron_M5[15]0_*",		"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
4451ff7f53fbSMartin K. Petersen 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
4452ff7f53fbSMartin K. Petersen 	{ "Crucial_CT*M550*",		"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
4453ff7f53fbSMartin K. Petersen 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
4454ff7f53fbSMartin K. Petersen 	{ "Crucial_CT*MX100*",		"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
4455ff7f53fbSMartin K. Petersen 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
44569a9324d3SMartin K. Petersen 	{ "Samsung SSD 8*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
44576fc4d97aSMartin K. Petersen 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
44587a7184b0SGuillermo A. Amaral 	{ "FCCT*M500*",			NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
44597a7184b0SGuillermo A. Amaral 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
4460e61f7d1cSMartin K. Petersen 
4461cda57b1bSArne Fitzenreiter 	/* devices that don't properly handle TRIM commands */
4462cda57b1bSArne Fitzenreiter 	{ "SuperSSpeed S238*",		NULL,	ATA_HORKAGE_NOTRIM, },
4463cda57b1bSArne Fitzenreiter 
4464e61f7d1cSMartin K. Petersen 	/*
4465e61f7d1cSMartin K. Petersen 	 * As defined, the DRAT (Deterministic Read After Trim) and RZAT
4466e61f7d1cSMartin K. Petersen 	 * (Return Zero After Trim) flags in the ATA Command Set are
4467e61f7d1cSMartin K. Petersen 	 * unreliable in the sense that they only define what happens if
4468e61f7d1cSMartin K. Petersen 	 * the device successfully executed the DSM TRIM command. TRIM
4469e61f7d1cSMartin K. Petersen 	 * is only advisory, however, and the device is free to silently
4470e61f7d1cSMartin K. Petersen 	 * ignore all or parts of the request.
4471e61f7d1cSMartin K. Petersen 	 *
4472e61f7d1cSMartin K. Petersen 	 * Whitelist drives that are known to reliably return zeroes
4473e61f7d1cSMartin K. Petersen 	 * after TRIM.
4474e61f7d1cSMartin K. Petersen 	 */
4475e61f7d1cSMartin K. Petersen 
4476e61f7d1cSMartin K. Petersen 	/*
4477e61f7d1cSMartin K. Petersen 	 * The intel 510 drive has buggy DRAT/RZAT. Explicitly exclude
4478e61f7d1cSMartin K. Petersen 	 * that model before whitelisting all other intel SSDs.
4479e61f7d1cSMartin K. Petersen 	 */
4480e61f7d1cSMartin K. Petersen 	{ "INTEL*SSDSC2MH*",		NULL,	0, },
4481e61f7d1cSMartin K. Petersen 
4482ff7f53fbSMartin K. Petersen 	{ "Micron*",			NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM, },
4483ff7f53fbSMartin K. Petersen 	{ "Crucial*",			NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM, },
4484e61f7d1cSMartin K. Petersen 	{ "INTEL*SSD*", 		NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM, },
4485e61f7d1cSMartin K. Petersen 	{ "SSD*INTEL*",			NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM, },
4486e61f7d1cSMartin K. Petersen 	{ "Samsung*SSD*",		NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM, },
4487e61f7d1cSMartin K. Petersen 	{ "SAMSUNG*SSD*",		NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM, },
4488e61f7d1cSMartin K. Petersen 	{ "ST[1248][0248]0[FH]*",	NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM, },
4489f78dea06SMarc Carino 
4490ecd75ad5STejun Heo 	/*
4491ecd75ad5STejun Heo 	 * Some WD SATA-I drives spin up and down erratically when the link
4492ecd75ad5STejun Heo 	 * is put into the slumber mode.  We don't have full list of the
4493ecd75ad5STejun Heo 	 * affected devices.  Disable LPM if the device matches one of the
4494ecd75ad5STejun Heo 	 * known prefixes and is SATA-1.  As a side effect LPM partial is
4495ecd75ad5STejun Heo 	 * lost too.
4496ecd75ad5STejun Heo 	 *
4497ecd75ad5STejun Heo 	 * https://bugzilla.kernel.org/show_bug.cgi?id=57211
4498ecd75ad5STejun Heo 	 */
4499ecd75ad5STejun Heo 	{ "WDC WD800JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4500ecd75ad5STejun Heo 	{ "WDC WD1200JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4501ecd75ad5STejun Heo 	{ "WDC WD1600JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4502ecd75ad5STejun Heo 	{ "WDC WD2000JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4503ecd75ad5STejun Heo 	{ "WDC WD2500JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4504ecd75ad5STejun Heo 	{ "WDC WD3000JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4505ecd75ad5STejun Heo 	{ "WDC WD3200JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4506ecd75ad5STejun Heo 
45076919a0a6SAlan Cox 	/* End Marker */
45086919a0a6SAlan Cox 	{ }
4509c6fd2807SJeff Garzik };
4510c6fd2807SJeff Garzik 
451175683fe7STejun Heo static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
4512c6fd2807SJeff Garzik {
45138bfa79fcSTejun Heo 	unsigned char model_num[ATA_ID_PROD_LEN + 1];
45148bfa79fcSTejun Heo 	unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
45156919a0a6SAlan Cox 	const struct ata_blacklist_entry *ad = ata_device_blacklist;
4516c6fd2807SJeff Garzik 
45178bfa79fcSTejun Heo 	ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
45188bfa79fcSTejun Heo 	ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
4519c6fd2807SJeff Garzik 
45206919a0a6SAlan Cox 	while (ad->model_num) {
45211c402799SGeorge Spelvin 		if (glob_match(ad->model_num, model_num)) {
45226919a0a6SAlan Cox 			if (ad->model_rev == NULL)
45236919a0a6SAlan Cox 				return ad->horkage;
45241c402799SGeorge Spelvin 			if (glob_match(ad->model_rev, model_rev))
45256919a0a6SAlan Cox 				return ad->horkage;
4526c6fd2807SJeff Garzik 		}
45276919a0a6SAlan Cox 		ad++;
4528c6fd2807SJeff Garzik 	}
4529c6fd2807SJeff Garzik 	return 0;
4530c6fd2807SJeff Garzik }
4531c6fd2807SJeff Garzik 
45326919a0a6SAlan Cox static int ata_dma_blacklisted(const struct ata_device *dev)
45336919a0a6SAlan Cox {
45346919a0a6SAlan Cox 	/* We don't support polling DMA.
45356919a0a6SAlan Cox 	 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
45366919a0a6SAlan Cox 	 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
45376919a0a6SAlan Cox 	 */
45389af5c9c9STejun Heo 	if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
45396919a0a6SAlan Cox 	    (dev->flags & ATA_DFLAG_CDB_INTR))
45406919a0a6SAlan Cox 		return 1;
454175683fe7STejun Heo 	return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
45426919a0a6SAlan Cox }
45436919a0a6SAlan Cox 
4544c6fd2807SJeff Garzik /**
45456bbfd53dSAlan Cox  *	ata_is_40wire		-	check drive side detection
45466bbfd53dSAlan Cox  *	@dev: device
45476bbfd53dSAlan Cox  *
45486bbfd53dSAlan Cox  *	Perform drive side detection decoding, allowing for device vendors
45496bbfd53dSAlan Cox  *	who can't follow the documentation.
45506bbfd53dSAlan Cox  */
45516bbfd53dSAlan Cox 
45526bbfd53dSAlan Cox static int ata_is_40wire(struct ata_device *dev)
45536bbfd53dSAlan Cox {
45546bbfd53dSAlan Cox 	if (dev->horkage & ATA_HORKAGE_IVB)
45556bbfd53dSAlan Cox 		return ata_drive_40wire_relaxed(dev->id);
45566bbfd53dSAlan Cox 	return ata_drive_40wire(dev->id);
45576bbfd53dSAlan Cox }
45586bbfd53dSAlan Cox 
45596bbfd53dSAlan Cox /**
456015a5551cSAlan Cox  *	cable_is_40wire		-	40/80/SATA decider
456115a5551cSAlan Cox  *	@ap: port to consider
456215a5551cSAlan Cox  *
456315a5551cSAlan Cox  *	This function encapsulates the policy for speed management
456415a5551cSAlan Cox  *	in one place. At the moment we don't cache the result but
456515a5551cSAlan Cox  *	there is a good case for setting ap->cbl to the result when
456615a5551cSAlan Cox  *	we are called with unknown cables (and figuring out if it
456715a5551cSAlan Cox  *	impacts hotplug at all).
456815a5551cSAlan Cox  *
456915a5551cSAlan Cox  *	Return 1 if the cable appears to be 40 wire.
457015a5551cSAlan Cox  */
457115a5551cSAlan Cox 
457215a5551cSAlan Cox static int cable_is_40wire(struct ata_port *ap)
457315a5551cSAlan Cox {
457415a5551cSAlan Cox 	struct ata_link *link;
457515a5551cSAlan Cox 	struct ata_device *dev;
457615a5551cSAlan Cox 
45774a9c7b33STejun Heo 	/* If the controller thinks we are 40 wire, we are. */
457815a5551cSAlan Cox 	if (ap->cbl == ATA_CBL_PATA40)
457915a5551cSAlan Cox 		return 1;
45804a9c7b33STejun Heo 
45814a9c7b33STejun Heo 	/* If the controller thinks we are 80 wire, we are. */
458215a5551cSAlan Cox 	if (ap->cbl == ATA_CBL_PATA80 || ap->cbl == ATA_CBL_SATA)
458315a5551cSAlan Cox 		return 0;
45844a9c7b33STejun Heo 
45854a9c7b33STejun Heo 	/* If the system is known to be 40 wire short cable (eg
45864a9c7b33STejun Heo 	 * laptop), then we allow 80 wire modes even if the drive
45874a9c7b33STejun Heo 	 * isn't sure.
45884a9c7b33STejun Heo 	 */
4589f792068eSAlan Cox 	if (ap->cbl == ATA_CBL_PATA40_SHORT)
4590f792068eSAlan Cox 		return 0;
459115a5551cSAlan Cox 
45924a9c7b33STejun Heo 	/* If the controller doesn't know, we scan.
45934a9c7b33STejun Heo 	 *
45944a9c7b33STejun Heo 	 * Note: We look for all 40 wire detects at this point.  Any
45954a9c7b33STejun Heo 	 *       80 wire detect is taken to be 80 wire cable because
45964a9c7b33STejun Heo 	 * - in many setups only the one drive (slave if present) will
45974a9c7b33STejun Heo 	 *   give a valid detect
45984a9c7b33STejun Heo 	 * - if you have a non detect capable drive you don't want it
45994a9c7b33STejun Heo 	 *   to colour the choice
460015a5551cSAlan Cox 	 */
46011eca4365STejun Heo 	ata_for_each_link(link, ap, EDGE) {
46021eca4365STejun Heo 		ata_for_each_dev(dev, link, ENABLED) {
46031eca4365STejun Heo 			if (!ata_is_40wire(dev))
460415a5551cSAlan Cox 				return 0;
460515a5551cSAlan Cox 		}
460615a5551cSAlan Cox 	}
460715a5551cSAlan Cox 	return 1;
460815a5551cSAlan Cox }
460915a5551cSAlan Cox 
461015a5551cSAlan Cox /**
4611c6fd2807SJeff Garzik  *	ata_dev_xfermask - Compute supported xfermask of the given device
4612c6fd2807SJeff Garzik  *	@dev: Device to compute xfermask for
4613c6fd2807SJeff Garzik  *
4614c6fd2807SJeff Garzik  *	Compute supported xfermask of @dev and store it in
4615c6fd2807SJeff Garzik  *	dev->*_mask.  This function is responsible for applying all
4616c6fd2807SJeff Garzik  *	known limits including host controller limits, device
4617c6fd2807SJeff Garzik  *	blacklist, etc...
4618c6fd2807SJeff Garzik  *
4619c6fd2807SJeff Garzik  *	LOCKING:
4620c6fd2807SJeff Garzik  *	None.
4621c6fd2807SJeff Garzik  */
4622c6fd2807SJeff Garzik static void ata_dev_xfermask(struct ata_device *dev)
4623c6fd2807SJeff Garzik {
46249af5c9c9STejun Heo 	struct ata_link *link = dev->link;
46259af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
4626cca3974eSJeff Garzik 	struct ata_host *host = ap->host;
4627c6fd2807SJeff Garzik 	unsigned long xfer_mask;
4628c6fd2807SJeff Garzik 
4629c6fd2807SJeff Garzik 	/* controller modes available */
4630c6fd2807SJeff Garzik 	xfer_mask = ata_pack_xfermask(ap->pio_mask,
4631c6fd2807SJeff Garzik 				      ap->mwdma_mask, ap->udma_mask);
4632c6fd2807SJeff Garzik 
46338343f889SRobert Hancock 	/* drive modes available */
4634c6fd2807SJeff Garzik 	xfer_mask &= ata_pack_xfermask(dev->pio_mask,
4635c6fd2807SJeff Garzik 				       dev->mwdma_mask, dev->udma_mask);
4636c6fd2807SJeff Garzik 	xfer_mask &= ata_id_xfermask(dev->id);
4637c6fd2807SJeff Garzik 
4638b352e57dSAlan Cox 	/*
4639b352e57dSAlan Cox 	 *	CFA Advanced TrueIDE timings are not allowed on a shared
4640b352e57dSAlan Cox 	 *	cable
4641b352e57dSAlan Cox 	 */
4642b352e57dSAlan Cox 	if (ata_dev_pair(dev)) {
4643b352e57dSAlan Cox 		/* No PIO5 or PIO6 */
4644b352e57dSAlan Cox 		xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
4645b352e57dSAlan Cox 		/* No MWDMA3 or MWDMA 4 */
4646b352e57dSAlan Cox 		xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
4647b352e57dSAlan Cox 	}
4648b352e57dSAlan Cox 
4649c6fd2807SJeff Garzik 	if (ata_dma_blacklisted(dev)) {
4650c6fd2807SJeff Garzik 		xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4651a9a79dfeSJoe Perches 		ata_dev_warn(dev,
4652c6fd2807SJeff Garzik 			     "device is on DMA blacklist, disabling DMA\n");
4653c6fd2807SJeff Garzik 	}
4654c6fd2807SJeff Garzik 
465514d66ab7SPetr Vandrovec 	if ((host->flags & ATA_HOST_SIMPLEX) &&
465614d66ab7SPetr Vandrovec 	    host->simplex_claimed && host->simplex_claimed != ap) {
4657c6fd2807SJeff Garzik 		xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4658a9a79dfeSJoe Perches 		ata_dev_warn(dev,
4659a9a79dfeSJoe Perches 			     "simplex DMA is claimed by other device, disabling DMA\n");
4660c6fd2807SJeff Garzik 	}
4661c6fd2807SJeff Garzik 
4662e424675fSJeff Garzik 	if (ap->flags & ATA_FLAG_NO_IORDY)
4663e424675fSJeff Garzik 		xfer_mask &= ata_pio_mask_no_iordy(dev);
4664e424675fSJeff Garzik 
4665c6fd2807SJeff Garzik 	if (ap->ops->mode_filter)
4666a76b62caSAlan Cox 		xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
4667c6fd2807SJeff Garzik 
46688343f889SRobert Hancock 	/* Apply cable rule here.  Don't apply it early because when
46698343f889SRobert Hancock 	 * we handle hot plug the cable type can itself change.
46708343f889SRobert Hancock 	 * Check this last so that we know if the transfer rate was
46718343f889SRobert Hancock 	 * solely limited by the cable.
46728343f889SRobert Hancock 	 * Unknown or 80 wire cables reported host side are checked
46738343f889SRobert Hancock 	 * drive side as well. Cases where we know a 40wire cable
46748343f889SRobert Hancock 	 * is used safely for 80 are not checked here.
46758343f889SRobert Hancock 	 */
46768343f889SRobert Hancock 	if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
46778343f889SRobert Hancock 		/* UDMA/44 or higher would be available */
467815a5551cSAlan Cox 		if (cable_is_40wire(ap)) {
4679a9a79dfeSJoe Perches 			ata_dev_warn(dev,
46808343f889SRobert Hancock 				     "limited to UDMA/33 due to 40-wire cable\n");
46818343f889SRobert Hancock 			xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
46828343f889SRobert Hancock 		}
46838343f889SRobert Hancock 
4684c6fd2807SJeff Garzik 	ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
4685c6fd2807SJeff Garzik 			    &dev->mwdma_mask, &dev->udma_mask);
4686c6fd2807SJeff Garzik }
4687c6fd2807SJeff Garzik 
4688c6fd2807SJeff Garzik /**
4689c6fd2807SJeff Garzik  *	ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
4690c6fd2807SJeff Garzik  *	@dev: Device to which command will be sent
4691c6fd2807SJeff Garzik  *
4692c6fd2807SJeff Garzik  *	Issue SET FEATURES - XFER MODE command to device @dev
4693c6fd2807SJeff Garzik  *	on port @ap.
4694c6fd2807SJeff Garzik  *
4695c6fd2807SJeff Garzik  *	LOCKING:
4696c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
4697c6fd2807SJeff Garzik  *
4698c6fd2807SJeff Garzik  *	RETURNS:
4699c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
4700c6fd2807SJeff Garzik  */
4701c6fd2807SJeff Garzik 
4702c6fd2807SJeff Garzik static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
4703c6fd2807SJeff Garzik {
4704c6fd2807SJeff Garzik 	struct ata_taskfile tf;
4705c6fd2807SJeff Garzik 	unsigned int err_mask;
4706c6fd2807SJeff Garzik 
4707c6fd2807SJeff Garzik 	/* set up set-features taskfile */
4708c6fd2807SJeff Garzik 	DPRINTK("set features - xfer mode\n");
4709c6fd2807SJeff Garzik 
4710464cf177STejun Heo 	/* Some controllers and ATAPI devices show flaky interrupt
4711464cf177STejun Heo 	 * behavior after setting xfer mode.  Use polling instead.
4712464cf177STejun Heo 	 */
4713c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
4714c6fd2807SJeff Garzik 	tf.command = ATA_CMD_SET_FEATURES;
4715c6fd2807SJeff Garzik 	tf.feature = SETFEATURES_XFER;
4716464cf177STejun Heo 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
4717c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
4718b9f8ab2dSAlan Cox 	/* If we are using IORDY we must send the mode setting command */
4719b9f8ab2dSAlan Cox 	if (ata_pio_need_iordy(dev))
4720c6fd2807SJeff Garzik 		tf.nsect = dev->xfer_mode;
4721b9f8ab2dSAlan Cox 	/* If the device has IORDY and the controller does not - turn it off */
4722b9f8ab2dSAlan Cox  	else if (ata_id_has_iordy(dev->id))
4723b9f8ab2dSAlan Cox 		tf.nsect = 0x01;
4724b9f8ab2dSAlan Cox 	else /* In the ancient relic department - skip all of this */
4725b9f8ab2dSAlan Cox 		return 0;
4726c6fd2807SJeff Garzik 
4727d531be2cSMikulas Patocka 	/* On some disks, this command causes spin-up, so we need longer timeout */
4728d531be2cSMikulas Patocka 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 15000);
4729c6fd2807SJeff Garzik 
4730c6fd2807SJeff Garzik 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
4731c6fd2807SJeff Garzik 	return err_mask;
4732c6fd2807SJeff Garzik }
47331152b261STejun Heo 
4734c6fd2807SJeff Garzik /**
4735218f3d30SJeff Garzik  *	ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES
47369f45cbd3SKristen Carlson Accardi  *	@dev: Device to which command will be sent
47379f45cbd3SKristen Carlson Accardi  *	@enable: Whether to enable or disable the feature
4738218f3d30SJeff Garzik  *	@feature: The sector count represents the feature to set
47399f45cbd3SKristen Carlson Accardi  *
47409f45cbd3SKristen Carlson Accardi  *	Issue SET FEATURES - SATA FEATURES command to device @dev
4741218f3d30SJeff Garzik  *	on port @ap with sector count
47429f45cbd3SKristen Carlson Accardi  *
47439f45cbd3SKristen Carlson Accardi  *	LOCKING:
47449f45cbd3SKristen Carlson Accardi  *	PCI/etc. bus probe sem.
47459f45cbd3SKristen Carlson Accardi  *
47469f45cbd3SKristen Carlson Accardi  *	RETURNS:
47479f45cbd3SKristen Carlson Accardi  *	0 on success, AC_ERR_* mask otherwise.
47489f45cbd3SKristen Carlson Accardi  */
47491152b261STejun Heo unsigned int ata_dev_set_feature(struct ata_device *dev, u8 enable, u8 feature)
47509f45cbd3SKristen Carlson Accardi {
47519f45cbd3SKristen Carlson Accardi 	struct ata_taskfile tf;
47529f45cbd3SKristen Carlson Accardi 	unsigned int err_mask;
4753974e0a45SDamien Le Moal 	unsigned long timeout = 0;
47549f45cbd3SKristen Carlson Accardi 
47559f45cbd3SKristen Carlson Accardi 	/* set up set-features taskfile */
47569f45cbd3SKristen Carlson Accardi 	DPRINTK("set features - SATA features\n");
47579f45cbd3SKristen Carlson Accardi 
47589f45cbd3SKristen Carlson Accardi 	ata_tf_init(dev, &tf);
47599f45cbd3SKristen Carlson Accardi 	tf.command = ATA_CMD_SET_FEATURES;
47609f45cbd3SKristen Carlson Accardi 	tf.feature = enable;
47619f45cbd3SKristen Carlson Accardi 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
47629f45cbd3SKristen Carlson Accardi 	tf.protocol = ATA_PROT_NODATA;
4763218f3d30SJeff Garzik 	tf.nsect = feature;
47649f45cbd3SKristen Carlson Accardi 
4765974e0a45SDamien Le Moal 	if (enable == SETFEATURES_SPINUP)
4766974e0a45SDamien Le Moal 		timeout = ata_probe_timeout ?
4767974e0a45SDamien Le Moal 			  ata_probe_timeout * 1000 : SETFEATURES_SPINUP_TIMEOUT;
4768974e0a45SDamien Le Moal 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, timeout);
47699f45cbd3SKristen Carlson Accardi 
47709f45cbd3SKristen Carlson Accardi 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
47719f45cbd3SKristen Carlson Accardi 	return err_mask;
47729f45cbd3SKristen Carlson Accardi }
4773633de4ccSJeff Garzik EXPORT_SYMBOL_GPL(ata_dev_set_feature);
47749f45cbd3SKristen Carlson Accardi 
47759f45cbd3SKristen Carlson Accardi /**
4776c6fd2807SJeff Garzik  *	ata_dev_init_params - Issue INIT DEV PARAMS command
4777c6fd2807SJeff Garzik  *	@dev: Device to which command will be sent
4778c6fd2807SJeff Garzik  *	@heads: Number of heads (taskfile parameter)
4779c6fd2807SJeff Garzik  *	@sectors: Number of sectors (taskfile parameter)
4780c6fd2807SJeff Garzik  *
4781c6fd2807SJeff Garzik  *	LOCKING:
4782c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
4783c6fd2807SJeff Garzik  *
4784c6fd2807SJeff Garzik  *	RETURNS:
4785c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
4786c6fd2807SJeff Garzik  */
4787c6fd2807SJeff Garzik static unsigned int ata_dev_init_params(struct ata_device *dev,
4788c6fd2807SJeff Garzik 					u16 heads, u16 sectors)
4789c6fd2807SJeff Garzik {
4790c6fd2807SJeff Garzik 	struct ata_taskfile tf;
4791c6fd2807SJeff Garzik 	unsigned int err_mask;
4792c6fd2807SJeff Garzik 
4793c6fd2807SJeff Garzik 	/* Number of sectors per track 1-255. Number of heads 1-16 */
4794c6fd2807SJeff Garzik 	if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
4795c6fd2807SJeff Garzik 		return AC_ERR_INVALID;
4796c6fd2807SJeff Garzik 
4797c6fd2807SJeff Garzik 	/* set up init dev params taskfile */
4798c6fd2807SJeff Garzik 	DPRINTK("init dev params \n");
4799c6fd2807SJeff Garzik 
4800c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
4801c6fd2807SJeff Garzik 	tf.command = ATA_CMD_INIT_DEV_PARAMS;
4802c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4803c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
4804c6fd2807SJeff Garzik 	tf.nsect = sectors;
4805c6fd2807SJeff Garzik 	tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
4806c6fd2807SJeff Garzik 
48072b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
480818b2466cSAlan Cox 	/* A clean abort indicates an original or just out of spec drive
480918b2466cSAlan Cox 	   and we should continue as we issue the setup based on the
481018b2466cSAlan Cox 	   drive reported working geometry */
481118b2466cSAlan Cox 	if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
481218b2466cSAlan Cox 		err_mask = 0;
4813c6fd2807SJeff Garzik 
4814c6fd2807SJeff Garzik 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
4815c6fd2807SJeff Garzik 	return err_mask;
4816c6fd2807SJeff Garzik }
4817c6fd2807SJeff Garzik 
4818c6fd2807SJeff Garzik /**
48195895ef9aSTejun Heo  *	atapi_check_dma - Check whether ATAPI DMA can be supported
4820c6fd2807SJeff Garzik  *	@qc: Metadata associated with taskfile to check
4821c6fd2807SJeff Garzik  *
4822c6fd2807SJeff Garzik  *	Allow low-level driver to filter ATA PACKET commands, returning
4823c6fd2807SJeff Garzik  *	a status indicating whether or not it is OK to use DMA for the
4824c6fd2807SJeff Garzik  *	supplied PACKET command.
4825c6fd2807SJeff Garzik  *
4826c6fd2807SJeff Garzik  *	LOCKING:
4827cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4828c6fd2807SJeff Garzik  *
4829c6fd2807SJeff Garzik  *	RETURNS: 0 when ATAPI DMA can be used
4830c6fd2807SJeff Garzik  *               nonzero otherwise
4831c6fd2807SJeff Garzik  */
48325895ef9aSTejun Heo int atapi_check_dma(struct ata_queued_cmd *qc)
4833c6fd2807SJeff Garzik {
4834c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4835c6fd2807SJeff Garzik 
4836b9a4197eSTejun Heo 	/* Don't allow DMA if it isn't multiple of 16 bytes.  Quite a
4837b9a4197eSTejun Heo 	 * few ATAPI devices choke on such DMA requests.
4838b9a4197eSTejun Heo 	 */
48396a87e42eSTejun Heo 	if (!(qc->dev->horkage & ATA_HORKAGE_ATAPI_MOD16_DMA) &&
48406a87e42eSTejun Heo 	    unlikely(qc->nbytes & 15))
48416f23a31dSAlbert Lee 		return 1;
48426f23a31dSAlbert Lee 
4843c6fd2807SJeff Garzik 	if (ap->ops->check_atapi_dma)
4844b9a4197eSTejun Heo 		return ap->ops->check_atapi_dma(qc);
4845c6fd2807SJeff Garzik 
4846b9a4197eSTejun Heo 	return 0;
4847c6fd2807SJeff Garzik }
4848b9a4197eSTejun Heo 
4849c6fd2807SJeff Garzik /**
485031cc23b3STejun Heo  *	ata_std_qc_defer - Check whether a qc needs to be deferred
485131cc23b3STejun Heo  *	@qc: ATA command in question
485231cc23b3STejun Heo  *
485331cc23b3STejun Heo  *	Non-NCQ commands cannot run with any other command, NCQ or
485431cc23b3STejun Heo  *	not.  As upper layer only knows the queue depth, we are
485531cc23b3STejun Heo  *	responsible for maintaining exclusion.  This function checks
485631cc23b3STejun Heo  *	whether a new command @qc can be issued.
485731cc23b3STejun Heo  *
485831cc23b3STejun Heo  *	LOCKING:
485931cc23b3STejun Heo  *	spin_lock_irqsave(host lock)
486031cc23b3STejun Heo  *
486131cc23b3STejun Heo  *	RETURNS:
486231cc23b3STejun Heo  *	ATA_DEFER_* if deferring is needed, 0 otherwise.
486331cc23b3STejun Heo  */
486431cc23b3STejun Heo int ata_std_qc_defer(struct ata_queued_cmd *qc)
486531cc23b3STejun Heo {
486631cc23b3STejun Heo 	struct ata_link *link = qc->dev->link;
486731cc23b3STejun Heo 
4868179b310aSHannes Reinecke 	if (ata_is_ncq(qc->tf.protocol)) {
486931cc23b3STejun Heo 		if (!ata_tag_valid(link->active_tag))
487031cc23b3STejun Heo 			return 0;
487131cc23b3STejun Heo 	} else {
487231cc23b3STejun Heo 		if (!ata_tag_valid(link->active_tag) && !link->sactive)
487331cc23b3STejun Heo 			return 0;
487431cc23b3STejun Heo 	}
487531cc23b3STejun Heo 
487631cc23b3STejun Heo 	return ATA_DEFER_LINK;
487731cc23b3STejun Heo }
487831cc23b3STejun Heo 
4879c6fd2807SJeff Garzik void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
4880c6fd2807SJeff Garzik 
4881c6fd2807SJeff Garzik /**
4882c6fd2807SJeff Garzik  *	ata_sg_init - Associate command with scatter-gather table.
4883c6fd2807SJeff Garzik  *	@qc: Command to be associated
4884c6fd2807SJeff Garzik  *	@sg: Scatter-gather table.
4885c6fd2807SJeff Garzik  *	@n_elem: Number of elements in s/g table.
4886c6fd2807SJeff Garzik  *
4887c6fd2807SJeff Garzik  *	Initialize the data-related elements of queued_cmd @qc
4888c6fd2807SJeff Garzik  *	to point to a scatter-gather table @sg, containing @n_elem
4889c6fd2807SJeff Garzik  *	elements.
4890c6fd2807SJeff Garzik  *
4891c6fd2807SJeff Garzik  *	LOCKING:
4892cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4893c6fd2807SJeff Garzik  */
4894c6fd2807SJeff Garzik void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
4895c6fd2807SJeff Garzik 		 unsigned int n_elem)
4896c6fd2807SJeff Garzik {
4897ff2aeb1eSTejun Heo 	qc->sg = sg;
4898c6fd2807SJeff Garzik 	qc->n_elem = n_elem;
4899ff2aeb1eSTejun Heo 	qc->cursg = qc->sg;
4900ff2aeb1eSTejun Heo }
4901ff2aeb1eSTejun Heo 
49022874d5eeSGeert Uytterhoeven #ifdef CONFIG_HAS_DMA
49032874d5eeSGeert Uytterhoeven 
49042874d5eeSGeert Uytterhoeven /**
49052874d5eeSGeert Uytterhoeven  *	ata_sg_clean - Unmap DMA memory associated with command
49062874d5eeSGeert Uytterhoeven  *	@qc: Command containing DMA memory to be released
49072874d5eeSGeert Uytterhoeven  *
49082874d5eeSGeert Uytterhoeven  *	Unmap all mapped DMA memory associated with this command.
49092874d5eeSGeert Uytterhoeven  *
49102874d5eeSGeert Uytterhoeven  *	LOCKING:
49112874d5eeSGeert Uytterhoeven  *	spin_lock_irqsave(host lock)
49122874d5eeSGeert Uytterhoeven  */
4913af27e01cSJason Yan static void ata_sg_clean(struct ata_queued_cmd *qc)
49142874d5eeSGeert Uytterhoeven {
49152874d5eeSGeert Uytterhoeven 	struct ata_port *ap = qc->ap;
49162874d5eeSGeert Uytterhoeven 	struct scatterlist *sg = qc->sg;
49172874d5eeSGeert Uytterhoeven 	int dir = qc->dma_dir;
49182874d5eeSGeert Uytterhoeven 
49192874d5eeSGeert Uytterhoeven 	WARN_ON_ONCE(sg == NULL);
49202874d5eeSGeert Uytterhoeven 
49212874d5eeSGeert Uytterhoeven 	VPRINTK("unmapping %u sg elements\n", qc->n_elem);
49222874d5eeSGeert Uytterhoeven 
49232874d5eeSGeert Uytterhoeven 	if (qc->n_elem)
49242874d5eeSGeert Uytterhoeven 		dma_unmap_sg(ap->dev, sg, qc->orig_n_elem, dir);
49252874d5eeSGeert Uytterhoeven 
49262874d5eeSGeert Uytterhoeven 	qc->flags &= ~ATA_QCFLAG_DMAMAP;
49272874d5eeSGeert Uytterhoeven 	qc->sg = NULL;
49282874d5eeSGeert Uytterhoeven }
49292874d5eeSGeert Uytterhoeven 
4930c6fd2807SJeff Garzik /**
4931c6fd2807SJeff Garzik  *	ata_sg_setup - DMA-map the scatter-gather table associated with a command.
4932c6fd2807SJeff Garzik  *	@qc: Command with scatter-gather table to be mapped.
4933c6fd2807SJeff Garzik  *
4934c6fd2807SJeff Garzik  *	DMA-map the scatter-gather table associated with queued_cmd @qc.
4935c6fd2807SJeff Garzik  *
4936c6fd2807SJeff Garzik  *	LOCKING:
4937cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4938c6fd2807SJeff Garzik  *
4939c6fd2807SJeff Garzik  *	RETURNS:
4940c6fd2807SJeff Garzik  *	Zero on success, negative on error.
4941c6fd2807SJeff Garzik  *
4942c6fd2807SJeff Garzik  */
4943c6fd2807SJeff Garzik static int ata_sg_setup(struct ata_queued_cmd *qc)
4944c6fd2807SJeff Garzik {
4945c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4946dde20207SJames Bottomley 	unsigned int n_elem;
4947c6fd2807SJeff Garzik 
494844877b4eSTejun Heo 	VPRINTK("ENTER, ata%u\n", ap->print_id);
4949c6fd2807SJeff Garzik 
4950dde20207SJames Bottomley 	n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir);
4951dde20207SJames Bottomley 	if (n_elem < 1)
4952c6fd2807SJeff Garzik 		return -1;
4953c6fd2807SJeff Garzik 
4954dde20207SJames Bottomley 	DPRINTK("%d sg elements mapped\n", n_elem);
49555825627cSFUJITA Tomonori 	qc->orig_n_elem = qc->n_elem;
4956dde20207SJames Bottomley 	qc->n_elem = n_elem;
4957f92a2636STejun Heo 	qc->flags |= ATA_QCFLAG_DMAMAP;
4958c6fd2807SJeff Garzik 
4959c6fd2807SJeff Garzik 	return 0;
4960c6fd2807SJeff Garzik }
4961c6fd2807SJeff Garzik 
49622874d5eeSGeert Uytterhoeven #else /* !CONFIG_HAS_DMA */
49632874d5eeSGeert Uytterhoeven 
49642874d5eeSGeert Uytterhoeven static inline void ata_sg_clean(struct ata_queued_cmd *qc) {}
49652874d5eeSGeert Uytterhoeven static inline int ata_sg_setup(struct ata_queued_cmd *qc) { return -1; }
49662874d5eeSGeert Uytterhoeven 
49672874d5eeSGeert Uytterhoeven #endif /* !CONFIG_HAS_DMA */
49682874d5eeSGeert Uytterhoeven 
4969c6fd2807SJeff Garzik /**
4970c6fd2807SJeff Garzik  *	swap_buf_le16 - swap halves of 16-bit words in place
4971c6fd2807SJeff Garzik  *	@buf:  Buffer to swap
4972c6fd2807SJeff Garzik  *	@buf_words:  Number of 16-bit words in buffer.
4973c6fd2807SJeff Garzik  *
4974c6fd2807SJeff Garzik  *	Swap halves of 16-bit words if needed to convert from
4975c6fd2807SJeff Garzik  *	little-endian byte order to native cpu byte order, or
4976c6fd2807SJeff Garzik  *	vice-versa.
4977c6fd2807SJeff Garzik  *
4978c6fd2807SJeff Garzik  *	LOCKING:
4979c6fd2807SJeff Garzik  *	Inherited from caller.
4980c6fd2807SJeff Garzik  */
4981c6fd2807SJeff Garzik void swap_buf_le16(u16 *buf, unsigned int buf_words)
4982c6fd2807SJeff Garzik {
4983c6fd2807SJeff Garzik #ifdef __BIG_ENDIAN
4984c6fd2807SJeff Garzik 	unsigned int i;
4985c6fd2807SJeff Garzik 
4986c6fd2807SJeff Garzik 	for (i = 0; i < buf_words; i++)
4987c6fd2807SJeff Garzik 		buf[i] = le16_to_cpu(buf[i]);
4988c6fd2807SJeff Garzik #endif /* __BIG_ENDIAN */
4989c6fd2807SJeff Garzik }
4990c6fd2807SJeff Garzik 
4991c6fd2807SJeff Garzik /**
4992c6fd2807SJeff Garzik  *	ata_qc_new_init - Request an available ATA command, and initialize it
4993c6fd2807SJeff Garzik  *	@dev: Device from whom we request an available command structure
499438755e89SRandy Dunlap  *	@tag: tag
4995c6fd2807SJeff Garzik  *
4996c6fd2807SJeff Garzik  *	LOCKING:
4997c6fd2807SJeff Garzik  *	None.
4998c6fd2807SJeff Garzik  */
4999c6fd2807SJeff Garzik 
500098bd4be1SShaohua Li struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev, int tag)
5001c6fd2807SJeff Garzik {
50029af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
5003c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
5004c6fd2807SJeff Garzik 
500598bd4be1SShaohua Li 	/* no command while frozen */
500698bd4be1SShaohua Li 	if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
500798bd4be1SShaohua Li 		return NULL;
500898bd4be1SShaohua Li 
500998bd4be1SShaohua Li 	/* libsas case */
50105067c046SShaohua Li 	if (ap->flags & ATA_FLAG_SAS_HOST) {
501198bd4be1SShaohua Li 		tag = ata_sas_allocate_tag(ap);
501298bd4be1SShaohua Li 		if (tag < 0)
501398bd4be1SShaohua Li 			return NULL;
501498bd4be1SShaohua Li 	}
501598bd4be1SShaohua Li 
501698bd4be1SShaohua Li 	qc = __ata_qc_from_tag(ap, tag);
501798bd4be1SShaohua Li 	qc->tag = tag;
5018c6fd2807SJeff Garzik 	qc->scsicmd = NULL;
5019c6fd2807SJeff Garzik 	qc->ap = ap;
5020c6fd2807SJeff Garzik 	qc->dev = dev;
5021c6fd2807SJeff Garzik 
5022c6fd2807SJeff Garzik 	ata_qc_reinit(qc);
5023c6fd2807SJeff Garzik 
5024c6fd2807SJeff Garzik 	return qc;
5025c6fd2807SJeff Garzik }
5026c6fd2807SJeff Garzik 
50278a8bc223STejun Heo /**
50288a8bc223STejun Heo  *	ata_qc_free - free unused ata_queued_cmd
50298a8bc223STejun Heo  *	@qc: Command to complete
50308a8bc223STejun Heo  *
50318a8bc223STejun Heo  *	Designed to free unused ata_queued_cmd object
50328a8bc223STejun Heo  *	in case something prevents using it.
50338a8bc223STejun Heo  *
50348a8bc223STejun Heo  *	LOCKING:
50358a8bc223STejun Heo  *	spin_lock_irqsave(host lock)
50368a8bc223STejun Heo  */
50378a8bc223STejun Heo void ata_qc_free(struct ata_queued_cmd *qc)
50388a8bc223STejun Heo {
5039a1104016SJulia Lawall 	struct ata_port *ap;
50408a8bc223STejun Heo 	unsigned int tag;
50418a8bc223STejun Heo 
5042efcb3cf7STejun Heo 	WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
5043a1104016SJulia Lawall 	ap = qc->ap;
50448a8bc223STejun Heo 
50458a8bc223STejun Heo 	qc->flags = 0;
50468a8bc223STejun Heo 	tag = qc->tag;
50478a8bc223STejun Heo 	if (likely(ata_tag_valid(tag))) {
50488a8bc223STejun Heo 		qc->tag = ATA_TAG_POISON;
50495067c046SShaohua Li 		if (ap->flags & ATA_FLAG_SAS_HOST)
505098bd4be1SShaohua Li 			ata_sas_free_tag(tag, ap);
50518a8bc223STejun Heo 	}
50528a8bc223STejun Heo }
50538a8bc223STejun Heo 
5054c6fd2807SJeff Garzik void __ata_qc_complete(struct ata_queued_cmd *qc)
5055c6fd2807SJeff Garzik {
5056a1104016SJulia Lawall 	struct ata_port *ap;
5057a1104016SJulia Lawall 	struct ata_link *link;
5058c6fd2807SJeff Garzik 
5059efcb3cf7STejun Heo 	WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
5060efcb3cf7STejun Heo 	WARN_ON_ONCE(!(qc->flags & ATA_QCFLAG_ACTIVE));
5061a1104016SJulia Lawall 	ap = qc->ap;
5062a1104016SJulia Lawall 	link = qc->dev->link;
5063c6fd2807SJeff Garzik 
5064c6fd2807SJeff Garzik 	if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
5065c6fd2807SJeff Garzik 		ata_sg_clean(qc);
5066c6fd2807SJeff Garzik 
5067c6fd2807SJeff Garzik 	/* command should be marked inactive atomically with qc completion */
5068179b310aSHannes Reinecke 	if (ata_is_ncq(qc->tf.protocol)) {
50699af5c9c9STejun Heo 		link->sactive &= ~(1 << qc->tag);
5070da917d69STejun Heo 		if (!link->sactive)
5071da917d69STejun Heo 			ap->nr_active_links--;
5072da917d69STejun Heo 	} else {
50739af5c9c9STejun Heo 		link->active_tag = ATA_TAG_POISON;
5074da917d69STejun Heo 		ap->nr_active_links--;
5075da917d69STejun Heo 	}
5076da917d69STejun Heo 
5077da917d69STejun Heo 	/* clear exclusive status */
5078da917d69STejun Heo 	if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
5079da917d69STejun Heo 		     ap->excl_link == link))
5080da917d69STejun Heo 		ap->excl_link = NULL;
5081c6fd2807SJeff Garzik 
5082c6fd2807SJeff Garzik 	/* atapi: mark qc as inactive to prevent the interrupt handler
5083c6fd2807SJeff Garzik 	 * from completing the command twice later, before the error handler
5084c6fd2807SJeff Garzik 	 * is called. (when rc != 0 and atapi request sense is needed)
5085c6fd2807SJeff Garzik 	 */
5086c6fd2807SJeff Garzik 	qc->flags &= ~ATA_QCFLAG_ACTIVE;
5087c6fd2807SJeff Garzik 	ap->qc_active &= ~(1 << qc->tag);
5088c6fd2807SJeff Garzik 
5089c6fd2807SJeff Garzik 	/* call completion callback */
5090c6fd2807SJeff Garzik 	qc->complete_fn(qc);
5091c6fd2807SJeff Garzik }
5092c6fd2807SJeff Garzik 
509339599a53STejun Heo static void fill_result_tf(struct ata_queued_cmd *qc)
509439599a53STejun Heo {
509539599a53STejun Heo 	struct ata_port *ap = qc->ap;
509639599a53STejun Heo 
509739599a53STejun Heo 	qc->result_tf.flags = qc->tf.flags;
509822183bf5STejun Heo 	ap->ops->qc_fill_rtf(qc);
509939599a53STejun Heo }
510039599a53STejun Heo 
510100115e0fSTejun Heo static void ata_verify_xfer(struct ata_queued_cmd *qc)
510200115e0fSTejun Heo {
510300115e0fSTejun Heo 	struct ata_device *dev = qc->dev;
510400115e0fSTejun Heo 
5105eb0effdfSChristoph Hellwig 	if (!ata_is_data(qc->tf.protocol))
510600115e0fSTejun Heo 		return;
510700115e0fSTejun Heo 
510800115e0fSTejun Heo 	if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
510900115e0fSTejun Heo 		return;
511000115e0fSTejun Heo 
511100115e0fSTejun Heo 	dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
511200115e0fSTejun Heo }
511300115e0fSTejun Heo 
5114c6fd2807SJeff Garzik /**
5115c6fd2807SJeff Garzik  *	ata_qc_complete - Complete an active ATA command
5116c6fd2807SJeff Garzik  *	@qc: Command to complete
5117c6fd2807SJeff Garzik  *
51181aadf5c3STejun Heo  *	Indicate to the mid and upper layers that an ATA command has
51191aadf5c3STejun Heo  *	completed, with either an ok or not-ok status.
51201aadf5c3STejun Heo  *
51211aadf5c3STejun Heo  *	Refrain from calling this function multiple times when
51221aadf5c3STejun Heo  *	successfully completing multiple NCQ commands.
51231aadf5c3STejun Heo  *	ata_qc_complete_multiple() should be used instead, which will
51241aadf5c3STejun Heo  *	properly update IRQ expect state.
5125c6fd2807SJeff Garzik  *
5126c6fd2807SJeff Garzik  *	LOCKING:
5127cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
5128c6fd2807SJeff Garzik  */
5129c6fd2807SJeff Garzik void ata_qc_complete(struct ata_queued_cmd *qc)
5130c6fd2807SJeff Garzik {
5131c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
5132c6fd2807SJeff Garzik 
5133eb25cb99SStephan Linz 	/* Trigger the LED (if available) */
5134eb25cb99SStephan Linz 	ledtrig_disk_activity();
5135eb25cb99SStephan Linz 
5136c6fd2807SJeff Garzik 	/* XXX: New EH and old EH use different mechanisms to
5137c6fd2807SJeff Garzik 	 * synchronize EH with regular execution path.
5138c6fd2807SJeff Garzik 	 *
5139c6fd2807SJeff Garzik 	 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
5140c6fd2807SJeff Garzik 	 * Normal execution path is responsible for not accessing a
5141c6fd2807SJeff Garzik 	 * failed qc.  libata core enforces the rule by returning NULL
5142c6fd2807SJeff Garzik 	 * from ata_qc_from_tag() for failed qcs.
5143c6fd2807SJeff Garzik 	 *
5144c6fd2807SJeff Garzik 	 * Old EH depends on ata_qc_complete() nullifying completion
5145c6fd2807SJeff Garzik 	 * requests if ATA_QCFLAG_EH_SCHEDULED is set.  Old EH does
5146c6fd2807SJeff Garzik 	 * not synchronize with interrupt handler.  Only PIO task is
5147c6fd2807SJeff Garzik 	 * taken care of.
5148c6fd2807SJeff Garzik 	 */
5149c6fd2807SJeff Garzik 	if (ap->ops->error_handler) {
51504dbfa39bSTejun Heo 		struct ata_device *dev = qc->dev;
51514dbfa39bSTejun Heo 		struct ata_eh_info *ehi = &dev->link->eh_info;
51524dbfa39bSTejun Heo 
5153c6fd2807SJeff Garzik 		if (unlikely(qc->err_mask))
5154c6fd2807SJeff Garzik 			qc->flags |= ATA_QCFLAG_FAILED;
5155c6fd2807SJeff Garzik 
5156f08dc1acSTejun Heo 		/*
5157f08dc1acSTejun Heo 		 * Finish internal commands without any further processing
5158f08dc1acSTejun Heo 		 * and always with the result TF filled.
5159f08dc1acSTejun Heo 		 */
5160f08dc1acSTejun Heo 		if (unlikely(ata_tag_internal(qc->tag))) {
516139599a53STejun Heo 			fill_result_tf(qc);
5162255c03d1SHannes Reinecke 			trace_ata_qc_complete_internal(qc);
5163f4b31db9STejun Heo 			__ata_qc_complete(qc);
5164c6fd2807SJeff Garzik 			return;
5165c6fd2807SJeff Garzik 		}
5166c6fd2807SJeff Garzik 
5167f08dc1acSTejun Heo 		/*
5168f08dc1acSTejun Heo 		 * Non-internal qc has failed.  Fill the result TF and
5169f08dc1acSTejun Heo 		 * summon EH.
5170f08dc1acSTejun Heo 		 */
5171f08dc1acSTejun Heo 		if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
5172f08dc1acSTejun Heo 			fill_result_tf(qc);
5173255c03d1SHannes Reinecke 			trace_ata_qc_complete_failed(qc);
5174f08dc1acSTejun Heo 			ata_qc_schedule_eh(qc);
5175f08dc1acSTejun Heo 			return;
5176f08dc1acSTejun Heo 		}
5177f08dc1acSTejun Heo 
51784dc738edSTejun Heo 		WARN_ON_ONCE(ap->pflags & ATA_PFLAG_FROZEN);
51794dc738edSTejun Heo 
5180c6fd2807SJeff Garzik 		/* read result TF if requested */
5181c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_RESULT_TF)
518239599a53STejun Heo 			fill_result_tf(qc);
5183c6fd2807SJeff Garzik 
5184255c03d1SHannes Reinecke 		trace_ata_qc_complete_done(qc);
51854dbfa39bSTejun Heo 		/* Some commands need post-processing after successful
51864dbfa39bSTejun Heo 		 * completion.
51874dbfa39bSTejun Heo 		 */
51884dbfa39bSTejun Heo 		switch (qc->tf.command) {
51894dbfa39bSTejun Heo 		case ATA_CMD_SET_FEATURES:
51904dbfa39bSTejun Heo 			if (qc->tf.feature != SETFEATURES_WC_ON &&
51910c12735eSTom Yan 			    qc->tf.feature != SETFEATURES_WC_OFF &&
51920c12735eSTom Yan 			    qc->tf.feature != SETFEATURES_RA_ON &&
51930c12735eSTom Yan 			    qc->tf.feature != SETFEATURES_RA_OFF)
51944dbfa39bSTejun Heo 				break;
51954dbfa39bSTejun Heo 			/* fall through */
51964dbfa39bSTejun Heo 		case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
51974dbfa39bSTejun Heo 		case ATA_CMD_SET_MULTI: /* multi_count changed */
51984dbfa39bSTejun Heo 			/* revalidate device */
51994dbfa39bSTejun Heo 			ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE;
52004dbfa39bSTejun Heo 			ata_port_schedule_eh(ap);
52014dbfa39bSTejun Heo 			break;
5202054a5fbaSTejun Heo 
5203054a5fbaSTejun Heo 		case ATA_CMD_SLEEP:
5204054a5fbaSTejun Heo 			dev->flags |= ATA_DFLAG_SLEEPING;
5205054a5fbaSTejun Heo 			break;
52064dbfa39bSTejun Heo 		}
52074dbfa39bSTejun Heo 
520800115e0fSTejun Heo 		if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
520900115e0fSTejun Heo 			ata_verify_xfer(qc);
521000115e0fSTejun Heo 
5211c6fd2807SJeff Garzik 		__ata_qc_complete(qc);
5212c6fd2807SJeff Garzik 	} else {
5213c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
5214c6fd2807SJeff Garzik 			return;
5215c6fd2807SJeff Garzik 
5216c6fd2807SJeff Garzik 		/* read result TF if failed or requested */
5217c6fd2807SJeff Garzik 		if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
521839599a53STejun Heo 			fill_result_tf(qc);
5219c6fd2807SJeff Garzik 
5220c6fd2807SJeff Garzik 		__ata_qc_complete(qc);
5221c6fd2807SJeff Garzik 	}
5222c6fd2807SJeff Garzik }
5223c6fd2807SJeff Garzik 
5224c6fd2807SJeff Garzik /**
5225c6fd2807SJeff Garzik  *	ata_qc_complete_multiple - Complete multiple qcs successfully
5226c6fd2807SJeff Garzik  *	@ap: port in question
5227c6fd2807SJeff Garzik  *	@qc_active: new qc_active mask
5228c6fd2807SJeff Garzik  *
5229c6fd2807SJeff Garzik  *	Complete in-flight commands.  This functions is meant to be
5230c6fd2807SJeff Garzik  *	called from low-level driver's interrupt routine to complete
5231c6fd2807SJeff Garzik  *	requests normally.  ap->qc_active and @qc_active is compared
5232c6fd2807SJeff Garzik  *	and commands are completed accordingly.
5233c6fd2807SJeff Garzik  *
52341aadf5c3STejun Heo  *	Always use this function when completing multiple NCQ commands
52351aadf5c3STejun Heo  *	from IRQ handlers instead of calling ata_qc_complete()
52361aadf5c3STejun Heo  *	multiple times to keep IRQ expect status properly in sync.
52371aadf5c3STejun Heo  *
5238c6fd2807SJeff Garzik  *	LOCKING:
5239cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
5240c6fd2807SJeff Garzik  *
5241c6fd2807SJeff Garzik  *	RETURNS:
5242c6fd2807SJeff Garzik  *	Number of completed commands on success, -errno otherwise.
5243c6fd2807SJeff Garzik  */
524479f97dadSTejun Heo int ata_qc_complete_multiple(struct ata_port *ap, u32 qc_active)
5245c6fd2807SJeff Garzik {
5246c6fd2807SJeff Garzik 	int nr_done = 0;
5247c6fd2807SJeff Garzik 	u32 done_mask;
5248c6fd2807SJeff Garzik 
5249c6fd2807SJeff Garzik 	done_mask = ap->qc_active ^ qc_active;
5250c6fd2807SJeff Garzik 
5251c6fd2807SJeff Garzik 	if (unlikely(done_mask & qc_active)) {
5252a9a79dfeSJoe Perches 		ata_port_err(ap, "illegal qc_active transition (%08x->%08x)\n",
5253a9a79dfeSJoe Perches 			     ap->qc_active, qc_active);
5254c6fd2807SJeff Garzik 		return -EINVAL;
5255c6fd2807SJeff Garzik 	}
5256c6fd2807SJeff Garzik 
525743768180SJens Axboe 	while (done_mask) {
5258c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc;
525943768180SJens Axboe 		unsigned int tag = __ffs(done_mask);
5260c6fd2807SJeff Garzik 
526143768180SJens Axboe 		qc = ata_qc_from_tag(ap, tag);
526243768180SJens Axboe 		if (qc) {
5263c6fd2807SJeff Garzik 			ata_qc_complete(qc);
5264c6fd2807SJeff Garzik 			nr_done++;
5265c6fd2807SJeff Garzik 		}
526643768180SJens Axboe 		done_mask &= ~(1 << tag);
5267c6fd2807SJeff Garzik 	}
5268c6fd2807SJeff Garzik 
5269c6fd2807SJeff Garzik 	return nr_done;
5270c6fd2807SJeff Garzik }
5271c6fd2807SJeff Garzik 
5272c6fd2807SJeff Garzik /**
5273c6fd2807SJeff Garzik  *	ata_qc_issue - issue taskfile to device
5274c6fd2807SJeff Garzik  *	@qc: command to issue to device
5275c6fd2807SJeff Garzik  *
5276c6fd2807SJeff Garzik  *	Prepare an ATA command to submission to device.
5277c6fd2807SJeff Garzik  *	This includes mapping the data into a DMA-able
5278c6fd2807SJeff Garzik  *	area, filling in the S/G table, and finally
5279c6fd2807SJeff Garzik  *	writing the taskfile to hardware, starting the command.
5280c6fd2807SJeff Garzik  *
5281c6fd2807SJeff Garzik  *	LOCKING:
5282cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
5283c6fd2807SJeff Garzik  */
5284c6fd2807SJeff Garzik void ata_qc_issue(struct ata_queued_cmd *qc)
5285c6fd2807SJeff Garzik {
5286c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
52879af5c9c9STejun Heo 	struct ata_link *link = qc->dev->link;
5288405e66b3STejun Heo 	u8 prot = qc->tf.protocol;
5289c6fd2807SJeff Garzik 
5290c6fd2807SJeff Garzik 	/* Make sure only one non-NCQ command is outstanding.  The
5291c6fd2807SJeff Garzik 	 * check is skipped for old EH because it reuses active qc to
5292c6fd2807SJeff Garzik 	 * request ATAPI sense.
5293c6fd2807SJeff Garzik 	 */
5294efcb3cf7STejun Heo 	WARN_ON_ONCE(ap->ops->error_handler && ata_tag_valid(link->active_tag));
5295c6fd2807SJeff Garzik 
52961973a023STejun Heo 	if (ata_is_ncq(prot)) {
5297efcb3cf7STejun Heo 		WARN_ON_ONCE(link->sactive & (1 << qc->tag));
5298da917d69STejun Heo 
5299da917d69STejun Heo 		if (!link->sactive)
5300da917d69STejun Heo 			ap->nr_active_links++;
53019af5c9c9STejun Heo 		link->sactive |= 1 << qc->tag;
5302c6fd2807SJeff Garzik 	} else {
5303efcb3cf7STejun Heo 		WARN_ON_ONCE(link->sactive);
5304da917d69STejun Heo 
5305da917d69STejun Heo 		ap->nr_active_links++;
53069af5c9c9STejun Heo 		link->active_tag = qc->tag;
5307c6fd2807SJeff Garzik 	}
5308c6fd2807SJeff Garzik 
5309c6fd2807SJeff Garzik 	qc->flags |= ATA_QCFLAG_ACTIVE;
5310c6fd2807SJeff Garzik 	ap->qc_active |= 1 << qc->tag;
5311c6fd2807SJeff Garzik 
531260f5d6efSTejun Heo 	/*
531360f5d6efSTejun Heo 	 * We guarantee to LLDs that they will have at least one
5314f92a2636STejun Heo 	 * non-zero sg if the command is a data command.
5315f92a2636STejun Heo 	 */
531660f5d6efSTejun Heo 	if (WARN_ON_ONCE(ata_is_data(prot) &&
531760f5d6efSTejun Heo 			 (!qc->sg || !qc->n_elem || !qc->nbytes)))
531860f5d6efSTejun Heo 		goto sys_err;
5319f92a2636STejun Heo 
5320405e66b3STejun Heo 	if (ata_is_dma(prot) || (ata_is_pio(prot) &&
5321f92a2636STejun Heo 				 (ap->flags & ATA_FLAG_PIO_DMA)))
5322c6fd2807SJeff Garzik 		if (ata_sg_setup(qc))
532360f5d6efSTejun Heo 			goto sys_err;
5324c6fd2807SJeff Garzik 
5325cf480626STejun Heo 	/* if device is sleeping, schedule reset and abort the link */
5326054a5fbaSTejun Heo 	if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
5327cf480626STejun Heo 		link->eh_info.action |= ATA_EH_RESET;
5328054a5fbaSTejun Heo 		ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
5329054a5fbaSTejun Heo 		ata_link_abort(link);
5330054a5fbaSTejun Heo 		return;
5331054a5fbaSTejun Heo 	}
5332054a5fbaSTejun Heo 
5333c6fd2807SJeff Garzik 	ap->ops->qc_prep(qc);
5334255c03d1SHannes Reinecke 	trace_ata_qc_issue(qc);
5335c6fd2807SJeff Garzik 	qc->err_mask |= ap->ops->qc_issue(qc);
5336c6fd2807SJeff Garzik 	if (unlikely(qc->err_mask))
5337c6fd2807SJeff Garzik 		goto err;
5338c6fd2807SJeff Garzik 	return;
5339c6fd2807SJeff Garzik 
534060f5d6efSTejun Heo sys_err:
5341c6fd2807SJeff Garzik 	qc->err_mask |= AC_ERR_SYSTEM;
5342c6fd2807SJeff Garzik err:
5343c6fd2807SJeff Garzik 	ata_qc_complete(qc);
5344c6fd2807SJeff Garzik }
5345c6fd2807SJeff Garzik 
5346c6fd2807SJeff Garzik /**
5347c6fd2807SJeff Garzik  *	sata_scr_valid - test whether SCRs are accessible
5348936fd732STejun Heo  *	@link: ATA link to test SCR accessibility for
5349c6fd2807SJeff Garzik  *
5350936fd732STejun Heo  *	Test whether SCRs are accessible for @link.
5351c6fd2807SJeff Garzik  *
5352c6fd2807SJeff Garzik  *	LOCKING:
5353c6fd2807SJeff Garzik  *	None.
5354c6fd2807SJeff Garzik  *
5355c6fd2807SJeff Garzik  *	RETURNS:
5356c6fd2807SJeff Garzik  *	1 if SCRs are accessible, 0 otherwise.
5357c6fd2807SJeff Garzik  */
5358936fd732STejun Heo int sata_scr_valid(struct ata_link *link)
5359c6fd2807SJeff Garzik {
5360936fd732STejun Heo 	struct ata_port *ap = link->ap;
5361936fd732STejun Heo 
5362a16abc0bSTejun Heo 	return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
5363c6fd2807SJeff Garzik }
5364c6fd2807SJeff Garzik 
5365c6fd2807SJeff Garzik /**
5366c6fd2807SJeff Garzik  *	sata_scr_read - read SCR register of the specified port
5367936fd732STejun Heo  *	@link: ATA link to read SCR for
5368c6fd2807SJeff Garzik  *	@reg: SCR to read
5369c6fd2807SJeff Garzik  *	@val: Place to store read value
5370c6fd2807SJeff Garzik  *
5371936fd732STejun Heo  *	Read SCR register @reg of @link into *@val.  This function is
5372633273a3STejun Heo  *	guaranteed to succeed if @link is ap->link, the cable type of
5373633273a3STejun Heo  *	the port is SATA and the port implements ->scr_read.
5374c6fd2807SJeff Garzik  *
5375c6fd2807SJeff Garzik  *	LOCKING:
5376633273a3STejun Heo  *	None if @link is ap->link.  Kernel thread context otherwise.
5377c6fd2807SJeff Garzik  *
5378c6fd2807SJeff Garzik  *	RETURNS:
5379c6fd2807SJeff Garzik  *	0 on success, negative errno on failure.
5380c6fd2807SJeff Garzik  */
5381936fd732STejun Heo int sata_scr_read(struct ata_link *link, int reg, u32 *val)
5382c6fd2807SJeff Garzik {
5383633273a3STejun Heo 	if (ata_is_host_link(link)) {
5384936fd732STejun Heo 		if (sata_scr_valid(link))
538582ef04fbSTejun Heo 			return link->ap->ops->scr_read(link, reg, val);
5386c6fd2807SJeff Garzik 		return -EOPNOTSUPP;
5387c6fd2807SJeff Garzik 	}
5388c6fd2807SJeff Garzik 
5389633273a3STejun Heo 	return sata_pmp_scr_read(link, reg, val);
5390633273a3STejun Heo }
5391633273a3STejun Heo 
5392c6fd2807SJeff Garzik /**
5393c6fd2807SJeff Garzik  *	sata_scr_write - write SCR register of the specified port
5394936fd732STejun Heo  *	@link: ATA link to write SCR for
5395c6fd2807SJeff Garzik  *	@reg: SCR to write
5396c6fd2807SJeff Garzik  *	@val: value to write
5397c6fd2807SJeff Garzik  *
5398936fd732STejun Heo  *	Write @val to SCR register @reg of @link.  This function is
5399633273a3STejun Heo  *	guaranteed to succeed if @link is ap->link, the cable type of
5400633273a3STejun Heo  *	the port is SATA and the port implements ->scr_read.
5401c6fd2807SJeff Garzik  *
5402c6fd2807SJeff Garzik  *	LOCKING:
5403633273a3STejun Heo  *	None if @link is ap->link.  Kernel thread context otherwise.
5404c6fd2807SJeff Garzik  *
5405c6fd2807SJeff Garzik  *	RETURNS:
5406c6fd2807SJeff Garzik  *	0 on success, negative errno on failure.
5407c6fd2807SJeff Garzik  */
5408936fd732STejun Heo int sata_scr_write(struct ata_link *link, int reg, u32 val)
5409c6fd2807SJeff Garzik {
5410633273a3STejun Heo 	if (ata_is_host_link(link)) {
5411936fd732STejun Heo 		if (sata_scr_valid(link))
541282ef04fbSTejun Heo 			return link->ap->ops->scr_write(link, reg, val);
5413c6fd2807SJeff Garzik 		return -EOPNOTSUPP;
5414c6fd2807SJeff Garzik 	}
5415c6fd2807SJeff Garzik 
5416633273a3STejun Heo 	return sata_pmp_scr_write(link, reg, val);
5417633273a3STejun Heo }
5418633273a3STejun Heo 
5419c6fd2807SJeff Garzik /**
5420c6fd2807SJeff Garzik  *	sata_scr_write_flush - write SCR register of the specified port and flush
5421936fd732STejun Heo  *	@link: ATA link to write SCR for
5422c6fd2807SJeff Garzik  *	@reg: SCR to write
5423c6fd2807SJeff Garzik  *	@val: value to write
5424c6fd2807SJeff Garzik  *
5425c6fd2807SJeff Garzik  *	This function is identical to sata_scr_write() except that this
5426c6fd2807SJeff Garzik  *	function performs flush after writing to the register.
5427c6fd2807SJeff Garzik  *
5428c6fd2807SJeff Garzik  *	LOCKING:
5429633273a3STejun Heo  *	None if @link is ap->link.  Kernel thread context otherwise.
5430c6fd2807SJeff Garzik  *
5431c6fd2807SJeff Garzik  *	RETURNS:
5432c6fd2807SJeff Garzik  *	0 on success, negative errno on failure.
5433c6fd2807SJeff Garzik  */
5434936fd732STejun Heo int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
5435c6fd2807SJeff Garzik {
5436633273a3STejun Heo 	if (ata_is_host_link(link)) {
5437da3dbb17STejun Heo 		int rc;
5438da3dbb17STejun Heo 
5439936fd732STejun Heo 		if (sata_scr_valid(link)) {
544082ef04fbSTejun Heo 			rc = link->ap->ops->scr_write(link, reg, val);
5441da3dbb17STejun Heo 			if (rc == 0)
544282ef04fbSTejun Heo 				rc = link->ap->ops->scr_read(link, reg, &val);
5443da3dbb17STejun Heo 			return rc;
5444c6fd2807SJeff Garzik 		}
5445c6fd2807SJeff Garzik 		return -EOPNOTSUPP;
5446c6fd2807SJeff Garzik 	}
5447c6fd2807SJeff Garzik 
5448633273a3STejun Heo 	return sata_pmp_scr_write(link, reg, val);
5449633273a3STejun Heo }
5450633273a3STejun Heo 
5451c6fd2807SJeff Garzik /**
5452b1c72916STejun Heo  *	ata_phys_link_online - test whether the given link is online
5453936fd732STejun Heo  *	@link: ATA link to test
5454c6fd2807SJeff Garzik  *
5455936fd732STejun Heo  *	Test whether @link is online.  Note that this function returns
5456936fd732STejun Heo  *	0 if online status of @link cannot be obtained, so
5457936fd732STejun Heo  *	ata_link_online(link) != !ata_link_offline(link).
5458c6fd2807SJeff Garzik  *
5459c6fd2807SJeff Garzik  *	LOCKING:
5460c6fd2807SJeff Garzik  *	None.
5461c6fd2807SJeff Garzik  *
5462c6fd2807SJeff Garzik  *	RETURNS:
5463b5b3fa38STejun Heo  *	True if the port online status is available and online.
5464c6fd2807SJeff Garzik  */
5465b1c72916STejun Heo bool ata_phys_link_online(struct ata_link *link)
5466c6fd2807SJeff Garzik {
5467c6fd2807SJeff Garzik 	u32 sstatus;
5468c6fd2807SJeff Garzik 
5469936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
54709913ff8aSTejun Heo 	    ata_sstatus_online(sstatus))
5471b5b3fa38STejun Heo 		return true;
5472b5b3fa38STejun Heo 	return false;
5473c6fd2807SJeff Garzik }
5474c6fd2807SJeff Garzik 
5475c6fd2807SJeff Garzik /**
5476b1c72916STejun Heo  *	ata_phys_link_offline - test whether the given link is offline
5477936fd732STejun Heo  *	@link: ATA link to test
5478c6fd2807SJeff Garzik  *
5479936fd732STejun Heo  *	Test whether @link is offline.  Note that this function
5480936fd732STejun Heo  *	returns 0 if offline status of @link cannot be obtained, so
5481936fd732STejun Heo  *	ata_link_online(link) != !ata_link_offline(link).
5482c6fd2807SJeff Garzik  *
5483c6fd2807SJeff Garzik  *	LOCKING:
5484c6fd2807SJeff Garzik  *	None.
5485c6fd2807SJeff Garzik  *
5486c6fd2807SJeff Garzik  *	RETURNS:
5487b5b3fa38STejun Heo  *	True if the port offline status is available and offline.
5488c6fd2807SJeff Garzik  */
5489b1c72916STejun Heo bool ata_phys_link_offline(struct ata_link *link)
5490c6fd2807SJeff Garzik {
5491c6fd2807SJeff Garzik 	u32 sstatus;
5492c6fd2807SJeff Garzik 
5493936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
54949913ff8aSTejun Heo 	    !ata_sstatus_online(sstatus))
5495b5b3fa38STejun Heo 		return true;
5496b5b3fa38STejun Heo 	return false;
5497c6fd2807SJeff Garzik }
5498c6fd2807SJeff Garzik 
5499b1c72916STejun Heo /**
5500b1c72916STejun Heo  *	ata_link_online - test whether the given link is online
5501b1c72916STejun Heo  *	@link: ATA link to test
5502b1c72916STejun Heo  *
5503b1c72916STejun Heo  *	Test whether @link is online.  This is identical to
5504b1c72916STejun Heo  *	ata_phys_link_online() when there's no slave link.  When
5505b1c72916STejun Heo  *	there's a slave link, this function should only be called on
5506b1c72916STejun Heo  *	the master link and will return true if any of M/S links is
5507b1c72916STejun Heo  *	online.
5508b1c72916STejun Heo  *
5509b1c72916STejun Heo  *	LOCKING:
5510b1c72916STejun Heo  *	None.
5511b1c72916STejun Heo  *
5512b1c72916STejun Heo  *	RETURNS:
5513b1c72916STejun Heo  *	True if the port online status is available and online.
5514b1c72916STejun Heo  */
5515b1c72916STejun Heo bool ata_link_online(struct ata_link *link)
5516b1c72916STejun Heo {
5517b1c72916STejun Heo 	struct ata_link *slave = link->ap->slave_link;
5518b1c72916STejun Heo 
5519b1c72916STejun Heo 	WARN_ON(link == slave);	/* shouldn't be called on slave link */
5520b1c72916STejun Heo 
5521b1c72916STejun Heo 	return ata_phys_link_online(link) ||
5522b1c72916STejun Heo 		(slave && ata_phys_link_online(slave));
5523b1c72916STejun Heo }
5524b1c72916STejun Heo 
5525b1c72916STejun Heo /**
5526b1c72916STejun Heo  *	ata_link_offline - test whether the given link is offline
5527b1c72916STejun Heo  *	@link: ATA link to test
5528b1c72916STejun Heo  *
5529b1c72916STejun Heo  *	Test whether @link is offline.  This is identical to
5530b1c72916STejun Heo  *	ata_phys_link_offline() when there's no slave link.  When
5531b1c72916STejun Heo  *	there's a slave link, this function should only be called on
5532b1c72916STejun Heo  *	the master link and will return true if both M/S links are
5533b1c72916STejun Heo  *	offline.
5534b1c72916STejun Heo  *
5535b1c72916STejun Heo  *	LOCKING:
5536b1c72916STejun Heo  *	None.
5537b1c72916STejun Heo  *
5538b1c72916STejun Heo  *	RETURNS:
5539b1c72916STejun Heo  *	True if the port offline status is available and offline.
5540b1c72916STejun Heo  */
5541b1c72916STejun Heo bool ata_link_offline(struct ata_link *link)
5542b1c72916STejun Heo {
5543b1c72916STejun Heo 	struct ata_link *slave = link->ap->slave_link;
5544b1c72916STejun Heo 
5545b1c72916STejun Heo 	WARN_ON(link == slave);	/* shouldn't be called on slave link */
5546b1c72916STejun Heo 
5547b1c72916STejun Heo 	return ata_phys_link_offline(link) &&
5548b1c72916STejun Heo 		(!slave || ata_phys_link_offline(slave));
5549b1c72916STejun Heo }
5550b1c72916STejun Heo 
55516ffa01d8STejun Heo #ifdef CONFIG_PM
5552bc6e7c4bSDan Williams static void ata_port_request_pm(struct ata_port *ap, pm_message_t mesg,
5553cca3974eSJeff Garzik 				unsigned int action, unsigned int ehi_flags,
5554bc6e7c4bSDan Williams 				bool async)
5555c6fd2807SJeff Garzik {
5556e3667ebfSTejun Heo 	struct ata_link *link;
55575ef41082SLin Ming 	unsigned long flags;
5558c6fd2807SJeff Garzik 
5559c6fd2807SJeff Garzik 	/* Previous resume operation might still be in
5560c6fd2807SJeff Garzik 	 * progress.  Wait for PM_PENDING to clear.
5561c6fd2807SJeff Garzik 	 */
5562c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_PM_PENDING) {
5563c6fd2807SJeff Garzik 		ata_port_wait_eh(ap);
5564c6fd2807SJeff Garzik 		WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5565c6fd2807SJeff Garzik 	}
5566c6fd2807SJeff Garzik 
5567c6fd2807SJeff Garzik 	/* request PM ops to EH */
5568c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
5569c6fd2807SJeff Garzik 
5570c6fd2807SJeff Garzik 	ap->pm_mesg = mesg;
5571c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_PM_PENDING;
55721eca4365STejun Heo 	ata_for_each_link(link, ap, HOST_FIRST) {
5573e3667ebfSTejun Heo 		link->eh_info.action |= action;
5574e3667ebfSTejun Heo 		link->eh_info.flags |= ehi_flags;
5575e3667ebfSTejun Heo 	}
5576c6fd2807SJeff Garzik 
5577c6fd2807SJeff Garzik 	ata_port_schedule_eh(ap);
5578c6fd2807SJeff Garzik 
5579c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
5580c6fd2807SJeff Garzik 
55812fcbdcb4SDan Williams 	if (!async) {
5582c6fd2807SJeff Garzik 		ata_port_wait_eh(ap);
5583c6fd2807SJeff Garzik 		WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5584c6fd2807SJeff Garzik 	}
5585c6fd2807SJeff Garzik }
5586c6fd2807SJeff Garzik 
558733574d68SLin Ming /*
5588bc6e7c4bSDan Williams  * On some hardware, device fails to respond after spun down for suspend.  As
5589bc6e7c4bSDan Williams  * the device won't be used before being resumed, we don't need to touch the
5590bc6e7c4bSDan Williams  * device.  Ask EH to skip the usual stuff and proceed directly to suspend.
559133574d68SLin Ming  *
559233574d68SLin Ming  * http://thread.gmane.org/gmane.linux.ide/46764
559333574d68SLin Ming  */
5594bc6e7c4bSDan Williams static const unsigned int ata_port_suspend_ehi = ATA_EHI_QUIET
5595bc6e7c4bSDan Williams 						 | ATA_EHI_NO_AUTOPSY
5596bc6e7c4bSDan Williams 						 | ATA_EHI_NO_RECOVERY;
5597bc6e7c4bSDan Williams 
5598bc6e7c4bSDan Williams static void ata_port_suspend(struct ata_port *ap, pm_message_t mesg)
5599bc6e7c4bSDan Williams {
5600bc6e7c4bSDan Williams 	ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, false);
56015ef41082SLin Ming }
56025ef41082SLin Ming 
5603bc6e7c4bSDan Williams static void ata_port_suspend_async(struct ata_port *ap, pm_message_t mesg)
5604bc6e7c4bSDan Williams {
5605bc6e7c4bSDan Williams 	ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, true);
5606bc6e7c4bSDan Williams }
5607bc6e7c4bSDan Williams 
5608bc6e7c4bSDan Williams static int ata_port_pm_suspend(struct device *dev)
56092fcbdcb4SDan Williams {
56102fcbdcb4SDan Williams 	struct ata_port *ap = to_ata_port(dev);
56112fcbdcb4SDan Williams 
56125ef41082SLin Ming 	if (pm_runtime_suspended(dev))
56135ef41082SLin Ming 		return 0;
56145ef41082SLin Ming 
5615bc6e7c4bSDan Williams 	ata_port_suspend(ap, PMSG_SUSPEND);
5616f5e6d0d0SAaron Lu 	return 0;
561733574d68SLin Ming }
561833574d68SLin Ming 
5619bc6e7c4bSDan Williams static int ata_port_pm_freeze(struct device *dev)
56202fcbdcb4SDan Williams {
56212fcbdcb4SDan Williams 	struct ata_port *ap = to_ata_port(dev);
56222fcbdcb4SDan Williams 
5623bc6e7c4bSDan Williams 	if (pm_runtime_suspended(dev))
5624bc6e7c4bSDan Williams 		return 0;
5625bc6e7c4bSDan Williams 
5626bc6e7c4bSDan Williams 	ata_port_suspend(ap, PMSG_FREEZE);
5627bc6e7c4bSDan Williams 	return 0;
56282fcbdcb4SDan Williams }
56292fcbdcb4SDan Williams 
5630bc6e7c4bSDan Williams static int ata_port_pm_poweroff(struct device *dev)
5631e90b1e5aSLin Ming {
5632bc6e7c4bSDan Williams 	ata_port_suspend(to_ata_port(dev), PMSG_HIBERNATE);
5633bc6e7c4bSDan Williams 	return 0;
5634bc6e7c4bSDan Williams }
5635e90b1e5aSLin Ming 
5636bc6e7c4bSDan Williams static const unsigned int ata_port_resume_ehi = ATA_EHI_NO_AUTOPSY
5637bc6e7c4bSDan Williams 						| ATA_EHI_QUIET;
5638bc6e7c4bSDan Williams 
5639bc6e7c4bSDan Williams static void ata_port_resume(struct ata_port *ap, pm_message_t mesg)
5640bc6e7c4bSDan Williams {
5641bc6e7c4bSDan Williams 	ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, false);
5642bc6e7c4bSDan Williams }
5643bc6e7c4bSDan Williams 
5644bc6e7c4bSDan Williams static void ata_port_resume_async(struct ata_port *ap, pm_message_t mesg)
5645bc6e7c4bSDan Williams {
5646bc6e7c4bSDan Williams 	ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, true);
5647bc6e7c4bSDan Williams }
5648bc6e7c4bSDan Williams 
5649bc6e7c4bSDan Williams static int ata_port_pm_resume(struct device *dev)
5650bc6e7c4bSDan Williams {
5651200421a8STodd Brandt 	ata_port_resume_async(to_ata_port(dev), PMSG_RESUME);
5652e90b1e5aSLin Ming 	pm_runtime_disable(dev);
5653e90b1e5aSLin Ming 	pm_runtime_set_active(dev);
5654e90b1e5aSLin Ming 	pm_runtime_enable(dev);
5655bc6e7c4bSDan Williams 	return 0;
5656e90b1e5aSLin Ming }
5657e90b1e5aSLin Ming 
56587e15e9beSAaron Lu /*
56597e15e9beSAaron Lu  * For ODDs, the upper layer will poll for media change every few seconds,
56607e15e9beSAaron Lu  * which will make it enter and leave suspend state every few seconds. And
56617e15e9beSAaron Lu  * as each suspend will cause a hard/soft reset, the gain of runtime suspend
56627e15e9beSAaron Lu  * is very little and the ODD may malfunction after constantly being reset.
56637e15e9beSAaron Lu  * So the idle callback here will not proceed to suspend if a non-ZPODD capable
56647e15e9beSAaron Lu  * ODD is attached to the port.
56657e15e9beSAaron Lu  */
56669ee4f393SLin Ming static int ata_port_runtime_idle(struct device *dev)
56679ee4f393SLin Ming {
56687e15e9beSAaron Lu 	struct ata_port *ap = to_ata_port(dev);
56697e15e9beSAaron Lu 	struct ata_link *link;
56707e15e9beSAaron Lu 	struct ata_device *adev;
56717e15e9beSAaron Lu 
56727e15e9beSAaron Lu 	ata_for_each_link(link, ap, HOST_FIRST) {
56737e15e9beSAaron Lu 		ata_for_each_dev(adev, link, ENABLED)
56747e15e9beSAaron Lu 			if (adev->class == ATA_DEV_ATAPI &&
56757e15e9beSAaron Lu 			    !zpodd_dev_enabled(adev))
56767e15e9beSAaron Lu 				return -EBUSY;
56777e15e9beSAaron Lu 	}
56787e15e9beSAaron Lu 
567945f0a85cSRafael J. Wysocki 	return 0;
56809ee4f393SLin Ming }
56819ee4f393SLin Ming 
5682a7ff60dbSAaron Lu static int ata_port_runtime_suspend(struct device *dev)
5683a7ff60dbSAaron Lu {
5684bc6e7c4bSDan Williams 	ata_port_suspend(to_ata_port(dev), PMSG_AUTO_SUSPEND);
5685bc6e7c4bSDan Williams 	return 0;
5686a7ff60dbSAaron Lu }
5687a7ff60dbSAaron Lu 
5688a7ff60dbSAaron Lu static int ata_port_runtime_resume(struct device *dev)
5689a7ff60dbSAaron Lu {
5690bc6e7c4bSDan Williams 	ata_port_resume(to_ata_port(dev), PMSG_AUTO_RESUME);
5691bc6e7c4bSDan Williams 	return 0;
5692a7ff60dbSAaron Lu }
5693a7ff60dbSAaron Lu 
56945ef41082SLin Ming static const struct dev_pm_ops ata_port_pm_ops = {
5695bc6e7c4bSDan Williams 	.suspend = ata_port_pm_suspend,
5696bc6e7c4bSDan Williams 	.resume = ata_port_pm_resume,
5697bc6e7c4bSDan Williams 	.freeze = ata_port_pm_freeze,
5698bc6e7c4bSDan Williams 	.thaw = ata_port_pm_resume,
5699bc6e7c4bSDan Williams 	.poweroff = ata_port_pm_poweroff,
5700bc6e7c4bSDan Williams 	.restore = ata_port_pm_resume,
57019ee4f393SLin Ming 
5702a7ff60dbSAaron Lu 	.runtime_suspend = ata_port_runtime_suspend,
5703a7ff60dbSAaron Lu 	.runtime_resume = ata_port_runtime_resume,
57049ee4f393SLin Ming 	.runtime_idle = ata_port_runtime_idle,
57055ef41082SLin Ming };
57065ef41082SLin Ming 
57072fcbdcb4SDan Williams /* sas ports don't participate in pm runtime management of ata_ports,
57082fcbdcb4SDan Williams  * and need to resume ata devices at the domain level, not the per-port
57092fcbdcb4SDan Williams  * level. sas suspend/resume is async to allow parallel port recovery
57102fcbdcb4SDan Williams  * since sas has multiple ata_port instances per Scsi_Host.
57112fcbdcb4SDan Williams  */
5712bc6e7c4bSDan Williams void ata_sas_port_suspend(struct ata_port *ap)
57132fcbdcb4SDan Williams {
5714bc6e7c4bSDan Williams 	ata_port_suspend_async(ap, PMSG_SUSPEND);
57152fcbdcb4SDan Williams }
5716bc6e7c4bSDan Williams EXPORT_SYMBOL_GPL(ata_sas_port_suspend);
57172fcbdcb4SDan Williams 
5718bc6e7c4bSDan Williams void ata_sas_port_resume(struct ata_port *ap)
57192fcbdcb4SDan Williams {
5720bc6e7c4bSDan Williams 	ata_port_resume_async(ap, PMSG_RESUME);
57212fcbdcb4SDan Williams }
5722bc6e7c4bSDan Williams EXPORT_SYMBOL_GPL(ata_sas_port_resume);
57232fcbdcb4SDan Williams 
5724c6fd2807SJeff Garzik /**
5725cca3974eSJeff Garzik  *	ata_host_suspend - suspend host
5726cca3974eSJeff Garzik  *	@host: host to suspend
5727c6fd2807SJeff Garzik  *	@mesg: PM message
5728c6fd2807SJeff Garzik  *
57295ef41082SLin Ming  *	Suspend @host.  Actual operation is performed by port suspend.
5730c6fd2807SJeff Garzik  */
5731cca3974eSJeff Garzik int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
5732c6fd2807SJeff Garzik {
573372ad6ec4SJeff Garzik 	host->dev->power.power_state = mesg;
57345ef41082SLin Ming 	return 0;
5735c6fd2807SJeff Garzik }
5736c6fd2807SJeff Garzik 
5737c6fd2807SJeff Garzik /**
5738cca3974eSJeff Garzik  *	ata_host_resume - resume host
5739cca3974eSJeff Garzik  *	@host: host to resume
5740c6fd2807SJeff Garzik  *
57415ef41082SLin Ming  *	Resume @host.  Actual operation is performed by port resume.
5742c6fd2807SJeff Garzik  */
5743cca3974eSJeff Garzik void ata_host_resume(struct ata_host *host)
5744c6fd2807SJeff Garzik {
574572ad6ec4SJeff Garzik 	host->dev->power.power_state = PMSG_ON;
5746c6fd2807SJeff Garzik }
57476ffa01d8STejun Heo #endif
5748c6fd2807SJeff Garzik 
57495ef41082SLin Ming struct device_type ata_port_type = {
57505ef41082SLin Ming 	.name = "ata_port",
57515ef41082SLin Ming #ifdef CONFIG_PM
57525ef41082SLin Ming 	.pm = &ata_port_pm_ops,
57535ef41082SLin Ming #endif
57545ef41082SLin Ming };
57555ef41082SLin Ming 
5756c6fd2807SJeff Garzik /**
5757c6fd2807SJeff Garzik  *	ata_dev_init - Initialize an ata_device structure
5758c6fd2807SJeff Garzik  *	@dev: Device structure to initialize
5759c6fd2807SJeff Garzik  *
5760c6fd2807SJeff Garzik  *	Initialize @dev in preparation for probing.
5761c6fd2807SJeff Garzik  *
5762c6fd2807SJeff Garzik  *	LOCKING:
5763c6fd2807SJeff Garzik  *	Inherited from caller.
5764c6fd2807SJeff Garzik  */
5765c6fd2807SJeff Garzik void ata_dev_init(struct ata_device *dev)
5766c6fd2807SJeff Garzik {
5767b1c72916STejun Heo 	struct ata_link *link = ata_dev_phys_link(dev);
57689af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
5769c6fd2807SJeff Garzik 	unsigned long flags;
5770c6fd2807SJeff Garzik 
5771b1c72916STejun Heo 	/* SATA spd limit is bound to the attached device, reset together */
57729af5c9c9STejun Heo 	link->sata_spd_limit = link->hw_sata_spd_limit;
57739af5c9c9STejun Heo 	link->sata_spd = 0;
5774c6fd2807SJeff Garzik 
5775c6fd2807SJeff Garzik 	/* High bits of dev->flags are used to record warm plug
5776c6fd2807SJeff Garzik 	 * requests which occur asynchronously.  Synchronize using
5777cca3974eSJeff Garzik 	 * host lock.
5778c6fd2807SJeff Garzik 	 */
5779c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
5780c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_INIT_MASK;
57813dcc323fSTejun Heo 	dev->horkage = 0;
5782c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
5783c6fd2807SJeff Garzik 
578499cf610aSTejun Heo 	memset((void *)dev + ATA_DEVICE_CLEAR_BEGIN, 0,
578599cf610aSTejun Heo 	       ATA_DEVICE_CLEAR_END - ATA_DEVICE_CLEAR_BEGIN);
5786c6fd2807SJeff Garzik 	dev->pio_mask = UINT_MAX;
5787c6fd2807SJeff Garzik 	dev->mwdma_mask = UINT_MAX;
5788c6fd2807SJeff Garzik 	dev->udma_mask = UINT_MAX;
5789c6fd2807SJeff Garzik }
5790c6fd2807SJeff Garzik 
5791c6fd2807SJeff Garzik /**
57924fb37a25STejun Heo  *	ata_link_init - Initialize an ata_link structure
57934fb37a25STejun Heo  *	@ap: ATA port link is attached to
57944fb37a25STejun Heo  *	@link: Link structure to initialize
57958989805dSTejun Heo  *	@pmp: Port multiplier port number
57964fb37a25STejun Heo  *
57974fb37a25STejun Heo  *	Initialize @link.
57984fb37a25STejun Heo  *
57994fb37a25STejun Heo  *	LOCKING:
58004fb37a25STejun Heo  *	Kernel thread context (may sleep)
58014fb37a25STejun Heo  */
5802fb7fd614STejun Heo void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
58034fb37a25STejun Heo {
58044fb37a25STejun Heo 	int i;
58054fb37a25STejun Heo 
58064fb37a25STejun Heo 	/* clear everything except for devices */
5807d9027470SGwendal Grignou 	memset((void *)link + ATA_LINK_CLEAR_BEGIN, 0,
5808d9027470SGwendal Grignou 	       ATA_LINK_CLEAR_END - ATA_LINK_CLEAR_BEGIN);
58094fb37a25STejun Heo 
58104fb37a25STejun Heo 	link->ap = ap;
58118989805dSTejun Heo 	link->pmp = pmp;
58124fb37a25STejun Heo 	link->active_tag = ATA_TAG_POISON;
58134fb37a25STejun Heo 	link->hw_sata_spd_limit = UINT_MAX;
58144fb37a25STejun Heo 
58154fb37a25STejun Heo 	/* can't use iterator, ap isn't initialized yet */
58164fb37a25STejun Heo 	for (i = 0; i < ATA_MAX_DEVICES; i++) {
58174fb37a25STejun Heo 		struct ata_device *dev = &link->device[i];
58184fb37a25STejun Heo 
58194fb37a25STejun Heo 		dev->link = link;
58204fb37a25STejun Heo 		dev->devno = dev - link->device;
5821110f66d2STejun Heo #ifdef CONFIG_ATA_ACPI
5822110f66d2STejun Heo 		dev->gtf_filter = ata_acpi_gtf_filter;
5823110f66d2STejun Heo #endif
58244fb37a25STejun Heo 		ata_dev_init(dev);
58254fb37a25STejun Heo 	}
58264fb37a25STejun Heo }
58274fb37a25STejun Heo 
58284fb37a25STejun Heo /**
58294fb37a25STejun Heo  *	sata_link_init_spd - Initialize link->sata_spd_limit
58304fb37a25STejun Heo  *	@link: Link to configure sata_spd_limit for
58314fb37a25STejun Heo  *
58324fb37a25STejun Heo  *	Initialize @link->[hw_]sata_spd_limit to the currently
58334fb37a25STejun Heo  *	configured value.
58344fb37a25STejun Heo  *
58354fb37a25STejun Heo  *	LOCKING:
58364fb37a25STejun Heo  *	Kernel thread context (may sleep).
58374fb37a25STejun Heo  *
58384fb37a25STejun Heo  *	RETURNS:
58394fb37a25STejun Heo  *	0 on success, -errno on failure.
58404fb37a25STejun Heo  */
5841fb7fd614STejun Heo int sata_link_init_spd(struct ata_link *link)
58424fb37a25STejun Heo {
584333267325STejun Heo 	u8 spd;
58444fb37a25STejun Heo 	int rc;
58454fb37a25STejun Heo 
5846d127ea7bSTejun Heo 	rc = sata_scr_read(link, SCR_CONTROL, &link->saved_scontrol);
58474fb37a25STejun Heo 	if (rc)
58484fb37a25STejun Heo 		return rc;
58494fb37a25STejun Heo 
5850d127ea7bSTejun Heo 	spd = (link->saved_scontrol >> 4) & 0xf;
58514fb37a25STejun Heo 	if (spd)
58524fb37a25STejun Heo 		link->hw_sata_spd_limit &= (1 << spd) - 1;
58534fb37a25STejun Heo 
585405944bdfSTejun Heo 	ata_force_link_limits(link);
585533267325STejun Heo 
58564fb37a25STejun Heo 	link->sata_spd_limit = link->hw_sata_spd_limit;
58574fb37a25STejun Heo 
58584fb37a25STejun Heo 	return 0;
58594fb37a25STejun Heo }
58604fb37a25STejun Heo 
58614fb37a25STejun Heo /**
5862f3187195STejun Heo  *	ata_port_alloc - allocate and initialize basic ATA port resources
5863f3187195STejun Heo  *	@host: ATA host this allocated port belongs to
5864c6fd2807SJeff Garzik  *
5865f3187195STejun Heo  *	Allocate and initialize basic ATA port resources.
5866f3187195STejun Heo  *
5867f3187195STejun Heo  *	RETURNS:
5868f3187195STejun Heo  *	Allocate ATA port on success, NULL on failure.
5869c6fd2807SJeff Garzik  *
5870c6fd2807SJeff Garzik  *	LOCKING:
5871f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5872c6fd2807SJeff Garzik  */
5873f3187195STejun Heo struct ata_port *ata_port_alloc(struct ata_host *host)
5874c6fd2807SJeff Garzik {
5875f3187195STejun Heo 	struct ata_port *ap;
5876c6fd2807SJeff Garzik 
5877f3187195STejun Heo 	DPRINTK("ENTER\n");
5878f3187195STejun Heo 
5879f3187195STejun Heo 	ap = kzalloc(sizeof(*ap), GFP_KERNEL);
5880f3187195STejun Heo 	if (!ap)
5881f3187195STejun Heo 		return NULL;
5882f3187195STejun Heo 
58837b3a24c5SMaxime Bizon 	ap->pflags |= ATA_PFLAG_INITIALIZING | ATA_PFLAG_FROZEN;
5884cca3974eSJeff Garzik 	ap->lock = &host->lock;
5885f3187195STejun Heo 	ap->print_id = -1;
5886e628dc99SDavid Milburn 	ap->local_port_no = -1;
5887cca3974eSJeff Garzik 	ap->host = host;
5888f3187195STejun Heo 	ap->dev = host->dev;
5889c6fd2807SJeff Garzik 
5890c6fd2807SJeff Garzik #if defined(ATA_VERBOSE_DEBUG)
5891c6fd2807SJeff Garzik 	/* turn on all debugging levels */
5892c6fd2807SJeff Garzik 	ap->msg_enable = 0x00FF;
5893c6fd2807SJeff Garzik #elif defined(ATA_DEBUG)
5894c6fd2807SJeff Garzik 	ap->msg_enable = ATA_MSG_DRV | ATA_MSG_INFO | ATA_MSG_CTL | ATA_MSG_WARN | ATA_MSG_ERR;
5895c6fd2807SJeff Garzik #else
5896c6fd2807SJeff Garzik 	ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
5897c6fd2807SJeff Garzik #endif
5898c6fd2807SJeff Garzik 
5899ad72cf98STejun Heo 	mutex_init(&ap->scsi_scan_mutex);
590065f27f38SDavid Howells 	INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
590165f27f38SDavid Howells 	INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
5902c6fd2807SJeff Garzik 	INIT_LIST_HEAD(&ap->eh_done_q);
5903c6fd2807SJeff Garzik 	init_waitqueue_head(&ap->eh_wait_q);
590445fabbb7SElias Oltmanns 	init_completion(&ap->park_req_pending);
59057c92357cSGeliang Tang 	setup_deferrable_timer(&ap->fastdrain_timer,
59067c92357cSGeliang Tang 			       ata_eh_fastdrain_timerfn,
59077c92357cSGeliang Tang 			       (unsigned long)ap);
5908c6fd2807SJeff Garzik 
5909c6fd2807SJeff Garzik 	ap->cbl = ATA_CBL_NONE;
5910c6fd2807SJeff Garzik 
59118989805dSTejun Heo 	ata_link_init(ap, &ap->link, 0);
5912c6fd2807SJeff Garzik 
5913c6fd2807SJeff Garzik #ifdef ATA_IRQ_TRAP
5914c6fd2807SJeff Garzik 	ap->stats.unhandled_irq = 1;
5915c6fd2807SJeff Garzik 	ap->stats.idle_irq = 1;
5916c6fd2807SJeff Garzik #endif
5917270390e1STejun Heo 	ata_sff_port_init(ap);
5918270390e1STejun Heo 
5919c6fd2807SJeff Garzik 	return ap;
5920c6fd2807SJeff Garzik }
5921c6fd2807SJeff Garzik 
5922f0d36efdSTejun Heo static void ata_host_release(struct device *gendev, void *res)
5923f0d36efdSTejun Heo {
5924f0d36efdSTejun Heo 	struct ata_host *host = dev_get_drvdata(gendev);
5925f0d36efdSTejun Heo 	int i;
5926f0d36efdSTejun Heo 
5927f0d36efdSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
5928f0d36efdSTejun Heo 		struct ata_port *ap = host->ports[i];
5929f0d36efdSTejun Heo 
5930ecef7253STejun Heo 		if (!ap)
5931ecef7253STejun Heo 			continue;
5932ecef7253STejun Heo 
59334911487aSTejun Heo 		if (ap->scsi_host)
59341aa506e4STejun Heo 			scsi_host_put(ap->scsi_host);
59351aa506e4STejun Heo 
5936633273a3STejun Heo 		kfree(ap->pmp_link);
5937b1c72916STejun Heo 		kfree(ap->slave_link);
59384911487aSTejun Heo 		kfree(ap);
59391aa506e4STejun Heo 		host->ports[i] = NULL;
59401aa506e4STejun Heo 	}
59411aa506e4STejun Heo 
59421aa56ccaSTejun Heo 	dev_set_drvdata(gendev, NULL);
5943f0d36efdSTejun Heo }
5944f0d36efdSTejun Heo 
5945c6fd2807SJeff Garzik /**
5946f3187195STejun Heo  *	ata_host_alloc - allocate and init basic ATA host resources
5947f3187195STejun Heo  *	@dev: generic device this host is associated with
5948f3187195STejun Heo  *	@max_ports: maximum number of ATA ports associated with this host
5949f3187195STejun Heo  *
5950f3187195STejun Heo  *	Allocate and initialize basic ATA host resources.  LLD calls
5951f3187195STejun Heo  *	this function to allocate a host, initializes it fully and
5952f3187195STejun Heo  *	attaches it using ata_host_register().
5953f3187195STejun Heo  *
5954f3187195STejun Heo  *	@max_ports ports are allocated and host->n_ports is
5955f3187195STejun Heo  *	initialized to @max_ports.  The caller is allowed to decrease
5956f3187195STejun Heo  *	host->n_ports before calling ata_host_register().  The unused
5957f3187195STejun Heo  *	ports will be automatically freed on registration.
5958f3187195STejun Heo  *
5959f3187195STejun Heo  *	RETURNS:
5960f3187195STejun Heo  *	Allocate ATA host on success, NULL on failure.
5961f3187195STejun Heo  *
5962f3187195STejun Heo  *	LOCKING:
5963f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5964f3187195STejun Heo  */
5965f3187195STejun Heo struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
5966f3187195STejun Heo {
5967f3187195STejun Heo 	struct ata_host *host;
5968f3187195STejun Heo 	size_t sz;
5969f3187195STejun Heo 	int i;
5970f3187195STejun Heo 
5971f3187195STejun Heo 	DPRINTK("ENTER\n");
5972f3187195STejun Heo 
5973f3187195STejun Heo 	if (!devres_open_group(dev, NULL, GFP_KERNEL))
5974f3187195STejun Heo 		return NULL;
5975f3187195STejun Heo 
5976f3187195STejun Heo 	/* alloc a container for our list of ATA ports (buses) */
5977f3187195STejun Heo 	sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
5978f3187195STejun Heo 	/* alloc a container for our list of ATA ports (buses) */
5979f3187195STejun Heo 	host = devres_alloc(ata_host_release, sz, GFP_KERNEL);
5980f3187195STejun Heo 	if (!host)
5981f3187195STejun Heo 		goto err_out;
5982f3187195STejun Heo 
5983f3187195STejun Heo 	devres_add(dev, host);
5984f3187195STejun Heo 	dev_set_drvdata(dev, host);
5985f3187195STejun Heo 
5986f3187195STejun Heo 	spin_lock_init(&host->lock);
5987c0c362b6STejun Heo 	mutex_init(&host->eh_mutex);
5988f3187195STejun Heo 	host->dev = dev;
5989f3187195STejun Heo 	host->n_ports = max_ports;
5990f3187195STejun Heo 
5991f3187195STejun Heo 	/* allocate ports bound to this host */
5992f3187195STejun Heo 	for (i = 0; i < max_ports; i++) {
5993f3187195STejun Heo 		struct ata_port *ap;
5994f3187195STejun Heo 
5995f3187195STejun Heo 		ap = ata_port_alloc(host);
5996f3187195STejun Heo 		if (!ap)
5997f3187195STejun Heo 			goto err_out;
5998f3187195STejun Heo 
5999f3187195STejun Heo 		ap->port_no = i;
6000f3187195STejun Heo 		host->ports[i] = ap;
6001f3187195STejun Heo 	}
6002f3187195STejun Heo 
6003f3187195STejun Heo 	devres_remove_group(dev, NULL);
6004f3187195STejun Heo 	return host;
6005f3187195STejun Heo 
6006f3187195STejun Heo  err_out:
6007f3187195STejun Heo 	devres_release_group(dev, NULL);
6008f3187195STejun Heo 	return NULL;
6009f3187195STejun Heo }
6010f3187195STejun Heo 
6011f3187195STejun Heo /**
6012f5cda257STejun Heo  *	ata_host_alloc_pinfo - alloc host and init with port_info array
6013f5cda257STejun Heo  *	@dev: generic device this host is associated with
6014f5cda257STejun Heo  *	@ppi: array of ATA port_info to initialize host with
6015f5cda257STejun Heo  *	@n_ports: number of ATA ports attached to this host
6016f5cda257STejun Heo  *
6017f5cda257STejun Heo  *	Allocate ATA host and initialize with info from @ppi.  If NULL
6018f5cda257STejun Heo  *	terminated, @ppi may contain fewer entries than @n_ports.  The
6019f5cda257STejun Heo  *	last entry will be used for the remaining ports.
6020f5cda257STejun Heo  *
6021f5cda257STejun Heo  *	RETURNS:
6022f5cda257STejun Heo  *	Allocate ATA host on success, NULL on failure.
6023f5cda257STejun Heo  *
6024f5cda257STejun Heo  *	LOCKING:
6025f5cda257STejun Heo  *	Inherited from calling layer (may sleep).
6026f5cda257STejun Heo  */
6027f5cda257STejun Heo struct ata_host *ata_host_alloc_pinfo(struct device *dev,
6028f5cda257STejun Heo 				      const struct ata_port_info * const * ppi,
6029f5cda257STejun Heo 				      int n_ports)
6030f5cda257STejun Heo {
6031f5cda257STejun Heo 	const struct ata_port_info *pi;
6032f5cda257STejun Heo 	struct ata_host *host;
6033f5cda257STejun Heo 	int i, j;
6034f5cda257STejun Heo 
6035f5cda257STejun Heo 	host = ata_host_alloc(dev, n_ports);
6036f5cda257STejun Heo 	if (!host)
6037f5cda257STejun Heo 		return NULL;
6038f5cda257STejun Heo 
6039f5cda257STejun Heo 	for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
6040f5cda257STejun Heo 		struct ata_port *ap = host->ports[i];
6041f5cda257STejun Heo 
6042f5cda257STejun Heo 		if (ppi[j])
6043f5cda257STejun Heo 			pi = ppi[j++];
6044f5cda257STejun Heo 
6045f5cda257STejun Heo 		ap->pio_mask = pi->pio_mask;
6046f5cda257STejun Heo 		ap->mwdma_mask = pi->mwdma_mask;
6047f5cda257STejun Heo 		ap->udma_mask = pi->udma_mask;
6048f5cda257STejun Heo 		ap->flags |= pi->flags;
60490c88758bSTejun Heo 		ap->link.flags |= pi->link_flags;
6050f5cda257STejun Heo 		ap->ops = pi->port_ops;
6051f5cda257STejun Heo 
6052f5cda257STejun Heo 		if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
6053f5cda257STejun Heo 			host->ops = pi->port_ops;
6054f5cda257STejun Heo 	}
6055f5cda257STejun Heo 
6056f5cda257STejun Heo 	return host;
6057f5cda257STejun Heo }
6058f5cda257STejun Heo 
6059b1c72916STejun Heo /**
6060b1c72916STejun Heo  *	ata_slave_link_init - initialize slave link
6061b1c72916STejun Heo  *	@ap: port to initialize slave link for
6062b1c72916STejun Heo  *
6063b1c72916STejun Heo  *	Create and initialize slave link for @ap.  This enables slave
6064b1c72916STejun Heo  *	link handling on the port.
6065b1c72916STejun Heo  *
6066b1c72916STejun Heo  *	In libata, a port contains links and a link contains devices.
6067b1c72916STejun Heo  *	There is single host link but if a PMP is attached to it,
6068b1c72916STejun Heo  *	there can be multiple fan-out links.  On SATA, there's usually
6069b1c72916STejun Heo  *	a single device connected to a link but PATA and SATA
6070b1c72916STejun Heo  *	controllers emulating TF based interface can have two - master
6071b1c72916STejun Heo  *	and slave.
6072b1c72916STejun Heo  *
6073b1c72916STejun Heo  *	However, there are a few controllers which don't fit into this
6074b1c72916STejun Heo  *	abstraction too well - SATA controllers which emulate TF
6075b1c72916STejun Heo  *	interface with both master and slave devices but also have
6076b1c72916STejun Heo  *	separate SCR register sets for each device.  These controllers
6077b1c72916STejun Heo  *	need separate links for physical link handling
6078b1c72916STejun Heo  *	(e.g. onlineness, link speed) but should be treated like a
6079b1c72916STejun Heo  *	traditional M/S controller for everything else (e.g. command
6080b1c72916STejun Heo  *	issue, softreset).
6081b1c72916STejun Heo  *
6082b1c72916STejun Heo  *	slave_link is libata's way of handling this class of
6083b1c72916STejun Heo  *	controllers without impacting core layer too much.  For
6084b1c72916STejun Heo  *	anything other than physical link handling, the default host
6085b1c72916STejun Heo  *	link is used for both master and slave.  For physical link
6086b1c72916STejun Heo  *	handling, separate @ap->slave_link is used.  All dirty details
6087b1c72916STejun Heo  *	are implemented inside libata core layer.  From LLD's POV, the
6088b1c72916STejun Heo  *	only difference is that prereset, hardreset and postreset are
6089b1c72916STejun Heo  *	called once more for the slave link, so the reset sequence
6090b1c72916STejun Heo  *	looks like the following.
6091b1c72916STejun Heo  *
6092b1c72916STejun Heo  *	prereset(M) -> prereset(S) -> hardreset(M) -> hardreset(S) ->
6093b1c72916STejun Heo  *	softreset(M) -> postreset(M) -> postreset(S)
6094b1c72916STejun Heo  *
6095b1c72916STejun Heo  *	Note that softreset is called only for the master.  Softreset
6096b1c72916STejun Heo  *	resets both M/S by definition, so SRST on master should handle
6097b1c72916STejun Heo  *	both (the standard method will work just fine).
6098b1c72916STejun Heo  *
6099b1c72916STejun Heo  *	LOCKING:
6100b1c72916STejun Heo  *	Should be called before host is registered.
6101b1c72916STejun Heo  *
6102b1c72916STejun Heo  *	RETURNS:
6103b1c72916STejun Heo  *	0 on success, -errno on failure.
6104b1c72916STejun Heo  */
6105b1c72916STejun Heo int ata_slave_link_init(struct ata_port *ap)
6106b1c72916STejun Heo {
6107b1c72916STejun Heo 	struct ata_link *link;
6108b1c72916STejun Heo 
6109b1c72916STejun Heo 	WARN_ON(ap->slave_link);
6110b1c72916STejun Heo 	WARN_ON(ap->flags & ATA_FLAG_PMP);
6111b1c72916STejun Heo 
6112b1c72916STejun Heo 	link = kzalloc(sizeof(*link), GFP_KERNEL);
6113b1c72916STejun Heo 	if (!link)
6114b1c72916STejun Heo 		return -ENOMEM;
6115b1c72916STejun Heo 
6116b1c72916STejun Heo 	ata_link_init(ap, link, 1);
6117b1c72916STejun Heo 	ap->slave_link = link;
6118b1c72916STejun Heo 	return 0;
6119b1c72916STejun Heo }
6120b1c72916STejun Heo 
612132ebbc0cSTejun Heo static void ata_host_stop(struct device *gendev, void *res)
612232ebbc0cSTejun Heo {
612332ebbc0cSTejun Heo 	struct ata_host *host = dev_get_drvdata(gendev);
612432ebbc0cSTejun Heo 	int i;
612532ebbc0cSTejun Heo 
612632ebbc0cSTejun Heo 	WARN_ON(!(host->flags & ATA_HOST_STARTED));
612732ebbc0cSTejun Heo 
612832ebbc0cSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
612932ebbc0cSTejun Heo 		struct ata_port *ap = host->ports[i];
613032ebbc0cSTejun Heo 
613132ebbc0cSTejun Heo 		if (ap->ops->port_stop)
613232ebbc0cSTejun Heo 			ap->ops->port_stop(ap);
613332ebbc0cSTejun Heo 	}
613432ebbc0cSTejun Heo 
613532ebbc0cSTejun Heo 	if (host->ops->host_stop)
613632ebbc0cSTejun Heo 		host->ops->host_stop(host);
613732ebbc0cSTejun Heo }
613832ebbc0cSTejun Heo 
6139f5cda257STejun Heo /**
6140029cfd6bSTejun Heo  *	ata_finalize_port_ops - finalize ata_port_operations
6141029cfd6bSTejun Heo  *	@ops: ata_port_operations to finalize
6142029cfd6bSTejun Heo  *
6143029cfd6bSTejun Heo  *	An ata_port_operations can inherit from another ops and that
6144029cfd6bSTejun Heo  *	ops can again inherit from another.  This can go on as many
6145029cfd6bSTejun Heo  *	times as necessary as long as there is no loop in the
6146029cfd6bSTejun Heo  *	inheritance chain.
6147029cfd6bSTejun Heo  *
6148029cfd6bSTejun Heo  *	Ops tables are finalized when the host is started.  NULL or
6149029cfd6bSTejun Heo  *	unspecified entries are inherited from the closet ancestor
6150029cfd6bSTejun Heo  *	which has the method and the entry is populated with it.
6151029cfd6bSTejun Heo  *	After finalization, the ops table directly points to all the
6152029cfd6bSTejun Heo  *	methods and ->inherits is no longer necessary and cleared.
6153029cfd6bSTejun Heo  *
6154029cfd6bSTejun Heo  *	Using ATA_OP_NULL, inheriting ops can force a method to NULL.
6155029cfd6bSTejun Heo  *
6156029cfd6bSTejun Heo  *	LOCKING:
6157029cfd6bSTejun Heo  *	None.
6158029cfd6bSTejun Heo  */
6159029cfd6bSTejun Heo static void ata_finalize_port_ops(struct ata_port_operations *ops)
6160029cfd6bSTejun Heo {
61612da67659SPradeep Singh Rautela 	static DEFINE_SPINLOCK(lock);
6162029cfd6bSTejun Heo 	const struct ata_port_operations *cur;
6163029cfd6bSTejun Heo 	void **begin = (void **)ops;
6164029cfd6bSTejun Heo 	void **end = (void **)&ops->inherits;
6165029cfd6bSTejun Heo 	void **pp;
6166029cfd6bSTejun Heo 
6167029cfd6bSTejun Heo 	if (!ops || !ops->inherits)
6168029cfd6bSTejun Heo 		return;
6169029cfd6bSTejun Heo 
6170029cfd6bSTejun Heo 	spin_lock(&lock);
6171029cfd6bSTejun Heo 
6172029cfd6bSTejun Heo 	for (cur = ops->inherits; cur; cur = cur->inherits) {
6173029cfd6bSTejun Heo 		void **inherit = (void **)cur;
6174029cfd6bSTejun Heo 
6175029cfd6bSTejun Heo 		for (pp = begin; pp < end; pp++, inherit++)
6176029cfd6bSTejun Heo 			if (!*pp)
6177029cfd6bSTejun Heo 				*pp = *inherit;
6178029cfd6bSTejun Heo 	}
6179029cfd6bSTejun Heo 
6180029cfd6bSTejun Heo 	for (pp = begin; pp < end; pp++)
6181029cfd6bSTejun Heo 		if (IS_ERR(*pp))
6182029cfd6bSTejun Heo 			*pp = NULL;
6183029cfd6bSTejun Heo 
6184029cfd6bSTejun Heo 	ops->inherits = NULL;
6185029cfd6bSTejun Heo 
6186029cfd6bSTejun Heo 	spin_unlock(&lock);
6187029cfd6bSTejun Heo }
6188029cfd6bSTejun Heo 
6189029cfd6bSTejun Heo /**
6190ecef7253STejun Heo  *	ata_host_start - start and freeze ports of an ATA host
6191ecef7253STejun Heo  *	@host: ATA host to start ports for
6192ecef7253STejun Heo  *
6193ecef7253STejun Heo  *	Start and then freeze ports of @host.  Started status is
6194ecef7253STejun Heo  *	recorded in host->flags, so this function can be called
6195ecef7253STejun Heo  *	multiple times.  Ports are guaranteed to get started only
6196f3187195STejun Heo  *	once.  If host->ops isn't initialized yet, its set to the
6197f3187195STejun Heo  *	first non-dummy port ops.
6198ecef7253STejun Heo  *
6199ecef7253STejun Heo  *	LOCKING:
6200ecef7253STejun Heo  *	Inherited from calling layer (may sleep).
6201ecef7253STejun Heo  *
6202ecef7253STejun Heo  *	RETURNS:
6203ecef7253STejun Heo  *	0 if all ports are started successfully, -errno otherwise.
6204ecef7253STejun Heo  */
6205ecef7253STejun Heo int ata_host_start(struct ata_host *host)
6206ecef7253STejun Heo {
620732ebbc0cSTejun Heo 	int have_stop = 0;
620832ebbc0cSTejun Heo 	void *start_dr = NULL;
6209ecef7253STejun Heo 	int i, rc;
6210ecef7253STejun Heo 
6211ecef7253STejun Heo 	if (host->flags & ATA_HOST_STARTED)
6212ecef7253STejun Heo 		return 0;
6213ecef7253STejun Heo 
6214029cfd6bSTejun Heo 	ata_finalize_port_ops(host->ops);
6215029cfd6bSTejun Heo 
6216ecef7253STejun Heo 	for (i = 0; i < host->n_ports; i++) {
6217ecef7253STejun Heo 		struct ata_port *ap = host->ports[i];
6218ecef7253STejun Heo 
6219029cfd6bSTejun Heo 		ata_finalize_port_ops(ap->ops);
6220029cfd6bSTejun Heo 
6221f3187195STejun Heo 		if (!host->ops && !ata_port_is_dummy(ap))
6222f3187195STejun Heo 			host->ops = ap->ops;
6223f3187195STejun Heo 
622432ebbc0cSTejun Heo 		if (ap->ops->port_stop)
622532ebbc0cSTejun Heo 			have_stop = 1;
622632ebbc0cSTejun Heo 	}
622732ebbc0cSTejun Heo 
622832ebbc0cSTejun Heo 	if (host->ops->host_stop)
622932ebbc0cSTejun Heo 		have_stop = 1;
623032ebbc0cSTejun Heo 
623132ebbc0cSTejun Heo 	if (have_stop) {
623232ebbc0cSTejun Heo 		start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
623332ebbc0cSTejun Heo 		if (!start_dr)
623432ebbc0cSTejun Heo 			return -ENOMEM;
623532ebbc0cSTejun Heo 	}
623632ebbc0cSTejun Heo 
623732ebbc0cSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
623832ebbc0cSTejun Heo 		struct ata_port *ap = host->ports[i];
623932ebbc0cSTejun Heo 
6240ecef7253STejun Heo 		if (ap->ops->port_start) {
6241ecef7253STejun Heo 			rc = ap->ops->port_start(ap);
6242ecef7253STejun Heo 			if (rc) {
62430f9fe9b7SAlan Cox 				if (rc != -ENODEV)
6244a44fec1fSJoe Perches 					dev_err(host->dev,
6245a44fec1fSJoe Perches 						"failed to start port %d (errno=%d)\n",
6246a44fec1fSJoe Perches 						i, rc);
6247ecef7253STejun Heo 				goto err_out;
6248ecef7253STejun Heo 			}
6249ecef7253STejun Heo 		}
6250ecef7253STejun Heo 		ata_eh_freeze_port(ap);
6251ecef7253STejun Heo 	}
6252ecef7253STejun Heo 
625332ebbc0cSTejun Heo 	if (start_dr)
625432ebbc0cSTejun Heo 		devres_add(host->dev, start_dr);
6255ecef7253STejun Heo 	host->flags |= ATA_HOST_STARTED;
6256ecef7253STejun Heo 	return 0;
6257ecef7253STejun Heo 
6258ecef7253STejun Heo  err_out:
6259ecef7253STejun Heo 	while (--i >= 0) {
6260ecef7253STejun Heo 		struct ata_port *ap = host->ports[i];
6261ecef7253STejun Heo 
6262ecef7253STejun Heo 		if (ap->ops->port_stop)
6263ecef7253STejun Heo 			ap->ops->port_stop(ap);
6264ecef7253STejun Heo 	}
626532ebbc0cSTejun Heo 	devres_free(start_dr);
6266ecef7253STejun Heo 	return rc;
6267ecef7253STejun Heo }
6268ecef7253STejun Heo 
6269ecef7253STejun Heo /**
62708d8e7d13SDan Williams  *	ata_sas_host_init - Initialize a host struct for sas (ipr, libsas)
6271cca3974eSJeff Garzik  *	@host:	host to initialize
6272cca3974eSJeff Garzik  *	@dev:	device host is attached to
6273c6fd2807SJeff Garzik  *	@ops:	port_ops
6274c6fd2807SJeff Garzik  *
6275c6fd2807SJeff Garzik  */
6276cca3974eSJeff Garzik void ata_host_init(struct ata_host *host, struct device *dev,
62778d8e7d13SDan Williams 		   struct ata_port_operations *ops)
6278c6fd2807SJeff Garzik {
6279cca3974eSJeff Garzik 	spin_lock_init(&host->lock);
6280c0c362b6STejun Heo 	mutex_init(&host->eh_mutex);
62811a112d10STejun Heo 	host->n_tags = ATA_MAX_QUEUE - 1;
6282cca3974eSJeff Garzik 	host->dev = dev;
6283cca3974eSJeff Garzik 	host->ops = ops;
6284c6fd2807SJeff Garzik }
6285c6fd2807SJeff Garzik 
62869508a66fSDan Williams void __ata_port_probe(struct ata_port *ap)
628779318057SArjan van de Ven {
628879318057SArjan van de Ven 	struct ata_eh_info *ehi = &ap->link.eh_info;
628979318057SArjan van de Ven 	unsigned long flags;
629079318057SArjan van de Ven 
629179318057SArjan van de Ven 	/* kick EH for boot probing */
629279318057SArjan van de Ven 	spin_lock_irqsave(ap->lock, flags);
629379318057SArjan van de Ven 
629479318057SArjan van de Ven 	ehi->probe_mask |= ATA_ALL_DEVICES;
62956b7ae954STejun Heo 	ehi->action |= ATA_EH_RESET;
629679318057SArjan van de Ven 	ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
629779318057SArjan van de Ven 
629879318057SArjan van de Ven 	ap->pflags &= ~ATA_PFLAG_INITIALIZING;
629979318057SArjan van de Ven 	ap->pflags |= ATA_PFLAG_LOADING;
630079318057SArjan van de Ven 	ata_port_schedule_eh(ap);
630179318057SArjan van de Ven 
630279318057SArjan van de Ven 	spin_unlock_irqrestore(ap->lock, flags);
63039508a66fSDan Williams }
630479318057SArjan van de Ven 
63059508a66fSDan Williams int ata_port_probe(struct ata_port *ap)
63069508a66fSDan Williams {
63079508a66fSDan Williams 	int rc = 0;
63089508a66fSDan Williams 
63099508a66fSDan Williams 	if (ap->ops->error_handler) {
63109508a66fSDan Williams 		__ata_port_probe(ap);
631179318057SArjan van de Ven 		ata_port_wait_eh(ap);
631279318057SArjan van de Ven 	} else {
631379318057SArjan van de Ven 		DPRINTK("ata%u: bus probe begin\n", ap->print_id);
631479318057SArjan van de Ven 		rc = ata_bus_probe(ap);
631579318057SArjan van de Ven 		DPRINTK("ata%u: bus probe end\n", ap->print_id);
6316238c9cf9SJames Bottomley 	}
6317238c9cf9SJames Bottomley 	return rc;
6318238c9cf9SJames Bottomley }
631979318057SArjan van de Ven 
6320238c9cf9SJames Bottomley 
6321238c9cf9SJames Bottomley static void async_port_probe(void *data, async_cookie_t cookie)
6322238c9cf9SJames Bottomley {
6323238c9cf9SJames Bottomley 	struct ata_port *ap = data;
6324238c9cf9SJames Bottomley 
6325238c9cf9SJames Bottomley 	/*
6326238c9cf9SJames Bottomley 	 * If we're not allowed to scan this host in parallel,
6327238c9cf9SJames Bottomley 	 * we need to wait until all previous scans have completed
6328238c9cf9SJames Bottomley 	 * before going further.
6329238c9cf9SJames Bottomley 	 * Jeff Garzik says this is only within a controller, so we
6330238c9cf9SJames Bottomley 	 * don't need to wait for port 0, only for later ports.
633179318057SArjan van de Ven 	 */
6332238c9cf9SJames Bottomley 	if (!(ap->host->flags & ATA_HOST_PARALLEL_SCAN) && ap->port_no != 0)
6333238c9cf9SJames Bottomley 		async_synchronize_cookie(cookie);
6334238c9cf9SJames Bottomley 
6335238c9cf9SJames Bottomley 	(void)ata_port_probe(ap);
6336f29d3b23SArjan van de Ven 
6337f29d3b23SArjan van de Ven 	/* in order to keep device order, we need to synchronize at this point */
6338f29d3b23SArjan van de Ven 	async_synchronize_cookie(cookie);
6339f29d3b23SArjan van de Ven 
6340f29d3b23SArjan van de Ven 	ata_scsi_scan_host(ap, 1);
634179318057SArjan van de Ven }
6342238c9cf9SJames Bottomley 
6343c6fd2807SJeff Garzik /**
6344f3187195STejun Heo  *	ata_host_register - register initialized ATA host
6345f3187195STejun Heo  *	@host: ATA host to register
6346f3187195STejun Heo  *	@sht: template for SCSI host
6347c6fd2807SJeff Garzik  *
6348f3187195STejun Heo  *	Register initialized ATA host.  @host is allocated using
6349f3187195STejun Heo  *	ata_host_alloc() and fully initialized by LLD.  This function
6350f3187195STejun Heo  *	starts ports, registers @host with ATA and SCSI layers and
6351f3187195STejun Heo  *	probe registered devices.
6352c6fd2807SJeff Garzik  *
6353c6fd2807SJeff Garzik  *	LOCKING:
6354f3187195STejun Heo  *	Inherited from calling layer (may sleep).
6355c6fd2807SJeff Garzik  *
6356c6fd2807SJeff Garzik  *	RETURNS:
6357f3187195STejun Heo  *	0 on success, -errno otherwise.
6358c6fd2807SJeff Garzik  */
6359f3187195STejun Heo int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
6360c6fd2807SJeff Garzik {
6361f3187195STejun Heo 	int i, rc;
6362c6fd2807SJeff Garzik 
63631a112d10STejun Heo 	host->n_tags = clamp(sht->can_queue, 1, ATA_MAX_QUEUE - 1);
63641871ee13SKevin Hao 
6365f3187195STejun Heo 	/* host must have been started */
6366f3187195STejun Heo 	if (!(host->flags & ATA_HOST_STARTED)) {
6367a44fec1fSJoe Perches 		dev_err(host->dev, "BUG: trying to register unstarted host\n");
6368f3187195STejun Heo 		WARN_ON(1);
6369f3187195STejun Heo 		return -EINVAL;
637002f076aaSAlan Cox 	}
6371f0d36efdSTejun Heo 
6372f3187195STejun Heo 	/* Blow away unused ports.  This happens when LLD can't
6373f3187195STejun Heo 	 * determine the exact number of ports to allocate at
6374f3187195STejun Heo 	 * allocation time.
6375f3187195STejun Heo 	 */
6376f3187195STejun Heo 	for (i = host->n_ports; host->ports[i]; i++)
6377f3187195STejun Heo 		kfree(host->ports[i]);
6378f0d36efdSTejun Heo 
6379f3187195STejun Heo 	/* give ports names and add SCSI hosts */
6380e628dc99SDavid Milburn 	for (i = 0; i < host->n_ports; i++) {
638185d6725bSDan Williams 		host->ports[i]->print_id = atomic_inc_return(&ata_print_id);
6382e628dc99SDavid Milburn 		host->ports[i]->local_port_no = i + 1;
6383e628dc99SDavid Milburn 	}
6384d9027470SGwendal Grignou 
6385d9027470SGwendal Grignou 	/* Create associated sysfs transport objects  */
6386d9027470SGwendal Grignou 	for (i = 0; i < host->n_ports; i++) {
6387d9027470SGwendal Grignou 		rc = ata_tport_add(host->dev,host->ports[i]);
6388d9027470SGwendal Grignou 		if (rc) {
6389d9027470SGwendal Grignou 			goto err_tadd;
6390d9027470SGwendal Grignou 		}
6391d9027470SGwendal Grignou 	}
6392d9027470SGwendal Grignou 
6393f3187195STejun Heo 	rc = ata_scsi_add_hosts(host, sht);
6394ecef7253STejun Heo 	if (rc)
6395d9027470SGwendal Grignou 		goto err_tadd;
6396ecef7253STejun Heo 
6397f3187195STejun Heo 	/* set cable, sata_spd_limit and report */
6398cca3974eSJeff Garzik 	for (i = 0; i < host->n_ports; i++) {
6399cca3974eSJeff Garzik 		struct ata_port *ap = host->ports[i];
6400f3187195STejun Heo 		unsigned long xfer_mask;
6401f3187195STejun Heo 
6402f3187195STejun Heo 		/* set SATA cable type if still unset */
6403f3187195STejun Heo 		if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
6404f3187195STejun Heo 			ap->cbl = ATA_CBL_SATA;
6405c6fd2807SJeff Garzik 
6406c6fd2807SJeff Garzik 		/* init sata_spd_limit to the current value */
64074fb37a25STejun Heo 		sata_link_init_spd(&ap->link);
6408b1c72916STejun Heo 		if (ap->slave_link)
6409b1c72916STejun Heo 			sata_link_init_spd(ap->slave_link);
6410c6fd2807SJeff Garzik 
6411cbcdd875STejun Heo 		/* print per-port info to dmesg */
6412f3187195STejun Heo 		xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
6413f3187195STejun Heo 					      ap->udma_mask);
6414f3187195STejun Heo 
6415abf6e8edSTejun Heo 		if (!ata_port_is_dummy(ap)) {
6416a9a79dfeSJoe Perches 			ata_port_info(ap, "%cATA max %s %s\n",
6417a16abc0bSTejun Heo 				      (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
6418f3187195STejun Heo 				      ata_mode_string(xfer_mask),
6419cbcdd875STejun Heo 				      ap->link.eh_info.desc);
6420abf6e8edSTejun Heo 			ata_ehi_clear_desc(&ap->link.eh_info);
6421abf6e8edSTejun Heo 		} else
6422a9a79dfeSJoe Perches 			ata_port_info(ap, "DUMMY\n");
6423c6fd2807SJeff Garzik 	}
6424c6fd2807SJeff Garzik 
6425f6005354SVegard Nossum 	/* perform each probe asynchronously */
6426f3187195STejun Heo 	for (i = 0; i < host->n_ports; i++) {
6427f3187195STejun Heo 		struct ata_port *ap = host->ports[i];
642879318057SArjan van de Ven 		async_schedule(async_port_probe, ap);
6429c6fd2807SJeff Garzik 	}
6430c6fd2807SJeff Garzik 
6431f3187195STejun Heo 	return 0;
6432d9027470SGwendal Grignou 
6433d9027470SGwendal Grignou  err_tadd:
6434d9027470SGwendal Grignou 	while (--i >= 0) {
6435d9027470SGwendal Grignou 		ata_tport_delete(host->ports[i]);
6436d9027470SGwendal Grignou 	}
6437d9027470SGwendal Grignou 	return rc;
6438d9027470SGwendal Grignou 
6439f3187195STejun Heo }
6440f3187195STejun Heo 
6441f3187195STejun Heo /**
6442f5cda257STejun Heo  *	ata_host_activate - start host, request IRQ and register it
6443f5cda257STejun Heo  *	@host: target ATA host
6444f5cda257STejun Heo  *	@irq: IRQ to request
6445f5cda257STejun Heo  *	@irq_handler: irq_handler used when requesting IRQ
6446f5cda257STejun Heo  *	@irq_flags: irq_flags used when requesting IRQ
6447f5cda257STejun Heo  *	@sht: scsi_host_template to use when registering the host
6448f5cda257STejun Heo  *
6449f5cda257STejun Heo  *	After allocating an ATA host and initializing it, most libata
6450f5cda257STejun Heo  *	LLDs perform three steps to activate the host - start host,
6451c9b5560aSMasanari Iida  *	request IRQ and register it.  This helper takes necessary
6452f5cda257STejun Heo  *	arguments and performs the three steps in one go.
6453f5cda257STejun Heo  *
64543d46b2e2SPaul Mundt  *	An invalid IRQ skips the IRQ registration and expects the host to
64553d46b2e2SPaul Mundt  *	have set polling mode on the port. In this case, @irq_handler
64563d46b2e2SPaul Mundt  *	should be NULL.
64573d46b2e2SPaul Mundt  *
6458f5cda257STejun Heo  *	LOCKING:
6459f5cda257STejun Heo  *	Inherited from calling layer (may sleep).
6460f5cda257STejun Heo  *
6461f5cda257STejun Heo  *	RETURNS:
6462f5cda257STejun Heo  *	0 on success, -errno otherwise.
6463f5cda257STejun Heo  */
6464f5cda257STejun Heo int ata_host_activate(struct ata_host *host, int irq,
6465f5cda257STejun Heo 		      irq_handler_t irq_handler, unsigned long irq_flags,
6466f5cda257STejun Heo 		      struct scsi_host_template *sht)
6467f5cda257STejun Heo {
6468cbcdd875STejun Heo 	int i, rc;
64697e22c002SHeiner Kallweit 	char *irq_desc;
6470f5cda257STejun Heo 
6471f5cda257STejun Heo 	rc = ata_host_start(host);
6472f5cda257STejun Heo 	if (rc)
6473f5cda257STejun Heo 		return rc;
6474f5cda257STejun Heo 
64753d46b2e2SPaul Mundt 	/* Special case for polling mode */
64763d46b2e2SPaul Mundt 	if (!irq) {
64773d46b2e2SPaul Mundt 		WARN_ON(irq_handler);
64783d46b2e2SPaul Mundt 		return ata_host_register(host, sht);
64793d46b2e2SPaul Mundt 	}
64803d46b2e2SPaul Mundt 
64817e22c002SHeiner Kallweit 	irq_desc = devm_kasprintf(host->dev, GFP_KERNEL, "%s[%s]",
64827e22c002SHeiner Kallweit 				  dev_driver_string(host->dev),
64837e22c002SHeiner Kallweit 				  dev_name(host->dev));
64847e22c002SHeiner Kallweit 	if (!irq_desc)
64857e22c002SHeiner Kallweit 		return -ENOMEM;
64867e22c002SHeiner Kallweit 
6487f5cda257STejun Heo 	rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
64887e22c002SHeiner Kallweit 			      irq_desc, host);
6489f5cda257STejun Heo 	if (rc)
6490f5cda257STejun Heo 		return rc;
6491f5cda257STejun Heo 
6492cbcdd875STejun Heo 	for (i = 0; i < host->n_ports; i++)
6493cbcdd875STejun Heo 		ata_port_desc(host->ports[i], "irq %d", irq);
64944031826bSTejun Heo 
6495f5cda257STejun Heo 	rc = ata_host_register(host, sht);
6496f5cda257STejun Heo 	/* if failed, just free the IRQ and leave ports alone */
6497f5cda257STejun Heo 	if (rc)
6498f5cda257STejun Heo 		devm_free_irq(host->dev, irq, host);
6499f5cda257STejun Heo 
6500f5cda257STejun Heo 	return rc;
6501f5cda257STejun Heo }
6502f5cda257STejun Heo 
6503f5cda257STejun Heo /**
6504c9b5560aSMasanari Iida  *	ata_port_detach - Detach ATA port in preparation of device removal
6505c6fd2807SJeff Garzik  *	@ap: ATA port to be detached
6506c6fd2807SJeff Garzik  *
6507c6fd2807SJeff Garzik  *	Detach all ATA devices and the associated SCSI devices of @ap;
6508c6fd2807SJeff Garzik  *	then, remove the associated SCSI host.  @ap is guaranteed to
6509c6fd2807SJeff Garzik  *	be quiescent on return from this function.
6510c6fd2807SJeff Garzik  *
6511c6fd2807SJeff Garzik  *	LOCKING:
6512c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
6513c6fd2807SJeff Garzik  */
6514741b7763SAdrian Bunk static void ata_port_detach(struct ata_port *ap)
6515c6fd2807SJeff Garzik {
6516c6fd2807SJeff Garzik 	unsigned long flags;
6517a6f9bf4dSLevente Kurusa 	struct ata_link *link;
6518a6f9bf4dSLevente Kurusa 	struct ata_device *dev;
6519c6fd2807SJeff Garzik 
6520c6fd2807SJeff Garzik 	if (!ap->ops->error_handler)
6521c6fd2807SJeff Garzik 		goto skip_eh;
6522c6fd2807SJeff Garzik 
6523c6fd2807SJeff Garzik 	/* tell EH we're leaving & flush EH */
6524c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
6525c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_UNLOADING;
6526ece180d1STejun Heo 	ata_port_schedule_eh(ap);
6527c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
6528c6fd2807SJeff Garzik 
6529ece180d1STejun Heo 	/* wait till EH commits suicide */
6530c6fd2807SJeff Garzik 	ata_port_wait_eh(ap);
6531c6fd2807SJeff Garzik 
6532ece180d1STejun Heo 	/* it better be dead now */
6533ece180d1STejun Heo 	WARN_ON(!(ap->pflags & ATA_PFLAG_UNLOADED));
6534c6fd2807SJeff Garzik 
6535afe2c511STejun Heo 	cancel_delayed_work_sync(&ap->hotplug_task);
6536c6fd2807SJeff Garzik 
6537c6fd2807SJeff Garzik  skip_eh:
6538a6f9bf4dSLevente Kurusa 	/* clean up zpodd on port removal */
6539a6f9bf4dSLevente Kurusa 	ata_for_each_link(link, ap, HOST_FIRST) {
6540a6f9bf4dSLevente Kurusa 		ata_for_each_dev(dev, link, ALL) {
6541a6f9bf4dSLevente Kurusa 			if (zpodd_dev_enabled(dev))
6542a6f9bf4dSLevente Kurusa 				zpodd_exit(dev);
6543a6f9bf4dSLevente Kurusa 		}
6544a6f9bf4dSLevente Kurusa 	}
6545d9027470SGwendal Grignou 	if (ap->pmp_link) {
6546d9027470SGwendal Grignou 		int i;
6547d9027470SGwendal Grignou 		for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
6548d9027470SGwendal Grignou 			ata_tlink_delete(&ap->pmp_link[i]);
6549d9027470SGwendal Grignou 	}
6550c6fd2807SJeff Garzik 	/* remove the associated SCSI host */
6551cca3974eSJeff Garzik 	scsi_remove_host(ap->scsi_host);
6552c5700766SRafael J. Wysocki 	ata_tport_delete(ap);
6553c6fd2807SJeff Garzik }
6554c6fd2807SJeff Garzik 
6555c6fd2807SJeff Garzik /**
65560529c159STejun Heo  *	ata_host_detach - Detach all ports of an ATA host
65570529c159STejun Heo  *	@host: Host to detach
65580529c159STejun Heo  *
65590529c159STejun Heo  *	Detach all ports of @host.
65600529c159STejun Heo  *
65610529c159STejun Heo  *	LOCKING:
65620529c159STejun Heo  *	Kernel thread context (may sleep).
65630529c159STejun Heo  */
65640529c159STejun Heo void ata_host_detach(struct ata_host *host)
65650529c159STejun Heo {
65660529c159STejun Heo 	int i;
65670529c159STejun Heo 
65680529c159STejun Heo 	for (i = 0; i < host->n_ports; i++)
65690529c159STejun Heo 		ata_port_detach(host->ports[i]);
6570562f0c2dSTejun Heo 
6571562f0c2dSTejun Heo 	/* the host is dead now, dissociate ACPI */
6572562f0c2dSTejun Heo 	ata_acpi_dissociate(host);
65730529c159STejun Heo }
65740529c159STejun Heo 
6575c6fd2807SJeff Garzik #ifdef CONFIG_PCI
6576c6fd2807SJeff Garzik 
6577c6fd2807SJeff Garzik /**
6578c6fd2807SJeff Garzik  *	ata_pci_remove_one - PCI layer callback for device removal
6579c6fd2807SJeff Garzik  *	@pdev: PCI device that was removed
6580c6fd2807SJeff Garzik  *
6581b878ca5dSTejun Heo  *	PCI layer indicates to libata via this hook that hot-unplug or
6582b878ca5dSTejun Heo  *	module unload event has occurred.  Detach all ports.  Resource
6583b878ca5dSTejun Heo  *	release is handled via devres.
6584c6fd2807SJeff Garzik  *
6585c6fd2807SJeff Garzik  *	LOCKING:
6586c6fd2807SJeff Garzik  *	Inherited from PCI layer (may sleep).
6587c6fd2807SJeff Garzik  */
6588c6fd2807SJeff Garzik void ata_pci_remove_one(struct pci_dev *pdev)
6589c6fd2807SJeff Garzik {
659004a3f5b7SBrian Norris 	struct ata_host *host = pci_get_drvdata(pdev);
6591c6fd2807SJeff Garzik 
6592f0d36efdSTejun Heo 	ata_host_detach(host);
6593c6fd2807SJeff Garzik }
6594c6fd2807SJeff Garzik 
6595c6fd2807SJeff Garzik /* move to PCI subsystem */
6596c6fd2807SJeff Garzik int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
6597c6fd2807SJeff Garzik {
6598c6fd2807SJeff Garzik 	unsigned long tmp = 0;
6599c6fd2807SJeff Garzik 
6600c6fd2807SJeff Garzik 	switch (bits->width) {
6601c6fd2807SJeff Garzik 	case 1: {
6602c6fd2807SJeff Garzik 		u8 tmp8 = 0;
6603c6fd2807SJeff Garzik 		pci_read_config_byte(pdev, bits->reg, &tmp8);
6604c6fd2807SJeff Garzik 		tmp = tmp8;
6605c6fd2807SJeff Garzik 		break;
6606c6fd2807SJeff Garzik 	}
6607c6fd2807SJeff Garzik 	case 2: {
6608c6fd2807SJeff Garzik 		u16 tmp16 = 0;
6609c6fd2807SJeff Garzik 		pci_read_config_word(pdev, bits->reg, &tmp16);
6610c6fd2807SJeff Garzik 		tmp = tmp16;
6611c6fd2807SJeff Garzik 		break;
6612c6fd2807SJeff Garzik 	}
6613c6fd2807SJeff Garzik 	case 4: {
6614c6fd2807SJeff Garzik 		u32 tmp32 = 0;
6615c6fd2807SJeff Garzik 		pci_read_config_dword(pdev, bits->reg, &tmp32);
6616c6fd2807SJeff Garzik 		tmp = tmp32;
6617c6fd2807SJeff Garzik 		break;
6618c6fd2807SJeff Garzik 	}
6619c6fd2807SJeff Garzik 
6620c6fd2807SJeff Garzik 	default:
6621c6fd2807SJeff Garzik 		return -EINVAL;
6622c6fd2807SJeff Garzik 	}
6623c6fd2807SJeff Garzik 
6624c6fd2807SJeff Garzik 	tmp &= bits->mask;
6625c6fd2807SJeff Garzik 
6626c6fd2807SJeff Garzik 	return (tmp == bits->val) ? 1 : 0;
6627c6fd2807SJeff Garzik }
6628c6fd2807SJeff Garzik 
66296ffa01d8STejun Heo #ifdef CONFIG_PM
6630c6fd2807SJeff Garzik void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
6631c6fd2807SJeff Garzik {
6632c6fd2807SJeff Garzik 	pci_save_state(pdev);
6633c6fd2807SJeff Garzik 	pci_disable_device(pdev);
66344c90d971STejun Heo 
66353a2d5b70SRafael J. Wysocki 	if (mesg.event & PM_EVENT_SLEEP)
6636c6fd2807SJeff Garzik 		pci_set_power_state(pdev, PCI_D3hot);
6637c6fd2807SJeff Garzik }
6638c6fd2807SJeff Garzik 
6639553c4aa6STejun Heo int ata_pci_device_do_resume(struct pci_dev *pdev)
6640c6fd2807SJeff Garzik {
6641553c4aa6STejun Heo 	int rc;
6642553c4aa6STejun Heo 
6643c6fd2807SJeff Garzik 	pci_set_power_state(pdev, PCI_D0);
6644c6fd2807SJeff Garzik 	pci_restore_state(pdev);
6645553c4aa6STejun Heo 
6646f0d36efdSTejun Heo 	rc = pcim_enable_device(pdev);
6647553c4aa6STejun Heo 	if (rc) {
6648a44fec1fSJoe Perches 		dev_err(&pdev->dev,
6649553c4aa6STejun Heo 			"failed to enable device after resume (%d)\n", rc);
6650553c4aa6STejun Heo 		return rc;
6651553c4aa6STejun Heo 	}
6652553c4aa6STejun Heo 
6653c6fd2807SJeff Garzik 	pci_set_master(pdev);
6654553c4aa6STejun Heo 	return 0;
6655c6fd2807SJeff Garzik }
6656c6fd2807SJeff Garzik 
6657c6fd2807SJeff Garzik int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
6658c6fd2807SJeff Garzik {
665904a3f5b7SBrian Norris 	struct ata_host *host = pci_get_drvdata(pdev);
6660c6fd2807SJeff Garzik 	int rc = 0;
6661c6fd2807SJeff Garzik 
6662cca3974eSJeff Garzik 	rc = ata_host_suspend(host, mesg);
6663c6fd2807SJeff Garzik 	if (rc)
6664c6fd2807SJeff Garzik 		return rc;
6665c6fd2807SJeff Garzik 
6666c6fd2807SJeff Garzik 	ata_pci_device_do_suspend(pdev, mesg);
6667c6fd2807SJeff Garzik 
6668c6fd2807SJeff Garzik 	return 0;
6669c6fd2807SJeff Garzik }
6670c6fd2807SJeff Garzik 
6671c6fd2807SJeff Garzik int ata_pci_device_resume(struct pci_dev *pdev)
6672c6fd2807SJeff Garzik {
667304a3f5b7SBrian Norris 	struct ata_host *host = pci_get_drvdata(pdev);
6674553c4aa6STejun Heo 	int rc;
6675c6fd2807SJeff Garzik 
6676553c4aa6STejun Heo 	rc = ata_pci_device_do_resume(pdev);
6677553c4aa6STejun Heo 	if (rc == 0)
6678cca3974eSJeff Garzik 		ata_host_resume(host);
6679553c4aa6STejun Heo 	return rc;
6680c6fd2807SJeff Garzik }
66816ffa01d8STejun Heo #endif /* CONFIG_PM */
66826ffa01d8STejun Heo 
6683c6fd2807SJeff Garzik #endif /* CONFIG_PCI */
6684c6fd2807SJeff Garzik 
6685b7db04d9SBrian Norris /**
6686b7db04d9SBrian Norris  *	ata_platform_remove_one - Platform layer callback for device removal
6687b7db04d9SBrian Norris  *	@pdev: Platform device that was removed
6688b7db04d9SBrian Norris  *
6689b7db04d9SBrian Norris  *	Platform layer indicates to libata via this hook that hot-unplug or
6690b7db04d9SBrian Norris  *	module unload event has occurred.  Detach all ports.  Resource
6691b7db04d9SBrian Norris  *	release is handled via devres.
6692b7db04d9SBrian Norris  *
6693b7db04d9SBrian Norris  *	LOCKING:
6694b7db04d9SBrian Norris  *	Inherited from platform layer (may sleep).
6695b7db04d9SBrian Norris  */
6696b7db04d9SBrian Norris int ata_platform_remove_one(struct platform_device *pdev)
6697b7db04d9SBrian Norris {
6698b7db04d9SBrian Norris 	struct ata_host *host = platform_get_drvdata(pdev);
6699b7db04d9SBrian Norris 
6700b7db04d9SBrian Norris 	ata_host_detach(host);
6701b7db04d9SBrian Norris 
6702b7db04d9SBrian Norris 	return 0;
6703b7db04d9SBrian Norris }
6704b7db04d9SBrian Norris 
670533267325STejun Heo static int __init ata_parse_force_one(char **cur,
670633267325STejun Heo 				      struct ata_force_ent *force_ent,
670733267325STejun Heo 				      const char **reason)
670833267325STejun Heo {
67090f5f264bSRasmus Villemoes 	static const struct ata_force_param force_tbl[] __initconst = {
671033267325STejun Heo 		{ "40c",	.cbl		= ATA_CBL_PATA40 },
671133267325STejun Heo 		{ "80c",	.cbl		= ATA_CBL_PATA80 },
671233267325STejun Heo 		{ "short40c",	.cbl		= ATA_CBL_PATA40_SHORT },
671333267325STejun Heo 		{ "unk",	.cbl		= ATA_CBL_PATA_UNK },
671433267325STejun Heo 		{ "ign",	.cbl		= ATA_CBL_PATA_IGN },
671533267325STejun Heo 		{ "sata",	.cbl		= ATA_CBL_SATA },
671633267325STejun Heo 		{ "1.5Gbps",	.spd_limit	= 1 },
671733267325STejun Heo 		{ "3.0Gbps",	.spd_limit	= 2 },
671833267325STejun Heo 		{ "noncq",	.horkage_on	= ATA_HORKAGE_NONCQ },
671933267325STejun Heo 		{ "ncq",	.horkage_off	= ATA_HORKAGE_NONCQ },
6720d7b16e4fSMartin K. Petersen 		{ "noncqtrim",	.horkage_on	= ATA_HORKAGE_NO_NCQ_TRIM },
6721d7b16e4fSMartin K. Petersen 		{ "ncqtrim",	.horkage_off	= ATA_HORKAGE_NO_NCQ_TRIM },
672243c9c591STejun Heo 		{ "dump_id",	.horkage_on	= ATA_HORKAGE_DUMP_ID },
672333267325STejun Heo 		{ "pio0",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 0) },
672433267325STejun Heo 		{ "pio1",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 1) },
672533267325STejun Heo 		{ "pio2",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 2) },
672633267325STejun Heo 		{ "pio3",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 3) },
672733267325STejun Heo 		{ "pio4",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 4) },
672833267325STejun Heo 		{ "pio5",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 5) },
672933267325STejun Heo 		{ "pio6",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 6) },
673033267325STejun Heo 		{ "mwdma0",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 0) },
673133267325STejun Heo 		{ "mwdma1",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 1) },
673233267325STejun Heo 		{ "mwdma2",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 2) },
673333267325STejun Heo 		{ "mwdma3",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 3) },
673433267325STejun Heo 		{ "mwdma4",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 4) },
673533267325STejun Heo 		{ "udma0",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
673633267325STejun Heo 		{ "udma16",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
673733267325STejun Heo 		{ "udma/16",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
673833267325STejun Heo 		{ "udma1",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
673933267325STejun Heo 		{ "udma25",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
674033267325STejun Heo 		{ "udma/25",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
674133267325STejun Heo 		{ "udma2",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
674233267325STejun Heo 		{ "udma33",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
674333267325STejun Heo 		{ "udma/33",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
674433267325STejun Heo 		{ "udma3",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
674533267325STejun Heo 		{ "udma44",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
674633267325STejun Heo 		{ "udma/44",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
674733267325STejun Heo 		{ "udma4",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
674833267325STejun Heo 		{ "udma66",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
674933267325STejun Heo 		{ "udma/66",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
675033267325STejun Heo 		{ "udma5",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
675133267325STejun Heo 		{ "udma100",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
675233267325STejun Heo 		{ "udma/100",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
675333267325STejun Heo 		{ "udma6",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
675433267325STejun Heo 		{ "udma133",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
675533267325STejun Heo 		{ "udma/133",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
675633267325STejun Heo 		{ "udma7",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 7) },
675705944bdfSTejun Heo 		{ "nohrst",	.lflags		= ATA_LFLAG_NO_HRST },
675805944bdfSTejun Heo 		{ "nosrst",	.lflags		= ATA_LFLAG_NO_SRST },
675905944bdfSTejun Heo 		{ "norst",	.lflags		= ATA_LFLAG_NO_HRST | ATA_LFLAG_NO_SRST },
6760ca6d43b0SDan Williams 		{ "rstonce",	.lflags		= ATA_LFLAG_RST_ONCE },
6761966fbe19SVincent Pelletier 		{ "atapi_dmadir", .horkage_on	= ATA_HORKAGE_ATAPI_DMADIR },
6762b8bd6dc3SRobin H. Johnson 		{ "disable",	.horkage_on	= ATA_HORKAGE_DISABLE },
676333267325STejun Heo 	};
676433267325STejun Heo 	char *start = *cur, *p = *cur;
676533267325STejun Heo 	char *id, *val, *endp;
676633267325STejun Heo 	const struct ata_force_param *match_fp = NULL;
676733267325STejun Heo 	int nr_matches = 0, i;
676833267325STejun Heo 
676933267325STejun Heo 	/* find where this param ends and update *cur */
677033267325STejun Heo 	while (*p != '\0' && *p != ',')
677133267325STejun Heo 		p++;
677233267325STejun Heo 
677333267325STejun Heo 	if (*p == '\0')
677433267325STejun Heo 		*cur = p;
677533267325STejun Heo 	else
677633267325STejun Heo 		*cur = p + 1;
677733267325STejun Heo 
677833267325STejun Heo 	*p = '\0';
677933267325STejun Heo 
678033267325STejun Heo 	/* parse */
678133267325STejun Heo 	p = strchr(start, ':');
678233267325STejun Heo 	if (!p) {
678333267325STejun Heo 		val = strstrip(start);
678433267325STejun Heo 		goto parse_val;
678533267325STejun Heo 	}
678633267325STejun Heo 	*p = '\0';
678733267325STejun Heo 
678833267325STejun Heo 	id = strstrip(start);
678933267325STejun Heo 	val = strstrip(p + 1);
679033267325STejun Heo 
679133267325STejun Heo 	/* parse id */
679233267325STejun Heo 	p = strchr(id, '.');
679333267325STejun Heo 	if (p) {
679433267325STejun Heo 		*p++ = '\0';
679533267325STejun Heo 		force_ent->device = simple_strtoul(p, &endp, 10);
679633267325STejun Heo 		if (p == endp || *endp != '\0') {
679733267325STejun Heo 			*reason = "invalid device";
679833267325STejun Heo 			return -EINVAL;
679933267325STejun Heo 		}
680033267325STejun Heo 	}
680133267325STejun Heo 
680233267325STejun Heo 	force_ent->port = simple_strtoul(id, &endp, 10);
680333267325STejun Heo 	if (p == endp || *endp != '\0') {
680433267325STejun Heo 		*reason = "invalid port/link";
680533267325STejun Heo 		return -EINVAL;
680633267325STejun Heo 	}
680733267325STejun Heo 
680833267325STejun Heo  parse_val:
680933267325STejun Heo 	/* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */
681033267325STejun Heo 	for (i = 0; i < ARRAY_SIZE(force_tbl); i++) {
681133267325STejun Heo 		const struct ata_force_param *fp = &force_tbl[i];
681233267325STejun Heo 
681333267325STejun Heo 		if (strncasecmp(val, fp->name, strlen(val)))
681433267325STejun Heo 			continue;
681533267325STejun Heo 
681633267325STejun Heo 		nr_matches++;
681733267325STejun Heo 		match_fp = fp;
681833267325STejun Heo 
681933267325STejun Heo 		if (strcasecmp(val, fp->name) == 0) {
682033267325STejun Heo 			nr_matches = 1;
682133267325STejun Heo 			break;
682233267325STejun Heo 		}
682333267325STejun Heo 	}
682433267325STejun Heo 
682533267325STejun Heo 	if (!nr_matches) {
682633267325STejun Heo 		*reason = "unknown value";
682733267325STejun Heo 		return -EINVAL;
682833267325STejun Heo 	}
682933267325STejun Heo 	if (nr_matches > 1) {
683033267325STejun Heo 		*reason = "ambigious value";
683133267325STejun Heo 		return -EINVAL;
683233267325STejun Heo 	}
683333267325STejun Heo 
683433267325STejun Heo 	force_ent->param = *match_fp;
683533267325STejun Heo 
683633267325STejun Heo 	return 0;
683733267325STejun Heo }
683833267325STejun Heo 
683933267325STejun Heo static void __init ata_parse_force_param(void)
684033267325STejun Heo {
684133267325STejun Heo 	int idx = 0, size = 1;
684233267325STejun Heo 	int last_port = -1, last_device = -1;
684333267325STejun Heo 	char *p, *cur, *next;
684433267325STejun Heo 
684533267325STejun Heo 	/* calculate maximum number of params and allocate force_tbl */
684633267325STejun Heo 	for (p = ata_force_param_buf; *p; p++)
684733267325STejun Heo 		if (*p == ',')
684833267325STejun Heo 			size++;
684933267325STejun Heo 
685033267325STejun Heo 	ata_force_tbl = kzalloc(sizeof(ata_force_tbl[0]) * size, GFP_KERNEL);
685133267325STejun Heo 	if (!ata_force_tbl) {
685233267325STejun Heo 		printk(KERN_WARNING "ata: failed to extend force table, "
685333267325STejun Heo 		       "libata.force ignored\n");
685433267325STejun Heo 		return;
685533267325STejun Heo 	}
685633267325STejun Heo 
685733267325STejun Heo 	/* parse and populate the table */
685833267325STejun Heo 	for (cur = ata_force_param_buf; *cur != '\0'; cur = next) {
685933267325STejun Heo 		const char *reason = "";
686033267325STejun Heo 		struct ata_force_ent te = { .port = -1, .device = -1 };
686133267325STejun Heo 
686233267325STejun Heo 		next = cur;
686333267325STejun Heo 		if (ata_parse_force_one(&next, &te, &reason)) {
686433267325STejun Heo 			printk(KERN_WARNING "ata: failed to parse force "
686533267325STejun Heo 			       "parameter \"%s\" (%s)\n",
686633267325STejun Heo 			       cur, reason);
686733267325STejun Heo 			continue;
686833267325STejun Heo 		}
686933267325STejun Heo 
687033267325STejun Heo 		if (te.port == -1) {
687133267325STejun Heo 			te.port = last_port;
687233267325STejun Heo 			te.device = last_device;
687333267325STejun Heo 		}
687433267325STejun Heo 
687533267325STejun Heo 		ata_force_tbl[idx++] = te;
687633267325STejun Heo 
687733267325STejun Heo 		last_port = te.port;
687833267325STejun Heo 		last_device = te.device;
687933267325STejun Heo 	}
688033267325STejun Heo 
688133267325STejun Heo 	ata_force_tbl_size = idx;
688233267325STejun Heo }
6883c6fd2807SJeff Garzik 
6884c6fd2807SJeff Garzik static int __init ata_init(void)
6885c6fd2807SJeff Garzik {
6886d9027470SGwendal Grignou 	int rc;
6887270390e1STejun Heo 
688833267325STejun Heo 	ata_parse_force_param();
688933267325STejun Heo 
6890270390e1STejun Heo 	rc = ata_sff_init();
6891ad72cf98STejun Heo 	if (rc) {
6892ad72cf98STejun Heo 		kfree(ata_force_tbl);
6893ad72cf98STejun Heo 		return rc;
6894ad72cf98STejun Heo 	}
6895c6fd2807SJeff Garzik 
6896d9027470SGwendal Grignou 	libata_transport_init();
6897d9027470SGwendal Grignou 	ata_scsi_transport_template = ata_attach_transport();
6898d9027470SGwendal Grignou 	if (!ata_scsi_transport_template) {
6899d9027470SGwendal Grignou 		ata_sff_exit();
6900d9027470SGwendal Grignou 		rc = -ENOMEM;
6901d9027470SGwendal Grignou 		goto err_out;
6902d9027470SGwendal Grignou 	}
6903d9027470SGwendal Grignou 
6904c6fd2807SJeff Garzik 	printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
6905c6fd2807SJeff Garzik 	return 0;
6906d9027470SGwendal Grignou 
6907d9027470SGwendal Grignou err_out:
6908d9027470SGwendal Grignou 	return rc;
6909c6fd2807SJeff Garzik }
6910c6fd2807SJeff Garzik 
6911c6fd2807SJeff Garzik static void __exit ata_exit(void)
6912c6fd2807SJeff Garzik {
6913d9027470SGwendal Grignou 	ata_release_transport(ata_scsi_transport_template);
6914d9027470SGwendal Grignou 	libata_transport_exit();
6915270390e1STejun Heo 	ata_sff_exit();
691633267325STejun Heo 	kfree(ata_force_tbl);
6917c6fd2807SJeff Garzik }
6918c6fd2807SJeff Garzik 
6919a4625085SBrian King subsys_initcall(ata_init);
6920c6fd2807SJeff Garzik module_exit(ata_exit);
6921c6fd2807SJeff Garzik 
69229990b6f3SAkinobu Mita static DEFINE_RATELIMIT_STATE(ratelimit, HZ / 5, 1);
6923c6fd2807SJeff Garzik 
6924c6fd2807SJeff Garzik int ata_ratelimit(void)
6925c6fd2807SJeff Garzik {
69269990b6f3SAkinobu Mita 	return __ratelimit(&ratelimit);
6927c6fd2807SJeff Garzik }
6928c6fd2807SJeff Garzik 
6929c0c362b6STejun Heo /**
6930c0c362b6STejun Heo  *	ata_msleep - ATA EH owner aware msleep
6931c0c362b6STejun Heo  *	@ap: ATA port to attribute the sleep to
6932c0c362b6STejun Heo  *	@msecs: duration to sleep in milliseconds
6933c0c362b6STejun Heo  *
6934c0c362b6STejun Heo  *	Sleeps @msecs.  If the current task is owner of @ap's EH, the
6935c0c362b6STejun Heo  *	ownership is released before going to sleep and reacquired
6936c0c362b6STejun Heo  *	after the sleep is complete.  IOW, other ports sharing the
6937c0c362b6STejun Heo  *	@ap->host will be allowed to own the EH while this task is
6938c0c362b6STejun Heo  *	sleeping.
6939c0c362b6STejun Heo  *
6940c0c362b6STejun Heo  *	LOCKING:
6941c0c362b6STejun Heo  *	Might sleep.
6942c0c362b6STejun Heo  */
694397750cebSTejun Heo void ata_msleep(struct ata_port *ap, unsigned int msecs)
694497750cebSTejun Heo {
6945c0c362b6STejun Heo 	bool owns_eh = ap && ap->host->eh_owner == current;
6946c0c362b6STejun Heo 
6947c0c362b6STejun Heo 	if (owns_eh)
6948c0c362b6STejun Heo 		ata_eh_release(ap);
6949c0c362b6STejun Heo 
6950848c3920SAnil Veliyankara Madam 	if (msecs < 20) {
6951848c3920SAnil Veliyankara Madam 		unsigned long usecs = msecs * USEC_PER_MSEC;
6952848c3920SAnil Veliyankara Madam 		usleep_range(usecs, usecs + 50);
6953848c3920SAnil Veliyankara Madam 	} else {
695497750cebSTejun Heo 		msleep(msecs);
6955848c3920SAnil Veliyankara Madam 	}
6956c0c362b6STejun Heo 
6957c0c362b6STejun Heo 	if (owns_eh)
6958c0c362b6STejun Heo 		ata_eh_acquire(ap);
695997750cebSTejun Heo }
696097750cebSTejun Heo 
6961c6fd2807SJeff Garzik /**
6962c6fd2807SJeff Garzik  *	ata_wait_register - wait until register value changes
696397750cebSTejun Heo  *	@ap: ATA port to wait register for, can be NULL
6964c6fd2807SJeff Garzik  *	@reg: IO-mapped register
6965c6fd2807SJeff Garzik  *	@mask: Mask to apply to read register value
6966c6fd2807SJeff Garzik  *	@val: Wait condition
6967341c2c95STejun Heo  *	@interval: polling interval in milliseconds
6968341c2c95STejun Heo  *	@timeout: timeout in milliseconds
6969c6fd2807SJeff Garzik  *
6970c6fd2807SJeff Garzik  *	Waiting for some bits of register to change is a common
6971c6fd2807SJeff Garzik  *	operation for ATA controllers.  This function reads 32bit LE
6972c6fd2807SJeff Garzik  *	IO-mapped register @reg and tests for the following condition.
6973c6fd2807SJeff Garzik  *
6974c6fd2807SJeff Garzik  *	(*@reg & mask) != val
6975c6fd2807SJeff Garzik  *
6976c6fd2807SJeff Garzik  *	If the condition is met, it returns; otherwise, the process is
6977c6fd2807SJeff Garzik  *	repeated after @interval_msec until timeout.
6978c6fd2807SJeff Garzik  *
6979c6fd2807SJeff Garzik  *	LOCKING:
6980c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
6981c6fd2807SJeff Garzik  *
6982c6fd2807SJeff Garzik  *	RETURNS:
6983c6fd2807SJeff Garzik  *	The final register value.
6984c6fd2807SJeff Garzik  */
698597750cebSTejun Heo u32 ata_wait_register(struct ata_port *ap, void __iomem *reg, u32 mask, u32 val,
6986341c2c95STejun Heo 		      unsigned long interval, unsigned long timeout)
6987c6fd2807SJeff Garzik {
6988341c2c95STejun Heo 	unsigned long deadline;
6989c6fd2807SJeff Garzik 	u32 tmp;
6990c6fd2807SJeff Garzik 
6991c6fd2807SJeff Garzik 	tmp = ioread32(reg);
6992c6fd2807SJeff Garzik 
6993c6fd2807SJeff Garzik 	/* Calculate timeout _after_ the first read to make sure
6994c6fd2807SJeff Garzik 	 * preceding writes reach the controller before starting to
6995c6fd2807SJeff Garzik 	 * eat away the timeout.
6996c6fd2807SJeff Garzik 	 */
6997341c2c95STejun Heo 	deadline = ata_deadline(jiffies, timeout);
6998c6fd2807SJeff Garzik 
6999341c2c95STejun Heo 	while ((tmp & mask) == val && time_before(jiffies, deadline)) {
700097750cebSTejun Heo 		ata_msleep(ap, interval);
7001c6fd2807SJeff Garzik 		tmp = ioread32(reg);
7002c6fd2807SJeff Garzik 	}
7003c6fd2807SJeff Garzik 
7004c6fd2807SJeff Garzik 	return tmp;
7005c6fd2807SJeff Garzik }
7006c6fd2807SJeff Garzik 
70078393b811SGabriele Mazzotta /**
70088393b811SGabriele Mazzotta  *	sata_lpm_ignore_phy_events - test if PHY event should be ignored
70098393b811SGabriele Mazzotta  *	@link: Link receiving the event
70108393b811SGabriele Mazzotta  *
70118393b811SGabriele Mazzotta  *	Test whether the received PHY event has to be ignored or not.
70128393b811SGabriele Mazzotta  *
70138393b811SGabriele Mazzotta  *	LOCKING:
70148393b811SGabriele Mazzotta  *	None:
70158393b811SGabriele Mazzotta  *
70168393b811SGabriele Mazzotta  *	RETURNS:
70178393b811SGabriele Mazzotta  *	True if the event has to be ignored.
70188393b811SGabriele Mazzotta  */
70198393b811SGabriele Mazzotta bool sata_lpm_ignore_phy_events(struct ata_link *link)
70208393b811SGabriele Mazzotta {
702109c5b480SGabriele Mazzotta 	unsigned long lpm_timeout = link->last_lpm_change +
702209c5b480SGabriele Mazzotta 				    msecs_to_jiffies(ATA_TMOUT_SPURIOUS_PHY);
702309c5b480SGabriele Mazzotta 
70248393b811SGabriele Mazzotta 	/* if LPM is enabled, PHYRDY doesn't mean anything */
702509c5b480SGabriele Mazzotta 	if (link->lpm_policy > ATA_LPM_MAX_POWER)
702609c5b480SGabriele Mazzotta 		return true;
702709c5b480SGabriele Mazzotta 
702809c5b480SGabriele Mazzotta 	/* ignore the first PHY event after the LPM policy changed
702909c5b480SGabriele Mazzotta 	 * as it is might be spurious
703009c5b480SGabriele Mazzotta 	 */
703109c5b480SGabriele Mazzotta 	if ((link->flags & ATA_LFLAG_CHANGED) &&
703209c5b480SGabriele Mazzotta 	    time_before(jiffies, lpm_timeout))
703309c5b480SGabriele Mazzotta 		return true;
703409c5b480SGabriele Mazzotta 
703509c5b480SGabriele Mazzotta 	return false;
70368393b811SGabriele Mazzotta }
70378393b811SGabriele Mazzotta EXPORT_SYMBOL_GPL(sata_lpm_ignore_phy_events);
70388393b811SGabriele Mazzotta 
7039c6fd2807SJeff Garzik /*
7040c6fd2807SJeff Garzik  * Dummy port_ops
7041c6fd2807SJeff Garzik  */
7042c6fd2807SJeff Garzik static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
7043c6fd2807SJeff Garzik {
7044c6fd2807SJeff Garzik 	return AC_ERR_SYSTEM;
7045c6fd2807SJeff Garzik }
7046c6fd2807SJeff Garzik 
7047182d7bbaSTejun Heo static void ata_dummy_error_handler(struct ata_port *ap)
7048182d7bbaSTejun Heo {
7049182d7bbaSTejun Heo 	/* truly dummy */
7050182d7bbaSTejun Heo }
7051182d7bbaSTejun Heo 
7052029cfd6bSTejun Heo struct ata_port_operations ata_dummy_port_ops = {
7053c6fd2807SJeff Garzik 	.qc_prep		= ata_noop_qc_prep,
7054c6fd2807SJeff Garzik 	.qc_issue		= ata_dummy_qc_issue,
7055182d7bbaSTejun Heo 	.error_handler		= ata_dummy_error_handler,
7056e4a9c373SDan Williams 	.sched_eh		= ata_std_sched_eh,
7057e4a9c373SDan Williams 	.end_eh			= ata_std_end_eh,
7058c6fd2807SJeff Garzik };
7059c6fd2807SJeff Garzik 
706021b0ad4fSTejun Heo const struct ata_port_info ata_dummy_port_info = {
706121b0ad4fSTejun Heo 	.port_ops		= &ata_dummy_port_ops,
706221b0ad4fSTejun Heo };
706321b0ad4fSTejun Heo 
7064c6fd2807SJeff Garzik /*
7065a9a79dfeSJoe Perches  * Utility print functions
7066a9a79dfeSJoe Perches  */
7067d7bead1bSJoe Perches void ata_port_printk(const struct ata_port *ap, const char *level,
7068a9a79dfeSJoe Perches 		     const char *fmt, ...)
7069a9a79dfeSJoe Perches {
7070a9a79dfeSJoe Perches 	struct va_format vaf;
7071a9a79dfeSJoe Perches 	va_list args;
7072a9a79dfeSJoe Perches 
7073a9a79dfeSJoe Perches 	va_start(args, fmt);
7074a9a79dfeSJoe Perches 
7075a9a79dfeSJoe Perches 	vaf.fmt = fmt;
7076a9a79dfeSJoe Perches 	vaf.va = &args;
7077a9a79dfeSJoe Perches 
7078d7bead1bSJoe Perches 	printk("%sata%u: %pV", level, ap->print_id, &vaf);
7079a9a79dfeSJoe Perches 
7080a9a79dfeSJoe Perches 	va_end(args);
7081a9a79dfeSJoe Perches }
7082a9a79dfeSJoe Perches EXPORT_SYMBOL(ata_port_printk);
7083a9a79dfeSJoe Perches 
7084d7bead1bSJoe Perches void ata_link_printk(const struct ata_link *link, const char *level,
7085a9a79dfeSJoe Perches 		     const char *fmt, ...)
7086a9a79dfeSJoe Perches {
7087a9a79dfeSJoe Perches 	struct va_format vaf;
7088a9a79dfeSJoe Perches 	va_list args;
7089a9a79dfeSJoe Perches 
7090a9a79dfeSJoe Perches 	va_start(args, fmt);
7091a9a79dfeSJoe Perches 
7092a9a79dfeSJoe Perches 	vaf.fmt = fmt;
7093a9a79dfeSJoe Perches 	vaf.va = &args;
7094a9a79dfeSJoe Perches 
7095a9a79dfeSJoe Perches 	if (sata_pmp_attached(link->ap) || link->ap->slave_link)
7096d7bead1bSJoe Perches 		printk("%sata%u.%02u: %pV",
7097a9a79dfeSJoe Perches 		       level, link->ap->print_id, link->pmp, &vaf);
7098a9a79dfeSJoe Perches 	else
7099d7bead1bSJoe Perches 		printk("%sata%u: %pV",
7100a9a79dfeSJoe Perches 		       level, link->ap->print_id, &vaf);
7101a9a79dfeSJoe Perches 
7102a9a79dfeSJoe Perches 	va_end(args);
7103a9a79dfeSJoe Perches }
7104a9a79dfeSJoe Perches EXPORT_SYMBOL(ata_link_printk);
7105a9a79dfeSJoe Perches 
7106d7bead1bSJoe Perches void ata_dev_printk(const struct ata_device *dev, const char *level,
7107a9a79dfeSJoe Perches 		    const char *fmt, ...)
7108a9a79dfeSJoe Perches {
7109a9a79dfeSJoe Perches 	struct va_format vaf;
7110a9a79dfeSJoe Perches 	va_list args;
7111a9a79dfeSJoe Perches 
7112a9a79dfeSJoe Perches 	va_start(args, fmt);
7113a9a79dfeSJoe Perches 
7114a9a79dfeSJoe Perches 	vaf.fmt = fmt;
7115a9a79dfeSJoe Perches 	vaf.va = &args;
7116a9a79dfeSJoe Perches 
7117d7bead1bSJoe Perches 	printk("%sata%u.%02u: %pV",
7118a9a79dfeSJoe Perches 	       level, dev->link->ap->print_id, dev->link->pmp + dev->devno,
7119a9a79dfeSJoe Perches 	       &vaf);
7120a9a79dfeSJoe Perches 
7121a9a79dfeSJoe Perches 	va_end(args);
7122a9a79dfeSJoe Perches }
7123a9a79dfeSJoe Perches EXPORT_SYMBOL(ata_dev_printk);
7124a9a79dfeSJoe Perches 
712506296a1eSJoe Perches void ata_print_version(const struct device *dev, const char *version)
712606296a1eSJoe Perches {
712706296a1eSJoe Perches 	dev_printk(KERN_DEBUG, dev, "version %s\n", version);
712806296a1eSJoe Perches }
712906296a1eSJoe Perches EXPORT_SYMBOL(ata_print_version);
713006296a1eSJoe Perches 
7131a9a79dfeSJoe Perches /*
7132c6fd2807SJeff Garzik  * libata is essentially a library of internal helper functions for
7133c6fd2807SJeff Garzik  * low-level ATA host controller drivers.  As such, the API/ABI is
7134c6fd2807SJeff Garzik  * likely to change as new drivers are added and updated.
7135c6fd2807SJeff Garzik  * Do not depend on ABI/API stability.
7136c6fd2807SJeff Garzik  */
7137c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
7138c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
7139c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_deb_timing_long);
7140029cfd6bSTejun Heo EXPORT_SYMBOL_GPL(ata_base_port_ops);
7141029cfd6bSTejun Heo EXPORT_SYMBOL_GPL(sata_port_ops);
7142c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
714321b0ad4fSTejun Heo EXPORT_SYMBOL_GPL(ata_dummy_port_info);
71441eca4365STejun Heo EXPORT_SYMBOL_GPL(ata_link_next);
71451eca4365STejun Heo EXPORT_SYMBOL_GPL(ata_dev_next);
7146c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_std_bios_param);
7147d8d9129eSTejun Heo EXPORT_SYMBOL_GPL(ata_scsi_unlock_native_capacity);
7148cca3974eSJeff Garzik EXPORT_SYMBOL_GPL(ata_host_init);
7149f3187195STejun Heo EXPORT_SYMBOL_GPL(ata_host_alloc);
7150f5cda257STejun Heo EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
7151b1c72916STejun Heo EXPORT_SYMBOL_GPL(ata_slave_link_init);
7152ecef7253STejun Heo EXPORT_SYMBOL_GPL(ata_host_start);
7153f3187195STejun Heo EXPORT_SYMBOL_GPL(ata_host_register);
7154f5cda257STejun Heo EXPORT_SYMBOL_GPL(ata_host_activate);
71550529c159STejun Heo EXPORT_SYMBOL_GPL(ata_host_detach);
7156c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_sg_init);
7157c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_qc_complete);
7158c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
7159436d34b3STejun Heo EXPORT_SYMBOL_GPL(atapi_cmd_type);
7160c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_tf_to_fis);
7161c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_tf_from_fis);
71626357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_pack_xfermask);
71636357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_unpack_xfermask);
71646357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
71656357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
71666357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
71676357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_mode_string);
71686357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_id_xfermask);
716904351821SAlan EXPORT_SYMBOL_GPL(ata_do_set_mode);
717031cc23b3STejun Heo EXPORT_SYMBOL_GPL(ata_std_qc_defer);
7171c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
717210305f0fSAlan EXPORT_SYMBOL_GPL(ata_dev_disable);
7173c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_set_spd);
7174aa2731adSTejun Heo EXPORT_SYMBOL_GPL(ata_wait_after_reset);
7175936fd732STejun Heo EXPORT_SYMBOL_GPL(sata_link_debounce);
7176936fd732STejun Heo EXPORT_SYMBOL_GPL(sata_link_resume);
71771152b261STejun Heo EXPORT_SYMBOL_GPL(sata_link_scr_lpm);
71780aa1113dSTejun Heo EXPORT_SYMBOL_GPL(ata_std_prereset);
7179cc0680a5STejun Heo EXPORT_SYMBOL_GPL(sata_link_hardreset);
718057c9efdfSTejun Heo EXPORT_SYMBOL_GPL(sata_std_hardreset);
7181203c75b8STejun Heo EXPORT_SYMBOL_GPL(ata_std_postreset);
7182c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_dev_classify);
7183c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_dev_pair);
7184c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_ratelimit);
718597750cebSTejun Heo EXPORT_SYMBOL_GPL(ata_msleep);
7186c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_wait_register);
7187c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
7188c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
7189c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
7190c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
7191f6e67035SDan Williams EXPORT_SYMBOL_GPL(__ata_change_queue_depth);
7192c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_valid);
7193c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_read);
7194c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_write);
7195c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_write_flush);
7196936fd732STejun Heo EXPORT_SYMBOL_GPL(ata_link_online);
7197936fd732STejun Heo EXPORT_SYMBOL_GPL(ata_link_offline);
71986ffa01d8STejun Heo #ifdef CONFIG_PM
7199cca3974eSJeff Garzik EXPORT_SYMBOL_GPL(ata_host_suspend);
7200cca3974eSJeff Garzik EXPORT_SYMBOL_GPL(ata_host_resume);
72016ffa01d8STejun Heo #endif /* CONFIG_PM */
7202c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_id_string);
7203c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_id_c_string);
7204963e4975SAlan Cox EXPORT_SYMBOL_GPL(ata_do_dev_read_id);
7205c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_simulate);
7206c6fd2807SJeff Garzik 
7207c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
72086357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_timing_find_mode);
7209c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_timing_compute);
7210c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_timing_merge);
7211a0f79b92STejun Heo EXPORT_SYMBOL_GPL(ata_timing_cycle2mode);
7212c6fd2807SJeff Garzik 
7213c6fd2807SJeff Garzik #ifdef CONFIG_PCI
7214c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(pci_test_config_bits);
7215c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_remove_one);
72166ffa01d8STejun Heo #ifdef CONFIG_PM
7217c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
7218c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
7219c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
7220c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_resume);
72216ffa01d8STejun Heo #endif /* CONFIG_PM */
7222c6fd2807SJeff Garzik #endif /* CONFIG_PCI */
7223c6fd2807SJeff Garzik 
7224b7db04d9SBrian Norris EXPORT_SYMBOL_GPL(ata_platform_remove_one);
7225b7db04d9SBrian Norris 
7226b64bbc39STejun Heo EXPORT_SYMBOL_GPL(__ata_ehi_push_desc);
7227b64bbc39STejun Heo EXPORT_SYMBOL_GPL(ata_ehi_push_desc);
7228b64bbc39STejun Heo EXPORT_SYMBOL_GPL(ata_ehi_clear_desc);
7229cbcdd875STejun Heo EXPORT_SYMBOL_GPL(ata_port_desc);
7230cbcdd875STejun Heo #ifdef CONFIG_PCI
7231cbcdd875STejun Heo EXPORT_SYMBOL_GPL(ata_port_pbar_desc);
7232cbcdd875STejun Heo #endif /* CONFIG_PCI */
7233c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
7234dbd82616STejun Heo EXPORT_SYMBOL_GPL(ata_link_abort);
7235c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_abort);
7236c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_freeze);
72377d77b247STejun Heo EXPORT_SYMBOL_GPL(sata_async_notification);
7238c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
7239c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
7240c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
7241c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
724210acf3b0SMark Lord EXPORT_SYMBOL_GPL(ata_eh_analyze_ncq_error);
7243c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_do_eh);
7244a1efdabaSTejun Heo EXPORT_SYMBOL_GPL(ata_std_error_handler);
7245be0d18dfSAlan Cox 
7246be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_40wire);
7247be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_80wire);
7248be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_unknown);
7249c88f90c3STejun Heo EXPORT_SYMBOL_GPL(ata_cable_ignore);
7250be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_sata);
7251