xref: /openbmc/linux/drivers/ata/libata-core.c (revision 25df73d9)
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 
2370891fd7c6SDamien Le Moal static int ata_dev_config_lba(struct ata_device *dev)
2371891fd7c6SDamien Le Moal {
2372891fd7c6SDamien Le Moal 	const u16 *id = dev->id;
2373891fd7c6SDamien Le Moal 	const char *lba_desc;
2374891fd7c6SDamien Le Moal 	char ncq_desc[24];
2375891fd7c6SDamien Le Moal 	int ret;
2376891fd7c6SDamien Le Moal 
2377891fd7c6SDamien Le Moal 	dev->flags |= ATA_DFLAG_LBA;
2378891fd7c6SDamien Le Moal 
2379891fd7c6SDamien Le Moal 	if (ata_id_has_lba48(id)) {
2380891fd7c6SDamien Le Moal 		lba_desc = "LBA48";
2381891fd7c6SDamien Le Moal 		dev->flags |= ATA_DFLAG_LBA48;
2382891fd7c6SDamien Le Moal 		if (dev->n_sectors >= (1UL << 28) &&
2383891fd7c6SDamien Le Moal 		    ata_id_has_flush_ext(id))
2384891fd7c6SDamien Le Moal 			dev->flags |= ATA_DFLAG_FLUSH_EXT;
2385891fd7c6SDamien Le Moal 	} else {
2386891fd7c6SDamien Le Moal 		lba_desc = "LBA";
2387891fd7c6SDamien Le Moal 	}
2388891fd7c6SDamien Le Moal 
2389891fd7c6SDamien Le Moal 	/* config NCQ */
2390891fd7c6SDamien Le Moal 	ret = ata_dev_config_ncq(dev, ncq_desc, sizeof(ncq_desc));
2391891fd7c6SDamien Le Moal 
2392891fd7c6SDamien Le Moal 	/* print device info to dmesg */
23931c95a27cSHannes Reinecke 	if (ata_dev_print_info(dev))
2394891fd7c6SDamien Le Moal 		ata_dev_info(dev,
2395891fd7c6SDamien Le Moal 			     "%llu sectors, multi %u: %s %s\n",
2396891fd7c6SDamien Le Moal 			     (unsigned long long)dev->n_sectors,
2397891fd7c6SDamien Le Moal 			     dev->multi_count, lba_desc, ncq_desc);
2398891fd7c6SDamien Le Moal 
2399891fd7c6SDamien Le Moal 	return ret;
2400891fd7c6SDamien Le Moal }
2401891fd7c6SDamien Le Moal 
2402891fd7c6SDamien Le Moal static void ata_dev_config_chs(struct ata_device *dev)
2403891fd7c6SDamien Le Moal {
2404891fd7c6SDamien Le Moal 	const u16 *id = dev->id;
2405891fd7c6SDamien Le Moal 
2406891fd7c6SDamien Le Moal 	if (ata_id_current_chs_valid(id)) {
2407891fd7c6SDamien Le Moal 		/* Current CHS translation is valid. */
2408891fd7c6SDamien Le Moal 		dev->cylinders = id[54];
2409891fd7c6SDamien Le Moal 		dev->heads     = id[55];
2410891fd7c6SDamien Le Moal 		dev->sectors   = id[56];
2411891fd7c6SDamien Le Moal 	} else {
2412891fd7c6SDamien Le Moal 		/* Default translation */
2413891fd7c6SDamien Le Moal 		dev->cylinders	= id[1];
2414891fd7c6SDamien Le Moal 		dev->heads	= id[3];
2415891fd7c6SDamien Le Moal 		dev->sectors	= id[6];
2416891fd7c6SDamien Le Moal 	}
2417891fd7c6SDamien Le Moal 
2418891fd7c6SDamien Le Moal 	/* print device info to dmesg */
24191c95a27cSHannes Reinecke 	if (ata_dev_print_info(dev))
2420891fd7c6SDamien Le Moal 		ata_dev_info(dev,
2421891fd7c6SDamien Le Moal 			     "%llu sectors, multi %u, CHS %u/%u/%u\n",
2422891fd7c6SDamien Le Moal 			     (unsigned long long)dev->n_sectors,
2423891fd7c6SDamien Le Moal 			     dev->multi_count, dev->cylinders,
2424891fd7c6SDamien Le Moal 			     dev->heads, dev->sectors);
2425891fd7c6SDamien Le Moal }
2426891fd7c6SDamien Le Moal 
24274d2e4980SDamien Le Moal static void ata_dev_config_fua(struct ata_device *dev)
24284d2e4980SDamien Le Moal {
24294d2e4980SDamien Le Moal 	/* Ignore FUA support if its use is disabled globally */
24304d2e4980SDamien Le Moal 	if (!libata_fua)
24314d2e4980SDamien Le Moal 		goto nofua;
24324d2e4980SDamien Le Moal 
24334d2e4980SDamien Le Moal 	/* Ignore devices without support for WRITE DMA FUA EXT */
24344d2e4980SDamien Le Moal 	if (!(dev->flags & ATA_DFLAG_LBA48) || !ata_id_has_fua(dev->id))
24354d2e4980SDamien Le Moal 		goto nofua;
24364d2e4980SDamien Le Moal 
24374d2e4980SDamien Le Moal 	/* Ignore known bad devices and devices that lack NCQ support */
24384d2e4980SDamien Le Moal 	if (!ata_ncq_supported(dev) || (dev->horkage & ATA_HORKAGE_NO_FUA))
24394d2e4980SDamien Le Moal 		goto nofua;
24404d2e4980SDamien Le Moal 
24414d2e4980SDamien Le Moal 	dev->flags |= ATA_DFLAG_FUA;
24424d2e4980SDamien Le Moal 
24434d2e4980SDamien Le Moal 	return;
24444d2e4980SDamien Le Moal 
24454d2e4980SDamien Le Moal nofua:
24464d2e4980SDamien Le Moal 	dev->flags &= ~ATA_DFLAG_FUA;
24474d2e4980SDamien Le Moal }
24484d2e4980SDamien Le Moal 
2449d8d8778cSDamien Le Moal static void ata_dev_config_devslp(struct ata_device *dev)
2450d8d8778cSDamien Le Moal {
2451d8d8778cSDamien Le Moal 	u8 *sata_setting = dev->link->ap->sector_buf;
2452d8d8778cSDamien Le Moal 	unsigned int err_mask;
2453d8d8778cSDamien Le Moal 	int i, j;
2454d8d8778cSDamien Le Moal 
2455d8d8778cSDamien Le Moal 	/*
2456d8d8778cSDamien Le Moal 	 * Check device sleep capability. Get DevSlp timing variables
2457d8d8778cSDamien Le Moal 	 * from SATA Settings page of Identify Device Data Log.
2458d8d8778cSDamien Le Moal 	 */
245906f6c4c6SDamien Le Moal 	if (!ata_id_has_devslp(dev->id) ||
246006f6c4c6SDamien Le Moal 	    !ata_identify_page_supported(dev, ATA_LOG_SATA_SETTINGS))
2461d8d8778cSDamien Le Moal 		return;
2462d8d8778cSDamien Le Moal 
2463d8d8778cSDamien Le Moal 	err_mask = ata_read_log_page(dev,
2464d8d8778cSDamien Le Moal 				     ATA_LOG_IDENTIFY_DEVICE,
2465d8d8778cSDamien Le Moal 				     ATA_LOG_SATA_SETTINGS,
2466d8d8778cSDamien Le Moal 				     sata_setting, 1);
2467fc5c8aa7SDamien Le Moal 	if (err_mask)
2468d8d8778cSDamien Le Moal 		return;
2469d8d8778cSDamien Le Moal 
2470d8d8778cSDamien Le Moal 	dev->flags |= ATA_DFLAG_DEVSLP;
2471d8d8778cSDamien Le Moal 	for (i = 0; i < ATA_LOG_DEVSLP_SIZE; i++) {
2472d8d8778cSDamien Le Moal 		j = ATA_LOG_DEVSLP_OFFSET + i;
2473d8d8778cSDamien Le Moal 		dev->devslp_timing[i] = sata_setting[j];
2474d8d8778cSDamien Le Moal 	}
2475d8d8778cSDamien Le Moal }
2476d8d8778cSDamien Le Moal 
2477fe22e1c2SDamien Le Moal static void ata_dev_config_cpr(struct ata_device *dev)
2478fe22e1c2SDamien Le Moal {
2479fe22e1c2SDamien Le Moal 	unsigned int err_mask;
2480fe22e1c2SDamien Le Moal 	size_t buf_len;
2481fe22e1c2SDamien Le Moal 	int i, nr_cpr = 0;
2482fe22e1c2SDamien Le Moal 	struct ata_cpr_log *cpr_log = NULL;
2483fe22e1c2SDamien Le Moal 	u8 *desc, *buf = NULL;
2484fe22e1c2SDamien Le Moal 
2485c745dfc5STyler Erickson 	if (ata_id_major_version(dev->id) < 11)
2486c745dfc5STyler Erickson 		goto out;
2487c745dfc5STyler Erickson 
2488c745dfc5STyler Erickson 	buf_len = ata_log_supported(dev, ATA_LOG_CONCURRENT_POSITIONING_RANGES);
2489c745dfc5STyler Erickson 	if (buf_len == 0)
2490fe22e1c2SDamien Le Moal 		goto out;
2491fe22e1c2SDamien Le Moal 
2492fe22e1c2SDamien Le Moal 	/*
2493fda17afcSDamien Le Moal 	 * Read the concurrent positioning ranges log (0x47). We can have at
2494c745dfc5STyler Erickson 	 * most 255 32B range descriptors plus a 64B header. This log varies in
2495c745dfc5STyler Erickson 	 * size, so use the size reported in the GPL directory. Reading beyond
2496c745dfc5STyler Erickson 	 * the supported length will result in an error.
2497fe22e1c2SDamien Le Moal 	 */
2498c745dfc5STyler Erickson 	buf_len <<= 9;
2499fe22e1c2SDamien Le Moal 	buf = kzalloc(buf_len, GFP_KERNEL);
2500fe22e1c2SDamien Le Moal 	if (!buf)
2501fe22e1c2SDamien Le Moal 		goto out;
2502fe22e1c2SDamien Le Moal 
2503fda17afcSDamien Le Moal 	err_mask = ata_read_log_page(dev, ATA_LOG_CONCURRENT_POSITIONING_RANGES,
2504fda17afcSDamien Le Moal 				     0, buf, buf_len >> 9);
2505fe22e1c2SDamien Le Moal 	if (err_mask)
2506fe22e1c2SDamien Le Moal 		goto out;
2507fe22e1c2SDamien Le Moal 
2508fe22e1c2SDamien Le Moal 	nr_cpr = buf[0];
2509fe22e1c2SDamien Le Moal 	if (!nr_cpr)
2510fe22e1c2SDamien Le Moal 		goto out;
2511fe22e1c2SDamien Le Moal 
2512fe22e1c2SDamien Le Moal 	cpr_log = kzalloc(struct_size(cpr_log, cpr, nr_cpr), GFP_KERNEL);
2513fe22e1c2SDamien Le Moal 	if (!cpr_log)
2514fe22e1c2SDamien Le Moal 		goto out;
2515fe22e1c2SDamien Le Moal 
2516fe22e1c2SDamien Le Moal 	cpr_log->nr_cpr = nr_cpr;
2517fe22e1c2SDamien Le Moal 	desc = &buf[64];
2518fe22e1c2SDamien Le Moal 	for (i = 0; i < nr_cpr; i++, desc += 32) {
2519fe22e1c2SDamien Le Moal 		cpr_log->cpr[i].num = desc[0];
2520fe22e1c2SDamien Le Moal 		cpr_log->cpr[i].num_storage_elements = desc[1];
2521fe22e1c2SDamien Le Moal 		cpr_log->cpr[i].start_lba = get_unaligned_le64(&desc[8]);
2522fe22e1c2SDamien Le Moal 		cpr_log->cpr[i].num_lbas = get_unaligned_le64(&desc[16]);
2523fe22e1c2SDamien Le Moal 	}
2524fe22e1c2SDamien Le Moal 
2525fe22e1c2SDamien Le Moal out:
2526fe22e1c2SDamien Le Moal 	swap(dev->cpr_log, cpr_log);
2527fe22e1c2SDamien Le Moal 	kfree(cpr_log);
2528fe22e1c2SDamien Le Moal 	kfree(buf);
2529fe22e1c2SDamien Le Moal }
2530fe22e1c2SDamien Le Moal 
2531d633b8a7SDamien Le Moal static void ata_dev_print_features(struct ata_device *dev)
2532d633b8a7SDamien Le Moal {
2533d633b8a7SDamien Le Moal 	if (!(dev->flags & ATA_DFLAG_FEATURES_MASK))
2534d633b8a7SDamien Le Moal 		return;
2535d633b8a7SDamien Le Moal 
2536d633b8a7SDamien Le Moal 	ata_dev_info(dev,
25374d2e4980SDamien Le Moal 		     "Features:%s%s%s%s%s%s%s\n",
25384d2e4980SDamien Le Moal 		     dev->flags & ATA_DFLAG_FUA ? " FUA" : "",
2539d633b8a7SDamien Le Moal 		     dev->flags & ATA_DFLAG_TRUSTED ? " Trust" : "",
2540d633b8a7SDamien Le Moal 		     dev->flags & ATA_DFLAG_DA ? " Dev-Attention" : "",
2541d633b8a7SDamien Le Moal 		     dev->flags & ATA_DFLAG_DEVSLP ? " Dev-Sleep" : "",
2542d633b8a7SDamien Le Moal 		     dev->flags & ATA_DFLAG_NCQ_SEND_RECV ? " NCQ-sndrcv" : "",
2543fe22e1c2SDamien Le Moal 		     dev->flags & ATA_DFLAG_NCQ_PRIO ? " NCQ-prio" : "",
2544fe22e1c2SDamien Le Moal 		     dev->cpr_log ? " CPR" : "");
2545d633b8a7SDamien Le Moal }
2546d633b8a7SDamien Le Moal 
2547c6fd2807SJeff Garzik /**
2548c6fd2807SJeff Garzik  *	ata_dev_configure - Configure the specified ATA/ATAPI device
2549c6fd2807SJeff Garzik  *	@dev: Target device to configure
2550c6fd2807SJeff Garzik  *
2551c6fd2807SJeff Garzik  *	Configure @dev according to @dev->id.  Generic and low-level
2552c6fd2807SJeff Garzik  *	driver specific fixups are also applied.
2553c6fd2807SJeff Garzik  *
2554c6fd2807SJeff Garzik  *	LOCKING:
2555c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
2556c6fd2807SJeff Garzik  *
2557c6fd2807SJeff Garzik  *	RETURNS:
2558c6fd2807SJeff Garzik  *	0 on success, -errno otherwise
2559c6fd2807SJeff Garzik  */
2560efdaedc4STejun Heo int ata_dev_configure(struct ata_device *dev)
2561c6fd2807SJeff Garzik {
25629af5c9c9STejun Heo 	struct ata_port *ap = dev->link->ap;
2563891fd7c6SDamien Le Moal 	bool print_info = ata_dev_print_info(dev);
2564c6fd2807SJeff Garzik 	const u16 *id = dev->id;
2565f0a6d77bSSergey Shtylyov 	unsigned int xfer_mask;
256665fe1f0fSShane Huang 	unsigned int err_mask;
2567b352e57dSAlan Cox 	char revbuf[7];		/* XYZ-99\0 */
25683f64f565SEric D. Mudama 	char fwrevbuf[ATA_ID_FW_REV_LEN+1];
25693f64f565SEric D. Mudama 	char modelbuf[ATA_ID_PROD_LEN+1];
2570c6fd2807SJeff Garzik 	int rc;
2571c6fd2807SJeff Garzik 
257296c810f2SHannes Reinecke 	if (!ata_dev_enabled(dev)) {
257396c810f2SHannes Reinecke 		ata_dev_dbg(dev, "no device\n");
2574c6fd2807SJeff Garzik 		return 0;
2575c6fd2807SJeff Garzik 	}
2576c6fd2807SJeff Garzik 
257775683fe7STejun Heo 	/* set horkage */
257875683fe7STejun Heo 	dev->horkage |= ata_dev_blacklisted(dev);
257933267325STejun Heo 	ata_force_horkage(dev);
258075683fe7STejun Heo 
258150af2fa1STejun Heo 	if (dev->horkage & ATA_HORKAGE_DISABLE) {
2582a9a79dfeSJoe Perches 		ata_dev_info(dev, "unsupported device, disabling\n");
258350af2fa1STejun Heo 		ata_dev_disable(dev);
258450af2fa1STejun Heo 		return 0;
258550af2fa1STejun Heo 	}
258650af2fa1STejun Heo 
25872486fa56STejun Heo 	if ((!atapi_enabled || (ap->flags & ATA_FLAG_NO_ATAPI)) &&
25882486fa56STejun Heo 	    dev->class == ATA_DEV_ATAPI) {
2589a9a79dfeSJoe Perches 		ata_dev_warn(dev, "WARNING: ATAPI is %s, device ignored\n",
25902486fa56STejun Heo 			     atapi_enabled ? "not supported with this driver"
25912486fa56STejun Heo 			     : "disabled");
25922486fa56STejun Heo 		ata_dev_disable(dev);
25932486fa56STejun Heo 		return 0;
25942486fa56STejun Heo 	}
25952486fa56STejun Heo 
25969062712fSTejun Heo 	rc = ata_do_link_spd_horkage(dev);
25979062712fSTejun Heo 	if (rc)
25989062712fSTejun Heo 		return rc;
25999062712fSTejun Heo 
2600ecd75ad5STejun Heo 	/* some WD SATA-1 drives have issues with LPM, turn on NOLPM for them */
2601ecd75ad5STejun Heo 	if ((dev->horkage & ATA_HORKAGE_WD_BROKEN_LPM) &&
2602ecd75ad5STejun Heo 	    (id[ATA_ID_SATA_CAPABILITY] & 0xe) == 0x2)
2603ecd75ad5STejun Heo 		dev->horkage |= ATA_HORKAGE_NOLPM;
2604ecd75ad5STejun Heo 
2605240630e6SHans de Goede 	if (ap->flags & ATA_FLAG_NO_LPM)
2606240630e6SHans de Goede 		dev->horkage |= ATA_HORKAGE_NOLPM;
2607240630e6SHans de Goede 
2608ecd75ad5STejun Heo 	if (dev->horkage & ATA_HORKAGE_NOLPM) {
2609ecd75ad5STejun Heo 		ata_dev_warn(dev, "LPM support broken, forcing max_power\n");
2610ecd75ad5STejun Heo 		dev->link->ap->target_lpm_policy = ATA_LPM_MAX_POWER;
2611ecd75ad5STejun Heo 	}
2612ecd75ad5STejun Heo 
26136746544cSTejun Heo 	/* let ACPI work its magic */
26146746544cSTejun Heo 	rc = ata_acpi_on_devcfg(dev);
26156746544cSTejun Heo 	if (rc)
26166746544cSTejun Heo 		return rc;
261708573a86SKristen Carlson Accardi 
261805027adcSTejun Heo 	/* massage HPA, do it early as it might change IDENTIFY data */
261905027adcSTejun Heo 	rc = ata_hpa_resize(dev);
262005027adcSTejun Heo 	if (rc)
262105027adcSTejun Heo 		return rc;
262205027adcSTejun Heo 
2623c6fd2807SJeff Garzik 	/* print device capabilities */
2624a9a79dfeSJoe Perches 	ata_dev_dbg(dev,
2625c6fd2807SJeff Garzik 		    "%s: cfg 49:%04x 82:%04x 83:%04x 84:%04x "
2626c6fd2807SJeff Garzik 		    "85:%04x 86:%04x 87:%04x 88:%04x\n",
26277f5e4e8dSHarvey Harrison 		    __func__,
2628c6fd2807SJeff Garzik 		    id[49], id[82], id[83], id[84],
2629c6fd2807SJeff Garzik 		    id[85], id[86], id[87], id[88]);
2630c6fd2807SJeff Garzik 
2631c6fd2807SJeff Garzik 	/* initialize to-be-configured parameters */
2632c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_CFG_MASK;
2633c6fd2807SJeff Garzik 	dev->max_sectors = 0;
2634c6fd2807SJeff Garzik 	dev->cdb_len = 0;
2635c6fd2807SJeff Garzik 	dev->n_sectors = 0;
2636c6fd2807SJeff Garzik 	dev->cylinders = 0;
2637c6fd2807SJeff Garzik 	dev->heads = 0;
2638c6fd2807SJeff Garzik 	dev->sectors = 0;
2639e18086d6SMark Lord 	dev->multi_count = 0;
2640c6fd2807SJeff Garzik 
2641c6fd2807SJeff Garzik 	/*
2642c6fd2807SJeff Garzik 	 * common ATA, ATAPI feature tests
2643c6fd2807SJeff Garzik 	 */
2644c6fd2807SJeff Garzik 
2645c6fd2807SJeff Garzik 	/* find max transfer mode; for printk only */
2646c6fd2807SJeff Garzik 	xfer_mask = ata_id_xfermask(id);
2647c6fd2807SJeff Garzik 
26486044f3c4SHannes Reinecke 	ata_dump_id(dev, id);
2649c6fd2807SJeff Garzik 
2650ef143d57SAlbert Lee 	/* SCSI only uses 4-char revisions, dump full 8 chars from ATA */
2651ef143d57SAlbert Lee 	ata_id_c_string(dev->id, fwrevbuf, ATA_ID_FW_REV,
2652ef143d57SAlbert Lee 			sizeof(fwrevbuf));
2653ef143d57SAlbert Lee 
2654ef143d57SAlbert Lee 	ata_id_c_string(dev->id, modelbuf, ATA_ID_PROD,
2655ef143d57SAlbert Lee 			sizeof(modelbuf));
2656ef143d57SAlbert Lee 
2657c6fd2807SJeff Garzik 	/* ATA-specific feature tests */
26589162c657SHannes Reinecke 	if (dev->class == ATA_DEV_ATA || dev->class == ATA_DEV_ZAC) {
2659b352e57dSAlan Cox 		if (ata_id_is_cfa(id)) {
266062afe5d7SSergei Shtylyov 			/* CPRM may make this media unusable */
266162afe5d7SSergei Shtylyov 			if (id[ATA_ID_CFA_KEY_MGMT] & 1)
2662a9a79dfeSJoe Perches 				ata_dev_warn(dev,
2663a9a79dfeSJoe Perches 	"supports DRM functions and may not be fully accessible\n");
2664b352e57dSAlan Cox 			snprintf(revbuf, 7, "CFA");
2665ae8d4ee7SAlan Cox 		} else {
2666b352e57dSAlan Cox 			snprintf(revbuf, 7, "ATA-%d", ata_id_major_version(id));
2667ae8d4ee7SAlan Cox 			/* Warn the user if the device has TPM extensions */
2668ae8d4ee7SAlan Cox 			if (ata_id_has_tpm(id))
2669a9a79dfeSJoe Perches 				ata_dev_warn(dev,
2670a9a79dfeSJoe Perches 	"supports DRM functions and may not be fully accessible\n");
2671ae8d4ee7SAlan Cox 		}
2672b352e57dSAlan Cox 
2673c6fd2807SJeff Garzik 		dev->n_sectors = ata_id_n_sectors(id);
2674c6fd2807SJeff Garzik 
2675e18086d6SMark Lord 		/* get current R/W Multiple count setting */
2676e18086d6SMark Lord 		if ((dev->id[47] >> 8) == 0x80 && (dev->id[59] & 0x100)) {
2677e18086d6SMark Lord 			unsigned int max = dev->id[47] & 0xff;
2678e18086d6SMark Lord 			unsigned int cnt = dev->id[59] & 0xff;
2679e18086d6SMark Lord 			/* only recognize/allow powers of two here */
2680e18086d6SMark Lord 			if (is_power_of_2(max) && is_power_of_2(cnt))
2681e18086d6SMark Lord 				if (cnt <= max)
2682e18086d6SMark Lord 					dev->multi_count = cnt;
2683e18086d6SMark Lord 		}
26843f64f565SEric D. Mudama 
2685891fd7c6SDamien Le Moal 		/* print device info to dmesg */
26861c95a27cSHannes Reinecke 		if (print_info)
2687891fd7c6SDamien Le Moal 			ata_dev_info(dev, "%s: %s, %s, max %s\n",
2688891fd7c6SDamien Le Moal 				     revbuf, modelbuf, fwrevbuf,
2689891fd7c6SDamien Le Moal 				     ata_mode_string(xfer_mask));
2690891fd7c6SDamien Le Moal 
2691c6fd2807SJeff Garzik 		if (ata_id_has_lba(id)) {
2692891fd7c6SDamien Le Moal 			rc = ata_dev_config_lba(dev);
2693388539f3SShaohua Li 			if (rc)
2694388539f3SShaohua Li 				return rc;
2695c6fd2807SJeff Garzik 		} else {
2696891fd7c6SDamien Le Moal 			ata_dev_config_chs(dev);
2697c6fd2807SJeff Garzik 		}
2698c6fd2807SJeff Garzik 
26994d2e4980SDamien Le Moal 		ata_dev_config_fua(dev);
2700d8d8778cSDamien Le Moal 		ata_dev_config_devslp(dev);
2701e87fd28cSHannes Reinecke 		ata_dev_config_sense_reporting(dev);
27026d1003aeSHannes Reinecke 		ata_dev_config_zac(dev);
2703818831c8SChristoph Hellwig 		ata_dev_config_trusted(dev);
2704fe22e1c2SDamien Le Moal 		ata_dev_config_cpr(dev);
2705b1ffbf85SMinwoo Im 		dev->cdb_len = 32;
2706d633b8a7SDamien Le Moal 
27071c95a27cSHannes Reinecke 		if (print_info)
2708d633b8a7SDamien Le Moal 			ata_dev_print_features(dev);
2709c6fd2807SJeff Garzik 	}
2710c6fd2807SJeff Garzik 
2711c6fd2807SJeff Garzik 	/* ATAPI-specific feature tests */
2712c6fd2807SJeff Garzik 	else if (dev->class == ATA_DEV_ATAPI) {
2713854c73a2STejun Heo 		const char *cdb_intr_string = "";
2714854c73a2STejun Heo 		const char *atapi_an_string = "";
271591163006STejun Heo 		const char *dma_dir_string = "";
27167d77b247STejun Heo 		u32 sntf;
2717c6fd2807SJeff Garzik 
2718c6fd2807SJeff Garzik 		rc = atapi_cdb_len(id);
2719c6fd2807SJeff Garzik 		if ((rc < 12) || (rc > ATAPI_CDB_LEN)) {
272016d42467SHannes Reinecke 			ata_dev_warn(dev, "unsupported CDB len %d\n", rc);
2721c6fd2807SJeff Garzik 			rc = -EINVAL;
2722c6fd2807SJeff Garzik 			goto err_out_nosup;
2723c6fd2807SJeff Garzik 		}
2724c6fd2807SJeff Garzik 		dev->cdb_len = (unsigned int) rc;
2725c6fd2807SJeff Garzik 
27267d77b247STejun Heo 		/* Enable ATAPI AN if both the host and device have
27277d77b247STejun Heo 		 * the support.  If PMP is attached, SNTF is required
27287d77b247STejun Heo 		 * to enable ATAPI AN to discern between PHY status
27297d77b247STejun Heo 		 * changed notifications and ATAPI ANs.
27309f45cbd3SKristen Carlson Accardi 		 */
2731e7ecd435STejun Heo 		if (atapi_an &&
2732e7ecd435STejun Heo 		    (ap->flags & ATA_FLAG_AN) && ata_id_has_atapi_AN(id) &&
2733071f44b1STejun Heo 		    (!sata_pmp_attached(ap) ||
27347d77b247STejun Heo 		     sata_scr_read(&ap->link, SCR_NOTIFICATION, &sntf) == 0)) {
27359f45cbd3SKristen Carlson Accardi 			/* issue SET feature command to turn this on */
2736218f3d30SJeff Garzik 			err_mask = ata_dev_set_feature(dev,
2737218f3d30SJeff Garzik 					SETFEATURES_SATA_ENABLE, SATA_AN);
2738854c73a2STejun Heo 			if (err_mask)
2739a9a79dfeSJoe Perches 				ata_dev_err(dev,
2740a9a79dfeSJoe Perches 					    "failed to enable ATAPI AN (err_mask=0x%x)\n",
2741a9a79dfeSJoe Perches 					    err_mask);
2742854c73a2STejun Heo 			else {
27439f45cbd3SKristen Carlson Accardi 				dev->flags |= ATA_DFLAG_AN;
2744854c73a2STejun Heo 				atapi_an_string = ", ATAPI AN";
2745854c73a2STejun Heo 			}
27469f45cbd3SKristen Carlson Accardi 		}
27479f45cbd3SKristen Carlson Accardi 
2748c6fd2807SJeff Garzik 		if (ata_id_cdb_intr(dev->id)) {
2749c6fd2807SJeff Garzik 			dev->flags |= ATA_DFLAG_CDB_INTR;
2750c6fd2807SJeff Garzik 			cdb_intr_string = ", CDB intr";
2751c6fd2807SJeff Garzik 		}
2752c6fd2807SJeff Garzik 
2753966fbe19SVincent Pelletier 		if (atapi_dmadir || (dev->horkage & ATA_HORKAGE_ATAPI_DMADIR) || atapi_id_dmadir(dev->id)) {
275491163006STejun Heo 			dev->flags |= ATA_DFLAG_DMADIR;
275591163006STejun Heo 			dma_dir_string = ", DMADIR";
275691163006STejun Heo 		}
275791163006STejun Heo 
2758afe75951SAaron Lu 		if (ata_id_has_da(dev->id)) {
2759b1354cbbSLin Ming 			dev->flags |= ATA_DFLAG_DA;
2760afe75951SAaron Lu 			zpodd_init(dev);
2761afe75951SAaron Lu 		}
2762b1354cbbSLin Ming 
2763c6fd2807SJeff Garzik 		/* print device info to dmesg */
27641c95a27cSHannes Reinecke 		if (print_info)
2765a9a79dfeSJoe Perches 			ata_dev_info(dev,
276691163006STejun Heo 				     "ATAPI: %s, %s, max %s%s%s%s\n",
2767ef143d57SAlbert Lee 				     modelbuf, fwrevbuf,
2768c6fd2807SJeff Garzik 				     ata_mode_string(xfer_mask),
276991163006STejun Heo 				     cdb_intr_string, atapi_an_string,
277091163006STejun Heo 				     dma_dir_string);
2771c6fd2807SJeff Garzik 	}
2772c6fd2807SJeff Garzik 
2773914ed354STejun Heo 	/* determine max_sectors */
2774914ed354STejun Heo 	dev->max_sectors = ATA_MAX_SECTORS;
2775914ed354STejun Heo 	if (dev->flags & ATA_DFLAG_LBA48)
2776914ed354STejun Heo 		dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2777914ed354STejun Heo 
2778c5038fc0SAlan Cox 	/* Limit PATA drive on SATA cable bridge transfers to udma5,
2779c5038fc0SAlan Cox 	   200 sectors */
2780c6fd2807SJeff Garzik 	if (ata_dev_knobble(dev)) {
27811c95a27cSHannes Reinecke 		if (print_info)
2782a9a79dfeSJoe Perches 			ata_dev_info(dev, "applying bridge limits\n");
2783c6fd2807SJeff Garzik 		dev->udma_mask &= ATA_UDMA5;
2784c6fd2807SJeff Garzik 		dev->max_sectors = ATA_MAX_SECTORS;
2785c6fd2807SJeff Garzik 	}
2786c6fd2807SJeff Garzik 
2787f8d8e579STony Battersby 	if ((dev->class == ATA_DEV_ATAPI) &&
2788f442cd86SAlbert Lee 	    (atapi_command_packet_set(id) == TYPE_TAPE)) {
2789f8d8e579STony Battersby 		dev->max_sectors = ATA_MAX_SECTORS_TAPE;
2790f442cd86SAlbert Lee 		dev->horkage |= ATA_HORKAGE_STUCK_ERR;
2791f442cd86SAlbert Lee 	}
2792f8d8e579STony Battersby 
279375683fe7STejun Heo 	if (dev->horkage & ATA_HORKAGE_MAX_SEC_128)
279403ec52deSTejun Heo 		dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_128,
279503ec52deSTejun Heo 					 dev->max_sectors);
279618d6e9d5SAlbert Lee 
2797af34d637SDavid Milburn 	if (dev->horkage & ATA_HORKAGE_MAX_SEC_1024)
2798af34d637SDavid Milburn 		dev->max_sectors = min_t(unsigned int, ATA_MAX_SECTORS_1024,
2799af34d637SDavid Milburn 					 dev->max_sectors);
2800af34d637SDavid Milburn 
2801a32450e1SShan Hai 	if (dev->horkage & ATA_HORKAGE_MAX_SEC_LBA48)
2802a32450e1SShan Hai 		dev->max_sectors = ATA_MAX_SECTORS_LBA48;
2803a32450e1SShan Hai 
2804c6fd2807SJeff Garzik 	if (ap->ops->dev_config)
2805cd0d3bbcSAlan 		ap->ops->dev_config(dev);
2806c6fd2807SJeff Garzik 
2807c5038fc0SAlan Cox 	if (dev->horkage & ATA_HORKAGE_DIAGNOSTIC) {
2808c5038fc0SAlan Cox 		/* Let the user know. We don't want to disallow opens for
2809c5038fc0SAlan Cox 		   rescue purposes, or in case the vendor is just a blithering
2810c5038fc0SAlan Cox 		   idiot. Do this after the dev_config call as some controllers
2811c5038fc0SAlan Cox 		   with buggy firmware may want to avoid reporting false device
2812c5038fc0SAlan Cox 		   bugs */
2813c5038fc0SAlan Cox 
2814c5038fc0SAlan Cox 		if (print_info) {
2815a9a79dfeSJoe Perches 			ata_dev_warn(dev,
2816c5038fc0SAlan Cox "Drive reports diagnostics failure. This may indicate a drive\n");
2817a9a79dfeSJoe Perches 			ata_dev_warn(dev,
2818c5038fc0SAlan Cox "fault or invalid emulation. Contact drive vendor for information.\n");
2819c5038fc0SAlan Cox 		}
2820c5038fc0SAlan Cox 	}
2821c5038fc0SAlan Cox 
2822ac70a964STejun Heo 	if ((dev->horkage & ATA_HORKAGE_FIRMWARE_WARN) && print_info) {
2823a9a79dfeSJoe Perches 		ata_dev_warn(dev, "WARNING: device requires firmware update to be fully functional\n");
2824a9a79dfeSJoe Perches 		ata_dev_warn(dev, "         contact the vendor or visit http://ata.wiki.kernel.org\n");
2825ac70a964STejun Heo 	}
2826ac70a964STejun Heo 
2827c6fd2807SJeff Garzik 	return 0;
2828c6fd2807SJeff Garzik 
2829c6fd2807SJeff Garzik err_out_nosup:
2830c6fd2807SJeff Garzik 	return rc;
2831c6fd2807SJeff Garzik }
2832c6fd2807SJeff Garzik 
2833c6fd2807SJeff Garzik /**
28342e41e8e6SAlan Cox  *	ata_cable_40wire	-	return 40 wire cable type
2835be0d18dfSAlan Cox  *	@ap: port
2836be0d18dfSAlan Cox  *
28372e41e8e6SAlan Cox  *	Helper method for drivers which want to hardwire 40 wire cable
2838be0d18dfSAlan Cox  *	detection.
2839be0d18dfSAlan Cox  */
2840be0d18dfSAlan Cox 
2841be0d18dfSAlan Cox int ata_cable_40wire(struct ata_port *ap)
2842be0d18dfSAlan Cox {
2843be0d18dfSAlan Cox 	return ATA_CBL_PATA40;
2844be0d18dfSAlan Cox }
2845a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_cable_40wire);
2846be0d18dfSAlan Cox 
2847be0d18dfSAlan Cox /**
28482e41e8e6SAlan Cox  *	ata_cable_80wire	-	return 80 wire cable type
2849be0d18dfSAlan Cox  *	@ap: port
2850be0d18dfSAlan Cox  *
28512e41e8e6SAlan Cox  *	Helper method for drivers which want to hardwire 80 wire cable
2852be0d18dfSAlan Cox  *	detection.
2853be0d18dfSAlan Cox  */
2854be0d18dfSAlan Cox 
2855be0d18dfSAlan Cox int ata_cable_80wire(struct ata_port *ap)
2856be0d18dfSAlan Cox {
2857be0d18dfSAlan Cox 	return ATA_CBL_PATA80;
2858be0d18dfSAlan Cox }
2859a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_cable_80wire);
2860be0d18dfSAlan Cox 
2861be0d18dfSAlan Cox /**
2862be0d18dfSAlan Cox  *	ata_cable_unknown	-	return unknown PATA cable.
2863be0d18dfSAlan Cox  *	@ap: port
2864be0d18dfSAlan Cox  *
2865be0d18dfSAlan Cox  *	Helper method for drivers which have no PATA cable detection.
2866be0d18dfSAlan Cox  */
2867be0d18dfSAlan Cox 
2868be0d18dfSAlan Cox int ata_cable_unknown(struct ata_port *ap)
2869be0d18dfSAlan Cox {
2870be0d18dfSAlan Cox 	return ATA_CBL_PATA_UNK;
2871be0d18dfSAlan Cox }
2872a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_cable_unknown);
2873be0d18dfSAlan Cox 
2874be0d18dfSAlan Cox /**
2875c88f90c3STejun Heo  *	ata_cable_ignore	-	return ignored PATA cable.
2876c88f90c3STejun Heo  *	@ap: port
2877c88f90c3STejun Heo  *
2878c88f90c3STejun Heo  *	Helper method for drivers which don't use cable type to limit
2879c88f90c3STejun Heo  *	transfer mode.
2880c88f90c3STejun Heo  */
2881c88f90c3STejun Heo int ata_cable_ignore(struct ata_port *ap)
2882c88f90c3STejun Heo {
2883c88f90c3STejun Heo 	return ATA_CBL_PATA_IGN;
2884c88f90c3STejun Heo }
2885a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_cable_ignore);
2886c88f90c3STejun Heo 
2887c88f90c3STejun Heo /**
2888be0d18dfSAlan Cox  *	ata_cable_sata	-	return SATA cable type
2889be0d18dfSAlan Cox  *	@ap: port
2890be0d18dfSAlan Cox  *
2891be0d18dfSAlan Cox  *	Helper method for drivers which have SATA cables
2892be0d18dfSAlan Cox  */
2893be0d18dfSAlan Cox 
2894be0d18dfSAlan Cox int ata_cable_sata(struct ata_port *ap)
2895be0d18dfSAlan Cox {
2896be0d18dfSAlan Cox 	return ATA_CBL_SATA;
2897be0d18dfSAlan Cox }
2898a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_cable_sata);
2899be0d18dfSAlan Cox 
2900be0d18dfSAlan Cox /**
2901c6fd2807SJeff Garzik  *	ata_bus_probe - Reset and probe ATA bus
2902c6fd2807SJeff Garzik  *	@ap: Bus to probe
2903c6fd2807SJeff Garzik  *
2904c6fd2807SJeff Garzik  *	Master ATA bus probing function.  Initiates a hardware-dependent
2905c6fd2807SJeff Garzik  *	bus reset, then attempts to identify any devices found on
2906c6fd2807SJeff Garzik  *	the bus.
2907c6fd2807SJeff Garzik  *
2908c6fd2807SJeff Garzik  *	LOCKING:
2909c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
2910c6fd2807SJeff Garzik  *
2911c6fd2807SJeff Garzik  *	RETURNS:
2912c6fd2807SJeff Garzik  *	Zero on success, negative errno otherwise.
2913c6fd2807SJeff Garzik  */
2914c6fd2807SJeff Garzik 
2915c6fd2807SJeff Garzik int ata_bus_probe(struct ata_port *ap)
2916c6fd2807SJeff Garzik {
2917c6fd2807SJeff Garzik 	unsigned int classes[ATA_MAX_DEVICES];
2918c6fd2807SJeff Garzik 	int tries[ATA_MAX_DEVICES];
2919f58229f8STejun Heo 	int rc;
2920c6fd2807SJeff Garzik 	struct ata_device *dev;
2921c6fd2807SJeff Garzik 
29221eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL)
2923f58229f8STejun Heo 		tries[dev->devno] = ATA_PROBE_MAX_TRIES;
2924c6fd2807SJeff Garzik 
2925c6fd2807SJeff Garzik  retry:
29261eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL) {
2927cdeab114STejun Heo 		/* If we issue an SRST then an ATA drive (not ATAPI)
2928cdeab114STejun Heo 		 * may change configuration and be in PIO0 timing. If
2929cdeab114STejun Heo 		 * we do a hard reset (or are coming from power on)
2930cdeab114STejun Heo 		 * this is true for ATA or ATAPI. Until we've set a
2931cdeab114STejun Heo 		 * suitable controller mode we should not touch the
2932cdeab114STejun Heo 		 * bus as we may be talking too fast.
2933cdeab114STejun Heo 		 */
2934cdeab114STejun Heo 		dev->pio_mode = XFER_PIO_0;
29355416912aSAaron Lu 		dev->dma_mode = 0xff;
2936cdeab114STejun Heo 
2937cdeab114STejun Heo 		/* If the controller has a pio mode setup function
2938cdeab114STejun Heo 		 * then use it to set the chipset to rights. Don't
2939cdeab114STejun Heo 		 * touch the DMA setup as that will be dealt with when
2940cdeab114STejun Heo 		 * configuring devices.
2941cdeab114STejun Heo 		 */
2942cdeab114STejun Heo 		if (ap->ops->set_piomode)
2943cdeab114STejun Heo 			ap->ops->set_piomode(ap, dev);
2944cdeab114STejun Heo 	}
2945cdeab114STejun Heo 
2946c6fd2807SJeff Garzik 	/* reset and determine device classes */
2947c6fd2807SJeff Garzik 	ap->ops->phy_reset(ap);
2948c6fd2807SJeff Garzik 
29491eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL) {
29503e4ec344STejun Heo 		if (dev->class != ATA_DEV_UNKNOWN)
2951c6fd2807SJeff Garzik 			classes[dev->devno] = dev->class;
2952c6fd2807SJeff Garzik 		else
2953c6fd2807SJeff Garzik 			classes[dev->devno] = ATA_DEV_NONE;
2954c6fd2807SJeff Garzik 
2955c6fd2807SJeff Garzik 		dev->class = ATA_DEV_UNKNOWN;
2956c6fd2807SJeff Garzik 	}
2957c6fd2807SJeff Garzik 
2958f31f0cc2SJeff Garzik 	/* read IDENTIFY page and configure devices. We have to do the identify
2959f31f0cc2SJeff Garzik 	   specific sequence bass-ackwards so that PDIAG- is released by
2960f31f0cc2SJeff Garzik 	   the slave device */
2961f31f0cc2SJeff Garzik 
29621eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ALL_REVERSE) {
2963f58229f8STejun Heo 		if (tries[dev->devno])
2964f58229f8STejun Heo 			dev->class = classes[dev->devno];
2965c6fd2807SJeff Garzik 
2966c6fd2807SJeff Garzik 		if (!ata_dev_enabled(dev))
2967c6fd2807SJeff Garzik 			continue;
2968c6fd2807SJeff Garzik 
2969bff04647STejun Heo 		rc = ata_dev_read_id(dev, &dev->class, ATA_READID_POSTRESET,
2970bff04647STejun Heo 				     dev->id);
2971c6fd2807SJeff Garzik 		if (rc)
2972c6fd2807SJeff Garzik 			goto fail;
2973f31f0cc2SJeff Garzik 	}
2974f31f0cc2SJeff Garzik 
2975be0d18dfSAlan Cox 	/* Now ask for the cable type as PDIAG- should have been released */
2976be0d18dfSAlan Cox 	if (ap->ops->cable_detect)
2977be0d18dfSAlan Cox 		ap->cbl = ap->ops->cable_detect(ap);
2978be0d18dfSAlan Cox 
29791eca4365STejun Heo 	/* We may have SATA bridge glue hiding here irrespective of
29801eca4365STejun Heo 	 * the reported cable types and sensed types.  When SATA
29811eca4365STejun Heo 	 * drives indicate we have a bridge, we don't know which end
29821eca4365STejun Heo 	 * of the link the bridge is which is a problem.
29831eca4365STejun Heo 	 */
29841eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED)
2985614fe29bSAlan Cox 		if (ata_id_is_sata(dev->id))
2986614fe29bSAlan Cox 			ap->cbl = ATA_CBL_SATA;
2987614fe29bSAlan Cox 
2988f31f0cc2SJeff Garzik 	/* After the identify sequence we can now set up the devices. We do
2989f31f0cc2SJeff Garzik 	   this in the normal order so that the user doesn't get confused */
2990f31f0cc2SJeff Garzik 
29911eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED) {
29929af5c9c9STejun Heo 		ap->link.eh_context.i.flags |= ATA_EHI_PRINTINFO;
2993efdaedc4STejun Heo 		rc = ata_dev_configure(dev);
29949af5c9c9STejun Heo 		ap->link.eh_context.i.flags &= ~ATA_EHI_PRINTINFO;
2995c6fd2807SJeff Garzik 		if (rc)
2996c6fd2807SJeff Garzik 			goto fail;
2997c6fd2807SJeff Garzik 	}
2998c6fd2807SJeff Garzik 
2999c6fd2807SJeff Garzik 	/* configure transfer mode */
30000260731fSTejun Heo 	rc = ata_set_mode(&ap->link, &dev);
30014ae72a1eSTejun Heo 	if (rc)
3002c6fd2807SJeff Garzik 		goto fail;
3003c6fd2807SJeff Garzik 
30041eca4365STejun Heo 	ata_for_each_dev(dev, &ap->link, ENABLED)
3005c6fd2807SJeff Garzik 		return 0;
3006c6fd2807SJeff Garzik 
3007c6fd2807SJeff Garzik 	return -ENODEV;
3008c6fd2807SJeff Garzik 
3009c6fd2807SJeff Garzik  fail:
30104ae72a1eSTejun Heo 	tries[dev->devno]--;
30114ae72a1eSTejun Heo 
3012c6fd2807SJeff Garzik 	switch (rc) {
3013c6fd2807SJeff Garzik 	case -EINVAL:
30144ae72a1eSTejun Heo 		/* eeek, something went very wrong, give up */
3015c6fd2807SJeff Garzik 		tries[dev->devno] = 0;
3016c6fd2807SJeff Garzik 		break;
30174ae72a1eSTejun Heo 
30184ae72a1eSTejun Heo 	case -ENODEV:
30194ae72a1eSTejun Heo 		/* give it just one more chance */
30204ae72a1eSTejun Heo 		tries[dev->devno] = min(tries[dev->devno], 1);
3021df561f66SGustavo A. R. Silva 		fallthrough;
3022c6fd2807SJeff Garzik 	case -EIO:
30234ae72a1eSTejun Heo 		if (tries[dev->devno] == 1) {
30244ae72a1eSTejun Heo 			/* This is the last chance, better to slow
30254ae72a1eSTejun Heo 			 * down than lose it.
30264ae72a1eSTejun Heo 			 */
3027a07d499bSTejun Heo 			sata_down_spd_limit(&ap->link, 0);
30284ae72a1eSTejun Heo 			ata_down_xfermask_limit(dev, ATA_DNXFER_PIO);
30294ae72a1eSTejun Heo 		}
3030c6fd2807SJeff Garzik 	}
3031c6fd2807SJeff Garzik 
30324ae72a1eSTejun Heo 	if (!tries[dev->devno])
3033c6fd2807SJeff Garzik 		ata_dev_disable(dev);
3034c6fd2807SJeff Garzik 
3035c6fd2807SJeff Garzik 	goto retry;
3036c6fd2807SJeff Garzik }
3037c6fd2807SJeff Garzik 
3038c6fd2807SJeff Garzik /**
3039c6fd2807SJeff Garzik  *	sata_print_link_status - Print SATA link status
3040936fd732STejun Heo  *	@link: SATA link to printk link status about
3041c6fd2807SJeff Garzik  *
3042c6fd2807SJeff Garzik  *	This function prints link speed and status of a SATA link.
3043c6fd2807SJeff Garzik  *
3044c6fd2807SJeff Garzik  *	LOCKING:
3045c6fd2807SJeff Garzik  *	None.
3046c6fd2807SJeff Garzik  */
30476bdb4fc9SAdrian Bunk static void sata_print_link_status(struct ata_link *link)
3048c6fd2807SJeff Garzik {
3049c6fd2807SJeff Garzik 	u32 sstatus, scontrol, tmp;
3050c6fd2807SJeff Garzik 
3051936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus))
3052c6fd2807SJeff Garzik 		return;
305355d5ba55SLi Zhong 	if (sata_scr_read(link, SCR_CONTROL, &scontrol))
305455d5ba55SLi Zhong 		return;
3055c6fd2807SJeff Garzik 
3056b1c72916STejun Heo 	if (ata_phys_link_online(link)) {
3057c6fd2807SJeff Garzik 		tmp = (sstatus >> 4) & 0xf;
3058a9a79dfeSJoe Perches 		ata_link_info(link, "SATA link up %s (SStatus %X SControl %X)\n",
3059c6fd2807SJeff Garzik 			      sata_spd_string(tmp), sstatus, scontrol);
3060c6fd2807SJeff Garzik 	} else {
3061a9a79dfeSJoe Perches 		ata_link_info(link, "SATA link down (SStatus %X SControl %X)\n",
3062c6fd2807SJeff Garzik 			      sstatus, scontrol);
3063c6fd2807SJeff Garzik 	}
3064c6fd2807SJeff Garzik }
3065c6fd2807SJeff Garzik 
3066c6fd2807SJeff Garzik /**
3067c6fd2807SJeff Garzik  *	ata_dev_pair		-	return other device on cable
3068c6fd2807SJeff Garzik  *	@adev: device
3069c6fd2807SJeff Garzik  *
3070c6fd2807SJeff Garzik  *	Obtain the other device on the same cable, or if none is
3071c6fd2807SJeff Garzik  *	present NULL is returned
3072c6fd2807SJeff Garzik  */
3073c6fd2807SJeff Garzik 
3074c6fd2807SJeff Garzik struct ata_device *ata_dev_pair(struct ata_device *adev)
3075c6fd2807SJeff Garzik {
30769af5c9c9STejun Heo 	struct ata_link *link = adev->link;
30779af5c9c9STejun Heo 	struct ata_device *pair = &link->device[1 - adev->devno];
3078c6fd2807SJeff Garzik 	if (!ata_dev_enabled(pair))
3079c6fd2807SJeff Garzik 		return NULL;
3080c6fd2807SJeff Garzik 	return pair;
3081c6fd2807SJeff Garzik }
3082a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_dev_pair);
3083c6fd2807SJeff Garzik 
3084c6fd2807SJeff Garzik /**
3085c6fd2807SJeff Garzik  *	sata_down_spd_limit - adjust SATA spd limit downward
3086936fd732STejun Heo  *	@link: Link to adjust SATA spd limit for
3087a07d499bSTejun Heo  *	@spd_limit: Additional limit
3088c6fd2807SJeff Garzik  *
3089936fd732STejun Heo  *	Adjust SATA spd limit of @link downward.  Note that this
3090c6fd2807SJeff Garzik  *	function only adjusts the limit.  The change must be applied
3091c6fd2807SJeff Garzik  *	using sata_set_spd().
3092c6fd2807SJeff Garzik  *
3093a07d499bSTejun Heo  *	If @spd_limit is non-zero, the speed is limited to equal to or
3094a07d499bSTejun Heo  *	lower than @spd_limit if such speed is supported.  If
3095a07d499bSTejun Heo  *	@spd_limit is slower than any supported speed, only the lowest
3096a07d499bSTejun Heo  *	supported speed is allowed.
3097a07d499bSTejun Heo  *
3098c6fd2807SJeff Garzik  *	LOCKING:
3099c6fd2807SJeff Garzik  *	Inherited from caller.
3100c6fd2807SJeff Garzik  *
3101c6fd2807SJeff Garzik  *	RETURNS:
3102c6fd2807SJeff Garzik  *	0 on success, negative errno on failure
3103c6fd2807SJeff Garzik  */
3104a07d499bSTejun Heo int sata_down_spd_limit(struct ata_link *link, u32 spd_limit)
3105c6fd2807SJeff Garzik {
3106c6fd2807SJeff Garzik 	u32 sstatus, spd, mask;
3107a07d499bSTejun Heo 	int rc, bit;
3108c6fd2807SJeff Garzik 
3109936fd732STejun Heo 	if (!sata_scr_valid(link))
3110008a7896STejun Heo 		return -EOPNOTSUPP;
3111008a7896STejun Heo 
3112008a7896STejun Heo 	/* If SCR can be read, use it to determine the current SPD.
3113936fd732STejun Heo 	 * If not, use cached value in link->sata_spd.
3114008a7896STejun Heo 	 */
3115936fd732STejun Heo 	rc = sata_scr_read(link, SCR_STATUS, &sstatus);
31169913ff8aSTejun Heo 	if (rc == 0 && ata_sstatus_online(sstatus))
3117008a7896STejun Heo 		spd = (sstatus >> 4) & 0xf;
3118008a7896STejun Heo 	else
3119936fd732STejun Heo 		spd = link->sata_spd;
3120c6fd2807SJeff Garzik 
3121936fd732STejun Heo 	mask = link->sata_spd_limit;
3122c6fd2807SJeff Garzik 	if (mask <= 1)
3123c6fd2807SJeff Garzik 		return -EINVAL;
3124008a7896STejun Heo 
3125008a7896STejun Heo 	/* unconditionally mask off the highest bit */
3126a07d499bSTejun Heo 	bit = fls(mask) - 1;
3127a07d499bSTejun Heo 	mask &= ~(1 << bit);
3128c6fd2807SJeff Garzik 
31292dc0b46bSDavid Milburn 	/*
31302dc0b46bSDavid Milburn 	 * Mask off all speeds higher than or equal to the current one.  At
31312dc0b46bSDavid Milburn 	 * this point, if current SPD is not available and we previously
31322dc0b46bSDavid Milburn 	 * recorded the link speed from SStatus, the driver has already
31332dc0b46bSDavid Milburn 	 * masked off the highest bit so mask should already be 1 or 0.
31342dc0b46bSDavid Milburn 	 * Otherwise, we should not force 1.5Gbps on a link where we have
31352dc0b46bSDavid Milburn 	 * not previously recorded speed from SStatus.  Just return in this
31362dc0b46bSDavid Milburn 	 * case.
3137008a7896STejun Heo 	 */
3138008a7896STejun Heo 	if (spd > 1)
3139008a7896STejun Heo 		mask &= (1 << (spd - 1)) - 1;
314069f2c934SDamien Le Moal 	else if (link->sata_spd)
31412dc0b46bSDavid Milburn 		return -EINVAL;
3142008a7896STejun Heo 
3143008a7896STejun Heo 	/* were we already at the bottom? */
3144c6fd2807SJeff Garzik 	if (!mask)
3145c6fd2807SJeff Garzik 		return -EINVAL;
3146c6fd2807SJeff Garzik 
3147a07d499bSTejun Heo 	if (spd_limit) {
3148a07d499bSTejun Heo 		if (mask & ((1 << spd_limit) - 1))
3149a07d499bSTejun Heo 			mask &= (1 << spd_limit) - 1;
3150a07d499bSTejun Heo 		else {
3151a07d499bSTejun Heo 			bit = ffs(mask) - 1;
3152a07d499bSTejun Heo 			mask = 1 << bit;
3153a07d499bSTejun Heo 		}
3154a07d499bSTejun Heo 	}
3155a07d499bSTejun Heo 
3156936fd732STejun Heo 	link->sata_spd_limit = mask;
3157c6fd2807SJeff Garzik 
3158a9a79dfeSJoe Perches 	ata_link_warn(link, "limiting SATA link speed to %s\n",
3159c6fd2807SJeff Garzik 		      sata_spd_string(fls(mask)));
3160c6fd2807SJeff Garzik 
3161c6fd2807SJeff Garzik 	return 0;
3162c6fd2807SJeff Garzik }
3163c6fd2807SJeff Garzik 
3164a9b2c120SBartlomiej Zolnierkiewicz #ifdef CONFIG_ATA_ACPI
3165c6fd2807SJeff Garzik /**
3166a0f79b92STejun Heo  *	ata_timing_cycle2mode - find xfer mode for the specified cycle duration
3167a0f79b92STejun Heo  *	@xfer_shift: ATA_SHIFT_* value for transfer type to examine.
3168a0f79b92STejun Heo  *	@cycle: cycle duration in ns
3169a0f79b92STejun Heo  *
3170a0f79b92STejun Heo  *	Return matching xfer mode for @cycle.  The returned mode is of
3171a0f79b92STejun Heo  *	the transfer type specified by @xfer_shift.  If @cycle is too
3172a0f79b92STejun Heo  *	slow for @xfer_shift, 0xff is returned.  If @cycle is faster
3173a0f79b92STejun Heo  *	than the fastest known mode, the fasted mode is returned.
3174a0f79b92STejun Heo  *
3175a0f79b92STejun Heo  *	LOCKING:
3176a0f79b92STejun Heo  *	None.
3177a0f79b92STejun Heo  *
3178a0f79b92STejun Heo  *	RETURNS:
3179a0f79b92STejun Heo  *	Matching xfer_mode, 0xff if no match found.
3180a0f79b92STejun Heo  */
3181a0f79b92STejun Heo u8 ata_timing_cycle2mode(unsigned int xfer_shift, int cycle)
3182a0f79b92STejun Heo {
3183a0f79b92STejun Heo 	u8 base_mode = 0xff, last_mode = 0xff;
3184a0f79b92STejun Heo 	const struct ata_xfer_ent *ent;
3185a0f79b92STejun Heo 	const struct ata_timing *t;
3186a0f79b92STejun Heo 
3187a0f79b92STejun Heo 	for (ent = ata_xfer_tbl; ent->shift >= 0; ent++)
3188a0f79b92STejun Heo 		if (ent->shift == xfer_shift)
3189a0f79b92STejun Heo 			base_mode = ent->base;
3190a0f79b92STejun Heo 
3191a0f79b92STejun Heo 	for (t = ata_timing_find_mode(base_mode);
3192a0f79b92STejun Heo 	     t && ata_xfer_mode2shift(t->mode) == xfer_shift; t++) {
3193a0f79b92STejun Heo 		unsigned short this_cycle;
3194a0f79b92STejun Heo 
3195a0f79b92STejun Heo 		switch (xfer_shift) {
3196a0f79b92STejun Heo 		case ATA_SHIFT_PIO:
3197a0f79b92STejun Heo 		case ATA_SHIFT_MWDMA:
3198a0f79b92STejun Heo 			this_cycle = t->cycle;
3199a0f79b92STejun Heo 			break;
3200a0f79b92STejun Heo 		case ATA_SHIFT_UDMA:
3201a0f79b92STejun Heo 			this_cycle = t->udma;
3202a0f79b92STejun Heo 			break;
3203a0f79b92STejun Heo 		default:
3204a0f79b92STejun Heo 			return 0xff;
3205a0f79b92STejun Heo 		}
3206a0f79b92STejun Heo 
3207a0f79b92STejun Heo 		if (cycle > this_cycle)
3208a0f79b92STejun Heo 			break;
3209a0f79b92STejun Heo 
3210a0f79b92STejun Heo 		last_mode = t->mode;
3211a0f79b92STejun Heo 	}
3212a0f79b92STejun Heo 
3213a0f79b92STejun Heo 	return last_mode;
3214a0f79b92STejun Heo }
3215a9b2c120SBartlomiej Zolnierkiewicz #endif
3216a0f79b92STejun Heo 
3217a0f79b92STejun Heo /**
3218c6fd2807SJeff Garzik  *	ata_down_xfermask_limit - adjust dev xfer masks downward
3219c6fd2807SJeff Garzik  *	@dev: Device to adjust xfer masks
3220458337dbSTejun Heo  *	@sel: ATA_DNXFER_* selector
3221c6fd2807SJeff Garzik  *
3222c6fd2807SJeff Garzik  *	Adjust xfer masks of @dev downward.  Note that this function
3223c6fd2807SJeff Garzik  *	does not apply the change.  Invoking ata_set_mode() afterwards
3224c6fd2807SJeff Garzik  *	will apply the limit.
3225c6fd2807SJeff Garzik  *
3226c6fd2807SJeff Garzik  *	LOCKING:
3227c6fd2807SJeff Garzik  *	Inherited from caller.
3228c6fd2807SJeff Garzik  *
3229c6fd2807SJeff Garzik  *	RETURNS:
3230c6fd2807SJeff Garzik  *	0 on success, negative errno on failure
3231c6fd2807SJeff Garzik  */
3232458337dbSTejun Heo int ata_down_xfermask_limit(struct ata_device *dev, unsigned int sel)
3233c6fd2807SJeff Garzik {
3234458337dbSTejun Heo 	char buf[32];
3235f0a6d77bSSergey Shtylyov 	unsigned int orig_mask, xfer_mask;
3236f0a6d77bSSergey Shtylyov 	unsigned int pio_mask, mwdma_mask, udma_mask;
3237458337dbSTejun Heo 	int quiet, highbit;
3238c6fd2807SJeff Garzik 
3239458337dbSTejun Heo 	quiet = !!(sel & ATA_DNXFER_QUIET);
3240458337dbSTejun Heo 	sel &= ~ATA_DNXFER_QUIET;
3241458337dbSTejun Heo 
3242458337dbSTejun Heo 	xfer_mask = orig_mask = ata_pack_xfermask(dev->pio_mask,
3243458337dbSTejun Heo 						  dev->mwdma_mask,
3244c6fd2807SJeff Garzik 						  dev->udma_mask);
3245458337dbSTejun Heo 	ata_unpack_xfermask(xfer_mask, &pio_mask, &mwdma_mask, &udma_mask);
3246c6fd2807SJeff Garzik 
3247458337dbSTejun Heo 	switch (sel) {
3248458337dbSTejun Heo 	case ATA_DNXFER_PIO:
3249458337dbSTejun Heo 		highbit = fls(pio_mask) - 1;
3250458337dbSTejun Heo 		pio_mask &= ~(1 << highbit);
3251458337dbSTejun Heo 		break;
3252458337dbSTejun Heo 
3253458337dbSTejun Heo 	case ATA_DNXFER_DMA:
3254458337dbSTejun Heo 		if (udma_mask) {
3255458337dbSTejun Heo 			highbit = fls(udma_mask) - 1;
3256458337dbSTejun Heo 			udma_mask &= ~(1 << highbit);
3257458337dbSTejun Heo 			if (!udma_mask)
3258458337dbSTejun Heo 				return -ENOENT;
3259458337dbSTejun Heo 		} else if (mwdma_mask) {
3260458337dbSTejun Heo 			highbit = fls(mwdma_mask) - 1;
3261458337dbSTejun Heo 			mwdma_mask &= ~(1 << highbit);
3262458337dbSTejun Heo 			if (!mwdma_mask)
3263458337dbSTejun Heo 				return -ENOENT;
3264458337dbSTejun Heo 		}
3265458337dbSTejun Heo 		break;
3266458337dbSTejun Heo 
3267458337dbSTejun Heo 	case ATA_DNXFER_40C:
3268458337dbSTejun Heo 		udma_mask &= ATA_UDMA_MASK_40C;
3269458337dbSTejun Heo 		break;
3270458337dbSTejun Heo 
3271458337dbSTejun Heo 	case ATA_DNXFER_FORCE_PIO0:
3272458337dbSTejun Heo 		pio_mask &= 1;
3273df561f66SGustavo A. R. Silva 		fallthrough;
3274458337dbSTejun Heo 	case ATA_DNXFER_FORCE_PIO:
3275458337dbSTejun Heo 		mwdma_mask = 0;
3276458337dbSTejun Heo 		udma_mask = 0;
3277458337dbSTejun Heo 		break;
3278458337dbSTejun Heo 
3279458337dbSTejun Heo 	default:
3280458337dbSTejun Heo 		BUG();
3281458337dbSTejun Heo 	}
3282458337dbSTejun Heo 
3283458337dbSTejun Heo 	xfer_mask &= ata_pack_xfermask(pio_mask, mwdma_mask, udma_mask);
3284458337dbSTejun Heo 
3285458337dbSTejun Heo 	if (!(xfer_mask & ATA_MASK_PIO) || xfer_mask == orig_mask)
3286458337dbSTejun Heo 		return -ENOENT;
3287458337dbSTejun Heo 
3288458337dbSTejun Heo 	if (!quiet) {
3289458337dbSTejun Heo 		if (xfer_mask & (ATA_MASK_MWDMA | ATA_MASK_UDMA))
3290458337dbSTejun Heo 			snprintf(buf, sizeof(buf), "%s:%s",
3291458337dbSTejun Heo 				 ata_mode_string(xfer_mask),
3292458337dbSTejun Heo 				 ata_mode_string(xfer_mask & ATA_MASK_PIO));
3293458337dbSTejun Heo 		else
3294458337dbSTejun Heo 			snprintf(buf, sizeof(buf), "%s",
3295458337dbSTejun Heo 				 ata_mode_string(xfer_mask));
3296458337dbSTejun Heo 
3297a9a79dfeSJoe Perches 		ata_dev_warn(dev, "limiting speed to %s\n", buf);
3298458337dbSTejun Heo 	}
3299c6fd2807SJeff Garzik 
3300c6fd2807SJeff Garzik 	ata_unpack_xfermask(xfer_mask, &dev->pio_mask, &dev->mwdma_mask,
3301c6fd2807SJeff Garzik 			    &dev->udma_mask);
3302c6fd2807SJeff Garzik 
3303c6fd2807SJeff Garzik 	return 0;
3304c6fd2807SJeff Garzik }
3305c6fd2807SJeff Garzik 
3306c6fd2807SJeff Garzik static int ata_dev_set_mode(struct ata_device *dev)
3307c6fd2807SJeff Garzik {
3308d0cb43b3STejun Heo 	struct ata_port *ap = dev->link->ap;
33099af5c9c9STejun Heo 	struct ata_eh_context *ehc = &dev->link->eh_context;
3310d0cb43b3STejun Heo 	const bool nosetxfer = dev->horkage & ATA_HORKAGE_NOSETXFER;
33114055dee7STejun Heo 	const char *dev_err_whine = "";
33124055dee7STejun Heo 	int ign_dev_err = 0;
3313d0cb43b3STejun Heo 	unsigned int err_mask = 0;
3314c6fd2807SJeff Garzik 	int rc;
3315c6fd2807SJeff Garzik 
3316c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_PIO;
3317c6fd2807SJeff Garzik 	if (dev->xfer_shift == ATA_SHIFT_PIO)
3318c6fd2807SJeff Garzik 		dev->flags |= ATA_DFLAG_PIO;
3319c6fd2807SJeff Garzik 
3320d0cb43b3STejun Heo 	if (nosetxfer && ap->flags & ATA_FLAG_SATA && ata_id_is_sata(dev->id))
3321d0cb43b3STejun Heo 		dev_err_whine = " (SET_XFERMODE skipped)";
3322d0cb43b3STejun Heo 	else {
3323d0cb43b3STejun Heo 		if (nosetxfer)
3324a9a79dfeSJoe Perches 			ata_dev_warn(dev,
3325d0cb43b3STejun Heo 				     "NOSETXFER but PATA detected - can't "
3326d0cb43b3STejun Heo 				     "skip SETXFER, might malfunction\n");
3327c6fd2807SJeff Garzik 		err_mask = ata_dev_set_xfermode(dev);
3328d0cb43b3STejun Heo 	}
33292dcb407eSJeff Garzik 
33304055dee7STejun Heo 	if (err_mask & ~AC_ERR_DEV)
33314055dee7STejun Heo 		goto fail;
33322dcb407eSJeff Garzik 
33334055dee7STejun Heo 	/* revalidate */
3334baa1e78aSTejun Heo 	ehc->i.flags |= ATA_EHI_POST_SETMODE;
3335422c9daaSTejun Heo 	rc = ata_dev_revalidate(dev, ATA_DEV_UNKNOWN, 0);
3336baa1e78aSTejun Heo 	ehc->i.flags &= ~ATA_EHI_POST_SETMODE;
3337c6fd2807SJeff Garzik 	if (rc)
3338c6fd2807SJeff Garzik 		return rc;
3339c6fd2807SJeff Garzik 
3340b93fda12SAlan Cox 	if (dev->xfer_shift == ATA_SHIFT_PIO) {
33414055dee7STejun Heo 		/* Old CFA may refuse this command, which is just fine */
3342b93fda12SAlan Cox 		if (ata_id_is_cfa(dev->id))
33434055dee7STejun Heo 			ign_dev_err = 1;
3344b93fda12SAlan Cox 		/* Catch several broken garbage emulations plus some pre
3345b93fda12SAlan Cox 		   ATA devices */
3346b93fda12SAlan Cox 		if (ata_id_major_version(dev->id) == 0 &&
33474055dee7STejun Heo 					dev->pio_mode <= XFER_PIO_2)
33484055dee7STejun Heo 			ign_dev_err = 1;
3349b93fda12SAlan Cox 		/* Some very old devices and some bad newer ones fail
3350b93fda12SAlan Cox 		   any kind of SET_XFERMODE request but support PIO0-2
3351b93fda12SAlan Cox 		   timings and no IORDY */
3352b93fda12SAlan Cox 		if (!ata_id_has_iordy(dev->id) && dev->pio_mode <= XFER_PIO_2)
3353b93fda12SAlan Cox 			ign_dev_err = 1;
3354b93fda12SAlan Cox 	}
33554055dee7STejun Heo 	/* Early MWDMA devices do DMA but don't allow DMA mode setting.
33564055dee7STejun Heo 	   Don't fail an MWDMA0 set IFF the device indicates it is in MWDMA0 */
33574055dee7STejun Heo 	if (dev->xfer_shift == ATA_SHIFT_MWDMA &&
33584055dee7STejun Heo 	    dev->dma_mode == XFER_MW_DMA_0 &&
33594055dee7STejun Heo 	    (dev->id[63] >> 8) & 1)
33604055dee7STejun Heo 		ign_dev_err = 1;
33614055dee7STejun Heo 
33624055dee7STejun Heo 	/* if the device is actually configured correctly, ignore dev err */
33634055dee7STejun Heo 	if (dev->xfer_mode == ata_xfer_mask2mode(ata_id_xfermask(dev->id)))
33644055dee7STejun Heo 		ign_dev_err = 1;
33654055dee7STejun Heo 
33664055dee7STejun Heo 	if (err_mask & AC_ERR_DEV) {
33674055dee7STejun Heo 		if (!ign_dev_err)
33684055dee7STejun Heo 			goto fail;
33694055dee7STejun Heo 		else
33704055dee7STejun Heo 			dev_err_whine = " (device error ignored)";
33714055dee7STejun Heo 	}
33724055dee7STejun Heo 
33734633778bSHannes Reinecke 	ata_dev_dbg(dev, "xfer_shift=%u, xfer_mode=0x%x\n",
3374c6fd2807SJeff Garzik 		    dev->xfer_shift, (int)dev->xfer_mode);
3375c6fd2807SJeff Garzik 
337607b9b6d6SDamien Le Moal 	if (!(ehc->i.flags & ATA_EHI_QUIET) ||
337707b9b6d6SDamien Le Moal 	    ehc->i.flags & ATA_EHI_DID_HARDRESET)
3378a9a79dfeSJoe Perches 		ata_dev_info(dev, "configured for %s%s\n",
33794055dee7STejun Heo 			     ata_mode_string(ata_xfer_mode2mask(dev->xfer_mode)),
33804055dee7STejun Heo 			     dev_err_whine);
33814055dee7STejun Heo 
3382c6fd2807SJeff Garzik 	return 0;
33834055dee7STejun Heo 
33844055dee7STejun Heo  fail:
3385a9a79dfeSJoe Perches 	ata_dev_err(dev, "failed to set xfermode (err_mask=0x%x)\n", err_mask);
33864055dee7STejun Heo 	return -EIO;
3387c6fd2807SJeff Garzik }
3388c6fd2807SJeff Garzik 
3389c6fd2807SJeff Garzik /**
339004351821SAlan  *	ata_do_set_mode - Program timings and issue SET FEATURES - XFER
33910260731fSTejun Heo  *	@link: link on which timings will be programmed
33921967b7ffSJoe Perches  *	@r_failed_dev: out parameter for failed device
3393c6fd2807SJeff Garzik  *
339404351821SAlan  *	Standard implementation of the function used to tune and set
339504351821SAlan  *	ATA device disk transfer mode (PIO3, UDMA6, etc.).  If
339604351821SAlan  *	ata_dev_set_mode() fails, pointer to the failing device is
3397c6fd2807SJeff Garzik  *	returned in @r_failed_dev.
3398c6fd2807SJeff Garzik  *
3399c6fd2807SJeff Garzik  *	LOCKING:
3400c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
3401c6fd2807SJeff Garzik  *
3402c6fd2807SJeff Garzik  *	RETURNS:
3403c6fd2807SJeff Garzik  *	0 on success, negative errno otherwise
3404c6fd2807SJeff Garzik  */
340504351821SAlan 
34060260731fSTejun Heo int ata_do_set_mode(struct ata_link *link, struct ata_device **r_failed_dev)
3407c6fd2807SJeff Garzik {
34080260731fSTejun Heo 	struct ata_port *ap = link->ap;
3409c6fd2807SJeff Garzik 	struct ata_device *dev;
3410f58229f8STejun Heo 	int rc = 0, used_dma = 0, found = 0;
3411c6fd2807SJeff Garzik 
3412c6fd2807SJeff Garzik 	/* step 1: calculate xfer_mask */
34131eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
3414f0a6d77bSSergey Shtylyov 		unsigned int pio_mask, dma_mask;
3415b3a70601SAlan Cox 		unsigned int mode_mask;
3416c6fd2807SJeff Garzik 
3417b3a70601SAlan Cox 		mode_mask = ATA_DMA_MASK_ATA;
3418b3a70601SAlan Cox 		if (dev->class == ATA_DEV_ATAPI)
3419b3a70601SAlan Cox 			mode_mask = ATA_DMA_MASK_ATAPI;
3420b3a70601SAlan Cox 		else if (ata_id_is_cfa(dev->id))
3421b3a70601SAlan Cox 			mode_mask = ATA_DMA_MASK_CFA;
3422b3a70601SAlan Cox 
3423c6fd2807SJeff Garzik 		ata_dev_xfermask(dev);
342433267325STejun Heo 		ata_force_xfermask(dev);
3425c6fd2807SJeff Garzik 
3426c6fd2807SJeff Garzik 		pio_mask = ata_pack_xfermask(dev->pio_mask, 0, 0);
3427b3a70601SAlan Cox 
3428b3a70601SAlan Cox 		if (libata_dma_mask & mode_mask)
342980a9c430SSergei Shtylyov 			dma_mask = ata_pack_xfermask(0, dev->mwdma_mask,
343080a9c430SSergei Shtylyov 						     dev->udma_mask);
3431b3a70601SAlan Cox 		else
3432b3a70601SAlan Cox 			dma_mask = 0;
3433b3a70601SAlan Cox 
3434c6fd2807SJeff Garzik 		dev->pio_mode = ata_xfer_mask2mode(pio_mask);
3435c6fd2807SJeff Garzik 		dev->dma_mode = ata_xfer_mask2mode(dma_mask);
3436c6fd2807SJeff Garzik 
3437c6fd2807SJeff Garzik 		found = 1;
3438b15b3ebaSAlan Cox 		if (ata_dma_enabled(dev))
3439c6fd2807SJeff Garzik 			used_dma = 1;
3440c6fd2807SJeff Garzik 	}
3441c6fd2807SJeff Garzik 	if (!found)
3442c6fd2807SJeff Garzik 		goto out;
3443c6fd2807SJeff Garzik 
3444c6fd2807SJeff Garzik 	/* step 2: always set host PIO timings */
34451eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
344670cd071eSTejun Heo 		if (dev->pio_mode == 0xff) {
3447a9a79dfeSJoe Perches 			ata_dev_warn(dev, "no PIO support\n");
3448c6fd2807SJeff Garzik 			rc = -EINVAL;
3449c6fd2807SJeff Garzik 			goto out;
3450c6fd2807SJeff Garzik 		}
3451c6fd2807SJeff Garzik 
3452c6fd2807SJeff Garzik 		dev->xfer_mode = dev->pio_mode;
3453c6fd2807SJeff Garzik 		dev->xfer_shift = ATA_SHIFT_PIO;
3454c6fd2807SJeff Garzik 		if (ap->ops->set_piomode)
3455c6fd2807SJeff Garzik 			ap->ops->set_piomode(ap, dev);
3456c6fd2807SJeff Garzik 	}
3457c6fd2807SJeff Garzik 
3458c6fd2807SJeff Garzik 	/* step 3: set host DMA timings */
34591eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
34601eca4365STejun Heo 		if (!ata_dma_enabled(dev))
3461c6fd2807SJeff Garzik 			continue;
3462c6fd2807SJeff Garzik 
3463c6fd2807SJeff Garzik 		dev->xfer_mode = dev->dma_mode;
3464c6fd2807SJeff Garzik 		dev->xfer_shift = ata_xfer_mode2shift(dev->dma_mode);
3465c6fd2807SJeff Garzik 		if (ap->ops->set_dmamode)
3466c6fd2807SJeff Garzik 			ap->ops->set_dmamode(ap, dev);
3467c6fd2807SJeff Garzik 	}
3468c6fd2807SJeff Garzik 
3469c6fd2807SJeff Garzik 	/* step 4: update devices' xfer mode */
34701eca4365STejun Heo 	ata_for_each_dev(dev, link, ENABLED) {
3471c6fd2807SJeff Garzik 		rc = ata_dev_set_mode(dev);
3472c6fd2807SJeff Garzik 		if (rc)
3473c6fd2807SJeff Garzik 			goto out;
3474c6fd2807SJeff Garzik 	}
3475c6fd2807SJeff Garzik 
3476c6fd2807SJeff Garzik 	/* Record simplex status. If we selected DMA then the other
3477c6fd2807SJeff Garzik 	 * host channels are not permitted to do so.
3478c6fd2807SJeff Garzik 	 */
3479cca3974eSJeff Garzik 	if (used_dma && (ap->host->flags & ATA_HOST_SIMPLEX))
3480032af1ceSAlan 		ap->host->simplex_claimed = ap;
3481c6fd2807SJeff Garzik 
3482c6fd2807SJeff Garzik  out:
3483c6fd2807SJeff Garzik 	if (rc)
3484c6fd2807SJeff Garzik 		*r_failed_dev = dev;
3485c6fd2807SJeff Garzik 	return rc;
3486c6fd2807SJeff Garzik }
3487a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_do_set_mode);
3488c6fd2807SJeff Garzik 
3489c6fd2807SJeff Garzik /**
3490aa2731adSTejun Heo  *	ata_wait_ready - wait for link to become ready
3491aa2731adSTejun Heo  *	@link: link to be waited on
3492aa2731adSTejun Heo  *	@deadline: deadline jiffies for the operation
3493aa2731adSTejun Heo  *	@check_ready: callback to check link readiness
3494aa2731adSTejun Heo  *
3495aa2731adSTejun Heo  *	Wait for @link to become ready.  @check_ready should return
3496aa2731adSTejun Heo  *	positive number if @link is ready, 0 if it isn't, -ENODEV if
3497aa2731adSTejun Heo  *	link doesn't seem to be occupied, other errno for other error
3498aa2731adSTejun Heo  *	conditions.
3499aa2731adSTejun Heo  *
3500aa2731adSTejun Heo  *	Transient -ENODEV conditions are allowed for
3501aa2731adSTejun Heo  *	ATA_TMOUT_FF_WAIT.
3502aa2731adSTejun Heo  *
3503aa2731adSTejun Heo  *	LOCKING:
3504aa2731adSTejun Heo  *	EH context.
3505aa2731adSTejun Heo  *
3506aa2731adSTejun Heo  *	RETURNS:
3507c9b5560aSMasanari Iida  *	0 if @link is ready before @deadline; otherwise, -errno.
3508aa2731adSTejun Heo  */
3509aa2731adSTejun Heo int ata_wait_ready(struct ata_link *link, unsigned long deadline,
3510aa2731adSTejun Heo 		   int (*check_ready)(struct ata_link *link))
3511aa2731adSTejun Heo {
3512aa2731adSTejun Heo 	unsigned long start = jiffies;
3513b48d58f5STejun Heo 	unsigned long nodev_deadline;
3514aa2731adSTejun Heo 	int warned = 0;
3515aa2731adSTejun Heo 
3516b48d58f5STejun Heo 	/* choose which 0xff timeout to use, read comment in libata.h */
3517b48d58f5STejun Heo 	if (link->ap->host->flags & ATA_HOST_PARALLEL_SCAN)
3518b48d58f5STejun Heo 		nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT_LONG);
3519b48d58f5STejun Heo 	else
3520b48d58f5STejun Heo 		nodev_deadline = ata_deadline(start, ATA_TMOUT_FF_WAIT);
3521b48d58f5STejun Heo 
3522b1c72916STejun Heo 	/* Slave readiness can't be tested separately from master.  On
3523b1c72916STejun Heo 	 * M/S emulation configuration, this function should be called
3524b1c72916STejun Heo 	 * only on the master and it will handle both master and slave.
3525b1c72916STejun Heo 	 */
3526b1c72916STejun Heo 	WARN_ON(link == link->ap->slave_link);
3527b1c72916STejun Heo 
3528aa2731adSTejun Heo 	if (time_after(nodev_deadline, deadline))
3529aa2731adSTejun Heo 		nodev_deadline = deadline;
3530aa2731adSTejun Heo 
3531aa2731adSTejun Heo 	while (1) {
3532aa2731adSTejun Heo 		unsigned long now = jiffies;
3533aa2731adSTejun Heo 		int ready, tmp;
3534aa2731adSTejun Heo 
3535aa2731adSTejun Heo 		ready = tmp = check_ready(link);
3536aa2731adSTejun Heo 		if (ready > 0)
3537aa2731adSTejun Heo 			return 0;
3538aa2731adSTejun Heo 
3539b48d58f5STejun Heo 		/*
3540b48d58f5STejun Heo 		 * -ENODEV could be transient.  Ignore -ENODEV if link
3541aa2731adSTejun Heo 		 * is online.  Also, some SATA devices take a long
3542b48d58f5STejun Heo 		 * time to clear 0xff after reset.  Wait for
3543b48d58f5STejun Heo 		 * ATA_TMOUT_FF_WAIT[_LONG] on -ENODEV if link isn't
3544b48d58f5STejun Heo 		 * offline.
3545aa2731adSTejun Heo 		 *
3546aa2731adSTejun Heo 		 * Note that some PATA controllers (pata_ali) explode
3547aa2731adSTejun Heo 		 * if status register is read more than once when
3548aa2731adSTejun Heo 		 * there's no device attached.
3549aa2731adSTejun Heo 		 */
3550aa2731adSTejun Heo 		if (ready == -ENODEV) {
3551aa2731adSTejun Heo 			if (ata_link_online(link))
3552aa2731adSTejun Heo 				ready = 0;
3553aa2731adSTejun Heo 			else if ((link->ap->flags & ATA_FLAG_SATA) &&
3554aa2731adSTejun Heo 				 !ata_link_offline(link) &&
3555aa2731adSTejun Heo 				 time_before(now, nodev_deadline))
3556aa2731adSTejun Heo 				ready = 0;
3557aa2731adSTejun Heo 		}
3558aa2731adSTejun Heo 
3559aa2731adSTejun Heo 		if (ready)
3560aa2731adSTejun Heo 			return ready;
3561aa2731adSTejun Heo 		if (time_after(now, deadline))
3562aa2731adSTejun Heo 			return -EBUSY;
3563aa2731adSTejun Heo 
3564aa2731adSTejun Heo 		if (!warned && time_after(now, start + 5 * HZ) &&
3565aa2731adSTejun Heo 		    (deadline - now > 3 * HZ)) {
3566a9a79dfeSJoe Perches 			ata_link_warn(link,
3567aa2731adSTejun Heo 				"link is slow to respond, please be patient "
3568aa2731adSTejun Heo 				"(ready=%d)\n", tmp);
3569aa2731adSTejun Heo 			warned = 1;
3570aa2731adSTejun Heo 		}
3571aa2731adSTejun Heo 
357297750cebSTejun Heo 		ata_msleep(link->ap, 50);
3573aa2731adSTejun Heo 	}
3574aa2731adSTejun Heo }
3575aa2731adSTejun Heo 
3576aa2731adSTejun Heo /**
3577aa2731adSTejun Heo  *	ata_wait_after_reset - wait for link to become ready after reset
3578aa2731adSTejun Heo  *	@link: link to be waited on
3579aa2731adSTejun Heo  *	@deadline: deadline jiffies for the operation
3580aa2731adSTejun Heo  *	@check_ready: callback to check link readiness
3581aa2731adSTejun Heo  *
3582aa2731adSTejun Heo  *	Wait for @link to become ready after reset.
3583aa2731adSTejun Heo  *
3584aa2731adSTejun Heo  *	LOCKING:
3585aa2731adSTejun Heo  *	EH context.
3586aa2731adSTejun Heo  *
3587aa2731adSTejun Heo  *	RETURNS:
3588c9b5560aSMasanari Iida  *	0 if @link is ready before @deadline; otherwise, -errno.
3589aa2731adSTejun Heo  */
35902b4221bbSHarvey Harrison int ata_wait_after_reset(struct ata_link *link, unsigned long deadline,
3591aa2731adSTejun Heo 				int (*check_ready)(struct ata_link *link))
3592aa2731adSTejun Heo {
359397750cebSTejun Heo 	ata_msleep(link->ap, ATA_WAIT_AFTER_RESET);
3594aa2731adSTejun Heo 
3595aa2731adSTejun Heo 	return ata_wait_ready(link, deadline, check_ready);
3596aa2731adSTejun Heo }
3597a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_wait_after_reset);
3598aa2731adSTejun Heo 
3599aa2731adSTejun Heo /**
36000aa1113dSTejun Heo  *	ata_std_prereset - prepare for reset
3601cc0680a5STejun Heo  *	@link: ATA link to be reset
3602d4b2bab4STejun Heo  *	@deadline: deadline jiffies for the operation
3603c6fd2807SJeff Garzik  *
3604cc0680a5STejun Heo  *	@link is about to be reset.  Initialize it.  Failure from
3605b8cffc6aSTejun Heo  *	prereset makes libata abort whole reset sequence and give up
3606b8cffc6aSTejun Heo  *	that port, so prereset should be best-effort.  It does its
3607b8cffc6aSTejun Heo  *	best to prepare for reset sequence but if things go wrong, it
3608b8cffc6aSTejun Heo  *	should just whine, not fail.
3609c6fd2807SJeff Garzik  *
3610c6fd2807SJeff Garzik  *	LOCKING:
3611c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3612c6fd2807SJeff Garzik  *
3613c6fd2807SJeff Garzik  *	RETURNS:
3614ac1eb665SSergey Shtylyov  *	Always 0.
3615c6fd2807SJeff Garzik  */
36160aa1113dSTejun Heo int ata_std_prereset(struct ata_link *link, unsigned long deadline)
3617c6fd2807SJeff Garzik {
3618cc0680a5STejun Heo 	struct ata_port *ap = link->ap;
3619936fd732STejun Heo 	struct ata_eh_context *ehc = &link->eh_context;
3620c6fd2807SJeff Garzik 	const unsigned long *timing = sata_ehc_deb_timing(ehc);
3621c6fd2807SJeff Garzik 	int rc;
3622c6fd2807SJeff Garzik 
3623c6fd2807SJeff Garzik 	/* if we're about to do hardreset, nothing more to do */
3624c6fd2807SJeff Garzik 	if (ehc->i.action & ATA_EH_HARDRESET)
3625c6fd2807SJeff Garzik 		return 0;
3626c6fd2807SJeff Garzik 
3627936fd732STejun Heo 	/* if SATA, resume link */
3628a16abc0bSTejun Heo 	if (ap->flags & ATA_FLAG_SATA) {
3629936fd732STejun Heo 		rc = sata_link_resume(link, timing, deadline);
3630b8cffc6aSTejun Heo 		/* whine about phy resume failure but proceed */
3631b8cffc6aSTejun Heo 		if (rc && rc != -EOPNOTSUPP)
3632a9a79dfeSJoe Perches 			ata_link_warn(link,
3633a9a79dfeSJoe Perches 				      "failed to resume link for reset (errno=%d)\n",
3634a9a79dfeSJoe Perches 				      rc);
3635c6fd2807SJeff Garzik 	}
3636c6fd2807SJeff Garzik 
363745db2f6cSTejun Heo 	/* no point in trying softreset on offline link */
3638b1c72916STejun Heo 	if (ata_phys_link_offline(link))
363945db2f6cSTejun Heo 		ehc->i.action &= ~ATA_EH_SOFTRESET;
364045db2f6cSTejun Heo 
3641c6fd2807SJeff Garzik 	return 0;
3642c6fd2807SJeff Garzik }
3643a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_std_prereset);
3644c6fd2807SJeff Garzik 
3645c6fd2807SJeff Garzik /**
364657c9efdfSTejun Heo  *	sata_std_hardreset - COMRESET w/o waiting or classification
364757c9efdfSTejun Heo  *	@link: link to reset
364857c9efdfSTejun Heo  *	@class: resulting class of attached device
364957c9efdfSTejun Heo  *	@deadline: deadline jiffies for the operation
365057c9efdfSTejun Heo  *
365157c9efdfSTejun Heo  *	Standard SATA COMRESET w/o waiting or classification.
365257c9efdfSTejun Heo  *
365357c9efdfSTejun Heo  *	LOCKING:
365457c9efdfSTejun Heo  *	Kernel thread context (may sleep)
365557c9efdfSTejun Heo  *
365657c9efdfSTejun Heo  *	RETURNS:
365757c9efdfSTejun Heo  *	0 if link offline, -EAGAIN if link online, -errno on errors.
365857c9efdfSTejun Heo  */
365957c9efdfSTejun Heo int sata_std_hardreset(struct ata_link *link, unsigned int *class,
366057c9efdfSTejun Heo 		       unsigned long deadline)
366157c9efdfSTejun Heo {
366257c9efdfSTejun Heo 	const unsigned long *timing = sata_ehc_deb_timing(&link->eh_context);
366357c9efdfSTejun Heo 	bool online;
366457c9efdfSTejun Heo 	int rc;
366557c9efdfSTejun Heo 
366657c9efdfSTejun Heo 	/* do hardreset */
366757c9efdfSTejun Heo 	rc = sata_link_hardreset(link, timing, deadline, &online, NULL);
366857c9efdfSTejun Heo 	return online ? -EAGAIN : rc;
366957c9efdfSTejun Heo }
3670a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(sata_std_hardreset);
367157c9efdfSTejun Heo 
367257c9efdfSTejun Heo /**
3673203c75b8STejun Heo  *	ata_std_postreset - standard postreset callback
3674cc0680a5STejun Heo  *	@link: the target ata_link
3675c6fd2807SJeff Garzik  *	@classes: classes of attached devices
3676c6fd2807SJeff Garzik  *
3677c6fd2807SJeff Garzik  *	This function is invoked after a successful reset.  Note that
3678c6fd2807SJeff Garzik  *	the device might have been reset more than once using
3679c6fd2807SJeff Garzik  *	different reset methods before postreset is invoked.
3680c6fd2807SJeff Garzik  *
3681c6fd2807SJeff Garzik  *	LOCKING:
3682c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3683c6fd2807SJeff Garzik  */
3684203c75b8STejun Heo void ata_std_postreset(struct ata_link *link, unsigned int *classes)
3685c6fd2807SJeff Garzik {
3686f046519fSTejun Heo 	u32 serror;
3687f046519fSTejun Heo 
3688f046519fSTejun Heo 	/* reset complete, clear SError */
3689f046519fSTejun Heo 	if (!sata_scr_read(link, SCR_ERROR, &serror))
3690f046519fSTejun Heo 		sata_scr_write(link, SCR_ERROR, serror);
3691f046519fSTejun Heo 
3692c6fd2807SJeff Garzik 	/* print link status */
3693936fd732STejun Heo 	sata_print_link_status(link);
3694c6fd2807SJeff Garzik }
3695a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_std_postreset);
3696c6fd2807SJeff Garzik 
3697c6fd2807SJeff Garzik /**
3698c6fd2807SJeff Garzik  *	ata_dev_same_device - Determine whether new ID matches configured device
3699c6fd2807SJeff Garzik  *	@dev: device to compare against
3700c6fd2807SJeff Garzik  *	@new_class: class of the new device
3701c6fd2807SJeff Garzik  *	@new_id: IDENTIFY page of the new device
3702c6fd2807SJeff Garzik  *
3703c6fd2807SJeff Garzik  *	Compare @new_class and @new_id against @dev and determine
3704c6fd2807SJeff Garzik  *	whether @dev is the device indicated by @new_class and
3705c6fd2807SJeff Garzik  *	@new_id.
3706c6fd2807SJeff Garzik  *
3707c6fd2807SJeff Garzik  *	LOCKING:
3708c6fd2807SJeff Garzik  *	None.
3709c6fd2807SJeff Garzik  *
3710c6fd2807SJeff Garzik  *	RETURNS:
3711c6fd2807SJeff Garzik  *	1 if @dev matches @new_class and @new_id, 0 otherwise.
3712c6fd2807SJeff Garzik  */
3713c6fd2807SJeff Garzik static int ata_dev_same_device(struct ata_device *dev, unsigned int new_class,
3714c6fd2807SJeff Garzik 			       const u16 *new_id)
3715c6fd2807SJeff Garzik {
3716c6fd2807SJeff Garzik 	const u16 *old_id = dev->id;
3717a0cf733bSTejun Heo 	unsigned char model[2][ATA_ID_PROD_LEN + 1];
3718a0cf733bSTejun Heo 	unsigned char serial[2][ATA_ID_SERNO_LEN + 1];
3719c6fd2807SJeff Garzik 
3720c6fd2807SJeff Garzik 	if (dev->class != new_class) {
3721a9a79dfeSJoe Perches 		ata_dev_info(dev, "class mismatch %d != %d\n",
3722c6fd2807SJeff Garzik 			     dev->class, new_class);
3723c6fd2807SJeff Garzik 		return 0;
3724c6fd2807SJeff Garzik 	}
3725c6fd2807SJeff Garzik 
3726a0cf733bSTejun Heo 	ata_id_c_string(old_id, model[0], ATA_ID_PROD, sizeof(model[0]));
3727a0cf733bSTejun Heo 	ata_id_c_string(new_id, model[1], ATA_ID_PROD, sizeof(model[1]));
3728a0cf733bSTejun Heo 	ata_id_c_string(old_id, serial[0], ATA_ID_SERNO, sizeof(serial[0]));
3729a0cf733bSTejun Heo 	ata_id_c_string(new_id, serial[1], ATA_ID_SERNO, sizeof(serial[1]));
3730c6fd2807SJeff Garzik 
3731c6fd2807SJeff Garzik 	if (strcmp(model[0], model[1])) {
3732a9a79dfeSJoe Perches 		ata_dev_info(dev, "model number mismatch '%s' != '%s'\n",
3733a9a79dfeSJoe Perches 			     model[0], model[1]);
3734c6fd2807SJeff Garzik 		return 0;
3735c6fd2807SJeff Garzik 	}
3736c6fd2807SJeff Garzik 
3737c6fd2807SJeff Garzik 	if (strcmp(serial[0], serial[1])) {
3738a9a79dfeSJoe Perches 		ata_dev_info(dev, "serial number mismatch '%s' != '%s'\n",
3739a9a79dfeSJoe Perches 			     serial[0], serial[1]);
3740c6fd2807SJeff Garzik 		return 0;
3741c6fd2807SJeff Garzik 	}
3742c6fd2807SJeff Garzik 
3743c6fd2807SJeff Garzik 	return 1;
3744c6fd2807SJeff Garzik }
3745c6fd2807SJeff Garzik 
3746c6fd2807SJeff Garzik /**
3747fe30911bSTejun Heo  *	ata_dev_reread_id - Re-read IDENTIFY data
37483fae450cSHenrik Kretzschmar  *	@dev: target ATA device
3749bff04647STejun Heo  *	@readid_flags: read ID flags
3750c6fd2807SJeff Garzik  *
3751c6fd2807SJeff Garzik  *	Re-read IDENTIFY page and make sure @dev is still attached to
3752c6fd2807SJeff Garzik  *	the port.
3753c6fd2807SJeff Garzik  *
3754c6fd2807SJeff Garzik  *	LOCKING:
3755c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
3756c6fd2807SJeff Garzik  *
3757c6fd2807SJeff Garzik  *	RETURNS:
3758c6fd2807SJeff Garzik  *	0 on success, negative errno otherwise
3759c6fd2807SJeff Garzik  */
3760fe30911bSTejun Heo int ata_dev_reread_id(struct ata_device *dev, unsigned int readid_flags)
3761c6fd2807SJeff Garzik {
3762c6fd2807SJeff Garzik 	unsigned int class = dev->class;
37639af5c9c9STejun Heo 	u16 *id = (void *)dev->link->ap->sector_buf;
3764c6fd2807SJeff Garzik 	int rc;
3765c6fd2807SJeff Garzik 
3766c6fd2807SJeff Garzik 	/* read ID data */
3767bff04647STejun Heo 	rc = ata_dev_read_id(dev, &class, readid_flags, id);
3768c6fd2807SJeff Garzik 	if (rc)
3769fe30911bSTejun Heo 		return rc;
3770c6fd2807SJeff Garzik 
3771c6fd2807SJeff Garzik 	/* is the device still there? */
3772fe30911bSTejun Heo 	if (!ata_dev_same_device(dev, class, id))
3773fe30911bSTejun Heo 		return -ENODEV;
3774c6fd2807SJeff Garzik 
3775c6fd2807SJeff Garzik 	memcpy(dev->id, id, sizeof(id[0]) * ATA_ID_WORDS);
3776fe30911bSTejun Heo 	return 0;
3777fe30911bSTejun Heo }
3778fe30911bSTejun Heo 
3779fe30911bSTejun Heo /**
3780fe30911bSTejun Heo  *	ata_dev_revalidate - Revalidate ATA device
3781fe30911bSTejun Heo  *	@dev: device to revalidate
3782422c9daaSTejun Heo  *	@new_class: new class code
3783fe30911bSTejun Heo  *	@readid_flags: read ID flags
3784fe30911bSTejun Heo  *
3785fe30911bSTejun Heo  *	Re-read IDENTIFY page, make sure @dev is still attached to the
3786fe30911bSTejun Heo  *	port and reconfigure it according to the new IDENTIFY page.
3787fe30911bSTejun Heo  *
3788fe30911bSTejun Heo  *	LOCKING:
3789fe30911bSTejun Heo  *	Kernel thread context (may sleep)
3790fe30911bSTejun Heo  *
3791fe30911bSTejun Heo  *	RETURNS:
3792fe30911bSTejun Heo  *	0 on success, negative errno otherwise
3793fe30911bSTejun Heo  */
3794422c9daaSTejun Heo int ata_dev_revalidate(struct ata_device *dev, unsigned int new_class,
3795422c9daaSTejun Heo 		       unsigned int readid_flags)
3796fe30911bSTejun Heo {
37976ddcd3b0STejun Heo 	u64 n_sectors = dev->n_sectors;
37985920dadfSTejun Heo 	u64 n_native_sectors = dev->n_native_sectors;
3799fe30911bSTejun Heo 	int rc;
3800fe30911bSTejun Heo 
3801fe30911bSTejun Heo 	if (!ata_dev_enabled(dev))
3802fe30911bSTejun Heo 		return -ENODEV;
3803fe30911bSTejun Heo 
3804422c9daaSTejun Heo 	/* fail early if !ATA && !ATAPI to avoid issuing [P]IDENTIFY to PMP */
3805422c9daaSTejun Heo 	if (ata_class_enabled(new_class) &&
3806f0d0613dSBorislav Petkov 	    new_class != ATA_DEV_ATA &&
3807f0d0613dSBorislav Petkov 	    new_class != ATA_DEV_ATAPI &&
38089162c657SHannes Reinecke 	    new_class != ATA_DEV_ZAC &&
3809f0d0613dSBorislav Petkov 	    new_class != ATA_DEV_SEMB) {
3810a9a79dfeSJoe Perches 		ata_dev_info(dev, "class mismatch %u != %u\n",
3811422c9daaSTejun Heo 			     dev->class, new_class);
3812422c9daaSTejun Heo 		rc = -ENODEV;
3813422c9daaSTejun Heo 		goto fail;
3814422c9daaSTejun Heo 	}
3815422c9daaSTejun Heo 
3816fe30911bSTejun Heo 	/* re-read ID */
3817fe30911bSTejun Heo 	rc = ata_dev_reread_id(dev, readid_flags);
3818fe30911bSTejun Heo 	if (rc)
3819fe30911bSTejun Heo 		goto fail;
3820c6fd2807SJeff Garzik 
3821c6fd2807SJeff Garzik 	/* configure device according to the new ID */
3822efdaedc4STejun Heo 	rc = ata_dev_configure(dev);
38236ddcd3b0STejun Heo 	if (rc)
38246ddcd3b0STejun Heo 		goto fail;
38256ddcd3b0STejun Heo 
38266ddcd3b0STejun Heo 	/* verify n_sectors hasn't changed */
3827445d211bSTejun Heo 	if (dev->class != ATA_DEV_ATA || !n_sectors ||
3828445d211bSTejun Heo 	    dev->n_sectors == n_sectors)
3829445d211bSTejun Heo 		return 0;
3830445d211bSTejun Heo 
3831445d211bSTejun Heo 	/* n_sectors has changed */
3832a9a79dfeSJoe Perches 	ata_dev_warn(dev, "n_sectors mismatch %llu != %llu\n",
38336ddcd3b0STejun Heo 		     (unsigned long long)n_sectors,
38346ddcd3b0STejun Heo 		     (unsigned long long)dev->n_sectors);
3835445d211bSTejun Heo 
38365920dadfSTejun Heo 	/*
38375920dadfSTejun Heo 	 * Something could have caused HPA to be unlocked
3838445d211bSTejun Heo 	 * involuntarily.  If n_native_sectors hasn't changed and the
3839445d211bSTejun Heo 	 * new size matches it, keep the device.
38405920dadfSTejun Heo 	 */
38415920dadfSTejun Heo 	if (dev->n_native_sectors == n_native_sectors &&
3842445d211bSTejun Heo 	    dev->n_sectors > n_sectors && dev->n_sectors == n_native_sectors) {
3843a9a79dfeSJoe Perches 		ata_dev_warn(dev,
38445920dadfSTejun Heo 			     "new n_sectors matches native, probably "
384568939ce5STejun Heo 			     "late HPA unlock, n_sectors updated\n");
384668939ce5STejun Heo 		/* use the larger n_sectors */
3847445d211bSTejun Heo 		return 0;
3848445d211bSTejun Heo 	}
3849445d211bSTejun Heo 
3850445d211bSTejun Heo 	/*
3851445d211bSTejun Heo 	 * Some BIOSes boot w/o HPA but resume w/ HPA locked.  Try
3852445d211bSTejun Heo 	 * unlocking HPA in those cases.
3853445d211bSTejun Heo 	 *
3854445d211bSTejun Heo 	 * https://bugzilla.kernel.org/show_bug.cgi?id=15396
3855445d211bSTejun Heo 	 */
3856445d211bSTejun Heo 	if (dev->n_native_sectors == n_native_sectors &&
3857445d211bSTejun Heo 	    dev->n_sectors < n_sectors && n_sectors == n_native_sectors &&
3858445d211bSTejun Heo 	    !(dev->horkage & ATA_HORKAGE_BROKEN_HPA)) {
3859a9a79dfeSJoe Perches 		ata_dev_warn(dev,
3860445d211bSTejun Heo 			     "old n_sectors matches native, probably "
3861445d211bSTejun Heo 			     "late HPA lock, will try to unlock HPA\n");
3862445d211bSTejun Heo 		/* try unlocking HPA */
3863445d211bSTejun Heo 		dev->flags |= ATA_DFLAG_UNLOCK_HPA;
3864445d211bSTejun Heo 		rc = -EIO;
3865445d211bSTejun Heo 	} else
3866445d211bSTejun Heo 		rc = -ENODEV;
3867445d211bSTejun Heo 
3868445d211bSTejun Heo 	/* restore original n_[native_]sectors and fail */
38695920dadfSTejun Heo 	dev->n_native_sectors = n_native_sectors;
38705920dadfSTejun Heo 	dev->n_sectors = n_sectors;
3871c6fd2807SJeff Garzik  fail:
3872a9a79dfeSJoe Perches 	ata_dev_err(dev, "revalidation failed (errno=%d)\n", rc);
3873c6fd2807SJeff Garzik 	return rc;
3874c6fd2807SJeff Garzik }
3875c6fd2807SJeff Garzik 
38766919a0a6SAlan Cox struct ata_blacklist_entry {
38776919a0a6SAlan Cox 	const char *model_num;
38786919a0a6SAlan Cox 	const char *model_rev;
38796919a0a6SAlan Cox 	unsigned long horkage;
38806919a0a6SAlan Cox };
38816919a0a6SAlan Cox 
38826919a0a6SAlan Cox static const struct ata_blacklist_entry ata_device_blacklist [] = {
38836919a0a6SAlan Cox 	/* Devices with DMA related problems under Linux */
38846919a0a6SAlan Cox 	{ "WDC AC11000H",	NULL,		ATA_HORKAGE_NODMA },
38856919a0a6SAlan Cox 	{ "WDC AC22100H",	NULL,		ATA_HORKAGE_NODMA },
38866919a0a6SAlan Cox 	{ "WDC AC32500H",	NULL,		ATA_HORKAGE_NODMA },
38876919a0a6SAlan Cox 	{ "WDC AC33100H",	NULL,		ATA_HORKAGE_NODMA },
38886919a0a6SAlan Cox 	{ "WDC AC31600H",	NULL,		ATA_HORKAGE_NODMA },
38896919a0a6SAlan Cox 	{ "WDC AC32100H",	"24.09P07",	ATA_HORKAGE_NODMA },
38906919a0a6SAlan Cox 	{ "WDC AC23200L",	"21.10N21",	ATA_HORKAGE_NODMA },
38916919a0a6SAlan Cox 	{ "Compaq CRD-8241B", 	NULL,		ATA_HORKAGE_NODMA },
38926919a0a6SAlan Cox 	{ "CRD-8400B",		NULL, 		ATA_HORKAGE_NODMA },
38937da4c935SMark Lord 	{ "CRD-848[02]B",	NULL,		ATA_HORKAGE_NODMA },
38946919a0a6SAlan Cox 	{ "CRD-84",		NULL,		ATA_HORKAGE_NODMA },
38956919a0a6SAlan Cox 	{ "SanDisk SDP3B",	NULL,		ATA_HORKAGE_NODMA },
38966919a0a6SAlan Cox 	{ "SanDisk SDP3B-64",	NULL,		ATA_HORKAGE_NODMA },
38976919a0a6SAlan Cox 	{ "SANYO CD-ROM CRD",	NULL,		ATA_HORKAGE_NODMA },
38986919a0a6SAlan Cox 	{ "HITACHI CDR-8",	NULL,		ATA_HORKAGE_NODMA },
38997da4c935SMark Lord 	{ "HITACHI CDR-8[34]35",NULL,		ATA_HORKAGE_NODMA },
39006919a0a6SAlan Cox 	{ "Toshiba CD-ROM XM-6202B", NULL,	ATA_HORKAGE_NODMA },
39016919a0a6SAlan Cox 	{ "TOSHIBA CD-ROM XM-1702BC", NULL,	ATA_HORKAGE_NODMA },
39026919a0a6SAlan Cox 	{ "CD-532E-A", 		NULL,		ATA_HORKAGE_NODMA },
39036919a0a6SAlan Cox 	{ "E-IDE CD-ROM CR-840",NULL,		ATA_HORKAGE_NODMA },
39046919a0a6SAlan Cox 	{ "CD-ROM Drive/F5A",	NULL,		ATA_HORKAGE_NODMA },
39056919a0a6SAlan Cox 	{ "WPI CDD-820", 	NULL,		ATA_HORKAGE_NODMA },
39066919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SC-148C", NULL,	ATA_HORKAGE_NODMA },
39076919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SC",	NULL,		ATA_HORKAGE_NODMA },
39086919a0a6SAlan Cox 	{ "ATAPI CD-ROM DRIVE 40X MAXIMUM",NULL,ATA_HORKAGE_NODMA },
39096919a0a6SAlan Cox 	{ "_NEC DV5800A", 	NULL,		ATA_HORKAGE_NODMA },
39106919a0a6SAlan Cox 	{ "SAMSUNG CD-ROM SN-124", "N001",	ATA_HORKAGE_NODMA },
391139f19886SDave Jones 	{ "Seagate STT20000A", NULL,		ATA_HORKAGE_NODMA },
3912d70e551cSPrarit Bhargava 	{ " 2GB ATA Flash Disk", "ADMA428M",	ATA_HORKAGE_NODMA },
3913b00622fcSShikha Jain 	{ "VRFDFC22048UCHC-TE*", NULL,		ATA_HORKAGE_NODMA },
39143af9a77aSTejun Heo 	/* Odd clown on sil3726/4726 PMPs */
391550af2fa1STejun Heo 	{ "Config  Disk",	NULL,		ATA_HORKAGE_DISABLE },
3916a66307d4SHannes Reinecke 	/* Similar story with ASMedia 1092 */
3917a66307d4SHannes Reinecke 	{ "ASMT109x- Config",	NULL,		ATA_HORKAGE_DISABLE },
39186919a0a6SAlan Cox 
391918d6e9d5SAlbert Lee 	/* Weird ATAPI devices */
392040a1d531STejun Heo 	{ "TORiSAN DVD-ROM DRD-N216", NULL,	ATA_HORKAGE_MAX_SEC_128 },
39216a87e42eSTejun Heo 	{ "QUANTUM DAT    DAT72-000", NULL,	ATA_HORKAGE_ATAPI_MOD16_DMA },
3922a32450e1SShan Hai 	{ "Slimtype DVD A  DS8A8SH", NULL,	ATA_HORKAGE_MAX_SEC_LBA48 },
39230523f037SShan Hai 	{ "Slimtype DVD A  DS8A9SH", NULL,	ATA_HORKAGE_MAX_SEC_LBA48 },
392418d6e9d5SAlbert Lee 
3925af34d637SDavid Milburn 	/*
3926af34d637SDavid Milburn 	 * Causes silent data corruption with higher max sects.
3927af34d637SDavid Milburn 	 * http://lkml.kernel.org/g/x49wpy40ysk.fsf@segfault.boston.devel.redhat.com
3928af34d637SDavid Milburn 	 */
3929af34d637SDavid Milburn 	{ "ST380013AS",		"3.20",		ATA_HORKAGE_MAX_SEC_1024 },
3930af34d637SDavid Milburn 
39311488a1e3STejun Heo 	/*
3932e0edc8c5STejun Heo 	 * These devices time out with higher max sects.
39331488a1e3STejun Heo 	 * https://bugzilla.kernel.org/show_bug.cgi?id=121671
39341488a1e3STejun Heo 	 */
3935e0edc8c5STejun Heo 	{ "LITEON CX1-JB*-HP",	NULL,		ATA_HORKAGE_MAX_SEC_1024 },
3936db5ff909SXinyu Lin 	{ "LITEON EP1-*",	NULL,		ATA_HORKAGE_MAX_SEC_1024 },
39371488a1e3STejun Heo 
39386919a0a6SAlan Cox 	/* Devices we expect to fail diagnostics */
39396919a0a6SAlan Cox 
39406919a0a6SAlan Cox 	/* Devices where NCQ should be avoided */
39416919a0a6SAlan Cox 	/* NCQ is slow */
39426919a0a6SAlan Cox 	{ "WDC WD740ADFD-00",	NULL,		ATA_HORKAGE_NONCQ },
3943ef1429c0SDamien Le Moal 	{ "WDC WD740ADFD-00NLR1", NULL,		ATA_HORKAGE_NONCQ },
394409125ea6STejun Heo 	/* http://thread.gmane.org/gmane.linux.ide/14907 */
394509125ea6STejun Heo 	{ "FUJITSU MHT2060BH",	NULL,		ATA_HORKAGE_NONCQ },
39467acfaf30SPaul Rolland 	/* NCQ is broken */
3947539cc7c7SJeff Garzik 	{ "Maxtor *",		"BANC*",	ATA_HORKAGE_NONCQ },
39480e3dbc01SAlan Cox 	{ "Maxtor 7V300F0",	"VA111630",	ATA_HORKAGE_NONCQ },
3949da6f0ec2SPaolo Ornati 	{ "ST380817AS",		"3.42",		ATA_HORKAGE_NONCQ },
3950e41bd3e8STejun Heo 	{ "ST3160023AS",	"3.42",		ATA_HORKAGE_NONCQ },
39515ccfca97SLubomir Bulej 	{ "OCZ CORE_SSD",	"02.10104",	ATA_HORKAGE_NONCQ },
3952539cc7c7SJeff Garzik 
3953ac70a964STejun Heo 	/* Seagate NCQ + FLUSH CACHE firmware bug */
39544d1f9082SMark Lord 	{ "ST31500341AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
3955ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
3956d10d491fSTejun Heo 
39574d1f9082SMark Lord 	{ "ST31000333AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
3958d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
3959d10d491fSTejun Heo 
39604d1f9082SMark Lord 	{ "ST3640[36]23AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
3961d10d491fSTejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
3962d10d491fSTejun Heo 
39634d1f9082SMark Lord 	{ "ST3320[68]13AS",	"SD1[5-9]",	ATA_HORKAGE_NONCQ |
3964ac70a964STejun Heo 						ATA_HORKAGE_FIRMWARE_WARN },
3965ac70a964STejun Heo 
396631f6264eSHans de Goede 	/* drives which fail FPDMA_AA activation (some may freeze afterwards)
396731f6264eSHans de Goede 	   the ST disks also have LPM issues */
39688756a25bSHans de Goede 	{ "ST1000LM024 HN-M101MBB", NULL,	ATA_HORKAGE_BROKEN_FPDMA_AA |
3969ef1429c0SDamien Le Moal 						ATA_HORKAGE_NOLPM },
397008c85d2aSAleksei Mamlin 	{ "VB0250EAVER",	"HPG7",		ATA_HORKAGE_BROKEN_FPDMA_AA },
397187809942SMichele Baldessari 
397236e337d0SRobert Hancock 	/* Blacklist entries taken from Silicon Image 3124/3132
397336e337d0SRobert Hancock 	   Windows driver .inf file - also several Linux problem reports */
3974ef1429c0SDamien Le Moal 	{ "HTS541060G9SA00",    "MB3OC60D",     ATA_HORKAGE_NONCQ },
3975ef1429c0SDamien Le Moal 	{ "HTS541080G9SA00",    "MB4OC60D",     ATA_HORKAGE_NONCQ },
3976ef1429c0SDamien Le Moal 	{ "HTS541010G9SA00",    "MBZOC60D",     ATA_HORKAGE_NONCQ },
39776919a0a6SAlan Cox 
397868b0ddb2STejun Heo 	/* https://bugzilla.kernel.org/show_bug.cgi?id=15573 */
3979ef1429c0SDamien Le Moal 	{ "C300-CTFDDAC128MAG",	"0001",		ATA_HORKAGE_NONCQ },
398068b0ddb2STejun Heo 
39813b545563STejun Heo 	/* Sandisk SD7/8/9s lock up hard on large trims */
3982ef1429c0SDamien Le Moal 	{ "SanDisk SD[789]*",	NULL,		ATA_HORKAGE_MAX_TRIM_128M },
3983322579dcSTejun Heo 
398416c55b03STejun Heo 	/* devices which puke on READ_NATIVE_MAX */
3985ef1429c0SDamien Le Moal 	{ "HDS724040KLSA80",	"KFAOA20N",	ATA_HORKAGE_BROKEN_HPA },
398616c55b03STejun Heo 	{ "WDC WD3200JD-00KLB0", "WD-WCAMR1130137", ATA_HORKAGE_BROKEN_HPA },
398716c55b03STejun Heo 	{ "WDC WD2500JD-00HBB0", "WD-WMAL71490727", ATA_HORKAGE_BROKEN_HPA },
398816c55b03STejun Heo 	{ "MAXTOR 6L080L4",	"A93.0500",	ATA_HORKAGE_BROKEN_HPA },
39896919a0a6SAlan Cox 
39907831387bSTejun Heo 	/* this one allows HPA unlocking but fails IOs on the area */
39917831387bSTejun Heo 	{ "OCZ-VERTEX",		    "1.30",	ATA_HORKAGE_BROKEN_HPA },
39927831387bSTejun Heo 
399393328e11SAlan Cox 	/* Devices which report 1 sector over size HPA */
3994ef1429c0SDamien Le Moal 	{ "ST340823A",		NULL,		ATA_HORKAGE_HPA_SIZE },
3995ef1429c0SDamien Le Moal 	{ "ST320413A",		NULL,		ATA_HORKAGE_HPA_SIZE },
3996ef1429c0SDamien Le Moal 	{ "ST310211A",		NULL,		ATA_HORKAGE_HPA_SIZE },
399793328e11SAlan Cox 
39986bbfd53dSAlan Cox 	/* Devices which get the IVB wrong */
3999ef1429c0SDamien Le Moal 	{ "QUANTUM FIREBALLlct10 05", "A03.0900", ATA_HORKAGE_IVB },
4000a79067e5SAlan Cox 	/* Maybe we should just blacklist TSSTcorp... */
4001ef1429c0SDamien Le Moal 	{ "TSSTcorp CDDVDW SH-S202[HJN]", "SB0[01]",  ATA_HORKAGE_IVB },
40026bbfd53dSAlan Cox 
40039ce8e307SJens Axboe 	/* Devices that do not need bridging limits applied */
4004ef1429c0SDamien Le Moal 	{ "MTRON MSP-SATA*",		NULL,	ATA_HORKAGE_BRIDGE_OK },
4005ef1429c0SDamien Le Moal 	{ "BUFFALO HD-QSU2/R5",		NULL,	ATA_HORKAGE_BRIDGE_OK },
40069ce8e307SJens Axboe 
40079062712fSTejun Heo 	/* Devices which aren't very happy with higher link speeds */
4008ef1429c0SDamien Le Moal 	{ "WD My Book",			NULL,	ATA_HORKAGE_1_5_GBPS },
4009ef1429c0SDamien Le Moal 	{ "Seagate FreeAgent GoFlex",	NULL,	ATA_HORKAGE_1_5_GBPS },
40109062712fSTejun Heo 
4011d0cb43b3STejun Heo 	/*
4012d0cb43b3STejun Heo 	 * Devices which choke on SETXFER.  Applies only if both the
4013d0cb43b3STejun Heo 	 * device and controller are SATA.
4014d0cb43b3STejun Heo 	 */
4015cd691876STejun Heo 	{ "PIONEER DVD-RW  DVRTD08",	NULL,	ATA_HORKAGE_NOSETXFER },
40163a25179eSVladimir LAVALLADE 	{ "PIONEER DVD-RW  DVRTD08A",	NULL,	ATA_HORKAGE_NOSETXFER },
40173a25179eSVladimir LAVALLADE 	{ "PIONEER DVD-RW  DVR-215",	NULL,	ATA_HORKAGE_NOSETXFER },
4018cd691876STejun Heo 	{ "PIONEER DVD-RW  DVR-212D",	NULL,	ATA_HORKAGE_NOSETXFER },
4019cd691876STejun Heo 	{ "PIONEER DVD-RW  DVR-216D",	NULL,	ATA_HORKAGE_NOSETXFER },
4020d0cb43b3STejun Heo 
4021ea08aec7SNiklas Cassel 	/* These specific Pioneer models have LPM issues */
4022ea08aec7SNiklas Cassel 	{ "PIONEER BD-RW   BDR-207M",	NULL,	ATA_HORKAGE_NOLPM },
4023ea08aec7SNiklas Cassel 	{ "PIONEER BD-RW   BDR-205",	NULL,	ATA_HORKAGE_NOLPM },
4024ea08aec7SNiklas Cassel 
4025b17e5729SKai-Heng Feng 	/* Crucial BX100 SSD 500GB has broken LPM support */
40263bf7b5d6SHans de Goede 	{ "CT500BX100SSD1",		NULL,	ATA_HORKAGE_NOLPM },
4027b17e5729SKai-Heng Feng 
4028d418ff56SHans de Goede 	/* 512GB MX100 with MU01 firmware has both queued TRIM and LPM issues */
4029d418ff56SHans de Goede 	{ "Crucial_CT512MX100*",	"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
40309c7be59fSHans de Goede 						ATA_HORKAGE_ZERO_AFTER_TRIM |
4031ef1429c0SDamien Le Moal 						ATA_HORKAGE_NOLPM },
4032d418ff56SHans de Goede 	/* 512GB MX100 with newer firmware has only LPM issues */
4033d418ff56SHans de Goede 	{ "Crucial_CT512MX100*",	NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM |
4034ef1429c0SDamien Le Moal 						ATA_HORKAGE_NOLPM },
40359c7be59fSHans de Goede 
403662ac3f73SHans de Goede 	/* 480GB+ M500 SSDs have both queued TRIM and LPM issues */
403762ac3f73SHans de Goede 	{ "Crucial_CT480M500*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
403862ac3f73SHans de Goede 						ATA_HORKAGE_ZERO_AFTER_TRIM |
4039ef1429c0SDamien Le Moal 						ATA_HORKAGE_NOLPM },
404062ac3f73SHans de Goede 	{ "Crucial_CT960M500*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
404162ac3f73SHans de Goede 						ATA_HORKAGE_ZERO_AFTER_TRIM |
4042ef1429c0SDamien Le Moal 						ATA_HORKAGE_NOLPM },
404362ac3f73SHans de Goede 
404476936e9aSFrançois Cami 	/* These specific Samsung models/firmware-revs do not handle LPM well */
4045ef1429c0SDamien Le Moal 	{ "SAMSUNG MZMPC128HBFU-000MV", "CXM14M1Q", ATA_HORKAGE_NOLPM },
4046ef1429c0SDamien Le Moal 	{ "SAMSUNG SSD PM830 mSATA *",  "CXM13D1Q", ATA_HORKAGE_NOLPM },
4047ef1429c0SDamien Le Moal 	{ "SAMSUNG MZ7TD256HAFV-000L9", NULL,       ATA_HORKAGE_NOLPM },
4048ef1429c0SDamien Le Moal 	{ "SAMSUNG MZ7TE512HMHP-000L1", "EXT06L0Q", ATA_HORKAGE_NOLPM },
4049b5b4d3a5SHans de Goede 
4050f78dea06SMarc Carino 	/* devices that don't properly handle queued TRIM commands */
4051136d769eSSudip Mukherjee 	{ "Micron_M500IT_*",		"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
4052ef1429c0SDamien Le Moal 						ATA_HORKAGE_ZERO_AFTER_TRIM },
4053243918beSMartin K. Petersen 	{ "Micron_M500_*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
4054ef1429c0SDamien Le Moal 						ATA_HORKAGE_ZERO_AFTER_TRIM },
4055ff7f53fbSMartin K. Petersen 	{ "Crucial_CT*M500*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
4056ef1429c0SDamien Le Moal 						ATA_HORKAGE_ZERO_AFTER_TRIM },
40579051bd39SMartin K. Petersen 	{ "Micron_M5[15]0_*",		"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
4058ef1429c0SDamien Le Moal 						ATA_HORKAGE_ZERO_AFTER_TRIM },
4059ff7f53fbSMartin K. Petersen 	{ "Crucial_CT*M550*",		"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
4060ef1429c0SDamien Le Moal 						ATA_HORKAGE_ZERO_AFTER_TRIM },
4061ff7f53fbSMartin K. Petersen 	{ "Crucial_CT*MX100*",		"MU01",	ATA_HORKAGE_NO_NCQ_TRIM |
4062ef1429c0SDamien Le Moal 						ATA_HORKAGE_ZERO_AFTER_TRIM },
406353997522SChristian Lamparter 	{ "Samsung SSD 840 EVO*",	NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
406453997522SChristian Lamparter 						ATA_HORKAGE_NO_DMA_LOG |
4065ef1429c0SDamien Le Moal 						ATA_HORKAGE_ZERO_AFTER_TRIM },
4066ca6bfcb2SJu Hyung Park 	{ "Samsung SSD 840*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
4067ef1429c0SDamien Le Moal 						ATA_HORKAGE_ZERO_AFTER_TRIM },
4068ca6bfcb2SJu Hyung Park 	{ "Samsung SSD 850*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
4069ef1429c0SDamien Le Moal 						ATA_HORKAGE_ZERO_AFTER_TRIM },
40708a6430abSHans de Goede 	{ "Samsung SSD 860*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
40717a8526a5SKate Hsuan 						ATA_HORKAGE_ZERO_AFTER_TRIM |
4072ef1429c0SDamien Le Moal 						ATA_HORKAGE_NO_NCQ_ON_ATI },
40738a6430abSHans de Goede 	{ "Samsung SSD 870*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
40747a8526a5SKate Hsuan 						ATA_HORKAGE_ZERO_AFTER_TRIM |
4075ef1429c0SDamien Le Moal 						ATA_HORKAGE_NO_NCQ_ON_ATI },
4076ead08957SPatrick McLean 	{ "SAMSUNG*MZ7LH*",		NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
4077ead08957SPatrick McLean 						ATA_HORKAGE_ZERO_AFTER_TRIM |
4078ead08957SPatrick McLean 						ATA_HORKAGE_NO_NCQ_ON_ATI, },
40797a7184b0SGuillermo A. Amaral 	{ "FCCT*M500*",			NULL,	ATA_HORKAGE_NO_NCQ_TRIM |
4080ef1429c0SDamien Le Moal 						ATA_HORKAGE_ZERO_AFTER_TRIM },
4081e61f7d1cSMartin K. Petersen 
4082cda57b1bSArne Fitzenreiter 	/* devices that don't properly handle TRIM commands */
4083ef1429c0SDamien Le Moal 	{ "SuperSSpeed S238*",		NULL,	ATA_HORKAGE_NOTRIM },
4084ef1429c0SDamien Le Moal 	{ "M88V29*",			NULL,	ATA_HORKAGE_NOTRIM },
4085cda57b1bSArne Fitzenreiter 
4086e61f7d1cSMartin K. Petersen 	/*
4087e61f7d1cSMartin K. Petersen 	 * As defined, the DRAT (Deterministic Read After Trim) and RZAT
4088e61f7d1cSMartin K. Petersen 	 * (Return Zero After Trim) flags in the ATA Command Set are
4089e61f7d1cSMartin K. Petersen 	 * unreliable in the sense that they only define what happens if
4090e61f7d1cSMartin K. Petersen 	 * the device successfully executed the DSM TRIM command. TRIM
4091e61f7d1cSMartin K. Petersen 	 * is only advisory, however, and the device is free to silently
4092e61f7d1cSMartin K. Petersen 	 * ignore all or parts of the request.
4093e61f7d1cSMartin K. Petersen 	 *
4094e61f7d1cSMartin K. Petersen 	 * Whitelist drives that are known to reliably return zeroes
4095e61f7d1cSMartin K. Petersen 	 * after TRIM.
4096e61f7d1cSMartin K. Petersen 	 */
4097e61f7d1cSMartin K. Petersen 
4098e61f7d1cSMartin K. Petersen 	/*
4099e61f7d1cSMartin K. Petersen 	 * The intel 510 drive has buggy DRAT/RZAT. Explicitly exclude
4100e61f7d1cSMartin K. Petersen 	 * that model before whitelisting all other intel SSDs.
4101e61f7d1cSMartin K. Petersen 	 */
4102ef1429c0SDamien Le Moal 	{ "INTEL*SSDSC2MH*",		NULL,	0 },
4103e61f7d1cSMartin K. Petersen 
4104ef1429c0SDamien Le Moal 	{ "Micron*",			NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM },
4105ef1429c0SDamien Le Moal 	{ "Crucial*",			NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM },
4106ef1429c0SDamien Le Moal 	{ "INTEL*SSD*", 		NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM },
4107ef1429c0SDamien Le Moal 	{ "SSD*INTEL*",			NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM },
4108ef1429c0SDamien Le Moal 	{ "Samsung*SSD*",		NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM },
4109ef1429c0SDamien Le Moal 	{ "SAMSUNG*SSD*",		NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM },
4110ef1429c0SDamien Le Moal 	{ "SAMSUNG*MZ7KM*",		NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM },
4111ef1429c0SDamien Le Moal 	{ "ST[1248][0248]0[FH]*",	NULL,	ATA_HORKAGE_ZERO_AFTER_TRIM },
4112f78dea06SMarc Carino 
4113ecd75ad5STejun Heo 	/*
4114ecd75ad5STejun Heo 	 * Some WD SATA-I drives spin up and down erratically when the link
4115ecd75ad5STejun Heo 	 * is put into the slumber mode.  We don't have full list of the
4116ecd75ad5STejun Heo 	 * affected devices.  Disable LPM if the device matches one of the
4117ecd75ad5STejun Heo 	 * known prefixes and is SATA-1.  As a side effect LPM partial is
4118ecd75ad5STejun Heo 	 * lost too.
4119ecd75ad5STejun Heo 	 *
4120ecd75ad5STejun Heo 	 * https://bugzilla.kernel.org/show_bug.cgi?id=57211
4121ecd75ad5STejun Heo 	 */
4122ecd75ad5STejun Heo 	{ "WDC WD800JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4123ecd75ad5STejun Heo 	{ "WDC WD1200JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4124ecd75ad5STejun Heo 	{ "WDC WD1600JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4125ecd75ad5STejun Heo 	{ "WDC WD2000JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4126ecd75ad5STejun Heo 	{ "WDC WD2500JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4127ecd75ad5STejun Heo 	{ "WDC WD3000JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4128ecd75ad5STejun Heo 	{ "WDC WD3200JD-*",		NULL,	ATA_HORKAGE_WD_BROKEN_LPM },
4129ecd75ad5STejun Heo 
4130ac9f0c81SAnton Lundin 	/*
4131ac9f0c81SAnton Lundin 	 * This sata dom device goes on a walkabout when the ATA_LOG_DIRECTORY
4132ac9f0c81SAnton Lundin 	 * log page is accessed. Ensure we never ask for this log page with
4133ac9f0c81SAnton Lundin 	 * these devices.
4134ac9f0c81SAnton Lundin 	 */
4135ac9f0c81SAnton Lundin 	{ "SATADOM-ML 3ME",		NULL,	ATA_HORKAGE_NO_LOG_DIR },
4136ac9f0c81SAnton Lundin 
41374d2e4980SDamien Le Moal 	/* Buggy FUA */
41384d2e4980SDamien Le Moal 	{ "Maxtor",		"BANC1G10",	ATA_HORKAGE_NO_FUA },
413938d43122SDamien Le Moal 	{ "WDC*WD2500J*",	NULL,		ATA_HORKAGE_NO_FUA },
414038d43122SDamien Le Moal 	{ "OCZ-VERTEX*",	NULL,		ATA_HORKAGE_NO_FUA },
414138d43122SDamien Le Moal 	{ "INTEL*SSDSC2CT*",	NULL,		ATA_HORKAGE_NO_FUA },
41424d2e4980SDamien Le Moal 
41436919a0a6SAlan Cox 	/* End Marker */
41446919a0a6SAlan Cox 	{ }
4145c6fd2807SJeff Garzik };
4146c6fd2807SJeff Garzik 
414775683fe7STejun Heo static unsigned long ata_dev_blacklisted(const struct ata_device *dev)
4148c6fd2807SJeff Garzik {
41498bfa79fcSTejun Heo 	unsigned char model_num[ATA_ID_PROD_LEN + 1];
41508bfa79fcSTejun Heo 	unsigned char model_rev[ATA_ID_FW_REV_LEN + 1];
41516919a0a6SAlan Cox 	const struct ata_blacklist_entry *ad = ata_device_blacklist;
4152c6fd2807SJeff Garzik 
41538bfa79fcSTejun Heo 	ata_id_c_string(dev->id, model_num, ATA_ID_PROD, sizeof(model_num));
41548bfa79fcSTejun Heo 	ata_id_c_string(dev->id, model_rev, ATA_ID_FW_REV, sizeof(model_rev));
4155c6fd2807SJeff Garzik 
41566919a0a6SAlan Cox 	while (ad->model_num) {
41571c402799SGeorge Spelvin 		if (glob_match(ad->model_num, model_num)) {
41586919a0a6SAlan Cox 			if (ad->model_rev == NULL)
41596919a0a6SAlan Cox 				return ad->horkage;
41601c402799SGeorge Spelvin 			if (glob_match(ad->model_rev, model_rev))
41616919a0a6SAlan Cox 				return ad->horkage;
4162c6fd2807SJeff Garzik 		}
41636919a0a6SAlan Cox 		ad++;
4164c6fd2807SJeff Garzik 	}
4165c6fd2807SJeff Garzik 	return 0;
4166c6fd2807SJeff Garzik }
4167c6fd2807SJeff Garzik 
41686919a0a6SAlan Cox static int ata_dma_blacklisted(const struct ata_device *dev)
41696919a0a6SAlan Cox {
41706919a0a6SAlan Cox 	/* We don't support polling DMA.
41716919a0a6SAlan Cox 	 * DMA blacklist those ATAPI devices with CDB-intr (and use PIO)
41726919a0a6SAlan Cox 	 * if the LLDD handles only interrupts in the HSM_ST_LAST state.
41736919a0a6SAlan Cox 	 */
41749af5c9c9STejun Heo 	if ((dev->link->ap->flags & ATA_FLAG_PIO_POLLING) &&
41756919a0a6SAlan Cox 	    (dev->flags & ATA_DFLAG_CDB_INTR))
41766919a0a6SAlan Cox 		return 1;
417775683fe7STejun Heo 	return (dev->horkage & ATA_HORKAGE_NODMA) ? 1 : 0;
41786919a0a6SAlan Cox }
41796919a0a6SAlan Cox 
4180c6fd2807SJeff Garzik /**
41816bbfd53dSAlan Cox  *	ata_is_40wire		-	check drive side detection
41826bbfd53dSAlan Cox  *	@dev: device
41836bbfd53dSAlan Cox  *
41846bbfd53dSAlan Cox  *	Perform drive side detection decoding, allowing for device vendors
41856bbfd53dSAlan Cox  *	who can't follow the documentation.
41866bbfd53dSAlan Cox  */
41876bbfd53dSAlan Cox 
41886bbfd53dSAlan Cox static int ata_is_40wire(struct ata_device *dev)
41896bbfd53dSAlan Cox {
41906bbfd53dSAlan Cox 	if (dev->horkage & ATA_HORKAGE_IVB)
41916bbfd53dSAlan Cox 		return ata_drive_40wire_relaxed(dev->id);
41926bbfd53dSAlan Cox 	return ata_drive_40wire(dev->id);
41936bbfd53dSAlan Cox }
41946bbfd53dSAlan Cox 
41956bbfd53dSAlan Cox /**
419615a5551cSAlan Cox  *	cable_is_40wire		-	40/80/SATA decider
419715a5551cSAlan Cox  *	@ap: port to consider
419815a5551cSAlan Cox  *
419915a5551cSAlan Cox  *	This function encapsulates the policy for speed management
420015a5551cSAlan Cox  *	in one place. At the moment we don't cache the result but
420115a5551cSAlan Cox  *	there is a good case for setting ap->cbl to the result when
420215a5551cSAlan Cox  *	we are called with unknown cables (and figuring out if it
420315a5551cSAlan Cox  *	impacts hotplug at all).
420415a5551cSAlan Cox  *
420515a5551cSAlan Cox  *	Return 1 if the cable appears to be 40 wire.
420615a5551cSAlan Cox  */
420715a5551cSAlan Cox 
420815a5551cSAlan Cox static int cable_is_40wire(struct ata_port *ap)
420915a5551cSAlan Cox {
421015a5551cSAlan Cox 	struct ata_link *link;
421115a5551cSAlan Cox 	struct ata_device *dev;
421215a5551cSAlan Cox 
42134a9c7b33STejun Heo 	/* If the controller thinks we are 40 wire, we are. */
421415a5551cSAlan Cox 	if (ap->cbl == ATA_CBL_PATA40)
421515a5551cSAlan Cox 		return 1;
42164a9c7b33STejun Heo 
42174a9c7b33STejun Heo 	/* If the controller thinks we are 80 wire, we are. */
421815a5551cSAlan Cox 	if (ap->cbl == ATA_CBL_PATA80 || ap->cbl == ATA_CBL_SATA)
421915a5551cSAlan Cox 		return 0;
42204a9c7b33STejun Heo 
42214a9c7b33STejun Heo 	/* If the system is known to be 40 wire short cable (eg
42224a9c7b33STejun Heo 	 * laptop), then we allow 80 wire modes even if the drive
42234a9c7b33STejun Heo 	 * isn't sure.
42244a9c7b33STejun Heo 	 */
4225f792068eSAlan Cox 	if (ap->cbl == ATA_CBL_PATA40_SHORT)
4226f792068eSAlan Cox 		return 0;
422715a5551cSAlan Cox 
42284a9c7b33STejun Heo 	/* If the controller doesn't know, we scan.
42294a9c7b33STejun Heo 	 *
42304a9c7b33STejun Heo 	 * Note: We look for all 40 wire detects at this point.  Any
42314a9c7b33STejun Heo 	 *       80 wire detect is taken to be 80 wire cable because
42324a9c7b33STejun Heo 	 * - in many setups only the one drive (slave if present) will
42334a9c7b33STejun Heo 	 *   give a valid detect
42344a9c7b33STejun Heo 	 * - if you have a non detect capable drive you don't want it
42354a9c7b33STejun Heo 	 *   to colour the choice
423615a5551cSAlan Cox 	 */
42371eca4365STejun Heo 	ata_for_each_link(link, ap, EDGE) {
42381eca4365STejun Heo 		ata_for_each_dev(dev, link, ENABLED) {
42391eca4365STejun Heo 			if (!ata_is_40wire(dev))
424015a5551cSAlan Cox 				return 0;
424115a5551cSAlan Cox 		}
424215a5551cSAlan Cox 	}
424315a5551cSAlan Cox 	return 1;
424415a5551cSAlan Cox }
424515a5551cSAlan Cox 
424615a5551cSAlan Cox /**
4247c6fd2807SJeff Garzik  *	ata_dev_xfermask - Compute supported xfermask of the given device
4248c6fd2807SJeff Garzik  *	@dev: Device to compute xfermask for
4249c6fd2807SJeff Garzik  *
4250c6fd2807SJeff Garzik  *	Compute supported xfermask of @dev and store it in
4251c6fd2807SJeff Garzik  *	dev->*_mask.  This function is responsible for applying all
4252c6fd2807SJeff Garzik  *	known limits including host controller limits, device
4253c6fd2807SJeff Garzik  *	blacklist, etc...
4254c6fd2807SJeff Garzik  *
4255c6fd2807SJeff Garzik  *	LOCKING:
4256c6fd2807SJeff Garzik  *	None.
4257c6fd2807SJeff Garzik  */
4258c6fd2807SJeff Garzik static void ata_dev_xfermask(struct ata_device *dev)
4259c6fd2807SJeff Garzik {
42609af5c9c9STejun Heo 	struct ata_link *link = dev->link;
42619af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
4262cca3974eSJeff Garzik 	struct ata_host *host = ap->host;
4263f0a6d77bSSergey Shtylyov 	unsigned int xfer_mask;
4264c6fd2807SJeff Garzik 
4265c6fd2807SJeff Garzik 	/* controller modes available */
4266c6fd2807SJeff Garzik 	xfer_mask = ata_pack_xfermask(ap->pio_mask,
4267c6fd2807SJeff Garzik 				      ap->mwdma_mask, ap->udma_mask);
4268c6fd2807SJeff Garzik 
42698343f889SRobert Hancock 	/* drive modes available */
4270c6fd2807SJeff Garzik 	xfer_mask &= ata_pack_xfermask(dev->pio_mask,
4271c6fd2807SJeff Garzik 				       dev->mwdma_mask, dev->udma_mask);
4272c6fd2807SJeff Garzik 	xfer_mask &= ata_id_xfermask(dev->id);
4273c6fd2807SJeff Garzik 
4274b352e57dSAlan Cox 	/*
4275b352e57dSAlan Cox 	 *	CFA Advanced TrueIDE timings are not allowed on a shared
4276b352e57dSAlan Cox 	 *	cable
4277b352e57dSAlan Cox 	 */
4278b352e57dSAlan Cox 	if (ata_dev_pair(dev)) {
4279b352e57dSAlan Cox 		/* No PIO5 or PIO6 */
4280b352e57dSAlan Cox 		xfer_mask &= ~(0x03 << (ATA_SHIFT_PIO + 5));
4281b352e57dSAlan Cox 		/* No MWDMA3 or MWDMA 4 */
4282b352e57dSAlan Cox 		xfer_mask &= ~(0x03 << (ATA_SHIFT_MWDMA + 3));
4283b352e57dSAlan Cox 	}
4284b352e57dSAlan Cox 
4285c6fd2807SJeff Garzik 	if (ata_dma_blacklisted(dev)) {
4286c6fd2807SJeff Garzik 		xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4287a9a79dfeSJoe Perches 		ata_dev_warn(dev,
4288c6fd2807SJeff Garzik 			     "device is on DMA blacklist, disabling DMA\n");
4289c6fd2807SJeff Garzik 	}
4290c6fd2807SJeff Garzik 
429114d66ab7SPetr Vandrovec 	if ((host->flags & ATA_HOST_SIMPLEX) &&
429214d66ab7SPetr Vandrovec 	    host->simplex_claimed && host->simplex_claimed != ap) {
4293c6fd2807SJeff Garzik 		xfer_mask &= ~(ATA_MASK_MWDMA | ATA_MASK_UDMA);
4294a9a79dfeSJoe Perches 		ata_dev_warn(dev,
4295a9a79dfeSJoe Perches 			     "simplex DMA is claimed by other device, disabling DMA\n");
4296c6fd2807SJeff Garzik 	}
4297c6fd2807SJeff Garzik 
4298e424675fSJeff Garzik 	if (ap->flags & ATA_FLAG_NO_IORDY)
4299e424675fSJeff Garzik 		xfer_mask &= ata_pio_mask_no_iordy(dev);
4300e424675fSJeff Garzik 
4301c6fd2807SJeff Garzik 	if (ap->ops->mode_filter)
4302a76b62caSAlan Cox 		xfer_mask = ap->ops->mode_filter(dev, xfer_mask);
4303c6fd2807SJeff Garzik 
43048343f889SRobert Hancock 	/* Apply cable rule here.  Don't apply it early because when
43058343f889SRobert Hancock 	 * we handle hot plug the cable type can itself change.
43068343f889SRobert Hancock 	 * Check this last so that we know if the transfer rate was
43078343f889SRobert Hancock 	 * solely limited by the cable.
43088343f889SRobert Hancock 	 * Unknown or 80 wire cables reported host side are checked
43098343f889SRobert Hancock 	 * drive side as well. Cases where we know a 40wire cable
43108343f889SRobert Hancock 	 * is used safely for 80 are not checked here.
43118343f889SRobert Hancock 	 */
43128343f889SRobert Hancock 	if (xfer_mask & (0xF8 << ATA_SHIFT_UDMA))
43138343f889SRobert Hancock 		/* UDMA/44 or higher would be available */
431415a5551cSAlan Cox 		if (cable_is_40wire(ap)) {
4315a9a79dfeSJoe Perches 			ata_dev_warn(dev,
43168343f889SRobert Hancock 				     "limited to UDMA/33 due to 40-wire cable\n");
43178343f889SRobert Hancock 			xfer_mask &= ~(0xF8 << ATA_SHIFT_UDMA);
43188343f889SRobert Hancock 		}
43198343f889SRobert Hancock 
4320c6fd2807SJeff Garzik 	ata_unpack_xfermask(xfer_mask, &dev->pio_mask,
4321c6fd2807SJeff Garzik 			    &dev->mwdma_mask, &dev->udma_mask);
4322c6fd2807SJeff Garzik }
4323c6fd2807SJeff Garzik 
4324c6fd2807SJeff Garzik /**
4325c6fd2807SJeff Garzik  *	ata_dev_set_xfermode - Issue SET FEATURES - XFER MODE command
4326c6fd2807SJeff Garzik  *	@dev: Device to which command will be sent
4327c6fd2807SJeff Garzik  *
4328c6fd2807SJeff Garzik  *	Issue SET FEATURES - XFER MODE command to device @dev
4329c6fd2807SJeff Garzik  *	on port @ap.
4330c6fd2807SJeff Garzik  *
4331c6fd2807SJeff Garzik  *	LOCKING:
4332c6fd2807SJeff Garzik  *	PCI/etc. bus probe sem.
4333c6fd2807SJeff Garzik  *
4334c6fd2807SJeff Garzik  *	RETURNS:
4335c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
4336c6fd2807SJeff Garzik  */
4337c6fd2807SJeff Garzik 
4338c6fd2807SJeff Garzik static unsigned int ata_dev_set_xfermode(struct ata_device *dev)
4339c6fd2807SJeff Garzik {
4340c6fd2807SJeff Garzik 	struct ata_taskfile tf;
4341c6fd2807SJeff Garzik 
4342c6fd2807SJeff Garzik 	/* set up set-features taskfile */
43434633778bSHannes Reinecke 	ata_dev_dbg(dev, "set features - xfer mode\n");
4344c6fd2807SJeff Garzik 
4345464cf177STejun Heo 	/* Some controllers and ATAPI devices show flaky interrupt
4346464cf177STejun Heo 	 * behavior after setting xfer mode.  Use polling instead.
4347464cf177STejun Heo 	 */
4348c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
4349c6fd2807SJeff Garzik 	tf.command = ATA_CMD_SET_FEATURES;
4350c6fd2807SJeff Garzik 	tf.feature = SETFEATURES_XFER;
4351464cf177STejun Heo 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE | ATA_TFLAG_POLLING;
4352c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
4353b9f8ab2dSAlan Cox 	/* If we are using IORDY we must send the mode setting command */
4354b9f8ab2dSAlan Cox 	if (ata_pio_need_iordy(dev))
4355c6fd2807SJeff Garzik 		tf.nsect = dev->xfer_mode;
4356b9f8ab2dSAlan Cox 	/* If the device has IORDY and the controller does not - turn it off */
4357b9f8ab2dSAlan Cox  	else if (ata_id_has_iordy(dev->id))
4358b9f8ab2dSAlan Cox 		tf.nsect = 0x01;
4359b9f8ab2dSAlan Cox 	else /* In the ancient relic department - skip all of this */
4360b9f8ab2dSAlan Cox 		return 0;
4361c6fd2807SJeff Garzik 
4362024811a2SDamien Le Moal 	/*
4363024811a2SDamien Le Moal 	 * On some disks, this command causes spin-up, so we need longer
4364024811a2SDamien Le Moal 	 * timeout.
4365024811a2SDamien Le Moal 	 */
4366024811a2SDamien Le Moal 	return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 15000);
4367c6fd2807SJeff Garzik }
43681152b261STejun Heo 
4369c6fd2807SJeff Garzik /**
437099ad3f9fSNiklas Cassel  *	ata_dev_set_feature - Issue SET FEATURES
43719f45cbd3SKristen Carlson Accardi  *	@dev: Device to which command will be sent
437299ad3f9fSNiklas Cassel  *	@subcmd: The SET FEATURES subcommand to be sent
437399ad3f9fSNiklas Cassel  *	@action: The sector count represents a subcommand specific action
43749f45cbd3SKristen Carlson Accardi  *
437599ad3f9fSNiklas Cassel  *	Issue SET FEATURES command to device @dev on port @ap with sector count
43769f45cbd3SKristen Carlson Accardi  *
43779f45cbd3SKristen Carlson Accardi  *	LOCKING:
43789f45cbd3SKristen Carlson Accardi  *	PCI/etc. bus probe sem.
43799f45cbd3SKristen Carlson Accardi  *
43809f45cbd3SKristen Carlson Accardi  *	RETURNS:
43819f45cbd3SKristen Carlson Accardi  *	0 on success, AC_ERR_* mask otherwise.
43829f45cbd3SKristen Carlson Accardi  */
438399ad3f9fSNiklas Cassel unsigned int ata_dev_set_feature(struct ata_device *dev, u8 subcmd, u8 action)
43849f45cbd3SKristen Carlson Accardi {
43859f45cbd3SKristen Carlson Accardi 	struct ata_taskfile tf;
438661176eedSSergey Shtylyov 	unsigned int timeout = 0;
43879f45cbd3SKristen Carlson Accardi 
43889f45cbd3SKristen Carlson Accardi 	/* set up set-features taskfile */
438999ad3f9fSNiklas Cassel 	ata_dev_dbg(dev, "set features\n");
43909f45cbd3SKristen Carlson Accardi 
43919f45cbd3SKristen Carlson Accardi 	ata_tf_init(dev, &tf);
43929f45cbd3SKristen Carlson Accardi 	tf.command = ATA_CMD_SET_FEATURES;
439399ad3f9fSNiklas Cassel 	tf.feature = subcmd;
43949f45cbd3SKristen Carlson Accardi 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
43959f45cbd3SKristen Carlson Accardi 	tf.protocol = ATA_PROT_NODATA;
439699ad3f9fSNiklas Cassel 	tf.nsect = action;
43979f45cbd3SKristen Carlson Accardi 
439899ad3f9fSNiklas Cassel 	if (subcmd == SETFEATURES_SPINUP)
4399974e0a45SDamien Le Moal 		timeout = ata_probe_timeout ?
4400974e0a45SDamien Le Moal 			  ata_probe_timeout * 1000 : SETFEATURES_SPINUP_TIMEOUT;
44019f45cbd3SKristen Carlson Accardi 
4402614065abSJinpeng Cui 	return ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, timeout);
44039f45cbd3SKristen Carlson Accardi }
4404633de4ccSJeff Garzik EXPORT_SYMBOL_GPL(ata_dev_set_feature);
44059f45cbd3SKristen Carlson Accardi 
44069f45cbd3SKristen Carlson Accardi /**
4407c6fd2807SJeff Garzik  *	ata_dev_init_params - Issue INIT DEV PARAMS command
4408c6fd2807SJeff Garzik  *	@dev: Device to which command will be sent
4409c6fd2807SJeff Garzik  *	@heads: Number of heads (taskfile parameter)
4410c6fd2807SJeff Garzik  *	@sectors: Number of sectors (taskfile parameter)
4411c6fd2807SJeff Garzik  *
4412c6fd2807SJeff Garzik  *	LOCKING:
4413c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
4414c6fd2807SJeff Garzik  *
4415c6fd2807SJeff Garzik  *	RETURNS:
4416c6fd2807SJeff Garzik  *	0 on success, AC_ERR_* mask otherwise.
4417c6fd2807SJeff Garzik  */
4418c6fd2807SJeff Garzik static unsigned int ata_dev_init_params(struct ata_device *dev,
4419c6fd2807SJeff Garzik 					u16 heads, u16 sectors)
4420c6fd2807SJeff Garzik {
4421c6fd2807SJeff Garzik 	struct ata_taskfile tf;
4422c6fd2807SJeff Garzik 	unsigned int err_mask;
4423c6fd2807SJeff Garzik 
4424c6fd2807SJeff Garzik 	/* Number of sectors per track 1-255. Number of heads 1-16 */
4425c6fd2807SJeff Garzik 	if (sectors < 1 || sectors > 255 || heads < 1 || heads > 16)
4426c6fd2807SJeff Garzik 		return AC_ERR_INVALID;
4427c6fd2807SJeff Garzik 
4428c6fd2807SJeff Garzik 	/* set up init dev params taskfile */
44294633778bSHannes Reinecke 	ata_dev_dbg(dev, "init dev params \n");
4430c6fd2807SJeff Garzik 
4431c6fd2807SJeff Garzik 	ata_tf_init(dev, &tf);
4432c6fd2807SJeff Garzik 	tf.command = ATA_CMD_INIT_DEV_PARAMS;
4433c6fd2807SJeff Garzik 	tf.flags |= ATA_TFLAG_ISADDR | ATA_TFLAG_DEVICE;
4434c6fd2807SJeff Garzik 	tf.protocol = ATA_PROT_NODATA;
4435c6fd2807SJeff Garzik 	tf.nsect = sectors;
4436c6fd2807SJeff Garzik 	tf.device |= (heads - 1) & 0x0f; /* max head = num. of heads - 1 */
4437c6fd2807SJeff Garzik 
44382b789108STejun Heo 	err_mask = ata_exec_internal(dev, &tf, NULL, DMA_NONE, NULL, 0, 0);
443918b2466cSAlan Cox 	/* A clean abort indicates an original or just out of spec drive
444018b2466cSAlan Cox 	   and we should continue as we issue the setup based on the
444118b2466cSAlan Cox 	   drive reported working geometry */
4442efcef265SSergey Shtylyov 	if (err_mask == AC_ERR_DEV && (tf.error & ATA_ABORTED))
444318b2466cSAlan Cox 		err_mask = 0;
4444c6fd2807SJeff Garzik 
4445c6fd2807SJeff Garzik 	return err_mask;
4446c6fd2807SJeff Garzik }
4447c6fd2807SJeff Garzik 
4448c6fd2807SJeff Garzik /**
44495895ef9aSTejun Heo  *	atapi_check_dma - Check whether ATAPI DMA can be supported
4450c6fd2807SJeff Garzik  *	@qc: Metadata associated with taskfile to check
4451c6fd2807SJeff Garzik  *
4452c6fd2807SJeff Garzik  *	Allow low-level driver to filter ATA PACKET commands, returning
4453c6fd2807SJeff Garzik  *	a status indicating whether or not it is OK to use DMA for the
4454c6fd2807SJeff Garzik  *	supplied PACKET command.
4455c6fd2807SJeff Garzik  *
4456c6fd2807SJeff Garzik  *	LOCKING:
4457cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4458c6fd2807SJeff Garzik  *
4459c6fd2807SJeff Garzik  *	RETURNS: 0 when ATAPI DMA can be used
4460c6fd2807SJeff Garzik  *               nonzero otherwise
4461c6fd2807SJeff Garzik  */
44625895ef9aSTejun Heo int atapi_check_dma(struct ata_queued_cmd *qc)
4463c6fd2807SJeff Garzik {
4464c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4465c6fd2807SJeff Garzik 
4466b9a4197eSTejun Heo 	/* Don't allow DMA if it isn't multiple of 16 bytes.  Quite a
4467b9a4197eSTejun Heo 	 * few ATAPI devices choke on such DMA requests.
4468b9a4197eSTejun Heo 	 */
44696a87e42eSTejun Heo 	if (!(qc->dev->horkage & ATA_HORKAGE_ATAPI_MOD16_DMA) &&
44706a87e42eSTejun Heo 	    unlikely(qc->nbytes & 15))
44716f23a31dSAlbert Lee 		return 1;
44726f23a31dSAlbert Lee 
4473c6fd2807SJeff Garzik 	if (ap->ops->check_atapi_dma)
4474b9a4197eSTejun Heo 		return ap->ops->check_atapi_dma(qc);
4475c6fd2807SJeff Garzik 
4476b9a4197eSTejun Heo 	return 0;
4477c6fd2807SJeff Garzik }
4478b9a4197eSTejun Heo 
4479c6fd2807SJeff Garzik /**
448031cc23b3STejun Heo  *	ata_std_qc_defer - Check whether a qc needs to be deferred
448131cc23b3STejun Heo  *	@qc: ATA command in question
448231cc23b3STejun Heo  *
448331cc23b3STejun Heo  *	Non-NCQ commands cannot run with any other command, NCQ or
448431cc23b3STejun Heo  *	not.  As upper layer only knows the queue depth, we are
448531cc23b3STejun Heo  *	responsible for maintaining exclusion.  This function checks
448631cc23b3STejun Heo  *	whether a new command @qc can be issued.
448731cc23b3STejun Heo  *
448831cc23b3STejun Heo  *	LOCKING:
448931cc23b3STejun Heo  *	spin_lock_irqsave(host lock)
449031cc23b3STejun Heo  *
449131cc23b3STejun Heo  *	RETURNS:
449231cc23b3STejun Heo  *	ATA_DEFER_* if deferring is needed, 0 otherwise.
449331cc23b3STejun Heo  */
449431cc23b3STejun Heo int ata_std_qc_defer(struct ata_queued_cmd *qc)
449531cc23b3STejun Heo {
449631cc23b3STejun Heo 	struct ata_link *link = qc->dev->link;
449731cc23b3STejun Heo 
4498179b310aSHannes Reinecke 	if (ata_is_ncq(qc->tf.protocol)) {
449931cc23b3STejun Heo 		if (!ata_tag_valid(link->active_tag))
450031cc23b3STejun Heo 			return 0;
450131cc23b3STejun Heo 	} else {
450231cc23b3STejun Heo 		if (!ata_tag_valid(link->active_tag) && !link->sactive)
450331cc23b3STejun Heo 			return 0;
450431cc23b3STejun Heo 	}
450531cc23b3STejun Heo 
450631cc23b3STejun Heo 	return ATA_DEFER_LINK;
450731cc23b3STejun Heo }
4508a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_std_qc_defer);
450931cc23b3STejun Heo 
451095364f36SJiri Slaby enum ata_completion_errors ata_noop_qc_prep(struct ata_queued_cmd *qc)
451195364f36SJiri Slaby {
451295364f36SJiri Slaby 	return AC_ERR_OK;
451395364f36SJiri Slaby }
4514a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_noop_qc_prep);
4515c6fd2807SJeff Garzik 
4516c6fd2807SJeff Garzik /**
4517c6fd2807SJeff Garzik  *	ata_sg_init - Associate command with scatter-gather table.
4518c6fd2807SJeff Garzik  *	@qc: Command to be associated
4519c6fd2807SJeff Garzik  *	@sg: Scatter-gather table.
4520c6fd2807SJeff Garzik  *	@n_elem: Number of elements in s/g table.
4521c6fd2807SJeff Garzik  *
4522c6fd2807SJeff Garzik  *	Initialize the data-related elements of queued_cmd @qc
4523c6fd2807SJeff Garzik  *	to point to a scatter-gather table @sg, containing @n_elem
4524c6fd2807SJeff Garzik  *	elements.
4525c6fd2807SJeff Garzik  *
4526c6fd2807SJeff Garzik  *	LOCKING:
4527cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4528c6fd2807SJeff Garzik  */
4529c6fd2807SJeff Garzik void ata_sg_init(struct ata_queued_cmd *qc, struct scatterlist *sg,
4530c6fd2807SJeff Garzik 		 unsigned int n_elem)
4531c6fd2807SJeff Garzik {
4532ff2aeb1eSTejun Heo 	qc->sg = sg;
4533c6fd2807SJeff Garzik 	qc->n_elem = n_elem;
4534ff2aeb1eSTejun Heo 	qc->cursg = qc->sg;
4535ff2aeb1eSTejun Heo }
4536ff2aeb1eSTejun Heo 
45372874d5eeSGeert Uytterhoeven #ifdef CONFIG_HAS_DMA
45382874d5eeSGeert Uytterhoeven 
45392874d5eeSGeert Uytterhoeven /**
45402874d5eeSGeert Uytterhoeven  *	ata_sg_clean - Unmap DMA memory associated with command
45412874d5eeSGeert Uytterhoeven  *	@qc: Command containing DMA memory to be released
45422874d5eeSGeert Uytterhoeven  *
45432874d5eeSGeert Uytterhoeven  *	Unmap all mapped DMA memory associated with this command.
45442874d5eeSGeert Uytterhoeven  *
45452874d5eeSGeert Uytterhoeven  *	LOCKING:
45462874d5eeSGeert Uytterhoeven  *	spin_lock_irqsave(host lock)
45472874d5eeSGeert Uytterhoeven  */
4548af27e01cSJason Yan static void ata_sg_clean(struct ata_queued_cmd *qc)
45492874d5eeSGeert Uytterhoeven {
45502874d5eeSGeert Uytterhoeven 	struct ata_port *ap = qc->ap;
45512874d5eeSGeert Uytterhoeven 	struct scatterlist *sg = qc->sg;
45522874d5eeSGeert Uytterhoeven 	int dir = qc->dma_dir;
45532874d5eeSGeert Uytterhoeven 
45542874d5eeSGeert Uytterhoeven 	WARN_ON_ONCE(sg == NULL);
45552874d5eeSGeert Uytterhoeven 
45562874d5eeSGeert Uytterhoeven 	if (qc->n_elem)
45572874d5eeSGeert Uytterhoeven 		dma_unmap_sg(ap->dev, sg, qc->orig_n_elem, dir);
45582874d5eeSGeert Uytterhoeven 
45592874d5eeSGeert Uytterhoeven 	qc->flags &= ~ATA_QCFLAG_DMAMAP;
45602874d5eeSGeert Uytterhoeven 	qc->sg = NULL;
45612874d5eeSGeert Uytterhoeven }
45622874d5eeSGeert Uytterhoeven 
4563c6fd2807SJeff Garzik /**
4564c6fd2807SJeff Garzik  *	ata_sg_setup - DMA-map the scatter-gather table associated with a command.
4565c6fd2807SJeff Garzik  *	@qc: Command with scatter-gather table to be mapped.
4566c6fd2807SJeff Garzik  *
4567c6fd2807SJeff Garzik  *	DMA-map the scatter-gather table associated with queued_cmd @qc.
4568c6fd2807SJeff Garzik  *
4569c6fd2807SJeff Garzik  *	LOCKING:
4570cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4571c6fd2807SJeff Garzik  *
4572c6fd2807SJeff Garzik  *	RETURNS:
4573c6fd2807SJeff Garzik  *	Zero on success, negative on error.
4574c6fd2807SJeff Garzik  *
4575c6fd2807SJeff Garzik  */
4576c6fd2807SJeff Garzik static int ata_sg_setup(struct ata_queued_cmd *qc)
4577c6fd2807SJeff Garzik {
4578c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4579dde20207SJames Bottomley 	unsigned int n_elem;
4580c6fd2807SJeff Garzik 
4581dde20207SJames Bottomley 	n_elem = dma_map_sg(ap->dev, qc->sg, qc->n_elem, qc->dma_dir);
4582dde20207SJames Bottomley 	if (n_elem < 1)
4583c6fd2807SJeff Garzik 		return -1;
4584c6fd2807SJeff Garzik 
45855825627cSFUJITA Tomonori 	qc->orig_n_elem = qc->n_elem;
4586dde20207SJames Bottomley 	qc->n_elem = n_elem;
4587f92a2636STejun Heo 	qc->flags |= ATA_QCFLAG_DMAMAP;
4588c6fd2807SJeff Garzik 
4589c6fd2807SJeff Garzik 	return 0;
4590c6fd2807SJeff Garzik }
4591c6fd2807SJeff Garzik 
45922874d5eeSGeert Uytterhoeven #else /* !CONFIG_HAS_DMA */
45932874d5eeSGeert Uytterhoeven 
45942874d5eeSGeert Uytterhoeven static inline void ata_sg_clean(struct ata_queued_cmd *qc) {}
45952874d5eeSGeert Uytterhoeven static inline int ata_sg_setup(struct ata_queued_cmd *qc) { return -1; }
45962874d5eeSGeert Uytterhoeven 
45972874d5eeSGeert Uytterhoeven #endif /* !CONFIG_HAS_DMA */
45982874d5eeSGeert Uytterhoeven 
4599c6fd2807SJeff Garzik /**
4600c6fd2807SJeff Garzik  *	swap_buf_le16 - swap halves of 16-bit words in place
4601c6fd2807SJeff Garzik  *	@buf:  Buffer to swap
4602c6fd2807SJeff Garzik  *	@buf_words:  Number of 16-bit words in buffer.
4603c6fd2807SJeff Garzik  *
4604c6fd2807SJeff Garzik  *	Swap halves of 16-bit words if needed to convert from
4605c6fd2807SJeff Garzik  *	little-endian byte order to native cpu byte order, or
4606c6fd2807SJeff Garzik  *	vice-versa.
4607c6fd2807SJeff Garzik  *
4608c6fd2807SJeff Garzik  *	LOCKING:
4609c6fd2807SJeff Garzik  *	Inherited from caller.
4610c6fd2807SJeff Garzik  */
4611c6fd2807SJeff Garzik void swap_buf_le16(u16 *buf, unsigned int buf_words)
4612c6fd2807SJeff Garzik {
4613c6fd2807SJeff Garzik #ifdef __BIG_ENDIAN
4614c6fd2807SJeff Garzik 	unsigned int i;
4615c6fd2807SJeff Garzik 
4616c6fd2807SJeff Garzik 	for (i = 0; i < buf_words; i++)
4617c6fd2807SJeff Garzik 		buf[i] = le16_to_cpu(buf[i]);
4618c6fd2807SJeff Garzik #endif /* __BIG_ENDIAN */
4619c6fd2807SJeff Garzik }
4620c6fd2807SJeff Garzik 
4621c6fd2807SJeff Garzik /**
46228a8bc223STejun Heo  *	ata_qc_free - free unused ata_queued_cmd
46238a8bc223STejun Heo  *	@qc: Command to complete
46248a8bc223STejun Heo  *
46258a8bc223STejun Heo  *	Designed to free unused ata_queued_cmd object
46268a8bc223STejun Heo  *	in case something prevents using it.
46278a8bc223STejun Heo  *
46288a8bc223STejun Heo  *	LOCKING:
46298a8bc223STejun Heo  *	spin_lock_irqsave(host lock)
46308a8bc223STejun Heo  */
46318a8bc223STejun Heo void ata_qc_free(struct ata_queued_cmd *qc)
46328a8bc223STejun Heo {
46338a8bc223STejun Heo 	qc->flags = 0;
46344f1a22eeSJohn Garry 	if (ata_tag_valid(qc->tag))
46358a8bc223STejun Heo 		qc->tag = ATA_TAG_POISON;
46368a8bc223STejun Heo }
46378a8bc223STejun Heo 
4638c6fd2807SJeff Garzik void __ata_qc_complete(struct ata_queued_cmd *qc)
4639c6fd2807SJeff Garzik {
4640a1104016SJulia Lawall 	struct ata_port *ap;
4641a1104016SJulia Lawall 	struct ata_link *link;
4642c6fd2807SJeff Garzik 
4643efcb3cf7STejun Heo 	WARN_ON_ONCE(qc == NULL); /* ata_qc_from_tag _might_ return NULL */
4644efcb3cf7STejun Heo 	WARN_ON_ONCE(!(qc->flags & ATA_QCFLAG_ACTIVE));
4645a1104016SJulia Lawall 	ap = qc->ap;
4646a1104016SJulia Lawall 	link = qc->dev->link;
4647c6fd2807SJeff Garzik 
4648c6fd2807SJeff Garzik 	if (likely(qc->flags & ATA_QCFLAG_DMAMAP))
4649c6fd2807SJeff Garzik 		ata_sg_clean(qc);
4650c6fd2807SJeff Garzik 
4651c6fd2807SJeff Garzik 	/* command should be marked inactive atomically with qc completion */
4652179b310aSHannes Reinecke 	if (ata_is_ncq(qc->tf.protocol)) {
46534e5b6260SJens Axboe 		link->sactive &= ~(1 << qc->hw_tag);
4654da917d69STejun Heo 		if (!link->sactive)
4655da917d69STejun Heo 			ap->nr_active_links--;
4656da917d69STejun Heo 	} else {
46579af5c9c9STejun Heo 		link->active_tag = ATA_TAG_POISON;
4658da917d69STejun Heo 		ap->nr_active_links--;
4659da917d69STejun Heo 	}
4660da917d69STejun Heo 
4661da917d69STejun Heo 	/* clear exclusive status */
4662da917d69STejun Heo 	if (unlikely(qc->flags & ATA_QCFLAG_CLEAR_EXCL &&
4663da917d69STejun Heo 		     ap->excl_link == link))
4664da917d69STejun Heo 		ap->excl_link = NULL;
4665c6fd2807SJeff Garzik 
4666c6fd2807SJeff Garzik 	/* atapi: mark qc as inactive to prevent the interrupt handler
4667c6fd2807SJeff Garzik 	 * from completing the command twice later, before the error handler
4668c6fd2807SJeff Garzik 	 * is called. (when rc != 0 and atapi request sense is needed)
4669c6fd2807SJeff Garzik 	 */
4670c6fd2807SJeff Garzik 	qc->flags &= ~ATA_QCFLAG_ACTIVE;
4671e3ed8939SJens Axboe 	ap->qc_active &= ~(1ULL << qc->tag);
4672c6fd2807SJeff Garzik 
4673c6fd2807SJeff Garzik 	/* call completion callback */
4674c6fd2807SJeff Garzik 	qc->complete_fn(qc);
4675c6fd2807SJeff Garzik }
4676c6fd2807SJeff Garzik 
467739599a53STejun Heo static void fill_result_tf(struct ata_queued_cmd *qc)
467839599a53STejun Heo {
467939599a53STejun Heo 	struct ata_port *ap = qc->ap;
468039599a53STejun Heo 
468139599a53STejun Heo 	qc->result_tf.flags = qc->tf.flags;
468222183bf5STejun Heo 	ap->ops->qc_fill_rtf(qc);
468339599a53STejun Heo }
468439599a53STejun Heo 
468500115e0fSTejun Heo static void ata_verify_xfer(struct ata_queued_cmd *qc)
468600115e0fSTejun Heo {
468700115e0fSTejun Heo 	struct ata_device *dev = qc->dev;
468800115e0fSTejun Heo 
4689eb0effdfSChristoph Hellwig 	if (!ata_is_data(qc->tf.protocol))
469000115e0fSTejun Heo 		return;
469100115e0fSTejun Heo 
469200115e0fSTejun Heo 	if ((dev->mwdma_mask || dev->udma_mask) && ata_is_pio(qc->tf.protocol))
469300115e0fSTejun Heo 		return;
469400115e0fSTejun Heo 
469500115e0fSTejun Heo 	dev->flags &= ~ATA_DFLAG_DUBIOUS_XFER;
469600115e0fSTejun Heo }
469700115e0fSTejun Heo 
4698c6fd2807SJeff Garzik /**
4699c6fd2807SJeff Garzik  *	ata_qc_complete - Complete an active ATA command
4700c6fd2807SJeff Garzik  *	@qc: Command to complete
4701c6fd2807SJeff Garzik  *
47021aadf5c3STejun Heo  *	Indicate to the mid and upper layers that an ATA command has
47031aadf5c3STejun Heo  *	completed, with either an ok or not-ok status.
47041aadf5c3STejun Heo  *
47051aadf5c3STejun Heo  *	Refrain from calling this function multiple times when
47061aadf5c3STejun Heo  *	successfully completing multiple NCQ commands.
47071aadf5c3STejun Heo  *	ata_qc_complete_multiple() should be used instead, which will
47081aadf5c3STejun Heo  *	properly update IRQ expect state.
4709c6fd2807SJeff Garzik  *
4710c6fd2807SJeff Garzik  *	LOCKING:
4711cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4712c6fd2807SJeff Garzik  */
4713c6fd2807SJeff Garzik void ata_qc_complete(struct ata_queued_cmd *qc)
4714c6fd2807SJeff Garzik {
4715c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
4716c6fd2807SJeff Garzik 
4717eb25cb99SStephan Linz 	/* Trigger the LED (if available) */
4718d1ed7c55SLinus Walleij 	ledtrig_disk_activity(!!(qc->tf.flags & ATA_TFLAG_WRITE));
4719eb25cb99SStephan Linz 
4720c6fd2807SJeff Garzik 	/* XXX: New EH and old EH use different mechanisms to
4721c6fd2807SJeff Garzik 	 * synchronize EH with regular execution path.
4722c6fd2807SJeff Garzik 	 *
472387629312SNiklas Cassel 	 * In new EH, a qc owned by EH is marked with ATA_QCFLAG_EH.
4724c6fd2807SJeff Garzik 	 * Normal execution path is responsible for not accessing a
472587629312SNiklas Cassel 	 * qc owned by EH.  libata core enforces the rule by returning NULL
472687629312SNiklas Cassel 	 * from ata_qc_from_tag() for qcs owned by EH.
4727c6fd2807SJeff Garzik 	 *
4728c6fd2807SJeff Garzik 	 * Old EH depends on ata_qc_complete() nullifying completion
4729c6fd2807SJeff Garzik 	 * requests if ATA_QCFLAG_EH_SCHEDULED is set.  Old EH does
4730c6fd2807SJeff Garzik 	 * not synchronize with interrupt handler.  Only PIO task is
4731c6fd2807SJeff Garzik 	 * taken care of.
4732c6fd2807SJeff Garzik 	 */
4733c6fd2807SJeff Garzik 	if (ap->ops->error_handler) {
47344dbfa39bSTejun Heo 		struct ata_device *dev = qc->dev;
47354dbfa39bSTejun Heo 		struct ata_eh_info *ehi = &dev->link->eh_info;
47364dbfa39bSTejun Heo 
4737c6fd2807SJeff Garzik 		if (unlikely(qc->err_mask))
473887629312SNiklas Cassel 			qc->flags |= ATA_QCFLAG_EH;
4739c6fd2807SJeff Garzik 
4740f08dc1acSTejun Heo 		/*
4741f08dc1acSTejun Heo 		 * Finish internal commands without any further processing
4742f08dc1acSTejun Heo 		 * and always with the result TF filled.
4743f08dc1acSTejun Heo 		 */
4744f08dc1acSTejun Heo 		if (unlikely(ata_tag_internal(qc->tag))) {
474539599a53STejun Heo 			fill_result_tf(qc);
4746255c03d1SHannes Reinecke 			trace_ata_qc_complete_internal(qc);
4747f4b31db9STejun Heo 			__ata_qc_complete(qc);
4748c6fd2807SJeff Garzik 			return;
4749c6fd2807SJeff Garzik 		}
4750c6fd2807SJeff Garzik 
4751f08dc1acSTejun Heo 		/*
4752f08dc1acSTejun Heo 		 * Non-internal qc has failed.  Fill the result TF and
4753f08dc1acSTejun Heo 		 * summon EH.
4754f08dc1acSTejun Heo 		 */
475587629312SNiklas Cassel 		if (unlikely(qc->flags & ATA_QCFLAG_EH)) {
4756f08dc1acSTejun Heo 			fill_result_tf(qc);
4757255c03d1SHannes Reinecke 			trace_ata_qc_complete_failed(qc);
4758f08dc1acSTejun Heo 			ata_qc_schedule_eh(qc);
4759f08dc1acSTejun Heo 			return;
4760f08dc1acSTejun Heo 		}
4761f08dc1acSTejun Heo 
47624cb7c6f1SNiklas Cassel 		WARN_ON_ONCE(ata_port_is_frozen(ap));
47634dc738edSTejun Heo 
4764c6fd2807SJeff Garzik 		/* read result TF if requested */
4765c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_RESULT_TF)
476639599a53STejun Heo 			fill_result_tf(qc);
4767c6fd2807SJeff Garzik 
4768255c03d1SHannes Reinecke 		trace_ata_qc_complete_done(qc);
47694dbfa39bSTejun Heo 		/* Some commands need post-processing after successful
47704dbfa39bSTejun Heo 		 * completion.
47714dbfa39bSTejun Heo 		 */
47724dbfa39bSTejun Heo 		switch (qc->tf.command) {
47734dbfa39bSTejun Heo 		case ATA_CMD_SET_FEATURES:
47744dbfa39bSTejun Heo 			if (qc->tf.feature != SETFEATURES_WC_ON &&
47750c12735eSTom Yan 			    qc->tf.feature != SETFEATURES_WC_OFF &&
47760c12735eSTom Yan 			    qc->tf.feature != SETFEATURES_RA_ON &&
47770c12735eSTom Yan 			    qc->tf.feature != SETFEATURES_RA_OFF)
47784dbfa39bSTejun Heo 				break;
4779df561f66SGustavo A. R. Silva 			fallthrough;
47804dbfa39bSTejun Heo 		case ATA_CMD_INIT_DEV_PARAMS: /* CHS translation changed */
47814dbfa39bSTejun Heo 		case ATA_CMD_SET_MULTI: /* multi_count changed */
47824dbfa39bSTejun Heo 			/* revalidate device */
47834dbfa39bSTejun Heo 			ehi->dev_action[dev->devno] |= ATA_EH_REVALIDATE;
47844dbfa39bSTejun Heo 			ata_port_schedule_eh(ap);
47854dbfa39bSTejun Heo 			break;
4786054a5fbaSTejun Heo 
4787054a5fbaSTejun Heo 		case ATA_CMD_SLEEP:
4788054a5fbaSTejun Heo 			dev->flags |= ATA_DFLAG_SLEEPING;
4789054a5fbaSTejun Heo 			break;
47904dbfa39bSTejun Heo 		}
47914dbfa39bSTejun Heo 
479200115e0fSTejun Heo 		if (unlikely(dev->flags & ATA_DFLAG_DUBIOUS_XFER))
479300115e0fSTejun Heo 			ata_verify_xfer(qc);
479400115e0fSTejun Heo 
4795c6fd2807SJeff Garzik 		__ata_qc_complete(qc);
4796c6fd2807SJeff Garzik 	} else {
4797c6fd2807SJeff Garzik 		if (qc->flags & ATA_QCFLAG_EH_SCHEDULED)
4798c6fd2807SJeff Garzik 			return;
4799c6fd2807SJeff Garzik 
4800c6fd2807SJeff Garzik 		/* read result TF if failed or requested */
4801c6fd2807SJeff Garzik 		if (qc->err_mask || qc->flags & ATA_QCFLAG_RESULT_TF)
480239599a53STejun Heo 			fill_result_tf(qc);
4803c6fd2807SJeff Garzik 
4804c6fd2807SJeff Garzik 		__ata_qc_complete(qc);
4805c6fd2807SJeff Garzik 	}
4806c6fd2807SJeff Garzik }
4807a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_qc_complete);
4808c6fd2807SJeff Garzik 
4809c6fd2807SJeff Garzik /**
48108385d756SSascha Hauer  *	ata_qc_get_active - get bitmask of active qcs
48118385d756SSascha Hauer  *	@ap: port in question
48128385d756SSascha Hauer  *
48138385d756SSascha Hauer  *	LOCKING:
48148385d756SSascha Hauer  *	spin_lock_irqsave(host lock)
48158385d756SSascha Hauer  *
48168385d756SSascha Hauer  *	RETURNS:
48178385d756SSascha Hauer  *	Bitmask of active qcs
48188385d756SSascha Hauer  */
48198385d756SSascha Hauer u64 ata_qc_get_active(struct ata_port *ap)
48208385d756SSascha Hauer {
48218385d756SSascha Hauer 	u64 qc_active = ap->qc_active;
48228385d756SSascha Hauer 
48238385d756SSascha Hauer 	/* ATA_TAG_INTERNAL is sent to hw as tag 0 */
48248385d756SSascha Hauer 	if (qc_active & (1ULL << ATA_TAG_INTERNAL)) {
48258385d756SSascha Hauer 		qc_active |= (1 << 0);
48268385d756SSascha Hauer 		qc_active &= ~(1ULL << ATA_TAG_INTERNAL);
48278385d756SSascha Hauer 	}
48288385d756SSascha Hauer 
48298385d756SSascha Hauer 	return qc_active;
48308385d756SSascha Hauer }
48318385d756SSascha Hauer EXPORT_SYMBOL_GPL(ata_qc_get_active);
48328385d756SSascha Hauer 
48338385d756SSascha Hauer /**
4834c6fd2807SJeff Garzik  *	ata_qc_issue - issue taskfile to device
4835c6fd2807SJeff Garzik  *	@qc: command to issue to device
4836c6fd2807SJeff Garzik  *
4837c6fd2807SJeff Garzik  *	Prepare an ATA command to submission to device.
4838c6fd2807SJeff Garzik  *	This includes mapping the data into a DMA-able
4839c6fd2807SJeff Garzik  *	area, filling in the S/G table, and finally
4840c6fd2807SJeff Garzik  *	writing the taskfile to hardware, starting the command.
4841c6fd2807SJeff Garzik  *
4842c6fd2807SJeff Garzik  *	LOCKING:
4843cca3974eSJeff Garzik  *	spin_lock_irqsave(host lock)
4844c6fd2807SJeff Garzik  */
4845c6fd2807SJeff Garzik void ata_qc_issue(struct ata_queued_cmd *qc)
4846c6fd2807SJeff Garzik {
4847c6fd2807SJeff Garzik 	struct ata_port *ap = qc->ap;
48489af5c9c9STejun Heo 	struct ata_link *link = qc->dev->link;
4849405e66b3STejun Heo 	u8 prot = qc->tf.protocol;
4850c6fd2807SJeff Garzik 
4851c6fd2807SJeff Garzik 	/* Make sure only one non-NCQ command is outstanding.  The
4852c6fd2807SJeff Garzik 	 * check is skipped for old EH because it reuses active qc to
4853c6fd2807SJeff Garzik 	 * request ATAPI sense.
4854c6fd2807SJeff Garzik 	 */
4855efcb3cf7STejun Heo 	WARN_ON_ONCE(ap->ops->error_handler && ata_tag_valid(link->active_tag));
4856c6fd2807SJeff Garzik 
48571973a023STejun Heo 	if (ata_is_ncq(prot)) {
48584e5b6260SJens Axboe 		WARN_ON_ONCE(link->sactive & (1 << qc->hw_tag));
4859da917d69STejun Heo 
4860da917d69STejun Heo 		if (!link->sactive)
4861da917d69STejun Heo 			ap->nr_active_links++;
48624e5b6260SJens Axboe 		link->sactive |= 1 << qc->hw_tag;
4863c6fd2807SJeff Garzik 	} else {
4864efcb3cf7STejun Heo 		WARN_ON_ONCE(link->sactive);
4865da917d69STejun Heo 
4866da917d69STejun Heo 		ap->nr_active_links++;
48679af5c9c9STejun Heo 		link->active_tag = qc->tag;
4868c6fd2807SJeff Garzik 	}
4869c6fd2807SJeff Garzik 
4870c6fd2807SJeff Garzik 	qc->flags |= ATA_QCFLAG_ACTIVE;
4871e3ed8939SJens Axboe 	ap->qc_active |= 1ULL << qc->tag;
4872c6fd2807SJeff Garzik 
487360f5d6efSTejun Heo 	/*
487460f5d6efSTejun Heo 	 * We guarantee to LLDs that they will have at least one
4875f92a2636STejun Heo 	 * non-zero sg if the command is a data command.
4876f92a2636STejun Heo 	 */
48779173e5e8SEric Biggers 	if (ata_is_data(prot) && (!qc->sg || !qc->n_elem || !qc->nbytes))
487860f5d6efSTejun Heo 		goto sys_err;
4879f92a2636STejun Heo 
4880405e66b3STejun Heo 	if (ata_is_dma(prot) || (ata_is_pio(prot) &&
4881f92a2636STejun Heo 				 (ap->flags & ATA_FLAG_PIO_DMA)))
4882c6fd2807SJeff Garzik 		if (ata_sg_setup(qc))
488360f5d6efSTejun Heo 			goto sys_err;
4884c6fd2807SJeff Garzik 
4885cf480626STejun Heo 	/* if device is sleeping, schedule reset and abort the link */
4886054a5fbaSTejun Heo 	if (unlikely(qc->dev->flags & ATA_DFLAG_SLEEPING)) {
4887cf480626STejun Heo 		link->eh_info.action |= ATA_EH_RESET;
4888054a5fbaSTejun Heo 		ata_ehi_push_desc(&link->eh_info, "waking up from sleep");
4889054a5fbaSTejun Heo 		ata_link_abort(link);
4890054a5fbaSTejun Heo 		return;
4891054a5fbaSTejun Heo 	}
4892054a5fbaSTejun Heo 
4893fc914faaSHannes Reinecke 	trace_ata_qc_prep(qc);
489495364f36SJiri Slaby 	qc->err_mask |= ap->ops->qc_prep(qc);
489595364f36SJiri Slaby 	if (unlikely(qc->err_mask))
489695364f36SJiri Slaby 		goto err;
4897255c03d1SHannes Reinecke 	trace_ata_qc_issue(qc);
4898c6fd2807SJeff Garzik 	qc->err_mask |= ap->ops->qc_issue(qc);
4899c6fd2807SJeff Garzik 	if (unlikely(qc->err_mask))
4900c6fd2807SJeff Garzik 		goto err;
4901c6fd2807SJeff Garzik 	return;
4902c6fd2807SJeff Garzik 
490360f5d6efSTejun Heo sys_err:
4904c6fd2807SJeff Garzik 	qc->err_mask |= AC_ERR_SYSTEM;
4905c6fd2807SJeff Garzik err:
4906c6fd2807SJeff Garzik 	ata_qc_complete(qc);
4907c6fd2807SJeff Garzik }
4908c6fd2807SJeff Garzik 
4909c6fd2807SJeff Garzik /**
4910b1c72916STejun Heo  *	ata_phys_link_online - test whether the given link is online
4911936fd732STejun Heo  *	@link: ATA link to test
4912c6fd2807SJeff Garzik  *
4913936fd732STejun Heo  *	Test whether @link is online.  Note that this function returns
4914936fd732STejun Heo  *	0 if online status of @link cannot be obtained, so
4915936fd732STejun Heo  *	ata_link_online(link) != !ata_link_offline(link).
4916c6fd2807SJeff Garzik  *
4917c6fd2807SJeff Garzik  *	LOCKING:
4918c6fd2807SJeff Garzik  *	None.
4919c6fd2807SJeff Garzik  *
4920c6fd2807SJeff Garzik  *	RETURNS:
4921b5b3fa38STejun Heo  *	True if the port online status is available and online.
4922c6fd2807SJeff Garzik  */
4923b1c72916STejun Heo bool ata_phys_link_online(struct ata_link *link)
4924c6fd2807SJeff Garzik {
4925c6fd2807SJeff Garzik 	u32 sstatus;
4926c6fd2807SJeff Garzik 
4927936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
49289913ff8aSTejun Heo 	    ata_sstatus_online(sstatus))
4929b5b3fa38STejun Heo 		return true;
4930b5b3fa38STejun Heo 	return false;
4931c6fd2807SJeff Garzik }
4932c6fd2807SJeff Garzik 
4933c6fd2807SJeff Garzik /**
4934b1c72916STejun Heo  *	ata_phys_link_offline - test whether the given link is offline
4935936fd732STejun Heo  *	@link: ATA link to test
4936c6fd2807SJeff Garzik  *
4937936fd732STejun Heo  *	Test whether @link is offline.  Note that this function
4938936fd732STejun Heo  *	returns 0 if offline status of @link cannot be obtained, so
4939936fd732STejun Heo  *	ata_link_online(link) != !ata_link_offline(link).
4940c6fd2807SJeff Garzik  *
4941c6fd2807SJeff Garzik  *	LOCKING:
4942c6fd2807SJeff Garzik  *	None.
4943c6fd2807SJeff Garzik  *
4944c6fd2807SJeff Garzik  *	RETURNS:
4945b5b3fa38STejun Heo  *	True if the port offline status is available and offline.
4946c6fd2807SJeff Garzik  */
4947b1c72916STejun Heo bool ata_phys_link_offline(struct ata_link *link)
4948c6fd2807SJeff Garzik {
4949c6fd2807SJeff Garzik 	u32 sstatus;
4950c6fd2807SJeff Garzik 
4951936fd732STejun Heo 	if (sata_scr_read(link, SCR_STATUS, &sstatus) == 0 &&
49529913ff8aSTejun Heo 	    !ata_sstatus_online(sstatus))
4953b5b3fa38STejun Heo 		return true;
4954b5b3fa38STejun Heo 	return false;
4955c6fd2807SJeff Garzik }
4956c6fd2807SJeff Garzik 
4957b1c72916STejun Heo /**
4958b1c72916STejun Heo  *	ata_link_online - test whether the given link is online
4959b1c72916STejun Heo  *	@link: ATA link to test
4960b1c72916STejun Heo  *
4961b1c72916STejun Heo  *	Test whether @link is online.  This is identical to
4962b1c72916STejun Heo  *	ata_phys_link_online() when there's no slave link.  When
4963b1c72916STejun Heo  *	there's a slave link, this function should only be called on
4964b1c72916STejun Heo  *	the master link and will return true if any of M/S links is
4965b1c72916STejun Heo  *	online.
4966b1c72916STejun Heo  *
4967b1c72916STejun Heo  *	LOCKING:
4968b1c72916STejun Heo  *	None.
4969b1c72916STejun Heo  *
4970b1c72916STejun Heo  *	RETURNS:
4971b1c72916STejun Heo  *	True if the port online status is available and online.
4972b1c72916STejun Heo  */
4973b1c72916STejun Heo bool ata_link_online(struct ata_link *link)
4974b1c72916STejun Heo {
4975b1c72916STejun Heo 	struct ata_link *slave = link->ap->slave_link;
4976b1c72916STejun Heo 
4977b1c72916STejun Heo 	WARN_ON(link == slave);	/* shouldn't be called on slave link */
4978b1c72916STejun Heo 
4979b1c72916STejun Heo 	return ata_phys_link_online(link) ||
4980b1c72916STejun Heo 		(slave && ata_phys_link_online(slave));
4981b1c72916STejun Heo }
4982a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_link_online);
4983b1c72916STejun Heo 
4984b1c72916STejun Heo /**
4985b1c72916STejun Heo  *	ata_link_offline - test whether the given link is offline
4986b1c72916STejun Heo  *	@link: ATA link to test
4987b1c72916STejun Heo  *
4988b1c72916STejun Heo  *	Test whether @link is offline.  This is identical to
4989b1c72916STejun Heo  *	ata_phys_link_offline() when there's no slave link.  When
4990b1c72916STejun Heo  *	there's a slave link, this function should only be called on
4991b1c72916STejun Heo  *	the master link and will return true if both M/S links are
4992b1c72916STejun Heo  *	offline.
4993b1c72916STejun Heo  *
4994b1c72916STejun Heo  *	LOCKING:
4995b1c72916STejun Heo  *	None.
4996b1c72916STejun Heo  *
4997b1c72916STejun Heo  *	RETURNS:
4998b1c72916STejun Heo  *	True if the port offline status is available and offline.
4999b1c72916STejun Heo  */
5000b1c72916STejun Heo bool ata_link_offline(struct ata_link *link)
5001b1c72916STejun Heo {
5002b1c72916STejun Heo 	struct ata_link *slave = link->ap->slave_link;
5003b1c72916STejun Heo 
5004b1c72916STejun Heo 	WARN_ON(link == slave);	/* shouldn't be called on slave link */
5005b1c72916STejun Heo 
5006b1c72916STejun Heo 	return ata_phys_link_offline(link) &&
5007b1c72916STejun Heo 		(!slave || ata_phys_link_offline(slave));
5008b1c72916STejun Heo }
5009a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_link_offline);
5010b1c72916STejun Heo 
50116ffa01d8STejun Heo #ifdef CONFIG_PM
5012bc6e7c4bSDan Williams static void ata_port_request_pm(struct ata_port *ap, pm_message_t mesg,
5013cca3974eSJeff Garzik 				unsigned int action, unsigned int ehi_flags,
5014bc6e7c4bSDan Williams 				bool async)
5015c6fd2807SJeff Garzik {
5016e3667ebfSTejun Heo 	struct ata_link *link;
50175ef41082SLin Ming 	unsigned long flags;
5018c6fd2807SJeff Garzik 
5019c6fd2807SJeff Garzik 	/* Previous resume operation might still be in
5020c6fd2807SJeff Garzik 	 * progress.  Wait for PM_PENDING to clear.
5021c6fd2807SJeff Garzik 	 */
5022c6fd2807SJeff Garzik 	if (ap->pflags & ATA_PFLAG_PM_PENDING) {
5023c6fd2807SJeff Garzik 		ata_port_wait_eh(ap);
5024c6fd2807SJeff Garzik 		WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5025c6fd2807SJeff Garzik 	}
5026c6fd2807SJeff Garzik 
5027c6fd2807SJeff Garzik 	/* request PM ops to EH */
5028c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
5029c6fd2807SJeff Garzik 
5030c6fd2807SJeff Garzik 	ap->pm_mesg = mesg;
5031c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_PM_PENDING;
50321eca4365STejun Heo 	ata_for_each_link(link, ap, HOST_FIRST) {
5033e3667ebfSTejun Heo 		link->eh_info.action |= action;
5034e3667ebfSTejun Heo 		link->eh_info.flags |= ehi_flags;
5035e3667ebfSTejun Heo 	}
5036c6fd2807SJeff Garzik 
5037c6fd2807SJeff Garzik 	ata_port_schedule_eh(ap);
5038c6fd2807SJeff Garzik 
5039c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
5040c6fd2807SJeff Garzik 
50412fcbdcb4SDan Williams 	if (!async) {
5042c6fd2807SJeff Garzik 		ata_port_wait_eh(ap);
5043c6fd2807SJeff Garzik 		WARN_ON(ap->pflags & ATA_PFLAG_PM_PENDING);
5044c6fd2807SJeff Garzik 	}
5045c6fd2807SJeff Garzik }
5046c6fd2807SJeff Garzik 
504733574d68SLin Ming /*
5048bc6e7c4bSDan Williams  * On some hardware, device fails to respond after spun down for suspend.  As
5049bc6e7c4bSDan Williams  * the device won't be used before being resumed, we don't need to touch the
5050bc6e7c4bSDan Williams  * device.  Ask EH to skip the usual stuff and proceed directly to suspend.
505133574d68SLin Ming  *
505233574d68SLin Ming  * http://thread.gmane.org/gmane.linux.ide/46764
505333574d68SLin Ming  */
5054bc6e7c4bSDan Williams static const unsigned int ata_port_suspend_ehi = ATA_EHI_QUIET
5055bc6e7c4bSDan Williams 						 | ATA_EHI_NO_AUTOPSY
5056bc6e7c4bSDan Williams 						 | ATA_EHI_NO_RECOVERY;
5057bc6e7c4bSDan Williams 
5058bc6e7c4bSDan Williams static void ata_port_suspend(struct ata_port *ap, pm_message_t mesg)
5059bc6e7c4bSDan Williams {
5060bc6e7c4bSDan Williams 	ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, false);
50615ef41082SLin Ming }
50625ef41082SLin Ming 
5063bc6e7c4bSDan Williams static void ata_port_suspend_async(struct ata_port *ap, pm_message_t mesg)
5064bc6e7c4bSDan Williams {
5065bc6e7c4bSDan Williams 	ata_port_request_pm(ap, mesg, 0, ata_port_suspend_ehi, true);
5066bc6e7c4bSDan Williams }
5067bc6e7c4bSDan Williams 
5068bc6e7c4bSDan Williams static int ata_port_pm_suspend(struct device *dev)
50692fcbdcb4SDan Williams {
50702fcbdcb4SDan Williams 	struct ata_port *ap = to_ata_port(dev);
50712fcbdcb4SDan Williams 
50725ef41082SLin Ming 	if (pm_runtime_suspended(dev))
50735ef41082SLin Ming 		return 0;
50745ef41082SLin Ming 
5075bc6e7c4bSDan Williams 	ata_port_suspend(ap, PMSG_SUSPEND);
5076f5e6d0d0SAaron Lu 	return 0;
507733574d68SLin Ming }
507833574d68SLin Ming 
5079bc6e7c4bSDan Williams static int ata_port_pm_freeze(struct device *dev)
50802fcbdcb4SDan Williams {
50812fcbdcb4SDan Williams 	struct ata_port *ap = to_ata_port(dev);
50822fcbdcb4SDan Williams 
5083bc6e7c4bSDan Williams 	if (pm_runtime_suspended(dev))
5084bc6e7c4bSDan Williams 		return 0;
5085bc6e7c4bSDan Williams 
5086bc6e7c4bSDan Williams 	ata_port_suspend(ap, PMSG_FREEZE);
5087bc6e7c4bSDan Williams 	return 0;
50882fcbdcb4SDan Williams }
50892fcbdcb4SDan Williams 
5090bc6e7c4bSDan Williams static int ata_port_pm_poweroff(struct device *dev)
5091e90b1e5aSLin Ming {
5092bc6e7c4bSDan Williams 	ata_port_suspend(to_ata_port(dev), PMSG_HIBERNATE);
5093bc6e7c4bSDan Williams 	return 0;
5094bc6e7c4bSDan Williams }
5095e90b1e5aSLin Ming 
5096bc6e7c4bSDan Williams static const unsigned int ata_port_resume_ehi = ATA_EHI_NO_AUTOPSY
5097bc6e7c4bSDan Williams 						| ATA_EHI_QUIET;
5098bc6e7c4bSDan Williams 
5099bc6e7c4bSDan Williams static void ata_port_resume(struct ata_port *ap, pm_message_t mesg)
5100bc6e7c4bSDan Williams {
5101bc6e7c4bSDan Williams 	ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, false);
5102bc6e7c4bSDan Williams }
5103bc6e7c4bSDan Williams 
5104bc6e7c4bSDan Williams static void ata_port_resume_async(struct ata_port *ap, pm_message_t mesg)
5105bc6e7c4bSDan Williams {
5106bc6e7c4bSDan Williams 	ata_port_request_pm(ap, mesg, ATA_EH_RESET, ata_port_resume_ehi, true);
5107bc6e7c4bSDan Williams }
5108bc6e7c4bSDan Williams 
5109bc6e7c4bSDan Williams static int ata_port_pm_resume(struct device *dev)
5110bc6e7c4bSDan Williams {
5111200421a8STodd Brandt 	ata_port_resume_async(to_ata_port(dev), PMSG_RESUME);
5112e90b1e5aSLin Ming 	pm_runtime_disable(dev);
5113e90b1e5aSLin Ming 	pm_runtime_set_active(dev);
5114e90b1e5aSLin Ming 	pm_runtime_enable(dev);
5115bc6e7c4bSDan Williams 	return 0;
5116e90b1e5aSLin Ming }
5117e90b1e5aSLin Ming 
51187e15e9beSAaron Lu /*
51197e15e9beSAaron Lu  * For ODDs, the upper layer will poll for media change every few seconds,
51207e15e9beSAaron Lu  * which will make it enter and leave suspend state every few seconds. And
51217e15e9beSAaron Lu  * as each suspend will cause a hard/soft reset, the gain of runtime suspend
51227e15e9beSAaron Lu  * is very little and the ODD may malfunction after constantly being reset.
51237e15e9beSAaron Lu  * So the idle callback here will not proceed to suspend if a non-ZPODD capable
51247e15e9beSAaron Lu  * ODD is attached to the port.
51257e15e9beSAaron Lu  */
51269ee4f393SLin Ming static int ata_port_runtime_idle(struct device *dev)
51279ee4f393SLin Ming {
51287e15e9beSAaron Lu 	struct ata_port *ap = to_ata_port(dev);
51297e15e9beSAaron Lu 	struct ata_link *link;
51307e15e9beSAaron Lu 	struct ata_device *adev;
51317e15e9beSAaron Lu 
51327e15e9beSAaron Lu 	ata_for_each_link(link, ap, HOST_FIRST) {
51337e15e9beSAaron Lu 		ata_for_each_dev(adev, link, ENABLED)
51347e15e9beSAaron Lu 			if (adev->class == ATA_DEV_ATAPI &&
51357e15e9beSAaron Lu 			    !zpodd_dev_enabled(adev))
51367e15e9beSAaron Lu 				return -EBUSY;
51377e15e9beSAaron Lu 	}
51387e15e9beSAaron Lu 
513945f0a85cSRafael J. Wysocki 	return 0;
51409ee4f393SLin Ming }
51419ee4f393SLin Ming 
5142a7ff60dbSAaron Lu static int ata_port_runtime_suspend(struct device *dev)
5143a7ff60dbSAaron Lu {
5144bc6e7c4bSDan Williams 	ata_port_suspend(to_ata_port(dev), PMSG_AUTO_SUSPEND);
5145bc6e7c4bSDan Williams 	return 0;
5146a7ff60dbSAaron Lu }
5147a7ff60dbSAaron Lu 
5148a7ff60dbSAaron Lu static int ata_port_runtime_resume(struct device *dev)
5149a7ff60dbSAaron Lu {
5150bc6e7c4bSDan Williams 	ata_port_resume(to_ata_port(dev), PMSG_AUTO_RESUME);
5151bc6e7c4bSDan Williams 	return 0;
5152a7ff60dbSAaron Lu }
5153a7ff60dbSAaron Lu 
51545ef41082SLin Ming static const struct dev_pm_ops ata_port_pm_ops = {
5155bc6e7c4bSDan Williams 	.suspend = ata_port_pm_suspend,
5156bc6e7c4bSDan Williams 	.resume = ata_port_pm_resume,
5157bc6e7c4bSDan Williams 	.freeze = ata_port_pm_freeze,
5158bc6e7c4bSDan Williams 	.thaw = ata_port_pm_resume,
5159bc6e7c4bSDan Williams 	.poweroff = ata_port_pm_poweroff,
5160bc6e7c4bSDan Williams 	.restore = ata_port_pm_resume,
51619ee4f393SLin Ming 
5162a7ff60dbSAaron Lu 	.runtime_suspend = ata_port_runtime_suspend,
5163a7ff60dbSAaron Lu 	.runtime_resume = ata_port_runtime_resume,
51649ee4f393SLin Ming 	.runtime_idle = ata_port_runtime_idle,
51655ef41082SLin Ming };
51665ef41082SLin Ming 
51672fcbdcb4SDan Williams /* sas ports don't participate in pm runtime management of ata_ports,
51682fcbdcb4SDan Williams  * and need to resume ata devices at the domain level, not the per-port
51692fcbdcb4SDan Williams  * level. sas suspend/resume is async to allow parallel port recovery
51702fcbdcb4SDan Williams  * since sas has multiple ata_port instances per Scsi_Host.
51712fcbdcb4SDan Williams  */
5172bc6e7c4bSDan Williams void ata_sas_port_suspend(struct ata_port *ap)
51732fcbdcb4SDan Williams {
5174bc6e7c4bSDan Williams 	ata_port_suspend_async(ap, PMSG_SUSPEND);
51752fcbdcb4SDan Williams }
5176bc6e7c4bSDan Williams EXPORT_SYMBOL_GPL(ata_sas_port_suspend);
51772fcbdcb4SDan Williams 
5178bc6e7c4bSDan Williams void ata_sas_port_resume(struct ata_port *ap)
51792fcbdcb4SDan Williams {
5180bc6e7c4bSDan Williams 	ata_port_resume_async(ap, PMSG_RESUME);
51812fcbdcb4SDan Williams }
5182bc6e7c4bSDan Williams EXPORT_SYMBOL_GPL(ata_sas_port_resume);
51832fcbdcb4SDan Williams 
5184c6fd2807SJeff Garzik /**
5185cca3974eSJeff Garzik  *	ata_host_suspend - suspend host
5186cca3974eSJeff Garzik  *	@host: host to suspend
5187c6fd2807SJeff Garzik  *	@mesg: PM message
5188c6fd2807SJeff Garzik  *
51895ef41082SLin Ming  *	Suspend @host.  Actual operation is performed by port suspend.
5190c6fd2807SJeff Garzik  */
5191ec87cf37SSergey Shtylyov void ata_host_suspend(struct ata_host *host, pm_message_t mesg)
5192c6fd2807SJeff Garzik {
519372ad6ec4SJeff Garzik 	host->dev->power.power_state = mesg;
5194c6fd2807SJeff Garzik }
5195a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_suspend);
5196c6fd2807SJeff Garzik 
5197c6fd2807SJeff Garzik /**
5198cca3974eSJeff Garzik  *	ata_host_resume - resume host
5199cca3974eSJeff Garzik  *	@host: host to resume
5200c6fd2807SJeff Garzik  *
52015ef41082SLin Ming  *	Resume @host.  Actual operation is performed by port resume.
5202c6fd2807SJeff Garzik  */
5203cca3974eSJeff Garzik void ata_host_resume(struct ata_host *host)
5204c6fd2807SJeff Garzik {
520572ad6ec4SJeff Garzik 	host->dev->power.power_state = PMSG_ON;
5206c6fd2807SJeff Garzik }
5207a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_resume);
52086ffa01d8STejun Heo #endif
5209c6fd2807SJeff Garzik 
52108df82c13SBhumika Goyal const struct device_type ata_port_type = {
52115ef41082SLin Ming 	.name = "ata_port",
52125ef41082SLin Ming #ifdef CONFIG_PM
52135ef41082SLin Ming 	.pm = &ata_port_pm_ops,
52145ef41082SLin Ming #endif
52155ef41082SLin Ming };
52165ef41082SLin Ming 
5217c6fd2807SJeff Garzik /**
5218c6fd2807SJeff Garzik  *	ata_dev_init - Initialize an ata_device structure
5219c6fd2807SJeff Garzik  *	@dev: Device structure to initialize
5220c6fd2807SJeff Garzik  *
5221c6fd2807SJeff Garzik  *	Initialize @dev in preparation for probing.
5222c6fd2807SJeff Garzik  *
5223c6fd2807SJeff Garzik  *	LOCKING:
5224c6fd2807SJeff Garzik  *	Inherited from caller.
5225c6fd2807SJeff Garzik  */
5226c6fd2807SJeff Garzik void ata_dev_init(struct ata_device *dev)
5227c6fd2807SJeff Garzik {
5228b1c72916STejun Heo 	struct ata_link *link = ata_dev_phys_link(dev);
52299af5c9c9STejun Heo 	struct ata_port *ap = link->ap;
5230c6fd2807SJeff Garzik 	unsigned long flags;
5231c6fd2807SJeff Garzik 
5232b1c72916STejun Heo 	/* SATA spd limit is bound to the attached device, reset together */
52339af5c9c9STejun Heo 	link->sata_spd_limit = link->hw_sata_spd_limit;
52349af5c9c9STejun Heo 	link->sata_spd = 0;
5235c6fd2807SJeff Garzik 
5236c6fd2807SJeff Garzik 	/* High bits of dev->flags are used to record warm plug
5237c6fd2807SJeff Garzik 	 * requests which occur asynchronously.  Synchronize using
5238cca3974eSJeff Garzik 	 * host lock.
5239c6fd2807SJeff Garzik 	 */
5240c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
5241c6fd2807SJeff Garzik 	dev->flags &= ~ATA_DFLAG_INIT_MASK;
52423dcc323fSTejun Heo 	dev->horkage = 0;
5243c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
5244c6fd2807SJeff Garzik 
524599cf610aSTejun Heo 	memset((void *)dev + ATA_DEVICE_CLEAR_BEGIN, 0,
524699cf610aSTejun Heo 	       ATA_DEVICE_CLEAR_END - ATA_DEVICE_CLEAR_BEGIN);
5247c6fd2807SJeff Garzik 	dev->pio_mask = UINT_MAX;
5248c6fd2807SJeff Garzik 	dev->mwdma_mask = UINT_MAX;
5249c6fd2807SJeff Garzik 	dev->udma_mask = UINT_MAX;
5250c6fd2807SJeff Garzik }
5251c6fd2807SJeff Garzik 
5252c6fd2807SJeff Garzik /**
52534fb37a25STejun Heo  *	ata_link_init - Initialize an ata_link structure
52544fb37a25STejun Heo  *	@ap: ATA port link is attached to
52554fb37a25STejun Heo  *	@link: Link structure to initialize
52568989805dSTejun Heo  *	@pmp: Port multiplier port number
52574fb37a25STejun Heo  *
52584fb37a25STejun Heo  *	Initialize @link.
52594fb37a25STejun Heo  *
52604fb37a25STejun Heo  *	LOCKING:
52614fb37a25STejun Heo  *	Kernel thread context (may sleep)
52624fb37a25STejun Heo  */
5263fb7fd614STejun Heo void ata_link_init(struct ata_port *ap, struct ata_link *link, int pmp)
52644fb37a25STejun Heo {
52654fb37a25STejun Heo 	int i;
52664fb37a25STejun Heo 
52674fb37a25STejun Heo 	/* clear everything except for devices */
5268d9027470SGwendal Grignou 	memset((void *)link + ATA_LINK_CLEAR_BEGIN, 0,
5269d9027470SGwendal Grignou 	       ATA_LINK_CLEAR_END - ATA_LINK_CLEAR_BEGIN);
52704fb37a25STejun Heo 
52714fb37a25STejun Heo 	link->ap = ap;
52728989805dSTejun Heo 	link->pmp = pmp;
52734fb37a25STejun Heo 	link->active_tag = ATA_TAG_POISON;
52744fb37a25STejun Heo 	link->hw_sata_spd_limit = UINT_MAX;
52754fb37a25STejun Heo 
52764fb37a25STejun Heo 	/* can't use iterator, ap isn't initialized yet */
52774fb37a25STejun Heo 	for (i = 0; i < ATA_MAX_DEVICES; i++) {
52784fb37a25STejun Heo 		struct ata_device *dev = &link->device[i];
52794fb37a25STejun Heo 
52804fb37a25STejun Heo 		dev->link = link;
52814fb37a25STejun Heo 		dev->devno = dev - link->device;
5282110f66d2STejun Heo #ifdef CONFIG_ATA_ACPI
5283110f66d2STejun Heo 		dev->gtf_filter = ata_acpi_gtf_filter;
5284110f66d2STejun Heo #endif
52854fb37a25STejun Heo 		ata_dev_init(dev);
52864fb37a25STejun Heo 	}
52874fb37a25STejun Heo }
52884fb37a25STejun Heo 
52894fb37a25STejun Heo /**
52904fb37a25STejun Heo  *	sata_link_init_spd - Initialize link->sata_spd_limit
52914fb37a25STejun Heo  *	@link: Link to configure sata_spd_limit for
52924fb37a25STejun Heo  *
5293a31a6997SMauro Carvalho Chehab  *	Initialize ``link->[hw_]sata_spd_limit`` to the currently
52944fb37a25STejun Heo  *	configured value.
52954fb37a25STejun Heo  *
52964fb37a25STejun Heo  *	LOCKING:
52974fb37a25STejun Heo  *	Kernel thread context (may sleep).
52984fb37a25STejun Heo  *
52994fb37a25STejun Heo  *	RETURNS:
53004fb37a25STejun Heo  *	0 on success, -errno on failure.
53014fb37a25STejun Heo  */
5302fb7fd614STejun Heo int sata_link_init_spd(struct ata_link *link)
53034fb37a25STejun Heo {
530433267325STejun Heo 	u8 spd;
53054fb37a25STejun Heo 	int rc;
53064fb37a25STejun Heo 
5307d127ea7bSTejun Heo 	rc = sata_scr_read(link, SCR_CONTROL, &link->saved_scontrol);
53084fb37a25STejun Heo 	if (rc)
53094fb37a25STejun Heo 		return rc;
53104fb37a25STejun Heo 
5311d127ea7bSTejun Heo 	spd = (link->saved_scontrol >> 4) & 0xf;
53124fb37a25STejun Heo 	if (spd)
53134fb37a25STejun Heo 		link->hw_sata_spd_limit &= (1 << spd) - 1;
53144fb37a25STejun Heo 
531505944bdfSTejun Heo 	ata_force_link_limits(link);
531633267325STejun Heo 
53174fb37a25STejun Heo 	link->sata_spd_limit = link->hw_sata_spd_limit;
53184fb37a25STejun Heo 
53194fb37a25STejun Heo 	return 0;
53204fb37a25STejun Heo }
53214fb37a25STejun Heo 
53224fb37a25STejun Heo /**
5323f3187195STejun Heo  *	ata_port_alloc - allocate and initialize basic ATA port resources
5324f3187195STejun Heo  *	@host: ATA host this allocated port belongs to
5325c6fd2807SJeff Garzik  *
5326f3187195STejun Heo  *	Allocate and initialize basic ATA port resources.
5327f3187195STejun Heo  *
5328f3187195STejun Heo  *	RETURNS:
5329f3187195STejun Heo  *	Allocate ATA port on success, NULL on failure.
5330c6fd2807SJeff Garzik  *
5331c6fd2807SJeff Garzik  *	LOCKING:
5332f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5333c6fd2807SJeff Garzik  */
5334f3187195STejun Heo struct ata_port *ata_port_alloc(struct ata_host *host)
5335c6fd2807SJeff Garzik {
5336f3187195STejun Heo 	struct ata_port *ap;
5337c6fd2807SJeff Garzik 
5338f3187195STejun Heo 	ap = kzalloc(sizeof(*ap), GFP_KERNEL);
5339f3187195STejun Heo 	if (!ap)
5340f3187195STejun Heo 		return NULL;
5341f3187195STejun Heo 
53427b3a24c5SMaxime Bizon 	ap->pflags |= ATA_PFLAG_INITIALIZING | ATA_PFLAG_FROZEN;
5343cca3974eSJeff Garzik 	ap->lock = &host->lock;
5344f3187195STejun Heo 	ap->print_id = -1;
5345e628dc99SDavid Milburn 	ap->local_port_no = -1;
5346cca3974eSJeff Garzik 	ap->host = host;
5347f3187195STejun Heo 	ap->dev = host->dev;
5348c6fd2807SJeff Garzik 
5349ad72cf98STejun Heo 	mutex_init(&ap->scsi_scan_mutex);
535065f27f38SDavid Howells 	INIT_DELAYED_WORK(&ap->hotplug_task, ata_scsi_hotplug);
535165f27f38SDavid Howells 	INIT_WORK(&ap->scsi_rescan_task, ata_scsi_dev_rescan);
5352c6fd2807SJeff Garzik 	INIT_LIST_HEAD(&ap->eh_done_q);
5353c6fd2807SJeff Garzik 	init_waitqueue_head(&ap->eh_wait_q);
535445fabbb7SElias Oltmanns 	init_completion(&ap->park_req_pending);
5355b93ab338SKees Cook 	timer_setup(&ap->fastdrain_timer, ata_eh_fastdrain_timerfn,
5356b93ab338SKees Cook 		    TIMER_DEFERRABLE);
5357c6fd2807SJeff Garzik 
5358c6fd2807SJeff Garzik 	ap->cbl = ATA_CBL_NONE;
5359c6fd2807SJeff Garzik 
53608989805dSTejun Heo 	ata_link_init(ap, &ap->link, 0);
5361c6fd2807SJeff Garzik 
5362c6fd2807SJeff Garzik #ifdef ATA_IRQ_TRAP
5363c6fd2807SJeff Garzik 	ap->stats.unhandled_irq = 1;
5364c6fd2807SJeff Garzik 	ap->stats.idle_irq = 1;
5365c6fd2807SJeff Garzik #endif
5366270390e1STejun Heo 	ata_sff_port_init(ap);
5367270390e1STejun Heo 
5368c6fd2807SJeff Garzik 	return ap;
5369c6fd2807SJeff Garzik }
5370c6fd2807SJeff Garzik 
53712623c7a5STaras Kondratiuk static void ata_devres_release(struct device *gendev, void *res)
5372f0d36efdSTejun Heo {
5373f0d36efdSTejun Heo 	struct ata_host *host = dev_get_drvdata(gendev);
5374f0d36efdSTejun Heo 	int i;
5375f0d36efdSTejun Heo 
5376f0d36efdSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
5377f0d36efdSTejun Heo 		struct ata_port *ap = host->ports[i];
5378f0d36efdSTejun Heo 
5379ecef7253STejun Heo 		if (!ap)
5380ecef7253STejun Heo 			continue;
5381ecef7253STejun Heo 
53824911487aSTejun Heo 		if (ap->scsi_host)
53831aa506e4STejun Heo 			scsi_host_put(ap->scsi_host);
53841aa506e4STejun Heo 
53852623c7a5STaras Kondratiuk 	}
53862623c7a5STaras Kondratiuk 
53872623c7a5STaras Kondratiuk 	dev_set_drvdata(gendev, NULL);
53882623c7a5STaras Kondratiuk 	ata_host_put(host);
53892623c7a5STaras Kondratiuk }
53902623c7a5STaras Kondratiuk 
53912623c7a5STaras Kondratiuk static void ata_host_release(struct kref *kref)
53922623c7a5STaras Kondratiuk {
53932623c7a5STaras Kondratiuk 	struct ata_host *host = container_of(kref, struct ata_host, kref);
53942623c7a5STaras Kondratiuk 	int i;
53952623c7a5STaras Kondratiuk 
53962623c7a5STaras Kondratiuk 	for (i = 0; i < host->n_ports; i++) {
53972623c7a5STaras Kondratiuk 		struct ata_port *ap = host->ports[i];
53982623c7a5STaras Kondratiuk 
5399633273a3STejun Heo 		kfree(ap->pmp_link);
5400b1c72916STejun Heo 		kfree(ap->slave_link);
54014911487aSTejun Heo 		kfree(ap);
54021aa506e4STejun Heo 		host->ports[i] = NULL;
54031aa506e4STejun Heo 	}
54042623c7a5STaras Kondratiuk 	kfree(host);
54052623c7a5STaras Kondratiuk }
54061aa506e4STejun Heo 
54072623c7a5STaras Kondratiuk void ata_host_get(struct ata_host *host)
54082623c7a5STaras Kondratiuk {
54092623c7a5STaras Kondratiuk 	kref_get(&host->kref);
54102623c7a5STaras Kondratiuk }
54112623c7a5STaras Kondratiuk 
54122623c7a5STaras Kondratiuk void ata_host_put(struct ata_host *host)
54132623c7a5STaras Kondratiuk {
54142623c7a5STaras Kondratiuk 	kref_put(&host->kref, ata_host_release);
5415f0d36efdSTejun Heo }
5416a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_put);
5417f0d36efdSTejun Heo 
5418c6fd2807SJeff Garzik /**
5419f3187195STejun Heo  *	ata_host_alloc - allocate and init basic ATA host resources
5420f3187195STejun Heo  *	@dev: generic device this host is associated with
5421f3187195STejun Heo  *	@max_ports: maximum number of ATA ports associated with this host
5422f3187195STejun Heo  *
5423f3187195STejun Heo  *	Allocate and initialize basic ATA host resources.  LLD calls
5424f3187195STejun Heo  *	this function to allocate a host, initializes it fully and
5425f3187195STejun Heo  *	attaches it using ata_host_register().
5426f3187195STejun Heo  *
5427f3187195STejun Heo  *	@max_ports ports are allocated and host->n_ports is
5428f3187195STejun Heo  *	initialized to @max_ports.  The caller is allowed to decrease
5429f3187195STejun Heo  *	host->n_ports before calling ata_host_register().  The unused
5430f3187195STejun Heo  *	ports will be automatically freed on registration.
5431f3187195STejun Heo  *
5432f3187195STejun Heo  *	RETURNS:
5433f3187195STejun Heo  *	Allocate ATA host on success, NULL on failure.
5434f3187195STejun Heo  *
5435f3187195STejun Heo  *	LOCKING:
5436f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5437f3187195STejun Heo  */
5438f3187195STejun Heo struct ata_host *ata_host_alloc(struct device *dev, int max_ports)
5439f3187195STejun Heo {
5440f3187195STejun Heo 	struct ata_host *host;
5441f3187195STejun Heo 	size_t sz;
5442f3187195STejun Heo 	int i;
54432623c7a5STaras Kondratiuk 	void *dr;
5444f3187195STejun Heo 
5445f3187195STejun Heo 	/* alloc a container for our list of ATA ports (buses) */
5446f3187195STejun Heo 	sz = sizeof(struct ata_host) + (max_ports + 1) * sizeof(void *);
54472623c7a5STaras Kondratiuk 	host = kzalloc(sz, GFP_KERNEL);
5448f3187195STejun Heo 	if (!host)
54492623c7a5STaras Kondratiuk 		return NULL;
54502623c7a5STaras Kondratiuk 
5451f3187195STejun Heo 	if (!devres_open_group(dev, NULL, GFP_KERNEL))
5452dafd6c49SColin Ian King 		goto err_free;
5453f3187195STejun Heo 
54542623c7a5STaras Kondratiuk 	dr = devres_alloc(ata_devres_release, 0, GFP_KERNEL);
54552623c7a5STaras Kondratiuk 	if (!dr)
5456f3187195STejun Heo 		goto err_out;
5457f3187195STejun Heo 
54582623c7a5STaras Kondratiuk 	devres_add(dev, dr);
5459f3187195STejun Heo 	dev_set_drvdata(dev, host);
5460f3187195STejun Heo 
5461f3187195STejun Heo 	spin_lock_init(&host->lock);
5462c0c362b6STejun Heo 	mutex_init(&host->eh_mutex);
5463f3187195STejun Heo 	host->dev = dev;
5464f3187195STejun Heo 	host->n_ports = max_ports;
54652623c7a5STaras Kondratiuk 	kref_init(&host->kref);
5466f3187195STejun Heo 
5467f3187195STejun Heo 	/* allocate ports bound to this host */
5468f3187195STejun Heo 	for (i = 0; i < max_ports; i++) {
5469f3187195STejun Heo 		struct ata_port *ap;
5470f3187195STejun Heo 
5471f3187195STejun Heo 		ap = ata_port_alloc(host);
5472f3187195STejun Heo 		if (!ap)
5473f3187195STejun Heo 			goto err_out;
5474f3187195STejun Heo 
5475f3187195STejun Heo 		ap->port_no = i;
5476f3187195STejun Heo 		host->ports[i] = ap;
5477f3187195STejun Heo 	}
5478f3187195STejun Heo 
5479f3187195STejun Heo 	devres_remove_group(dev, NULL);
5480f3187195STejun Heo 	return host;
5481f3187195STejun Heo 
5482f3187195STejun Heo  err_out:
5483f3187195STejun Heo 	devres_release_group(dev, NULL);
5484dafd6c49SColin Ian King  err_free:
5485dafd6c49SColin Ian King 	kfree(host);
5486f3187195STejun Heo 	return NULL;
5487f3187195STejun Heo }
5488a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_alloc);
5489f3187195STejun Heo 
5490f3187195STejun Heo /**
5491f5cda257STejun Heo  *	ata_host_alloc_pinfo - alloc host and init with port_info array
5492f5cda257STejun Heo  *	@dev: generic device this host is associated with
5493f5cda257STejun Heo  *	@ppi: array of ATA port_info to initialize host with
5494f5cda257STejun Heo  *	@n_ports: number of ATA ports attached to this host
5495f5cda257STejun Heo  *
5496f5cda257STejun Heo  *	Allocate ATA host and initialize with info from @ppi.  If NULL
5497f5cda257STejun Heo  *	terminated, @ppi may contain fewer entries than @n_ports.  The
5498f5cda257STejun Heo  *	last entry will be used for the remaining ports.
5499f5cda257STejun Heo  *
5500f5cda257STejun Heo  *	RETURNS:
5501f5cda257STejun Heo  *	Allocate ATA host on success, NULL on failure.
5502f5cda257STejun Heo  *
5503f5cda257STejun Heo  *	LOCKING:
5504f5cda257STejun Heo  *	Inherited from calling layer (may sleep).
5505f5cda257STejun Heo  */
5506f5cda257STejun Heo struct ata_host *ata_host_alloc_pinfo(struct device *dev,
5507f5cda257STejun Heo 				      const struct ata_port_info * const * ppi,
5508f5cda257STejun Heo 				      int n_ports)
5509f5cda257STejun Heo {
5510bf476fe2SSergey Shtylyov 	const struct ata_port_info *pi = &ata_dummy_port_info;
5511f5cda257STejun Heo 	struct ata_host *host;
5512f5cda257STejun Heo 	int i, j;
5513f5cda257STejun Heo 
5514f5cda257STejun Heo 	host = ata_host_alloc(dev, n_ports);
5515f5cda257STejun Heo 	if (!host)
5516f5cda257STejun Heo 		return NULL;
5517f5cda257STejun Heo 
5518bf476fe2SSergey Shtylyov 	for (i = 0, j = 0; i < host->n_ports; i++) {
5519f5cda257STejun Heo 		struct ata_port *ap = host->ports[i];
5520f5cda257STejun Heo 
5521f5cda257STejun Heo 		if (ppi[j])
5522f5cda257STejun Heo 			pi = ppi[j++];
5523f5cda257STejun Heo 
5524f5cda257STejun Heo 		ap->pio_mask = pi->pio_mask;
5525f5cda257STejun Heo 		ap->mwdma_mask = pi->mwdma_mask;
5526f5cda257STejun Heo 		ap->udma_mask = pi->udma_mask;
5527f5cda257STejun Heo 		ap->flags |= pi->flags;
55280c88758bSTejun Heo 		ap->link.flags |= pi->link_flags;
5529f5cda257STejun Heo 		ap->ops = pi->port_ops;
5530f5cda257STejun Heo 
5531f5cda257STejun Heo 		if (!host->ops && (pi->port_ops != &ata_dummy_port_ops))
5532f5cda257STejun Heo 			host->ops = pi->port_ops;
5533f5cda257STejun Heo 	}
5534f5cda257STejun Heo 
5535f5cda257STejun Heo 	return host;
5536f5cda257STejun Heo }
5537a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_alloc_pinfo);
5538f5cda257STejun Heo 
553932ebbc0cSTejun Heo static void ata_host_stop(struct device *gendev, void *res)
554032ebbc0cSTejun Heo {
554132ebbc0cSTejun Heo 	struct ata_host *host = dev_get_drvdata(gendev);
554232ebbc0cSTejun Heo 	int i;
554332ebbc0cSTejun Heo 
554432ebbc0cSTejun Heo 	WARN_ON(!(host->flags & ATA_HOST_STARTED));
554532ebbc0cSTejun Heo 
554632ebbc0cSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
554732ebbc0cSTejun Heo 		struct ata_port *ap = host->ports[i];
554832ebbc0cSTejun Heo 
554932ebbc0cSTejun Heo 		if (ap->ops->port_stop)
555032ebbc0cSTejun Heo 			ap->ops->port_stop(ap);
555132ebbc0cSTejun Heo 	}
555232ebbc0cSTejun Heo 
555332ebbc0cSTejun Heo 	if (host->ops->host_stop)
555432ebbc0cSTejun Heo 		host->ops->host_stop(host);
555532ebbc0cSTejun Heo }
555632ebbc0cSTejun Heo 
5557f5cda257STejun Heo /**
5558029cfd6bSTejun Heo  *	ata_finalize_port_ops - finalize ata_port_operations
5559029cfd6bSTejun Heo  *	@ops: ata_port_operations to finalize
5560029cfd6bSTejun Heo  *
5561029cfd6bSTejun Heo  *	An ata_port_operations can inherit from another ops and that
5562029cfd6bSTejun Heo  *	ops can again inherit from another.  This can go on as many
5563029cfd6bSTejun Heo  *	times as necessary as long as there is no loop in the
5564029cfd6bSTejun Heo  *	inheritance chain.
5565029cfd6bSTejun Heo  *
5566029cfd6bSTejun Heo  *	Ops tables are finalized when the host is started.  NULL or
5567029cfd6bSTejun Heo  *	unspecified entries are inherited from the closet ancestor
5568029cfd6bSTejun Heo  *	which has the method and the entry is populated with it.
5569029cfd6bSTejun Heo  *	After finalization, the ops table directly points to all the
5570029cfd6bSTejun Heo  *	methods and ->inherits is no longer necessary and cleared.
5571029cfd6bSTejun Heo  *
5572029cfd6bSTejun Heo  *	Using ATA_OP_NULL, inheriting ops can force a method to NULL.
5573029cfd6bSTejun Heo  *
5574029cfd6bSTejun Heo  *	LOCKING:
5575029cfd6bSTejun Heo  *	None.
5576029cfd6bSTejun Heo  */
5577029cfd6bSTejun Heo static void ata_finalize_port_ops(struct ata_port_operations *ops)
5578029cfd6bSTejun Heo {
55792da67659SPradeep Singh Rautela 	static DEFINE_SPINLOCK(lock);
5580029cfd6bSTejun Heo 	const struct ata_port_operations *cur;
5581029cfd6bSTejun Heo 	void **begin = (void **)ops;
5582029cfd6bSTejun Heo 	void **end = (void **)&ops->inherits;
5583029cfd6bSTejun Heo 	void **pp;
5584029cfd6bSTejun Heo 
5585029cfd6bSTejun Heo 	if (!ops || !ops->inherits)
5586029cfd6bSTejun Heo 		return;
5587029cfd6bSTejun Heo 
5588029cfd6bSTejun Heo 	spin_lock(&lock);
5589029cfd6bSTejun Heo 
5590029cfd6bSTejun Heo 	for (cur = ops->inherits; cur; cur = cur->inherits) {
5591029cfd6bSTejun Heo 		void **inherit = (void **)cur;
5592029cfd6bSTejun Heo 
5593029cfd6bSTejun Heo 		for (pp = begin; pp < end; pp++, inherit++)
5594029cfd6bSTejun Heo 			if (!*pp)
5595029cfd6bSTejun Heo 				*pp = *inherit;
5596029cfd6bSTejun Heo 	}
5597029cfd6bSTejun Heo 
5598029cfd6bSTejun Heo 	for (pp = begin; pp < end; pp++)
5599029cfd6bSTejun Heo 		if (IS_ERR(*pp))
5600029cfd6bSTejun Heo 			*pp = NULL;
5601029cfd6bSTejun Heo 
5602029cfd6bSTejun Heo 	ops->inherits = NULL;
5603029cfd6bSTejun Heo 
5604029cfd6bSTejun Heo 	spin_unlock(&lock);
5605029cfd6bSTejun Heo }
5606029cfd6bSTejun Heo 
5607029cfd6bSTejun Heo /**
5608ecef7253STejun Heo  *	ata_host_start - start and freeze ports of an ATA host
5609ecef7253STejun Heo  *	@host: ATA host to start ports for
5610ecef7253STejun Heo  *
5611ecef7253STejun Heo  *	Start and then freeze ports of @host.  Started status is
5612ecef7253STejun Heo  *	recorded in host->flags, so this function can be called
5613ecef7253STejun Heo  *	multiple times.  Ports are guaranteed to get started only
5614e0af10acSDiego Viola  *	once.  If host->ops is not initialized yet, it is set to the
5615f3187195STejun Heo  *	first non-dummy port ops.
5616ecef7253STejun Heo  *
5617ecef7253STejun Heo  *	LOCKING:
5618ecef7253STejun Heo  *	Inherited from calling layer (may sleep).
5619ecef7253STejun Heo  *
5620ecef7253STejun Heo  *	RETURNS:
5621ecef7253STejun Heo  *	0 if all ports are started successfully, -errno otherwise.
5622ecef7253STejun Heo  */
5623ecef7253STejun Heo int ata_host_start(struct ata_host *host)
5624ecef7253STejun Heo {
562532ebbc0cSTejun Heo 	int have_stop = 0;
562632ebbc0cSTejun Heo 	void *start_dr = NULL;
5627ecef7253STejun Heo 	int i, rc;
5628ecef7253STejun Heo 
5629ecef7253STejun Heo 	if (host->flags & ATA_HOST_STARTED)
5630ecef7253STejun Heo 		return 0;
5631ecef7253STejun Heo 
5632029cfd6bSTejun Heo 	ata_finalize_port_ops(host->ops);
5633029cfd6bSTejun Heo 
5634ecef7253STejun Heo 	for (i = 0; i < host->n_ports; i++) {
5635ecef7253STejun Heo 		struct ata_port *ap = host->ports[i];
5636ecef7253STejun Heo 
5637029cfd6bSTejun Heo 		ata_finalize_port_ops(ap->ops);
5638029cfd6bSTejun Heo 
5639f3187195STejun Heo 		if (!host->ops && !ata_port_is_dummy(ap))
5640f3187195STejun Heo 			host->ops = ap->ops;
5641f3187195STejun Heo 
564232ebbc0cSTejun Heo 		if (ap->ops->port_stop)
564332ebbc0cSTejun Heo 			have_stop = 1;
564432ebbc0cSTejun Heo 	}
564532ebbc0cSTejun Heo 
5646355a8031SDamien Le Moal 	if (host->ops && host->ops->host_stop)
564732ebbc0cSTejun Heo 		have_stop = 1;
564832ebbc0cSTejun Heo 
564932ebbc0cSTejun Heo 	if (have_stop) {
565032ebbc0cSTejun Heo 		start_dr = devres_alloc(ata_host_stop, 0, GFP_KERNEL);
565132ebbc0cSTejun Heo 		if (!start_dr)
565232ebbc0cSTejun Heo 			return -ENOMEM;
565332ebbc0cSTejun Heo 	}
565432ebbc0cSTejun Heo 
565532ebbc0cSTejun Heo 	for (i = 0; i < host->n_ports; i++) {
565632ebbc0cSTejun Heo 		struct ata_port *ap = host->ports[i];
565732ebbc0cSTejun Heo 
5658ecef7253STejun Heo 		if (ap->ops->port_start) {
5659ecef7253STejun Heo 			rc = ap->ops->port_start(ap);
5660ecef7253STejun Heo 			if (rc) {
56610f9fe9b7SAlan Cox 				if (rc != -ENODEV)
5662a44fec1fSJoe Perches 					dev_err(host->dev,
5663a44fec1fSJoe Perches 						"failed to start port %d (errno=%d)\n",
5664a44fec1fSJoe Perches 						i, rc);
5665ecef7253STejun Heo 				goto err_out;
5666ecef7253STejun Heo 			}
5667ecef7253STejun Heo 		}
5668ecef7253STejun Heo 		ata_eh_freeze_port(ap);
5669ecef7253STejun Heo 	}
5670ecef7253STejun Heo 
567132ebbc0cSTejun Heo 	if (start_dr)
567232ebbc0cSTejun Heo 		devres_add(host->dev, start_dr);
5673ecef7253STejun Heo 	host->flags |= ATA_HOST_STARTED;
5674ecef7253STejun Heo 	return 0;
5675ecef7253STejun Heo 
5676ecef7253STejun Heo  err_out:
5677ecef7253STejun Heo 	while (--i >= 0) {
5678ecef7253STejun Heo 		struct ata_port *ap = host->ports[i];
5679ecef7253STejun Heo 
5680ecef7253STejun Heo 		if (ap->ops->port_stop)
5681ecef7253STejun Heo 			ap->ops->port_stop(ap);
5682ecef7253STejun Heo 	}
568332ebbc0cSTejun Heo 	devres_free(start_dr);
5684ecef7253STejun Heo 	return rc;
5685ecef7253STejun Heo }
5686a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_start);
5687ecef7253STejun Heo 
5688ecef7253STejun Heo /**
568994bd5719SMauro Carvalho Chehab  *	ata_host_init - Initialize a host struct for sas (ipr, libsas)
5690cca3974eSJeff Garzik  *	@host:	host to initialize
5691cca3974eSJeff Garzik  *	@dev:	device host is attached to
5692c6fd2807SJeff Garzik  *	@ops:	port_ops
5693c6fd2807SJeff Garzik  *
5694c6fd2807SJeff Garzik  */
5695cca3974eSJeff Garzik void ata_host_init(struct ata_host *host, struct device *dev,
56968d8e7d13SDan Williams 		   struct ata_port_operations *ops)
5697c6fd2807SJeff Garzik {
5698cca3974eSJeff Garzik 	spin_lock_init(&host->lock);
5699c0c362b6STejun Heo 	mutex_init(&host->eh_mutex);
570069278f79SJens Axboe 	host->n_tags = ATA_MAX_QUEUE;
5701cca3974eSJeff Garzik 	host->dev = dev;
5702cca3974eSJeff Garzik 	host->ops = ops;
57032fa4a326SJason Yan 	kref_init(&host->kref);
5704c6fd2807SJeff Garzik }
5705a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_init);
5706c6fd2807SJeff Garzik 
57079508a66fSDan Williams void __ata_port_probe(struct ata_port *ap)
570879318057SArjan van de Ven {
570979318057SArjan van de Ven 	struct ata_eh_info *ehi = &ap->link.eh_info;
571079318057SArjan van de Ven 	unsigned long flags;
571179318057SArjan van de Ven 
571279318057SArjan van de Ven 	/* kick EH for boot probing */
571379318057SArjan van de Ven 	spin_lock_irqsave(ap->lock, flags);
571479318057SArjan van de Ven 
571579318057SArjan van de Ven 	ehi->probe_mask |= ATA_ALL_DEVICES;
57166b7ae954STejun Heo 	ehi->action |= ATA_EH_RESET;
571779318057SArjan van de Ven 	ehi->flags |= ATA_EHI_NO_AUTOPSY | ATA_EHI_QUIET;
571879318057SArjan van de Ven 
571979318057SArjan van de Ven 	ap->pflags &= ~ATA_PFLAG_INITIALIZING;
572079318057SArjan van de Ven 	ap->pflags |= ATA_PFLAG_LOADING;
572179318057SArjan van de Ven 	ata_port_schedule_eh(ap);
572279318057SArjan van de Ven 
572379318057SArjan van de Ven 	spin_unlock_irqrestore(ap->lock, flags);
57249508a66fSDan Williams }
572579318057SArjan van de Ven 
57269508a66fSDan Williams int ata_port_probe(struct ata_port *ap)
57279508a66fSDan Williams {
57289508a66fSDan Williams 	int rc = 0;
57299508a66fSDan Williams 
57309508a66fSDan Williams 	if (ap->ops->error_handler) {
57319508a66fSDan Williams 		__ata_port_probe(ap);
573279318057SArjan van de Ven 		ata_port_wait_eh(ap);
573379318057SArjan van de Ven 	} else {
573479318057SArjan van de Ven 		rc = ata_bus_probe(ap);
5735238c9cf9SJames Bottomley 	}
5736238c9cf9SJames Bottomley 	return rc;
5737238c9cf9SJames Bottomley }
573879318057SArjan van de Ven 
5739238c9cf9SJames Bottomley 
5740238c9cf9SJames Bottomley static void async_port_probe(void *data, async_cookie_t cookie)
5741238c9cf9SJames Bottomley {
5742238c9cf9SJames Bottomley 	struct ata_port *ap = data;
5743238c9cf9SJames Bottomley 
5744238c9cf9SJames Bottomley 	/*
5745238c9cf9SJames Bottomley 	 * If we're not allowed to scan this host in parallel,
5746238c9cf9SJames Bottomley 	 * we need to wait until all previous scans have completed
5747238c9cf9SJames Bottomley 	 * before going further.
5748238c9cf9SJames Bottomley 	 * Jeff Garzik says this is only within a controller, so we
5749238c9cf9SJames Bottomley 	 * don't need to wait for port 0, only for later ports.
575079318057SArjan van de Ven 	 */
5751238c9cf9SJames Bottomley 	if (!(ap->host->flags & ATA_HOST_PARALLEL_SCAN) && ap->port_no != 0)
5752238c9cf9SJames Bottomley 		async_synchronize_cookie(cookie);
5753238c9cf9SJames Bottomley 
5754238c9cf9SJames Bottomley 	(void)ata_port_probe(ap);
5755f29d3b23SArjan van de Ven 
5756f29d3b23SArjan van de Ven 	/* in order to keep device order, we need to synchronize at this point */
5757f29d3b23SArjan van de Ven 	async_synchronize_cookie(cookie);
5758f29d3b23SArjan van de Ven 
5759f29d3b23SArjan van de Ven 	ata_scsi_scan_host(ap, 1);
576079318057SArjan van de Ven }
5761238c9cf9SJames Bottomley 
5762c6fd2807SJeff Garzik /**
5763f3187195STejun Heo  *	ata_host_register - register initialized ATA host
5764f3187195STejun Heo  *	@host: ATA host to register
5765f3187195STejun Heo  *	@sht: template for SCSI host
5766c6fd2807SJeff Garzik  *
5767f3187195STejun Heo  *	Register initialized ATA host.  @host is allocated using
5768f3187195STejun Heo  *	ata_host_alloc() and fully initialized by LLD.  This function
5769f3187195STejun Heo  *	starts ports, registers @host with ATA and SCSI layers and
5770f3187195STejun Heo  *	probe registered devices.
5771c6fd2807SJeff Garzik  *
5772c6fd2807SJeff Garzik  *	LOCKING:
5773f3187195STejun Heo  *	Inherited from calling layer (may sleep).
5774c6fd2807SJeff Garzik  *
5775c6fd2807SJeff Garzik  *	RETURNS:
5776f3187195STejun Heo  *	0 on success, -errno otherwise.
5777c6fd2807SJeff Garzik  */
5778*25df73d9SBart Van Assche int ata_host_register(struct ata_host *host, const struct scsi_host_template *sht)
5779c6fd2807SJeff Garzik {
5780f3187195STejun Heo 	int i, rc;
5781c6fd2807SJeff Garzik 
578269278f79SJens Axboe 	host->n_tags = clamp(sht->can_queue, 1, ATA_MAX_QUEUE);
57831871ee13SKevin Hao 
5784f3187195STejun Heo 	/* host must have been started */
5785f3187195STejun Heo 	if (!(host->flags & ATA_HOST_STARTED)) {
5786a44fec1fSJoe Perches 		dev_err(host->dev, "BUG: trying to register unstarted host\n");
5787f3187195STejun Heo 		WARN_ON(1);
5788f3187195STejun Heo 		return -EINVAL;
578902f076aaSAlan Cox 	}
5790f0d36efdSTejun Heo 
5791f3187195STejun Heo 	/* Blow away unused ports.  This happens when LLD can't
5792f3187195STejun Heo 	 * determine the exact number of ports to allocate at
5793f3187195STejun Heo 	 * allocation time.
5794f3187195STejun Heo 	 */
5795f3187195STejun Heo 	for (i = host->n_ports; host->ports[i]; i++)
5796f3187195STejun Heo 		kfree(host->ports[i]);
5797f0d36efdSTejun Heo 
5798f3187195STejun Heo 	/* give ports names and add SCSI hosts */
5799e628dc99SDavid Milburn 	for (i = 0; i < host->n_ports; i++) {
580085d6725bSDan Williams 		host->ports[i]->print_id = atomic_inc_return(&ata_print_id);
5801e628dc99SDavid Milburn 		host->ports[i]->local_port_no = i + 1;
5802e628dc99SDavid Milburn 	}
5803d9027470SGwendal Grignou 
5804d9027470SGwendal Grignou 	/* Create associated sysfs transport objects  */
5805d9027470SGwendal Grignou 	for (i = 0; i < host->n_ports; i++) {
5806d9027470SGwendal Grignou 		rc = ata_tport_add(host->dev,host->ports[i]);
5807d9027470SGwendal Grignou 		if (rc) {
5808d9027470SGwendal Grignou 			goto err_tadd;
5809d9027470SGwendal Grignou 		}
5810d9027470SGwendal Grignou 	}
5811d9027470SGwendal Grignou 
5812f3187195STejun Heo 	rc = ata_scsi_add_hosts(host, sht);
5813ecef7253STejun Heo 	if (rc)
5814d9027470SGwendal Grignou 		goto err_tadd;
5815ecef7253STejun Heo 
5816f3187195STejun Heo 	/* set cable, sata_spd_limit and report */
5817cca3974eSJeff Garzik 	for (i = 0; i < host->n_ports; i++) {
5818cca3974eSJeff Garzik 		struct ata_port *ap = host->ports[i];
5819f0a6d77bSSergey Shtylyov 		unsigned int xfer_mask;
5820f3187195STejun Heo 
5821f3187195STejun Heo 		/* set SATA cable type if still unset */
5822f3187195STejun Heo 		if (ap->cbl == ATA_CBL_NONE && (ap->flags & ATA_FLAG_SATA))
5823f3187195STejun Heo 			ap->cbl = ATA_CBL_SATA;
5824c6fd2807SJeff Garzik 
5825c6fd2807SJeff Garzik 		/* init sata_spd_limit to the current value */
58264fb37a25STejun Heo 		sata_link_init_spd(&ap->link);
5827b1c72916STejun Heo 		if (ap->slave_link)
5828b1c72916STejun Heo 			sata_link_init_spd(ap->slave_link);
5829c6fd2807SJeff Garzik 
5830cbcdd875STejun Heo 		/* print per-port info to dmesg */
5831f3187195STejun Heo 		xfer_mask = ata_pack_xfermask(ap->pio_mask, ap->mwdma_mask,
5832f3187195STejun Heo 					      ap->udma_mask);
5833f3187195STejun Heo 
5834abf6e8edSTejun Heo 		if (!ata_port_is_dummy(ap)) {
5835a9a79dfeSJoe Perches 			ata_port_info(ap, "%cATA max %s %s\n",
5836a16abc0bSTejun Heo 				      (ap->flags & ATA_FLAG_SATA) ? 'S' : 'P',
5837f3187195STejun Heo 				      ata_mode_string(xfer_mask),
5838cbcdd875STejun Heo 				      ap->link.eh_info.desc);
5839abf6e8edSTejun Heo 			ata_ehi_clear_desc(&ap->link.eh_info);
5840abf6e8edSTejun Heo 		} else
5841a9a79dfeSJoe Perches 			ata_port_info(ap, "DUMMY\n");
5842c6fd2807SJeff Garzik 	}
5843c6fd2807SJeff Garzik 
5844f6005354SVegard Nossum 	/* perform each probe asynchronously */
5845f3187195STejun Heo 	for (i = 0; i < host->n_ports; i++) {
5846f3187195STejun Heo 		struct ata_port *ap = host->ports[i];
5847b5292111SKai-Heng Feng 		ap->cookie = async_schedule(async_port_probe, ap);
5848c6fd2807SJeff Garzik 	}
5849c6fd2807SJeff Garzik 
5850f3187195STejun Heo 	return 0;
5851d9027470SGwendal Grignou 
5852d9027470SGwendal Grignou  err_tadd:
5853d9027470SGwendal Grignou 	while (--i >= 0) {
5854d9027470SGwendal Grignou 		ata_tport_delete(host->ports[i]);
5855d9027470SGwendal Grignou 	}
5856d9027470SGwendal Grignou 	return rc;
5857d9027470SGwendal Grignou 
5858f3187195STejun Heo }
5859a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_register);
5860f3187195STejun Heo 
5861f3187195STejun Heo /**
5862f5cda257STejun Heo  *	ata_host_activate - start host, request IRQ and register it
5863f5cda257STejun Heo  *	@host: target ATA host
5864f5cda257STejun Heo  *	@irq: IRQ to request
5865f5cda257STejun Heo  *	@irq_handler: irq_handler used when requesting IRQ
5866f5cda257STejun Heo  *	@irq_flags: irq_flags used when requesting IRQ
5867f5cda257STejun Heo  *	@sht: scsi_host_template to use when registering the host
5868f5cda257STejun Heo  *
5869f5cda257STejun Heo  *	After allocating an ATA host and initializing it, most libata
5870f5cda257STejun Heo  *	LLDs perform three steps to activate the host - start host,
5871c9b5560aSMasanari Iida  *	request IRQ and register it.  This helper takes necessary
5872f5cda257STejun Heo  *	arguments and performs the three steps in one go.
5873f5cda257STejun Heo  *
58743d46b2e2SPaul Mundt  *	An invalid IRQ skips the IRQ registration and expects the host to
58753d46b2e2SPaul Mundt  *	have set polling mode on the port. In this case, @irq_handler
58763d46b2e2SPaul Mundt  *	should be NULL.
58773d46b2e2SPaul Mundt  *
5878f5cda257STejun Heo  *	LOCKING:
5879f5cda257STejun Heo  *	Inherited from calling layer (may sleep).
5880f5cda257STejun Heo  *
5881f5cda257STejun Heo  *	RETURNS:
5882f5cda257STejun Heo  *	0 on success, -errno otherwise.
5883f5cda257STejun Heo  */
5884f5cda257STejun Heo int ata_host_activate(struct ata_host *host, int irq,
5885f5cda257STejun Heo 		      irq_handler_t irq_handler, unsigned long irq_flags,
5886*25df73d9SBart Van Assche 		      const struct scsi_host_template *sht)
5887f5cda257STejun Heo {
5888cbcdd875STejun Heo 	int i, rc;
58897e22c002SHeiner Kallweit 	char *irq_desc;
5890f5cda257STejun Heo 
5891f5cda257STejun Heo 	rc = ata_host_start(host);
5892f5cda257STejun Heo 	if (rc)
5893f5cda257STejun Heo 		return rc;
5894f5cda257STejun Heo 
58953d46b2e2SPaul Mundt 	/* Special case for polling mode */
58963d46b2e2SPaul Mundt 	if (!irq) {
58973d46b2e2SPaul Mundt 		WARN_ON(irq_handler);
58983d46b2e2SPaul Mundt 		return ata_host_register(host, sht);
58993d46b2e2SPaul Mundt 	}
59003d46b2e2SPaul Mundt 
59017e22c002SHeiner Kallweit 	irq_desc = devm_kasprintf(host->dev, GFP_KERNEL, "%s[%s]",
59027e22c002SHeiner Kallweit 				  dev_driver_string(host->dev),
59037e22c002SHeiner Kallweit 				  dev_name(host->dev));
59047e22c002SHeiner Kallweit 	if (!irq_desc)
59057e22c002SHeiner Kallweit 		return -ENOMEM;
59067e22c002SHeiner Kallweit 
5907f5cda257STejun Heo 	rc = devm_request_irq(host->dev, irq, irq_handler, irq_flags,
59087e22c002SHeiner Kallweit 			      irq_desc, host);
5909f5cda257STejun Heo 	if (rc)
5910f5cda257STejun Heo 		return rc;
5911f5cda257STejun Heo 
5912cbcdd875STejun Heo 	for (i = 0; i < host->n_ports; i++)
5913cbcdd875STejun Heo 		ata_port_desc(host->ports[i], "irq %d", irq);
59144031826bSTejun Heo 
5915f5cda257STejun Heo 	rc = ata_host_register(host, sht);
5916f5cda257STejun Heo 	/* if failed, just free the IRQ and leave ports alone */
5917f5cda257STejun Heo 	if (rc)
5918f5cda257STejun Heo 		devm_free_irq(host->dev, irq, host);
5919f5cda257STejun Heo 
5920f5cda257STejun Heo 	return rc;
5921f5cda257STejun Heo }
5922a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_activate);
5923f5cda257STejun Heo 
5924f5cda257STejun Heo /**
5925c9b5560aSMasanari Iida  *	ata_port_detach - Detach ATA port in preparation of device removal
5926c6fd2807SJeff Garzik  *	@ap: ATA port to be detached
5927c6fd2807SJeff Garzik  *
5928c6fd2807SJeff Garzik  *	Detach all ATA devices and the associated SCSI devices of @ap;
5929c6fd2807SJeff Garzik  *	then, remove the associated SCSI host.  @ap is guaranteed to
5930c6fd2807SJeff Garzik  *	be quiescent on return from this function.
5931c6fd2807SJeff Garzik  *
5932c6fd2807SJeff Garzik  *	LOCKING:
5933c6fd2807SJeff Garzik  *	Kernel thread context (may sleep).
5934c6fd2807SJeff Garzik  */
5935741b7763SAdrian Bunk static void ata_port_detach(struct ata_port *ap)
5936c6fd2807SJeff Garzik {
5937c6fd2807SJeff Garzik 	unsigned long flags;
5938a6f9bf4dSLevente Kurusa 	struct ata_link *link;
5939a6f9bf4dSLevente Kurusa 	struct ata_device *dev;
5940c6fd2807SJeff Garzik 
5941c6fd2807SJeff Garzik 	if (!ap->ops->error_handler)
5942c6fd2807SJeff Garzik 		goto skip_eh;
5943c6fd2807SJeff Garzik 
5944c6fd2807SJeff Garzik 	/* tell EH we're leaving & flush EH */
5945c6fd2807SJeff Garzik 	spin_lock_irqsave(ap->lock, flags);
5946c6fd2807SJeff Garzik 	ap->pflags |= ATA_PFLAG_UNLOADING;
5947ece180d1STejun Heo 	ata_port_schedule_eh(ap);
5948c6fd2807SJeff Garzik 	spin_unlock_irqrestore(ap->lock, flags);
5949c6fd2807SJeff Garzik 
5950ece180d1STejun Heo 	/* wait till EH commits suicide */
5951c6fd2807SJeff Garzik 	ata_port_wait_eh(ap);
5952c6fd2807SJeff Garzik 
5953ece180d1STejun Heo 	/* it better be dead now */
5954ece180d1STejun Heo 	WARN_ON(!(ap->pflags & ATA_PFLAG_UNLOADED));
5955c6fd2807SJeff Garzik 
5956afe2c511STejun Heo 	cancel_delayed_work_sync(&ap->hotplug_task);
5957c6fd2807SJeff Garzik 
5958c6fd2807SJeff Garzik  skip_eh:
5959a6f9bf4dSLevente Kurusa 	/* clean up zpodd on port removal */
5960a6f9bf4dSLevente Kurusa 	ata_for_each_link(link, ap, HOST_FIRST) {
5961a6f9bf4dSLevente Kurusa 		ata_for_each_dev(dev, link, ALL) {
5962a6f9bf4dSLevente Kurusa 			if (zpodd_dev_enabled(dev))
5963a6f9bf4dSLevente Kurusa 				zpodd_exit(dev);
5964a6f9bf4dSLevente Kurusa 		}
5965a6f9bf4dSLevente Kurusa 	}
5966d9027470SGwendal Grignou 	if (ap->pmp_link) {
5967d9027470SGwendal Grignou 		int i;
5968d9027470SGwendal Grignou 		for (i = 0; i < SATA_PMP_MAX_PORTS; i++)
5969d9027470SGwendal Grignou 			ata_tlink_delete(&ap->pmp_link[i]);
5970d9027470SGwendal Grignou 	}
5971c6fd2807SJeff Garzik 	/* remove the associated SCSI host */
5972cca3974eSJeff Garzik 	scsi_remove_host(ap->scsi_host);
5973c5700766SRafael J. Wysocki 	ata_tport_delete(ap);
5974c6fd2807SJeff Garzik }
5975c6fd2807SJeff Garzik 
5976c6fd2807SJeff Garzik /**
59770529c159STejun Heo  *	ata_host_detach - Detach all ports of an ATA host
59780529c159STejun Heo  *	@host: Host to detach
59790529c159STejun Heo  *
59800529c159STejun Heo  *	Detach all ports of @host.
59810529c159STejun Heo  *
59820529c159STejun Heo  *	LOCKING:
59830529c159STejun Heo  *	Kernel thread context (may sleep).
59840529c159STejun Heo  */
59850529c159STejun Heo void ata_host_detach(struct ata_host *host)
59860529c159STejun Heo {
59870529c159STejun Heo 	int i;
59880529c159STejun Heo 
5989b5292111SKai-Heng Feng 	for (i = 0; i < host->n_ports; i++) {
5990130f4cafSJohn Garry 		/* Ensure ata_port probe has completed */
5991b5292111SKai-Heng Feng 		async_synchronize_cookie(host->ports[i]->cookie + 1);
59920529c159STejun Heo 		ata_port_detach(host->ports[i]);
5993b5292111SKai-Heng Feng 	}
5994562f0c2dSTejun Heo 
5995562f0c2dSTejun Heo 	/* the host is dead now, dissociate ACPI */
5996562f0c2dSTejun Heo 	ata_acpi_dissociate(host);
59970529c159STejun Heo }
5998a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_host_detach);
59990529c159STejun Heo 
6000c6fd2807SJeff Garzik #ifdef CONFIG_PCI
6001c6fd2807SJeff Garzik 
6002c6fd2807SJeff Garzik /**
6003c6fd2807SJeff Garzik  *	ata_pci_remove_one - PCI layer callback for device removal
6004c6fd2807SJeff Garzik  *	@pdev: PCI device that was removed
6005c6fd2807SJeff Garzik  *
6006b878ca5dSTejun Heo  *	PCI layer indicates to libata via this hook that hot-unplug or
6007b878ca5dSTejun Heo  *	module unload event has occurred.  Detach all ports.  Resource
6008b878ca5dSTejun Heo  *	release is handled via devres.
6009c6fd2807SJeff Garzik  *
6010c6fd2807SJeff Garzik  *	LOCKING:
6011c6fd2807SJeff Garzik  *	Inherited from PCI layer (may sleep).
6012c6fd2807SJeff Garzik  */
6013c6fd2807SJeff Garzik void ata_pci_remove_one(struct pci_dev *pdev)
6014c6fd2807SJeff Garzik {
601504a3f5b7SBrian Norris 	struct ata_host *host = pci_get_drvdata(pdev);
6016c6fd2807SJeff Garzik 
6017f0d36efdSTejun Heo 	ata_host_detach(host);
6018c6fd2807SJeff Garzik }
6019a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pci_remove_one);
6020c6fd2807SJeff Garzik 
602110a663a1SPrabhakar Kushwaha void ata_pci_shutdown_one(struct pci_dev *pdev)
602210a663a1SPrabhakar Kushwaha {
602310a663a1SPrabhakar Kushwaha 	struct ata_host *host = pci_get_drvdata(pdev);
602410a663a1SPrabhakar Kushwaha 	int i;
602510a663a1SPrabhakar Kushwaha 
602610a663a1SPrabhakar Kushwaha 	for (i = 0; i < host->n_ports; i++) {
602710a663a1SPrabhakar Kushwaha 		struct ata_port *ap = host->ports[i];
602810a663a1SPrabhakar Kushwaha 
602910a663a1SPrabhakar Kushwaha 		ap->pflags |= ATA_PFLAG_FROZEN;
603010a663a1SPrabhakar Kushwaha 
603110a663a1SPrabhakar Kushwaha 		/* Disable port interrupts */
603210a663a1SPrabhakar Kushwaha 		if (ap->ops->freeze)
603310a663a1SPrabhakar Kushwaha 			ap->ops->freeze(ap);
603410a663a1SPrabhakar Kushwaha 
603510a663a1SPrabhakar Kushwaha 		/* Stop the port DMA engines */
603610a663a1SPrabhakar Kushwaha 		if (ap->ops->port_stop)
603710a663a1SPrabhakar Kushwaha 			ap->ops->port_stop(ap);
603810a663a1SPrabhakar Kushwaha 	}
603910a663a1SPrabhakar Kushwaha }
6040a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pci_shutdown_one);
604110a663a1SPrabhakar Kushwaha 
6042c6fd2807SJeff Garzik /* move to PCI subsystem */
6043c6fd2807SJeff Garzik int pci_test_config_bits(struct pci_dev *pdev, const struct pci_bits *bits)
6044c6fd2807SJeff Garzik {
6045c6fd2807SJeff Garzik 	unsigned long tmp = 0;
6046c6fd2807SJeff Garzik 
6047c6fd2807SJeff Garzik 	switch (bits->width) {
6048c6fd2807SJeff Garzik 	case 1: {
6049c6fd2807SJeff Garzik 		u8 tmp8 = 0;
6050c6fd2807SJeff Garzik 		pci_read_config_byte(pdev, bits->reg, &tmp8);
6051c6fd2807SJeff Garzik 		tmp = tmp8;
6052c6fd2807SJeff Garzik 		break;
6053c6fd2807SJeff Garzik 	}
6054c6fd2807SJeff Garzik 	case 2: {
6055c6fd2807SJeff Garzik 		u16 tmp16 = 0;
6056c6fd2807SJeff Garzik 		pci_read_config_word(pdev, bits->reg, &tmp16);
6057c6fd2807SJeff Garzik 		tmp = tmp16;
6058c6fd2807SJeff Garzik 		break;
6059c6fd2807SJeff Garzik 	}
6060c6fd2807SJeff Garzik 	case 4: {
6061c6fd2807SJeff Garzik 		u32 tmp32 = 0;
6062c6fd2807SJeff Garzik 		pci_read_config_dword(pdev, bits->reg, &tmp32);
6063c6fd2807SJeff Garzik 		tmp = tmp32;
6064c6fd2807SJeff Garzik 		break;
6065c6fd2807SJeff Garzik 	}
6066c6fd2807SJeff Garzik 
6067c6fd2807SJeff Garzik 	default:
6068c6fd2807SJeff Garzik 		return -EINVAL;
6069c6fd2807SJeff Garzik 	}
6070c6fd2807SJeff Garzik 
6071c6fd2807SJeff Garzik 	tmp &= bits->mask;
6072c6fd2807SJeff Garzik 
6073c6fd2807SJeff Garzik 	return (tmp == bits->val) ? 1 : 0;
6074c6fd2807SJeff Garzik }
6075a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(pci_test_config_bits);
6076c6fd2807SJeff Garzik 
60776ffa01d8STejun Heo #ifdef CONFIG_PM
6078c6fd2807SJeff Garzik void ata_pci_device_do_suspend(struct pci_dev *pdev, pm_message_t mesg)
6079c6fd2807SJeff Garzik {
6080c6fd2807SJeff Garzik 	pci_save_state(pdev);
6081c6fd2807SJeff Garzik 	pci_disable_device(pdev);
60824c90d971STejun Heo 
60833a2d5b70SRafael J. Wysocki 	if (mesg.event & PM_EVENT_SLEEP)
6084c6fd2807SJeff Garzik 		pci_set_power_state(pdev, PCI_D3hot);
6085c6fd2807SJeff Garzik }
6086a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pci_device_do_suspend);
6087c6fd2807SJeff Garzik 
6088553c4aa6STejun Heo int ata_pci_device_do_resume(struct pci_dev *pdev)
6089c6fd2807SJeff Garzik {
6090553c4aa6STejun Heo 	int rc;
6091553c4aa6STejun Heo 
6092c6fd2807SJeff Garzik 	pci_set_power_state(pdev, PCI_D0);
6093c6fd2807SJeff Garzik 	pci_restore_state(pdev);
6094553c4aa6STejun Heo 
6095f0d36efdSTejun Heo 	rc = pcim_enable_device(pdev);
6096553c4aa6STejun Heo 	if (rc) {
6097a44fec1fSJoe Perches 		dev_err(&pdev->dev,
6098553c4aa6STejun Heo 			"failed to enable device after resume (%d)\n", rc);
6099553c4aa6STejun Heo 		return rc;
6100553c4aa6STejun Heo 	}
6101553c4aa6STejun Heo 
6102c6fd2807SJeff Garzik 	pci_set_master(pdev);
6103553c4aa6STejun Heo 	return 0;
6104c6fd2807SJeff Garzik }
6105a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pci_device_do_resume);
6106c6fd2807SJeff Garzik 
6107c6fd2807SJeff Garzik int ata_pci_device_suspend(struct pci_dev *pdev, pm_message_t mesg)
6108c6fd2807SJeff Garzik {
610904a3f5b7SBrian Norris 	struct ata_host *host = pci_get_drvdata(pdev);
6110c6fd2807SJeff Garzik 
6111ec87cf37SSergey Shtylyov 	ata_host_suspend(host, mesg);
6112c6fd2807SJeff Garzik 
6113c6fd2807SJeff Garzik 	ata_pci_device_do_suspend(pdev, mesg);
6114c6fd2807SJeff Garzik 
6115c6fd2807SJeff Garzik 	return 0;
6116c6fd2807SJeff Garzik }
6117a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pci_device_suspend);
6118c6fd2807SJeff Garzik 
6119c6fd2807SJeff Garzik int ata_pci_device_resume(struct pci_dev *pdev)
6120c6fd2807SJeff Garzik {
612104a3f5b7SBrian Norris 	struct ata_host *host = pci_get_drvdata(pdev);
6122553c4aa6STejun Heo 	int rc;
6123c6fd2807SJeff Garzik 
6124553c4aa6STejun Heo 	rc = ata_pci_device_do_resume(pdev);
6125553c4aa6STejun Heo 	if (rc == 0)
6126cca3974eSJeff Garzik 		ata_host_resume(host);
6127553c4aa6STejun Heo 	return rc;
6128c6fd2807SJeff Garzik }
6129a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_pci_device_resume);
61306ffa01d8STejun Heo #endif /* CONFIG_PM */
6131c6fd2807SJeff Garzik #endif /* CONFIG_PCI */
6132c6fd2807SJeff Garzik 
6133b7db04d9SBrian Norris /**
6134b7db04d9SBrian Norris  *	ata_platform_remove_one - Platform layer callback for device removal
6135b7db04d9SBrian Norris  *	@pdev: Platform device that was removed
6136b7db04d9SBrian Norris  *
6137b7db04d9SBrian Norris  *	Platform layer indicates to libata via this hook that hot-unplug or
6138b7db04d9SBrian Norris  *	module unload event has occurred.  Detach all ports.  Resource
6139b7db04d9SBrian Norris  *	release is handled via devres.
6140b7db04d9SBrian Norris  *
6141b7db04d9SBrian Norris  *	LOCKING:
6142b7db04d9SBrian Norris  *	Inherited from platform layer (may sleep).
6143b7db04d9SBrian Norris  */
6144b7db04d9SBrian Norris int ata_platform_remove_one(struct platform_device *pdev)
6145b7db04d9SBrian Norris {
6146b7db04d9SBrian Norris 	struct ata_host *host = platform_get_drvdata(pdev);
6147b7db04d9SBrian Norris 
6148b7db04d9SBrian Norris 	ata_host_detach(host);
6149b7db04d9SBrian Norris 
6150b7db04d9SBrian Norris 	return 0;
6151b7db04d9SBrian Norris }
6152a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_platform_remove_one);
6153b7db04d9SBrian Norris 
6154bf89b0bfSBartlomiej Zolnierkiewicz #ifdef CONFIG_ATA_FORCE
6155168af4afSDamien Le Moal 
6156168af4afSDamien Le Moal #define force_cbl(name, flag)				\
6157168af4afSDamien Le Moal 	{ #name,	.cbl		= (flag) }
6158168af4afSDamien Le Moal 
6159168af4afSDamien Le Moal #define force_spd_limit(spd, val)			\
6160168af4afSDamien Le Moal 	{ #spd,	.spd_limit		= (val) }
6161168af4afSDamien Le Moal 
6162168af4afSDamien Le Moal #define force_xfer(mode, shift)				\
6163168af4afSDamien Le Moal 	{ #mode,	.xfer_mask	= (1UL << (shift)) }
6164168af4afSDamien Le Moal 
61653af9ca4dSDamien Le Moal #define force_lflag_on(name, flags)			\
61663af9ca4dSDamien Le Moal 	{ #name,	.lflags_on	= (flags) }
61673af9ca4dSDamien Le Moal 
61683af9ca4dSDamien Le Moal #define force_lflag_onoff(name, flags)			\
61693af9ca4dSDamien Le Moal 	{ "no" #name,	.lflags_on	= (flags) },	\
61703af9ca4dSDamien Le Moal 	{ #name,	.lflags_off	= (flags) }
6171168af4afSDamien Le Moal 
6172168af4afSDamien Le Moal #define force_horkage_on(name, flag)			\
6173168af4afSDamien Le Moal 	{ #name,	.horkage_on	= (flag) }
6174168af4afSDamien Le Moal 
6175168af4afSDamien Le Moal #define force_horkage_onoff(name, flag)			\
6176168af4afSDamien Le Moal 	{ "no" #name,	.horkage_on	= (flag) },	\
6177168af4afSDamien Le Moal 	{ #name,	.horkage_off	= (flag) }
6178168af4afSDamien Le Moal 
6179168af4afSDamien Le Moal static const struct ata_force_param force_tbl[] __initconst = {
6180168af4afSDamien Le Moal 	force_cbl(40c,			ATA_CBL_PATA40),
6181168af4afSDamien Le Moal 	force_cbl(80c,			ATA_CBL_PATA80),
6182168af4afSDamien Le Moal 	force_cbl(short40c,		ATA_CBL_PATA40_SHORT),
6183168af4afSDamien Le Moal 	force_cbl(unk,			ATA_CBL_PATA_UNK),
6184168af4afSDamien Le Moal 	force_cbl(ign,			ATA_CBL_PATA_IGN),
6185168af4afSDamien Le Moal 	force_cbl(sata,			ATA_CBL_SATA),
6186168af4afSDamien Le Moal 
6187168af4afSDamien Le Moal 	force_spd_limit(1.5Gbps,	1),
6188168af4afSDamien Le Moal 	force_spd_limit(3.0Gbps,	2),
6189168af4afSDamien Le Moal 
6190168af4afSDamien Le Moal 	force_xfer(pio0,		ATA_SHIFT_PIO + 0),
6191168af4afSDamien Le Moal 	force_xfer(pio1,		ATA_SHIFT_PIO + 1),
6192168af4afSDamien Le Moal 	force_xfer(pio2,		ATA_SHIFT_PIO + 2),
6193168af4afSDamien Le Moal 	force_xfer(pio3,		ATA_SHIFT_PIO + 3),
6194168af4afSDamien Le Moal 	force_xfer(pio4,		ATA_SHIFT_PIO + 4),
6195168af4afSDamien Le Moal 	force_xfer(pio5,		ATA_SHIFT_PIO + 5),
6196168af4afSDamien Le Moal 	force_xfer(pio6,		ATA_SHIFT_PIO + 6),
6197168af4afSDamien Le Moal 	force_xfer(mwdma0,		ATA_SHIFT_MWDMA + 0),
6198168af4afSDamien Le Moal 	force_xfer(mwdma1,		ATA_SHIFT_MWDMA + 1),
6199168af4afSDamien Le Moal 	force_xfer(mwdma2,		ATA_SHIFT_MWDMA + 2),
6200168af4afSDamien Le Moal 	force_xfer(mwdma3,		ATA_SHIFT_MWDMA + 3),
6201168af4afSDamien Le Moal 	force_xfer(mwdma4,		ATA_SHIFT_MWDMA + 4),
6202168af4afSDamien Le Moal 	force_xfer(udma0,		ATA_SHIFT_UDMA + 0),
6203168af4afSDamien Le Moal 	force_xfer(udma16,		ATA_SHIFT_UDMA + 0),
6204168af4afSDamien Le Moal 	force_xfer(udma/16,		ATA_SHIFT_UDMA + 0),
6205168af4afSDamien Le Moal 	force_xfer(udma1,		ATA_SHIFT_UDMA + 1),
6206168af4afSDamien Le Moal 	force_xfer(udma25,		ATA_SHIFT_UDMA + 1),
6207168af4afSDamien Le Moal 	force_xfer(udma/25,		ATA_SHIFT_UDMA + 1),
6208168af4afSDamien Le Moal 	force_xfer(udma2,		ATA_SHIFT_UDMA + 2),
6209168af4afSDamien Le Moal 	force_xfer(udma33,		ATA_SHIFT_UDMA + 2),
6210168af4afSDamien Le Moal 	force_xfer(udma/33,		ATA_SHIFT_UDMA + 2),
6211168af4afSDamien Le Moal 	force_xfer(udma3,		ATA_SHIFT_UDMA + 3),
6212168af4afSDamien Le Moal 	force_xfer(udma44,		ATA_SHIFT_UDMA + 3),
6213168af4afSDamien Le Moal 	force_xfer(udma/44,		ATA_SHIFT_UDMA + 3),
6214168af4afSDamien Le Moal 	force_xfer(udma4,		ATA_SHIFT_UDMA + 4),
6215168af4afSDamien Le Moal 	force_xfer(udma66,		ATA_SHIFT_UDMA + 4),
6216168af4afSDamien Le Moal 	force_xfer(udma/66,		ATA_SHIFT_UDMA + 4),
6217168af4afSDamien Le Moal 	force_xfer(udma5,		ATA_SHIFT_UDMA + 5),
6218168af4afSDamien Le Moal 	force_xfer(udma100,		ATA_SHIFT_UDMA + 5),
6219168af4afSDamien Le Moal 	force_xfer(udma/100,		ATA_SHIFT_UDMA + 5),
6220168af4afSDamien Le Moal 	force_xfer(udma6,		ATA_SHIFT_UDMA + 6),
6221168af4afSDamien Le Moal 	force_xfer(udma133,		ATA_SHIFT_UDMA + 6),
6222168af4afSDamien Le Moal 	force_xfer(udma/133,		ATA_SHIFT_UDMA + 6),
6223168af4afSDamien Le Moal 	force_xfer(udma7,		ATA_SHIFT_UDMA + 7),
6224168af4afSDamien Le Moal 
62253af9ca4dSDamien Le Moal 	force_lflag_on(nohrst,		ATA_LFLAG_NO_HRST),
62263af9ca4dSDamien Le Moal 	force_lflag_on(nosrst,		ATA_LFLAG_NO_SRST),
62273af9ca4dSDamien Le Moal 	force_lflag_on(norst,		ATA_LFLAG_NO_HRST | ATA_LFLAG_NO_SRST),
62283af9ca4dSDamien Le Moal 	force_lflag_on(rstonce,		ATA_LFLAG_RST_ONCE),
62293af9ca4dSDamien Le Moal 	force_lflag_onoff(dbdelay,	ATA_LFLAG_NO_DEBOUNCE_DELAY),
6230168af4afSDamien Le Moal 
6231168af4afSDamien Le Moal 	force_horkage_onoff(ncq,	ATA_HORKAGE_NONCQ),
6232168af4afSDamien Le Moal 	force_horkage_onoff(ncqtrim,	ATA_HORKAGE_NO_NCQ_TRIM),
6233168af4afSDamien Le Moal 	force_horkage_onoff(ncqati,	ATA_HORKAGE_NO_NCQ_ON_ATI),
6234168af4afSDamien Le Moal 
62352c33bbdaSDamien Le Moal 	force_horkage_onoff(trim,	ATA_HORKAGE_NOTRIM),
62362c33bbdaSDamien Le Moal 	force_horkage_on(trim_zero,	ATA_HORKAGE_ZERO_AFTER_TRIM),
62372c33bbdaSDamien Le Moal 	force_horkage_on(max_trim_128m, ATA_HORKAGE_MAX_TRIM_128M),
62382c33bbdaSDamien Le Moal 
62392c33bbdaSDamien Le Moal 	force_horkage_onoff(dma,	ATA_HORKAGE_NODMA),
6240168af4afSDamien Le Moal 	force_horkage_on(atapi_dmadir,	ATA_HORKAGE_ATAPI_DMADIR),
62412c33bbdaSDamien Le Moal 	force_horkage_on(atapi_mod16_dma, ATA_HORKAGE_ATAPI_MOD16_DMA),
62422c33bbdaSDamien Le Moal 
62432c33bbdaSDamien Le Moal 	force_horkage_onoff(dmalog,	ATA_HORKAGE_NO_DMA_LOG),
62442c33bbdaSDamien Le Moal 	force_horkage_onoff(iddevlog,	ATA_HORKAGE_NO_ID_DEV_LOG),
62452c33bbdaSDamien Le Moal 	force_horkage_onoff(logdir,	ATA_HORKAGE_NO_LOG_DIR),
62462c33bbdaSDamien Le Moal 
62472c33bbdaSDamien Le Moal 	force_horkage_on(max_sec_128,	ATA_HORKAGE_MAX_SEC_128),
62482c33bbdaSDamien Le Moal 	force_horkage_on(max_sec_1024,	ATA_HORKAGE_MAX_SEC_1024),
62492c33bbdaSDamien Le Moal 	force_horkage_on(max_sec_lba48,	ATA_HORKAGE_MAX_SEC_LBA48),
62502c33bbdaSDamien Le Moal 
62512c33bbdaSDamien Le Moal 	force_horkage_onoff(lpm,	ATA_HORKAGE_NOLPM),
62522c33bbdaSDamien Le Moal 	force_horkage_onoff(setxfer,	ATA_HORKAGE_NOSETXFER),
62532c33bbdaSDamien Le Moal 	force_horkage_on(dump_id,	ATA_HORKAGE_DUMP_ID),
62544d2e4980SDamien Le Moal 	force_horkage_onoff(fua,	ATA_HORKAGE_NO_FUA),
62552c33bbdaSDamien Le Moal 
62562c33bbdaSDamien Le Moal 	force_horkage_on(disable,	ATA_HORKAGE_DISABLE),
6257168af4afSDamien Le Moal };
6258168af4afSDamien Le Moal 
625933267325STejun Heo static int __init ata_parse_force_one(char **cur,
626033267325STejun Heo 				      struct ata_force_ent *force_ent,
626133267325STejun Heo 				      const char **reason)
626233267325STejun Heo {
626333267325STejun Heo 	char *start = *cur, *p = *cur;
626433267325STejun Heo 	char *id, *val, *endp;
626533267325STejun Heo 	const struct ata_force_param *match_fp = NULL;
626633267325STejun Heo 	int nr_matches = 0, i;
626733267325STejun Heo 
626833267325STejun Heo 	/* find where this param ends and update *cur */
626933267325STejun Heo 	while (*p != '\0' && *p != ',')
627033267325STejun Heo 		p++;
627133267325STejun Heo 
627233267325STejun Heo 	if (*p == '\0')
627333267325STejun Heo 		*cur = p;
627433267325STejun Heo 	else
627533267325STejun Heo 		*cur = p + 1;
627633267325STejun Heo 
627733267325STejun Heo 	*p = '\0';
627833267325STejun Heo 
627933267325STejun Heo 	/* parse */
628033267325STejun Heo 	p = strchr(start, ':');
628133267325STejun Heo 	if (!p) {
628233267325STejun Heo 		val = strstrip(start);
628333267325STejun Heo 		goto parse_val;
628433267325STejun Heo 	}
628533267325STejun Heo 	*p = '\0';
628633267325STejun Heo 
628733267325STejun Heo 	id = strstrip(start);
628833267325STejun Heo 	val = strstrip(p + 1);
628933267325STejun Heo 
629033267325STejun Heo 	/* parse id */
629133267325STejun Heo 	p = strchr(id, '.');
629233267325STejun Heo 	if (p) {
629333267325STejun Heo 		*p++ = '\0';
629433267325STejun Heo 		force_ent->device = simple_strtoul(p, &endp, 10);
629533267325STejun Heo 		if (p == endp || *endp != '\0') {
629633267325STejun Heo 			*reason = "invalid device";
629733267325STejun Heo 			return -EINVAL;
629833267325STejun Heo 		}
629933267325STejun Heo 	}
630033267325STejun Heo 
630133267325STejun Heo 	force_ent->port = simple_strtoul(id, &endp, 10);
6302f7cf69aeSTejun Heo 	if (id == endp || *endp != '\0') {
630333267325STejun Heo 		*reason = "invalid port/link";
630433267325STejun Heo 		return -EINVAL;
630533267325STejun Heo 	}
630633267325STejun Heo 
630733267325STejun Heo  parse_val:
630833267325STejun Heo 	/* parse val, allow shortcuts so that both 1.5 and 1.5Gbps work */
630933267325STejun Heo 	for (i = 0; i < ARRAY_SIZE(force_tbl); i++) {
631033267325STejun Heo 		const struct ata_force_param *fp = &force_tbl[i];
631133267325STejun Heo 
631233267325STejun Heo 		if (strncasecmp(val, fp->name, strlen(val)))
631333267325STejun Heo 			continue;
631433267325STejun Heo 
631533267325STejun Heo 		nr_matches++;
631633267325STejun Heo 		match_fp = fp;
631733267325STejun Heo 
631833267325STejun Heo 		if (strcasecmp(val, fp->name) == 0) {
631933267325STejun Heo 			nr_matches = 1;
632033267325STejun Heo 			break;
632133267325STejun Heo 		}
632233267325STejun Heo 	}
632333267325STejun Heo 
632433267325STejun Heo 	if (!nr_matches) {
632533267325STejun Heo 		*reason = "unknown value";
632633267325STejun Heo 		return -EINVAL;
632733267325STejun Heo 	}
632833267325STejun Heo 	if (nr_matches > 1) {
63299de55351SArvind Yadav 		*reason = "ambiguous value";
633033267325STejun Heo 		return -EINVAL;
633133267325STejun Heo 	}
633233267325STejun Heo 
633333267325STejun Heo 	force_ent->param = *match_fp;
633433267325STejun Heo 
633533267325STejun Heo 	return 0;
633633267325STejun Heo }
633733267325STejun Heo 
633833267325STejun Heo static void __init ata_parse_force_param(void)
633933267325STejun Heo {
634033267325STejun Heo 	int idx = 0, size = 1;
634133267325STejun Heo 	int last_port = -1, last_device = -1;
634233267325STejun Heo 	char *p, *cur, *next;
634333267325STejun Heo 
6344168af4afSDamien Le Moal 	/* Calculate maximum number of params and allocate ata_force_tbl */
634533267325STejun Heo 	for (p = ata_force_param_buf; *p; p++)
634633267325STejun Heo 		if (*p == ',')
634733267325STejun Heo 			size++;
634833267325STejun Heo 
63496396bb22SKees Cook 	ata_force_tbl = kcalloc(size, sizeof(ata_force_tbl[0]), GFP_KERNEL);
635033267325STejun Heo 	if (!ata_force_tbl) {
635133267325STejun Heo 		printk(KERN_WARNING "ata: failed to extend force table, "
635233267325STejun Heo 		       "libata.force ignored\n");
635333267325STejun Heo 		return;
635433267325STejun Heo 	}
635533267325STejun Heo 
635633267325STejun Heo 	/* parse and populate the table */
635733267325STejun Heo 	for (cur = ata_force_param_buf; *cur != '\0'; cur = next) {
635833267325STejun Heo 		const char *reason = "";
635933267325STejun Heo 		struct ata_force_ent te = { .port = -1, .device = -1 };
636033267325STejun Heo 
636133267325STejun Heo 		next = cur;
636233267325STejun Heo 		if (ata_parse_force_one(&next, &te, &reason)) {
636333267325STejun Heo 			printk(KERN_WARNING "ata: failed to parse force "
636433267325STejun Heo 			       "parameter \"%s\" (%s)\n",
636533267325STejun Heo 			       cur, reason);
636633267325STejun Heo 			continue;
636733267325STejun Heo 		}
636833267325STejun Heo 
636933267325STejun Heo 		if (te.port == -1) {
637033267325STejun Heo 			te.port = last_port;
637133267325STejun Heo 			te.device = last_device;
637233267325STejun Heo 		}
637333267325STejun Heo 
637433267325STejun Heo 		ata_force_tbl[idx++] = te;
637533267325STejun Heo 
637633267325STejun Heo 		last_port = te.port;
637733267325STejun Heo 		last_device = te.device;
637833267325STejun Heo 	}
637933267325STejun Heo 
638033267325STejun Heo 	ata_force_tbl_size = idx;
638133267325STejun Heo }
6382c6fd2807SJeff Garzik 
6383bf89b0bfSBartlomiej Zolnierkiewicz static void ata_free_force_param(void)
6384bf89b0bfSBartlomiej Zolnierkiewicz {
6385bf89b0bfSBartlomiej Zolnierkiewicz 	kfree(ata_force_tbl);
6386bf89b0bfSBartlomiej Zolnierkiewicz }
6387bf89b0bfSBartlomiej Zolnierkiewicz #else
6388bf89b0bfSBartlomiej Zolnierkiewicz static inline void ata_parse_force_param(void) { }
6389bf89b0bfSBartlomiej Zolnierkiewicz static inline void ata_free_force_param(void) { }
6390bf89b0bfSBartlomiej Zolnierkiewicz #endif
6391bf89b0bfSBartlomiej Zolnierkiewicz 
6392c6fd2807SJeff Garzik static int __init ata_init(void)
6393c6fd2807SJeff Garzik {
6394d9027470SGwendal Grignou 	int rc;
6395270390e1STejun Heo 
639633267325STejun Heo 	ata_parse_force_param();
639733267325STejun Heo 
6398270390e1STejun Heo 	rc = ata_sff_init();
6399ad72cf98STejun Heo 	if (rc) {
6400bf89b0bfSBartlomiej Zolnierkiewicz 		ata_free_force_param();
6401ad72cf98STejun Heo 		return rc;
6402ad72cf98STejun Heo 	}
6403c6fd2807SJeff Garzik 
6404d9027470SGwendal Grignou 	libata_transport_init();
6405d9027470SGwendal Grignou 	ata_scsi_transport_template = ata_attach_transport();
6406d9027470SGwendal Grignou 	if (!ata_scsi_transport_template) {
6407d9027470SGwendal Grignou 		ata_sff_exit();
6408d9027470SGwendal Grignou 		rc = -ENOMEM;
6409d9027470SGwendal Grignou 		goto err_out;
6410d9027470SGwendal Grignou 	}
6411d9027470SGwendal Grignou 
6412c6fd2807SJeff Garzik 	printk(KERN_DEBUG "libata version " DRV_VERSION " loaded.\n");
6413c6fd2807SJeff Garzik 	return 0;
6414d9027470SGwendal Grignou 
6415d9027470SGwendal Grignou err_out:
6416d9027470SGwendal Grignou 	return rc;
6417c6fd2807SJeff Garzik }
6418c6fd2807SJeff Garzik 
6419c6fd2807SJeff Garzik static void __exit ata_exit(void)
6420c6fd2807SJeff Garzik {
6421d9027470SGwendal Grignou 	ata_release_transport(ata_scsi_transport_template);
6422d9027470SGwendal Grignou 	libata_transport_exit();
6423270390e1STejun Heo 	ata_sff_exit();
6424bf89b0bfSBartlomiej Zolnierkiewicz 	ata_free_force_param();
6425c6fd2807SJeff Garzik }
6426c6fd2807SJeff Garzik 
6427a4625085SBrian King subsys_initcall(ata_init);
6428c6fd2807SJeff Garzik module_exit(ata_exit);
6429c6fd2807SJeff Garzik 
64309990b6f3SAkinobu Mita static DEFINE_RATELIMIT_STATE(ratelimit, HZ / 5, 1);
6431c6fd2807SJeff Garzik 
6432c6fd2807SJeff Garzik int ata_ratelimit(void)
6433c6fd2807SJeff Garzik {
64349990b6f3SAkinobu Mita 	return __ratelimit(&ratelimit);
6435c6fd2807SJeff Garzik }
6436a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_ratelimit);
6437c6fd2807SJeff Garzik 
6438c0c362b6STejun Heo /**
6439c0c362b6STejun Heo  *	ata_msleep - ATA EH owner aware msleep
6440c0c362b6STejun Heo  *	@ap: ATA port to attribute the sleep to
6441c0c362b6STejun Heo  *	@msecs: duration to sleep in milliseconds
6442c0c362b6STejun Heo  *
6443c0c362b6STejun Heo  *	Sleeps @msecs.  If the current task is owner of @ap's EH, the
6444c0c362b6STejun Heo  *	ownership is released before going to sleep and reacquired
6445c0c362b6STejun Heo  *	after the sleep is complete.  IOW, other ports sharing the
6446c0c362b6STejun Heo  *	@ap->host will be allowed to own the EH while this task is
6447c0c362b6STejun Heo  *	sleeping.
6448c0c362b6STejun Heo  *
6449c0c362b6STejun Heo  *	LOCKING:
6450c0c362b6STejun Heo  *	Might sleep.
6451c0c362b6STejun Heo  */
645297750cebSTejun Heo void ata_msleep(struct ata_port *ap, unsigned int msecs)
645397750cebSTejun Heo {
6454c0c362b6STejun Heo 	bool owns_eh = ap && ap->host->eh_owner == current;
6455c0c362b6STejun Heo 
6456c0c362b6STejun Heo 	if (owns_eh)
6457c0c362b6STejun Heo 		ata_eh_release(ap);
6458c0c362b6STejun Heo 
6459848c3920SAnil Veliyankara Madam 	if (msecs < 20) {
6460848c3920SAnil Veliyankara Madam 		unsigned long usecs = msecs * USEC_PER_MSEC;
6461848c3920SAnil Veliyankara Madam 		usleep_range(usecs, usecs + 50);
6462848c3920SAnil Veliyankara Madam 	} else {
646397750cebSTejun Heo 		msleep(msecs);
6464848c3920SAnil Veliyankara Madam 	}
6465c0c362b6STejun Heo 
6466c0c362b6STejun Heo 	if (owns_eh)
6467c0c362b6STejun Heo 		ata_eh_acquire(ap);
646897750cebSTejun Heo }
6469a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_msleep);
647097750cebSTejun Heo 
6471c6fd2807SJeff Garzik /**
6472c6fd2807SJeff Garzik  *	ata_wait_register - wait until register value changes
647397750cebSTejun Heo  *	@ap: ATA port to wait register for, can be NULL
6474c6fd2807SJeff Garzik  *	@reg: IO-mapped register
6475c6fd2807SJeff Garzik  *	@mask: Mask to apply to read register value
6476c6fd2807SJeff Garzik  *	@val: Wait condition
6477341c2c95STejun Heo  *	@interval: polling interval in milliseconds
6478341c2c95STejun Heo  *	@timeout: timeout in milliseconds
6479c6fd2807SJeff Garzik  *
6480c6fd2807SJeff Garzik  *	Waiting for some bits of register to change is a common
6481c6fd2807SJeff Garzik  *	operation for ATA controllers.  This function reads 32bit LE
6482c6fd2807SJeff Garzik  *	IO-mapped register @reg and tests for the following condition.
6483c6fd2807SJeff Garzik  *
6484c6fd2807SJeff Garzik  *	(*@reg & mask) != val
6485c6fd2807SJeff Garzik  *
6486c6fd2807SJeff Garzik  *	If the condition is met, it returns; otherwise, the process is
6487c6fd2807SJeff Garzik  *	repeated after @interval_msec until timeout.
6488c6fd2807SJeff Garzik  *
6489c6fd2807SJeff Garzik  *	LOCKING:
6490c6fd2807SJeff Garzik  *	Kernel thread context (may sleep)
6491c6fd2807SJeff Garzik  *
6492c6fd2807SJeff Garzik  *	RETURNS:
6493c6fd2807SJeff Garzik  *	The final register value.
6494c6fd2807SJeff Garzik  */
649597750cebSTejun Heo u32 ata_wait_register(struct ata_port *ap, void __iomem *reg, u32 mask, u32 val,
6496341c2c95STejun Heo 		      unsigned long interval, unsigned long timeout)
6497c6fd2807SJeff Garzik {
6498341c2c95STejun Heo 	unsigned long deadline;
6499c6fd2807SJeff Garzik 	u32 tmp;
6500c6fd2807SJeff Garzik 
6501c6fd2807SJeff Garzik 	tmp = ioread32(reg);
6502c6fd2807SJeff Garzik 
6503c6fd2807SJeff Garzik 	/* Calculate timeout _after_ the first read to make sure
6504c6fd2807SJeff Garzik 	 * preceding writes reach the controller before starting to
6505c6fd2807SJeff Garzik 	 * eat away the timeout.
6506c6fd2807SJeff Garzik 	 */
6507341c2c95STejun Heo 	deadline = ata_deadline(jiffies, timeout);
6508c6fd2807SJeff Garzik 
6509341c2c95STejun Heo 	while ((tmp & mask) == val && time_before(jiffies, deadline)) {
651097750cebSTejun Heo 		ata_msleep(ap, interval);
6511c6fd2807SJeff Garzik 		tmp = ioread32(reg);
6512c6fd2807SJeff Garzik 	}
6513c6fd2807SJeff Garzik 
6514c6fd2807SJeff Garzik 	return tmp;
6515c6fd2807SJeff Garzik }
6516a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_wait_register);
6517c6fd2807SJeff Garzik 
6518c6fd2807SJeff Garzik /*
6519c6fd2807SJeff Garzik  * Dummy port_ops
6520c6fd2807SJeff Garzik  */
6521c6fd2807SJeff Garzik static unsigned int ata_dummy_qc_issue(struct ata_queued_cmd *qc)
6522c6fd2807SJeff Garzik {
6523c6fd2807SJeff Garzik 	return AC_ERR_SYSTEM;
6524c6fd2807SJeff Garzik }
6525c6fd2807SJeff Garzik 
6526182d7bbaSTejun Heo static void ata_dummy_error_handler(struct ata_port *ap)
6527182d7bbaSTejun Heo {
6528182d7bbaSTejun Heo 	/* truly dummy */
6529182d7bbaSTejun Heo }
6530182d7bbaSTejun Heo 
6531029cfd6bSTejun Heo struct ata_port_operations ata_dummy_port_ops = {
6532c6fd2807SJeff Garzik 	.qc_prep		= ata_noop_qc_prep,
6533c6fd2807SJeff Garzik 	.qc_issue		= ata_dummy_qc_issue,
6534182d7bbaSTejun Heo 	.error_handler		= ata_dummy_error_handler,
6535e4a9c373SDan Williams 	.sched_eh		= ata_std_sched_eh,
6536e4a9c373SDan Williams 	.end_eh			= ata_std_end_eh,
6537c6fd2807SJeff Garzik };
6538a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_dummy_port_ops);
6539c6fd2807SJeff Garzik 
654021b0ad4fSTejun Heo const struct ata_port_info ata_dummy_port_info = {
654121b0ad4fSTejun Heo 	.port_ops		= &ata_dummy_port_ops,
654221b0ad4fSTejun Heo };
6543a52fbcfcSBartlomiej Zolnierkiewicz EXPORT_SYMBOL_GPL(ata_dummy_port_info);
654421b0ad4fSTejun Heo 
654506296a1eSJoe Perches void ata_print_version(const struct device *dev, const char *version)
654606296a1eSJoe Perches {
654706296a1eSJoe Perches 	dev_printk(KERN_DEBUG, dev, "version %s\n", version);
654806296a1eSJoe Perches }
654906296a1eSJoe Perches EXPORT_SYMBOL(ata_print_version);
6550c206a389SHannes Reinecke 
6551c206a389SHannes Reinecke EXPORT_TRACEPOINT_SYMBOL_GPL(ata_tf_load);
6552c206a389SHannes Reinecke EXPORT_TRACEPOINT_SYMBOL_GPL(ata_exec_command);
6553c206a389SHannes Reinecke EXPORT_TRACEPOINT_SYMBOL_GPL(ata_bmdma_setup);
6554c206a389SHannes Reinecke EXPORT_TRACEPOINT_SYMBOL_GPL(ata_bmdma_start);
6555c206a389SHannes Reinecke EXPORT_TRACEPOINT_SYMBOL_GPL(ata_bmdma_status);
6556