xref: /openbmc/linux/drivers/ata/libata-core.c (revision 322579dc)
1c6fd2807SJeff Garzik /*
2c6fd2807SJeff Garzik  *  libata-core.c - helper library for ATA
3c6fd2807SJeff Garzik  *
48c3d3d4bSTejun Heo  *  Maintained by:  Tejun Heo <tj@kernel.org>
5c6fd2807SJeff Garzik  *    		    Please ALWAYS copy linux-ide@vger.kernel.org
6c6fd2807SJeff Garzik  *		    on emails.
7c6fd2807SJeff Garzik  *
8c6fd2807SJeff Garzik  *  Copyright 2003-2004 Red Hat, Inc.  All rights reserved.
9c6fd2807SJeff Garzik  *  Copyright 2003-2004 Jeff Garzik
10c6fd2807SJeff Garzik  *
11c6fd2807SJeff Garzik  *
12c6fd2807SJeff Garzik  *  This program is free software; you can redistribute it and/or modify
13c6fd2807SJeff Garzik  *  it under the terms of the GNU General Public License as published by
14c6fd2807SJeff Garzik  *  the Free Software Foundation; either version 2, or (at your option)
15c6fd2807SJeff Garzik  *  any later version.
16c6fd2807SJeff Garzik  *
17c6fd2807SJeff Garzik  *  This program is distributed in the hope that it will be useful,
18c6fd2807SJeff Garzik  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
19c6fd2807SJeff Garzik  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20c6fd2807SJeff Garzik  *  GNU General Public License for more details.
21c6fd2807SJeff Garzik  *
22c6fd2807SJeff Garzik  *  You should have received a copy of the GNU General Public License
23c6fd2807SJeff Garzik  *  along with this program; see the file COPYING.  If not, write to
24c6fd2807SJeff Garzik  *  the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25c6fd2807SJeff Garzik  *
26c6fd2807SJeff Garzik  *
27c6fd2807SJeff Garzik  *  libata documentation is available via 'make {ps|pdf}docs',
2819285f3cSMauro Carvalho Chehab  *  as Documentation/driver-api/libata.rst
29c6fd2807SJeff Garzik  *
30c6fd2807SJeff Garzik  *  Hardware documentation available from http://www.t13.org/ and
31c6fd2807SJeff Garzik  *  http://www.sata-io.org/
32c6fd2807SJeff Garzik  *
3392c52c52SAlan Cox  *  Standards documents from:
3492c52c52SAlan Cox  *	http://www.t13.org (ATA standards, PCI DMA IDE spec)
3592c52c52SAlan Cox  *	http://www.t10.org (SCSI MMC - for ATAPI MMC)
3692c52c52SAlan Cox  *	http://www.sata-io.org (SATA)
3792c52c52SAlan Cox  *	http://www.compactflash.org (CF)
3892c52c52SAlan Cox  *	http://www.qic.org (QIC157 - Tape and DSC)
3992c52c52SAlan Cox  *	http://www.ce-ata.org (CE-ATA: not supported)
4092c52c52SAlan Cox  *
41c6fd2807SJeff Garzik  */
42c6fd2807SJeff Garzik 
43c6fd2807SJeff Garzik #include <linux/kernel.h>
44c6fd2807SJeff Garzik #include <linux/module.h>
45c6fd2807SJeff Garzik #include <linux/pci.h>
46c6fd2807SJeff Garzik #include <linux/init.h>
47c6fd2807SJeff Garzik #include <linux/list.h>
48c6fd2807SJeff Garzik #include <linux/mm.h>
49c6fd2807SJeff Garzik #include <linux/spinlock.h>
50c6fd2807SJeff Garzik #include <linux/blkdev.h>
51c6fd2807SJeff Garzik #include <linux/delay.h>
52c6fd2807SJeff Garzik #include <linux/timer.h>
53848c3920SAnil Veliyankara Madam #include <linux/time.h>
54c6fd2807SJeff Garzik #include <linux/interrupt.h>
55c6fd2807SJeff Garzik #include <linux/completion.h>
56c6fd2807SJeff Garzik #include <linux/suspend.h>
57c6fd2807SJeff Garzik #include <linux/workqueue.h>
58c6fd2807SJeff Garzik #include <linux/scatterlist.h>
592dcb407eSJeff Garzik #include <linux/io.h>
6079318057SArjan van de Ven #include <linux/async.h>
61e18086d6SMark Lord #include <linux/log2.h>
625a0e3ad6STejun Heo #include <linux/slab.h>
63428ac5fcSGeorge Spelvin #include <linux/glob.h>
64c6fd2807SJeff Garzik #include <scsi/scsi.h>
65c6fd2807SJeff Garzik #include <scsi/scsi_cmnd.h>
66c6fd2807SJeff Garzik #include <scsi/scsi_host.h>
67c6fd2807SJeff Garzik #include <linux/libata.h>
68c6fd2807SJeff Garzik #include <asm/byteorder.h>
69fe5af0ccSHannes Reinecke #include <asm/unaligned.h>
70140b5e59STejun Heo #include <linux/cdrom.h>
719990b6f3SAkinobu Mita #include <linux/ratelimit.h>
72eb25cb99SStephan Linz #include <linux/leds.h>
739ee4f393SLin Ming #include <linux/pm_runtime.h>
74b7db04d9SBrian Norris #include <linux/platform_device.h>
75c6fd2807SJeff Garzik 
76255c03d1SHannes Reinecke #define CREATE_TRACE_POINTS
77255c03d1SHannes Reinecke #include <trace/events/libata.h>
78255c03d1SHannes Reinecke 
79c6fd2807SJeff Garzik #include "libata.h"
80d9027470SGwendal Grignou #include "libata-transport.h"
81fda0efc5SJeff Garzik 
82c6fd2807SJeff Garzik /* debounce timing parameters in msecs { interval, duration, timeout } */
83c6fd2807SJeff Garzik const unsigned long sata_deb_timing_normal[]		= {   5,  100, 2000 };
84c6fd2807SJeff Garzik const unsigned long sata_deb_timing_hotplug[]		= {  25,  500, 2000 };
85c6fd2807SJeff Garzik const unsigned long sata_deb_timing_long[]		= { 100, 2000, 5000 };
86c6fd2807SJeff Garzik 
87029cfd6bSTejun Heo const struct ata_port_operations ata_base_port_ops = {
880aa1113dSTejun Heo 	.prereset		= ata_std_prereset,
89203c75b8STejun Heo 	.postreset		= ata_std_postreset,
90a1efdabaSTejun Heo 	.error_handler		= ata_std_error_handler,
91e4a9c373SDan Williams 	.sched_eh		= ata_std_sched_eh,
92e4a9c373SDan Williams 	.end_eh			= ata_std_end_eh,
93029cfd6bSTejun Heo };
94029cfd6bSTejun Heo 
95029cfd6bSTejun Heo const struct ata_port_operations sata_port_ops = {
96029cfd6bSTejun Heo 	.inherits		= &ata_base_port_ops,
97029cfd6bSTejun Heo 
98029cfd6bSTejun Heo 	.qc_defer		= ata_std_qc_defer,
9957c9efdfSTejun Heo 	.hardreset		= sata_std_hardreset,
100029cfd6bSTejun Heo };
101029cfd6bSTejun Heo 
102c6fd2807SJeff Garzik static unsigned int ata_dev_init_params(struct ata_device *dev,
103c6fd2807SJeff Garzik 					u16 heads, u16 sectors);
104c6fd2807SJeff Garzik static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
105c6fd2807SJeff Garzik static void ata_dev_xfermask(struct ata_device *dev);
10675683fe7STejun Heo static unsigned long ata_dev_blacklisted(const struct ata_device *dev);
107c6fd2807SJeff Garzik 
108a78f57afSTero Roponen atomic_t ata_print_id = ATOMIC_INIT(0);
109c6fd2807SJeff Garzik 
11033267325STejun Heo struct ata_force_param {
11133267325STejun Heo 	const char	*name;
11233267325STejun Heo 	unsigned int	cbl;
11333267325STejun Heo 	int		spd_limit;
11433267325STejun Heo 	unsigned long	xfer_mask;
11533267325STejun Heo 	unsigned int	horkage_on;
11633267325STejun Heo 	unsigned int	horkage_off;
11705944bdfSTejun Heo 	unsigned int	lflags;
11833267325STejun Heo };
11933267325STejun Heo 
12033267325STejun Heo struct ata_force_ent {
12133267325STejun Heo 	int			port;
12233267325STejun Heo 	int			device;
12333267325STejun Heo 	struct ata_force_param	param;
12433267325STejun Heo };
12533267325STejun Heo 
12633267325STejun Heo static struct ata_force_ent *ata_force_tbl;
12733267325STejun Heo static int ata_force_tbl_size;
12833267325STejun Heo 
12933267325STejun Heo static char ata_force_param_buf[PAGE_SIZE] __initdata;
1307afb4222STejun Heo /* param_buf is thrown away after initialization, disallow read */
1317afb4222STejun Heo module_param_string(force, ata_force_param_buf, sizeof(ata_force_param_buf), 0);
1328c27ceffSMauro Carvalho Chehab MODULE_PARM_DESC(force, "Force ATA configurations including cable type, link speed and transfer mode (see Documentation/admin-guide/kernel-parameters.rst for details)");
13333267325STejun Heo 
1342486fa56STejun Heo static int atapi_enabled = 1;
135c6fd2807SJeff Garzik module_param(atapi_enabled, int, 0444);
136ad5d8eacSEvgeni Golov MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on [default])");
137c6fd2807SJeff Garzik 
138c5c61bdaSAdrian Bunk static int atapi_dmadir = 0;
139c6fd2807SJeff Garzik module_param(atapi_dmadir, int, 0444);
140ad5d8eacSEvgeni Golov MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off [default], 1=on)");
141c6fd2807SJeff Garzik 
142baf4fdfaSMark Lord int atapi_passthru16 = 1;
143baf4fdfaSMark Lord module_param(atapi_passthru16, int, 0444);
144ad5d8eacSEvgeni Golov MODULE_PARM_DESC(atapi_passthru16, "Enable ATA_16 passthru for ATAPI devices (0=off, 1=on [default])");
145baf4fdfaSMark Lord 
146c6fd2807SJeff Garzik int libata_fua = 0;
147c6fd2807SJeff Garzik module_param_named(fua, libata_fua, int, 0444);
148ad5d8eacSEvgeni Golov MODULE_PARM_DESC(fua, "FUA support (0=off [default], 1=on)");
149c6fd2807SJeff Garzik 
1502dcb407eSJeff Garzik static int ata_ignore_hpa;
1511e999736SAlan Cox module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644);
1521e999736SAlan Cox MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)");
1531e999736SAlan Cox 
154b3a70601SAlan Cox static int libata_dma_mask = ATA_DMA_MASK_ATA|ATA_DMA_MASK_ATAPI|ATA_DMA_MASK_CFA;
155b3a70601SAlan Cox module_param_named(dma, libata_dma_mask, int, 0444);
156b3a70601SAlan Cox MODULE_PARM_DESC(dma, "DMA enable/disable (0x1==ATA, 0x2==ATAPI, 0x4==CF)");
157b3a70601SAlan Cox 
15887fbc5a0STejun Heo static int ata_probe_timeout;
159c6fd2807SJeff Garzik module_param(ata_probe_timeout, int, 0444);
160c6fd2807SJeff Garzik MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
161c6fd2807SJeff Garzik 
1626ebe9d86SJeff Garzik int libata_noacpi = 0;
163d7d0dad6SJeff Garzik module_param_named(noacpi, libata_noacpi, int, 0444);
164ad5d8eacSEvgeni Golov MODULE_PARM_DESC(noacpi, "Disable the use of ACPI in probe/suspend/resume (0=off [default], 1=on)");
16511ef697bSKristen Carlson Accardi 
166ae8d4ee7SAlan Cox int libata_allow_tpm = 0;
167ae8d4ee7SAlan Cox module_param_named(allow_tpm, libata_allow_tpm, int, 0444);
168ad5d8eacSEvgeni Golov MODULE_PARM_DESC(allow_tpm, "Permit the use of TPM commands (0=off [default], 1=on)");
169ae8d4ee7SAlan Cox 
170e7ecd435STejun Heo static int atapi_an;
171e7ecd435STejun Heo module_param(atapi_an, int, 0444);
172e7ecd435STejun Heo MODULE_PARM_DESC(atapi_an, "Enable ATAPI AN media presence notification (0=0ff [default], 1=on)");
173e7ecd435STejun Heo 
174c6fd2807SJeff Garzik MODULE_AUTHOR("Jeff Garzik");
175c6fd2807SJeff Garzik MODULE_DESCRIPTION("Library module for ATA devices");
176c6fd2807SJeff Garzik MODULE_LICENSE("GPL");
177c6fd2807SJeff Garzik MODULE_VERSION(DRV_VERSION);
178c6fd2807SJeff Garzik 
179c6fd2807SJeff Garzik 
1809913ff8aSTejun Heo static bool ata_sstatus_online(u32 sstatus)
1819913ff8aSTejun Heo {
1829913ff8aSTejun Heo 	return (sstatus & 0xf) == 0x3;
1839913ff8aSTejun Heo }
1849913ff8aSTejun Heo 
1851eca4365STejun Heo /**
1861eca4365STejun Heo  *	ata_link_next - link iteration helper
1871eca4365STejun Heo  *	@link: the previous link, NULL to start
1881eca4365STejun Heo  *	@ap: ATA port containing links to iterate
1891eca4365STejun Heo  *	@mode: iteration mode, one of ATA_LITER_*
190aadffb68STejun Heo  *
191aadffb68STejun Heo  *	LOCKING:
192aadffb68STejun Heo  *	Host lock or EH context.
1931eca4365STejun Heo  *
1941eca4365STejun Heo  *	RETURNS:
1951eca4365STejun Heo  *	Pointer to the next link.
196aadffb68STejun Heo  */
1971eca4365STejun Heo struct ata_link *ata_link_next(struct ata_link *link, struct ata_port *ap,
1981eca4365STejun Heo 			       enum ata_link_iter_mode mode)
199aadffb68STejun Heo {
2001eca4365STejun Heo 	BUG_ON(mode != ATA_LITER_EDGE &&
2011eca4365STejun Heo 	       mode != ATA_LITER_PMP_FIRST && mode != ATA_LITER_HOST_FIRST);
2021eca4365STejun Heo 
203aadffb68STejun Heo 	/* NULL link indicates start of iteration */
2041eca4365STejun Heo 	if (!link)
2051eca4365STejun Heo 		switch (mode) {
2061eca4365STejun Heo 		case ATA_LITER_EDGE:
2071eca4365STejun Heo 		case ATA_LITER_PMP_FIRST:
2081eca4365STejun Heo 			if (sata_pmp_attached(ap))
209aadffb68STejun Heo 				return ap->pmp_link;
2101eca4365STejun Heo 			/* fall through */
2111eca4365STejun Heo 		case ATA_LITER_HOST_FIRST:
212aadffb68STejun Heo 			return &ap->link;
213aadffb68STejun Heo 		}
214aadffb68STejun Heo 
2151eca4365STejun Heo 	/* we just iterated over the host link, what's next? */
2161eca4365STejun Heo 	if (link == &ap->link)
2171eca4365STejun Heo 		switch (mode) {
2181eca4365STejun Heo 		case ATA_LITER_HOST_FIRST:
2191eca4365STejun Heo 			if (sata_pmp_attached(ap))
220aadffb68STejun Heo 				return ap->pmp_link;
2211eca4365STejun Heo 			/* fall through */
2221eca4365STejun Heo 		case ATA_LITER_PMP_FIRST:
2231eca4365STejun Heo 			if (unlikely(ap->slave_link))
2241eca4365STejun Heo 				return ap->slave_link;
2251eca4365STejun Heo 			/* fall through */
2261eca4365STejun Heo 		case ATA_LITER_EDGE:
2271eca4365STejun Heo 			return NULL;
228aadffb68STejun Heo 		}
229aadffb68STejun Heo 
230b1c72916STejun Heo 	/* slave_link excludes PMP */
231b1c72916STejun Heo 	if (unlikely(link == ap->slave_link))
232b1c72916STejun Heo 		return NULL;
233b1c72916STejun Heo 
2341eca4365STejun Heo 	/* we were over a PMP link */
235aadffb68STejun Heo 	if (++link < ap->pmp_link + ap->nr_pmp_links)
236aadffb68STejun Heo 		return link;
2371eca4365STejun Heo 
2381eca4365STejun Heo 	if (mode == ATA_LITER_PMP_FIRST)
2391eca4365STejun Heo 		return &ap->link;
2401eca4365STejun Heo 
241aadffb68STejun Heo 	return NULL;
242aadffb68STejun Heo }
243aadffb68STejun Heo 
244c6fd2807SJeff Garzik /**
2451eca4365STejun Heo  *	ata_dev_next - device iteration helper
2461eca4365STejun Heo  *	@dev: the previous device, NULL to start
2471eca4365STejun Heo  *	@link: ATA link containing devices to iterate
2481eca4365STejun Heo  *	@mode: iteration mode, one of ATA_DITER_*
2491eca4365STejun Heo  *
2501eca4365STejun Heo  *	LOCKING:
2511eca4365STejun Heo  *	Host lock or EH context.
2521eca4365STejun Heo  *
2531eca4365STejun Heo  *	RETURNS:
2541eca4365STejun Heo  *	Pointer to the next device.
2551eca4365STejun Heo  */
2561eca4365STejun Heo struct ata_device *ata_dev_next(struct ata_device *dev, struct ata_link *link,
2571eca4365STejun Heo 				enum ata_dev_iter_mode mode)
2581eca4365STejun Heo {
2591eca4365STejun Heo 	BUG_ON(mode != ATA_DITER_ENABLED && mode != ATA_DITER_ENABLED_REVERSE &&
2601eca4365STejun Heo 	       mode != ATA_DITER_ALL && mode != ATA_DITER_ALL_REVERSE);
2611eca4365STejun Heo 
2621eca4365STejun Heo 	/* NULL dev indicates start of iteration */
2631eca4365STejun Heo 	if (!dev)
2641eca4365STejun Heo 		switch (mode) {
2651eca4365STejun Heo 		case ATA_DITER_ENABLED:
2661eca4365STejun Heo 		case ATA_DITER_ALL:
2671eca4365STejun Heo 			dev = link->device;
2681eca4365STejun Heo 			goto check;
2691eca4365STejun Heo 		case ATA_DITER_ENABLED_REVERSE:
2701eca4365STejun Heo 		case ATA_DITER_ALL_REVERSE:
2711eca4365STejun Heo 			dev = link->device + ata_link_max_devices(link) - 1;
2721eca4365STejun Heo 			goto check;
2731eca4365STejun Heo 		}
2741eca4365STejun Heo 
2751eca4365STejun Heo  next:
2761eca4365STejun Heo 	/* move to the next one */
2771eca4365STejun Heo 	switch (mode) {
2781eca4365STejun Heo 	case ATA_DITER_ENABLED:
2791eca4365STejun Heo 	case ATA_DITER_ALL:
2801eca4365STejun Heo 		if (++dev < link->device + ata_link_max_devices(link))
2811eca4365STejun Heo 			goto check;
2821eca4365STejun Heo 		return NULL;
2831eca4365STejun Heo 	case ATA_DITER_ENABLED_REVERSE:
2841eca4365STejun Heo 	case ATA_DITER_ALL_REVERSE:
2851eca4365STejun Heo 		if (--dev >= link->device)
2861eca4365STejun Heo 			goto check;
2871eca4365STejun Heo 		return NULL;
2881eca4365STejun Heo 	}
2891eca4365STejun Heo 
2901eca4365STejun Heo  check:
2911eca4365STejun Heo 	if ((mode == ATA_DITER_ENABLED || mode == ATA_DITER_ENABLED_REVERSE) &&
2921eca4365STejun Heo 	    !ata_dev_enabled(dev))
2931eca4365STejun Heo 		goto next;
2941eca4365STejun Heo 	return dev;
2951eca4365STejun Heo }
2961eca4365STejun Heo 
2971eca4365STejun Heo /**
298b1c72916STejun Heo  *	ata_dev_phys_link - find physical link for a device
299b1c72916STejun Heo  *	@dev: ATA device to look up physical link for
300b1c72916STejun Heo  *
301b1c72916STejun Heo  *	Look up physical link which @dev is attached to.  Note that
302b1c72916STejun Heo  *	this is different from @dev->link only when @dev is on slave
303b1c72916STejun Heo  *	link.  For all other cases, it's the same as @dev->link.
304b1c72916STejun Heo  *
305b1c72916STejun Heo  *	LOCKING:
306b1c72916STejun Heo  *	Don't care.
307b1c72916STejun Heo  *
308b1c72916STejun Heo  *	RETURNS:
309b1c72916STejun Heo  *	Pointer to the found physical link.
310b1c72916STejun Heo  */
311b1c72916STejun Heo struct ata_link *ata_dev_phys_link(struct ata_device *dev)
312b1c72916STejun Heo {
313b1c72916STejun Heo 	struct ata_port *ap = dev->link->ap;
314b1c72916STejun Heo 
315b1c72916STejun Heo 	if (!ap->slave_link)
316b1c72916STejun Heo 		return dev->link;
317b1c72916STejun Heo 	if (!dev->devno)
318b1c72916STejun Heo 		return &ap->link;
319b1c72916STejun Heo 	return ap->slave_link;
320b1c72916STejun Heo }
321b1c72916STejun Heo 
322b1c72916STejun Heo /**
32333267325STejun Heo  *	ata_force_cbl - force cable type according to libata.force
3244cdfa1b3SRandy Dunlap  *	@ap: ATA port of interest
32533267325STejun Heo  *
32633267325STejun Heo  *	Force cable type according to libata.force and whine about it.
32733267325STejun Heo  *	The last entry which has matching port number is used, so it
32833267325STejun Heo  *	can be specified as part of device force parameters.  For
32933267325STejun Heo  *	example, both "a:40c,1.00:udma4" and "1.00:40c,udma4" have the
33033267325STejun Heo  *	same effect.
33133267325STejun Heo  *
33233267325STejun Heo  *	LOCKING:
33333267325STejun Heo  *	EH context.
33433267325STejun Heo  */
33533267325STejun Heo void ata_force_cbl(struct ata_port *ap)
33633267325STejun Heo {
33733267325STejun Heo 	int i;
33833267325STejun Heo 
33933267325STejun Heo 	for (i = ata_force_tbl_size - 1; i >= 0; i--) {
34033267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
34133267325STejun Heo 
34233267325STejun Heo 		if (fe->port != -1 && fe->port != ap->print_id)
34333267325STejun Heo 			continue;
34433267325STejun Heo 
34533267325STejun Heo 		if (fe->param.cbl == ATA_CBL_NONE)
34633267325STejun Heo 			continue;
34733267325STejun Heo 
34833267325STejun Heo 		ap->cbl = fe->param.cbl;
349a9a79dfeSJoe Perches 		ata_port_notice(ap, "FORCE: cable set to %s\n", fe->param.name);
35033267325STejun Heo 		return;
35133267325STejun Heo 	}
35233267325STejun Heo }
35333267325STejun Heo 
35433267325STejun Heo /**
35505944bdfSTejun Heo  *	ata_force_link_limits - force link limits according to libata.force
35633267325STejun Heo  *	@link: ATA link of interest
35733267325STejun Heo  *
35805944bdfSTejun Heo  *	Force link flags and SATA spd limit according to libata.force
35905944bdfSTejun Heo  *	and whine about it.  When only the port part is specified
36005944bdfSTejun Heo  *	(e.g. 1:), the limit applies to all links connected to both
36105944bdfSTejun Heo  *	the host link and all fan-out ports connected via PMP.  If the
36205944bdfSTejun Heo  *	device part is specified as 0 (e.g. 1.00:), it specifies the
36305944bdfSTejun Heo  *	first fan-out link not the host link.  Device number 15 always
364b1c72916STejun Heo  *	points to the host link whether PMP is attached or not.  If the
365b1c72916STejun Heo  *	controller has slave link, device number 16 points to it.
36633267325STejun Heo  *
36733267325STejun Heo  *	LOCKING:
36833267325STejun Heo  *	EH context.
36933267325STejun Heo  */
37005944bdfSTejun Heo static void ata_force_link_limits(struct ata_link *link)
37133267325STejun Heo {
37205944bdfSTejun Heo 	bool did_spd = false;
373b1c72916STejun Heo 	int linkno = link->pmp;
374b1c72916STejun Heo 	int i;
37533267325STejun Heo 
37633267325STejun Heo 	if (ata_is_host_link(link))
377b1c72916STejun Heo 		linkno += 15;
37833267325STejun Heo 
37933267325STejun Heo 	for (i = ata_force_tbl_size - 1; i >= 0; i--) {
38033267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
38133267325STejun Heo 
38233267325STejun Heo 		if (fe->port != -1 && fe->port != link->ap->print_id)
38333267325STejun Heo 			continue;
38433267325STejun Heo 
38533267325STejun Heo 		if (fe->device != -1 && fe->device != linkno)
38633267325STejun Heo 			continue;
38733267325STejun Heo 
38805944bdfSTejun Heo 		/* only honor the first spd limit */
38905944bdfSTejun Heo 		if (!did_spd && fe->param.spd_limit) {
39033267325STejun Heo 			link->hw_sata_spd_limit = (1 << fe->param.spd_limit) - 1;
391a9a79dfeSJoe Perches 			ata_link_notice(link, "FORCE: PHY spd limit set to %s\n",
39205944bdfSTejun Heo 					fe->param.name);
39305944bdfSTejun Heo 			did_spd = true;
39405944bdfSTejun Heo 		}
39505944bdfSTejun Heo 
39605944bdfSTejun Heo 		/* let lflags stack */
39705944bdfSTejun Heo 		if (fe->param.lflags) {
39805944bdfSTejun Heo 			link->flags |= fe->param.lflags;
399a9a79dfeSJoe Perches 			ata_link_notice(link,
40005944bdfSTejun Heo 					"FORCE: link flag 0x%x forced -> 0x%x\n",
40105944bdfSTejun Heo 					fe->param.lflags, link->flags);
40205944bdfSTejun Heo 		}
40333267325STejun Heo 	}
40433267325STejun Heo }
40533267325STejun Heo 
40633267325STejun Heo /**
40733267325STejun Heo  *	ata_force_xfermask - force xfermask according to libata.force
40833267325STejun Heo  *	@dev: ATA device of interest
40933267325STejun Heo  *
41033267325STejun Heo  *	Force xfer_mask according to libata.force and whine about it.
41133267325STejun Heo  *	For consistency with link selection, device number 15 selects
41233267325STejun Heo  *	the first device connected to the host link.
41333267325STejun Heo  *
41433267325STejun Heo  *	LOCKING:
41533267325STejun Heo  *	EH context.
41633267325STejun Heo  */
41733267325STejun Heo static void ata_force_xfermask(struct ata_device *dev)
41833267325STejun Heo {
41933267325STejun Heo 	int devno = dev->link->pmp + dev->devno;
42033267325STejun Heo 	int alt_devno = devno;
42133267325STejun Heo 	int i;
42233267325STejun Heo 
423b1c72916STejun Heo 	/* allow n.15/16 for devices attached to host port */
424b1c72916STejun Heo 	if (ata_is_host_link(dev->link))
425b1c72916STejun Heo 		alt_devno += 15;
42633267325STejun Heo 
42733267325STejun Heo 	for (i = ata_force_tbl_size - 1; i >= 0; i--) {
42833267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
42933267325STejun Heo 		unsigned long pio_mask, mwdma_mask, udma_mask;
43033267325STejun Heo 
43133267325STejun Heo 		if (fe->port != -1 && fe->port != dev->link->ap->print_id)
43233267325STejun Heo 			continue;
43333267325STejun Heo 
43433267325STejun Heo 		if (fe->device != -1 && fe->device != devno &&
43533267325STejun Heo 		    fe->device != alt_devno)
43633267325STejun Heo 			continue;
43733267325STejun Heo 
43833267325STejun Heo 		if (!fe->param.xfer_mask)
43933267325STejun Heo 			continue;
44033267325STejun Heo 
44133267325STejun Heo 		ata_unpack_xfermask(fe->param.xfer_mask,
44233267325STejun Heo 				    &pio_mask, &mwdma_mask, &udma_mask);
44333267325STejun Heo 		if (udma_mask)
44433267325STejun Heo 			dev->udma_mask = udma_mask;
44533267325STejun Heo 		else if (mwdma_mask) {
44633267325STejun Heo 			dev->udma_mask = 0;
44733267325STejun Heo 			dev->mwdma_mask = mwdma_mask;
44833267325STejun Heo 		} else {
44933267325STejun Heo 			dev->udma_mask = 0;
45033267325STejun Heo 			dev->mwdma_mask = 0;
45133267325STejun Heo 			dev->pio_mask = pio_mask;
45233267325STejun Heo 		}
45333267325STejun Heo 
454a9a79dfeSJoe Perches 		ata_dev_notice(dev, "FORCE: xfer_mask set to %s\n",
455a9a79dfeSJoe Perches 			       fe->param.name);
45633267325STejun Heo 		return;
45733267325STejun Heo 	}
45833267325STejun Heo }
45933267325STejun Heo 
46033267325STejun Heo /**
46133267325STejun Heo  *	ata_force_horkage - force horkage according to libata.force
46233267325STejun Heo  *	@dev: ATA device of interest
46333267325STejun Heo  *
46433267325STejun Heo  *	Force horkage according to libata.force and whine about it.
46533267325STejun Heo  *	For consistency with link selection, device number 15 selects
46633267325STejun Heo  *	the first device connected to the host link.
46733267325STejun Heo  *
46833267325STejun Heo  *	LOCKING:
46933267325STejun Heo  *	EH context.
47033267325STejun Heo  */
47133267325STejun Heo static void ata_force_horkage(struct ata_device *dev)
47233267325STejun Heo {
47333267325STejun Heo 	int devno = dev->link->pmp + dev->devno;
47433267325STejun Heo 	int alt_devno = devno;
47533267325STejun Heo 	int i;
47633267325STejun Heo 
477b1c72916STejun Heo 	/* allow n.15/16 for devices attached to host port */
478b1c72916STejun Heo 	if (ata_is_host_link(dev->link))
479b1c72916STejun Heo 		alt_devno += 15;
48033267325STejun Heo 
48133267325STejun Heo 	for (i = 0; i < ata_force_tbl_size; i++) {
48233267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
48333267325STejun Heo 
48433267325STejun Heo 		if (fe->port != -1 && fe->port != dev->link->ap->print_id)
48533267325STejun Heo 			continue;
48633267325STejun Heo 
48733267325STejun Heo 		if (fe->device != -1 && fe->device != devno &&
48833267325STejun Heo 		    fe->device != alt_devno)
48933267325STejun Heo 			continue;
49033267325STejun Heo 
49133267325STejun Heo 		if (!(~dev->horkage & fe->param.horkage_on) &&
49233267325STejun Heo 		    !(dev->horkage & fe->param.horkage_off))
49333267325STejun Heo 			continue;
49433267325STejun Heo 
49533267325STejun Heo 		dev->horkage |= fe->param.horkage_on;
49633267325STejun Heo 		dev->horkage &= ~fe->param.horkage_off;
49733267325STejun Heo 
498a9a79dfeSJoe Perches 		ata_dev_notice(dev, "FORCE: horkage modified (%s)\n",
499a9a79dfeSJoe Perches 			       fe->param.name);
50033267325STejun Heo 	}
50133267325STejun Heo }
50233267325STejun Heo 
50333267325STejun Heo /**
504436d34b3STejun Heo  *	atapi_cmd_type - Determine ATAPI command type from SCSI opcode
505436d34b3STejun Heo  *	@opcode: SCSI opcode
506436d34b3STejun Heo  *
507436d34b3STejun Heo  *	Determine ATAPI command type from @opcode.
508436d34b3STejun Heo  *
509436d34b3STejun Heo  *	LOCKING:
510436d34b3STejun Heo  *	None.
511436d34b3STejun Heo  *
512436d34b3STejun Heo  *	RETURNS:
513436d34b3STejun Heo  *	ATAPI_{READ|WRITE|READ_CD|PASS_THRU|MISC}
514436d34b3STejun Heo  */
515436d34b3STejun Heo int atapi_cmd_type(u8 opcode)
516436d34b3STejun Heo {
517436d34b3STejun Heo 	switch (opcode) {
518436d34b3STejun Heo 	case GPCMD_READ_10:
519436d34b3STejun Heo 	case GPCMD_READ_12:
520436d34b3STejun Heo 		return ATAPI_READ;
521436d34b3STejun Heo 
522436d34b3STejun Heo 	case GPCMD_WRITE_10:
523436d34b3STejun Heo 	case GPCMD_WRITE_12:
524436d34b3STejun Heo 	case GPCMD_WRITE_AND_VERIFY_10:
525436d34b3STejun Heo 		return ATAPI_WRITE;
526436d34b3STejun Heo 
527436d34b3STejun Heo 	case GPCMD_READ_CD:
528436d34b3STejun Heo 	case GPCMD_READ_CD_MSF:
529436d34b3STejun Heo 		return ATAPI_READ_CD;
530436d34b3STejun Heo 
531e52dcc48STejun Heo 	case ATA_16:
532e52dcc48STejun Heo 	case ATA_12:
533e52dcc48STejun Heo 		if (atapi_passthru16)
534e52dcc48STejun Heo 			return ATAPI_PASS_THRU;
535e52dcc48STejun Heo 		/* fall thru */
536436d34b3STejun Heo 	default:
537436d34b3STejun Heo 		return ATAPI_MISC;
538436d34b3STejun Heo 	}
539436d34b3STejun Heo }
540436d34b3STejun Heo 
541436d34b3STejun Heo /**
542c6fd2807SJeff Garzik  *	ata_tf_to_fis - Convert ATA taskfile to SATA FIS structure
543c6fd2807SJeff Garzik  *	@tf: Taskfile to convert
544c6fd2807SJeff Garzik  *	@pmp: Port multiplier port
5459977126cSTejun Heo  *	@is_cmd: This FIS is for command
5469977126cSTejun Heo  *	@fis: Buffer into which data will output
547c6fd2807SJeff Garzik  *
548c6fd2807SJeff Garzik  *	Converts a standard ATA taskfile to a Serial ATA
549c6fd2807SJeff Garzik  *	FIS structure (Register - Host to Device).
550c6fd2807SJeff Garzik  *
551c6fd2807SJeff Garzik  *	LOCKING:
552c6fd2807SJeff Garzik  *	Inherited from caller.
553c6fd2807SJeff Garzik  */
5549977126cSTejun Heo void ata_tf_to_fis(const struct ata_taskfile *tf, u8 pmp, int is_cmd, u8 *fis)
555c6fd2807SJeff Garzik {
556c6fd2807SJeff Garzik 	fis[0] = 0x27;			/* Register - Host to Device FIS */
5579977126cSTejun Heo 	fis[1] = pmp & 0xf;		/* Port multiplier number*/
5589977126cSTejun Heo 	if (is_cmd)
5599977126cSTejun Heo 		fis[1] |= (1 << 7);	/* bit 7 indicates Command FIS */
5609977126cSTejun Heo 
561c6fd2807SJeff Garzik 	fis[2] = tf->command;
562c6fd2807SJeff Garzik 	fis[3] = tf->feature;
563c6fd2807SJeff Garzik 
564c6fd2807SJeff Garzik 	fis[4] = tf->lbal;
565c6fd2807SJeff Garzik 	fis[5] = tf->lbam;
566c6fd2807SJeff Garzik 	fis[6] = tf->lbah;
567c6fd2807SJeff Garzik 	fis[7] = tf->device;
568c6fd2807SJeff Garzik 
569c6fd2807SJeff Garzik 	fis[8] = tf->hob_lbal;
570c6fd2807SJeff Garzik 	fis[9] = tf->hob_lbam;
571c6fd2807SJeff Garzik 	fis[10] = tf->hob_lbah;
572c6fd2807SJeff Garzik 	fis[11] = tf->hob_feature;
573c6fd2807SJeff Garzik 
574c6fd2807SJeff Garzik 	fis[12] = tf->nsect;
575c6fd2807SJeff Garzik 	fis[13] = tf->hob_nsect;
576c6fd2807SJeff Garzik 	fis[14] = 0;
577c6fd2807SJeff Garzik 	fis[15] = tf->ctl;
578c6fd2807SJeff Garzik 
57986a565e6SMarc Carino 	fis[16] = tf->auxiliary & 0xff;
58086a565e6SMarc Carino 	fis[17] = (tf->auxiliary >> 8) & 0xff;
58186a565e6SMarc Carino 	fis[18] = (tf->auxiliary >> 16) & 0xff;
58286a565e6SMarc Carino 	fis[19] = (tf->auxiliary >> 24) & 0xff;
583c6fd2807SJeff Garzik }
584c6fd2807SJeff Garzik 
585c6fd2807SJeff Garzik /**
586c6fd2807SJeff Garzik  *	ata_tf_from_fis - Convert SATA FIS to ATA taskfile
587c6fd2807SJeff Garzik  *	@fis: Buffer from which data will be input
588c6fd2807SJeff Garzik  *	@tf: Taskfile to output
589c6fd2807SJeff Garzik  *
590c6fd2807SJeff Garzik  *	Converts a serial ATA FIS structure to a standard ATA taskfile.
591c6fd2807SJeff Garzik  *
592c6fd2807SJeff Garzik  *	LOCKING:
593c6fd2807SJeff Garzik  *	Inherited from caller.
594c6fd2807SJeff Garzik  */
595c6fd2807SJeff Garzik 
596c6fd2807SJeff Garzik void ata_tf_from_fis(const u8 *fis, struct ata_taskfile *tf)
597c6fd2807SJeff Garzik {
598c6fd2807SJeff Garzik 	tf->command	= fis[2];	/* status */
599c6fd2807SJeff Garzik 	tf->feature	= fis[3];	/* error */
600c6fd2807SJeff Garzik 
601c6fd2807SJeff Garzik 	tf->lbal	= fis[4];
602c6fd2807SJeff Garzik 	tf->lbam	= fis[5];
603c6fd2807SJeff Garzik 	tf->lbah	= fis[6];
604c6fd2807SJeff Garzik 	tf->device	= fis[7];
605c6fd2807SJeff Garzik 
606c6fd2807SJeff Garzik 	tf->hob_lbal	= fis[8];
607c6fd2807SJeff Garzik 	tf->hob_lbam	= fis[9];
608c6fd2807SJeff Garzik 	tf->hob_lbah	= fis[10];
609c6fd2807SJeff Garzik 
610c6fd2807SJeff Garzik 	tf->nsect	= fis[12];
611c6fd2807SJeff Garzik 	tf->hob_nsect	= fis[13];
612c6fd2807SJeff Garzik }
613c6fd2807SJeff Garzik 
614c6fd2807SJeff Garzik static const u8 ata_rw_cmds[] = {
615c6fd2807SJeff Garzik 	/* pio multi */
616c6fd2807SJeff Garzik 	ATA_CMD_READ_MULTI,
617c6fd2807SJeff Garzik 	ATA_CMD_WRITE_MULTI,
618c6fd2807SJeff Garzik 	ATA_CMD_READ_MULTI_EXT,
619c6fd2807SJeff Garzik 	ATA_CMD_WRITE_MULTI_EXT,
620c6fd2807SJeff Garzik 	0,
621c6fd2807SJeff Garzik 	0,
622c6fd2807SJeff Garzik 	0,
623c6fd2807SJeff Garzik 	ATA_CMD_WRITE_MULTI_FUA_EXT,
624c6fd2807SJeff Garzik 	/* pio */
625c6fd2807SJeff Garzik 	ATA_CMD_PIO_READ,
626c6fd2807SJeff Garzik 	ATA_CMD_PIO_WRITE,
627c6fd2807SJeff Garzik 	ATA_CMD_PIO_READ_EXT,
628c6fd2807SJeff Garzik 	ATA_CMD_PIO_WRITE_EXT,
629c6fd2807SJeff Garzik 	0,
630c6fd2807SJeff Garzik 	0,
631c6fd2807SJeff Garzik 	0,
632c6fd2807SJeff Garzik 	0,
633c6fd2807SJeff Garzik 	/* dma */
634c6fd2807SJeff Garzik 	ATA_CMD_READ,
635c6fd2807SJeff Garzik 	ATA_CMD_WRITE,
636c6fd2807SJeff Garzik 	ATA_CMD_READ_EXT,
637c6fd2807SJeff Garzik 	ATA_CMD_WRITE_EXT,
638c6fd2807SJeff Garzik 	0,
639c6fd2807SJeff Garzik 	0,
640c6fd2807SJeff Garzik 	0,
641c6fd2807SJeff Garzik 	ATA_CMD_WRITE_FUA_EXT
642c6fd2807SJeff Garzik };
643c6fd2807SJeff Garzik 
644c6fd2807SJeff Garzik /**
645c6fd2807SJeff Garzik  *	ata_rwcmd_protocol - set taskfile r/w commands and protocol
646bd056d7eSTejun Heo  *	@tf: command to examine and configure
647bd056d7eSTejun Heo  *	@dev: device tf belongs to
648c6fd2807SJeff Garzik  *
649c6fd2807SJeff Garzik  *	Examine the device configuration and tf->flags to calculate
650c6fd2807SJeff Garzik  *	the proper read/write commands and protocol to use.
651c6fd2807SJeff Garzik  *
652c6fd2807SJeff Garzik  *	LOCKING:
653c6fd2807SJeff Garzik  *	caller.
654c6fd2807SJeff Garzik  */
655bd056d7eSTejun Heo static int ata_rwcmd_protocol(struct ata_taskfile *tf, struct ata_device *dev)
656c6fd2807SJeff Garzik {
657c6fd2807SJeff Garzik 	u8 cmd;
658c6fd2807SJeff Garzik 
659c6fd2807SJeff Garzik 	int index, fua, lba48, write;
660c6fd2807SJeff Garzik 
661c6fd2807SJeff Garzik 	fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
662c6fd2807SJeff Garzik 	lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
663c6fd2807SJeff Garzik 	write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
664c6fd2807SJeff Garzik 
665c6fd2807SJeff Garzik 	if (dev->flags & ATA_DFLAG_PIO) {
666c6fd2807SJeff Garzik 		tf->protocol = ATA_PROT_PIO;
667c6fd2807SJeff Garzik 		index = dev->multi_count ? 0 : 8;
6689af5c9c9STejun Heo 	} else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) {
669c6fd2807SJeff Garzik 		/* Unable to use DMA due to host limitation */
670c6fd2807SJeff Garzik 		tf->protocol = ATA_PROT_PIO;
671c6fd2807SJeff Garzik 		index = dev->multi_count ? 0 : 8;
672c6fd2807SJeff Garzik 	} else {
673c6fd2807SJeff Garzik 		tf->protocol = ATA_PROT_DMA;
674c6fd2807SJeff Garzik 		index = 16;
675c6fd2807SJeff Garzik 	}
676c6fd2807SJeff Garzik 
677c6fd2807SJeff Garzik 	cmd = ata_rw_cmds[index + fua + lba48 + write];
678c6fd2807SJeff Garzik 	if (cmd) {
679c6fd2807SJeff Garzik 		tf->command = cmd;
680c6fd2807SJeff Garzik 		return 0;
681c6fd2807SJeff Garzik 	}
682c6fd2807SJeff Garzik 	return -1;
683c6fd2807SJeff Garzik }
684c6fd2807SJeff Garzik 
685c6fd2807SJeff Garzik /**
68635b649feSTejun Heo  *	ata_tf_read_block - Read block address from ATA taskfile
68735b649feSTejun Heo  *	@tf: ATA taskfile of interest
68835b649feSTejun Heo  *	@dev: ATA device @tf belongs to
68935b649feSTejun Heo  *
69035b649feSTejun Heo  *	LOCKING:
69135b649feSTejun Heo  *	None.
69235b649feSTejun Heo  *
69335b649feSTejun Heo  *	Read block address from @tf.  This function can handle all
69435b649feSTejun Heo  *	three address formats - LBA, LBA48 and CHS.  tf->protocol and
69535b649feSTejun Heo  *	flags select the address format to use.
69635b649feSTejun Heo  *
69735b649feSTejun Heo  *	RETURNS:
69835b649feSTejun Heo  *	Block address read from @tf.
69935b649feSTejun Heo  */
700cffd1ee9SHannes Reinecke u64 ata_tf_read_block(const struct ata_taskfile *tf, struct ata_device *dev)
70135b649feSTejun Heo {
70235b649feSTejun Heo 	u64 block = 0;
70335b649feSTejun Heo 
704fe16d4f2STejun Heo 	if (tf->flags & ATA_TFLAG_LBA) {
70535b649feSTejun Heo 		if (tf->flags & ATA_TFLAG_LBA48) {
70635b649feSTejun Heo 			block |= (u64)tf->hob_lbah << 40;
70735b649feSTejun Heo 			block |= (u64)tf->hob_lbam << 32;
70844901a96SRoland Dreier 			block |= (u64)tf->hob_lbal << 24;
70935b649feSTejun Heo 		} else
71035b649feSTejun Heo 			block |= (tf->device & 0xf) << 24;
71135b649feSTejun Heo 
71235b649feSTejun Heo 		block |= tf->lbah << 16;
71335b649feSTejun Heo 		block |= tf->lbam << 8;
71435b649feSTejun Heo 		block |= tf->lbal;
71535b649feSTejun Heo 	} else {
71635b649feSTejun Heo 		u32 cyl, head, sect;
71735b649feSTejun Heo 
71835b649feSTejun Heo 		cyl = tf->lbam | (tf->lbah << 8);
71935b649feSTejun Heo 		head = tf->device & 0xf;
72035b649feSTejun Heo 		sect = tf->lbal;
72135b649feSTejun Heo 
722ac8672eaSTejun Heo 		if (!sect) {
723a9a79dfeSJoe Perches 			ata_dev_warn(dev,
724a9a79dfeSJoe Perches 				     "device reported invalid CHS sector 0\n");
725cffd1ee9SHannes Reinecke 			return U64_MAX;
726ac8672eaSTejun Heo 		}
727ac8672eaSTejun Heo 
728ac8672eaSTejun Heo 		block = (cyl * dev->heads + head) * dev->sectors + sect - 1;
72935b649feSTejun Heo 	}
73035b649feSTejun Heo 
73135b649feSTejun Heo 	return block;
73235b649feSTejun Heo }
73335b649feSTejun Heo 
73435b649feSTejun Heo /**
735bd056d7eSTejun Heo  *	ata_build_rw_tf - Build ATA taskfile for given read/write request
736bd056d7eSTejun Heo  *	@tf: Target ATA taskfile
737bd056d7eSTejun Heo  *	@dev: ATA device @tf belongs to
738bd056d7eSTejun Heo  *	@block: Block address
739bd056d7eSTejun Heo  *	@n_block: Number of blocks
740bd056d7eSTejun Heo  *	@tf_flags: RW/FUA etc...
741bd056d7eSTejun Heo  *	@tag: tag
7428e061784SAdam Manzanares  *	@class: IO priority class
743bd056d7eSTejun Heo  *
744bd056d7eSTejun Heo  *	LOCKING:
745bd056d7eSTejun Heo  *	None.
746bd056d7eSTejun Heo  *
747bd056d7eSTejun Heo  *	Build ATA taskfile @tf for read/write request described by
748bd056d7eSTejun Heo  *	@block, @n_block, @tf_flags and @tag on @dev.
749bd056d7eSTejun Heo  *
750bd056d7eSTejun Heo  *	RETURNS:
751bd056d7eSTejun Heo  *
752bd056d7eSTejun Heo  *	0 on success, -ERANGE if the request is too large for @dev,
753bd056d7eSTejun Heo  *	-EINVAL if the request is invalid.
754bd056d7eSTejun Heo  */
755bd056d7eSTejun Heo int ata_build_rw_tf(struct ata_taskfile *tf, struct ata_device *dev,
756bd056d7eSTejun Heo 		    u64 block, u32 n_block, unsigned int tf_flags,
7578e061784SAdam Manzanares 		    unsigned int tag, int class)
758bd056d7eSTejun Heo {
759bd056d7eSTejun Heo 	tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
760bd056d7eSTejun Heo 	tf->flags |= tf_flags;
761bd056d7eSTejun Heo 
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 */
188205b83605SGustavo A. R. Silva 		/* fall through */
1883c6fd2807SJeff Garzik 	case ATA_DEV_ATA:
18849162c657SHannes Reinecke 	case ATA_DEV_ZAC:
1885c6fd2807SJeff Garzik 		tf.command = ATA_CMD_ID_ATA;
1886c6fd2807SJeff Garzik 		break;
1887c6fd2807SJeff Garzik 	case ATA_DEV_ATAPI:
1888c6fd2807SJeff Garzik 		tf.command = ATA_CMD_ID_ATAPI;
1889c6fd2807SJeff Garzik 		break;
1890c6fd2807SJeff Garzik 	default:
1891c6fd2807SJeff Garzik 		rc = -ENODEV;
1892c6fd2807SJeff Garzik 		reason = "unsupported class";
1893c6fd2807SJeff Garzik 		goto err_out;
1894c6fd2807SJeff Garzik 	}
1895c6fd2807SJeff Garzik 
1896c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_PIO;
189781afe893STejun Heo 
189881afe893STejun Heo 	/* Some devices choke if TF registers contain garbage.  Make
189981afe893STejun Heo 	 * sure those are properly initialized.
190081afe893STejun Heo 	 */
190181afe893STejun Heo 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
190281afe893STejun Heo 
190381afe893STejun Heo 	/* Device presence detection is unreliable on some
190481afe893STejun Heo 	 * controllers.  Always poll IDENTIFY if available.
190581afe893STejun Heo 	 */
190681afe893STejun Heo 	tf.flags |= ATA_TFLAG_POLLING;
1907c6fd2807SJeff Garzik 
1908963e4975SAlan Cox 	if (ap->ops->read_id)
1909963e4975SAlan Cox 		err_mask = ap->ops->read_id(dev, &tf, id);
1910963e4975SAlan Cox 	else
1911963e4975SAlan Cox 		err_mask = ata_do_dev_read_id(dev, &tf, id);
1912963e4975SAlan Cox 
1913c6fd2807SJeff Garzik 	if (err_mask) {
1914800b3996STejun Heo 		if (err_mask & AC_ERR_NODEV_HINT) {
1915a9a79dfeSJoe Perches 			ata_dev_dbg(dev, "NODEV after polling detection\n");
191655a8e2c8STejun Heo 			return -ENOENT;
191755a8e2c8STejun Heo 		}
191855a8e2c8STejun Heo 
191979b42babSTejun Heo 		if (is_semb) {
1920a9a79dfeSJoe Perches 			ata_dev_info(dev,
1921a9a79dfeSJoe Perches 		     "IDENTIFY failed on device w/ SEMB sig, disabled\n");
192279b42babSTejun Heo 			/* SEMB is not supported yet */
192379b42babSTejun Heo 			*p_class = ATA_DEV_SEMB_UNSUP;
192479b42babSTejun Heo 			return 0;
192579b42babSTejun Heo 		}
192679b42babSTejun Heo 
19271ffc151fSTejun Heo 		if ((err_mask == AC_ERR_DEV) && (tf.feature & ATA_ABORTED)) {
19281ffc151fSTejun Heo 			/* Device or controller might have reported
19291ffc151fSTejun Heo 			 * the wrong device class.  Give a shot at the
19301ffc151fSTejun Heo 			 * other IDENTIFY if the current one is
19311ffc151fSTejun Heo 			 * aborted by the device.
193254936f8bSTejun Heo 			 */
19331ffc151fSTejun Heo 			if (may_fallback) {
193454936f8bSTejun Heo 				may_fallback = 0;
193554936f8bSTejun Heo 
193654936f8bSTejun Heo 				if (class == ATA_DEV_ATA)
193754936f8bSTejun Heo 					class = ATA_DEV_ATAPI;
193854936f8bSTejun Heo 				else
193954936f8bSTejun Heo 					class = ATA_DEV_ATA;
194054936f8bSTejun Heo 				goto retry;
194154936f8bSTejun Heo 			}
194254936f8bSTejun Heo 
19431ffc151fSTejun Heo 			/* Control reaches here iff the device aborted
19441ffc151fSTejun Heo 			 * both flavors of IDENTIFYs which happens
19451ffc151fSTejun Heo 			 * sometimes with phantom devices.
19461ffc151fSTejun Heo 			 */
1947a9a79dfeSJoe Perches 			ata_dev_dbg(dev,
19481ffc151fSTejun Heo 				    "both IDENTIFYs aborted, assuming NODEV\n");
19491ffc151fSTejun Heo 			return -ENOENT;
19501ffc151fSTejun Heo 		}
19511ffc151fSTejun Heo 
1952c6fd2807SJeff Garzik 		rc = -EIO;
1953c6fd2807SJeff Garzik 		reason = "I/O error";
1954c6fd2807SJeff Garzik 		goto err_out;
1955c6fd2807SJeff Garzik 	}
1956c6fd2807SJeff Garzik 
195743c9c591STejun Heo 	if (dev->horkage & ATA_HORKAGE_DUMP_ID) {
1958a9a79dfeSJoe Perches 		ata_dev_dbg(dev, "dumping IDENTIFY data, "
195943c9c591STejun Heo 			    "class=%d may_fallback=%d tried_spinup=%d\n",
196043c9c591STejun Heo 			    class, may_fallback, tried_spinup);
196143c9c591STejun Heo 		print_hex_dump(KERN_DEBUG, "", DUMP_PREFIX_OFFSET,
196243c9c591STejun Heo 			       16, 2, id, ATA_ID_WORDS * sizeof(*id), true);
196343c9c591STejun Heo 	}
196443c9c591STejun Heo 
196554936f8bSTejun Heo 	/* Falling back doesn't make sense if ID data was read
196654936f8bSTejun Heo 	 * successfully at least once.
196754936f8bSTejun Heo 	 */
196854936f8bSTejun Heo 	may_fallback = 0;
196954936f8bSTejun Heo 
1970c6fd2807SJeff Garzik 	swap_buf_le16(id, ATA_ID_WORDS);
1971c6fd2807SJeff Garzik 
1972c6fd2807SJeff Garzik 	/* sanity check */
1973c6fd2807SJeff Garzik 	rc = -EINVAL;
19746070068bSAlan Cox 	reason = "device reports invalid type";
19754a3381feSJeff Garzik 
19769162c657SHannes Reinecke 	if (class == ATA_DEV_ATA || class == ATA_DEV_ZAC) {
19774a3381feSJeff Garzik 		if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
19784a3381feSJeff Garzik 			goto err_out;
1979db63a4c8SAndy Whitcroft 		if (ap->host->flags & ATA_HOST_IGNORE_ATA &&
1980db63a4c8SAndy Whitcroft 							ata_id_is_ata(id)) {
1981db63a4c8SAndy Whitcroft 			ata_dev_dbg(dev,
1982db63a4c8SAndy Whitcroft 				"host indicates ignore ATA devices, ignored\n");
1983db63a4c8SAndy Whitcroft 			return -ENOENT;
1984db63a4c8SAndy Whitcroft 		}
19854a3381feSJeff Garzik 	} else {
19864a3381feSJeff Garzik 		if (ata_id_is_ata(id))
1987c6fd2807SJeff Garzik 			goto err_out;
1988c6fd2807SJeff Garzik 	}
1989c6fd2807SJeff Garzik 
1990169439c2SMark Lord 	if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
1991169439c2SMark Lord 		tried_spinup = 1;
1992169439c2SMark Lord 		/*
1993169439c2SMark Lord 		 * Drive powered-up in standby mode, and requires a specific
1994169439c2SMark Lord 		 * SET_FEATURES spin-up subcommand before it will accept
1995169439c2SMark Lord 		 * anything other than the original IDENTIFY command.
1996169439c2SMark Lord 		 */
1997218f3d30SJeff Garzik 		err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
1998fb0582f9SRyan Power 		if (err_mask && id[2] != 0x738c) {
1999169439c2SMark Lord 			rc = -EIO;
2000169439c2SMark Lord 			reason = "SPINUP failed";
2001169439c2SMark Lord 			goto err_out;
2002169439c2SMark Lord 		}
2003169439c2SMark Lord 		/*
2004169439c2SMark Lord 		 * If the drive initially returned incomplete IDENTIFY info,
2005169439c2SMark Lord 		 * we now must reissue the IDENTIFY command.
2006169439c2SMark Lord 		 */
2007169439c2SMark Lord 		if (id[2] == 0x37c8)
2008169439c2SMark Lord 			goto retry;
2009169439c2SMark Lord 	}
2010169439c2SMark Lord 
20119162c657SHannes Reinecke 	if ((flags & ATA_READID_POSTRESET) &&
20129162c657SHannes Reinecke 	    (class == ATA_DEV_ATA || class == ATA_DEV_ZAC)) {
2013c6fd2807SJeff Garzik 		/*
2014c6fd2807SJeff Garzik 		 * The exact sequence expected by certain pre-ATA4 drives is:
2015c6fd2807SJeff Garzik 		 * SRST RESET
201650a99018SAlan Cox 		 * IDENTIFY (optional in early ATA)
201750a99018SAlan Cox 		 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
2018c6fd2807SJeff Garzik 		 * anything else..
2019c6fd2807SJeff Garzik 		 * Some drives were very specific about that exact sequence.
202050a99018SAlan Cox 		 *
202150a99018SAlan Cox 		 * Note that ATA4 says lba is mandatory so the second check
2022c9404c9cSAdam Buchbinder 		 * should never trigger.
2023c6fd2807SJeff Garzik 		 */
2024c6fd2807SJeff Garzik 		if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
2025c6fd2807SJeff Garzik 			err_mask = ata_dev_init_params(dev, id[3], id[6]);
2026c6fd2807SJeff Garzik 			if (err_mask) {
2027c6fd2807SJeff Garzik 				rc = -EIO;
2028c6fd2807SJeff Garzik 				reason = "INIT_DEV_PARAMS failed";
2029c6fd2807SJeff Garzik 				goto err_out;
2030c6fd2807SJeff Garzik 			}
2031c6fd2807SJeff Garzik 
2032c6fd2807SJeff Garzik 			/* current CHS translation info (id[53-58]) might be
2033c6fd2807SJeff Garzik 			 * changed. reread the identify device info.
2034c6fd2807SJeff Garzik 			 */
2035bff04647STejun Heo 			flags &= ~ATA_READID_POSTRESET;
2036c6fd2807SJeff Garzik 			goto retry;
2037c6fd2807SJeff Garzik 		}
2038c6fd2807SJeff Garzik 	}
2039c6fd2807SJeff Garzik 
2040c6fd2807SJeff Garzik 	*p_class = class;
2041c6fd2807SJeff Garzik 
2042c6fd2807SJeff Garzik 	return 0;
2043c6fd2807SJeff Garzik 
2044c6fd2807SJeff Garzik  err_out:
2045c6fd2807SJeff Garzik 	if (ata_msg_warn(ap))
2046a9a79dfeSJoe Perches 		ata_dev_warn(dev, "failed to IDENTIFY (%s, err_mask=0x%x)\n",
2047a9a79dfeSJoe Perches 			     reason, err_mask);
2048c6fd2807SJeff Garzik 	return rc;
2049c6fd2807SJeff Garzik }
2050c6fd2807SJeff Garzik 
2051f01f62c2SChristoph Hellwig /**
2052f01f62c2SChristoph Hellwig  *	ata_read_log_page - read a specific log page
2053f01f62c2SChristoph Hellwig  *	@dev: target device
2054f01f62c2SChristoph Hellwig  *	@log: log to read
2055f01f62c2SChristoph Hellwig  *	@page: page to read
2056f01f62c2SChristoph Hellwig  *	@buf: buffer to store read page
2057f01f62c2SChristoph Hellwig  *	@sectors: number of sectors to read
2058f01f62c2SChristoph Hellwig  *
2059f01f62c2SChristoph Hellwig  *	Read log page using READ_LOG_EXT command.
2060f01f62c2SChristoph Hellwig  *
2061f01f62c2SChristoph Hellwig  *	LOCKING:
2062f01f62c2SChristoph Hellwig  *	Kernel thread context (may sleep).
2063f01f62c2SChristoph Hellwig  *
2064f01f62c2SChristoph Hellwig  *	RETURNS:
2065f01f62c2SChristoph Hellwig  *	0 on success, AC_ERR_* mask otherwise.
2066f01f62c2SChristoph Hellwig  */
2067f01f62c2SChristoph Hellwig unsigned int ata_read_log_page(struct ata_device *dev, u8 log,
2068f01f62c2SChristoph Hellwig 			       u8 page, void *buf, unsigned int sectors)
2069f01f62c2SChristoph Hellwig {
2070f01f62c2SChristoph Hellwig 	unsigned long ap_flags = dev->link->ap->flags;
2071f01f62c2SChristoph Hellwig 	struct ata_taskfile tf;
2072f01f62c2SChristoph Hellwig 	unsigned int err_mask;
2073f01f62c2SChristoph Hellwig 	bool dma = false;
2074f01f62c2SChristoph Hellwig 
2075f01f62c2SChristoph Hellwig 	DPRINTK("read log page - log 0x%x, page 0x%x\n", log, page);
2076f01f62c2SChristoph Hellwig 
2077f01f62c2SChristoph Hellwig 	/*
2078f01f62c2SChristoph Hellwig 	 * Return error without actually issuing the command on controllers
2079f01f62c2SChristoph Hellwig 	 * which e.g. lockup on a read log page.
2080f01f62c2SChristoph Hellwig 	 */
2081f01f62c2SChristoph Hellwig 	if (ap_flags & ATA_FLAG_NO_LOG_PAGE)
2082f01f62c2SChristoph Hellwig 		return AC_ERR_DEV;
2083f01f62c2SChristoph Hellwig 
2084f01f62c2SChristoph Hellwig retry:
2085f01f62c2SChristoph Hellwig 	ata_tf_init(dev, &tf);
2086f01f62c2SChristoph Hellwig 	if (dev->dma_mode && ata_id_has_read_log_dma_ext(dev->id) &&
20877cfdfdc8SDamien Le Moal 	    !(dev->horkage & ATA_HORKAGE_NO_DMA_LOG)) {
2088f01f62c2SChristoph Hellwig 		tf.command = ATA_CMD_READ_LOG_DMA_EXT;
2089f01f62c2SChristoph Hellwig 		tf.protocol = ATA_PROT_DMA;
2090f01f62c2SChristoph Hellwig 		dma = true;
2091f01f62c2SChristoph Hellwig 	} else {
2092f01f62c2SChristoph Hellwig 		tf.command = ATA_CMD_READ_LOG_EXT;
2093f01f62c2SChristoph Hellwig 		tf.protocol = ATA_PROT_PIO;
2094f01f62c2SChristoph Hellwig 		dma = false;
2095f01f62c2SChristoph Hellwig 	}
2096f01f62c2SChristoph Hellwig 	tf.lbal = log;
2097f01f62c2SChristoph Hellwig 	tf.lbam = page;
2098f01f62c2SChristoph Hellwig 	tf.nsect = sectors;
2099f01f62c2SChristoph Hellwig 	tf.hob_nsect = sectors >> 8;
2100f01f62c2SChristoph Hellwig 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
2101f01f62c2SChristoph Hellwig 
2102f01f62c2SChristoph Hellwig 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
2103f01f62c2SChristoph Hellwig 				     buf, sectors * ATA_SECT_SIZE, 0);
2104f01f62c2SChristoph Hellwig 
2105f01f62c2SChristoph Hellwig 	if (err_mask && dma) {
21067cfdfdc8SDamien Le Moal 		dev->horkage |= ATA_HORKAGE_NO_DMA_LOG;
21077cfdfdc8SDamien Le Moal 		ata_dev_warn(dev, "READ LOG DMA EXT failed, trying PIO\n");
2108f01f62c2SChristoph Hellwig 		goto retry;
2109f01f62c2SChristoph Hellwig 	}
2110f01f62c2SChristoph Hellwig 
2111f01f62c2SChristoph Hellwig 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
2112f01f62c2SChristoph Hellwig 	return err_mask;
2113f01f62c2SChristoph Hellwig }
2114f01f62c2SChristoph Hellwig 
2115efe205a3SChristoph Hellwig static bool ata_log_supported(struct ata_device *dev, u8 log)
2116efe205a3SChristoph Hellwig {
2117efe205a3SChristoph Hellwig 	struct ata_port *ap = dev->link->ap;
2118efe205a3SChristoph Hellwig 
2119efe205a3SChristoph Hellwig 	if (ata_read_log_page(dev, ATA_LOG_DIRECTORY, 0, ap->sector_buf, 1))
2120efe205a3SChristoph Hellwig 		return false;
2121efe205a3SChristoph Hellwig 	return get_unaligned_le16(&ap->sector_buf[log * 2]) ? true : false;
2122efe205a3SChristoph Hellwig }
2123efe205a3SChristoph Hellwig 
2124a0fd2454SChristoph Hellwig static bool ata_identify_page_supported(struct ata_device *dev, u8 page)
2125a0fd2454SChristoph Hellwig {
2126a0fd2454SChristoph Hellwig 	struct ata_port *ap = dev->link->ap;
2127a0fd2454SChristoph Hellwig 	unsigned int err, i;
2128a0fd2454SChristoph Hellwig 
2129a0fd2454SChristoph Hellwig 	if (!ata_log_supported(dev, ATA_LOG_IDENTIFY_DEVICE)) {
2130a0fd2454SChristoph Hellwig 		ata_dev_warn(dev, "ATA Identify Device Log not supported\n");
2131a0fd2454SChristoph Hellwig 		return false;
2132a0fd2454SChristoph Hellwig 	}
2133a0fd2454SChristoph Hellwig 
2134a0fd2454SChristoph Hellwig 	/*
2135a0fd2454SChristoph Hellwig 	 * Read IDENTIFY DEVICE data log, page 0, to figure out if the page is
2136a0fd2454SChristoph Hellwig 	 * supported.
2137a0fd2454SChristoph Hellwig 	 */
2138a0fd2454SChristoph Hellwig 	err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, 0, ap->sector_buf,
2139a0fd2454SChristoph Hellwig 				1);
2140a0fd2454SChristoph Hellwig 	if (err) {
2141a0fd2454SChristoph Hellwig 		ata_dev_info(dev,
2142a0fd2454SChristoph Hellwig 			     "failed to get Device Identify Log Emask 0x%x\n",
2143a0fd2454SChristoph Hellwig 			     err);
2144a0fd2454SChristoph Hellwig 		return false;
2145a0fd2454SChristoph Hellwig 	}
2146a0fd2454SChristoph Hellwig 
2147a0fd2454SChristoph Hellwig 	for (i = 0; i < ap->sector_buf[8]; i++) {
2148a0fd2454SChristoph Hellwig 		if (ap->sector_buf[9 + i] == page)
2149a0fd2454SChristoph Hellwig 			return true;
2150a0fd2454SChristoph Hellwig 	}
2151a0fd2454SChristoph Hellwig 
2152a0fd2454SChristoph Hellwig 	return false;
2153a0fd2454SChristoph Hellwig }
2154a0fd2454SChristoph Hellwig 
21559062712fSTejun Heo static int ata_do_link_spd_horkage(struct ata_device *dev)
21569062712fSTejun Heo {
21579062712fSTejun Heo 	struct ata_link *plink = ata_dev_phys_link(dev);
21589062712fSTejun Heo 	u32 target, target_limit;
21599062712fSTejun Heo 
21609062712fSTejun Heo 	if (!sata_scr_valid(plink))
21619062712fSTejun Heo 		return 0;
21629062712fSTejun Heo 
21639062712fSTejun Heo 	if (dev->horkage & ATA_HORKAGE_1_5_GBPS)
21649062712fSTejun Heo 		target = 1;
21659062712fSTejun Heo 	else
21669062712fSTejun Heo 		return 0;
21679062712fSTejun Heo 
21689062712fSTejun Heo 	target_limit = (1 << target) - 1;
21699062712fSTejun Heo 
21709062712fSTejun Heo 	/* if already on stricter limit, no need to push further */
21719062712fSTejun Heo 	if (plink->sata_spd_limit <= target_limit)
21729062712fSTejun Heo 		return 0;
21739062712fSTejun Heo 
21749062712fSTejun Heo 	plink->sata_spd_limit = target_limit;
21759062712fSTejun Heo 
21769062712fSTejun Heo 	/* Request another EH round by returning -EAGAIN if link is
21779062712fSTejun Heo 	 * going faster than the target speed.  Forward progress is
21789062712fSTejun Heo 	 * guaranteed by setting sata_spd_limit to target_limit above.
21799062712fSTejun Heo 	 */
21809062712fSTejun Heo 	if (plink->sata_spd > target) {
2181a9a79dfeSJoe Perches 		ata_dev_info(dev, "applying link speed limit horkage to %s\n",
21829062712fSTejun Heo 			     sata_spd_string(target));
21839062712fSTejun Heo 		return -EAGAIN;
21849062712fSTejun Heo 	}
21859062712fSTejun Heo 	return 0;
21869062712fSTejun Heo }
21879062712fSTejun Heo 
2188c6fd2807SJeff Garzik static inline u8 ata_dev_knobble(struct ata_device *dev)
2189c6fd2807SJeff Garzik {
21909af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
21919ce8e307SJens Axboe 
21929ce8e307SJens Axboe 	if (ata_dev_blacklisted(dev) & ATA_HORKAGE_BRIDGE_OK)
21939ce8e307SJens Axboe 		return 0;
21949ce8e307SJens Axboe 
21959af5c9c9STejun Heo 	return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
2196c6fd2807SJeff Garzik }
2197c6fd2807SJeff Garzik 
21985a233551SHannes Reinecke static void ata_dev_config_ncq_send_recv(struct ata_device *dev)
21995a233551SHannes Reinecke {
22005a233551SHannes Reinecke 	struct ata_port *ap = dev->link->ap;
22015a233551SHannes Reinecke 	unsigned int err_mask;
22025a233551SHannes Reinecke 
2203efe205a3SChristoph Hellwig 	if (!ata_log_supported(dev, ATA_LOG_NCQ_SEND_RECV)) {
2204efe205a3SChristoph Hellwig 		ata_dev_warn(dev, "NCQ Send/Recv Log not supported\n");
2205fe5af0ccSHannes Reinecke 		return;
2206fe5af0ccSHannes Reinecke 	}
22075a233551SHannes Reinecke 	err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_SEND_RECV,
22085a233551SHannes Reinecke 				     0, ap->sector_buf, 1);
22095a233551SHannes Reinecke 	if (err_mask) {
22105a233551SHannes Reinecke 		ata_dev_dbg(dev,
22115a233551SHannes Reinecke 			    "failed to get NCQ Send/Recv Log Emask 0x%x\n",
22125a233551SHannes Reinecke 			    err_mask);
22135a233551SHannes Reinecke 	} else {
22145a233551SHannes Reinecke 		u8 *cmds = dev->ncq_send_recv_cmds;
22155a233551SHannes Reinecke 
22165a233551SHannes Reinecke 		dev->flags |= ATA_DFLAG_NCQ_SEND_RECV;
22175a233551SHannes Reinecke 		memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_SEND_RECV_SIZE);
22185a233551SHannes Reinecke 
22195a233551SHannes Reinecke 		if (dev->horkage & ATA_HORKAGE_NO_NCQ_TRIM) {
22205a233551SHannes Reinecke 			ata_dev_dbg(dev, "disabling queued TRIM support\n");
22215a233551SHannes Reinecke 			cmds[ATA_LOG_NCQ_SEND_RECV_DSM_OFFSET] &=
22225a233551SHannes Reinecke 				~ATA_LOG_NCQ_SEND_RECV_DSM_TRIM;
22235a233551SHannes Reinecke 		}
22245a233551SHannes Reinecke 	}
22255a233551SHannes Reinecke }
22265a233551SHannes Reinecke 
2227284b3b77SHannes Reinecke static void ata_dev_config_ncq_non_data(struct ata_device *dev)
2228284b3b77SHannes Reinecke {
2229284b3b77SHannes Reinecke 	struct ata_port *ap = dev->link->ap;
2230284b3b77SHannes Reinecke 	unsigned int err_mask;
2231284b3b77SHannes Reinecke 
2232efe205a3SChristoph Hellwig 	if (!ata_log_supported(dev, ATA_LOG_NCQ_NON_DATA)) {
2233284b3b77SHannes Reinecke 		ata_dev_warn(dev,
2234284b3b77SHannes Reinecke 			     "NCQ Send/Recv Log not supported\n");
2235284b3b77SHannes Reinecke 		return;
2236284b3b77SHannes Reinecke 	}
2237284b3b77SHannes Reinecke 	err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_NON_DATA,
2238284b3b77SHannes Reinecke 				     0, ap->sector_buf, 1);
2239284b3b77SHannes Reinecke 	if (err_mask) {
2240284b3b77SHannes Reinecke 		ata_dev_dbg(dev,
2241284b3b77SHannes Reinecke 			    "failed to get NCQ Non-Data Log Emask 0x%x\n",
2242284b3b77SHannes Reinecke 			    err_mask);
2243284b3b77SHannes Reinecke 	} else {
2244284b3b77SHannes Reinecke 		u8 *cmds = dev->ncq_non_data_cmds;
2245284b3b77SHannes Reinecke 
2246284b3b77SHannes Reinecke 		memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_NON_DATA_SIZE);
2247284b3b77SHannes Reinecke 	}
2248284b3b77SHannes Reinecke }
2249284b3b77SHannes Reinecke 
22508e061784SAdam Manzanares static void ata_dev_config_ncq_prio(struct ata_device *dev)
22518e061784SAdam Manzanares {
22528e061784SAdam Manzanares 	struct ata_port *ap = dev->link->ap;
22538e061784SAdam Manzanares 	unsigned int err_mask;
22548e061784SAdam Manzanares 
22559f56eca3SAdam Manzanares 	if (!(dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLE)) {
22569f56eca3SAdam Manzanares 		dev->flags &= ~ATA_DFLAG_NCQ_PRIO;
22579f56eca3SAdam Manzanares 		return;
22589f56eca3SAdam Manzanares 	}
22599f56eca3SAdam Manzanares 
22608e061784SAdam Manzanares 	err_mask = ata_read_log_page(dev,
22611d51d5f3SChristoph Hellwig 				     ATA_LOG_IDENTIFY_DEVICE,
22628e061784SAdam Manzanares 				     ATA_LOG_SATA_SETTINGS,
22638e061784SAdam Manzanares 				     ap->sector_buf,
22648e061784SAdam Manzanares 				     1);
22658e061784SAdam Manzanares 	if (err_mask) {
22668e061784SAdam Manzanares 		ata_dev_dbg(dev,
22678e061784SAdam Manzanares 			    "failed to get Identify Device data, Emask 0x%x\n",
22688e061784SAdam Manzanares 			    err_mask);
22698e061784SAdam Manzanares 		return;
22708e061784SAdam Manzanares 	}
22718e061784SAdam Manzanares 
22729f56eca3SAdam Manzanares 	if (ap->sector_buf[ATA_LOG_NCQ_PRIO_OFFSET] & BIT(3)) {
22738e061784SAdam Manzanares 		dev->flags |= ATA_DFLAG_NCQ_PRIO;
22749f56eca3SAdam Manzanares 	} else {
22759f56eca3SAdam Manzanares 		dev->flags &= ~ATA_DFLAG_NCQ_PRIO;
22768e061784SAdam Manzanares 		ata_dev_dbg(dev, "SATA page does not support priority\n");
22779f56eca3SAdam Manzanares 	}
22788e061784SAdam Manzanares 
22798e061784SAdam Manzanares }
22808e061784SAdam Manzanares 
2281388539f3SShaohua Li static int ata_dev_config_ncq(struct ata_device *dev,
2282c6fd2807SJeff Garzik 			       char *desc, size_t desc_sz)
2283c6fd2807SJeff Garzik {
22849af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
2285c6fd2807SJeff Garzik 	int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
2286388539f3SShaohua Li 	unsigned int err_mask;
2287388539f3SShaohua Li 	char *aa_desc = "";
2288c6fd2807SJeff Garzik 
2289c6fd2807SJeff Garzik 	if (!ata_id_has_ncq(dev->id)) {
2290c6fd2807SJeff Garzik 		desc[0] = '\0';
2291388539f3SShaohua Li 		return 0;
2292c6fd2807SJeff Garzik 	}
229375683fe7STejun Heo 	if (dev->horkage & ATA_HORKAGE_NONCQ) {
22946919a0a6SAlan Cox 		snprintf(desc, desc_sz, "NCQ (not used)");
2295388539f3SShaohua Li 		return 0;
22966919a0a6SAlan Cox 	}
2297c6fd2807SJeff Garzik 	if (ap->flags & ATA_FLAG_NCQ) {
2298cca3974eSJeff Garzik 		hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE - 1);
2299c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_NCQ;
2300c6fd2807SJeff Garzik 	}
2301c6fd2807SJeff Garzik 
2302388539f3SShaohua Li 	if (!(dev->horkage & ATA_HORKAGE_BROKEN_FPDMA_AA) &&
2303388539f3SShaohua Li 		(ap->flags & ATA_FLAG_FPDMA_AA) &&
2304388539f3SShaohua Li 		ata_id_has_fpdma_aa(dev->id)) {
2305388539f3SShaohua Li 		err_mask = ata_dev_set_feature(dev, SETFEATURES_SATA_ENABLE,
2306388539f3SShaohua Li 			SATA_FPDMA_AA);
2307388539f3SShaohua Li 		if (err_mask) {
2308a9a79dfeSJoe Perches 			ata_dev_err(dev,
2309a9a79dfeSJoe Perches 				    "failed to enable AA (error_mask=0x%x)\n",
2310a9a79dfeSJoe Perches 				    err_mask);
2311388539f3SShaohua Li 			if (err_mask != AC_ERR_DEV) {
2312388539f3SShaohua Li 				dev->horkage |= ATA_HORKAGE_BROKEN_FPDMA_AA;
2313388539f3SShaohua Li 				return -EIO;
2314388539f3SShaohua Li 			}
2315388539f3SShaohua Li 		} else
2316388539f3SShaohua Li 			aa_desc = ", AA";
2317388539f3SShaohua Li 	}
2318388539f3SShaohua Li 
2319c6fd2807SJeff Garzik 	if (hdepth >= ddepth)
2320388539f3SShaohua Li 		snprintf(desc, desc_sz, "NCQ (depth %d)%s", ddepth, aa_desc);
2321c6fd2807SJeff Garzik 	else
2322388539f3SShaohua Li 		snprintf(desc, desc_sz, "NCQ (depth %d/%d)%s", hdepth,
2323388539f3SShaohua Li 			ddepth, aa_desc);
2324ed36911cSMarc Carino 
2325284b3b77SHannes Reinecke 	if ((ap->flags & ATA_FLAG_FPDMA_AUX)) {
2326284b3b77SHannes Reinecke 		if (ata_id_has_ncq_send_and_recv(dev->id))
23275a233551SHannes Reinecke 			ata_dev_config_ncq_send_recv(dev);
2328284b3b77SHannes Reinecke 		if (ata_id_has_ncq_non_data(dev->id))
2329284b3b77SHannes Reinecke 			ata_dev_config_ncq_non_data(dev);
23308e061784SAdam Manzanares 		if (ata_id_has_ncq_prio(dev->id))
23318e061784SAdam Manzanares 			ata_dev_config_ncq_prio(dev);
2332ed36911cSMarc Carino 	}
2333ed36911cSMarc Carino 
2334388539f3SShaohua Li 	return 0;
2335c6fd2807SJeff Garzik }
2336c6fd2807SJeff Garzik 
2337e87fd28cSHannes Reinecke static void ata_dev_config_sense_reporting(struct ata_device *dev)
2338e87fd28cSHannes Reinecke {
2339e87fd28cSHannes Reinecke 	unsigned int err_mask;
2340e87fd28cSHannes Reinecke 
2341e87fd28cSHannes Reinecke 	if (!ata_id_has_sense_reporting(dev->id))
2342e87fd28cSHannes Reinecke 		return;
2343e87fd28cSHannes Reinecke 
2344e87fd28cSHannes Reinecke 	if (ata_id_sense_reporting_enabled(dev->id))
2345e87fd28cSHannes Reinecke 		return;
2346e87fd28cSHannes Reinecke 
2347e87fd28cSHannes Reinecke 	err_mask = ata_dev_set_feature(dev, SETFEATURE_SENSE_DATA, 0x1);
2348e87fd28cSHannes Reinecke 	if (err_mask) {
2349e87fd28cSHannes Reinecke 		ata_dev_dbg(dev,
2350e87fd28cSHannes Reinecke 			    "failed to enable Sense Data Reporting, Emask 0x%x\n",
2351e87fd28cSHannes Reinecke 			    err_mask);
2352e87fd28cSHannes Reinecke 	}
2353e87fd28cSHannes Reinecke }
2354e87fd28cSHannes Reinecke 
23556d1003aeSHannes Reinecke static void ata_dev_config_zac(struct ata_device *dev)
23566d1003aeSHannes Reinecke {
23576d1003aeSHannes Reinecke 	struct ata_port *ap = dev->link->ap;
23586d1003aeSHannes Reinecke 	unsigned int err_mask;
23596d1003aeSHannes Reinecke 	u8 *identify_buf = ap->sector_buf;
23606d1003aeSHannes Reinecke 
23616d1003aeSHannes Reinecke 	dev->zac_zones_optimal_open = U32_MAX;
23626d1003aeSHannes Reinecke 	dev->zac_zones_optimal_nonseq = U32_MAX;
23636d1003aeSHannes Reinecke 	dev->zac_zones_max_open = U32_MAX;
23646d1003aeSHannes Reinecke 
23656d1003aeSHannes Reinecke 	/*
23666d1003aeSHannes Reinecke 	 * Always set the 'ZAC' flag for Host-managed devices.
23676d1003aeSHannes Reinecke 	 */
23686d1003aeSHannes Reinecke 	if (dev->class == ATA_DEV_ZAC)
23696d1003aeSHannes Reinecke 		dev->flags |= ATA_DFLAG_ZAC;
23706d1003aeSHannes Reinecke 	else if (ata_id_zoned_cap(dev->id) == 0x01)
23716d1003aeSHannes Reinecke 		/*
23726d1003aeSHannes Reinecke 		 * Check for host-aware devices.
23736d1003aeSHannes Reinecke 		 */
23746d1003aeSHannes Reinecke 		dev->flags |= ATA_DFLAG_ZAC;
23756d1003aeSHannes Reinecke 
23766d1003aeSHannes Reinecke 	if (!(dev->flags & ATA_DFLAG_ZAC))
23776d1003aeSHannes Reinecke 		return;
23786d1003aeSHannes Reinecke 
2379a0fd2454SChristoph Hellwig 	if (!ata_identify_page_supported(dev, ATA_LOG_ZONED_INFORMATION)) {
23806d1003aeSHannes Reinecke 		ata_dev_warn(dev,
23816d1003aeSHannes Reinecke 			     "ATA Zoned Information Log not supported\n");
23826d1003aeSHannes Reinecke 		return;
23836d1003aeSHannes Reinecke 	}
23846d1003aeSHannes Reinecke 
23856d1003aeSHannes Reinecke 	/*
23866d1003aeSHannes Reinecke 	 * Read IDENTIFY DEVICE data log, page 9 (Zoned-device information)
23876d1003aeSHannes Reinecke 	 */
23881d51d5f3SChristoph Hellwig 	err_mask = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE,
23896d1003aeSHannes Reinecke 				     ATA_LOG_ZONED_INFORMATION,
23906d1003aeSHannes Reinecke 				     identify_buf, 1);
23916d1003aeSHannes Reinecke 	if (!err_mask) {
23926d1003aeSHannes Reinecke 		u64 zoned_cap, opt_open, opt_nonseq, max_open;
23936d1003aeSHannes Reinecke 
23946d1003aeSHannes Reinecke 		zoned_cap = get_unaligned_le64(&identify_buf[8]);
23956d1003aeSHannes Reinecke 		if ((zoned_cap >> 63))
23966d1003aeSHannes Reinecke 			dev->zac_zoned_cap = (zoned_cap & 1);
23976d1003aeSHannes Reinecke 		opt_open = get_unaligned_le64(&identify_buf[24]);
23986d1003aeSHannes Reinecke 		if ((opt_open >> 63))
23996d1003aeSHannes Reinecke 			dev->zac_zones_optimal_open = (u32)opt_open;
24006d1003aeSHannes Reinecke 		opt_nonseq = get_unaligned_le64(&identify_buf[32]);
24016d1003aeSHannes Reinecke 		if ((opt_nonseq >> 63))
24026d1003aeSHannes Reinecke 			dev->zac_zones_optimal_nonseq = (u32)opt_nonseq;
24036d1003aeSHannes Reinecke 		max_open = get_unaligned_le64(&identify_buf[40]);
24046d1003aeSHannes Reinecke 		if ((max_open >> 63))
24056d1003aeSHannes Reinecke 			dev->zac_zones_max_open = (u32)max_open;
24066d1003aeSHannes Reinecke 	}
24076d1003aeSHannes Reinecke }
24086d1003aeSHannes Reinecke 
2409818831c8SChristoph Hellwig static void ata_dev_config_trusted(struct ata_device *dev)
2410818831c8SChristoph Hellwig {
2411818831c8SChristoph Hellwig 	struct ata_port *ap = dev->link->ap;
2412818831c8SChristoph Hellwig 	u64 trusted_cap;
2413818831c8SChristoph Hellwig 	unsigned int err;
2414818831c8SChristoph Hellwig 
2415e8f11db9SChristoph Hellwig 	if (!ata_id_has_trusted(dev->id))
2416e8f11db9SChristoph Hellwig 		return;
2417e8f11db9SChristoph Hellwig 
2418818831c8SChristoph Hellwig 	if (!ata_identify_page_supported(dev, ATA_LOG_SECURITY)) {
2419818831c8SChristoph Hellwig 		ata_dev_warn(dev,
2420818831c8SChristoph Hellwig 			     "Security Log not supported\n");
2421818831c8SChristoph Hellwig 		return;
2422818831c8SChristoph Hellwig 	}
2423818831c8SChristoph Hellwig 
2424818831c8SChristoph Hellwig 	err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, ATA_LOG_SECURITY,
2425818831c8SChristoph Hellwig 			ap->sector_buf, 1);
2426818831c8SChristoph Hellwig 	if (err) {
2427818831c8SChristoph Hellwig 		ata_dev_dbg(dev,
2428818831c8SChristoph Hellwig 			    "failed to read Security Log, Emask 0x%x\n", err);
2429818831c8SChristoph Hellwig 		return;
2430818831c8SChristoph Hellwig 	}
2431818831c8SChristoph Hellwig 
2432818831c8SChristoph Hellwig 	trusted_cap = get_unaligned_le64(&ap->sector_buf[40]);
2433818831c8SChristoph Hellwig 	if (!(trusted_cap & (1ULL << 63))) {
2434818831c8SChristoph Hellwig 		ata_dev_dbg(dev,
2435818831c8SChristoph Hellwig 			    "Trusted Computing capability qword not valid!\n");
2436818831c8SChristoph Hellwig 		return;
2437818831c8SChristoph Hellwig 	}
2438818831c8SChristoph Hellwig 
2439818831c8SChristoph Hellwig 	if (trusted_cap & (1 << 0))
2440818831c8SChristoph Hellwig 		dev->flags |= ATA_DFLAG_TRUSTED;
2441818831c8SChristoph Hellwig }
2442818831c8SChristoph Hellwig 
2443c6fd2807SJeff Garzik /**
2444c6fd2807SJeff Garzik  *	ata_dev_configure - Configure the specified ATA/ATAPI device
2445c6fd2807SJeff Garzik  *	@dev: Target device to configure
2446c6fd2807SJeff Garzik  *
2447c6fd2807SJeff Garzik  *	Configure @dev according to @dev->id.  Generic and low-level
2448c6fd2807SJeff Garzik  *	driver specific fixups are also applied.
2449c6fd2807SJeff Garzik  *
2450c6fd2807SJeff Garzik  *	LOCKING:
2451c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
2452c6fd2807SJeff Garzik  *
2453c6fd2807SJeff Garzik  *	RETURNS:
2454c6fd2807SJeff Garzik  *	0 on success, -errno otherwise
2455c6fd2807SJeff Garzik  */
2456efdaedc4STejun Heo int ata_dev_configure(struct ata_device *dev)
2457c6fd2807SJeff Garzik {
24589af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
24599af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
24606746544cSTejun Heo 	int print_info = ehc->i.flags & ATA_EHI_PRINTINFO;
2461c6fd2807SJeff Garzik 	const u16 *id = dev->id;
24627dc951aeSTejun Heo 	unsigned long xfer_mask;
246365fe1f0fSShane Huang 	unsigned int err_mask;
2464b352e57dSAlan Cox 	char revbuf[7];		/* XYZ-99\0 */
24653f64f565SEric D. Mudama 	char fwrevbuf[ATA_ID_FW_REV_LEN+1];
24663f64f565SEric D. Mudama 	char modelbuf[ATA_ID_PROD_LEN+1];
2467c6fd2807SJeff Garzik 	int rc;
2468c6fd2807SJeff Garzik 
2469c6fd2807SJeff Garzik 	if (!ata_dev_enabled(dev) && ata_msg_info(ap)) {
2470a9a79dfeSJoe Perches 		ata_dev_info(dev, "%s: ENTER/EXIT -- nodev\n", __func__);
2471c6fd2807SJeff Garzik 		return 0;
2472c6fd2807SJeff Garzik 	}
2473c6fd2807SJeff Garzik 
2474c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2475a9a79dfeSJoe Perches 		ata_dev_dbg(dev, "%s: ENTER\n", __func__);
2476c6fd2807SJeff Garzik 
247775683fe7STejun Heo 	/* set horkage */
247875683fe7STejun Heo 	dev->horkage |= ata_dev_blacklisted(dev);
247933267325STejun Heo 	ata_force_horkage(dev);
248075683fe7STejun Heo 
248150af2fa1STejun Heo 	if (dev->horkage & ATA_HORKAGE_DISABLE) {
2482a9a79dfeSJoe Perches 		ata_dev_info(dev, "unsupported device, disabling\n");
248350af2fa1STejun Heo 		ata_dev_disable(dev);
248450af2fa1STejun Heo 		return 0;
248550af2fa1STejun Heo 	}
248650af2fa1STejun Heo 
24872486fa56STejun Heo 	if ((!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) &&
24882486fa56STejun Heo 	    dev->class == ATA_DEV_ATAPI) {
2489a9a79dfeSJoe Perches 		ata_dev_warn(dev, "WARNING: ATAPI is %s, device ignored\n",
24902486fa56STejun Heo 			     atapi_enabled ? "not supported with this driver"
24912486fa56STejun Heo 			     : "disabled");
24922486fa56STejun Heo 		ata_dev_disable(dev);
24932486fa56STejun Heo 		return 0;
24942486fa56STejun Heo 	}
24952486fa56STejun Heo 
24969062712fSTejun Heo 	rc = ata_do_link_spd_horkage(dev);
24979062712fSTejun Heo 	if (rc)
24989062712fSTejun Heo 		return rc;
24999062712fSTejun Heo 
2500ecd75ad5STejun Heo 	/* some WD SATA-1 drives have issues with LPM, turn on NOLPM for them */
2501ecd75ad5STejun Heo 	if ((dev->horkage & ATA_HORKAGE_WD_BROKEN_LPM) &&
2502ecd75ad5STejun Heo 	    (id[ATA_ID_SATA_CAPABILITY] & 0xe) == 0x2)
2503ecd75ad5STejun Heo 		dev->horkage |= ATA_HORKAGE_NOLPM;
2504ecd75ad5STejun Heo 
2505ecd75ad5STejun Heo 	if (dev->horkage & ATA_HORKAGE_NOLPM) {
2506ecd75ad5STejun Heo 		ata_dev_warn(dev, "LPM support broken, forcing max_power\n");
2507ecd75ad5STejun Heo 		dev->link->ap->target_lpm_policy = ATA_LPM_MAX_POWER;
2508ecd75ad5STejun Heo 	}
2509ecd75ad5STejun Heo 
25106746544cSTejun Heo 	/* let ACPI work its magic */
25116746544cSTejun Heo 	rc = ata_acpi_on_devcfg(dev);
25126746544cSTejun Heo 	if (rc)
25136746544cSTejun Heo 		return rc;
251408573a86SKristen Carlson Accardi 
251505027adcSTejun Heo 	/* massage HPA, do it early as it might change IDENTIFY data */
251605027adcSTejun Heo 	rc = ata_hpa_resize(dev);
251705027adcSTejun Heo 	if (rc)
251805027adcSTejun Heo 		return rc;
251905027adcSTejun Heo 
2520c6fd2807SJeff Garzik 	/* print device capabilities */
2521c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2522a9a79dfeSJoe Perches 		ata_dev_dbg(dev,
2523c6fd2807SJeff Garzik 			    "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
2524c6fd2807SJeff Garzik 			    "85:%04x 86:%04x 87:%04x 88:%04x\n",
25257f5e4e8dSHarvey Harrison 			    __func__,
2526c6fd2807SJeff Garzik 			    id[49], id[82], id[83], id[84],
2527c6fd2807SJeff Garzik 			    id[85], id[86], id[87], id[88]);
2528c6fd2807SJeff Garzik 
2529c6fd2807SJeff Garzik 	/* initialize to-be-configured parameters */
2530c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_CFG_MASK;
2531c6fd2807SJeff Garzik 	dev->max_sectors = 0;
2532c6fd2807SJeff Garzik 	dev->cdb_len = 0;
2533c6fd2807SJeff Garzik 	dev->n_sectors = 0;
2534c6fd2807SJeff Garzik 	dev->cylinders = 0;
2535c6fd2807SJeff Garzik 	dev->heads = 0;
2536c6fd2807SJeff Garzik 	dev->sectors = 0;
2537e18086d6SMark Lord 	dev->multi_count = 0;
2538c6fd2807SJeff Garzik 
2539c6fd2807SJeff Garzik 	/*
2540c6fd2807SJeff Garzik 	 * common ATA, ATAPI feature tests
2541c6fd2807SJeff Garzik 	 */
2542c6fd2807SJeff Garzik 
2543c6fd2807SJeff Garzik 	/* find max transfer mode; for printk only */
2544c6fd2807SJeff Garzik 	xfer_mask = ata_id_xfermask(id);
2545c6fd2807SJeff Garzik 
2546c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2547c6fd2807SJeff Garzik 		ata_dump_id(id);
2548c6fd2807SJeff Garzik 
2549ef143d57SAlbert Lee 	/* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
2550ef143d57SAlbert Lee 	ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
2551ef143d57SAlbert Lee 			sizeof(fwrevbuf));
2552ef143d57SAlbert Lee 
2553ef143d57SAlbert Lee 	ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
2554ef143d57SAlbert Lee 			sizeof(modelbuf));
2555ef143d57SAlbert Lee 
2556c6fd2807SJeff Garzik 	/* ATA-specific feature tests */
25579162c657SHannes Reinecke 	if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) {
2558b352e57dSAlan Cox 		if (ata_id_is_cfa(id)) {
255962afe5d7SSergei Shtylyov 			/* CPRM may make this media unusable */
256062afe5d7SSergei Shtylyov 			if (id[ATA_ID_CFA_KEY_MGMT] & 1)
2561a9a79dfeSJoe Perches 				ata_dev_warn(dev,
2562a9a79dfeSJoe Perches 	"supports DRM functions and may not be fully accessible\n");
2563b352e57dSAlan Cox 			snprintf(revbuf, 7, "CFA");
2564ae8d4ee7SAlan Cox 		} else {
2565b352e57dSAlan Cox 			snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
2566ae8d4ee7SAlan Cox 			/* Warn the user if the device has TPM extensions */
2567ae8d4ee7SAlan Cox 			if (ata_id_has_tpm(id))
2568a9a79dfeSJoe Perches 				ata_dev_warn(dev,
2569a9a79dfeSJoe Perches 	"supports DRM functions and may not be fully accessible\n");
2570ae8d4ee7SAlan Cox 		}
2571b352e57dSAlan Cox 
2572c6fd2807SJeff Garzik 		dev->n_sectors = ata_id_n_sectors(id);
2573c6fd2807SJeff Garzik 
2574e18086d6SMark Lord 		/* get current R/W Multiple count setting */
2575e18086d6SMark Lord 		if ((dev->id[47] >> 8) == 0x80 && (dev->id[59] & 0x100)) {
2576e18086d6SMark Lord 			unsigned int max = dev->id[47] & 0xff;
2577e18086d6SMark Lord 			unsigned int cnt = dev->id[59] & 0xff;
2578e18086d6SMark Lord 			/* only recognize/allow powers of two here */
2579e18086d6SMark Lord 			if (is_power_of_2(max) && is_power_of_2(cnt))
2580e18086d6SMark Lord 				if (cnt <= max)
2581e18086d6SMark Lord 					dev->multi_count = cnt;
2582e18086d6SMark Lord 		}
25833f64f565SEric D. Mudama 
2584c6fd2807SJeff Garzik 		if (ata_id_has_lba(id)) {
2585c6fd2807SJeff Garzik 			const char *lba_desc;
2586388539f3SShaohua Li 			char ncq_desc[24];
2587c6fd2807SJeff Garzik 
2588c6fd2807SJeff Garzik 			lba_desc = "LBA";
2589c6fd2807SJeff Garzik 			dev->flags |= ATA_DFLAG_LBA;
2590c6fd2807SJeff Garzik 			if (ata_id_has_lba48(id)) {
2591c6fd2807SJeff Garzik 				dev->flags |= ATA_DFLAG_LBA48;
2592c6fd2807SJeff Garzik 				lba_desc = "LBA48";
25936fc49adbSTejun Heo 
25946fc49adbSTejun Heo 				if (dev->n_sectors >= (1UL << 28) &&
25956fc49adbSTejun Heo 				    ata_id_has_flush_ext(id))
25966fc49adbSTejun Heo 					dev->flags |= ATA_DFLAG_FLUSH_EXT;
2597c6fd2807SJeff Garzik 			}
2598c6fd2807SJeff Garzik 
2599c6fd2807SJeff Garzik 			/* config NCQ */
2600388539f3SShaohua Li 			rc = ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
2601388539f3SShaohua Li 			if (rc)
2602388539f3SShaohua Li 				return rc;
2603c6fd2807SJeff Garzik 
2604c6fd2807SJeff Garzik 			/* print device info to dmesg */
26053f64f565SEric D. Mudama 			if (ata_msg_drv(ap) && print_info) {
2606a9a79dfeSJoe Perches 				ata_dev_info(dev, "%s: %s, %s, max %s\n",
26073f64f565SEric D. Mudama 					     revbuf, modelbuf, fwrevbuf,
26083f64f565SEric D. Mudama 					     ata_mode_string(xfer_mask));
2609a9a79dfeSJoe Perches 				ata_dev_info(dev,
2610a9a79dfeSJoe Perches 					     "%llu sectors, multi %u: %s %s\n",
2611c6fd2807SJeff Garzik 					(unsigned long long)dev->n_sectors,
26123f64f565SEric D. Mudama 					dev->multi_count, lba_desc, ncq_desc);
26133f64f565SEric D. Mudama 			}
2614c6fd2807SJeff Garzik 		} else {
2615c6fd2807SJeff Garzik 			/* CHS */
2616c6fd2807SJeff Garzik 
2617c6fd2807SJeff Garzik 			/* Default translation */
2618c6fd2807SJeff Garzik 			dev->cylinders	= id[1];
2619c6fd2807SJeff Garzik 			dev->heads	= id[3];
2620c6fd2807SJeff Garzik 			dev->sectors	= id[6];
2621c6fd2807SJeff Garzik 
2622c6fd2807SJeff Garzik 			if (ata_id_current_chs_valid(id)) {
2623c6fd2807SJeff Garzik 				/* Current CHS translation is valid. */
2624c6fd2807SJeff Garzik 				dev->cylinders = id[54];
2625c6fd2807SJeff Garzik 				dev->heads     = id[55];
2626c6fd2807SJeff Garzik 				dev->sectors   = id[56];
2627c6fd2807SJeff Garzik 			}
2628c6fd2807SJeff Garzik 
2629c6fd2807SJeff Garzik 			/* print device info to dmesg */
26303f64f565SEric D. Mudama 			if (ata_msg_drv(ap) && print_info) {
2631a9a79dfeSJoe Perches 				ata_dev_info(dev, "%s: %s, %s, max %s\n",
26323f64f565SEric D. Mudama 					     revbuf,	modelbuf, fwrevbuf,
26333f64f565SEric D. Mudama 					     ata_mode_string(xfer_mask));
2634a9a79dfeSJoe Perches 				ata_dev_info(dev,
2635a9a79dfeSJoe Perches 					     "%llu sectors, multi %u, CHS %u/%u/%u\n",
26363f64f565SEric D. Mudama 					     (unsigned long long)dev->n_sectors,
26373f64f565SEric D. Mudama 					     dev->multi_count, dev->cylinders,
26383f64f565SEric D. Mudama 					     dev->heads, dev->sectors);
26393f64f565SEric D. Mudama 			}
2640c6fd2807SJeff Garzik 		}
2641c6fd2807SJeff Garzik 
2642803739d2SShane Huang 		/* Check and mark DevSlp capability. Get DevSlp timing variables
2643803739d2SShane Huang 		 * from SATA Settings page of Identify Device Data Log.
264465fe1f0fSShane Huang 		 */
2645803739d2SShane Huang 		if (ata_id_has_devslp(dev->id)) {
26468e725c7fSDavid Woodhouse 			u8 *sata_setting = ap->sector_buf;
2647803739d2SShane Huang 			int i, j;
2648803739d2SShane Huang 
2649803739d2SShane Huang 			dev->flags |= ATA_DFLAG_DEVSLP;
265065fe1f0fSShane Huang 			err_mask = ata_read_log_page(dev,
26511d51d5f3SChristoph Hellwig 						     ATA_LOG_IDENTIFY_DEVICE,
265265fe1f0fSShane Huang 						     ATA_LOG_SATA_SETTINGS,
2653803739d2SShane Huang 						     sata_setting,
265465fe1f0fSShane Huang 						     1);
265565fe1f0fSShane Huang 			if (err_mask)
265665fe1f0fSShane Huang 				ata_dev_dbg(dev,
265765fe1f0fSShane Huang 					    "failed to get Identify Device Data, Emask 0x%x\n",
265865fe1f0fSShane Huang 					    err_mask);
2659803739d2SShane Huang 			else
2660803739d2SShane Huang 				for (i = 0; i < ATA_LOG_DEVSLP_SIZE; i++) {
2661803739d2SShane Huang 					j = ATA_LOG_DEVSLP_OFFSET + i;
2662803739d2SShane Huang 					dev->devslp_timing[i] = sata_setting[j];
2663803739d2SShane Huang 				}
266465fe1f0fSShane Huang 		}
2665e87fd28cSHannes Reinecke 		ata_dev_config_sense_reporting(dev);
26666d1003aeSHannes Reinecke 		ata_dev_config_zac(dev);
2667818831c8SChristoph Hellwig 		ata_dev_config_trusted(dev);
2668b1ffbf85SMinwoo Im 		dev->cdb_len = 32;
2669c6fd2807SJeff Garzik 	}
2670c6fd2807SJeff Garzik 
2671c6fd2807SJeff Garzik 	/* ATAPI-specific feature tests */
2672c6fd2807SJeff Garzik 	else if (dev->class == ATA_DEV_ATAPI) {
2673854c73a2STejun Heo 		const char *cdb_intr_string = "";
2674854c73a2STejun Heo 		const char *atapi_an_string = "";
267591163006STejun Heo 		const char *dma_dir_string = "";
26767d77b247STejun Heo 		u32 sntf;
2677c6fd2807SJeff Garzik 
2678c6fd2807SJeff Garzik 		rc = atapi_cdb_len(id);
2679c6fd2807SJeff Garzik 		if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
2680c6fd2807SJeff Garzik 			if (ata_msg_warn(ap))
2681a9a79dfeSJoe Perches 				ata_dev_warn(dev, "unsupported CDB len\n");
2682c6fd2807SJeff Garzik 			rc = -EINVAL;
2683c6fd2807SJeff Garzik 			goto err_out_nosup;
2684c6fd2807SJeff Garzik 		}
2685c6fd2807SJeff Garzik 		dev->cdb_len = (unsigned int) rc;
2686c6fd2807SJeff Garzik 
26877d77b247STejun Heo 		/* Enable ATAPI AN if both the host and device have
26887d77b247STejun Heo 		 * the support.  If PMP is attached, SNTF is required
26897d77b247STejun Heo 		 * to enable ATAPI AN to discern between PHY status
26907d77b247STejun Heo 		 * changed notifications and ATAPI ANs.
26919f45cbd3SKristen Carlson Accardi 		 */
2692e7ecd435STejun Heo 		if (atapi_an &&
2693e7ecd435STejun Heo 		    (ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
2694071f44b1STejun Heo 		    (!sata_pmp_attached(ap) ||
26957d77b247STejun Heo 		     sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
26969f45cbd3SKristen Carlson Accardi 			/* issue SET feature command to turn this on */
2697218f3d30SJeff Garzik 			err_mask = ata_dev_set_feature(dev,
2698218f3d30SJeff Garzik 					SETFEATURES_SATA_ENABLE, SATA_AN);
2699854c73a2STejun Heo 			if (err_mask)
2700a9a79dfeSJoe Perches 				ata_dev_err(dev,
2701a9a79dfeSJoe Perches 					    "failed to enable ATAPI AN (err_mask=0x%x)\n",
2702a9a79dfeSJoe Perches 					    err_mask);
2703854c73a2STejun Heo 			else {
27049f45cbd3SKristen Carlson Accardi 				dev->flags |= ATA_DFLAG_AN;
2705854c73a2STejun Heo 				atapi_an_string = ", ATAPI AN";
2706854c73a2STejun Heo 			}
27079f45cbd3SKristen Carlson Accardi 		}
27089f45cbd3SKristen Carlson Accardi 
2709c6fd2807SJeff Garzik 		if (ata_id_cdb_intr(dev->id)) {
2710c6fd2807SJeff Garzik 			dev->flags |= ATA_DFLAG_CDB_INTR;
2711c6fd2807SJeff Garzik 			cdb_intr_string = ", CDB intr";
2712c6fd2807SJeff Garzik 		}
2713c6fd2807SJeff Garzik 
2714966fbe19SVincent Pelletier 		if (atapi_dmadir || (dev->horkage & ATA_HORKAGE_ATAPI_DMADIR) || atapi_id_dmadir(dev->id)) {
271591163006STejun Heo 			dev->flags |= ATA_DFLAG_DMADIR;
271691163006STejun Heo 			dma_dir_string = ", DMADIR";
271791163006STejun Heo 		}
271891163006STejun Heo 
2719afe75951SAaron Lu 		if (ata_id_has_da(dev->id)) {
2720b1354cbbSLin Ming 			dev->flags |= ATA_DFLAG_DA;
2721afe75951SAaron Lu 			zpodd_init(dev);
2722afe75951SAaron Lu 		}
2723b1354cbbSLin Ming 
2724c6fd2807SJeff Garzik 		/* print device info to dmesg */
2725c6fd2807SJeff Garzik 		if (ata_msg_drv(ap) && print_info)
2726a9a79dfeSJoe Perches 			ata_dev_info(dev,
272791163006STejun Heo 				     "ATAPI: %s, %s, max %s%s%s%s\n",
2728ef143d57SAlbert Lee 				     modelbuf, fwrevbuf,
2729c6fd2807SJeff Garzik 				     ata_mode_string(xfer_mask),
273091163006STejun Heo 				     cdb_intr_string, atapi_an_string,
273191163006STejun Heo 				     dma_dir_string);
2732c6fd2807SJeff Garzik 	}
2733c6fd2807SJeff Garzik 
2734914ed354STejun Heo 	/* determine max_sectors */
2735914ed354STejun Heo 	dev->max_sectors = ATA_MAX_SECTORS;
2736914ed354STejun Heo 	if (dev->flags & ATA_DFLAG_LBA48)
2737914ed354STejun Heo 		dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2738914ed354STejun Heo 
2739c5038fc0SAlan Cox 	/* Limit PATA drive on SATA cable bridge transfers to udma5,
2740c5038fc0SAlan Cox 	   200 sectors */
2741c6fd2807SJeff Garzik 	if (ata_dev_knobble(dev)) {
2742c6fd2807SJeff Garzik 		if (ata_msg_drv(ap) && print_info)
2743a9a79dfeSJoe Perches 			ata_dev_info(dev, "applying bridge limits\n");
2744c6fd2807SJeff Garzik 		dev->udma_mask &= ATA_UDMA5;
2745c6fd2807SJeff Garzik 		dev->max_sectors = ATA_MAX_SECTORS;
2746c6fd2807SJeff Garzik 	}
2747c6fd2807SJeff Garzik 
2748f8d8e579STony Battersby 	if ((dev->class == ATA_DEV_ATAPI) &&
2749f442cd86SAlbert Lee 	    (atapi_command_packet_set(id) == TYPE_TAPE)) {
2750f8d8e579STony Battersby 		dev->max_sectors = ATA_MAX_SECTORS_TAPE;
2751f442cd86SAlbert Lee 		dev->horkage |= ATA_HORKAGE_STUCK_ERR;
2752f442cd86SAlbert Lee 	}
2753f8d8e579STony Battersby 
275475683fe7STejun Heo 	if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
275503ec52deSTejun Heo 		dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
275603ec52deSTejun Heo 					 dev->max_sectors);
275718d6e9d5SAlbert Lee 
2758af34d637SDavid Milburn 	if (dev->horkage & ATA_HORKAGE_MAX_SEC_1024)
2759af34d637SDavid Milburn 		dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_1024,
2760af34d637SDavid Milburn 					 dev->max_sectors);
2761af34d637SDavid Milburn 
2762a32450e1SShan Hai 	if (dev->horkage & ATA_HORKAGE_MAX_SEC_LBA48)
2763a32450e1SShan Hai 		dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2764a32450e1SShan Hai 
2765c6fd2807SJeff Garzik 	if (ap->ops->dev_config)
2766cd0d3bbcSAlan 		ap->ops->dev_config(dev);
2767c6fd2807SJeff Garzik 
2768c5038fc0SAlan Cox 	if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2769c5038fc0SAlan Cox 		/* Let the user know. We don't want to disallow opens for
2770c5038fc0SAlan Cox 		   rescue purposes, or in case the vendor is just a blithering
2771c5038fc0SAlan Cox 		   idiot. Do this after the dev_config call as some controllers
2772c5038fc0SAlan Cox 		   with buggy firmware may want to avoid reporting false device
2773c5038fc0SAlan Cox 		   bugs */
2774c5038fc0SAlan Cox 
2775c5038fc0SAlan Cox 		if (print_info) {
2776a9a79dfeSJoe Perches 			ata_dev_warn(dev,
2777c5038fc0SAlan Cox "Drive reports diagnostics failure. This may indicate a drive\n");
2778a9a79dfeSJoe Perches 			ata_dev_warn(dev,
2779c5038fc0SAlan Cox "fault or invalid emulation. Contact drive vendor for information.\n");
2780c5038fc0SAlan Cox 		}
2781c5038fc0SAlan Cox 	}
2782c5038fc0SAlan Cox 
2783ac70a964STejun Heo 	if ((dev->horkage & ATA_HORKAGE_FIRMWARE_WARN) && print_info) {
2784a9a79dfeSJoe Perches 		ata_dev_warn(dev, "WARNING: device requires firmware update to be fully functional\n");
2785a9a79dfeSJoe Perches 		ata_dev_warn(dev, "         contact the vendor or visit http://ata.wiki.kernel.org\n");
2786ac70a964STejun Heo 	}
2787ac70a964STejun Heo 
2788c6fd2807SJeff Garzik 	return 0;
2789c6fd2807SJeff Garzik 
2790c6fd2807SJeff Garzik err_out_nosup:
2791c6fd2807SJeff Garzik 	if (ata_msg_probe(ap))
2792a9a79dfeSJoe Perches 		ata_dev_dbg(dev, "%s: EXIT, err\n", __func__);
2793c6fd2807SJeff Garzik 	return rc;
2794c6fd2807SJeff Garzik }
2795c6fd2807SJeff Garzik 
2796c6fd2807SJeff Garzik /**
27972e41e8e6SAlan Cox  *	ata_cable_40wire	-	return 40 wire cable type
2798be0d18dfSAlan Cox  *	@ap: port
2799be0d18dfSAlan Cox  *
28002e41e8e6SAlan Cox  *	Helper method for drivers which want to hardwire 40 wire cable
2801be0d18dfSAlan Cox  *	detection.
2802be0d18dfSAlan Cox  */
2803be0d18dfSAlan Cox 
2804be0d18dfSAlan Cox int ata_cable_40wire(struct ata_port *ap)
2805be0d18dfSAlan Cox {
2806be0d18dfSAlan Cox 	return ATA_CBL_PATA40;
2807be0d18dfSAlan Cox }
2808be0d18dfSAlan Cox 
2809be0d18dfSAlan Cox /**
28102e41e8e6SAlan Cox  *	ata_cable_80wire	-	return 80 wire cable type
2811be0d18dfSAlan Cox  *	@ap: port
2812be0d18dfSAlan Cox  *
28132e41e8e6SAlan Cox  *	Helper method for drivers which want to hardwire 80 wire cable
2814be0d18dfSAlan Cox  *	detection.
2815be0d18dfSAlan Cox  */
2816be0d18dfSAlan Cox 
2817be0d18dfSAlan Cox int ata_cable_80wire(struct ata_port *ap)
2818be0d18dfSAlan Cox {
2819be0d18dfSAlan Cox 	return ATA_CBL_PATA80;
2820be0d18dfSAlan Cox }
2821be0d18dfSAlan Cox 
2822be0d18dfSAlan Cox /**
2823be0d18dfSAlan Cox  *	ata_cable_unknown	-	return unknown PATA cable.
2824be0d18dfSAlan Cox  *	@ap: port
2825be0d18dfSAlan Cox  *
2826be0d18dfSAlan Cox  *	Helper method for drivers which have no PATA cable detection.
2827be0d18dfSAlan Cox  */
2828be0d18dfSAlan Cox 
2829be0d18dfSAlan Cox int ata_cable_unknown(struct ata_port *ap)
2830be0d18dfSAlan Cox {
2831be0d18dfSAlan Cox 	return ATA_CBL_PATA_UNK;
2832be0d18dfSAlan Cox }
2833be0d18dfSAlan Cox 
2834be0d18dfSAlan Cox /**
2835c88f90c3STejun Heo  *	ata_cable_ignore	-	return ignored PATA cable.
2836c88f90c3STejun Heo  *	@ap: port
2837c88f90c3STejun Heo  *
2838c88f90c3STejun Heo  *	Helper method for drivers which don't use cable type to limit
2839c88f90c3STejun Heo  *	transfer mode.
2840c88f90c3STejun Heo  */
2841c88f90c3STejun Heo int ata_cable_ignore(struct ata_port *ap)
2842c88f90c3STejun Heo {
2843c88f90c3STejun Heo 	return ATA_CBL_PATA_IGN;
2844c88f90c3STejun Heo }
2845c88f90c3STejun Heo 
2846c88f90c3STejun Heo /**
2847be0d18dfSAlan Cox  *	ata_cable_sata	-	return SATA cable type
2848be0d18dfSAlan Cox  *	@ap: port
2849be0d18dfSAlan Cox  *
2850be0d18dfSAlan Cox  *	Helper method for drivers which have SATA cables
2851be0d18dfSAlan Cox  */
2852be0d18dfSAlan Cox 
2853be0d18dfSAlan Cox int ata_cable_sata(struct ata_port *ap)
2854be0d18dfSAlan Cox {
2855be0d18dfSAlan Cox 	return ATA_CBL_SATA;
2856be0d18dfSAlan Cox }
2857be0d18dfSAlan Cox 
2858be0d18dfSAlan Cox /**
2859c6fd2807SJeff Garzik  *	ata_bus_probe - Reset and probe ATA bus
2860c6fd2807SJeff Garzik  *	@ap: Bus to probe
2861c6fd2807SJeff Garzik  *
2862c6fd2807SJeff Garzik  *	Master ATA bus probing function.  Initiates a hardware-dependent
2863c6fd2807SJeff Garzik  *	bus reset, then attempts to identify any devices found on
2864c6fd2807SJeff Garzik  *	the bus.
2865c6fd2807SJeff Garzik  *
2866c6fd2807SJeff Garzik  *	LOCKING:
2867c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
2868c6fd2807SJeff Garzik  *
2869c6fd2807SJeff Garzik  *	RETURNS:
2870c6fd2807SJeff Garzik  *	Zero on success, negative errno otherwise.
2871c6fd2807SJeff Garzik  */
2872c6fd2807SJeff Garzik 
2873c6fd2807SJeff Garzik int ata_bus_probe(struct ata_port *ap)
2874c6fd2807SJeff Garzik {
2875c6fd2807SJeff Garzik 	unsigned int classes[ATA_MAX_DEVICES];
2876c6fd2807SJeff Garzik 	int tries[ATA_MAX_DEVICES];
2877f58229f8STejun Heo 	int rc;
2878c6fd2807SJeff Garzik 	struct ata_device *dev;
2879c6fd2807SJeff Garzik 
28801eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL)
2881f58229f8STejun Heo 		tries[dev->devno] = ATA_PROBE_MAX_TRIES;
2882c6fd2807SJeff Garzik 
2883c6fd2807SJeff Garzik  retry:
28841eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL) {
2885cdeab114STejun Heo 		/* If we issue an SRST then an ATA drive (not ATAPI)
2886cdeab114STejun Heo 		 * may change configuration and be in PIO0 timing. If
2887cdeab114STejun Heo 		 * we do a hard reset (or are coming from power on)
2888cdeab114STejun Heo 		 * this is true for ATA or ATAPI. Until we've set a
2889cdeab114STejun Heo 		 * suitable controller mode we should not touch the
2890cdeab114STejun Heo 		 * bus as we may be talking too fast.
2891cdeab114STejun Heo 		 */
2892cdeab114STejun Heo 		dev->pio_mode = XFER_PIO_0;
28935416912aSAaron Lu 		dev->dma_mode = 0xff;
2894cdeab114STejun Heo 
2895cdeab114STejun Heo 		/* If the controller has a pio mode setup function
2896cdeab114STejun Heo 		 * then use it to set the chipset to rights. Don't
2897cdeab114STejun Heo 		 * touch the DMA setup as that will be dealt with when
2898cdeab114STejun Heo 		 * configuring devices.
2899cdeab114STejun Heo 		 */
2900cdeab114STejun Heo 		if (ap->ops->set_piomode)
2901cdeab114STejun Heo 			ap->ops->set_piomode(ap, dev);
2902cdeab114STejun Heo 	}
2903cdeab114STejun Heo 
2904c6fd2807SJeff Garzik 	/* reset and determine device classes */
2905c6fd2807SJeff Garzik 	ap->ops->phy_reset(ap);
2906c6fd2807SJeff Garzik 
29071eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL) {
29083e4ec344STejun Heo 		if (dev->class != ATA_DEV_UNKNOWN)
2909c6fd2807SJeff Garzik 			classes[dev->devno] = dev->class;
2910c6fd2807SJeff Garzik 		else
2911c6fd2807SJeff Garzik 			classes[dev->devno] = ATA_DEV_NONE;
2912c6fd2807SJeff Garzik 
2913c6fd2807SJeff Garzik 		dev->class = ATA_DEV_UNKNOWN;
2914c6fd2807SJeff Garzik 	}
2915c6fd2807SJeff Garzik 
2916f31f0cc2SJeff Garzik 	/* read IDENTIFY page and configure devices. We have to do the identify
2917f31f0cc2SJeff Garzik 	   specific sequence bass-ackwards so that PDIAG- is released by
2918f31f0cc2SJeff Garzik 	   the slave device */
2919f31f0cc2SJeff Garzik 
29201eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL_REVERSE) {
2921f58229f8STejun Heo 		if (tries[dev->devno])
2922f58229f8STejun Heo 			dev->class = classes[dev->devno];
2923c6fd2807SJeff Garzik 
2924c6fd2807SJeff Garzik 		if (!ata_dev_enabled(dev))
2925c6fd2807SJeff Garzik 			continue;
2926c6fd2807SJeff Garzik 
2927bff04647STejun Heo 		rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2928bff04647STejun Heo 				     dev->id);
2929c6fd2807SJeff Garzik 		if (rc)
2930c6fd2807SJeff Garzik 			goto fail;
2931f31f0cc2SJeff Garzik 	}
2932f31f0cc2SJeff Garzik 
2933be0d18dfSAlan Cox 	/* Now ask for the cable type as PDIAG- should have been released */
2934be0d18dfSAlan Cox 	if (ap->ops->cable_detect)
2935be0d18dfSAlan Cox 		ap->cbl = ap->ops->cable_detect(ap);
2936be0d18dfSAlan Cox 
29371eca4365STejun Heo 	/* We may have SATA bridge glue hiding here irrespective of
29381eca4365STejun Heo 	 * the reported cable types and sensed types.  When SATA
29391eca4365STejun Heo 	 * drives indicate we have a bridge, we don't know which end
29401eca4365STejun Heo 	 * of the link the bridge is which is a problem.
29411eca4365STejun Heo 	 */
29421eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED)
2943614fe29bSAlan Cox 		if (ata_id_is_sata(dev->id))
2944614fe29bSAlan Cox 			ap->cbl = ATA_CBL_SATA;
2945614fe29bSAlan Cox 
2946f31f0cc2SJeff Garzik 	/* After the identify sequence we can now set up the devices. We do
2947f31f0cc2SJeff Garzik 	   this in the normal order so that the user doesn't get confused */
2948f31f0cc2SJeff Garzik 
29491eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED) {
29509af5c9c9STejun Heo 		ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
2951efdaedc4STejun Heo 		rc = ata_dev_configure(dev);
29529af5c9c9STejun Heo 		ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
2953c6fd2807SJeff Garzik 		if (rc)
2954c6fd2807SJeff Garzik 			goto fail;
2955c6fd2807SJeff Garzik 	}
2956c6fd2807SJeff Garzik 
2957c6fd2807SJeff Garzik 	/* configure transfer mode */
29580260731fSTejun Heo 	rc = ata_set_mode(&ap->link, &dev);
29594ae72a1eSTejun Heo 	if (rc)
2960c6fd2807SJeff Garzik 		goto fail;
2961c6fd2807SJeff Garzik 
29621eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED)
2963c6fd2807SJeff Garzik 		return 0;
2964c6fd2807SJeff Garzik 
2965c6fd2807SJeff Garzik 	return -ENODEV;
2966c6fd2807SJeff Garzik 
2967c6fd2807SJeff Garzik  fail:
29684ae72a1eSTejun Heo 	tries[dev->devno]--;
29694ae72a1eSTejun Heo 
2970c6fd2807SJeff Garzik 	switch (rc) {
2971c6fd2807SJeff Garzik 	case -EINVAL:
29724ae72a1eSTejun Heo 		/* eeek, something went very wrong, give up */
2973c6fd2807SJeff Garzik 		tries[dev->devno] = 0;
2974c6fd2807SJeff Garzik 		break;
29754ae72a1eSTejun Heo 
29764ae72a1eSTejun Heo 	case -ENODEV:
29774ae72a1eSTejun Heo 		/* give it just one more chance */
29784ae72a1eSTejun Heo 		tries[dev->devno] = min(tries[dev->devno], 1);
297905b83605SGustavo A. R. Silva 		/* fall through */
2980c6fd2807SJeff Garzik 	case -EIO:
29814ae72a1eSTejun Heo 		if (tries[dev->devno] == 1) {
29824ae72a1eSTejun Heo 			/* This is the last chance, better to slow
29834ae72a1eSTejun Heo 			 * down than lose it.
29844ae72a1eSTejun Heo 			 */
2985a07d499bSTejun Heo 			sata_down_spd_limit(&ap->link, 0);
29864ae72a1eSTejun Heo 			ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
29874ae72a1eSTejun Heo 		}
2988c6fd2807SJeff Garzik 	}
2989c6fd2807SJeff Garzik 
29904ae72a1eSTejun Heo 	if (!tries[dev->devno])
2991c6fd2807SJeff Garzik 		ata_dev_disable(dev);
2992c6fd2807SJeff Garzik 
2993c6fd2807SJeff Garzik 	goto retry;
2994c6fd2807SJeff Garzik }
2995c6fd2807SJeff Garzik 
2996c6fd2807SJeff Garzik /**
2997c6fd2807SJeff Garzik  *	sata_print_link_status - Print SATA link status
2998936fd732STejun Heo  *	@link: SATA link to printk link status about
2999c6fd2807SJeff Garzik  *
3000c6fd2807SJeff Garzik  *	This function prints link speed and status of a SATA link.
3001c6fd2807SJeff Garzik  *
3002c6fd2807SJeff Garzik  *	LOCKING:
3003c6fd2807SJeff Garzik  *	None.
3004c6fd2807SJeff Garzik  */
30056bdb4fc9SAdrian Bunk static void sata_print_link_status(struct ata_link *link)
3006c6fd2807SJeff Garzik {
3007c6fd2807SJeff Garzik 	u32 sstatus, scontrol, tmp;
3008c6fd2807SJeff Garzik 
3009936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus))
3010c6fd2807SJeff Garzik 		return;
3011936fd732STejun Heo 	sata_scr_read(link, SCR_CONTROL, &scontrol);
3012c6fd2807SJeff Garzik 
3013b1c72916STejun Heo 	if (ata_phys_link_online(link)) {
3014c6fd2807SJeff Garzik 		tmp = (sstatus >> 4) & 0xf;
3015a9a79dfeSJoe Perches 		ata_link_info(link, "SATA link up %s (SStatus %X SControl %X)\n",
3016c6fd2807SJeff Garzik 			      sata_spd_string(tmp), sstatus, scontrol);
3017c6fd2807SJeff Garzik 	} else {
3018a9a79dfeSJoe Perches 		ata_link_info(link, "SATA link down (SStatus %X SControl %X)\n",
3019c6fd2807SJeff Garzik 			      sstatus, scontrol);
3020c6fd2807SJeff Garzik 	}
3021c6fd2807SJeff Garzik }
3022c6fd2807SJeff Garzik 
3023c6fd2807SJeff Garzik /**
3024c6fd2807SJeff Garzik  *	ata_dev_pair		-	return other device on cable
3025c6fd2807SJeff Garzik  *	@adev: device
3026c6fd2807SJeff Garzik  *
3027c6fd2807SJeff Garzik  *	Obtain the other device on the same cable, or if none is
3028c6fd2807SJeff Garzik  *	present NULL is returned
3029c6fd2807SJeff Garzik  */
3030c6fd2807SJeff Garzik 
3031c6fd2807SJeff Garzik struct ata_device *ata_dev_pair(struct ata_device *adev)
3032c6fd2807SJeff Garzik {
30339af5c9c9STejun Heo 	struct ata_link *link = adev->link;
30349af5c9c9STejun Heo 	struct ata_device *pair = &link->device[1 - adev->devno];
3035c6fd2807SJeff Garzik 	if (!ata_dev_enabled(pair))
3036c6fd2807SJeff Garzik 		return NULL;
3037c6fd2807SJeff Garzik 	return pair;
3038c6fd2807SJeff Garzik }
3039c6fd2807SJeff Garzik 
3040c6fd2807SJeff Garzik /**
3041c6fd2807SJeff Garzik  *	sata_down_spd_limit - adjust SATA spd limit downward
3042936fd732STejun Heo  *	@link: Link to adjust SATA spd limit for
3043a07d499bSTejun Heo  *	@spd_limit: Additional limit
3044c6fd2807SJeff Garzik  *
3045936fd732STejun Heo  *	Adjust SATA spd limit of @link downward.  Note that this
3046c6fd2807SJeff Garzik  *	function only adjusts the limit.  The change must be applied
3047c6fd2807SJeff Garzik  *	using sata_set_spd().
3048c6fd2807SJeff Garzik  *
3049a07d499bSTejun Heo  *	If @spd_limit is non-zero, the speed is limited to equal to or
3050a07d499bSTejun Heo  *	lower than @spd_limit if such speed is supported.  If
3051a07d499bSTejun Heo  *	@spd_limit is slower than any supported speed, only the lowest
3052a07d499bSTejun Heo  *	supported speed is allowed.
3053a07d499bSTejun Heo  *
3054c6fd2807SJeff Garzik  *	LOCKING:
3055c6fd2807SJeff Garzik  *	Inherited from caller.
3056c6fd2807SJeff Garzik  *
3057c6fd2807SJeff Garzik  *	RETURNS:
3058c6fd2807SJeff Garzik  *	0 on success, negative errno on failure
3059c6fd2807SJeff Garzik  */
3060a07d499bSTejun Heo int sata_down_spd_limit(struct ata_link *link, u32 spd_limit)
3061c6fd2807SJeff Garzik {
3062c6fd2807SJeff Garzik 	u32 sstatus, spd, mask;
3063a07d499bSTejun Heo 	int rc, bit;
3064c6fd2807SJeff Garzik 
3065936fd732STejun Heo 	if (!sata_scr_valid(link))
3066008a7896STejun Heo 		return -EOPNOTSUPP;
3067008a7896STejun Heo 
3068008a7896STejun Heo 	/* If SCR can be read, use it to determine the current SPD.
3069936fd732STejun Heo 	 * If not, use cached value in link->sata_spd.
3070008a7896STejun Heo 	 */
3071936fd732STejun Heo 	rc = sata_scr_read(link, SCR_STATUS, &sstatus);
30729913ff8aSTejun Heo 	if (rc == 0 && ata_sstatus_online(sstatus))
3073008a7896STejun Heo 		spd = (sstatus >> 4) & 0xf;
3074008a7896STejun Heo 	else
3075936fd732STejun Heo 		spd = link->sata_spd;
3076c6fd2807SJeff Garzik 
3077936fd732STejun Heo 	mask = link->sata_spd_limit;
3078c6fd2807SJeff Garzik 	if (mask <= 1)
3079c6fd2807SJeff Garzik 		return -EINVAL;
3080008a7896STejun Heo 
3081008a7896STejun Heo 	/* unconditionally mask off the highest bit */
3082a07d499bSTejun Heo 	bit = fls(mask) - 1;
3083a07d499bSTejun Heo 	mask &= ~(1 << bit);
3084c6fd2807SJeff Garzik 
30852dc0b46bSDavid Milburn 	/*
30862dc0b46bSDavid Milburn 	 * Mask off all speeds higher than or equal to the current one.  At
30872dc0b46bSDavid Milburn 	 * this point, if current SPD is not available and we previously
30882dc0b46bSDavid Milburn 	 * recorded the link speed from SStatus, the driver has already
30892dc0b46bSDavid Milburn 	 * masked off the highest bit so mask should already be 1 or 0.
30902dc0b46bSDavid Milburn 	 * Otherwise, we should not force 1.5Gbps on a link where we have
30912dc0b46bSDavid Milburn 	 * not previously recorded speed from SStatus.  Just return in this
30922dc0b46bSDavid Milburn 	 * case.
3093008a7896STejun Heo 	 */
3094008a7896STejun Heo 	if (spd > 1)
3095008a7896STejun Heo 		mask &= (1 << (spd - 1)) - 1;
3096008a7896STejun Heo 	else
30972dc0b46bSDavid Milburn 		return -EINVAL;
3098008a7896STejun Heo 
3099008a7896STejun Heo 	/* were we already at the bottom? */
3100c6fd2807SJeff Garzik 	if (!mask)
3101c6fd2807SJeff Garzik 		return -EINVAL;
3102c6fd2807SJeff Garzik 
3103a07d499bSTejun Heo 	if (spd_limit) {
3104a07d499bSTejun Heo 		if (mask & ((1 << spd_limit) - 1))
3105a07d499bSTejun Heo 			mask &= (1 << spd_limit) - 1;
3106a07d499bSTejun Heo 		else {
3107a07d499bSTejun Heo 			bit = ffs(mask) - 1;
3108a07d499bSTejun Heo 			mask = 1 << bit;
3109a07d499bSTejun Heo 		}
3110a07d499bSTejun Heo 	}
3111a07d499bSTejun Heo 
3112936fd732STejun Heo 	link->sata_spd_limit = mask;
3113c6fd2807SJeff Garzik 
3114a9a79dfeSJoe Perches 	ata_link_warn(link, "limiting SATA link speed to %s\n",
3115c6fd2807SJeff Garzik 		      sata_spd_string(fls(mask)));
3116c6fd2807SJeff Garzik 
3117c6fd2807SJeff Garzik 	return 0;
3118c6fd2807SJeff Garzik }
3119c6fd2807SJeff Garzik 
3120936fd732STejun Heo static int __sata_set_spd_needed(struct ata_link *link, u32 *scontrol)
3121c6fd2807SJeff Garzik {
31225270222fSTejun Heo 	struct ata_link *host_link = &link->ap->link;
31235270222fSTejun Heo 	u32 limit, target, spd;
3124c6fd2807SJeff Garzik 
31255270222fSTejun Heo 	limit = link->sata_spd_limit;
31265270222fSTejun Heo 
31275270222fSTejun Heo 	/* Don't configure downstream link faster than upstream link.
31285270222fSTejun Heo 	 * It doesn't speed up anything and some PMPs choke on such
31295270222fSTejun Heo 	 * configuration.
31305270222fSTejun Heo 	 */
31315270222fSTejun Heo 	if (!ata_is_host_link(link) && host_link->sata_spd)
31325270222fSTejun Heo 		limit &= (1 << host_link->sata_spd) - 1;
31335270222fSTejun Heo 
31345270222fSTejun Heo 	if (limit == UINT_MAX)
31355270222fSTejun Heo 		target = 0;
3136c6fd2807SJeff Garzik 	else
31375270222fSTejun Heo 		target = fls(limit);
3138c6fd2807SJeff Garzik 
3139c6fd2807SJeff Garzik 	spd = (*scontrol >> 4) & 0xf;
31405270222fSTejun Heo 	*scontrol = (*scontrol & ~0xf0) | ((target & 0xf) << 4);
3141c6fd2807SJeff Garzik 
31425270222fSTejun Heo 	return spd != target;
3143c6fd2807SJeff Garzik }
3144c6fd2807SJeff Garzik 
3145c6fd2807SJeff Garzik /**
3146c6fd2807SJeff Garzik  *	sata_set_spd_needed - is SATA spd configuration needed
3147936fd732STejun Heo  *	@link: Link in question
3148c6fd2807SJeff Garzik  *
3149c6fd2807SJeff Garzik  *	Test whether the spd limit in SControl matches
3150936fd732STejun Heo  *	@link->sata_spd_limit.  This function is used to determine
3151c6fd2807SJeff Garzik  *	whether hardreset is necessary to apply SATA spd
3152c6fd2807SJeff Garzik  *	configuration.
3153c6fd2807SJeff Garzik  *
3154c6fd2807SJeff Garzik  *	LOCKING:
3155c6fd2807SJeff Garzik  *	Inherited from caller.
3156c6fd2807SJeff Garzik  *
3157c6fd2807SJeff Garzik  *	RETURNS:
3158c6fd2807SJeff Garzik  *	1 if SATA spd configuration is needed, 0 otherwise.
3159c6fd2807SJeff Garzik  */
31601dc55e87SAdrian Bunk static int sata_set_spd_needed(struct ata_link *link)
3161c6fd2807SJeff Garzik {
3162c6fd2807SJeff Garzik 	u32 scontrol;
3163c6fd2807SJeff Garzik 
3164936fd732STejun Heo 	if (sata_scr_read(link, SCR_CONTROL, &scontrol))
3165db64bcf3STejun Heo 		return 1;
3166c6fd2807SJeff Garzik 
3167936fd732STejun Heo 	return __sata_set_spd_needed(link, &scontrol);
3168c6fd2807SJeff Garzik }
3169c6fd2807SJeff Garzik 
3170c6fd2807SJeff Garzik /**
3171c6fd2807SJeff Garzik  *	sata_set_spd - set SATA spd according to spd limit
3172936fd732STejun Heo  *	@link: Link to set SATA spd for
3173c6fd2807SJeff Garzik  *
3174936fd732STejun Heo  *	Set SATA spd of @link according to sata_spd_limit.
3175c6fd2807SJeff Garzik  *
3176c6fd2807SJeff Garzik  *	LOCKING:
3177c6fd2807SJeff Garzik  *	Inherited from caller.
3178c6fd2807SJeff Garzik  *
3179c6fd2807SJeff Garzik  *	RETURNS:
3180c6fd2807SJeff Garzik  *	0 if spd doesn't need to be changed, 1 if spd has been
3181c6fd2807SJeff Garzik  *	changed.  Negative errno if SCR registers are inaccessible.
3182c6fd2807SJeff Garzik  */
3183936fd732STejun Heo int sata_set_spd(struct ata_link *link)
3184c6fd2807SJeff Garzik {
3185c6fd2807SJeff Garzik 	u32 scontrol;
3186c6fd2807SJeff Garzik 	int rc;
3187c6fd2807SJeff Garzik 
3188936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3189c6fd2807SJeff Garzik 		return rc;
3190c6fd2807SJeff Garzik 
3191936fd732STejun Heo 	if (!__sata_set_spd_needed(link, &scontrol))
3192c6fd2807SJeff Garzik 		return 0;
3193c6fd2807SJeff Garzik 
3194936fd732STejun Heo 	if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3195c6fd2807SJeff Garzik 		return rc;
3196c6fd2807SJeff Garzik 
3197c6fd2807SJeff Garzik 	return 1;
3198c6fd2807SJeff Garzik }
3199c6fd2807SJeff Garzik 
3200c6fd2807SJeff Garzik /*
3201c6fd2807SJeff Garzik  * This mode timing computation functionality is ported over from
3202c6fd2807SJeff Garzik  * drivers/ide/ide-timing.h and was originally written by Vojtech Pavlik
3203c6fd2807SJeff Garzik  */
3204c6fd2807SJeff Garzik /*
3205b352e57dSAlan Cox  * PIO 0-4, MWDMA 0-2 and UDMA 0-6 timings (in nanoseconds).
3206c6fd2807SJeff Garzik  * These were taken from ATA/ATAPI-6 standard, rev 0a, except
3207b352e57dSAlan Cox  * for UDMA6, which is currently supported only by Maxtor drives.
3208b352e57dSAlan Cox  *
3209b352e57dSAlan Cox  * For PIO 5/6 MWDMA 3/4 see the CFA specification 3.0.
3210c6fd2807SJeff Garzik  */
3211c6fd2807SJeff Garzik 
3212c6fd2807SJeff Garzik static const struct ata_timing ata_timing[] = {
32133ada9c12SDavid Daney /*	{ XFER_PIO_SLOW, 120, 290, 240, 960, 290, 240, 0,  960,   0 }, */
32143ada9c12SDavid Daney 	{ XFER_PIO_0,     70, 290, 240, 600, 165, 150, 0,  600,   0 },
32153ada9c12SDavid Daney 	{ XFER_PIO_1,     50, 290,  93, 383, 125, 100, 0,  383,   0 },
32163ada9c12SDavid Daney 	{ XFER_PIO_2,     30, 290,  40, 330, 100,  90, 0,  240,   0 },
32173ada9c12SDavid Daney 	{ XFER_PIO_3,     30,  80,  70, 180,  80,  70, 0,  180,   0 },
32183ada9c12SDavid Daney 	{ XFER_PIO_4,     25,  70,  25, 120,  70,  25, 0,  120,   0 },
32193ada9c12SDavid Daney 	{ XFER_PIO_5,     15,  65,  25, 100,  65,  25, 0,  100,   0 },
32203ada9c12SDavid Daney 	{ XFER_PIO_6,     10,  55,  20,  80,  55,  20, 0,   80,   0 },
3221c6fd2807SJeff Garzik 
32223ada9c12SDavid Daney 	{ XFER_SW_DMA_0, 120,   0,   0,   0, 480, 480, 50, 960,   0 },
32233ada9c12SDavid Daney 	{ XFER_SW_DMA_1,  90,   0,   0,   0, 240, 240, 30, 480,   0 },
32243ada9c12SDavid Daney 	{ XFER_SW_DMA_2,  60,   0,   0,   0, 120, 120, 20, 240,   0 },
3225c6fd2807SJeff Garzik 
32263ada9c12SDavid Daney 	{ XFER_MW_DMA_0,  60,   0,   0,   0, 215, 215, 20, 480,   0 },
32273ada9c12SDavid Daney 	{ XFER_MW_DMA_1,  45,   0,   0,   0,  80,  50, 5,  150,   0 },
32283ada9c12SDavid Daney 	{ XFER_MW_DMA_2,  25,   0,   0,   0,  70,  25, 5,  120,   0 },
32293ada9c12SDavid Daney 	{ XFER_MW_DMA_3,  25,   0,   0,   0,  65,  25, 5,  100,   0 },
32303ada9c12SDavid Daney 	{ XFER_MW_DMA_4,  25,   0,   0,   0,  55,  20, 5,   80,   0 },
3231c6fd2807SJeff Garzik 
32323ada9c12SDavid Daney /*	{ XFER_UDMA_SLOW,  0,   0,   0,   0,   0,   0, 0,    0, 150 }, */
32333ada9c12SDavid Daney 	{ XFER_UDMA_0,     0,   0,   0,   0,   0,   0, 0,    0, 120 },
32343ada9c12SDavid Daney 	{ XFER_UDMA_1,     0,   0,   0,   0,   0,   0, 0,    0,  80 },
32353ada9c12SDavid Daney 	{ XFER_UDMA_2,     0,   0,   0,   0,   0,   0, 0,    0,  60 },
32363ada9c12SDavid Daney 	{ XFER_UDMA_3,     0,   0,   0,   0,   0,   0, 0,    0,  45 },
32373ada9c12SDavid Daney 	{ XFER_UDMA_4,     0,   0,   0,   0,   0,   0, 0,    0,  30 },
32383ada9c12SDavid Daney 	{ XFER_UDMA_5,     0,   0,   0,   0,   0,   0, 0,    0,  20 },
32393ada9c12SDavid Daney 	{ XFER_UDMA_6,     0,   0,   0,   0,   0,   0, 0,    0,  15 },
3240c6fd2807SJeff Garzik 
3241c6fd2807SJeff Garzik 	{ 0xFF }
3242c6fd2807SJeff Garzik };
3243c6fd2807SJeff Garzik 
3244c6fd2807SJeff Garzik #define ENOUGH(v, unit)		(((v)-1)/(unit)+1)
324523e4c67aSArnd Bergmann #define EZ(v, unit)		((v)?ENOUGH(((v) * 1000), unit):0)
3246c6fd2807SJeff Garzik 
3247c6fd2807SJeff Garzik static void ata_timing_quantize(const struct ata_timing *t, struct ata_timing *q, int T, int UT)
3248c6fd2807SJeff Garzik {
324923e4c67aSArnd Bergmann 	q->setup	= EZ(t->setup,       T);
325023e4c67aSArnd Bergmann 	q->act8b	= EZ(t->act8b,       T);
325123e4c67aSArnd Bergmann 	q->rec8b	= EZ(t->rec8b,       T);
325223e4c67aSArnd Bergmann 	q->cyc8b	= EZ(t->cyc8b,       T);
325323e4c67aSArnd Bergmann 	q->active	= EZ(t->active,      T);
325423e4c67aSArnd Bergmann 	q->recover	= EZ(t->recover,     T);
325523e4c67aSArnd Bergmann 	q->dmack_hold	= EZ(t->dmack_hold,  T);
325623e4c67aSArnd Bergmann 	q->cycle	= EZ(t->cycle,       T);
325723e4c67aSArnd Bergmann 	q->udma		= EZ(t->udma,       UT);
3258c6fd2807SJeff Garzik }
3259c6fd2807SJeff Garzik 
3260c6fd2807SJeff Garzik void ata_timing_merge(const struct ata_timing *a, const struct ata_timing *b,
3261c6fd2807SJeff Garzik 		      struct ata_timing *m, unsigned int what)
3262c6fd2807SJeff Garzik {
3263c6fd2807SJeff Garzik 	if (what & ATA_TIMING_SETUP  ) m->setup   = max(a->setup,   b->setup);
3264c6fd2807SJeff Garzik 	if (what & ATA_TIMING_ACT8B  ) m->act8b   = max(a->act8b,   b->act8b);
3265c6fd2807SJeff Garzik 	if (what & ATA_TIMING_REC8B  ) m->rec8b   = max(a->rec8b,   b->rec8b);
3266c6fd2807SJeff Garzik 	if (what & ATA_TIMING_CYC8B  ) m->cyc8b   = max(a->cyc8b,   b->cyc8b);
3267c6fd2807SJeff Garzik 	if (what & ATA_TIMING_ACTIVE ) m->active  = max(a->active,  b->active);
3268c6fd2807SJeff Garzik 	if (what & ATA_TIMING_RECOVER) m->recover = max(a->recover, b->recover);
32693ada9c12SDavid Daney 	if (what & ATA_TIMING_DMACK_HOLD) m->dmack_hold = max(a->dmack_hold, b->dmack_hold);
3270c6fd2807SJeff Garzik 	if (what & ATA_TIMING_CYCLE  ) m->cycle   = max(a->cycle,   b->cycle);
3271c6fd2807SJeff Garzik 	if (what & ATA_TIMING_UDMA   ) m->udma    = max(a->udma,    b->udma);
3272c6fd2807SJeff Garzik }
3273c6fd2807SJeff Garzik 
32746357357cSTejun Heo const struct ata_timing *ata_timing_find_mode(u8 xfer_mode)
3275c6fd2807SJeff Garzik {
327670cd071eSTejun Heo 	const struct ata_timing *t = ata_timing;
3277c6fd2807SJeff Garzik 
327870cd071eSTejun Heo 	while (xfer_mode > t->mode)
327970cd071eSTejun Heo 		t++;
328070cd071eSTejun Heo 
328170cd071eSTejun Heo 	if (xfer_mode == t->mode)
3282c6fd2807SJeff Garzik 		return t;
3283cd705d5aSBorislav Petkov 
3284cd705d5aSBorislav Petkov 	WARN_ONCE(true, "%s: unable to find timing for xfer_mode 0x%x\n",
3285cd705d5aSBorislav Petkov 			__func__, xfer_mode);
3286cd705d5aSBorislav Petkov 
328770cd071eSTejun Heo 	return NULL;
3288c6fd2807SJeff Garzik }
3289c6fd2807SJeff Garzik 
3290c6fd2807SJeff Garzik int ata_timing_compute(struct ata_device *adev, unsigned short speed,
3291c6fd2807SJeff Garzik 		       struct ata_timing *t, int T, int UT)
3292c6fd2807SJeff Garzik {
32939e8808a9SBartlomiej Zolnierkiewicz 	const u16 *id = adev->id;
3294c6fd2807SJeff Garzik 	const struct ata_timing *s;
3295c6fd2807SJeff Garzik 	struct ata_timing p;
3296c6fd2807SJeff Garzik 
3297c6fd2807SJeff Garzik 	/*
3298c6fd2807SJeff Garzik 	 * Find the mode.
3299c6fd2807SJeff Garzik 	 */
3300c6fd2807SJeff Garzik 
3301c6fd2807SJeff Garzik 	if (!(s = ata_timing_find_mode(speed)))
3302c6fd2807SJeff Garzik 		return -EINVAL;
3303c6fd2807SJeff Garzik 
3304c6fd2807SJeff Garzik 	memcpy(t, s, sizeof(*s));
3305c6fd2807SJeff Garzik 
3306c6fd2807SJeff Garzik 	/*
3307c6fd2807SJeff Garzik 	 * If the drive is an EIDE drive, it can tell us it needs extended
3308c6fd2807SJeff Garzik 	 * PIO/MW_DMA cycle timing.
3309c6fd2807SJeff Garzik 	 */
3310c6fd2807SJeff Garzik 
33119e8808a9SBartlomiej Zolnierkiewicz 	if (id[ATA_ID_FIELD_VALID] & 2) {	/* EIDE drive */
3312c6fd2807SJeff Garzik 		memset(&p, 0, sizeof(p));
33139e8808a9SBartlomiej Zolnierkiewicz 
3314bff00256SBartlomiej Zolnierkiewicz 		if (speed >= XFER_PIO_0 && speed < XFER_SW_DMA_0) {
33159e8808a9SBartlomiej Zolnierkiewicz 			if (speed <= XFER_PIO_2)
33169e8808a9SBartlomiej Zolnierkiewicz 				p.cycle = p.cyc8b = id[ATA_ID_EIDE_PIO];
33179e8808a9SBartlomiej Zolnierkiewicz 			else if ((speed <= XFER_PIO_4) ||
33189e8808a9SBartlomiej Zolnierkiewicz 				 (speed == XFER_PIO_5 && !ata_id_is_cfa(id)))
33199e8808a9SBartlomiej Zolnierkiewicz 				p.cycle = p.cyc8b = id[ATA_ID_EIDE_PIO_IORDY];
33209e8808a9SBartlomiej Zolnierkiewicz 		} else if (speed >= XFER_MW_DMA_0 && speed <= XFER_MW_DMA_2)
33219e8808a9SBartlomiej Zolnierkiewicz 			p.cycle = id[ATA_ID_EIDE_DMA_MIN];
33229e8808a9SBartlomiej Zolnierkiewicz 
3323c6fd2807SJeff Garzik 		ata_timing_merge(&p, t, t, ATA_TIMING_CYCLE | ATA_TIMING_CYC8B);
3324c6fd2807SJeff Garzik 	}
3325c6fd2807SJeff Garzik 
3326c6fd2807SJeff Garzik 	/*
3327c6fd2807SJeff Garzik 	 * Convert the timing to bus clock counts.
3328c6fd2807SJeff Garzik 	 */
3329c6fd2807SJeff Garzik 
3330c6fd2807SJeff Garzik 	ata_timing_quantize(t, t, T, UT);
3331c6fd2807SJeff Garzik 
3332c6fd2807SJeff Garzik 	/*
3333c6fd2807SJeff Garzik 	 * Even in DMA/UDMA modes we still use PIO access for IDENTIFY,
3334c6fd2807SJeff Garzik 	 * S.M.A.R.T * and some other commands. We have to ensure that the
3335c6fd2807SJeff Garzik 	 * DMA cycle timing is slower/equal than the fastest PIO timing.
3336c6fd2807SJeff Garzik 	 */
3337c6fd2807SJeff Garzik 
3338fd3367afSAlan 	if (speed > XFER_PIO_6) {
3339c6fd2807SJeff Garzik 		ata_timing_compute(adev, adev->pio_mode, &p, T, UT);
3340c6fd2807SJeff Garzik 		ata_timing_merge(&p, t, t, ATA_TIMING_ALL);
3341c6fd2807SJeff Garzik 	}
3342c6fd2807SJeff Garzik 
3343c6fd2807SJeff Garzik 	/*
3344c6fd2807SJeff Garzik 	 * Lengthen active & recovery time so that cycle time is correct.
3345c6fd2807SJeff Garzik 	 */
3346c6fd2807SJeff Garzik 
3347c6fd2807SJeff Garzik 	if (t->act8b + t->rec8b < t->cyc8b) {
3348c6fd2807SJeff Garzik 		t->act8b += (t->cyc8b - (t->act8b + t->rec8b)) / 2;
3349c6fd2807SJeff Garzik 		t->rec8b = t->cyc8b - t->act8b;
3350c6fd2807SJeff Garzik 	}
3351c6fd2807SJeff Garzik 
3352c6fd2807SJeff Garzik 	if (t->active + t->recover < t->cycle) {
3353c6fd2807SJeff Garzik 		t->active += (t->cycle - (t->active + t->recover)) / 2;
3354c6fd2807SJeff Garzik 		t->recover = t->cycle - t->active;
3355c6fd2807SJeff Garzik 	}
33564f701d1eSAlan Cox 
33574f701d1eSAlan Cox 	/* In a few cases quantisation may produce enough errors to
33584f701d1eSAlan Cox 	   leave t->cycle too low for the sum of active and recovery
33594f701d1eSAlan Cox 	   if so we must correct this */
33604f701d1eSAlan Cox 	if (t->active + t->recover > t->cycle)
33614f701d1eSAlan Cox 		t->cycle = t->active + t->recover;
3362c6fd2807SJeff Garzik 
3363c6fd2807SJeff Garzik 	return 0;
3364c6fd2807SJeff Garzik }
3365c6fd2807SJeff Garzik 
3366c6fd2807SJeff Garzik /**
3367a0f79b92STejun Heo  *	ata_timing_cycle2mode - find xfer mode for the specified cycle duration
3368a0f79b92STejun Heo  *	@xfer_shift: ATA_SHIFT_* value for transfer type to examine.
3369a0f79b92STejun Heo  *	@cycle: cycle duration in ns
3370a0f79b92STejun Heo  *
3371a0f79b92STejun Heo  *	Return matching xfer mode for @cycle.  The returned mode is of
3372a0f79b92STejun Heo  *	the transfer type specified by @xfer_shift.  If @cycle is too
3373a0f79b92STejun Heo  *	slow for @xfer_shift, 0xff is returned.  If @cycle is faster
3374a0f79b92STejun Heo  *	than the fastest known mode, the fasted mode is returned.
3375a0f79b92STejun Heo  *
3376a0f79b92STejun Heo  *	LOCKING:
3377a0f79b92STejun Heo  *	None.
3378a0f79b92STejun Heo  *
3379a0f79b92STejun Heo  *	RETURNS:
3380a0f79b92STejun Heo  *	Matching xfer_mode, 0xff if no match found.
3381a0f79b92STejun Heo  */
3382a0f79b92STejun Heo u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
3383a0f79b92STejun Heo {
3384a0f79b92STejun Heo 	u8 base_mode = 0xff, last_mode = 0xff;
3385a0f79b92STejun Heo 	const struct ata_xfer_ent *ent;
3386a0f79b92STejun Heo 	const struct ata_timing *t;
3387a0f79b92STejun Heo 
3388a0f79b92STejun Heo 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
3389a0f79b92STejun Heo 		if (ent->shift == xfer_shift)
3390a0f79b92STejun Heo 			base_mode = ent->base;
3391a0f79b92STejun Heo 
3392a0f79b92STejun Heo 	for (t = ata_timing_find_mode(base_mode);
3393a0f79b92STejun Heo 	     t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
3394a0f79b92STejun Heo 		unsigned short this_cycle;
3395a0f79b92STejun Heo 
3396a0f79b92STejun Heo 		switch (xfer_shift) {
3397a0f79b92STejun Heo 		case ATA_SHIFT_PIO:
3398a0f79b92STejun Heo 		case ATA_SHIFT_MWDMA:
3399a0f79b92STejun Heo 			this_cycle = t->cycle;
3400a0f79b92STejun Heo 			break;
3401a0f79b92STejun Heo 		case ATA_SHIFT_UDMA:
3402a0f79b92STejun Heo 			this_cycle = t->udma;
3403a0f79b92STejun Heo 			break;
3404a0f79b92STejun Heo 		default:
3405a0f79b92STejun Heo 			return 0xff;
3406a0f79b92STejun Heo 		}
3407a0f79b92STejun Heo 
3408a0f79b92STejun Heo 		if (cycle > this_cycle)
3409a0f79b92STejun Heo 			break;
3410a0f79b92STejun Heo 
3411a0f79b92STejun Heo 		last_mode = t->mode;
3412a0f79b92STejun Heo 	}
3413a0f79b92STejun Heo 
3414a0f79b92STejun Heo 	return last_mode;
3415a0f79b92STejun Heo }
3416a0f79b92STejun Heo 
3417a0f79b92STejun Heo /**
3418c6fd2807SJeff Garzik  *	ata_down_xfermask_limit - adjust dev xfer masks downward
3419c6fd2807SJeff Garzik  *	@dev: Device to adjust xfer masks
3420458337dbSTejun Heo  *	@sel: ATA_DNXFER_* selector
3421c6fd2807SJeff Garzik  *
3422c6fd2807SJeff Garzik  *	Adjust xfer masks of @dev downward.  Note that this function
3423c6fd2807SJeff Garzik  *	does not apply the change.  Invoking ata_set_mode() afterwards
3424c6fd2807SJeff Garzik  *	will apply the limit.
3425c6fd2807SJeff Garzik  *
3426c6fd2807SJeff Garzik  *	LOCKING:
3427c6fd2807SJeff Garzik  *	Inherited from caller.
3428c6fd2807SJeff Garzik  *
3429c6fd2807SJeff Garzik  *	RETURNS:
3430c6fd2807SJeff Garzik  *	0 on success, negative errno on failure
3431c6fd2807SJeff Garzik  */
3432458337dbSTejun Heo int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
3433c6fd2807SJeff Garzik {
3434458337dbSTejun Heo 	char buf[32];
34357dc951aeSTejun Heo 	unsigned long orig_mask, xfer_mask;
34367dc951aeSTejun Heo 	unsigned long pio_mask, mwdma_mask, udma_mask;
3437458337dbSTejun Heo 	int quiet, highbit;
3438c6fd2807SJeff Garzik 
3439458337dbSTejun Heo 	quiet = !!(sel & ATA_DNXFER_QUIET);
3440458337dbSTejun Heo 	sel &= ~ATA_DNXFER_QUIET;
3441458337dbSTejun Heo 
3442458337dbSTejun Heo 	xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
3443458337dbSTejun Heo 						  dev->mwdma_mask,
3444c6fd2807SJeff Garzik 						  dev->udma_mask);
3445458337dbSTejun Heo 	ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
3446c6fd2807SJeff Garzik 
3447458337dbSTejun Heo 	switch (sel) {
3448458337dbSTejun Heo 	case ATA_DNXFER_PIO:
3449458337dbSTejun Heo 		highbit = fls(pio_mask) - 1;
3450458337dbSTejun Heo 		pio_mask &= ~(1 << highbit);
3451458337dbSTejun Heo 		break;
3452458337dbSTejun Heo 
3453458337dbSTejun Heo 	case ATA_DNXFER_DMA:
3454458337dbSTejun Heo 		if (udma_mask) {
3455458337dbSTejun Heo 			highbit = fls(udma_mask) - 1;
3456458337dbSTejun Heo 			udma_mask &= ~(1 << highbit);
3457458337dbSTejun Heo 			if (!udma_mask)
3458458337dbSTejun Heo 				return -ENOENT;
3459458337dbSTejun Heo 		} else if (mwdma_mask) {
3460458337dbSTejun Heo 			highbit = fls(mwdma_mask) - 1;
3461458337dbSTejun Heo 			mwdma_mask &= ~(1 << highbit);
3462458337dbSTejun Heo 			if (!mwdma_mask)
3463458337dbSTejun Heo 				return -ENOENT;
3464458337dbSTejun Heo 		}
3465458337dbSTejun Heo 		break;
3466458337dbSTejun Heo 
3467458337dbSTejun Heo 	case ATA_DNXFER_40C:
3468458337dbSTejun Heo 		udma_mask &= ATA_UDMA_MASK_40C;
3469458337dbSTejun Heo 		break;
3470458337dbSTejun Heo 
3471458337dbSTejun Heo 	case ATA_DNXFER_FORCE_PIO0:
3472458337dbSTejun Heo 		pio_mask &= 1;
347305b83605SGustavo A. R. Silva 		/* fall through */
3474458337dbSTejun Heo 	case ATA_DNXFER_FORCE_PIO:
3475458337dbSTejun Heo 		mwdma_mask = 0;
3476458337dbSTejun Heo 		udma_mask = 0;
3477458337dbSTejun Heo 		break;
3478458337dbSTejun Heo 
3479458337dbSTejun Heo 	default:
3480458337dbSTejun Heo 		BUG();
3481458337dbSTejun Heo 	}
3482458337dbSTejun Heo 
3483458337dbSTejun Heo 	xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
3484458337dbSTejun Heo 
3485458337dbSTejun Heo 	if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
3486458337dbSTejun Heo 		return -ENOENT;
3487458337dbSTejun Heo 
3488458337dbSTejun Heo 	if (!quiet) {
3489458337dbSTejun Heo 		if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
3490458337dbSTejun Heo 			snprintf(buf, sizeof(buf), "%s:%s",
3491458337dbSTejun Heo 				 ata_mode_string(xfer_mask),
3492458337dbSTejun Heo 				 ata_mode_string(xfer_mask & ATA_MASK_PIO));
3493458337dbSTejun Heo 		else
3494458337dbSTejun Heo 			snprintf(buf, sizeof(buf), "%s",
3495458337dbSTejun Heo 				 ata_mode_string(xfer_mask));
3496458337dbSTejun Heo 
3497a9a79dfeSJoe Perches 		ata_dev_warn(dev, "limiting speed to %s\n", buf);
3498458337dbSTejun Heo 	}
3499c6fd2807SJeff Garzik 
3500c6fd2807SJeff Garzik 	ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
3501c6fd2807SJeff Garzik 			    &dev->udma_mask);
3502c6fd2807SJeff Garzik 
3503c6fd2807SJeff Garzik 	return 0;
3504c6fd2807SJeff Garzik }
3505c6fd2807SJeff Garzik 
3506c6fd2807SJeff Garzik static int ata_dev_set_mode(struct ata_device *dev)
3507c6fd2807SJeff Garzik {
3508d0cb43b3STejun Heo 	struct ata_port *ap = dev->link->ap;
35099af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
3510d0cb43b3STejun Heo 	const bool nosetxfer = dev->horkage & ATA_HORKAGE_NOSETXFER;
35114055dee7STejun Heo 	const char *dev_err_whine = "";
35124055dee7STejun Heo 	int ign_dev_err = 0;
3513d0cb43b3STejun Heo 	unsigned int err_mask = 0;
3514c6fd2807SJeff Garzik 	int rc;
3515c6fd2807SJeff Garzik 
3516c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_PIO;
3517c6fd2807SJeff Garzik 	if (dev->xfer_shift == ATA_SHIFT_PIO)
3518c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_PIO;
3519c6fd2807SJeff Garzik 
3520d0cb43b3STejun Heo 	if (nosetxfer && ap->flags & ATA_FLAG_SATA && ata_id_is_sata(dev->id))
3521d0cb43b3STejun Heo 		dev_err_whine = " (SET_XFERMODE skipped)";
3522d0cb43b3STejun Heo 	else {
3523d0cb43b3STejun Heo 		if (nosetxfer)
3524a9a79dfeSJoe Perches 			ata_dev_warn(dev,
3525d0cb43b3STejun Heo 				     "NOSETXFER but PATA detected - can't "
3526d0cb43b3STejun Heo 				     "skip SETXFER, might malfunction\n");
3527c6fd2807SJeff Garzik 		err_mask = ata_dev_set_xfermode(dev);
3528d0cb43b3STejun Heo 	}
35292dcb407eSJeff Garzik 
35304055dee7STejun Heo 	if (err_mask & ~AC_ERR_DEV)
35314055dee7STejun Heo 		goto fail;
35322dcb407eSJeff Garzik 
35334055dee7STejun Heo 	/* revalidate */
3534baa1e78aSTejun Heo 	ehc->i.flags |= ATA_EHI_POST_SETMODE;
3535422c9daaSTejun Heo 	rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
3536baa1e78aSTejun Heo 	ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
3537c6fd2807SJeff Garzik 	if (rc)
3538c6fd2807SJeff Garzik 		return rc;
3539c6fd2807SJeff Garzik 
3540b93fda12SAlan Cox 	if (dev->xfer_shift == ATA_SHIFT_PIO) {
35414055dee7STejun Heo 		/* Old CFA may refuse this command, which is just fine */
3542b93fda12SAlan Cox 		if (ata_id_is_cfa(dev->id))
35434055dee7STejun Heo 			ign_dev_err = 1;
3544b93fda12SAlan Cox 		/* Catch several broken garbage emulations plus some pre
3545b93fda12SAlan Cox 		   ATA devices */
3546b93fda12SAlan Cox 		if (ata_id_major_version(dev->id) == 0 &&
35474055dee7STejun Heo 					dev->pio_mode <= XFER_PIO_2)
35484055dee7STejun Heo 			ign_dev_err = 1;
3549b93fda12SAlan Cox 		/* Some very old devices and some bad newer ones fail
3550b93fda12SAlan Cox 		   any kind of SET_XFERMODE request but support PIO0-2
3551b93fda12SAlan Cox 		   timings and no IORDY */
3552b93fda12SAlan Cox 		if (!ata_id_has_iordy(dev->id) && dev->pio_mode <= XFER_PIO_2)
3553b93fda12SAlan Cox 			ign_dev_err = 1;
3554b93fda12SAlan Cox 	}
35554055dee7STejun Heo 	/* Early MWDMA devices do DMA but don't allow DMA mode setting.
35564055dee7STejun Heo 	   Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
35574055dee7STejun Heo 	if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
35584055dee7STejun Heo 	    dev->dma_mode == XFER_MW_DMA_0 &&
35594055dee7STejun Heo 	    (dev->id[63] >> 8) & 1)
35604055dee7STejun Heo 		ign_dev_err = 1;
35614055dee7STejun Heo 
35624055dee7STejun Heo 	/* if the device is actually configured correctly, ignore dev err */
35634055dee7STejun Heo 	if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id)))
35644055dee7STejun Heo 		ign_dev_err = 1;
35654055dee7STejun Heo 
35664055dee7STejun Heo 	if (err_mask & AC_ERR_DEV) {
35674055dee7STejun Heo 		if (!ign_dev_err)
35684055dee7STejun Heo 			goto fail;
35694055dee7STejun Heo 		else
35704055dee7STejun Heo 			dev_err_whine = " (device error ignored)";
35714055dee7STejun Heo 	}
35724055dee7STejun Heo 
3573c6fd2807SJeff Garzik 	DPRINTK("xfer_shift=%u, xfer_mode=0x%x\n",
3574c6fd2807SJeff Garzik 		dev->xfer_shift, (int)dev->xfer_mode);
3575c6fd2807SJeff Garzik 
3576a9a79dfeSJoe Perches 	ata_dev_info(dev, "configured for %s%s\n",
35774055dee7STejun Heo 		     ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)),
35784055dee7STejun Heo 		     dev_err_whine);
35794055dee7STejun Heo 
3580c6fd2807SJeff Garzik 	return 0;
35814055dee7STejun Heo 
35824055dee7STejun Heo  fail:
3583a9a79dfeSJoe Perches 	ata_dev_err(dev, "failed to set xfermode (err_mask=0x%x)\n", err_mask);
35844055dee7STejun Heo 	return -EIO;
3585c6fd2807SJeff Garzik }
3586c6fd2807SJeff Garzik 
3587c6fd2807SJeff Garzik /**
358804351821SAlan  *	ata_do_set_mode - Program timings and issue SET FEATURES - XFER
35890260731fSTejun Heo  *	@link: link on which timings will be programmed
35901967b7ffSJoe Perches  *	@r_failed_dev: out parameter for failed device
3591c6fd2807SJeff Garzik  *
359204351821SAlan  *	Standard implementation of the function used to tune and set
359304351821SAlan  *	ATA device disk transfer mode (PIO3, UDMA6, etc.).  If
359404351821SAlan  *	ata_dev_set_mode() fails, pointer to the failing device is
3595c6fd2807SJeff Garzik  *	returned in @r_failed_dev.
3596c6fd2807SJeff Garzik  *
3597c6fd2807SJeff Garzik  *	LOCKING:
3598c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
3599c6fd2807SJeff Garzik  *
3600c6fd2807SJeff Garzik  *	RETURNS:
3601c6fd2807SJeff Garzik  *	0 on success, negative errno otherwise
3602c6fd2807SJeff Garzik  */
360304351821SAlan 
36040260731fSTejun Heo int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
3605c6fd2807SJeff Garzik {
36060260731fSTejun Heo 	struct ata_port *ap = link->ap;
3607c6fd2807SJeff Garzik 	struct ata_device *dev;
3608f58229f8STejun Heo 	int rc = 0, used_dma = 0, found = 0;
3609c6fd2807SJeff Garzik 
3610c6fd2807SJeff Garzik 	/* step 1: calculate xfer_mask */
36111eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
36127dc951aeSTejun Heo 		unsigned long pio_mask, dma_mask;
3613b3a70601SAlan Cox 		unsigned int mode_mask;
3614c6fd2807SJeff Garzik 
3615b3a70601SAlan Cox 		mode_mask = ATA_DMA_MASK_ATA;
3616b3a70601SAlan Cox 		if (dev->class == ATA_DEV_ATAPI)
3617b3a70601SAlan Cox 			mode_mask = ATA_DMA_MASK_ATAPI;
3618b3a70601SAlan Cox 		else if (ata_id_is_cfa(dev->id))
3619b3a70601SAlan Cox 			mode_mask = ATA_DMA_MASK_CFA;
3620b3a70601SAlan Cox 
3621c6fd2807SJeff Garzik 		ata_dev_xfermask(dev);
362233267325STejun Heo 		ata_force_xfermask(dev);
3623c6fd2807SJeff Garzik 
3624c6fd2807SJeff Garzik 		pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
3625b3a70601SAlan Cox 
3626b3a70601SAlan Cox 		if (libata_dma_mask & mode_mask)
362780a9c430SSergei Shtylyov 			dma_mask = ata_pack_xfermask(0, dev->mwdma_mask,
362880a9c430SSergei Shtylyov 						     dev->udma_mask);
3629b3a70601SAlan Cox 		else
3630b3a70601SAlan Cox 			dma_mask = 0;
3631b3a70601SAlan Cox 
3632c6fd2807SJeff Garzik 		dev->pio_mode = ata_xfer_mask2mode(pio_mask);
3633c6fd2807SJeff Garzik 		dev->dma_mode = ata_xfer_mask2mode(dma_mask);
3634c6fd2807SJeff Garzik 
3635c6fd2807SJeff Garzik 		found = 1;
3636b15b3ebaSAlan Cox 		if (ata_dma_enabled(dev))
3637c6fd2807SJeff Garzik 			used_dma = 1;
3638c6fd2807SJeff Garzik 	}
3639c6fd2807SJeff Garzik 	if (!found)
3640c6fd2807SJeff Garzik 		goto out;
3641c6fd2807SJeff Garzik 
3642c6fd2807SJeff Garzik 	/* step 2: always set host PIO timings */
36431eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
364470cd071eSTejun Heo 		if (dev->pio_mode == 0xff) {
3645a9a79dfeSJoe Perches 			ata_dev_warn(dev, "no PIO support\n");
3646c6fd2807SJeff Garzik 			rc = -EINVAL;
3647c6fd2807SJeff Garzik 			goto out;
3648c6fd2807SJeff Garzik 		}
3649c6fd2807SJeff Garzik 
3650c6fd2807SJeff Garzik 		dev->xfer_mode = dev->pio_mode;
3651c6fd2807SJeff Garzik 		dev->xfer_shift = ATA_SHIFT_PIO;
3652c6fd2807SJeff Garzik 		if (ap->ops->set_piomode)
3653c6fd2807SJeff Garzik 			ap->ops->set_piomode(ap, dev);
3654c6fd2807SJeff Garzik 	}
3655c6fd2807SJeff Garzik 
3656c6fd2807SJeff Garzik 	/* step 3: set host DMA timings */
36571eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
36581eca4365STejun Heo 		if (!ata_dma_enabled(dev))
3659c6fd2807SJeff Garzik 			continue;
3660c6fd2807SJeff Garzik 
3661c6fd2807SJeff Garzik 		dev->xfer_mode = dev->dma_mode;
3662c6fd2807SJeff Garzik 		dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
3663c6fd2807SJeff Garzik 		if (ap->ops->set_dmamode)
3664c6fd2807SJeff Garzik 			ap->ops->set_dmamode(ap, dev);
3665c6fd2807SJeff Garzik 	}
3666c6fd2807SJeff Garzik 
3667c6fd2807SJeff Garzik 	/* step 4: update devices' xfer mode */
36681eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
3669c6fd2807SJeff Garzik 		rc = ata_dev_set_mode(dev);
3670c6fd2807SJeff Garzik 		if (rc)
3671c6fd2807SJeff Garzik 			goto out;
3672c6fd2807SJeff Garzik 	}
3673c6fd2807SJeff Garzik 
3674c6fd2807SJeff Garzik 	/* Record simplex status. If we selected DMA then the other
3675c6fd2807SJeff Garzik 	 * host channels are not permitted to do so.
3676c6fd2807SJeff Garzik 	 */
3677cca3974eSJeff Garzik 	if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
3678032af1ceSAlan 		ap->host->simplex_claimed = ap;
3679c6fd2807SJeff Garzik 
3680c6fd2807SJeff Garzik  out:
3681c6fd2807SJeff Garzik 	if (rc)
3682c6fd2807SJeff Garzik 		*r_failed_dev = dev;
3683c6fd2807SJeff Garzik 	return rc;
3684c6fd2807SJeff Garzik }
3685c6fd2807SJeff Garzik 
3686c6fd2807SJeff Garzik /**
3687aa2731adSTejun Heo  *	ata_wait_ready - wait for link to become ready
3688aa2731adSTejun Heo  *	@link: link to be waited on
3689aa2731adSTejun Heo  *	@deadline: deadline jiffies for the operation
3690aa2731adSTejun Heo  *	@check_ready: callback to check link readiness
3691aa2731adSTejun Heo  *
3692aa2731adSTejun Heo  *	Wait for @link to become ready.  @check_ready should return
3693aa2731adSTejun Heo  *	positive number if @link is ready, 0 if it isn't, -ENODEV if
3694aa2731adSTejun Heo  *	link doesn't seem to be occupied, other errno for other error
3695aa2731adSTejun Heo  *	conditions.
3696aa2731adSTejun Heo  *
3697aa2731adSTejun Heo  *	Transient -ENODEV conditions are allowed for
3698aa2731adSTejun Heo  *	ATA_TMOUT_FF_WAIT.
3699aa2731adSTejun Heo  *
3700aa2731adSTejun Heo  *	LOCKING:
3701aa2731adSTejun Heo  *	EH context.
3702aa2731adSTejun Heo  *
3703aa2731adSTejun Heo  *	RETURNS:
3704c9b5560aSMasanari Iida  *	0 if @link is ready before @deadline; otherwise, -errno.
3705aa2731adSTejun Heo  */
3706aa2731adSTejun Heo int ata_wait_ready(struct ata_link *link, unsigned long deadline,
3707aa2731adSTejun Heo 		   int (*check_ready)(struct ata_link *link))
3708aa2731adSTejun Heo {
3709aa2731adSTejun Heo 	unsigned long start = jiffies;
3710b48d58f5STejun Heo 	unsigned long nodev_deadline;
3711aa2731adSTejun Heo 	int warned = 0;
3712aa2731adSTejun Heo 
3713b48d58f5STejun Heo 	/* choose which 0xff timeout to use, read comment in libata.h */
3714b48d58f5STejun Heo 	if (link->ap->host->flags & ATA_HOST_PARALLEL_SCAN)
3715b48d58f5STejun Heo 		nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT_LONG);
3716b48d58f5STejun Heo 	else
3717b48d58f5STejun Heo 		nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT);
3718b48d58f5STejun Heo 
3719b1c72916STejun Heo 	/* Slave readiness can't be tested separately from master.  On
3720b1c72916STejun Heo 	 * M/S emulation configuration, this function should be called
3721b1c72916STejun Heo 	 * only on the master and it will handle both master and slave.
3722b1c72916STejun Heo 	 */
3723b1c72916STejun Heo 	WARN_ON(link == link->ap->slave_link);
3724b1c72916STejun Heo 
3725aa2731adSTejun Heo 	if (time_after(nodev_deadline, deadline))
3726aa2731adSTejun Heo 		nodev_deadline = deadline;
3727aa2731adSTejun Heo 
3728aa2731adSTejun Heo 	while (1) {
3729aa2731adSTejun Heo 		unsigned long now = jiffies;
3730aa2731adSTejun Heo 		int ready, tmp;
3731aa2731adSTejun Heo 
3732aa2731adSTejun Heo 		ready = tmp = check_ready(link);
3733aa2731adSTejun Heo 		if (ready > 0)
3734aa2731adSTejun Heo 			return 0;
3735aa2731adSTejun Heo 
3736b48d58f5STejun Heo 		/*
3737b48d58f5STejun Heo 		 * -ENODEV could be transient.  Ignore -ENODEV if link
3738aa2731adSTejun Heo 		 * is online.  Also, some SATA devices take a long
3739b48d58f5STejun Heo 		 * time to clear 0xff after reset.  Wait for
3740b48d58f5STejun Heo 		 * ATA_TMOUT_FF_WAIT[_LONG] on -ENODEV if link isn't
3741b48d58f5STejun Heo 		 * offline.
3742aa2731adSTejun Heo 		 *
3743aa2731adSTejun Heo 		 * Note that some PATA controllers (pata_ali) explode
3744aa2731adSTejun Heo 		 * if status register is read more than once when
3745aa2731adSTejun Heo 		 * there's no device attached.
3746aa2731adSTejun Heo 		 */
3747aa2731adSTejun Heo 		if (ready == -ENODEV) {
3748aa2731adSTejun Heo 			if (ata_link_online(link))
3749aa2731adSTejun Heo 				ready = 0;
3750aa2731adSTejun Heo 			else if ((link->ap->flags & ATA_FLAG_SATA) &&
3751aa2731adSTejun Heo 				 !ata_link_offline(link) &&
3752aa2731adSTejun Heo 				 time_before(now, nodev_deadline))
3753aa2731adSTejun Heo 				ready = 0;
3754aa2731adSTejun Heo 		}
3755aa2731adSTejun Heo 
3756aa2731adSTejun Heo 		if (ready)
3757aa2731adSTejun Heo 			return ready;
3758aa2731adSTejun Heo 		if (time_after(now, deadline))
3759aa2731adSTejun Heo 			return -EBUSY;
3760aa2731adSTejun Heo 
3761aa2731adSTejun Heo 		if (!warned && time_after(now, start + 5 * HZ) &&
3762aa2731adSTejun Heo 		    (deadline - now > 3 * HZ)) {
3763a9a79dfeSJoe Perches 			ata_link_warn(link,
3764aa2731adSTejun Heo 				"link is slow to respond, please be patient "
3765aa2731adSTejun Heo 				"(ready=%d)\n", tmp);
3766aa2731adSTejun Heo 			warned = 1;
3767aa2731adSTejun Heo 		}
3768aa2731adSTejun Heo 
376997750cebSTejun Heo 		ata_msleep(link->ap, 50);
3770aa2731adSTejun Heo 	}
3771aa2731adSTejun Heo }
3772aa2731adSTejun Heo 
3773aa2731adSTejun Heo /**
3774aa2731adSTejun Heo  *	ata_wait_after_reset - wait for link to become ready after reset
3775aa2731adSTejun Heo  *	@link: link to be waited on
3776aa2731adSTejun Heo  *	@deadline: deadline jiffies for the operation
3777aa2731adSTejun Heo  *	@check_ready: callback to check link readiness
3778aa2731adSTejun Heo  *
3779aa2731adSTejun Heo  *	Wait for @link to become ready after reset.
3780aa2731adSTejun Heo  *
3781aa2731adSTejun Heo  *	LOCKING:
3782aa2731adSTejun Heo  *	EH context.
3783aa2731adSTejun Heo  *
3784aa2731adSTejun Heo  *	RETURNS:
3785c9b5560aSMasanari Iida  *	0 if @link is ready before @deadline; otherwise, -errno.
3786aa2731adSTejun Heo  */
37872b4221bbSHarvey Harrison int ata_wait_after_reset(struct ata_link *link, unsigned long deadline,
3788aa2731adSTejun Heo 				int (*check_ready)(struct ata_link *link))
3789aa2731adSTejun Heo {
379097750cebSTejun Heo 	ata_msleep(link->ap, ATA_WAIT_AFTER_RESET);
3791aa2731adSTejun Heo 
3792aa2731adSTejun Heo 	return ata_wait_ready(link, deadline, check_ready);
3793aa2731adSTejun Heo }
3794aa2731adSTejun Heo 
3795aa2731adSTejun Heo /**
3796936fd732STejun Heo  *	sata_link_debounce - debounce SATA phy status
3797936fd732STejun Heo  *	@link: ATA link to debounce SATA phy status for
3798c9b5560aSMasanari Iida  *	@params: timing parameters { interval, duration, timeout } in msec
3799d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
3800c6fd2807SJeff Garzik  *
3801936fd732STejun Heo  *	Make sure SStatus of @link reaches stable state, determined by
3802c6fd2807SJeff Garzik  *	holding the same value where DET is not 1 for @duration polled
3803c6fd2807SJeff Garzik  *	every @interval, before @timeout.  Timeout constraints the
3804d4b2bab4STejun Heo  *	beginning of the stable state.  Because DET gets stuck at 1 on
3805d4b2bab4STejun Heo  *	some controllers after hot unplugging, this functions waits
3806c6fd2807SJeff Garzik  *	until timeout then returns 0 if DET is stable at 1.
3807c6fd2807SJeff Garzik  *
3808d4b2bab4STejun Heo  *	@timeout is further limited by @deadline.  The sooner of the
3809d4b2bab4STejun Heo  *	two is used.
3810d4b2bab4STejun Heo  *
3811c6fd2807SJeff Garzik  *	LOCKING:
3812c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3813c6fd2807SJeff Garzik  *
3814c6fd2807SJeff Garzik  *	RETURNS:
3815c6fd2807SJeff Garzik  *	0 on success, -errno on failure.
3816c6fd2807SJeff Garzik  */
3817936fd732STejun Heo int sata_link_debounce(struct ata_link *link, const unsigned long *params,
3818d4b2bab4STejun Heo 		       unsigned long deadline)
3819c6fd2807SJeff Garzik {
3820341c2c95STejun Heo 	unsigned long interval = params[0];
3821341c2c95STejun Heo 	unsigned long duration = params[1];
3822d4b2bab4STejun Heo 	unsigned long last_jiffies, t;
3823c6fd2807SJeff Garzik 	u32 last, cur;
3824c6fd2807SJeff Garzik 	int rc;
3825c6fd2807SJeff Garzik 
3826341c2c95STejun Heo 	t = ata_deadline(jiffies, params[2]);
3827d4b2bab4STejun Heo 	if (time_before(t, deadline))
3828d4b2bab4STejun Heo 		deadline = t;
3829d4b2bab4STejun Heo 
3830936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3831c6fd2807SJeff Garzik 		return rc;
3832c6fd2807SJeff Garzik 	cur &= 0xf;
3833c6fd2807SJeff Garzik 
3834c6fd2807SJeff Garzik 	last = cur;
3835c6fd2807SJeff Garzik 	last_jiffies = jiffies;
3836c6fd2807SJeff Garzik 
3837c6fd2807SJeff Garzik 	while (1) {
383897750cebSTejun Heo 		ata_msleep(link->ap, interval);
3839936fd732STejun Heo 		if ((rc = sata_scr_read(link, SCR_STATUS, &cur)))
3840c6fd2807SJeff Garzik 			return rc;
3841c6fd2807SJeff Garzik 		cur &= 0xf;
3842c6fd2807SJeff Garzik 
3843c6fd2807SJeff Garzik 		/* DET stable? */
3844c6fd2807SJeff Garzik 		if (cur == last) {
3845d4b2bab4STejun Heo 			if (cur == 1 && time_before(jiffies, deadline))
3846c6fd2807SJeff Garzik 				continue;
3847341c2c95STejun Heo 			if (time_after(jiffies,
3848341c2c95STejun Heo 				       ata_deadline(last_jiffies, duration)))
3849c6fd2807SJeff Garzik 				return 0;
3850c6fd2807SJeff Garzik 			continue;
3851c6fd2807SJeff Garzik 		}
3852c6fd2807SJeff Garzik 
3853c6fd2807SJeff Garzik 		/* unstable, start over */
3854c6fd2807SJeff Garzik 		last = cur;
3855c6fd2807SJeff Garzik 		last_jiffies = jiffies;
3856c6fd2807SJeff Garzik 
3857f1545154STejun Heo 		/* Check deadline.  If debouncing failed, return
3858f1545154STejun Heo 		 * -EPIPE to tell upper layer to lower link speed.
3859f1545154STejun Heo 		 */
3860d4b2bab4STejun Heo 		if (time_after(jiffies, deadline))
3861f1545154STejun Heo 			return -EPIPE;
3862c6fd2807SJeff Garzik 	}
3863c6fd2807SJeff Garzik }
3864c6fd2807SJeff Garzik 
3865c6fd2807SJeff Garzik /**
3866936fd732STejun Heo  *	sata_link_resume - resume SATA link
3867936fd732STejun Heo  *	@link: ATA link to resume SATA
3868c9b5560aSMasanari Iida  *	@params: timing parameters { interval, duration, timeout } in msec
3869d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
3870c6fd2807SJeff Garzik  *
3871936fd732STejun Heo  *	Resume SATA phy @link and debounce it.
3872c6fd2807SJeff Garzik  *
3873c6fd2807SJeff Garzik  *	LOCKING:
3874c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3875c6fd2807SJeff Garzik  *
3876c6fd2807SJeff Garzik  *	RETURNS:
3877c6fd2807SJeff Garzik  *	0 on success, -errno on failure.
3878c6fd2807SJeff Garzik  */
3879936fd732STejun Heo int sata_link_resume(struct ata_link *link, const unsigned long *params,
3880d4b2bab4STejun Heo 		     unsigned long deadline)
3881c6fd2807SJeff Garzik {
38825040ab67STejun Heo 	int tries = ATA_LINK_RESUME_TRIES;
3883ac371987STejun Heo 	u32 scontrol, serror;
3884c6fd2807SJeff Garzik 	int rc;
3885c6fd2807SJeff Garzik 
3886936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
3887c6fd2807SJeff Garzik 		return rc;
3888c6fd2807SJeff Garzik 
38895040ab67STejun Heo 	/*
38905040ab67STejun Heo 	 * Writes to SControl sometimes get ignored under certain
38915040ab67STejun Heo 	 * controllers (ata_piix SIDPR).  Make sure DET actually is
38925040ab67STejun Heo 	 * cleared.
38935040ab67STejun Heo 	 */
38945040ab67STejun Heo 	do {
3895c6fd2807SJeff Garzik 		scontrol = (scontrol & 0x0f0) | 0x300;
3896936fd732STejun Heo 		if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
3897c6fd2807SJeff Garzik 			return rc;
38985040ab67STejun Heo 		/*
38995040ab67STejun Heo 		 * Some PHYs react badly if SStatus is pounded
39005040ab67STejun Heo 		 * immediately after resuming.  Delay 200ms before
39015040ab67STejun Heo 		 * debouncing.
3902c6fd2807SJeff Garzik 		 */
3903e39b2bb3SDanesh Petigara 		if (!(link->flags & ATA_LFLAG_NO_DB_DELAY))
390497750cebSTejun Heo 			ata_msleep(link->ap, 200);
3905c6fd2807SJeff Garzik 
39065040ab67STejun Heo 		/* is SControl restored correctly? */
39075040ab67STejun Heo 		if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
39085040ab67STejun Heo 			return rc;
39095040ab67STejun Heo 	} while ((scontrol & 0xf0f) != 0x300 && --tries);
39105040ab67STejun Heo 
39115040ab67STejun Heo 	if ((scontrol & 0xf0f) != 0x300) {
391238941c95STejun Heo 		ata_link_warn(link, "failed to resume link (SControl %X)\n",
39135040ab67STejun Heo 			     scontrol);
39145040ab67STejun Heo 		return 0;
39155040ab67STejun Heo 	}
39165040ab67STejun Heo 
39175040ab67STejun Heo 	if (tries < ATA_LINK_RESUME_TRIES)
3918a9a79dfeSJoe Perches 		ata_link_warn(link, "link resume succeeded after %d retries\n",
39195040ab67STejun Heo 			      ATA_LINK_RESUME_TRIES - tries);
39205040ab67STejun Heo 
3921ac371987STejun Heo 	if ((rc = sata_link_debounce(link, params, deadline)))
3922ac371987STejun Heo 		return rc;
3923ac371987STejun Heo 
3924f046519fSTejun Heo 	/* clear SError, some PHYs require this even for SRST to work */
3925ac371987STejun Heo 	if (!(rc = sata_scr_read(link, SCR_ERROR, &serror)))
3926ac371987STejun Heo 		rc = sata_scr_write(link, SCR_ERROR, serror);
3927ac371987STejun Heo 
3928f046519fSTejun Heo 	return rc != -EINVAL ? rc : 0;
3929c6fd2807SJeff Garzik }
3930c6fd2807SJeff Garzik 
3931c6fd2807SJeff Garzik /**
39321152b261STejun Heo  *	sata_link_scr_lpm - manipulate SControl IPM and SPM fields
39331152b261STejun Heo  *	@link: ATA link to manipulate SControl for
39341152b261STejun Heo  *	@policy: LPM policy to configure
39351152b261STejun Heo  *	@spm_wakeup: initiate LPM transition to active state
39361152b261STejun Heo  *
39371152b261STejun Heo  *	Manipulate the IPM field of the SControl register of @link
39381152b261STejun Heo  *	according to @policy.  If @policy is ATA_LPM_MAX_POWER and
39391152b261STejun Heo  *	@spm_wakeup is %true, the SPM field is manipulated to wake up
39401152b261STejun Heo  *	the link.  This function also clears PHYRDY_CHG before
39411152b261STejun Heo  *	returning.
39421152b261STejun Heo  *
39431152b261STejun Heo  *	LOCKING:
39441152b261STejun Heo  *	EH context.
39451152b261STejun Heo  *
39461152b261STejun Heo  *	RETURNS:
39478485187bSMasanari Iida  *	0 on success, -errno otherwise.
39481152b261STejun Heo  */
39491152b261STejun Heo int sata_link_scr_lpm(struct ata_link *link, enum ata_lpm_policy policy,
39501152b261STejun Heo 		      bool spm_wakeup)
39511152b261STejun Heo {
39521152b261STejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
39531152b261STejun Heo 	bool woken_up = false;
39541152b261STejun Heo 	u32 scontrol;
39551152b261STejun Heo 	int rc;
39561152b261STejun Heo 
39571152b261STejun Heo 	rc = sata_scr_read(link, SCR_CONTROL, &scontrol);
39581152b261STejun Heo 	if (rc)
39591152b261STejun Heo 		return rc;
39601152b261STejun Heo 
39611152b261STejun Heo 	switch (policy) {
39621152b261STejun Heo 	case ATA_LPM_MAX_POWER:
39631152b261STejun Heo 		/* disable all LPM transitions */
396465fe1f0fSShane Huang 		scontrol |= (0x7 << 8);
39651152b261STejun Heo 		/* initiate transition to active state */
39661152b261STejun Heo 		if (spm_wakeup) {
39671152b261STejun Heo 			scontrol |= (0x4 << 12);
39681152b261STejun Heo 			woken_up = true;
39691152b261STejun Heo 		}
39701152b261STejun Heo 		break;
39711152b261STejun Heo 	case ATA_LPM_MED_POWER:
39721152b261STejun Heo 		/* allow LPM to PARTIAL */
39731152b261STejun Heo 		scontrol &= ~(0x1 << 8);
397465fe1f0fSShane Huang 		scontrol |= (0x6 << 8);
39751152b261STejun Heo 		break;
3976f4ac6476SHans de Goede 	case ATA_LPM_MED_POWER_WITH_DIPM:
39771152b261STejun Heo 	case ATA_LPM_MIN_POWER:
39788a745f1fSKristen Carlson Accardi 		if (ata_link_nr_enabled(link) > 0)
39791152b261STejun Heo 			/* no restrictions on LPM transitions */
398065fe1f0fSShane Huang 			scontrol &= ~(0x7 << 8);
39818a745f1fSKristen Carlson Accardi 		else {
39828a745f1fSKristen Carlson Accardi 			/* empty port, power off */
39838a745f1fSKristen Carlson Accardi 			scontrol &= ~0xf;
39848a745f1fSKristen Carlson Accardi 			scontrol |= (0x1 << 2);
39858a745f1fSKristen Carlson Accardi 		}
39861152b261STejun Heo 		break;
39871152b261STejun Heo 	default:
39881152b261STejun Heo 		WARN_ON(1);
39891152b261STejun Heo 	}
39901152b261STejun Heo 
39911152b261STejun Heo 	rc = sata_scr_write(link, SCR_CONTROL, scontrol);
39921152b261STejun Heo 	if (rc)
39931152b261STejun Heo 		return rc;
39941152b261STejun Heo 
39951152b261STejun Heo 	/* give the link time to transit out of LPM state */
39961152b261STejun Heo 	if (woken_up)
39971152b261STejun Heo 		msleep(10);
39981152b261STejun Heo 
39991152b261STejun Heo 	/* clear PHYRDY_CHG from SError */
40001152b261STejun Heo 	ehc->i.serror &= ~SERR_PHYRDY_CHG;
40011152b261STejun Heo 	return sata_scr_write(link, SCR_ERROR, SERR_PHYRDY_CHG);
40021152b261STejun Heo }
40031152b261STejun Heo 
40041152b261STejun Heo /**
40050aa1113dSTejun Heo  *	ata_std_prereset - prepare for reset
4006cc0680a5STejun Heo  *	@link: ATA link to be reset
4007d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
4008c6fd2807SJeff Garzik  *
4009cc0680a5STejun Heo  *	@link is about to be reset.  Initialize it.  Failure from
4010b8cffc6aSTejun Heo  *	prereset makes libata abort whole reset sequence and give up
4011b8cffc6aSTejun Heo  *	that port, so prereset should be best-effort.  It does its
4012b8cffc6aSTejun Heo  *	best to prepare for reset sequence but if things go wrong, it
4013b8cffc6aSTejun Heo  *	should just whine, not fail.
4014c6fd2807SJeff Garzik  *
4015c6fd2807SJeff Garzik  *	LOCKING:
4016c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
4017c6fd2807SJeff Garzik  *
4018c6fd2807SJeff Garzik  *	RETURNS:
4019c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
4020c6fd2807SJeff Garzik  */
40210aa1113dSTejun Heo int ata_std_prereset(struct ata_link *link, unsigned long deadline)
4022c6fd2807SJeff Garzik {
4023cc0680a5STejun Heo 	struct ata_port *ap = link->ap;
4024936fd732STejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
4025c6fd2807SJeff Garzik 	const unsigned long *timing = sata_ehc_deb_timing(ehc);
4026c6fd2807SJeff Garzik 	int rc;
4027c6fd2807SJeff Garzik 
4028c6fd2807SJeff Garzik 	/* if we're about to do hardreset, nothing more to do */
4029c6fd2807SJeff Garzik 	if (ehc->i.action & ATA_EH_HARDRESET)
4030c6fd2807SJeff Garzik 		return 0;
4031c6fd2807SJeff Garzik 
4032936fd732STejun Heo 	/* if SATA, resume link */
4033a16abc0bSTejun Heo 	if (ap->flags & ATA_FLAG_SATA) {
4034936fd732STejun Heo 		rc = sata_link_resume(link, timing, deadline);
4035b8cffc6aSTejun Heo 		/* whine about phy resume failure but proceed */
4036b8cffc6aSTejun Heo 		if (rc && rc != -EOPNOTSUPP)
4037a9a79dfeSJoe Perches 			ata_link_warn(link,
4038a9a79dfeSJoe Perches 				      "failed to resume link for reset (errno=%d)\n",
4039a9a79dfeSJoe Perches 				      rc);
4040c6fd2807SJeff Garzik 	}
4041c6fd2807SJeff Garzik 
404245db2f6cSTejun Heo 	/* no point in trying softreset on offline link */
4043b1c72916STejun Heo 	if (ata_phys_link_offline(link))
404445db2f6cSTejun Heo 		ehc->i.action &= ~ATA_EH_SOFTRESET;
404545db2f6cSTejun Heo 
4046c6fd2807SJeff Garzik 	return 0;
4047c6fd2807SJeff Garzik }
4048c6fd2807SJeff Garzik 
4049c6fd2807SJeff Garzik /**
4050cc0680a5STejun Heo  *	sata_link_hardreset - reset link via SATA phy reset
4051cc0680a5STejun Heo  *	@link: link to reset
4052c9b5560aSMasanari Iida  *	@timing: timing parameters { interval, duration, timeout } in msec
4053d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
40549dadd45bSTejun Heo  *	@online: optional out parameter indicating link onlineness
40559dadd45bSTejun Heo  *	@check_ready: optional callback to check link readiness
4056c6fd2807SJeff Garzik  *
4057cc0680a5STejun Heo  *	SATA phy-reset @link using DET bits of SControl register.
40589dadd45bSTejun Heo  *	After hardreset, link readiness is waited upon using
40599dadd45bSTejun Heo  *	ata_wait_ready() if @check_ready is specified.  LLDs are
40609dadd45bSTejun Heo  *	allowed to not specify @check_ready and wait itself after this
40619dadd45bSTejun Heo  *	function returns.  Device classification is LLD's
40629dadd45bSTejun Heo  *	responsibility.
40639dadd45bSTejun Heo  *
40649dadd45bSTejun Heo  *	*@online is set to one iff reset succeeded and @link is online
40659dadd45bSTejun Heo  *	after reset.
4066c6fd2807SJeff Garzik  *
4067c6fd2807SJeff Garzik  *	LOCKING:
4068c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
4069c6fd2807SJeff Garzik  *
4070c6fd2807SJeff Garzik  *	RETURNS:
4071c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
4072c6fd2807SJeff Garzik  */
4073cc0680a5STejun Heo int sata_link_hardreset(struct ata_link *link, const unsigned long *timing,
40749dadd45bSTejun Heo 			unsigned long deadline,
40759dadd45bSTejun Heo 			bool *online, int (*check_ready)(struct ata_link *))
4076c6fd2807SJeff Garzik {
4077c6fd2807SJeff Garzik 	u32 scontrol;
4078c6fd2807SJeff Garzik 	int rc;
4079c6fd2807SJeff Garzik 
4080c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
4081c6fd2807SJeff Garzik 
40829dadd45bSTejun Heo 	if (online)
40839dadd45bSTejun Heo 		*online = false;
40849dadd45bSTejun Heo 
4085936fd732STejun Heo 	if (sata_set_spd_needed(link)) {
4086c6fd2807SJeff Garzik 		/* SATA spec says nothing about how to reconfigure
4087c6fd2807SJeff Garzik 		 * spd.  To be on the safe side, turn off phy during
4088c6fd2807SJeff Garzik 		 * reconfiguration.  This works for at least ICH7 AHCI
4089c6fd2807SJeff Garzik 		 * and Sil3124.
4090c6fd2807SJeff Garzik 		 */
4091936fd732STejun Heo 		if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
4092b6103f6dSTejun Heo 			goto out;
4093c6fd2807SJeff Garzik 
4094cea0d336SJeff Garzik 		scontrol = (scontrol & 0x0f0) | 0x304;
4095c6fd2807SJeff Garzik 
4096936fd732STejun Heo 		if ((rc = sata_scr_write(link, SCR_CONTROL, scontrol)))
4097b6103f6dSTejun Heo 			goto out;
4098c6fd2807SJeff Garzik 
4099936fd732STejun Heo 		sata_set_spd(link);
4100c6fd2807SJeff Garzik 	}
4101c6fd2807SJeff Garzik 
4102c6fd2807SJeff Garzik 	/* issue phy wake/reset */
4103936fd732STejun Heo 	if ((rc = sata_scr_read(link, SCR_CONTROL, &scontrol)))
4104b6103f6dSTejun Heo 		goto out;
4105c6fd2807SJeff Garzik 
4106c6fd2807SJeff Garzik 	scontrol = (scontrol & 0x0f0) | 0x301;
4107c6fd2807SJeff Garzik 
4108936fd732STejun Heo 	if ((rc = sata_scr_write_flush(link, SCR_CONTROL, scontrol)))
4109b6103f6dSTejun Heo 		goto out;
4110c6fd2807SJeff Garzik 
4111c6fd2807SJeff Garzik 	/* Couldn't find anything in SATA I/II specs, but AHCI-1.1
4112c6fd2807SJeff Garzik 	 * 10.4.2 says at least 1 ms.
4113c6fd2807SJeff Garzik 	 */
411497750cebSTejun Heo 	ata_msleep(link->ap, 1);
4115c6fd2807SJeff Garzik 
4116936fd732STejun Heo 	/* bring link back */
4117936fd732STejun Heo 	rc = sata_link_resume(link, timing, deadline);
41189dadd45bSTejun Heo 	if (rc)
41199dadd45bSTejun Heo 		goto out;
41209dadd45bSTejun Heo 	/* if link is offline nothing more to do */
4121b1c72916STejun Heo 	if (ata_phys_link_offline(link))
41229dadd45bSTejun Heo 		goto out;
41239dadd45bSTejun Heo 
41249dadd45bSTejun Heo 	/* Link is online.  From this point, -ENODEV too is an error. */
41259dadd45bSTejun Heo 	if (online)
41269dadd45bSTejun Heo 		*online = true;
41279dadd45bSTejun Heo 
4128071f44b1STejun Heo 	if (sata_pmp_supported(link->ap) && ata_is_host_link(link)) {
41299dadd45bSTejun Heo 		/* If PMP is supported, we have to do follow-up SRST.
41309dadd45bSTejun Heo 		 * Some PMPs don't send D2H Reg FIS after hardreset if
41319dadd45bSTejun Heo 		 * the first port is empty.  Wait only for
41329dadd45bSTejun Heo 		 * ATA_TMOUT_PMP_SRST_WAIT.
41339dadd45bSTejun Heo 		 */
41349dadd45bSTejun Heo 		if (check_ready) {
41359dadd45bSTejun Heo 			unsigned long pmp_deadline;
41369dadd45bSTejun Heo 
4137341c2c95STejun Heo 			pmp_deadline = ata_deadline(jiffies,
4138341c2c95STejun Heo 						    ATA_TMOUT_PMP_SRST_WAIT);
41399dadd45bSTejun Heo 			if (time_after(pmp_deadline, deadline))
41409dadd45bSTejun Heo 				pmp_deadline = deadline;
41419dadd45bSTejun Heo 			ata_wait_ready(link, pmp_deadline, check_ready);
41429dadd45bSTejun Heo 		}
41439dadd45bSTejun Heo 		rc = -EAGAIN;
41449dadd45bSTejun Heo 		goto out;
41459dadd45bSTejun Heo 	}
41469dadd45bSTejun Heo 
41479dadd45bSTejun Heo 	rc = 0;
41489dadd45bSTejun Heo 	if (check_ready)
41499dadd45bSTejun Heo 		rc = ata_wait_ready(link, deadline, check_ready);
4150b6103f6dSTejun Heo  out:
41510cbf0711STejun Heo 	if (rc && rc != -EAGAIN) {
41520cbf0711STejun Heo 		/* online is set iff link is online && reset succeeded */
41530cbf0711STejun Heo 		if (online)
41540cbf0711STejun Heo 			*online = false;
4155a9a79dfeSJoe Perches 		ata_link_err(link, "COMRESET failed (errno=%d)\n", rc);
41560cbf0711STejun Heo 	}
4157b6103f6dSTejun Heo 	DPRINTK("EXIT, rc=%d\n", rc);
4158b6103f6dSTejun Heo 	return rc;
4159b6103f6dSTejun Heo }
4160b6103f6dSTejun Heo 
4161b6103f6dSTejun Heo /**
416257c9efdfSTejun Heo  *	sata_std_hardreset - COMRESET w/o waiting or classification
416357c9efdfSTejun Heo  *	@link: link to reset
416457c9efdfSTejun Heo  *	@class: resulting class of attached device
416557c9efdfSTejun Heo  *	@deadline: deadline jiffies for the operation
416657c9efdfSTejun Heo  *
416757c9efdfSTejun Heo  *	Standard SATA COMRESET w/o waiting or classification.
416857c9efdfSTejun Heo  *
416957c9efdfSTejun Heo  *	LOCKING:
417057c9efdfSTejun Heo  *	Kernel thread context (may sleep)
417157c9efdfSTejun Heo  *
417257c9efdfSTejun Heo  *	RETURNS:
417357c9efdfSTejun Heo  *	0 if link offline, -EAGAIN if link online, -errno on errors.
417457c9efdfSTejun Heo  */
417557c9efdfSTejun Heo int sata_std_hardreset(struct ata_link *link, unsigned int *class,
417657c9efdfSTejun Heo 		       unsigned long deadline)
417757c9efdfSTejun Heo {
417857c9efdfSTejun Heo 	const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
417957c9efdfSTejun Heo 	bool online;
418057c9efdfSTejun Heo 	int rc;
418157c9efdfSTejun Heo 
418257c9efdfSTejun Heo 	/* do hardreset */
418357c9efdfSTejun Heo 	rc = sata_link_hardreset(link, timing, deadline, &online, NULL);
418457c9efdfSTejun Heo 	return online ? -EAGAIN : rc;
418557c9efdfSTejun Heo }
418657c9efdfSTejun Heo 
418757c9efdfSTejun Heo /**
4188203c75b8STejun Heo  *	ata_std_postreset - standard postreset callback
4189cc0680a5STejun Heo  *	@link: the target ata_link
4190c6fd2807SJeff Garzik  *	@classes: classes of attached devices
4191c6fd2807SJeff Garzik  *
4192c6fd2807SJeff Garzik  *	This function is invoked after a successful reset.  Note that
4193c6fd2807SJeff Garzik  *	the device might have been reset more than once using
4194c6fd2807SJeff Garzik  *	different reset methods before postreset is invoked.
4195c6fd2807SJeff Garzik  *
4196c6fd2807SJeff Garzik  *	LOCKING:
4197c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
4198c6fd2807SJeff Garzik  */
4199203c75b8STejun Heo void ata_std_postreset(struct ata_link *link, unsigned int *classes)
4200c6fd2807SJeff Garzik {
4201f046519fSTejun Heo 	u32 serror;
4202f046519fSTejun Heo 
4203c6fd2807SJeff Garzik 	DPRINTK("ENTER\n");
4204c6fd2807SJeff Garzik 
4205f046519fSTejun Heo 	/* reset complete, clear SError */
4206f046519fSTejun Heo 	if (!sata_scr_read(link, SCR_ERROR, &serror))
4207f046519fSTejun Heo 		sata_scr_write(link, SCR_ERROR, serror);
4208f046519fSTejun Heo 
4209c6fd2807SJeff Garzik 	/* print link status */
4210936fd732STejun Heo 	sata_print_link_status(link);
4211c6fd2807SJeff Garzik 
4212c6fd2807SJeff Garzik 	DPRINTK("EXIT\n");
4213c6fd2807SJeff Garzik }
4214c6fd2807SJeff Garzik 
4215c6fd2807SJeff Garzik /**
4216c6fd2807SJeff Garzik  *	ata_dev_same_device - Determine whether new ID matches configured device
4217c6fd2807SJeff Garzik  *	@dev: device to compare against
4218c6fd2807SJeff Garzik  *	@new_class: class of the new device
4219c6fd2807SJeff Garzik  *	@new_id: IDENTIFY page of the new device
4220c6fd2807SJeff Garzik  *
4221c6fd2807SJeff Garzik  *	Compare @new_class and @new_id against @dev and determine
4222c6fd2807SJeff Garzik  *	whether @dev is the device indicated by @new_class and
4223c6fd2807SJeff Garzik  *	@new_id.
4224c6fd2807SJeff Garzik  *
4225c6fd2807SJeff Garzik  *	LOCKING:
4226c6fd2807SJeff Garzik  *	None.
4227c6fd2807SJeff Garzik  *
4228c6fd2807SJeff Garzik  *	RETURNS:
4229c6fd2807SJeff Garzik  *	1 if @dev matches @new_class and @new_id, 0 otherwise.
4230c6fd2807SJeff Garzik  */
4231c6fd2807SJeff Garzik static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
4232c6fd2807SJeff Garzik 			       const u16 *new_id)
4233c6fd2807SJeff Garzik {
4234c6fd2807SJeff Garzik 	const u16 *old_id = dev->id;
4235a0cf733bSTejun Heo 	unsigned char model[2][ATA_ID_PROD_LEN + 1];
4236a0cf733bSTejun Heo 	unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
4237c6fd2807SJeff Garzik 
4238c6fd2807SJeff Garzik 	if (dev->class != new_class) {
4239a9a79dfeSJoe Perches 		ata_dev_info(dev, "class mismatch %d != %d\n",
4240c6fd2807SJeff Garzik 			     dev->class, new_class);
4241c6fd2807SJeff Garzik 		return 0;
4242c6fd2807SJeff Garzik 	}
4243c6fd2807SJeff Garzik 
4244a0cf733bSTejun Heo 	ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
4245a0cf733bSTejun Heo 	ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
4246a0cf733bSTejun Heo 	ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
4247a0cf733bSTejun Heo 	ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
4248c6fd2807SJeff Garzik 
4249c6fd2807SJeff Garzik 	if (strcmp(model[0], model[1])) {
4250a9a79dfeSJoe Perches 		ata_dev_info(dev, "model number mismatch '%s' != '%s'\n",
4251a9a79dfeSJoe Perches 			     model[0], model[1]);
4252c6fd2807SJeff Garzik 		return 0;
4253c6fd2807SJeff Garzik 	}
4254c6fd2807SJeff Garzik 
4255c6fd2807SJeff Garzik 	if (strcmp(serial[0], serial[1])) {
4256a9a79dfeSJoe Perches 		ata_dev_info(dev, "serial number mismatch '%s' != '%s'\n",
4257a9a79dfeSJoe Perches 			     serial[0], serial[1]);
4258c6fd2807SJeff Garzik 		return 0;
4259c6fd2807SJeff Garzik 	}
4260c6fd2807SJeff Garzik 
4261c6fd2807SJeff Garzik 	return 1;
4262c6fd2807SJeff Garzik }
4263c6fd2807SJeff Garzik 
4264c6fd2807SJeff Garzik /**
4265fe30911bSTejun Heo  *	ata_dev_reread_id - Re-read IDENTIFY data
42663fae450cSHenrik Kretzschmar  *	@dev: target ATA device
4267bff04647STejun Heo  *	@readid_flags: read ID flags
4268c6fd2807SJeff Garzik  *
4269c6fd2807SJeff Garzik  *	Re-read IDENTIFY page and make sure @dev is still attached to
4270c6fd2807SJeff Garzik  *	the port.
4271c6fd2807SJeff Garzik  *
4272c6fd2807SJeff Garzik  *	LOCKING:
4273c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
4274c6fd2807SJeff Garzik  *
4275c6fd2807SJeff Garzik  *	RETURNS:
4276c6fd2807SJeff Garzik  *	0 on success, negative errno otherwise
4277c6fd2807SJeff Garzik  */
4278fe30911bSTejun Heo int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
4279c6fd2807SJeff Garzik {
4280c6fd2807SJeff Garzik 	unsigned int class = dev->class;
42819af5c9c9STejun Heo 	u16 *id = (void *)dev->link->ap->sector_buf;
4282c6fd2807SJeff Garzik 	int rc;
4283c6fd2807SJeff Garzik 
4284c6fd2807SJeff Garzik 	/* read ID data */
4285bff04647STejun Heo 	rc = ata_dev_read_id(dev, &class, readid_flags, id);
4286c6fd2807SJeff Garzik 	if (rc)
4287fe30911bSTejun Heo 		return rc;
4288c6fd2807SJeff Garzik 
4289c6fd2807SJeff Garzik 	/* is the device still there? */
4290fe30911bSTejun Heo 	if (!ata_dev_same_device(dev, class, id))
4291fe30911bSTejun Heo 		return -ENODEV;
4292c6fd2807SJeff Garzik 
4293c6fd2807SJeff Garzik 	memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
4294fe30911bSTejun Heo 	return 0;
4295fe30911bSTejun Heo }
4296fe30911bSTejun Heo 
4297fe30911bSTejun Heo /**
4298fe30911bSTejun Heo  *	ata_dev_revalidate - Revalidate ATA device
4299fe30911bSTejun Heo  *	@dev: device to revalidate
4300422c9daaSTejun Heo  *	@new_class: new class code
4301fe30911bSTejun Heo  *	@readid_flags: read ID flags
4302fe30911bSTejun Heo  *
4303fe30911bSTejun Heo  *	Re-read IDENTIFY page, make sure @dev is still attached to the
4304fe30911bSTejun Heo  *	port and reconfigure it according to the new IDENTIFY page.
4305fe30911bSTejun Heo  *
4306fe30911bSTejun Heo  *	LOCKING:
4307fe30911bSTejun Heo  *	Kernel thread context (may sleep)
4308fe30911bSTejun Heo  *
4309fe30911bSTejun Heo  *	RETURNS:
4310fe30911bSTejun Heo  *	0 on success, negative errno otherwise
4311fe30911bSTejun Heo  */
4312422c9daaSTejun Heo int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
4313422c9daaSTejun Heo 		       unsigned int readid_flags)
4314fe30911bSTejun Heo {
43156ddcd3b0STejun Heo 	u64 n_sectors = dev->n_sectors;
43165920dadfSTejun Heo 	u64 n_native_sectors = dev->n_native_sectors;
4317fe30911bSTejun Heo 	int rc;
4318fe30911bSTejun Heo 
4319fe30911bSTejun Heo 	if (!ata_dev_enabled(dev))
4320fe30911bSTejun Heo 		return -ENODEV;
4321fe30911bSTejun Heo 
4322422c9daaSTejun Heo 	/* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
4323422c9daaSTejun Heo 	if (ata_class_enabled(new_class) &&
4324f0d0613dSBorislav Petkov 	    new_class != ATA_DEV_ATA &&
4325f0d0613dSBorislav Petkov 	    new_class != ATA_DEV_ATAPI &&
43269162c657SHannes Reinecke 	    new_class != ATA_DEV_ZAC &&
4327f0d0613dSBorislav Petkov 	    new_class != ATA_DEV_SEMB) {
4328a9a79dfeSJoe Perches 		ata_dev_info(dev, "class mismatch %u != %u\n",
4329422c9daaSTejun Heo 			     dev->class, new_class);
4330422c9daaSTejun Heo 		rc = -ENODEV;
4331422c9daaSTejun Heo 		goto fail;
4332422c9daaSTejun Heo 	}
4333422c9daaSTejun Heo 
4334fe30911bSTejun Heo 	/* re-read ID */
4335fe30911bSTejun Heo 	rc = ata_dev_reread_id(dev, readid_flags);
4336fe30911bSTejun Heo 	if (rc)
4337fe30911bSTejun Heo 		goto fail;
4338c6fd2807SJeff Garzik 
4339c6fd2807SJeff Garzik 	/* configure device according to the new ID */
4340efdaedc4STejun Heo 	rc = ata_dev_configure(dev);
43416ddcd3b0STejun Heo 	if (rc)
43426ddcd3b0STejun Heo 		goto fail;
43436ddcd3b0STejun Heo 
43446ddcd3b0STejun Heo 	/* verify n_sectors hasn't changed */
4345445d211bSTejun Heo 	if (dev->class != ATA_DEV_ATA || !n_sectors ||
4346445d211bSTejun Heo 	    dev->n_sectors == n_sectors)
4347445d211bSTejun Heo 		return 0;
4348445d211bSTejun Heo 
4349445d211bSTejun Heo 	/* n_sectors has changed */
4350a9a79dfeSJoe Perches 	ata_dev_warn(dev, "n_sectors mismatch %llu != %llu\n",
43516ddcd3b0STejun Heo 		     (unsigned long long)n_sectors,
43526ddcd3b0STejun Heo 		     (unsigned long long)dev->n_sectors);
4353445d211bSTejun Heo 
43545920dadfSTejun Heo 	/*
43555920dadfSTejun Heo 	 * Something could have caused HPA to be unlocked
4356445d211bSTejun Heo 	 * involuntarily.  If n_native_sectors hasn't changed and the
4357445d211bSTejun Heo 	 * new size matches it, keep the device.
43585920dadfSTejun Heo 	 */
43595920dadfSTejun Heo 	if (dev->n_native_sectors == n_native_sectors &&
4360445d211bSTejun Heo 	    dev->n_sectors > n_sectors && dev->n_sectors == n_native_sectors) {
4361a9a79dfeSJoe Perches 		ata_dev_warn(dev,
43625920dadfSTejun Heo 			     "new n_sectors matches native, probably "
436368939ce5STejun Heo 			     "late HPA unlock, n_sectors updated\n");
436468939ce5STejun Heo 		/* use the larger n_sectors */
4365445d211bSTejun Heo 		return 0;
4366445d211bSTejun Heo 	}
4367445d211bSTejun Heo 
4368445d211bSTejun Heo 	/*
4369445d211bSTejun Heo 	 * Some BIOSes boot w/o HPA but resume w/ HPA locked.  Try
4370445d211bSTejun Heo 	 * unlocking HPA in those cases.
4371445d211bSTejun Heo 	 *
4372445d211bSTejun Heo 	 * https://bugzilla.kernel.org/show_bug.cgi?id=15396
4373445d211bSTejun Heo 	 */
4374445d211bSTejun Heo 	if (dev->n_native_sectors == n_native_sectors &&
4375445d211bSTejun Heo 	    dev->n_sectors < n_sectors && n_sectors == n_native_sectors &&
4376445d211bSTejun Heo 	    !(dev->horkage & ATA_HORKAGE_BROKEN_HPA)) {
4377a9a79dfeSJoe Perches 		ata_dev_warn(dev,
4378445d211bSTejun Heo 			     "old n_sectors matches native, probably "
4379445d211bSTejun Heo 			     "late HPA lock, will try to unlock HPA\n");
4380445d211bSTejun Heo 		/* try unlocking HPA */
4381445d211bSTejun Heo 		dev->flags |= ATA_DFLAG_UNLOCK_HPA;
4382445d211bSTejun Heo 		rc = -EIO;
4383445d211bSTejun Heo 	} else
4384445d211bSTejun Heo 		rc = -ENODEV;
4385445d211bSTejun Heo 
4386445d211bSTejun Heo 	/* restore original n_[native_]sectors and fail */
43875920dadfSTejun Heo 	dev->n_native_sectors = n_native_sectors;
43885920dadfSTejun Heo 	dev->n_sectors = n_sectors;
4389c6fd2807SJeff Garzik  fail:
4390a9a79dfeSJoe Perches 	ata_dev_err(dev, "revalidation failed (errno=%d)\n", rc);
4391c6fd2807SJeff Garzik 	return rc;
4392c6fd2807SJeff Garzik }
4393c6fd2807SJeff Garzik 
43946919a0a6SAlan Cox struct ata_blacklist_entry {
43956919a0a6SAlan Cox 	const char *model_num;
43966919a0a6SAlan Cox 	const char *model_rev;
43976919a0a6SAlan Cox 	unsigned long horkage;
43986919a0a6SAlan Cox };
43996919a0a6SAlan Cox 
44006919a0a6SAlan Cox static const struct ata_blacklist_entry ata_device_blacklist [] = {
44016919a0a6SAlan Cox 	/* Devices with DMA related problems under Linux */
44026919a0a6SAlan Cox 	{ "WDC AC11000H",	NULL,		ATA_HORKAGE_NODMA },
44036919a0a6SAlan Cox 	{ "WDC AC22100H",	NULL,		ATA_HORKAGE_NODMA },
44046919a0a6SAlan Cox 	{ "WDC AC32500H",	NULL,		ATA_HORKAGE_NODMA },
44056919a0a6SAlan Cox 	{ "WDC AC33100H",	NULL,		ATA_HORKAGE_NODMA },
44066919a0a6SAlan Cox 	{ "WDC AC31600H",	NULL,		ATA_HORKAGE_NODMA },
44076919a0a6SAlan Cox 	{ "WDC AC32100H",	"24.09P07",	ATA_HORKAGE_NODMA },
44086919a0a6SAlan Cox 	{ "WDC AC23200L",	"21.10N21",	ATA_HORKAGE_NODMA },
44096919a0a6SAlan Cox 	{ "Compaq CRD-8241B", 	NULL,		ATA_HORKAGE_NODMA },
44106919a0a6SAlan Cox 	{ "CRD-8400B",		NULL, 		ATA_HORKAGE_NODMA },
44117da4c935SMark Lord 	{ "CRD-848[02]B",	NULL,		ATA_HORKAGE_NODMA },
44126919a0a6SAlan Cox 	{ "CRD-84",		NULL,		ATA_HORKAGE_NODMA },
44136919a0a6SAlan Cox 	{ "SanDisk SDP3B",	NULL,		ATA_HORKAGE_NODMA },
44146919a0a6SAlan Cox 	{ "SanDisk SDP3B-64",	NULL,		ATA_HORKAGE_NODMA },
44156919a0a6SAlan Cox 	{ "SANYO CD-ROM CRD",	NULL,		ATA_HORKAGE_NODMA },
44166919a0a6SAlan Cox 	{ "HITACHI CDR-8",	NULL,		ATA_HORKAGE_NODMA },
44177da4c935SMark Lord 	{ "HITACHI CDR-8[34]35",NULL,		ATA_HORKAGE_NODMA },
44186919a0a6SAlan Cox 	{ "Toshiba CD-ROM XM-6202B", NULL,	ATA_HORKAGE_NODMA },
44196919a0a6SAlan Cox 	{ "TOSHIBA CD-ROM XM-1702BC", NULL,	ATA_HORKAGE_NODMA },
44206919a0a6SAlan Cox 	{ "CD-532E-A", 		NULL,		ATA_HORKAGE_NODMA },
44216919a0a6SAlan Cox 	{ "E-IDE CD-ROM CR-840",NULL,		ATA_HORKAGE_NODMA },
44226919a0a6SAlan Cox 	{ "CD-ROM Drive/F5A",	NULL,		ATA_HORKAGE_NODMA },
44236919a0a6SAlan Cox 	{ "WPI CDD-820", 	NULL,		ATA_HORKAGE_NODMA },
44246919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SC-148C", NULL,	ATA_HORKAGE_NODMA },
44256919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SC",	NULL,		ATA_HORKAGE_NODMA },
44266919a0a6SAlan Cox 	{ "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
44276919a0a6SAlan Cox 	{ "_NEC DV5800A", 	NULL,		ATA_HORKAGE_NODMA },
44286919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SN-124", "N001",	ATA_HORKAGE_NODMA },
442939f19886SDave Jones 	{ "Seagate STT20000A", NULL,		ATA_HORKAGE_NODMA },
4430d70e551cSPrarit Bhargava 	{ " 2GB ATA Flash Disk", "ADMA428M",	ATA_HORKAGE_NODMA },
4431b00622fcSShikha Jain 	{ "VRFDFC22048UCHC-TE*", NULL,		ATA_HORKAGE_NODMA },
44323af9a77aSTejun Heo 	/* Odd clown on sil3726/4726 PMPs */
443350af2fa1STejun Heo 	{ "Config  Disk",	NULL,		ATA_HORKAGE_DISABLE },
44346919a0a6SAlan Cox 
443518d6e9d5SAlbert Lee 	/* Weird ATAPI devices */
443640a1d531STejun Heo 	{ "TORiSAN DVD-ROM DRD-N216", NULL,	ATA_HORKAGE_MAX_SEC_128 },
44376a87e42eSTejun Heo 	{ "QUANTUM DAT    DAT72-000", NULL,	ATA_HORKAGE_ATAPI_MOD16_DMA },
4438a32450e1SShan Hai 	{ "Slimtype DVD A  DS8A8SH", NULL,	ATA_HORKAGE_MAX_SEC_LBA48 },
44390523f037SShan Hai 	{ "Slimtype DVD A  DS8A9SH", NULL,	ATA_HORKAGE_MAX_SEC_LBA48 },
444018d6e9d5SAlbert Lee 
4441af34d637SDavid Milburn 	/*
4442af34d637SDavid Milburn 	 * Causes silent data corruption with higher max sects.
4443af34d637SDavid Milburn 	 * http://lkml.kernel.org/g/x49wpy40ysk.fsf@segfault.boston.devel.redhat.com
4444af34d637SDavid Milburn 	 */
4445af34d637SDavid Milburn 	{ "ST380013AS",		"3.20",		ATA_HORKAGE_MAX_SEC_1024 },
4446af34d637SDavid Milburn 
44471488a1e3STejun Heo 	/*
4448e0edc8c5STejun Heo 	 * These devices time out with higher max sects.
44491488a1e3STejun Heo 	 * https://bugzilla.kernel.org/show_bug.cgi?id=121671
44501488a1e3STejun Heo 	 */
4451e0edc8c5STejun Heo 	{ "LITEON CX1-JB*-HP",	NULL,		ATA_HORKAGE_MAX_SEC_1024 },
4452db5ff909SXinyu Lin 	{ "LITEON EP1-*",	NULL,		ATA_HORKAGE_MAX_SEC_1024 },
44531488a1e3STejun Heo 
44546919a0a6SAlan Cox 	/* Devices we expect to fail diagnostics */
44556919a0a6SAlan Cox 
44566919a0a6SAlan Cox 	/* Devices where NCQ should be avoided */
44576919a0a6SAlan Cox 	/* NCQ is slow */
44586919a0a6SAlan Cox 	{ "WDC WD740ADFD-00",	NULL,		ATA_HORKAGE_NONCQ },
4459459ad688STejun Heo 	{ "WDC WD740ADFD-00NLR1", NULL,		ATA_HORKAGE_NONCQ, },
446009125ea6STejun Heo 	/* http://thread.gmane.org/gmane.linux.ide/14907 */
446109125ea6STejun Heo 	{ "FUJITSU MHT2060BH",	NULL,		ATA_HORKAGE_NONCQ },
44627acfaf30SPaul Rolland 	/* NCQ is broken */
4463539cc7c7SJeff Garzik 	{ "Maxtor *",		"BANC*",	ATA_HORKAGE_NONCQ },
44640e3dbc01SAlan Cox 	{ "Maxtor 7V300F0",	"VA111630",	ATA_HORKAGE_NONCQ },
4465da6f0ec2SPaolo Ornati 	{ "ST380817AS",		"3.42",		ATA_HORKAGE_NONCQ },
4466e41bd3e8STejun Heo 	{ "ST3160023AS",	"3.42",		ATA_HORKAGE_NONCQ },
44675ccfca97SLubomir Bulej 	{ "OCZ CORE_SSD",	"02.10104",	ATA_HORKAGE_NONCQ },
4468539cc7c7SJeff Garzik 
4469ac70a964STejun Heo 	/* Seagate NCQ + FLUSH CACHE firmware bug */
44704d1f9082SMark Lord 	{ "ST31500341AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
4471ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4472d10d491fSTejun Heo 
44734d1f9082SMark Lord 	{ "ST31000333AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
4474d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4475d10d491fSTejun Heo 
44764d1f9082SMark Lord 	{ "ST3640[36]23AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
4477d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4478d10d491fSTejun Heo 
44794d1f9082SMark Lord 	{ "ST3320[68]13AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
4480ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4481ac70a964STejun Heo 
448208c85d2aSAleksei Mamlin 	/* drives which fail FPDMA_AA activation (some may freeze afterwards) */
448387809942SMichele Baldessari 	{ "ST1000LM024 HN-M101MBB", "2AR10001",	ATA_HORKAGE_BROKEN_FPDMA_AA },
4484b28a613eSMichele Baldessari 	{ "ST1000LM024 HN-M101MBB", "2BA30001",	ATA_HORKAGE_BROKEN_FPDMA_AA },
448508c85d2aSAleksei Mamlin 	{ "VB0250EAVER",	"HPG7",		ATA_HORKAGE_BROKEN_FPDMA_AA },
448687809942SMichele Baldessari 
448736e337d0SRobert Hancock 	/* Blacklist entries taken from Silicon Image 3124/3132
448836e337d0SRobert Hancock 	   Windows driver .inf file - also several Linux problem reports */
448936e337d0SRobert Hancock 	{ "HTS541060G9SA00",    "MB3OC60D",     ATA_HORKAGE_NONCQ, },
449036e337d0SRobert Hancock 	{ "HTS541080G9SA00",    "MB4OC60D",     ATA_HORKAGE_NONCQ, },
449136e337d0SRobert Hancock 	{ "HTS541010G9SA00",    "MBZOC60D",     ATA_HORKAGE_NONCQ, },
44926919a0a6SAlan Cox 
449368b0ddb2STejun Heo 	/* https://bugzilla.kernel.org/show_bug.cgi?id=15573 */
449468b0ddb2STejun Heo 	{ "C300-CTFDDAC128MAG",	"0001",		ATA_HORKAGE_NONCQ, },
449568b0ddb2STejun Heo 
4496322579dcSTejun Heo 	/* Some Sandisk SSDs lock up hard with NCQ enabled.  Reported on
4497322579dcSTejun Heo 	   SD7SN6S256G and SD8SN8U256G */
4498322579dcSTejun Heo 	{ "SanDisk SD[78]SN*G",	NULL,		ATA_HORKAGE_NONCQ, },
4499322579dcSTejun Heo 
450016c55b03STejun Heo 	/* devices which puke on READ_NATIVE_MAX */
450116c55b03STejun Heo 	{ "HDS724040KLSA80",	"KFAOA20N",	ATA_HORKAGE_BROKEN_HPA, },
450216c55b03STejun Heo 	{ "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
450316c55b03STejun Heo 	{ "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
450416c55b03STejun Heo 	{ "MAXTOR 6L080L4",	"A93.0500",	ATA_HORKAGE_BROKEN_HPA },
45056919a0a6SAlan Cox 
45067831387bSTejun Heo 	/* this one allows HPA unlocking but fails IOs on the area */
45077831387bSTejun Heo 	{ "OCZ-VERTEX",		    "1.30",	ATA_HORKAGE_BROKEN_HPA },
45087831387bSTejun Heo 
450993328e11SAlan Cox 	/* Devices which report 1 sector over size HPA */
451093328e11SAlan Cox 	{ "ST340823A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
451193328e11SAlan Cox 	{ "ST320413A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
4512b152fcd3SMikko Rapeli 	{ "ST310211A",		NULL,		ATA_HORKAGE_HPA_SIZE, },
451393328e11SAlan Cox 
45146bbfd53dSAlan Cox 	/* Devices which get the IVB wrong */
45156bbfd53dSAlan Cox 	{ "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB, },
4516a79067e5SAlan Cox 	/* Maybe we should just blacklist TSSTcorp... */
45177da4c935SMark Lord 	{ "TSSTcorp CDDVDW SH-S202[HJN]", "SB0[01]",  ATA_HORKAGE_IVB, },
45186bbfd53dSAlan Cox 
45199ce8e307SJens Axboe 	/* Devices that do not need bridging limits applied */
45209ce8e307SJens Axboe 	{ "MTRON MSP-SATA*",		NULL,	ATA_HORKAGE_BRIDGE_OK, },
452104d0f1b8SJeff Garzik 	{ "BUFFALO HD-QSU2/R5",		NULL,	ATA_HORKAGE_BRIDGE_OK, },
45229ce8e307SJens Axboe 
45239062712fSTejun Heo 	/* Devices which aren't very happy with higher link speeds */
45249062712fSTejun Heo 	{ "WD My Book",			NULL,	ATA_HORKAGE_1_5_GBPS, },
4525c531077fSDaniel J Blueman 	{ "Seagate FreeAgent GoFlex",	NULL,	ATA_HORKAGE_1_5_GBPS, },
45269062712fSTejun Heo 
4527d0cb43b3STejun Heo 	/*
4528d0cb43b3STejun Heo 	 * Devices which choke on SETXFER.  Applies only if both the
4529d0cb43b3STejun Heo 	 * device and controller are SATA.
4530d0cb43b3STejun Heo 	 */
4531cd691876STejun Heo 	{ "PIONEER DVD-RW  DVRTD08",	NULL,	ATA_HORKAGE_NOSETXFER },
45323a25179eSVladimir LAVALLADE 	{ "PIONEER DVD-RW  DVRTD08A",	NULL,	ATA_HORKAGE_NOSETXFER },
45333a25179eSVladimir LAVALLADE 	{ "PIONEER DVD-RW  DVR-215",	NULL,	ATA_HORKAGE_NOSETXFER },
4534cd691876STejun Heo 	{ "PIONEER DVD-RW  DVR-212D",	NULL,	ATA_HORKAGE_NOSETXFER },
4535cd691876STejun Heo 	{ "PIONEER DVD-RW  DVR-216D",	NULL,	ATA_HORKAGE_NOSETXFER },
4536d0cb43b3STejun Heo 
4537b17e5729SKai-Heng Feng 	/* Crucial BX100 SSD 500GB has broken LPM support */
45383bf7b5d6SHans de Goede 	{ "CT500BX100SSD1",		NULL,	ATA_HORKAGE_NOLPM },
4539b17e5729SKai-Heng Feng 
4540d418ff56SHans de Goede 	/* 512GB MX100 with MU01 firmware has both queued TRIM and LPM issues */
4541d418ff56SHans de Goede 	{ "Crucial_CT512MX100*",	"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
45429c7be59fSHans de Goede 						ATA_HORKAGE_ZERO_AFTER_TRIM |
45439c7be59fSHans de Goede 						ATA_HORKAGE_NOLPM, },
4544d418ff56SHans de Goede 	/* 512GB MX100 with newer firmware has only LPM issues */
4545d418ff56SHans de Goede 	{ "Crucial_CT512MX100*",	NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM |
4546d418ff56SHans de Goede 						ATA_HORKAGE_NOLPM, },
45479c7be59fSHans de Goede 
454862ac3f73SHans de Goede 	/* 480GB+ M500 SSDs have both queued TRIM and LPM issues */
454962ac3f73SHans de Goede 	{ "Crucial_CT480M500*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
455062ac3f73SHans de Goede 						ATA_HORKAGE_ZERO_AFTER_TRIM |
455162ac3f73SHans de Goede 						ATA_HORKAGE_NOLPM, },
455262ac3f73SHans de Goede 	{ "Crucial_CT960M500*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
455362ac3f73SHans de Goede 						ATA_HORKAGE_ZERO_AFTER_TRIM |
455462ac3f73SHans de Goede 						ATA_HORKAGE_NOLPM, },
455562ac3f73SHans de Goede 
4556b5b4d3a5SHans de Goede 	/* This specific Samsung model/firmware-rev does not handle LPM well */
4557b5b4d3a5SHans de Goede 	{ "SAMSUNG MZMPC128HBFU-000MV", "CXM14M1Q", ATA_HORKAGE_NOLPM, },
4558b5b4d3a5SHans de Goede 
4559184add2cSHans de Goede 	/* Sandisk devices which are known to not handle LPM well */
4560184add2cSHans de Goede 	{ "SanDisk SD7UB3Q*G1001",	NULL,	ATA_HORKAGE_NOLPM, },
4561184add2cSHans de Goede 
4562f78dea06SMarc Carino 	/* devices that don't properly handle queued TRIM commands */
4563243918beSMartin K. Petersen 	{ "Micron_M500_*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
4564e61f7d1cSMartin K. Petersen 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
4565ff7f53fbSMartin K. Petersen 	{ "Crucial_CT*M500*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
4566ff7f53fbSMartin K. Petersen 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
45679051bd39SMartin K. Petersen 	{ "Micron_M5[15]0_*",		"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
4568ff7f53fbSMartin K. Petersen 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
4569ff7f53fbSMartin K. Petersen 	{ "Crucial_CT*M550*",		"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
4570ff7f53fbSMartin K. Petersen 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
4571ff7f53fbSMartin K. Petersen 	{ "Crucial_CT*MX100*",		"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
4572ff7f53fbSMartin K. Petersen 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
4573ca6bfcb2SJu Hyung Park 	{ "Samsung SSD 840*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
4574ca6bfcb2SJu Hyung Park 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
4575ca6bfcb2SJu Hyung Park 	{ "Samsung SSD 850*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
45766fc4d97aSMartin K. Petersen 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
45777a7184b0SGuillermo A. Amaral 	{ "FCCT*M500*",			NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
45787a7184b0SGuillermo A. Amaral 						ATA_HORKAGE_ZERO_AFTER_TRIM, },
4579e61f7d1cSMartin K. Petersen 
4580cda57b1bSArne Fitzenreiter 	/* devices that don't properly handle TRIM commands */
4581cda57b1bSArne Fitzenreiter 	{ "SuperSSpeed S238*",		NULL,	ATA_HORKAGE_NOTRIM, },
4582cda57b1bSArne Fitzenreiter 
4583e61f7d1cSMartin K. Petersen 	/*
4584e61f7d1cSMartin K. Petersen 	 * As defined, the DRAT (Deterministic Read After Trim) and RZAT
4585e61f7d1cSMartin K. Petersen 	 * (Return Zero After Trim) flags in the ATA Command Set are
4586e61f7d1cSMartin K. Petersen 	 * unreliable in the sense that they only define what happens if
4587e61f7d1cSMartin K. Petersen 	 * the device successfully executed the DSM TRIM command. TRIM
4588e61f7d1cSMartin K. Petersen 	 * is only advisory, however, and the device is free to silently
4589e61f7d1cSMartin K. Petersen 	 * ignore all or parts of the request.
4590e61f7d1cSMartin K. Petersen 	 *
4591e61f7d1cSMartin K. Petersen 	 * Whitelist drives that are known to reliably return zeroes
4592e61f7d1cSMartin K. Petersen 	 * after TRIM.
4593e61f7d1cSMartin K. Petersen 	 */
4594e61f7d1cSMartin K. Petersen 
4595e61f7d1cSMartin K. Petersen 	/*
4596e61f7d1cSMartin K. Petersen 	 * The intel 510 drive has buggy DRAT/RZAT. Explicitly exclude
4597e61f7d1cSMartin K. Petersen 	 * that model before whitelisting all other intel SSDs.
4598e61f7d1cSMartin K. Petersen 	 */
4599e61f7d1cSMartin K. Petersen 	{ "INTEL*SSDSC2MH*",		NULL,	0, },
4600e61f7d1cSMartin K. Petersen 
4601ff7f53fbSMartin K. Petersen 	{ "Micron*",			NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM, },
4602ff7f53fbSMartin K. Petersen 	{ "Crucial*",			NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM, },
4603e61f7d1cSMartin K. Petersen 	{ "INTEL*SSD*", 		NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM, },
4604e61f7d1cSMartin K. Petersen 	{ "SSD*INTEL*",			NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM, },
4605e61f7d1cSMartin K. Petersen 	{ "Samsung*SSD*",		NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM, },
4606e61f7d1cSMartin K. Petersen 	{ "SAMSUNG*SSD*",		NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM, },
4607e61f7d1cSMartin K. Petersen 	{ "ST[1248][0248]0[FH]*",	NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM, },
4608f78dea06SMarc Carino 
4609ecd75ad5STejun Heo 	/*
4610ecd75ad5STejun Heo 	 * Some WD SATA-I drives spin up and down erratically when the link
4611ecd75ad5STejun Heo 	 * is put into the slumber mode.  We don't have full list of the
4612ecd75ad5STejun Heo 	 * affected devices.  Disable LPM if the device matches one of the
4613ecd75ad5STejun Heo 	 * known prefixes and is SATA-1.  As a side effect LPM partial is
4614ecd75ad5STejun Heo 	 * lost too.
4615ecd75ad5STejun Heo 	 *
4616ecd75ad5STejun Heo 	 * https://bugzilla.kernel.org/show_bug.cgi?id=57211
4617ecd75ad5STejun Heo 	 */
4618ecd75ad5STejun Heo 	{ "WDC WD800JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4619ecd75ad5STejun Heo 	{ "WDC WD1200JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4620ecd75ad5STejun Heo 	{ "WDC WD1600JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4621ecd75ad5STejun Heo 	{ "WDC WD2000JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4622ecd75ad5STejun Heo 	{ "WDC WD2500JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4623ecd75ad5STejun Heo 	{ "WDC WD3000JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4624ecd75ad5STejun Heo 	{ "WDC WD3200JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4625ecd75ad5STejun Heo 
46266919a0a6SAlan Cox 	/* End Marker */
46276919a0a6SAlan Cox 	{ }
4628c6fd2807SJeff Garzik };
4629c6fd2807SJeff Garzik 
463075683fe7STejun Heo static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
4631c6fd2807SJeff Garzik {
46328bfa79fcSTejun Heo 	unsigned char model_num[ATA_ID_PROD_LEN + 1];
46338bfa79fcSTejun Heo 	unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
46346919a0a6SAlan Cox 	const struct ata_blacklist_entry *ad = ata_device_blacklist;
4635c6fd2807SJeff Garzik 
46368bfa79fcSTejun Heo 	ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
46378bfa79fcSTejun Heo 	ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
4638c6fd2807SJeff Garzik 
46396919a0a6SAlan Cox 	while (ad->model_num) {
46401c402799SGeorge Spelvin 		if (glob_match(ad->model_num, model_num)) {
46416919a0a6SAlan Cox 			if (ad->model_rev == NULL)
46426919a0a6SAlan Cox 				return ad->horkage;
46431c402799SGeorge Spelvin 			if (glob_match(ad->model_rev, model_rev))
46446919a0a6SAlan Cox 				return ad->horkage;
4645c6fd2807SJeff Garzik 		}
46466919a0a6SAlan Cox 		ad++;
4647c6fd2807SJeff Garzik 	}
4648c6fd2807SJeff Garzik 	return 0;
4649c6fd2807SJeff Garzik }
4650c6fd2807SJeff Garzik 
46516919a0a6SAlan Cox static int ata_dma_blacklisted(const struct ata_device *dev)
46526919a0a6SAlan Cox {
46536919a0a6SAlan Cox 	/* We don't support polling DMA.
46546919a0a6SAlan Cox 	 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
46556919a0a6SAlan Cox 	 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
46566919a0a6SAlan Cox 	 */
46579af5c9c9STejun Heo 	if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
46586919a0a6SAlan Cox 	    (dev->flags & ATA_DFLAG_CDB_INTR))
46596919a0a6SAlan Cox 		return 1;
466075683fe7STejun Heo 	return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
46616919a0a6SAlan Cox }
46626919a0a6SAlan Cox 
4663c6fd2807SJeff Garzik /**
46646bbfd53dSAlan Cox  *	ata_is_40wire		-	check drive side detection
46656bbfd53dSAlan Cox  *	@dev: device
46666bbfd53dSAlan Cox  *
46676bbfd53dSAlan Cox  *	Perform drive side detection decoding, allowing for device vendors
46686bbfd53dSAlan Cox  *	who can't follow the documentation.
46696bbfd53dSAlan Cox  */
46706bbfd53dSAlan Cox 
46716bbfd53dSAlan Cox static int ata_is_40wire(struct ata_device *dev)
46726bbfd53dSAlan Cox {
46736bbfd53dSAlan Cox 	if (dev->horkage & ATA_HORKAGE_IVB)
46746bbfd53dSAlan Cox 		return ata_drive_40wire_relaxed(dev->id);
46756bbfd53dSAlan Cox 	return ata_drive_40wire(dev->id);
46766bbfd53dSAlan Cox }
46776bbfd53dSAlan Cox 
46786bbfd53dSAlan Cox /**
467915a5551cSAlan Cox  *	cable_is_40wire		-	40/80/SATA decider
468015a5551cSAlan Cox  *	@ap: port to consider
468115a5551cSAlan Cox  *
468215a5551cSAlan Cox  *	This function encapsulates the policy for speed management
468315a5551cSAlan Cox  *	in one place. At the moment we don't cache the result but
468415a5551cSAlan Cox  *	there is a good case for setting ap->cbl to the result when
468515a5551cSAlan Cox  *	we are called with unknown cables (and figuring out if it
468615a5551cSAlan Cox  *	impacts hotplug at all).
468715a5551cSAlan Cox  *
468815a5551cSAlan Cox  *	Return 1 if the cable appears to be 40 wire.
468915a5551cSAlan Cox  */
469015a5551cSAlan Cox 
469115a5551cSAlan Cox static int cable_is_40wire(struct ata_port *ap)
469215a5551cSAlan Cox {
469315a5551cSAlan Cox 	struct ata_link *link;
469415a5551cSAlan Cox 	struct ata_device *dev;
469515a5551cSAlan Cox 
46964a9c7b33STejun Heo 	/* If the controller thinks we are 40 wire, we are. */
469715a5551cSAlan Cox 	if (ap->cbl == ATA_CBL_PATA40)
469815a5551cSAlan Cox 		return 1;
46994a9c7b33STejun Heo 
47004a9c7b33STejun Heo 	/* If the controller thinks we are 80 wire, we are. */
470115a5551cSAlan Cox 	if (ap->cbl == ATA_CBL_PATA80 || ap->cbl == ATA_CBL_SATA)
470215a5551cSAlan Cox 		return 0;
47034a9c7b33STejun Heo 
47044a9c7b33STejun Heo 	/* If the system is known to be 40 wire short cable (eg
47054a9c7b33STejun Heo 	 * laptop), then we allow 80 wire modes even if the drive
47064a9c7b33STejun Heo 	 * isn't sure.
47074a9c7b33STejun Heo 	 */
4708f792068eSAlan Cox 	if (ap->cbl == ATA_CBL_PATA40_SHORT)
4709f792068eSAlan Cox 		return 0;
471015a5551cSAlan Cox 
47114a9c7b33STejun Heo 	/* If the controller doesn't know, we scan.
47124a9c7b33STejun Heo 	 *
47134a9c7b33STejun Heo 	 * Note: We look for all 40 wire detects at this point.  Any
47144a9c7b33STejun Heo 	 *       80 wire detect is taken to be 80 wire cable because
47154a9c7b33STejun Heo 	 * - in many setups only the one drive (slave if present) will
47164a9c7b33STejun Heo 	 *   give a valid detect
47174a9c7b33STejun Heo 	 * - if you have a non detect capable drive you don't want it
47184a9c7b33STejun Heo 	 *   to colour the choice
471915a5551cSAlan Cox 	 */
47201eca4365STejun Heo 	ata_for_each_link(link, ap, EDGE) {
47211eca4365STejun Heo 		ata_for_each_dev(dev, link, ENABLED) {
47221eca4365STejun Heo 			if (!ata_is_40wire(dev))
472315a5551cSAlan Cox 				return 0;
472415a5551cSAlan Cox 		}
472515a5551cSAlan Cox 	}
472615a5551cSAlan Cox 	return 1;
472715a5551cSAlan Cox }
472815a5551cSAlan Cox 
472915a5551cSAlan Cox /**
4730c6fd2807SJeff Garzik  *	ata_dev_xfermask - Compute supported xfermask of the given device
4731c6fd2807SJeff Garzik  *	@dev: Device to compute xfermask for
4732c6fd2807SJeff Garzik  *
4733c6fd2807SJeff Garzik  *	Compute supported xfermask of @dev and store it in
4734c6fd2807SJeff Garzik  *	dev->*_mask.  This function is responsible for applying all
4735c6fd2807SJeff Garzik  *	known limits including host controller limits, device
4736c6fd2807SJeff Garzik  *	blacklist, etc...
4737c6fd2807SJeff Garzik  *
4738c6fd2807SJeff Garzik  *	LOCKING:
4739c6fd2807SJeff Garzik  *	None.
4740c6fd2807SJeff Garzik  */
4741c6fd2807SJeff Garzik static void ata_dev_xfermask(struct ata_device *dev)
4742c6fd2807SJeff Garzik {
47439af5c9c9STejun Heo 	struct ata_link *link = dev->link;
47449af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
4745cca3974eSJeff Garzik 	struct ata_host *host = ap->host;
4746c6fd2807SJeff Garzik 	unsigned long xfer_mask;
4747c6fd2807SJeff Garzik 
4748c6fd2807SJeff Garzik 	/* controller modes available */
4749c6fd2807SJeff Garzik 	xfer_mask = ata_pack_xfermask(ap->pio_mask,
4750c6fd2807SJeff Garzik 				      ap->mwdma_mask, ap->udma_mask);
4751c6fd2807SJeff Garzik 
47528343f889SRobert Hancock 	/* drive modes available */
4753c6fd2807SJeff Garzik 	xfer_mask &= ata_pack_xfermask(dev->pio_mask,
4754c6fd2807SJeff Garzik 				       dev->mwdma_mask, dev->udma_mask);
4755c6fd2807SJeff Garzik 	xfer_mask &= ata_id_xfermask(dev->id);
4756c6fd2807SJeff Garzik 
4757b352e57dSAlan Cox 	/*
4758b352e57dSAlan Cox 	 *	CFA Advanced TrueIDE timings are not allowed on a shared
4759b352e57dSAlan Cox 	 *	cable
4760b352e57dSAlan Cox 	 */
4761b352e57dSAlan Cox 	if (ata_dev_pair(dev)) {
4762b352e57dSAlan Cox 		/* No PIO5 or PIO6 */
4763b352e57dSAlan Cox 		xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
4764b352e57dSAlan Cox 		/* No MWDMA3 or MWDMA 4 */
4765b352e57dSAlan Cox 		xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
4766b352e57dSAlan Cox 	}
4767b352e57dSAlan Cox 
4768c6fd2807SJeff Garzik 	if (ata_dma_blacklisted(dev)) {
4769c6fd2807SJeff Garzik 		xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4770a9a79dfeSJoe Perches 		ata_dev_warn(dev,
4771c6fd2807SJeff Garzik 			     "device is on DMA blacklist, disabling DMA\n");
4772c6fd2807SJeff Garzik 	}
4773c6fd2807SJeff Garzik 
477414d66ab7SPetr Vandrovec 	if ((host->flags & ATA_HOST_SIMPLEX) &&
477514d66ab7SPetr Vandrovec 	    host->simplex_claimed && host->simplex_claimed != ap) {
4776c6fd2807SJeff Garzik 		xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4777a9a79dfeSJoe Perches 		ata_dev_warn(dev,
4778a9a79dfeSJoe Perches 			     "simplex DMA is claimed by other device, disabling DMA\n");
4779c6fd2807SJeff Garzik 	}
4780c6fd2807SJeff Garzik 
4781e424675fSJeff Garzik 	if (ap->flags & ATA_FLAG_NO_IORDY)
4782e424675fSJeff Garzik 		xfer_mask &= ata_pio_mask_no_iordy(dev);
4783e424675fSJeff Garzik 
4784c6fd2807SJeff Garzik 	if (ap->ops->mode_filter)
4785a76b62caSAlan Cox 		xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
4786c6fd2807SJeff Garzik 
47878343f889SRobert Hancock 	/* Apply cable rule here.  Don't apply it early because when
47888343f889SRobert Hancock 	 * we handle hot plug the cable type can itself change.
47898343f889SRobert Hancock 	 * Check this last so that we know if the transfer rate was
47908343f889SRobert Hancock 	 * solely limited by the cable.
47918343f889SRobert Hancock 	 * Unknown or 80 wire cables reported host side are checked
47928343f889SRobert Hancock 	 * drive side as well. Cases where we know a 40wire cable
47938343f889SRobert Hancock 	 * is used safely for 80 are not checked here.
47948343f889SRobert Hancock 	 */
47958343f889SRobert Hancock 	if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
47968343f889SRobert Hancock 		/* UDMA/44 or higher would be available */
479715a5551cSAlan Cox 		if (cable_is_40wire(ap)) {
4798a9a79dfeSJoe Perches 			ata_dev_warn(dev,
47998343f889SRobert Hancock 				     "limited to UDMA/33 due to 40-wire cable\n");
48008343f889SRobert Hancock 			xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
48018343f889SRobert Hancock 		}
48028343f889SRobert Hancock 
4803c6fd2807SJeff Garzik 	ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
4804c6fd2807SJeff Garzik 			    &dev->mwdma_mask, &dev->udma_mask);
4805c6fd2807SJeff Garzik }
4806c6fd2807SJeff Garzik 
4807c6fd2807SJeff Garzik /**
4808c6fd2807SJeff Garzik  *	ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
4809c6fd2807SJeff Garzik  *	@dev: Device to which command will be sent
4810c6fd2807SJeff Garzik  *
4811c6fd2807SJeff Garzik  *	Issue SET FEATURES - XFER MODE command to device @dev
4812c6fd2807SJeff Garzik  *	on port @ap.
4813c6fd2807SJeff Garzik  *
4814c6fd2807SJeff Garzik  *	LOCKING:
4815c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
4816c6fd2807SJeff Garzik  *
4817c6fd2807SJeff Garzik  *	RETURNS:
4818c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
4819c6fd2807SJeff Garzik  */
4820c6fd2807SJeff Garzik 
4821c6fd2807SJeff Garzik static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
4822c6fd2807SJeff Garzik {
4823c6fd2807SJeff Garzik 	struct ata_taskfile tf;
4824c6fd2807SJeff Garzik 	unsigned int err_mask;
4825c6fd2807SJeff Garzik 
4826c6fd2807SJeff Garzik 	/* set up set-features taskfile */
4827c6fd2807SJeff Garzik 	DPRINTK("set features - xfer mode\n");
4828c6fd2807SJeff Garzik 
4829464cf177STejun Heo 	/* Some controllers and ATAPI devices show flaky interrupt
4830464cf177STejun Heo 	 * behavior after setting xfer mode.  Use polling instead.
4831464cf177STejun Heo 	 */
4832c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
4833c6fd2807SJeff Garzik 	tf.command = ATA_CMD_SET_FEATURES;
4834c6fd2807SJeff Garzik 	tf.feature = SETFEATURES_XFER;
4835464cf177STejun Heo 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
4836c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
4837b9f8ab2dSAlan Cox 	/* If we are using IORDY we must send the mode setting command */
4838b9f8ab2dSAlan Cox 	if (ata_pio_need_iordy(dev))
4839c6fd2807SJeff Garzik 		tf.nsect = dev->xfer_mode;
4840b9f8ab2dSAlan Cox 	/* If the device has IORDY and the controller does not - turn it off */
4841b9f8ab2dSAlan Cox  	else if (ata_id_has_iordy(dev->id))
4842b9f8ab2dSAlan Cox 		tf.nsect = 0x01;
4843b9f8ab2dSAlan Cox 	else /* In the ancient relic department - skip all of this */
4844b9f8ab2dSAlan Cox 		return 0;
4845c6fd2807SJeff Garzik 
4846d531be2cSMikulas Patocka 	/* On some disks, this command causes spin-up, so we need longer timeout */
4847d531be2cSMikulas Patocka 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 15000);
4848c6fd2807SJeff Garzik 
4849c6fd2807SJeff Garzik 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
4850c6fd2807SJeff Garzik 	return err_mask;
4851c6fd2807SJeff Garzik }
48521152b261STejun Heo 
4853c6fd2807SJeff Garzik /**
4854218f3d30SJeff Garzik  *	ata_dev_set_feature - Issue SET FEATURES - SATA FEATURES
48559f45cbd3SKristen Carlson Accardi  *	@dev: Device to which command will be sent
48569f45cbd3SKristen Carlson Accardi  *	@enable: Whether to enable or disable the feature
4857218f3d30SJeff Garzik  *	@feature: The sector count represents the feature to set
48589f45cbd3SKristen Carlson Accardi  *
48599f45cbd3SKristen Carlson Accardi  *	Issue SET FEATURES - SATA FEATURES command to device @dev
4860218f3d30SJeff Garzik  *	on port @ap with sector count
48619f45cbd3SKristen Carlson Accardi  *
48629f45cbd3SKristen Carlson Accardi  *	LOCKING:
48639f45cbd3SKristen Carlson Accardi  *	PCI/etc. bus probe sem.
48649f45cbd3SKristen Carlson Accardi  *
48659f45cbd3SKristen Carlson Accardi  *	RETURNS:
48669f45cbd3SKristen Carlson Accardi  *	0 on success, AC_ERR_* mask otherwise.
48679f45cbd3SKristen Carlson Accardi  */
48681152b261STejun Heo unsigned int ata_dev_set_feature(struct ata_device *dev, u8 enable, u8 feature)
48699f45cbd3SKristen Carlson Accardi {
48709f45cbd3SKristen Carlson Accardi 	struct ata_taskfile tf;
48719f45cbd3SKristen Carlson Accardi 	unsigned int err_mask;
4872974e0a45SDamien Le Moal 	unsigned long timeout = 0;
48739f45cbd3SKristen Carlson Accardi 
48749f45cbd3SKristen Carlson Accardi 	/* set up set-features taskfile */
48759f45cbd3SKristen Carlson Accardi 	DPRINTK("set features - SATA features\n");
48769f45cbd3SKristen Carlson Accardi 
48779f45cbd3SKristen Carlson Accardi 	ata_tf_init(dev, &tf);
48789f45cbd3SKristen Carlson Accardi 	tf.command = ATA_CMD_SET_FEATURES;
48799f45cbd3SKristen Carlson Accardi 	tf.feature = enable;
48809f45cbd3SKristen Carlson Accardi 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
48819f45cbd3SKristen Carlson Accardi 	tf.protocol = ATA_PROT_NODATA;
4882218f3d30SJeff Garzik 	tf.nsect = feature;
48839f45cbd3SKristen Carlson Accardi 
4884974e0a45SDamien Le Moal 	if (enable == SETFEATURES_SPINUP)
4885974e0a45SDamien Le Moal 		timeout = ata_probe_timeout ?
4886974e0a45SDamien Le Moal 			  ata_probe_timeout * 1000 : SETFEATURES_SPINUP_TIMEOUT;
4887974e0a45SDamien Le Moal 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, timeout);
48889f45cbd3SKristen Carlson Accardi 
48899f45cbd3SKristen Carlson Accardi 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
48909f45cbd3SKristen Carlson Accardi 	return err_mask;
48919f45cbd3SKristen Carlson Accardi }
4892633de4ccSJeff Garzik EXPORT_SYMBOL_GPL(ata_dev_set_feature);
48939f45cbd3SKristen Carlson Accardi 
48949f45cbd3SKristen Carlson Accardi /**
4895c6fd2807SJeff Garzik  *	ata_dev_init_params - Issue INIT DEV PARAMS command
4896c6fd2807SJeff Garzik  *	@dev: Device to which command will be sent
4897c6fd2807SJeff Garzik  *	@heads: Number of heads (taskfile parameter)
4898c6fd2807SJeff Garzik  *	@sectors: Number of sectors (taskfile parameter)
4899c6fd2807SJeff Garzik  *
4900c6fd2807SJeff Garzik  *	LOCKING:
4901c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
4902c6fd2807SJeff Garzik  *
4903c6fd2807SJeff Garzik  *	RETURNS:
4904c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
4905c6fd2807SJeff Garzik  */
4906c6fd2807SJeff Garzik static unsigned int ata_dev_init_params(struct ata_device *dev,
4907c6fd2807SJeff Garzik 					u16 heads, u16 sectors)
4908c6fd2807SJeff Garzik {
4909c6fd2807SJeff Garzik 	struct ata_taskfile tf;
4910c6fd2807SJeff Garzik 	unsigned int err_mask;
4911c6fd2807SJeff Garzik 
4912c6fd2807SJeff Garzik 	/* Number of sectors per track 1-255. Number of heads 1-16 */
4913c6fd2807SJeff Garzik 	if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
4914c6fd2807SJeff Garzik 		return AC_ERR_INVALID;
4915c6fd2807SJeff Garzik 
4916c6fd2807SJeff Garzik 	/* set up init dev params taskfile */
4917c6fd2807SJeff Garzik 	DPRINTK("init dev params \n");
4918c6fd2807SJeff Garzik 
4919c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
4920c6fd2807SJeff Garzik 	tf.command = ATA_CMD_INIT_DEV_PARAMS;
4921c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4922c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
4923c6fd2807SJeff Garzik 	tf.nsect = sectors;
4924c6fd2807SJeff Garzik 	tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
4925c6fd2807SJeff Garzik 
49262b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
492718b2466cSAlan Cox 	/* A clean abort indicates an original or just out of spec drive
492818b2466cSAlan Cox 	   and we should continue as we issue the setup based on the
492918b2466cSAlan Cox 	   drive reported working geometry */
493018b2466cSAlan Cox 	if (err_mask == AC_ERR_DEV && (tf.feature & ATA_ABORTED))
493118b2466cSAlan Cox 		err_mask = 0;
4932c6fd2807SJeff Garzik 
4933c6fd2807SJeff Garzik 	DPRINTK("EXIT, err_mask=%x\n", err_mask);
4934c6fd2807SJeff Garzik 	return err_mask;
4935c6fd2807SJeff Garzik }
4936c6fd2807SJeff Garzik 
4937c6fd2807SJeff Garzik /**
49385895ef9aSTejun Heo  *	atapi_check_dma - Check whether ATAPI DMA can be supported
4939c6fd2807SJeff Garzik  *	@qc: Metadata associated with taskfile to check
4940c6fd2807SJeff Garzik  *
4941c6fd2807SJeff Garzik  *	Allow low-level driver to filter ATA PACKET commands, returning
4942c6fd2807SJeff Garzik  *	a status indicating whether or not it is OK to use DMA for the
4943c6fd2807SJeff Garzik  *	supplied PACKET command.
4944c6fd2807SJeff Garzik  *
4945c6fd2807SJeff Garzik  *	LOCKING:
4946cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4947c6fd2807SJeff Garzik  *
4948c6fd2807SJeff Garzik  *	RETURNS: 0 when ATAPI DMA can be used
4949c6fd2807SJeff Garzik  *               nonzero otherwise
4950c6fd2807SJeff Garzik  */
49515895ef9aSTejun Heo int atapi_check_dma(struct ata_queued_cmd *qc)
4952c6fd2807SJeff Garzik {
4953c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4954c6fd2807SJeff Garzik 
4955b9a4197eSTejun Heo 	/* Don't allow DMA if it isn't multiple of 16 bytes.  Quite a
4956b9a4197eSTejun Heo 	 * few ATAPI devices choke on such DMA requests.
4957b9a4197eSTejun Heo 	 */
49586a87e42eSTejun Heo 	if (!(qc->dev->horkage & ATA_HORKAGE_ATAPI_MOD16_DMA) &&
49596a87e42eSTejun Heo 	    unlikely(qc->nbytes & 15))
49606f23a31dSAlbert Lee 		return 1;
49616f23a31dSAlbert Lee 
4962c6fd2807SJeff Garzik 	if (ap->ops->check_atapi_dma)
4963b9a4197eSTejun Heo 		return ap->ops->check_atapi_dma(qc);
4964c6fd2807SJeff Garzik 
4965b9a4197eSTejun Heo 	return 0;
4966c6fd2807SJeff Garzik }
4967b9a4197eSTejun Heo 
4968c6fd2807SJeff Garzik /**
496931cc23b3STejun Heo  *	ata_std_qc_defer - Check whether a qc needs to be deferred
497031cc23b3STejun Heo  *	@qc: ATA command in question
497131cc23b3STejun Heo  *
497231cc23b3STejun Heo  *	Non-NCQ commands cannot run with any other command, NCQ or
497331cc23b3STejun Heo  *	not.  As upper layer only knows the queue depth, we are
497431cc23b3STejun Heo  *	responsible for maintaining exclusion.  This function checks
497531cc23b3STejun Heo  *	whether a new command @qc can be issued.
497631cc23b3STejun Heo  *
497731cc23b3STejun Heo  *	LOCKING:
497831cc23b3STejun Heo  *	spin_lock_irqsave(host lock)
497931cc23b3STejun Heo  *
498031cc23b3STejun Heo  *	RETURNS:
498131cc23b3STejun Heo  *	ATA_DEFER_* if deferring is needed, 0 otherwise.
498231cc23b3STejun Heo  */
498331cc23b3STejun Heo int ata_std_qc_defer(struct ata_queued_cmd *qc)
498431cc23b3STejun Heo {
498531cc23b3STejun Heo 	struct ata_link *link = qc->dev->link;
498631cc23b3STejun Heo 
4987179b310aSHannes Reinecke 	if (ata_is_ncq(qc->tf.protocol)) {
498831cc23b3STejun Heo 		if (!ata_tag_valid(link->active_tag))
498931cc23b3STejun Heo 			return 0;
499031cc23b3STejun Heo 	} else {
499131cc23b3STejun Heo 		if (!ata_tag_valid(link->active_tag) && !link->sactive)
499231cc23b3STejun Heo 			return 0;
499331cc23b3STejun Heo 	}
499431cc23b3STejun Heo 
499531cc23b3STejun Heo 	return ATA_DEFER_LINK;
499631cc23b3STejun Heo }
499731cc23b3STejun Heo 
4998c6fd2807SJeff Garzik void ata_noop_qc_prep(struct ata_queued_cmd *qc) { }
4999c6fd2807SJeff Garzik 
5000c6fd2807SJeff Garzik /**
5001c6fd2807SJeff Garzik  *	ata_sg_init - Associate command with scatter-gather table.
5002c6fd2807SJeff Garzik  *	@qc: Command to be associated
5003c6fd2807SJeff Garzik  *	@sg: Scatter-gather table.
5004c6fd2807SJeff Garzik  *	@n_elem: Number of elements in s/g table.
5005c6fd2807SJeff Garzik  *
5006c6fd2807SJeff Garzik  *	Initialize the data-related elements of queued_cmd @qc
5007c6fd2807SJeff Garzik  *	to point to a scatter-gather table @sg, containing @n_elem
5008c6fd2807SJeff Garzik  *	elements.
5009c6fd2807SJeff Garzik  *
5010c6fd2807SJeff Garzik  *	LOCKING:
5011cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
5012c6fd2807SJeff Garzik  */
5013c6fd2807SJeff Garzik void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
5014c6fd2807SJeff Garzik 		 unsigned int n_elem)
5015c6fd2807SJeff Garzik {
5016ff2aeb1eSTejun Heo 	qc->sg = sg;
5017c6fd2807SJeff Garzik 	qc->n_elem = n_elem;
5018ff2aeb1eSTejun Heo 	qc->cursg = qc->sg;
5019ff2aeb1eSTejun Heo }
5020ff2aeb1eSTejun Heo 
50212874d5eeSGeert Uytterhoeven #ifdef CONFIG_HAS_DMA
50222874d5eeSGeert Uytterhoeven 
50232874d5eeSGeert Uytterhoeven /**
50242874d5eeSGeert Uytterhoeven  *	ata_sg_clean - Unmap DMA memory associated with command
50252874d5eeSGeert Uytterhoeven  *	@qc: Command containing DMA memory to be released
50262874d5eeSGeert Uytterhoeven  *
50272874d5eeSGeert Uytterhoeven  *	Unmap all mapped DMA memory associated with this command.
50282874d5eeSGeert Uytterhoeven  *
50292874d5eeSGeert Uytterhoeven  *	LOCKING:
50302874d5eeSGeert Uytterhoeven  *	spin_lock_irqsave(host lock)
50312874d5eeSGeert Uytterhoeven  */
5032af27e01cSJason Yan static void ata_sg_clean(struct ata_queued_cmd *qc)
50332874d5eeSGeert Uytterhoeven {
50342874d5eeSGeert Uytterhoeven 	struct ata_port *ap = qc->ap;
50352874d5eeSGeert Uytterhoeven 	struct scatterlist *sg = qc->sg;
50362874d5eeSGeert Uytterhoeven 	int dir = qc->dma_dir;
50372874d5eeSGeert Uytterhoeven 
50382874d5eeSGeert Uytterhoeven 	WARN_ON_ONCE(sg == NULL);
50392874d5eeSGeert Uytterhoeven 
50402874d5eeSGeert Uytterhoeven 	VPRINTK("unmapping %u sg elements\n", qc->n_elem);
50412874d5eeSGeert Uytterhoeven 
50422874d5eeSGeert Uytterhoeven 	if (qc->n_elem)
50432874d5eeSGeert Uytterhoeven 		dma_unmap_sg(ap->dev, sg, qc->orig_n_elem, dir);
50442874d5eeSGeert Uytterhoeven 
50452874d5eeSGeert Uytterhoeven 	qc->flags &= ~ATA_QCFLAG_DMAMAP;
50462874d5eeSGeert Uytterhoeven 	qc->sg = NULL;
50472874d5eeSGeert Uytterhoeven }
50482874d5eeSGeert Uytterhoeven 
5049c6fd2807SJeff Garzik /**
5050c6fd2807SJeff Garzik  *	ata_sg_setup - DMA-map the scatter-gather table associated with a command.
5051c6fd2807SJeff Garzik  *	@qc: Command with scatter-gather table to be mapped.
5052c6fd2807SJeff Garzik  *
5053c6fd2807SJeff Garzik  *	DMA-map the scatter-gather table associated with queued_cmd @qc.
5054c6fd2807SJeff Garzik  *
5055c6fd2807SJeff Garzik  *	LOCKING:
5056cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
5057c6fd2807SJeff Garzik  *
5058c6fd2807SJeff Garzik  *	RETURNS:
5059c6fd2807SJeff Garzik  *	Zero on success, negative on error.
5060c6fd2807SJeff Garzik  *
5061c6fd2807SJeff Garzik  */
5062c6fd2807SJeff Garzik static int ata_sg_setup(struct ata_queued_cmd *qc)
5063c6fd2807SJeff Garzik {
5064c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
5065dde20207SJames Bottomley 	unsigned int n_elem;
5066c6fd2807SJeff Garzik 
506744877b4eSTejun Heo 	VPRINTK("ENTER, ata%u\n", ap->print_id);
5068c6fd2807SJeff Garzik 
5069dde20207SJames Bottomley 	n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir);
5070dde20207SJames Bottomley 	if (n_elem < 1)
5071c6fd2807SJeff Garzik 		return -1;
5072c6fd2807SJeff Garzik 
5073dde20207SJames Bottomley 	DPRINTK("%d sg elements mapped\n", n_elem);
50745825627cSFUJITA Tomonori 	qc->orig_n_elem = qc->n_elem;
5075dde20207SJames Bottomley 	qc->n_elem = n_elem;
5076f92a2636STejun Heo 	qc->flags |= ATA_QCFLAG_DMAMAP;
5077c6fd2807SJeff Garzik 
5078c6fd2807SJeff Garzik 	return 0;
5079c6fd2807SJeff Garzik }
5080c6fd2807SJeff Garzik 
50812874d5eeSGeert Uytterhoeven #else /* !CONFIG_HAS_DMA */
50822874d5eeSGeert Uytterhoeven 
50832874d5eeSGeert Uytterhoeven static inline void ata_sg_clean(struct ata_queued_cmd *qc) {}
50842874d5eeSGeert Uytterhoeven static inline int ata_sg_setup(struct ata_queued_cmd *qc) { return -1; }
50852874d5eeSGeert Uytterhoeven 
50862874d5eeSGeert Uytterhoeven #endif /* !CONFIG_HAS_DMA */
50872874d5eeSGeert Uytterhoeven 
5088c6fd2807SJeff Garzik /**
5089c6fd2807SJeff Garzik  *	swap_buf_le16 - swap halves of 16-bit words in place
5090c6fd2807SJeff Garzik  *	@buf:  Buffer to swap
5091c6fd2807SJeff Garzik  *	@buf_words:  Number of 16-bit words in buffer.
5092c6fd2807SJeff Garzik  *
5093c6fd2807SJeff Garzik  *	Swap halves of 16-bit words if needed to convert from
5094c6fd2807SJeff Garzik  *	little-endian byte order to native cpu byte order, or
5095c6fd2807SJeff Garzik  *	vice-versa.
5096c6fd2807SJeff Garzik  *
5097c6fd2807SJeff Garzik  *	LOCKING:
5098c6fd2807SJeff Garzik  *	Inherited from caller.
5099c6fd2807SJeff Garzik  */
5100c6fd2807SJeff Garzik void swap_buf_le16(u16 *buf, unsigned int buf_words)
5101c6fd2807SJeff Garzik {
5102c6fd2807SJeff Garzik #ifdef __BIG_ENDIAN
5103c6fd2807SJeff Garzik 	unsigned int i;
5104c6fd2807SJeff Garzik 
5105c6fd2807SJeff Garzik 	for (i = 0; i < buf_words; i++)
5106c6fd2807SJeff Garzik 		buf[i] = le16_to_cpu(buf[i]);
5107c6fd2807SJeff Garzik #endif /* __BIG_ENDIAN */
5108c6fd2807SJeff Garzik }
5109c6fd2807SJeff Garzik 
5110c6fd2807SJeff Garzik /**
5111c6fd2807SJeff Garzik  *	ata_qc_new_init - Request an available ATA command, and initialize it
5112c6fd2807SJeff Garzik  *	@dev: Device from whom we request an available command structure
511338755e89SRandy Dunlap  *	@tag: tag
5114c6fd2807SJeff Garzik  *
5115c6fd2807SJeff Garzik  *	LOCKING:
5116c6fd2807SJeff Garzik  *	None.
5117c6fd2807SJeff Garzik  */
5118c6fd2807SJeff Garzik 
511998bd4be1SShaohua Li struct ata_queued_cmd *ata_qc_new_init(struct ata_device *dev, int tag)
5120c6fd2807SJeff Garzik {
51219af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
5122c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
5123c6fd2807SJeff Garzik 
512498bd4be1SShaohua Li 	/* no command while frozen */
512598bd4be1SShaohua Li 	if (unlikely(ap->pflags & ATA_PFLAG_FROZEN))
512698bd4be1SShaohua Li 		return NULL;
512798bd4be1SShaohua Li 
512898bd4be1SShaohua Li 	/* libsas case */
51295067c046SShaohua Li 	if (ap->flags & ATA_FLAG_SAS_HOST) {
513098bd4be1SShaohua Li 		tag = ata_sas_allocate_tag(ap);
513198bd4be1SShaohua Li 		if (tag < 0)
513298bd4be1SShaohua Li 			return NULL;
513398bd4be1SShaohua Li 	}
513498bd4be1SShaohua Li 
513598bd4be1SShaohua Li 	qc = __ata_qc_from_tag(ap, tag);
513698bd4be1SShaohua Li 	qc->tag = tag;
5137c6fd2807SJeff Garzik 	qc->scsicmd = NULL;
5138c6fd2807SJeff Garzik 	qc->ap = ap;
5139c6fd2807SJeff Garzik 	qc->dev = dev;
5140c6fd2807SJeff Garzik 
5141c6fd2807SJeff Garzik 	ata_qc_reinit(qc);
5142c6fd2807SJeff Garzik 
5143c6fd2807SJeff Garzik 	return qc;
5144c6fd2807SJeff Garzik }
5145c6fd2807SJeff Garzik 
51468a8bc223STejun Heo /**
51478a8bc223STejun Heo  *	ata_qc_free - free unused ata_queued_cmd
51488a8bc223STejun Heo  *	@qc: Command to complete
51498a8bc223STejun Heo  *
51508a8bc223STejun Heo  *	Designed to free unused ata_queued_cmd object
51518a8bc223STejun Heo  *	in case something prevents using it.
51528a8bc223STejun Heo  *
51538a8bc223STejun Heo  *	LOCKING:
51548a8bc223STejun Heo  *	spin_lock_irqsave(host lock)
51558a8bc223STejun Heo  */
51568a8bc223STejun Heo void ata_qc_free(struct ata_queued_cmd *qc)
51578a8bc223STejun Heo {
5158a1104016SJulia Lawall 	struct ata_port *ap;
51598a8bc223STejun Heo 	unsigned int tag;
51608a8bc223STejun Heo 
5161efcb3cf7STejun Heo 	WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
5162a1104016SJulia Lawall 	ap = qc->ap;
51638a8bc223STejun Heo 
51648a8bc223STejun Heo 	qc->flags = 0;
51658a8bc223STejun Heo 	tag = qc->tag;
51668a8bc223STejun Heo 	if (likely(ata_tag_valid(tag))) {
51678a8bc223STejun Heo 		qc->tag = ATA_TAG_POISON;
51685067c046SShaohua Li 		if (ap->flags & ATA_FLAG_SAS_HOST)
516998bd4be1SShaohua Li 			ata_sas_free_tag(tag, ap);
51708a8bc223STejun Heo 	}
51718a8bc223STejun Heo }
51728a8bc223STejun Heo 
5173c6fd2807SJeff Garzik void __ata_qc_complete(struct ata_queued_cmd *qc)
5174c6fd2807SJeff Garzik {
5175a1104016SJulia Lawall 	struct ata_port *ap;
5176a1104016SJulia Lawall 	struct ata_link *link;
5177c6fd2807SJeff Garzik 
5178efcb3cf7STejun Heo 	WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
5179efcb3cf7STejun Heo 	WARN_ON_ONCE(!(qc->flags & ATA_QCFLAG_ACTIVE));
5180a1104016SJulia Lawall 	ap = qc->ap;
5181a1104016SJulia Lawall 	link = qc->dev->link;
5182c6fd2807SJeff Garzik 
5183c6fd2807SJeff Garzik 	if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
5184c6fd2807SJeff Garzik 		ata_sg_clean(qc);
5185c6fd2807SJeff Garzik 
5186c6fd2807SJeff Garzik 	/* command should be marked inactive atomically with qc completion */
5187179b310aSHannes Reinecke 	if (ata_is_ncq(qc->tf.protocol)) {
51889af5c9c9STejun Heo 		link->sactive &= ~(1 << qc->tag);
5189da917d69STejun Heo 		if (!link->sactive)
5190da917d69STejun Heo 			ap->nr_active_links--;
5191da917d69STejun Heo 	} else {
51929af5c9c9STejun Heo 		link->active_tag = ATA_TAG_POISON;
5193da917d69STejun Heo 		ap->nr_active_links--;
5194da917d69STejun Heo 	}
5195da917d69STejun Heo 
5196da917d69STejun Heo 	/* clear exclusive status */
5197da917d69STejun Heo 	if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
5198da917d69STejun Heo 		     ap->excl_link == link))
5199da917d69STejun Heo 		ap->excl_link = NULL;
5200c6fd2807SJeff Garzik 
5201c6fd2807SJeff Garzik 	/* atapi: mark qc as inactive to prevent the interrupt handler
5202c6fd2807SJeff Garzik 	 * from completing the command twice later, before the error handler
5203c6fd2807SJeff Garzik 	 * is called. (when rc != 0 and atapi request sense is needed)
5204c6fd2807SJeff Garzik 	 */
5205c6fd2807SJeff Garzik 	qc->flags &= ~ATA_QCFLAG_ACTIVE;
5206c6fd2807SJeff Garzik 	ap->qc_active &= ~(1 << qc->tag);
5207c6fd2807SJeff Garzik 
5208c6fd2807SJeff Garzik 	/* call completion callback */
5209c6fd2807SJeff Garzik 	qc->complete_fn(qc);
5210c6fd2807SJeff Garzik }
5211c6fd2807SJeff Garzik 
521239599a53STejun Heo static void fill_result_tf(struct ata_queued_cmd *qc)
521339599a53STejun Heo {
521439599a53STejun Heo 	struct ata_port *ap = qc->ap;
521539599a53STejun Heo 
521639599a53STejun Heo 	qc->result_tf.flags = qc->tf.flags;
521722183bf5STejun Heo 	ap->ops->qc_fill_rtf(qc);
521839599a53STejun Heo }
521939599a53STejun Heo 
522000115e0fSTejun Heo static void ata_verify_xfer(struct ata_queued_cmd *qc)
522100115e0fSTejun Heo {
522200115e0fSTejun Heo 	struct ata_device *dev = qc->dev;
522300115e0fSTejun Heo 
5224eb0effdfSChristoph Hellwig 	if (!ata_is_data(qc->tf.protocol))
522500115e0fSTejun Heo 		return;
522600115e0fSTejun Heo 
522700115e0fSTejun Heo 	if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
522800115e0fSTejun Heo 		return;
522900115e0fSTejun Heo 
523000115e0fSTejun Heo 	dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
523100115e0fSTejun Heo }
523200115e0fSTejun Heo 
5233c6fd2807SJeff Garzik /**
5234c6fd2807SJeff Garzik  *	ata_qc_complete - Complete an active ATA command
5235c6fd2807SJeff Garzik  *	@qc: Command to complete
5236c6fd2807SJeff Garzik  *
52371aadf5c3STejun Heo  *	Indicate to the mid and upper layers that an ATA command has
52381aadf5c3STejun Heo  *	completed, with either an ok or not-ok status.
52391aadf5c3STejun Heo  *
52401aadf5c3STejun Heo  *	Refrain from calling this function multiple times when
52411aadf5c3STejun Heo  *	successfully completing multiple NCQ commands.
52421aadf5c3STejun Heo  *	ata_qc_complete_multiple() should be used instead, which will
52431aadf5c3STejun Heo  *	properly update IRQ expect state.
5244c6fd2807SJeff Garzik  *
5245c6fd2807SJeff Garzik  *	LOCKING:
5246cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
5247c6fd2807SJeff Garzik  */
5248c6fd2807SJeff Garzik void ata_qc_complete(struct ata_queued_cmd *qc)
5249c6fd2807SJeff Garzik {
5250c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
5251c6fd2807SJeff Garzik 
5252eb25cb99SStephan Linz 	/* Trigger the LED (if available) */
5253d1ed7c55SLinus Walleij 	ledtrig_disk_activity(!!(qc->tf.flags & ATA_TFLAG_WRITE));
5254eb25cb99SStephan Linz 
5255c6fd2807SJeff Garzik 	/* XXX: New EH and old EH use different mechanisms to
5256c6fd2807SJeff Garzik 	 * synchronize EH with regular execution path.
5257c6fd2807SJeff Garzik 	 *
5258c6fd2807SJeff Garzik 	 * In new EH, a failed qc is marked with ATA_QCFLAG_FAILED.
5259c6fd2807SJeff Garzik 	 * Normal execution path is responsible for not accessing a
5260c6fd2807SJeff Garzik 	 * failed qc.  libata core enforces the rule by returning NULL
5261c6fd2807SJeff Garzik 	 * from ata_qc_from_tag() for failed qcs.
5262c6fd2807SJeff Garzik 	 *
5263c6fd2807SJeff Garzik 	 * Old EH depends on ata_qc_complete() nullifying completion
5264c6fd2807SJeff Garzik 	 * requests if ATA_QCFLAG_EH_SCHEDULED is set.  Old EH does
5265c6fd2807SJeff Garzik 	 * not synchronize with interrupt handler.  Only PIO task is
5266c6fd2807SJeff Garzik 	 * taken care of.
5267c6fd2807SJeff Garzik 	 */
5268c6fd2807SJeff Garzik 	if (ap->ops->error_handler) {
52694dbfa39bSTejun Heo 		struct ata_device *dev = qc->dev;
52704dbfa39bSTejun Heo 		struct ata_eh_info *ehi = &dev->link->eh_info;
52714dbfa39bSTejun Heo 
5272c6fd2807SJeff Garzik 		if (unlikely(qc->err_mask))
5273c6fd2807SJeff Garzik 			qc->flags |= ATA_QCFLAG_FAILED;
5274c6fd2807SJeff Garzik 
5275f08dc1acSTejun Heo 		/*
5276f08dc1acSTejun Heo 		 * Finish internal commands without any further processing
5277f08dc1acSTejun Heo 		 * and always with the result TF filled.
5278f08dc1acSTejun Heo 		 */
5279f08dc1acSTejun Heo 		if (unlikely(ata_tag_internal(qc->tag))) {
528039599a53STejun Heo 			fill_result_tf(qc);
5281255c03d1SHannes Reinecke 			trace_ata_qc_complete_internal(qc);
5282f4b31db9STejun Heo 			__ata_qc_complete(qc);
5283c6fd2807SJeff Garzik 			return;
5284c6fd2807SJeff Garzik 		}
5285c6fd2807SJeff Garzik 
5286f08dc1acSTejun Heo 		/*
5287f08dc1acSTejun Heo 		 * Non-internal qc has failed.  Fill the result TF and
5288f08dc1acSTejun Heo 		 * summon EH.
5289f08dc1acSTejun Heo 		 */
5290f08dc1acSTejun Heo 		if (unlikely(qc->flags & ATA_QCFLAG_FAILED)) {
5291f08dc1acSTejun Heo 			fill_result_tf(qc);
5292255c03d1SHannes Reinecke 			trace_ata_qc_complete_failed(qc);
5293f08dc1acSTejun Heo 			ata_qc_schedule_eh(qc);
5294f08dc1acSTejun Heo 			return;
5295f08dc1acSTejun Heo 		}
5296f08dc1acSTejun Heo 
52974dc738edSTejun Heo 		WARN_ON_ONCE(ap->pflags & ATA_PFLAG_FROZEN);
52984dc738edSTejun Heo 
5299c6fd2807SJeff Garzik 		/* read result TF if requested */
5300c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_RESULT_TF)
530139599a53STejun Heo 			fill_result_tf(qc);
5302c6fd2807SJeff Garzik 
5303255c03d1SHannes Reinecke 		trace_ata_qc_complete_done(qc);
53044dbfa39bSTejun Heo 		/* Some commands need post-processing after successful
53054dbfa39bSTejun Heo 		 * completion.
53064dbfa39bSTejun Heo 		 */
53074dbfa39bSTejun Heo 		switch (qc->tf.command) {
53084dbfa39bSTejun Heo 		case ATA_CMD_SET_FEATURES:
53094dbfa39bSTejun Heo 			if (qc->tf.feature != SETFEATURES_WC_ON &&
53100c12735eSTom Yan 			    qc->tf.feature != SETFEATURES_WC_OFF &&
53110c12735eSTom Yan 			    qc->tf.feature != SETFEATURES_RA_ON &&
53120c12735eSTom Yan 			    qc->tf.feature != SETFEATURES_RA_OFF)
53134dbfa39bSTejun Heo 				break;
53144dbfa39bSTejun Heo 			/* fall through */
53154dbfa39bSTejun Heo 		case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
53164dbfa39bSTejun Heo 		case ATA_CMD_SET_MULTI: /* multi_count changed */
53174dbfa39bSTejun Heo 			/* revalidate device */
53184dbfa39bSTejun Heo 			ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE;
53194dbfa39bSTejun Heo 			ata_port_schedule_eh(ap);
53204dbfa39bSTejun Heo 			break;
5321054a5fbaSTejun Heo 
5322054a5fbaSTejun Heo 		case ATA_CMD_SLEEP:
5323054a5fbaSTejun Heo 			dev->flags |= ATA_DFLAG_SLEEPING;
5324054a5fbaSTejun Heo 			break;
53254dbfa39bSTejun Heo 		}
53264dbfa39bSTejun Heo 
532700115e0fSTejun Heo 		if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
532800115e0fSTejun Heo 			ata_verify_xfer(qc);
532900115e0fSTejun Heo 
5330c6fd2807SJeff Garzik 		__ata_qc_complete(qc);
5331c6fd2807SJeff Garzik 	} else {
5332c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
5333c6fd2807SJeff Garzik 			return;
5334c6fd2807SJeff Garzik 
5335c6fd2807SJeff Garzik 		/* read result TF if failed or requested */
5336c6fd2807SJeff Garzik 		if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
533739599a53STejun Heo 			fill_result_tf(qc);
5338c6fd2807SJeff Garzik 
5339c6fd2807SJeff Garzik 		__ata_qc_complete(qc);
5340c6fd2807SJeff Garzik 	}
5341c6fd2807SJeff Garzik }
5342c6fd2807SJeff Garzik 
5343c6fd2807SJeff Garzik /**
5344c6fd2807SJeff Garzik  *	ata_qc_complete_multiple - Complete multiple qcs successfully
5345c6fd2807SJeff Garzik  *	@ap: port in question
5346c6fd2807SJeff Garzik  *	@qc_active: new qc_active mask
5347c6fd2807SJeff Garzik  *
5348c6fd2807SJeff Garzik  *	Complete in-flight commands.  This functions is meant to be
5349c6fd2807SJeff Garzik  *	called from low-level driver's interrupt routine to complete
5350c6fd2807SJeff Garzik  *	requests normally.  ap->qc_active and @qc_active is compared
5351c6fd2807SJeff Garzik  *	and commands are completed accordingly.
5352c6fd2807SJeff Garzik  *
53531aadf5c3STejun Heo  *	Always use this function when completing multiple NCQ commands
53541aadf5c3STejun Heo  *	from IRQ handlers instead of calling ata_qc_complete()
53551aadf5c3STejun Heo  *	multiple times to keep IRQ expect status properly in sync.
53561aadf5c3STejun Heo  *
5357c6fd2807SJeff Garzik  *	LOCKING:
5358cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
5359c6fd2807SJeff Garzik  *
5360c6fd2807SJeff Garzik  *	RETURNS:
5361c6fd2807SJeff Garzik  *	Number of completed commands on success, -errno otherwise.
5362c6fd2807SJeff Garzik  */
536379f97dadSTejun Heo int ata_qc_complete_multiple(struct ata_port *ap, u32 qc_active)
5364c6fd2807SJeff Garzik {
5365c6fd2807SJeff Garzik 	int nr_done = 0;
5366c6fd2807SJeff Garzik 	u32 done_mask;
5367c6fd2807SJeff Garzik 
5368c6fd2807SJeff Garzik 	done_mask = ap->qc_active ^ qc_active;
5369c6fd2807SJeff Garzik 
5370c6fd2807SJeff Garzik 	if (unlikely(done_mask & qc_active)) {
5371a9a79dfeSJoe Perches 		ata_port_err(ap, "illegal qc_active transition (%08x->%08x)\n",
5372a9a79dfeSJoe Perches 			     ap->qc_active, qc_active);
5373c6fd2807SJeff Garzik 		return -EINVAL;
5374c6fd2807SJeff Garzik 	}
5375c6fd2807SJeff Garzik 
537643768180SJens Axboe 	while (done_mask) {
5377c6fd2807SJeff Garzik 		struct ata_queued_cmd *qc;
537843768180SJens Axboe 		unsigned int tag = __ffs(done_mask);
5379c6fd2807SJeff Garzik 
538043768180SJens Axboe 		qc = ata_qc_from_tag(ap, tag);
538143768180SJens Axboe 		if (qc) {
5382c6fd2807SJeff Garzik 			ata_qc_complete(qc);
5383c6fd2807SJeff Garzik 			nr_done++;
5384c6fd2807SJeff Garzik 		}
538543768180SJens Axboe 		done_mask &= ~(1 << tag);
5386c6fd2807SJeff Garzik 	}
5387c6fd2807SJeff Garzik 
5388c6fd2807SJeff Garzik 	return nr_done;
5389c6fd2807SJeff Garzik }
5390c6fd2807SJeff Garzik 
5391c6fd2807SJeff Garzik /**
5392c6fd2807SJeff Garzik  *	ata_qc_issue - issue taskfile to device
5393c6fd2807SJeff Garzik  *	@qc: command to issue to device
5394c6fd2807SJeff Garzik  *
5395c6fd2807SJeff Garzik  *	Prepare an ATA command to submission to device.
5396c6fd2807SJeff Garzik  *	This includes mapping the data into a DMA-able
5397c6fd2807SJeff Garzik  *	area, filling in the S/G table, and finally
5398c6fd2807SJeff Garzik  *	writing the taskfile to hardware, starting the command.
5399c6fd2807SJeff Garzik  *
5400c6fd2807SJeff Garzik  *	LOCKING:
5401cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
5402c6fd2807SJeff Garzik  */
5403c6fd2807SJeff Garzik void ata_qc_issue(struct ata_queued_cmd *qc)
5404c6fd2807SJeff Garzik {
5405c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
54069af5c9c9STejun Heo 	struct ata_link *link = qc->dev->link;
5407405e66b3STejun Heo 	u8 prot = qc->tf.protocol;
5408c6fd2807SJeff Garzik 
5409c6fd2807SJeff Garzik 	/* Make sure only one non-NCQ command is outstanding.  The
5410c6fd2807SJeff Garzik 	 * check is skipped for old EH because it reuses active qc to
5411c6fd2807SJeff Garzik 	 * request ATAPI sense.
5412c6fd2807SJeff Garzik 	 */
5413efcb3cf7STejun Heo 	WARN_ON_ONCE(ap->ops->error_handler && ata_tag_valid(link->active_tag));
5414c6fd2807SJeff Garzik 
54151973a023STejun Heo 	if (ata_is_ncq(prot)) {
5416efcb3cf7STejun Heo 		WARN_ON_ONCE(link->sactive & (1 << qc->tag));
5417da917d69STejun Heo 
5418da917d69STejun Heo 		if (!link->sactive)
5419da917d69STejun Heo 			ap->nr_active_links++;
54209af5c9c9STejun Heo 		link->sactive |= 1 << qc->tag;
5421c6fd2807SJeff Garzik 	} else {
5422efcb3cf7STejun Heo 		WARN_ON_ONCE(link->sactive);
5423da917d69STejun Heo 
5424da917d69STejun Heo 		ap->nr_active_links++;
54259af5c9c9STejun Heo 		link->active_tag = qc->tag;
5426c6fd2807SJeff Garzik 	}
5427c6fd2807SJeff Garzik 
5428c6fd2807SJeff Garzik 	qc->flags |= ATA_QCFLAG_ACTIVE;
5429c6fd2807SJeff Garzik 	ap->qc_active |= 1 << qc->tag;
5430c6fd2807SJeff Garzik 
543160f5d6efSTejun Heo 	/*
543260f5d6efSTejun Heo 	 * We guarantee to LLDs that they will have at least one
5433f92a2636STejun Heo 	 * non-zero sg if the command is a data command.
5434f92a2636STejun Heo 	 */
54359173e5e8SEric Biggers 	if (ata_is_data(prot) && (!qc->sg || !qc->n_elem || !qc->nbytes))
543660f5d6efSTejun Heo 		goto sys_err;
5437f92a2636STejun Heo 
5438405e66b3STejun Heo 	if (ata_is_dma(prot) || (ata_is_pio(prot) &&
5439f92a2636STejun Heo 				 (ap->flags & ATA_FLAG_PIO_DMA)))
5440c6fd2807SJeff Garzik 		if (ata_sg_setup(qc))
544160f5d6efSTejun Heo 			goto sys_err;
5442c6fd2807SJeff Garzik 
5443cf480626STejun Heo 	/* if device is sleeping, schedule reset and abort the link */
5444054a5fbaSTejun Heo 	if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
5445cf480626STejun Heo 		link->eh_info.action |= ATA_EH_RESET;
5446054a5fbaSTejun Heo 		ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
5447054a5fbaSTejun Heo 		ata_link_abort(link);
5448054a5fbaSTejun Heo 		return;
5449054a5fbaSTejun Heo 	}
5450054a5fbaSTejun Heo 
5451c6fd2807SJeff Garzik 	ap->ops->qc_prep(qc);
5452255c03d1SHannes Reinecke 	trace_ata_qc_issue(qc);
5453c6fd2807SJeff Garzik 	qc->err_mask |= ap->ops->qc_issue(qc);
5454c6fd2807SJeff Garzik 	if (unlikely(qc->err_mask))
5455c6fd2807SJeff Garzik 		goto err;
5456c6fd2807SJeff Garzik 	return;
5457c6fd2807SJeff Garzik 
545860f5d6efSTejun Heo sys_err:
5459c6fd2807SJeff Garzik 	qc->err_mask |= AC_ERR_SYSTEM;
5460c6fd2807SJeff Garzik err:
5461c6fd2807SJeff Garzik 	ata_qc_complete(qc);
5462c6fd2807SJeff Garzik }
5463c6fd2807SJeff Garzik 
5464c6fd2807SJeff Garzik /**
5465c6fd2807SJeff Garzik  *	sata_scr_valid - test whether SCRs are accessible
5466936fd732STejun Heo  *	@link: ATA link to test SCR accessibility for
5467c6fd2807SJeff Garzik  *
5468936fd732STejun Heo  *	Test whether SCRs are accessible for @link.
5469c6fd2807SJeff Garzik  *
5470c6fd2807SJeff Garzik  *	LOCKING:
5471c6fd2807SJeff Garzik  *	None.
5472c6fd2807SJeff Garzik  *
5473c6fd2807SJeff Garzik  *	RETURNS:
5474c6fd2807SJeff Garzik  *	1 if SCRs are accessible, 0 otherwise.
5475c6fd2807SJeff Garzik  */
5476936fd732STejun Heo int sata_scr_valid(struct ata_link *link)
5477c6fd2807SJeff Garzik {
5478936fd732STejun Heo 	struct ata_port *ap = link->ap;
5479936fd732STejun Heo 
5480a16abc0bSTejun Heo 	return (ap->flags & ATA_FLAG_SATA) && ap->ops->scr_read;
5481c6fd2807SJeff Garzik }
5482c6fd2807SJeff Garzik 
5483c6fd2807SJeff Garzik /**
5484c6fd2807SJeff Garzik  *	sata_scr_read - read SCR register of the specified port
5485936fd732STejun Heo  *	@link: ATA link to read SCR for
5486c6fd2807SJeff Garzik  *	@reg: SCR to read
5487c6fd2807SJeff Garzik  *	@val: Place to store read value
5488c6fd2807SJeff Garzik  *
5489936fd732STejun Heo  *	Read SCR register @reg of @link into *@val.  This function is
5490633273a3STejun Heo  *	guaranteed to succeed if @link is ap->link, the cable type of
5491633273a3STejun Heo  *	the port is SATA and the port implements ->scr_read.
5492c6fd2807SJeff Garzik  *
5493c6fd2807SJeff Garzik  *	LOCKING:
5494633273a3STejun Heo  *	None if @link is ap->link.  Kernel thread context otherwise.
5495c6fd2807SJeff Garzik  *
5496c6fd2807SJeff Garzik  *	RETURNS:
5497c6fd2807SJeff Garzik  *	0 on success, negative errno on failure.
5498c6fd2807SJeff Garzik  */
5499936fd732STejun Heo int sata_scr_read(struct ata_link *link, int reg, u32 *val)
5500c6fd2807SJeff Garzik {
5501633273a3STejun Heo 	if (ata_is_host_link(link)) {
5502936fd732STejun Heo 		if (sata_scr_valid(link))
550382ef04fbSTejun Heo 			return link->ap->ops->scr_read(link, reg, val);
5504c6fd2807SJeff Garzik 		return -EOPNOTSUPP;
5505c6fd2807SJeff Garzik 	}
5506c6fd2807SJeff Garzik 
5507633273a3STejun Heo 	return sata_pmp_scr_read(link, reg, val);
5508633273a3STejun Heo }
5509633273a3STejun Heo 
5510c6fd2807SJeff Garzik /**
5511c6fd2807SJeff Garzik  *	sata_scr_write - write SCR register of the specified port
5512936fd732STejun Heo  *	@link: ATA link to write SCR for
5513c6fd2807SJeff Garzik  *	@reg: SCR to write
5514c6fd2807SJeff Garzik  *	@val: value to write
5515c6fd2807SJeff Garzik  *
5516936fd732STejun Heo  *	Write @val to SCR register @reg of @link.  This function is
5517633273a3STejun Heo  *	guaranteed to succeed if @link is ap->link, the cable type of
5518633273a3STejun Heo  *	the port is SATA and the port implements ->scr_read.
5519c6fd2807SJeff Garzik  *
5520c6fd2807SJeff Garzik  *	LOCKING:
5521633273a3STejun Heo  *	None if @link is ap->link.  Kernel thread context otherwise.
5522c6fd2807SJeff Garzik  *
5523c6fd2807SJeff Garzik  *	RETURNS:
5524c6fd2807SJeff Garzik  *	0 on success, negative errno on failure.
5525c6fd2807SJeff Garzik  */
5526936fd732STejun Heo int sata_scr_write(struct ata_link *link, int reg, u32 val)
5527c6fd2807SJeff Garzik {
5528633273a3STejun Heo 	if (ata_is_host_link(link)) {
5529936fd732STejun Heo 		if (sata_scr_valid(link))
553082ef04fbSTejun Heo 			return link->ap->ops->scr_write(link, reg, val);
5531c6fd2807SJeff Garzik 		return -EOPNOTSUPP;
5532c6fd2807SJeff Garzik 	}
5533c6fd2807SJeff Garzik 
5534633273a3STejun Heo 	return sata_pmp_scr_write(link, reg, val);
5535633273a3STejun Heo }
5536633273a3STejun Heo 
5537c6fd2807SJeff Garzik /**
5538c6fd2807SJeff Garzik  *	sata_scr_write_flush - write SCR register of the specified port and flush
5539936fd732STejun Heo  *	@link: ATA link to write SCR for
5540c6fd2807SJeff Garzik  *	@reg: SCR to write
5541c6fd2807SJeff Garzik  *	@val: value to write
5542c6fd2807SJeff Garzik  *
5543c6fd2807SJeff Garzik  *	This function is identical to sata_scr_write() except that this
5544c6fd2807SJeff Garzik  *	function performs flush after writing to the register.
5545c6fd2807SJeff Garzik  *
5546c6fd2807SJeff Garzik  *	LOCKING:
5547633273a3STejun Heo  *	None if @link is ap->link.  Kernel thread context otherwise.
5548c6fd2807SJeff Garzik  *
5549c6fd2807SJeff Garzik  *	RETURNS:
5550c6fd2807SJeff Garzik  *	0 on success, negative errno on failure.
5551c6fd2807SJeff Garzik  */
5552936fd732STejun Heo int sata_scr_write_flush(struct ata_link *link, int reg, u32 val)
5553c6fd2807SJeff Garzik {
5554633273a3STejun Heo 	if (ata_is_host_link(link)) {
5555da3dbb17STejun Heo 		int rc;
5556da3dbb17STejun Heo 
5557936fd732STejun Heo 		if (sata_scr_valid(link)) {
555882ef04fbSTejun Heo 			rc = link->ap->ops->scr_write(link, reg, val);
5559da3dbb17STejun Heo 			if (rc == 0)
556082ef04fbSTejun Heo 				rc = link->ap->ops->scr_read(link, reg, &val);
5561da3dbb17STejun Heo 			return rc;
5562c6fd2807SJeff Garzik 		}
5563c6fd2807SJeff Garzik 		return -EOPNOTSUPP;
5564c6fd2807SJeff Garzik 	}
5565c6fd2807SJeff Garzik 
5566633273a3STejun Heo 	return sata_pmp_scr_write(link, reg, val);
5567633273a3STejun Heo }
5568633273a3STejun Heo 
5569c6fd2807SJeff Garzik /**
5570b1c72916STejun Heo  *	ata_phys_link_online - test whether the given link is online
5571936fd732STejun Heo  *	@link: ATA link to test
5572c6fd2807SJeff Garzik  *
5573936fd732STejun Heo  *	Test whether @link is online.  Note that this function returns
5574936fd732STejun Heo  *	0 if online status of @link cannot be obtained, so
5575936fd732STejun Heo  *	ata_link_online(link) != !ata_link_offline(link).
5576c6fd2807SJeff Garzik  *
5577c6fd2807SJeff Garzik  *	LOCKING:
5578c6fd2807SJeff Garzik  *	None.
5579c6fd2807SJeff Garzik  *
5580c6fd2807SJeff Garzik  *	RETURNS:
5581b5b3fa38STejun Heo  *	True if the port online status is available and online.
5582c6fd2807SJeff Garzik  */
5583b1c72916STejun Heo bool ata_phys_link_online(struct ata_link *link)
5584c6fd2807SJeff Garzik {
5585c6fd2807SJeff Garzik 	u32 sstatus;
5586c6fd2807SJeff Garzik 
5587936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
55889913ff8aSTejun Heo 	    ata_sstatus_online(sstatus))
5589b5b3fa38STejun Heo 		return true;
5590b5b3fa38STejun Heo 	return false;
5591c6fd2807SJeff Garzik }
5592c6fd2807SJeff Garzik 
5593c6fd2807SJeff Garzik /**
5594b1c72916STejun Heo  *	ata_phys_link_offline - test whether the given link is offline
5595936fd732STejun Heo  *	@link: ATA link to test
5596c6fd2807SJeff Garzik  *
5597936fd732STejun Heo  *	Test whether @link is offline.  Note that this function
5598936fd732STejun Heo  *	returns 0 if offline status of @link cannot be obtained, so
5599936fd732STejun Heo  *	ata_link_online(link) != !ata_link_offline(link).
5600c6fd2807SJeff Garzik  *
5601c6fd2807SJeff Garzik  *	LOCKING:
5602c6fd2807SJeff Garzik  *	None.
5603c6fd2807SJeff Garzik  *
5604c6fd2807SJeff Garzik  *	RETURNS:
5605b5b3fa38STejun Heo  *	True if the port offline status is available and offline.
5606c6fd2807SJeff Garzik  */
5607b1c72916STejun Heo bool ata_phys_link_offline(struct ata_link *link)
5608c6fd2807SJeff Garzik {
5609c6fd2807SJeff Garzik 	u32 sstatus;
5610c6fd2807SJeff Garzik 
5611936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
56129913ff8aSTejun Heo 	    !ata_sstatus_online(sstatus))
5613b5b3fa38STejun Heo 		return true;
5614b5b3fa38STejun Heo 	return false;
5615c6fd2807SJeff Garzik }
5616c6fd2807SJeff Garzik 
5617b1c72916STejun Heo /**
5618b1c72916STejun Heo  *	ata_link_online - test whether the given link is online
5619b1c72916STejun Heo  *	@link: ATA link to test
5620b1c72916STejun Heo  *
5621b1c72916STejun Heo  *	Test whether @link is online.  This is identical to
5622b1c72916STejun Heo  *	ata_phys_link_online() when there's no slave link.  When
5623b1c72916STejun Heo  *	there's a slave link, this function should only be called on
5624b1c72916STejun Heo  *	the master link and will return true if any of M/S links is
5625b1c72916STejun Heo  *	online.
5626b1c72916STejun Heo  *
5627b1c72916STejun Heo  *	LOCKING:
5628b1c72916STejun Heo  *	None.
5629b1c72916STejun Heo  *
5630b1c72916STejun Heo  *	RETURNS:
5631b1c72916STejun Heo  *	True if the port online status is available and online.
5632b1c72916STejun Heo  */
5633b1c72916STejun Heo bool ata_link_online(struct ata_link *link)
5634b1c72916STejun Heo {
5635b1c72916STejun Heo 	struct ata_link *slave = link->ap->slave_link;
5636b1c72916STejun Heo 
5637b1c72916STejun Heo 	WARN_ON(link == slave);	/* shouldn't be called on slave link */
5638b1c72916STejun Heo 
5639b1c72916STejun Heo 	return ata_phys_link_online(link) ||
5640b1c72916STejun Heo 		(slave && ata_phys_link_online(slave));
5641b1c72916STejun Heo }
5642b1c72916STejun Heo 
5643b1c72916STejun Heo /**
5644b1c72916STejun Heo  *	ata_link_offline - test whether the given link is offline
5645b1c72916STejun Heo  *	@link: ATA link to test
5646b1c72916STejun Heo  *
5647b1c72916STejun Heo  *	Test whether @link is offline.  This is identical to
5648b1c72916STejun Heo  *	ata_phys_link_offline() when there's no slave link.  When
5649b1c72916STejun Heo  *	there's a slave link, this function should only be called on
5650b1c72916STejun Heo  *	the master link and will return true if both M/S links are
5651b1c72916STejun Heo  *	offline.
5652b1c72916STejun Heo  *
5653b1c72916STejun Heo  *	LOCKING:
5654b1c72916STejun Heo  *	None.
5655b1c72916STejun Heo  *
5656b1c72916STejun Heo  *	RETURNS:
5657b1c72916STejun Heo  *	True if the port offline status is available and offline.
5658b1c72916STejun Heo  */
5659b1c72916STejun Heo bool ata_link_offline(struct ata_link *link)
5660b1c72916STejun Heo {
5661b1c72916STejun Heo 	struct ata_link *slave = link->ap->slave_link;
5662b1c72916STejun Heo 
5663b1c72916STejun Heo 	WARN_ON(link == slave);	/* shouldn't be called on slave link */
5664b1c72916STejun Heo 
5665b1c72916STejun Heo 	return ata_phys_link_offline(link) &&
5666b1c72916STejun Heo 		(!slave || ata_phys_link_offline(slave));
5667b1c72916STejun Heo }
5668b1c72916STejun Heo 
56696ffa01d8STejun Heo #ifdef CONFIG_PM
5670bc6e7c4bSDan Williams static void ata_port_request_pm(struct ata_port *ap, pm_message_t mesg,
5671cca3974eSJeff Garzik 				unsigned int action, unsigned int ehi_flags,
5672bc6e7c4bSDan Williams 				bool async)
5673c6fd2807SJeff Garzik {
5674e3667ebfSTejun Heo 	struct ata_link *link;
56755ef41082SLin Ming 	unsigned long flags;
5676c6fd2807SJeff Garzik 
5677c6fd2807SJeff Garzik 	/* Previous resume operation might still be in
5678c6fd2807SJeff Garzik 	 * progress.  Wait for PM_PENDING to clear.
5679c6fd2807SJeff Garzik 	 */
5680c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_PM_PENDING) {
5681c6fd2807SJeff Garzik 		ata_port_wait_eh(ap);
5682c6fd2807SJeff Garzik 		WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5683c6fd2807SJeff Garzik 	}
5684c6fd2807SJeff Garzik 
5685c6fd2807SJeff Garzik 	/* request PM ops to EH */
5686c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
5687c6fd2807SJeff Garzik 
5688c6fd2807SJeff Garzik 	ap->pm_mesg = mesg;
5689c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_PM_PENDING;
56901eca4365STejun Heo 	ata_for_each_link(link, ap, HOST_FIRST) {
5691e3667ebfSTejun Heo 		link->eh_info.action |= action;
5692e3667ebfSTejun Heo 		link->eh_info.flags |= ehi_flags;
5693e3667ebfSTejun Heo 	}
5694c6fd2807SJeff Garzik 
5695c6fd2807SJeff Garzik 	ata_port_schedule_eh(ap);
5696c6fd2807SJeff Garzik 
5697c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
5698c6fd2807SJeff Garzik 
56992fcbdcb4SDan Williams 	if (!async) {
5700c6fd2807SJeff Garzik 		ata_port_wait_eh(ap);
5701c6fd2807SJeff Garzik 		WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5702c6fd2807SJeff Garzik 	}
5703c6fd2807SJeff Garzik }
5704c6fd2807SJeff Garzik 
570533574d68SLin Ming /*
5706bc6e7c4bSDan Williams  * On some hardware, device fails to respond after spun down for suspend.  As
5707bc6e7c4bSDan Williams  * the device won't be used before being resumed, we don't need to touch the
5708bc6e7c4bSDan Williams  * device.  Ask EH to skip the usual stuff and proceed directly to suspend.
570933574d68SLin Ming  *
571033574d68SLin Ming  * http://thread.gmane.org/gmane.linux.ide/46764
571133574d68SLin Ming  */
5712bc6e7c4bSDan Williams static const unsigned int ata_port_suspend_ehi = ATA_EHI_QUIET
5713bc6e7c4bSDan Williams 						 | ATA_EHI_NO_AUTOPSY
5714bc6e7c4bSDan Williams 						 | ATA_EHI_NO_RECOVERY;
5715bc6e7c4bSDan Williams 
5716bc6e7c4bSDan Williams static void ata_port_suspend(struct ata_port *ap, pm_message_t mesg)
5717bc6e7c4bSDan Williams {
5718bc6e7c4bSDan Williams 	ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, false);
57195ef41082SLin Ming }
57205ef41082SLin Ming 
5721bc6e7c4bSDan Williams static void ata_port_suspend_async(struct ata_port *ap, pm_message_t mesg)
5722bc6e7c4bSDan Williams {
5723bc6e7c4bSDan Williams 	ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, true);
5724bc6e7c4bSDan Williams }
5725bc6e7c4bSDan Williams 
5726bc6e7c4bSDan Williams static int ata_port_pm_suspend(struct device *dev)
57272fcbdcb4SDan Williams {
57282fcbdcb4SDan Williams 	struct ata_port *ap = to_ata_port(dev);
57292fcbdcb4SDan Williams 
57305ef41082SLin Ming 	if (pm_runtime_suspended(dev))
57315ef41082SLin Ming 		return 0;
57325ef41082SLin Ming 
5733bc6e7c4bSDan Williams 	ata_port_suspend(ap, PMSG_SUSPEND);
5734f5e6d0d0SAaron Lu 	return 0;
573533574d68SLin Ming }
573633574d68SLin Ming 
5737bc6e7c4bSDan Williams static int ata_port_pm_freeze(struct device *dev)
57382fcbdcb4SDan Williams {
57392fcbdcb4SDan Williams 	struct ata_port *ap = to_ata_port(dev);
57402fcbdcb4SDan Williams 
5741bc6e7c4bSDan Williams 	if (pm_runtime_suspended(dev))
5742bc6e7c4bSDan Williams 		return 0;
5743bc6e7c4bSDan Williams 
5744bc6e7c4bSDan Williams 	ata_port_suspend(ap, PMSG_FREEZE);
5745bc6e7c4bSDan Williams 	return 0;
57462fcbdcb4SDan Williams }
57472fcbdcb4SDan Williams 
5748bc6e7c4bSDan Williams static int ata_port_pm_poweroff(struct device *dev)
5749e90b1e5aSLin Ming {
5750bc6e7c4bSDan Williams 	ata_port_suspend(to_ata_port(dev), PMSG_HIBERNATE);
5751bc6e7c4bSDan Williams 	return 0;
5752bc6e7c4bSDan Williams }
5753e90b1e5aSLin Ming 
5754bc6e7c4bSDan Williams static const unsigned int ata_port_resume_ehi = ATA_EHI_NO_AUTOPSY
5755bc6e7c4bSDan Williams 						| ATA_EHI_QUIET;
5756bc6e7c4bSDan Williams 
5757bc6e7c4bSDan Williams static void ata_port_resume(struct ata_port *ap, pm_message_t mesg)
5758bc6e7c4bSDan Williams {
5759bc6e7c4bSDan Williams 	ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, false);
5760bc6e7c4bSDan Williams }
5761bc6e7c4bSDan Williams 
5762bc6e7c4bSDan Williams static void ata_port_resume_async(struct ata_port *ap, pm_message_t mesg)
5763bc6e7c4bSDan Williams {
5764bc6e7c4bSDan Williams 	ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, true);
5765bc6e7c4bSDan Williams }
5766bc6e7c4bSDan Williams 
5767bc6e7c4bSDan Williams static int ata_port_pm_resume(struct device *dev)
5768bc6e7c4bSDan Williams {
5769200421a8STodd Brandt 	ata_port_resume_async(to_ata_port(dev), PMSG_RESUME);
5770e90b1e5aSLin Ming 	pm_runtime_disable(dev);
5771e90b1e5aSLin Ming 	pm_runtime_set_active(dev);
5772e90b1e5aSLin Ming 	pm_runtime_enable(dev);
5773bc6e7c4bSDan Williams 	return 0;
5774e90b1e5aSLin Ming }
5775e90b1e5aSLin Ming 
57767e15e9beSAaron Lu /*
57777e15e9beSAaron Lu  * For ODDs, the upper layer will poll for media change every few seconds,
57787e15e9beSAaron Lu  * which will make it enter and leave suspend state every few seconds. And
57797e15e9beSAaron Lu  * as each suspend will cause a hard/soft reset, the gain of runtime suspend
57807e15e9beSAaron Lu  * is very little and the ODD may malfunction after constantly being reset.
57817e15e9beSAaron Lu  * So the idle callback here will not proceed to suspend if a non-ZPODD capable
57827e15e9beSAaron Lu  * ODD is attached to the port.
57837e15e9beSAaron Lu  */
57849ee4f393SLin Ming static int ata_port_runtime_idle(struct device *dev)
57859ee4f393SLin Ming {
57867e15e9beSAaron Lu 	struct ata_port *ap = to_ata_port(dev);
57877e15e9beSAaron Lu 	struct ata_link *link;
57887e15e9beSAaron Lu 	struct ata_device *adev;
57897e15e9beSAaron Lu 
57907e15e9beSAaron Lu 	ata_for_each_link(link, ap, HOST_FIRST) {
57917e15e9beSAaron Lu 		ata_for_each_dev(adev, link, ENABLED)
57927e15e9beSAaron Lu 			if (adev->class == ATA_DEV_ATAPI &&
57937e15e9beSAaron Lu 			    !zpodd_dev_enabled(adev))
57947e15e9beSAaron Lu 				return -EBUSY;
57957e15e9beSAaron Lu 	}
57967e15e9beSAaron Lu 
579745f0a85cSRafael J. Wysocki 	return 0;
57989ee4f393SLin Ming }
57999ee4f393SLin Ming 
5800a7ff60dbSAaron Lu static int ata_port_runtime_suspend(struct device *dev)
5801a7ff60dbSAaron Lu {
5802bc6e7c4bSDan Williams 	ata_port_suspend(to_ata_port(dev), PMSG_AUTO_SUSPEND);
5803bc6e7c4bSDan Williams 	return 0;
5804a7ff60dbSAaron Lu }
5805a7ff60dbSAaron Lu 
5806a7ff60dbSAaron Lu static int ata_port_runtime_resume(struct device *dev)
5807a7ff60dbSAaron Lu {
5808bc6e7c4bSDan Williams 	ata_port_resume(to_ata_port(dev), PMSG_AUTO_RESUME);
5809bc6e7c4bSDan Williams 	return 0;
5810a7ff60dbSAaron Lu }
5811a7ff60dbSAaron Lu 
58125ef41082SLin Ming static const struct dev_pm_ops ata_port_pm_ops = {
5813bc6e7c4bSDan Williams 	.suspend = ata_port_pm_suspend,
5814bc6e7c4bSDan Williams 	.resume = ata_port_pm_resume,
5815bc6e7c4bSDan Williams 	.freeze = ata_port_pm_freeze,
5816bc6e7c4bSDan Williams 	.thaw = ata_port_pm_resume,
5817bc6e7c4bSDan Williams 	.poweroff = ata_port_pm_poweroff,
5818bc6e7c4bSDan Williams 	.restore = ata_port_pm_resume,
58199ee4f393SLin Ming 
5820a7ff60dbSAaron Lu 	.runtime_suspend = ata_port_runtime_suspend,
5821a7ff60dbSAaron Lu 	.runtime_resume = ata_port_runtime_resume,
58229ee4f393SLin Ming 	.runtime_idle = ata_port_runtime_idle,
58235ef41082SLin Ming };
58245ef41082SLin Ming 
58252fcbdcb4SDan Williams /* sas ports don't participate in pm runtime management of ata_ports,
58262fcbdcb4SDan Williams  * and need to resume ata devices at the domain level, not the per-port
58272fcbdcb4SDan Williams  * level. sas suspend/resume is async to allow parallel port recovery
58282fcbdcb4SDan Williams  * since sas has multiple ata_port instances per Scsi_Host.
58292fcbdcb4SDan Williams  */
5830bc6e7c4bSDan Williams void ata_sas_port_suspend(struct ata_port *ap)
58312fcbdcb4SDan Williams {
5832bc6e7c4bSDan Williams 	ata_port_suspend_async(ap, PMSG_SUSPEND);
58332fcbdcb4SDan Williams }
5834bc6e7c4bSDan Williams EXPORT_SYMBOL_GPL(ata_sas_port_suspend);
58352fcbdcb4SDan Williams 
5836bc6e7c4bSDan Williams void ata_sas_port_resume(struct ata_port *ap)
58372fcbdcb4SDan Williams {
5838bc6e7c4bSDan Williams 	ata_port_resume_async(ap, PMSG_RESUME);
58392fcbdcb4SDan Williams }
5840bc6e7c4bSDan Williams EXPORT_SYMBOL_GPL(ata_sas_port_resume);
58412fcbdcb4SDan Williams 
5842c6fd2807SJeff Garzik /**
5843cca3974eSJeff Garzik  *	ata_host_suspend - suspend host
5844cca3974eSJeff Garzik  *	@host: host to suspend
5845c6fd2807SJeff Garzik  *	@mesg: PM message
5846c6fd2807SJeff Garzik  *
58475ef41082SLin Ming  *	Suspend @host.  Actual operation is performed by port suspend.
5848c6fd2807SJeff Garzik  */
5849cca3974eSJeff Garzik int ata_host_suspend(struct ata_host *host, pm_message_t mesg)
5850c6fd2807SJeff Garzik {
585172ad6ec4SJeff Garzik 	host->dev->power.power_state = mesg;
58525ef41082SLin Ming 	return 0;
5853c6fd2807SJeff Garzik }
5854c6fd2807SJeff Garzik 
5855c6fd2807SJeff Garzik /**
5856cca3974eSJeff Garzik  *	ata_host_resume - resume host
5857cca3974eSJeff Garzik  *	@host: host to resume
5858c6fd2807SJeff Garzik  *
58595ef41082SLin Ming  *	Resume @host.  Actual operation is performed by port resume.
5860c6fd2807SJeff Garzik  */
5861cca3974eSJeff Garzik void ata_host_resume(struct ata_host *host)
5862c6fd2807SJeff Garzik {
586372ad6ec4SJeff Garzik 	host->dev->power.power_state = PMSG_ON;
5864c6fd2807SJeff Garzik }
58656ffa01d8STejun Heo #endif
5866c6fd2807SJeff Garzik 
58678df82c13SBhumika Goyal const struct device_type ata_port_type = {
58685ef41082SLin Ming 	.name = "ata_port",
58695ef41082SLin Ming #ifdef CONFIG_PM
58705ef41082SLin Ming 	.pm = &ata_port_pm_ops,
58715ef41082SLin Ming #endif
58725ef41082SLin Ming };
58735ef41082SLin Ming 
5874c6fd2807SJeff Garzik /**
5875c6fd2807SJeff Garzik  *	ata_dev_init - Initialize an ata_device structure
5876c6fd2807SJeff Garzik  *	@dev: Device structure to initialize
5877c6fd2807SJeff Garzik  *
5878c6fd2807SJeff Garzik  *	Initialize @dev in preparation for probing.
5879c6fd2807SJeff Garzik  *
5880c6fd2807SJeff Garzik  *	LOCKING:
5881c6fd2807SJeff Garzik  *	Inherited from caller.
5882c6fd2807SJeff Garzik  */
5883c6fd2807SJeff Garzik void ata_dev_init(struct ata_device *dev)
5884c6fd2807SJeff Garzik {
5885b1c72916STejun Heo 	struct ata_link *link = ata_dev_phys_link(dev);
58869af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
5887c6fd2807SJeff Garzik 	unsigned long flags;
5888c6fd2807SJeff Garzik 
5889b1c72916STejun Heo 	/* SATA spd limit is bound to the attached device, reset together */
58909af5c9c9STejun Heo 	link->sata_spd_limit = link->hw_sata_spd_limit;
58919af5c9c9STejun Heo 	link->sata_spd = 0;
5892c6fd2807SJeff Garzik 
5893c6fd2807SJeff Garzik 	/* High bits of dev->flags are used to record warm plug
5894c6fd2807SJeff Garzik 	 * requests which occur asynchronously.  Synchronize using
5895cca3974eSJeff Garzik 	 * host lock.
5896c6fd2807SJeff Garzik 	 */
5897c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
5898c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_INIT_MASK;
58993dcc323fSTejun Heo 	dev->horkage = 0;
5900c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
5901c6fd2807SJeff Garzik 
590299cf610aSTejun Heo 	memset((void *)dev + ATA_DEVICE_CLEAR_BEGIN, 0,
590399cf610aSTejun Heo 	       ATA_DEVICE_CLEAR_END - ATA_DEVICE_CLEAR_BEGIN);
5904c6fd2807SJeff Garzik 	dev->pio_mask = UINT_MAX;
5905c6fd2807SJeff Garzik 	dev->mwdma_mask = UINT_MAX;
5906c6fd2807SJeff Garzik 	dev->udma_mask = UINT_MAX;
5907c6fd2807SJeff Garzik }
5908c6fd2807SJeff Garzik 
5909c6fd2807SJeff Garzik /**
59104fb37a25STejun Heo  *	ata_link_init - Initialize an ata_link structure
59114fb37a25STejun Heo  *	@ap: ATA port link is attached to
59124fb37a25STejun Heo  *	@link: Link structure to initialize
59138989805dSTejun Heo  *	@pmp: Port multiplier port number
59144fb37a25STejun Heo  *
59154fb37a25STejun Heo  *	Initialize @link.
59164fb37a25STejun Heo  *
59174fb37a25STejun Heo  *	LOCKING:
59184fb37a25STejun Heo  *	Kernel thread context (may sleep)
59194fb37a25STejun Heo  */
5920fb7fd614STejun Heo void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
59214fb37a25STejun Heo {
59224fb37a25STejun Heo 	int i;
59234fb37a25STejun Heo 
59244fb37a25STejun Heo 	/* clear everything except for devices */
5925d9027470SGwendal Grignou 	memset((void *)link + ATA_LINK_CLEAR_BEGIN, 0,
5926d9027470SGwendal Grignou 	       ATA_LINK_CLEAR_END - ATA_LINK_CLEAR_BEGIN);
59274fb37a25STejun Heo 
59284fb37a25STejun Heo 	link->ap = ap;
59298989805dSTejun Heo 	link->pmp = pmp;
59304fb37a25STejun Heo 	link->active_tag = ATA_TAG_POISON;
59314fb37a25STejun Heo 	link->hw_sata_spd_limit = UINT_MAX;
59324fb37a25STejun Heo 
59334fb37a25STejun Heo 	/* can't use iterator, ap isn't initialized yet */
59344fb37a25STejun Heo 	for (i = 0; i < ATA_MAX_DEVICES; i++) {
59354fb37a25STejun Heo 		struct ata_device *dev = &link->device[i];
59364fb37a25STejun Heo 
59374fb37a25STejun Heo 		dev->link = link;
59384fb37a25STejun Heo 		dev->devno = dev - link->device;
5939110f66d2STejun Heo #ifdef CONFIG_ATA_ACPI
5940110f66d2STejun Heo 		dev->gtf_filter = ata_acpi_gtf_filter;
5941110f66d2STejun Heo #endif
59424fb37a25STejun Heo 		ata_dev_init(dev);
59434fb37a25STejun Heo 	}
59444fb37a25STejun Heo }
59454fb37a25STejun Heo 
59464fb37a25STejun Heo /**
59474fb37a25STejun Heo  *	sata_link_init_spd - Initialize link->sata_spd_limit
59484fb37a25STejun Heo  *	@link: Link to configure sata_spd_limit for
59494fb37a25STejun Heo  *
59504fb37a25STejun Heo  *	Initialize @link->[hw_]sata_spd_limit to the currently
59514fb37a25STejun Heo  *	configured value.
59524fb37a25STejun Heo  *
59534fb37a25STejun Heo  *	LOCKING:
59544fb37a25STejun Heo  *	Kernel thread context (may sleep).
59554fb37a25STejun Heo  *
59564fb37a25STejun Heo  *	RETURNS:
59574fb37a25STejun Heo  *	0 on success, -errno on failure.
59584fb37a25STejun Heo  */
5959fb7fd614STejun Heo int sata_link_init_spd(struct ata_link *link)
59604fb37a25STejun Heo {
596133267325STejun Heo 	u8 spd;
59624fb37a25STejun Heo 	int rc;
59634fb37a25STejun Heo 
5964d127ea7bSTejun Heo 	rc = sata_scr_read(link, SCR_CONTROL, &link->saved_scontrol);
59654fb37a25STejun Heo 	if (rc)
59664fb37a25STejun Heo 		return rc;
59674fb37a25STejun Heo 
5968d127ea7bSTejun Heo 	spd = (link->saved_scontrol >> 4) & 0xf;
59694fb37a25STejun Heo 	if (spd)
59704fb37a25STejun Heo 		link->hw_sata_spd_limit &= (1 << spd) - 1;
59714fb37a25STejun Heo 
597205944bdfSTejun Heo 	ata_force_link_limits(link);
597333267325STejun Heo 
59744fb37a25STejun Heo 	link->sata_spd_limit = link->hw_sata_spd_limit;
59754fb37a25STejun Heo 
59764fb37a25STejun Heo 	return 0;
59774fb37a25STejun Heo }
59784fb37a25STejun Heo 
59794fb37a25STejun Heo /**
5980f3187195STejun Heo  *	ata_port_alloc - allocate and initialize basic ATA port resources
5981f3187195STejun Heo  *	@host: ATA host this allocated port belongs to
5982c6fd2807SJeff Garzik  *
5983f3187195STejun Heo  *	Allocate and initialize basic ATA port resources.
5984f3187195STejun Heo  *
5985f3187195STejun Heo  *	RETURNS:
5986f3187195STejun Heo  *	Allocate ATA port on success, NULL on failure.
5987c6fd2807SJeff Garzik  *
5988c6fd2807SJeff Garzik  *	LOCKING:
5989f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5990c6fd2807SJeff Garzik  */
5991f3187195STejun Heo struct ata_port *ata_port_alloc(struct ata_host *host)
5992c6fd2807SJeff Garzik {
5993f3187195STejun Heo 	struct ata_port *ap;
5994c6fd2807SJeff Garzik 
5995f3187195STejun Heo 	DPRINTK("ENTER\n");
5996f3187195STejun Heo 
5997f3187195STejun Heo 	ap = kzalloc(sizeof(*ap), GFP_KERNEL);
5998f3187195STejun Heo 	if (!ap)
5999f3187195STejun Heo 		return NULL;
6000f3187195STejun Heo 
60017b3a24c5SMaxime Bizon 	ap->pflags |= ATA_PFLAG_INITIALIZING | ATA_PFLAG_FROZEN;
6002cca3974eSJeff Garzik 	ap->lock = &host->lock;
6003f3187195STejun Heo 	ap->print_id = -1;
6004e628dc99SDavid Milburn 	ap->local_port_no = -1;
6005cca3974eSJeff Garzik 	ap->host = host;
6006f3187195STejun Heo 	ap->dev = host->dev;
6007c6fd2807SJeff Garzik 
6008c6fd2807SJeff Garzik #if defined(ATA_VERBOSE_DEBUG)
6009c6fd2807SJeff Garzik 	/* turn on all debugging levels */
6010c6fd2807SJeff Garzik 	ap->msg_enable = 0x00FF;
6011c6fd2807SJeff Garzik #elif defined(ATA_DEBUG)
6012c6fd2807SJeff Garzik 	ap->msg_enable = ATA_MSG_DRV | ATA_MSG_INFO | ATA_MSG_CTL | ATA_MSG_WARN | ATA_MSG_ERR;
6013c6fd2807SJeff Garzik #else
6014c6fd2807SJeff Garzik 	ap->msg_enable = ATA_MSG_DRV | ATA_MSG_ERR | ATA_MSG_WARN;
6015c6fd2807SJeff Garzik #endif
6016c6fd2807SJeff Garzik 
6017ad72cf98STejun Heo 	mutex_init(&ap->scsi_scan_mutex);
601865f27f38SDavid Howells 	INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
601965f27f38SDavid Howells 	INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
6020c6fd2807SJeff Garzik 	INIT_LIST_HEAD(&ap->eh_done_q);
6021c6fd2807SJeff Garzik 	init_waitqueue_head(&ap->eh_wait_q);
602245fabbb7SElias Oltmanns 	init_completion(&ap->park_req_pending);
6023b93ab338SKees Cook 	timer_setup(&ap->fastdrain_timer, ata_eh_fastdrain_timerfn,
6024b93ab338SKees Cook 		    TIMER_DEFERRABLE);
6025c6fd2807SJeff Garzik 
6026c6fd2807SJeff Garzik 	ap->cbl = ATA_CBL_NONE;
6027c6fd2807SJeff Garzik 
60288989805dSTejun Heo 	ata_link_init(ap, &ap->link, 0);
6029c6fd2807SJeff Garzik 
6030c6fd2807SJeff Garzik #ifdef ATA_IRQ_TRAP
6031c6fd2807SJeff Garzik 	ap->stats.unhandled_irq = 1;
6032c6fd2807SJeff Garzik 	ap->stats.idle_irq = 1;
6033c6fd2807SJeff Garzik #endif
6034270390e1STejun Heo 	ata_sff_port_init(ap);
6035270390e1STejun Heo 
6036c6fd2807SJeff Garzik 	return ap;
6037c6fd2807SJeff Garzik }
6038c6fd2807SJeff Garzik 
60392623c7a5STaras Kondratiuk static void ata_devres_release(struct device *gendev, void *res)
6040f0d36efdSTejun Heo {
6041f0d36efdSTejun Heo 	struct ata_host *host = dev_get_drvdata(gendev);
6042f0d36efdSTejun Heo 	int i;
6043f0d36efdSTejun Heo 
6044f0d36efdSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
6045f0d36efdSTejun Heo 		struct ata_port *ap = host->ports[i];
6046f0d36efdSTejun Heo 
6047ecef7253STejun Heo 		if (!ap)
6048ecef7253STejun Heo 			continue;
6049ecef7253STejun Heo 
60504911487aSTejun Heo 		if (ap->scsi_host)
60511aa506e4STejun Heo 			scsi_host_put(ap->scsi_host);
60521aa506e4STejun Heo 
60532623c7a5STaras Kondratiuk 	}
60542623c7a5STaras Kondratiuk 
60552623c7a5STaras Kondratiuk 	dev_set_drvdata(gendev, NULL);
60562623c7a5STaras Kondratiuk 	ata_host_put(host);
60572623c7a5STaras Kondratiuk }
60582623c7a5STaras Kondratiuk 
60592623c7a5STaras Kondratiuk static void ata_host_release(struct kref *kref)
60602623c7a5STaras Kondratiuk {
60612623c7a5STaras Kondratiuk 	struct ata_host *host = container_of(kref, struct ata_host, kref);
60622623c7a5STaras Kondratiuk 	int i;
60632623c7a5STaras Kondratiuk 
60642623c7a5STaras Kondratiuk 	for (i = 0; i < host->n_ports; i++) {
60652623c7a5STaras Kondratiuk 		struct ata_port *ap = host->ports[i];
60662623c7a5STaras Kondratiuk 
6067633273a3STejun Heo 		kfree(ap->pmp_link);
6068b1c72916STejun Heo 		kfree(ap->slave_link);
60694911487aSTejun Heo 		kfree(ap);
60701aa506e4STejun Heo 		host->ports[i] = NULL;
60711aa506e4STejun Heo 	}
60722623c7a5STaras Kondratiuk 	kfree(host);
60732623c7a5STaras Kondratiuk }
60741aa506e4STejun Heo 
60752623c7a5STaras Kondratiuk void ata_host_get(struct ata_host *host)
60762623c7a5STaras Kondratiuk {
60772623c7a5STaras Kondratiuk 	kref_get(&host->kref);
60782623c7a5STaras Kondratiuk }
60792623c7a5STaras Kondratiuk 
60802623c7a5STaras Kondratiuk void ata_host_put(struct ata_host *host)
60812623c7a5STaras Kondratiuk {
60822623c7a5STaras Kondratiuk 	kref_put(&host->kref, ata_host_release);
6083f0d36efdSTejun Heo }
6084f0d36efdSTejun Heo 
6085c6fd2807SJeff Garzik /**
6086f3187195STejun Heo  *	ata_host_alloc - allocate and init basic ATA host resources
6087f3187195STejun Heo  *	@dev: generic device this host is associated with
6088f3187195STejun Heo  *	@max_ports: maximum number of ATA ports associated with this host
6089f3187195STejun Heo  *
6090f3187195STejun Heo  *	Allocate and initialize basic ATA host resources.  LLD calls
6091f3187195STejun Heo  *	this function to allocate a host, initializes it fully and
6092f3187195STejun Heo  *	attaches it using ata_host_register().
6093f3187195STejun Heo  *
6094f3187195STejun Heo  *	@max_ports ports are allocated and host->n_ports is
6095f3187195STejun Heo  *	initialized to @max_ports.  The caller is allowed to decrease
6096f3187195STejun Heo  *	host->n_ports before calling ata_host_register().  The unused
6097f3187195STejun Heo  *	ports will be automatically freed on registration.
6098f3187195STejun Heo  *
6099f3187195STejun Heo  *	RETURNS:
6100f3187195STejun Heo  *	Allocate ATA host on success, NULL on failure.
6101f3187195STejun Heo  *
6102f3187195STejun Heo  *	LOCKING:
6103f3187195STejun Heo  *	Inherited from calling layer (may sleep).
6104f3187195STejun Heo  */
6105f3187195STejun Heo struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
6106f3187195STejun Heo {
6107f3187195STejun Heo 	struct ata_host *host;
6108f3187195STejun Heo 	size_t sz;
6109f3187195STejun Heo 	int i;
61102623c7a5STaras Kondratiuk 	void *dr;
6111f3187195STejun Heo 
6112f3187195STejun Heo 	DPRINTK("ENTER\n");
6113f3187195STejun Heo 
6114f3187195STejun Heo 	/* alloc a container for our list of ATA ports (buses) */
6115f3187195STejun Heo 	sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
61162623c7a5STaras Kondratiuk 	host = kzalloc(sz, GFP_KERNEL);
6117f3187195STejun Heo 	if (!host)
61182623c7a5STaras Kondratiuk 		return NULL;
61192623c7a5STaras Kondratiuk 
6120f3187195STejun Heo 	if (!devres_open_group(dev, NULL, GFP_KERNEL))
6121dafd6c49SColin Ian King 		goto err_free;
6122f3187195STejun Heo 
61232623c7a5STaras Kondratiuk 	dr = devres_alloc(ata_devres_release, 0, GFP_KERNEL);
61242623c7a5STaras Kondratiuk 	if (!dr)
6125f3187195STejun Heo 		goto err_out;
6126f3187195STejun Heo 
61272623c7a5STaras Kondratiuk 	devres_add(dev, dr);
6128f3187195STejun Heo 	dev_set_drvdata(dev, host);
6129f3187195STejun Heo 
6130f3187195STejun Heo 	spin_lock_init(&host->lock);
6131c0c362b6STejun Heo 	mutex_init(&host->eh_mutex);
6132f3187195STejun Heo 	host->dev = dev;
6133f3187195STejun Heo 	host->n_ports = max_ports;
61342623c7a5STaras Kondratiuk 	kref_init(&host->kref);
6135f3187195STejun Heo 
6136f3187195STejun Heo 	/* allocate ports bound to this host */
6137f3187195STejun Heo 	for (i = 0; i < max_ports; i++) {
6138f3187195STejun Heo 		struct ata_port *ap;
6139f3187195STejun Heo 
6140f3187195STejun Heo 		ap = ata_port_alloc(host);
6141f3187195STejun Heo 		if (!ap)
6142f3187195STejun Heo 			goto err_out;
6143f3187195STejun Heo 
6144f3187195STejun Heo 		ap->port_no = i;
6145f3187195STejun Heo 		host->ports[i] = ap;
6146f3187195STejun Heo 	}
6147f3187195STejun Heo 
6148f3187195STejun Heo 	devres_remove_group(dev, NULL);
6149f3187195STejun Heo 	return host;
6150f3187195STejun Heo 
6151f3187195STejun Heo  err_out:
6152f3187195STejun Heo 	devres_release_group(dev, NULL);
6153dafd6c49SColin Ian King  err_free:
6154dafd6c49SColin Ian King 	kfree(host);
6155f3187195STejun Heo 	return NULL;
6156f3187195STejun Heo }
6157f3187195STejun Heo 
6158f3187195STejun Heo /**
6159f5cda257STejun Heo  *	ata_host_alloc_pinfo - alloc host and init with port_info array
6160f5cda257STejun Heo  *	@dev: generic device this host is associated with
6161f5cda257STejun Heo  *	@ppi: array of ATA port_info to initialize host with
6162f5cda257STejun Heo  *	@n_ports: number of ATA ports attached to this host
6163f5cda257STejun Heo  *
6164f5cda257STejun Heo  *	Allocate ATA host and initialize with info from @ppi.  If NULL
6165f5cda257STejun Heo  *	terminated, @ppi may contain fewer entries than @n_ports.  The
6166f5cda257STejun Heo  *	last entry will be used for the remaining ports.
6167f5cda257STejun Heo  *
6168f5cda257STejun Heo  *	RETURNS:
6169f5cda257STejun Heo  *	Allocate ATA host on success, NULL on failure.
6170f5cda257STejun Heo  *
6171f5cda257STejun Heo  *	LOCKING:
6172f5cda257STejun Heo  *	Inherited from calling layer (may sleep).
6173f5cda257STejun Heo  */
6174f5cda257STejun Heo struct ata_host *ata_host_alloc_pinfo(struct device *dev,
6175f5cda257STejun Heo 				      const struct ata_port_info * const * ppi,
6176f5cda257STejun Heo 				      int n_ports)
6177f5cda257STejun Heo {
6178f5cda257STejun Heo 	const struct ata_port_info *pi;
6179f5cda257STejun Heo 	struct ata_host *host;
6180f5cda257STejun Heo 	int i, j;
6181f5cda257STejun Heo 
6182f5cda257STejun Heo 	host = ata_host_alloc(dev, n_ports);
6183f5cda257STejun Heo 	if (!host)
6184f5cda257STejun Heo 		return NULL;
6185f5cda257STejun Heo 
6186f5cda257STejun Heo 	for (i = 0, j = 0, pi = NULL; i < host->n_ports; i++) {
6187f5cda257STejun Heo 		struct ata_port *ap = host->ports[i];
6188f5cda257STejun Heo 
6189f5cda257STejun Heo 		if (ppi[j])
6190f5cda257STejun Heo 			pi = ppi[j++];
6191f5cda257STejun Heo 
6192f5cda257STejun Heo 		ap->pio_mask = pi->pio_mask;
6193f5cda257STejun Heo 		ap->mwdma_mask = pi->mwdma_mask;
6194f5cda257STejun Heo 		ap->udma_mask = pi->udma_mask;
6195f5cda257STejun Heo 		ap->flags |= pi->flags;
61960c88758bSTejun Heo 		ap->link.flags |= pi->link_flags;
6197f5cda257STejun Heo 		ap->ops = pi->port_ops;
6198f5cda257STejun Heo 
6199f5cda257STejun Heo 		if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
6200f5cda257STejun Heo 			host->ops = pi->port_ops;
6201f5cda257STejun Heo 	}
6202f5cda257STejun Heo 
6203f5cda257STejun Heo 	return host;
6204f5cda257STejun Heo }
6205f5cda257STejun Heo 
6206b1c72916STejun Heo /**
6207b1c72916STejun Heo  *	ata_slave_link_init - initialize slave link
6208b1c72916STejun Heo  *	@ap: port to initialize slave link for
6209b1c72916STejun Heo  *
6210b1c72916STejun Heo  *	Create and initialize slave link for @ap.  This enables slave
6211b1c72916STejun Heo  *	link handling on the port.
6212b1c72916STejun Heo  *
6213b1c72916STejun Heo  *	In libata, a port contains links and a link contains devices.
6214b1c72916STejun Heo  *	There is single host link but if a PMP is attached to it,
6215b1c72916STejun Heo  *	there can be multiple fan-out links.  On SATA, there's usually
6216b1c72916STejun Heo  *	a single device connected to a link but PATA and SATA
6217b1c72916STejun Heo  *	controllers emulating TF based interface can have two - master
6218b1c72916STejun Heo  *	and slave.
6219b1c72916STejun Heo  *
6220b1c72916STejun Heo  *	However, there are a few controllers which don't fit into this
6221b1c72916STejun Heo  *	abstraction too well - SATA controllers which emulate TF
6222b1c72916STejun Heo  *	interface with both master and slave devices but also have
6223b1c72916STejun Heo  *	separate SCR register sets for each device.  These controllers
6224b1c72916STejun Heo  *	need separate links for physical link handling
6225b1c72916STejun Heo  *	(e.g. onlineness, link speed) but should be treated like a
6226b1c72916STejun Heo  *	traditional M/S controller for everything else (e.g. command
6227b1c72916STejun Heo  *	issue, softreset).
6228b1c72916STejun Heo  *
6229b1c72916STejun Heo  *	slave_link is libata's way of handling this class of
6230b1c72916STejun Heo  *	controllers without impacting core layer too much.  For
6231b1c72916STejun Heo  *	anything other than physical link handling, the default host
6232b1c72916STejun Heo  *	link is used for both master and slave.  For physical link
6233b1c72916STejun Heo  *	handling, separate @ap->slave_link is used.  All dirty details
6234b1c72916STejun Heo  *	are implemented inside libata core layer.  From LLD's POV, the
6235b1c72916STejun Heo  *	only difference is that prereset, hardreset and postreset are
6236b1c72916STejun Heo  *	called once more for the slave link, so the reset sequence
6237b1c72916STejun Heo  *	looks like the following.
6238b1c72916STejun Heo  *
6239b1c72916STejun Heo  *	prereset(M) -> prereset(S) -> hardreset(M) -> hardreset(S) ->
6240b1c72916STejun Heo  *	softreset(M) -> postreset(M) -> postreset(S)
6241b1c72916STejun Heo  *
6242b1c72916STejun Heo  *	Note that softreset is called only for the master.  Softreset
6243b1c72916STejun Heo  *	resets both M/S by definition, so SRST on master should handle
6244b1c72916STejun Heo  *	both (the standard method will work just fine).
6245b1c72916STejun Heo  *
6246b1c72916STejun Heo  *	LOCKING:
6247b1c72916STejun Heo  *	Should be called before host is registered.
6248b1c72916STejun Heo  *
6249b1c72916STejun Heo  *	RETURNS:
6250b1c72916STejun Heo  *	0 on success, -errno on failure.
6251b1c72916STejun Heo  */
6252b1c72916STejun Heo int ata_slave_link_init(struct ata_port *ap)
6253b1c72916STejun Heo {
6254b1c72916STejun Heo 	struct ata_link *link;
6255b1c72916STejun Heo 
6256b1c72916STejun Heo 	WARN_ON(ap->slave_link);
6257b1c72916STejun Heo 	WARN_ON(ap->flags & ATA_FLAG_PMP);
6258b1c72916STejun Heo 
6259b1c72916STejun Heo 	link = kzalloc(sizeof(*link), GFP_KERNEL);
6260b1c72916STejun Heo 	if (!link)
6261b1c72916STejun Heo 		return -ENOMEM;
6262b1c72916STejun Heo 
6263b1c72916STejun Heo 	ata_link_init(ap, link, 1);
6264b1c72916STejun Heo 	ap->slave_link = link;
6265b1c72916STejun Heo 	return 0;
6266b1c72916STejun Heo }
6267b1c72916STejun Heo 
626832ebbc0cSTejun Heo static void ata_host_stop(struct device *gendev, void *res)
626932ebbc0cSTejun Heo {
627032ebbc0cSTejun Heo 	struct ata_host *host = dev_get_drvdata(gendev);
627132ebbc0cSTejun Heo 	int i;
627232ebbc0cSTejun Heo 
627332ebbc0cSTejun Heo 	WARN_ON(!(host->flags & ATA_HOST_STARTED));
627432ebbc0cSTejun Heo 
627532ebbc0cSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
627632ebbc0cSTejun Heo 		struct ata_port *ap = host->ports[i];
627732ebbc0cSTejun Heo 
627832ebbc0cSTejun Heo 		if (ap->ops->port_stop)
627932ebbc0cSTejun Heo 			ap->ops->port_stop(ap);
628032ebbc0cSTejun Heo 	}
628132ebbc0cSTejun Heo 
628232ebbc0cSTejun Heo 	if (host->ops->host_stop)
628332ebbc0cSTejun Heo 		host->ops->host_stop(host);
628432ebbc0cSTejun Heo }
628532ebbc0cSTejun Heo 
6286f5cda257STejun Heo /**
6287029cfd6bSTejun Heo  *	ata_finalize_port_ops - finalize ata_port_operations
6288029cfd6bSTejun Heo  *	@ops: ata_port_operations to finalize
6289029cfd6bSTejun Heo  *
6290029cfd6bSTejun Heo  *	An ata_port_operations can inherit from another ops and that
6291029cfd6bSTejun Heo  *	ops can again inherit from another.  This can go on as many
6292029cfd6bSTejun Heo  *	times as necessary as long as there is no loop in the
6293029cfd6bSTejun Heo  *	inheritance chain.
6294029cfd6bSTejun Heo  *
6295029cfd6bSTejun Heo  *	Ops tables are finalized when the host is started.  NULL or
6296029cfd6bSTejun Heo  *	unspecified entries are inherited from the closet ancestor
6297029cfd6bSTejun Heo  *	which has the method and the entry is populated with it.
6298029cfd6bSTejun Heo  *	After finalization, the ops table directly points to all the
6299029cfd6bSTejun Heo  *	methods and ->inherits is no longer necessary and cleared.
6300029cfd6bSTejun Heo  *
6301029cfd6bSTejun Heo  *	Using ATA_OP_NULL, inheriting ops can force a method to NULL.
6302029cfd6bSTejun Heo  *
6303029cfd6bSTejun Heo  *	LOCKING:
6304029cfd6bSTejun Heo  *	None.
6305029cfd6bSTejun Heo  */
6306029cfd6bSTejun Heo static void ata_finalize_port_ops(struct ata_port_operations *ops)
6307029cfd6bSTejun Heo {
63082da67659SPradeep Singh Rautela 	static DEFINE_SPINLOCK(lock);
6309029cfd6bSTejun Heo 	const struct ata_port_operations *cur;
6310029cfd6bSTejun Heo 	void **begin = (void **)ops;
6311029cfd6bSTejun Heo 	void **end = (void **)&ops->inherits;
6312029cfd6bSTejun Heo 	void **pp;
6313029cfd6bSTejun Heo 
6314029cfd6bSTejun Heo 	if (!ops || !ops->inherits)
6315029cfd6bSTejun Heo 		return;
6316029cfd6bSTejun Heo 
6317029cfd6bSTejun Heo 	spin_lock(&lock);
6318029cfd6bSTejun Heo 
6319029cfd6bSTejun Heo 	for (cur = ops->inherits; cur; cur = cur->inherits) {
6320029cfd6bSTejun Heo 		void **inherit = (void **)cur;
6321029cfd6bSTejun Heo 
6322029cfd6bSTejun Heo 		for (pp = begin; pp < end; pp++, inherit++)
6323029cfd6bSTejun Heo 			if (!*pp)
6324029cfd6bSTejun Heo 				*pp = *inherit;
6325029cfd6bSTejun Heo 	}
6326029cfd6bSTejun Heo 
6327029cfd6bSTejun Heo 	for (pp = begin; pp < end; pp++)
6328029cfd6bSTejun Heo 		if (IS_ERR(*pp))
6329029cfd6bSTejun Heo 			*pp = NULL;
6330029cfd6bSTejun Heo 
6331029cfd6bSTejun Heo 	ops->inherits = NULL;
6332029cfd6bSTejun Heo 
6333029cfd6bSTejun Heo 	spin_unlock(&lock);
6334029cfd6bSTejun Heo }
6335029cfd6bSTejun Heo 
6336029cfd6bSTejun Heo /**
6337ecef7253STejun Heo  *	ata_host_start - start and freeze ports of an ATA host
6338ecef7253STejun Heo  *	@host: ATA host to start ports for
6339ecef7253STejun Heo  *
6340ecef7253STejun Heo  *	Start and then freeze ports of @host.  Started status is
6341ecef7253STejun Heo  *	recorded in host->flags, so this function can be called
6342ecef7253STejun Heo  *	multiple times.  Ports are guaranteed to get started only
6343f3187195STejun Heo  *	once.  If host->ops isn't initialized yet, its set to the
6344f3187195STejun Heo  *	first non-dummy port ops.
6345ecef7253STejun Heo  *
6346ecef7253STejun Heo  *	LOCKING:
6347ecef7253STejun Heo  *	Inherited from calling layer (may sleep).
6348ecef7253STejun Heo  *
6349ecef7253STejun Heo  *	RETURNS:
6350ecef7253STejun Heo  *	0 if all ports are started successfully, -errno otherwise.
6351ecef7253STejun Heo  */
6352ecef7253STejun Heo int ata_host_start(struct ata_host *host)
6353ecef7253STejun Heo {
635432ebbc0cSTejun Heo 	int have_stop = 0;
635532ebbc0cSTejun Heo 	void *start_dr = NULL;
6356ecef7253STejun Heo 	int i, rc;
6357ecef7253STejun Heo 
6358ecef7253STejun Heo 	if (host->flags & ATA_HOST_STARTED)
6359ecef7253STejun Heo 		return 0;
6360ecef7253STejun Heo 
6361029cfd6bSTejun Heo 	ata_finalize_port_ops(host->ops);
6362029cfd6bSTejun Heo 
6363ecef7253STejun Heo 	for (i = 0; i < host->n_ports; i++) {
6364ecef7253STejun Heo 		struct ata_port *ap = host->ports[i];
6365ecef7253STejun Heo 
6366029cfd6bSTejun Heo 		ata_finalize_port_ops(ap->ops);
6367029cfd6bSTejun Heo 
6368f3187195STejun Heo 		if (!host->ops && !ata_port_is_dummy(ap))
6369f3187195STejun Heo 			host->ops = ap->ops;
6370f3187195STejun Heo 
637132ebbc0cSTejun Heo 		if (ap->ops->port_stop)
637232ebbc0cSTejun Heo 			have_stop = 1;
637332ebbc0cSTejun Heo 	}
637432ebbc0cSTejun Heo 
637532ebbc0cSTejun Heo 	if (host->ops->host_stop)
637632ebbc0cSTejun Heo 		have_stop = 1;
637732ebbc0cSTejun Heo 
637832ebbc0cSTejun Heo 	if (have_stop) {
637932ebbc0cSTejun Heo 		start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
638032ebbc0cSTejun Heo 		if (!start_dr)
638132ebbc0cSTejun Heo 			return -ENOMEM;
638232ebbc0cSTejun Heo 	}
638332ebbc0cSTejun Heo 
638432ebbc0cSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
638532ebbc0cSTejun Heo 		struct ata_port *ap = host->ports[i];
638632ebbc0cSTejun Heo 
6387ecef7253STejun Heo 		if (ap->ops->port_start) {
6388ecef7253STejun Heo 			rc = ap->ops->port_start(ap);
6389ecef7253STejun Heo 			if (rc) {
63900f9fe9b7SAlan Cox 				if (rc != -ENODEV)
6391a44fec1fSJoe Perches 					dev_err(host->dev,
6392a44fec1fSJoe Perches 						"failed to start port %d (errno=%d)\n",
6393a44fec1fSJoe Perches 						i, rc);
6394ecef7253STejun Heo 				goto err_out;
6395ecef7253STejun Heo 			}
6396ecef7253STejun Heo 		}
6397ecef7253STejun Heo 		ata_eh_freeze_port(ap);
6398ecef7253STejun Heo 	}
6399ecef7253STejun Heo 
640032ebbc0cSTejun Heo 	if (start_dr)
640132ebbc0cSTejun Heo 		devres_add(host->dev, start_dr);
6402ecef7253STejun Heo 	host->flags |= ATA_HOST_STARTED;
6403ecef7253STejun Heo 	return 0;
6404ecef7253STejun Heo 
6405ecef7253STejun Heo  err_out:
6406ecef7253STejun Heo 	while (--i >= 0) {
6407ecef7253STejun Heo 		struct ata_port *ap = host->ports[i];
6408ecef7253STejun Heo 
6409ecef7253STejun Heo 		if (ap->ops->port_stop)
6410ecef7253STejun Heo 			ap->ops->port_stop(ap);
6411ecef7253STejun Heo 	}
641232ebbc0cSTejun Heo 	devres_free(start_dr);
6413ecef7253STejun Heo 	return rc;
6414ecef7253STejun Heo }
6415ecef7253STejun Heo 
6416ecef7253STejun Heo /**
64178d8e7d13SDan Williams  *	ata_sas_host_init - Initialize a host struct for sas (ipr, libsas)
6418cca3974eSJeff Garzik  *	@host:	host to initialize
6419cca3974eSJeff Garzik  *	@dev:	device host is attached to
6420c6fd2807SJeff Garzik  *	@ops:	port_ops
6421c6fd2807SJeff Garzik  *
6422c6fd2807SJeff Garzik  */
6423cca3974eSJeff Garzik void ata_host_init(struct ata_host *host, struct device *dev,
64248d8e7d13SDan Williams 		   struct ata_port_operations *ops)
6425c6fd2807SJeff Garzik {
6426cca3974eSJeff Garzik 	spin_lock_init(&host->lock);
6427c0c362b6STejun Heo 	mutex_init(&host->eh_mutex);
64281a112d10STejun Heo 	host->n_tags = ATA_MAX_QUEUE - 1;
6429cca3974eSJeff Garzik 	host->dev = dev;
6430cca3974eSJeff Garzik 	host->ops = ops;
6431c6fd2807SJeff Garzik }
6432c6fd2807SJeff Garzik 
64339508a66fSDan Williams void __ata_port_probe(struct ata_port *ap)
643479318057SArjan van de Ven {
643579318057SArjan van de Ven 	struct ata_eh_info *ehi = &ap->link.eh_info;
643679318057SArjan van de Ven 	unsigned long flags;
643779318057SArjan van de Ven 
643879318057SArjan van de Ven 	/* kick EH for boot probing */
643979318057SArjan van de Ven 	spin_lock_irqsave(ap->lock, flags);
644079318057SArjan van de Ven 
644179318057SArjan van de Ven 	ehi->probe_mask |= ATA_ALL_DEVICES;
64426b7ae954STejun Heo 	ehi->action |= ATA_EH_RESET;
644379318057SArjan van de Ven 	ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
644479318057SArjan van de Ven 
644579318057SArjan van de Ven 	ap->pflags &= ~ATA_PFLAG_INITIALIZING;
644679318057SArjan van de Ven 	ap->pflags |= ATA_PFLAG_LOADING;
644779318057SArjan van de Ven 	ata_port_schedule_eh(ap);
644879318057SArjan van de Ven 
644979318057SArjan van de Ven 	spin_unlock_irqrestore(ap->lock, flags);
64509508a66fSDan Williams }
645179318057SArjan van de Ven 
64529508a66fSDan Williams int ata_port_probe(struct ata_port *ap)
64539508a66fSDan Williams {
64549508a66fSDan Williams 	int rc = 0;
64559508a66fSDan Williams 
64569508a66fSDan Williams 	if (ap->ops->error_handler) {
64579508a66fSDan Williams 		__ata_port_probe(ap);
645879318057SArjan van de Ven 		ata_port_wait_eh(ap);
645979318057SArjan van de Ven 	} else {
646079318057SArjan van de Ven 		DPRINTK("ata%u: bus probe begin\n", ap->print_id);
646179318057SArjan van de Ven 		rc = ata_bus_probe(ap);
646279318057SArjan van de Ven 		DPRINTK("ata%u: bus probe end\n", ap->print_id);
6463238c9cf9SJames Bottomley 	}
6464238c9cf9SJames Bottomley 	return rc;
6465238c9cf9SJames Bottomley }
646679318057SArjan van de Ven 
6467238c9cf9SJames Bottomley 
6468238c9cf9SJames Bottomley static void async_port_probe(void *data, async_cookie_t cookie)
6469238c9cf9SJames Bottomley {
6470238c9cf9SJames Bottomley 	struct ata_port *ap = data;
6471238c9cf9SJames Bottomley 
6472238c9cf9SJames Bottomley 	/*
6473238c9cf9SJames Bottomley 	 * If we're not allowed to scan this host in parallel,
6474238c9cf9SJames Bottomley 	 * we need to wait until all previous scans have completed
6475238c9cf9SJames Bottomley 	 * before going further.
6476238c9cf9SJames Bottomley 	 * Jeff Garzik says this is only within a controller, so we
6477238c9cf9SJames Bottomley 	 * don't need to wait for port 0, only for later ports.
647879318057SArjan van de Ven 	 */
6479238c9cf9SJames Bottomley 	if (!(ap->host->flags & ATA_HOST_PARALLEL_SCAN) && ap->port_no != 0)
6480238c9cf9SJames Bottomley 		async_synchronize_cookie(cookie);
6481238c9cf9SJames Bottomley 
6482238c9cf9SJames Bottomley 	(void)ata_port_probe(ap);
6483f29d3b23SArjan van de Ven 
6484f29d3b23SArjan van de Ven 	/* in order to keep device order, we need to synchronize at this point */
6485f29d3b23SArjan van de Ven 	async_synchronize_cookie(cookie);
6486f29d3b23SArjan van de Ven 
6487f29d3b23SArjan van de Ven 	ata_scsi_scan_host(ap, 1);
648879318057SArjan van de Ven }
6489238c9cf9SJames Bottomley 
6490c6fd2807SJeff Garzik /**
6491f3187195STejun Heo  *	ata_host_register - register initialized ATA host
6492f3187195STejun Heo  *	@host: ATA host to register
6493f3187195STejun Heo  *	@sht: template for SCSI host
6494c6fd2807SJeff Garzik  *
6495f3187195STejun Heo  *	Register initialized ATA host.  @host is allocated using
6496f3187195STejun Heo  *	ata_host_alloc() and fully initialized by LLD.  This function
6497f3187195STejun Heo  *	starts ports, registers @host with ATA and SCSI layers and
6498f3187195STejun Heo  *	probe registered devices.
6499c6fd2807SJeff Garzik  *
6500c6fd2807SJeff Garzik  *	LOCKING:
6501f3187195STejun Heo  *	Inherited from calling layer (may sleep).
6502c6fd2807SJeff Garzik  *
6503c6fd2807SJeff Garzik  *	RETURNS:
6504f3187195STejun Heo  *	0 on success, -errno otherwise.
6505c6fd2807SJeff Garzik  */
6506f3187195STejun Heo int ata_host_register(struct ata_host *host, struct scsi_host_template *sht)
6507c6fd2807SJeff Garzik {
6508f3187195STejun Heo 	int i, rc;
6509c6fd2807SJeff Garzik 
65101a112d10STejun Heo 	host->n_tags = clamp(sht->can_queue, 1, ATA_MAX_QUEUE - 1);
65111871ee13SKevin Hao 
6512f3187195STejun Heo 	/* host must have been started */
6513f3187195STejun Heo 	if (!(host->flags & ATA_HOST_STARTED)) {
6514a44fec1fSJoe Perches 		dev_err(host->dev, "BUG: trying to register unstarted host\n");
6515f3187195STejun Heo 		WARN_ON(1);
6516f3187195STejun Heo 		return -EINVAL;
651702f076aaSAlan Cox 	}
6518f0d36efdSTejun Heo 
6519f3187195STejun Heo 	/* Blow away unused ports.  This happens when LLD can't
6520f3187195STejun Heo 	 * determine the exact number of ports to allocate at
6521f3187195STejun Heo 	 * allocation time.
6522f3187195STejun Heo 	 */
6523f3187195STejun Heo 	for (i = host->n_ports; host->ports[i]; i++)
6524f3187195STejun Heo 		kfree(host->ports[i]);
6525f0d36efdSTejun Heo 
6526f3187195STejun Heo 	/* give ports names and add SCSI hosts */
6527e628dc99SDavid Milburn 	for (i = 0; i < host->n_ports; i++) {
652885d6725bSDan Williams 		host->ports[i]->print_id = atomic_inc_return(&ata_print_id);
6529e628dc99SDavid Milburn 		host->ports[i]->local_port_no = i + 1;
6530e628dc99SDavid Milburn 	}
6531d9027470SGwendal Grignou 
6532d9027470SGwendal Grignou 	/* Create associated sysfs transport objects  */
6533d9027470SGwendal Grignou 	for (i = 0; i < host->n_ports; i++) {
6534d9027470SGwendal Grignou 		rc = ata_tport_add(host->dev,host->ports[i]);
6535d9027470SGwendal Grignou 		if (rc) {
6536d9027470SGwendal Grignou 			goto err_tadd;
6537d9027470SGwendal Grignou 		}
6538d9027470SGwendal Grignou 	}
6539d9027470SGwendal Grignou 
6540f3187195STejun Heo 	rc = ata_scsi_add_hosts(host, sht);
6541ecef7253STejun Heo 	if (rc)
6542d9027470SGwendal Grignou 		goto err_tadd;
6543ecef7253STejun Heo 
6544f3187195STejun Heo 	/* set cable, sata_spd_limit and report */
6545cca3974eSJeff Garzik 	for (i = 0; i < host->n_ports; i++) {
6546cca3974eSJeff Garzik 		struct ata_port *ap = host->ports[i];
6547f3187195STejun Heo 		unsigned long xfer_mask;
6548f3187195STejun Heo 
6549f3187195STejun Heo 		/* set SATA cable type if still unset */
6550f3187195STejun Heo 		if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
6551f3187195STejun Heo 			ap->cbl = ATA_CBL_SATA;
6552c6fd2807SJeff Garzik 
6553c6fd2807SJeff Garzik 		/* init sata_spd_limit to the current value */
65544fb37a25STejun Heo 		sata_link_init_spd(&ap->link);
6555b1c72916STejun Heo 		if (ap->slave_link)
6556b1c72916STejun Heo 			sata_link_init_spd(ap->slave_link);
6557c6fd2807SJeff Garzik 
6558cbcdd875STejun Heo 		/* print per-port info to dmesg */
6559f3187195STejun Heo 		xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
6560f3187195STejun Heo 					      ap->udma_mask);
6561f3187195STejun Heo 
6562abf6e8edSTejun Heo 		if (!ata_port_is_dummy(ap)) {
6563a9a79dfeSJoe Perches 			ata_port_info(ap, "%cATA max %s %s\n",
6564a16abc0bSTejun Heo 				      (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
6565f3187195STejun Heo 				      ata_mode_string(xfer_mask),
6566cbcdd875STejun Heo 				      ap->link.eh_info.desc);
6567abf6e8edSTejun Heo 			ata_ehi_clear_desc(&ap->link.eh_info);
6568abf6e8edSTejun Heo 		} else
6569a9a79dfeSJoe Perches 			ata_port_info(ap, "DUMMY\n");
6570c6fd2807SJeff Garzik 	}
6571c6fd2807SJeff Garzik 
6572f6005354SVegard Nossum 	/* perform each probe asynchronously */
6573f3187195STejun Heo 	for (i = 0; i < host->n_ports; i++) {
6574f3187195STejun Heo 		struct ata_port *ap = host->ports[i];
657579318057SArjan van de Ven 		async_schedule(async_port_probe, ap);
6576c6fd2807SJeff Garzik 	}
6577c6fd2807SJeff Garzik 
6578f3187195STejun Heo 	return 0;
6579d9027470SGwendal Grignou 
6580d9027470SGwendal Grignou  err_tadd:
6581d9027470SGwendal Grignou 	while (--i >= 0) {
6582d9027470SGwendal Grignou 		ata_tport_delete(host->ports[i]);
6583d9027470SGwendal Grignou 	}
6584d9027470SGwendal Grignou 	return rc;
6585d9027470SGwendal Grignou 
6586f3187195STejun Heo }
6587f3187195STejun Heo 
6588f3187195STejun Heo /**
6589f5cda257STejun Heo  *	ata_host_activate - start host, request IRQ and register it
6590f5cda257STejun Heo  *	@host: target ATA host
6591f5cda257STejun Heo  *	@irq: IRQ to request
6592f5cda257STejun Heo  *	@irq_handler: irq_handler used when requesting IRQ
6593f5cda257STejun Heo  *	@irq_flags: irq_flags used when requesting IRQ
6594f5cda257STejun Heo  *	@sht: scsi_host_template to use when registering the host
6595f5cda257STejun Heo  *
6596f5cda257STejun Heo  *	After allocating an ATA host and initializing it, most libata
6597f5cda257STejun Heo  *	LLDs perform three steps to activate the host - start host,
6598c9b5560aSMasanari Iida  *	request IRQ and register it.  This helper takes necessary
6599f5cda257STejun Heo  *	arguments and performs the three steps in one go.
6600f5cda257STejun Heo  *
66013d46b2e2SPaul Mundt  *	An invalid IRQ skips the IRQ registration and expects the host to
66023d46b2e2SPaul Mundt  *	have set polling mode on the port. In this case, @irq_handler
66033d46b2e2SPaul Mundt  *	should be NULL.
66043d46b2e2SPaul Mundt  *
6605f5cda257STejun Heo  *	LOCKING:
6606f5cda257STejun Heo  *	Inherited from calling layer (may sleep).
6607f5cda257STejun Heo  *
6608f5cda257STejun Heo  *	RETURNS:
6609f5cda257STejun Heo  *	0 on success, -errno otherwise.
6610f5cda257STejun Heo  */
6611f5cda257STejun Heo int ata_host_activate(struct ata_host *host, int irq,
6612f5cda257STejun Heo 		      irq_handler_t irq_handler, unsigned long irq_flags,
6613f5cda257STejun Heo 		      struct scsi_host_template *sht)
6614f5cda257STejun Heo {
6615cbcdd875STejun Heo 	int i, rc;
66167e22c002SHeiner Kallweit 	char *irq_desc;
6617f5cda257STejun Heo 
6618f5cda257STejun Heo 	rc = ata_host_start(host);
6619f5cda257STejun Heo 	if (rc)
6620f5cda257STejun Heo 		return rc;
6621f5cda257STejun Heo 
66223d46b2e2SPaul Mundt 	/* Special case for polling mode */
66233d46b2e2SPaul Mundt 	if (!irq) {
66243d46b2e2SPaul Mundt 		WARN_ON(irq_handler);
66253d46b2e2SPaul Mundt 		return ata_host_register(host, sht);
66263d46b2e2SPaul Mundt 	}
66273d46b2e2SPaul Mundt 
66287e22c002SHeiner Kallweit 	irq_desc = devm_kasprintf(host->dev, GFP_KERNEL, "%s[%s]",
66297e22c002SHeiner Kallweit 				  dev_driver_string(host->dev),
66307e22c002SHeiner Kallweit 				  dev_name(host->dev));
66317e22c002SHeiner Kallweit 	if (!irq_desc)
66327e22c002SHeiner Kallweit 		return -ENOMEM;
66337e22c002SHeiner Kallweit 
6634f5cda257STejun Heo 	rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
66357e22c002SHeiner Kallweit 			      irq_desc, host);
6636f5cda257STejun Heo 	if (rc)
6637f5cda257STejun Heo 		return rc;
6638f5cda257STejun Heo 
6639cbcdd875STejun Heo 	for (i = 0; i < host->n_ports; i++)
6640cbcdd875STejun Heo 		ata_port_desc(host->ports[i], "irq %d", irq);
66414031826bSTejun Heo 
6642f5cda257STejun Heo 	rc = ata_host_register(host, sht);
6643f5cda257STejun Heo 	/* if failed, just free the IRQ and leave ports alone */
6644f5cda257STejun Heo 	if (rc)
6645f5cda257STejun Heo 		devm_free_irq(host->dev, irq, host);
6646f5cda257STejun Heo 
6647f5cda257STejun Heo 	return rc;
6648f5cda257STejun Heo }
6649f5cda257STejun Heo 
6650f5cda257STejun Heo /**
6651c9b5560aSMasanari Iida  *	ata_port_detach - Detach ATA port in preparation of device removal
6652c6fd2807SJeff Garzik  *	@ap: ATA port to be detached
6653c6fd2807SJeff Garzik  *
6654c6fd2807SJeff Garzik  *	Detach all ATA devices and the associated SCSI devices of @ap;
6655c6fd2807SJeff Garzik  *	then, remove the associated SCSI host.  @ap is guaranteed to
6656c6fd2807SJeff Garzik  *	be quiescent on return from this function.
6657c6fd2807SJeff Garzik  *
6658c6fd2807SJeff Garzik  *	LOCKING:
6659c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
6660c6fd2807SJeff Garzik  */
6661741b7763SAdrian Bunk static void ata_port_detach(struct ata_port *ap)
6662c6fd2807SJeff Garzik {
6663c6fd2807SJeff Garzik 	unsigned long flags;
6664a6f9bf4dSLevente Kurusa 	struct ata_link *link;
6665a6f9bf4dSLevente Kurusa 	struct ata_device *dev;
6666c6fd2807SJeff Garzik 
6667c6fd2807SJeff Garzik 	if (!ap->ops->error_handler)
6668c6fd2807SJeff Garzik 		goto skip_eh;
6669c6fd2807SJeff Garzik 
6670c6fd2807SJeff Garzik 	/* tell EH we're leaving & flush EH */
6671c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
6672c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_UNLOADING;
6673ece180d1STejun Heo 	ata_port_schedule_eh(ap);
6674c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
6675c6fd2807SJeff Garzik 
6676ece180d1STejun Heo 	/* wait till EH commits suicide */
6677c6fd2807SJeff Garzik 	ata_port_wait_eh(ap);
6678c6fd2807SJeff Garzik 
6679ece180d1STejun Heo 	/* it better be dead now */
6680ece180d1STejun Heo 	WARN_ON(!(ap->pflags & ATA_PFLAG_UNLOADED));
6681c6fd2807SJeff Garzik 
6682afe2c511STejun Heo 	cancel_delayed_work_sync(&ap->hotplug_task);
6683c6fd2807SJeff Garzik 
6684c6fd2807SJeff Garzik  skip_eh:
6685a6f9bf4dSLevente Kurusa 	/* clean up zpodd on port removal */
6686a6f9bf4dSLevente Kurusa 	ata_for_each_link(link, ap, HOST_FIRST) {
6687a6f9bf4dSLevente Kurusa 		ata_for_each_dev(dev, link, ALL) {
6688a6f9bf4dSLevente Kurusa 			if (zpodd_dev_enabled(dev))
6689a6f9bf4dSLevente Kurusa 				zpodd_exit(dev);
6690a6f9bf4dSLevente Kurusa 		}
6691a6f9bf4dSLevente Kurusa 	}
6692d9027470SGwendal Grignou 	if (ap->pmp_link) {
6693d9027470SGwendal Grignou 		int i;
6694d9027470SGwendal Grignou 		for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
6695d9027470SGwendal Grignou 			ata_tlink_delete(&ap->pmp_link[i]);
6696d9027470SGwendal Grignou 	}
6697c6fd2807SJeff Garzik 	/* remove the associated SCSI host */
6698cca3974eSJeff Garzik 	scsi_remove_host(ap->scsi_host);
6699c5700766SRafael J. Wysocki 	ata_tport_delete(ap);
6700c6fd2807SJeff Garzik }
6701c6fd2807SJeff Garzik 
6702c6fd2807SJeff Garzik /**
67030529c159STejun Heo  *	ata_host_detach - Detach all ports of an ATA host
67040529c159STejun Heo  *	@host: Host to detach
67050529c159STejun Heo  *
67060529c159STejun Heo  *	Detach all ports of @host.
67070529c159STejun Heo  *
67080529c159STejun Heo  *	LOCKING:
67090529c159STejun Heo  *	Kernel thread context (may sleep).
67100529c159STejun Heo  */
67110529c159STejun Heo void ata_host_detach(struct ata_host *host)
67120529c159STejun Heo {
67130529c159STejun Heo 	int i;
67140529c159STejun Heo 
67150529c159STejun Heo 	for (i = 0; i < host->n_ports; i++)
67160529c159STejun Heo 		ata_port_detach(host->ports[i]);
6717562f0c2dSTejun Heo 
6718562f0c2dSTejun Heo 	/* the host is dead now, dissociate ACPI */
6719562f0c2dSTejun Heo 	ata_acpi_dissociate(host);
67200529c159STejun Heo }
67210529c159STejun Heo 
6722c6fd2807SJeff Garzik #ifdef CONFIG_PCI
6723c6fd2807SJeff Garzik 
6724c6fd2807SJeff Garzik /**
6725c6fd2807SJeff Garzik  *	ata_pci_remove_one - PCI layer callback for device removal
6726c6fd2807SJeff Garzik  *	@pdev: PCI device that was removed
6727c6fd2807SJeff Garzik  *
6728b878ca5dSTejun Heo  *	PCI layer indicates to libata via this hook that hot-unplug or
6729b878ca5dSTejun Heo  *	module unload event has occurred.  Detach all ports.  Resource
6730b878ca5dSTejun Heo  *	release is handled via devres.
6731c6fd2807SJeff Garzik  *
6732c6fd2807SJeff Garzik  *	LOCKING:
6733c6fd2807SJeff Garzik  *	Inherited from PCI layer (may sleep).
6734c6fd2807SJeff Garzik  */
6735c6fd2807SJeff Garzik void ata_pci_remove_one(struct pci_dev *pdev)
6736c6fd2807SJeff Garzik {
673704a3f5b7SBrian Norris 	struct ata_host *host = pci_get_drvdata(pdev);
6738c6fd2807SJeff Garzik 
6739f0d36efdSTejun Heo 	ata_host_detach(host);
6740c6fd2807SJeff Garzik }
6741c6fd2807SJeff Garzik 
6742c6fd2807SJeff Garzik /* move to PCI subsystem */
6743c6fd2807SJeff Garzik int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
6744c6fd2807SJeff Garzik {
6745c6fd2807SJeff Garzik 	unsigned long tmp = 0;
6746c6fd2807SJeff Garzik 
6747c6fd2807SJeff Garzik 	switch (bits->width) {
6748c6fd2807SJeff Garzik 	case 1: {
6749c6fd2807SJeff Garzik 		u8 tmp8 = 0;
6750c6fd2807SJeff Garzik 		pci_read_config_byte(pdev, bits->reg, &tmp8);
6751c6fd2807SJeff Garzik 		tmp = tmp8;
6752c6fd2807SJeff Garzik 		break;
6753c6fd2807SJeff Garzik 	}
6754c6fd2807SJeff Garzik 	case 2: {
6755c6fd2807SJeff Garzik 		u16 tmp16 = 0;
6756c6fd2807SJeff Garzik 		pci_read_config_word(pdev, bits->reg, &tmp16);
6757c6fd2807SJeff Garzik 		tmp = tmp16;
6758c6fd2807SJeff Garzik 		break;
6759c6fd2807SJeff Garzik 	}
6760c6fd2807SJeff Garzik 	case 4: {
6761c6fd2807SJeff Garzik 		u32 tmp32 = 0;
6762c6fd2807SJeff Garzik 		pci_read_config_dword(pdev, bits->reg, &tmp32);
6763c6fd2807SJeff Garzik 		tmp = tmp32;
6764c6fd2807SJeff Garzik 		break;
6765c6fd2807SJeff Garzik 	}
6766c6fd2807SJeff Garzik 
6767c6fd2807SJeff Garzik 	default:
6768c6fd2807SJeff Garzik 		return -EINVAL;
6769c6fd2807SJeff Garzik 	}
6770c6fd2807SJeff Garzik 
6771c6fd2807SJeff Garzik 	tmp &= bits->mask;
6772c6fd2807SJeff Garzik 
6773c6fd2807SJeff Garzik 	return (tmp == bits->val) ? 1 : 0;
6774c6fd2807SJeff Garzik }
6775c6fd2807SJeff Garzik 
67766ffa01d8STejun Heo #ifdef CONFIG_PM
6777c6fd2807SJeff Garzik void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
6778c6fd2807SJeff Garzik {
6779c6fd2807SJeff Garzik 	pci_save_state(pdev);
6780c6fd2807SJeff Garzik 	pci_disable_device(pdev);
67814c90d971STejun Heo 
67823a2d5b70SRafael J. Wysocki 	if (mesg.event & PM_EVENT_SLEEP)
6783c6fd2807SJeff Garzik 		pci_set_power_state(pdev, PCI_D3hot);
6784c6fd2807SJeff Garzik }
6785c6fd2807SJeff Garzik 
6786553c4aa6STejun Heo int ata_pci_device_do_resume(struct pci_dev *pdev)
6787c6fd2807SJeff Garzik {
6788553c4aa6STejun Heo 	int rc;
6789553c4aa6STejun Heo 
6790c6fd2807SJeff Garzik 	pci_set_power_state(pdev, PCI_D0);
6791c6fd2807SJeff Garzik 	pci_restore_state(pdev);
6792553c4aa6STejun Heo 
6793f0d36efdSTejun Heo 	rc = pcim_enable_device(pdev);
6794553c4aa6STejun Heo 	if (rc) {
6795a44fec1fSJoe Perches 		dev_err(&pdev->dev,
6796553c4aa6STejun Heo 			"failed to enable device after resume (%d)\n", rc);
6797553c4aa6STejun Heo 		return rc;
6798553c4aa6STejun Heo 	}
6799553c4aa6STejun Heo 
6800c6fd2807SJeff Garzik 	pci_set_master(pdev);
6801553c4aa6STejun Heo 	return 0;
6802c6fd2807SJeff Garzik }
6803c6fd2807SJeff Garzik 
6804c6fd2807SJeff Garzik int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
6805c6fd2807SJeff Garzik {
680604a3f5b7SBrian Norris 	struct ata_host *host = pci_get_drvdata(pdev);
6807c6fd2807SJeff Garzik 	int rc = 0;
6808c6fd2807SJeff Garzik 
6809cca3974eSJeff Garzik 	rc = ata_host_suspend(host, mesg);
6810c6fd2807SJeff Garzik 	if (rc)
6811c6fd2807SJeff Garzik 		return rc;
6812c6fd2807SJeff Garzik 
6813c6fd2807SJeff Garzik 	ata_pci_device_do_suspend(pdev, mesg);
6814c6fd2807SJeff Garzik 
6815c6fd2807SJeff Garzik 	return 0;
6816c6fd2807SJeff Garzik }
6817c6fd2807SJeff Garzik 
6818c6fd2807SJeff Garzik int ata_pci_device_resume(struct pci_dev *pdev)
6819c6fd2807SJeff Garzik {
682004a3f5b7SBrian Norris 	struct ata_host *host = pci_get_drvdata(pdev);
6821553c4aa6STejun Heo 	int rc;
6822c6fd2807SJeff Garzik 
6823553c4aa6STejun Heo 	rc = ata_pci_device_do_resume(pdev);
6824553c4aa6STejun Heo 	if (rc == 0)
6825cca3974eSJeff Garzik 		ata_host_resume(host);
6826553c4aa6STejun Heo 	return rc;
6827c6fd2807SJeff Garzik }
68286ffa01d8STejun Heo #endif /* CONFIG_PM */
68296ffa01d8STejun Heo 
6830c6fd2807SJeff Garzik #endif /* CONFIG_PCI */
6831c6fd2807SJeff Garzik 
6832b7db04d9SBrian Norris /**
6833b7db04d9SBrian Norris  *	ata_platform_remove_one - Platform layer callback for device removal
6834b7db04d9SBrian Norris  *	@pdev: Platform device that was removed
6835b7db04d9SBrian Norris  *
6836b7db04d9SBrian Norris  *	Platform layer indicates to libata via this hook that hot-unplug or
6837b7db04d9SBrian Norris  *	module unload event has occurred.  Detach all ports.  Resource
6838b7db04d9SBrian Norris  *	release is handled via devres.
6839b7db04d9SBrian Norris  *
6840b7db04d9SBrian Norris  *	LOCKING:
6841b7db04d9SBrian Norris  *	Inherited from platform layer (may sleep).
6842b7db04d9SBrian Norris  */
6843b7db04d9SBrian Norris int ata_platform_remove_one(struct platform_device *pdev)
6844b7db04d9SBrian Norris {
6845b7db04d9SBrian Norris 	struct ata_host *host = platform_get_drvdata(pdev);
6846b7db04d9SBrian Norris 
6847b7db04d9SBrian Norris 	ata_host_detach(host);
6848b7db04d9SBrian Norris 
6849b7db04d9SBrian Norris 	return 0;
6850b7db04d9SBrian Norris }
6851b7db04d9SBrian Norris 
685233267325STejun Heo static int __init ata_parse_force_one(char **cur,
685333267325STejun Heo 				      struct ata_force_ent *force_ent,
685433267325STejun Heo 				      const char **reason)
685533267325STejun Heo {
68560f5f264bSRasmus Villemoes 	static const struct ata_force_param force_tbl[] __initconst = {
685733267325STejun Heo 		{ "40c",	.cbl		= ATA_CBL_PATA40 },
685833267325STejun Heo 		{ "80c",	.cbl		= ATA_CBL_PATA80 },
685933267325STejun Heo 		{ "short40c",	.cbl		= ATA_CBL_PATA40_SHORT },
686033267325STejun Heo 		{ "unk",	.cbl		= ATA_CBL_PATA_UNK },
686133267325STejun Heo 		{ "ign",	.cbl		= ATA_CBL_PATA_IGN },
686233267325STejun Heo 		{ "sata",	.cbl		= ATA_CBL_SATA },
686333267325STejun Heo 		{ "1.5Gbps",	.spd_limit	= 1 },
686433267325STejun Heo 		{ "3.0Gbps",	.spd_limit	= 2 },
686533267325STejun Heo 		{ "noncq",	.horkage_on	= ATA_HORKAGE_NONCQ },
686633267325STejun Heo 		{ "ncq",	.horkage_off	= ATA_HORKAGE_NONCQ },
6867d7b16e4fSMartin K. Petersen 		{ "noncqtrim",	.horkage_on	= ATA_HORKAGE_NO_NCQ_TRIM },
6868d7b16e4fSMartin K. Petersen 		{ "ncqtrim",	.horkage_off	= ATA_HORKAGE_NO_NCQ_TRIM },
686943c9c591STejun Heo 		{ "dump_id",	.horkage_on	= ATA_HORKAGE_DUMP_ID },
687033267325STejun Heo 		{ "pio0",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 0) },
687133267325STejun Heo 		{ "pio1",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 1) },
687233267325STejun Heo 		{ "pio2",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 2) },
687333267325STejun Heo 		{ "pio3",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 3) },
687433267325STejun Heo 		{ "pio4",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 4) },
687533267325STejun Heo 		{ "pio5",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 5) },
687633267325STejun Heo 		{ "pio6",	.xfer_mask	= 1 << (ATA_SHIFT_PIO + 6) },
687733267325STejun Heo 		{ "mwdma0",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 0) },
687833267325STejun Heo 		{ "mwdma1",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 1) },
687933267325STejun Heo 		{ "mwdma2",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 2) },
688033267325STejun Heo 		{ "mwdma3",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 3) },
688133267325STejun Heo 		{ "mwdma4",	.xfer_mask	= 1 << (ATA_SHIFT_MWDMA + 4) },
688233267325STejun Heo 		{ "udma0",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
688333267325STejun Heo 		{ "udma16",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
688433267325STejun Heo 		{ "udma/16",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 0) },
688533267325STejun Heo 		{ "udma1",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
688633267325STejun Heo 		{ "udma25",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
688733267325STejun Heo 		{ "udma/25",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 1) },
688833267325STejun Heo 		{ "udma2",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
688933267325STejun Heo 		{ "udma33",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
689033267325STejun Heo 		{ "udma/33",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 2) },
689133267325STejun Heo 		{ "udma3",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
689233267325STejun Heo 		{ "udma44",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
689333267325STejun Heo 		{ "udma/44",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 3) },
689433267325STejun Heo 		{ "udma4",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
689533267325STejun Heo 		{ "udma66",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
689633267325STejun Heo 		{ "udma/66",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 4) },
689733267325STejun Heo 		{ "udma5",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
689833267325STejun Heo 		{ "udma100",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
689933267325STejun Heo 		{ "udma/100",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 5) },
690033267325STejun Heo 		{ "udma6",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
690133267325STejun Heo 		{ "udma133",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
690233267325STejun Heo 		{ "udma/133",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 6) },
690333267325STejun Heo 		{ "udma7",	.xfer_mask	= 1 << (ATA_SHIFT_UDMA + 7) },
690405944bdfSTejun Heo 		{ "nohrst",	.lflags		= ATA_LFLAG_NO_HRST },
690505944bdfSTejun Heo 		{ "nosrst",	.lflags		= ATA_LFLAG_NO_SRST },
690605944bdfSTejun Heo 		{ "norst",	.lflags		= ATA_LFLAG_NO_HRST | ATA_LFLAG_NO_SRST },
6907ca6d43b0SDan Williams 		{ "rstonce",	.lflags		= ATA_LFLAG_RST_ONCE },
6908966fbe19SVincent Pelletier 		{ "atapi_dmadir", .horkage_on	= ATA_HORKAGE_ATAPI_DMADIR },
6909b8bd6dc3SRobin H. Johnson 		{ "disable",	.horkage_on	= ATA_HORKAGE_DISABLE },
691033267325STejun Heo 	};
691133267325STejun Heo 	char *start = *cur, *p = *cur;
691233267325STejun Heo 	char *id, *val, *endp;
691333267325STejun Heo 	const struct ata_force_param *match_fp = NULL;
691433267325STejun Heo 	int nr_matches = 0, i;
691533267325STejun Heo 
691633267325STejun Heo 	/* find where this param ends and update *cur */
691733267325STejun Heo 	while (*p != '\0' && *p != ',')
691833267325STejun Heo 		p++;
691933267325STejun Heo 
692033267325STejun Heo 	if (*p == '\0')
692133267325STejun Heo 		*cur = p;
692233267325STejun Heo 	else
692333267325STejun Heo 		*cur = p + 1;
692433267325STejun Heo 
692533267325STejun Heo 	*p = '\0';
692633267325STejun Heo 
692733267325STejun Heo 	/* parse */
692833267325STejun Heo 	p = strchr(start, ':');
692933267325STejun Heo 	if (!p) {
693033267325STejun Heo 		val = strstrip(start);
693133267325STejun Heo 		goto parse_val;
693233267325STejun Heo 	}
693333267325STejun Heo 	*p = '\0';
693433267325STejun Heo 
693533267325STejun Heo 	id = strstrip(start);
693633267325STejun Heo 	val = strstrip(p + 1);
693733267325STejun Heo 
693833267325STejun Heo 	/* parse id */
693933267325STejun Heo 	p = strchr(id, '.');
694033267325STejun Heo 	if (p) {
694133267325STejun Heo 		*p++ = '\0';
694233267325STejun Heo 		force_ent->device = simple_strtoul(p, &endp, 10);
694333267325STejun Heo 		if (p == endp || *endp != '\0') {
694433267325STejun Heo 			*reason = "invalid device";
694533267325STejun Heo 			return -EINVAL;
694633267325STejun Heo 		}
694733267325STejun Heo 	}
694833267325STejun Heo 
694933267325STejun Heo 	force_ent->port = simple_strtoul(id, &endp, 10);
6950f7cf69aeSTejun Heo 	if (id == endp || *endp != '\0') {
695133267325STejun Heo 		*reason = "invalid port/link";
695233267325STejun Heo 		return -EINVAL;
695333267325STejun Heo 	}
695433267325STejun Heo 
695533267325STejun Heo  parse_val:
695633267325STejun Heo 	/* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */
695733267325STejun Heo 	for (i = 0; i < ARRAY_SIZE(force_tbl); i++) {
695833267325STejun Heo 		const struct ata_force_param *fp = &force_tbl[i];
695933267325STejun Heo 
696033267325STejun Heo 		if (strncasecmp(val, fp->name, strlen(val)))
696133267325STejun Heo 			continue;
696233267325STejun Heo 
696333267325STejun Heo 		nr_matches++;
696433267325STejun Heo 		match_fp = fp;
696533267325STejun Heo 
696633267325STejun Heo 		if (strcasecmp(val, fp->name) == 0) {
696733267325STejun Heo 			nr_matches = 1;
696833267325STejun Heo 			break;
696933267325STejun Heo 		}
697033267325STejun Heo 	}
697133267325STejun Heo 
697233267325STejun Heo 	if (!nr_matches) {
697333267325STejun Heo 		*reason = "unknown value";
697433267325STejun Heo 		return -EINVAL;
697533267325STejun Heo 	}
697633267325STejun Heo 	if (nr_matches > 1) {
69779de55351SArvind Yadav 		*reason = "ambiguous value";
697833267325STejun Heo 		return -EINVAL;
697933267325STejun Heo 	}
698033267325STejun Heo 
698133267325STejun Heo 	force_ent->param = *match_fp;
698233267325STejun Heo 
698333267325STejun Heo 	return 0;
698433267325STejun Heo }
698533267325STejun Heo 
698633267325STejun Heo static void __init ata_parse_force_param(void)
698733267325STejun Heo {
698833267325STejun Heo 	int idx = 0, size = 1;
698933267325STejun Heo 	int last_port = -1, last_device = -1;
699033267325STejun Heo 	char *p, *cur, *next;
699133267325STejun Heo 
699233267325STejun Heo 	/* calculate maximum number of params and allocate force_tbl */
699333267325STejun Heo 	for (p = ata_force_param_buf; *p; p++)
699433267325STejun Heo 		if (*p == ',')
699533267325STejun Heo 			size++;
699633267325STejun Heo 
699733267325STejun Heo 	ata_force_tbl = kzalloc(sizeof(ata_force_tbl[0]) * size, GFP_KERNEL);
699833267325STejun Heo 	if (!ata_force_tbl) {
699933267325STejun Heo 		printk(KERN_WARNING "ata: failed to extend force table, "
700033267325STejun Heo 		       "libata.force ignored\n");
700133267325STejun Heo 		return;
700233267325STejun Heo 	}
700333267325STejun Heo 
700433267325STejun Heo 	/* parse and populate the table */
700533267325STejun Heo 	for (cur = ata_force_param_buf; *cur != '\0'; cur = next) {
700633267325STejun Heo 		const char *reason = "";
700733267325STejun Heo 		struct ata_force_ent te = { .port = -1, .device = -1 };
700833267325STejun Heo 
700933267325STejun Heo 		next = cur;
701033267325STejun Heo 		if (ata_parse_force_one(&next, &te, &reason)) {
701133267325STejun Heo 			printk(KERN_WARNING "ata: failed to parse force "
701233267325STejun Heo 			       "parameter \"%s\" (%s)\n",
701333267325STejun Heo 			       cur, reason);
701433267325STejun Heo 			continue;
701533267325STejun Heo 		}
701633267325STejun Heo 
701733267325STejun Heo 		if (te.port == -1) {
701833267325STejun Heo 			te.port = last_port;
701933267325STejun Heo 			te.device = last_device;
702033267325STejun Heo 		}
702133267325STejun Heo 
702233267325STejun Heo 		ata_force_tbl[idx++] = te;
702333267325STejun Heo 
702433267325STejun Heo 		last_port = te.port;
702533267325STejun Heo 		last_device = te.device;
702633267325STejun Heo 	}
702733267325STejun Heo 
702833267325STejun Heo 	ata_force_tbl_size = idx;
702933267325STejun Heo }
7030c6fd2807SJeff Garzik 
7031c6fd2807SJeff Garzik static int __init ata_init(void)
7032c6fd2807SJeff Garzik {
7033d9027470SGwendal Grignou 	int rc;
7034270390e1STejun Heo 
703533267325STejun Heo 	ata_parse_force_param();
703633267325STejun Heo 
7037270390e1STejun Heo 	rc = ata_sff_init();
7038ad72cf98STejun Heo 	if (rc) {
7039ad72cf98STejun Heo 		kfree(ata_force_tbl);
7040ad72cf98STejun Heo 		return rc;
7041ad72cf98STejun Heo 	}
7042c6fd2807SJeff Garzik 
7043d9027470SGwendal Grignou 	libata_transport_init();
7044d9027470SGwendal Grignou 	ata_scsi_transport_template = ata_attach_transport();
7045d9027470SGwendal Grignou 	if (!ata_scsi_transport_template) {
7046d9027470SGwendal Grignou 		ata_sff_exit();
7047d9027470SGwendal Grignou 		rc = -ENOMEM;
7048d9027470SGwendal Grignou 		goto err_out;
7049d9027470SGwendal Grignou 	}
7050d9027470SGwendal Grignou 
7051c6fd2807SJeff Garzik 	printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
7052c6fd2807SJeff Garzik 	return 0;
7053d9027470SGwendal Grignou 
7054d9027470SGwendal Grignou err_out:
7055d9027470SGwendal Grignou 	return rc;
7056c6fd2807SJeff Garzik }
7057c6fd2807SJeff Garzik 
7058c6fd2807SJeff Garzik static void __exit ata_exit(void)
7059c6fd2807SJeff Garzik {
7060d9027470SGwendal Grignou 	ata_release_transport(ata_scsi_transport_template);
7061d9027470SGwendal Grignou 	libata_transport_exit();
7062270390e1STejun Heo 	ata_sff_exit();
706333267325STejun Heo 	kfree(ata_force_tbl);
7064c6fd2807SJeff Garzik }
7065c6fd2807SJeff Garzik 
7066a4625085SBrian King subsys_initcall(ata_init);
7067c6fd2807SJeff Garzik module_exit(ata_exit);
7068c6fd2807SJeff Garzik 
70699990b6f3SAkinobu Mita static DEFINE_RATELIMIT_STATE(ratelimit, HZ / 5, 1);
7070c6fd2807SJeff Garzik 
7071c6fd2807SJeff Garzik int ata_ratelimit(void)
7072c6fd2807SJeff Garzik {
70739990b6f3SAkinobu Mita 	return __ratelimit(&ratelimit);
7074c6fd2807SJeff Garzik }
7075c6fd2807SJeff Garzik 
7076c0c362b6STejun Heo /**
7077c0c362b6STejun Heo  *	ata_msleep - ATA EH owner aware msleep
7078c0c362b6STejun Heo  *	@ap: ATA port to attribute the sleep to
7079c0c362b6STejun Heo  *	@msecs: duration to sleep in milliseconds
7080c0c362b6STejun Heo  *
7081c0c362b6STejun Heo  *	Sleeps @msecs.  If the current task is owner of @ap's EH, the
7082c0c362b6STejun Heo  *	ownership is released before going to sleep and reacquired
7083c0c362b6STejun Heo  *	after the sleep is complete.  IOW, other ports sharing the
7084c0c362b6STejun Heo  *	@ap->host will be allowed to own the EH while this task is
7085c0c362b6STejun Heo  *	sleeping.
7086c0c362b6STejun Heo  *
7087c0c362b6STejun Heo  *	LOCKING:
7088c0c362b6STejun Heo  *	Might sleep.
7089c0c362b6STejun Heo  */
709097750cebSTejun Heo void ata_msleep(struct ata_port *ap, unsigned int msecs)
709197750cebSTejun Heo {
7092c0c362b6STejun Heo 	bool owns_eh = ap && ap->host->eh_owner == current;
7093c0c362b6STejun Heo 
7094c0c362b6STejun Heo 	if (owns_eh)
7095c0c362b6STejun Heo 		ata_eh_release(ap);
7096c0c362b6STejun Heo 
7097848c3920SAnil Veliyankara Madam 	if (msecs < 20) {
7098848c3920SAnil Veliyankara Madam 		unsigned long usecs = msecs * USEC_PER_MSEC;
7099848c3920SAnil Veliyankara Madam 		usleep_range(usecs, usecs + 50);
7100848c3920SAnil Veliyankara Madam 	} else {
710197750cebSTejun Heo 		msleep(msecs);
7102848c3920SAnil Veliyankara Madam 	}
7103c0c362b6STejun Heo 
7104c0c362b6STejun Heo 	if (owns_eh)
7105c0c362b6STejun Heo 		ata_eh_acquire(ap);
710697750cebSTejun Heo }
710797750cebSTejun Heo 
7108c6fd2807SJeff Garzik /**
7109c6fd2807SJeff Garzik  *	ata_wait_register - wait until register value changes
711097750cebSTejun Heo  *	@ap: ATA port to wait register for, can be NULL
7111c6fd2807SJeff Garzik  *	@reg: IO-mapped register
7112c6fd2807SJeff Garzik  *	@mask: Mask to apply to read register value
7113c6fd2807SJeff Garzik  *	@val: Wait condition
7114341c2c95STejun Heo  *	@interval: polling interval in milliseconds
7115341c2c95STejun Heo  *	@timeout: timeout in milliseconds
7116c6fd2807SJeff Garzik  *
7117c6fd2807SJeff Garzik  *	Waiting for some bits of register to change is a common
7118c6fd2807SJeff Garzik  *	operation for ATA controllers.  This function reads 32bit LE
7119c6fd2807SJeff Garzik  *	IO-mapped register @reg and tests for the following condition.
7120c6fd2807SJeff Garzik  *
7121c6fd2807SJeff Garzik  *	(*@reg & mask) != val
7122c6fd2807SJeff Garzik  *
7123c6fd2807SJeff Garzik  *	If the condition is met, it returns; otherwise, the process is
7124c6fd2807SJeff Garzik  *	repeated after @interval_msec until timeout.
7125c6fd2807SJeff Garzik  *
7126c6fd2807SJeff Garzik  *	LOCKING:
7127c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
7128c6fd2807SJeff Garzik  *
7129c6fd2807SJeff Garzik  *	RETURNS:
7130c6fd2807SJeff Garzik  *	The final register value.
7131c6fd2807SJeff Garzik  */
713297750cebSTejun Heo u32 ata_wait_register(struct ata_port *ap, void __iomem *reg, u32 mask, u32 val,
7133341c2c95STejun Heo 		      unsigned long interval, unsigned long timeout)
7134c6fd2807SJeff Garzik {
7135341c2c95STejun Heo 	unsigned long deadline;
7136c6fd2807SJeff Garzik 	u32 tmp;
7137c6fd2807SJeff Garzik 
7138c6fd2807SJeff Garzik 	tmp = ioread32(reg);
7139c6fd2807SJeff Garzik 
7140c6fd2807SJeff Garzik 	/* Calculate timeout _after_ the first read to make sure
7141c6fd2807SJeff Garzik 	 * preceding writes reach the controller before starting to
7142c6fd2807SJeff Garzik 	 * eat away the timeout.
7143c6fd2807SJeff Garzik 	 */
7144341c2c95STejun Heo 	deadline = ata_deadline(jiffies, timeout);
7145c6fd2807SJeff Garzik 
7146341c2c95STejun Heo 	while ((tmp & mask) == val && time_before(jiffies, deadline)) {
714797750cebSTejun Heo 		ata_msleep(ap, interval);
7148c6fd2807SJeff Garzik 		tmp = ioread32(reg);
7149c6fd2807SJeff Garzik 	}
7150c6fd2807SJeff Garzik 
7151c6fd2807SJeff Garzik 	return tmp;
7152c6fd2807SJeff Garzik }
7153c6fd2807SJeff Garzik 
71548393b811SGabriele Mazzotta /**
71558393b811SGabriele Mazzotta  *	sata_lpm_ignore_phy_events - test if PHY event should be ignored
71568393b811SGabriele Mazzotta  *	@link: Link receiving the event
71578393b811SGabriele Mazzotta  *
71588393b811SGabriele Mazzotta  *	Test whether the received PHY event has to be ignored or not.
71598393b811SGabriele Mazzotta  *
71608393b811SGabriele Mazzotta  *	LOCKING:
71618393b811SGabriele Mazzotta  *	None:
71628393b811SGabriele Mazzotta  *
71638393b811SGabriele Mazzotta  *	RETURNS:
71648393b811SGabriele Mazzotta  *	True if the event has to be ignored.
71658393b811SGabriele Mazzotta  */
71668393b811SGabriele Mazzotta bool sata_lpm_ignore_phy_events(struct ata_link *link)
71678393b811SGabriele Mazzotta {
716809c5b480SGabriele Mazzotta 	unsigned long lpm_timeout = link->last_lpm_change +
716909c5b480SGabriele Mazzotta 				    msecs_to_jiffies(ATA_TMOUT_SPURIOUS_PHY);
717009c5b480SGabriele Mazzotta 
71718393b811SGabriele Mazzotta 	/* if LPM is enabled, PHYRDY doesn't mean anything */
717209c5b480SGabriele Mazzotta 	if (link->lpm_policy > ATA_LPM_MAX_POWER)
717309c5b480SGabriele Mazzotta 		return true;
717409c5b480SGabriele Mazzotta 
717509c5b480SGabriele Mazzotta 	/* ignore the first PHY event after the LPM policy changed
717609c5b480SGabriele Mazzotta 	 * as it is might be spurious
717709c5b480SGabriele Mazzotta 	 */
717809c5b480SGabriele Mazzotta 	if ((link->flags & ATA_LFLAG_CHANGED) &&
717909c5b480SGabriele Mazzotta 	    time_before(jiffies, lpm_timeout))
718009c5b480SGabriele Mazzotta 		return true;
718109c5b480SGabriele Mazzotta 
718209c5b480SGabriele Mazzotta 	return false;
71838393b811SGabriele Mazzotta }
71848393b811SGabriele Mazzotta EXPORT_SYMBOL_GPL(sata_lpm_ignore_phy_events);
71858393b811SGabriele Mazzotta 
7186c6fd2807SJeff Garzik /*
7187c6fd2807SJeff Garzik  * Dummy port_ops
7188c6fd2807SJeff Garzik  */
7189c6fd2807SJeff Garzik static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
7190c6fd2807SJeff Garzik {
7191c6fd2807SJeff Garzik 	return AC_ERR_SYSTEM;
7192c6fd2807SJeff Garzik }
7193c6fd2807SJeff Garzik 
7194182d7bbaSTejun Heo static void ata_dummy_error_handler(struct ata_port *ap)
7195182d7bbaSTejun Heo {
7196182d7bbaSTejun Heo 	/* truly dummy */
7197182d7bbaSTejun Heo }
7198182d7bbaSTejun Heo 
7199029cfd6bSTejun Heo struct ata_port_operations ata_dummy_port_ops = {
7200c6fd2807SJeff Garzik 	.qc_prep		= ata_noop_qc_prep,
7201c6fd2807SJeff Garzik 	.qc_issue		= ata_dummy_qc_issue,
7202182d7bbaSTejun Heo 	.error_handler		= ata_dummy_error_handler,
7203e4a9c373SDan Williams 	.sched_eh		= ata_std_sched_eh,
7204e4a9c373SDan Williams 	.end_eh			= ata_std_end_eh,
7205c6fd2807SJeff Garzik };
7206c6fd2807SJeff Garzik 
720721b0ad4fSTejun Heo const struct ata_port_info ata_dummy_port_info = {
720821b0ad4fSTejun Heo 	.port_ops		= &ata_dummy_port_ops,
720921b0ad4fSTejun Heo };
721021b0ad4fSTejun Heo 
7211c6fd2807SJeff Garzik /*
7212a9a79dfeSJoe Perches  * Utility print functions
7213a9a79dfeSJoe Perches  */
7214d7bead1bSJoe Perches void ata_port_printk(const struct ata_port *ap, const char *level,
7215a9a79dfeSJoe Perches 		     const char *fmt, ...)
7216a9a79dfeSJoe Perches {
7217a9a79dfeSJoe Perches 	struct va_format vaf;
7218a9a79dfeSJoe Perches 	va_list args;
7219a9a79dfeSJoe Perches 
7220a9a79dfeSJoe Perches 	va_start(args, fmt);
7221a9a79dfeSJoe Perches 
7222a9a79dfeSJoe Perches 	vaf.fmt = fmt;
7223a9a79dfeSJoe Perches 	vaf.va = &args;
7224a9a79dfeSJoe Perches 
7225d7bead1bSJoe Perches 	printk("%sata%u: %pV", level, ap->print_id, &vaf);
7226a9a79dfeSJoe Perches 
7227a9a79dfeSJoe Perches 	va_end(args);
7228a9a79dfeSJoe Perches }
7229a9a79dfeSJoe Perches EXPORT_SYMBOL(ata_port_printk);
7230a9a79dfeSJoe Perches 
7231d7bead1bSJoe Perches void ata_link_printk(const struct ata_link *link, const char *level,
7232a9a79dfeSJoe Perches 		     const char *fmt, ...)
7233a9a79dfeSJoe Perches {
7234a9a79dfeSJoe Perches 	struct va_format vaf;
7235a9a79dfeSJoe Perches 	va_list args;
7236a9a79dfeSJoe Perches 
7237a9a79dfeSJoe Perches 	va_start(args, fmt);
7238a9a79dfeSJoe Perches 
7239a9a79dfeSJoe Perches 	vaf.fmt = fmt;
7240a9a79dfeSJoe Perches 	vaf.va = &args;
7241a9a79dfeSJoe Perches 
7242a9a79dfeSJoe Perches 	if (sata_pmp_attached(link->ap) || link->ap->slave_link)
7243d7bead1bSJoe Perches 		printk("%sata%u.%02u: %pV",
7244a9a79dfeSJoe Perches 		       level, link->ap->print_id, link->pmp, &vaf);
7245a9a79dfeSJoe Perches 	else
7246d7bead1bSJoe Perches 		printk("%sata%u: %pV",
7247a9a79dfeSJoe Perches 		       level, link->ap->print_id, &vaf);
7248a9a79dfeSJoe Perches 
7249a9a79dfeSJoe Perches 	va_end(args);
7250a9a79dfeSJoe Perches }
7251a9a79dfeSJoe Perches EXPORT_SYMBOL(ata_link_printk);
7252a9a79dfeSJoe Perches 
7253d7bead1bSJoe Perches void ata_dev_printk(const struct ata_device *dev, const char *level,
7254a9a79dfeSJoe Perches 		    const char *fmt, ...)
7255a9a79dfeSJoe Perches {
7256a9a79dfeSJoe Perches 	struct va_format vaf;
7257a9a79dfeSJoe Perches 	va_list args;
7258a9a79dfeSJoe Perches 
7259a9a79dfeSJoe Perches 	va_start(args, fmt);
7260a9a79dfeSJoe Perches 
7261a9a79dfeSJoe Perches 	vaf.fmt = fmt;
7262a9a79dfeSJoe Perches 	vaf.va = &args;
7263a9a79dfeSJoe Perches 
7264d7bead1bSJoe Perches 	printk("%sata%u.%02u: %pV",
7265a9a79dfeSJoe Perches 	       level, dev->link->ap->print_id, dev->link->pmp + dev->devno,
7266a9a79dfeSJoe Perches 	       &vaf);
7267a9a79dfeSJoe Perches 
7268a9a79dfeSJoe Perches 	va_end(args);
7269a9a79dfeSJoe Perches }
7270a9a79dfeSJoe Perches EXPORT_SYMBOL(ata_dev_printk);
7271a9a79dfeSJoe Perches 
727206296a1eSJoe Perches void ata_print_version(const struct device *dev, const char *version)
727306296a1eSJoe Perches {
727406296a1eSJoe Perches 	dev_printk(KERN_DEBUG, dev, "version %s\n", version);
727506296a1eSJoe Perches }
727606296a1eSJoe Perches EXPORT_SYMBOL(ata_print_version);
727706296a1eSJoe Perches 
7278a9a79dfeSJoe Perches /*
7279c6fd2807SJeff Garzik  * libata is essentially a library of internal helper functions for
7280c6fd2807SJeff Garzik  * low-level ATA host controller drivers.  As such, the API/ABI is
7281c6fd2807SJeff Garzik  * likely to change as new drivers are added and updated.
7282c6fd2807SJeff Garzik  * Do not depend on ABI/API stability.
7283c6fd2807SJeff Garzik  */
7284c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_deb_timing_normal);
7285c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_deb_timing_hotplug);
7286c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_deb_timing_long);
7287029cfd6bSTejun Heo EXPORT_SYMBOL_GPL(ata_base_port_ops);
7288029cfd6bSTejun Heo EXPORT_SYMBOL_GPL(sata_port_ops);
7289c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
729021b0ad4fSTejun Heo EXPORT_SYMBOL_GPL(ata_dummy_port_info);
72911eca4365STejun Heo EXPORT_SYMBOL_GPL(ata_link_next);
72921eca4365STejun Heo EXPORT_SYMBOL_GPL(ata_dev_next);
7293c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_std_bios_param);
7294d8d9129eSTejun Heo EXPORT_SYMBOL_GPL(ata_scsi_unlock_native_capacity);
7295cca3974eSJeff Garzik EXPORT_SYMBOL_GPL(ata_host_init);
7296f3187195STejun Heo EXPORT_SYMBOL_GPL(ata_host_alloc);
7297f5cda257STejun Heo EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
7298b1c72916STejun Heo EXPORT_SYMBOL_GPL(ata_slave_link_init);
7299ecef7253STejun Heo EXPORT_SYMBOL_GPL(ata_host_start);
7300f3187195STejun Heo EXPORT_SYMBOL_GPL(ata_host_register);
7301f5cda257STejun Heo EXPORT_SYMBOL_GPL(ata_host_activate);
73020529c159STejun Heo EXPORT_SYMBOL_GPL(ata_host_detach);
7303c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_sg_init);
7304c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_qc_complete);
7305c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_qc_complete_multiple);
7306436d34b3STejun Heo EXPORT_SYMBOL_GPL(atapi_cmd_type);
7307c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_tf_to_fis);
7308c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_tf_from_fis);
73096357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_pack_xfermask);
73106357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_unpack_xfermask);
73116357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
73126357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
73136357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
73146357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_mode_string);
73156357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_id_xfermask);
731604351821SAlan EXPORT_SYMBOL_GPL(ata_do_set_mode);
731731cc23b3STejun Heo EXPORT_SYMBOL_GPL(ata_std_qc_defer);
7318c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
731910305f0fSAlan EXPORT_SYMBOL_GPL(ata_dev_disable);
7320c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_set_spd);
7321aa2731adSTejun Heo EXPORT_SYMBOL_GPL(ata_wait_after_reset);
7322936fd732STejun Heo EXPORT_SYMBOL_GPL(sata_link_debounce);
7323936fd732STejun Heo EXPORT_SYMBOL_GPL(sata_link_resume);
73241152b261STejun Heo EXPORT_SYMBOL_GPL(sata_link_scr_lpm);
73250aa1113dSTejun Heo EXPORT_SYMBOL_GPL(ata_std_prereset);
7326cc0680a5STejun Heo EXPORT_SYMBOL_GPL(sata_link_hardreset);
732757c9efdfSTejun Heo EXPORT_SYMBOL_GPL(sata_std_hardreset);
7328203c75b8STejun Heo EXPORT_SYMBOL_GPL(ata_std_postreset);
7329c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_dev_classify);
7330c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_dev_pair);
7331c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_ratelimit);
733297750cebSTejun Heo EXPORT_SYMBOL_GPL(ata_msleep);
7333c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_wait_register);
7334c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_queuecmd);
7335c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_slave_config);
7336c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_slave_destroy);
7337c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_change_queue_depth);
7338f6e67035SDan Williams EXPORT_SYMBOL_GPL(__ata_change_queue_depth);
7339c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_valid);
7340c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_read);
7341c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_write);
7342c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(sata_scr_write_flush);
7343936fd732STejun Heo EXPORT_SYMBOL_GPL(ata_link_online);
7344936fd732STejun Heo EXPORT_SYMBOL_GPL(ata_link_offline);
73456ffa01d8STejun Heo #ifdef CONFIG_PM
7346cca3974eSJeff Garzik EXPORT_SYMBOL_GPL(ata_host_suspend);
7347cca3974eSJeff Garzik EXPORT_SYMBOL_GPL(ata_host_resume);
73486ffa01d8STejun Heo #endif /* CONFIG_PM */
7349c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_id_string);
7350c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_id_c_string);
7351963e4975SAlan Cox EXPORT_SYMBOL_GPL(ata_do_dev_read_id);
7352c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_scsi_simulate);
7353c6fd2807SJeff Garzik 
7354c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
73556357357cSTejun Heo EXPORT_SYMBOL_GPL(ata_timing_find_mode);
7356c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_timing_compute);
7357c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_timing_merge);
7358a0f79b92STejun Heo EXPORT_SYMBOL_GPL(ata_timing_cycle2mode);
7359c6fd2807SJeff Garzik 
7360c6fd2807SJeff Garzik #ifdef CONFIG_PCI
7361c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(pci_test_config_bits);
7362c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_remove_one);
73636ffa01d8STejun Heo #ifdef CONFIG_PM
7364c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
7365c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
7366c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
7367c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_pci_device_resume);
73686ffa01d8STejun Heo #endif /* CONFIG_PM */
7369c6fd2807SJeff Garzik #endif /* CONFIG_PCI */
7370c6fd2807SJeff Garzik 
7371b7db04d9SBrian Norris EXPORT_SYMBOL_GPL(ata_platform_remove_one);
7372b7db04d9SBrian Norris 
7373b64bbc39STejun Heo EXPORT_SYMBOL_GPL(__ata_ehi_push_desc);
7374b64bbc39STejun Heo EXPORT_SYMBOL_GPL(ata_ehi_push_desc);
7375b64bbc39STejun Heo EXPORT_SYMBOL_GPL(ata_ehi_clear_desc);
7376cbcdd875STejun Heo EXPORT_SYMBOL_GPL(ata_port_desc);
7377cbcdd875STejun Heo #ifdef CONFIG_PCI
7378cbcdd875STejun Heo EXPORT_SYMBOL_GPL(ata_port_pbar_desc);
7379cbcdd875STejun Heo #endif /* CONFIG_PCI */
7380c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_schedule_eh);
7381dbd82616STejun Heo EXPORT_SYMBOL_GPL(ata_link_abort);
7382c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_abort);
7383c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_port_freeze);
73847d77b247STejun Heo EXPORT_SYMBOL_GPL(sata_async_notification);
7385c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_freeze_port);
7386c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_thaw_port);
7387c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_qc_complete);
7388c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_eh_qc_retry);
738910acf3b0SMark Lord EXPORT_SYMBOL_GPL(ata_eh_analyze_ncq_error);
7390c6fd2807SJeff Garzik EXPORT_SYMBOL_GPL(ata_do_eh);
7391a1efdabaSTejun Heo EXPORT_SYMBOL_GPL(ata_std_error_handler);
7392be0d18dfSAlan Cox 
7393be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_40wire);
7394be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_80wire);
7395be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_unknown);
7396c88f90c3STejun Heo EXPORT_SYMBOL_GPL(ata_cable_ignore);
7397be0d18dfSAlan Cox EXPORT_SYMBOL_GPL(ata_cable_sata);
7398