xref: /openbmc/linux/drivers/ata/libata-core.c (revision 62e4a60e)
1c82ee6d3SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2c6fd2807SJeff Garzik /*
3c6fd2807SJeff Garzik  *  libata-core.c - helper library for ATA
4c6fd2807SJeff Garzik  *
5c6fd2807SJeff Garzik  *  Copyright 2003-2004 Red Hat, Inc.  All rights reserved.
6c6fd2807SJeff Garzik  *  Copyright 2003-2004 Jeff Garzik
7c6fd2807SJeff Garzik  *
8c6fd2807SJeff Garzik  *  libata documentation is available via 'make {ps|pdf}docs',
919285f3cSMauro Carvalho Chehab  *  as Documentation/driver-api/libata.rst
10c6fd2807SJeff Garzik  *
11c6fd2807SJeff Garzik  *  Hardware documentation available from http://www.t13.org/ and
12c6fd2807SJeff Garzik  *  http://www.sata-io.org/
13c6fd2807SJeff Garzik  *
1492c52c52SAlan Cox  *  Standards documents from:
1592c52c52SAlan Cox  *	http://www.t13.org (ATA standards, PCI DMA IDE spec)
1692c52c52SAlan Cox  *	http://www.t10.org (SCSI MMC - for ATAPI MMC)
1792c52c52SAlan Cox  *	http://www.sata-io.org (SATA)
1892c52c52SAlan Cox  *	http://www.compactflash.org (CF)
1992c52c52SAlan Cox  *	http://www.qic.org (QIC157 - Tape and DSC)
2092c52c52SAlan Cox  *	http://www.ce-ata.org (CE-ATA: not supported)
21a52fbcfcSBartlomiej Zolnierkiewicz  *
22a52fbcfcSBartlomiej Zolnierkiewicz  * libata is essentially a library of internal helper functions for
23a52fbcfcSBartlomiej Zolnierkiewicz  * low-level ATA host controller drivers.  As such, the API/ABI is
24a52fbcfcSBartlomiej Zolnierkiewicz  * likely to change as new drivers are added and updated.
25a52fbcfcSBartlomiej Zolnierkiewicz  * Do not depend on ABI/API stability.
26c6fd2807SJeff Garzik  */
27c6fd2807SJeff Garzik 
28c6fd2807SJeff Garzik #include <linux/kernel.h>
29c6fd2807SJeff Garzik #include <linux/module.h>
30c6fd2807SJeff Garzik #include <linux/pci.h>
31c6fd2807SJeff Garzik #include <linux/init.h>
32c6fd2807SJeff Garzik #include <linux/list.h>
33c6fd2807SJeff Garzik #include <linux/mm.h>
34c6fd2807SJeff Garzik #include <linux/spinlock.h>
35c6fd2807SJeff Garzik #include <linux/blkdev.h>
36c6fd2807SJeff Garzik #include <linux/delay.h>
37c6fd2807SJeff Garzik #include <linux/timer.h>
38848c3920SAnil Veliyankara Madam #include <linux/time.h>
39c6fd2807SJeff Garzik #include <linux/interrupt.h>
40c6fd2807SJeff Garzik #include <linux/completion.h>
41c6fd2807SJeff Garzik #include <linux/suspend.h>
42c6fd2807SJeff Garzik #include <linux/workqueue.h>
43c6fd2807SJeff Garzik #include <linux/scatterlist.h>
442dcb407eSJeff Garzik #include <linux/io.h>
45e18086d6SMark Lord #include <linux/log2.h>
465a0e3ad6STejun Heo #include <linux/slab.h>
47428ac5fcSGeorge Spelvin #include <linux/glob.h>
48c6fd2807SJeff Garzik #include <scsi/scsi.h>
49c6fd2807SJeff Garzik #include <scsi/scsi_cmnd.h>
50c6fd2807SJeff Garzik #include <scsi/scsi_host.h>
51c6fd2807SJeff Garzik #include <linux/libata.h>
52c6fd2807SJeff Garzik #include <asm/byteorder.h>
53fe5af0ccSHannes Reinecke #include <asm/unaligned.h>
54140b5e59STejun Heo #include <linux/cdrom.h>
559990b6f3SAkinobu Mita #include <linux/ratelimit.h>
56eb25cb99SStephan Linz #include <linux/leds.h>
579ee4f393SLin Ming #include <linux/pm_runtime.h>
58b7db04d9SBrian Norris #include <linux/platform_device.h>
59bbf5a097SBartlomiej Zolnierkiewicz #include <asm/setup.h>
60c6fd2807SJeff Garzik 
61255c03d1SHannes Reinecke #define CREATE_TRACE_POINTS
62255c03d1SHannes Reinecke #include <trace/events/libata.h>
63255c03d1SHannes Reinecke 
64c6fd2807SJeff Garzik #include "libata.h"
65d9027470SGwendal Grignou #include "libata-transport.h"
66fda0efc5SJeff Garzik 
67029cfd6bSTejun Heo const struct ata_port_operations ata_base_port_ops = {
680aa1113dSTejun Heo 	.prereset		= ata_std_prereset,
69203c75b8STejun Heo 	.postreset		= ata_std_postreset,
70a1efdabaSTejun Heo 	.error_handler		= ata_std_error_handler,
71e4a9c373SDan Williams 	.sched_eh		= ata_std_sched_eh,
72e4a9c373SDan Williams 	.end_eh			= ata_std_end_eh,
73029cfd6bSTejun Heo };
74029cfd6bSTejun Heo 
75029cfd6bSTejun Heo const struct ata_port_operations sata_port_ops = {
76029cfd6bSTejun Heo 	.inherits		= &ata_base_port_ops,
77029cfd6bSTejun Heo 
78029cfd6bSTejun Heo 	.qc_defer		= ata_std_qc_defer,
7957c9efdfSTejun Heo 	.hardreset		= sata_std_hardreset,
80029cfd6bSTejun Heo };
81a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(sata_port_ops);
82029cfd6bSTejun Heo 
83c6fd2807SJeff Garzik static unsigned int ata_dev_init_params(struct ata_device *dev,
84c6fd2807SJeff Garzik 					u16 heads, u16 sectors);
85c6fd2807SJeff Garzik static unsigned int ata_dev_set_xfermode(struct ata_device *dev);
86c6fd2807SJeff Garzik static void ata_dev_xfermask(struct ata_device *dev);
8775683fe7STejun Heo static unsigned long ata_dev_blacklisted(const struct ata_device *dev);
88c6fd2807SJeff Garzik 
89a78f57afSTero Roponen atomic_t ata_print_id = ATOMIC_INIT(0);
90c6fd2807SJeff Garzik 
91bf89b0bfSBartlomiej Zolnierkiewicz #ifdef CONFIG_ATA_FORCE
9233267325STejun Heo struct ata_force_param {
9333267325STejun Heo 	const char	*name;
948ba5a45cSBartlomiej Zolnierkiewicz 	u8		cbl;
958ba5a45cSBartlomiej Zolnierkiewicz 	u8		spd_limit;
96f0a6d77bSSergey Shtylyov 	unsigned int	xfer_mask;
9733267325STejun Heo 	unsigned int	horkage_on;
9833267325STejun Heo 	unsigned int	horkage_off;
993af9ca4dSDamien Le Moal 	u16		lflags_on;
1003af9ca4dSDamien Le Moal 	u16		lflags_off;
10133267325STejun Heo };
10233267325STejun Heo 
10333267325STejun Heo struct ata_force_ent {
10433267325STejun Heo 	int			port;
10533267325STejun Heo 	int			device;
10633267325STejun Heo 	struct ata_force_param	param;
10733267325STejun Heo };
10833267325STejun Heo 
10933267325STejun Heo static struct ata_force_ent *ata_force_tbl;
11033267325STejun Heo static int ata_force_tbl_size;
11133267325STejun Heo 
112bbf5a097SBartlomiej Zolnierkiewicz static char ata_force_param_buf[COMMAND_LINE_SIZE] __initdata;
1137afb4222STejun Heo /* param_buf is thrown away after initialization, disallow read */
1147afb4222STejun Heo module_param_string(force, ata_force_param_buf, sizeof(ata_force_param_buf), 0);
1158c27ceffSMauro 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)");
116bf89b0bfSBartlomiej Zolnierkiewicz #endif
11733267325STejun Heo 
1182486fa56STejun Heo static int atapi_enabled = 1;
119c6fd2807SJeff Garzik module_param(atapi_enabled, int, 0444);
120ad5d8eacSEvgeni Golov MODULE_PARM_DESC(atapi_enabled, "Enable discovery of ATAPI devices (0=off, 1=on [default])");
121c6fd2807SJeff Garzik 
122c5c61bdaSAdrian Bunk static int atapi_dmadir = 0;
123c6fd2807SJeff Garzik module_param(atapi_dmadir, int, 0444);
124ad5d8eacSEvgeni Golov MODULE_PARM_DESC(atapi_dmadir, "Enable ATAPI DMADIR bridge support (0=off [default], 1=on)");
125c6fd2807SJeff Garzik 
126baf4fdfaSMark Lord int atapi_passthru16 = 1;
127baf4fdfaSMark Lord module_param(atapi_passthru16, int, 0444);
128ad5d8eacSEvgeni Golov MODULE_PARM_DESC(atapi_passthru16, "Enable ATA_16 passthru for ATAPI devices (0=off, 1=on [default])");
129baf4fdfaSMark Lord 
130c6fd2807SJeff Garzik int libata_fua = 0;
131c6fd2807SJeff Garzik module_param_named(fua, libata_fua, int, 0444);
132ad5d8eacSEvgeni Golov MODULE_PARM_DESC(fua, "FUA support (0=off [default], 1=on)");
133c6fd2807SJeff Garzik 
1342dcb407eSJeff Garzik static int ata_ignore_hpa;
1351e999736SAlan Cox module_param_named(ignore_hpa, ata_ignore_hpa, int, 0644);
1361e999736SAlan Cox MODULE_PARM_DESC(ignore_hpa, "Ignore HPA limit (0=keep BIOS limits, 1=ignore limits, using full disk)");
1371e999736SAlan Cox 
138b3a70601SAlan Cox static int libata_dma_mask = ATA_DMA_MASK_ATA|ATA_DMA_MASK_ATAPI|ATA_DMA_MASK_CFA;
139b3a70601SAlan Cox module_param_named(dma, libata_dma_mask, int, 0444);
140b3a70601SAlan Cox MODULE_PARM_DESC(dma, "DMA enable/disable (0x1==ATA, 0x2==ATAPI, 0x4==CF)");
141b3a70601SAlan Cox 
14287fbc5a0STejun Heo static int ata_probe_timeout;
143c6fd2807SJeff Garzik module_param(ata_probe_timeout, int, 0444);
144c6fd2807SJeff Garzik MODULE_PARM_DESC(ata_probe_timeout, "Set ATA probing timeout (seconds)");
145c6fd2807SJeff Garzik 
1466ebe9d86SJeff Garzik int libata_noacpi = 0;
147d7d0dad6SJeff Garzik module_param_named(noacpi, libata_noacpi, int, 0444);
148ad5d8eacSEvgeni Golov MODULE_PARM_DESC(noacpi, "Disable the use of ACPI in probe/suspend/resume (0=off [default], 1=on)");
14911ef697bSKristen Carlson Accardi 
150ae8d4ee7SAlan Cox int libata_allow_tpm = 0;
151ae8d4ee7SAlan Cox module_param_named(allow_tpm, libata_allow_tpm, int, 0444);
152ad5d8eacSEvgeni Golov MODULE_PARM_DESC(allow_tpm, "Permit the use of TPM commands (0=off [default], 1=on)");
153ae8d4ee7SAlan Cox 
154e7ecd435STejun Heo static int atapi_an;
155e7ecd435STejun Heo module_param(atapi_an, int, 0444);
156e7ecd435STejun Heo MODULE_PARM_DESC(atapi_an, "Enable ATAPI AN media presence notification (0=0ff [default], 1=on)");
157e7ecd435STejun Heo 
158c6fd2807SJeff Garzik MODULE_AUTHOR("Jeff Garzik");
159c6fd2807SJeff Garzik MODULE_DESCRIPTION("Library module for ATA devices");
160c6fd2807SJeff Garzik MODULE_LICENSE("GPL");
161c6fd2807SJeff Garzik MODULE_VERSION(DRV_VERSION);
162c6fd2807SJeff Garzik 
163891fd7c6SDamien Le Moal static inline bool ata_dev_print_info(struct ata_device *dev)
164891fd7c6SDamien Le Moal {
165891fd7c6SDamien Le Moal 	struct ata_eh_context *ehc = &dev->link->eh_context;
166891fd7c6SDamien Le Moal 
167891fd7c6SDamien Le Moal 	return ehc->i.flags & ATA_EHI_PRINTINFO;
168891fd7c6SDamien Le Moal }
169c6fd2807SJeff Garzik 
1709913ff8aSTejun Heo static bool ata_sstatus_online(u32 sstatus)
1719913ff8aSTejun Heo {
1729913ff8aSTejun Heo 	return (sstatus & 0xf) == 0x3;
1739913ff8aSTejun Heo }
1749913ff8aSTejun Heo 
1751eca4365STejun Heo /**
1761eca4365STejun Heo  *	ata_link_next - link iteration helper
1771eca4365STejun Heo  *	@link: the previous link, NULL to start
1781eca4365STejun Heo  *	@ap: ATA port containing links to iterate
1791eca4365STejun Heo  *	@mode: iteration mode, one of ATA_LITER_*
180aadffb68STejun Heo  *
181aadffb68STejun Heo  *	LOCKING:
182aadffb68STejun Heo  *	Host lock or EH context.
1831eca4365STejun Heo  *
1841eca4365STejun Heo  *	RETURNS:
1851eca4365STejun Heo  *	Pointer to the next link.
186aadffb68STejun Heo  */
1871eca4365STejun Heo struct ata_link *ata_link_next(struct ata_link *link, struct ata_port *ap,
1881eca4365STejun Heo 			       enum ata_link_iter_mode mode)
189aadffb68STejun Heo {
1901eca4365STejun Heo 	BUG_ON(mode != ATA_LITER_EDGE &&
1911eca4365STejun Heo 	       mode != ATA_LITER_PMP_FIRST && mode != ATA_LITER_HOST_FIRST);
1921eca4365STejun Heo 
193aadffb68STejun Heo 	/* NULL link indicates start of iteration */
1941eca4365STejun Heo 	if (!link)
1951eca4365STejun Heo 		switch (mode) {
1961eca4365STejun Heo 		case ATA_LITER_EDGE:
1971eca4365STejun Heo 		case ATA_LITER_PMP_FIRST:
1981eca4365STejun Heo 			if (sata_pmp_attached(ap))
199aadffb68STejun Heo 				return ap->pmp_link;
200df561f66SGustavo A. R. Silva 			fallthrough;
2011eca4365STejun Heo 		case ATA_LITER_HOST_FIRST:
202aadffb68STejun Heo 			return &ap->link;
203aadffb68STejun Heo 		}
204aadffb68STejun Heo 
2051eca4365STejun Heo 	/* we just iterated over the host link, what's next? */
2061eca4365STejun Heo 	if (link == &ap->link)
2071eca4365STejun Heo 		switch (mode) {
2081eca4365STejun Heo 		case ATA_LITER_HOST_FIRST:
2091eca4365STejun Heo 			if (sata_pmp_attached(ap))
210aadffb68STejun Heo 				return ap->pmp_link;
211df561f66SGustavo A. R. Silva 			fallthrough;
2121eca4365STejun Heo 		case ATA_LITER_PMP_FIRST:
2131eca4365STejun Heo 			if (unlikely(ap->slave_link))
2141eca4365STejun Heo 				return ap->slave_link;
215df561f66SGustavo A. R. Silva 			fallthrough;
2161eca4365STejun Heo 		case ATA_LITER_EDGE:
2171eca4365STejun Heo 			return NULL;
218aadffb68STejun Heo 		}
219aadffb68STejun Heo 
220b1c72916STejun Heo 	/* slave_link excludes PMP */
221b1c72916STejun Heo 	if (unlikely(link == ap->slave_link))
222b1c72916STejun Heo 		return NULL;
223b1c72916STejun Heo 
2241eca4365STejun Heo 	/* we were over a PMP link */
225aadffb68STejun Heo 	if (++link < ap->pmp_link + ap->nr_pmp_links)
226aadffb68STejun Heo 		return link;
2271eca4365STejun Heo 
2281eca4365STejun Heo 	if (mode == ATA_LITER_PMP_FIRST)
2291eca4365STejun Heo 		return &ap->link;
2301eca4365STejun Heo 
231aadffb68STejun Heo 	return NULL;
232aadffb68STejun Heo }
233a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_link_next);
234aadffb68STejun Heo 
235c6fd2807SJeff Garzik /**
2361eca4365STejun Heo  *	ata_dev_next - device iteration helper
2371eca4365STejun Heo  *	@dev: the previous device, NULL to start
2381eca4365STejun Heo  *	@link: ATA link containing devices to iterate
2391eca4365STejun Heo  *	@mode: iteration mode, one of ATA_DITER_*
2401eca4365STejun Heo  *
2411eca4365STejun Heo  *	LOCKING:
2421eca4365STejun Heo  *	Host lock or EH context.
2431eca4365STejun Heo  *
2441eca4365STejun Heo  *	RETURNS:
2451eca4365STejun Heo  *	Pointer to the next device.
2461eca4365STejun Heo  */
2471eca4365STejun Heo struct ata_device *ata_dev_next(struct ata_device *dev, struct ata_link *link,
2481eca4365STejun Heo 				enum ata_dev_iter_mode mode)
2491eca4365STejun Heo {
2501eca4365STejun Heo 	BUG_ON(mode != ATA_DITER_ENABLED && mode != ATA_DITER_ENABLED_REVERSE &&
2511eca4365STejun Heo 	       mode != ATA_DITER_ALL && mode != ATA_DITER_ALL_REVERSE);
2521eca4365STejun Heo 
2531eca4365STejun Heo 	/* NULL dev indicates start of iteration */
2541eca4365STejun Heo 	if (!dev)
2551eca4365STejun Heo 		switch (mode) {
2561eca4365STejun Heo 		case ATA_DITER_ENABLED:
2571eca4365STejun Heo 		case ATA_DITER_ALL:
2581eca4365STejun Heo 			dev = link->device;
2591eca4365STejun Heo 			goto check;
2601eca4365STejun Heo 		case ATA_DITER_ENABLED_REVERSE:
2611eca4365STejun Heo 		case ATA_DITER_ALL_REVERSE:
2621eca4365STejun Heo 			dev = link->device + ata_link_max_devices(link) - 1;
2631eca4365STejun Heo 			goto check;
2641eca4365STejun Heo 		}
2651eca4365STejun Heo 
2661eca4365STejun Heo  next:
2671eca4365STejun Heo 	/* move to the next one */
2681eca4365STejun Heo 	switch (mode) {
2691eca4365STejun Heo 	case ATA_DITER_ENABLED:
2701eca4365STejun Heo 	case ATA_DITER_ALL:
2711eca4365STejun Heo 		if (++dev < link->device + ata_link_max_devices(link))
2721eca4365STejun Heo 			goto check;
2731eca4365STejun Heo 		return NULL;
2741eca4365STejun Heo 	case ATA_DITER_ENABLED_REVERSE:
2751eca4365STejun Heo 	case ATA_DITER_ALL_REVERSE:
2761eca4365STejun Heo 		if (--dev >= link->device)
2771eca4365STejun Heo 			goto check;
2781eca4365STejun Heo 		return NULL;
2791eca4365STejun Heo 	}
2801eca4365STejun Heo 
2811eca4365STejun Heo  check:
2821eca4365STejun Heo 	if ((mode == ATA_DITER_ENABLED || mode == ATA_DITER_ENABLED_REVERSE) &&
2831eca4365STejun Heo 	    !ata_dev_enabled(dev))
2841eca4365STejun Heo 		goto next;
2851eca4365STejun Heo 	return dev;
2861eca4365STejun Heo }
287a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_dev_next);
2881eca4365STejun Heo 
2891eca4365STejun Heo /**
290b1c72916STejun Heo  *	ata_dev_phys_link - find physical link for a device
291b1c72916STejun Heo  *	@dev: ATA device to look up physical link for
292b1c72916STejun Heo  *
293b1c72916STejun Heo  *	Look up physical link which @dev is attached to.  Note that
294b1c72916STejun Heo  *	this is different from @dev->link only when @dev is on slave
295b1c72916STejun Heo  *	link.  For all other cases, it's the same as @dev->link.
296b1c72916STejun Heo  *
297b1c72916STejun Heo  *	LOCKING:
298b1c72916STejun Heo  *	Don't care.
299b1c72916STejun Heo  *
300b1c72916STejun Heo  *	RETURNS:
301b1c72916STejun Heo  *	Pointer to the found physical link.
302b1c72916STejun Heo  */
303b1c72916STejun Heo struct ata_link *ata_dev_phys_link(struct ata_device *dev)
304b1c72916STejun Heo {
305b1c72916STejun Heo 	struct ata_port *ap = dev->link->ap;
306b1c72916STejun Heo 
307b1c72916STejun Heo 	if (!ap->slave_link)
308b1c72916STejun Heo 		return dev->link;
309b1c72916STejun Heo 	if (!dev->devno)
310b1c72916STejun Heo 		return &ap->link;
311b1c72916STejun Heo 	return ap->slave_link;
312b1c72916STejun Heo }
313b1c72916STejun Heo 
314bf89b0bfSBartlomiej Zolnierkiewicz #ifdef CONFIG_ATA_FORCE
315b1c72916STejun Heo /**
31633267325STejun Heo  *	ata_force_cbl - force cable type according to libata.force
3174cdfa1b3SRandy Dunlap  *	@ap: ATA port of interest
31833267325STejun Heo  *
31933267325STejun Heo  *	Force cable type according to libata.force and whine about it.
32033267325STejun Heo  *	The last entry which has matching port number is used, so it
32133267325STejun Heo  *	can be specified as part of device force parameters.  For
32233267325STejun Heo  *	example, both "a:40c,1.00:udma4" and "1.00:40c,udma4" have the
32333267325STejun Heo  *	same effect.
32433267325STejun Heo  *
32533267325STejun Heo  *	LOCKING:
32633267325STejun Heo  *	EH context.
32733267325STejun Heo  */
32833267325STejun Heo void ata_force_cbl(struct ata_port *ap)
32933267325STejun Heo {
33033267325STejun Heo 	int i;
33133267325STejun Heo 
33233267325STejun Heo 	for (i = ata_force_tbl_size - 1; i >= 0; i--) {
33333267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
33433267325STejun Heo 
33533267325STejun Heo 		if (fe->port != -1 && fe->port != ap->print_id)
33633267325STejun Heo 			continue;
33733267325STejun Heo 
33833267325STejun Heo 		if (fe->param.cbl == ATA_CBL_NONE)
33933267325STejun Heo 			continue;
34033267325STejun Heo 
34133267325STejun Heo 		ap->cbl = fe->param.cbl;
342a9a79dfeSJoe Perches 		ata_port_notice(ap, "FORCE: cable set to %s\n", fe->param.name);
34333267325STejun Heo 		return;
34433267325STejun Heo 	}
34533267325STejun Heo }
34633267325STejun Heo 
34733267325STejun Heo /**
34805944bdfSTejun Heo  *	ata_force_link_limits - force link limits according to libata.force
34933267325STejun Heo  *	@link: ATA link of interest
35033267325STejun Heo  *
35105944bdfSTejun Heo  *	Force link flags and SATA spd limit according to libata.force
35205944bdfSTejun Heo  *	and whine about it.  When only the port part is specified
35305944bdfSTejun Heo  *	(e.g. 1:), the limit applies to all links connected to both
35405944bdfSTejun Heo  *	the host link and all fan-out ports connected via PMP.  If the
35505944bdfSTejun Heo  *	device part is specified as 0 (e.g. 1.00:), it specifies the
35605944bdfSTejun Heo  *	first fan-out link not the host link.  Device number 15 always
357b1c72916STejun Heo  *	points to the host link whether PMP is attached or not.  If the
358b1c72916STejun Heo  *	controller has slave link, device number 16 points to it.
35933267325STejun Heo  *
36033267325STejun Heo  *	LOCKING:
36133267325STejun Heo  *	EH context.
36233267325STejun Heo  */
36305944bdfSTejun Heo static void ata_force_link_limits(struct ata_link *link)
36433267325STejun Heo {
36505944bdfSTejun Heo 	bool did_spd = false;
366b1c72916STejun Heo 	int linkno = link->pmp;
367b1c72916STejun Heo 	int i;
36833267325STejun Heo 
36933267325STejun Heo 	if (ata_is_host_link(link))
370b1c72916STejun Heo 		linkno += 15;
37133267325STejun Heo 
37233267325STejun Heo 	for (i = ata_force_tbl_size - 1; i >= 0; i--) {
37333267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
37433267325STejun Heo 
37533267325STejun Heo 		if (fe->port != -1 && fe->port != link->ap->print_id)
37633267325STejun Heo 			continue;
37733267325STejun Heo 
37833267325STejun Heo 		if (fe->device != -1 && fe->device != linkno)
37933267325STejun Heo 			continue;
38033267325STejun Heo 
38105944bdfSTejun Heo 		/* only honor the first spd limit */
38205944bdfSTejun Heo 		if (!did_spd && fe->param.spd_limit) {
38333267325STejun Heo 			link->hw_sata_spd_limit = (1 << fe->param.spd_limit) - 1;
384a9a79dfeSJoe Perches 			ata_link_notice(link, "FORCE: PHY spd limit set to %s\n",
38505944bdfSTejun Heo 					fe->param.name);
38605944bdfSTejun Heo 			did_spd = true;
38705944bdfSTejun Heo 		}
38805944bdfSTejun Heo 
38905944bdfSTejun Heo 		/* let lflags stack */
3903af9ca4dSDamien Le Moal 		if (fe->param.lflags_on) {
3913af9ca4dSDamien Le Moal 			link->flags |= fe->param.lflags_on;
392a9a79dfeSJoe Perches 			ata_link_notice(link,
39305944bdfSTejun Heo 					"FORCE: link flag 0x%x forced -> 0x%x\n",
3943af9ca4dSDamien Le Moal 					fe->param.lflags_on, link->flags);
3953af9ca4dSDamien Le Moal 		}
3963af9ca4dSDamien Le Moal 		if (fe->param.lflags_off) {
3973af9ca4dSDamien Le Moal 			link->flags &= ~fe->param.lflags_off;
3983af9ca4dSDamien Le Moal 			ata_link_notice(link,
3993af9ca4dSDamien Le Moal 				"FORCE: link flag 0x%x cleared -> 0x%x\n",
4003af9ca4dSDamien Le Moal 				fe->param.lflags_off, link->flags);
40105944bdfSTejun Heo 		}
40233267325STejun Heo 	}
40333267325STejun Heo }
40433267325STejun Heo 
40533267325STejun Heo /**
40633267325STejun Heo  *	ata_force_xfermask - force xfermask according to libata.force
40733267325STejun Heo  *	@dev: ATA device of interest
40833267325STejun Heo  *
40933267325STejun Heo  *	Force xfer_mask according to libata.force and whine about it.
41033267325STejun Heo  *	For consistency with link selection, device number 15 selects
41133267325STejun Heo  *	the first device connected to the host link.
41233267325STejun Heo  *
41333267325STejun Heo  *	LOCKING:
41433267325STejun Heo  *	EH context.
41533267325STejun Heo  */
41633267325STejun Heo static void ata_force_xfermask(struct ata_device *dev)
41733267325STejun Heo {
41833267325STejun Heo 	int devno = dev->link->pmp + dev->devno;
41933267325STejun Heo 	int alt_devno = devno;
42033267325STejun Heo 	int i;
42133267325STejun Heo 
422b1c72916STejun Heo 	/* allow n.15/16 for devices attached to host port */
423b1c72916STejun Heo 	if (ata_is_host_link(dev->link))
424b1c72916STejun Heo 		alt_devno += 15;
42533267325STejun Heo 
42633267325STejun Heo 	for (i = ata_force_tbl_size - 1; i >= 0; i--) {
42733267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
428f0a6d77bSSergey Shtylyov 		unsigned int pio_mask, mwdma_mask, udma_mask;
42933267325STejun Heo 
43033267325STejun Heo 		if (fe->port != -1 && fe->port != dev->link->ap->print_id)
43133267325STejun Heo 			continue;
43233267325STejun Heo 
43333267325STejun Heo 		if (fe->device != -1 && fe->device != devno &&
43433267325STejun Heo 		    fe->device != alt_devno)
43533267325STejun Heo 			continue;
43633267325STejun Heo 
43733267325STejun Heo 		if (!fe->param.xfer_mask)
43833267325STejun Heo 			continue;
43933267325STejun Heo 
44033267325STejun Heo 		ata_unpack_xfermask(fe->param.xfer_mask,
44133267325STejun Heo 				    &pio_mask, &mwdma_mask, &udma_mask);
44233267325STejun Heo 		if (udma_mask)
44333267325STejun Heo 			dev->udma_mask = udma_mask;
44433267325STejun Heo 		else if (mwdma_mask) {
44533267325STejun Heo 			dev->udma_mask = 0;
44633267325STejun Heo 			dev->mwdma_mask = mwdma_mask;
44733267325STejun Heo 		} else {
44833267325STejun Heo 			dev->udma_mask = 0;
44933267325STejun Heo 			dev->mwdma_mask = 0;
45033267325STejun Heo 			dev->pio_mask = pio_mask;
45133267325STejun Heo 		}
45233267325STejun Heo 
453a9a79dfeSJoe Perches 		ata_dev_notice(dev, "FORCE: xfer_mask set to %s\n",
454a9a79dfeSJoe Perches 			       fe->param.name);
45533267325STejun Heo 		return;
45633267325STejun Heo 	}
45733267325STejun Heo }
45833267325STejun Heo 
45933267325STejun Heo /**
46033267325STejun Heo  *	ata_force_horkage - force horkage according to libata.force
46133267325STejun Heo  *	@dev: ATA device of interest
46233267325STejun Heo  *
46333267325STejun Heo  *	Force horkage according to libata.force and whine about it.
46433267325STejun Heo  *	For consistency with link selection, device number 15 selects
46533267325STejun Heo  *	the first device connected to the host link.
46633267325STejun Heo  *
46733267325STejun Heo  *	LOCKING:
46833267325STejun Heo  *	EH context.
46933267325STejun Heo  */
47033267325STejun Heo static void ata_force_horkage(struct ata_device *dev)
47133267325STejun Heo {
47233267325STejun Heo 	int devno = dev->link->pmp + dev->devno;
47333267325STejun Heo 	int alt_devno = devno;
47433267325STejun Heo 	int i;
47533267325STejun Heo 
476b1c72916STejun Heo 	/* allow n.15/16 for devices attached to host port */
477b1c72916STejun Heo 	if (ata_is_host_link(dev->link))
478b1c72916STejun Heo 		alt_devno += 15;
47933267325STejun Heo 
48033267325STejun Heo 	for (i = 0; i < ata_force_tbl_size; i++) {
48133267325STejun Heo 		const struct ata_force_ent *fe = &ata_force_tbl[i];
48233267325STejun Heo 
48333267325STejun Heo 		if (fe->port != -1 && fe->port != dev->link->ap->print_id)
48433267325STejun Heo 			continue;
48533267325STejun Heo 
48633267325STejun Heo 		if (fe->device != -1 && fe->device != devno &&
48733267325STejun Heo 		    fe->device != alt_devno)
48833267325STejun Heo 			continue;
48933267325STejun Heo 
49033267325STejun Heo 		if (!(~dev->horkage & fe->param.horkage_on) &&
49133267325STejun Heo 		    !(dev->horkage & fe->param.horkage_off))
49233267325STejun Heo 			continue;
49333267325STejun Heo 
49433267325STejun Heo 		dev->horkage |= fe->param.horkage_on;
49533267325STejun Heo 		dev->horkage &= ~fe->param.horkage_off;
49633267325STejun Heo 
497a9a79dfeSJoe Perches 		ata_dev_notice(dev, "FORCE: horkage modified (%s)\n",
498a9a79dfeSJoe Perches 			       fe->param.name);
49933267325STejun Heo 	}
50033267325STejun Heo }
501bf89b0bfSBartlomiej Zolnierkiewicz #else
502bf89b0bfSBartlomiej Zolnierkiewicz static inline void ata_force_link_limits(struct ata_link *link) { }
503bf89b0bfSBartlomiej Zolnierkiewicz static inline void ata_force_xfermask(struct ata_device *dev) { }
504bf89b0bfSBartlomiej Zolnierkiewicz static inline void ata_force_horkage(struct ata_device *dev) { }
505bf89b0bfSBartlomiej Zolnierkiewicz #endif
50633267325STejun Heo 
50733267325STejun Heo /**
508436d34b3STejun Heo  *	atapi_cmd_type - Determine ATAPI command type from SCSI opcode
509436d34b3STejun Heo  *	@opcode: SCSI opcode
510436d34b3STejun Heo  *
511436d34b3STejun Heo  *	Determine ATAPI command type from @opcode.
512436d34b3STejun Heo  *
513436d34b3STejun Heo  *	LOCKING:
514436d34b3STejun Heo  *	None.
515436d34b3STejun Heo  *
516436d34b3STejun Heo  *	RETURNS:
517436d34b3STejun Heo  *	ATAPI_{READ|WRITE|READ_CD|PASS_THRU|MISC}
518436d34b3STejun Heo  */
519436d34b3STejun Heo int atapi_cmd_type(u8 opcode)
520436d34b3STejun Heo {
521436d34b3STejun Heo 	switch (opcode) {
522436d34b3STejun Heo 	case GPCMD_READ_10:
523436d34b3STejun Heo 	case GPCMD_READ_12:
524436d34b3STejun Heo 		return ATAPI_READ;
525436d34b3STejun Heo 
526436d34b3STejun Heo 	case GPCMD_WRITE_10:
527436d34b3STejun Heo 	case GPCMD_WRITE_12:
528436d34b3STejun Heo 	case GPCMD_WRITE_AND_VERIFY_10:
529436d34b3STejun Heo 		return ATAPI_WRITE;
530436d34b3STejun Heo 
531436d34b3STejun Heo 	case GPCMD_READ_CD:
532436d34b3STejun Heo 	case GPCMD_READ_CD_MSF:
533436d34b3STejun Heo 		return ATAPI_READ_CD;
534436d34b3STejun Heo 
535e52dcc48STejun Heo 	case ATA_16:
536e52dcc48STejun Heo 	case ATA_12:
537e52dcc48STejun Heo 		if (atapi_passthru16)
538e52dcc48STejun Heo 			return ATAPI_PASS_THRU;
539df561f66SGustavo A. R. Silva 		fallthrough;
540436d34b3STejun Heo 	default:
541436d34b3STejun Heo 		return ATAPI_MISC;
542436d34b3STejun Heo 	}
543436d34b3STejun Heo }
544a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(atapi_cmd_type);
545436d34b3STejun Heo 
546c6fd2807SJeff Garzik static const u8 ata_rw_cmds[] = {
547c6fd2807SJeff Garzik 	/* pio multi */
548c6fd2807SJeff Garzik 	ATA_CMD_READ_MULTI,
549c6fd2807SJeff Garzik 	ATA_CMD_WRITE_MULTI,
550c6fd2807SJeff Garzik 	ATA_CMD_READ_MULTI_EXT,
551c6fd2807SJeff Garzik 	ATA_CMD_WRITE_MULTI_EXT,
552c6fd2807SJeff Garzik 	0,
553c6fd2807SJeff Garzik 	0,
554c6fd2807SJeff Garzik 	0,
555bc9af490SDamien Le Moal 	0,
556c6fd2807SJeff Garzik 	/* pio */
557c6fd2807SJeff Garzik 	ATA_CMD_PIO_READ,
558c6fd2807SJeff Garzik 	ATA_CMD_PIO_WRITE,
559c6fd2807SJeff Garzik 	ATA_CMD_PIO_READ_EXT,
560c6fd2807SJeff Garzik 	ATA_CMD_PIO_WRITE_EXT,
561c6fd2807SJeff Garzik 	0,
562c6fd2807SJeff Garzik 	0,
563c6fd2807SJeff Garzik 	0,
564c6fd2807SJeff Garzik 	0,
565c6fd2807SJeff Garzik 	/* dma */
566c6fd2807SJeff Garzik 	ATA_CMD_READ,
567c6fd2807SJeff Garzik 	ATA_CMD_WRITE,
568c6fd2807SJeff Garzik 	ATA_CMD_READ_EXT,
569c6fd2807SJeff Garzik 	ATA_CMD_WRITE_EXT,
570c6fd2807SJeff Garzik 	0,
571c6fd2807SJeff Garzik 	0,
572c6fd2807SJeff Garzik 	0,
573c6fd2807SJeff Garzik 	ATA_CMD_WRITE_FUA_EXT
574c6fd2807SJeff Garzik };
575c6fd2807SJeff Garzik 
576c6fd2807SJeff Garzik /**
57777839debSDamien Le Moal  *	ata_set_rwcmd_protocol - set taskfile r/w command and protocol
57877839debSDamien Le Moal  *	@dev: target device for the taskfile
57977839debSDamien Le Moal  *	@tf: taskfile to examine and configure
580c6fd2807SJeff Garzik  *
58177839debSDamien Le Moal  *	Examine the device configuration and tf->flags to determine
58277839debSDamien Le Moal  *	the proper read/write command and protocol to use for @tf.
583c6fd2807SJeff Garzik  *
584c6fd2807SJeff Garzik  *	LOCKING:
585c6fd2807SJeff Garzik  *	caller.
586c6fd2807SJeff Garzik  */
58777839debSDamien Le Moal static bool ata_set_rwcmd_protocol(struct ata_device *dev,
58877839debSDamien Le Moal 				   struct ata_taskfile *tf)
589c6fd2807SJeff Garzik {
590c6fd2807SJeff Garzik 	u8 cmd;
591c6fd2807SJeff Garzik 
592c6fd2807SJeff Garzik 	int index, fua, lba48, write;
593c6fd2807SJeff Garzik 
594c6fd2807SJeff Garzik 	fua = (tf->flags & ATA_TFLAG_FUA) ? 4 : 0;
595c6fd2807SJeff Garzik 	lba48 = (tf->flags & ATA_TFLAG_LBA48) ? 2 : 0;
596c6fd2807SJeff Garzik 	write = (tf->flags & ATA_TFLAG_WRITE) ? 1 : 0;
597c6fd2807SJeff Garzik 
598c6fd2807SJeff Garzik 	if (dev->flags & ATA_DFLAG_PIO) {
599c6fd2807SJeff Garzik 		tf->protocol = ATA_PROT_PIO;
600c6fd2807SJeff Garzik 		index = dev->multi_count ? 0 : 8;
6019af5c9c9STejun Heo 	} else if (lba48 && (dev->link->ap->flags & ATA_FLAG_PIO_LBA48)) {
602c6fd2807SJeff Garzik 		/* Unable to use DMA due to host limitation */
603c6fd2807SJeff Garzik 		tf->protocol = ATA_PROT_PIO;
604c6fd2807SJeff Garzik 		index = dev->multi_count ? 0 : 8;
605c6fd2807SJeff Garzik 	} else {
606c6fd2807SJeff Garzik 		tf->protocol = ATA_PROT_DMA;
607c6fd2807SJeff Garzik 		index = 16;
608c6fd2807SJeff Garzik 	}
609c6fd2807SJeff Garzik 
610c6fd2807SJeff Garzik 	cmd = ata_rw_cmds[index + fua + lba48 + write];
61177839debSDamien Le Moal 	if (!cmd)
61277839debSDamien Le Moal 		return false;
61377839debSDamien Le Moal 
614c6fd2807SJeff Garzik 	tf->command = cmd;
61577839debSDamien Le Moal 
61677839debSDamien Le Moal 	return true;
617c6fd2807SJeff Garzik }
618c6fd2807SJeff Garzik 
619c6fd2807SJeff Garzik /**
62035b649feSTejun Heo  *	ata_tf_read_block - Read block address from ATA taskfile
62135b649feSTejun Heo  *	@tf: ATA taskfile of interest
62235b649feSTejun Heo  *	@dev: ATA device @tf belongs to
62335b649feSTejun Heo  *
62435b649feSTejun Heo  *	LOCKING:
62535b649feSTejun Heo  *	None.
62635b649feSTejun Heo  *
62735b649feSTejun Heo  *	Read block address from @tf.  This function can handle all
62835b649feSTejun Heo  *	three address formats - LBA, LBA48 and CHS.  tf->protocol and
62935b649feSTejun Heo  *	flags select the address format to use.
63035b649feSTejun Heo  *
63135b649feSTejun Heo  *	RETURNS:
63235b649feSTejun Heo  *	Block address read from @tf.
63335b649feSTejun Heo  */
634cffd1ee9SHannes Reinecke u64 ata_tf_read_block(const struct ata_taskfile *tf, struct ata_device *dev)
63535b649feSTejun Heo {
63635b649feSTejun Heo 	u64 block = 0;
63735b649feSTejun Heo 
638fe16d4f2STejun Heo 	if (tf->flags & ATA_TFLAG_LBA) {
63935b649feSTejun Heo 		if (tf->flags & ATA_TFLAG_LBA48) {
64035b649feSTejun Heo 			block |= (u64)tf->hob_lbah << 40;
64135b649feSTejun Heo 			block |= (u64)tf->hob_lbam << 32;
64244901a96SRoland Dreier 			block |= (u64)tf->hob_lbal << 24;
64335b649feSTejun Heo 		} else
64435b649feSTejun Heo 			block |= (tf->device & 0xf) << 24;
64535b649feSTejun Heo 
64635b649feSTejun Heo 		block |= tf->lbah << 16;
64735b649feSTejun Heo 		block |= tf->lbam << 8;
64835b649feSTejun Heo 		block |= tf->lbal;
64935b649feSTejun Heo 	} else {
65035b649feSTejun Heo 		u32 cyl, head, sect;
65135b649feSTejun Heo 
65235b649feSTejun Heo 		cyl = tf->lbam | (tf->lbah << 8);
65335b649feSTejun Heo 		head = tf->device & 0xf;
65435b649feSTejun Heo 		sect = tf->lbal;
65535b649feSTejun Heo 
656ac8672eaSTejun Heo 		if (!sect) {
657a9a79dfeSJoe Perches 			ata_dev_warn(dev,
658a9a79dfeSJoe Perches 				     "device reported invalid CHS sector 0\n");
659cffd1ee9SHannes Reinecke 			return U64_MAX;
660ac8672eaSTejun Heo 		}
661ac8672eaSTejun Heo 
662ac8672eaSTejun Heo 		block = (cyl * dev->heads + head) * dev->sectors + sect - 1;
66335b649feSTejun Heo 	}
66435b649feSTejun Heo 
66535b649feSTejun Heo 	return block;
66635b649feSTejun Heo }
66735b649feSTejun Heo 
66835b649feSTejun Heo /**
669bd056d7eSTejun Heo  *	ata_build_rw_tf - Build ATA taskfile for given read/write request
670066de3b9SDamien Le Moal  *	@qc: Metadata associated with the taskfile to build
671bd056d7eSTejun Heo  *	@block: Block address
672bd056d7eSTejun Heo  *	@n_block: Number of blocks
673bd056d7eSTejun Heo  *	@tf_flags: RW/FUA etc...
6748e061784SAdam Manzanares  *	@class: IO priority class
675bd056d7eSTejun Heo  *
676bd056d7eSTejun Heo  *	LOCKING:
677bd056d7eSTejun Heo  *	None.
678bd056d7eSTejun Heo  *
679066de3b9SDamien Le Moal  *	Build ATA taskfile for the command @qc for read/write request described
680066de3b9SDamien Le Moal  *	by @block, @n_block, @tf_flags and @class.
681bd056d7eSTejun Heo  *
682bd056d7eSTejun Heo  *	RETURNS:
683bd056d7eSTejun Heo  *
684bd056d7eSTejun Heo  *	0 on success, -ERANGE if the request is too large for @dev,
685bd056d7eSTejun Heo  *	-EINVAL if the request is invalid.
686bd056d7eSTejun Heo  */
687066de3b9SDamien Le Moal int ata_build_rw_tf(struct ata_queued_cmd *qc, u64 block, u32 n_block,
688066de3b9SDamien Le Moal 		    unsigned int tf_flags, int class)
689bd056d7eSTejun Heo {
690066de3b9SDamien Le Moal 	struct ata_taskfile *tf = &qc->tf;
691066de3b9SDamien Le Moal 	struct ata_device *dev = qc->dev;
692066de3b9SDamien Le Moal 
693bd056d7eSTejun Heo 	tf->flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
694bd056d7eSTejun Heo 	tf->flags |= tf_flags;
695bd056d7eSTejun Heo 
696066de3b9SDamien Le Moal 	if (ata_ncq_enabled(dev)) {
697bd056d7eSTejun Heo 		/* yay, NCQ */
698bd056d7eSTejun Heo 		if (!lba_48_ok(block, n_block))
699bd056d7eSTejun Heo 			return -ERANGE;
700bd056d7eSTejun Heo 
701bd056d7eSTejun Heo 		tf->protocol = ATA_PROT_NCQ;
702bd056d7eSTejun Heo 		tf->flags |= ATA_TFLAG_LBA | ATA_TFLAG_LBA48;
703bd056d7eSTejun Heo 
704bd056d7eSTejun Heo 		if (tf->flags & ATA_TFLAG_WRITE)
705bd056d7eSTejun Heo 			tf->command = ATA_CMD_FPDMA_WRITE;
706bd056d7eSTejun Heo 		else
707bd056d7eSTejun Heo 			tf->command = ATA_CMD_FPDMA_READ;
708bd056d7eSTejun Heo 
709066de3b9SDamien Le Moal 		tf->nsect = qc->hw_tag << 3;
710bd056d7eSTejun Heo 		tf->hob_feature = (n_block >> 8) & 0xff;
711bd056d7eSTejun Heo 		tf->feature = n_block & 0xff;
712bd056d7eSTejun Heo 
713bd056d7eSTejun Heo 		tf->hob_lbah = (block >> 40) & 0xff;
714bd056d7eSTejun Heo 		tf->hob_lbam = (block >> 32) & 0xff;
715bd056d7eSTejun Heo 		tf->hob_lbal = (block >> 24) & 0xff;
716bd056d7eSTejun Heo 		tf->lbah = (block >> 16) & 0xff;
717bd056d7eSTejun Heo 		tf->lbam = (block >> 8) & 0xff;
718bd056d7eSTejun Heo 		tf->lbal = block & 0xff;
719bd056d7eSTejun Heo 
7209ca7cfa4SSergei Shtylyov 		tf->device = ATA_LBA;
721bd056d7eSTejun Heo 		if (tf->flags & ATA_TFLAG_FUA)
722bd056d7eSTejun Heo 			tf->device |= 1 << 7;
7238e061784SAdam Manzanares 
724e00923c5SDamien Le Moal 		if (dev->flags & ATA_DFLAG_NCQ_PRIO_ENABLED &&
7252360fa18SDamien Le Moal 		    class == IOPRIO_CLASS_RT)
7262360fa18SDamien Le Moal 			tf->hob_nsect |= ATA_PRIO_HIGH << ATA_SHIFT_PRIO;
727bd056d7eSTejun Heo 	} else if (dev->flags & ATA_DFLAG_LBA) {
728bd056d7eSTejun Heo 		tf->flags |= ATA_TFLAG_LBA;
729bd056d7eSTejun Heo 
730bc9af490SDamien Le Moal 		/* We need LBA48 for FUA writes */
731bc9af490SDamien Le Moal 		if (!(tf->flags & ATA_TFLAG_FUA) && lba_28_ok(block, n_block)) {
732bd056d7eSTejun Heo 			/* use LBA28 */
733bd056d7eSTejun Heo 			tf->device |= (block >> 24) & 0xf;
734bd056d7eSTejun Heo 		} else if (lba_48_ok(block, n_block)) {
735bd056d7eSTejun Heo 			if (!(dev->flags & ATA_DFLAG_LBA48))
736bd056d7eSTejun Heo 				return -ERANGE;
737bd056d7eSTejun Heo 
738bd056d7eSTejun Heo 			/* use LBA48 */
739bd056d7eSTejun Heo 			tf->flags |= ATA_TFLAG_LBA48;
740bd056d7eSTejun Heo 
741bd056d7eSTejun Heo 			tf->hob_nsect = (n_block >> 8) & 0xff;
742bd056d7eSTejun Heo 
743bd056d7eSTejun Heo 			tf->hob_lbah = (block >> 40) & 0xff;
744bd056d7eSTejun Heo 			tf->hob_lbam = (block >> 32) & 0xff;
745bd056d7eSTejun Heo 			tf->hob_lbal = (block >> 24) & 0xff;
746bc9af490SDamien Le Moal 		} else {
747bd056d7eSTejun Heo 			/* request too large even for LBA48 */
748bd056d7eSTejun Heo 			return -ERANGE;
749bc9af490SDamien Le Moal 		}
750bd056d7eSTejun Heo 
75177839debSDamien Le Moal 		if (unlikely(!ata_set_rwcmd_protocol(dev, tf)))
752bd056d7eSTejun Heo 			return -EINVAL;
753bd056d7eSTejun Heo 
754bd056d7eSTejun Heo 		tf->nsect = n_block & 0xff;
755bd056d7eSTejun Heo 
756bd056d7eSTejun Heo 		tf->lbah = (block >> 16) & 0xff;
757bd056d7eSTejun Heo 		tf->lbam = (block >> 8) & 0xff;
758bd056d7eSTejun Heo 		tf->lbal = block & 0xff;
759bd056d7eSTejun Heo 
760bd056d7eSTejun Heo 		tf->device |= ATA_LBA;
761bd056d7eSTejun Heo 	} else {
762bd056d7eSTejun Heo 		/* CHS */
763bd056d7eSTejun Heo 		u32 sect, head, cyl, track;
764bd056d7eSTejun Heo 
765bd056d7eSTejun Heo 		/* The request -may- be too large for CHS addressing. */
766bd056d7eSTejun Heo 		if (!lba_28_ok(block, n_block))
767bd056d7eSTejun Heo 			return -ERANGE;
768bd056d7eSTejun Heo 
76977839debSDamien Le Moal 		if (unlikely(!ata_set_rwcmd_protocol(dev, tf)))
770bd056d7eSTejun Heo 			return -EINVAL;
771bd056d7eSTejun Heo 
772bd056d7eSTejun Heo 		/* Convert LBA to CHS */
773bd056d7eSTejun Heo 		track = (u32)block / dev->sectors;
774bd056d7eSTejun Heo 		cyl   = track / dev->heads;
775bd056d7eSTejun Heo 		head  = track % dev->heads;
776bd056d7eSTejun Heo 		sect  = (u32)block % dev->sectors + 1;
777bd056d7eSTejun Heo 
778bd056d7eSTejun Heo 		/* Check whether the converted CHS can fit.
779bd056d7eSTejun Heo 		   Cylinder: 0-65535
780bd056d7eSTejun Heo 		   Head: 0-15
781bd056d7eSTejun Heo 		   Sector: 1-255*/
782bd056d7eSTejun Heo 		if ((cyl >> 16) || (head >> 4) || (sect >> 8) || (!sect))
783bd056d7eSTejun Heo 			return -ERANGE;
784bd056d7eSTejun Heo 
785bd056d7eSTejun Heo 		tf->nsect = n_block & 0xff; /* Sector count 0 means 256 sectors */
786bd056d7eSTejun Heo 		tf->lbal = sect;
787bd056d7eSTejun Heo 		tf->lbam = cyl;
788bd056d7eSTejun Heo 		tf->lbah = cyl >> 8;
789bd056d7eSTejun Heo 		tf->device |= head;
790bd056d7eSTejun Heo 	}
791bd056d7eSTejun Heo 
792bd056d7eSTejun Heo 	return 0;
793bd056d7eSTejun Heo }
794bd056d7eSTejun Heo 
795bd056d7eSTejun Heo /**
796c6fd2807SJeff Garzik  *	ata_pack_xfermask - Pack pio, mwdma and udma masks into xfer_mask
797c6fd2807SJeff Garzik  *	@pio_mask: pio_mask
798c6fd2807SJeff Garzik  *	@mwdma_mask: mwdma_mask
799c6fd2807SJeff Garzik  *	@udma_mask: udma_mask
800c6fd2807SJeff Garzik  *
801c6fd2807SJeff Garzik  *	Pack @pio_mask, @mwdma_mask and @udma_mask into a single
802c6fd2807SJeff Garzik  *	unsigned int xfer_mask.
803c6fd2807SJeff Garzik  *
804c6fd2807SJeff Garzik  *	LOCKING:
805c6fd2807SJeff Garzik  *	None.
806c6fd2807SJeff Garzik  *
807c6fd2807SJeff Garzik  *	RETURNS:
808c6fd2807SJeff Garzik  *	Packed xfer_mask.
809c6fd2807SJeff Garzik  */
810f0a6d77bSSergey Shtylyov unsigned int ata_pack_xfermask(unsigned int pio_mask,
811f0a6d77bSSergey Shtylyov 			       unsigned int mwdma_mask,
812f0a6d77bSSergey Shtylyov 			       unsigned int udma_mask)
813c6fd2807SJeff Garzik {
814c6fd2807SJeff Garzik 	return	((pio_mask << ATA_SHIFT_PIO) & ATA_MASK_PIO) |
815c6fd2807SJeff Garzik 		((mwdma_mask << ATA_SHIFT_MWDMA) & ATA_MASK_MWDMA) |
816c6fd2807SJeff Garzik 		((udma_mask << ATA_SHIFT_UDMA) & ATA_MASK_UDMA);
817c6fd2807SJeff Garzik }
818a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pack_xfermask);
819c6fd2807SJeff Garzik 
820c6fd2807SJeff Garzik /**
821c6fd2807SJeff Garzik  *	ata_unpack_xfermask - Unpack xfer_mask into pio, mwdma and udma masks
822c6fd2807SJeff Garzik  *	@xfer_mask: xfer_mask to unpack
823c6fd2807SJeff Garzik  *	@pio_mask: resulting pio_mask
824c6fd2807SJeff Garzik  *	@mwdma_mask: resulting mwdma_mask
825c6fd2807SJeff Garzik  *	@udma_mask: resulting udma_mask
826c6fd2807SJeff Garzik  *
827c6fd2807SJeff Garzik  *	Unpack @xfer_mask into @pio_mask, @mwdma_mask and @udma_mask.
828c9b5560aSMasanari Iida  *	Any NULL destination masks will be ignored.
829c6fd2807SJeff Garzik  */
830f0a6d77bSSergey Shtylyov void ata_unpack_xfermask(unsigned int xfer_mask, unsigned int *pio_mask,
831f0a6d77bSSergey Shtylyov 			 unsigned int *mwdma_mask, unsigned int *udma_mask)
832c6fd2807SJeff Garzik {
833c6fd2807SJeff Garzik 	if (pio_mask)
834c6fd2807SJeff Garzik 		*pio_mask = (xfer_mask & ATA_MASK_PIO) >> ATA_SHIFT_PIO;
835c6fd2807SJeff Garzik 	if (mwdma_mask)
836c6fd2807SJeff Garzik 		*mwdma_mask = (xfer_mask & ATA_MASK_MWDMA) >> ATA_SHIFT_MWDMA;
837c6fd2807SJeff Garzik 	if (udma_mask)
838c6fd2807SJeff Garzik 		*udma_mask = (xfer_mask & ATA_MASK_UDMA) >> ATA_SHIFT_UDMA;
839c6fd2807SJeff Garzik }
840c6fd2807SJeff Garzik 
841c6fd2807SJeff Garzik static const struct ata_xfer_ent {
842c6fd2807SJeff Garzik 	int shift, bits;
843c6fd2807SJeff Garzik 	u8 base;
844c6fd2807SJeff Garzik } ata_xfer_tbl[] = {
84570cd071eSTejun Heo 	{ ATA_SHIFT_PIO, ATA_NR_PIO_MODES, XFER_PIO_0 },
84670cd071eSTejun Heo 	{ ATA_SHIFT_MWDMA, ATA_NR_MWDMA_MODES, XFER_MW_DMA_0 },
84770cd071eSTejun Heo 	{ ATA_SHIFT_UDMA, ATA_NR_UDMA_MODES, XFER_UDMA_0 },
848c6fd2807SJeff Garzik 	{ -1, },
849c6fd2807SJeff Garzik };
850c6fd2807SJeff Garzik 
851c6fd2807SJeff Garzik /**
852c6fd2807SJeff Garzik  *	ata_xfer_mask2mode - Find matching XFER_* for the given xfer_mask
853c6fd2807SJeff Garzik  *	@xfer_mask: xfer_mask of interest
854c6fd2807SJeff Garzik  *
855c6fd2807SJeff Garzik  *	Return matching XFER_* value for @xfer_mask.  Only the highest
856c6fd2807SJeff Garzik  *	bit of @xfer_mask is considered.
857c6fd2807SJeff Garzik  *
858c6fd2807SJeff Garzik  *	LOCKING:
859c6fd2807SJeff Garzik  *	None.
860c6fd2807SJeff Garzik  *
861c6fd2807SJeff Garzik  *	RETURNS:
86270cd071eSTejun Heo  *	Matching XFER_* value, 0xff if no match found.
863c6fd2807SJeff Garzik  */
864f0a6d77bSSergey Shtylyov u8 ata_xfer_mask2mode(unsigned int xfer_mask)
865c6fd2807SJeff Garzik {
866c6fd2807SJeff Garzik 	int highbit = fls(xfer_mask) - 1;
867c6fd2807SJeff Garzik 	const struct ata_xfer_ent *ent;
868c6fd2807SJeff Garzik 
869c6fd2807SJeff Garzik 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
870c6fd2807SJeff Garzik 		if (highbit >= ent->shift && highbit < ent->shift + ent->bits)
871c6fd2807SJeff Garzik 			return ent->base + highbit - ent->shift;
87270cd071eSTejun Heo 	return 0xff;
873c6fd2807SJeff Garzik }
874a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_xfer_mask2mode);
875c6fd2807SJeff Garzik 
876c6fd2807SJeff Garzik /**
877c6fd2807SJeff Garzik  *	ata_xfer_mode2mask - Find matching xfer_mask for XFER_*
878c6fd2807SJeff Garzik  *	@xfer_mode: XFER_* of interest
879c6fd2807SJeff Garzik  *
880c6fd2807SJeff Garzik  *	Return matching xfer_mask for @xfer_mode.
881c6fd2807SJeff Garzik  *
882c6fd2807SJeff Garzik  *	LOCKING:
883c6fd2807SJeff Garzik  *	None.
884c6fd2807SJeff Garzik  *
885c6fd2807SJeff Garzik  *	RETURNS:
886c6fd2807SJeff Garzik  *	Matching xfer_mask, 0 if no match found.
887c6fd2807SJeff Garzik  */
888f0a6d77bSSergey Shtylyov unsigned int ata_xfer_mode2mask(u8 xfer_mode)
889c6fd2807SJeff Garzik {
890c6fd2807SJeff Garzik 	const struct ata_xfer_ent *ent;
891c6fd2807SJeff Garzik 
892c6fd2807SJeff Garzik 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
893c6fd2807SJeff Garzik 		if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
89470cd071eSTejun Heo 			return ((2 << (ent->shift + xfer_mode - ent->base)) - 1)
89570cd071eSTejun Heo 				& ~((1 << ent->shift) - 1);
896c6fd2807SJeff Garzik 	return 0;
897c6fd2807SJeff Garzik }
898a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_xfer_mode2mask);
899c6fd2807SJeff Garzik 
900c6fd2807SJeff Garzik /**
901c6fd2807SJeff Garzik  *	ata_xfer_mode2shift - Find matching xfer_shift for XFER_*
902c6fd2807SJeff Garzik  *	@xfer_mode: XFER_* of interest
903c6fd2807SJeff Garzik  *
904c6fd2807SJeff Garzik  *	Return matching xfer_shift for @xfer_mode.
905c6fd2807SJeff Garzik  *
906c6fd2807SJeff Garzik  *	LOCKING:
907c6fd2807SJeff Garzik  *	None.
908c6fd2807SJeff Garzik  *
909c6fd2807SJeff Garzik  *	RETURNS:
910c6fd2807SJeff Garzik  *	Matching xfer_shift, -1 if no match found.
911c6fd2807SJeff Garzik  */
912a28c1ab3SSergey Shtylyov int ata_xfer_mode2shift(u8 xfer_mode)
913c6fd2807SJeff Garzik {
914c6fd2807SJeff Garzik 	const struct ata_xfer_ent *ent;
915c6fd2807SJeff Garzik 
916c6fd2807SJeff Garzik 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
917c6fd2807SJeff Garzik 		if (xfer_mode >= ent->base && xfer_mode < ent->base + ent->bits)
918c6fd2807SJeff Garzik 			return ent->shift;
919c6fd2807SJeff Garzik 	return -1;
920c6fd2807SJeff Garzik }
921a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_xfer_mode2shift);
922c6fd2807SJeff Garzik 
923c6fd2807SJeff Garzik /**
924c6fd2807SJeff Garzik  *	ata_mode_string - convert xfer_mask to string
925c6fd2807SJeff Garzik  *	@xfer_mask: mask of bits supported; only highest bit counts.
926c6fd2807SJeff Garzik  *
927c6fd2807SJeff Garzik  *	Determine string which represents the highest speed
928c6fd2807SJeff Garzik  *	(highest bit in @modemask).
929c6fd2807SJeff Garzik  *
930c6fd2807SJeff Garzik  *	LOCKING:
931c6fd2807SJeff Garzik  *	None.
932c6fd2807SJeff Garzik  *
933c6fd2807SJeff Garzik  *	RETURNS:
934c6fd2807SJeff Garzik  *	Constant C string representing highest speed listed in
935c6fd2807SJeff Garzik  *	@mode_mask, or the constant C string "<n/a>".
936c6fd2807SJeff Garzik  */
937f0a6d77bSSergey Shtylyov const char *ata_mode_string(unsigned int xfer_mask)
938c6fd2807SJeff Garzik {
939c6fd2807SJeff Garzik 	static const char * const xfer_mode_str[] = {
940c6fd2807SJeff Garzik 		"PIO0",
941c6fd2807SJeff Garzik 		"PIO1",
942c6fd2807SJeff Garzik 		"PIO2",
943c6fd2807SJeff Garzik 		"PIO3",
944c6fd2807SJeff Garzik 		"PIO4",
945b352e57dSAlan Cox 		"PIO5",
946b352e57dSAlan Cox 		"PIO6",
947c6fd2807SJeff Garzik 		"MWDMA0",
948c6fd2807SJeff Garzik 		"MWDMA1",
949c6fd2807SJeff Garzik 		"MWDMA2",
950b352e57dSAlan Cox 		"MWDMA3",
951b352e57dSAlan Cox 		"MWDMA4",
952c6fd2807SJeff Garzik 		"UDMA/16",
953c6fd2807SJeff Garzik 		"UDMA/25",
954c6fd2807SJeff Garzik 		"UDMA/33",
955c6fd2807SJeff Garzik 		"UDMA/44",
956c6fd2807SJeff Garzik 		"UDMA/66",
957c6fd2807SJeff Garzik 		"UDMA/100",
958c6fd2807SJeff Garzik 		"UDMA/133",
959c6fd2807SJeff Garzik 		"UDMA7",
960c6fd2807SJeff Garzik 	};
961c6fd2807SJeff Garzik 	int highbit;
962c6fd2807SJeff Garzik 
963c6fd2807SJeff Garzik 	highbit = fls(xfer_mask) - 1;
964c6fd2807SJeff Garzik 	if (highbit >= 0 && highbit < ARRAY_SIZE(xfer_mode_str))
965c6fd2807SJeff Garzik 		return xfer_mode_str[highbit];
966c6fd2807SJeff Garzik 	return "<n/a>";
967c6fd2807SJeff Garzik }
968a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_mode_string);
969c6fd2807SJeff Garzik 
970d9027470SGwendal Grignou const char *sata_spd_string(unsigned int spd)
971c6fd2807SJeff Garzik {
972c6fd2807SJeff Garzik 	static const char * const spd_str[] = {
973c6fd2807SJeff Garzik 		"1.5 Gbps",
974c6fd2807SJeff Garzik 		"3.0 Gbps",
9758522ee25SShane Huang 		"6.0 Gbps",
976c6fd2807SJeff Garzik 	};
977c6fd2807SJeff Garzik 
978c6fd2807SJeff Garzik 	if (spd == 0 || (spd - 1) >= ARRAY_SIZE(spd_str))
979c6fd2807SJeff Garzik 		return "<unknown>";
980c6fd2807SJeff Garzik 	return spd_str[spd - 1];
981c6fd2807SJeff Garzik }
982c6fd2807SJeff Garzik 
983c6fd2807SJeff Garzik /**
984c6fd2807SJeff Garzik  *	ata_dev_classify - determine device type based on ATA-spec signature
985c6fd2807SJeff Garzik  *	@tf: ATA taskfile register set for device to be identified
986c6fd2807SJeff Garzik  *
987c6fd2807SJeff Garzik  *	Determine from taskfile register contents whether a device is
988c6fd2807SJeff Garzik  *	ATA or ATAPI, as per "Signature and persistence" section
989c6fd2807SJeff Garzik  *	of ATA/PI spec (volume 1, sect 5.14).
990c6fd2807SJeff Garzik  *
991c6fd2807SJeff Garzik  *	LOCKING:
992c6fd2807SJeff Garzik  *	None.
993c6fd2807SJeff Garzik  *
994c6fd2807SJeff Garzik  *	RETURNS:
9959162c657SHannes Reinecke  *	Device type, %ATA_DEV_ATA, %ATA_DEV_ATAPI, %ATA_DEV_PMP,
9969162c657SHannes Reinecke  *	%ATA_DEV_ZAC, or %ATA_DEV_UNKNOWN the event of failure.
997c6fd2807SJeff Garzik  */
998c6fd2807SJeff Garzik unsigned int ata_dev_classify(const struct ata_taskfile *tf)
999c6fd2807SJeff Garzik {
1000c6fd2807SJeff Garzik 	/* Apple's open source Darwin code hints that some devices only
1001c6fd2807SJeff Garzik 	 * put a proper signature into the LBA mid/high registers,
1002c6fd2807SJeff Garzik 	 * So, we only check those.  It's sufficient for uniqueness.
1003633273a3STejun Heo 	 *
1004633273a3STejun Heo 	 * ATA/ATAPI-7 (d1532v1r1: Feb. 19, 2003) specified separate
1005633273a3STejun Heo 	 * signatures for ATA and ATAPI devices attached on SerialATA,
1006633273a3STejun Heo 	 * 0x3c/0xc3 and 0x69/0x96 respectively.  However, SerialATA
1007633273a3STejun Heo 	 * spec has never mentioned about using different signatures
1008633273a3STejun Heo 	 * for ATA/ATAPI devices.  Then, Serial ATA II: Port
1009633273a3STejun Heo 	 * Multiplier specification began to use 0x69/0x96 to identify
1010633273a3STejun Heo 	 * port multpliers and 0x3c/0xc3 to identify SEMB device.
1011633273a3STejun Heo 	 * ATA/ATAPI-7 dropped descriptions about 0x3c/0xc3 and
1012633273a3STejun Heo 	 * 0x69/0x96 shortly and described them as reserved for
1013633273a3STejun Heo 	 * SerialATA.
1014633273a3STejun Heo 	 *
1015633273a3STejun Heo 	 * We follow the current spec and consider that 0x69/0x96
1016633273a3STejun Heo 	 * identifies a port multiplier and 0x3c/0xc3 a SEMB device.
101779b42babSTejun Heo 	 * Unfortunately, WDC WD1600JS-62MHB5 (a hard drive) reports
101879b42babSTejun Heo 	 * SEMB signature.  This is worked around in
101979b42babSTejun Heo 	 * ata_dev_read_id().
1020c6fd2807SJeff Garzik 	 */
10216c952a0dSHannes Reinecke 	if (tf->lbam == 0 && tf->lbah == 0)
1022c6fd2807SJeff Garzik 		return ATA_DEV_ATA;
1023c6fd2807SJeff Garzik 
10246c952a0dSHannes Reinecke 	if (tf->lbam == 0x14 && tf->lbah == 0xeb)
1025c6fd2807SJeff Garzik 		return ATA_DEV_ATAPI;
1026c6fd2807SJeff Garzik 
10276c952a0dSHannes Reinecke 	if (tf->lbam == 0x69 && tf->lbah == 0x96)
1028633273a3STejun Heo 		return ATA_DEV_PMP;
1029633273a3STejun Heo 
10306c952a0dSHannes Reinecke 	if (tf->lbam == 0x3c && tf->lbah == 0xc3)
103179b42babSTejun Heo 		return ATA_DEV_SEMB;
1032633273a3STejun Heo 
10336c952a0dSHannes Reinecke 	if (tf->lbam == 0xcd && tf->lbah == 0xab)
10349162c657SHannes Reinecke 		return ATA_DEV_ZAC;
10359162c657SHannes Reinecke 
1036c6fd2807SJeff Garzik 	return ATA_DEV_UNKNOWN;
1037c6fd2807SJeff Garzik }
1038a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_dev_classify);
1039c6fd2807SJeff Garzik 
1040c6fd2807SJeff Garzik /**
1041c6fd2807SJeff Garzik  *	ata_id_string - Convert IDENTIFY DEVICE page into string
1042c6fd2807SJeff Garzik  *	@id: IDENTIFY DEVICE results we will examine
1043c6fd2807SJeff Garzik  *	@s: string into which data is output
1044c6fd2807SJeff Garzik  *	@ofs: offset into identify device page
1045c6fd2807SJeff Garzik  *	@len: length of string to return. must be an even number.
1046c6fd2807SJeff Garzik  *
1047c6fd2807SJeff Garzik  *	The strings in the IDENTIFY DEVICE page are broken up into
1048c6fd2807SJeff Garzik  *	16-bit chunks.  Run through the string, and output each
1049c6fd2807SJeff Garzik  *	8-bit chunk linearly, regardless of platform.
1050c6fd2807SJeff Garzik  *
1051c6fd2807SJeff Garzik  *	LOCKING:
1052c6fd2807SJeff Garzik  *	caller.
1053c6fd2807SJeff Garzik  */
1054c6fd2807SJeff Garzik 
1055c6fd2807SJeff Garzik void ata_id_string(const u16 *id, unsigned char *s,
1056c6fd2807SJeff Garzik 		   unsigned int ofs, unsigned int len)
1057c6fd2807SJeff Garzik {
1058c6fd2807SJeff Garzik 	unsigned int c;
1059c6fd2807SJeff Garzik 
1060963e4975SAlan Cox 	BUG_ON(len & 1);
1061963e4975SAlan Cox 
1062c6fd2807SJeff Garzik 	while (len > 0) {
1063c6fd2807SJeff Garzik 		c = id[ofs] >> 8;
1064c6fd2807SJeff Garzik 		*s = c;
1065c6fd2807SJeff Garzik 		s++;
1066c6fd2807SJeff Garzik 
1067c6fd2807SJeff Garzik 		c = id[ofs] & 0xff;
1068c6fd2807SJeff Garzik 		*s = c;
1069c6fd2807SJeff Garzik 		s++;
1070c6fd2807SJeff Garzik 
1071c6fd2807SJeff Garzik 		ofs++;
1072c6fd2807SJeff Garzik 		len -= 2;
1073c6fd2807SJeff Garzik 	}
1074c6fd2807SJeff Garzik }
1075a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_id_string);
1076c6fd2807SJeff Garzik 
1077c6fd2807SJeff Garzik /**
1078c6fd2807SJeff Garzik  *	ata_id_c_string - Convert IDENTIFY DEVICE page into C string
1079c6fd2807SJeff Garzik  *	@id: IDENTIFY DEVICE results we will examine
1080c6fd2807SJeff Garzik  *	@s: string into which data is output
1081c6fd2807SJeff Garzik  *	@ofs: offset into identify device page
1082c6fd2807SJeff Garzik  *	@len: length of string to return. must be an odd number.
1083c6fd2807SJeff Garzik  *
1084c6fd2807SJeff Garzik  *	This function is identical to ata_id_string except that it
1085c6fd2807SJeff Garzik  *	trims trailing spaces and terminates the resulting string with
1086c6fd2807SJeff Garzik  *	null.  @len must be actual maximum length (even number) + 1.
1087c6fd2807SJeff Garzik  *
1088c6fd2807SJeff Garzik  *	LOCKING:
1089c6fd2807SJeff Garzik  *	caller.
1090c6fd2807SJeff Garzik  */
1091c6fd2807SJeff Garzik void ata_id_c_string(const u16 *id, unsigned char *s,
1092c6fd2807SJeff Garzik 		     unsigned int ofs, unsigned int len)
1093c6fd2807SJeff Garzik {
1094c6fd2807SJeff Garzik 	unsigned char *p;
1095c6fd2807SJeff Garzik 
1096c6fd2807SJeff Garzik 	ata_id_string(id, s, ofs, len - 1);
1097c6fd2807SJeff Garzik 
1098c6fd2807SJeff Garzik 	p = s + strnlen(s, len - 1);
1099c6fd2807SJeff Garzik 	while (p > s && p[-1] == ' ')
1100c6fd2807SJeff Garzik 		p--;
1101c6fd2807SJeff Garzik 	*p = '\0';
1102c6fd2807SJeff Garzik }
1103a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_id_c_string);
1104c6fd2807SJeff Garzik 
1105db6f8759STejun Heo static u64 ata_id_n_sectors(const u16 *id)
1106db6f8759STejun Heo {
1107db6f8759STejun Heo 	if (ata_id_has_lba(id)) {
1108db6f8759STejun Heo 		if (ata_id_has_lba48(id))
1109968e594aSRobert Hancock 			return ata_id_u64(id, ATA_ID_LBA_CAPACITY_2);
11105eb8deb4SSergey Shtylyov 
1111968e594aSRobert Hancock 		return ata_id_u32(id, ATA_ID_LBA_CAPACITY);
1112db6f8759STejun Heo 	}
11135eb8deb4SSergey Shtylyov 
11145eb8deb4SSergey Shtylyov 	if (ata_id_current_chs_valid(id))
11155eb8deb4SSergey Shtylyov 		return (u32)id[ATA_ID_CUR_CYLS] * (u32)id[ATA_ID_CUR_HEADS] *
11165eb8deb4SSergey Shtylyov 		       (u32)id[ATA_ID_CUR_SECTORS];
11175eb8deb4SSergey Shtylyov 
11185eb8deb4SSergey Shtylyov 	return (u32)id[ATA_ID_CYLS] * (u32)id[ATA_ID_HEADS] *
11195eb8deb4SSergey Shtylyov 	       (u32)id[ATA_ID_SECTORS];
1120db6f8759STejun Heo }
1121db6f8759STejun Heo 
1122a5987e0aSTejun Heo u64 ata_tf_to_lba48(const struct ata_taskfile *tf)
11231e999736SAlan Cox {
11241e999736SAlan Cox 	u64 sectors = 0;
11251e999736SAlan Cox 
11261e999736SAlan Cox 	sectors |= ((u64)(tf->hob_lbah & 0xff)) << 40;
11271e999736SAlan Cox 	sectors |= ((u64)(tf->hob_lbam & 0xff)) << 32;
1128ba14a9c2SRoland Dreier 	sectors |= ((u64)(tf->hob_lbal & 0xff)) << 24;
11291e999736SAlan Cox 	sectors |= (tf->lbah & 0xff) << 16;
11301e999736SAlan Cox 	sectors |= (tf->lbam & 0xff) << 8;
11311e999736SAlan Cox 	sectors |= (tf->lbal & 0xff);
11321e999736SAlan Cox 
1133a5987e0aSTejun Heo 	return sectors;
11341e999736SAlan Cox }
11351e999736SAlan Cox 
1136a5987e0aSTejun Heo u64 ata_tf_to_lba(const struct ata_taskfile *tf)
11371e999736SAlan Cox {
11381e999736SAlan Cox 	u64 sectors = 0;
11391e999736SAlan Cox 
11401e999736SAlan Cox 	sectors |= (tf->device & 0x0f) << 24;
11411e999736SAlan Cox 	sectors |= (tf->lbah & 0xff) << 16;
11421e999736SAlan Cox 	sectors |= (tf->lbam & 0xff) << 8;
11431e999736SAlan Cox 	sectors |= (tf->lbal & 0xff);
11441e999736SAlan Cox 
1145a5987e0aSTejun Heo 	return sectors;
11461e999736SAlan Cox }
11471e999736SAlan Cox 
11481e999736SAlan Cox /**
1149c728a914STejun Heo  *	ata_read_native_max_address - Read native max address
1150c728a914STejun Heo  *	@dev: target device
1151c728a914STejun Heo  *	@max_sectors: out parameter for the result native max address
11521e999736SAlan Cox  *
1153c728a914STejun Heo  *	Perform an LBA48 or LBA28 native size query upon the device in
1154c728a914STejun Heo  *	question.
1155c728a914STejun Heo  *
1156c728a914STejun Heo  *	RETURNS:
1157c728a914STejun Heo  *	0 on success, -EACCES if command is aborted by the drive.
1158c728a914STejun Heo  *	-EIO on other errors.
11591e999736SAlan Cox  */
1160c728a914STejun Heo static int ata_read_native_max_address(struct ata_device *dev, u64 *max_sectors)
11611e999736SAlan Cox {
1162c728a914STejun Heo 	unsigned int err_mask;
11631e999736SAlan Cox 	struct ata_taskfile tf;
1164c728a914STejun Heo 	int lba48 = ata_id_has_lba48(dev->id);
11651e999736SAlan Cox 
11661e999736SAlan Cox 	ata_tf_init(dev, &tf);
11671e999736SAlan Cox 
1168c728a914STejun Heo 	/* always clear all address registers */
11691e999736SAlan Cox 	tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
1170c728a914STejun Heo 
1171c728a914STejun Heo 	if (lba48) {
1172c728a914STejun Heo 		tf.command = ATA_CMD_READ_NATIVE_MAX_EXT;
1173c728a914STejun Heo 		tf.flags |= ATA_TFLAG_LBA48;
1174c728a914STejun Heo 	} else
1175c728a914STejun Heo 		tf.command = ATA_CMD_READ_NATIVE_MAX;
1176c728a914STejun Heo 
1177bd18bc04SHannes Reinecke 	tf.protocol = ATA_PROT_NODATA;
1178c728a914STejun Heo 	tf.device |= ATA_LBA;
11791e999736SAlan Cox 
11802b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1181c728a914STejun Heo 	if (err_mask) {
1182a9a79dfeSJoe Perches 		ata_dev_warn(dev,
1183a9a79dfeSJoe Perches 			     "failed to read native max address (err_mask=0x%x)\n",
1184a9a79dfeSJoe Perches 			     err_mask);
1185efcef265SSergey Shtylyov 		if (err_mask == AC_ERR_DEV && (tf.error & ATA_ABORTED))
1186c728a914STejun Heo 			return -EACCES;
1187c728a914STejun Heo 		return -EIO;
1188c728a914STejun Heo 	}
1189c728a914STejun Heo 
1190c728a914STejun Heo 	if (lba48)
1191a5987e0aSTejun Heo 		*max_sectors = ata_tf_to_lba48(&tf) + 1;
1192c728a914STejun Heo 	else
1193a5987e0aSTejun Heo 		*max_sectors = ata_tf_to_lba(&tf) + 1;
119493328e11SAlan Cox 	if (dev->horkage & ATA_HORKAGE_HPA_SIZE)
119593328e11SAlan Cox 		(*max_sectors)--;
11961e999736SAlan Cox 	return 0;
11971e999736SAlan Cox }
11981e999736SAlan Cox 
11991e999736SAlan Cox /**
1200c728a914STejun Heo  *	ata_set_max_sectors - Set max sectors
1201c728a914STejun Heo  *	@dev: target device
12026b38d1d1SRandy Dunlap  *	@new_sectors: new max sectors value to set for the device
12031e999736SAlan Cox  *
1204c728a914STejun Heo  *	Set max sectors of @dev to @new_sectors.
1205c728a914STejun Heo  *
1206c728a914STejun Heo  *	RETURNS:
1207c728a914STejun Heo  *	0 on success, -EACCES if command is aborted or denied (due to
1208c728a914STejun Heo  *	previous non-volatile SET_MAX) by the drive.  -EIO on other
1209c728a914STejun Heo  *	errors.
12101e999736SAlan Cox  */
121105027adcSTejun Heo static int ata_set_max_sectors(struct ata_device *dev, u64 new_sectors)
12121e999736SAlan Cox {
1213c728a914STejun Heo 	unsigned int err_mask;
12141e999736SAlan Cox 	struct ata_taskfile tf;
1215c728a914STejun Heo 	int lba48 = ata_id_has_lba48(dev->id);
12161e999736SAlan Cox 
12171e999736SAlan Cox 	new_sectors--;
12181e999736SAlan Cox 
12191e999736SAlan Cox 	ata_tf_init(dev, &tf);
12201e999736SAlan Cox 
1221c728a914STejun Heo 	tf.flags |= ATA_TFLAG_DEVICE | ATA_TFLAG_ISADDR;
12221e999736SAlan Cox 
1223c728a914STejun Heo 	if (lba48) {
1224c728a914STejun Heo 		tf.command = ATA_CMD_SET_MAX_EXT;
1225c728a914STejun Heo 		tf.flags |= ATA_TFLAG_LBA48;
12261e999736SAlan Cox 
12271e999736SAlan Cox 		tf.hob_lbal = (new_sectors >> 24) & 0xff;
12281e999736SAlan Cox 		tf.hob_lbam = (new_sectors >> 32) & 0xff;
12291e999736SAlan Cox 		tf.hob_lbah = (new_sectors >> 40) & 0xff;
12301e582ba4STejun Heo 	} else {
12311e999736SAlan Cox 		tf.command = ATA_CMD_SET_MAX;
1232c728a914STejun Heo 
12331e582ba4STejun Heo 		tf.device |= (new_sectors >> 24) & 0xf;
12341e582ba4STejun Heo 	}
12351e582ba4STejun Heo 
1236bd18bc04SHannes Reinecke 	tf.protocol = ATA_PROT_NODATA;
1237c728a914STejun Heo 	tf.device |= ATA_LBA;
12381e999736SAlan Cox 
12391e999736SAlan Cox 	tf.lbal = (new_sectors >> 0) & 0xff;
12401e999736SAlan Cox 	tf.lbam = (new_sectors >> 8) & 0xff;
12411e999736SAlan Cox 	tf.lbah = (new_sectors >> 16) & 0xff;
12421e999736SAlan Cox 
12432b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
1244c728a914STejun Heo 	if (err_mask) {
1245a9a79dfeSJoe Perches 		ata_dev_warn(dev,
1246a9a79dfeSJoe Perches 			     "failed to set max address (err_mask=0x%x)\n",
1247a9a79dfeSJoe Perches 			     err_mask);
1248c728a914STejun Heo 		if (err_mask == AC_ERR_DEV &&
1249efcef265SSergey Shtylyov 		    (tf.error & (ATA_ABORTED | ATA_IDNF)))
1250c728a914STejun Heo 			return -EACCES;
1251c728a914STejun Heo 		return -EIO;
1252c728a914STejun Heo 	}
1253c728a914STejun Heo 
12541e999736SAlan Cox 	return 0;
12551e999736SAlan Cox }
12561e999736SAlan Cox 
12571e999736SAlan Cox /**
12581e999736SAlan Cox  *	ata_hpa_resize		-	Resize a device with an HPA set
12591e999736SAlan Cox  *	@dev: Device to resize
12601e999736SAlan Cox  *
12611e999736SAlan Cox  *	Read the size of an LBA28 or LBA48 disk with HPA features and resize
12621e999736SAlan Cox  *	it if required to the full size of the media. The caller must check
12631e999736SAlan Cox  *	the drive has the HPA feature set enabled.
126405027adcSTejun Heo  *
126505027adcSTejun Heo  *	RETURNS:
126605027adcSTejun Heo  *	0 on success, -errno on failure.
12671e999736SAlan Cox  */
126805027adcSTejun Heo static int ata_hpa_resize(struct ata_device *dev)
12691e999736SAlan Cox {
1270891fd7c6SDamien Le Moal 	bool print_info = ata_dev_print_info(dev);
1271445d211bSTejun Heo 	bool unlock_hpa = ata_ignore_hpa || dev->flags & ATA_DFLAG_UNLOCK_HPA;
127205027adcSTejun Heo 	u64 sectors = ata_id_n_sectors(dev->id);
127305027adcSTejun Heo 	u64 native_sectors;
1274c728a914STejun Heo 	int rc;
12751e999736SAlan Cox 
127605027adcSTejun Heo 	/* do we need to do it? */
12779162c657SHannes Reinecke 	if ((dev->class != ATA_DEV_ATA && dev->class != ATA_DEV_ZAC) ||
127805027adcSTejun Heo 	    !ata_id_has_lba(dev->id) || !ata_id_hpa_enabled(dev->id) ||
127905027adcSTejun Heo 	    (dev->horkage & ATA_HORKAGE_BROKEN_HPA))
1280c728a914STejun Heo 		return 0;
12811e999736SAlan Cox 
128205027adcSTejun Heo 	/* read native max address */
128305027adcSTejun Heo 	rc = ata_read_native_max_address(dev, &native_sectors);
128405027adcSTejun Heo 	if (rc) {
1285dda7aba1STejun Heo 		/* If device aborted the command or HPA isn't going to
1286dda7aba1STejun Heo 		 * be unlocked, skip HPA resizing.
128705027adcSTejun Heo 		 */
1288445d211bSTejun Heo 		if (rc == -EACCES || !unlock_hpa) {
1289a9a79dfeSJoe Perches 			ata_dev_warn(dev,
1290a9a79dfeSJoe Perches 				     "HPA support seems broken, skipping HPA handling\n");
129105027adcSTejun Heo 			dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
129205027adcSTejun Heo 
129305027adcSTejun Heo 			/* we can continue if device aborted the command */
129405027adcSTejun Heo 			if (rc == -EACCES)
129505027adcSTejun Heo 				rc = 0;
129605027adcSTejun Heo 		}
129705027adcSTejun Heo 
129805027adcSTejun Heo 		return rc;
129905027adcSTejun Heo 	}
13005920dadfSTejun Heo 	dev->n_native_sectors = native_sectors;
130105027adcSTejun Heo 
130205027adcSTejun Heo 	/* nothing to do? */
1303445d211bSTejun Heo 	if (native_sectors <= sectors || !unlock_hpa) {
130405027adcSTejun Heo 		if (!print_info || native_sectors == sectors)
130505027adcSTejun Heo 			return 0;
130605027adcSTejun Heo 
130705027adcSTejun Heo 		if (native_sectors > sectors)
1308a9a79dfeSJoe Perches 			ata_dev_info(dev,
130905027adcSTejun Heo 				"HPA detected: current %llu, native %llu\n",
131005027adcSTejun Heo 				(unsigned long long)sectors,
131105027adcSTejun Heo 				(unsigned long long)native_sectors);
131205027adcSTejun Heo 		else if (native_sectors < sectors)
1313a9a79dfeSJoe Perches 			ata_dev_warn(dev,
1314a9a79dfeSJoe Perches 				"native sectors (%llu) is smaller than sectors (%llu)\n",
131505027adcSTejun Heo 				(unsigned long long)native_sectors,
131605027adcSTejun Heo 				(unsigned long long)sectors);
131705027adcSTejun Heo 		return 0;
13181e999736SAlan Cox 	}
131937301a55STejun Heo 
132005027adcSTejun Heo 	/* let's unlock HPA */
132105027adcSTejun Heo 	rc = ata_set_max_sectors(dev, native_sectors);
132205027adcSTejun Heo 	if (rc == -EACCES) {
132305027adcSTejun Heo 		/* if device aborted the command, skip HPA resizing */
1324a9a79dfeSJoe Perches 		ata_dev_warn(dev,
1325a9a79dfeSJoe Perches 			     "device aborted resize (%llu -> %llu), skipping HPA handling\n",
132605027adcSTejun Heo 			     (unsigned long long)sectors,
132705027adcSTejun Heo 			     (unsigned long long)native_sectors);
132805027adcSTejun Heo 		dev->horkage |= ATA_HORKAGE_BROKEN_HPA;
132905027adcSTejun Heo 		return 0;
133005027adcSTejun Heo 	} else if (rc)
133105027adcSTejun Heo 		return rc;
133205027adcSTejun Heo 
133305027adcSTejun Heo 	/* re-read IDENTIFY data */
133405027adcSTejun Heo 	rc = ata_dev_reread_id(dev, 0);
133505027adcSTejun Heo 	if (rc) {
1336a9a79dfeSJoe Perches 		ata_dev_err(dev,
1337a9a79dfeSJoe Perches 			    "failed to re-read IDENTIFY data after HPA resizing\n");
133805027adcSTejun Heo 		return rc;
133905027adcSTejun Heo 	}
134005027adcSTejun Heo 
134105027adcSTejun Heo 	if (print_info) {
134205027adcSTejun Heo 		u64 new_sectors = ata_id_n_sectors(dev->id);
1343a9a79dfeSJoe Perches 		ata_dev_info(dev,
134405027adcSTejun Heo 			"HPA unlocked: %llu -> %llu, native %llu\n",
134505027adcSTejun Heo 			(unsigned long long)sectors,
134605027adcSTejun Heo 			(unsigned long long)new_sectors,
134705027adcSTejun Heo 			(unsigned long long)native_sectors);
134805027adcSTejun Heo 	}
134905027adcSTejun Heo 
135005027adcSTejun Heo 	return 0;
13511e999736SAlan Cox }
13521e999736SAlan Cox 
1353c6fd2807SJeff Garzik /**
1354c6fd2807SJeff Garzik  *	ata_dump_id - IDENTIFY DEVICE info debugging output
13556044f3c4SHannes Reinecke  *	@dev: device from which the information is fetched
1356c6fd2807SJeff Garzik  *	@id: IDENTIFY DEVICE page to dump
1357c6fd2807SJeff Garzik  *
1358c6fd2807SJeff Garzik  *	Dump selected 16-bit words from the given IDENTIFY DEVICE
1359c6fd2807SJeff Garzik  *	page.
1360c6fd2807SJeff Garzik  *
1361c6fd2807SJeff Garzik  *	LOCKING:
1362c6fd2807SJeff Garzik  *	caller.
1363c6fd2807SJeff Garzik  */
1364c6fd2807SJeff Garzik 
13656044f3c4SHannes Reinecke static inline void ata_dump_id(struct ata_device *dev, const u16 *id)
1366c6fd2807SJeff Garzik {
13676044f3c4SHannes Reinecke 	ata_dev_dbg(dev,
13686044f3c4SHannes Reinecke 		"49==0x%04x  53==0x%04x  63==0x%04x  64==0x%04x  75==0x%04x\n"
13696044f3c4SHannes Reinecke 		"80==0x%04x  81==0x%04x  82==0x%04x  83==0x%04x  84==0x%04x\n"
13706044f3c4SHannes Reinecke 		"88==0x%04x  93==0x%04x\n",
13716044f3c4SHannes Reinecke 		id[49], id[53], id[63], id[64], id[75], id[80],
13726044f3c4SHannes Reinecke 		id[81], id[82], id[83], id[84], id[88], id[93]);
1373c6fd2807SJeff Garzik }
1374c6fd2807SJeff Garzik 
1375c6fd2807SJeff Garzik /**
1376c6fd2807SJeff Garzik  *	ata_id_xfermask - Compute xfermask from the given IDENTIFY data
1377c6fd2807SJeff Garzik  *	@id: IDENTIFY data to compute xfer mask from
1378c6fd2807SJeff Garzik  *
1379c6fd2807SJeff Garzik  *	Compute the xfermask for this device. This is not as trivial
1380c6fd2807SJeff Garzik  *	as it seems if we must consider early devices correctly.
1381c6fd2807SJeff Garzik  *
1382c6fd2807SJeff Garzik  *	FIXME: pre IDE drive timing (do we care ?).
1383c6fd2807SJeff Garzik  *
1384c6fd2807SJeff Garzik  *	LOCKING:
1385c6fd2807SJeff Garzik  *	None.
1386c6fd2807SJeff Garzik  *
1387c6fd2807SJeff Garzik  *	RETURNS:
1388c6fd2807SJeff Garzik  *	Computed xfermask
1389c6fd2807SJeff Garzik  */
1390f0a6d77bSSergey Shtylyov unsigned int ata_id_xfermask(const u16 *id)
1391c6fd2807SJeff Garzik {
1392f0a6d77bSSergey Shtylyov 	unsigned int pio_mask, mwdma_mask, udma_mask;
1393c6fd2807SJeff Garzik 
1394c6fd2807SJeff Garzik 	/* Usual case. Word 53 indicates word 64 is valid */
1395c6fd2807SJeff Garzik 	if (id[ATA_ID_FIELD_VALID] & (1 << 1)) {
1396c6fd2807SJeff Garzik 		pio_mask = id[ATA_ID_PIO_MODES] & 0x03;
1397c6fd2807SJeff Garzik 		pio_mask <<= 3;
1398c6fd2807SJeff Garzik 		pio_mask |= 0x7;
1399c6fd2807SJeff Garzik 	} else {
1400c6fd2807SJeff Garzik 		/* If word 64 isn't valid then Word 51 high byte holds
1401c6fd2807SJeff Garzik 		 * the PIO timing number for the maximum. Turn it into
1402c6fd2807SJeff Garzik 		 * a mask.
1403c6fd2807SJeff Garzik 		 */
14047a0f1c8aSLennert Buytenhek 		u8 mode = (id[ATA_ID_OLD_PIO_MODES] >> 8) & 0xFF;
140546767aebSAlan Cox 		if (mode < 5)	/* Valid PIO range */
140646767aebSAlan Cox 			pio_mask = (2 << mode) - 1;
140746767aebSAlan Cox 		else
140846767aebSAlan Cox 			pio_mask = 1;
1409c6fd2807SJeff Garzik 
1410c6fd2807SJeff Garzik 		/* But wait.. there's more. Design your standards by
1411c6fd2807SJeff Garzik 		 * committee and you too can get a free iordy field to
1412e0af10acSDiego Viola 		 * process. However it is the speeds not the modes that
1413c6fd2807SJeff Garzik 		 * are supported... Note drivers using the timing API
1414c6fd2807SJeff Garzik 		 * will get this right anyway
1415c6fd2807SJeff Garzik 		 */
1416c6fd2807SJeff Garzik 	}
1417c6fd2807SJeff Garzik 
1418c6fd2807SJeff Garzik 	mwdma_mask = id[ATA_ID_MWDMA_MODES] & 0x07;
1419c6fd2807SJeff Garzik 
1420b352e57dSAlan Cox 	if (ata_id_is_cfa(id)) {
1421b352e57dSAlan Cox 		/*
1422b352e57dSAlan Cox 		 *	Process compact flash extended modes
1423b352e57dSAlan Cox 		 */
142462afe5d7SSergei Shtylyov 		int pio = (id[ATA_ID_CFA_MODES] >> 0) & 0x7;
142562afe5d7SSergei Shtylyov 		int dma = (id[ATA_ID_CFA_MODES] >> 3) & 0x7;
1426b352e57dSAlan Cox 
1427b352e57dSAlan Cox 		if (pio)
1428b352e57dSAlan Cox 			pio_mask |= (1 << 5);
1429b352e57dSAlan Cox 		if (pio > 1)
1430b352e57dSAlan Cox 			pio_mask |= (1 << 6);
1431b352e57dSAlan Cox 		if (dma)
1432b352e57dSAlan Cox 			mwdma_mask |= (1 << 3);
1433b352e57dSAlan Cox 		if (dma > 1)
1434b352e57dSAlan Cox 			mwdma_mask |= (1 << 4);
1435b352e57dSAlan Cox 	}
1436b352e57dSAlan Cox 
1437c6fd2807SJeff Garzik 	udma_mask = 0;
1438c6fd2807SJeff Garzik 	if (id[ATA_ID_FIELD_VALID] & (1 << 2))
1439c6fd2807SJeff Garzik 		udma_mask = id[ATA_ID_UDMA_MODES] & 0xff;
1440c6fd2807SJeff Garzik 
1441c6fd2807SJeff Garzik 	return ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
1442c6fd2807SJeff Garzik }
1443a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_id_xfermask);
1444c6fd2807SJeff Garzik 
14457102d230SAdrian Bunk static void ata_qc_complete_internal(struct ata_queued_cmd *qc)
1446c6fd2807SJeff Garzik {
1447c6fd2807SJeff Garzik 	struct completion *waiting = qc->private_data;
1448c6fd2807SJeff Garzik 
1449c6fd2807SJeff Garzik 	complete(waiting);
1450c6fd2807SJeff Garzik }
1451c6fd2807SJeff Garzik 
1452c6fd2807SJeff Garzik /**
14532432697bSTejun Heo  *	ata_exec_internal_sg - execute libata internal command
1454c6fd2807SJeff Garzik  *	@dev: Device to which the command is sent
1455c6fd2807SJeff Garzik  *	@tf: Taskfile registers for the command and the result
1456c6fd2807SJeff Garzik  *	@cdb: CDB for packet command
1457e227867fSMasanari Iida  *	@dma_dir: Data transfer direction of the command
14585c1ad8b3SRandy Dunlap  *	@sgl: sg list for the data buffer of the command
14592432697bSTejun Heo  *	@n_elem: Number of sg entries
14602b789108STejun Heo  *	@timeout: Timeout in msecs (0 for default)
1461c6fd2807SJeff Garzik  *
1462c6fd2807SJeff Garzik  *	Executes libata internal command with timeout.  @tf contains
1463c6fd2807SJeff Garzik  *	command on entry and result on return.  Timeout and error
1464c6fd2807SJeff Garzik  *	conditions are reported via return value.  No recovery action
1465c6fd2807SJeff Garzik  *	is taken after a command times out.  It's caller's duty to
1466c6fd2807SJeff Garzik  *	clean up after timeout.
1467c6fd2807SJeff Garzik  *
1468c6fd2807SJeff Garzik  *	LOCKING:
1469c6fd2807SJeff Garzik  *	None.  Should be called with kernel context, might sleep.
1470c6fd2807SJeff Garzik  *
1471c6fd2807SJeff Garzik  *	RETURNS:
1472c6fd2807SJeff Garzik  *	Zero on success, AC_ERR_* mask on failure
1473c6fd2807SJeff Garzik  */
14744d6119f0SSergey Shtylyov static unsigned ata_exec_internal_sg(struct ata_device *dev,
1475c6fd2807SJeff Garzik 				     struct ata_taskfile *tf, const u8 *cdb,
147687260216SJens Axboe 				     int dma_dir, struct scatterlist *sgl,
147761176eedSSergey Shtylyov 				     unsigned int n_elem, unsigned int timeout)
1478c6fd2807SJeff Garzik {
14799af5c9c9STejun Heo 	struct ata_link *link = dev->link;
14809af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
1481c6fd2807SJeff Garzik 	u8 command = tf->command;
148287fbc5a0STejun Heo 	int auto_timeout = 0;
1483c6fd2807SJeff Garzik 	struct ata_queued_cmd *qc;
148428361c40SJens Axboe 	unsigned int preempted_tag;
1485e3ed8939SJens Axboe 	u32 preempted_sactive;
1486e3ed8939SJens Axboe 	u64 preempted_qc_active;
1487da917d69STejun Heo 	int preempted_nr_active_links;
1488c6fd2807SJeff Garzik 	DECLARE_COMPLETION_ONSTACK(wait);
1489c6fd2807SJeff Garzik 	unsigned long flags;
1490c6fd2807SJeff Garzik 	unsigned int err_mask;
1491c6fd2807SJeff Garzik 	int rc;
1492c6fd2807SJeff Garzik 
1493c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1494c6fd2807SJeff Garzik 
1495c6fd2807SJeff Garzik 	/* no internal command while frozen */
14964cb7c6f1SNiklas Cassel 	if (ata_port_is_frozen(ap)) {
1497c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
1498c6fd2807SJeff Garzik 		return AC_ERR_SYSTEM;
1499c6fd2807SJeff Garzik 	}
1500c6fd2807SJeff Garzik 
1501c6fd2807SJeff Garzik 	/* initialize internal qc */
150228361c40SJens Axboe 	qc = __ata_qc_from_tag(ap, ATA_TAG_INTERNAL);
1503c6fd2807SJeff Garzik 
150428361c40SJens Axboe 	qc->tag = ATA_TAG_INTERNAL;
150528361c40SJens Axboe 	qc->hw_tag = 0;
1506c6fd2807SJeff Garzik 	qc->scsicmd = NULL;
1507c6fd2807SJeff Garzik 	qc->ap = ap;
1508c6fd2807SJeff Garzik 	qc->dev = dev;
1509c6fd2807SJeff Garzik 	ata_qc_reinit(qc);
1510c6fd2807SJeff Garzik 
15119af5c9c9STejun Heo 	preempted_tag = link->active_tag;
15129af5c9c9STejun Heo 	preempted_sactive = link->sactive;
1513c6fd2807SJeff Garzik 	preempted_qc_active = ap->qc_active;
1514da917d69STejun Heo 	preempted_nr_active_links = ap->nr_active_links;
15159af5c9c9STejun Heo 	link->active_tag = ATA_TAG_POISON;
15169af5c9c9STejun Heo 	link->sactive = 0;
1517c6fd2807SJeff Garzik 	ap->qc_active = 0;
1518da917d69STejun Heo 	ap->nr_active_links = 0;
1519c6fd2807SJeff Garzik 
1520c6fd2807SJeff Garzik 	/* prepare & issue qc */
1521c6fd2807SJeff Garzik 	qc->tf = *tf;
1522c6fd2807SJeff Garzik 	if (cdb)
1523c6fd2807SJeff Garzik 		memcpy(qc->cdb, cdb, ATAPI_CDB_LEN);
1524e771451cSVincent Pelletier 
1525e771451cSVincent Pelletier 	/* some SATA bridges need us to indicate data xfer direction */
1526e771451cSVincent Pelletier 	if (tf->protocol == ATAPI_PROT_DMA && (dev->flags & ATA_DFLAG_DMADIR) &&
1527e771451cSVincent Pelletier 	    dma_dir == DMA_FROM_DEVICE)
1528e771451cSVincent Pelletier 		qc->tf.feature |= ATAPI_DMADIR;
1529e771451cSVincent Pelletier 
1530c6fd2807SJeff Garzik 	qc->flags |= ATA_QCFLAG_RESULT_TF;
1531c6fd2807SJeff Garzik 	qc->dma_dir = dma_dir;
1532c6fd2807SJeff Garzik 	if (dma_dir != DMA_NONE) {
15332432697bSTejun Heo 		unsigned int i, buflen = 0;
153487260216SJens Axboe 		struct scatterlist *sg;
15352432697bSTejun Heo 
153687260216SJens Axboe 		for_each_sg(sgl, sg, n_elem, i)
153787260216SJens Axboe 			buflen += sg->length;
15382432697bSTejun Heo 
153987260216SJens Axboe 		ata_sg_init(qc, sgl, n_elem);
154049c80429SBrian King 		qc->nbytes = buflen;
1541c6fd2807SJeff Garzik 	}
1542c6fd2807SJeff Garzik 
1543c6fd2807SJeff Garzik 	qc->private_data = &wait;
1544c6fd2807SJeff Garzik 	qc->complete_fn = ata_qc_complete_internal;
1545c6fd2807SJeff Garzik 
1546c6fd2807SJeff Garzik 	ata_qc_issue(qc);
1547c6fd2807SJeff Garzik 
1548c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1549c6fd2807SJeff Garzik 
155087fbc5a0STejun Heo 	if (!timeout) {
155187fbc5a0STejun Heo 		if (ata_probe_timeout)
1552341c2c95STejun Heo 			timeout = ata_probe_timeout * 1000;
155387fbc5a0STejun Heo 		else {
155487fbc5a0STejun Heo 			timeout = ata_internal_cmd_timeout(dev, command);
155587fbc5a0STejun Heo 			auto_timeout = 1;
155687fbc5a0STejun Heo 		}
155787fbc5a0STejun Heo 	}
15582b789108STejun Heo 
1559c0c362b6STejun Heo 	if (ap->ops->error_handler)
1560c0c362b6STejun Heo 		ata_eh_release(ap);
1561c0c362b6STejun Heo 
15622b789108STejun Heo 	rc = wait_for_completion_timeout(&wait, msecs_to_jiffies(timeout));
1563c6fd2807SJeff Garzik 
1564c0c362b6STejun Heo 	if (ap->ops->error_handler)
1565c0c362b6STejun Heo 		ata_eh_acquire(ap);
1566c0c362b6STejun Heo 
1567c429137aSTejun Heo 	ata_sff_flush_pio_task(ap);
1568c6fd2807SJeff Garzik 
1569c6fd2807SJeff Garzik 	if (!rc) {
1570c6fd2807SJeff Garzik 		spin_lock_irqsave(ap->lock, flags);
1571c6fd2807SJeff Garzik 
1572c6fd2807SJeff Garzik 		/* We're racing with irq here.  If we lose, the
1573c6fd2807SJeff Garzik 		 * following test prevents us from completing the qc
1574c6fd2807SJeff Garzik 		 * twice.  If we win, the port is frozen and will be
1575c6fd2807SJeff Garzik 		 * cleaned up by ->post_internal_cmd().
1576c6fd2807SJeff Garzik 		 */
1577c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_ACTIVE) {
1578c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_TIMEOUT;
1579c6fd2807SJeff Garzik 
1580c6fd2807SJeff Garzik 			if (ap->ops->error_handler)
1581c6fd2807SJeff Garzik 				ata_port_freeze(ap);
1582c6fd2807SJeff Garzik 			else
1583c6fd2807SJeff Garzik 				ata_qc_complete(qc);
1584c6fd2807SJeff Garzik 
158516169fb7STomas Henzl 			ata_dev_warn(dev, "qc timeout after %u msecs (cmd 0x%x)\n",
158616169fb7STomas Henzl 				     timeout, command);
1587c6fd2807SJeff Garzik 		}
1588c6fd2807SJeff Garzik 
1589c6fd2807SJeff Garzik 		spin_unlock_irqrestore(ap->lock, flags);
1590c6fd2807SJeff Garzik 	}
1591c6fd2807SJeff Garzik 
1592c6fd2807SJeff Garzik 	/* do post_internal_cmd */
1593c6fd2807SJeff Garzik 	if (ap->ops->post_internal_cmd)
1594c6fd2807SJeff Garzik 		ap->ops->post_internal_cmd(qc);
1595c6fd2807SJeff Garzik 
1596a51d644aSTejun Heo 	/* perform minimal error analysis */
159787629312SNiklas Cassel 	if (qc->flags & ATA_QCFLAG_EH) {
1598efcef265SSergey Shtylyov 		if (qc->result_tf.status & (ATA_ERR | ATA_DF))
1599a51d644aSTejun Heo 			qc->err_mask |= AC_ERR_DEV;
1600a51d644aSTejun Heo 
1601a51d644aSTejun Heo 		if (!qc->err_mask)
1602c6fd2807SJeff Garzik 			qc->err_mask |= AC_ERR_OTHER;
1603a51d644aSTejun Heo 
1604a51d644aSTejun Heo 		if (qc->err_mask & ~AC_ERR_OTHER)
1605a51d644aSTejun Heo 			qc->err_mask &= ~AC_ERR_OTHER;
16062dae9955SDamien Le Moal 	} else if (qc->tf.command == ATA_CMD_REQ_SENSE_DATA) {
1607efcef265SSergey Shtylyov 		qc->result_tf.status |= ATA_SENSE;
1608c6fd2807SJeff Garzik 	}
1609c6fd2807SJeff Garzik 
1610c6fd2807SJeff Garzik 	/* finish up */
1611c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
1612c6fd2807SJeff Garzik 
1613c6fd2807SJeff Garzik 	*tf = qc->result_tf;
1614c6fd2807SJeff Garzik 	err_mask = qc->err_mask;
1615c6fd2807SJeff Garzik 
1616c6fd2807SJeff Garzik 	ata_qc_free(qc);
16179af5c9c9STejun Heo 	link->active_tag = preempted_tag;
16189af5c9c9STejun Heo 	link->sactive = preempted_sactive;
1619c6fd2807SJeff Garzik 	ap->qc_active = preempted_qc_active;
1620da917d69STejun Heo 	ap->nr_active_links = preempted_nr_active_links;
1621c6fd2807SJeff Garzik 
1622c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
1623c6fd2807SJeff Garzik 
162487fbc5a0STejun Heo 	if ((err_mask & AC_ERR_TIMEOUT) && auto_timeout)
162587fbc5a0STejun Heo 		ata_internal_cmd_timed_out(dev, command);
162687fbc5a0STejun Heo 
1627c6fd2807SJeff Garzik 	return err_mask;
1628c6fd2807SJeff Garzik }
1629c6fd2807SJeff Garzik 
1630c6fd2807SJeff Garzik /**
163133480a0eSTejun Heo  *	ata_exec_internal - execute libata internal command
16322432697bSTejun Heo  *	@dev: Device to which the command is sent
16332432697bSTejun Heo  *	@tf: Taskfile registers for the command and the result
16342432697bSTejun Heo  *	@cdb: CDB for packet command
1635e227867fSMasanari Iida  *	@dma_dir: Data transfer direction of the command
16362432697bSTejun Heo  *	@buf: Data buffer of the command
16372432697bSTejun Heo  *	@buflen: Length of data buffer
16382b789108STejun Heo  *	@timeout: Timeout in msecs (0 for default)
16392432697bSTejun Heo  *
16402432697bSTejun Heo  *	Wrapper around ata_exec_internal_sg() which takes simple
16412432697bSTejun Heo  *	buffer instead of sg list.
16422432697bSTejun Heo  *
16432432697bSTejun Heo  *	LOCKING:
16442432697bSTejun Heo  *	None.  Should be called with kernel context, might sleep.
16452432697bSTejun Heo  *
16462432697bSTejun Heo  *	RETURNS:
16472432697bSTejun Heo  *	Zero on success, AC_ERR_* mask on failure
16482432697bSTejun Heo  */
16492432697bSTejun Heo unsigned ata_exec_internal(struct ata_device *dev,
16502432697bSTejun Heo 			   struct ata_taskfile *tf, const u8 *cdb,
16512b789108STejun Heo 			   int dma_dir, void *buf, unsigned int buflen,
165261176eedSSergey Shtylyov 			   unsigned int timeout)
16532432697bSTejun Heo {
165433480a0eSTejun Heo 	struct scatterlist *psg = NULL, sg;
165533480a0eSTejun Heo 	unsigned int n_elem = 0;
16562432697bSTejun Heo 
165733480a0eSTejun Heo 	if (dma_dir != DMA_NONE) {
165833480a0eSTejun Heo 		WARN_ON(!buf);
16592432697bSTejun Heo 		sg_init_one(&sg, buf, buflen);
166033480a0eSTejun Heo 		psg = &sg;
166133480a0eSTejun Heo 		n_elem++;
166233480a0eSTejun Heo 	}
16632432697bSTejun Heo 
16642b789108STejun Heo 	return ata_exec_internal_sg(dev, tf, cdb, dma_dir, psg, n_elem,
16652b789108STejun Heo 				    timeout);
16662432697bSTejun Heo }
16672432697bSTejun Heo 
16682432697bSTejun Heo /**
1669c6fd2807SJeff Garzik  *	ata_pio_need_iordy	-	check if iordy needed
1670c6fd2807SJeff Garzik  *	@adev: ATA device
1671c6fd2807SJeff Garzik  *
1672c6fd2807SJeff Garzik  *	Check if the current speed of the device requires IORDY. Used
1673c6fd2807SJeff Garzik  *	by various controllers for chip configuration.
1674c6fd2807SJeff Garzik  */
1675c6fd2807SJeff Garzik unsigned int ata_pio_need_iordy(const struct ata_device *adev)
1676c6fd2807SJeff Garzik {
16770d9e6659STejun Heo 	/* Don't set IORDY if we're preparing for reset.  IORDY may
16780d9e6659STejun Heo 	 * lead to controller lock up on certain controllers if the
16790d9e6659STejun Heo 	 * port is not occupied.  See bko#11703 for details.
16800d9e6659STejun Heo 	 */
16810d9e6659STejun Heo 	if (adev->link->ap->pflags & ATA_PFLAG_RESETTING)
16820d9e6659STejun Heo 		return 0;
16830d9e6659STejun Heo 	/* Controller doesn't support IORDY.  Probably a pointless
16840d9e6659STejun Heo 	 * check as the caller should know this.
16850d9e6659STejun Heo 	 */
16869af5c9c9STejun Heo 	if (adev->link->ap->flags & ATA_FLAG_NO_IORDY)
1687c6fd2807SJeff Garzik 		return 0;
16885c18c4d2SDavid Daney 	/* CF spec. r4.1 Table 22 says no iordy on PIO5 and PIO6.  */
16895c18c4d2SDavid Daney 	if (ata_id_is_cfa(adev->id)
16905c18c4d2SDavid Daney 	    && (adev->pio_mode == XFER_PIO_5 || adev->pio_mode == XFER_PIO_6))
16915c18c4d2SDavid Daney 		return 0;
1692432729f0SAlan Cox 	/* PIO3 and higher it is mandatory */
1693432729f0SAlan Cox 	if (adev->pio_mode > XFER_PIO_2)
1694c6fd2807SJeff Garzik 		return 1;
1695432729f0SAlan Cox 	/* We turn it on when possible */
1696432729f0SAlan Cox 	if (ata_id_has_iordy(adev->id))
1697432729f0SAlan Cox 		return 1;
1698432729f0SAlan Cox 	return 0;
1699432729f0SAlan Cox }
1700a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pio_need_iordy);
1701c6fd2807SJeff Garzik 
1702432729f0SAlan Cox /**
1703432729f0SAlan Cox  *	ata_pio_mask_no_iordy	-	Return the non IORDY mask
1704432729f0SAlan Cox  *	@adev: ATA device
1705432729f0SAlan Cox  *
1706432729f0SAlan Cox  *	Compute the highest mode possible if we are not using iordy. Return
1707432729f0SAlan Cox  *	-1 if no iordy mode is available.
1708432729f0SAlan Cox  */
1709432729f0SAlan Cox static u32 ata_pio_mask_no_iordy(const struct ata_device *adev)
1710432729f0SAlan Cox {
1711c6fd2807SJeff Garzik 	/* If we have no drive specific rule, then PIO 2 is non IORDY */
1712c6fd2807SJeff Garzik 	if (adev->id[ATA_ID_FIELD_VALID] & 2) {	/* EIDE */
1713432729f0SAlan Cox 		u16 pio = adev->id[ATA_ID_EIDE_PIO];
1714c6fd2807SJeff Garzik 		/* Is the speed faster than the drive allows non IORDY ? */
1715c6fd2807SJeff Garzik 		if (pio) {
1716c6fd2807SJeff Garzik 			/* This is cycle times not frequency - watch the logic! */
1717c6fd2807SJeff Garzik 			if (pio > 240)	/* PIO2 is 240nS per cycle */
1718432729f0SAlan Cox 				return 3 << ATA_SHIFT_PIO;
1719432729f0SAlan Cox 			return 7 << ATA_SHIFT_PIO;
1720c6fd2807SJeff Garzik 		}
1721c6fd2807SJeff Garzik 	}
1722432729f0SAlan Cox 	return 3 << ATA_SHIFT_PIO;
1723c6fd2807SJeff Garzik }
1724c6fd2807SJeff Garzik 
1725c6fd2807SJeff Garzik /**
1726963e4975SAlan Cox  *	ata_do_dev_read_id		-	default ID read method
1727963e4975SAlan Cox  *	@dev: device
1728963e4975SAlan Cox  *	@tf: proposed taskfile
1729963e4975SAlan Cox  *	@id: data buffer
1730963e4975SAlan Cox  *
1731963e4975SAlan Cox  *	Issue the identify taskfile and hand back the buffer containing
1732963e4975SAlan Cox  *	identify data. For some RAID controllers and for pre ATA devices
1733963e4975SAlan Cox  *	this function is wrapped or replaced by the driver
1734963e4975SAlan Cox  */
1735963e4975SAlan Cox unsigned int ata_do_dev_read_id(struct ata_device *dev,
17360561e514SDamien Le Moal 				struct ata_taskfile *tf, __le16 *id)
1737963e4975SAlan Cox {
1738963e4975SAlan Cox 	return ata_exec_internal(dev, tf, NULL, DMA_FROM_DEVICE,
1739963e4975SAlan Cox 				     id, sizeof(id[0]) * ATA_ID_WORDS, 0);
1740963e4975SAlan Cox }
1741a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_do_dev_read_id);
1742963e4975SAlan Cox 
1743963e4975SAlan Cox /**
1744c6fd2807SJeff Garzik  *	ata_dev_read_id - Read ID data from the specified device
1745c6fd2807SJeff Garzik  *	@dev: target device
1746c6fd2807SJeff Garzik  *	@p_class: pointer to class of the target device (may be changed)
1747bff04647STejun Heo  *	@flags: ATA_READID_* flags
1748c6fd2807SJeff Garzik  *	@id: buffer to read IDENTIFY data into
1749c6fd2807SJeff Garzik  *
1750c6fd2807SJeff Garzik  *	Read ID data from the specified device.  ATA_CMD_ID_ATA is
1751c6fd2807SJeff Garzik  *	performed on ATA devices and ATA_CMD_ID_ATAPI on ATAPI
1752c6fd2807SJeff Garzik  *	devices.  This function also issues ATA_CMD_INIT_DEV_PARAMS
1753c6fd2807SJeff Garzik  *	for pre-ATA4 drives.
1754c6fd2807SJeff Garzik  *
175550a99018SAlan Cox  *	FIXME: ATA_CMD_ID_ATA is optional for early drives and right
175650a99018SAlan Cox  *	now we abort if we hit that case.
175750a99018SAlan Cox  *
1758c6fd2807SJeff Garzik  *	LOCKING:
1759c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
1760c6fd2807SJeff Garzik  *
1761c6fd2807SJeff Garzik  *	RETURNS:
1762c6fd2807SJeff Garzik  *	0 on success, -errno otherwise.
1763c6fd2807SJeff Garzik  */
1764c6fd2807SJeff Garzik int ata_dev_read_id(struct ata_device *dev, unsigned int *p_class,
1765bff04647STejun Heo 		    unsigned int flags, u16 *id)
1766c6fd2807SJeff Garzik {
17679af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
1768c6fd2807SJeff Garzik 	unsigned int class = *p_class;
1769c6fd2807SJeff Garzik 	struct ata_taskfile tf;
1770c6fd2807SJeff Garzik 	unsigned int err_mask = 0;
1771c6fd2807SJeff Garzik 	const char *reason;
177279b42babSTejun Heo 	bool is_semb = class == ATA_DEV_SEMB;
177354936f8bSTejun Heo 	int may_fallback = 1, tried_spinup = 0;
1774c6fd2807SJeff Garzik 	int rc;
1775c6fd2807SJeff Garzik 
1776c6fd2807SJeff Garzik retry:
1777c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
1778c6fd2807SJeff Garzik 
1779c6fd2807SJeff Garzik 	switch (class) {
178079b42babSTejun Heo 	case ATA_DEV_SEMB:
178179b42babSTejun Heo 		class = ATA_DEV_ATA;	/* some hard drives report SEMB sig */
1782df561f66SGustavo A. R. Silva 		fallthrough;
1783c6fd2807SJeff Garzik 	case ATA_DEV_ATA:
17849162c657SHannes Reinecke 	case ATA_DEV_ZAC:
1785c6fd2807SJeff Garzik 		tf.command = ATA_CMD_ID_ATA;
1786c6fd2807SJeff Garzik 		break;
1787c6fd2807SJeff Garzik 	case ATA_DEV_ATAPI:
1788c6fd2807SJeff Garzik 		tf.command = ATA_CMD_ID_ATAPI;
1789c6fd2807SJeff Garzik 		break;
1790c6fd2807SJeff Garzik 	default:
1791c6fd2807SJeff Garzik 		rc = -ENODEV;
1792c6fd2807SJeff Garzik 		reason = "unsupported class";
1793c6fd2807SJeff Garzik 		goto err_out;
1794c6fd2807SJeff Garzik 	}
1795c6fd2807SJeff Garzik 
1796c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_PIO;
179781afe893STejun Heo 
179881afe893STejun Heo 	/* Some devices choke if TF registers contain garbage.  Make
179981afe893STejun Heo 	 * sure those are properly initialized.
180081afe893STejun Heo 	 */
180181afe893STejun Heo 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
180281afe893STejun Heo 
180381afe893STejun Heo 	/* Device presence detection is unreliable on some
180481afe893STejun Heo 	 * controllers.  Always poll IDENTIFY if available.
180581afe893STejun Heo 	 */
180681afe893STejun Heo 	tf.flags |= ATA_TFLAG_POLLING;
1807c6fd2807SJeff Garzik 
1808963e4975SAlan Cox 	if (ap->ops->read_id)
18090561e514SDamien Le Moal 		err_mask = ap->ops->read_id(dev, &tf, (__le16 *)id);
1810963e4975SAlan Cox 	else
18110561e514SDamien Le Moal 		err_mask = ata_do_dev_read_id(dev, &tf, (__le16 *)id);
1812963e4975SAlan Cox 
1813c6fd2807SJeff Garzik 	if (err_mask) {
1814800b3996STejun Heo 		if (err_mask & AC_ERR_NODEV_HINT) {
1815a9a79dfeSJoe Perches 			ata_dev_dbg(dev, "NODEV after polling detection\n");
181655a8e2c8STejun Heo 			return -ENOENT;
181755a8e2c8STejun Heo 		}
181855a8e2c8STejun Heo 
181979b42babSTejun Heo 		if (is_semb) {
1820a9a79dfeSJoe Perches 			ata_dev_info(dev,
1821a9a79dfeSJoe Perches 		     "IDENTIFY failed on device w/ SEMB sig, disabled\n");
182279b42babSTejun Heo 			/* SEMB is not supported yet */
182379b42babSTejun Heo 			*p_class = ATA_DEV_SEMB_UNSUP;
182479b42babSTejun Heo 			return 0;
182579b42babSTejun Heo 		}
182679b42babSTejun Heo 
1827efcef265SSergey Shtylyov 		if ((err_mask == AC_ERR_DEV) && (tf.error & ATA_ABORTED)) {
18281ffc151fSTejun Heo 			/* Device or controller might have reported
18291ffc151fSTejun Heo 			 * the wrong device class.  Give a shot at the
18301ffc151fSTejun Heo 			 * other IDENTIFY if the current one is
18311ffc151fSTejun Heo 			 * aborted by the device.
183254936f8bSTejun Heo 			 */
18331ffc151fSTejun Heo 			if (may_fallback) {
183454936f8bSTejun Heo 				may_fallback = 0;
183554936f8bSTejun Heo 
183654936f8bSTejun Heo 				if (class == ATA_DEV_ATA)
183754936f8bSTejun Heo 					class = ATA_DEV_ATAPI;
183854936f8bSTejun Heo 				else
183954936f8bSTejun Heo 					class = ATA_DEV_ATA;
184054936f8bSTejun Heo 				goto retry;
184154936f8bSTejun Heo 			}
184254936f8bSTejun Heo 
18431ffc151fSTejun Heo 			/* Control reaches here iff the device aborted
18441ffc151fSTejun Heo 			 * both flavors of IDENTIFYs which happens
18451ffc151fSTejun Heo 			 * sometimes with phantom devices.
18461ffc151fSTejun Heo 			 */
1847a9a79dfeSJoe Perches 			ata_dev_dbg(dev,
18481ffc151fSTejun Heo 				    "both IDENTIFYs aborted, assuming NODEV\n");
18491ffc151fSTejun Heo 			return -ENOENT;
18501ffc151fSTejun Heo 		}
18511ffc151fSTejun Heo 
1852c6fd2807SJeff Garzik 		rc = -EIO;
1853c6fd2807SJeff Garzik 		reason = "I/O error";
1854c6fd2807SJeff Garzik 		goto err_out;
1855c6fd2807SJeff Garzik 	}
1856c6fd2807SJeff Garzik 
185743c9c591STejun Heo 	if (dev->horkage & ATA_HORKAGE_DUMP_ID) {
18584baa5745SHannes Reinecke 		ata_dev_info(dev, "dumping IDENTIFY data, "
185943c9c591STejun Heo 			    "class=%d may_fallback=%d tried_spinup=%d\n",
186043c9c591STejun Heo 			    class, may_fallback, tried_spinup);
18614baa5745SHannes Reinecke 		print_hex_dump(KERN_INFO, "", DUMP_PREFIX_OFFSET,
186243c9c591STejun Heo 			       16, 2, id, ATA_ID_WORDS * sizeof(*id), true);
186343c9c591STejun Heo 	}
186443c9c591STejun Heo 
186554936f8bSTejun Heo 	/* Falling back doesn't make sense if ID data was read
186654936f8bSTejun Heo 	 * successfully at least once.
186754936f8bSTejun Heo 	 */
186854936f8bSTejun Heo 	may_fallback = 0;
186954936f8bSTejun Heo 
1870c6fd2807SJeff Garzik 	swap_buf_le16(id, ATA_ID_WORDS);
1871c6fd2807SJeff Garzik 
1872c6fd2807SJeff Garzik 	/* sanity check */
1873c6fd2807SJeff Garzik 	rc = -EINVAL;
18746070068bSAlan Cox 	reason = "device reports invalid type";
18754a3381feSJeff Garzik 
18769162c657SHannes Reinecke 	if (class == ATA_DEV_ATA || class == ATA_DEV_ZAC) {
18774a3381feSJeff Garzik 		if (!ata_id_is_ata(id) && !ata_id_is_cfa(id))
18784a3381feSJeff Garzik 			goto err_out;
1879db63a4c8SAndy Whitcroft 		if (ap->host->flags & ATA_HOST_IGNORE_ATA &&
1880db63a4c8SAndy Whitcroft 							ata_id_is_ata(id)) {
1881db63a4c8SAndy Whitcroft 			ata_dev_dbg(dev,
1882db63a4c8SAndy Whitcroft 				"host indicates ignore ATA devices, ignored\n");
1883db63a4c8SAndy Whitcroft 			return -ENOENT;
1884db63a4c8SAndy Whitcroft 		}
18854a3381feSJeff Garzik 	} else {
18864a3381feSJeff Garzik 		if (ata_id_is_ata(id))
1887c6fd2807SJeff Garzik 			goto err_out;
1888c6fd2807SJeff Garzik 	}
1889c6fd2807SJeff Garzik 
1890169439c2SMark Lord 	if (!tried_spinup && (id[2] == 0x37c8 || id[2] == 0x738c)) {
1891169439c2SMark Lord 		tried_spinup = 1;
1892169439c2SMark Lord 		/*
1893169439c2SMark Lord 		 * Drive powered-up in standby mode, and requires a specific
1894169439c2SMark Lord 		 * SET_FEATURES spin-up subcommand before it will accept
1895169439c2SMark Lord 		 * anything other than the original IDENTIFY command.
1896169439c2SMark Lord 		 */
1897218f3d30SJeff Garzik 		err_mask = ata_dev_set_feature(dev, SETFEATURES_SPINUP, 0);
1898fb0582f9SRyan Power 		if (err_mask && id[2] != 0x738c) {
1899169439c2SMark Lord 			rc = -EIO;
1900169439c2SMark Lord 			reason = "SPINUP failed";
1901169439c2SMark Lord 			goto err_out;
1902169439c2SMark Lord 		}
1903169439c2SMark Lord 		/*
1904169439c2SMark Lord 		 * If the drive initially returned incomplete IDENTIFY info,
1905169439c2SMark Lord 		 * we now must reissue the IDENTIFY command.
1906169439c2SMark Lord 		 */
1907169439c2SMark Lord 		if (id[2] == 0x37c8)
1908169439c2SMark Lord 			goto retry;
1909169439c2SMark Lord 	}
1910169439c2SMark Lord 
19119162c657SHannes Reinecke 	if ((flags & ATA_READID_POSTRESET) &&
19129162c657SHannes Reinecke 	    (class == ATA_DEV_ATA || class == ATA_DEV_ZAC)) {
1913c6fd2807SJeff Garzik 		/*
1914c6fd2807SJeff Garzik 		 * The exact sequence expected by certain pre-ATA4 drives is:
1915c6fd2807SJeff Garzik 		 * SRST RESET
191650a99018SAlan Cox 		 * IDENTIFY (optional in early ATA)
191750a99018SAlan Cox 		 * INITIALIZE DEVICE PARAMETERS (later IDE and ATA)
1918c6fd2807SJeff Garzik 		 * anything else..
1919c6fd2807SJeff Garzik 		 * Some drives were very specific about that exact sequence.
192050a99018SAlan Cox 		 *
192150a99018SAlan Cox 		 * Note that ATA4 says lba is mandatory so the second check
1922c9404c9cSAdam Buchbinder 		 * should never trigger.
1923c6fd2807SJeff Garzik 		 */
1924c6fd2807SJeff Garzik 		if (ata_id_major_version(id) < 4 || !ata_id_has_lba(id)) {
1925c6fd2807SJeff Garzik 			err_mask = ata_dev_init_params(dev, id[3], id[6]);
1926c6fd2807SJeff Garzik 			if (err_mask) {
1927c6fd2807SJeff Garzik 				rc = -EIO;
1928c6fd2807SJeff Garzik 				reason = "INIT_DEV_PARAMS failed";
1929c6fd2807SJeff Garzik 				goto err_out;
1930c6fd2807SJeff Garzik 			}
1931c6fd2807SJeff Garzik 
1932c6fd2807SJeff Garzik 			/* current CHS translation info (id[53-58]) might be
1933c6fd2807SJeff Garzik 			 * changed. reread the identify device info.
1934c6fd2807SJeff Garzik 			 */
1935bff04647STejun Heo 			flags &= ~ATA_READID_POSTRESET;
1936c6fd2807SJeff Garzik 			goto retry;
1937c6fd2807SJeff Garzik 		}
1938c6fd2807SJeff Garzik 	}
1939c6fd2807SJeff Garzik 
1940c6fd2807SJeff Garzik 	*p_class = class;
1941c6fd2807SJeff Garzik 
1942c6fd2807SJeff Garzik 	return 0;
1943c6fd2807SJeff Garzik 
1944c6fd2807SJeff Garzik  err_out:
1945a9a79dfeSJoe Perches 	ata_dev_warn(dev, "failed to IDENTIFY (%s, err_mask=0x%x)\n",
1946a9a79dfeSJoe Perches 		     reason, err_mask);
1947c6fd2807SJeff Garzik 	return rc;
1948c6fd2807SJeff Garzik }
1949c6fd2807SJeff Garzik 
1950f01f62c2SChristoph Hellwig /**
1951f01f62c2SChristoph Hellwig  *	ata_read_log_page - read a specific log page
1952f01f62c2SChristoph Hellwig  *	@dev: target device
1953f01f62c2SChristoph Hellwig  *	@log: log to read
1954f01f62c2SChristoph Hellwig  *	@page: page to read
1955f01f62c2SChristoph Hellwig  *	@buf: buffer to store read page
1956f01f62c2SChristoph Hellwig  *	@sectors: number of sectors to read
1957f01f62c2SChristoph Hellwig  *
1958f01f62c2SChristoph Hellwig  *	Read log page using READ_LOG_EXT command.
1959f01f62c2SChristoph Hellwig  *
1960f01f62c2SChristoph Hellwig  *	LOCKING:
1961f01f62c2SChristoph Hellwig  *	Kernel thread context (may sleep).
1962f01f62c2SChristoph Hellwig  *
1963f01f62c2SChristoph Hellwig  *	RETURNS:
1964f01f62c2SChristoph Hellwig  *	0 on success, AC_ERR_* mask otherwise.
1965f01f62c2SChristoph Hellwig  */
1966f01f62c2SChristoph Hellwig unsigned int ata_read_log_page(struct ata_device *dev, u8 log,
1967f01f62c2SChristoph Hellwig 			       u8 page, void *buf, unsigned int sectors)
1968f01f62c2SChristoph Hellwig {
1969f01f62c2SChristoph Hellwig 	unsigned long ap_flags = dev->link->ap->flags;
1970f01f62c2SChristoph Hellwig 	struct ata_taskfile tf;
1971f01f62c2SChristoph Hellwig 	unsigned int err_mask;
1972f01f62c2SChristoph Hellwig 	bool dma = false;
1973f01f62c2SChristoph Hellwig 
19744633778bSHannes Reinecke 	ata_dev_dbg(dev, "read log page - log 0x%x, page 0x%x\n", log, page);
1975f01f62c2SChristoph Hellwig 
1976f01f62c2SChristoph Hellwig 	/*
1977f01f62c2SChristoph Hellwig 	 * Return error without actually issuing the command on controllers
1978f01f62c2SChristoph Hellwig 	 * which e.g. lockup on a read log page.
1979f01f62c2SChristoph Hellwig 	 */
1980f01f62c2SChristoph Hellwig 	if (ap_flags & ATA_FLAG_NO_LOG_PAGE)
1981f01f62c2SChristoph Hellwig 		return AC_ERR_DEV;
1982f01f62c2SChristoph Hellwig 
1983f01f62c2SChristoph Hellwig retry:
1984f01f62c2SChristoph Hellwig 	ata_tf_init(dev, &tf);
1985f971a854SReimar Döffinger 	if (ata_dma_enabled(dev) && ata_id_has_read_log_dma_ext(dev->id) &&
19867cfdfdc8SDamien Le Moal 	    !(dev->horkage & ATA_HORKAGE_NO_DMA_LOG)) {
1987f01f62c2SChristoph Hellwig 		tf.command = ATA_CMD_READ_LOG_DMA_EXT;
1988f01f62c2SChristoph Hellwig 		tf.protocol = ATA_PROT_DMA;
1989f01f62c2SChristoph Hellwig 		dma = true;
1990f01f62c2SChristoph Hellwig 	} else {
1991f01f62c2SChristoph Hellwig 		tf.command = ATA_CMD_READ_LOG_EXT;
1992f01f62c2SChristoph Hellwig 		tf.protocol = ATA_PROT_PIO;
1993f01f62c2SChristoph Hellwig 		dma = false;
1994f01f62c2SChristoph Hellwig 	}
1995f01f62c2SChristoph Hellwig 	tf.lbal = log;
1996f01f62c2SChristoph Hellwig 	tf.lbam = page;
1997f01f62c2SChristoph Hellwig 	tf.nsect = sectors;
1998f01f62c2SChristoph Hellwig 	tf.hob_nsect = sectors >> 8;
1999f01f62c2SChristoph Hellwig 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_LBA48 | ATA_TFLAG_DEVICE;
2000f01f62c2SChristoph Hellwig 
2001f01f62c2SChristoph Hellwig 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_FROM_DEVICE,
2002f01f62c2SChristoph Hellwig 				     buf, sectors * ATA_SECT_SIZE, 0);
2003f01f62c2SChristoph Hellwig 
2004fc5c8aa7SDamien Le Moal 	if (err_mask) {
2005fc5c8aa7SDamien Le Moal 		if (dma) {
20067cfdfdc8SDamien Le Moal 			dev->horkage |= ATA_HORKAGE_NO_DMA_LOG;
20075122e53eSNiklas Cassel 			if (!ata_port_is_frozen(dev->link->ap))
2008f01f62c2SChristoph Hellwig 				goto retry;
2009f01f62c2SChristoph Hellwig 		}
201023ef63d5SDamien Le Moal 		ata_dev_err(dev,
201123ef63d5SDamien Le Moal 			    "Read log 0x%02x page 0x%02x failed, Emask 0x%x\n",
201223ef63d5SDamien Le Moal 			    (unsigned int)log, (unsigned int)page, err_mask);
2013fc5c8aa7SDamien Le Moal 	}
2014f01f62c2SChristoph Hellwig 
2015f01f62c2SChristoph Hellwig 	return err_mask;
2016f01f62c2SChristoph Hellwig }
2017f01f62c2SChristoph Hellwig 
2018c745dfc5STyler Erickson static int ata_log_supported(struct ata_device *dev, u8 log)
2019efe205a3SChristoph Hellwig {
2020efe205a3SChristoph Hellwig 	struct ata_port *ap = dev->link->ap;
2021efe205a3SChristoph Hellwig 
2022ac9f0c81SAnton Lundin 	if (dev->horkage & ATA_HORKAGE_NO_LOG_DIR)
2023c745dfc5STyler Erickson 		return 0;
2024ac9f0c81SAnton Lundin 
2025efe205a3SChristoph Hellwig 	if (ata_read_log_page(dev, ATA_LOG_DIRECTORY, 0, ap->sector_buf, 1))
2026c745dfc5STyler Erickson 		return 0;
2027c745dfc5STyler Erickson 	return get_unaligned_le16(&ap->sector_buf[log * 2]);
2028efe205a3SChristoph Hellwig }
2029efe205a3SChristoph Hellwig 
2030a0fd2454SChristoph Hellwig static bool ata_identify_page_supported(struct ata_device *dev, u8 page)
2031a0fd2454SChristoph Hellwig {
2032a0fd2454SChristoph Hellwig 	struct ata_port *ap = dev->link->ap;
2033a0fd2454SChristoph Hellwig 	unsigned int err, i;
2034a0fd2454SChristoph Hellwig 
2035636f6e2aSDamien Le Moal 	if (dev->horkage & ATA_HORKAGE_NO_ID_DEV_LOG)
2036636f6e2aSDamien Le Moal 		return false;
2037636f6e2aSDamien Le Moal 
2038a0fd2454SChristoph Hellwig 	if (!ata_log_supported(dev, ATA_LOG_IDENTIFY_DEVICE)) {
2039636f6e2aSDamien Le Moal 		/*
2040636f6e2aSDamien Le Moal 		 * IDENTIFY DEVICE data log is defined as mandatory starting
2041636f6e2aSDamien Le Moal 		 * with ACS-3 (ATA version 10). Warn about the missing log
2042636f6e2aSDamien Le Moal 		 * for drives which implement this ATA level or above.
2043636f6e2aSDamien Le Moal 		 */
2044636f6e2aSDamien Le Moal 		if (ata_id_major_version(dev->id) >= 10)
2045636f6e2aSDamien Le Moal 			ata_dev_warn(dev,
2046636f6e2aSDamien Le Moal 				"ATA Identify Device Log not supported\n");
2047636f6e2aSDamien Le Moal 		dev->horkage |= ATA_HORKAGE_NO_ID_DEV_LOG;
2048a0fd2454SChristoph Hellwig 		return false;
2049a0fd2454SChristoph Hellwig 	}
2050a0fd2454SChristoph Hellwig 
2051a0fd2454SChristoph Hellwig 	/*
2052a0fd2454SChristoph Hellwig 	 * Read IDENTIFY DEVICE data log, page 0, to figure out if the page is
2053a0fd2454SChristoph Hellwig 	 * supported.
2054a0fd2454SChristoph Hellwig 	 */
2055a0fd2454SChristoph Hellwig 	err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, 0, ap->sector_buf,
2056a0fd2454SChristoph Hellwig 				1);
2057fc5c8aa7SDamien Le Moal 	if (err)
2058a0fd2454SChristoph Hellwig 		return false;
2059a0fd2454SChristoph Hellwig 
2060a0fd2454SChristoph Hellwig 	for (i = 0; i < ap->sector_buf[8]; i++) {
2061a0fd2454SChristoph Hellwig 		if (ap->sector_buf[9 + i] == page)
2062a0fd2454SChristoph Hellwig 			return true;
2063a0fd2454SChristoph Hellwig 	}
2064a0fd2454SChristoph Hellwig 
2065a0fd2454SChristoph Hellwig 	return false;
2066a0fd2454SChristoph Hellwig }
2067a0fd2454SChristoph Hellwig 
20689062712fSTejun Heo static int ata_do_link_spd_horkage(struct ata_device *dev)
20699062712fSTejun Heo {
20709062712fSTejun Heo 	struct ata_link *plink = ata_dev_phys_link(dev);
20719062712fSTejun Heo 	u32 target, target_limit;
20729062712fSTejun Heo 
20739062712fSTejun Heo 	if (!sata_scr_valid(plink))
20749062712fSTejun Heo 		return 0;
20759062712fSTejun Heo 
20769062712fSTejun Heo 	if (dev->horkage & ATA_HORKAGE_1_5_GBPS)
20779062712fSTejun Heo 		target = 1;
20789062712fSTejun Heo 	else
20799062712fSTejun Heo 		return 0;
20809062712fSTejun Heo 
20819062712fSTejun Heo 	target_limit = (1 << target) - 1;
20829062712fSTejun Heo 
20839062712fSTejun Heo 	/* if already on stricter limit, no need to push further */
20849062712fSTejun Heo 	if (plink->sata_spd_limit <= target_limit)
20859062712fSTejun Heo 		return 0;
20869062712fSTejun Heo 
20879062712fSTejun Heo 	plink->sata_spd_limit = target_limit;
20889062712fSTejun Heo 
20899062712fSTejun Heo 	/* Request another EH round by returning -EAGAIN if link is
20909062712fSTejun Heo 	 * going faster than the target speed.  Forward progress is
20919062712fSTejun Heo 	 * guaranteed by setting sata_spd_limit to target_limit above.
20929062712fSTejun Heo 	 */
20939062712fSTejun Heo 	if (plink->sata_spd > target) {
2094a9a79dfeSJoe Perches 		ata_dev_info(dev, "applying link speed limit horkage to %s\n",
20959062712fSTejun Heo 			     sata_spd_string(target));
20969062712fSTejun Heo 		return -EAGAIN;
20979062712fSTejun Heo 	}
20989062712fSTejun Heo 	return 0;
20999062712fSTejun Heo }
21009062712fSTejun Heo 
2101c6fd2807SJeff Garzik static inline u8 ata_dev_knobble(struct ata_device *dev)
2102c6fd2807SJeff Garzik {
21039af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
21049ce8e307SJens Axboe 
21059ce8e307SJens Axboe 	if (ata_dev_blacklisted(dev) & ATA_HORKAGE_BRIDGE_OK)
21069ce8e307SJens Axboe 		return 0;
21079ce8e307SJens Axboe 
21089af5c9c9STejun Heo 	return ((ap->cbl == ATA_CBL_SATA) && (!ata_id_is_sata(dev->id)));
2109c6fd2807SJeff Garzik }
2110c6fd2807SJeff Garzik 
21115a233551SHannes Reinecke static void ata_dev_config_ncq_send_recv(struct ata_device *dev)
21125a233551SHannes Reinecke {
21135a233551SHannes Reinecke 	struct ata_port *ap = dev->link->ap;
21145a233551SHannes Reinecke 	unsigned int err_mask;
21155a233551SHannes Reinecke 
2116efe205a3SChristoph Hellwig 	if (!ata_log_supported(dev, ATA_LOG_NCQ_SEND_RECV)) {
2117efe205a3SChristoph Hellwig 		ata_dev_warn(dev, "NCQ Send/Recv Log not supported\n");
2118fe5af0ccSHannes Reinecke 		return;
2119fe5af0ccSHannes Reinecke 	}
21205a233551SHannes Reinecke 	err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_SEND_RECV,
21215a233551SHannes Reinecke 				     0, ap->sector_buf, 1);
2122fc5c8aa7SDamien Le Moal 	if (!err_mask) {
21235a233551SHannes Reinecke 		u8 *cmds = dev->ncq_send_recv_cmds;
21245a233551SHannes Reinecke 
21255a233551SHannes Reinecke 		dev->flags |= ATA_DFLAG_NCQ_SEND_RECV;
21265a233551SHannes Reinecke 		memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_SEND_RECV_SIZE);
21275a233551SHannes Reinecke 
21285a233551SHannes Reinecke 		if (dev->horkage & ATA_HORKAGE_NO_NCQ_TRIM) {
21295a233551SHannes Reinecke 			ata_dev_dbg(dev, "disabling queued TRIM support\n");
21305a233551SHannes Reinecke 			cmds[ATA_LOG_NCQ_SEND_RECV_DSM_OFFSET] &=
21315a233551SHannes Reinecke 				~ATA_LOG_NCQ_SEND_RECV_DSM_TRIM;
21325a233551SHannes Reinecke 		}
21335a233551SHannes Reinecke 	}
21345a233551SHannes Reinecke }
21355a233551SHannes Reinecke 
2136284b3b77SHannes Reinecke static void ata_dev_config_ncq_non_data(struct ata_device *dev)
2137284b3b77SHannes Reinecke {
2138284b3b77SHannes Reinecke 	struct ata_port *ap = dev->link->ap;
2139284b3b77SHannes Reinecke 	unsigned int err_mask;
2140284b3b77SHannes Reinecke 
2141efe205a3SChristoph Hellwig 	if (!ata_log_supported(dev, ATA_LOG_NCQ_NON_DATA)) {
2142284b3b77SHannes Reinecke 		ata_dev_warn(dev,
2143284b3b77SHannes Reinecke 			     "NCQ Send/Recv Log not supported\n");
2144284b3b77SHannes Reinecke 		return;
2145284b3b77SHannes Reinecke 	}
2146284b3b77SHannes Reinecke 	err_mask = ata_read_log_page(dev, ATA_LOG_NCQ_NON_DATA,
2147284b3b77SHannes Reinecke 				     0, ap->sector_buf, 1);
2148fc5c8aa7SDamien Le Moal 	if (!err_mask) {
2149284b3b77SHannes Reinecke 		u8 *cmds = dev->ncq_non_data_cmds;
2150284b3b77SHannes Reinecke 
2151284b3b77SHannes Reinecke 		memcpy(cmds, ap->sector_buf, ATA_LOG_NCQ_NON_DATA_SIZE);
2152284b3b77SHannes Reinecke 	}
2153284b3b77SHannes Reinecke }
2154284b3b77SHannes Reinecke 
21558e061784SAdam Manzanares static void ata_dev_config_ncq_prio(struct ata_device *dev)
21568e061784SAdam Manzanares {
21578e061784SAdam Manzanares 	struct ata_port *ap = dev->link->ap;
21588e061784SAdam Manzanares 	unsigned int err_mask;
21598e061784SAdam Manzanares 
216006f6c4c6SDamien Le Moal 	if (!ata_identify_page_supported(dev, ATA_LOG_SATA_SETTINGS))
216106f6c4c6SDamien Le Moal 		return;
216206f6c4c6SDamien Le Moal 
21638e061784SAdam Manzanares 	err_mask = ata_read_log_page(dev,
21641d51d5f3SChristoph Hellwig 				     ATA_LOG_IDENTIFY_DEVICE,
21658e061784SAdam Manzanares 				     ATA_LOG_SATA_SETTINGS,
21668e061784SAdam Manzanares 				     ap->sector_buf,
21678e061784SAdam Manzanares 				     1);
2168fc5c8aa7SDamien Le Moal 	if (err_mask)
21692360fa18SDamien Le Moal 		goto not_supported;
21708e061784SAdam Manzanares 
21712360fa18SDamien Le Moal 	if (!(ap->sector_buf[ATA_LOG_NCQ_PRIO_OFFSET] & BIT(3)))
21722360fa18SDamien Le Moal 		goto not_supported;
21732360fa18SDamien Le Moal 
21748e061784SAdam Manzanares 	dev->flags |= ATA_DFLAG_NCQ_PRIO;
21758e061784SAdam Manzanares 
21762360fa18SDamien Le Moal 	return;
21772360fa18SDamien Le Moal 
21782360fa18SDamien Le Moal not_supported:
2179e00923c5SDamien Le Moal 	dev->flags &= ~ATA_DFLAG_NCQ_PRIO_ENABLED;
21802360fa18SDamien Le Moal 	dev->flags &= ~ATA_DFLAG_NCQ_PRIO;
21818e061784SAdam Manzanares }
21828e061784SAdam Manzanares 
21837a8526a5SKate Hsuan static bool ata_dev_check_adapter(struct ata_device *dev,
21847a8526a5SKate Hsuan 				  unsigned short vendor_id)
21857a8526a5SKate Hsuan {
21867a8526a5SKate Hsuan 	struct pci_dev *pcidev = NULL;
21877a8526a5SKate Hsuan 	struct device *parent_dev = NULL;
21887a8526a5SKate Hsuan 
21897a8526a5SKate Hsuan 	for (parent_dev = dev->tdev.parent; parent_dev != NULL;
21907a8526a5SKate Hsuan 	     parent_dev = parent_dev->parent) {
21917a8526a5SKate Hsuan 		if (dev_is_pci(parent_dev)) {
21927a8526a5SKate Hsuan 			pcidev = to_pci_dev(parent_dev);
21937a8526a5SKate Hsuan 			if (pcidev->vendor == vendor_id)
21947a8526a5SKate Hsuan 				return true;
21957a8526a5SKate Hsuan 			break;
21967a8526a5SKate Hsuan 		}
21977a8526a5SKate Hsuan 	}
21987a8526a5SKate Hsuan 
21997a8526a5SKate Hsuan 	return false;
22007a8526a5SKate Hsuan }
22017a8526a5SKate Hsuan 
2202388539f3SShaohua Li static int ata_dev_config_ncq(struct ata_device *dev,
2203c6fd2807SJeff Garzik 			       char *desc, size_t desc_sz)
2204c6fd2807SJeff Garzik {
22059af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
2206c6fd2807SJeff Garzik 	int hdepth = 0, ddepth = ata_id_queue_depth(dev->id);
2207388539f3SShaohua Li 	unsigned int err_mask;
2208388539f3SShaohua Li 	char *aa_desc = "";
2209c6fd2807SJeff Garzik 
2210c6fd2807SJeff Garzik 	if (!ata_id_has_ncq(dev->id)) {
2211c6fd2807SJeff Garzik 		desc[0] = '\0';
2212388539f3SShaohua Li 		return 0;
2213c6fd2807SJeff Garzik 	}
2214cba97ea1SBartlomiej Zolnierkiewicz 	if (!IS_ENABLED(CONFIG_SATA_HOST))
2215cba97ea1SBartlomiej Zolnierkiewicz 		return 0;
221675683fe7STejun Heo 	if (dev->horkage & ATA_HORKAGE_NONCQ) {
22176919a0a6SAlan Cox 		snprintf(desc, desc_sz, "NCQ (not used)");
2218388539f3SShaohua Li 		return 0;
22196919a0a6SAlan Cox 	}
22207a8526a5SKate Hsuan 
22217a8526a5SKate Hsuan 	if (dev->horkage & ATA_HORKAGE_NO_NCQ_ON_ATI &&
22227a8526a5SKate Hsuan 	    ata_dev_check_adapter(dev, PCI_VENDOR_ID_ATI)) {
22237a8526a5SKate Hsuan 		snprintf(desc, desc_sz, "NCQ (not used)");
22247a8526a5SKate Hsuan 		return 0;
22257a8526a5SKate Hsuan 	}
22267a8526a5SKate Hsuan 
2227c6fd2807SJeff Garzik 	if (ap->flags & ATA_FLAG_NCQ) {
222869278f79SJens Axboe 		hdepth = min(ap->scsi_host->can_queue, ATA_MAX_QUEUE);
2229c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_NCQ;
2230c6fd2807SJeff Garzik 	}
2231c6fd2807SJeff Garzik 
2232388539f3SShaohua Li 	if (!(dev->horkage & ATA_HORKAGE_BROKEN_FPDMA_AA) &&
2233388539f3SShaohua Li 		(ap->flags & ATA_FLAG_FPDMA_AA) &&
2234388539f3SShaohua Li 		ata_id_has_fpdma_aa(dev->id)) {
2235388539f3SShaohua Li 		err_mask = ata_dev_set_feature(dev, SETFEATURES_SATA_ENABLE,
2236388539f3SShaohua Li 			SATA_FPDMA_AA);
2237388539f3SShaohua Li 		if (err_mask) {
2238a9a79dfeSJoe Perches 			ata_dev_err(dev,
2239a9a79dfeSJoe Perches 				    "failed to enable AA (error_mask=0x%x)\n",
2240a9a79dfeSJoe Perches 				    err_mask);
2241388539f3SShaohua Li 			if (err_mask != AC_ERR_DEV) {
2242388539f3SShaohua Li 				dev->horkage |= ATA_HORKAGE_BROKEN_FPDMA_AA;
2243388539f3SShaohua Li 				return -EIO;
2244388539f3SShaohua Li 			}
2245388539f3SShaohua Li 		} else
2246388539f3SShaohua Li 			aa_desc = ", AA";
2247388539f3SShaohua Li 	}
2248388539f3SShaohua Li 
2249c6fd2807SJeff Garzik 	if (hdepth >= ddepth)
2250388539f3SShaohua Li 		snprintf(desc, desc_sz, "NCQ (depth %d)%s", ddepth, aa_desc);
2251c6fd2807SJeff Garzik 	else
2252388539f3SShaohua Li 		snprintf(desc, desc_sz, "NCQ (depth %d/%d)%s", hdepth,
2253388539f3SShaohua Li 			ddepth, aa_desc);
2254ed36911cSMarc Carino 
2255284b3b77SHannes Reinecke 	if ((ap->flags & ATA_FLAG_FPDMA_AUX)) {
2256284b3b77SHannes Reinecke 		if (ata_id_has_ncq_send_and_recv(dev->id))
22575a233551SHannes Reinecke 			ata_dev_config_ncq_send_recv(dev);
2258284b3b77SHannes Reinecke 		if (ata_id_has_ncq_non_data(dev->id))
2259284b3b77SHannes Reinecke 			ata_dev_config_ncq_non_data(dev);
22608e061784SAdam Manzanares 		if (ata_id_has_ncq_prio(dev->id))
22618e061784SAdam Manzanares 			ata_dev_config_ncq_prio(dev);
2262ed36911cSMarc Carino 	}
2263ed36911cSMarc Carino 
2264388539f3SShaohua Li 	return 0;
2265c6fd2807SJeff Garzik }
2266c6fd2807SJeff Garzik 
2267e87fd28cSHannes Reinecke static void ata_dev_config_sense_reporting(struct ata_device *dev)
2268e87fd28cSHannes Reinecke {
2269e87fd28cSHannes Reinecke 	unsigned int err_mask;
2270e87fd28cSHannes Reinecke 
2271e87fd28cSHannes Reinecke 	if (!ata_id_has_sense_reporting(dev->id))
2272e87fd28cSHannes Reinecke 		return;
2273e87fd28cSHannes Reinecke 
2274e87fd28cSHannes Reinecke 	if (ata_id_sense_reporting_enabled(dev->id))
2275e87fd28cSHannes Reinecke 		return;
2276e87fd28cSHannes Reinecke 
2277e87fd28cSHannes Reinecke 	err_mask = ata_dev_set_feature(dev, SETFEATURE_SENSE_DATA, 0x1);
2278e87fd28cSHannes Reinecke 	if (err_mask) {
2279e87fd28cSHannes Reinecke 		ata_dev_dbg(dev,
2280e87fd28cSHannes Reinecke 			    "failed to enable Sense Data Reporting, Emask 0x%x\n",
2281e87fd28cSHannes Reinecke 			    err_mask);
2282e87fd28cSHannes Reinecke 	}
2283e87fd28cSHannes Reinecke }
2284e87fd28cSHannes Reinecke 
22856d1003aeSHannes Reinecke static void ata_dev_config_zac(struct ata_device *dev)
22866d1003aeSHannes Reinecke {
22876d1003aeSHannes Reinecke 	struct ata_port *ap = dev->link->ap;
22886d1003aeSHannes Reinecke 	unsigned int err_mask;
22896d1003aeSHannes Reinecke 	u8 *identify_buf = ap->sector_buf;
22906d1003aeSHannes Reinecke 
22916d1003aeSHannes Reinecke 	dev->zac_zones_optimal_open = U32_MAX;
22926d1003aeSHannes Reinecke 	dev->zac_zones_optimal_nonseq = U32_MAX;
22936d1003aeSHannes Reinecke 	dev->zac_zones_max_open = U32_MAX;
22946d1003aeSHannes Reinecke 
22956d1003aeSHannes Reinecke 	/*
22966d1003aeSHannes Reinecke 	 * Always set the 'ZAC' flag for Host-managed devices.
22976d1003aeSHannes Reinecke 	 */
22986d1003aeSHannes Reinecke 	if (dev->class == ATA_DEV_ZAC)
22996d1003aeSHannes Reinecke 		dev->flags |= ATA_DFLAG_ZAC;
23006d1003aeSHannes Reinecke 	else if (ata_id_zoned_cap(dev->id) == 0x01)
23016d1003aeSHannes Reinecke 		/*
23026d1003aeSHannes Reinecke 		 * Check for host-aware devices.
23036d1003aeSHannes Reinecke 		 */
23046d1003aeSHannes Reinecke 		dev->flags |= ATA_DFLAG_ZAC;
23056d1003aeSHannes Reinecke 
23066d1003aeSHannes Reinecke 	if (!(dev->flags & ATA_DFLAG_ZAC))
23076d1003aeSHannes Reinecke 		return;
23086d1003aeSHannes Reinecke 
2309a0fd2454SChristoph Hellwig 	if (!ata_identify_page_supported(dev, ATA_LOG_ZONED_INFORMATION)) {
23106d1003aeSHannes Reinecke 		ata_dev_warn(dev,
23116d1003aeSHannes Reinecke 			     "ATA Zoned Information Log not supported\n");
23126d1003aeSHannes Reinecke 		return;
23136d1003aeSHannes Reinecke 	}
23146d1003aeSHannes Reinecke 
23156d1003aeSHannes Reinecke 	/*
23166d1003aeSHannes Reinecke 	 * Read IDENTIFY DEVICE data log, page 9 (Zoned-device information)
23176d1003aeSHannes Reinecke 	 */
23181d51d5f3SChristoph Hellwig 	err_mask = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE,
23196d1003aeSHannes Reinecke 				     ATA_LOG_ZONED_INFORMATION,
23206d1003aeSHannes Reinecke 				     identify_buf, 1);
23216d1003aeSHannes Reinecke 	if (!err_mask) {
23226d1003aeSHannes Reinecke 		u64 zoned_cap, opt_open, opt_nonseq, max_open;
23236d1003aeSHannes Reinecke 
23246d1003aeSHannes Reinecke 		zoned_cap = get_unaligned_le64(&identify_buf[8]);
23256d1003aeSHannes Reinecke 		if ((zoned_cap >> 63))
23266d1003aeSHannes Reinecke 			dev->zac_zoned_cap = (zoned_cap & 1);
23276d1003aeSHannes Reinecke 		opt_open = get_unaligned_le64(&identify_buf[24]);
23286d1003aeSHannes Reinecke 		if ((opt_open >> 63))
23296d1003aeSHannes Reinecke 			dev->zac_zones_optimal_open = (u32)opt_open;
23306d1003aeSHannes Reinecke 		opt_nonseq = get_unaligned_le64(&identify_buf[32]);
23316d1003aeSHannes Reinecke 		if ((opt_nonseq >> 63))
23326d1003aeSHannes Reinecke 			dev->zac_zones_optimal_nonseq = (u32)opt_nonseq;
23336d1003aeSHannes Reinecke 		max_open = get_unaligned_le64(&identify_buf[40]);
23346d1003aeSHannes Reinecke 		if ((max_open >> 63))
23356d1003aeSHannes Reinecke 			dev->zac_zones_max_open = (u32)max_open;
23366d1003aeSHannes Reinecke 	}
23376d1003aeSHannes Reinecke }
23386d1003aeSHannes Reinecke 
2339818831c8SChristoph Hellwig static void ata_dev_config_trusted(struct ata_device *dev)
2340818831c8SChristoph Hellwig {
2341818831c8SChristoph Hellwig 	struct ata_port *ap = dev->link->ap;
2342818831c8SChristoph Hellwig 	u64 trusted_cap;
2343818831c8SChristoph Hellwig 	unsigned int err;
2344818831c8SChristoph Hellwig 
2345e8f11db9SChristoph Hellwig 	if (!ata_id_has_trusted(dev->id))
2346e8f11db9SChristoph Hellwig 		return;
2347e8f11db9SChristoph Hellwig 
2348818831c8SChristoph Hellwig 	if (!ata_identify_page_supported(dev, ATA_LOG_SECURITY)) {
2349818831c8SChristoph Hellwig 		ata_dev_warn(dev,
2350818831c8SChristoph Hellwig 			     "Security Log not supported\n");
2351818831c8SChristoph Hellwig 		return;
2352818831c8SChristoph Hellwig 	}
2353818831c8SChristoph Hellwig 
2354818831c8SChristoph Hellwig 	err = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE, ATA_LOG_SECURITY,
2355818831c8SChristoph Hellwig 			ap->sector_buf, 1);
2356fc5c8aa7SDamien Le Moal 	if (err)
2357818831c8SChristoph Hellwig 		return;
2358818831c8SChristoph Hellwig 
2359818831c8SChristoph Hellwig 	trusted_cap = get_unaligned_le64(&ap->sector_buf[40]);
2360818831c8SChristoph Hellwig 	if (!(trusted_cap & (1ULL << 63))) {
2361818831c8SChristoph Hellwig 		ata_dev_dbg(dev,
2362818831c8SChristoph Hellwig 			    "Trusted Computing capability qword not valid!\n");
2363818831c8SChristoph Hellwig 		return;
2364818831c8SChristoph Hellwig 	}
2365818831c8SChristoph Hellwig 
2366818831c8SChristoph Hellwig 	if (trusted_cap & (1 << 0))
2367818831c8SChristoph Hellwig 		dev->flags |= ATA_DFLAG_TRUSTED;
2368818831c8SChristoph Hellwig }
2369818831c8SChristoph Hellwig 
2370*62e4a60eSDamien Le Moal static void ata_dev_config_cdl(struct ata_device *dev)
2371*62e4a60eSDamien Le Moal {
2372*62e4a60eSDamien Le Moal 	struct ata_port *ap = dev->link->ap;
2373*62e4a60eSDamien Le Moal 	unsigned int err_mask;
2374*62e4a60eSDamien Le Moal 	u64 val;
2375*62e4a60eSDamien Le Moal 
2376*62e4a60eSDamien Le Moal 	if (ata_id_major_version(dev->id) < 12)
2377*62e4a60eSDamien Le Moal 		goto not_supported;
2378*62e4a60eSDamien Le Moal 
2379*62e4a60eSDamien Le Moal 	if (!ata_log_supported(dev, ATA_LOG_IDENTIFY_DEVICE) ||
2380*62e4a60eSDamien Le Moal 	    !ata_identify_page_supported(dev, ATA_LOG_SUPPORTED_CAPABILITIES))
2381*62e4a60eSDamien Le Moal 		goto not_supported;
2382*62e4a60eSDamien Le Moal 
2383*62e4a60eSDamien Le Moal 	err_mask = ata_read_log_page(dev, ATA_LOG_IDENTIFY_DEVICE,
2384*62e4a60eSDamien Le Moal 				     ATA_LOG_SUPPORTED_CAPABILITIES,
2385*62e4a60eSDamien Le Moal 				     ap->sector_buf, 1);
2386*62e4a60eSDamien Le Moal 	if (err_mask)
2387*62e4a60eSDamien Le Moal 		goto not_supported;
2388*62e4a60eSDamien Le Moal 
2389*62e4a60eSDamien Le Moal 	/* Check Command Duration Limit Supported bits */
2390*62e4a60eSDamien Le Moal 	val = get_unaligned_le64(&ap->sector_buf[168]);
2391*62e4a60eSDamien Le Moal 	if (!(val & BIT_ULL(63)) || !(val & BIT_ULL(0)))
2392*62e4a60eSDamien Le Moal 		goto not_supported;
2393*62e4a60eSDamien Le Moal 
2394*62e4a60eSDamien Le Moal 	/* Warn the user if command duration guideline is not supported */
2395*62e4a60eSDamien Le Moal 	if (!(val & BIT_ULL(1)))
2396*62e4a60eSDamien Le Moal 		ata_dev_warn(dev,
2397*62e4a60eSDamien Le Moal 			"Command duration guideline is not supported\n");
2398*62e4a60eSDamien Le Moal 
2399*62e4a60eSDamien Le Moal 	/*
2400*62e4a60eSDamien Le Moal 	 * Command duration limits is supported: cache the CDL log page 18h
2401*62e4a60eSDamien Le Moal 	 * (command duration descriptors).
2402*62e4a60eSDamien Le Moal 	 */
2403*62e4a60eSDamien Le Moal 	err_mask = ata_read_log_page(dev, ATA_LOG_CDL, 0, ap->sector_buf, 1);
2404*62e4a60eSDamien Le Moal 	if (err_mask) {
2405*62e4a60eSDamien Le Moal 		ata_dev_warn(dev, "Read Command Duration Limits log failed\n");
2406*62e4a60eSDamien Le Moal 		goto not_supported;
2407*62e4a60eSDamien Le Moal 	}
2408*62e4a60eSDamien Le Moal 
2409*62e4a60eSDamien Le Moal 	memcpy(dev->cdl, ap->sector_buf, ATA_LOG_CDL_SIZE);
2410*62e4a60eSDamien Le Moal 	dev->flags |= ATA_DFLAG_CDL;
2411*62e4a60eSDamien Le Moal 
2412*62e4a60eSDamien Le Moal 	return;
2413*62e4a60eSDamien Le Moal 
2414*62e4a60eSDamien Le Moal not_supported:
2415*62e4a60eSDamien Le Moal 	dev->flags &= ~ATA_DFLAG_CDL;
2416*62e4a60eSDamien Le Moal }
2417*62e4a60eSDamien Le Moal 
2418891fd7c6SDamien Le Moal static int ata_dev_config_lba(struct ata_device *dev)
2419891fd7c6SDamien Le Moal {
2420891fd7c6SDamien Le Moal 	const u16 *id = dev->id;
2421891fd7c6SDamien Le Moal 	const char *lba_desc;
2422891fd7c6SDamien Le Moal 	char ncq_desc[24];
2423891fd7c6SDamien Le Moal 	int ret;
2424891fd7c6SDamien Le Moal 
2425891fd7c6SDamien Le Moal 	dev->flags |= ATA_DFLAG_LBA;
2426891fd7c6SDamien Le Moal 
2427891fd7c6SDamien Le Moal 	if (ata_id_has_lba48(id)) {
2428891fd7c6SDamien Le Moal 		lba_desc = "LBA48";
2429891fd7c6SDamien Le Moal 		dev->flags |= ATA_DFLAG_LBA48;
2430891fd7c6SDamien Le Moal 		if (dev->n_sectors >= (1UL << 28) &&
2431891fd7c6SDamien Le Moal 		    ata_id_has_flush_ext(id))
2432891fd7c6SDamien Le Moal 			dev->flags |= ATA_DFLAG_FLUSH_EXT;
2433891fd7c6SDamien Le Moal 	} else {
2434891fd7c6SDamien Le Moal 		lba_desc = "LBA";
2435891fd7c6SDamien Le Moal 	}
2436891fd7c6SDamien Le Moal 
2437891fd7c6SDamien Le Moal 	/* config NCQ */
2438891fd7c6SDamien Le Moal 	ret = ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
2439891fd7c6SDamien Le Moal 
2440891fd7c6SDamien Le Moal 	/* print device info to dmesg */
24411c95a27cSHannes Reinecke 	if (ata_dev_print_info(dev))
2442891fd7c6SDamien Le Moal 		ata_dev_info(dev,
2443891fd7c6SDamien Le Moal 			     "%llu sectors, multi %u: %s %s\n",
2444891fd7c6SDamien Le Moal 			     (unsigned long long)dev->n_sectors,
2445891fd7c6SDamien Le Moal 			     dev->multi_count, lba_desc, ncq_desc);
2446891fd7c6SDamien Le Moal 
2447891fd7c6SDamien Le Moal 	return ret;
2448891fd7c6SDamien Le Moal }
2449891fd7c6SDamien Le Moal 
2450891fd7c6SDamien Le Moal static void ata_dev_config_chs(struct ata_device *dev)
2451891fd7c6SDamien Le Moal {
2452891fd7c6SDamien Le Moal 	const u16 *id = dev->id;
2453891fd7c6SDamien Le Moal 
2454891fd7c6SDamien Le Moal 	if (ata_id_current_chs_valid(id)) {
2455891fd7c6SDamien Le Moal 		/* Current CHS translation is valid. */
2456891fd7c6SDamien Le Moal 		dev->cylinders = id[54];
2457891fd7c6SDamien Le Moal 		dev->heads     = id[55];
2458891fd7c6SDamien Le Moal 		dev->sectors   = id[56];
2459891fd7c6SDamien Le Moal 	} else {
2460891fd7c6SDamien Le Moal 		/* Default translation */
2461891fd7c6SDamien Le Moal 		dev->cylinders	= id[1];
2462891fd7c6SDamien Le Moal 		dev->heads	= id[3];
2463891fd7c6SDamien Le Moal 		dev->sectors	= id[6];
2464891fd7c6SDamien Le Moal 	}
2465891fd7c6SDamien Le Moal 
2466891fd7c6SDamien Le Moal 	/* print device info to dmesg */
24671c95a27cSHannes Reinecke 	if (ata_dev_print_info(dev))
2468891fd7c6SDamien Le Moal 		ata_dev_info(dev,
2469891fd7c6SDamien Le Moal 			     "%llu sectors, multi %u, CHS %u/%u/%u\n",
2470891fd7c6SDamien Le Moal 			     (unsigned long long)dev->n_sectors,
2471891fd7c6SDamien Le Moal 			     dev->multi_count, dev->cylinders,
2472891fd7c6SDamien Le Moal 			     dev->heads, dev->sectors);
2473891fd7c6SDamien Le Moal }
2474891fd7c6SDamien Le Moal 
24754d2e4980SDamien Le Moal static void ata_dev_config_fua(struct ata_device *dev)
24764d2e4980SDamien Le Moal {
24774d2e4980SDamien Le Moal 	/* Ignore FUA support if its use is disabled globally */
24784d2e4980SDamien Le Moal 	if (!libata_fua)
24794d2e4980SDamien Le Moal 		goto nofua;
24804d2e4980SDamien Le Moal 
24814d2e4980SDamien Le Moal 	/* Ignore devices without support for WRITE DMA FUA EXT */
24824d2e4980SDamien Le Moal 	if (!(dev->flags & ATA_DFLAG_LBA48) || !ata_id_has_fua(dev->id))
24834d2e4980SDamien Le Moal 		goto nofua;
24844d2e4980SDamien Le Moal 
24854d2e4980SDamien Le Moal 	/* Ignore known bad devices and devices that lack NCQ support */
24864d2e4980SDamien Le Moal 	if (!ata_ncq_supported(dev) || (dev->horkage & ATA_HORKAGE_NO_FUA))
24874d2e4980SDamien Le Moal 		goto nofua;
24884d2e4980SDamien Le Moal 
24894d2e4980SDamien Le Moal 	dev->flags |= ATA_DFLAG_FUA;
24904d2e4980SDamien Le Moal 
24914d2e4980SDamien Le Moal 	return;
24924d2e4980SDamien Le Moal 
24934d2e4980SDamien Le Moal nofua:
24944d2e4980SDamien Le Moal 	dev->flags &= ~ATA_DFLAG_FUA;
24954d2e4980SDamien Le Moal }
24964d2e4980SDamien Le Moal 
2497d8d8778cSDamien Le Moal static void ata_dev_config_devslp(struct ata_device *dev)
2498d8d8778cSDamien Le Moal {
2499d8d8778cSDamien Le Moal 	u8 *sata_setting = dev->link->ap->sector_buf;
2500d8d8778cSDamien Le Moal 	unsigned int err_mask;
2501d8d8778cSDamien Le Moal 	int i, j;
2502d8d8778cSDamien Le Moal 
2503d8d8778cSDamien Le Moal 	/*
2504d8d8778cSDamien Le Moal 	 * Check device sleep capability. Get DevSlp timing variables
2505d8d8778cSDamien Le Moal 	 * from SATA Settings page of Identify Device Data Log.
2506d8d8778cSDamien Le Moal 	 */
250706f6c4c6SDamien Le Moal 	if (!ata_id_has_devslp(dev->id) ||
250806f6c4c6SDamien Le Moal 	    !ata_identify_page_supported(dev, ATA_LOG_SATA_SETTINGS))
2509d8d8778cSDamien Le Moal 		return;
2510d8d8778cSDamien Le Moal 
2511d8d8778cSDamien Le Moal 	err_mask = ata_read_log_page(dev,
2512d8d8778cSDamien Le Moal 				     ATA_LOG_IDENTIFY_DEVICE,
2513d8d8778cSDamien Le Moal 				     ATA_LOG_SATA_SETTINGS,
2514d8d8778cSDamien Le Moal 				     sata_setting, 1);
2515fc5c8aa7SDamien Le Moal 	if (err_mask)
2516d8d8778cSDamien Le Moal 		return;
2517d8d8778cSDamien Le Moal 
2518d8d8778cSDamien Le Moal 	dev->flags |= ATA_DFLAG_DEVSLP;
2519d8d8778cSDamien Le Moal 	for (i = 0; i < ATA_LOG_DEVSLP_SIZE; i++) {
2520d8d8778cSDamien Le Moal 		j = ATA_LOG_DEVSLP_OFFSET + i;
2521d8d8778cSDamien Le Moal 		dev->devslp_timing[i] = sata_setting[j];
2522d8d8778cSDamien Le Moal 	}
2523d8d8778cSDamien Le Moal }
2524d8d8778cSDamien Le Moal 
2525fe22e1c2SDamien Le Moal static void ata_dev_config_cpr(struct ata_device *dev)
2526fe22e1c2SDamien Le Moal {
2527fe22e1c2SDamien Le Moal 	unsigned int err_mask;
2528fe22e1c2SDamien Le Moal 	size_t buf_len;
2529fe22e1c2SDamien Le Moal 	int i, nr_cpr = 0;
2530fe22e1c2SDamien Le Moal 	struct ata_cpr_log *cpr_log = NULL;
2531fe22e1c2SDamien Le Moal 	u8 *desc, *buf = NULL;
2532fe22e1c2SDamien Le Moal 
2533c745dfc5STyler Erickson 	if (ata_id_major_version(dev->id) < 11)
2534c745dfc5STyler Erickson 		goto out;
2535c745dfc5STyler Erickson 
2536c745dfc5STyler Erickson 	buf_len = ata_log_supported(dev, ATA_LOG_CONCURRENT_POSITIONING_RANGES);
2537c745dfc5STyler Erickson 	if (buf_len == 0)
2538fe22e1c2SDamien Le Moal 		goto out;
2539fe22e1c2SDamien Le Moal 
2540fe22e1c2SDamien Le Moal 	/*
2541fda17afcSDamien Le Moal 	 * Read the concurrent positioning ranges log (0x47). We can have at
2542c745dfc5STyler Erickson 	 * most 255 32B range descriptors plus a 64B header. This log varies in
2543c745dfc5STyler Erickson 	 * size, so use the size reported in the GPL directory. Reading beyond
2544c745dfc5STyler Erickson 	 * the supported length will result in an error.
2545fe22e1c2SDamien Le Moal 	 */
2546c745dfc5STyler Erickson 	buf_len <<= 9;
2547fe22e1c2SDamien Le Moal 	buf = kzalloc(buf_len, GFP_KERNEL);
2548fe22e1c2SDamien Le Moal 	if (!buf)
2549fe22e1c2SDamien Le Moal 		goto out;
2550fe22e1c2SDamien Le Moal 
2551fda17afcSDamien Le Moal 	err_mask = ata_read_log_page(dev, ATA_LOG_CONCURRENT_POSITIONING_RANGES,
2552fda17afcSDamien Le Moal 				     0, buf, buf_len >> 9);
2553fe22e1c2SDamien Le Moal 	if (err_mask)
2554fe22e1c2SDamien Le Moal 		goto out;
2555fe22e1c2SDamien Le Moal 
2556fe22e1c2SDamien Le Moal 	nr_cpr = buf[0];
2557fe22e1c2SDamien Le Moal 	if (!nr_cpr)
2558fe22e1c2SDamien Le Moal 		goto out;
2559fe22e1c2SDamien Le Moal 
2560fe22e1c2SDamien Le Moal 	cpr_log = kzalloc(struct_size(cpr_log, cpr, nr_cpr), GFP_KERNEL);
2561fe22e1c2SDamien Le Moal 	if (!cpr_log)
2562fe22e1c2SDamien Le Moal 		goto out;
2563fe22e1c2SDamien Le Moal 
2564fe22e1c2SDamien Le Moal 	cpr_log->nr_cpr = nr_cpr;
2565fe22e1c2SDamien Le Moal 	desc = &buf[64];
2566fe22e1c2SDamien Le Moal 	for (i = 0; i < nr_cpr; i++, desc += 32) {
2567fe22e1c2SDamien Le Moal 		cpr_log->cpr[i].num = desc[0];
2568fe22e1c2SDamien Le Moal 		cpr_log->cpr[i].num_storage_elements = desc[1];
2569fe22e1c2SDamien Le Moal 		cpr_log->cpr[i].start_lba = get_unaligned_le64(&desc[8]);
2570fe22e1c2SDamien Le Moal 		cpr_log->cpr[i].num_lbas = get_unaligned_le64(&desc[16]);
2571fe22e1c2SDamien Le Moal 	}
2572fe22e1c2SDamien Le Moal 
2573fe22e1c2SDamien Le Moal out:
2574fe22e1c2SDamien Le Moal 	swap(dev->cpr_log, cpr_log);
2575fe22e1c2SDamien Le Moal 	kfree(cpr_log);
2576fe22e1c2SDamien Le Moal 	kfree(buf);
2577fe22e1c2SDamien Le Moal }
2578fe22e1c2SDamien Le Moal 
2579d633b8a7SDamien Le Moal static void ata_dev_print_features(struct ata_device *dev)
2580d633b8a7SDamien Le Moal {
2581d633b8a7SDamien Le Moal 	if (!(dev->flags & ATA_DFLAG_FEATURES_MASK))
2582d633b8a7SDamien Le Moal 		return;
2583d633b8a7SDamien Le Moal 
2584d633b8a7SDamien Le Moal 	ata_dev_info(dev,
2585*62e4a60eSDamien Le Moal 		     "Features:%s%s%s%s%s%s%s%s\n",
25864d2e4980SDamien Le Moal 		     dev->flags & ATA_DFLAG_FUA ? " FUA" : "",
2587d633b8a7SDamien Le Moal 		     dev->flags & ATA_DFLAG_TRUSTED ? " Trust" : "",
2588d633b8a7SDamien Le Moal 		     dev->flags & ATA_DFLAG_DA ? " Dev-Attention" : "",
2589d633b8a7SDamien Le Moal 		     dev->flags & ATA_DFLAG_DEVSLP ? " Dev-Sleep" : "",
2590d633b8a7SDamien Le Moal 		     dev->flags & ATA_DFLAG_NCQ_SEND_RECV ? " NCQ-sndrcv" : "",
2591fe22e1c2SDamien Le Moal 		     dev->flags & ATA_DFLAG_NCQ_PRIO ? " NCQ-prio" : "",
2592*62e4a60eSDamien Le Moal 		     dev->flags & ATA_DFLAG_CDL ? " CDL" : "",
2593fe22e1c2SDamien Le Moal 		     dev->cpr_log ? " CPR" : "");
2594d633b8a7SDamien Le Moal }
2595d633b8a7SDamien Le Moal 
2596c6fd2807SJeff Garzik /**
2597c6fd2807SJeff Garzik  *	ata_dev_configure - Configure the specified ATA/ATAPI device
2598c6fd2807SJeff Garzik  *	@dev: Target device to configure
2599c6fd2807SJeff Garzik  *
2600c6fd2807SJeff Garzik  *	Configure @dev according to @dev->id.  Generic and low-level
2601c6fd2807SJeff Garzik  *	driver specific fixups are also applied.
2602c6fd2807SJeff Garzik  *
2603c6fd2807SJeff Garzik  *	LOCKING:
2604c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
2605c6fd2807SJeff Garzik  *
2606c6fd2807SJeff Garzik  *	RETURNS:
2607c6fd2807SJeff Garzik  *	0 on success, -errno otherwise
2608c6fd2807SJeff Garzik  */
2609efdaedc4STejun Heo int ata_dev_configure(struct ata_device *dev)
2610c6fd2807SJeff Garzik {
26119af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
2612891fd7c6SDamien Le Moal 	bool print_info = ata_dev_print_info(dev);
2613c6fd2807SJeff Garzik 	const u16 *id = dev->id;
2614f0a6d77bSSergey Shtylyov 	unsigned int xfer_mask;
261565fe1f0fSShane Huang 	unsigned int err_mask;
2616b352e57dSAlan Cox 	char revbuf[7];		/* XYZ-99\0 */
26173f64f565SEric D. Mudama 	char fwrevbuf[ATA_ID_FW_REV_LEN+1];
26183f64f565SEric D. Mudama 	char modelbuf[ATA_ID_PROD_LEN+1];
2619c6fd2807SJeff Garzik 	int rc;
2620c6fd2807SJeff Garzik 
262196c810f2SHannes Reinecke 	if (!ata_dev_enabled(dev)) {
262296c810f2SHannes Reinecke 		ata_dev_dbg(dev, "no device\n");
2623c6fd2807SJeff Garzik 		return 0;
2624c6fd2807SJeff Garzik 	}
2625c6fd2807SJeff Garzik 
262675683fe7STejun Heo 	/* set horkage */
262775683fe7STejun Heo 	dev->horkage |= ata_dev_blacklisted(dev);
262833267325STejun Heo 	ata_force_horkage(dev);
262975683fe7STejun Heo 
263050af2fa1STejun Heo 	if (dev->horkage & ATA_HORKAGE_DISABLE) {
2631a9a79dfeSJoe Perches 		ata_dev_info(dev, "unsupported device, disabling\n");
263250af2fa1STejun Heo 		ata_dev_disable(dev);
263350af2fa1STejun Heo 		return 0;
263450af2fa1STejun Heo 	}
263550af2fa1STejun Heo 
26362486fa56STejun Heo 	if ((!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) &&
26372486fa56STejun Heo 	    dev->class == ATA_DEV_ATAPI) {
2638a9a79dfeSJoe Perches 		ata_dev_warn(dev, "WARNING: ATAPI is %s, device ignored\n",
26392486fa56STejun Heo 			     atapi_enabled ? "not supported with this driver"
26402486fa56STejun Heo 			     : "disabled");
26412486fa56STejun Heo 		ata_dev_disable(dev);
26422486fa56STejun Heo 		return 0;
26432486fa56STejun Heo 	}
26442486fa56STejun Heo 
26459062712fSTejun Heo 	rc = ata_do_link_spd_horkage(dev);
26469062712fSTejun Heo 	if (rc)
26479062712fSTejun Heo 		return rc;
26489062712fSTejun Heo 
2649ecd75ad5STejun Heo 	/* some WD SATA-1 drives have issues with LPM, turn on NOLPM for them */
2650ecd75ad5STejun Heo 	if ((dev->horkage & ATA_HORKAGE_WD_BROKEN_LPM) &&
2651ecd75ad5STejun Heo 	    (id[ATA_ID_SATA_CAPABILITY] & 0xe) == 0x2)
2652ecd75ad5STejun Heo 		dev->horkage |= ATA_HORKAGE_NOLPM;
2653ecd75ad5STejun Heo 
2654240630e6SHans de Goede 	if (ap->flags & ATA_FLAG_NO_LPM)
2655240630e6SHans de Goede 		dev->horkage |= ATA_HORKAGE_NOLPM;
2656240630e6SHans de Goede 
2657ecd75ad5STejun Heo 	if (dev->horkage & ATA_HORKAGE_NOLPM) {
2658ecd75ad5STejun Heo 		ata_dev_warn(dev, "LPM support broken, forcing max_power\n");
2659ecd75ad5STejun Heo 		dev->link->ap->target_lpm_policy = ATA_LPM_MAX_POWER;
2660ecd75ad5STejun Heo 	}
2661ecd75ad5STejun Heo 
26626746544cSTejun Heo 	/* let ACPI work its magic */
26636746544cSTejun Heo 	rc = ata_acpi_on_devcfg(dev);
26646746544cSTejun Heo 	if (rc)
26656746544cSTejun Heo 		return rc;
266608573a86SKristen Carlson Accardi 
266705027adcSTejun Heo 	/* massage HPA, do it early as it might change IDENTIFY data */
266805027adcSTejun Heo 	rc = ata_hpa_resize(dev);
266905027adcSTejun Heo 	if (rc)
267005027adcSTejun Heo 		return rc;
267105027adcSTejun Heo 
2672c6fd2807SJeff Garzik 	/* print device capabilities */
2673a9a79dfeSJoe Perches 	ata_dev_dbg(dev,
2674c6fd2807SJeff Garzik 		    "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
2675c6fd2807SJeff Garzik 		    "85:%04x 86:%04x 87:%04x 88:%04x\n",
26767f5e4e8dSHarvey Harrison 		    __func__,
2677c6fd2807SJeff Garzik 		    id[49], id[82], id[83], id[84],
2678c6fd2807SJeff Garzik 		    id[85], id[86], id[87], id[88]);
2679c6fd2807SJeff Garzik 
2680c6fd2807SJeff Garzik 	/* initialize to-be-configured parameters */
2681c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_CFG_MASK;
2682c6fd2807SJeff Garzik 	dev->max_sectors = 0;
2683c6fd2807SJeff Garzik 	dev->cdb_len = 0;
2684c6fd2807SJeff Garzik 	dev->n_sectors = 0;
2685c6fd2807SJeff Garzik 	dev->cylinders = 0;
2686c6fd2807SJeff Garzik 	dev->heads = 0;
2687c6fd2807SJeff Garzik 	dev->sectors = 0;
2688e18086d6SMark Lord 	dev->multi_count = 0;
2689c6fd2807SJeff Garzik 
2690c6fd2807SJeff Garzik 	/*
2691c6fd2807SJeff Garzik 	 * common ATA, ATAPI feature tests
2692c6fd2807SJeff Garzik 	 */
2693c6fd2807SJeff Garzik 
2694c6fd2807SJeff Garzik 	/* find max transfer mode; for printk only */
2695c6fd2807SJeff Garzik 	xfer_mask = ata_id_xfermask(id);
2696c6fd2807SJeff Garzik 
26976044f3c4SHannes Reinecke 	ata_dump_id(dev, id);
2698c6fd2807SJeff Garzik 
2699ef143d57SAlbert Lee 	/* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
2700ef143d57SAlbert Lee 	ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
2701ef143d57SAlbert Lee 			sizeof(fwrevbuf));
2702ef143d57SAlbert Lee 
2703ef143d57SAlbert Lee 	ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
2704ef143d57SAlbert Lee 			sizeof(modelbuf));
2705ef143d57SAlbert Lee 
2706c6fd2807SJeff Garzik 	/* ATA-specific feature tests */
27079162c657SHannes Reinecke 	if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) {
2708b352e57dSAlan Cox 		if (ata_id_is_cfa(id)) {
270962afe5d7SSergei Shtylyov 			/* CPRM may make this media unusable */
271062afe5d7SSergei Shtylyov 			if (id[ATA_ID_CFA_KEY_MGMT] & 1)
2711a9a79dfeSJoe Perches 				ata_dev_warn(dev,
2712a9a79dfeSJoe Perches 	"supports DRM functions and may not be fully accessible\n");
2713b352e57dSAlan Cox 			snprintf(revbuf, 7, "CFA");
2714ae8d4ee7SAlan Cox 		} else {
2715b352e57dSAlan Cox 			snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
2716ae8d4ee7SAlan Cox 			/* Warn the user if the device has TPM extensions */
2717ae8d4ee7SAlan Cox 			if (ata_id_has_tpm(id))
2718a9a79dfeSJoe Perches 				ata_dev_warn(dev,
2719a9a79dfeSJoe Perches 	"supports DRM functions and may not be fully accessible\n");
2720ae8d4ee7SAlan Cox 		}
2721b352e57dSAlan Cox 
2722c6fd2807SJeff Garzik 		dev->n_sectors = ata_id_n_sectors(id);
2723c6fd2807SJeff Garzik 
2724e18086d6SMark Lord 		/* get current R/W Multiple count setting */
2725e18086d6SMark Lord 		if ((dev->id[47] >> 8) == 0x80 && (dev->id[59] & 0x100)) {
2726e18086d6SMark Lord 			unsigned int max = dev->id[47] & 0xff;
2727e18086d6SMark Lord 			unsigned int cnt = dev->id[59] & 0xff;
2728e18086d6SMark Lord 			/* only recognize/allow powers of two here */
2729e18086d6SMark Lord 			if (is_power_of_2(max) && is_power_of_2(cnt))
2730e18086d6SMark Lord 				if (cnt <= max)
2731e18086d6SMark Lord 					dev->multi_count = cnt;
2732e18086d6SMark Lord 		}
27333f64f565SEric D. Mudama 
2734891fd7c6SDamien Le Moal 		/* print device info to dmesg */
27351c95a27cSHannes Reinecke 		if (print_info)
2736891fd7c6SDamien Le Moal 			ata_dev_info(dev, "%s: %s, %s, max %s\n",
2737891fd7c6SDamien Le Moal 				     revbuf, modelbuf, fwrevbuf,
2738891fd7c6SDamien Le Moal 				     ata_mode_string(xfer_mask));
2739891fd7c6SDamien Le Moal 
2740c6fd2807SJeff Garzik 		if (ata_id_has_lba(id)) {
2741891fd7c6SDamien Le Moal 			rc = ata_dev_config_lba(dev);
2742388539f3SShaohua Li 			if (rc)
2743388539f3SShaohua Li 				return rc;
2744c6fd2807SJeff Garzik 		} else {
2745891fd7c6SDamien Le Moal 			ata_dev_config_chs(dev);
2746c6fd2807SJeff Garzik 		}
2747c6fd2807SJeff Garzik 
27484d2e4980SDamien Le Moal 		ata_dev_config_fua(dev);
2749d8d8778cSDamien Le Moal 		ata_dev_config_devslp(dev);
2750e87fd28cSHannes Reinecke 		ata_dev_config_sense_reporting(dev);
27516d1003aeSHannes Reinecke 		ata_dev_config_zac(dev);
2752818831c8SChristoph Hellwig 		ata_dev_config_trusted(dev);
2753fe22e1c2SDamien Le Moal 		ata_dev_config_cpr(dev);
2754*62e4a60eSDamien Le Moal 		ata_dev_config_cdl(dev);
2755b1ffbf85SMinwoo Im 		dev->cdb_len = 32;
2756d633b8a7SDamien Le Moal 
27571c95a27cSHannes Reinecke 		if (print_info)
2758d633b8a7SDamien Le Moal 			ata_dev_print_features(dev);
2759c6fd2807SJeff Garzik 	}
2760c6fd2807SJeff Garzik 
2761c6fd2807SJeff Garzik 	/* ATAPI-specific feature tests */
2762c6fd2807SJeff Garzik 	else if (dev->class == ATA_DEV_ATAPI) {
2763854c73a2STejun Heo 		const char *cdb_intr_string = "";
2764854c73a2STejun Heo 		const char *atapi_an_string = "";
276591163006STejun Heo 		const char *dma_dir_string = "";
27667d77b247STejun Heo 		u32 sntf;
2767c6fd2807SJeff Garzik 
2768c6fd2807SJeff Garzik 		rc = atapi_cdb_len(id);
2769c6fd2807SJeff Garzik 		if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
277016d42467SHannes Reinecke 			ata_dev_warn(dev, "unsupported CDB len %d\n", rc);
2771c6fd2807SJeff Garzik 			rc = -EINVAL;
2772c6fd2807SJeff Garzik 			goto err_out_nosup;
2773c6fd2807SJeff Garzik 		}
2774c6fd2807SJeff Garzik 		dev->cdb_len = (unsigned int) rc;
2775c6fd2807SJeff Garzik 
27767d77b247STejun Heo 		/* Enable ATAPI AN if both the host and device have
27777d77b247STejun Heo 		 * the support.  If PMP is attached, SNTF is required
27787d77b247STejun Heo 		 * to enable ATAPI AN to discern between PHY status
27797d77b247STejun Heo 		 * changed notifications and ATAPI ANs.
27809f45cbd3SKristen Carlson Accardi 		 */
2781e7ecd435STejun Heo 		if (atapi_an &&
2782e7ecd435STejun Heo 		    (ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
2783071f44b1STejun Heo 		    (!sata_pmp_attached(ap) ||
27847d77b247STejun Heo 		     sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
27859f45cbd3SKristen Carlson Accardi 			/* issue SET feature command to turn this on */
2786218f3d30SJeff Garzik 			err_mask = ata_dev_set_feature(dev,
2787218f3d30SJeff Garzik 					SETFEATURES_SATA_ENABLE, SATA_AN);
2788854c73a2STejun Heo 			if (err_mask)
2789a9a79dfeSJoe Perches 				ata_dev_err(dev,
2790a9a79dfeSJoe Perches 					    "failed to enable ATAPI AN (err_mask=0x%x)\n",
2791a9a79dfeSJoe Perches 					    err_mask);
2792854c73a2STejun Heo 			else {
27939f45cbd3SKristen Carlson Accardi 				dev->flags |= ATA_DFLAG_AN;
2794854c73a2STejun Heo 				atapi_an_string = ", ATAPI AN";
2795854c73a2STejun Heo 			}
27969f45cbd3SKristen Carlson Accardi 		}
27979f45cbd3SKristen Carlson Accardi 
2798c6fd2807SJeff Garzik 		if (ata_id_cdb_intr(dev->id)) {
2799c6fd2807SJeff Garzik 			dev->flags |= ATA_DFLAG_CDB_INTR;
2800c6fd2807SJeff Garzik 			cdb_intr_string = ", CDB intr";
2801c6fd2807SJeff Garzik 		}
2802c6fd2807SJeff Garzik 
2803966fbe19SVincent Pelletier 		if (atapi_dmadir || (dev->horkage & ATA_HORKAGE_ATAPI_DMADIR) || atapi_id_dmadir(dev->id)) {
280491163006STejun Heo 			dev->flags |= ATA_DFLAG_DMADIR;
280591163006STejun Heo 			dma_dir_string = ", DMADIR";
280691163006STejun Heo 		}
280791163006STejun Heo 
2808afe75951SAaron Lu 		if (ata_id_has_da(dev->id)) {
2809b1354cbbSLin Ming 			dev->flags |= ATA_DFLAG_DA;
2810afe75951SAaron Lu 			zpodd_init(dev);
2811afe75951SAaron Lu 		}
2812b1354cbbSLin Ming 
2813c6fd2807SJeff Garzik 		/* print device info to dmesg */
28141c95a27cSHannes Reinecke 		if (print_info)
2815a9a79dfeSJoe Perches 			ata_dev_info(dev,
281691163006STejun Heo 				     "ATAPI: %s, %s, max %s%s%s%s\n",
2817ef143d57SAlbert Lee 				     modelbuf, fwrevbuf,
2818c6fd2807SJeff Garzik 				     ata_mode_string(xfer_mask),
281991163006STejun Heo 				     cdb_intr_string, atapi_an_string,
282091163006STejun Heo 				     dma_dir_string);
2821c6fd2807SJeff Garzik 	}
2822c6fd2807SJeff Garzik 
2823914ed354STejun Heo 	/* determine max_sectors */
2824914ed354STejun Heo 	dev->max_sectors = ATA_MAX_SECTORS;
2825914ed354STejun Heo 	if (dev->flags & ATA_DFLAG_LBA48)
2826914ed354STejun Heo 		dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2827914ed354STejun Heo 
2828c5038fc0SAlan Cox 	/* Limit PATA drive on SATA cable bridge transfers to udma5,
2829c5038fc0SAlan Cox 	   200 sectors */
2830c6fd2807SJeff Garzik 	if (ata_dev_knobble(dev)) {
28311c95a27cSHannes Reinecke 		if (print_info)
2832a9a79dfeSJoe Perches 			ata_dev_info(dev, "applying bridge limits\n");
2833c6fd2807SJeff Garzik 		dev->udma_mask &= ATA_UDMA5;
2834c6fd2807SJeff Garzik 		dev->max_sectors = ATA_MAX_SECTORS;
2835c6fd2807SJeff Garzik 	}
2836c6fd2807SJeff Garzik 
2837f8d8e579STony Battersby 	if ((dev->class == ATA_DEV_ATAPI) &&
2838f442cd86SAlbert Lee 	    (atapi_command_packet_set(id) == TYPE_TAPE)) {
2839f8d8e579STony Battersby 		dev->max_sectors = ATA_MAX_SECTORS_TAPE;
2840f442cd86SAlbert Lee 		dev->horkage |= ATA_HORKAGE_STUCK_ERR;
2841f442cd86SAlbert Lee 	}
2842f8d8e579STony Battersby 
284375683fe7STejun Heo 	if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
284403ec52deSTejun Heo 		dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
284503ec52deSTejun Heo 					 dev->max_sectors);
284618d6e9d5SAlbert Lee 
2847af34d637SDavid Milburn 	if (dev->horkage & ATA_HORKAGE_MAX_SEC_1024)
2848af34d637SDavid Milburn 		dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_1024,
2849af34d637SDavid Milburn 					 dev->max_sectors);
2850af34d637SDavid Milburn 
2851a32450e1SShan Hai 	if (dev->horkage & ATA_HORKAGE_MAX_SEC_LBA48)
2852a32450e1SShan Hai 		dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2853a32450e1SShan Hai 
2854c6fd2807SJeff Garzik 	if (ap->ops->dev_config)
2855cd0d3bbcSAlan 		ap->ops->dev_config(dev);
2856c6fd2807SJeff Garzik 
2857c5038fc0SAlan Cox 	if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2858c5038fc0SAlan Cox 		/* Let the user know. We don't want to disallow opens for
2859c5038fc0SAlan Cox 		   rescue purposes, or in case the vendor is just a blithering
2860c5038fc0SAlan Cox 		   idiot. Do this after the dev_config call as some controllers
2861c5038fc0SAlan Cox 		   with buggy firmware may want to avoid reporting false device
2862c5038fc0SAlan Cox 		   bugs */
2863c5038fc0SAlan Cox 
2864c5038fc0SAlan Cox 		if (print_info) {
2865a9a79dfeSJoe Perches 			ata_dev_warn(dev,
2866c5038fc0SAlan Cox "Drive reports diagnostics failure. This may indicate a drive\n");
2867a9a79dfeSJoe Perches 			ata_dev_warn(dev,
2868c5038fc0SAlan Cox "fault or invalid emulation. Contact drive vendor for information.\n");
2869c5038fc0SAlan Cox 		}
2870c5038fc0SAlan Cox 	}
2871c5038fc0SAlan Cox 
2872ac70a964STejun Heo 	if ((dev->horkage & ATA_HORKAGE_FIRMWARE_WARN) && print_info) {
2873a9a79dfeSJoe Perches 		ata_dev_warn(dev, "WARNING: device requires firmware update to be fully functional\n");
2874a9a79dfeSJoe Perches 		ata_dev_warn(dev, "         contact the vendor or visit http://ata.wiki.kernel.org\n");
2875ac70a964STejun Heo 	}
2876ac70a964STejun Heo 
2877c6fd2807SJeff Garzik 	return 0;
2878c6fd2807SJeff Garzik 
2879c6fd2807SJeff Garzik err_out_nosup:
2880c6fd2807SJeff Garzik 	return rc;
2881c6fd2807SJeff Garzik }
2882c6fd2807SJeff Garzik 
2883c6fd2807SJeff Garzik /**
28842e41e8e6SAlan Cox  *	ata_cable_40wire	-	return 40 wire cable type
2885be0d18dfSAlan Cox  *	@ap: port
2886be0d18dfSAlan Cox  *
28872e41e8e6SAlan Cox  *	Helper method for drivers which want to hardwire 40 wire cable
2888be0d18dfSAlan Cox  *	detection.
2889be0d18dfSAlan Cox  */
2890be0d18dfSAlan Cox 
2891be0d18dfSAlan Cox int ata_cable_40wire(struct ata_port *ap)
2892be0d18dfSAlan Cox {
2893be0d18dfSAlan Cox 	return ATA_CBL_PATA40;
2894be0d18dfSAlan Cox }
2895a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_cable_40wire);
2896be0d18dfSAlan Cox 
2897be0d18dfSAlan Cox /**
28982e41e8e6SAlan Cox  *	ata_cable_80wire	-	return 80 wire cable type
2899be0d18dfSAlan Cox  *	@ap: port
2900be0d18dfSAlan Cox  *
29012e41e8e6SAlan Cox  *	Helper method for drivers which want to hardwire 80 wire cable
2902be0d18dfSAlan Cox  *	detection.
2903be0d18dfSAlan Cox  */
2904be0d18dfSAlan Cox 
2905be0d18dfSAlan Cox int ata_cable_80wire(struct ata_port *ap)
2906be0d18dfSAlan Cox {
2907be0d18dfSAlan Cox 	return ATA_CBL_PATA80;
2908be0d18dfSAlan Cox }
2909a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_cable_80wire);
2910be0d18dfSAlan Cox 
2911be0d18dfSAlan Cox /**
2912be0d18dfSAlan Cox  *	ata_cable_unknown	-	return unknown PATA cable.
2913be0d18dfSAlan Cox  *	@ap: port
2914be0d18dfSAlan Cox  *
2915be0d18dfSAlan Cox  *	Helper method for drivers which have no PATA cable detection.
2916be0d18dfSAlan Cox  */
2917be0d18dfSAlan Cox 
2918be0d18dfSAlan Cox int ata_cable_unknown(struct ata_port *ap)
2919be0d18dfSAlan Cox {
2920be0d18dfSAlan Cox 	return ATA_CBL_PATA_UNK;
2921be0d18dfSAlan Cox }
2922a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_cable_unknown);
2923be0d18dfSAlan Cox 
2924be0d18dfSAlan Cox /**
2925c88f90c3STejun Heo  *	ata_cable_ignore	-	return ignored PATA cable.
2926c88f90c3STejun Heo  *	@ap: port
2927c88f90c3STejun Heo  *
2928c88f90c3STejun Heo  *	Helper method for drivers which don't use cable type to limit
2929c88f90c3STejun Heo  *	transfer mode.
2930c88f90c3STejun Heo  */
2931c88f90c3STejun Heo int ata_cable_ignore(struct ata_port *ap)
2932c88f90c3STejun Heo {
2933c88f90c3STejun Heo 	return ATA_CBL_PATA_IGN;
2934c88f90c3STejun Heo }
2935a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_cable_ignore);
2936c88f90c3STejun Heo 
2937c88f90c3STejun Heo /**
2938be0d18dfSAlan Cox  *	ata_cable_sata	-	return SATA cable type
2939be0d18dfSAlan Cox  *	@ap: port
2940be0d18dfSAlan Cox  *
2941be0d18dfSAlan Cox  *	Helper method for drivers which have SATA cables
2942be0d18dfSAlan Cox  */
2943be0d18dfSAlan Cox 
2944be0d18dfSAlan Cox int ata_cable_sata(struct ata_port *ap)
2945be0d18dfSAlan Cox {
2946be0d18dfSAlan Cox 	return ATA_CBL_SATA;
2947be0d18dfSAlan Cox }
2948a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_cable_sata);
2949be0d18dfSAlan Cox 
2950be0d18dfSAlan Cox /**
2951c6fd2807SJeff Garzik  *	ata_bus_probe - Reset and probe ATA bus
2952c6fd2807SJeff Garzik  *	@ap: Bus to probe
2953c6fd2807SJeff Garzik  *
2954c6fd2807SJeff Garzik  *	Master ATA bus probing function.  Initiates a hardware-dependent
2955c6fd2807SJeff Garzik  *	bus reset, then attempts to identify any devices found on
2956c6fd2807SJeff Garzik  *	the bus.
2957c6fd2807SJeff Garzik  *
2958c6fd2807SJeff Garzik  *	LOCKING:
2959c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
2960c6fd2807SJeff Garzik  *
2961c6fd2807SJeff Garzik  *	RETURNS:
2962c6fd2807SJeff Garzik  *	Zero on success, negative errno otherwise.
2963c6fd2807SJeff Garzik  */
2964c6fd2807SJeff Garzik 
2965c6fd2807SJeff Garzik int ata_bus_probe(struct ata_port *ap)
2966c6fd2807SJeff Garzik {
2967c6fd2807SJeff Garzik 	unsigned int classes[ATA_MAX_DEVICES];
2968c6fd2807SJeff Garzik 	int tries[ATA_MAX_DEVICES];
2969f58229f8STejun Heo 	int rc;
2970c6fd2807SJeff Garzik 	struct ata_device *dev;
2971c6fd2807SJeff Garzik 
29721eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL)
2973f58229f8STejun Heo 		tries[dev->devno] = ATA_PROBE_MAX_TRIES;
2974c6fd2807SJeff Garzik 
2975c6fd2807SJeff Garzik  retry:
29761eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL) {
2977cdeab114STejun Heo 		/* If we issue an SRST then an ATA drive (not ATAPI)
2978cdeab114STejun Heo 		 * may change configuration and be in PIO0 timing. If
2979cdeab114STejun Heo 		 * we do a hard reset (or are coming from power on)
2980cdeab114STejun Heo 		 * this is true for ATA or ATAPI. Until we've set a
2981cdeab114STejun Heo 		 * suitable controller mode we should not touch the
2982cdeab114STejun Heo 		 * bus as we may be talking too fast.
2983cdeab114STejun Heo 		 */
2984cdeab114STejun Heo 		dev->pio_mode = XFER_PIO_0;
29855416912aSAaron Lu 		dev->dma_mode = 0xff;
2986cdeab114STejun Heo 
2987cdeab114STejun Heo 		/* If the controller has a pio mode setup function
2988cdeab114STejun Heo 		 * then use it to set the chipset to rights. Don't
2989cdeab114STejun Heo 		 * touch the DMA setup as that will be dealt with when
2990cdeab114STejun Heo 		 * configuring devices.
2991cdeab114STejun Heo 		 */
2992cdeab114STejun Heo 		if (ap->ops->set_piomode)
2993cdeab114STejun Heo 			ap->ops->set_piomode(ap, dev);
2994cdeab114STejun Heo 	}
2995cdeab114STejun Heo 
2996c6fd2807SJeff Garzik 	/* reset and determine device classes */
2997c6fd2807SJeff Garzik 	ap->ops->phy_reset(ap);
2998c6fd2807SJeff Garzik 
29991eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL) {
30003e4ec344STejun Heo 		if (dev->class != ATA_DEV_UNKNOWN)
3001c6fd2807SJeff Garzik 			classes[dev->devno] = dev->class;
3002c6fd2807SJeff Garzik 		else
3003c6fd2807SJeff Garzik 			classes[dev->devno] = ATA_DEV_NONE;
3004c6fd2807SJeff Garzik 
3005c6fd2807SJeff Garzik 		dev->class = ATA_DEV_UNKNOWN;
3006c6fd2807SJeff Garzik 	}
3007c6fd2807SJeff Garzik 
3008f31f0cc2SJeff Garzik 	/* read IDENTIFY page and configure devices. We have to do the identify
3009f31f0cc2SJeff Garzik 	   specific sequence bass-ackwards so that PDIAG- is released by
3010f31f0cc2SJeff Garzik 	   the slave device */
3011f31f0cc2SJeff Garzik 
30121eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL_REVERSE) {
3013f58229f8STejun Heo 		if (tries[dev->devno])
3014f58229f8STejun Heo 			dev->class = classes[dev->devno];
3015c6fd2807SJeff Garzik 
3016c6fd2807SJeff Garzik 		if (!ata_dev_enabled(dev))
3017c6fd2807SJeff Garzik 			continue;
3018c6fd2807SJeff Garzik 
3019bff04647STejun Heo 		rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
3020bff04647STejun Heo 				     dev->id);
3021c6fd2807SJeff Garzik 		if (rc)
3022c6fd2807SJeff Garzik 			goto fail;
3023f31f0cc2SJeff Garzik 	}
3024f31f0cc2SJeff Garzik 
3025be0d18dfSAlan Cox 	/* Now ask for the cable type as PDIAG- should have been released */
3026be0d18dfSAlan Cox 	if (ap->ops->cable_detect)
3027be0d18dfSAlan Cox 		ap->cbl = ap->ops->cable_detect(ap);
3028be0d18dfSAlan Cox 
30291eca4365STejun Heo 	/* We may have SATA bridge glue hiding here irrespective of
30301eca4365STejun Heo 	 * the reported cable types and sensed types.  When SATA
30311eca4365STejun Heo 	 * drives indicate we have a bridge, we don't know which end
30321eca4365STejun Heo 	 * of the link the bridge is which is a problem.
30331eca4365STejun Heo 	 */
30341eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED)
3035614fe29bSAlan Cox 		if (ata_id_is_sata(dev->id))
3036614fe29bSAlan Cox 			ap->cbl = ATA_CBL_SATA;
3037614fe29bSAlan Cox 
3038f31f0cc2SJeff Garzik 	/* After the identify sequence we can now set up the devices. We do
3039f31f0cc2SJeff Garzik 	   this in the normal order so that the user doesn't get confused */
3040f31f0cc2SJeff Garzik 
30411eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED) {
30429af5c9c9STejun Heo 		ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
3043efdaedc4STejun Heo 		rc = ata_dev_configure(dev);
30449af5c9c9STejun Heo 		ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
3045c6fd2807SJeff Garzik 		if (rc)
3046c6fd2807SJeff Garzik 			goto fail;
3047c6fd2807SJeff Garzik 	}
3048c6fd2807SJeff Garzik 
3049c6fd2807SJeff Garzik 	/* configure transfer mode */
30500260731fSTejun Heo 	rc = ata_set_mode(&ap->link, &dev);
30514ae72a1eSTejun Heo 	if (rc)
3052c6fd2807SJeff Garzik 		goto fail;
3053c6fd2807SJeff Garzik 
30541eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED)
3055c6fd2807SJeff Garzik 		return 0;
3056c6fd2807SJeff Garzik 
3057c6fd2807SJeff Garzik 	return -ENODEV;
3058c6fd2807SJeff Garzik 
3059c6fd2807SJeff Garzik  fail:
30604ae72a1eSTejun Heo 	tries[dev->devno]--;
30614ae72a1eSTejun Heo 
3062c6fd2807SJeff Garzik 	switch (rc) {
3063c6fd2807SJeff Garzik 	case -EINVAL:
30644ae72a1eSTejun Heo 		/* eeek, something went very wrong, give up */
3065c6fd2807SJeff Garzik 		tries[dev->devno] = 0;
3066c6fd2807SJeff Garzik 		break;
30674ae72a1eSTejun Heo 
30684ae72a1eSTejun Heo 	case -ENODEV:
30694ae72a1eSTejun Heo 		/* give it just one more chance */
30704ae72a1eSTejun Heo 		tries[dev->devno] = min(tries[dev->devno], 1);
3071df561f66SGustavo A. R. Silva 		fallthrough;
3072c6fd2807SJeff Garzik 	case -EIO:
30734ae72a1eSTejun Heo 		if (tries[dev->devno] == 1) {
30744ae72a1eSTejun Heo 			/* This is the last chance, better to slow
30754ae72a1eSTejun Heo 			 * down than lose it.
30764ae72a1eSTejun Heo 			 */
3077a07d499bSTejun Heo 			sata_down_spd_limit(&ap->link, 0);
30784ae72a1eSTejun Heo 			ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
30794ae72a1eSTejun Heo 		}
3080c6fd2807SJeff Garzik 	}
3081c6fd2807SJeff Garzik 
30824ae72a1eSTejun Heo 	if (!tries[dev->devno])
3083c6fd2807SJeff Garzik 		ata_dev_disable(dev);
3084c6fd2807SJeff Garzik 
3085c6fd2807SJeff Garzik 	goto retry;
3086c6fd2807SJeff Garzik }
3087c6fd2807SJeff Garzik 
3088c6fd2807SJeff Garzik /**
3089c6fd2807SJeff Garzik  *	sata_print_link_status - Print SATA link status
3090936fd732STejun Heo  *	@link: SATA link to printk link status about
3091c6fd2807SJeff Garzik  *
3092c6fd2807SJeff Garzik  *	This function prints link speed and status of a SATA link.
3093c6fd2807SJeff Garzik  *
3094c6fd2807SJeff Garzik  *	LOCKING:
3095c6fd2807SJeff Garzik  *	None.
3096c6fd2807SJeff Garzik  */
30976bdb4fc9SAdrian Bunk static void sata_print_link_status(struct ata_link *link)
3098c6fd2807SJeff Garzik {
3099c6fd2807SJeff Garzik 	u32 sstatus, scontrol, tmp;
3100c6fd2807SJeff Garzik 
3101936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus))
3102c6fd2807SJeff Garzik 		return;
310355d5ba55SLi Zhong 	if (sata_scr_read(link, SCR_CONTROL, &scontrol))
310455d5ba55SLi Zhong 		return;
3105c6fd2807SJeff Garzik 
3106b1c72916STejun Heo 	if (ata_phys_link_online(link)) {
3107c6fd2807SJeff Garzik 		tmp = (sstatus >> 4) & 0xf;
3108a9a79dfeSJoe Perches 		ata_link_info(link, "SATA link up %s (SStatus %X SControl %X)\n",
3109c6fd2807SJeff Garzik 			      sata_spd_string(tmp), sstatus, scontrol);
3110c6fd2807SJeff Garzik 	} else {
3111a9a79dfeSJoe Perches 		ata_link_info(link, "SATA link down (SStatus %X SControl %X)\n",
3112c6fd2807SJeff Garzik 			      sstatus, scontrol);
3113c6fd2807SJeff Garzik 	}
3114c6fd2807SJeff Garzik }
3115c6fd2807SJeff Garzik 
3116c6fd2807SJeff Garzik /**
3117c6fd2807SJeff Garzik  *	ata_dev_pair		-	return other device on cable
3118c6fd2807SJeff Garzik  *	@adev: device
3119c6fd2807SJeff Garzik  *
3120c6fd2807SJeff Garzik  *	Obtain the other device on the same cable, or if none is
3121c6fd2807SJeff Garzik  *	present NULL is returned
3122c6fd2807SJeff Garzik  */
3123c6fd2807SJeff Garzik 
3124c6fd2807SJeff Garzik struct ata_device *ata_dev_pair(struct ata_device *adev)
3125c6fd2807SJeff Garzik {
31269af5c9c9STejun Heo 	struct ata_link *link = adev->link;
31279af5c9c9STejun Heo 	struct ata_device *pair = &link->device[1 - adev->devno];
3128c6fd2807SJeff Garzik 	if (!ata_dev_enabled(pair))
3129c6fd2807SJeff Garzik 		return NULL;
3130c6fd2807SJeff Garzik 	return pair;
3131c6fd2807SJeff Garzik }
3132a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_dev_pair);
3133c6fd2807SJeff Garzik 
3134c6fd2807SJeff Garzik /**
3135c6fd2807SJeff Garzik  *	sata_down_spd_limit - adjust SATA spd limit downward
3136936fd732STejun Heo  *	@link: Link to adjust SATA spd limit for
3137a07d499bSTejun Heo  *	@spd_limit: Additional limit
3138c6fd2807SJeff Garzik  *
3139936fd732STejun Heo  *	Adjust SATA spd limit of @link downward.  Note that this
3140c6fd2807SJeff Garzik  *	function only adjusts the limit.  The change must be applied
3141c6fd2807SJeff Garzik  *	using sata_set_spd().
3142c6fd2807SJeff Garzik  *
3143a07d499bSTejun Heo  *	If @spd_limit is non-zero, the speed is limited to equal to or
3144a07d499bSTejun Heo  *	lower than @spd_limit if such speed is supported.  If
3145a07d499bSTejun Heo  *	@spd_limit is slower than any supported speed, only the lowest
3146a07d499bSTejun Heo  *	supported speed is allowed.
3147a07d499bSTejun Heo  *
3148c6fd2807SJeff Garzik  *	LOCKING:
3149c6fd2807SJeff Garzik  *	Inherited from caller.
3150c6fd2807SJeff Garzik  *
3151c6fd2807SJeff Garzik  *	RETURNS:
3152c6fd2807SJeff Garzik  *	0 on success, negative errno on failure
3153c6fd2807SJeff Garzik  */
3154a07d499bSTejun Heo int sata_down_spd_limit(struct ata_link *link, u32 spd_limit)
3155c6fd2807SJeff Garzik {
3156c6fd2807SJeff Garzik 	u32 sstatus, spd, mask;
3157a07d499bSTejun Heo 	int rc, bit;
3158c6fd2807SJeff Garzik 
3159936fd732STejun Heo 	if (!sata_scr_valid(link))
3160008a7896STejun Heo 		return -EOPNOTSUPP;
3161008a7896STejun Heo 
3162008a7896STejun Heo 	/* If SCR can be read, use it to determine the current SPD.
3163936fd732STejun Heo 	 * If not, use cached value in link->sata_spd.
3164008a7896STejun Heo 	 */
3165936fd732STejun Heo 	rc = sata_scr_read(link, SCR_STATUS, &sstatus);
31669913ff8aSTejun Heo 	if (rc == 0 && ata_sstatus_online(sstatus))
3167008a7896STejun Heo 		spd = (sstatus >> 4) & 0xf;
3168008a7896STejun Heo 	else
3169936fd732STejun Heo 		spd = link->sata_spd;
3170c6fd2807SJeff Garzik 
3171936fd732STejun Heo 	mask = link->sata_spd_limit;
3172c6fd2807SJeff Garzik 	if (mask <= 1)
3173c6fd2807SJeff Garzik 		return -EINVAL;
3174008a7896STejun Heo 
3175008a7896STejun Heo 	/* unconditionally mask off the highest bit */
3176a07d499bSTejun Heo 	bit = fls(mask) - 1;
3177a07d499bSTejun Heo 	mask &= ~(1 << bit);
3178c6fd2807SJeff Garzik 
31792dc0b46bSDavid Milburn 	/*
31802dc0b46bSDavid Milburn 	 * Mask off all speeds higher than or equal to the current one.  At
31812dc0b46bSDavid Milburn 	 * this point, if current SPD is not available and we previously
31822dc0b46bSDavid Milburn 	 * recorded the link speed from SStatus, the driver has already
31832dc0b46bSDavid Milburn 	 * masked off the highest bit so mask should already be 1 or 0.
31842dc0b46bSDavid Milburn 	 * Otherwise, we should not force 1.5Gbps on a link where we have
31852dc0b46bSDavid Milburn 	 * not previously recorded speed from SStatus.  Just return in this
31862dc0b46bSDavid Milburn 	 * case.
3187008a7896STejun Heo 	 */
3188008a7896STejun Heo 	if (spd > 1)
3189008a7896STejun Heo 		mask &= (1 << (spd - 1)) - 1;
319069f2c934SDamien Le Moal 	else if (link->sata_spd)
31912dc0b46bSDavid Milburn 		return -EINVAL;
3192008a7896STejun Heo 
3193008a7896STejun Heo 	/* were we already at the bottom? */
3194c6fd2807SJeff Garzik 	if (!mask)
3195c6fd2807SJeff Garzik 		return -EINVAL;
3196c6fd2807SJeff Garzik 
3197a07d499bSTejun Heo 	if (spd_limit) {
3198a07d499bSTejun Heo 		if (mask & ((1 << spd_limit) - 1))
3199a07d499bSTejun Heo 			mask &= (1 << spd_limit) - 1;
3200a07d499bSTejun Heo 		else {
3201a07d499bSTejun Heo 			bit = ffs(mask) - 1;
3202a07d499bSTejun Heo 			mask = 1 << bit;
3203a07d499bSTejun Heo 		}
3204a07d499bSTejun Heo 	}
3205a07d499bSTejun Heo 
3206936fd732STejun Heo 	link->sata_spd_limit = mask;
3207c6fd2807SJeff Garzik 
3208a9a79dfeSJoe Perches 	ata_link_warn(link, "limiting SATA link speed to %s\n",
3209c6fd2807SJeff Garzik 		      sata_spd_string(fls(mask)));
3210c6fd2807SJeff Garzik 
3211c6fd2807SJeff Garzik 	return 0;
3212c6fd2807SJeff Garzik }
3213c6fd2807SJeff Garzik 
3214a9b2c120SBartlomiej Zolnierkiewicz #ifdef CONFIG_ATA_ACPI
3215c6fd2807SJeff Garzik /**
3216a0f79b92STejun Heo  *	ata_timing_cycle2mode - find xfer mode for the specified cycle duration
3217a0f79b92STejun Heo  *	@xfer_shift: ATA_SHIFT_* value for transfer type to examine.
3218a0f79b92STejun Heo  *	@cycle: cycle duration in ns
3219a0f79b92STejun Heo  *
3220a0f79b92STejun Heo  *	Return matching xfer mode for @cycle.  The returned mode is of
3221a0f79b92STejun Heo  *	the transfer type specified by @xfer_shift.  If @cycle is too
3222a0f79b92STejun Heo  *	slow for @xfer_shift, 0xff is returned.  If @cycle is faster
3223a0f79b92STejun Heo  *	than the fastest known mode, the fasted mode is returned.
3224a0f79b92STejun Heo  *
3225a0f79b92STejun Heo  *	LOCKING:
3226a0f79b92STejun Heo  *	None.
3227a0f79b92STejun Heo  *
3228a0f79b92STejun Heo  *	RETURNS:
3229a0f79b92STejun Heo  *	Matching xfer_mode, 0xff if no match found.
3230a0f79b92STejun Heo  */
3231a0f79b92STejun Heo u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
3232a0f79b92STejun Heo {
3233a0f79b92STejun Heo 	u8 base_mode = 0xff, last_mode = 0xff;
3234a0f79b92STejun Heo 	const struct ata_xfer_ent *ent;
3235a0f79b92STejun Heo 	const struct ata_timing *t;
3236a0f79b92STejun Heo 
3237a0f79b92STejun Heo 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
3238a0f79b92STejun Heo 		if (ent->shift == xfer_shift)
3239a0f79b92STejun Heo 			base_mode = ent->base;
3240a0f79b92STejun Heo 
3241a0f79b92STejun Heo 	for (t = ata_timing_find_mode(base_mode);
3242a0f79b92STejun Heo 	     t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
3243a0f79b92STejun Heo 		unsigned short this_cycle;
3244a0f79b92STejun Heo 
3245a0f79b92STejun Heo 		switch (xfer_shift) {
3246a0f79b92STejun Heo 		case ATA_SHIFT_PIO:
3247a0f79b92STejun Heo 		case ATA_SHIFT_MWDMA:
3248a0f79b92STejun Heo 			this_cycle = t->cycle;
3249a0f79b92STejun Heo 			break;
3250a0f79b92STejun Heo 		case ATA_SHIFT_UDMA:
3251a0f79b92STejun Heo 			this_cycle = t->udma;
3252a0f79b92STejun Heo 			break;
3253a0f79b92STejun Heo 		default:
3254a0f79b92STejun Heo 			return 0xff;
3255a0f79b92STejun Heo 		}
3256a0f79b92STejun Heo 
3257a0f79b92STejun Heo 		if (cycle > this_cycle)
3258a0f79b92STejun Heo 			break;
3259a0f79b92STejun Heo 
3260a0f79b92STejun Heo 		last_mode = t->mode;
3261a0f79b92STejun Heo 	}
3262a0f79b92STejun Heo 
3263a0f79b92STejun Heo 	return last_mode;
3264a0f79b92STejun Heo }
3265a9b2c120SBartlomiej Zolnierkiewicz #endif
3266a0f79b92STejun Heo 
3267a0f79b92STejun Heo /**
3268c6fd2807SJeff Garzik  *	ata_down_xfermask_limit - adjust dev xfer masks downward
3269c6fd2807SJeff Garzik  *	@dev: Device to adjust xfer masks
3270458337dbSTejun Heo  *	@sel: ATA_DNXFER_* selector
3271c6fd2807SJeff Garzik  *
3272c6fd2807SJeff Garzik  *	Adjust xfer masks of @dev downward.  Note that this function
3273c6fd2807SJeff Garzik  *	does not apply the change.  Invoking ata_set_mode() afterwards
3274c6fd2807SJeff Garzik  *	will apply the limit.
3275c6fd2807SJeff Garzik  *
3276c6fd2807SJeff Garzik  *	LOCKING:
3277c6fd2807SJeff Garzik  *	Inherited from caller.
3278c6fd2807SJeff Garzik  *
3279c6fd2807SJeff Garzik  *	RETURNS:
3280c6fd2807SJeff Garzik  *	0 on success, negative errno on failure
3281c6fd2807SJeff Garzik  */
3282458337dbSTejun Heo int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
3283c6fd2807SJeff Garzik {
3284458337dbSTejun Heo 	char buf[32];
3285f0a6d77bSSergey Shtylyov 	unsigned int orig_mask, xfer_mask;
3286f0a6d77bSSergey Shtylyov 	unsigned int pio_mask, mwdma_mask, udma_mask;
3287458337dbSTejun Heo 	int quiet, highbit;
3288c6fd2807SJeff Garzik 
3289458337dbSTejun Heo 	quiet = !!(sel & ATA_DNXFER_QUIET);
3290458337dbSTejun Heo 	sel &= ~ATA_DNXFER_QUIET;
3291458337dbSTejun Heo 
3292458337dbSTejun Heo 	xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
3293458337dbSTejun Heo 						  dev->mwdma_mask,
3294c6fd2807SJeff Garzik 						  dev->udma_mask);
3295458337dbSTejun Heo 	ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
3296c6fd2807SJeff Garzik 
3297458337dbSTejun Heo 	switch (sel) {
3298458337dbSTejun Heo 	case ATA_DNXFER_PIO:
3299458337dbSTejun Heo 		highbit = fls(pio_mask) - 1;
3300458337dbSTejun Heo 		pio_mask &= ~(1 << highbit);
3301458337dbSTejun Heo 		break;
3302458337dbSTejun Heo 
3303458337dbSTejun Heo 	case ATA_DNXFER_DMA:
3304458337dbSTejun Heo 		if (udma_mask) {
3305458337dbSTejun Heo 			highbit = fls(udma_mask) - 1;
3306458337dbSTejun Heo 			udma_mask &= ~(1 << highbit);
3307458337dbSTejun Heo 			if (!udma_mask)
3308458337dbSTejun Heo 				return -ENOENT;
3309458337dbSTejun Heo 		} else if (mwdma_mask) {
3310458337dbSTejun Heo 			highbit = fls(mwdma_mask) - 1;
3311458337dbSTejun Heo 			mwdma_mask &= ~(1 << highbit);
3312458337dbSTejun Heo 			if (!mwdma_mask)
3313458337dbSTejun Heo 				return -ENOENT;
3314458337dbSTejun Heo 		}
3315458337dbSTejun Heo 		break;
3316458337dbSTejun Heo 
3317458337dbSTejun Heo 	case ATA_DNXFER_40C:
3318458337dbSTejun Heo 		udma_mask &= ATA_UDMA_MASK_40C;
3319458337dbSTejun Heo 		break;
3320458337dbSTejun Heo 
3321458337dbSTejun Heo 	case ATA_DNXFER_FORCE_PIO0:
3322458337dbSTejun Heo 		pio_mask &= 1;
3323df561f66SGustavo A. R. Silva 		fallthrough;
3324458337dbSTejun Heo 	case ATA_DNXFER_FORCE_PIO:
3325458337dbSTejun Heo 		mwdma_mask = 0;
3326458337dbSTejun Heo 		udma_mask = 0;
3327458337dbSTejun Heo 		break;
3328458337dbSTejun Heo 
3329458337dbSTejun Heo 	default:
3330458337dbSTejun Heo 		BUG();
3331458337dbSTejun Heo 	}
3332458337dbSTejun Heo 
3333458337dbSTejun Heo 	xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
3334458337dbSTejun Heo 
3335458337dbSTejun Heo 	if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
3336458337dbSTejun Heo 		return -ENOENT;
3337458337dbSTejun Heo 
3338458337dbSTejun Heo 	if (!quiet) {
3339458337dbSTejun Heo 		if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
3340458337dbSTejun Heo 			snprintf(buf, sizeof(buf), "%s:%s",
3341458337dbSTejun Heo 				 ata_mode_string(xfer_mask),
3342458337dbSTejun Heo 				 ata_mode_string(xfer_mask & ATA_MASK_PIO));
3343458337dbSTejun Heo 		else
3344458337dbSTejun Heo 			snprintf(buf, sizeof(buf), "%s",
3345458337dbSTejun Heo 				 ata_mode_string(xfer_mask));
3346458337dbSTejun Heo 
3347a9a79dfeSJoe Perches 		ata_dev_warn(dev, "limiting speed to %s\n", buf);
3348458337dbSTejun Heo 	}
3349c6fd2807SJeff Garzik 
3350c6fd2807SJeff Garzik 	ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
3351c6fd2807SJeff Garzik 			    &dev->udma_mask);
3352c6fd2807SJeff Garzik 
3353c6fd2807SJeff Garzik 	return 0;
3354c6fd2807SJeff Garzik }
3355c6fd2807SJeff Garzik 
3356c6fd2807SJeff Garzik static int ata_dev_set_mode(struct ata_device *dev)
3357c6fd2807SJeff Garzik {
3358d0cb43b3STejun Heo 	struct ata_port *ap = dev->link->ap;
33599af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
3360d0cb43b3STejun Heo 	const bool nosetxfer = dev->horkage & ATA_HORKAGE_NOSETXFER;
33614055dee7STejun Heo 	const char *dev_err_whine = "";
33624055dee7STejun Heo 	int ign_dev_err = 0;
3363d0cb43b3STejun Heo 	unsigned int err_mask = 0;
3364c6fd2807SJeff Garzik 	int rc;
3365c6fd2807SJeff Garzik 
3366c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_PIO;
3367c6fd2807SJeff Garzik 	if (dev->xfer_shift == ATA_SHIFT_PIO)
3368c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_PIO;
3369c6fd2807SJeff Garzik 
3370d0cb43b3STejun Heo 	if (nosetxfer && ap->flags & ATA_FLAG_SATA && ata_id_is_sata(dev->id))
3371d0cb43b3STejun Heo 		dev_err_whine = " (SET_XFERMODE skipped)";
3372d0cb43b3STejun Heo 	else {
3373d0cb43b3STejun Heo 		if (nosetxfer)
3374a9a79dfeSJoe Perches 			ata_dev_warn(dev,
3375d0cb43b3STejun Heo 				     "NOSETXFER but PATA detected - can't "
3376d0cb43b3STejun Heo 				     "skip SETXFER, might malfunction\n");
3377c6fd2807SJeff Garzik 		err_mask = ata_dev_set_xfermode(dev);
3378d0cb43b3STejun Heo 	}
33792dcb407eSJeff Garzik 
33804055dee7STejun Heo 	if (err_mask & ~AC_ERR_DEV)
33814055dee7STejun Heo 		goto fail;
33822dcb407eSJeff Garzik 
33834055dee7STejun Heo 	/* revalidate */
3384baa1e78aSTejun Heo 	ehc->i.flags |= ATA_EHI_POST_SETMODE;
3385422c9daaSTejun Heo 	rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
3386baa1e78aSTejun Heo 	ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
3387c6fd2807SJeff Garzik 	if (rc)
3388c6fd2807SJeff Garzik 		return rc;
3389c6fd2807SJeff Garzik 
3390b93fda12SAlan Cox 	if (dev->xfer_shift == ATA_SHIFT_PIO) {
33914055dee7STejun Heo 		/* Old CFA may refuse this command, which is just fine */
3392b93fda12SAlan Cox 		if (ata_id_is_cfa(dev->id))
33934055dee7STejun Heo 			ign_dev_err = 1;
3394b93fda12SAlan Cox 		/* Catch several broken garbage emulations plus some pre
3395b93fda12SAlan Cox 		   ATA devices */
3396b93fda12SAlan Cox 		if (ata_id_major_version(dev->id) == 0 &&
33974055dee7STejun Heo 					dev->pio_mode <= XFER_PIO_2)
33984055dee7STejun Heo 			ign_dev_err = 1;
3399b93fda12SAlan Cox 		/* Some very old devices and some bad newer ones fail
3400b93fda12SAlan Cox 		   any kind of SET_XFERMODE request but support PIO0-2
3401b93fda12SAlan Cox 		   timings and no IORDY */
3402b93fda12SAlan Cox 		if (!ata_id_has_iordy(dev->id) && dev->pio_mode <= XFER_PIO_2)
3403b93fda12SAlan Cox 			ign_dev_err = 1;
3404b93fda12SAlan Cox 	}
34054055dee7STejun Heo 	/* Early MWDMA devices do DMA but don't allow DMA mode setting.
34064055dee7STejun Heo 	   Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
34074055dee7STejun Heo 	if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
34084055dee7STejun Heo 	    dev->dma_mode == XFER_MW_DMA_0 &&
34094055dee7STejun Heo 	    (dev->id[63] >> 8) & 1)
34104055dee7STejun Heo 		ign_dev_err = 1;
34114055dee7STejun Heo 
34124055dee7STejun Heo 	/* if the device is actually configured correctly, ignore dev err */
34134055dee7STejun Heo 	if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id)))
34144055dee7STejun Heo 		ign_dev_err = 1;
34154055dee7STejun Heo 
34164055dee7STejun Heo 	if (err_mask & AC_ERR_DEV) {
34174055dee7STejun Heo 		if (!ign_dev_err)
34184055dee7STejun Heo 			goto fail;
34194055dee7STejun Heo 		else
34204055dee7STejun Heo 			dev_err_whine = " (device error ignored)";
34214055dee7STejun Heo 	}
34224055dee7STejun Heo 
34234633778bSHannes Reinecke 	ata_dev_dbg(dev, "xfer_shift=%u, xfer_mode=0x%x\n",
3424c6fd2807SJeff Garzik 		    dev->xfer_shift, (int)dev->xfer_mode);
3425c6fd2807SJeff Garzik 
342607b9b6d6SDamien Le Moal 	if (!(ehc->i.flags & ATA_EHI_QUIET) ||
342707b9b6d6SDamien Le Moal 	    ehc->i.flags & ATA_EHI_DID_HARDRESET)
3428a9a79dfeSJoe Perches 		ata_dev_info(dev, "configured for %s%s\n",
34294055dee7STejun Heo 			     ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)),
34304055dee7STejun Heo 			     dev_err_whine);
34314055dee7STejun Heo 
3432c6fd2807SJeff Garzik 	return 0;
34334055dee7STejun Heo 
34344055dee7STejun Heo  fail:
3435a9a79dfeSJoe Perches 	ata_dev_err(dev, "failed to set xfermode (err_mask=0x%x)\n", err_mask);
34364055dee7STejun Heo 	return -EIO;
3437c6fd2807SJeff Garzik }
3438c6fd2807SJeff Garzik 
3439c6fd2807SJeff Garzik /**
344004351821SAlan  *	ata_do_set_mode - Program timings and issue SET FEATURES - XFER
34410260731fSTejun Heo  *	@link: link on which timings will be programmed
34421967b7ffSJoe Perches  *	@r_failed_dev: out parameter for failed device
3443c6fd2807SJeff Garzik  *
344404351821SAlan  *	Standard implementation of the function used to tune and set
344504351821SAlan  *	ATA device disk transfer mode (PIO3, UDMA6, etc.).  If
344604351821SAlan  *	ata_dev_set_mode() fails, pointer to the failing device is
3447c6fd2807SJeff Garzik  *	returned in @r_failed_dev.
3448c6fd2807SJeff Garzik  *
3449c6fd2807SJeff Garzik  *	LOCKING:
3450c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
3451c6fd2807SJeff Garzik  *
3452c6fd2807SJeff Garzik  *	RETURNS:
3453c6fd2807SJeff Garzik  *	0 on success, negative errno otherwise
3454c6fd2807SJeff Garzik  */
345504351821SAlan 
34560260731fSTejun Heo int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
3457c6fd2807SJeff Garzik {
34580260731fSTejun Heo 	struct ata_port *ap = link->ap;
3459c6fd2807SJeff Garzik 	struct ata_device *dev;
3460f58229f8STejun Heo 	int rc = 0, used_dma = 0, found = 0;
3461c6fd2807SJeff Garzik 
3462c6fd2807SJeff Garzik 	/* step 1: calculate xfer_mask */
34631eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
3464f0a6d77bSSergey Shtylyov 		unsigned int pio_mask, dma_mask;
3465b3a70601SAlan Cox 		unsigned int mode_mask;
3466c6fd2807SJeff Garzik 
3467b3a70601SAlan Cox 		mode_mask = ATA_DMA_MASK_ATA;
3468b3a70601SAlan Cox 		if (dev->class == ATA_DEV_ATAPI)
3469b3a70601SAlan Cox 			mode_mask = ATA_DMA_MASK_ATAPI;
3470b3a70601SAlan Cox 		else if (ata_id_is_cfa(dev->id))
3471b3a70601SAlan Cox 			mode_mask = ATA_DMA_MASK_CFA;
3472b3a70601SAlan Cox 
3473c6fd2807SJeff Garzik 		ata_dev_xfermask(dev);
347433267325STejun Heo 		ata_force_xfermask(dev);
3475c6fd2807SJeff Garzik 
3476c6fd2807SJeff Garzik 		pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
3477b3a70601SAlan Cox 
3478b3a70601SAlan Cox 		if (libata_dma_mask & mode_mask)
347980a9c430SSergei Shtylyov 			dma_mask = ata_pack_xfermask(0, dev->mwdma_mask,
348080a9c430SSergei Shtylyov 						     dev->udma_mask);
3481b3a70601SAlan Cox 		else
3482b3a70601SAlan Cox 			dma_mask = 0;
3483b3a70601SAlan Cox 
3484c6fd2807SJeff Garzik 		dev->pio_mode = ata_xfer_mask2mode(pio_mask);
3485c6fd2807SJeff Garzik 		dev->dma_mode = ata_xfer_mask2mode(dma_mask);
3486c6fd2807SJeff Garzik 
3487c6fd2807SJeff Garzik 		found = 1;
3488b15b3ebaSAlan Cox 		if (ata_dma_enabled(dev))
3489c6fd2807SJeff Garzik 			used_dma = 1;
3490c6fd2807SJeff Garzik 	}
3491c6fd2807SJeff Garzik 	if (!found)
3492c6fd2807SJeff Garzik 		goto out;
3493c6fd2807SJeff Garzik 
3494c6fd2807SJeff Garzik 	/* step 2: always set host PIO timings */
34951eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
349670cd071eSTejun Heo 		if (dev->pio_mode == 0xff) {
3497a9a79dfeSJoe Perches 			ata_dev_warn(dev, "no PIO support\n");
3498c6fd2807SJeff Garzik 			rc = -EINVAL;
3499c6fd2807SJeff Garzik 			goto out;
3500c6fd2807SJeff Garzik 		}
3501c6fd2807SJeff Garzik 
3502c6fd2807SJeff Garzik 		dev->xfer_mode = dev->pio_mode;
3503c6fd2807SJeff Garzik 		dev->xfer_shift = ATA_SHIFT_PIO;
3504c6fd2807SJeff Garzik 		if (ap->ops->set_piomode)
3505c6fd2807SJeff Garzik 			ap->ops->set_piomode(ap, dev);
3506c6fd2807SJeff Garzik 	}
3507c6fd2807SJeff Garzik 
3508c6fd2807SJeff Garzik 	/* step 3: set host DMA timings */
35091eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
35101eca4365STejun Heo 		if (!ata_dma_enabled(dev))
3511c6fd2807SJeff Garzik 			continue;
3512c6fd2807SJeff Garzik 
3513c6fd2807SJeff Garzik 		dev->xfer_mode = dev->dma_mode;
3514c6fd2807SJeff Garzik 		dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
3515c6fd2807SJeff Garzik 		if (ap->ops->set_dmamode)
3516c6fd2807SJeff Garzik 			ap->ops->set_dmamode(ap, dev);
3517c6fd2807SJeff Garzik 	}
3518c6fd2807SJeff Garzik 
3519c6fd2807SJeff Garzik 	/* step 4: update devices' xfer mode */
35201eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
3521c6fd2807SJeff Garzik 		rc = ata_dev_set_mode(dev);
3522c6fd2807SJeff Garzik 		if (rc)
3523c6fd2807SJeff Garzik 			goto out;
3524c6fd2807SJeff Garzik 	}
3525c6fd2807SJeff Garzik 
3526c6fd2807SJeff Garzik 	/* Record simplex status. If we selected DMA then the other
3527c6fd2807SJeff Garzik 	 * host channels are not permitted to do so.
3528c6fd2807SJeff Garzik 	 */
3529cca3974eSJeff Garzik 	if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
3530032af1ceSAlan 		ap->host->simplex_claimed = ap;
3531c6fd2807SJeff Garzik 
3532c6fd2807SJeff Garzik  out:
3533c6fd2807SJeff Garzik 	if (rc)
3534c6fd2807SJeff Garzik 		*r_failed_dev = dev;
3535c6fd2807SJeff Garzik 	return rc;
3536c6fd2807SJeff Garzik }
3537a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_do_set_mode);
3538c6fd2807SJeff Garzik 
3539c6fd2807SJeff Garzik /**
3540aa2731adSTejun Heo  *	ata_wait_ready - wait for link to become ready
3541aa2731adSTejun Heo  *	@link: link to be waited on
3542aa2731adSTejun Heo  *	@deadline: deadline jiffies for the operation
3543aa2731adSTejun Heo  *	@check_ready: callback to check link readiness
3544aa2731adSTejun Heo  *
3545aa2731adSTejun Heo  *	Wait for @link to become ready.  @check_ready should return
3546aa2731adSTejun Heo  *	positive number if @link is ready, 0 if it isn't, -ENODEV if
3547aa2731adSTejun Heo  *	link doesn't seem to be occupied, other errno for other error
3548aa2731adSTejun Heo  *	conditions.
3549aa2731adSTejun Heo  *
3550aa2731adSTejun Heo  *	Transient -ENODEV conditions are allowed for
3551aa2731adSTejun Heo  *	ATA_TMOUT_FF_WAIT.
3552aa2731adSTejun Heo  *
3553aa2731adSTejun Heo  *	LOCKING:
3554aa2731adSTejun Heo  *	EH context.
3555aa2731adSTejun Heo  *
3556aa2731adSTejun Heo  *	RETURNS:
3557c9b5560aSMasanari Iida  *	0 if @link is ready before @deadline; otherwise, -errno.
3558aa2731adSTejun Heo  */
3559aa2731adSTejun Heo int ata_wait_ready(struct ata_link *link, unsigned long deadline,
3560aa2731adSTejun Heo 		   int (*check_ready)(struct ata_link *link))
3561aa2731adSTejun Heo {
3562aa2731adSTejun Heo 	unsigned long start = jiffies;
3563b48d58f5STejun Heo 	unsigned long nodev_deadline;
3564aa2731adSTejun Heo 	int warned = 0;
3565aa2731adSTejun Heo 
3566b48d58f5STejun Heo 	/* choose which 0xff timeout to use, read comment in libata.h */
3567b48d58f5STejun Heo 	if (link->ap->host->flags & ATA_HOST_PARALLEL_SCAN)
3568b48d58f5STejun Heo 		nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT_LONG);
3569b48d58f5STejun Heo 	else
3570b48d58f5STejun Heo 		nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT);
3571b48d58f5STejun Heo 
3572b1c72916STejun Heo 	/* Slave readiness can't be tested separately from master.  On
3573b1c72916STejun Heo 	 * M/S emulation configuration, this function should be called
3574b1c72916STejun Heo 	 * only on the master and it will handle both master and slave.
3575b1c72916STejun Heo 	 */
3576b1c72916STejun Heo 	WARN_ON(link == link->ap->slave_link);
3577b1c72916STejun Heo 
3578aa2731adSTejun Heo 	if (time_after(nodev_deadline, deadline))
3579aa2731adSTejun Heo 		nodev_deadline = deadline;
3580aa2731adSTejun Heo 
3581aa2731adSTejun Heo 	while (1) {
3582aa2731adSTejun Heo 		unsigned long now = jiffies;
3583aa2731adSTejun Heo 		int ready, tmp;
3584aa2731adSTejun Heo 
3585aa2731adSTejun Heo 		ready = tmp = check_ready(link);
3586aa2731adSTejun Heo 		if (ready > 0)
3587aa2731adSTejun Heo 			return 0;
3588aa2731adSTejun Heo 
3589b48d58f5STejun Heo 		/*
3590b48d58f5STejun Heo 		 * -ENODEV could be transient.  Ignore -ENODEV if link
3591aa2731adSTejun Heo 		 * is online.  Also, some SATA devices take a long
3592b48d58f5STejun Heo 		 * time to clear 0xff after reset.  Wait for
3593b48d58f5STejun Heo 		 * ATA_TMOUT_FF_WAIT[_LONG] on -ENODEV if link isn't
3594b48d58f5STejun Heo 		 * offline.
3595aa2731adSTejun Heo 		 *
3596aa2731adSTejun Heo 		 * Note that some PATA controllers (pata_ali) explode
3597aa2731adSTejun Heo 		 * if status register is read more than once when
3598aa2731adSTejun Heo 		 * there's no device attached.
3599aa2731adSTejun Heo 		 */
3600aa2731adSTejun Heo 		if (ready == -ENODEV) {
3601aa2731adSTejun Heo 			if (ata_link_online(link))
3602aa2731adSTejun Heo 				ready = 0;
3603aa2731adSTejun Heo 			else if ((link->ap->flags & ATA_FLAG_SATA) &&
3604aa2731adSTejun Heo 				 !ata_link_offline(link) &&
3605aa2731adSTejun Heo 				 time_before(now, nodev_deadline))
3606aa2731adSTejun Heo 				ready = 0;
3607aa2731adSTejun Heo 		}
3608aa2731adSTejun Heo 
3609aa2731adSTejun Heo 		if (ready)
3610aa2731adSTejun Heo 			return ready;
3611aa2731adSTejun Heo 		if (time_after(now, deadline))
3612aa2731adSTejun Heo 			return -EBUSY;
3613aa2731adSTejun Heo 
3614aa2731adSTejun Heo 		if (!warned && time_after(now, start + 5 * HZ) &&
3615aa2731adSTejun Heo 		    (deadline - now > 3 * HZ)) {
3616a9a79dfeSJoe Perches 			ata_link_warn(link,
3617aa2731adSTejun Heo 				"link is slow to respond, please be patient "
3618aa2731adSTejun Heo 				"(ready=%d)\n", tmp);
3619aa2731adSTejun Heo 			warned = 1;
3620aa2731adSTejun Heo 		}
3621aa2731adSTejun Heo 
362297750cebSTejun Heo 		ata_msleep(link->ap, 50);
3623aa2731adSTejun Heo 	}
3624aa2731adSTejun Heo }
3625aa2731adSTejun Heo 
3626aa2731adSTejun Heo /**
3627aa2731adSTejun Heo  *	ata_wait_after_reset - wait for link to become ready after reset
3628aa2731adSTejun Heo  *	@link: link to be waited on
3629aa2731adSTejun Heo  *	@deadline: deadline jiffies for the operation
3630aa2731adSTejun Heo  *	@check_ready: callback to check link readiness
3631aa2731adSTejun Heo  *
3632aa2731adSTejun Heo  *	Wait for @link to become ready after reset.
3633aa2731adSTejun Heo  *
3634aa2731adSTejun Heo  *	LOCKING:
3635aa2731adSTejun Heo  *	EH context.
3636aa2731adSTejun Heo  *
3637aa2731adSTejun Heo  *	RETURNS:
3638c9b5560aSMasanari Iida  *	0 if @link is ready before @deadline; otherwise, -errno.
3639aa2731adSTejun Heo  */
36402b4221bbSHarvey Harrison int ata_wait_after_reset(struct ata_link *link, unsigned long deadline,
3641aa2731adSTejun Heo 				int (*check_ready)(struct ata_link *link))
3642aa2731adSTejun Heo {
364397750cebSTejun Heo 	ata_msleep(link->ap, ATA_WAIT_AFTER_RESET);
3644aa2731adSTejun Heo 
3645aa2731adSTejun Heo 	return ata_wait_ready(link, deadline, check_ready);
3646aa2731adSTejun Heo }
3647a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_wait_after_reset);
3648aa2731adSTejun Heo 
3649aa2731adSTejun Heo /**
36500aa1113dSTejun Heo  *	ata_std_prereset - prepare for reset
3651cc0680a5STejun Heo  *	@link: ATA link to be reset
3652d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
3653c6fd2807SJeff Garzik  *
3654cc0680a5STejun Heo  *	@link is about to be reset.  Initialize it.  Failure from
3655b8cffc6aSTejun Heo  *	prereset makes libata abort whole reset sequence and give up
3656b8cffc6aSTejun Heo  *	that port, so prereset should be best-effort.  It does its
3657b8cffc6aSTejun Heo  *	best to prepare for reset sequence but if things go wrong, it
3658b8cffc6aSTejun Heo  *	should just whine, not fail.
3659c6fd2807SJeff Garzik  *
3660c6fd2807SJeff Garzik  *	LOCKING:
3661c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3662c6fd2807SJeff Garzik  *
3663c6fd2807SJeff Garzik  *	RETURNS:
3664ac1eb665SSergey Shtylyov  *	Always 0.
3665c6fd2807SJeff Garzik  */
36660aa1113dSTejun Heo int ata_std_prereset(struct ata_link *link, unsigned long deadline)
3667c6fd2807SJeff Garzik {
3668cc0680a5STejun Heo 	struct ata_port *ap = link->ap;
3669936fd732STejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
3670c6fd2807SJeff Garzik 	const unsigned long *timing = sata_ehc_deb_timing(ehc);
3671c6fd2807SJeff Garzik 	int rc;
3672c6fd2807SJeff Garzik 
3673c6fd2807SJeff Garzik 	/* if we're about to do hardreset, nothing more to do */
3674c6fd2807SJeff Garzik 	if (ehc->i.action & ATA_EH_HARDRESET)
3675c6fd2807SJeff Garzik 		return 0;
3676c6fd2807SJeff Garzik 
3677936fd732STejun Heo 	/* if SATA, resume link */
3678a16abc0bSTejun Heo 	if (ap->flags & ATA_FLAG_SATA) {
3679936fd732STejun Heo 		rc = sata_link_resume(link, timing, deadline);
3680b8cffc6aSTejun Heo 		/* whine about phy resume failure but proceed */
3681b8cffc6aSTejun Heo 		if (rc && rc != -EOPNOTSUPP)
3682a9a79dfeSJoe Perches 			ata_link_warn(link,
3683a9a79dfeSJoe Perches 				      "failed to resume link for reset (errno=%d)\n",
3684a9a79dfeSJoe Perches 				      rc);
3685c6fd2807SJeff Garzik 	}
3686c6fd2807SJeff Garzik 
368745db2f6cSTejun Heo 	/* no point in trying softreset on offline link */
3688b1c72916STejun Heo 	if (ata_phys_link_offline(link))
368945db2f6cSTejun Heo 		ehc->i.action &= ~ATA_EH_SOFTRESET;
369045db2f6cSTejun Heo 
3691c6fd2807SJeff Garzik 	return 0;
3692c6fd2807SJeff Garzik }
3693a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_std_prereset);
3694c6fd2807SJeff Garzik 
3695c6fd2807SJeff Garzik /**
369657c9efdfSTejun Heo  *	sata_std_hardreset - COMRESET w/o waiting or classification
369757c9efdfSTejun Heo  *	@link: link to reset
369857c9efdfSTejun Heo  *	@class: resulting class of attached device
369957c9efdfSTejun Heo  *	@deadline: deadline jiffies for the operation
370057c9efdfSTejun Heo  *
370157c9efdfSTejun Heo  *	Standard SATA COMRESET w/o waiting or classification.
370257c9efdfSTejun Heo  *
370357c9efdfSTejun Heo  *	LOCKING:
370457c9efdfSTejun Heo  *	Kernel thread context (may sleep)
370557c9efdfSTejun Heo  *
370657c9efdfSTejun Heo  *	RETURNS:
370757c9efdfSTejun Heo  *	0 if link offline, -EAGAIN if link online, -errno on errors.
370857c9efdfSTejun Heo  */
370957c9efdfSTejun Heo int sata_std_hardreset(struct ata_link *link, unsigned int *class,
371057c9efdfSTejun Heo 		       unsigned long deadline)
371157c9efdfSTejun Heo {
371257c9efdfSTejun Heo 	const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
371357c9efdfSTejun Heo 	bool online;
371457c9efdfSTejun Heo 	int rc;
371557c9efdfSTejun Heo 
371657c9efdfSTejun Heo 	/* do hardreset */
371757c9efdfSTejun Heo 	rc = sata_link_hardreset(link, timing, deadline, &online, NULL);
371857c9efdfSTejun Heo 	return online ? -EAGAIN : rc;
371957c9efdfSTejun Heo }
3720a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(sata_std_hardreset);
372157c9efdfSTejun Heo 
372257c9efdfSTejun Heo /**
3723203c75b8STejun Heo  *	ata_std_postreset - standard postreset callback
3724cc0680a5STejun Heo  *	@link: the target ata_link
3725c6fd2807SJeff Garzik  *	@classes: classes of attached devices
3726c6fd2807SJeff Garzik  *
3727c6fd2807SJeff Garzik  *	This function is invoked after a successful reset.  Note that
3728c6fd2807SJeff Garzik  *	the device might have been reset more than once using
3729c6fd2807SJeff Garzik  *	different reset methods before postreset is invoked.
3730c6fd2807SJeff Garzik  *
3731c6fd2807SJeff Garzik  *	LOCKING:
3732c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3733c6fd2807SJeff Garzik  */
3734203c75b8STejun Heo void ata_std_postreset(struct ata_link *link, unsigned int *classes)
3735c6fd2807SJeff Garzik {
3736f046519fSTejun Heo 	u32 serror;
3737f046519fSTejun Heo 
3738f046519fSTejun Heo 	/* reset complete, clear SError */
3739f046519fSTejun Heo 	if (!sata_scr_read(link, SCR_ERROR, &serror))
3740f046519fSTejun Heo 		sata_scr_write(link, SCR_ERROR, serror);
3741f046519fSTejun Heo 
3742c6fd2807SJeff Garzik 	/* print link status */
3743936fd732STejun Heo 	sata_print_link_status(link);
3744c6fd2807SJeff Garzik }
3745a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_std_postreset);
3746c6fd2807SJeff Garzik 
3747c6fd2807SJeff Garzik /**
3748c6fd2807SJeff Garzik  *	ata_dev_same_device - Determine whether new ID matches configured device
3749c6fd2807SJeff Garzik  *	@dev: device to compare against
3750c6fd2807SJeff Garzik  *	@new_class: class of the new device
3751c6fd2807SJeff Garzik  *	@new_id: IDENTIFY page of the new device
3752c6fd2807SJeff Garzik  *
3753c6fd2807SJeff Garzik  *	Compare @new_class and @new_id against @dev and determine
3754c6fd2807SJeff Garzik  *	whether @dev is the device indicated by @new_class and
3755c6fd2807SJeff Garzik  *	@new_id.
3756c6fd2807SJeff Garzik  *
3757c6fd2807SJeff Garzik  *	LOCKING:
3758c6fd2807SJeff Garzik  *	None.
3759c6fd2807SJeff Garzik  *
3760c6fd2807SJeff Garzik  *	RETURNS:
3761c6fd2807SJeff Garzik  *	1 if @dev matches @new_class and @new_id, 0 otherwise.
3762c6fd2807SJeff Garzik  */
3763c6fd2807SJeff Garzik static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
3764c6fd2807SJeff Garzik 			       const u16 *new_id)
3765c6fd2807SJeff Garzik {
3766c6fd2807SJeff Garzik 	const u16 *old_id = dev->id;
3767a0cf733bSTejun Heo 	unsigned char model[2][ATA_ID_PROD_LEN + 1];
3768a0cf733bSTejun Heo 	unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
3769c6fd2807SJeff Garzik 
3770c6fd2807SJeff Garzik 	if (dev->class != new_class) {
3771a9a79dfeSJoe Perches 		ata_dev_info(dev, "class mismatch %d != %d\n",
3772c6fd2807SJeff Garzik 			     dev->class, new_class);
3773c6fd2807SJeff Garzik 		return 0;
3774c6fd2807SJeff Garzik 	}
3775c6fd2807SJeff Garzik 
3776a0cf733bSTejun Heo 	ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
3777a0cf733bSTejun Heo 	ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
3778a0cf733bSTejun Heo 	ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
3779a0cf733bSTejun Heo 	ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
3780c6fd2807SJeff Garzik 
3781c6fd2807SJeff Garzik 	if (strcmp(model[0], model[1])) {
3782a9a79dfeSJoe Perches 		ata_dev_info(dev, "model number mismatch '%s' != '%s'\n",
3783a9a79dfeSJoe Perches 			     model[0], model[1]);
3784c6fd2807SJeff Garzik 		return 0;
3785c6fd2807SJeff Garzik 	}
3786c6fd2807SJeff Garzik 
3787c6fd2807SJeff Garzik 	if (strcmp(serial[0], serial[1])) {
3788a9a79dfeSJoe Perches 		ata_dev_info(dev, "serial number mismatch '%s' != '%s'\n",
3789a9a79dfeSJoe Perches 			     serial[0], serial[1]);
3790c6fd2807SJeff Garzik 		return 0;
3791c6fd2807SJeff Garzik 	}
3792c6fd2807SJeff Garzik 
3793c6fd2807SJeff Garzik 	return 1;
3794c6fd2807SJeff Garzik }
3795c6fd2807SJeff Garzik 
3796c6fd2807SJeff Garzik /**
3797fe30911bSTejun Heo  *	ata_dev_reread_id - Re-read IDENTIFY data
37983fae450cSHenrik Kretzschmar  *	@dev: target ATA device
3799bff04647STejun Heo  *	@readid_flags: read ID flags
3800c6fd2807SJeff Garzik  *
3801c6fd2807SJeff Garzik  *	Re-read IDENTIFY page and make sure @dev is still attached to
3802c6fd2807SJeff Garzik  *	the port.
3803c6fd2807SJeff Garzik  *
3804c6fd2807SJeff Garzik  *	LOCKING:
3805c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3806c6fd2807SJeff Garzik  *
3807c6fd2807SJeff Garzik  *	RETURNS:
3808c6fd2807SJeff Garzik  *	0 on success, negative errno otherwise
3809c6fd2807SJeff Garzik  */
3810fe30911bSTejun Heo int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
3811c6fd2807SJeff Garzik {
3812c6fd2807SJeff Garzik 	unsigned int class = dev->class;
38139af5c9c9STejun Heo 	u16 *id = (void *)dev->link->ap->sector_buf;
3814c6fd2807SJeff Garzik 	int rc;
3815c6fd2807SJeff Garzik 
3816c6fd2807SJeff Garzik 	/* read ID data */
3817bff04647STejun Heo 	rc = ata_dev_read_id(dev, &class, readid_flags, id);
3818c6fd2807SJeff Garzik 	if (rc)
3819fe30911bSTejun Heo 		return rc;
3820c6fd2807SJeff Garzik 
3821c6fd2807SJeff Garzik 	/* is the device still there? */
3822fe30911bSTejun Heo 	if (!ata_dev_same_device(dev, class, id))
3823fe30911bSTejun Heo 		return -ENODEV;
3824c6fd2807SJeff Garzik 
3825c6fd2807SJeff Garzik 	memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
3826fe30911bSTejun Heo 	return 0;
3827fe30911bSTejun Heo }
3828fe30911bSTejun Heo 
3829fe30911bSTejun Heo /**
3830fe30911bSTejun Heo  *	ata_dev_revalidate - Revalidate ATA device
3831fe30911bSTejun Heo  *	@dev: device to revalidate
3832422c9daaSTejun Heo  *	@new_class: new class code
3833fe30911bSTejun Heo  *	@readid_flags: read ID flags
3834fe30911bSTejun Heo  *
3835fe30911bSTejun Heo  *	Re-read IDENTIFY page, make sure @dev is still attached to the
3836fe30911bSTejun Heo  *	port and reconfigure it according to the new IDENTIFY page.
3837fe30911bSTejun Heo  *
3838fe30911bSTejun Heo  *	LOCKING:
3839fe30911bSTejun Heo  *	Kernel thread context (may sleep)
3840fe30911bSTejun Heo  *
3841fe30911bSTejun Heo  *	RETURNS:
3842fe30911bSTejun Heo  *	0 on success, negative errno otherwise
3843fe30911bSTejun Heo  */
3844422c9daaSTejun Heo int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
3845422c9daaSTejun Heo 		       unsigned int readid_flags)
3846fe30911bSTejun Heo {
38476ddcd3b0STejun Heo 	u64 n_sectors = dev->n_sectors;
38485920dadfSTejun Heo 	u64 n_native_sectors = dev->n_native_sectors;
3849fe30911bSTejun Heo 	int rc;
3850fe30911bSTejun Heo 
3851fe30911bSTejun Heo 	if (!ata_dev_enabled(dev))
3852fe30911bSTejun Heo 		return -ENODEV;
3853fe30911bSTejun Heo 
3854422c9daaSTejun Heo 	/* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
3855422c9daaSTejun Heo 	if (ata_class_enabled(new_class) &&
3856f0d0613dSBorislav Petkov 	    new_class != ATA_DEV_ATA &&
3857f0d0613dSBorislav Petkov 	    new_class != ATA_DEV_ATAPI &&
38589162c657SHannes Reinecke 	    new_class != ATA_DEV_ZAC &&
3859f0d0613dSBorislav Petkov 	    new_class != ATA_DEV_SEMB) {
3860a9a79dfeSJoe Perches 		ata_dev_info(dev, "class mismatch %u != %u\n",
3861422c9daaSTejun Heo 			     dev->class, new_class);
3862422c9daaSTejun Heo 		rc = -ENODEV;
3863422c9daaSTejun Heo 		goto fail;
3864422c9daaSTejun Heo 	}
3865422c9daaSTejun Heo 
3866fe30911bSTejun Heo 	/* re-read ID */
3867fe30911bSTejun Heo 	rc = ata_dev_reread_id(dev, readid_flags);
3868fe30911bSTejun Heo 	if (rc)
3869fe30911bSTejun Heo 		goto fail;
3870c6fd2807SJeff Garzik 
3871c6fd2807SJeff Garzik 	/* configure device according to the new ID */
3872efdaedc4STejun Heo 	rc = ata_dev_configure(dev);
38736ddcd3b0STejun Heo 	if (rc)
38746ddcd3b0STejun Heo 		goto fail;
38756ddcd3b0STejun Heo 
38766ddcd3b0STejun Heo 	/* verify n_sectors hasn't changed */
3877445d211bSTejun Heo 	if (dev->class != ATA_DEV_ATA || !n_sectors ||
3878445d211bSTejun Heo 	    dev->n_sectors == n_sectors)
3879445d211bSTejun Heo 		return 0;
3880445d211bSTejun Heo 
3881445d211bSTejun Heo 	/* n_sectors has changed */
3882a9a79dfeSJoe Perches 	ata_dev_warn(dev, "n_sectors mismatch %llu != %llu\n",
38836ddcd3b0STejun Heo 		     (unsigned long long)n_sectors,
38846ddcd3b0STejun Heo 		     (unsigned long long)dev->n_sectors);
3885445d211bSTejun Heo 
38865920dadfSTejun Heo 	/*
38875920dadfSTejun Heo 	 * Something could have caused HPA to be unlocked
3888445d211bSTejun Heo 	 * involuntarily.  If n_native_sectors hasn't changed and the
3889445d211bSTejun Heo 	 * new size matches it, keep the device.
38905920dadfSTejun Heo 	 */
38915920dadfSTejun Heo 	if (dev->n_native_sectors == n_native_sectors &&
3892445d211bSTejun Heo 	    dev->n_sectors > n_sectors && dev->n_sectors == n_native_sectors) {
3893a9a79dfeSJoe Perches 		ata_dev_warn(dev,
38945920dadfSTejun Heo 			     "new n_sectors matches native, probably "
389568939ce5STejun Heo 			     "late HPA unlock, n_sectors updated\n");
389668939ce5STejun Heo 		/* use the larger n_sectors */
3897445d211bSTejun Heo 		return 0;
3898445d211bSTejun Heo 	}
3899445d211bSTejun Heo 
3900445d211bSTejun Heo 	/*
3901445d211bSTejun Heo 	 * Some BIOSes boot w/o HPA but resume w/ HPA locked.  Try
3902445d211bSTejun Heo 	 * unlocking HPA in those cases.
3903445d211bSTejun Heo 	 *
3904445d211bSTejun Heo 	 * https://bugzilla.kernel.org/show_bug.cgi?id=15396
3905445d211bSTejun Heo 	 */
3906445d211bSTejun Heo 	if (dev->n_native_sectors == n_native_sectors &&
3907445d211bSTejun Heo 	    dev->n_sectors < n_sectors && n_sectors == n_native_sectors &&
3908445d211bSTejun Heo 	    !(dev->horkage & ATA_HORKAGE_BROKEN_HPA)) {
3909a9a79dfeSJoe Perches 		ata_dev_warn(dev,
3910445d211bSTejun Heo 			     "old n_sectors matches native, probably "
3911445d211bSTejun Heo 			     "late HPA lock, will try to unlock HPA\n");
3912445d211bSTejun Heo 		/* try unlocking HPA */
3913445d211bSTejun Heo 		dev->flags |= ATA_DFLAG_UNLOCK_HPA;
3914445d211bSTejun Heo 		rc = -EIO;
3915445d211bSTejun Heo 	} else
3916445d211bSTejun Heo 		rc = -ENODEV;
3917445d211bSTejun Heo 
3918445d211bSTejun Heo 	/* restore original n_[native_]sectors and fail */
39195920dadfSTejun Heo 	dev->n_native_sectors = n_native_sectors;
39205920dadfSTejun Heo 	dev->n_sectors = n_sectors;
3921c6fd2807SJeff Garzik  fail:
3922a9a79dfeSJoe Perches 	ata_dev_err(dev, "revalidation failed (errno=%d)\n", rc);
3923c6fd2807SJeff Garzik 	return rc;
3924c6fd2807SJeff Garzik }
3925c6fd2807SJeff Garzik 
39266919a0a6SAlan Cox struct ata_blacklist_entry {
39276919a0a6SAlan Cox 	const char *model_num;
39286919a0a6SAlan Cox 	const char *model_rev;
39296919a0a6SAlan Cox 	unsigned long horkage;
39306919a0a6SAlan Cox };
39316919a0a6SAlan Cox 
39326919a0a6SAlan Cox static const struct ata_blacklist_entry ata_device_blacklist [] = {
39336919a0a6SAlan Cox 	/* Devices with DMA related problems under Linux */
39346919a0a6SAlan Cox 	{ "WDC AC11000H",	NULL,		ATA_HORKAGE_NODMA },
39356919a0a6SAlan Cox 	{ "WDC AC22100H",	NULL,		ATA_HORKAGE_NODMA },
39366919a0a6SAlan Cox 	{ "WDC AC32500H",	NULL,		ATA_HORKAGE_NODMA },
39376919a0a6SAlan Cox 	{ "WDC AC33100H",	NULL,		ATA_HORKAGE_NODMA },
39386919a0a6SAlan Cox 	{ "WDC AC31600H",	NULL,		ATA_HORKAGE_NODMA },
39396919a0a6SAlan Cox 	{ "WDC AC32100H",	"24.09P07",	ATA_HORKAGE_NODMA },
39406919a0a6SAlan Cox 	{ "WDC AC23200L",	"21.10N21",	ATA_HORKAGE_NODMA },
39416919a0a6SAlan Cox 	{ "Compaq CRD-8241B", 	NULL,		ATA_HORKAGE_NODMA },
39426919a0a6SAlan Cox 	{ "CRD-8400B",		NULL, 		ATA_HORKAGE_NODMA },
39437da4c935SMark Lord 	{ "CRD-848[02]B",	NULL,		ATA_HORKAGE_NODMA },
39446919a0a6SAlan Cox 	{ "CRD-84",		NULL,		ATA_HORKAGE_NODMA },
39456919a0a6SAlan Cox 	{ "SanDisk SDP3B",	NULL,		ATA_HORKAGE_NODMA },
39466919a0a6SAlan Cox 	{ "SanDisk SDP3B-64",	NULL,		ATA_HORKAGE_NODMA },
39476919a0a6SAlan Cox 	{ "SANYO CD-ROM CRD",	NULL,		ATA_HORKAGE_NODMA },
39486919a0a6SAlan Cox 	{ "HITACHI CDR-8",	NULL,		ATA_HORKAGE_NODMA },
39497da4c935SMark Lord 	{ "HITACHI CDR-8[34]35",NULL,		ATA_HORKAGE_NODMA },
39506919a0a6SAlan Cox 	{ "Toshiba CD-ROM XM-6202B", NULL,	ATA_HORKAGE_NODMA },
39516919a0a6SAlan Cox 	{ "TOSHIBA CD-ROM XM-1702BC", NULL,	ATA_HORKAGE_NODMA },
39526919a0a6SAlan Cox 	{ "CD-532E-A", 		NULL,		ATA_HORKAGE_NODMA },
39536919a0a6SAlan Cox 	{ "E-IDE CD-ROM CR-840",NULL,		ATA_HORKAGE_NODMA },
39546919a0a6SAlan Cox 	{ "CD-ROM Drive/F5A",	NULL,		ATA_HORKAGE_NODMA },
39556919a0a6SAlan Cox 	{ "WPI CDD-820", 	NULL,		ATA_HORKAGE_NODMA },
39566919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SC-148C", NULL,	ATA_HORKAGE_NODMA },
39576919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SC",	NULL,		ATA_HORKAGE_NODMA },
39586919a0a6SAlan Cox 	{ "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
39596919a0a6SAlan Cox 	{ "_NEC DV5800A", 	NULL,		ATA_HORKAGE_NODMA },
39606919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SN-124", "N001",	ATA_HORKAGE_NODMA },
396139f19886SDave Jones 	{ "Seagate STT20000A", NULL,		ATA_HORKAGE_NODMA },
3962d70e551cSPrarit Bhargava 	{ " 2GB ATA Flash Disk", "ADMA428M",	ATA_HORKAGE_NODMA },
3963b00622fcSShikha Jain 	{ "VRFDFC22048UCHC-TE*", NULL,		ATA_HORKAGE_NODMA },
39643af9a77aSTejun Heo 	/* Odd clown on sil3726/4726 PMPs */
396550af2fa1STejun Heo 	{ "Config  Disk",	NULL,		ATA_HORKAGE_DISABLE },
3966a66307d4SHannes Reinecke 	/* Similar story with ASMedia 1092 */
3967a66307d4SHannes Reinecke 	{ "ASMT109x- Config",	NULL,		ATA_HORKAGE_DISABLE },
39686919a0a6SAlan Cox 
396918d6e9d5SAlbert Lee 	/* Weird ATAPI devices */
397040a1d531STejun Heo 	{ "TORiSAN DVD-ROM DRD-N216", NULL,	ATA_HORKAGE_MAX_SEC_128 },
39716a87e42eSTejun Heo 	{ "QUANTUM DAT    DAT72-000", NULL,	ATA_HORKAGE_ATAPI_MOD16_DMA },
3972a32450e1SShan Hai 	{ "Slimtype DVD A  DS8A8SH", NULL,	ATA_HORKAGE_MAX_SEC_LBA48 },
39730523f037SShan Hai 	{ "Slimtype DVD A  DS8A9SH", NULL,	ATA_HORKAGE_MAX_SEC_LBA48 },
397418d6e9d5SAlbert Lee 
3975af34d637SDavid Milburn 	/*
3976af34d637SDavid Milburn 	 * Causes silent data corruption with higher max sects.
3977af34d637SDavid Milburn 	 * http://lkml.kernel.org/g/x49wpy40ysk.fsf@segfault.boston.devel.redhat.com
3978af34d637SDavid Milburn 	 */
3979af34d637SDavid Milburn 	{ "ST380013AS",		"3.20",		ATA_HORKAGE_MAX_SEC_1024 },
3980af34d637SDavid Milburn 
39811488a1e3STejun Heo 	/*
3982e0edc8c5STejun Heo 	 * These devices time out with higher max sects.
39831488a1e3STejun Heo 	 * https://bugzilla.kernel.org/show_bug.cgi?id=121671
39841488a1e3STejun Heo 	 */
3985e0edc8c5STejun Heo 	{ "LITEON CX1-JB*-HP",	NULL,		ATA_HORKAGE_MAX_SEC_1024 },
3986db5ff909SXinyu Lin 	{ "LITEON EP1-*",	NULL,		ATA_HORKAGE_MAX_SEC_1024 },
39871488a1e3STejun Heo 
39886919a0a6SAlan Cox 	/* Devices we expect to fail diagnostics */
39896919a0a6SAlan Cox 
39906919a0a6SAlan Cox 	/* Devices where NCQ should be avoided */
39916919a0a6SAlan Cox 	/* NCQ is slow */
39926919a0a6SAlan Cox 	{ "WDC WD740ADFD-00",	NULL,		ATA_HORKAGE_NONCQ },
3993ef1429c0SDamien Le Moal 	{ "WDC WD740ADFD-00NLR1", NULL,		ATA_HORKAGE_NONCQ },
399409125ea6STejun Heo 	/* http://thread.gmane.org/gmane.linux.ide/14907 */
399509125ea6STejun Heo 	{ "FUJITSU MHT2060BH",	NULL,		ATA_HORKAGE_NONCQ },
39967acfaf30SPaul Rolland 	/* NCQ is broken */
3997539cc7c7SJeff Garzik 	{ "Maxtor *",		"BANC*",	ATA_HORKAGE_NONCQ },
39980e3dbc01SAlan Cox 	{ "Maxtor 7V300F0",	"VA111630",	ATA_HORKAGE_NONCQ },
3999da6f0ec2SPaolo Ornati 	{ "ST380817AS",		"3.42",		ATA_HORKAGE_NONCQ },
4000e41bd3e8STejun Heo 	{ "ST3160023AS",	"3.42",		ATA_HORKAGE_NONCQ },
40015ccfca97SLubomir Bulej 	{ "OCZ CORE_SSD",	"02.10104",	ATA_HORKAGE_NONCQ },
4002539cc7c7SJeff Garzik 
4003ac70a964STejun Heo 	/* Seagate NCQ + FLUSH CACHE firmware bug */
40044d1f9082SMark Lord 	{ "ST31500341AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
4005ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4006d10d491fSTejun Heo 
40074d1f9082SMark Lord 	{ "ST31000333AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
4008d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4009d10d491fSTejun Heo 
40104d1f9082SMark Lord 	{ "ST3640[36]23AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
4011d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4012d10d491fSTejun Heo 
40134d1f9082SMark Lord 	{ "ST3320[68]13AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
4014ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
4015ac70a964STejun Heo 
401631f6264eSHans de Goede 	/* drives which fail FPDMA_AA activation (some may freeze afterwards)
401731f6264eSHans de Goede 	   the ST disks also have LPM issues */
40188756a25bSHans de Goede 	{ "ST1000LM024 HN-M101MBB", NULL,	ATA_HORKAGE_BROKEN_FPDMA_AA |
4019ef1429c0SDamien Le Moal 						ATA_HORKAGE_NOLPM },
402008c85d2aSAleksei Mamlin 	{ "VB0250EAVER",	"HPG7",		ATA_HORKAGE_BROKEN_FPDMA_AA },
402187809942SMichele Baldessari 
402236e337d0SRobert Hancock 	/* Blacklist entries taken from Silicon Image 3124/3132
402336e337d0SRobert Hancock 	   Windows driver .inf file - also several Linux problem reports */
4024ef1429c0SDamien Le Moal 	{ "HTS541060G9SA00",    "MB3OC60D",     ATA_HORKAGE_NONCQ },
4025ef1429c0SDamien Le Moal 	{ "HTS541080G9SA00",    "MB4OC60D",     ATA_HORKAGE_NONCQ },
4026ef1429c0SDamien Le Moal 	{ "HTS541010G9SA00",    "MBZOC60D",     ATA_HORKAGE_NONCQ },
40276919a0a6SAlan Cox 
402868b0ddb2STejun Heo 	/* https://bugzilla.kernel.org/show_bug.cgi?id=15573 */
4029ef1429c0SDamien Le Moal 	{ "C300-CTFDDAC128MAG",	"0001",		ATA_HORKAGE_NONCQ },
403068b0ddb2STejun Heo 
40313b545563STejun Heo 	/* Sandisk SD7/8/9s lock up hard on large trims */
4032ef1429c0SDamien Le Moal 	{ "SanDisk SD[789]*",	NULL,		ATA_HORKAGE_MAX_TRIM_128M },
4033322579dcSTejun Heo 
403416c55b03STejun Heo 	/* devices which puke on READ_NATIVE_MAX */
4035ef1429c0SDamien Le Moal 	{ "HDS724040KLSA80",	"KFAOA20N",	ATA_HORKAGE_BROKEN_HPA },
403616c55b03STejun Heo 	{ "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
403716c55b03STejun Heo 	{ "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
403816c55b03STejun Heo 	{ "MAXTOR 6L080L4",	"A93.0500",	ATA_HORKAGE_BROKEN_HPA },
40396919a0a6SAlan Cox 
40407831387bSTejun Heo 	/* this one allows HPA unlocking but fails IOs on the area */
40417831387bSTejun Heo 	{ "OCZ-VERTEX",		    "1.30",	ATA_HORKAGE_BROKEN_HPA },
40427831387bSTejun Heo 
404393328e11SAlan Cox 	/* Devices which report 1 sector over size HPA */
4044ef1429c0SDamien Le Moal 	{ "ST340823A",		NULL,		ATA_HORKAGE_HPA_SIZE },
4045ef1429c0SDamien Le Moal 	{ "ST320413A",		NULL,		ATA_HORKAGE_HPA_SIZE },
4046ef1429c0SDamien Le Moal 	{ "ST310211A",		NULL,		ATA_HORKAGE_HPA_SIZE },
404793328e11SAlan Cox 
40486bbfd53dSAlan Cox 	/* Devices which get the IVB wrong */
4049ef1429c0SDamien Le Moal 	{ "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB },
4050a79067e5SAlan Cox 	/* Maybe we should just blacklist TSSTcorp... */
4051ef1429c0SDamien Le Moal 	{ "TSSTcorp CDDVDW SH-S202[HJN]", "SB0[01]",  ATA_HORKAGE_IVB },
40526bbfd53dSAlan Cox 
40539ce8e307SJens Axboe 	/* Devices that do not need bridging limits applied */
4054ef1429c0SDamien Le Moal 	{ "MTRON MSP-SATA*",		NULL,	ATA_HORKAGE_BRIDGE_OK },
4055ef1429c0SDamien Le Moal 	{ "BUFFALO HD-QSU2/R5",		NULL,	ATA_HORKAGE_BRIDGE_OK },
40569ce8e307SJens Axboe 
40579062712fSTejun Heo 	/* Devices which aren't very happy with higher link speeds */
4058ef1429c0SDamien Le Moal 	{ "WD My Book",			NULL,	ATA_HORKAGE_1_5_GBPS },
4059ef1429c0SDamien Le Moal 	{ "Seagate FreeAgent GoFlex",	NULL,	ATA_HORKAGE_1_5_GBPS },
40609062712fSTejun Heo 
4061d0cb43b3STejun Heo 	/*
4062d0cb43b3STejun Heo 	 * Devices which choke on SETXFER.  Applies only if both the
4063d0cb43b3STejun Heo 	 * device and controller are SATA.
4064d0cb43b3STejun Heo 	 */
4065cd691876STejun Heo 	{ "PIONEER DVD-RW  DVRTD08",	NULL,	ATA_HORKAGE_NOSETXFER },
40663a25179eSVladimir LAVALLADE 	{ "PIONEER DVD-RW  DVRTD08A",	NULL,	ATA_HORKAGE_NOSETXFER },
40673a25179eSVladimir LAVALLADE 	{ "PIONEER DVD-RW  DVR-215",	NULL,	ATA_HORKAGE_NOSETXFER },
4068cd691876STejun Heo 	{ "PIONEER DVD-RW  DVR-212D",	NULL,	ATA_HORKAGE_NOSETXFER },
4069cd691876STejun Heo 	{ "PIONEER DVD-RW  DVR-216D",	NULL,	ATA_HORKAGE_NOSETXFER },
4070d0cb43b3STejun Heo 
4071ea08aec7SNiklas Cassel 	/* These specific Pioneer models have LPM issues */
4072ea08aec7SNiklas Cassel 	{ "PIONEER BD-RW   BDR-207M",	NULL,	ATA_HORKAGE_NOLPM },
4073ea08aec7SNiklas Cassel 	{ "PIONEER BD-RW   BDR-205",	NULL,	ATA_HORKAGE_NOLPM },
4074ea08aec7SNiklas Cassel 
4075b17e5729SKai-Heng Feng 	/* Crucial BX100 SSD 500GB has broken LPM support */
40763bf7b5d6SHans de Goede 	{ "CT500BX100SSD1",		NULL,	ATA_HORKAGE_NOLPM },
4077b17e5729SKai-Heng Feng 
4078d418ff56SHans de Goede 	/* 512GB MX100 with MU01 firmware has both queued TRIM and LPM issues */
4079d418ff56SHans de Goede 	{ "Crucial_CT512MX100*",	"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
40809c7be59fSHans de Goede 						ATA_HORKAGE_ZERO_AFTER_TRIM |
4081ef1429c0SDamien Le Moal 						ATA_HORKAGE_NOLPM },
4082d418ff56SHans de Goede 	/* 512GB MX100 with newer firmware has only LPM issues */
4083d418ff56SHans de Goede 	{ "Crucial_CT512MX100*",	NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM |
4084ef1429c0SDamien Le Moal 						ATA_HORKAGE_NOLPM },
40859c7be59fSHans de Goede 
408662ac3f73SHans de Goede 	/* 480GB+ M500 SSDs have both queued TRIM and LPM issues */
408762ac3f73SHans de Goede 	{ "Crucial_CT480M500*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
408862ac3f73SHans de Goede 						ATA_HORKAGE_ZERO_AFTER_TRIM |
4089ef1429c0SDamien Le Moal 						ATA_HORKAGE_NOLPM },
409062ac3f73SHans de Goede 	{ "Crucial_CT960M500*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
409162ac3f73SHans de Goede 						ATA_HORKAGE_ZERO_AFTER_TRIM |
4092ef1429c0SDamien Le Moal 						ATA_HORKAGE_NOLPM },
409362ac3f73SHans de Goede 
409476936e9aSFrançois Cami 	/* These specific Samsung models/firmware-revs do not handle LPM well */
4095ef1429c0SDamien Le Moal 	{ "SAMSUNG MZMPC128HBFU-000MV", "CXM14M1Q", ATA_HORKAGE_NOLPM },
4096ef1429c0SDamien Le Moal 	{ "SAMSUNG SSD PM830 mSATA *",  "CXM13D1Q", ATA_HORKAGE_NOLPM },
4097ef1429c0SDamien Le Moal 	{ "SAMSUNG MZ7TD256HAFV-000L9", NULL,       ATA_HORKAGE_NOLPM },
4098ef1429c0SDamien Le Moal 	{ "SAMSUNG MZ7TE512HMHP-000L1", "EXT06L0Q", ATA_HORKAGE_NOLPM },
4099b5b4d3a5SHans de Goede 
4100f78dea06SMarc Carino 	/* devices that don't properly handle queued TRIM commands */
4101136d769eSSudip Mukherjee 	{ "Micron_M500IT_*",		"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
4102ef1429c0SDamien Le Moal 						ATA_HORKAGE_ZERO_AFTER_TRIM },
4103243918beSMartin K. Petersen 	{ "Micron_M500_*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
4104ef1429c0SDamien Le Moal 						ATA_HORKAGE_ZERO_AFTER_TRIM },
4105ff7f53fbSMartin K. Petersen 	{ "Crucial_CT*M500*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
4106ef1429c0SDamien Le Moal 						ATA_HORKAGE_ZERO_AFTER_TRIM },
41079051bd39SMartin K. Petersen 	{ "Micron_M5[15]0_*",		"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
4108ef1429c0SDamien Le Moal 						ATA_HORKAGE_ZERO_AFTER_TRIM },
4109ff7f53fbSMartin K. Petersen 	{ "Crucial_CT*M550*",		"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
4110ef1429c0SDamien Le Moal 						ATA_HORKAGE_ZERO_AFTER_TRIM },
4111ff7f53fbSMartin K. Petersen 	{ "Crucial_CT*MX100*",		"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
4112ef1429c0SDamien Le Moal 						ATA_HORKAGE_ZERO_AFTER_TRIM },
411353997522SChristian Lamparter 	{ "Samsung SSD 840 EVO*",	NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
411453997522SChristian Lamparter 						ATA_HORKAGE_NO_DMA_LOG |
4115ef1429c0SDamien Le Moal 						ATA_HORKAGE_ZERO_AFTER_TRIM },
4116ca6bfcb2SJu Hyung Park 	{ "Samsung SSD 840*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
4117ef1429c0SDamien Le Moal 						ATA_HORKAGE_ZERO_AFTER_TRIM },
4118ca6bfcb2SJu Hyung Park 	{ "Samsung SSD 850*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
4119ef1429c0SDamien Le Moal 						ATA_HORKAGE_ZERO_AFTER_TRIM },
41208a6430abSHans de Goede 	{ "Samsung SSD 860*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
41217a8526a5SKate Hsuan 						ATA_HORKAGE_ZERO_AFTER_TRIM |
4122ef1429c0SDamien Le Moal 						ATA_HORKAGE_NO_NCQ_ON_ATI },
41238a6430abSHans de Goede 	{ "Samsung SSD 870*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
41247a8526a5SKate Hsuan 						ATA_HORKAGE_ZERO_AFTER_TRIM |
4125ef1429c0SDamien Le Moal 						ATA_HORKAGE_NO_NCQ_ON_ATI },
4126ead08957SPatrick McLean 	{ "SAMSUNG*MZ7LH*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
4127ead08957SPatrick McLean 						ATA_HORKAGE_ZERO_AFTER_TRIM |
4128ead08957SPatrick McLean 						ATA_HORKAGE_NO_NCQ_ON_ATI, },
41297a7184b0SGuillermo A. Amaral 	{ "FCCT*M500*",			NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
4130ef1429c0SDamien Le Moal 						ATA_HORKAGE_ZERO_AFTER_TRIM },
4131e61f7d1cSMartin K. Petersen 
4132cda57b1bSArne Fitzenreiter 	/* devices that don't properly handle TRIM commands */
4133ef1429c0SDamien Le Moal 	{ "SuperSSpeed S238*",		NULL,	ATA_HORKAGE_NOTRIM },
4134ef1429c0SDamien Le Moal 	{ "M88V29*",			NULL,	ATA_HORKAGE_NOTRIM },
4135cda57b1bSArne Fitzenreiter 
4136e61f7d1cSMartin K. Petersen 	/*
4137e61f7d1cSMartin K. Petersen 	 * As defined, the DRAT (Deterministic Read After Trim) and RZAT
4138e61f7d1cSMartin K. Petersen 	 * (Return Zero After Trim) flags in the ATA Command Set are
4139e61f7d1cSMartin K. Petersen 	 * unreliable in the sense that they only define what happens if
4140e61f7d1cSMartin K. Petersen 	 * the device successfully executed the DSM TRIM command. TRIM
4141e61f7d1cSMartin K. Petersen 	 * is only advisory, however, and the device is free to silently
4142e61f7d1cSMartin K. Petersen 	 * ignore all or parts of the request.
4143e61f7d1cSMartin K. Petersen 	 *
4144e61f7d1cSMartin K. Petersen 	 * Whitelist drives that are known to reliably return zeroes
4145e61f7d1cSMartin K. Petersen 	 * after TRIM.
4146e61f7d1cSMartin K. Petersen 	 */
4147e61f7d1cSMartin K. Petersen 
4148e61f7d1cSMartin K. Petersen 	/*
4149e61f7d1cSMartin K. Petersen 	 * The intel 510 drive has buggy DRAT/RZAT. Explicitly exclude
4150e61f7d1cSMartin K. Petersen 	 * that model before whitelisting all other intel SSDs.
4151e61f7d1cSMartin K. Petersen 	 */
4152ef1429c0SDamien Le Moal 	{ "INTEL*SSDSC2MH*",		NULL,	0 },
4153e61f7d1cSMartin K. Petersen 
4154ef1429c0SDamien Le Moal 	{ "Micron*",			NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM },
4155ef1429c0SDamien Le Moal 	{ "Crucial*",			NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM },
4156ef1429c0SDamien Le Moal 	{ "INTEL*SSD*", 		NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM },
4157ef1429c0SDamien Le Moal 	{ "SSD*INTEL*",			NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM },
4158ef1429c0SDamien Le Moal 	{ "Samsung*SSD*",		NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM },
4159ef1429c0SDamien Le Moal 	{ "SAMSUNG*SSD*",		NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM },
4160ef1429c0SDamien Le Moal 	{ "SAMSUNG*MZ7KM*",		NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM },
4161ef1429c0SDamien Le Moal 	{ "ST[1248][0248]0[FH]*",	NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM },
4162f78dea06SMarc Carino 
4163ecd75ad5STejun Heo 	/*
4164ecd75ad5STejun Heo 	 * Some WD SATA-I drives spin up and down erratically when the link
4165ecd75ad5STejun Heo 	 * is put into the slumber mode.  We don't have full list of the
4166ecd75ad5STejun Heo 	 * affected devices.  Disable LPM if the device matches one of the
4167ecd75ad5STejun Heo 	 * known prefixes and is SATA-1.  As a side effect LPM partial is
4168ecd75ad5STejun Heo 	 * lost too.
4169ecd75ad5STejun Heo 	 *
4170ecd75ad5STejun Heo 	 * https://bugzilla.kernel.org/show_bug.cgi?id=57211
4171ecd75ad5STejun Heo 	 */
4172ecd75ad5STejun Heo 	{ "WDC WD800JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4173ecd75ad5STejun Heo 	{ "WDC WD1200JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4174ecd75ad5STejun Heo 	{ "WDC WD1600JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4175ecd75ad5STejun Heo 	{ "WDC WD2000JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4176ecd75ad5STejun Heo 	{ "WDC WD2500JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4177ecd75ad5STejun Heo 	{ "WDC WD3000JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4178ecd75ad5STejun Heo 	{ "WDC WD3200JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4179ecd75ad5STejun Heo 
4180ac9f0c81SAnton Lundin 	/*
4181ac9f0c81SAnton Lundin 	 * This sata dom device goes on a walkabout when the ATA_LOG_DIRECTORY
4182ac9f0c81SAnton Lundin 	 * log page is accessed. Ensure we never ask for this log page with
4183ac9f0c81SAnton Lundin 	 * these devices.
4184ac9f0c81SAnton Lundin 	 */
4185ac9f0c81SAnton Lundin 	{ "SATADOM-ML 3ME",		NULL,	ATA_HORKAGE_NO_LOG_DIR },
4186ac9f0c81SAnton Lundin 
41874d2e4980SDamien Le Moal 	/* Buggy FUA */
41884d2e4980SDamien Le Moal 	{ "Maxtor",		"BANC1G10",	ATA_HORKAGE_NO_FUA },
418938d43122SDamien Le Moal 	{ "WDC*WD2500J*",	NULL,		ATA_HORKAGE_NO_FUA },
419038d43122SDamien Le Moal 	{ "OCZ-VERTEX*",	NULL,		ATA_HORKAGE_NO_FUA },
419138d43122SDamien Le Moal 	{ "INTEL*SSDSC2CT*",	NULL,		ATA_HORKAGE_NO_FUA },
41924d2e4980SDamien Le Moal 
41936919a0a6SAlan Cox 	/* End Marker */
41946919a0a6SAlan Cox 	{ }
4195c6fd2807SJeff Garzik };
4196c6fd2807SJeff Garzik 
419775683fe7STejun Heo static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
4198c6fd2807SJeff Garzik {
41998bfa79fcSTejun Heo 	unsigned char model_num[ATA_ID_PROD_LEN + 1];
42008bfa79fcSTejun Heo 	unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
42016919a0a6SAlan Cox 	const struct ata_blacklist_entry *ad = ata_device_blacklist;
4202c6fd2807SJeff Garzik 
42038bfa79fcSTejun Heo 	ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
42048bfa79fcSTejun Heo 	ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
4205c6fd2807SJeff Garzik 
42066919a0a6SAlan Cox 	while (ad->model_num) {
42071c402799SGeorge Spelvin 		if (glob_match(ad->model_num, model_num)) {
42086919a0a6SAlan Cox 			if (ad->model_rev == NULL)
42096919a0a6SAlan Cox 				return ad->horkage;
42101c402799SGeorge Spelvin 			if (glob_match(ad->model_rev, model_rev))
42116919a0a6SAlan Cox 				return ad->horkage;
4212c6fd2807SJeff Garzik 		}
42136919a0a6SAlan Cox 		ad++;
4214c6fd2807SJeff Garzik 	}
4215c6fd2807SJeff Garzik 	return 0;
4216c6fd2807SJeff Garzik }
4217c6fd2807SJeff Garzik 
42186919a0a6SAlan Cox static int ata_dma_blacklisted(const struct ata_device *dev)
42196919a0a6SAlan Cox {
42206919a0a6SAlan Cox 	/* We don't support polling DMA.
42216919a0a6SAlan Cox 	 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
42226919a0a6SAlan Cox 	 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
42236919a0a6SAlan Cox 	 */
42249af5c9c9STejun Heo 	if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
42256919a0a6SAlan Cox 	    (dev->flags & ATA_DFLAG_CDB_INTR))
42266919a0a6SAlan Cox 		return 1;
422775683fe7STejun Heo 	return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
42286919a0a6SAlan Cox }
42296919a0a6SAlan Cox 
4230c6fd2807SJeff Garzik /**
42316bbfd53dSAlan Cox  *	ata_is_40wire		-	check drive side detection
42326bbfd53dSAlan Cox  *	@dev: device
42336bbfd53dSAlan Cox  *
42346bbfd53dSAlan Cox  *	Perform drive side detection decoding, allowing for device vendors
42356bbfd53dSAlan Cox  *	who can't follow the documentation.
42366bbfd53dSAlan Cox  */
42376bbfd53dSAlan Cox 
42386bbfd53dSAlan Cox static int ata_is_40wire(struct ata_device *dev)
42396bbfd53dSAlan Cox {
42406bbfd53dSAlan Cox 	if (dev->horkage & ATA_HORKAGE_IVB)
42416bbfd53dSAlan Cox 		return ata_drive_40wire_relaxed(dev->id);
42426bbfd53dSAlan Cox 	return ata_drive_40wire(dev->id);
42436bbfd53dSAlan Cox }
42446bbfd53dSAlan Cox 
42456bbfd53dSAlan Cox /**
424615a5551cSAlan Cox  *	cable_is_40wire		-	40/80/SATA decider
424715a5551cSAlan Cox  *	@ap: port to consider
424815a5551cSAlan Cox  *
424915a5551cSAlan Cox  *	This function encapsulates the policy for speed management
425015a5551cSAlan Cox  *	in one place. At the moment we don't cache the result but
425115a5551cSAlan Cox  *	there is a good case for setting ap->cbl to the result when
425215a5551cSAlan Cox  *	we are called with unknown cables (and figuring out if it
425315a5551cSAlan Cox  *	impacts hotplug at all).
425415a5551cSAlan Cox  *
425515a5551cSAlan Cox  *	Return 1 if the cable appears to be 40 wire.
425615a5551cSAlan Cox  */
425715a5551cSAlan Cox 
425815a5551cSAlan Cox static int cable_is_40wire(struct ata_port *ap)
425915a5551cSAlan Cox {
426015a5551cSAlan Cox 	struct ata_link *link;
426115a5551cSAlan Cox 	struct ata_device *dev;
426215a5551cSAlan Cox 
42634a9c7b33STejun Heo 	/* If the controller thinks we are 40 wire, we are. */
426415a5551cSAlan Cox 	if (ap->cbl == ATA_CBL_PATA40)
426515a5551cSAlan Cox 		return 1;
42664a9c7b33STejun Heo 
42674a9c7b33STejun Heo 	/* If the controller thinks we are 80 wire, we are. */
426815a5551cSAlan Cox 	if (ap->cbl == ATA_CBL_PATA80 || ap->cbl == ATA_CBL_SATA)
426915a5551cSAlan Cox 		return 0;
42704a9c7b33STejun Heo 
42714a9c7b33STejun Heo 	/* If the system is known to be 40 wire short cable (eg
42724a9c7b33STejun Heo 	 * laptop), then we allow 80 wire modes even if the drive
42734a9c7b33STejun Heo 	 * isn't sure.
42744a9c7b33STejun Heo 	 */
4275f792068eSAlan Cox 	if (ap->cbl == ATA_CBL_PATA40_SHORT)
4276f792068eSAlan Cox 		return 0;
427715a5551cSAlan Cox 
42784a9c7b33STejun Heo 	/* If the controller doesn't know, we scan.
42794a9c7b33STejun Heo 	 *
42804a9c7b33STejun Heo 	 * Note: We look for all 40 wire detects at this point.  Any
42814a9c7b33STejun Heo 	 *       80 wire detect is taken to be 80 wire cable because
42824a9c7b33STejun Heo 	 * - in many setups only the one drive (slave if present) will
42834a9c7b33STejun Heo 	 *   give a valid detect
42844a9c7b33STejun Heo 	 * - if you have a non detect capable drive you don't want it
42854a9c7b33STejun Heo 	 *   to colour the choice
428615a5551cSAlan Cox 	 */
42871eca4365STejun Heo 	ata_for_each_link(link, ap, EDGE) {
42881eca4365STejun Heo 		ata_for_each_dev(dev, link, ENABLED) {
42891eca4365STejun Heo 			if (!ata_is_40wire(dev))
429015a5551cSAlan Cox 				return 0;
429115a5551cSAlan Cox 		}
429215a5551cSAlan Cox 	}
429315a5551cSAlan Cox 	return 1;
429415a5551cSAlan Cox }
429515a5551cSAlan Cox 
429615a5551cSAlan Cox /**
4297c6fd2807SJeff Garzik  *	ata_dev_xfermask - Compute supported xfermask of the given device
4298c6fd2807SJeff Garzik  *	@dev: Device to compute xfermask for
4299c6fd2807SJeff Garzik  *
4300c6fd2807SJeff Garzik  *	Compute supported xfermask of @dev and store it in
4301c6fd2807SJeff Garzik  *	dev->*_mask.  This function is responsible for applying all
4302c6fd2807SJeff Garzik  *	known limits including host controller limits, device
4303c6fd2807SJeff Garzik  *	blacklist, etc...
4304c6fd2807SJeff Garzik  *
4305c6fd2807SJeff Garzik  *	LOCKING:
4306c6fd2807SJeff Garzik  *	None.
4307c6fd2807SJeff Garzik  */
4308c6fd2807SJeff Garzik static void ata_dev_xfermask(struct ata_device *dev)
4309c6fd2807SJeff Garzik {
43109af5c9c9STejun Heo 	struct ata_link *link = dev->link;
43119af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
4312cca3974eSJeff Garzik 	struct ata_host *host = ap->host;
4313f0a6d77bSSergey Shtylyov 	unsigned int xfer_mask;
4314c6fd2807SJeff Garzik 
4315c6fd2807SJeff Garzik 	/* controller modes available */
4316c6fd2807SJeff Garzik 	xfer_mask = ata_pack_xfermask(ap->pio_mask,
4317c6fd2807SJeff Garzik 				      ap->mwdma_mask, ap->udma_mask);
4318c6fd2807SJeff Garzik 
43198343f889SRobert Hancock 	/* drive modes available */
4320c6fd2807SJeff Garzik 	xfer_mask &= ata_pack_xfermask(dev->pio_mask,
4321c6fd2807SJeff Garzik 				       dev->mwdma_mask, dev->udma_mask);
4322c6fd2807SJeff Garzik 	xfer_mask &= ata_id_xfermask(dev->id);
4323c6fd2807SJeff Garzik 
4324b352e57dSAlan Cox 	/*
4325b352e57dSAlan Cox 	 *	CFA Advanced TrueIDE timings are not allowed on a shared
4326b352e57dSAlan Cox 	 *	cable
4327b352e57dSAlan Cox 	 */
4328b352e57dSAlan Cox 	if (ata_dev_pair(dev)) {
4329b352e57dSAlan Cox 		/* No PIO5 or PIO6 */
4330b352e57dSAlan Cox 		xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
4331b352e57dSAlan Cox 		/* No MWDMA3 or MWDMA 4 */
4332b352e57dSAlan Cox 		xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
4333b352e57dSAlan Cox 	}
4334b352e57dSAlan Cox 
4335c6fd2807SJeff Garzik 	if (ata_dma_blacklisted(dev)) {
4336c6fd2807SJeff Garzik 		xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4337a9a79dfeSJoe Perches 		ata_dev_warn(dev,
4338c6fd2807SJeff Garzik 			     "device is on DMA blacklist, disabling DMA\n");
4339c6fd2807SJeff Garzik 	}
4340c6fd2807SJeff Garzik 
434114d66ab7SPetr Vandrovec 	if ((host->flags & ATA_HOST_SIMPLEX) &&
434214d66ab7SPetr Vandrovec 	    host->simplex_claimed && host->simplex_claimed != ap) {
4343c6fd2807SJeff Garzik 		xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4344a9a79dfeSJoe Perches 		ata_dev_warn(dev,
4345a9a79dfeSJoe Perches 			     "simplex DMA is claimed by other device, disabling DMA\n");
4346c6fd2807SJeff Garzik 	}
4347c6fd2807SJeff Garzik 
4348e424675fSJeff Garzik 	if (ap->flags & ATA_FLAG_NO_IORDY)
4349e424675fSJeff Garzik 		xfer_mask &= ata_pio_mask_no_iordy(dev);
4350e424675fSJeff Garzik 
4351c6fd2807SJeff Garzik 	if (ap->ops->mode_filter)
4352a76b62caSAlan Cox 		xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
4353c6fd2807SJeff Garzik 
43548343f889SRobert Hancock 	/* Apply cable rule here.  Don't apply it early because when
43558343f889SRobert Hancock 	 * we handle hot plug the cable type can itself change.
43568343f889SRobert Hancock 	 * Check this last so that we know if the transfer rate was
43578343f889SRobert Hancock 	 * solely limited by the cable.
43588343f889SRobert Hancock 	 * Unknown or 80 wire cables reported host side are checked
43598343f889SRobert Hancock 	 * drive side as well. Cases where we know a 40wire cable
43608343f889SRobert Hancock 	 * is used safely for 80 are not checked here.
43618343f889SRobert Hancock 	 */
43628343f889SRobert Hancock 	if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
43638343f889SRobert Hancock 		/* UDMA/44 or higher would be available */
436415a5551cSAlan Cox 		if (cable_is_40wire(ap)) {
4365a9a79dfeSJoe Perches 			ata_dev_warn(dev,
43668343f889SRobert Hancock 				     "limited to UDMA/33 due to 40-wire cable\n");
43678343f889SRobert Hancock 			xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
43688343f889SRobert Hancock 		}
43698343f889SRobert Hancock 
4370c6fd2807SJeff Garzik 	ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
4371c6fd2807SJeff Garzik 			    &dev->mwdma_mask, &dev->udma_mask);
4372c6fd2807SJeff Garzik }
4373c6fd2807SJeff Garzik 
4374c6fd2807SJeff Garzik /**
4375c6fd2807SJeff Garzik  *	ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
4376c6fd2807SJeff Garzik  *	@dev: Device to which command will be sent
4377c6fd2807SJeff Garzik  *
4378c6fd2807SJeff Garzik  *	Issue SET FEATURES - XFER MODE command to device @dev
4379c6fd2807SJeff Garzik  *	on port @ap.
4380c6fd2807SJeff Garzik  *
4381c6fd2807SJeff Garzik  *	LOCKING:
4382c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
4383c6fd2807SJeff Garzik  *
4384c6fd2807SJeff Garzik  *	RETURNS:
4385c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
4386c6fd2807SJeff Garzik  */
4387c6fd2807SJeff Garzik 
4388c6fd2807SJeff Garzik static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
4389c6fd2807SJeff Garzik {
4390c6fd2807SJeff Garzik 	struct ata_taskfile tf;
4391c6fd2807SJeff Garzik 
4392c6fd2807SJeff Garzik 	/* set up set-features taskfile */
43934633778bSHannes Reinecke 	ata_dev_dbg(dev, "set features - xfer mode\n");
4394c6fd2807SJeff Garzik 
4395464cf177STejun Heo 	/* Some controllers and ATAPI devices show flaky interrupt
4396464cf177STejun Heo 	 * behavior after setting xfer mode.  Use polling instead.
4397464cf177STejun Heo 	 */
4398c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
4399c6fd2807SJeff Garzik 	tf.command = ATA_CMD_SET_FEATURES;
4400c6fd2807SJeff Garzik 	tf.feature = SETFEATURES_XFER;
4401464cf177STejun Heo 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
4402c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
4403b9f8ab2dSAlan Cox 	/* If we are using IORDY we must send the mode setting command */
4404b9f8ab2dSAlan Cox 	if (ata_pio_need_iordy(dev))
4405c6fd2807SJeff Garzik 		tf.nsect = dev->xfer_mode;
4406b9f8ab2dSAlan Cox 	/* If the device has IORDY and the controller does not - turn it off */
4407b9f8ab2dSAlan Cox  	else if (ata_id_has_iordy(dev->id))
4408b9f8ab2dSAlan Cox 		tf.nsect = 0x01;
4409b9f8ab2dSAlan Cox 	else /* In the ancient relic department - skip all of this */
4410b9f8ab2dSAlan Cox 		return 0;
4411c6fd2807SJeff Garzik 
4412024811a2SDamien Le Moal 	/*
4413024811a2SDamien Le Moal 	 * On some disks, this command causes spin-up, so we need longer
4414024811a2SDamien Le Moal 	 * timeout.
4415024811a2SDamien Le Moal 	 */
4416024811a2SDamien Le Moal 	return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 15000);
4417c6fd2807SJeff Garzik }
44181152b261STejun Heo 
4419c6fd2807SJeff Garzik /**
442099ad3f9fSNiklas Cassel  *	ata_dev_set_feature - Issue SET FEATURES
44219f45cbd3SKristen Carlson Accardi  *	@dev: Device to which command will be sent
442299ad3f9fSNiklas Cassel  *	@subcmd: The SET FEATURES subcommand to be sent
442399ad3f9fSNiklas Cassel  *	@action: The sector count represents a subcommand specific action
44249f45cbd3SKristen Carlson Accardi  *
442599ad3f9fSNiklas Cassel  *	Issue SET FEATURES command to device @dev on port @ap with sector count
44269f45cbd3SKristen Carlson Accardi  *
44279f45cbd3SKristen Carlson Accardi  *	LOCKING:
44289f45cbd3SKristen Carlson Accardi  *	PCI/etc. bus probe sem.
44299f45cbd3SKristen Carlson Accardi  *
44309f45cbd3SKristen Carlson Accardi  *	RETURNS:
44319f45cbd3SKristen Carlson Accardi  *	0 on success, AC_ERR_* mask otherwise.
44329f45cbd3SKristen Carlson Accardi  */
443399ad3f9fSNiklas Cassel unsigned int ata_dev_set_feature(struct ata_device *dev, u8 subcmd, u8 action)
44349f45cbd3SKristen Carlson Accardi {
44359f45cbd3SKristen Carlson Accardi 	struct ata_taskfile tf;
443661176eedSSergey Shtylyov 	unsigned int timeout = 0;
44379f45cbd3SKristen Carlson Accardi 
44389f45cbd3SKristen Carlson Accardi 	/* set up set-features taskfile */
443999ad3f9fSNiklas Cassel 	ata_dev_dbg(dev, "set features\n");
44409f45cbd3SKristen Carlson Accardi 
44419f45cbd3SKristen Carlson Accardi 	ata_tf_init(dev, &tf);
44429f45cbd3SKristen Carlson Accardi 	tf.command = ATA_CMD_SET_FEATURES;
444399ad3f9fSNiklas Cassel 	tf.feature = subcmd;
44449f45cbd3SKristen Carlson Accardi 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
44459f45cbd3SKristen Carlson Accardi 	tf.protocol = ATA_PROT_NODATA;
444699ad3f9fSNiklas Cassel 	tf.nsect = action;
44479f45cbd3SKristen Carlson Accardi 
444899ad3f9fSNiklas Cassel 	if (subcmd == SETFEATURES_SPINUP)
4449974e0a45SDamien Le Moal 		timeout = ata_probe_timeout ?
4450974e0a45SDamien Le Moal 			  ata_probe_timeout * 1000 : SETFEATURES_SPINUP_TIMEOUT;
44519f45cbd3SKristen Carlson Accardi 
4452614065abSJinpeng Cui 	return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, timeout);
44539f45cbd3SKristen Carlson Accardi }
4454633de4ccSJeff Garzik EXPORT_SYMBOL_GPL(ata_dev_set_feature);
44559f45cbd3SKristen Carlson Accardi 
44569f45cbd3SKristen Carlson Accardi /**
4457c6fd2807SJeff Garzik  *	ata_dev_init_params - Issue INIT DEV PARAMS command
4458c6fd2807SJeff Garzik  *	@dev: Device to which command will be sent
4459c6fd2807SJeff Garzik  *	@heads: Number of heads (taskfile parameter)
4460c6fd2807SJeff Garzik  *	@sectors: Number of sectors (taskfile parameter)
4461c6fd2807SJeff Garzik  *
4462c6fd2807SJeff Garzik  *	LOCKING:
4463c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
4464c6fd2807SJeff Garzik  *
4465c6fd2807SJeff Garzik  *	RETURNS:
4466c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
4467c6fd2807SJeff Garzik  */
4468c6fd2807SJeff Garzik static unsigned int ata_dev_init_params(struct ata_device *dev,
4469c6fd2807SJeff Garzik 					u16 heads, u16 sectors)
4470c6fd2807SJeff Garzik {
4471c6fd2807SJeff Garzik 	struct ata_taskfile tf;
4472c6fd2807SJeff Garzik 	unsigned int err_mask;
4473c6fd2807SJeff Garzik 
4474c6fd2807SJeff Garzik 	/* Number of sectors per track 1-255. Number of heads 1-16 */
4475c6fd2807SJeff Garzik 	if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
4476c6fd2807SJeff Garzik 		return AC_ERR_INVALID;
4477c6fd2807SJeff Garzik 
4478c6fd2807SJeff Garzik 	/* set up init dev params taskfile */
44794633778bSHannes Reinecke 	ata_dev_dbg(dev, "init dev params \n");
4480c6fd2807SJeff Garzik 
4481c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
4482c6fd2807SJeff Garzik 	tf.command = ATA_CMD_INIT_DEV_PARAMS;
4483c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4484c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
4485c6fd2807SJeff Garzik 	tf.nsect = sectors;
4486c6fd2807SJeff Garzik 	tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
4487c6fd2807SJeff Garzik 
44882b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
448918b2466cSAlan Cox 	/* A clean abort indicates an original or just out of spec drive
449018b2466cSAlan Cox 	   and we should continue as we issue the setup based on the
449118b2466cSAlan Cox 	   drive reported working geometry */
4492efcef265SSergey Shtylyov 	if (err_mask == AC_ERR_DEV && (tf.error & ATA_ABORTED))
449318b2466cSAlan Cox 		err_mask = 0;
4494c6fd2807SJeff Garzik 
4495c6fd2807SJeff Garzik 	return err_mask;
4496c6fd2807SJeff Garzik }
4497c6fd2807SJeff Garzik 
4498c6fd2807SJeff Garzik /**
44995895ef9aSTejun Heo  *	atapi_check_dma - Check whether ATAPI DMA can be supported
4500c6fd2807SJeff Garzik  *	@qc: Metadata associated with taskfile to check
4501c6fd2807SJeff Garzik  *
4502c6fd2807SJeff Garzik  *	Allow low-level driver to filter ATA PACKET commands, returning
4503c6fd2807SJeff Garzik  *	a status indicating whether or not it is OK to use DMA for the
4504c6fd2807SJeff Garzik  *	supplied PACKET command.
4505c6fd2807SJeff Garzik  *
4506c6fd2807SJeff Garzik  *	LOCKING:
4507cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4508c6fd2807SJeff Garzik  *
4509c6fd2807SJeff Garzik  *	RETURNS: 0 when ATAPI DMA can be used
4510c6fd2807SJeff Garzik  *               nonzero otherwise
4511c6fd2807SJeff Garzik  */
45125895ef9aSTejun Heo int atapi_check_dma(struct ata_queued_cmd *qc)
4513c6fd2807SJeff Garzik {
4514c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4515c6fd2807SJeff Garzik 
4516b9a4197eSTejun Heo 	/* Don't allow DMA if it isn't multiple of 16 bytes.  Quite a
4517b9a4197eSTejun Heo 	 * few ATAPI devices choke on such DMA requests.
4518b9a4197eSTejun Heo 	 */
45196a87e42eSTejun Heo 	if (!(qc->dev->horkage & ATA_HORKAGE_ATAPI_MOD16_DMA) &&
45206a87e42eSTejun Heo 	    unlikely(qc->nbytes & 15))
45216f23a31dSAlbert Lee 		return 1;
45226f23a31dSAlbert Lee 
4523c6fd2807SJeff Garzik 	if (ap->ops->check_atapi_dma)
4524b9a4197eSTejun Heo 		return ap->ops->check_atapi_dma(qc);
4525c6fd2807SJeff Garzik 
4526b9a4197eSTejun Heo 	return 0;
4527c6fd2807SJeff Garzik }
4528b9a4197eSTejun Heo 
4529c6fd2807SJeff Garzik /**
453031cc23b3STejun Heo  *	ata_std_qc_defer - Check whether a qc needs to be deferred
453131cc23b3STejun Heo  *	@qc: ATA command in question
453231cc23b3STejun Heo  *
453331cc23b3STejun Heo  *	Non-NCQ commands cannot run with any other command, NCQ or
453431cc23b3STejun Heo  *	not.  As upper layer only knows the queue depth, we are
453531cc23b3STejun Heo  *	responsible for maintaining exclusion.  This function checks
453631cc23b3STejun Heo  *	whether a new command @qc can be issued.
453731cc23b3STejun Heo  *
453831cc23b3STejun Heo  *	LOCKING:
453931cc23b3STejun Heo  *	spin_lock_irqsave(host lock)
454031cc23b3STejun Heo  *
454131cc23b3STejun Heo  *	RETURNS:
454231cc23b3STejun Heo  *	ATA_DEFER_* if deferring is needed, 0 otherwise.
454331cc23b3STejun Heo  */
454431cc23b3STejun Heo int ata_std_qc_defer(struct ata_queued_cmd *qc)
454531cc23b3STejun Heo {
454631cc23b3STejun Heo 	struct ata_link *link = qc->dev->link;
454731cc23b3STejun Heo 
4548179b310aSHannes Reinecke 	if (ata_is_ncq(qc->tf.protocol)) {
454931cc23b3STejun Heo 		if (!ata_tag_valid(link->active_tag))
455031cc23b3STejun Heo 			return 0;
455131cc23b3STejun Heo 	} else {
455231cc23b3STejun Heo 		if (!ata_tag_valid(link->active_tag) && !link->sactive)
455331cc23b3STejun Heo 			return 0;
455431cc23b3STejun Heo 	}
455531cc23b3STejun Heo 
455631cc23b3STejun Heo 	return ATA_DEFER_LINK;
455731cc23b3STejun Heo }
4558a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_std_qc_defer);
455931cc23b3STejun Heo 
456095364f36SJiri Slaby enum ata_completion_errors ata_noop_qc_prep(struct ata_queued_cmd *qc)
456195364f36SJiri Slaby {
456295364f36SJiri Slaby 	return AC_ERR_OK;
456395364f36SJiri Slaby }
4564a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
4565c6fd2807SJeff Garzik 
4566c6fd2807SJeff Garzik /**
4567c6fd2807SJeff Garzik  *	ata_sg_init - Associate command with scatter-gather table.
4568c6fd2807SJeff Garzik  *	@qc: Command to be associated
4569c6fd2807SJeff Garzik  *	@sg: Scatter-gather table.
4570c6fd2807SJeff Garzik  *	@n_elem: Number of elements in s/g table.
4571c6fd2807SJeff Garzik  *
4572c6fd2807SJeff Garzik  *	Initialize the data-related elements of queued_cmd @qc
4573c6fd2807SJeff Garzik  *	to point to a scatter-gather table @sg, containing @n_elem
4574c6fd2807SJeff Garzik  *	elements.
4575c6fd2807SJeff Garzik  *
4576c6fd2807SJeff Garzik  *	LOCKING:
4577cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4578c6fd2807SJeff Garzik  */
4579c6fd2807SJeff Garzik void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
4580c6fd2807SJeff Garzik 		 unsigned int n_elem)
4581c6fd2807SJeff Garzik {
4582ff2aeb1eSTejun Heo 	qc->sg = sg;
4583c6fd2807SJeff Garzik 	qc->n_elem = n_elem;
4584ff2aeb1eSTejun Heo 	qc->cursg = qc->sg;
4585ff2aeb1eSTejun Heo }
4586ff2aeb1eSTejun Heo 
45872874d5eeSGeert Uytterhoeven #ifdef CONFIG_HAS_DMA
45882874d5eeSGeert Uytterhoeven 
45892874d5eeSGeert Uytterhoeven /**
45902874d5eeSGeert Uytterhoeven  *	ata_sg_clean - Unmap DMA memory associated with command
45912874d5eeSGeert Uytterhoeven  *	@qc: Command containing DMA memory to be released
45922874d5eeSGeert Uytterhoeven  *
45932874d5eeSGeert Uytterhoeven  *	Unmap all mapped DMA memory associated with this command.
45942874d5eeSGeert Uytterhoeven  *
45952874d5eeSGeert Uytterhoeven  *	LOCKING:
45962874d5eeSGeert Uytterhoeven  *	spin_lock_irqsave(host lock)
45972874d5eeSGeert Uytterhoeven  */
4598af27e01cSJason Yan static void ata_sg_clean(struct ata_queued_cmd *qc)
45992874d5eeSGeert Uytterhoeven {
46002874d5eeSGeert Uytterhoeven 	struct ata_port *ap = qc->ap;
46012874d5eeSGeert Uytterhoeven 	struct scatterlist *sg = qc->sg;
46022874d5eeSGeert Uytterhoeven 	int dir = qc->dma_dir;
46032874d5eeSGeert Uytterhoeven 
46042874d5eeSGeert Uytterhoeven 	WARN_ON_ONCE(sg == NULL);
46052874d5eeSGeert Uytterhoeven 
46062874d5eeSGeert Uytterhoeven 	if (qc->n_elem)
46072874d5eeSGeert Uytterhoeven 		dma_unmap_sg(ap->dev, sg, qc->orig_n_elem, dir);
46082874d5eeSGeert Uytterhoeven 
46092874d5eeSGeert Uytterhoeven 	qc->flags &= ~ATA_QCFLAG_DMAMAP;
46102874d5eeSGeert Uytterhoeven 	qc->sg = NULL;
46112874d5eeSGeert Uytterhoeven }
46122874d5eeSGeert Uytterhoeven 
4613c6fd2807SJeff Garzik /**
4614c6fd2807SJeff Garzik  *	ata_sg_setup - DMA-map the scatter-gather table associated with a command.
4615c6fd2807SJeff Garzik  *	@qc: Command with scatter-gather table to be mapped.
4616c6fd2807SJeff Garzik  *
4617c6fd2807SJeff Garzik  *	DMA-map the scatter-gather table associated with queued_cmd @qc.
4618c6fd2807SJeff Garzik  *
4619c6fd2807SJeff Garzik  *	LOCKING:
4620cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4621c6fd2807SJeff Garzik  *
4622c6fd2807SJeff Garzik  *	RETURNS:
4623c6fd2807SJeff Garzik  *	Zero on success, negative on error.
4624c6fd2807SJeff Garzik  *
4625c6fd2807SJeff Garzik  */
4626c6fd2807SJeff Garzik static int ata_sg_setup(struct ata_queued_cmd *qc)
4627c6fd2807SJeff Garzik {
4628c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4629dde20207SJames Bottomley 	unsigned int n_elem;
4630c6fd2807SJeff Garzik 
4631dde20207SJames Bottomley 	n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir);
4632dde20207SJames Bottomley 	if (n_elem < 1)
4633c6fd2807SJeff Garzik 		return -1;
4634c6fd2807SJeff Garzik 
46355825627cSFUJITA Tomonori 	qc->orig_n_elem = qc->n_elem;
4636dde20207SJames Bottomley 	qc->n_elem = n_elem;
4637f92a2636STejun Heo 	qc->flags |= ATA_QCFLAG_DMAMAP;
4638c6fd2807SJeff Garzik 
4639c6fd2807SJeff Garzik 	return 0;
4640c6fd2807SJeff Garzik }
4641c6fd2807SJeff Garzik 
46422874d5eeSGeert Uytterhoeven #else /* !CONFIG_HAS_DMA */
46432874d5eeSGeert Uytterhoeven 
46442874d5eeSGeert Uytterhoeven static inline void ata_sg_clean(struct ata_queued_cmd *qc) {}
46452874d5eeSGeert Uytterhoeven static inline int ata_sg_setup(struct ata_queued_cmd *qc) { return -1; }
46462874d5eeSGeert Uytterhoeven 
46472874d5eeSGeert Uytterhoeven #endif /* !CONFIG_HAS_DMA */
46482874d5eeSGeert Uytterhoeven 
4649c6fd2807SJeff Garzik /**
4650c6fd2807SJeff Garzik  *	swap_buf_le16 - swap halves of 16-bit words in place
4651c6fd2807SJeff Garzik  *	@buf:  Buffer to swap
4652c6fd2807SJeff Garzik  *	@buf_words:  Number of 16-bit words in buffer.
4653c6fd2807SJeff Garzik  *
4654c6fd2807SJeff Garzik  *	Swap halves of 16-bit words if needed to convert from
4655c6fd2807SJeff Garzik  *	little-endian byte order to native cpu byte order, or
4656c6fd2807SJeff Garzik  *	vice-versa.
4657c6fd2807SJeff Garzik  *
4658c6fd2807SJeff Garzik  *	LOCKING:
4659c6fd2807SJeff Garzik  *	Inherited from caller.
4660c6fd2807SJeff Garzik  */
4661c6fd2807SJeff Garzik void swap_buf_le16(u16 *buf, unsigned int buf_words)
4662c6fd2807SJeff Garzik {
4663c6fd2807SJeff Garzik #ifdef __BIG_ENDIAN
4664c6fd2807SJeff Garzik 	unsigned int i;
4665c6fd2807SJeff Garzik 
4666c6fd2807SJeff Garzik 	for (i = 0; i < buf_words; i++)
4667c6fd2807SJeff Garzik 		buf[i] = le16_to_cpu(buf[i]);
4668c6fd2807SJeff Garzik #endif /* __BIG_ENDIAN */
4669c6fd2807SJeff Garzik }
4670c6fd2807SJeff Garzik 
4671c6fd2807SJeff Garzik /**
46728a8bc223STejun Heo  *	ata_qc_free - free unused ata_queued_cmd
46738a8bc223STejun Heo  *	@qc: Command to complete
46748a8bc223STejun Heo  *
46758a8bc223STejun Heo  *	Designed to free unused ata_queued_cmd object
46768a8bc223STejun Heo  *	in case something prevents using it.
46778a8bc223STejun Heo  *
46788a8bc223STejun Heo  *	LOCKING:
46798a8bc223STejun Heo  *	spin_lock_irqsave(host lock)
46808a8bc223STejun Heo  */
46818a8bc223STejun Heo void ata_qc_free(struct ata_queued_cmd *qc)
46828a8bc223STejun Heo {
46838a8bc223STejun Heo 	qc->flags = 0;
46844f1a22eeSJohn Garry 	if (ata_tag_valid(qc->tag))
46858a8bc223STejun Heo 		qc->tag = ATA_TAG_POISON;
46868a8bc223STejun Heo }
46878a8bc223STejun Heo 
4688c6fd2807SJeff Garzik void __ata_qc_complete(struct ata_queued_cmd *qc)
4689c6fd2807SJeff Garzik {
4690a1104016SJulia Lawall 	struct ata_port *ap;
4691a1104016SJulia Lawall 	struct ata_link *link;
4692c6fd2807SJeff Garzik 
4693efcb3cf7STejun Heo 	WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4694efcb3cf7STejun Heo 	WARN_ON_ONCE(!(qc->flags & ATA_QCFLAG_ACTIVE));
4695a1104016SJulia Lawall 	ap = qc->ap;
4696a1104016SJulia Lawall 	link = qc->dev->link;
4697c6fd2807SJeff Garzik 
4698c6fd2807SJeff Garzik 	if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
4699c6fd2807SJeff Garzik 		ata_sg_clean(qc);
4700c6fd2807SJeff Garzik 
4701c6fd2807SJeff Garzik 	/* command should be marked inactive atomically with qc completion */
4702179b310aSHannes Reinecke 	if (ata_is_ncq(qc->tf.protocol)) {
47034e5b6260SJens Axboe 		link->sactive &= ~(1 << qc->hw_tag);
4704da917d69STejun Heo 		if (!link->sactive)
4705da917d69STejun Heo 			ap->nr_active_links--;
4706da917d69STejun Heo 	} else {
47079af5c9c9STejun Heo 		link->active_tag = ATA_TAG_POISON;
4708da917d69STejun Heo 		ap->nr_active_links--;
4709da917d69STejun Heo 	}
4710da917d69STejun Heo 
4711da917d69STejun Heo 	/* clear exclusive status */
4712da917d69STejun Heo 	if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
4713da917d69STejun Heo 		     ap->excl_link == link))
4714da917d69STejun Heo 		ap->excl_link = NULL;
4715c6fd2807SJeff Garzik 
4716c6fd2807SJeff Garzik 	/* atapi: mark qc as inactive to prevent the interrupt handler
4717c6fd2807SJeff Garzik 	 * from completing the command twice later, before the error handler
4718c6fd2807SJeff Garzik 	 * is called. (when rc != 0 and atapi request sense is needed)
4719c6fd2807SJeff Garzik 	 */
4720c6fd2807SJeff Garzik 	qc->flags &= ~ATA_QCFLAG_ACTIVE;
4721e3ed8939SJens Axboe 	ap->qc_active &= ~(1ULL << qc->tag);
4722c6fd2807SJeff Garzik 
4723c6fd2807SJeff Garzik 	/* call completion callback */
4724c6fd2807SJeff Garzik 	qc->complete_fn(qc);
4725c6fd2807SJeff Garzik }
4726c6fd2807SJeff Garzik 
472739599a53STejun Heo static void fill_result_tf(struct ata_queued_cmd *qc)
472839599a53STejun Heo {
472939599a53STejun Heo 	struct ata_port *ap = qc->ap;
473039599a53STejun Heo 
473139599a53STejun Heo 	qc->result_tf.flags = qc->tf.flags;
473222183bf5STejun Heo 	ap->ops->qc_fill_rtf(qc);
473339599a53STejun Heo }
473439599a53STejun Heo 
473500115e0fSTejun Heo static void ata_verify_xfer(struct ata_queued_cmd *qc)
473600115e0fSTejun Heo {
473700115e0fSTejun Heo 	struct ata_device *dev = qc->dev;
473800115e0fSTejun Heo 
4739eb0effdfSChristoph Hellwig 	if (!ata_is_data(qc->tf.protocol))
474000115e0fSTejun Heo 		return;
474100115e0fSTejun Heo 
474200115e0fSTejun Heo 	if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
474300115e0fSTejun Heo 		return;
474400115e0fSTejun Heo 
474500115e0fSTejun Heo 	dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
474600115e0fSTejun Heo }
474700115e0fSTejun Heo 
4748c6fd2807SJeff Garzik /**
4749c6fd2807SJeff Garzik  *	ata_qc_complete - Complete an active ATA command
4750c6fd2807SJeff Garzik  *	@qc: Command to complete
4751c6fd2807SJeff Garzik  *
47521aadf5c3STejun Heo  *	Indicate to the mid and upper layers that an ATA command has
47531aadf5c3STejun Heo  *	completed, with either an ok or not-ok status.
47541aadf5c3STejun Heo  *
47551aadf5c3STejun Heo  *	Refrain from calling this function multiple times when
47561aadf5c3STejun Heo  *	successfully completing multiple NCQ commands.
47571aadf5c3STejun Heo  *	ata_qc_complete_multiple() should be used instead, which will
47581aadf5c3STejun Heo  *	properly update IRQ expect state.
4759c6fd2807SJeff Garzik  *
4760c6fd2807SJeff Garzik  *	LOCKING:
4761cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4762c6fd2807SJeff Garzik  */
4763c6fd2807SJeff Garzik void ata_qc_complete(struct ata_queued_cmd *qc)
4764c6fd2807SJeff Garzik {
4765c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4766c6fd2807SJeff Garzik 
4767eb25cb99SStephan Linz 	/* Trigger the LED (if available) */
4768d1ed7c55SLinus Walleij 	ledtrig_disk_activity(!!(qc->tf.flags & ATA_TFLAG_WRITE));
4769eb25cb99SStephan Linz 
4770c6fd2807SJeff Garzik 	/* XXX: New EH and old EH use different mechanisms to
4771c6fd2807SJeff Garzik 	 * synchronize EH with regular execution path.
4772c6fd2807SJeff Garzik 	 *
477387629312SNiklas Cassel 	 * In new EH, a qc owned by EH is marked with ATA_QCFLAG_EH.
4774c6fd2807SJeff Garzik 	 * Normal execution path is responsible for not accessing a
477587629312SNiklas Cassel 	 * qc owned by EH.  libata core enforces the rule by returning NULL
477687629312SNiklas Cassel 	 * from ata_qc_from_tag() for qcs owned by EH.
4777c6fd2807SJeff Garzik 	 *
4778c6fd2807SJeff Garzik 	 * Old EH depends on ata_qc_complete() nullifying completion
4779c6fd2807SJeff Garzik 	 * requests if ATA_QCFLAG_EH_SCHEDULED is set.  Old EH does
4780c6fd2807SJeff Garzik 	 * not synchronize with interrupt handler.  Only PIO task is
4781c6fd2807SJeff Garzik 	 * taken care of.
4782c6fd2807SJeff Garzik 	 */
4783c6fd2807SJeff Garzik 	if (ap->ops->error_handler) {
47844dbfa39bSTejun Heo 		struct ata_device *dev = qc->dev;
47854dbfa39bSTejun Heo 		struct ata_eh_info *ehi = &dev->link->eh_info;
47864dbfa39bSTejun Heo 
4787c6fd2807SJeff Garzik 		if (unlikely(qc->err_mask))
478887629312SNiklas Cassel 			qc->flags |= ATA_QCFLAG_EH;
4789c6fd2807SJeff Garzik 
4790f08dc1acSTejun Heo 		/*
4791f08dc1acSTejun Heo 		 * Finish internal commands without any further processing
4792f08dc1acSTejun Heo 		 * and always with the result TF filled.
4793f08dc1acSTejun Heo 		 */
4794f08dc1acSTejun Heo 		if (unlikely(ata_tag_internal(qc->tag))) {
479539599a53STejun Heo 			fill_result_tf(qc);
4796255c03d1SHannes Reinecke 			trace_ata_qc_complete_internal(qc);
4797f4b31db9STejun Heo 			__ata_qc_complete(qc);
4798c6fd2807SJeff Garzik 			return;
4799c6fd2807SJeff Garzik 		}
4800c6fd2807SJeff Garzik 
4801f08dc1acSTejun Heo 		/*
4802f08dc1acSTejun Heo 		 * Non-internal qc has failed.  Fill the result TF and
4803f08dc1acSTejun Heo 		 * summon EH.
4804f08dc1acSTejun Heo 		 */
480587629312SNiklas Cassel 		if (unlikely(qc->flags & ATA_QCFLAG_EH)) {
4806f08dc1acSTejun Heo 			fill_result_tf(qc);
4807255c03d1SHannes Reinecke 			trace_ata_qc_complete_failed(qc);
4808f08dc1acSTejun Heo 			ata_qc_schedule_eh(qc);
4809f08dc1acSTejun Heo 			return;
4810f08dc1acSTejun Heo 		}
4811f08dc1acSTejun Heo 
48124cb7c6f1SNiklas Cassel 		WARN_ON_ONCE(ata_port_is_frozen(ap));
48134dc738edSTejun Heo 
4814c6fd2807SJeff Garzik 		/* read result TF if requested */
4815c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_RESULT_TF)
481639599a53STejun Heo 			fill_result_tf(qc);
4817c6fd2807SJeff Garzik 
4818255c03d1SHannes Reinecke 		trace_ata_qc_complete_done(qc);
48194dbfa39bSTejun Heo 		/* Some commands need post-processing after successful
48204dbfa39bSTejun Heo 		 * completion.
48214dbfa39bSTejun Heo 		 */
48224dbfa39bSTejun Heo 		switch (qc->tf.command) {
48234dbfa39bSTejun Heo 		case ATA_CMD_SET_FEATURES:
48244dbfa39bSTejun Heo 			if (qc->tf.feature != SETFEATURES_WC_ON &&
48250c12735eSTom Yan 			    qc->tf.feature != SETFEATURES_WC_OFF &&
48260c12735eSTom Yan 			    qc->tf.feature != SETFEATURES_RA_ON &&
48270c12735eSTom Yan 			    qc->tf.feature != SETFEATURES_RA_OFF)
48284dbfa39bSTejun Heo 				break;
4829df561f66SGustavo A. R. Silva 			fallthrough;
48304dbfa39bSTejun Heo 		case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
48314dbfa39bSTejun Heo 		case ATA_CMD_SET_MULTI: /* multi_count changed */
48324dbfa39bSTejun Heo 			/* revalidate device */
48334dbfa39bSTejun Heo 			ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE;
48344dbfa39bSTejun Heo 			ata_port_schedule_eh(ap);
48354dbfa39bSTejun Heo 			break;
4836054a5fbaSTejun Heo 
4837054a5fbaSTejun Heo 		case ATA_CMD_SLEEP:
4838054a5fbaSTejun Heo 			dev->flags |= ATA_DFLAG_SLEEPING;
4839054a5fbaSTejun Heo 			break;
48404dbfa39bSTejun Heo 		}
48414dbfa39bSTejun Heo 
484200115e0fSTejun Heo 		if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
484300115e0fSTejun Heo 			ata_verify_xfer(qc);
484400115e0fSTejun Heo 
4845c6fd2807SJeff Garzik 		__ata_qc_complete(qc);
4846c6fd2807SJeff Garzik 	} else {
4847c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
4848c6fd2807SJeff Garzik 			return;
4849c6fd2807SJeff Garzik 
4850c6fd2807SJeff Garzik 		/* read result TF if failed or requested */
4851c6fd2807SJeff Garzik 		if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
485239599a53STejun Heo 			fill_result_tf(qc);
4853c6fd2807SJeff Garzik 
4854c6fd2807SJeff Garzik 		__ata_qc_complete(qc);
4855c6fd2807SJeff Garzik 	}
4856c6fd2807SJeff Garzik }
4857a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_qc_complete);
4858c6fd2807SJeff Garzik 
4859c6fd2807SJeff Garzik /**
48608385d756SSascha Hauer  *	ata_qc_get_active - get bitmask of active qcs
48618385d756SSascha Hauer  *	@ap: port in question
48628385d756SSascha Hauer  *
48638385d756SSascha Hauer  *	LOCKING:
48648385d756SSascha Hauer  *	spin_lock_irqsave(host lock)
48658385d756SSascha Hauer  *
48668385d756SSascha Hauer  *	RETURNS:
48678385d756SSascha Hauer  *	Bitmask of active qcs
48688385d756SSascha Hauer  */
48698385d756SSascha Hauer u64 ata_qc_get_active(struct ata_port *ap)
48708385d756SSascha Hauer {
48718385d756SSascha Hauer 	u64 qc_active = ap->qc_active;
48728385d756SSascha Hauer 
48738385d756SSascha Hauer 	/* ATA_TAG_INTERNAL is sent to hw as tag 0 */
48748385d756SSascha Hauer 	if (qc_active & (1ULL << ATA_TAG_INTERNAL)) {
48758385d756SSascha Hauer 		qc_active |= (1 << 0);
48768385d756SSascha Hauer 		qc_active &= ~(1ULL << ATA_TAG_INTERNAL);
48778385d756SSascha Hauer 	}
48788385d756SSascha Hauer 
48798385d756SSascha Hauer 	return qc_active;
48808385d756SSascha Hauer }
48818385d756SSascha Hauer EXPORT_SYMBOL_GPL(ata_qc_get_active);
48828385d756SSascha Hauer 
48838385d756SSascha Hauer /**
4884c6fd2807SJeff Garzik  *	ata_qc_issue - issue taskfile to device
4885c6fd2807SJeff Garzik  *	@qc: command to issue to device
4886c6fd2807SJeff Garzik  *
4887c6fd2807SJeff Garzik  *	Prepare an ATA command to submission to device.
4888c6fd2807SJeff Garzik  *	This includes mapping the data into a DMA-able
4889c6fd2807SJeff Garzik  *	area, filling in the S/G table, and finally
4890c6fd2807SJeff Garzik  *	writing the taskfile to hardware, starting the command.
4891c6fd2807SJeff Garzik  *
4892c6fd2807SJeff Garzik  *	LOCKING:
4893cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4894c6fd2807SJeff Garzik  */
4895c6fd2807SJeff Garzik void ata_qc_issue(struct ata_queued_cmd *qc)
4896c6fd2807SJeff Garzik {
4897c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
48989af5c9c9STejun Heo 	struct ata_link *link = qc->dev->link;
4899405e66b3STejun Heo 	u8 prot = qc->tf.protocol;
4900c6fd2807SJeff Garzik 
4901c6fd2807SJeff Garzik 	/* Make sure only one non-NCQ command is outstanding.  The
4902c6fd2807SJeff Garzik 	 * check is skipped for old EH because it reuses active qc to
4903c6fd2807SJeff Garzik 	 * request ATAPI sense.
4904c6fd2807SJeff Garzik 	 */
4905efcb3cf7STejun Heo 	WARN_ON_ONCE(ap->ops->error_handler && ata_tag_valid(link->active_tag));
4906c6fd2807SJeff Garzik 
49071973a023STejun Heo 	if (ata_is_ncq(prot)) {
49084e5b6260SJens Axboe 		WARN_ON_ONCE(link->sactive & (1 << qc->hw_tag));
4909da917d69STejun Heo 
4910da917d69STejun Heo 		if (!link->sactive)
4911da917d69STejun Heo 			ap->nr_active_links++;
49124e5b6260SJens Axboe 		link->sactive |= 1 << qc->hw_tag;
4913c6fd2807SJeff Garzik 	} else {
4914efcb3cf7STejun Heo 		WARN_ON_ONCE(link->sactive);
4915da917d69STejun Heo 
4916da917d69STejun Heo 		ap->nr_active_links++;
49179af5c9c9STejun Heo 		link->active_tag = qc->tag;
4918c6fd2807SJeff Garzik 	}
4919c6fd2807SJeff Garzik 
4920c6fd2807SJeff Garzik 	qc->flags |= ATA_QCFLAG_ACTIVE;
4921e3ed8939SJens Axboe 	ap->qc_active |= 1ULL << qc->tag;
4922c6fd2807SJeff Garzik 
492360f5d6efSTejun Heo 	/*
492460f5d6efSTejun Heo 	 * We guarantee to LLDs that they will have at least one
4925f92a2636STejun Heo 	 * non-zero sg if the command is a data command.
4926f92a2636STejun Heo 	 */
49279173e5e8SEric Biggers 	if (ata_is_data(prot) && (!qc->sg || !qc->n_elem || !qc->nbytes))
492860f5d6efSTejun Heo 		goto sys_err;
4929f92a2636STejun Heo 
4930405e66b3STejun Heo 	if (ata_is_dma(prot) || (ata_is_pio(prot) &&
4931f92a2636STejun Heo 				 (ap->flags & ATA_FLAG_PIO_DMA)))
4932c6fd2807SJeff Garzik 		if (ata_sg_setup(qc))
493360f5d6efSTejun Heo 			goto sys_err;
4934c6fd2807SJeff Garzik 
4935cf480626STejun Heo 	/* if device is sleeping, schedule reset and abort the link */
4936054a5fbaSTejun Heo 	if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
4937cf480626STejun Heo 		link->eh_info.action |= ATA_EH_RESET;
4938054a5fbaSTejun Heo 		ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
4939054a5fbaSTejun Heo 		ata_link_abort(link);
4940054a5fbaSTejun Heo 		return;
4941054a5fbaSTejun Heo 	}
4942054a5fbaSTejun Heo 
4943fc914faaSHannes Reinecke 	trace_ata_qc_prep(qc);
494495364f36SJiri Slaby 	qc->err_mask |= ap->ops->qc_prep(qc);
494595364f36SJiri Slaby 	if (unlikely(qc->err_mask))
494695364f36SJiri Slaby 		goto err;
4947255c03d1SHannes Reinecke 	trace_ata_qc_issue(qc);
4948c6fd2807SJeff Garzik 	qc->err_mask |= ap->ops->qc_issue(qc);
4949c6fd2807SJeff Garzik 	if (unlikely(qc->err_mask))
4950c6fd2807SJeff Garzik 		goto err;
4951c6fd2807SJeff Garzik 	return;
4952c6fd2807SJeff Garzik 
495360f5d6efSTejun Heo sys_err:
4954c6fd2807SJeff Garzik 	qc->err_mask |= AC_ERR_SYSTEM;
4955c6fd2807SJeff Garzik err:
4956c6fd2807SJeff Garzik 	ata_qc_complete(qc);
4957c6fd2807SJeff Garzik }
4958c6fd2807SJeff Garzik 
4959c6fd2807SJeff Garzik /**
4960b1c72916STejun Heo  *	ata_phys_link_online - test whether the given link is online
4961936fd732STejun Heo  *	@link: ATA link to test
4962c6fd2807SJeff Garzik  *
4963936fd732STejun Heo  *	Test whether @link is online.  Note that this function returns
4964936fd732STejun Heo  *	0 if online status of @link cannot be obtained, so
4965936fd732STejun Heo  *	ata_link_online(link) != !ata_link_offline(link).
4966c6fd2807SJeff Garzik  *
4967c6fd2807SJeff Garzik  *	LOCKING:
4968c6fd2807SJeff Garzik  *	None.
4969c6fd2807SJeff Garzik  *
4970c6fd2807SJeff Garzik  *	RETURNS:
4971b5b3fa38STejun Heo  *	True if the port online status is available and online.
4972c6fd2807SJeff Garzik  */
4973b1c72916STejun Heo bool ata_phys_link_online(struct ata_link *link)
4974c6fd2807SJeff Garzik {
4975c6fd2807SJeff Garzik 	u32 sstatus;
4976c6fd2807SJeff Garzik 
4977936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
49789913ff8aSTejun Heo 	    ata_sstatus_online(sstatus))
4979b5b3fa38STejun Heo 		return true;
4980b5b3fa38STejun Heo 	return false;
4981c6fd2807SJeff Garzik }
4982c6fd2807SJeff Garzik 
4983c6fd2807SJeff Garzik /**
4984b1c72916STejun Heo  *	ata_phys_link_offline - test whether the given link is offline
4985936fd732STejun Heo  *	@link: ATA link to test
4986c6fd2807SJeff Garzik  *
4987936fd732STejun Heo  *	Test whether @link is offline.  Note that this function
4988936fd732STejun Heo  *	returns 0 if offline status of @link cannot be obtained, so
4989936fd732STejun Heo  *	ata_link_online(link) != !ata_link_offline(link).
4990c6fd2807SJeff Garzik  *
4991c6fd2807SJeff Garzik  *	LOCKING:
4992c6fd2807SJeff Garzik  *	None.
4993c6fd2807SJeff Garzik  *
4994c6fd2807SJeff Garzik  *	RETURNS:
4995b5b3fa38STejun Heo  *	True if the port offline status is available and offline.
4996c6fd2807SJeff Garzik  */
4997b1c72916STejun Heo bool ata_phys_link_offline(struct ata_link *link)
4998c6fd2807SJeff Garzik {
4999c6fd2807SJeff Garzik 	u32 sstatus;
5000c6fd2807SJeff Garzik 
5001936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
50029913ff8aSTejun Heo 	    !ata_sstatus_online(sstatus))
5003b5b3fa38STejun Heo 		return true;
5004b5b3fa38STejun Heo 	return false;
5005c6fd2807SJeff Garzik }
5006c6fd2807SJeff Garzik 
5007b1c72916STejun Heo /**
5008b1c72916STejun Heo  *	ata_link_online - test whether the given link is online
5009b1c72916STejun Heo  *	@link: ATA link to test
5010b1c72916STejun Heo  *
5011b1c72916STejun Heo  *	Test whether @link is online.  This is identical to
5012b1c72916STejun Heo  *	ata_phys_link_online() when there's no slave link.  When
5013b1c72916STejun Heo  *	there's a slave link, this function should only be called on
5014b1c72916STejun Heo  *	the master link and will return true if any of M/S links is
5015b1c72916STejun Heo  *	online.
5016b1c72916STejun Heo  *
5017b1c72916STejun Heo  *	LOCKING:
5018b1c72916STejun Heo  *	None.
5019b1c72916STejun Heo  *
5020b1c72916STejun Heo  *	RETURNS:
5021b1c72916STejun Heo  *	True if the port online status is available and online.
5022b1c72916STejun Heo  */
5023b1c72916STejun Heo bool ata_link_online(struct ata_link *link)
5024b1c72916STejun Heo {
5025b1c72916STejun Heo 	struct ata_link *slave = link->ap->slave_link;
5026b1c72916STejun Heo 
5027b1c72916STejun Heo 	WARN_ON(link == slave);	/* shouldn't be called on slave link */
5028b1c72916STejun Heo 
5029b1c72916STejun Heo 	return ata_phys_link_online(link) ||
5030b1c72916STejun Heo 		(slave && ata_phys_link_online(slave));
5031b1c72916STejun Heo }
5032a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_link_online);
5033b1c72916STejun Heo 
5034b1c72916STejun Heo /**
5035b1c72916STejun Heo  *	ata_link_offline - test whether the given link is offline
5036b1c72916STejun Heo  *	@link: ATA link to test
5037b1c72916STejun Heo  *
5038b1c72916STejun Heo  *	Test whether @link is offline.  This is identical to
5039b1c72916STejun Heo  *	ata_phys_link_offline() when there's no slave link.  When
5040b1c72916STejun Heo  *	there's a slave link, this function should only be called on
5041b1c72916STejun Heo  *	the master link and will return true if both M/S links are
5042b1c72916STejun Heo  *	offline.
5043b1c72916STejun Heo  *
5044b1c72916STejun Heo  *	LOCKING:
5045b1c72916STejun Heo  *	None.
5046b1c72916STejun Heo  *
5047b1c72916STejun Heo  *	RETURNS:
5048b1c72916STejun Heo  *	True if the port offline status is available and offline.
5049b1c72916STejun Heo  */
5050b1c72916STejun Heo bool ata_link_offline(struct ata_link *link)
5051b1c72916STejun Heo {
5052b1c72916STejun Heo 	struct ata_link *slave = link->ap->slave_link;
5053b1c72916STejun Heo 
5054b1c72916STejun Heo 	WARN_ON(link == slave);	/* shouldn't be called on slave link */
5055b1c72916STejun Heo 
5056b1c72916STejun Heo 	return ata_phys_link_offline(link) &&
5057b1c72916STejun Heo 		(!slave || ata_phys_link_offline(slave));
5058b1c72916STejun Heo }
5059a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_link_offline);
5060b1c72916STejun Heo 
50616ffa01d8STejun Heo #ifdef CONFIG_PM
5062bc6e7c4bSDan Williams static void ata_port_request_pm(struct ata_port *ap, pm_message_t mesg,
5063cca3974eSJeff Garzik 				unsigned int action, unsigned int ehi_flags,
5064bc6e7c4bSDan Williams 				bool async)
5065c6fd2807SJeff Garzik {
5066e3667ebfSTejun Heo 	struct ata_link *link;
50675ef41082SLin Ming 	unsigned long flags;
5068c6fd2807SJeff Garzik 
5069c6fd2807SJeff Garzik 	/* Previous resume operation might still be in
5070c6fd2807SJeff Garzik 	 * progress.  Wait for PM_PENDING to clear.
5071c6fd2807SJeff Garzik 	 */
5072c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_PM_PENDING) {
5073c6fd2807SJeff Garzik 		ata_port_wait_eh(ap);
5074c6fd2807SJeff Garzik 		WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5075c6fd2807SJeff Garzik 	}
5076c6fd2807SJeff Garzik 
5077c6fd2807SJeff Garzik 	/* request PM ops to EH */
5078c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
5079c6fd2807SJeff Garzik 
5080c6fd2807SJeff Garzik 	ap->pm_mesg = mesg;
5081c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_PM_PENDING;
50821eca4365STejun Heo 	ata_for_each_link(link, ap, HOST_FIRST) {
5083e3667ebfSTejun Heo 		link->eh_info.action |= action;
5084e3667ebfSTejun Heo 		link->eh_info.flags |= ehi_flags;
5085e3667ebfSTejun Heo 	}
5086c6fd2807SJeff Garzik 
5087c6fd2807SJeff Garzik 	ata_port_schedule_eh(ap);
5088c6fd2807SJeff Garzik 
5089c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
5090c6fd2807SJeff Garzik 
50912fcbdcb4SDan Williams 	if (!async) {
5092c6fd2807SJeff Garzik 		ata_port_wait_eh(ap);
5093c6fd2807SJeff Garzik 		WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5094c6fd2807SJeff Garzik 	}
5095c6fd2807SJeff Garzik }
5096c6fd2807SJeff Garzik 
509733574d68SLin Ming /*
5098bc6e7c4bSDan Williams  * On some hardware, device fails to respond after spun down for suspend.  As
5099bc6e7c4bSDan Williams  * the device won't be used before being resumed, we don't need to touch the
5100bc6e7c4bSDan Williams  * device.  Ask EH to skip the usual stuff and proceed directly to suspend.
510133574d68SLin Ming  *
510233574d68SLin Ming  * http://thread.gmane.org/gmane.linux.ide/46764
510333574d68SLin Ming  */
5104bc6e7c4bSDan Williams static const unsigned int ata_port_suspend_ehi = ATA_EHI_QUIET
5105bc6e7c4bSDan Williams 						 | ATA_EHI_NO_AUTOPSY
5106bc6e7c4bSDan Williams 						 | ATA_EHI_NO_RECOVERY;
5107bc6e7c4bSDan Williams 
5108bc6e7c4bSDan Williams static void ata_port_suspend(struct ata_port *ap, pm_message_t mesg)
5109bc6e7c4bSDan Williams {
5110bc6e7c4bSDan Williams 	ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, false);
51115ef41082SLin Ming }
51125ef41082SLin Ming 
5113bc6e7c4bSDan Williams static void ata_port_suspend_async(struct ata_port *ap, pm_message_t mesg)
5114bc6e7c4bSDan Williams {
5115bc6e7c4bSDan Williams 	ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, true);
5116bc6e7c4bSDan Williams }
5117bc6e7c4bSDan Williams 
5118bc6e7c4bSDan Williams static int ata_port_pm_suspend(struct device *dev)
51192fcbdcb4SDan Williams {
51202fcbdcb4SDan Williams 	struct ata_port *ap = to_ata_port(dev);
51212fcbdcb4SDan Williams 
51225ef41082SLin Ming 	if (pm_runtime_suspended(dev))
51235ef41082SLin Ming 		return 0;
51245ef41082SLin Ming 
5125bc6e7c4bSDan Williams 	ata_port_suspend(ap, PMSG_SUSPEND);
5126f5e6d0d0SAaron Lu 	return 0;
512733574d68SLin Ming }
512833574d68SLin Ming 
5129bc6e7c4bSDan Williams static int ata_port_pm_freeze(struct device *dev)
51302fcbdcb4SDan Williams {
51312fcbdcb4SDan Williams 	struct ata_port *ap = to_ata_port(dev);
51322fcbdcb4SDan Williams 
5133bc6e7c4bSDan Williams 	if (pm_runtime_suspended(dev))
5134bc6e7c4bSDan Williams 		return 0;
5135bc6e7c4bSDan Williams 
5136bc6e7c4bSDan Williams 	ata_port_suspend(ap, PMSG_FREEZE);
5137bc6e7c4bSDan Williams 	return 0;
51382fcbdcb4SDan Williams }
51392fcbdcb4SDan Williams 
5140bc6e7c4bSDan Williams static int ata_port_pm_poweroff(struct device *dev)
5141e90b1e5aSLin Ming {
5142bc6e7c4bSDan Williams 	ata_port_suspend(to_ata_port(dev), PMSG_HIBERNATE);
5143bc6e7c4bSDan Williams 	return 0;
5144bc6e7c4bSDan Williams }
5145e90b1e5aSLin Ming 
5146bc6e7c4bSDan Williams static const unsigned int ata_port_resume_ehi = ATA_EHI_NO_AUTOPSY
5147bc6e7c4bSDan Williams 						| ATA_EHI_QUIET;
5148bc6e7c4bSDan Williams 
5149bc6e7c4bSDan Williams static void ata_port_resume(struct ata_port *ap, pm_message_t mesg)
5150bc6e7c4bSDan Williams {
5151bc6e7c4bSDan Williams 	ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, false);
5152bc6e7c4bSDan Williams }
5153bc6e7c4bSDan Williams 
5154bc6e7c4bSDan Williams static void ata_port_resume_async(struct ata_port *ap, pm_message_t mesg)
5155bc6e7c4bSDan Williams {
5156bc6e7c4bSDan Williams 	ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, true);
5157bc6e7c4bSDan Williams }
5158bc6e7c4bSDan Williams 
5159bc6e7c4bSDan Williams static int ata_port_pm_resume(struct device *dev)
5160bc6e7c4bSDan Williams {
5161200421a8STodd Brandt 	ata_port_resume_async(to_ata_port(dev), PMSG_RESUME);
5162e90b1e5aSLin Ming 	pm_runtime_disable(dev);
5163e90b1e5aSLin Ming 	pm_runtime_set_active(dev);
5164e90b1e5aSLin Ming 	pm_runtime_enable(dev);
5165bc6e7c4bSDan Williams 	return 0;
5166e90b1e5aSLin Ming }
5167e90b1e5aSLin Ming 
51687e15e9beSAaron Lu /*
51697e15e9beSAaron Lu  * For ODDs, the upper layer will poll for media change every few seconds,
51707e15e9beSAaron Lu  * which will make it enter and leave suspend state every few seconds. And
51717e15e9beSAaron Lu  * as each suspend will cause a hard/soft reset, the gain of runtime suspend
51727e15e9beSAaron Lu  * is very little and the ODD may malfunction after constantly being reset.
51737e15e9beSAaron Lu  * So the idle callback here will not proceed to suspend if a non-ZPODD capable
51747e15e9beSAaron Lu  * ODD is attached to the port.
51757e15e9beSAaron Lu  */
51769ee4f393SLin Ming static int ata_port_runtime_idle(struct device *dev)
51779ee4f393SLin Ming {
51787e15e9beSAaron Lu 	struct ata_port *ap = to_ata_port(dev);
51797e15e9beSAaron Lu 	struct ata_link *link;
51807e15e9beSAaron Lu 	struct ata_device *adev;
51817e15e9beSAaron Lu 
51827e15e9beSAaron Lu 	ata_for_each_link(link, ap, HOST_FIRST) {
51837e15e9beSAaron Lu 		ata_for_each_dev(adev, link, ENABLED)
51847e15e9beSAaron Lu 			if (adev->class == ATA_DEV_ATAPI &&
51857e15e9beSAaron Lu 			    !zpodd_dev_enabled(adev))
51867e15e9beSAaron Lu 				return -EBUSY;
51877e15e9beSAaron Lu 	}
51887e15e9beSAaron Lu 
518945f0a85cSRafael J. Wysocki 	return 0;
51909ee4f393SLin Ming }
51919ee4f393SLin Ming 
5192a7ff60dbSAaron Lu static int ata_port_runtime_suspend(struct device *dev)
5193a7ff60dbSAaron Lu {
5194bc6e7c4bSDan Williams 	ata_port_suspend(to_ata_port(dev), PMSG_AUTO_SUSPEND);
5195bc6e7c4bSDan Williams 	return 0;
5196a7ff60dbSAaron Lu }
5197a7ff60dbSAaron Lu 
5198a7ff60dbSAaron Lu static int ata_port_runtime_resume(struct device *dev)
5199a7ff60dbSAaron Lu {
5200bc6e7c4bSDan Williams 	ata_port_resume(to_ata_port(dev), PMSG_AUTO_RESUME);
5201bc6e7c4bSDan Williams 	return 0;
5202a7ff60dbSAaron Lu }
5203a7ff60dbSAaron Lu 
52045ef41082SLin Ming static const struct dev_pm_ops ata_port_pm_ops = {
5205bc6e7c4bSDan Williams 	.suspend = ata_port_pm_suspend,
5206bc6e7c4bSDan Williams 	.resume = ata_port_pm_resume,
5207bc6e7c4bSDan Williams 	.freeze = ata_port_pm_freeze,
5208bc6e7c4bSDan Williams 	.thaw = ata_port_pm_resume,
5209bc6e7c4bSDan Williams 	.poweroff = ata_port_pm_poweroff,
5210bc6e7c4bSDan Williams 	.restore = ata_port_pm_resume,
52119ee4f393SLin Ming 
5212a7ff60dbSAaron Lu 	.runtime_suspend = ata_port_runtime_suspend,
5213a7ff60dbSAaron Lu 	.runtime_resume = ata_port_runtime_resume,
52149ee4f393SLin Ming 	.runtime_idle = ata_port_runtime_idle,
52155ef41082SLin Ming };
52165ef41082SLin Ming 
52172fcbdcb4SDan Williams /* sas ports don't participate in pm runtime management of ata_ports,
52182fcbdcb4SDan Williams  * and need to resume ata devices at the domain level, not the per-port
52192fcbdcb4SDan Williams  * level. sas suspend/resume is async to allow parallel port recovery
52202fcbdcb4SDan Williams  * since sas has multiple ata_port instances per Scsi_Host.
52212fcbdcb4SDan Williams  */
5222bc6e7c4bSDan Williams void ata_sas_port_suspend(struct ata_port *ap)
52232fcbdcb4SDan Williams {
5224bc6e7c4bSDan Williams 	ata_port_suspend_async(ap, PMSG_SUSPEND);
52252fcbdcb4SDan Williams }
5226bc6e7c4bSDan Williams EXPORT_SYMBOL_GPL(ata_sas_port_suspend);
52272fcbdcb4SDan Williams 
5228bc6e7c4bSDan Williams void ata_sas_port_resume(struct ata_port *ap)
52292fcbdcb4SDan Williams {
5230bc6e7c4bSDan Williams 	ata_port_resume_async(ap, PMSG_RESUME);
52312fcbdcb4SDan Williams }
5232bc6e7c4bSDan Williams EXPORT_SYMBOL_GPL(ata_sas_port_resume);
52332fcbdcb4SDan Williams 
5234c6fd2807SJeff Garzik /**
5235cca3974eSJeff Garzik  *	ata_host_suspend - suspend host
5236cca3974eSJeff Garzik  *	@host: host to suspend
5237c6fd2807SJeff Garzik  *	@mesg: PM message
5238c6fd2807SJeff Garzik  *
52395ef41082SLin Ming  *	Suspend @host.  Actual operation is performed by port suspend.
5240c6fd2807SJeff Garzik  */
5241ec87cf37SSergey Shtylyov void ata_host_suspend(struct ata_host *host, pm_message_t mesg)
5242c6fd2807SJeff Garzik {
524372ad6ec4SJeff Garzik 	host->dev->power.power_state = mesg;
5244c6fd2807SJeff Garzik }
5245a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_suspend);
5246c6fd2807SJeff Garzik 
5247c6fd2807SJeff Garzik /**
5248cca3974eSJeff Garzik  *	ata_host_resume - resume host
5249cca3974eSJeff Garzik  *	@host: host to resume
5250c6fd2807SJeff Garzik  *
52515ef41082SLin Ming  *	Resume @host.  Actual operation is performed by port resume.
5252c6fd2807SJeff Garzik  */
5253cca3974eSJeff Garzik void ata_host_resume(struct ata_host *host)
5254c6fd2807SJeff Garzik {
525572ad6ec4SJeff Garzik 	host->dev->power.power_state = PMSG_ON;
5256c6fd2807SJeff Garzik }
5257a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_resume);
52586ffa01d8STejun Heo #endif
5259c6fd2807SJeff Garzik 
52608df82c13SBhumika Goyal const struct device_type ata_port_type = {
52615ef41082SLin Ming 	.name = "ata_port",
52625ef41082SLin Ming #ifdef CONFIG_PM
52635ef41082SLin Ming 	.pm = &ata_port_pm_ops,
52645ef41082SLin Ming #endif
52655ef41082SLin Ming };
52665ef41082SLin Ming 
5267c6fd2807SJeff Garzik /**
5268c6fd2807SJeff Garzik  *	ata_dev_init - Initialize an ata_device structure
5269c6fd2807SJeff Garzik  *	@dev: Device structure to initialize
5270c6fd2807SJeff Garzik  *
5271c6fd2807SJeff Garzik  *	Initialize @dev in preparation for probing.
5272c6fd2807SJeff Garzik  *
5273c6fd2807SJeff Garzik  *	LOCKING:
5274c6fd2807SJeff Garzik  *	Inherited from caller.
5275c6fd2807SJeff Garzik  */
5276c6fd2807SJeff Garzik void ata_dev_init(struct ata_device *dev)
5277c6fd2807SJeff Garzik {
5278b1c72916STejun Heo 	struct ata_link *link = ata_dev_phys_link(dev);
52799af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
5280c6fd2807SJeff Garzik 	unsigned long flags;
5281c6fd2807SJeff Garzik 
5282b1c72916STejun Heo 	/* SATA spd limit is bound to the attached device, reset together */
52839af5c9c9STejun Heo 	link->sata_spd_limit = link->hw_sata_spd_limit;
52849af5c9c9STejun Heo 	link->sata_spd = 0;
5285c6fd2807SJeff Garzik 
5286c6fd2807SJeff Garzik 	/* High bits of dev->flags are used to record warm plug
5287c6fd2807SJeff Garzik 	 * requests which occur asynchronously.  Synchronize using
5288cca3974eSJeff Garzik 	 * host lock.
5289c6fd2807SJeff Garzik 	 */
5290c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
5291c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_INIT_MASK;
52923dcc323fSTejun Heo 	dev->horkage = 0;
5293c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
5294c6fd2807SJeff Garzik 
529599cf610aSTejun Heo 	memset((void *)dev + ATA_DEVICE_CLEAR_BEGIN, 0,
529699cf610aSTejun Heo 	       ATA_DEVICE_CLEAR_END - ATA_DEVICE_CLEAR_BEGIN);
5297c6fd2807SJeff Garzik 	dev->pio_mask = UINT_MAX;
5298c6fd2807SJeff Garzik 	dev->mwdma_mask = UINT_MAX;
5299c6fd2807SJeff Garzik 	dev->udma_mask = UINT_MAX;
5300c6fd2807SJeff Garzik }
5301c6fd2807SJeff Garzik 
5302c6fd2807SJeff Garzik /**
53034fb37a25STejun Heo  *	ata_link_init - Initialize an ata_link structure
53044fb37a25STejun Heo  *	@ap: ATA port link is attached to
53054fb37a25STejun Heo  *	@link: Link structure to initialize
53068989805dSTejun Heo  *	@pmp: Port multiplier port number
53074fb37a25STejun Heo  *
53084fb37a25STejun Heo  *	Initialize @link.
53094fb37a25STejun Heo  *
53104fb37a25STejun Heo  *	LOCKING:
53114fb37a25STejun Heo  *	Kernel thread context (may sleep)
53124fb37a25STejun Heo  */
5313fb7fd614STejun Heo void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
53144fb37a25STejun Heo {
53154fb37a25STejun Heo 	int i;
53164fb37a25STejun Heo 
53174fb37a25STejun Heo 	/* clear everything except for devices */
5318d9027470SGwendal Grignou 	memset((void *)link + ATA_LINK_CLEAR_BEGIN, 0,
5319d9027470SGwendal Grignou 	       ATA_LINK_CLEAR_END - ATA_LINK_CLEAR_BEGIN);
53204fb37a25STejun Heo 
53214fb37a25STejun Heo 	link->ap = ap;
53228989805dSTejun Heo 	link->pmp = pmp;
53234fb37a25STejun Heo 	link->active_tag = ATA_TAG_POISON;
53244fb37a25STejun Heo 	link->hw_sata_spd_limit = UINT_MAX;
53254fb37a25STejun Heo 
53264fb37a25STejun Heo 	/* can't use iterator, ap isn't initialized yet */
53274fb37a25STejun Heo 	for (i = 0; i < ATA_MAX_DEVICES; i++) {
53284fb37a25STejun Heo 		struct ata_device *dev = &link->device[i];
53294fb37a25STejun Heo 
53304fb37a25STejun Heo 		dev->link = link;
53314fb37a25STejun Heo 		dev->devno = dev - link->device;
5332110f66d2STejun Heo #ifdef CONFIG_ATA_ACPI
5333110f66d2STejun Heo 		dev->gtf_filter = ata_acpi_gtf_filter;
5334110f66d2STejun Heo #endif
53354fb37a25STejun Heo 		ata_dev_init(dev);
53364fb37a25STejun Heo 	}
53374fb37a25STejun Heo }
53384fb37a25STejun Heo 
53394fb37a25STejun Heo /**
53404fb37a25STejun Heo  *	sata_link_init_spd - Initialize link->sata_spd_limit
53414fb37a25STejun Heo  *	@link: Link to configure sata_spd_limit for
53424fb37a25STejun Heo  *
5343a31a6997SMauro Carvalho Chehab  *	Initialize ``link->[hw_]sata_spd_limit`` to the currently
53444fb37a25STejun Heo  *	configured value.
53454fb37a25STejun Heo  *
53464fb37a25STejun Heo  *	LOCKING:
53474fb37a25STejun Heo  *	Kernel thread context (may sleep).
53484fb37a25STejun Heo  *
53494fb37a25STejun Heo  *	RETURNS:
53504fb37a25STejun Heo  *	0 on success, -errno on failure.
53514fb37a25STejun Heo  */
5352fb7fd614STejun Heo int sata_link_init_spd(struct ata_link *link)
53534fb37a25STejun Heo {
535433267325STejun Heo 	u8 spd;
53554fb37a25STejun Heo 	int rc;
53564fb37a25STejun Heo 
5357d127ea7bSTejun Heo 	rc = sata_scr_read(link, SCR_CONTROL, &link->saved_scontrol);
53584fb37a25STejun Heo 	if (rc)
53594fb37a25STejun Heo 		return rc;
53604fb37a25STejun Heo 
5361d127ea7bSTejun Heo 	spd = (link->saved_scontrol >> 4) & 0xf;
53624fb37a25STejun Heo 	if (spd)
53634fb37a25STejun Heo 		link->hw_sata_spd_limit &= (1 << spd) - 1;
53644fb37a25STejun Heo 
536505944bdfSTejun Heo 	ata_force_link_limits(link);
536633267325STejun Heo 
53674fb37a25STejun Heo 	link->sata_spd_limit = link->hw_sata_spd_limit;
53684fb37a25STejun Heo 
53694fb37a25STejun Heo 	return 0;
53704fb37a25STejun Heo }
53714fb37a25STejun Heo 
53724fb37a25STejun Heo /**
5373f3187195STejun Heo  *	ata_port_alloc - allocate and initialize basic ATA port resources
5374f3187195STejun Heo  *	@host: ATA host this allocated port belongs to
5375c6fd2807SJeff Garzik  *
5376f3187195STejun Heo  *	Allocate and initialize basic ATA port resources.
5377f3187195STejun Heo  *
5378f3187195STejun Heo  *	RETURNS:
5379f3187195STejun Heo  *	Allocate ATA port on success, NULL on failure.
5380c6fd2807SJeff Garzik  *
5381c6fd2807SJeff Garzik  *	LOCKING:
5382f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5383c6fd2807SJeff Garzik  */
5384f3187195STejun Heo struct ata_port *ata_port_alloc(struct ata_host *host)
5385c6fd2807SJeff Garzik {
5386f3187195STejun Heo 	struct ata_port *ap;
5387c6fd2807SJeff Garzik 
5388f3187195STejun Heo 	ap = kzalloc(sizeof(*ap), GFP_KERNEL);
5389f3187195STejun Heo 	if (!ap)
5390f3187195STejun Heo 		return NULL;
5391f3187195STejun Heo 
53927b3a24c5SMaxime Bizon 	ap->pflags |= ATA_PFLAG_INITIALIZING | ATA_PFLAG_FROZEN;
5393cca3974eSJeff Garzik 	ap->lock = &host->lock;
5394f3187195STejun Heo 	ap->print_id = -1;
5395e628dc99SDavid Milburn 	ap->local_port_no = -1;
5396cca3974eSJeff Garzik 	ap->host = host;
5397f3187195STejun Heo 	ap->dev = host->dev;
5398c6fd2807SJeff Garzik 
5399ad72cf98STejun Heo 	mutex_init(&ap->scsi_scan_mutex);
540065f27f38SDavid Howells 	INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
540165f27f38SDavid Howells 	INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
5402c6fd2807SJeff Garzik 	INIT_LIST_HEAD(&ap->eh_done_q);
5403c6fd2807SJeff Garzik 	init_waitqueue_head(&ap->eh_wait_q);
540445fabbb7SElias Oltmanns 	init_completion(&ap->park_req_pending);
5405b93ab338SKees Cook 	timer_setup(&ap->fastdrain_timer, ata_eh_fastdrain_timerfn,
5406b93ab338SKees Cook 		    TIMER_DEFERRABLE);
5407c6fd2807SJeff Garzik 
5408c6fd2807SJeff Garzik 	ap->cbl = ATA_CBL_NONE;
5409c6fd2807SJeff Garzik 
54108989805dSTejun Heo 	ata_link_init(ap, &ap->link, 0);
5411c6fd2807SJeff Garzik 
5412c6fd2807SJeff Garzik #ifdef ATA_IRQ_TRAP
5413c6fd2807SJeff Garzik 	ap->stats.unhandled_irq = 1;
5414c6fd2807SJeff Garzik 	ap->stats.idle_irq = 1;
5415c6fd2807SJeff Garzik #endif
5416270390e1STejun Heo 	ata_sff_port_init(ap);
5417270390e1STejun Heo 
5418c6fd2807SJeff Garzik 	return ap;
5419c6fd2807SJeff Garzik }
5420c6fd2807SJeff Garzik 
54212623c7a5STaras Kondratiuk static void ata_devres_release(struct device *gendev, void *res)
5422f0d36efdSTejun Heo {
5423f0d36efdSTejun Heo 	struct ata_host *host = dev_get_drvdata(gendev);
5424f0d36efdSTejun Heo 	int i;
5425f0d36efdSTejun Heo 
5426f0d36efdSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
5427f0d36efdSTejun Heo 		struct ata_port *ap = host->ports[i];
5428f0d36efdSTejun Heo 
5429ecef7253STejun Heo 		if (!ap)
5430ecef7253STejun Heo 			continue;
5431ecef7253STejun Heo 
54324911487aSTejun Heo 		if (ap->scsi_host)
54331aa506e4STejun Heo 			scsi_host_put(ap->scsi_host);
54341aa506e4STejun Heo 
54352623c7a5STaras Kondratiuk 	}
54362623c7a5STaras Kondratiuk 
54372623c7a5STaras Kondratiuk 	dev_set_drvdata(gendev, NULL);
54382623c7a5STaras Kondratiuk 	ata_host_put(host);
54392623c7a5STaras Kondratiuk }
54402623c7a5STaras Kondratiuk 
54412623c7a5STaras Kondratiuk static void ata_host_release(struct kref *kref)
54422623c7a5STaras Kondratiuk {
54432623c7a5STaras Kondratiuk 	struct ata_host *host = container_of(kref, struct ata_host, kref);
54442623c7a5STaras Kondratiuk 	int i;
54452623c7a5STaras Kondratiuk 
54462623c7a5STaras Kondratiuk 	for (i = 0; i < host->n_ports; i++) {
54472623c7a5STaras Kondratiuk 		struct ata_port *ap = host->ports[i];
54482623c7a5STaras Kondratiuk 
5449633273a3STejun Heo 		kfree(ap->pmp_link);
5450b1c72916STejun Heo 		kfree(ap->slave_link);
54514911487aSTejun Heo 		kfree(ap);
54521aa506e4STejun Heo 		host->ports[i] = NULL;
54531aa506e4STejun Heo 	}
54542623c7a5STaras Kondratiuk 	kfree(host);
54552623c7a5STaras Kondratiuk }
54561aa506e4STejun Heo 
54572623c7a5STaras Kondratiuk void ata_host_get(struct ata_host *host)
54582623c7a5STaras Kondratiuk {
54592623c7a5STaras Kondratiuk 	kref_get(&host->kref);
54602623c7a5STaras Kondratiuk }
54612623c7a5STaras Kondratiuk 
54622623c7a5STaras Kondratiuk void ata_host_put(struct ata_host *host)
54632623c7a5STaras Kondratiuk {
54642623c7a5STaras Kondratiuk 	kref_put(&host->kref, ata_host_release);
5465f0d36efdSTejun Heo }
5466a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_put);
5467f0d36efdSTejun Heo 
5468c6fd2807SJeff Garzik /**
5469f3187195STejun Heo  *	ata_host_alloc - allocate and init basic ATA host resources
5470f3187195STejun Heo  *	@dev: generic device this host is associated with
5471f3187195STejun Heo  *	@max_ports: maximum number of ATA ports associated with this host
5472f3187195STejun Heo  *
5473f3187195STejun Heo  *	Allocate and initialize basic ATA host resources.  LLD calls
5474f3187195STejun Heo  *	this function to allocate a host, initializes it fully and
5475f3187195STejun Heo  *	attaches it using ata_host_register().
5476f3187195STejun Heo  *
5477f3187195STejun Heo  *	@max_ports ports are allocated and host->n_ports is
5478f3187195STejun Heo  *	initialized to @max_ports.  The caller is allowed to decrease
5479f3187195STejun Heo  *	host->n_ports before calling ata_host_register().  The unused
5480f3187195STejun Heo  *	ports will be automatically freed on registration.
5481f3187195STejun Heo  *
5482f3187195STejun Heo  *	RETURNS:
5483f3187195STejun Heo  *	Allocate ATA host on success, NULL on failure.
5484f3187195STejun Heo  *
5485f3187195STejun Heo  *	LOCKING:
5486f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5487f3187195STejun Heo  */
5488f3187195STejun Heo struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
5489f3187195STejun Heo {
5490f3187195STejun Heo 	struct ata_host *host;
5491f3187195STejun Heo 	size_t sz;
5492f3187195STejun Heo 	int i;
54932623c7a5STaras Kondratiuk 	void *dr;
5494f3187195STejun Heo 
5495f3187195STejun Heo 	/* alloc a container for our list of ATA ports (buses) */
5496f3187195STejun Heo 	sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
54972623c7a5STaras Kondratiuk 	host = kzalloc(sz, GFP_KERNEL);
5498f3187195STejun Heo 	if (!host)
54992623c7a5STaras Kondratiuk 		return NULL;
55002623c7a5STaras Kondratiuk 
5501f3187195STejun Heo 	if (!devres_open_group(dev, NULL, GFP_KERNEL))
5502dafd6c49SColin Ian King 		goto err_free;
5503f3187195STejun Heo 
55042623c7a5STaras Kondratiuk 	dr = devres_alloc(ata_devres_release, 0, GFP_KERNEL);
55052623c7a5STaras Kondratiuk 	if (!dr)
5506f3187195STejun Heo 		goto err_out;
5507f3187195STejun Heo 
55082623c7a5STaras Kondratiuk 	devres_add(dev, dr);
5509f3187195STejun Heo 	dev_set_drvdata(dev, host);
5510f3187195STejun Heo 
5511f3187195STejun Heo 	spin_lock_init(&host->lock);
5512c0c362b6STejun Heo 	mutex_init(&host->eh_mutex);
5513f3187195STejun Heo 	host->dev = dev;
5514f3187195STejun Heo 	host->n_ports = max_ports;
55152623c7a5STaras Kondratiuk 	kref_init(&host->kref);
5516f3187195STejun Heo 
5517f3187195STejun Heo 	/* allocate ports bound to this host */
5518f3187195STejun Heo 	for (i = 0; i < max_ports; i++) {
5519f3187195STejun Heo 		struct ata_port *ap;
5520f3187195STejun Heo 
5521f3187195STejun Heo 		ap = ata_port_alloc(host);
5522f3187195STejun Heo 		if (!ap)
5523f3187195STejun Heo 			goto err_out;
5524f3187195STejun Heo 
5525f3187195STejun Heo 		ap->port_no = i;
5526f3187195STejun Heo 		host->ports[i] = ap;
5527f3187195STejun Heo 	}
5528f3187195STejun Heo 
5529f3187195STejun Heo 	devres_remove_group(dev, NULL);
5530f3187195STejun Heo 	return host;
5531f3187195STejun Heo 
5532f3187195STejun Heo  err_out:
5533f3187195STejun Heo 	devres_release_group(dev, NULL);
5534dafd6c49SColin Ian King  err_free:
5535dafd6c49SColin Ian King 	kfree(host);
5536f3187195STejun Heo 	return NULL;
5537f3187195STejun Heo }
5538a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_alloc);
5539f3187195STejun Heo 
5540f3187195STejun Heo /**
5541f5cda257STejun Heo  *	ata_host_alloc_pinfo - alloc host and init with port_info array
5542f5cda257STejun Heo  *	@dev: generic device this host is associated with
5543f5cda257STejun Heo  *	@ppi: array of ATA port_info to initialize host with
5544f5cda257STejun Heo  *	@n_ports: number of ATA ports attached to this host
5545f5cda257STejun Heo  *
5546f5cda257STejun Heo  *	Allocate ATA host and initialize with info from @ppi.  If NULL
5547f5cda257STejun Heo  *	terminated, @ppi may contain fewer entries than @n_ports.  The
5548f5cda257STejun Heo  *	last entry will be used for the remaining ports.
5549f5cda257STejun Heo  *
5550f5cda257STejun Heo  *	RETURNS:
5551f5cda257STejun Heo  *	Allocate ATA host on success, NULL on failure.
5552f5cda257STejun Heo  *
5553f5cda257STejun Heo  *	LOCKING:
5554f5cda257STejun Heo  *	Inherited from calling layer (may sleep).
5555f5cda257STejun Heo  */
5556f5cda257STejun Heo struct ata_host *ata_host_alloc_pinfo(struct device *dev,
5557f5cda257STejun Heo 				      const struct ata_port_info * const * ppi,
5558f5cda257STejun Heo 				      int n_ports)
5559f5cda257STejun Heo {
5560bf476fe2SSergey Shtylyov 	const struct ata_port_info *pi = &ata_dummy_port_info;
5561f5cda257STejun Heo 	struct ata_host *host;
5562f5cda257STejun Heo 	int i, j;
5563f5cda257STejun Heo 
5564f5cda257STejun Heo 	host = ata_host_alloc(dev, n_ports);
5565f5cda257STejun Heo 	if (!host)
5566f5cda257STejun Heo 		return NULL;
5567f5cda257STejun Heo 
5568bf476fe2SSergey Shtylyov 	for (i = 0, j = 0; i < host->n_ports; i++) {
5569f5cda257STejun Heo 		struct ata_port *ap = host->ports[i];
5570f5cda257STejun Heo 
5571f5cda257STejun Heo 		if (ppi[j])
5572f5cda257STejun Heo 			pi = ppi[j++];
5573f5cda257STejun Heo 
5574f5cda257STejun Heo 		ap->pio_mask = pi->pio_mask;
5575f5cda257STejun Heo 		ap->mwdma_mask = pi->mwdma_mask;
5576f5cda257STejun Heo 		ap->udma_mask = pi->udma_mask;
5577f5cda257STejun Heo 		ap->flags |= pi->flags;
55780c88758bSTejun Heo 		ap->link.flags |= pi->link_flags;
5579f5cda257STejun Heo 		ap->ops = pi->port_ops;
5580f5cda257STejun Heo 
5581f5cda257STejun Heo 		if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
5582f5cda257STejun Heo 			host->ops = pi->port_ops;
5583f5cda257STejun Heo 	}
5584f5cda257STejun Heo 
5585f5cda257STejun Heo 	return host;
5586f5cda257STejun Heo }
5587a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
5588f5cda257STejun Heo 
558932ebbc0cSTejun Heo static void ata_host_stop(struct device *gendev, void *res)
559032ebbc0cSTejun Heo {
559132ebbc0cSTejun Heo 	struct ata_host *host = dev_get_drvdata(gendev);
559232ebbc0cSTejun Heo 	int i;
559332ebbc0cSTejun Heo 
559432ebbc0cSTejun Heo 	WARN_ON(!(host->flags & ATA_HOST_STARTED));
559532ebbc0cSTejun Heo 
559632ebbc0cSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
559732ebbc0cSTejun Heo 		struct ata_port *ap = host->ports[i];
559832ebbc0cSTejun Heo 
559932ebbc0cSTejun Heo 		if (ap->ops->port_stop)
560032ebbc0cSTejun Heo 			ap->ops->port_stop(ap);
560132ebbc0cSTejun Heo 	}
560232ebbc0cSTejun Heo 
560332ebbc0cSTejun Heo 	if (host->ops->host_stop)
560432ebbc0cSTejun Heo 		host->ops->host_stop(host);
560532ebbc0cSTejun Heo }
560632ebbc0cSTejun Heo 
5607f5cda257STejun Heo /**
5608029cfd6bSTejun Heo  *	ata_finalize_port_ops - finalize ata_port_operations
5609029cfd6bSTejun Heo  *	@ops: ata_port_operations to finalize
5610029cfd6bSTejun Heo  *
5611029cfd6bSTejun Heo  *	An ata_port_operations can inherit from another ops and that
5612029cfd6bSTejun Heo  *	ops can again inherit from another.  This can go on as many
5613029cfd6bSTejun Heo  *	times as necessary as long as there is no loop in the
5614029cfd6bSTejun Heo  *	inheritance chain.
5615029cfd6bSTejun Heo  *
5616029cfd6bSTejun Heo  *	Ops tables are finalized when the host is started.  NULL or
5617029cfd6bSTejun Heo  *	unspecified entries are inherited from the closet ancestor
5618029cfd6bSTejun Heo  *	which has the method and the entry is populated with it.
5619029cfd6bSTejun Heo  *	After finalization, the ops table directly points to all the
5620029cfd6bSTejun Heo  *	methods and ->inherits is no longer necessary and cleared.
5621029cfd6bSTejun Heo  *
5622029cfd6bSTejun Heo  *	Using ATA_OP_NULL, inheriting ops can force a method to NULL.
5623029cfd6bSTejun Heo  *
5624029cfd6bSTejun Heo  *	LOCKING:
5625029cfd6bSTejun Heo  *	None.
5626029cfd6bSTejun Heo  */
5627029cfd6bSTejun Heo static void ata_finalize_port_ops(struct ata_port_operations *ops)
5628029cfd6bSTejun Heo {
56292da67659SPradeep Singh Rautela 	static DEFINE_SPINLOCK(lock);
5630029cfd6bSTejun Heo 	const struct ata_port_operations *cur;
5631029cfd6bSTejun Heo 	void **begin = (void **)ops;
5632029cfd6bSTejun Heo 	void **end = (void **)&ops->inherits;
5633029cfd6bSTejun Heo 	void **pp;
5634029cfd6bSTejun Heo 
5635029cfd6bSTejun Heo 	if (!ops || !ops->inherits)
5636029cfd6bSTejun Heo 		return;
5637029cfd6bSTejun Heo 
5638029cfd6bSTejun Heo 	spin_lock(&lock);
5639029cfd6bSTejun Heo 
5640029cfd6bSTejun Heo 	for (cur = ops->inherits; cur; cur = cur->inherits) {
5641029cfd6bSTejun Heo 		void **inherit = (void **)cur;
5642029cfd6bSTejun Heo 
5643029cfd6bSTejun Heo 		for (pp = begin; pp < end; pp++, inherit++)
5644029cfd6bSTejun Heo 			if (!*pp)
5645029cfd6bSTejun Heo 				*pp = *inherit;
5646029cfd6bSTejun Heo 	}
5647029cfd6bSTejun Heo 
5648029cfd6bSTejun Heo 	for (pp = begin; pp < end; pp++)
5649029cfd6bSTejun Heo 		if (IS_ERR(*pp))
5650029cfd6bSTejun Heo 			*pp = NULL;
5651029cfd6bSTejun Heo 
5652029cfd6bSTejun Heo 	ops->inherits = NULL;
5653029cfd6bSTejun Heo 
5654029cfd6bSTejun Heo 	spin_unlock(&lock);
5655029cfd6bSTejun Heo }
5656029cfd6bSTejun Heo 
5657029cfd6bSTejun Heo /**
5658ecef7253STejun Heo  *	ata_host_start - start and freeze ports of an ATA host
5659ecef7253STejun Heo  *	@host: ATA host to start ports for
5660ecef7253STejun Heo  *
5661ecef7253STejun Heo  *	Start and then freeze ports of @host.  Started status is
5662ecef7253STejun Heo  *	recorded in host->flags, so this function can be called
5663ecef7253STejun Heo  *	multiple times.  Ports are guaranteed to get started only
5664e0af10acSDiego Viola  *	once.  If host->ops is not initialized yet, it is set to the
5665f3187195STejun Heo  *	first non-dummy port ops.
5666ecef7253STejun Heo  *
5667ecef7253STejun Heo  *	LOCKING:
5668ecef7253STejun Heo  *	Inherited from calling layer (may sleep).
5669ecef7253STejun Heo  *
5670ecef7253STejun Heo  *	RETURNS:
5671ecef7253STejun Heo  *	0 if all ports are started successfully, -errno otherwise.
5672ecef7253STejun Heo  */
5673ecef7253STejun Heo int ata_host_start(struct ata_host *host)
5674ecef7253STejun Heo {
567532ebbc0cSTejun Heo 	int have_stop = 0;
567632ebbc0cSTejun Heo 	void *start_dr = NULL;
5677ecef7253STejun Heo 	int i, rc;
5678ecef7253STejun Heo 
5679ecef7253STejun Heo 	if (host->flags & ATA_HOST_STARTED)
5680ecef7253STejun Heo 		return 0;
5681ecef7253STejun Heo 
5682029cfd6bSTejun Heo 	ata_finalize_port_ops(host->ops);
5683029cfd6bSTejun Heo 
5684ecef7253STejun Heo 	for (i = 0; i < host->n_ports; i++) {
5685ecef7253STejun Heo 		struct ata_port *ap = host->ports[i];
5686ecef7253STejun Heo 
5687029cfd6bSTejun Heo 		ata_finalize_port_ops(ap->ops);
5688029cfd6bSTejun Heo 
5689f3187195STejun Heo 		if (!host->ops && !ata_port_is_dummy(ap))
5690f3187195STejun Heo 			host->ops = ap->ops;
5691f3187195STejun Heo 
569232ebbc0cSTejun Heo 		if (ap->ops->port_stop)
569332ebbc0cSTejun Heo 			have_stop = 1;
569432ebbc0cSTejun Heo 	}
569532ebbc0cSTejun Heo 
5696355a8031SDamien Le Moal 	if (host->ops && host->ops->host_stop)
569732ebbc0cSTejun Heo 		have_stop = 1;
569832ebbc0cSTejun Heo 
569932ebbc0cSTejun Heo 	if (have_stop) {
570032ebbc0cSTejun Heo 		start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
570132ebbc0cSTejun Heo 		if (!start_dr)
570232ebbc0cSTejun Heo 			return -ENOMEM;
570332ebbc0cSTejun Heo 	}
570432ebbc0cSTejun Heo 
570532ebbc0cSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
570632ebbc0cSTejun Heo 		struct ata_port *ap = host->ports[i];
570732ebbc0cSTejun Heo 
5708ecef7253STejun Heo 		if (ap->ops->port_start) {
5709ecef7253STejun Heo 			rc = ap->ops->port_start(ap);
5710ecef7253STejun Heo 			if (rc) {
57110f9fe9b7SAlan Cox 				if (rc != -ENODEV)
5712a44fec1fSJoe Perches 					dev_err(host->dev,
5713a44fec1fSJoe Perches 						"failed to start port %d (errno=%d)\n",
5714a44fec1fSJoe Perches 						i, rc);
5715ecef7253STejun Heo 				goto err_out;
5716ecef7253STejun Heo 			}
5717ecef7253STejun Heo 		}
5718ecef7253STejun Heo 		ata_eh_freeze_port(ap);
5719ecef7253STejun Heo 	}
5720ecef7253STejun Heo 
572132ebbc0cSTejun Heo 	if (start_dr)
572232ebbc0cSTejun Heo 		devres_add(host->dev, start_dr);
5723ecef7253STejun Heo 	host->flags |= ATA_HOST_STARTED;
5724ecef7253STejun Heo 	return 0;
5725ecef7253STejun Heo 
5726ecef7253STejun Heo  err_out:
5727ecef7253STejun Heo 	while (--i >= 0) {
5728ecef7253STejun Heo 		struct ata_port *ap = host->ports[i];
5729ecef7253STejun Heo 
5730ecef7253STejun Heo 		if (ap->ops->port_stop)
5731ecef7253STejun Heo 			ap->ops->port_stop(ap);
5732ecef7253STejun Heo 	}
573332ebbc0cSTejun Heo 	devres_free(start_dr);
5734ecef7253STejun Heo 	return rc;
5735ecef7253STejun Heo }
5736a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_start);
5737ecef7253STejun Heo 
5738ecef7253STejun Heo /**
573994bd5719SMauro Carvalho Chehab  *	ata_host_init - Initialize a host struct for sas (ipr, libsas)
5740cca3974eSJeff Garzik  *	@host:	host to initialize
5741cca3974eSJeff Garzik  *	@dev:	device host is attached to
5742c6fd2807SJeff Garzik  *	@ops:	port_ops
5743c6fd2807SJeff Garzik  *
5744c6fd2807SJeff Garzik  */
5745cca3974eSJeff Garzik void ata_host_init(struct ata_host *host, struct device *dev,
57468d8e7d13SDan Williams 		   struct ata_port_operations *ops)
5747c6fd2807SJeff Garzik {
5748cca3974eSJeff Garzik 	spin_lock_init(&host->lock);
5749c0c362b6STejun Heo 	mutex_init(&host->eh_mutex);
575069278f79SJens Axboe 	host->n_tags = ATA_MAX_QUEUE;
5751cca3974eSJeff Garzik 	host->dev = dev;
5752cca3974eSJeff Garzik 	host->ops = ops;
57532fa4a326SJason Yan 	kref_init(&host->kref);
5754c6fd2807SJeff Garzik }
5755a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_init);
5756c6fd2807SJeff Garzik 
57579508a66fSDan Williams void __ata_port_probe(struct ata_port *ap)
575879318057SArjan van de Ven {
575979318057SArjan van de Ven 	struct ata_eh_info *ehi = &ap->link.eh_info;
576079318057SArjan van de Ven 	unsigned long flags;
576179318057SArjan van de Ven 
576279318057SArjan van de Ven 	/* kick EH for boot probing */
576379318057SArjan van de Ven 	spin_lock_irqsave(ap->lock, flags);
576479318057SArjan van de Ven 
576579318057SArjan van de Ven 	ehi->probe_mask |= ATA_ALL_DEVICES;
57666b7ae954STejun Heo 	ehi->action |= ATA_EH_RESET;
576779318057SArjan van de Ven 	ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
576879318057SArjan van de Ven 
576979318057SArjan van de Ven 	ap->pflags &= ~ATA_PFLAG_INITIALIZING;
577079318057SArjan van de Ven 	ap->pflags |= ATA_PFLAG_LOADING;
577179318057SArjan van de Ven 	ata_port_schedule_eh(ap);
577279318057SArjan van de Ven 
577379318057SArjan van de Ven 	spin_unlock_irqrestore(ap->lock, flags);
57749508a66fSDan Williams }
577579318057SArjan van de Ven 
57769508a66fSDan Williams int ata_port_probe(struct ata_port *ap)
57779508a66fSDan Williams {
57789508a66fSDan Williams 	int rc = 0;
57799508a66fSDan Williams 
57809508a66fSDan Williams 	if (ap->ops->error_handler) {
57819508a66fSDan Williams 		__ata_port_probe(ap);
578279318057SArjan van de Ven 		ata_port_wait_eh(ap);
578379318057SArjan van de Ven 	} else {
578479318057SArjan van de Ven 		rc = ata_bus_probe(ap);
5785238c9cf9SJames Bottomley 	}
5786238c9cf9SJames Bottomley 	return rc;
5787238c9cf9SJames Bottomley }
578879318057SArjan van de Ven 
5789238c9cf9SJames Bottomley 
5790238c9cf9SJames Bottomley static void async_port_probe(void *data, async_cookie_t cookie)
5791238c9cf9SJames Bottomley {
5792238c9cf9SJames Bottomley 	struct ata_port *ap = data;
5793238c9cf9SJames Bottomley 
5794238c9cf9SJames Bottomley 	/*
5795238c9cf9SJames Bottomley 	 * If we're not allowed to scan this host in parallel,
5796238c9cf9SJames Bottomley 	 * we need to wait until all previous scans have completed
5797238c9cf9SJames Bottomley 	 * before going further.
5798238c9cf9SJames Bottomley 	 * Jeff Garzik says this is only within a controller, so we
5799238c9cf9SJames Bottomley 	 * don't need to wait for port 0, only for later ports.
580079318057SArjan van de Ven 	 */
5801238c9cf9SJames Bottomley 	if (!(ap->host->flags & ATA_HOST_PARALLEL_SCAN) && ap->port_no != 0)
5802238c9cf9SJames Bottomley 		async_synchronize_cookie(cookie);
5803238c9cf9SJames Bottomley 
5804238c9cf9SJames Bottomley 	(void)ata_port_probe(ap);
5805f29d3b23SArjan van de Ven 
5806f29d3b23SArjan van de Ven 	/* in order to keep device order, we need to synchronize at this point */
5807f29d3b23SArjan van de Ven 	async_synchronize_cookie(cookie);
5808f29d3b23SArjan van de Ven 
5809f29d3b23SArjan van de Ven 	ata_scsi_scan_host(ap, 1);
581079318057SArjan van de Ven }
5811238c9cf9SJames Bottomley 
5812c6fd2807SJeff Garzik /**
5813f3187195STejun Heo  *	ata_host_register - register initialized ATA host
5814f3187195STejun Heo  *	@host: ATA host to register
5815f3187195STejun Heo  *	@sht: template for SCSI host
5816c6fd2807SJeff Garzik  *
5817f3187195STejun Heo  *	Register initialized ATA host.  @host is allocated using
5818f3187195STejun Heo  *	ata_host_alloc() and fully initialized by LLD.  This function
5819f3187195STejun Heo  *	starts ports, registers @host with ATA and SCSI layers and
5820f3187195STejun Heo  *	probe registered devices.
5821c6fd2807SJeff Garzik  *
5822c6fd2807SJeff Garzik  *	LOCKING:
5823f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5824c6fd2807SJeff Garzik  *
5825c6fd2807SJeff Garzik  *	RETURNS:
5826f3187195STejun Heo  *	0 on success, -errno otherwise.
5827c6fd2807SJeff Garzik  */
582825df73d9SBart Van Assche int ata_host_register(struct ata_host *host, const struct scsi_host_template *sht)
5829c6fd2807SJeff Garzik {
5830f3187195STejun Heo 	int i, rc;
5831c6fd2807SJeff Garzik 
583269278f79SJens Axboe 	host->n_tags = clamp(sht->can_queue, 1, ATA_MAX_QUEUE);
58331871ee13SKevin Hao 
5834f3187195STejun Heo 	/* host must have been started */
5835f3187195STejun Heo 	if (!(host->flags & ATA_HOST_STARTED)) {
5836a44fec1fSJoe Perches 		dev_err(host->dev, "BUG: trying to register unstarted host\n");
5837f3187195STejun Heo 		WARN_ON(1);
5838f3187195STejun Heo 		return -EINVAL;
583902f076aaSAlan Cox 	}
5840f0d36efdSTejun Heo 
5841f3187195STejun Heo 	/* Blow away unused ports.  This happens when LLD can't
5842f3187195STejun Heo 	 * determine the exact number of ports to allocate at
5843f3187195STejun Heo 	 * allocation time.
5844f3187195STejun Heo 	 */
5845f3187195STejun Heo 	for (i = host->n_ports; host->ports[i]; i++)
5846f3187195STejun Heo 		kfree(host->ports[i]);
5847f0d36efdSTejun Heo 
5848f3187195STejun Heo 	/* give ports names and add SCSI hosts */
5849e628dc99SDavid Milburn 	for (i = 0; i < host->n_ports; i++) {
585085d6725bSDan Williams 		host->ports[i]->print_id = atomic_inc_return(&ata_print_id);
5851e628dc99SDavid Milburn 		host->ports[i]->local_port_no = i + 1;
5852e628dc99SDavid Milburn 	}
5853d9027470SGwendal Grignou 
5854d9027470SGwendal Grignou 	/* Create associated sysfs transport objects  */
5855d9027470SGwendal Grignou 	for (i = 0; i < host->n_ports; i++) {
5856d9027470SGwendal Grignou 		rc = ata_tport_add(host->dev,host->ports[i]);
5857d9027470SGwendal Grignou 		if (rc) {
5858d9027470SGwendal Grignou 			goto err_tadd;
5859d9027470SGwendal Grignou 		}
5860d9027470SGwendal Grignou 	}
5861d9027470SGwendal Grignou 
5862f3187195STejun Heo 	rc = ata_scsi_add_hosts(host, sht);
5863ecef7253STejun Heo 	if (rc)
5864d9027470SGwendal Grignou 		goto err_tadd;
5865ecef7253STejun Heo 
5866f3187195STejun Heo 	/* set cable, sata_spd_limit and report */
5867cca3974eSJeff Garzik 	for (i = 0; i < host->n_ports; i++) {
5868cca3974eSJeff Garzik 		struct ata_port *ap = host->ports[i];
5869f0a6d77bSSergey Shtylyov 		unsigned int xfer_mask;
5870f3187195STejun Heo 
5871f3187195STejun Heo 		/* set SATA cable type if still unset */
5872f3187195STejun Heo 		if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
5873f3187195STejun Heo 			ap->cbl = ATA_CBL_SATA;
5874c6fd2807SJeff Garzik 
5875c6fd2807SJeff Garzik 		/* init sata_spd_limit to the current value */
58764fb37a25STejun Heo 		sata_link_init_spd(&ap->link);
5877b1c72916STejun Heo 		if (ap->slave_link)
5878b1c72916STejun Heo 			sata_link_init_spd(ap->slave_link);
5879c6fd2807SJeff Garzik 
5880cbcdd875STejun Heo 		/* print per-port info to dmesg */
5881f3187195STejun Heo 		xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
5882f3187195STejun Heo 					      ap->udma_mask);
5883f3187195STejun Heo 
5884abf6e8edSTejun Heo 		if (!ata_port_is_dummy(ap)) {
5885a9a79dfeSJoe Perches 			ata_port_info(ap, "%cATA max %s %s\n",
5886a16abc0bSTejun Heo 				      (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
5887f3187195STejun Heo 				      ata_mode_string(xfer_mask),
5888cbcdd875STejun Heo 				      ap->link.eh_info.desc);
5889abf6e8edSTejun Heo 			ata_ehi_clear_desc(&ap->link.eh_info);
5890abf6e8edSTejun Heo 		} else
5891a9a79dfeSJoe Perches 			ata_port_info(ap, "DUMMY\n");
5892c6fd2807SJeff Garzik 	}
5893c6fd2807SJeff Garzik 
5894f6005354SVegard Nossum 	/* perform each probe asynchronously */
5895f3187195STejun Heo 	for (i = 0; i < host->n_ports; i++) {
5896f3187195STejun Heo 		struct ata_port *ap = host->ports[i];
5897b5292111SKai-Heng Feng 		ap->cookie = async_schedule(async_port_probe, ap);
5898c6fd2807SJeff Garzik 	}
5899c6fd2807SJeff Garzik 
5900f3187195STejun Heo 	return 0;
5901d9027470SGwendal Grignou 
5902d9027470SGwendal Grignou  err_tadd:
5903d9027470SGwendal Grignou 	while (--i >= 0) {
5904d9027470SGwendal Grignou 		ata_tport_delete(host->ports[i]);
5905d9027470SGwendal Grignou 	}
5906d9027470SGwendal Grignou 	return rc;
5907d9027470SGwendal Grignou 
5908f3187195STejun Heo }
5909a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_register);
5910f3187195STejun Heo 
5911f3187195STejun Heo /**
5912f5cda257STejun Heo  *	ata_host_activate - start host, request IRQ and register it
5913f5cda257STejun Heo  *	@host: target ATA host
5914f5cda257STejun Heo  *	@irq: IRQ to request
5915f5cda257STejun Heo  *	@irq_handler: irq_handler used when requesting IRQ
5916f5cda257STejun Heo  *	@irq_flags: irq_flags used when requesting IRQ
5917f5cda257STejun Heo  *	@sht: scsi_host_template to use when registering the host
5918f5cda257STejun Heo  *
5919f5cda257STejun Heo  *	After allocating an ATA host and initializing it, most libata
5920f5cda257STejun Heo  *	LLDs perform three steps to activate the host - start host,
5921c9b5560aSMasanari Iida  *	request IRQ and register it.  This helper takes necessary
5922f5cda257STejun Heo  *	arguments and performs the three steps in one go.
5923f5cda257STejun Heo  *
59243d46b2e2SPaul Mundt  *	An invalid IRQ skips the IRQ registration and expects the host to
59253d46b2e2SPaul Mundt  *	have set polling mode on the port. In this case, @irq_handler
59263d46b2e2SPaul Mundt  *	should be NULL.
59273d46b2e2SPaul Mundt  *
5928f5cda257STejun Heo  *	LOCKING:
5929f5cda257STejun Heo  *	Inherited from calling layer (may sleep).
5930f5cda257STejun Heo  *
5931f5cda257STejun Heo  *	RETURNS:
5932f5cda257STejun Heo  *	0 on success, -errno otherwise.
5933f5cda257STejun Heo  */
5934f5cda257STejun Heo int ata_host_activate(struct ata_host *host, int irq,
5935f5cda257STejun Heo 		      irq_handler_t irq_handler, unsigned long irq_flags,
593625df73d9SBart Van Assche 		      const struct scsi_host_template *sht)
5937f5cda257STejun Heo {
5938cbcdd875STejun Heo 	int i, rc;
59397e22c002SHeiner Kallweit 	char *irq_desc;
5940f5cda257STejun Heo 
5941f5cda257STejun Heo 	rc = ata_host_start(host);
5942f5cda257STejun Heo 	if (rc)
5943f5cda257STejun Heo 		return rc;
5944f5cda257STejun Heo 
59453d46b2e2SPaul Mundt 	/* Special case for polling mode */
59463d46b2e2SPaul Mundt 	if (!irq) {
59473d46b2e2SPaul Mundt 		WARN_ON(irq_handler);
59483d46b2e2SPaul Mundt 		return ata_host_register(host, sht);
59493d46b2e2SPaul Mundt 	}
59503d46b2e2SPaul Mundt 
59517e22c002SHeiner Kallweit 	irq_desc = devm_kasprintf(host->dev, GFP_KERNEL, "%s[%s]",
59527e22c002SHeiner Kallweit 				  dev_driver_string(host->dev),
59537e22c002SHeiner Kallweit 				  dev_name(host->dev));
59547e22c002SHeiner Kallweit 	if (!irq_desc)
59557e22c002SHeiner Kallweit 		return -ENOMEM;
59567e22c002SHeiner Kallweit 
5957f5cda257STejun Heo 	rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
59587e22c002SHeiner Kallweit 			      irq_desc, host);
5959f5cda257STejun Heo 	if (rc)
5960f5cda257STejun Heo 		return rc;
5961f5cda257STejun Heo 
5962cbcdd875STejun Heo 	for (i = 0; i < host->n_ports; i++)
5963cbcdd875STejun Heo 		ata_port_desc(host->ports[i], "irq %d", irq);
59644031826bSTejun Heo 
5965f5cda257STejun Heo 	rc = ata_host_register(host, sht);
5966f5cda257STejun Heo 	/* if failed, just free the IRQ and leave ports alone */
5967f5cda257STejun Heo 	if (rc)
5968f5cda257STejun Heo 		devm_free_irq(host->dev, irq, host);
5969f5cda257STejun Heo 
5970f5cda257STejun Heo 	return rc;
5971f5cda257STejun Heo }
5972a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_activate);
5973f5cda257STejun Heo 
5974f5cda257STejun Heo /**
5975c9b5560aSMasanari Iida  *	ata_port_detach - Detach ATA port in preparation of device removal
5976c6fd2807SJeff Garzik  *	@ap: ATA port to be detached
5977c6fd2807SJeff Garzik  *
5978c6fd2807SJeff Garzik  *	Detach all ATA devices and the associated SCSI devices of @ap;
5979c6fd2807SJeff Garzik  *	then, remove the associated SCSI host.  @ap is guaranteed to
5980c6fd2807SJeff Garzik  *	be quiescent on return from this function.
5981c6fd2807SJeff Garzik  *
5982c6fd2807SJeff Garzik  *	LOCKING:
5983c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
5984c6fd2807SJeff Garzik  */
5985741b7763SAdrian Bunk static void ata_port_detach(struct ata_port *ap)
5986c6fd2807SJeff Garzik {
5987c6fd2807SJeff Garzik 	unsigned long flags;
5988a6f9bf4dSLevente Kurusa 	struct ata_link *link;
5989a6f9bf4dSLevente Kurusa 	struct ata_device *dev;
5990c6fd2807SJeff Garzik 
5991c6fd2807SJeff Garzik 	if (!ap->ops->error_handler)
5992c6fd2807SJeff Garzik 		goto skip_eh;
5993c6fd2807SJeff Garzik 
5994c6fd2807SJeff Garzik 	/* tell EH we're leaving & flush EH */
5995c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
5996c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_UNLOADING;
5997ece180d1STejun Heo 	ata_port_schedule_eh(ap);
5998c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
5999c6fd2807SJeff Garzik 
6000ece180d1STejun Heo 	/* wait till EH commits suicide */
6001c6fd2807SJeff Garzik 	ata_port_wait_eh(ap);
6002c6fd2807SJeff Garzik 
6003ece180d1STejun Heo 	/* it better be dead now */
6004ece180d1STejun Heo 	WARN_ON(!(ap->pflags & ATA_PFLAG_UNLOADED));
6005c6fd2807SJeff Garzik 
6006afe2c511STejun Heo 	cancel_delayed_work_sync(&ap->hotplug_task);
6007c6fd2807SJeff Garzik 
6008c6fd2807SJeff Garzik  skip_eh:
6009a6f9bf4dSLevente Kurusa 	/* clean up zpodd on port removal */
6010a6f9bf4dSLevente Kurusa 	ata_for_each_link(link, ap, HOST_FIRST) {
6011a6f9bf4dSLevente Kurusa 		ata_for_each_dev(dev, link, ALL) {
6012a6f9bf4dSLevente Kurusa 			if (zpodd_dev_enabled(dev))
6013a6f9bf4dSLevente Kurusa 				zpodd_exit(dev);
6014a6f9bf4dSLevente Kurusa 		}
6015a6f9bf4dSLevente Kurusa 	}
6016d9027470SGwendal Grignou 	if (ap->pmp_link) {
6017d9027470SGwendal Grignou 		int i;
6018d9027470SGwendal Grignou 		for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
6019d9027470SGwendal Grignou 			ata_tlink_delete(&ap->pmp_link[i]);
6020d9027470SGwendal Grignou 	}
6021c6fd2807SJeff Garzik 	/* remove the associated SCSI host */
6022cca3974eSJeff Garzik 	scsi_remove_host(ap->scsi_host);
6023c5700766SRafael J. Wysocki 	ata_tport_delete(ap);
6024c6fd2807SJeff Garzik }
6025c6fd2807SJeff Garzik 
6026c6fd2807SJeff Garzik /**
60270529c159STejun Heo  *	ata_host_detach - Detach all ports of an ATA host
60280529c159STejun Heo  *	@host: Host to detach
60290529c159STejun Heo  *
60300529c159STejun Heo  *	Detach all ports of @host.
60310529c159STejun Heo  *
60320529c159STejun Heo  *	LOCKING:
60330529c159STejun Heo  *	Kernel thread context (may sleep).
60340529c159STejun Heo  */
60350529c159STejun Heo void ata_host_detach(struct ata_host *host)
60360529c159STejun Heo {
60370529c159STejun Heo 	int i;
60380529c159STejun Heo 
6039b5292111SKai-Heng Feng 	for (i = 0; i < host->n_ports; i++) {
6040130f4cafSJohn Garry 		/* Ensure ata_port probe has completed */
6041b5292111SKai-Heng Feng 		async_synchronize_cookie(host->ports[i]->cookie + 1);
60420529c159STejun Heo 		ata_port_detach(host->ports[i]);
6043b5292111SKai-Heng Feng 	}
6044562f0c2dSTejun Heo 
6045562f0c2dSTejun Heo 	/* the host is dead now, dissociate ACPI */
6046562f0c2dSTejun Heo 	ata_acpi_dissociate(host);
60470529c159STejun Heo }
6048a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_detach);
60490529c159STejun Heo 
6050c6fd2807SJeff Garzik #ifdef CONFIG_PCI
6051c6fd2807SJeff Garzik 
6052c6fd2807SJeff Garzik /**
6053c6fd2807SJeff Garzik  *	ata_pci_remove_one - PCI layer callback for device removal
6054c6fd2807SJeff Garzik  *	@pdev: PCI device that was removed
6055c6fd2807SJeff Garzik  *
6056b878ca5dSTejun Heo  *	PCI layer indicates to libata via this hook that hot-unplug or
6057b878ca5dSTejun Heo  *	module unload event has occurred.  Detach all ports.  Resource
6058b878ca5dSTejun Heo  *	release is handled via devres.
6059c6fd2807SJeff Garzik  *
6060c6fd2807SJeff Garzik  *	LOCKING:
6061c6fd2807SJeff Garzik  *	Inherited from PCI layer (may sleep).
6062c6fd2807SJeff Garzik  */
6063c6fd2807SJeff Garzik void ata_pci_remove_one(struct pci_dev *pdev)
6064c6fd2807SJeff Garzik {
606504a3f5b7SBrian Norris 	struct ata_host *host = pci_get_drvdata(pdev);
6066c6fd2807SJeff Garzik 
6067f0d36efdSTejun Heo 	ata_host_detach(host);
6068c6fd2807SJeff Garzik }
6069a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pci_remove_one);
6070c6fd2807SJeff Garzik 
607110a663a1SPrabhakar Kushwaha void ata_pci_shutdown_one(struct pci_dev *pdev)
607210a663a1SPrabhakar Kushwaha {
607310a663a1SPrabhakar Kushwaha 	struct ata_host *host = pci_get_drvdata(pdev);
607410a663a1SPrabhakar Kushwaha 	int i;
607510a663a1SPrabhakar Kushwaha 
607610a663a1SPrabhakar Kushwaha 	for (i = 0; i < host->n_ports; i++) {
607710a663a1SPrabhakar Kushwaha 		struct ata_port *ap = host->ports[i];
607810a663a1SPrabhakar Kushwaha 
607910a663a1SPrabhakar Kushwaha 		ap->pflags |= ATA_PFLAG_FROZEN;
608010a663a1SPrabhakar Kushwaha 
608110a663a1SPrabhakar Kushwaha 		/* Disable port interrupts */
608210a663a1SPrabhakar Kushwaha 		if (ap->ops->freeze)
608310a663a1SPrabhakar Kushwaha 			ap->ops->freeze(ap);
608410a663a1SPrabhakar Kushwaha 
608510a663a1SPrabhakar Kushwaha 		/* Stop the port DMA engines */
608610a663a1SPrabhakar Kushwaha 		if (ap->ops->port_stop)
608710a663a1SPrabhakar Kushwaha 			ap->ops->port_stop(ap);
608810a663a1SPrabhakar Kushwaha 	}
608910a663a1SPrabhakar Kushwaha }
6090a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pci_shutdown_one);
609110a663a1SPrabhakar Kushwaha 
6092c6fd2807SJeff Garzik /* move to PCI subsystem */
6093c6fd2807SJeff Garzik int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
6094c6fd2807SJeff Garzik {
6095c6fd2807SJeff Garzik 	unsigned long tmp = 0;
6096c6fd2807SJeff Garzik 
6097c6fd2807SJeff Garzik 	switch (bits->width) {
6098c6fd2807SJeff Garzik 	case 1: {
6099c6fd2807SJeff Garzik 		u8 tmp8 = 0;
6100c6fd2807SJeff Garzik 		pci_read_config_byte(pdev, bits->reg, &tmp8);
6101c6fd2807SJeff Garzik 		tmp = tmp8;
6102c6fd2807SJeff Garzik 		break;
6103c6fd2807SJeff Garzik 	}
6104c6fd2807SJeff Garzik 	case 2: {
6105c6fd2807SJeff Garzik 		u16 tmp16 = 0;
6106c6fd2807SJeff Garzik 		pci_read_config_word(pdev, bits->reg, &tmp16);
6107c6fd2807SJeff Garzik 		tmp = tmp16;
6108c6fd2807SJeff Garzik 		break;
6109c6fd2807SJeff Garzik 	}
6110c6fd2807SJeff Garzik 	case 4: {
6111c6fd2807SJeff Garzik 		u32 tmp32 = 0;
6112c6fd2807SJeff Garzik 		pci_read_config_dword(pdev, bits->reg, &tmp32);
6113c6fd2807SJeff Garzik 		tmp = tmp32;
6114c6fd2807SJeff Garzik 		break;
6115c6fd2807SJeff Garzik 	}
6116c6fd2807SJeff Garzik 
6117c6fd2807SJeff Garzik 	default:
6118c6fd2807SJeff Garzik 		return -EINVAL;
6119c6fd2807SJeff Garzik 	}
6120c6fd2807SJeff Garzik 
6121c6fd2807SJeff Garzik 	tmp &= bits->mask;
6122c6fd2807SJeff Garzik 
6123c6fd2807SJeff Garzik 	return (tmp == bits->val) ? 1 : 0;
6124c6fd2807SJeff Garzik }
6125a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(pci_test_config_bits);
6126c6fd2807SJeff Garzik 
61276ffa01d8STejun Heo #ifdef CONFIG_PM
6128c6fd2807SJeff Garzik void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
6129c6fd2807SJeff Garzik {
6130c6fd2807SJeff Garzik 	pci_save_state(pdev);
6131c6fd2807SJeff Garzik 	pci_disable_device(pdev);
61324c90d971STejun Heo 
61333a2d5b70SRafael J. Wysocki 	if (mesg.event & PM_EVENT_SLEEP)
6134c6fd2807SJeff Garzik 		pci_set_power_state(pdev, PCI_D3hot);
6135c6fd2807SJeff Garzik }
6136a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
6137c6fd2807SJeff Garzik 
6138553c4aa6STejun Heo int ata_pci_device_do_resume(struct pci_dev *pdev)
6139c6fd2807SJeff Garzik {
6140553c4aa6STejun Heo 	int rc;
6141553c4aa6STejun Heo 
6142c6fd2807SJeff Garzik 	pci_set_power_state(pdev, PCI_D0);
6143c6fd2807SJeff Garzik 	pci_restore_state(pdev);
6144553c4aa6STejun Heo 
6145f0d36efdSTejun Heo 	rc = pcim_enable_device(pdev);
6146553c4aa6STejun Heo 	if (rc) {
6147a44fec1fSJoe Perches 		dev_err(&pdev->dev,
6148553c4aa6STejun Heo 			"failed to enable device after resume (%d)\n", rc);
6149553c4aa6STejun Heo 		return rc;
6150553c4aa6STejun Heo 	}
6151553c4aa6STejun Heo 
6152c6fd2807SJeff Garzik 	pci_set_master(pdev);
6153553c4aa6STejun Heo 	return 0;
6154c6fd2807SJeff Garzik }
6155a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
6156c6fd2807SJeff Garzik 
6157c6fd2807SJeff Garzik int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
6158c6fd2807SJeff Garzik {
615904a3f5b7SBrian Norris 	struct ata_host *host = pci_get_drvdata(pdev);
6160c6fd2807SJeff Garzik 
6161ec87cf37SSergey Shtylyov 	ata_host_suspend(host, mesg);
6162c6fd2807SJeff Garzik 
6163c6fd2807SJeff Garzik 	ata_pci_device_do_suspend(pdev, mesg);
6164c6fd2807SJeff Garzik 
6165c6fd2807SJeff Garzik 	return 0;
6166c6fd2807SJeff Garzik }
6167a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
6168c6fd2807SJeff Garzik 
6169c6fd2807SJeff Garzik int ata_pci_device_resume(struct pci_dev *pdev)
6170c6fd2807SJeff Garzik {
617104a3f5b7SBrian Norris 	struct ata_host *host = pci_get_drvdata(pdev);
6172553c4aa6STejun Heo 	int rc;
6173c6fd2807SJeff Garzik 
6174553c4aa6STejun Heo 	rc = ata_pci_device_do_resume(pdev);
6175553c4aa6STejun Heo 	if (rc == 0)
6176cca3974eSJeff Garzik 		ata_host_resume(host);
6177553c4aa6STejun Heo 	return rc;
6178c6fd2807SJeff Garzik }
6179a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pci_device_resume);
61806ffa01d8STejun Heo #endif /* CONFIG_PM */
6181c6fd2807SJeff Garzik #endif /* CONFIG_PCI */
6182c6fd2807SJeff Garzik 
6183b7db04d9SBrian Norris /**
6184b7db04d9SBrian Norris  *	ata_platform_remove_one - Platform layer callback for device removal
6185b7db04d9SBrian Norris  *	@pdev: Platform device that was removed
6186b7db04d9SBrian Norris  *
6187b7db04d9SBrian Norris  *	Platform layer indicates to libata via this hook that hot-unplug or
6188b7db04d9SBrian Norris  *	module unload event has occurred.  Detach all ports.  Resource
6189b7db04d9SBrian Norris  *	release is handled via devres.
6190b7db04d9SBrian Norris  *
6191b7db04d9SBrian Norris  *	LOCKING:
6192b7db04d9SBrian Norris  *	Inherited from platform layer (may sleep).
6193b7db04d9SBrian Norris  */
6194b7db04d9SBrian Norris int ata_platform_remove_one(struct platform_device *pdev)
6195b7db04d9SBrian Norris {
6196b7db04d9SBrian Norris 	struct ata_host *host = platform_get_drvdata(pdev);
6197b7db04d9SBrian Norris 
6198b7db04d9SBrian Norris 	ata_host_detach(host);
6199b7db04d9SBrian Norris 
6200b7db04d9SBrian Norris 	return 0;
6201b7db04d9SBrian Norris }
6202a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_platform_remove_one);
6203b7db04d9SBrian Norris 
6204bf89b0bfSBartlomiej Zolnierkiewicz #ifdef CONFIG_ATA_FORCE
6205168af4afSDamien Le Moal 
6206168af4afSDamien Le Moal #define force_cbl(name, flag)				\
6207168af4afSDamien Le Moal 	{ #name,	.cbl		= (flag) }
6208168af4afSDamien Le Moal 
6209168af4afSDamien Le Moal #define force_spd_limit(spd, val)			\
6210168af4afSDamien Le Moal 	{ #spd,	.spd_limit		= (val) }
6211168af4afSDamien Le Moal 
6212168af4afSDamien Le Moal #define force_xfer(mode, shift)				\
6213168af4afSDamien Le Moal 	{ #mode,	.xfer_mask	= (1UL << (shift)) }
6214168af4afSDamien Le Moal 
62153af9ca4dSDamien Le Moal #define force_lflag_on(name, flags)			\
62163af9ca4dSDamien Le Moal 	{ #name,	.lflags_on	= (flags) }
62173af9ca4dSDamien Le Moal 
62183af9ca4dSDamien Le Moal #define force_lflag_onoff(name, flags)			\
62193af9ca4dSDamien Le Moal 	{ "no" #name,	.lflags_on	= (flags) },	\
62203af9ca4dSDamien Le Moal 	{ #name,	.lflags_off	= (flags) }
6221168af4afSDamien Le Moal 
6222168af4afSDamien Le Moal #define force_horkage_on(name, flag)			\
6223168af4afSDamien Le Moal 	{ #name,	.horkage_on	= (flag) }
6224168af4afSDamien Le Moal 
6225168af4afSDamien Le Moal #define force_horkage_onoff(name, flag)			\
6226168af4afSDamien Le Moal 	{ "no" #name,	.horkage_on	= (flag) },	\
6227168af4afSDamien Le Moal 	{ #name,	.horkage_off	= (flag) }
6228168af4afSDamien Le Moal 
6229168af4afSDamien Le Moal static const struct ata_force_param force_tbl[] __initconst = {
6230168af4afSDamien Le Moal 	force_cbl(40c,			ATA_CBL_PATA40),
6231168af4afSDamien Le Moal 	force_cbl(80c,			ATA_CBL_PATA80),
6232168af4afSDamien Le Moal 	force_cbl(short40c,		ATA_CBL_PATA40_SHORT),
6233168af4afSDamien Le Moal 	force_cbl(unk,			ATA_CBL_PATA_UNK),
6234168af4afSDamien Le Moal 	force_cbl(ign,			ATA_CBL_PATA_IGN),
6235168af4afSDamien Le Moal 	force_cbl(sata,			ATA_CBL_SATA),
6236168af4afSDamien Le Moal 
6237168af4afSDamien Le Moal 	force_spd_limit(1.5Gbps,	1),
6238168af4afSDamien Le Moal 	force_spd_limit(3.0Gbps,	2),
6239168af4afSDamien Le Moal 
6240168af4afSDamien Le Moal 	force_xfer(pio0,		ATA_SHIFT_PIO + 0),
6241168af4afSDamien Le Moal 	force_xfer(pio1,		ATA_SHIFT_PIO + 1),
6242168af4afSDamien Le Moal 	force_xfer(pio2,		ATA_SHIFT_PIO + 2),
6243168af4afSDamien Le Moal 	force_xfer(pio3,		ATA_SHIFT_PIO + 3),
6244168af4afSDamien Le Moal 	force_xfer(pio4,		ATA_SHIFT_PIO + 4),
6245168af4afSDamien Le Moal 	force_xfer(pio5,		ATA_SHIFT_PIO + 5),
6246168af4afSDamien Le Moal 	force_xfer(pio6,		ATA_SHIFT_PIO + 6),
6247168af4afSDamien Le Moal 	force_xfer(mwdma0,		ATA_SHIFT_MWDMA + 0),
6248168af4afSDamien Le Moal 	force_xfer(mwdma1,		ATA_SHIFT_MWDMA + 1),
6249168af4afSDamien Le Moal 	force_xfer(mwdma2,		ATA_SHIFT_MWDMA + 2),
6250168af4afSDamien Le Moal 	force_xfer(mwdma3,		ATA_SHIFT_MWDMA + 3),
6251168af4afSDamien Le Moal 	force_xfer(mwdma4,		ATA_SHIFT_MWDMA + 4),
6252168af4afSDamien Le Moal 	force_xfer(udma0,		ATA_SHIFT_UDMA + 0),
6253168af4afSDamien Le Moal 	force_xfer(udma16,		ATA_SHIFT_UDMA + 0),
6254168af4afSDamien Le Moal 	force_xfer(udma/16,		ATA_SHIFT_UDMA + 0),
6255168af4afSDamien Le Moal 	force_xfer(udma1,		ATA_SHIFT_UDMA + 1),
6256168af4afSDamien Le Moal 	force_xfer(udma25,		ATA_SHIFT_UDMA + 1),
6257168af4afSDamien Le Moal 	force_xfer(udma/25,		ATA_SHIFT_UDMA + 1),
6258168af4afSDamien Le Moal 	force_xfer(udma2,		ATA_SHIFT_UDMA + 2),
6259168af4afSDamien Le Moal 	force_xfer(udma33,		ATA_SHIFT_UDMA + 2),
6260168af4afSDamien Le Moal 	force_xfer(udma/33,		ATA_SHIFT_UDMA + 2),
6261168af4afSDamien Le Moal 	force_xfer(udma3,		ATA_SHIFT_UDMA + 3),
6262168af4afSDamien Le Moal 	force_xfer(udma44,		ATA_SHIFT_UDMA + 3),
6263168af4afSDamien Le Moal 	force_xfer(udma/44,		ATA_SHIFT_UDMA + 3),
6264168af4afSDamien Le Moal 	force_xfer(udma4,		ATA_SHIFT_UDMA + 4),
6265168af4afSDamien Le Moal 	force_xfer(udma66,		ATA_SHIFT_UDMA + 4),
6266168af4afSDamien Le Moal 	force_xfer(udma/66,		ATA_SHIFT_UDMA + 4),
6267168af4afSDamien Le Moal 	force_xfer(udma5,		ATA_SHIFT_UDMA + 5),
6268168af4afSDamien Le Moal 	force_xfer(udma100,		ATA_SHIFT_UDMA + 5),
6269168af4afSDamien Le Moal 	force_xfer(udma/100,		ATA_SHIFT_UDMA + 5),
6270168af4afSDamien Le Moal 	force_xfer(udma6,		ATA_SHIFT_UDMA + 6),
6271168af4afSDamien Le Moal 	force_xfer(udma133,		ATA_SHIFT_UDMA + 6),
6272168af4afSDamien Le Moal 	force_xfer(udma/133,		ATA_SHIFT_UDMA + 6),
6273168af4afSDamien Le Moal 	force_xfer(udma7,		ATA_SHIFT_UDMA + 7),
6274168af4afSDamien Le Moal 
62753af9ca4dSDamien Le Moal 	force_lflag_on(nohrst,		ATA_LFLAG_NO_HRST),
62763af9ca4dSDamien Le Moal 	force_lflag_on(nosrst,		ATA_LFLAG_NO_SRST),
62773af9ca4dSDamien Le Moal 	force_lflag_on(norst,		ATA_LFLAG_NO_HRST | ATA_LFLAG_NO_SRST),
62783af9ca4dSDamien Le Moal 	force_lflag_on(rstonce,		ATA_LFLAG_RST_ONCE),
62793af9ca4dSDamien Le Moal 	force_lflag_onoff(dbdelay,	ATA_LFLAG_NO_DEBOUNCE_DELAY),
6280168af4afSDamien Le Moal 
6281168af4afSDamien Le Moal 	force_horkage_onoff(ncq,	ATA_HORKAGE_NONCQ),
6282168af4afSDamien Le Moal 	force_horkage_onoff(ncqtrim,	ATA_HORKAGE_NO_NCQ_TRIM),
6283168af4afSDamien Le Moal 	force_horkage_onoff(ncqati,	ATA_HORKAGE_NO_NCQ_ON_ATI),
6284168af4afSDamien Le Moal 
62852c33bbdaSDamien Le Moal 	force_horkage_onoff(trim,	ATA_HORKAGE_NOTRIM),
62862c33bbdaSDamien Le Moal 	force_horkage_on(trim_zero,	ATA_HORKAGE_ZERO_AFTER_TRIM),
62872c33bbdaSDamien Le Moal 	force_horkage_on(max_trim_128m, ATA_HORKAGE_MAX_TRIM_128M),
62882c33bbdaSDamien Le Moal 
62892c33bbdaSDamien Le Moal 	force_horkage_onoff(dma,	ATA_HORKAGE_NODMA),
6290168af4afSDamien Le Moal 	force_horkage_on(atapi_dmadir,	ATA_HORKAGE_ATAPI_DMADIR),
62912c33bbdaSDamien Le Moal 	force_horkage_on(atapi_mod16_dma, ATA_HORKAGE_ATAPI_MOD16_DMA),
62922c33bbdaSDamien Le Moal 
62932c33bbdaSDamien Le Moal 	force_horkage_onoff(dmalog,	ATA_HORKAGE_NO_DMA_LOG),
62942c33bbdaSDamien Le Moal 	force_horkage_onoff(iddevlog,	ATA_HORKAGE_NO_ID_DEV_LOG),
62952c33bbdaSDamien Le Moal 	force_horkage_onoff(logdir,	ATA_HORKAGE_NO_LOG_DIR),
62962c33bbdaSDamien Le Moal 
62972c33bbdaSDamien Le Moal 	force_horkage_on(max_sec_128,	ATA_HORKAGE_MAX_SEC_128),
62982c33bbdaSDamien Le Moal 	force_horkage_on(max_sec_1024,	ATA_HORKAGE_MAX_SEC_1024),
62992c33bbdaSDamien Le Moal 	force_horkage_on(max_sec_lba48,	ATA_HORKAGE_MAX_SEC_LBA48),
63002c33bbdaSDamien Le Moal 
63012c33bbdaSDamien Le Moal 	force_horkage_onoff(lpm,	ATA_HORKAGE_NOLPM),
63022c33bbdaSDamien Le Moal 	force_horkage_onoff(setxfer,	ATA_HORKAGE_NOSETXFER),
63032c33bbdaSDamien Le Moal 	force_horkage_on(dump_id,	ATA_HORKAGE_DUMP_ID),
63044d2e4980SDamien Le Moal 	force_horkage_onoff(fua,	ATA_HORKAGE_NO_FUA),
63052c33bbdaSDamien Le Moal 
63062c33bbdaSDamien Le Moal 	force_horkage_on(disable,	ATA_HORKAGE_DISABLE),
6307168af4afSDamien Le Moal };
6308168af4afSDamien Le Moal 
630933267325STejun Heo static int __init ata_parse_force_one(char **cur,
631033267325STejun Heo 				      struct ata_force_ent *force_ent,
631133267325STejun Heo 				      const char **reason)
631233267325STejun Heo {
631333267325STejun Heo 	char *start = *cur, *p = *cur;
631433267325STejun Heo 	char *id, *val, *endp;
631533267325STejun Heo 	const struct ata_force_param *match_fp = NULL;
631633267325STejun Heo 	int nr_matches = 0, i;
631733267325STejun Heo 
631833267325STejun Heo 	/* find where this param ends and update *cur */
631933267325STejun Heo 	while (*p != '\0' && *p != ',')
632033267325STejun Heo 		p++;
632133267325STejun Heo 
632233267325STejun Heo 	if (*p == '\0')
632333267325STejun Heo 		*cur = p;
632433267325STejun Heo 	else
632533267325STejun Heo 		*cur = p + 1;
632633267325STejun Heo 
632733267325STejun Heo 	*p = '\0';
632833267325STejun Heo 
632933267325STejun Heo 	/* parse */
633033267325STejun Heo 	p = strchr(start, ':');
633133267325STejun Heo 	if (!p) {
633233267325STejun Heo 		val = strstrip(start);
633333267325STejun Heo 		goto parse_val;
633433267325STejun Heo 	}
633533267325STejun Heo 	*p = '\0';
633633267325STejun Heo 
633733267325STejun Heo 	id = strstrip(start);
633833267325STejun Heo 	val = strstrip(p + 1);
633933267325STejun Heo 
634033267325STejun Heo 	/* parse id */
634133267325STejun Heo 	p = strchr(id, '.');
634233267325STejun Heo 	if (p) {
634333267325STejun Heo 		*p++ = '\0';
634433267325STejun Heo 		force_ent->device = simple_strtoul(p, &endp, 10);
634533267325STejun Heo 		if (p == endp || *endp != '\0') {
634633267325STejun Heo 			*reason = "invalid device";
634733267325STejun Heo 			return -EINVAL;
634833267325STejun Heo 		}
634933267325STejun Heo 	}
635033267325STejun Heo 
635133267325STejun Heo 	force_ent->port = simple_strtoul(id, &endp, 10);
6352f7cf69aeSTejun Heo 	if (id == endp || *endp != '\0') {
635333267325STejun Heo 		*reason = "invalid port/link";
635433267325STejun Heo 		return -EINVAL;
635533267325STejun Heo 	}
635633267325STejun Heo 
635733267325STejun Heo  parse_val:
635833267325STejun Heo 	/* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */
635933267325STejun Heo 	for (i = 0; i < ARRAY_SIZE(force_tbl); i++) {
636033267325STejun Heo 		const struct ata_force_param *fp = &force_tbl[i];
636133267325STejun Heo 
636233267325STejun Heo 		if (strncasecmp(val, fp->name, strlen(val)))
636333267325STejun Heo 			continue;
636433267325STejun Heo 
636533267325STejun Heo 		nr_matches++;
636633267325STejun Heo 		match_fp = fp;
636733267325STejun Heo 
636833267325STejun Heo 		if (strcasecmp(val, fp->name) == 0) {
636933267325STejun Heo 			nr_matches = 1;
637033267325STejun Heo 			break;
637133267325STejun Heo 		}
637233267325STejun Heo 	}
637333267325STejun Heo 
637433267325STejun Heo 	if (!nr_matches) {
637533267325STejun Heo 		*reason = "unknown value";
637633267325STejun Heo 		return -EINVAL;
637733267325STejun Heo 	}
637833267325STejun Heo 	if (nr_matches > 1) {
63799de55351SArvind Yadav 		*reason = "ambiguous value";
638033267325STejun Heo 		return -EINVAL;
638133267325STejun Heo 	}
638233267325STejun Heo 
638333267325STejun Heo 	force_ent->param = *match_fp;
638433267325STejun Heo 
638533267325STejun Heo 	return 0;
638633267325STejun Heo }
638733267325STejun Heo 
638833267325STejun Heo static void __init ata_parse_force_param(void)
638933267325STejun Heo {
639033267325STejun Heo 	int idx = 0, size = 1;
639133267325STejun Heo 	int last_port = -1, last_device = -1;
639233267325STejun Heo 	char *p, *cur, *next;
639333267325STejun Heo 
6394168af4afSDamien Le Moal 	/* Calculate maximum number of params and allocate ata_force_tbl */
639533267325STejun Heo 	for (p = ata_force_param_buf; *p; p++)
639633267325STejun Heo 		if (*p == ',')
639733267325STejun Heo 			size++;
639833267325STejun Heo 
63996396bb22SKees Cook 	ata_force_tbl = kcalloc(size, sizeof(ata_force_tbl[0]), GFP_KERNEL);
640033267325STejun Heo 	if (!ata_force_tbl) {
640133267325STejun Heo 		printk(KERN_WARNING "ata: failed to extend force table, "
640233267325STejun Heo 		       "libata.force ignored\n");
640333267325STejun Heo 		return;
640433267325STejun Heo 	}
640533267325STejun Heo 
640633267325STejun Heo 	/* parse and populate the table */
640733267325STejun Heo 	for (cur = ata_force_param_buf; *cur != '\0'; cur = next) {
640833267325STejun Heo 		const char *reason = "";
640933267325STejun Heo 		struct ata_force_ent te = { .port = -1, .device = -1 };
641033267325STejun Heo 
641133267325STejun Heo 		next = cur;
641233267325STejun Heo 		if (ata_parse_force_one(&next, &te, &reason)) {
641333267325STejun Heo 			printk(KERN_WARNING "ata: failed to parse force "
641433267325STejun Heo 			       "parameter \"%s\" (%s)\n",
641533267325STejun Heo 			       cur, reason);
641633267325STejun Heo 			continue;
641733267325STejun Heo 		}
641833267325STejun Heo 
641933267325STejun Heo 		if (te.port == -1) {
642033267325STejun Heo 			te.port = last_port;
642133267325STejun Heo 			te.device = last_device;
642233267325STejun Heo 		}
642333267325STejun Heo 
642433267325STejun Heo 		ata_force_tbl[idx++] = te;
642533267325STejun Heo 
642633267325STejun Heo 		last_port = te.port;
642733267325STejun Heo 		last_device = te.device;
642833267325STejun Heo 	}
642933267325STejun Heo 
643033267325STejun Heo 	ata_force_tbl_size = idx;
643133267325STejun Heo }
6432c6fd2807SJeff Garzik 
6433bf89b0bfSBartlomiej Zolnierkiewicz static void ata_free_force_param(void)
6434bf89b0bfSBartlomiej Zolnierkiewicz {
6435bf89b0bfSBartlomiej Zolnierkiewicz 	kfree(ata_force_tbl);
6436bf89b0bfSBartlomiej Zolnierkiewicz }
6437bf89b0bfSBartlomiej Zolnierkiewicz #else
6438bf89b0bfSBartlomiej Zolnierkiewicz static inline void ata_parse_force_param(void) { }
6439bf89b0bfSBartlomiej Zolnierkiewicz static inline void ata_free_force_param(void) { }
6440bf89b0bfSBartlomiej Zolnierkiewicz #endif
6441bf89b0bfSBartlomiej Zolnierkiewicz 
6442c6fd2807SJeff Garzik static int __init ata_init(void)
6443c6fd2807SJeff Garzik {
6444d9027470SGwendal Grignou 	int rc;
6445270390e1STejun Heo 
644633267325STejun Heo 	ata_parse_force_param();
644733267325STejun Heo 
6448270390e1STejun Heo 	rc = ata_sff_init();
6449ad72cf98STejun Heo 	if (rc) {
6450bf89b0bfSBartlomiej Zolnierkiewicz 		ata_free_force_param();
6451ad72cf98STejun Heo 		return rc;
6452ad72cf98STejun Heo 	}
6453c6fd2807SJeff Garzik 
6454d9027470SGwendal Grignou 	libata_transport_init();
6455d9027470SGwendal Grignou 	ata_scsi_transport_template = ata_attach_transport();
6456d9027470SGwendal Grignou 	if (!ata_scsi_transport_template) {
6457d9027470SGwendal Grignou 		ata_sff_exit();
6458d9027470SGwendal Grignou 		rc = -ENOMEM;
6459d9027470SGwendal Grignou 		goto err_out;
6460d9027470SGwendal Grignou 	}
6461d9027470SGwendal Grignou 
6462c6fd2807SJeff Garzik 	printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
6463c6fd2807SJeff Garzik 	return 0;
6464d9027470SGwendal Grignou 
6465d9027470SGwendal Grignou err_out:
6466d9027470SGwendal Grignou 	return rc;
6467c6fd2807SJeff Garzik }
6468c6fd2807SJeff Garzik 
6469c6fd2807SJeff Garzik static void __exit ata_exit(void)
6470c6fd2807SJeff Garzik {
6471d9027470SGwendal Grignou 	ata_release_transport(ata_scsi_transport_template);
6472d9027470SGwendal Grignou 	libata_transport_exit();
6473270390e1STejun Heo 	ata_sff_exit();
6474bf89b0bfSBartlomiej Zolnierkiewicz 	ata_free_force_param();
6475c6fd2807SJeff Garzik }
6476c6fd2807SJeff Garzik 
6477a4625085SBrian King subsys_initcall(ata_init);
6478c6fd2807SJeff Garzik module_exit(ata_exit);
6479c6fd2807SJeff Garzik 
64809990b6f3SAkinobu Mita static DEFINE_RATELIMIT_STATE(ratelimit, HZ / 5, 1);
6481c6fd2807SJeff Garzik 
6482c6fd2807SJeff Garzik int ata_ratelimit(void)
6483c6fd2807SJeff Garzik {
64849990b6f3SAkinobu Mita 	return __ratelimit(&ratelimit);
6485c6fd2807SJeff Garzik }
6486a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_ratelimit);
6487c6fd2807SJeff Garzik 
6488c0c362b6STejun Heo /**
6489c0c362b6STejun Heo  *	ata_msleep - ATA EH owner aware msleep
6490c0c362b6STejun Heo  *	@ap: ATA port to attribute the sleep to
6491c0c362b6STejun Heo  *	@msecs: duration to sleep in milliseconds
6492c0c362b6STejun Heo  *
6493c0c362b6STejun Heo  *	Sleeps @msecs.  If the current task is owner of @ap's EH, the
6494c0c362b6STejun Heo  *	ownership is released before going to sleep and reacquired
6495c0c362b6STejun Heo  *	after the sleep is complete.  IOW, other ports sharing the
6496c0c362b6STejun Heo  *	@ap->host will be allowed to own the EH while this task is
6497c0c362b6STejun Heo  *	sleeping.
6498c0c362b6STejun Heo  *
6499c0c362b6STejun Heo  *	LOCKING:
6500c0c362b6STejun Heo  *	Might sleep.
6501c0c362b6STejun Heo  */
650297750cebSTejun Heo void ata_msleep(struct ata_port *ap, unsigned int msecs)
650397750cebSTejun Heo {
6504c0c362b6STejun Heo 	bool owns_eh = ap && ap->host->eh_owner == current;
6505c0c362b6STejun Heo 
6506c0c362b6STejun Heo 	if (owns_eh)
6507c0c362b6STejun Heo 		ata_eh_release(ap);
6508c0c362b6STejun Heo 
6509848c3920SAnil Veliyankara Madam 	if (msecs < 20) {
6510848c3920SAnil Veliyankara Madam 		unsigned long usecs = msecs * USEC_PER_MSEC;
6511848c3920SAnil Veliyankara Madam 		usleep_range(usecs, usecs + 50);
6512848c3920SAnil Veliyankara Madam 	} else {
651397750cebSTejun Heo 		msleep(msecs);
6514848c3920SAnil Veliyankara Madam 	}
6515c0c362b6STejun Heo 
6516c0c362b6STejun Heo 	if (owns_eh)
6517c0c362b6STejun Heo 		ata_eh_acquire(ap);
651897750cebSTejun Heo }
6519a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_msleep);
652097750cebSTejun Heo 
6521c6fd2807SJeff Garzik /**
6522c6fd2807SJeff Garzik  *	ata_wait_register - wait until register value changes
652397750cebSTejun Heo  *	@ap: ATA port to wait register for, can be NULL
6524c6fd2807SJeff Garzik  *	@reg: IO-mapped register
6525c6fd2807SJeff Garzik  *	@mask: Mask to apply to read register value
6526c6fd2807SJeff Garzik  *	@val: Wait condition
6527341c2c95STejun Heo  *	@interval: polling interval in milliseconds
6528341c2c95STejun Heo  *	@timeout: timeout in milliseconds
6529c6fd2807SJeff Garzik  *
6530c6fd2807SJeff Garzik  *	Waiting for some bits of register to change is a common
6531c6fd2807SJeff Garzik  *	operation for ATA controllers.  This function reads 32bit LE
6532c6fd2807SJeff Garzik  *	IO-mapped register @reg and tests for the following condition.
6533c6fd2807SJeff Garzik  *
6534c6fd2807SJeff Garzik  *	(*@reg & mask) != val
6535c6fd2807SJeff Garzik  *
6536c6fd2807SJeff Garzik  *	If the condition is met, it returns; otherwise, the process is
6537c6fd2807SJeff Garzik  *	repeated after @interval_msec until timeout.
6538c6fd2807SJeff Garzik  *
6539c6fd2807SJeff Garzik  *	LOCKING:
6540c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
6541c6fd2807SJeff Garzik  *
6542c6fd2807SJeff Garzik  *	RETURNS:
6543c6fd2807SJeff Garzik  *	The final register value.
6544c6fd2807SJeff Garzik  */
654597750cebSTejun Heo u32 ata_wait_register(struct ata_port *ap, void __iomem *reg, u32 mask, u32 val,
6546341c2c95STejun Heo 		      unsigned long interval, unsigned long timeout)
6547c6fd2807SJeff Garzik {
6548341c2c95STejun Heo 	unsigned long deadline;
6549c6fd2807SJeff Garzik 	u32 tmp;
6550c6fd2807SJeff Garzik 
6551c6fd2807SJeff Garzik 	tmp = ioread32(reg);
6552c6fd2807SJeff Garzik 
6553c6fd2807SJeff Garzik 	/* Calculate timeout _after_ the first read to make sure
6554c6fd2807SJeff Garzik 	 * preceding writes reach the controller before starting to
6555c6fd2807SJeff Garzik 	 * eat away the timeout.
6556c6fd2807SJeff Garzik 	 */
6557341c2c95STejun Heo 	deadline = ata_deadline(jiffies, timeout);
6558c6fd2807SJeff Garzik 
6559341c2c95STejun Heo 	while ((tmp & mask) == val && time_before(jiffies, deadline)) {
656097750cebSTejun Heo 		ata_msleep(ap, interval);
6561c6fd2807SJeff Garzik 		tmp = ioread32(reg);
6562c6fd2807SJeff Garzik 	}
6563c6fd2807SJeff Garzik 
6564c6fd2807SJeff Garzik 	return tmp;
6565c6fd2807SJeff Garzik }
6566a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_wait_register);
6567c6fd2807SJeff Garzik 
6568c6fd2807SJeff Garzik /*
6569c6fd2807SJeff Garzik  * Dummy port_ops
6570c6fd2807SJeff Garzik  */
6571c6fd2807SJeff Garzik static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
6572c6fd2807SJeff Garzik {
6573c6fd2807SJeff Garzik 	return AC_ERR_SYSTEM;
6574c6fd2807SJeff Garzik }
6575c6fd2807SJeff Garzik 
6576182d7bbaSTejun Heo static void ata_dummy_error_handler(struct ata_port *ap)
6577182d7bbaSTejun Heo {
6578182d7bbaSTejun Heo 	/* truly dummy */
6579182d7bbaSTejun Heo }
6580182d7bbaSTejun Heo 
6581029cfd6bSTejun Heo struct ata_port_operations ata_dummy_port_ops = {
6582c6fd2807SJeff Garzik 	.qc_prep		= ata_noop_qc_prep,
6583c6fd2807SJeff Garzik 	.qc_issue		= ata_dummy_qc_issue,
6584182d7bbaSTejun Heo 	.error_handler		= ata_dummy_error_handler,
6585e4a9c373SDan Williams 	.sched_eh		= ata_std_sched_eh,
6586e4a9c373SDan Williams 	.end_eh			= ata_std_end_eh,
6587c6fd2807SJeff Garzik };
6588a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
6589c6fd2807SJeff Garzik 
659021b0ad4fSTejun Heo const struct ata_port_info ata_dummy_port_info = {
659121b0ad4fSTejun Heo 	.port_ops		= &ata_dummy_port_ops,
659221b0ad4fSTejun Heo };
6593a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_dummy_port_info);
659421b0ad4fSTejun Heo 
659506296a1eSJoe Perches void ata_print_version(const struct device *dev, const char *version)
659606296a1eSJoe Perches {
659706296a1eSJoe Perches 	dev_printk(KERN_DEBUG, dev, "version %s\n", version);
659806296a1eSJoe Perches }
659906296a1eSJoe Perches EXPORT_SYMBOL(ata_print_version);
6600c206a389SHannes Reinecke 
6601c206a389SHannes Reinecke EXPORT_TRACEPOINT_SYMBOL_GPL(ata_tf_load);
6602c206a389SHannes Reinecke EXPORT_TRACEPOINT_SYMBOL_GPL(ata_exec_command);
6603c206a389SHannes Reinecke EXPORT_TRACEPOINT_SYMBOL_GPL(ata_bmdma_setup);
6604c206a389SHannes Reinecke EXPORT_TRACEPOINT_SYMBOL_GPL(ata_bmdma_start);
6605c206a389SHannes Reinecke EXPORT_TRACEPOINT_SYMBOL_GPL(ata_bmdma_status);
6606